emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master c9a8a47ba4: Add new user option 'yank-transform-functions'


From: Lars Ingebrigtsen
Subject: master c9a8a47ba4: Add new user option 'yank-transform-functions'
Date: Thu, 19 May 2022 18:15:45 -0400 (EDT)

branch: master
commit c9a8a47ba4b8cda05c48fff4259ce8f0bd079c87
Author: Lars Ingebrigtsen <larsi@gnus.org>
Commit: Lars Ingebrigtsen <larsi@gnus.org>

    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.
---
 doc/lispref/text.texi | 20 ++++++++++++++++++++
 etc/NEWS              |  3 +++
 lisp/simple.el        | 11 +++++++++++
 lisp/subr.el          |  7 ++++++-
 4 files changed, 40 insertions(+), 1 deletion(-)

diff --git a/doc/lispref/text.texi b/doc/lispref/text.texi
index a1db715db6..8fd8a5fb97 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 4f6df48129..c3bc1f0f58 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 cd7a82b7ac..bed72457c3 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 d7f06bdcde..945587db53 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))



reply via email to

[Prev in Thread] Current Thread [Next in Thread]