4

I'd like to set some things (e.g., cursor shape) when in read-only-mode. That part is easy enough with

  (add-hook 'read-only-mode-hook 
             (lambda() (setq-local cursor-type 'box)))

Then I'd like to switch it back when exiting read-only-mode. Something like

  (add-hook 'read-write-mode-hook 
             (lambda() (setq-local cursor-type 'bar)))

except that there is no such thing as read-write-mode.

Drew
  • 75,699
  • 9
  • 109
  • 225
Ista
  • 1,148
  • 8
  • 12

1 Answers1

6

Like all minor mode hooks, read-only-mode-hook runs when entering or leaving read-only mode. So you only need to make the setting of the cursor a bit smarter, probably by checking the value of buffer-read-only and acting appropriately.

Stefan
  • 26,154
  • 3
  • 46
  • 84
NickD
  • 27,023
  • 3
  • 23
  • 42
  • 1
    Ah, I have achieved enlightenment, and all it cost me was the embarrassment of asking a stupid question in the internet. – Ista May 05 '17 at 18:44
  • @Stefan. Interesting. I understand this is because of the "toggle" nature of minor modes but I can't find any explicit mention of it in the documentation. Did I miss it? – JeanPierre May 05 '17 at 20:01
  • 1
    The doc for `read-only-mode-hook` explicitly mentions "entering or leaving". Not sure about a more general statement, although the `define-minor-mode` doc in the Elisp manual say that it runs `MODE-hook`, but you still have to make the connection that it runs it whenever it is called (i.e. both entering and leaving) which I had not made before @Stefan pointed it out. – NickD May 05 '17 at 20:37
  • 2
    At present you could still use `MODE-on-hook` and `MODE-off-hook` for minor modes, but the code indicates that these are only there for backwards-compatibility, so they might be removed at some point in the future. The intention is certainly that you use `MODE-hook` and test the value of the `MODE` variable. – phils May 05 '17 at 23:43
  • @ista: thanks for your "stupid question" - I think it enlightened quite a few of us! – NickD May 07 '17 at 12:18
  • One thing isn't clear though, even from the docs I can find: does it run before, or after, `buffer-read-only` gets set? My experiments say after, so if `buffer-read-only` is `t` inside the hook, we're *entering* `read-only-mode`. – Dave Abrahams Feb 18 '21 at 03:20