From: Lars Ingebrigtsen Date: Thu, 19 May 2022 22:15:28 +0000 (+0200) Subject: Add new user option 'yank-transform-functions' X-Git-Tag: emacs-29.0.90~1910^2~582 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=c9a8a47ba4b8cda05c48fff4259ce8f0bd079c87;p=emacs.git Add new user option 'yank-transform-functions' * doc/lispref/text.texi (Yanking): Mention it. (Yanking): Document it. * lisp/simple.el (yank-transform-functions): New user option. (yank): Mention it. * lisp/subr.el (insert-for-yank): Use it. --- diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi index a1db715db6e..8fd8a5fb97a 100644 --- a/doc/lispref/text.texi +++ b/doc/lispref/text.texi @@ -1034,6 +1034,9 @@ text in @var{string} according to the @code{yank-handler} text property, as well as the variables @code{yank-handled-properties} and @code{yank-excluded-properties} (see below), before inserting the result into the current buffer. + +@var{string} will be run through @code{yank-transform-functions} (see +below) before inserting. @end defun @defun insert-buffer-substring-as-yank buf &optional start end @@ -1108,6 +1111,23 @@ or specifying key bindings. It takes effect after @code{yank-handled-properties}. @end defopt +@defopt yank-transform-functions +This variable is a list of functions. Each function is called (in +order) with the string to be yanked as the parameter, and should +return a (possibly transformed) string. This variable can be set +globally, but can also be used to create new commands that are +variations on @code{yank}. For instance, to create a command that +works like @code{yank}, but cleans up whitespace before inserting, you +could say something like: + +@lisp +(defun yank-with-clean-whitespace () + (interactive) + (let ((yank-transform-functions + '(string-clean-whitespace))) + (call-interactively #'yank))) +@end lisp +@end defopt @node Yank Commands @subsection Functions for Yanking diff --git a/etc/NEWS b/etc/NEWS index 4f6df48129e..c3bc1f0f58b 100644 --- a/etc/NEWS +++ b/etc/NEWS @@ -2190,6 +2190,9 @@ platforms. This command lets you examine all data in the current selection and the clipboard, and insert it into the buffer. +** New user option 'yank-transform-functions'. +This function allows the user to alter the string to be inserted. + --- ** New function 'minibuffer-lazy-highlight-setup'. This function allows setting up the minibuffer so that lazy diff --git a/lisp/simple.el b/lisp/simple.el index cd7a82b7aca..bed72457c3a 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -5928,6 +5928,15 @@ See also `yank-handled-properties'." :group 'killing :version "24.3") +(defcustom yank-transform-functions nil + "List of functions to run on strings to be yanked. +Each function in this list will be called (in order) with the +string to be yanked as the sole argument, and should return the (possibly) + transformed string." + :type '(repeat function) + :version "29.1" + :group 'killing) + (defvar yank-window-start nil) (defvar yank-undo-function nil "If non-nil, function used by `yank-pop' to delete last stretch of yanked text. @@ -5999,6 +6008,8 @@ property, as described below. Properties listed in `yank-handled-properties' are processed, then those listed in `yank-excluded-properties' are discarded. +STRING will be run through `yank-transform-functions'. + If STRING has a non-nil `yank-handler' property anywhere, the normal insert behavior is altered, and instead, for each contiguous segment of STRING that has a given value of the `yank-handler' diff --git a/lisp/subr.el b/lisp/subr.el index d7f06bdcde8..945587db53c 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -4070,7 +4070,12 @@ remove properties specified by `yank-excluded-properties'." This function is like `insert', except it honors the variables `yank-handled-properties' and `yank-excluded-properties', and the -`yank-handler' text property, in the way that `yank' does." +`yank-handler' text property, in the way that `yank' does. + +It also runs the string through `yank-transform-functions'." + ;; Allow altering the yank string. + (dolist (func yank-transform-functions) + (setq string (funcall func string))) (let (to) (while (setq to (next-single-property-change 0 'yank-handler string)) (insert-for-yank-1 (substring string 0 to))