3

I want to create a buffer where some parts are editable, but others are not. I'm using the read-only string property to do this, e.g.:

(with-current-buffer (get-buffer-create "*scratch-demo*")
  (insert ";; you can edit me\n")
  (insert (propertize
           ";; but you cannot edit me\n"
           'read-only t)))

If I paste the second line into a buffer in fundamental mode, I can edit it.

this buffer is in fundamental mode
;; but you cannot *foo* edit me

However, if I paste the second line into an emacs-lisp-mode buffer, I cannot edit it.

;; this buffer is in emacs-lisp-mode
;; I cannot modify the line below:
;; but you cannot edit me

How can I ensure users can edit text, which has been yanked from read-only portions of my buffer?

1
  1. I don't see the problem you mention. Maybe I miss the recipe. I did this, in a session from emacs -Q:

    a. In buffer *scratch* I typed your code and evaluated it.

    b. I visited buffer *scratch-demo*. The text of the second line was read-only (could not modify it). I copied the text there using M-w.

    c. I visited a buffer in Emacs Lisp mode and yanked the copied text, using C-y. I had no problem modifying the yanked text, including the part from the read-only portion.

    d. I did the same thing in a buffer in Fundamental mode - same effect.

  2. If you have trouble with this kind of thing, have a look at function insert-for-yank and variables yank-handled-properties and yank-excluded-properties, to get the properties you want and not get the properties you do not want, respectively.

    See also functions remove-yank-excluded-properties and insert-buffer-substring-as-yank.

    See the Elisp manual, node Yanking for details.

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.