comparison lisp/files.el @ 19305:030c40d12288

(insert-file-literally): New command.
author Richard M. Stallman <rms@gnu.org>
date Mon, 11 Aug 1997 22:32:01 +0000
parents da8774e1207b
children 608acf74b95b
comparison
equal deleted inserted replaced
19304:a2327a135fe7 19305:030c40d12288
686 (setq buffer-file-truename otrue) 686 (setq buffer-file-truename otrue)
687 (lock-buffer) 687 (lock-buffer)
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;
695 otherwise a string <2> or <3> or ... is appended to get an unused name." 695 otherwise a string <2> or <3> or ... is appended to get an unused name."
696 (let ((lastname (file-name-nondirectory filename))) 696 (let ((lastname (file-name-nondirectory filename)))
797 (equal (nthcdr 10 (file-attributes buffer-file-name)) 797 (equal (nthcdr 10 (file-attributes buffer-file-name))
798 number)) 798 number))
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
830 verify that the file has not changed since visited or saved. 806 verify that the file has not changed since visited or saved.
831 The buffer is not selected, just returned to the caller. 807 The buffer is not selected, just returned to the caller.
888 "File %s changed on disk. Discard your edits in %s? " 864 "File %s changed on disk. Discard your edits in %s? "
889 "File %s changed on disk. Reread from disk into %s? ") 865 "File %s changed on disk. Reread from disk into %s? ")
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)))
897 ;;; (linked-buf (and (stringp link-name) 877 ;;; (linked-buf (and (stringp link-name)
898 ;;; (get-file-buffer link-name)))) 878 ;;; (get-file-buffer link-name))))
939 (and (not (funcall backup-enable-predicate buffer-file-name)) 919 (and (not (funcall backup-enable-predicate buffer-file-name))
940 (progn 920 (progn
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.
951 Format conversion and character code conversion are both disabled, 976 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
963 (defun after-find-file (&optional error warn noauto 995 (defun after-find-file (&optional error warn noauto
964 after-find-file-from-revert-buffer 996 after-find-file-from-revert-buffer