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