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