(comint-input-ring-size): Increase to 150.
[emacs.git] / lisp / desktop.el
blob55ebd662df686db1e4e703835bc5c2ce1312f0e6
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
133 :version "21.4")
135 (defcustom desktop-base-file-name
136 (convert-standard-filename ".emacs.desktop")
137 "Name of file for Emacs desktop, excluding the directory part."
138 :type 'file
139 :group 'desktop)
140 (defvaralias 'desktop-basefilename 'desktop-base-file-name)
142 (defcustom desktop-path '("." "~")
143 "List of directories to search for the desktop file.
144 The base name of the file is specified in `desktop-base-file-name'."
145 :type '(repeat directory)
146 :group 'desktop
147 :version "21.4")
149 (defcustom desktop-missing-file-warning nil
150 "*If non-nil then `desktop-read' asks if a non-existent file should be recreated.
151 Also pause for a moment to display message about errors signaled in
152 `desktop-buffer-mode-handlers'.
154 If nil, just print error messages in the message buffer."
155 :type 'boolean
156 :group 'desktop
157 :version "21.4")
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
164 :version "21.4")
166 (defcustom desktop-after-read-hook nil
167 "Normal hook run after a successful `desktop-read'.
168 May e.g. be used to show a buffer list."
169 :type 'hook
170 :group 'desktop
171 :version "21.4")
173 (defcustom desktop-save-hook nil
174 "Normal hook run before the desktop is saved in a desktop file.
175 This is useful for truncating history lists, for example."
176 :type 'hook
177 :group 'desktop)
179 (defcustom desktop-globals-to-save
180 '(desktop-missing-file-warning
181 tags-file-name
182 tags-table-list
183 search-ring
184 regexp-search-ring
185 register-alist)
186 "List of global variables saved by `desktop-save'.
187 An element may be variable name (a symbol) or a cons cell of the form
188 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
189 MAX-SIZE elements (if the value is a list) before saving the value.
190 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
191 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
192 :group 'desktop)
194 (defcustom desktop-globals-to-clear
195 '(kill-ring
196 kill-ring-yank-pointer
197 search-ring
198 search-ring-yank-pointer
199 regexp-search-ring
200 regexp-search-ring-yank-pointer)
201 "List of global variables to clear by `desktop-clear'.
202 An element may be variable name (a symbol) or a cons cell of the form
203 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
204 to the value obtained by evaluateing FORM."
205 :type '(repeat (restricted-sexp :match-alternatives (symbolp consp)))
206 :group 'desktop
207 :version "21.4")
209 (defcustom desktop-clear-preserve-buffers-regexp
210 "^\\(\\*scratch\\*\\|\\*Messages\\*\\|\\*tramp/.+\\*\\)$"
211 "Regexp identifying buffers that `desktop-clear' should not delete.
212 See also `desktop-clear-preserve-buffers'."
213 :type 'regexp
214 :group 'desktop
215 :version "21.4")
217 (defcustom desktop-clear-preserve-buffers nil
218 "*List of buffer names that `desktop-clear' should not delete.
219 See also `desktop-clear-preserve-buffers-regexp'."
220 :type '(repeat string)
221 :group 'desktop)
223 (defcustom desktop-locals-to-save
224 '(desktop-locals-to-save ; Itself! Think it over.
225 truncate-lines
226 case-fold-search
227 case-replace
228 fill-column
229 overwrite-mode
230 change-log-default-name
231 line-number-mode
232 buffer-file-coding-system)
233 "List of local variables to save for each buffer.
234 The variables are saved only when they really are local."
235 :type '(repeat symbol)
236 :group 'desktop)
237 (make-variable-buffer-local 'desktop-locals-to-save)
239 ;; We skip .log files because they are normally temporary.
240 ;; (ftp) files because they require passwords and whatnot.
241 ;; TAGS files to save time (tags-file-name is saved instead).
242 (defcustom desktop-buffers-not-to-save
243 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
244 "Regexp identifying buffers that are to be excluded from saving."
245 :type 'regexp
246 :group 'desktop)
248 ;; Skip tramp and ange-ftp files
249 (defcustom desktop-files-not-to-save
250 "^/[^/:]*:"
251 "Regexp identifying files whose buffers are to be excluded from saving."
252 :type 'regexp
253 :group 'desktop)
255 (defcustom desktop-modes-not-to-save nil
256 "List of major modes whose buffers should not be saved."
257 :type '(repeat symbol)
258 :group 'desktop)
260 (defcustom desktop-file-name-format 'absolute
261 "*Format in which desktop file names should be saved.
262 Possible values are:
263 absolute -- Absolute file name.
264 tilde -- Relative to ~.
265 local -- Relative to directory of desktop file."
266 :type '(choice (const absolute) (const tilde) (const local))
267 :group 'desktop
268 :version "21.4")
270 ;;;###autoload
271 (defvar desktop-save-buffer nil
272 "When non-nil, save buffer status in desktop file.
273 This variable becomes buffer local when set.
275 If the value is a function, it called by `desktop-save' with argument
276 DESKTOP-DIRNAME to obtain auxiliary information to saved in the desktop
277 file along with the state of the buffer for which it was called.
279 When file names are returned, they should be formatted using the call
280 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
282 Later, when `desktop-read' calls a function in `desktop-buffer-mode-handlers'
283 to restore the buffer, the auxiliary information is passed as the argument
284 DESKTOP-BUFFER-MISC.")
285 (make-variable-buffer-local 'desktop-save-buffer)
286 (make-obsolete-variable 'desktop-buffer-modes-to-save
287 'desktop-save-buffer)
288 (make-obsolete-variable 'desktop-buffer-misc-functions
289 'desktop-save-buffer)
291 (defcustom desktop-buffer-mode-handlers
292 '((dired-mode . dired-restore-desktop-buffer)
293 (rmail-mode . rmail-restore-desktop-buffer)
294 (mh-folder-mode . mh-restore-desktop-buffer)
295 (Info-mode . Info-restore-desktop-buffer))
296 "Alist of major mode specific functions to restore a desktop buffer.
297 Functions are called by `desktop-read'. List elements must have the form
298 \(MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
300 Buffers with a major mode not specified here, are restored by the default
301 handler `desktop-restore-file-buffer'.
303 Handlers are called with argument list
305 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
307 Furthermore, they may use the following variables:
309 desktop-file-version
310 desktop-buffer-major-mode
311 desktop-buffer-minor-modes
312 desktop-buffer-point
313 desktop-buffer-mark
314 desktop-buffer-read-only
315 desktop-buffer-locals
317 If a handler returns a buffer, then the saved mode settings
318 and variable values for that buffer are copied into it."
319 :type 'alist
320 :group 'desktop)
322 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
323 (make-obsolete-variable 'desktop-buffer-handlers
324 'desktop-buffer-mode-handlers)
326 (defcustom desktop-minor-mode-table
327 '((auto-fill-function auto-fill-mode)
328 (vc-mode nil))
329 "Table mapping minor mode variables to minor mode functions.
330 Each entry has the form (NAME RESTORE-FUNCTION).
331 NAME is the name of the buffer-local variable indicating that the minor
332 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
333 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
334 Only minor modes for which the name of the buffer-local variable
335 and the name of the minor mode function are different have to be added to
336 this table."
337 :type 'sexp
338 :group 'desktop)
340 ;; ----------------------------------------------------------------------------
341 (defvar desktop-dirname nil
342 "The directory in which the desktop file should be saved.")
344 (defconst desktop-header
345 ";; --------------------------------------------------------------------------
346 ;; Desktop File for Emacs
347 ;; --------------------------------------------------------------------------
348 " "*Header to place in Desktop file.")
350 (defvar desktop-delay-hook nil
351 "Hooks run after all buffers are loaded; intended for internal use.")
353 ;; ----------------------------------------------------------------------------
354 (defun desktop-truncate (list n)
355 "Truncate LIST to at most N elements destructively."
356 (let ((here (nthcdr (1- n) list)))
357 (if (consp here)
358 (setcdr here nil))))
360 ;; ----------------------------------------------------------------------------
361 (defun desktop-clear ()
362 "Empty the Desktop.
363 This kills all buffers except for internal ones and those matching
364 `desktop-clear-preserve-buffers-regexp' or listed in
365 `desktop-clear-preserve-buffers'. Furthermore, it clears the
366 variables listed in `desktop-globals-to-clear'."
367 (interactive)
368 (dolist (var desktop-globals-to-clear)
369 (if (symbolp var)
370 (eval `(setq-default ,var nil))
371 (eval `(setq-default ,(car var) ,(cdr var)))))
372 (let ((buffers (buffer-list)))
373 (while buffers
374 (let ((bufname (buffer-name (car buffers))))
376 (null bufname)
377 (string-match desktop-clear-preserve-buffers-regexp bufname)
378 (member bufname desktop-clear-preserve-buffers)
379 ;; Don't kill buffers made for internal purposes.
380 (and (not (equal bufname "")) (eq (aref bufname 0) ?\ ))
381 (kill-buffer (car buffers))))
382 (setq buffers (cdr buffers))))
383 (delete-other-windows))
385 ;; ----------------------------------------------------------------------------
386 (add-hook 'kill-emacs-hook 'desktop-kill)
388 (defun desktop-kill ()
389 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
390 If the desktop should be saved and `desktop-dirname'
391 is nil, ask the user where to save the desktop."
392 (when
393 (and
394 desktop-save-mode
395 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
397 (eq desktop-save t)
398 (and exists (memq desktop-save '(ask-if-new if-exists)))
399 (and
401 (memq desktop-save '(ask ask-if-new))
402 (and exists (eq desktop-save 'ask-if-exists)))
403 (y-or-n-p "Save desktop? ")))))
404 (unless desktop-dirname
405 (setq desktop-dirname
406 (file-name-as-directory
407 (expand-file-name
408 (call-interactively
409 (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
410 (condition-case err
411 (desktop-save desktop-dirname)
412 (file-error
413 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
414 (signal (car err) (cdr err)))))))
416 ;; ----------------------------------------------------------------------------
417 (defun desktop-list* (&rest args)
418 (if (null (cdr args))
419 (car args)
420 (setq args (nreverse args))
421 (let ((value (cons (nth 1 args) (car args))))
422 (setq args (cdr (cdr args)))
423 (while args
424 (setq value (cons (car args) value))
425 (setq args (cdr args)))
426 value)))
428 ;; ----------------------------------------------------------------------------
429 (defun desktop-internal-v2s (value)
430 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
431 TXT is a string that when read and evaluated yields value.
432 QUOTE may be `may' (value may be quoted),
433 `must' (values must be quoted), or nil (value may not be quoted)."
434 (cond
435 ((or (numberp value) (null value) (eq t value))
436 (cons 'may (prin1-to-string value)))
437 ((stringp value)
438 (let ((copy (copy-sequence value)))
439 (set-text-properties 0 (length copy) nil copy)
440 ;; Get rid of text properties because we cannot read them
441 (cons 'may (prin1-to-string copy))))
442 ((symbolp value)
443 (cons 'must (prin1-to-string value)))
444 ((vectorp value)
445 (let* ((special nil)
446 (pass1 (mapcar
447 (lambda (el)
448 (let ((res (desktop-internal-v2s el)))
449 (if (null (car res))
450 (setq special t))
451 res))
452 value)))
453 (if special
454 (cons nil (concat "(vector "
455 (mapconcat (lambda (el)
456 (if (eq (car el) 'must)
457 (concat "'" (cdr el))
458 (cdr el)))
459 pass1
460 " ")
461 ")"))
462 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
463 ((consp value)
464 (let ((p value)
465 newlist
466 use-list*
467 anynil)
468 (while (consp p)
469 (let ((q.txt (desktop-internal-v2s (car p))))
470 (or anynil (setq anynil (null (car q.txt))))
471 (setq newlist (cons q.txt newlist)))
472 (setq p (cdr p)))
473 (if p
474 (let ((last (desktop-internal-v2s p))
475 (el (car newlist)))
476 (or anynil (setq anynil (null (car last))))
477 (or anynil
478 (setq newlist (cons '(must . ".") newlist)))
479 (setq use-list* t)
480 (setq newlist (cons last newlist))))
481 (setq newlist (nreverse newlist))
482 (if anynil
483 (cons nil
484 (concat (if use-list* "(desktop-list* " "(list ")
485 (mapconcat (lambda (el)
486 (if (eq (car el) 'must)
487 (concat "'" (cdr el))
488 (cdr el)))
489 newlist
490 " ")
491 ")"))
492 (cons 'must
493 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
494 ((subrp value)
495 (cons nil (concat "(symbol-function '"
496 (substring (prin1-to-string value) 7 -1)
497 ")")))
498 ((markerp value)
499 (let ((pos (prin1-to-string (marker-position value)))
500 (buf (prin1-to-string (buffer-name (marker-buffer value)))))
501 (cons nil (concat "(let ((mk (make-marker)))"
502 " (add-hook 'desktop-delay-hook"
503 " (list 'lambda '() (list 'set-marker mk "
504 pos " (get-buffer " buf ")))) mk)"))))
505 (t ; save as text
506 (cons 'may "\"Unprintable entity\""))))
508 ;; ----------------------------------------------------------------------------
509 (defun desktop-value-to-string (value)
510 "Convert VALUE to a string that when read evaluates to the same value.
511 Not all types of values are supported."
512 (let* ((print-escape-newlines t)
513 (float-output-format nil)
514 (quote.txt (desktop-internal-v2s value))
515 (quote (car quote.txt))
516 (txt (cdr quote.txt)))
517 (if (eq quote 'must)
518 (concat "'" txt)
519 txt)))
521 ;; ----------------------------------------------------------------------------
522 (defun desktop-outvar (varspec)
523 "Output a setq statement for variable VAR to the desktop file.
524 The argument VARSPEC may be the variable name VAR (a symbol),
525 or a cons cell of the form (VAR . MAX-SIZE),
526 which means to truncate VAR's value to at most MAX-SIZE elements
527 \(if the value is a list) before saving the value."
528 (let (var size)
529 (if (consp varspec)
530 (setq var (car varspec) size (cdr varspec))
531 (setq var varspec))
532 (if (boundp var)
533 (progn
534 (if (and (integerp size)
535 (> size 0)
536 (listp (eval var)))
537 (desktop-truncate (eval var) size))
538 (insert "(setq "
539 (symbol-name var)
541 (desktop-value-to-string (symbol-value var))
542 ")\n")))))
544 ;; ----------------------------------------------------------------------------
545 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
546 "Return t if buffer should have its state saved in the desktop file.
547 FILENAME is the visited file name, BUFNAME is the buffer name, and
548 MODE is the major mode."
549 (let ((case-fold-search nil))
550 (and (not (string-match desktop-buffers-not-to-save bufname))
551 (not (memq mode desktop-modes-not-to-save))
552 (or (and filename
553 (not (string-match desktop-files-not-to-save filename)))
554 (and (eq mode 'dired-mode)
555 (with-current-buffer bufname
556 (not (string-match desktop-files-not-to-save
557 default-directory))))
558 (and (null filename)
559 (with-current-buffer bufname desktop-save-buffer))))))
561 ;; ----------------------------------------------------------------------------
562 (defun desktop-file-name (filename dirname)
563 "Convert FILENAME to format specified in `desktop-file-name-format'.
564 DIRNAME must be the directory in which the desktop file will be saved."
565 (cond
566 ((not filename) nil)
567 ((eq desktop-file-name-format 'tilde)
568 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
569 (cond
570 ((file-name-absolute-p relative-name) relative-name)
571 ((string= "./" relative-name) "~/")
572 ((string= "." relative-name) "~")
573 (t (concat "~/" relative-name)))))
574 ((eq desktop-file-name-format 'local) (file-relative-name filename dirname))
575 (t (expand-file-name filename))))
577 ;; ----------------------------------------------------------------------------
578 (defun desktop-save (dirname)
579 "Save the desktop in a desktop file.
580 Parameter DIRNAME specifies where to save the desktop file.
581 See also `desktop-base-file-name'."
582 (interactive "DDirectory to save desktop file in: ")
583 (run-hooks 'desktop-save-hook)
584 (setq dirname (file-name-as-directory (expand-file-name dirname)))
585 (save-excursion
586 (let ((filename (expand-file-name desktop-base-file-name dirname))
587 (info
588 (mapcar
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 (mapc
598 #'(lambda (minor-mode)
599 (and
600 (boundp minor-mode)
601 (symbol-value minor-mode)
602 (let ((special (assq minor-mode desktop-minor-mode-table)))
603 (when (or special (functionp minor-mode))
604 (setq ret
605 (cons
606 (if special (cadr special) minor-mode)
607 ret))))))
608 (mapcar #'car minor-mode-alist))
609 ret)
610 (point)
611 (list (mark t) mark-active)
612 buffer-read-only
613 ;; Auxiliary information
614 (when (functionp desktop-save-buffer)
615 (funcall desktop-save-buffer dirname))
616 (let ((locals desktop-locals-to-save)
617 (loclist (buffer-local-variables))
618 (ll))
619 (while locals
620 (let ((here (assq (car locals) loclist)))
621 (if here
622 (setq ll (cons here ll))
623 (when (member (car locals) loclist)
624 (setq ll (cons (car locals) ll)))))
625 (setq locals (cdr locals)))
626 ll)))
627 (buffer-list)))
628 (buf (get-buffer-create "*desktop*")))
629 (set-buffer buf)
630 (erase-buffer)
632 (insert
633 ";; -*- coding: emacs-mule; -*-\n"
634 desktop-header
635 ";; Created " (current-time-string) "\n"
636 ";; Desktop file format version " desktop-file-version "\n"
637 ";; Emacs version " emacs-version "\n\n"
638 ";; Global section:\n")
639 (mapc (function desktop-outvar) desktop-globals-to-save)
640 (if (memq 'kill-ring desktop-globals-to-save)
641 (insert
642 "(setq kill-ring-yank-pointer (nthcdr "
643 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
644 " kill-ring))\n"))
646 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
647 (mapc #'(lambda (l)
648 (if (apply 'desktop-save-buffer-p l)
649 (progn
650 (insert "(desktop-create-buffer " desktop-file-version)
651 (mapc #'(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 its mode, ...;
814 ;; called from Desktop file 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 ((buffer-list (buffer-list))
840 (result
841 (condition-case err
842 (funcall (or (cdr (assq desktop-buffer-major-mode
843 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
851 (error-message-string err))
852 (when desktop-missing-file-warning (sit-for 1))
853 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 ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible
872 (auto-fill-mode 1))
873 ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible
874 (auto-fill-mode 0))
876 (mapc #'(lambda (minor-mode)
877 (when (functionp minor-mode) (funcall minor-mode 1)))
878 desktop-buffer-minor-modes)))
879 ;; Even though point and mark are non-nil when written by `desktop-save'
880 ;; they may be modified by handlers wanting to set point or mark themselves.
881 (when desktop-buffer-point
882 (goto-char
883 (condition-case err
884 ;; Evaluate point. Thus point can be something like '(search-forward ...
885 (eval desktop-buffer-point)
886 (error (message "%s" (error-message-string err)) 1))))
887 (when desktop-buffer-mark
888 (if (consp desktop-buffer-mark)
889 (progn
890 (set-mark (car desktop-buffer-mark))
891 (setq mark-active (car (cdr desktop-buffer-mark))))
892 (set-mark desktop-buffer-mark)))
893 ;; Never override file system if the file really is read-only marked.
894 (if desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only))
895 (while desktop-buffer-locals
896 (let ((this (car desktop-buffer-locals)))
897 (if (consp this)
898 ;; an entry of this form `(symbol . value)'
899 (progn
900 (make-local-variable (car this))
901 (set (car this) (cdr this)))
902 ;; an entry of the form `symbol'
903 (make-local-variable this)
904 (makunbound this)))
905 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
907 ;; ----------------------------------------------------------------------------
908 ;; Backward compatibility -- update parameters to 205 standards.
909 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
910 desktop-buffer-major-mode
911 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
912 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
913 desktop-buffer-major-mode (cdr mim) pt mk ro
914 desktop-buffer-misc
915 (list (cons 'truncate-lines tl)
916 (cons 'fill-column fc)
917 (cons 'case-fold-search cfs)
918 (cons 'case-replace cr)
919 (cons 'overwrite-mode (car mim)))))
921 ;; ----------------------------------------------------------------------------
922 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
923 ;; command line, we do the rest of what it takes to use desktop, but do it
924 ;; after finishing loading the init file.
925 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
926 ;; functions are processed after `after-init-hook'.
927 (add-hook
928 'after-init-hook
929 '(lambda ()
930 (let ((key "--no-desktop"))
931 (if (member key command-line-args)
932 (delete key command-line-args)
933 (when desktop-save-mode (desktop-read))))))
935 (provide 'desktop)
937 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
938 ;;; desktop.el ends here