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