Merge from emacs-23
[emacs.git] / lisp / desktop.el
blob5c2e6d9b2f5c0e4234272a7dc1df2f12f1def1f4
1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: Morten Welinder <terra@diku.dk>
8 ;; Keywords: convenience
9 ;; Favourite-brand-of-beer: None, I hate beer.
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26 ;;; Commentary:
28 ;; Save the Desktop, i.e.,
29 ;; - some global variables
30 ;; - the list of buffers with associated files. For each buffer also
31 ;; - the major mode
32 ;; - the default directory
33 ;; - the point
34 ;; - the mark & mark-active
35 ;; - buffer-read-only
36 ;; - some local variables
38 ;; To use this, use customize to turn on desktop-save-mode or add the
39 ;; following line somewhere in your .emacs file:
41 ;; (desktop-save-mode 1)
43 ;; For further usage information, look at the section
44 ;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual.
46 ;; When the desktop module is loaded, the function `desktop-kill' is
47 ;; added to the `kill-emacs-hook'. This function is responsible for
48 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
49 ;; function is added to the `after-init-hook'. This function is
50 ;; responsible for loading the desktop when Emacs is started.
52 ;; Special handling.
53 ;; -----------------
54 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
55 ;; are supplied to handle special major and minor modes respectively.
56 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
57 ;; to restore a desktop buffer. Elements must have the form
59 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
61 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
62 ;; evaluates the desktop file. Buffers with a major mode not specified here,
63 ;; are restored by the default handler `desktop-restore-file-buffer'.
64 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
65 ;; non-standard minor modes. Elements must have the form
67 ;; (MINOR-MODE . RESTORE-FUNCTION).
69 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
70 ;; Minor modes not specified here, are restored by the standard minor mode
71 ;; function. If you write a module that defines a major or minor mode that
72 ;; needs a special handler, then place code like
74 ;; (defun foo-restore-desktop-buffer
75 ;; ...
76 ;; (add-to-list 'desktop-buffer-mode-handlers
77 ;; '(foo-mode . foo-restore-desktop-buffer))
79 ;; or
81 ;; (defun bar-desktop-restore
82 ;; ...
83 ;; (add-to-list 'desktop-minor-mode-handlers
84 ;; '(bar-mode . bar-desktop-restore))
86 ;; in the module itself, and make sure that the mode function is
87 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
88 ;; `desktop-minor-mode-handlers' for more info.
90 ;; Minor modes.
91 ;; ------------
92 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
93 ;; manual) are handled in the following way:
94 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
95 ;; saves as `desktop-minor-modes' the list of names of those variables in
96 ;; `minor-mode-alist' that have a non-nil value.
97 ;; When `desktop-create' restores the buffer, each of the symbols in
98 ;; `desktop-minor-modes' is called as function with parameter 1.
99 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
100 ;; are used to handle non-conventional minor modes. `desktop-save' uses
101 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
102 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
103 ;; variable name that is different form its function name, an entry
105 ;; (NAME RESTORE-FUNCTION)
107 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
108 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
109 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
110 ;; function different from the usual minor mode function.
111 ;; ---------------------------------------------------------------------------
113 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
114 ;; in your home directory is used for that. Saving global default values
115 ;; for buffers is an example of misuse.
117 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
118 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
119 ;; things you did not mean to keep. Use M-x desktop-clear RET.
121 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
122 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
123 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
124 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
125 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
126 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
127 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
128 ;; ---------------------------------------------------------------------------
129 ;; TODO:
131 ;; Save window configuration.
132 ;; Recognize more minor modes.
133 ;; Save mark rings.
135 ;;; Code:
137 (defvar desktop-file-version "206"
138 "Version number of desktop file format.
139 Written into the desktop file and used at desktop read to provide
140 backward compatibility.")
142 ;; ----------------------------------------------------------------------------
143 ;; USER OPTIONS -- settings you might want to play with.
144 ;; ----------------------------------------------------------------------------
146 (defgroup desktop nil
147 "Save status of Emacs when you exit."
148 :group 'frames)
150 ;;;###autoload
151 (define-minor-mode desktop-save-mode
152 "Toggle desktop saving mode.
153 With numeric ARG, turn desktop saving on if ARG is positive, off
154 otherwise. If desktop saving is turned on, the state of Emacs is
155 saved from one session to another. See variable `desktop-save'
156 and function `desktop-read' for details."
157 :global t
158 :group 'desktop)
160 ;; Maintained for backward compatibility
161 (define-obsolete-variable-alias 'desktop-enable
162 'desktop-save-mode "22.1")
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 "22.1")
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 "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 :type '(repeat string)
311 :group 'desktop)
313 ;;;###autoload
314 (defcustom desktop-locals-to-save
315 '(desktop-locals-to-save ; Itself! Think it over.
316 truncate-lines
317 case-fold-search
318 case-replace
319 fill-column
320 overwrite-mode
321 change-log-default-name
322 line-number-mode
323 column-number-mode
324 size-indication-mode
325 buffer-file-coding-system
326 indent-tabs-mode
327 tab-width
328 indicate-buffer-boundaries
329 indicate-empty-lines
330 show-trailing-whitespace)
331 "List of local variables to save for each buffer.
332 The variables are saved only when they really are local. Conventional minor
333 modes are restored automatically; they should not be listed here."
334 :type '(repeat symbol)
335 :group 'desktop)
337 (defcustom desktop-buffers-not-to-save nil
338 "Regexp identifying buffers that are to be excluded from saving."
339 :type '(choice (const :tag "None" nil)
340 regexp)
341 :version "23.2" ; set to nil
342 :group 'desktop)
344 ;; Skip tramp and ange-ftp files
345 (defcustom desktop-files-not-to-save
346 "\\(^/[^/:]*:\\|(ftp)$\\)"
347 "Regexp identifying files whose buffers are to be excluded from saving."
348 :type '(choice (const :tag "None" nil)
349 regexp)
350 :group 'desktop)
352 ;; We skip TAGS files to save time (tags-file-name is saved instead).
353 (defcustom desktop-modes-not-to-save
354 '(tags-table-mode)
355 "List of major modes whose buffers should not be saved."
356 :type '(repeat symbol)
357 :group 'desktop)
359 (defcustom desktop-file-name-format 'absolute
360 "Format in which desktop file names should be saved.
361 Possible values are:
362 absolute -- Absolute file name.
363 tilde -- Relative to ~.
364 local -- Relative to directory of desktop file."
365 :type '(choice (const absolute) (const tilde) (const local))
366 :group 'desktop
367 :version "22.1")
369 (defcustom desktop-restore-eager t
370 "Number of buffers to restore immediately.
371 Remaining buffers are restored lazily (when Emacs is idle).
372 If value is t, all buffers are restored immediately."
373 :type '(choice (const t) integer)
374 :group 'desktop
375 :version "22.1")
377 (defcustom desktop-lazy-verbose t
378 "Verbose reporting of lazily created buffers."
379 :type 'boolean
380 :group 'desktop
381 :version "22.1")
383 (defcustom desktop-lazy-idle-delay 5
384 "Idle delay before starting to create buffers.
385 See `desktop-restore-eager'."
386 :type 'integer
387 :group 'desktop
388 :version "22.1")
390 ;;;###autoload
391 (defvar desktop-save-buffer nil
392 "When non-nil, save buffer status in desktop file.
393 This variable becomes buffer local when set.
395 If the value is a function, it is called by `desktop-save' with argument
396 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
397 file along with the state of the buffer for which it was called.
399 When file names are returned, they should be formatted using the call
400 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
402 Later, when `desktop-read' evaluates the desktop file, auxiliary information
403 is passed as the argument DESKTOP-BUFFER-MISC to functions in
404 `desktop-buffer-mode-handlers'.")
405 (make-variable-buffer-local 'desktop-save-buffer)
406 (make-obsolete-variable 'desktop-buffer-modes-to-save
407 'desktop-save-buffer "22.1")
408 (make-obsolete-variable 'desktop-buffer-misc-functions
409 'desktop-save-buffer "22.1")
411 ;;;###autoload
412 (defvar desktop-buffer-mode-handlers
414 "Alist of major mode specific functions to restore a desktop buffer.
415 Functions listed are called by `desktop-create-buffer' when `desktop-read'
416 evaluates the desktop file. List elements must have the form
418 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
420 Buffers with a major mode not specified here, are restored by the default
421 handler `desktop-restore-file-buffer'.
423 Handlers are called with argument list
425 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
427 Furthermore, they may use the following variables:
429 desktop-file-version
430 desktop-buffer-major-mode
431 desktop-buffer-minor-modes
432 desktop-buffer-point
433 desktop-buffer-mark
434 desktop-buffer-read-only
435 desktop-buffer-locals
437 If a handler returns a buffer, then the saved mode settings
438 and variable values for that buffer are copied into it.
440 Modules that define a major mode that needs a special handler should contain
441 code like
443 (defun foo-restore-desktop-buffer
445 (add-to-list 'desktop-buffer-mode-handlers
446 '(foo-mode . foo-restore-desktop-buffer))
448 Furthermore the major mode function must be autoloaded.")
450 ;;;###autoload
451 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
452 (make-obsolete-variable 'desktop-buffer-handlers
453 'desktop-buffer-mode-handlers "22.1")
455 (defcustom desktop-minor-mode-table
456 '((auto-fill-function auto-fill-mode)
457 (vc-mode nil)
458 (vc-dired-mode nil)
459 (erc-track-minor-mode nil)
460 (savehist-mode nil))
461 "Table mapping minor mode variables to minor mode functions.
462 Each entry has the form (NAME RESTORE-FUNCTION).
463 NAME is the name of the buffer-local variable indicating that the minor
464 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
465 RESTORE-FUNCTION nil means don't try to restore the minor mode.
466 Only minor modes for which the name of the buffer-local variable
467 and the name of the minor mode function are different have to be added to
468 this table. See also `desktop-minor-mode-handlers'."
469 :type 'sexp
470 :group 'desktop)
472 ;;;###autoload
473 (defvar desktop-minor-mode-handlers
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 (add-hook 'kill-emacs-hook 'desktop-kill)
616 (defun desktop-kill ()
617 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
618 If the desktop should be saved and `desktop-dirname'
619 is nil, ask the user where to save the desktop."
620 (when (and desktop-save-mode
621 (let ((exists (file-exists-p (desktop-full-file-name))))
622 (or (eq desktop-save t)
623 (and exists (eq desktop-save 'if-exists))
624 ;; If it exists, but we aren't using it, we are going
625 ;; to ask for a new directory below.
626 (and exists desktop-dirname (eq desktop-save 'ask-if-new))
627 (and
628 (or (memq desktop-save '(ask ask-if-new))
629 (and exists (eq desktop-save 'ask-if-exists)))
630 (y-or-n-p "Save desktop? ")))))
631 (unless desktop-dirname
632 (setq desktop-dirname
633 (file-name-as-directory
634 (expand-file-name
635 (read-directory-name "Directory for desktop file: " nil nil t)))))
636 (condition-case err
637 (desktop-save desktop-dirname t)
638 (file-error
639 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
640 (signal (car err) (cdr err))))))
641 ;; If we own it, we don't anymore.
642 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
644 ;; ----------------------------------------------------------------------------
645 (defun desktop-list* (&rest args)
646 (if (null (cdr args))
647 (car args)
648 (setq args (nreverse args))
649 (let ((value (cons (nth 1 args) (car args))))
650 (setq args (cdr (cdr args)))
651 (while args
652 (setq value (cons (car args) value))
653 (setq args (cdr args)))
654 value)))
656 ;; ----------------------------------------------------------------------------
657 (defun desktop-buffer-info (buffer)
658 (set-buffer buffer)
659 (list
660 ;; base name of the buffer; replaces the buffer name if managed by uniquify
661 (and (fboundp 'uniquify-buffer-base-name) (uniquify-buffer-base-name))
662 ;; basic information
663 (desktop-file-name (buffer-file-name) desktop-dirname)
664 (buffer-name)
665 major-mode
666 ;; minor modes
667 (let (ret)
668 (mapc
669 #'(lambda (minor-mode)
670 (and (boundp minor-mode)
671 (symbol-value minor-mode)
672 (let* ((special (assq minor-mode desktop-minor-mode-table))
673 (value (cond (special (cadr special))
674 ((functionp minor-mode) minor-mode))))
675 (when value (add-to-list 'ret value)))))
676 (mapcar #'car minor-mode-alist))
677 ret)
678 ;; point and mark, and read-only status
679 (point)
680 (list (mark t) mark-active)
681 buffer-read-only
682 ;; auxiliary information
683 (when (functionp desktop-save-buffer)
684 (funcall desktop-save-buffer desktop-dirname))
685 ;; local variables
686 (let ((locals desktop-locals-to-save)
687 (loclist (buffer-local-variables))
688 (ll))
689 (while locals
690 (let ((here (assq (car locals) loclist)))
691 (if here
692 (setq ll (cons here ll))
693 (when (member (car locals) loclist)
694 (setq ll (cons (car locals) ll)))))
695 (setq locals (cdr locals)))
696 ll)))
698 ;; ----------------------------------------------------------------------------
699 (defun desktop-internal-v2s (value)
700 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
701 TXT is a string that when read and evaluated yields value.
702 QUOTE may be `may' (value may be quoted),
703 `must' (values must be quoted), or nil (value may not be quoted)."
704 (cond
705 ((or (numberp value) (null value) (eq t value) (keywordp value))
706 (cons 'may (prin1-to-string value)))
707 ((stringp value)
708 (let ((copy (copy-sequence value)))
709 (set-text-properties 0 (length copy) nil copy)
710 ;; Get rid of text properties because we cannot read them
711 (cons 'may (prin1-to-string copy))))
712 ((symbolp value)
713 (cons 'must (prin1-to-string value)))
714 ((vectorp value)
715 (let* ((special nil)
716 (pass1 (mapcar
717 (lambda (el)
718 (let ((res (desktop-internal-v2s el)))
719 (if (null (car res))
720 (setq special t))
721 res))
722 value)))
723 (if special
724 (cons nil (concat "(vector "
725 (mapconcat (lambda (el)
726 (if (eq (car el) 'must)
727 (concat "'" (cdr el))
728 (cdr el)))
729 pass1
730 " ")
731 ")"))
732 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
733 ((consp value)
734 (let ((p value)
735 newlist
736 use-list*
737 anynil)
738 (while (consp p)
739 (let ((q.txt (desktop-internal-v2s (car p))))
740 (or anynil (setq anynil (null (car q.txt))))
741 (setq newlist (cons q.txt newlist)))
742 (setq p (cdr p)))
743 (if p
744 (let ((last (desktop-internal-v2s p)))
745 (or anynil (setq anynil (null (car last))))
746 (or anynil
747 (setq newlist (cons '(must . ".") newlist)))
748 (setq use-list* t)
749 (setq newlist (cons last newlist))))
750 (setq newlist (nreverse newlist))
751 (if anynil
752 (cons nil
753 (concat (if use-list* "(desktop-list* " "(list ")
754 (mapconcat (lambda (el)
755 (if (eq (car el) 'must)
756 (concat "'" (cdr el))
757 (cdr el)))
758 newlist
759 " ")
760 ")"))
761 (cons 'must
762 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
763 ((subrp value)
764 (cons nil (concat "(symbol-function '"
765 (substring (prin1-to-string value) 7 -1)
766 ")")))
767 ((markerp value)
768 (let ((pos (prin1-to-string (marker-position value)))
769 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
770 (cons nil (concat "(let ((mk (make-marker)))"
771 " (add-hook 'desktop-delay-hook"
772 " (list 'lambda '() (list 'set-marker mk "
773 pos " (get-buffer " buf ")))) mk)"))))
774 (t ; save as text
775 (cons 'may "\"Unprintable entity\""))))
777 ;; ----------------------------------------------------------------------------
778 (defun desktop-value-to-string (value)
779 "Convert VALUE to a string that when read evaluates to the same value.
780 Not all types of values are supported."
781 (let* ((print-escape-newlines t)
782 (float-output-format nil)
783 (quote.txt (desktop-internal-v2s value))
784 (quote (car quote.txt))
785 (txt (cdr quote.txt)))
786 (if (eq quote 'must)
787 (concat "'" txt)
788 txt)))
790 ;; ----------------------------------------------------------------------------
791 (defun desktop-outvar (varspec)
792 "Output a setq statement for variable VAR to the desktop file.
793 The argument VARSPEC may be the variable name VAR (a symbol),
794 or a cons cell of the form (VAR . MAX-SIZE),
795 which means to truncate VAR's value to at most MAX-SIZE elements
796 \(if the value is a list) before saving the value."
797 (let (var size)
798 (if (consp varspec)
799 (setq var (car varspec) size (cdr varspec))
800 (setq var varspec))
801 (when (boundp var)
802 (when (and (integerp size)
803 (> size 0)
804 (listp (eval var)))
805 (desktop-truncate (eval var) size))
806 (insert "(setq "
807 (symbol-name var)
809 (desktop-value-to-string (symbol-value var))
810 ")\n"))))
812 ;; ----------------------------------------------------------------------------
813 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
814 "Return t if buffer should have its state saved in the desktop file.
815 FILENAME is the visited file name, BUFNAME is the buffer name, and
816 MODE is the major mode.
817 \n\(fn FILENAME BUFNAME MODE)"
818 (let ((case-fold-search nil)
819 dired-skip)
820 (and (not (and (stringp desktop-buffers-not-to-save)
821 (not filename)
822 (string-match desktop-buffers-not-to-save bufname)))
823 (not (memq mode desktop-modes-not-to-save))
824 ;; FIXME this is broken if desktop-files-not-to-save is nil.
825 (or (and filename
826 (stringp desktop-files-not-to-save)
827 (not (string-match desktop-files-not-to-save filename)))
828 (and (eq mode 'dired-mode)
829 (with-current-buffer bufname
830 (not (setq dired-skip
831 (string-match desktop-files-not-to-save
832 default-directory)))))
833 (and (null filename)
834 (null dired-skip) ; bug#5755
835 (with-current-buffer bufname desktop-save-buffer))))))
837 ;; ----------------------------------------------------------------------------
838 (defun desktop-file-name (filename dirname)
839 "Convert FILENAME to format specified in `desktop-file-name-format'.
840 DIRNAME must be the directory in which the desktop file will be saved."
841 (cond
842 ((not filename) nil)
843 ((eq desktop-file-name-format 'tilde)
844 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
845 (cond
846 ((file-name-absolute-p relative-name) relative-name)
847 ((string= "./" relative-name) "~/")
848 ((string= "." relative-name) "~")
849 (t (concat "~/" relative-name)))))
850 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
851 (t (expand-file-name filename))))
854 ;; ----------------------------------------------------------------------------
855 ;;;###autoload
856 (defun desktop-save (dirname &optional release)
857 "Save the desktop in a desktop file.
858 Parameter DIRNAME specifies where to save the desktop file.
859 Optional parameter RELEASE says whether we're done with this desktop.
860 See also `desktop-base-file-name'."
861 (interactive "DDirectory to save desktop file in: ")
862 (setq desktop-dirname (file-name-as-directory (expand-file-name dirname)))
863 (save-excursion
864 (let ((eager desktop-restore-eager)
865 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
866 (when
867 (or (not new-modtime) ; nothing to overwrite
868 (equal desktop-file-modtime new-modtime)
869 (yes-or-no-p (if desktop-file-modtime
870 (if (> (float-time new-modtime) (float-time desktop-file-modtime))
871 "Desktop file is more recent than the one loaded. Save anyway? "
872 "Desktop file isn't the one loaded. Overwrite it? ")
873 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
874 (unless release (error "Desktop file conflict")))
876 ;; If we're done with it, release the lock.
877 ;; Otherwise, claim it if it's unclaimed or if we created it.
878 (if release
879 (desktop-release-lock)
880 (unless (and new-modtime (desktop-owner)) (desktop-claim-lock)))
882 (with-temp-buffer
883 (insert
884 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
885 desktop-header
886 ";; Created " (current-time-string) "\n"
887 ";; Desktop file format version " desktop-file-version "\n"
888 ";; Emacs version " emacs-version "\n")
889 (save-excursion (run-hooks 'desktop-save-hook))
890 (goto-char (point-max))
891 (insert "\n;; Global section:\n")
892 (mapc (function desktop-outvar) desktop-globals-to-save)
893 (when (memq 'kill-ring desktop-globals-to-save)
894 (insert
895 "(setq kill-ring-yank-pointer (nthcdr "
896 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
897 " kill-ring))\n"))
899 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
900 (dolist (l (mapcar 'desktop-buffer-info (buffer-list)))
901 (let ((base (pop l)))
902 (when (apply 'desktop-save-buffer-p l)
903 (insert "("
904 (if (or (not (integerp eager))
905 (if (zerop eager)
907 (setq eager (1- eager))))
908 "desktop-create-buffer"
909 "desktop-append-buffer-args")
911 desktop-file-version)
912 ;; If there's a non-empty base name, we save it instead of the buffer name
913 (when (and base (not (string= base "")))
914 (setcar (nthcdr 1 l) base))
915 (dolist (e l)
916 (insert "\n " (desktop-value-to-string e)))
917 (insert ")\n\n"))))
919 (setq default-directory desktop-dirname)
920 (let ((coding-system-for-write 'emacs-mule))
921 (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage))
922 ;; We remember when it was modified (which is presumably just now).
923 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))))))
925 ;; ----------------------------------------------------------------------------
926 ;;;###autoload
927 (defun desktop-remove ()
928 "Delete desktop file in `desktop-dirname'.
929 This function also sets `desktop-dirname' to nil."
930 (interactive)
931 (when desktop-dirname
932 (let ((filename (desktop-full-file-name)))
933 (setq desktop-dirname nil)
934 (when (file-exists-p filename)
935 (delete-file filename)))))
937 (defvar desktop-buffer-args-list nil
938 "List of args for `desktop-create-buffer'.")
940 (defvar desktop-lazy-timer nil)
942 ;; ----------------------------------------------------------------------------
943 ;;;###autoload
944 (defun desktop-read (&optional dirname)
945 "Read and process the desktop file in directory DIRNAME.
946 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
947 directories listed in `desktop-path'. If a desktop file is found, it
948 is processed and `desktop-after-read-hook' is run. If no desktop file
949 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
950 This function is a no-op when Emacs is running in batch mode.
951 It returns t if a desktop file was loaded, nil otherwise."
952 (interactive)
953 (unless noninteractive
954 (setq desktop-dirname
955 (file-name-as-directory
956 (expand-file-name
958 ;; If DIRNAME is specified, use it.
959 (and (< 0 (length dirname)) dirname)
960 ;; Otherwise search desktop file in desktop-path.
961 (let ((dirs desktop-path))
962 (while (and dirs
963 (not (file-exists-p
964 (desktop-full-file-name (car dirs)))))
965 (setq dirs (cdr dirs)))
966 (and dirs (car dirs)))
967 ;; If not found and `desktop-path' is non-nil, use its first element.
968 (and desktop-path (car desktop-path))
969 ;; Default: Home directory.
970 "~"))))
971 (if (file-exists-p (desktop-full-file-name))
972 ;; Desktop file found, but is it already in use?
973 (let ((desktop-first-buffer nil)
974 (desktop-buffer-ok-count 0)
975 (desktop-buffer-fail-count 0)
976 (owner (desktop-owner))
977 ;; Avoid desktop saving during evaluation of desktop buffer.
978 (desktop-save nil))
979 (if (and owner
980 (memq desktop-load-locked-desktop '(nil ask))
981 (or (null desktop-load-locked-desktop)
982 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
983 Using it may cause conflicts. Use it anyway? " owner)))))
984 (let ((default-directory desktop-dirname))
985 (setq desktop-dirname nil)
986 (run-hooks 'desktop-not-loaded-hook)
987 (unless desktop-dirname
988 (message "Desktop file in use; not loaded.")))
989 (desktop-lazy-abort)
990 ;; Evaluate desktop buffer and remember when it was modified.
991 (load (desktop-full-file-name) t t t)
992 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))
993 ;; If it wasn't already, mark it as in-use, to bother other
994 ;; desktop instances.
995 (unless owner
996 (condition-case nil
997 (desktop-claim-lock)
998 (file-error (message "Couldn't record use of desktop file")
999 (sit-for 1))))
1001 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1002 ;; We want buffers existing prior to evaluating the desktop (and
1003 ;; not reused) to be placed at the end of the buffer list, so we
1004 ;; move them here.
1005 (mapc 'bury-buffer
1006 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
1007 (switch-to-buffer (car (buffer-list)))
1008 (run-hooks 'desktop-delay-hook)
1009 (setq desktop-delay-hook nil)
1010 (run-hooks 'desktop-after-read-hook)
1011 (message "Desktop: %d buffer%s restored%s%s."
1012 desktop-buffer-ok-count
1013 (if (= 1 desktop-buffer-ok-count) "" "s")
1014 (if (< 0 desktop-buffer-fail-count)
1015 (format ", %d failed to restore" desktop-buffer-fail-count)
1017 (if desktop-buffer-args-list
1018 (format ", %d to restore lazily"
1019 (length desktop-buffer-args-list))
1020 ""))
1022 ;; No desktop file found.
1023 (desktop-clear)
1024 (let ((default-directory desktop-dirname))
1025 (run-hooks 'desktop-no-desktop-file-hook))
1026 (message "No desktop file.")
1027 nil)))
1029 ;; ----------------------------------------------------------------------------
1030 ;; Maintained for backward compatibility
1031 ;;;###autoload
1032 (defun desktop-load-default ()
1033 "Load the `default' start-up library manually.
1034 Also inhibit further loading of it."
1035 (unless inhibit-default-init ; safety check
1036 (load "default" t t)
1037 (setq inhibit-default-init t)))
1038 (make-obsolete 'desktop-load-default
1039 'desktop-save-mode "22.1")
1041 ;; ----------------------------------------------------------------------------
1042 ;;;###autoload
1043 (defun desktop-change-dir (dirname)
1044 "Change to desktop saved in DIRNAME.
1045 Kill the desktop as specified by variables `desktop-save-mode' and
1046 `desktop-save', then clear the desktop and load the desktop file in
1047 directory DIRNAME."
1048 (interactive "DChange to directory: ")
1049 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
1050 (desktop-kill)
1051 (desktop-clear)
1052 (desktop-read dirname))
1054 ;; ----------------------------------------------------------------------------
1055 ;;;###autoload
1056 (defun desktop-save-in-desktop-dir ()
1057 "Save the desktop in directory `desktop-dirname'."
1058 (interactive)
1059 (if desktop-dirname
1060 (desktop-save desktop-dirname)
1061 (call-interactively 'desktop-save))
1062 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname)))
1064 ;; ----------------------------------------------------------------------------
1065 ;;;###autoload
1066 (defun desktop-revert ()
1067 "Revert to the last loaded desktop."
1068 (interactive)
1069 (unless desktop-dirname
1070 (error "Unknown desktop directory"))
1071 (unless (file-exists-p (desktop-full-file-name))
1072 (error "No desktop file found"))
1073 (desktop-clear)
1074 (desktop-read desktop-dirname))
1076 (defvar desktop-buffer-major-mode)
1077 (defvar desktop-buffer-locals)
1078 ;; ----------------------------------------------------------------------------
1079 (defun desktop-restore-file-buffer (desktop-buffer-file-name
1080 desktop-buffer-name
1081 desktop-buffer-misc)
1082 "Restore a file buffer."
1083 (when desktop-buffer-file-name
1084 (if (or (file-exists-p desktop-buffer-file-name)
1085 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1086 desktop-buffer-file-name)))
1087 (if desktop-missing-file-warning
1088 (y-or-n-p (concat msg " Re-create buffer? "))
1089 (message "%s" msg)
1090 nil)))
1091 (let* ((auto-insert nil) ; Disable auto insertion
1092 (coding-system-for-read
1093 (or coding-system-for-read
1094 (cdr (assq 'buffer-file-coding-system
1095 desktop-buffer-locals))))
1096 (buf (find-file-noselect desktop-buffer-file-name)))
1097 (condition-case nil
1098 (switch-to-buffer buf)
1099 (error (pop-to-buffer buf)))
1100 (and (not (eq major-mode desktop-buffer-major-mode))
1101 (functionp desktop-buffer-major-mode)
1102 (funcall desktop-buffer-major-mode))
1103 buf)
1104 nil)))
1106 (defun desktop-load-file (function)
1107 "Load the file where auto loaded FUNCTION is defined."
1108 (when function
1109 (let ((fcell (and (fboundp function) (symbol-function function))))
1110 (when (and (listp fcell)
1111 (eq 'autoload (car fcell)))
1112 (load (cadr fcell))))))
1114 ;; ----------------------------------------------------------------------------
1115 ;; Create a buffer, load its file, set its mode, ...;
1116 ;; called from Desktop file only.
1118 ;; Just to silence the byte compiler.
1120 (defvar desktop-first-buffer) ; Dynamically bound in `desktop-read'
1122 ;; Bound locally in `desktop-read'.
1123 (defvar desktop-buffer-ok-count)
1124 (defvar desktop-buffer-fail-count)
1126 (defun desktop-create-buffer
1127 (desktop-file-version
1128 desktop-buffer-file-name
1129 desktop-buffer-name
1130 desktop-buffer-major-mode
1131 desktop-buffer-minor-modes
1132 desktop-buffer-point
1133 desktop-buffer-mark
1134 desktop-buffer-read-only
1135 desktop-buffer-misc
1136 &optional
1137 desktop-buffer-locals)
1138 ;; To make desktop files with relative file names possible, we cannot
1139 ;; allow `default-directory' to change. Therefore we save current buffer.
1140 (save-current-buffer
1141 ;; Give major mode module a chance to add a handler.
1142 (desktop-load-file desktop-buffer-major-mode)
1143 (let ((buffer-list (buffer-list))
1144 (result
1145 (condition-case-no-debug err
1146 (funcall (or (cdr (assq desktop-buffer-major-mode
1147 desktop-buffer-mode-handlers))
1148 'desktop-restore-file-buffer)
1149 desktop-buffer-file-name
1150 desktop-buffer-name
1151 desktop-buffer-misc)
1152 (error
1153 (message "Desktop: Can't load buffer %s: %s"
1154 desktop-buffer-name
1155 (error-message-string err))
1156 (when desktop-missing-file-warning (sit-for 1))
1157 nil))))
1158 (if (bufferp result)
1159 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1160 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1161 (setq result nil))
1162 ;; Restore buffer list order with new buffer at end. Don't change
1163 ;; the order for old desktop files (old desktop module behaviour).
1164 (unless (< desktop-file-version 206)
1165 (mapc 'bury-buffer buffer-list)
1166 (when result (bury-buffer result)))
1167 (when result
1168 (unless (or desktop-first-buffer (< desktop-file-version 206))
1169 (setq desktop-first-buffer result))
1170 (set-buffer result)
1171 (unless (equal (buffer-name) desktop-buffer-name)
1172 (rename-buffer desktop-buffer-name t))
1173 ;; minor modes
1174 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1175 (auto-fill-mode 1))
1176 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1177 (auto-fill-mode 0))
1179 (dolist (minor-mode desktop-buffer-minor-modes)
1180 ;; Give minor mode module a chance to add a handler.
1181 (desktop-load-file minor-mode)
1182 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1183 (if handler
1184 (funcall handler desktop-buffer-locals)
1185 (when (functionp minor-mode)
1186 (funcall minor-mode 1)))))))
1187 ;; Even though point and mark are non-nil when written by
1188 ;; `desktop-save', they may be modified by handlers wanting to set
1189 ;; point or mark themselves.
1190 (when desktop-buffer-point
1191 (goto-char
1192 (condition-case err
1193 ;; Evaluate point. Thus point can be something like
1194 ;; '(search-forward ...
1195 (eval desktop-buffer-point)
1196 (error (message "%s" (error-message-string err)) 1))))
1197 (when desktop-buffer-mark
1198 (if (consp desktop-buffer-mark)
1199 (progn
1200 (set-mark (car desktop-buffer-mark))
1201 (setq mark-active (car (cdr desktop-buffer-mark))))
1202 (set-mark desktop-buffer-mark)))
1203 ;; Never override file system if the file really is read-only marked.
1204 (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
1205 (while desktop-buffer-locals
1206 (let ((this (car desktop-buffer-locals)))
1207 (if (consp this)
1208 ;; an entry of this form `(symbol . value)'
1209 (progn
1210 (make-local-variable (car this))
1211 (set (car this) (cdr this)))
1212 ;; an entry of the form `symbol'
1213 (make-local-variable this)
1214 (makunbound this)))
1215 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
1217 ;; ----------------------------------------------------------------------------
1218 ;; Backward compatibility -- update parameters to 205 standards.
1219 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1220 desktop-buffer-major-mode
1221 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
1222 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
1223 desktop-buffer-major-mode (cdr mim) pt mk ro
1224 desktop-buffer-misc
1225 (list (cons 'truncate-lines tl)
1226 (cons 'fill-column fc)
1227 (cons 'case-fold-search cfs)
1228 (cons 'case-replace cr)
1229 (cons 'overwrite-mode (car mim)))))
1231 (defun desktop-append-buffer-args (&rest args)
1232 "Append ARGS at end of `desktop-buffer-args-list'.
1233 ARGS must be an argument list for `desktop-create-buffer'."
1234 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1235 (unless desktop-lazy-timer
1236 (setq desktop-lazy-timer
1237 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1239 (defun desktop-lazy-create-buffer ()
1240 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1241 (when desktop-buffer-args-list
1242 (let* ((remaining (length desktop-buffer-args-list))
1243 (args (pop desktop-buffer-args-list))
1244 (buffer-name (nth 2 args))
1245 (msg (format "Desktop lazily opening %s (%s remaining)..."
1246 buffer-name remaining)))
1247 (when desktop-lazy-verbose
1248 (message "%s" msg))
1249 (let ((desktop-first-buffer nil)
1250 (desktop-buffer-ok-count 0)
1251 (desktop-buffer-fail-count 0))
1252 (apply 'desktop-create-buffer args)
1253 (run-hooks 'desktop-delay-hook)
1254 (setq desktop-delay-hook nil)
1255 (bury-buffer (get-buffer buffer-name))
1256 (when desktop-lazy-verbose
1257 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1259 (defun desktop-idle-create-buffers ()
1260 "Create buffers until the user does something, then stop.
1261 If there are no buffers left to create, kill the timer."
1262 (let ((repeat 1))
1263 (while (and repeat desktop-buffer-args-list)
1264 (save-window-excursion
1265 (desktop-lazy-create-buffer))
1266 (setq repeat (sit-for 0.2))
1267 (unless desktop-buffer-args-list
1268 (cancel-timer desktop-lazy-timer)
1269 (setq desktop-lazy-timer nil)
1270 (message "Lazy desktop load complete")
1271 (sit-for 3)
1272 (message "")))))
1274 (defun desktop-lazy-complete ()
1275 "Run the desktop load to completion."
1276 (interactive)
1277 (let ((desktop-lazy-verbose t))
1278 (while desktop-buffer-args-list
1279 (save-window-excursion
1280 (desktop-lazy-create-buffer)))
1281 (message "Lazy desktop load complete")))
1283 (defun desktop-lazy-abort ()
1284 "Abort lazy loading of the desktop."
1285 (interactive)
1286 (when desktop-lazy-timer
1287 (cancel-timer desktop-lazy-timer)
1288 (setq desktop-lazy-timer nil))
1289 (when desktop-buffer-args-list
1290 (setq desktop-buffer-args-list nil)
1291 (when (called-interactively-p 'interactive)
1292 (message "Lazy desktop load aborted"))))
1294 ;; ----------------------------------------------------------------------------
1295 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1296 ;; command line, we do the rest of what it takes to use desktop, but do it
1297 ;; after finishing loading the init file.
1298 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1299 ;; functions are processed after `after-init-hook'.
1300 (add-hook
1301 'after-init-hook
1302 (lambda ()
1303 (let ((key "--no-desktop"))
1304 (when (member key command-line-args)
1305 (setq command-line-args (delete key command-line-args))
1306 (setq desktop-save-mode nil)))
1307 (when desktop-save-mode
1308 (desktop-read)
1309 (setq inhibit-startup-screen t))))
1311 (provide 'desktop)
1313 ;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1314 ;;; desktop.el ends here