1 ;;; desktop.el --- save partial status of Emacs when killed -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1995, 1997, 2000-2015 Free Software Foundation,
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Keywords: convenience
8 ;; Favorite-brand-of-beer: None, I hate beer.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;; Save the Desktop, i.e.,
28 ;; - some global variables
29 ;; - the list of buffers with associated files. For each buffer also
31 ;; - the default directory
33 ;; - the mark & mark-active
35 ;; - some local variables
36 ;; - frame and window configuration
38 ;; To use this, use customize to turn on desktop-save-mode or add the
39 ;; following line somewhere in your init file:
41 ;; (desktop-save-mode 1)
43 ;; For further usage information, look at the section
44 ;; (info "(emacs)Saving Emacs Sessions") in the GNU Emacs Manual.
46 ;; When the desktop module is loaded, the function `desktop-kill' is
47 ;; added to the `kill-emacs-hook'. This function is responsible for
48 ;; saving the desktop when Emacs is killed. Furthermore an anonymous
49 ;; function is added to the `after-init-hook'. This function is
50 ;; responsible for loading the desktop when Emacs is started.
54 ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers'
55 ;; are supplied to handle special major and minor modes respectively.
56 ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions
57 ;; to restore a desktop buffer. Elements must have the form
59 ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
61 ;; Functions listed are called by `desktop-create-buffer' when `desktop-read'
62 ;; evaluates the desktop file. Buffers with a major mode not specified here,
63 ;; are restored by the default handler `desktop-restore-file-buffer'.
64 ;; `desktop-minor-mode-handlers' is an alist of functions to restore
65 ;; non-standard minor modes. Elements must have the form
67 ;; (MINOR-MODE . RESTORE-FUNCTION).
69 ;; Functions are called by `desktop-create-buffer' to restore minor modes.
70 ;; Minor modes not specified here, are restored by the standard minor mode
71 ;; function. If you write a module that defines a major or minor mode that
72 ;; needs a special handler, then place code like
74 ;; (defun foo-restore-desktop-buffer
76 ;; (add-to-list 'desktop-buffer-mode-handlers
77 ;; '(foo-mode . foo-restore-desktop-buffer))
81 ;; (defun bar-desktop-restore
83 ;; (add-to-list 'desktop-minor-mode-handlers
84 ;; '(bar-mode . bar-desktop-restore))
86 ;; in the module itself, and make sure that the mode function is
87 ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and
88 ;; `desktop-minor-mode-handlers' for more info.
92 ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp
93 ;; manual) are handled in the following way:
94 ;; When `desktop-save' saves the state of a buffer to the desktop file, it
95 ;; saves as `desktop-minor-modes' the list of names of those variables in
96 ;; `minor-mode-alist' that have a non-nil value.
97 ;; When `desktop-create' restores the buffer, each of the symbols in
98 ;; `desktop-minor-modes' is called as function with parameter 1.
99 ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers'
100 ;; are used to handle non-conventional minor modes. `desktop-save' uses
101 ;; `desktop-minor-mode-table' to map minor mode variables to minor mode
102 ;; functions before writing `desktop-minor-modes'. If a minor mode has a
103 ;; variable name that is different form its function name, an entry
105 ;; (NAME RESTORE-FUNCTION)
107 ;; should be added to `desktop-minor-mode-table'. If a minor mode should not
108 ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses
109 ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore
110 ;; function different from the usual minor mode function.
111 ;; ---------------------------------------------------------------------------
113 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
114 ;; in your home directory is used for that. Saving global default values
115 ;; for buffers is an example of misuse.
117 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
118 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
119 ;; things you did not mean to keep. Use M-x desktop-clear RET.
121 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
122 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
123 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
124 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
125 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
126 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
127 ;; pot@cnuce.cnr.it (Francesco Potortì) for misc. tips.
128 ;; ---------------------------------------------------------------------------
131 ;; Recognize more minor modes.
139 (defvar desktop-file-version
"208"
140 "Version number of desktop file format.
141 Written into the desktop file and used at desktop read to provide
142 backward compatibility.")
144 ;; ----------------------------------------------------------------------------
145 ;; USER OPTIONS -- settings you might want to play with.
146 ;; ----------------------------------------------------------------------------
148 (defgroup desktop nil
149 "Save status of Emacs when you exit."
152 ;; Maintained for backward compatibility
153 (define-obsolete-variable-alias 'desktop-enable
'desktop-save-mode
"22.1")
155 (define-minor-mode desktop-save-mode
156 "Toggle desktop saving (Desktop Save mode).
157 With a prefix argument ARG, enable Desktop Save mode if ARG is positive,
158 and disable it otherwise. If called from Lisp, enable the mode if ARG
161 When Desktop Save mode is enabled, the state of Emacs is saved from
162 one session to another. In particular, Emacs will save the desktop when
163 it exits (this may prompt you; see the option `desktop-save'). The next
164 time Emacs starts, if this mode is active it will restore the desktop.
166 To manually save the desktop at any time, use the command `M-x desktop-save'.
167 To load it, use `M-x desktop-read'.
169 Once a desktop file exists, Emacs will auto-save it according to the
170 option `desktop-auto-save-timeout'.
172 To see all the options you can set, browse the `desktop' customization group.
174 For further details, see info node `(emacs)Saving Emacs Sessions'."
177 (if desktop-save-mode
178 (desktop-auto-save-enable)
179 (desktop-auto-save-disable)))
181 (defun desktop-save-mode-off ()
182 "Disable `desktop-save-mode'. Provided for use in hooks."
183 (desktop-save-mode 0))
185 (defcustom desktop-save
'ask-if-new
186 "Specifies whether the desktop should be saved when it is killed.
187 A desktop is killed when the user changes desktop or quits Emacs.
191 ask-if-new -- ask if no desktop file exists, otherwise just save.
192 ask-if-exists -- ask if desktop file exists, otherwise don't save.
193 if-exists -- save if desktop file exists, otherwise don't save.
195 The desktop is never saved when `desktop-save-mode' is nil.
196 The variables `desktop-dirname' and `desktop-base-file-name'
197 determine where the desktop is saved."
200 (const :tag
"Always save" t
)
201 (const :tag
"Always ask" ask
)
202 (const :tag
"Ask if desktop file is new, else do save" ask-if-new
)
203 (const :tag
"Ask if desktop file exists, else don't save" ask-if-exists
)
204 (const :tag
"Save if desktop file exists, else don't" if-exists
)
205 (const :tag
"Never save" nil
))
209 (defcustom desktop-auto-save-timeout auto-save-timeout
210 "Number of seconds idle time before auto-save of the desktop.
211 The idle timer activates auto-saving only when window configuration changes.
212 This applies to an existing desktop file when `desktop-save-mode' is enabled.
213 Zero or nil means disable auto-saving due to idleness."
214 :type
'(choice (const :tag
"Off" nil
)
215 (integer :tag
"Seconds"))
216 :set
(lambda (symbol value
)
217 (set-default symbol value
)
219 (if (and (integerp value
) (> value
0))
220 (desktop-auto-save-enable value
)
221 (desktop-auto-save-disable))))
225 (defcustom desktop-load-locked-desktop
'ask
226 "Specifies whether the desktop should be loaded if locked.
231 If the value is nil, or `ask' and the user chooses not to load the desktop,
232 the normal hook `desktop-not-loaded-hook' is run."
235 (const :tag
"Load anyway" t
)
236 (const :tag
"Don't load" nil
)
237 (const :tag
"Ask the user" ask
))
241 (define-obsolete-variable-alias 'desktop-basefilename
242 'desktop-base-file-name
"22.1")
244 (defcustom desktop-base-file-name
245 (convert-standard-filename ".emacs.desktop")
246 "Name of file for Emacs desktop, excluding the directory part."
250 (defcustom desktop-base-lock-name
251 (convert-standard-filename ".emacs.desktop.lock")
252 "Name of lock file for Emacs desktop, excluding the directory part."
257 (defcustom desktop-path
(list user-emacs-directory
"~")
258 "List of directories to search for the desktop file.
259 The base name of the file is specified in `desktop-base-file-name'."
260 :type
'(repeat directory
)
262 :version
"23.2") ; user-emacs-directory added
264 (defcustom desktop-missing-file-warning nil
265 "If non-nil, offer to recreate the buffer of a deleted file.
266 Also pause for a moment to display message about errors signaled in
267 `desktop-buffer-mode-handlers'.
269 If nil, just print error messages in the message buffer."
274 (defcustom desktop-no-desktop-file-hook nil
275 "Normal hook run when `desktop-read' can't find a desktop file.
276 Run in the directory in which the desktop file was sought.
277 May be used to show a dired buffer."
282 (defcustom desktop-not-loaded-hook nil
283 "Normal hook run when the user declines to re-use a desktop file.
284 Run in the directory in which the desktop file was found.
285 May be used to deal with accidental multiple Emacs jobs."
288 :options
'(desktop-save-mode-off save-buffers-kill-emacs
)
291 (defcustom desktop-after-read-hook nil
292 "Normal hook run after a successful `desktop-read'.
293 May be used to show a buffer list."
296 :options
'(list-buffers)
299 (defcustom desktop-save-hook nil
300 "Normal hook run before the desktop is saved in a desktop file.
301 Run with the desktop buffer current with only the header present.
302 May be used to add to the desktop code or to truncate history lists,
307 (defcustom desktop-globals-to-save
308 '(desktop-missing-file-warning
315 "List of global variables saved by `desktop-save'.
316 An element may be variable name (a symbol) or a cons cell of the form
317 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
318 MAX-SIZE elements (if the value is a list) before saving the value.
319 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
320 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
323 (defcustom desktop-globals-to-clear
325 kill-ring-yank-pointer
327 search-ring-yank-pointer
329 regexp-search-ring-yank-pointer
)
330 "List of global variables that `desktop-clear' will clear.
331 An element may be variable name (a symbol) or a cons cell of the form
332 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
333 to the value obtained by evaluating FORM."
334 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
338 (defcustom desktop-clear-preserve-buffers
339 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*"
341 "List of buffers that `desktop-clear' should not delete.
342 Each element is a regular expression. Buffers with a name matched by any of
343 these won't be deleted."
344 :version
"23.3" ; added Warnings - bug#6336
345 :type
'(repeat string
)
349 (defcustom desktop-locals-to-save
350 '(desktop-locals-to-save ; Itself! Think it over.
356 change-log-default-name
360 buffer-file-coding-system
363 indicate-buffer-boundaries
365 show-trailing-whitespace
)
366 "List of local variables to save for each buffer.
367 The variables are saved only when they really are local. Conventional minor
368 modes are restored automatically; they should not be listed here."
369 :type
'(repeat symbol
)
372 (defcustom desktop-buffers-not-to-save
"\\` "
373 "Regexp identifying buffers that are to be excluded from saving."
374 :type
'(choice (const :tag
"None" nil
)
376 :version
"24.4" ; skip invisible temporary buffers
379 ;; Skip tramp and ange-ftp files
380 (defcustom desktop-files-not-to-save
381 "\\(^/[^/:]*:\\|(ftp)$\\)"
382 "Regexp identifying files whose buffers are to be excluded from saving."
383 :type
'(choice (const :tag
"None" nil
)
387 ;; We skip TAGS files to save time (tags-file-name is saved instead).
388 (defcustom desktop-modes-not-to-save
390 "List of major modes whose buffers should not be saved."
391 :type
'(repeat symbol
)
394 (defcustom desktop-restore-frames t
395 "When non-nil, save and restore the frame and window configuration.
396 See related options `desktop-restore-reuses-frames',
397 `desktop-restore-in-current-display', and `desktop-restore-forces-onscreen'."
402 (defcustom desktop-restore-in-current-display nil
403 "Controls how restoring of frames treats displays.
404 If t, restores frames into the current display.
405 If nil, restores frames into their original displays (if possible).
406 If `delete', deletes frames on other displays instead of restoring them."
407 :type
'(choice (const :tag
"Restore in current display" t
)
408 (const :tag
"Restore in original display" nil
)
409 (const :tag
"Delete frames in other displays" delete
))
413 (defcustom desktop-restore-forces-onscreen t
414 "If t, restores frames that are fully offscreen onscreen instead.
415 If `all', also restores frames that are partially offscreen onscreen.
417 Note that checking of frame boundaries is only approximate.
418 It can fail to reliably detect frames whose onscreen/offscreen state
419 depends on a few pixels, especially near the right / bottom borders
421 :type
'(choice (const :tag
"Only fully offscreen frames" t
)
422 (const :tag
"Also partially offscreen frames" all
)
423 (const :tag
"Do not force frames onscreen" nil
))
427 (defcustom desktop-restore-reuses-frames t
428 "If t, restoring frames reuses existing frames.
429 If nil, deletes existing frames.
430 If `keep', keeps existing frames and does not reuse them."
431 :type
'(choice (const :tag
"Reuse existing frames" t
)
432 (const :tag
"Delete existing frames" nil
)
433 (const :tag
"Keep existing frames" :keep
))
437 (defcustom desktop-file-name-format
'absolute
438 "Format in which desktop file names should be saved.
440 absolute -- Absolute file name.
441 tilde -- Relative to ~.
442 local -- Relative to directory of desktop file."
443 :type
'(choice (const absolute
) (const tilde
) (const local
))
447 (defcustom desktop-restore-eager t
448 "Number of buffers to restore immediately.
449 Remaining buffers are restored lazily (when Emacs is idle).
450 If value is t, all buffers are restored immediately."
451 :type
'(choice (const t
) integer
)
455 (defcustom desktop-lazy-verbose t
456 "Verbose reporting of lazily created buffers."
461 (defcustom desktop-lazy-idle-delay
5
462 "Idle delay before starting to create buffers.
463 See `desktop-restore-eager'."
469 (defvar-local desktop-save-buffer nil
470 "When non-nil, save buffer status in desktop file.
472 If the value is a function, it is called by `desktop-save' with argument
473 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
474 file along with the state of the buffer for which it was called.
476 When file names are returned, they should be formatted using the call
477 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
479 Later, when `desktop-read' evaluates the desktop file, auxiliary information
480 is passed as the argument DESKTOP-BUFFER-MISC to functions in
481 `desktop-buffer-mode-handlers'.")
482 (make-obsolete-variable 'desktop-buffer-modes-to-save
483 'desktop-save-buffer
"22.1")
484 (make-obsolete-variable 'desktop-buffer-misc-functions
485 'desktop-save-buffer
"22.1")
488 (defvar desktop-buffer-mode-handlers nil
489 "Alist of major mode specific functions to restore a desktop buffer.
490 Functions listed are called by `desktop-create-buffer' when `desktop-read'
491 evaluates the desktop file. List elements must have the form
493 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
495 Buffers with a major mode not specified here, are restored by the default
496 handler `desktop-restore-file-buffer'.
498 Handlers are called with argument list
500 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
502 Furthermore, they may use the following variables:
504 `desktop-file-version'
505 `desktop-buffer-major-mode'
506 `desktop-buffer-minor-modes'
507 `desktop-buffer-point'
508 `desktop-buffer-mark'
509 `desktop-buffer-read-only'
510 `desktop-buffer-locals'
512 If a handler returns a buffer, then the saved mode settings
513 and variable values for that buffer are copied into it.
515 Modules that define a major mode that needs a special handler should contain
518 (defun foo-restore-desktop-buffer
520 (add-to-list 'desktop-buffer-mode-handlers
521 '(foo-mode . foo-restore-desktop-buffer))
523 Furthermore the major mode function must be autoloaded.")
526 (put 'desktop-buffer-mode-handlers
'risky-local-variable t
)
527 (make-obsolete-variable 'desktop-buffer-handlers
528 'desktop-buffer-mode-handlers
"22.1")
530 (defcustom desktop-minor-mode-table
531 '((auto-fill-function auto-fill-mode
)
532 (defining-kbd-macro nil
)
536 (erc-track-minor-mode nil
)
538 "Table mapping minor mode variables to minor mode functions.
539 Each entry has the form (NAME RESTORE-FUNCTION).
540 NAME is the name of the buffer-local variable indicating that the minor
541 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
542 RESTORE-FUNCTION nil means don't try to restore the minor mode.
543 Only minor modes for which the name of the buffer-local variable
544 and the name of the minor mode function are different have to be added to
545 this table. See also `desktop-minor-mode-handlers'."
550 (defvar desktop-minor-mode-handlers nil
551 "Alist of functions to restore non-standard minor modes.
552 Functions are called by `desktop-create-buffer' to restore minor modes.
553 List elements must have the form
555 (MINOR-MODE . RESTORE-FUNCTION).
557 Minor modes not specified here, are restored by the standard minor mode
560 Handlers are called with argument list
562 (DESKTOP-BUFFER-LOCALS)
564 Furthermore, they may use the following variables:
566 `desktop-file-version'
567 `desktop-buffer-file-name'
568 `desktop-buffer-name'
569 `desktop-buffer-major-mode'
570 `desktop-buffer-minor-modes'
571 `desktop-buffer-point'
572 `desktop-buffer-mark'
573 `desktop-buffer-read-only'
574 `desktop-buffer-misc'
576 When a handler is called, the buffer has been created and the major mode has
577 been set, but local variables listed in desktop-buffer-locals has not yet been
580 Modules that define a minor mode that needs a special handler should contain
583 (defun foo-desktop-restore
585 (add-to-list 'desktop-minor-mode-handlers
586 '(foo-mode . foo-desktop-restore))
588 Furthermore the minor mode function must be autoloaded.
590 See also `desktop-minor-mode-table'.")
593 (put 'desktop-minor-mode-handlers
'risky-local-variable t
)
595 ;; ----------------------------------------------------------------------------
596 (defvar desktop-dirname nil
597 "The directory in which the desktop file should be saved.")
599 (defun desktop-full-file-name (&optional dirname
)
600 "Return the full name of the desktop file in DIRNAME.
601 DIRNAME omitted or nil means use `desktop-dirname'."
602 (expand-file-name desktop-base-file-name
(or dirname desktop-dirname
)))
604 (defun desktop-full-lock-name (&optional dirname
)
605 "Return the full name of the desktop lock file in DIRNAME.
606 DIRNAME omitted or nil means use `desktop-dirname'."
607 (expand-file-name desktop-base-lock-name
(or dirname desktop-dirname
)))
609 (defconst desktop-header
610 ";; --------------------------------------------------------------------------
611 ;; Desktop File for Emacs
612 ;; --------------------------------------------------------------------------
613 " "*Header to place in Desktop file.")
615 (defvar desktop-delay-hook nil
616 "Hooks run after all buffers are loaded; intended for internal use.")
618 (defvar desktop-file-checksum nil
619 "Checksum of the last auto-saved contents of the desktop file.
620 Used to avoid writing contents unchanged between auto-saves.")
622 (defvar desktop-saved-frameset nil
623 "Saved state of all frames.
624 Only valid during frame saving & restoring; intended for internal use.")
626 ;; ----------------------------------------------------------------------------
627 ;; Desktop file conflict detection
628 (defvar desktop-file-modtime nil
629 "When the desktop file was last modified to the knowledge of this Emacs.
630 Used to detect desktop file conflicts.")
632 (defvar desktop-var-serdes-funs
636 (mapcar #'marker-position mr
))
638 (mapcar #'copy-marker mr
))))
639 "Table of serialization/deserialization functions for variables.
640 Each record is a list of form: (var serializer deserializer).
641 These records can be freely reordered, deleted, or new ones added.
642 However, for compatibility, don't modify the functions for existing records.")
644 (defun desktop-owner (&optional dirname
)
645 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
646 Return nil if no desktop file found or no Emacs process is using it.
647 DIRNAME omitted or nil means use `desktop-dirname'."
649 (file (desktop-full-lock-name dirname
)))
650 (and (file-exists-p file
)
653 (insert-file-contents-literally file
)
654 (goto-char (point-min))
655 (setq owner
(read (current-buffer)))
659 (defun desktop-claim-lock (&optional dirname
)
660 "Record this Emacs process as the owner of the desktop file in DIRNAME.
661 DIRNAME omitted or nil means use `desktop-dirname'."
662 (write-region (number-to-string (emacs-pid)) nil
663 (desktop-full-lock-name dirname
)))
665 (defun desktop-release-lock (&optional dirname
)
666 "Remove the lock file for the desktop in DIRNAME.
667 DIRNAME omitted or nil means use `desktop-dirname'."
668 (let ((file (desktop-full-lock-name dirname
)))
669 (when (file-exists-p file
) (delete-file file
))))
671 ;; ----------------------------------------------------------------------------
672 (defun desktop-truncate (list n
)
673 "Truncate LIST to at most N elements destructively."
674 (let ((here (nthcdr (1- n
) list
)))
678 ;; ----------------------------------------------------------------------------
680 (defun desktop-clear ()
682 This kills all buffers except for internal ones and those with names matched by
683 a regular expression in the list `desktop-clear-preserve-buffers'.
684 Furthermore, it clears the variables listed in `desktop-globals-to-clear'.
685 When called interactively and `desktop-restore-frames' is non-nil, it also
686 deletes all frames except the selected one (and its minibuffer frame,
690 (dolist (var desktop-globals-to-clear
)
692 (eval `(setq-default ,var nil
))
693 (eval `(setq-default ,(car var
) ,(cdr var
)))))
694 (let ((preserve-regexp (concat "^\\("
695 (mapconcat (lambda (regexp)
696 (concat "\\(" regexp
"\\)"))
697 desktop-clear-preserve-buffers
700 (dolist (buffer (buffer-list))
701 (let ((bufname (buffer-name buffer
)))
702 (unless (or (eq (aref bufname
0) ?\s
) ;; Don't kill internal buffers
703 (string-match-p preserve-regexp bufname
))
704 (kill-buffer buffer
)))))
705 (delete-other-windows)
706 (when (and desktop-restore-frames
707 ;; Non-interactive calls to desktop-clear happen before desktop-read
708 ;; which already takes care of frame restoration and deletion.
709 (called-interactively-p 'any
))
710 (let* ((this (selected-frame))
711 (mini (window-frame (minibuffer-window this
)))) ; in case they differ
712 (dolist (frame (sort (frame-list) #'frameset-minibufferless-first-p
))
714 (unless (or (eq frame this
)
716 (frame-parameter frame
'desktop-dont-clear
))
717 (delete-frame frame
))
719 (delay-warning 'desktop
(error-message-string err
))))))))
721 ;; ----------------------------------------------------------------------------
722 (unless noninteractive
723 (add-hook 'kill-emacs-hook
'desktop-kill
))
725 (defun desktop-kill ()
726 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
727 If the desktop should be saved and `desktop-dirname'
728 is nil, ask the user where to save the desktop."
729 (when (and desktop-save-mode
730 (let ((exists (file-exists-p (desktop-full-file-name))))
731 (or (eq desktop-save t
)
732 (and exists
(eq desktop-save
'if-exists
))
733 ;; If it exists, but we aren't using it, we are going
734 ;; to ask for a new directory below.
735 (and exists desktop-dirname
(eq desktop-save
'ask-if-new
))
737 (or (memq desktop-save
'(ask ask-if-new
))
738 (and exists
(eq desktop-save
'ask-if-exists
)))
739 (y-or-n-p "Save desktop? ")))))
740 (unless desktop-dirname
741 (setq desktop-dirname
742 (file-name-as-directory
744 (read-directory-name "Directory for desktop file: " nil nil t
)))))
746 (desktop-save desktop-dirname t
)
748 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
749 (signal (car err
) (cdr err
))))))
750 ;; If we own it, we don't anymore.
751 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
753 ;; ----------------------------------------------------------------------------
754 (defun desktop-list* (&rest args
)
755 (and args
(apply #'cl-list
* args
)))
757 ;; ----------------------------------------------------------------------------
758 (defun desktop-buffer-info (buffer)
761 ;; base name of the buffer; replaces the buffer name if managed by uniquify
762 (and (fboundp 'uniquify-buffer-base-name
) (uniquify-buffer-base-name))
764 (desktop-file-name (buffer-file-name) desktop-dirname
)
770 #'(lambda (minor-mode)
771 (and (boundp minor-mode
)
772 (symbol-value minor-mode
)
773 (let* ((special (assq minor-mode desktop-minor-mode-table
))
774 (value (cond (special (cadr special
))
775 ((functionp minor-mode
) minor-mode
))))
776 (when value
(add-to-list 'ret value
)))))
777 (mapcar #'car minor-mode-alist
))
779 ;; point and mark, and read-only status
781 (list (mark t
) mark-active
)
783 ;; auxiliary information
784 (when (functionp desktop-save-buffer
)
785 (funcall desktop-save-buffer desktop-dirname
))
787 (let ((loclist (buffer-local-variables))
789 (dolist (local desktop-locals-to-save
)
790 (let ((here (assq local loclist
)))
793 ((member local loclist
)
796 (mapcar (lambda (record)
797 (let ((var (car record
)))
799 (funcall (cadr record
) (symbol-value var
)))))
800 desktop-var-serdes-funs
)))
802 ;; ----------------------------------------------------------------------------
803 (defun desktop--v2s (value)
804 "Convert VALUE to a pair (QUOTE . SEXP); (eval SEXP) gives VALUE.
805 SEXP is an sexp that when evaluated yields VALUE.
806 QUOTE may be `may' (value may be quoted),
807 `must' (value must be quoted), or nil (value must not be quoted)."
809 ((or (numberp value
) (null value
) (eq t value
) (keywordp value
))
812 (let ((copy (copy-sequence value
)))
813 (set-text-properties 0 (length copy
) nil copy
)
814 ;; Get rid of text properties because we cannot read them.
819 (let* ((pass1 (mapcar #'desktop--v2s value
))
820 (special (assq nil pass1
)))
823 ,@(mapcar (lambda (el)
824 (if (eq (car el
) 'must
)
825 `',(cdr el
) (cdr el
)))
827 (cons 'may
`[,@(mapcar #'cdr pass1
)]))))
833 (let ((q.sexp
(desktop--v2s (car p
))))
834 (push q.sexp newlist
))
837 (let ((last (desktop--v2s p
)))
839 (push last newlist
)))
840 (if (assq nil newlist
)
842 `(,(if use-list
* 'desktop-list
* 'list
)
843 ,@(mapcar (lambda (el)
844 (if (eq (car el
) 'must
)
845 `',(cdr el
) (cdr el
)))
846 (nreverse newlist
))))
849 (nreverse (if use-list
* (cdr newlist
) newlist
)))
850 ,@(if use-list
* (cdar newlist
)))))))
852 (cons nil
`(symbol-function
853 ',(intern-soft (substring (prin1-to-string value
) 7 -
1)))))
855 (let ((pos (marker-position value
))
856 (buf (buffer-name (marker-buffer value
))))
858 `(let ((mk (make-marker)))
859 (add-hook 'desktop-delay-hook
861 (set-marker ,mk
,,pos
(get-buffer ,,buf
))))
864 (cons 'may
"Unprintable entity"))))
866 ;; ----------------------------------------------------------------------------
867 (defun desktop-value-to-string (value)
868 "Convert VALUE to a string that when read evaluates to the same value.
869 Not all types of values are supported."
870 (let* ((print-escape-newlines t
)
873 (float-output-format nil
)
874 (quote.sexp
(desktop--v2s value
))
875 (quote (car quote.sexp
))
877 (txt (prin1-to-string (cdr quote.sexp
))))
882 ;; ----------------------------------------------------------------------------
883 (defun desktop-outvar (varspec)
884 "Output a setq statement for variable VAR to the desktop file.
885 The argument VARSPEC may be the variable name VAR (a symbol),
886 or a cons cell of the form (VAR . MAX-SIZE),
887 which means to truncate VAR's value to at most MAX-SIZE elements
888 \(if the value is a list) before saving the value."
891 (setq var
(car varspec
) size
(cdr varspec
))
894 (when (and (integerp size
)
897 (desktop-truncate (eval var
) size
))
901 (desktop-value-to-string (symbol-value var
))
904 ;; ----------------------------------------------------------------------------
905 (defun desktop-save-buffer-p (filename bufname mode
&rest _dummy
)
906 "Return t if buffer should have its state saved in the desktop file.
907 FILENAME is the visited file name, BUFNAME is the buffer name, and
908 MODE is the major mode.
909 \n\(fn FILENAME BUFNAME MODE)"
910 (let ((case-fold-search nil
)
911 (no-regexp-to-check (not (stringp desktop-files-not-to-save
)))
914 (not (stringp desktop-buffers-not-to-save
))
915 (not (string-match-p desktop-buffers-not-to-save bufname
)))
916 (not (memq mode desktop-modes-not-to-save
))
918 (or no-regexp-to-check
919 (not (string-match-p desktop-files-not-to-save filename
))))
920 (and (memq mode
'(dired-mode vc-dir-mode
))
921 (or no-regexp-to-check
922 (not (setq dired-skip
923 (with-current-buffer bufname
924 (string-match-p desktop-files-not-to-save
925 default-directory
))))))
927 (null dired-skip
) ; bug#5755
928 (with-current-buffer bufname desktop-save-buffer
)))
931 ;; ----------------------------------------------------------------------------
932 (defun desktop-file-name (filename dirname
)
933 "Convert FILENAME to format specified in `desktop-file-name-format'.
934 DIRNAME must be the directory in which the desktop file will be saved."
937 ((eq desktop-file-name-format
'tilde
)
938 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
940 ((file-name-absolute-p relative-name
) relative-name
)
941 ((string= "./" relative-name
) "~/")
942 ((string= "." relative-name
) "~")
943 (t (concat "~/" relative-name
)))))
944 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
945 (t (expand-file-name filename
))))
948 ;; ----------------------------------------------------------------------------
949 (defun desktop--check-dont-save (frame)
950 (not (frame-parameter frame
'desktop-dont-save
)))
952 (defconst desktop--app-id
`(desktop .
,desktop-file-version
))
954 (defun desktop-save-frameset ()
955 "Save the state of existing frames in `desktop-saved-frameset'.
956 Frames with a non-nil `desktop-dont-save' parameter are not saved."
957 (setq desktop-saved-frameset
958 (and desktop-restore-frames
961 :name
(concat user-login-name
"@" (system-name))
962 :predicate
#'desktop--check-dont-save
))))
965 (defun desktop-save (dirname &optional release only-if-changed
)
966 "Save the desktop in a desktop file.
967 Parameter DIRNAME specifies where to save the desktop file.
968 Optional parameter RELEASE says whether we're done with this desktop.
969 If ONLY-IF-CHANGED is non-nil, compare the current desktop information
970 to that in the desktop file, and if the desktop information has not
971 changed since it was last saved then do not rewrite the file."
973 ;; Or should we just use (car desktop-path)?
974 (let ((default (if (member "." desktop-path
)
976 user-emacs-directory
)))
977 (read-directory-name "Directory to save desktop file in: "
978 default default t
))))
979 (setq desktop-dirname
(file-name-as-directory (expand-file-name dirname
)))
981 (let ((eager desktop-restore-eager
)
982 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
984 (or (not new-modtime
) ; nothing to overwrite
985 (equal desktop-file-modtime new-modtime
)
986 (yes-or-no-p (if desktop-file-modtime
987 (if (> (float-time new-modtime
) (float-time desktop-file-modtime
))
988 "Desktop file is more recent than the one loaded. Save anyway? "
989 "Desktop file isn't the one loaded. Overwrite it? ")
990 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
991 (unless release
(error "Desktop file conflict")))
993 ;; If we're done with it, release the lock.
994 ;; Otherwise, claim it if it's unclaimed or if we created it.
996 (desktop-release-lock)
997 (unless (and new-modtime
(desktop-owner)) (desktop-claim-lock)))
1001 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
1003 ";; Created " (current-time-string) "\n"
1004 ";; Desktop file format version " desktop-file-version
"\n"
1005 ";; Emacs version " emacs-version
"\n")
1006 (save-excursion (run-hooks 'desktop-save-hook
))
1007 (goto-char (point-max))
1008 (insert "\n;; Global section:\n")
1009 ;; Called here because we save the window/frame state as a global
1010 ;; variable for compatibility with previous Emacsen.
1011 (desktop-save-frameset)
1012 (unless (memq 'desktop-saved-frameset desktop-globals-to-save
)
1013 (desktop-outvar 'desktop-saved-frameset
))
1014 (mapc (function desktop-outvar
) desktop-globals-to-save
)
1015 (setq desktop-saved-frameset nil
) ; after saving desktop-globals-to-save
1016 (when (memq 'kill-ring desktop-globals-to-save
)
1018 "(setq kill-ring-yank-pointer (nthcdr "
1019 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
1022 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
1023 (dolist (l (mapcar 'desktop-buffer-info
(buffer-list)))
1024 (let ((base (pop l
)))
1025 (when (apply 'desktop-save-buffer-p l
)
1027 (if (or (not (integerp eager
))
1030 (setq eager
(1- eager
))))
1031 "desktop-create-buffer"
1032 "desktop-append-buffer-args")
1034 desktop-file-version
)
1035 ;; If there's a non-empty base name, we save it instead of the buffer name
1036 (when (and base
(not (string= base
"")))
1037 (setcar (nthcdr 1 l
) base
))
1039 (insert "\n " (desktop-value-to-string e
)))
1042 (setq default-directory desktop-dirname
)
1043 ;; When auto-saving, avoid writing if nothing has changed since the last write.
1044 (let* ((beg (and only-if-changed
1046 (goto-char (point-min))
1047 ;; Don't check the header with changing timestamp
1048 (and (search-forward "Global section" nil t
)
1049 ;; Also skip the timestamp in desktop-saved-frameset
1050 ;; if it's saved in the first non-header line
1051 (search-forward "desktop-saved-frameset"
1052 (line-beginning-position 3) t
)
1053 ;; This is saved after the timestamp
1054 (search-forward (format "%S" desktop--app-id
) nil t
))
1056 (checksum (and beg
(md5 (current-buffer) beg
(point-max) 'emacs-mule
))))
1057 (unless (and checksum
(equal checksum desktop-file-checksum
))
1058 (let ((coding-system-for-write 'emacs-mule
))
1059 (write-region (point-min) (point-max) (desktop-full-file-name) nil
'nomessage
))
1060 (setq desktop-file-checksum checksum
)
1061 ;; We remember when it was modified (which is presumably just now).
1062 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name)))))))))))
1064 ;; ----------------------------------------------------------------------------
1066 (defun desktop-remove ()
1067 "Delete desktop file in `desktop-dirname'.
1068 This function also sets `desktop-dirname' to nil."
1070 (when desktop-dirname
1071 (let ((filename (desktop-full-file-name)))
1072 (setq desktop-dirname nil
)
1073 (when (file-exists-p filename
)
1074 (delete-file filename
)))))
1076 (defvar desktop-buffer-args-list nil
1077 "List of args for `desktop-create-buffer'.")
1079 (defvar desktop-lazy-timer nil
)
1081 ;; ----------------------------------------------------------------------------
1082 (defun desktop-restoring-frameset-p ()
1083 "True if calling `desktop-restore-frameset' will actually restore it."
1084 (and desktop-restore-frames desktop-saved-frameset t
))
1086 (defun desktop-restore-frameset ()
1087 "Restore the state of a set of frames.
1088 This function depends on the value of `desktop-saved-frameset'
1089 being set (usually, by reading it from the desktop)."
1090 (when (desktop-restoring-frameset-p)
1091 (frameset-restore desktop-saved-frameset
1092 :reuse-frames
(eq desktop-restore-reuses-frames t
)
1093 :cleanup-frames
(not (eq desktop-restore-reuses-frames
'keep
))
1094 :force-display desktop-restore-in-current-display
1095 :force-onscreen desktop-restore-forces-onscreen
)))
1097 ;; Just to silence the byte compiler.
1098 ;; Dynamically bound in `desktop-read'.
1099 (defvar desktop-first-buffer
)
1100 (defvar desktop-buffer-ok-count
)
1101 (defvar desktop-buffer-fail-count
)
1103 ;; FIXME Interactively, this should have the option to prompt for dirname.
1105 (defun desktop-read (&optional dirname
)
1106 "Read and process the desktop file in directory DIRNAME.
1107 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
1108 directories listed in `desktop-path'. If a desktop file is found, it
1109 is processed and `desktop-after-read-hook' is run. If no desktop file
1110 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
1111 This function is a no-op when Emacs is running in batch mode.
1112 It returns t if a desktop file was loaded, nil otherwise."
1114 (unless noninteractive
1115 (setq desktop-dirname
1116 (file-name-as-directory
1119 ;; If DIRNAME is specified, use it.
1120 (and (< 0 (length dirname
)) dirname
)
1121 ;; Otherwise search desktop file in desktop-path.
1122 (let ((dirs desktop-path
))
1125 (desktop-full-file-name (car dirs
)))))
1126 (setq dirs
(cdr dirs
)))
1127 (and dirs
(car dirs
)))
1128 ;; If not found and `desktop-path' is non-nil, use its first element.
1129 (and desktop-path
(car desktop-path
))
1130 ;; Default: .emacs.d.
1131 user-emacs-directory
))))
1132 (if (file-exists-p (desktop-full-file-name))
1133 ;; Desktop file found, but is it already in use?
1134 (let ((desktop-first-buffer nil
)
1135 (desktop-buffer-ok-count 0)
1136 (desktop-buffer-fail-count 0)
1137 (owner (desktop-owner))
1138 ;; Avoid desktop saving during evaluation of desktop buffer.
1140 (desktop-autosave-was-enabled))
1142 (memq desktop-load-locked-desktop
'(nil ask
))
1143 (or (null desktop-load-locked-desktop
)
1145 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
1146 Using it may cause conflicts. Use it anyway? " owner
)))))
1147 (let ((default-directory desktop-dirname
))
1148 (setq desktop-dirname nil
)
1149 (run-hooks 'desktop-not-loaded-hook
)
1150 (unless desktop-dirname
1151 (message "Desktop file in use; not loaded.")))
1152 (desktop-lazy-abort)
1153 ;; Temporarily disable the autosave that will leave it
1154 ;; disabled when loading the desktop fails with errors,
1155 ;; thus not overwriting the desktop with broken contents.
1156 (setq desktop-autosave-was-enabled
1157 (memq 'desktop-auto-save-set-timer window-configuration-change-hook
))
1158 (desktop-auto-save-disable)
1159 ;; Evaluate desktop buffer and remember when it was modified.
1160 (load (desktop-full-file-name) t t t
)
1161 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name))))
1162 ;; If it wasn't already, mark it as in-use, to bother other
1163 ;; desktop instances.
1164 (unless (eq (emacs-pid) owner
)
1166 (desktop-claim-lock)
1167 (file-error (message "Couldn't record use of desktop file")
1170 (unless (desktop-restoring-frameset-p)
1171 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1172 ;; We want buffers existing prior to evaluating the desktop (and
1173 ;; not reused) to be placed at the end of the buffer list, so we
1176 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
1177 (switch-to-buffer (car (buffer-list))))
1178 (run-hooks 'desktop-delay-hook
)
1179 (setq desktop-delay-hook nil
)
1180 (desktop-restore-frameset)
1181 (run-hooks 'desktop-after-read-hook
)
1182 (message "Desktop: %s%d buffer%s restored%s%s."
1183 (if desktop-saved-frameset
1184 (let ((fn (length (frameset-states desktop-saved-frameset
))))
1185 (format "%d frame%s, "
1186 fn
(if (= fn
1) "" "s")))
1188 desktop-buffer-ok-count
1189 (if (= 1 desktop-buffer-ok-count
) "" "s")
1190 (if (< 0 desktop-buffer-fail-count
)
1191 (format ", %d failed to restore" desktop-buffer-fail-count
)
1193 (if desktop-buffer-args-list
1194 (format ", %d to restore lazily"
1195 (length desktop-buffer-args-list
))
1197 (unless (desktop-restoring-frameset-p)
1198 ;; Bury the *Messages* buffer to not reshow it when burying
1199 ;; the buffer we switched to above.
1200 (when (buffer-live-p (get-buffer "*Messages*"))
1201 (bury-buffer "*Messages*"))
1202 ;; Clear all windows' previous and next buffers, these have
1203 ;; been corrupted by the `switch-to-buffer' calls in
1204 ;; `desktop-restore-file-buffer' (bug#11556). This is a
1205 ;; brute force fix and should be replaced by a more subtle
1206 ;; strategy eventually.
1207 (walk-window-tree (lambda (window)
1208 (set-window-prev-buffers window nil
)
1209 (set-window-next-buffers window nil
))))
1210 (setq desktop-saved-frameset nil
)
1211 (if desktop-autosave-was-enabled
(desktop-auto-save-enable))
1213 ;; No desktop file found.
1214 (let ((default-directory desktop-dirname
))
1215 (run-hooks 'desktop-no-desktop-file-hook
))
1216 (message "No desktop file.")
1219 ;; ----------------------------------------------------------------------------
1220 ;; Maintained for backward compatibility
1222 (defun desktop-load-default ()
1223 "Load the `default' start-up library manually.
1224 Also inhibit further loading of it."
1225 (declare (obsolete desktop-save-mode
"22.1"))
1226 (unless inhibit-default-init
; safety check
1227 (load "default" t t
)
1228 (setq inhibit-default-init t
)))
1230 ;; ----------------------------------------------------------------------------
1232 (defun desktop-change-dir (dirname)
1233 "Change to desktop saved in DIRNAME.
1234 Kill the desktop as specified by variables `desktop-save-mode' and
1235 `desktop-save', then clear the desktop and load the desktop file in
1237 (interactive "DChange to directory: ")
1238 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
1241 (desktop-read dirname
))
1243 ;; ----------------------------------------------------------------------------
1245 (defun desktop-save-in-desktop-dir ()
1246 "Save the desktop in directory `desktop-dirname'."
1249 (desktop-save desktop-dirname
)
1250 (call-interactively 'desktop-save
))
1251 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname
)))
1253 ;; ----------------------------------------------------------------------------
1255 (defvar desktop-auto-save-timer nil
)
1257 (defun desktop-auto-save-enable (&optional timeout
)
1258 (when (and (integerp (or timeout desktop-auto-save-timeout
))
1259 (> (or timeout desktop-auto-save-timeout
) 0))
1260 (add-hook 'window-configuration-change-hook
'desktop-auto-save-set-timer
)))
1262 (defun desktop-auto-save-disable ()
1263 (remove-hook 'window-configuration-change-hook
'desktop-auto-save-set-timer
)
1264 (desktop-auto-save-cancel-timer))
1266 (defun desktop-auto-save ()
1267 "Save the desktop periodically.
1268 Called by the timer created in `desktop-auto-save-set-timer'."
1269 (when (and desktop-save-mode
1270 (integerp desktop-auto-save-timeout
)
1271 (> desktop-auto-save-timeout
0)
1272 ;; Avoid desktop saving during lazy loading.
1273 (not desktop-lazy-timer
)
1274 ;; Save only to own desktop file.
1275 (eq (emacs-pid) (desktop-owner))
1277 (desktop-save desktop-dirname nil t
)))
1279 (defun desktop-auto-save-set-timer ()
1280 "Set the auto-save timer.
1281 Cancel any previous timer. When `desktop-auto-save-timeout' is a positive
1282 integer, start a new idle timer to call `desktop-auto-save' repeatedly
1283 after that many seconds of idle time."
1284 (desktop-auto-save-cancel-timer)
1285 (when (and (integerp desktop-auto-save-timeout
)
1286 (> desktop-auto-save-timeout
0))
1287 (setq desktop-auto-save-timer
1288 (run-with-idle-timer desktop-auto-save-timeout nil
1289 'desktop-auto-save
))))
1291 (defun desktop-auto-save-cancel-timer ()
1292 (when desktop-auto-save-timer
1293 (cancel-timer desktop-auto-save-timer
)
1294 (setq desktop-auto-save-timer nil
)))
1296 ;; ----------------------------------------------------------------------------
1298 (defun desktop-revert ()
1299 "Revert to the last loaded desktop."
1301 (unless desktop-dirname
1302 (error "Unknown desktop directory"))
1303 (unless (file-exists-p (desktop-full-file-name))
1304 (error "No desktop file found"))
1306 (desktop-read desktop-dirname
))
1308 (defvar desktop-buffer-major-mode
)
1309 (defvar desktop-buffer-locals
)
1310 (defvar auto-insert
) ; from autoinsert.el
1311 ;; ----------------------------------------------------------------------------
1312 (defun desktop-restore-file-buffer (buffer-filename
1315 "Restore a file buffer."
1316 (when buffer-filename
1317 (if (or (file-exists-p buffer-filename
)
1318 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1320 (if desktop-missing-file-warning
1321 (y-or-n-p (concat msg
" Re-create buffer? "))
1324 (let* ((auto-insert nil
) ; Disable auto insertion
1325 (coding-system-for-read
1326 (or coding-system-for-read
1327 (cdr (assq 'buffer-file-coding-system
1328 desktop-buffer-locals
))))
1329 (buf (find-file-noselect buffer-filename
)))
1331 (switch-to-buffer buf
)
1332 (error (pop-to-buffer buf
)))
1333 (and (not (eq major-mode desktop-buffer-major-mode
))
1334 (functionp desktop-buffer-major-mode
)
1335 (funcall desktop-buffer-major-mode
))
1339 (defun desktop-load-file (function)
1340 "Load the file where auto loaded FUNCTION is defined."
1341 (when (fboundp function
)
1342 (autoload-do-load (symbol-function function
) function
)))
1344 ;; ----------------------------------------------------------------------------
1345 ;; Create a buffer, load its file, set its mode, ...;
1346 ;; called from Desktop file only.
1348 (defun desktop-create-buffer
1363 (let ((desktop-file-version file-version
)
1364 (desktop-buffer-file-name buffer-filename
)
1365 (desktop-buffer-name buffer-name
)
1366 (desktop-buffer-major-mode buffer-majormode
)
1367 (desktop-buffer-minor-modes buffer-minormodes
)
1368 (desktop-buffer-point buffer-point
)
1369 (desktop-buffer-mark buffer-mark
)
1370 (desktop-buffer-read-only buffer-readonly
)
1371 (desktop-buffer-misc buffer-misc
)
1372 (desktop-buffer-locals buffer-locals
))
1373 ;; To make desktop files with relative file names possible, we cannot
1374 ;; allow `default-directory' to change. Therefore we save current buffer.
1375 (save-current-buffer
1376 ;; Give major mode module a chance to add a handler.
1377 (desktop-load-file desktop-buffer-major-mode
)
1378 (let ((buffer-list (buffer-list))
1380 (condition-case-unless-debug err
1381 (funcall (or (cdr (assq desktop-buffer-major-mode
1382 desktop-buffer-mode-handlers
))
1383 'desktop-restore-file-buffer
)
1384 desktop-buffer-file-name
1386 desktop-buffer-misc
)
1388 (message "Desktop: Can't load buffer %s: %s"
1390 (error-message-string err
))
1391 (when desktop-missing-file-warning
(sit-for 1))
1393 (if (bufferp result
)
1394 (setq desktop-buffer-ok-count
(1+ desktop-buffer-ok-count
))
1395 (setq desktop-buffer-fail-count
(1+ desktop-buffer-fail-count
))
1397 ;; Restore buffer list order with new buffer at end. Don't change
1398 ;; the order for old desktop files (old desktop module behavior).
1399 (unless (< desktop-file-version
206)
1400 (dolist (buf buffer-list
)
1401 (and (buffer-live-p buf
)
1403 (when result
(bury-buffer result
)))
1405 (unless (or desktop-first-buffer
(< desktop-file-version
206))
1406 (setq desktop-first-buffer result
))
1408 (unless (equal (buffer-name) desktop-buffer-name
)
1409 (rename-buffer desktop-buffer-name t
))
1411 (cond ((equal '(t) desktop-buffer-minor-modes
) ; backwards compatible
1413 ((equal '(nil) desktop-buffer-minor-modes
) ; backwards compatible
1416 (dolist (minor-mode desktop-buffer-minor-modes
)
1417 ;; Give minor mode module a chance to add a handler.
1418 (desktop-load-file minor-mode
)
1419 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers
))))
1421 (funcall handler desktop-buffer-locals
)
1422 (when (functionp minor-mode
)
1423 (funcall minor-mode
1)))))))
1424 ;; Even though point and mark are non-nil when written by
1425 ;; `desktop-save', they may be modified by handlers wanting to set
1426 ;; point or mark themselves.
1427 (when desktop-buffer-point
1430 ;; Evaluate point. Thus point can be something like
1431 ;; '(search-forward ...
1432 (eval desktop-buffer-point
)
1433 (error (message "%s" (error-message-string err
)) 1))))
1434 (when desktop-buffer-mark
1435 (if (consp desktop-buffer-mark
)
1437 (move-marker (mark-marker) (car desktop-buffer-mark
))
1438 (if (car (cdr desktop-buffer-mark
))
1439 (activate-mark 'dont-touch-tmm
)))
1440 (move-marker (mark-marker) desktop-buffer-mark
)))
1441 ;; Never override file system if the file really is read-only marked.
1442 (when desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
1443 (dolist (this desktop-buffer-locals
)
1445 ;; An entry of this form `(symbol . value)'.
1447 (make-local-variable (car this
))
1448 (set (car this
) (cdr this
)))
1449 ;; An entry of the form `symbol'.
1450 (make-local-variable this
)
1452 (unless (< desktop-file-version
208) ; Don't misinterpret any old custom args
1453 (dolist (record compacted-vars
)
1456 (deser-fun (cl-caddr (assq var desktop-var-serdes-funs
))))
1457 (if deser-fun
(set var
(funcall deser-fun
(cadr record
))))))))
1460 ;; ----------------------------------------------------------------------------
1461 ;; Backward compatibility -- update parameters to 205 standards.
1462 (defun desktop-buffer (buffer-filename buffer-name buffer-majormode
1463 mim pt mk ro tl fc cfs cr buffer-misc
)
1464 (desktop-create-buffer 205 buffer-filename buffer-name
1465 buffer-majormode
(cdr mim
) pt mk ro
1467 (list (cons 'truncate-lines tl
)
1468 (cons 'fill-column fc
)
1469 (cons 'case-fold-search cfs
)
1470 (cons 'case-replace cr
)
1471 (cons 'overwrite-mode
(car mim
)))))
1473 (defun desktop-append-buffer-args (&rest args
)
1474 "Append ARGS at end of `desktop-buffer-args-list'.
1475 ARGS must be an argument list for `desktop-create-buffer'."
1476 (setq desktop-buffer-args-list
(nconc desktop-buffer-args-list
(list args
)))
1477 (unless desktop-lazy-timer
1478 (setq desktop-lazy-timer
1479 (run-with-idle-timer desktop-lazy-idle-delay t
'desktop-idle-create-buffers
))))
1481 (defun desktop-lazy-create-buffer ()
1482 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1483 (when desktop-buffer-args-list
1484 (let* ((remaining (length desktop-buffer-args-list
))
1485 (args (pop desktop-buffer-args-list
))
1486 (buffer-name (nth 2 args
))
1487 (msg (format "Desktop lazily opening %s (%s remaining)..."
1488 buffer-name remaining
)))
1489 (when desktop-lazy-verbose
1491 (let ((desktop-first-buffer nil
)
1492 (desktop-buffer-ok-count 0)
1493 (desktop-buffer-fail-count 0))
1494 (apply 'desktop-create-buffer args
)
1495 (run-hooks 'desktop-delay-hook
)
1496 (setq desktop-delay-hook nil
)
1497 (bury-buffer (get-buffer buffer-name
))
1498 (when desktop-lazy-verbose
1499 (message "%s%s" msg
(if (> desktop-buffer-ok-count
0) "done" "failed")))))))
1501 (defun desktop-idle-create-buffers ()
1502 "Create buffers until the user does something, then stop.
1503 If there are no buffers left to create, kill the timer."
1505 (while (and repeat desktop-buffer-args-list
)
1506 (save-window-excursion
1507 (desktop-lazy-create-buffer))
1508 (setq repeat
(sit-for 0.2))
1509 (unless desktop-buffer-args-list
1510 (cancel-timer desktop-lazy-timer
)
1511 (setq desktop-lazy-timer nil
)
1512 (message "Lazy desktop load complete")
1516 (defun desktop-lazy-complete ()
1517 "Run the desktop load to completion."
1519 (let ((desktop-lazy-verbose t
))
1520 (while desktop-buffer-args-list
1521 (save-window-excursion
1522 (desktop-lazy-create-buffer)))
1523 (message "Lazy desktop load complete")))
1525 (defun desktop-lazy-abort ()
1526 "Abort lazy loading of the desktop."
1528 (when desktop-lazy-timer
1529 (cancel-timer desktop-lazy-timer
)
1530 (setq desktop-lazy-timer nil
))
1531 (when desktop-buffer-args-list
1532 (setq desktop-buffer-args-list nil
)
1533 (when (called-interactively-p 'interactive
)
1534 (message "Lazy desktop load aborted"))))
1536 ;; ----------------------------------------------------------------------------
1537 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1538 ;; command line, we do the rest of what it takes to use desktop, but do it
1539 ;; after finishing loading the init file.
1540 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1541 ;; functions are processed after `after-init-hook'.
1545 (let ((key "--no-desktop"))
1546 (when (member key command-line-args
)
1547 (setq command-line-args
(delete key command-line-args
))
1548 (desktop-save-mode 0)))
1549 (when desktop-save-mode
1550 ;; People don't expect emacs -nw, or --daemon,
1551 ;; to create graphical frames (bug#17693).
1552 ;; TODO perhaps there should be a separate value
1553 ;; for desktop-restore-frames to control this startup behavior?
1554 (let ((desktop-restore-frames (and desktop-restore-frames
1555 initial-window-system
1558 (setq inhibit-startup-screen t
)))))
1560 ;; So we can restore vc-dir buffers.
1561 (autoload 'vc-dir-mode
"vc-dir" nil t
)
1565 ;;; desktop.el ends here