1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001
4 ;; Free Software Foundation, Inc.
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Maintainter: Lars Hansen <larsh@math.ku.dk>
8 ;; Keywords: convenience
9 ;; Favourite-brand-of-beer: None, I hate beer.
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
26 ;; Boston, MA 02111-1307, USA.
30 ;; Save the Desktop, i.e.,
31 ;; - some global variables
32 ;; - the list of buffers with associated files. For each buffer also
34 ;; - the default directory
36 ;; - the mark & mark-active
38 ;; - some local variables
40 ;; To use this, use customize to turn on desktop-save-mode or add the
41 ;; following line somewhere in your .emacs file:
43 ;; (desktop-save-mode 1)
45 ;; For further usage information, look at the section
46 ;; "Saving Emacs Sessions" in the GNU Emacs Manual.
48 ;; When the desktop module is loaded, the function `desktop-kill' is
49 ;; added to the `kill-emacs-hook'. This function is responsible for
50 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
51 ;; function is added to the `after-init-hook'. This function is
52 ;; responsible for loading the desktop when Emacs is started.
54 ;; Some words on minor modes: Most minor modes are controlled by
55 ;; buffer-local variables, which have a standard save / restore
56 ;; mechanism. To handle all minor modes, we take the following
57 ;; approach: (1) check whether the variable name from
58 ;; `minor-mode-alist' is also a function; and (2) use translation
59 ;; table `desktop-minor-mode-table' in the case where the two names
62 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
63 ;; in your home directory is used for that. Saving global default values
64 ;; for buffers is an example of misuse.
66 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
67 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
68 ;; things you did not mean to keep. Use M-x desktop-clear RET.
70 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
71 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
72 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
73 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
74 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
75 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
76 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
77 ;; ---------------------------------------------------------------------------
80 ;; Save window configuration.
81 ;; Recognize more minor modes.
86 (defvar desktop-file-version "206"
87 "Version number of desktop file format.
88 Written into the desktop file and used at desktop read to provide
89 backward compatibility.")
91 ;; ----------------------------------------------------------------------------
92 ;; USER OPTIONS -- settings you might want to play with.
93 ;; ----------------------------------------------------------------------------
96 "Save status of Emacs when you exit."
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
108 ;; Maintained for backward compatibility
109 (defvaralias 'desktop-enable 'desktop-save-mode)
110 (make-obsolete-variable 'desktop-enable 'desktop-save-mode)
112 (defcustom desktop-save 'ask-if-new
113 "*Specifies whether the desktop should be saved when it is killed.
114 A desktop is killed when the user changes desktop or quits Emacs.
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.
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."
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))
134 (defcustom desktop-base-file-name
135 (convert-standard-filename ".emacs.desktop")
136 "Name of file for Emacs desktop, excluding the directory part."
139 (defvaralias 'desktop-basefilename 'desktop-base-file-name)
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)
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."
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."
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."
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."
171 (defcustom desktop-globals-to-save '(
172 desktop-missing-file-warning
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)))
186 (defcustom desktop-globals-to-clear '(
188 kill-ring-yank-pointer
190 search-ring-yank-pointer
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)))
200 (defcustom desktop-clear-preserve-buffers-regexp
201 "^\\(\\*scratch\\*\\|\\*Messages\\*\\|\\*tramp/.+\\*\\)$"
202 "Regexp identifying buffers that `desktop-clear' should not delete."
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)
212 (make-obsolete-variable 'desktop-clear-preserve-buffers
213 'desktop-clear-preserve-buffers-regexp)
215 (defcustom desktop-locals-to-save '(
216 desktop-locals-to-save ; Itself! Think it over.
222 change-log-default-name
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)
229 (make-variable-buffer-local 'desktop-locals-to-save)
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."
240 ;; Skip tramp and ange-ftp files
241 (defcustom desktop-files-not-to-save
243 "Regexp identifying files whose buffers are to be excluded from saving."
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'
253 :type '(repeat symbol)
256 (defcustom desktop-modes-not-to-save nil
257 "List of major modes whose buffers should not be saved."
258 :type '(repeat symbol)
261 (defcustom desktop-file-name-format 'absolute
262 "*Format in which desktop file names should be saved.
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))
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.
278 When file names are returned, they should be formatted using the call
279 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
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)
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).
296 Buffers with a major mode not specified here, are restored by the default
297 handler `desktop-restore-file-buffer'.
299 Handlers are called with parameters
301 desktop-buffer-file-name
305 Furthermore, they may use the following variables:
308 desktop-buffer-major-mode
309 desktop-buffer-minor-modes
312 desktop-buffer-read-only
313 desktop-buffer-locals
315 If a handler returns a buffer, then the saved mode settings
316 and variable values for that buffer are copied into it."
320 (put 'desktop-buffer-mode-handlers 'risky-local-variable t)
321 (make-obsolete-variable 'desktop-buffer-handlers
322 'desktop-buffer-mode-handlers)
324 (defcustom desktop-minor-mode-table
325 '((auto-fill-function auto-fill-mode)
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
338 ;; ----------------------------------------------------------------------------
339 (defvar desktop-dirname nil
340 "The directory in which the desktop file should be saved.")
342 (defconst desktop-header
343 ";; --------------------------------------------------------------------------
344 ;; Desktop File for Emacs
345 ;; --------------------------------------------------------------------------
346 " "*Header to place in Desktop file.")
348 (defvar desktop-delay-hook nil
349 "Hooks run after all buffers are loaded; intended for internal use.")
351 ;; ----------------------------------------------------------------------------
352 (defun desktop-truncate (list n)
353 "Truncate LIST to at most N elements destructively."
354 (let ((here (nthcdr (1- n) list)))
358 ;; ----------------------------------------------------------------------------
359 (defun desktop-clear ()
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'."
366 (dolist (var desktop-globals-to-clear)
368 (eval `(setq-default ,var nil))
369 (eval `(setq-default ,(car var) ,(cdr var)))))
370 (let ((buffers (buffer-list)))
372 (let ((bufname (buffer-name (car buffers))))
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))
383 ;; ----------------------------------------------------------------------------
384 (add-hook 'kill-emacs-hook 'desktop-kill)
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."
393 (let ((exists (file-exists-p (expand-file-name desktop-base-file-name desktop-dirname))))
396 (and exists (memq desktop-save '(ask-if-new if-exists)))
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
407 (lambda (dir) (interactive "DDirectory for desktop file: ") dir))))))
409 (desktop-save desktop-dirname)
411 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
412 (signal (car err) (cdr err)))))))
414 ;; ----------------------------------------------------------------------------
415 (defun desktop-list* (&rest args)
416 (if (null (cdr args))
418 (setq args (nreverse args))
419 (let ((value (cons (nth 1 args) (car args))))
420 (setq args (cdr (cdr args)))
422 (setq value (cons (car args) value))
423 (setq args (cdr args)))
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)."
433 ((or (numberp value) (null value) (eq t value))
434 (cons 'may (prin1-to-string 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))))
441 (cons 'must (prin1-to-string value)))
446 (let ((res (desktop-internal-v2s el)))
452 (cons nil (concat "(vector "
453 (mapconcat (lambda (el)
454 (if (eq (car el) 'must)
455 (concat "'" (cdr el))
460 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
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)))
472 (let ((last (desktop-internal-v2s p))
474 (or anynil (setq anynil (null (car last))))
476 (setq newlist (cons '(must . ".") newlist)))
478 (setq newlist (cons last newlist))))
479 (setq newlist (nreverse newlist))
482 (concat (if use-list* "(desktop-list* " "(list ")
483 (mapconcat (lambda (el)
484 (if (eq (car el) 'must)
485 (concat "'" (cdr el))
491 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
493 (cons nil (concat "(symbol-function '"
494 (substring (prin1-to-string value) 7 -1)
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)"))))
504 (cons 'may "\"Unprintable entity\""))))
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)))
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."
528 (setq var (car varspec) size (cdr varspec))
532 (if (and (integerp size)
535 (desktop-truncate (eval var) size))
539 (desktop-value-to-string (symbol-value var))
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))
551 (not (string-match desktop-files-not-to-save filename)))
552 (and (eq mode 'dired-mode)
554 (set-buffer (get-buffer bufname))
555 (not (string-match desktop-files-not-to-save
556 default-directory))))
558 (memq mode desktop-buffer-modes-to-save))))))
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."
566 ((eq desktop-file-name-format 'tilde)
567 (let ((relative-name (file-relative-name (expand-file-name filename) "~")))
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))))
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)))
585 (let ((filename (expand-file-name desktop-base-file-name dirname))
592 (desktop-file-name (buffer-file-name) dirname)
604 (let ((special (assq mim desktop-minor-mode-table)))
605 (if special (cadr special) mim))
607 (mapcar #'car minor-mode-alist))
610 (list (mark t) mark-active)
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))
619 (let ((here (assq (car locals) loclist)))
621 (setq ll (cons here ll))
622 (when (member (car locals) loclist)
623 (setq ll (cons (car locals) ll)))))
624 (setq locals (cdr locals)))
627 (buf (get-buffer-create "*desktop*")))
632 ";; -*- coding: emacs-mule; -*-\n"
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)
641 "(setq kill-ring-yank-pointer (nthcdr "
642 (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer)))
645 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
649 (if (apply 'desktop-save-buffer-p l)
651 (insert "(desktop-create-buffer " desktop-file-version)
655 (insert "\n " (desktop-value-to-string e))))
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))
665 ;; ----------------------------------------------------------------------------
666 (defun desktop-remove ()
667 "Delete desktop file in `desktop-dirname'.
668 This function also sets `desktop-dirname' to nil."
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)))))
676 ;; ----------------------------------------------------------------------------
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."
687 (unless noninteractive
688 (setq desktop-dirname
689 (file-name-as-directory
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))
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.
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.
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)
730 ;; No desktop file found.
732 (let ((default-directory desktop-dirname))
733 (run-hooks 'desktop-no-desktop-file-hook))
734 (message "No desktop file.")
737 ;; ----------------------------------------------------------------------------
738 ;; Maintained for backward compatibility
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
746 (setq inhibit-default-init t))))
747 (make-obsolete 'desktop-load-default 'desktop-save-mode)
749 ;; ----------------------------------------------------------------------------
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
756 (interactive "DChange to directory: ")
757 (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname)))
760 (desktop-read dirname))
762 ;; ----------------------------------------------------------------------------
764 (defun desktop-save-in-desktop-dir ()
765 "Save the desktop in directory `desktop-dirname'."
768 (desktop-save desktop-dirname)
769 (call-interactively 'desktop-save))
770 (message "Desktop saved in %s" desktop-dirname))
772 ;; ----------------------------------------------------------------------------
774 (defun desktop-revert ()
775 "Revert to the last loaded desktop."
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"))
782 (desktop-read desktop-dirname))
784 ;; ----------------------------------------------------------------------------
785 (defun desktop-restore-file-buffer (desktop-buffer-file-name
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? "))
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)))
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))
815 ;; ----------------------------------------------------------------------------
816 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
819 (eval-when-compile ; Just to silence the byte compiler
820 (defvar desktop-first-buffer) ;; Dynamically bound in `desktop-read'
823 (defun desktop-create-buffer (
825 desktop-buffer-file-name
827 desktop-buffer-major-mode
828 desktop-buffer-minor-modes
831 desktop-buffer-read-only
834 desktop-buffer-locals)
835 ;; Just to silence the byte compiler. Bound locally in `desktop-read'.
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.
843 (buffer-list (buffer-list))
846 (funcall (or (cdr (assq desktop-buffer-major-mode desktop-buffer-mode-handlers))
847 'desktop-restore-file-buffer)
848 desktop-buffer-file-name
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))
858 (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count))
859 (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count))
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)))
868 (unless (or desktop-first-buffer (< desktop-file-version 206))
869 (setq desktop-first-buffer result))
871 (unless (equal (buffer-name) desktop-buffer-name)
872 (rename-buffer desktop-buffer-name))
875 ;; backwards compatible
876 (equal '(t) desktop-buffer-minor-modes)
878 (equal '(nil) desktop-buffer-minor-modes)
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
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)
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)))
904 ;; an entry of this form `(symbol . value)'
906 (make-local-variable (car this))
907 (set (car this) (cdr this)))
908 ;; an entry of the form `symbol'
909 (make-local-variable this)
911 (setq desktop-buffer-locals (cdr desktop-buffer-locals)))))))
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
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)))))
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'.
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))))))
943 ;;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9
944 ;;; desktop.el ends here