Search code examples
emacselisp

emacs remove region read-only


Closely related to Emacs specific region read only how do you remove the read only property on a region of text in a buffer.

For example, if you are using a python shell in emacs and accidentally print out a huge list and would like to remove the output from your buffer.


Solution

  • I use the following. It's similar to tcaswell's answer, but deals with the buffer modification issue.

    (defun set-region-read-only (begin end)
      "Sets the read-only text property on the marked region.
    
    Use `set-region-writeable' to remove this property."
      ;; See https://stackoverflow.com/questions/7410125
      (interactive "r")
      (with-silent-modifications
        (put-text-property begin end 'read-only t)))
    
    (defun set-region-writeable (begin end)
      "Removes the read-only text property from the marked region.
    
    Use `set-region-read-only' to set this property."
      ;; See https://stackoverflow.com/questions/7410125
      (interactive "r")
      (with-silent-modifications
        (remove-text-properties begin end '(read-only t))))