Skip to content

Commit ec94fa6

Browse files
authored
Add meow-replace-pop. (#509)
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 <[email protected]>
1 parent ba0cf4d commit ec94fa6

File tree

2 files changed

+57
-0
lines changed

2 files changed

+57
-0
lines changed

meow-command.el

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,7 @@ This command supports `meow-selection-command-fallback'."
541541
(when (meow--allow-modify-p)
542542
(when-let ((s (string-trim-right (current-kill 0 t) "\n")))
543543
(delete-region (region-beginning) (region-end))
544+
(set-marker meow--replace-start-marker (point))
544545
(insert s))))))
545546

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

555557
(defun meow-replace-save ()
@@ -565,10 +567,43 @@ This command supports `meow-selection-command-fallback'."
565567
(buffer-substring-no-properties (region-beginning) (region-end)))))
566568
(progn
567569
(delete-region (region-beginning) (region-end))
570+
(set-marker meow--replace-start-marker (point))
568571
(insert s)
569572
(kill-new old)))
573+
(set-marker meow--replace-start-marker (point))
570574
(insert s)))))))
571575

576+
(defun meow-replace-pop ()
577+
"Like `yank-pop', but for `meow-replace'.
578+
579+
If this command is called after `meow-replace',
580+
`meow-replace-char', `meow-replace-save', or itself, replace the
581+
previous replacement with the next item in the `kill-ring'.
582+
583+
Unlike `yank-pop', this command does not rotate the `kill-ring'.
584+
For that, see the command `rotate-yank-pointer'.
585+
586+
For custom commands, see also the user option
587+
`meow-replace-pop-command-start-indexes'."
588+
(interactive "*")
589+
(unless kill-ring (user-error "Can't replace; kill ring is empty"))
590+
(let ((select-enable-clipboard meow-use-clipboard))
591+
(when (meow--allow-modify-p)
592+
(setq meow--replace-pop-index
593+
(cond
594+
((eq last-command 'meow-replace-pop) (1+ meow--replace-pop-index))
595+
((alist-get last-command meow-replace-pop-command-start-indexes))
596+
(t (user-error "Can only run `meow-replace-pop' after itself or a command in `meow-replace-pop-command-start-indexes'"))))
597+
(when (>= meow--replace-pop-index (length kill-ring))
598+
(setq meow--replace-pop-index 0)
599+
(message "`meow-replace-pop': Reached end of kill ring"))
600+
(let ((txt (string-trim-right (current-kill meow--replace-pop-index t)
601+
"\n")))
602+
(delete-region meow--replace-start-marker (point))
603+
(set-marker meow--replace-start-marker (point))
604+
(insert txt))))
605+
(setq this-command 'meow-replace-pop))
606+
572607
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
573608
;;; CHAR MOVEMENT
574609
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

meow-var.el

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,6 +644,28 @@ The value can be nil, quick or record.")
644644
(meow-backspace . "backspace"))
645645
"A list of (command . short-name)")
646646

647+
(defcustom meow-replace-pop-command-start-indexes
648+
'((meow-replace . 1)
649+
(meow-replace-char . 1)
650+
(meow-replace-save . 2))
651+
"Alist of commands and their starting indices for use by `meow-replace-pop'.
652+
653+
If `meow-replace-pop' is run and the previous command is not
654+
`meow-replace-pop' or a command which is present in this alist,
655+
`meow-replace-pop' signals an error."
656+
:type '(alist :key-type function :value-type natnum))
657+
658+
(defvar meow--replace-pop-index nil
659+
"The index of the previous replacement in the `kill-ring'.
660+
See also the command `meow-replace-pop'.")
661+
662+
(defvar meow--replace-start-marker (make-marker)
663+
"The beginning of the replaced text.
664+
665+
This marker stays before any text inserted at the location, to
666+
account for any automatic formatting that happens after inserting
667+
the replacement text.")
668+
647669
;;; Backup variables
648670

649671
(defvar meow--backup-var-delete-activae-region nil

0 commit comments

Comments
 (0)