emacs-evil / evil

The extensible vi layer for Emacs.
GNU General Public License v3.0
3.35k stars 283 forks source link

Programmatic options for `paste-after` #1920

Closed KyleBarton closed 3 months ago

KyleBarton commented 3 months ago

Issue type

Environment

Emacs version: GNU Emacs 29.2 (build 2, aarch64-apple-darwin23.1.0, NS appkit-2487.20 Version 14.1 (Build 23B2073)) of 2024-01-26 Operating System: macOS Sonoma 14.5 Evil version: Evil version 1.15.0 Evil installation type: MELPA Graphical/Terminal: graphical Tested in a make emacs session (see CONTRIBUTING.md): No

Problem

I have a few use-cases where I'd like to "insert" a value while in normal mode, in such a way that mimics the behavior of evil-paste-after -- that is, after the "normal-mode" cursor, rather than before the cursor as in when using insert. For instance, I have a quick function that pastes the current date into the buffer. I solve with a function that uses a register I don't normally use in combination with evil-paste-after:

(defun kjb/evil-paste-content-after (content)
  "Pastes content after the cursor. Meant to be used in evil normal
state."
  (evil-set-register (max-char) content)
  (evil-paste-after 1 (max-char)))

Hence, instead of (insert "foo"), I can call (kjb/evil-paste-content-after "foo") and text is pasted after the cursor.

Question

So my question is, is there a way to do this without using a register? Ultimately it's kind of a hack because I just want to paste content programmatically in these use cases, so using the register is kind of a leaky abstraction. Really the only part of evil-paste-after that I'm looking to leverage is the logic that inserts into the active buffer after the normal-mode cursor, rather than before. However, I'm not sure how feasible it is to extract that logic in a way that's similar to insert.

tomdl89 commented 3 months ago

I think you'll most likely just need these parts of evil-paste-after to get what you want:

(defun kjb/evil-insert-content-after (content)
  "Insert CONTENT after the cursor."
  (unless (eolp) (forward-char))
  (insert-for-yank content)
  (when (evil-normal-state-p) (evil-move-cursor-back)))

and I expect you can replace insert-for-yank with insert because you won't care about yank handlers. lmk if that works for you.

KyleBarton commented 3 months ago

Thanks, yeah that works great! Appreciate your help.