1621 |
(setq alist (cdr alist)))) |
(setq alist (cdr alist)))) |
1622 |
coding-system)) |
coding-system)) |
1623 |
|
|
1624 |
(defun set-auto-coding (filename size) |
(defun find-auto-coding (filename size) |
1625 |
"Return coding system for a file FILENAME of which SIZE bytes follow point. |
"Find a coding system for a file FILENAME of which SIZE bytes follow point. |
1626 |
These bytes should include at least the first 1k of the file |
These bytes should include at least the first 1k of the file |
1627 |
and the last 3k of the file, but the middle may be omitted. |
and the last 3k of the file, but the middle may be omitted. |
1628 |
|
|
1636 |
succeed, it checks to see if any function in `auto-coding-functions' |
succeed, it checks to see if any function in `auto-coding-functions' |
1637 |
gives a match. |
gives a match. |
1638 |
|
|
1639 |
The return value is the specified coding system, or nil if nothing is |
If a coding system is specifed, the return value is a |
1640 |
specified. |
cons (CODING . SOURCE), where CODING is the specified coding |
1641 |
|
system and SOURCE is a symbol `auto-coding-alist', |
1642 |
|
`auto-coding-regexp-alist', `coding:', or `auto-coding-functions' |
1643 |
|
indicating by what CODING is specified. Note that the validity |
1644 |
|
of CODING is not checked; it's callers responsibility to check |
1645 |
|
it. |
1646 |
|
|
1647 |
|
If nothing is specified, the return value is nil. |
1648 |
|
|
1649 |
The variable `set-auto-coding-function' (which see) is set to this |
The variable `set-auto-coding-function' (which see) is set to this |
1650 |
function by default." |
function by default." |
1651 |
(or (auto-coding-alist-lookup filename) |
(or (let ((coding-system (auto-coding-alist-lookup filename))) |
1652 |
|
(if coding-system |
1653 |
|
(cons coding-system 'auto-coding-alist))) |
1654 |
;; Try using `auto-coding-regexp-alist'. |
;; Try using `auto-coding-regexp-alist'. |
1655 |
(save-excursion |
(save-excursion |
1656 |
(let ((alist auto-coding-regexp-alist) |
(let ((alist auto-coding-regexp-alist) |
1660 |
(when (re-search-forward regexp (+ (point) size) t) |
(when (re-search-forward regexp (+ (point) size) t) |
1661 |
(setq coding-system (cdr (car alist))))) |
(setq coding-system (cdr (car alist))))) |
1662 |
(setq alist (cdr alist))) |
(setq alist (cdr alist))) |
1663 |
coding-system)) |
(if coding-system |
1664 |
|
(cons coding-system 'auto-coding-regexp-alist)))) |
1665 |
(let* ((case-fold-search t) |
(let* ((case-fold-search t) |
1666 |
(head-start (point)) |
(head-start (point)) |
1667 |
(head-end (+ head-start (min size 1024))) |
(head-end (+ head-start (min size 1024))) |
1695 |
(re-search-forward |
(re-search-forward |
1696 |
"\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" |
"\\(.*;\\)?[ \t]*coding:[ \t]*\\([^ ;]+\\)" |
1697 |
head-end t)) |
head-end t)) |
1698 |
(setq coding-system (intern (match-string 2))) |
(setq coding-system (intern (match-string 2)))))) |
|
(or (coding-system-p coding-system) |
|
|
(setq coding-system nil))))) |
|
1699 |
|
|
1700 |
;; If no coding: tag in the head, check the tail. |
;; If no coding: tag in the head, check the tail. |
1701 |
;; Here we must pay attention to the case that the end-of-line |
;; Here we must pay attention to the case that the end-of-line |
1736 |
(setq coding-system 'raw-text)) |
(setq coding-system 'raw-text)) |
1737 |
(when (and (not coding-system) |
(when (and (not coding-system) |
1738 |
(re-search-forward re-coding tail-end t)) |
(re-search-forward re-coding tail-end t)) |
1739 |
(setq coding-system (intern (match-string 1))) |
(setq coding-system (intern (match-string 1))))))) |
1740 |
(or (coding-system-p coding-system) |
(if coding-system |
1741 |
(setq coding-system nil)))))) |
(cons coding-system :coding))) |
|
coding-system) |
|
1742 |
;; Finally, try all the `auto-coding-functions'. |
;; Finally, try all the `auto-coding-functions'. |
1743 |
(let ((funcs auto-coding-functions) |
(let ((funcs auto-coding-functions) |
1744 |
(coding-system nil)) |
(coding-system nil)) |
1748 |
(goto-char (point-min)) |
(goto-char (point-min)) |
1749 |
(funcall (pop funcs) size)) |
(funcall (pop funcs) size)) |
1750 |
(error nil)))) |
(error nil)))) |
1751 |
coding-system))) |
(if coding-system |
1752 |
|
(cons coding-system 'auto-coding-functions))))) |
1753 |
|
|
1754 |
|
(defun set-auto-coding (filename size) |
1755 |
|
"Return coding system for a file FILENAME of which SIZE bytes follow point. |
1756 |
|
See `find-auto-coding' for how the coding system is found. |
1757 |
|
Return nil if an invalid coding system is found." |
1758 |
|
(let ((found (find-auto-coding filename size))) |
1759 |
|
(if (and found (coding-system-p (car found))) |
1760 |
|
(car found)))) |
1761 |
|
|
1762 |
(setq set-auto-coding-function 'set-auto-coding) |
(setq set-auto-coding-function 'set-auto-coding) |
1763 |
|
|