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.

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.

| improve this answer | |
  • 1
    Ah, I have achieved enlightenment, and all it cost me was the embarrassment of asking a stupid question in the internet. – Ista May 5 '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 5 '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 5 '17 at 20:37
  • 1
    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 5 '17 at 23:43
  • @ista: thanks for your "stupid question" - I think it enlightened quite a few of us! – NickD May 7 '17 at 12:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.