1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Maintainter: Lars Hansen <larsh@math.ku.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 2, or (at your option)
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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Save the Desktop, i.e.,
31 ;; - some global variables
32 ;; - the list of buffers with associated files. For each buffer also
34 ;; - the default directory
36 ;; - the mark & mark-active
38 ;; - some local variables
40 ;; To use this, use customize to turn on desktop-save-mode or add the
41 ;; following line somewhere in your .emacs file:
43 ;; (desktop-save-mode 1)
45 ;; For further usage information, look at the section
46 ;; "Saving Emacs Sessions" in the GNU Emacs Manual.
48 ;; When the desktop module is loaded, the function `desktop-kill' is
49 ;; added to the `kill-emacs-hook'. This function is responsible for
50 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
51 ;; function is added to the `after-init-hook'. This function is
52 ;; responsible for loading the desktop when Emacs is started.
54 ;; Some words on minor modes: Most minor modes are controlled by
55 ;; buffer-local variables, which have a standard save / restore
56 ;; mechanism. To handle all minor modes, we take the following
57 ;; approach: (1) check whether the variable name from
58 ;; `minor-mode-alist' is also a function; and (2) use translation
59 ;; table `desktop-minor-mode-table' in the case where the two names
62 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
63 ;; in your home directory is used for that. Saving global default values
64 ;; for buffers is an example of misuse.
66 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
67 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
68 ;; things you did not mean to keep. Use M-x desktop-clear RET.
70 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
71 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
72 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
73 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
74 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
75 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
76 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
77 ;; ---------------------------------------------------------------------------
80 ;; Save window configuration.
81 ;; Recognize more minor modes.
86 ;; Make the compilation more silent
88 ;; We use functions from these modules
89 ;; We can't (require 'mh-e) since that wants to load something.
90 (mapcar 'require
'(info dired reporter
)))
92 (defvar desktop-file-version
"206"
93 "Verion number of desktop file format.
94 Written into the desktop file and used at desktop read to provide
95 backward compatibility.")
97 ;; ----------------------------------------------------------------------------
98 ;; USER OPTIONS -- settings you might want to play with.
99 ;; ----------------------------------------------------------------------------
101 (defgroup desktop nil
102 "Save status of Emacs when you exit."
106 (define-minor-mode desktop-save-mode
107 "Toggle desktop saving mode.
108 With numeric ARG, turn desktop saving on if ARG is positive, off
109 otherwise. See variable `desktop-save' for a description of when the
114 ;; Maintained for backward compatibility
115 (defvaralias 'desktop-enable
'desktop-save-mode
)
116 (make-obsolete-variable 'desktop-enable
'desktop-save-mode
)
118 (defcustom desktop-save
'ask-if-new
119 "*Specifies whether the desktop should be saved when it is killed.
120 A desktop is killed when the user changes desktop or quits Emacs.
124 ask-if-new -- ask if no desktop file exists, otherwise just save.
125 ask-if-exists -- ask if desktop file exists, otherwise don't save.
126 if-exists -- save if desktop file exists, otherwise don't save.
128 The desktop is never saved when `desktop-save-mode' is nil.
129 The variables `desktop-directory' and `desktop-base-file-name'
130 determine where the desktop is saved."
132 (const :tag
"Always save" t
)
133 (const :tag
"Always ask" ask
)
134 (const :tag
"Ask if desktop file is new, else do save" ask-if-new
)
135 (const :tag
"Ask if desktop file exists, else don't save" ask-if-exists
)
136 (const :tag
"Save if desktop file exists, else don't" if-exists
)
137 (const :tag
"Never save" nil
))
140 (defcustom desktop-base-file-name
141 (convert-standard-filename ".emacs.desktop")
142 "Name of file for Emacs desktop, excluding the directory part."
145 (defvaralias 'desktop-basefilename
'desktop-base-file-name
)
147 (defcustom desktop-path
'("." "~")
148 "List of directories to search for the desktop file.
149 The base name of the file is specified in `desktop-base-file-name'."
150 :type
'(repeat directory
)
153 (defcustom desktop-missing-file-warning nil
154 "*If non-nil then `desktop-read' warns when a file no longer exists.
155 Otherwise it simply ignores that file."
159 (defcustom desktop-no-desktop-file-hook nil
160 "Normal hook run when `desktop-read' can't find a desktop file.
161 May e.g. be used to show a dired buffer."
165 (defcustom desktop-after-read-hook nil
166 "Normal hook run after a sucessful `desktop-read'.
167 May e.g. be used to show a buffer list."
171 (defcustom desktop-save-hook nil
172 "Normal hook run before the desktop is saved in a desktop file.
173 This is useful for truncating history lists, for example."
177 (defcustom desktop-globals-to-save
'(
178 desktop-missing-file-warning
184 "List of global variables saved by `desktop-save'.
185 An element may be variable name (a symbol) or a cons cell of the form
186 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
187 MAX-SIZE elements (if the value is a list) before saving the value.
188 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
189 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
192 (defcustom desktop-globals-to-clear
'(
194 kill-ring-yank-pointer
196 search-ring-yank-pointer
198 regexp-search-ring-yank-pointer
)
199 "List of global variables to clear by `desktop-clear'.
200 An element may be variable name (a symbol) or a cons cell of the form
201 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
202 to the value obtained by evaluateing FORM."
203 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
206 (defcustom desktop-clear-preserve-buffers-regexp
207 "^\\(\\*scratch\\*\\|\\*Messages\\*\\|\\*tramp/.+\\*\\)$"
208 "Regexp identifying buffers that `desktop-clear' should not delete."
212 ;; Maintained for backward compatibility
213 (defcustom desktop-clear-preserve-buffers nil
214 "*List of buffer names that `desktop-clear' should not delete.
215 This variable is maintained for backward compatibility only. Use
216 `desktop-clear-preserve-buffers-regexp' instead."
217 :type
'(repeat string
)
219 (make-obsolete-variable 'desktop-clear-preserve-buffers
220 'desktop-clear-preserve-buffers-regexp
)
222 (defcustom desktop-locals-to-save
'(
223 desktop-locals-to-save
; Itself! Think it over.
229 change-log-default-name
231 buffer-file-coding-system
)
232 "List of local variables to save for each buffer.
233 The variables are saved only when they really are local."
234 :type
'(repeat symbol
)
236 (make-variable-buffer-local 'desktop-locals-to-save
)
238 ;; We skip .log files because they are normally temporary.
239 ;; (ftp) files because they require passwords and whatnot.
240 ;; TAGS files to save time (tags-file-name is saved instead).
241 (defcustom desktop-buffers-not-to-save
242 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
243 "Regexp identifying buffers that are to be excluded from saving."
247 ;; Skip tramp and ange-ftp files
248 (defcustom desktop-files-not-to-save
250 "Regexp identifying files whose buffers are to be excluded from saving."
254 (defcustom desktop-buffer-modes-to-save
255 '(Info-mode rmail-mode
)
256 "If a buffer is of one of these major modes, save the buffer state.
257 It is up to the functions in `desktop-buffer-handlers' to decide
258 whether the buffer should be recreated or not, and how."
259 :type
'(repeat symbol
)
262 (defcustom desktop-modes-not-to-save nil
263 "List of major modes whose buffers should not be saved."
264 :type
'(repeat symbol
)
267 (defcustom desktop-file-name-format
'absolute
268 "*Format in which desktop file names should be saved.
270 absolute -- Absolute file name.
271 tilde -- Relative to ~.
272 local -- Relative to directory of desktop file."
273 :type
'(choice (const absolute
) (const tilde
) (const local
))
276 (defcustom desktop-buffer-misc-functions
277 '(desktop-buffer-info-misc-data
278 desktop-buffer-dired-misc-data
)
279 "*Functions used to determine auxiliary information for a buffer.
280 These functions are called by `desktop-save' in order, with no
281 arguments. If a function returns non-nil, its value is saved along
282 with the state of the buffer for which it was called; no further
283 functions will be called.
285 When file names are returned, they should be formatted using the call
286 \"(desktop-file-name FILE-NAME dirname)\".
288 Later, when `desktop-read' restores buffers, each of the functions in
289 `desktop-buffer-handlers' will have access to a buffer local variable,
290 named `desktop-buffer-misc', whose value is what the function in
291 `desktop-buffer-misc-functions' returned."
292 :type
'(repeat function
)
295 (defcustom desktop-buffer-handlers
296 '(desktop-buffer-dired
301 "*Functions called by `desktop-read' in order to create a buffer.
302 The functions are called without explicit parameters but can use the
306 desktop-buffer-file-name
308 desktop-buffer-major-mode
309 desktop-buffer-minor-modes
312 desktop-buffer-read-only
314 desktop-buffer-locals
316 If one function returns non-nil, no further functions are called.
317 If the function returns a buffer, then the saved mode settings
318 and variable values for that buffer are copied into it."
319 :type
'(repeat function
)
322 (put 'desktop-buffer-handlers
'risky-local-variable t
)
324 (defcustom desktop-minor-mode-table
325 '((auto-fill-function auto-fill-mode
)
327 "Table mapping minor mode variables to minor mode functions.
328 Each entry has the form (NAME RESTORE-FUNCTION).
329 NAME is the name of the buffer-local variable indicating that the minor
330 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
331 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
332 Only minor modes for which the name of the buffer-local variable
333 and the name of the minor mode function are different have to added to
338 ;; ----------------------------------------------------------------------------
339 (defvar desktop-dirname nil
340 "The directory in which the desktop file should be saved.")
342 (defconst desktop-header
343 ";; --------------------------------------------------------------------------
344 ;; Desktop File for Emacs
345 ;; --------------------------------------------------------------------------
346 " "*Header to place in Desktop file.")
348 (defvar desktop-delay-hook nil
349 "Hooks run after all buffers are loaded; intended for internal use.")
351 ;; ----------------------------------------------------------------------------
352 (defun desktop-truncate (l n
)
353 "Truncate LIST to at most N elements destructively."
354 (let ((here (nthcdr (1- n
) l
)))
358 ;; ----------------------------------------------------------------------------
359 (defun desktop-clear ()
361 This kills all buffers except for internal ones and those matching
362 `desktop-clear-preserve-buffers-regexp' or listed in
363 `desktop-clear-preserve-buffers'. Furthermore, it clears the
364 variables listed in `desktop-globals-to-clear'."
366 (dolist (var desktop-globals-to-clear
)
368 (eval `(setq-default ,var nil
))
369 (eval `(setq-default ,(car var
) ,(cdr var
)))))
370 (let ((buffers (buffer-list)))
372 (let ((bufname (buffer-name (car buffers
))))
375 (string-match desktop-clear-preserve-buffers-regexp bufname
)
376 (member bufname desktop-clear-preserve-buffers
)
377 ;; Don't kill buffers made for internal purposes.
378 (and (not (equal bufname
"")) (eq (aref bufname
0) ?\
))
379 (kill-buffer (car buffers
))))
380 (setq buffers
(cdr buffers
))))
381 (delete-other-windows))
383 ;; ----------------------------------------------------------------------------
384 (add-hook 'kill-emacs-hook
'desktop-kill
)
386 (defun desktop-kill ()
387 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
388 If the desktop should be saved and `desktop-dirname'
389 is nil, ask the user where to save the desktop."
393 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))))
396 (and exists
(memq desktop-save
'(ask-if-new if-exists
)))
399 (memq desktop-save
'(ask ask-if-new
))
400 (and exists
(eq desktop-save
'ask-if-exists
)))
401 (y-or-n-p "Save desktop? ")))))
402 (unless desktop-dirname
403 (setq desktop-dirname
404 (file-name-as-directory
407 (lambda (dir) (interactive "DDirectory for desktop file: ") dir
))))))
409 (desktop-save desktop-dirname
)
411 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
412 (signal (car err
) (cdr err
)))))))
414 ;; ----------------------------------------------------------------------------
415 (defun desktop-list* (&rest args
)
416 (if (null (cdr args
))
418 (setq args
(nreverse args
))
419 (let ((value (cons (nth 1 args
) (car args
))))
420 (setq args
(cdr (cdr args
)))
422 (setq value
(cons (car args
) value
))
423 (setq args
(cdr args
)))
426 ;; ----------------------------------------------------------------------------
427 (defun desktop-internal-v2s (val)
428 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
429 TXT is a string that when read and evaluated yields value.
430 QUOTE may be `may' (value may be quoted),
431 `must' (values must be quoted), or nil (value may not be quoted)."
433 ((or (numberp val
) (null val
) (eq t val
))
434 (cons 'may
(prin1-to-string val
)))
436 (let ((copy (copy-sequence val
)))
437 (set-text-properties 0 (length copy
) nil copy
)
438 ;; Get rid of text properties because we cannot read them
439 (cons 'may
(prin1-to-string copy
))))
441 (cons 'must
(prin1-to-string val
)))
446 (let ((res (desktop-internal-v2s el
)))
452 (cons nil
(concat "(vector "
453 (mapconcat (lambda (el)
454 (if (eq (car el
) 'must
)
455 (concat "'" (cdr el
))
460 (cons 'may
(concat "[" (mapconcat 'cdr pass1
" ") "]")))))
467 (let ((q.txt
(desktop-internal-v2s (car p
))))
468 (or anynil
(setq anynil
(null (car q.txt
))))
469 (setq newlist
(cons q.txt newlist
)))
472 (let ((last (desktop-internal-v2s p
))
474 (or anynil
(setq anynil
(null (car last
))))
476 (setq newlist
(cons '(must .
".") newlist
)))
478 (setq newlist
(cons last newlist
))))
479 (setq newlist
(nreverse newlist
))
482 (concat (if use-list
* "(desktop-list* " "(list ")
483 (mapconcat (lambda (el)
484 (if (eq (car el
) 'must
)
485 (concat "'" (cdr el
))
491 (concat "(" (mapconcat 'cdr newlist
" ") ")")))))
493 (cons nil
(concat "(symbol-function '"
494 (substring (prin1-to-string val
) 7 -
1)
497 (let ((pos (prin1-to-string (marker-position val
)))
498 (buf (prin1-to-string (buffer-name (marker-buffer val
)))))
499 (cons nil
(concat "(let ((mk (make-marker)))"
500 " (add-hook 'desktop-delay-hook"
501 " (list 'lambda '() (list 'set-marker mk "
502 pos
" (get-buffer " buf
")))) mk)"))))
504 (cons 'may
"\"Unprintable entity\""))))
506 ;; ----------------------------------------------------------------------------
507 (defun desktop-value-to-string (val)
508 "Convert VALUE to a string that when read evaluates to the same value.
509 Not all types of values are supported."
510 (let* ((print-escape-newlines t
)
511 (float-output-format nil
)
512 (quote.txt
(desktop-internal-v2s val
))
513 (quote (car quote.txt
))
514 (txt (cdr quote.txt
)))
519 ;; ----------------------------------------------------------------------------
520 (defun desktop-outvar (varspec)
521 "Output a setq statement for variable VAR to the desktop file.
522 The argument VARSPEC may be the variable name VAR (a symbol),
523 or a cons cell of the form (VAR . MAX-SIZE),
524 which means to truncate VAR's value to at most MAX-SIZE elements
525 \(if the value is a list) before saving the value."
528 (setq var
(car varspec
) size
(cdr varspec
))
532 (if (and (integerp size
)
535 (desktop-truncate (eval var
) size
))
539 (desktop-value-to-string (symbol-value var
))
542 ;; ----------------------------------------------------------------------------
543 (defun desktop-save-buffer-p (filename bufname mode
&rest dummy
)
544 "Return t if the desktop should record a particular buffer for next startup.
545 FILENAME is the visited file name, BUFNAME is the buffer name, and
546 MODE is the major mode."
547 (let ((case-fold-search nil
))
548 (and (not (string-match desktop-buffers-not-to-save bufname
))
549 (not (memq mode desktop-modes-not-to-save
))
551 (not (string-match desktop-files-not-to-save filename
)))
552 (and (eq mode
'dired-mode
)
554 (set-buffer (get-buffer bufname
))
555 (not (string-match desktop-files-not-to-save
556 default-directory
))))
558 (memq mode desktop-buffer-modes-to-save
))))))
560 ;; ----------------------------------------------------------------------------
561 (defun desktop-file-name (filename dirname
)
562 "Convert FILENAME to format specified in `desktop-file-name-format'.
563 DIRNAME must be the directory in which the desktop file will be saved."
566 ((eq desktop-file-name-format
'tilde
)
567 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
569 ((file-name-absolute-p relative-name
) relative-name
)
570 ((string= "./" relative-name
) "~/")
571 ((string= "." relative-name
) "~")
572 (t (concat "~/" relative-name
)))))
573 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
574 (t (expand-file-name filename
))))
576 ;; ----------------------------------------------------------------------------
577 (defun desktop-save (dirname)
578 "Save the desktop in a desktop file.
579 Parameter DIRNAME specifies where to save the desktop file.
580 See also `desktop-base-file-name'."
581 (interactive "DDirectory to save desktop file in: ")
582 (run-hooks 'desktop-save-hook
)
583 (setq dirname
(file-name-as-directory (expand-file-name dirname
)))
585 (let ((filename (expand-file-name desktop-base-file-name dirname
))
592 (desktop-file-name (buffer-file-name) dirname
)
604 (let ((special (assq mim desktop-minor-mode-table
)))
605 (if special
(cadr special
) mim
))
607 (mapcar #'car minor-mode-alist
))
610 (list (mark t
) mark-active
)
612 (run-hook-with-args-until-success 'desktop-buffer-misc-functions
)
613 (let ((locals desktop-locals-to-save
)
614 (loclist (buffer-local-variables))
617 (let ((here (assq (car locals
) loclist
)))
619 (setq ll
(cons here ll
))
620 (when (member (car locals
) loclist
)
621 (setq ll
(cons (car locals
) ll
)))))
622 (setq locals
(cdr locals
)))
625 (buf (get-buffer-create "*desktop*")))
630 ";; -*- coding: emacs-mule; -*-\n"
632 ";; Created " (current-time-string) "\n"
633 ";; Desktop file format version " desktop-file-version
"\n"
634 ";; Emacs version " emacs-version
"\n\n"
635 ";; Global section:\n")
636 (mapcar (function desktop-outvar
) desktop-globals-to-save
)
637 (if (memq 'kill-ring desktop-globals-to-save
)
639 "(setq kill-ring-yank-pointer (nthcdr "
640 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
643 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
647 (if (apply 'desktop-save-buffer-p l
)
649 (insert "(desktop-create-buffer " desktop-file-version
)
653 (insert "\n " (desktop-value-to-string e
))))
657 (setq default-directory dirname
)
658 (when (file-exists-p filename
) (delete-file filename
))
659 (let ((coding-system-for-write 'emacs-mule
))
660 (write-region (point-min) (point-max) filename nil
'nomessage
))))
661 (setq desktop-dirname dirname
))
663 ;; ----------------------------------------------------------------------------
664 (defun desktop-remove ()
665 "Delete desktop file in `desktop-dirname'.
666 This function also sets `desktop-dirname' to nil."
668 (when desktop-dirname
669 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname
)))
670 (setq desktop-dirname nil
)
671 (when (file-exists-p filename
)
672 (delete-file filename
)))))
674 ;; ----------------------------------------------------------------------------
676 (defun desktop-read (&optional dirname
)
677 "Read and process the desktop file in directory DIRNAME.
678 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
679 directories listed in `desktop-path'. If a desktop file is found, it
680 is processed and `desktop-after-read-hook' is run. If no desktop file
681 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
682 This function is a no-op when Emacs is running in batch mode.
683 It returns t if a desktop file was loaded, nil otherwise."
685 (unless noninteractive
686 (setq desktop-dirname
687 (file-name-as-directory
690 ;; If DIRNAME is specified, use it.
691 (and (< 0 (length dirname
)) dirname
)
692 ;; Otherwise search desktop file in desktop-path.
693 (let ((dirs desktop-path
))
698 (file-exists-p (expand-file-name desktop-base-file-name
(car dirs
)))))
699 (setq dirs
(cdr dirs
)))
700 (and dirs
(car dirs
)))
701 ;; If not found and `desktop-path' is non-nil, use its first element.
702 (and desktop-path
(car desktop-path
))
703 ;; Default: Home directory.
705 (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))
706 ;; Desktop file found, process it.
707 (let ((desktop-first-buffer nil
))
708 ;; Evaluate desktop buffer.
709 (load (expand-file-name desktop-base-file-name desktop-dirname
) t t t
)
710 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
711 ;; We want buffers existing prior to evaluating the desktop (and not reused)
712 ;; to be placed at the end of the buffer list, so we move them here.
714 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
715 (switch-to-buffer (car (buffer-list)))
716 (run-hooks 'desktop-delay-hook
)
717 (setq desktop-delay-hook nil
)
718 (run-hooks 'desktop-after-read-hook
)
719 (message "Desktop loaded.")
721 ;; No desktop file found.
723 (let ((default-directory desktop-dirname
))
724 (run-hooks 'desktop-no-desktop-file-hook
))
725 (message "No desktop file.")
728 ;; ----------------------------------------------------------------------------
729 ;; Maintained for backward compatibility
731 (defun desktop-load-default ()
732 "Load the `default' start-up library manually.
733 Also inhibit further loading of it."
734 (if (not inhibit-default-init
) ; safety check
737 (setq inhibit-default-init t
))))
738 (make-obsolete 'desktop-load-default
'desktop-save-mode
)
740 ;; ----------------------------------------------------------------------------
742 (defun desktop-change-dir (dirname)
743 "Change to desktop saved in DIRNAME.
744 Kill the desktop as specified by variables `desktop-save-mode' and
745 `desktop-save', then clear the desktop and load the desktop file in
747 (interactive "DChange to directory: ")
748 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
751 (desktop-read dirname
))
753 ;; ----------------------------------------------------------------------------
755 (defun desktop-save-in-desktop-dir ()
756 "Save the desktop in directory `desktop-dirname'."
759 (desktop-save desktop-dirname
)
760 (call-interactively 'desktop-save
))
761 (message "Desktop saved in %s" desktop-dirname
))
763 ;; ----------------------------------------------------------------------------
765 (defun desktop-revert ()
766 "Revert to the last loaded desktop."
768 (unless desktop-dirname
769 (error "Unknown desktop directory"))
770 (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname
))
771 (error "No desktop file found"))
773 (desktop-read desktop-dirname
))
775 ;; ----------------------------------------------------------------------------
776 ;; Note: the following functions use the dynamic variable binding in Lisp.
779 (eval-when-compile ; Just to silence the byte compiler
780 (defvar desktop-file-version
)
781 (defvar desktop-buffer-file-name
)
782 (defvar desktop-buffer-name
)
783 (defvar desktop-buffer-major-mode
)
784 (defvar desktop-buffer-minor-modes
)
785 (defvar desktop-buffer-point
)
786 (defvar desktop-buffer-mark
)
787 (defvar desktop-buffer-read-only
)
788 (defvar desktop-buffer-misc
)
789 (defvar desktop-buffer-locals
)
792 (defun desktop-buffer-info-misc-data ()
793 (if (eq major-mode
'Info-mode
)
794 (list Info-current-file
797 ;; ----------------------------------------------------------------------------
798 (defun desktop-buffer-dired-misc-data ()
799 (when (eq major-mode
'dired-mode
)
800 (eval-when-compile (defvar dirname
))
802 ;; Value of `dired-directory'.
803 (if (consp dired-directory
)
804 ;; Directory name followed by list of files.
805 (cons (desktop-file-name (car dired-directory
) dirname
) (cdr dired-directory
))
806 ;; Directory name, optionally with with shell wildcard.
807 (desktop-file-name dired-directory dirname
))
808 ;; Subdirectories in `dired-subdir-alist'.
812 (function (lambda (f) (desktop-file-name (car f
) dirname
)))
813 dired-subdir-alist
))))))
815 ;; ----------------------------------------------------------------------------
816 (defun desktop-buffer-info () "Load an info file."
817 (if (eq 'Info-mode desktop-buffer-major-mode
)
819 (let ((first (nth 0 desktop-buffer-misc
))
820 (second (nth 1 desktop-buffer-misc
)))
821 (when (and first second
)
824 (Info-find-node first second
))
825 (current-buffer))))))
827 ;; ----------------------------------------------------------------------------
828 (eval-when-compile (defvar rmail-buffer
)) ; Just to silence the byte compiler.
829 (defun desktop-buffer-rmail () "Load an RMAIL file."
830 (if (eq 'rmail-mode desktop-buffer-major-mode
)
831 (condition-case error
832 (progn (rmail-input desktop-buffer-file-name
)
833 (if (eq major-mode
'rmail-mode
)
837 (kill-buffer (current-buffer))
840 ;; ----------------------------------------------------------------------------
841 (defun desktop-buffer-mh () "Load a folder in the mh system."
842 (if (eq 'mh-folder-mode desktop-buffer-major-mode
)
845 (mh-visit-folder desktop-buffer-name
)
848 ;; ----------------------------------------------------------------------------
849 (defun desktop-buffer-dired () "Load a directory using dired."
850 (if (eq 'dired-mode desktop-buffer-major-mode
)
851 ;; First element of `desktop-buffer-misc' is the value of `dired-directory'.
852 ;; This value is a directory name, optionally with with shell wildcard or
853 ;; a directory name followed by list of files.
854 (let* ((dired-dir (car desktop-buffer-misc
))
855 (dir (if (consp dired-dir
) (car dired-dir
) dired-dir
)))
856 (if (file-directory-p (file-name-directory dir
))
859 ;; The following elements of `desktop-buffer-misc' are the keys
860 ;; from `dired-subdir-alist'.
861 (mapcar 'dired-maybe-insert-subdir
(cdr desktop-buffer-misc
))
863 (message "Directory %s no longer exists." dir
)
867 ;; ----------------------------------------------------------------------------
868 (defun desktop-buffer-file ()
870 (if desktop-buffer-file-name
871 (if (or (file-exists-p desktop-buffer-file-name
)
872 (and desktop-missing-file-warning
874 "File \"%s\" no longer exists. Re-create? "
875 desktop-buffer-file-name
))))
876 (let* ((auto-insert nil
) ; Disable auto insertion
877 (coding-system-for-read
878 (or coding-system-for-read
879 (cdr (assq 'buffer-file-coding-system
880 desktop-buffer-locals
))))
881 (buf (find-file-noselect desktop-buffer-file-name
)))
883 (switch-to-buffer buf
)
884 (error (pop-to-buffer buf
)))
885 (and (not (eq major-mode desktop-buffer-major-mode
))
886 (functionp desktop-buffer-major-mode
)
887 (funcall desktop-buffer-major-mode
))
891 ;; ----------------------------------------------------------------------------
892 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
895 (eval-when-compile ; Just to silence the byte compiler
896 (defvar desktop-first-buffer
) ;; Dynamically bound in `desktop-read'
899 (defun desktop-create-buffer (
901 desktop-buffer-file-name
903 desktop-buffer-major-mode
904 desktop-buffer-minor-modes
907 desktop-buffer-read-only
910 desktop-buffer-locals
)
911 ;; To make desktop files with relative file names possible, we cannot
912 ;; allow `default-directory' to change. Therefore we save current buffer.
915 (buffer-list (buffer-list))
916 (hlist desktop-buffer-handlers
)
920 ;; Call desktop-buffer-handlers to create buffer.
921 (while (and (not result
) hlist
)
922 (setq handler
(car hlist
))
923 (setq result
(funcall handler
))
924 (setq hlist
(cdr hlist
)))
925 (unless (bufferp result
) (setq result nil
))
926 ;; Restore buffer list order with new buffer at end. Don't change
927 ;; the order for old desktop files (old desktop module behaviour).
928 (unless (< desktop-file-version
206)
929 (mapcar 'bury-buffer buffer-list
)
930 (when result
(bury-buffer result
)))
932 (unless (or desktop-first-buffer
(< desktop-file-version
206))
933 (setq desktop-first-buffer result
))
935 (unless (equal (buffer-name) desktop-buffer-name
)
936 (rename-buffer desktop-buffer-name
))
939 ;; backwards compatible
940 (equal '(t) desktop-buffer-minor-modes
)
942 (equal '(nil) desktop-buffer-minor-modes
)
946 #'(lambda (minor-mode)
947 (when (functionp minor-mode
) (funcall minor-mode
1)))
948 desktop-buffer-minor-modes
)))
949 ;; Even though point and mark are non-nil when written by `desktop-save'
950 ;; they may be modified by handlers wanting to set point or mark themselves.
951 (when desktop-buffer-point
(goto-char desktop-buffer-point
))
952 (when desktop-buffer-mark
953 (if (consp desktop-buffer-mark
)
955 (set-mark (car desktop-buffer-mark
))
956 (setq mark-active
(car (cdr desktop-buffer-mark
))))
957 (set-mark desktop-buffer-mark
)))
958 ;; Never override file system if the file really is read-only marked.
959 (if desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
960 (while desktop-buffer-locals
961 (let ((this (car desktop-buffer-locals
)))
963 ;; an entry of this form `(symbol . value)'
965 (make-local-variable (car this
))
966 (set (car this
) (cdr this
)))
967 ;; an entry of the form `symbol'
968 (make-local-variable this
)
970 (setq desktop-buffer-locals
(cdr desktop-buffer-locals
)))))))
972 ;; ----------------------------------------------------------------------------
973 ;; Backward compatibility -- update parameters to 205 standards.
974 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
975 desktop-buffer-major-mode
976 mim pt mk ro tl fc cfs cr desktop-buffer-misc
)
977 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
978 desktop-buffer-major-mode
(cdr mim
) pt mk ro
980 (list (cons 'truncate-lines tl
)
981 (cons 'fill-column fc
)
982 (cons 'case-fold-search cfs
)
983 (cons 'case-replace cr
)
984 (cons 'overwrite-mode
(car mim
)))))
986 ;; ----------------------------------------------------------------------------
987 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
988 ;; command line, we do the rest of what it takes to use desktop, but do it
989 ;; after finishing loading the init file.
990 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
991 ;; functions are processed after `after-init-hook'.
995 (let ((key "--no-desktop"))
996 (if (member key command-line-args
)
997 (delete key command-line-args
)
998 (when desktop-save-mode
(desktop-read))))))
1002 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1003 ;;; desktop.el ends here