• Home
  • History
  • Annotate
  • Raw
  • Download
  • only in /macosx-10.9.5/emacs-92/emacs/lisp/

Lines Matching +defs:dired +defs:rename +defs:file

1 ;;; files.el --- file input and output commands for Emacs
9 ;; This file is part of GNU Emacs.
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Defines most of Emacs's file- and directory-handling functions,
29 ;; including basic file visiting, backup generation, link handling,
41 (defgroup find-file nil
47 "Non-nil means delete auto-save file when a buffer is saved or killed.
49 Note that the auto-save file will not be deleted if the buffer is killed
56 "Alist of abbreviations for file directories.
59 done when setting up the default directory of a newly visited file.
73 :group 'find-file)
77 "Non-nil means make a backup of a file the first time it is saved.
78 This can be done by renaming the file or by copying.
80 Renaming means that Emacs renames the existing file so that it is a
81 backup file, then writes the buffer into a new file. Any other names
82 that the old file had will now refer to the backup file. The new file
85 Copying means that Emacs copies the existing file into the backup
86 file, then writes the buffer on top of the existing file. Any other
87 names that the old file had will now refer to the new (edited) file.
88 The file's owner and group are unchanged.
97 ;; Do this so that local variables based on the file name
121 when it would not result in changing the owner or group of the file;
123 the default for a new file created there by you.
131 when it would not result in changing the owner of the file or if the owner
141 "Predicate that looks at a file name and decides whether to make backups.
142 Called with an absolute file name as argument, it returns t to enable backup.")
146 Do so even if the buffer is not visiting a file.
152 (defcustom find-file-existing-other-name t
153 "Non-nil means find a file under alternative names, in existing buffers.
154 This means if any existing buffer is visiting the file you want
157 :group 'find-file)
159 (defcustom find-file-visit-truename nil
160 "*Non-nil means visit a file under its truename.
161 The truename of a file is found by chasing all links
162 both at the file level and at the levels of the containing directories."
164 :group 'find-file)
165 (put 'find-file-visit-truename 'safe-local-variable 'boolean)
170 If the file name matches one of these regular expressions,
171 then `revert-buffer' reverts the file without querying
172 if the file has changed on disk and you have not edited the buffer."
174 :group 'find-file)
176 (defvar buffer-file-number nil
177 "The device number and file number of the file visited in the current buffer.
179 This pair of numbers uniquely identifies the file.
180 If the buffer is visiting a new file, the value is nil.")
181 (make-variable-buffer-local 'buffer-file-number)
182 (put 'buffer-file-number 'permanent-local t)
184 (defvar buffer-file-numbers-unique (not (memq system-type '(windows-nt)))
185 "Non-nil means that `buffer-file-number' uniquely identifies files.")
187 (defvar buffer-file-read-only nil
188 "Non-nil if visited file was read-only when visited.")
189 (make-variable-buffer-local 'buffer-file-read-only)
191 (defcustom temporary-file-directory
192 (file-name-as-directory
203 (defcustom small-temporary-file-directory
206 If non-nil, this directory is used instead of `temporary-file-directory'
215 (defvar file-name-invalid-regexp
216 (cond ((and (eq system-type 'ms-dos) (not (msdos-long-file-names)))
226 "Regexp recognizing file names which aren't allowed by the filesystem.")
228 (defcustom file-precious-flag nil
232 This feature works by writing the new contents into a temporary file
233 and then renaming the temporary file to replace the original.
235 and there is never any instant where the file is nonexistent.
238 Yet, at the same time, saving a precious file
256 (defcustom dired-kept-versions 2
260 :group 'dired)
284 "Whether to add a newline automatically at the end of the file.
286 A value of t means do this only when the file is about to be saved.
287 A value of `visit' means do this right after the file is visited.
302 "Whether to add a newline at end of file, in certain major modes.
307 A value of t means do this only when the file is about to be saved.
308 A value of `visit' means do this right after the file is visited.
315 add a final newline, whenever you save a file that really needs one."
325 "Non-nil says by default do auto-saving of every file-visiting buffer."
329 (defcustom auto-save-visited-file-name nil
330 "Non-nil says auto-save a buffer in the file it is visiting, when practical.
335 (defcustom auto-save-file-name-transforms
337 ;; Don't put "\\2" inside expand-file-name, since it will be
339 ,(concat temporary-file-directory "\\2") t))
340 "Transforms to apply to buffer file name before making auto-save file name.
342 REGEXP is a regular expression to match against the file name.
345 If the optional element UNIQUIFY is non-nil, the auto-save file name is
346 constructed by taking the directory part of the replaced file-name,
347 concatenated with the buffer file name with all directory separators
355 The default value is set up to put the auto-save file into the
356 temporary directory (see the variable `temporary-file-directory') for
357 editing a remote file.
372 (defcustom find-file-run-dired t
373 "Non-nil means allow `find-file' to visit directories.
374 To visit the directory, `find-file' runs `find-directory-functions'."
376 :group 'find-file)
378 (defcustom find-directory-functions '(cvs-dired-noselect dired-noselect)
382 :type '(hook :options (cvs-dired-noselect dired-noselect))
383 :group 'find-file)
386 ;;;(put 'find-file-not-found-hooks 'permanent-local t)
387 (defvar find-file-not-found-functions nil
388 "List of functions to be called for `find-file' on nonexistent file.
390 Variable `buffer-file-name' is already set up.
392 (define-obsolete-variable-alias 'find-file-not-found-hooks
393 'find-file-not-found-functions "22.1")
396 ;;;(put 'find-file-hooks 'permanent-local t)
397 (defcustom find-file-hook nil
398 "List of functions to be called after a buffer is loaded from a file.
401 :group 'find-file
405 (define-obsolete-variable-alias 'find-file-hooks 'find-file-hook "22.1")
407 (defvar write-file-functions nil
408 "List of functions to be called before writing out a buffer to a file.
409 If one of them returns non-nil, the file is considered already written
411 These hooks are considered to pertain to the visited file.
413 the visited file name with \\[set-visited-file-name], but not when you
418 to how to save a buffer to file, for instance, choosing a suitable
422 (put 'write-file-functions 'permanent-local t)
423 (define-obsolete-variable-alias 'write-file-hooks 'write-file-functions "22.1")
425 (defvar local-write-file-hooks nil)
426 (make-variable-buffer-local 'local-write-file-hooks)
427 (put 'local-write-file-hooks 'permanent-local t)
428 (make-obsolete-variable 'local-write-file-hooks 'write-file-functions "22.1")
431 "List of functions to be called before writing out a buffer to a file.
432 If one of them returns non-nil, the file is considered already written
434 `write-file-functions'.
437 buffer's contents, not to the particular visited file; thus,
438 `set-visited-file-name' does not clear this variable; but changing the
441 For hooks that _do_ pertain to the particular visited file, use
442 `write-file-functions'. Both this variable and
443 `write-file-functions' relate to how a buffer is saved to file.
454 A value of t means file local variables specifications are obeyed
462 A value of nil means always ignore the file local variables.
472 always obeys file local variable specifications and the -*- line,
479 :group 'find-file)
485 variables list in the file will be ignored.
491 "Control processing of the \"variable\" `eval' in a file's local variables.
498 :group 'find-file)
505 (or (fboundp 'file-locked-p)
506 (defalias 'file-locked-p 'ignore))
512 you visit a file you cannot alter, and buffers you make read-only
517 (defvar file-name-history nil
518 "History list of file names entered in the minibuffer.")
527 (if (memq op '(file-name-completion file-name-all-completions))
529 (let ((inhibit-file-name-handlers
531 (and (eq inhibit-file-name-operation op)
532 inhibit-file-name-handlers)))
533 (inhibit-file-name-operation op))
537 "Convert a standard file's name to something suitable for the OS.
541 FILENAME should be an absolute file name since the conversion rules
542 sometimes vary depending on the position in the file name. E.g. c:/foo
543 is a valid DOS file name, but c:/bar/c:/foo is not.
565 Value is not expanded---you must call `expand-file-name' yourself.
580 (read-file-name prompt dir (or default-dirname
581 (if initial (expand-file-name initial dir)
584 'file-directory-p))
610 (substitute-in-file-name
611 (file-name-as-directory
617 "Change current directory to given absolute file name DIR."
619 ;; because otherwise expand-file-name may give some bad results.
621 (setq dir (file-name-as-directory dir)))
622 (setq dir (abbreviate-file-name (expand-file-name dir)))
623 (if (not (file-directory-p dir))
624 (if (file-exists-p dir)
627 (if (file-executable-p dir)
642 (if (file-name-absolute-p dir)
643 (cd-absolute (expand-file-name dir))
650 (let ((f (expand-file-name (concat x dir))))
651 (if (file-directory-p f)
659 (defun load-file (file)
660 "Load the Lisp file named FILE."
664 (read-file-name "Load file: "))))
665 (load (expand-file-name file) nil nil t))
667 (defun locate-file (filename path &optional suffixes predicate)
669 If found, return the absolute file name of FILENAME, with its suffixes;
674 file name when searching. If SUFFIXES is nil, it is equivalent to '(\"\").
676 If non-nil, PREDICATE is used instead of `file-readable-p'.
678 in which case file-name handlers are ignored. This usage is deprecated.
690 (locate-file-internal filename path suffixes predicate))
692 (defun locate-file-completion (string path-and-suffixes action)
693 "Do completion for file names passed to `locate-file'.
695 (if (file-name-absolute-p string)
696 (read-file-name-internal string nil action)
699 (string-dir (file-name-directory string)))
703 (if string-dir (setq dir (expand-file-name string-dir dir)))
704 (when (file-directory-p dir)
705 (dolist (file (file-name-all-completions
706 (file-name-nondirectory string) dir))
707 (add-to-list 'names (if string-dir (concat string-dir file) file))
708 (when (string-match suffix file)
709 (setq file (substring file 0 (match-beginning 0)))
710 (push (if string-dir (concat string-dir file) file) names)))))
717 "Search for COMMAND in `exec-path' and return the absolute file name.
719 ;; Use 1 rather than file-executable-p to better match the behavior of
721 (locate-file command exec-path exec-suffixes 1))
728 'locate-file-completion
732 (defun file-remote-p (file)
740 (let ((handler (find-file-name-handler file 'file-remote-p)))
742 (funcall handler 'file-remote-p file)
745 (defun file-local-copy (file)
746 "Copy the file FILE into a temporary file on this machine.
751 (let ((handler (find-file-name-handler file 'file-local-copy)))
753 (funcall handler 'file-local-copy file)
756 (defun file-truename (filename &optional counter prev-dirs)
758 The truename of a file name is found by chasing symbolic links
759 both at the level of the file and at the level of the directories
769 (setq filename (expand-file-name filename))
777 (setq filename (concat (expand-file-name first-part) rest)))))
784 (file-name-handler-alist
785 (if prev-dirs file-name-handler-alist
786 (let ((tem (copy-sequence file-name-handler-alist)))
791 ;; invoking the file-name handler) currently applies on Windows
796 ;; It seems appropriate for file-truename to resolve these issues in
798 ;; `w32-long-file-name' - this returns the exact name of a file as
802 (let ((handler (find-file-name-handler filename 'file-truename)))
803 ;; For file name that has a special handler, call handler.
806 (setq filename (funcall handler 'file-truename filename))
810 (setq filename (or (w32-long-file-name filename) filename))))
813 ;; If this file directly leads to a link, process that iteratively
819 (let ((handler (find-file-name-handler filename 'file-truename)))
820 ;; For file name that has a special handler, call handler.
823 (setq filename (funcall handler 'file-truename filename)
825 (let ((dir (or (file-name-directory filename) default-directory))
828 (setq dirfile (directory-file-name dir))
836 (new (file-name-as-directory (file-truename dirfile counter prev-dirs))))
839 (if (equal ".." (file-name-nondirectory filename))
841 (directory-file-name (file-name-directory (directory-file-name dir)))
843 (if (equal "." (file-name-nondirectory filename))
844 (setq filename (directory-file-name dir)
846 ;; Put it back on the file name.
847 (setq filename (concat dir (file-name-nondirectory filename)))
848 ;; Is the file name the name of a link?
849 (setq target (file-symlink-p filename))
853 ;; We can't safely use expand-file-name here
857 (if (file-name-absolute-p target)
865 (defun file-chase-links (filename &optional limit)
867 Unlike `file-truename', this does not check whether a parent
874 (setq tem (file-symlink-p newname)))
887 (setq newname (expand-file-name newname))
890 (file-chase-links
891 (directory-file-name (file-name-directory newname))))
893 (setq newname (file-name-directory newname)))
894 (setq newname (expand-file-name tem (file-name-directory newname)))
898 (defun make-temp-file (prefix &optional dir-flag suffix)
899 "Create a temporary file.
900 The returned file name (created by appending some random characters at the end
901 of PREFIX, and expanding against `temporary-file-directory' if necessary),
902 is guaranteed to point to a newly created empty file.
903 You can then use `write-region' to write new data into the file.
905 If DIR-FLAG is non-nil, create a new empty directory instead of a file.
907 If SUFFIX is non-nil, add that at the end of the file name."
908 (let ((umask (default-file-modes))
909 file)
915 (set-default-file-modes ?\700)
918 (setq file
920 (expand-file-name prefix temporary-file-directory)))
922 (setq file (concat file suffix)))
924 (make-directory file)
925 (write-region "" nil file nil 'silent nil 'excl))
927 (file-already-exists t))
928 ;; the file was somehow created by someone else between
931 file)
933 (set-default-file-modes umask))))
935 (defun recode-file-name (file coding new-coding &optional ok-if-already-exists)
938 Signals a `file-already-exists' error if a file of the new name
944 (let ((default-coding (or file-name-coding-system
945 default-file-name-coding-system))
946 (filename (read-file-name "Recode filename: " nil nil t))
974 (let* ((default-coding (or file-name-coding-system
975 default-file-name-coding-system))
977 (encoded (encode-coding-string file default-coding))
981 (file-name-coding-system nil)
982 (default-file-name-coding-system nil)
984 (rename-file encoded new-encoded ok-if-already-exists)
1035 (defvar find-file-default nil
1036 "Used within `find-file-read-args'.")
1057 (defun find-file-read-args (prompt mustmatch)
1058 (list (let ((find-file-default
1059 (and buffer-file-name
1060 (abbreviate-file-name buffer-file-name))))
1062 (lambda () (setq minibuffer-default find-file-default))
1063 (read-file-name prompt nil default-directory mustmatch)))
1066 (defun find-file (filename &optional wildcards)
1067 "Edit file FILENAME.
1068 Switch to a buffer visiting file FILENAME,
1071 but the visited file name is available through the minibuffer history:
1076 suppress wildcard expansion by setting `find-file-wildcards' to nil.
1078 To visit a file without any kind of conversion and without
1079 automatically choosing a major mode, use \\[find-file-literally]."
1080 (interactive (find-file-read-args "Find file: " nil))
1081 (let ((value (find-file-noselect filename nil nil wildcards)))
1086 (defun find-file-other-window (filename &optional wildcards)
1087 "Edit file FILENAME, in another window.
1092 but the visited file name is available through the minibuffer history:
1097 (interactive (find-file-read-args "Find file in other window: " nil))
1098 (let ((value (find-file-noselect filename nil nil wildcards)))
1106 (defun find-file-other-frame (filename &optional wildcards)
1107 "Edit file FILENAME, in another frame.
1112 but the visited file name is available through the minibuffer history:
1117 (interactive (find-file-read-args "Find file in other frame: " nil))
1118 (let ((value (find-file-noselect filename nil nil wildcards)))
1126 (defun find-file-existing (filename)
1127 "Edit the existing file FILENAME.
1128 Like \\[find-file] but only allow a file that exists, and do not allow
1129 file names with wildcards."
1130 (interactive (nbutlast (find-file-read-args "Find existing file: " t)))
1131 (if (and (not (interactive-p)) (not (file-exists-p filename)))
1133 (find-file filename)
1136 (defun find-file-read-only (filename &optional wildcards)
1137 "Edit file FILENAME but don't allow changes.
1138 Like \\[find-file] but marks buffer as read-only.
1140 (interactive (find-file-read-args "Find file read-only: " nil))
1141 (unless (or (and wildcards find-file-wildcards
1144 (file-exists-p filename))
1146 (let ((value (find-file filename wildcards)))
1151 (defun find-file-read-only-other-window (filename &optional wildcards)
1152 "Edit file FILENAME in another window but don't allow changes.
1153 Like \\[find-file-other-window] but marks buffer as read-only.
1155 (interactive (find-file-read-args "Find file read-only other window: " nil))
1156 (unless (or (and wildcards find-file-wildcards
1159 (file-exists-p filename))
1161 (let ((value (find-file-other-window filename wildcards)))
1166 (defun find-file-read-only-other-frame (filename &optional wildcards)
1167 "Edit file FILENAME in another frame but don't allow changes.
1168 Like \\[find-file-other-frame] but marks buffer as read-only.
1170 (interactive (find-file-read-args "Find file read-only other frame: " nil))
1171 (unless (or (and wildcards find-file-wildcards
1174 (file-exists-p filename))
1176 (let ((value (find-file-other-frame filename wildcards)))
1181 (defun find-alternate-file-other-window (filename &optional wildcards)
1182 "Find file FILENAME as a replacement for the file in the next window.
1186 expand wildcards (if any) and replace the file with multiple files."
1190 (let ((file buffer-file-name)
1191 (file-name nil)
1192 (file-dir nil))
1193 (and file
1194 (setq file-name (file-name-nondirectory file)
1195 file-dir (file-name-directory file)))
1196 (list (read-file-name
1197 "Find alternate file: " file-dir nil nil file-name)
1200 (find-file-other-window filename wildcards)
1203 (find-alternate-file filename wildcards))))
1205 (defun find-alternate-file (filename &optional wildcards)
1206 "Find file FILENAME, select its buffer, kill previous buffer.
1207 If the current buffer now contains an empty file that you just visited
1208 \(presumably by mistake), use this command to visit the file you really want.
1211 expand wildcards (if any) and replace the file with multiple files.
1217 (let ((file buffer-file-name)
1218 (file-name nil)
1219 (file-dir nil))
1220 (and file
1221 (setq file-name (file-name-nondirectory file)
1222 file-dir (file-name-directory file)))
1223 (list (read-file-name
1224 "Find alternate file: " file-dir nil nil file-name)
1228 (when (and (buffer-modified-p) (buffer-file-name))
1235 (ofile buffer-file-name)
1236 (onum buffer-file-number)
1237 (odir dired-directory)
1238 (otrue buffer-file-truename)
1242 (rename-buffer " **lose**")
1247 ;; if we specified the same file again.
1248 (setq buffer-file-name nil)
1249 (setq buffer-file-number nil)
1250 (setq buffer-file-truename nil)
1251 ;; Likewise for dired buffers.
1252 (setq dired-directory nil)
1253 (find-file filename wildcards))
1255 ;; This executes if find-file gets an error
1258 ;; If find-file actually finds something, we kill obuf below.
1259 (setq buffer-file-name ofile)
1260 (setq buffer-file-number onum)
1261 (setq buffer-file-truename otrue)
1262 (setq dired-directory odir)
1264 (rename-buffer oname)))
1272 (defun create-file-buffer (filename)
1276 (let ((lastname (file-name-nondirectory filename)))
1294 (defun abbreviate-file-name (filename)
1303 (file-exists-p (file-name-directory
1322 (concat "^" (abbreviate-file-name (expand-file-name "~"))
1344 (defcustom find-file-not-true-dirname-list nil
1346 On VMS, when you visit a file using a logical name that searches a path,
1347 you may or may not want the visited file name to record the specific
1348 directory where the file was found. If you *do not* want that, add the logical
1351 :group 'find-file)
1354 "Return the buffer visiting file FILENAME (a string).
1355 This is like `get-file-buffer', except that it checks for any buffer
1356 visiting the same file, possibly under a different name.
1361 (truename (abbreviate-file-name (file-truename filename))))
1362 (or (let ((buf (get-file-buffer filename)))
1368 (if (and buffer-file-name
1369 (string= buffer-file-truename truename)
1374 (let* ((attributes (file-attributes truename))
1377 (and buffer-file-numbers-unique
1381 (if (and buffer-file-name
1382 (equal buffer-file-number number)
1383 ;; Verify this buffer's file number
1384 ;; still belongs to its file.
1385 (file-exists-p buffer-file-name)
1386 (equal (file-attributes buffer-file-truename)
1394 (defcustom find-file-wildcards t
1395 "Non-nil means file-visiting commands should handle wildcards.
1402 (defcustom find-file-suppress-same-file-warnings nil
1404 When nil, Emacs prints a warning when visiting a file that is already
1411 (defcustom large-file-warning-threshold 10000000
1412 "Maximum size of file above which a confirmation is requested.
1415 :group 'find-file
1419 (defun find-file-noselect (filename &optional nowarn rawfile wildcards)
1420 "Read file FILENAME into a buffer and return the buffer.
1422 verify that the file has not changed since visited or saved.
1425 Optional third arg RAWFILE non-nil means the file is read literally.
1431 (abbreviate-file-name
1432 (expand-file-name filename)))
1433 (if (file-directory-p filename)
1434 (or (and find-file-run-dired
1437 (if find-file-visit-truename
1438 (abbreviate-file-name (file-truename filename))
1442 find-file-wildcards
1446 (file-expand-wildcards filename t)
1448 (find-file-wildcards nil))
1450 (find-file-noselect filename)
1451 (mapcar #'find-file-noselect files)))
1452 (let* ((buf (get-file-buffer filename))
1453 (truename (abbreviate-file-name (file-truename filename)))
1454 (attributes (file-attributes truename))
1456 ;; Find any buffer for a file which has same truename.
1462 find-file-suppress-same-file-warnings
1463 (string-equal filename (buffer-file-name other))
1464 (message "%s and %s are the same file"
1465 filename (buffer-file-name other)))
1467 (if (or find-file-existing-other-name find-file-visit-truename)
1469 ;; Check to see if the file looks uncommonly large.
1470 (when (and large-file-warning-threshold (nth 7 attributes)
1471 ;; Don't ask again if we already have the file or
1474 (> (nth 7 attributes) large-file-warning-threshold)
1477 (file-name-nondirectory filename)
1484 (verify-visited-file-modtime buf)
1485 (cond ((not (file-exists-p filename))
1499 (message "Reverting file %s..." filename)
1501 (message "Reverting file %s...done" filename)))
1503 (if (string= (file-name-nondirectory filename)
1509 (file-name-nondirectory filename))
1514 (file-name-nondirectory filename)
1520 ;; Check if a formerly read-only file has become
1522 ;; with the new state of the file, that is ok too.
1523 (let ((read-only (not (file-writable-p buffer-file-name))))
1525 (eq read-only buffer-file-read-only)
1530 buffer-file-name
1534 (setq buffer-file-read-only read-only))
1537 (not (null find-file-literally))))
1540 ;; non-literally if they have the file in
1547 "The file %s is already visited normally,
1550 Emacs can only visit a file in one way at a time.
1552 Do you want to save the file, and visit it literally instead? "
1553 "The file %s is already visited literally,
1555 You have edited the buffer. Now you have asked to visit the file normally,
1556 but Emacs can only visit a file in one way at a time.
1558 Do you want to save the file, and visit it normally instead? ")
1559 (file-name-nondirectory filename)))
1562 (find-file-noselect-1 buf filename nowarn
1568 Do you want to discard your changes, and visit the file literally now? "
1570 Do you want to discard your changes, and visit the file normally now? ")))
1571 (find-file-noselect-1 buf filename nowarn
1578 "The file %s is already visited normally.
1581 But Emacs can only visit a file in one way at a time.
1583 Do you want to revisit the file literally now? "
1584 "The file %s is already visited literally,
1587 but Emacs can only visit a file in one way at a time.
1589 Do you want to revisit the file normally now? ")
1590 (file-name-nondirectory filename)))
1591 (find-file-noselect-1 buf filename nowarn
1598 (setq buf (create-file-buffer filename))
1599 ;; find-file-noselect-1 may use a different buffer.
1600 (find-file-noselect-1 buf filename nowarn
1603 (defun find-file-noselect-1 (buf filename nowarn rawfile truename number)
1606 (kill-local-variable 'find-file-literally)
1607 ;; Needed in case we are re-visiting the file with a different
1609 (kill-local-variable 'buffer-file-coding-system)
1619 (insert-file-contents-literally filename t))
1620 (file-error
1621 (when (and (file-exists-p filename)
1622 (not (file-readable-p filename)))
1624 (signal 'file-error (list "File is not readable"
1630 (insert-file-contents filename t))
1631 (file-error
1632 (when (and (file-exists-p filename)
1633 (not (file-readable-p filename)))
1635 (signal 'file-error (list "File is not readable"
1637 ;; Run find-file-not-found-functions until one returns non-nil.
1638 (or (run-hook-with-args-until-success 'find-file-not-found-functions)
1641 ;; Record the file's truename, and maybe use that as visited name.
1642 (if (equal filename buffer-file-name)
1643 (setq buffer-file-truename truename)
1644 (setq buffer-file-truename
1645 (abbreviate-file-name (file-truename buffer-file-name))))
1646 (setq buffer-file-number number)
1648 ;; the file was found in.
1651 (if (string-match ":" (file-name-directory filename))
1652 (setq logical (substring (file-name-directory filename)
1654 (not (member logical find-file-not-true-dirname-list)))
1655 (setq buffer-file-name buffer-file-truename))
1656 (if find-file-visit-truename
1657 (setq buffer-file-name (expand-file-name buffer-file-truename)))
1658 ;; Set buffer's default directory to that of the file.
1659 (setq default-directory (file-name-directory buffer-file-name))
1660 ;; Turn off backup files for certain file names. Since
1663 (not (funcall backup-enable-predicate buffer-file-name))
1670 (setq buffer-file-coding-system 'no-conversion)
1672 (make-local-variable 'find-file-literally)
1673 (setq find-file-literally t))
1674 (after-find-file error (not nowarn)))
1678 (defun insert-file-contents-literally (filename &optional visit beg end replace)
1679 "Like `insert-file-contents', but only reads in the file literally.
1682 conversion, `find-file-hook', automatic uncompression, etc.
1686 (after-insert-file-functions nil)
1689 (find-buffer-file-type-function
1690 (if (fboundp 'find-buffer-file-type)
1691 (symbol-function 'find-buffer-file-type)
1693 (inhibit-file-name-handlers
1694 (append '(jka-compr-handler image-file-handler)
1695 inhibit-file-name-handlers))
1696 (inhibit-file-name-operation 'insert-file-contents))
1699 (fset 'find-buffer-file-type (lambda (filename) t))
1700 (insert-file-contents filename visit beg end replace))
1701 (if find-buffer-file-type-function
1702 (fset 'find-buffer-file-type find-buffer-file-type-function)
1703 (fmakunbound 'find-buffer-file-type)))))
1705 (defun insert-file-1 (filename insert-func)
1706 (if (file-directory-p filename)
1707 (signal 'file-error (list "Opening input file" "file is a directory"
1709 (let* ((buffer (find-buffer-visiting (abbreviate-file-name (file-truename filename))
1717 (defun insert-file-literally (filename)
1718 "Insert contents of file FILENAME into buffer after point with no conversion.
1721 Don't call it from programs! Use `insert-file-contents-literally' instead.
1723 (interactive "*fInsert file literally: ")
1724 (insert-file-1 filename #'insert-file-contents-literally))
1726 (defvar find-file-literally nil
1727 "Non-nil if this buffer was made by `find-file-literally' or equivalent.
1729 (put 'find-file-literally 'permanent-local t)
1731 (defun find-file-literally (filename)
1732 "Visit file FILENAME with no conversion of any kind.
1735 The major mode used is Fundamental mode regardless of the file name,
1736 and local variable specifications in the file are ignored.
1738 file due to `require-final-newline' is also disabled.
1741 visiting the file literally. If Emacs already has a buffer
1742 which is visiting the file, you get the existing buffer,
1745 In a Lisp program, if you want to be sure of accessing a file's
1747 the file contents into it using `insert-file-contents-literally'."
1748 (interactive "FFind file literally: ")
1749 (switch-to-buffer (find-file-noselect filename nil t)))
1752 (defvar after-find-file-from-revert-buffer nil)
1754 (defun after-find-file (&optional error warn noauto
1755 after-find-file-from-revert-buffer
1757 "Called after finding a file and by the default revert function.
1760 error in reading the file. WARN non-nil means warn if there
1761 exists an auto-save file more recent than the visited file.
1765 Fifth arg NOMODES non-nil means don't alter the file's modes.
1766 Finishes by calling the functions in `find-file-hook'
1768 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
1775 ((and error (file-attributes buffer-file-name))
1781 ;; under the name of the visited file.
1782 (not (and buffer-file-name
1783 auto-save-visited-file-name))
1784 (file-newer-than-file-p (or buffer-auto-save-file-name
1785 (make-auto-save-file-name))
1786 buffer-file-name))
1787 (format "%s has auto save data; consider M-x recover-this-file"
1788 (file-name-nondirectory buffer-file-name))
1790 (if error "(New file)" nil)))
1793 "Note: file is write protected")
1794 ((file-attributes (directory-file-name default-directory))
1796 ((file-exists-p (file-name-directory buffer-file-name))
1807 ;; before altering a backup file.
1808 (when (backup-file-name-p buffer-file-name)
1810 ;; When a file is marked read-only,
1812 (when (and (file-modes (buffer-file-name))
1813 (zerop (logand (file-modes (buffer-file-name)) #o222)))
1819 ;; If requested, add a newline at the end of the file.
1832 (run-hooks 'find-file-hook)))
1847 (defun normal-mode (&optional find-file)
1849 Also sets up any specified local variables of the file.
1850 Uses the visited file name, the -*- line, and the local variables spec.
1852 This function is called automatically from `find-file'. In that case,
1853 we may set up the file-specified mode and local variables,
1863 (let ((enable-local-variables (or (not find-file) enable-local-variables)))
1869 ;; whatever file local variables are relevant to it.
1885 On systems with case-insensitive file names, this variable is ignored,
1894 ;; directives in that file. That way is discouraged since it
1967 ;; The list of archive file extensions should be in sync with
2031 ;; This is for files saved by cvs-merge that look like .#<file>.<rev>
2032 ;; or .#<file>.<rev>-<rev> or VC's <file>.~<rev>~.
2038 ;; and after the .scm.[0-9] and CVS' <file>.<rev> patterns too.
2044 Visiting a file whose name matches REGEXP specifies FUNCTION as the
2051 If the file name matches `inhibit-first-line-modes-regexps',
2059 and `magic-mode-alist', which determines modes based on file contents.")
2064 ;; file. That way is discouraged since it spreads out the
2112 "List of regexps; if one matches a file name, don't look for `-*-'.")
2117 from the end of the file name anything that matches one of these regexps.")
2122 "Regexp matching interpreters, for file mode determination.
2123 This regular expression is matched against the first line of a file
2124 to determine the file's mode in `set-auto-mode'. If it matches, the file
2133 After visiting a file, if REGEXP matches the text at the beginning of the
2164 After visiting a file, if REGEXP matches the text at the beginning of the
2167 have not specified a mode for this file.
2186 Local Variables section of the file; for that, use `hack-local-variables'.
2242 done (assoc (file-name-nondirectory mode)
2263 (if buffer-file-name
2264 (let ((name buffer-file-name))
2265 ;; Remove backup-suffixes from file name.
2266 (setq name (file-name-sans-versions name))
2311 ;; set-visited-file-name. In that case, if the major mode specified is the
2334 ;; Don't look for -*- if this file name matches any
2337 (name (if buffer-file-name
2338 (file-name-sans-versions buffer-file-name)
2351 ;; If the file begins with "#!"
2356 ;; such a file without screwing up
2371 ;;; Handling file local variables
2375 "Variables to be ignored in a file's local variable spec.")
2378 "Normal hook run after processing a file's local variables specs.
2386 :group 'find-file
2394 :group 'find-file
2402 buffer-auto-save-file-name
2403 buffer-file-name
2404 buffer-file-truename
2416 file-name-handler-alist
2447 rmail-output-file-alist
2460 ;; the major mode's file, since that will be loaded before file variables are
2464 ;; file defining the minor mode after the defcustom/defvar using an autoload
2469 ;; Otherwise, when Emacs visits a file specifying that local variable, the
2470 ;; minor mode file may not be loaded yet.
2490 (let ((name (if buffer-file-name
2491 (file-name-nondirectory buffer-file-name)
2562 ;; When this is called from desktop-restore-file-buffer,
2565 (if (or custom-file user-init-file)
2763 "Non-nil if SYM is safe as a file-local variable with value VAL.
2776 "Non-nil if SYM could be dangerous as a file-local variable.
2805 "Return t if it is safe to eval EXP when it is found in a file."
2866 (defcustom change-major-mode-with-file-name t
2867 "Non-nil means \\[write-file] should set the major mode from the file name.
2872 \(3) the new file name does not particularly specify any mode."
2876 (defun set-visited-file-name (filename &optional no-query along-with-file)
2877 "Change name of file visited in current buffer to FILENAME.
2878 This also renames the buffer to correspond to the new file.
2879 The next time the buffer is saved it will go in the newly specified file.
2880 FILENAME nil or an empty string means mark buffer as not visiting any file.
2888 the old visited file has been renamed to the new name FILENAME."
2889 (interactive "FSet visited file name: ")
2891 (error "An indirect buffer cannot visit a file"))
2897 (expand-file-name filename))))
2900 (setq truename (file-truename filename))
2901 (if find-file-visit-truename
2904 (let ((new-name (file-name-nondirectory filename)))
2906 (error "Empty file name"))))
2913 (or (equal filename buffer-file-name)
2917 (setq buffer-file-name filename)
2919 (let ((new-name (file-name-nondirectory buffer-file-name)))
2922 (setq default-directory (file-name-directory buffer-file-name))
2924 ;; and it's considered as a feature in rename-buffer.
2926 (rename-buffer new-name t))))
2928 (or along-with-file
2929 (clear-visited-file-modtime))
2930 ;; Abbreviate the file names of the buffer.
2933 (setq buffer-file-truename (abbreviate-file-name truename))
2934 (if find-file-visit-truename
2935 (setq buffer-file-name truename))))
2936 (setq buffer-file-number
2938 (nthcdr 10 (file-attributes buffer-file-name))
2940 ;; write-file-functions is normally used for things like ftp-find-file
2942 ;; Changing to visit an ordinary local file instead should flush the hook.
2943 (kill-local-variable 'write-file-functions)
2944 (kill-local-variable 'local-write-file-hooks)
2952 ;; Turn off backup files for certain file names.
2954 (and buffer-file-name
2956 (not (funcall backup-enable-predicate buffer-file-name))
2960 (let ((oauto buffer-auto-save-file-name))
2962 (if (not buffer-auto-save-file-name)
2963 (and buffer-file-name auto-save-default
2966 ;; We deliberately don't rename or delete the old auto save
2967 ;; for the old visited file name. This is because perhaps
2969 ;; previous state from the auto save file.
2970 (setq buffer-auto-save-file-name
2971 (make-auto-save-file-name)))
2972 ;; Rename the old auto save file if any.
2973 (and oauto buffer-auto-save-file-name
2974 (file-exists-p oauto)
2975 (rename-file oauto buffer-auto-save-file-name t)))
2976 (and buffer-file-name
2977 (not along-with-file)
2979 ;; Update the major mode, if the file name determines it.
2982 (or (not change-major-mode-with-file-name)
2989 (defun write-file (filename &optional confirm)
2990 "Write current buffer into file FILENAME.
2991 This makes the buffer visit that file, and marks it as not modified.
2994 the default file name but in that directory. You can also yank
2995 the default file name into the minibuffer to edit it, using \\<minibuffer-local-map>\\[next-history-element].
2997 If the buffer is not already visiting a file, the default file name
2998 for the output file is the buffer name.
3001 asks for confirmation before overwriting an existing file.
3003 ;; (interactive "FWrite file: ")
3005 (list (if buffer-file-name
3006 (read-file-name "Write file: "
3008 (read-file-name "Write file: " default-directory
3009 (expand-file-name
3010 (file-name-nondirectory (buffer-name))
3017 ;; use the default file name, but in that directory.
3018 (if (file-directory-p filename)
3019 (setq filename (concat (file-name-as-directory filename)
3020 (file-name-nondirectory
3021 (or buffer-file-name (buffer-name))))))
3023 (file-exists-p filename)
3026 (set-visited-file-name filename (not confirm))))
3028 ;; Make buffer writable if file is writable.
3029 (and buffer-file-name
3030 (file-writable-p buffer-file-name)
3035 (vc-find-file-hook))
3039 "Make a backup of the disk file visited by the current buffer, if appropriate.
3043 variable `make-backup-files'. If it's done by renaming, then the file is
3048 MODES is the result of `file-modes' on the original
3049 file; this means that the caller, after saving the buffer, should change
3050 the modes of the new file to agree with the old modes.
3051 BACKUPNAME is the backup file name, which is the old file renamed."
3054 (file-exists-p buffer-file-name)
3055 (memq (aref (elt (file-attributes buffer-file-name) 8) 0)
3057 (let ((real-file-name buffer-file-name)
3060 ;; Thus we make the backups in the directory where the real file is.
3061 (setq real-file-name (file-chase-links real-file-name))
3062 (setq backup-info (find-backup-file-name real-file-name)
3065 ;; (if (file-directory-p buffer-file-name)
3066 ;; (error "Cannot save buffer in directory %s" buffer-file-name))
3077 real-file-name)))))
3078 (modes (file-modes buffer-file-name)))
3079 ;; Actually write the back up file.
3081 (if (or file-precious-flag
3082 ; (file-symlink-p buffer-file-name)
3084 ;; Don't rename a suid or sgid file.
3086 (not (file-writable-p (file-name-directory real-file-name)))
3088 (> (file-nlinks real-file-name) 1))
3091 (let ((attr (file-attributes real-file-name)))
3097 (not (file-ownership-preserved-p real-file-name)))))))
3098 (backup-buffer-copy real-file-name backupname modes)
3099 ;; rename-file should delete old backup.
3100 (rename-file real-file-name backupname t)
3102 (file-error
3104 (setq backupname (expand-file-name
3107 (message "Cannot write backup file; backing up in %s"
3110 (backup-buffer-copy real-file-name backupname modes)))
3116 (delete-file (car targets))
3117 (file-error nil))
3120 (file-error nil))))))
3123 (let ((umask (default-file-modes)))
3129 (set-default-file-modes ?\700)
3133 (delete-file to-name)
3134 (file-error nil))
3135 (copy-file from-name to-name nil t)
3137 (file-already-exists t))
3138 ;; The file was somehow created by someone else between
3139 ;; `delete-file' and `copy-file', so let's try again.
3142 (set-default-file-modes umask)))
3144 (set-file-modes to-name (logand modes #o1777))))
3146 (defun file-name-sans-versions (name &optional keep-backup-version)
3147 "Return file NAME sans backup versions or strings.
3148 This is a separate procedure so your site-init or startup file can
3151 we do not remove backup version numbers, only true file version numbers."
3152 (let ((handler (find-file-name-handler name 'file-name-sans-versions)))
3154 (funcall handler 'file-name-sans-versions name keep-backup-version)
3161 ;; device/directory part of the file name.
3173 (defun file-ownership-preserved-p (file)
3175 (let ((handler (find-file-name-handler file 'file-ownership-preserved-p)))
3177 (funcall handler 'file-ownership-preserved-p file)
3178 (let ((attributes (file-attributes file)))
3179 ;; Return t if the file doesn't exist, since it's true that no
3184 (defun file-name-sans-extension (filename)
3186 The extension, in a file name, is the part that follows the last `.',
3189 (let ((file (file-name-sans-versions (file-name-nondirectory filename)))
3191 (if (and (string-match "\\.[^.]*\\'" file)
3193 (if (setq directory (file-name-directory filename))
3194 ;; Don't use expand-file-name here; if DIRECTORY is relative,
3196 (concat directory (substring file 0 (match-beginning 0)))
3197 (substring file 0 (match-beginning 0)))
3200 (defun file-name-extension (filename &optional period)
3202 The extension, in a file name, is the part that follows the last `.',
3205 Return nil for extensionless file names such as `foo'.
3206 Return the empty string for file names such as `foo.'.
3212 (let ((file (file-name-sans-versions (file-name-nondirectory filename))))
3213 (if (and (string-match "\\.[^.]*\\'" file)
3215 (substring file (+ (match-beginning 0) (if period 0 1)))
3219 (defcustom make-backup-file-name-function nil
3220 "A function to use instead of the default `make-backup-file-name'.
3221 A value of nil gives the default `make-backup-file-name' behavior.
3224 files. If you define it, you may need to change `backup-file-name-p'
3225 and `file-name-sans-versions' too.
3237 are backed up into the same directory, the file names in this
3238 directory will be the full name of the file backed up with all
3247 is made in the original file's directory.
3257 Checks for files in `temporary-file-directory',
3258 `small-temporary-file-directory', and /tmp."
3259 (not (or (let ((comp (compare-strings temporary-file-directory 0 nil
3261 ;; Directory is under temporary-file-directory.
3263 (< comp (- (length temporary-file-directory)))))
3269 (if small-temporary-file-directory
3270 (let ((comp (compare-strings small-temporary-file-directory
3273 ;; Directory is under small-temporary-file-directory.
3275 (< comp (- (length small-temporary-file-directory)))))))))
3277 (defun make-backup-file-name (file)
3278 "Create the non-numeric backup file name for FILE.
3279 Normally this will just be the file's name with `~' appended.
3282 If the variable `make-backup-file-name-function' is non-nil, its value
3289 (if make-backup-file-name-function
3290 (funcall make-backup-file-name-function file)
3292 (not (msdos-long-file-names)))
3293 (let ((fn (file-name-nondirectory file)))
3294 (concat (file-name-directory file)
3299 (concat (make-backup-file-name-1 file) "~"))))
3301 (defun make-backup-file-name-1 (file)
3302 "Subroutine of `make-backup-file-name' and `find-backup-file-name'."
3307 (if (string-match (car elt) file)
3311 ;; file's directory. By expanding explicitly here, we avoid
3315 (expand-file-name backup-directory
3316 (file-name-directory file))))
3317 (if (and abs-backup-directory (not (file-exists-p abs-backup-directory)))
3320 (file-error (setq backup-directory nil
3323 file
3324 (if (file-name-absolute-p backup-directory)
3327 ;; Normalize DOSish file names: downcase the drive
3330 (or (file-name-absolute-p file)
3331 (setq file (expand-file-name file))) ; make defaults explicit
3332 ;; Replace any invalid file-name characters (for the
3334 (setq file (expand-file-name (convert-standard-filename file)))
3335 (if (eq (aref file 1) ?:)
3336 (setq file (concat "/"
3338 (char-to-string (downcase (aref file 0)))
3339 (if (eq (aref file 2) ?/)
3342 (substring file 2)))))
3346 (expand-file-name
3349 (replace-regexp-in-string "!" "!!" file))
3351 (expand-file-name (file-name-nondirectory file)
3352 (file-name-as-directory abs-backup-directory))))))
3354 (defun backup-file-name-p (file)
3355 "Return non-nil if FILE is a backup file name (numeric or not).
3357 You may need to redefine `file-name-sans-versions' as well."
3358 (string-match "~\\'" file))
3366 "Given the name of a numeric backup file, FN, return the backup number.
3376 (defun find-backup-file-name (fn)
3377 "Find a file name for a backup file FN, and suggestions for deletions.
3378 Value is a list whose car is the name for the backup file
3382 `make-backup-file-name'."
3383 (let ((handler (find-file-name-handler fn 'find-backup-file-name)))
3386 (funcall handler 'find-backup-file-name fn)
3389 ;; when long file names are unavailable.
3391 (not (msdos-long-file-names))))
3392 (list (make-backup-file-name fn))
3393 (let* ((basic-name (make-backup-file-name-1 fn))
3394 (base-versions (concat (file-name-nondirectory basic-name)
3401 (setq possibilities (file-name-all-completions
3403 (file-name-directory basic-name))
3414 (file-error (setq possibilities nil)))
3416 (list (make-backup-file-name fn))
3428 (defun file-nlinks (filename)
3429 "Return number of names file FILENAME has."
3430 (car (cdr (file-attributes filename))))
3432 ;; (defun file-relative-name (filename &optional directory)
3434 ;; This function returns a relative file name which is equivalent to FILENAME
3437 ;; when the file name and directory use different drive names)
3440 ;; (let ((fname (expand-file-name filename)))
3441 ;; (setq directory (file-name-as-directory
3442 ;; (expand-file-name (or directory default-directory))))
3452 ;; (fname-dir (file-name-as-directory fname)))
3455 ;; (setq directory (file-name-directory (substring directory 0 -1))
3468 ;; (concat (file-name-as-directory ancestor) rest)))
3472 (defun file-relative-name (filename &optional directory)
3474 This function returns a relative file name which is equivalent to FILENAME
3480 (file-name-as-directory (expand-file-name (or directory
3482 (setq filename (expand-file-name filename))
3483 (let ((fremote (file-remote-p filename))
3484 (dremote (file-remote-p directory)))
3492 ;; Test for different remote file system identification
3496 (filename-dir (file-name-as-directory filename)))
3503 (setq directory (file-name-directory (substring directory 0 -1))
3516 (concat (file-name-as-directory ancestor) rest)))
3522 "Save current buffer in visited file if modified.
3525 By default, makes the previous version into a backup file
3530 unconditionally makes the previous version into a backup file.
3533 and unconditionally makes the previous version into a backup file.
3536 into a backup file.
3538 If a file's name is FOO, the names of its numbered backup versions are
3539 FOO.~i~ for various integers i. A non-numbered backup file is called FOO~.
3542 numeric versions of the file being backed up, or `version-control' is
3548 `dired-kept-versions' controls dired's clean-directory (.) command.
3563 (if (and modp large (buffer-file-name))
3564 (message "Saving file %s..." (buffer-file-name)))
3568 (defun delete-auto-save-file-if-necessary (&optional force)
3569 "Delete auto-save file for current buffer if `delete-auto-save-files' is t.
3570 Normally delete only if the file was written by this Emacs since
3572 (and buffer-auto-save-file-name delete-auto-save-files
3573 (not (string= buffer-file-name buffer-auto-save-file-name))
3577 (delete-file buffer-auto-save-file-name)
3578 (file-error nil))
3585 "Normal hook that is run before a buffer is saved to its file."
3591 "Normal hook that is run after a buffer is saved to its file."
3592 :options '(executable-make-buffer-file-executable-if-script-p)
3599 value of `buffer-file-coding-system', when saving the buffer.
3601 will still use `buffer-file-coding-system'; this variable has no effect
3608 "Save the current buffer in its visited file, if it has been modified.
3609 The hooks `write-contents-functions' and `write-file-functions' get a chance
3611 the visited file in the usual way.
3622 ;; On VMS, rename file and buffer to get rid of version number.
3624 (not (string= buffer-file-name
3625 (file-name-sans-versions buffer-file-name))))
3628 (setq buffer-file-name
3629 (file-name-sans-versions buffer-file-name))
3631 (let ((buf (create-file-buffer (downcase buffer-file-name))))
3634 (rename-buffer buffer-new-name)))
3635 ;; If buffer has no file name, ask user for one.
3636 (or buffer-file-name
3638 (expand-file-name
3639 (read-file-name "File to save in: ") nil)))
3640 (if (file-exists-p filename)
3641 (if (file-directory-p filename)
3650 (let ((dir (file-name-directory filename)))
3651 (unless (file-directory-p dir)
3652 (if (file-exists-p dir)
3655 (set-visited-file-name filename)))
3656 (or (verify-visited-file-modtime (current-buffer))
3657 (not (file-exists-p buffer-file-name))
3660 (file-name-nondirectory buffer-file-name)))
3666 (not find-file-literally)
3683 (run-hook-with-args-until-success 'local-write-file-hooks)
3684 (run-hook-with-args-until-success 'write-file-functions)
3685 ;; If a hook returned t, file is already "written".
3689 ;; that buffer-file-coding-system is fixed to what
3693 (setq buffer-file-coding-system last-coding-system-used))
3694 (setq buffer-file-number
3695 (nthcdr 10 (file-attributes buffer-file-name)))
3698 (set-file-modes buffer-file-name (car setmodes))
3700 ;; If the auto-save file was recent before this command,
3702 (delete-auto-save-file-if-necessary recent-save)
3708 ;; This does the "real job" of writing a buffer into its visited file
3709 ;; and making a backup file. This is what is normally done
3710 ;; but inhibited if one of write-file-functions returns non-nil.
3718 (setq buffer-file-coding-system-explicit last-coding-system-used)))
3723 (if (not (file-writable-p buffer-file-name))
3724 (let ((dir (file-name-directory buffer-file-name)))
3725 (if (not (file-directory-p dir))
3726 (if (file-exists-p dir)
3729 (if (not (file-exists-p buffer-file-name))
3733 (file-name-nondirectory
3734 buffer-file-name)))
3736 (error "Attempt to save to a file which you aren't allowed to write"))))))
3739 (let ((dir (file-name-directory buffer-file-name)))
3740 (if (and file-precious-flag
3741 (file-writable-p dir))
3742 ;; If file is precious, write temp name, then rename it.
3745 (let ((realname buffer-file-name)
3747 (umask (default-file-modes))
3748 (old-modtime (visited-file-modtime)))
3754 (clear-visited-file-modtime)
3755 (set-default-file-modes ?\700)
3757 ;; This code follows the example of make-temp-file,
3764 (expand-file-name "tmp" dir)))
3767 buffer-file-truename 'excl)
3769 (file-already-exists t))
3770 ;; The file was somehow created by someone else between
3775 (set-default-file-modes umask)
3778 (set-visited-file-modtime old-modtime)))
3779 ;; Since we have created an entirely new file,
3782 (cons (or (file-modes buffer-file-name)
3784 buffer-file-name)))
3785 ;; We succeeded in writing the temp file,
3786 ;; so rename it.
3787 (rename-file tempname buffer-file-name t))
3788 ;; If file not writable, see if we can make it writable
3794 (setq setmodes (cons (file-modes buffer-file-name) buffer-file-name))
3795 (set-file-modes buffer-file-name (logior (car setmodes) 128))))
3800 buffer-file-name nil t buffer-file-truename)
3802 ;; If we get an error writing the new file, and we made
3806 (rename-file (cdr setmodes) buffer-file-name t)
3810 (defun diff-buffer-with-file (&optional buffer)
3811 "View the differences between BUFFER and its associated file.
3815 (if (and buffer-file-name
3816 (file-exists-p buffer-file-name))
3817 (let ((tempfile (make-temp-file "buffer-content-")))
3822 (diff buffer-file-name tempfile nil t)
3824 (when (file-exists-p tempfile)
3825 (delete-file tempfile))))
3826 (message "Buffer %s has no associated file on disc" (buffer-name))
3846 (diff-buffer-with-file buf))
3859 "Save some modified file-visiting buffers. Asks user about each one.
3862 view the differences using `diff-buffer-with-file'.
3866 If PRED is nil, all the file-visiting buffers are considered.
3867 If PRED is t, then certain non-file buffers will also be considered.
3893 (buffer-file-name buffer)
3903 (if (buffer-file-name buffer)
3904 (format "Save file %s? "
3905 (buffer-file-name buffer))
3922 abbrev-file-name)))
3923 (write-abbrev-file nil))
3945 "Change whether this buffer is visiting its file read-only.
3947 If visiting file read-only and `view-read-only' is non-nil, enter view mode."
3966 (if (vc-backend buffer-file-name)
3971 (defun insert-file (filename)
3972 "Insert contents of file FILENAME into buffer after point.
3976 Don't call it from programs! Use `insert-file-contents' instead.
3978 (interactive "*fInsert file: ")
3979 (insert-file-1 filename #'insert-file-contents))
3981 (defun append-to-file (start end filename)
3982 "Append the contents of the region to the end of file FILENAME.
3986 (interactive "r\nFAppend to file: ")
3989 (defun file-newest-backup (filename)
3990 "Return most recent backup file for FILENAME or nil if no backups exist."
3991 ;; `make-backup-file-name' will get us the right directory for
3994 (let* ((filename (file-name-sans-versions
3995 (make-backup-file-name (expand-file-name filename))))
3996 (file (file-name-nondirectory filename))
3997 (dir (file-name-directory filename))
3998 (comp (file-name-all-completions file dir))
4003 (cond ((and (backup-file-name-p tem)
4004 (string= (file-name-sans-versions tem) file))
4007 (file-newer-than-file-p tem newest))
4011 (defun rename-uniquely ()
4019 (not (and buffer-file-name
4021 (file-name-nondirectory buffer-file-name))))
4023 ;; which isn't part of the file name (if any),
4026 (rename-buffer (generate-new-buffer-name base-name))
4032 is the current default directory for file names.
4033 That is useful when you have visited a file in a nonexistent directory.
4039 (list (read-file-name "Make directory: " default-directory default-directory
4044 (setq dir (expand-file-name dir))
4045 (let ((handler (find-file-name-handler dir 'make-directory)))
4050 (let ((dir (directory-file-name (expand-file-name dir)))
4052 (while (not (file-exists-p dir))
4054 dir (directory-file-name (file-name-directory dir))))
4066 (put 'revert-buffer-insert-file-contents-function 'permanent-local t)
4067 (defvar revert-buffer-insert-file-contents-function nil
4069 Gets two args, first the nominal file name to use,
4070 and second, t if reading the auto-save file.
4075 "Function to check whether a non-file buffer needs reverting.
4108 "Replace current buffer text with the text of the visited file on disk.
4109 This undoes all changes since the file was visited or saved.
4110 With a prefix argument, offer to revert from latest auto-save file, if
4111 that is more recent than the visited file.
4114 doesn't come from a file, but reflects some other data base instead:
4119 to revert from the auto-save file when this is nil. Note that the
4134 and the end, and if `revert-buffer-insert-file-contents-function' is
4135 non-nil, it is called instead of rereading visited file contents."
4139 ;; argument should be t to avoid consulting the auto-save file, and
4150 buffer-auto-save-file-name
4151 (file-readable-p buffer-auto-save-file-name)
4153 "Buffer has been auto-saved recently. Revert from auto-save file? ")))
4154 (file-name (if auto-save-p
4155 buffer-auto-save-file-name
4156 buffer-file-name)))
4157 (cond ((null file-name)
4158 (error "Buffer does not seem to be associated with any file"))
4163 (when (string-match regexp file-name)
4165 (yes-or-no-p (format "Revert buffer from file %s? "
4166 file-name)))
4168 ;; If file was backed up but has changed since,
4171 (not (verify-visited-file-modtime (current-buffer)))
4174 ;; since after-find-file will clobber it.
4180 (revert-buffer-insert-file-contents-function
4186 (funcall revert-buffer-insert-file-contents-function
4187 file-name auto-save-p)))
4188 ((not (file-exists-p file-name))
4189 (error (if buffer-file-number
4191 "Cannot revert nonexistent file %s")
4192 file-name))
4193 ((not (file-readable-p file-name))
4194 (error (if buffer-file-number
4196 "Cannot revert unreadable file %s")
4197 file-name))
4199 ;; Bind buffer-file-name to nil
4200 ;; so that we don't try to lock the file.
4201 (let ((buffer-file-name nil))
4206 ;; Auto-saved file should be read by Emacs'
4210 buffer-file-coding-system-explicit))))
4221 ;; This force after-insert-file-set-coding
4222 ;; (called from insert-file-contents) to set
4223 ;; buffer-file-coding-system to a proper value.
4224 (kill-local-variable 'buffer-file-coding-system)
4228 (let ((buffer-file-format buffer-file-format))
4229 (insert-file-contents file-name (not auto-save-p)
4231 (insert-file-contents file-name (not auto-save-p)
4235 (setq buffer-file-truename
4236 (abbreviate-file-name (file-truename buffer-file-name)))
4237 (after-find-file nil nil t t preserve-modes)
4247 (defun recover-this-file ()
4248 "Recover the visited file--get contents from its last auto-save file."
4250 (recover-file buffer-file-name))
4252 (defun recover-file (file)
4253 "Visit file FILE, but get contents from its last auto-save file."
4254 ;; Actually putting the file name in the minibuffer should be used
4257 (interactive "FRecover file: ")
4258 (setq file (expand-file-name file))
4259 (if (auto-save-file-name-p (file-name-nondirectory file))
4260 (error "%s is an auto-save file" (abbreviate-file-name file)))
4261 (let ((file-name (let ((buffer-file-name file))
4262 (make-auto-save-file-name))))
4263 (cond ((if (file-exists-p file)
4264 (not (file-newer-than-file-p file-name file))
4265 (not (file-exists-p file-name)))
4266 (error "Auto-save file %s not current"
4267 (abbreviate-file-name file-name)))
4272 (let ((switches dired-listing-switches))
4273 (if (file-symlink-p file)
4278 ;; FILE might not exist if the auto-save file was for
4279 ;; a buffer that didn't visit a file, such as "*mail*".
4282 (insert-directory-safely file switches)
4283 (insert-directory-safely file-name switches))))
4284 (yes-or-no-p (format "Recover auto save file %s? " file-name)))
4285 (switch-to-buffer (find-file-noselect file t))
4287 ;; Keep the current buffer-file-coding-system.
4288 (coding-system buffer-file-coding-system)
4289 ;; Auto-saved file should be read with special coding.
4292 (insert-file-contents file-name nil)
4293 (set-buffer-file-coding-system coding-system))
4294 (after-find-file nil nil t))
4295 (t (error "Recover-file cancelled")))))
4304 (if (null auto-save-list-file-prefix)
4305 (error "You set `auto-save-list-file-prefix' to disable making session files"))
4306 (let ((dir (file-name-directory auto-save-list-file-prefix)))
4307 (unless (file-directory-p dir)
4311 (file-name-nondirectory
4312 auto-save-list-file-prefix)))
4316 (dired (concat auto-save-list-file-prefix "*")
4317 (concat dired-listing-switches "t")))
4327 " type d on a line to mark that file for deletion.\n\n"))))
4336 ;; Get the name of the session file to recover from.
4337 (let ((file (dired-get-filename))
4340 (dired-unmark 1)
4341 (dired-do-flagged-delete t)
4344 ;; Read in the auto-save-list file.
4347 (insert-file-contents file)
4348 ;; Loop thru the text of that file
4353 ;; This is a pair of lines for a non-file-visiting buffer.
4354 ;; Get the auto-save file name and manufacture
4355 ;; a "visited file name" from that.
4358 ;; If there is no auto-save file name, the
4359 ;; auto-save-list file is probably corrupted.
4366 (expand-file-name
4368 (file-name-nondirectory autofile)
4370 (file-name-directory autofile))))
4372 ;; This pair of lines is a file-visiting
4373 ;; buffer. Use the visited file name.
4383 ;; Ignore a file if its auto-save file does not exist now.
4384 (if (and autofile (file-exists-p autofile))
4387 ;; The file contains a pair of line for each auto-saved buffer.
4388 ;; The first line of the pair contains the visited file name
4389 ;; or is empty if the buffer was not visiting a file.
4390 ;; The second line is the auto-save file name.
4393 (lambda (file)
4395 (save-excursion (recover-file file))
4397 "Failed to recover `%s'" file)))
4399 '("file" "files" "recover"))
4430 (setq buffer-auto-save-file-name
4432 (or (not buffer-auto-save-file-name)
4437 (if (and buffer-file-name auto-save-visited-file-name
4439 buffer-file-name
4440 (make-auto-save-file-name))))
4447 (if buffer-auto-save-file-name "on" "off")))
4448 buffer-auto-save-file-name)
4450 (defun rename-auto-save-file ()
4451 "Adjust current buffer's auto save file name for current conditions.
4452 Also rename any existing auto save file, if it was made in this session."
4453 (let ((osave buffer-auto-save-file-name))
4454 (setq buffer-auto-save-file-name
4455 (make-auto-save-file-name))
4456 (if (and osave buffer-auto-save-file-name
4457 (not (string= buffer-auto-save-file-name buffer-file-name))
4458 (not (string= buffer-auto-save-file-name osave))
4459 (file-exists-p osave)
4461 (rename-file osave buffer-auto-save-file-name t))))
4463 (defun make-auto-save-file-name ()
4464 "Return file name to use for auto-saves of current buffer.
4465 Does not consider `auto-save-visited-file-name' as that variable is checked
4467 See also `auto-save-file-name-p'."
4468 (if buffer-file-name
4469 (let ((handler (find-file-name-handler buffer-file-name
4470 'make-auto-save-file-name)))
4472 (funcall handler 'make-auto-save-file-name)
4473 (let ((list auto-save-file-name-transforms)
4474 (filename buffer-file-name)
4477 ;; to the file name.
4487 (file-name-directory result)
4495 (not (msdos-long-file-names)))
4496 ;; We truncate the file name to DOS 8+3 limits
4502 (file-name-nondirectory buffer-file-name))))
4506 (concat (file-name-directory buffer-file-name)
4509 (concat (file-name-directory filename)
4511 (file-name-nondirectory filename)
4513 ;; Make sure auto-save file names don't contain characters
4526 file-name)
4538 ;; Generate the file name.
4539 (setq file-name
4540 (make-temp-file
4542 (expand-file-name
4547 ((file-writable-p default-directory) default-directory)
4548 ((file-writable-p "/var/tmp/") "/var/tmp/")
4555 ;; DOS/Windows filesystems. make-temp-file writes to the
4556 ;; file it creates, so we must fix the file name _before_
4557 ;; make-temp-file is called.
4561 ;; make-temp-file creates the file,
4564 (delete-file file-name)
4565 (file-error nil))
4566 file-name)))
4568 (defun auto-save-file-name-p (filename)
4569 "Return non-nil if FILENAME can be yielded by `make-auto-save-file-name'.
4575 "Given a shell file name pattern WILDCARD, return an equivalent regexp.
4650 :group 'dired)
4658 :group 'dired)
4660 (defun file-expand-wildcards (pattern &optional full)
4662 This returns a list of file names which match the pattern.
4664 If PATTERN is written as an absolute file name,
4667 If PATTERN is written as a relative file name, it is interpreted
4669 The file names returned are normally also relative to the current
4672 (let* ((nondir (file-name-nondirectory pattern))
4673 (dirpart (file-name-directory pattern))
4678 (mapcar 'file-name-as-directory
4679 (file-expand-wildcards (directory-file-name dirpart)))
4684 (file-directory-p (directory-file-name (car dirs))))
4690 (file-name-nondirectory name))
4711 (list (read-file-name (if pfx "List directory (verbose): "
4719 (setq dirname (expand-file-name dirname))
4728 (let ((wildcard (not (file-directory-p dirname))))
4733 (if (file-directory-p dirname)
4734 (file-name-as-directory dirname)
4735 (file-name-directory dirname))))))
4740 PATTERN is assumed to represent a file-name wildcard suitable for the
4750 ;; DOS/Windows don't allow `"' in file names. So if the
4787 "Program to get the amount of free space on a file system.
4789 The value of this variable must be just a command name or file name;
4794 If the function `file-system-info' is defined, it is always used in
4797 :group 'dired)
4803 :group 'dired)
4806 "Return the amount of free space on directory DIR's file system.
4811 This function calls `file-system-info' if it is available, or invokes the
4813 (when (not (file-remote-p dir))
4816 (if (fboundp 'file-system-info)
4817 (let ((fsinfo (file-system-info dir)))
4841 ;; The following expression replaces `dired-move-to-filename-regexp'.
4881 ;; vc dired listings provide the state or blanks between file
4889 "Regular expression to match up to the file name in a directory listing.
4900 ;; - must insert exactly one line for each file if WILDCARD or
4902 ;; before the file lines, plus optional text after the file lines.
4907 ;; - functions dired-move-to-filename, (these two define what a file line is)
4908 ;; dired-move-to-end-of-filename,
4909 ;; dired-between-files, (shortcut for (not (dired-move-to-filename)))
4910 ;; dired-insert-headerline
4911 ;; dired-after-subdir-garbage (defines what a "total" line is)
4912 ;; - variable dired-subdir-regexp
4913 ;; - may be passed "--dired" as the first argument in SWITCHES.
4916 (defun insert-directory (file switches &optional wildcard full-directory-p)
4922 Optional fourth arg FULL-DIRECTORY-P means file is a directory and
4927 If WILDCARD, it also runs the shell specified by `shell-file-name'.
4929 When SWITCHES contains the long `--dired' option, this function
4930 treats it specially, for the sake of dired. However, the
4934 (let ((handler (find-file-name-handler (expand-file-name file)
4937 (funcall handler 'insert-directory file switches
4940 (vms-read-directory file switches (current-buffer))
4946 ;; putting text property `dired-filename, decode one
4952 (or file-name-coding-system
4953 default-file-name-coding-system))))
4956 ;; Run ls in the directory part of the file pattern
4959 (if (file-name-absolute-p file)
4960 (file-name-directory file)
4961 (file-name-directory (expand-file-name file))))
4962 (pattern (file-name-nondirectory file)))
4964 shell-file-name nil t nil
4997 (if (string-match "\\`~" file)
4998 (setq file (expand-file-name file)))
5001 (concat (file-name-as-directory file) ".")
5002 file))))))))
5008 (string-match "--dired\\>" switches)
5009 (member "--dired" switches))
5062 (if (and (file-directory-p file)
5068 file result)
5069 ;; Unix. Access the file to get a suitable error.
5070 (access-file file "Reading directory")
5071 (error "Listing directory failed but `access-file' worked")))
5074 (string-match "--dired\\>" switches)
5075 (member "--dired" switches))
5077 ;; directory listed with "--dired", but without "-a"
5097 ;; Now read the numeric positions of file names.
5110 (put-text-property start end 'dired-filename t)
5113 (put-text-property beg (point) 'dired-filename nil)
5127 file-name-coding-system
5128 default-file-name-coding-system
5147 val (get-text-property (point) 'dired-filename))
5149 (point) 'dired-filename nil (point-max)))
5150 ;; Force no eol conversion on a file name, so
5156 'dired-filename t)))))))
5172 "Convert `ls --dired' file name position value POS to a buffer position.
5173 File name position values returned in ls --dired output
5183 (defun insert-directory-safely (file switches
5189 (if (file-exists-p file)
5190 (insert-directory file switches wildcard full-directory-p)
5192 (insert (format "%s: No such file or directory\n" file))))
5213 With prefix arg, silently save all file-visiting buffers, then kill."
5217 (lambda (buf) (and (buffer-file-name buf)
5240 ;; We use /: as a prefix to "quote" a file name
5241 ;; so that magic file name handlers will not apply to it.
5243 (setq file-name-handler-alist
5244 (cons '("\\`/:" . file-name-non-special)
5245 file-name-handler-alist))
5250 ;; So it is safe for us to inhibit *all* magic file name handlers.
5252 (defun file-name-non-special (operation &rest arguments)
5253 (let ((file-name-handler-alist nil)
5256 (directory-file-name
5257 (expand-file-name
5258 (unhandled-file-name-directory default-directory)))
5260 ;; Get a list of the indices of the args which are file names.
5261 (file-arg-indices
5264 ;; return a file name. We want to include the /:
5267 '((expand-file-name . nil)
5268 (file-name-directory . nil)
5269 (file-name-as-directory . nil)
5270 (directory-file-name . nil)
5271 (file-name-sans-versions . nil)
5272 (find-backup-file-name . nil)
5275 (substitute-in-file-name identity)
5277 (file-truename add 0)
5278 ;; `quote' means add "/:" to buffer-file-name.
5279 (insert-file-contents quote 0)
5280 ;; `unquote-then-quote' means set buffer-file-name
5282 (verify-visited-file-modtime unquote-then-quote)
5284 (file-name-completion 1)
5285 (file-name-all-completions 1)
5287 (rename-file 0 1)
5288 (copy-file 0 1)
5290 (add-name-to-file 0 1)))
5292 ;; as the file name.
5297 (if (symbolp (car file-arg-indices))
5298 (setq method (pop file-arg-indices)))
5299 ;; Strip off the /: from the file names that have it.
5301 (while (consp file-arg-indices)
5302 (let ((pair (nthcdr (car file-arg-indices) arguments)))
5309 (setq file-arg-indices (cdr file-arg-indices))))
5317 (setq buffer-file-name (concat "/:" buffer-file-name))))
5320 (setq buffer-file-name (substring buffer-file-name 2))
5322 (setq buffer-file-name (concat "/:" buffer-file-name))
5328 (define-key ctl-x-map "\C-f" 'find-file)
5329 (define-key ctl-x-map "\C-r" 'find-file-read-only)
5330 (define-key ctl-x-map "\C-v" 'find-alternate-file)
5333 (define-key ctl-x-map "\C-w" 'write-file)
5334 (define-key ctl-x-map "i" 'insert-file)
5340 (define-key ctl-x-4-map "f" 'find-file-other-window)
5341 (define-key ctl-x-4-map "r" 'find-file-read-only-other-window)
5342 (define-key ctl-x-4-map "\C-f" 'find-file-other-window)
5347 (define-key ctl-x-5-map "f" 'find-file-other-frame)
5348 (define-key ctl-x-5-map "\C-f" 'find-file-other-frame)
5349 (define-key ctl-x-5-map "r" 'find-file-read-only-other-frame)