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