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.
This commit is contained in:
Po Lu 2023-08-11 15:58:31 +08:00
parent 43cc92d6e4
commit dcd551d6c8
2 changed files with 62 additions and 0 deletions

View file

@ -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

View file

@ -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