reddit / emacs

How to keep point at EOL after C-e?

pyz3n · 2022-06-04 21:08
I'm trying to achieve something like `track-eol`: > When line-move-visual is nil, you can also set the variable track-eol to a non-nil value. Then C-n and C-p, when starting at the end of the logical line, move to the end of the next logical line. Normally, track-eol is nil. but with two differences: * it should be activated after a `C-e` (easy, just advise `move-end-of-line`), and automatically disabled after any horizontal motion (I could advise every horizontal movement command, but is there a better way?). Essentially, if I move to the end of line with `C-f` I don't want `track-eol`-like behaviour. * it should work with `line-move-visual` set, which probably means using `goal-column`. I tried setting `temporary-goal-column` after `C-e`, but it gets ignored if the last command isn't one of an handful (`move-end-of-line` not included). This is unfortunate since I think `temporary-goal-column` has pretty much the behaviour I want. How would you do this? EDIT: I ended up with this (defvar ad/sticky-eol-active nil) (defun ad/sticky-eol () (if (and ad/sticky-eol-active (memq this-command '(previous-line next-line boon-repeat-command))) (end-of-line) (setq ad/sticky-eol-active (eq this-command 'end-of-line)))) (add-hook 'post-command-hook #'ad/sticky-eol)
thetemp_ · 2022-06-05 00:03
> it should be activated after a `C-e` (easy, just advise `move-end-of-line`), and automatically disabled after any horizontal motion (I could advise every horizontal movement command, but is there a better way?) Why not just create a function to go on post-command-hook? If the last command was move-end-of-line, it sets the variable "my-tracking-eol-p" (or whatever you name it) to a non-nil value. If the last command is in your list of horizontal movement commands, it sets the variable to nil. If the last command is in your list of vertical movement commands AND my-tracking-eol-p has a non-nil value, then it moves point to eol. Otherwise, it does nothing.
pyz3n · 2022-06-05 11:09
Thank you, I didn't know about `post-command-hook`. I hoped explicitly listing horizontal and vertical movement commands wouldn't be required, but oh well.
thetemp_ · 2022-06-05 17:55
> I hoped explicitly listing horizontal and vertical movement commands wouldn't be required Another approach could be to record the column and line numbers each time and compare them between commands (See functions: `current-column` and `line-number-at-pos`). Probably also need to make sure the buffer hasn't changed.
00-11 · 2022-06-05 18:09
An alternative you might like: Bind `C-a` and `C-e` to *repeatable* versions, `beginning-of-line+` and `end-of-line+`, from library [`misc-cmds.el`](https://www.emacswiki.org/emacs/download/misc-cmds.el). Then you can still have the usual tracking and yet also have end-of-line and beginning-of-line tracking.
pyz3n · 2022-06-05 23:11
Nice, it's a cleaner way to do it for sure. It requires an extra keypress (negative argument) to move to the end of the previous line though.