10

I'm extending an existing Emacs package and I want to make a part of the buffer not editable (read only). In fact, I want to make only one line in the middle of the buffer editable and I use the following snippet for achieving that:

(put-text-property (point-min) point-before-editable-text 'read-only t)
;; (put-text-property point-after-editable-text (point-max) 'read-only t)

However, I can still put cursor at the beginning of buffer and insert some text which is not the desired behavior.

How can I prevent insertions at the beginning of the buffer?

Drew
  • 75,699
  • 9
  • 109
  • 225
Andrii Tykhonov
  • 452
  • 2
  • 13

1 Answers1

10

You need to (before adding property read-only), make the first character have a value that includes read-only for property front-sticky:

(put-text-property 1 2 'front-sticky '(read-only)) ; Do this one first.
(put-text-property (point-min) 50 'read-only t)

See the Elisp manual, node Sticky Properties. The problem was that although the first char had a non-nil property read-only, insertion before it did not inherit that property, because read-only was not a front-sticky property value for the first character.

Kaushal Modi
  • 25,203
  • 3
  • 74
  • 179
Drew
  • 75,699
  • 9
  • 109
  • 225