(texinfo-menu-copy-old-description): do not copy `@detailmenu' or
[emacs.git] / lisp / desktop.el
blob0cff5e5a21f0436d9a7238d5e7f02620ad3bdf50
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)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; 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.
28 ;;; Commentary:
30 ;; Save the Desktop, i.e.,
31 ;; - some global variables
32 ;; - the list of buffers with associated files. For each buffer also
33 ;; - the major mode
34 ;; - the default directory
35 ;; - the point
36 ;; - the mark & mark-active
37 ;; - buffer-read-only
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
60 ;; are not the same.
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 ;; ---------------------------------------------------------------------------
78 ;; TODO:
80 ;; Save window configuration.
81 ;; Recognize more minor modes.
82 ;; Save mark rings.
84 ;;; Code:
86 ;; Make the compilation more silent
87 (eval-when-compile
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."
103 :group 'frames)
105 ;;;###autoload
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
110 desktop is saved."
111 :global t
112 :group 'desktop)
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.
121 Possible values are:
122 t -- always save.
123 ask -- always ask.
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.
127 nil -- never 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."
131 :type '(choice
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))
138 :group 'desktop)
140 (defcustom desktop-base-file-name
141 (convert-standard-filename ".emacs.desktop")
142 "Name of file for Emacs desktop, excluding the directory part."
143 :type 'file
144 :group 'desktop)
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)
151 :group 'desktop)
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."
156 :type 'boolean
157 :group 'desktop)
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."
162 :type 'hook
163 :group 'desktop)
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."
168 :type 'hook
169 :group 'desktop)
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."
174 :type 'hook
175 :group 'desktop)
177 (defcustom desktop-globals-to-save '(
178 desktop-missing-file-warning
179 tags-file-name
180 tags-table-list
181 search-ring
182 regexp-search-ring
183 register-alist)
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)))
190 :group 'desktop)
192 (defcustom desktop-globals-to-clear '(
193 kill-ring
194 kill-ring-yank-pointer
195 search-ring
196 search-ring-yank-pointer
197 regexp-search-ring
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)))
204 :group 'desktop)
206 (defcustom desktop-clear-preserve-buffers-regexp
207 "^\\(\\*scratch\\*\\|\\*Messages\\*\\|\\*tramp/.+\\*\\)$"
208 "Regexp identifying buffers that `desktop-clear' should not delete."
209 :type 'regexp
210 :group 'desktop)
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)
218 :group 'desktop)
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.
224 truncate-lines
225 case-fold-search
226 case-replace
227 fill-column
228 overwrite-mode
229 change-log-default-name
230 line-number-mode
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)
235 :group 'desktop)
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."
244 :type 'regexp
245 :group 'desktop)
247 ;; Skip tramp and ange-ftp files
248 (defcustom desktop-files-not-to-save
249 "^/[^/:]*:"
250 "Regexp identifying files whose buffers are to be excluded from saving."
251 :type 'regexp
252 :group 'desktop)
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)
260 :group 'desktop)
262 (defcustom desktop-modes-not-to-save nil
263 "List of major modes whose buffers should not be saved."
264 :type '(repeat symbol)
265 :group 'desktop)
267 (defcustom desktop-file-name-format 'absolute
268 "*Format in which desktop file names should be saved.
269 Possible values are:
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))
274 :group 'desktop)
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)
293 :group 'desktop)
295 (defcustom desktop-buffer-handlers
296 '(desktop-buffer-dired
297 desktop-buffer-rmail
298 desktop-buffer-mh
299 desktop-buffer-info
300 desktop-buffer-file)
301 "*Functions called by `desktop-read' in order to create a buffer.
302 The functions are called without explicit parameters but can use the
303 following variables:
305 desktop-file-version
306 desktop-buffer-file-name
307 desktop-buffer-name
308 desktop-buffer-major-mode
309 desktop-buffer-minor-modes
310 desktop-buffer-point
311 desktop-buffer-mark
312 desktop-buffer-read-only
313 desktop-buffer-misc
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)
320 :group 'desktop)
322 (put 'desktop-buffer-handlers 'risky-local-variable t)
324 (defcustom desktop-minor-mode-table
325 '((auto-fill-function auto-fill-mode)
326 (vc-mode nil))
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
334 this table."
335 :type 'sexp
336 :group 'desktop)
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)))
355 (if (consp here)
356 (setcdr here nil))))
358 ;; ----------------------------------------------------------------------------
359 (defun desktop-clear ()
360 "Empty the Desktop.
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'."
365 (interactive)
366 (dolist (var desktop-globals-to-clear)
367 (if (symbolp var)
368 (eval `(setq-default ,var nil))
369 (eval `(setq-default ,(car var) ,(cdr var)))))
370 (let ((buffers (buffer-list)))
371 (while buffers
372 (let ((bufname (buffer-name (car buffers))))
374 (null bufname)
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."
390 (when
391 (and
392 desktop-save-mode
393 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
395 (eq desktop-save t)
396 (and exists (memq desktop-save '(ask-if-new if-exists)))
397 (and
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
405 (expand-file-name
406 (call-interactively
407 (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
408 (condition-case err
409 (desktop-save desktop-dirname)
410 (file-error
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))
417 (car args)
418 (setq args (nreverse args))
419 (let ((value (cons (nth 1 args) (car args))))
420 (setq args (cdr (cdr args)))
421 (while args
422 (setq value (cons (car args) value))
423 (setq args (cdr args)))
424 value)))
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)."
432 (cond
433 ((or (numberp val) (null val) (eq t val))
434 (cons 'may (prin1-to-string val)))
435 ((stringp 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))))
440 ((symbolp val)
441 (cons 'must (prin1-to-string val)))
442 ((vectorp val)
443 (let* ((special nil)
444 (pass1 (mapcar
445 (lambda (el)
446 (let ((res (desktop-internal-v2s el)))
447 (if (null (car res))
448 (setq special t))
449 res))
450 val)))
451 (if special
452 (cons nil (concat "(vector "
453 (mapconcat (lambda (el)
454 (if (eq (car el) 'must)
455 (concat "'" (cdr el))
456 (cdr el)))
457 pass1
458 " ")
459 ")"))
460 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
461 ((consp val)
462 (let ((p val)
463 newlist
464 use-list*
465 anynil)
466 (while (consp p)
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)))
470 (setq p (cdr p)))
471 (if p
472 (let ((last (desktop-internal-v2s p))
473 (el (car newlist)))
474 (or anynil (setq anynil (null (car last))))
475 (or anynil
476 (setq newlist (cons '(must . ".") newlist)))
477 (setq use-list* t)
478 (setq newlist (cons last newlist))))
479 (setq newlist (nreverse newlist))
480 (if anynil
481 (cons nil
482 (concat (if use-list* "(desktop-list* " "(list ")
483 (mapconcat (lambda (el)
484 (if (eq (car el) 'must)
485 (concat "'" (cdr el))
486 (cdr el)))
487 newlist
488 " ")
489 ")"))
490 (cons 'must
491 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
492 ((subrp val)
493 (cons nil (concat "(symbol-function '"
494 (substring (prin1-to-string val) 7 -1)
495 ")")))
496 ((markerp val)
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)"))))
503 (t ; save as text
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)))
515 (if (eq quote 'must)
516 (concat "'" txt)
517 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."
526 (let (var size)
527 (if (consp varspec)
528 (setq var (car varspec) size (cdr varspec))
529 (setq var varspec))
530 (if (boundp var)
531 (progn
532 (if (and (integerp size)
533 (> size 0)
534 (listp (eval var)))
535 (desktop-truncate (eval var) size))
536 (insert "(setq "
537 (symbol-name var)
539 (desktop-value-to-string (symbol-value var))
540 ")\n")))))
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))
550 (or (and filename
551 (not (string-match desktop-files-not-to-save filename)))
552 (and (eq mode 'dired-mode)
553 (save-excursion
554 (set-buffer (get-buffer bufname))
555 (not (string-match desktop-files-not-to-save
556 default-directory))))
557 (and (null filename)
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."
564 (cond
565 ((not filename) nil)
566 ((eq desktop-file-name-format 'tilde)
567 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
568 (cond
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)))
584 (save-excursion
585 (let ((filename (expand-file-name desktop-base-file-name dirname))
586 (info
587 (mapcar
588 (function
589 (lambda (b)
590 (set-buffer b)
591 (list
592 (desktop-file-name (buffer-file-name) dirname)
593 (buffer-name)
594 major-mode
595 ;; minor modes
596 (let (ret)
597 (mapcar
598 #'(lambda (mim)
599 (and
600 (boundp mim)
601 (symbol-value mim)
602 (setq ret
603 (cons
604 (let ((special (assq mim desktop-minor-mode-table)))
605 (if special (cadr special) mim))
606 ret))))
607 (mapcar #'car minor-mode-alist))
608 ret)
609 (point)
610 (list (mark t) mark-active)
611 buffer-read-only
612 (run-hook-with-args-until-success 'desktop-buffer-misc-functions)
613 (let ((locals desktop-locals-to-save)
614 (loclist (buffer-local-variables))
615 (ll))
616 (while locals
617 (let ((here (assq (car locals) loclist)))
618 (if here
619 (setq ll (cons here ll))
620 (when (member (car locals) loclist)
621 (setq ll (cons (car locals) ll)))))
622 (setq locals (cdr locals)))
623 ll))))
624 (buffer-list)))
625 (buf (get-buffer-create "*desktop*")))
626 (set-buffer buf)
627 (erase-buffer)
629 (insert
630 ";; -*- coding: emacs-mule; -*-\n"
631 desktop-header
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)
638 (insert
639 "(setq kill-ring-yank-pointer (nthcdr "
640 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
641 " kill-ring))\n"))
643 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
644 (mapcar
645 (function
646 (lambda (l)
647 (if (apply 'desktop-save-buffer-p l)
648 (progn
649 (insert "(desktop-create-buffer " desktop-file-version)
650 (mapcar
651 (function
652 (lambda (e)
653 (insert "\n " (desktop-value-to-string e))))
655 (insert ")\n\n")))))
656 info)
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."
667 (interactive)
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 ;; ----------------------------------------------------------------------------
675 ;;;###autoload
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."
684 (interactive)
685 (unless noninteractive
686 (setq desktop-dirname
687 (file-name-as-directory
688 (expand-file-name
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))
694 (while
695 (and
696 dirs
697 (not
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.
704 "~"))))
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.
713 (mapcar 'bury-buffer
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.
722 (desktop-clear)
723 (let ((default-directory desktop-dirname))
724 (run-hooks 'desktop-no-desktop-file-hook))
725 (message "No desktop file.")
726 nil)))
728 ;; ----------------------------------------------------------------------------
729 ;; Maintained for backward compatibility
730 ;;;###autoload
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
735 (progn
736 (load "default" t t)
737 (setq inhibit-default-init t))))
738 (make-obsolete 'desktop-load-default 'desktop-save-mode)
740 ;; ----------------------------------------------------------------------------
741 ;;;###autoload
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
746 directory DIRNAME."
747 (interactive "DChange to directory: ")
748 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
749 (desktop-kill)
750 (desktop-clear)
751 (desktop-read dirname))
753 ;; ----------------------------------------------------------------------------
754 ;;;###autoload
755 (defun desktop-save-in-desktop-dir ()
756 "Save the desktop in directory `desktop-dirname'."
757 (interactive)
758 (if desktop-dirname
759 (desktop-save desktop-dirname)
760 (call-interactively 'desktop-save))
761 (message "Desktop saved in %s" desktop-dirname))
763 ;; ----------------------------------------------------------------------------
764 ;;;###autoload
765 (defun desktop-revert ()
766 "Revert to the last loaded desktop."
767 (interactive)
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"))
772 (desktop-clear)
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
795 Info-current-node)))
797 ;; ----------------------------------------------------------------------------
798 (defun desktop-buffer-dired-misc-data ()
799 (when (eq major-mode 'dired-mode)
800 (eval-when-compile (defvar dirname))
801 (cons
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'.
809 (cdr
810 (nreverse
811 (mapcar
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)
818 (progn
819 (let ((first (nth 0 desktop-buffer-misc))
820 (second (nth 1 desktop-buffer-misc)))
821 (when (and first second)
822 (require 'info)
823 (with-no-warnings
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)
834 (current-buffer)
835 rmail-buffer))
836 (file-locked
837 (kill-buffer (current-buffer))
838 'ignored))))
840 ;; ----------------------------------------------------------------------------
841 (defun desktop-buffer-mh () "Load a folder in the mh system."
842 (if (eq 'mh-folder-mode desktop-buffer-major-mode)
843 (with-no-warnings
844 (mh-find-path)
845 (mh-visit-folder desktop-buffer-name)
846 (current-buffer))))
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))
857 (progn
858 (dired dired-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))
862 (current-buffer))
863 (message "Directory %s no longer exists." dir)
864 (sit-for 1)
865 'ignored))))
867 ;; ----------------------------------------------------------------------------
868 (defun desktop-buffer-file ()
869 "Load a file."
870 (if desktop-buffer-file-name
871 (if (or (file-exists-p desktop-buffer-file-name)
872 (and desktop-missing-file-warning
873 (y-or-n-p (format
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)))
882 (condition-case nil
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))
888 buf)
889 'ignored)))
891 ;; ----------------------------------------------------------------------------
892 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
893 ;; only.
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 (
900 desktop-file-version
901 desktop-buffer-file-name
902 desktop-buffer-name
903 desktop-buffer-major-mode
904 desktop-buffer-minor-modes
905 desktop-buffer-point
906 desktop-buffer-mark
907 desktop-buffer-read-only
908 desktop-buffer-misc
909 &optional
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.
913 (save-current-buffer
914 (let (
915 (buffer-list (buffer-list))
916 (hlist desktop-buffer-handlers)
917 (result)
918 (handler)
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)))
931 (when result
932 (unless (or desktop-first-buffer (< desktop-file-version 206))
933 (setq desktop-first-buffer result))
934 (set-buffer result)
935 (unless (equal (buffer-name) desktop-buffer-name)
936 (rename-buffer desktop-buffer-name))
937 ;; minor modes
938 (cond (
939 ;; backwards compatible
940 (equal '(t) desktop-buffer-minor-modes)
941 (auto-fill-mode 1))(
942 (equal '(nil) desktop-buffer-minor-modes)
943 (auto-fill-mode 0))(
945 (mapcar
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)
954 (progn
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)))
962 (if (consp this)
963 ;; an entry of this form `(symbol . value)'
964 (progn
965 (make-local-variable (car this))
966 (set (car this) (cdr this)))
967 ;; an entry of the form `symbol'
968 (make-local-variable this)
969 (makunbound 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
979 desktop-buffer-misc
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'.
992 (add-hook
993 'after-init-hook
994 '(lambda ()
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))))))
1000 (provide 'desktop)
1002 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
1003 ;;; desktop.el ends here