1 ;;; mule-diag.el --- show diagnosis of multilingual environment (Mule)
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2001 Free Software Foundation, Inc.
7 ;; Keywords: multilingual, charset, coding system, fontset, diagnosis, i18n
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
30 ;; Make sure the help-xref button type is defined.
33 ;;; General utility function
35 ;; Print all arguments with single space separator in one line.
36 (defun print-list (&rest args)
41 (setq args (cdr args)))
45 ;; Re-order the elements of charset-list.
46 (defun sort-charset-list ()
49 (function (lambda (x y) (< (charset-id x) (charset-id y)))))))
53 (define-button-type 'sort-listed-character-sets
54 'help-echo (purecopy "mouse-2, RET: sort on this column")
56 'action #'(lambda (button)
57 (sort-listed-character-sets (button-get button 'sort-key))))
59 (define-button-type 'list-charset-chars
61 'help-function #'list-charset-chars
62 'help-echo "mouse-2, RET: show table of characters for this character set")
66 (defun list-character-sets (arg)
67 "Display a list of all character sets.
69 The ID-NUM column contains a charset identification number
70 for internal Emacs use.
72 The MULTIBYTE-FORM column contains a format of multibyte sequence
73 of characters in the charset for buffer and string
74 by one to four hexadecimal digits.
75 `xx' stands for any byte in the range 0..127.
76 `XX' stands for any byte in the range 160..255.
78 The D column contains a dimension of this character set.
79 The CH column contains a number of characters in a block of this character set.
80 The FINAL-CHAR column contains an ISO-2022's <final-char> to use for
81 designating this character set in ISO-2022-based coding systems.
83 With prefix arg, the output format gets more cryptic,
84 but still shows the full information."
86 (help-setup-xref (list #'list-character-sets arg) (interactive-p))
87 (with-output-to-temp-buffer (help-buffer)
88 (with-current-buffer standard-output
90 (list-character-sets-2)
93 (substitute-command-keys
95 (if (display-mouse-p) "\\[help-follow-mouse] or ")
96 "\\[help-follow]:\n")))
97 (insert " on a column title to sort by that title,")
99 (insert "+----DIMENSION\n")
100 (insert " on a charset name to list characters.")
102 (insert "| +--CHARS\n")
103 (let ((columns '(("ID-NUM" . id) "\t"
104 ("CHARSET-NAME" . name) "\t\t\t"
105 ("MULTIBYTE-FORM" . id) "\t"
106 ("D CH FINAL-CHAR" . iso-spec)))
109 (if (stringp (car columns))
110 (insert (car columns))
111 (insert-text-button (car (car columns))
112 :type 'sort-listed-character-sets
113 'sort-key (cdr (car columns)))
114 (goto-char (point-max)))
115 (setq columns (cdr columns)))
117 (insert "------\t------------\t\t\t--------------\t- -- ----------\n")
119 ;; Insert body sorted by charset IDs.
120 (list-character-sets-1 'id)))))
123 ;; Sort character set list by SORT-KEY.
125 (defun sort-listed-character-sets (sort-key)
128 (help-setup-xref (list #'list-character-sets nil) t)
129 (let ((buffer-read-only nil))
130 (goto-char (point-min))
131 (re-search-forward "[0-9][0-9][0-9]")
133 (delete-region (point) (point-max))
134 (list-character-sets-1 sort-key)))))
136 (defun charset-multibyte-form-string (charset)
137 (let ((info (charset-info charset)))
138 (cond ((eq charset 'ascii)
140 ((eq charset 'eight-bit-control)
141 (format "%2X Xx" (aref info 6)))
142 ((eq charset 'eight-bit-graphic)
145 (let ((str (format "%2X" (aref info 6))))
146 (if (> (aref info 7) 0)
147 (setq str (format "%s %2X"
149 (setq str (concat str " XX"))
150 (if (> (aref info 2) 1)
151 (setq str (concat str " XX")))
154 ;; Insert a list of character sets sorted by SORT-KEY. SORT-KEY
155 ;; should be one of `id', `name', and `iso-spec'. If SORT-KEY is nil,
156 ;; it defaults to `id'.
158 (defun list-character-sets-1 (sort-key)
161 (let ((tail (charset-list))
162 charset-info-list elt charset info sort-func)
164 (setq charset (car tail) tail (cdr tail)
165 info (charset-info charset))
167 ;; Generate a list that contains all information to display.
168 (setq charset-info-list
169 (cons (list (charset-id charset) ; ID-NUM
170 charset ; CHARSET-NAME
171 (charset-multibyte-form-string charset); MULTIBYTE-FORM
172 (aref info 2) ; DIMENSION
173 (aref info 3) ; CHARS
174 (aref info 8) ; FINAL-CHAR
178 ;; Determine a predicate for `sort' by SORT-KEY.
180 (cond ((eq sort-key 'id)
181 (function (lambda (x y) (< (car x) (car y)))))
184 (function (lambda (x y) (string< (nth 1 x) (nth 1 y)))))
186 ((eq sort-key 'iso-spec)
187 ;; Sort by DIMENSION CHARS FINAL-CHAR
190 (or (< (nth 3 x) (nth 3 y))
191 (and (= (nth 3 x) (nth 3 y))
192 (or (< (nth 4 x) (nth 4 y))
193 (and (= (nth 4 x) (nth 4 y))
194 (< (nth 5 x) (nth 5 y)))))))))
196 (error "Invalid charset sort key: %s" sort-key))))
198 (setq charset-info-list (sort charset-info-list sort-func))
200 ;; Insert information of character sets.
201 (while charset-info-list
202 (setq elt (car charset-info-list)
203 charset-info-list (cdr charset-info-list))
204 (insert (format "%03d(%02X)" (car elt) (car elt))) ; ID-NUM
206 (insert-text-button (symbol-name (nth 1 elt))
207 :type 'list-charset-chars
208 'help-args (list (nth 1 elt)))
209 (goto-char (point-max))
212 (insert (nth 2 elt)) ; MULTIBYTE-FORM
214 (insert (format "%d %2d " (nth 3 elt) (nth 4 elt)) ; DIMENSION and CHARS
215 (if (< (nth 5 elt) 0) "none" (nth 5 elt))) ; FINAL-CHAR
219 ;; List all character sets in a form that a program can easily parse.
221 (defun list-character-sets-2 ()
222 (insert "#########################
224 ## Each line corresponds to one charset.
225 ## The following attributes are listed in this order
226 ## separated by a colon `:' in one line.
228 ## CHARSET-SYMBOL-NAME,
229 ## DIMENSION (1 or 2)
231 ## BYTES (of multibyte form: 1, 2, 3, or 4),
232 ## WIDTH (occupied column numbers: 1 or 2),
233 ## DIRECTION (0:left-to-right, 1:right-to-left),
234 ## ISO-FINAL-CHAR (character code of ISO-2022's final character)
235 ## ISO-GRAPHIC-PLANE (ISO-2022's graphic plane, 0:GL, 1:GR)
236 ## DESCRIPTION (describing string of the charset)
238 (let ((l charset-list)
241 (setq charset (car l) l (cdr l))
242 (princ (format "%03d:%s:%d:%d:%d:%d:%d:%d:%d:%s\n"
245 (charset-dimension charset)
246 (charset-chars charset)
247 (charset-bytes charset)
248 (charset-width charset)
249 (charset-direction charset)
250 (charset-iso-final-char charset)
251 (charset-iso-graphic-plane charset)
252 (charset-description charset))))))
254 (defvar non-iso-charset-alist
256 (ascii vietnamese-viscii-lower vietnamese-viscii-upper)
257 viet-viscii-nonascii-translation-table
260 (ascii cyrillic-iso8859-5)
261 cyrillic-koi8-r-nonascii-translation-table
264 (ascii cyrillic-iso8859-5)
265 cyrillic-alternativnyj-nonascii-translation-table
268 (ascii chinese-big5-1 chinese-big5-2)
271 ((?\xA1 ?\xFE) . (?\x40 ?\x7E ?\xA1 ?\xFE))))
273 (ascii katakana-jisx0201 japanese-jisx0208)
275 ((32 127 ?\xA1 ?\xDF)
276 ((?\x81 ?\x9F ?\xE0 ?\xEF) . (?\x40 ?\x7E ?\x80 ?\xFC)))))
277 "Alist of non-ISO charset names vs the corresponding information.
279 Non-ISO charsets are what Emacs can read (or write) by mapping to (or
280 from) some Emacs' charsets that correspond to ISO charsets.
282 Each element has the following format:
283 (NON-ISO-CHARSET CHARSET-LIST TRANSLATION-METHOD [ CODE-RANGE ])
285 NON-ISO-CHARSET is a name (symbol) of the non-ISO charset.
287 CHARSET-LIST is a list of Emacs' charsets into which characters of
288 NON-ISO-CHARSET are mapped.
290 TRANSLATION-METHOD is a translation table (symbol) to translate a
291 character code of NON-ISO-CHARSET to the corresponding Emacs character
292 code. It can also be a function to call with one argument, a
293 character code in NON-ISO-CHARSET.
295 CODE-RANGE specifies the valid code ranges of NON-ISO-CHARSET.
296 It is a list of RANGEs, where each RANGE is of the form:
297 (FROM1 TO1 FROM2 TO2 ...)
299 ((FROM1-1 TO1-1 FROM1-2 TO1-2 ...) . (FROM2-1 TO2-1 FROM2-2 TO2-2 ...))
300 In the first form, valid codes are between FROM1 and TO1, or FROM2 and
302 The second form is used for 2-byte codes. The car part is the ranges
303 of the first byte, and the cdr part is the ranges of the second byte.")
306 ;; Decode a character that has code CODE in CODEPAGE. Value is a
307 ;; string of decoded character.
309 (defun decode-codepage-char (codepage code)
310 ;; Each CODEPAGE corresponds to a coding system cpCODEPAGE.
311 (let ((coding-system (intern (format "cp%d" codepage))))
312 (or (coding-system-p coding-system)
313 (codepage-setup codepage))
315 (decode-coding-string (char-to-string code) coding-system))))
318 ;; Add DOS codepages to `non-iso-charset-alist'.
320 (let ((tail (cp-supported-codepages))
323 (setq elt (car tail) tail (cdr tail))
324 ;; Now ELT is (CODEPAGE . CHARSET), where CODEPAGE is a string
325 ;; (e.g. "850"), CHARSET is a charset that characters in CODEPAGE
327 (setq non-iso-charset-alist
328 (cons (list (intern (concat "cp" (car elt)))
329 (list 'ascii (cdr elt))
331 (decode-codepage-char ,(string-to-int (car elt))
334 non-iso-charset-alist))))
337 ;; A variable to hold charset input history.
338 (defvar charset-history nil)
342 (defun read-charset (prompt &optional default-value initial-input)
343 "Read a character set from the minibuffer, prompting with string PROMPT.
344 It reads an Emacs' character set listed in the variable `charset-list'
345 or a non-ISO character set listed in the variable
346 `non-iso-charset-alist'.
348 Optional arguments are DEFAULT-VALUE and INITIAL-INPUT.
349 DEFAULT-VALUE, if non-nil, is the default value.
350 INITIAL-INPUT, if non-nil, is a string inserted in the minibuffer initially.
351 See the documentation of the function `completing-read' for the
352 detailed meanings of these arguments."
353 (let* ((table (append (mapcar (function (lambda (x) (list (symbol-name x))))
355 (mapcar (function (lambda (x)
356 (list (symbol-name (car x)))))
357 non-iso-charset-alist)))
358 (charset (completing-read prompt table
359 nil t initial-input 'charset-history
361 (if (> (length charset) 0)
365 ;; List characters of the range MIN and MAX of CHARSET. If dimension
366 ;; of CHARSET is two (i.e. 2-byte charset), ROW is the first byte
367 ;; (block index) of the characters, and MIN and MAX are the second
368 ;; bytes of the characters. If the dimension is one, ROW should be 0.
369 ;; For a non-ISO charset, CHARSET is a translation table (symbol) or a
370 ;; function to get Emacs' character codes that corresponds to the
371 ;; characters to list.
373 (defun list-block-of-chars (charset row min max)
375 (insert-char ?- (+ 4 (* 3 16)))
379 (insert (format "%3X" i))
381 (setq i (* (/ min 16) 16))
384 (insert (format "\n%3Xx" (/ (+ (* row 256) i) 16))))
385 (setq ch (cond ((< i min)
389 (make-char charset i)
390 (make-char charset row i)))
391 ((and (symbolp charset) (get charset 'translation-table))
392 (aref (get charset 'translation-table) i))
393 (t (funcall charset (+ (* row 256) i)))))
394 (if (and (char-table-p charset)
395 (or (< ch 32) (and (>= ch 127) (<= ch 255))))
396 ;; Don't insert a control code.
398 (indent-to (+ (* (% i 16) 3) 6))
404 ;; List all characters in ISO charset CHARSET.
406 (defun list-iso-charset-chars (charset)
407 (let ((dim (charset-dimension charset))
408 (chars (charset-chars charset))
409 (plane (charset-iso-graphic-plane charset))
411 (insert (format "Characters in the charset %s.\n" charset))
413 (cond ((eq charset 'eight-bit-control)
414 (setq min 128 max 159))
415 ((eq charset 'eight-bit-graphic)
416 (setq min 160 max 255))
419 (setq min 33 max 126)
420 (setq min 32 max 127))
422 (setq min (+ min 128) max (+ max 128)))))
425 (list-block-of-chars charset 0 min max)
428 (list-block-of-chars charset i min max)
432 ;; List all characters in non-ISO charset CHARSET.
434 (defun list-non-iso-charset-chars (charset)
435 (let* ((slot (assq charset non-iso-charset-alist))
436 (charsets (nth 1 slot))
437 (translate-method (nth 2 slot))
438 (ranges (nth 3 slot))
441 (error "Unknown external charset: %s" charset))
442 (insert (format "Characters in non-ISO charset %s.\n" charset))
443 (insert "They are mapped to: "
444 (mapconcat #'symbol-name charsets ", ")
447 (setq range (car ranges) ranges (cdr ranges))
448 (if (integerp (car range))
449 ;; The form of RANGES is (FROM1 TO1 FROM2 TO2 ...).
451 (list-block-of-chars translate-method
452 0 (car range) (nth 1 range))
453 (setq range (nthcdr 2 range)))
454 ;; The form of RANGES is ((FROM1-1 TO1-1 ...) . (FROM2-1 TO2-1 ...)).
455 (let ((row-range (car range))
457 col-range col col-max)
459 (setq row (car row-range) row-max (nth 1 row-range)
460 row-range (nthcdr 2 row-range))
461 (while (<= row row-max)
462 (setq col-range (cdr range))
464 (setq col (car col-range) col-max (nth 1 col-range)
465 col-range (nthcdr 2 col-range))
466 (list-block-of-chars translate-method row col col-max))
467 (setq row (1+ row)))))))))
471 (defun list-charset-chars (charset)
472 "Display a list of characters in the specified character set."
473 (interactive (list (read-charset "Character set: ")))
474 (with-output-to-temp-buffer "*Help*"
475 (with-current-buffer standard-output
476 (set-buffer-multibyte t)
477 (cond ((charsetp charset)
478 (list-iso-charset-chars charset))
479 ((assq charset non-iso-charset-alist)
480 (list-non-iso-charset-chars charset))
482 (error "Invalid charset %s" charset))))))
486 (defun describe-character-set (charset)
487 "Display information about character set CHARSET."
488 (interactive (list (let ((non-iso-charset-alist nil))
489 (read-charset "Charset: "))))
490 (or (charsetp charset)
491 (error "Invalid charset: %S" charset))
492 (let ((info (charset-info charset)))
493 (help-setup-xref (list #'describe-character-set charset) (interactive-p))
494 (with-output-to-temp-buffer (help-buffer)
495 (with-current-buffer standard-output
496 (insert "Character set: " (symbol-name charset)
497 (format " (ID:%d)\n\n" (aref info 0)))
498 (insert (aref info 13) "\n\n") ; description
499 (insert "number of contained characters: "
500 (if (= (aref info 2) 1)
501 (format "%d\n" (aref info 3))
502 (format "%dx%d\n" (aref info 3) (aref info 3))))
503 (insert "the final char of ISO2022's designation sequence: ")
504 (if (>= (aref info 8) 0)
505 (insert (format "`%c'\n" (aref info 8)))
506 (insert "not assigned\n"))
507 (insert (format "width (how many columns on screen): %d\n"
509 (insert (format "internal multibyte sequence: %s\n"
510 (charset-multibyte-form-string charset)))
511 (let ((coding (plist-get (aref info 14) 'preferred-coding-system)))
513 (insert (format "preferred coding system: %s\n" coding))
514 (search-backward (symbol-name coding))
515 (help-xref-button 0 'describe-coding-system coding)))))))
518 (defun describe-char-after (&optional pos)
519 "Display information about the character at POS in the current buffer.
520 POS defaults to point.
521 The information includes character code, charset and code points in it,
522 syntax, category, how the character is encoded in a file,
523 which font is being used for displaying the character."
527 (if (>= pos (point-max))
528 (error "No character at point"))
529 (let* ((char (char-after pos))
530 (charset (char-charset char))
531 (composition (find-composition (point) nil nil t))
532 (composed (if composition (buffer-substring (car composition)
533 (nth 1 composition))))
534 (multibyte-p enable-multibyte-characters)
536 (if (eq charset 'unknown)
539 ,(format "%s (0%o, %d, 0x%x) -- invalid character code"
541 (single-key-description char)
542 (char-to-string char))
546 ,(format "%s (0%o, %d, 0x%x)" (if (< char 256)
547 (single-key-description char)
548 (char-to-string char))
551 ,(symbol-name charset)
552 ,(format "(%s)" (charset-description charset)))
554 ,(let ((split (split-char char)))
555 (if (= (charset-dimension charset) 1)
556 (format "%d" (nth 1 split))
557 (format "%d %d" (nth 1 split) (nth 2 split)))))
559 ,(let ((syntax (aref (syntax-table) char)))
561 (internal-describe-syntax-value syntax)
564 ,@(let ((category-set (char-category-set char)))
565 (if (not category-set)
567 (mapcar #'(lambda (x) (format "%c:%s "
568 x (category-docstring x)))
569 (category-set-mnemonics category-set)))))
571 ,(encoded-string-description
572 (string-as-unibyte (char-to-string char)) nil))
574 ,@(let* ((coding buffer-file-coding-system)
575 (encoded (encode-coding-char char coding)))
577 (list (encoded-string-description encoded coding)
578 (format "(encoded by coding system %S)" coding))
579 (list "not encodable by coding system"
580 (symbol-name coding)))))
581 ,(if (display-graphic-p (selected-frame))
582 (list "font" (or (internal-char-font (point))
584 (list "terminal code"
585 (let* ((coding (terminal-coding-system))
586 (encoded (encode-coding-char char coding)))
588 (encoded-string-description encoded coding)
589 "not encodable")))))))
590 (setq max-width (apply #'max (mapcar #'(lambda (x) (length (car x)))
592 (with-output-to-temp-buffer "*Help*"
594 (set-buffer standard-output)
595 (set-buffer-multibyte multibyte-p)
596 (let ((formatter (format "%%%ds:" max-width)))
597 (dolist (elt item-list)
598 (insert (format formatter (car elt)))
599 (dolist (clm (cdr elt))
600 (when (>= (+ (current-column)
601 (or (string-match "\n" clm)
602 (string-width clm)) 1)
605 (indent-to (1+ max-width)))
609 (insert "\nComposed with the following character(s) "
610 (mapconcat (lambda (x) (format "`%c'" x))
611 (substring composed 1)
613 " to form `" composed "'")
614 (if (nth 3 composition)
616 (insert "\nby the rule ("
617 (mapconcat (lambda (x)
618 (format (if (consp x) "%S" "?%c") x))
622 "See the variable `reference-point-alist' for the meaning of the rule.\n")))
628 ;; Print information of designation of each graphic register in FLAGS
629 ;; in human readable format. See the documentation of
630 ;; `make-coding-system' for the meaning of FLAGS.
631 (defun print-designation (flags)
632 (let ((graphic-register 0)
634 (while (< graphic-register 4)
635 (setq charset (aref flags graphic-register))
639 (cond ((null charset)
642 "no initial designation, and used by any charsets")
645 charset (charset-description charset)))
647 (if (charsetp (car charset))
648 (format "%s:%s, and also used by the followings:"
650 (charset-description (car charset)))
651 "no initial designation, and used by the followings:"))
653 "invalid designation information"))))
654 (when (listp charset)
655 (setq charset (cdr charset))
657 (cond ((eq (car charset) t)
658 (princ "\tany other charsets\n"))
659 ((charsetp (car charset))
660 (princ (format "\t%s:%s\n"
662 (charset-description (car charset)))))
664 "invalid designation information"))
665 (setq charset (cdr charset))))
666 (setq graphic-register (1+ graphic-register)))))
669 (defun describe-coding-system (coding-system)
670 "Display information about CODING-SYSTEM."
671 (interactive "zDescribe coding system (default, current choices): ")
672 (if (null coding-system)
673 (describe-current-coding-system)
674 (help-setup-xref (list #'describe-coding-system coding-system)
676 (with-output-to-temp-buffer (help-buffer)
677 (print-coding-system-briefly coding-system 'doc-string)
678 (let ((coding-spec (coding-system-spec coding-system)))
680 (let ((type (coding-system-type coding-system))
681 (flags (coding-system-flags coding-system)))
684 (princ " (do no conversion)"))
686 (princ " (do automatic conversion)"))
688 (princ " (Emacs internal multibyte form)"))
690 (princ " (Shift-JIS, MS-KANJI)"))
692 (princ " (variant of ISO-2022)\n")
693 (princ "Initial designations:\n")
694 (print-designation flags)
695 (princ "Other Form: \n ")
696 (princ (if (aref flags 4) "short-form" "long-form"))
697 (if (aref flags 5) (princ ", ASCII@EOL"))
698 (if (aref flags 6) (princ ", ASCII@CNTL"))
699 (princ (if (aref flags 7) ", 7-bit" ", 8-bit"))
700 (if (aref flags 8) (princ ", use-locking-shift"))
701 (if (aref flags 9) (princ ", use-single-shift"))
702 (if (aref flags 10) (princ ", use-roman"))
703 (if (aref flags 11) (princ ", use-old-jis"))
704 (if (aref flags 12) (princ ", no-ISO6429"))
705 (if (aref flags 13) (princ ", init-bol"))
706 (if (aref flags 14) (princ ", designation-bol"))
707 (if (aref flags 15) (princ ", convert-unsafe"))
708 (if (aref flags 16) (princ ", accept-latin-extra-code"))
713 (princ " (do conversion by CCL program)"))
715 (princ " (text with random binary characters)"))
716 (t (princ ": invalid coding-system."))))
717 (princ "\nEOL type: ")
718 (let ((eol-type (coding-system-eol-type coding-system)))
719 (cond ((vectorp eol-type)
720 (princ "Automatic selection from:\n\t")
723 ((or (null eol-type) (eq eol-type 0)) (princ "LF\n"))
724 ((eq eol-type 1) (princ "CRLF\n"))
725 ((eq eol-type 2) (princ "CR\n"))
726 (t (princ "invalid\n")))))
727 (let ((postread (coding-system-get coding-system 'post-read-conversion)))
729 (princ "After decoding text normally,")
730 (princ " perform post-conversion using the function: ")
734 (let ((prewrite (coding-system-get coding-system 'pre-write-conversion)))
736 (princ "Before encoding text normally,")
737 (princ " perform pre-conversion using the function: ")
741 (with-current-buffer standard-output
742 (let ((charsets (coding-system-get coding-system 'safe-charsets)))
743 (when (and (not (memq (coding-system-base coding-system)
744 '(raw-text emacs-mule)))
747 (insert "This coding system can encode all charsets except for
748 eight-bit-control and eight-bit-graphic.\n")
749 (insert "This coding system encodes the following charsets:\n ")
751 (insert " " (symbol-name (car charsets)))
752 (search-backward (symbol-name (car charsets)))
753 (help-xref-button 0 'help-character-set (car charsets))
754 (goto-char (point-max))
755 (setq charsets (cdr charsets))))))))))
759 (defun describe-current-coding-system-briefly ()
760 "Display coding systems currently used in a brief format in echo area.
762 The format is \"F[..],K[..],T[..],P>[..],P<[..], default F[..],P<[..],P<[..]\",
763 where mnemonics of the following coding systems come in this order
764 at the place of `..':
765 `buffer-file-coding-system' (of the current buffer)
766 eol-type of `buffer-file-coding-system' (of the current buffer)
767 Value returned by `keyboard-coding-system'
768 eol-type of `keyboard-coding-system'
769 Value returned by `terminal-coding-system'.
770 eol-type of `terminal-coding-system'
771 `process-coding-system' for read (of the current buffer, if any)
772 eol-type of `process-coding-system' for read (of the current buffer, if any)
773 `process-coding-system' for write (of the current buffer, if any)
774 eol-type of `process-coding-system' for write (of the current buffer, if any)
775 `default-buffer-file-coding-system'
776 eol-type of `default-buffer-file-coding-system'
777 `default-process-coding-system' for read
778 eol-type of `default-process-coding-system' for read
779 `default-process-coding-system' for write
780 eol-type of `default-process-coding-system'"
782 (let* ((proc (get-buffer-process (current-buffer)))
783 (process-coding-systems (if proc (process-coding-system proc))))
785 "F[%c%s],K[%c%s],T[%c%s],P>[%c%s],P<[%c%s], default F[%c%s],P>[%c%s],P<[%c%s]"
786 (coding-system-mnemonic buffer-file-coding-system)
787 (coding-system-eol-type-mnemonic buffer-file-coding-system)
788 (coding-system-mnemonic (keyboard-coding-system))
789 (coding-system-eol-type-mnemonic (keyboard-coding-system))
790 (coding-system-mnemonic (terminal-coding-system))
791 (coding-system-eol-type-mnemonic (terminal-coding-system))
792 (coding-system-mnemonic (car process-coding-systems))
793 (coding-system-eol-type-mnemonic (car process-coding-systems))
794 (coding-system-mnemonic (cdr process-coding-systems))
795 (coding-system-eol-type-mnemonic (cdr process-coding-systems))
796 (coding-system-mnemonic default-buffer-file-coding-system)
797 (coding-system-eol-type-mnemonic default-buffer-file-coding-system)
798 (coding-system-mnemonic (car default-process-coding-system))
799 (coding-system-eol-type-mnemonic (car default-process-coding-system))
800 (coding-system-mnemonic (cdr default-process-coding-system))
801 (coding-system-eol-type-mnemonic (cdr default-process-coding-system))
804 ;; Print symbol name and mnemonic letter of CODING-SYSTEM with `princ'.
805 (defun print-coding-system-briefly (coding-system &optional doc-string)
806 (if (not coding-system)
808 (princ (format "%c -- %s"
809 (coding-system-mnemonic coding-system)
811 (let ((aliases (coding-system-get coding-system 'alias-coding-systems)))
812 (if (eq coding-system (car aliases))
814 (princ (format " %S" (cons 'alias: (cdr aliases)))))
815 (if (memq coding-system aliases)
816 (princ (format " (alias of %s)" (car aliases))))))
819 (setq doc-string (coding-system-doc-string coding-system)))
820 (princ (format " %s\n" doc-string)))))
823 (defun describe-current-coding-system ()
824 "Display coding systems currently used, in detail."
826 (with-output-to-temp-buffer "*Help*"
827 (let* ((proc (get-buffer-process (current-buffer)))
828 (process-coding-systems (if proc (process-coding-system proc))))
829 (princ "Coding system for saving this buffer:\n ")
830 (if (local-variable-p 'buffer-file-coding-system)
831 (print-coding-system-briefly buffer-file-coding-system)
832 (princ "Not set locally, use the default.\n"))
833 (princ "Default coding system (for new files):\n ")
834 (print-coding-system-briefly default-buffer-file-coding-system)
835 (princ "Coding system for keyboard input:\n ")
836 (print-coding-system-briefly (keyboard-coding-system))
837 (princ "Coding system for terminal output:\n ")
838 (print-coding-system-briefly (terminal-coding-system))
839 (when (get-buffer-process (current-buffer))
840 (princ "Coding systems for process I/O:\n")
841 (princ " encoding input to the process: ")
842 (print-coding-system-briefly (cdr process-coding-systems))
843 (princ " decoding output from the process: ")
844 (print-coding-system-briefly (car process-coding-systems)))
845 (princ "Defaults for subprocess I/O:\n")
846 (princ " decoding: ")
847 (print-coding-system-briefly (car default-process-coding-system))
848 (princ " encoding: ")
849 (print-coding-system-briefly (cdr default-process-coding-system)))
851 (with-current-buffer standard-output
853 (princ "\nPriority order for recognizing coding systems when reading files:\n")
854 (let ((l coding-category-list)
859 (setq coding (symbol-value (car l)))
860 ;; Do not list up the same coding system twice.
861 (when (and coding (not (memq coding coding-list)))
862 (setq coding-list (cons coding coding-list))
863 (princ (format " %d. %s " i coding))
864 (setq aliases (coding-system-get coding 'alias-coding-systems))
865 (if (eq coding (car aliases))
867 (princ (cons 'alias: (cdr aliases))))
868 (if (memq coding aliases)
869 (princ (list 'alias 'of (car aliases)))))
874 (princ "\n Other coding systems cannot be distinguished automatically
875 from these, and therefore cannot be recognized automatically
876 with the present coding system priorities.\n\n")
878 (let ((categories '(coding-category-iso-7 coding-category-iso-7-else))
879 coding-system codings)
881 (setq coding-system (symbol-value (car categories)))
885 (if (and (not (eq x coding-system))
886 (coding-system-get x 'no-initial-designation)
887 (let ((flags (coding-system-flags x)))
888 (not (or (aref flags 10) (aref flags 11)))))
889 (setq codings (cons x codings)))))
890 (get (car categories) 'coding-systems))
892 (let ((max-col (frame-width))
894 (princ (format " The followings are decoded correctly but recognized as %s:\n " coding-system))
897 (insert (format " %s" (car codings)))
898 (when (> (current-column) max-col)
901 (goto-char (point-max)))
902 (setq codings (cdr codings)))
904 (setq categories (cdr categories))))
906 (princ "Particular coding systems specified for certain file names:\n")
908 (princ " OPERATION\tTARGET PATTERN\t\tCODING SYSTEM(s)\n")
909 (princ " ---------\t--------------\t\t----------------\n")
910 (let ((func (lambda (operation alist)
914 (princ "\tnothing specified\n")
917 (prin1 (car (car alist)))
918 (if (>= (current-column) 40)
921 (princ (cdr (car alist)))
923 (setq alist (cdr alist)))))))
924 (funcall func "File I/O" file-coding-system-alist)
925 (funcall func "Process I/O" process-coding-system-alist)
926 (funcall func "Network I/O" network-coding-system-alist))
929 ;; Print detailed information on CODING-SYSTEM.
930 (defun print-coding-system (coding-system)
931 (let ((type (coding-system-type coding-system))
932 (eol-type (coding-system-eol-type coding-system))
933 (flags (coding-system-flags coding-system))
934 (aliases (coding-system-get coding-system 'alias-coding-systems)))
935 (if (not (eq (car aliases) coding-system))
936 (princ (format "%s (alias of %s)\n" coding-system (car aliases)))
937 (princ coding-system)
938 (setq aliases (cdr aliases))
941 (princ (car aliases))
942 (setq aliases (cdr aliases)))
943 (princ (format ":%s:%c:%d:"
945 (coding-system-mnemonic coding-system)
946 (if (integerp eol-type) eol-type 3)))
947 (cond ((eq type 2) ; ISO-2022
951 (setq charset (aref flags idx))
952 (cond ((null charset)
960 (princ (car charset))
961 (setq charset (cdr charset))
964 (princ (car charset))
965 (setq charset (cdr charset)))
970 (princ (if (aref flags idx) 1 0))
973 (princ (if (aref flags idx) 1 0))))
976 (if (symbolp (car flags))
977 (princ (format " %s" (car flags)))
978 (setq i 0 len (length (car flags)))
980 (princ (format " %x" (aref (car flags) i)))
983 (if (symbolp (cdr flags))
984 (princ (format "%s" (cdr flags)))
985 (setq i 0 len (length (cdr flags)))
987 (princ (format " %x" (aref (cdr flags) i)))
991 (princ (coding-system-doc-string coding-system))
995 (defun list-coding-systems (&optional arg)
996 "Display a list of all coding systems.
997 This shows the mnemonic letter, name, and description of each coding system.
999 With prefix arg, the output format gets more cryptic,
1000 but still contains full information about each coding system."
1002 (with-output-to-temp-buffer "*Help*"
1003 (list-coding-systems-1 arg)))
1005 (defun list-coding-systems-1 (arg)
1008 ###############################################
1009 # List of coding systems in the following format:
1010 # MNEMONIC-LETTER -- CODING-SYSTEM-NAME
1014 #########################
1015 ## LIST OF CODING SYSTEMS
1016 ## Each line corresponds to one coding system
1017 ## Format of a line is:
1018 ## NAME[,ALIAS...]:TYPE:MNEMONIC:EOL:FLAGS:POST-READ-CONVERSION
1019 ## :PRE-WRITE-CONVERSION:DOC-STRING,
1021 ## NAME = coding system name
1022 ## ALIAS = alias of the coding system
1023 ## TYPE = nil (no conversion), t (undecided or automatic detection),
1024 ## 0 (EMACS-MULE), 1 (SJIS), 2 (ISO2022), 3 (BIG5), or 4 (CCL)
1025 ## EOL = 0 (LF), 1 (CRLF), 2 (CR), or 3 (Automatic detection)
1028 ## comma (`,') separated data of the followings:
1029 ## G0, G1, G2, G3, SHORT-FORM, ASCII-EOL, ASCII-CNTL, SEVEN,
1030 ## LOCKING-SHIFT, SINGLE-SHIFT, USE-ROMAN, USE-OLDJIS, NO-ISO6429
1031 ## else if TYPE = 4 then
1032 ## comma (`,') separated CCL programs for read and write
1035 ## POST-READ-CONVERSION, PRE-WRITE-CONVERSION = function name to be called
1038 (let ((bases (coding-system-list 'base-only))
1041 (setq coding-system (car bases))
1043 (print-coding-system-briefly coding-system 'doc-string)
1044 (print-coding-system coding-system))
1045 (setq bases (cdr bases)))))
1048 (defun list-coding-categories ()
1049 "Display a list of all coding categories."
1050 (with-output-to-temp-buffer "*Help*"
1052 ############################
1053 ## LIST OF CODING CATEGORIES (ordered by priority)
1054 ## CATEGORY:CODING-SYSTEM
1057 (let ((l coding-category-list))
1059 (princ (format "%s:%s\n" (car l) (symbol-value (car l))))
1060 (setq l (cdr l))))))
1064 ;; Print information of a font in FONTINFO.
1065 (defun describe-font-internal (font-info &optional verbose)
1066 (print-list "name (opened by):" (aref font-info 0))
1067 (print-list " full name:" (aref font-info 1))
1068 (print-list " size:" (format "%2d" (aref font-info 2)))
1069 (print-list " height:" (format "%2d" (aref font-info 3)))
1070 (print-list " baseline-offset:" (format "%2d" (aref font-info 4)))
1071 (print-list "relative-compose:" (format "%2d" (aref font-info 5))))
1074 (defun describe-font (fontname)
1075 "Display information about fonts which partially match FONTNAME."
1076 (interactive "sFontname (default, current choice for ASCII chars): ")
1077 (or (and window-system (fboundp 'fontset-list))
1078 (error "No fontsets being used"))
1079 (when (or (not fontname) (= (length fontname) 0))
1080 (setq fontname (cdr (assq 'font (frame-parameters))))
1081 (if (query-fontset fontname)
1083 (nth 1 (assq 'ascii (aref (fontset-info fontname) 2))))))
1084 (let ((font-info (font-info fontname)))
1085 (if (null font-info)
1086 (message "No matching font")
1087 (with-output-to-temp-buffer "*Help*"
1088 (describe-font-internal font-info 'verbose)))))
1090 ;; Print information of FONTSET. If optional arg PRINT-FONTS is
1091 ;; non-nil, print also names of all opened fonts for FONTSET. This
1092 ;; function actually INSERT such information in the current buffer.
1093 (defun print-fontset (fontset &optional print-fonts)
1094 (let ((tail (aref (fontset-info fontset) 2))
1095 elt chars font-spec opened prev-charset charset from to)
1097 (insert "Fontset: " fontset "\n")
1098 (insert "CHARSET or CHAR RANGE")
1100 (insert "FONT NAME\n")
1101 (insert "---------------------")
1103 (insert "---------")
1106 (setq elt (car tail) tail (cdr tail))
1107 (setq chars (car elt) font-spec (car (cdr elt)) opened (cdr (cdr elt)))
1109 (setq charset chars from nil to nil)
1110 (if (integerp chars)
1111 (setq charset (char-charset chars) from chars to chars)
1112 (setq charset (char-charset (car chars))
1113 from (car chars) to (cdr chars))))
1114 (unless (eq charset prev-charset)
1115 (insert (symbol-name charset))
1119 (let ((split (split-char from)))
1120 (if (and (= (charset-dimension charset) 2)
1121 (= (nth 2 split) 0))
1123 (make-char charset (nth 1 split)
1124 (if (= (charset-chars charset) 94) 33 32))))
1128 (let ((split (split-char to)))
1129 (if (and (= (charset-dimension charset) 2)
1130 (= (nth 2 split) 0))
1132 (make-char charset (nth 1 split)
1133 (if (= (charset-chars charset) 94) 126 127))))
1136 (if (stringp font-spec)
1139 (if (string-match "-" (car font-spec))
1140 (insert "-" (car font-spec) "-*-")
1141 (insert "-*-" (car font-spec) "-*-"))
1144 (if (string-match "-" (cdr font-spec))
1145 (insert (cdr font-spec))
1146 (insert (cdr font-spec) "-*"))
1152 (insert "[" (car opened) "]\n")
1153 (setq opened (cdr opened))))
1154 (setq prev-charset charset)
1158 (defun describe-fontset (fontset)
1159 "Display information of FONTSET.
1160 This shows which font is used for which character(s)."
1162 (if (not (and window-system (fboundp 'fontset-list)))
1163 (error "No fontsets being used")
1164 (let ((fontset-list (nconc
1165 (mapcar 'list (fontset-list))
1166 (mapcar (lambda (x) (list (cdr x)))
1167 fontset-alias-alist)))
1168 (completion-ignore-case t))
1169 (list (completing-read
1170 "Fontset (default, used by the current frame): "
1171 fontset-list nil t)))))
1172 (if (= (length fontset) 0)
1173 (setq fontset (cdr (assq 'font (frame-parameters)))))
1174 (if (not (setq fontset (query-fontset fontset)))
1175 (error "Current frame is using font, not fontset"))
1176 (help-setup-xref (list #'describe-fontset fontset) (interactive-p))
1177 (with-output-to-temp-buffer (help-buffer)
1178 (with-current-buffer standard-output
1179 (print-fontset fontset t))))
1182 (defun list-fontsets (arg)
1183 "Display a list of all fontsets.
1184 This shows the name, size, and style of each fontset.
1185 With prefix arg, it also list the fonts contained in each fontset;
1186 see the function `describe-fontset' for the format of the list."
1188 (if (not (and window-system (fboundp 'fontset-list)))
1189 (error "No fontsets being used")
1190 (help-setup-xref (list #'list-fontsets arg) (interactive-p))
1191 (with-output-to-temp-buffer (help-buffer)
1192 (with-current-buffer standard-output
1193 ;; This code is duplicated near the end of mule-diag.
1195 (sort (fontset-list)
1196 (function (lambda (x y)
1197 (string< (fontset-plain-name x)
1198 (fontset-plain-name y)))))))
1201 (print-fontset (car fontsets) nil)
1202 (insert "Fontset: " (car fontsets) "\n"))
1203 (setq fontsets (cdr fontsets))))))))
1206 (defun list-input-methods ()
1207 "Display information about all input methods."
1209 (with-output-to-temp-buffer "*Help*"
1210 (list-input-methods-1)))
1212 (defun list-input-methods-1 ()
1213 (if (not input-method-alist)
1216 No input method is available, perhaps because you have not yet
1217 installed LEIM (Libraries of Emacs Input Method).
1219 LEIM is available from the same ftp directory as Emacs. For instance,
1220 if there exists an archive file `emacs-M.N.tar.gz', there should also
1221 be a file `leim-M.N.tar.gz'. When you extract this file, LEIM files
1222 are put under the subdirectory `emacs-M.N/leim'. When you install
1223 Emacs again, you should be able to use various input methods."))
1224 (princ "LANGUAGE\n NAME (`TITLE' in mode line)\n")
1225 (princ " SHORT-DESCRIPTION\n------------------------------\n")
1226 (setq input-method-alist
1227 (sort input-method-alist
1228 (function (lambda (x y) (string< (nth 1 x) (nth 1 y))))))
1229 (let ((l input-method-alist)
1232 (setq elt (car l) l (cdr l))
1233 (when (not (equal language (nth 1 elt)))
1234 (setq language (nth 1 elt))
1237 (princ (format " %s (`%s' in mode line)\n %s\n"
1239 (let ((title (nth 3 elt)))
1240 (if (and (consp title) (stringp (car title)))
1243 (let ((description (nth 4 elt)))
1244 (string-match ".*" description)
1245 (match-string 0 description))))))))
1249 ;; Insert a header of a section with SECTION-NUMBER and TITLE.
1250 (defun insert-section (section-number title)
1251 (insert "########################################\n"
1252 "# Section " (format "%d" section-number) ". " title "\n"
1253 "########################################\n\n"))
1257 "Display diagnosis of the multilingual environment (Mule).
1259 This shows various information related to the current multilingual
1260 environment, including lists of input methods, coding systems,
1261 character sets, and fontsets (if Emacs is running under a window
1262 system which uses fontsets)."
1264 (with-output-to-temp-buffer "*Mule-Diagnosis*"
1265 (with-current-buffer standard-output
1266 (insert "###############################################\n"
1267 "### Current Status of Multilingual Features ###\n"
1268 "###############################################\n\n"
1269 "CONTENTS: Section 1. General Information\n"
1270 " Section 2. Display\n"
1271 " Section 3. Input methods\n"
1272 " Section 4. Coding systems\n"
1273 " Section 5. Character sets\n")
1274 (if (and window-system (fboundp 'fontset-list))
1275 (insert " Section 6. Fontsets\n"))
1278 (insert-section 1 "General Information")
1279 (insert "Version of this emacs:\n " (emacs-version) "\n\n")
1280 (insert "Configuration options:\n " system-configuration-options "\n\n")
1281 (insert "Multibyte characters awareness:\n"
1282 (format " default: %S\n" default-enable-multibyte-characters)
1283 (format " current-buffer: %S\n\n" enable-multibyte-characters))
1284 (insert "Current language environment: " current-language-environment
1287 (insert-section 2 "Display")
1289 (insert "Window-system: "
1290 (symbol-name window-system)
1291 (format "%s" window-system-version))
1292 (insert "Terminal: " (getenv "TERM")))
1295 (if (eq window-system 'x)
1296 (let ((font (cdr (assq 'font (frame-parameters)))))
1297 (insert "The selected frame is using the "
1298 (if (query-fontset font) "fontset" "font")
1300 (insert "Coding system of the terminal: "
1301 (symbol-name (terminal-coding-system))))
1304 (insert-section 3 "Input methods")
1305 (list-input-methods-1)
1307 (if default-input-method
1308 (insert (format "Default input method: %s\n" default-input-method))
1309 (insert "No default input method is specified\n"))
1311 (insert-section 4 "Coding systems")
1312 (list-coding-systems-1 t)
1314 ############################
1315 ## LIST OF CODING CATEGORIES (ordered by priority)
1316 ## CATEGORY:CODING-SYSTEM
1319 (let ((l coding-category-list))
1321 (princ (format "%s:%s\n" (car l) (symbol-value (car l))))
1325 (insert-section 5 "Character sets")
1326 (list-character-sets-2)
1329 (when (and window-system (fboundp 'fontset-list))
1330 ;; This code duplicates most of list-fontsets.
1331 (insert-section 6 "Fontsets")
1332 (insert "Fontset-Name\t\t\t\t\t\t WDxHT Style\n")
1333 (insert "------------\t\t\t\t\t\t ----- -----\n")
1334 (let ((fontsets (fontset-list)))
1336 (print-fontset (car fontsets) t)
1337 (setq fontsets (cdr fontsets)))))
1338 (print-help-return-message))))
1344 (defun dump-charsets ()
1345 "Dump information about all charsets into the file `CHARSETS'.
1346 The file is saved in the directory `data-directory'."
1347 (let ((file (expand-file-name "CHARSETS" data-directory))
1349 (or (file-writable-p file)
1350 (error "Can't write to file %s" file))
1351 (setq buf (find-file-noselect file))
1352 (save-window-excursion
1353 (with-current-buffer buf
1354 (setq buffer-read-only nil)
1356 (list-character-sets-2)
1357 (insert-buffer-substring "*Help*")
1358 (let (make-backup-files
1359 coding-system-for-write)
1366 (defun dump-codings ()
1367 "Dump information about all coding systems into the file `CODINGS'.
1368 The file is saved in the directory `data-directory'."
1369 (let ((file (expand-file-name "CODINGS" data-directory))
1371 (or (file-writable-p file)
1372 (error "Can't write to file %s" file))
1373 (setq buf (find-file-noselect file))
1374 (save-window-excursion
1375 (with-current-buffer buf
1376 (setq buffer-read-only nil)
1378 (list-coding-systems t)
1379 (insert-buffer-substring "*Help*")
1380 (list-coding-categories)
1381 (insert-buffer-substring "*Help*")
1382 (let (make-backup-files
1383 coding-system-for-write)
1389 ;;; mule-diag.el ends here