Skip to content

Instantly share code, notes, and snippets.

@daniel-koudouna
Last active September 19, 2021 00:53
Show Gist options
  • Save daniel-koudouna/39f03845914e34acde4d4c6a27c5176a to your computer and use it in GitHub Desktop.
Save daniel-koudouna/39f03845914e34acde4d4c6a27c5176a to your computer and use it in GitHub Desktop.
Fixes the problem of certain functions adding input before the cursor when using evil mode in emacs
(defun my:is-end-of-line ()
"Compare point with end of line."
(let* ((pos (current-column))
(end-pos (save-excursion
(evil-end-of-line)
(current-column))))
(eq pos end-pos)))
(defun my:compare-with-end-of-word ()
"Compare point with end of word."
(let* ((pos (current-column))
(end-pos (save-excursion
(evil-backward-word-begin)
(evil-forward-word-end)
(current-column))))
(- pos end-pos)))
(defun my:point-is-space ()
"Check if point is whitespace."
(char-equal ?\s (char-after)))
(defun my:insert-after (func)
"Run FUNC after the end of word, ignoring whitespace."
(interactive)
(let ((relative-loc (my:compare-with-end-of-word)))
(cond ((my:is-end-of-line)
(end-of-line)
(call-interactively func))
((eq 0 relative-loc)
(evil-forward-char)
(call-interactively func))
((and (> 0 relative-loc) (not (my:point-is-space)))
(evil-forward-word-end)
(if (my:is-end-of-line)
(end-of-line)
(evil-forward-char))
(call-interactively func))
(t
(call-interactively func)))))
;; Example usage
(defun my:reftex-citation ()
"Custom reftex to ensure citations appear at the end of words."
(interactive)
(my:insert-after 'reftex-citation))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment