0

The view-read-only variable, when set, ensures view-mode is enabled on all read-only buffers, which I find generally good. But sometimes, the major mode already has similar functionality, so I'd prefer view-mode not to be activated.

How can I make an exception to the view-read-only behavior, stating the rule should not apply for buffers with some specific major mode(s)?

Setting (setq-local view-read-only nil) in the major mode initialisation does not work, neither does adding to the major mode hook a function calling (view-mode -1).

1 Answer 1

2

But sometimes, the major mode already has similar functionality, so I'd prefer view-mode not to be activated.

I'm wondering which modes those are, since special modes are already excluded from view-read-only. I have it enabled as well but haven't noticed view-mode becoming enabled in modes that have similar features.

To solve this problem, we can take a look at how the variable is used in the Emacs source code. By doing C-h v view-read-only RET, we can see the variable's documentation. From there, pressing s opens the elisp file where it is defined. There is no guarantee that that will be the same file where it is used. But in this case it is.

Now, with "files.el" opened, we can search for the variable in this file. And we find the following code included in the after-find-file function.

 (when (and buffer-read-only view-read-only (not (eq (get major-mode 'mode-class) 'special))) (view-mode-enter)) 

So it appears that for view-read-only to cause view-mode to be enabled when opening a read-only file, the major-mode must not have a mode-class property of special.

This means we should be able to make exceptions to the behavior of view-read-only by setting the mode-class property of the major-mode. For example, if we don't want to use view-mode with read-only Elisp buffers, we can do:

(put 'emacs-lisp-mode 'mode-class 'special) 

However, this may have other side-effects. See the major-mode conventions section of the manual (near the bottom) for more information.

2
  • 1
    Thanks for your answer. The actual major mode is irfc-mode, to display rfc documents (emacswiki.org/emacs/Irfc). It does not stricly match the purpose of special ("text produced specially by Emacs, rather than directly from a file") but that seems to work. Let's see if someone has more to say about that! Commented Nov 1 at 19:08
  • Actually, this seems to be the correct thing to do. Commented 19 hours ago

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.