2

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?

  • Welcome. Which language is this for? – Andrew Swann Dec 23 '17 at 11:15
  • @AndrewSwann Hi! this is for the rust language. – Victor Polevoy Dec 23 '17 at 11:35
1

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)))
  • 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. – Victor Polevoy Dec 24 '17 at 18:59
  • Thank you for spending your time. When I check it and it works, I will accept your answer. – Victor Polevoy Dec 25 '17 at 6: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? – Victor Polevoy Oct 3 '18 at 11:41
  • That's 3 questions. But the answer is to look at the Elisp manual, node Changing Properties. 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 for a description of properties read-only and face. – Drew Oct 3 '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 3 '18 at 14:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.