开发者

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

开发者 https://www.devze.com 2023-02-13 22:44 出处:网络
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 thi

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?


You can use view-mode instead.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消