Commit | Line | Data |
---|---|---|
6343ed61 RS |
1 | ;;; desktop.el --- save partial status of Emacs when killed |
2 | ||
f4c73d07 | 3 | ;; Copyright (C) 1993, 1994, 1995 Free Software Foundation, Inc. |
6343ed61 RS |
4 | |
5 | ;; Author: Morten Welinder <terra@diku.dk> | |
b6105c76 RS |
6 | ;; Keywords: customization |
7 | ;; Favourite-brand-of-beer: None, I hate beer. | |
6343ed61 RS |
8 | |
9 | ;; This file is part of GNU Emacs. | |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b578f267 EN |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
6343ed61 RS |
25 | |
26 | ;;; Commentary: | |
27 | ||
0b9bd504 RS |
28 | ;; Save the Desktop, i.e., |
29 | ;; - some global variables | |
30 | ;; - the list of buffers with associated files. For each buffer also | |
31 | ;; - the major mode | |
32 | ;; - the default directory | |
33 | ;; - the point | |
34 | ;; - the mark & mark-active | |
35 | ;; - buffer-read-only | |
ec4c6f22 | 36 | ;; - some local variables |
6343ed61 | 37 | |
0b9bd504 RS |
38 | ;; To use this, first put these three lines in the bottom of your .emacs |
39 | ;; file (the later the better): | |
40 | ;; | |
41 | ;; (load "desktop") | |
42 | ;; (desktop-load-default) | |
43 | ;; (desktop-read) | |
44 | ;; | |
ec4c6f22 | 45 | ;; Between the second and the third line you may wish to add something that |
de9e2828 | 46 | ;; updates the variables `desktop-globals-to-save' and/or |
ec4c6f22 RS |
47 | ;; `desktop-locals-to-save'. If for instance you want to save the local |
48 | ;; variable `foobar' for every buffer in which it is local, you could add | |
49 | ;; the line | |
50 | ;; | |
51 | ;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save)) | |
52 | ;; | |
f4c73d07 | 53 | ;; To avoid saving excessive amounts of data you may also wish to add |
ec4c6f22 RS |
54 | ;; something like the following |
55 | ;; | |
56 | ;; (add-hook 'kill-emacs-hook | |
de9e2828 | 57 | ;; '(lambda () |
ec4c6f22 RS |
58 | ;; (desktop-truncate search-ring 3) |
59 | ;; (desktop-truncate regexp-search-ring 3))) | |
60 | ;; | |
61 | ;; which will make sure that no more than three search items are saved. You | |
fa379636 KH |
62 | ;; must place this line *after* the (load "desktop") line. See also the |
63 | ;; variable desktop-save-hook. | |
6343ed61 | 64 | |
0b9bd504 RS |
65 | ;; Start Emacs in the root directory of your "project". The desktop saver |
66 | ;; is inactive by default. You activate it by M-x desktop-save RET. When | |
67 | ;; you exit the next time the above data will be saved. This ensures that | |
68 | ;; all the files you were editing will be reloaded the next time you start | |
69 | ;; Emacs from the same directory and that points will be set where you | |
ec4c6f22 | 70 | ;; left them. If you save a desktop file in your home directory it will |
de9e2828 | 71 | ;; act as a default desktop when you start Emacs from a directory that |
ec4c6f22 RS |
72 | ;; doesn't have its own. I never do this, but you may want to. |
73 | ||
74 | ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs | |
75 | ;; in your home directory is used for that. Saving global default values | |
76 | ;; for buffers is an example of misuse. | |
77 | ||
0b9bd504 RS |
78 | ;; PLEASE NOTE: The kill ring can be saved as specified by the variable |
79 | ;; `desktop-globals-to-save' (by default it isn't). This may result in saving | |
80 | ;; things you did not mean to keep. Use M-x desktop-clear RET. | |
ec4c6f22 | 81 | |
fa379636 KH |
82 | ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas. |
83 | ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip. | |
84 | ;; chris@tecc.co.uk (Chris Boucher) for a mark tip. | |
85 | ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip. | |
86 | ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt. | |
f4c73d07 | 87 | ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips. |
253406b9 | 88 | ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips. |
0b9bd504 RS |
89 | ;; --------------------------------------------------------------------------- |
90 | ;; TODO: | |
91 | ;; | |
92 | ;; Save window configuration. | |
93 | ;; Recognize more minor modes. | |
94 | ;; Save mark rings. | |
95 | ;; Start-up with buffer-menu??? | |
6343ed61 RS |
96 | |
97 | ;;; Code: | |
98 | ||
ec4c6f22 RS |
99 | ;; Make the compilation more silent |
100 | (eval-when-compile | |
101 | ;; We use functions from these modules | |
fa379636 KH |
102 | ;; We can't (require 'mh-e) since that wants to load something. |
103 | (mapcar 'require '(info dired reporter))) | |
ec4c6f22 | 104 | ;; ---------------------------------------------------------------------------- |
0b9bd504 RS |
105 | ;; USER OPTIONS -- settings you might want to play with. |
106 | ;; ---------------------------------------------------------------------------- | |
107 | (defconst desktop-basefilename | |
4a091f2a | 108 | (if (or (eq system-type 'ms-dos) (eq system-type 'windows-nt)) |
6343ed61 RS |
109 | "emacs.dsk" ; Ms-Dos does not support multiple dots in file name |
110 | ".emacs.desktop") | |
577ed2b2 | 111 | "File for Emacs desktop, not including the directory name.") |
6343ed61 | 112 | |
0b9bd504 | 113 | (defvar desktop-missing-file-warning t |
577ed2b2 RS |
114 | "*If non-nil then desktop warns when a file no longer exists. |
115 | Otherwise it simply ignores that file.") | |
6343ed61 | 116 | |
0b9bd504 | 117 | (defvar desktop-globals-to-save |
6343ed61 | 118 | (list 'desktop-missing-file-warning |
0b9bd504 | 119 | ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer |
ec4c6f22 | 120 | ;; 'kill-ring |
0b9bd504 RS |
121 | 'tags-file-name |
122 | 'tags-table-list | |
ec4c6f22 RS |
123 | 'search-ring |
124 | 'regexp-search-ring | |
de9e2828 | 125 | 'register-alist |
0b9bd504 | 126 | ;; 'desktop-globals-to-save ; Itself! |
b6105c76 | 127 | ) |
0e7c8611 RS |
128 | "List of global variables to save when killing Emacs. |
129 | An element may be variable name (a symbol) | |
130 | or a cons cell of the form (VAR . MAX-SIZE), | |
131 | which means to truncate VAR's value to at most MAX-SIZE elements | |
132 | \(if the value is a list) before saving the value.") | |
6343ed61 | 133 | |
ec4c6f22 RS |
134 | (defvar desktop-locals-to-save |
135 | (list 'desktop-locals-to-save ; Itself! Think it over. | |
136 | 'truncate-lines | |
137 | 'case-fold-search | |
138 | 'case-replace | |
139 | 'fill-column | |
140 | 'overwrite-mode | |
141 | 'change-log-default-name | |
fa379636 | 142 | 'line-number-mode |
ec4c6f22 | 143 | ) |
577ed2b2 RS |
144 | "List of local variables to save for each buffer. |
145 | The variables are saved only when they really are local.") | |
de9e2828 | 146 | (make-variable-buffer-local 'desktop-locals-to-save) |
ec4c6f22 | 147 | |
0b9bd504 | 148 | ;; We skip .log files because they are normally temporary. |
a7acbbe4 | 149 | ;; (ftp) files because they require passwords and whatnot. |
0b9bd504 | 150 | ;; TAGS files to save time (tags-file-name is saved instead). |
6343ed61 | 151 | (defvar desktop-buffers-not-to-save |
de9e2828 | 152 | "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$" |
6343ed61 RS |
153 | "Regexp identifying buffers that are to be excluded from saving.") |
154 | ||
fa379636 KH |
155 | ;; Skip ange-ftp files |
156 | (defvar desktop-files-not-to-save | |
157 | "^/[^/:]*:" | |
158 | "Regexp identifying files whose buffers are to be excluded from saving.") | |
159 | ||
569c754e RS |
160 | (defvar desktop-buffer-major-mode nil |
161 | "When desktop creates a buffer, this holds the desired Major mode.") | |
162 | ||
163 | (defvar desktop-buffer-file-name nil | |
164 | "When desktop creates a buffer, this holds the file name to visit.") | |
165 | ||
166 | (defvar desktop-buffer-name nil | |
167 | "When desktop creates a buffer, this holds the desired buffer name.") | |
168 | ||
2699e23e RS |
169 | (defvar desktop-buffer-misc nil |
170 | "When desktop creates a buffer, this holds a list of misc info. | |
171 | It is used by the `desktop-buffer-handlers' functions.") | |
172 | ||
6343ed61 | 173 | (defvar desktop-buffer-handlers |
0b9bd504 | 174 | '(desktop-buffer-dired |
6343ed61 | 175 | desktop-buffer-rmail |
ec4c6f22 | 176 | desktop-buffer-mh |
6343ed61 RS |
177 | desktop-buffer-info |
178 | desktop-buffer-file) | |
577ed2b2 | 179 | "*List of functions to call in order to create a buffer. |
569c754e RS |
180 | The functions are called without explicit parameters but can use the |
181 | variables `desktop-buffer-major-mode', `desktop-buffer-file-name', | |
182 | `desktop-buffer-name'. | |
183 | If one function returns non-nil, no further functions are called. | |
184 | If the function returns t then the buffer is considered created.") | |
ec4c6f22 RS |
185 | |
186 | (defvar desktop-create-buffer-form "(desktop-create-buffer 205" | |
187 | "Opening of form for creation of new buffers.") | |
fa379636 KH |
188 | |
189 | (defvar desktop-save-hook nil | |
190 | "Hook run before saving the desktop to allow you to cut history lists and | |
191 | the like shorter.") | |
0b9bd504 RS |
192 | ;; ---------------------------------------------------------------------------- |
193 | (defvar desktop-dirname nil | |
6343ed61 RS |
194 | "The directory in which the current desktop file resides.") |
195 | ||
196 | (defconst desktop-header | |
0b9bd504 RS |
197 | ";; -------------------------------------------------------------------------- |
198 | ;; Desktop File for Emacs | |
199 | ;; -------------------------------------------------------------------------- | |
6343ed61 | 200 | " "*Header to place in Desktop file.") |
de9e2828 RS |
201 | |
202 | (defvar desktop-delay-hook nil | |
203 | "Hooks run after all buffers are loaded; intended for internal use.") | |
0b9bd504 | 204 | ;; ---------------------------------------------------------------------------- |
ec4c6f22 RS |
205 | (defun desktop-truncate (l n) |
206 | "Truncate LIST to at most N elements destructively." | |
207 | (let ((here (nthcdr (1- n) l))) | |
208 | (if (consp here) | |
de9e2828 | 209 | (setcdr here nil)))) |
ec4c6f22 | 210 | ;; ---------------------------------------------------------------------------- |
6343ed61 RS |
211 | (defun desktop-clear () "Empty the Desktop." |
212 | (interactive) | |
fa379636 KH |
213 | (setq kill-ring nil |
214 | kill-ring-yank-pointer nil | |
215 | search-ring nil | |
216 | search-ring-yank-pointer nil | |
217 | regexp-search-ring nil | |
218 | regexp-search-ring-yank-pointer nil) | |
b6105c76 RS |
219 | (mapcar (function kill-buffer) (buffer-list)) |
220 | (delete-other-windows)) | |
0b9bd504 | 221 | ;; ---------------------------------------------------------------------------- |
de9e2828 | 222 | (add-hook 'kill-emacs-hook 'desktop-kill) |
ec4c6f22 | 223 | |
6343ed61 | 224 | (defun desktop-kill () |
0b9bd504 | 225 | (if desktop-dirname |
fa379636 KH |
226 | (condition-case err |
227 | (desktop-save desktop-dirname) | |
228 | (file-error | |
229 | (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ") | |
230 | nil | |
231 | (signal (car err) (cdr err))))))) | |
0b9bd504 | 232 | ;; ---------------------------------------------------------------------------- |
ed2f7fc8 RS |
233 | (defun desktop-list* (&rest args) |
234 | (if (null (cdr args)) | |
235 | (car args) | |
236 | (setq args (nreverse args)) | |
237 | (let ((value (cons (nth 1 args) (car args)))) | |
238 | (setq args (cdr (cdr args))) | |
239 | (while args | |
240 | (setq value (cons (car args) value)) | |
241 | (setq args (cdr args))) | |
242 | value))) | |
243 | ||
de9e2828 | 244 | (defun desktop-internal-v2s (val) |
577ed2b2 RS |
245 | "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE. |
246 | TXT is a string that when read and evaluated yields value. | |
247 | QUOTE may be `may' (value may be quoted), | |
248 | `must' (values must be quoted), or nil (value may not be quoted)." | |
de9e2828 | 249 | (cond |
e2247420 | 250 | ((or (numberp val) (null val) (eq t val)) |
de9e2828 | 251 | (cons 'may (prin1-to-string val))) |
e2247420 | 252 | ((stringp val) |
95bf6435 RS |
253 | (let ((copy (copy-sequence val))) |
254 | (set-text-properties 0 (length copy) nil copy) | |
255 | ;; Get rid of text properties because we cannot read them | |
256 | (cons 'may (prin1-to-string copy)))) | |
de9e2828 RS |
257 | ((symbolp val) |
258 | (cons 'must (prin1-to-string val))) | |
259 | ((vectorp val) | |
260 | (let* ((special nil) | |
261 | (pass1 (mapcar | |
262 | (lambda (el) | |
263 | (let ((res (desktop-internal-v2s el))) | |
264 | (if (null (car res)) | |
265 | (setq special t)) | |
266 | res)) | |
267 | val))) | |
268 | (if special | |
269 | (cons nil (concat "(vector " | |
270 | (mapconcat (lambda (el) | |
271 | (if (eq (car el) 'must) | |
272 | (concat "'" (cdr el)) | |
273 | (cdr el))) | |
274 | pass1 | |
275 | " ") | |
276 | ")")) | |
277 | (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]"))))) | |
278 | ((consp val) | |
b4929f75 RS |
279 | (let ((p val) |
280 | newlist | |
ed2f7fc8 | 281 | use-list* |
b4929f75 RS |
282 | anynil) |
283 | (while (consp p) | |
284 | (let ((q.txt (desktop-internal-v2s (car p)))) | |
285 | (or anynil (setq anynil (null (car q.txt)))) | |
286 | (setq newlist (cons q.txt newlist))) | |
287 | (setq p (cdr p))) | |
288 | (if p | |
289 | (let ((last (desktop-internal-v2s p)) | |
290 | (el (car newlist))) | |
ed2f7fc8 RS |
291 | (or anynil (setq anynil (null (car last)))) |
292 | (or anynil | |
293 | (setq newlist (cons '(must . ".") newlist))) | |
294 | (setq use-list* t) | |
295 | (setq newlist (cons last newlist)))) | |
b4929f75 RS |
296 | (setq newlist (nreverse newlist)) |
297 | (if anynil | |
298 | (cons nil | |
ed2f7fc8 | 299 | (concat (if use-list* "(desktop-list* " "(list ") |
b4929f75 RS |
300 | (mapconcat (lambda (el) |
301 | (if (eq (car el) 'must) | |
302 | (concat "'" (cdr el)) | |
303 | (cdr el))) | |
304 | newlist | |
305 | " ") | |
306 | ")")) | |
307 | (cons 'must | |
308 | (concat "(" (mapconcat 'cdr newlist " ") ")"))))) | |
de9e2828 RS |
309 | ((subrp val) |
310 | (cons nil (concat "(symbol-function '" | |
311 | (substring (prin1-to-string val) 7 -1) | |
312 | ")"))) | |
313 | ((markerp val) | |
314 | (let ((pos (prin1-to-string (marker-position val))) | |
315 | (buf (prin1-to-string (buffer-name (marker-buffer val))))) | |
316 | (cons nil (concat "(let ((mk (make-marker)))" | |
317 | " (add-hook 'desktop-delay-hook" | |
318 | " (list 'lambda '() (list 'set-marker mk " | |
319 | pos " (get-buffer " buf ")))) mk)")))) | |
320 | (t ; save as text | |
fa379636 | 321 | (cons 'may "\"Unprintable entity\"")))) |
de9e2828 | 322 | |
ec4c6f22 | 323 | (defun desktop-value-to-string (val) |
577ed2b2 RS |
324 | "Convert VALUE to a string that when read evaluates to the same value. |
325 | Not all types of values are supported." | |
de9e2828 RS |
326 | (let* ((print-escape-newlines t) |
327 | (float-output-format nil) | |
328 | (quote.txt (desktop-internal-v2s val)) | |
329 | (quote (car quote.txt)) | |
330 | (txt (cdr quote.txt))) | |
331 | (if (eq quote 'must) | |
332 | (concat "'" txt) | |
333 | txt))) | |
ec4c6f22 | 334 | ;; ---------------------------------------------------------------------------- |
0e7c8611 RS |
335 | (defun desktop-outvar (varspec) |
336 | "Output a setq statement for variable VAR to the desktop file. | |
337 | The argument VARSPEC may be the variable name VAR (a symbol), | |
338 | or a cons cell of the form (VAR . MAX-SIZE), | |
339 | which means to truncate VAR's value to at most MAX-SIZE elements | |
340 | \(if the value is a list) before saving the value." | |
341 | (let (var size) | |
342 | (if (consp varspec) | |
343 | (setq var (car varspec) size (cdr varspec)) | |
344 | (setq var varspec)) | |
345 | (if (boundp var) | |
346 | (progn | |
347 | (if (and (integerp size) | |
348 | (> size 0) | |
349 | (listp (eval var))) | |
350 | (desktop-truncate (eval var) size)) | |
351 | (insert "(setq " | |
352 | (symbol-name var) | |
353 | " " | |
354 | (desktop-value-to-string (symbol-value var)) | |
355 | ")\n"))))) | |
0b9bd504 | 356 | ;; ---------------------------------------------------------------------------- |
ec4c6f22 | 357 | (defun desktop-save-buffer-p (filename bufname mode &rest dummy) |
b6105c76 | 358 | "Return t if the desktop should record a particular buffer for next startup. |
0b9bd504 | 359 | FILENAME is the visited file name, BUFNAME is the buffer name, and |
6343ed61 | 360 | MODE is the major mode." |
fa379636 KH |
361 | (let ((case-fold-search nil)) |
362 | (or (and filename | |
363 | (not (string-match desktop-buffers-not-to-save bufname)) | |
364 | (not (string-match desktop-files-not-to-save filename))) | |
365 | (and (eq mode 'dired-mode) | |
366 | (save-excursion | |
367 | (set-buffer (get-buffer bufname)) | |
368 | (not (string-match desktop-files-not-to-save | |
369 | default-directory)))) | |
370 | (and (null filename) | |
371 | (memq mode '(Info-mode rmail-mode)))))) | |
0b9bd504 | 372 | ;; ---------------------------------------------------------------------------- |
6343ed61 RS |
373 | (defun desktop-save (dirname) |
374 | "Save the Desktop file. Parameter DIRNAME specifies where to save desktop." | |
375 | (interactive "DDirectory to save desktop file in: ") | |
fa379636 | 376 | (run-hooks 'desktop-save-hook) |
6343ed61 | 377 | (save-excursion |
0b9bd504 | 378 | (let ((filename (expand-file-name |
6343ed61 | 379 | (concat dirname desktop-basefilename))) |
0b9bd504 RS |
380 | (info (nreverse |
381 | (mapcar | |
6343ed61 RS |
382 | (function (lambda (b) |
383 | (set-buffer b) | |
0b9bd504 | 384 | (list |
6343ed61 RS |
385 | (buffer-file-name) |
386 | (buffer-name) | |
ec4c6f22 RS |
387 | major-mode |
388 | (list ; list explaining minor modes | |
fa379636 | 389 | (not (null auto-fill-function))) |
6343ed61 | 390 | (point) |
de9e2828 | 391 | (list (mark t) mark-active) |
6343ed61 | 392 | buffer-read-only |
ec4c6f22 RS |
393 | (cond ((eq major-mode 'Info-mode) |
394 | (list Info-current-file | |
395 | Info-current-node)) | |
396 | ((eq major-mode 'dired-mode) | |
f4c73d07 RS |
397 | (cons |
398 | (expand-file-name dired-directory) | |
399 | (cdr | |
400 | (nreverse | |
401 | (mapcar | |
402 | (function car) | |
403 | dired-subdir-alist)))))) | |
ec4c6f22 RS |
404 | (let ((locals desktop-locals-to-save) |
405 | (loclist (buffer-local-variables)) | |
406 | (ll)) | |
407 | (while locals | |
408 | (let ((here (assq (car locals) loclist))) | |
409 | (if here | |
410 | (setq ll (cons here ll)) | |
411 | (if (member (car locals) loclist) | |
412 | (setq ll (cons (car locals) ll))))) | |
413 | (setq locals (cdr locals))) | |
414 | ll) | |
6343ed61 RS |
415 | ))) |
416 | (buffer-list)))) | |
417 | (buf (get-buffer-create "*desktop*"))) | |
418 | (set-buffer buf) | |
419 | (erase-buffer) | |
fa379636 | 420 | |
0b9bd504 RS |
421 | (insert desktop-header |
422 | ";; Created " (current-time-string) "\n" | |
fa379636 KH |
423 | ";; Emacs version " emacs-version "\n\n" |
424 | ";; Global section:\n") | |
6343ed61 RS |
425 | (mapcar (function desktop-outvar) desktop-globals-to-save) |
426 | (if (memq 'kill-ring desktop-globals-to-save) | |
0b9bd504 RS |
427 | (insert "(setq kill-ring-yank-pointer (nthcdr " |
428 | (int-to-string | |
6343ed61 RS |
429 | (- (length kill-ring) (length kill-ring-yank-pointer))) |
430 | " kill-ring))\n")) | |
431 | ||
0b9bd504 | 432 | (insert "\n;; Buffer section:\n") |
de9e2828 RS |
433 | (mapcar |
434 | (function (lambda (l) | |
435 | (if (apply 'desktop-save-buffer-p l) | |
436 | (progn | |
437 | (insert desktop-create-buffer-form) | |
438 | (mapcar | |
439 | (function (lambda (e) | |
440 | (insert "\n " | |
441 | (desktop-value-to-string e)))) | |
442 | l) | |
443 | (insert ")\n\n"))))) | |
444 | info) | |
6343ed61 RS |
445 | (setq default-directory dirname) |
446 | (if (file-exists-p filename) (delete-file filename)) | |
447 | (write-region (point-min) (point-max) filename nil 'nomessage))) | |
448 | (setq desktop-dirname dirname)) | |
0b9bd504 | 449 | ;; ---------------------------------------------------------------------------- |
6343ed61 RS |
450 | (defun desktop-remove () |
451 | "Delete the Desktop file and inactivate the desktop system." | |
452 | (interactive) | |
453 | (if desktop-dirname | |
454 | (let ((filename (concat desktop-dirname desktop-basefilename))) | |
fa379636 KH |
455 | (setq desktop-dirname nil) |
456 | (if (file-exists-p filename) | |
457 | (delete-file filename))))) | |
0b9bd504 | 458 | ;; ---------------------------------------------------------------------------- |
6343ed61 | 459 | (defun desktop-read () |
253406b9 RS |
460 | "Read the Desktop file and the files it specifies. |
461 | This is a no-op when Emacs is running in batch mode." | |
6343ed61 | 462 | (interactive) |
253406b9 RS |
463 | (if noninteractive |
464 | nil | |
465 | (let ((dirs '("./" "~/"))) | |
466 | (while (and dirs | |
467 | (not (file-exists-p (expand-file-name | |
468 | desktop-basefilename | |
469 | (car dirs))))) | |
470 | (setq dirs (cdr dirs))) | |
471 | (setq desktop-dirname (and dirs (expand-file-name (car dirs)))) | |
472 | (if desktop-dirname | |
473 | (progn | |
474 | (load (expand-file-name desktop-basefilename desktop-dirname) | |
475 | t t t) | |
476 | (run-hooks 'desktop-delay-hook) | |
477 | (setq desktop-delay-hook nil) | |
478 | (message "Desktop loaded.")) | |
479 | (desktop-clear))))) | |
0b9bd504 | 480 | ;; ---------------------------------------------------------------------------- |
6343ed61 | 481 | (defun desktop-load-default () |
577ed2b2 RS |
482 | "Load the `default' start-up library manually. |
483 | Also inhibit further loading of it. Call this from your `.emacs' file | |
484 | to provide correct modes for autoloaded files." | |
0b9bd504 | 485 | (if (not inhibit-default-init) ; safety check |
6343ed61 RS |
486 | (progn |
487 | (load "default" t t) | |
488 | (setq inhibit-default-init t)))) | |
0b9bd504 RS |
489 | ;; ---------------------------------------------------------------------------- |
490 | ;; Note: the following functions use the dynamic variable binding in Lisp. | |
0b9bd504 | 491 | ;; |
6343ed61 | 492 | (defun desktop-buffer-info () "Load an info file." |
569c754e | 493 | (if (eq 'Info-mode desktop-buffer-major-mode) |
6343ed61 RS |
494 | (progn |
495 | (require 'info) | |
2699e23e | 496 | (Info-find-node (nth 0 desktop-buffer-misc) (nth 1 desktop-buffer-misc)) |
6343ed61 | 497 | t))) |
0b9bd504 | 498 | ;; ---------------------------------------------------------------------------- |
b6105c76 | 499 | (defun desktop-buffer-rmail () "Load an RMAIL file." |
569c754e | 500 | (if (eq 'rmail-mode desktop-buffer-major-mode) |
e36d00d4 | 501 | (condition-case error |
569c754e | 502 | (progn (rmail-input desktop-buffer-file-name) t) |
e2247420 RS |
503 | (file-locked |
504 | (kill-buffer (current-buffer)) | |
505 | 'ignored)))) | |
0b9bd504 | 506 | ;; ---------------------------------------------------------------------------- |
ec4c6f22 | 507 | (defun desktop-buffer-mh () "Load a folder in the mh system." |
569c754e | 508 | (if (eq 'mh-folder-mode desktop-buffer-major-mode) |
ec4c6f22 RS |
509 | (progn |
510 | (require 'mh-e) | |
511 | (mh-find-path) | |
569c754e | 512 | (mh-visit-folder desktop-buffer-name) |
ec4c6f22 RS |
513 | t))) |
514 | ;; ---------------------------------------------------------------------------- | |
6343ed61 | 515 | (defun desktop-buffer-dired () "Load a directory using dired." |
569c754e | 516 | (if (eq 'dired-mode desktop-buffer-major-mode) |
2699e23e | 517 | (if (file-directory-p (file-name-directory (car desktop-buffer-misc))) |
fa379636 | 518 | (progn |
2699e23e RS |
519 | (dired (car desktop-buffer-misc)) |
520 | (mapcar 'dired-insert-subdir (cdr desktop-buffer-misc)) | |
fa379636 | 521 | t) |
2699e23e | 522 | (message "Directory %s no longer exists." (car desktop-buffer-misc)) |
fa379636 KH |
523 | (sit-for 1) |
524 | 'ignored))) | |
0b9bd504 | 525 | ;; ---------------------------------------------------------------------------- |
6343ed61 | 526 | (defun desktop-buffer-file () "Load a file." |
569c754e RS |
527 | (if desktop-buffer-file-name |
528 | (if (or (file-exists-p desktop-buffer-file-name) | |
6343ed61 | 529 | (and desktop-missing-file-warning |
0b9bd504 RS |
530 | (y-or-n-p (format |
531 | "File \"%s\" no longer exists. Re-create? " | |
569c754e RS |
532 | desktop-buffer-file-name)))) |
533 | (progn (find-file desktop-buffer-file-name) t) | |
6343ed61 | 534 | 'ignored))) |
0b9bd504 RS |
535 | ;; ---------------------------------------------------------------------------- |
536 | ;; Create a buffer, load its file, set is mode, ...; called from Desktop file | |
6343ed61 | 537 | ;; only. |
569c754e RS |
538 | (defun desktop-create-buffer (ver desktop-buffer-file-name desktop-buffer-name |
539 | desktop-buffer-major-mode | |
2699e23e | 540 | mim pt mk ro desktop-buffer-misc &optional locals) |
6343ed61 RS |
541 | (let ((hlist desktop-buffer-handlers) |
542 | (result) | |
543 | (handler)) | |
544 | (while (and (not result) hlist) | |
545 | (setq handler (car hlist)) | |
546 | (setq result (funcall handler)) | |
547 | (setq hlist (cdr hlist))) | |
b6105c76 | 548 | (if (eq result t) |
6343ed61 | 549 | (progn |
569c754e RS |
550 | (if (not (equal (buffer-name) desktop-buffer-name)) |
551 | (rename-buffer desktop-buffer-name)) | |
ec4c6f22 | 552 | (auto-fill-mode (if (nth 0 mim) 1 0)) |
6343ed61 | 553 | (goto-char pt) |
0b9bd504 RS |
554 | (if (consp mk) |
555 | (progn | |
556 | (set-mark (car mk)) | |
557 | (setq mark-active (car (cdr mk)))) | |
558 | (set-mark mk)) | |
559 | ;; Never override file system if the file really is read-only marked. | |
560 | (if ro (setq buffer-read-only ro)) | |
ec4c6f22 RS |
561 | (while locals |
562 | (let ((this (car locals))) | |
563 | (if (consp this) | |
564 | ;; an entry of this form `(symbol . value)' | |
565 | (progn | |
566 | (make-local-variable (car this)) | |
567 | (set (car this) (cdr this))) | |
568 | ;; an entry of the form `symbol' | |
569 | (make-local-variable this) | |
570 | (makunbound this))) | |
571 | (setq locals (cdr locals))) | |
6343ed61 | 572 | )))) |
ec4c6f22 RS |
573 | |
574 | ;; Backward compatibility -- update parameters to 205 standards. | |
569c754e RS |
575 | (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name |
576 | desktop-buffer-major-mode | |
2699e23e | 577 | mim pt mk ro tl fc cfs cr desktop-buffer-misc) |
569c754e | 578 | (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name |
2699e23e RS |
579 | desktop-buffer-major-mode (cdr mim) pt mk ro |
580 | desktop-buffer-misc | |
ec4c6f22 RS |
581 | (list (cons 'truncate-lines tl) |
582 | (cons 'fill-column fc) | |
583 | (cons 'case-fold-search cfs) | |
584 | (cons 'case-replace cr) | |
585 | (cons 'overwrite-mode (car mim))))) | |
0b9bd504 | 586 | ;; ---------------------------------------------------------------------------- |
ab1d55ea | 587 | (provide 'desktop) |
6343ed61 RS |
588 | |
589 | ;; desktop.el ends here. |