Subject: bug#69097: [PATCH] Add 'kill-region-or-word' command

From: Philip Kaludercic <[email-masked]>
Date: Thu, 19 Sep 2024 07:19:11 +0000

Juri Linkov <[email-masked]> writes:

> Thanks for the new command!  I'd like to use it, but currently can't
> because I have customized 'mark-even-if-inactive' to nil.
>
>> +** New user option 'kill-word-dwim'.
>> +This option, if non-nil, modifies the fall-back behavior of
>> +'kill-region' if no region is active, and will kill the last word
>> +instead of raising an error.  Note that if you have disabled Transient
>> +Mark mode you might prefer to use 'unix-word-rubout', as this feature
>> +relies on there being an active region.
>
> This could mention the default keybindings of 'kill-region' ('C-w').
>
>> +(defcustom kill-region-dwim nil
>> +  "Behavior when `kill-region' is invoked without an active region.
>> +If set to nil (default), then the behavior `kill-region' will not
>> +change.  If set to `emacs-word', then kill the last word as defined by
>> +the current major mode.  If set to `unix-word', then kill the last word
>> +in the style of a shell like Bash, disregarding the major mode like with
>> +`unix-word-rubout'."
>> +  :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))
>
> Usually the default value 'nil' comes first.
> This helps to disable an option by always selecting 0
> from the Customization menu.

Good point, I had no real reason for listing it as the last option
besides that the line length was shorter that way.

>>  (defun kill-region (beg end &optional region)
>>    "Kill (\"cut\") text between point and mark.
>>  This deletes the text from the buffer and saves it in the kill ring.
>> @@ -5843,21 +5856,35 @@ kill-region
>>   (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
>> - non-nil, the function ignores BEG and END, and kills the current
>> + `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."
>> + this command always kills the current region.  It is possible to
>> + override this behavior by customising the user option
>> + `kill-region-dwim'."
>
> I tried to use this command with `emacs -Q`, and typed 'C-w C-w' twice,
> but it fails with
>
>   Debugger entered--Lisp error: (wrong-type-argument number-or-marker-p nil)
>     kill-region(nil 16 emacs-word)
>     funcall-interactively(kill-region nil 16 emacs-word)
>     command-execute(kill-region)
>
> because for the second 'C-w' it calls 'kill-append':
>
>   (if (eq last-command 'kill-region)
>       (kill-append string (< end beg))
>     (kill-new string))

Juri Linkov <[email-masked]> writes:

>> Thanks for the new command!
>
> Sorry, I meant the new option.
>
>> I'd like to use it, but currently can't
>> because I have customized 'mark-even-if-inactive' to nil.
>
> This small fix addresses the remaining issues:
>
> diff --git a/lisp/simple.el b/lisp/simple.el
> index 9fbd9bfb577..40266f37209 100644
> --- a/lisp/simple.el
> +++ b/lisp/simple.el
> @@ -5865,7 +5865,7 @@ kill-region
>    ;; Pass mark first, then point, because the order matters when
>    ;; calling `kill-append'.
>    (interactive (progn
> -                 (let ((beg (mark))
> +                 (let ((beg (mark kill-region-dwim))
>                         (end (point)))
>                     (cond
>                      ((and kill-region-dwim (not (use-region-p)))
> @@ -5888,10 +5888,12 @@ kill-region
>                       ((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)
> +	  (if (and (null kill-region-dwim)
> +		   (eq last-command 'kill-region))
>  	      (kill-append string (< end beg))
>  	    (kill-new string)))
> -	(when (or string (eq last-command 'kill-region))
> +	(when (and (null kill-region-dwim)
> +		   (or string (eq last-command 'kill-region)))
>  	  (setq this-command 'kill-region))
>  	(setq deactivate-mark t)
>  	nil)

Do you want to push this fix?  I can take care of the other two points.

-- 
	Philip Kaludercic on siskin