diff options
| author | Richard M. Stallman | 1997-08-11 22:32:01 +0000 |
|---|---|---|
| committer | Richard M. Stallman | 1997-08-11 22:32:01 +0000 |
| commit | 9b8ef27dedb6746a5c42a6a6841c14202e6cd0b7 (patch) | |
| tree | 48892c456ecadcba4a529b5213ddaca950036248 | |
| parent | 19823ce3b10f7fca48baeaa8b617582bd140ebd7 (diff) | |
| download | emacs-9b8ef27dedb6746a5c42a6a6841c14202e6cd0b7.tar.gz emacs-9b8ef27dedb6746a5c42a6a6841c14202e6cd0b7.zip | |
(insert-file-literally): New command.
| -rw-r--r-- | lisp/files.el | 96 |
1 files changed, 64 insertions, 32 deletions
diff --git a/lisp/files.el b/lisp/files.el index 5fcaef03406..4bd9e89262b 100644 --- a/lisp/files.el +++ b/lisp/files.el | |||
| @@ -688,7 +688,7 @@ If the current buffer now contains an empty file that you just visited | |||
| 688 | (rename-buffer oname)))) | 688 | (rename-buffer oname)))) |
| 689 | (or (eq (current-buffer) obuf) | 689 | (or (eq (current-buffer) obuf) |
| 690 | (kill-buffer obuf)))) | 690 | (kill-buffer obuf)))) |
| 691 | 691 | ||
| 692 | (defun create-file-buffer (filename) | 692 | (defun create-file-buffer (filename) |
| 693 | "Create a suitably named buffer for visiting FILENAME, and return it. | 693 | "Create a suitably named buffer for visiting FILENAME, and return it. |
| 694 | FILENAME (sans directory) is used unchanged if that name is free; | 694 | FILENAME (sans directory) is used unchanged if that name is free; |
| @@ -799,31 +799,7 @@ If there is no such live buffer, return nil." | |||
| 799 | (setq found (car list)))) | 799 | (setq found (car list)))) |
| 800 | (setq list (cdr list)))) | 800 | (setq list (cdr list)))) |
| 801 | found)))) | 801 | found)))) |
| 802 | 802 | ||
| 803 | (defun insert-file-contents-literally (filename &optional visit beg end replace) | ||
| 804 | "Like `insert-file-contents', but only reads in the file literally. | ||
| 805 | A buffer may be modified in several ways after reading into the buffer, | ||
| 806 | to Emacs features such as format decoding, character code | ||
| 807 | conversion, find-file-hooks, automatic uncompression, etc. | ||
| 808 | |||
| 809 | This function ensures that none of these modifications will take place." | ||
| 810 | (let ((format-alist nil) | ||
| 811 | (after-insert-file-functions nil) | ||
| 812 | (coding-system-for-read 'no-conversion) | ||
| 813 | (coding-system-for-write 'no-conversion) | ||
| 814 | (jka-compr-compression-info-list nil) | ||
| 815 | (find-buffer-file-type-function | ||
| 816 | (if (fboundp 'find-buffer-file-type) | ||
| 817 | (symbol-function 'find-buffer-file-type) | ||
| 818 | nil))) | ||
| 819 | (unwind-protect | ||
| 820 | (progn | ||
| 821 | (fset 'find-buffer-file-type (lambda (filename) t)) | ||
| 822 | (insert-file-contents filename visit beg end replace)) | ||
| 823 | (if find-buffer-file-type-function | ||
| 824 | (fset 'find-buffer-file-type find-buffer-file-type-function) | ||
| 825 | (fmakunbound 'find-buffer-file-type))))) | ||
| 826 | |||
| 827 | (defun find-file-noselect (filename &optional nowarn rawfile) | 803 | (defun find-file-noselect (filename &optional nowarn rawfile) |
| 828 | "Read file FILENAME into a buffer and return the buffer. | 804 | "Read file FILENAME into a buffer and return the buffer. |
| 829 | If a buffer exists visiting FILENAME, return that one, but | 805 | If a buffer exists visiting FILENAME, return that one, but |
| @@ -890,7 +866,11 @@ Optional second arg RAWFILE non-nil means the file is read literally." | |||
| 890 | (file-name-nondirectory filename) | 866 | (file-name-nondirectory filename) |
| 891 | (buffer-name buf)))) | 867 | (buffer-name buf)))) |
| 892 | (with-current-buffer buf | 868 | (with-current-buffer buf |
| 893 | (revert-buffer t t))))) | 869 | (revert-buffer t t))) |
| 870 | ((not (eq rawfile (not (null find-file-literally)))) | ||
| 871 | (if rawfile | ||
| 872 | (message "File is already visited, and not literally") | ||
| 873 | (message "File is already visited, and visited literally"))))) | ||
| 894 | (save-excursion | 874 | (save-excursion |
| 895 | ;;; The truename stuff makes this obsolete. | 875 | ;;; The truename stuff makes this obsolete. |
| 896 | ;;; (let* ((link-name (car (file-attributes filename))) | 876 | ;;; (let* ((link-name (car (file-attributes filename))) |
| @@ -941,10 +921,55 @@ Optional second arg RAWFILE non-nil means the file is read literally." | |||
| 941 | (make-local-variable 'backup-inhibited) | 921 | (make-local-variable 'backup-inhibited) |
| 942 | (setq backup-inhibited t))) | 922 | (setq backup-inhibited t))) |
| 943 | (if rawfile | 923 | (if rawfile |
| 944 | nil | 924 | (progn |
| 925 | (setq enable-multibyte-characters nil) | ||
| 926 | (make-local-variable 'find-file-literally) | ||
| 927 | (setq find-file-literally t)) | ||
| 945 | (after-find-file error (not nowarn)) | 928 | (after-find-file error (not nowarn)) |
| 946 | (setq buf (current-buffer))))) | 929 | (setq buf (current-buffer))))) |
| 947 | buf))) | 930 | buf))) |
| 931 | |||
| 932 | (defun insert-file-contents-literally (filename &optional visit beg end replace) | ||
| 933 | "Like `insert-file-contents', but only reads in the file literally. | ||
| 934 | A buffer may be modified in several ways after reading into the buffer, | ||
| 935 | to Emacs features such as format decoding, character code | ||
| 936 | conversion, find-file-hooks, automatic uncompression, etc. | ||
| 937 | |||
| 938 | This function ensures that none of these modifications will take place." | ||
| 939 | (let ((format-alist nil) | ||
| 940 | (after-insert-file-functions nil) | ||
| 941 | (coding-system-for-read 'no-conversion) | ||
| 942 | (coding-system-for-write 'no-conversion) | ||
| 943 | (jka-compr-compression-info-list nil) | ||
| 944 | (find-buffer-file-type-function | ||
| 945 | (if (fboundp 'find-buffer-file-type) | ||
| 946 | (symbol-function 'find-buffer-file-type) | ||
| 947 | nil))) | ||
| 948 | (unwind-protect | ||
| 949 | (progn | ||
| 950 | (fset 'find-buffer-file-type (lambda (filename) t)) | ||
| 951 | (insert-file-contents filename visit beg end replace)) | ||
| 952 | (if find-buffer-file-type-function | ||
| 953 | (fset 'find-buffer-file-type find-buffer-file-type-function) | ||
| 954 | (fmakunbound 'find-buffer-file-type))))) | ||
| 955 | |||
| 956 | (defun insert-file-literally (filename) | ||
| 957 | "Insert contents of file FILENAME into buffer after point with no conversion. | ||
| 958 | |||
| 959 | This function is meant for the user to run interactively. | ||
| 960 | Don't call it from programs! Use `insert-file-contents-literally' instead. | ||
| 961 | \(Its calling sequence is different; see its documentation)." | ||
| 962 | (interactive "*fInsert file literally: ") | ||
| 963 | (if (file-directory-p filename) | ||
| 964 | (signal 'file-error (list "Opening input file" "file is a directory" | ||
| 965 | filename))) | ||
| 966 | (let ((tem (insert-file-contents-literally filename))) | ||
| 967 | (push-mark (+ (point) (car (cdr tem)))))) | ||
| 968 | |||
| 969 | (defvar find-file-literally nil | ||
| 970 | "Non-nil if this buffer was made by `find-file-literally' or equivalent. | ||
| 971 | This is a permanent local.") | ||
| 972 | (put 'find-file-literally 'permanent-local t) | ||
| 948 | 973 | ||
| 949 | (defun find-file-literally (filename) | 974 | (defun find-file-literally (filename) |
| 950 | "Visit file FILENAME with no conversion of any kind. | 975 | "Visit file FILENAME with no conversion of any kind. |
| @@ -952,11 +977,18 @@ Format conversion and character code conversion are both disabled, | |||
| 952 | and multibyte characters are disabled in the resulting buffer. | 977 | and multibyte characters are disabled in the resulting buffer. |
| 953 | The major mode used is Fundamental mode regardless of the file name, | 978 | The major mode used is Fundamental mode regardless of the file name, |
| 954 | and local variable specifications in the file are ignored. | 979 | and local variable specifications in the file are ignored. |
| 955 | Automatic uncompression is also disabled." | 980 | Automatic uncompression is also disabled. |
| 981 | |||
| 982 | You cannot absolutely rely on this function to result in | ||
| 983 | visiting the file literally. If Emacs already has a buffer \ | ||
| 984 | which is visiting the file, you get the existing buffer, | ||
| 985 | regardless of whether it was created literally or not. | ||
| 986 | |||
| 987 | In a Lisp program, if you want to be sure of accessing a file's | ||
| 988 | contents literally, you should create a temporary buffer and then read | ||
| 989 | the file contents into it using `insert-file-contents-literally'." | ||
| 956 | (interactive "FFind file literally: ") | 990 | (interactive "FFind file literally: ") |
| 957 | (prog1 | 991 | (switch-to-buffer (find-file-noselect filename nil t))) |
| 958 | (switch-to-buffer (find-file-noselect filename nil t)) | ||
| 959 | (setq enable-multibyte-characters nil))) | ||
| 960 | 992 | ||
| 961 | (defvar after-find-file-from-revert-buffer nil) | 993 | (defvar after-find-file-from-revert-buffer nil) |
| 962 | 994 | ||