aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorColin Walters2002-06-08 20:58:59 +0000
committerColin Walters2002-06-08 20:58:59 +0000
commit447404a34f72c0fc3ee07e7b010f1e6bc6bb4a6a (patch)
treef1dda778645b15272d27ad3edb81a71502f3f636
parenta176c9ebfbd7bfb36342de647e5f056588fd5b82 (diff)
downloademacs-447404a34f72c0fc3ee07e7b010f1e6bc6bb4a6a.tar.gz
emacs-447404a34f72c0fc3ee07e7b010f1e6bc6bb4a6a.zip
(sgml-html-meta-auto-coding-function): New function.
(auto-coding-from-file-contents): Delete; merge functionality into `set-auto-coding'. (set-auto-coding): Move tests from `auto-coding-functions' so that they have a lower priority than coding: tags. Put `auto-coding-regexp-alist' tests before coding: tag tests. (sgml-xml-auto-coding-function): Simply `intern' the match, and test if it's a valid coding system. (auto-coding-functions): Add `sgml-html-meta-auto-coding-function'.
-rw-r--r--lisp/international/mule.el86
1 files changed, 51 insertions, 35 deletions
diff --git a/lisp/international/mule.el b/lisp/international/mule.el
index 694e8358527..f9ec6cdfc49 100644
--- a/lisp/international/mule.el
+++ b/lisp/international/mule.el
@@ -1490,7 +1490,8 @@ and the contents of `file-coding-system-alist'."
1490 (symbol :tag "Coding system")))) 1490 (symbol :tag "Coding system"))))
1491 1491
1492;; See the bottom of this file for built-in auto coding functions. 1492;; See the bottom of this file for built-in auto coding functions.
1493(defcustom auto-coding-functions '(sgml-xml-auto-coding-function) 1493(defcustom auto-coding-functions '(sgml-xml-auto-coding-function
1494 sgml-html-meta-auto-coding-function)
1494 "A list of functions which attempt to determine a coding system. 1495 "A list of functions which attempt to determine a coding system.
1495 1496
1496Each function in this list should be written to operate on the current 1497Each function in this list should be written to operate on the current
@@ -1499,8 +1500,8 @@ argument SIZE, past which it should not search. If a function
1499succeeds in determining a coding system, it should return that coding 1500succeeds in determining a coding system, it should return that coding
1500system. Otherwise, it should return nil. 1501system. Otherwise, it should return nil.
1501 1502
1502The functions in this list take priority over `coding:' tags in the 1503Any `coding:' tags present have a higher priority than the
1503file, just as for `auto-coding-regexp-alist'." 1504functions in this list."
1504 :group 'files 1505 :group 'files
1505 :group 'mule 1506 :group 'mule
1506 :type '(repeat function)) 1507 :type '(repeat function))
@@ -1520,26 +1521,6 @@ This is used for loading and byte-compiling Emacs Lisp files.")
1520 (setq alist (cdr alist)))) 1521 (setq alist (cdr alist))))
1521 coding-system)) 1522 coding-system))
1522 1523
1523(defun auto-coding-from-file-contents (size)
1524 "Determine a coding system from the contents of the current buffer.
1525The current buffer contains SIZE bytes starting at point.
1526Value is either a coding system or nil."
1527 (save-excursion
1528 (let ((alist auto-coding-regexp-alist)
1529 (funcs auto-coding-functions)
1530 coding-system)
1531 (while (and alist (not coding-system))
1532 (let ((regexp (car (car alist))))
1533 (when (re-search-forward regexp (+ (point) size) t)
1534 (setq coding-system (cdr (car alist)))))
1535 (setq alist (cdr alist)))
1536 (while (and funcs (not coding-system))
1537 (setq coding-system (condition-case e
1538 (save-excursion
1539 (funcall (pop funcs) size))
1540 (error nil))))
1541 coding-system)))
1542
1543(defun set-auto-coding (filename size) 1524(defun set-auto-coding (filename size)
1544 "Return coding system for a file FILENAME of which SIZE bytes follow point. 1525 "Return coding system for a file FILENAME of which SIZE bytes follow point.
1545These bytes should include at least the first 1k of the file 1526These bytes should include at least the first 1k of the file
@@ -1548,11 +1529,12 @@ and the last 3k of the file, but the middle may be omitted.
1548It checks FILENAME against the variable `auto-coding-alist'. If 1529It checks FILENAME against the variable `auto-coding-alist'. If
1549FILENAME doesn't match any entries in the variable, it checks the 1530FILENAME doesn't match any entries in the variable, it checks the
1550contents of the current buffer following point against 1531contents of the current buffer following point against
1551`auto-coding-regexp-alist', and tries calling each function in 1532`auto-coding-regexp-alist'. If no match is found, it checks for a
1552`auto-coding-functions'. If no match is found, it checks for a
1553`coding:' tag in the first one or two lines following point. If no 1533`coding:' tag in the first one or two lines following point. If no
1554`coding:' tag is found, it checks for local variables list in the last 1534`coding:' tag is found, it checks for local variables list in the last
15553K bytes out of the SIZE bytes. 15353K bytes out of the SIZE bytes. Finally, if none of these methods
1536succeed, then it checks to see if any function in
1537`auto-coding-functions' gives a match.
1556 1538
1557The return value is the specified coding system, 1539The return value is the specified coding system,
1558or nil if nothing specified. 1540or nil if nothing specified.
@@ -1560,7 +1542,16 @@ or nil if nothing specified.
1560The variable `set-auto-coding-function' (which see) is set to this 1542The variable `set-auto-coding-function' (which see) is set to this
1561function by default." 1543function by default."
1562 (or (auto-coding-alist-lookup filename) 1544 (or (auto-coding-alist-lookup filename)
1563 (auto-coding-from-file-contents size) 1545 ;; Try using `auto-coding-regexp-alist'.
1546 (save-excursion
1547 (let ((alist auto-coding-regexp-alist)
1548 coding-system)
1549 (while (and alist (not coding-system))
1550 (let ((regexp (car (car alist))))
1551 (when (re-search-forward regexp (+ (point) size) t)
1552 (setq coding-system (cdr (car alist)))))
1553 (setq alist (cdr alist)))
1554 coding-system))
1564 (let* ((case-fold-search t) 1555 (let* ((case-fold-search t)
1565 (head-start (point)) 1556 (head-start (point))
1566 (head-end (+ head-start (min size 1024))) 1557 (head-end (+ head-start (min size 1024)))
@@ -1635,6 +1626,16 @@ function by default."
1635 (setq coding-system (intern (match-string 1))) 1626 (setq coding-system (intern (match-string 1)))
1636 (or (coding-system-p coding-system) 1627 (or (coding-system-p coding-system)
1637 (setq coding-system nil)))))) 1628 (setq coding-system nil))))))
1629 coding-system)
1630 ;; Finally, try all the `auto-coding-functions'.
1631 (let ((funcs auto-coding-functions)
1632 (coding-system nil))
1633 (while (and funcs (not coding-system))
1634 (setq coding-system (condition-case e
1635 (save-excursion
1636 (goto-char (point-min))
1637 (funcall (pop funcs) size))
1638 (error nil))))
1638 coding-system))) 1639 coding-system)))
1639 1640
1640(setq set-auto-coding-function 'set-auto-coding) 1641(setq set-auto-coding-function 'set-auto-coding)
@@ -1931,16 +1932,31 @@ This function is intended to be added to `auto-coding-functions'."
1931 (re-search-forward "\"\\s-*\\?>" size t)))) 1932 (re-search-forward "\"\\s-*\\?>" size t))))
1932 (when end 1933 (when end
1933 (if (re-search-forward "encoding=\"\\(.+?\\)\"" end t) 1934 (if (re-search-forward "encoding=\"\\(.+?\\)\"" end t)
1934 (let ((match (downcase (match-string 1)))) 1935 (let* ((match (match-string 1))
1935 (cond ((member match '("utf-8" "iso-2022-jp" 1936 (sym (intern (downcase match))))
1936 "euc-jp" "shift_jis")) 1937 (if (coding-system-p sym)
1937 (intern match)) 1938 sym
1938 ((string-match "iso-8859-[[:digit:]]+" match) 1939 (message "Warning: unknown coding system \"%s\"" match)
1939 (intern match)) 1940 nil))
1940 (t (message "Warning: unknown XML encoding %s" match)
1941 nil)))
1942 'utf-8))))) 1941 'utf-8)))))
1943 1942
1943(defun sgml-html-meta-auto-coding-function (size)
1944 "If the buffer has an HTML meta tag, use it to determine encoding.
1945This function is intended to be added to `auto-coding-functions'."
1946 (setq size (min size
1947 ;; Only search forward 10 lines
1948 (save-excursion
1949 (forward-line 10)
1950 (point))))
1951 (when (and (search-forward "<html>" size t)
1952 (re-search-forward "<meta\\s-+http-equiv=\"content-type\"\\s-+content=\"text/\\sw+;\\s-*charset=\\(.+?\\)\"" size t))
1953 (let* ((match (match-string 1))
1954 (sym (intern (downcase match))))
1955 (if (coding-system-p sym)
1956 sym
1957 (message "Warning: unknown coding system \"%s\"" match)
1958 nil))))
1959
1944;;; 1960;;;
1945(provide 'mule) 1961(provide 'mule)
1946 1962