comparison lisp/prim/files.el @ 151:59463afc5666 r20-3b2

Import from CVS: tag r20-3b2
author cvs
date Mon, 13 Aug 2007 09:37:19 +0200
parents 538048ae2ab8
children 25f70ba0133c
comparison
equal deleted inserted replaced
150:8ebb1c0f0f6f 151:59463afc5666
822 On VMS, when you visit a file using a logical name that searches a path, 822 On VMS, when you visit a file using a logical name that searches a path,
823 you may or may not want the visited file name to record the specific 823 you may or may not want the visited file name to record the specific
824 directory where the file was found. If you *do not* want that, add the logical 824 directory where the file was found. If you *do not* want that, add the logical
825 name to this list as a string.") 825 name to this list as a string.")
826 826
827 ;(defun find-buffer-visiting (filename) 827 ;; This function is needed by FSF vc.el. I hope somebody can make it
828 ; "Return the buffer visiting file FILENAME (a string). 828 ;; work for XEmacs. -sb.
829 ;This is like `get-file-buffer', except that it checks for any buffer 829 (defun find-buffer-visiting (filename)
830 ;visiting the same file, possibly under a different name. 830 "Return the buffer visiting file FILENAME (a string).
831 ;If there is no such live buffer, return nil." 831 This is like `get-file-buffer', except that it checks for any buffer
832 ; (let ((buf (get-file-buffer filename)) 832 visiting the same file, possibly under a different name.
833 ; (truename (abbreviate-file-name (file-truename filename)))) 833 If there is no such live buffer, return nil."
834 ; (or buf 834 (let ((buf (get-file-buffer filename))
835 ; (let ((list (buffer-list)) found) 835 (truename (abbreviate-file-name (file-truename filename))))
836 ; (while (and (not found) list) 836 (or buf
837 ; (save-excursion 837 (let ((list (buffer-list)) found)
838 ; (set-buffer (car list)) 838 (while (and (not found) list)
839 ; (if (and buffer-file-name 839 (save-excursion
840 ; (string= buffer-file-truename truename)) 840 (set-buffer (car list))
841 ; (setq found (car list)))) 841 (if (and buffer-file-name
842 ; (setq list (cdr list))) 842 (string= buffer-file-truename truename))
843 ; found) 843 (setq found (car list))))
844 ; (let ((number (nthcdr 10 (file-attributes truename))) 844 (setq list (cdr list)))
845 ; (list (buffer-list)) found) 845 found)
846 ; (and number 846 (let ((number (nthcdr 10 (file-attributes truename)))
847 ; (while (and (not found) list) 847 (list (buffer-list)) found)
848 ; (save-excursion 848 (and buffer-file-numbers-unique
849 ; (set-buffer (car list)) 849 number
850 ; (if (and buffer-file-number 850 (while (and (not found) list)
851 ; (equal buffer-file-number number) 851 (save-excursion
852 ; ;; Verify this buffer's file number 852 (set-buffer (car list))
853 ; ;; still belongs to its file. 853 (if (and buffer-file-number
854 ; (file-exists-p buffer-file-name) 854 (equal buffer-file-number number)
855 ; (equal (nthcdr 10 (file-attributes buffer-file-name)) 855 ;; Verify this buffer's file number
856 ; number)) 856 ;; still belongs to its file.
857 ; (setq found (car list)))) 857 (file-exists-p buffer-file-name)
858 ; (setq list (cdr list)))) 858 (equal (nthcdr 10 (file-attributes buffer-file-name))
859 ; found)))) 859 number))
860 (setq found (car list))))
861 (setq list (cdr list))))
862 found))))
860 863
861 (defun insert-file-contents-literally (filename &optional visit beg end replace) 864 (defun insert-file-contents-literally (filename &optional visit beg end replace)
862 "Like `insert-file-contents', q.v., but only reads in the file. 865 "Like `insert-file-contents', q.v., but only reads in the file.
863 A buffer may be modified in several ways after reading into the buffer due 866 A buffer may be modified in several ways after reading into the buffer due
864 to advanced Emacs features, such as file-name-handlers, format decoding, 867 to advanced Emacs features, such as file-name-handlers, format decoding,
1008 buf))) 1011 buf)))
1009 1012
1010 (defvar after-find-file-from-revert-buffer nil) 1013 (defvar after-find-file-from-revert-buffer nil)
1011 1014
1012 (defun after-find-file (&optional error warn noauto 1015 (defun after-find-file (&optional error warn noauto
1013 after-find-file-from-revert-buffer) 1016 after-find-file-from-revert-buffer
1017 nomodes)
1014 "Called after finding a file and by the default revert function. 1018 "Called after finding a file and by the default revert function.
1015 Sets buffer mode, parses local variables. 1019 Sets buffer mode, parses local variables.
1016 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an 1020 Optional args ERROR, WARN, and NOAUTO: ERROR non-nil means there was an
1017 error in reading the file. WARN non-nil means warn if there 1021 error in reading the file. WARN non-nil means warn if there
1018 exists an auto-save file more recent than the visited file. 1022 exists an auto-save file more recent than the visited file.
1019 NOAUTO means don't mess with auto-save mode. 1023 NOAUTO means don't mess with auto-save mode.
1020 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil 1024 Fourth arg AFTER-FIND-FILE-FROM-REVERT-BUFFER non-nil
1021 means this call was from `revert-buffer'. 1025 means this call was from `revert-buffer'.
1026 Fifth arg NOMODES non-nil means don't alter the file's modes.
1022 Finishes by calling the functions in `find-file-hooks'." 1027 Finishes by calling the functions in `find-file-hooks'."
1023 (setq buffer-read-only (not (file-writable-p buffer-file-name))) 1028 (setq buffer-read-only (not (file-writable-p buffer-file-name)))
1024 (if noninteractive 1029 (if noninteractive
1025 nil 1030 nil
1026 (let* (not-serious 1031 (let* (not-serious
1030 (gettext "File exists, but cannot be read.")) 1035 (gettext "File exists, but cannot be read."))
1031 ((not buffer-read-only) 1036 ((not buffer-read-only)
1032 (if (and warn 1037 (if (and warn
1033 (file-newer-than-file-p (make-auto-save-file-name) 1038 (file-newer-than-file-p (make-auto-save-file-name)
1034 buffer-file-name)) 1039 buffer-file-name))
1035 (gettext "Auto save file is newer; consider M-x recover-file") 1040 (format "%s has auto save data; consider M-x recover-file"
1036 (setq not-serious t) 1041 (setq not-serious t)
1037 (if error (gettext "(New file)") nil))) 1042 (if error (gettext "(New file)") nil))))
1038 ((not error) 1043 ((not error)
1039 (setq not-serious t) 1044 (setq not-serious t)
1040 (gettext "Note: file is write protected")) 1045 (gettext "Note: file is write protected"))
1041 ((file-attributes (directory-file-name default-directory)) 1046 ((file-attributes (directory-file-name default-directory))
1042 (gettext "File not found and directory write-protected")) 1047 (gettext "File not found and directory write-protected"))
1046 ;; If the directory the buffer is in doesn't exist, 1051 ;; If the directory the buffer is in doesn't exist,
1047 ;; offer to create it. It's better to do this now 1052 ;; offer to create it. It's better to do this now
1048 ;; than when we save the buffer, because we want 1053 ;; than when we save the buffer, because we want
1049 ;; autosaving to work. 1054 ;; autosaving to work.
1050 (setq buffer-read-only nil) 1055 (setq buffer-read-only nil)
1051 (or (file-exists-p (file-name-directory buffer-file-name)) 1056 (if (file-exists-p (file-name-directory (directory-file-name (file-name-directory buffer-file-name))))
1052 (if (yes-or-no-p 1057 "Use M-x make-dir RET RET to create the directory"
1053 (format 1058 "Use C-u M-x make-dir RET RET to create directory and its parents")))))
1054 "The directory containing %s does not exist. Create? "
1055 (abbreviate-file-name buffer-file-name)))
1056 (make-directory (file-name-directory
1057 buffer-file-name)
1058 t)))
1059 nil))))
1060 (if msg 1059 (if msg
1061 (progn 1060 (progn
1062 (message msg) 1061 (message msg)
1063 (or not-serious (sit-for 1 t))))) 1062 (or not-serious (sit-for 1 t)))))
1064 (if (and auto-save-default (not noauto)) 1063 (if (and auto-save-default (not noauto))
1065 (auto-save-mode t))) 1064 (auto-save-mode t)))
1066 (normal-mode t) 1065 (unless nomodes
1067 (run-hooks 'find-file-hooks)) 1066 (normal-mode t)
1067 (run-hooks 'find-file-hooks)))
1068 1068
1069 (defun normal-mode (&optional find-file) 1069 (defun normal-mode (&optional find-file)
1070 "Choose the major mode for this buffer automatically. 1070 "Choose the major mode for this buffer automatically.
1071 Also sets up any specified local variables of the file. 1071 Also sets up any specified local variables of the file.
1072 Uses the visited file name, the -*- line, and the local variables spec. 1072 Uses the visited file name, the -*- line, and the local variables spec.
1954 and `kept-new-versions', which tells how many newest versions to keep. 1954 and `kept-new-versions', which tells how many newest versions to keep.
1955 Defaults are 2 old versions and 2 new. 1955 Defaults are 2 old versions and 2 new.
1956 `dired-kept-versions' controls dired's clean-directory (.) command. 1956 `dired-kept-versions' controls dired's clean-directory (.) command.
1957 If `delete-old-versions' is nil, system will query user 1957 If `delete-old-versions' is nil, system will query user
1958 before trimming versions. Otherwise it does it silently." 1958 before trimming versions. Otherwise it does it silently."
1959 (interactive "p") 1959 (interactive "_p")
1960 (let ((modp (buffer-modified-p)) 1960 (let ((modp (buffer-modified-p))
1961 (large (> (buffer-size) 50000)) 1961 (large (> (buffer-size) 50000))
1962 (make-backup-files (or (and make-backup-files (not (eq args 0))) 1962 (make-backup-files (or (and make-backup-files (not (eq args 0)))
1963 (memq args '(16 64))))) 1963 (memq args '(16 64)))))
1964 (and modp (memq args '(16 64)) (setq buffer-backed-up nil)) 1964 (and modp (memq args '(16 64)) (setq buffer-backed-up nil))
2416 hook functions. 2416 hook functions.
2417 2417
2418 If `revert-buffer-function' is used to override the normal revert 2418 If `revert-buffer-function' is used to override the normal revert
2419 mechanism, this hook is not used.") 2419 mechanism, this hook is not used.")
2420 2420
2421 (defun revert-buffer (&optional ignore-auto noconfirm) 2421 (defun revert-buffer (&optional ignore-auto noconfirm preserve-modes)
2422 "Replace the buffer text with the text of the visited file on disk. 2422 "Replace the buffer text with the text of the visited file on disk.
2423 This undoes all changes since the file was visited or saved. 2423 This undoes all changes since the file was visited or saved.
2424 With a prefix argument, offer to revert from latest auto-save file, if 2424 With a prefix argument, offer to revert from latest auto-save file, if
2425 that is more recent than the visited file. 2425 that is more recent than the visited file.
2426
2427 This command also works for special buffers that contain text which
2428 doesn't come from a file, but reflects some other data base instead:
2429 for example, Dired buffers and buffer-list buffers. In these cases,
2430 it reconstructs the buffer contents from the appropriate data base.
2431
2426 When called from Lisp, the first argument is IGNORE-AUTO; only offer 2432 When called from Lisp, the first argument is IGNORE-AUTO; only offer
2427 to revert from the auto-save file when this is nil. Note that the 2433 to revert from the auto-save file when this is nil. Note that the
2428 sense of this argument is the reverse of the prefix argument, for the 2434 sense of this argument is the reverse of the prefix argument, for the
2429 sake of backward compatibility. IGNORE-AUTO is optional, defaulting 2435 sake of backward compatibility. IGNORE-AUTO is optional, defaulting
2430 to nil. 2436 to nil.
2431 2437
2432 Optional second argument NOCONFIRM means don't ask for confirmation at 2438 Optional second argument NOCONFIRM means don't ask for confirmation at
2433 all. 2439 all.
2440
2441 Optional third argument PRESERVE-MODES non-nil means don't alter
2442 the files modes. Normally we reinitialize them using `normal-mode'.
2434 2443
2435 If the value of `revert-buffer-function' is non-nil, it is called to 2444 If the value of `revert-buffer-function' is non-nil, it is called to
2436 do the work. 2445 do the work.
2437 2446
2438 The default revert function runs the hook `before-revert-hook' at the 2447 The default revert function runs the hook `before-revert-hook' at the
2496 nil nil t))) 2505 nil nil t)))
2497 (goto-char (min opoint (point-max))) 2506 (goto-char (min opoint (point-max)))
2498 ;; Recompute the truename in case changes in symlinks 2507 ;; Recompute the truename in case changes in symlinks
2499 ;; have changed the truename. 2508 ;; have changed the truename.
2500 ;XEmacs: already done by insert-file-contents 2509 ;XEmacs: already done by insert-file-contents
2501 ;(compute-buffer-file-truename) 2510 ;;(setq buffer-file-truename
2502 (after-find-file nil nil t t) 2511 ;;(abbreviate-file-name (file-truename buffer-file-name)))
2512 (after-find-file nil nil t t preserve-modes)
2503 ;; Run after-revert-hook as it was before we reverted. 2513 ;; Run after-revert-hook as it was before we reverted.
2504 (setq-default revert-buffer-internal-hook global-hook) 2514 (setq-default revert-buffer-internal-hook global-hook)
2505 (if local-hook-p 2515 (if local-hook-p
2506 (progn 2516 (progn
2507 (make-local-variable 'revert-buffer-internal-hook) 2517 (make-local-variable 'revert-buffer-internal-hook)