1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001, 2002, 2003,
4 ;; 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 ;; Free Software Foundation, Inc.
7 ;; Author: Morten Welinder <terra@diku.dk>
8 ;; Keywords: convenience
9 ;; Favourite-brand-of-beer: None, I hate beer.
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
28 ;; Save the Desktop, i.e.,
29 ;; - some global variables
30 ;; - the list of buffers with associated files. For each buffer also
32 ;; - the default directory
34 ;; - the mark & mark-active
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.
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
76 ;; (add-to-list 'desktop-buffer-mode-handlers
77 ;; '(foo-mode . foo-restore-desktop-buffer))
81 ;; (defun bar-desktop-restore
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.
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 ;; ---------------------------------------------------------------------------
131 ;; Save window configuration.
132 ;; Recognize more minor modes.
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."
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."
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.
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.
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."
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
))
192 (defcustom desktop-load-locked-desktop
'ask
193 "Specifies whether the desktop should be loaded if locked.
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."
202 (const :tag
"Load anyway" t
)
203 (const :tag
"Don't load" nil
)
204 (const :tag
"Ask the user" ask
))
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."
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."
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
)
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."
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."
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."
255 :options
'(desktop-save-mode-off save-buffers-kill-emacs
)
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."
263 :options
'(list-buffers)
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,
274 (defcustom desktop-globals-to-save
275 '(desktop-missing-file-warning
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
)))
290 (defcustom desktop-globals-to-clear
292 kill-ring-yank-pointer
294 search-ring-yank-pointer
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
)))
305 (defcustom desktop-clear-preserve-buffers
306 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*"
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
)
316 (defcustom desktop-locals-to-save
317 '(desktop-locals-to-save ; Itself! Think it over.
323 change-log-default-name
327 buffer-file-coding-system
330 indicate-buffer-boundaries
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
)
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
)
343 :version
"23.2" ; set to nil
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
)
354 ;; We skip TAGS files to save time (tags-file-name is saved instead).
355 (defcustom desktop-modes-not-to-save
357 "List of major modes whose buffers should not be saved."
358 :type
'(repeat symbol
)
361 (defcustom desktop-file-name-format
'absolute
362 "Format in which desktop file names should be saved.
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
))
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
)
379 (defcustom desktop-lazy-verbose t
380 "Verbose reporting of lazily created buffers."
385 (defcustom desktop-lazy-idle-delay
5
386 "Idle delay before starting to create buffers.
387 See `desktop-restore-eager'."
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")
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:
432 desktop-buffer-major-mode
433 desktop-buffer-minor-modes
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
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.")
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
)
461 (erc-track-minor-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'."
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
486 Handlers are called with argument list
488 (DESKTOP-BUFFER-LOCALS)
490 Furthermore, they may use the following variables:
493 desktop-buffer-file-name
495 desktop-buffer-major-mode
496 desktop-buffer-minor-modes
499 desktop-buffer-read-only
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
506 Modules that define a minor mode that needs a special handler should contain
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'.")
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'."
555 (and (file-exists-p (desktop-full-lock-name dirname
))
558 (insert-file-contents-literally (desktop-full-lock-name dirname
))
559 (goto-char (point-min))
560 (setq owner
(read (current-buffer)))
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
)))
584 ;; ----------------------------------------------------------------------------
586 (defun desktop-clear ()
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'."
593 (dolist (var desktop-globals-to-clear
)
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
605 (let ((bufname (buffer-name (car buffers
))))
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
(eq desktop-save
'if-exists
))
626 ;; If it exists, but we aren't using it, we are going
627 ;; to ask for a new directory below.
628 (and exists desktop-dirname
(eq desktop-save
'ask-if-new
))
630 (or (memq desktop-save
'(ask ask-if-new
))
631 (and exists
(eq desktop-save
'ask-if-exists
)))
632 (y-or-n-p "Save desktop? ")))))
633 (unless desktop-dirname
634 (setq desktop-dirname
635 (file-name-as-directory
637 (read-directory-name "Directory for desktop file: " nil nil t
)))))
639 (desktop-save desktop-dirname t
)
641 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
642 (signal (car err
) (cdr err
))))))
643 ;; If we own it, we don't anymore.
644 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
646 ;; ----------------------------------------------------------------------------
647 (defun desktop-list* (&rest args
)
648 (if (null (cdr args
))
650 (setq args
(nreverse args
))
651 (let ((value (cons (nth 1 args
) (car args
))))
652 (setq args
(cdr (cdr args
)))
654 (setq value
(cons (car args
) value
))
655 (setq args
(cdr args
)))
658 ;; ----------------------------------------------------------------------------
659 (defun desktop-buffer-info (buffer)
662 ;; base name of the buffer; replaces the buffer name if managed by uniquify
663 (and (fboundp 'uniquify-buffer-base-name
) (uniquify-buffer-base-name))
665 (desktop-file-name (buffer-file-name) desktop-dirname
)
671 #'(lambda (minor-mode)
672 (and (boundp minor-mode
)
673 (symbol-value minor-mode
)
674 (let* ((special (assq minor-mode desktop-minor-mode-table
))
675 (value (cond (special (cadr special
))
676 ((functionp minor-mode
) minor-mode
))))
677 (when value
(add-to-list 'ret value
)))))
678 (mapcar #'car minor-mode-alist
))
680 ;; point and mark, and read-only status
682 (list (mark t
) mark-active
)
684 ;; auxiliary information
685 (when (functionp desktop-save-buffer
)
686 (funcall desktop-save-buffer desktop-dirname
))
688 (let ((locals desktop-locals-to-save
)
689 (loclist (buffer-local-variables))
692 (let ((here (assq (car locals
) loclist
)))
694 (setq ll
(cons here ll
))
695 (when (member (car locals
) loclist
)
696 (setq ll
(cons (car locals
) ll
)))))
697 (setq locals
(cdr locals
)))
700 ;; ----------------------------------------------------------------------------
701 (defun desktop-internal-v2s (value)
702 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
703 TXT is a string that when read and evaluated yields value.
704 QUOTE may be `may' (value may be quoted),
705 `must' (values must be quoted), or nil (value may not be quoted)."
707 ((or (numberp value
) (null value
) (eq t value
) (keywordp value
))
708 (cons 'may
(prin1-to-string value
)))
710 (let ((copy (copy-sequence value
)))
711 (set-text-properties 0 (length copy
) nil copy
)
712 ;; Get rid of text properties because we cannot read them
713 (cons 'may
(prin1-to-string copy
))))
715 (cons 'must
(prin1-to-string value
)))
720 (let ((res (desktop-internal-v2s el
)))
726 (cons nil
(concat "(vector "
727 (mapconcat (lambda (el)
728 (if (eq (car el
) 'must
)
729 (concat "'" (cdr el
))
734 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
741 (let ((q.txt
(desktop-internal-v2s (car p
))))
742 (or anynil
(setq anynil
(null (car q.txt
))))
743 (setq newlist
(cons q.txt newlist
)))
746 (let ((last (desktop-internal-v2s p
)))
747 (or anynil
(setq anynil
(null (car last
))))
749 (setq newlist
(cons '(must .
".") newlist
)))
751 (setq newlist
(cons last newlist
))))
752 (setq newlist
(nreverse newlist
))
755 (concat (if use-list
* "(desktop-list* " "(list ")
756 (mapconcat (lambda (el)
757 (if (eq (car el
) 'must
)
758 (concat "'" (cdr el
))
764 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
766 (cons nil
(concat "(symbol-function '"
767 (substring (prin1-to-string value
) 7 -
1)
770 (let ((pos (prin1-to-string (marker-position value
)))
771 (buf (prin1-to-string (buffer-name (marker-buffer value
)))))
772 (cons nil
(concat "(let ((mk (make-marker)))"
773 " (add-hook 'desktop-delay-hook"
774 " (list 'lambda '() (list 'set-marker mk "
775 pos
" (get-buffer " buf
")))) mk)"))))
777 (cons 'may
"\"Unprintable entity\""))))
779 ;; ----------------------------------------------------------------------------
780 (defun desktop-value-to-string (value)
781 "Convert VALUE to a string that when read evaluates to the same value.
782 Not all types of values are supported."
783 (let* ((print-escape-newlines t
)
784 (float-output-format nil
)
785 (quote.txt
(desktop-internal-v2s value
))
786 (quote (car quote.txt
))
787 (txt (cdr quote.txt
)))
792 ;; ----------------------------------------------------------------------------
793 (defun desktop-outvar (varspec)
794 "Output a setq statement for variable VAR to the desktop file.
795 The argument VARSPEC may be the variable name VAR (a symbol),
796 or a cons cell of the form (VAR . MAX-SIZE),
797 which means to truncate VAR's value to at most MAX-SIZE elements
798 \(if the value is a list) before saving the value."
801 (setq var
(car varspec
) size
(cdr varspec
))
804 (when (and (integerp size
)
807 (desktop-truncate (eval var
) size
))
811 (desktop-value-to-string (symbol-value var
))
814 ;; ----------------------------------------------------------------------------
815 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
816 "Return t if buffer should have its state saved in the desktop file.
817 FILENAME is the visited file name, BUFNAME is the buffer name, and
818 MODE is the major mode.
819 \n\(fn FILENAME BUFNAME MODE)"
820 (let ((case-fold-search nil
)
822 (and (not (and (stringp desktop-buffers-not-to-save
)
824 (string-match desktop-buffers-not-to-save bufname
)))
825 (not (memq mode desktop-modes-not-to-save
))
826 ;; FIXME this is broken if desktop-files-not-to-save is nil.
828 (stringp desktop-files-not-to-save
)
829 (not (string-match desktop-files-not-to-save filename
)))
830 (and (eq mode
'dired-mode
)
831 (with-current-buffer bufname
832 (not (setq dired-skip
833 (string-match desktop-files-not-to-save
834 default-directory
)))))
836 (null dired-skip
) ; bug#5755
837 (with-current-buffer bufname desktop-save-buffer
))))))
839 ;; ----------------------------------------------------------------------------
840 (defun desktop-file-name (filename dirname
)
841 "Convert FILENAME to format specified in `desktop-file-name-format'.
842 DIRNAME must be the directory in which the desktop file will be saved."
845 ((eq desktop-file-name-format
'tilde
)
846 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
848 ((file-name-absolute-p relative-name
) relative-name
)
849 ((string= "./" relative-name
) "~/")
850 ((string= "." relative-name
) "~")
851 (t (concat "~/" relative-name
)))))
852 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
853 (t (expand-file-name filename
))))
856 ;; ----------------------------------------------------------------------------
858 (defun desktop-save (dirname &optional release
)
859 "Save the desktop in a desktop file.
860 Parameter DIRNAME specifies where to save the desktop file.
861 Optional parameter RELEASE says whether we're done with this desktop.
862 See also `desktop-base-file-name'."
863 (interactive "DDirectory to save desktop file in: ")
864 (setq desktop-dirname
(file-name-as-directory (expand-file-name dirname
)))
866 (let ((eager desktop-restore-eager
)
867 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
869 (or (not new-modtime
) ; nothing to overwrite
870 (equal desktop-file-modtime new-modtime
)
871 (yes-or-no-p (if desktop-file-modtime
872 (if (> (float-time new-modtime
) (float-time desktop-file-modtime
))
873 "Desktop file is more recent than the one loaded. Save anyway? "
874 "Desktop file isn't the one loaded. Overwrite it? ")
875 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
876 (unless release
(error "Desktop file conflict")))
878 ;; If we're done with it, release the lock.
879 ;; Otherwise, claim it if it's unclaimed or if we created it.
881 (desktop-release-lock)
882 (unless (and new-modtime
(desktop-owner)) (desktop-claim-lock)))
886 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
888 ";; Created " (current-time-string) "\n"
889 ";; Desktop file format version " desktop-file-version
"\n"
890 ";; Emacs version " emacs-version
"\n")
891 (save-excursion (run-hooks 'desktop-save-hook
))
892 (goto-char (point-max))
893 (insert "\n;; Global section:\n")
894 (mapc (function desktop-outvar
) desktop-globals-to-save
)
895 (when (memq 'kill-ring desktop-globals-to-save
)
897 "(setq kill-ring-yank-pointer (nthcdr "
898 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
901 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
902 (dolist (l (mapcar 'desktop-buffer-info
(buffer-list)))
903 (let ((base (pop l
)))
904 (when (apply 'desktop-save-buffer-p l
)
906 (if (or (not (integerp eager
))
909 (setq eager
(1- eager
))))
910 "desktop-create-buffer"
911 "desktop-append-buffer-args")
913 desktop-file-version
)
914 ;; If there's a non-empty base name, we save it instead of the buffer name
915 (when (and base
(not (string= base
"")))
916 (setcar (nthcdr 1 l
) base
))
918 (insert "\n " (desktop-value-to-string e
)))
921 (setq default-directory desktop-dirname
)
922 (let ((coding-system-for-write 'emacs-mule
))
923 (write-region (point-min) (point-max) (desktop-full-file-name) nil
'nomessage
))
924 ;; We remember when it was modified (which is presumably just now).
925 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name)))))))))
927 ;; ----------------------------------------------------------------------------
929 (defun desktop-remove ()
930 "Delete desktop file in `desktop-dirname'.
931 This function also sets `desktop-dirname' to nil."
933 (when desktop-dirname
934 (let ((filename (desktop-full-file-name)))
935 (setq desktop-dirname nil
)
936 (when (file-exists-p filename
)
937 (delete-file filename
)))))
939 (defvar desktop-buffer-args-list nil
940 "List of args for `desktop-create-buffer'.")
942 (defvar desktop-lazy-timer nil
)
944 ;; ----------------------------------------------------------------------------
946 (defun desktop-read (&optional dirname
)
947 "Read and process the desktop file in directory DIRNAME.
948 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
949 directories listed in `desktop-path'. If a desktop file is found, it
950 is processed and `desktop-after-read-hook' is run. If no desktop file
951 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
952 This function is a no-op when Emacs is running in batch mode.
953 It returns t if a desktop file was loaded, nil otherwise."
955 (unless noninteractive
956 (setq desktop-dirname
957 (file-name-as-directory
960 ;; If DIRNAME is specified, use it.
961 (and (< 0 (length dirname
)) dirname
)
962 ;; Otherwise search desktop file in desktop-path.
963 (let ((dirs desktop-path
))
966 (desktop-full-file-name (car dirs
)))))
967 (setq dirs
(cdr dirs
)))
968 (and dirs
(car dirs
)))
969 ;; If not found and `desktop-path' is non-nil, use its first element.
970 (and desktop-path
(car desktop-path
))
971 ;; Default: Home directory.
973 (if (file-exists-p (desktop-full-file-name))
974 ;; Desktop file found, but is it already in use?
975 (let ((desktop-first-buffer nil
)
976 (desktop-buffer-ok-count 0)
977 (desktop-buffer-fail-count 0)
978 (owner (desktop-owner))
979 ;; Avoid desktop saving during evaluation of desktop buffer.
982 (memq desktop-load-locked-desktop
'(nil ask
))
983 (or (null desktop-load-locked-desktop
)
984 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
985 Using it may cause conflicts. Use it anyway? " owner
)))))
986 (let ((default-directory desktop-dirname
))
987 (setq desktop-dirname nil
)
988 (run-hooks 'desktop-not-loaded-hook
)
989 (unless desktop-dirname
990 (message "Desktop file in use; not loaded.")))
992 ;; Evaluate desktop buffer and remember when it was modified.
993 (load (desktop-full-file-name) t t t
)
994 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name))))
995 ;; If it wasn't already, mark it as in-use, to bother other
996 ;; desktop instances.
1000 (file-error (message "Couldn't record use of desktop file")
1003 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1004 ;; We want buffers existing prior to evaluating the desktop (and
1005 ;; not reused) to be placed at the end of the buffer list, so we
1008 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
1009 (switch-to-buffer (car (buffer-list)))
1010 (run-hooks 'desktop-delay-hook
)
1011 (setq desktop-delay-hook nil
)
1012 (run-hooks 'desktop-after-read-hook
)
1013 (message "Desktop: %d buffer%s restored%s%s."
1014 desktop-buffer-ok-count
1015 (if (= 1 desktop-buffer-ok-count
) "" "s")
1016 (if (< 0 desktop-buffer-fail-count
)
1017 (format ", %d failed to restore" desktop-buffer-fail-count
)
1019 (if desktop-buffer-args-list
1020 (format ", %d to restore lazily"
1021 (length desktop-buffer-args-list
))
1024 ;; No desktop file found.
1026 (let ((default-directory desktop-dirname
))
1027 (run-hooks 'desktop-no-desktop-file-hook
))
1028 (message "No desktop file.")
1031 ;; ----------------------------------------------------------------------------
1032 ;; Maintained for backward compatibility
1034 (defun desktop-load-default ()
1035 "Load the `default' start-up library manually.
1036 Also inhibit further loading of it."
1037 (unless inhibit-default-init
; safety check
1038 (load "default" t t
)
1039 (setq inhibit-default-init t
)))
1040 (make-obsolete 'desktop-load-default
1041 'desktop-save-mode
"22.1")
1043 ;; ----------------------------------------------------------------------------
1045 (defun desktop-change-dir (dirname)
1046 "Change to desktop saved in DIRNAME.
1047 Kill the desktop as specified by variables `desktop-save-mode' and
1048 `desktop-save', then clear the desktop and load the desktop file in
1050 (interactive "DChange to directory: ")
1051 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
1054 (desktop-read dirname
))
1056 ;; ----------------------------------------------------------------------------
1058 (defun desktop-save-in-desktop-dir ()
1059 "Save the desktop in directory `desktop-dirname'."
1062 (desktop-save desktop-dirname
)
1063 (call-interactively 'desktop-save
))
1064 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname
)))
1066 ;; ----------------------------------------------------------------------------
1068 (defun desktop-revert ()
1069 "Revert to the last loaded desktop."
1071 (unless desktop-dirname
1072 (error "Unknown desktop directory"))
1073 (unless (file-exists-p (desktop-full-file-name))
1074 (error "No desktop file found"))
1076 (desktop-read desktop-dirname
))
1078 (defvar desktop-buffer-major-mode
)
1079 (defvar desktop-buffer-locals
)
1080 ;; ----------------------------------------------------------------------------
1081 (defun desktop-restore-file-buffer (desktop-buffer-file-name
1083 desktop-buffer-misc
)
1084 "Restore a file buffer."
1085 (when desktop-buffer-file-name
1086 (if (or (file-exists-p desktop-buffer-file-name
)
1087 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1088 desktop-buffer-file-name
)))
1089 (if desktop-missing-file-warning
1090 (y-or-n-p (concat msg
" Re-create buffer? "))
1093 (let* ((auto-insert nil
) ; Disable auto insertion
1094 (coding-system-for-read
1095 (or coding-system-for-read
1096 (cdr (assq 'buffer-file-coding-system
1097 desktop-buffer-locals
))))
1098 (buf (find-file-noselect desktop-buffer-file-name
)))
1100 (switch-to-buffer buf
)
1101 (error (pop-to-buffer buf
)))
1102 (and (not (eq major-mode desktop-buffer-major-mode
))
1103 (functionp desktop-buffer-major-mode
)
1104 (funcall desktop-buffer-major-mode
))
1108 (defun desktop-load-file (function)
1109 "Load the file where auto loaded FUNCTION is defined."
1111 (let ((fcell (and (fboundp function
) (symbol-function function
))))
1112 (when (and (listp fcell
)
1113 (eq 'autoload
(car fcell
)))
1114 (load (cadr fcell
))))))
1116 ;; ----------------------------------------------------------------------------
1117 ;; Create a buffer, load its file, set its mode, ...;
1118 ;; called from Desktop file only.
1120 ;; Just to silence the byte compiler.
1122 (defvar desktop-first-buffer
) ; Dynamically bound in `desktop-read'
1124 ;; Bound locally in `desktop-read'.
1125 (defvar desktop-buffer-ok-count
)
1126 (defvar desktop-buffer-fail-count
)
1128 (defun desktop-create-buffer
1129 (desktop-file-version
1130 desktop-buffer-file-name
1132 desktop-buffer-major-mode
1133 desktop-buffer-minor-modes
1134 desktop-buffer-point
1136 desktop-buffer-read-only
1139 desktop-buffer-locals
)
1140 ;; To make desktop files with relative file names possible, we cannot
1141 ;; allow `default-directory' to change. Therefore we save current buffer.
1142 (save-current-buffer
1143 ;; Give major mode module a chance to add a handler.
1144 (desktop-load-file desktop-buffer-major-mode
)
1145 (let ((buffer-list (buffer-list))
1147 (condition-case-no-debug err
1148 (funcall (or (cdr (assq desktop-buffer-major-mode
1149 desktop-buffer-mode-handlers
))
1150 'desktop-restore-file-buffer
)
1151 desktop-buffer-file-name
1153 desktop-buffer-misc
)
1155 (message "Desktop: Can't load buffer %s: %s"
1157 (error-message-string err
))
1158 (when desktop-missing-file-warning
(sit-for 1))
1160 (if (bufferp result
)
1161 (setq desktop-buffer-ok-count
(1+ desktop-buffer-ok-count
))
1162 (setq desktop-buffer-fail-count
(1+ desktop-buffer-fail-count
))
1164 ;; Restore buffer list order with new buffer at end. Don't change
1165 ;; the order for old desktop files (old desktop module behaviour).
1166 (unless (< desktop-file-version
206)
1167 (mapc 'bury-buffer buffer-list
)
1168 (when result
(bury-buffer result
)))
1170 (unless (or desktop-first-buffer
(< desktop-file-version
206))
1171 (setq desktop-first-buffer result
))
1173 (unless (equal (buffer-name) desktop-buffer-name
)
1174 (rename-buffer desktop-buffer-name t
))
1176 (cond ((equal '(t) desktop-buffer-minor-modes
) ; backwards compatible
1178 ((equal '(nil) desktop-buffer-minor-modes
) ; backwards compatible
1181 (dolist (minor-mode desktop-buffer-minor-modes
)
1182 ;; Give minor mode module a chance to add a handler.
1183 (desktop-load-file minor-mode
)
1184 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers
))))
1186 (funcall handler desktop-buffer-locals
)
1187 (when (functionp minor-mode
)
1188 (funcall minor-mode
1)))))))
1189 ;; Even though point and mark are non-nil when written by
1190 ;; `desktop-save', they may be modified by handlers wanting to set
1191 ;; point or mark themselves.
1192 (when desktop-buffer-point
1195 ;; Evaluate point. Thus point can be something like
1196 ;; '(search-forward ...
1197 (eval desktop-buffer-point
)
1198 (error (message "%s" (error-message-string err
)) 1))))
1199 (when desktop-buffer-mark
1200 (if (consp desktop-buffer-mark
)
1202 (set-mark (car desktop-buffer-mark
))
1203 (setq mark-active
(car (cdr desktop-buffer-mark
))))
1204 (set-mark desktop-buffer-mark
)))
1205 ;; Never override file system if the file really is read-only marked.
1206 (when desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
1207 (while desktop-buffer-locals
1208 (let ((this (car desktop-buffer-locals
)))
1210 ;; an entry of this form `(symbol . value)'
1212 (make-local-variable (car this
))
1213 (set (car this
) (cdr this
)))
1214 ;; an entry of the form `symbol'
1215 (make-local-variable this
)
1217 (setq desktop-buffer-locals
(cdr desktop-buffer-locals
)))))))
1219 ;; ----------------------------------------------------------------------------
1220 ;; Backward compatibility -- update parameters to 205 standards.
1221 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1222 desktop-buffer-major-mode
1223 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
1224 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
1225 desktop-buffer-major-mode
(cdr mim
) pt mk ro
1227 (list (cons 'truncate-lines tl
)
1228 (cons 'fill-column fc
)
1229 (cons 'case-fold-search cfs
)
1230 (cons 'case-replace cr
)
1231 (cons 'overwrite-mode
(car mim
)))))
1233 (defun desktop-append-buffer-args (&rest args
)
1234 "Append ARGS at end of `desktop-buffer-args-list'.
1235 ARGS must be an argument list for `desktop-create-buffer'."
1236 (setq desktop-buffer-args-list
(nconc desktop-buffer-args-list
(list args
)))
1237 (unless desktop-lazy-timer
1238 (setq desktop-lazy-timer
1239 (run-with-idle-timer desktop-lazy-idle-delay t
'desktop-idle-create-buffers
))))
1241 (defun desktop-lazy-create-buffer ()
1242 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1243 (when desktop-buffer-args-list
1244 (let* ((remaining (length desktop-buffer-args-list
))
1245 (args (pop desktop-buffer-args-list
))
1246 (buffer-name (nth 2 args
))
1247 (msg (format "Desktop lazily opening %s (%s remaining)..."
1248 buffer-name remaining
)))
1249 (when desktop-lazy-verbose
1251 (let ((desktop-first-buffer nil
)
1252 (desktop-buffer-ok-count 0)
1253 (desktop-buffer-fail-count 0))
1254 (apply 'desktop-create-buffer args
)
1255 (run-hooks 'desktop-delay-hook
)
1256 (setq desktop-delay-hook nil
)
1257 (bury-buffer (get-buffer buffer-name
))
1258 (when desktop-lazy-verbose
1259 (message "%s%s" msg
(if (> desktop-buffer-ok-count
0) "done" "failed")))))))
1261 (defun desktop-idle-create-buffers ()
1262 "Create buffers until the user does something, then stop.
1263 If there are no buffers left to create, kill the timer."
1265 (while (and repeat desktop-buffer-args-list
)
1266 (save-window-excursion
1267 (desktop-lazy-create-buffer))
1268 (setq repeat
(sit-for 0.2))
1269 (unless desktop-buffer-args-list
1270 (cancel-timer desktop-lazy-timer
)
1271 (setq desktop-lazy-timer nil
)
1272 (message "Lazy desktop load complete")
1276 (defun desktop-lazy-complete ()
1277 "Run the desktop load to completion."
1279 (let ((desktop-lazy-verbose t
))
1280 (while desktop-buffer-args-list
1281 (save-window-excursion
1282 (desktop-lazy-create-buffer)))
1283 (message "Lazy desktop load complete")))
1285 (defun desktop-lazy-abort ()
1286 "Abort lazy loading of the desktop."
1288 (when desktop-lazy-timer
1289 (cancel-timer desktop-lazy-timer
)
1290 (setq desktop-lazy-timer nil
))
1291 (when desktop-buffer-args-list
1292 (setq desktop-buffer-args-list nil
)
1293 (when (called-interactively-p 'interactive
)
1294 (message "Lazy desktop load aborted"))))
1296 ;; ----------------------------------------------------------------------------
1297 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1298 ;; command line, we do the rest of what it takes to use desktop, but do it
1299 ;; after finishing loading the init file.
1300 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1301 ;; functions are processed after `after-init-hook'.
1305 (let ((key "--no-desktop"))
1306 (when (member key command-line-args
)
1307 (setq command-line-args
(delete key command-line-args
))
1308 (setq desktop-save-mode nil
)))
1309 (when desktop-save-mode
1311 (setq inhibit-startup-screen t
))))
1315 ;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1316 ;;; desktop.el ends here