* international/mule-diag.el (print-list): Fix 2008-12-03 change.
[bpt/emacs.git] / lisp / international / mule-diag.el
1 ;;; mule-diag.el --- show diagnosis of multilingual environment (Mule)
2
3 ;; Copyright (C) 1997, 1998, 2000, 2001, 2002, 2003, 2004, 2005, 2006,
4 ;; 2007, 2008, 2009 Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 ;; 2005, 2006, 2007, 2008, 2009
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
9 ;; Copyright (C) 2003
10 ;; National Institute of Advanced Industrial Science and Technology (AIST)
11 ;; Registration Number H13PRO009
12
13 ;; Keywords: multilingual, charset, coding system, fontset, diagnosis, i18n
14
15 ;; This file is part of GNU Emacs.
16
17 ;; GNU Emacs is free software: you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation, either version 3 of the License, or
20 ;; (at your option) any later version.
21
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
26
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29
30 ;;; Commentary:
31
32 ;;; Code:
33
34 ;; Make sure the help-xref button type is defined.
35 (require 'help-fns)
36
37 ;;; General utility function
38
39 (defun print-list (&rest args)
40 "Print all arguments with single space separator in one line."
41 (princ (mapconcat (lambda (arg) (prin1-to-string arg t)) args " "))
42 (princ "\n"))
43
44 ;;; CHARSET
45
46 (define-button-type 'sort-listed-character-sets
47 'help-echo (purecopy "mouse-2, RET: sort on this column")
48 'face 'bold
49 'action #'(lambda (button)
50 (sort-listed-character-sets (button-get button 'sort-key))))
51
52 (define-button-type 'list-charset-chars
53 :supertype 'help-xref
54 'help-function #'list-charset-chars
55 'help-echo "mouse-2, RET: show table of characters for this character set")
56
57 ;;;###autoload
58 (defun list-character-sets (arg)
59 "Display a list of all character sets.
60
61 The D column contains the dimension of this character set. The CH
62 column contains the number of characters in a block of this character
63 set. The FINAL-CHAR column contains an ISO-2022 <final-char> to use
64 for designating this character set in ISO-2022-based coding systems.
65
66 With prefix ARG, the output format gets more cryptic,
67 but still shows the full information."
68 (interactive "P")
69 (help-setup-xref (list #'list-character-sets arg) (interactive-p))
70 (with-output-to-temp-buffer "*Character Set List*"
71 (with-current-buffer standard-output
72 (if arg
73 (list-character-sets-2)
74 ;; Insert header.
75 (insert "Supplementary character sets are shown below.\n")
76 (insert
77 (substitute-command-keys
78 (concat "Use "
79 (if (display-mouse-p) "\\[help-follow-mouse] or ")
80 "\\[help-follow]:\n")))
81 (insert " on a column title to sort by that title,")
82 (indent-to 48)
83 (insert "+----DIMENSION\n")
84 (insert " on a charset name to list characters.")
85 (indent-to 48)
86 (insert "| +--CHARS\n")
87 (let ((columns '(("CHARSET-NAME" . name) "\t\t\t\t\t"
88 ("D CH FINAL-CHAR" . iso-spec)))
89 pos)
90 (while columns
91 (if (stringp (car columns))
92 (insert (car columns))
93 (insert-text-button (car (car columns))
94 :type 'sort-listed-character-sets
95 'sort-key (cdr (car columns)))
96 (goto-char (point-max)))
97 (setq columns (cdr columns)))
98 (insert "\n"))
99 (insert "------------\t\t\t\t\t- --- ----------\n")
100
101 ;; Insert body sorted by charset IDs.
102 (list-character-sets-1 'name)))))
103
104 (defun sort-listed-character-sets (sort-key)
105 (if sort-key
106 (save-excursion
107 (let ((buffer-read-only nil))
108 (goto-char (point-min))
109 (search-forward "\n-")
110 (forward-line 1)
111 (delete-region (point) (point-max))
112 (list-character-sets-1 sort-key)))))
113
114 (defun list-character-sets-1 (sort-key)
115 "Insert a list of character sets sorted by SORT-KEY.
116 SORT-KEY should be `name' or `iso-spec' (default `name')."
117 (or sort-key
118 (setq sort-key 'name))
119 (let ((tail charset-list)
120 charset-info-list supplementary-list charset sort-func)
121 (dolist (charset charset-list)
122 ;; Generate a list that contains all information to display.
123 (let ((elt (list charset
124 (charset-dimension charset)
125 (charset-chars charset)
126 (charset-iso-final-char charset))))
127 (if (plist-get (charset-plist charset) :supplementary-p)
128 (push elt supplementary-list)
129 (push elt charset-info-list))))
130
131 ;; Determine a predicate for `sort' by SORT-KEY.
132 (setq sort-func
133 (cond ((eq sort-key 'name)
134 (lambda (x y) (string< (car x) (car y))))
135
136 ((eq sort-key 'iso-spec)
137 ;; Sort by DIMENSION CHARS FINAL-CHAR
138 (function
139 (lambda (x y)
140 (or (< (nth 1 x) (nth 1 y))
141 (and (= (nth 1 x) (nth 1 y))
142 (or (< (nth 2 x) (nth 2 y))
143 (and (= (nth 2 x) (nth 2 y))
144 (< (nth 3 x) (nth 3 y)))))))))
145 (t
146 (error "Invalid charset sort key: %s" sort-key))))
147
148 (setq charset-info-list (sort charset-info-list sort-func))
149 (setq supplementary-list (sort supplementary-list sort-func))
150
151 ;; Insert information of character sets.
152 (dolist (elt (append charset-info-list (list t) supplementary-list))
153 (if (eq elt t)
154 (insert "-------------- Supplementary Character Sets --------------")
155 (insert-text-button (symbol-name (car elt)) ; NAME
156 :type 'list-charset-chars
157 'help-args (list (car elt)))
158 (goto-char (point-max))
159 (insert "\t")
160 (indent-to 48)
161 (insert (format "%d %3d "
162 (nth 1 elt) (nth 2 elt)) ; DIMENSION and CHARS
163 (if (< (nth 3 elt) 0)
164 "none"
165 (nth 3 elt)))) ; FINAL-CHAR
166 (insert "\n"))))
167
168
169 ;; List all character sets in a form that a program can easily parse.
170
171 (defun list-character-sets-2 ()
172 (insert "#########################
173 ## LIST OF CHARSETS
174 ## Each line corresponds to one charset.
175 ## The following attributes are listed in this order
176 ## separated by a colon `:' in one line.
177 ## CHARSET-SYMBOL-NAME,
178 ## DIMENSION (1-4)
179 ## CHARS (number of characters in first dimension of charset)
180 ## ISO-FINAL-CHAR (character code of ISO-2022's final character)
181 ## -1 means that no final character is assigned.
182 ## DESCRIPTION (describing string of the charset)
183 ")
184 (dolist (charset charset-list)
185 (princ (format "%s:%d:%d:%d:%s\n"
186 charset
187 (charset-dimension charset)
188 (charset-chars charset)
189 ;;; (char-width (make-char charset))
190 ;;; (charset-direction charset)
191 (charset-iso-final-char charset)
192 ;;; (charset-iso-graphic-plane charset)
193 (charset-description charset)))))
194
195 (defvar non-iso-charset-alist nil
196 "Obsolete.")
197 (make-obsolete-variable 'non-iso-charset-alist "no longer relevant." "23.1")
198
199 (defun decode-codepage-char (codepage code)
200 "Decode a character that has code CODE in CODEPAGE.
201 Return a decoded character string. Each CODEPAGE corresponds to a
202 coding system cpCODEPAGE."
203 (decode-char (intern (format "cp%d" codepage)) code))
204 (make-obsolete 'decode-codepage-char 'decode-char "23.1")
205
206 ;; A variable to hold charset input history.
207 (defvar charset-history nil)
208
209
210 ;;;###autoload
211 (defun read-charset (prompt &optional default-value initial-input)
212 "Read a character set from the minibuffer, prompting with string PROMPT.
213 It must be an Emacs character set listed in the variable `charset-list'.
214
215 Optional arguments are DEFAULT-VALUE and INITIAL-INPUT.
216 DEFAULT-VALUE, if non-nil, is the default value.
217 INITIAL-INPUT, if non-nil, is a string inserted in the minibuffer initially.
218 See the documentation of the function `completing-read' for the detailed
219 meanings of these arguments."
220 (let* ((table (mapcar (lambda (x) (list (symbol-name x))) charset-list))
221 (charset (completing-read prompt table
222 nil t initial-input 'charset-history
223 default-value)))
224 (if (> (length charset) 0)
225 (intern charset))))
226
227 ;; List characters of the range MIN and MAX of CHARSET. If dimension
228 ;; of CHARSET is two (i.e. 2-byte charset), ROW is the first byte
229 ;; (block index) of the characters, and MIN and MAX are the second
230 ;; bytes of the characters. If the dimension is one, ROW should be 0.
231
232 (defun list-block-of-chars (charset row min max)
233 (let (i ch)
234 (insert-char ?- (+ 7 (* 4 16)))
235 (insert "\n ")
236 (setq i 0)
237 (while (< i 16)
238 (insert (format "%4X" i))
239 (setq i (1+ i)))
240 (setq i (* (/ min 16) 16))
241 (while (<= i max)
242 (if (= (% i 16) 0)
243 (insert (format "\n%6Xx" (/ (+ (* row 256) i) 16))))
244 (setq ch (if (< i min)
245 32
246 (or (decode-char charset (+ (* row 256) i))
247 32))) ; gap in mapping
248 ;; Don't insert control codes, non-Unicode characters.
249 (if (or (< ch 32) (= ch 127))
250 (setq ch (single-key-description ch))
251 (if (and (>= ch 128) (< ch 160))
252 (setq ch (format "%02Xh" ch))
253 (if (> ch #x10FFFF)
254 (setq ch 32))))
255 (insert "\t" ch)
256 (setq i (1+ i))))
257 (insert "\n"))
258
259 ;;;###autoload
260 (defun list-charset-chars (charset)
261 "Display a list of characters in character set CHARSET."
262 (interactive (list (read-charset "Character set: ")))
263 (or (charsetp charset)
264 (error "Invalid character set: %s" charset))
265 (with-output-to-temp-buffer "*Character List*"
266 (with-current-buffer standard-output
267 (if (coding-system-p charset)
268 ;; Useful to be able to do C-u C-x = to find file code, for
269 ;; instance:
270 (set-buffer-file-coding-system charset))
271 (setq mode-line-format (copy-sequence mode-line-format))
272 (let ((slot (memq 'mode-line-buffer-identification mode-line-format)))
273 (if slot
274 (setcdr slot
275 (cons (format " (%s)" charset)
276 (cdr slot)))))
277 (setq tab-width 4)
278 (set-buffer-multibyte t)
279 (let ((dim (charset-dimension charset))
280 (chars (charset-chars charset))
281 ;; (plane (charset-iso-graphic-plane charset))
282 (plane 1)
283 (range (plist-get (charset-plist charset) :code-space))
284 min max min2 max2)
285 (if (> dim 2)
286 (error "Can only list 1- and 2-dimensional charsets"))
287 (insert (format "Characters in the coded character set %s.\n" charset))
288 (narrow-to-region (point) (point))
289 (setq min (aref range 0)
290 max (aref range 1))
291 (if (= dim 1)
292 (list-block-of-chars charset 0 min max)
293 (setq min2 (aref range 2)
294 max2 (aref range 3))
295 (let ((i min2))
296 (while (<= i max2)
297 (list-block-of-chars charset i min max)
298 (setq i (1+ i)))))
299 (put-text-property (point-min) (point-max) 'charset charset)
300 (widen)))))
301
302
303 ;;;###autoload
304 (defun describe-character-set (charset)
305 "Display information about built-in character set CHARSET."
306 (interactive (list (read-charset "Charset: ")))
307 (or (charsetp charset)
308 (error "Invalid charset: %S" charset))
309 (help-setup-xref (list #'describe-character-set charset) (interactive-p))
310 (with-output-to-temp-buffer (help-buffer)
311 (with-current-buffer standard-output
312 (insert "Character set: " (symbol-name charset))
313 (let ((name (get-charset-property charset :name)))
314 (if (not (eq name charset))
315 (insert " (alias of " (symbol-name name) ?\))))
316 (insert "\n\n" (charset-description charset) "\n\n")
317 (insert "Number of contained characters: ")
318 (dotimes (i (charset-dimension charset))
319 (unless (= i 0)
320 (insert ?x))
321 (insert (format "%d" (charset-chars charset (1+ i)))))
322 (insert ?\n)
323 (let ((char (charset-iso-final-char charset)))
324 (when (> char 0)
325 (insert "Final char of ISO2022 designation sequence: ")
326 (insert (format "`%c'\n" char))))
327 (insert (format "Width (how many columns on screen): %d\n"
328 (aref char-width-table (make-char charset))))
329 (let (aliases)
330 (dolist (c charset-list)
331 (if (and (not (eq c charset))
332 (eq charset (get-charset-property c :name)))
333 (push c aliases)))
334 (if aliases
335 (insert "Aliases: " (mapconcat #'symbol-name aliases ", ") ?\n)))
336
337 (dolist (elt `((:ascii-compatible-p "ASCII compatible." nil)
338 (:map "Map file: " identity)
339 (:unify-map "Unification map file: " identity)
340 (:invalid-code
341 nil
342 ,(lambda (c)
343 (format "Invalid character: %c (code %d)" c c)))
344 (:emacs-mule-id "Id in emacs-mule coding system: "
345 number-to-string)
346 (:parents "Parents: "
347 (lambda (parents)
348 (mapconcat ,(lambda (elt)
349 (format "%s" elt))
350 parents
351 ", ")))
352 (:code-space "Code space: " ,(lambda (c)
353 (format "%s" c)))
354 (:code-offset "Code offset: " number-to-string)
355 (:iso-revision-number "ISO revision number: "
356 number-to-string)
357 (:supplementary-p
358 "Used only as a parent of some other charset." nil)))
359 (let ((val (get-charset-property charset (car elt))))
360 (when val
361 (if (cadr elt) (insert (cadr elt)))
362 (if (nth 2 elt)
363 (insert (funcall (nth 2 elt) val)))
364 (insert ?\n)))))))
365 \f
366 ;;; CODING-SYSTEM
367
368 (defvar graphic-register) ; dynamic bondage
369
370 ;; Print information about designation of each graphic register in
371 ;; DESIGNATIONS in human readable format. See the documentation of
372 ;; `define-coding-system' for the meaning of DESIGNATIONS
373 ;; (`:designation' property).
374 (defun print-designation (designations)
375 (let (charset)
376 (dotimes (graphic-register 4)
377 (setq charset (aref designations graphic-register))
378 (princ (format
379 " G%d -- %s\n"
380 graphic-register
381 (cond ((null charset)
382 "never used")
383 ((eq charset t)
384 "no initial designation, and used by any charsets")
385 ((symbolp charset)
386 (format "%s:%s"
387 charset (charset-description charset)))
388 ((listp charset)
389 (if (charsetp (car charset))
390 (format "%s:%s, and also used by the following:"
391 (car charset)
392 (charset-description (car charset)))
393 "no initial designation, and used by the following:"))
394 (t
395 "invalid designation information"))))
396 (when (listp charset)
397 (setq charset (cdr charset))
398 (while charset
399 (cond ((eq (car charset) t)
400 (princ "\tany other charsets\n"))
401 ((charsetp (car charset))
402 (princ (format "\t%s:%s\n"
403 (car charset)
404 (charset-description (car charset)))))
405 (t
406 "invalid designation information"))
407 (setq charset (cdr charset)))))))
408
409 ;;;###autoload
410 (defun describe-coding-system (coding-system)
411 "Display information about CODING-SYSTEM."
412 (interactive "zDescribe coding system (default current choices): ")
413 (if (null coding-system)
414 (describe-current-coding-system)
415 (help-setup-xref (list #'describe-coding-system coding-system)
416 (interactive-p))
417 (with-output-to-temp-buffer (help-buffer)
418 (print-coding-system-briefly coding-system 'doc-string)
419 (let ((type (coding-system-type coding-system))
420 ;; Fixme: use this
421 (extra-spec (coding-system-plist coding-system)))
422 (princ "Type: ")
423 (princ type)
424 (cond ((eq type 'undecided)
425 (princ " (do automatic conversion)"))
426 ((eq type 'utf-8)
427 (princ " (UTF-8: Emacs internal multibyte form)"))
428 ((eq type 'utf-16)
429 ;; (princ " (UTF-16)")
430 )
431 ((eq type 'shift-jis)
432 (princ " (Shift-JIS, MS-KANJI)"))
433 ((eq type 'iso-2022)
434 (princ " (variant of ISO-2022)\n")
435 (princ "Initial designations:\n")
436 (print-designation (coding-system-get coding-system
437 :designation))
438
439 (when (coding-system-get coding-system :flags)
440 (princ "Other specifications: \n ")
441 (apply #'print-list
442 (coding-system-get coding-system :flags))))
443 ((eq type 'charset)
444 (princ " (charset)"))
445 ((eq type 'ccl)
446 (princ " (do conversion by CCL program)"))
447 ((eq type 'raw-text)
448 (princ " (text with random binary characters)"))
449 ((eq type 'emacs-mule)
450 (princ " (Emacs 21 internal encoding)"))
451 (t (princ ": invalid coding-system.")))
452 (princ "\nEOL type: ")
453 (let ((eol-type (coding-system-eol-type coding-system)))
454 (cond ((vectorp eol-type)
455 (princ "Automatic selection from:\n\t")
456 (princ eol-type)
457 (princ "\n"))
458 ((or (null eol-type) (eq eol-type 0)) (princ "LF\n"))
459 ((eq eol-type 1) (princ "CRLF\n"))
460 ((eq eol-type 2) (princ "CR\n"))
461 (t (princ "invalid\n")))))
462 (let ((postread (coding-system-get coding-system :post-read-conversion)))
463 (when postread
464 (princ "After decoding text normally,")
465 (princ " perform post-conversion using the function: ")
466 (princ "\n ")
467 (princ postread)
468 (princ "\n")))
469 (let ((prewrite (coding-system-get coding-system :pre-write-conversion)))
470 (when prewrite
471 (princ "Before encoding text normally,")
472 (princ " perform pre-conversion using the function: ")
473 (princ "\n ")
474 (princ prewrite)
475 (princ "\n")))
476 (with-current-buffer standard-output
477 (let ((charsets (coding-system-charset-list coding-system)))
478 (when (and (not (eq (coding-system-base coding-system) 'raw-text))
479 charsets)
480 (cond
481 ((eq charsets 'iso-2022)
482 (insert "This coding system can encode all ISO 2022 charsets."))
483 ((eq charsets 'emacs-mule)
484 (insert "This coding system can encode all emacs-mule charsets\
485 ."""))
486 (t
487 (insert "This coding system encodes the following charsets:\n ")
488 (while charsets
489 (insert " " (symbol-name (car charsets)))
490 (search-backward (symbol-name (car charsets)))
491 (help-xref-button 0 'help-character-set (car charsets))
492 (goto-char (point-max))
493 (setq charsets (cdr charsets)))))))))))
494
495 ;;;###autoload
496 (defun describe-current-coding-system-briefly ()
497 "Display coding systems currently used in a brief format in echo area.
498
499 The format is \"F[..],K[..],T[..],P>[..],P<[..], default F[..],P<[..],P<[..]\",
500 where mnemonics of the following coding systems come in this order
501 in place of `..':
502 `buffer-file-coding-system' (of the current buffer)
503 eol-type of `buffer-file-coding-system' (of the current buffer)
504 Value returned by `keyboard-coding-system'
505 eol-type of `keyboard-coding-system'
506 Value returned by `terminal-coding-system'.
507 eol-type of `terminal-coding-system'
508 `process-coding-system' for read (of the current buffer, if any)
509 eol-type of `process-coding-system' for read (of the current buffer, if any)
510 `process-coding-system' for write (of the current buffer, if any)
511 eol-type of `process-coding-system' for write (of the current buffer, if any)
512 `default-buffer-file-coding-system'
513 eol-type of `default-buffer-file-coding-system'
514 `default-process-coding-system' for read
515 eol-type of `default-process-coding-system' for read
516 `default-process-coding-system' for write
517 eol-type of `default-process-coding-system'"
518 (interactive)
519 (let* ((proc (get-buffer-process (current-buffer)))
520 (process-coding-systems (if proc (process-coding-system proc))))
521 (message
522 "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]"
523 (coding-system-mnemonic buffer-file-coding-system)
524 (coding-system-eol-type-mnemonic buffer-file-coding-system)
525 (coding-system-mnemonic (keyboard-coding-system))
526 (coding-system-eol-type-mnemonic (keyboard-coding-system))
527 (coding-system-mnemonic (terminal-coding-system))
528 (coding-system-eol-type-mnemonic (terminal-coding-system))
529 (coding-system-mnemonic (car process-coding-systems))
530 (coding-system-eol-type-mnemonic (car process-coding-systems))
531 (coding-system-mnemonic (cdr process-coding-systems))
532 (coding-system-eol-type-mnemonic (cdr process-coding-systems))
533 (coding-system-mnemonic default-buffer-file-coding-system)
534 (coding-system-eol-type-mnemonic default-buffer-file-coding-system)
535 (coding-system-mnemonic (car default-process-coding-system))
536 (coding-system-eol-type-mnemonic (car default-process-coding-system))
537 (coding-system-mnemonic (cdr default-process-coding-system))
538 (coding-system-eol-type-mnemonic (cdr default-process-coding-system))
539 )))
540
541 (defun print-coding-system-briefly (coding-system &optional doc-string)
542 "Print symbol name and mnemonic letter of CODING-SYSTEM with `princ'.
543 If DOC-STRING is non-nil, print also the docstring of CODING-SYSTEM.
544 If DOC-STRING is `tightly', don't print an empty line before the
545 docstring, and print only the first line of the docstring."
546 (if (not coding-system)
547 (princ "nil\n")
548 (princ (format "%c -- %s"
549 (coding-system-mnemonic coding-system)
550 coding-system))
551 (let ((aliases (coding-system-aliases coding-system)))
552 (cond ((eq coding-system (car aliases))
553 (if (cdr aliases)
554 (princ (format " %S" (cons 'alias: (cdr aliases))))))
555 ((memq coding-system aliases)
556 (princ (format " (alias of %s)" (car aliases))))
557 (t
558 (let ((eol-type (coding-system-eol-type coding-system))
559 (base-eol-type (coding-system-eol-type (car aliases))))
560 (if (and (integerp eol-type)
561 (vectorp base-eol-type)
562 (not (eq coding-system (aref base-eol-type eol-type))))
563 (princ (format " (alias of %s)"
564 (aref base-eol-type eol-type))))))))
565 (princ "\n")
566 (or (eq doc-string 'tightly)
567 (princ "\n"))
568 (if doc-string
569 (let ((doc (or (coding-system-doc-string coding-system) "")))
570 (when (eq doc-string 'tightly)
571 (if (string-match "\n" doc)
572 (setq doc (substring doc 0 (match-beginning 0))))
573 (setq doc (concat " " doc)))
574 (princ (format "%s\n" doc))))))
575
576 ;;;###autoload
577 (defun describe-current-coding-system ()
578 "Display coding systems currently used, in detail."
579 (interactive)
580 (with-output-to-temp-buffer "*Help*"
581 (let* ((proc (get-buffer-process (current-buffer)))
582 (process-coding-systems (if proc (process-coding-system proc))))
583 (princ "Coding system for saving this buffer:\n ")
584 (if (local-variable-p 'buffer-file-coding-system)
585 (print-coding-system-briefly buffer-file-coding-system)
586 (princ "Not set locally, use the default.\n"))
587 (princ "Default coding system (for new files):\n ")
588 (print-coding-system-briefly default-buffer-file-coding-system)
589 (princ "Coding system for keyboard input:\n ")
590 (print-coding-system-briefly (keyboard-coding-system))
591 (princ "Coding system for terminal output:\n ")
592 (print-coding-system-briefly (terminal-coding-system))
593 (when (boundp 'selection-coding-system)
594 (princ "Coding system for inter-client cut and paste:\n ")
595 (print-coding-system-briefly selection-coding-system))
596 (when (get-buffer-process (current-buffer))
597 (princ "Coding systems for process I/O:\n")
598 (princ " encoding input to the process: ")
599 (print-coding-system-briefly (cdr process-coding-systems))
600 (princ " decoding output from the process: ")
601 (print-coding-system-briefly (car process-coding-systems)))
602 (princ "Defaults for subprocess I/O:\n")
603 (princ " decoding: ")
604 (print-coding-system-briefly (car default-process-coding-system))
605 (princ " encoding: ")
606 (print-coding-system-briefly (cdr default-process-coding-system)))
607
608 (with-current-buffer standard-output
609
610 (princ "
611 Priority order for recognizing coding systems when reading files:\n")
612 (let ((i 1))
613 (dolist (elt (coding-system-priority-list))
614 (princ (format " %d. %s " i elt))
615 (let ((aliases (coding-system-aliases elt)))
616 (if (eq elt (car aliases))
617 (if (cdr aliases)
618 (princ (cons 'alias: (cdr aliases))))
619 (princ (list 'alias 'of (car aliases))))
620 (terpri)
621 (setq i (1+ i)))))
622
623 (princ "\n Other coding systems cannot be distinguished automatically
624 from these, and therefore cannot be recognized automatically
625 with the present coding system priorities.\n\n")
626
627 ;; Fixme: should this be replaced or junked?
628 (if nil
629 (let ((categories '(coding-category-iso-7 coding-category-iso-7-else))
630 coding-system codings)
631 (while categories
632 (setq coding-system (symbol-value (car categories)))
633 (mapc
634 (lambda (x)
635 (if (and (not (eq x coding-system))
636 (let ((flags (coding-system-get :flags)))
637 (not (or (memq 'use-roman flags)
638 (memq 'use-oldjis flags)))))
639 (setq codings (cons x codings))))
640 (get (car categories) 'coding-systems))
641 (if codings
642 (let ((max-col (window-width))
643 pos)
644 (princ (format "\
645 The following are decoded correctly but recognized as %s:\n "
646 coding-system))
647 (while codings
648 (setq pos (point))
649 (insert (format " %s" (car codings)))
650 (when (> (current-column) max-col)
651 (goto-char pos)
652 (insert "\n ")
653 (goto-char (point-max)))
654 (setq codings (cdr codings)))
655 (insert "\n\n")))
656 (setq categories (cdr categories)))))
657
658 (princ "Particular coding systems specified for certain file names:\n")
659 (terpri)
660 (princ " OPERATION\tTARGET PATTERN\t\tCODING SYSTEM(s)\n")
661 (princ " ---------\t--------------\t\t----------------\n")
662 (let ((func (lambda (operation alist)
663 (princ " ")
664 (princ operation)
665 (if (not alist)
666 (princ "\tnothing specified\n")
667 (while alist
668 (indent-to 16)
669 (prin1 (car (car alist)))
670 (if (>= (current-column) 40)
671 (newline))
672 (indent-to 40)
673 (princ (cdr (car alist)))
674 (princ "\n")
675 (setq alist (cdr alist)))))))
676 (funcall func "File I/O" file-coding-system-alist)
677 (funcall func "Process I/O" process-coding-system-alist)
678 (funcall func "Network I/O" network-coding-system-alist))
679 (help-mode))))
680
681 (defun print-coding-system (coding-system)
682 "Print detailed information on CODING-SYSTEM."
683 (let ((type (coding-system-type coding-system))
684 (eol-type (coding-system-eol-type coding-system))
685 (flags (coding-system-get coding-system :flags))
686 (aliases (coding-system-aliases coding-system)))
687 (if (not (eq (car aliases) coding-system))
688 (princ (format "%s (alias of %s)\n" coding-system (car aliases)))
689 (princ coding-system)
690 (dolist (alias (cdr aliases))
691 (princ ",")
692 (princ alias))
693 (princ (format ":%s:%c:%d:"
694 type
695 (coding-system-mnemonic coding-system)
696 (if (integerp eol-type) eol-type 3)))
697 (cond ((eq type 'iso2022)
698 (let ((idx 0)
699 charset)
700 (while (< idx 4)
701 (setq charset (aref flags idx))
702 (cond ((null charset)
703 (princ -1))
704 ((eq charset t)
705 (princ -2))
706 ((charsetp charset)
707 (princ charset))
708 ((listp charset)
709 (princ "(")
710 (princ (car charset))
711 (setq charset (cdr charset))
712 (while charset
713 (princ ",")
714 (princ (car charset))
715 (setq charset (cdr charset)))
716 (princ ")")))
717 (princ ",")
718 (setq idx (1+ idx)))
719 (while (< idx 12)
720 (princ (if (aref flags idx) 1 0))
721 (princ ",")
722 (setq idx (1+ idx)))
723 (princ (if (aref flags idx) 1 0))))
724 ((eq type 'ccl)
725 (let (i len)
726 (if (symbolp (car flags))
727 (princ (format " %s" (car flags)))
728 (setq i 0 len (length (car flags)))
729 (while (< i len)
730 (princ (format " %x" (aref (car flags) i)))
731 (setq i (1+ i))))
732 (princ ",")
733 (if (symbolp (cdr flags))
734 (princ (format "%s" (cdr flags)))
735 (setq i 0 len (length (cdr flags)))
736 (while (< i len)
737 (princ (format " %x" (aref (cdr flags) i)))
738 (setq i (1+ i))))))
739 (t (princ 0)))
740 (princ ":")
741 (princ (coding-system-doc-string coding-system))
742 (princ "\n"))))
743
744 ;;;###autoload
745 (defun list-coding-systems (&optional arg)
746 "Display a list of all coding systems.
747 This shows the mnemonic letter, name, and description of each coding system.
748
749 With prefix ARG, the output format gets more cryptic,
750 but still contains full information about each coding system."
751 (interactive "P")
752 (with-output-to-temp-buffer "*Help*"
753 (list-coding-systems-1 arg)))
754
755 (defun list-coding-systems-1 (arg)
756 (if (null arg)
757 (princ "\
758 ###############################################
759 # List of coding systems in the following format:
760 # MNEMONIC-LETTER -- CODING-SYSTEM-NAME
761 # DOC-STRING
762 ")
763 (princ "\
764 #########################
765 ## LIST OF CODING SYSTEMS
766 ## Each line corresponds to one coding system
767 ## Format of a line is:
768 ## NAME[,ALIAS...]:TYPE:MNEMONIC:EOL:FLAGS:POST-READ-CONVERSION
769 ## :PRE-WRITE-CONVERSION:DOC-STRING,
770 ## where
771 ## NAME = coding system name
772 ## ALIAS = alias of the coding system
773 ## TYPE = nil (no conversion), t (undecided or automatic detection),
774 ## 0 (EMACS-MULE), 1 (SJIS), 2 (ISO2022), 3 (BIG5), or 4 (CCL)
775 ## EOL = 0 (LF), 1 (CRLF), 2 (CR), or 3 (Automatic detection)
776 ## FLAGS =
777 ## if TYPE = 2 then
778 ## comma (`,') separated data of the following:
779 ## G0, G1, G2, G3, SHORT-FORM, ASCII-EOL, ASCII-CNTL, SEVEN,
780 ## LOCKING-SHIFT, SINGLE-SHIFT, USE-ROMAN, USE-OLDJIS, NO-ISO6429
781 ## else if TYPE = 4 then
782 ## comma (`,') separated CCL programs for read and write
783 ## else
784 ## 0
785 ## POST-READ-CONVERSION, PRE-WRITE-CONVERSION = function name to be called
786 ##
787 "))
788 (dolist (coding-system (sort-coding-systems (coding-system-list 'base-only)))
789 (if (null arg)
790 (print-coding-system-briefly coding-system 'tightly)
791 (print-coding-system coding-system))))
792
793 ;; Fixme: delete?
794 ;;;###autoload
795 (defun list-coding-categories ()
796 "Display a list of all coding categories."
797 (with-output-to-temp-buffer "*Help*"
798 (princ "\
799 ############################
800 ## LIST OF CODING CATEGORIES (ordered by priority)
801 ## CATEGORY:CODING-SYSTEM
802 ##
803 ")
804 (let ((l coding-category-list))
805 (while l
806 (princ (format "%s:%s\n" (car l) (symbol-value (car l))))
807 (setq l (cdr l))))))
808 \f
809 ;;; FONT
810
811 (declare-function font-info "font.c" (name &optional frame))
812
813 (defun describe-font-internal (font-info &optional verbose)
814 "Print information about a font in FONT-INFO."
815 (print-list "name (opened by):" (aref font-info 0))
816 (print-list " full name:" (aref font-info 1))
817 (print-list " size:" (format "%2d" (aref font-info 2)))
818 (print-list " height:" (format "%2d" (aref font-info 3)))
819 (print-list " baseline-offset:" (format "%2d" (aref font-info 4)))
820 (print-list "relative-compose:" (format "%2d" (aref font-info 5))))
821
822 ;;;###autoload
823 (defun describe-font (fontname)
824 "Display information about a font whose name is FONTNAME.
825 The font must be already used by Emacs."
826 (interactive "sFont name (default current choice for ASCII chars): ")
827 (or (and window-system (fboundp 'fontset-list))
828 (error "No fonts being used"))
829 (let (font-info)
830 (if (or (not fontname) (= (length fontname) 0))
831 (setq fontname (face-attribute 'default :font)))
832 (setq font-info (font-info fontname))
833 (if (null font-info)
834 (if (fontp fontname 'font-object)
835 ;; The font should be surely used. So, there's some
836 ;; problem about getting information about it. It is
837 ;; better to print the fontname to show which font has
838 ;; this problem.
839 (message "No information about \"%s\"" (font-xlfd-name fontname))
840 (message "No matching font found"))
841 (with-output-to-temp-buffer "*Help*"
842 (describe-font-internal font-info 'verbose)))))
843
844 (defun print-fontset-element (val)
845 ;; VAL has this format:
846 ;; ((REQUESTED-FONT-NAME OPENED-FONT-NAME ...) ...)
847 ;; CHAR RANGE is already inserted. Get character codes from
848 ;; the current line.
849 (beginning-of-line)
850 (let ((from (following-char))
851 (to (if (looking-at "[^.]*[.]* ")
852 (char-after (match-end 0)))))
853 (if (re-search-forward "[ \t]*$" nil t)
854 (delete-region (match-beginning 0) (match-end 0)))
855
856 ;; For non-ASCII characters, insert also CODE RANGE.
857 (if (or (>= from 128) (and to (>= to 128)))
858 (if to
859 (insert (format " (#x%02X .. #x%02X)" from to))
860 (insert (format " (#x%02X)" from))))
861
862 ;; Insert a requested font name.
863 (dolist (elt val)
864 (if (not elt)
865 (insert "\n -- inhibit fallback fonts --")
866 (let ((requested (car elt)))
867 (if (stringp requested)
868 (insert "\n " requested)
869 (let (family registry weight slant width adstyle)
870 (if (and (fboundp 'fontp) (fontp requested))
871 (setq family (font-get requested :family)
872 registry (font-get requested :registry)
873 weight (font-get requested :weight)
874 slant (font-get requested :slant)
875 width (font-get requested :width)
876 adstyle (font-get requested :adstyle))
877 (setq family (aref requested 0)
878 registry (aref requested 5)
879 weight (aref requested 1)
880 slant (aref requested 2)
881 width (aref requested 3)
882 adstyle (aref requested 4)))
883 (if (not family)
884 (setq family "*-*")
885 (if (symbolp family)
886 (setq family (symbol-name family)))
887 (or (string-match "-" family)
888 (setq family (concat "*-" family))))
889 (if (not registry)
890 (setq registry "*-*")
891 (if (symbolp registry)
892 (setq registry (symbol-name registry)))
893 (or (string-match "-" registry)
894 (= (aref registry (1- (length registry))) ?*)
895 (setq registry (concat registry "*"))))
896 (insert (format"\n -%s-%s-%s-%s-%s-*-*-*-*-*-*-%s"
897 family (or weight "*") (or slant "*") (or width "*")
898 (or adstyle "*") registry)))))
899
900 ;; Insert opened font names (if any).
901 (if (and (boundp 'print-opened) (symbol-value 'print-opened))
902 (dolist (opened (cdr elt))
903 (insert "\n\t[" opened "]")))))))
904
905 (declare-function query-fontset "fontset.c" (pattern &optional regexpp))
906 (declare-function fontset-info "fontset.c" (fontset &optional frame))
907
908 (defun print-fontset (fontset &optional print-opened)
909 "Print information about FONTSET.
910 If FONTSET is nil, print information about the default fontset.
911 If optional arg PRINT-OPENED is non-nil, also print names of all opened
912 fonts for FONTSET. This function actually inserts the information in
913 the current buffer."
914 (or fontset
915 (setq fontset (query-fontset "fontset-default")))
916 (beginning-of-line)
917 (insert "Fontset: " fontset "\n")
918 (insert (propertize "CHAR RANGE" 'face 'underline)
919 " (" (propertize "CODE RANGE" 'face 'underline) ")\n")
920 (insert " " (propertize "FONT NAME" 'face 'underline)
921 " (" (propertize "REQUESTED" 'face 'underline)
922 " and [" (propertize "OPENED" 'face 'underline) "])")
923 (let ((info (fontset-info fontset)))
924 (describe-vector info 'print-fontset-element)
925 (insert "\n ---<fallback to the default fontset>---")
926 (describe-vector (char-table-extra-slot info 0) 'print-fontset-element)))
927
928 (defvar fontset-alias-alist)
929 (declare-function fontset-list "fontset.c" ())
930
931 ;;;###autoload
932 (defun describe-fontset (fontset)
933 "Display information about FONTSET.
934 This shows which font is used for which character(s)."
935 (interactive
936 (if (not (and window-system (fboundp 'fontset-list)))
937 (error "No fontsets being used")
938 (let ((fontset-list (nconc
939 (fontset-list)
940 (mapcar 'cdr fontset-alias-alist)))
941 (completion-ignore-case t))
942 (list (completing-read
943 "Fontset (default used by the current frame): "
944 fontset-list nil t)))))
945 (if (= (length fontset) 0)
946 (setq fontset (frame-parameter nil 'font)))
947 (setq fontset (query-fontset fontset))
948 (help-setup-xref (list #'describe-fontset fontset) (interactive-p))
949 (with-output-to-temp-buffer (help-buffer)
950 (with-current-buffer standard-output
951 (print-fontset fontset t))))
952
953 (declare-function fontset-plain-name "fontset" (fontset))
954
955 ;;;###autoload
956 (defun list-fontsets (arg)
957 "Display a list of all fontsets.
958 This shows the name, size, and style of each fontset.
959 With prefix arg, also list the fonts contained in each fontset;
960 see the function `describe-fontset' for the format of the list."
961 (interactive "P")
962 (if (not (and window-system (fboundp 'fontset-list)))
963 (error "No fontsets being used")
964 (help-setup-xref (list #'list-fontsets arg) (interactive-p))
965 (with-output-to-temp-buffer (help-buffer)
966 (with-current-buffer standard-output
967 ;; This code is duplicated near the end of mule-diag.
968 (let ((fontsets
969 (sort (fontset-list)
970 (lambda (x y)
971 (string< (fontset-plain-name x)
972 (fontset-plain-name y))))))
973 (while fontsets
974 (if arg
975 (print-fontset (car fontsets) nil)
976 (insert "Fontset: " (car fontsets) "\n"))
977 (setq fontsets (cdr fontsets))))))))
978 \f
979 ;;;###autoload
980 (defun list-input-methods ()
981 "Display information about all input methods."
982 (interactive)
983 (help-setup-xref '(list-input-methods) (interactive-p))
984 (with-output-to-temp-buffer (help-buffer)
985 (list-input-methods-1)
986 (with-current-buffer standard-output
987 (save-excursion
988 (goto-char (point-min))
989 (while (re-search-forward
990 "^ \\([^ ]+\\) (`.*' in mode line)$" nil t)
991 (help-xref-button 1 'help-input-method (match-string 1)))))))
992
993 (defun list-input-methods-1 ()
994 (if (not input-method-alist)
995 (princ "
996 No input method is available, perhaps because you have not
997 installed LEIM (Libraries of Emacs Input Methods).")
998 (princ "LANGUAGE\n NAME (`TITLE' in mode line)\n")
999 (princ " SHORT-DESCRIPTION\n------------------------------\n")
1000 (setq input-method-alist
1001 (sort input-method-alist
1002 (lambda (x y) (string< (nth 1 x) (nth 1 y)))))
1003
1004 (let (language)
1005 (dolist (elt input-method-alist)
1006 (when (not (equal language (nth 1 elt)))
1007 (setq language (nth 1 elt))
1008 (princ language)
1009 (terpri))
1010 (princ (format " %s (`%s' in mode line)\n %s\n"
1011 (car elt)
1012 (let ((title (nth 3 elt)))
1013 (if (and (consp title) (stringp (car title)))
1014 (car title)
1015 title))
1016 (nth 4 elt)))))))
1017 \f
1018 ;;; DIAGNOSIS
1019
1020 ;; Insert a header of a section with SECTION-NUMBER and TITLE.
1021 (defun insert-section (section-number title)
1022 (insert "########################################\n"
1023 "# Section " (format "%d" section-number) ". " title "\n"
1024 "########################################\n\n"))
1025
1026 ;;;###autoload
1027 (defun mule-diag ()
1028 "Display diagnosis of the multilingual environment (Mule).
1029
1030 This shows various information related to the current multilingual
1031 environment, including lists of input methods, coding systems,
1032 character sets, and fontsets (if Emacs is running under a window
1033 system which uses fontsets)."
1034 (interactive)
1035 (with-output-to-temp-buffer "*Mule-Diagnosis*"
1036 (with-current-buffer standard-output
1037 (insert "###############################################\n"
1038 "### Current Status of Multilingual Features ###\n"
1039 "###############################################\n\n"
1040 "CONTENTS: Section 1. General Information\n"
1041 " Section 2. Display\n"
1042 " Section 3. Input methods\n"
1043 " Section 4. Coding systems\n"
1044 " Section 5. Character sets\n")
1045 (if (and window-system (fboundp 'fontset-list))
1046 (insert " Section 6. Fontsets\n"))
1047 (insert "\n")
1048
1049 (insert-section 1 "General Information")
1050 (insert "Version of this emacs:\n " (emacs-version) "\n\n")
1051 (insert "Configuration options:\n " system-configuration-options "\n\n")
1052 (insert "Multibyte characters awareness:\n"
1053 (format " default: %S\n" default-enable-multibyte-characters)
1054 (format " current-buffer: %S\n\n" enable-multibyte-characters))
1055 (insert "Current language environment: " current-language-environment
1056 "\n\n")
1057
1058 (insert-section 2 "Display")
1059 (if window-system
1060 (insert (format "Window-system: %s, version %s"
1061 window-system window-system-version))
1062 (insert "Terminal: " (getenv "TERM")))
1063 (insert "\n\n")
1064
1065 (if window-system
1066 (let ((font (cdr (assq 'font (frame-parameters)))))
1067 (insert "The selected frame is using the "
1068 (if (query-fontset font) "fontset" "font")
1069 ":\n\t" font))
1070 (insert "Coding system of the terminal: "
1071 (symbol-name (terminal-coding-system))))
1072 (insert "\n\n")
1073
1074 (insert-section 3 "Input methods")
1075 (list-input-methods-1)
1076 (insert "\n")
1077 (if default-input-method
1078 (insert (format "Default input method: %s\n" default-input-method))
1079 (insert "No default input method is specified\n"))
1080
1081 (insert-section 4 "Coding systems")
1082 (list-coding-systems-1 t)
1083 (insert "\n")
1084
1085 (insert-section 5 "Character sets")
1086 (list-character-sets-2)
1087 (insert "\n")
1088
1089 (when (and window-system (fboundp 'fontset-list))
1090 ;; This code duplicates most of list-fontsets.
1091 (insert-section 6 "Fontsets")
1092 (insert "Fontset-Name\t\t\t\t\t\t WDxHT Style\n")
1093 (insert "------------\t\t\t\t\t\t ----- -----\n")
1094 (dolist (fontset (fontset-list))
1095 (print-fontset fontset t)))
1096 (print-help-return-message))))
1097
1098 ;;;###autoload
1099 (defun font-show-log (&optional limit)
1100 "Show log of font listing and opening.
1101 Prefix arg LIMIT says how many fonts to show for each listing.
1102 The default is 20. If LIMIT is negative, do not limit the listing."
1103 (interactive "P")
1104 (setq limit (if limit (prefix-numeric-value limit) 20))
1105 (if (eq font-log t)
1106 (message "Font logging is currently suppressed")
1107 (with-output-to-temp-buffer "*Help*"
1108 (set-buffer standard-output)
1109 (dolist (elt (reverse font-log))
1110 (insert (format "%s: %s\n" (car elt) (cadr elt)))
1111 (setq elt (nth 2 elt))
1112 (if (or (vectorp elt) (listp elt))
1113 (let ((i 0))
1114 (catch 'tag
1115 (mapc #'(lambda (x)
1116 (setq i (1+ i))
1117 (when (= i limit)
1118 (insert " ...\n")
1119 (throw 'tag nil))
1120 (insert (format " %s\n" x)))
1121 elt)))
1122 (insert (format " %s\n" elt)))))))
1123
1124
1125 (provide 'mule-diag)
1126
1127 ;; arch-tag: cd3b607c-2893-45a0-a4fa-a6535754dbee
1128 ;;; mule-diag.el ends here