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