(match): Use yellow background on light-bg terminals.
[emacs.git] / lisp / desktop.el
blob4328b8e38396fce5266d40f195ebf5582d88ebea
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 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 2, or (at your option)
15 ;; 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; 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.
27 ;;; Commentary:
29 ;; Save the Desktop, i.e.,
30 ;; - some global variables
31 ;; - the list of buffers with associated files. For each buffer also
32 ;; - the major mode
33 ;; - the default directory
34 ;; - the point
35 ;; - the mark & mark-active
36 ;; - buffer-read-only
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.
53 ;; Special handling.
54 ;; -----------------
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
76 ;; ...
77 ;; (add-to-list 'desktop-buffer-mode-handlers
78 ;; '(foo-mode . foo-restore-desktop-buffer))
80 ;; or
82 ;; (defun bar-desktop-restore
83 ;; ...
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.
91 ;; Minor modes.
92 ;; ------------
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 ;; ---------------------------------------------------------------------------
130 ;; TODO:
132 ;; Save window configuration.
133 ;; Recognize more minor modes.
134 ;; Save mark rings.
136 ;;; Code:
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."
149 :group 'frames)
151 ;;;###autoload
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."
158 :global t
159 :group 'desktop)
161 ;; Maintained for backward compatibility
162 (define-obsolete-variable-alias 'desktop-enable
163 'desktop-save-mode "22.1")
165 (defcustom desktop-save 'ask-if-new
166 "*Specifies whether the desktop should be saved when it is killed.
167 A desktop is killed when the user changes desktop or quits Emacs.
168 Possible values are:
169 t -- always save.
170 ask -- always ask.
171 ask-if-new -- ask if no desktop file exists, otherwise just save.
172 ask-if-exists -- ask if desktop file exists, otherwise don't save.
173 if-exists -- save if desktop file exists, otherwise don't save.
174 nil -- never save.
175 The desktop is never saved when `desktop-save-mode' is nil.
176 The variables `desktop-dirname' and `desktop-base-file-name'
177 determine where the desktop is saved."
178 :type
179 '(choice
180 (const :tag "Always save" t)
181 (const :tag "Always ask" ask)
182 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
183 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
184 (const :tag "Save if desktop file exists, else don't" if-exists)
185 (const :tag "Never save" nil))
186 :group 'desktop
187 :version "22.1")
189 (defcustom desktop-base-file-name
190 (convert-standard-filename ".emacs.desktop")
191 "Name of file for Emacs desktop, excluding the directory part."
192 :type 'file
193 :group 'desktop)
194 (define-obsolete-variable-alias 'desktop-basefilename
195 'desktop-base-file-name "22.1")
197 (defcustom desktop-path '("." "~")
198 "List of directories to search for the desktop file.
199 The base name of the file is specified in `desktop-base-file-name'."
200 :type '(repeat directory)
201 :group 'desktop
202 :version "22.1")
204 (defcustom desktop-missing-file-warning nil
205 "If non-nil, offer to recreate the buffer of a deleted file.
206 Also pause for a moment to display message about errors signaled in
207 `desktop-buffer-mode-handlers'.
209 If nil, just print error messages in the message buffer."
210 :type 'boolean
211 :group 'desktop
212 :version "22.1")
214 (defcustom desktop-no-desktop-file-hook nil
215 "Normal hook run when `desktop-read' can't find a desktop file.
216 Run in the directory in which the desktop file was sought.
217 May be used to show a dired buffer."
218 :type 'hook
219 :group 'desktop
220 :version "22.1")
222 (defcustom desktop-after-read-hook nil
223 "Normal hook run after a successful `desktop-read'.
224 May be used to show a buffer list."
225 :type 'hook
226 :group 'desktop
227 :options '(list-buffers)
228 :version "22.1")
230 (defcustom desktop-save-hook nil
231 "Normal hook run before the desktop is saved in a desktop file.
232 Run with the desktop buffer current with only the header present.
233 May be used to add to the desktop code or to truncate history lists,
234 for example."
235 :type 'hook
236 :group 'desktop)
238 (defcustom desktop-globals-to-save
239 '(desktop-missing-file-warning
240 tags-file-name
241 tags-table-list
242 search-ring
243 regexp-search-ring
244 register-alist)
245 "List of global variables saved by `desktop-save'.
246 An element may be variable name (a symbol) or a cons cell of the form
247 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
248 MAX-SIZE elements (if the value is a list) before saving the value.
249 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
250 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
251 :group 'desktop)
253 (defcustom desktop-globals-to-clear
254 '(kill-ring
255 kill-ring-yank-pointer
256 search-ring
257 search-ring-yank-pointer
258 regexp-search-ring
259 regexp-search-ring-yank-pointer)
260 "List of global variables that `desktop-clear' will clear.
261 An element may be variable name (a symbol) or a cons cell of the form
262 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
263 to the value obtained by evaluating FORM."
264 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
265 :group 'desktop
266 :version "22.1")
268 (defcustom desktop-clear-preserve-buffers
269 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*")
270 "*List of buffers that `desktop-clear' should not delete.
271 Each element is a regular expression. Buffers with a name matched by any of
272 these won't be deleted."
273 :type '(repeat string)
274 :group 'desktop)
276 ;;;###autoload
277 (defcustom desktop-locals-to-save
278 '(desktop-locals-to-save ; Itself! Think it over.
279 truncate-lines
280 case-fold-search
281 case-replace
282 fill-column
283 overwrite-mode
284 change-log-default-name
285 line-number-mode
286 column-number-mode
287 size-indication-mode
288 buffer-file-coding-system
289 indent-tabs-mode
290 tab-width
291 indicate-buffer-boundaries
292 indicate-empty-lines
293 show-trailing-whitespace)
294 "List of local variables to save for each buffer.
295 The variables are saved only when they really are local. Conventional minor
296 modes are restored automatically; they should not be listed here."
297 :type '(repeat symbol)
298 :group 'desktop)
300 ;; We skip .log files because they are normally temporary.
301 ;; (ftp) files because they require passwords and whatnot.
302 (defcustom desktop-buffers-not-to-save
303 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\)$"
304 "Regexp identifying buffers that are to be excluded from saving."
305 :type 'regexp
306 :group 'desktop)
308 ;; Skip tramp and ange-ftp files
309 (defcustom desktop-files-not-to-save
310 "^/[^/:]*:"
311 "Regexp identifying files whose buffers are to be excluded from saving."
312 :type 'regexp
313 :group 'desktop)
315 ;; We skip TAGS files to save time (tags-file-name is saved instead).
316 (defcustom desktop-modes-not-to-save
317 '(tags-table-mode)
318 "List of major modes whose buffers should not be saved."
319 :type '(repeat symbol)
320 :group 'desktop)
322 (defcustom desktop-file-name-format 'absolute
323 "*Format in which desktop file names should be saved.
324 Possible values are:
325 absolute -- Absolute file name.
326 tilde -- Relative to ~.
327 local -- Relative to directory of desktop file."
328 :type '(choice (const absolute) (const tilde) (const local))
329 :group 'desktop
330 :version "22.1")
332 (defcustom desktop-restore-eager t
333 "Number of buffers to restore immediately.
334 Remaining buffers are restored lazily (when Emacs is idle).
335 If value is t, all buffers are restored immediately."
336 :type '(choice (const t) integer)
337 :group 'desktop
338 :version "22.1")
340 (defcustom desktop-lazy-verbose t
341 "Verbose reporting of lazily created buffers."
342 :type 'boolean
343 :group 'desktop
344 :version "22.1")
346 (defcustom desktop-lazy-idle-delay 5
347 "Idle delay before starting to create buffers.
348 See `desktop-restore-eager'."
349 :type 'integer
350 :group 'desktop
351 :version "22.1")
353 ;;;###autoload
354 (defvar desktop-save-buffer nil
355 "When non-nil, save buffer status in desktop file.
356 This variable becomes buffer local when set.
358 If the value is a function, it is called by `desktop-save' with argument
359 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
360 file along with the state of the buffer for which it was called.
362 When file names are returned, they should be formatted using the call
363 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
365 Later, when `desktop-read' evaluates the desktop file, auxiliary information
366 is passed as the argument DESKTOP-BUFFER-MISC to functions in
367 `desktop-buffer-mode-handlers'.")
368 (make-variable-buffer-local 'desktop-save-buffer)
369 (make-obsolete-variable 'desktop-buffer-modes-to-save
370 'desktop-save-buffer "22.1")
371 (make-obsolete-variable 'desktop-buffer-misc-functions
372 'desktop-save-buffer "22.1")
374 ;;;###autoload
375 (defvar desktop-buffer-mode-handlers
377 "Alist of major mode specific functions to restore a desktop buffer.
378 Functions listed are called by `desktop-create-buffer' when `desktop-read'
379 evaluates the desktop file. List elements must have the form
381 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
383 Buffers with a major mode not specified here, are restored by the default
384 handler `desktop-restore-file-buffer'.
386 Handlers are called with argument list
388 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
390 Furthermore, they may use the following variables:
392 desktop-file-version
393 desktop-buffer-major-mode
394 desktop-buffer-minor-modes
395 desktop-buffer-point
396 desktop-buffer-mark
397 desktop-buffer-read-only
398 desktop-buffer-locals
400 If a handler returns a buffer, then the saved mode settings
401 and variable values for that buffer are copied into it.
403 Modules that define a major mode that needs a special handler should contain
404 code like
406 (defun foo-restore-desktop-buffer
408 (add-to-list 'desktop-buffer-mode-handlers
409 '(foo-mode . foo-restore-desktop-buffer))
411 Furthermore the major mode function must be autoloaded.")
413 ;;;###autoload
414 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
415 (make-obsolete-variable 'desktop-buffer-handlers
416 'desktop-buffer-mode-handlers "22.1")
418 (defcustom desktop-minor-mode-table
419 '((auto-fill-function auto-fill-mode)
420 (vc-mode nil)
421 (vc-dired-mode nil))
422 "Table mapping minor mode variables to minor mode functions.
423 Each entry has the form (NAME RESTORE-FUNCTION).
424 NAME is the name of the buffer-local variable indicating that the minor
425 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
426 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
427 Only minor modes for which the name of the buffer-local variable
428 and the name of the minor mode function are different have to be added to
429 this table. See also `desktop-minor-mode-handlers'."
430 :type 'sexp
431 :group 'desktop)
433 ;;;###autoload
434 (defvar desktop-minor-mode-handlers
436 "Alist of functions to restore non-standard minor modes.
437 Functions are called by `desktop-create-buffer' to restore minor modes.
438 List elements must have the form
440 (MINOR-MODE . RESTORE-FUNCTION).
442 Minor modes not specified here, are restored by the standard minor mode
443 function.
445 Handlers are called with argument list
447 (DESKTOP-BUFFER-LOCALS)
449 Furthermore, they may use the following variables:
451 desktop-file-version
452 desktop-buffer-file-name
453 desktop-buffer-name
454 desktop-buffer-major-mode
455 desktop-buffer-minor-modes
456 desktop-buffer-point
457 desktop-buffer-mark
458 desktop-buffer-read-only
459 desktop-buffer-misc
461 When a handler is called, the buffer has been created and the major mode has
462 been set, but local variables listed in desktop-buffer-locals has not yet been
463 created and set.
465 Modules that define a minor mode that needs a special handler should contain
466 code like
468 (defun foo-desktop-restore
470 (add-to-list 'desktop-minor-mode-handlers
471 '(foo-mode . foo-desktop-restore))
473 Furthermore the minor mode function must be autoloaded.
475 See also `desktop-minor-mode-table'.")
477 ;;;###autoload
478 (put 'desktop-minor-mode-handlers 'risky-local-variable t)
480 ;; ----------------------------------------------------------------------------
481 (defvar desktop-dirname nil
482 "The directory in which the desktop file should be saved.")
484 (defun desktop-full-file-name (&optional dirname)
485 "Return the full name of the desktop file in DIRNAME.
486 DIRNAME omitted or nil means use `desktop-dirname'."
487 (expand-file-name desktop-base-file-name (or dirname desktop-dirname)))
489 (defconst desktop-header
490 ";; --------------------------------------------------------------------------
491 ;; Desktop File for Emacs
492 ;; --------------------------------------------------------------------------
493 " "*Header to place in Desktop file.")
495 (defvar desktop-delay-hook nil
496 "Hooks run after all buffers are loaded; intended for internal use.")
498 ;; ----------------------------------------------------------------------------
499 (defun desktop-truncate (list n)
500 "Truncate LIST to at most N elements destructively."
501 (let ((here (nthcdr (1- n) list)))
502 (if (consp here)
503 (setcdr here nil))))
505 ;; ----------------------------------------------------------------------------
506 ;;;###autoload
507 (defun desktop-clear ()
508 "Empty the Desktop.
509 This kills all buffers except for internal ones and those with names matched by
510 a regular expression in the list `desktop-clear-preserve-buffers'.
511 Furthermore, it clears the variables listed in `desktop-globals-to-clear'."
512 (interactive)
513 (desktop-lazy-abort)
514 (dolist (var desktop-globals-to-clear)
515 (if (symbolp var)
516 (eval `(setq-default ,var nil))
517 (eval `(setq-default ,(car var) ,(cdr var)))))
518 (let ((buffers (buffer-list))
519 (preserve-regexp (concat "^\\("
520 (mapconcat (lambda (regexp)
521 (concat "\\(" regexp "\\)"))
522 desktop-clear-preserve-buffers
523 "\\|")
524 "\\)$")))
525 (while buffers
526 (let ((bufname (buffer-name (car buffers))))
528 (null bufname)
529 (string-match preserve-regexp bufname)
530 ;; Don't kill buffers made for internal purposes.
531 (and (not (equal bufname "")) (eq (aref bufname 0) ?\s))
532 (kill-buffer (car buffers))))
533 (setq buffers (cdr buffers))))
534 (delete-other-windows))
536 ;; ----------------------------------------------------------------------------
537 (add-hook 'kill-emacs-hook 'desktop-kill)
539 (defun desktop-kill ()
540 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
541 If the desktop should be saved and `desktop-dirname'
542 is nil, ask the user where to save the desktop."
543 (when (and desktop-save-mode
544 (let ((exists (file-exists-p (desktop-full-file-name))))
545 (or (eq desktop-save t)
546 (and exists (memq desktop-save '(ask-if-new if-exists)))
547 (and
548 (or (memq desktop-save '(ask ask-if-new))
549 (and exists (eq desktop-save 'ask-if-exists)))
550 (y-or-n-p "Save desktop? ")))))
551 (unless desktop-dirname
552 (setq desktop-dirname
553 (file-name-as-directory
554 (expand-file-name
555 (call-interactively
556 (lambda (dir)
557 (interactive "DDirectory for desktop file: ") dir))))))
558 (condition-case err
559 (desktop-save desktop-dirname)
560 (file-error
561 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
562 (signal (car err) (cdr err)))))))
564 ;; ----------------------------------------------------------------------------
565 (defun desktop-list* (&rest args)
566 (if (null (cdr args))
567 (car args)
568 (setq args (nreverse args))
569 (let ((value (cons (nth 1 args) (car args))))
570 (setq args (cdr (cdr args)))
571 (while args
572 (setq value (cons (car args) value))
573 (setq args (cdr args)))
574 value)))
576 ;; ----------------------------------------------------------------------------
577 (defun desktop-internal-v2s (value)
578 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
579 TXT is a string that when read and evaluated yields value.
580 QUOTE may be `may' (value may be quoted),
581 `must' (values must be quoted), or nil (value may not be quoted)."
582 (cond
583 ((or (numberp value) (null value) (eq t value) (keywordp value))
584 (cons 'may (prin1-to-string value)))
585 ((stringp value)
586 (let ((copy (copy-sequence value)))
587 (set-text-properties 0 (length copy) nil copy)
588 ;; Get rid of text properties because we cannot read them
589 (cons 'may (prin1-to-string copy))))
590 ((symbolp value)
591 (cons 'must (prin1-to-string value)))
592 ((vectorp value)
593 (let* ((special nil)
594 (pass1 (mapcar
595 (lambda (el)
596 (let ((res (desktop-internal-v2s el)))
597 (if (null (car res))
598 (setq special t))
599 res))
600 value)))
601 (if special
602 (cons nil (concat "(vector "
603 (mapconcat (lambda (el)
604 (if (eq (car el) 'must)
605 (concat "'" (cdr el))
606 (cdr el)))
607 pass1
608 " ")
609 ")"))
610 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
611 ((consp value)
612 (let ((p value)
613 newlist
614 use-list*
615 anynil)
616 (while (consp p)
617 (let ((q.txt (desktop-internal-v2s (car p))))
618 (or anynil (setq anynil (null (car q.txt))))
619 (setq newlist (cons q.txt newlist)))
620 (setq p (cdr p)))
621 (if p
622 (let ((last (desktop-internal-v2s p))
623 (el (car newlist)))
624 (or anynil (setq anynil (null (car last))))
625 (or anynil
626 (setq newlist (cons '(must . ".") newlist)))
627 (setq use-list* t)
628 (setq newlist (cons last newlist))))
629 (setq newlist (nreverse newlist))
630 (if anynil
631 (cons nil
632 (concat (if use-list* "(desktop-list* " "(list ")
633 (mapconcat (lambda (el)
634 (if (eq (car el) 'must)
635 (concat "'" (cdr el))
636 (cdr el)))
637 newlist
638 " ")
639 ")"))
640 (cons 'must
641 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
642 ((subrp value)
643 (cons nil (concat "(symbol-function '"
644 (substring (prin1-to-string value) 7 -1)
645 ")")))
646 ((markerp value)
647 (let ((pos (prin1-to-string (marker-position value)))
648 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
649 (cons nil (concat "(let ((mk (make-marker)))"
650 " (add-hook 'desktop-delay-hook"
651 " (list 'lambda '() (list 'set-marker mk "
652 pos " (get-buffer " buf ")))) mk)"))))
653 (t ; save as text
654 (cons 'may "\"Unprintable entity\""))))
656 ;; ----------------------------------------------------------------------------
657 (defun desktop-value-to-string (value)
658 "Convert VALUE to a string that when read evaluates to the same value.
659 Not all types of values are supported."
660 (let* ((print-escape-newlines t)
661 (float-output-format nil)
662 (quote.txt (desktop-internal-v2s value))
663 (quote (car quote.txt))
664 (txt (cdr quote.txt)))
665 (if (eq quote 'must)
666 (concat "'" txt)
667 txt)))
669 ;; ----------------------------------------------------------------------------
670 (defun desktop-outvar (varspec)
671 "Output a setq statement for variable VAR to the desktop file.
672 The argument VARSPEC may be the variable name VAR (a symbol),
673 or a cons cell of the form (VAR . MAX-SIZE),
674 which means to truncate VAR's value to at most MAX-SIZE elements
675 \(if the value is a list) before saving the value."
676 (let (var size)
677 (if (consp varspec)
678 (setq var (car varspec) size (cdr varspec))
679 (setq var varspec))
680 (if (boundp var)
681 (progn
682 (if (and (integerp size)
683 (> size 0)
684 (listp (eval var)))
685 (desktop-truncate (eval var) size))
686 (insert "(setq "
687 (symbol-name var)
689 (desktop-value-to-string (symbol-value var))
690 ")\n")))))
692 ;; ----------------------------------------------------------------------------
693 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
694 "Return t if buffer should have its state saved in the desktop file.
695 FILENAME is the visited file name, BUFNAME is the buffer name, and
696 MODE is the major mode.
697 \n\(fn FILENAME BUFNAME MODE)"
698 (let ((case-fold-search nil))
699 (and (not (string-match desktop-buffers-not-to-save bufname))
700 (not (memq mode desktop-modes-not-to-save))
701 (or (and filename
702 (not (string-match desktop-files-not-to-save filename)))
703 (and (eq mode 'dired-mode)
704 (with-current-buffer bufname
705 (not (string-match desktop-files-not-to-save
706 default-directory))))
707 (and (null filename)
708 (with-current-buffer bufname desktop-save-buffer))))))
710 ;; ----------------------------------------------------------------------------
711 (defun desktop-file-name (filename dirname)
712 "Convert FILENAME to format specified in `desktop-file-name-format'.
713 DIRNAME must be the directory in which the desktop file will be saved."
714 (cond
715 ((not filename) nil)
716 ((eq desktop-file-name-format 'tilde)
717 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
718 (cond
719 ((file-name-absolute-p relative-name) relative-name)
720 ((string= "./" relative-name) "~/")
721 ((string= "." relative-name) "~")
722 (t (concat "~/" relative-name)))))
723 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
724 (t (expand-file-name filename))))
726 ;; ----------------------------------------------------------------------------
727 ;;;###autoload
728 (defun desktop-save (dirname)
729 "Save the desktop in a desktop file.
730 Parameter DIRNAME specifies where to save the desktop file.
731 See also `desktop-base-file-name'."
732 (interactive "DDirectory to save desktop file in: ")
733 (run-hooks 'desktop-save-hook)
734 (setq dirname (file-name-as-directory (expand-file-name dirname)))
735 (save-excursion
736 (let ((filename (desktop-full-file-name dirname))
737 (info
738 (mapcar
739 #'(lambda (b)
740 (set-buffer b)
741 (list
742 (desktop-file-name (buffer-file-name) dirname)
743 (buffer-name)
744 major-mode
745 ;; minor modes
746 (let (ret)
747 (mapc
748 #'(lambda (minor-mode)
749 (and
750 (boundp minor-mode)
751 (symbol-value minor-mode)
752 (let* ((special (assq minor-mode desktop-minor-mode-table))
753 (value (cond (special (cadr special))
754 ((functionp minor-mode) minor-mode))))
755 (when value (add-to-list 'ret value)))))
756 (mapcar #'car minor-mode-alist))
757 ret)
758 (point)
759 (list (mark t) mark-active)
760 buffer-read-only
761 ;; Auxiliary information
762 (when (functionp desktop-save-buffer)
763 (funcall desktop-save-buffer dirname))
764 (let ((locals desktop-locals-to-save)
765 (loclist (buffer-local-variables))
766 (ll))
767 (while locals
768 (let ((here (assq (car locals) loclist)))
769 (if here
770 (setq ll (cons here ll))
771 (when (member (car locals) loclist)
772 (setq ll (cons (car locals) ll)))))
773 (setq locals (cdr locals)))
774 ll)))
775 (buffer-list)))
776 (eager desktop-restore-eager))
777 (with-temp-buffer
778 (insert
779 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
780 desktop-header
781 ";; Created " (current-time-string) "\n"
782 ";; Desktop file format version " desktop-file-version "\n"
783 ";; Emacs version " emacs-version "\n\n"
784 ";; Global section:\n")
785 (mapc (function desktop-outvar) desktop-globals-to-save)
786 (if (memq 'kill-ring desktop-globals-to-save)
787 (insert
788 "(setq kill-ring-yank-pointer (nthcdr "
789 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
790 " kill-ring))\n"))
792 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
793 (mapc #'(lambda (l)
794 (when (apply 'desktop-save-buffer-p l)
795 (insert "("
796 (if (or (not (integerp eager))
797 (unless (zerop eager)
798 (setq eager (1- eager))
800 "desktop-create-buffer"
801 "desktop-append-buffer-args")
803 desktop-file-version)
804 (mapc #'(lambda (e)
805 (insert "\n " (desktop-value-to-string e)))
807 (insert ")\n\n")))
808 info)
809 (setq default-directory dirname)
810 (let ((coding-system-for-write 'emacs-mule))
811 (write-region (point-min) (point-max) filename nil 'nomessage)))))
812 (setq desktop-dirname dirname))
814 ;; ----------------------------------------------------------------------------
815 ;;;###autoload
816 (defun desktop-remove ()
817 "Delete desktop file in `desktop-dirname'.
818 This function also sets `desktop-dirname' to nil."
819 (interactive)
820 (when desktop-dirname
821 (let ((filename (desktop-full-file-name)))
822 (setq desktop-dirname nil)
823 (when (file-exists-p filename)
824 (delete-file filename)))))
826 (defvar desktop-buffer-args-list nil
827 "List of args for `desktop-create-buffer'.")
829 (defvar desktop-lazy-timer nil)
831 ;; ----------------------------------------------------------------------------
832 ;;;###autoload
833 (defun desktop-read (&optional dirname)
834 "Read and process the desktop file in directory DIRNAME.
835 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
836 directories listed in `desktop-path'. If a desktop file is found, it
837 is processed and `desktop-after-read-hook' is run. If no desktop file
838 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
839 This function is a no-op when Emacs is running in batch mode.
840 It returns t if a desktop file was loaded, nil otherwise."
841 (interactive)
842 (unless noninteractive
843 (setq desktop-dirname
844 (file-name-as-directory
845 (expand-file-name
847 ;; If DIRNAME is specified, use it.
848 (and (< 0 (length dirname)) dirname)
849 ;; Otherwise search desktop file in desktop-path.
850 (let ((dirs desktop-path))
851 (while (and dirs
852 (not (file-exists-p
853 (desktop-full-file-name (car dirs)))))
854 (setq dirs (cdr dirs)))
855 (and dirs (car dirs)))
856 ;; If not found and `desktop-path' is non-nil, use its first element.
857 (and desktop-path (car desktop-path))
858 ;; Default: Home directory.
859 "~"))))
860 (if (file-exists-p (desktop-full-file-name))
861 ;; Desktop file found, process it.
862 (let ((desktop-first-buffer nil)
863 (desktop-buffer-ok-count 0)
864 (desktop-buffer-fail-count 0)
865 ;; Avoid desktop saving during evaluation of desktop buffer.
866 (desktop-save nil))
867 (desktop-lazy-abort)
868 ;; Evaluate desktop buffer.
869 (load (desktop-full-file-name) t t t)
870 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
871 ;; We want buffers existing prior to evaluating the desktop (and not reused)
872 ;; to be placed at the end of the buffer list, so we move them here.
873 (mapc 'bury-buffer
874 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
875 (switch-to-buffer (car (buffer-list)))
876 (run-hooks 'desktop-delay-hook)
877 (setq desktop-delay-hook nil)
878 (run-hooks 'desktop-after-read-hook)
879 (message "Desktop: %d buffer%s restored%s%s."
880 desktop-buffer-ok-count
881 (if (= 1 desktop-buffer-ok-count) "" "s")
882 (if (< 0 desktop-buffer-fail-count)
883 (format ", %d failed to restore" desktop-buffer-fail-count)
885 (if desktop-buffer-args-list
886 (format ", %d to restore lazily"
887 (length desktop-buffer-args-list))
888 ""))
890 ;; No desktop file found.
891 (desktop-clear)
892 (let ((default-directory desktop-dirname))
893 (run-hooks 'desktop-no-desktop-file-hook))
894 (message "No desktop file.")
895 nil)))
897 ;; ----------------------------------------------------------------------------
898 ;; Maintained for backward compatibility
899 ;;;###autoload
900 (defun desktop-load-default ()
901 "Load the `default' start-up library manually.
902 Also inhibit further loading of it."
903 (unless inhibit-default-init ; safety check
904 (load "default" t t)
905 (setq inhibit-default-init t)))
906 (make-obsolete 'desktop-load-default
907 'desktop-save-mode "22.1")
909 ;; ----------------------------------------------------------------------------
910 ;;;###autoload
911 (defun desktop-change-dir (dirname)
912 "Change to desktop saved in DIRNAME.
913 Kill the desktop as specified by variables `desktop-save-mode' and
914 `desktop-save', then clear the desktop and load the desktop file in
915 directory DIRNAME."
916 (interactive "DChange to directory: ")
917 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
918 (desktop-kill)
919 (desktop-clear)
920 (desktop-read dirname))
922 ;; ----------------------------------------------------------------------------
923 ;;;###autoload
924 (defun desktop-save-in-desktop-dir ()
925 "Save the desktop in directory `desktop-dirname'."
926 (interactive)
927 (if desktop-dirname
928 (desktop-save desktop-dirname)
929 (call-interactively 'desktop-save))
930 (message "Desktop saved in %s" desktop-dirname))
932 ;; ----------------------------------------------------------------------------
933 ;;;###autoload
934 (defun desktop-revert ()
935 "Revert to the last loaded desktop."
936 (interactive)
937 (unless desktop-dirname
938 (error "Unknown desktop directory"))
939 (unless (file-exists-p (desktop-full-file-name))
940 (error "No desktop file found"))
941 (desktop-clear)
942 (desktop-read desktop-dirname))
944 ;; ----------------------------------------------------------------------------
945 (defun desktop-restore-file-buffer (desktop-buffer-file-name
946 desktop-buffer-name
947 desktop-buffer-misc)
948 "Restore a file buffer."
949 (eval-when-compile ; Just to silence the byte compiler
950 (defvar desktop-buffer-major-mode)
951 (defvar desktop-buffer-locals))
952 (if desktop-buffer-file-name
953 (if (or (file-exists-p desktop-buffer-file-name)
954 (let ((msg (format "Desktop: File \"%s\" no longer exists."
955 desktop-buffer-file-name)))
956 (if desktop-missing-file-warning
957 (y-or-n-p (concat msg " Re-create buffer? "))
958 (message "%s" msg)
959 nil)))
960 (let* ((auto-insert nil) ; Disable auto insertion
961 (coding-system-for-read
962 (or coding-system-for-read
963 (cdr (assq 'buffer-file-coding-system
964 desktop-buffer-locals))))
965 (buf (find-file-noselect desktop-buffer-file-name)))
966 (condition-case nil
967 (switch-to-buffer buf)
968 (error (pop-to-buffer buf)))
969 (and (not (eq major-mode desktop-buffer-major-mode))
970 (functionp desktop-buffer-major-mode)
971 (funcall desktop-buffer-major-mode))
972 buf)
973 nil)))
975 (defun desktop-load-file (function)
976 "Load the file where auto loaded FUNCTION is defined."
977 (when function
978 (let ((fcell (and (fboundp function) (symbol-function function))))
979 (when (and (listp fcell)
980 (eq 'autoload (car fcell)))
981 (load (cadr fcell))))))
983 ;; ----------------------------------------------------------------------------
984 ;; Create a buffer, load its file, set its mode, ...;
985 ;; called from Desktop file only.
987 ;; Just to silence the byte compiler.
988 (eval-when-compile
989 (defvar desktop-first-buffer)) ; Dynamically bound in `desktop-read'
991 (defun desktop-create-buffer
992 (desktop-file-version
993 desktop-buffer-file-name
994 desktop-buffer-name
995 desktop-buffer-major-mode
996 desktop-buffer-minor-modes
997 desktop-buffer-point
998 desktop-buffer-mark
999 desktop-buffer-read-only
1000 desktop-buffer-misc
1001 &optional
1002 desktop-buffer-locals)
1003 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
1004 (eval-when-compile
1005 (defvar desktop-buffer-ok-count)
1006 (defvar desktop-buffer-fail-count))
1007 ;; To make desktop files with relative file names possible, we cannot
1008 ;; allow `default-directory' to change. Therefore we save current buffer.
1009 (save-current-buffer
1010 ;; Give major mode module a chance to add a handler.
1011 (desktop-load-file desktop-buffer-major-mode)
1012 (let ((buffer-list (buffer-list))
1013 (result
1014 (condition-case err
1015 (funcall (or (cdr (assq desktop-buffer-major-mode
1016 desktop-buffer-mode-handlers))
1017 'desktop-restore-file-buffer)
1018 desktop-buffer-file-name
1019 desktop-buffer-name
1020 desktop-buffer-misc)
1021 (error
1022 (message "Desktop: Can't load buffer %s: %s"
1023 desktop-buffer-name
1024 (error-message-string err))
1025 (when desktop-missing-file-warning (sit-for 1))
1026 nil))))
1027 (if (bufferp result)
1028 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
1029 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
1030 (setq result nil))
1031 ;; Restore buffer list order with new buffer at end. Don't change
1032 ;; the order for old desktop files (old desktop module behaviour).
1033 (unless (< desktop-file-version 206)
1034 (mapc 'bury-buffer buffer-list)
1035 (when result (bury-buffer result)))
1036 (when result
1037 (unless (or desktop-first-buffer (< desktop-file-version 206))
1038 (setq desktop-first-buffer result))
1039 (set-buffer result)
1040 (unless (equal (buffer-name) desktop-buffer-name)
1041 (rename-buffer desktop-buffer-name))
1042 ;; minor modes
1043 (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
1044 (auto-fill-mode 1))
1045 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
1046 (auto-fill-mode 0))
1048 (mapc #'(lambda (minor-mode)
1049 ;; Give minor mode module a chance to add a handler.
1050 (desktop-load-file minor-mode)
1051 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers))))
1052 (if handler
1053 (funcall handler desktop-buffer-locals)
1054 (when (functionp minor-mode)
1055 (funcall minor-mode 1)))))
1056 desktop-buffer-minor-modes)))
1057 ;; Even though point and mark are non-nil when written by `desktop-save',
1058 ;; they may be modified by handlers wanting to set point or mark themselves.
1059 (when desktop-buffer-point
1060 (goto-char
1061 (condition-case err
1062 ;; Evaluate point. Thus point can be something like '(search-forward ...
1063 (eval desktop-buffer-point)
1064 (error (message "%s" (error-message-string err)) 1))))
1065 (when desktop-buffer-mark
1066 (if (consp desktop-buffer-mark)
1067 (progn
1068 (set-mark (car desktop-buffer-mark))
1069 (setq mark-active (car (cdr desktop-buffer-mark))))
1070 (set-mark desktop-buffer-mark)))
1071 ;; Never override file system if the file really is read-only marked.
1072 (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
1073 (while desktop-buffer-locals
1074 (let ((this (car desktop-buffer-locals)))
1075 (if (consp this)
1076 ;; an entry of this form `(symbol . value)'
1077 (progn
1078 (make-local-variable (car this))
1079 (set (car this) (cdr this)))
1080 ;; an entry of the form `symbol'
1081 (make-local-variable this)
1082 (makunbound this)))
1083 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
1085 ;; ----------------------------------------------------------------------------
1086 ;; Backward compatibility -- update parameters to 205 standards.
1087 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
1088 desktop-buffer-major-mode
1089 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
1090 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
1091 desktop-buffer-major-mode (cdr mim) pt mk ro
1092 desktop-buffer-misc
1093 (list (cons 'truncate-lines tl)
1094 (cons 'fill-column fc)
1095 (cons 'case-fold-search cfs)
1096 (cons 'case-replace cr)
1097 (cons 'overwrite-mode (car mim)))))
1099 (defun desktop-append-buffer-args (&rest args)
1100 "Append ARGS at end of `desktop-buffer-args-list'.
1101 ARGS must be an argument list for `desktop-create-buffer'."
1102 (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args)))
1103 (unless desktop-lazy-timer
1104 (setq desktop-lazy-timer
1105 (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers))))
1107 (defun desktop-lazy-create-buffer ()
1108 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1109 (when desktop-buffer-args-list
1110 (let* ((remaining (length desktop-buffer-args-list))
1111 (args (pop desktop-buffer-args-list))
1112 (buffer-name (nth 2 args))
1113 (msg (format "Desktop lazily opening %s (%s remaining)..."
1114 buffer-name remaining)))
1115 (when desktop-lazy-verbose
1116 (message "%s" msg))
1117 (let ((desktop-first-buffer nil)
1118 (desktop-buffer-ok-count 0)
1119 (desktop-buffer-fail-count 0))
1120 (apply 'desktop-create-buffer args)
1121 (run-hooks 'desktop-delay-hook)
1122 (setq desktop-delay-hook nil)
1123 (bury-buffer (get-buffer buffer-name))
1124 (when desktop-lazy-verbose
1125 (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed")))))))
1127 (defun desktop-idle-create-buffers ()
1128 "Create buffers until the user does something, then stop.
1129 If there are no buffers left to create, kill the timer."
1130 (let ((repeat 1))
1131 (while (and repeat desktop-buffer-args-list)
1132 (save-window-excursion
1133 (desktop-lazy-create-buffer))
1134 (setq repeat (sit-for 0.2))
1135 (unless desktop-buffer-args-list
1136 (cancel-timer desktop-lazy-timer)
1137 (setq desktop-lazy-timer nil)
1138 (message "Lazy desktop load complete")
1139 (sit-for 3)
1140 (message "")))))
1142 (defun desktop-lazy-complete ()
1143 "Run the desktop load to completion."
1144 (interactive)
1145 (let ((desktop-lazy-verbose t))
1146 (while desktop-buffer-args-list
1147 (save-window-excursion
1148 (desktop-lazy-create-buffer)))
1149 (message "Lazy desktop load complete")))
1151 (defun desktop-lazy-abort ()
1152 "Abort lazy loading of the desktop."
1153 (interactive)
1154 (when desktop-lazy-timer
1155 (cancel-timer desktop-lazy-timer)
1156 (setq desktop-lazy-timer nil))
1157 (when desktop-buffer-args-list
1158 (setq desktop-buffer-args-list nil)
1159 (when (interactive-p)
1160 (message "Lazy desktop load aborted"))))
1162 ;; ----------------------------------------------------------------------------
1163 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1164 ;; command line, we do the rest of what it takes to use desktop, but do it
1165 ;; after finishing loading the init file.
1166 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1167 ;; functions are processed after `after-init-hook'.
1168 (add-hook
1169 'after-init-hook
1170 '(lambda ()
1171 (let ((key "--no-desktop"))
1172 (when (member key command-line-args)
1173 (setq command-line-args (delete key command-line-args))
1174 (setq desktop-save-mode nil)))
1175 (when desktop-save-mode (desktop-read))))
1177 (provide 'desktop)
1179 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1180 ;;; desktop.el ends here