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