I'm trying to get Emacs to imitate Vim's EOL behavior. Namely at the end of line ("\n"), the cursor comes to a halt instead of progressing to the start of the next line.
The following works fine and dandy but the cursor gets stuck before empty lines:
(defun make-eol-intangible ()
(interactive)
(add-hook 'post-command-hook
(lambda ()
(when (and (point) (looking-at "\n"))
(backward-char)) t t)))
I could put an additional condition that tests for (point) looking-back however Emacs docs mention that looking-back should be avoided, and is possibly deprecated. On that note it's probably less optimal to use looking-at either.
I would like some suggestions.
Amended function (doesn't work as expected):
(defun make-eol-intangible ()
(interactive)
(add-hook 'post-command-hook
(lambda ()
(when (and (eq (char-before (point)) ?\n)
(eq (char-after (point)) ?\.))
(backward-char)))))
(make-eol-intangible)
forward-char
is invoked? Why are you checking(point)
in a condition? It will always return non-nil
.looking-at
uses regexps, but you don't need a regexp to detect a newline.following-char
,char-after
,preceding-char
, andchar-before
can all be used to detect a newline character after or before point, respectively. – Basil Jan 2 at 13:24