]> code.delx.au - gnu-emacs/blob - lisp/files.el
(write-file): Fix arg count in call to y-or-n-p.
[gnu-emacs] / lisp / files.el
1 ;;; files.el --- file input and output commands for Emacs
2
3 ;; Copyright (C) 1985, 86, 87, 92, 93, 94, 1995 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
7 ;; This file is part of GNU Emacs.
8
9 ;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;; it under the terms of the GNU General Public License as published by
11 ;; the Free Software Foundation; either version 2, or (at your option)
12 ;; any later version.
13
14 ;; GNU Emacs is distributed in the hope that it will be useful,
15 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;; GNU General Public License for more details.
18
19 ;; You should have received a copy of the GNU General Public License
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; Defines most of Emacs's file- and directory-handling functions,
26 ;; including basic file visiting, backup generation, link handling,
27 ;; ITS-id version control, load- and write-hook handling, and the like.
28
29 ;;; Code:
30
31 (defconst delete-auto-save-files t
32 "*Non-nil means delete auto-save file when a buffer is saved or killed.")
33
34 (defconst directory-abbrev-alist
35 nil
36 "*Alist of abbreviations for file directories.
37 A list of elements of the form (FROM . TO), each meaning to replace
38 FROM with TO when it appears in a directory name. This replacement is
39 done when setting up the default directory of a newly visited file.
40 *Every* FROM string should start with `^'.
41
42 Do not use `~' in the TO strings.
43 They should be ordinary absolute directory names.
44
45 Use this feature when you have directories which you normally refer to
46 via absolute symbolic links. Make TO the name of the link, and FROM
47 the name it is linked to.")
48
49 ;;; Turn off backup files on VMS since it has version numbers.
50 (defconst make-backup-files (not (eq system-type 'vax-vms))
51 "*Non-nil means make a backup of a file the first time it is saved.
52 This can be done by renaming the file or by copying.
53
54 Renaming means that Emacs renames the existing file so that it is a
55 backup file, then writes the buffer into a new file. Any other names
56 that the old file had will now refer to the backup file. The new file
57 is owned by you and its group is defaulted.
58
59 Copying means that Emacs copies the existing file into the backup
60 file, then writes the buffer on top of the existing file. Any other
61 names that the old file had will now refer to the new (edited) file.
62 The file's owner and group are unchanged.
63
64 The choice of renaming or copying is controlled by the variables
65 `backup-by-copying', `backup-by-copying-when-linked' and
66 `backup-by-copying-when-mismatch'. See also `backup-inhibited'.")
67
68 ;; Do this so that local variables based on the file name
69 ;; are not overridden by the major mode.
70 (defvar backup-inhibited nil
71 "Non-nil means don't make a backup, regardless of the other parameters.
72 This variable is intended for use by making it local to a buffer.
73 But it is local only if you make it local.")
74 (put 'backup-inhibited 'permanent-local t)
75
76 (defconst backup-by-copying nil
77 "*Non-nil means always use copying to create backup files.
78 See documentation of variable `make-backup-files'.")
79
80 (defconst backup-by-copying-when-linked nil
81 "*Non-nil means use copying to create backups for files with multiple names.
82 This causes the alternate names to refer to the latest version as edited.
83 This variable is relevant only if `backup-by-copying' is nil.")
84
85 (defconst backup-by-copying-when-mismatch nil
86 "*Non-nil means create backups by copying if this preserves owner or group.
87 Renaming may still be used (subject to control of other variables)
88 when it would not result in changing the owner or group of the file;
89 that is, for files which are owned by you and whose group matches
90 the default for a new file created there by you.
91 This variable is relevant only if `backup-by-copying' is nil.")
92
93 (defvar backup-enable-predicate
94 '(lambda (name)
95 (or (< (length name) 5)
96 (not (string-equal "/tmp/" (substring name 0 5)))))
97 "Predicate that looks at a file name and decides whether to make backups.
98 Called with an absolute file name as argument, it returns t to enable backup.")
99
100 (defconst buffer-offer-save nil
101 "*Non-nil in a buffer means offer to save the buffer on exit
102 even if the buffer is not visiting a file.
103 Automatically local in all buffers.")
104 (make-variable-buffer-local 'buffer-offer-save)
105
106 (defconst find-file-existing-other-name nil
107 "*Non-nil means find a file under alternative names, in existing buffers.
108 This means if any existing buffer is visiting the file you want
109 under another name, you get the existing buffer instead of a new buffer.")
110
111 (defconst find-file-visit-truename nil
112 "*Non-nil means visit a file under its truename.
113 The truename of a file is found by chasing all links
114 both at the file level and at the levels of the containing directories.")
115
116 (defvar buffer-file-truename nil
117 "The abbreviated truename of the file visited in the current buffer.
118 That is, (abbreviate-file-name (file-truename buffer-file-name)).
119 This variable is automatically local in all buffers, when non-nil.")
120 (make-variable-buffer-local 'buffer-file-truename)
121 (put 'buffer-file-truename 'permanent-local t)
122
123 (defvar buffer-file-number nil
124 "The device number and file number of the file visited in the current buffer.
125 The value is a list of the form (FILENUM DEVNUM).
126 This pair of numbers uniquely identifies the file.
127 If the buffer is visiting a new file, the value is nil.")
128 (make-variable-buffer-local 'buffer-file-number)
129 (put 'buffer-file-number 'permanent-local t)
130
131 (defconst file-precious-flag nil
132 "*Non-nil means protect against I/O errors while saving files.
133 Some modes set this non-nil in particular buffers.")
134
135 (defvar version-control nil
136 "*Control use of version numbers for backup files.
137 t means make numeric backup versions unconditionally.
138 nil means make them for files that have some already.
139 `never' means do not make them.")
140
141 (defvar dired-kept-versions 2
142 "*When cleaning directory, number of versions to keep.")
143
144 (defvar delete-old-versions nil
145 "*If t, delete excess backup versions silently.
146 If nil, ask confirmation. Any other value prevents any trimming.")
147
148 (defvar kept-old-versions 2
149 "*Number of oldest versions to keep when a new numbered backup is made.")
150
151 (defvar kept-new-versions 2
152 "*Number of newest versions to keep when a new numbered backup is made.
153 Includes the new backup. Must be > 0")
154
155 (defconst require-final-newline nil
156 "*Value of t says silently ensure a file ends in a newline when it is saved.
157 Non-nil but not t says ask user whether to add a newline when there isn't one.
158 nil means don't add newlines.")
159
160 (defconst auto-save-default t
161 "*Non-nil says by default do auto-saving of every file-visiting buffer.")
162
163 (defconst auto-save-visited-file-name nil
164 "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
165 Normally auto-save files are written under other names.")
166
167 (defconst save-abbrevs nil
168 "*Non-nil means save word abbrevs too when files are saved.
169 Loading an abbrev file sets this to t.")
170
171 (defconst find-file-run-dired t
172 "*Non-nil says run dired if `find-file' is given the name of a directory.")
173
174 ;;;It is not useful to make this a local variable.
175 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
176 (defvar find-file-not-found-hooks nil
177 "List of functions to be called for `find-file' on nonexistent file.
178 These functions are called as soon as the error is detected.
179 `buffer-file-name' is already set up.
180 The functions are called in the order given until one of them returns non-nil.")
181
182 ;;;It is not useful to make this a local variable.
183 ;;;(put 'find-file-hooks 'permanent-local t)
184 (defvar find-file-hooks nil
185 "List of functions to be called after a buffer is loaded from a file.
186 The buffer's local variables (if any) will have been processed before the
187 functions are called.")
188
189 (defvar write-file-hooks nil
190 "List of functions to be called before writing out a buffer to a file.
191 If one of them returns non-nil, the file is considered already written
192 and the rest are not called.
193 These hooks are considered to pertain to the visited file.
194 So this list is cleared if you change the visited file name.
195 See also `write-contents-hooks'.
196 Don't make this variable buffer-local; instead, use `local-write-file-hooks'.")
197 ;;; However, in case someone does make it local...
198 (put 'write-file-hooks 'permanent-local t)
199
200 (defvar local-write-file-hooks nil
201 "Just like `write-file-hooks', except intended for per-buffer use.
202 The functions in this list are called before the ones in
203 `write-file-hooks'.")
204 (make-variable-buffer-local 'local-write-file-hooks)
205 (put 'local-write-file-hooks 'permanent-local t)
206
207 (defvar write-contents-hooks nil
208 "List of functions to be called before writing out a buffer to a file.
209 If one of them returns non-nil, the file is considered already written
210 and the rest are not called.
211 These hooks are considered to pertain to the buffer's contents,
212 not to the particular visited file; thus, `set-visited-file-name' does
213 not clear this variable, but changing the major mode does clear it.
214 See also `write-file-hooks'.")
215
216 (defconst enable-local-variables t
217 "*Control use of local-variables lists in files you visit.
218 The value can be t, nil or something else.
219 A value of t means local-variables lists are obeyed;
220 nil means they are ignored; anything else means query.
221
222 The command \\[normal-mode] always obeys local-variables lists
223 and ignores this variable.")
224
225 (defconst enable-local-eval 'maybe
226 "*Control processing of the \"variable\" `eval' in a file's local variables.
227 The value can be t, nil or something else.
228 A value of t means obey `eval' variables;
229 nil means ignore them; anything else means query.
230
231 The command \\[normal-mode] always obeys local-variables lists
232 and ignores this variable.")
233
234 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
235 (or (fboundp 'lock-buffer)
236 (defalias 'lock-buffer 'ignore))
237 (or (fboundp 'unlock-buffer)
238 (defalias 'unlock-buffer 'ignore))
239
240 ;; This hook function provides support for ange-ftp host name
241 ;; completion. It runs the usual ange-ftp hook, but only for
242 ;; completion operations. Having this here avoids the need
243 ;; to load ange-ftp when it's not really in use.
244 (defun ange-ftp-completion-hook-function (op &rest args)
245 (if (memq op '(file-name-completion file-name-all-completions))
246 (apply 'ange-ftp-hook-function op args)
247 (let ((inhibit-file-name-handlers
248 (cons 'ange-ftp-completion-hook-function
249 (and (eq inhibit-file-name-operation op)
250 inhibit-file-name-handlers)))
251 (inhibit-file-name-operation op))
252 (apply op args))))
253 \f
254 (defun pwd ()
255 "Show the current default directory."
256 (interactive nil)
257 (message "Directory %s" default-directory))
258
259 (defvar cd-path nil
260 "Value of the CDPATH environment variable, as a list.
261 Not actually set up until the first time you you use it.")
262
263 (defun parse-colon-path (cd-path)
264 "Explode a colon-separated list of paths into a string list."
265 (and cd-path
266 (let (cd-prefix cd-list (cd-start 0) cd-colon)
267 (setq cd-path (concat cd-path ":"))
268 (while (setq cd-colon (string-match ":" cd-path cd-start))
269 (setq cd-list
270 (nconc cd-list
271 (list (if (= cd-start cd-colon)
272 nil
273 (substitute-in-file-name
274 (file-name-as-directory
275 (substring cd-path cd-start cd-colon)))))))
276 (setq cd-start (+ cd-colon 1)))
277 cd-list)))
278
279 (defun cd-absolute (dir)
280 "Change current directory to given absolute file name DIR."
281 ;; Put the name into directory syntax now,
282 ;; because otherwise expand-file-name may give some bad results.
283 (if (not (eq system-type 'vax-vms))
284 (setq dir (file-name-as-directory dir)))
285 (setq dir (abbreviate-file-name (expand-file-name dir)))
286 (if (not (file-directory-p dir))
287 (error "%s is not a directory" dir)
288 (if (file-executable-p dir)
289 (setq default-directory dir)
290 (error "Cannot cd to %s: Permission denied" dir))))
291
292 (defun cd (dir)
293 "Make DIR become the current buffer's default directory.
294 If your environment includes a `CDPATH' variable, try each one of that
295 colon-separated list of directories when resolving a relative directory name."
296 (interactive "FChange default directory: ")
297 (if (file-name-absolute-p dir)
298 (cd-absolute (expand-file-name dir))
299 (if (null cd-path)
300 (let ((trypath (parse-colon-path (getenv "CDPATH"))))
301 (setq cd-path (or trypath (list "./")))))
302 (if (not (catch 'found
303 (mapcar
304 (function (lambda (x)
305 (let ((f (expand-file-name (concat x dir))))
306 (if (file-directory-p f)
307 (progn
308 (cd-absolute f)
309 (throw 'found t))))))
310 cd-path)
311 nil))
312 (error "No such directory found via CDPATH environment variable"))))
313
314 (defun load-file (file)
315 "Load the Lisp file named FILE."
316 (interactive "fLoad file: ")
317 (load (expand-file-name file) nil nil t))
318
319 (defun load-library (library)
320 "Load the library named LIBRARY.
321 This is an interface to the function `load'."
322 (interactive "sLoad library: ")
323 (load library))
324
325 (defun file-local-copy (file &optional buffer)
326 "Copy the file FILE into a temporary file on this machine.
327 Returns the name of the local copy, or nil, if FILE is directly
328 accessible."
329 (let ((handler (find-file-name-handler file 'file-local-copy)))
330 (if handler
331 (funcall handler 'file-local-copy file)
332 nil)))
333
334 (defun file-truename (filename &optional counter prev-dirs)
335 "Return the truename of FILENAME, which should be absolute.
336 The truename of a file name is found by chasing symbolic links
337 both at the level of the file and at the level of the directories
338 containing it, until no links are left at any level.
339
340 The arguments COUNTER and PREV-DIRS are used only in recursive calls.
341 Do not specify them in other calls."
342 ;; COUNTER can be a cons cell whose car is the count of how many more links
343 ;; to chase before getting an error.
344 ;; PREV-DIRS can be a cons cell whose car is an alist
345 ;; of truenames we've just recently computed.
346 (if (or (string= filename "~")
347 (and (string= (substring filename 0 1) "~")
348 (string-match "~[^/]*" filename)))
349 (progn
350 (setq filename (expand-file-name filename))
351 (if (string= filename "")
352 (setq filename "/"))))
353 (or counter (setq counter (list 100)))
354 (let (done
355 ;; For speed, remove the ange-ftp completion handler from the list.
356 ;; We know it's not needed here.
357 ;; For even more speed, do this only on the outermost call.
358 (file-name-handler-alist
359 (if prev-dirs file-name-handler-alist
360 (let ((tem (copy-sequence file-name-handler-alist)))
361 (delq (rassq 'ange-ftp-completion-hook-function tem) tem)))))
362 (or prev-dirs (setq prev-dirs (list nil)))
363 ;; If this file directly leads to a link, process that iteratively
364 ;; so that we don't use lots of stack.
365 (while (not done)
366 (setcar counter (1- (car counter)))
367 (if (< (car counter) 0)
368 (error "Apparent cycle of symbolic links for %s" filename))
369 (let ((handler (find-file-name-handler filename 'file-truename)))
370 ;; For file name that has a special handler, call handler.
371 ;; This is so that ange-ftp can save time by doing a no-op.
372 (if handler
373 (setq filename (funcall handler 'file-truename filename)
374 done t)
375 (let ((dir (or (file-name-directory filename) default-directory))
376 target dirfile)
377 ;; Get the truename of the directory.
378 (setq dirfile (directory-file-name dir))
379 ;; If these are equal, we have the (or a) root directory.
380 (or (string= dir dirfile)
381 ;; If this is the same dir we last got the truename for,
382 ;; save time--don't recalculate.
383 (if (assoc dir (car prev-dirs))
384 (setq dir (cdr (assoc dir (car prev-dirs))))
385 (let ((old dir)
386 (new (file-name-as-directory (file-truename dirfile counter prev-dirs))))
387 (setcar prev-dirs (cons (cons old new) (car prev-dirs)))
388 (setq dir new))))
389 (if (equal ".." (file-name-nondirectory filename))
390 (setq filename
391 (directory-file-name (file-name-directory (directory-file-name dir)))
392 done t)
393 (if (equal "." (file-name-nondirectory filename))
394 (setq filename (directory-file-name dir)
395 done t)
396 ;; Put it back on the file name.
397 (setq filename (concat dir (file-name-nondirectory filename)))
398 ;; Is the file name the name of a link?
399 (setq target (file-symlink-p filename))
400 (if target
401 ;; Yes => chase that link, then start all over
402 ;; since the link may point to a directory name that uses links.
403 ;; We can't safely use expand-file-name here
404 ;; since target might look like foo/../bar where foo
405 ;; is itself a link. Instead, we handle . and .. above.
406 (setq filename
407 (if (file-name-absolute-p target)
408 target
409 (concat dir target))
410 done nil)
411 ;; No, we are done!
412 (setq done t))))))))
413 filename))
414
415 (defun file-chase-links (filename)
416 "Chase links in FILENAME until a name that is not a link.
417 Does not examine containing directories for links,
418 unlike `file-truename'."
419 (let (tem (count 100) (newname filename))
420 (while (setq tem (file-symlink-p newname))
421 (if (= count 0)
422 (error "Apparent cycle of symbolic links for %s" filename))
423 ;; In the context of a link, `//' doesn't mean what Emacs thinks.
424 (while (string-match "//+" tem)
425 (setq tem (concat (substring tem 0 (1+ (match-beginning 0)))
426 (substring tem (match-end 0)))))
427 ;; Handle `..' by hand, since it needs to work in the
428 ;; target of any directory symlink.
429 ;; This code is not quite complete; it does not handle
430 ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose.
431 (while (string-match "\\`\\.\\./" tem)
432 (setq tem (substring tem 3))
433 (setq newname (file-name-as-directory
434 ;; Do the .. by hand.
435 (directory-file-name
436 (file-name-directory
437 ;; Chase links in the default dir of the symlink.
438 (file-chase-links
439 (directory-file-name
440 (file-name-directory newname))))))))
441 (setq newname (expand-file-name tem (file-name-directory newname)))
442 (setq count (1- count)))
443 newname))
444 \f
445 (defun switch-to-buffer-other-window (buffer)
446 "Select buffer BUFFER in another window."
447 (interactive "BSwitch to buffer in other window: ")
448 (let ((pop-up-windows t))
449 (pop-to-buffer buffer t)))
450
451 (defun switch-to-buffer-other-frame (buffer)
452 "Switch to buffer BUFFER in another frame."
453 (interactive "BSwitch to buffer in other frame: ")
454 (let ((pop-up-frames t))
455 (pop-to-buffer buffer t)
456 (raise-frame (window-frame (selected-window)))))
457
458 (defun find-file (filename)
459 "Edit file FILENAME.
460 Switch to a buffer visiting file FILENAME,
461 creating one if none already exists."
462 (interactive "FFind file: ")
463 (switch-to-buffer (find-file-noselect filename)))
464
465 (defun find-file-other-window (filename)
466 "Edit file FILENAME, in another window.
467 May create a new window, or reuse an existing one.
468 See the function `display-buffer'."
469 (interactive "FFind file in other window: ")
470 (switch-to-buffer-other-window (find-file-noselect filename)))
471
472 (defun find-file-other-frame (filename)
473 "Edit file FILENAME, in another frame.
474 May create a new frame, or reuse an existing one.
475 See the function `display-buffer'."
476 (interactive "FFind file in other frame: ")
477 (switch-to-buffer-other-frame (find-file-noselect filename)))
478
479 (defun find-file-read-only (filename)
480 "Edit file FILENAME but don't allow changes.
481 Like \\[find-file] but marks buffer as read-only.
482 Use \\[toggle-read-only] to permit editing."
483 (interactive "fFind file read-only: ")
484 (find-file filename)
485 (setq buffer-read-only t)
486 (current-buffer))
487
488 (defun find-file-read-only-other-window (filename)
489 "Edit file FILENAME in another window but don't allow changes.
490 Like \\[find-file-other-window] but marks buffer as read-only.
491 Use \\[toggle-read-only] to permit editing."
492 (interactive "fFind file read-only other window: ")
493 (find-file-other-window filename)
494 (setq buffer-read-only t)
495 (current-buffer))
496
497 (defun find-file-read-only-other-frame (filename)
498 "Edit file FILENAME in another frame but don't allow changes.
499 Like \\[find-file-other-frame] but marks buffer as read-only.
500 Use \\[toggle-read-only] to permit editing."
501 (interactive "fFind file read-only other frame: ")
502 (find-file-other-frame filename)
503 (setq buffer-read-only t)
504 (current-buffer))
505
506 (defun find-alternate-file (filename)
507 "Find file FILENAME, select its buffer, kill previous buffer.
508 If the current buffer now contains an empty file that you just visited
509 \(presumably by mistake), use this command to visit the file you really want."
510 (interactive
511 (let ((file buffer-file-name)
512 (file-name nil)
513 (file-dir nil))
514 (and file
515 (setq file-name (file-name-nondirectory file)
516 file-dir (file-name-directory file)))
517 (list (read-file-name
518 "Find alternate file: " file-dir nil nil file-name))))
519 (and (buffer-modified-p) (buffer-file-name)
520 ;; (not buffer-read-only)
521 (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
522 (buffer-name))))
523 (error "Aborted"))
524 (let ((obuf (current-buffer))
525 (ofile buffer-file-name)
526 (onum buffer-file-number)
527 (otrue buffer-file-truename)
528 (oname (buffer-name)))
529 (rename-buffer " **lose**")
530 (setq buffer-file-name nil)
531 (setq buffer-file-number nil)
532 (setq buffer-file-truename nil)
533 (unwind-protect
534 (progn
535 (unlock-buffer)
536 (find-file filename))
537 (cond ((eq obuf (current-buffer))
538 (setq buffer-file-name ofile)
539 (setq buffer-file-number onum)
540 (setq buffer-file-truename otrue)
541 (lock-buffer)
542 (rename-buffer oname))))
543 (or (eq (current-buffer) obuf)
544 (kill-buffer obuf))))
545
546 (defun create-file-buffer (filename)
547 "Create a suitably named buffer for visiting FILENAME, and return it.
548 FILENAME (sans directory) is used unchanged if that name is free;
549 otherwise a string <2> or <3> or ... is appended to get an unused name."
550 (let ((lastname (file-name-nondirectory filename)))
551 (if (string= lastname "")
552 (setq lastname filename))
553 (generate-new-buffer lastname)))
554
555 (defun generate-new-buffer (name)
556 "Create and return a buffer with a name based on NAME.
557 Choose the buffer's name using `generate-new-buffer-name'."
558 (get-buffer-create (generate-new-buffer-name name)))
559
560 (defconst automount-dir-prefix "^/tmp_mnt/"
561 "Regexp to match the automounter prefix in a directory name.")
562
563 (defvar abbreviated-home-dir nil
564 "The user's homedir abbreviated according to `directory-abbrev-list'.")
565
566 (defun abbreviate-file-name (filename)
567 "Return a version of FILENAME shortened using `directory-abbrev-alist'.
568 This also substitutes \"~\" for the user's home directory.
569 Type \\[describe-variable] directory-abbrev-alist RET for more information."
570 ;; Get rid of the prefixes added by the automounter.
571 (if (and (string-match automount-dir-prefix filename)
572 (file-exists-p (file-name-directory
573 (substring filename (1- (match-end 0))))))
574 (setq filename (substring filename (1- (match-end 0)))))
575 (let ((tail directory-abbrev-alist))
576 ;; If any elt of directory-abbrev-alist matches this name,
577 ;; abbreviate accordingly.
578 (while tail
579 (if (string-match (car (car tail)) filename)
580 (setq filename
581 (concat (cdr (car tail)) (substring filename (match-end 0)))))
582 (setq tail (cdr tail)))
583 ;; Compute and save the abbreviated homedir name.
584 ;; We defer computing this until the first time it's needed, to
585 ;; give time for directory-abbrev-alist to be set properly.
586 ;; We include a slash at the end, to avoid spurious matches
587 ;; such as `/usr/foobar' when the home dir is `/usr/foo'.
588 (or abbreviated-home-dir
589 (setq abbreviated-home-dir
590 (let ((abbreviated-home-dir "$foo"))
591 (concat "^" (abbreviate-file-name (expand-file-name "~"))
592 "\\(/\\|$\\)"))))
593
594 ;; If FILENAME starts with the abbreviated homedir,
595 ;; make it start with `~' instead.
596 (if (and (string-match abbreviated-home-dir filename)
597 ;; If the home dir is just /, don't change it.
598 (not (and (= (match-end 0) 1)
599 (= (aref filename 0) ?/)))
600 (not (and (or (eq system-type 'ms-dos)
601 (eq system-type 'windows-nt))
602 (save-match-data
603 (string-match "^[a-zA-Z]:/$" filename)))))
604 (setq filename
605 (concat "~"
606 (substring filename (match-beginning 1) (match-end 1))
607 (substring filename (match-end 0)))))
608 filename))
609
610 (defvar find-file-not-true-dirname-list nil
611 "*List of logical names for which visiting shouldn't save the true dirname.
612 On VMS, when you visit a file using a logical name that searches a path,
613 you may or may not want the visited file name to record the specific
614 directory where the file was found. If you *do not* want that, add the logical
615 name to this list as a string.")
616
617 (defun find-buffer-visiting (filename)
618 "Return the buffer visiting file FILENAME (a string).
619 This is like `get-file-buffer', except that it checks for any buffer
620 visiting the same file, possibly under a different name.
621 If there is no such live buffer, return nil."
622 (let ((buf (get-file-buffer filename))
623 (truename (abbreviate-file-name (file-truename filename))))
624 (or buf
625 (let ((list (buffer-list)) found)
626 (while (and (not found) list)
627 (save-excursion
628 (set-buffer (car list))
629 (if (and buffer-file-name
630 (string= buffer-file-truename truename))
631 (setq found (car list))))
632 (setq list (cdr list)))
633 found)
634 (let ((number (nthcdr 10 (file-attributes truename)))
635 (list (buffer-list)) found)
636 (and number
637 (while (and (not found) list)
638 (save-excursion
639 (set-buffer (car list))
640 (if (and buffer-file-name
641 (equal buffer-file-number number)
642 ;; Verify this buffer's file number
643 ;; still belongs to its file.
644 (file-exists-p buffer-file-name)
645 (equal (nthcdr 10 (file-attributes buffer-file-name))
646 number))
647 (setq found (car list))))
648 (setq list (cdr list))))
649 found))))
650
651 (defun find-file-noselect (filename &optional nowarn)
652 "Read file FILENAME into a buffer and return the buffer.
653 If a buffer exists visiting FILENAME, return that one, but
654 verify that the file has not changed since visited or saved.
655 The buffer is not selected, just returned to the caller."
656 (setq filename
657 (abbreviate-file-name
658 (expand-file-name filename)))
659 (if (file-directory-p filename)
660 (if find-file-run-dired
661 (dired-noselect filename)
662 (error "%s is a directory." filename))
663 (let* ((buf (get-file-buffer filename))
664 (truename (abbreviate-file-name (file-truename filename)))
665 (number (nthcdr 10 (file-attributes truename)))
666 ;; Find any buffer for a file which has same truename.
667 (other (and (not buf) (find-buffer-visiting filename)))
668 error)
669 ;; Let user know if there is a buffer with the same truename.
670 (if other
671 (progn
672 (or nowarn
673 (string-equal filename (buffer-file-name other))
674 (message "%s and %s are the same file"
675 filename (buffer-file-name other)))
676 ;; Optionally also find that buffer.
677 (if (or find-file-existing-other-name find-file-visit-truename)
678 (setq buf other))))
679 (if buf
680 (or nowarn
681 (verify-visited-file-modtime buf)
682 (cond ((not (file-exists-p filename))
683 (error "File %s no longer exists!" filename))
684 ((yes-or-no-p
685 (format
686 (if (buffer-modified-p buf)
687 "File %s changed on disk. Discard your edits? "
688 "File %s changed on disk. Reread from disk? ")
689 (file-name-nondirectory filename)))
690 (save-excursion
691 (set-buffer buf)
692 (revert-buffer t t)))))
693 (save-excursion
694 ;;; The truename stuff makes this obsolete.
695 ;;; (let* ((link-name (car (file-attributes filename)))
696 ;;; (linked-buf (and (stringp link-name)
697 ;;; (get-file-buffer link-name))))
698 ;;; (if (bufferp linked-buf)
699 ;;; (message "Symbolic link to file in buffer %s"
700 ;;; (buffer-name linked-buf))))
701 (setq buf (create-file-buffer filename))
702 (set-buffer-major-mode buf)
703 (set-buffer buf)
704 (erase-buffer)
705 (condition-case ()
706 (insert-file-contents filename t)
707 (file-error
708 ;; Run find-file-not-found-hooks until one returns non-nil.
709 (or (run-hook-with-args-until-success 'find-file-not-found-hooks)
710 ;; If they fail too, set error.
711 (setq error t))))
712 ;; Find the file's truename, and maybe use that as visited name.
713 (setq buffer-file-truename truename)
714 (setq buffer-file-number number)
715 ;; On VMS, we may want to remember which directory in a search list
716 ;; the file was found in.
717 (and (eq system-type 'vax-vms)
718 (let (logical)
719 (if (string-match ":" (file-name-directory filename))
720 (setq logical (substring (file-name-directory filename)
721 0 (match-beginning 0))))
722 (not (member logical find-file-not-true-dirname-list)))
723 (setq buffer-file-name buffer-file-truename))
724 (if find-file-visit-truename
725 (setq buffer-file-name
726 (setq filename
727 (expand-file-name buffer-file-truename))))
728 ;; Set buffer's default directory to that of the file.
729 (setq default-directory (file-name-directory filename))
730 ;; Turn off backup files for certain file names. Since
731 ;; this is a permanent local, the major mode won't eliminate it.
732 (and (not (funcall backup-enable-predicate buffer-file-name))
733 (progn
734 (make-local-variable 'backup-inhibited)
735 (setq backup-inhibited t)))
736 (after-find-file error (not nowarn))))
737 buf)))
738 \f
739 (defvar after-find-file-from-revert-buffer nil)
740
741 (defun after-find-file (&optional error warn noauto
742 after-find-file-from-revert-buffer)
743 "Called after finding a file and by the default revert function.
744 Sets buffer mode, parses local variables.
745 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an
746 error in reading the file. WARN non-nil means warn if there
747 exists an auto-save file more recent than the visited file.
748 NOAUTO means don't mess with auto-save mode.
749 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil
750 means this call was from `revert-buffer'.
751 Finishes by calling the functions in `find-file-hooks'."
752 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
753 (if noninteractive
754 nil
755 (let* (not-serious
756 (msg
757 (cond ((and error (file-attributes buffer-file-name))
758 (setq buffer-read-only t)
759 "File exists, but cannot be read.")
760 ((not buffer-read-only)
761 (if (and warn
762 (file-newer-than-file-p (make-auto-save-file-name)
763 buffer-file-name))
764 "Auto save file is newer; consider M-x recover-file"
765 (setq not-serious t)
766 (if error "(New file)" nil)))
767 ((not error)
768 (setq not-serious t)
769 "Note: file is write protected")
770 ((file-attributes (directory-file-name default-directory))
771 "File not found and directory write-protected")
772 ((file-exists-p (file-name-directory buffer-file-name))
773 (setq buffer-read-only nil))
774 (t
775 (setq buffer-read-only nil)
776 (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name))))
777 "Use M-x make-dir RET RET to create the directory"
778 "Use C-u M-x make-dir RET RET to create directory and its parents")))))
779 (if msg
780 (progn
781 (message msg)
782 (or not-serious (sit-for 1 nil t)))))
783 (if (and auto-save-default (not noauto))
784 (auto-save-mode t)))
785 (normal-mode t)
786 (run-hooks 'find-file-hooks))
787
788 (defun normal-mode (&optional find-file)
789 "Choose the major mode for this buffer automatically.
790 Also sets up any specified local variables of the file.
791 Uses the visited file name, the -*- line, and the local variables spec.
792
793 This function is called automatically from `find-file'. In that case,
794 we may set up specified local variables depending on the value of
795 `enable-local-variables': if it is t, we do; if it is nil, we don't;
796 otherwise, we query. `enable-local-variables' is ignored if you
797 run `normal-mode' explicitly."
798 (interactive)
799 (or find-file (funcall (or default-major-mode 'fundamental-mode)))
800 (condition-case err
801 (set-auto-mode)
802 (error (message "File mode specification error: %s"
803 (prin1-to-string err))))
804 (condition-case err
805 (let ((enable-local-variables (or (not find-file)
806 enable-local-variables)))
807 (hack-local-variables))
808 (error (message "File local-variables error: %s"
809 (prin1-to-string err)))))
810
811 (defvar auto-mode-alist (mapcar 'purecopy
812 '(("\\.text\\'" . text-mode)
813 ("\\.c\\'" . c-mode)
814 ("\\.h\\'" . c-mode)
815 ("\\.tex\\'" . tex-mode)
816 ("\\.ltx\\'" . latex-mode)
817 ("\\.el\\'" . emacs-lisp-mode)
818 ("\\.mm\\'" . nroff-mode)
819 ("\\.me\\'" . nroff-mode)
820 ("\\.ms\\'" . nroff-mode)
821 ("\\.man\\'" . nroff-mode)
822 ("\\.scm\\'" . scheme-mode)
823 ("\\.l\\'" . lisp-mode)
824 ("\\.lisp\\'" . lisp-mode)
825 ("\\.f\\'" . fortran-mode)
826 ("\\.for\\'" . fortran-mode)
827 ("\\.p\\'" . pascal-mode)
828 ("\\.pas\\'" . pascal-mode)
829 ("\\.mss\\'" . scribe-mode)
830 ("\\.ada\\'" . ada-mode)
831 ("\\.icn\\'" . icon-mode)
832 ("\\.pl\\'" . prolog-mode)
833 ("\\.cc\\'" . c++-mode)
834 ("\\.hh\\'" . c++-mode)
835 ("\\.C\\'" . c++-mode)
836 ("\\.H\\'" . c++-mode)
837 ("\\.cpp\\'" . c++-mode)
838 ("\\.cxx\\'" . c++-mode)
839 ("\\.hxx\\'" . c++-mode)
840 ("\\.c\\+\\+\\'" . c++-mode)
841 ("\\.h\\+\\+\\'" . c++-mode)
842 ;;; ("\\.mk\\'" . makefile-mode)
843 ;;; ("[Mm]akefile" . makefile-mode)
844 ;;; Less common extensions come here
845 ;;; so more common ones above are found faster.
846 ("\\.texinfo\\'" . texinfo-mode)
847 ("\\.texi\\'" . texinfo-mode)
848 ("\\.s\\'" . asm-mode)
849 ("ChangeLog\\'" . change-log-mode)
850 ("change.log\\'" . change-log-mode)
851 ("changelo\\'" . change-log-mode)
852 ("ChangeLog.[0-9]+\\'" . change-log-mode)
853 ("\\$CHANGE_LOG\\$\\.TXT" . change-log-mode)
854 ;; The following should come after the ChangeLog pattern
855 ;; for the sake of ChangeLog.1, etc.
856 ("\\.[12345678]\\'" . nroff-mode)
857 ("\\.TeX\\'" . tex-mode)
858 ("\\.sty\\'" . latex-mode)
859 ("\\.cls\\'" . latex-mode) ;LaTeX 2e class
860 ("\\.bbl\\'" . latex-mode)
861 ("\\.bib\\'" . bibtex-mode)
862 ("\\.article\\'" . text-mode)
863 ("\\.letter\\'" . text-mode)
864 ("\\.tcl\\'" . tcl-mode)
865 ("\\.lsp\\'" . lisp-mode)
866 ("\\.awk\\'" . awk-mode)
867 ("\\.prolog\\'" . prolog-mode)
868 ("\\.tar\\'" . tar-mode)
869 ;; Mailer puts message to be edited in
870 ;; /tmp/Re.... or Message
871 ("^/tmp/Re" . text-mode)
872 ("/Message[0-9]*\\'" . text-mode)
873 ("/drafts/[0-9]+\\'" . mh-letter-mode)
874 ;; some news reader is reported to use this
875 ("^/tmp/fol/" . text-mode)
876 ("\\.y\\'" . c-mode)
877 ("\\.lex\\'" . c-mode)
878 ("\\.oak\\'" . scheme-mode)
879 ("\\.scm.[0-9]*\\'" . scheme-mode)
880 ("\\.sgm\\'" . sgml-mode)
881 ("\\.sgml\\'" . sgml-mode)
882 ("\\.dtd\\'" . sgml-mode)
883 ;; .emacs following a directory delimiter
884 ;; in either Unix or VMS syntax.
885 ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode)
886 ;; _emacs following a directory delimiter
887 ;; in MsDos syntax
888 ("[:/]_emacs\\'" . emacs-lisp-mode)
889 ("\\.ml\\'" . lisp-mode)))
890 "\
891 Alist of filename patterns vs corresponding major mode functions.
892 Each element looks like (REGEXP . FUNCTION) or (REGEXP FUNCTION NON-NIL).
893 \(NON-NIL stands for anything that is not nil; the value does not matter.)
894 Visiting a file whose name matches REGEXP specifies FUNCTION as the
895 mode function to use. FUNCTION will be called, unless it is nil.
896
897 If the element has the form (REGEXP FUNCTION NON-NIL), then after
898 calling FUNCTION (if it's not nil), we delete the suffix that matched
899 REGEXP and search the list again for another match.")
900
901 (defconst interpreter-mode-alist
902 '(("perl" . perl-mode)
903 ("wish" . tcl-mode)
904 ("wishx" . tcl-mode)
905 ("tcl" . tcl-mode)
906 ("tclsh" . tcl-mode)
907 ("awk" . awk-mode)
908 ("gawk" . awk-mode)
909 ("scm" . scheme-mode))
910 "Alist mapping interpreter names to major modes.
911 This alist applies to files whose first line starts with `#!'.
912 Each element looks like (INTERPRETER . MODE).
913 The car of each element is compared with
914 the name of the interpreter specified in the first line.
915 If it matches, mode MODE is selected.")
916
917 (defconst inhibit-first-line-modes-regexps '("\\.tar\\'")
918 "List of regexps; if one matches a file name, don't look for `-*-'.")
919
920 (defconst inhibit-first-line-modes-suffixes nil
921 "List of regexps for what to ignore, for `inhibit-first-line-modes-regexps'.
922 When checking `inhibit-first-line-modes-regexps', we first discard
923 from the end of the file name anything that matches one of these regexps.")
924
925 (defvar user-init-file
926 "" ; set by command-line
927 "File name including directory of user's initialization file.")
928
929 (defun set-auto-mode ()
930 "Select major mode appropriate for current buffer.
931 This checks for a -*- mode tag in the buffer's text,
932 compares the filename against the entries in `auto-mode-alist',
933 or checks the interpreter that runs this file against
934 `interpreter-mode-alist'.
935
936 It does not check for the `mode:' local variable in the
937 Local Variables section of the file; for that, use `hack-local-variables'.
938
939 If `enable-local-variables' is nil, this function does not check for a
940 -*- mode tag."
941 ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
942 (let (beg end done)
943 (save-excursion
944 (goto-char (point-min))
945 (skip-chars-forward " \t\n")
946 (and enable-local-variables
947 ;; Don't look for -*- if this file name matches any
948 ;; of the regexps in inhibit-first-line-modes-regexps.
949 (let ((temp inhibit-first-line-modes-regexps)
950 (name (file-name-sans-versions buffer-file-name)))
951 (while (let ((sufs inhibit-first-line-modes-suffixes))
952 (while (and sufs (not (string-match (car sufs) name)))
953 (setq sufs (cdr sufs)))
954 sufs)
955 (setq name (substring name 0 (match-beginning 0))))
956 (while (and temp
957 (not (string-match (car temp) name)))
958 (setq temp (cdr temp)))
959 (not temp))
960 (search-forward "-*-" (save-excursion
961 ;; If the file begins with "#!"
962 ;; (exec interpreter magic), look
963 ;; for mode frobs in the first two
964 ;; lines. You cannot necessarily
965 ;; put them in the first line of
966 ;; such a file without screwing up
967 ;; the interpreter invocation.
968 (end-of-line (and (looking-at "^#!") 2))
969 (point)) t)
970 (progn
971 (skip-chars-forward " \t")
972 (setq beg (point))
973 (search-forward "-*-"
974 (save-excursion (end-of-line) (point))
975 t))
976 (progn
977 (forward-char -3)
978 (skip-chars-backward " \t")
979 (setq end (point))
980 (goto-char beg)
981 (if (save-excursion (search-forward ":" end t))
982 ;; Find all specifications for the `mode:' variable
983 ;; and execute them left to right.
984 (while (let ((case-fold-search t))
985 (or (and (looking-at "mode:")
986 (goto-char (match-end 0)))
987 (re-search-forward "[ \t;]mode:" end t)))
988 (skip-chars-forward " \t")
989 (setq beg (point))
990 (if (search-forward ";" end t)
991 (forward-char -1)
992 (goto-char end))
993 (skip-chars-backward " \t")
994 (funcall (intern (concat (downcase (buffer-substring beg (point))) "-mode"))))
995 ;; Simple -*-MODE-*- case.
996 (funcall (intern (concat (downcase (buffer-substring beg end)) "-mode"))))
997 (setq done t)))
998 ;; If we didn't find a mode from a -*- line, try using the file name.
999 (if (and (not done) buffer-file-name)
1000 (let ((name buffer-file-name)
1001 (keep-going t))
1002 ;; Remove backup-suffixes from file name.
1003 (setq name (file-name-sans-versions name))
1004 (while keep-going
1005 (setq keep-going nil)
1006 (let ((alist auto-mode-alist)
1007 (mode nil))
1008 ;; Find first matching alist entry.
1009 (let ((case-fold-search (eq system-type 'vax-vms)))
1010 (while (and (not mode) alist)
1011 (if (string-match (car (car alist)) name)
1012 (if (and (consp (cdr (car alist)))
1013 (nth 2 (car alist)))
1014 (progn
1015 (setq mode (car (cdr (car alist)))
1016 name (substring name 0 (match-beginning 0))
1017 keep-going t))
1018 (setq mode (cdr (car alist))
1019 keep-going nil)))
1020 (setq alist (cdr alist))))
1021 (if mode
1022 (funcall mode)
1023 ;; If we can't deduce a mode from the file name,
1024 ;; look for an interpreter specified in the first line.
1025 (let ((interpreter
1026 (save-excursion
1027 (goto-char (point-min))
1028 (if (looking-at "#! *\\([^ \t\n]+\\)")
1029 (buffer-substring (match-beginning 1)
1030 (match-end 1))
1031 "")))
1032 elt)
1033 ;; Map interpreter name to a mode.
1034 (setq elt (assoc (file-name-nondirectory interpreter)
1035 interpreter-mode-alist))
1036 (if elt
1037 (funcall (cdr elt))))))))))))
1038
1039 (defun hack-local-variables-prop-line ()
1040 ;; Set local variables specified in the -*- line.
1041 ;; Ignore any specification for `mode:';
1042 ;; set-auto-mode should already have handled that.
1043 (save-excursion
1044 (goto-char (point-min))
1045 (let ((result nil)
1046 (end (save-excursion (end-of-line (and (looking-at "^#!") 2)) (point))))
1047 ;; Parse the -*- line into the `result' alist.
1048 (cond ((not (search-forward "-*-" end t))
1049 ;; doesn't have one.
1050 nil)
1051 ((looking-at "[ \t]*\\([^ \t\n\r:;]+\\)\\([ \t]*-\\*-\\)")
1052 ;; Simple form: "-*- MODENAME -*-". Already handled.
1053 nil)
1054 (t
1055 ;; Hairy form: '-*-' [ <variable> ':' <value> ';' ]* '-*-'
1056 ;; (last ";" is optional).
1057 (save-excursion
1058 (if (search-forward "-*-" end t)
1059 (setq end (- (point) 3))
1060 (error "-*- not terminated before end of line")))
1061 (while (< (point) end)
1062 (or (looking-at "[ \t]*\\([^ \t\n:]+\\)[ \t]*:[ \t]*")
1063 (error "malformed -*- line"))
1064 (goto-char (match-end 0))
1065 (let ((key (intern (downcase (buffer-substring
1066 (match-beginning 1)
1067 (match-end 1)))))
1068 (val (save-restriction
1069 (narrow-to-region (point) end)
1070 (read (current-buffer)))))
1071 (or (eq key 'mode)
1072 (setq result (cons (cons key val) result)))
1073 (skip-chars-forward " \t;")))
1074 (setq result (nreverse result))))
1075
1076 (if (and result
1077 (or (eq enable-local-variables t)
1078 (and enable-local-variables
1079 (save-window-excursion
1080 (condition-case nil
1081 (switch-to-buffer (current-buffer))
1082 (error
1083 ;; If we fail to switch in the selected window,
1084 ;; it is probably a minibuffer.
1085 ;; So try another window.
1086 (condition-case nil
1087 (switch-to-buffer-other-window (current-buffer))
1088 (error
1089 (switch-to-buffer-other-frame (current-buffer))))))
1090 (y-or-n-p (format "Set local variables as specified in -*- line of %s? "
1091 (file-name-nondirectory buffer-file-name)))))))
1092 (while result
1093 (hack-one-local-variable (car (car result)) (cdr (car result)))
1094 (setq result (cdr result)))))))
1095
1096 (defvar hack-local-variables-hook nil
1097 "Normal hook run after processing a file's local variables specs.
1098 Major modes can use this to examine user-specified local variables
1099 in order to initialize other data structure based on them.")
1100
1101 (defun hack-local-variables ()
1102 "Parse and put into effect this buffer's local variables spec."
1103 (hack-local-variables-prop-line)
1104 ;; Look for "Local variables:" line in last page.
1105 (save-excursion
1106 (goto-char (point-max))
1107 (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
1108 (if (let ((case-fold-search t))
1109 (and (search-forward "Local Variables:" nil t)
1110 (or (eq enable-local-variables t)
1111 (and enable-local-variables
1112 (save-window-excursion
1113 (switch-to-buffer (current-buffer))
1114 (save-excursion
1115 (beginning-of-line)
1116 (set-window-start (selected-window) (point)))
1117 (y-or-n-p (format "Set local variables as specified at end of %s? "
1118 (if buffer-file-name
1119 (file-name-nondirectory
1120 buffer-file-name)
1121 (concat "buffer "
1122 (buffer-name))))))))))
1123 (let ((continue t)
1124 prefix prefixlen suffix beg
1125 (enable-local-eval enable-local-eval))
1126 ;; The prefix is what comes before "local variables:" in its line.
1127 ;; The suffix is what comes after "local variables:" in its line.
1128 (skip-chars-forward " \t")
1129 (or (eolp)
1130 (setq suffix (buffer-substring (point)
1131 (progn (end-of-line) (point)))))
1132 (goto-char (match-beginning 0))
1133 (or (bolp)
1134 (setq prefix
1135 (buffer-substring (point)
1136 (progn (beginning-of-line) (point)))))
1137
1138 (if prefix (setq prefixlen (length prefix)
1139 prefix (regexp-quote prefix)))
1140 (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
1141 (while continue
1142 ;; Look at next local variable spec.
1143 (if selective-display (re-search-forward "[\n\C-m]")
1144 (forward-line 1))
1145 ;; Skip the prefix, if any.
1146 (if prefix
1147 (if (looking-at prefix)
1148 (forward-char prefixlen)
1149 (error "Local variables entry is missing the prefix")))
1150 ;; Find the variable name; strip whitespace.
1151 (skip-chars-forward " \t")
1152 (setq beg (point))
1153 (skip-chars-forward "^:\n")
1154 (if (eolp) (error "Missing colon in local variables entry"))
1155 (skip-chars-backward " \t")
1156 (let* ((str (buffer-substring beg (point)))
1157 (var (read str))
1158 val)
1159 ;; Setting variable named "end" means end of list.
1160 (if (string-equal (downcase str) "end")
1161 (setq continue nil)
1162 ;; Otherwise read the variable value.
1163 (skip-chars-forward "^:")
1164 (forward-char 1)
1165 (setq val (read (current-buffer)))
1166 (skip-chars-backward "\n")
1167 (skip-chars-forward " \t")
1168 (or (if suffix (looking-at suffix) (eolp))
1169 (error "Local variables entry is terminated incorrectly"))
1170 ;; Set the variable. "Variables" mode and eval are funny.
1171 (hack-one-local-variable var val)))))))
1172 (run-hooks 'hack-local-variables-hook))
1173
1174 (defconst ignored-local-variables
1175 '(enable-local-eval)
1176 "Variables to be ignored in a file's local variable spec.")
1177
1178 ;; Get confirmation before setting these variables as locals in a file.
1179 (put 'debugger 'risky-local-variable t)
1180 (put 'enable-local-eval 'risky-local-variable t)
1181 (put 'ignored-local-variables 'risky-local-variable t)
1182 (put 'eval 'risky-local-variable t)
1183 (put 'file-name-handler-alist 'risky-local-variable t)
1184 (put 'minor-mode-map-alist 'risky-local-variable t)
1185 (put 'after-load-alist 'risky-local-variable t)
1186 (put 'buffer-file-name 'risky-local-variable t)
1187 (put 'buffer-auto-save-file-name 'risky-local-variable t)
1188 (put 'buffer-file-truename 'risky-local-variable t)
1189 (put 'exec-path 'risky-local-variable t)
1190 (put 'load-path 'risky-local-variable t)
1191 (put 'exec-directory 'risky-local-variable t)
1192 (put 'process-environment 'risky-local-variable t)
1193 ;; Don't wait for outline.el to be loaded, for the sake of outline-minor-mode.
1194 (put 'outline-level 'risky-local-variable t)
1195 (put 'rmail-output-file-alist 'risky-local-variable t)
1196
1197 ;; This one is safe because the user gets to check it before it is used.
1198 (put 'compile-command 'safe-local-variable t)
1199
1200 (defun hack-one-local-variable-quotep (exp)
1201 (and (consp exp) (eq (car exp) 'quote) (consp (cdr exp))))
1202
1203 ;; "Set" one variable in a local variables spec.
1204 ;; A few variable names are treated specially.
1205 (defun hack-one-local-variable (var val)
1206 (cond ((eq var 'mode)
1207 (funcall (intern (concat (downcase (symbol-name val))
1208 "-mode"))))
1209 ((memq var ignored-local-variables)
1210 nil)
1211 ;; "Setting" eval means either eval it or do nothing.
1212 ;; Likewise for setting hook variables.
1213 ((or (get var 'risky-local-variable)
1214 (and
1215 (string-match "-hooks?$\\|-functions?$\\|-forms?$\\|-program$\\|-command$"
1216 (symbol-name var))
1217 (not (get var 'safe-local-variable))))
1218 ;; Permit evaling a put of a harmless property
1219 ;; if the args do nothing tricky.
1220 (if (or (and (eq var 'eval)
1221 (consp val)
1222 (eq (car val) 'put)
1223 (hack-one-local-variable-quotep (nth 1 val))
1224 (hack-one-local-variable-quotep (nth 2 val))
1225 ;; Only allow safe values of lisp-indent-hook;
1226 ;; not functions.
1227 (or (numberp (nth 3 val))
1228 (equal (nth 3 val) ''defun))
1229 (memq (nth 1 (nth 2 val))
1230 '(lisp-indent-hook)))
1231 ;; Permit eval if not root and user says ok.
1232 (and (not (zerop (user-uid)))
1233 (or (eq enable-local-eval t)
1234 (and enable-local-eval
1235 (save-window-excursion
1236 (switch-to-buffer (current-buffer))
1237 (save-excursion
1238 (beginning-of-line)
1239 (set-window-start (selected-window) (point)))
1240 (setq enable-local-eval
1241 (y-or-n-p (format "Process `eval' or hook local variables in file %s? "
1242 (file-name-nondirectory buffer-file-name)))))))))
1243 (if (eq var 'eval)
1244 (save-excursion (eval val))
1245 (make-local-variable var)
1246 (set var val))
1247 (message "Ignoring `eval:' in file's local variables")))
1248 ;; Ordinary variable, really set it.
1249 (t (make-local-variable var)
1250 (set var val))))
1251
1252 \f
1253 (defun set-visited-file-name (filename)
1254 "Change name of file visited in current buffer to FILENAME.
1255 The next time the buffer is saved it will go in the newly specified file.
1256 nil or empty string as argument means make buffer not be visiting any file.
1257 Remember to delete the initial contents of the minibuffer
1258 if you wish to pass an empty string as the argument."
1259 (interactive "FSet visited file name: ")
1260 (if (buffer-base-buffer)
1261 (error "An indirect buffer cannot visit a file"))
1262 (let (truename)
1263 (if filename
1264 (setq filename
1265 (if (string-equal filename "")
1266 nil
1267 (expand-file-name filename))))
1268 (if filename
1269 (progn
1270 (setq truename (file-truename filename))
1271 (if find-file-visit-truename
1272 ;; Do not use the abbreviated filename, because
1273 ;; write-region will reset it to the expanded filename
1274 (setq filename truename))))
1275 (or (equal filename buffer-file-name)
1276 (progn
1277 (and filename (lock-buffer filename))
1278 (unlock-buffer)))
1279 (setq buffer-file-name filename)
1280 (if filename ; make buffer name reflect filename.
1281 (let ((new-name (file-name-nondirectory buffer-file-name)))
1282 (if (string= new-name "")
1283 (error "Empty file name"))
1284 (if (eq system-type 'vax-vms)
1285 (setq new-name (downcase new-name)))
1286 (setq default-directory (file-name-directory buffer-file-name))
1287 (or (string= new-name (buffer-name))
1288 (rename-buffer new-name t))))
1289 (setq buffer-backed-up nil)
1290 (clear-visited-file-modtime)
1291 (if truename
1292 (setq buffer-file-truename (abbreviate-file-name truename)))
1293 (setq buffer-file-number
1294 (if filename
1295 (nth 10 (file-attributes buffer-file-name))
1296 nil)))
1297 ;; write-file-hooks is normally used for things like ftp-find-file
1298 ;; that visit things that are not local files as if they were files.
1299 ;; Changing to visit an ordinary local file instead should flush the hook.
1300 (kill-local-variable 'write-file-hooks)
1301 (kill-local-variable 'local-write-file-hooks)
1302 (kill-local-variable 'revert-buffer-function)
1303 (kill-local-variable 'backup-inhibited)
1304 ;; If buffer was read-only because of version control,
1305 ;; that reason is gone now, so make it writable.
1306 (if vc-mode
1307 (setq buffer-read-only nil))
1308 (kill-local-variable 'vc-mode)
1309 ;; Turn off backup files for certain file names.
1310 ;; Since this is a permanent local, the major mode won't eliminate it.
1311 (and (not (funcall backup-enable-predicate buffer-file-name))
1312 (progn
1313 (make-local-variable 'backup-inhibited)
1314 (setq backup-inhibited t)))
1315 (let ((oauto buffer-auto-save-file-name))
1316 ;; If auto-save was not already on, turn it on if appropriate.
1317 (if (not buffer-auto-save-file-name)
1318 (and buffer-file-name auto-save-default
1319 (auto-save-mode t))
1320 ;; If auto save is on, start using a new name.
1321 ;; We deliberately don't rename or delete the old auto save
1322 ;; for the old visited file name. This is because perhaps
1323 ;; the user wants to save the new state and then compare with the
1324 ;; previous state from the auto save file.
1325 (setq buffer-auto-save-file-name
1326 (make-auto-save-file-name)))
1327 ;; Rename the old auto save file if any.
1328 (and oauto buffer-auto-save-file-name
1329 (file-exists-p oauto)
1330 (rename-file oauto buffer-auto-save-file-name t)))
1331 (if buffer-file-name
1332 (set-buffer-modified-p t)))
1333
1334 (defun write-file (filename)
1335 "Write current buffer into file FILENAME.
1336 Makes buffer visit that file, and marks it not modified.
1337 If the buffer is already visiting a file, you can specify
1338 a directory name as FILENAME, to write a file of the same
1339 old name in that directory."
1340 ;; (interactive "FWrite file: ")
1341 (interactive
1342 (list (if buffer-file-name
1343 (read-file-name "Write file: "
1344 nil nil nil nil)
1345 (read-file-name "Write file: "
1346 (cdr (assq 'default-directory
1347 (buffer-local-variables)))
1348 nil nil (buffer-name)))))
1349 (or (null filename) (string-equal filename "")
1350 (progn
1351 ;; If arg is just a directory,
1352 ;; use same file name, but in that directory.
1353 (if (and (file-directory-p filename) buffer-file-name)
1354 (setq filename (concat (file-name-as-directory filename)
1355 (file-name-nondirectory buffer-file-name))))
1356 (if (file-exists-p filename)
1357 (or (y-or-n-p (format "File `%s' exists; overwrite? " filename))
1358 (error "Canceled")))
1359 (set-visited-file-name filename)))
1360 (set-buffer-modified-p t)
1361 (save-buffer))
1362 \f
1363 (defun backup-buffer ()
1364 "Make a backup of the disk file visited by the current buffer, if appropriate.
1365 This is normally done before saving the buffer the first time.
1366 If the value is non-nil, it is the result of `file-modes' on the original
1367 file; this means that the caller, after saving the buffer, should change
1368 the modes of the new file to agree with the old modes."
1369 (if (and make-backup-files (not backup-inhibited)
1370 (not buffer-backed-up)
1371 (file-exists-p buffer-file-name)
1372 (memq (aref (elt (file-attributes buffer-file-name) 8) 0)
1373 '(?- ?l)))
1374 (let ((real-file-name buffer-file-name)
1375 backup-info backupname targets setmodes)
1376 ;; If specified name is a symbolic link, chase it to the target.
1377 ;; Thus we make the backups in the directory where the real file is.
1378 (setq real-file-name (file-chase-links real-file-name))
1379 (setq backup-info (find-backup-file-name real-file-name)
1380 backupname (car backup-info)
1381 targets (cdr backup-info))
1382 ;;; (if (file-directory-p buffer-file-name)
1383 ;;; (error "Cannot save buffer in directory %s" buffer-file-name))
1384 (if backup-info
1385 (condition-case ()
1386 (let ((delete-old-versions
1387 ;; If have old versions to maybe delete,
1388 ;; ask the user to confirm now, before doing anything.
1389 ;; But don't actually delete til later.
1390 (and targets
1391 (or (eq delete-old-versions t) (eq delete-old-versions nil))
1392 (or delete-old-versions
1393 (y-or-n-p (format "Delete excess backup versions of %s? "
1394 real-file-name))))))
1395 ;; Actually write the back up file.
1396 (condition-case ()
1397 (if (or file-precious-flag
1398 ; (file-symlink-p buffer-file-name)
1399 backup-by-copying
1400 (and backup-by-copying-when-linked
1401 (> (file-nlinks real-file-name) 1))
1402 (and backup-by-copying-when-mismatch
1403 (let ((attr (file-attributes real-file-name)))
1404 (or (nth 9 attr)
1405 (not (file-ownership-preserved-p real-file-name))))))
1406 (condition-case ()
1407 (copy-file real-file-name backupname t t)
1408 (file-error
1409 ;; If copying fails because file BACKUPNAME
1410 ;; is not writable, delete that file and try again.
1411 (if (and (file-exists-p backupname)
1412 (not (file-writable-p backupname)))
1413 (delete-file backupname))
1414 (copy-file real-file-name backupname t t)))
1415 ;; rename-file should delete old backup.
1416 (rename-file real-file-name backupname t)
1417 (setq setmodes (file-modes backupname)))
1418 (file-error
1419 ;; If trouble writing the backup, write it in ~.
1420 (setq backupname (expand-file-name "~/%backup%~"))
1421 (message "Cannot write backup file; backing up in ~/%%backup%%~")
1422 (sleep-for 1)
1423 (condition-case ()
1424 (copy-file real-file-name backupname t t)
1425 (file-error
1426 ;; If copying fails because file BACKUPNAME
1427 ;; is not writable, delete that file and try again.
1428 (if (and (file-exists-p backupname)
1429 (not (file-writable-p backupname)))
1430 (delete-file backupname))
1431 (copy-file real-file-name backupname t t)))))
1432 (setq buffer-backed-up t)
1433 ;; Now delete the old versions, if desired.
1434 (if delete-old-versions
1435 (while targets
1436 (condition-case ()
1437 (delete-file (car targets))
1438 (file-error nil))
1439 (setq targets (cdr targets))))
1440 setmodes)
1441 (file-error nil))))))
1442
1443 (defun file-name-sans-versions (name &optional keep-backup-version)
1444 "Return FILENAME sans backup versions or strings.
1445 This is a separate procedure so your site-init or startup file can
1446 redefine it.
1447 If the optional argument KEEP-BACKUP-VERSION is non-nil,
1448 we do not remove backup version numbers, only true file version numbers."
1449 (let ((handler (find-file-name-handler name 'file-name-sans-versions)))
1450 (if handler
1451 (funcall handler 'file-name-sans-versions name keep-backup-version)
1452 (substring name 0
1453 (if (eq system-type 'vax-vms)
1454 ;; VMS version number is (a) semicolon, optional
1455 ;; sign, zero or more digits or (b) period, option
1456 ;; sign, zero or more digits, provided this is the
1457 ;; second period encountered outside of the
1458 ;; device/directory part of the file name.
1459 (or (string-match ";[-+]?[0-9]*\\'" name)
1460 (if (string-match "\\.[^]>:]*\\(\\.[-+]?[0-9]*\\)\\'"
1461 name)
1462 (match-beginning 1))
1463 (length name))
1464 (if keep-backup-version
1465 (length name)
1466 (or (string-match "\\.~[0-9]+~\\'" name)
1467 (string-match "~\\'" name)
1468 (length name))))))))
1469
1470 (defun file-ownership-preserved-p (file)
1471 "Returns t if deleting FILE and rewriting it would preserve the owner."
1472 (let ((handler (find-file-name-handler file 'file-ownership-preserved-p)))
1473 (if handler
1474 (funcall handler 'file-ownership-preserved-p file)
1475 (= (nth 2 (file-attributes file)) (user-uid)))))
1476
1477 (defun file-name-sans-extension (filename)
1478 "Return FILENAME sans final \"extension\".
1479 The extension, in a file name, is the part that follows the last `.'."
1480 (save-match-data
1481 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
1482 directory)
1483 (if (string-match "\\.[^.]*\\'" file)
1484 (if (setq directory (file-name-directory filename))
1485 (expand-file-name (substring file 0 (match-beginning 0))
1486 directory)
1487 (substring file 0 (match-beginning 0)))
1488 filename))))
1489
1490 (defun make-backup-file-name (file)
1491 "Create the non-numeric backup file name for FILE.
1492 This is a separate function so you can redefine it for customization."
1493 (if (eq system-type 'ms-dos)
1494 (let ((fn (file-name-nondirectory file)))
1495 (concat (file-name-directory file)
1496 (if (string-match "\\([^.]*\\)\\(\\..*\\)?" fn)
1497 (substring fn 0 (match-end 1)))
1498 ".bak"))
1499 (concat file "~")))
1500
1501 (defun backup-file-name-p (file)
1502 "Return non-nil if FILE is a backup file name (numeric or not).
1503 This is a separate function so you can redefine it for customization.
1504 You may need to redefine `file-name-sans-versions' as well."
1505 (if (eq system-type 'ms-dos)
1506 (string-match "\\.bak$" file)
1507 (string-match "~$" file)))
1508
1509 ;; This is used in various files.
1510 ;; The usage of bv-length is not very clean,
1511 ;; but I can't see a good alternative,
1512 ;; so as of now I am leaving it alone.
1513 (defun backup-extract-version (fn)
1514 "Given the name of a numeric backup file, return the backup number.
1515 Uses the free variable `bv-length', whose value should be
1516 the index in the name where the version number begins."
1517 (if (and (string-match "[0-9]+~$" fn bv-length)
1518 (= (match-beginning 0) bv-length))
1519 (string-to-int (substring fn bv-length -1))
1520 0))
1521
1522 ;; I believe there is no need to alter this behavior for VMS;
1523 ;; since backup files are not made on VMS, it should not get called.
1524 (defun find-backup-file-name (fn)
1525 "Find a file name for a backup file, and suggestions for deletions.
1526 Value is a list whose car is the name for the backup file
1527 and whose cdr is a list of old versions to consider deleting now.
1528 If the value is nil, don't make a backup."
1529 (let ((handler (find-file-name-handler fn 'find-backup-file-name)))
1530 ;; Run a handler for this function so that ange-ftp can refuse to do it.
1531 (if handler
1532 (funcall handler 'find-backup-file-name fn)
1533 (if (eq version-control 'never)
1534 (list (make-backup-file-name fn))
1535 (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
1536 (bv-length (length base-versions))
1537 possibilities
1538 (versions nil)
1539 (high-water-mark 0)
1540 (deserve-versions-p nil)
1541 (number-to-delete 0))
1542 (condition-case ()
1543 (setq possibilities (file-name-all-completions
1544 base-versions
1545 (file-name-directory fn))
1546 versions (sort (mapcar
1547 (function backup-extract-version)
1548 possibilities)
1549 '<)
1550 high-water-mark (apply 'max 0 versions)
1551 deserve-versions-p (or version-control
1552 (> high-water-mark 0))
1553 number-to-delete (- (length versions)
1554 kept-old-versions kept-new-versions -1))
1555 (file-error
1556 (setq possibilities nil)))
1557 (if (not deserve-versions-p)
1558 (list (make-backup-file-name fn))
1559 (cons (concat fn ".~" (int-to-string (1+ high-water-mark)) "~")
1560 (if (and (> number-to-delete 0)
1561 ;; Delete nothing if there is overflow
1562 ;; in the number of versions to keep.
1563 (>= (+ kept-new-versions kept-old-versions -1) 0))
1564 (mapcar (function (lambda (n)
1565 (concat fn ".~" (int-to-string n) "~")))
1566 (let ((v (nthcdr kept-old-versions versions)))
1567 (rplacd (nthcdr (1- number-to-delete) v) ())
1568 v))))))))))
1569
1570 (defun file-nlinks (filename)
1571 "Return number of names file FILENAME has."
1572 (car (cdr (file-attributes filename))))
1573
1574 (defun file-relative-name (filename &optional directory)
1575 "Convert FILENAME to be relative to DIRECTORY (default: default-directory)."
1576 (setq filename (expand-file-name filename)
1577 directory (file-name-as-directory (expand-file-name
1578 (or directory default-directory))))
1579 (let ((ancestor ""))
1580 (while (not (string-match (concat "^" (regexp-quote directory)) filename))
1581 (setq directory (file-name-directory (substring directory 0 -1))
1582 ancestor (concat "../" ancestor)))
1583 (concat ancestor (substring filename (match-end 0)))))
1584 \f
1585 (defun save-buffer (&optional args)
1586 "Save current buffer in visited file if modified. Versions described below.
1587 By default, makes the previous version into a backup file
1588 if previously requested or if this is the first save.
1589 With 1 or 3 \\[universal-argument]'s, marks this version
1590 to become a backup when the next save is done.
1591 With 2 or 3 \\[universal-argument]'s,
1592 unconditionally makes the previous version into a backup file.
1593 With argument of 0, never makes the previous version into a backup file.
1594
1595 If a file's name is FOO, the names of its numbered backup versions are
1596 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
1597 Numeric backups (rather than FOO~) will be made if value of
1598 `version-control' is not the atom `never' and either there are already
1599 numeric versions of the file being backed up, or `version-control' is
1600 non-nil.
1601 We don't want excessive versions piling up, so there are variables
1602 `kept-old-versions', which tells Emacs how many oldest versions to keep,
1603 and `kept-new-versions', which tells how many newest versions to keep.
1604 Defaults are 2 old versions and 2 new.
1605 `dired-kept-versions' controls dired's clean-directory (.) command.
1606 If `delete-old-versions' is nil, system will query user
1607 before trimming versions. Otherwise it does it silently."
1608 (interactive "p")
1609 (let ((modp (buffer-modified-p))
1610 (large (> (buffer-size) 50000))
1611 (make-backup-files (or (and make-backup-files (not (eq args 0)))
1612 (memq args '(16 64)))))
1613 (and modp (memq args '(16 64)) (setq buffer-backed-up nil))
1614 (if (and modp large) (message "Saving file %s..." (buffer-file-name)))
1615 (basic-save-buffer)
1616 (and modp (memq args '(4 64)) (setq buffer-backed-up nil))))
1617
1618 (defun delete-auto-save-file-if-necessary (&optional force)
1619 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
1620 Normally delete only if the file was written by this Emacs since
1621 the last real save, but optional arg FORCE non-nil means delete anyway."
1622 (and buffer-auto-save-file-name delete-auto-save-files
1623 (not (string= buffer-file-name buffer-auto-save-file-name))
1624 (or force (recent-auto-save-p))
1625 (progn
1626 (condition-case ()
1627 (delete-file buffer-auto-save-file-name)
1628 (file-error nil))
1629 (set-buffer-auto-saved))))
1630
1631 (defun basic-save-buffer ()
1632 "Save the current buffer in its visited file, if it has been modified."
1633 (interactive)
1634 (save-excursion
1635 ;; In an indirect buffer, save its base buffer instead.
1636 (if (buffer-base-buffer)
1637 (set-buffer (buffer-base-buffer)))
1638 (if (buffer-modified-p)
1639 (let ((recent-save (recent-auto-save-p))
1640 setmodes tempsetmodes)
1641 ;; On VMS, rename file and buffer to get rid of version number.
1642 (if (and (eq system-type 'vax-vms)
1643 (not (string= buffer-file-name
1644 (file-name-sans-versions buffer-file-name))))
1645 (let (buffer-new-name)
1646 ;; Strip VMS version number before save.
1647 (setq buffer-file-name
1648 (file-name-sans-versions buffer-file-name))
1649 ;; Construct a (unique) buffer name to correspond.
1650 (let ((buf (create-file-buffer (downcase buffer-file-name))))
1651 (setq buffer-new-name (buffer-name buf))
1652 (kill-buffer buf))
1653 (rename-buffer buffer-new-name)))
1654 ;; If buffer has no file name, ask user for one.
1655 (or buffer-file-name
1656 (set-visited-file-name
1657 (expand-file-name (read-file-name "File to save in: ") nil)))
1658 (or (verify-visited-file-modtime (current-buffer))
1659 (not (file-exists-p buffer-file-name))
1660 (yes-or-no-p
1661 (format "%s has changed since visited or saved. Save anyway? "
1662 (file-name-nondirectory buffer-file-name)))
1663 (error "Save not confirmed"))
1664 (save-restriction
1665 (widen)
1666 (and (> (point-max) 1)
1667 (/= (char-after (1- (point-max))) ?\n)
1668 (not (and (eq selective-display t)
1669 (= (char-after (1- (point-max))) ?\r)))
1670 (or (eq require-final-newline t)
1671 (and require-final-newline
1672 (y-or-n-p
1673 (format "Buffer %s does not end in newline. Add one? "
1674 (buffer-name)))))
1675 (save-excursion
1676 (goto-char (point-max))
1677 (insert ?\n)))
1678 (or (run-hook-with-args-until-success 'write-contents-hooks)
1679 (run-hook-with-args-until-success 'local-write-file-hooks)
1680 (run-hook-with-args-until-success 'write-file-hooks)
1681 ;; If a hook returned t, file is already "written".
1682 ;; Otherwise, write it the usual way now.
1683 (setq setmodes (basic-save-buffer-1)))
1684 (setq buffer-file-number (nth 10 (file-attributes buffer-file-name)))
1685 (if setmodes
1686 (condition-case ()
1687 (set-file-modes buffer-file-name setmodes)
1688 (error nil))))
1689 ;; If the auto-save file was recent before this command,
1690 ;; delete it now.
1691 (delete-auto-save-file-if-necessary recent-save)
1692 (run-hooks 'after-save-hook))
1693 (message "(No changes need to be saved)"))))
1694
1695 ;; This does the "real job" of writing a buffer into its visited file
1696 ;; and making a backup file. This is what is normally done
1697 ;; but inhibited if one of write-file-hooks returns non-nil.
1698 ;; It returns a value to store in setmodes.
1699 (defun basic-save-buffer-1 ()
1700 (let (tempsetmodes setmodes)
1701 (if (not (file-writable-p buffer-file-name))
1702 (let ((dir (file-name-directory buffer-file-name)))
1703 (if (not (file-directory-p dir))
1704 (error "%s is not a directory" dir)
1705 (if (not (file-exists-p buffer-file-name))
1706 (error "Directory %s write-protected" dir)
1707 (if (yes-or-no-p
1708 (format "File %s is write-protected; try to save anyway? "
1709 (file-name-nondirectory
1710 buffer-file-name)))
1711 (setq tempsetmodes t)
1712 (error "Attempt to save to a file which you aren't allowed to write"))))))
1713 (or buffer-backed-up
1714 (setq setmodes (backup-buffer)))
1715 (let ((dir (file-name-directory buffer-file-name)))
1716 (if (and file-precious-flag
1717 (file-writable-p dir))
1718 ;; If file is precious, write temp name, then rename it.
1719 ;; This requires write access to the containing dir,
1720 ;; which is why we don't try it if we don't have that access.
1721 (let ((realname buffer-file-name)
1722 tempname temp nogood i succeed
1723 (old-modtime (visited-file-modtime)))
1724 (setq i 0)
1725 (setq nogood t)
1726 ;; Find the temporary name to write under.
1727 (while nogood
1728 (setq tempname (format "%s#tmp#%d" dir i))
1729 (setq nogood (file-exists-p tempname))
1730 (setq i (1+ i)))
1731 (unwind-protect
1732 (progn (clear-visited-file-modtime)
1733 (write-region (point-min) (point-max)
1734 tempname nil realname)
1735 (setq succeed t))
1736 ;; If writing the temp file fails,
1737 ;; delete the temp file.
1738 (or succeed
1739 (progn
1740 (delete-file tempname)
1741 (set-visited-file-modtime old-modtime))))
1742 ;; Since we have created an entirely new file
1743 ;; and renamed it, make sure it gets the
1744 ;; right permission bits set.
1745 (setq setmodes (file-modes buffer-file-name))
1746 ;; We succeeded in writing the temp file,
1747 ;; so rename it.
1748 (rename-file tempname buffer-file-name t))
1749 ;; If file not writable, see if we can make it writable
1750 ;; temporarily while we write it.
1751 ;; But no need to do so if we have just backed it up
1752 ;; (setmodes is set) because that says we're superseding.
1753 (cond ((and tempsetmodes (not setmodes))
1754 ;; Change the mode back, after writing.
1755 (setq setmodes (file-modes buffer-file-name))
1756 (set-file-modes buffer-file-name 511)))
1757 (write-region (point-min) (point-max)
1758 buffer-file-name nil t)))
1759 setmodes))
1760
1761 (defun save-some-buffers (&optional arg exiting)
1762 "Save some modified file-visiting buffers. Asks user about each one.
1763 Optional argument (the prefix) non-nil means save all with no questions.
1764 Optional second argument EXITING means ask about certain non-file buffers
1765 as well as about file buffers."
1766 (interactive "P")
1767 (save-window-excursion
1768 (let ((files-done
1769 (map-y-or-n-p
1770 (function
1771 (lambda (buffer)
1772 (and (buffer-modified-p buffer)
1773 (not (buffer-base-buffer buffer))
1774 (or
1775 (buffer-file-name buffer)
1776 (and exiting
1777 (progn
1778 (set-buffer buffer)
1779 (and buffer-offer-save (> (buffer-size) 0)))))
1780 (if arg
1781 t
1782 (if (buffer-file-name buffer)
1783 (format "Save file %s? "
1784 (buffer-file-name buffer))
1785 (format "Save buffer %s? "
1786 (buffer-name buffer)))))))
1787 (function
1788 (lambda (buffer)
1789 (set-buffer buffer)
1790 (save-buffer)))
1791 (buffer-list)
1792 '("buffer" "buffers" "save")
1793 (list (list ?\C-r (lambda (buf)
1794 (view-buffer buf)
1795 (setq view-exit-action
1796 '(lambda (ignore)
1797 (exit-recursive-edit)))
1798 (recursive-edit)
1799 ;; Return nil to ask about BUF again.
1800 nil)
1801 "display the current buffer"))))
1802 (abbrevs-done
1803 (and save-abbrevs abbrevs-changed
1804 (progn
1805 (if (or arg
1806 (y-or-n-p (format "Save abbrevs in %s? " abbrev-file-name)))
1807 (write-abbrev-file nil))
1808 ;; Don't keep bothering user if he says no.
1809 (setq abbrevs-changed nil)
1810 t))))
1811 (or (> files-done 0) abbrevs-done
1812 (message "(No files need saving)")))))
1813 \f
1814 (defun not-modified (&optional arg)
1815 "Mark current buffer as unmodified, not needing to be saved.
1816 With prefix arg, mark buffer as modified, so \\[save-buffer] will save.
1817
1818 It is not a good idea to use this function in Lisp programs, because it
1819 prints a message in the minibuffer. Instead, use `set-buffer-modified-p'."
1820 (interactive "P")
1821 (message (if arg "Modification-flag set"
1822 "Modification-flag cleared"))
1823 (set-buffer-modified-p arg))
1824
1825 (defun toggle-read-only (&optional arg)
1826 "Change whether this buffer is visiting its file read-only.
1827 With arg, set read-only iff arg is positive."
1828 (interactive "P")
1829 (setq buffer-read-only
1830 (if (null arg)
1831 (not buffer-read-only)
1832 (> (prefix-numeric-value arg) 0)))
1833 ;; Force mode-line redisplay
1834 (set-buffer-modified-p (buffer-modified-p)))
1835
1836 (defun insert-file (filename)
1837 "Insert contents of file FILENAME into buffer after point.
1838 Set mark after the inserted text.
1839
1840 This function is meant for the user to run interactively.
1841 Don't call it from programs! Use `insert-file-contents' instead.
1842 \(Its calling sequence is different; see its documentation)."
1843 (interactive "*fInsert file: ")
1844 (if (file-directory-p filename)
1845 (signal 'file-error (list "Opening input file" "file is a directory"
1846 filename)))
1847 (let ((tem (insert-file-contents filename)))
1848 (push-mark (+ (point) (car (cdr tem))))))
1849
1850 (defun append-to-file (start end filename)
1851 "Append the contents of the region to the end of file FILENAME.
1852 When called from a function, expects three arguments,
1853 START, END and FILENAME. START and END are buffer positions
1854 saying what text to write."
1855 (interactive "r\nFAppend to file: ")
1856 (write-region start end filename t))
1857
1858 (defun file-newest-backup (filename)
1859 "Return most recent backup file for FILENAME or nil if no backups exist."
1860 (let* ((filename (expand-file-name filename))
1861 (file (file-name-nondirectory filename))
1862 (dir (file-name-directory filename))
1863 (comp (file-name-all-completions file dir))
1864 newest)
1865 (while comp
1866 (setq file (concat dir (car comp))
1867 comp (cdr comp))
1868 (if (and (backup-file-name-p file)
1869 (or (null newest) (file-newer-than-file-p file newest)))
1870 (setq newest file)))
1871 newest))
1872
1873 (defun rename-uniquely ()
1874 "Rename current buffer to a similar name not already taken.
1875 This function is useful for creating multiple shell process buffers
1876 or multiple mail buffers, etc."
1877 (interactive)
1878 (let* ((new-buf (generate-new-buffer (buffer-name)))
1879 (name (buffer-name new-buf)))
1880 (kill-buffer new-buf)
1881 (rename-buffer name)
1882 (set-buffer-modified-p (buffer-modified-p)))) ; force mode line update
1883
1884 (defun make-directory (dir &optional parents)
1885 "Create the directory DIR and any nonexistent parent dirs.
1886 Interactively, the default choice of directory to create
1887 is the current default directory for file names.
1888 That is useful when you have visited a file in a nonexistint directory.
1889
1890 Noninteractively, the second (optional) argument PARENTS says whether
1891 to create parent directories if they don't exist."
1892 (interactive
1893 (list (read-file-name "Make directory: " default-directory default-directory
1894 nil nil)
1895 t))
1896 (let ((handler (find-file-name-handler dir 'make-directory)))
1897 (if handler
1898 (funcall handler 'make-directory dir parents)
1899 (if (not parents)
1900 (make-directory-internal dir)
1901 (let ((dir (directory-file-name (expand-file-name dir)))
1902 create-list)
1903 (while (not (file-exists-p dir))
1904 (setq create-list (cons dir create-list)
1905 dir (directory-file-name (file-name-directory dir))))
1906 (while create-list
1907 (make-directory-internal (car create-list))
1908 (setq create-list (cdr create-list))))))))
1909 \f
1910 (put 'revert-buffer-function 'permanent-local t)
1911 (defvar revert-buffer-function nil
1912 "Function to use to revert this buffer, or nil to do the default.
1913 The function receives two arguments IGNORE-AUTO and NOCONFIRM,
1914 which are the arguments that `revert-buffer' received.")
1915
1916 (put 'revert-buffer-insert-file-contents-function 'permanent-local t)
1917 (defvar revert-buffer-insert-file-contents-function nil
1918 "Function to use to insert contents when reverting this buffer.
1919 Gets two args, first the nominal file name to use,
1920 and second, t if reading the auto-save file.")
1921
1922 (defun revert-buffer (&optional ignore-auto noconfirm)
1923 "Replace the buffer text with the text of the visited file on disk.
1924 This undoes all changes since the file was visited or saved.
1925 With a prefix argument, offer to revert from latest auto-save file, if
1926 that is more recent than the visited file.
1927
1928 When called from Lisp, the first argument is IGNORE-AUTO; only offer
1929 to revert from the auto-save file when this is nil. Note that the
1930 sense of this argument is the reverse of the prefix argument, for the
1931 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
1932 to nil.
1933
1934 Optional second argument NOCONFIRM means don't ask for confirmation at
1935 all.
1936
1937 If the value of `revert-buffer-function' is non-nil, it is called to
1938 do the work.
1939
1940 The default revert function runs the hook `before-revert-hook' at the
1941 beginning and `after-revert-hook' at the end."
1942 ;; I admit it's odd to reverse the sense of the prefix argument, but
1943 ;; there is a lot of code out there which assumes that the first
1944 ;; argument should be t to avoid consulting the auto-save file, and
1945 ;; there's no straightforward way to encourage authors to notice a
1946 ;; reversal of the argument sense. So I'm just changing the user
1947 ;; interface, but leaving the programmatic interface the same.
1948 (interactive (list (not current-prefix-arg)))
1949 (if revert-buffer-function
1950 (funcall revert-buffer-function ignore-auto noconfirm)
1951 (let* ((opoint (point))
1952 (auto-save-p (and (not ignore-auto)
1953 (recent-auto-save-p)
1954 buffer-auto-save-file-name
1955 (file-readable-p buffer-auto-save-file-name)
1956 (y-or-n-p
1957 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
1958 (file-name (if auto-save-p
1959 buffer-auto-save-file-name
1960 buffer-file-name)))
1961 (cond ((null file-name)
1962 (error "Buffer does not seem to be associated with any file"))
1963 ((or noconfirm
1964 (yes-or-no-p (format "Revert buffer from file %s? "
1965 file-name)))
1966 (run-hooks 'before-revert-hook)
1967 ;; If file was backed up but has changed since,
1968 ;; we shd make another backup.
1969 (and (not auto-save-p)
1970 (not (verify-visited-file-modtime (current-buffer)))
1971 (setq buffer-backed-up nil))
1972 ;; Get rid of all undo records for this buffer.
1973 (or (eq buffer-undo-list t)
1974 (setq buffer-undo-list nil))
1975 (let ((buffer-read-only nil)
1976 ;; Don't make undo records for the reversion.
1977 (buffer-undo-list t))
1978 (if revert-buffer-insert-file-contents-function
1979 (funcall revert-buffer-insert-file-contents-function
1980 file-name auto-save-p)
1981 (if (not (file-exists-p file-name))
1982 (error "File %s no longer exists!" file-name))
1983 ;; Bind buffer-file-name to nil
1984 ;; so that we don't try to lock the file.
1985 (let ((buffer-file-name nil))
1986 (or auto-save-p
1987 (unlock-buffer)))
1988 (widen)
1989 (insert-file-contents file-name (not auto-save-p)
1990 nil nil t)))
1991 (goto-char (min opoint (point-max)))
1992 ;; Recompute the truename in case changes in symlinks
1993 ;; have changed the truename.
1994 (setq buffer-file-truename
1995 (abbreviate-file-name (file-truename buffer-file-name)))
1996 (after-find-file nil nil t t)
1997 (run-hooks 'after-revert-hook)
1998 t)))))
1999
2000 (defun recover-file (file)
2001 "Visit file FILE, but get contents from its last auto-save file."
2002 ;; Actually putting the file name in the minibuffer should be used
2003 ;; only rarely.
2004 ;; Not just because users often use the default.
2005 (interactive "fRecover file: ")
2006 (setq file (expand-file-name file))
2007 (if (auto-save-file-name-p (file-name-nondirectory file))
2008 (error "%s is an auto-save file" file))
2009 (let ((file-name (let ((buffer-file-name file))
2010 (make-auto-save-file-name))))
2011 (cond ((not (file-newer-than-file-p file-name file))
2012 (error "Auto-save file %s not current" file-name))
2013 ((save-window-excursion
2014 (if (not (eq system-type 'vax-vms))
2015 (with-output-to-temp-buffer "*Directory*"
2016 (buffer-disable-undo standard-output)
2017 (call-process "ls" nil standard-output nil
2018 (if (file-symlink-p file) "-lL" "-l")
2019 file file-name)))
2020 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
2021 (switch-to-buffer (find-file-noselect file t))
2022 (let ((buffer-read-only nil))
2023 (erase-buffer)
2024 (insert-file-contents file-name nil))
2025 (after-find-file nil nil t))
2026 (t (error "Recover-file cancelled.")))))
2027
2028 (defun kill-some-buffers ()
2029 "For each buffer, ask whether to kill it."
2030 (interactive)
2031 (let ((list (buffer-list)))
2032 (while list
2033 (let* ((buffer (car list))
2034 (name (buffer-name buffer)))
2035 (and (not (string-equal name ""))
2036 (/= (aref name 0) ? )
2037 (yes-or-no-p
2038 (format "Buffer %s %s. Kill? "
2039 name
2040 (if (buffer-modified-p buffer)
2041 "HAS BEEN EDITED" "is unmodified")))
2042 (kill-buffer buffer)))
2043 (setq list (cdr list)))))
2044 \f
2045 (defun auto-save-mode (arg)
2046 "Toggle auto-saving of contents of current buffer.
2047 With prefix argument ARG, turn auto-saving on if positive, else off."
2048 (interactive "P")
2049 (setq buffer-auto-save-file-name
2050 (and (if (null arg)
2051 (or (not buffer-auto-save-file-name)
2052 ;; If autosave is off because buffer has shrunk,
2053 ;; then toggling should turn it on.
2054 (< buffer-saved-size 0))
2055 (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))
2056 (if (and buffer-file-name auto-save-visited-file-name
2057 (not buffer-read-only))
2058 buffer-file-name
2059 (make-auto-save-file-name))))
2060 ;; If -1 was stored here, to temporarily turn off saving,
2061 ;; turn it back on.
2062 (and (< buffer-saved-size 0)
2063 (setq buffer-saved-size 0))
2064 (if (interactive-p)
2065 (message "Auto-save %s (in this buffer)"
2066 (if buffer-auto-save-file-name "on" "off")))
2067 buffer-auto-save-file-name)
2068
2069 (defun rename-auto-save-file ()
2070 "Adjust current buffer's auto save file name for current conditions.
2071 Also rename any existing auto save file, if it was made in this session."
2072 (let ((osave buffer-auto-save-file-name))
2073 (setq buffer-auto-save-file-name
2074 (make-auto-save-file-name))
2075 (if (and osave buffer-auto-save-file-name
2076 (not (string= buffer-auto-save-file-name buffer-file-name))
2077 (not (string= buffer-auto-save-file-name osave))
2078 (file-exists-p osave)
2079 (recent-auto-save-p))
2080 (rename-file osave buffer-auto-save-file-name t))))
2081
2082 (defun make-auto-save-file-name ()
2083 "Return file name to use for auto-saves of current buffer.
2084 Does not consider `auto-save-visited-file-name' as that variable is checked
2085 before calling this function. You can redefine this for customization.
2086 See also `auto-save-file-name-p'."
2087 (if buffer-file-name
2088 (concat (file-name-directory buffer-file-name)
2089 "#"
2090 (file-name-nondirectory buffer-file-name)
2091 "#")
2092
2093 ;; Deal with buffers that don't have any associated files. (Mail
2094 ;; mode tends to create a good number of these.)
2095
2096 (let ((buffer-name (buffer-name))
2097 (limit 0))
2098 ;; Use technique from Sebastian Kremer's auto-save
2099 ;; package to turn slashes into \\!. This ensures that
2100 ;; the auto-save buffer name is unique.
2101
2102 (while (string-match "[/\\]" buffer-name limit)
2103 (setq buffer-name (concat (substring buffer-name 0 (match-beginning 0))
2104 (if (string= (substring buffer-name
2105 (match-beginning 0)
2106 (match-end 0))
2107 "/")
2108 "\\!"
2109 "\\\\")
2110 (substring buffer-name (match-end 0))))
2111 (setq limit (1+ (match-end 0))))
2112
2113 (expand-file-name (format "#%s#%s#" buffer-name (make-temp-name ""))))))
2114
2115 (defun auto-save-file-name-p (filename)
2116 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
2117 FILENAME should lack slashes. You can redefine this for customization."
2118 (string-match "^#.*#$" filename))
2119 \f
2120 (defconst list-directory-brief-switches
2121 (if (eq system-type 'vax-vms) "" "-CF")
2122 "*Switches for list-directory to pass to `ls' for brief listing,")
2123
2124 (defconst list-directory-verbose-switches
2125 (if (eq system-type 'vax-vms)
2126 "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
2127 "-l")
2128 "*Switches for list-directory to pass to `ls' for verbose listing,")
2129
2130 (defun list-directory (dirname &optional verbose)
2131 "Display a list of files in or matching DIRNAME, a la `ls'.
2132 DIRNAME is globbed by the shell if necessary.
2133 Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
2134 Actions controlled by variables `list-directory-brief-switches'
2135 and `list-directory-verbose-switches'."
2136 (interactive (let ((pfx current-prefix-arg))
2137 (list (read-file-name (if pfx "List directory (verbose): "
2138 "List directory (brief): ")
2139 nil default-directory nil)
2140 pfx)))
2141 (let ((switches (if verbose list-directory-verbose-switches
2142 list-directory-brief-switches)))
2143 (or dirname (setq dirname default-directory))
2144 (setq dirname (expand-file-name dirname))
2145 (with-output-to-temp-buffer "*Directory*"
2146 (buffer-disable-undo standard-output)
2147 (princ "Directory ")
2148 (princ dirname)
2149 (terpri)
2150 (save-excursion
2151 (set-buffer "*Directory*")
2152 (let ((wildcard (not (file-directory-p dirname))))
2153 (insert-directory dirname switches wildcard (not wildcard)))))))
2154
2155 (defvar insert-directory-program "ls"
2156 "Absolute or relative name of the `ls' program used by `insert-directory'.")
2157
2158 ;; insert-directory
2159 ;; - must insert _exactly_one_line_ describing FILE if WILDCARD and
2160 ;; FULL-DIRECTORY-P is nil.
2161 ;; The single line of output must display FILE's name as it was
2162 ;; given, namely, an absolute path name.
2163 ;; - must insert exactly one line for each file if WILDCARD or
2164 ;; FULL-DIRECTORY-P is t, plus one optional "total" line
2165 ;; before the file lines, plus optional text after the file lines.
2166 ;; Lines are delimited by "\n", so filenames containing "\n" are not
2167 ;; allowed.
2168 ;; File lines should display the basename.
2169 ;; - must be consistent with
2170 ;; - functions dired-move-to-filename, (these two define what a file line is)
2171 ;; dired-move-to-end-of-filename,
2172 ;; dired-between-files, (shortcut for (not (dired-move-to-filename)))
2173 ;; dired-insert-headerline
2174 ;; dired-after-subdir-garbage (defines what a "total" line is)
2175 ;; - variable dired-subdir-regexp
2176 (defun insert-directory (file switches &optional wildcard full-directory-p)
2177 "Insert directory listing for FILE, formatted according to SWITCHES.
2178 Leaves point after the inserted text.
2179 SWITCHES may be a string of options, or a list of strings.
2180 Optional third arg WILDCARD means treat FILE as shell wildcard.
2181 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
2182 switches do not contain `d', so that a full listing is expected.
2183
2184 This works by running a directory listing program
2185 whose name is in the variable `insert-directory-program'.
2186 If WILDCARD, it also runs the shell specified by `shell-file-name'."
2187 ;; We need the directory in order to find the right handler.
2188 (let ((handler (find-file-name-handler (expand-file-name file)
2189 'insert-directory)))
2190 (if handler
2191 (funcall handler 'insert-directory file switches
2192 wildcard full-directory-p)
2193 (if (eq system-type 'vax-vms)
2194 (vms-read-directory file switches (current-buffer))
2195 (if wildcard
2196 ;; Run ls in the directory of the file pattern we asked for.
2197 (let ((default-directory
2198 (if (file-name-absolute-p file)
2199 (file-name-directory file)
2200 (file-name-directory (expand-file-name file))))
2201 (pattern (file-name-nondirectory file))
2202 (beg 0))
2203 ;; Quote some characters that have special meanings in shells;
2204 ;; but don't quote the wildcards--we want them to be special.
2205 ;; We also currently don't quote the quoting characters
2206 ;; in case people want to use them explicitly to quote
2207 ;; wildcard characters.
2208 (while (string-match "[ \t\n;<>&|()#$]" pattern beg)
2209 (setq pattern
2210 (concat (substring pattern 0 (match-beginning 0))
2211 "\\"
2212 (substring pattern (match-beginning 0)))
2213 beg (1+ (match-end 0))))
2214 (call-process shell-file-name nil t nil
2215 "-c" (concat "\\" ;; Disregard shell aliases!
2216 insert-directory-program
2217 " -d "
2218 (if (stringp switches)
2219 switches
2220 (mapconcat 'identity switches " "))
2221 " "
2222 pattern)))
2223 ;; SunOS 4.1.3, SVr4 and others need the "." to list the
2224 ;; directory if FILE is a symbolic link.
2225 (apply 'call-process
2226 insert-directory-program nil t nil
2227 (let (list)
2228 (if (listp switches)
2229 (setq list switches)
2230 (if (not (equal switches ""))
2231 (progn
2232 ;; Split the switches at any spaces
2233 ;; so we can pass separate options as separate args.
2234 (while (string-match " " switches)
2235 (setq list (cons (substring switches 0 (match-beginning 0))
2236 list)
2237 switches (substring switches (match-end 0))))
2238 (setq list (cons switches list)))))
2239 (append list
2240 (list
2241 (if full-directory-p
2242 (concat (file-name-as-directory file) ".")
2243 file))))))))))
2244
2245 (defvar kill-emacs-query-functions nil
2246 "Functions to call with no arguments to query about killing Emacs.
2247 If any of these functions returns nil, killing Emacs is cancelled.
2248 `save-buffers-kill-emacs' (\\[save-buffers-kill-emacs]) calls these functions,
2249 but `kill-emacs', the low level primitive, does not.
2250 See also `kill-emacs-hook'.")
2251
2252 (defun save-buffers-kill-emacs (&optional arg)
2253 "Offer to save each buffer, then kill this Emacs process.
2254 With prefix arg, silently save all file-visiting buffers, then kill."
2255 (interactive "P")
2256 (save-some-buffers arg t)
2257 (and (or (not (memq t (mapcar (function
2258 (lambda (buf) (and (buffer-file-name buf)
2259 (buffer-modified-p buf))))
2260 (buffer-list))))
2261 (yes-or-no-p "Modified buffers exist; exit anyway? "))
2262 (or (not (fboundp 'process-list))
2263 ;; process-list is not defined on VMS.
2264 (let ((processes (process-list))
2265 active)
2266 (while processes
2267 (and (memq (process-status (car processes)) '(run stop open))
2268 (let ((val (process-kill-without-query (car processes))))
2269 (process-kill-without-query (car processes) val)
2270 val)
2271 (setq active t))
2272 (setq processes (cdr processes)))
2273 (or (not active)
2274 (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
2275 ;; Query the user for other things, perhaps.
2276 (run-hook-with-args-until-failure 'kill-emacs-query-functions)
2277 (kill-emacs)))
2278 \f
2279 (define-key ctl-x-map "\C-f" 'find-file)
2280 (define-key ctl-x-map "\C-q" 'toggle-read-only)
2281 (define-key ctl-x-map "\C-r" 'find-file-read-only)
2282 (define-key ctl-x-map "\C-v" 'find-alternate-file)
2283 (define-key ctl-x-map "\C-s" 'save-buffer)
2284 (define-key ctl-x-map "s" 'save-some-buffers)
2285 (define-key ctl-x-map "\C-w" 'write-file)
2286 (define-key ctl-x-map "i" 'insert-file)
2287 (define-key esc-map "~" 'not-modified)
2288 (define-key ctl-x-map "\C-d" 'list-directory)
2289 (define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs)
2290
2291 (define-key ctl-x-4-map "f" 'find-file-other-window)
2292 (define-key ctl-x-4-map "r" 'find-file-read-only-other-window)
2293 (define-key ctl-x-4-map "\C-f" 'find-file-other-window)
2294 (define-key ctl-x-4-map "b" 'switch-to-buffer-other-window)
2295 (define-key ctl-x-4-map "\C-o" 'display-buffer)
2296
2297 (define-key ctl-x-5-map "b" 'switch-to-buffer-other-frame)
2298 (define-key ctl-x-5-map "f" 'find-file-other-frame)
2299 (define-key ctl-x-5-map "\C-f" 'find-file-other-frame)
2300 (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame)
2301
2302 ;;; files.el ends here