aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenichi Handa2005-07-19 02:29:58 +0000
committerKenichi Handa2005-07-19 02:29:58 +0000
commit29d04c4f94fec9092a99154e5abaee387b5a5cca (patch)
tree7b0658b7967295fc84ad37d20cf46ef945080bdd
parentbcd983312cd9a5f9f5aec48821110aa7688b29b5 (diff)
downloademacs-29d04c4f94fec9092a99154e5abaee387b5a5cca.tar.gz
emacs-29d04c4f94fec9092a99154e5abaee387b5a5cca.zip
(select-safe-coding-system): Try to
use an auto-coding (if any) before anything else. If the found auto-coding is invalid, show a warning message.
-rw-r--r--lisp/international/mule-cmds.el136
1 files changed, 82 insertions, 54 deletions
diff --git a/lisp/international/mule-cmds.el b/lisp/international/mule-cmds.el
index d503eebe6d9..077a196f474 100644
--- a/lisp/international/mule-cmds.el
+++ b/lisp/international/mule-cmds.el
@@ -820,7 +820,7 @@ returned without any user interaction. See also the command
820`prefer-coding-system'. 820`prefer-coding-system'.
821 821
822However, the user is queried if the chosen coding system is 822However, the user is queried if the chosen coding system is
823inconsistent with what would be selected by `set-auto-coding' from 823inconsistent with what would be selected by `find-auto-coding' from
824coding cookies &c. if the contents of the region were read from a 824coding cookies &c. if the contents of the region were read from a
825file. (That could lead to data corruption in a file subsequently 825file. (That could lead to data corruption in a file subsequently
826re-visited and edited.) 826re-visited and edited.)
@@ -850,7 +850,33 @@ and TO is ignored."
850 (not (listp default-coding-system))) 850 (not (listp default-coding-system)))
851 (setq default-coding-system (list default-coding-system))) 851 (setq default-coding-system (list default-coding-system)))
852 852
853 (let ((no-other-defaults nil)) 853 (let ((no-other-defaults nil)
854 auto-cs)
855 (unless (or (stringp from) find-file-literally)
856 ;; Find an auto-coding that is specified for the the current
857 ;; buffer and file from the region FROM and TO.
858 (save-excursion
859 (save-restriction
860 (widen)
861 (goto-char from)
862 (setq auto-cs (find-auto-coding (or file buffer-file-name "")
863 (- to from)))
864 (if auto-cs
865 (if (coding-system-p (car auto-cs))
866 (setq auto-cs (car auto-cs))
867 (display-warning
868 :warning
869 (format "\
870Invalid coding system `%s' is specified
871for the current buffer/file by the %s.
872It is highly recommended to fix it before writing to a file."
873 (car auto-cs)
874 (if (eq (cdr auto-cs) :coding) ":coding tag"
875 (format "variable `%s'" (cdr auto-cs)))))
876 (or (yes-or-no-p "Really proceed with writing? ")
877 (error "Save aborted"))
878 (setq auto-cs nil))))))
879
854 (if (eq (car default-coding-system) t) 880 (if (eq (car default-coding-system) t)
855 (setq no-other-defaults t 881 (setq no-other-defaults t
856 default-coding-system (cdr default-coding-system))) 882 default-coding-system (cdr default-coding-system)))
@@ -860,6 +886,15 @@ and TO is ignored."
860 (mapcar (function (lambda (x) (cons x (coding-system-base x)))) 886 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
861 default-coding-system)) 887 default-coding-system))
862 888
889 (if (and auto-cs (not no-other-defaults))
890 ;; If the file has a coding cookie, try to use it before anything
891 ;; else (i.e. before default-coding-system which will typically come
892 ;; from file-coding-system-alist).
893 (let ((base (coding-system-base auto-cs)))
894 (or (memq base '(nil undecided))
895 (rassq base default-coding-system)
896 (push (cons auto-cs base) default-coding-system))))
897
863 ;; From now on, the list of defaults is reversed. 898 ;; From now on, the list of defaults is reversed.
864 (setq default-coding-system (nreverse default-coding-system)) 899 (setq default-coding-system (nreverse default-coding-system))
865 900
@@ -893,56 +928,49 @@ and TO is ignored."
893 (coding-system-get preferred 'mime-charset) 928 (coding-system-get preferred 'mime-charset)
894 (not (rassq base default-coding-system)) 929 (not (rassq base default-coding-system))
895 (push (cons preferred base) 930 (push (cons preferred base)
896 default-coding-system))))) 931 default-coding-system))))
897 932
898 (if select-safe-coding-system-accept-default-p 933 (if select-safe-coding-system-accept-default-p
899 (setq accept-default-p select-safe-coding-system-accept-default-p)) 934 (setq accept-default-p select-safe-coding-system-accept-default-p))
900 935
901 (let ((codings (find-coding-systems-region from to)) 936 (let ((codings (find-coding-systems-region from to))
902 (coding-system nil) 937 (coding-system nil)
903 safe rejected unsafe) 938 safe rejected unsafe)
904 (if (eq (car codings) 'undecided) 939 (if (eq (car codings) 'undecided)
905 ;; Any coding system is ok. 940 ;; Any coding system is ok.
906 (setq coding-system t) 941 (setq coding-system t)
907 ;; Classify the defaults into safe, rejected, and unsafe. 942 ;; Classify the defaults into safe, rejected, and unsafe.
908 (dolist (elt default-coding-system) 943 (dolist (elt default-coding-system)
909 (if (memq (cdr elt) codings) 944 (if (memq (cdr elt) codings)
910 (if (and (functionp accept-default-p) 945 (if (and (functionp accept-default-p)
911 (not (funcall accept-default-p (cdr elt)))) 946 (not (funcall accept-default-p (cdr elt))))
912 (push (car elt) rejected) 947 (push (car elt) rejected)
913 (push (car elt) safe)) 948 (push (car elt) safe))
914 (push (car elt) unsafe))) 949 (push (car elt) unsafe)))
915 (if safe 950 (if safe
916 (setq coding-system (car safe)))) 951 (setq coding-system (car safe))))
917 952
918 ;; If all the defaults failed, ask a user. 953 ;; If all the defaults failed, ask a user.
919 (when (not coding-system) 954 (when (not coding-system)
920 (setq coding-system (select-safe-coding-system-interactively 955 (setq coding-system (select-safe-coding-system-interactively
921 from to codings unsafe rejected (car codings)))) 956 from to codings unsafe rejected (car codings))))
922 957
923 (if (vectorp (coding-system-eol-type coding-system)) 958 (if (vectorp (coding-system-eol-type coding-system))
924 (let ((eol (coding-system-eol-type buffer-file-coding-system))) 959 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
925 (if (numberp eol) 960 (if (numberp eol)
926 (setq coding-system 961 (setq coding-system
927 (coding-system-change-eol-conversion coding-system eol))))) 962 (coding-system-change-eol-conversion coding-system eol)))))
928 963
929 (if (eq coding-system t) 964 (if (eq coding-system t)
930 (setq coding-system buffer-file-coding-system)) 965 (setq coding-system buffer-file-coding-system))
931 ;; Check we're not inconsistent with what `coding:' spec &c would 966 ;; Check we're not inconsistent with what `coding:' spec &c would
932 ;; give when file is re-read. 967 ;; give when file is re-read.
933 ;; But don't do this if we explicitly ignored the cookie 968 ;; But don't do this if we explicitly ignored the cookie
934 ;; by using `find-file-literally'. 969 ;; by using `find-file-literally'.
935 (unless (or (stringp from) 970 (when (and auto-cs
936 find-file-literally 971 (not (and
937 (and coding-system 972 coding-system
938 (memq (coding-system-type coding-system) '(0 5)))) 973 (memq (coding-system-type coding-system) '(0 5)))))
939 (let ((auto-cs (save-excursion
940 (save-restriction
941 (widen)
942 (narrow-to-region from to)
943 (goto-char (point-min))
944 (set-auto-coding (or file buffer-file-name "")
945 (buffer-size))))))
946 ;; Merge coding-system and auto-cs as far as possible. 974 ;; Merge coding-system and auto-cs as far as possible.
947 (if (not coding-system) 975 (if (not coding-system)
948 (setq coding-system auto-cs) 976 (setq coding-system auto-cs)
@@ -974,8 +1002,8 @@ and TO is ignored."
974 (format "Selected encoding %s disagrees with \ 1002 (format "Selected encoding %s disagrees with \
975%s specified by file contents. Really save (else edit coding cookies \ 1003%s specified by file contents. Really save (else edit coding cookies \
976and try again)? " coding-system auto-cs)) 1004and try again)? " coding-system auto-cs))
977 (error "Save aborted"))))) 1005 (error "Save aborted"))))
978 coding-system)) 1006 coding-system)))
979 1007
980(setq select-safe-coding-system-function 'select-safe-coding-system) 1008(setq select-safe-coding-system-function 'select-safe-coding-system)
981 1009