emacs-diffs
[Top][All Lists]
Advanced

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

master dcd551d6c85: New global minor mode `kill-ring-deindent-mode'


From: Po Lu
Subject: master dcd551d6c85: New global minor mode `kill-ring-deindent-mode'
Date: Fri, 11 Aug 2023 04:00:42 -0400 (EDT)

branch: master
commit dcd551d6c852cfa321d5552e2dfe273609396b45
Author: Po Lu <luangruo@yahoo.com>
Commit: Po Lu <luangruo@yahoo.com>

    New global minor mode `kill-ring-deindent-mode'
    
    * etc/NEWS: Announce the new minor mode.
    
    * lisp/simple.el (kill-ring-deindent-buffer-substring-function):
    New function.
    (kill-ring-deindent-mode): New minor mode, for trimming excess
    indentation from saved text.
---
 etc/NEWS       | 23 +++++++++++++++++++++++
 lisp/simple.el | 39 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 5c7cee3e63b..ffd9632dc05 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -145,6 +145,29 @@ right-aligned to is controlled by the new user option
 
 * Editing Changes in Emacs 30.1
 
+---
+** New global minor mode 'kill-ring-deindent-mode'.
+When enabled, text being saved to the kill ring will be de-indented by
+the number of columns its first line is indented.  For example,
+saving the entire function call within:
+
+foo ()
+{
+  long_function_with_several_arguments (argument_1_compute (),
+                                       argument_2_compute (),
+                                       argument_3_compute ());
+}
+
+will save:
+
+long_function_with_several_arguments (argument_1_compute (),
+                                     argument_2_compute (),
+                                     argument_3_compute ())
+
+to the kill ring, omitting the two columns of extra indentation that
+would otherwise be present in the second and third lines of the
+function call.
+
 +++
 ** Emacs now has better support for touchscreen devices.
 Many touch screen gestures are now implemented and translated into
diff --git a/lisp/simple.el b/lisp/simple.el
index e21976c30ee..61e3af07df2 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -11142,6 +11142,45 @@ seconds."
 
 
 
+;; Indent Filter mode.  When enabled, this minor mode filters all
+;; killed text to remove leading indentation.
+
+(defun kill-ring-deindent-buffer-substring-function (beg end delete)
+  "Save the text within BEG and END to the kill ring.
+Maybe delete it if DELETE is non-nil.
+
+Before saving the text, indent it leftwards by the number of
+columns of indentation at BEG."
+  (let ((a beg)
+        (b end))
+    (setq beg (min a b)
+          end (max a b)))
+  (let ((indentation (save-excursion (goto-char beg)
+                                     (current-indentation)))
+        (text (if delete
+                  (delete-and-extract-region beg end)
+                (buffer-substring beg end))))
+    (with-temp-buffer
+      (insert text)
+      (indent-rigidly (point-min) (point-max)
+                      (- indentation))
+      (buffer-string))))
+
+(define-minor-mode kill-ring-deindent-mode
+  "Toggle removal of indentation from text saved to the kill ring.
+
+When this minor mode is enabled, text saved into the kill ring is
+indented towards the left by the number of columns the line at
+the start of the text being saved is in turn indented."
+  :global 't
+  :group 'killing
+  (if kill-ring-deindent-mode
+      (add-function :override filter-buffer-substring-function
+                    #'kill-ring-deindent-buffer-substring-function
+                    '(:depth 100))
+    (remove-function filter-buffer-substring-function
+                     #'kill-ring-deindent-buffer-substring-function)))
+
 (provide 'simple)
 
 ;;; simple.el ends here



reply via email to

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