1 ;;; desktop.el --- save partial status of Emacs when killed
3 ;; Copyright (C) 1993-1995, 1997, 2000-2013 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 Potorti`) for misc. tips.
128 ;; ---------------------------------------------------------------------------
131 ;; Recognize more minor modes.
139 (defvar desktop-file-version
"206"
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
158 positive, and disable it otherwise. If called from Lisp, enable
159 the mode if ARG is omitted or nil.
161 If Desktop Save mode is enabled, the state of Emacs is saved from
162 one session to another. See variable `desktop-save' and function
163 `desktop-read' for details."
167 (defun desktop-save-mode-off ()
168 "Disable `desktop-save-mode'. Provided for use in hooks."
169 (desktop-save-mode 0))
171 (defcustom desktop-save
'ask-if-new
172 "Specifies whether the desktop should be saved when it is killed.
173 A desktop is killed when the user changes desktop or quits Emacs.
177 ask-if-new -- ask if no desktop file exists, otherwise just save.
178 ask-if-exists -- ask if desktop file exists, otherwise don't save.
179 if-exists -- save if desktop file exists, otherwise don't save.
181 The desktop is never saved when `desktop-save-mode' is nil.
182 The variables `desktop-dirname' and `desktop-base-file-name'
183 determine where the desktop is saved."
186 (const :tag
"Always save" t
)
187 (const :tag
"Always ask" ask
)
188 (const :tag
"Ask if desktop file is new, else do save" ask-if-new
)
189 (const :tag
"Ask if desktop file exists, else don't save" ask-if-exists
)
190 (const :tag
"Save if desktop file exists, else don't" if-exists
)
191 (const :tag
"Never save" nil
))
195 (defcustom desktop-auto-save-timeout nil
196 "Number of seconds between auto-saves of the desktop.
197 Zero or nil means disable timer-based auto-saving."
198 :type
'(choice (const :tag
"Off" nil
)
199 (integer :tag
"Seconds"))
200 :set
(lambda (symbol value
)
201 (set-default symbol value
)
202 (ignore-errors (desktop-auto-save-set-timer)))
206 (defcustom desktop-load-locked-desktop
'ask
207 "Specifies whether the desktop should be loaded if locked.
212 If the value is nil, or `ask' and the user chooses not to load the desktop,
213 the normal hook `desktop-not-loaded-hook' is run."
216 (const :tag
"Load anyway" t
)
217 (const :tag
"Don't load" nil
)
218 (const :tag
"Ask the user" ask
))
222 (define-obsolete-variable-alias 'desktop-basefilename
223 'desktop-base-file-name
"22.1")
225 (defcustom desktop-base-file-name
226 (convert-standard-filename ".emacs.desktop")
227 "Name of file for Emacs desktop, excluding the directory part."
231 (defcustom desktop-base-lock-name
232 (convert-standard-filename ".emacs.desktop.lock")
233 "Name of lock file for Emacs desktop, excluding the directory part."
238 (defcustom desktop-path
(list user-emacs-directory
"~")
239 "List of directories to search for the desktop file.
240 The base name of the file is specified in `desktop-base-file-name'."
241 :type
'(repeat directory
)
243 :version
"23.2") ; user-emacs-directory added
245 (defcustom desktop-missing-file-warning nil
246 "If non-nil, offer to recreate the buffer of a deleted file.
247 Also pause for a moment to display message about errors signaled in
248 `desktop-buffer-mode-handlers'.
250 If nil, just print error messages in the message buffer."
255 (defcustom desktop-no-desktop-file-hook nil
256 "Normal hook run when `desktop-read' can't find a desktop file.
257 Run in the directory in which the desktop file was sought.
258 May be used to show a dired buffer."
263 (defcustom desktop-not-loaded-hook nil
264 "Normal hook run when the user declines to re-use a desktop file.
265 Run in the directory in which the desktop file was found.
266 May be used to deal with accidental multiple Emacs jobs."
269 :options
'(desktop-save-mode-off save-buffers-kill-emacs
)
272 (defcustom desktop-after-read-hook nil
273 "Normal hook run after a successful `desktop-read'.
274 May be used to show a buffer list."
277 :options
'(list-buffers)
280 (defcustom desktop-save-hook nil
281 "Normal hook run before the desktop is saved in a desktop file.
282 Run with the desktop buffer current with only the header present.
283 May be used to add to the desktop code or to truncate history lists,
288 (defcustom desktop-globals-to-save
289 '(desktop-missing-file-warning
296 "List of global variables saved by `desktop-save'.
297 An element may be variable name (a symbol) or a cons cell of the form
298 \(VAR . MAX-SIZE), which means to truncate VAR's value to at most
299 MAX-SIZE elements (if the value is a list) before saving the value.
300 Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'."
301 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
304 (defcustom desktop-globals-to-clear
306 kill-ring-yank-pointer
308 search-ring-yank-pointer
310 regexp-search-ring-yank-pointer
)
311 "List of global variables that `desktop-clear' will clear.
312 An element may be variable name (a symbol) or a cons cell of the form
313 \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set
314 to the value obtained by evaluating FORM."
315 :type
'(repeat (restricted-sexp :match-alternatives
(symbolp consp
)))
319 (defcustom desktop-clear-preserve-buffers
320 '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*"
322 "List of buffers that `desktop-clear' should not delete.
323 Each element is a regular expression. Buffers with a name matched by any of
324 these won't be deleted."
325 :version
"23.3" ; added Warnings - bug#6336
326 :type
'(repeat string
)
330 (defcustom desktop-locals-to-save
331 '(desktop-locals-to-save ; Itself! Think it over.
337 change-log-default-name
341 buffer-file-coding-system
344 indicate-buffer-boundaries
346 show-trailing-whitespace
)
347 "List of local variables to save for each buffer.
348 The variables are saved only when they really are local. Conventional minor
349 modes are restored automatically; they should not be listed here."
350 :type
'(repeat symbol
)
353 (defcustom desktop-buffers-not-to-save nil
354 "Regexp identifying buffers that are to be excluded from saving."
355 :type
'(choice (const :tag
"None" nil
)
357 :version
"23.2" ; set to nil
360 ;; Skip tramp and ange-ftp files
361 (defcustom desktop-files-not-to-save
362 "\\(^/[^/:]*:\\|(ftp)$\\)"
363 "Regexp identifying files whose buffers are to be excluded from saving."
364 :type
'(choice (const :tag
"None" nil
)
368 ;; We skip TAGS files to save time (tags-file-name is saved instead).
369 (defcustom desktop-modes-not-to-save
371 "List of major modes whose buffers should not be saved."
372 :type
'(repeat symbol
)
375 (defcustom desktop-restore-frames t
376 "When non-nil, save frames to desktop file."
381 (defcustom desktop-restore-in-current-display nil
382 "If t, frames are restored in the current display.
383 If nil, frames are restored, if possible, in their original displays.
384 If `delete', frames on other displays are deleted instead of restored."
385 :type
'(choice (const :tag
"Restore in current display" t
)
386 (const :tag
"Restore in original display" nil
)
387 (const :tag
"Delete frames in other displays" 'delete
))
391 (defcustom desktop-restore-forces-onscreen t
392 "If t, offscreen frames are restored onscreen instead.
393 If `all', frames that are partially offscreen are also forced onscren.
394 NOTE: Checking of frame boundaries is only approximate and can fail
395 to reliably detect frames whose onscreen/offscreen state depends on a
396 few pixels, especially near the right / bottom borders of the screen."
397 :type
'(choice (const :tag
"Only fully offscreen frames" t
)
398 (const :tag
"Also partially offscreen frames" 'all
)
399 (const :tag
"Do not force frames onscreen" nil
))
403 (defcustom desktop-restore-reuses-frames t
404 "If t, restoring frames reuses existing frames.
405 If nil, existing frames are deleted.
406 If `keep', existing frames are kept and not reused."
407 :type
'(choice (const :tag
"Reuse existing frames" t
)
408 (const :tag
"Delete existing frames" nil
)
409 (const :tag
"Keep existing frames" 'keep
))
413 (defcustom desktop-file-name-format
'absolute
414 "Format in which desktop file names should be saved.
416 absolute -- Absolute file name.
417 tilde -- Relative to ~.
418 local -- Relative to directory of desktop file."
419 :type
'(choice (const absolute
) (const tilde
) (const local
))
423 (defcustom desktop-restore-eager t
424 "Number of buffers to restore immediately.
425 Remaining buffers are restored lazily (when Emacs is idle).
426 If value is t, all buffers are restored immediately."
427 :type
'(choice (const t
) integer
)
431 (defcustom desktop-lazy-verbose t
432 "Verbose reporting of lazily created buffers."
437 (defcustom desktop-lazy-idle-delay
5
438 "Idle delay before starting to create buffers.
439 See `desktop-restore-eager'."
445 (defvar-local desktop-save-buffer nil
446 "When non-nil, save buffer status in desktop file.
448 If the value is a function, it is called by `desktop-save' with argument
449 DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop
450 file along with the state of the buffer for which it was called.
452 When file names are returned, they should be formatted using the call
453 \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\".
455 Later, when `desktop-read' evaluates the desktop file, auxiliary information
456 is passed as the argument DESKTOP-BUFFER-MISC to functions in
457 `desktop-buffer-mode-handlers'.")
458 (make-obsolete-variable 'desktop-buffer-modes-to-save
459 'desktop-save-buffer
"22.1")
460 (make-obsolete-variable 'desktop-buffer-misc-functions
461 'desktop-save-buffer
"22.1")
464 (defvar desktop-buffer-mode-handlers nil
465 "Alist of major mode specific functions to restore a desktop buffer.
466 Functions listed are called by `desktop-create-buffer' when `desktop-read'
467 evaluates the desktop file. List elements must have the form
469 (MAJOR-MODE . RESTORE-BUFFER-FUNCTION).
471 Buffers with a major mode not specified here, are restored by the default
472 handler `desktop-restore-file-buffer'.
474 Handlers are called with argument list
476 (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC)
478 Furthermore, they may use the following variables:
481 desktop-buffer-major-mode
482 desktop-buffer-minor-modes
485 desktop-buffer-read-only
486 desktop-buffer-locals
488 If a handler returns a buffer, then the saved mode settings
489 and variable values for that buffer are copied into it.
491 Modules that define a major mode that needs a special handler should contain
494 (defun foo-restore-desktop-buffer
496 (add-to-list 'desktop-buffer-mode-handlers
497 '(foo-mode . foo-restore-desktop-buffer))
499 Furthermore the major mode function must be autoloaded.")
502 (put 'desktop-buffer-mode-handlers
'risky-local-variable t
)
503 (make-obsolete-variable 'desktop-buffer-handlers
504 'desktop-buffer-mode-handlers
"22.1")
506 (defcustom desktop-minor-mode-table
507 '((auto-fill-function auto-fill-mode
)
510 (erc-track-minor-mode nil
)
512 "Table mapping minor mode variables to minor mode functions.
513 Each entry has the form (NAME RESTORE-FUNCTION).
514 NAME is the name of the buffer-local variable indicating that the minor
515 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
516 RESTORE-FUNCTION nil means don't try to restore the minor mode.
517 Only minor modes for which the name of the buffer-local variable
518 and the name of the minor mode function are different have to be added to
519 this table. See also `desktop-minor-mode-handlers'."
524 (defvar desktop-minor-mode-handlers nil
525 "Alist of functions to restore non-standard minor modes.
526 Functions are called by `desktop-create-buffer' to restore minor modes.
527 List elements must have the form
529 (MINOR-MODE . RESTORE-FUNCTION).
531 Minor modes not specified here, are restored by the standard minor mode
534 Handlers are called with argument list
536 (DESKTOP-BUFFER-LOCALS)
538 Furthermore, they may use the following variables:
541 desktop-buffer-file-name
543 desktop-buffer-major-mode
544 desktop-buffer-minor-modes
547 desktop-buffer-read-only
550 When a handler is called, the buffer has been created and the major mode has
551 been set, but local variables listed in desktop-buffer-locals has not yet been
554 Modules that define a minor mode that needs a special handler should contain
557 (defun foo-desktop-restore
559 (add-to-list 'desktop-minor-mode-handlers
560 '(foo-mode . foo-desktop-restore))
562 Furthermore the minor mode function must be autoloaded.
564 See also `desktop-minor-mode-table'.")
567 (put 'desktop-minor-mode-handlers
'risky-local-variable t
)
569 ;; ----------------------------------------------------------------------------
570 (defvar desktop-dirname nil
571 "The directory in which the desktop file should be saved.")
573 (defun desktop-full-file-name (&optional dirname
)
574 "Return the full name of the desktop file in DIRNAME.
575 DIRNAME omitted or nil means use `desktop-dirname'."
576 (expand-file-name desktop-base-file-name
(or dirname desktop-dirname
)))
578 (defun desktop-full-lock-name (&optional dirname
)
579 "Return the full name of the desktop lock file in DIRNAME.
580 DIRNAME omitted or nil means use `desktop-dirname'."
581 (expand-file-name desktop-base-lock-name
(or dirname desktop-dirname
)))
583 (defconst desktop-header
584 ";; --------------------------------------------------------------------------
585 ;; Desktop File for Emacs
586 ;; --------------------------------------------------------------------------
587 " "*Header to place in Desktop file.")
589 (defvar desktop-delay-hook nil
590 "Hooks run after all buffers are loaded; intended for internal use.")
592 (defvar desktop-file-checksum nil
593 "Checksum of the last auto-saved contents of the desktop file.
594 Used to avoid writing contents unchanged between auto-saves.")
596 (defvar desktop-saved-frameset nil
597 "Saved state of all frames.
598 Only valid during frame saving & restoring; intended for internal use.")
600 ;; ----------------------------------------------------------------------------
601 ;; Desktop file conflict detection
602 (defvar desktop-file-modtime nil
603 "When the desktop file was last modified to the knowledge of this Emacs.
604 Used to detect desktop file conflicts.")
606 (defun desktop-owner (&optional dirname
)
607 "Return the PID of the Emacs process that owns the desktop file in DIRNAME.
608 Return nil if no desktop file found or no Emacs process is using it.
609 DIRNAME omitted or nil means use `desktop-dirname'."
611 (file (desktop-full-lock-name dirname
)))
612 (and (file-exists-p file
)
615 (insert-file-contents-literally file
)
616 (goto-char (point-min))
617 (setq owner
(read (current-buffer)))
621 (defun desktop-claim-lock (&optional dirname
)
622 "Record this Emacs process as the owner of the desktop file in DIRNAME.
623 DIRNAME omitted or nil means use `desktop-dirname'."
624 (write-region (number-to-string (emacs-pid)) nil
625 (desktop-full-lock-name dirname
)))
627 (defun desktop-release-lock (&optional dirname
)
628 "Remove the lock file for the desktop in DIRNAME.
629 DIRNAME omitted or nil means use `desktop-dirname'."
630 (let ((file (desktop-full-lock-name dirname
)))
631 (when (file-exists-p file
) (delete-file file
))))
633 ;; ----------------------------------------------------------------------------
634 (defun desktop-truncate (list n
)
635 "Truncate LIST to at most N elements destructively."
636 (let ((here (nthcdr (1- n
) list
)))
640 ;; ----------------------------------------------------------------------------
642 (defun desktop-clear ()
644 This kills all buffers except for internal ones and those with names matched by
645 a regular expression in the list `desktop-clear-preserve-buffers'.
646 Furthermore, it clears the variables listed in `desktop-globals-to-clear'.
647 When called interactively and `desktop-restore-frames' is non-nil, it also
648 deletes all frames except the selected one (and its minibuffer frame,
652 (dolist (var desktop-globals-to-clear
)
654 (eval `(setq-default ,var nil
))
655 (eval `(setq-default ,(car var
) ,(cdr var
)))))
656 (let ((preserve-regexp (concat "^\\("
657 (mapconcat (lambda (regexp)
658 (concat "\\(" regexp
"\\)"))
659 desktop-clear-preserve-buffers
662 (dolist (buffer (buffer-list))
663 (let ((bufname (buffer-name buffer
)))
664 (unless (or (eq (aref bufname
0) ?\s
) ;; Don't kill internal buffers
665 (string-match-p preserve-regexp bufname
))
666 (kill-buffer buffer
)))))
667 (delete-other-windows)
668 (when (and desktop-restore-frames
669 ;; Non-interactive calls to desktop-clear happen before desktop-read
670 ;; which already takes care of frame restoration and deletion.
671 (called-interactively-p 'any
))
672 (let* ((this (selected-frame))
673 (mini (window-frame (minibuffer-window this
)))) ; in case they difer
674 (dolist (frame (sort (frame-list) #'frameset-sort-frames-for-deletion
))
676 (unless (or (eq frame this
)
678 (frame-parameter frame
'desktop-dont-clear
))
679 (delete-frame frame
))
681 (delay-warning 'desktop
(error-message-string err
))))))))
683 ;; ----------------------------------------------------------------------------
684 (unless noninteractive
685 (add-hook 'kill-emacs-hook
'desktop-kill
))
687 (defun desktop-kill ()
688 "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do.
689 If the desktop should be saved and `desktop-dirname'
690 is nil, ask the user where to save the desktop."
691 (when (and desktop-save-mode
692 (let ((exists (file-exists-p (desktop-full-file-name))))
693 (or (eq desktop-save t
)
694 (and exists
(eq desktop-save
'if-exists
))
695 ;; If it exists, but we aren't using it, we are going
696 ;; to ask for a new directory below.
697 (and exists desktop-dirname
(eq desktop-save
'ask-if-new
))
699 (or (memq desktop-save
'(ask ask-if-new
))
700 (and exists
(eq desktop-save
'ask-if-exists
)))
701 (y-or-n-p "Save desktop? ")))))
702 (unless desktop-dirname
703 (setq desktop-dirname
704 (file-name-as-directory
706 (read-directory-name "Directory for desktop file: " nil nil t
)))))
708 (desktop-save desktop-dirname t
)
710 (unless (yes-or-no-p "Error while saving the desktop. Ignore? ")
711 (signal (car err
) (cdr err
))))))
712 ;; If we own it, we don't anymore.
713 (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock)))
715 ;; ----------------------------------------------------------------------------
716 (defun desktop-list* (&rest args
)
717 (and args
(apply #'cl-list
* args
)))
719 ;; ----------------------------------------------------------------------------
720 (defun desktop-buffer-info (buffer)
723 ;; base name of the buffer; replaces the buffer name if managed by uniquify
724 (and (fboundp 'uniquify-buffer-base-name
) (uniquify-buffer-base-name))
726 (desktop-file-name (buffer-file-name) desktop-dirname
)
732 #'(lambda (minor-mode)
733 (and (boundp minor-mode
)
734 (symbol-value minor-mode
)
735 (let* ((special (assq minor-mode desktop-minor-mode-table
))
736 (value (cond (special (cadr special
))
737 ((functionp minor-mode
) minor-mode
))))
738 (when value
(add-to-list 'ret value
)))))
739 (mapcar #'car minor-mode-alist
))
741 ;; point and mark, and read-only status
743 (list (mark t
) mark-active
)
745 ;; auxiliary information
746 (when (functionp desktop-save-buffer
)
747 (funcall desktop-save-buffer desktop-dirname
))
749 (let ((loclist (buffer-local-variables))
751 (dolist (local desktop-locals-to-save
)
752 (let ((here (assq local loclist
)))
755 ((member local loclist
)
759 ;; ----------------------------------------------------------------------------
760 (defun desktop--v2s (value)
761 "Convert VALUE to a pair (QUOTE . SEXP); (eval SEXP) gives VALUE.
762 SEXP is an sexp that when evaluated yields VALUE.
763 QUOTE may be `may' (value may be quoted),
764 `must' (value must be quoted), or nil (value must not be quoted)."
766 ((or (numberp value
) (null value
) (eq t value
) (keywordp value
))
769 (let ((copy (copy-sequence value
)))
770 (set-text-properties 0 (length copy
) nil copy
)
771 ;; Get rid of text properties because we cannot read them.
776 (let* ((pass1 (mapcar #'desktop--v2s value
))
777 (special (assq nil pass1
)))
780 ,@(mapcar (lambda (el)
781 (if (eq (car el
) 'must
)
782 `',(cdr el
) (cdr el
)))
784 (cons 'may
`[,@(mapcar #'cdr pass1
)]))))
790 (let ((q.sexp
(desktop--v2s (car p
))))
791 (push q.sexp newlist
))
794 (let ((last (desktop--v2s p
)))
796 (push last newlist
)))
797 (if (assq nil newlist
)
799 `(,(if use-list
* 'desktop-list
* 'list
)
800 ,@(mapcar (lambda (el)
801 (if (eq (car el
) 'must
)
802 `',(cdr el
) (cdr el
)))
803 (nreverse newlist
))))
806 (nreverse (if use-list
* (cdr newlist
) newlist
)))
807 ,@(if use-list
* (cdar newlist
)))))))
809 (cons nil
`(symbol-function
810 ',(intern-soft (substring (prin1-to-string value
) 7 -
1)))))
812 (let ((pos (marker-position value
))
813 (buf (buffer-name (marker-buffer value
))))
815 `(let ((mk (make-marker)))
816 (add-hook 'desktop-delay-hook
818 (set-marker ,mk
,,pos
(get-buffer ,,buf
))))
821 (cons 'may
"Unprintable entity"))))
823 ;; ----------------------------------------------------------------------------
824 (defun desktop-value-to-string (value)
825 "Convert VALUE to a string that when read evaluates to the same value.
826 Not all types of values are supported."
827 (let* ((print-escape-newlines t
)
828 (float-output-format nil
)
829 (quote.sexp
(desktop--v2s value
))
830 (quote (car quote.sexp
))
832 (let ((print-quoted t
))
833 (prin1-to-string (cdr quote.sexp
)))))
838 ;; ----------------------------------------------------------------------------
839 (defun desktop-outvar (varspec)
840 "Output a setq statement for variable VAR to the desktop file.
841 The argument VARSPEC may be the variable name VAR (a symbol),
842 or a cons cell of the form (VAR . MAX-SIZE),
843 which means to truncate VAR's value to at most MAX-SIZE elements
844 \(if the value is a list) before saving the value."
847 (setq var
(car varspec
) size
(cdr varspec
))
850 (when (and (integerp size
)
853 (desktop-truncate (eval var
) size
))
857 (desktop-value-to-string (symbol-value var
))
860 ;; ----------------------------------------------------------------------------
861 (defun desktop-save-buffer-p (filename bufname mode
&rest _dummy
)
862 "Return t if buffer should have its state saved in the desktop file.
863 FILENAME is the visited file name, BUFNAME is the buffer name, and
864 MODE is the major mode.
865 \n\(fn FILENAME BUFNAME MODE)"
866 (let ((case-fold-search nil
)
868 (and (not (and (stringp desktop-buffers-not-to-save
)
870 (string-match-p desktop-buffers-not-to-save bufname
)))
871 (not (memq mode desktop-modes-not-to-save
))
872 ;; FIXME this is broken if desktop-files-not-to-save is nil.
874 (stringp desktop-files-not-to-save
)
875 (not (string-match-p desktop-files-not-to-save filename
)))
876 (and (memq mode
'(dired-mode vc-dir-mode
))
877 (with-current-buffer bufname
878 (not (setq dired-skip
879 (string-match-p desktop-files-not-to-save
880 default-directory
)))))
882 (null dired-skip
) ; bug#5755
883 (with-current-buffer bufname desktop-save-buffer
))))))
885 ;; ----------------------------------------------------------------------------
886 (defun desktop-file-name (filename dirname
)
887 "Convert FILENAME to format specified in `desktop-file-name-format'.
888 DIRNAME must be the directory in which the desktop file will be saved."
891 ((eq desktop-file-name-format
'tilde
)
892 (let ((relative-name (file-relative-name (expand-file-name filename
) "~")))
894 ((file-name-absolute-p relative-name
) relative-name
)
895 ((string= "./" relative-name
) "~/")
896 ((string= "." relative-name
) "~")
897 (t (concat "~/" relative-name
)))))
898 ((eq desktop-file-name-format
'local
) (file-relative-name filename dirname
))
899 (t (expand-file-name filename
))))
902 ;; ----------------------------------------------------------------------------
903 (defvar desktop-filter-parameters-alist
904 (append '((font-backend . t
)
906 (outer-window-id . t
)
908 (tty . desktop--filter-tty
*)
909 (tty-type . desktop--filter-tty
*)
912 frameset-filter-alist
)
913 "Alist of frame parameters and filtering functions.
914 Its format is identical to `frameset-filter-alist' (which see).")
916 (defun desktop--filter-tty* (_current parameters saving
)
917 ;; Remove tty and tty-type parameters when switching
920 (not (frameset-switch-to-gui-p parameters
))))
922 (defun desktop--check-dont-save (frame)
923 (not (frame-parameter frame
'desktop-dont-save
)))
925 (defconst desktop--app-id
`(desktop .
,desktop-file-version
))
927 (defun desktop-save-frameset ()
928 "Save the state of existing frames in `desktop-saved-frameset'.
929 Frames with a non-nil `desktop-dont-save' parameter are not saved."
930 (setq desktop-saved-frameset
931 (and desktop-restore-frames
932 (let ((name (concat user-login-name
"@" system-name
933 (format-time-string " %Y-%m-%d %T"))))
935 :filters desktop-filter-parameters-alist
936 :predicate
#'desktop--check-dont-save
937 :properties
(list :app desktop--app-id
941 (defun desktop-save (dirname &optional release auto-save
)
942 "Save the desktop in a desktop file.
943 Parameter DIRNAME specifies where to save the desktop file.
944 Optional parameter RELEASE says whether we're done with this desktop.
945 If AUTO-SAVE is non-nil, compare the saved contents to the one last saved,
946 and don't save the buffer if they are the same."
947 (interactive "DDirectory to save desktop file in: ")
948 (setq desktop-dirname
(file-name-as-directory (expand-file-name dirname
)))
950 (let ((eager desktop-restore-eager
)
951 (new-modtime (nth 5 (file-attributes (desktop-full-file-name)))))
953 (or (not new-modtime
) ; nothing to overwrite
954 (equal desktop-file-modtime new-modtime
)
955 (yes-or-no-p (if desktop-file-modtime
956 (if (> (float-time new-modtime
) (float-time desktop-file-modtime
))
957 "Desktop file is more recent than the one loaded. Save anyway? "
958 "Desktop file isn't the one loaded. Overwrite it? ")
959 "Current desktop was not loaded from a file. Overwrite this desktop file? "))
960 (unless release
(error "Desktop file conflict")))
962 ;; If we're done with it, release the lock.
963 ;; Otherwise, claim it if it's unclaimed or if we created it.
965 (desktop-release-lock)
966 (unless (and new-modtime
(desktop-owner)) (desktop-claim-lock)))
970 ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n"
972 ";; Created " (current-time-string) "\n"
973 ";; Desktop file format version " desktop-file-version
"\n"
974 ";; Emacs version " emacs-version
"\n")
975 (save-excursion (run-hooks 'desktop-save-hook
))
976 (goto-char (point-max))
977 (insert "\n;; Global section:\n")
978 ;; Called here because we save the window/frame state as a global
979 ;; variable for compatibility with previous Emacsen.
980 (desktop-save-frameset)
981 (unless (memq 'desktop-saved-frameset desktop-globals-to-save
)
982 (desktop-outvar 'desktop-saved-frameset
))
983 (mapc (function desktop-outvar
) desktop-globals-to-save
)
984 (setq desktop-saved-frameset nil
) ; after saving desktop-globals-to-save
985 (when (memq 'kill-ring desktop-globals-to-save
)
987 "(setq kill-ring-yank-pointer (nthcdr "
988 (int-to-string (- (length kill-ring
) (length kill-ring-yank-pointer
)))
991 (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n")
992 (dolist (l (mapcar 'desktop-buffer-info
(buffer-list)))
993 (let ((base (pop l
)))
994 (when (apply 'desktop-save-buffer-p l
)
996 (if (or (not (integerp eager
))
999 (setq eager
(1- eager
))))
1000 "desktop-create-buffer"
1001 "desktop-append-buffer-args")
1003 desktop-file-version
)
1004 ;; If there's a non-empty base name, we save it instead of the buffer name
1005 (when (and base
(not (string= base
"")))
1006 (setcar (nthcdr 1 l
) base
))
1008 (insert "\n " (desktop-value-to-string e
)))
1011 (setq default-directory desktop-dirname
)
1012 ;; If auto-saving, avoid writing if nothing has changed since the last write.
1013 ;; Don't check 300 characters of the header that contains the timestamp.
1014 (let ((checksum (and auto-save
(md5 (current-buffer)
1015 (+ (point-min) 300) (point-max)
1017 (unless (and auto-save
(equal checksum desktop-file-checksum
))
1018 (let ((coding-system-for-write 'emacs-mule
))
1019 (write-region (point-min) (point-max) (desktop-full-file-name) nil
'nomessage
))
1020 (setq desktop-file-checksum checksum
)
1021 ;; We remember when it was modified (which is presumably just now).
1022 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name)))))))))))
1024 ;; ----------------------------------------------------------------------------
1026 (defun desktop-remove ()
1027 "Delete desktop file in `desktop-dirname'.
1028 This function also sets `desktop-dirname' to nil."
1030 (when desktop-dirname
1031 (let ((filename (desktop-full-file-name)))
1032 (setq desktop-dirname nil
)
1033 (when (file-exists-p filename
)
1034 (delete-file filename
)))))
1036 (defvar desktop-buffer-args-list nil
1037 "List of args for `desktop-create-buffer'.")
1039 (defvar desktop-lazy-timer nil
)
1041 ;; ----------------------------------------------------------------------------
1042 (defun desktop-restoring-frameset-p ()
1043 "True if calling `desktop-restore-frameset' will actually restore it."
1044 (and desktop-restore-frames desktop-saved-frameset t
))
1046 (defun desktop-restore-frameset ()
1047 "Restore the state of a set of frames.
1048 This function depends on the value of `desktop-saved-frameset'
1049 being set (usually, by reading it from the desktop)."
1050 (when (desktop-restoring-frameset-p)
1051 (frameset-restore desktop-saved-frameset
1052 :filters desktop-filter-parameters-alist
1053 :reuse-frames desktop-restore-reuses-frames
1054 :force-display desktop-restore-in-current-display
1055 :force-onscreen desktop-restore-forces-onscreen
)))
1057 ;; Just to silence the byte compiler.
1058 ;; Dynamicaly bound in `desktop-read'.
1059 (defvar desktop-first-buffer
)
1060 (defvar desktop-buffer-ok-count
)
1061 (defvar desktop-buffer-fail-count
)
1064 (defun desktop-read (&optional dirname
)
1065 "Read and process the desktop file in directory DIRNAME.
1066 Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in
1067 directories listed in `desktop-path'. If a desktop file is found, it
1068 is processed and `desktop-after-read-hook' is run. If no desktop file
1069 is found, clear the desktop and run `desktop-no-desktop-file-hook'.
1070 This function is a no-op when Emacs is running in batch mode.
1071 It returns t if a desktop file was loaded, nil otherwise."
1073 (unless noninteractive
1074 (setq desktop-dirname
1075 (file-name-as-directory
1078 ;; If DIRNAME is specified, use it.
1079 (and (< 0 (length dirname
)) dirname
)
1080 ;; Otherwise search desktop file in desktop-path.
1081 (let ((dirs desktop-path
))
1084 (desktop-full-file-name (car dirs
)))))
1085 (setq dirs
(cdr dirs
)))
1086 (and dirs
(car dirs
)))
1087 ;; If not found and `desktop-path' is non-nil, use its first element.
1088 (and desktop-path
(car desktop-path
))
1089 ;; Default: .emacs.d.
1090 user-emacs-directory
))))
1091 (if (file-exists-p (desktop-full-file-name))
1092 ;; Desktop file found, but is it already in use?
1093 (let ((desktop-first-buffer nil
)
1094 (desktop-buffer-ok-count 0)
1095 (desktop-buffer-fail-count 0)
1096 (owner (desktop-owner))
1097 ;; Avoid desktop saving during evaluation of desktop buffer.
1100 (memq desktop-load-locked-desktop
'(nil ask
))
1101 (or (null desktop-load-locked-desktop
)
1103 (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\
1104 Using it may cause conflicts. Use it anyway? " owner
)))))
1105 (let ((default-directory desktop-dirname
))
1106 (setq desktop-dirname nil
)
1107 (run-hooks 'desktop-not-loaded-hook
)
1108 (unless desktop-dirname
1109 (message "Desktop file in use; not loaded.")))
1110 (desktop-lazy-abort)
1111 ;; Evaluate desktop buffer and remember when it was modified.
1112 (load (desktop-full-file-name) t t t
)
1113 (setq desktop-file-modtime
(nth 5 (file-attributes (desktop-full-file-name))))
1114 ;; If it wasn't already, mark it as in-use, to bother other
1115 ;; desktop instances.
1118 (desktop-claim-lock)
1119 (file-error (message "Couldn't record use of desktop file")
1122 (unless (desktop-restoring-frameset-p)
1123 ;; `desktop-create-buffer' puts buffers at end of the buffer list.
1124 ;; We want buffers existing prior to evaluating the desktop (and
1125 ;; not reused) to be placed at the end of the buffer list, so we
1128 (nreverse (cdr (memq desktop-first-buffer
(nreverse (buffer-list))))))
1129 (switch-to-buffer (car (buffer-list))))
1130 (run-hooks 'desktop-delay-hook
)
1131 (setq desktop-delay-hook nil
)
1132 (desktop-restore-frameset)
1133 (run-hooks 'desktop-after-read-hook
)
1134 (message "Desktop: %s%d buffer%s restored%s%s."
1135 (if desktop-saved-frameset
1136 (let ((fn (length (frameset-states desktop-saved-frameset
))))
1137 (format "%d frame%s, "
1138 fn
(if (= fn
1) "" "s")))
1140 desktop-buffer-ok-count
1141 (if (= 1 desktop-buffer-ok-count
) "" "s")
1142 (if (< 0 desktop-buffer-fail-count
)
1143 (format ", %d failed to restore" desktop-buffer-fail-count
)
1145 (if desktop-buffer-args-list
1146 (format ", %d to restore lazily"
1147 (length desktop-buffer-args-list
))
1149 (unless (desktop-restoring-frameset-p)
1150 ;; Bury the *Messages* buffer to not reshow it when burying
1151 ;; the buffer we switched to above.
1152 (when (buffer-live-p (get-buffer "*Messages*"))
1153 (bury-buffer "*Messages*"))
1154 ;; Clear all windows' previous and next buffers, these have
1155 ;; been corrupted by the `switch-to-buffer' calls in
1156 ;; `desktop-restore-file-buffer' (bug#11556). This is a
1157 ;; brute force fix and should be replaced by a more subtle
1158 ;; strategy eventually.
1159 (walk-window-tree (lambda (window)
1160 (set-window-prev-buffers window nil
)
1161 (set-window-next-buffers window nil
))))
1162 (setq desktop-saved-frameset nil
)
1164 ;; No desktop file found.
1166 (let ((default-directory desktop-dirname
))
1167 (run-hooks 'desktop-no-desktop-file-hook
))
1168 (message "No desktop file.")
1171 ;; ----------------------------------------------------------------------------
1172 ;; Maintained for backward compatibility
1174 (defun desktop-load-default ()
1175 "Load the `default' start-up library manually.
1176 Also inhibit further loading of it."
1177 (declare (obsolete desktop-save-mode
"22.1"))
1178 (unless inhibit-default-init
; safety check
1179 (load "default" t t
)
1180 (setq inhibit-default-init t
)))
1182 ;; ----------------------------------------------------------------------------
1184 (defun desktop-change-dir (dirname)
1185 "Change to desktop saved in DIRNAME.
1186 Kill the desktop as specified by variables `desktop-save-mode' and
1187 `desktop-save', then clear the desktop and load the desktop file in
1189 (interactive "DChange to directory: ")
1190 (setq dirname
(file-name-as-directory (expand-file-name dirname desktop-dirname
)))
1193 (desktop-read dirname
))
1195 ;; ----------------------------------------------------------------------------
1197 (defun desktop-save-in-desktop-dir ()
1198 "Save the desktop in directory `desktop-dirname'."
1201 (desktop-save desktop-dirname
)
1202 (call-interactively 'desktop-save
))
1203 (message "Desktop saved in %s" (abbreviate-file-name desktop-dirname
)))
1205 ;; ----------------------------------------------------------------------------
1207 (defvar desktop-auto-save-timer nil
)
1209 (defun desktop-auto-save ()
1210 "Save the desktop periodically.
1211 Called by the timer created in `desktop-auto-save-set-timer'."
1212 (when (and desktop-save-mode
1213 (integerp desktop-auto-save-timeout
)
1214 (> desktop-auto-save-timeout
0)
1215 ;; Avoid desktop saving during lazy loading.
1216 (not desktop-lazy-timer
)
1217 ;; Save only to own desktop file.
1218 (eq (emacs-pid) (desktop-owner))
1220 (desktop-save desktop-dirname nil t
))
1221 (desktop-auto-save-set-timer))
1223 (defun desktop-auto-save-set-timer ()
1224 "Reset the auto-save timer.
1225 Cancel any previous timer. When `desktop-auto-save-timeout' is a positive
1226 integer, start a new timer to call `desktop-auto-save' in that many seconds."
1227 (when desktop-auto-save-timer
1228 (cancel-timer desktop-auto-save-timer
)
1229 (setq desktop-auto-save-timer nil
))
1230 (when (and (integerp desktop-auto-save-timeout
)
1231 (> desktop-auto-save-timeout
0))
1232 (setq desktop-auto-save-timer
1233 (run-with-timer desktop-auto-save-timeout nil
1234 'desktop-auto-save
))))
1236 ;; ----------------------------------------------------------------------------
1238 (defun desktop-revert ()
1239 "Revert to the last loaded desktop."
1241 (unless desktop-dirname
1242 (error "Unknown desktop directory"))
1243 (unless (file-exists-p (desktop-full-file-name))
1244 (error "No desktop file found"))
1246 (desktop-read desktop-dirname
))
1248 (defvar desktop-buffer-major-mode
)
1249 (defvar desktop-buffer-locals
)
1250 (defvar auto-insert
) ; from autoinsert.el
1251 ;; ----------------------------------------------------------------------------
1252 (defun desktop-restore-file-buffer (buffer-filename
1255 "Restore a file buffer."
1256 (when buffer-filename
1257 (if (or (file-exists-p buffer-filename
)
1258 (let ((msg (format "Desktop: File \"%s\" no longer exists."
1260 (if desktop-missing-file-warning
1261 (y-or-n-p (concat msg
" Re-create buffer? "))
1264 (let* ((auto-insert nil
) ; Disable auto insertion
1265 (coding-system-for-read
1266 (or coding-system-for-read
1267 (cdr (assq 'buffer-file-coding-system
1268 desktop-buffer-locals
))))
1269 (buf (find-file-noselect buffer-filename
)))
1271 (switch-to-buffer buf
)
1272 (error (pop-to-buffer buf
)))
1273 (and (not (eq major-mode desktop-buffer-major-mode
))
1274 (functionp desktop-buffer-major-mode
)
1275 (funcall desktop-buffer-major-mode
))
1279 (defun desktop-load-file (function)
1280 "Load the file where auto loaded FUNCTION is defined."
1281 (when (fboundp function
)
1282 (autoload-do-load (symbol-function function
) function
)))
1284 ;; ----------------------------------------------------------------------------
1285 ;; Create a buffer, load its file, set its mode, ...;
1286 ;; called from Desktop file only.
1288 (defun desktop-create-buffer
1301 (let ((desktop-file-version file-version
)
1302 (desktop-buffer-file-name buffer-filename
)
1303 (desktop-buffer-name buffer-name
)
1304 (desktop-buffer-major-mode buffer-majormode
)
1305 (desktop-buffer-minor-modes buffer-minormodes
)
1306 (desktop-buffer-point buffer-point
)
1307 (desktop-buffer-mark buffer-mark
)
1308 (desktop-buffer-read-only buffer-readonly
)
1309 (desktop-buffer-misc buffer-misc
)
1310 (desktop-buffer-locals buffer-locals
))
1311 ;; To make desktop files with relative file names possible, we cannot
1312 ;; allow `default-directory' to change. Therefore we save current buffer.
1313 (save-current-buffer
1314 ;; Give major mode module a chance to add a handler.
1315 (desktop-load-file desktop-buffer-major-mode
)
1316 (let ((buffer-list (buffer-list))
1318 (condition-case-unless-debug err
1319 (funcall (or (cdr (assq desktop-buffer-major-mode
1320 desktop-buffer-mode-handlers
))
1321 'desktop-restore-file-buffer
)
1322 desktop-buffer-file-name
1324 desktop-buffer-misc
)
1326 (message "Desktop: Can't load buffer %s: %s"
1328 (error-message-string err
))
1329 (when desktop-missing-file-warning
(sit-for 1))
1331 (if (bufferp result
)
1332 (setq desktop-buffer-ok-count
(1+ desktop-buffer-ok-count
))
1333 (setq desktop-buffer-fail-count
(1+ desktop-buffer-fail-count
))
1335 ;; Restore buffer list order with new buffer at end. Don't change
1336 ;; the order for old desktop files (old desktop module behavior).
1337 (unless (< desktop-file-version
206)
1338 (mapc 'bury-buffer buffer-list
)
1339 (when result
(bury-buffer result
)))
1341 (unless (or desktop-first-buffer
(< desktop-file-version
206))
1342 (setq desktop-first-buffer result
))
1344 (unless (equal (buffer-name) desktop-buffer-name
)
1345 (rename-buffer desktop-buffer-name t
))
1347 (cond ((equal '(t) desktop-buffer-minor-modes
) ; backwards compatible
1349 ((equal '(nil) desktop-buffer-minor-modes
) ; backwards compatible
1352 (dolist (minor-mode desktop-buffer-minor-modes
)
1353 ;; Give minor mode module a chance to add a handler.
1354 (desktop-load-file minor-mode
)
1355 (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers
))))
1357 (funcall handler desktop-buffer-locals
)
1358 (when (functionp minor-mode
)
1359 (funcall minor-mode
1)))))))
1360 ;; Even though point and mark are non-nil when written by
1361 ;; `desktop-save', they may be modified by handlers wanting to set
1362 ;; point or mark themselves.
1363 (when desktop-buffer-point
1366 ;; Evaluate point. Thus point can be something like
1367 ;; '(search-forward ...
1368 (eval desktop-buffer-point
)
1369 (error (message "%s" (error-message-string err
)) 1))))
1370 (when desktop-buffer-mark
1371 (if (consp desktop-buffer-mark
)
1373 (set-mark (car desktop-buffer-mark
))
1374 (setq mark-active
(car (cdr desktop-buffer-mark
))))
1375 (set-mark desktop-buffer-mark
)))
1376 ;; Never override file system if the file really is read-only marked.
1377 (when desktop-buffer-read-only
(setq buffer-read-only desktop-buffer-read-only
))
1378 (dolist (this desktop-buffer-locals
)
1380 ;; an entry of this form `(symbol . value)'
1382 (make-local-variable (car this
))
1383 (set (car this
) (cdr this
)))
1384 ;; an entry of the form `symbol'
1385 (make-local-variable this
)
1386 (makunbound this
))))))))
1388 ;; ----------------------------------------------------------------------------
1389 ;; Backward compatibility -- update parameters to 205 standards.
1390 (defun desktop-buffer (buffer-filename buffer-name buffer-majormode
1391 mim pt mk ro tl fc cfs cr buffer-misc
)
1392 (desktop-create-buffer 205 buffer-filename buffer-name
1393 buffer-majormode
(cdr mim
) pt mk ro
1395 (list (cons 'truncate-lines tl
)
1396 (cons 'fill-column fc
)
1397 (cons 'case-fold-search cfs
)
1398 (cons 'case-replace cr
)
1399 (cons 'overwrite-mode
(car mim
)))))
1401 (defun desktop-append-buffer-args (&rest args
)
1402 "Append ARGS at end of `desktop-buffer-args-list'.
1403 ARGS must be an argument list for `desktop-create-buffer'."
1404 (setq desktop-buffer-args-list
(nconc desktop-buffer-args-list
(list args
)))
1405 (unless desktop-lazy-timer
1406 (setq desktop-lazy-timer
1407 (run-with-idle-timer desktop-lazy-idle-delay t
'desktop-idle-create-buffers
))))
1409 (defun desktop-lazy-create-buffer ()
1410 "Pop args from `desktop-buffer-args-list', create buffer and bury it."
1411 (when desktop-buffer-args-list
1412 (let* ((remaining (length desktop-buffer-args-list
))
1413 (args (pop desktop-buffer-args-list
))
1414 (buffer-name (nth 2 args
))
1415 (msg (format "Desktop lazily opening %s (%s remaining)..."
1416 buffer-name remaining
)))
1417 (when desktop-lazy-verbose
1419 (let ((desktop-first-buffer nil
)
1420 (desktop-buffer-ok-count 0)
1421 (desktop-buffer-fail-count 0))
1422 (apply 'desktop-create-buffer args
)
1423 (run-hooks 'desktop-delay-hook
)
1424 (setq desktop-delay-hook nil
)
1425 (bury-buffer (get-buffer buffer-name
))
1426 (when desktop-lazy-verbose
1427 (message "%s%s" msg
(if (> desktop-buffer-ok-count
0) "done" "failed")))))))
1429 (defun desktop-idle-create-buffers ()
1430 "Create buffers until the user does something, then stop.
1431 If there are no buffers left to create, kill the timer."
1433 (while (and repeat desktop-buffer-args-list
)
1434 (save-window-excursion
1435 (desktop-lazy-create-buffer))
1436 (setq repeat
(sit-for 0.2))
1437 (unless desktop-buffer-args-list
1438 (cancel-timer desktop-lazy-timer
)
1439 (setq desktop-lazy-timer nil
)
1440 (message "Lazy desktop load complete")
1444 (defun desktop-lazy-complete ()
1445 "Run the desktop load to completion."
1447 (let ((desktop-lazy-verbose t
))
1448 (while desktop-buffer-args-list
1449 (save-window-excursion
1450 (desktop-lazy-create-buffer)))
1451 (message "Lazy desktop load complete")))
1453 (defun desktop-lazy-abort ()
1454 "Abort lazy loading of the desktop."
1456 (when desktop-lazy-timer
1457 (cancel-timer desktop-lazy-timer
)
1458 (setq desktop-lazy-timer nil
))
1459 (when desktop-buffer-args-list
1460 (setq desktop-buffer-args-list nil
)
1461 (when (called-interactively-p 'interactive
)
1462 (message "Lazy desktop load aborted"))))
1464 ;; ----------------------------------------------------------------------------
1465 ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the
1466 ;; command line, we do the rest of what it takes to use desktop, but do it
1467 ;; after finishing loading the init file.
1468 ;; We cannot use `command-switch-alist' to process "--no-desktop" because these
1469 ;; functions are processed after `after-init-hook'.
1473 (let ((key "--no-desktop"))
1474 (when (member key command-line-args
)
1475 (setq command-line-args
(delete key command-line-args
))
1476 (setq desktop-save-mode nil
)))
1477 (when desktop-save-mode
1479 (desktop-auto-save-set-timer)
1480 (setq inhibit-startup-screen t
))))
1482 ;; So we can restore vc-dir buffers.
1483 (autoload 'vc-dir-mode
"vc-dir" nil t
)
1487 ;;; desktop.el ends here