Search code examples
emacsadvising-functions

How can I prevent emacs hanging when initial-buffer-choice has auto save data?


I use emacs in daemon mode and I also have an initial-buffer-choice variable set. Sometimes emacs will crash when I am editing the file that I use for initial-buffer-choice. In this case, when I start emacs with --daemon, it will hang with the message:

"todo.org has auto save data; consider M-x recover-this-file"

Since I mostly start the daemon from an init script, I can't confirm or deny this dialog, so the daemon hangs forever. How can I bypass the notification of auto-save data in this case? I don't mind losing the auto save data if necessary.

Here was my attempt to do it:

(defadvice command-line
  (around my-command-line-advice)
  "Be non-interactive while starting a daemon."
  (if (and (daemonp)
           (not server-process))
      (let ((noninteractive t))
        ad-do-it)
    ad-do-it))
 (ad-activate 'command-line)

However, this doesn't work. I still get the same hanging behaviour. Indeed, putting a 'message' call inside the advice shows that the advice isn't invoked at all.

Similar question: emacs-daemon startup freezes if file has auto-save data. However this solution does not work for initial-buffer-choice. The accepted answer seems to have been edited from a previous version which may have successfully defined advice on command-line as I attempted to do, but unfortunately this version is now gone and replaced with a desktop.el-specific version.


Solution

  • Based upon the description of the behavior reported by the original poster in the question to this thread, it would appear that when Emacs is activated with --daemon, the initial-buffer-choice (i.e., to find-file-noselect "~/.../todo.org" [see ... lisp/startup.el]) is being activated in the start-up sequence before the default setting of auto-save can be disabled with (setq auto-save-default nil). Provided that changing the order has no effect [i.e., placing (setq auto-save-default nil) higher up in priority in the initialization file so that it precedes the initial-buffer-choice], then the next step is to take affirmative action to ensure that auto-save is disabled before opening a file (e.g., todo.org). This can be achieved by placing (setq initial-buffer-choice t) and (setq auto-save-default nil) in the init.el or .emacs file (without a hook) -- then, to ensure that all other settings have been loaded first, use the emacs-startup-hook to (kill-buffer "*scratch*") and (find-file "~/.../todo.org") -- this ensures that auto-save is disabled before find-file is called (which uses find-file-noselect [see ... lisp/files.el]).