I know that we can start emacs without opening last session's files and status, start it by:
emacs --no-desktop
But how do we do that in the init.el
file?
Just ensure that desktop-save-mode
is turned off in your init file.
If the mode is on when Emacs starts then your saved desktop file is loaded. If the mode is off then it is not loaded.
You can then turn the mode on anytime you like, to enable automatic saving. Or you can just use M-x desktop-save
to manually save it anytime.
From the doc string of desktop-save-mode
:
When Desktop Save mode is enabled, the state of Emacs is saved from one session to another. In particular, Emacs will save the desktop when it exits (this may prompt you; see the option `desktop-save'). The next time Emacs starts, if this mode is active it will restore the desktop.
Yes, this design of automatic loading being coupled with automatic saving is a bit bizarre, IMHO. But it's not the most bizarre thing in the design of desktop.el.
That distinction belongs instead to the assumption (built into the signatures of the functions) that there is at most one desktop file per directory.
That a user might find it convenient to have at most one such file per directory is one thing. But to make the functions depend on that assumption is nuts.
Take a look, for example, at function desktop-read
. Then imagine that you want to use it to read a desktop file that might be located anywhere, passing the absolute file name. I had to roll my own function to do that (bmkp-desktop-read
, which takes a file name as argument).
after-init-hook
and make sure that it is removed before theafter-init-hook
runs. Because it is alambda
function defined indesktop.el
beginning at line 1513, you may need to remove it the same way. Your version of Emacs may be different than mine so, you should opendesktop.el
and copy it -- your code that will be added to yourinit.el
will probably look something like(remove-hook 'after-init-hook (lambda () (let ((key "--no-desktop")) (when (member key command-line-args) . . .)
I haven't actually tried it.desktop.el
after theafter-init-hook
has already run -- e.g., load / requiredesktop.el
using theemacs-startup-hook
, which runs after theafter-init-hook
. This will only work if nothing else loads / requiresdesktop.el
beforehand. Again, I haven't actually tried it.