From: Eshel Yaron Date: Sun, 15 Sep 2024 08:26:10 +0000 (+0200) Subject: Simplify 'kill-region' X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=9d863409b4c8e7e3e5a9899449d23301152aa68b;p=emacs.git Simplify 'kill-region' --- diff --git a/lisp/avy.el b/lisp/avy.el index cba9348d44b..02937a1ec8d 100644 --- a/lisp/avy.el +++ b/lisp/avy.el @@ -2058,8 +2058,7 @@ newline." (if (not (numberp start)) (user-error "Fail to select the line to kill") (save-excursion - (let ((kill-read-only-ok t) - (buffer-read-only t)) + (let ((buffer-read-only t)) (goto-char start) (kill-whole-line arg)))))) (select-window initial-window))) diff --git a/lisp/bindings.el b/lisp/bindings.el index fabaa93eeac..a41f113f303 100644 --- a/lisp/bindings.el +++ b/lisp/bindings.el @@ -1411,16 +1411,6 @@ if `inhibit-field-text-motion' is non-nil." ;; can use S-tab instead to access that binding. (define-key function-key-map [S-tab] [backtab]) -(defun ignore-preserving-kill-region (&rest _) - "Like `ignore', but don't overwrite `last-event' if it's `kill-region'." - (declare (completion ignore)) - (interactive) - (when (eq last-command 'kill-region) - (setq this-command 'kill-region)) - nil) - -(define-key global-map [mouse-movement] #'ignore-preserving-kill-region) - (define-key global-map "\C-t" 'transpose-chars) (define-key esc-map "t" 'transpose-words) (define-key esc-map "\C-t" 'transpose-sexps) diff --git a/lisp/delsel.el b/lisp/delsel.el index 3c0206bda92..8a7002eb965 100644 --- a/lisp/delsel.el +++ b/lisp/delsel.el @@ -105,9 +105,7 @@ the active region is killed instead of deleted." (interactive "P") (cond (killp - ;; Don't allow `kill-region' to change the value of `this-command'. - (let (this-command) - (kill-region (point) (mark) t))) + (kill-region (point) (mark))) (delete-selection-save-to-register (set-register delete-selection-save-to-register (funcall region-extract-function t)) diff --git a/lisp/dired.el b/lisp/dired.el index b33d043790b..72ce20449b4 100644 --- a/lisp/dired.el +++ b/lisp/dired.el @@ -3454,9 +3454,7 @@ You can then feed the file name(s) to other commands with \\[yank]." files " ")))) (unless (string= string "") - (if (eq last-command 'kill-region) - (kill-append string nil) - (kill-new string)) + (kill-new string) (message "%s" string)))) diff --git a/lisp/emulation/cua-base.el b/lisp/emulation/cua-base.el index c674a6b7a4d..532d2c34d3f 100644 --- a/lisp/emulation/cua-base.el +++ b/lisp/emulation/cua-base.el @@ -824,9 +824,9 @@ With numeric prefix arg, copy to register 0-9 instead." (cua--register (copy-to-register cua--register start end t 'region)) ((eq this-original-command 'clipboard-kill-region) - (clipboard-kill-region start end 'region)) + (clipboard-kill-region start end)) (t - (kill-region start end 'region)))) + (kill-region start end)))) (cua--deactivate))) ;;; Generic commands for regions, rectangles, and global marks diff --git a/lisp/emulation/cua-rect.el b/lisp/emulation/cua-rect.el index e50924dbd1f..bdb87f99e69 100644 --- a/lisp/emulation/cua-rect.el +++ b/lisp/emulation/cua-rect.el @@ -1467,10 +1467,6 @@ With prefix arg, indent to that column." (setq killed-rectangle strs) (setq cua--last-killed-rectangle (cons (and kill-ring (car kill-ring)) killed-rectangle)) - (when (eq last-command 'kill-region) - ;; Try to prevent kill-region from appending this to some - ;; earlier element. - (setq last-command 'kill-region-dont-append)) (when strs (put-text-property 0 (length str) 'yank-handler `(rectangle--insert-for-yank ,strs t) diff --git a/lisp/emulation/viper-cmd.el b/lisp/emulation/viper-cmd.el index 192eb99a570..bfe1cca9f87 100644 --- a/lisp/emulation/viper-cmd.el +++ b/lisp/emulation/viper-cmd.el @@ -1264,8 +1264,6 @@ as a Meta key and any number of multiple escapes are allowed." (t (setq viper-use-register nil) (error viper-InvalidRegister viper-use-register))) (setq viper-use-register nil))) - (setq last-command - (if (eq last-command 'd-command) 'kill-region nil)) (setq chars-deleted (abs (- (point) viper-com-point))) (if (> chars-deleted viper-change-notification-threshold) (unless (viper-is-in-minibuffer) @@ -1291,8 +1289,6 @@ as a Meta key and any number of multiple escapes are allowed." (t (setq viper-use-register nil) (error viper-InvalidRegister viper-use-register))) (setq viper-use-register nil))) - (setq last-command - (if (eq last-command 'D-command) 'kill-region nil)) (setq lines-deleted (count-lines (point) viper-com-point)) (if (> lines-deleted viper-change-notification-threshold) (unless (viper-is-in-minibuffer) diff --git a/lisp/ibuf-ext.el b/lisp/ibuf-ext.el index 9cff15c76be..b8fc23ec763 100644 --- a/lisp/ibuf-ext.el +++ b/lisp/ibuf-ext.el @@ -1758,9 +1758,7 @@ You can then feed the file name(s) to other commands with \\[yank]." (string (mapconcat #'identity (delete "" file-names) " "))) (unless (string= string "") - (if (eq last-command 'kill-region) - (kill-append string nil) - (kill-new string)) + (kill-new string) (message "%s" string)))) ;;;###autoload @@ -1776,9 +1774,7 @@ You can then feed the file name(s) to other commands with \\[yank]." (list (ibuffer-current-buffer)))))) (string (mapconcat #'buffer-name buffers " "))) (unless (string= string "") - (if (eq last-command 'kill-region) - (kill-append string nil) - (kill-new string)) + (kill-new string) (message "%s" string)))) (defun ibuffer-mark-on-buffer (func &optional ibuffer-mark-on-buffer-mark group) diff --git a/lisp/menu-bar.el b/lisp/menu-bar.el index 6c27bd6d921..7069cbf08c5 100644 --- a/lisp/menu-bar.el +++ b/lisp/menu-bar.el @@ -640,13 +640,11 @@ BEG and END, and saves the current region instead." (let ((select-enable-clipboard t)) (kill-ring-save beg end region))) -(defun clipboard-kill-region (beg end &optional region) - "Kill the region, and save it in the GUI's clipboard. -If the optional argument REGION is non-nil, the function ignores -BEG and END, and kills the current region instead." +(defun clipboard-kill-region (beg end &optional _ignored) + "Kill the region from BEG to END, and save it in the GUI's clipboard." (interactive "r\np") (let ((select-enable-clipboard t)) - (kill-region beg end region))) + (kill-region beg end))) (defun menu-bar-enable-clipboard () "Make CUT, PASTE and COPY (keys and menu bar items) use the clipboard. diff --git a/lisp/mouse.el b/lisp/mouse.el index 65d77762a5e..a6a956f9edf 100644 --- a/lisp/mouse.el +++ b/lisp/mouse.el @@ -1635,7 +1635,6 @@ is dragged over to." (mouse-drag-and-drop-region start-event) ;; Give temporary modes such as isearch a chance to turn off. (run-hooks 'mouse-leave-buffer-hook) - (ignore-preserving-kill-region) (mouse-drag-track start-event))) ;; Inhibit the region-confinement when undoing mouse-drag-region @@ -1846,8 +1845,7 @@ The region will be defined with mark and point." nil start-point)) ((>= mouse-row bottom) (mouse-scroll-subr start-window (1+ (- mouse-row bottom)) - nil start-point)))))) - (ignore-preserving-kill-region))) + nil start-point)))))))) map) t (lambda () (funcall cleanup) @@ -2191,7 +2189,7 @@ if `mouse-drag-copy-region' is non-nil)." ;; Region already saved in the previous click; ;; don't make a duplicate entry, just delete. (funcall region-extract-function 'delete-only) - (kill-region (mark t) (point) 'region)) + (kill-region (mark t) (point))) (setq mouse-selection-click-count 0) (setq mouse-save-then-kill-posn nil)) diff --git a/lisp/org/org-archive.el b/lisp/org/org-archive.el index 53c825ed6f6..c5b69105fc1 100644 --- a/lisp/org/org-archive.el +++ b/lisp/org/org-archive.el @@ -304,7 +304,7 @@ direct children of this heading." ;; We first only copy, in case something goes wrong ;; we need to protect `this-command', to avoid kill-region sets it, ;; which would lead to duplication of subtrees - (let (this-command) (org-copy-subtree 1 nil t)) + (org-copy-subtree 1 nil t) (set-buffer buffer) ;; Enforce Org mode for the archive buffer (if (not (derived-mode-p 'org-mode)) diff --git a/lisp/rect.el b/lisp/rect.el index 93007824679..00def3b527e 100644 --- a/lisp/rect.el +++ b/lisp/rect.el @@ -333,20 +333,10 @@ deleted. If the buffer is read-only, Emacs will beep and refrain from deleting the rectangle, but put it in `killed-rectangle' anyway. This means that -you can use this command to copy text from a read-only buffer. -\(If the variable `kill-read-only-ok' is non-nil, then this won't -even beep.)" +you can use this command to copy text from a read-only buffer." (interactive "r\nP") - (condition-case nil - (let (indent-tabs-mode) - (setq killed-rectangle (delete-extract-rectangle start end fill))) - ((buffer-read-only text-read-only) - (setq deactivate-mark t) - (setq killed-rectangle (extract-rectangle start end)) - (if kill-read-only-ok - (progn (message "Read only text copied to `killed-rectangle'") nil) - (barf-if-buffer-read-only) - (signal 'text-read-only (list (current-buffer))))))) + (let (indent-tabs-mode) + (setq killed-rectangle (delete-extract-rectangle start end fill)))) ;;;###autoload (defun copy-rectangle-as-kill (start end) @@ -813,10 +803,6 @@ Ignores `line-move-visual'." (let (rectangle-mark-mode) (region-beginning)) (let (rectangle-mark-mode) (region-end)))) (str (mapconcat #'identity strs "\n"))) - (when (eq last-command 'kill-region) - ;; Try to prevent kill-region from appending this to some - ;; earlier element. - (setq last-command 'kill-region-dont-append)) (when strs (put-text-property 0 (length str) 'yank-handler `(rectangle--insert-for-yank ,strs t) diff --git a/lisp/replace.el b/lisp/replace.el index 160dd5db2b1..76656625f37 100644 --- a/lisp/replace.el +++ b/lisp/replace.el @@ -1138,8 +1138,6 @@ the kill ring, the \\[delete-matching-lines] command is faster." (save-excursion (while (and (< (point) rend) (re-search-forward regexp rend t)) - (unless (zerop count) - (setq last-command 'kill-region)) (kill-region (save-excursion (goto-char (match-beginning 0)) (forward-line 0) (point)) @@ -1198,8 +1196,6 @@ interactively, also print the number." (save-excursion (while (and (< (point) rend) (re-search-forward regexp rend t)) - (unless (zerop count) - (setq last-command 'kill-region)) (copy-region-as-kill (save-excursion (goto-char (match-beginning 0)) (forward-line 0) (point)) diff --git a/lisp/simple.el b/lisp/simple.el index f80c531cf7a..9361c69879a 100644 --- a/lisp/simple.el +++ b/lisp/simple.el @@ -1474,7 +1474,7 @@ the end of the line." (= n 1)) ;; If a region is active, kill or delete it. (if (eq delete-active-region 'kill) - (kill-region (region-beginning) (region-end) 'region) + (kill-region (region-beginning) (region-end)) (funcall region-extract-function 'delete-only))) ;; In Overwrite mode, maybe untabify while deleting ((null (or (null overwrite-mode) @@ -1517,7 +1517,7 @@ the actual saved text might be different from what was killed." (= n 1)) ;; If a region is active, kill or delete it. (if (eq delete-active-region 'kill) - (kill-region (region-beginning) (region-end) 'region) + (kill-region (region-beginning) (region-end)) (funcall region-extract-function 'delete-only))) ;; For forward deletion, treat composed characters as a single @@ -5691,26 +5691,19 @@ move the yanking point; just return the Nth kill forward." ;;;; Commands for manipulating the kill ring. -(defcustom kill-read-only-ok nil - "Non-nil means don't signal an error for killing read-only text." - :type 'boolean - :group 'killing) +(defvar kill-read-only-ok nil "Unused obsolete variable.") +(make-obsolete-variable 'kill-read-only-ok nil "31.1") -(defcustom kill-region-dwim nil - "Behavior when `kill-region' is invoked without an active region. +(defcustom kill-region-dwim t + "Whether `kill-region' kills last word when region is not active. If set to nil (default), kill the region even if it is inactive, -signalling an error if there is no region. -If set to `emacs-word', kill the last word as defined by the -current major mode. -If set to `unix-word', kill the last word in the style of a shell like -Bash. This ignores the major mode like `unix-word-rubout' (which see)." - :type '(choice (const :tag "Kill a word like `backward-kill-word'" emacs-word) - (const :tag "Kill a word like Bash would" unix-word) - (const :tag "Do not kill anything" nil)) +signalling an error if there is no region. Otherwise, kill the last +word instead." + :type 'boolean :group 'killing :version "31.1") -(defun kill-region (beg end &optional region) +(defun kill-region (beg end &optional _ignored) "Kill (\"cut\") text between point and mark. This deletes the text from the buffer and saves it in the kill ring. The command \\[yank] can retrieve it from there. @@ -5732,64 +5725,23 @@ If the buffer is read-only, Emacs will beep and refrain from deleting the text, but put the text in the kill ring anyway. This means that you can use the killing commands to copy text from a read-only buffer. -Lisp programs should use this function for killing text. - (To delete text, use `delete-region'.) -Supply two arguments, character positions BEG and END indicating the - stretch of text to be killed. If the optional argument REGION is - `region', the function ignores BEG and END, and kills the current - region instead. Interactively, REGION is always non-nil, and so - this command always kills the current region. It is possible to - override this behavior by customising the user option - `kill-region-dwim'." +Lisp programs should use this function for killing text. (To delete +text, use `delete-region'.) Supply two arguments, character positions +BEG and END indicating the stretch of text to be killed." ;; Pass mark first, then point, because the order matters when ;; calling `kill-append'. - (interactive (progn - (let ((beg (mark)) - (end (point))) - (cond - ((and kill-region-dwim (not (use-region-p))) - (list beg end kill-region-dwim)) - ((not (or beg end)) - (user-error "The mark is not set now, so there is no region")) - ((list beg end 'region)))))) - - (condition-case nil - (let ((string (cond - ((memq region '(unix-word emacs-word)) - (let ((end (point))) - (save-excursion - (if (eq region 'emacs-word) - (forward-word -1) - (forward-unix-word -1)) - (filter-buffer-substring (point) end 'delete)))) - (region - (funcall region-extract-function 'delete)) - ((filter-buffer-substring beg end 'delete))))) - (when string ;STRING is nil if BEG = END - ;; Add that string to the kill ring, one way or another. - (if (eq last-command 'kill-region) - (kill-append string (< end beg)) - (kill-new string))) - (when (or string (eq last-command 'kill-region)) - (setq this-command 'kill-region)) - (setq deactivate-mark t) - nil) - ((buffer-read-only text-read-only) - ;; The code above failed because the buffer, or some of the characters - ;; in the region, are read-only. - ;; We should beep, in case the user just isn't aware of this. - ;; However, there's no harm in putting - ;; the region's text in the kill ring, anyway. - (copy-region-as-kill beg end region) - ;; Set this-command now, so it will be set even if we get an error. - (setq this-command 'kill-region) - ;; This should barf, if appropriate, and give us the correct error. - (if kill-read-only-ok - (progn (message "Read only text copied to kill ring") nil) - ;; Signal an error if the buffer is read-only. - (barf-if-buffer-read-only) - ;; If the buffer isn't read-only, the text is. - (signal 'text-read-only (list (current-buffer))))))) + (interactive + (cond + ((use-region-p) + (list (use-region-beginning) (use-region-end))) + (kill-region-dwim + (list (save-excursion (backward-word) (point)) (point))) + (t (list (or (mark t) + (user-error "The mark is not set now, so there is no region")) + (point))))) + (when-let ((string (filter-buffer-substring beg end 'delete))) + (kill-new string) + (setq deactivate-mark t))) ;; copy-region-as-kill no longer sets this-command, because it's confusing ;; to get two copies of the text when the user accidentally types M-w and @@ -6554,9 +6506,7 @@ use \\[append-next-kill] before \\[kill-line]. If the buffer is read-only, Emacs will beep and refrain from deleting the line, but put the line in the kill ring anyway. This means that -you can use this command to copy text from a read-only buffer. -\(If the variable `kill-read-only-ok' is non-nil, then this won't -even beep.)" +you can use this command to copy text from a read-only buffer." (interactive "P") (kill-region (point) ;; It is better to move point to the other end of the kill @@ -6637,10 +6587,8 @@ If ARG is zero, kill current line but exclude the trailing newline." ;; - Make the first kill-region emit a non-local exit only if the ;; second kill-region below would not operate on a non-empty ;; region. - (let ((kill-read-only-ok (or kill-read-only-ok - (/= regions-begin (point))))) - (kill-region (marker-position regions-begin) - (marker-position region1-end))) + (kill-region (marker-position regions-begin) + (marker-position region1-end)) (kill-region (marker-position regions-begin) (point)) (set-marker regions-begin nil) @@ -8385,9 +8333,7 @@ use \\[append-next-kill] before \\[kill-line]. If the buffer is read-only, Emacs will beep and refrain from deleting the line, but put the line in the kill ring anyway. This means that -you can use this command to copy text from a read-only buffer. -\(If the variable `kill-read-only-ok' is non-nil, then this won't -even beep.)" +you can use this command to copy text from a read-only buffer." (interactive "P") ;; Like in `kill-line', it's better to move point to the other end ;; of the kill before killing.