comparison lisp/prim/files.el @ 0:376386a54a3c r19-14

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