Monday, December 1, 2008

Emacs Tip #27: midnight-mode

I generally have a single emacs session that runs for a couple weeks, up to about 3 months (when work does it's quarterly preventative maintenance reboot), and because of this, I often have dozens of buffers open. Periodically I'd go through them and remove a bunch - mostly to free up memory.

Luckily, there's a mode that already does that for you, periodically flushing unused and old buffers:

(require 'midnight)
The following variables can be used to customize the behavior of the clean-buffer-list (which is run daily at midnight). It responds to settings of the following variables:
clean-buffer-list-kill-buffer-names
clean-buffer-list-kill-regexps
clean-buffer-list-kill-never-buffer-names
clean-buffer-list-kill-never-regexps
clean-buffer-list-delay-general
clean-buffer-list-delay-special
I love finding packages that implement functionality I hadn't realized I wanted. It is very relieving, like accidentally scratching an itch I didn't know was bothering me.

Edited to reflect proper require statement.

6 comments:

Boyd Adamson said...

Not sure if it's a version thing, but here (GNU Emacs 23.0.60.1 (sparc-sun-solaris2.10, X toolkit) of 2008-12-01) it's (require 'midnight), rather than (require 'midnight-mode)

a said...

Thanks, that's correct.

SmartFlix / HeavyInk said...

I use the buffer list a lot for this sort of thing, and I wrote a function a while back that marks all saved buffers for deletion. In buffer mode I hit 'z' to mark them, then 'x' to actually do the deletion. I like this as I can see what I'm doing, and can alter the suggested actions before executing.

(defun tjic-buffer-mark-for-delete-all-saved ()
"in buffer-meny-mode, mark for deletion all buffers that are saved"
(interactive)
(Buffer-menu-beginning)
(let ((continue-p t))
(while continue-p
(let ((modified-p (save-excursion
(set-buffer (Buffer-menu-buffer t))
(buffer-modified-p))))
(if modified-p
(forward-line)
(Buffer-menu-delete)))
(if (equal (point) (point-max))
(setq continue-p nil)))))

(define-key Buffer-menu-mode-map "z" 'tjic-buffer-mark-for-delete-all-saved)

Robert said...

How would midnight interact with something like org-mode? Org-mode's agenda functions seem to require the underlying org files all to be loaded.

a said...

Robert,

That's a good question about interaction with org-mode. My org-mode usage is pretty limited (only one file), so I do not know the answer.

That being said, you could easily customize the variables such that the org-mode files are skipped.

Knusper said...

The problem I have with midinght-mode that at weekends it closes all buffers that I leave behind on friday... That is annoying :D