Pablo Marin-Garcia
Pablo Marin-Garcia

Reputation: 4251

how to make a opened buffer read only without reloading again with 'find-file-read-only'

I like to have my coding buffers read-only when I am not editing them (like Vi modes edit, read-only). For that, I can use C-x C-q for turning a emacs buffer on/off the writable status. Seems that this write-locking feature has some inheritance from RCS or similar way of file-loking/revision-system so working with files under Version Control does not work:

C-x C-q
File is under version-control; use C-x v v to check in/out 

I don't want to check in/out I want only to prevent "cat-typing". I can write a macro flip-flop find-file-read-only '/'find-file ' and give some key-binding to it but I am sure there would be a solution inside emacs or .el written already. Any suggestion?

Upvotes: 10

Views: 4455

Answers (3)

alinsoar
alinsoar

Reputation: 15803

You can set a file variable, settting the value of buffer-read-only to a positive value.

You need to set it on the first 2 lines of a file. For example, here I set it to one into a python buffer:

#!/usr/bin/python
# -*- coding: utf-8 ; buffer-read-only: 1 ; -*-

Like that, you won't need to remember after each opening to set it manually.

Upvotes: 2

adben
adben

Reputation: 606

or you can hook find-file

(add-hook 'find-file-hook
  '(lambda ()
     (when (and (buffer-file-name)
        (file-exists-p (buffer-file-name))
        (file-writable-p (buffer-file-name)))
       (message "Toggle to read-only for existing file")
       (toggle-read-only 1))))

and using C-x C-q to change the read-only state of the file

Upvotes: 4

Chen Levy
Chen Levy

Reputation: 16398

You can use view-mode instead.

Upvotes: 8

Related Questions