5

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.

Community
  • 1
  • 1
amoe
  • 4,473
  • 4
  • 31
  • 51
  • `"... has auto save data; consider ..."` is just a message, not a dialog. The reason why the daemon hangs must be something else. – Rörd Oct 21 '13 at 10:33
  • Perhaps taking a look at `after-find-file` within `files.el` might give you some ideas on how to resolve the confirmation issue. Alternatively, take a look at the function that calls `after-find-file` when loading Emacs with `--daemon`. – lawlist Oct 21 '13 at 14:25

3 Answers3

1

One possibility would be to put this in your .emacs:

(setq auto-save-default nil)

Another (probably better solution) is to suppress the warning message by using this to find files instead:

(find-file-noselect FILENAME &optional NOWARN RAWFILE WILDCARDS)

As you can see here, you can suppress the warning message using the optional NOWARN argument (because that is what is causing the problem).

Source: this EmacsWiki page

Here is a change you could make if I were to solve this for myself. Define in the .emacs setup:

(defun find-file (filename &optional wildcards)
  (interactive
   (find-file-read-args "Find file: "
                        (confirm-nonexistent-file-or-buffer)))

  ; the "t" here is normally set to "nil", this should solve the problem
  (let ((value (find-file-noselect filename t nil wildcards)))  
    (if (listp value)
    (mapcar 'switch-to-buffer (nreverse value))
      (switch-to-buffer value))))

EDIT: This did not help anyone so far, but for completeness it might help out some.

The current way confirmed by the questioner is to use the emacs-startup-hook and combine it with (kill-buffer "*scratch*") and (find-file "~/.../todo.org").

PascalVKooten
  • 20,643
  • 17
  • 103
  • 160
  • Thanks for the help Dualinity. Unfortunately I am unable to reproduce the original issue at the moment. I will test and accept the answer when I can reproduce it. – amoe Apr 02 '13 at 22:55
  • That didn't help. `emacs --daemon` still hangs while loading – Sergey May 01 '13 at 08:44
  • Perhaps the `initial-buffer-choice` is being activated in the start-up sequence *before* `(setq auto-save-default nil)`. If changing the order has no effect, then consider using `(setq initial-buffer-choice t)` in addition to `(setq auto-save-default nil)`, and then use the `emacs-startup-hook` to `(kill-buffer "*scratch*")` `(find-file "~/.../todo.org")`. – lawlist Oct 21 '13 at 14:29
  • This final one worked -- using `emacs-startup-hook`, although it's a horrible hack, if you post it as an answer I can accept it. – amoe Oct 24 '13 at 12:46
1

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]).

lawlist
  • 13,099
  • 3
  • 49
  • 158
0

Since "...has auto save data; consider M-x recover-this-file" is only a message I am not sure that the problem is really related to the auto-save file. But, if this is the case you could built a test whether the auto-save directory for emacs is empty (directory-files) into your server init file. If the auto-save directory is not empty, move all the files to another place and go on (rename-file).

I am really curious whether this would solve the problem.

The real question is: How to debug server problems in emacs. Debug and edebug are pretty sensless in servermode. One should better trace everything to a file (something like logging).

Tobias
  • 5,038
  • 1
  • 18
  • 39
  • Yes, this solution worked, super hacky: `(let ((autosave-path "/home/amoe/etc/autosave")) (dolist (autosave-file (directory-files autosave-path nil "^#")) (rename-file (concat autosave-path "/" autosave-file) (concat "/tmp/" autosave-file))))` – amoe Oct 24 '13 at 12:59