+ ;; Check for long options with attached arguments
+ ;; and separate out the attached option argument into argval.
+ (if (string-match "^--[^=]*=" this-switch)
+ (setq argval (substring this-switch (match-end 0))
+ this-switch (substring this-switch 0 (1- (match-end 0)))))
+ ;; Complete names of long options.
+ (if (string-match "^--" this-switch)
+ (progn
+ (setq completion (try-completion this-switch command-line-x-option-alist))
+ (if (eq completion t)
+ ;; Exact match for long option.
+ nil
+ (if (stringp completion)
+ (let ((elt (assoc completion command-line-x-option-alist)))
+ ;; Check for abbreviated long option.
+ (or elt
+ (error "Option `%s' is ambiguous" this-switch))
+ (setq this-switch completion))))))
+ (setq aelt (assoc this-switch command-line-x-option-alist))
+ (if aelt (setq handler (nth 2 aelt)))
+ (if handler
+ (if argval
+ (let ((x-invocation-args
+ (cons argval x-invocation-args)))
+ (funcall handler this-switch))
+ (funcall handler this-switch))
+ (setq args (cons orig-this-switch args)))))
+ (nconc (nreverse args) x-invocation-args))
+
+;; Handle the --smid switch. This is used by the session manager
+;; to give us back our session id we had on the previous run.
+(defun x-handle-smid (switch)
+ (or (consp x-invocation-args)
+ (error "%s: missing argument to `%s' option" (invocation-name) switch))
+ (setq x-session-previous-id (car x-invocation-args)
+ x-invocation-args (cdr x-invocation-args)))
+
+(defvar emacs-save-session-functions nil
+ "Functions to run when a save-session event occurs.
+The functions does not get any argument.
+Functions can return non-nil to inform the session manager that the
+window system shutdown should be aborted.
+
+See also `emacs-session-save'.")
+
+(defun emacs-session-filename (session-id)
+ "Construct a filename to save the session in based on SESSION-ID.
+If the directory ~/.emacs.d exists, we make a filename in there, otherwise
+a file in the home directory."
+ (let ((basename (concat "session." session-id))
+ (emacs-dir "~/.emacs.d/"))
+ (expand-file-name (if (file-directory-p emacs-dir)
+ (concat emacs-dir basename)
+ (concat "~/.emacs-" basename)))))
+
+(defun emacs-session-save ()
+ "This function is called when the window system is shutting down.
+If this function returns non-nil, the window system shutdown is cancelled.
+
+When a session manager tells Emacs that the window system is shutting
+down, this function is called. It calls the functions in the hook
+`emacs-save-session-functions'. Functions are called with the current
+buffer set to a temporary buffer. Functions should use `insert' to insert
+lisp code to save the session state. The buffer is saved
+in a file in the home directory of the user running Emacs. The file
+is evaluated when Emacs is restarted by the session manager.
+
+If any of the functions returns non-nil, no more functions are called
+and this function returns non-nil. This will inform the session manager
+that it should abort the window system shutdown."
+ (let ((filename (emacs-session-filename x-session-id))
+ (buf (get-buffer-create (concat " *SES " x-session-id))))
+ (when (file-exists-p filename)
+ (delete-file filename))
+ (with-current-buffer buf
+ (let ((cancel-shutdown (condition-case nil
+ ;; A return of t means cancel the shutdown.
+ (run-hook-with-args-until-success
+ 'emacs-save-session-functions)
+ (error t))))
+ (unless cancel-shutdown
+ (write-file filename))
+ (kill-buffer buf)
+ cancel-shutdown))))
+
+(defun emacs-session-restore (previous-session-id)
+ "Restore the Emacs session if started by a session manager.
+The file saved by `emacs-session-save' is evaluated and deleted if it
+exists."
+ (let ((filename (emacs-session-filename previous-session-id)))
+ (when (file-exists-p filename)
+ (load-file filename)
+ (delete-file filename)
+ (message "Restored session data"))))
+