4

I wonder if it is possible to prevent specific parts of a buffer from being editable. For example, when I have outlined my document in an org-mode file, I would like to prevent any accidental changes to the headlines, and only being able to edit the contents:

* Headline 1: not editable

Content under Headline 1 is editable.

* Headline 2: not editable

Content under Headline is editable.

** Subheadline 1 under Headline 2: not editable

Content under Subheadline 1 is editable

Or if I have code block in an org-file, I would like to prevent changes to the code block commands:

#+begin_src emacs-lisp                       <- not editable
(defun fun (a b)                             <- editable
  (+ a b)                                    <- editable
#+end_src                                    <- not editable

EDIT: I would prefer a solution where the non-editable parts can still move up or down the y-axis in the buffer to ensure that content in between non-editable parts can be expanded:

#+begin_src emacs-lisp              <- not editable, but can move up or down
(defun fun (a b)                    <- editable
  (+ a b)                           <- editable
)                                   <- editable
#+end_src                           <- not editable, but can move up or down

1 Answers1

4

Yes, just put text-property read-only on those bits of text.

For example, to make a zone of text from position START to position END read-only, do this:

(put-text-property start end 'read-only t)
Drew
  • 75,699
  • 9
  • 109
  • 225
  • So, for example, I would have to find the beginning and end positions of all occurrences of `org-mode` headlines and then apply said function to all of them? – Singulaere Entitaet Aug 12 '17 at 16:29
  • Ok, the issue seems to be that this locks the text in an absolute position on the y-axis. In the above examples this maybe detrimental, if one wants to insert more text within area encompassed by the read-only parts. – Singulaere Entitaet Aug 12 '17 at 16:49
  • If you want to insert text then the place where you want to insert it cannot be read-only. Rather, it could be read-only, and your code that inserts there could temporarily inhibit the read-only aspect: e.g., bind variable `inhibit-read-only` to non-`nil`. (But the question is not clear wrt wanting the text to be read-only vs wanting to insert there.) – Drew Aug 12 '17 at 17:18