reddit / emacs

Option to Make Parts of Buffer Read-Only?

owmagow · 2022-10-26 22:08
When I'm coding, switching around betwixt buffers, running M-x commands, etc, I seem to be accidentally touching things and messing up code pretty often. Is there a way to make part of a buffer read-only so that I don't accidentally jack it up? (Or should I just have my third thumb surgically removed?) :)
xtifr · 2022-10-26 22:43
You can assign a `read-only` property to specific text in a buffer using the normal text property functions. This is, however, likely to be a quite complicated solution! Thumb surgery might well be easier and less painful. :)
owmagow · 2022-10-26 22:45
LOL. Thank you.
eleven_cupfuls · 2022-10-26 22:54
Depending on your uses and how you're navigating, you might consider some hooks to turn on `read-only-mode` as you switch. E.g. if you're jumping with xref, you could do something like (defun owm/read-only-after-xref-jump () (when (...some conditions like `project-current`, or regex on buffer name...) (read-only-mode +1))) Not exactly what you were asking for, just an alternative that might be helpful.
owmagow · 2022-10-26 22:58
Thank you for the suggestion. I think mostly what is happening is that I'm working on one section of code, and as I move around to view other sections (often in the same buffer), I'm inadvertently changing things and then having to go back and troubleshoot to figure out what I broke. It'd be nice if I could just keep the current defun writable and make the rest of them read-only. But I'm betting I could create some sort of keybinding that will allow me to highlight stuff I want to be read-only and put some tags around it.
eleven_cupfuls · 2022-10-26 23:06
Yeah, that makes sense. I also sometimes make unintentional edits, especially when jumping with avy. If you'll forgive me for another not-really-what-you-asked-for idea that I just had... you could check out [indirect buffers](https://www.gnu.org/software/emacs/manual/html_node/emacs/Indirect-Buffers.html) which lets you put the same buffer content into another window but with different modes. So you could have an editable view and a read-only view of the same content.
owmagow · 2022-10-26 23:09
Ah, that is actually a very usable idea. I'm gonna give that a go. Thank you kindly.
poiu- · 2022-10-26 23:19
Modifying text properties isn't actually that hard.
andyjda · 2022-10-26 23:52
It’s not directly what you’re asking, but have you looked into packages like vundo, or undo-tree, that would just help you keep track of changes and visualize any inadvertent mess-up? I use vundo and it’s a good way to go through recent changes and check for anything wrong. Sometimes I even just use the magit interface and that’s also pretty useful, especially across large files (although of course you need to be inside a git repo)
owmagow · 2022-10-26 23:55
I've not looked into those packages. But I will certainly have a look. Thank you. Good idea.
andyjda · 2022-10-27 00:13
Happy to help :) I just thought of another possible solution that's a bit closer to your ask. You could bind a key like `<escape>` to `(read-only-mode)`, (which would toggle the mode). That way you can switch quickly between jumping around text/files and editing them. The only problem with this is that I don't think there's a quick way to set `read-only-mode` globally, so if you're just exploring text in a file and you jump to another file, the new file would be editable.
d_metal_ · 2022-10-27 00:27
Probably using narrowing functions might work for you, I us them pretty often when I want to focus on some piece of code, and the buffer functions will only work for the narrowed section https://www.gnu.org/software/emacs/manual/html_node/emacs/Narrowing.html
[deleted] · 2022-10-27 00:41
I maintain a package that makes text a certain distance above point read-only on an idle timer: https://github.com/rnkn/freeze-it I'm sure you could adapt it to your needs.
MunsterPlop · 2022-10-27 04:32
There's actually a solution that works pretty well for me. I have a toggle that adds or removes view-mode from prog-mode-hook: (defun mnc/toggle-global-view-mode () "Toggle view-mode in prog-modes." (interactive) (if (seq-contains-p prog-mode-hook 'view-mode) (progn (remove-hook 'prog-mode-hook 'view-mode) (message "Global view-mode disabled.")) (add-hook 'prog-mode-hook 'view-mode) (message "Global view-mode enabled."))) It should easily be tweakable to use any mode you want.
owmagow · 2022-10-27 14:02
Thanks. will have a look!
owmagow · 2022-10-27 14:06
Thank you! Checking it out now!
arthurno1 · 2022-10-27 14:31
> Is there a way to make part of a buffer read-only so that I don't accidentally jack it up? Of course, you can. Wdired for example does it for some parts of Dired buffer (it lets you change only permissions and names and the rest is read-only). Have you looked at [text properties](https://www.gnu.org/software/emacs/manual/html_node/elisp/Text-Properties.html)? > (Or should I just have my third thumb surgically removed?) :) > This is, however, likely to be a quite complicated solution! Njah, it's not that difficult, in order to save at least one of your thumbs, here is a small illustration: (defun region--toggle-read-only (beg end read-only) (let ((inhibit-read-only (not read-only))) (put-text-property beg end 'read-only read-only))) (defun region-read-only (beg end) (interactive "r") (region--toggle-read-only beg end t)) (defun region-writeable (beg end) (interactive "r") (region--toggle-read-only beg end nil)) Nine lines of code gives you a small prototype. Now I would rework that into something to your liking. For example, you might use a named property instead of hard-coded t and nil, so you can use [property search](https://www.gnu.org/software/emacs/manual/html_node/elisp/Property-Search.html) to find and toggle all regions marked with your property. You could also rewrite those functions to work in 'dwim' style, so you can for example mark a region, sexp, defun, or I don't know which kind of text you work with, line, paragraph. They already work that way to some extent I think due to how Emacs work with mark and point, at least in my Emacs, but you might wish to do your own stuff depending on the text you work with, as a suggestion.
owmagow · 2022-10-27 14:36
Thanks. And thanks for the template.
arthurno1 · 2022-10-27 19:47
Not for nothing, but you don't happen to use Emacs on a laptop? Have you try to disable the tiuchpad (or whatever it is called)? [There is a mode for that too!](https://github.com/purcell/disable-mouse) 😀