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