3

I am new to emacs lisp and emacs itself, coming from vim. I have seen such a nice feature in the idea:

Screenshot

This is a mutable buffer with read-only text hints: type of a variable and a argument name in the print function. I'd like to implement such functionality in the emacs editor. Is it possible?

Drew
  • 75,699
  • 9
  • 109
  • 225
VP.
  • 187
  • 1
  • 9

1 Answers1

2

I'm not sure what you're asking, but I think you're asking how you might make certain parts of a buffer read-only.

You can do that by putting text-property read-only on the text you want to be read-only. See the Elisp manual, node Special Properties. See also node Changing Properties.

The very simple way of inserting new read-only text is demonstrated in the next example. You can try this example in the *scratch* buffer.

 (insert (propertize "some text" 'read-only t))

After you inserted the read-only text it is important to know how to remove read-only text. You can ignore the read-only status of buffer text by setting the special variable inhibit-read-only to a non-nil value.

Mark the read only text some text in our example and type the following stuff:

M-: (let ((inhibit-read-only t)) (delete-region (region-beginning) (region-end)))

Note that the key sequence M-: runs the command eval-expression and the expression is input in the mini-buffer.

It is also possible to set the read-only text property for an already existing buffer-substring. That is demonstrated in the following example which produces the same read-only text as the former example.

(progn
  (let ((pos1 (point)))
    (insert "some text")
    (put-text-property pos1 (point) 'read-only t)))
Tobias
  • 32,569
  • 1
  • 34
  • 75
Drew
  • 75,699
  • 9
  • 109
  • 225
  • I am not sure I understand how to use it: how to add a text into some place of a buffer and mark it with `read-only` special property? I have found the documentation but only for these properties but not about a function which sets it. – VP. Dec 24 '17 at 18:59
  • Thank you for spending your time. When I check it and it works, I will accept your answer. – VP. Dec 25 '17 at 06:08
  • Could you also tell me please, how can I make its background color different from other text and how can I make so that cursor is unable to be on the text but only before or after it? – VP. Oct 03 '18 at 11:41
  • That's 3 questions. But the answer is to look at the Elisp manual, node [Changing Properties](https://www.gnu.org/software/emacs/manual/html_node/elisp/Changing-Properties.html). You can get there inside Emacs using `C-h i`, choosing Elisp manual, then `i text property TAB` (or in this case just `g changing p TAB`. See node [Special Properties](https://www.gnu.org/software/emacs/manual/html_node/elisp/Special-Properties.html) for a description of properties `read-only` and `face`. – Drew Oct 03 '18 at 14:51
  • The code in my answer shows how to insert `some text` at position `pos1` and make that text read-only. Define a function that uses that code, if you want. Put it on some hook, if you want. Make it a command, if you want. All of those things are possible (and different questions). – Drew Oct 03 '18 at 14:54
  • I did not say that your answer is bad and that it did not answer something. It was an additional question and I just thought as it related to original question, it would be good to ask it here. Anyway, thanks. – VP. Oct 03 '18 at 15:26