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