Use accented form of some names
[bpt/emacs.git] / lisp / desktop.el
CommitLineData
a912c016 1;;; desktop.el --- save partial status of Emacs when killed -*- lexical-binding: t -*-
6343ed61 2
9271e90e 3;; Copyright (C) 1993-1995, 1997, 2000-2013 Free Software Foundation, Inc.
6343ed61
RS
4
5;; Author: Morten Welinder <terra@diku.dk>
3ea96cac 6;; Keywords: convenience
c5e87d10 7;; Favorite-brand-of-beer: None, I hate beer.
6343ed61
RS
8
9;; This file is part of GNU Emacs.
10
eb3fa2cf 11;; GNU Emacs is free software: you can redistribute it and/or modify
6343ed61 12;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
13;; the Free Software Foundation, either version 3 of the License, or
14;; (at your option) any later version.
6343ed61
RS
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
eb3fa2cf 22;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
6343ed61
RS
23
24;;; Commentary:
25
0b9bd504
RS
26;; Save the Desktop, i.e.,
27;; - some global variables
28;; - the list of buffers with associated files. For each buffer also
29;; - the major mode
30;; - the default directory
31;; - the point
32;; - the mark & mark-active
33;; - buffer-read-only
ec4c6f22 34;; - some local variables
b958c0ad 35;; - frame and window configuration
6343ed61 36
6b61353c 37;; To use this, use customize to turn on desktop-save-mode or add the
865fe16f 38;; following line somewhere in your init file:
0b9bd504 39;;
6b61353c 40;; (desktop-save-mode 1)
0b9bd504 41;;
6b61353c 42;; For further usage information, look at the section
20535d5b 43;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual.
6b61353c
KH
44
45;; When the desktop module is loaded, the function `desktop-kill' is
341c2f07 46;; added to the `kill-emacs-hook'. This function is responsible for
6b61353c 47;; saving the desktop when Emacs is killed. Furthermore an anonymous
341c2f07 48;; function is added to the `after-init-hook'. This function is
6b61353c 49;; responsible for loading the desktop when Emacs is started.
ec4c6f22 50
0f4804e7
LH
51;; Special handling.
52;; -----------------
53;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
54;; are supplied to handle special major and minor modes respectively.
55;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
341c2f07 56;; to restore a desktop buffer. Elements must have the form
be617bbf 57;;
0f4804e7 58;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
be617bbf 59;;
0f4804e7 60;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
341c2f07 61;; evaluates the desktop file. Buffers with a major mode not specified here,
0f4804e7
LH
62;; are restored by the default handler `desktop-restore-file-buffer'.
63;; `desktop-minor-mode-handlers' is an alist of functions to restore
64;; non-standard minor modes. Elements must have the form
be617bbf 65;;
0f4804e7 66;; (MINOR-MODE . RESTORE-FUNCTION).
be617bbf 67;;
0f4804e7
LH
68;; Functions are called by `desktop-create-buffer' to restore minor modes.
69;; Minor modes not specified here, are restored by the standard minor mode
70;; function. If you write a module that defines a major or minor mode that
71;; needs a special handler, then place code like
72
73;; (defun foo-restore-desktop-buffer
74;; ...
75;; (add-to-list 'desktop-buffer-mode-handlers
76;; '(foo-mode . foo-restore-desktop-buffer))
77
78;; or
79
80;; (defun bar-desktop-restore
81;; ...
82;; (add-to-list 'desktop-minor-mode-handlers
83;; '(bar-mode . bar-desktop-restore))
84
fb8a6326 85;; in the module itself, and make sure that the mode function is
341c2f07 86;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
0f4804e7
LH
87;; `desktop-minor-mode-handlers' for more info.
88
89;; Minor modes.
90;; ------------
91;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
92;; manual) are handled in the following way:
93;; When `desktop-save' saves the state of a buffer to the desktop file, it
94;; saves as `desktop-minor-modes' the list of names of those variables in
95;; `minor-mode-alist' that have a non-nil value.
96;; When `desktop-create' restores the buffer, each of the symbols in
97;; `desktop-minor-modes' is called as function with parameter 1.
98;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
99;; are used to handle non-conventional minor modes. `desktop-save' uses
100;; `desktop-minor-mode-table' to map minor mode variables to minor mode
341c2f07 101;; functions before writing `desktop-minor-modes'. If a minor mode has a
0f4804e7
LH
102;; variable name that is different form its function name, an entry
103
104;; (NAME RESTORE-FUNCTION)
105
106;; should be added to `desktop-minor-mode-table'. If a minor mode should not
107;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
108;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
109;; function different from the usual minor mode function.
110;; ---------------------------------------------------------------------------
47640244 111
ec4c6f22
RS
112;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
113;; in your home directory is used for that. Saving global default values
114;; for buffers is an example of misuse.
115
0b9bd504
RS
116;; PLEASE NOTE: The kill ring can be saved as specified by the variable
117;; `desktop-globals-to-save' (by default it isn't). This may result in saving
118;; things you did not mean to keep. Use M-x desktop-clear RET.
ec4c6f22 119
fa379636
KH
120;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
121;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
122;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
123;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
124;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
f4c73d07 125;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
8c106d17 126;; pot@cnuce.cnr.it (Francesco Potortì) for misc. tips.
0b9bd504
RS
127;; ---------------------------------------------------------------------------
128;; TODO:
129;;
0b9bd504
RS
130;; Recognize more minor modes.
131;; Save mark rings.
6343ed61
RS
132
133;;; Code:
134
a1c80d9d 135(require 'cl-lib)
9421876d 136(require 'frameset)
a1c80d9d 137
4a2fce7a 138(defvar desktop-file-version "206"
6b61353c 139 "Version number of desktop file format.
4a2fce7a
RS
140Written into the desktop file and used at desktop read to provide
141backward compatibility.")
142
ec4c6f22 143;; ----------------------------------------------------------------------------
0b9bd504
RS
144;; USER OPTIONS -- settings you might want to play with.
145;; ----------------------------------------------------------------------------
bbf5eb28
RS
146
147(defgroup desktop nil
148 "Save status of Emacs when you exit."
149 :group 'frames)
150
e5bd0a28
SM
151;; Maintained for backward compatibility
152(define-obsolete-variable-alias 'desktop-enable 'desktop-save-mode "22.1")
6b61353c
KH
153;;;###autoload
154(define-minor-mode desktop-save-mode
06e21633
CY
155 "Toggle desktop saving (Desktop Save mode).
156With a prefix argument ARG, enable Desktop Save mode if ARG is
157positive, and disable it otherwise. If called from Lisp, enable
158the mode if ARG is omitted or nil.
159
160If Desktop Save mode is enabled, the state of Emacs is saved from
161one session to another. See variable `desktop-save' and function
162`desktop-read' for details."
6b61353c
KH
163 :global t
164 :group 'desktop)
165
e88110db
JB
166(defun desktop-save-mode-off ()
167 "Disable `desktop-save-mode'. Provided for use in hooks."
168 (desktop-save-mode 0))
169
4a2fce7a 170(defcustom desktop-save 'ask-if-new
9201cc28 171 "Specifies whether the desktop should be saved when it is killed.
6b61353c
KH
172A desktop is killed when the user changes desktop or quits Emacs.
173Possible values are:
4a2fce7a
RS
174 t -- always save.
175 ask -- always ask.
176 ask-if-new -- ask if no desktop file exists, otherwise just save.
177 ask-if-exists -- ask if desktop file exists, otherwise don't save.
178 if-exists -- save if desktop file exists, otherwise don't save.
179 nil -- never save.
6b61353c 180The desktop is never saved when `desktop-save-mode' is nil.
cc8b76bf 181The variables `desktop-dirname' and `desktop-base-file-name'
6b61353c 182determine where the desktop is saved."
11425834
LH
183 :type
184 '(choice
4a2fce7a
RS
185 (const :tag "Always save" t)
186 (const :tag "Always ask" ask)
187 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
188 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
189 (const :tag "Save if desktop file exists, else don't" if-exists)
190 (const :tag "Never save" nil))
af61551b 191 :group 'desktop
bf247b6e 192 :version "22.1")
4a2fce7a 193
5db9dace
JL
194(defcustom desktop-auto-save-timeout nil
195 "Number of seconds between auto-saves of the desktop.
196Zero or nil means disable timer-based auto-saving."
197 :type '(choice (const :tag "Off" nil)
198 (integer :tag "Seconds"))
199 :set (lambda (symbol value)
200 (set-default symbol value)
acfcc8c5 201 (ignore-errors (desktop-auto-save-set-timer)))
5db9dace
JL
202 :group 'desktop
203 :version "24.4")
204
1f7efe1b
JB
205(defcustom desktop-load-locked-desktop 'ask
206 "Specifies whether the desktop should be loaded if locked.
207Possible values are:
208 t -- load anyway.
209 nil -- don't load.
210 ask -- ask the user.
211If the value is nil, or `ask' and the user chooses not to load the desktop,
212the normal hook `desktop-not-loaded-hook' is run."
213 :type
214 '(choice
215 (const :tag "Load anyway" t)
216 (const :tag "Don't load" nil)
217 (const :tag "Ask the user" ask))
218 :group 'desktop
8f3f313d 219 :version "22.2")
1f7efe1b 220
cd6ef82d
GM
221(define-obsolete-variable-alias 'desktop-basefilename
222 'desktop-base-file-name "22.1")
223
4a2fce7a 224(defcustom desktop-base-file-name
fc715501 225 (convert-standard-filename ".emacs.desktop")
6b61353c 226 "Name of file for Emacs desktop, excluding the directory part."
813dbb2d
RS
227 :type 'file
228 :group 'desktop)
6343ed61 229
e88110db
JB
230(defcustom desktop-base-lock-name
231 (convert-standard-filename ".emacs.desktop.lock")
232 "Name of lock file for Emacs desktop, excluding the directory part."
233 :type 'file
234 :group 'desktop
8f3f313d 235 :version "22.2")
e88110db 236
e76f0800 237(defcustom desktop-path (list user-emacs-directory "~")
4a2fce7a
RS
238 "List of directories to search for the desktop file.
239The base name of the file is specified in `desktop-base-file-name'."
240 :type '(repeat directory)
af61551b 241 :group 'desktop
a3b337cd 242 :version "23.2") ; user-emacs-directory added
4a2fce7a 243
bbf5eb28 244(defcustom desktop-missing-file-warning nil
6c27fdb9 245 "If non-nil, offer to recreate the buffer of a deleted file.
ebb39555
LH
246Also pause for a moment to display message about errors signaled in
247`desktop-buffer-mode-handlers'.
248
249If nil, just print error messages in the message buffer."
bbf5eb28 250 :type 'boolean
af61551b 251 :group 'desktop
bf247b6e 252 :version "22.1")
6343ed61 253
4a2fce7a 254(defcustom desktop-no-desktop-file-hook nil
6b61353c 255 "Normal hook run when `desktop-read' can't find a desktop file.
11425834 256Run in the directory in which the desktop file was sought.
8649a87b 257May be used to show a dired buffer."
4a2fce7a 258 :type 'hook
af61551b 259 :group 'desktop
bf247b6e 260 :version "22.1")
4a2fce7a 261
e88110db
JB
262(defcustom desktop-not-loaded-hook nil
263 "Normal hook run when the user declines to re-use a desktop file.
264Run in the directory in which the desktop file was found.
265May be used to deal with accidental multiple Emacs jobs."
266 :type 'hook
267 :group 'desktop
268 :options '(desktop-save-mode-off save-buffers-kill-emacs)
8f3f313d 269 :version "22.2")
e88110db 270
4a2fce7a 271(defcustom desktop-after-read-hook nil
6b61353c 272 "Normal hook run after a successful `desktop-read'.
8649a87b 273May be used to show a buffer list."
4a2fce7a 274 :type 'hook
af61551b 275 :group 'desktop
11425834 276 :options '(list-buffers)
bf247b6e 277 :version "22.1")
4a2fce7a
RS
278
279(defcustom desktop-save-hook nil
6b61353c 280 "Normal hook run before the desktop is saved in a desktop file.
11425834
LH
281Run with the desktop buffer current with only the header present.
282May be used to add to the desktop code or to truncate history lists,
283for example."
4a2fce7a
RS
284 :type 'hook
285 :group 'desktop)
286
340db502
LH
287(defcustom desktop-globals-to-save
288 '(desktop-missing-file-warning
289 tags-file-name
290 tags-table-list
291 search-ring
292 regexp-search-ring
c760f19e
CY
293 register-alist
294 file-name-history)
6b61353c
KH
295 "List of global variables saved by `desktop-save'.
296An element may be variable name (a symbol) or a cons cell of the form
297\(VAR . MAX-SIZE), which means to truncate VAR's value to at most
298MAX-SIZE elements (if the value is a list) before saving the value.
4a2fce7a
RS
299Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
300 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
301 :group 'desktop)
302
340db502
LH
303(defcustom desktop-globals-to-clear
304 '(kill-ring
305 kill-ring-yank-pointer
306 search-ring
307 search-ring-yank-pointer
308 regexp-search-ring
309 regexp-search-ring-yank-pointer)
150f39ee 310 "List of global variables that `desktop-clear' will clear.
4a2fce7a 311An element may be variable name (a symbol) or a cons cell of the form
cc8b76bf
JB
312\(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
313to the value obtained by evaluating FORM."
4a2fce7a 314 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
af61551b 315 :group 'desktop
bf247b6e 316 :version "22.1")
4a2fce7a 317
0f4804e7 318(defcustom desktop-clear-preserve-buffers
3845c322
GM
319 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*"
320 "\\*Warnings\\*")
9201cc28 321 "List of buffers that `desktop-clear' should not delete.
0f4804e7
LH
322Each element is a regular expression. Buffers with a name matched by any of
323these won't be deleted."
3845c322 324 :version "23.3" ; added Warnings - bug#6336
4a2fce7a
RS
325 :type '(repeat string)
326 :group 'desktop)
340db502 327
0f4804e7 328;;;###autoload
340db502
LH
329(defcustom desktop-locals-to-save
330 '(desktop-locals-to-save ; Itself! Think it over.
331 truncate-lines
332 case-fold-search
333 case-replace
334 fill-column
335 overwrite-mode
336 change-log-default-name
337 line-number-mode
0f4804e7
LH
338 column-number-mode
339 size-indication-mode
340 buffer-file-coding-system
341 indent-tabs-mode
11425834 342 tab-width
0f4804e7
LH
343 indicate-buffer-boundaries
344 indicate-empty-lines
345 show-trailing-whitespace)
577ed2b2 346 "List of local variables to save for each buffer.
0f4804e7
LH
347The variables are saved only when they really are local. Conventional minor
348modes are restored automatically; they should not be listed here."
6b61353c
KH
349 :type '(repeat symbol)
350 :group 'desktop)
ec4c6f22 351
a6c2c80c 352(defcustom desktop-buffers-not-to-save nil
4a2fce7a 353 "Regexp identifying buffers that are to be excluded from saving."
a6c2c80c
EZ
354 :type '(choice (const :tag "None" nil)
355 regexp)
1a8d3541 356 :version "23.2" ; set to nil
4a2fce7a 357 :group 'desktop)
6343ed61 358
6b61353c 359;; Skip tramp and ange-ftp files
bbf5eb28 360(defcustom desktop-files-not-to-save
a6c2c80c 361 "\\(^/[^/:]*:\\|(ftp)$\\)"
bbf5eb28 362 "Regexp identifying files whose buffers are to be excluded from saving."
a6c2c80c
EZ
363 :type '(choice (const :tag "None" nil)
364 regexp)
bbf5eb28 365 :group 'desktop)
fa379636 366
df410295
JL
367;; We skip TAGS files to save time (tags-file-name is saved instead).
368(defcustom desktop-modes-not-to-save
369 '(tags-table-mode)
80f9f3db
SM
370 "List of major modes whose buffers should not be saved."
371 :type '(repeat symbol)
372 :group 'desktop)
373
b958c0ad 374(defcustom desktop-restore-frames t
9421876d 375 "When non-nil, save frames to desktop file."
39c0e36f
JB
376 :type 'boolean
377 :group 'desktop
378 :version "24.4")
379
2addf922 380(defcustom desktop-restore-in-current-display nil
b958c0ad
JB
381 "If t, frames are restored in the current display.
382If nil, frames are restored, if possible, in their original displays.
383If `delete', frames on other displays are deleted instead of restored."
384 :type '(choice (const :tag "Restore in current display" t)
385 (const :tag "Restore in original display" nil)
386 (const :tag "Delete frames in other displays" 'delete))
387 :group 'desktop
388 :version "24.4")
389
ddeffb17
JB
390(defcustom desktop-restore-forces-onscreen t
391 "If t, offscreen frames are restored onscreen instead.
526e5233 392If `:all', frames that are partially offscreen are also forced onscreen.
ddeffb17
JB
393NOTE: Checking of frame boundaries is only approximate and can fail
394to reliably detect frames whose onscreen/offscreen state depends on a
395few pixels, especially near the right / bottom borders of the screen."
396 :type '(choice (const :tag "Only fully offscreen frames" t)
d5671a82 397 (const :tag "Also partially offscreen frames" :all)
ddeffb17
JB
398 (const :tag "Do not force frames onscreen" nil))
399 :group 'desktop
400 :version "24.4")
401
9421876d 402(defcustom desktop-restore-reuses-frames t
b958c0ad
JB
403 "If t, restoring frames reuses existing frames.
404If nil, existing frames are deleted.
d5671a82 405If `:keep', existing frames are kept and not reused."
b958c0ad
JB
406 :type '(choice (const :tag "Reuse existing frames" t)
407 (const :tag "Delete existing frames" nil)
d5671a82 408 (const :tag "Keep existing frames" :keep))
2addf922
JB
409 :group 'desktop
410 :version "24.4")
411
4a2fce7a 412(defcustom desktop-file-name-format 'absolute
9201cc28 413 "Format in which desktop file names should be saved.
4a2fce7a
RS
414Possible values are:
415 absolute -- Absolute file name.
416 tilde -- Relative to ~.
417 local -- Relative to directory of desktop file."
418 :type '(choice (const absolute) (const tilde) (const local))
af61551b 419 :group 'desktop
bf247b6e 420 :version "22.1")
569c754e 421
150f39ee
LH
422(defcustom desktop-restore-eager t
423 "Number of buffers to restore immediately.
424Remaining buffers are restored lazily (when Emacs is idle).
425If value is t, all buffers are restored immediately."
0ba9bc53 426 :type '(choice (const t) integer)
150f39ee 427 :group 'desktop
bf247b6e 428 :version "22.1")
150f39ee
LH
429
430(defcustom desktop-lazy-verbose t
431 "Verbose reporting of lazily created buffers."
432 :type 'boolean
433 :group 'desktop
bf247b6e 434 :version "22.1")
150f39ee
LH
435
436(defcustom desktop-lazy-idle-delay 5
437 "Idle delay before starting to create buffers.
438See `desktop-restore-eager'."
439 :type 'integer
440 :group 'desktop
bf247b6e 441 :version "22.1")
150f39ee 442
e5780ae1 443;;;###autoload
acfcc8c5 444(defvar-local desktop-save-buffer nil
ebb39555 445 "When non-nil, save buffer status in desktop file.
ebb39555 446
b89c5a72
JB
447If the value is a function, it is called by `desktop-save' with argument
448DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
ebb39555 449file along with the state of the buffer for which it was called.
b6b70cda 450
6b61353c 451When file names are returned, they should be formatted using the call
e5780ae1 452\"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
4a2fce7a 453
0f4804e7
LH
454Later, when `desktop-read' evaluates the desktop file, auxiliary information
455is passed as the argument DESKTOP-BUFFER-MISC to functions in
456`desktop-buffer-mode-handlers'.")
ebb39555 457(make-obsolete-variable 'desktop-buffer-modes-to-save
cc8b76bf 458 'desktop-save-buffer "22.1")
e5780ae1 459(make-obsolete-variable 'desktop-buffer-misc-functions
cc8b76bf 460 'desktop-save-buffer "22.1")
b6b70cda 461
0f4804e7 462;;;###autoload
e76f0800 463(defvar desktop-buffer-mode-handlers nil
e5780ae1 464 "Alist of major mode specific functions to restore a desktop buffer.
0f4804e7
LH
465Functions listed are called by `desktop-create-buffer' when `desktop-read'
466evaluates the desktop file. List elements must have the form
467
468 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
e5780ae1
LH
469
470Buffers with a major mode not specified here, are restored by the default
471handler `desktop-restore-file-buffer'.
472
55775448 473Handlers are called with argument list
4a2fce7a 474
9bcabb45 475 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
e5780ae1
LH
476
477Furthermore, they may use the following variables:
4a2fce7a
RS
478
479 desktop-file-version
4a2fce7a
RS
480 desktop-buffer-major-mode
481 desktop-buffer-minor-modes
482 desktop-buffer-point
483 desktop-buffer-mark
484 desktop-buffer-read-only
4a2fce7a
RS
485 desktop-buffer-locals
486
e5780ae1 487If a handler returns a buffer, then the saved mode settings
0f4804e7
LH
488and variable values for that buffer are copied into it.
489
490Modules that define a major mode that needs a special handler should contain
491code like
ec4c6f22 492
0f4804e7
LH
493 (defun foo-restore-desktop-buffer
494 ...
495 (add-to-list 'desktop-buffer-mode-handlers
496 '(foo-mode . foo-restore-desktop-buffer))
497
498Furthermore the major mode function must be autoloaded.")
ec4c6f22 499
498eb267 500;;;###autoload
e5780ae1
LH
501(put 'desktop-buffer-mode-handlers 'risky-local-variable t)
502(make-obsolete-variable 'desktop-buffer-handlers
cc8b76bf 503 'desktop-buffer-mode-handlers "22.1")
b6b70cda 504
47640244
GM
505(defcustom desktop-minor-mode-table
506 '((auto-fill-function auto-fill-mode)
5fff0265 507 (vc-mode nil)
f2168a4c 508 (vc-dired-mode nil)
fb8a6326
JB
509 (erc-track-minor-mode nil)
510 (savehist-mode nil))
47640244
GM
511 "Table mapping minor mode variables to minor mode functions.
512Each entry has the form (NAME RESTORE-FUNCTION).
513NAME is the name of the buffer-local variable indicating that the minor
514mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
7d6ee4df 515RESTORE-FUNCTION nil means don't try to restore the minor mode.
47640244 516Only minor modes for which the name of the buffer-local variable
7bfa55b3 517and the name of the minor mode function are different have to be added to
0f4804e7 518this table. See also `desktop-minor-mode-handlers'."
47640244
GM
519 :type 'sexp
520 :group 'desktop)
521
0f4804e7 522;;;###autoload
e76f0800 523(defvar desktop-minor-mode-handlers nil
0f4804e7
LH
524 "Alist of functions to restore non-standard minor modes.
525Functions are called by `desktop-create-buffer' to restore minor modes.
526List elements must have the form
527
528 (MINOR-MODE . RESTORE-FUNCTION).
529
530Minor modes not specified here, are restored by the standard minor mode
531function.
532
533Handlers are called with argument list
534
535 (DESKTOP-BUFFER-LOCALS)
536
537Furthermore, they may use the following variables:
538
539 desktop-file-version
540 desktop-buffer-file-name
541 desktop-buffer-name
542 desktop-buffer-major-mode
543 desktop-buffer-minor-modes
544 desktop-buffer-point
545 desktop-buffer-mark
546 desktop-buffer-read-only
547 desktop-buffer-misc
548
549When a handler is called, the buffer has been created and the major mode has
550been set, but local variables listed in desktop-buffer-locals has not yet been
551created and set.
552
553Modules that define a minor mode that needs a special handler should contain
554code like
555
556 (defun foo-desktop-restore
557 ...
558 (add-to-list 'desktop-minor-mode-handlers
559 '(foo-mode . foo-desktop-restore))
560
561Furthermore the minor mode function must be autoloaded.
562
563See also `desktop-minor-mode-table'.")
564
498eb267 565;;;###autoload
0f4804e7
LH
566(put 'desktop-minor-mode-handlers 'risky-local-variable t)
567
0b9bd504
RS
568;; ----------------------------------------------------------------------------
569(defvar desktop-dirname nil
6b61353c 570 "The directory in which the desktop file should be saved.")
6343ed61 571
11425834
LH
572(defun desktop-full-file-name (&optional dirname)
573 "Return the full name of the desktop file in DIRNAME.
574DIRNAME omitted or nil means use `desktop-dirname'."
575 (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
576
e88110db
JB
577(defun desktop-full-lock-name (&optional dirname)
578 "Return the full name of the desktop lock file in DIRNAME.
579DIRNAME omitted or nil means use `desktop-dirname'."
580 (expand-file-name desktop-base-lock-name (or dirname desktop-dirname)))
581
6343ed61 582(defconst desktop-header
0b9bd504
RS
583";; --------------------------------------------------------------------------
584;; Desktop File for Emacs
585;; --------------------------------------------------------------------------
6343ed61 586" "*Header to place in Desktop file.")
de9e2828
RS
587
588(defvar desktop-delay-hook nil
589 "Hooks run after all buffers are loaded; intended for internal use.")
813dbb2d 590
5db9dace
JL
591(defvar desktop-file-checksum nil
592 "Checksum of the last auto-saved contents of the desktop file.
593Used to avoid writing contents unchanged between auto-saves.")
594
9421876d 595(defvar desktop-saved-frameset nil
56bc453c
JB
596 "Saved state of all frames.
597Only valid during frame saving & restoring; intended for internal use.")
39c0e36f 598
e88110db
JB
599;; ----------------------------------------------------------------------------
600;; Desktop file conflict detection
601(defvar desktop-file-modtime nil
602 "When the desktop file was last modified to the knowledge of this Emacs.
603Used to detect desktop file conflicts.")
604
605(defun desktop-owner (&optional dirname)
606 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
607Return nil if no desktop file found or no Emacs process is using it.
608DIRNAME omitted or nil means use `desktop-dirname'."
acfcc8c5
JB
609 (let (owner
610 (file (desktop-full-lock-name dirname)))
611 (and (file-exists-p file)
612 (ignore-errors
613 (with-temp-buffer
614 (insert-file-contents-literally file)
615 (goto-char (point-min))
616 (setq owner (read (current-buffer)))
617 (integerp owner)))
e88110db
JB
618 owner)))
619
620(defun desktop-claim-lock (&optional dirname)
621 "Record this Emacs process as the owner of the desktop file in DIRNAME.
622DIRNAME omitted or nil means use `desktop-dirname'."
623 (write-region (number-to-string (emacs-pid)) nil
624 (desktop-full-lock-name dirname)))
625
626(defun desktop-release-lock (&optional dirname)
627 "Remove the lock file for the desktop in DIRNAME.
628DIRNAME omitted or nil means use `desktop-dirname'."
629 (let ((file (desktop-full-lock-name dirname)))
630 (when (file-exists-p file) (delete-file file))))
631
0b9bd504 632;; ----------------------------------------------------------------------------
6b61353c 633(defun desktop-truncate (list n)
ec4c6f22 634 "Truncate LIST to at most N elements destructively."
6b61353c 635 (let ((here (nthcdr (1- n) list)))
1f7efe1b
JB
636 (when (consp here)
637 (setcdr here nil))))
f9be4574 638
4a2fce7a 639;; ----------------------------------------------------------------------------
11425834 640;;;###autoload
f9be4574
RS
641(defun desktop-clear ()
642 "Empty the Desktop.
0f4804e7
LH
643This kills all buffers except for internal ones and those with names matched by
644a regular expression in the list `desktop-clear-preserve-buffers'.
5414a283
JB
645Furthermore, it clears the variables listed in `desktop-globals-to-clear'.
646When called interactively and `desktop-restore-frames' is non-nil, it also
647deletes all frames except the selected one (and its minibuffer frame,
648if different)."
6343ed61 649 (interactive)
150f39ee 650 (desktop-lazy-abort)
4a2fce7a
RS
651 (dolist (var desktop-globals-to-clear)
652 (if (symbolp var)
1f7efe1b 653 (eval `(setq-default ,var nil))
4a2fce7a 654 (eval `(setq-default ,(car var) ,(cdr var)))))
b61d71e4 655 (let ((preserve-regexp (concat "^\\("
0f4804e7
LH
656 (mapconcat (lambda (regexp)
657 (concat "\\(" regexp "\\)"))
658 desktop-clear-preserve-buffers
659 "\\|")
660 "\\)$")))
b61d71e4
JB
661 (dolist (buffer (buffer-list))
662 (let ((bufname (buffer-name buffer)))
da77a2e2 663 (unless (or (eq (aref bufname 0) ?\s) ;; Don't kill internal buffers
b61d71e4
JB
664 (string-match-p preserve-regexp bufname))
665 (kill-buffer buffer)))))
9421876d 666 (delete-other-windows)
5414a283
JB
667 (when (and desktop-restore-frames
668 ;; Non-interactive calls to desktop-clear happen before desktop-read
669 ;; which already takes care of frame restoration and deletion.
670 (called-interactively-p 'any))
671 (let* ((this (selected-frame))
526e5233 672 (mini (window-frame (minibuffer-window this)))) ; in case they differ
063233c3 673 (dolist (frame (sort (frame-list) #'frameset-minibufferless-first-p))
5414a283
JB
674 (condition-case err
675 (unless (or (eq frame this)
676 (eq frame mini)
677 (frame-parameter frame 'desktop-dont-clear))
678 (delete-frame frame))
679 (error
680 (delay-warning 'desktop (error-message-string err))))))))
4a2fce7a 681
0b9bd504 682;; ----------------------------------------------------------------------------
845fc5e5
JB
683(unless noninteractive
684 (add-hook 'kill-emacs-hook 'desktop-kill))
ec4c6f22 685
6343ed61 686(defun desktop-kill ()
6b61353c 687 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
4a2fce7a
RS
688If the desktop should be saved and `desktop-dirname'
689is nil, ask the user where to save the desktop."
11425834
LH
690 (when (and desktop-save-mode
691 (let ((exists (file-exists-p (desktop-full-file-name))))
692 (or (eq desktop-save t)
7dd7fbb9
JD
693 (and exists (eq desktop-save 'if-exists))
694 ;; If it exists, but we aren't using it, we are going
695 ;; to ask for a new directory below.
696 (and exists desktop-dirname (eq desktop-save 'ask-if-new))
11425834
LH
697 (and
698 (or (memq desktop-save '(ask ask-if-new))
699 (and exists (eq desktop-save 'ask-if-exists)))
700 (y-or-n-p "Save desktop? ")))))
4a2fce7a
RS
701 (unless desktop-dirname
702 (setq desktop-dirname
11425834
LH
703 (file-name-as-directory
704 (expand-file-name
794855ca 705 (read-directory-name "Directory for desktop file: " nil nil t)))))
4a2fce7a 706 (condition-case err
e88110db 707 (desktop-save desktop-dirname t)
4a2fce7a 708 (file-error
11425834 709 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
e88110db
JB
710 (signal (car err) (cdr err))))))
711 ;; If we own it, we don't anymore.
712 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
4a2fce7a 713
0b9bd504 714;; ----------------------------------------------------------------------------
ed2f7fc8 715(defun desktop-list* (&rest args)
74d7f75a 716 (and args (apply #'cl-list* args)))
ed2f7fc8 717
e88110db
JB
718;; ----------------------------------------------------------------------------
719(defun desktop-buffer-info (buffer)
720 (set-buffer buffer)
721 (list
a8049a30
JB
722 ;; base name of the buffer; replaces the buffer name if managed by uniquify
723 (and (fboundp 'uniquify-buffer-base-name) (uniquify-buffer-base-name))
e88110db 724 ;; basic information
9e29c91c 725 (desktop-file-name (buffer-file-name) desktop-dirname)
a8049a30 726 (buffer-name)
e88110db
JB
727 major-mode
728 ;; minor modes
729 (let (ret)
730 (mapc
731 #'(lambda (minor-mode)
732 (and (boundp minor-mode)
733 (symbol-value minor-mode)
734 (let* ((special (assq minor-mode desktop-minor-mode-table))
735 (value (cond (special (cadr special))
736 ((functionp minor-mode) minor-mode))))
737 (when value (add-to-list 'ret value)))))
738 (mapcar #'car minor-mode-alist))
739 ret)
740 ;; point and mark, and read-only status
741 (point)
742 (list (mark t) mark-active)
743 buffer-read-only
744 ;; auxiliary information
745 (when (functionp desktop-save-buffer)
9e29c91c 746 (funcall desktop-save-buffer desktop-dirname))
e88110db 747 ;; local variables
b61d71e4
JB
748 (let ((loclist (buffer-local-variables))
749 (ll nil))
750 (dolist (local desktop-locals-to-save)
751 (let ((here (assq local loclist)))
752 (cond (here
753 (push here ll))
754 ((member local loclist)
755 (push local ll)))))
e88110db
JB
756 ll)))
757
4a2fce7a 758;; ----------------------------------------------------------------------------
69b2c07e
SM
759(defun desktop--v2s (value)
760 "Convert VALUE to a pair (QUOTE . SEXP); (eval SEXP) gives VALUE.
761SEXP is an sexp that when evaluated yields VALUE.
577ed2b2 762QUOTE may be `may' (value may be quoted),
60ff536c 763`must' (value must be quoted), or nil (value must not be quoted)."
de9e2828 764 (cond
1f7efe1b 765 ((or (numberp value) (null value) (eq t value) (keywordp value))
69b2c07e 766 (cons 'may value))
1f7efe1b
JB
767 ((stringp value)
768 (let ((copy (copy-sequence value)))
769 (set-text-properties 0 (length copy) nil copy)
69b2c07e
SM
770 ;; Get rid of text properties because we cannot read them.
771 (cons 'may copy)))
1f7efe1b 772 ((symbolp value)
69b2c07e 773 (cons 'must value))
1f7efe1b 774 ((vectorp value)
69b2c07e
SM
775 (let* ((pass1 (mapcar #'desktop--v2s value))
776 (special (assq nil pass1)))
1f7efe1b 777 (if special
69b2c07e
SM
778 (cons nil `(vector
779 ,@(mapcar (lambda (el)
780 (if (eq (car el) 'must)
781 `',(cdr el) (cdr el)))
782 pass1)))
783 (cons 'may `[,@(mapcar #'cdr pass1)]))))
1f7efe1b
JB
784 ((consp value)
785 (let ((p value)
786 newlist
acfcc8c5 787 use-list*)
1f7efe1b 788 (while (consp p)
69b2c07e
SM
789 (let ((q.sexp (desktop--v2s (car p))))
790 (push q.sexp newlist))
1f7efe1b 791 (setq p (cdr p)))
69b2c07e
SM
792 (when p
793 (let ((last (desktop--v2s p)))
794 (setq use-list* t)
795 (push last newlist)))
796 (if (assq nil newlist)
1f7efe1b 797 (cons nil
69b2c07e
SM
798 `(,(if use-list* 'desktop-list* 'list)
799 ,@(mapcar (lambda (el)
800 (if (eq (car el) 'must)
801 `',(cdr el) (cdr el)))
802 (nreverse newlist))))
1f7efe1b 803 (cons 'must
69b2c07e
SM
804 `(,@(mapcar #'cdr
805 (nreverse (if use-list* (cdr newlist) newlist)))
806 ,@(if use-list* (cdar newlist)))))))
1f7efe1b 807 ((subrp value)
69b2c07e
SM
808 (cons nil `(symbol-function
809 ',(intern-soft (substring (prin1-to-string value) 7 -1)))))
1f7efe1b 810 ((markerp value)
69b2c07e
SM
811 (let ((pos (marker-position value))
812 (buf (buffer-name (marker-buffer value))))
813 (cons nil
814 `(let ((mk (make-marker)))
815 (add-hook 'desktop-delay-hook
816 `(lambda ()
817 (set-marker ,mk ,,pos (get-buffer ,,buf))))
818 mk))))
819 (t ; Save as text.
820 (cons 'may "Unprintable entity"))))
de9e2828 821
4a2fce7a 822;; ----------------------------------------------------------------------------
6b61353c 823(defun desktop-value-to-string (value)
577ed2b2
RS
824 "Convert VALUE to a string that when read evaluates to the same value.
825Not all types of values are supported."
de9e2828
RS
826 (let* ((print-escape-newlines t)
827 (float-output-format nil)
69b2c07e
SM
828 (quote.sexp (desktop--v2s value))
829 (quote (car quote.sexp))
830 (txt
831 (let ((print-quoted t))
832 (prin1-to-string (cdr quote.sexp)))))
de9e2828
RS
833 (if (eq quote 'must)
834 (concat "'" txt)
835 txt)))
4a2fce7a 836
ec4c6f22 837;; ----------------------------------------------------------------------------
0e7c8611
RS
838(defun desktop-outvar (varspec)
839 "Output a setq statement for variable VAR to the desktop file.
840The argument VARSPEC may be the variable name VAR (a symbol),
be617bbf 841or a cons cell of the form (VAR . MAX-SIZE),
0e7c8611
RS
842which means to truncate VAR's value to at most MAX-SIZE elements
843\(if the value is a list) before saving the value."
844 (let (var size)
845 (if (consp varspec)
846 (setq var (car varspec) size (cdr varspec))
847 (setq var varspec))
1f7efe1b
JB
848 (when (boundp var)
849 (when (and (integerp size)
850 (> size 0)
851 (listp (eval var)))
852 (desktop-truncate (eval var) size))
853 (insert "(setq "
854 (symbol-name var)
855 " "
856 (desktop-value-to-string (symbol-value var))
857 ")\n"))))
4a2fce7a 858
0b9bd504 859;; ----------------------------------------------------------------------------
06b60517 860(defun desktop-save-buffer-p (filename bufname mode &rest _dummy)
ebb39555 861 "Return t if buffer should have its state saved in the desktop file.
0b9bd504 862FILENAME is the visited file name, BUFNAME is the buffer name, and
be617bbf
JB
863MODE is the major mode.
864\n\(fn FILENAME BUFNAME MODE)"
b23caf75
GM
865 (let ((case-fold-search nil)
866 dired-skip)
a6c2c80c
EZ
867 (and (not (and (stringp desktop-buffers-not-to-save)
868 (not filename)
acfcc8c5 869 (string-match-p desktop-buffers-not-to-save bufname)))
ebb39555 870 (not (memq mode desktop-modes-not-to-save))
b23caf75 871 ;; FIXME this is broken if desktop-files-not-to-save is nil.
ebb39555 872 (or (and filename
a6c2c80c 873 (stringp desktop-files-not-to-save)
acfcc8c5 874 (not (string-match-p desktop-files-not-to-save filename)))
4ad900d9 875 (and (memq mode '(dired-mode vc-dir-mode))
ebb39555 876 (with-current-buffer bufname
b23caf75 877 (not (setq dired-skip
acfcc8c5
JB
878 (string-match-p desktop-files-not-to-save
879 default-directory)))))
ebb39555 880 (and (null filename)
b23caf75 881 (null dired-skip) ; bug#5755
ebb39555 882 (with-current-buffer bufname desktop-save-buffer))))))
4a2fce7a 883
0b9bd504 884;; ----------------------------------------------------------------------------
4a2fce7a
RS
885(defun desktop-file-name (filename dirname)
886 "Convert FILENAME to format specified in `desktop-file-name-format'.
887DIRNAME must be the directory in which the desktop file will be saved."
888 (cond
889 ((not filename) nil)
890 ((eq desktop-file-name-format 'tilde)
891 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
892 (cond
893 ((file-name-absolute-p relative-name) relative-name)
894 ((string= "./" relative-name) "~/")
895 ((string= "." relative-name) "~")
896 (t (concat "~/" relative-name)))))
897 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
898 (t (expand-file-name filename))))
e5714620 899
b3615392 900
4a2fce7a 901;; ----------------------------------------------------------------------------
9421876d
JB
902(defun desktop--check-dont-save (frame)
903 (not (frame-parameter frame 'desktop-dont-save)))
904
905(defconst desktop--app-id `(desktop . ,desktop-file-version))
906
907(defun desktop-save-frameset ()
908 "Save the state of existing frames in `desktop-saved-frameset'.
a1c80d9d 909Frames with a non-nil `desktop-dont-save' parameter are not saved."
9421876d 910 (setq desktop-saved-frameset
2addf922 911 (and desktop-restore-frames
a912c016
JB
912 (frameset-save nil
913 :app desktop--app-id
914 :name (concat user-login-name "@" system-name)
915 :predicate #'desktop--check-dont-save))))
39c0e36f 916
11425834 917;;;###autoload
5db9dace 918(defun desktop-save (dirname &optional release auto-save)
6b61353c
KH
919 "Save the desktop in a desktop file.
920Parameter DIRNAME specifies where to save the desktop file.
e88110db 921Optional parameter RELEASE says whether we're done with this desktop.
5db9dace
JL
922If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
923and don't save the buffer if they are the same."
9271e90e
GM
924 (interactive (list
925 ;; Or should we just use (car desktop-path)?
926 (let ((default (if (member "." desktop-path)
927 default-directory
928 user-emacs-directory)))
929 (read-directory-name "Directory to save desktop file in: "
930 default default t))))
e88110db 931 (setq desktop-dirname (file-name-as-directory (expand-file-name dirname)))
6343ed61 932 (save-excursion
e88110db
JB
933 (let ((eager desktop-restore-eager)
934 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
935 (when
936 (or (not new-modtime) ; nothing to overwrite
937 (equal desktop-file-modtime new-modtime)
938 (yes-or-no-p (if desktop-file-modtime
939 (if (> (float-time new-modtime) (float-time desktop-file-modtime))
940 "Desktop file is more recent than the one loaded. Save anyway? "
941 "Desktop file isn't the one loaded. Overwrite it? ")
942 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
943 (unless release (error "Desktop file conflict")))
944
945 ;; If we're done with it, release the lock.
946 ;; Otherwise, claim it if it's unclaimed or if we created it.
947 (if release
948 (desktop-release-lock)
949 (unless (and new-modtime (desktop-owner)) (desktop-claim-lock)))
950
951 (with-temp-buffer
952 (insert
953 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
954 desktop-header
955 ";; Created " (current-time-string) "\n"
956 ";; Desktop file format version " desktop-file-version "\n"
957 ";; Emacs version " emacs-version "\n")
958 (save-excursion (run-hooks 'desktop-save-hook))
959 (goto-char (point-max))
960 (insert "\n;; Global section:\n")
39c0e36f
JB
961 ;; Called here because we save the window/frame state as a global
962 ;; variable for compatibility with previous Emacsen.
9421876d
JB
963 (desktop-save-frameset)
964 (unless (memq 'desktop-saved-frameset desktop-globals-to-save)
965 (desktop-outvar 'desktop-saved-frameset))
e88110db 966 (mapc (function desktop-outvar) desktop-globals-to-save)
9421876d 967 (setq desktop-saved-frameset nil) ; after saving desktop-globals-to-save
e88110db
JB
968 (when (memq 'kill-ring desktop-globals-to-save)
969 (insert
970 "(setq kill-ring-yank-pointer (nthcdr "
971 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
972 " kill-ring))\n"))
973
974 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
975 (dolist (l (mapcar 'desktop-buffer-info (buffer-list)))
a8049a30
JB
976 (let ((base (pop l)))
977 (when (apply 'desktop-save-buffer-p l)
978 (insert "("
979 (if (or (not (integerp eager))
980 (if (zerop eager)
981 nil
982 (setq eager (1- eager))))
983 "desktop-create-buffer"
984 "desktop-append-buffer-args")
985 " "
986 desktop-file-version)
ae4370a8
JB
987 ;; If there's a non-empty base name, we save it instead of the buffer name
988 (when (and base (not (string= base "")))
989 (setcar (nthcdr 1 l) base))
a8049a30
JB
990 (dolist (e l)
991 (insert "\n " (desktop-value-to-string e)))
992 (insert ")\n\n"))))
e88110db 993
9e29c91c 994 (setq default-directory desktop-dirname)
5db9dace
JL
995 ;; If auto-saving, avoid writing if nothing has changed since the last write.
996 ;; Don't check 300 characters of the header that contains the timestamp.
997 (let ((checksum (and auto-save (md5 (current-buffer)
998 (+ (point-min) 300) (point-max)
999 'emacs-mule))))
1000 (unless (and auto-save (equal checksum desktop-file-checksum))
1001 (let ((coding-system-for-write 'emacs-mule))
1002 (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage))
1003 (setq desktop-file-checksum checksum)
1004 ;; We remember when it was modified (which is presumably just now).
1005 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))))))))
4a2fce7a 1006
0b9bd504 1007;; ----------------------------------------------------------------------------
11425834 1008;;;###autoload
6343ed61 1009(defun desktop-remove ()
6b61353c
KH
1010 "Delete desktop file in `desktop-dirname'.
1011This function also sets `desktop-dirname' to nil."
6343ed61 1012 (interactive)
6b61353c 1013 (when desktop-dirname
11425834 1014 (let ((filename (desktop-full-file-name)))
6b61353c
KH
1015 (setq desktop-dirname nil)
1016 (when (file-exists-p filename)
73b0b745 1017 (delete-file filename)))))
6b61353c 1018
150f39ee
LH
1019(defvar desktop-buffer-args-list nil
1020 "List of args for `desktop-create-buffer'.")
1021
1022(defvar desktop-lazy-timer nil)
1023
0b9bd504 1024;; ----------------------------------------------------------------------------
9421876d
JB
1025(defun desktop-restoring-frameset-p ()
1026 "True if calling `desktop-restore-frameset' will actually restore it."
1027 (and desktop-restore-frames desktop-saved-frameset t))
1028
1029(defun desktop-restore-frameset ()
1030 "Restore the state of a set of frames.
1031This function depends on the value of `desktop-saved-frameset'
56bc453c 1032being set (usually, by reading it from the desktop)."
9421876d
JB
1033 (when (desktop-restoring-frameset-p)
1034 (frameset-restore desktop-saved-frameset
9421876d
JB
1035 :reuse-frames desktop-restore-reuses-frames
1036 :force-display desktop-restore-in-current-display
1037 :force-onscreen desktop-restore-forces-onscreen)))
1038
1039;; Just to silence the byte compiler.
526e5233 1040;; Dynamically bound in `desktop-read'.
9421876d
JB
1041(defvar desktop-first-buffer)
1042(defvar desktop-buffer-ok-count)
1043(defvar desktop-buffer-fail-count)
39c0e36f 1044
478653c9 1045;;;###autoload
6b61353c
KH
1046(defun desktop-read (&optional dirname)
1047 "Read and process the desktop file in directory DIRNAME.
1048Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
1049directories listed in `desktop-path'. If a desktop file is found, it
cc8b76bf 1050is processed and `desktop-after-read-hook' is run. If no desktop file
6b61353c
KH
1051is found, clear the desktop and run `desktop-no-desktop-file-hook'.
1052This function is a no-op when Emacs is running in batch mode.
1053It returns t if a desktop file was loaded, nil otherwise."
6343ed61 1054 (interactive)
4a2fce7a 1055 (unless noninteractive
6b61353c 1056 (setq desktop-dirname
11425834
LH
1057 (file-name-as-directory
1058 (expand-file-name
1059 (or
1060 ;; If DIRNAME is specified, use it.
1061 (and (< 0 (length dirname)) dirname)
1062 ;; Otherwise search desktop file in desktop-path.
1063 (let ((dirs desktop-path))
1064 (while (and dirs
1065 (not (file-exists-p
1066 (desktop-full-file-name (car dirs)))))
1067 (setq dirs (cdr dirs)))
1068 (and dirs (car dirs)))
1069 ;; If not found and `desktop-path' is non-nil, use its first element.
1070 (and desktop-path (car desktop-path))
6b67c0d4
CY
1071 ;; Default: .emacs.d.
1072 user-emacs-directory))))
11425834 1073 (if (file-exists-p (desktop-full-file-name))
e88110db
JB
1074 ;; Desktop file found, but is it already in use?
1075 (let ((desktop-first-buffer nil)
1076 (desktop-buffer-ok-count 0)
1077 (desktop-buffer-fail-count 0)
1078 (owner (desktop-owner))
1079 ;; Avoid desktop saving during evaluation of desktop buffer.
1080 (desktop-save nil))
1081 (if (and owner
1f7efe1b
JB
1082 (memq desktop-load-locked-desktop '(nil ask))
1083 (or (null desktop-load-locked-desktop)
e76f0800 1084 (daemonp)
1f7efe1b
JB
1085 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
1086Using it may cause conflicts. Use it anyway? " owner)))))
c41cf130 1087 (let ((default-directory desktop-dirname))
794855ca 1088 (setq desktop-dirname nil)
c41cf130
JB
1089 (run-hooks 'desktop-not-loaded-hook)
1090 (unless desktop-dirname
1091 (message "Desktop file in use; not loaded.")))
e88110db
JB
1092 (desktop-lazy-abort)
1093 ;; Evaluate desktop buffer and remember when it was modified.
1094 (load (desktop-full-file-name) t t t)
1095 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))
1096 ;; If it wasn't already, mark it as in-use, to bother other
1097 ;; desktop instances.
1098 (unless owner
1099 (condition-case nil
1100 (desktop-claim-lock)
1101 (file-error (message "Couldn't record use of desktop file")
1102 (sit-for 1))))
1103
9421876d 1104 (unless (desktop-restoring-frameset-p)
56bc453c
JB
1105 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1106 ;; We want buffers existing prior to evaluating the desktop (and
1107 ;; not reused) to be placed at the end of the buffer list, so we
1108 ;; move them here.
1109 (mapc 'bury-buffer
1110 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
1111 (switch-to-buffer (car (buffer-list))))
e88110db
JB
1112 (run-hooks 'desktop-delay-hook)
1113 (setq desktop-delay-hook nil)
9421876d 1114 (desktop-restore-frameset)
e88110db 1115 (run-hooks 'desktop-after-read-hook)
9421876d
JB
1116 (message "Desktop: %s%d buffer%s restored%s%s."
1117 (if desktop-saved-frameset
1118 (let ((fn (length (frameset-states desktop-saved-frameset))))
1119 (format "%d frame%s, "
1120 fn (if (= fn 1) "" "s")))
1121 "")
e88110db
JB
1122 desktop-buffer-ok-count
1123 (if (= 1 desktop-buffer-ok-count) "" "s")
1124 (if (< 0 desktop-buffer-fail-count)
1125 (format ", %d failed to restore" desktop-buffer-fail-count)
1126 "")
1127 (if desktop-buffer-args-list
1128 (format ", %d to restore lazily"
1129 (length desktop-buffer-args-list))
1130 ""))
9421876d 1131 (unless (desktop-restoring-frameset-p)
56bc453c
JB
1132 ;; Bury the *Messages* buffer to not reshow it when burying
1133 ;; the buffer we switched to above.
1134 (when (buffer-live-p (get-buffer "*Messages*"))
1135 (bury-buffer "*Messages*"))
1136 ;; Clear all windows' previous and next buffers, these have
1137 ;; been corrupted by the `switch-to-buffer' calls in
1138 ;; `desktop-restore-file-buffer' (bug#11556). This is a
1139 ;; brute force fix and should be replaced by a more subtle
1140 ;; strategy eventually.
1141 (walk-window-tree (lambda (window)
1142 (set-window-prev-buffers window nil)
1143 (set-window-next-buffers window nil))))
5414a283 1144 (setq desktop-saved-frameset nil)
e88110db 1145 t))
6b61353c
KH
1146 ;; No desktop file found.
1147 (desktop-clear)
1148 (let ((default-directory desktop-dirname))
1149 (run-hooks 'desktop-no-desktop-file-hook))
1150 (message "No desktop file.")
1151 nil)))
4a2fce7a 1152
0b9bd504 1153;; ----------------------------------------------------------------------------
6b61353c 1154;; Maintained for backward compatibility
478653c9 1155;;;###autoload
6343ed61 1156(defun desktop-load-default ()
577ed2b2 1157 "Load the `default' start-up library manually.
6b61353c 1158Also inhibit further loading of it."
59f7af81 1159 (declare (obsolete desktop-save-mode "22.1"))
b89c5a72
JB
1160 (unless inhibit-default-init ; safety check
1161 (load "default" t t)
1162 (setq inhibit-default-init t)))
4a2fce7a
RS
1163
1164;; ----------------------------------------------------------------------------
1165;;;###autoload
6b61353c
KH
1166(defun desktop-change-dir (dirname)
1167 "Change to desktop saved in DIRNAME.
1168Kill the desktop as specified by variables `desktop-save-mode' and
1169`desktop-save', then clear the desktop and load the desktop file in
1170directory DIRNAME."
1171 (interactive "DChange to directory: ")
1172 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
4a2fce7a
RS
1173 (desktop-kill)
1174 (desktop-clear)
6b61353c 1175 (desktop-read dirname))
bf247b6e 1176
6b61353c 1177;; ----------------------------------------------------------------------------
4a2fce7a 1178;;;###autoload
6b61353c
KH
1179(defun desktop-save-in-desktop-dir ()
1180 "Save the desktop in directory `desktop-dirname'."
4a2fce7a
RS
1181 (interactive)
1182 (if desktop-dirname
b89c5a72 1183 (desktop-save desktop-dirname)
4a2fce7a 1184 (call-interactively 'desktop-save))
a609d13b 1185 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname)))
4a2fce7a 1186
5db9dace
JL
1187;; ----------------------------------------------------------------------------
1188;; Auto-Saving.
1189(defvar desktop-auto-save-timer nil)
1190
1191(defun desktop-auto-save ()
1192 "Save the desktop periodically.
1193Called by the timer created in `desktop-auto-save-set-timer'."
1194 (when (and desktop-save-mode
1195 (integerp desktop-auto-save-timeout)
1196 (> desktop-auto-save-timeout 0)
1197 ;; Avoid desktop saving during lazy loading.
1198 (not desktop-lazy-timer)
1199 ;; Save only to own desktop file.
1200 (eq (emacs-pid) (desktop-owner))
1201 desktop-dirname)
1202 (desktop-save desktop-dirname nil t))
1203 (desktop-auto-save-set-timer))
1204
1205(defun desktop-auto-save-set-timer ()
1206 "Reset the auto-save timer.
1207Cancel any previous timer. When `desktop-auto-save-timeout' is a positive
1208integer, start a new timer to call `desktop-auto-save' in that many seconds."
1209 (when desktop-auto-save-timer
1210 (cancel-timer desktop-auto-save-timer)
1211 (setq desktop-auto-save-timer nil))
1212 (when (and (integerp desktop-auto-save-timeout)
1213 (> desktop-auto-save-timeout 0))
1214 (setq desktop-auto-save-timer
1215 (run-with-timer desktop-auto-save-timeout nil
1216 'desktop-auto-save))))
1217
4a2fce7a
RS
1218;; ----------------------------------------------------------------------------
1219;;;###autoload
1220(defun desktop-revert ()
1221 "Revert to the last loaded desktop."
1222 (interactive)
6b61353c
KH
1223 (unless desktop-dirname
1224 (error "Unknown desktop directory"))
11425834 1225 (unless (file-exists-p (desktop-full-file-name))
6b61353c
KH
1226 (error "No desktop file found"))
1227 (desktop-clear)
1228 (desktop-read desktop-dirname))
4a2fce7a 1229
341c2f07
SM
1230(defvar desktop-buffer-major-mode)
1231(defvar desktop-buffer-locals)
06b60517 1232(defvar auto-insert) ; from autoinsert.el
0b9bd504 1233;; ----------------------------------------------------------------------------
06b60517
JB
1234(defun desktop-restore-file-buffer (buffer-filename
1235 _buffer-name
1236 _buffer-misc)
e5780ae1 1237 "Restore a file buffer."
06b60517
JB
1238 (when buffer-filename
1239 (if (or (file-exists-p buffer-filename)
1f7efe1b 1240 (let ((msg (format "Desktop: File \"%s\" no longer exists."
06b60517 1241 buffer-filename)))
1f7efe1b
JB
1242 (if desktop-missing-file-warning
1243 (y-or-n-p (concat msg " Re-create buffer? "))
1244 (message "%s" msg)
1245 nil)))
1246 (let* ((auto-insert nil) ; Disable auto insertion
1247 (coding-system-for-read
1248 (or coding-system-for-read
1249 (cdr (assq 'buffer-file-coding-system
1250 desktop-buffer-locals))))
06b60517 1251 (buf (find-file-noselect buffer-filename)))
1f7efe1b
JB
1252 (condition-case nil
1253 (switch-to-buffer buf)
1254 (error (pop-to-buffer buf)))
1255 (and (not (eq major-mode desktop-buffer-major-mode))
1256 (functionp desktop-buffer-major-mode)
1257 (funcall desktop-buffer-major-mode))
1258 buf)
1259 nil)))
4a2fce7a 1260
0f4804e7
LH
1261(defun desktop-load-file (function)
1262 "Load the file where auto loaded FUNCTION is defined."
7abaf5cc
SM
1263 (when (fboundp function)
1264 (autoload-do-load (symbol-function function) function)))
0f4804e7 1265
0b9bd504 1266;; ----------------------------------------------------------------------------
eb982898
JL
1267;; Create a buffer, load its file, set its mode, ...;
1268;; called from Desktop file only.
1d500ca6 1269
340db502 1270(defun desktop-create-buffer
06b60517
JB
1271 (file-version
1272 buffer-filename
1273 buffer-name
1274 buffer-majormode
1275 buffer-minormodes
1276 buffer-point
1277 buffer-mark
1278 buffer-readonly
1279 buffer-misc
1280 &optional
1281 buffer-locals)
1282
1283 (let ((desktop-file-version file-version)
1284 (desktop-buffer-file-name buffer-filename)
1285 (desktop-buffer-name buffer-name)
1286 (desktop-buffer-major-mode buffer-majormode)
1287 (desktop-buffer-minor-modes buffer-minormodes)
1288 (desktop-buffer-point buffer-point)
1289 (desktop-buffer-mark buffer-mark)
1290 (desktop-buffer-read-only buffer-readonly)
1291 (desktop-buffer-misc buffer-misc)
1292 (desktop-buffer-locals buffer-locals))
1293 ;; To make desktop files with relative file names possible, we cannot
1294 ;; allow `default-directory' to change. Therefore we save current buffer.
1295 (save-current-buffer
1296 ;; Give major mode module a chance to add a handler.
1297 (desktop-load-file desktop-buffer-major-mode)
1298 (let ((buffer-list (buffer-list))
1299 (result
1be3ca5a 1300 (condition-case-unless-debug err
06b60517
JB
1301 (funcall (or (cdr (assq desktop-buffer-major-mode
1302 desktop-buffer-mode-handlers))
1303 'desktop-restore-file-buffer)
1304 desktop-buffer-file-name
1305 desktop-buffer-name
1306 desktop-buffer-misc)
1307 (error
1308 (message "Desktop: Can't load buffer %s: %s"
1309 desktop-buffer-name
1310 (error-message-string err))
1311 (when desktop-missing-file-warning (sit-for 1))
1312 nil))))
1313 (if (bufferp result)
1314 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1315 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1316 (setq result nil))
1317 ;; Restore buffer list order with new buffer at end. Don't change
91af3942 1318 ;; the order for old desktop files (old desktop module behavior).
06b60517
JB
1319 (unless (< desktop-file-version 206)
1320 (mapc 'bury-buffer buffer-list)
1321 (when result (bury-buffer result)))
1322 (when result
1323 (unless (or desktop-first-buffer (< desktop-file-version 206))
1324 (setq desktop-first-buffer result))
1325 (set-buffer result)
1326 (unless (equal (buffer-name) desktop-buffer-name)
1327 (rename-buffer desktop-buffer-name t))
1328 ;; minor modes
1329 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1330 (auto-fill-mode 1))
1331 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1332 (auto-fill-mode 0))
1333 (t
1334 (dolist (minor-mode desktop-buffer-minor-modes)
1335 ;; Give minor mode module a chance to add a handler.
1336 (desktop-load-file minor-mode)
1337 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1338 (if handler
1339 (funcall handler desktop-buffer-locals)
1340 (when (functionp minor-mode)
1341 (funcall minor-mode 1)))))))
1342 ;; Even though point and mark are non-nil when written by
1343 ;; `desktop-save', they may be modified by handlers wanting to set
1344 ;; point or mark themselves.
1345 (when desktop-buffer-point
1346 (goto-char
1347 (condition-case err
1348 ;; Evaluate point. Thus point can be something like
1349 ;; '(search-forward ...
1350 (eval desktop-buffer-point)
1351 (error (message "%s" (error-message-string err)) 1))))
1352 (when desktop-buffer-mark
1353 (if (consp desktop-buffer-mark)
1f7efe1b 1354 (progn
06b60517
JB
1355 (set-mark (car desktop-buffer-mark))
1356 (setq mark-active (car (cdr desktop-buffer-mark))))
1357 (set-mark desktop-buffer-mark)))
1358 ;; Never override file system if the file really is read-only marked.
1359 (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
b61d71e4
JB
1360 (dolist (this desktop-buffer-locals)
1361 (if (consp this)
1362 ;; an entry of this form `(symbol . value)'
1363 (progn
1364 (make-local-variable (car this))
1365 (set (car this) (cdr this)))
1366 ;; an entry of the form `symbol'
1367 (make-local-variable this)
1368 (makunbound this))))))))
ec4c6f22 1369
4a2fce7a 1370;; ----------------------------------------------------------------------------
ec4c6f22 1371;; Backward compatibility -- update parameters to 205 standards.
06b60517
JB
1372(defun desktop-buffer (buffer-filename buffer-name buffer-majormode
1373 mim pt mk ro tl fc cfs cr buffer-misc)
1374 (desktop-create-buffer 205 buffer-filename buffer-name
1375 buffer-majormode (cdr mim) pt mk ro
1376 buffer-misc
ec4c6f22
RS
1377 (list (cons 'truncate-lines tl)
1378 (cons 'fill-column fc)
1379 (cons 'case-fold-search cfs)
1380 (cons 'case-replace cr)
1381 (cons 'overwrite-mode (car mim)))))
84b538ec 1382
150f39ee 1383(defun desktop-append-buffer-args (&rest args)
cc8b76bf 1384 "Append ARGS at end of `desktop-buffer-args-list'.
150f39ee
LH
1385ARGS must be an argument list for `desktop-create-buffer'."
1386 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1387 (unless desktop-lazy-timer
1388 (setq desktop-lazy-timer
1389 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1390
1391(defun desktop-lazy-create-buffer ()
1392 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1393 (when desktop-buffer-args-list
1394 (let* ((remaining (length desktop-buffer-args-list))
1395 (args (pop desktop-buffer-args-list))
1396 (buffer-name (nth 2 args))
1397 (msg (format "Desktop lazily opening %s (%s remaining)..."
1398 buffer-name remaining)))
1399 (when desktop-lazy-verbose
8a26c165 1400 (message "%s" msg))
150f39ee
LH
1401 (let ((desktop-first-buffer nil)
1402 (desktop-buffer-ok-count 0)
1403 (desktop-buffer-fail-count 0))
1404 (apply 'desktop-create-buffer args)
1405 (run-hooks 'desktop-delay-hook)
1406 (setq desktop-delay-hook nil)
1407 (bury-buffer (get-buffer buffer-name))
1408 (when desktop-lazy-verbose
1409 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1410
1411(defun desktop-idle-create-buffers ()
1412 "Create buffers until the user does something, then stop.
1413If there are no buffers left to create, kill the timer."
1414 (let ((repeat 1))
1415 (while (and repeat desktop-buffer-args-list)
1416 (save-window-excursion
1417 (desktop-lazy-create-buffer))
1418 (setq repeat (sit-for 0.2))
1419 (unless desktop-buffer-args-list
1420 (cancel-timer desktop-lazy-timer)
1421 (setq desktop-lazy-timer nil)
1422 (message "Lazy desktop load complete")
1423 (sit-for 3)
1424 (message "")))))
1425
1426(defun desktop-lazy-complete ()
1427 "Run the desktop load to completion."
1428 (interactive)
1429 (let ((desktop-lazy-verbose t))
1430 (while desktop-buffer-args-list
1431 (save-window-excursion
1432 (desktop-lazy-create-buffer)))
1433 (message "Lazy desktop load complete")))
1434
1435(defun desktop-lazy-abort ()
1436 "Abort lazy loading of the desktop."
1437 (interactive)
1438 (when desktop-lazy-timer
1439 (cancel-timer desktop-lazy-timer)
1440 (setq desktop-lazy-timer nil))
1441 (when desktop-buffer-args-list
1442 (setq desktop-buffer-args-list nil)
32226619 1443 (when (called-interactively-p 'interactive)
150f39ee
LH
1444 (message "Lazy desktop load aborted"))))
1445
0b9bd504 1446;; ----------------------------------------------------------------------------
6b61353c 1447;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
4a2fce7a
RS
1448;; command line, we do the rest of what it takes to use desktop, but do it
1449;; after finishing loading the init file.
1450;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1451;; functions are processed after `after-init-hook'.
1452(add-hook
1453 'after-init-hook
341c2f07 1454 (lambda ()
4a2fce7a 1455 (let ((key "--no-desktop"))
4b217d46
LH
1456 (when (member key command-line-args)
1457 (setq command-line-args (delete key command-line-args))
1458 (setq desktop-save-mode nil)))
54d22a6f
JL
1459 (when desktop-save-mode
1460 (desktop-read)
5db9dace 1461 (desktop-auto-save-set-timer)
54d22a6f 1462 (setq inhibit-startup-screen t))))
813dbb2d 1463
f12ad6ec
GM
1464;; So we can restore vc-dir buffers.
1465(autoload 'vc-dir-mode "vc-dir" nil t)
1466
ab1d55ea 1467(provide 'desktop)
6343ed61 1468
80f9f3db 1469;;; desktop.el ends here
8c106d17
GM
1470
1471;; Local Variables:
1472;; coding: utf-8
1473;; End: