2

I have a particular buffer that collects statistics from my emacs sessions. I could write the buffer every time I update the statistics, but that's not efficient. I want to auto-write the buffer whenever I quit emacs.

I have normal auto-saving turned off. (And besides, auto-saving is not the same as save-automatically-on-exit.)

I've read up on things like kill-emacs-hook and kill-buffer-hook and even local-write-files-hook, but none of them do the job and fire at the right time. In particular, I get the usual "Buffer XX is modified do you want to save it?" prompt before the kill-emacs-hook is run.

I would rather catch and save the buffer before those exit prompts run, but I could also attach a local hook to the stats file when I first read it into my session.

Is there any recommended way to do this kind of thing (save on an exit event, or get ahead of that prompt, rather than auto-save-on-a-timer)? Thanks

Here's the kind of thing I'm looking for, but this is for shell process buffers, not for normal buffers.

;; don't prompt me when I try to quit a shell buffer
(add-hook 'comint-exec-hook
      (lambda ()
        (set-process-query-on-exit-flag (get-buffer-process (current-buffer)) nil)))
2

When Emacs exits, it saves buffers by calling save-some-buffers. This is the function that does the prompting. You can turn off the prompting in a buffer by setting the buffer-local variable buffer-save-without-query to t.

This will also save the buffer without asking if you press C-x s or when some other code calls save-some-buffers, which is probably desirable.

0

After thinking about this all day, I finally realized that I can rebind the normal ctrl-x ctrl-c emacs exit keybinding to my own function, which could save the buffer and then continue with the usual save-buffers-kill-terminal function that is the default keybinding. (I also wondered why it took me so long to arrive at this simple idea.)

Not really the answer the I was looking for, but I could see how this one would work ok for my purposes. Maybe adding advice to save-buffers-kill-terminal could accomplish the same goal. I've never used advice, but it would be doing essentially the same thing that rebinding to my own function wold accomplish.

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.