home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / emacs-18.59-bin.lha / lib / emacs / 18.59 / lisp / files.el < prev    next >
Lisp/Scheme  |  1996-09-09  |  43KB  |  1,101 lines

  1. ;; File input and output commands for Emacs
  2. ;; Copyright (C) 1985, 1986, 1987, 1990 Free Software Foundation, Inc.
  3.  
  4. ;; This file is part of GNU Emacs.
  5.  
  6. ;; GNU Emacs is free software; you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation; either version 1, or (at your option)
  9. ;; any later version.
  10.  
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. ;; GNU General Public License for more details.
  15.  
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs; see the file COPYING.  If not, write to
  18. ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.  
  21. (defconst delete-auto-save-files t
  22.   "*Non-nil means delete a buffer's auto-save file
  23. when the buffer is saved for real.")
  24.  
  25. ;(make-variable-buffer-local 'buffer-backed-up)
  26. ;(defvar buffer-backed-up nil
  27. ;  "Non-nil if this buffer's file has been backed up.
  28. ;Backing up is done before the first time the file is saved.")
  29.  
  30. ;;; Turn off backup files on VMS since it has version numbers.
  31. (defconst make-backup-files (not (eq system-type 'vax-vms))
  32.   "*Create a backup of each file when it is saved for the first time.
  33. This can be done by renaming the file or by copying.
  34.  
  35. Renaming means that Emacs renames the existing file so that it is a
  36. backup file, then writes the buffer into a new file.  Any other names
  37. that the old file had will now refer to the backup file.
  38. The new file is owned by you and its group is defaulted.
  39.  
  40. Copying means that Emacs copies the existing file into the backup file,
  41. then writes the buffer on top of the existing file.  Any other names
  42. that the old file had will now refer to the new (edited) file.
  43. The file's owner and group are unchanged.
  44.  
  45. The choice of renaming or copying is controlled by the variables
  46. backup-by-copying, backup-by-copying-when-linked and
  47. backup-by-copying-when-mismatch.")
  48.  
  49. (defconst backup-by-copying nil
  50.  "*Non-nil means always use copying to create backup files.
  51. See documentation of variable  make-backup-files.")
  52.  
  53. (defconst backup-by-copying-when-linked nil
  54.  "*Non-nil means use copying to create backups for files with multiple names.
  55. This causes the alternate names to refer to the latest version as edited.
  56. This variable is relevant only if  backup-by-copying  is nil.")
  57.  
  58. (defconst backup-by-copying-when-mismatch nil
  59.   "*Non-nil means create backups by copying if this preserves owner or group.
  60. Renaming may still be used (subject to control of other variables)
  61. when it would not result in changing the owner or group of the file;
  62. that is, for files which are owned by you and whose group matches
  63. the default for a new file created there by you.
  64. This variable is relevant only if  backup-by-copying  is nil.")
  65.  
  66. (defconst buffer-offer-save nil
  67.   "*Non-nil in a buffer means offer to save the buffer on exit
  68. even if the buffer is not visiting a file.  Automatically local in
  69. all buffers.")
  70. (make-variable-buffer-local 'buffer-offer-save)
  71.  
  72. (defconst file-precious-flag nil
  73.   "*Non-nil means protect against I/O errors while saving files.
  74. Some modes set this non-nil in particular buffers.")
  75.  
  76. (defvar version-control nil
  77.   "*Control use of version numbers for backup files.
  78. t means make numeric backup versions unconditionally.
  79. nil means make them for files that have some already.
  80. never means do not make them.")
  81.  
  82. (defvar dired-kept-versions 2
  83.   "*When cleaning directory, number of versions to keep.")
  84.  
  85. (defvar trim-versions-without-asking nil
  86.   "*If true, deletes excess backup versions silently.
  87. Otherwise asks confirmation.")
  88.  
  89. (defvar kept-old-versions 2
  90.   "*Number of oldest versions to keep when a new numbered backup is made.")
  91.  
  92. (defvar kept-new-versions 2
  93.   "*Number of newest versions to keep when a new numbered backup is made.
  94. Includes the new backup.  Must be > 0")
  95.  
  96. (defconst require-final-newline nil
  97.   "*t says silently put a newline at the end whenever a file is saved.
  98. Non-nil but not t says ask user whether to add a newline in each such case.
  99. nil means don't add newlines.")
  100.  
  101. (defconst auto-save-default t
  102.   "*t says by default do auto-saving of every file-visiting buffer.")
  103.  
  104. (defconst auto-save-visited-file-name nil
  105.   "*t says auto-save a buffer in the file it is visiting, when practical.
  106. Normally auto-save files are written under other names.")
  107.  
  108. (defconst save-abbrevs nil
  109.   "*Non-nil means save word abbrevs too when files are saved.
  110. Loading an abbrev file sets this to t.")
  111.  
  112. (defconst find-file-run-dired t
  113.   "*Non-nil says run dired if find-file is given the name of a directory.")
  114.  
  115. (defvar find-file-not-found-hooks nil
  116.   "List of functions to be called for find-file on nonexistent file.
  117. These functions are called as soon as the error is detected.
  118. buffer-file-name is already set up.
  119. The functions are called in the order given,
  120. until one of them returns non-nil.")
  121.  
  122. (defvar find-file-hooks nil
  123.   "List of functions to be called after a buffer is loaded from a file.
  124. The buffer's local variables (if any) will have been processed before the
  125. functions are called.")
  126.  
  127. (defvar write-file-hooks nil
  128.   "List of functions to be called before writing out a buffer to a file.
  129. If one of them returns non-nil, the file is considered already written
  130. and the rest are not called.")
  131.  
  132. (defconst inhibit-local-variables nil
  133.   "*Non-nil means query before obeying a file's local-variables list.
  134. This applies when the local-variables list is scanned automatically
  135. after you find a file.  If you explicitly request such a scan with
  136. \\[normal-mode], there is no query, regardless of this variable.")
  137.  
  138. (defconst ignore-local-eval nil
  139.   "*Non-nil means ignore the \"variable\" `eval' in a file's local variables.
  140. This applies when the local-variables list is scanned automatically
  141. after you find a file.  If you explicitly request such a scan with
  142. \\[normal-mode], there is no query, regardless of this variable.")
  143.  
  144. (defconst backup-char (if (eq system-type 'amigaos) "!" "~")
  145.   "Character to add to file names to make backup names.")
  146.  
  147. (defconst autosave-char (if (eq system-type 'amigaos) "@" "#")
  148.   "Character to add to file names to make autosave names.")
  149.  
  150. (defconst bufferfile-char (if (eq system-type 'amigaos) "^" "%")
  151.   "Character to add to buffer names to make file names.")
  152.  
  153.  
  154. ;; Avoid losing in versions where CLASH_DETECTION is disabled.
  155. (or (fboundp 'lock-buffer)
  156.     (fset 'lock-buffer 'ignore))
  157. (or (fboundp 'unlock-buffer)
  158.     (fset 'unlock-buffer 'ignore))
  159.  
  160. (defun pwd ()
  161.   "Show the current default directory."
  162.   (interactive nil)
  163.   (message "Directory %s" default-directory))
  164.  
  165. (defun cd (dir)
  166.   "Make DIR become the current buffer's default directory."
  167.   (interactive "DChange default directory: ")
  168.   (setq dir (expand-file-name dir))
  169.   (if (not (eq system-type 'vax-vms))
  170.       (setq dir (file-name-as-directory dir)))
  171.   (if (not (file-directory-p dir))
  172.       (error "%s is not a directory" dir)
  173.     (setq default-directory dir))
  174.   (pwd))
  175.  
  176. (defun load-file (file)
  177.   "Load the file FILE of Lisp code."
  178.   (interactive "fLoad file: ")
  179.   (load (expand-file-name file) nil nil t))
  180.  
  181. (defun load-library (library)
  182.   "Load the library named LIBRARY.
  183. This is an interface to the function `load'."
  184.   (interactive "sLoad library: ")
  185.   (load library))
  186.  
  187. (defun switch-to-buffer-other-window (buffer)
  188.   "Select buffer BUFFER in another window."
  189.   (interactive "BSwitch to buffer in other window: ")
  190.   (let ((pop-up-windows t))
  191.     (pop-to-buffer buffer t)))
  192.  
  193. (defun find-file (filename)
  194.   "Edit file FILENAME.
  195. Switch to a buffer visiting file FILENAME,
  196. creating one if none already exists."
  197.   (interactive "FFind file: ")
  198.   (switch-to-buffer (find-file-noselect filename)))
  199.  
  200. (defun find-file-other-window (filename)
  201.   "Edit file FILENAME, in another window.
  202. May create a new window, or reuse an existing one;
  203. see the function display-buffer."
  204.   (interactive "FFind file in other window: ")
  205.   (switch-to-buffer-other-window (find-file-noselect filename)))
  206.  
  207. (defun find-file-read-only (filename)
  208.   "Edit file FILENAME but don't save without confirmation.
  209. Like find-file but marks buffer as read-only."
  210.   (interactive "fFind file read-only: ")
  211.   (find-file filename)
  212.   (setq buffer-read-only t))
  213.  
  214. (defun find-alternate-file (filename)
  215.   "Find file FILENAME, select its buffer, kill previous buffer.
  216. If the current buffer now contains an empty file that you just visited
  217. \(presumably by mistake), use this command to visit the file you really want."
  218.   (interactive "FFind alternate file: ")
  219.   (and (buffer-modified-p)
  220. ;;;       (not buffer-read-only)
  221.        (not (yes-or-no-p (format "Buffer %s is modified; kill anyway? "
  222.                  (buffer-name))))
  223.        (error "Aborted"))
  224.   (let ((obuf (current-buffer))
  225.     (ofile buffer-file-name)
  226.     (oname (buffer-name)))
  227.     (rename-buffer " **lose**")
  228.     (setq buffer-file-name nil)
  229.     (unwind-protect
  230.     (progn
  231.       (unlock-buffer)
  232.       (find-file filename))
  233.       (cond ((eq obuf (current-buffer))
  234.          (setq buffer-file-name ofile)
  235.          (lock-buffer)
  236.          (rename-buffer oname))))
  237.     (or (eq obuf (current-buffer))
  238.     (kill-buffer obuf))))
  239.  
  240. (defun create-file-buffer (filename)
  241.   "Create a suitably named buffer for visiting FILENAME, and return it.
  242. FILENAME (sans directory) is used unchanged if that name is free;
  243. otherwise a string <2> or <3> or ... is appended to get an unused name."
  244.   (let ((lastname (file-name-nondirectory filename)))
  245.     (if (string= lastname "")
  246.     (setq lastname filename))
  247.     (generate-new-buffer lastname)))
  248.  
  249. (defconst automount-dir-prefix "^/tmp_mnt/"
  250.   "Regexp to match the automounter prefix in a directory name.")
  251.  
  252. (defun find-file-noselect (filename &optional nowarn)
  253.   "Read file FILENAME into a buffer and return the buffer.
  254. If a buffer exists visiting FILENAME, return that one,
  255. but verify that the file has not changed since visited or saved.
  256. The buffer is not selected, just returned to the caller."
  257.   (setq filename (expand-file-name filename))
  258.   ;; Get rid of the prefixes added by the automounter.
  259.   (if (and (string-match automount-dir-prefix filename)
  260.        (file-exists-p (file-name-directory
  261.                (substring filename (1- (match-end 0))))))
  262.       (setq filename (substring filename (1- (match-end 0)))))
  263.   (if (file-directory-p filename)
  264.       (if find-file-run-dired
  265.       (dired-noselect filename)
  266.     (error "%s is a directory." filename))
  267.     (let ((buf (get-file-buffer filename))
  268.       error)
  269.       (if buf
  270.       (or nowarn
  271.           (verify-visited-file-modtime buf)
  272.           (cond ((not (file-exists-p filename))
  273.              (error "File %s no longer exists!" filename))
  274.             ((yes-or-no-p
  275.               (if (buffer-modified-p buf)
  276.     "File has changed since last visited or saved.  Flush your changes? "
  277.     "File has changed since last visited or saved.  Read from disk? "))
  278.              (save-excursion
  279.                (set-buffer buf)
  280.                (revert-buffer t t)))))
  281.     (save-excursion
  282.       (setq buf (create-file-buffer filename))
  283.       (set-buffer buf)
  284.       (erase-buffer)
  285.       (condition-case ()
  286.           (insert-file-contents filename t)
  287.         (file-error
  288.          (setq error t)
  289.          ;; Run find-file-not-found-hooks until one returns non-nil.
  290.          (let ((hooks find-file-not-found-hooks))
  291.            (while (and hooks
  292.                (not (funcall (car hooks))))
  293.          (setq hooks (cdr hooks))))))
  294.       (setq default-directory (file-name-directory filename))
  295.       (after-find-file error (not nowarn))))
  296.       buf)))
  297.  
  298. (defun after-find-file (&optional error warn)
  299.   "Called after finding a file and by the default revert function.
  300. Sets buffer mode, parses local variables.
  301. Optional args ERROR and WARN: ERROR non-nil means there was an
  302. error in reading the file.  WARN non-nil means warn if there
  303. exists an auto-save file more recent than the visited file.
  304. Finishes by calling the functions in find-file-hooks."
  305.   (setq buffer-read-only (not (file-writable-p buffer-file-name)))
  306.   (if noninteractive
  307.       nil
  308.     (let* (not-serious
  309.        (msg
  310.         (cond ((not buffer-read-only)
  311.            (if (and warn
  312.                 (file-newer-than-file-p (make-auto-save-file-name)
  313.                             buffer-file-name))
  314.                "Auto save file is newer; consider M-x recover-file"
  315.              (setq not-serious t)
  316.              (if error "(New file)" nil)))
  317.           ((not error)
  318.            (setq not-serious t)
  319.            "File is write protected")
  320.           ((file-attributes buffer-file-name)
  321.            "File exists, but is read-protected.")
  322.           ((file-attributes (directory-file-name default-directory))
  323.            "File not found and directory write-protected")
  324.           (t
  325.            "File not found and directory doesn't exist"))))
  326.       (if msg
  327.       (progn
  328.         (message msg)
  329.         (or not-serious (sit-for 1 t)))))
  330.     (if auto-save-default
  331.     (auto-save-mode t)))
  332.   (normal-mode t)
  333.   (mapcar 'funcall find-file-hooks))
  334.  
  335. (defun normal-mode (&optional find-file)
  336.   "Choose the major mode for this buffer automatically.
  337. Also sets up any specified local variables of the file.
  338. Uses the visited file name, the -*- line, and the local variables spec.
  339.  
  340. This function is called automatically from `find-file'.  In that case,
  341. if `inhibit-local-variables' is non-`nil' we require confirmation before
  342. processing a local variables spec.  If you run `normal-mode' explicitly,
  343. confirmation is never required."
  344.   (interactive)
  345.   (or find-file (funcall (or default-major-mode 'fundamental-mode)))
  346.   (condition-case err
  347.       (set-auto-mode)
  348.     (error (message "File mode specification error: %s"
  349.             (prin1-to-string err))))
  350.   (condition-case err
  351.       (hack-local-variables (not find-file))
  352.     (error (message "File local-variables error: %s"
  353.             (prin1-to-string err)))))
  354.  
  355. ;(defvar auto-mode-alist ...) now in loaddefs.el
  356. (defun set-auto-mode ()
  357.   "Select major mode appropriate for current buffer.
  358. May base decision on visited file name (See variable  auto-mode-list)
  359. or on buffer contents (-*- line or local variables spec), but does not look
  360. for the \"mode:\" local variable.  For that, use  hack-local-variables."
  361.   ;; Look for -*-MODENAME-*- or -*- ... mode: MODENAME; ... -*-
  362.   (let (beg end mode)
  363.     (save-excursion
  364.       (goto-char (point-min))
  365.       (skip-chars-forward " \t\n")
  366.       (if (and (search-forward "-*-" (save-excursion (end-of-line) (point)) t)
  367.            (progn
  368.          (skip-chars-forward " \t")
  369.          (setq beg (point))
  370.          (search-forward "-*-" (save-excursion (end-of-line) (point)) t))
  371.            (progn
  372.          (forward-char -3)
  373.          (skip-chars-backward " \t")
  374.          (setq end (point))
  375.          (goto-char beg)
  376.          (if (search-forward ":" end t)
  377.              (progn
  378.                (goto-char beg)
  379.                (if (let ((case-fold-search t))
  380.                  (search-forward "mode:" end t))
  381.                (progn
  382.                  (skip-chars-forward " \t")
  383.                  (setq beg (point))
  384.                  (if (search-forward ";" end t)
  385.                  (forward-char -1)
  386.                    (goto-char end))
  387.                  (skip-chars-backward " \t")
  388.                  (setq mode (buffer-substring beg (point))))))
  389.            (setq mode (buffer-substring beg end)))))
  390.       (funcall (intern (concat (downcase mode) "-mode")))
  391.     (let ((alist auto-mode-alist)
  392.           (name buffer-file-name))
  393.       (let ((case-fold-search (or (eq system-type 'amigaos)
  394.                       (eq system-type 'vax-vms))))
  395.         ;; Remove backup-suffixes from file name.
  396.         (setq name (file-name-sans-versions name))
  397.         ;; Find first matching alist entry.
  398.         (while (and (not mode) alist)
  399.           (if (string-match (car (car alist)) name)
  400.           (setq mode (cdr (car alist))))
  401.           (setq alist (cdr alist))))
  402.       (if mode (funcall mode)))))))
  403.  
  404. (defun hack-local-variables (&optional force)
  405.   "Parse, and bind or evaluate as appropriate, any local variables
  406. for current buffer."
  407.   ;; Look for "Local variables:" line in last page.
  408.   (save-excursion
  409.     (goto-char (point-max))
  410.     (search-backward "\n\^L" (max (- (point-max) 3000) (point-min)) 'move)
  411.     (if (let ((case-fold-search t))
  412.       (and (search-forward "Local Variables:" nil t)
  413.            (or (not inhibit-local-variables)
  414.            force
  415.            (save-window-excursion
  416.              (switch-to-buffer (current-buffer))
  417.              (save-excursion
  418.                (beginning-of-line)
  419.                (set-window-start (selected-window) (point)))
  420.              (y-or-n-p (format "Set local variables as specified at end of %s? "
  421.                        (file-name-nondirectory buffer-file-name)))))))
  422.     (let ((continue t)
  423.           prefix prefixlen suffix beg)
  424.       ;; The prefix is what comes before "local variables:" in its line.
  425.       ;; The suffix is what comes after "local variables:" in its line.
  426.       (skip-chars-forward " \t")
  427.       (or (eolp)
  428.           (setq suffix (buffer-substring (point)
  429.                          (progn (end-of-line) (point)))))
  430.       (goto-char (match-beginning 0))
  431.       (or (bolp)
  432.           (setq prefix
  433.             (buffer-substring (point)
  434.                       (progn (beginning-of-line) (point)))))
  435.       (if prefix (setq prefixlen (length prefix)
  436.                prefix (regexp-quote prefix)))
  437.       (if suffix (setq suffix (concat (regexp-quote suffix) "$")))
  438.       (while continue
  439.         ;; Look at next local variable spec.
  440.         (if selective-display (re-search-forward "[\n\C-m]")
  441.           (forward-line 1))
  442.         ;; Skip the prefix, if any.
  443.         (if prefix
  444.         (if (looking-at prefix)
  445.             (forward-char prefixlen)
  446.           (error "Local variables entry is missing the prefix")))
  447.         ;; Find the variable name; strip whitespace.
  448.         (skip-chars-forward " \t")
  449.         (setq beg (point))
  450.         (skip-chars-forward "^:\n")
  451.         (if (eolp) (error "Missing colon in local variables entry"))
  452.         (skip-chars-backward " \t")
  453.         (let* ((str (buffer-substring beg (point)))
  454.            (var (read str))
  455.           val)
  456.           ;; Setting variable named "end" means end of list.
  457.           (if (string-equal (downcase str) "end")
  458.           (setq continue nil)
  459.         ;; Otherwise read the variable value.
  460.         (skip-chars-forward "^:")
  461.         (forward-char 1)
  462.         (setq val (read (current-buffer)))
  463.         (skip-chars-backward "\n")
  464.         (skip-chars-forward " \t")
  465.         (or (if suffix (looking-at suffix) (eolp))
  466.             (error "Local variables entry is terminated incorrectly"))
  467.         ;; Set the variable.  "Variables" mode and eval are funny.
  468.         (cond ((eq var 'mode)
  469.                (funcall (intern (concat (downcase (symbol-name val))
  470.                         "-mode"))))
  471.               ((eq var 'force) nil)
  472.               ((eq var 'ignore-local-eval)
  473.                nil)
  474.               ((eq var 'eval)
  475.                (if (or (and ignore-local-eval (not force))
  476.                    (string= (user-login-name) "root"))
  477.                (message "Ignoring `eval:' in file's local variables")
  478.              (eval val)))
  479.               (t (make-local-variable var)
  480.              (set var val))))))))))
  481.  
  482. (defun set-visited-file-name (filename)
  483.   "Change name of file visited in current buffer to FILENAME.
  484. The next time the buffer is saved it will go in the newly specified file.
  485. nil or empty string as argument means make buffer not be visiting any file.
  486. Remember to delete the initial contents of the minibuffer
  487. if you wish to pass an empty string as the argument."
  488.   (interactive "FSet visited file name: ")
  489.   (if filename
  490.       (setq filename
  491.         (if (string-equal filename "")
  492.         nil
  493.           (expand-file-name filename))))
  494.   (or (equal filename buffer-file-name)
  495.       (null filename)
  496.       (progn
  497.     (lock-buffer filename)
  498.     (unlock-buffer)))
  499.   (if filename
  500.       (let ((new-name (file-name-nondirectory filename)))
  501.     (if (string= new-name "")
  502.         (error "Empty file name"))
  503.     (if (file-directory-p filename)
  504.         (error "File %s is a directory" filename))
  505.     (if (eq system-type 'vax-vms)
  506.         (setq new-name (downcase new-name)))
  507.     (setq buffer-file-name filename)
  508.     (setq default-directory (file-name-directory buffer-file-name))
  509.     (or (get-buffer new-name) (rename-buffer new-name)))
  510.     (setq buffer-file-name nil))
  511.   (setq buffer-backed-up nil)
  512.   (clear-visited-file-modtime)
  513.   ;; So that C-x C-w after ftp-find-file
  514.   ;; writes an ordinary local file in the ordinary way.
  515.   (kill-local-variable 'write-file-hooks)
  516.   ;; So that revert works normally after theat C-x C-w.
  517.   (kill-local-variable 'revert-buffer-function)
  518.   ;; Rename the auto-save file to go with the new visited name.
  519.   ;; If auto-save was not already on, turn it on if appropriate.
  520.   (if buffer-auto-save-file-name
  521.       (rename-auto-save-file)
  522.     (auto-save-mode (and buffer-file-name auto-save-default)))
  523.   (if buffer-file-name
  524.       (set-buffer-modified-p t)))
  525.  
  526. (defun write-file (filename)
  527.   "Write current buffer into file FILENAME.
  528. Makes buffer visit that file, and marks it not modified."
  529.   (interactive "FWrite file: ")
  530.   (or (null filename) (string-equal filename "")
  531.       (set-visited-file-name filename))
  532.   (set-buffer-modified-p t)
  533.   (save-buffer))
  534.  
  535. (defun backup-buffer ()
  536.   "Make a backup of the disk file visited by the current buffer, if appropriate.
  537. This is normally done before saving the buffer the first time.
  538. If the value is non-nil, it is the result of `file-modes' on the original file;
  539. this means that the caller, after saving the buffer, should change the modes
  540. of the new file to agree with the old modes."
  541.   (and make-backup-files
  542.        (not buffer-backed-up)
  543.        (file-exists-p buffer-file-name)
  544.        (memq (aref (elt (file-attributes buffer-file-name) 8) 0)
  545.          '(?- ?l))
  546.        (or (< (length buffer-file-name) 5)
  547.        (not (string-equal "/tmp/" (substring buffer-file-name 0 5))))
  548.     (condition-case ()
  549.     (let* ((backup-info (find-backup-file-name buffer-file-name))
  550.            (backupname (car backup-info))
  551.            (targets (cdr backup-info))
  552.            setmodes)
  553. ;      (if (file-directory-p buffer-file-name)
  554. ;          (error "Cannot save buffer in directory %s" buffer-file-name))
  555.       (condition-case ()
  556.           (if (or file-precious-flag
  557.               (file-symlink-p buffer-file-name)
  558.               backup-by-copying
  559.               (and backup-by-copying-when-linked
  560.                (> (file-nlinks buffer-file-name) 1))
  561.               (and backup-by-copying-when-mismatch
  562.                (let ((attr (file-attributes buffer-file-name)))
  563.                  (or (nth 9 attr)
  564.                  (/= (nth 2 attr) (user-uid))))))
  565.           (copy-file buffer-file-name backupname t t)
  566.         (condition-case ()
  567.             (delete-file backupname)
  568.           (file-error nil))
  569.         (rename-file buffer-file-name backupname t)
  570.         (setq setmodes (file-modes backupname)))
  571.         (file-error
  572.          ;; If trouble writing the backup, write it in ~.
  573.          (setq backupname
  574.            (expand-file-name (concat "~/" bufferfile-char "backup"
  575.                          bufferfile-char backup-char)))
  576.          (message "Cannot write backup file; backing up in ~/%sbackup%s%s"
  577.               bufferfile-char bufferfile-char backup-char)
  578.          (sleep-for 1)
  579.          (condition-case ()
  580.          (delete-file backupname)
  581.            (file-error nil))
  582.          (copy-file buffer-file-name backupname t t)))
  583.       (setq buffer-backed-up t)
  584.       (if (and targets
  585.            (or trim-versions-without-asking
  586.                (y-or-n-p (format "Delete excess backup versions of %s? "
  587.                      buffer-file-name))))
  588.           (while targets
  589.         (condition-case ()
  590.             (delete-file (car targets))
  591.           (file-error nil))
  592.         (setq targets (cdr targets))))
  593.       setmodes)
  594.       (file-error nil))))
  595.  
  596. (defun file-name-sans-versions (name)
  597.   "Return FILENAME sans backup versions or strings.
  598. This is a separate procedure so your site-init or startup file can
  599. redefine it."
  600.   (substring name 0
  601.          (if (eq system-type 'vax-vms)
  602.          (or (string-match ";[0-9]*\\'" name)
  603.              (and (string-match "\\." name (string-match "[]>]" name))
  604.               (string-match "\\.[0-9]*\\'" name (match-end 0))))
  605.            (string-match (concat "\\(\\." backup-char "[0-9]+\\)?"
  606.                      backup-char "\\'") name))))
  607.  
  608. (defun make-backup-file-name (file)
  609.   "Create the non-numeric backup file name for FILE.
  610. This is a separate function so you can redefine it for customization."
  611.   (concat file backup-char))
  612.  
  613. (defun backup-file-name-p (file)
  614.   "Return non-nil if FILE is a backup file name (numeric or not).
  615. This is a separate function so you can redefine it for customization.
  616. You may need to redefine file-name-sans-versions as well."
  617.   (string-match (concat backup-char "$") file))
  618.  
  619. ;; I believe there is no need to alter this behavior for VMS;
  620. ;; since backup files are not made on VMS, it should not get called.
  621. (defun find-backup-file-name (fn)
  622.   "Find a file name for a backup file, and suggestions for deletions.
  623. Value is a list whose car is the name for the backup file
  624.  and whose cdr is a list of old versions to consider deleting now."
  625.   (if (eq version-control 'never)
  626.       (list (make-backup-file-name fn))
  627.     (let* ((base-versions (concat (file-name-nondirectory fn) "." backup-char))
  628.        (bv-length (length base-versions))
  629.        (possibilities (file-name-all-completions
  630.                base-versions
  631.                (file-name-directory fn)))
  632.        (versions (sort (mapcar 'backup-extract-version possibilities)
  633.                '<))
  634.        (high-water-mark (apply 'max (cons 0 versions)))
  635.        (deserve-versions-p
  636.         (or version-control
  637.         (> high-water-mark 0)))
  638.        (number-to-delete (- (length versions)
  639.                 ;; -1 compensates for the backup
  640.                 ;; we are about to make.
  641.                 kept-old-versions kept-new-versions -1)))
  642.       (if (not deserve-versions-p)
  643.       (list (make-backup-file-name fn))
  644.     (cons (concat fn "." backup-char (int-to-string (1+ high-water-mark))
  645.               backup-char)
  646.           (if (and (> number-to-delete 0)
  647.                ;; Delete nothing if there is overflow
  648.                ;; in the number of versions to keep.
  649.                (>= (+ kept-new-versions kept-old-versions -1) 0))
  650.           (mapcar (function (lambda (n)
  651.                       (concat fn "." backup-char
  652.                           (int-to-string n) backup-char)))
  653.               (let ((v (nthcdr kept-old-versions versions)))
  654.                 (rplacd (nthcdr (1- number-to-delete) v) ())
  655.                 v))))))))
  656.  
  657. (defun backup-extract-version (fn)
  658.   (if (and (string-match (concat "[0-9]+" backup-char "$") fn bv-length)
  659.        (= (match-beginning 0) bv-length))
  660.       (string-to-int (substring fn bv-length -1))
  661.       0))
  662.  
  663. (defun file-nlinks (filename)
  664.   "Return number of names file FILENAME has." 
  665.   (car (cdr (file-attributes filename))))
  666.  
  667. (defun save-buffer (&optional args)
  668.   "Save current buffer in visited file if modified.  Versions described below.
  669.  
  670. By default, makes the previous version into a backup file
  671.  if previously requested or if this is the first save.
  672. With 1 or 3 \\[universal-argument]'s, marks this version
  673.  to become a backup when the next save is done.
  674. With 2 or 3 \\[universal-argument]'s,
  675.  unconditionally makes the previous version into a backup file.
  676. With argument of 0, never makes the previous version into a backup file.
  677.  
  678. If a file's name is FOO, the names of its numbered backup versions are
  679.  FOO.~i~ for various integers i.  A non-numbered backup file is called FOO~.
  680. Numeric backups (rather than FOO~) will be made if value of
  681.  `version-control' is not the atom `never' and either there are already
  682.  numeric versions of the file being backed up, or `version-control' is
  683.  non-nil.
  684. We don't want excessive versions piling up, so there are variables
  685.  `kept-old-versions', which tells Emacs how many oldest versions to keep,
  686.  and `kept-new-versions', which tells how many newest versions to keep.
  687.  Defaults are 2 old versions and 2 new.
  688. `dired-kept-versions' controls dired's clean-directory (.) command.
  689. If `trim-versions-without-asking' is nil, system will query user
  690.  before trimming versions.  Otherwise it does it silently."
  691.   (interactive "p")
  692.   (let ((modp (buffer-modified-p))
  693.     (large (> (buffer-size) 50000))
  694.     (make-backup-files (and make-backup-files (not (eq args 0)))))
  695.     (and modp (memq args '(16 64)) (setq buffer-backed-up nil))
  696.     (if (and modp large) (message "Saving file %s..." (buffer-file-name)))
  697.     (basic-save-buffer)
  698.     (and modp (memq args '(4 64)) (setq buffer-backed-up nil))))
  699.  
  700. (defun delete-auto-save-file-if-necessary ()
  701.   "Delete the auto-save filename for the current buffer (if it has one)
  702. if variable  delete-auto-save-files  is non-nil."
  703.   (and buffer-auto-save-file-name delete-auto-save-files
  704.        (not (string= buffer-file-name buffer-auto-save-file-name))
  705.        (progn
  706.      (condition-case ()
  707.          (delete-file buffer-auto-save-file-name)
  708.        (file-error nil))
  709.      (set-buffer-auto-saved))))
  710.  
  711. (defun basic-save-buffer ()
  712.   "Save the current buffer in its visited file, if it has been modified."  
  713.   (interactive)
  714.   (if (buffer-modified-p)
  715.       (let (setmodes tempsetmodes)
  716.     (or buffer-file-name
  717.         (progn
  718.           (setq buffer-file-name
  719.             (expand-file-name (read-file-name "File to save in: ") nil)
  720.             default-directory (file-name-directory buffer-file-name))
  721.           (auto-save-mode auto-save-default)))
  722.     (if (not (file-writable-p buffer-file-name))
  723.         (if (yes-or-no-p
  724.          (format "File %s is write-protected; try to save anyway? "
  725.              (file-name-nondirectory buffer-file-name)))
  726.         (setq tempsetmodes t)
  727.           (error
  728.    "Attempt to save to a file which you aren't allowed to write")))
  729.     (or (verify-visited-file-modtime (current-buffer))
  730.         (not (file-exists-p buffer-file-name))
  731.         (yes-or-no-p
  732.           "Disk file has changed since visited or saved.  Save anyway? ")
  733.         (error "Save not confirmed"))
  734.     (or buffer-backed-up
  735.         (setq setmodes (backup-buffer)))
  736.     (save-restriction
  737.       (widen)
  738.       (and (> (point-max) 1)
  739.            (/= (char-after (1- (point-max))) ?\n)
  740.            (or (eq require-final-newline t)
  741.            (and require-final-newline
  742.             (yes-or-no-p
  743.              (format "Buffer %s does not end in newline.  Add one? "
  744.                  (buffer-name)))))
  745.            (save-excursion
  746.          (goto-char (point-max))
  747.          (insert ?\n)))
  748.       (let ((hooks write-file-hooks)
  749.         (done nil))
  750.         (while (and hooks
  751.             (not (setq done (funcall (car hooks)))))
  752.           (setq hooks (cdr hooks)))
  753.         ;; If a hook returned t, file is already "written".
  754.         (cond (done (setq setmodes nil))
  755.           ((not done)
  756.            (if file-precious-flag
  757.                ;; If file is precious, rename it away before
  758.                ;; overwriting it.
  759.                (let ((rename t) nodelete
  760.                  (file (concat buffer-file-name autosave-char)))
  761.              (condition-case ()
  762.                  (progn (rename-file buffer-file-name file t)
  763.                     (setq setmodes (file-modes file)))
  764.                (file-error (setq rename nil nodelete t)))
  765.              (unwind-protect
  766.                  (progn (clear-visited-file-modtime)
  767.                     (write-region (point-min) (point-max)
  768.                           buffer-file-name nil t)
  769.                     (setq rename nil))
  770.                ;; If rename is still t, writing failed.
  771.                ;; So rename the old file back to original name,
  772.                (if rename
  773.                    (progn
  774.                  (rename-file file buffer-file-name t)
  775.                  (clear-visited-file-modtime))
  776.                  ;; Otherwise we don't need the original file,
  777.                  ;; so flush it.  Unless we already lost it.
  778.                  (or nodelete
  779.                  (condition-case ()
  780.                      (delete-file file)
  781.                    (error nil))))))
  782.              ;; If file not writable, see if we can make it writable
  783.              ;; temporarily while we write it.
  784.              ;; But no need to do so if we have just backed it up
  785.              ;; (setmodes is set) because that says we're superseding.
  786.              ;; Systems with version numbers need not do this.
  787.              (if (eq system-type 'vax-vms)
  788.              (setq setmodes nil tempsetmodes nil))
  789.              (cond ((and tempsetmodes (not setmodes))
  790.                 ;; Change the mode back, after writing.
  791.                 (setq setmodes (file-modes buffer-file-name))
  792.                 (set-file-modes buffer-file-name 511)))
  793.              (if (eq system-type 'amigaos)
  794.              (if amiga-create-icons
  795.                  (amiga-put-icon buffer-file-name nil)))
  796.              (write-region (point-min) (point-max) 
  797.                    buffer-file-name nil t)))))
  798.       (if setmodes
  799.           (condition-case ()
  800.            (set-file-modes buffer-file-name setmodes)
  801.         (error nil))))
  802.     (delete-auto-save-file-if-necessary))
  803.     (message "(No changes need to be saved)")))
  804.  
  805. (defun save-some-buffers (&optional arg exiting)
  806.   "Save some modified file-visiting buffers.  Asks user about each one.
  807. Optional argument (the prefix) non-nil means save all with no questions.
  808. Optional second argument EXITING means ask about certain non-file buffers
  809.  as well as about file buffers."
  810.   (interactive "P")
  811.   (let (considered (list (buffer-list)))
  812.     (while list
  813.       (let ((buffer (car list)))
  814.     (and (buffer-modified-p buffer)
  815.          (save-excursion
  816.            (set-buffer buffer)
  817.            (and
  818.         (or buffer-file-name
  819.             (and exiting buffer-offer-save (> (buffer-size) 0)))
  820.         (setq considered t)
  821.         (or arg
  822.             (y-or-n-p (if buffer-file-name
  823.                   (format "Save file %s? "
  824.                       buffer-file-name)
  825.                 (format "Save buffer %s? " (buffer-name)))))
  826.         (condition-case ()
  827.             (save-buffer)
  828.           (error nil))))))
  829.       (setq list (cdr list)))
  830.     (and save-abbrevs abbrevs-changed
  831.      (progn
  832.        (setq considered t)
  833.        (if (or arg
  834.            (y-or-n-p (format "Save abbrevs in %s? " abbrev-file-name)))
  835.            (write-abbrev-file nil))
  836.        ;; Don't keep bothering user if he says no.
  837.        (setq abbrevs-changed nil)))
  838.     (if considered
  839.     (message "")
  840.     (message "(No files need saving)"))))
  841.  
  842. (defun not-modified ()
  843.   "Mark current buffer as unmodified, not needing to be saved."
  844.   (interactive)
  845.   (message "Modification-flag cleared")
  846.   (set-buffer-modified-p nil))
  847.  
  848. (defun toggle-read-only ()
  849.   "Change whether this buffer is visiting its file read-only."
  850.   (interactive)
  851.   (setq buffer-read-only (not buffer-read-only))
  852.   ;; Force mode-line redisplay
  853.   (set-buffer-modified-p (buffer-modified-p)))
  854.  
  855. (defun insert-file (filename)
  856.   "Insert contents of file FILENAME into buffer after point.
  857. Set mark after the inserted text."
  858.   (interactive "fInsert file: ")
  859.   (let ((tem (insert-file-contents filename)))
  860.     (push-mark (+ (point) (car (cdr tem))))))
  861.  
  862. (defun append-to-file (start end filename)
  863.   "Append the contents of the region to the end of file FILENAME.
  864. When called from a function, expects three arguments,
  865. START, END and FILENAME.  START and END are buffer positions
  866. saying what text to write."
  867.   (interactive "r\nFAppend to file: ")
  868.   (write-region start end filename t))
  869.  
  870. (defvar revert-buffer-function nil
  871.   "Function to use to revert this buffer, or nil to do the default.")
  872.  
  873. (defun revert-buffer (&optional arg noconfirm)
  874.   "Replace the buffer text with the text of the visited file on disk.
  875. This undoes all changes since the file was visited or saved.
  876. If latest auto-save file is more recent than the visited file,
  877. asks user whether to use that instead.
  878. First argument (optional) non-nil means don't offer to use auto-save file.
  879.  This is the prefix arg when called interactively.
  880.  
  881. Second argument (optional) non-nil means don't ask for confirmation at all.
  882.  
  883. If revert-buffer-function's value is non-nil, it is called to do the work."
  884.   (interactive "P")
  885.   (if revert-buffer-function
  886.       (funcall revert-buffer-function arg noconfirm)
  887.     (let* ((opoint (point))
  888.        (auto-save-p (and (null arg) (recent-auto-save-p)
  889.                  buffer-auto-save-file-name
  890.                  (file-readable-p buffer-auto-save-file-name)
  891.                  (y-or-n-p
  892.    "Buffer has been auto-saved recently.  Revert from auto-save file? ")))
  893.        (file-name (if auto-save-p
  894.               buffer-auto-save-file-name
  895.             buffer-file-name)))
  896.       (cond ((null file-name)
  897.          (error "Buffer does not seem to be associated with any file"))
  898.         ((not (file-exists-p file-name))
  899.          (error "File %s no longer exists!" file-name))
  900.         ((or noconfirm
  901.          (yes-or-no-p (format "Revert buffer from file %s? "
  902.                       file-name)))
  903.          ;; If file was backed up but has changed since,
  904.          ;; we shd make another backup.
  905.          (and (not auto-save-p)
  906.           (not (verify-visited-file-modtime (current-buffer)))
  907.           (setq buffer-backed-up nil))
  908.          ;; Discard all the undo information.
  909.          (or (eq buffer-undo-list t)
  910.          (setq buffer-undo-list nil))
  911.          (let ((buffer-read-only nil)
  912.            ;; Don't record undo info for the revert itself.
  913.            ;; Doing so chews up too much storage.
  914.            (buffer-undo-list t))
  915.            ;; Bind buffer-file-name to nil
  916.            ;; so that we don't try to lock the file.
  917.            (let ((buffer-file-name nil))
  918.          (or auto-save-p
  919.              (unlock-buffer))
  920.          (erase-buffer))
  921.            (insert-file-contents file-name (not auto-save-p)))
  922.          (goto-char (min opoint (point-max)))
  923.          (after-find-file nil)
  924.          t)))))
  925.  
  926. (defun recover-file (file)
  927.   "Visit file FILE, but get contents from its last auto-save file."
  928.   (interactive "FRecover file: ")
  929.   (setq file (expand-file-name file))
  930.   (if (auto-save-file-name-p file) (error "%s is an auto-save file" file))
  931.   (let ((file-name (let ((buffer-file-name file))
  932.              (make-auto-save-file-name))))
  933.     (cond ((not (file-newer-than-file-p file-name file))
  934.        (error "Auto-save file %s not current" file-name))
  935.       ((save-window-excursion
  936.          (if (not (eq system-type 'vax-vms))
  937.          (with-output-to-temp-buffer "*Directory*"
  938.            (buffer-flush-undo standard-output)
  939.            (call-process "ls" nil standard-output nil
  940.                  "-l" file file-name)))
  941.          (yes-or-no-p (format "Recover auto save file %s? " file-name)))
  942.        (switch-to-buffer (find-file-noselect file t))
  943.        (let ((buffer-read-only nil))
  944.          (erase-buffer)
  945.          (insert-file-contents file-name nil))
  946.        (after-find-file nil))
  947.       (t (error "Recover-file cancelled."))))
  948.   (setq buffer-auto-save-file-name nil)
  949.   (message "Auto-save off in this buffer till you do M-x auto-save-mode."))
  950.  
  951. (defun kill-some-buffers ()
  952.   "For each buffer, ask whether to kill it."
  953.   (interactive)
  954.   (let ((list (buffer-list)))
  955.     (while list
  956.       (let* ((buffer (car list))
  957.          (name (buffer-name buffer)))
  958.     (and (not (string-equal name ""))
  959.          (/= (aref name 0) ? )
  960.          (yes-or-no-p
  961.           (format "Buffer %s %s.  Kill? "
  962.               name
  963.               (if (buffer-modified-p buffer)
  964.               "HAS BEEN EDITED" "is unmodified")))
  965.          (kill-buffer buffer)))
  966.       (setq list (cdr list)))))
  967.  
  968. (defun auto-save-mode (arg)
  969.   "Toggle auto-saving of contents of current buffer.
  970. With arg, turn auto-saving on if arg is positive, else off."
  971.   (interactive "P")
  972.   (setq buffer-auto-save-file-name
  973.         (and (if (null arg)
  974.          (not buffer-auto-save-file-name)
  975.            (or (eq arg t) (listp arg) (and (integerp arg) (> arg 0))))
  976.          (if (and buffer-file-name auto-save-visited-file-name
  977.               (not buffer-read-only))
  978.          buffer-file-name
  979.            (make-auto-save-file-name))))
  980.   (if (interactive-p)
  981.       (message "Auto-save %s (in this buffer)"
  982.            (if buffer-auto-save-file-name "on" "off")))
  983.   buffer-auto-save-file-name)
  984.  
  985. (defun rename-auto-save-file ()
  986.   "Adjust current buffer's auto save file name for current conditions.
  987. Also rename any existing auto save file."
  988.   (let ((osave buffer-auto-save-file-name))
  989.     (setq buffer-auto-save-file-name
  990.       (make-auto-save-file-name))
  991.     (if (and osave buffer-auto-save-file-name
  992.          (not (string= buffer-auto-save-file-name buffer-file-name))
  993.          (not (string= buffer-auto-save-file-name osave))
  994.          (file-exists-p osave))
  995.     (rename-file osave buffer-auto-save-file-name t))))
  996.  
  997. (defun make-auto-save-file-name ()
  998.   "Return file name to use for auto-saves of current buffer.
  999. Does not consider auto-save-visited-file-name; that is checked
  1000. before calling this function.
  1001. You can redefine this for customization.
  1002. See also auto-save-file-name-p."
  1003.   (if buffer-file-name
  1004.       (concat (file-name-directory buffer-file-name)
  1005.           autosave-char
  1006.           (file-name-nondirectory buffer-file-name)
  1007.           autosave-char)
  1008.     (expand-file-name (concat autosave-char bufferfile-char (buffer-name)
  1009.                   autosave-char))))
  1010.  
  1011. (defun auto-save-file-name-p (filename)
  1012.   "Return non-nil if FILENAME can be yielded by make-auto-save-file-name.
  1013. FILENAME should lack slashes.
  1014. You can redefine this for customization."
  1015.   (string-match (concat "^" autosave-char ".*" autosave-char "$") filename))
  1016.  
  1017. (defconst list-directory-brief-switches "-CF"
  1018.   "*Switches for list-directory to pass to `ls' for brief listing,")
  1019. (defconst list-directory-verbose-switches "-l"
  1020.   "*Switches for list-directory to pass to `ls' for verbose listing,")
  1021.  
  1022. (defun list-directory (dirname &optional verbose)
  1023.   "Display a list of files in or matching DIRNAME, a la `ls'.
  1024. DIRNAME is globbed by the shell if necessary.
  1025. Prefix arg (second arg if noninteractive) means supply -l switch to `ls'.
  1026. Actions controlled by variables list-directory-brief-switches
  1027.  and list-directory-verbose-switches."
  1028.   (interactive (let ((pfx current-prefix-arg))
  1029.          (list (read-file-name (if pfx "List directory (verbose): "
  1030.                      "List directory (brief): ")
  1031.                        nil default-directory nil)
  1032.                pfx)))
  1033.   (let ((switches (if verbose list-directory-verbose-switches
  1034.             list-directory-brief-switches))
  1035.     full-dir-p)
  1036.     (or dirname (setq dirname default-directory))
  1037.     (if (file-directory-p dirname)
  1038.     (progn
  1039.      (setq full-dir-p t)
  1040.      (or (string-match "/$" dirname)
  1041.          (setq dirname (concat dirname "/")))))
  1042.     (setq dirname (expand-file-name dirname))
  1043.     (with-output-to-temp-buffer "*Directory*"
  1044.       (buffer-flush-undo standard-output)
  1045.       (princ "Directory ")
  1046.       (princ dirname)
  1047.       (terpri)
  1048.       (if full-dir-p
  1049.       (call-process "ls" nil standard-output nil
  1050.             switches dirname)
  1051.     (let ((default-directory (file-name-directory dirname)))
  1052.       (call-process shell-file-name nil standard-output nil
  1053.             "-c" (concat "exec ls "
  1054.                      switches " "
  1055.                      (file-name-nondirectory dirname))))))))
  1056.  
  1057. (defun save-buffers-kill-emacs (&optional arg)
  1058.   "Offer to save each buffer, then kill this Emacs fork.
  1059. With prefix arg, silently save all file-visiting buffers, then kill."
  1060.   (interactive "P")
  1061.   (save-some-buffers arg t)
  1062.   (and (or (not (memq t (mapcar (function
  1063.                   (lambda (buf) (and (buffer-file-name buf)
  1064.                              (buffer-modified-p buf))))
  1065.                 (buffer-list))))
  1066.        (yes-or-no-p "Modified buffers exist; exit anyway? "))
  1067.        (or (not (fboundp 'process-list))
  1068.        ;; process-list is not defined on VMS.
  1069.        (let ((processes (process-list))
  1070.          active)
  1071.          (while processes
  1072.            (and (memq (process-status (car processes)) '(run stop open))
  1073.             (let ((val (process-kill-without-query (car processes))))
  1074.               (process-kill-without-query (car processes) val)
  1075.               val)
  1076.             (setq active t))
  1077.            (setq processes (cdr processes)))
  1078.          (or (not active)
  1079.          (yes-or-no-p "Active processes exist; kill them and exit anyway? "))))
  1080.        (kill-emacs)))
  1081.  
  1082. (define-key ctl-x-map "\C-f" 'find-file)
  1083. (define-key ctl-x-map "\C-q" 'toggle-read-only)
  1084. (define-key ctl-x-map "\C-r" 'find-file-read-only)
  1085. (define-key ctl-x-map "\C-v" 'find-alternate-file)
  1086. (define-key ctl-x-map "\C-s" 'save-buffer)
  1087. (define-key ctl-x-map "s" 'save-some-buffers)
  1088. (define-key ctl-x-map "\C-w" 'write-file)
  1089. (define-key ctl-x-map "i" 'insert-file)
  1090. (define-key esc-map "~" 'not-modified)
  1091. (define-key ctl-x-map "\C-d" 'list-directory)
  1092. (define-key ctl-x-map "\C-c" 'save-buffers-kill-emacs)
  1093.  
  1094. (defvar ctl-x-4-map (make-keymap)
  1095.   "Keymap for subcommands of C-x 4")
  1096. (fset 'ctl-x-4-prefix ctl-x-4-map)
  1097. (define-key ctl-x-map "4" 'ctl-x-4-prefix)
  1098. (define-key ctl-x-4-map "f" 'find-file-other-window)
  1099. (define-key ctl-x-4-map "\C-f" 'find-file-other-window)
  1100. (define-key ctl-x-4-map "b" 'switch-to-buffer-other-window)
  1101.