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