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