0

I want to restart Emacs at the same cursor position in the file (or buffer) I was just working on.

In my Emacs config I have the following code to try and do this:

(use-package desktop
      :straight nil
      ;;:defer 1
      :init
      ;; Automatically save and restore sessions
      ;; FIX: Replace hardcoded desktop-dirname with my-savefile-dir
      (setq desktop-dirname             "~/.emacs.d/desktop/"
        desktop-base-file-name      "emacs.desktop"
        desktop-base-lock-name      "lock"
        desktop-path                (list desktop-dirname)
        desktop-save                t
        desktop-files-not-to-save   "^$" ;reload tramp paths
        desktop-load-locked-desktop nil
        desktop-auto-save-timeout   30
        desktop-restore-reuses-frames nil
        desktop-buffers-not-to-save
        (concat "\\("
            "^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS"
            "\\|\\.emacs.*\\|\\.diary\\|\\.newsrc-dribble\\|\\.bbdb"
            "\\)$"))
      (setq desktop-restore-eager 1)
      (desktop-save-mode 1)
      (desktop-read)
      :config
      (add-to-list 'desktop-modes-not-to-save 'dired-mode)
      ;;(add-to-list 'desktop-modes-not-to-save 'Info-mode)
      ;;(add-to-list 'desktop-modes-not-to-save 'info-lookup-mode)
      (add-to-list 'desktop-modes-not-to-save 'fundamental-mode))

Note: This is code updated slightly from the original post, but the issue still remains.

It seems to save the cursor in the given file if I exit.

However the problem is that when Emacs opens, it opens many buffers in separate windows. Each Emacs window appears to be a clone of the others . This is confusing and not the intended outcome. Not sure what is causing this issue.

For example, if I was working on file1.org and left it at column 10, line 20, then I want a single Emacs window to open in file1.org at column 10, line 20. I don't want multiple windows to open with any previously visited Dired buffers etc.

I can confirm that if I comment out the above code, the problem goes away, but so does the required effect, i.e., I no longer start at file1.org at the given location.

Here is a screenshot of the multiple windows that open when I startup Emacs.App (v28.1) on my macOS (Monterey v12.1):

enter image description here

I can clarify my settings further if needed.

8
  • I'm not quite got your question, but maybe you can try (setq desktop-restore-eager 1) to restore only the frontmost buffer immediately. Jun 9, 2022 at 6:07
  • @TianshuWang - thanks for your feedback. This didn't work. The main issue is that with the current setting above, multiple emacs windows open when I re-launch emacs. If I was last working in file1.org column 10, line 20, this is exactly where I would like emacs to have my cursor on startup. Right now it is opening up dired buffers etc, even though I tried to ignore them. Does that clarify better? Jun 9, 2022 at 17:54
  • @TianshuWang I've added a screenshot to make it easier to see the issue. Three emacs windows open, when I would just like one window to open. Jun 9, 2022 at 18:00
  • I cannot reproduce with emacs -Q with your config like this. Would you mind delete your desktop file and check with emacs -Q? Jun 10, 2022 at 13:14
  • @TianshuWang - Thanks for your efforts and suggestion. I tried to delete the emacs.d/desktop/emacs.desktop and emacs.d/desktop/lock files and restart. It did work as intended once. But since then I can't reproduce this even if I keep deleting these files and restarting. I've updated my config slightly in the original post. Is there some issue with the lock file setting? Jun 11, 2022 at 0:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Browse other questions tagged or ask your own question.