home *** CD-ROM | disk | FTP | other *** search
/ BCI NET 2 / BCI NET 2.iso / archives / programming / source / a2.0bemacs-src.lha / Emacs-19.25 / lisp / files.el < prev    next >
Encoding:
Text File  |  1994-08-28  |  83.0 KB  |  2,146 lines

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