diff options
| author | Kenichi Handa | 1997-09-01 07:19:38 +0000 |
|---|---|---|
| committer | Kenichi Handa | 1997-09-01 07:19:38 +0000 |
| commit | 6356130450fca57f06a0340c5b26aaa136b1d1d9 (patch) | |
| tree | ebc089e96e818c042a4fd9261b69f7e6c5b7dbd3 | |
| parent | f5c7c0ebafa7f1861c879a9c78ada7bea611599f (diff) | |
| download | emacs-6356130450fca57f06a0340c5b26aaa136b1d1d9.tar.gz emacs-6356130450fca57f06a0340c5b26aaa136b1d1d9.zip | |
(set-auto-coding): Name changed from
auto-file-coding-system. The argument STRING is now a
concatination of the heading 1K-byte and the tailing 3K-byte of a
file.
(set-auto-coding-function): Set it to `set-auto-coding'.
| -rw-r--r-- | lisp/international/mule.el | 96 |
1 files changed, 71 insertions, 25 deletions
diff --git a/lisp/international/mule.el b/lisp/international/mule.el index 30063dd5a9a..05631e1a1bc 100644 --- a/lisp/international/mule.el +++ b/lisp/international/mule.el | |||
| @@ -613,34 +613,80 @@ LIST is a list of coding-categories ordered by priority." | |||
| 613 | 613 | ||
| 614 | ;;; FILE I/O | 614 | ;;; FILE I/O |
| 615 | 615 | ||
| 616 | (defun auto-file-coding-system (head-lines) | 616 | (defun set-auto-coding (string) |
| 617 | "Return coding system for a file which has HEAD-LINES at the head. | 617 | "Return coding system for a file which has STRING at the head and tail. |
| 618 | HEAD-LINES is a string of the first two lines of the file. | 618 | STRING is a concatination of the first 1K-byte and |
| 619 | This checks for a -*- coding tag in the buffers's text, | 619 | the last 3K-byte of the file. |
| 620 | and return the specified coding system. | 620 | |
| 621 | It checks for a -*- coding: tag in the first one or two lines of STRING. | ||
| 622 | If there's no coding: tag in the head, it checks local variables spec | ||
| 623 | in the tailing 3K-byte oof STRING. | ||
| 624 | |||
| 625 | The return value is the specified coding system, | ||
| 626 | or nil if nothing specified. | ||
| 621 | 627 | ||
| 622 | The variable `auto-file-coding-system' (which see) is set to this | 628 | The variable `auto-file-coding-system' (which see) is set to this |
| 623 | function by default." | 629 | function by default." |
| 624 | (let ((limit (string-match "\n" head-lines)) | 630 | (condition-case nil |
| 625 | (coding-system nil)) | 631 | (let ((case-fold-search t) |
| 626 | (if limit | 632 | (len (length string)) |
| 627 | (when (string-match "^#!" head-lines) | 633 | (limit (string-match "\n" string)) |
| 628 | ;; If the file begins with "#!" (exec interpreter magic), | 634 | (coding-system nil)) |
| 629 | ;; look for coding frobs in the first two lines. You cannot | 635 | |
| 630 | ;; necessarily put them in the first line of such a file | 636 | ;; At first check the head. |
| 631 | ;; without screwing up the interpreter invocation. | 637 | (if limit |
| 632 | (setq limit (string-match "\n" head-lines limit)) | 638 | (when (string-match "^#!" string) |
| 633 | (or limit | 639 | ;; If the file begins with "#!" (exec interpreter |
| 634 | (setq limit (length head-lines)))) | 640 | ;; magic), look for coding frobs in the first two lines. |
| 635 | (setq limit (length head-lines))) | 641 | ;; You cannot necessarily put them in the first line of |
| 636 | (when (and (string-match "-\\*-[ \t]*coding:[ \t]*\\([^ ;]+\\)" head-lines) | 642 | ;; such a file without screwing up the interpreter |
| 637 | (< (match-beginning 1) limit)) | 643 | ;; invocation. |
| 638 | (setq coding-system | 644 | (setq limit (string-match "\n" string limit)) |
| 639 | (intern (substring head-lines (match-beginning 1) (match-end 1)))) | 645 | (or limit |
| 640 | (if (coding-system-p coding-system) | 646 | (setq limit len))) |
| 641 | coding-system)))) | 647 | (setq limit len)) |
| 642 | 648 | (when (and (string-match "-\\*-[ \t]*coding:[ \t]*\\([^ ;]+\\)" string) | |
| 643 | (setq auto-file-coding-system-function 'auto-file-coding-system) | 649 | (< (match-beginning 1) limit)) |
| 650 | (setq coding-system | ||
| 651 | (intern (substring string (match-beginning 1) (match-end 1)))) | ||
| 652 | (if (not (coding-system-p coding-system)) | ||
| 653 | (setq coding-system nil))) | ||
| 654 | |||
| 655 | ;; If no coding system is specified in the head, check the tail. | ||
| 656 | (when (and (not coding-system) | ||
| 657 | (let ((idx (if (> len 3000) (- len 3000) 0)) | ||
| 658 | start) | ||
| 659 | (while (setq start (string-match "\n\^L" string idx)) | ||
| 660 | (setq idx (+ start 2))) | ||
| 661 | (string-match | ||
| 662 | "^\\(.*\\)[ \t]*Local Variables:[ \t]*\\(.*\\)$" | ||
| 663 | string idx))) | ||
| 664 | ;; The prefix is what comes before "local variables:" in its line. | ||
| 665 | ;; The suffix is what comes after "local variables:" in its line. | ||
| 666 | (let* ((idx (1+ (match-end 0))) | ||
| 667 | (prefix (regexp-quote | ||
| 668 | (substring string | ||
| 669 | (match-beginning 1) (match-end 1)))) | ||
| 670 | (suffix (regexp-quote | ||
| 671 | (substring string | ||
| 672 | (match-beginning 2) (match-end 2)))) | ||
| 673 | (re-coding (concat "^" prefix | ||
| 674 | "coding[ \t]*:[ \t]*\\([^ \t]+\\)[ \t]*" | ||
| 675 | suffix "$")) | ||
| 676 | (re-end (concat "^" prefix "end *:[ \t]*" suffix "$")) | ||
| 677 | (limit (or (string-match re-end string idx) len))) | ||
| 678 | (when (and (setq idx (string-match re-coding string idx)) | ||
| 679 | (< idx limit)) | ||
| 680 | (setq coding-system | ||
| 681 | (intern (substring string | ||
| 682 | (match-beginning 1) (match-end 1)))) | ||
| 683 | (or (coding-system-p coding-system) | ||
| 684 | (setq coding-system nil))))) | ||
| 685 | |||
| 686 | coding-system) | ||
| 687 | (error nil))) | ||
| 688 | |||
| 689 | (setq set-auto-coding-function 'set-auto-coding) | ||
| 644 | 690 | ||
| 645 | ;; Set buffer-file-coding-system of the current buffer after some text | 691 | ;; Set buffer-file-coding-system of the current buffer after some text |
| 646 | ;; is inserted. | 692 | ;; is inserted. |