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