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