terça-feira, 7 de janeiro de 2014

Emacs - purge session files

The problem

I have always been bothered with the session files left by emacs is user-emacs-directory, normally ~/.emacs.d/. These files have the name session.[a long string of hexadecimal characters] and save the session variables when emacs exits without being explicitely terminated. Like when one ends up the session by the session manager and does not bother to close the emacs window first.

After a few days, a set of session files ends up cluttering the emacs directory. These files lose their purpose once one does not want to restore the session on the next emacs invocation, but they are not purged automatically.

The solution

purge-session-files deletes all session files, preserving the n most recent. n defaults to 1. The function can be called interactively. In such case, the user is given the choice of the number of session files to preserve (defaulting to one).

Here is the code:

(defun purge-session-files (&optional n)
  "Purge all session files but the n more recent files."
  (interactive (list (read-number "Number of files to preserve" 1)))
  (let ((files (directory-files user-emacs-directory t "session.*")))
    (dolist (file (butlast files (or n 1)))
      (delete-file file nil))))

Sem comentários:

Enviar um comentário