aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorKenichi Handa1998-06-12 01:39:07 +0000
committerKenichi Handa1998-06-12 01:39:07 +0000
commit25ca8cf6d738721eefc8599a8a1c3989ace80283 (patch)
tree9dbbbb7cff580e0136f60962f22ed152af4dec13
parentb413243374cb897b48e786b4c84f4a39eb18ccef (diff)
downloademacs-25ca8cf6d738721eefc8599a8a1c3989ace80283.tar.gz
emacs-25ca8cf6d738721eefc8599a8a1c3989ace80283.zip
(set-auto-coding): Argument is changed to SIZE.
-rw-r--r--lisp/international/mule.el132
1 files changed, 68 insertions, 64 deletions
diff --git a/lisp/international/mule.el b/lisp/international/mule.el
index b085b90b70f..a1a863c064c 100644
--- a/lisp/international/mule.el
+++ b/lisp/international/mule.el
@@ -774,78 +774,82 @@ LIST is a list of coding categories ordered by priority."
774 774
775;;; FILE I/O 775;;; FILE I/O
776 776
777(defun set-auto-coding (string) 777(defun set-auto-coding (size)
778 "Return coding system for a file which has STRING at the head and tail. 778 "Return coding system for a file of which SIZE bytes follow point.
779STRING is a concatination of the first 1K-byte and
780 the last 3K-byte of the file.
781 779
782It checks for a -*- coding: tag in the first one or two lines of STRING. 780It checks for a -*- coding: tag in the first one or two lines
783If there's no coding: tag in the head, it checks local variables spec 781following point. If no coding: tag is found, it checks local
784in the tailing 3K-byte oof STRING. 782variables spec in the last 3K-byte of SIZE bytes.
785 783
786The return value is the specified coding system, 784The return value is the specified coding system,
787or nil if nothing specified. 785or nil if nothing specified.
788 786
789The variable `set-auto-coding-function' (which see) is set to this 787The variable `set-auto-coding-function' (which see) is set to this
790function by default." 788function by default."
791 (condition-case nil 789 (let* ((case-fold-search t)
792 (let ((case-fold-search t) 790 (head-start (point))
793 (len (length string)) 791 (head-end (+ head-start (min size 1024)))
794 (limit (string-match "\n" string)) 792 (tail-start (+ head-start (max (- size 3072) 0)))
795 (coding-system nil)) 793 (tail-end (+ head-start size))
796 794 coding-system head-found tail-found pos)
797 ;; At first check the head. 795 ;; Try a short cut by searching for the string "coding:" at the
798 (if limit 796 ;; head and tail of SIZE bytes.
799 (when (string-match "^#!" string) 797 (setq head-found (search-forward "coding:" head-end t))
800 ;; If the file begins with "#!" (exec interpreter 798 (if (and head-found (> head-found tail-start))
801 ;; magic), look for coding frobs in the first two lines. 799 ;; Head and tail are overlapped.
802 ;; You cannot necessarily put them in the first line of 800 (setq tail-found head-found)
803 ;; such a file without screwing up the interpreter 801 (goto-char tail-start)
804 ;; invocation. 802 (setq tail-found (search-forward "coding:" tail-end t)))
805 (setq limit (string-match "\n" string limit)) 803
806 (or limit 804 ;; At first check the head.
807 (setq limit len))) 805 (when head-found
808 (setq limit len)) 806 (goto-char head-start)
809 (when (and (string-match "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" string) 807 (setq pos (re-search-forward "[\n\r]" head-end t))
810 (< (match-beginning 2) limit)) 808 (if (and pos
811 (setq coding-system 809 (= (char-after head-start) ?#)
812 (intern (substring string (match-beginning 2) (match-end 2)))) 810 (= (char-after (1+ head-start)) ?!))
813 (if (not (coding-system-p coding-system)) 811 ;; If the file begins with "#!" (exec interpreter magic),
814 (setq coding-system nil))) 812 ;; look for coding frobs in the first two lines. You cannot
815 813 ;; necessarily put them in the first line of such a file
816 ;; If no coding system is specified in the head, check the tail. 814 ;; without screwing up the interpreter invocation.
817 (when (and (not coding-system) 815 (setq pos (search-forward "\n" head-end t)))
818 (let ((idx (if (> len 3000) (- len 3000) 0)) 816 (if pos (setq head-end pos))
819 start) 817 (when (< head-found head-end)
820 (while (setq start (string-match "\n\^L" string idx)) 818 (goto-char head-start)
821 (setq idx (+ start 2))) 819 (if (re-search-forward
822 (string-match 820 "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" head-end t)
823 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" 821 (progn
824 string idx))) 822 (setq coding-system (intern (match-string 2)))
825 ;; The prefix is what comes before "local variables:" in its line.
826 ;; The suffix is what comes after "local variables:" in its line.
827 (let* ((idx (1+ (match-end 0)))
828 (prefix (regexp-quote
829 (substring string
830 (match-beginning 1) (match-end 1))))
831 (suffix (regexp-quote
832 (substring string
833 (match-beginning 2) (match-end 2))))
834 (re-coding (concat "^" prefix
835 "coding[ \t]*:[ \t]*\\([^ \t\n]+\\)[ \t]*"
836 suffix "$"))
837 (re-end (concat "^" prefix "end *:[ \t]*" suffix "$"))
838 (limit (or (string-match re-end string idx) len)))
839 (when (and (setq idx (string-match re-coding string idx))
840 (< idx limit))
841 (setq coding-system
842 (intern (substring string
843 (match-beginning 1) (match-end 1))))
844 (or (coding-system-p coding-system) 823 (or (coding-system-p coding-system)
845 (setq coding-system nil))))) 824 (setq coding-system nil))))))
846 825
847 coding-system) 826 ;; If no coding: tag in the head, check the tail.
848 (error nil))) 827 (when (and tail-found (not coding-system))
828 (goto-char tail-start)
829 (search-forward "\n\^L" nil t)
830 (if (re-search-forward
831 "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" tail-end t)
832 ;; The prefix is what comes before "local variables:" in its
833 ;; line. The suffix is what comes after "local variables:"
834 ;; in its line.
835 (let* ((prefix (regexp-quote (match-string 1)))
836 (suffix (regexp-quote (match-string 2)))
837 (re-coding (concat
838 "^" prefix
839 "coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*"
840 suffix "$"))
841 (re-end (concat
842 "^" prefix "end *:[ \t]*" suffix "$"))
843 (pos (point)))
844 (re-search-forward re-end tail-end 'move)
845 (setq tail-end (point))
846 (goto-char pos)
847 (if (re-search-forward re-coding tail-end t)
848 (progn
849 (setq coding-system (intern (match-string 1)))
850 (or (coding-system-p coding-system)
851 (setq coding-system nil)))))))
852 coding-system))
849 853
850(setq set-auto-coding-function 'set-auto-coding) 854(setq set-auto-coding-function 'set-auto-coding)
851 855