(x_alloc_nearest_color_1): New function.
[emacs.git] / lisp / desktop.el
blob95ae3c91040b696f9cc06e44730ad38043fcd533
1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000 Free Software Foundation, Inc.
5 ;; Author: Morten Welinder <terra@diku.dk>
6 ;; Keywords: convenience
7 ;; Favourite-brand-of-beer: None, I hate beer.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;; Save the Desktop, i.e.,
29 ;; - some global variables
30 ;; - the list of buffers with associated files. For each buffer also
31 ;; - the major mode
32 ;; - the default directory
33 ;; - the point
34 ;; - the mark & mark-active
35 ;; - buffer-read-only
36 ;; - some local variables
38 ;; To use this, first put these two lines in the bottom of your .emacs
39 ;; file (the later the better):
41 ;; (desktop-load-default)
42 ;; (desktop-read)
44 ;; Between these two lines you may wish to add something that updates the
45 ;; variables `desktop-globals-to-save' and/or `desktop-locals-to-save'. If
46 ;; for instance you want to save the local variable `foobar' for every buffer
47 ;; in which it is local, you could add the line
49 ;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
51 ;; To avoid saving excessive amounts of data you may also wish to add
52 ;; something like the following
54 ;; (add-hook 'kill-emacs-hook
55 ;; '(lambda ()
56 ;; (desktop-truncate search-ring 3)
57 ;; (desktop-truncate regexp-search-ring 3)))
59 ;; which will make sure that no more than three search items are saved. You
60 ;; must place this line *after* the `(desktop-load-default)' line. See also
61 ;; the variable `desktop-save-hook'.
63 ;; Start Emacs in the root directory of your "project". The desktop saver
64 ;; is inactive by default. You activate it by M-x desktop-save RET. When
65 ;; you exit the next time the above data will be saved. This ensures that
66 ;; all the files you were editing will be reloaded the next time you start
67 ;; Emacs from the same directory and that points will be set where you
68 ;; left them. If you save a desktop file in your home directory it will
69 ;; act as a default desktop when you start Emacs from a directory that
70 ;; doesn't have its own. I never do this, but you may want to.
72 ;; Some words on minor modes: Most minor modes are controlled by
73 ;; buffer-local variables, which have a standard save / restore
74 ;; mechanism. To handle all minor modes, we take the following
75 ;; approach: (1) check whether the variable name from
76 ;; `minor-mode-alist' is also a function; and (2) use translation
77 ;; table `desktop-minor-mode-table' in the case where the two names
78 ;; are not the same.
80 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
81 ;; in your home directory is used for that. Saving global default values
82 ;; for buffers is an example of misuse.
84 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
85 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
86 ;; things you did not mean to keep. Use M-x desktop-clear RET.
88 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
89 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
90 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
91 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
92 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
93 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
94 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
95 ;; ---------------------------------------------------------------------------
96 ;; TODO:
98 ;; Save window configuration.
99 ;; Recognize more minor modes.
100 ;; Save mark rings.
101 ;; Start-up with buffer-menu???
103 ;;; Code:
105 ;; Make the compilation more silent
106 (eval-when-compile
107 ;; We use functions from these modules
108 ;; We can't (require 'mh-e) since that wants to load something.
109 (mapcar 'require '(info dired reporter)))
110 ;; ----------------------------------------------------------------------------
111 ;; USER OPTIONS -- settings you might want to play with.
112 ;; ----------------------------------------------------------------------------
114 (defgroup desktop nil
115 "Save status of Emacs when you exit."
116 :group 'frames)
118 (defcustom desktop-enable nil
119 "*Non-nil enable Desktop to save the state of Emacs when you exit."
120 :group 'desktop
121 :type 'boolean
122 :require 'desktop
123 :initialize 'custom-initialize-default
124 :version "20.3")
126 (defcustom desktop-basefilename
127 (convert-standard-filename ".emacs.desktop")
128 "File for Emacs desktop, not including the directory name."
129 :type 'file
130 :group 'desktop)
132 (defcustom desktop-missing-file-warning nil
133 "*If non-nil then desktop warns when a file no longer exists.
134 Otherwise it simply ignores that file."
135 :type 'boolean
136 :group 'desktop)
138 (defvar desktop-globals-to-save
139 (list 'desktop-missing-file-warning
140 ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
141 ;; 'kill-ring
142 'tags-file-name
143 'tags-table-list
144 'search-ring
145 'regexp-search-ring
146 'register-alist
147 ;; 'desktop-globals-to-save ; Itself!
149 "List of global variables to save when killing Emacs.
150 An element may be variable name (a symbol)
151 or a cons cell of the form (VAR . MAX-SIZE),
152 which means to truncate VAR's value to at most MAX-SIZE elements
153 \(if the value is a list) before saving the value.")
155 (defvar desktop-locals-to-save
156 (list 'desktop-locals-to-save ; Itself! Think it over.
157 'truncate-lines
158 'case-fold-search
159 'case-replace
160 'fill-column
161 'overwrite-mode
162 'change-log-default-name
163 'line-number-mode
165 "List of local variables to save for each buffer.
166 The variables are saved only when they really are local.")
167 (make-variable-buffer-local 'desktop-locals-to-save)
169 ;; We skip .log files because they are normally temporary.
170 ;; (ftp) files because they require passwords and whatnot.
171 ;; TAGS files to save time (tags-file-name is saved instead).
172 (defcustom desktop-buffers-not-to-save
173 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
174 "Regexp identifying buffers that are to be excluded from saving."
175 :type 'regexp
176 :group 'desktop)
178 ;; Skip ange-ftp files
179 (defcustom desktop-files-not-to-save
180 "^/[^/:]*:"
181 "Regexp identifying files whose buffers are to be excluded from saving."
182 :type 'regexp
183 :group 'desktop)
185 (defcustom desktop-buffer-modes-to-save
186 '(Info-mode rmail-mode)
187 "If a buffer is of one of these major modes, save the buffer name.
188 It is up to the functions in `desktop-buffer-handlers' to decide
189 whether the buffer should be recreated or not, and how."
190 :type '(repeat symbol)
191 :group 'desktop)
193 (defcustom desktop-modes-not-to-save nil
194 "List of major modes whose buffers should not be saved."
195 :type '(repeat symbol)
196 :group 'desktop)
198 (defcustom desktop-buffer-major-mode nil
199 "When desktop creates a buffer, this holds the desired Major mode."
200 :type 'symbol
201 :group 'desktop)
203 (defcustom desktop-buffer-file-name nil
204 "When desktop creates a buffer, this holds the file name to visit."
205 :type '(choice file (const nil))
206 :group 'desktop)
208 (defcustom desktop-buffer-name nil
209 "When desktop creates a buffer, this holds the desired buffer name."
210 :type '(choice string (const nil))
211 :group 'desktop)
213 (defvar desktop-buffer-misc nil
214 "When desktop creates a buffer, this holds a list of misc info.
215 It is used by the `desktop-buffer-handlers' functions.")
217 (defcustom desktop-buffer-misc-functions
218 '(desktop-buffer-info-misc-data
219 desktop-buffer-dired-misc-data)
220 "*Functions used to determine auxiliary information for a buffer.
221 These functions are called in order, with no arguments. If a function
222 returns non-nil, its value is saved along with the desktop buffer for
223 which it was called; no further functions will be called.
225 Later, when desktop.el restores the buffers it has saved, each of the
226 `desktop-buffer-handlers' functions will have access to a buffer local
227 variable, named `desktop-buffer-misc', whose value is what the
228 \"misc\" function returned previously."
229 :type '(repeat function)
230 :group 'desktop)
232 (defcustom desktop-buffer-handlers
233 '(desktop-buffer-dired
234 desktop-buffer-rmail
235 desktop-buffer-mh
236 desktop-buffer-info
237 desktop-buffer-file)
238 "*List of functions to call in order to create a buffer.
239 The functions are called without explicit parameters but can use the
240 variables `desktop-buffer-major-mode', `desktop-buffer-file-name',
241 `desktop-buffer-name'.
242 If one function returns non-nil, no further functions are called.
243 If the function returns t then the buffer is considered created."
244 :type '(repeat function)
245 :group 'desktop)
247 (put 'desktop-buffer-handlers 'risky-local-variable t)
249 (defvar desktop-create-buffer-form "(desktop-create-buffer 205"
250 "Opening of form for creation of new buffers.")
252 (defcustom desktop-save-hook nil
253 "Hook run before desktop saves the state of Emacs.
254 This is useful for truncating history lists, for example."
255 :type 'hook
256 :group 'desktop)
258 (defcustom desktop-minor-mode-table
259 '((auto-fill-function auto-fill-mode)
260 (vc-mode nil))
261 "Table mapping minor mode variables to minor mode functions.
262 Each entry has the form (NAME RESTORE-FUNCTION).
263 NAME is the name of the buffer-local variable indicating that the minor
264 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
265 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
266 Only minor modes for which the name of the buffer-local variable
267 and the name of the minor mode function are different have to added to
268 this table."
269 :type 'sexp
270 :group 'desktop)
272 ;; ----------------------------------------------------------------------------
273 (defvar desktop-dirname nil
274 "The directory in which the current desktop file resides.")
276 (defconst desktop-header
277 ";; --------------------------------------------------------------------------
278 ;; Desktop File for Emacs
279 ;; --------------------------------------------------------------------------
280 " "*Header to place in Desktop file.")
282 (defvar desktop-delay-hook nil
283 "Hooks run after all buffers are loaded; intended for internal use.")
285 ;; ----------------------------------------------------------------------------
286 (defun desktop-truncate (l n)
287 "Truncate LIST to at most N elements destructively."
288 (let ((here (nthcdr (1- n) l)))
289 (if (consp here)
290 (setcdr here nil))))
291 ;; ----------------------------------------------------------------------------
292 (defcustom desktop-clear-preserve-buffers
293 '("*scratch*" "*Messages*")
294 "*Buffer names that `desktop-clear' should not delete."
295 :type '(repeat string)
296 :group 'desktop)
298 (defun desktop-clear ()
299 "Empty the Desktop.
300 This kills all buffers except for internal ones
301 and those listed in `desktop-clear-preserve-buffers'."
302 (interactive)
303 (setq kill-ring nil
304 kill-ring-yank-pointer nil
305 search-ring nil
306 search-ring-yank-pointer nil
307 regexp-search-ring nil
308 regexp-search-ring-yank-pointer nil)
309 (let ((buffers (buffer-list)))
310 (while buffers
311 (or (member (buffer-name (car buffers)) desktop-clear-preserve-buffers)
312 (null (buffer-name (car buffers)))
313 ;; Don't kill buffers made for internal purposes.
314 (and (not (equal (buffer-name (car buffers)) ""))
315 (eq (aref (buffer-name (car buffers)) 0) ?\ ))
316 (kill-buffer (car buffers)))
317 (setq buffers (cdr buffers))))
318 (delete-other-windows))
319 ;; ----------------------------------------------------------------------------
320 (add-hook 'kill-emacs-hook 'desktop-kill)
322 (defun desktop-kill ()
323 (if desktop-dirname
324 (condition-case err
325 (desktop-save desktop-dirname)
326 (file-error
327 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
329 (signal (car err) (cdr err)))))))
330 ;; ----------------------------------------------------------------------------
331 (defun desktop-list* (&rest args)
332 (if (null (cdr args))
333 (car args)
334 (setq args (nreverse args))
335 (let ((value (cons (nth 1 args) (car args))))
336 (setq args (cdr (cdr args)))
337 (while args
338 (setq value (cons (car args) value))
339 (setq args (cdr args)))
340 value)))
342 (defun desktop-internal-v2s (val)
343 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
344 TXT is a string that when read and evaluated yields value.
345 QUOTE may be `may' (value may be quoted),
346 `must' (values must be quoted), or nil (value may not be quoted)."
347 (cond
348 ((or (numberp val) (null val) (eq t val))
349 (cons 'may (prin1-to-string val)))
350 ((stringp val)
351 (let ((copy (copy-sequence val)))
352 (set-text-properties 0 (length copy) nil copy)
353 ;; Get rid of text properties because we cannot read them
354 (cons 'may (prin1-to-string copy))))
355 ((symbolp val)
356 (cons 'must (prin1-to-string val)))
357 ((vectorp val)
358 (let* ((special nil)
359 (pass1 (mapcar
360 (lambda (el)
361 (let ((res (desktop-internal-v2s el)))
362 (if (null (car res))
363 (setq special t))
364 res))
365 val)))
366 (if special
367 (cons nil (concat "(vector "
368 (mapconcat (lambda (el)
369 (if (eq (car el) 'must)
370 (concat "'" (cdr el))
371 (cdr el)))
372 pass1
373 " ")
374 ")"))
375 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
376 ((consp val)
377 (let ((p val)
378 newlist
379 use-list*
380 anynil)
381 (while (consp p)
382 (let ((q.txt (desktop-internal-v2s (car p))))
383 (or anynil (setq anynil (null (car q.txt))))
384 (setq newlist (cons q.txt newlist)))
385 (setq p (cdr p)))
386 (if p
387 (let ((last (desktop-internal-v2s p))
388 (el (car newlist)))
389 (or anynil (setq anynil (null (car last))))
390 (or anynil
391 (setq newlist (cons '(must . ".") newlist)))
392 (setq use-list* t)
393 (setq newlist (cons last newlist))))
394 (setq newlist (nreverse newlist))
395 (if anynil
396 (cons nil
397 (concat (if use-list* "(desktop-list* " "(list ")
398 (mapconcat (lambda (el)
399 (if (eq (car el) 'must)
400 (concat "'" (cdr el))
401 (cdr el)))
402 newlist
403 " ")
404 ")"))
405 (cons 'must
406 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
407 ((subrp val)
408 (cons nil (concat "(symbol-function '"
409 (substring (prin1-to-string val) 7 -1)
410 ")")))
411 ((markerp val)
412 (let ((pos (prin1-to-string (marker-position val)))
413 (buf (prin1-to-string (buffer-name (marker-buffer val)))))
414 (cons nil (concat "(let ((mk (make-marker)))"
415 " (add-hook 'desktop-delay-hook"
416 " (list 'lambda '() (list 'set-marker mk "
417 pos " (get-buffer " buf ")))) mk)"))))
418 (t ; save as text
419 (cons 'may "\"Unprintable entity\""))))
421 (defun desktop-value-to-string (val)
422 "Convert VALUE to a string that when read evaluates to the same value.
423 Not all types of values are supported."
424 (let* ((print-escape-newlines t)
425 (float-output-format nil)
426 (quote.txt (desktop-internal-v2s val))
427 (quote (car quote.txt))
428 (txt (cdr quote.txt)))
429 (if (eq quote 'must)
430 (concat "'" txt)
431 txt)))
432 ;; ----------------------------------------------------------------------------
433 (defun desktop-outvar (varspec)
434 "Output a setq statement for variable VAR to the desktop file.
435 The argument VARSPEC may be the variable name VAR (a symbol),
436 or a cons cell of the form (VAR . MAX-SIZE),
437 which means to truncate VAR's value to at most MAX-SIZE elements
438 \(if the value is a list) before saving the value."
439 (let (var size)
440 (if (consp varspec)
441 (setq var (car varspec) size (cdr varspec))
442 (setq var varspec))
443 (if (boundp var)
444 (progn
445 (if (and (integerp size)
446 (> size 0)
447 (listp (eval var)))
448 (desktop-truncate (eval var) size))
449 (insert "(setq "
450 (symbol-name var)
452 (desktop-value-to-string (symbol-value var))
453 ")\n")))))
454 ;; ----------------------------------------------------------------------------
455 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
456 "Return t if the desktop should record a particular buffer for next startup.
457 FILENAME is the visited file name, BUFNAME is the buffer name, and
458 MODE is the major mode."
459 (let ((case-fold-search nil))
460 (and (not (string-match desktop-buffers-not-to-save bufname))
461 (not (memq mode desktop-modes-not-to-save))
462 (or (and filename
463 (not (string-match desktop-files-not-to-save filename)))
464 (and (eq mode 'dired-mode)
465 (save-excursion
466 (set-buffer (get-buffer bufname))
467 (not (string-match desktop-files-not-to-save
468 default-directory))))
469 (and (null filename)
470 (memq mode desktop-buffer-modes-to-save))))))
471 ;; ----------------------------------------------------------------------------
472 (defun desktop-save (dirname)
473 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
474 (interactive "DDirectory to save desktop file in: ")
475 (run-hooks 'desktop-save-hook)
476 (save-excursion
477 (let ((filename (expand-file-name
478 (concat dirname desktop-basefilename)))
479 (info (nreverse
480 (mapcar
481 (function
482 (lambda (b)
483 (set-buffer b)
484 (list
485 (buffer-file-name)
486 (buffer-name)
487 major-mode
488 ;; minor modes
489 (let (ret)
490 (mapcar
491 #'(lambda (mim)
492 (and (boundp mim)
493 (symbol-value mim)
494 (setq ret
495 (cons (let ((special (assq mim desktop-minor-mode-table)))
496 (if special
497 (cadr special)
498 mim))
499 ret))))
500 (mapcar #'car minor-mode-alist))
501 ret)
502 (point)
503 (list (mark t) mark-active)
504 buffer-read-only
505 (run-hook-with-args-until-success
506 'desktop-buffer-misc-functions)
507 (let ((locals desktop-locals-to-save)
508 (loclist (buffer-local-variables))
509 (ll))
510 (while locals
511 (let ((here (assq (car locals) loclist)))
512 (if here
513 (setq ll (cons here ll))
514 (if (member (car locals) loclist)
515 (setq ll (cons (car locals) ll)))))
516 (setq locals (cdr locals)))
519 (buffer-list))))
520 (buf (get-buffer-create "*desktop*")))
521 (set-buffer buf)
522 (erase-buffer)
524 (insert desktop-header
525 ";; Created " (current-time-string) "\n"
526 ";; Emacs version " emacs-version "\n\n"
527 ";; Global section:\n")
528 (mapcar (function desktop-outvar) desktop-globals-to-save)
529 (if (memq 'kill-ring desktop-globals-to-save)
530 (insert "(setq kill-ring-yank-pointer (nthcdr "
531 (int-to-string
532 (- (length kill-ring) (length kill-ring-yank-pointer)))
533 " kill-ring))\n"))
535 (insert "\n;; Buffer section:\n")
536 (mapcar
537 (function (lambda (l)
538 (if (apply 'desktop-save-buffer-p l)
539 (progn
540 (insert desktop-create-buffer-form)
541 (mapcar
542 (function (lambda (e)
543 (insert "\n "
544 (desktop-value-to-string e))))
546 (insert ")\n\n")))))
547 info)
548 (setq default-directory dirname)
549 (if (file-exists-p filename) (delete-file filename))
550 (write-region (point-min) (point-max) filename nil 'nomessage)))
551 (setq desktop-dirname dirname))
552 ;; ----------------------------------------------------------------------------
553 (defun desktop-remove ()
554 "Delete the Desktop file and inactivate the desktop system."
555 (interactive)
556 (if desktop-dirname
557 (let ((filename (concat desktop-dirname desktop-basefilename)))
558 (setq desktop-dirname nil)
559 (if (file-exists-p filename)
560 (delete-file filename)))))
561 ;; ----------------------------------------------------------------------------
562 ;;;###autoload
563 (defun desktop-read ()
564 "Read the Desktop file and the files it specifies.
565 This is a no-op when Emacs is running in batch mode."
566 (interactive)
567 (if noninteractive
569 (let ((dirs '("./" "~/")))
570 (while (and dirs
571 (not (file-exists-p (expand-file-name
572 desktop-basefilename
573 (car dirs)))))
574 (setq dirs (cdr dirs)))
575 (setq desktop-dirname (and dirs (expand-file-name (car dirs))))
576 (if desktop-dirname
577 (progn
578 (load (expand-file-name desktop-basefilename desktop-dirname)
579 t t t)
580 (run-hooks 'desktop-delay-hook)
581 (setq desktop-delay-hook nil)
582 (message "Desktop loaded."))
583 (desktop-clear)))))
584 ;; ----------------------------------------------------------------------------
585 ;;;###autoload
586 (defun desktop-load-default ()
587 "Load the `default' start-up library manually.
588 Also inhibit further loading of it. Call this from your `.emacs' file
589 to provide correct modes for autoloaded files."
590 (if (not inhibit-default-init) ; safety check
591 (progn
592 (load "default" t t)
593 (setq inhibit-default-init t))))
594 ;; ----------------------------------------------------------------------------
595 ;; Note: the following functions use the dynamic variable binding in Lisp.
597 (defun desktop-buffer-info-misc-data ()
598 (if (eq major-mode 'Info-mode)
599 (list Info-current-file
600 Info-current-node)))
602 (defun desktop-buffer-dired-misc-data ()
603 (if (eq major-mode 'dired-mode)
604 (cons
605 (expand-file-name dired-directory)
606 (cdr
607 (nreverse
608 (mapcar
609 (function car)
610 dired-subdir-alist))))))
612 (defun desktop-buffer-info () "Load an info file."
613 (if (eq 'Info-mode desktop-buffer-major-mode)
614 (progn
615 (let ((first (nth 0 desktop-buffer-misc))
616 (second (nth 1 desktop-buffer-misc)))
617 (when (and first second)
618 (require 'info)
619 (Info-find-node first second)
620 (current-buffer))))))
621 ;; ----------------------------------------------------------------------------
622 (defun desktop-buffer-rmail () "Load an RMAIL file."
623 (if (eq 'rmail-mode desktop-buffer-major-mode)
624 (condition-case error
625 (progn (rmail-input desktop-buffer-file-name)
626 (if (eq major-mode 'rmail-mode)
627 (current-buffer)
628 rmail-buffer))
629 (file-locked
630 (kill-buffer (current-buffer))
631 'ignored))))
632 ;; ----------------------------------------------------------------------------
633 (defun desktop-buffer-mh () "Load a folder in the mh system."
634 (if (eq 'mh-folder-mode desktop-buffer-major-mode)
635 (progn
636 (require 'mh-e)
637 (mh-find-path)
638 (mh-visit-folder desktop-buffer-name)
639 (current-buffer))))
640 ;; ----------------------------------------------------------------------------
641 (defun desktop-buffer-dired () "Load a directory using dired."
642 (if (eq 'dired-mode desktop-buffer-major-mode)
643 (if (file-directory-p (file-name-directory (car desktop-buffer-misc)))
644 (progn
645 (dired (car desktop-buffer-misc))
646 (mapcar 'dired-maybe-insert-subdir (cdr desktop-buffer-misc))
647 (current-buffer))
648 (message "Directory %s no longer exists." (car desktop-buffer-misc))
649 (sit-for 1)
650 'ignored)))
651 ;; ----------------------------------------------------------------------------
652 (defun desktop-buffer-file () "Load a file."
653 (if desktop-buffer-file-name
654 (if (or (file-exists-p desktop-buffer-file-name)
655 (and desktop-missing-file-warning
656 (y-or-n-p (format
657 "File \"%s\" no longer exists. Re-create? "
658 desktop-buffer-file-name))))
659 (let ((buf (find-file-noselect desktop-buffer-file-name)))
660 (condition-case nil
661 (switch-to-buffer buf)
662 (error (pop-to-buffer buf))))
663 'ignored)))
664 ;; ----------------------------------------------------------------------------
665 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
666 ;; only.
667 (defun desktop-create-buffer (ver desktop-buffer-file-name desktop-buffer-name
668 desktop-buffer-major-mode
669 mim pt mk ro desktop-buffer-misc
670 &optional locals)
671 (let ((hlist desktop-buffer-handlers)
672 (result)
673 (handler))
674 (while (and (not result) hlist)
675 (setq handler (car hlist))
676 (setq result (funcall handler))
677 (setq hlist (cdr hlist)))
678 (when (bufferp result)
679 (set-buffer result)
680 (if (not (equal (buffer-name) desktop-buffer-name))
681 (rename-buffer desktop-buffer-name))
682 ;; minor modes
683 (cond ((equal '(t) mim) (auto-fill-mode 1)) ; backwards compatible
684 ((equal '(nil) mim) (auto-fill-mode 0))
685 (t (mapcar #'(lambda (minor-mode)
686 (unless (or (eq minor-mode t) (eq minor-mode nil))
687 (if (and minor-mode (fboundp minor-mode))
688 (funcall minor-mode 1))))
689 mim)))
690 (goto-char pt)
691 (if (consp mk)
692 (progn
693 (set-mark (car mk))
694 (setq mark-active (car (cdr mk))))
695 (set-mark mk))
696 ;; Never override file system if the file really is read-only marked.
697 (if ro (setq buffer-read-only ro))
698 (while locals
699 (let ((this (car locals)))
700 (if (consp this)
701 ;; an entry of this form `(symbol . value)'
702 (progn
703 (make-local-variable (car this))
704 (set (car this) (cdr this)))
705 ;; an entry of the form `symbol'
706 (make-local-variable this)
707 (makunbound this)))
708 (setq locals (cdr locals))))))
710 ;; Backward compatibility -- update parameters to 205 standards.
711 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
712 desktop-buffer-major-mode
713 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
714 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
715 desktop-buffer-major-mode (cdr mim) pt mk ro
716 desktop-buffer-misc
717 (list (cons 'truncate-lines tl)
718 (cons 'fill-column fc)
719 (cons 'case-fold-search cfs)
720 (cons 'case-replace cr)
721 (cons 'overwrite-mode (car mim)))))
722 ;; ----------------------------------------------------------------------------
724 ;; If the user set desktop-enable to t with Custom,
725 ;; do the rest of what it takes to use desktop,
726 ;; but do it after finishing loading the init file.
727 (add-hook 'after-init-hook
728 '(lambda ()
729 (when desktop-enable
730 (desktop-load-default)
731 (desktop-read))))
733 (provide 'desktop)
735 ;;; desktop.el ends here