3404
|
1 ;;; desktop.el --- save partial status of Emacs when killed
|
|
2
|
20526
|
3 ;; Copyright (C) 1993, 1994, 1995, 1997 Free Software Foundation, Inc.
|
3404
|
4
|
|
5 ;; Author: Morten Welinder <terra@diku.dk>
|
27577
|
6 ;; Keywords: convenience
|
5314
|
7 ;; Favourite-brand-of-beer: None, I hate beer.
|
3404
|
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
|
14169
|
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.
|
3404
|
25
|
|
26 ;;; Commentary:
|
|
27
|
4830
|
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
|
5465
|
36 ;; - some local variables
|
3404
|
37
|
21303
|
38 ;; To use this, first put these two lines in the bottom of your .emacs
|
4830
|
39 ;; file (the later the better):
|
|
40 ;;
|
|
41 ;; (desktop-load-default)
|
|
42 ;; (desktop-read)
|
|
43 ;;
|
28719
|
44 ;; Between these two lines you may wish to add something that updates the
|
|
45 ;; variables `desktop-globals-to-save' and/or `desktop-locals-to-save'. If
|
|
46 ;; for instance you want to save the local variable `foobar' for every buffer
|
|
47 ;; in which it is local, you could add the line
|
5465
|
48 ;;
|
|
49 ;; (setq desktop-locals-to-save (cons 'foobar desktop-locals-to-save))
|
|
50 ;;
|
11224
|
51 ;; To avoid saving excessive amounts of data you may also wish to add
|
5465
|
52 ;; something like the following
|
|
53 ;;
|
|
54 ;; (add-hook 'kill-emacs-hook
|
5788
|
55 ;; '(lambda ()
|
5465
|
56 ;; (desktop-truncate search-ring 3)
|
|
57 ;; (desktop-truncate regexp-search-ring 3)))
|
|
58 ;;
|
|
59 ;; which will make sure that no more than three search items are saved. You
|
28719
|
60 ;; must place this line *after* the `(desktop-load-default)' line. See also
|
|
61 ;; the variable `desktop-save-hook'.
|
3404
|
62
|
4830
|
63 ;; Start Emacs in the root directory of your "project". The desktop saver
|
|
64 ;; is inactive by default. You activate it by M-x desktop-save RET. When
|
|
65 ;; you exit the next time the above data will be saved. This ensures that
|
|
66 ;; all the files you were editing will be reloaded the next time you start
|
|
67 ;; Emacs from the same directory and that points will be set where you
|
5465
|
68 ;; left them. If you save a desktop file in your home directory it will
|
5788
|
69 ;; act as a default desktop when you start Emacs from a directory that
|
5465
|
70 ;; doesn't have its own. I never do this, but you may want to.
|
|
71
|
28719
|
72 ;; Some words on minor modes: Most minor modes are controlled by
|
|
73 ;; buffer-local variables, which have a standard save / restore
|
|
74 ;; mechanism. To handle all minor modes, we take the following
|
|
75 ;; approach: (1) check whether the variable name from
|
|
76 ;; `minor-mode-alist' is also a function; and (2) use translation
|
|
77 ;; table `desktop-minor-mode-table' in the case where the two names
|
|
78 ;; are not the same.
|
|
79
|
5465
|
80 ;; By the way: don't use desktop.el to customize Emacs -- the file .emacs
|
|
81 ;; in your home directory is used for that. Saving global default values
|
|
82 ;; for buffers is an example of misuse.
|
|
83
|
4830
|
84 ;; PLEASE NOTE: The kill ring can be saved as specified by the variable
|
|
85 ;; `desktop-globals-to-save' (by default it isn't). This may result in saving
|
|
86 ;; things you did not mean to keep. Use M-x desktop-clear RET.
|
5465
|
87
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
88 ;; Thanks to hetrick@phys.uva.nl (Jim Hetrick) for useful ideas.
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
89 ;; avk@rtsg.mot.com (Andrew V. Klein) for a dired tip.
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
90 ;; chris@tecc.co.uk (Chris Boucher) for a mark tip.
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
91 ;; f89-kam@nada.kth.se (Klas Mellbourn) for a mh-e tip.
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
92 ;; kifer@sbkifer.cs.sunysb.edu (M. Kifer) for a bug hunt.
|
11224
|
93 ;; treese@lcs.mit.edu (Win Treese) for ange-ftp tips.
|
14172
|
94 ;; pot@cnuce.cnr.it (Francesco Potorti`) for misc. tips.
|
4830
|
95 ;; ---------------------------------------------------------------------------
|
|
96 ;; TODO:
|
|
97 ;;
|
|
98 ;; Save window configuration.
|
|
99 ;; Recognize more minor modes.
|
|
100 ;; Save mark rings.
|
|
101 ;; Start-up with buffer-menu???
|
3404
|
102
|
|
103 ;;; Code:
|
|
104
|
5465
|
105 ;; Make the compilation more silent
|
|
106 (eval-when-compile
|
|
107 ;; We use functions from these modules
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
108 ;; We can't (require 'mh-e) since that wants to load something.
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
109 (mapcar 'require '(info dired reporter)))
|
5465
|
110 ;; ----------------------------------------------------------------------------
|
4830
|
111 ;; USER OPTIONS -- settings you might want to play with.
|
|
112 ;; ----------------------------------------------------------------------------
|
17411
|
113
|
|
114 (defgroup desktop nil
|
|
115 "Save status of Emacs when you exit."
|
|
116 :group 'frames)
|
|
117
|
20526
|
118 (defcustom desktop-enable nil
|
|
119 "*Non-nil enable Desktop to save the state of Emacs when you exit."
|
|
120 :group 'desktop
|
|
121 :type 'boolean
|
|
122 :require 'desktop
|
21669
|
123 :initialize 'custom-initialize-default
|
|
124 :version "20.3")
|
20526
|
125
|
|
126 (defcustom desktop-basefilename
|
16037
|
127 (convert-standard-filename ".emacs.desktop")
|
20526
|
128 "File for Emacs desktop, not including the directory name."
|
|
129 :type 'file
|
|
130 :group 'desktop)
|
3404
|
131
|
17411
|
132 (defcustom desktop-missing-file-warning nil
|
10595
|
133 "*If non-nil then desktop warns when a file no longer exists.
|
17411
|
134 Otherwise it simply ignores that file."
|
|
135 :type 'boolean
|
|
136 :group 'desktop)
|
3404
|
137
|
4830
|
138 (defvar desktop-globals-to-save
|
3404
|
139 (list 'desktop-missing-file-warning
|
4830
|
140 ;; Feature: saving kill-ring implies saving kill-ring-yank-pointer
|
5465
|
141 ;; 'kill-ring
|
4830
|
142 'tags-file-name
|
|
143 'tags-table-list
|
5465
|
144 'search-ring
|
|
145 'regexp-search-ring
|
5788
|
146 'register-alist
|
4830
|
147 ;; 'desktop-globals-to-save ; Itself!
|
5314
|
148 )
|
13154
|
149 "List of global variables to save when killing Emacs.
|
|
150 An element may be variable name (a symbol)
|
|
151 or a cons cell of the form (VAR . MAX-SIZE),
|
|
152 which means to truncate VAR's value to at most MAX-SIZE elements
|
|
153 \(if the value is a list) before saving the value.")
|
3404
|
154
|
5465
|
155 (defvar desktop-locals-to-save
|
|
156 (list 'desktop-locals-to-save ; Itself! Think it over.
|
|
157 'truncate-lines
|
|
158 'case-fold-search
|
|
159 'case-replace
|
|
160 'fill-column
|
|
161 'overwrite-mode
|
|
162 'change-log-default-name
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
163 'line-number-mode
|
5465
|
164 )
|
10595
|
165 "List of local variables to save for each buffer.
|
|
166 The variables are saved only when they really are local.")
|
5788
|
167 (make-variable-buffer-local 'desktop-locals-to-save)
|
5465
|
168
|
4830
|
169 ;; We skip .log files because they are normally temporary.
|
14040
|
170 ;; (ftp) files because they require passwords and whatnot.
|
4830
|
171 ;; TAGS files to save time (tags-file-name is saved instead).
|
17411
|
172 (defcustom desktop-buffers-not-to-save
|
5788
|
173 "\\(^nn\\.a[0-9]+\\|\\.log\\|(ftp)\\|^tags\\|^TAGS\\)$"
|
17411
|
174 "Regexp identifying buffers that are to be excluded from saving."
|
|
175 :type 'regexp
|
|
176 :group 'desktop)
|
3404
|
177
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
178 ;; Skip ange-ftp files
|
17411
|
179 (defcustom desktop-files-not-to-save
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
180 "^/[^/:]*:"
|
17411
|
181 "Regexp identifying files whose buffers are to be excluded from saving."
|
|
182 :type 'regexp
|
|
183 :group 'desktop)
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
184
|
17411
|
185 (defcustom desktop-buffer-major-mode nil
|
|
186 "When desktop creates a buffer, this holds the desired Major mode."
|
|
187 :type 'symbol
|
|
188 :group 'desktop)
|
14755
|
189
|
17411
|
190 (defcustom desktop-buffer-file-name nil
|
|
191 "When desktop creates a buffer, this holds the file name to visit."
|
|
192 :type '(choice file (const nil))
|
|
193 :group 'desktop)
|
14755
|
194
|
17411
|
195 (defcustom desktop-buffer-name nil
|
|
196 "When desktop creates a buffer, this holds the desired buffer name."
|
|
197 :type '(choice string (const nil))
|
|
198 :group 'desktop)
|
14755
|
199
|
14756
|
200 (defvar desktop-buffer-misc nil
|
|
201 "When desktop creates a buffer, this holds a list of misc info.
|
|
202 It is used by the `desktop-buffer-handlers' functions.")
|
|
203
|
17411
|
204 (defcustom desktop-buffer-handlers
|
4830
|
205 '(desktop-buffer-dired
|
3404
|
206 desktop-buffer-rmail
|
5465
|
207 desktop-buffer-mh
|
3404
|
208 desktop-buffer-info
|
|
209 desktop-buffer-file)
|
10595
|
210 "*List of functions to call in order to create a buffer.
|
14755
|
211 The functions are called without explicit parameters but can use the
|
|
212 variables `desktop-buffer-major-mode', `desktop-buffer-file-name',
|
|
213 `desktop-buffer-name'.
|
|
214 If one function returns non-nil, no further functions are called.
|
17411
|
215 If the function returns t then the buffer is considered created."
|
|
216 :type '(repeat function)
|
|
217 :group 'desktop)
|
5465
|
218
|
|
219 (defvar desktop-create-buffer-form "(desktop-create-buffer 205"
|
|
220 "Opening of form for creation of new buffers.")
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
221
|
17411
|
222 (defcustom desktop-save-hook nil
|
20526
|
223 "Hook run before desktop saves the state of Emacs.
|
|
224 This is useful for truncating history lists, for example."
|
17411
|
225 :type 'hook
|
|
226 :group 'desktop)
|
20526
|
227
|
28719
|
228 (defcustom desktop-minor-mode-table
|
|
229 '((auto-fill-function auto-fill-mode)
|
|
230 (vc-mode nil))
|
|
231 "Table mapping minor mode variables to minor mode functions.
|
|
232 Each entry has the form (NAME RESTORE-FUNCTION).
|
|
233 NAME is the name of the buffer-local variable indicating that the minor
|
|
234 mode is active. RESTORE-FUNCTION is the function to activate the minor mode.
|
|
235 called. RESTORE-FUNCTION nil means don't try to restore the minor mode.
|
|
236 Only minor modes for which the name of the buffer-local variable
|
|
237 and the name of the minor mode function are different have to added to
|
|
238 this table."
|
|
239 :type 'sexp
|
|
240 :group 'desktop)
|
|
241
|
4830
|
242 ;; ----------------------------------------------------------------------------
|
|
243 (defvar desktop-dirname nil
|
3404
|
244 "The directory in which the current desktop file resides.")
|
|
245
|
|
246 (defconst desktop-header
|
4830
|
247 ";; --------------------------------------------------------------------------
|
|
248 ;; Desktop File for Emacs
|
|
249 ;; --------------------------------------------------------------------------
|
3404
|
250 " "*Header to place in Desktop file.")
|
5788
|
251
|
|
252 (defvar desktop-delay-hook nil
|
|
253 "Hooks run after all buffers are loaded; intended for internal use.")
|
20526
|
254
|
4830
|
255 ;; ----------------------------------------------------------------------------
|
5465
|
256 (defun desktop-truncate (l n)
|
|
257 "Truncate LIST to at most N elements destructively."
|
|
258 (let ((here (nthcdr (1- n) l)))
|
|
259 (if (consp here)
|
5788
|
260 (setcdr here nil))))
|
5465
|
261 ;; ----------------------------------------------------------------------------
|
18581
|
262 (defcustom desktop-clear-preserve-buffers
|
|
263 '("*scratch*" "*Messages*")
|
|
264 "*Buffer names that `desktop-clear' should not delete."
|
|
265 :type '(repeat string)
|
|
266 :group 'desktop)
|
|
267
|
|
268 (defun desktop-clear ()
|
|
269 "Empty the Desktop.
|
|
270 This kills all buffers except for internal ones
|
|
271 and those listed in `desktop-clear-preserve-buffers'."
|
3404
|
272 (interactive)
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
273 (setq kill-ring nil
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
274 kill-ring-yank-pointer nil
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
275 search-ring nil
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
276 search-ring-yank-pointer nil
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
277 regexp-search-ring nil
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
278 regexp-search-ring-yank-pointer nil)
|
18581
|
279 (let ((buffers (buffer-list)))
|
|
280 (while buffers
|
|
281 (or (member (buffer-name (car buffers)) desktop-clear-preserve-buffers)
|
20772
|
282 (null (buffer-name (car buffers)))
|
18581
|
283 ;; Don't kill buffers made for internal purposes.
|
|
284 (and (not (equal (buffer-name (car buffers)) ""))
|
|
285 (eq (aref (buffer-name (car buffers)) 0) ?\ ))
|
|
286 (kill-buffer (car buffers)))
|
|
287 (setq buffers (cdr buffers))))
|
5314
|
288 (delete-other-windows))
|
4830
|
289 ;; ----------------------------------------------------------------------------
|
5788
|
290 (add-hook 'kill-emacs-hook 'desktop-kill)
|
5465
|
291
|
3404
|
292 (defun desktop-kill ()
|
4830
|
293 (if desktop-dirname
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
294 (condition-case err
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
295 (desktop-save desktop-dirname)
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
296 (file-error
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
297 (if (yes-or-no-p "Error while saving the desktop. Quit anyway? ")
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
298 nil
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
299 (signal (car err) (cdr err)))))))
|
4830
|
300 ;; ----------------------------------------------------------------------------
|
14754
|
301 (defun desktop-list* (&rest args)
|
|
302 (if (null (cdr args))
|
|
303 (car args)
|
|
304 (setq args (nreverse args))
|
|
305 (let ((value (cons (nth 1 args) (car args))))
|
|
306 (setq args (cdr (cdr args)))
|
|
307 (while args
|
|
308 (setq value (cons (car args) value))
|
|
309 (setq args (cdr args)))
|
|
310 value)))
|
|
311
|
5788
|
312 (defun desktop-internal-v2s (val)
|
10595
|
313 "Convert VALUE to a pair (QUOTE . TXT); (eval (read TXT)) gives VALUE.
|
|
314 TXT is a string that when read and evaluated yields value.
|
|
315 QUOTE may be `may' (value may be quoted),
|
|
316 `must' (values must be quoted), or nil (value may not be quoted)."
|
5788
|
317 (cond
|
9521
d53ed4fd05ca
(desktop-internal-v2s): Remove all text properties from strings.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
318 ((or (numberp val) (null val) (eq t val))
|
5788
|
319 (cons 'may (prin1-to-string val)))
|
9521
d53ed4fd05ca
(desktop-internal-v2s): Remove all text properties from strings.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
320 ((stringp val)
|
10777
9f4a9820fea1
(desktop-internal-v2s): Don't use format to eliminate text properties.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
321 (let ((copy (copy-sequence val)))
|
9f4a9820fea1
(desktop-internal-v2s): Don't use format to eliminate text properties.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
322 (set-text-properties 0 (length copy) nil copy)
|
9f4a9820fea1
(desktop-internal-v2s): Don't use format to eliminate text properties.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
323 ;; Get rid of text properties because we cannot read them
|
9f4a9820fea1
(desktop-internal-v2s): Don't use format to eliminate text properties.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
324 (cons 'may (prin1-to-string copy))))
|
5788
|
325 ((symbolp val)
|
|
326 (cons 'must (prin1-to-string val)))
|
|
327 ((vectorp val)
|
|
328 (let* ((special nil)
|
|
329 (pass1 (mapcar
|
|
330 (lambda (el)
|
|
331 (let ((res (desktop-internal-v2s el)))
|
|
332 (if (null (car res))
|
|
333 (setq special t))
|
|
334 res))
|
|
335 val)))
|
|
336 (if special
|
|
337 (cons nil (concat "(vector "
|
|
338 (mapconcat (lambda (el)
|
|
339 (if (eq (car el) 'must)
|
|
340 (concat "'" (cdr el))
|
|
341 (cdr el)))
|
|
342 pass1
|
|
343 " ")
|
|
344 ")"))
|
|
345 (cons 'may (concat "[" (mapconcat 'cdr pass1 " ") "]")))))
|
|
346 ((consp val)
|
7200
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
347 (let ((p val)
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
348 newlist
|
14754
|
349 use-list*
|
7200
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
350 anynil)
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
351 (while (consp p)
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
352 (let ((q.txt (desktop-internal-v2s (car p))))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
353 (or anynil (setq anynil (null (car q.txt))))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
354 (setq newlist (cons q.txt newlist)))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
355 (setq p (cdr p)))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
356 (if p
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
357 (let ((last (desktop-internal-v2s p))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
358 (el (car newlist)))
|
14754
|
359 (or anynil (setq anynil (null (car last))))
|
|
360 (or anynil
|
|
361 (setq newlist (cons '(must . ".") newlist)))
|
|
362 (setq use-list* t)
|
|
363 (setq newlist (cons last newlist))))
|
7200
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
364 (setq newlist (nreverse newlist))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
365 (if anynil
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
366 (cons nil
|
14754
|
367 (concat (if use-list* "(desktop-list* " "(list ")
|
7200
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
368 (mapconcat (lambda (el)
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
369 (if (eq (car el) 'must)
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
370 (concat "'" (cdr el))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
371 (cdr el)))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
372 newlist
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
373 " ")
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
374 ")"))
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
375 (cons 'must
|
eabd5e6e95de
(desktop-internal-v2s): Default case fixed to return correct quote flag.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
376 (concat "(" (mapconcat 'cdr newlist " ") ")")))))
|
5788
|
377 ((subrp val)
|
|
378 (cons nil (concat "(symbol-function '"
|
|
379 (substring (prin1-to-string val) 7 -1)
|
|
380 ")")))
|
|
381 ((markerp val)
|
|
382 (let ((pos (prin1-to-string (marker-position val)))
|
|
383 (buf (prin1-to-string (buffer-name (marker-buffer val)))))
|
|
384 (cons nil (concat "(let ((mk (make-marker)))"
|
|
385 " (add-hook 'desktop-delay-hook"
|
|
386 " (list 'lambda '() (list 'set-marker mk "
|
|
387 pos " (get-buffer " buf ")))) mk)"))))
|
|
388 (t ; save as text
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
389 (cons 'may "\"Unprintable entity\""))))
|
5788
|
390
|
5465
|
391 (defun desktop-value-to-string (val)
|
10595
|
392 "Convert VALUE to a string that when read evaluates to the same value.
|
|
393 Not all types of values are supported."
|
5788
|
394 (let* ((print-escape-newlines t)
|
|
395 (float-output-format nil)
|
|
396 (quote.txt (desktop-internal-v2s val))
|
|
397 (quote (car quote.txt))
|
|
398 (txt (cdr quote.txt)))
|
|
399 (if (eq quote 'must)
|
|
400 (concat "'" txt)
|
|
401 txt)))
|
5465
|
402 ;; ----------------------------------------------------------------------------
|
13154
|
403 (defun desktop-outvar (varspec)
|
|
404 "Output a setq statement for variable VAR to the desktop file.
|
|
405 The argument VARSPEC may be the variable name VAR (a symbol),
|
|
406 or a cons cell of the form (VAR . MAX-SIZE),
|
|
407 which means to truncate VAR's value to at most MAX-SIZE elements
|
|
408 \(if the value is a list) before saving the value."
|
|
409 (let (var size)
|
|
410 (if (consp varspec)
|
|
411 (setq var (car varspec) size (cdr varspec))
|
|
412 (setq var varspec))
|
|
413 (if (boundp var)
|
|
414 (progn
|
|
415 (if (and (integerp size)
|
|
416 (> size 0)
|
|
417 (listp (eval var)))
|
28719
|
418 (desktop-truncate (eval var) size))
|
13154
|
419 (insert "(setq "
|
|
420 (symbol-name var)
|
|
421 " "
|
|
422 (desktop-value-to-string (symbol-value var))
|
|
423 ")\n")))))
|
4830
|
424 ;; ----------------------------------------------------------------------------
|
5465
|
425 (defun desktop-save-buffer-p (filename bufname mode &rest dummy)
|
5314
|
426 "Return t if the desktop should record a particular buffer for next startup.
|
4830
|
427 FILENAME is the visited file name, BUFNAME is the buffer name, and
|
3404
|
428 MODE is the major mode."
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
429 (let ((case-fold-search nil))
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
430 (or (and filename
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
431 (not (string-match desktop-buffers-not-to-save bufname))
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
432 (not (string-match desktop-files-not-to-save filename)))
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
433 (and (eq mode 'dired-mode)
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
434 (save-excursion
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
435 (set-buffer (get-buffer bufname))
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
436 (not (string-match desktop-files-not-to-save
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
437 default-directory))))
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
438 (and (null filename)
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
439 (memq mode '(Info-mode rmail-mode))))))
|
4830
|
440 ;; ----------------------------------------------------------------------------
|
3404
|
441 (defun desktop-save (dirname)
|
|
442 "Save the Desktop file. Parameter DIRNAME specifies where to save desktop."
|
|
443 (interactive "DDirectory to save desktop file in: ")
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
444 (run-hooks 'desktop-save-hook)
|
3404
|
445 (save-excursion
|
4830
|
446 (let ((filename (expand-file-name
|
3404
|
447 (concat dirname desktop-basefilename)))
|
4830
|
448 (info (nreverse
|
|
449 (mapcar
|
28719
|
450 (function
|
|
451 (lambda (b)
|
3404
|
452 (set-buffer b)
|
4830
|
453 (list
|
3404
|
454 (buffer-file-name)
|
|
455 (buffer-name)
|
5465
|
456 major-mode
|
28719
|
457 ;; minor modes
|
|
458 (let (ret)
|
|
459 (mapcar
|
|
460 #'(lambda (mim)
|
|
461 (and (symbol-value mim)
|
|
462 (setq ret
|
|
463 (cons (let ((special (assq mim desktop-minor-mode-table)))
|
|
464 (if special
|
|
465 (cadr special)
|
|
466 mim))
|
|
467 ret))))
|
|
468 (mapcar #'car minor-mode-alist))
|
|
469 ret)
|
3404
|
470 (point)
|
5788
|
471 (list (mark t) mark-active)
|
3404
|
472 buffer-read-only
|
5465
|
473 (cond ((eq major-mode 'Info-mode)
|
|
474 (list Info-current-file
|
|
475 Info-current-node))
|
|
476 ((eq major-mode 'dired-mode)
|
11224
|
477 (cons
|
|
478 (expand-file-name dired-directory)
|
|
479 (cdr
|
|
480 (nreverse
|
|
481 (mapcar
|
|
482 (function car)
|
|
483 dired-subdir-alist))))))
|
5465
|
484 (let ((locals desktop-locals-to-save)
|
|
485 (loclist (buffer-local-variables))
|
|
486 (ll))
|
|
487 (while locals
|
|
488 (let ((here (assq (car locals) loclist)))
|
|
489 (if here
|
|
490 (setq ll (cons here ll))
|
|
491 (if (member (car locals) loclist)
|
|
492 (setq ll (cons (car locals) ll)))))
|
|
493 (setq locals (cdr locals)))
|
|
494 ll)
|
3404
|
495 )))
|
|
496 (buffer-list))))
|
|
497 (buf (get-buffer-create "*desktop*")))
|
|
498 (set-buffer buf)
|
|
499 (erase-buffer)
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
500
|
4830
|
501 (insert desktop-header
|
|
502 ";; Created " (current-time-string) "\n"
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
503 ";; Emacs version " emacs-version "\n\n"
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
504 ";; Global section:\n")
|
3404
|
505 (mapcar (function desktop-outvar) desktop-globals-to-save)
|
|
506 (if (memq 'kill-ring desktop-globals-to-save)
|
4830
|
507 (insert "(setq kill-ring-yank-pointer (nthcdr "
|
|
508 (int-to-string
|
3404
|
509 (- (length kill-ring) (length kill-ring-yank-pointer)))
|
|
510 " kill-ring))\n"))
|
|
511
|
4830
|
512 (insert "\n;; Buffer section:\n")
|
5788
|
513 (mapcar
|
|
514 (function (lambda (l)
|
|
515 (if (apply 'desktop-save-buffer-p l)
|
|
516 (progn
|
|
517 (insert desktop-create-buffer-form)
|
|
518 (mapcar
|
|
519 (function (lambda (e)
|
|
520 (insert "\n "
|
|
521 (desktop-value-to-string e))))
|
|
522 l)
|
|
523 (insert ")\n\n")))))
|
|
524 info)
|
3404
|
525 (setq default-directory dirname)
|
|
526 (if (file-exists-p filename) (delete-file filename))
|
|
527 (write-region (point-min) (point-max) filename nil 'nomessage)))
|
|
528 (setq desktop-dirname dirname))
|
4830
|
529 ;; ----------------------------------------------------------------------------
|
3404
|
530 (defun desktop-remove ()
|
|
531 "Delete the Desktop file and inactivate the desktop system."
|
|
532 (interactive)
|
|
533 (if desktop-dirname
|
|
534 (let ((filename (concat desktop-dirname desktop-basefilename)))
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
535 (setq desktop-dirname nil)
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
536 (if (file-exists-p filename)
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
537 (delete-file filename)))))
|
4830
|
538 ;; ----------------------------------------------------------------------------
|
21303
|
539 ;;;###autoload
|
3404
|
540 (defun desktop-read ()
|
14172
|
541 "Read the Desktop file and the files it specifies.
|
|
542 This is a no-op when Emacs is running in batch mode."
|
3404
|
543 (interactive)
|
14172
|
544 (if noninteractive
|
|
545 nil
|
|
546 (let ((dirs '("./" "~/")))
|
|
547 (while (and dirs
|
|
548 (not (file-exists-p (expand-file-name
|
|
549 desktop-basefilename
|
|
550 (car dirs)))))
|
|
551 (setq dirs (cdr dirs)))
|
|
552 (setq desktop-dirname (and dirs (expand-file-name (car dirs))))
|
|
553 (if desktop-dirname
|
|
554 (progn
|
|
555 (load (expand-file-name desktop-basefilename desktop-dirname)
|
|
556 t t t)
|
|
557 (run-hooks 'desktop-delay-hook)
|
|
558 (setq desktop-delay-hook nil)
|
|
559 (message "Desktop loaded."))
|
|
560 (desktop-clear)))))
|
4830
|
561 ;; ----------------------------------------------------------------------------
|
21303
|
562 ;;;###autoload
|
3404
|
563 (defun desktop-load-default ()
|
10595
|
564 "Load the `default' start-up library manually.
|
|
565 Also inhibit further loading of it. Call this from your `.emacs' file
|
|
566 to provide correct modes for autoloaded files."
|
4830
|
567 (if (not inhibit-default-init) ; safety check
|
3404
|
568 (progn
|
|
569 (load "default" t t)
|
|
570 (setq inhibit-default-init t))))
|
4830
|
571 ;; ----------------------------------------------------------------------------
|
|
572 ;; Note: the following functions use the dynamic variable binding in Lisp.
|
|
573 ;;
|
3404
|
574 (defun desktop-buffer-info () "Load an info file."
|
14755
|
575 (if (eq 'Info-mode desktop-buffer-major-mode)
|
3404
|
576 (progn
|
|
577 (require 'info)
|
14756
|
578 (Info-find-node (nth 0 desktop-buffer-misc) (nth 1 desktop-buffer-misc))
|
21316
|
579 (current-buffer))))
|
4830
|
580 ;; ----------------------------------------------------------------------------
|
5314
|
581 (defun desktop-buffer-rmail () "Load an RMAIL file."
|
14755
|
582 (if (eq 'rmail-mode desktop-buffer-major-mode)
|
8152
|
583 (condition-case error
|
21316
|
584 (progn (rmail-input desktop-buffer-file-name)
|
|
585 (if (eq major-mode 'rmail-mode)
|
|
586 (current-buffer)
|
|
587 rmail-buffer))
|
9521
d53ed4fd05ca
(desktop-internal-v2s): Remove all text properties from strings.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
588 (file-locked
|
d53ed4fd05ca
(desktop-internal-v2s): Remove all text properties from strings.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
589 (kill-buffer (current-buffer))
|
d53ed4fd05ca
(desktop-internal-v2s): Remove all text properties from strings.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
590 'ignored))))
|
4830
|
591 ;; ----------------------------------------------------------------------------
|
5465
|
592 (defun desktop-buffer-mh () "Load a folder in the mh system."
|
14755
|
593 (if (eq 'mh-folder-mode desktop-buffer-major-mode)
|
5465
|
594 (progn
|
|
595 (require 'mh-e)
|
|
596 (mh-find-path)
|
21316
|
597 (mh-visit-folder desktop-buffer-name)
|
|
598 (current-buffer))))
|
5465
|
599 ;; ----------------------------------------------------------------------------
|
3404
|
600 (defun desktop-buffer-dired () "Load a directory using dired."
|
14755
|
601 (if (eq 'dired-mode desktop-buffer-major-mode)
|
14756
|
602 (if (file-directory-p (file-name-directory (car desktop-buffer-misc)))
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
603 (progn
|
21316
|
604 (dired (car desktop-buffer-misc))
|
23541
|
605 (mapcar 'dired-maybe-insert-subdir (cdr desktop-buffer-misc))
|
21316
|
606 (current-buffer))
|
14756
|
607 (message "Directory %s no longer exists." (car desktop-buffer-misc))
|
7240
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
608 (sit-for 1)
|
195e64dad1eb
(desktop-files-not-to-save): New variable to exclude certain files -- magic
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
609 'ignored)))
|
4830
|
610 ;; ----------------------------------------------------------------------------
|
3404
|
611 (defun desktop-buffer-file () "Load a file."
|
14755
|
612 (if desktop-buffer-file-name
|
|
613 (if (or (file-exists-p desktop-buffer-file-name)
|
3404
|
614 (and desktop-missing-file-warning
|
4830
|
615 (y-or-n-p (format
|
|
616 "File \"%s\" no longer exists. Re-create? "
|
14755
|
617 desktop-buffer-file-name))))
|
21316
|
618 (progn (find-file desktop-buffer-file-name) (current-buffer))
|
3404
|
619 'ignored)))
|
4830
|
620 ;; ----------------------------------------------------------------------------
|
|
621 ;; Create a buffer, load its file, set is mode, ...; called from Desktop file
|
3404
|
622 ;; only.
|
14755
|
623 (defun desktop-create-buffer (ver desktop-buffer-file-name desktop-buffer-name
|
|
624 desktop-buffer-major-mode
|
28719
|
625 mim pt mk ro desktop-buffer-misc
|
|
626 &optional locals)
|
3404
|
627 (let ((hlist desktop-buffer-handlers)
|
|
628 (result)
|
|
629 (handler))
|
|
630 (while (and (not result) hlist)
|
|
631 (setq handler (car hlist))
|
|
632 (setq result (funcall handler))
|
|
633 (setq hlist (cdr hlist)))
|
21316
|
634 (when (bufferp result)
|
|
635 (set-buffer result)
|
|
636 (if (not (equal (buffer-name) desktop-buffer-name))
|
|
637 (rename-buffer desktop-buffer-name))
|
28719
|
638 ;; minor modes
|
|
639 (cond ((equal '(t) mim) (auto-fill-mode 1)) ; backwards compatible
|
|
640 ((equal '(nil) mim) (auto-fill-mode 0))
|
|
641 (t (mapcar #'(lambda (minor-mode)
|
|
642 (when minor-mode (funcall minor-mode 1)))
|
|
643 mim)))
|
23535
28c62eea9ae7
(desktop-create-buffer): Handle old (broken) minor mode support.
Thien-Thi Nguyen <ttn@gnuvola.org>
diff
changeset
|
644 (goto-char pt)
|
21316
|
645 (if (consp mk)
|
|
646 (progn
|
|
647 (set-mark (car mk))
|
|
648 (setq mark-active (car (cdr mk))))
|
|
649 (set-mark mk))
|
|
650 ;; Never override file system if the file really is read-only marked.
|
|
651 (if ro (setq buffer-read-only ro))
|
|
652 (while locals
|
|
653 (let ((this (car locals)))
|
|
654 (if (consp this)
|
|
655 ;; an entry of this form `(symbol . value)'
|
4830
|
656 (progn
|
21316
|
657 (make-local-variable (car this))
|
|
658 (set (car this) (cdr this)))
|
|
659 ;; an entry of the form `symbol'
|
|
660 (make-local-variable this)
|
|
661 (makunbound this)))
|
|
662 (setq locals (cdr locals))))))
|
5465
|
663
|
|
664 ;; Backward compatibility -- update parameters to 205 standards.
|
14755
|
665 (defun desktop-buffer (desktop-buffer-file-name desktop-buffer-name
|
|
666 desktop-buffer-major-mode
|
14756
|
667 mim pt mk ro tl fc cfs cr desktop-buffer-misc)
|
14755
|
668 (desktop-create-buffer 205 desktop-buffer-file-name desktop-buffer-name
|
14756
|
669 desktop-buffer-major-mode (cdr mim) pt mk ro
|
|
670 desktop-buffer-misc
|
5465
|
671 (list (cons 'truncate-lines tl)
|
|
672 (cons 'fill-column fc)
|
|
673 (cons 'case-fold-search cfs)
|
|
674 (cons 'case-replace cr)
|
|
675 (cons 'overwrite-mode (car mim)))))
|
4830
|
676 ;; ----------------------------------------------------------------------------
|
20526
|
677
|
|
678 ;; If the user set desktop-enable to t with Custom,
|
|
679 ;; do the rest of what it takes to use desktop,
|
|
680 ;; but do it after finishing loading the init file.
|
|
681 (add-hook 'after-init-hook
|
|
682 '(lambda ()
|
|
683 (when desktop-enable
|
|
684 (desktop-load-default)
|
|
685 (desktop-read))))
|
|
686
|
3405
|
687 (provide 'desktop)
|
3404
|
688
|
|
689 ;; desktop.el ends here.
|