EmacsSession

This package is one possible SessionManagement implementation.

When you start Emacs, package Session restores various variables (e.g., input histories) from your last session. It also provides a menu containing recently changed/visited files and restores the places (e.g., point) of such a file when you revisit it.

To restore the variables, this package writes a session file (~/.session) when you exit Emacs. The file includes the values of variables which are automatically updated by Emacs during some editing operations:

To restore the places of a recently changed/visited file when you revisit it, this packages stores the places of a buffer in a special variable (the list mentioned above) when you kill that buffer (this includes exiting Emacs). Places are:

As opposed to DeskTop and other packages, Session does not automatically revisits all files from your last session, most of which are not interesting anymore.

CategoryPersistence

Installation

  1. Get the tarball from the link given above
  2. extract session.el and put it in a directory on your LoadPath
  3. Add the following to your InitFile:
    (require 'session)
    (add-hook 'after-init-hook 'session-initialize)

Discussion

I agree with the comment given above: Most of the files you visited last time are not interesting anymore. Having their names in the find-file history is good enough for me. Desktop was just too complex for my needs.

Sometimes, I use OrgMode. But org-mark-ring is a circular object. Use the following:

(when (require 'session nil t)
  (add-hook 'after-init-hook 'session-initialize)
  (add-to-list 'session-globals-exclude 'org-mark-ring))

AlexSchroeder

There is a function in session that’s not really persistence related – ‘session-jump-to-last-change’ <C-x C-/>. This is the singular most useful function of any Emacs add-on to me. It moves the point to the last modified location. Keep calling it and you will visit all the locations you’ve made modifications. Absolutely brilliant. Unobstrusive, unlike highlight-changes-mode.

However, it doesn’t automatically reveal folded sections. Here is the fix:

;;; expanded folded secitons as required
(defun le::maybe-reveal ()
  (when (and (or (memq major-mode  '(org-mode outline-mode))
                 (and (boundp 'outline-minor-mode)
                      outline-minor-mode))
             (outline-invisible-p))
    (if (eq major-mode 'org-mode)
        (org-reveal)
      (show-subtree))))

(add-hook 'session-after-jump-to-last-change-hook
          'le::maybe-reveal)

LeWang

Can this mode replace savehist, saveplace and desktop all at once and advantageneously ? Sounds like it is really capable and as I do not like restoring all the files and things like that, that could help just activating one mode for all this stuff

– Anonymous

I’ve been using session for many years. I also use it to restore my project specific settings in a per-project basis. Here is how it works: Add the following code segment into your .emacs init script. It will search “.emacs.session” file in the directory where you start Emacs.

;;; Local session.
(unless (daemonp)
  (custom-set-variables '(session-save-file ".emacs.session"))

  (let ((local-session (concat default-directory session-save-file)))
    (if (file-exists-p local-session)
        (progn
          (custom-set-variables '(session-save-file local-session))
          (message (concat "Local session file set to \"" session-save-file "\".")))
      (custom-set-variables '(session-save-file (concat "~/" session-save-file))))))

– Luke Lee


Session does not work well with emacs daemon mode.

When one exits/restarts emacs server, the session position is not saved.

This is because kill-emacs-hook is not run in this case.

Here is my solution for session.el. It worked fine for emacs 24.3.

(require 'session)
(session-jump-to-last-change)
(session-initialize)
;; session will save if any frame is deleted.
(add-hook 'delete-frame-functions
         (lambda (frame)
           (session-save-session t)
           ))

Just be careful to exit existing emacs frame before restarting the emacs daemon.

– Billy.Zheng (zw963)


I’ve made little modification of Billy.Zheng (zw963) script, instead using delete-frame-functions I use after-save-hook, so the session is save every time buffer is save to file without waiting to exit the frame of emacs.

(require 'session)
(session-jump-to-last-change)
(session-initialize)
;; session will be save if a buffer is save to a file.
(add-hook 'after-save-hook #'session-save-session)

– kenardes

CategoryPersistence SessionManagement