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 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, or (at your option)
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; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
25 ;; Boston, MA 02110-1301, USA.
29 ;; Save the Desktop, i.e.,
30 ;; - some global variables
31 ;; - the list of buffers with associated files. For each buffer also
33 ;; - the default directory
35 ;; - the mark & mark-active
37 ;; - some local variables
39 ;; To use this, use customize to turn on desktop-save-mode or add the
40 ;; following line somewhere in your .emacs file:
42 ;; (desktop-save-mode 1)
44 ;; For further usage information, look at the section
45 ;; "Saving Emacs Sessions" in the GNU Emacs Manual.
47 ;; When the desktop module is loaded, the function `desktop-kill' is
48 ;; added to the `kill-emacs-hook'. This function is responsible for
49 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
50 ;; function is added to the `after-init-hook'. This function is
51 ;; responsible for loading the desktop when Emacs is started.
55 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
56 ;; are supplied to handle special major and minor modes respectively.
57 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
58 ;; to restore a desktop buffer. Elements must have the form
60 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
62 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
63 ;; evaluates the desktop file. Buffers with a major mode not specified here,
64 ;; are restored by the default handler `desktop-restore-file-buffer'.
65 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
66 ;; non-standard minor modes. Elements must have the form
68 ;; (MINOR-MODE . RESTORE-FUNCTION).
70 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
71 ;; Minor modes not specified here, are restored by the standard minor mode
72 ;; function. If you write a module that defines a major or minor mode that
73 ;; needs a special handler, then place code like
75 ;; (defun foo-restore-desktop-buffer
77 ;; (add-to-list 'desktop-buffer-mode-handlers
78 ;; '(foo-mode . foo-restore-desktop-buffer))
82 ;; (defun bar-desktop-restore
84 ;; (add-to-list 'desktop-minor-mode-handlers
85 ;; '(bar-mode . bar-desktop-restore))
87 ;; in the module itself, and make shure that the mode function is
88 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
89 ;; `desktop-minor-mode-handlers' for more info.
93 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
94 ;; manual) are handled in the following way:
95 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
96 ;; saves as `desktop-minor-modes' the list of names of those variables in
97 ;; `minor-mode-alist' that have a non-nil value.
98 ;; When `desktop-create' restores the buffer, each of the symbols in
99 ;; `desktop-minor-modes' is called as function with parameter 1.
100 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
101 ;; are used to handle non-conventional minor modes. `desktop-save' uses
102 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
103 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
104 ;; variable name that is different form its function name, an entry
106 ;; (NAME RESTORE-FUNCTION)
108 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
109 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
110 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
111 ;; function different from the usual minor mode function.
112 ;; ---------------------------------------------------------------------------
114 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
115 ;; in your home directory is used for that. Saving global default values
116 ;; for buffers is an example of misuse.
118 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
119 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
120 ;; things you did not mean to keep. Use M-x desktop-clear RET.
122 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
123 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
124 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
125 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
126 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
127 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
128 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
129 ;; ---------------------------------------------------------------------------
132 ;; Save window configuration.
133 ;; Recognize more minor modes.
138 (defvar desktop-file-version
"206"
139 "Version number of desktop file format.
140 Written into the desktop file and used at desktop read to provide
141 backward compatibility.")
143 ;; ----------------------------------------------------------------------------
144 ;; USER OPTIONS -- settings you might want to play with.
145 ;; ----------------------------------------------------------------------------
147 (defgroup desktop nil
148 "Save status of Emacs when you exit."
152 (define-minor-mode desktop-save-mode
153 "Toggle desktop saving mode.
154 With numeric ARG, turn desktop saving on if ARG is positive, off
155 otherwise. If desktop saving is turned on, the state of Emacs is
156 saved from one session to another. See variable `desktop-save'
157 and function `desktop-read' for details."
161 ;; Maintained for backward compatibility
162 (define-obsolete-variable-alias 'desktop-enable
163 'desktop-save-mode
"22.1")
165 (defun desktop-save-mode-off ()
166 "Disable `desktop-save-mode'. Provided for use in hooks."
167 (desktop-save-mode 0))
169 (defcustom desktop-save
'ask-if-new
170 "*Specifies whether the desktop should be saved when it is killed.
171 A desktop is killed when the user changes desktop or quits Emacs.
175 ask-if-new -- ask if no desktop file exists, otherwise just save.
176 ask-if-exists -- ask if desktop file exists, otherwise don't save.
177 if-exists -- save if desktop file exists, otherwise don't save.
179 The desktop is never saved when `desktop-save-mode' is nil.
180 The variables `desktop-dirname' and `desktop-base-file-name'
181 determine where the desktop is saved."
184 (const :tag
"Always save" t
)
185 (const :tag
"Always ask" ask
)
186 (const :tag
"Ask if desktop file is new, else do save" ask-if-new
)
187 (const :tag
"Ask if desktop file exists, else don't save" ask-if-exists
)
188 (const :tag
"Save if desktop file exists, else don't" if-exists
)
189 (const :tag
"Never save" nil
))
193 (defcustom desktop-load-locked-desktop
'ask
194 "Specifies whether the desktop should be loaded if locked.
199 If the value is nil, or `ask' and the user chooses not to load the desktop,
200 the normal hook `desktop-not-loaded-hook' is run."
203 (const :tag
"Load anyway" t
)
204 (const :tag
"Don't load" nil
)
205 (const :tag
"Ask the user" ask
))
209 (defcustom desktop-base-file-name
210 (convert-standard-filename ".emacs.desktop")
211 "Name of file for Emacs desktop, excluding the directory part."
214 (define-obsolete-variable-alias 'desktop-basefilename
215 'desktop-base-file-name
"22.1")
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
'("." "~")
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
)
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
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 ;; We skip .log files because they are normally temporary.
337 ;; (ftp) files because they require passwords and whatnot.
338 (defcustom desktop-buffers-not-to-save
339 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\)$"
340 "Regexp identifying buffers that are to be excluded from saving."
344 ;; Skip tramp and ange-ftp files
345 (defcustom desktop-files-not-to-save
347 "Regexp identifying files whose buffers are to be excluded from saving."
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
)
459 "Table mapping minor mode variables to minor mode functions.
460 Each entry has the form (NAME RESTORE-FUNCTION).
461 NAME is the name of the buffer-local variable indicating that the minor
462 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
463 RESTORE-FUNCTION nil means don't try to restore the minor mode.
464 Only minor modes for which the name of the buffer-local variable
465 and the name of the minor mode function are different have to be added to
466 this table. See also `desktop-minor-mode-handlers'."
471 (defvar desktop-minor-mode-handlers
473 "Alist of functions to restore non-standard minor modes.
474 Functions are called by `desktop-create-buffer' to restore minor modes.
475 List elements must have the form
477 (MINOR-MODE . RESTORE-FUNCTION).
479 Minor modes not specified here, are restored by the standard minor mode
482 Handlers are called with argument list
484 (DESKTOP-BUFFER-LOCALS)
486 Furthermore, they may use the following variables:
489 desktop-buffer-file-name
491 desktop-buffer-major-mode
492 desktop-buffer-minor-modes
495 desktop-buffer-read-only
498 When a handler is called, the buffer has been created and the major mode has
499 been set, but local variables listed in desktop-buffer-locals has not yet been
502 Modules that define a minor mode that needs a special handler should contain
505 (defun foo-desktop-restore
507 (add-to-list 'desktop-minor-mode-handlers
508 '(foo-mode . foo-desktop-restore))
510 Furthermore the minor mode function must be autoloaded.
512 See also `desktop-minor-mode-table'.")
515 (put 'desktop-minor-mode-handlers
'risky-local-variable t
)
517 ;; ----------------------------------------------------------------------------
518 (defvar desktop-dirname nil
519 "The directory in which the desktop file should be saved.")
521 (defun desktop-full-file-name (&optional dirname
)
522 "Return the full name of the desktop file in DIRNAME.
523 DIRNAME omitted or nil means use `desktop-dirname'."
524 (expand-file-name desktop-base-file-name
(or dirname desktop-dirname
)))
526 (defun desktop-full-lock-name (&optional dirname
)
527 "Return the full name of the desktop lock file in DIRNAME.
528 DIRNAME omitted or nil means use `desktop-dirname'."
529 (expand-file-name desktop-base-lock-name
(or dirname desktop-dirname
)))
531 (defconst desktop-header
532 ";; --------------------------------------------------------------------------
533 ;; Desktop File for Emacs
534 ;; --------------------------------------------------------------------------
535 " "*Header to place in Desktop file.")
537 (defvar desktop-delay-hook nil
538 "Hooks run after all buffers are loaded; intended for internal use.")
540 ;; ----------------------------------------------------------------------------
541 ;; Desktop file conflict detection
542 (defvar desktop-file-modtime nil
543 "When the desktop file was last modified to the knowledge of this Emacs.
544 Used to detect desktop file conflicts.")
546 (defun desktop-owner (&optional dirname
)
547 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
548 Return nil if no desktop file found or no Emacs process is using it.
549 DIRNAME omitted or nil means use `desktop-dirname'."
551 (and (file-exists-p (desktop-full-lock-name dirname
))
554 (insert-file-contents-literally (desktop-full-lock-name dirname
))
555 (goto-char (point-min))
556 (setq owner
(read (current-buffer)))
561 (defun desktop-claim-lock (&optional dirname
)
562 "Record this Emacs process as the owner of the desktop file in DIRNAME.
563 DIRNAME omitted or nil means use `desktop-dirname'."
564 (write-region (number-to-string (emacs-pid)) nil
565 (desktop-full-lock-name dirname
)))
567 (defun desktop-release-lock (&optional dirname
)
568 "Remove the lock file for the desktop in DIRNAME.
569 DIRNAME omitted or nil means use `desktop-dirname'."
570 (let ((file (desktop-full-lock-name dirname
)))
571 (when (file-exists-p file
) (delete-file file
))))
573 ;; ----------------------------------------------------------------------------
574 (defun desktop-truncate (list n
)
575 "Truncate LIST to at most N elements destructively."
576 (let ((here (nthcdr (1- n
) list
)))
580 ;; ----------------------------------------------------------------------------
582 (defun desktop-clear ()
584 This kills all buffers except for internal ones and those with names matched by
585 a regular expression in the list `desktop-clear-preserve-buffers'.
586 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
589 (dolist (var desktop-globals-to-clear
)
591 (eval `(setq-default ,var nil
))
592 (eval `(setq-default ,(car var
) ,(cdr var
)))))
593 (let ((buffers (buffer-list))
594 (preserve-regexp (concat "^\\("
595 (mapconcat (lambda (regexp)
596 (concat "\\(" regexp
"\\)"))
597 desktop-clear-preserve-buffers
601 (let ((bufname (buffer-name (car buffers
))))
604 (string-match preserve-regexp bufname
)
605 ;; Don't kill buffers made for internal purposes.
606 (and (not (equal bufname
"")) (eq (aref bufname
0) ?\s
))
607 (kill-buffer (car buffers
))))
608 (setq buffers
(cdr buffers
))))
609 (delete-other-windows))
611 ;; ----------------------------------------------------------------------------
612 (add-hook 'kill-emacs-hook
'desktop-kill
)
614 (defun desktop-kill ()
615 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
616 If the desktop should be saved and `desktop-dirname'
617 is nil, ask the user where to save the desktop."
618 (when (and desktop-save-mode
619 (let ((exists (file-exists-p (desktop-full-file-name))))
620 (or (eq desktop-save t
)
621 (and exists
(memq desktop-save
'(ask-if-new if-exists
)))
623 (or (memq desktop-save
'(ask ask-if-new
))
624 (and exists
(eq desktop-save
'ask-if-exists
)))
625 (y-or-n-p "Save desktop? ")))))
626 (unless desktop-dirname
627 (setq desktop-dirname
628 (file-name-as-directory
630 (read-directory-name "Directory for desktop file: " nil nil t
)))))
632 (desktop-save desktop-dirname t
)
634 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
635 (signal (car err
) (cdr err
))))))
636 ;; If we own it, we don't anymore.
637 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
639 ;; ----------------------------------------------------------------------------
640 (defun desktop-list* (&rest args
)
641 (if (null (cdr args
))
643 (setq args
(nreverse args
))
644 (let ((value (cons (nth 1 args
) (car args
))))
645 (setq args
(cdr (cdr args
)))
647 (setq value
(cons (car args
) value
))
648 (setq args
(cdr args
)))
651 ;; ----------------------------------------------------------------------------
652 (defun desktop-buffer-info (buffer)
655 ;; base name of the buffer; replaces the buffer name if managed by uniquify
656 (and (fboundp 'uniquify-buffer-base-name
) (uniquify-buffer-base-name))
658 (desktop-file-name (buffer-file-name) desktop-dirname
)
664 #'(lambda (minor-mode)
665 (and (boundp minor-mode
)
666 (symbol-value minor-mode
)
667 (let* ((special (assq minor-mode desktop-minor-mode-table
))
668 (value (cond (special (cadr special
))
669 ((functionp minor-mode
) minor-mode
))))
670 (when value
(add-to-list 'ret value
)))))
671 (mapcar #'car minor-mode-alist
))
673 ;; point and mark, and read-only status
675 (list (mark t
) mark-active
)
677 ;; auxiliary information
678 (when (functionp desktop-save-buffer
)
679 (funcall desktop-save-buffer desktop-dirname
))
681 (let ((locals desktop-locals-to-save
)
682 (loclist (buffer-local-variables))
685 (let ((here (assq (car locals
) loclist
)))
687 (setq ll
(cons here ll
))
688 (when (member (car locals
) loclist
)
689 (setq ll
(cons (car locals
) ll
)))))
690 (setq locals
(cdr locals
)))
693 ;; ----------------------------------------------------------------------------
694 (defun desktop-internal-v2s (value)
695 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
696 TXT is a string that when read and evaluated yields value.
697 QUOTE may be `may' (value may be quoted),
698 `must' (values must be quoted), or nil (value may not be quoted)."
700 ((or (numberp value
) (null value
) (eq t value
) (keywordp value
))
701 (cons 'may
(prin1-to-string value
)))
703 (let ((copy (copy-sequence value
)))
704 (set-text-properties 0 (length copy
) nil copy
)
705 ;; Get rid of text properties because we cannot read them
706 (cons 'may
(prin1-to-string copy
))))
708 (cons 'must
(prin1-to-string value
)))
713 (let ((res (desktop-internal-v2s el
)))
719 (cons nil
(concat "(vector "
720 (mapconcat (lambda (el)
721 (if (eq (car el
) 'must
)
722 (concat "'" (cdr el
))
727 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
734 (let ((q.txt
(desktop-internal-v2s (car p
))))
735 (or anynil
(setq anynil
(null (car q.txt
))))
736 (setq newlist
(cons q.txt newlist
)))
739 (let ((last (desktop-internal-v2s p
)))
740 (or anynil
(setq anynil
(null (car last
))))
742 (setq newlist
(cons '(must .
".") newlist
)))
744 (setq newlist
(cons last newlist
))))
745 (setq newlist
(nreverse newlist
))
748 (concat (if use-list
* "(desktop-list* " "(list ")
749 (mapconcat (lambda (el)
750 (if (eq (car el
) 'must
)
751 (concat "'" (cdr el
))
757 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
759 (cons nil
(concat "(symbol-function '"
760 (substring (prin1-to-string value
) 7 -
1)
763 (let ((pos (prin1-to-string (marker-position value
)))
764 (buf (prin1-to-string (buffer-name (marker-buffer value
)))))
765 (cons nil
(concat "(let ((mk (make-marker)))"
766 " (add-hook 'desktop-delay-hook"
767 " (list 'lambda '() (list 'set-marker mk "
768 pos
" (get-buffer " buf
")))) mk)"))))
770 (cons 'may
"\"Unprintable entity\""))))
772 ;; ----------------------------------------------------------------------------
773 (defun desktop-value-to-string (value)
774 "Convert VALUE to a string that when read evaluates to the same value.
775 Not all types of values are supported."
776 (let* ((print-escape-newlines t
)
777 (float-output-format nil
)
778 (quote.txt
(desktop-internal-v2s value
))
779 (quote (car quote.txt
))
780 (txt (cdr quote.txt
)))
785 ;; ----------------------------------------------------------------------------
786 (defun desktop-outvar (varspec)
787 "Output a setq statement for variable VAR to the desktop file.
788 The argument VARSPEC may be the variable name VAR (a symbol),
789 or a cons cell of the form (VAR . MAX-SIZE),
790 which means to truncate VAR's value to at most MAX-SIZE elements
791 \(if the value is a list) before saving the value."
794 (setq var
(car varspec
) size
(cdr varspec
))
797 (when (and (integerp size
)
800 (desktop-truncate (eval var
) size
))
804 (desktop-value-to-string (symbol-value var
))
807 ;; ----------------------------------------------------------------------------
808 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
809 "Return t if buffer should have its state saved in the desktop file.
810 FILENAME is the visited file name, BUFNAME is the buffer name, and
811 MODE is the major mode.
812 \n\(fn FILENAME BUFNAME MODE)"
813 (let ((case-fold-search nil
))
814 (and (not (string-match desktop-buffers-not-to-save bufname
))
815 (not (memq mode desktop-modes-not-to-save
))
817 (not (string-match desktop-files-not-to-save filename
)))
818 (and (eq mode
'dired-mode
)
819 (with-current-buffer bufname
820 (not (string-match desktop-files-not-to-save
821 default-directory
))))
823 (with-current-buffer bufname desktop-save-buffer
))))))
825 ;; ----------------------------------------------------------------------------
826 (defun desktop-file-name (filename dirname
)
827 "Convert FILENAME to format specified in `desktop-file-name-format'.
828 DIRNAME must be the directory in which the desktop file will be saved."
831 ((eq desktop-file-name-format
'tilde
)
832 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
834 ((file-name-absolute-p relative-name
) relative-name
)
835 ((string= "./" relative-name
) "~/")
836 ((string= "." relative-name
) "~")
837 (t (concat "~/" relative-name
)))))
838 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
839 (t (expand-file-name filename
))))
841 ;; ----------------------------------------------------------------------------
843 (defun desktop-save (dirname &optional release
)
844 "Save the desktop in a desktop file.
845 Parameter DIRNAME specifies where to save the desktop file.
846 Optional parameter RELEASE says whether we're done with this desktop.
847 See also `desktop-base-file-name'."
848 (interactive "DDirectory to save desktop file in: ")
849 (setq desktop-dirname
(file-name-as-directory (expand-file-name dirname
)))
851 (let ((eager desktop-restore-eager
)
852 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
854 (or (not new-modtime
) ; nothing to overwrite
855 (equal desktop-file-modtime new-modtime
)
856 (yes-or-no-p (if desktop-file-modtime
857 (if (> (float-time new-modtime
) (float-time desktop-file-modtime
))
858 "Desktop file is more recent than the one loaded. Save anyway? "
859 "Desktop file isn't the one loaded. Overwrite it? ")
860 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
861 (unless release
(error "Desktop file conflict")))
863 ;; If we're done with it, release the lock.
864 ;; Otherwise, claim it if it's unclaimed or if we created it.
866 (desktop-release-lock)
867 (unless (and new-modtime
(desktop-owner)) (desktop-claim-lock)))
871 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
873 ";; Created " (current-time-string) "\n"
874 ";; Desktop file format version " desktop-file-version
"\n"
875 ";; Emacs version " emacs-version
"\n")
876 (save-excursion (run-hooks 'desktop-save-hook
))
877 (goto-char (point-max))
878 (insert "\n;; Global section:\n")
879 (mapc (function desktop-outvar
) desktop-globals-to-save
)
880 (when (memq 'kill-ring desktop-globals-to-save
)
882 "(setq kill-ring-yank-pointer (nthcdr "
883 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
886 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
887 (dolist (l (mapcar 'desktop-buffer-info
(buffer-list)))
888 (let ((base (pop l
)))
889 (when (apply 'desktop-save-buffer-p l
)
891 (if (or (not (integerp eager
))
894 (setq eager
(1- eager
))))
895 "desktop-create-buffer"
896 "desktop-append-buffer-args")
898 desktop-file-version
)
899 ;; If there's a non-empty base name, we save it instead of the buffer name
900 (when (and base
(not (string= base
"")))
901 (setcar (nthcdr 1 l
) base
))
903 (insert "\n " (desktop-value-to-string e
)))
906 (setq default-directory desktop-dirname
)
907 (let ((coding-system-for-write 'emacs-mule
))
908 (write-region (point-min) (point-max) (desktop-full-file-name) nil
'nomessage
))
909 ;; We remember when it was modified (which is presumably just now).
910 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name)))))))))
912 ;; ----------------------------------------------------------------------------
914 (defun desktop-remove ()
915 "Delete desktop file in `desktop-dirname'.
916 This function also sets `desktop-dirname' to nil."
918 (when desktop-dirname
919 (let ((filename (desktop-full-file-name)))
920 (setq desktop-dirname nil
)
921 (when (file-exists-p filename
)
922 (delete-file filename
)))))
924 (defvar desktop-buffer-args-list nil
925 "List of args for `desktop-create-buffer'.")
927 (defvar desktop-lazy-timer nil
)
929 ;; ----------------------------------------------------------------------------
931 (defun desktop-read (&optional dirname
)
932 "Read and process the desktop file in directory DIRNAME.
933 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
934 directories listed in `desktop-path'. If a desktop file is found, it
935 is processed and `desktop-after-read-hook' is run. If no desktop file
936 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
937 This function is a no-op when Emacs is running in batch mode.
938 It returns t if a desktop file was loaded, nil otherwise."
940 (unless noninteractive
941 (setq desktop-dirname
942 (file-name-as-directory
945 ;; If DIRNAME is specified, use it.
946 (and (< 0 (length dirname
)) dirname
)
947 ;; Otherwise search desktop file in desktop-path.
948 (let ((dirs desktop-path
))
951 (desktop-full-file-name (car dirs
)))))
952 (setq dirs
(cdr dirs
)))
953 (and dirs
(car dirs
)))
954 ;; If not found and `desktop-path' is non-nil, use its first element.
955 (and desktop-path
(car desktop-path
))
956 ;; Default: Home directory.
958 (if (file-exists-p (desktop-full-file-name))
959 ;; Desktop file found, but is it already in use?
960 (let ((desktop-first-buffer nil
)
961 (desktop-buffer-ok-count 0)
962 (desktop-buffer-fail-count 0)
963 (owner (desktop-owner))
964 ;; Avoid desktop saving during evaluation of desktop buffer.
967 (memq desktop-load-locked-desktop
'(nil ask
))
968 (or (null desktop-load-locked-desktop
)
969 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
970 Using it may cause conflicts. Use it anyway? " owner
)))))
971 (let ((default-directory desktop-dirname
))
972 (setq desktop-dirname nil
)
973 (run-hooks 'desktop-not-loaded-hook
)
974 (unless desktop-dirname
975 (message "Desktop file in use; not loaded.")))
977 ;; Evaluate desktop buffer and remember when it was modified.
978 (load (desktop-full-file-name) t t t
)
979 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name))))
980 ;; If it wasn't already, mark it as in-use, to bother other
981 ;; desktop instances.
985 (file-error (message "Couldn't record use of desktop file")
988 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
989 ;; We want buffers existing prior to evaluating the desktop (and
990 ;; not reused) to be placed at the end of the buffer list, so we
993 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
994 (switch-to-buffer (car (buffer-list)))
995 (run-hooks 'desktop-delay-hook
)
996 (setq desktop-delay-hook nil
)
997 (run-hooks 'desktop-after-read-hook
)
998 (message "Desktop: %d buffer%s restored%s%s."
999 desktop-buffer-ok-count
1000 (if (= 1 desktop-buffer-ok-count
) "" "s")
1001 (if (< 0 desktop-buffer-fail-count
)
1002 (format ", %d failed to restore" desktop-buffer-fail-count
)
1004 (if desktop-buffer-args-list
1005 (format ", %d to restore lazily"
1006 (length desktop-buffer-args-list
))
1009 ;; No desktop file found.
1011 (let ((default-directory desktop-dirname
))
1012 (run-hooks 'desktop-no-desktop-file-hook
))
1013 (message "No desktop file.")
1016 ;; ----------------------------------------------------------------------------
1017 ;; Maintained for backward compatibility
1019 (defun desktop-load-default ()
1020 "Load the `default' start-up library manually.
1021 Also inhibit further loading of it."
1022 (unless inhibit-default-init
; safety check
1023 (load "default" t t
)
1024 (setq inhibit-default-init t
)))
1025 (make-obsolete 'desktop-load-default
1026 'desktop-save-mode
"22.1")
1028 ;; ----------------------------------------------------------------------------
1030 (defun desktop-change-dir (dirname)
1031 "Change to desktop saved in DIRNAME.
1032 Kill the desktop as specified by variables `desktop-save-mode' and
1033 `desktop-save', then clear the desktop and load the desktop file in
1035 (interactive "DChange to directory: ")
1036 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
1039 (desktop-read dirname
))
1041 ;; ----------------------------------------------------------------------------
1043 (defun desktop-save-in-desktop-dir ()
1044 "Save the desktop in directory `desktop-dirname'."
1047 (desktop-save desktop-dirname
)
1048 (call-interactively 'desktop-save
))
1049 (message "Desktop saved in %s" desktop-dirname
))
1051 ;; ----------------------------------------------------------------------------
1053 (defun desktop-revert ()
1054 "Revert to the last loaded desktop."
1056 (unless desktop-dirname
1057 (error "Unknown desktop directory"))
1058 (unless (file-exists-p (desktop-full-file-name))
1059 (error "No desktop file found"))
1061 (desktop-read desktop-dirname
))
1063 (defvar desktop-buffer-major-mode
)
1064 (defvar desktop-buffer-locals
)
1065 ;; ----------------------------------------------------------------------------
1066 (defun desktop-restore-file-buffer (desktop-buffer-file-name
1068 desktop-buffer-misc
)
1069 "Restore a file buffer."
1070 (when desktop-buffer-file-name
1071 (if (or (file-exists-p desktop-buffer-file-name
)
1072 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1073 desktop-buffer-file-name
)))
1074 (if desktop-missing-file-warning
1075 (y-or-n-p (concat msg
" Re-create buffer? "))
1078 (let* ((auto-insert nil
) ; Disable auto insertion
1079 (coding-system-for-read
1080 (or coding-system-for-read
1081 (cdr (assq 'buffer-file-coding-system
1082 desktop-buffer-locals
))))
1083 (buf (find-file-noselect desktop-buffer-file-name
)))
1085 (switch-to-buffer buf
)
1086 (error (pop-to-buffer buf
)))
1087 (and (not (eq major-mode desktop-buffer-major-mode
))
1088 (functionp desktop-buffer-major-mode
)
1089 (funcall desktop-buffer-major-mode
))
1093 (defun desktop-load-file (function)
1094 "Load the file where auto loaded FUNCTION is defined."
1096 (let ((fcell (and (fboundp function
) (symbol-function function
))))
1097 (when (and (listp fcell
)
1098 (eq 'autoload
(car fcell
)))
1099 (load (cadr fcell
))))))
1101 ;; ----------------------------------------------------------------------------
1102 ;; Create a buffer, load its file, set its mode, ...;
1103 ;; called from Desktop file only.
1105 ;; Just to silence the byte compiler.
1107 (defvar desktop-first-buffer
) ; Dynamically bound in `desktop-read'
1109 ;; Bound locally in `desktop-read'.
1110 (defvar desktop-buffer-ok-count
)
1111 (defvar desktop-buffer-fail-count
)
1113 (defun desktop-create-buffer
1114 (desktop-file-version
1115 desktop-buffer-file-name
1117 desktop-buffer-major-mode
1118 desktop-buffer-minor-modes
1119 desktop-buffer-point
1121 desktop-buffer-read-only
1124 desktop-buffer-locals
)
1125 ;; To make desktop files with relative file names possible, we cannot
1126 ;; allow `default-directory' to change. Therefore we save current buffer.
1127 (save-current-buffer
1128 ;; Give major mode module a chance to add a handler.
1129 (desktop-load-file desktop-buffer-major-mode
)
1130 (let ((buffer-list (buffer-list))
1133 (funcall (or (cdr (assq desktop-buffer-major-mode
1134 desktop-buffer-mode-handlers
))
1135 'desktop-restore-file-buffer
)
1136 desktop-buffer-file-name
1138 desktop-buffer-misc
)
1140 (message "Desktop: Can't load buffer %s: %s"
1142 (error-message-string err
))
1143 (when desktop-missing-file-warning
(sit-for 1))
1145 (if (bufferp result
)
1146 (setq desktop-buffer-ok-count
(1+ desktop-buffer-ok-count
))
1147 (setq desktop-buffer-fail-count
(1+ desktop-buffer-fail-count
))
1149 ;; Restore buffer list order with new buffer at end. Don't change
1150 ;; the order for old desktop files (old desktop module behaviour).
1151 (unless (< desktop-file-version
206)
1152 (mapc 'bury-buffer buffer-list
)
1153 (when result
(bury-buffer result
)))
1155 (unless (or desktop-first-buffer
(< desktop-file-version
206))
1156 (setq desktop-first-buffer result
))
1158 (unless (equal (buffer-name) desktop-buffer-name
)
1159 (rename-buffer desktop-buffer-name t
))
1161 (cond ((equal '(t) desktop-buffer-minor-modes
) ; backwards compatible
1163 ((equal '(nil) desktop-buffer-minor-modes
) ; backwards compatible
1166 (dolist (minor-mode desktop-buffer-minor-modes
)
1167 ;; Give minor mode module a chance to add a handler.
1168 (desktop-load-file minor-mode
)
1169 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers
))))
1171 (funcall handler desktop-buffer-locals
)
1172 (when (functionp minor-mode
)
1173 (funcall minor-mode
1)))))))
1174 ;; Even though point and mark are non-nil when written by
1175 ;; `desktop-save', they may be modified by handlers wanting to set
1176 ;; point or mark themselves.
1177 (when desktop-buffer-point
1180 ;; Evaluate point. Thus point can be something like
1181 ;; '(search-forward ...
1182 (eval desktop-buffer-point
)
1183 (error (message "%s" (error-message-string err
)) 1))))
1184 (when desktop-buffer-mark
1185 (if (consp desktop-buffer-mark
)
1187 (set-mark (car desktop-buffer-mark
))
1188 (setq mark-active
(car (cdr desktop-buffer-mark
))))
1189 (set-mark desktop-buffer-mark
)))
1190 ;; Never override file system if the file really is read-only marked.
1191 (when desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
1192 (while desktop-buffer-locals
1193 (let ((this (car desktop-buffer-locals
)))
1195 ;; an entry of this form `(symbol . value)'
1197 (make-local-variable (car this
))
1198 (set (car this
) (cdr this
)))
1199 ;; an entry of the form `symbol'
1200 (make-local-variable this
)
1202 (setq desktop-buffer-locals
(cdr desktop-buffer-locals
)))))))
1204 ;; ----------------------------------------------------------------------------
1205 ;; Backward compatibility -- update parameters to 205 standards.
1206 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1207 desktop-buffer-major-mode
1208 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
1209 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
1210 desktop-buffer-major-mode
(cdr mim
) pt mk ro
1212 (list (cons 'truncate-lines tl
)
1213 (cons 'fill-column fc
)
1214 (cons 'case-fold-search cfs
)
1215 (cons 'case-replace cr
)
1216 (cons 'overwrite-mode
(car mim
)))))
1218 (defun desktop-append-buffer-args (&rest args
)
1219 "Append ARGS at end of `desktop-buffer-args-list'.
1220 ARGS must be an argument list for `desktop-create-buffer'."
1221 (setq desktop-buffer-args-list
(nconc desktop-buffer-args-list
(list args
)))
1222 (unless desktop-lazy-timer
1223 (setq desktop-lazy-timer
1224 (run-with-idle-timer desktop-lazy-idle-delay t
'desktop-idle-create-buffers
))))
1226 (defun desktop-lazy-create-buffer ()
1227 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1228 (when desktop-buffer-args-list
1229 (let* ((remaining (length desktop-buffer-args-list
))
1230 (args (pop desktop-buffer-args-list
))
1231 (buffer-name (nth 2 args
))
1232 (msg (format "Desktop lazily opening %s (%s remaining)..."
1233 buffer-name remaining
)))
1234 (when desktop-lazy-verbose
1236 (let ((desktop-first-buffer nil
)
1237 (desktop-buffer-ok-count 0)
1238 (desktop-buffer-fail-count 0))
1239 (apply 'desktop-create-buffer args
)
1240 (run-hooks 'desktop-delay-hook
)
1241 (setq desktop-delay-hook nil
)
1242 (bury-buffer (get-buffer buffer-name
))
1243 (when desktop-lazy-verbose
1244 (message "%s%s" msg
(if (> desktop-buffer-ok-count
0) "done" "failed")))))))
1246 (defun desktop-idle-create-buffers ()
1247 "Create buffers until the user does something, then stop.
1248 If there are no buffers left to create, kill the timer."
1250 (while (and repeat desktop-buffer-args-list
)
1251 (save-window-excursion
1252 (desktop-lazy-create-buffer))
1253 (setq repeat
(sit-for 0.2))
1254 (unless desktop-buffer-args-list
1255 (cancel-timer desktop-lazy-timer
)
1256 (setq desktop-lazy-timer nil
)
1257 (message "Lazy desktop load complete")
1261 (defun desktop-lazy-complete ()
1262 "Run the desktop load to completion."
1264 (let ((desktop-lazy-verbose t
))
1265 (while desktop-buffer-args-list
1266 (save-window-excursion
1267 (desktop-lazy-create-buffer)))
1268 (message "Lazy desktop load complete")))
1270 (defun desktop-lazy-abort ()
1271 "Abort lazy loading of the desktop."
1273 (when desktop-lazy-timer
1274 (cancel-timer desktop-lazy-timer
)
1275 (setq desktop-lazy-timer nil
))
1276 (when desktop-buffer-args-list
1277 (setq desktop-buffer-args-list nil
)
1278 (when (interactive-p)
1279 (message "Lazy desktop load aborted"))))
1281 ;; ----------------------------------------------------------------------------
1282 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1283 ;; command line, we do the rest of what it takes to use desktop, but do it
1284 ;; after finishing loading the init file.
1285 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1286 ;; functions are processed after `after-init-hook'.
1290 (let ((key "--no-desktop"))
1291 (when (member key command-line-args
)
1292 (setq command-line-args
(delete key command-line-args
))
1293 (setq desktop-save-mode nil
)))
1294 (when desktop-save-mode
1296 (setq inhibit-startup-screen t
))))
1300 ;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1301 ;;; desktop.el ends here