Subject: bug#68648: 30.0.50; read-only-mode-hook's are not executed when buffer-read-only is t

From: Stefan Monnier <[email-masked]>
Date: Thu, 01 Feb 2024 09:17:13 -0500

>> > >> 1. (add-hook 'read-only-mode-hook (lambda () (print "foobar")))
>> > >> 2. open file that is read-only
>> > >> 3. foobar should be print but isn't.
>> > > That's because read-only-mode is not entered when you visit a
>> > > write-protected file.
>> > It does look like the mode is entered because when calling
>> > read-only-mode it is disabled.

I agree that it's far from clear that "read-only-mode is not entered".

>> That's only a circumstantial evidence.  read-only-mode is a thin
>> wrapper around buffer-read-only variable, but setting buffer-read-only
>> directly (which is what we do when you visit a write-protected file)
>> doesn't invoke the mode function, and thus the mode hook is not run.

That's the technical explanation of what happens, but it's not very
satisfactory as a justification for why it should happen.

>> For the same reason, the setting of view-read-only is not honored in
>> the scenario you described.

I don't understand what you're trying to say here:

    emacs --eval '(setq view-read-only t)' -Q /etc/password

does put me in `view-mode`, so the setting of `view-read-only` seems to
be honored.

> Stefan & Stefan, any comments or opinions on this issue?  I' debating
> whether to do anything (and if so, what) about this, or close this bug
> as wontfix.

[ This a bug of my own making, when I decided to replace
  `toggle-read-only` with`read-only-mode`.  ]

I'd be in favor of replacing the `(setq buffer-read-only t)` with
`(read-only-mode 1)`, but it will require other changes: since calling
`read-only-mode` will handle `view-read-only`, it might make for a nice
simplification, but since the code is fairly complex over there, it
might also make things worse.

        Stefan