Subject: bug#75313: 31.0.50; ERC 5.6.1-git: M-w cannot copy multiline text when kill-ring-deindent-mode is on

From: "J.P." <[email-masked]>
Date: Fri, 03 Jan 2025 12:10:16 -0800

DU Zaichuan via "Bug reports for GNU Emacs, the Swiss army knife of text
editors" <[email-masked]> writes:

> To reproduce the bug in emacs -Q,
>
> 1. (kill-ring-deindent-mode t)
>
> 2. open erc and join any channel.
>
> 3. try to copy with M-w someone's message or the channel notice.

Thanks. A slightly distilled version, sans ERC:

  1. M-x kill-ring-deindent-mode RET
  2. M-: (insert (propertize "(progn\n  1\n  2\n  3)" 'read-only t)) RET
  3. C-SPC
  4. C-P ; error: Text is read-only
  5. M-w ; error: Text is read-only

>
> When using the set-mark-command to highlight the texts, message gives
> "kill-ring-deindent-buffer-substring-function: Text is read-only".

Indeed. It seems all `filter-buffer-substring-function' advice members,
like `kill-ring-deindent-buffer-substring-function', run after every
command when the region is active because `region-extract-function'
needs to supply `gui-set-selection' with the region's text. I see two
ways of dealing with this:

1. Local advice around `filter-buffer-substring-function'.

   (defun erc--filter-buffer-substring (orig &rest args)
     (if (eq this-command #'kill-ring-save)
         (with-silent-modifications (apply orig args))
       (apply orig args)))
  
   So, somewhere in ERC's major-mode setup, we'd do:

   (add-function :around (local 'filter-buffer-substring-function)
                 #'erc--filter-buffer-substring)

   However, this won't do anything for "Text is read only" spam related
   to other commands, like `previous-line' or `move-end-of-line' (when
   the region is active). But it should at least restore the ability to
   copy with M-w.

2. Address this somewhere in lisp/indent-aux.el.

   Binding `inhibit-read-only' to t around the call to `indent-rigidly'
   in `kill-ring-deindent-buffer-substring-function' seems to help in
   cursory experiments. Not sure if that's the right move, though.
   Perhaps Po Lu (Cc'd) has some insights.