209
|
1 ;;; files.el --- file input and output commands for XEmacs.
|
|
2
|
|
3 ;; Copyright (C) 1985-1987, 1992-1995, 1997 Free Software Foundation, Inc.
|
|
4 ;; Copyright (C) 1995 Sun Microsystems.
|
|
5
|
|
6 ;; Maintainer: XEmacs Development Team
|
|
7 ;; Keywords: extensions, dumped
|
|
8
|
|
9 ;; This file is part of XEmacs.
|
|
10
|
|
11 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
12 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
19 ;; General Public License for more details.
|
|
20
|
|
21 ;; You should have received a copy of the GNU General Public License
|
|
22 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
23 ;; Software Foundation, Inc. 59 Temple Place - Suite 330, Boston, MA
|
|
24 ;; 02111-1307, USA.
|
|
25
|
|
26 ;;; Synched up with: FSF 19.34 [Partial].
|
|
27 ;;; Warning: Merging this file is tough. Beware.
|
|
28
|
|
29 ;;; Commentary:
|
|
30
|
|
31 ;; This file is dumped with XEmacs.
|
|
32
|
|
33 ;; Defines most of XEmacs's file- and directory-handling functions,
|
|
34 ;; including basic file visiting, backup generation, link handling,
|
|
35 ;; ITS-id version control, load- and write-hook handling, and the like.
|
|
36
|
|
37 ;;; Code:
|
|
38
|
|
39 ;; XEmacs: Avoid compilation warnings.
|
|
40 (defvar coding-system-for-read)
|
|
41 (defvar buffer-file-coding-system)
|
|
42
|
227
|
43 (defgroup files nil
|
|
44 "Support editing files."
|
|
45 :group 'emacs)
|
|
46
|
209
|
47 (defgroup backup nil
|
|
48 "Backups of edited data files."
|
|
49 :group 'files)
|
|
50
|
|
51 (defgroup find-file nil
|
|
52 "Finding and editing files."
|
|
53 :group 'files)
|
|
54
|
|
55
|
|
56 ;; XEmacs: In buffer.c
|
|
57 ;(defconst delete-auto-save-files t
|
|
58 ; "*Non-nil means delete auto-save file when a buffer is saved or killed.")
|
|
59
|
|
60 ;; FSF has automount-dir-prefix. Our directory-abbrev-alist is more general.
|
|
61 ;; note: tmp_mnt bogosity conversion is established in paths.el.
|
|
62 (defcustom directory-abbrev-alist nil
|
|
63 "*Alist of abbreviations for file directories.
|
|
64 A list of elements of the form (FROM . TO), each meaning to replace
|
|
65 FROM with TO when it appears in a directory name.
|
|
66 This replacement is done when setting up the default directory of a
|
|
67 newly visited file. *Every* FROM string should start with \\\\` or ^.
|
|
68
|
|
69 Use this feature when you have directories which you normally refer to
|
|
70 via absolute symbolic links or to eliminate automounter mount points
|
|
71 from the beginning of your filenames. Make TO the name of the link,
|
|
72 and FROM the name it is linked to."
|
|
73 :type '(repeat (cons :format "%v"
|
|
74 :value ("\\`" . "")
|
|
75 (regexp :tag "From")
|
|
76 (regexp :tag "To")))
|
|
77 :group 'find-file)
|
|
78
|
|
79 ;;; Turn off backup files on VMS since it has version numbers.
|
|
80 (defcustom make-backup-files (not (eq system-type 'vax-vms))
|
|
81 "*Non-nil means make a backup of a file the first time it is saved.
|
|
82 This can be done by renaming the file or by copying.
|
|
83
|
|
84 Renaming means that XEmacs renames the existing file so that it is a
|
|
85 backup file, then writes the buffer into a new file. Any other names
|
|
86 that the old file had will now refer to the backup file. The new file
|
|
87 is owned by you and its group is defaulted.
|
|
88
|
|
89 Copying means that XEmacs copies the existing file into the backup
|
|
90 file, then writes the buffer on top of the existing file. Any other
|
|
91 names that the old file had will now refer to the new (edited) file.
|
|
92 The file's owner and group are unchanged.
|
|
93
|
|
94 The choice of renaming or copying is controlled by the variables
|
|
95 `backup-by-copying', `backup-by-copying-when-linked' and
|
|
96 `backup-by-copying-when-mismatch'. See also `backup-inhibited'."
|
|
97 :type 'boolean
|
|
98 :group 'backup)
|
|
99
|
|
100 ;; Do this so that local variables based on the file name
|
|
101 ;; are not overridden by the major mode.
|
|
102 (defvar backup-inhibited nil
|
|
103 "Non-nil means don't make a backup, regardless of the other parameters.
|
|
104 This variable is intended for use by making it local to a buffer.
|
|
105 But it is local only if you make it local.")
|
|
106 (put 'backup-inhibited 'permanent-local t)
|
|
107
|
|
108 (defcustom backup-by-copying nil
|
|
109 "*Non-nil means always use copying to create backup files.
|
|
110 See documentation of variable `make-backup-files'."
|
|
111 :type 'boolean
|
|
112 :group 'backup)
|
|
113
|
|
114 (defcustom backup-by-copying-when-linked nil
|
|
115 "*Non-nil means use copying to create backups for files with multiple names.
|
|
116 This causes the alternate names to refer to the latest version as edited.
|
|
117 This variable is relevant only if `backup-by-copying' is nil."
|
|
118 :type 'boolean
|
|
119 :group 'backup)
|
|
120
|
|
121 (defcustom backup-by-copying-when-mismatch nil
|
|
122 "*Non-nil means create backups by copying if this preserves owner or group.
|
|
123 Renaming may still be used (subject to control of other variables)
|
|
124 when it would not result in changing the owner or group of the file;
|
|
125 that is, for files which are owned by you and whose group matches
|
|
126 the default for a new file created there by you.
|
|
127 This variable is relevant only if `backup-by-copying' is nil."
|
|
128 :type 'boolean
|
|
129 :group 'backup)
|
|
130
|
|
131 (defvar backup-enable-predicate
|
|
132 '(lambda (name)
|
265
|
133 (not (or (string-match "^/tmp/" name)
|
263
|
134 (let ((tmpdir (temp-directory)))
|
|
135 (and tmpdir
|
265
|
136 (string-match (concat "^" (regexp-quote tmpdir) "/")
|
|
137 tmpdir))))))
|
209
|
138 "Predicate that looks at a file name and decides whether to make backups.
|
|
139 Called with an absolute file name as argument, it returns t to enable backup.")
|
|
140
|
|
141 (defcustom buffer-offer-save nil
|
|
142 "*Non-nil in a buffer means offer to save the buffer on exit
|
|
143 even if the buffer is not visiting a file.
|
|
144 Automatically local in all buffers."
|
|
145 :type 'boolean
|
|
146 :group 'find-file)
|
|
147 (make-variable-buffer-local 'buffer-offer-save)
|
|
148
|
|
149 ;; FSF uses normal defconst
|
|
150 (defvaralias 'find-file-visit-truename 'find-file-use-truenames)
|
|
151 (defvaralias 'find-file-existing-other-name 'find-file-compare-truenames)
|
|
152
|
|
153 (defcustom revert-without-query nil
|
|
154 "*Specify which files should be reverted without query.
|
|
155 The value is a list of regular expressions.
|
|
156 If the file name matches one of these regular expressions,
|
|
157 then `revert-buffer' reverts the file without querying
|
|
158 if the file has changed on disk and you have not edited the buffer."
|
|
159 :type '(repeat (regexp ""))
|
|
160 :group 'find-file)
|
|
161
|
|
162 (defvar buffer-file-number nil
|
|
163 "The device number and file number of the file visited in the current buffer.
|
|
164 The value is a list of the form (FILENUM DEVNUM).
|
|
165 This pair of numbers uniquely identifies the file.
|
|
166 If the buffer is visiting a new file, the value is nil.")
|
|
167 (make-variable-buffer-local 'buffer-file-number)
|
|
168 (put 'buffer-file-number 'permanent-local t)
|
|
169
|
|
170 (defvar buffer-file-numbers-unique (not (memq system-type '(windows-nt)))
|
|
171 "Non-nil means that buffer-file-number uniquely identifies files.")
|
|
172
|
|
173 (defcustom file-precious-flag nil
|
|
174 "*Non-nil means protect against I/O errors while saving files.
|
|
175 Some modes set this non-nil in particular buffers.
|
|
176
|
|
177 This feature works by writing the new contents into a temporary file
|
|
178 and then renaming the temporary file to replace the original.
|
|
179 In this way, any I/O error in writing leaves the original untouched,
|
|
180 and there is never any instant where the file is nonexistent.
|
|
181
|
|
182 Note that this feature forces backups to be made by copying.
|
|
183 Yet, at the same time, saving a precious file
|
|
184 breaks any hard links between it and other files."
|
|
185 :type 'boolean
|
|
186 :group 'backup)
|
|
187
|
|
188 (defcustom version-control nil
|
|
189 "*Control use of version numbers for backup files.
|
|
190 t means make numeric backup versions unconditionally.
|
|
191 nil means make them for files that have some already.
|
|
192 `never' means do not make them."
|
|
193 :type 'boolean
|
|
194 :group 'backup
|
|
195 :group 'vc)
|
|
196
|
|
197 ;; This is now defined in efs.
|
|
198 ;(defvar dired-kept-versions 2
|
|
199 ; "*When cleaning directory, number of versions to keep.")
|
|
200
|
|
201 (defcustom delete-old-versions nil
|
|
202 "*If t, delete excess backup versions silently.
|
|
203 If nil, ask confirmation. Any other value prevents any trimming."
|
|
204 :type '(choice (const :tag "Delete" t)
|
|
205 (const :tag "Ask" nil)
|
|
206 (sexp :tag "Leave" :format "%t\n" other))
|
|
207 :group 'backup)
|
|
208
|
|
209 (defcustom kept-old-versions 2
|
|
210 "*Number of oldest versions to keep when a new numbered backup is made."
|
|
211 :type 'integer
|
|
212 :group 'backup)
|
|
213
|
|
214 (defcustom kept-new-versions 2
|
|
215 "*Number of newest versions to keep when a new numbered backup is made.
|
|
216 Includes the new backup. Must be > 0"
|
|
217 :type 'integer
|
|
218 :group 'backup)
|
|
219
|
|
220 (defcustom require-final-newline nil
|
|
221 "*Value of t says silently ensure a file ends in a newline when it is saved.
|
|
222 Non-nil but not t says ask user whether to add a newline when there isn't one.
|
|
223 nil means don't add newlines."
|
|
224 :type '(choice (const :tag "Off" nil)
|
|
225 (const :tag "Add" t)
|
|
226 (sexp :tag "Ask" :format "%t\n" ask))
|
|
227 :group 'editing-basics)
|
|
228
|
|
229 (defcustom auto-save-default t
|
|
230 "*Non-nil says by default do auto-saving of every file-visiting buffer."
|
|
231 :type 'boolean
|
|
232 :group 'auto-save)
|
|
233
|
|
234 (defcustom auto-save-visited-file-name nil
|
|
235 "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
|
|
236 Normally auto-save files are written under other names."
|
|
237 :type 'boolean
|
|
238 :group 'auto-save)
|
|
239
|
|
240 (defcustom save-abbrevs nil
|
|
241 "*Non-nil means save word abbrevs too when files are saved.
|
|
242 Loading an abbrev file sets this to t."
|
|
243 :type 'boolean
|
|
244 :group 'abbrev)
|
|
245
|
|
246 (defcustom find-file-run-dired t
|
|
247 "*Non-nil says run dired if `find-file' is given the name of a directory."
|
|
248 :type 'boolean
|
|
249 :group 'find-file)
|
|
250
|
|
251 ;;;It is not useful to make this a local variable.
|
|
252 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
|
|
253 (defvar find-file-not-found-hooks nil
|
|
254 "List of functions to be called for `find-file' on nonexistent file.
|
|
255 These functions are called as soon as the error is detected.
|
|
256 `buffer-file-name' is already set up.
|
|
257 The functions are called in the order given until one of them returns non-nil.")
|
|
258
|
|
259 ;;;It is not useful to make this a local variable.
|
|
260 ;;;(put 'find-file-hooks 'permanent-local t)
|
|
261 (defvar find-file-hooks nil
|
|
262 "List of functions to be called after a buffer is loaded from a file.
|
|
263 The buffer's local variables (if any) will have been processed before the
|
|
264 functions are called.")
|
|
265
|
|
266 (defvar write-file-hooks nil
|
|
267 "List of functions to be called before writing out a buffer to a file.
|
|
268 If one of them returns non-nil, the file is considered already written
|
|
269 and the rest are not called.
|
|
270 These hooks are considered to pertain to the visited file.
|
|
271 So this list is cleared if you change the visited file name.
|
|
272 See also `write-contents-hooks' and `continue-save-buffer'.")
|
|
273 ;;; However, in case someone does make it local...
|
|
274 (put 'write-file-hooks 'permanent-local t)
|
|
275
|
|
276 (defvar local-write-file-hooks nil
|
|
277 "Just like `write-file-hooks', except intended for per-buffer use.
|
|
278 The functions in this list are called before the ones in
|
|
279 `write-file-hooks'.
|
|
280
|
|
281 This variable is meant to be used for hooks that have to do with a
|
|
282 particular visited file. Therefore, it is a permanent local, so that
|
|
283 changing the major mode does not clear it. However, calling
|
|
284 `set-visited-file-name' does clear it.")
|
|
285 (make-variable-buffer-local 'local-write-file-hooks)
|
|
286 (put 'local-write-file-hooks 'permanent-local t)
|
|
287
|
|
288
|
|
289 ;; #### think about this (added by Sun).
|
|
290 (put 'after-set-visited-file-name-hooks 'permanent-local t)
|
|
291 (defvar after-set-visited-file-name-hooks nil
|
|
292 "List of functions to be called after \\[set-visited-file-name]
|
|
293 or during \\[write-file].
|
|
294 You can use this hook to restore local values of write-file-hooks,
|
|
295 after-save-hook, and revert-buffer-function, which pertain
|
|
296 to a specific file and therefore are normally killed by a rename.
|
|
297 Put hooks pertaining to the buffer contents on write-contents-hooks
|
|
298 and revert-buffer-insert-file-contents-function.")
|
|
299
|
|
300 (defvar write-contents-hooks nil
|
|
301 "List of functions to be called before writing out a buffer to a file.
|
|
302 If one of them returns non-nil, the file is considered already written
|
|
303 and the rest are not called.
|
|
304 These hooks are considered to pertain to the buffer's contents,
|
|
305 not to the particular visited file; thus, `set-visited-file-name' does
|
|
306 not clear this variable, but changing the major mode does clear it.
|
|
307 See also `write-file-hooks' and `continue-save-buffer'.")
|
|
308
|
|
309 ;; XEmacs addition
|
|
310 ;; Energize needed this to hook into save-buffer at a lower level; we need
|
|
311 ;; to provide a new output method, but don't want to have to duplicate all
|
|
312 ;; of the backup file and file modes logic.that does not occur if one uses
|
|
313 ;; a write-file-hook which returns non-nil.
|
|
314 (put 'write-file-data-hooks 'permanent-local t)
|
|
315 (defvar write-file-data-hooks nil
|
|
316 "List of functions to be called to put the bytes on disk.
|
|
317 These functions receive the name of the file to write to as argument.
|
|
318 The default behavior is to call
|
|
319 (write-region (point-min) (point-max) filename nil t)
|
|
320 If one of them returns non-nil, the file is considered already written
|
|
321 and the rest are not called.
|
|
322 These hooks are considered to pertain to the visited file.
|
|
323 So this list is cleared if you change the visited file name.
|
|
324 See also `write-file-hooks'.")
|
|
325
|
|
326 (defcustom enable-local-variables t
|
|
327 "*Control use of local-variables lists in files you visit.
|
|
328 The value can be t, nil or something else.
|
|
329 A value of t means local-variables lists are obeyed;
|
|
330 nil means they are ignored; anything else means query.
|
|
331
|
|
332 The command \\[normal-mode] always obeys local-variables lists
|
|
333 and ignores this variable."
|
|
334 :type '(choice (const :tag "Obey" t)
|
|
335 (const :tag "Ignore" nil)
|
|
336 (sexp :tag "Query" :format "%t\n" other))
|
|
337 :group 'find-file)
|
|
338
|
|
339 (defcustom enable-local-eval 'maybe
|
|
340 "*Control processing of the \"variable\" `eval' in a file's local variables.
|
|
341 The value can be t, nil or something else.
|
|
342 A value of t means obey `eval' variables;
|
|
343 nil means ignore them; anything else means query.
|
|
344
|
|
345 The command \\[normal-mode] always obeys local-variables lists
|
|
346 and ignores this variable."
|
|
347 :type '(choice (const :tag "Obey" t)
|
|
348 (const :tag "Ignore" nil)
|
|
349 (sexp :tag "Query" :format "%t\n" other))
|
|
350 :group 'find-file)
|
|
351
|
|
352 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
|
|
353 (or (fboundp 'lock-buffer)
|
|
354 (defalias 'lock-buffer 'ignore))
|
|
355 (or (fboundp 'unlock-buffer)
|
|
356 (defalias 'unlock-buffer 'ignore))
|
|
357
|
|
358 ;;FSFmacs bastardized ange-ftp cruft
|
|
359 ;; This hook function provides support for ange-ftp host name
|
|
360 ;; completion. It runs the usual ange-ftp hook, but only for
|
|
361 ;; completion operations. Having this here avoids the need
|
|
362 ;; to load ange-ftp when it's not really in use.
|
|
363 ;(defun ange-ftp-completion-hook-function (op &rest args)
|
|
364 ; (if (memq op '(file-name-completion file-name-all-completions))
|
|
365 ; (apply 'ange-ftp-hook-function op args)
|
|
366 ; (let ((inhibit-file-name-handlers
|
|
367 ; (cons 'ange-ftp-completion-hook-function
|
|
368 ; (and (eq inhibit-file-name-operation op)
|
|
369 ; inhibit-file-name-handlers)))
|
|
370 ; (inhibit-file-name-operation op))
|
|
371 ; (apply op args))
|
|
372
|
|
373 (defun convert-standard-filename (filename)
|
|
374 "Convert a standard file's name to something suitable for the current OS.
|
|
375 This function's standard definition is trivial; it just returns the argument.
|
|
376 However, on some systems, the function is redefined
|
|
377 with a definition that really does change some file names."
|
|
378 filename)
|
|
379
|
|
380 (defun pwd ()
|
|
381 "Show the current default directory."
|
|
382 (interactive nil)
|
|
383 (message "Directory %s" default-directory))
|
|
384
|
|
385 (defvar cd-path nil
|
|
386 "Value of the CDPATH environment variable, as a list.
|
|
387 Not actually set up until the first time you use it.")
|
|
388
|
265
|
389 (defvar cdpath-previous nil
|
|
390 "Prior value of the CDPATH environment variable.")
|
|
391
|
209
|
392 (defvar path-separator ":"
|
|
393 "Character used to separate concatenated paths.")
|
|
394
|
267
|
395 (defun parse-colon-path (cd-path)
|
|
396 "Explode a colon-separated list of paths into a string list.
|
|
397 This will be moved into \"C\" when decode_path is no longer called so
|
|
398 early in XEmacs initialization."
|
|
399 (and cd-path
|
|
400 (let (cd-list (cd-start 0) cd-colon)
|
|
401 (setq cd-path (concat cd-path path-separator))
|
|
402 (while (setq cd-colon (string-match path-separator cd-path cd-start))
|
|
403 (setq cd-list
|
|
404 (nconc cd-list
|
|
405 (list (if (= cd-start cd-colon)
|
|
406 nil
|
|
407 (substitute-in-file-name
|
|
408 (file-name-as-directory
|
|
409 (substring cd-path cd-start cd-colon)))))))
|
|
410 (setq cd-start (+ cd-colon 1)))
|
|
411 cd-list)))
|
209
|
412
|
|
413 (defun cd-absolute (dir)
|
|
414 "Change current directory to given absolute file name DIR."
|
|
415 ;; Put the name into directory syntax now,
|
|
416 ;; because otherwise expand-file-name may give some bad results.
|
|
417 (if (not (eq system-type 'vax-vms))
|
|
418 (setq dir (file-name-as-directory dir)))
|
|
419 ;; XEmacs change: stig@hackvan.com
|
|
420 (if find-file-use-truenames
|
|
421 (setq dir (file-truename dir)))
|
|
422 (setq dir (abbreviate-file-name (expand-file-name dir)))
|
|
423 (cond ((not (file-directory-p dir))
|
|
424 (error "%s is not a directory" dir))
|
|
425 ;; this breaks ange-ftp, which doesn't (can't?) overload `file-executable-p'.
|
|
426 ;;((not (file-executable-p dir))
|
|
427 ;; (error "Cannot cd to %s: Permission denied" dir))
|
|
428 (t
|
|
429 (setq default-directory dir))))
|
|
430
|
|
431 (defun cd (dir)
|
|
432 "Make DIR become the current buffer's default directory.
|
|
433 If your environment includes a `CDPATH' variable, try each one of that
|
|
434 colon-separated list of directories when resolving a relative directory name."
|
|
435 (interactive
|
|
436 ;; XEmacs change? (read-file-name => read-directory-name)
|
|
437 (list (read-directory-name "Change default directory: "
|
|
438 default-directory default-directory
|
|
439 (and (member cd-path '(nil ("./")))
|
|
440 (null (getenv "CDPATH"))))))
|
|
441 (if (file-name-absolute-p dir)
|
|
442 (cd-absolute (expand-file-name dir))
|
|
443 ;; XEmacs
|
265
|
444 (unless (and cd-path (equal (getenv "CDPATH") cdpath-previous))
|
|
445 ;;#### Unix-specific
|
|
446 (let ((trypath (parse-colon-path
|
|
447 (setq cdpath-previous (getenv "CDPATH")))))
|
|
448 (setq cd-path (or trypath (list "./")))))
|
209
|
449 (or (catch 'found
|
|
450 (mapcar #'(lambda (x)
|
|
451 (let ((f (expand-file-name (concat x dir))))
|
|
452 (if (file-directory-p f)
|
|
453 (progn
|
|
454 (cd-absolute f)
|
|
455 (throw 'found t)))))
|
|
456 cd-path)
|
|
457 nil)
|
|
458 ;; jwz: give a better error message to those of us with the
|
|
459 ;; good taste not to use a kludge like $CDPATH.
|
|
460 (if (equal cd-path '("./"))
|
|
461 (error "No such directory: %s" (expand-file-name dir))
|
|
462 (error "Directory not found in $CDPATH: %s" dir)))))
|
|
463
|
|
464 (defun load-file (file)
|
|
465 "Load the Lisp file named FILE."
|
|
466 (interactive "fLoad file: ")
|
|
467 (load (expand-file-name file) nil nil t))
|
|
468
|
|
469 ; We now dump utils/lib-complete.el which has improved versions of this.
|
|
470 ;(defun load-library (library)
|
|
471 ; "Load the library named LIBRARY.
|
|
472 ;This is an interface to the function `load'."
|
|
473 ; (interactive "sLoad library: ")
|
|
474 ; (load library))
|
|
475 ;
|
|
476 ;(defun find-library (library)
|
|
477 ; "Find the library of Lisp code named LIBRARY.
|
|
478 ;This searches `load-path' for a file named either \"LIBRARY\" or \"LIBRARY.el\"."
|
|
479 ; (interactive "sFind library file: ")
|
|
480 ; (let ((f (locate-file library load-path ":.el:")))
|
|
481 ; (if f
|
|
482 ; (find-file f)
|
|
483 ; (error "Couldn't locate library %s" library))))
|
|
484
|
|
485 (defun file-local-copy (file &optional buffer)
|
|
486 "Copy the file FILE into a temporary file on this machine.
|
|
487 Returns the name of the local copy, or nil, if FILE is directly
|
|
488 accessible."
|
|
489 (let ((handler (find-file-name-handler file 'file-local-copy)))
|
|
490 (if handler
|
|
491 (funcall handler 'file-local-copy file)
|
|
492 nil)))
|
|
493
|
|
494 ;; XEmacs change block
|
|
495 ; We have this in C and use the realpath() system call.
|
|
496
|
|
497 ;(defun file-truename (filename &optional counter prev-dirs)
|
|
498 ; "Return the truename of FILENAME, which should be absolute.
|
|
499 ;The truename of a file name is found by chasing symbolic links
|
|
500 ;both at the level of the file and at the level of the directories
|
|
501 ;containing it, until no links are left at any level.
|
|
502 ;
|
|
503 ;The arguments COUNTER and PREV-DIRS are used only in recursive calls.
|
|
504 ;Do not specify them in other calls."
|
|
505 ; ;; COUNTER can be a cons cell whose car is the count of how many more links
|
|
506 ; ;; to chase before getting an error.
|
|
507 ; ;; PREV-DIRS can be a cons cell whose car is an alist
|
|
508 ; ;; of truenames we've just recently computed.
|
|
509 ; ;; The last test looks dubious, maybe `+' is meant here? --simon.
|
|
510 ; (if (or (string= filename "") (string= filename "~")
|
|
511 ; (and (string= (substring filename 0 1) "~")
|
|
512 ; (string-match "~[^/]*" filename)))
|
|
513 ; (progn
|
|
514 ; (setq filename (expand-file-name filename))
|
|
515 ; (if (string= filename "")
|
|
516 ; (setq filename "/"))))
|
|
517 ; (or counter (setq counter (list 100)))
|
|
518 ; (let (done
|
|
519 ; ;; For speed, remove the ange-ftp completion handler from the list.
|
|
520 ; ;; We know it's not needed here.
|
|
521 ; ;; For even more speed, do this only on the outermost call.
|
|
522 ; (file-name-handler-alist
|
|
523 ; (if prev-dirs file-name-handler-alist
|
|
524 ; (let ((tem (copy-sequence file-name-handler-alist)))
|
|
525 ; (delq (rassq 'ange-ftp-completion-hook-function tem) tem)))))
|
|
526 ; (or prev-dirs (setq prev-dirs (list nil)))
|
|
527 ; ;; If this file directly leads to a link, process that iteratively
|
|
528 ; ;; so that we don't use lots of stack.
|
|
529 ; (while (not done)
|
|
530 ; (setcar counter (1- (car counter)))
|
|
531 ; (if (< (car counter) 0)
|
|
532 ; (error "Apparent cycle of symbolic links for %s" filename))
|
|
533 ; (let ((handler (find-file-name-handler filename 'file-truename)))
|
|
534 ; ;; For file name that has a special handler, call handler.
|
|
535 ; ;; This is so that ange-ftp can save time by doing a no-op.
|
|
536 ; (if handler
|
|
537 ; (setq filename (funcall handler 'file-truename filename)
|
|
538 ; done t)
|
|
539 ; (let ((dir (or (file-name-directory filename) default-directory))
|
|
540 ; target dirfile)
|
|
541 ; ;; Get the truename of the directory.
|
|
542 ; (setq dirfile (directory-file-name dir))
|
|
543 ; ;; If these are equal, we have the (or a) root directory.
|
|
544 ; (or (string= dir dirfile)
|
|
545 ; ;; If this is the same dir we last got the truename for,
|
|
546 ; ;; save time--don't recalculate.
|
|
547 ; (if (assoc dir (car prev-dirs))
|
|
548 ; (setq dir (cdr (assoc dir (car prev-dirs))))
|
|
549 ; (let ((old dir)
|
|
550 ; (new (file-name-as-directory (file-truename dirfile counter prev-dirs))))
|
|
551 ; (setcar prev-dirs (cons (cons old new) (car prev-dirs)))
|
|
552 ; (setq dir new))))
|
|
553 ; (if (equal ".." (file-name-nondirectory filename))
|
|
554 ; (setq filename
|
|
555 ; (directory-file-name (file-name-directory (directory-file-name dir)))
|
|
556 ; done t)
|
|
557 ; (if (equal "." (file-name-nondirectory filename))
|
|
558 ; (setq filename (directory-file-name dir)
|
|
559 ; done t)
|
|
560 ; ;; Put it back on the file name.
|
|
561 ; (setq filename (concat dir (file-name-nondirectory filename)))
|
|
562 ; ;; Is the file name the name of a link?
|
|
563 ; (setq target (file-symlink-p filename))
|
|
564 ; (if target
|
|
565 ; ;; Yes => chase that link, then start all over
|
|
566 ; ;; since the link may point to a directory name that uses links.
|
|
567 ; ;; We can't safely use expand-file-name here
|
|
568 ; ;; since target might look like foo/../bar where foo
|
|
569 ; ;; is itself a link. Instead, we handle . and .. above.
|
|
570 ; (setq filename
|
|
571 ; (if (file-name-absolute-p target)
|
|
572 ; target
|
|
573 ; (concat dir target))
|
|
574 ; done nil)
|
|
575 ; ;; No, we are done!
|
|
576 ; (setq done t))))))))
|
|
577 ; filename))
|
|
578
|
|
579 ;; XEmacs addition. Called from `insert-file-contents-internal'
|
|
580 ;; at the appropriate time.
|
|
581 (defun compute-buffer-file-truename (&optional buffer)
|
|
582 "Recomputes BUFFER's value of `buffer-file-truename'
|
|
583 based on the current value of `buffer-file-name'.
|
|
584 BUFFER defaults to the current buffer if unspecified."
|
|
585 (save-excursion
|
|
586 (set-buffer (or buffer (current-buffer)))
|
|
587 (cond ((null buffer-file-name)
|
|
588 (setq buffer-file-truename nil))
|
|
589 ((setq buffer-file-truename (file-truename buffer-file-name))
|
|
590 ;; it exists, we're done.
|
|
591 nil)
|
|
592 (t
|
|
593 ;; the file doesn't exist, but maybe the directory does.
|
|
594 (let* ((dir (file-name-directory buffer-file-name))
|
|
595 (truedir (file-truename dir)))
|
|
596 (if truedir (setq dir truedir))
|
|
597 (setq buffer-file-truename
|
|
598 (expand-file-name (file-name-nondirectory buffer-file-name)
|
|
599 dir)))))
|
|
600 (if (and find-file-use-truenames buffer-file-truename)
|
|
601 (setq buffer-file-name (abbreviate-file-name buffer-file-truename)
|
|
602 default-directory (file-name-directory buffer-file-name)))
|
|
603 buffer-file-truename))
|
|
604 ;; End XEmacs change block
|
|
605
|
|
606 (defun file-chase-links (filename)
|
|
607 "Chase links in FILENAME until a name that is not a link.
|
|
608 Does not examine containing directories for links,
|
|
609 unlike `file-truename'."
|
|
610 (let (tem (count 100) (newname filename))
|
|
611 (while (setq tem (file-symlink-p newname))
|
|
612 (if (= count 0)
|
|
613 (error "Apparent cycle of symbolic links for %s" filename))
|
|
614 ;; In the context of a link, `//' doesn't mean what XEmacs thinks.
|
|
615 (while (string-match "//+" tem)
|
|
616 (setq tem (concat (substring tem 0 (1+ (match-beginning 0)))
|
|
617 (substring tem (match-end 0)))))
|
|
618 ;; Handle `..' by hand, since it needs to work in the
|
|
619 ;; target of any directory symlink.
|
|
620 ;; This code is not quite complete; it does not handle
|
|
621 ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose.
|
|
622 (while (string-match "\\`\\.\\./" tem) ;#### Unix specific
|
|
623 (setq tem (substring tem 3))
|
|
624 (setq newname (file-name-as-directory
|
|
625 ;; Do the .. by hand.
|
|
626 (directory-file-name
|
|
627 (file-name-directory
|
|
628 ;; Chase links in the default dir of the symlink.
|
|
629 (file-chase-links
|
|
630 (directory-file-name
|
|
631 (file-name-directory newname))))))))
|
|
632 (setq newname (expand-file-name tem (file-name-directory newname)))
|
|
633 (setq count (1- count)))
|
|
634 newname))
|
|
635
|
|
636 (defun switch-to-other-buffer (arg)
|
|
637 "Switch to the previous buffer. With a numeric arg, n, switch to the nth
|
|
638 most recent buffer. With an arg of 0, buries the current buffer at the
|
|
639 bottom of the buffer stack."
|
|
640 (interactive "p")
|
|
641 (if (eq arg 0)
|
|
642 (bury-buffer (current-buffer)))
|
|
643 (switch-to-buffer
|
|
644 (if (<= arg 1) (other-buffer (current-buffer))
|
|
645 (nth (1+ arg) (buffer-list)))))
|
|
646
|
|
647 (defun switch-to-buffer-other-window (buffer)
|
|
648 "Select buffer BUFFER in another window."
|
|
649 (interactive "BSwitch to buffer in other window: ")
|
|
650 (let ((pop-up-windows t))
|
|
651 ;; XEmacs: this used to have (selected-frame) as the third argument,
|
|
652 ;; but this is obnoxious. If the user wants the buffer in a
|
|
653 ;; different frame, then it should be this way.
|
|
654
|
|
655 ;; Change documented above undone --mrb
|
|
656 (pop-to-buffer buffer t (selected-frame))))
|
|
657
|
|
658 (defun switch-to-buffer-other-frame (buffer)
|
|
659 "Switch to buffer BUFFER in a newly-created frame."
|
|
660 (interactive "BSwitch to buffer in other frame: ")
|
|
661 (let* ((name (get-frame-name-for-buffer buffer))
|
|
662 (frame (make-frame (if name
|
|
663 (list (cons 'name (symbol-name name)))))))
|
|
664 (pop-to-buffer buffer t frame)
|
|
665 (make-frame-visible frame)
|
|
666 buffer))
|
|
667
|
|
668 (defun find-file (filename &optional codesys)
|
|
669 "Edit file FILENAME.
|
|
670 Switch to a buffer visiting file FILENAME,
|
|
671 creating one if none already exists.
|
|
672 Under XEmacs/Mule, optional second argument specifies the
|
|
673 coding system to use when decoding the file. Interactively,
|
|
674 with a prefix argument, you will be prompted for the coding system."
|
|
675 (interactive "FFind file: \nZCoding system: ")
|
|
676 (if codesys
|
|
677 (let ((coding-system-for-read
|
|
678 (get-coding-system codesys)))
|
|
679 (switch-to-buffer (find-file-noselect filename)))
|
|
680 (switch-to-buffer (find-file-noselect filename))))
|
|
681
|
|
682 (defun find-file-other-window (filename &optional codesys)
|
|
683 "Edit file FILENAME, in another window.
|
|
684 May create a new window, or reuse an existing one.
|
|
685 See the function `display-buffer'.
|
|
686 Under XEmacs/Mule, optional second argument specifies the
|
|
687 coding system to use when decoding the file. Interactively,
|
|
688 with a prefix argument, you will be prompted for the coding system."
|
|
689 (interactive "FFind file in other window: \nZCoding system: ")
|
|
690 (if codesys
|
|
691 (let ((coding-system-for-read
|
|
692 (get-coding-system codesys)))
|
|
693 (switch-to-buffer-other-window (find-file-noselect filename)))
|
|
694 (switch-to-buffer-other-window (find-file-noselect filename))))
|
|
695
|
|
696 (defun find-file-other-frame (filename &optional codesys)
|
|
697 "Edit file FILENAME, in a newly-created frame.
|
|
698 Under XEmacs/Mule, optional second argument specifies the
|
|
699 coding system to use when decoding the file. Interactively,
|
|
700 with a prefix argument, you will be prompted for the coding system."
|
|
701 (interactive "FFind file in other frame: \nZCoding system: ")
|
|
702 (if codesys
|
|
703 (let ((coding-system-for-read
|
|
704 (get-coding-system codesys)))
|
|
705 (switch-to-buffer-other-frame (find-file-noselect filename)))
|
|
706 (switch-to-buffer-other-frame (find-file-noselect filename))))
|
|
707
|
|
708 (defun find-file-read-only (filename &optional codesys)
|
|
709 "Edit file FILENAME but don't allow changes.
|
|
710 Like \\[find-file] but marks buffer as read-only.
|
|
711 Use \\[toggle-read-only] to permit editing.
|
|
712 Under XEmacs/Mule, optional second argument specifies the
|
|
713 coding system to use when decoding the file. Interactively,
|
|
714 with a prefix argument, you will be prompted for the coding system."
|
|
715 (interactive "fFind file read-only: \nZCoding system: ")
|
|
716 (if codesys
|
|
717 (let ((coding-system-for-read
|
|
718 (get-coding-system codesys)))
|
|
719 (find-file filename))
|
|
720 (find-file filename))
|
|
721 (setq buffer-read-only t)
|
|
722 (current-buffer))
|
|
723
|
|
724 (defun find-file-read-only-other-window (filename &optional codesys)
|
|
725 "Edit file FILENAME in another window but don't allow changes.
|
|
726 Like \\[find-file-other-window] but marks buffer as read-only.
|
|
727 Use \\[toggle-read-only] to permit editing.
|
|
728 Under XEmacs/Mule, optional second argument specifies the
|
|
729 coding system to use when decoding the file. Interactively,
|
|
730 with a prefix argument, you will be prompted for the coding system."
|
|
731 (interactive "fFind file read-only other window: \nZCoding system: ")
|
|
732 (if codesys
|
|
733 (let ((coding-system-for-read
|
|
734 (get-coding-system codesys)))
|
|
735 (find-file-other-window filename))
|
|
736 (find-file-other-window filename))
|
|
737 (setq buffer-read-only t)
|
|
738 (current-buffer))
|
|
739
|
|
740 (defun find-file-read-only-other-frame (filename &optional codesys)
|
|
741 "Edit file FILENAME in another frame but don't allow changes.
|
|
742 Like \\[find-file-other-frame] but marks buffer as read-only.
|
|
743 Use \\[toggle-read-only] to permit editing.
|
|
744 Under XEmacs/Mule, optional second argument specifies the
|
|
745 coding system to use when decoding the file. Interactively,
|
|
746 with a prefix argument, you will be prompted for the coding system."
|
|
747 (interactive "fFind file read-only other frame: \nZCoding system: ")
|
|
748 (if codesys
|
|
749 (let ((coding-system-for-read
|
|
750 (get-coding-system codesys)))
|
|
751 (find-file-other-frame filename))
|
|
752 (find-file-other-frame filename))
|
|
753 (setq buffer-read-only t)
|
|
754 (current-buffer))
|
|
755
|
|
756 (defun find-alternate-file-other-window (filename &optional codesys)
|
|
757 "Find file FILENAME as a replacement for the file in the next window.
|
|
758 This command does not select that window.
|
|
759 Under XEmacs/Mule, optional second argument specifies the
|
|
760 coding system to use when decoding the file. Interactively,
|
|
761 with a prefix argument, you will be prompted for the coding system."
|
|
762 (interactive
|
|
763 (save-selected-window
|
|
764 (other-window 1)
|
|
765 (let ((file buffer-file-name)
|
|
766 (file-name nil)
|
|
767 (file-dir nil))
|
|
768 (and file
|
|
769 (setq file-name (file-name-nondirectory file)
|
|
770 file-dir (file-name-directory file)))
|
|
771 (list (read-file-name
|
|
772 "Find alternate file: " file-dir nil nil file-name)
|
|
773 (if (and current-prefix-arg (featurep 'mule))
|
|
774 (read-coding-system "Coding-system: "))))))
|
|
775 (if (one-window-p)
|
|
776 (find-file-other-window filename)
|
|
777 (save-selected-window
|
|
778 (other-window 1)
|
|
779 (find-alternate-file filename codesys))))
|
|
780
|
|
781 (defun find-alternate-file (filename &optional codesys)
|
|
782 "Find file FILENAME, select its buffer, kill previous buffer.
|
|
783 If the current buffer now contains an empty file that you just visited
|
|
784 \(presumably by mistake), use this command to visit the file you really want.
|
|
785 Under XEmacs/Mule, optional second argument specifies the
|
|
786 coding system to use when decoding the file. Interactively,
|
|
787 with a prefix argument, you will be prompted for the coding system."
|
|
788 (interactive
|
|
789 (let ((file buffer-file-name)
|
|
790 (file-name nil)
|
|
791 (file-dir nil))
|
|
792 (and file
|
|
793 (setq file-name (file-name-nondirectory file)
|
|
794 file-dir (file-name-directory file)))
|
|
795 (list (read-file-name
|
|
796 "Find alternate file: " file-dir nil nil file-name)
|
|
797 (if (and current-prefix-arg (featurep 'mule))
|
|
798 (read-coding-system "Coding-system: ")))))
|
|
799 (and (buffer-modified-p) (buffer-file-name)
|
|
800 ;; (not buffer-read-only)
|
|
801 (not (yes-or-no-p (format
|
|
802 "Buffer %s is modified; kill anyway? "
|
|
803 (buffer-name))))
|
|
804 (error "Aborted"))
|
|
805 (let ((obuf (current-buffer))
|
|
806 (ofile buffer-file-name)
|
|
807 (onum buffer-file-number)
|
|
808 (otrue buffer-file-truename)
|
|
809 (oname (buffer-name)))
|
|
810 (if (get-buffer " **lose**")
|
|
811 (kill-buffer " **lose**"))
|
|
812 (rename-buffer " **lose**")
|
|
813 (setq buffer-file-name nil)
|
|
814 (setq buffer-file-number nil)
|
|
815 (setq buffer-file-truename nil)
|
|
816 (unwind-protect
|
|
817 (progn
|
|
818 (unlock-buffer)
|
|
819 (if codesys
|
|
820 (let ((coding-system-for-read
|
|
821 (get-coding-system codesys)))
|
|
822 (find-file filename))
|
|
823 (find-file filename)))
|
|
824 (cond ((eq obuf (current-buffer))
|
|
825 (setq buffer-file-name ofile)
|
|
826 (setq buffer-file-number onum)
|
|
827 (setq buffer-file-truename otrue)
|
|
828 (lock-buffer)
|
|
829 (rename-buffer oname))))
|
|
830 (or (eq (current-buffer) obuf)
|
|
831 (kill-buffer obuf))))
|
|
832
|
|
833 (defun create-file-buffer (filename)
|
|
834 "Create a suitably named buffer for visiting FILENAME, and return it.
|
|
835 FILENAME (sans directory) is used unchanged if that name is free;
|
|
836 otherwise a string <2> or <3> or ... is appended to get an unused name."
|
|
837 (let ((handler (find-file-name-handler filename 'create-file-buffer)))
|
|
838 (if handler
|
|
839 (funcall handler 'create-file-buffer filename)
|
|
840 (let ((lastname (file-name-nondirectory filename)))
|
|
841 (if (string= lastname "")
|
|
842 (setq lastname filename))
|
|
843 (generate-new-buffer lastname)))))
|
|
844
|
|
845 (defun generate-new-buffer (name)
|
|
846 "Create and return a buffer with a name based on NAME.
|
|
847 Choose the buffer's name using `generate-new-buffer-name'."
|
|
848 (get-buffer-create (generate-new-buffer-name name)))
|
|
849
|
|
850 (defvar abbreviated-home-dir nil
|
|
851 "The user's homedir abbreviated according to `directory-abbrev-alist'.")
|
|
852
|
|
853 (defun abbreviate-file-name (filename &optional hack-homedir)
|
|
854 "Return a version of FILENAME shortened using `directory-abbrev-alist'.
|
|
855 See documentation of variable `directory-abbrev-alist' for more information.
|
|
856 If optional argument HACK-HOMEDIR is non-nil, then this also substitutes
|
|
857 \"~\" for the user's home directory."
|
|
858 (let ((handler (find-file-name-handler filename 'abbreviate-file-name)))
|
|
859 (if handler
|
|
860 (funcall handler 'abbreviate-file-name filename hack-homedir)
|
|
861 ;; Get rid of the prefixes added by the automounter.
|
|
862 ;;(if (and (string-match automount-dir-prefix filename)
|
|
863 ;; (file-exists-p (file-name-directory
|
|
864 ;; (substring filename (1- (match-end 0))))))
|
|
865 ;; (setq filename (substring filename (1- (match-end 0)))))
|
|
866 (let ((tail directory-abbrev-alist))
|
|
867 ;; If any elt of directory-abbrev-alist matches this name,
|
|
868 ;; abbreviate accordingly.
|
|
869 (while tail
|
|
870 (if (string-match (car (car tail)) filename)
|
|
871 (setq filename
|
|
872 (concat (cdr (car tail)) (substring filename (match-end 0)))))
|
|
873 (setq tail (cdr tail))))
|
|
874 (if hack-homedir
|
|
875 (progn
|
|
876 ;; Compute and save the abbreviated homedir name.
|
|
877 ;; We defer computing this until the first time it's needed, to
|
|
878 ;; give time for directory-abbrev-alist to be set properly.
|
|
879 ;; We include a slash at the end, to avoid spurious matches
|
|
880 ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
|
|
881 (or abbreviated-home-dir
|
|
882 (setq abbreviated-home-dir
|
|
883 (let ((abbreviated-home-dir "$foo"))
|
|
884 (concat "\\`" (regexp-quote (abbreviate-file-name
|
|
885 (expand-file-name "~")))
|
|
886 "\\(/\\|\\'\\)"))))
|
|
887 ;; If FILENAME starts with the abbreviated homedir,
|
|
888 ;; make it start with `~' instead.
|
|
889 (if (and (string-match abbreviated-home-dir filename)
|
|
890 ;; If the home dir is just /, don't change it.
|
|
891 (not (and (= (match-end 0) 1) ;#### unix-specific
|
|
892 (= (aref filename 0) ?/)))
|
|
893 (not (and (or (eq system-type 'ms-dos)
|
|
894 (eq system-type 'windows-nt))
|
|
895 (save-match-data
|
|
896 (string-match "^[a-zA-Z]:/$" filename)))))
|
|
897 (setq filename
|
|
898 (concat "~"
|
|
899 (substring filename
|
|
900 (match-beginning 1) (match-end 1))
|
|
901 (substring filename (match-end 0)))))))
|
|
902 filename)))
|
|
903
|
|
904 (defcustom find-file-not-true-dirname-list nil
|
|
905 "*List of logical names for which visiting shouldn't save the true dirname.
|
|
906 On VMS, when you visit a file using a logical name that searches a path,
|
|
907 you may or may not want the visited file name to record the specific
|
|
908 directory where the file was found. If you *do not* want that, add the logical
|
|
909 name to this list as a string."
|
|
910 :type '(repeat (string :tag "Name"))
|
|
911 :group 'find-file)
|
|
912
|
|
913 ;; This function is needed by FSF vc.el. I hope somebody can make it
|
|
914 ;; work for XEmacs. -sb.
|
|
915 (defun find-buffer-visiting (filename)
|
|
916 "Return the buffer visiting file FILENAME (a string).
|
|
917 This is like `get-file-buffer', except that it checks for any buffer
|
|
918 visiting the same file, possibly under a different name.
|
|
919 If there is no such live buffer, return nil."
|
|
920 (let ((buf (get-file-buffer filename))
|
|
921 (truename (abbreviate-file-name (file-truename filename))))
|
|
922 (or buf
|
|
923 (let ((list (buffer-list)) found)
|
|
924 (while (and (not found) list)
|
|
925 (save-excursion
|
|
926 (set-buffer (car list))
|
|
927 (if (and buffer-file-name
|
|
928 (string= buffer-file-truename truename))
|
|
929 (setq found (car list))))
|
|
930 (setq list (cdr list)))
|
|
931 found)
|
|
932 (let ((number (nthcdr 10 (file-attributes truename)))
|
|
933 (list (buffer-list)) found)
|
|
934 (and buffer-file-numbers-unique
|
|
935 number
|
|
936 (while (and (not found) list)
|
|
937 (save-excursion
|
|
938 (set-buffer (car list))
|
|
939 (if (and buffer-file-number
|
|
940 (equal buffer-file-number number)
|
|
941 ;; Verify this buffer's file number
|
|
942 ;; still belongs to its file.
|
|
943 (file-exists-p buffer-file-name)
|
|
944 (equal (nthcdr 10 (file-attributes buffer-file-name))
|
|
945 number))
|
|
946 (setq found (car list))))
|
|
947 (setq list (cdr list))))
|
|
948 found))))
|
|
949
|
|
950 (defun insert-file-contents-literally (filename &optional visit beg end replace)
|
|
951 "Like `insert-file-contents', q.v., but only reads in the file.
|
|
952 A buffer may be modified in several ways after reading into the buffer due
|
|
953 to advanced Emacs features, such as file-name-handlers, format decoding,
|
|
954 find-file-hooks, etc.
|
|
955 This function ensures that none of these modifications will take place."
|
|
956 (let ((file-name-handler-alist nil)
|
|
957 (format-alist nil)
|
|
958 (after-insert-file-functions nil)
|
|
959 (find-buffer-file-type-function
|
|
960 (if (fboundp 'find-buffer-file-type)
|
|
961 (symbol-function 'find-buffer-file-type)
|
|
962 nil)))
|
|
963 (unwind-protect
|
|
964 (progn
|
|
965 (fset 'find-buffer-file-type (lambda (filename) t))
|
|
966 (insert-file-contents filename visit beg end replace))
|
|
967 (if find-buffer-file-type-function
|
|
968 (fset 'find-buffer-file-type find-buffer-file-type-function)
|
|
969 (fmakunbound 'find-buffer-file-type)))))
|
|
970
|
|
971 (defun find-file-noselect (filename &optional nowarn rawfile)
|
|
972 "Read file FILENAME into a buffer and return the buffer.
|
|
973 If a buffer exists visiting FILENAME, return that one, but
|
|
974 verify that the file has not changed since visited or saved.
|
|
975 The buffer is not selected, just returned to the caller.
|
|
976 If NOWARN is non-nil, warning messages about several potential
|
|
977 problems will be suppressed."
|
|
978 (setq filename (abbreviate-file-name (expand-file-name filename)))
|
|
979 (if (file-directory-p filename)
|
211
|
980 (if (and (fboundp 'dired-noselect) find-file-run-dired)
|
209
|
981 (dired-noselect (if find-file-use-truenames
|
|
982 (abbreviate-file-name (file-truename filename))
|
|
983 filename))
|
|
984 (error "%s is a directory." filename))
|
|
985 (let* ((buf (get-file-buffer filename))
|
263
|
986 (truename (abbreviate-file-name (file-truename filename)))
|
209
|
987 (number (nthcdr 10 (file-attributes (file-truename filename))))
|
|
988 ; (number (and buffer-file-truename
|
|
989 ; (nthcdr 10 (file-attributes buffer-file-truename))))
|
|
990 ; ;; Find any buffer for a file which has same truename.
|
|
991 ; (other (and (not buf) (find-buffer-visiting filename)))
|
|
992 (error nil))
|
|
993
|
|
994 ; ;; Let user know if there is a buffer with the same truename.
|
|
995 ; (if (and (not buf) same-truename (not nowarn))
|
|
996 ; (message "%s and %s are the same file (%s)"
|
|
997 ; filename (buffer-file-name same-truename)
|
|
998 ; truename)
|
|
999 ; (if (and (not buf) same-number (not nowarn))
|
|
1000 ; (message "%s and %s are the same file"
|
|
1001 ; filename (buffer-file-name same-number))))
|
|
1002 ; ;; Optionally also find that buffer.
|
|
1003 ; (if (or find-file-existing-other-name find-file-visit-truename)
|
|
1004 ; (setq buf (or same-truename same-number)))
|
|
1005
|
|
1006 (when (and buf
|
|
1007 (or find-file-compare-truenames find-file-use-truenames)
|
|
1008 (not nowarn))
|
|
1009 (save-excursion
|
|
1010 (set-buffer buf)
|
|
1011 (if (not (string-equal buffer-file-name filename))
|
|
1012 (message "%s and %s are the same file (%s)"
|
|
1013 filename buffer-file-name
|
|
1014 buffer-file-truename))))
|
|
1015
|
|
1016 (if buf
|
|
1017 (or nowarn
|
|
1018 (verify-visited-file-modtime buf)
|
|
1019 (cond ((not (file-exists-p filename))
|
|
1020 (error "File %s no longer exists!" filename))
|
|
1021 ;; Certain files should be reverted automatically
|
|
1022 ;; if they have changed on disk and not in the buffer.
|
|
1023 ((and (not (buffer-modified-p buf))
|
|
1024 (let (found)
|
|
1025 (dolist (rx revert-without-query found)
|
|
1026 (when (string-match rx filename)
|
|
1027 (setq found t)))))
|
|
1028 (with-current-buffer buf
|
|
1029 (message "Reverting file %s..." filename)
|
|
1030 (revert-buffer t t)
|
|
1031 (message "Reverting file %s... done" filename)))
|
|
1032 ((yes-or-no-p
|
|
1033 (if (string= (file-name-nondirectory filename)
|
|
1034 (buffer-name buf))
|
|
1035 (format
|
|
1036 (if (buffer-modified-p buf)
|
|
1037 (gettext "File %s changed on disk. Discard your edits? ")
|
|
1038 (gettext "File %s changed on disk. Reread from disk? "))
|
|
1039 (file-name-nondirectory filename))
|
|
1040 (format
|
|
1041 (if (buffer-modified-p buf)
|
|
1042 (gettext "File %s changed on disk. Discard your edits in %s? ")
|
|
1043 (gettext "File %s changed on disk. Reread from disk into %s? "))
|
|
1044 (file-name-nondirectory filename)
|
|
1045 (buffer-name buf))))
|
|
1046 (save-excursion
|
|
1047 (set-buffer buf)
|
|
1048 (revert-buffer t t)))))
|
|
1049 ;; Else: we must create a new buffer for filename
|
|
1050 (save-excursion
|
|
1051 ;;; The truename stuff makes this obsolete.
|
|
1052 ;;; (let* ((link-name (car (file-attributes filename)))
|
|
1053 ;;; (linked-buf (and (stringp link-name)
|
|
1054 ;;; (get-file-buffer link-name))))
|
|
1055 ;;; (if (bufferp linked-buf)
|
|
1056 ;;; (message "Symbolic link to file in buffer %s"
|
|
1057 ;;; (buffer-name linked-buf))))
|
|
1058 (setq buf (create-file-buffer filename))
|
|
1059 (set-buffer-major-mode buf)
|
|
1060 (set-buffer buf)
|
|
1061 (erase-buffer)
|
|
1062 (if rawfile
|
|
1063 (condition-case ()
|
|
1064 (insert-file-contents-literally filename t)
|
|
1065 (file-error
|
|
1066 ;; Unconditionally set error
|
|
1067 (setq error t)))
|
|
1068 (condition-case e
|
|
1069 (insert-file-contents filename t)
|
|
1070 (file-error
|
|
1071 ;; Run find-file-not-found-hooks until one returns non-nil.
|
|
1072 (or (run-hook-with-args-until-success 'find-file-not-found-hooks)
|
|
1073 ;; If they fail too, set error.
|
|
1074 (setq error e)))))
|
|
1075 ;; Find the file's truename, and maybe use that as visited name.
|
263
|
1076 ;; automatically computed in XEmacs, unless jka-compr was used!
|
|
1077 (unless buffer-file-truename
|
|
1078 (setq buffer-file-truename truename))
|
209
|
1079 (setq buffer-file-number number)
|
|
1080 ;; On VMS, we may want to remember which directory in a search list
|
|
1081 ;; the file was found in.
|
|
1082 (and (eq system-type 'vax-vms)
|
|
1083 (let (logical)
|
|
1084 (if (string-match ":" (file-name-directory filename))
|
|
1085 (setq logical (substring (file-name-directory filename)
|
|
1086 0 (match-beginning 0))))
|
|
1087 (not (member logical find-file-not-true-dirname-list)))
|
|
1088 (setq buffer-file-name buffer-file-truename))
|
|
1089 ; (if find-file-visit-truename
|
|
1090 ; (setq buffer-file-name
|
|
1091 ; (setq filename
|
|
1092 ; (expand-file-name buffer-file-truename))))
|
|
1093 (and find-file-use-truenames
|
|
1094 ;; This should be in C. Put pathname abbreviations that have
|
|
1095 ;; been explicitly requested back into the pathname. Most
|
|
1096 ;; importantly, strip out automounter /tmp_mnt directories so
|
|
1097 ;; that auto-save will work
|
|
1098 (setq buffer-file-name (abbreviate-file-name buffer-file-name)))
|
|
1099 ;; Set buffer's default directory to that of the file.
|
|
1100 (setq default-directory (file-name-directory buffer-file-name))
|
|
1101 ;; Turn off backup files for certain file names. Since
|
|
1102 ;; this is a permanent local, the major mode won't eliminate it.
|
|
1103 (and (not (funcall backup-enable-predicate buffer-file-name))
|
|
1104 (progn
|
|
1105 (make-local-variable 'backup-inhibited)
|
|
1106 (setq backup-inhibited t)))
|
|
1107 (if rawfile
|
|
1108 nil
|
|
1109 (after-find-file error (not nowarn))
|
|
1110 (setq buf (current-buffer)))))
|
|
1111 buf)))
|
|
1112
|
|
1113 (defvar after-find-file-from-revert-buffer nil)
|
|
1114
|
|
1115 (defun after-find-file (&optional error warn noauto
|
|
1116 after-find-file-from-revert-buffer
|
|
1117 nomodes)
|
|
1118 "Called after finding a file and by the default revert function.
|
|
1119 Sets buffer mode, parses local variables.
|
|
1120 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an
|
|
1121 error in reading the file. WARN non-nil means warn if there
|
|
1122 exists an auto-save file more recent than the visited file.
|
|
1123 NOAUTO means don't mess with auto-save mode.
|
|
1124 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil
|
|
1125 means this call was from `revert-buffer'.
|
|
1126 Fifth arg NOMODES non-nil means don't alter the file's modes.
|
|
1127 Finishes by calling the functions in `find-file-hooks'."
|
|
1128 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
|
|
1129 (if noninteractive
|
|
1130 nil
|
|
1131 (let* (not-serious
|
|
1132 (msg
|
|
1133 (cond ((and error (file-attributes buffer-file-name))
|
|
1134 (setq buffer-read-only t)
|
|
1135 (gettext "File exists, but cannot be read."))
|
|
1136 ((not buffer-read-only)
|
|
1137 (if (and warn
|
|
1138 (file-newer-than-file-p (make-auto-save-file-name)
|
|
1139 buffer-file-name))
|
|
1140 (format "%s has auto save data; consider M-x recover-file"
|
255
|
1141 (file-name-nondirectory buffer-file-name))
|
209
|
1142 (setq not-serious t)
|
255
|
1143 (if error (gettext "(New file)") nil)))
|
209
|
1144 ((not error)
|
|
1145 (setq not-serious t)
|
|
1146 (gettext "Note: file is write protected"))
|
|
1147 ((file-attributes (directory-file-name default-directory))
|
|
1148 (gettext "File not found and directory write-protected"))
|
|
1149 ((file-exists-p (file-name-directory buffer-file-name))
|
|
1150 (setq buffer-read-only nil))
|
|
1151 (t
|
|
1152 ;; If the directory the buffer is in doesn't exist,
|
|
1153 ;; offer to create it. It's better to do this now
|
|
1154 ;; than when we save the buffer, because we want
|
|
1155 ;; autosaving to work.
|
|
1156 (setq buffer-read-only nil)
|
|
1157 ;; XEmacs
|
|
1158 (or (file-exists-p (file-name-directory buffer-file-name))
|
|
1159 (if (yes-or-no-p
|
|
1160 (format
|
|
1161 "The directory containing %s does not exist. Create? "
|
|
1162 (abbreviate-file-name buffer-file-name)))
|
|
1163 (make-directory (file-name-directory
|
|
1164 buffer-file-name)
|
|
1165 t)))
|
|
1166 nil))))
|
|
1167 (if msg
|
|
1168 (progn
|
255
|
1169 (message "%s" msg)
|
209
|
1170 (or not-serious (sit-for 1 t)))))
|
|
1171 (if (and auto-save-default (not noauto))
|
|
1172 (auto-save-mode t)))
|
|
1173 (unless nomodes
|
|
1174 (normal-mode t)
|
|
1175 (run-hooks 'find-file-hooks)))
|
|
1176
|
|
1177 (defun normal-mode (&optional find-file)
|
|
1178 "Choose the major mode for this buffer automatically.
|
|
1179 Also sets up any specified local variables of the file.
|
|
1180 Uses the visited file name, the -*- line, and the local variables spec.
|
|
1181
|
|
1182 This function is called automatically from `find-file'. In that case,
|
|
1183 we may set up specified local variables depending on the value of
|
|
1184 `enable-local-variables': if it is t, we do; if it is nil, we don't;
|
|
1185 otherwise, we query. `enable-local-variables' is ignored if you
|
|
1186 run `normal-mode' explicitly."
|
|
1187 (interactive)
|
|
1188 (or find-file (funcall (or default-major-mode 'fundamental-mode)))
|
|
1189 (and (condition-case err
|
|
1190 (progn (set-auto-mode)
|
|
1191 t)
|
|
1192 (error (message "File mode specification error: %s"
|
|
1193 (prin1-to-string err))
|
|
1194 nil))
|
|
1195 (condition-case err
|
|
1196 (hack-local-variables (not find-file))
|
|
1197 (error (message "File local-variables error: %s"
|
|
1198 (prin1-to-string err))))))
|
|
1199
|
|
1200 (defvar auto-mode-alist
|
|
1201 '(("\\.te?xt\\'" . text-mode)
|
|
1202 ("\\.[ch]\\'" . c-mode)
|
|
1203 ("\\.el\\'" . emacs-lisp-mode)
|
|
1204 ("\\.\\([CH]\\|cc\\|hh\\)\\'" . c++-mode)
|
|
1205 ("\\.[ch]\\(pp\\|xx\\|\\+\\+\\)\\'" . c++-mode)
|
|
1206 ("\\.java\\'" . java-mode)
|
|
1207 ("\\.f\\(or\\)?\\'" . fortran-mode)
|
|
1208 ("\\.F\\(OR\\)?\\'" . fortran-mode)
|
|
1209 ("\\.[fF]90\\'" . f90-mode)
|
|
1210 ;;; Less common extensions come here
|
|
1211 ;;; so more common ones above are found faster.
|
|
1212 ("\\.p[lm]\\'" . perl-mode)
|
|
1213 ("\\.py\\'" . python-mode)
|
|
1214 ("\\.texi\\(nfo\\)?\\'" . texinfo-mode)
|
|
1215 ("\\.ad[abs]\\'" . ada-mode)
|
|
1216 ("\\.c?l\\(i?sp\\)?\\'" . lisp-mode)
|
|
1217 ("\\.p\\(as\\)?\\'" . pascal-mode)
|
|
1218 ("\\.ltx\\'" . latex-mode)
|
|
1219 ("\\.[sS]\\'" . asm-mode)
|
|
1220 ("[Cc]hange.?[Ll]og?\\(.[0-9]+\\)?\\'" . change-log-mode)
|
|
1221 ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode)
|
|
1222 ("\\.scm\\(\\.[0-9]*\\)?\\'" . scheme-mode)
|
|
1223 ("\\.e\\'" . eiffel-mode)
|
|
1224 ("\\.mss\\'" . scribe-mode)
|
|
1225 ("\\.m\\([mes]\\|an\\)\\'" . nroff-mode)
|
|
1226 ("\\.icn\\'" . icon-mode)
|
|
1227 ("\\.\\([ckz]?sh\\|shar\\)\\'" . sh-mode)
|
|
1228 ("/\\.\\(bash_\\|z\\)?\\(profile\\|login\||logout\\)\\'" . sh-mode)
|
|
1229 ("/\\.\\([ckz]sh\\|bash\\|tcsh\\|es\\|xinit\\|startx\\)rc\\'" . sh-mode)
|
|
1230 ("/\\.\\([kz]shenv\\|xsession\\)\\'" . sh-mode)
|
|
1231 ;;; The following should come after the ChangeLog pattern
|
|
1232 ;;; for the sake of ChangeLog.1, etc.
|
|
1233 ;;; and after the .scm.[0-9] pattern too.
|
|
1234 ("\\.[12345678]\\'" . nroff-mode)
|
|
1235 ("\\.[tT]e[xX]\\'" . tex-mode)
|
|
1236 ("\\.\\(sty\\|cls\\|bbl\\)\\'" . latex-mode)
|
|
1237 ("\\.bib\\'" . bibtex-mode)
|
|
1238 ("\\.article\\'" . text-mode)
|
|
1239 ("\\.letter\\'" . text-mode)
|
|
1240 ("\\.\\(tcl\\|exp\\)\\'" . tcl-mode)
|
|
1241 ("\\.wrl\\'" . vrml-mode)
|
|
1242 ("\\.awk\\'" . awk-mode)
|
|
1243 ("\\.prolog\\'" . prolog-mode)
|
|
1244 ("\\.tar\\'" . tar-mode)
|
|
1245 ("\\.\\(arc\\|zip\\|lzh\\|zoo\\)\\'" . archive-mode)
|
|
1246 ;; Mailer puts message to be edited in
|
|
1247 ;; /tmp/Re.... or Message
|
|
1248 ("^/tmp/Re" . text-mode)
|
|
1249 ("/Message[0-9]*\\'" . text-mode)
|
|
1250 ("/drafts/[0-9]+\\'" . mh-letter-mode)
|
|
1251 ;; some news reader is reported to use this
|
|
1252 ("^/tmp/fol/" . text-mode)
|
|
1253 ("\\.y\\'" . c-mode)
|
|
1254 ("\\.lex\\'" . c-mode)
|
|
1255 ("\\.m\\'" . objc-mode)
|
|
1256 ("\\.oak\\'" . scheme-mode)
|
|
1257 ("\\.s?html?\\'" . html-mode)
|
|
1258 ("\\.htm?l?3\\'" . html3-mode)
|
|
1259 ("\\.\\(sgml?\\|dtd\\)\\'" . sgml-mode)
|
|
1260 ("\\.c?ps\\'" . postscript-mode)
|
|
1261 ;; .emacs following a directory delimiter
|
|
1262 ;; in either Unix or VMS syntax.
|
|
1263 ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode)
|
|
1264 ;; _emacs following a directory delimiter
|
|
1265 ;; in MsDos syntax
|
|
1266 ("[:/]_emacs\\'" . emacs-lisp-mode)
|
|
1267 ("\\.m4\\'" . autoconf-mode)
|
|
1268 ("configure\\.in\\'" . autoconf-mode)
|
|
1269 ("\\.ml\\'" . lisp-mode)
|
|
1270 ("\\.ma?k\\'" . makefile-mode)
|
|
1271 ("[Mm]akefile\\(\\.\\|\\'\\)" . makefile-mode)
|
|
1272 ("\\.X\\(defaults\\|environment\\|resources\\|modmap\\)\\'" . xrdb-mode)
|
|
1273 ("/app-defaults/" . xrdb-mode)
|
|
1274 ("\\.[^/]*wm\\'" . winmgr-mode)
|
|
1275 ("\\.[^/]*wm2?rc" . winmgr-mode)
|
|
1276 ("\\.[Jj][Pp][Ee]?[Gg]\\'" . image-mode)
|
|
1277 ("\\.[Pp][Nn][Gg]\\'" . image-mode)
|
|
1278 ("\\.[Gg][Ii][Ff]\\'" . image-mode)
|
|
1279 )
|
|
1280 "Alist of filename patterns vs. corresponding major mode functions.
|
|
1281 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
|
|
1282 \(NON-NIL stands for anything that is not nil; the value does not matter.)
|
|
1283 Visiting a file whose name matches REGEXP specifies FUNCTION as the
|
|
1284 mode function to use. FUNCTION will be called, unless it is nil.
|
|
1285
|
|
1286 If the element has the form (REGEXP FUNCTION NON-NIL), then after
|
|
1287 calling FUNCTION (if it's not nil), we delete the suffix that matched
|
|
1288 REGEXP and search the list again for another match.")
|
|
1289
|
|
1290 (defconst interpreter-mode-alist
|
|
1291 '(("^#!.*csh" . sh-mode)
|
|
1292 ("^#!.*sh\\b" . sh-mode)
|
|
1293 ("^#!.*\\b\\(scope\\|wish\\|tcl\\|expect\\)" . tcl-mode)
|
|
1294 ("perl" . perl-mode)
|
|
1295 ("python" . python-mode)
|
|
1296 ("awk\\b" . awk-mode)
|
|
1297 ("rexx" . rexx-mode)
|
|
1298 ("scm" . scheme-mode)
|
|
1299 ("^:" . sh-mode))
|
|
1300 "Alist mapping interpreter names to major modes.
|
|
1301 This alist is used to guess the major mode of a file based on the
|
|
1302 contents of the first line. This line often contains something like:
|
|
1303 #!/bin/sh
|
|
1304 but may contain something more imaginative like
|
|
1305 #! /bin/env python
|
|
1306 or
|
|
1307 eval 'exec perl -w -S $0 ${1+\"$@\"}'.
|
|
1308
|
|
1309 Each alist element looks like (INTERPRETER . MODE).
|
|
1310 The car of each element is a regular expression which is compared
|
|
1311 with the name of the interpreter specified in the first line.
|
|
1312 If it matches, mode MODE is selected.")
|
|
1313
|
|
1314 (defconst inhibit-first-line-modes-regexps (purecopy '("\\.tar\\'"))
|
|
1315 "List of regexps; if one matches a file name, don't look for `-*-'.")
|
|
1316
|
|
1317 (defconst inhibit-first-line-modes-suffixes nil
|
|
1318 "List of regexps for what to ignore, for `inhibit-first-line-modes-regexps'.
|
|
1319 When checking `inhibit-first-line-modes-regexps', we first discard
|
|
1320 from the end of the file name anything that matches one of these regexps.")
|
|
1321
|
|
1322 (defvar user-init-file
|
|
1323 "" ; set by command-line
|
|
1324 "File name including directory of user's initialization file.")
|
|
1325
|
|
1326 (defun set-auto-mode ()
|
|
1327 "Select major mode appropriate for current buffer.
|
|
1328 This checks for a -*- mode tag in the buffer's text,
|
|
1329 compares the filename against the entries in `auto-mode-alist',
|
|
1330 or checks the interpreter that runs this file against
|
|
1331 `interpreter-mode-alist'.
|
|
1332
|
|
1333 It does not check for the `mode:' local variable in the
|
|
1334 Local Variables section of the file; for that, use `hack-local-variables'.
|
|
1335
|
|
1336 If `enable-local-variables' is nil, this function does not check for a
|
|
1337 -*- mode tag."
|
|
1338 (save-excursion
|
|
1339 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
|
|
1340 ;; Do this by calling the hack-local-variables helper to avoid redundancy.
|
|
1341 ;; We bind enable-local-variables to nil this time because we're going to
|
219
|
1342 ;; call hack-local-variables-prop-line again later, "for real." Note that
|
|
1343 ;; this temporary binding does not prevent hack-local-variables-prop-line
|
|
1344 ;; from setting the major mode.
|
|
1345 (or (and enable-local-variables
|
|
1346 (let ((enable-local-variables nil))
|
|
1347 (hack-local-variables-prop-line nil))
|
|
1348 )
|
209
|
1349 ;; It's not in the -*- line, so check the auto-mode-alist, unless
|
|
1350 ;; this buffer isn't associated with a file.
|
|
1351 (null buffer-file-name)
|
|
1352 (let ((name (file-name-sans-versions buffer-file-name))
|
|
1353 (keep-going t))
|
|
1354 (while keep-going
|
|
1355 (setq keep-going nil)
|
|
1356 (let ((alist auto-mode-alist)
|
|
1357 (mode nil))
|
|
1358 ;; Find first matching alist entry.
|
|
1359 (let ((case-fold-search
|
|
1360 (memq system-type '(vax-vms windows-nt))))
|
|
1361 (while (and (not mode) alist)
|
|
1362 (if (string-match (car (car alist)) name)
|
|
1363 (if (and (consp (cdr (car alist)))
|
|
1364 (nth 2 (car alist)))
|
|
1365 (progn
|
|
1366 (setq mode (car (cdr (car alist)))
|
|
1367 name (substring name 0 (match-beginning 0))
|
|
1368 keep-going t))
|
|
1369 (setq mode (cdr (car alist))
|
|
1370 keep-going nil)))
|
|
1371 (setq alist (cdr alist))))
|
|
1372 ;; If we can't deduce a mode from the file name,
|
|
1373 ;; look for an interpreter specified in the first line.
|
|
1374 (if (and (null mode)
|
|
1375 (save-excursion ; XEmacs
|
|
1376 (goto-char (point-min))
|
|
1377 (looking-at "#!")))
|
|
1378 (let ((firstline
|
|
1379 (buffer-substring
|
|
1380 (point-min)
|
|
1381 (save-excursion
|
|
1382 (goto-char (point-min)) (end-of-line) (point)))))
|
|
1383 (setq alist interpreter-mode-alist)
|
|
1384 (while alist
|
|
1385 (if (string-match (car (car alist)) firstline)
|
|
1386 (progn
|
|
1387 (setq mode (cdr (car alist)))
|
|
1388 (setq alist nil))
|
|
1389 (setq alist (cdr alist))))))
|
|
1390 (if mode
|
265
|
1391 (if (not (fboundp mode))
|
|
1392 (progn
|
|
1393 (if (or (not (boundp 'package-get-base))
|
|
1394 (not package-get-base))
|
|
1395 (load "package-get-base"))
|
|
1396 (require 'package-get)
|
|
1397 (let ((name (package-get-package-provider mode)))
|
|
1398 (if name
|
|
1399 (message "Mode %s is not installed. Download package %s" mode name)
|
|
1400 (message "Mode %s either doesn't exist or is not a known package" mode))
|
|
1401 (sit-for 2)
|
|
1402 (error "%s" mode)))
|
|
1403 (funcall mode)))
|
209
|
1404 ))))))
|
|
1405
|
|
1406 (defvar hack-local-variables-hook nil
|
|
1407 "Normal hook run after processing a file's local variables specs.
|
|
1408 Major modes can use this to examine user-specified local variables
|
|
1409 in order to initialize other data structure based on them.
|
|
1410
|
|
1411 This hook runs even if there were no local variables or if their
|
|
1412 evaluation was suppressed. See also `enable-local-variables' and
|
|
1413 `enable-local-eval'.")
|
|
1414
|
|
1415 (defun hack-local-variables (&optional force)
|
|
1416 "Parse, and bind or evaluate as appropriate, any local variables
|
|
1417 for current buffer."
|
|
1418 ;; Don't look for -*- if this file name matches any
|
|
1419 ;; of the regexps in inhibit-first-line-modes-regexps.
|
|
1420 (if (or (null buffer-file-name) ; don't lose if buffer has no file!
|
|
1421 (not (let ((temp inhibit-first-line-modes-regexps)
|
|
1422 (name (if buffer-file-name
|
|
1423 (file-name-sans-versions buffer-file-name)
|
|
1424 (buffer-name))))
|
|
1425 (while (let ((sufs inhibit-first-line-modes-suffixes))
|
|
1426 (while (and sufs (not
|
|
1427 (string-match (car sufs) name)))
|
|
1428 (setq sufs (cdr sufs)))
|
|
1429 sufs)
|
|
1430 (setq name (substring name 0 (match-beginning 0))))
|
|
1431 (while (and temp
|
|
1432 (not (string-match (car temp) name)))
|
|
1433 (setq temp (cdr temp))
|
|
1434 temp))))
|
|
1435 (progn
|
|
1436 ;; Look for variables in the -*- line.
|
|
1437 (hack-local-variables-prop-line force)
|
|
1438 ;; Look for "Local variables:" block in last page.
|
|
1439 (hack-local-variables-last-page force)))
|
|
1440 (run-hooks 'hack-local-variables-hook))
|
|
1441
|
|
1442 ;;; Local variables may be specified in the last page of the file (within 3k
|
|
1443 ;;; from the end of the file and after the last ^L) in the form
|
|
1444 ;;;
|
|
1445 ;;; Local variables:
|
|
1446 ;;; variable-name: variable-value
|
|
1447 ;;; end:
|
|
1448 ;;;
|
|
1449 ;;; The lines may begin with a common prefix, like ";;; " in the above
|
|
1450 ;;; example. They may also have a common suffix (" */" for example). In
|
|
1451 ;;; this form, the local variable "mode" can be used to change the major
|
|
1452 ;;; mode, and the local variable "eval" can be used to evaluate an arbitrary
|
|
1453 ;;; form.
|
|
1454 ;;;
|
|
1455 ;;; Local variables may also be specified in the first line of the file.
|
|
1456 ;;; Embedded in this line are a pair of "-*-" sequences. What lies between
|
|
1457 ;;; them are variable-name/variable-value pairs, like:
|
|
1458 ;;;
|
|
1459 ;;; -*- mode: emacs-lisp -*-
|
|
1460 ;;; or -*- mode: postscript; version-control: never -*-
|
|
1461 ;;; or -*- tags-file-name: "/foo/bar/TAGS" -*-
|
|
1462 ;;;
|
|
1463 ;;; The local variable "eval" is not used with this form. For hysterical
|
|
1464 ;;; reasons, the syntax "-*- modename -*-" is allowed as well.
|
|
1465 ;;;
|
|
1466
|
|
1467 (defun hack-local-variables-p (modeline)
|
|
1468 (or (eq enable-local-variables t)
|
|
1469 (and enable-local-variables
|
|
1470 (save-window-excursion
|
|
1471 (condition-case nil
|
|
1472 (switch-to-buffer (current-buffer))
|
|
1473 (error
|
|
1474 ;; If we fail to switch in the selected window,
|
|
1475 ;; it is probably a minibuffer.
|
|
1476 ;; So try another window.
|
|
1477 (condition-case nil
|
|
1478 (switch-to-buffer-other-window (current-buffer))
|
|
1479 (error
|
|
1480 (switch-to-buffer-other-frame (current-buffer))))))
|
|
1481 (or modeline (save-excursion
|
|
1482 (beginning-of-line)
|
|
1483 (set-window-start (selected-window) (point))))
|
|
1484 (y-or-n-p (format
|
|
1485 "Set local variables as specified %s of %s? "
|
|
1486 (if modeline "in -*- line" "at end")
|
|
1487 (if buffer-file-name
|
|
1488 (file-name-nondirectory buffer-file-name)
|
|
1489 (concat "buffer " (buffer-name)))))))))
|
|
1490
|
|
1491 (defun hack-local-variables-last-page (&optional force)
|
|
1492 ;; Set local variables set in the "Local Variables:" block of the last page.
|
|
1493 (save-excursion
|
|
1494 (goto-char (point-max))
|
|
1495 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
|
|
1496 (if (let ((case-fold-search t))
|
|
1497 (and (search-forward "Local Variables:" nil t)
|
|
1498 (or force
|
|
1499 (hack-local-variables-p nil))))
|
|
1500 (let ((continue t)
|
|
1501 prefix prefixlen suffix beg
|
|
1502 (enable-local-eval enable-local-eval))
|
|
1503 ;; The prefix is what comes before "local variables:" in its line.
|
|
1504 ;; The suffix is what comes after "local variables:" in its line.
|
|
1505 (skip-chars-forward " \t")
|
|
1506 (or (eolp)
|
|
1507 (setq suffix (buffer-substring (point)
|
|
1508 (progn (end-of-line) (point)))))
|
|
1509 (goto-char (match-beginning 0))
|
|
1510 (or (bolp)
|
|
1511 (setq prefix
|
|
1512 (buffer-substring (point)
|
|
1513 (progn (beginning-of-line) (point)))))
|
|
1514 (if prefix (setq prefixlen (length prefix)
|
|
1515 prefix (regexp-quote prefix)))
|
|
1516 (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
|
|
1517 (while continue
|
|
1518 ;; Look at next local variable spec.
|
|
1519 (if selective-display (re-search-forward "[\n\C-m]")
|
|
1520 (forward-line 1))
|
|
1521 ;; Skip the prefix, if any.
|
|
1522 (if prefix
|
|
1523 (if (looking-at prefix)
|
|
1524 (forward-char prefixlen)
|
|
1525 (error "Local variables entry is missing the prefix")))
|
|
1526 ;; Find the variable name; strip whitespace.
|
|
1527 (skip-chars-forward " \t")
|
|
1528 (setq beg (point))
|
|
1529 (skip-chars-forward "^:\n")
|
|
1530 (if (eolp) (error "Missing colon in local variables entry"))
|
|
1531 (skip-chars-backward " \t")
|
|
1532 (let* ((str (buffer-substring beg (point)))
|
|
1533 (var (read str))
|
|
1534 val)
|
|
1535 ;; Setting variable named "end" means end of list.
|
|
1536 (if (string-equal (downcase str) "end")
|
|
1537 (setq continue nil)
|
|
1538 ;; Otherwise read the variable value.
|
|
1539 (skip-chars-forward "^:")
|
|
1540 (forward-char 1)
|
|
1541 (setq val (read (current-buffer)))
|
|
1542 (skip-chars-backward "\n")
|
|
1543 (skip-chars-forward " \t")
|
|
1544 (or (if suffix (looking-at suffix) (eolp))
|
|
1545 (error "Local variables entry is terminated incorrectly"))
|
|
1546 ;; Set the variable. "Variables" mode and eval are funny.
|
|
1547 (hack-one-local-variable var val))))))))
|
|
1548
|
|
1549 ;; jwz - New Version 20.1/19.15
|
|
1550 (defun hack-local-variables-prop-line (&optional force)
|
|
1551 ;; Set local variables specified in the -*- line.
|
|
1552 ;; Returns t if mode was set.
|
|
1553 (let ((result nil))
|
|
1554 (save-excursion
|
|
1555 (goto-char (point-min))
|
|
1556 (skip-chars-forward " \t\n\r")
|
|
1557 (let ((end (save-excursion
|
|
1558 ;; If the file begins with "#!"
|
|
1559 ;; (un*x exec interpreter magic), look
|
|
1560 ;; for mode frobs in the first two
|
|
1561 ;; lines. You cannot necessarily
|
|
1562 ;; put them in the first line of
|
|
1563 ;; such a file without screwing up
|
|
1564 ;; the interpreter invocation.
|
|
1565 (end-of-line (and (looking-at "^#!") 2))
|
|
1566 (point))))
|
|
1567 ;; Parse the -*- line into the `result' alist.
|
|
1568 (cond ((not (search-forward "-*-" end t))
|
|
1569 ;; doesn't have one.
|
|
1570 (setq force t))
|
|
1571 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
|
|
1572 ;; Antiquated form: "-*- ModeName -*-".
|
|
1573 (setq result
|
|
1574 (list (cons 'mode
|
|
1575 (intern (buffer-substring
|
|
1576 (match-beginning 1)
|
|
1577 (match-end 1)))))
|
|
1578 ))
|
|
1579 (t
|
|
1580 ;; Usual form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
|
|
1581 ;; (last ";" is optional).
|
|
1582 (save-excursion
|
|
1583 (if (search-forward "-*-" end t)
|
|
1584 (setq end (- (point) 3))
|
|
1585 (error "-*- not terminated before end of line")))
|
|
1586 (while (< (point) end)
|
|
1587 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
|
|
1588 (error "malformed -*- line"))
|
|
1589 (goto-char (match-end 0))
|
|
1590 ;; There used to be a downcase here,
|
|
1591 ;; but the manual didn't say so,
|
|
1592 ;; and people want to set var names that aren't all lc.
|
|
1593 (let ((key (intern (buffer-substring
|
|
1594 (match-beginning 1)
|
|
1595 (match-end 1))))
|
|
1596 (val (save-restriction
|
|
1597 (narrow-to-region (point) end)
|
|
1598 (read (current-buffer)))))
|
|
1599 ;; Case sensitivity! Icepicks in my forehead!
|
|
1600 (if (equal (downcase (symbol-name key)) "mode")
|
|
1601 (setq key 'mode))
|
|
1602 (setq result (cons (cons key val) result))
|
|
1603 (skip-chars-forward " \t;")))
|
|
1604 (setq result (nreverse result))))))
|
|
1605
|
219
|
1606 (let ((set-any-p (or force
|
|
1607 ;; It's OK to force null specifications.
|
|
1608 (null result)
|
|
1609 ;; It's OK to force mode-only specifications.
|
|
1610 (let ((remaining result)
|
|
1611 (mode-specs-only t))
|
|
1612 (while remaining
|
|
1613 (if (eq (car (car remaining)) 'mode)
|
|
1614 (setq remaining (cdr remaining))
|
|
1615 ;; Otherwise, we have a real local.
|
|
1616 (setq mode-specs-only nil
|
|
1617 remaining nil))
|
|
1618 )
|
|
1619 mode-specs-only)
|
|
1620 ;; Otherwise, check.
|
|
1621 (hack-local-variables-p t)))
|
209
|
1622 (mode-p nil))
|
|
1623 (while result
|
|
1624 (let ((key (car (car result)))
|
|
1625 (val (cdr (car result))))
|
|
1626 (cond ((eq key 'mode)
|
219
|
1627 (setq mode-p t)
|
|
1628 (let ((mode (intern (concat (downcase (symbol-name val))
|
|
1629 "-mode"))))
|
|
1630 ;; Without this guard, `normal-mode' would potentially run
|
|
1631 ;; the major mode function twice: once via `set-auto-mode'
|
|
1632 ;; and once via `hack-local-variables'.
|
|
1633 (if (not (eq mode major-mode))
|
|
1634 (funcall mode))
|
|
1635 ))
|
209
|
1636 (set-any-p
|
|
1637 (hack-one-local-variable key val))
|
|
1638 (t
|
|
1639 nil)))
|
|
1640 (setq result (cdr result)))
|
|
1641 mode-p)))
|
|
1642
|
|
1643 (defconst ignored-local-variables
|
|
1644 (list 'enable-local-eval)
|
|
1645 "Variables to be ignored in a file's local variable spec.")
|
|
1646
|
|
1647 ;; Get confirmation before setting these variables as locals in a file.
|
|
1648 (put 'debugger 'risky-local-variable t)
|
|
1649 (put 'enable-local-eval 'risky-local-variable t)
|
|
1650 (put 'ignored-local-variables 'risky-local-variable t)
|
|
1651 (put 'eval 'risky-local-variable t)
|
|
1652 (put 'file-name-handler-alist 'risky-local-variable t)
|
|
1653 (put 'minor-mode-map-alist 'risky-local-variable t)
|
|
1654 (put 'after-load-alist 'risky-local-variable t)
|
|
1655 (put 'buffer-file-name 'risky-local-variable t)
|
|
1656 (put 'buffer-auto-save-file-name 'risky-local-variable t)
|
|
1657 (put 'buffer-file-truename 'risky-local-variable t)
|
|
1658 (put 'exec-path 'risky-local-variable t)
|
|
1659 (put 'load-path 'risky-local-variable t)
|
|
1660 (put 'exec-directory 'risky-local-variable t)
|
|
1661 (put 'process-environment 'risky-local-variable t)
|
|
1662 ;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode.
|
|
1663 (put 'outline-level 'risky-local-variable t)
|
|
1664 (put 'rmail-output-file-alist 'risky-local-variable t)
|
|
1665
|
|
1666 ;; This one is safe because the user gets to check it before it is used.
|
|
1667 (put 'compile-command 'safe-local-variable t)
|
|
1668
|
|
1669 ;(defun hack-one-local-variable-quotep (exp)
|
|
1670 ; (and (consp exp) (eq (car exp) 'quote) (consp (cdr exp))))
|
|
1671
|
|
1672 ;; "Set" one variable in a local variables spec.
|
|
1673 ;; A few variable names are treated specially.
|
|
1674 (defun hack-one-local-variable (var val)
|
|
1675 (cond ((eq var 'mode)
|
|
1676 (funcall (intern (concat (downcase (symbol-name val))
|
|
1677 "-mode"))))
|
|
1678 ((memq var ignored-local-variables)
|
|
1679 nil)
|
|
1680 ;; "Setting" eval means either eval it or do nothing.
|
|
1681 ;; Likewise for setting hook variables.
|
|
1682 ((or (get var 'risky-local-variable)
|
|
1683 (and
|
|
1684 (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$"
|
|
1685 (symbol-name var))
|
|
1686 (not (get var 'safe-local-variable))))
|
|
1687 ; ;; Permit evaling a put of a harmless property
|
|
1688 ; ;; if the args do nothing tricky.
|
|
1689 ; (if (or (and (eq var 'eval)
|
|
1690 ; (consp val)
|
|
1691 ; (eq (car val) 'put)
|
|
1692 ; (hack-one-local-variable-quotep (nth 1 val))
|
|
1693 ; (hack-one-local-variable-quotep (nth 2 val))
|
|
1694 ; ;; Only allow safe values of lisp-indent-hook;
|
|
1695 ; ;; not functions.
|
|
1696 ; (or (numberp (nth 3 val))
|
|
1697 ; (equal (nth 3 val) ''defun))
|
|
1698 ; (memq (nth 1 (nth 2 val))
|
|
1699 ; '(lisp-indent-hook)))
|
|
1700 (if (and (not (zerop (user-uid)))
|
|
1701 (or (eq enable-local-eval t)
|
|
1702 (and enable-local-eval
|
|
1703 (save-window-excursion
|
|
1704 (switch-to-buffer (current-buffer))
|
|
1705 (save-excursion
|
|
1706 (beginning-of-line)
|
|
1707 (set-window-start (selected-window) (point)))
|
|
1708 (setq enable-local-eval
|
|
1709 (y-or-n-p (format "Process `eval' or hook local variables in file %s? "
|
|
1710 (file-name-nondirectory buffer-file-name))))))))
|
|
1711 (if (eq var 'eval)
|
|
1712 (save-excursion (eval val))
|
|
1713 (make-local-variable var)
|
|
1714 (set var val))
|
|
1715 (message "Ignoring `eval:' in file's local variables")))
|
|
1716 ;; Ordinary variable, really set it.
|
|
1717 (t (make-local-variable var)
|
|
1718 (set var val))))
|
|
1719
|
|
1720 (defun set-visited-file-name (filename)
|
|
1721 "Change name of file visited in current buffer to FILENAME.
|
|
1722 The next time the buffer is saved it will go in the newly specified file.
|
|
1723 nil or empty string as argument means make buffer not be visiting any file.
|
|
1724 Remember to delete the initial contents of the minibuffer
|
|
1725 if you wish to pass an empty string as the argument."
|
|
1726 (interactive "FSet visited file name: ")
|
|
1727 (if (buffer-base-buffer)
|
|
1728 (error "An indirect buffer cannot visit a file"))
|
|
1729 (let (truename)
|
|
1730 (if filename
|
|
1731 (setq filename
|
|
1732 (if (string-equal filename "")
|
|
1733 nil
|
|
1734 (expand-file-name filename))))
|
|
1735 (if filename
|
|
1736 (progn
|
|
1737 (setq truename (file-truename filename))
|
|
1738 ;; #### Do we need to check if truename is non-nil?
|
|
1739 (if find-file-use-truenames
|
|
1740 (setq filename truename))))
|
|
1741 (or (equal filename buffer-file-name)
|
|
1742 (progn
|
|
1743 (and filename (lock-buffer filename))
|
|
1744 (unlock-buffer)))
|
|
1745 (setq buffer-file-name filename)
|
|
1746 (if filename ; make buffer name reflect filename.
|
|
1747 (let ((new-name (file-name-nondirectory buffer-file-name)))
|
|
1748 (if (string= new-name "")
|
|
1749 (error "Empty file name"))
|
|
1750 (if (eq system-type 'vax-vms)
|
|
1751 (setq new-name (downcase new-name)))
|
|
1752 (setq default-directory (file-name-directory buffer-file-name))
|
|
1753 (or (string= new-name (buffer-name))
|
|
1754 (rename-buffer new-name t))))
|
|
1755 (setq buffer-backed-up nil)
|
|
1756 (clear-visited-file-modtime)
|
|
1757 (compute-buffer-file-truename) ; insert-file-contents does this too.
|
|
1758 ; ;; Abbreviate the file names of the buffer.
|
|
1759 ; (if truename
|
|
1760 ; (progn
|
|
1761 ; (setq buffer-file-truename (abbreviate-file-name truename))
|
|
1762 ; (if find-file-visit-truename
|
|
1763 ; (setq buffer-file-name buffer-file-truename))))
|
|
1764 (setq buffer-file-number
|
|
1765 (if filename
|
|
1766 (nthcdr 10 (file-attributes buffer-file-name))
|
|
1767 nil)))
|
|
1768 ;; write-file-hooks is normally used for things like ftp-find-file
|
|
1769 ;; that visit things that are not local files as if they were files.
|
|
1770 ;; Changing to visit an ordinary local file instead should flush the hook.
|
|
1771 (kill-local-variable 'write-file-hooks)
|
|
1772 (kill-local-variable 'after-save-hook)
|
|
1773 (kill-local-variable 'local-write-file-hooks)
|
|
1774 (kill-local-variable 'write-file-data-hooks)
|
|
1775 (kill-local-variable 'revert-buffer-function)
|
|
1776 (kill-local-variable 'backup-inhibited)
|
|
1777 ;; If buffer was read-only because of version control,
|
|
1778 ;; that reason is gone now, so make it writable.
|
|
1779 (if (and (boundp 'vc-mode) vc-mode)
|
|
1780 (setq buffer-read-only nil))
|
|
1781 (kill-local-variable 'vc-mode)
|
|
1782 ;; Turn off backup files for certain file names.
|
|
1783 ;; Since this is a permanent local, the major mode won't eliminate it.
|
|
1784 (and (not (funcall backup-enable-predicate buffer-file-name))
|
|
1785 (progn
|
|
1786 (make-local-variable 'backup-inhibited)
|
|
1787 (setq backup-inhibited t)))
|
|
1788 (let ((oauto buffer-auto-save-file-name))
|
|
1789 ;; If auto-save was not already on, turn it on if appropriate.
|
|
1790 (if (not buffer-auto-save-file-name)
|
|
1791 (and buffer-file-name auto-save-default
|
|
1792 (auto-save-mode t))
|
|
1793 ;; If auto save is on, start using a new name.
|
|
1794 ;; We deliberately don't rename or delete the old auto save
|
|
1795 ;; for the old visited file name. This is because perhaps
|
|
1796 ;; the user wants to save the new state and then compare with the
|
|
1797 ;; previous state from the auto save file.
|
|
1798 (setq buffer-auto-save-file-name
|
|
1799 (make-auto-save-file-name)))
|
|
1800 ;; Rename the old auto save file if any.
|
|
1801 (and oauto buffer-auto-save-file-name
|
|
1802 (file-exists-p oauto)
|
|
1803 (rename-file oauto buffer-auto-save-file-name t)))
|
|
1804 (if buffer-file-name
|
|
1805 (set-buffer-modified-p t))
|
|
1806 ;; #### ??
|
|
1807 (run-hooks 'after-set-visited-file-name-hooks))
|
|
1808
|
|
1809 (defun write-file (filename &optional confirm codesys)
|
|
1810 "Write current buffer into file FILENAME.
|
|
1811 Makes buffer visit that file, and marks it not modified.
|
|
1812 If the buffer is already visiting a file, you can specify
|
|
1813 a directory name as FILENAME, to write a file of the same
|
|
1814 old name in that directory.
|
|
1815 If optional second arg CONFIRM is non-nil,
|
|
1816 ask for confirmation for overwriting an existing file.
|
|
1817 Under XEmacs/Mule, optional third argument specifies the
|
|
1818 coding system to use when encoding the file. Interactively,
|
|
1819 with a prefix argument, you will be prompted for the coding system."
|
|
1820 ;; (interactive "FWrite file: ")
|
|
1821 (interactive
|
|
1822 (list (if buffer-file-name
|
|
1823 (read-file-name "Write file: "
|
|
1824 nil nil nil nil)
|
|
1825 (read-file-name "Write file: "
|
|
1826 (cdr (assq 'default-directory
|
|
1827 (buffer-local-variables)))
|
|
1828 nil nil (buffer-name)))
|
|
1829 t
|
|
1830 (if (and current-prefix-arg (featurep 'mule))
|
|
1831 (read-coding-system "Coding system: "))))
|
|
1832 (and (eq (current-buffer) mouse-grabbed-buffer)
|
|
1833 (error "Can't write minibuffer window"))
|
|
1834 (or (null filename) (string-equal filename "")
|
|
1835 (progn
|
|
1836 ;; If arg is just a directory,
|
|
1837 ;; use same file name, but in that directory.
|
|
1838 (if (and (file-directory-p filename) buffer-file-name)
|
|
1839 (setq filename (concat (file-name-as-directory filename)
|
|
1840 (file-name-nondirectory buffer-file-name))))
|
|
1841 (and confirm
|
|
1842 (file-exists-p filename)
|
|
1843 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
|
|
1844 (error "Canceled")))
|
|
1845 (set-visited-file-name filename)))
|
|
1846 (set-buffer-modified-p t)
|
|
1847 (setq buffer-read-only nil)
|
|
1848 (if codesys
|
|
1849 (let ((buffer-file-coding-system (get-coding-system codesys)))
|
|
1850 (save-buffer))
|
|
1851 (save-buffer)))
|
|
1852
|
|
1853 (defun backup-buffer ()
|
|
1854 "Make a backup of the disk file visited by the current buffer, if appropriate.
|
|
1855 This is normally done before saving the buffer the first time.
|
|
1856 If the value is non-nil, it is the result of `file-modes' on the original file;
|
|
1857 this means that the caller, after saving the buffer, should change the modes
|
|
1858 of the new file to agree with the old modes."
|
|
1859 (if buffer-file-name
|
|
1860 (let ((handler (find-file-name-handler buffer-file-name 'backup-buffer)))
|
|
1861 (if handler
|
|
1862 (funcall handler 'backup-buffer)
|
|
1863 (if (and make-backup-files
|
|
1864 (not backup-inhibited)
|
|
1865 (not buffer-backed-up)
|
|
1866 (file-exists-p buffer-file-name)
|
|
1867 (memq (aref (elt (file-attributes buffer-file-name) 8) 0)
|
|
1868 '(?- ?l)))
|
|
1869 (let ((real-file-name buffer-file-name)
|
|
1870 backup-info backupname targets setmodes)
|
|
1871 ;; If specified name is a symbolic link, chase it to the target.
|
|
1872 ;; Thus we make the backups in the directory where the real file is.
|
|
1873 (setq real-file-name (file-chase-links real-file-name))
|
|
1874 (setq backup-info (find-backup-file-name real-file-name)
|
|
1875 backupname (car backup-info)
|
|
1876 targets (cdr backup-info))
|
|
1877 ;;; (if (file-directory-p buffer-file-name)
|
|
1878 ;;; (error "Cannot save buffer in directory %s" buffer-file-name))
|
|
1879 (if backup-info
|
|
1880 (condition-case ()
|
|
1881 (let ((delete-old-versions
|
|
1882 ;; If have old versions to maybe delete,
|
|
1883 ;; ask the user to confirm now, before doing anything.
|
|
1884 ;; But don't actually delete til later.
|
|
1885 (and targets
|
|
1886 (or (eq delete-old-versions t)
|
|
1887 (eq delete-old-versions nil))
|
|
1888 (or delete-old-versions
|
|
1889 (y-or-n-p (format "Delete excess backup versions of %s? "
|
|
1890 real-file-name))))))
|
|
1891 ;; Actually write the back up file.
|
|
1892 (condition-case ()
|
|
1893 (if (or file-precious-flag
|
|
1894 ; (file-symlink-p buffer-file-name)
|
|
1895 backup-by-copying
|
|
1896 (and backup-by-copying-when-linked
|
|
1897 (> (file-nlinks real-file-name) 1))
|
|
1898 (and backup-by-copying-when-mismatch
|
|
1899 (let ((attr (file-attributes real-file-name)))
|
|
1900 (or (nth 9 attr)
|
|
1901 (not (file-ownership-preserved-p real-file-name))))))
|
|
1902 (condition-case ()
|
|
1903 (copy-file real-file-name backupname t t)
|
|
1904 (file-error
|
|
1905 ;; If copying fails because file BACKUPNAME
|
|
1906 ;; is not writable, delete that file and try again.
|
|
1907 (if (and (file-exists-p backupname)
|
|
1908 (not (file-writable-p backupname)))
|
|
1909 (delete-file backupname))
|
|
1910 (copy-file real-file-name backupname t t)))
|
|
1911 ;; rename-file should delete old backup.
|
|
1912 (rename-file real-file-name backupname t)
|
|
1913 (setq setmodes (file-modes backupname)))
|
|
1914 (file-error
|
|
1915 ;; If trouble writing the backup, write it in ~.
|
|
1916 (setq backupname (expand-file-name "~/%backup%~"))
|
|
1917 (message "Cannot write backup file; backing up in ~/%%backup%%~")
|
|
1918 (sleep-for 1)
|
|
1919 (condition-case ()
|
|
1920 (copy-file real-file-name backupname t t)
|
|
1921 (file-error
|
|
1922 ;; If copying fails because file BACKUPNAME
|
|
1923 ;; is not writable, delete that file and try again.
|
|
1924 (if (and (file-exists-p backupname)
|
|
1925 (not (file-writable-p backupname)))
|
|
1926 (delete-file backupname))
|
|
1927 (copy-file real-file-name backupname t t)))))
|
|
1928 (setq buffer-backed-up t)
|
|
1929 ;; Now delete the old versions, if desired.
|
|
1930 (if delete-old-versions
|
|
1931 (while targets
|
|
1932 (condition-case ()
|
|
1933 (delete-file (car targets))
|
|
1934 (file-error nil))
|
|
1935 (setq targets (cdr targets))))
|
|
1936 setmodes)
|
|
1937 (file-error nil)))))))))
|
|
1938
|
|
1939 (defun file-name-sans-versions (name &optional keep-backup-version)
|
|
1940 "Return FILENAME sans backup versions or strings.
|
|
1941 This is a separate procedure so your site-init or startup file can
|
|
1942 redefine it.
|
|
1943 If the optional argument KEEP-BACKUP-VERSION is non-nil,
|
|
1944 we do not remove backup version numbers, only true file version numbers."
|
|
1945 (let ((handler (find-file-name-handler name 'file-name-sans-versions)))
|
|
1946 (if handler
|
|
1947 (funcall handler 'file-name-sans-versions name keep-backup-version)
|
|
1948 (substring name 0
|
|
1949 (if (eq system-type 'vax-vms)
|
|
1950 ;; VMS version number is (a) semicolon, optional
|
|
1951 ;; sign, zero or more digits or (b) period, option
|
|
1952 ;; sign, zero or more digits, provided this is the
|
|
1953 ;; second period encountered outside of the
|
|
1954 ;; device/directory part of the file name.
|
|
1955 (or (string-match ";[-+]?[0-9]*\\'" name)
|
|
1956 (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'"
|
|
1957 name)
|
|
1958 (match-beginning 1))
|
|
1959 (length name))
|
|
1960 (if keep-backup-version
|
|
1961 (length name)
|
|
1962 (or (string-match "\\.~[0-9.]+~\\'" name)
|
|
1963 ;; XEmacs - VC uses extensions like ".~tagname~" or ".~1.1.5.2~"
|
|
1964 (let ((pos (string-match "\\.~\\([^.~ \t]+\\|[0-9.]+\\)~\\'" name)))
|
|
1965 (and pos
|
|
1966 ;; #### - is this filesystem check too paranoid?
|
|
1967 (file-exists-p (substring name 0 pos))
|
|
1968 pos))
|
|
1969 (string-match "~\\'" name)
|
|
1970 (length name))))))))
|
|
1971
|
|
1972 (defun file-ownership-preserved-p (file)
|
|
1973 "Returns t if deleting FILE and rewriting it would preserve the owner."
|
|
1974 (let ((handler (find-file-name-handler file 'file-ownership-preserved-p)))
|
|
1975 (if handler
|
|
1976 (funcall handler 'file-ownership-preserved-p file)
|
|
1977 (let ((attributes (file-attributes file)))
|
|
1978 ;; Return t if the file doesn't exist, since it's true that no
|
|
1979 ;; information would be lost by an (attempted) delete and create.
|
|
1980 (or (null attributes)
|
|
1981 (= (nth 2 attributes) (user-uid)))))))
|
|
1982
|
|
1983 (defun file-name-sans-extension (filename)
|
|
1984 "Return FILENAME sans final \"extension\".
|
|
1985 The extension, in a file name, is the part that follows the last `.'."
|
|
1986 (save-match-data
|
|
1987 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
|
|
1988 directory)
|
|
1989 (if (string-match "\\.[^.]*\\'" file)
|
|
1990 (if (setq directory (file-name-directory filename))
|
|
1991 (expand-file-name (substring file 0 (match-beginning 0))
|
|
1992 directory)
|
|
1993 (substring file 0 (match-beginning 0)))
|
|
1994 filename))))
|
|
1995
|
|
1996 (defun make-backup-file-name (file)
|
|
1997 "Create the non-numeric backup file name for FILE.
|
|
1998 This is a separate function so you can redefine it for customization."
|
|
1999 (if (eq system-type 'ms-dos)
|
|
2000 (let ((fn (file-name-nondirectory file)))
|
|
2001 (concat (file-name-directory file)
|
|
2002 (if (string-match "\\([^.]*\\)\\(\\..*\\)?" fn)
|
|
2003 (substring fn 0 (match-end 1)))
|
|
2004 ".bak"))
|
|
2005 (concat file "~")))
|
|
2006
|
|
2007 (defun backup-file-name-p (file)
|
|
2008 "Return non-nil if FILE is a backup file name (numeric or not).
|
|
2009 This is a separate function so you can redefine it for customization.
|
|
2010 You may need to redefine `file-name-sans-versions' as well."
|
|
2011 (if (eq system-type 'ms-dos)
|
|
2012 (string-match "\\.bak\\'" file)
|
|
2013 (string-match "~\\'" file)))
|
|
2014
|
|
2015 ;; This is used in various files.
|
|
2016 ;; The usage of bv-length is not very clean,
|
|
2017 ;; but I can't see a good alternative,
|
|
2018 ;; so as of now I am leaving it alone.
|
|
2019 (defun backup-extract-version (fn)
|
|
2020 "Given the name of a numeric backup file, return the backup number.
|
|
2021 Uses the free variable `bv-length', whose value should be
|
|
2022 the index in the name where the version number begins."
|
|
2023 (declare (special bv-length))
|
|
2024 (if (and (string-match "[0-9]+~\\'" fn bv-length)
|
|
2025 (= (match-beginning 0) bv-length))
|
|
2026 (string-to-int (substring fn bv-length -1))
|
|
2027 0))
|
|
2028
|
|
2029 ;; I believe there is no need to alter this behavior for VMS;
|
|
2030 ;; since backup files are not made on VMS, it should not get called.
|
|
2031 (defun find-backup-file-name (fn)
|
|
2032 "Find a file name for a backup file, and suggestions for deletions.
|
|
2033 Value is a list whose car is the name for the backup file
|
|
2034 and whose cdr is a list of old versions to consider deleting now.
|
|
2035 If the value is nil, don't make a backup."
|
|
2036 (let ((handler (find-file-name-handler fn 'find-backup-file-name)))
|
|
2037 ;; Run a handler for this function so that ange-ftp can refuse to do it.
|
|
2038 (if handler
|
|
2039 (funcall handler 'find-backup-file-name fn)
|
|
2040 (if (eq version-control 'never)
|
|
2041 (list (make-backup-file-name fn))
|
|
2042 (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
|
|
2043 ;; used by backup-extract-version:
|
|
2044 (bv-length (length base-versions))
|
|
2045 possibilities
|
|
2046 (versions nil)
|
|
2047 (high-water-mark 0)
|
|
2048 (deserve-versions-p nil)
|
|
2049 (number-to-delete 0))
|
|
2050 (condition-case ()
|
|
2051 (setq possibilities (file-name-all-completions
|
|
2052 base-versions
|
|
2053 (file-name-directory fn))
|
|
2054 versions (sort (mapcar
|
|
2055 #'backup-extract-version
|
|
2056 possibilities)
|
|
2057 '<)
|
|
2058 high-water-mark (apply #'max 0 versions)
|
|
2059 deserve-versions-p (or version-control
|
|
2060 (> high-water-mark 0))
|
|
2061 number-to-delete (- (length versions)
|
|
2062 kept-old-versions kept-new-versions -1))
|
|
2063 (file-error
|
|
2064 (setq possibilities nil)))
|
|
2065 (if (not deserve-versions-p)
|
|
2066 (list (make-backup-file-name fn))
|
|
2067 (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~")
|
|
2068 (if (and (> number-to-delete 0)
|
|
2069 ;; Delete nothing if there is overflow
|
|
2070 ;; in the number of versions to keep.
|
|
2071 (>= (+ kept-new-versions kept-old-versions -1) 0))
|
|
2072 (mapcar #'(lambda (n)
|
|
2073 (concat fn ".~" (int-to-string n) "~"))
|
|
2074 (let ((v (nthcdr kept-old-versions versions)))
|
|
2075 (rplacd (nthcdr (1- number-to-delete) v) ())
|
|
2076 v))))))))))
|
|
2077
|
|
2078 (defun file-nlinks (filename)
|
|
2079 "Return number of names file FILENAME has."
|
|
2080 (car (cdr (file-attributes filename))))
|
|
2081
|
|
2082 (defun file-relative-name (filename &optional directory)
|
|
2083 "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
|
|
2084 (setq filename (expand-file-name filename)
|
|
2085 directory (file-name-as-directory (expand-file-name
|
|
2086 (or directory default-directory))))
|
|
2087 (let ((ancestor ""))
|
|
2088 (while (not (string-match (concat "^" (regexp-quote directory)) filename))
|
|
2089 (setq directory (file-name-directory (substring directory 0 -1))
|
|
2090 ancestor (concat "../" ancestor)))
|
|
2091 (concat ancestor (substring filename (match-end 0)))))
|
|
2092
|
|
2093 (defun save-buffer (&optional args)
|
|
2094 "Save current buffer in visited file if modified. Versions described below.
|
|
2095
|
|
2096 By default, makes the previous version into a backup file
|
|
2097 if previously requested or if this is the first save.
|
|
2098 With 1 or 3 \\[universal-argument]'s, marks this version
|
|
2099 to become a backup when the next save is done.
|
|
2100 With 2 or 3 \\[universal-argument]'s,
|
|
2101 unconditionally makes the previous version into a backup file.
|
|
2102 With argument of 0, never makes the previous version into a backup file.
|
|
2103
|
|
2104 If a file's name is FOO, the names of its numbered backup versions are
|
|
2105 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
|
|
2106 Numeric backups (rather than FOO~) will be made if value of
|
|
2107 `version-control' is not the atom `never' and either there are already
|
|
2108 numeric versions of the file being backed up, or `version-control' is
|
|
2109 non-nil.
|
|
2110 We don't want excessive versions piling up, so there are variables
|
|
2111 `kept-old-versions', which tells XEmacs how many oldest versions to keep,
|
|
2112 and `kept-new-versions', which tells how many newest versions to keep.
|
|
2113 Defaults are 2 old versions and 2 new.
|
|
2114 `dired-kept-versions' controls dired's clean-directory (.) command.
|
|
2115 If `delete-old-versions' is nil, system will query user
|
|
2116 before trimming versions. Otherwise it does it silently."
|
|
2117 (interactive "_p")
|
|
2118 (let ((modp (buffer-modified-p))
|
|
2119 (large (> (buffer-size) 50000))
|
|
2120 (make-backup-files (or (and make-backup-files (not (eq args 0)))
|
|
2121 (memq args '(16 64)))))
|
|
2122 (and modp (memq args '(16 64)) (setq buffer-backed-up nil))
|
|
2123 (if (and modp large) (display-message
|
|
2124 'progress (format "Saving file %s..."
|
|
2125 (buffer-file-name))))
|
|
2126 (basic-save-buffer)
|
|
2127 (and modp (memq args '(4 64)) (setq buffer-backed-up nil))))
|
|
2128
|
|
2129 (defun delete-auto-save-file-if-necessary (&optional force)
|
|
2130 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
|
|
2131 Normally delete only if the file was written by this XEmacs
|
|
2132 since the last real save, but optional arg FORCE non-nil means delete anyway."
|
|
2133 (and buffer-auto-save-file-name delete-auto-save-files
|
|
2134 (not (string= buffer-file-name buffer-auto-save-file-name))
|
|
2135 (or force (recent-auto-save-p))
|
|
2136 (progn
|
|
2137 (condition-case ()
|
|
2138 (delete-file buffer-auto-save-file-name)
|
|
2139 (file-error nil))
|
|
2140 (set-buffer-auto-saved))))
|
|
2141
|
|
2142 ;; XEmacs change (from Sun)
|
|
2143 ;; used to communicate with continue-save-buffer:
|
|
2144 (defvar continue-save-buffer-hooks-tail nil)
|
|
2145
|
|
2146 ;; Not in FSFmacs
|
|
2147 (defun basic-write-file-data (realname truename)
|
|
2148 ;; call the hooks until the bytes are put
|
|
2149 ;; call write-region as a last resort
|
|
2150 (let ((region-written nil)
|
|
2151 (hooks write-file-data-hooks))
|
|
2152 (while (and hooks (not region-written))
|
|
2153 (setq region-written (funcall (car hooks) realname)
|
|
2154 hooks (cdr hooks)))
|
|
2155 (if (not region-written)
|
|
2156 (write-region (point-min) (point-max) realname nil t truename))))
|
|
2157
|
|
2158 (put 'after-save-hook 'permanent-local t)
|
|
2159 (defvar after-save-hook nil
|
|
2160 "Normal hook that is run after a buffer is saved to its file.
|
|
2161 These hooks are considered to pertain to the visited file.
|
|
2162 So this list is cleared if you change the visited file name.")
|
|
2163
|
|
2164 (defun files-fetch-hook-value (hook)
|
|
2165 (let ((localval (symbol-value hook))
|
|
2166 (globalval (default-value hook)))
|
|
2167 (if (memq t localval)
|
|
2168 (setq localval (append (delq t localval) (delq t globalval))))
|
|
2169 localval))
|
|
2170
|
|
2171 (defun basic-save-buffer ()
|
|
2172 "Save the current buffer in its visited file, if it has been modified.
|
|
2173 After saving the buffer, run `after-save-hook'."
|
|
2174 (interactive)
|
|
2175 (save-excursion
|
|
2176 ;; In an indirect buffer, save its base buffer instead.
|
|
2177 (if (buffer-base-buffer)
|
|
2178 (set-buffer (buffer-base-buffer)))
|
|
2179 (if (buffer-modified-p)
|
|
2180 (let ((recent-save (recent-auto-save-p)))
|
|
2181 ;; On VMS, rename file and buffer to get rid of version number.
|
|
2182 (if (and (eq system-type 'vax-vms)
|
|
2183 (not (string= buffer-file-name
|
|
2184 (file-name-sans-versions buffer-file-name))))
|
|
2185 (let (buffer-new-name)
|
|
2186 ;; Strip VMS version number before save.
|
|
2187 (setq buffer-file-name
|
|
2188 (file-name-sans-versions buffer-file-name))
|
|
2189 ;; Construct a (unique) buffer name to correspond.
|
|
2190 (let ((buf (create-file-buffer (downcase buffer-file-name))))
|
|
2191 (setq buffer-new-name (buffer-name buf))
|
|
2192 (kill-buffer buf))
|
|
2193 (rename-buffer buffer-new-name)))
|
|
2194 ;; If buffer has no file name, ask user for one.
|
|
2195 (or buffer-file-name
|
|
2196 (let ((filename
|
|
2197 (expand-file-name
|
|
2198 (read-file-name "File to save in: ") nil)))
|
|
2199 (and (file-exists-p filename)
|
|
2200 (or (y-or-n-p (format "File `%s' exists; overwrite? "
|
|
2201 filename))
|
|
2202 (error "Canceled")))
|
|
2203 (set-visited-file-name filename)))
|
|
2204 (or (verify-visited-file-modtime (current-buffer))
|
|
2205 (not (file-exists-p buffer-file-name))
|
|
2206 (yes-or-no-p
|
|
2207 (format "%s has changed since visited or saved. Save anyway? "
|
|
2208 (file-name-nondirectory buffer-file-name)))
|
|
2209 (error "Save not confirmed"))
|
|
2210 (save-restriction
|
|
2211 (widen)
|
|
2212 (and (> (point-max) 1)
|
|
2213 (/= (char-after (1- (point-max))) ?\n)
|
|
2214 (not (and (eq selective-display t)
|
|
2215 (= (char-after (1- (point-max))) ?\r)))
|
|
2216 (or (eq require-final-newline t)
|
|
2217 (and require-final-newline
|
|
2218 (y-or-n-p
|
|
2219 (format "Buffer %s does not end in newline. Add one? "
|
|
2220 (buffer-name)))))
|
|
2221 (save-excursion
|
|
2222 (goto-char (point-max))
|
|
2223 (insert ?\n)))
|
|
2224 ;;
|
|
2225 ;; Run the write-file-hooks until one returns non-null.
|
|
2226 ;; Bind after-save-hook to nil while running the
|
|
2227 ;; write-file-hooks so that if this function is called
|
|
2228 ;; recursively (from inside a write-file-hook) the
|
|
2229 ;; after-hooks will only get run once (from the
|
|
2230 ;; outermost call).
|
|
2231 ;;
|
|
2232 ;; Ugh, have to duplicate logic of run-hook-with-args-until-success
|
|
2233 (let ((hooks (append (files-fetch-hook-value 'write-contents-hooks)
|
|
2234 (files-fetch-hook-value
|
|
2235 'local-write-file-hooks)
|
|
2236 (files-fetch-hook-value 'write-file-hooks)))
|
|
2237 (after-save-hook nil)
|
|
2238 (local-write-file-hooks nil)
|
|
2239 (write-contents-hooks nil)
|
|
2240 (write-file-hooks nil)
|
|
2241 done)
|
|
2242 (while (and hooks
|
|
2243 (let ((continue-save-buffer-hooks-tail hooks))
|
|
2244 (not (setq done (funcall (car hooks))))))
|
|
2245 (setq hooks (cdr hooks)))
|
|
2246 ;; If a hook returned t, file is already "written".
|
|
2247 ;; Otherwise, write it the usual way now.
|
|
2248 (if (not done)
|
|
2249 (basic-save-buffer-1)))
|
|
2250 ;; XEmacs: next two clauses (buffer-file-number setting and
|
|
2251 ;; set-file-modes) moved into basic-save-buffer-1.
|
|
2252 )
|
|
2253 ;; If the auto-save file was recent before this command,
|
|
2254 ;; delete it now.
|
|
2255 (delete-auto-save-file-if-necessary recent-save)
|
|
2256 ;; Support VC `implicit' locking.
|
|
2257 (when (fboundp 'vc-after-save)
|
|
2258 (vc-after-save))
|
|
2259 (run-hooks 'after-save-hook))
|
|
2260 (display-message 'no-log "(No changes need to be saved)"))))
|
|
2261
|
|
2262 ;; This does the "real job" of writing a buffer into its visited file
|
|
2263 ;; and making a backup file. This is what is normally done
|
|
2264 ;; but inhibited if one of write-file-hooks returns non-nil.
|
|
2265 ;; It returns a value to store in setmodes.
|
|
2266 (defun basic-save-buffer-1 ()
|
|
2267 (let (setmodes tempsetmodes)
|
|
2268 (if (not (file-writable-p buffer-file-name))
|
|
2269 (let ((dir (file-name-directory buffer-file-name)))
|
|
2270 (if (not (file-directory-p dir))
|
|
2271 (error "%s is not a directory" dir)
|
|
2272 (if (not (file-exists-p buffer-file-name))
|
|
2273 (error "Directory %s write-protected" dir)
|
|
2274 (if (yes-or-no-p
|
|
2275 (format "File %s is write-protected; try to save anyway? "
|
|
2276 (file-name-nondirectory
|
|
2277 buffer-file-name)))
|
|
2278 (setq tempsetmodes t)
|
|
2279 (error
|
|
2280 "Attempt to save to a file which you aren't allowed to write"))))))
|
|
2281 (or buffer-backed-up
|
|
2282 (setq setmodes (backup-buffer)))
|
|
2283 (let ((dir (file-name-directory buffer-file-name)))
|
|
2284 (if (and file-precious-flag
|
|
2285 (file-writable-p dir))
|
|
2286 ;; If file is precious, write temp name, then rename it.
|
|
2287 ;; This requires write access to the containing dir,
|
|
2288 ;; which is why we don't try it if we don't have that access.
|
|
2289 (let ((realname buffer-file-name)
|
|
2290 tempname nogood i succeed
|
|
2291 (old-modtime (visited-file-modtime)))
|
|
2292 (setq i 0)
|
|
2293 (setq nogood t)
|
|
2294 ;; Find the temporary name to write under.
|
|
2295 (while nogood
|
|
2296 (setq tempname (format "%s#tmp#%d" dir i))
|
|
2297 (setq nogood (file-exists-p tempname))
|
|
2298 (setq i (1+ i)))
|
|
2299 (unwind-protect
|
|
2300 (progn (clear-visited-file-modtime)
|
|
2301 (write-region (point-min) (point-max)
|
|
2302 tempname nil realname
|
|
2303 buffer-file-truename)
|
|
2304 (setq succeed t))
|
|
2305 ;; If writing the temp file fails,
|
|
2306 ;; delete the temp file.
|
|
2307 (or succeed
|
|
2308 (progn
|
|
2309 (delete-file tempname)
|
|
2310 (set-visited-file-modtime old-modtime))))
|
|
2311 ;; Since we have created an entirely new file
|
|
2312 ;; and renamed it, make sure it gets the
|
|
2313 ;; right permission bits set.
|
|
2314 (setq setmodes (file-modes buffer-file-name))
|
|
2315 ;; We succeeded in writing the temp file,
|
|
2316 ;; so rename it.
|
|
2317 (rename-file tempname buffer-file-name t))
|
|
2318 ;; If file not writable, see if we can make it writable
|
|
2319 ;; temporarily while we write it.
|
|
2320 ;; But no need to do so if we have just backed it up
|
|
2321 ;; (setmodes is set) because that says we're superseding.
|
|
2322 (cond ((and tempsetmodes (not setmodes))
|
|
2323 ;; Change the mode back, after writing.
|
|
2324 (setq setmodes (file-modes buffer-file-name))
|
|
2325 (set-file-modes buffer-file-name 511)))
|
|
2326 (basic-write-file-data buffer-file-name buffer-file-truename)))
|
|
2327 (setq buffer-file-number
|
|
2328 (if buffer-file-name
|
|
2329 (nth 10 (file-attributes buffer-file-name))
|
|
2330 nil))
|
|
2331 (if setmodes
|
|
2332 (condition-case ()
|
|
2333 (set-file-modes buffer-file-name setmodes)
|
|
2334 (error nil)))))
|
|
2335
|
|
2336 ;; XEmacs change, from Sun
|
|
2337 (defun continue-save-buffer ()
|
|
2338 "Provide a clean way for a write-file-hook to wrap AROUND
|
|
2339 the execution of the remaining hooks and writing to disk.
|
|
2340 Do not call this function except from a functions
|
|
2341 on the write-file-hooks or write-contents-hooks list.
|
|
2342 A hook that calls this function must return non-nil,
|
|
2343 to signal completion to its caller. continue-save-buffer
|
|
2344 always returns non-nil."
|
|
2345 (let ((hooks (cdr (or continue-save-buffer-hooks-tail
|
|
2346 (error
|
|
2347 "continue-save-buffer called outside a write-file-hook!"))))
|
|
2348 (done nil))
|
|
2349 ;; Do something like this:
|
|
2350 ;; (let ((write-file-hooks hooks)) (basic-save-buffer))
|
|
2351 ;; First run the rest of the hooks.
|
|
2352 (while (and hooks
|
|
2353 (let ((continue-save-buffer-hooks-tail hooks))
|
|
2354 (not (setq done (funcall (car hooks))))))
|
|
2355 (setq hooks (cdr hooks)))
|
|
2356 ;;
|
|
2357 ;; If a hook returned t, file is already "written".
|
|
2358 (if (not done)
|
|
2359 (basic-save-buffer-1))
|
|
2360 'continue-save-buffer))
|
|
2361
|
239
|
2362 (defcustom save-some-buffers-query-display-buffer t
|
209
|
2363 "*Non-nil makes `\\[save-some-buffers]' switch to the buffer offered for saving."
|
|
2364 :type 'boolean
|
|
2365 :group 'editing-basics)
|
|
2366
|
|
2367 (defun save-some-buffers (&optional arg exiting)
|
|
2368 "Save some modified file-visiting buffers. Asks user about each one.
|
|
2369 Optional argument (the prefix) non-nil means save all with no questions.
|
|
2370 Optional second argument EXITING means ask about certain non-file buffers
|
|
2371 as well as about file buffers."
|
|
2372 (interactive "P")
|
|
2373 (save-excursion
|
|
2374 (save-window-excursion
|
|
2375 ;; This can bomb during autoloads generation
|
|
2376 (when (and (not noninteractive)
|
241
|
2377 (not (eq (selected-window) (minibuffer-window)))
|
209
|
2378 save-some-buffers-query-display-buffer)
|
|
2379 (delete-other-windows))
|
|
2380 ;; XEmacs - do not use queried flag
|
|
2381 (let ((files-done
|
|
2382 (map-y-or-n-p
|
|
2383 (function
|
|
2384 (lambda (buffer)
|
|
2385 (and (buffer-modified-p buffer)
|
|
2386 (not (buffer-base-buffer buffer))
|
|
2387 ;; XEmacs addition:
|
|
2388 (not (symbol-value-in-buffer 'save-buffers-skip buffer))
|
|
2389 (or
|
|
2390 (buffer-file-name buffer)
|
|
2391 (and exiting
|
|
2392 (progn
|
|
2393 (set-buffer buffer)
|
|
2394 (and buffer-offer-save (> (buffer-size) 0)))))
|
|
2395 (if arg
|
|
2396 t
|
|
2397 (when save-some-buffers-query-display-buffer
|
|
2398 (condition-case nil
|
|
2399 (switch-to-buffer buffer t)
|
|
2400 (error nil)))
|
|
2401 (if (buffer-file-name buffer)
|
|
2402 (format "Save file %s? "
|
|
2403 (buffer-file-name buffer))
|
|
2404 (format "Save buffer %s? "
|
|
2405 (buffer-name buffer)))))))
|
|
2406 (function
|
|
2407 (lambda (buffer)
|
|
2408 (set-buffer buffer)
|
|
2409 (condition-case ()
|
|
2410 (save-buffer)
|
|
2411 (error nil))))
|
|
2412 (buffer-list)
|
|
2413 '("buffer" "buffers" "save")
|
|
2414 ;;instead of this we just say "yes all", "no all", etc.
|
|
2415 ;;"save all the rest"
|
|
2416 ;;"save only this buffer" "save no more buffers")
|
|
2417 ;; this is rather bogus. --ben
|
|
2418 ;; (it makes the dialog box too big, and you get an error
|
|
2419 ;; "wrong type argument: framep, nil" when you hit q after
|
|
2420 ;; choosing the option from the dialog box)
|
|
2421 ; (list (list ?\C-r (lambda (buf)
|
|
2422 ; (view-buffer buf)
|
|
2423 ; (setq view-exit-action
|
|
2424 ; '(lambda (ignore)
|
|
2425 ; (exit-recursive-edit)))
|
|
2426 ; (recursive-edit)
|
|
2427 ; ;; Return nil to ask about BUF again.
|
|
2428 ; nil)
|
|
2429 ; "display the current buffer"))
|
|
2430 ))
|
|
2431 (abbrevs-done
|
|
2432 (and save-abbrevs abbrevs-changed
|
|
2433 (progn
|
|
2434 (if (or arg
|
|
2435 (y-or-n-p (format "Save abbrevs in %s? " abbrev-file-name)))
|
|
2436 (write-abbrev-file nil))
|
|
2437 ;; Don't keep bothering user if he says no.
|
|
2438 (setq abbrevs-changed nil)
|
|
2439 t))))
|
|
2440 (or (> files-done 0) abbrevs-done
|
|
2441 (display-message 'no-log "(No files need saving)"))))))
|
|
2442
|
|
2443
|
|
2444 (defun not-modified (&optional arg)
|
|
2445 "Mark current buffer as unmodified, not needing to be saved.
|
|
2446 With prefix arg, mark buffer as modified, so \\[save-buffer] will save.
|
|
2447
|
|
2448 It is not a good idea to use this function in Lisp programs, because it
|
|
2449 prints a message in the minibuffer. Instead, use `set-buffer-modified-p'."
|
|
2450 (interactive "_P")
|
|
2451 (if arg ;; rewritten for I18N3 snarfing
|
|
2452 (display-message 'command "Modification-flag set")
|
|
2453 (display-message 'command "Modification-flag cleared"))
|
|
2454 (set-buffer-modified-p arg))
|
|
2455
|
|
2456 (defun toggle-read-only (&optional arg)
|
|
2457 "Change whether this buffer is visiting its file read-only.
|
|
2458 With arg, set read-only iff arg is positive."
|
|
2459 (interactive "_P")
|
|
2460 (setq buffer-read-only
|
|
2461 (if (null arg)
|
|
2462 (not buffer-read-only)
|
|
2463 (> (prefix-numeric-value arg) 0)))
|
|
2464 ;; Force modeline redisplay
|
|
2465 (redraw-modeline))
|
|
2466
|
|
2467 (defun insert-file (filename &optional codesys)
|
|
2468 "Insert contents of file FILENAME into buffer after point.
|
|
2469 Set mark after the inserted text.
|
|
2470
|
|
2471 Under XEmacs/Mule, optional second argument specifies the
|
|
2472 coding system to use when decoding the file. Interactively,
|
|
2473 with a prefix argument, you will be prompted for the coding system.
|
|
2474
|
|
2475 This function is meant for the user to run interactively.
|
|
2476 Don't call it from programs! Use `insert-file-contents' instead.
|
|
2477 \(Its calling sequence is different; see its documentation)."
|
|
2478 (interactive "*fInsert file: \nZCoding system: ")
|
|
2479 (if (file-directory-p filename)
|
|
2480 (signal 'file-error (list "Opening input file" "file is a directory"
|
|
2481 filename)))
|
223
|
2482 (let* (format-alist ; format.el only confuses people in this context
|
|
2483 (tem
|
209
|
2484 (if codesys
|
|
2485 (let ((coding-system-for-read
|
|
2486 (get-coding-system codesys)))
|
|
2487 (insert-file-contents filename))
|
|
2488 (insert-file-contents filename))))
|
|
2489 (push-mark (+ (point) (car (cdr tem))))))
|
|
2490
|
|
2491 (defun append-to-file (start end filename &optional codesys)
|
|
2492 "Append the contents of the region to the end of file FILENAME.
|
|
2493 When called from a function, expects three arguments,
|
|
2494 START, END and FILENAME. START and END are buffer positions
|
|
2495 saying what text to write.
|
|
2496 Under XEmacs/Mule, optional fourth argument specifies the
|
|
2497 coding system to use when encoding the file. Interactively,
|
|
2498 with a prefix argument, you will be prompted for the coding system."
|
|
2499 (interactive "r\nFAppend to file: \nZCoding system: ")
|
|
2500 (if codesys
|
|
2501 (let ((buffer-file-coding-system (get-coding-system codesys)))
|
|
2502 (write-region start end filename t))
|
|
2503 (write-region start end filename t)))
|
|
2504
|
|
2505 (defun file-newest-backup (filename)
|
|
2506 "Return most recent backup file for FILENAME or nil if no backups exist."
|
|
2507 (let* ((filename (expand-file-name filename))
|
|
2508 (file (file-name-nondirectory filename))
|
|
2509 (dir (file-name-directory filename))
|
|
2510 (comp (file-name-all-completions file dir))
|
|
2511 newest)
|
|
2512 (while comp
|
|
2513 (setq file (concat dir (car comp))
|
|
2514 comp (cdr comp))
|
|
2515 (if (and (backup-file-name-p file)
|
|
2516 (or (null newest) (file-newer-than-file-p file newest)))
|
|
2517 (setq newest file)))
|
|
2518 newest))
|
|
2519
|
|
2520 (defun rename-uniquely ()
|
|
2521 "Rename current buffer to a similar name not already taken.
|
|
2522 This function is useful for creating multiple shell process buffers
|
|
2523 or multiple mail buffers, etc."
|
|
2524 (interactive)
|
|
2525 (save-match-data
|
|
2526 (let* ((base-name (if (and (string-match "<[0-9]+>\\'" (buffer-name))
|
|
2527 (not (and buffer-file-name
|
|
2528 (string= (buffer-name)
|
|
2529 (file-name-nondirectory
|
|
2530 buffer-file-name)))))
|
|
2531 ;; If the existing buffer name has a <NNN>,
|
|
2532 ;; which isn't part of the file name (if any),
|
|
2533 ;; then get rid of that.
|
|
2534 (substring (buffer-name) 0 (match-beginning 0))
|
|
2535 (buffer-name)))
|
|
2536 (new-buf (generate-new-buffer base-name))
|
|
2537 (name (buffer-name new-buf)))
|
|
2538 (kill-buffer new-buf)
|
|
2539 (rename-buffer name)
|
|
2540 (redraw-modeline))))
|
|
2541
|
|
2542 (defun make-directory-path (path)
|
|
2543 "Create all the directories along path that don't exist yet."
|
|
2544 (interactive "Fdirectory path to create: ")
|
|
2545 (make-directory path t))
|
|
2546
|
|
2547 (defun make-directory (dir &optional parents)
|
|
2548 "Create the directory DIR and any nonexistent parent dirs.
|
|
2549 Interactively, the default choice of directory to create
|
|
2550 is the current default directory for file names.
|
|
2551 That is useful when you have visited a file in a nonexistent directory.
|
|
2552
|
|
2553 Noninteractively, the second (optional) argument PARENTS says whether
|
|
2554 to create parent directories if they don't exist."
|
|
2555 (interactive (list (let ((current-prefix-arg current-prefix-arg))
|
|
2556 (read-directory-name "Create directory: "))
|
|
2557 current-prefix-arg))
|
|
2558 (let ((handler (find-file-name-handler dir 'make-directory)))
|
|
2559 (if handler
|
|
2560 (funcall handler 'make-directory dir parents)
|
|
2561 (if (not parents)
|
|
2562 (make-directory-internal dir)
|
|
2563 (let ((dir (directory-file-name (expand-file-name dir)))
|
|
2564 create-list)
|
|
2565 (while (not (file-exists-p dir))
|
|
2566 (setq create-list (cons dir create-list)
|
|
2567 dir (directory-file-name (file-name-directory dir))))
|
|
2568 (while create-list
|
|
2569 (make-directory-internal (car create-list))
|
|
2570 (setq create-list (cdr create-list))))))))
|
|
2571
|
|
2572 (put 'revert-buffer-function 'permanent-local t)
|
|
2573 (defvar revert-buffer-function nil
|
|
2574 "Function to use to revert this buffer, or nil to do the default.
|
|
2575 The function receives two arguments IGNORE-AUTO and NOCONFIRM,
|
|
2576 which are the arguments that `revert-buffer' received.")
|
|
2577
|
|
2578 (put 'revert-buffer-insert-file-contents-function 'permanent-local t)
|
|
2579 (defvar revert-buffer-insert-file-contents-function nil
|
|
2580 "Function to use to insert contents when reverting this buffer.
|
|
2581 Gets two args, first the nominal file name to use,
|
|
2582 and second, t if reading the auto-save file.")
|
|
2583
|
|
2584 (defvar before-revert-hook nil
|
|
2585 "Normal hook for `revert-buffer' to run before reverting.
|
|
2586 If `revert-buffer-function' is used to override the normal revert
|
|
2587 mechanism, this hook is not used.")
|
|
2588
|
|
2589 (defvar after-revert-hook nil
|
|
2590 "Normal hook for `revert-buffer' to run after reverting.
|
|
2591 Note that the hook value that it runs is the value that was in effect
|
|
2592 before reverting; that makes a difference if you have buffer-local
|
|
2593 hook functions.
|
|
2594
|
|
2595 If `revert-buffer-function' is used to override the normal revert
|
|
2596 mechanism, this hook is not used.")
|
|
2597
|
|
2598 (defvar revert-buffer-internal-hook nil
|
|
2599 "Don't use this.")
|
|
2600
|
|
2601 (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes)
|
|
2602 "Replace the buffer text with the text of the visited file on disk.
|
|
2603 This undoes all changes since the file was visited or saved.
|
|
2604 With a prefix argument, offer to revert from latest auto-save file, if
|
|
2605 that is more recent than the visited file.
|
|
2606
|
|
2607 This command also works for special buffers that contain text which
|
|
2608 doesn't come from a file, but reflects some other data base instead:
|
|
2609 for example, Dired buffers and buffer-list buffers. In these cases,
|
|
2610 it reconstructs the buffer contents from the appropriate data base.
|
|
2611
|
|
2612 When called from Lisp, the first argument is IGNORE-AUTO; only offer
|
|
2613 to revert from the auto-save file when this is nil. Note that the
|
|
2614 sense of this argument is the reverse of the prefix argument, for the
|
|
2615 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
|
|
2616 to nil.
|
|
2617
|
|
2618 Optional second argument NOCONFIRM means don't ask for confirmation at
|
|
2619 all.
|
|
2620
|
|
2621 Optional third argument PRESERVE-MODES non-nil means don't alter
|
|
2622 the files modes. Normally we reinitialize them using `normal-mode'.
|
|
2623
|
|
2624 If the value of `revert-buffer-function' is non-nil, it is called to
|
|
2625 do the work.
|
|
2626
|
|
2627 The default revert function runs the hook `before-revert-hook' at the
|
|
2628 beginning and `after-revert-hook' at the end."
|
|
2629 ;; I admit it's odd to reverse the sense of the prefix argument, but
|
|
2630 ;; there is a lot of code out there which assumes that the first
|
|
2631 ;; argument should be t to avoid consulting the auto-save file, and
|
|
2632 ;; there's no straightforward way to encourage authors to notice a
|
|
2633 ;; reversal of the argument sense. So I'm just changing the user
|
|
2634 ;; interface, but leaving the programmatic interface the same.
|
|
2635 (interactive (list (not current-prefix-arg)))
|
|
2636 (if revert-buffer-function
|
|
2637 (funcall revert-buffer-function ignore-auto noconfirm)
|
|
2638 (let* ((opoint (point))
|
|
2639 (auto-save-p (and (not ignore-auto)
|
|
2640 (recent-auto-save-p)
|
|
2641 buffer-auto-save-file-name
|
|
2642 (file-readable-p buffer-auto-save-file-name)
|
|
2643 (y-or-n-p
|
|
2644 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
|
|
2645 (file-name (if auto-save-p
|
|
2646 buffer-auto-save-file-name
|
|
2647 buffer-file-name)))
|
|
2648 (cond ((null file-name)
|
|
2649 (error "Buffer does not seem to be associated with any file"))
|
|
2650 ((or noconfirm
|
|
2651 (and (not (buffer-modified-p))
|
|
2652 (let (found)
|
|
2653 (dolist (rx revert-without-query found)
|
|
2654 (when (string-match rx file-name)
|
|
2655 (setq found t)))))
|
|
2656 (yes-or-no-p (format "Revert buffer from file %s? "
|
|
2657 file-name)))
|
|
2658 (run-hooks 'before-revert-hook)
|
|
2659 ;; If file was backed up but has changed since,
|
|
2660 ;; we shd make another backup.
|
|
2661 (and (not auto-save-p)
|
|
2662 (not (verify-visited-file-modtime (current-buffer)))
|
|
2663 (setq buffer-backed-up nil))
|
|
2664 ;; Get rid of all undo records for this buffer.
|
|
2665 (or (eq buffer-undo-list t)
|
|
2666 (setq buffer-undo-list nil))
|
|
2667 ;; Effectively copy the after-revert-hook status,
|
|
2668 ;; since after-find-file will clobber it.
|
|
2669 (let ((global-hook (default-value 'after-revert-hook))
|
|
2670 (local-hook-p (local-variable-p 'after-revert-hook
|
|
2671 (current-buffer)))
|
|
2672 (local-hook (and (local-variable-p 'after-revert-hook
|
|
2673 (current-buffer))
|
|
2674 after-revert-hook)))
|
|
2675 (let (buffer-read-only
|
|
2676 ;; Don't make undo records for the reversion.
|
|
2677 (buffer-undo-list t))
|
|
2678 (if revert-buffer-insert-file-contents-function
|
|
2679 (funcall revert-buffer-insert-file-contents-function
|
|
2680 file-name auto-save-p)
|
|
2681 (if (not (file-exists-p file-name))
|
|
2682 (error "File %s no longer exists!" file-name))
|
|
2683 ;; Bind buffer-file-name to nil
|
|
2684 ;; so that we don't try to lock the file.
|
|
2685 (let ((buffer-file-name nil))
|
|
2686 (or auto-save-p
|
|
2687 (unlock-buffer)))
|
|
2688 (widen)
|
|
2689 (insert-file-contents file-name (not auto-save-p)
|
|
2690 nil nil t)))
|
|
2691 (goto-char (min opoint (point-max)))
|
|
2692 ;; Recompute the truename in case changes in symlinks
|
|
2693 ;; have changed the truename.
|
|
2694 ;XEmacs: already done by insert-file-contents
|
|
2695 ;;(setq buffer-file-truename
|
|
2696 ;;(abbreviate-file-name (file-truename buffer-file-name)))
|
|
2697 (after-find-file nil nil t t preserve-modes)
|
|
2698 ;; Run after-revert-hook as it was before we reverted.
|
|
2699 (setq-default revert-buffer-internal-hook global-hook)
|
|
2700 (if local-hook-p
|
|
2701 (progn
|
|
2702 (make-local-variable 'revert-buffer-internal-hook)
|
|
2703 (setq revert-buffer-internal-hook local-hook))
|
|
2704 (kill-local-variable 'revert-buffer-internal-hook))
|
|
2705 (run-hooks 'revert-buffer-internal-hook))
|
|
2706 t)))))
|
|
2707
|
|
2708 (defun recover-file (file)
|
|
2709 "Visit file FILE, but get contents from its last auto-save file."
|
|
2710 ;; Actually putting the file name in the minibuffer should be used
|
|
2711 ;; only rarely.
|
|
2712 ;; Not just because users often use the default.
|
|
2713 (interactive "FRecover file: ")
|
|
2714 (setq file (expand-file-name file))
|
|
2715 (let ((handler (or (find-file-name-handler file 'recover-file)
|
|
2716 (find-file-name-handler
|
|
2717 (let ((buffer-file-name file))
|
|
2718 (make-auto-save-file-name))
|
|
2719 'recover-file))))
|
|
2720 (if handler
|
|
2721 (funcall handler 'recover-file file)
|
|
2722 (if (auto-save-file-name-p file)
|
|
2723 (error "%s is an auto-save file" file))
|
|
2724 (let ((file-name (let ((buffer-file-name file))
|
|
2725 (make-auto-save-file-name))))
|
|
2726 (cond ((if (file-exists-p file)
|
|
2727 (not (file-newer-than-file-p file-name file))
|
|
2728 (not (file-exists-p file-name)))
|
|
2729 (error "Auto-save file %s not current" file-name))
|
|
2730 ((save-window-excursion
|
|
2731 (if (not (eq system-type 'vax-vms))
|
|
2732 (with-output-to-temp-buffer "*Directory*"
|
|
2733 (buffer-disable-undo standard-output)
|
|
2734 (call-process "ls" nil standard-output nil
|
|
2735 (if (file-symlink-p file) "-lL" "-l")
|
|
2736 file file-name)))
|
|
2737 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
|
|
2738 (switch-to-buffer (find-file-noselect file t))
|
|
2739 (let ((buffer-read-only nil))
|
|
2740 (erase-buffer)
|
|
2741 (insert-file-contents file-name nil))
|
|
2742 (after-find-file nil nil t))
|
|
2743 (t (error "Recover-file cancelled.")))))))
|
|
2744
|
|
2745 (defun recover-session ()
|
|
2746 "Recover auto save files from a previous Emacs session.
|
|
2747 This command first displays a Dired buffer showing you the
|
|
2748 previous sessions that you could recover from.
|
|
2749 To choose one, move point to the proper line and then type C-c C-c.
|
|
2750 Then you'll be asked about a number of files to recover."
|
|
2751 (interactive)
|
219
|
2752 (unless (fboundp 'dired)
|
|
2753 (error "recover-session requires dired"))
|
209
|
2754 (dired (concat auto-save-list-file-prefix "*"))
|
|
2755 (goto-char (point-min))
|
|
2756 (or (looking-at "Move to the session you want to recover,")
|
|
2757 (let ((inhibit-read-only t))
|
|
2758 (insert "Move to the session you want to recover,\n"
|
|
2759 "then type C-c C-c to select it.\n\n"
|
|
2760 "You can also delete some of these files;\n"
|
|
2761 "type d on a line to mark that file for deletion.\n\n")))
|
|
2762 (use-local-map (let ((map (make-sparse-keymap)))
|
|
2763 (set-keymap-parents map (list (current-local-map)))
|
|
2764 map))
|
|
2765 (define-key (current-local-map) "\C-c\C-c" 'recover-session-finish))
|
|
2766
|
|
2767 (defun recover-session-finish ()
|
|
2768 "Choose one saved session to recover auto-save files from.
|
|
2769 This command is used in the special Dired buffer created by
|
|
2770 \\[recover-session]."
|
|
2771 (interactive)
|
|
2772 ;; Get the name of the session file to recover from.
|
|
2773 (let ((file (dired-get-filename))
|
|
2774 files
|
|
2775 (buffer (get-buffer-create " *recover*")))
|
|
2776 ;; #### dired-do-flagged-delete in FSF.
|
|
2777 ;; This version is for ange-ftp
|
|
2778 ;;(dired-do-deletions t)
|
|
2779 ;; This version is for efs
|
|
2780 (dired-expunge-deletions)
|
|
2781 (unwind-protect
|
|
2782 (save-excursion
|
|
2783 ;; Read in the auto-save-list file.
|
|
2784 (set-buffer buffer)
|
|
2785 (erase-buffer)
|
|
2786 (insert-file-contents file)
|
|
2787 ;; Loop thru the text of that file
|
|
2788 ;; and get out the names of the files to recover.
|
|
2789 (while (not (eobp))
|
|
2790 (let (thisfile autofile)
|
|
2791 (if (eolp)
|
|
2792 ;; This is a pair of lines for a non-file-visiting buffer.
|
|
2793 ;; Get the auto-save file name and manufacture
|
|
2794 ;; a "visited file name" from that.
|
|
2795 (progn
|
|
2796 (forward-line 1)
|
|
2797 (setq autofile
|
|
2798 (buffer-substring-no-properties
|
|
2799 (point)
|
|
2800 (save-excursion
|
|
2801 (end-of-line)
|
|
2802 (point))))
|
|
2803 (setq thisfile
|
|
2804 (expand-file-name
|
|
2805 (substring
|
|
2806 (file-name-nondirectory autofile)
|
|
2807 1 -1)
|
|
2808 (file-name-directory autofile)))
|
|
2809 (forward-line 1))
|
|
2810 ;; This pair of lines is a file-visiting
|
|
2811 ;; buffer. Use the visited file name.
|
|
2812 (progn
|
|
2813 (setq thisfile
|
|
2814 (buffer-substring-no-properties
|
|
2815 (point) (progn (end-of-line) (point))))
|
|
2816 (forward-line 1)
|
|
2817 (setq autofile
|
|
2818 (buffer-substring-no-properties
|
|
2819 (point) (progn (end-of-line) (point))))
|
|
2820 (forward-line 1)))
|
|
2821 ;; Ignore a file if its auto-save file does not exist now.
|
|
2822 (if (file-exists-p autofile)
|
|
2823 (setq files (cons thisfile files)))))
|
|
2824 (setq files (nreverse files))
|
|
2825 ;; The file contains a pair of line for each auto-saved buffer.
|
|
2826 ;; The first line of the pair contains the visited file name
|
|
2827 ;; or is empty if the buffer was not visiting a file.
|
|
2828 ;; The second line is the auto-save file name.
|
|
2829 (if files
|
|
2830 (map-y-or-n-p "Recover %s? "
|
|
2831 (lambda (file)
|
|
2832 (condition-case nil
|
|
2833 (save-excursion (recover-file file))
|
|
2834 (error
|
|
2835 "Failed to recover `%s'" file)))
|
|
2836 files
|
|
2837 '("file" "files" "recover"))
|
|
2838 (message "No files can be recovered from this session now")))
|
|
2839 (kill-buffer buffer))))
|
|
2840
|
|
2841 (defun kill-some-buffers ()
|
|
2842 "For each buffer, ask whether to kill it."
|
|
2843 (interactive)
|
|
2844 (let ((list (buffer-list)))
|
|
2845 (while list
|
|
2846 (let* ((buffer (car list))
|
|
2847 (name (buffer-name buffer)))
|
|
2848 (and (not (string-equal name ""))
|
|
2849 (/= (aref name 0) ? )
|
|
2850 (yes-or-no-p
|
|
2851 (format
|
|
2852 (if (buffer-modified-p buffer)
|
|
2853 (gettext "Buffer %s HAS BEEN EDITED. Kill? ")
|
|
2854 (gettext "Buffer %s is unmodified. Kill? "))
|
|
2855 name))
|
|
2856 (kill-buffer buffer)))
|
|
2857 (setq list (cdr list)))))
|
|
2858
|
|
2859 (defun auto-save-mode (arg)
|
|
2860 "Toggle auto-saving of contents of current buffer.
|
|
2861 With prefix argument ARG, turn auto-saving on if positive, else off."
|
|
2862 (interactive "P")
|
|
2863 (setq buffer-auto-save-file-name
|
|
2864 (and (if (null arg)
|
|
2865 (or (not buffer-auto-save-file-name)
|
|
2866 ;; If autosave is off because buffer has shrunk,
|
|
2867 ;; then toggling should turn it on.
|
|
2868 (< buffer-saved-size 0))
|
|
2869 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))
|
|
2870 (if (and buffer-file-name auto-save-visited-file-name
|
|
2871 (not buffer-read-only))
|
|
2872 buffer-file-name
|
|
2873 (make-auto-save-file-name))))
|
|
2874 ;; If -1 was stored here, to temporarily turn off saving,
|
|
2875 ;; turn it back on.
|
|
2876 (and (< buffer-saved-size 0)
|
|
2877 (setq buffer-saved-size 0))
|
|
2878 (if (interactive-p)
|
|
2879 (if buffer-auto-save-file-name ;; rewritten for I18N3 snarfing
|
|
2880 (display-message 'command "Auto-save on (in this buffer)")
|
|
2881 (display-message 'command "Auto-save off (in this buffer)")))
|
|
2882 buffer-auto-save-file-name)
|
|
2883
|
|
2884 (defun rename-auto-save-file ()
|
|
2885 "Adjust current buffer's auto save file name for current conditions.
|
|
2886 Also rename any existing auto save file, if it was made in this session."
|
|
2887 (let ((osave buffer-auto-save-file-name))
|
|
2888 (setq buffer-auto-save-file-name
|
|
2889 (make-auto-save-file-name))
|
|
2890 (if (and osave buffer-auto-save-file-name
|
|
2891 (not (string= buffer-auto-save-file-name buffer-file-name))
|
|
2892 (not (string= buffer-auto-save-file-name osave))
|
|
2893 (file-exists-p osave)
|
|
2894 (recent-auto-save-p))
|
|
2895 (rename-file osave buffer-auto-save-file-name t))))
|
|
2896
|
|
2897 ;; see also ../packages/auto-save.el
|
|
2898 (defun make-auto-save-file-name (&optional filename)
|
|
2899 "Return file name to use for auto-saves of current buffer.
|
|
2900 Does not consider `auto-save-visited-file-name' as that variable is checked
|
|
2901 before calling this function. You can redefine this for customization.
|
|
2902 See also `auto-save-file-name-p'."
|
|
2903 (let ((fname (or filename buffer-file-name))
|
|
2904 name)
|
|
2905 (setq name
|
|
2906 (if fname
|
|
2907 (concat (file-name-directory fname)
|
|
2908 "#"
|
|
2909 (file-name-nondirectory fname)
|
|
2910 "#")
|
|
2911
|
|
2912 ;; Deal with buffers that don't have any associated files. (Mail
|
|
2913 ;; mode tends to create a good number of these.)
|
|
2914
|
|
2915 (let ((buffer-name (buffer-name))
|
|
2916 (limit 0))
|
|
2917 ;; Use technique from Sebastian Kremer's auto-save
|
|
2918 ;; package to turn slashes into \\!. This ensures that
|
|
2919 ;; the auto-save buffer name is unique.
|
|
2920
|
|
2921 ;; #### - yuck! yuck! yuck! move this functionality
|
|
2922 ;; somewhere else and make the name translation customizable.
|
|
2923 ;; Using "\!" as part of a filename on a UNIX filesystem is nearly
|
|
2924 ;; IMPOSSIBLE to get past a shell parser. -stig
|
|
2925
|
|
2926 (while (string-match "[/\\]" buffer-name limit)
|
|
2927 (setq buffer-name
|
|
2928 (concat (substring buffer-name 0 (match-beginning 0))
|
|
2929 (if (string= (substring buffer-name
|
|
2930 (match-beginning 0)
|
|
2931 (match-end 0))
|
|
2932 "/")
|
|
2933 "\\!"
|
|
2934 "\\\\")
|
|
2935 (substring buffer-name (match-end 0))))
|
|
2936 (setq limit (1+ (match-end 0))))
|
|
2937
|
|
2938 ;; (expand-file-name (format "#%s#%s#" (buffer-name) (make-temp-name "")))
|
|
2939
|
|
2940 ;; jwz: putting the emacs PID in the auto-save file name
|
|
2941 ;; is bad news, because that defeats auto-save-recovery of
|
|
2942 ;; *mail* buffers -- the (sensible) code in sendmail.el
|
|
2943 ;; calls (make-auto-save-file-name) to determine whether
|
|
2944 ;; there is unsent, auto-saved mail to recover. If that
|
|
2945 ;; mail came from a previous emacs process (far and away
|
|
2946 ;; the most likely case) then this can never succeed as
|
|
2947 ;; the pid differs.
|
|
2948
|
|
2949 (expand-file-name (format "#%s#" buffer-name)))
|
|
2950 ))
|
|
2951 ;; don't try to write auto-save files in unwritable places. Unless
|
|
2952 ;; there's already an autosave file here, put ours somewhere safe. --Stig
|
|
2953 (if (or (file-writable-p name)
|
|
2954 (file-exists-p name))
|
|
2955 name
|
|
2956 (expand-file-name (concat "~/" (file-name-nondirectory name))))))
|
|
2957
|
|
2958 (defun auto-save-file-name-p (filename)
|
|
2959 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
|
|
2960 FILENAME should lack slashes.
|
|
2961 You can redefine this for customization."
|
|
2962 (string-match "\\`#.*#\\'" filename))
|
|
2963
|
263
|
2964 (defun wildcard-to-regexp (wildcard)
|
|
2965 "Given a shell file name pattern WILDCARD, return an equivalent regexp.
|
|
2966 The generated regexp will match a filename iff the filename
|
|
2967 matches that wildcard according to shell rules. Only wildcards known
|
|
2968 by `sh' are supported."
|
|
2969 (let* ((i (string-match "[[.*+\\^$?]" wildcard))
|
|
2970 ;; Copy the initial run of non-special characters.
|
|
2971 (result (substring wildcard 0 i))
|
|
2972 (len (length wildcard)))
|
|
2973 ;; If no special characters, we're almost done.
|
|
2974 (if i
|
|
2975 (while (< i len)
|
|
2976 (let ((ch (aref wildcard i))
|
|
2977 j)
|
|
2978 (setq
|
|
2979 result
|
|
2980 (concat result
|
|
2981 (cond
|
|
2982 ((eq ch ?\[) ; [...] maps to regexp char class
|
|
2983 (progn
|
|
2984 (setq i (1+ i))
|
|
2985 (concat
|
|
2986 (cond
|
|
2987 ((eq (aref wildcard i) ?!) ; [!...] -> [^...]
|
|
2988 (progn
|
|
2989 (setq i (1+ i))
|
|
2990 (if (eq (aref wildcard i) ?\])
|
|
2991 (progn
|
|
2992 (setq i (1+ i))
|
|
2993 "[^]")
|
|
2994 "[^")))
|
|
2995 ((eq (aref wildcard i) ?^)
|
|
2996 ;; Found "[^". Insert a `\0' character
|
|
2997 ;; (which cannot happen in a filename)
|
|
2998 ;; into the character class, so that `^'
|
|
2999 ;; is not the first character after `[',
|
|
3000 ;; and thus non-special in a regexp.
|
|
3001 (progn
|
|
3002 (setq i (1+ i))
|
|
3003 "[\000^"))
|
|
3004 ((eq (aref wildcard i) ?\])
|
|
3005 ;; I don't think `]' can appear in a
|
|
3006 ;; character class in a wildcard, but
|
|
3007 ;; let's be general here.
|
|
3008 (progn
|
|
3009 (setq i (1+ i))
|
|
3010 "[]"))
|
|
3011 (t "["))
|
|
3012 (prog1 ; copy everything upto next `]'.
|
|
3013 (substring wildcard
|
|
3014 i
|
|
3015 (setq j (string-match
|
|
3016 "]" wildcard i)))
|
|
3017 (setq i (if j (1- j) (1- len)))))))
|
|
3018 ((eq ch ?.) "\\.")
|
|
3019 ((eq ch ?*) "[^\000]*")
|
|
3020 ((eq ch ?+) "\\+")
|
|
3021 ((eq ch ?^) "\\^")
|
|
3022 ((eq ch ?$) "\\$")
|
|
3023 ((eq ch ?\\) "\\\\") ; probably cannot happen...
|
|
3024 ((eq ch ??) "[^\000]")
|
|
3025 (t (char-to-string ch)))))
|
|
3026 (setq i (1+ i)))))
|
|
3027 ;; Shell wildcards should match the entire filename,
|
|
3028 ;; not its part. Make the regexp say so.
|
|
3029 (concat "\\`" result "\\'")))
|
|
3030
|
209
|
3031 (defcustom list-directory-brief-switches
|
|
3032 (if (eq system-type 'vax-vms) "" "-CF")
|
|
3033 "*Switches for list-directory to pass to `ls' for brief listing."
|
|
3034 :type 'string
|
|
3035 :group 'dired)
|
|
3036
|
|
3037 (defcustom list-directory-verbose-switches
|
|
3038 (if (eq system-type 'vax-vms)
|
|
3039 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
|
|
3040 "-l")
|
|
3041 "*Switches for list-directory to pass to `ls' for verbose listing,"
|
|
3042 :type 'string
|
|
3043 :group 'dired)
|
|
3044
|
|
3045 (defun list-directory (dirname &optional verbose)
|
|
3046 "Display a list of files in or matching DIRNAME, a la `ls'.
|
|
3047 DIRNAME is globbed by the shell if necessary.
|
|
3048 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
|
|
3049 Actions controlled by variables `list-directory-brief-switches'
|
|
3050 and `list-directory-verbose-switches'."
|
|
3051 (interactive (let ((pfx current-prefix-arg))
|
|
3052 (list (read-file-name (if pfx (gettext "List directory (verbose): ")
|
|
3053 (gettext "List directory (brief): "))
|
|
3054 nil default-directory nil)
|
|
3055 pfx)))
|
|
3056 (let ((switches (if verbose list-directory-verbose-switches
|
|
3057 list-directory-brief-switches)))
|
|
3058 (or dirname (setq dirname default-directory))
|
|
3059 (setq dirname (expand-file-name dirname))
|
|
3060 (with-output-to-temp-buffer "*Directory*"
|
|
3061 (buffer-disable-undo standard-output)
|
|
3062 (princ "Directory ")
|
|
3063 (princ dirname)
|
|
3064 (terpri)
|
|
3065 (save-excursion
|
|
3066 (set-buffer "*Directory*")
|
|
3067 (setq default-directory (file-name-directory dirname))
|
|
3068 (let ((wildcard (not (file-directory-p dirname))))
|
|
3069 (insert-directory dirname switches wildcard (not wildcard)))))))
|
|
3070
|
|
3071 (defvar insert-directory-program "ls"
|
|
3072 "Absolute or relative name of the `ls' program used by `insert-directory'.")
|
|
3073
|
|
3074 ;; insert-directory
|
|
3075 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
|
|
3076 ;; FULL-DIRECTORY-P is nil.
|
|
3077 ;; The single line of output must display FILE's name as it was
|
|
3078 ;; given, namely, an absolute path name.
|
|
3079 ;; - must insert exactly one line for each file if WILDCARD or
|
|
3080 ;; FULL-DIRECTORY-P is t, plus one optional "total" line
|
|
3081 ;; before the file lines, plus optional text after the file lines.
|
|
3082 ;; Lines are delimited by "\n", so filenames containing "\n" are not
|
|
3083 ;; allowed.
|
|
3084 ;; File lines should display the basename.
|
|
3085 ;; - must be consistent with
|
|
3086 ;; - functions dired-move-to-filename, (these two define what a file line is)
|
|
3087 ;; dired-move-to-end-of-filename,
|
|
3088 ;; dired-between-files, (shortcut for (not (dired-move-to-filename)))
|
|
3089 ;; dired-insert-headerline
|
|
3090 ;; dired-after-subdir-garbage (defines what a "total" line is)
|
|
3091 ;; - variable dired-subdir-regexp
|
|
3092 (defun insert-directory (file switches &optional wildcard full-directory-p)
|
|
3093 "Insert directory listing for FILE, formatted according to SWITCHES.
|
|
3094 Leaves point after the inserted text.
|
|
3095 SWITCHES may be a string of options, or a list of strings.
|
|
3096 Optional third arg WILDCARD means treat FILE as shell wildcard.
|
|
3097 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
|
|
3098 switches do not contain `d', so that a full listing is expected.
|
|
3099
|
|
3100 This works by running a directory listing program
|
|
3101 whose name is in the variable `insert-directory-program'.
|
|
3102 If WILDCARD, it also runs the shell specified by `shell-file-name'."
|
|
3103 ;; We need the directory in order to find the right handler.
|
|
3104 (let ((handler (find-file-name-handler (expand-file-name file)
|
|
3105 'insert-directory)))
|
|
3106 (if handler
|
|
3107 (funcall handler 'insert-directory file switches
|
|
3108 wildcard full-directory-p)
|
263
|
3109 (cond
|
|
3110 ((eq system-type 'vax-vms)
|
|
3111 (vms-read-directory file switches (current-buffer)))
|
|
3112 ((and (fboundp 'mswindows-insert-directory)
|
|
3113 (eq system-type 'windows-nt))
|
|
3114 (mswindows-insert-directory file switches wildcard full-directory-p))
|
|
3115 (t
|
209
|
3116 (if wildcard
|
|
3117 ;; Run ls in the directory of the file pattern we asked for.
|
|
3118 (let ((default-directory
|
|
3119 (if (file-name-absolute-p file)
|
|
3120 (file-name-directory file)
|
|
3121 (file-name-directory (expand-file-name file))))
|
|
3122 (pattern (file-name-nondirectory file))
|
|
3123 (beg 0))
|
|
3124 ;; Quote some characters that have special meanings in shells;
|
|
3125 ;; but don't quote the wildcards--we want them to be special.
|
|
3126 ;; We also currently don't quote the quoting characters
|
|
3127 ;; in case people want to use them explicitly to quote
|
|
3128 ;; wildcard characters.
|
|
3129 ;;#### Unix-specific
|
|
3130 (while (string-match "[ \t\n;<>&|()#$]" pattern beg)
|
|
3131 (setq pattern
|
|
3132 (concat (substring pattern 0 (match-beginning 0))
|
|
3133 "\\"
|
|
3134 (substring pattern (match-beginning 0)))
|
|
3135 beg (1+ (match-end 0))))
|
|
3136 (call-process shell-file-name nil t nil
|
|
3137 "-c" (concat "\\" ;; Disregard shell aliases!
|
|
3138 insert-directory-program
|
|
3139 " -d "
|
|
3140 (if (stringp switches)
|
|
3141 switches
|
|
3142 (mapconcat 'identity switches " "))
|
|
3143 " "
|
|
3144 pattern)))
|
|
3145 ;; SunOS 4.1.3, SVr4 and others need the "." to list the
|
|
3146 ;; directory if FILE is a symbolic link.
|
|
3147 (apply 'call-process
|
|
3148 insert-directory-program nil t nil
|
|
3149 (let (list)
|
|
3150 (if (listp switches)
|
|
3151 (setq list switches)
|
|
3152 (if (not (equal switches ""))
|
|
3153 (progn
|
|
3154 ;; Split the switches at any spaces
|
|
3155 ;; so we can pass separate options as separate args.
|
|
3156 (while (string-match " " switches)
|
|
3157 (setq list (cons (substring switches 0 (match-beginning 0))
|
|
3158 list)
|
|
3159 switches (substring switches (match-end 0))))
|
|
3160 (setq list (cons switches list)))))
|
|
3161 (append list
|
|
3162 (list
|
|
3163 (if full-directory-p
|
|
3164 (concat (file-name-as-directory file)
|
|
3165 ;;#### Unix-specific
|
|
3166 ".")
|
263
|
3167 file)))))))))))
|
209
|
3168
|
|
3169 (defvar kill-emacs-query-functions nil
|
|
3170 "Functions to call with no arguments to query about killing XEmacs.
|
|
3171 If any of these functions returns nil, killing Emacs is cancelled.
|
|
3172 `save-buffers-kill-emacs' (\\[save-buffers-kill-emacs]) calls these functions,
|
|
3173 but `kill-emacs', the low level primitive, does not.
|
|
3174 See also `kill-emacs-hook'.")
|
|
3175
|
|
3176 (defun save-buffers-kill-emacs (&optional arg)
|
|
3177 "Offer to save each buffer, then kill this XEmacs process.
|
|
3178 With prefix arg, silently save all file-visiting buffers, then kill."
|
|
3179 (interactive "P")
|
|
3180 (save-some-buffers arg t)
|
|
3181 (and (or (not (memq t (mapcar #'(lambda (buf) (and (buffer-file-name buf)
|
|
3182 (buffer-modified-p buf)))
|
|
3183 (buffer-list))))
|
|
3184 (yes-or-no-p "Modified buffers exist; exit anyway? "))
|
|
3185 (or (not (fboundp 'process-list))
|
|
3186 ;; process-list is not defined on VMS.
|
|
3187 (let ((processes (process-list))
|
|
3188 active)
|
|
3189 (while processes
|
|
3190 (and (memq (process-status (car processes)) '(run stop open))
|
|
3191 (let ((val (process-kill-without-query (car processes))))
|
|
3192 (process-kill-without-query (car processes) val)
|
|
3193 val)
|
|
3194 (setq active t))
|
|
3195 (setq processes (cdr processes)))
|
|
3196 (or
|
|
3197 (not active)
|
|
3198 (save-excursion
|
|
3199 (save-window-excursion
|
|
3200 (delete-other-windows)
|
|
3201 (list-processes)
|
|
3202 (yes-or-no-p
|
|
3203 "Active processes exist; kill them and exit anyway? "))))))
|
|
3204 ;; Query the user for other things, perhaps.
|
|
3205 (run-hook-with-args-until-failure 'kill-emacs-query-functions)
|
|
3206 (kill-emacs)))
|
|
3207
|
|
3208 (defun symlink-expand-file-name (filename)
|
|
3209 "If FILENAME is a symlink, return its non-symlink equivalent.
|
|
3210 Unlike `file-truename', this doesn't chase symlinks in directory
|
|
3211 components of the file or expand a relative pathname into an
|
|
3212 absolute one."
|
|
3213 (let ((count 20))
|
|
3214 (while (and (> count 0) (file-symlink-p filename))
|
|
3215 (setq filename (file-symlink-p filename)
|
|
3216 count (1- count)))
|
|
3217 (if (> count 0)
|
|
3218 filename
|
|
3219 (error "Apparently circular symlink path"))))
|
|
3220
|
|
3221 ;; Suggested by Michael Kifer <kifer@CS.SunySB.EDU>
|
|
3222 (defun file-remote-p (file-name)
|
|
3223 "Test whether FILE-NAME is looked for on a remote system."
|
|
3224 (cond ((not allow-remote-paths) nil)
|
|
3225 ((featurep 'ange-ftp) (ange-ftp-ftp-path file-name))
|
219
|
3226 ((fboundp 'efs-ftp-path) (efs-ftp-path file-name))
|
|
3227 (t nil)))
|
209
|
3228
|
|
3229 ;;; files.el ends here
|