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