;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
;; Licensed to the Free Software Foundation.
-;; Copyright (C) 2001 Free Software Foundation, Inc.
+;; Copyright (C) 2001, 2002 Free Software Foundation, Inc.
;; Keywords: mule, multilingual, character set, coding system
"Return character specified by coded character set CCS and CODE-POINT in it.
Return nil if such a character is not supported.
Currently the only supported coded character set is `ucs' (ISO/IEC
-10646: Universal Multi-Octet Coded Character Set).
+10646: Universal Multi-Octet Coded Character Set), and the result is
+translated through the char table `utf-8-translation-table-for-decode'.
Optional argument RESTRICTION specifies a way to map the pair of CCS
and CODE-POINT to a character. Currently not supported and just ignored."
- (cond ((eq ccs 'ucs)
- (cond ((< code-point 160)
- code-point)
- ((< code-point 256)
- (make-char 'latin-iso8859-1 code-point))
- ((< code-point #x2500)
- (setq code-point (- code-point #x0100))
- (make-char 'mule-unicode-0100-24ff
- (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
- ((< code-point #x3400)
- (setq code-point (- code-point #x2500))
- (make-char 'mule-unicode-2500-33ff
- (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
- ((and (>= code-point #xe000) (< code-point #x10000))
- (setq code-point (- code-point #xe000))
- (make-char 'mule-unicode-e000-ffff
- (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
- ))))
+ (cond
+ ((eq ccs 'ucs)
+ (let ((c (cond
+ ((< code-point 160)
+ code-point)
+ ((< code-point 256)
+ (make-char 'latin-iso8859-1 code-point))
+ ((< code-point #x2500)
+ (setq code-point (- code-point #x0100))
+ (make-char 'mule-unicode-0100-24ff
+ (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
+ ((< code-point #x3400)
+ (setq code-point (- code-point #x2500))
+ (make-char 'mule-unicode-2500-33ff
+ (+ (/ code-point 96) 32) (+ (% code-point 96) 32)))
+ ((and (>= code-point #xe000) (< code-point #x10000))
+ (setq code-point (- code-point #xe000))
+ (make-char 'mule-unicode-e000-ffff
+ (+ (/ code-point 96) 32) (+ (% code-point 96) 32))))))
+ (if (and c (aref utf-8-translation-table-for-decode c))
+ (aref utf-8-translation-table-for-decode c)
+ c)))))
(defun encode-char (char ccs &optional restriction)
"Return code-point in coded character set CCS that corresponds to CHAR.
Return nil if CHAR is not included in CCS.
Currently the only supported coded character set is `ucs' (ISO/IEC
-10646: Universal Multi-Octet Coded Character Set).
+10646: Universal Multi-Octet Coded Character Set), and CHAR is first
+translated through the char-table `ucs-mule-to-mule-unicode'.
CHAR should be in one of these charsets:
ascii, latin-iso8859-1, mule-unicode-0100-24ff, mule-unicode-2500-33ff,
Optional argument RESTRICTION specifies a way to map CHAR to a
code-point in CCS. Currently not supported and just ignored."
(let* ((split (split-char char))
- (charset (car split)))
+ (charset (car split))
+ trans)
(cond ((eq ccs 'ucs)
+ (setq trans (aref ucs-mule-to-mule-unicode char))
+ (if trans
+ (setq split (split-char trans)
+ charset (car split)))
(cond ((eq charset 'ascii)
char)
((eq charset 'latin-iso8859-1)
following properties are recognized:
o post-read-conversion
-
+
The value is a function to call after some text is inserted and
decoded by the coding system itself and before any functions in
`after-insert-functions' are called. The argument of this
function is the same as for a function in
`after-insert-file-functions', i.e. LENGTH of the text inserted,
with point at the head of the text to be decoded.
-
+
o pre-write-conversion
-
+
The value is a function to call after all functions in
`write-region-annotate-functions' and `buffer-file-format' are
called, and before the text is encoded by the coding system itself.
The arguments to this function are the same as those of a function
in `write-region-annotate-functions', i.e. FROM and TO, specifying
a region of text.
-
+
o translation-table-for-decode
-
+
The value is a translation table to be applied on decoding. See
the function `make-translation-table' for the format of translation
table. This is not applicable to type 4 (CCL-based) coding systems.
-
+
o translation-table-for-encode
-
+
The value is a translation table to be applied on encoding. This is
not applicable to type 4 (CCL-based) coding systems.
-
+
o safe-chars
-
+
The value is a char table. If a character has non-nil value in it,
the character is safely supported by the coding system. This
overrides the specification of safe-charsets.
mean that the charset can't be encoded in the coding system;
it just means that some other receiver of text encoded
in the coding system won't be able to handle that charset.
-
+
o mime-charset
-
+
The value is a symbol whose name is the `MIME-charset' parameter of
the coding system.
-
+
o valid-codes (meaningful only for a coding system based on CCL)
-
+
The value is a list to indicate valid byte ranges of the encoded
file. Each element of the list is an integer or a cons of integer.
In the former case, the integer value is a valid byte code. In the
(defun define-coding-system-alias (alias coding-system)
"Define ALIAS as an alias for coding system CODING-SYSTEM."
(put alias 'coding-system (coding-system-spec coding-system))
- (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
(add-to-coding-system-list alias)
(setq coding-system-alist (cons (list (symbol-name alias))
coding-system-alist))
(let ((eol-type (coding-system-eol-type coding-system)))
(if (vectorp eol-type)
- (put alias 'eol-type (make-subsidiary-coding-system alias))
+ (progn
+ (nconc (coding-system-get alias 'alias-coding-systems) (list alias))
+ (put alias 'eol-type (make-subsidiary-coding-system alias)))
(put alias 'eol-type eol-type))))
(defun set-buffer-file-coding-system (coding-system &optional force)
(set-buffer-modified-p modified-p)
(- (point-max) (point-min)))))
+;; If you add charsets here, be sure to modify the regexp used by
+;; ctext-pre-write-conversion to look up non-standard charsets.
(defvar non-standard-designations-alist
'(("$(0" . (big5 "big5-0" 2))
("$(1" . (big5 "big5-0" 2))
(buffer-disable-undo)
(if (stringp from)
(insert from)
- (insert-buffer-substring buf from to)))))
+ (insert-buffer-substring buf from to))
+ (setq from (point-min) to (point-max)))))
(encode-coding-region from to 'ctext-no-compositions)
;; Replace ISO-2022 charset designations with extended segments, for
;; those charsets that are not part of the official X registry.
;;; FILE I/O
(defcustom auto-coding-alist
- '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|tar\\|tgz\\)\\'" . no-conversion)
+ '(("\\.\\(arc\\|zip\\|lzh\\|zoo\\|jar\\|sx[dmicw]\\|tar\\|tgz\\)\\'" . no-conversion)
("\\.\\(gz\\|Z\\|bz\\|bz2\\|gpg\\)\\'" . no-conversion)
("/#[^/]+#\\'" . emacs-mule))
"Alist of filename patterns vs corresponding coding systems.
(symbol :tag "Coding system"))))
;; See the bottom of this file for built-in auto coding functions.
-(defcustom auto-coding-functions '(sgml-xml-auto-coding-function)
+(defcustom auto-coding-functions '(sgml-xml-auto-coding-function
+ sgml-html-meta-auto-coding-function)
"A list of functions which attempt to determine a coding system.
Each function in this list should be written to operate on the current
succeeds in determining a coding system, it should return that coding
system. Otherwise, it should return nil.
-The functions in this list take priority over `coding:' tags in the
-file, just as for `auto-coding-regexp-alist'."
+Any `coding:' tags present have a higher priority than the
+functions in this list."
:group 'files
:group 'mule
:type '(repeat function))
(setq alist (cdr alist))))
coding-system))
-(defun auto-coding-from-file-contents (size)
- "Determine a coding system from the contents of the current buffer.
-The current buffer contains SIZE bytes starting at point.
-Value is either a coding system or nil."
- (save-excursion
- (let ((alist auto-coding-regexp-alist)
- (funcs auto-coding-functions)
- coding-system)
- (while (and alist (not coding-system))
- (let ((regexp (car (car alist))))
- (when (re-search-forward regexp (+ (point) size) t)
- (setq coding-system (cdr (car alist)))))
- (setq alist (cdr alist)))
- (while (and funcs (not coding-system))
- (setq coding-system (condition-case e
- (save-excursion
- (funcall (pop funcs) size))
- (error nil))))
- coding-system)))
-
(defun set-auto-coding (filename size)
"Return coding system for a file FILENAME of which SIZE bytes follow point.
These bytes should include at least the first 1k of the file
and the last 3k of the file, but the middle may be omitted.
-It checks FILENAME against the variable `auto-coding-alist'. If
-FILENAME doesn't match any entries in the variable, it checks the
+The function checks FILENAME against the variable `auto-coding-alist'.
+If FILENAME doesn't match any entries in the variable, it checks the
contents of the current buffer following point against
-`auto-coding-regexp-alist', and tries calling each function in
-`auto-coding-functions'. If no match is found, it checks for a
+`auto-coding-regexp-alist'. If no match is found, it checks for a
`coding:' tag in the first one or two lines following point. If no
-`coding:' tag is found, it checks for local variables list in the last
-3K bytes out of the SIZE bytes.
+`coding:' tag is found, it checks any local variables list in the last
+3K bytes out of the SIZE bytes. Finally, if none of these methods
+succeed, it checks to see if any function in `auto-coding-functions'
+gives a match.
-The return value is the specified coding system,
-or nil if nothing specified.
+The return value is the specified coding system, or nil if nothing is
+specified.
The variable `set-auto-coding-function' (which see) is set to this
function by default."
(or (auto-coding-alist-lookup filename)
- (auto-coding-from-file-contents size)
+ ;; Try using `auto-coding-regexp-alist'.
+ (save-excursion
+ (let ((alist auto-coding-regexp-alist)
+ coding-system)
+ (while (and alist (not coding-system))
+ (let ((regexp (car (car alist))))
+ (when (re-search-forward regexp (+ (point) size) t)
+ (setq coding-system (cdr (car alist)))))
+ (setq alist (cdr alist)))
+ coding-system))
(let* ((case-fold-search t)
(head-start (point))
(head-end (+ head-start (min size 1024)))
(setq coding-system (intern (match-string 1)))
(or (coding-system-p coding-system)
(setq coding-system nil))))))
+ coding-system)
+ ;; Finally, try all the `auto-coding-functions'.
+ (let ((funcs auto-coding-functions)
+ (coding-system nil))
+ (while (and funcs (not coding-system))
+ (setq coding-system (condition-case e
+ (save-excursion
+ (goto-char (point-min))
+ (funcall (pop funcs) size))
+ (error nil))))
coding-system)))
(setq set-auto-coding-function 'set-auto-coding)
(progn ,@body)
(set-category-table current-category-table))))
+(defun define-translation-hash-table (symbol table)
+ "Define SYMBOL as the name of the hash translation TABLE for use in CCL.
+
+Analogous to `define-translation-table', but updates
+`translation-hash-table-vector' and the table is for use in the CCL
+`lookup-integer' and `lookup-character' functions."
+ (unless (and (symbolp symbol)
+ (hash-table-p table))
+ (error "Bad args to define-translation-hash-table"))
+ (let ((len (length translation-hash-table-vector))
+ (id 0)
+ done)
+ (put symbol 'translation-hash-table table)
+ (while (not done)
+ (if (>= id len)
+ (setq translation-hash-table-vector
+ (vconcat translation-hash-table-vector [nil])))
+ (let ((slot (aref translation-hash-table-vector id)))
+ (if (or (not slot)
+ (eq (car slot) symbol))
+ (progn
+ (aset translation-hash-table-vector id (cons symbol table))
+ (setq done t))
+ (setq id (1+ id)))))
+ (put symbol 'translation-hash-table-id id)
+ id))
+
;;; Initialize some variables.
(put 'use-default-ascent 'char-table-extra-slots 0)
(re-search-forward "\"\\s-*\\?>" size t))))
(when end
(if (re-search-forward "encoding=\"\\(.+?\\)\"" end t)
- (let ((match (downcase (match-string 1))))
- (cond ((member match '("utf-8" "iso-2022-jp"
- "euc-jp" "shift_jis"))
- (intern match))
- ((string-match "iso-8859-[[:digit:]]+" match)
- (intern match))
- (t (message "Warning: unknown XML encoding %s" match)
- nil)))
+ (let* ((match (match-string 1))
+ (sym (intern (downcase match))))
+ (if (coding-system-p sym)
+ sym
+ (message "Warning: unknown coding system \"%s\"" match)
+ nil))
'utf-8)))))
+(defun sgml-html-meta-auto-coding-function (size)
+ "If the buffer has an HTML meta tag, use it to determine encoding.
+This function is intended to be added to `auto-coding-functions'."
+ (setq size (min size
+ ;; Only search forward 10 lines
+ (save-excursion
+ (forward-line 10)
+ (point))))
+ (when (and (search-forward "<html>" size t)
+ (re-search-forward "<meta\\s-+http-equiv=\"content-type\"\\s-+content=\"text/\\sw+;\\s-*charset=\\(.+?\\)\"" size t))
+ (let* ((match (match-string 1))
+ (sym (intern (downcase match))))
+ (if (coding-system-p sym)
+ sym
+ (message "Warning: unknown coding system \"%s\"" match)
+ nil))))
+
;;;
(provide 'mule)