(desktop-buffer-misc-data-function): Rename to desktop-save-buffer and change docstring.
[emacs.git] / lisp / desktop.el
blobe9b130388a29a684c568e4c5e119e6bba26c19b4
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 (defvar desktop-file-version "206"
87 "Version number of desktop file format.
88 Written into the desktop file and used at desktop read to provide
89 backward compatibility.")
91 ;; ----------------------------------------------------------------------------
92 ;; USER OPTIONS -- settings you might want to play with.
93 ;; ----------------------------------------------------------------------------
95 (defgroup desktop nil
96 "Save status of Emacs when you exit."
97 :group 'frames)
99 ;;;###autoload
100 (define-minor-mode desktop-save-mode
101 "Toggle desktop saving mode.
102 With numeric ARG, turn desktop saving on if ARG is positive, off
103 otherwise. See variable `desktop-save' for a description of when the
104 desktop is saved."
105 :global t
106 :group 'desktop)
108 ;; Maintained for backward compatibility
109 (defvaralias 'desktop-enable 'desktop-save-mode)
110 (make-obsolete-variable 'desktop-enable 'desktop-save-mode)
112 (defcustom desktop-save 'ask-if-new
113 "*Specifies whether the desktop should be saved when it is killed.
114 A desktop is killed when the user changes desktop or quits Emacs.
115 Possible values are:
116 t -- always save.
117 ask -- always ask.
118 ask-if-new -- ask if no desktop file exists, otherwise just save.
119 ask-if-exists -- ask if desktop file exists, otherwise don't save.
120 if-exists -- save if desktop file exists, otherwise don't save.
121 nil -- never save.
122 The desktop is never saved when `desktop-save-mode' is nil.
123 The variables `desktop-directory' and `desktop-base-file-name'
124 determine where the desktop is saved."
125 :type '(choice
126 (const :tag "Always save" t)
127 (const :tag "Always ask" ask)
128 (const :tag "Ask if desktop file is new, else do save" ask-if-new)
129 (const :tag "Ask if desktop file exists, else don't save" ask-if-exists)
130 (const :tag "Save if desktop file exists, else don't" if-exists)
131 (const :tag "Never save" nil))
132 :group 'desktop)
134 (defcustom desktop-base-file-name
135 (convert-standard-filename ".emacs.desktop")
136 "Name of file for Emacs desktop, excluding the directory part."
137 :type 'file
138 :group 'desktop)
139 (defvaralias 'desktop-basefilename 'desktop-base-file-name)
141 (defcustom desktop-path '("." "~")
142 "List of directories to search for the desktop file.
143 The base name of the file is specified in `desktop-base-file-name'."
144 :type '(repeat directory)
145 :group 'desktop)
147 (defcustom desktop-missing-file-warning nil
148 "*If non-nil then `desktop-read' asks if a non-existent file should be recreated.
149 Also pause for a moment to display message about errors signaled in
150 `desktop-buffer-mode-handlers'.
152 If nil, just print error messages in the message buffer."
153 :type 'boolean
154 :group 'desktop)
156 (defcustom desktop-no-desktop-file-hook nil
157 "Normal hook run when `desktop-read' can't find a desktop file.
158 May e.g. be used to show a dired buffer."
159 :type 'hook
160 :group 'desktop)
162 (defcustom desktop-after-read-hook nil
163 "Normal hook run after a successful `desktop-read'.
164 May e.g. be used to show a buffer list."
165 :type 'hook
166 :group 'desktop)
168 (defcustom desktop-save-hook nil
169 "Normal hook run before the desktop is saved in a desktop file.
170 This is useful for truncating history lists, for example."
171 :type 'hook
172 :group 'desktop)
174 (defcustom desktop-globals-to-save '(
175 desktop-missing-file-warning
176 tags-file-name
177 tags-table-list
178 search-ring
179 regexp-search-ring
180 register-alist)
181 "List of global variables saved by `desktop-save'.
182 An element may be variable name (a symbol) or a cons cell of the form
183 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
184 MAX-SIZE elements (if the value is a list) before saving the value.
185 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
186 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
187 :group 'desktop)
189 (defcustom desktop-globals-to-clear '(
190 kill-ring
191 kill-ring-yank-pointer
192 search-ring
193 search-ring-yank-pointer
194 regexp-search-ring
195 regexp-search-ring-yank-pointer)
196 "List of global variables to clear by `desktop-clear'.
197 An element may be variable name (a symbol) or a cons cell of the form
198 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
199 to the value obtained by evaluateing FORM."
200 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
201 :group 'desktop)
203 (defcustom desktop-clear-preserve-buffers-regexp
204 "^\\(\\*scratch\\*\\|\\*Messages\\*\\|\\*tramp/.+\\*\\)$"
205 "Regexp identifying buffers that `desktop-clear' should not delete."
206 :type 'regexp
207 :group 'desktop)
209 ;; Maintained for backward compatibility
210 (defcustom desktop-clear-preserve-buffers nil
211 "*List of buffer names that `desktop-clear' should not delete.
212 This variable is maintained for backward compatibility only."
213 :type '(repeat string)
214 :group 'desktop)
215 (make-obsolete-variable 'desktop-clear-preserve-buffers
216 'desktop-clear-preserve-buffers-regexp)
218 (defcustom desktop-locals-to-save '(
219 desktop-locals-to-save ; Itself! Think it over.
220 truncate-lines
221 case-fold-search
222 case-replace
223 fill-column
224 overwrite-mode
225 change-log-default-name
226 line-number-mode
227 buffer-file-coding-system)
228 "List of local variables to save for each buffer.
229 The variables are saved only when they really are local."
230 :type '(repeat symbol)
231 :group 'desktop)
232 (make-variable-buffer-local 'desktop-locals-to-save)
234 ;; We skip .log files because they are normally temporary.
235 ;; (ftp) files because they require passwords and whatnot.
236 ;; TAGS files to save time (tags-file-name is saved instead).
237 (defcustom desktop-buffers-not-to-save
238 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
239 "Regexp identifying buffers that are to be excluded from saving."
240 :type 'regexp
241 :group 'desktop)
243 ;; Skip tramp and ange-ftp files
244 (defcustom desktop-files-not-to-save
245 "^/[^/:]*:"
246 "Regexp identifying files whose buffers are to be excluded from saving."
247 :type 'regexp
248 :group 'desktop)
250 (defcustom desktop-modes-not-to-save nil
251 "List of major modes whose buffers should not be saved."
252 :type '(repeat symbol)
253 :group 'desktop)
255 (defcustom desktop-file-name-format 'absolute
256 "*Format in which desktop file names should be saved.
257 Possible values are:
258 absolute -- Absolute file name.
259 tilde -- Relative to ~.
260 local -- Relative to directory of desktop file."
261 :type '(choice (const absolute) (const tilde) (const local))
262 :group 'desktop)
264 ;;;###autoload
265 (defvar desktop-save-buffer nil
266 "When non-nil, save buffer status in desktop file.
267 This variable becomes buffer local when set.
269 If the value is a function, it called by `desktop-save' with argument
270 DESKTOP-DIRNAME to obtain auxiliary information to saved in the desktop
271 file along with the state of the buffer for which it was called.
273 When file names are returned, they should be formatted using the call
274 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
276 Later, when `desktop-read' calls a function in `desktop-buffer-mode-handlers'
277 to restore the buffer, the auxiliary information is passed as the argument
278 DESKTOP-BUFFER-MISC.")
279 (make-variable-buffer-local 'desktop-save-buffer)
280 (make-obsolete-variable 'desktop-buffer-modes-to-save
281 'desktop-save-buffer)
282 (make-obsolete-variable 'desktop-buffer-misc-functions
283 'desktop-save-buffer)
285 (defcustom desktop-buffer-mode-handlers '(
286 (dired-mode . dired-restore-desktop-buffer)
287 (rmail-mode . rmail-restore-desktop-buffer)
288 (mh-folder-mode . mh-restore-desktop-buffer)
289 (Info-mode . Info-restore-desktop-buffer))
290 "Alist of major mode specific functions to restore a desktop buffer.
291 Functions are called by `desktop-read'. List elements must have the form
292 \(MAJOR-MODE . FUNCTION).
294 Buffers with a major mode not specified here, are restored by the default
295 handler `desktop-restore-file-buffer'.
297 Handlers are called with parameters
299 desktop-buffer-file-name
300 desktop-buffer-name
301 desktop-buffer-misc
303 Furthermore, they may use the following variables:
305 desktop-file-version
306 desktop-buffer-major-mode
307 desktop-buffer-minor-modes
308 desktop-buffer-point
309 desktop-buffer-mark
310 desktop-buffer-read-only
311 desktop-buffer-locals
313 If a handler returns a buffer, then the saved mode settings
314 and variable values for that buffer are copied into it."
315 :type 'alist
316 :group 'desktop)
318 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
319 (make-obsolete-variable 'desktop-buffer-handlers
320 'desktop-buffer-mode-handlers)
322 (defcustom desktop-minor-mode-table
323 '((auto-fill-function auto-fill-mode)
324 (vc-mode nil))
325 "Table mapping minor mode variables to minor mode functions.
326 Each entry has the form (NAME RESTORE-FUNCTION).
327 NAME is the name of the buffer-local variable indicating that the minor
328 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
329 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
330 Only minor modes for which the name of the buffer-local variable
331 and the name of the minor mode function are different have to added to
332 this table."
333 :type 'sexp
334 :group 'desktop)
336 ;; ----------------------------------------------------------------------------
337 (defvar desktop-dirname nil
338 "The directory in which the desktop file should be saved.")
340 (defconst desktop-header
341 ";; --------------------------------------------------------------------------
342 ;; Desktop File for Emacs
343 ;; --------------------------------------------------------------------------
344 " "*Header to place in Desktop file.")
346 (defvar desktop-delay-hook nil
347 "Hooks run after all buffers are loaded; intended for internal use.")
349 ;; ----------------------------------------------------------------------------
350 (defun desktop-truncate (list n)
351 "Truncate LIST to at most N elements destructively."
352 (let ((here (nthcdr (1- n) list)))
353 (if (consp here)
354 (setcdr here nil))))
356 ;; ----------------------------------------------------------------------------
357 (defun desktop-clear ()
358 "Empty the Desktop.
359 This kills all buffers except for internal ones and those matching
360 `desktop-clear-preserve-buffers-regexp' or listed in
361 `desktop-clear-preserve-buffers'. Furthermore, it clears the
362 variables listed in `desktop-globals-to-clear'."
363 (interactive)
364 (dolist (var desktop-globals-to-clear)
365 (if (symbolp var)
366 (eval `(setq-default ,var nil))
367 (eval `(setq-default ,(car var) ,(cdr var)))))
368 (let ((buffers (buffer-list)))
369 (while buffers
370 (let ((bufname (buffer-name (car buffers))))
372 (null bufname)
373 (string-match desktop-clear-preserve-buffers-regexp bufname)
374 (member bufname desktop-clear-preserve-buffers)
375 ;; Don't kill buffers made for internal purposes.
376 (and (not (equal bufname "")) (eq (aref bufname 0) ?\ ))
377 (kill-buffer (car buffers))))
378 (setq buffers (cdr buffers))))
379 (delete-other-windows))
381 ;; ----------------------------------------------------------------------------
382 (add-hook 'kill-emacs-hook 'desktop-kill)
384 (defun desktop-kill ()
385 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
386 If the desktop should be saved and `desktop-dirname'
387 is nil, ask the user where to save the desktop."
388 (when
389 (and
390 desktop-save-mode
391 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
393 (eq desktop-save t)
394 (and exists (memq desktop-save '(ask-if-new if-exists)))
395 (and
397 (memq desktop-save '(ask ask-if-new))
398 (and exists (eq desktop-save 'ask-if-exists)))
399 (y-or-n-p "Save desktop? ")))))
400 (unless desktop-dirname
401 (setq desktop-dirname
402 (file-name-as-directory
403 (expand-file-name
404 (call-interactively
405 (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
406 (condition-case err
407 (desktop-save desktop-dirname)
408 (file-error
409 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
410 (signal (car err) (cdr err)))))))
412 ;; ----------------------------------------------------------------------------
413 (defun desktop-list* (&rest args)
414 (if (null (cdr args))
415 (car args)
416 (setq args (nreverse args))
417 (let ((value (cons (nth 1 args) (car args))))
418 (setq args (cdr (cdr args)))
419 (while args
420 (setq value (cons (car args) value))
421 (setq args (cdr args)))
422 value)))
424 ;; ----------------------------------------------------------------------------
425 (defun desktop-internal-v2s (value)
426 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
427 TXT is a string that when read and evaluated yields value.
428 QUOTE may be `may' (value may be quoted),
429 `must' (values must be quoted), or nil (value may not be quoted)."
430 (cond
431 ((or (numberp value) (null value) (eq t value))
432 (cons 'may (prin1-to-string value)))
433 ((stringp value)
434 (let ((copy (copy-sequence value)))
435 (set-text-properties 0 (length copy) nil copy)
436 ;; Get rid of text properties because we cannot read them
437 (cons 'may (prin1-to-string copy))))
438 ((symbolp value)
439 (cons 'must (prin1-to-string value)))
440 ((vectorp value)
441 (let* ((special nil)
442 (pass1 (mapcar
443 (lambda (el)
444 (let ((res (desktop-internal-v2s el)))
445 (if (null (car res))
446 (setq special t))
447 res))
448 value)))
449 (if special
450 (cons nil (concat "(vector "
451 (mapconcat (lambda (el)
452 (if (eq (car el) 'must)
453 (concat "'" (cdr el))
454 (cdr el)))
455 pass1
456 " ")
457 ")"))
458 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
459 ((consp value)
460 (let ((p value)
461 newlist
462 use-list*
463 anynil)
464 (while (consp p)
465 (let ((q.txt (desktop-internal-v2s (car p))))
466 (or anynil (setq anynil (null (car q.txt))))
467 (setq newlist (cons q.txt newlist)))
468 (setq p (cdr p)))
469 (if p
470 (let ((last (desktop-internal-v2s p))
471 (el (car newlist)))
472 (or anynil (setq anynil (null (car last))))
473 (or anynil
474 (setq newlist (cons '(must . ".") newlist)))
475 (setq use-list* t)
476 (setq newlist (cons last newlist))))
477 (setq newlist (nreverse newlist))
478 (if anynil
479 (cons nil
480 (concat (if use-list* "(desktop-list* " "(list ")
481 (mapconcat (lambda (el)
482 (if (eq (car el) 'must)
483 (concat "'" (cdr el))
484 (cdr el)))
485 newlist
486 " ")
487 ")"))
488 (cons 'must
489 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
490 ((subrp value)
491 (cons nil (concat "(symbol-function '"
492 (substring (prin1-to-string value) 7 -1)
493 ")")))
494 ((markerp value)
495 (let ((pos (prin1-to-string (marker-position value)))
496 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
497 (cons nil (concat "(let ((mk (make-marker)))"
498 " (add-hook 'desktop-delay-hook"
499 " (list 'lambda '() (list 'set-marker mk "
500 pos " (get-buffer " buf ")))) mk)"))))
501 (t ; save as text
502 (cons 'may "\"Unprintable entity\""))))
504 ;; ----------------------------------------------------------------------------
505 (defun desktop-value-to-string (value)
506 "Convert VALUE to a string that when read evaluates to the same value.
507 Not all types of values are supported."
508 (let* ((print-escape-newlines t)
509 (float-output-format nil)
510 (quote.txt (desktop-internal-v2s value))
511 (quote (car quote.txt))
512 (txt (cdr quote.txt)))
513 (if (eq quote 'must)
514 (concat "'" txt)
515 txt)))
517 ;; ----------------------------------------------------------------------------
518 (defun desktop-outvar (varspec)
519 "Output a setq statement for variable VAR to the desktop file.
520 The argument VARSPEC may be the variable name VAR (a symbol),
521 or a cons cell of the form (VAR . MAX-SIZE),
522 which means to truncate VAR's value to at most MAX-SIZE elements
523 \(if the value is a list) before saving the value."
524 (let (var size)
525 (if (consp varspec)
526 (setq var (car varspec) size (cdr varspec))
527 (setq var varspec))
528 (if (boundp var)
529 (progn
530 (if (and (integerp size)
531 (> size 0)
532 (listp (eval var)))
533 (desktop-truncate (eval var) size))
534 (insert "(setq "
535 (symbol-name var)
537 (desktop-value-to-string (symbol-value var))
538 ")\n")))))
540 ;; ----------------------------------------------------------------------------
541 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
542 "Return t if buffer should have its state saved in the desktop file.
543 FILENAME is the visited file name, BUFNAME is the buffer name, and
544 MODE is the major mode."
545 (let ((case-fold-search nil))
546 (and (not (string-match desktop-buffers-not-to-save bufname))
547 (not (memq mode desktop-modes-not-to-save))
548 (or (and filename
549 (not (string-match desktop-files-not-to-save filename)))
550 (and (eq mode 'dired-mode)
551 (with-current-buffer bufname
552 (not (string-match desktop-files-not-to-save
553 default-directory))))
554 (and (null filename)
555 (with-current-buffer bufname desktop-save-buffer))))))
557 ;; ----------------------------------------------------------------------------
558 (defun desktop-file-name (filename dirname)
559 "Convert FILENAME to format specified in `desktop-file-name-format'.
560 DIRNAME must be the directory in which the desktop file will be saved."
561 (cond
562 ((not filename) nil)
563 ((eq desktop-file-name-format 'tilde)
564 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
565 (cond
566 ((file-name-absolute-p relative-name) relative-name)
567 ((string= "./" relative-name) "~/")
568 ((string= "." relative-name) "~")
569 (t (concat "~/" relative-name)))))
570 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
571 (t (expand-file-name filename))))
573 ;; ----------------------------------------------------------------------------
574 (defun desktop-save (dirname)
575 "Save the desktop in a desktop file.
576 Parameter DIRNAME specifies where to save the desktop file.
577 See also `desktop-base-file-name'."
578 (interactive "DDirectory to save desktop file in: ")
579 (run-hooks 'desktop-save-hook)
580 (setq dirname (file-name-as-directory (expand-file-name dirname)))
581 (save-excursion
582 (let ((filename (expand-file-name desktop-base-file-name dirname))
583 (info
584 (mapcar
585 (function
586 (lambda (b)
587 (set-buffer b)
588 (list
589 (desktop-file-name (buffer-file-name) dirname)
590 (buffer-name)
591 major-mode
592 ;; minor modes
593 (let (ret)
594 (mapcar
595 #'(lambda (mim)
596 (and
597 (boundp mim)
598 (symbol-value mim)
599 (setq ret
600 (cons
601 (let ((special (assq mim desktop-minor-mode-table)))
602 (if special (cadr special) mim))
603 ret))))
604 (mapcar #'car minor-mode-alist))
605 ret)
606 (point)
607 (list (mark t) mark-active)
608 buffer-read-only
609 ;; Auxiliary information
610 (when (functionp desktop-save-buffer)
611 (funcall desktop-save-buffer dirname))
612 (let ((locals desktop-locals-to-save)
613 (loclist (buffer-local-variables))
614 (ll))
615 (while locals
616 (let ((here (assq (car locals) loclist)))
617 (if here
618 (setq ll (cons here ll))
619 (when (member (car locals) loclist)
620 (setq ll (cons (car locals) ll)))))
621 (setq locals (cdr locals)))
622 ll))))
623 (buffer-list)))
624 (buf (get-buffer-create "*desktop*")))
625 (set-buffer buf)
626 (erase-buffer)
628 (insert
629 ";; -*- coding: emacs-mule; -*-\n"
630 desktop-header
631 ";; Created " (current-time-string) "\n"
632 ";; Desktop file format version " desktop-file-version "\n"
633 ";; Emacs version " emacs-version "\n\n"
634 ";; Global section:\n")
635 (mapcar (function desktop-outvar) desktop-globals-to-save)
636 (if (memq 'kill-ring desktop-globals-to-save)
637 (insert
638 "(setq kill-ring-yank-pointer (nthcdr "
639 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
640 " kill-ring))\n"))
642 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
643 (mapcar
644 (function
645 (lambda (l)
646 (if (apply 'desktop-save-buffer-p l)
647 (progn
648 (insert "(desktop-create-buffer " desktop-file-version)
649 (mapcar
650 (function
651 (lambda (e)
652 (insert "\n " (desktop-value-to-string e))))
654 (insert ")\n\n")))))
655 info)
656 (setq default-directory dirname)
657 (when (file-exists-p filename) (delete-file filename))
658 (let ((coding-system-for-write 'emacs-mule))
659 (write-region (point-min) (point-max) filename nil 'nomessage))))
660 (setq desktop-dirname dirname))
662 ;; ----------------------------------------------------------------------------
663 (defun desktop-remove ()
664 "Delete desktop file in `desktop-dirname'.
665 This function also sets `desktop-dirname' to nil."
666 (interactive)
667 (when desktop-dirname
668 (let ((filename (expand-file-name desktop-base-file-name desktop-dirname)))
669 (setq desktop-dirname nil)
670 (when (file-exists-p filename)
671 (delete-file filename)))))
673 ;; ----------------------------------------------------------------------------
674 ;;;###autoload
675 (defun desktop-read (&optional dirname)
676 "Read and process the desktop file in directory DIRNAME.
677 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
678 directories listed in `desktop-path'. If a desktop file is found, it
679 is processed and `desktop-after-read-hook' is run. If no desktop file
680 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
681 This function is a no-op when Emacs is running in batch mode.
682 It returns t if a desktop file was loaded, nil otherwise."
683 (interactive)
684 (unless noninteractive
685 (setq desktop-dirname
686 (file-name-as-directory
687 (expand-file-name
689 ;; If DIRNAME is specified, use it.
690 (and (< 0 (length dirname)) dirname)
691 ;; Otherwise search desktop file in desktop-path.
692 (let ((dirs desktop-path))
693 (while
694 (and
695 dirs
696 (not
697 (file-exists-p (expand-file-name desktop-base-file-name (car dirs)))))
698 (setq dirs (cdr dirs)))
699 (and dirs (car dirs)))
700 ;; If not found and `desktop-path' is non-nil, use its first element.
701 (and desktop-path (car desktop-path))
702 ;; Default: Home directory.
703 "~"))))
704 (if (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
705 ;; Desktop file found, process it.
706 (let ((desktop-first-buffer nil)
707 (desktop-buffer-ok-count 0)
708 (desktop-buffer-fail-count 0))
709 ;; Evaluate desktop buffer.
710 (load (expand-file-name desktop-base-file-name desktop-dirname) t t t)
711 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
712 ;; We want buffers existing prior to evaluating the desktop (and not reused)
713 ;; to be placed at the end of the buffer list, so we move them here.
714 (mapcar 'bury-buffer
715 (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list))))))
716 (switch-to-buffer (car (buffer-list)))
717 (run-hooks 'desktop-delay-hook)
718 (setq desktop-delay-hook nil)
719 (run-hooks 'desktop-after-read-hook)
720 (message "Desktop: %d buffer%s restored%s."
721 desktop-buffer-ok-count
722 (if (= 1 desktop-buffer-ok-count) "" "s")
723 (if (< 0 desktop-buffer-fail-count)
724 (format ", %d failed to restore" desktop-buffer-fail-count)
725 ""))
727 ;; No desktop file found.
728 (desktop-clear)
729 (let ((default-directory desktop-dirname))
730 (run-hooks 'desktop-no-desktop-file-hook))
731 (message "No desktop file.")
732 nil)))
734 ;; ----------------------------------------------------------------------------
735 ;; Maintained for backward compatibility
736 ;;;###autoload
737 (defun desktop-load-default ()
738 "Load the `default' start-up library manually.
739 Also inhibit further loading of it."
740 (if (not inhibit-default-init) ; safety check
741 (progn
742 (load "default" t t)
743 (setq inhibit-default-init t))))
744 (make-obsolete 'desktop-load-default 'desktop-save-mode)
746 ;; ----------------------------------------------------------------------------
747 ;;;###autoload
748 (defun desktop-change-dir (dirname)
749 "Change to desktop saved in DIRNAME.
750 Kill the desktop as specified by variables `desktop-save-mode' and
751 `desktop-save', then clear the desktop and load the desktop file in
752 directory DIRNAME."
753 (interactive "DChange to directory: ")
754 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
755 (desktop-kill)
756 (desktop-clear)
757 (desktop-read dirname))
759 ;; ----------------------------------------------------------------------------
760 ;;;###autoload
761 (defun desktop-save-in-desktop-dir ()
762 "Save the desktop in directory `desktop-dirname'."
763 (interactive)
764 (if desktop-dirname
765 (desktop-save desktop-dirname)
766 (call-interactively 'desktop-save))
767 (message "Desktop saved in %s" desktop-dirname))
769 ;; ----------------------------------------------------------------------------
770 ;;;###autoload
771 (defun desktop-revert ()
772 "Revert to the last loaded desktop."
773 (interactive)
774 (unless desktop-dirname
775 (error "Unknown desktop directory"))
776 (unless (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))
777 (error "No desktop file found"))
778 (desktop-clear)
779 (desktop-read desktop-dirname))
781 ;; ----------------------------------------------------------------------------
782 (defun desktop-restore-file-buffer (desktop-buffer-file-name
783 desktop-buffer-name
784 desktop-buffer-misc)
785 "Restore a file buffer."
786 (eval-when-compile ; Just to silence the byte compiler
787 (defvar desktop-buffer-major-mode)
788 (defvar desktop-buffer-locals))
789 (if desktop-buffer-file-name
790 (if (or (file-exists-p desktop-buffer-file-name)
791 (let ((msg (format "Desktop: File \"%s\" no longer exists."
792 desktop-buffer-file-name)))
793 (if desktop-missing-file-warning
794 (y-or-n-p (concat msg " Re-create? "))
795 (message msg)
796 nil)))
797 (let* ((auto-insert nil) ; Disable auto insertion
798 (coding-system-for-read
799 (or coding-system-for-read
800 (cdr (assq 'buffer-file-coding-system
801 desktop-buffer-locals))))
802 (buf (find-file-noselect desktop-buffer-file-name)))
803 (condition-case nil
804 (switch-to-buffer buf)
805 (error (pop-to-buffer buf)))
806 (and (not (eq major-mode desktop-buffer-major-mode))
807 (functionp desktop-buffer-major-mode)
808 (funcall desktop-buffer-major-mode))
809 buf)
810 nil)))
812 ;; ----------------------------------------------------------------------------
813 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
814 ;; only.
816 (eval-when-compile ; Just to silence the byte compiler
817 (defvar desktop-first-buffer) ;; Dynamically bound in `desktop-read'
820 (defun desktop-create-buffer (
821 desktop-file-version
822 desktop-buffer-file-name
823 desktop-buffer-name
824 desktop-buffer-major-mode
825 desktop-buffer-minor-modes
826 desktop-buffer-point
827 desktop-buffer-mark
828 desktop-buffer-read-only
829 desktop-buffer-misc
830 &optional
831 desktop-buffer-locals)
832 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
833 (eval-when-compile
834 (defvar desktop-buffer-ok-count)
835 (defvar desktop-buffer-fail-count))
836 ;; To make desktop files with relative file names possible, we cannot
837 ;; allow `default-directory' to change. Therefore we save current buffer.
838 (save-current-buffer
839 (let (
840 (buffer-list (buffer-list))
841 (result
842 (condition-case err
843 (funcall (or (cdr (assq desktop-buffer-major-mode desktop-buffer-mode-handlers))
844 'desktop-restore-file-buffer)
845 desktop-buffer-file-name
846 desktop-buffer-name
847 desktop-buffer-misc)
848 (error
849 (message "Desktop: Can't load buffer %s: %s"
850 desktop-buffer-name (error-message-string err))
851 (when desktop-missing-file-warning (sit-for 1))
852 nil)))
854 (if (bufferp result)
855 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
856 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
857 (setq result nil))
858 (unless (bufferp result) (setq result nil))
859 ;; Restore buffer list order with new buffer at end. Don't change
860 ;; the order for old desktop files (old desktop module behaviour).
861 (unless (< desktop-file-version 206)
862 (mapcar 'bury-buffer buffer-list)
863 (when result (bury-buffer result)))
864 (when result
865 (unless (or desktop-first-buffer (< desktop-file-version 206))
866 (setq desktop-first-buffer result))
867 (set-buffer result)
868 (unless (equal (buffer-name) desktop-buffer-name)
869 (rename-buffer desktop-buffer-name))
870 ;; minor modes
871 (cond (
872 ;; backwards compatible
873 (equal '(t) desktop-buffer-minor-modes)
874 (auto-fill-mode 1))(
875 (equal '(nil) desktop-buffer-minor-modes)
876 (auto-fill-mode 0))(
878 (mapcar
879 #'(lambda (minor-mode)
880 (when (functionp minor-mode) (funcall minor-mode 1)))
881 desktop-buffer-minor-modes)))
882 ;; Even though point and mark are non-nil when written by `desktop-save'
883 ;; they may be modified by handlers wanting to set point or mark themselves.
884 (when desktop-buffer-point
885 (goto-char
886 (condition-case err
887 ;; Evaluate point. Thus point can be something like '(search-forward ...
888 (eval desktop-buffer-point)
889 (error (message "%s" (error-message-string err)) 1))))
890 (when desktop-buffer-mark
891 (if (consp desktop-buffer-mark)
892 (progn
893 (set-mark (car desktop-buffer-mark))
894 (setq mark-active (car (cdr desktop-buffer-mark))))
895 (set-mark desktop-buffer-mark)))
896 ;; Never override file system if the file really is read-only marked.
897 (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
898 (while desktop-buffer-locals
899 (let ((this (car desktop-buffer-locals)))
900 (if (consp this)
901 ;; an entry of this form `(symbol . value)'
902 (progn
903 (make-local-variable (car this))
904 (set (car this) (cdr this)))
905 ;; an entry of the form `symbol'
906 (make-local-variable this)
907 (makunbound this)))
908 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
910 ;; ----------------------------------------------------------------------------
911 ;; Backward compatibility -- update parameters to 205 standards.
912 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
913 desktop-buffer-major-mode
914 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
915 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
916 desktop-buffer-major-mode (cdr mim) pt mk ro
917 desktop-buffer-misc
918 (list (cons 'truncate-lines tl)
919 (cons 'fill-column fc)
920 (cons 'case-fold-search cfs)
921 (cons 'case-replace cr)
922 (cons 'overwrite-mode (car mim)))))
924 ;; ----------------------------------------------------------------------------
925 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
926 ;; command line, we do the rest of what it takes to use desktop, but do it
927 ;; after finishing loading the init file.
928 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
929 ;; functions are processed after `after-init-hook'.
930 (add-hook
931 'after-init-hook
932 '(lambda ()
933 (let ((key "--no-desktop"))
934 (if (member key command-line-args)
935 (delete key command-line-args)
936 (when desktop-save-mode (desktop-read))))))
938 (provide 'desktop)
940 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
941 ;;; desktop.el ends here