Skip to content

Commit

Permalink
Allow custom delete-region', insert' functions
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, that define
their own `delete-region' and `insert' functions, without massive code
duplication.
  • Loading branch information
45mg committed May 27, 2024
1 parent 99e08c9 commit 82816e0
Show file tree
Hide file tree
Showing 4 changed files with 30 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 @@ -500,7 +500,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)))
(funcall meow--delete-region-function (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))
(funcall meow--delete-region-function (region-beginning) (region-end))
(kill-append (meow--prepare-string-for-kill-append s) nil))))))))

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

(defun meow-open-above-visual ()
Expand Down Expand Up @@ -511,7 +511,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))
(funcall meow--delete-region-function (region-beginning) (region-end))
(meow--switch-state 'insert)
(setq-local meow--insert-pos (point)))))

Expand Down Expand Up @@ -540,19 +540,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))
(funcall meow--delete-region-function (region-beginning) (region-end))
(set-marker meow--replace-start-marker (point))
(insert s))))))
(funcall meow--insert-function 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)))
(funcall meow--delete-region-function (point) (1+ (point)))
(set-marker meow--replace-start-marker (point))
(insert s)))))
(funcall meow--insert-function s)))))

(defun meow-replace-save ()
(interactive)
Expand All @@ -566,12 +566,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))
(funcall meow--delete-region-function (region-beginning) (region-end))
(set-marker meow--replace-start-marker (point))
(insert s)
(funcall meow--insert-function s)
(kill-new old)))
(set-marker meow--replace-start-marker (point))
(insert s)))))))
(funcall meow--insert-function s)))))))

(defun meow-replace-pop ()
"Like `yank-pop', but for `meow-replace'.
Expand Down Expand Up @@ -599,9 +599,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))
(funcall meow--delete-region-function meow--replace-start-marker (point))
(set-marker meow--replace-start-marker (point))
(insert txt))))
(funcall meow--insert-function txt))))
(setq this-command 'meow-replace-pop))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
Expand Down Expand Up @@ -1748,8 +1748,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 (funcall meow--delete-region-function rbeg rend))
(when sel-str (funcall meow--insert-function sel-str))
(move-marker next-marker (point))
(meow--second-sel-set-string (or region-str ""))
(when (overlayp mouse-secondary-overlay)
Expand Down
8 changes: 4 additions & 4 deletions meow-util.el
Original file line number Diff line number Diff line change
Expand Up @@ -414,7 +414,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))))
(funcall meow--insert-function s))))

(defun meow--parse-string-to-keypad-keys (str)
(let ((strs (split-string str " ")))
Expand Down Expand Up @@ -531,12 +531,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)))
(funcall meow--delete-region-function (overlay-start mouse-secondary-overlay) (overlay-end mouse-secondary-overlay))
(funcall meow--insert-function string)))
((markerp mouse-secondary-start)
(with-current-buffer (marker-buffer mouse-secondary-start)
(goto-char (marker-position mouse-secondary-start))
(insert string)))))
(funcall meow--insert-function 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 @@ -480,6 +480,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 82816e0

Please sign in to comment.