Add 2011 to FSF/AIST copyright years.
[emacs.git] / lisp / desktop.el
blob4af1fea1f9a417c1e693114f1611d3efe7426b4c
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, 2011
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 "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
416 "Alist of major mode specific functions to restore a desktop buffer.
417 Functions listed are called by `desktop-create-buffer' when `desktop-read'
418 evaluates the desktop file. List elements must have the form
420 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
422 Buffers with a major mode not specified here, are restored by the default
423 handler `desktop-restore-file-buffer'.
425 Handlers are called with argument list
427 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
429 Furthermore, they may use the following variables:
431 desktop-file-version
432 desktop-buffer-major-mode
433 desktop-buffer-minor-modes
434 desktop-buffer-point
435 desktop-buffer-mark
436 desktop-buffer-read-only
437 desktop-buffer-locals
439 If a handler returns a buffer, then the saved mode settings
440 and variable values for that buffer are copied into it.
442 Modules that define a major mode that needs a special handler should contain
443 code like
445 (defun foo-restore-desktop-buffer
447 (add-to-list 'desktop-buffer-mode-handlers
448 '(foo-mode . foo-restore-desktop-buffer))
450 Furthermore the major mode function must be autoloaded.")
452 ;;;###autoload
453 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
454 (make-obsolete-variable 'desktop-buffer-handlers
455 'desktop-buffer-mode-handlers "22.1")
457 (defcustom desktop-minor-mode-table
458 '((auto-fill-function auto-fill-mode)
459 (vc-mode nil)
460 (vc-dired-mode nil)
461 (erc-track-minor-mode nil)
462 (savehist-mode nil))
463 "Table mapping minor mode variables to minor mode functions.
464 Each entry has the form (NAME RESTORE-FUNCTION).
465 NAME is the name of the buffer-local variable indicating that the minor
466 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
467 RESTORE-FUNCTION nil means don't try to restore the minor mode.
468 Only minor modes for which the name of the buffer-local variable
469 and the name of the minor mode function are different have to be added to
470 this table. See also `desktop-minor-mode-handlers'."
471 :type 'sexp
472 :group 'desktop)
474 ;;;###autoload
475 (defvar desktop-minor-mode-handlers
477 "Alist of functions to restore non-standard minor modes.
478 Functions are called by `desktop-create-buffer' to restore minor modes.
479 List elements must have the form
481 (MINOR-MODE . RESTORE-FUNCTION).
483 Minor modes not specified here, are restored by the standard minor mode
484 function.
486 Handlers are called with argument list
488 (DESKTOP-BUFFER-LOCALS)
490 Furthermore, they may use the following variables:
492 desktop-file-version
493 desktop-buffer-file-name
494 desktop-buffer-name
495 desktop-buffer-major-mode
496 desktop-buffer-minor-modes
497 desktop-buffer-point
498 desktop-buffer-mark
499 desktop-buffer-read-only
500 desktop-buffer-misc
502 When a handler is called, the buffer has been created and the major mode has
503 been set, but local variables listed in desktop-buffer-locals has not yet been
504 created and set.
506 Modules that define a minor mode that needs a special handler should contain
507 code like
509 (defun foo-desktop-restore
511 (add-to-list 'desktop-minor-mode-handlers
512 '(foo-mode . foo-desktop-restore))
514 Furthermore the minor mode function must be autoloaded.
516 See also `desktop-minor-mode-table'.")
518 ;;;###autoload
519 (put 'desktop-minor-mode-handlers 'risky-local-variable t)
521 ;; ----------------------------------------------------------------------------
522 (defvar desktop-dirname nil
523 "The directory in which the desktop file should be saved.")
525 (defun desktop-full-file-name (&optional dirname)
526 "Return the full name of the desktop file in DIRNAME.
527 DIRNAME omitted or nil means use `desktop-dirname'."
528 (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
530 (defun desktop-full-lock-name (&optional dirname)
531 "Return the full name of the desktop lock file in DIRNAME.
532 DIRNAME omitted or nil means use `desktop-dirname'."
533 (expand-file-name desktop-base-lock-name (or dirname desktop-dirname)))
535 (defconst desktop-header
536 ";; --------------------------------------------------------------------------
537 ;; Desktop File for Emacs
538 ;; --------------------------------------------------------------------------
539 " "*Header to place in Desktop file.")
541 (defvar desktop-delay-hook nil
542 "Hooks run after all buffers are loaded; intended for internal use.")
544 ;; ----------------------------------------------------------------------------
545 ;; Desktop file conflict detection
546 (defvar desktop-file-modtime nil
547 "When the desktop file was last modified to the knowledge of this Emacs.
548 Used to detect desktop file conflicts.")
550 (defun desktop-owner (&optional dirname)
551 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
552 Return nil if no desktop file found or no Emacs process is using it.
553 DIRNAME omitted or nil means use `desktop-dirname'."
554 (let (owner)
555 (and (file-exists-p (desktop-full-lock-name dirname))
556 (condition-case nil
557 (with-temp-buffer
558 (insert-file-contents-literally (desktop-full-lock-name dirname))
559 (goto-char (point-min))
560 (setq owner (read (current-buffer)))
561 (integerp owner))
562 (error nil))
563 owner)))
565 (defun desktop-claim-lock (&optional dirname)
566 "Record this Emacs process as the owner of the desktop file in DIRNAME.
567 DIRNAME omitted or nil means use `desktop-dirname'."
568 (write-region (number-to-string (emacs-pid)) nil
569 (desktop-full-lock-name dirname)))
571 (defun desktop-release-lock (&optional dirname)
572 "Remove the lock file for the desktop in DIRNAME.
573 DIRNAME omitted or nil means use `desktop-dirname'."
574 (let ((file (desktop-full-lock-name dirname)))
575 (when (file-exists-p file) (delete-file file))))
577 ;; ----------------------------------------------------------------------------
578 (defun desktop-truncate (list n)
579 "Truncate LIST to at most N elements destructively."
580 (let ((here (nthcdr (1- n) list)))
581 (when (consp here)
582 (setcdr here nil))))
584 ;; ----------------------------------------------------------------------------
585 ;;;###autoload
586 (defun desktop-clear ()
587 "Empty the Desktop.
588 This kills all buffers except for internal ones and those with names matched by
589 a regular expression in the list `desktop-clear-preserve-buffers'.
590 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
591 (interactive)
592 (desktop-lazy-abort)
593 (dolist (var desktop-globals-to-clear)
594 (if (symbolp var)
595 (eval `(setq-default ,var nil))
596 (eval `(setq-default ,(car var) ,(cdr var)))))
597 (let ((buffers (buffer-list))
598 (preserve-regexp (concat "^\\("
599 (mapconcat (lambda (regexp)
600 (concat "\\(" regexp "\\)"))
601 desktop-clear-preserve-buffers
602 "\\|")
603 "\\)$")))
604 (while buffers
605 (let ((bufname (buffer-name (car buffers))))
607 (null bufname)
608 (string-match preserve-regexp bufname)
609 ;; Don't kill buffers made for internal purposes.
610 (and (not (equal bufname "")) (eq (aref bufname 0) ?\s))
611 (kill-buffer (car buffers))))
612 (setq buffers (cdr buffers))))
613 (delete-other-windows))
615 ;; ----------------------------------------------------------------------------
616 (add-hook 'kill-emacs-hook 'desktop-kill)
618 (defun desktop-kill ()
619 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
620 If the desktop should be saved and `desktop-dirname'
621 is nil, ask the user where to save the desktop."
622 (when (and desktop-save-mode
623 (let ((exists (file-exists-p (desktop-full-file-name))))
624 (or (eq desktop-save t)
625 (and exists (memq desktop-save '(ask-if-new if-exists)))
626 (and
627 (or (memq desktop-save '(ask ask-if-new))
628 (and exists (eq desktop-save 'ask-if-exists)))
629 (y-or-n-p "Save desktop? ")))))
630 (unless desktop-dirname
631 (setq desktop-dirname
632 (file-name-as-directory
633 (expand-file-name
634 (read-directory-name "Directory for desktop file: " nil nil t)))))
635 (condition-case err
636 (desktop-save desktop-dirname t)
637 (file-error
638 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
639 (signal (car err) (cdr err))))))
640 ;; If we own it, we don't anymore.
641 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
643 ;; ----------------------------------------------------------------------------
644 (defun desktop-list* (&rest args)
645 (if (null (cdr args))
646 (car args)
647 (setq args (nreverse args))
648 (let ((value (cons (nth 1 args) (car args))))
649 (setq args (cdr (cdr args)))
650 (while args
651 (setq value (cons (car args) value))
652 (setq args (cdr args)))
653 value)))
655 ;; ----------------------------------------------------------------------------
656 (defun desktop-buffer-info (buffer)
657 (set-buffer buffer)
658 (list
659 ;; base name of the buffer; replaces the buffer name if managed by uniquify
660 (and (fboundp 'uniquify-buffer-base-name) (uniquify-buffer-base-name))
661 ;; basic information
662 (desktop-file-name (buffer-file-name) desktop-dirname)
663 (buffer-name)
664 major-mode
665 ;; minor modes
666 (let (ret)
667 (mapc
668 #'(lambda (minor-mode)
669 (and (boundp minor-mode)
670 (symbol-value minor-mode)
671 (let* ((special (assq minor-mode desktop-minor-mode-table))
672 (value (cond (special (cadr special))
673 ((functionp minor-mode) minor-mode))))
674 (when value (add-to-list 'ret value)))))
675 (mapcar #'car minor-mode-alist))
676 ret)
677 ;; point and mark, and read-only status
678 (point)
679 (list (mark t) mark-active)
680 buffer-read-only
681 ;; auxiliary information
682 (when (functionp desktop-save-buffer)
683 (funcall desktop-save-buffer desktop-dirname))
684 ;; local variables
685 (let ((locals desktop-locals-to-save)
686 (loclist (buffer-local-variables))
687 (ll))
688 (while locals
689 (let ((here (assq (car locals) loclist)))
690 (if here
691 (setq ll (cons here ll))
692 (when (member (car locals) loclist)
693 (setq ll (cons (car locals) ll)))))
694 (setq locals (cdr locals)))
695 ll)))
697 ;; ----------------------------------------------------------------------------
698 (defun desktop-internal-v2s (value)
699 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
700 TXT is a string that when read and evaluated yields value.
701 QUOTE may be `may' (value may be quoted),
702 `must' (values must be quoted), or nil (value may not be quoted)."
703 (cond
704 ((or (numberp value) (null value) (eq t value) (keywordp value))
705 (cons 'may (prin1-to-string value)))
706 ((stringp value)
707 (let ((copy (copy-sequence value)))
708 (set-text-properties 0 (length copy) nil copy)
709 ;; Get rid of text properties because we cannot read them
710 (cons 'may (prin1-to-string copy))))
711 ((symbolp value)
712 (cons 'must (prin1-to-string value)))
713 ((vectorp value)
714 (let* ((special nil)
715 (pass1 (mapcar
716 (lambda (el)
717 (let ((res (desktop-internal-v2s el)))
718 (if (null (car res))
719 (setq special t))
720 res))
721 value)))
722 (if special
723 (cons nil (concat "(vector "
724 (mapconcat (lambda (el)
725 (if (eq (car el) 'must)
726 (concat "'" (cdr el))
727 (cdr el)))
728 pass1
729 " ")
730 ")"))
731 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
732 ((consp value)
733 (let ((p value)
734 newlist
735 use-list*
736 anynil)
737 (while (consp p)
738 (let ((q.txt (desktop-internal-v2s (car p))))
739 (or anynil (setq anynil (null (car q.txt))))
740 (setq newlist (cons q.txt newlist)))
741 (setq p (cdr p)))
742 (if p
743 (let ((last (desktop-internal-v2s p)))
744 (or anynil (setq anynil (null (car last))))
745 (or anynil
746 (setq newlist (cons '(must . ".") newlist)))
747 (setq use-list* t)
748 (setq newlist (cons last newlist))))
749 (setq newlist (nreverse newlist))
750 (if anynil
751 (cons nil
752 (concat (if use-list* "(desktop-list* " "(list ")
753 (mapconcat (lambda (el)
754 (if (eq (car el) 'must)
755 (concat "'" (cdr el))
756 (cdr el)))
757 newlist
758 " ")
759 ")"))
760 (cons 'must
761 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
762 ((subrp value)
763 (cons nil (concat "(symbol-function '"
764 (substring (prin1-to-string value) 7 -1)
765 ")")))
766 ((markerp value)
767 (let ((pos (prin1-to-string (marker-position value)))
768 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
769 (cons nil (concat "(let ((mk (make-marker)))"
770 " (add-hook 'desktop-delay-hook"
771 " (list 'lambda '() (list 'set-marker mk "
772 pos " (get-buffer " buf ")))) mk)"))))
773 (t ; save as text
774 (cons 'may "\"Unprintable entity\""))))
776 ;; ----------------------------------------------------------------------------
777 (defun desktop-value-to-string (value)
778 "Convert VALUE to a string that when read evaluates to the same value.
779 Not all types of values are supported."
780 (let* ((print-escape-newlines t)
781 (float-output-format nil)
782 (quote.txt (desktop-internal-v2s value))
783 (quote (car quote.txt))
784 (txt (cdr quote.txt)))
785 (if (eq quote 'must)
786 (concat "'" txt)
787 txt)))
789 ;; ----------------------------------------------------------------------------
790 (defun desktop-outvar (varspec)
791 "Output a setq statement for variable VAR to the desktop file.
792 The argument VARSPEC may be the variable name VAR (a symbol),
793 or a cons cell of the form (VAR . MAX-SIZE),
794 which means to truncate VAR's value to at most MAX-SIZE elements
795 \(if the value is a list) before saving the value."
796 (let (var size)
797 (if (consp varspec)
798 (setq var (car varspec) size (cdr varspec))
799 (setq var varspec))
800 (when (boundp var)
801 (when (and (integerp size)
802 (> size 0)
803 (listp (eval var)))
804 (desktop-truncate (eval var) size))
805 (insert "(setq "
806 (symbol-name var)
808 (desktop-value-to-string (symbol-value var))
809 ")\n"))))
811 ;; ----------------------------------------------------------------------------
812 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
813 "Return t if buffer should have its state saved in the desktop file.
814 FILENAME is the visited file name, BUFNAME is the buffer name, and
815 MODE is the major mode.
816 \n\(fn FILENAME BUFNAME MODE)"
817 (let ((case-fold-search nil)
818 dired-skip)
819 (and (not (and (stringp desktop-buffers-not-to-save)
820 (not filename)
821 (string-match desktop-buffers-not-to-save bufname)))
822 (not (memq mode desktop-modes-not-to-save))
823 ;; FIXME this is broken if desktop-files-not-to-save is nil.
824 (or (and filename
825 (stringp desktop-files-not-to-save)
826 (not (string-match desktop-files-not-to-save filename)))
827 (and (eq mode 'dired-mode)
828 (with-current-buffer bufname
829 (not (setq dired-skip
830 (string-match desktop-files-not-to-save
831 default-directory)))))
832 (and (null filename)
833 (null dired-skip) ; bug#5755
834 (with-current-buffer bufname desktop-save-buffer))))))
836 ;; ----------------------------------------------------------------------------
837 (defun desktop-file-name (filename dirname)
838 "Convert FILENAME to format specified in `desktop-file-name-format'.
839 DIRNAME must be the directory in which the desktop file will be saved."
840 (cond
841 ((not filename) nil)
842 ((eq desktop-file-name-format 'tilde)
843 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
844 (cond
845 ((file-name-absolute-p relative-name) relative-name)
846 ((string= "./" relative-name) "~/")
847 ((string= "." relative-name) "~")
848 (t (concat "~/" relative-name)))))
849 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
850 (t (expand-file-name filename))))
853 ;; ----------------------------------------------------------------------------
854 ;;;###autoload
855 (defun desktop-save (dirname &optional release)
856 "Save the desktop in a desktop file.
857 Parameter DIRNAME specifies where to save the desktop file.
858 Optional parameter RELEASE says whether we're done with this desktop.
859 See also `desktop-base-file-name'."
860 (interactive "DDirectory to save desktop file in: ")
861 (setq desktop-dirname (file-name-as-directory (expand-file-name dirname)))
862 (save-excursion
863 (let ((eager desktop-restore-eager)
864 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
865 (when
866 (or (not new-modtime) ; nothing to overwrite
867 (equal desktop-file-modtime new-modtime)
868 (yes-or-no-p (if desktop-file-modtime
869 (if (> (float-time new-modtime) (float-time desktop-file-modtime))
870 "Desktop file is more recent than the one loaded. Save anyway? "
871 "Desktop file isn't the one loaded. Overwrite it? ")
872 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
873 (unless release (error "Desktop file conflict")))
875 ;; If we're done with it, release the lock.
876 ;; Otherwise, claim it if it's unclaimed or if we created it.
877 (if release
878 (desktop-release-lock)
879 (unless (and new-modtime (desktop-owner)) (desktop-claim-lock)))
881 (with-temp-buffer
882 (insert
883 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
884 desktop-header
885 ";; Created " (current-time-string) "\n"
886 ";; Desktop file format version " desktop-file-version "\n"
887 ";; Emacs version " emacs-version "\n")
888 (save-excursion (run-hooks 'desktop-save-hook))
889 (goto-char (point-max))
890 (insert "\n;; Global section:\n")
891 (mapc (function desktop-outvar) desktop-globals-to-save)
892 (when (memq 'kill-ring desktop-globals-to-save)
893 (insert
894 "(setq kill-ring-yank-pointer (nthcdr "
895 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
896 " kill-ring))\n"))
898 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
899 (dolist (l (mapcar 'desktop-buffer-info (buffer-list)))
900 (let ((base (pop l)))
901 (when (apply 'desktop-save-buffer-p l)
902 (insert "("
903 (if (or (not (integerp eager))
904 (if (zerop eager)
906 (setq eager (1- eager))))
907 "desktop-create-buffer"
908 "desktop-append-buffer-args")
910 desktop-file-version)
911 ;; If there's a non-empty base name, we save it instead of the buffer name
912 (when (and base (not (string= base "")))
913 (setcar (nthcdr 1 l) base))
914 (dolist (e l)
915 (insert "\n " (desktop-value-to-string e)))
916 (insert ")\n\n"))))
918 (setq default-directory desktop-dirname)
919 (let ((coding-system-for-write 'emacs-mule))
920 (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage))
921 ;; We remember when it was modified (which is presumably just now).
922 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))))))))
924 ;; ----------------------------------------------------------------------------
925 ;;;###autoload
926 (defun desktop-remove ()
927 "Delete desktop file in `desktop-dirname'.
928 This function also sets `desktop-dirname' to nil."
929 (interactive)
930 (when desktop-dirname
931 (let ((filename (desktop-full-file-name)))
932 (setq desktop-dirname nil)
933 (when (file-exists-p filename)
934 (delete-file filename)))))
936 (defvar desktop-buffer-args-list nil
937 "List of args for `desktop-create-buffer'.")
939 (defvar desktop-lazy-timer nil)
941 ;; ----------------------------------------------------------------------------
942 ;;;###autoload
943 (defun desktop-read (&optional dirname)
944 "Read and process the desktop file in directory DIRNAME.
945 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
946 directories listed in `desktop-path'. If a desktop file is found, it
947 is processed and `desktop-after-read-hook' is run. If no desktop file
948 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
949 This function is a no-op when Emacs is running in batch mode.
950 It returns t if a desktop file was loaded, nil otherwise."
951 (interactive)
952 (unless noninteractive
953 (setq desktop-dirname
954 (file-name-as-directory
955 (expand-file-name
957 ;; If DIRNAME is specified, use it.
958 (and (< 0 (length dirname)) dirname)
959 ;; Otherwise search desktop file in desktop-path.
960 (let ((dirs desktop-path))
961 (while (and dirs
962 (not (file-exists-p
963 (desktop-full-file-name (car dirs)))))
964 (setq dirs (cdr dirs)))
965 (and dirs (car dirs)))
966 ;; If not found and `desktop-path' is non-nil, use its first element.
967 (and desktop-path (car desktop-path))
968 ;; Default: Home directory.
969 "~"))))
970 (if (file-exists-p (desktop-full-file-name))
971 ;; Desktop file found, but is it already in use?
972 (let ((desktop-first-buffer nil)
973 (desktop-buffer-ok-count 0)
974 (desktop-buffer-fail-count 0)
975 (owner (desktop-owner))
976 ;; Avoid desktop saving during evaluation of desktop buffer.
977 (desktop-save nil))
978 (if (and owner
979 (memq desktop-load-locked-desktop '(nil ask))
980 (or (null desktop-load-locked-desktop)
981 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
982 Using it may cause conflicts. Use it anyway? " owner)))))
983 (let ((default-directory desktop-dirname))
984 (setq desktop-dirname nil)
985 (run-hooks 'desktop-not-loaded-hook)
986 (unless desktop-dirname
987 (message "Desktop file in use; not loaded.")))
988 (desktop-lazy-abort)
989 ;; Evaluate desktop buffer and remember when it was modified.
990 (load (desktop-full-file-name) t t t)
991 (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))
992 ;; If it wasn't already, mark it as in-use, to bother other
993 ;; desktop instances.
994 (unless owner
995 (condition-case nil
996 (desktop-claim-lock)
997 (file-error (message "Couldn't record use of desktop file")
998 (sit-for 1))))
1000 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1001 ;; We want buffers existing prior to evaluating the desktop (and
1002 ;; not reused) to be placed at the end of the buffer list, so we
1003 ;; move them here.
1004 (mapc 'bury-buffer
1005 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
1006 (switch-to-buffer (car (buffer-list)))
1007 (run-hooks 'desktop-delay-hook)
1008 (setq desktop-delay-hook nil)
1009 (run-hooks 'desktop-after-read-hook)
1010 (message "Desktop: %d buffer%s restored%s%s."
1011 desktop-buffer-ok-count
1012 (if (= 1 desktop-buffer-ok-count) "" "s")
1013 (if (< 0 desktop-buffer-fail-count)
1014 (format ", %d failed to restore" desktop-buffer-fail-count)
1016 (if desktop-buffer-args-list
1017 (format ", %d to restore lazily"
1018 (length desktop-buffer-args-list))
1019 ""))
1021 ;; No desktop file found.
1022 (desktop-clear)
1023 (let ((default-directory desktop-dirname))
1024 (run-hooks 'desktop-no-desktop-file-hook))
1025 (message "No desktop file.")
1026 nil)))
1028 ;; ----------------------------------------------------------------------------
1029 ;; Maintained for backward compatibility
1030 ;;;###autoload
1031 (defun desktop-load-default ()
1032 "Load the `default' start-up library manually.
1033 Also inhibit further loading of it."
1034 (unless inhibit-default-init ; safety check
1035 (load "default" t t)
1036 (setq inhibit-default-init t)))
1037 (make-obsolete 'desktop-load-default
1038 'desktop-save-mode "22.1")
1040 ;; ----------------------------------------------------------------------------
1041 ;;;###autoload
1042 (defun desktop-change-dir (dirname)
1043 "Change to desktop saved in DIRNAME.
1044 Kill the desktop as specified by variables `desktop-save-mode' and
1045 `desktop-save', then clear the desktop and load the desktop file in
1046 directory DIRNAME."
1047 (interactive "DChange to directory: ")
1048 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
1049 (desktop-kill)
1050 (desktop-clear)
1051 (desktop-read dirname))
1053 ;; ----------------------------------------------------------------------------
1054 ;;;###autoload
1055 (defun desktop-save-in-desktop-dir ()
1056 "Save the desktop in directory `desktop-dirname'."
1057 (interactive)
1058 (if desktop-dirname
1059 (desktop-save desktop-dirname)
1060 (call-interactively 'desktop-save))
1061 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname)))
1063 ;; ----------------------------------------------------------------------------
1064 ;;;###autoload
1065 (defun desktop-revert ()
1066 "Revert to the last loaded desktop."
1067 (interactive)
1068 (unless desktop-dirname
1069 (error "Unknown desktop directory"))
1070 (unless (file-exists-p (desktop-full-file-name))
1071 (error "No desktop file found"))
1072 (desktop-clear)
1073 (desktop-read desktop-dirname))
1075 (defvar desktop-buffer-major-mode)
1076 (defvar desktop-buffer-locals)
1077 ;; ----------------------------------------------------------------------------
1078 (defun desktop-restore-file-buffer (desktop-buffer-file-name
1079 desktop-buffer-name
1080 desktop-buffer-misc)
1081 "Restore a file buffer."
1082 (when desktop-buffer-file-name
1083 (if (or (file-exists-p desktop-buffer-file-name)
1084 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1085 desktop-buffer-file-name)))
1086 (if desktop-missing-file-warning
1087 (y-or-n-p (concat msg " Re-create buffer? "))
1088 (message "%s" msg)
1089 nil)))
1090 (let* ((auto-insert nil) ; Disable auto insertion
1091 (coding-system-for-read
1092 (or coding-system-for-read
1093 (cdr (assq 'buffer-file-coding-system
1094 desktop-buffer-locals))))
1095 (buf (find-file-noselect desktop-buffer-file-name)))
1096 (condition-case nil
1097 (switch-to-buffer buf)
1098 (error (pop-to-buffer buf)))
1099 (and (not (eq major-mode desktop-buffer-major-mode))
1100 (functionp desktop-buffer-major-mode)
1101 (funcall desktop-buffer-major-mode))
1102 buf)
1103 nil)))
1105 (defun desktop-load-file (function)
1106 "Load the file where auto loaded FUNCTION is defined."
1107 (when function
1108 (let ((fcell (and (fboundp function) (symbol-function function))))
1109 (when (and (listp fcell)
1110 (eq 'autoload (car fcell)))
1111 (load (cadr fcell))))))
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 (desktop-file-version
1127 desktop-buffer-file-name
1128 desktop-buffer-name
1129 desktop-buffer-major-mode
1130 desktop-buffer-minor-modes
1131 desktop-buffer-point
1132 desktop-buffer-mark
1133 desktop-buffer-read-only
1134 desktop-buffer-misc
1135 &optional
1136 desktop-buffer-locals)
1137 ;; To make desktop files with relative file names possible, we cannot
1138 ;; allow `default-directory' to change. Therefore we save current buffer.
1139 (save-current-buffer
1140 ;; Give major mode module a chance to add a handler.
1141 (desktop-load-file desktop-buffer-major-mode)
1142 (let ((buffer-list (buffer-list))
1143 (result
1144 (condition-case-no-debug err
1145 (funcall (or (cdr (assq desktop-buffer-major-mode
1146 desktop-buffer-mode-handlers))
1147 'desktop-restore-file-buffer)
1148 desktop-buffer-file-name
1149 desktop-buffer-name
1150 desktop-buffer-misc)
1151 (error
1152 (message "Desktop: Can't load buffer %s: %s"
1153 desktop-buffer-name
1154 (error-message-string err))
1155 (when desktop-missing-file-warning (sit-for 1))
1156 nil))))
1157 (if (bufferp result)
1158 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1159 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1160 (setq result nil))
1161 ;; Restore buffer list order with new buffer at end. Don't change
1162 ;; the order for old desktop files (old desktop module behaviour).
1163 (unless (< desktop-file-version 206)
1164 (mapc 'bury-buffer buffer-list)
1165 (when result (bury-buffer result)))
1166 (when result
1167 (unless (or desktop-first-buffer (< desktop-file-version 206))
1168 (setq desktop-first-buffer result))
1169 (set-buffer result)
1170 (unless (equal (buffer-name) desktop-buffer-name)
1171 (rename-buffer desktop-buffer-name t))
1172 ;; minor modes
1173 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1174 (auto-fill-mode 1))
1175 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1176 (auto-fill-mode 0))
1178 (dolist (minor-mode desktop-buffer-minor-modes)
1179 ;; Give minor mode module a chance to add a handler.
1180 (desktop-load-file minor-mode)
1181 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1182 (if handler
1183 (funcall handler desktop-buffer-locals)
1184 (when (functionp minor-mode)
1185 (funcall minor-mode 1)))))))
1186 ;; Even though point and mark are non-nil when written by
1187 ;; `desktop-save', they may be modified by handlers wanting to set
1188 ;; point or mark themselves.
1189 (when desktop-buffer-point
1190 (goto-char
1191 (condition-case err
1192 ;; Evaluate point. Thus point can be something like
1193 ;; '(search-forward ...
1194 (eval desktop-buffer-point)
1195 (error (message "%s" (error-message-string err)) 1))))
1196 (when desktop-buffer-mark
1197 (if (consp desktop-buffer-mark)
1198 (progn
1199 (set-mark (car desktop-buffer-mark))
1200 (setq mark-active (car (cdr desktop-buffer-mark))))
1201 (set-mark desktop-buffer-mark)))
1202 ;; Never override file system if the file really is read-only marked.
1203 (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
1204 (while desktop-buffer-locals
1205 (let ((this (car desktop-buffer-locals)))
1206 (if (consp this)
1207 ;; an entry of this form `(symbol . value)'
1208 (progn
1209 (make-local-variable (car this))
1210 (set (car this) (cdr this)))
1211 ;; an entry of the form `symbol'
1212 (make-local-variable this)
1213 (makunbound this)))
1214 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
1216 ;; ----------------------------------------------------------------------------
1217 ;; Backward compatibility -- update parameters to 205 standards.
1218 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1219 desktop-buffer-major-mode
1220 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
1221 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
1222 desktop-buffer-major-mode (cdr mim) pt mk ro
1223 desktop-buffer-misc
1224 (list (cons 'truncate-lines tl)
1225 (cons 'fill-column fc)
1226 (cons 'case-fold-search cfs)
1227 (cons 'case-replace cr)
1228 (cons 'overwrite-mode (car mim)))))
1230 (defun desktop-append-buffer-args (&rest args)
1231 "Append ARGS at end of `desktop-buffer-args-list'.
1232 ARGS must be an argument list for `desktop-create-buffer'."
1233 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1234 (unless desktop-lazy-timer
1235 (setq desktop-lazy-timer
1236 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1238 (defun desktop-lazy-create-buffer ()
1239 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1240 (when desktop-buffer-args-list
1241 (let* ((remaining (length desktop-buffer-args-list))
1242 (args (pop desktop-buffer-args-list))
1243 (buffer-name (nth 2 args))
1244 (msg (format "Desktop lazily opening %s (%s remaining)..."
1245 buffer-name remaining)))
1246 (when desktop-lazy-verbose
1247 (message "%s" msg))
1248 (let ((desktop-first-buffer nil)
1249 (desktop-buffer-ok-count 0)
1250 (desktop-buffer-fail-count 0))
1251 (apply 'desktop-create-buffer args)
1252 (run-hooks 'desktop-delay-hook)
1253 (setq desktop-delay-hook nil)
1254 (bury-buffer (get-buffer buffer-name))
1255 (when desktop-lazy-verbose
1256 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1258 (defun desktop-idle-create-buffers ()
1259 "Create buffers until the user does something, then stop.
1260 If there are no buffers left to create, kill the timer."
1261 (let ((repeat 1))
1262 (while (and repeat desktop-buffer-args-list)
1263 (save-window-excursion
1264 (desktop-lazy-create-buffer))
1265 (setq repeat (sit-for 0.2))
1266 (unless desktop-buffer-args-list
1267 (cancel-timer desktop-lazy-timer)
1268 (setq desktop-lazy-timer nil)
1269 (message "Lazy desktop load complete")
1270 (sit-for 3)
1271 (message "")))))
1273 (defun desktop-lazy-complete ()
1274 "Run the desktop load to completion."
1275 (interactive)
1276 (let ((desktop-lazy-verbose t))
1277 (while desktop-buffer-args-list
1278 (save-window-excursion
1279 (desktop-lazy-create-buffer)))
1280 (message "Lazy desktop load complete")))
1282 (defun desktop-lazy-abort ()
1283 "Abort lazy loading of the desktop."
1284 (interactive)
1285 (when desktop-lazy-timer
1286 (cancel-timer desktop-lazy-timer)
1287 (setq desktop-lazy-timer nil))
1288 (when desktop-buffer-args-list
1289 (setq desktop-buffer-args-list nil)
1290 (when (called-interactively-p 'interactive)
1291 (message "Lazy desktop load aborted"))))
1293 ;; ----------------------------------------------------------------------------
1294 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1295 ;; command line, we do the rest of what it takes to use desktop, but do it
1296 ;; after finishing loading the init file.
1297 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1298 ;; functions are processed after `after-init-hook'.
1299 (add-hook
1300 'after-init-hook
1301 (lambda ()
1302 (let ((key "--no-desktop"))
1303 (when (member key command-line-args)
1304 (setq command-line-args (delete key command-line-args))
1305 (setq desktop-save-mode nil)))
1306 (when desktop-save-mode
1307 (desktop-read)
1308 (setq inhibit-startup-screen t))))
1310 (provide 'desktop)
1312 ;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1313 ;;; desktop.el ends here