1

I am writing a custom function which is supposed to write selected text to some arbitrary file on the system. It does something like

(defun write-region-to-file (&optional arg)
  (interactive "*p")
  (write-region (point) (mark-marker) "/path/to/file/other/then/visited"))

If I then run it in a read-only buffer I get

command-execute: Buffer is read-only: #<buffer ...>

Why is this happening? I don't do any modification to the visited one, I am writing to a completely different file.

Any ideas?

1 Answer 1

3

The interactive code * is used to: "Signal an error if the current buffer is read-only. Special."

https://www.gnu.org/software/emacs/manual/html_node/elisp/Interactive-Codes.html

Thus, the line that reads (interactive "*p") can be changed to eliminate the asterisk.

NOTE: In the example, the optional argument ARG is not used. Thus, consider changing (&optional arg) to just (); and, consider changing the interactive statement to just (interactive).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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