Skip to content

Commit

Permalink
Allow custom delete-region', insert' functions (#609)
Browse files Browse the repository at this point in the history
Make it possible to specify which functions are used to delete the
region and insert text.

This is needed to get Meow to work with modes like Vterm, which define
their own `delete-region' and `insert' functions, without massive code
duplication.
  • Loading branch information
45mg authored Oct 5, 2024
1 parent 774c454 commit 675cd12
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 19 deletions.
2 changes: 1 addition & 1 deletion meow-beacon.el
Original file line number Diff line number Diff line change
Expand Up @@ -506,7 +506,7 @@ The recorded kmacro will be applied to all cursors immediately."
(delete-overlay ov)))))))

(defun meow--beacon-delete-region ()
(delete-region (region-beginning) (region-end)))
(meow--delete-region (region-beginning) (region-end)))

(defun meow-beacon-kill-delete ()
"Delete all selections.
Expand Down
28 changes: 14 additions & 14 deletions meow-command.el
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ This command supports `meow-selection-command-fallback'."
(t
(meow--prepare-region-for-kill)
(let ((s (buffer-substring-no-properties (region-beginning) (region-end))))
(delete-region (region-beginning) (region-end))
(meow--delete-region (region-beginning) (region-end))
(kill-append (meow--prepare-string-for-kill-append s) nil))))))))

(defun meow-C-k ()
Expand Down Expand Up @@ -469,7 +469,7 @@ This command supports `meow-selection-command-fallback'."
(save-mark-and-excursion
(newline))
;; (save-mark-and-excursion
;; (insert "\n"))
;; (meow--insert "\n"))
(indent-according-to-mode)))

(defun meow-open-above-visual ()
Expand Down Expand Up @@ -515,7 +515,7 @@ This command supports `meow-selection-command-fallback'."
(when (meow--allow-modify-p)
(setq this-command #'meow-change)
(meow--with-selection-fallback
(delete-region (region-beginning) (region-end))
(meow--delete-region (region-beginning) (region-end))
(meow--switch-state 'insert)
(when meow-select-on-change
(setq-local meow--insert-pos (point))))))
Expand Down Expand Up @@ -547,19 +547,19 @@ This command supports `meow-selection-command-fallback'."
(let ((select-enable-clipboard meow-use-clipboard))
(when (meow--allow-modify-p)
(when-let ((s (string-trim-right (current-kill 0 t) "\n")))
(delete-region (region-beginning) (region-end))
(meow--delete-region (region-beginning) (region-end))
(set-marker meow--replace-start-marker (point))
(insert s))))))
(meow--insert s))))))

(defun meow-replace-char ()
"Replace current char with selection."
(interactive)
(let ((select-enable-clipboard meow-use-clipboard))
(when (< (point) (point-max))
(when-let ((s (string-trim-right (current-kill 0 t) "\n")))
(delete-region (point) (1+ (point)))
(meow--delete-region (point) (1+ (point)))
(set-marker meow--replace-start-marker (point))
(insert s)))))
(meow--insert s)))))

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

(defun meow-replace-pop ()
"Like `yank-pop', but for `meow-replace'.
Expand Down Expand Up @@ -606,9 +606,9 @@ For custom commands, see also the user option
(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))
(meow--delete-region meow--replace-start-marker (point))
(set-marker meow--replace-start-marker (point))
(insert txt))))
(meow--insert txt))))
(setq this-command 'meow-replace-pop))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -1736,8 +1736,8 @@ This command is a replacement for built-in `kmacro-end-macro'."
(region-str (when (region-active-p) (buffer-substring-no-properties rbeg rend)))
(sel-str (meow--second-sel-get-string))
(next-marker (make-marker)))
(when region-str (delete-region rbeg rend))
(when sel-str (insert sel-str))
(when region-str (meow--delete-region rbeg rend))
(when sel-str (meow--insert sel-str))
(move-marker next-marker (point))
(meow--second-sel-set-string (or region-str ""))
(when (overlayp mouse-secondary-overlay)
Expand Down
16 changes: 12 additions & 4 deletions meow-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,14 @@ Looks up the state in meow-replace-state-name-list"
(when-let ((bounds (bounds-of-thing-at-point thing)))
(cons type bounds)))

(defun meow--insert (&rest args)
"Use `meow--insert-function' to insert ARGS at point."
(apply meow--insert-function args))

(defun meow--delete-region (start end)
"Use `meow--delete-region-function' to delete text between START and END."
(funcall meow--delete-region-function start end))

(defun meow--push-search (search)
(unless (string-equal search (car regexp-search-ring))
(add-to-history 'regexp-search-ring search regexp-search-ring-max)))
Expand Down Expand Up @@ -424,7 +432,7 @@ Looks up the state in meow-replace-state-name-list"
(when (or (member this-command meow-grab-fill-commands)
(member meow--keypad-this-command meow-grab-fill-commands))
(when-let ((s (meow--second-sel-get-string)))
(insert s))))
(meow--insert s))))

(defun meow--parse-string-to-keypad-keys (str)
(let ((strs (split-string str " ")))
Expand Down Expand Up @@ -541,12 +549,12 @@ that bound to DEF. Otherwise, return DEF."
((meow--second-sel-buffer)
(with-current-buffer (overlay-buffer mouse-secondary-overlay)
(goto-char (overlay-start mouse-secondary-overlay))
(delete-region (overlay-start mouse-secondary-overlay) (overlay-end mouse-secondary-overlay))
(insert string)))
(meow--delete-region (overlay-start mouse-secondary-overlay) (overlay-end mouse-secondary-overlay))
(meow--insert string)))
((markerp mouse-secondary-start)
(with-current-buffer (marker-buffer mouse-secondary-start)
(goto-char (marker-position mouse-secondary-start))
(insert string)))))
(meow--insert string)))))

(defun meow--second-sel-get-string ()
(when (meow--second-sel-buffer)
Expand Down
11 changes: 11 additions & 0 deletions meow-var.el
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,17 @@ Use (setq meow-keypad-describe-keymap-function \\='nil) to disable popup.")
(defvar meow--kbd-goto-line "M-g g"
"KBD macro for command `goto-line'.")

(defvar meow--delete-region-function #'delete-region
"The function used to delete the selection.
Allows support of modes that define their own equivalent of
`delete-region'.")

(defvar meow--insert-function #'insert
"The function used to insert text in Normal state.
Allows support of modes that define their own equivalent of `insert'.")

(defvar-local meow--indicator nil
"Indicator for current buffer.")

Expand Down

0 comments on commit 675cd12

Please sign in to comment.