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