6

If, in a function run from before-save-hook, I want to abort the saving of a file, so that the file is not saved and the user is signaled, how can I do that? Is it possible (without rebinding C-xC-s or doing defadvice around save-buffer)?

(It used to be, I think in Emacs 23, that all you had to do was (error "Bad!"), and the file save operation would be aborted. But that does no longer work; any errors in a before-save-hook are ignored.)

2

Try using one of the hooks write-file-functions or write-contents-functions instead. Your hook function can signal an error or just return non-nil, and in either case it will abort the save.

  • If the hook function simply returns non-nil, the buffer will be marked as saved (i.e. not modified), and cause subsequent save-buffer operations to be ignored, since the file is considered “saved”. But signaling an error works! – Teddy Mar 5 '16 at 16:45
  • Huh. For me, returning non-nil aborts the save and doesn't mark the buffer as modified (which surprised me, since I was expecting the behavior you describe). – Aaron Harris Mar 5 '16 at 21:42

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.