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 Free Software Foundation, Inc.
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Keywords: convenience
8 ;; Favourite-brand-of-beer: None, I hate beer.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Save the Desktop, i.e.,
28 ;; - some global variables
29 ;; - the list of buffers with associated files. For each buffer also
31 ;; - the default directory
33 ;; - the mark & mark-active
35 ;; - some local variables
37 ;; To use this, use customize to turn on desktop-save-mode or add the
38 ;; following line somewhere in your .emacs file:
40 ;; (desktop-save-mode 1)
42 ;; For further usage information, look at the section
43 ;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual.
45 ;; When the desktop module is loaded, the function `desktop-kill' is
46 ;; added to the `kill-emacs-hook'. This function is responsible for
47 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
48 ;; function is added to the `after-init-hook'. This function is
49 ;; responsible for loading the desktop when Emacs is started.
53 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
54 ;; are supplied to handle special major and minor modes respectively.
55 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
56 ;; to restore a desktop buffer. Elements must have the form
58 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
60 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
61 ;; evaluates the desktop file. Buffers with a major mode not specified here,
62 ;; are restored by the default handler `desktop-restore-file-buffer'.
63 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
64 ;; non-standard minor modes. Elements must have the form
66 ;; (MINOR-MODE . RESTORE-FUNCTION).
68 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
69 ;; Minor modes not specified here, are restored by the standard minor mode
70 ;; function. If you write a module that defines a major or minor mode that
71 ;; needs a special handler, then place code like
73 ;; (defun foo-restore-desktop-buffer
75 ;; (add-to-list 'desktop-buffer-mode-handlers
76 ;; '(foo-mode . foo-restore-desktop-buffer))
80 ;; (defun bar-desktop-restore
82 ;; (add-to-list 'desktop-minor-mode-handlers
83 ;; '(bar-mode . bar-desktop-restore))
85 ;; in the module itself, and make sure that the mode function is
86 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
87 ;; `desktop-minor-mode-handlers' for more info.
91 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
92 ;; manual) are handled in the following way:
93 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
94 ;; saves as `desktop-minor-modes' the list of names of those variables in
95 ;; `minor-mode-alist' that have a non-nil value.
96 ;; When `desktop-create' restores the buffer, each of the symbols in
97 ;; `desktop-minor-modes' is called as function with parameter 1.
98 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
99 ;; are used to handle non-conventional minor modes. `desktop-save' uses
100 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
101 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
102 ;; variable name that is different form its function name, an entry
104 ;; (NAME RESTORE-FUNCTION)
106 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
107 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
108 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
109 ;; function different from the usual minor mode function.
110 ;; ---------------------------------------------------------------------------
112 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
113 ;; in your home directory is used for that. Saving global default values
114 ;; for buffers is an example of misuse.
116 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
117 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
118 ;; things you did not mean to keep. Use M-x desktop-clear RET.
120 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
121 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
122 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
123 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
124 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
125 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
126 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
127 ;; ---------------------------------------------------------------------------
130 ;; Save window configuration.
131 ;; Recognize more minor modes.
136 (defvar desktop-file-version
"206"
137 "Version number of desktop file format.
138 Written into the desktop file and used at desktop read to provide
139 backward compatibility.")
141 ;; ----------------------------------------------------------------------------
142 ;; USER OPTIONS -- settings you might want to play with.
143 ;; ----------------------------------------------------------------------------
145 (defgroup desktop nil
146 "Save status of Emacs when you exit."
150 (define-minor-mode desktop-save-mode
151 "Toggle desktop saving mode.
152 With numeric ARG, turn desktop saving on if ARG is positive, off
153 otherwise. If desktop saving is turned on, the state of Emacs is
154 saved from one session to another. See variable `desktop-save'
155 and function `desktop-read' for details."
159 ;; Maintained for backward compatibility
160 (define-obsolete-variable-alias 'desktop-enable
161 'desktop-save-mode
"22.1")
163 (defun desktop-save-mode-off ()
164 "Disable `desktop-save-mode'. Provided for use in hooks."
165 (desktop-save-mode 0))
167 (defcustom desktop-save
'ask-if-new
168 "Specifies whether the desktop should be saved when it is killed.
169 A desktop is killed when the user changes desktop or quits Emacs.
173 ask-if-new -- ask if no desktop file exists, otherwise just save.
174 ask-if-exists -- ask if desktop file exists, otherwise don't save.
175 if-exists -- save if desktop file exists, otherwise don't save.
177 The desktop is never saved when `desktop-save-mode' is nil.
178 The variables `desktop-dirname' and `desktop-base-file-name'
179 determine where the desktop is saved."
182 (const :tag
"Always save" t
)
183 (const :tag
"Always ask" ask
)
184 (const :tag
"Ask if desktop file is new, else do save" ask-if-new
)
185 (const :tag
"Ask if desktop file exists, else don't save" ask-if-exists
)
186 (const :tag
"Save if desktop file exists, else don't" if-exists
)
187 (const :tag
"Never save" nil
))
191 (defcustom desktop-load-locked-desktop
'ask
192 "Specifies whether the desktop should be loaded if locked.
197 If the value is nil, or `ask' and the user chooses not to load the desktop,
198 the normal hook `desktop-not-loaded-hook' is run."
201 (const :tag
"Load anyway" t
)
202 (const :tag
"Don't load" nil
)
203 (const :tag
"Ask the user" ask
))
207 (define-obsolete-variable-alias 'desktop-basefilename
208 'desktop-base-file-name
"22.1")
210 (defcustom desktop-base-file-name
211 (convert-standard-filename ".emacs.desktop")
212 "Name of file for Emacs desktop, excluding the directory part."
216 (defcustom desktop-base-lock-name
217 (convert-standard-filename ".emacs.desktop.lock")
218 "Name of lock file for Emacs desktop, excluding the directory part."
223 (defcustom desktop-path
(list "." user-emacs-directory
"~")
224 "List of directories to search for the desktop file.
225 The base name of the file is specified in `desktop-base-file-name'."
226 :type
'(repeat directory
)
230 (defcustom desktop-missing-file-warning nil
231 "If non-nil, offer to recreate the buffer of a deleted file.
232 Also pause for a moment to display message about errors signaled in
233 `desktop-buffer-mode-handlers'.
235 If nil, just print error messages in the message buffer."
240 (defcustom desktop-no-desktop-file-hook nil
241 "Normal hook run when `desktop-read' can't find a desktop file.
242 Run in the directory in which the desktop file was sought.
243 May be used to show a dired buffer."
248 (defcustom desktop-not-loaded-hook nil
249 "Normal hook run when the user declines to re-use a desktop file.
250 Run in the directory in which the desktop file was found.
251 May be used to deal with accidental multiple Emacs jobs."
254 :options
'(desktop-save-mode-off save-buffers-kill-emacs
)
257 (defcustom desktop-after-read-hook nil
258 "Normal hook run after a successful `desktop-read'.
259 May be used to show a buffer list."
262 :options
'(list-buffers)
265 (defcustom desktop-save-hook nil
266 "Normal hook run before the desktop is saved in a desktop file.
267 Run with the desktop buffer current with only the header present.
268 May be used to add to the desktop code or to truncate history lists,
273 (defcustom desktop-globals-to-save
274 '(desktop-missing-file-warning
281 "List of global variables saved by `desktop-save'.
282 An element may be variable name (a symbol) or a cons cell of the form
283 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
284 MAX-SIZE elements (if the value is a list) before saving the value.
285 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
286 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
289 (defcustom desktop-globals-to-clear
291 kill-ring-yank-pointer
293 search-ring-yank-pointer
295 regexp-search-ring-yank-pointer
)
296 "List of global variables that `desktop-clear' will clear.
297 An element may be variable name (a symbol) or a cons cell of the form
298 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
299 to the value obtained by evaluating FORM."
300 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
304 (defcustom desktop-clear-preserve-buffers
305 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*")
306 "List of buffers that `desktop-clear' should not delete.
307 Each element is a regular expression. Buffers with a name matched by any of
308 these won't be deleted."
309 :type
'(repeat string
)
313 (defcustom desktop-locals-to-save
314 '(desktop-locals-to-save ; Itself! Think it over.
320 change-log-default-name
324 buffer-file-coding-system
327 indicate-buffer-boundaries
329 show-trailing-whitespace
)
330 "List of local variables to save for each buffer.
331 The variables are saved only when they really are local. Conventional minor
332 modes are restored automatically; they should not be listed here."
333 :type
'(repeat symbol
)
336 (defcustom desktop-buffers-not-to-save nil
337 "Regexp identifying buffers that are to be excluded from saving."
338 :type
'(choice (const :tag
"None" nil
)
340 :version
"23.2" ; set to nil
343 ;; Skip tramp and ange-ftp files
344 (defcustom desktop-files-not-to-save
345 "\\(^/[^/:]*:\\|(ftp)$\\)"
346 "Regexp identifying files whose buffers are to be excluded from saving."
347 :type
'(choice (const :tag
"None" nil
)
351 ;; We skip TAGS files to save time (tags-file-name is saved instead).
352 (defcustom desktop-modes-not-to-save
354 "List of major modes whose buffers should not be saved."
355 :type
'(repeat symbol
)
358 (defcustom desktop-file-name-format
'absolute
359 "Format in which desktop file names should be saved.
361 absolute -- Absolute file name.
362 tilde -- Relative to ~.
363 local -- Relative to directory of desktop file."
364 :type
'(choice (const absolute
) (const tilde
) (const local
))
368 (defcustom desktop-restore-eager t
369 "Number of buffers to restore immediately.
370 Remaining buffers are restored lazily (when Emacs is idle).
371 If value is t, all buffers are restored immediately."
372 :type
'(choice (const t
) integer
)
376 (defcustom desktop-lazy-verbose t
377 "Verbose reporting of lazily created buffers."
382 (defcustom desktop-lazy-idle-delay
5
383 "Idle delay before starting to create buffers.
384 See `desktop-restore-eager'."
390 (defvar desktop-save-buffer nil
391 "When non-nil, save buffer status in desktop file.
392 This variable becomes buffer local when set.
394 If the value is a function, it is called by `desktop-save' with argument
395 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
396 file along with the state of the buffer for which it was called.
398 When file names are returned, they should be formatted using the call
399 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
401 Later, when `desktop-read' evaluates the desktop file, auxiliary information
402 is passed as the argument DESKTOP-BUFFER-MISC to functions in
403 `desktop-buffer-mode-handlers'.")
404 (make-variable-buffer-local 'desktop-save-buffer
)
405 (make-obsolete-variable 'desktop-buffer-modes-to-save
406 'desktop-save-buffer
"22.1")
407 (make-obsolete-variable 'desktop-buffer-misc-functions
408 'desktop-save-buffer
"22.1")
411 (defvar desktop-buffer-mode-handlers
413 "Alist of major mode specific functions to restore a desktop buffer.
414 Functions listed are called by `desktop-create-buffer' when `desktop-read'
415 evaluates the desktop file. List elements must have the form
417 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
419 Buffers with a major mode not specified here, are restored by the default
420 handler `desktop-restore-file-buffer'.
422 Handlers are called with argument list
424 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
426 Furthermore, they may use the following variables:
429 desktop-buffer-major-mode
430 desktop-buffer-minor-modes
433 desktop-buffer-read-only
434 desktop-buffer-locals
436 If a handler returns a buffer, then the saved mode settings
437 and variable values for that buffer are copied into it.
439 Modules that define a major mode that needs a special handler should contain
442 (defun foo-restore-desktop-buffer
444 (add-to-list 'desktop-buffer-mode-handlers
445 '(foo-mode . foo-restore-desktop-buffer))
447 Furthermore the major mode function must be autoloaded.")
450 (put 'desktop-buffer-mode-handlers
'risky-local-variable t
)
451 (make-obsolete-variable 'desktop-buffer-handlers
452 'desktop-buffer-mode-handlers
"22.1")
454 (defcustom desktop-minor-mode-table
455 '((auto-fill-function auto-fill-mode
)
458 (erc-track-minor-mode nil
)
460 "Table mapping minor mode variables to minor mode functions.
461 Each entry has the form (NAME RESTORE-FUNCTION).
462 NAME is the name of the buffer-local variable indicating that the minor
463 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
464 RESTORE-FUNCTION nil means don't try to restore the minor mode.
465 Only minor modes for which the name of the buffer-local variable
466 and the name of the minor mode function are different have to be added to
467 this table. See also `desktop-minor-mode-handlers'."
472 (defvar desktop-minor-mode-handlers
474 "Alist of functions to restore non-standard minor modes.
475 Functions are called by `desktop-create-buffer' to restore minor modes.
476 List elements must have the form
478 (MINOR-MODE . RESTORE-FUNCTION).
480 Minor modes not specified here, are restored by the standard minor mode
483 Handlers are called with argument list
485 (DESKTOP-BUFFER-LOCALS)
487 Furthermore, they may use the following variables:
490 desktop-buffer-file-name
492 desktop-buffer-major-mode
493 desktop-buffer-minor-modes
496 desktop-buffer-read-only
499 When a handler is called, the buffer has been created and the major mode has
500 been set, but local variables listed in desktop-buffer-locals has not yet been
503 Modules that define a minor mode that needs a special handler should contain
506 (defun foo-desktop-restore
508 (add-to-list 'desktop-minor-mode-handlers
509 '(foo-mode . foo-desktop-restore))
511 Furthermore the minor mode function must be autoloaded.
513 See also `desktop-minor-mode-table'.")
516 (put 'desktop-minor-mode-handlers
'risky-local-variable t
)
518 ;; ----------------------------------------------------------------------------
519 (defvar desktop-dirname nil
520 "The directory in which the desktop file should be saved.")
522 (defun desktop-full-file-name (&optional dirname
)
523 "Return the full name of the desktop file in DIRNAME.
524 DIRNAME omitted or nil means use `desktop-dirname'."
525 (expand-file-name desktop-base-file-name
(or dirname desktop-dirname
)))
527 (defun desktop-full-lock-name (&optional dirname
)
528 "Return the full name of the desktop lock file in DIRNAME.
529 DIRNAME omitted or nil means use `desktop-dirname'."
530 (expand-file-name desktop-base-lock-name
(or dirname desktop-dirname
)))
532 (defconst desktop-header
533 ";; --------------------------------------------------------------------------
534 ;; Desktop File for Emacs
535 ;; --------------------------------------------------------------------------
536 " "*Header to place in Desktop file.")
538 (defvar desktop-delay-hook nil
539 "Hooks run after all buffers are loaded; intended for internal use.")
541 ;; ----------------------------------------------------------------------------
542 ;; Desktop file conflict detection
543 (defvar desktop-file-modtime nil
544 "When the desktop file was last modified to the knowledge of this Emacs.
545 Used to detect desktop file conflicts.")
547 (defun desktop-owner (&optional dirname
)
548 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
549 Return nil if no desktop file found or no Emacs process is using it.
550 DIRNAME omitted or nil means use `desktop-dirname'."
552 (and (file-exists-p (desktop-full-lock-name dirname
))
555 (insert-file-contents-literally (desktop-full-lock-name dirname
))
556 (goto-char (point-min))
557 (setq owner
(read (current-buffer)))
562 (defun desktop-claim-lock (&optional dirname
)
563 "Record this Emacs process as the owner of the desktop file in DIRNAME.
564 DIRNAME omitted or nil means use `desktop-dirname'."
565 (write-region (number-to-string (emacs-pid)) nil
566 (desktop-full-lock-name dirname
)))
568 (defun desktop-release-lock (&optional dirname
)
569 "Remove the lock file for the desktop in DIRNAME.
570 DIRNAME omitted or nil means use `desktop-dirname'."
571 (let ((file (desktop-full-lock-name dirname
)))
572 (when (file-exists-p file
) (delete-file file
))))
574 ;; ----------------------------------------------------------------------------
575 (defun desktop-truncate (list n
)
576 "Truncate LIST to at most N elements destructively."
577 (let ((here (nthcdr (1- n
) list
)))
581 ;; ----------------------------------------------------------------------------
583 (defun desktop-clear ()
585 This kills all buffers except for internal ones and those with names matched by
586 a regular expression in the list `desktop-clear-preserve-buffers'.
587 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
590 (dolist (var desktop-globals-to-clear
)
592 (eval `(setq-default ,var nil
))
593 (eval `(setq-default ,(car var
) ,(cdr var
)))))
594 (let ((buffers (buffer-list))
595 (preserve-regexp (concat "^\\("
596 (mapconcat (lambda (regexp)
597 (concat "\\(" regexp
"\\)"))
598 desktop-clear-preserve-buffers
602 (let ((bufname (buffer-name (car buffers
))))
605 (string-match preserve-regexp bufname
)
606 ;; Don't kill buffers made for internal purposes.
607 (and (not (equal bufname
"")) (eq (aref bufname
0) ?\s
))
608 (kill-buffer (car buffers
))))
609 (setq buffers
(cdr buffers
))))
610 (delete-other-windows))
612 ;; ----------------------------------------------------------------------------
613 (add-hook 'kill-emacs-hook
'desktop-kill
)
615 (defun desktop-kill ()
616 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
617 If the desktop should be saved and `desktop-dirname'
618 is nil, ask the user where to save the desktop."
619 (when (and desktop-save-mode
620 (let ((exists (file-exists-p (desktop-full-file-name))))
621 (or (eq desktop-save t
)
622 (and exists
(memq desktop-save
'(ask-if-new if-exists
)))
624 (or (memq desktop-save
'(ask ask-if-new
))
625 (and exists
(eq desktop-save
'ask-if-exists
)))
626 (y-or-n-p "Save desktop? ")))))
627 (unless desktop-dirname
628 (setq desktop-dirname
629 (file-name-as-directory
631 (read-directory-name "Directory for desktop file: " nil nil t
)))))
633 (desktop-save desktop-dirname t
)
635 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
636 (signal (car err
) (cdr err
))))))
637 ;; If we own it, we don't anymore.
638 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
640 ;; ----------------------------------------------------------------------------
641 (defun desktop-list* (&rest args
)
642 (if (null (cdr args
))
644 (setq args
(nreverse args
))
645 (let ((value (cons (nth 1 args
) (car args
))))
646 (setq args
(cdr (cdr args
)))
648 (setq value
(cons (car args
) value
))
649 (setq args
(cdr args
)))
652 ;; ----------------------------------------------------------------------------
653 (defun desktop-buffer-info (buffer)
656 ;; base name of the buffer; replaces the buffer name if managed by uniquify
657 (and (fboundp 'uniquify-buffer-base-name
) (uniquify-buffer-base-name))
659 (desktop-file-name (buffer-file-name) desktop-dirname
)
665 #'(lambda (minor-mode)
666 (and (boundp minor-mode
)
667 (symbol-value minor-mode
)
668 (let* ((special (assq minor-mode desktop-minor-mode-table
))
669 (value (cond (special (cadr special
))
670 ((functionp minor-mode
) minor-mode
))))
671 (when value
(add-to-list 'ret value
)))))
672 (mapcar #'car minor-mode-alist
))
674 ;; point and mark, and read-only status
676 (list (mark t
) mark-active
)
678 ;; auxiliary information
679 (when (functionp desktop-save-buffer
)
680 (funcall desktop-save-buffer desktop-dirname
))
682 (let ((locals desktop-locals-to-save
)
683 (loclist (buffer-local-variables))
686 (let ((here (assq (car locals
) loclist
)))
688 (setq ll
(cons here ll
))
689 (when (member (car locals
) loclist
)
690 (setq ll
(cons (car locals
) ll
)))))
691 (setq locals
(cdr locals
)))
694 ;; ----------------------------------------------------------------------------
695 (defun desktop-internal-v2s (value)
696 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
697 TXT is a string that when read and evaluated yields value.
698 QUOTE may be `may' (value may be quoted),
699 `must' (values must be quoted), or nil (value may not be quoted)."
701 ((or (numberp value
) (null value
) (eq t value
) (keywordp value
))
702 (cons 'may
(prin1-to-string value
)))
704 (let ((copy (copy-sequence value
)))
705 (set-text-properties 0 (length copy
) nil copy
)
706 ;; Get rid of text properties because we cannot read them
707 (cons 'may
(prin1-to-string copy
))))
709 (cons 'must
(prin1-to-string value
)))
714 (let ((res (desktop-internal-v2s el
)))
720 (cons nil
(concat "(vector "
721 (mapconcat (lambda (el)
722 (if (eq (car el
) 'must
)
723 (concat "'" (cdr el
))
728 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
735 (let ((q.txt
(desktop-internal-v2s (car p
))))
736 (or anynil
(setq anynil
(null (car q.txt
))))
737 (setq newlist
(cons q.txt newlist
)))
740 (let ((last (desktop-internal-v2s p
)))
741 (or anynil
(setq anynil
(null (car last
))))
743 (setq newlist
(cons '(must .
".") newlist
)))
745 (setq newlist
(cons last newlist
))))
746 (setq newlist
(nreverse newlist
))
749 (concat (if use-list
* "(desktop-list* " "(list ")
750 (mapconcat (lambda (el)
751 (if (eq (car el
) 'must
)
752 (concat "'" (cdr el
))
758 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
760 (cons nil
(concat "(symbol-function '"
761 (substring (prin1-to-string value
) 7 -
1)
764 (let ((pos (prin1-to-string (marker-position value
)))
765 (buf (prin1-to-string (buffer-name (marker-buffer value
)))))
766 (cons nil
(concat "(let ((mk (make-marker)))"
767 " (add-hook 'desktop-delay-hook"
768 " (list 'lambda '() (list 'set-marker mk "
769 pos
" (get-buffer " buf
")))) mk)"))))
771 (cons 'may
"\"Unprintable entity\""))))
773 ;; ----------------------------------------------------------------------------
774 (defun desktop-value-to-string (value)
775 "Convert VALUE to a string that when read evaluates to the same value.
776 Not all types of values are supported."
777 (let* ((print-escape-newlines t
)
778 (float-output-format nil
)
779 (quote.txt
(desktop-internal-v2s value
))
780 (quote (car quote.txt
))
781 (txt (cdr quote.txt
)))
786 ;; ----------------------------------------------------------------------------
787 (defun desktop-outvar (varspec)
788 "Output a setq statement for variable VAR to the desktop file.
789 The argument VARSPEC may be the variable name VAR (a symbol),
790 or a cons cell of the form (VAR . MAX-SIZE),
791 which means to truncate VAR's value to at most MAX-SIZE elements
792 \(if the value is a list) before saving the value."
795 (setq var
(car varspec
) size
(cdr varspec
))
798 (when (and (integerp size
)
801 (desktop-truncate (eval var
) size
))
805 (desktop-value-to-string (symbol-value var
))
808 ;; ----------------------------------------------------------------------------
809 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
810 "Return t if buffer should have its state saved in the desktop file.
811 FILENAME is the visited file name, BUFNAME is the buffer name, and
812 MODE is the major mode.
813 \n\(fn FILENAME BUFNAME MODE)"
814 (let ((case-fold-search nil
))
815 (and (not (and (stringp desktop-buffers-not-to-save
)
817 (string-match desktop-buffers-not-to-save bufname
)))
818 (not (memq mode desktop-modes-not-to-save
))
820 (stringp desktop-files-not-to-save
)
821 (not (string-match desktop-files-not-to-save filename
)))
822 (and (eq mode
'dired-mode
)
823 (with-current-buffer bufname
824 (not (string-match desktop-files-not-to-save
825 default-directory
))))
827 (with-current-buffer bufname desktop-save-buffer
))))))
829 ;; ----------------------------------------------------------------------------
830 (defun desktop-file-name (filename dirname
)
831 "Convert FILENAME to format specified in `desktop-file-name-format'.
832 DIRNAME must be the directory in which the desktop file will be saved."
835 ((eq desktop-file-name-format
'tilde
)
836 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
838 ((file-name-absolute-p relative-name
) relative-name
)
839 ((string= "./" relative-name
) "~/")
840 ((string= "." relative-name
) "~")
841 (t (concat "~/" relative-name
)))))
842 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
843 (t (expand-file-name filename
))))
846 ;; ----------------------------------------------------------------------------
848 (defun desktop-save (dirname &optional release
)
849 "Save the desktop in a desktop file.
850 Parameter DIRNAME specifies where to save the desktop file.
851 Optional parameter RELEASE says whether we're done with this desktop.
852 See also `desktop-base-file-name'."
853 (interactive "DDirectory to save desktop file in: ")
854 (setq desktop-dirname
(file-name-as-directory (expand-file-name dirname
)))
856 (let ((eager desktop-restore-eager
)
857 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
859 (or (not new-modtime
) ; nothing to overwrite
860 (equal desktop-file-modtime new-modtime
)
861 (yes-or-no-p (if desktop-file-modtime
862 (if (> (float-time new-modtime
) (float-time desktop-file-modtime
))
863 "Desktop file is more recent than the one loaded. Save anyway? "
864 "Desktop file isn't the one loaded. Overwrite it? ")
865 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
866 (unless release
(error "Desktop file conflict")))
868 ;; If we're done with it, release the lock.
869 ;; Otherwise, claim it if it's unclaimed or if we created it.
871 (desktop-release-lock)
872 (unless (and new-modtime
(desktop-owner)) (desktop-claim-lock)))
876 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
878 ";; Created " (current-time-string) "\n"
879 ";; Desktop file format version " desktop-file-version
"\n"
880 ";; Emacs version " emacs-version
"\n")
881 (save-excursion (run-hooks 'desktop-save-hook
))
882 (goto-char (point-max))
883 (insert "\n;; Global section:\n")
884 (mapc (function desktop-outvar
) desktop-globals-to-save
)
885 (when (memq 'kill-ring desktop-globals-to-save
)
887 "(setq kill-ring-yank-pointer (nthcdr "
888 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
891 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
892 (dolist (l (mapcar 'desktop-buffer-info
(buffer-list)))
893 (let ((base (pop l
)))
894 (when (apply 'desktop-save-buffer-p l
)
896 (if (or (not (integerp eager
))
899 (setq eager
(1- eager
))))
900 "desktop-create-buffer"
901 "desktop-append-buffer-args")
903 desktop-file-version
)
904 ;; If there's a non-empty base name, we save it instead of the buffer name
905 (when (and base
(not (string= base
"")))
906 (setcar (nthcdr 1 l
) base
))
908 (insert "\n " (desktop-value-to-string e
)))
911 (setq default-directory desktop-dirname
)
912 (let ((coding-system-for-write 'emacs-mule
))
913 (write-region (point-min) (point-max) (desktop-full-file-name) nil
'nomessage
))
914 ;; We remember when it was modified (which is presumably just now).
915 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name)))))))))
917 ;; ----------------------------------------------------------------------------
919 (defun desktop-remove ()
920 "Delete desktop file in `desktop-dirname'.
921 This function also sets `desktop-dirname' to nil."
923 (when desktop-dirname
924 (let ((filename (desktop-full-file-name)))
925 (setq desktop-dirname nil
)
926 (when (file-exists-p filename
)
927 (delete-file filename
)))))
929 (defvar desktop-buffer-args-list nil
930 "List of args for `desktop-create-buffer'.")
932 (defvar desktop-lazy-timer nil
)
934 ;; ----------------------------------------------------------------------------
936 (defun desktop-read (&optional dirname
)
937 "Read and process the desktop file in directory DIRNAME.
938 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
939 directories listed in `desktop-path'. If a desktop file is found, it
940 is processed and `desktop-after-read-hook' is run. If no desktop file
941 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
942 This function is a no-op when Emacs is running in batch mode.
943 It returns t if a desktop file was loaded, nil otherwise."
945 (unless noninteractive
946 (setq desktop-dirname
947 (file-name-as-directory
950 ;; If DIRNAME is specified, use it.
951 (and (< 0 (length dirname
)) dirname
)
952 ;; Otherwise search desktop file in desktop-path.
953 (let ((dirs desktop-path
))
956 (desktop-full-file-name (car dirs
)))))
957 (setq dirs
(cdr dirs
)))
958 (and dirs
(car dirs
)))
959 ;; If not found and `desktop-path' is non-nil, use its first element.
960 (and desktop-path
(car desktop-path
))
961 ;; Default: Home directory.
963 (if (file-exists-p (desktop-full-file-name))
964 ;; Desktop file found, but is it already in use?
965 (let ((desktop-first-buffer nil
)
966 (desktop-buffer-ok-count 0)
967 (desktop-buffer-fail-count 0)
968 (owner (desktop-owner))
969 ;; Avoid desktop saving during evaluation of desktop buffer.
972 (memq desktop-load-locked-desktop
'(nil ask
))
973 (or (null desktop-load-locked-desktop
)
974 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
975 Using it may cause conflicts. Use it anyway? " owner
)))))
976 (let ((default-directory desktop-dirname
))
977 (setq desktop-dirname nil
)
978 (run-hooks 'desktop-not-loaded-hook
)
979 (unless desktop-dirname
980 (message "Desktop file in use; not loaded.")))
982 ;; Evaluate desktop buffer and remember when it was modified.
983 (load (desktop-full-file-name) t t t
)
984 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name))))
985 ;; If it wasn't already, mark it as in-use, to bother other
986 ;; desktop instances.
990 (file-error (message "Couldn't record use of desktop file")
993 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
994 ;; We want buffers existing prior to evaluating the desktop (and
995 ;; not reused) to be placed at the end of the buffer list, so we
998 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
999 (switch-to-buffer (car (buffer-list)))
1000 (run-hooks 'desktop-delay-hook
)
1001 (setq desktop-delay-hook nil
)
1002 (run-hooks 'desktop-after-read-hook
)
1003 (message "Desktop: %d buffer%s restored%s%s."
1004 desktop-buffer-ok-count
1005 (if (= 1 desktop-buffer-ok-count
) "" "s")
1006 (if (< 0 desktop-buffer-fail-count
)
1007 (format ", %d failed to restore" desktop-buffer-fail-count
)
1009 (if desktop-buffer-args-list
1010 (format ", %d to restore lazily"
1011 (length desktop-buffer-args-list
))
1014 ;; No desktop file found.
1016 (let ((default-directory desktop-dirname
))
1017 (run-hooks 'desktop-no-desktop-file-hook
))
1018 (message "No desktop file.")
1021 ;; ----------------------------------------------------------------------------
1022 ;; Maintained for backward compatibility
1024 (defun desktop-load-default ()
1025 "Load the `default' start-up library manually.
1026 Also inhibit further loading of it."
1027 (unless inhibit-default-init
; safety check
1028 (load "default" t t
)
1029 (setq inhibit-default-init t
)))
1030 (make-obsolete 'desktop-load-default
1031 'desktop-save-mode
"22.1")
1033 ;; ----------------------------------------------------------------------------
1035 (defun desktop-change-dir (dirname)
1036 "Change to desktop saved in DIRNAME.
1037 Kill the desktop as specified by variables `desktop-save-mode' and
1038 `desktop-save', then clear the desktop and load the desktop file in
1040 (interactive "DChange to directory: ")
1041 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
1044 (desktop-read dirname
))
1046 ;; ----------------------------------------------------------------------------
1048 (defun desktop-save-in-desktop-dir ()
1049 "Save the desktop in directory `desktop-dirname'."
1052 (desktop-save desktop-dirname
)
1053 (call-interactively 'desktop-save
))
1054 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname
)))
1056 ;; ----------------------------------------------------------------------------
1058 (defun desktop-revert ()
1059 "Revert to the last loaded desktop."
1061 (unless desktop-dirname
1062 (error "Unknown desktop directory"))
1063 (unless (file-exists-p (desktop-full-file-name))
1064 (error "No desktop file found"))
1066 (desktop-read desktop-dirname
))
1068 (defvar desktop-buffer-major-mode
)
1069 (defvar desktop-buffer-locals
)
1070 ;; ----------------------------------------------------------------------------
1071 (defun desktop-restore-file-buffer (desktop-buffer-file-name
1073 desktop-buffer-misc
)
1074 "Restore a file buffer."
1075 (when desktop-buffer-file-name
1076 (if (or (file-exists-p desktop-buffer-file-name
)
1077 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1078 desktop-buffer-file-name
)))
1079 (if desktop-missing-file-warning
1080 (y-or-n-p (concat msg
" Re-create buffer? "))
1083 (let* ((auto-insert nil
) ; Disable auto insertion
1084 (coding-system-for-read
1085 (or coding-system-for-read
1086 (cdr (assq 'buffer-file-coding-system
1087 desktop-buffer-locals
))))
1088 (buf (find-file-noselect desktop-buffer-file-name
)))
1090 (switch-to-buffer buf
)
1091 (error (pop-to-buffer buf
)))
1092 (and (not (eq major-mode desktop-buffer-major-mode
))
1093 (functionp desktop-buffer-major-mode
)
1094 (funcall desktop-buffer-major-mode
))
1098 (defun desktop-load-file (function)
1099 "Load the file where auto loaded FUNCTION is defined."
1101 (let ((fcell (and (fboundp function
) (symbol-function function
))))
1102 (when (and (listp fcell
)
1103 (eq 'autoload
(car fcell
)))
1104 (load (cadr fcell
))))))
1106 ;; ----------------------------------------------------------------------------
1107 ;; Create a buffer, load its file, set its mode, ...;
1108 ;; called from Desktop file only.
1110 ;; Just to silence the byte compiler.
1112 (defvar desktop-first-buffer
) ; Dynamically bound in `desktop-read'
1114 ;; Bound locally in `desktop-read'.
1115 (defvar desktop-buffer-ok-count
)
1116 (defvar desktop-buffer-fail-count
)
1118 (defun desktop-create-buffer
1119 (desktop-file-version
1120 desktop-buffer-file-name
1122 desktop-buffer-major-mode
1123 desktop-buffer-minor-modes
1124 desktop-buffer-point
1126 desktop-buffer-read-only
1129 desktop-buffer-locals
)
1130 ;; To make desktop files with relative file names possible, we cannot
1131 ;; allow `default-directory' to change. Therefore we save current buffer.
1132 (save-current-buffer
1133 ;; Give major mode module a chance to add a handler.
1134 (desktop-load-file desktop-buffer-major-mode
)
1135 (let ((buffer-list (buffer-list))
1137 (condition-case-no-debug err
1138 (funcall (or (cdr (assq desktop-buffer-major-mode
1139 desktop-buffer-mode-handlers
))
1140 'desktop-restore-file-buffer
)
1141 desktop-buffer-file-name
1143 desktop-buffer-misc
)
1145 (message "Desktop: Can't load buffer %s: %s"
1147 (error-message-string err
))
1148 (when desktop-missing-file-warning
(sit-for 1))
1150 (if (bufferp result
)
1151 (setq desktop-buffer-ok-count
(1+ desktop-buffer-ok-count
))
1152 (setq desktop-buffer-fail-count
(1+ desktop-buffer-fail-count
))
1154 ;; Restore buffer list order with new buffer at end. Don't change
1155 ;; the order for old desktop files (old desktop module behaviour).
1156 (unless (< desktop-file-version
206)
1157 (mapc 'bury-buffer buffer-list
)
1158 (when result
(bury-buffer result
)))
1160 (unless (or desktop-first-buffer
(< desktop-file-version
206))
1161 (setq desktop-first-buffer result
))
1163 (unless (equal (buffer-name) desktop-buffer-name
)
1164 (rename-buffer desktop-buffer-name t
))
1166 (cond ((equal '(t) desktop-buffer-minor-modes
) ; backwards compatible
1168 ((equal '(nil) desktop-buffer-minor-modes
) ; backwards compatible
1171 (dolist (minor-mode desktop-buffer-minor-modes
)
1172 ;; Give minor mode module a chance to add a handler.
1173 (desktop-load-file minor-mode
)
1174 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers
))))
1176 (funcall handler desktop-buffer-locals
)
1177 (when (functionp minor-mode
)
1178 (funcall minor-mode
1)))))))
1179 ;; Even though point and mark are non-nil when written by
1180 ;; `desktop-save', they may be modified by handlers wanting to set
1181 ;; point or mark themselves.
1182 (when desktop-buffer-point
1185 ;; Evaluate point. Thus point can be something like
1186 ;; '(search-forward ...
1187 (eval desktop-buffer-point
)
1188 (error (message "%s" (error-message-string err
)) 1))))
1189 (when desktop-buffer-mark
1190 (if (consp desktop-buffer-mark
)
1192 (set-mark (car desktop-buffer-mark
))
1193 (setq mark-active
(car (cdr desktop-buffer-mark
))))
1194 (set-mark desktop-buffer-mark
)))
1195 ;; Never override file system if the file really is read-only marked.
1196 (when desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
1197 (while desktop-buffer-locals
1198 (let ((this (car desktop-buffer-locals
)))
1200 ;; an entry of this form `(symbol . value)'
1202 (make-local-variable (car this
))
1203 (set (car this
) (cdr this
)))
1204 ;; an entry of the form `symbol'
1205 (make-local-variable this
)
1207 (setq desktop-buffer-locals
(cdr desktop-buffer-locals
)))))))
1209 ;; ----------------------------------------------------------------------------
1210 ;; Backward compatibility -- update parameters to 205 standards.
1211 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1212 desktop-buffer-major-mode
1213 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
1214 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
1215 desktop-buffer-major-mode
(cdr mim
) pt mk ro
1217 (list (cons 'truncate-lines tl
)
1218 (cons 'fill-column fc
)
1219 (cons 'case-fold-search cfs
)
1220 (cons 'case-replace cr
)
1221 (cons 'overwrite-mode
(car mim
)))))
1223 (defun desktop-append-buffer-args (&rest args
)
1224 "Append ARGS at end of `desktop-buffer-args-list'.
1225 ARGS must be an argument list for `desktop-create-buffer'."
1226 (setq desktop-buffer-args-list
(nconc desktop-buffer-args-list
(list args
)))
1227 (unless desktop-lazy-timer
1228 (setq desktop-lazy-timer
1229 (run-with-idle-timer desktop-lazy-idle-delay t
'desktop-idle-create-buffers
))))
1231 (defun desktop-lazy-create-buffer ()
1232 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1233 (when desktop-buffer-args-list
1234 (let* ((remaining (length desktop-buffer-args-list
))
1235 (args (pop desktop-buffer-args-list
))
1236 (buffer-name (nth 2 args
))
1237 (msg (format "Desktop lazily opening %s (%s remaining)..."
1238 buffer-name remaining
)))
1239 (when desktop-lazy-verbose
1241 (let ((desktop-first-buffer nil
)
1242 (desktop-buffer-ok-count 0)
1243 (desktop-buffer-fail-count 0))
1244 (apply 'desktop-create-buffer args
)
1245 (run-hooks 'desktop-delay-hook
)
1246 (setq desktop-delay-hook nil
)
1247 (bury-buffer (get-buffer buffer-name
))
1248 (when desktop-lazy-verbose
1249 (message "%s%s" msg
(if (> desktop-buffer-ok-count
0) "done" "failed")))))))
1251 (defun desktop-idle-create-buffers ()
1252 "Create buffers until the user does something, then stop.
1253 If there are no buffers left to create, kill the timer."
1255 (while (and repeat desktop-buffer-args-list
)
1256 (save-window-excursion
1257 (desktop-lazy-create-buffer))
1258 (setq repeat
(sit-for 0.2))
1259 (unless desktop-buffer-args-list
1260 (cancel-timer desktop-lazy-timer
)
1261 (setq desktop-lazy-timer nil
)
1262 (message "Lazy desktop load complete")
1266 (defun desktop-lazy-complete ()
1267 "Run the desktop load to completion."
1269 (let ((desktop-lazy-verbose t
))
1270 (while desktop-buffer-args-list
1271 (save-window-excursion
1272 (desktop-lazy-create-buffer)))
1273 (message "Lazy desktop load complete")))
1275 (defun desktop-lazy-abort ()
1276 "Abort lazy loading of the desktop."
1278 (when desktop-lazy-timer
1279 (cancel-timer desktop-lazy-timer
)
1280 (setq desktop-lazy-timer nil
))
1281 (when desktop-buffer-args-list
1282 (setq desktop-buffer-args-list nil
)
1283 (when (called-interactively-p 'interactive
)
1284 (message "Lazy desktop load aborted"))))
1286 ;; ----------------------------------------------------------------------------
1287 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1288 ;; command line, we do the rest of what it takes to use desktop, but do it
1289 ;; after finishing loading the init file.
1290 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1291 ;; functions are processed after `after-init-hook'.
1295 (let ((key "--no-desktop"))
1296 (when (member key command-line-args
)
1297 (setq command-line-args
(delete key command-line-args
))
1298 (setq desktop-save-mode nil
)))
1299 (when desktop-save-mode
1301 (setq inhibit-startup-screen t
))))
1305 ;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1306 ;;; desktop.el ends here