diff options
| author | Richard M. Stallman | 1998-06-09 02:49:43 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1998-06-09 02:49:43 +0000 |
| commit | ddd64da9df7d21ebf7c9328b6338c0102c189966 (patch) | |
| tree | d2d4ee43259b4db518f9b295f04032ba17efb279 | |
| parent | 3e007e816f921f10d4a81fce0d725db35e023108 (diff) | |
| download | emacs-ddd64da9df7d21ebf7c9328b6338c0102c189966.tar.gz emacs-ddd64da9df7d21ebf7c9328b6338c0102c189966.zip | |
(find-file-noselect-1): New function.
(find-file-noselect): If want to visit literally and buffer is
visited the ordinary way, or vice versa, ask user whether
to re-visit, or save and revisit. Use find-file-noselect-1.
| -rw-r--r-- | lisp/files.el | 224 |
1 files changed, 123 insertions, 101 deletions
diff --git a/lisp/files.el b/lisp/files.el index 82b109b04fa..2db3657f2d5 100644 --- a/lisp/files.el +++ b/lisp/files.el | |||
| @@ -882,112 +882,134 @@ Optional second arg RAWFILE non-nil means the file is read literally." | |||
| 882 | (if (or find-file-existing-other-name find-file-visit-truename) | 882 | (if (or find-file-existing-other-name find-file-visit-truename) |
| 883 | (setq buf other)))) | 883 | (setq buf other)))) |
| 884 | (if buf | 884 | (if buf |
| 885 | (or nowarn | 885 | (progn |
| 886 | (verify-visited-file-modtime buf) | 886 | (or nowarn |
| 887 | (cond ((not (file-exists-p filename)) | 887 | (verify-visited-file-modtime buf) |
| 888 | (error "File %s no longer exists!" filename)) | 888 | (cond ((not (file-exists-p filename)) |
| 889 | ;; Certain files should be reverted automatically | 889 | (error "File %s no longer exists!" filename)) |
| 890 | ;; if they have changed on disk and not in the buffer. | 890 | ;; Certain files should be reverted automatically |
| 891 | ((and (not (buffer-modified-p buf)) | 891 | ;; if they have changed on disk and not in the buffer. |
| 892 | (let ((tail revert-without-query) | 892 | ((and (not (buffer-modified-p buf)) |
| 893 | (found nil)) | 893 | (let ((tail revert-without-query) |
| 894 | (while tail | 894 | (found nil)) |
| 895 | (if (string-match (car tail) filename) | 895 | (while tail |
| 896 | (setq found t)) | 896 | (if (string-match (car tail) filename) |
| 897 | (setq tail (cdr tail))) | 897 | (setq found t)) |
| 898 | found)) | 898 | (setq tail (cdr tail))) |
| 899 | (with-current-buffer buf | 899 | found)) |
| 900 | (message "Reverting file %s..." filename) | 900 | (with-current-buffer buf |
| 901 | (revert-buffer t t) | 901 | (message "Reverting file %s..." filename) |
| 902 | (message "Reverting file %s...done" filename))) | 902 | (revert-buffer t t) |
| 903 | ((yes-or-no-p | 903 | (message "Reverting file %s...done" filename))) |
| 904 | (if (string= (file-name-nondirectory filename) | 904 | ((yes-or-no-p |
| 905 | (buffer-name buf)) | 905 | (if (string= (file-name-nondirectory filename) |
| 906 | (buffer-name buf)) | ||
| 907 | (format | ||
| 908 | (if (buffer-modified-p buf) | ||
| 909 | "File %s changed on disk. Discard your edits? " | ||
| 910 | "File %s changed on disk. Reread from disk? ") | ||
| 911 | (file-name-nondirectory filename)) | ||
| 906 | (format | 912 | (format |
| 907 | (if (buffer-modified-p buf) | 913 | (if (buffer-modified-p buf) |
| 908 | "File %s changed on disk. Discard your edits? " | 914 | "File %s changed on disk. Discard your edits in %s? " |
| 909 | "File %s changed on disk. Reread from disk? ") | 915 | "File %s changed on disk. Reread from disk into %s? ") |
| 910 | (file-name-nondirectory filename)) | 916 | (file-name-nondirectory filename) |
| 911 | (format | 917 | (buffer-name buf)))) |
| 912 | (if (buffer-modified-p buf) | 918 | (with-current-buffer buf |
| 913 | "File %s changed on disk. Discard your edits in %s? " | 919 | (revert-buffer t t))))) |
| 914 | "File %s changed on disk. Reread from disk into %s? ") | 920 | (when (not (eq rawfile (not (null find-file-literally)))) |
| 915 | (file-name-nondirectory filename) | 921 | (with-current-buffer buf |
| 916 | (buffer-name buf)))) | 922 | (if (buffer-modified-p) |
| 917 | (with-current-buffer buf | 923 | (if (y-or-n-p (if rawfile |
| 918 | (revert-buffer t t))) | 924 | "Save file and revisit literally? " |
| 919 | ((not (eq rawfile (not (null find-file-literally)))) | 925 | "Save file and revisit non-literally? ")) |
| 920 | (if rawfile | 926 | (progn |
| 921 | (message "File is already visited, and not literally") | 927 | (save-buffer) |
| 922 | (message "File is already visited, and visited literally"))))) | 928 | (revert-buffer t t)) |
| 923 | (save-excursion | 929 | (if (y-or-n-p (if rawfile |
| 924 | ;;; The truename stuff makes this obsolete. | 930 | "Discard your edits and revisit file literally? " |
| 925 | ;;; (let* ((link-name (car (file-attributes filename))) | 931 | "Discard your edits and revisit file non-literally? ")) |
| 926 | ;;; (linked-buf (and (stringp link-name) | 932 | (find-file-noselect-1 buf filename nowarn |
| 927 | ;;; (get-file-buffer link-name)))) | 933 | rawfile truename number) |
| 928 | ;;; (if (bufferp linked-buf) | 934 | (error (if rawfile "File already visited non-literally" |
| 929 | ;;; (message "Symbolic link to file in buffer %s" | 935 | "File already visited literally")))) |
| 930 | ;;; (buffer-name linked-buf)))) | 936 | (if (y-or-n-p (if rawfile |
| 937 | "Revisit file literally? " | ||
| 938 | "Revisit file non-literally? ")) | ||
| 939 | (find-file-noselect-1 buf filename nowarn | ||
| 940 | rawfile truename number) | ||
| 941 | (error (if rawfile "File already visited non-literally" | ||
| 942 | "File already visited literally"))))))) | ||
| 943 | (progn | ||
| 931 | (setq buf (create-file-buffer filename)) | 944 | (setq buf (create-file-buffer filename)) |
| 932 | (set-buffer-major-mode buf) | 945 | (set-buffer-major-mode buf) |
| 933 | (set-buffer buf) | 946 | (find-file-noselect-1 buf filename nowarn rawfile truename number))) |
| 934 | (erase-buffer) | ||
| 935 | (if rawfile | ||
| 936 | (condition-case () | ||
| 937 | (insert-file-contents-literally filename t) | ||
| 938 | (file-error | ||
| 939 | (when (and (file-exists-p filename) | ||
| 940 | (not (file-readable-p filename))) | ||
| 941 | (kill-buffer buf) | ||
| 942 | (signal 'file-error (list "File is not readable" | ||
| 943 | filename))) | ||
| 944 | ;; Unconditionally set error | ||
| 945 | (setq error t))) | ||
| 946 | (condition-case () | ||
| 947 | (insert-file-contents filename t) | ||
| 948 | (file-error | ||
| 949 | (when (and (file-exists-p filename) | ||
| 950 | (not (file-readable-p filename))) | ||
| 951 | (kill-buffer buf) | ||
| 952 | (signal 'file-error (list "File is not readable" | ||
| 953 | filename))) | ||
| 954 | ;; Run find-file-not-found-hooks until one returns non-nil. | ||
| 955 | (or (run-hook-with-args-until-success 'find-file-not-found-hooks) | ||
| 956 | ;; If they fail too, set error. | ||
| 957 | (setq error t))))) | ||
| 958 | ;; Find the file's truename, and maybe use that as visited name. | ||
| 959 | (setq buffer-file-truename truename) | ||
| 960 | (setq buffer-file-number number) | ||
| 961 | ;; On VMS, we may want to remember which directory in a search list | ||
| 962 | ;; the file was found in. | ||
| 963 | (and (eq system-type 'vax-vms) | ||
| 964 | (let (logical) | ||
| 965 | (if (string-match ":" (file-name-directory filename)) | ||
| 966 | (setq logical (substring (file-name-directory filename) | ||
| 967 | 0 (match-beginning 0)))) | ||
| 968 | (not (member logical find-file-not-true-dirname-list))) | ||
| 969 | (setq buffer-file-name buffer-file-truename)) | ||
| 970 | (if find-file-visit-truename | ||
| 971 | (setq buffer-file-name | ||
| 972 | (setq filename | ||
| 973 | (expand-file-name buffer-file-truename)))) | ||
| 974 | ;; Set buffer's default directory to that of the file. | ||
| 975 | (setq default-directory (file-name-directory filename)) | ||
| 976 | ;; Turn off backup files for certain file names. Since | ||
| 977 | ;; this is a permanent local, the major mode won't eliminate it. | ||
| 978 | (and (not (funcall backup-enable-predicate buffer-file-name)) | ||
| 979 | (progn | ||
| 980 | (make-local-variable 'backup-inhibited) | ||
| 981 | (setq backup-inhibited t))) | ||
| 982 | (if rawfile | ||
| 983 | (progn | ||
| 984 | (set-buffer-multibyte nil) | ||
| 985 | (setq buffer-file-coding-system 'no-conversion) | ||
| 986 | (make-local-variable 'find-file-literally) | ||
| 987 | (setq find-file-literally t)) | ||
| 988 | (after-find-file error (not nowarn)) | ||
| 989 | (setq buf (current-buffer))))) | ||
| 990 | buf))) | 947 | buf))) |
| 948 | |||
| 949 | (defun find-file-noselect-1 (buf filename nowarn rawfile truename number) | ||
| 950 | (let ((inhibit-read-only t)) | ||
| 951 | (with-current-buffer buf | ||
| 952 | (kill-local-variable 'find-file-literally) | ||
| 953 | (setq buffer-file-coding-system nil) | ||
| 954 | (erase-buffer) | ||
| 955 | (and (default-value 'enable-multibyte-characters) | ||
| 956 | (not rawfile) | ||
| 957 | (set-buffer-multibyte t)) | ||
| 958 | (if rawfile | ||
| 959 | (condition-case () | ||
| 960 | (insert-file-contents-literally filename t) | ||
| 961 | (file-error | ||
| 962 | (when (and (file-exists-p filename) | ||
| 963 | (not (file-readable-p filename))) | ||
| 964 | (kill-buffer buf) | ||
| 965 | (signal 'file-error (list "File is not readable" | ||
| 966 | filename))) | ||
| 967 | ;; Unconditionally set error | ||
| 968 | (setq error t))) | ||
| 969 | (condition-case () | ||
| 970 | (insert-file-contents filename t) | ||
| 971 | (file-error | ||
| 972 | (when (and (file-exists-p filename) | ||
| 973 | (not (file-readable-p filename))) | ||
| 974 | (kill-buffer buf) | ||
| 975 | (signal 'file-error (list "File is not readable" | ||
| 976 | filename))) | ||
| 977 | ;; Run find-file-not-found-hooks until one returns non-nil. | ||
| 978 | (or (run-hook-with-args-until-success 'find-file-not-found-hooks) | ||
| 979 | ;; If they fail too, set error. | ||
| 980 | (setq error t))))) | ||
| 981 | ;; Find the file's truename, and maybe use that as visited name. | ||
| 982 | (setq buffer-file-truename truename) | ||
| 983 | (setq buffer-file-number number) | ||
| 984 | ;; On VMS, we may want to remember which directory in a search list | ||
| 985 | ;; the file was found in. | ||
| 986 | (and (eq system-type 'vax-vms) | ||
| 987 | (let (logical) | ||
| 988 | (if (string-match ":" (file-name-directory filename)) | ||
| 989 | (setq logical (substring (file-name-directory filename) | ||
| 990 | 0 (match-beginning 0)))) | ||
| 991 | (not (member logical find-file-not-true-dirname-list))) | ||
| 992 | (setq buffer-file-name buffer-file-truename)) | ||
| 993 | (if find-file-visit-truename | ||
| 994 | (setq buffer-file-name | ||
| 995 | (setq filename | ||
| 996 | (expand-file-name buffer-file-truename)))) | ||
| 997 | ;; Set buffer's default directory to that of the file. | ||
| 998 | (setq default-directory (file-name-directory filename)) | ||
| 999 | ;; Turn off backup files for certain file names. Since | ||
| 1000 | ;; this is a permanent local, the major mode won't eliminate it. | ||
| 1001 | (and (not (funcall backup-enable-predicate buffer-file-name)) | ||
| 1002 | (progn | ||
| 1003 | (make-local-variable 'backup-inhibited) | ||
| 1004 | (setq backup-inhibited t))) | ||
| 1005 | (if rawfile | ||
| 1006 | (progn | ||
| 1007 | (set-buffer-multibyte nil) | ||
| 1008 | (setq buffer-file-coding-system 'no-conversion) | ||
| 1009 | (make-local-variable 'find-file-literally) | ||
| 1010 | (setq find-file-literally t)) | ||
| 1011 | (after-find-file error (not nowarn)) | ||
| 1012 | (setq buf (current-buffer)))))) | ||
| 991 | 1013 | ||
| 992 | (defun insert-file-contents-literally (filename &optional visit beg end replace) | 1014 | (defun insert-file-contents-literally (filename &optional visit beg end replace) |
| 993 | "Like `insert-file-contents', but only reads in the file literally. | 1015 | "Like `insert-file-contents', but only reads in the file literally. |