Commit | Line | Data |
---|---|---|
6343ed61 RS |
1 | ;;; desktop.el --- save partial status of Emacs when killed |
2 | ||
0d30b337 | 3 | ;; Copyright (C) 1993, 1994, 1995, 1997, 2000, 2001, 2002, 2003, |
409cc4a3 | 4 | ;; 2004, 2005, 2006, 2007, 2008 Free Software Foundation, Inc. |
6343ed61 RS |
5 | |
6 | ;; Author: Morten Welinder <terra@diku.dk> | |
3ea96cac | 7 | ;; Keywords: convenience |
b6105c76 | 8 | ;; Favourite-brand-of-beer: None, I hate beer. |
6343ed61 RS |
9 | |
10 | ;; This file is part of GNU Emacs. | |
11 | ||
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 | |
b4aa6026 | 14 | ;; the Free Software Foundation; either version 3, or (at your option) |
6343ed61 RS |
15 | ;; any later version. |
16 | ||
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. | |
21 | ||
22 | ;; You should have received a copy of the GNU General Public License | |
b578f267 | 23 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
086add15 LK |
24 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
25 | ;; Boston, MA 02110-1301, USA. | |
6343ed61 RS |
26 | |
27 | ;;; Commentary: | |
28 | ||
0b9bd504 RS |
29 | ;; Save the Desktop, i.e., |
30 | ;; - some global variables | |
31 | ;; - the list of buffers with associated files. For each buffer also | |
32 | ;; - the major mode | |
33 | ;; - the default directory | |
34 | ;; - the point | |
35 | ;; - the mark & mark-active | |
36 | ;; - buffer-read-only | |
ec4c6f22 | 37 | ;; - some local variables |
6343ed61 | 38 | |
c5b31c4d LH |
39 | ;; To use this, use customize to turn on desktop-save-mode or add the |
40 | ;; following line somewhere in your .emacs file: | |
0b9bd504 | 41 | ;; |
c5b31c4d | 42 | ;; (desktop-save-mode 1) |
0b9bd504 | 43 | ;; |
c5b31c4d LH |
44 | ;; For further usage information, look at the section |
45 | ;; "Saving Emacs Sessions" in the GNU Emacs Manual. | |
46 | ||
47 | ;; When the desktop module is loaded, the function `desktop-kill' is | |
341c2f07 | 48 | ;; added to the `kill-emacs-hook'. This function is responsible for |
c5b31c4d | 49 | ;; saving the desktop when Emacs is killed. Furthermore an anonymous |
341c2f07 | 50 | ;; function is added to the `after-init-hook'. This function is |
c5b31c4d | 51 | ;; responsible for loading the desktop when Emacs is started. |
ec4c6f22 | 52 | |
0f4804e7 LH |
53 | ;; Special handling. |
54 | ;; ----------------- | |
55 | ;; Variables `desktop-buffer-mode-handlers' and `desktop-minor-mode-handlers' | |
56 | ;; are supplied to handle special major and minor modes respectively. | |
57 | ;; `desktop-buffer-mode-handlers' is an alist of major mode specific functions | |
341c2f07 | 58 | ;; to restore a desktop buffer. Elements must have the form |
be617bbf | 59 | ;; |
0f4804e7 | 60 | ;; (MAJOR-MODE . RESTORE-BUFFER-FUNCTION). |
be617bbf | 61 | ;; |
0f4804e7 | 62 | ;; Functions listed are called by `desktop-create-buffer' when `desktop-read' |
341c2f07 | 63 | ;; evaluates the desktop file. Buffers with a major mode not specified here, |
0f4804e7 LH |
64 | ;; are restored by the default handler `desktop-restore-file-buffer'. |
65 | ;; `desktop-minor-mode-handlers' is an alist of functions to restore | |
66 | ;; non-standard minor modes. Elements must have the form | |
be617bbf | 67 | ;; |
0f4804e7 | 68 | ;; (MINOR-MODE . RESTORE-FUNCTION). |
be617bbf | 69 | ;; |
0f4804e7 LH |
70 | ;; Functions are called by `desktop-create-buffer' to restore minor modes. |
71 | ;; Minor modes not specified here, are restored by the standard minor mode | |
72 | ;; function. If you write a module that defines a major or minor mode that | |
73 | ;; needs a special handler, then place code like | |
74 | ||
75 | ;; (defun foo-restore-desktop-buffer | |
76 | ;; ... | |
77 | ;; (add-to-list 'desktop-buffer-mode-handlers | |
78 | ;; '(foo-mode . foo-restore-desktop-buffer)) | |
79 | ||
80 | ;; or | |
81 | ||
82 | ;; (defun bar-desktop-restore | |
83 | ;; ... | |
84 | ;; (add-to-list 'desktop-minor-mode-handlers | |
85 | ;; '(bar-mode . bar-desktop-restore)) | |
86 | ||
87 | ;; in the module itself, and make shure that the mode function is | |
341c2f07 | 88 | ;; autoloaded. See the docstrings of `desktop-buffer-mode-handlers' and |
0f4804e7 LH |
89 | ;; `desktop-minor-mode-handlers' for more info. |
90 | ||
91 | ;; Minor modes. | |
92 | ;; ------------ | |
93 | ;; Conventional minor modes (see node "Minor Mode Conventions" in the elisp | |
94 | ;; manual) are handled in the following way: | |
95 | ;; When `desktop-save' saves the state of a buffer to the desktop file, it | |
96 | ;; saves as `desktop-minor-modes' the list of names of those variables in | |
97 | ;; `minor-mode-alist' that have a non-nil value. | |
98 | ;; When `desktop-create' restores the buffer, each of the symbols in | |
99 | ;; `desktop-minor-modes' is called as function with parameter 1. | |
100 | ;; The variables `desktop-minor-mode-table' and `desktop-minor-mode-handlers' | |
101 | ;; are used to handle non-conventional minor modes. `desktop-save' uses | |
102 | ;; `desktop-minor-mode-table' to map minor mode variables to minor mode | |
341c2f07 | 103 | ;; functions before writing `desktop-minor-modes'. If a minor mode has a |
0f4804e7 LH |
104 | ;; variable name that is different form its function name, an entry |
105 | ||
106 | ;; (NAME RESTORE-FUNCTION) | |
107 | ||
108 | ;; should be added to `desktop-minor-mode-table'. If a minor mode should not | |
109 | ;; be restored, RESTORE-FUNCTION should be set to nil. `desktop-create' uses | |
110 | ;; `desktop-minor-mode-handlers' to lookup minor modes that needs a restore | |
111 | ;; function different from the usual minor mode function. | |
112 | ;; --------------------------------------------------------------------------- | |
47640244 | 113 | |
ec4c6f22 RS |
114 | ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs |
115 | ;; in your home directory is used for that. Saving global default values | |
116 | ;; for buffers is an example of misuse. | |
117 | ||
0b9bd504 RS |
118 | ;; PLEASE NOTE: The kill ring can be saved as specified by the variable |
119 | ;; `desktop-globals-to-save' (by default it isn't). This may result in saving | |
120 | ;; things you did not mean to keep. Use M-x desktop-clear RET. | |
ec4c6f22 | 121 | |
fa379636 KH |
122 | ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas. |
123 | ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip. | |
124 | ;; chris@tecc.co.uk (Chris Boucher) for a mark tip. | |
125 | ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip. | |
126 | ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt. | |
f4c73d07 | 127 | ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips. |
253406b9 | 128 | ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips. |
0b9bd504 RS |
129 | ;; --------------------------------------------------------------------------- |
130 | ;; TODO: | |
131 | ;; | |
132 | ;; Save window configuration. | |
133 | ;; Recognize more minor modes. | |
134 | ;; Save mark rings. | |
6343ed61 RS |
135 | |
136 | ;;; Code: | |
137 | ||
4a2fce7a | 138 | (defvar desktop-file-version "206" |
05f1c4ec | 139 | "Version number of desktop file format. |
4a2fce7a RS |
140 | Written into the desktop file and used at desktop read to provide |
141 | backward compatibility.") | |
142 | ||
ec4c6f22 | 143 | ;; ---------------------------------------------------------------------------- |
0b9bd504 RS |
144 | ;; USER OPTIONS -- settings you might want to play with. |
145 | ;; ---------------------------------------------------------------------------- | |
bbf5eb28 RS |
146 | |
147 | (defgroup desktop nil | |
148 | "Save status of Emacs when you exit." | |
149 | :group 'frames) | |
150 | ||
c5b31c4d LH |
151 | ;;;###autoload |
152 | (define-minor-mode desktop-save-mode | |
153 | "Toggle desktop saving mode. | |
154 | With numeric ARG, turn desktop saving on if ARG is positive, off | |
11425834 LH |
155 | otherwise. If desktop saving is turned on, the state of Emacs is |
156 | saved from one session to another. See variable `desktop-save' | |
157 | and function `desktop-read' for details." | |
c5b31c4d LH |
158 | :global t |
159 | :group 'desktop) | |
160 | ||
161 | ;; Maintained for backward compatibility | |
b89c5a72 JB |
162 | (define-obsolete-variable-alias 'desktop-enable |
163 | 'desktop-save-mode "22.1") | |
813dbb2d | 164 | |
3ae6b03f JB |
165 | (defun desktop-save-mode-off () |
166 | "Disable `desktop-save-mode'. Provided for use in hooks." | |
167 | (desktop-save-mode 0)) | |
168 | ||
4a2fce7a | 169 | (defcustom desktop-save 'ask-if-new |
c5b31c4d LH |
170 | "*Specifies whether the desktop should be saved when it is killed. |
171 | A desktop is killed when the user changes desktop or quits Emacs. | |
172 | Possible values are: | |
4a2fce7a RS |
173 | t -- always save. |
174 | ask -- always ask. | |
175 | ask-if-new -- ask if no desktop file exists, otherwise just save. | |
176 | ask-if-exists -- ask if desktop file exists, otherwise don't save. | |
177 | if-exists -- save if desktop file exists, otherwise don't save. | |
178 | nil -- never save. | |
c5b31c4d | 179 | The desktop is never saved when `desktop-save-mode' is nil. |
cc8b76bf | 180 | The variables `desktop-dirname' and `desktop-base-file-name' |
c5b31c4d | 181 | determine where the desktop is saved." |
11425834 LH |
182 | :type |
183 | '(choice | |
4a2fce7a RS |
184 | (const :tag "Always save" t) |
185 | (const :tag "Always ask" ask) | |
186 | (const :tag "Ask if desktop file is new, else do save" ask-if-new) | |
187 | (const :tag "Ask if desktop file exists, else don't save" ask-if-exists) | |
188 | (const :tag "Save if desktop file exists, else don't" if-exists) | |
189 | (const :tag "Never save" nil)) | |
af61551b | 190 | :group 'desktop |
bf247b6e | 191 | :version "22.1") |
4a2fce7a | 192 | |
3ae6b03f JB |
193 | (defcustom desktop-load-locked-desktop 'ask |
194 | "Specifies whether the desktop should be loaded if locked. | |
195 | Possible values are: | |
196 | t -- load anyway. | |
197 | nil -- don't load. | |
198 | ask -- ask the user. | |
199 | If the value is nil, or `ask' and the user chooses not to load the desktop, | |
200 | the normal hook `desktop-not-loaded-hook' is run." | |
201 | :type | |
202 | '(choice | |
203 | (const :tag "Load anyway" t) | |
204 | (const :tag "Don't load" nil) | |
205 | (const :tag "Ask the user" ask)) | |
206 | :group 'desktop | |
207 | :version "22.2") | |
208 | ||
4a2fce7a | 209 | (defcustom desktop-base-file-name |
fc715501 | 210 | (convert-standard-filename ".emacs.desktop") |
c5b31c4d | 211 | "Name of file for Emacs desktop, excluding the directory part." |
813dbb2d RS |
212 | :type 'file |
213 | :group 'desktop) | |
b89c5a72 JB |
214 | (define-obsolete-variable-alias 'desktop-basefilename |
215 | 'desktop-base-file-name "22.1") | |
6343ed61 | 216 | |
3ae6b03f JB |
217 | (defcustom desktop-base-lock-name |
218 | (convert-standard-filename ".emacs.desktop.lock") | |
219 | "Name of lock file for Emacs desktop, excluding the directory part." | |
220 | :type 'file | |
221 | :group 'desktop | |
222 | :version "22.2") | |
223 | ||
4a2fce7a RS |
224 | (defcustom desktop-path '("." "~") |
225 | "List of directories to search for the desktop file. | |
226 | The base name of the file is specified in `desktop-base-file-name'." | |
227 | :type '(repeat directory) | |
af61551b | 228 | :group 'desktop |
bf247b6e | 229 | :version "22.1") |
4a2fce7a | 230 | |
bbf5eb28 | 231 | (defcustom desktop-missing-file-warning nil |
6c27fdb9 | 232 | "If non-nil, offer to recreate the buffer of a deleted file. |
ebb39555 LH |
233 | Also pause for a moment to display message about errors signaled in |
234 | `desktop-buffer-mode-handlers'. | |
235 | ||
236 | If nil, just print error messages in the message buffer." | |
bbf5eb28 | 237 | :type 'boolean |
af61551b | 238 | :group 'desktop |
bf247b6e | 239 | :version "22.1") |
6343ed61 | 240 | |
4a2fce7a | 241 | (defcustom desktop-no-desktop-file-hook nil |
c5b31c4d | 242 | "Normal hook run when `desktop-read' can't find a desktop file. |
11425834 | 243 | Run in the directory in which the desktop file was sought. |
8649a87b | 244 | May be used to show a dired buffer." |
4a2fce7a | 245 | :type 'hook |
af61551b | 246 | :group 'desktop |
bf247b6e | 247 | :version "22.1") |
4a2fce7a | 248 | |
3ae6b03f JB |
249 | (defcustom desktop-not-loaded-hook nil |
250 | "Normal hook run when the user declines to re-use a desktop file. | |
251 | Run in the directory in which the desktop file was found. | |
252 | May be used to deal with accidental multiple Emacs jobs." | |
253 | :type 'hook | |
254 | :group 'desktop | |
255 | :options '(desktop-save-mode-off save-buffers-kill-emacs) | |
256 | :version "22.2") | |
257 | ||
4a2fce7a | 258 | (defcustom desktop-after-read-hook nil |
05f1c4ec | 259 | "Normal hook run after a successful `desktop-read'. |
8649a87b | 260 | May be used to show a buffer list." |
4a2fce7a | 261 | :type 'hook |
af61551b | 262 | :group 'desktop |
11425834 | 263 | :options '(list-buffers) |
bf247b6e | 264 | :version "22.1") |
4a2fce7a RS |
265 | |
266 | (defcustom desktop-save-hook nil | |
c5b31c4d | 267 | "Normal hook run before the desktop is saved in a desktop file. |
11425834 LH |
268 | Run with the desktop buffer current with only the header present. |
269 | May be used to add to the desktop code or to truncate history lists, | |
270 | for example." | |
4a2fce7a RS |
271 | :type 'hook |
272 | :group 'desktop) | |
273 | ||
340db502 LH |
274 | (defcustom desktop-globals-to-save |
275 | '(desktop-missing-file-warning | |
276 | tags-file-name | |
277 | tags-table-list | |
278 | search-ring | |
279 | regexp-search-ring | |
280 | register-alist) | |
c5b31c4d LH |
281 | "List of global variables saved by `desktop-save'. |
282 | An element may be variable name (a symbol) or a cons cell of the form | |
283 | \(VAR . MAX-SIZE), which means to truncate VAR's value to at most | |
284 | MAX-SIZE elements (if the value is a list) before saving the value. | |
4a2fce7a RS |
285 | Feature: Saving `kill-ring' implies saving `kill-ring-yank-pointer'." |
286 | :type '(repeat (restricted-sexp :match-alternatives (symbolp consp))) | |
287 | :group 'desktop) | |
288 | ||
340db502 LH |
289 | (defcustom desktop-globals-to-clear |
290 | '(kill-ring | |
291 | kill-ring-yank-pointer | |
292 | search-ring | |
293 | search-ring-yank-pointer | |
294 | regexp-search-ring | |
295 | regexp-search-ring-yank-pointer) | |
150f39ee | 296 | "List of global variables that `desktop-clear' will clear. |
4a2fce7a | 297 | An element may be variable name (a symbol) or a cons cell of the form |
cc8b76bf JB |
298 | \(VAR . FORM). Symbols are set to nil and for cons cells VAR is set |
299 | to the value obtained by evaluating FORM." | |
4a2fce7a | 300 | :type '(repeat (restricted-sexp :match-alternatives (symbolp consp))) |
af61551b | 301 | :group 'desktop |
bf247b6e | 302 | :version "22.1") |
4a2fce7a | 303 | |
0f4804e7 LH |
304 | (defcustom desktop-clear-preserve-buffers |
305 | '("\\*scratch\\*" "\\*Messages\\*" "\\*server\\*" "\\*tramp/.+\\*") | |
306 | "*List of buffers that `desktop-clear' should not delete. | |
307 | Each element is a regular expression. Buffers with a name matched by any of | |
308 | these won't be deleted." | |
4a2fce7a RS |
309 | :type '(repeat string) |
310 | :group 'desktop) | |
340db502 | 311 | |
0f4804e7 | 312 | ;;;###autoload |
340db502 LH |
313 | (defcustom desktop-locals-to-save |
314 | '(desktop-locals-to-save ; Itself! Think it over. | |
315 | truncate-lines | |
316 | case-fold-search | |
317 | case-replace | |
318 | fill-column | |
319 | overwrite-mode | |
320 | change-log-default-name | |
321 | line-number-mode | |
0f4804e7 LH |
322 | column-number-mode |
323 | size-indication-mode | |
324 | buffer-file-coding-system | |
325 | indent-tabs-mode | |
11425834 | 326 | tab-width |
0f4804e7 LH |
327 | indicate-buffer-boundaries |
328 | indicate-empty-lines | |
329 | show-trailing-whitespace) | |
577ed2b2 | 330 | "List of local variables to save for each buffer. |
0f4804e7 LH |
331 | The variables are saved only when they really are local. Conventional minor |
332 | modes are restored automatically; they should not be listed here." | |
c5b31c4d LH |
333 | :type '(repeat symbol) |
334 | :group 'desktop) | |
ec4c6f22 | 335 | |
0b9bd504 | 336 | ;; We skip .log files because they are normally temporary. |
a7acbbe4 | 337 | ;; (ftp) files because they require passwords and whatnot. |
bbf5eb28 | 338 | (defcustom desktop-buffers-not-to-save |
df410295 | 339 | "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\)$" |
4a2fce7a RS |
340 | "Regexp identifying buffers that are to be excluded from saving." |
341 | :type 'regexp | |
342 | :group 'desktop) | |
6343ed61 | 343 | |
c5b31c4d | 344 | ;; Skip tramp and ange-ftp files |
bbf5eb28 | 345 | (defcustom desktop-files-not-to-save |
fa379636 | 346 | "^/[^/:]*:" |
bbf5eb28 RS |
347 | "Regexp identifying files whose buffers are to be excluded from saving." |
348 | :type 'regexp | |
349 | :group 'desktop) | |
fa379636 | 350 | |
df410295 JL |
351 | ;; We skip TAGS files to save time (tags-file-name is saved instead). |
352 | (defcustom desktop-modes-not-to-save | |
353 | '(tags-table-mode) | |
80f9f3db SM |
354 | "List of major modes whose buffers should not be saved." |
355 | :type '(repeat symbol) | |
356 | :group 'desktop) | |
357 | ||
4a2fce7a RS |
358 | (defcustom desktop-file-name-format 'absolute |
359 | "*Format in which desktop file names should be saved. | |
360 | Possible values are: | |
361 | absolute -- Absolute file name. | |
362 | tilde -- Relative to ~. | |
363 | local -- Relative to directory of desktop file." | |
364 | :type '(choice (const absolute) (const tilde) (const local)) | |
af61551b | 365 | :group 'desktop |
bf247b6e | 366 | :version "22.1") |
569c754e | 367 | |
150f39ee LH |
368 | (defcustom desktop-restore-eager t |
369 | "Number of buffers to restore immediately. | |
370 | Remaining buffers are restored lazily (when Emacs is idle). | |
371 | If value is t, all buffers are restored immediately." | |
0ba9bc53 | 372 | :type '(choice (const t) integer) |
150f39ee | 373 | :group 'desktop |
bf247b6e | 374 | :version "22.1") |
150f39ee LH |
375 | |
376 | (defcustom desktop-lazy-verbose t | |
377 | "Verbose reporting of lazily created buffers." | |
378 | :type 'boolean | |
379 | :group 'desktop | |
bf247b6e | 380 | :version "22.1") |
150f39ee LH |
381 | |
382 | (defcustom desktop-lazy-idle-delay 5 | |
383 | "Idle delay before starting to create buffers. | |
384 | See `desktop-restore-eager'." | |
385 | :type 'integer | |
386 | :group 'desktop | |
bf247b6e | 387 | :version "22.1") |
150f39ee | 388 | |
e5780ae1 | 389 | ;;;###autoload |
ebb39555 LH |
390 | (defvar desktop-save-buffer nil |
391 | "When non-nil, save buffer status in desktop file. | |
e5780ae1 | 392 | This variable becomes buffer local when set. |
ebb39555 | 393 | |
b89c5a72 JB |
394 | If the value is a function, it is called by `desktop-save' with argument |
395 | DESKTOP-DIRNAME to obtain auxiliary information to save in the desktop | |
ebb39555 | 396 | file along with the state of the buffer for which it was called. |
b6b70cda | 397 | |
c5b31c4d | 398 | When file names are returned, they should be formatted using the call |
e5780ae1 | 399 | \"(desktop-file-name FILE-NAME DESKTOP-DIRNAME)\". |
4a2fce7a | 400 | |
0f4804e7 LH |
401 | Later, when `desktop-read' evaluates the desktop file, auxiliary information |
402 | is passed as the argument DESKTOP-BUFFER-MISC to functions in | |
403 | `desktop-buffer-mode-handlers'.") | |
ebb39555 LH |
404 | (make-variable-buffer-local 'desktop-save-buffer) |
405 | (make-obsolete-variable 'desktop-buffer-modes-to-save | |
cc8b76bf | 406 | 'desktop-save-buffer "22.1") |
e5780ae1 | 407 | (make-obsolete-variable 'desktop-buffer-misc-functions |
cc8b76bf | 408 | 'desktop-save-buffer "22.1") |
b6b70cda | 409 | |
0f4804e7 LH |
410 | ;;;###autoload |
411 | (defvar desktop-buffer-mode-handlers | |
412 | nil | |
e5780ae1 | 413 | "Alist of major mode specific functions to restore a desktop buffer. |
0f4804e7 LH |
414 | Functions listed are called by `desktop-create-buffer' when `desktop-read' |
415 | evaluates the desktop file. List elements must have the form | |
416 | ||
417 | (MAJOR-MODE . RESTORE-BUFFER-FUNCTION). | |
e5780ae1 LH |
418 | |
419 | Buffers with a major mode not specified here, are restored by the default | |
420 | handler `desktop-restore-file-buffer'. | |
421 | ||
55775448 | 422 | Handlers are called with argument list |
4a2fce7a | 423 | |
9bcabb45 | 424 | (DESKTOP-BUFFER-FILE-NAME DESKTOP-BUFFER-NAME DESKTOP-BUFFER-MISC) |
e5780ae1 LH |
425 | |
426 | Furthermore, they may use the following variables: | |
427 | ||
428 | desktop-file-version | |
4a2fce7a RS |
429 | desktop-buffer-major-mode |
430 | desktop-buffer-minor-modes | |
431 | desktop-buffer-point | |
432 | desktop-buffer-mark | |
433 | desktop-buffer-read-only | |
4a2fce7a RS |
434 | desktop-buffer-locals |
435 | ||
e5780ae1 | 436 | If a handler returns a buffer, then the saved mode settings |
0f4804e7 LH |
437 | and variable values for that buffer are copied into it. |
438 | ||
439 | Modules that define a major mode that needs a special handler should contain | |
440 | code like | |
441 | ||
442 | (defun foo-restore-desktop-buffer | |
443 | ... | |
444 | (add-to-list 'desktop-buffer-mode-handlers | |
445 | '(foo-mode . foo-restore-desktop-buffer)) | |
446 | ||
447 | Furthermore the major mode function must be autoloaded.") | |
ec4c6f22 | 448 | |
498eb267 | 449 | ;;;###autoload |
e5780ae1 LH |
450 | (put 'desktop-buffer-mode-handlers 'risky-local-variable t) |
451 | (make-obsolete-variable 'desktop-buffer-handlers | |
cc8b76bf | 452 | 'desktop-buffer-mode-handlers "22.1") |
b6b70cda | 453 | |
47640244 GM |
454 | (defcustom desktop-minor-mode-table |
455 | '((auto-fill-function auto-fill-mode) | |
5fff0265 JL |
456 | (vc-mode nil) |
457 | (vc-dired-mode nil)) | |
47640244 GM |
458 | "Table mapping minor mode variables to minor mode functions. |
459 | Each entry has the form (NAME RESTORE-FUNCTION). | |
460 | NAME is the name of the buffer-local variable indicating that the minor | |
461 | mode is active. RESTORE-FUNCTION is the function to activate the minor mode. | |
b258555d | 462 | RESTORE-FUNCTION nil means don't try to restore the minor mode. |
47640244 | 463 | Only minor modes for which the name of the buffer-local variable |
7bfa55b3 | 464 | and the name of the minor mode function are different have to be added to |
0f4804e7 | 465 | this table. See also `desktop-minor-mode-handlers'." |
47640244 GM |
466 | :type 'sexp |
467 | :group 'desktop) | |
468 | ||
0f4804e7 LH |
469 | ;;;###autoload |
470 | (defvar desktop-minor-mode-handlers | |
471 | nil | |
472 | "Alist of functions to restore non-standard minor modes. | |
473 | Functions are called by `desktop-create-buffer' to restore minor modes. | |
474 | List elements must have the form | |
475 | ||
476 | (MINOR-MODE . RESTORE-FUNCTION). | |
477 | ||
478 | Minor modes not specified here, are restored by the standard minor mode | |
479 | function. | |
480 | ||
481 | Handlers are called with argument list | |
482 | ||
483 | (DESKTOP-BUFFER-LOCALS) | |
484 | ||
485 | Furthermore, they may use the following variables: | |
486 | ||
487 | desktop-file-version | |
488 | desktop-buffer-file-name | |
489 | desktop-buffer-name | |
490 | desktop-buffer-major-mode | |
491 | desktop-buffer-minor-modes | |
492 | desktop-buffer-point | |
493 | desktop-buffer-mark | |
494 | desktop-buffer-read-only | |
495 | desktop-buffer-misc | |
496 | ||
497 | When a handler is called, the buffer has been created and the major mode has | |
498 | been set, but local variables listed in desktop-buffer-locals has not yet been | |
499 | created and set. | |
500 | ||
501 | Modules that define a minor mode that needs a special handler should contain | |
502 | code like | |
503 | ||
504 | (defun foo-desktop-restore | |
505 | ... | |
506 | (add-to-list 'desktop-minor-mode-handlers | |
507 | '(foo-mode . foo-desktop-restore)) | |
508 | ||
509 | Furthermore the minor mode function must be autoloaded. | |
510 | ||
511 | See also `desktop-minor-mode-table'.") | |
512 | ||
498eb267 | 513 | ;;;###autoload |
0f4804e7 LH |
514 | (put 'desktop-minor-mode-handlers 'risky-local-variable t) |
515 | ||
0b9bd504 RS |
516 | ;; ---------------------------------------------------------------------------- |
517 | (defvar desktop-dirname nil | |
c5b31c4d | 518 | "The directory in which the desktop file should be saved.") |
6343ed61 | 519 | |
11425834 LH |
520 | (defun desktop-full-file-name (&optional dirname) |
521 | "Return the full name of the desktop file in DIRNAME. | |
522 | DIRNAME omitted or nil means use `desktop-dirname'." | |
523 | (expand-file-name desktop-base-file-name (or dirname desktop-dirname))) | |
524 | ||
3ae6b03f JB |
525 | (defun desktop-full-lock-name (&optional dirname) |
526 | "Return the full name of the desktop lock file in DIRNAME. | |
527 | DIRNAME omitted or nil means use `desktop-dirname'." | |
528 | (expand-file-name desktop-base-lock-name (or dirname desktop-dirname))) | |
529 | ||
6343ed61 | 530 | (defconst desktop-header |
0b9bd504 RS |
531 | ";; -------------------------------------------------------------------------- |
532 | ;; Desktop File for Emacs | |
533 | ;; -------------------------------------------------------------------------- | |
6343ed61 | 534 | " "*Header to place in Desktop file.") |
de9e2828 RS |
535 | |
536 | (defvar desktop-delay-hook nil | |
537 | "Hooks run after all buffers are loaded; intended for internal use.") | |
813dbb2d | 538 | |
3ae6b03f JB |
539 | ;; ---------------------------------------------------------------------------- |
540 | ;; Desktop file conflict detection | |
541 | (defvar desktop-file-modtime nil | |
542 | "When the desktop file was last modified to the knowledge of this Emacs. | |
543 | Used to detect desktop file conflicts.") | |
544 | ||
545 | (defun desktop-owner (&optional dirname) | |
546 | "Return the PID of the Emacs process that owns the desktop file in DIRNAME. | |
547 | Return nil if no desktop file found or no Emacs process is using it. | |
548 | DIRNAME omitted or nil means use `desktop-dirname'." | |
549 | (let (owner) | |
550 | (and (file-exists-p (desktop-full-lock-name dirname)) | |
551 | (condition-case nil | |
552 | (with-temp-buffer | |
553 | (insert-file-contents-literally (desktop-full-lock-name dirname)) | |
554 | (goto-char (point-min)) | |
555 | (setq owner (read (current-buffer))) | |
556 | (integerp owner)) | |
557 | (error nil)) | |
558 | owner))) | |
559 | ||
560 | (defun desktop-claim-lock (&optional dirname) | |
561 | "Record this Emacs process as the owner of the desktop file in DIRNAME. | |
562 | DIRNAME omitted or nil means use `desktop-dirname'." | |
563 | (write-region (number-to-string (emacs-pid)) nil | |
564 | (desktop-full-lock-name dirname))) | |
565 | ||
566 | (defun desktop-release-lock (&optional dirname) | |
567 | "Remove the lock file for the desktop in DIRNAME. | |
568 | DIRNAME omitted or nil means use `desktop-dirname'." | |
569 | (let ((file (desktop-full-lock-name dirname))) | |
570 | (when (file-exists-p file) (delete-file file)))) | |
571 | ||
0b9bd504 | 572 | ;; ---------------------------------------------------------------------------- |
05f1c4ec | 573 | (defun desktop-truncate (list n) |
ec4c6f22 | 574 | "Truncate LIST to at most N elements destructively." |
05f1c4ec | 575 | (let ((here (nthcdr (1- n) list))) |
3ae6b03f JB |
576 | (when (consp here) |
577 | (setcdr here nil)))) | |
f9be4574 | 578 | |
4a2fce7a | 579 | ;; ---------------------------------------------------------------------------- |
11425834 | 580 | ;;;###autoload |
f9be4574 RS |
581 | (defun desktop-clear () |
582 | "Empty the Desktop. | |
0f4804e7 LH |
583 | This kills all buffers except for internal ones and those with names matched by |
584 | a regular expression in the list `desktop-clear-preserve-buffers'. | |
585 | Furthermore, it clears the variables listed in `desktop-globals-to-clear'." | |
6343ed61 | 586 | (interactive) |
150f39ee | 587 | (desktop-lazy-abort) |
4a2fce7a RS |
588 | (dolist (var desktop-globals-to-clear) |
589 | (if (symbolp var) | |
3ae6b03f | 590 | (eval `(setq-default ,var nil)) |
4a2fce7a | 591 | (eval `(setq-default ,(car var) ,(cdr var))))) |
0f4804e7 LH |
592 | (let ((buffers (buffer-list)) |
593 | (preserve-regexp (concat "^\\(" | |
594 | (mapconcat (lambda (regexp) | |
595 | (concat "\\(" regexp "\\)")) | |
596 | desktop-clear-preserve-buffers | |
597 | "\\|") | |
598 | "\\)$"))) | |
f9be4574 | 599 | (while buffers |
4a2fce7a RS |
600 | (let ((bufname (buffer-name (car buffers)))) |
601 | (or | |
602 | (null bufname) | |
0f4804e7 | 603 | (string-match preserve-regexp bufname) |
4a2fce7a | 604 | ;; Don't kill buffers made for internal purposes. |
3f32d6a3 | 605 | (and (not (equal bufname "")) (eq (aref bufname 0) ?\s)) |
4a2fce7a | 606 | (kill-buffer (car buffers)))) |
f9be4574 | 607 | (setq buffers (cdr buffers)))) |
b6105c76 | 608 | (delete-other-windows)) |
4a2fce7a | 609 | |
0b9bd504 | 610 | ;; ---------------------------------------------------------------------------- |
de9e2828 | 611 | (add-hook 'kill-emacs-hook 'desktop-kill) |
ec4c6f22 | 612 | |
6343ed61 | 613 | (defun desktop-kill () |
c5b31c4d | 614 | "If `desktop-save-mode' is non-nil, do what `desktop-save' says to do. |
4a2fce7a RS |
615 | If the desktop should be saved and `desktop-dirname' |
616 | is nil, ask the user where to save the desktop." | |
11425834 LH |
617 | (when (and desktop-save-mode |
618 | (let ((exists (file-exists-p (desktop-full-file-name)))) | |
619 | (or (eq desktop-save t) | |
620 | (and exists (memq desktop-save '(ask-if-new if-exists))) | |
621 | (and | |
622 | (or (memq desktop-save '(ask ask-if-new)) | |
623 | (and exists (eq desktop-save 'ask-if-exists))) | |
624 | (y-or-n-p "Save desktop? "))))) | |
4a2fce7a RS |
625 | (unless desktop-dirname |
626 | (setq desktop-dirname | |
11425834 LH |
627 | (file-name-as-directory |
628 | (expand-file-name | |
3ae6b03f | 629 | (read-directory-name "Directory for desktop file: " nil nil t))))) |
4a2fce7a | 630 | (condition-case err |
3ae6b03f | 631 | (desktop-save desktop-dirname t) |
4a2fce7a | 632 | (file-error |
11425834 | 633 | (unless (yes-or-no-p "Error while saving the desktop. Ignore? ") |
3ae6b03f JB |
634 | (signal (car err) (cdr err)))))) |
635 | ;; If we own it, we don't anymore. | |
636 | (when (eq (emacs-pid) (desktop-owner)) (desktop-release-lock))) | |
4a2fce7a | 637 | |
0b9bd504 | 638 | ;; ---------------------------------------------------------------------------- |
ed2f7fc8 RS |
639 | (defun desktop-list* (&rest args) |
640 | (if (null (cdr args)) | |
641 | (car args) | |
642 | (setq args (nreverse args)) | |
643 | (let ((value (cons (nth 1 args) (car args)))) | |
644 | (setq args (cdr (cdr args))) | |
645 | (while args | |
646 | (setq value (cons (car args) value)) | |
647 | (setq args (cdr args))) | |
648 | value))) | |
649 | ||
3ae6b03f JB |
650 | ;; ---------------------------------------------------------------------------- |
651 | (defun desktop-buffer-info (buffer) | |
652 | (set-buffer buffer) | |
653 | (list | |
97872686 JB |
654 | ;; base name of the buffer; replaces the buffer name if managed by uniquify |
655 | (and (fboundp 'uniquify-buffer-base-name) (uniquify-buffer-base-name)) | |
3ae6b03f | 656 | ;; basic information |
eca04a35 | 657 | (desktop-file-name (buffer-file-name) desktop-dirname) |
97872686 | 658 | (buffer-name) |
3ae6b03f JB |
659 | major-mode |
660 | ;; minor modes | |
661 | (let (ret) | |
662 | (mapc | |
663 | #'(lambda (minor-mode) | |
664 | (and (boundp minor-mode) | |
665 | (symbol-value minor-mode) | |
666 | (let* ((special (assq minor-mode desktop-minor-mode-table)) | |
667 | (value (cond (special (cadr special)) | |
668 | ((functionp minor-mode) minor-mode)))) | |
669 | (when value (add-to-list 'ret value))))) | |
670 | (mapcar #'car minor-mode-alist)) | |
671 | ret) | |
672 | ;; point and mark, and read-only status | |
673 | (point) | |
674 | (list (mark t) mark-active) | |
675 | buffer-read-only | |
676 | ;; auxiliary information | |
677 | (when (functionp desktop-save-buffer) | |
eca04a35 | 678 | (funcall desktop-save-buffer desktop-dirname)) |
3ae6b03f JB |
679 | ;; local variables |
680 | (let ((locals desktop-locals-to-save) | |
681 | (loclist (buffer-local-variables)) | |
682 | (ll)) | |
683 | (while locals | |
684 | (let ((here (assq (car locals) loclist))) | |
685 | (if here | |
686 | (setq ll (cons here ll)) | |
687 | (when (member (car locals) loclist) | |
688 | (setq ll (cons (car locals) ll))))) | |
689 | (setq locals (cdr locals))) | |
690 | ll))) | |
691 | ||
4a2fce7a | 692 | ;; ---------------------------------------------------------------------------- |
05f1c4ec | 693 | (defun desktop-internal-v2s (value) |
577ed2b2 RS |
694 | "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE. |
695 | TXT is a string that when read and evaluated yields value. | |
696 | QUOTE may be `may' (value may be quoted), | |
697 | `must' (values must be quoted), or nil (value may not be quoted)." | |
de9e2828 | 698 | (cond |
3ae6b03f JB |
699 | ((or (numberp value) (null value) (eq t value) (keywordp value)) |
700 | (cons 'may (prin1-to-string value))) | |
701 | ((stringp value) | |
702 | (let ((copy (copy-sequence value))) | |
703 | (set-text-properties 0 (length copy) nil copy) | |
704 | ;; Get rid of text properties because we cannot read them | |
705 | (cons 'may (prin1-to-string copy)))) | |
706 | ((symbolp value) | |
707 | (cons 'must (prin1-to-string value))) | |
708 | ((vectorp value) | |
709 | (let* ((special nil) | |
710 | (pass1 (mapcar | |
711 | (lambda (el) | |
712 | (let ((res (desktop-internal-v2s el))) | |
713 | (if (null (car res)) | |
714 | (setq special t)) | |
715 | res)) | |
716 | value))) | |
717 | (if special | |
718 | (cons nil (concat "(vector " | |
719 | (mapconcat (lambda (el) | |
720 | (if (eq (car el) 'must) | |
721 | (concat "'" (cdr el)) | |
722 | (cdr el))) | |
723 | pass1 | |
724 | " ") | |
725 | ")")) | |
726 | (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]"))))) | |
727 | ((consp value) | |
728 | (let ((p value) | |
729 | newlist | |
730 | use-list* | |
731 | anynil) | |
732 | (while (consp p) | |
733 | (let ((q.txt (desktop-internal-v2s (car p)))) | |
734 | (or anynil (setq anynil (null (car q.txt)))) | |
735 | (setq newlist (cons q.txt newlist))) | |
736 | (setq p (cdr p))) | |
737 | (if p | |
738 | (let ((last (desktop-internal-v2s p))) | |
739 | (or anynil (setq anynil (null (car last)))) | |
740 | (or anynil | |
741 | (setq newlist (cons '(must . ".") newlist))) | |
742 | (setq use-list* t) | |
743 | (setq newlist (cons last newlist)))) | |
744 | (setq newlist (nreverse newlist)) | |
745 | (if anynil | |
746 | (cons nil | |
747 | (concat (if use-list* "(desktop-list* " "(list ") | |
748 | (mapconcat (lambda (el) | |
749 | (if (eq (car el) 'must) | |
750 | (concat "'" (cdr el)) | |
751 | (cdr el))) | |
752 | newlist | |
753 | " ") | |
754 | ")")) | |
755 | (cons 'must | |
756 | (concat "(" (mapconcat 'cdr newlist " ") ")"))))) | |
757 | ((subrp value) | |
758 | (cons nil (concat "(symbol-function '" | |
759 | (substring (prin1-to-string value) 7 -1) | |
760 | ")"))) | |
761 | ((markerp value) | |
762 | (let ((pos (prin1-to-string (marker-position value))) | |
763 | (buf (prin1-to-string (buffer-name (marker-buffer value))))) | |
764 | (cons nil (concat "(let ((mk (make-marker)))" | |
765 | " (add-hook 'desktop-delay-hook" | |
766 | " (list 'lambda '() (list 'set-marker mk " | |
767 | pos " (get-buffer " buf ")))) mk)")))) | |
768 | (t ; save as text | |
769 | (cons 'may "\"Unprintable entity\"")))) | |
de9e2828 | 770 | |
4a2fce7a | 771 | ;; ---------------------------------------------------------------------------- |
05f1c4ec | 772 | (defun desktop-value-to-string (value) |
577ed2b2 RS |
773 | "Convert VALUE to a string that when read evaluates to the same value. |
774 | Not all types of values are supported." | |
de9e2828 RS |
775 | (let* ((print-escape-newlines t) |
776 | (float-output-format nil) | |
05f1c4ec | 777 | (quote.txt (desktop-internal-v2s value)) |
de9e2828 RS |
778 | (quote (car quote.txt)) |
779 | (txt (cdr quote.txt))) | |
780 | (if (eq quote 'must) | |
781 | (concat "'" txt) | |
782 | txt))) | |
4a2fce7a | 783 | |
ec4c6f22 | 784 | ;; ---------------------------------------------------------------------------- |
0e7c8611 RS |
785 | (defun desktop-outvar (varspec) |
786 | "Output a setq statement for variable VAR to the desktop file. | |
787 | The argument VARSPEC may be the variable name VAR (a symbol), | |
be617bbf | 788 | or a cons cell of the form (VAR . MAX-SIZE), |
0e7c8611 RS |
789 | which means to truncate VAR's value to at most MAX-SIZE elements |
790 | \(if the value is a list) before saving the value." | |
791 | (let (var size) | |
792 | (if (consp varspec) | |
793 | (setq var (car varspec) size (cdr varspec)) | |
794 | (setq var varspec)) | |
3ae6b03f JB |
795 | (when (boundp var) |
796 | (when (and (integerp size) | |
797 | (> size 0) | |
798 | (listp (eval var))) | |
799 | (desktop-truncate (eval var) size)) | |
800 | (insert "(setq " | |
801 | (symbol-name var) | |
802 | " " | |
803 | (desktop-value-to-string (symbol-value var)) | |
804 | ")\n")))) | |
4a2fce7a | 805 | |
0b9bd504 | 806 | ;; ---------------------------------------------------------------------------- |
ec4c6f22 | 807 | (defun desktop-save-buffer-p (filename bufname mode &rest dummy) |
ebb39555 | 808 | "Return t if buffer should have its state saved in the desktop file. |
0b9bd504 | 809 | FILENAME is the visited file name, BUFNAME is the buffer name, and |
be617bbf JB |
810 | MODE is the major mode. |
811 | \n\(fn FILENAME BUFNAME MODE)" | |
fa379636 | 812 | (let ((case-fold-search nil)) |
80f9f3db | 813 | (and (not (string-match desktop-buffers-not-to-save bufname)) |
ebb39555 LH |
814 | (not (memq mode desktop-modes-not-to-save)) |
815 | (or (and filename | |
816 | (not (string-match desktop-files-not-to-save filename))) | |
817 | (and (eq mode 'dired-mode) | |
818 | (with-current-buffer bufname | |
819 | (not (string-match desktop-files-not-to-save | |
820 | default-directory)))) | |
821 | (and (null filename) | |
822 | (with-current-buffer bufname desktop-save-buffer)))))) | |
4a2fce7a | 823 | |
0b9bd504 | 824 | ;; ---------------------------------------------------------------------------- |
4a2fce7a RS |
825 | (defun desktop-file-name (filename dirname) |
826 | "Convert FILENAME to format specified in `desktop-file-name-format'. | |
827 | DIRNAME must be the directory in which the desktop file will be saved." | |
828 | (cond | |
829 | ((not filename) nil) | |
830 | ((eq desktop-file-name-format 'tilde) | |
831 | (let ((relative-name (file-relative-name (expand-file-name filename) "~"))) | |
832 | (cond | |
833 | ((file-name-absolute-p relative-name) relative-name) | |
834 | ((string= "./" relative-name) "~/") | |
835 | ((string= "." relative-name) "~") | |
836 | (t (concat "~/" relative-name))))) | |
837 | ((eq desktop-file-name-format 'local) (file-relative-name filename dirname)) | |
838 | (t (expand-file-name filename)))) | |
e5714620 | 839 | |
4a2fce7a | 840 | ;; ---------------------------------------------------------------------------- |
11425834 | 841 | ;;;###autoload |
3ae6b03f | 842 | (defun desktop-save (dirname &optional release) |
c5b31c4d LH |
843 | "Save the desktop in a desktop file. |
844 | Parameter DIRNAME specifies where to save the desktop file. | |
3ae6b03f | 845 | Optional parameter RELEASE says whether we're done with this desktop. |
c5b31c4d | 846 | See also `desktop-base-file-name'." |
6343ed61 | 847 | (interactive "DDirectory to save desktop file in: ") |
3ae6b03f | 848 | (setq desktop-dirname (file-name-as-directory (expand-file-name dirname))) |
6343ed61 | 849 | (save-excursion |
3ae6b03f JB |
850 | (let ((eager desktop-restore-eager) |
851 | (new-modtime (nth 5 (file-attributes (desktop-full-file-name))))) | |
852 | (when | |
853 | (or (not new-modtime) ; nothing to overwrite | |
854 | (equal desktop-file-modtime new-modtime) | |
855 | (yes-or-no-p (if desktop-file-modtime | |
856 | (if (> (float-time new-modtime) (float-time desktop-file-modtime)) | |
857 | "Desktop file is more recent than the one loaded. Save anyway? " | |
858 | "Desktop file isn't the one loaded. Overwrite it? ") | |
859 | "Current desktop was not loaded from a file. Overwrite this desktop file? ")) | |
860 | (unless release (error "Desktop file conflict"))) | |
861 | ||
862 | ;; If we're done with it, release the lock. | |
863 | ;; Otherwise, claim it if it's unclaimed or if we created it. | |
864 | (if release | |
865 | (desktop-release-lock) | |
866 | (unless (and new-modtime (desktop-owner)) (desktop-claim-lock))) | |
867 | ||
868 | (with-temp-buffer | |
869 | (insert | |
870 | ";; -*- mode: emacs-lisp; coding: emacs-mule; -*-\n" | |
871 | desktop-header | |
872 | ";; Created " (current-time-string) "\n" | |
873 | ";; Desktop file format version " desktop-file-version "\n" | |
874 | ";; Emacs version " emacs-version "\n") | |
875 | (save-excursion (run-hooks 'desktop-save-hook)) | |
876 | (goto-char (point-max)) | |
877 | (insert "\n;; Global section:\n") | |
878 | (mapc (function desktop-outvar) desktop-globals-to-save) | |
879 | (when (memq 'kill-ring desktop-globals-to-save) | |
880 | (insert | |
881 | "(setq kill-ring-yank-pointer (nthcdr " | |
882 | (int-to-string (- (length kill-ring) (length kill-ring-yank-pointer))) | |
883 | " kill-ring))\n")) | |
884 | ||
885 | (insert "\n;; Buffer section -- buffers listed in same order as in buffer list:\n") | |
886 | (dolist (l (mapcar 'desktop-buffer-info (buffer-list))) | |
97872686 JB |
887 | (let ((base (pop l))) |
888 | (when (apply 'desktop-save-buffer-p l) | |
889 | (insert "(" | |
890 | (if (or (not (integerp eager)) | |
891 | (if (zerop eager) | |
892 | nil | |
893 | (setq eager (1- eager)))) | |
894 | "desktop-create-buffer" | |
895 | "desktop-append-buffer-args") | |
896 | " " | |
897 | desktop-file-version) | |
67aa22cb JB |
898 | ;; If there's a non-empty base name, we save it instead of the buffer name |
899 | (when (and base (not (string= base ""))) | |
900 | (setcar (nthcdr 1 l) base)) | |
97872686 JB |
901 | (dolist (e l) |
902 | (insert "\n " (desktop-value-to-string e))) | |
903 | (insert ")\n\n")))) | |
3ae6b03f | 904 | |
eca04a35 | 905 | (setq default-directory desktop-dirname) |
3ae6b03f JB |
906 | (let ((coding-system-for-write 'emacs-mule)) |
907 | (write-region (point-min) (point-max) (desktop-full-file-name) nil 'nomessage)) | |
908 | ;; We remember when it was modified (which is presumably just now). | |
909 | (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name))))))))) | |
4a2fce7a | 910 | |
0b9bd504 | 911 | ;; ---------------------------------------------------------------------------- |
11425834 | 912 | ;;;###autoload |
6343ed61 | 913 | (defun desktop-remove () |
c5b31c4d LH |
914 | "Delete desktop file in `desktop-dirname'. |
915 | This function also sets `desktop-dirname' to nil." | |
6343ed61 | 916 | (interactive) |
c5b31c4d | 917 | (when desktop-dirname |
11425834 | 918 | (let ((filename (desktop-full-file-name))) |
c5b31c4d LH |
919 | (setq desktop-dirname nil) |
920 | (when (file-exists-p filename) | |
73b0b745 | 921 | (delete-file filename))))) |
c5b31c4d | 922 | |
150f39ee LH |
923 | (defvar desktop-buffer-args-list nil |
924 | "List of args for `desktop-create-buffer'.") | |
925 | ||
926 | (defvar desktop-lazy-timer nil) | |
927 | ||
0b9bd504 | 928 | ;; ---------------------------------------------------------------------------- |
478653c9 | 929 | ;;;###autoload |
c5b31c4d LH |
930 | (defun desktop-read (&optional dirname) |
931 | "Read and process the desktop file in directory DIRNAME. | |
932 | Look for a desktop file in DIRNAME, or if DIRNAME is omitted, look in | |
933 | directories listed in `desktop-path'. If a desktop file is found, it | |
cc8b76bf | 934 | is processed and `desktop-after-read-hook' is run. If no desktop file |
c5b31c4d LH |
935 | is found, clear the desktop and run `desktop-no-desktop-file-hook'. |
936 | This function is a no-op when Emacs is running in batch mode. | |
937 | It returns t if a desktop file was loaded, nil otherwise." | |
6343ed61 | 938 | (interactive) |
4a2fce7a | 939 | (unless noninteractive |
c5b31c4d | 940 | (setq desktop-dirname |
11425834 LH |
941 | (file-name-as-directory |
942 | (expand-file-name | |
943 | (or | |
944 | ;; If DIRNAME is specified, use it. | |
945 | (and (< 0 (length dirname)) dirname) | |
946 | ;; Otherwise search desktop file in desktop-path. | |
947 | (let ((dirs desktop-path)) | |
948 | (while (and dirs | |
949 | (not (file-exists-p | |
950 | (desktop-full-file-name (car dirs))))) | |
951 | (setq dirs (cdr dirs))) | |
952 | (and dirs (car dirs))) | |
953 | ;; If not found and `desktop-path' is non-nil, use its first element. | |
954 | (and desktop-path (car desktop-path)) | |
955 | ;; Default: Home directory. | |
956 | "~")))) | |
957 | (if (file-exists-p (desktop-full-file-name)) | |
3ae6b03f JB |
958 | ;; Desktop file found, but is it already in use? |
959 | (let ((desktop-first-buffer nil) | |
960 | (desktop-buffer-ok-count 0) | |
961 | (desktop-buffer-fail-count 0) | |
962 | (owner (desktop-owner)) | |
963 | ;; Avoid desktop saving during evaluation of desktop buffer. | |
964 | (desktop-save nil)) | |
965 | (if (and owner | |
966 | (memq desktop-load-locked-desktop '(nil ask)) | |
967 | (or (null desktop-load-locked-desktop) | |
968 | (not (y-or-n-p (format "Warning: desktop file appears to be in use by PID %s.\n\ | |
969 | Using it may cause conflicts. Use it anyway? " owner))))) | |
04ba4313 | 970 | (let ((default-directory desktop-dirname)) |
3ae6b03f | 971 | (setq desktop-dirname nil) |
04ba4313 JB |
972 | (run-hooks 'desktop-not-loaded-hook) |
973 | (unless desktop-dirname | |
974 | (message "Desktop file in use; not loaded."))) | |
3ae6b03f JB |
975 | (desktop-lazy-abort) |
976 | ;; Evaluate desktop buffer and remember when it was modified. | |
977 | (load (desktop-full-file-name) t t t) | |
978 | (setq desktop-file-modtime (nth 5 (file-attributes (desktop-full-file-name)))) | |
979 | ;; If it wasn't already, mark it as in-use, to bother other | |
980 | ;; desktop instances. | |
981 | (unless owner | |
982 | (condition-case nil | |
983 | (desktop-claim-lock) | |
984 | (file-error (message "Couldn't record use of desktop file") | |
985 | (sit-for 1)))) | |
986 | ||
987 | ;; `desktop-create-buffer' puts buffers at end of the buffer list. | |
988 | ;; We want buffers existing prior to evaluating the desktop (and | |
989 | ;; not reused) to be placed at the end of the buffer list, so we | |
990 | ;; move them here. | |
991 | (mapc 'bury-buffer | |
992 | (nreverse (cdr (memq desktop-first-buffer (nreverse (buffer-list)))))) | |
993 | (switch-to-buffer (car (buffer-list))) | |
994 | (run-hooks 'desktop-delay-hook) | |
995 | (setq desktop-delay-hook nil) | |
996 | (run-hooks 'desktop-after-read-hook) | |
997 | (message "Desktop: %d buffer%s restored%s%s." | |
998 | desktop-buffer-ok-count | |
999 | (if (= 1 desktop-buffer-ok-count) "" "s") | |
1000 | (if (< 0 desktop-buffer-fail-count) | |
1001 | (format ", %d failed to restore" desktop-buffer-fail-count) | |
1002 | "") | |
1003 | (if desktop-buffer-args-list | |
1004 | (format ", %d to restore lazily" | |
1005 | (length desktop-buffer-args-list)) | |
1006 | "")) | |
1007 | t)) | |
c5b31c4d LH |
1008 | ;; No desktop file found. |
1009 | (desktop-clear) | |
1010 | (let ((default-directory desktop-dirname)) | |
1011 | (run-hooks 'desktop-no-desktop-file-hook)) | |
1012 | (message "No desktop file.") | |
1013 | nil))) | |
4a2fce7a | 1014 | |
0b9bd504 | 1015 | ;; ---------------------------------------------------------------------------- |
c5b31c4d | 1016 | ;; Maintained for backward compatibility |
478653c9 | 1017 | ;;;###autoload |
6343ed61 | 1018 | (defun desktop-load-default () |
577ed2b2 | 1019 | "Load the `default' start-up library manually. |
c5b31c4d | 1020 | Also inhibit further loading of it." |
b89c5a72 JB |
1021 | (unless inhibit-default-init ; safety check |
1022 | (load "default" t t) | |
1023 | (setq inhibit-default-init t))) | |
1024 | (make-obsolete 'desktop-load-default | |
1025 | 'desktop-save-mode "22.1") | |
4a2fce7a RS |
1026 | |
1027 | ;; ---------------------------------------------------------------------------- | |
1028 | ;;;###autoload | |
c5b31c4d LH |
1029 | (defun desktop-change-dir (dirname) |
1030 | "Change to desktop saved in DIRNAME. | |
1031 | Kill the desktop as specified by variables `desktop-save-mode' and | |
1032 | `desktop-save', then clear the desktop and load the desktop file in | |
1033 | directory DIRNAME." | |
1034 | (interactive "DChange to directory: ") | |
1035 | (setq dirname (file-name-as-directory (expand-file-name dirname desktop-dirname))) | |
4a2fce7a RS |
1036 | (desktop-kill) |
1037 | (desktop-clear) | |
c5b31c4d | 1038 | (desktop-read dirname)) |
bf247b6e | 1039 | |
c5b31c4d | 1040 | ;; ---------------------------------------------------------------------------- |
4a2fce7a | 1041 | ;;;###autoload |
c5b31c4d LH |
1042 | (defun desktop-save-in-desktop-dir () |
1043 | "Save the desktop in directory `desktop-dirname'." | |
4a2fce7a RS |
1044 | (interactive) |
1045 | (if desktop-dirname | |
b89c5a72 | 1046 | (desktop-save desktop-dirname) |
4a2fce7a RS |
1047 | (call-interactively 'desktop-save)) |
1048 | (message "Desktop saved in %s" desktop-dirname)) | |
1049 | ||
1050 | ;; ---------------------------------------------------------------------------- | |
1051 | ;;;###autoload | |
1052 | (defun desktop-revert () | |
1053 | "Revert to the last loaded desktop." | |
1054 | (interactive) | |
c5b31c4d LH |
1055 | (unless desktop-dirname |
1056 | (error "Unknown desktop directory")) | |
11425834 | 1057 | (unless (file-exists-p (desktop-full-file-name)) |
c5b31c4d LH |
1058 | (error "No desktop file found")) |
1059 | (desktop-clear) | |
1060 | (desktop-read desktop-dirname)) | |
4a2fce7a | 1061 | |
341c2f07 SM |
1062 | (defvar desktop-buffer-major-mode) |
1063 | (defvar desktop-buffer-locals) | |
0b9bd504 | 1064 | ;; ---------------------------------------------------------------------------- |
e5780ae1 LH |
1065 | (defun desktop-restore-file-buffer (desktop-buffer-file-name |
1066 | desktop-buffer-name | |
1067 | desktop-buffer-misc) | |
1068 | "Restore a file buffer." | |
3ae6b03f JB |
1069 | (when desktop-buffer-file-name |
1070 | (if (or (file-exists-p desktop-buffer-file-name) | |
1071 | (let ((msg (format "Desktop: File \"%s\" no longer exists." | |
1072 | desktop-buffer-file-name))) | |
1073 | (if desktop-missing-file-warning | |
1074 | (y-or-n-p (concat msg " Re-create buffer? ")) | |
1075 | (message "%s" msg) | |
1076 | nil))) | |
1077 | (let* ((auto-insert nil) ; Disable auto insertion | |
1078 | (coding-system-for-read | |
1079 | (or coding-system-for-read | |
1080 | (cdr (assq 'buffer-file-coding-system | |
1081 | desktop-buffer-locals)))) | |
1082 | (buf (find-file-noselect desktop-buffer-file-name))) | |
1083 | (condition-case nil | |
1084 | (switch-to-buffer buf) | |
1085 | (error (pop-to-buffer buf))) | |
1086 | (and (not (eq major-mode desktop-buffer-major-mode)) | |
1087 | (functionp desktop-buffer-major-mode) | |
1088 | (funcall desktop-buffer-major-mode)) | |
1089 | buf) | |
1090 | nil))) | |
4a2fce7a | 1091 | |
0f4804e7 LH |
1092 | (defun desktop-load-file (function) |
1093 | "Load the file where auto loaded FUNCTION is defined." | |
97b3a214 | 1094 | (when function |
07696982 | 1095 | (let ((fcell (and (fboundp function) (symbol-function function)))) |
97b3a214 LH |
1096 | (when (and (listp fcell) |
1097 | (eq 'autoload (car fcell))) | |
1098 | (load (cadr fcell)))))) | |
0f4804e7 | 1099 | |
0b9bd504 | 1100 | ;; ---------------------------------------------------------------------------- |
eb982898 JL |
1101 | ;; Create a buffer, load its file, set its mode, ...; |
1102 | ;; called from Desktop file only. | |
1d500ca6 | 1103 | |
0f4804e7 | 1104 | ;; Just to silence the byte compiler. |
341c2f07 SM |
1105 | |
1106 | (defvar desktop-first-buffer) ; Dynamically bound in `desktop-read' | |
1107 | ||
1108 | ;; Bound locally in `desktop-read'. | |
1109 | (defvar desktop-buffer-ok-count) | |
1110 | (defvar desktop-buffer-fail-count) | |
4a2fce7a | 1111 | |
340db502 LH |
1112 | (defun desktop-create-buffer |
1113 | (desktop-file-version | |
1114 | desktop-buffer-file-name | |
1115 | desktop-buffer-name | |
1116 | desktop-buffer-major-mode | |
1117 | desktop-buffer-minor-modes | |
1118 | desktop-buffer-point | |
1119 | desktop-buffer-mark | |
1120 | desktop-buffer-read-only | |
1121 | desktop-buffer-misc | |
1122 | &optional | |
1123 | desktop-buffer-locals) | |
4a2fce7a RS |
1124 | ;; To make desktop files with relative file names possible, we cannot |
1125 | ;; allow `default-directory' to change. Therefore we save current buffer. | |
1126 | (save-current-buffer | |
0f4804e7 LH |
1127 | ;; Give major mode module a chance to add a handler. |
1128 | (desktop-load-file desktop-buffer-major-mode) | |
340db502 LH |
1129 | (let ((buffer-list (buffer-list)) |
1130 | (result | |
1131 | (condition-case err | |
1132 | (funcall (or (cdr (assq desktop-buffer-major-mode | |
1133 | desktop-buffer-mode-handlers)) | |
1134 | 'desktop-restore-file-buffer) | |
1135 | desktop-buffer-file-name | |
1136 | desktop-buffer-name | |
1137 | desktop-buffer-misc) | |
1138 | (error | |
1139 | (message "Desktop: Can't load buffer %s: %s" | |
1140 | desktop-buffer-name | |
1141 | (error-message-string err)) | |
1142 | (when desktop-missing-file-warning (sit-for 1)) | |
1143 | nil)))) | |
e5780ae1 LH |
1144 | (if (bufferp result) |
1145 | (setq desktop-buffer-ok-count (1+ desktop-buffer-ok-count)) | |
1146 | (setq desktop-buffer-fail-count (1+ desktop-buffer-fail-count)) | |
1147 | (setq result nil)) | |
7bcbf3c2 JB |
1148 | ;; Restore buffer list order with new buffer at end. Don't change |
1149 | ;; the order for old desktop files (old desktop module behaviour). | |
4a2fce7a | 1150 | (unless (< desktop-file-version 206) |
48aa4dfc | 1151 | (mapc 'bury-buffer buffer-list) |
7bcbf3c2 | 1152 | (when result (bury-buffer result))) |
4a2fce7a | 1153 | (when result |
7bcbf3c2 JB |
1154 | (unless (or desktop-first-buffer (< desktop-file-version 206)) |
1155 | (setq desktop-first-buffer result)) | |
4a2fce7a RS |
1156 | (set-buffer result) |
1157 | (unless (equal (buffer-name) desktop-buffer-name) | |
93c5a976 | 1158 | (rename-buffer desktop-buffer-name t)) |
4a2fce7a | 1159 | ;; minor modes |
340db502 LH |
1160 | (cond ((equal '(t) desktop-buffer-minor-modes) ; backwards compatible |
1161 | (auto-fill-mode 1)) | |
1162 | ((equal '(nil) desktop-buffer-minor-modes) ; backwards compatible | |
1163 | (auto-fill-mode 0)) | |
1164 | (t | |
341c2f07 SM |
1165 | (dolist (minor-mode desktop-buffer-minor-modes) |
1166 | ;; Give minor mode module a chance to add a handler. | |
1167 | (desktop-load-file minor-mode) | |
1168 | (let ((handler (cdr (assq minor-mode desktop-minor-mode-handlers)))) | |
1169 | (if handler | |
1170 | (funcall handler desktop-buffer-locals) | |
1171 | (when (functionp minor-mode) | |
1172 | (funcall minor-mode 1))))))) | |
1173 | ;; Even though point and mark are non-nil when written by | |
1174 | ;; `desktop-save', they may be modified by handlers wanting to set | |
1175 | ;; point or mark themselves. | |
e5780ae1 LH |
1176 | (when desktop-buffer-point |
1177 | (goto-char | |
1178 | (condition-case err | |
341c2f07 SM |
1179 | ;; Evaluate point. Thus point can be something like |
1180 | ;; '(search-forward ... | |
e5780ae1 LH |
1181 | (eval desktop-buffer-point) |
1182 | (error (message "%s" (error-message-string err)) 1)))) | |
4a2fce7a RS |
1183 | (when desktop-buffer-mark |
1184 | (if (consp desktop-buffer-mark) | |
3ae6b03f JB |
1185 | (progn |
1186 | (set-mark (car desktop-buffer-mark)) | |
1187 | (setq mark-active (car (cdr desktop-buffer-mark)))) | |
4a2fce7a RS |
1188 | (set-mark desktop-buffer-mark))) |
1189 | ;; Never override file system if the file really is read-only marked. | |
3ae6b03f | 1190 | (when desktop-buffer-read-only (setq buffer-read-only desktop-buffer-read-only)) |
4a2fce7a RS |
1191 | (while desktop-buffer-locals |
1192 | (let ((this (car desktop-buffer-locals))) | |
1193 | (if (consp this) | |
3ae6b03f JB |
1194 | ;; an entry of this form `(symbol . value)' |
1195 | (progn | |
1196 | (make-local-variable (car this)) | |
1197 | (set (car this) (cdr this))) | |
4a2fce7a RS |
1198 | ;; an entry of the form `symbol' |
1199 | (make-local-variable this) | |
1200 | (makunbound this))) | |
1201 | (setq desktop-buffer-locals (cdr desktop-buffer-locals))))))) | |
ec4c6f22 | 1202 | |
4a2fce7a | 1203 | ;; ---------------------------------------------------------------------------- |
ec4c6f22 | 1204 | ;; Backward compatibility -- update parameters to 205 standards. |
569c754e RS |
1205 | (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name |
1206 | desktop-buffer-major-mode | |
2699e23e | 1207 | mim pt mk ro tl fc cfs cr desktop-buffer-misc) |
569c754e | 1208 | (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name |
2699e23e RS |
1209 | desktop-buffer-major-mode (cdr mim) pt mk ro |
1210 | desktop-buffer-misc | |
ec4c6f22 RS |
1211 | (list (cons 'truncate-lines tl) |
1212 | (cons 'fill-column fc) | |
1213 | (cons 'case-fold-search cfs) | |
1214 | (cons 'case-replace cr) | |
1215 | (cons 'overwrite-mode (car mim))))) | |
84b538ec | 1216 | |
150f39ee | 1217 | (defun desktop-append-buffer-args (&rest args) |
cc8b76bf | 1218 | "Append ARGS at end of `desktop-buffer-args-list'. |
150f39ee LH |
1219 | ARGS must be an argument list for `desktop-create-buffer'." |
1220 | (setq desktop-buffer-args-list (nconc desktop-buffer-args-list (list args))) | |
1221 | (unless desktop-lazy-timer | |
1222 | (setq desktop-lazy-timer | |
1223 | (run-with-idle-timer desktop-lazy-idle-delay t 'desktop-idle-create-buffers)))) | |
1224 | ||
1225 | (defun desktop-lazy-create-buffer () | |
1226 | "Pop args from `desktop-buffer-args-list', create buffer and bury it." | |
1227 | (when desktop-buffer-args-list | |
1228 | (let* ((remaining (length desktop-buffer-args-list)) | |
1229 | (args (pop desktop-buffer-args-list)) | |
1230 | (buffer-name (nth 2 args)) | |
1231 | (msg (format "Desktop lazily opening %s (%s remaining)..." | |
1232 | buffer-name remaining))) | |
1233 | (when desktop-lazy-verbose | |
8a26c165 | 1234 | (message "%s" msg)) |
150f39ee LH |
1235 | (let ((desktop-first-buffer nil) |
1236 | (desktop-buffer-ok-count 0) | |
1237 | (desktop-buffer-fail-count 0)) | |
1238 | (apply 'desktop-create-buffer args) | |
1239 | (run-hooks 'desktop-delay-hook) | |
1240 | (setq desktop-delay-hook nil) | |
1241 | (bury-buffer (get-buffer buffer-name)) | |
1242 | (when desktop-lazy-verbose | |
1243 | (message "%s%s" msg (if (> desktop-buffer-ok-count 0) "done" "failed"))))))) | |
1244 | ||
1245 | (defun desktop-idle-create-buffers () | |
1246 | "Create buffers until the user does something, then stop. | |
1247 | If there are no buffers left to create, kill the timer." | |
1248 | (let ((repeat 1)) | |
1249 | (while (and repeat desktop-buffer-args-list) | |
1250 | (save-window-excursion | |
1251 | (desktop-lazy-create-buffer)) | |
1252 | (setq repeat (sit-for 0.2)) | |
1253 | (unless desktop-buffer-args-list | |
1254 | (cancel-timer desktop-lazy-timer) | |
1255 | (setq desktop-lazy-timer nil) | |
1256 | (message "Lazy desktop load complete") | |
1257 | (sit-for 3) | |
1258 | (message ""))))) | |
1259 | ||
1260 | (defun desktop-lazy-complete () | |
1261 | "Run the desktop load to completion." | |
1262 | (interactive) | |
1263 | (let ((desktop-lazy-verbose t)) | |
1264 | (while desktop-buffer-args-list | |
1265 | (save-window-excursion | |
1266 | (desktop-lazy-create-buffer))) | |
1267 | (message "Lazy desktop load complete"))) | |
1268 | ||
1269 | (defun desktop-lazy-abort () | |
1270 | "Abort lazy loading of the desktop." | |
1271 | (interactive) | |
1272 | (when desktop-lazy-timer | |
1273 | (cancel-timer desktop-lazy-timer) | |
1274 | (setq desktop-lazy-timer nil)) | |
1275 | (when desktop-buffer-args-list | |
1276 | (setq desktop-buffer-args-list nil) | |
1277 | (when (interactive-p) | |
1278 | (message "Lazy desktop load aborted")))) | |
1279 | ||
0b9bd504 | 1280 | ;; ---------------------------------------------------------------------------- |
c5b31c4d | 1281 | ;; When `desktop-save-mode' is non-nil and "--no-desktop" is not specified on the |
4a2fce7a RS |
1282 | ;; command line, we do the rest of what it takes to use desktop, but do it |
1283 | ;; after finishing loading the init file. | |
1284 | ;; We cannot use `command-switch-alist' to process "--no-desktop" because these | |
1285 | ;; functions are processed after `after-init-hook'. | |
1286 | (add-hook | |
1287 | 'after-init-hook | |
341c2f07 | 1288 | (lambda () |
4a2fce7a | 1289 | (let ((key "--no-desktop")) |
4b217d46 LH |
1290 | (when (member key command-line-args) |
1291 | (setq command-line-args (delete key command-line-args)) | |
1292 | (setq desktop-save-mode nil))) | |
4c9702c3 JL |
1293 | (when desktop-save-mode |
1294 | (desktop-read) | |
1295 | (setq inhibit-startup-screen t)))) | |
813dbb2d | 1296 | |
ab1d55ea | 1297 | (provide 'desktop) |
6343ed61 | 1298 | |
341c2f07 | 1299 | ;; arch-tag: 221907c3-1771-4fd3-9c2e-c6f700c6ede9 |
80f9f3db | 1300 | ;;; desktop.el ends here |