diff options
| -rw-r--r-- | lisp/international/mule.el | 132 |
1 files changed, 64 insertions, 68 deletions
diff --git a/lisp/international/mule.el b/lisp/international/mule.el index a1a863c064c..b085b90b70f 100644 --- a/lisp/international/mule.el +++ b/lisp/international/mule.el | |||
| @@ -774,82 +774,78 @@ 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 (size) | 777 | (defun set-auto-coding (string) |
| 778 | "Return coding system for a file of which SIZE bytes follow point. | 778 | "Return coding system for a file which has STRING at the head and tail. |
| 779 | STRING is a concatination of the first 1K-byte and | ||
| 780 | the last 3K-byte of the file. | ||
| 779 | 781 | ||
| 780 | It checks for a -*- coding: tag in the first one or two lines | 782 | It checks for a -*- coding: tag in the first one or two lines of STRING. |
| 781 | following point. If no coding: tag is found, it checks local | 783 | If there's no coding: tag in the head, it checks local variables spec |
| 782 | variables spec in the last 3K-byte of SIZE bytes. | 784 | in the tailing 3K-byte oof STRING. |
| 783 | 785 | ||
| 784 | The return value is the specified coding system, | 786 | The return value is the specified coding system, |
| 785 | or nil if nothing specified. | 787 | or nil if nothing specified. |
| 786 | 788 | ||
| 787 | The variable `set-auto-coding-function' (which see) is set to this | 789 | The variable `set-auto-coding-function' (which see) is set to this |
| 788 | function by default." | 790 | function by default." |
| 789 | (let* ((case-fold-search t) | 791 | (condition-case nil |
| 790 | (head-start (point)) | 792 | (let ((case-fold-search t) |
| 791 | (head-end (+ head-start (min size 1024))) | 793 | (len (length string)) |
| 792 | (tail-start (+ head-start (max (- size 3072) 0))) | 794 | (limit (string-match "\n" string)) |
| 793 | (tail-end (+ head-start size)) | 795 | (coding-system nil)) |
| 794 | coding-system head-found tail-found pos) | 796 | |
| 795 | ;; Try a short cut by searching for the string "coding:" at the | 797 | ;; At first check the head. |
| 796 | ;; head and tail of SIZE bytes. | 798 | (if limit |
| 797 | (setq head-found (search-forward "coding:" head-end t)) | 799 | (when (string-match "^#!" string) |
| 798 | (if (and head-found (> head-found tail-start)) | 800 | ;; If the file begins with "#!" (exec interpreter |
| 799 | ;; Head and tail are overlapped. | 801 | ;; magic), look for coding frobs in the first two lines. |
| 800 | (setq tail-found head-found) | 802 | ;; You cannot necessarily put them in the first line of |
| 801 | (goto-char tail-start) | 803 | ;; such a file without screwing up the interpreter |
| 802 | (setq tail-found (search-forward "coding:" tail-end t))) | 804 | ;; invocation. |
| 803 | 805 | (setq limit (string-match "\n" string limit)) | |
| 804 | ;; At first check the head. | 806 | (or limit |
| 805 | (when head-found | 807 | (setq limit len))) |
| 806 | (goto-char head-start) | 808 | (setq limit len)) |
| 807 | (setq pos (re-search-forward "[\n\r]" head-end t)) | 809 | (when (and (string-match "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" string) |
| 808 | (if (and pos | 810 | (< (match-beginning 2) limit)) |
| 809 | (= (char-after head-start) ?#) | 811 | (setq coding-system |
| 810 | (= (char-after (1+ head-start)) ?!)) | 812 | (intern (substring string (match-beginning 2) (match-end 2)))) |
| 811 | ;; If the file begins with "#!" (exec interpreter magic), | 813 | (if (not (coding-system-p coding-system)) |
| 812 | ;; look for coding frobs in the first two lines. You cannot | 814 | (setq coding-system nil))) |
| 813 | ;; necessarily put them in the first line of such a file | 815 | |
| 814 | ;; without screwing up the interpreter invocation. | 816 | ;; If no coding system is specified in the head, check the tail. |
| 815 | (setq pos (search-forward "\n" head-end t))) | 817 | (when (and (not coding-system) |
| 816 | (if pos (setq head-end pos)) | 818 | (let ((idx (if (> len 3000) (- len 3000) 0)) |
| 817 | (when (< head-found head-end) | 819 | start) |
| 818 | (goto-char head-start) | 820 | (while (setq start (string-match "\n\^L" string idx)) |
| 819 | (if (re-search-forward | 821 | (setq idx (+ start 2))) |
| 820 | "-\\*-\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" head-end t) | 822 | (string-match |
| 821 | (progn | 823 | "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" |
| 822 | (setq coding-system (intern (match-string 2))) | 824 | string idx))) |
| 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)))) | ||
| 823 | (or (coding-system-p coding-system) | 844 | (or (coding-system-p coding-system) |
| 824 | (setq coding-system nil)))))) | 845 | (setq coding-system nil))))) |
| 825 | 846 | ||
| 826 | ;; If no coding: tag in the head, check the tail. | 847 | coding-system) |
| 827 | (when (and tail-found (not coding-system)) | 848 | (error nil))) |
| 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)) | ||
| 853 | 849 | ||
| 854 | (setq set-auto-coding-function 'set-auto-coding) | 850 | (setq set-auto-coding-function 'set-auto-coding) |
| 855 | 851 | ||