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