Skip to content

Commit

Permalink
Add meow-replace-pop. (#509)
Browse files Browse the repository at this point in the history
This command, when run after a replacement command, such as `meow-replace` or
itself, replaces the just inserted text with the next item in the kill ring,
without rotating the kill ring.

- Add command `meow-replace`.
- Add variables `meow--replace-pop-index`, `meow--replace-start-marker`,
  and `meow-replace-pop-command-start-indexes`.
- Modify commands `meow-replace`, `meow-replace-save`, and `meow-replace-char`
  to set the marker before inserting the replacement text.

Co-authored-by: okamsn <okamsn@users.noreply.github.com>
  • Loading branch information
okamsn and okamsn authored Sep 25, 2023
1 parent ba0cf4d commit ec94fa6
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 0 deletions.
35 changes: 35 additions & 0 deletions meow-command.el
Original file line number Diff line number Diff line change
Expand Up @@ -541,6 +541,7 @@ This command supports `meow-selection-command-fallback'."
(when (meow--allow-modify-p)
(when-let ((s (string-trim-right (current-kill 0 t) "\n")))
(delete-region (region-beginning) (region-end))
(set-marker meow--replace-start-marker (point))
(insert s))))))

(defun meow-replace-char ()
Expand All @@ -550,6 +551,7 @@ This command supports `meow-selection-command-fallback'."
(when (< (point) (point-max))
(when-let ((s (string-trim-right (current-kill 0 t) "\n")))
(delete-region (point) (1+ (point)))
(set-marker meow--replace-start-marker (point))
(insert s)))))

(defun meow-replace-save ()
Expand All @@ -565,10 +567,43 @@ This command supports `meow-selection-command-fallback'."
(buffer-substring-no-properties (region-beginning) (region-end)))))
(progn
(delete-region (region-beginning) (region-end))
(set-marker meow--replace-start-marker (point))
(insert s)
(kill-new old)))
(set-marker meow--replace-start-marker (point))
(insert s)))))))

(defun meow-replace-pop ()
"Like `yank-pop', but for `meow-replace'.
If this command is called after `meow-replace',
`meow-replace-char', `meow-replace-save', or itself, replace the
previous replacement with the next item in the `kill-ring'.
Unlike `yank-pop', this command does not rotate the `kill-ring'.
For that, see the command `rotate-yank-pointer'.
For custom commands, see also the user option
`meow-replace-pop-command-start-indexes'."
(interactive "*")
(unless kill-ring (user-error "Can't replace; kill ring is empty"))
(let ((select-enable-clipboard meow-use-clipboard))
(when (meow--allow-modify-p)
(setq meow--replace-pop-index
(cond
((eq last-command 'meow-replace-pop) (1+ meow--replace-pop-index))
((alist-get last-command meow-replace-pop-command-start-indexes))
(t (user-error "Can only run `meow-replace-pop' after itself or a command in `meow-replace-pop-command-start-indexes'"))))
(when (>= meow--replace-pop-index (length kill-ring))
(setq meow--replace-pop-index 0)
(message "`meow-replace-pop': Reached end of kill ring"))
(let ((txt (string-trim-right (current-kill meow--replace-pop-index t)
"\n")))
(delete-region meow--replace-start-marker (point))
(set-marker meow--replace-start-marker (point))
(insert txt))))
(setq this-command 'meow-replace-pop))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; CHAR MOVEMENT
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down
22 changes: 22 additions & 0 deletions meow-var.el
Original file line number Diff line number Diff line change
Expand Up @@ -644,6 +644,28 @@ The value can be nil, quick or record.")
(meow-backspace . "backspace"))
"A list of (command . short-name)")

(defcustom meow-replace-pop-command-start-indexes
'((meow-replace . 1)
(meow-replace-char . 1)
(meow-replace-save . 2))
"Alist of commands and their starting indices for use by `meow-replace-pop'.
If `meow-replace-pop' is run and the previous command is not
`meow-replace-pop' or a command which is present in this alist,
`meow-replace-pop' signals an error."
:type '(alist :key-type function :value-type natnum))

(defvar meow--replace-pop-index nil
"The index of the previous replacement in the `kill-ring'.
See also the command `meow-replace-pop'.")

(defvar meow--replace-start-marker (make-marker)
"The beginning of the replaced text.
This marker stays before any text inserted at the location, to
account for any automatic formatting that happens after inserting
the replacement text.")

;;; Backup variables

(defvar meow--backup-var-delete-activae-region nil
Expand Down

0 comments on commit ec94fa6

Please sign in to comment.