Skip to content

Commit

Permalink
Add meow-replace-pop.
Browse files Browse the repository at this point in the history
This command, when run after `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` and `meow--replace-start-marker`.
- Modify commands `meow-replace`, `meow-replace-save`, and `meow-replace-char`
  to set the marker before inserting the replacement text.
  • Loading branch information
okamsn committed Sep 23, 2023
1 parent f553db1 commit f6fa8da
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 0 deletions.
42 changes: 42 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,50 @@ 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'."
(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
(pcase last-command
('meow-replace-pop (1+ meow--replace-pop-index))
('meow-replace 1)
('meow-replace-char 1)
('meow-replace-save 2)
(_ 0)))
(when (>= meow--replace-pop-index (length kill-ring))
(setq meow--replace-pop-index 0))
(let ((txt (string-trim-right (current-kill meow--replace-pop-index t)
"\n")))
;; If the marker's info isn't good (for example, if we didn't run a
;; "replace" command in this buffer), don't delete anything. We could
;; choose to run `meow-replace' instead, but that might not be the
;; user's preferred command.
(delete-region (if (or (null (marker-position meow--replace-start-marker))
(not (equal (marker-buffer meow--replace-start-marker)
(current-buffer))))
(point)
meow--replace-start-marker)
(point))
(set-marker meow--replace-start-marker (point))
(insert txt))))
(setq this-command 'meow-replace-pop))

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

(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 f6fa8da

Please sign in to comment.