diff options
| author | Po Lu | 2023-08-11 15:58:31 +0800 |
|---|---|---|
| committer | Po Lu | 2023-08-11 15:58:57 +0800 |
| commit | dcd551d6c852cfa321d5552e2dfe273609396b45 (patch) | |
| tree | 2495421548a6c0d91ceefe1aebc0a4eb3b9d60bc | |
| parent | 43cc92d6e41804cf1034e1d6e3cdf35e299bd196 (diff) | |
| download | emacs-dcd551d6c852cfa321d5552e2dfe273609396b45.tar.gz emacs-dcd551d6c852cfa321d5552e2dfe273609396b45.zip | |
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.
| -rw-r--r-- | etc/NEWS | 23 | ||||
| -rw-r--r-- | lisp/simple.el | 39 |
2 files changed, 62 insertions, 0 deletions
| @@ -145,6 +145,29 @@ right-aligned to is controlled by the new user option | |||
| 145 | 145 | ||
| 146 | * Editing Changes in Emacs 30.1 | 146 | * Editing Changes in Emacs 30.1 |
| 147 | 147 | ||
| 148 | --- | ||
| 149 | ** New global minor mode 'kill-ring-deindent-mode'. | ||
| 150 | When enabled, text being saved to the kill ring will be de-indented by | ||
| 151 | the number of columns its first line is indented. For example, | ||
| 152 | saving the entire function call within: | ||
| 153 | |||
| 154 | foo () | ||
| 155 | { | ||
| 156 | long_function_with_several_arguments (argument_1_compute (), | ||
| 157 | argument_2_compute (), | ||
| 158 | argument_3_compute ()); | ||
| 159 | } | ||
| 160 | |||
| 161 | will save: | ||
| 162 | |||
| 163 | long_function_with_several_arguments (argument_1_compute (), | ||
| 164 | argument_2_compute (), | ||
| 165 | argument_3_compute ()) | ||
| 166 | |||
| 167 | to the kill ring, omitting the two columns of extra indentation that | ||
| 168 | would otherwise be present in the second and third lines of the | ||
| 169 | function call. | ||
| 170 | |||
| 148 | +++ | 171 | +++ |
| 149 | ** Emacs now has better support for touchscreen devices. | 172 | ** Emacs now has better support for touchscreen devices. |
| 150 | Many touch screen gestures are now implemented and translated into | 173 | 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." | |||
| 11142 | 11142 | ||
| 11143 | 11143 | ||
| 11144 | 11144 | ||
| 11145 | ;; Indent Filter mode. When enabled, this minor mode filters all | ||
| 11146 | ;; killed text to remove leading indentation. | ||
| 11147 | |||
| 11148 | (defun kill-ring-deindent-buffer-substring-function (beg end delete) | ||
| 11149 | "Save the text within BEG and END to the kill ring. | ||
| 11150 | Maybe delete it if DELETE is non-nil. | ||
| 11151 | |||
| 11152 | Before saving the text, indent it leftwards by the number of | ||
| 11153 | columns of indentation at BEG." | ||
| 11154 | (let ((a beg) | ||
| 11155 | (b end)) | ||
| 11156 | (setq beg (min a b) | ||
| 11157 | end (max a b))) | ||
| 11158 | (let ((indentation (save-excursion (goto-char beg) | ||
| 11159 | (current-indentation))) | ||
| 11160 | (text (if delete | ||
| 11161 | (delete-and-extract-region beg end) | ||
| 11162 | (buffer-substring beg end)))) | ||
| 11163 | (with-temp-buffer | ||
| 11164 | (insert text) | ||
| 11165 | (indent-rigidly (point-min) (point-max) | ||
| 11166 | (- indentation)) | ||
| 11167 | (buffer-string)))) | ||
| 11168 | |||
| 11169 | (define-minor-mode kill-ring-deindent-mode | ||
| 11170 | "Toggle removal of indentation from text saved to the kill ring. | ||
| 11171 | |||
| 11172 | When this minor mode is enabled, text saved into the kill ring is | ||
| 11173 | indented towards the left by the number of columns the line at | ||
| 11174 | the start of the text being saved is in turn indented." | ||
| 11175 | :global 't | ||
| 11176 | :group 'killing | ||
| 11177 | (if kill-ring-deindent-mode | ||
| 11178 | (add-function :override filter-buffer-substring-function | ||
| 11179 | #'kill-ring-deindent-buffer-substring-function | ||
| 11180 | '(:depth 100)) | ||
| 11181 | (remove-function filter-buffer-substring-function | ||
| 11182 | #'kill-ring-deindent-buffer-substring-function))) | ||
| 11183 | |||
| 11145 | (provide 'simple) | 11184 | (provide 'simple) |
| 11146 | 11185 | ||
| 11147 | ;;; simple.el ends here | 11186 | ;;; simple.el ends here |