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