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