]> code.delx.au - gnu-emacs/blobdiff - lisp/files.el
(idl-mode-hook): New variable.
[gnu-emacs] / lisp / files.el
index f4876110d1d474f333bc6f913d72ddde779cb4d2..5fcaef03406062b7fd419d9db9324d05f4a4907e 100644 (file)
 
 ;;; Code:
 
-(defvar delete-auto-save-files t
-  "*Non-nil means delete auto-save file when a buffer is saved or killed.")
+(defgroup backup nil
+  "Backups of edited data files."
+  :group 'files)
 
-(defvar directory-abbrev-alist
+(defgroup find-file nil
+  "Finding files."
+  :group 'files)
+
+
+(defcustom delete-auto-save-files t
+  "*Non-nil means delete auto-save file when a buffer is saved or killed."
+  :type 'boolean
+  :group 'auto-save)
+
+(defcustom directory-abbrev-alist
   nil
   "*Alist of abbreviations for file directories.
 A list of elements of the form (FROM . TO), each meaning to replace
@@ -46,10 +57,16 @@ They should be ordinary absolute directory names.
 
 Use this feature when you have directories which you normally refer to
 via absolute symbolic links.  Make TO the name of the link, and FROM
-the name it is linked to.")
+the name it is linked to."
+  :type '(repeat (cons :format "%v"
+                      :value ("" . "")
+                      (regexp :tag "From")
+                      (regexp :tag "To")))
+  :group 'abbrev
+  :group 'find-file)
 
 ;;; Turn off backup files on VMS since it has version numbers.
-(defvar make-backup-files (not (eq system-type 'vax-vms))
+(defcustom make-backup-files (not (eq system-type 'vax-vms))
   "*Non-nil means make a backup of a file the first time it is saved.
 This can be done by renaming the file or by copying.
 
@@ -65,7 +82,9 @@ The file's owner and group are unchanged.
 
 The choice of renaming or copying is controlled by the variables
 `backup-by-copying', `backup-by-copying-when-linked' and
-`backup-by-copying-when-mismatch'.  See also `backup-inhibited'.")
+`backup-by-copying-when-mismatch'.  See also `backup-inhibited'."
+  :type 'boolean
+  :group 'backup)
 
 ;; Do this so that local variables based on the file name
 ;; are not overridden by the major mode.
@@ -75,22 +94,28 @@ This variable is intended for use by making it local to a buffer.
 But it is local only if you make it local.")
 (put 'backup-inhibited 'permanent-local t)
 
-(defvar backup-by-copying nil
+(defcustom backup-by-copying nil
  "*Non-nil means always use copying to create backup files.
-See documentation of variable `make-backup-files'.")
+See documentation of variable `make-backup-files'."
+ :type 'boolean
+ :group 'backup)
 
-(defvar backup-by-copying-when-linked nil
+(defcustom backup-by-copying-when-linked nil
  "*Non-nil means use copying to create backups for files with multiple names.
 This causes the alternate names to refer to the latest version as edited.
-This variable is relevant only if `backup-by-copying' is nil.")
+This variable is relevant only if `backup-by-copying' is nil."
+ :type 'boolean
+ :group 'backup)
 
-(defvar backup-by-copying-when-mismatch nil
+(defcustom backup-by-copying-when-mismatch nil
   "*Non-nil means create backups by copying if this preserves owner or group.
 Renaming may still be used (subject to control of other variables)
 when it would not result in changing the owner or group of the file;
 that is, for files which are owned by you and whose group matches
 the default for a new file created there by you.
-This variable is relevant only if `backup-by-copying' is nil.")
+This variable is relevant only if `backup-by-copying' is nil."
+  :type 'boolean
+  :group 'backup)
 
 (defvar backup-enable-predicate
   '(lambda (name)
@@ -99,29 +124,37 @@ This variable is relevant only if `backup-by-copying' is nil.")
   "Predicate that looks at a file name and decides whether to make backups.
 Called with an absolute file name as argument, it returns t to enable backup.")
 
-(defvar buffer-offer-save nil
+(defcustom buffer-offer-save nil
   "*Non-nil in a buffer means offer to save the buffer on exit
 even if the buffer is not visiting a file.
-Automatically local in all buffers.")
+Automatically local in all buffers."
+  :type 'boolean
+  :group 'backup)
 (make-variable-buffer-local 'buffer-offer-save)
 
-(defvar find-file-existing-other-name t
+(defcustom find-file-existing-other-name t
   "*Non-nil means find a file under alternative names, in existing buffers.
 This means if any existing buffer is visiting the file you want
-under another name, you get the existing buffer instead of a new buffer.")
+under another name, you get the existing buffer instead of a new buffer."
+  :type 'boolean
+  :group 'find-file)
 
-(defvar find-file-visit-truename nil
+(defcustom find-file-visit-truename nil
   "*Non-nil means visit a file under its truename.
 The truename of a file is found by chasing all links
-both at the file level and at the levels of the containing directories.")
+both at the file level and at the levels of the containing directories."
+  :type 'boolean
+  :group 'find-file)
 
-(defvar find-file-revert-without-query
+(defcustom revert-without-query
   nil
   "*Specify which files should be reverted without query.
 The value is a list of regular expressions.
 If the file name matches one of these regular expressions,
-then `find-file' reverts the file without querying
-if the file has changed on disk and you have not edited the buffer.")
+then `revert-buffer' reverts the file without querying
+if the file has changed on disk and you have not edited the buffer."
+  :type 'boolean
+  :group 'find-file)
 
 (defvar buffer-file-number nil
   "The device number and file number of the file visited in the current buffer.
@@ -134,7 +167,7 @@ If the buffer is visiting a new file, the value is nil.")
 (defvar buffer-file-numbers-unique (not (memq system-type '(windows-nt)))
   "Non-nil means that buffer-file-number uniquely identifies files.")
 
-(defvar file-precious-flag nil
+(defcustom file-precious-flag nil
   "*Non-nil means protect against I/O errors while saving files.
 Some modes set this non-nil in particular buffers.
 
@@ -145,46 +178,74 @@ and there is never any instant where the file is nonexistent.
 
 Note that this feature forces backups to be made by copying.
 Yet, at the same time, saving a precious file
-breaks any hard links between it and other files.")
+breaks any hard links between it and other files."
+  :type 'boolean
+  :group 'backup)
 
-(defvar version-control nil
+(defcustom version-control nil
   "*Control use of version numbers for backup files.
 t means make numeric backup versions unconditionally.
 nil means make them for files that have some already.
-`never' means do not make them.")
-
-(defvar dired-kept-versions 2
-  "*When cleaning directory, number of versions to keep.")
-
-(defvar delete-old-versions nil
+`never' means do not make them."
+  :type 'boolean
+  :group 'backup
+  :group 'vc)
+
+(defcustom dired-kept-versions 2
+  "*When cleaning directory, number of versions to keep."
+  :type 'integer
+  :group 'backup
+  :group 'dired)
+
+(defcustom delete-old-versions nil
   "*If t, delete excess backup versions silently.
-If nil, ask confirmation.  Any other value prevents any trimming.")
-
-(defvar kept-old-versions 2
-  "*Number of oldest versions to keep when a new numbered backup is made.")
-
-(defvar kept-new-versions 2
+If nil, ask confirmation.  Any other value prevents any trimming."
+  :type '(choice (const :tag "Delete" t)
+                (const :tag "Ask" nil)
+                (sexp :tag "Leave" :format "%t\n" other))
+  :group 'backup)
+
+(defcustom kept-old-versions 2
+  "*Number of oldest versions to keep when a new numbered backup is made."
+  :type 'integer
+  :group 'backup)
+
+(defcustom kept-new-versions 2
   "*Number of newest versions to keep when a new numbered backup is made.
-Includes the new backup.  Must be > 0")
+Includes the new backup.  Must be > 0"
+  :type 'integer
+  :group 'backup)
 
-(defvar require-final-newline nil
+(defcustom require-final-newline nil
   "*Value of t says silently ensure a file ends in a newline when it is saved.
 Non-nil but not t says ask user whether to add a newline when there isn't one.
-nil means don't add newlines.")
-
-(defvar auto-save-default t
-  "*Non-nil says by default do auto-saving of every file-visiting buffer.")
-
-(defvar auto-save-visited-file-name nil
+nil means don't add newlines."
+  :type '(choice (const :tag "Off" nil)
+                (const :tag "Add" t)
+                (sexp :tag "Ask" :format "%t\n" ask))
+  :group 'editing-basics)
+
+(defcustom auto-save-default t
+  "*Non-nil says by default do auto-saving of every file-visiting buffer."
+  :type 'boolean
+  :group 'auto-save)
+
+(defcustom auto-save-visited-file-name nil
   "*Non-nil says auto-save a buffer in the file it is visiting, when practical.
-Normally auto-save files are written under other names.")
+Normally auto-save files are written under other names."
+  :type 'boolean
+  :group 'auto-save)
 
-(defvar save-abbrevs nil
+(defcustom save-abbrevs nil
   "*Non-nil means save word abbrevs too when files are saved.
-Loading an abbrev file sets this to t.")
+Loading an abbrev file sets this to t."
+  :type 'boolean
+  :group 'abbrev)
 
-(defvar find-file-run-dired t
-  "*Non-nil says run dired if `find-file' is given the name of a directory.")
+(defcustom find-file-run-dired t
+  "*Non-nil says run dired if `find-file' is given the name of a directory."
+  :type 'boolean
+  :group 'find-file)
 
 ;;;It is not useful to make this a local variable.
 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
@@ -242,23 +303,31 @@ LOCAL argument.
 See also `write-file-hooks'.")
 (make-variable-buffer-local 'write-contents-hooks)
 
-(defvar enable-local-variables t
+(defcustom enable-local-variables t
   "*Control use of local variables in files you visit.
 The value can be t, nil or something else.
 A value of t means file local variables specifications are obeyed;
 nil means they are ignored; anything else means query.
 
 The command \\[normal-mode] always obeys file local variable
-specifications and ignores this variable.")
+specifications and ignores this variable."
+  :type '(choice (const :tag "Obey" t)
+                (const :tag "Ignore" nil)
+                (sexp :tag "Query" :format "%t\n" other))
+  :group 'find-file)
 
-(defvar enable-local-eval 'maybe
+(defcustom enable-local-eval 'maybe
   "*Control processing of the \"variable\" `eval' in a file's local variables.
 The value can be t, nil or something else.
 A value of t means obey `eval' variables;
 nil means ignore them; anything else means query.
 
 The command \\[normal-mode] always obeys local-variables lists
-and ignores this variable.")
+and ignores this variable."
+  :type '(choice (const :tag "Obey" t)
+                (const :tag "Ignore" nil)
+                (sexp :tag "Query" :format "%t\n" other))
+  :group 'find-file)
 
 ;; Avoid losing in versions where CLASH_DETECTION is disabled.
 (or (fboundp 'lock-buffer)
@@ -268,6 +337,9 @@ and ignores this variable.")
 (or (fboundp 'file-locked-p)
     (defalias 'file-locked-p 'ignore))
 
+(defvar view-read-only nil
+  "*Non-nil means buffers visiting files read-only, do it in view mode.")
+
 ;; This hook function provides support for ange-ftp host name
 ;; completion.  It runs the usual ange-ftp hook, but only for
 ;; completion operations.  Having this here avoids the need
@@ -299,10 +371,10 @@ with a definition that really does change some file names."
 Not actually set up until the first time you you use it.")
 
 (defvar path-separator ":"
-  "Character used to separate concatenated paths.")
+  "Character used to separate directories in search paths.")
 
 (defun parse-colon-path (cd-path)
-  "Explode a colon-separated list of paths into a string list."
+  "Explode a colon-separated search path into a list of directory names."
   (and cd-path
        (let (cd-prefix cd-list (cd-start 0) cd-colon)
         (setq cd-path (concat cd-path path-separator))
@@ -325,7 +397,9 @@ Not actually set up until the first time you you use it.")
       (setq dir (file-name-as-directory dir)))
   (setq dir (abbreviate-file-name (expand-file-name dir)))
   (if (not (file-directory-p dir))
-      (error "%s is not a directory" dir)
+      (if (file-exists-p dir)
+         (error "%s is not a directory" dir)
+       (error "%s: no such directory" dir))
     (if (file-executable-p dir)
        (setq default-directory dir)
       (error "Cannot cd to %s:  Permission denied" dir))))
@@ -465,28 +539,27 @@ Does not examine containing directories for links,
 unlike `file-truename'."
   (let (tem (count 100) (newname filename))
     (while (setq tem (file-symlink-p newname))
-      (if (= count 0)
-         (error "Apparent cycle of symbolic links for %s" filename))
-      ;; In the context of a link, `//' doesn't mean what Emacs thinks.
-      (while (string-match "//+" tem)
-       (setq tem (concat (substring tem 0 (1+ (match-beginning 0)))
-                         (substring tem (match-end 0)))))
-      ;; Handle `..' by hand, since it needs to work in the
-      ;; target of any directory symlink.
-      ;; This code is not quite complete; it does not handle
-      ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose.
-      (while (string-match "\\`\\.\\./" tem)
-       (setq tem (substring tem 3))
-       (setq newname (file-name-as-directory
-                      ;; Do the .. by hand.
-                      (directory-file-name
-                       (file-name-directory
-                        ;; Chase links in the default dir of the symlink.
-                        (file-chase-links
-                         (directory-file-name
-                          (file-name-directory newname))))))))
-      (setq newname (expand-file-name tem (file-name-directory newname)))
-      (setq count (1- count)))
+      (save-match-data
+       (if (= count 0)
+           (error "Apparent cycle of symbolic links for %s" filename))
+       ;; In the context of a link, `//' doesn't mean what Emacs thinks.
+       (while (string-match "//+" tem)
+         (setq tem (replace-match "/" nil nil tem)))
+       ;; Handle `..' by hand, since it needs to work in the
+       ;; target of any directory symlink.
+       ;; This code is not quite complete; it does not handle
+       ;; embedded .. in some cases such as ./../foo and foo/bar/../../../lose.
+       (while (string-match "\\`\\.\\./" tem)
+         (setq tem (substring tem 3))
+         (setq newname (expand-file-name newname))
+         ;; Chase links in the default dir of the symlink.
+         (setq newname
+               (file-chase-links
+                (directory-file-name (file-name-directory newname))))
+         ;; Now find the parent of that dir.
+         (setq newname (file-name-directory newname)))
+       (setq newname (expand-file-name tem (file-name-directory newname)))
+       (setq count (1- count))))
     newname))
 \f
 (defun switch-to-buffer-other-window (buffer &optional norecord)
@@ -506,73 +579,57 @@ do not put this buffer at the front of the list of recently selected ones."
     (pop-to-buffer buffer t norecord)
     (raise-frame (window-frame (selected-window)))))
 
-(defun find-file (filename &optional coding-system)
+(defun find-file (filename)
   "Edit file FILENAME.
 Switch to a buffer visiting file FILENAME,
-creating one if none already exists.
-A prefix argument enables user to specify the coding-system interactively."
-  (interactive "FFind file: \nZCoding-system: ")
-  (if coding-system
-      (let ((coding-system-for-read coding-system))
-       (switch-to-buffer (find-file-noselect filename)))
-    (switch-to-buffer (find-file-noselect filename))))
+creating one if none already exists."
+  (interactive "FFind file: ")
+  (switch-to-buffer (find-file-noselect filename)))
 
-(defun find-file-other-window (filename &optional coding-system)
+(defun find-file-other-window (filename)
   "Edit file FILENAME, in another window.
 May create a new window, or reuse an existing one.
-A prefix argument enables user to specify the coding-system interactively.
 See the function `display-buffer'."
-  (interactive "FFind file in other window: \nZCoding-system: ")
-  (if coding-system
-      (let ((coding-system-for-read coding-system))
-       (switch-to-buffer-other-window (find-file-noselect filename)))
-    (switch-to-buffer-other-window (find-file-noselect filename))))
+  (interactive "FFind file in other window: ")
+  (switch-to-buffer-other-window (find-file-noselect filename)))
 
-(defun find-file-other-frame (filename &optional coding-system)
+(defun find-file-other-frame (filename)
   "Edit file FILENAME, in another frame.
 May create a new frame, or reuse an existing one.
-A prefix argument enables user to specify the coding-system interactively.
 See the function `display-buffer'."
-  (interactive "FFind file in other frame: \nZCoding-system: ")
-  (if coding-system
-      (let ((coding-system-for-read coding-system))
-       (switch-to-buffer-other-frame (find-file-noselect filename)))
-    (switch-to-buffer-other-frame (find-file-noselect filename))))
+  (interactive "FFind file in other frame: ")
+  (switch-to-buffer-other-frame (find-file-noselect filename)))
 
-(defun find-file-read-only (filename &optional coding-system)
+(defun find-file-read-only (filename)
   "Edit file FILENAME but don't allow changes.
 Like \\[find-file] but marks buffer as read-only.
-A prefix argument enables user to specify the coding-system interactively.
 Use \\[toggle-read-only] to permit editing."
-  (interactive "fFind file read-only: \nZCoding-system: ")
-  (find-file filename coding-system)
-  (setq buffer-read-only t)
+  (interactive "fFind file read-only: ")
+  (find-file filename)
+  (toggle-read-only 1)
   (current-buffer))
 
-(defun find-file-read-only-other-window (filename &optional coding-system)
+(defun find-file-read-only-other-window (filename)
   "Edit file FILENAME in another window but don't allow changes.
 Like \\[find-file-other-window] but marks buffer as read-only.
-A prefix argument enables user to specify the coding-system interactively.
 Use \\[toggle-read-only] to permit editing."
-  (interactive "fFind file read-only other window: \nZCoding-system: ")
-  (find-file-other-window filename coding-system)
-  (setq buffer-read-only t)
+  (interactive "fFind file read-only other window: ")
+  (find-file-other-window filename)
+  (toggle-read-only 1)
   (current-buffer))
 
-(defun find-file-read-only-other-frame (filename &optional coding-system)
+(defun find-file-read-only-other-frame (filename)
   "Edit file FILENAME in another frame but don't allow changes.
 Like \\[find-file-other-frame] but marks buffer as read-only.
-A prefix argument enables user to specify the coding-system interactively.
 Use \\[toggle-read-only] to permit editing."
-  (interactive "fFind file read-only other frame: \nZCoding-system: ")
-  (find-file-other-frame filename coding-system)
-  (setq buffer-read-only t)
+  (interactive "fFind file read-only other frame: ")
+  (find-file-other-frame filename)
+  (toggle-read-only 1)
   (current-buffer))
 
-(defun find-alternate-file-other-window (filename &optional coding-system)
+(defun find-alternate-file-other-window (filename)
   "Find file FILENAME as a replacement for the file in the next window.
-This command does not select that window.
-A prefix argument enables user to specify the coding-system interactively."
+This command does not select that window."
   (interactive
    (save-selected-window
      (other-window 1)
@@ -583,20 +640,17 @@ A prefix argument enables user to specify the coding-system interactively."
            (setq file-name (file-name-nondirectory file)
                  file-dir (file-name-directory file)))
        (list (read-file-name
-             "Find alternate file: " file-dir nil nil file-name)
-            (if current-prefix-arg
-                (read-coding-system "Coding-system: "))))))
+             "Find alternate file: " file-dir nil nil file-name)))))
   (if (one-window-p)
-      (find-file-other-window filename coding-system)
+      (find-file-other-window filename)
     (save-selected-window
       (other-window 1)
-      (find-alternate-file filename coding-system))))
+      (find-alternate-file filename))))
 
-(defun find-alternate-file (filename &optional coding-system)
+(defun find-alternate-file (filename)
   "Find file FILENAME, select its buffer, kill previous buffer.
 If the current buffer now contains an empty file that you just visited
-\(presumably by mistake), use this command to visit the file you really want.
-A prefix argument enables user to specify the coding-system interactively."
+\(presumably by mistake), use this command to visit the file you really want."
   (interactive
    (let ((file buffer-file-name)
         (file-name nil)
@@ -605,9 +659,7 @@ A prefix argument enables user to specify the coding-system interactively."
          (setq file-name (file-name-nondirectory file)
                file-dir (file-name-directory file)))
      (list (read-file-name
-           "Find alternate file: " file-dir nil nil file-name)
-          (if current-prefix-arg
-              (read-coding-system "Coding-system: ")))))
+           "Find alternate file: " file-dir nil nil file-name))))
   (and (buffer-modified-p) (buffer-file-name)
        ;; (not buffer-read-only)
        (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
@@ -627,7 +679,7 @@ A prefix argument enables user to specify the coding-system interactively."
          (setq buffer-file-name nil)
          (setq buffer-file-number nil)
          (setq buffer-file-truename nil)
-         (find-file filename coding-system))
+         (find-file filename))
       (cond ((eq obuf (current-buffer))
             (setq buffer-file-name ofile)
             (setq buffer-file-number onum)
@@ -704,12 +756,14 @@ Type \\[describe-variable] directory-abbrev-alist RET for more information."
                      (substring filename (match-end 0)))))
     filename))
 
-(defvar find-file-not-true-dirname-list nil
+(defcustom find-file-not-true-dirname-list nil
   "*List of logical names for which visiting shouldn't save the true dirname.
 On VMS, when you visit a file using a logical name that searches a path,
 you may or may not want the visited file name to record the specific
 directory where the file was found.  If you *do not* want that, add the logical
-name to this list as a string.")
+name to this list as a string."
+  :type '(repeat (string :tag "Name"))
+  :group 'find-file)
 
 (defun find-buffer-visiting (filename)
   "Return the buffer visiting file FILENAME (a string).
@@ -747,17 +801,17 @@ If there is no such live buffer, return nil."
          found))))
 
 (defun insert-file-contents-literally (filename &optional visit beg end replace)
-  "Like `insert-file-contents', q.v., but only reads in the file.
-A buffer may be modified in several ways after reading into the buffer due
-to advanced Emacs features, such as file-name-handlers, format decoding,
-find-file-hooks, etc.
-  This function ensures that none of these modifications will take place.
-
-This function does not work for remote files, because it turns off
-file name handlers and remote file access uses a file name handler."
-  (let ((file-name-handler-alist nil)
-       (format-alist nil)
+  "Like `insert-file-contents', but only reads in the file literally.
+A buffer may be modified in several ways after reading into the buffer,
+to Emacs features such as format decoding, character code
+conversion, find-file-hooks, automatic uncompression, etc.
+
+This function ensures that none of these modifications will take place."
+  (let ((format-alist nil)
        (after-insert-file-functions nil)
+       (coding-system-for-read 'no-conversion)
+       (coding-system-for-write 'no-conversion)
+       (jka-compr-compression-info-list nil)
        (find-buffer-file-type-function
         (if (fboundp 'find-buffer-file-type)
             (symbol-function 'find-buffer-file-type)
@@ -776,7 +830,7 @@ If a buffer exists visiting FILENAME, return that one, but
 verify that the file has not changed since visited or saved.
 The buffer is not selected, just returned to the caller.
 Optional first arg NOWARN non-nil means suppress any warning messages.
-Optional second arg RAWFILE non-nil means the file is read literally"
+Optional second arg RAWFILE non-nil means the file is read literally."
   (setq filename
        (abbreviate-file-name
         (expand-file-name filename)))
@@ -810,7 +864,7 @@ Optional second arg RAWFILE non-nil means the file is read literally"
                    ;; Certain files should be reverted automatically
                    ;; if they have changed on disk and not in the buffer.
                    ((and (not (buffer-modified-p buf))
-                         (let ((tail find-file-revert-without-query)
+                         (let ((tail revert-without-query)
                                (found nil))
                            (while tail
                              (if (string-match (car tail) filename)
@@ -891,6 +945,18 @@ Optional second arg RAWFILE non-nil means the file is read literally"
            (after-find-file error (not nowarn))
            (setq buf (current-buffer)))))
       buf)))
+
+(defun find-file-literally (filename) 
+  "Visit file FILENAME with no conversion of any kind.
+Format conversion and character code conversion are both disabled,
+and multibyte characters are disabled in the resulting buffer.
+The major mode used is Fundamental mode regardless of the file name,
+and local variable specifications in the file are ignored.
+Automatic uncompression is also disabled."
+  (interactive "FFind file literally: ")
+  (prog1
+      (switch-to-buffer (find-file-noselect filename nil t))
+    (setq enable-multibyte-characters nil)))
 \f
 (defvar after-find-file-from-revert-buffer nil)
 
@@ -945,6 +1011,9 @@ unless NOMODES is non-nil."
   (if nomodes
       nil
     (normal-mode t)
+    (if (and buffer-read-only view-read-only
+            (not (eq (get major-mode 'mode-class) 'special)))
+       (view-mode-enter))
     (run-hooks 'find-file-hooks)))
 
 (defun normal-mode (&optional find-file)
@@ -989,11 +1058,10 @@ run `normal-mode' explicitly."
     ("\\.for\\'" . fortran-mode)
     ("\\.p\\'" . pascal-mode)
     ("\\.pas\\'" . pascal-mode)
-    ("\\.mss\\'" . scribe-mode)
     ("\\.ad[abs]\\'" . ada-mode)
-    ("\\.icn\\'" . icon-mode)
     ("\\.pl\\'" . perl-mode)
     ("\\.pm\\'" . perl-mode)
+    ("\\.s?html?\\'" . html-mode)
     ("\\.cc\\'" . c++-mode)
     ("\\.hh\\'" . c++-mode)
     ("\\.hpp\\'" . c++-mode)
@@ -1006,7 +1074,6 @@ run `normal-mode' explicitly."
     ("\\.h\\+\\+\\'" . c++-mode)
     ("\\.m\\'" . objc-mode)
     ("\\.java\\'" . java-mode)
-    ("\\.sim\\'" . simula-mode)
     ("\\.mk\\'" . makefile-mode)
     ("\\(M\\|m\\|GNUm\\)akefile\\(.in\\)?\\'" . makefile-mode)
 ;;; Less common extensions come here
@@ -1036,14 +1103,20 @@ run `normal-mode' explicitly."
     ("\\.TeX\\'" . tex-mode)
     ("\\.sty\\'" . latex-mode)
     ("\\.cls\\'" . latex-mode)         ;LaTeX 2e class
+    ("\\.clo\\'" . latex-mode)         ;LaTeX 2e class option
     ("\\.bbl\\'" . latex-mode)
     ("\\.bib\\'" . bibtex-mode)
+    ("\\.mf\\'" . metafont-mode)
+    ("\\.mp\\'" . metapost-mode)
     ("\\.article\\'" . text-mode)
     ("\\.letter\\'" . text-mode)
     ("\\.tcl\\'" . tcl-mode)
     ("\\.exp\\'" . tcl-mode)
     ("\\.itcl\\'" . tcl-mode)
     ("\\.itk\\'" . tcl-mode)
+    ("\\.icn\\'" . icon-mode)
+    ("\\.sim\\'" . simula-mode)
+    ("\\.mss\\'" . scribe-mode)
     ("\\.f90\\'" . f90-mode)
     ("\\.lsp\\'" . lisp-mode)
     ("\\.awk\\'" . awk-mode)
@@ -1064,10 +1137,9 @@ run `normal-mode' explicitly."
     ("\\.sgml?\\'" . sgml-mode)
     ("\\.dtd\\'" . sgml-mode)
     ("\\.ds\\(ss\\)?l\\'" . dsssl-mode)
-    ("\\.s?html?\\'" . html-mode)
     ;; .emacs following a directory delimiter
-    ;; in either Unix or VMS syntax.
-    ("[]>:/]\\..*emacs\\'" . emacs-lisp-mode)
+    ;; in Unix, MSDOG or VMS syntax.
+    ("[]>:/\\]\\..*emacs\\'" . emacs-lisp-mode)
     ;; _emacs following a directory delimiter
     ;; in MsDos syntax
     ("[:/]_emacs\\'" . emacs-lisp-mode)
@@ -1214,10 +1286,10 @@ and we don't even do that unless it would come from the file name."
                                 modes))))))
     ;; If we found modes to use, invoke them now,
     ;; outside the save-excursion.
-    (if modes
-       (unless just-from-file-name
-         (mapcar 'funcall (nreverse modes))
-         (setq done t)))
+    (when modes
+      (unless just-from-file-name
+       (mapcar 'funcall (nreverse modes)))
+      (setq done t))
     ;; If we didn't find a mode from a -*- line, try using the file name.
     (if (and (not done) buffer-file-name)
        (let ((name buffer-file-name)
@@ -1498,13 +1570,15 @@ is specified, returning t if it is specified."
           (set var val))))
 
 \f
-(defvar change-major-mode-with-file-name t
+(defcustom change-major-mode-with-file-name t
   "*Non-nil means \\[write-file] should set the major mode from the file name.
 However, the mode will not be changed if
 \(1) a local variables list or the `-*-' line specifies a major mode, or
 \(2) the current major mode is a \"special\" mode,
-     not suitable for ordinary files, or
-\(3) the new file name does not particularly specify any mode.")
+\    not suitable for ordinary files, or
+\(3) the new file name does not particularly specify any mode."
+  :type 'boolean
+  :group 'editing-basics)
 
 (defun set-visited-file-name (filename &optional no-query along-with-file)
   "Change name of file visited in current buffer to FILENAME.
@@ -1612,7 +1686,7 @@ the old visited file has been renamed to the new name FILENAME."
          (set-auto-mode t))
     (error nil)))
 
-(defun write-file (filename &optional confirm coding-system)
+(defun write-file (filename &optional confirm)
   "Write current buffer into file FILENAME.
 Makes buffer visit that file, and marks it not modified.
 If the buffer is already visiting a file, you can specify
@@ -1621,10 +1695,7 @@ old name in that directory.
 
 If optional second arg CONFIRM is non-nil,
 ask for confirmation for overwriting an existing file.
-Interactively, confirmation is required unless you supply a prefix argument.
-
-A prefix argument also enables user to interactively specify a
-coding-system for encoding the file."
+Interactively, confirmation is required unless you supply a prefix argument."
 ;;  (interactive "FWrite file: ")
   (interactive
    (list (if buffer-file-name
@@ -1634,10 +1705,7 @@ coding-system for encoding the file."
                               (cdr (assq 'default-directory
                                          (buffer-local-variables)))
                               nil nil (buffer-name)))
-        (not current-prefix-arg)
-        (if current-prefix-arg
-            (read-coding-system "Coding-system: "))
-        ))
+        (not current-prefix-arg)))
   (or (null filename) (string-equal filename "")
       (progn
        ;; If arg is just a directory,
@@ -1651,13 +1719,7 @@ coding-system for encoding the file."
                 (error "Canceled")))
        (set-visited-file-name filename (not confirm))))
   (set-buffer-modified-p t)
-  (if coding-system
-      (let ((coding-system-for-write coding-system))
-       ;; It is convenient to change buffer-file-coding-system to the
-       ;; specified one.
-       (set-buffer-file-coding-system coding-system)
-       (save-buffer))
-    (save-buffer)))
+  (save-buffer))
 \f
 (defun backup-buffer ()
   "Make a backup of the disk file visited by the current buffer, if appropriate.
@@ -1833,17 +1895,19 @@ This is a separate function so you can redefine it for customization.
 You may need to redefine `file-name-sans-versions' as well."
     (string-match "~\\'" file))
 
+(defvar backup-extract-version-start)
+
 ;; This is used in various files.
 ;; The usage of bv-length is not very clean,
 ;; but I can't see a good alternative,
 ;; so as of now I am leaving it alone.
 (defun backup-extract-version (fn)
   "Given the name of a numeric backup file, return the backup number.
-Uses the free variable `bv-length', whose value should be
+Uses the free variable `backup-extract-version-start', whose value should be
 the index in the name where the version number begins."
-  (if (and (string-match "[0-9]+~$" fn bv-length)
-          (= (match-beginning 0) bv-length))
-      (string-to-int (substring fn bv-length -1))
+  (if (and (string-match "[0-9]+~$" fn backup-extract-version-start)
+          (= (match-beginning 0) backup-extract-version-start))
+      (string-to-int (substring fn backup-extract-version-start -1))
       0))
 
 ;; I believe there is no need to alter this behavior for VMS;
@@ -1860,7 +1924,7 @@ If the value is nil, don't make a backup."
       (if (eq version-control 'never)
          (list (make-backup-file-name fn))
        (let* ((base-versions (concat (file-name-nondirectory fn) ".~"))
-              (bv-length (length base-versions))
+              (backup-extract-version-start (length base-versions))
               possibilities
               (versions nil)
               (high-water-mark 0)
@@ -1906,21 +1970,36 @@ If this is impossible (which can happen on MSDOS and Windows
 when the file name and directory use different drive names)
 then it returns FILENAME."
   (save-match-data
-    (setq fname (expand-file-name filename)
-         directory (file-name-as-directory
-                    (expand-file-name (or directory default-directory))))
-    ;; On Microsoft OSes, if FILENAME and DIRECTORY have different
-    ;; drive names, they can't be relative, so return the absolute name.
-    (if (and (or (eq system-type 'ms-dos)
-                (eq system-type 'windows-nt))
-            (not (string-equal (substring fname  0 2)
-                               (substring directory 0 2))))
-       filename
-      (let ((ancestor ""))
-       (while (not (string-match (concat "^" (regexp-quote directory)) fname))
-         (setq directory (file-name-directory (substring directory 0 -1))
-               ancestor (concat "../" ancestor)))
-       (concat ancestor (substring fname (match-end 0)))))))
+    (let ((fname (expand-file-name filename)))
+      (setq directory (file-name-as-directory
+                      (expand-file-name (or directory default-directory))))
+      ;; On Microsoft OSes, if FILENAME and DIRECTORY have different
+      ;; drive names, they can't be relative, so return the absolute name.
+      (if (and (or (eq system-type 'ms-dos)
+                  (eq system-type 'windows-nt))
+              (not (string-equal (substring fname  0 2)
+                                 (substring directory 0 2))))
+         filename
+       (let ((ancestor ".")
+             (fname-dir (file-name-as-directory fname)))
+         (while (and (not (string-match (concat "^" (regexp-quote directory)) fname-dir))
+                     (not (string-match (concat "^" (regexp-quote directory)) fname)))
+           (setq directory (file-name-directory (substring directory 0 -1))
+                 ancestor (if (equal ancestor ".")
+                              ".."
+                            (concat "../" ancestor))))
+         ;; Now ancestor is empty, or .., or ../.., etc.
+         (if (string-match (concat "^" (regexp-quote directory)) fname)
+             ;; We matched within FNAME's directory part.
+             ;; Add the rest of FNAME onto ANCESTOR.
+             (let ((rest (substring fname (match-end 0))))
+               (if (and (equal ancestor ".")
+                        (not (equal rest "")))
+                   ;; But don't bother with ANCESTOR if it would give us `./'.
+                   rest
+                 (concat (file-name-as-directory ancestor) rest)))
+           ;; We matched FNAME's directory equivalent.
+           ancestor))))))
 \f
 (defun save-buffer (&optional args)
   "Save current buffer in visited file if modified.  Versions described below.
@@ -2058,7 +2137,9 @@ After saving the buffer, run `after-save-hook'."
     (if (not (file-writable-p buffer-file-name))
        (let ((dir (file-name-directory buffer-file-name)))
          (if (not (file-directory-p dir))
-             (error "%s is not a directory" dir)
+             (if (file-exists-p dir)
+                 (error "%s is not a directory" dir)
+               (error "%s: no such directory" buffer-file-name))
            (if (not (file-exists-p buffer-file-name))
                (error "Directory %s write-protected" dir)
              (if (yes-or-no-p
@@ -2156,10 +2237,10 @@ Optional second argument EXITING means ask about certain non-file buffers
             (buffer-list)
             '("buffer" "buffers" "save")
             (list (list ?\C-r (lambda (buf)
-                                (view-buffer buf)
-                                (setq view-exit-action
-                                      '(lambda (ignore)
-                                         (exit-recursive-edit)))
+                                (view-buffer buf
+                                             (function
+                                              (lambda (ignore)
+                                                (exit-recursive-edit))))
                                 (recursive-edit)
                                 ;; Return nil to ask about BUF again.
                                 nil)
@@ -2190,44 +2271,44 @@ prints a message in the minibuffer.  Instead, use `set-buffer-modified-p'."
 
 (defun toggle-read-only (&optional arg)
   "Change whether this buffer is visiting its file read-only.
-With arg, set read-only iff arg is positive."
+With arg, set read-only iff arg is positive.
+If visiting file read-only and `view-read-only' is non-nil, enter view mode."
   (interactive "P")
-  (setq buffer-read-only
-       (if (null arg)
-            (not buffer-read-only)
-            (> (prefix-numeric-value arg) 0)))
-  (force-mode-line-update))
+  (cond
+   ((and arg (if (> (prefix-numeric-value arg) 0) buffer-read-only
+              (not buffer-read-only))) ; If buffer-read-only is set correctly,
+    nil)                               ; do nothing.
+   ;; Toggle.
+   ((and buffer-read-only view-mode)
+    (View-exit-and-edit))              ; Must leave view mode.
+   ((and (not buffer-read-only) view-read-only
+        (not (eq (get major-mode 'mode-class) 'special)))
+    (view-mode-enter))
+   (t (setq buffer-read-only (not buffer-read-only))
+      (force-mode-line-update))))
 
-(defun insert-file (filename &optional coding-system)
+(defun insert-file (filename)
   "Insert contents of file FILENAME into buffer after point.
 Set mark after the inserted text.
-A prefix argument enables user to specify the coding-system interactively.
 
 This function is meant for the user to run interactively.
 Don't call it from programs!  Use `insert-file-contents' instead.
 \(Its calling sequence is different; see its documentation)."
-  (interactive "*fInsert file: \nZCoding-system: ")
+  (interactive "*fInsert file: ")
   (if (file-directory-p filename)
       (signal 'file-error (list "Opening input file" "file is a directory"
                                filename)))
-  (let ((tem
-        (if coding-system
-            (let ((coding-system-for-read coding-system))
-              (insert-file-contents filename))
-          (insert-file-contents filename))))
+  (let ((tem (insert-file-contents filename)))
     (push-mark (+ (point) (car (cdr tem))))))
 
-(defun append-to-file (start end filename &optional coding-system)
+(defun append-to-file (start end filename)
   "Append the contents of the region to the end of file FILENAME.
 When called from a function, expects three arguments,
 START, END and FILENAME.  START and END are buffer positions
 saying what text to write.
 A prefix argument enables user to specify the coding-system interactively."
-  (interactive "r\nFAppend to file: \nZCoding-system: ")
-  (if coding-system
-      (let ((coding-system-for-write coding-system))
-       (write-region start end filename t))
-    (write-region start end filename t)))
+  (interactive "r\nFAppend to file: ")
+  (write-region start end filename t))
 
 (defun file-newest-backup (filename)
   "Return most recent backup file for FILENAME or nil if no backups exist."
@@ -2373,6 +2454,14 @@ non-nil, it is called instead of rereading visited file contents."
       (cond ((null file-name)
             (error "Buffer does not seem to be associated with any file"))
            ((or noconfirm
+                (and (not (buffer-modified-p))
+                     (let ((tail revert-without-query)
+                           (found nil))
+                       (while tail
+                         (if (string-match (car tail) file-name)
+                             (setq found t))
+                         (setq tail (cdr tail)))
+                       found))
                 (yes-or-no-p (format "Revert buffer from file %s? "
                                      file-name)))
             (run-hooks 'before-revert-hook)
@@ -2721,15 +2810,19 @@ by `sh' are supported."
     ;; not its part.  Make the regexp say so.
     (concat "\\`" result "\\'")))
 \f
-(defvar list-directory-brief-switches
+(defcustom list-directory-brief-switches
   (if (eq system-type 'vax-vms) "" "-CF")
-  "*Switches for list-directory to pass to `ls' for brief listing,")
+  "*Switches for list-directory to pass to `ls' for brief listing,"
+  :type 'string
+  :group 'dired)
 
-(defvar list-directory-verbose-switches
+(defcustom list-directory-verbose-switches
   (if (eq system-type 'vax-vms)
       "/PROTECTION/SIZE/DATE/OWNER/WIDTH=(OWNER:10)"
     "-l")
-  "*Switches for list-directory to pass to `ls' for verbose listing,")
+  "*Switches for list-directory to pass to `ls' for verbose listing,"
+  :type 'string
+  :group 'dired)
 
 (defun list-directory (dirname &optional verbose)
   "Display a list of files in or matching DIRNAME, a la `ls'.
@@ -2912,9 +3005,14 @@ With prefix arg, silently save all file-visiting buffers, then kill."
                        ;; in the return value.
                        ;; So just avoid stripping it in the first place.
                        '((expand-file-name . nil)
+                         ;; `identity' means just return the first arg
+                         ;; as stripped of its quoting.
+                         (substitute-in-file-name . identity)
                          (file-name-directory . nil)
                          (file-name-as-directory . nil)
                          (directory-file-name . nil)
+                         (file-name-completion 0 1)
+                         (file-name-all-completions 0 1)
                          (rename-file 0 1)
                          (copy-file 0 1)
                          (make-symbolic-link 0 1)
@@ -2926,16 +3024,17 @@ With prefix arg, silently save all file-visiting buffers, then kill."
        (arguments (copy-sequence arguments)))
     ;; Strip off the /: from the file names that have this handler.
     (save-match-data
-      (while file-arg-indices
+      (while (consp file-arg-indices)
        (and (nth (car file-arg-indices) arguments)
             (string-match "\\`/:" (nth (car file-arg-indices) arguments))
             (setcar (nthcdr (car file-arg-indices) arguments)
                     (substring (nth (car file-arg-indices) arguments) 2)))
        (setq file-arg-indices (cdr file-arg-indices))))
-    (apply operation arguments)))
+    (if (eq file-arg-indices 'identity)
+       (car arguments)
+      (apply operation arguments))))
 \f
 (define-key ctl-x-map "\C-f" 'find-file)
-(define-key ctl-x-map "\C-q" 'toggle-read-only)
 (define-key ctl-x-map "\C-r" 'find-file-read-only)
 (define-key ctl-x-map "\C-v" 'find-alternate-file)
 (define-key ctl-x-map "\C-s" 'save-buffer)