0

I want to setup session with use-package but I don't know elisp except copying from working code. Here is what I've copied:

;;;_* session
(use-package session
  :if (not noninteractive)
  :load-path "site-lisp/session/lisp/"
  :init
  (progn
    (session-initialize)
    (defun remove-session-use-package-from-settings ()
      (when (string= (file-name-nondirectory (buffer-file-name)) "settings.el")
        (save-excursion
          (goto-char (point-min))
          (when (re-search-forward "^ '(session-use-package " nil t)
            (delete-region (line-beginning-position)
                           (1+ (line-end-position)))))))
    (add-hook 'before-save-hook 'remove-session-use-package-from-settings)
    ;; 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)
    (defun save-information ()
      (with-temp-message "Saving Emacs information..."
        (recentf-cleanup)
        (loop for func in kill-emacs-hook
              unless (memq func '(exit-gnus-on-exit server-force-stop))
              do (funcall func))
        (unless (or noninteractive
                    running-alternate-emacs
                    (eq 'listen (process-status server-process)))
          (server-start))))
    (run-with-idle-timer 300 t 'save-information)
    (if window-system
        (add-hook 'after-init-hook 'session-initialize t))))

But I am getting the following error with --debug-init:

Debugger entered--Lisp error: (void-function session-initialize)
  (session-initialize)
  (progn (session-initialize) (defalias 'remove-session-use-package-from-settings #'(lambda nil (if (string= (file-name-nondirectory (buffer-file-name)) "settings.el") (progn (save-excursion (goto-char ...) (if ... ...)))))) (add-hook 'before-save-hook 'remove-session-use-package-from-settings) (defalias 'le::maybe-reveal #'(lambda nil (if (and (or (memq major-mode ...) (and ... outline-minor-mode)) (outline-invisible-p)) (progn (if (eq major-mode ...) (org-reveal) (show-subtree)))))) (add-hook 'session-after-jump-to-last-change-hook 'le::maybe-reveal) (defalias 'save-information #'(lambda nil (let ((with-temp-message "Saving Emacs information...") (current-message)) (unwind-protect (progn (if with-temp-message ...) (recentf-cleanup) (let* ... ... nil) (if ... nil ...)) (and with-temp-message (if current-message ... ...)))))) (run-with-idle-timer 300 t 'save-information) (if window-system (add-hook 'after-init-hook 'session-initialize t)))
  (condition-case err (progn (session-initialize) (defalias 'remove-session-use-package-from-settings #'(lambda nil (if (string= (file-name-nondirectory ...) "settings.el") (progn (save-excursion ... ...))))) (add-hook 'before-save-hook 'remove-session-use-package-from-settings) (defalias 'le::maybe-reveal #'(lambda nil (if (and (or ... ...) (outline-invisible-p)) (progn (if ... ... ...))))) (add-hook 'session-after-jump-to-last-change-hook 'le::maybe-reveal) (defalias 'save-information #'(lambda nil (let ((with-temp-message "Saving Emacs information...") (current-message)) (unwind-protect (progn ... ... ... ...) (and with-temp-message ...))))) (run-with-idle-timer 300 t 'save-information) (if window-system (add-hook 'after-init-hook 'session-initialize t))) ((debug error) (funcall use-package--warning10 :init err)))
  (progn (condition-case err (progn (session-initialize) (defalias 'remove-session-use-package-from-settings #'(lambda nil (if (string= ... "settings.el") (progn ...)))) (add-hook 'before-save-hook 'remove-session-use-package-from-settings) (defalias 'le::maybe-reveal #'(lambda nil (if (and ... ...) (progn ...)))) (add-hook 'session-after-jump-to-last-change-hook 'le::maybe-reveal) (defalias 'save-information #'(lambda nil (let (... ...) (unwind-protect ... ...)))) (run-with-idle-timer 300 t 'save-information) (if window-system (add-hook 'after-init-hook 'session-initialize t))) ((debug error) (funcall use-package--warning10 :init err))) (if (not (require 'session nil t)) (display-warning 'use-package (format "Cannot load %s" 'session) :error)))
  (condition-case err (progn (condition-case err (progn (session-initialize) (defalias 'remove-session-use-package-from-settings #'(lambda nil (if ... ...))) (add-hook 'before-save-hook 'remove-session-use-package-from-settings) (defalias 'le::maybe-reveal #'(lambda nil (if ... ...))) (add-hook 'session-after-jump-to-last-change-hook 'le::maybe-reveal) (defalias 'save-information #'(lambda nil (let ... ...))) (run-with-idle-timer 300 t 'save-information) (if window-system (add-hook 'after-init-hook 'session-initialize t))) ((debug error) (funcall use-package--warning10 :init err))) (if (not (require 'session nil t)) (display-warning 'use-package (format "Cannot load %s" 'session) :error))) ((debug error) (funcall use-package--warning10 :catch err)))
  (progn (defvar use-package--warning10 #'(lambda (keyword err) (let ((msg (format "%s/%s: %s" ... keyword ...))) (display-warning 'use-package msg :error)))) (condition-case err (progn (condition-case err (progn (session-initialize) (defalias 'remove-session-use-package-from-settings #'(lambda nil ...)) (add-hook 'before-save-hook 'remove-session-use-package-from-settings) (defalias 'le::maybe-reveal #'(lambda nil ...)) (add-hook 'session-after-jump-to-last-change-hook 'le::maybe-reveal) (defalias 'save-information #'(lambda nil ...)) (run-with-idle-timer 300 t 'save-information) (if window-system (add-hook 'after-init-hook 'session-initialize t))) ((debug error) (funcall use-package--warning10 :init err))) (if (not (require 'session nil t)) (display-warning 'use-package (format "Cannot load %s" 'session) :error))) ((debug error) (funcall use-package--warning10 :catch err))))
  (if (not noninteractive) (progn (defvar use-package--warning10 #'(lambda (keyword err) (let ((msg ...)) (display-warning 'use-package msg :error)))) (condition-case err (progn (condition-case err (progn (session-initialize) (defalias 'remove-session-use-package-from-settings #'...) (add-hook 'before-save-hook 'remove-session-use-package-from-settings) (defalias 'le::maybe-reveal #'...) (add-hook 'session-after-jump-to-last-change-hook 'le::maybe-reveal) (defalias 'save-information #'...) (run-with-idle-timer 300 t 'save-information) (if window-system (add-hook ... ... t))) ((debug error) (funcall use-package--warning10 :init err))) (if (not (require 'session nil t)) (display-warning 'use-package (format "Cannot load %s" 'session) :error))) ((debug error) (funcall use-package--warning10 :catch err)))))
  eval-buffer(#<buffer  *load*-868872> nil "/home/...

I cannot figure out anything from the above error message.
Somebody give me a working use-package session example please.

1

use-package's :init block is run before the package has been loaded. Try putting the code inside a :config block instead, like:

(use-package session
  :config
  (session-initialize)
  (defun ...

I also removed the progn, as use-package allows multiple statements in the :config block.

EDIT: as requested in a comment, the desktop-save-mode use-package block:

(use-package desktop
  :ensure nil
  :config
  (desktop-save-mode))
3
  • Thanks a lot zck, will it work eventually? The "stack trace" is now gone but I got "Error (use-package): Cannot load session" but nothing else, with --debug-init, or even debug-on-error. Could you post the full use-package session code that works for you please? thx – xpt Jun 13 at 14:14
  • Unfortunately, I have never used session. I do use desktop-save-mode to do similar functionality, but not session. You might consider getting session to work outside of use-package first, then moving it into use-package. That might be easier. – zck Jun 14 at 17:11
  • In that case, post your use-package setting with desktop-save-mode please, zck. I just want a working solution to save the state of Emacs from one session to another, but reading the page of gnu.org/software/emacs/manual/html_node/elisp/… gives me no clue how to use it. thx – xpt Jun 15 at 2:17
0

Adapted from https://github.com/ronert/.emacs.d/blob/master/lisp/init-sessions.el#L29

(use-package session
  :ensure t
  ;:pin melpa-stable
  :config
  (progn
    (setq session-save-file (expand-file-name "~/.emacs.d/.session"))
    (add-hook 'after-init-hook 'session-initialize)

    ;; save a bunch of variables to the desktop file
    ;; for lists specify the len of the maximal saved data also
    (setq desktop-globals-to-save
          (append '((extended-command-history . 30)
                    (file-name-history        . 100)
                    (grep-history             . 30)
                    (compile-history          . 30)
                    (minibuffer-history       . 50)
                    (query-replace-history    . 60)
                    (read-expression-history  . 60)
                    (regexp-history           . 60)
                    (regexp-search-ring       . 20)
                    (search-ring              . 20)
                    (comint-input-ring        . 50)
                    (shell-command-history    . 50)
                    desktop-missing-file-warning
                    tags-file-name
                    register-alist)))
    ))

Tested fine.

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.