mule-diag.el 47.2 KB
Newer Older
1
;;; mule-diag.el --- Show diagnosis of multilingual environment (Mule)
Karl Heuer's avatar
Karl Heuer committed
2 3

;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
Richard M. Stallman's avatar
Richard M. Stallman committed
4
;; Licensed to the Free Software Foundation.
Karl Heuer's avatar
Karl Heuer committed
5

Dave Love's avatar
Dave Love committed
6
;; Keywords: multilingual, charset, coding system, fontset, diagnosis, i18n
Karl Heuer's avatar
Karl Heuer committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20

;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
Karl Heuer's avatar
Karl Heuer committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Karl Heuer's avatar
Karl Heuer committed
24 25 26 27 28 29

;;; General utility function

;; Print all arguments with single space separator in one line.
(defun print-list (&rest args)
  (while (cdr args)
Kenichi Handa's avatar
Kenichi Handa committed
30 31 32
    (when (car args)
      (princ (car args))
      (princ " "))
Karl Heuer's avatar
Karl Heuer committed
33 34 35 36
    (setq args (cdr args)))
  (princ (car args))
  (princ "\n"))

Kenichi Handa's avatar
Kenichi Handa committed
37 38 39 40 41 42
;; Re-order the elements of charset-list.
(defun sort-charset-list ()
  (setq charset-list
	(sort charset-list
	      (function (lambda (x y) (< (charset-id x) (charset-id y)))))))

Karl Heuer's avatar
Karl Heuer committed
43 44 45
;;; CHARSET

;;;###autoload
46
(defun list-character-sets (arg)
Kenichi Handa's avatar
Kenichi Handa committed
47 48
  "Display a list of all character sets.

49 50 51 52 53 54 55 56 57 58 59 60 61
The ID-NUM column contains a charset identification number
  for internal Emacs use.

The MULTIBYTE-FORM column contains a format of multibyte sequence
  of characters in the charset for buffer and string
  by one to four hexadecimal digits.
  `xx' stands for any byte in the range 0..127.
  `XX' stands for any byte in the range 160..255.

The D column contains a dimension of this character set.
The CH column contains a number of characters in a block of this character set.
The FINAL-CHAR column contains an ISO-2022's <final-char> to use for
  designating this character set in ISO-2022-based coding systems.
Kenichi Handa's avatar
Kenichi Handa committed
62

Karl Heuer's avatar
Karl Heuer committed
63 64
With prefix arg, the output format gets more cryptic,
but still shows the full information."
Kenichi Handa's avatar
Kenichi Handa committed
65
  (interactive "P")
Karl Heuer's avatar
Karl Heuer committed
66
  (with-output-to-temp-buffer "*Help*"
67 68 69 70 71 72
    (with-current-buffer standard-output
      (if arg
	  (list-character-sets-2)
	;; Insert header.
	(insert
	 (substitute-command-keys
73 74 75 76
	  (concat "Use "
		  (if (display-mouse-p) "\\[help-follow-mouse] or ")
		  "\\[help-follow]:\n")))
	(insert "  on a column title to sort by that title,")
77 78
	(indent-to 56)
	(insert "+----DIMENSION\n")
79
	(insert "  on a charset name to list characters.")
80 81 82 83 84 85 86
	(indent-to 56)
	(insert "| +--CHARS\n")
	(let ((columns '(("ID-NUM" . id) "\t"
			 ("CHARSET-NAME" . name) "\t\t\t"
			 ("MULTIBYTE-FORM" . id) "\t"
			 ("D CH FINAL-CHAR" . iso-spec)))
	      (help-highlight-face 'region)
87 88 89 90
	      (help-echo
	       (substitute-command-keys
		(concat (if (display-mouse-p) "\\[help-follow-mouse], ")
			"\\[help-follow]: sort on this column")))
91 92 93 94 95 96 97
	      pos)
	  (while columns
	    (if (stringp (car columns))
		(insert (car columns))
	      (insert (car (car columns)))
	      (search-backward (car (car columns)))
	      (help-xref-button 0 'sort-listed-character-sets
Dave Love's avatar
Dave Love committed
98
				(cdr (car columns))
99
				help-echo)
100 101 102 103
	      (goto-char (point-max)))
	    (setq columns (cdr columns)))
	  (insert "\n"))
	(insert "------\t------------\t\t\t--------------\t- -- ----------\n")
104

105
	;; Insert body sorted by charset IDs.
Dave Love's avatar
Dave Love committed
106 107
	(list-character-sets-1 'id)
	(help-setup-xref (list #'list-character-sets arg) (interactive-p))))))
108 109 110 111 112 113 114 115 116 117 118 119


;; Sort character set list by SORT-KEY.

(defun sort-listed-character-sets (sort-key)
  (if sort-key
      (save-excursion
	(let ((buffer-read-only nil))
	  (goto-char (point-min))
	  (re-search-forward "[0-9][0-9][0-9]")
	  (beginning-of-line)
	  (delete-region (point) (point-max))
120 121
	  (list-character-sets-1 sort-key)
	  (help-setup-xref (list #'list-character-sets nil) t)))))
122

123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
(defun charset-multibyte-form-string (charset)
  (let ((info (charset-info charset)))
    (cond ((eq charset 'ascii)
	   "xx")
	  ((eq charset 'eight-bit-control)
	   (format "%2X Xx" (aref info 6)))
	  ((eq charset 'eight-bit-graphic)
	   "XX")
	  (t
	   (let ((str (format "%2X" (aref info 6))))
	     (if (> (aref info 7) 0)
		 (setq str (format "%s %2X"
				   str (aref info 7))))
	     (setq str (concat str " XX"))
	     (if (> (aref info 2) 1)
		 (setq str (concat str " XX")))
	     str)))))

141 142 143 144 145 146 147 148
;; Insert a list of character sets sorted by SORT-KEY.  SORT-KEY
;; should be one of `id', `name', and `iso-spec'.  If SORT-KEY is nil,
;; it defaults to `id'.

(defun list-character-sets-1 (sort-key)
  (or sort-key
      (setq sort-key 'id))
  (let ((tail (charset-list))
149 150 151 152
	(help-echo
	 (substitute-command-keys
	  (concat (if (display-mouse-p) "\\[help-follow-mouse], ")
		  "\\[help-follow]: show table of this character set")))
153 154 155 156 157 158 159 160 161
	charset-info-list elt charset info sort-func)
    (while tail
      (setq charset (car tail) tail (cdr tail)
	    info (charset-info charset))

      ;; Generate a list that contains all information to display.
      (setq charset-info-list
	    (cons (list (charset-id charset)	; ID-NUM
			charset			; CHARSET-NAME
162
			(charset-multibyte-form-string charset); MULTIBYTE-FORM
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
			(aref info 2)		; DIMENSION
			(aref info 3)		; CHARS
			(aref info 8)		; FINAL-CHAR
			)
		  charset-info-list)))

    ;; Determine a predicate for `sort' by SORT-KEY.
    (setq sort-func
	  (cond ((eq sort-key 'id)
		 (function (lambda (x y) (< (car x) (car y)))))

		((eq sort-key 'name)
		 (function (lambda (x y) (string< (nth 1 x) (nth 1 y)))))

		((eq sort-key 'iso-spec)
		 ;; Sort by DIMENSION CHARS FINAL-CHAR
		 (function
		  (lambda (x y)
		    (or (< (nth 3 x) (nth 3 y))
			(and (= (nth 3 x) (nth 3 y))
			     (or (< (nth 4 x) (nth 4 y))
				 (and (= (nth 4 x) (nth 4 y))
				      (< (nth 5 x) (nth 5 y)))))))))
		(t
		 (error "Invalid charset sort key: %s" sort-key))))

    (setq charset-info-list (sort charset-info-list sort-func))

    ;; Insert information of character sets.
    (while charset-info-list
      (setq elt (car charset-info-list)
	    charset-info-list (cdr charset-info-list))
      (insert (format "%03d(%02X)" (car elt) (car elt))) ; ID-NUM
      (indent-to 8)
      (insert (symbol-name (nth 1 elt))) ; CHARSET-NAME
      (search-backward (symbol-name (nth 1 elt)))
199
      (help-xref-button 0 'list-charset-chars (nth 1 elt) help-echo)
200 201 202 203 204
      (goto-char (point-max))
      (insert "\t")
      (indent-to 40)
      (insert (nth 2 elt))		; MULTIBYTE-FORM
      (indent-to 56)
205 206
      (insert (format "%d %2d " (nth 3 elt) (nth 4 elt)) ; DIMENSION and CHARS
	      (if (< (nth 5 elt) 0) "none" (nth 5 elt))) ; FINAL-CHAR
207 208 209 210 211 212 213
      (insert "\n"))))


;; List all character sets in a form that a program can easily parse.

(defun list-character-sets-2 ()
  (insert "#########################
Karl Heuer's avatar
Karl Heuer committed
214 215 216 217 218
## LIST OF CHARSETS
## Each line corresponds to one charset.
## The following attributes are listed in this order
## separated by a colon `:' in one line.
##	CHARSET-ID,
Kenichi Handa's avatar
Kenichi Handa committed
219
##	CHARSET-SYMBOL-NAME,
Karl Heuer's avatar
Karl Heuer committed
220 221 222 223 224 225 226 227 228
##	DIMENSION (1 or 2)
##	CHARS (94 or 96)
##	BYTES (of multibyte form: 1, 2, 3, or 4),
##	WIDTH (occupied column numbers: 1 or 2),
##	DIRECTION (0:left-to-right, 1:right-to-left),
##	ISO-FINAL-CHAR (character code of ISO-2022's final character)
##	ISO-GRAPHIC-PLANE (ISO-2022's graphic plane, 0:GL, 1:GR)
##	DESCRIPTION (describing string of the charset)
")
229 230 231 232
  (let ((l charset-list)
	charset)
    (while l
      (setq charset (car l) l (cdr l))
233
      (princ (format "%03d:%s:%d:%d:%d:%d:%d:%d:%d:%s\n"
234 235 236 237 238 239 240 241 242 243 244 245 246 247
		     (charset-id charset)
		     charset
		     (charset-dimension charset)
		     (charset-chars charset)
		     (charset-bytes charset)
		     (charset-width charset)
		     (charset-direction charset)
		     (charset-iso-final-char charset)
		     (charset-iso-graphic-plane charset)
		     (charset-description charset))))))

(defvar non-iso-charset-alist
  `((viscii
     (ascii vietnamese-viscii-lower vietnamese-viscii-upper)
248
     viet-viscii-nonascii-translation-table
249 250 251
     ((0 255)))
    (koi8-r
     (ascii cyrillic-iso8859-5)
252
     cyrillic-koi8-r-nonascii-translation-table
253 254 255
     ((32 255)))
    (alternativnyj
     (ascii cyrillic-iso8859-5)
256
     cyrillic-alternativnyj-nonascii-translation-table
257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
     ((32 255)))
    (big5
     (ascii chinese-big5-1 chinese-big5-2)
     decode-big5-char
     ((32 127)
      ((?\xA1 ?\xFE) . (?\x40 ?\x7E ?\xA1 ?\xFE))))
    (sjis
     (ascii katakana-jisx0201 japanese-jisx0208)
     decode-sjis-char
     ((32 127 ?\xA1 ?\xDF)
      ((?\x81 ?\x9F ?\xE0 ?\xEF) . (?\x40 ?\x7E ?\x80 ?\xFC)))))
  "Alist of non-ISO charset names vs the corresponding information.

Non-ISO charsets are what Emacs can read (or write) by mapping to (or
from) some Emacs' charsets that correspond to ISO charsets.

Each element has the following format:
  (NON-ISO-CHARSET CHARSET-LIST TRANSLATION-METHOD [ CODE-RANGE ])

NON-ISO-CHARSET is a name (symbol) of the non-ISO charset.

CHARSET-LIST is a list of Emacs' charsets into which characters of
NON-ISO-CHARSET are mapped.

281
TRANSLATION-METHOD is a translation table (symbol) to translate a
282 283 284
character code of NON-ISO-CHARSET to the corresponding Emacs character
code.  It can also be a function to call with one argument, a
character code in NON-ISO-CHARSET.
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353

CODE-RANGE specifies the valid code ranges of NON-ISO-CHARSET.
It is a list of RANGEs, where each RANGE is of the form:
  (FROM1 TO1 FROM2 TO2 ...)
or
  ((FROM1-1 TO1-1 FROM1-2 TO1-2 ...) . (FROM2-1 TO2-1 FROM2-2 TO2-2 ...))
In the first form, valid codes are between FROM1 and TO1, or FROM2 and
TO2, or...
The second form is used for 2-byte codes.  The car part is the ranges
of the first byte, and the cdr part is the ranges of the second byte.")


;; Decode a character that has code CODE in CODEPAGE.  Value is a
;; string of decoded character.

(defun decode-codepage-char (codepage code)
  ;; Each CODEPAGE corresponds to a coding system cpCODEPAGE.
  (let ((coding-system (intern (format "cp%d" codepage))))
    (or (coding-system-p coding-system)
	(codepage-setup codepage))
    (string-to-char
     (decode-coding-string (char-to-string code) coding-system))))


;; Add DOS codepages to `non-iso-charset-alist'.

(let ((tail (cp-supported-codepages))
      elt)
  (while tail
    (setq elt (car tail) tail (cdr tail))
    ;; Now ELT is (CODEPAGE . CHARSET), where CODEPAGE is a string
    ;; (e.g. "850"), CHARSET is a charset that characters in CODEPAGE
    ;; are mapped to.
    (setq non-iso-charset-alist
	  (cons (list (intern (concat "cp" (car elt)))
		      (list 'ascii (cdr elt))
		      `(lambda (code)
			 (decode-codepage-char ,(string-to-int (car elt))
					       code))
		      (list (list 0 255)))
		non-iso-charset-alist))))


;; A variable to hold charset input history.
(defvar charset-history nil)


;;;###autoload
(defun read-charset (prompt &optional default-value initial-input)
  "Read a character set from the minibuffer, prompting with string PROMPT.
It reads an Emacs' character set listed in the variable `charset-list'
or a non-ISO character set listed in the variable
`non-iso-charset-alist'.

Optional arguments are DEFAULT-VALUE and INITIAL-INPUT.
DEFAULT-VALUE, if non-nil, is the default value.
INITIAL-INPUT, if non-nil, is a string inserted in the minibuffer initially.
See the documentation of the function `completing-read' for the
detailed meanings of these arguments."
  (let* ((table (append (mapcar (function (lambda (x) (list (symbol-name x))))
				charset-list)
			(mapcar (function (lambda (x)
					    (list (symbol-name (car x)))))
				non-iso-charset-alist)))
	 (charset (completing-read prompt table
				   nil t initial-input 'charset-history
				   default-value)))
    (if (> (length charset) 0)
	(intern charset))))
354

355 356 357 358 359

;; List characters of the range MIN and MAX of CHARSET.  If dimension
;; of CHARSET is two (i.e. 2-byte charset), ROW is the first byte
;; (block index) of the characters, and MIN and MAX are the second
;; bytes of the characters.  If the dimension is one, ROW should be 0.
360 361 362
;; For a non-ISO charset, CHARSET is a translation table (symbol) or a
;; function to get Emacs' character codes that corresponds to the
;; characters to list.
363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

(defun list-block-of-chars (charset row min max)
  (let (i ch)
    (insert-char ?- (+ 4 (* 3 16)))
    (insert "\n    ")
    (setq i 0)
    (while (< i 16)
      (insert (format "%3X" i))
      (setq i (1+ i)))
    (setq i (* (/ min 16) 16))
    (while (<= i max)
      (if (= (% i 16) 0)
	  (insert (format "\n%3Xx" (/ (+ (* row 256) i) 16))))
      (setq ch (cond ((< i min)
		      32)
		     ((charsetp charset)
		      (if (= row 0)
			  (make-char charset i)
			(make-char charset row i)))
382 383
		     ((and (symbolp charset) (get charset 'translation-table))
		      (aref (get charset 'translation-table) i))
384
		     (t (funcall charset (+ (* row 256) i)))))
385 386
      (if (and (char-table-p charset)
	       (or (< ch 32) (and (>= ch 127) (<= ch 255))))
387 388
	  ;; Don't insert a control code.
	  (setq ch 32))
389 390
      (indent-to (+ (* (% i 16) 3) 6))
      (insert ch)
391 392 393 394 395 396 397 398 399 400 401 402 403
      (setq i (1+ i))))
  (insert "\n"))


;; List all characters in ISO charset CHARSET.

(defun list-iso-charset-chars (charset)
  (let ((dim (charset-dimension charset))
	(chars (charset-chars charset))
	(plane (charset-iso-graphic-plane charset))
	min max)
    (insert (format "Characters in the charset %s.\n" charset))

404 405 406 407 408 409 410 411 412 413
    (cond ((eq charset 'eight-bit-control)
	   (setq min 128 max 159))
	  ((eq charset 'eight-bit-graphic)
	   (setq min 160 max 255))
	  (t
	   (if (= chars 94)
	       (setq min 33 max 126)
	     (setq min 32 max 127))
	   (or (= plane 0)
	       (setq min (+ min 128) max (+ max 128)))))
414 415 416 417

    (if (= dim 1)
	(list-block-of-chars charset 0 min max)
      (let ((i min))
418
	(while (<= i max)
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434
	  (list-block-of-chars charset i min max)
	  (setq i (1+ i)))))))


;; List all characters in non-ISO charset CHARSET.

(defun list-non-iso-charset-chars (charset)
  (let* ((slot (assq charset non-iso-charset-alist))
	 (charsets (nth 1 slot))
	 (translate-method (nth 2 slot))
	 (ranges (nth 3 slot))
	 range)
    (or slot
	(error "Unknown external charset: %s" charset))
    (insert (format "Characters in non-ISO charset %s.\n" charset))
    (insert "They are mapped to: "
435
	    (mapconcat #'symbol-name charsets ", ")
436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451
	    "\n")
    (while ranges
      (setq range (car ranges) ranges (cdr ranges))
      (if (integerp (car range))
	  ;; The form of RANGES is (FROM1 TO1 FROM2 TO2 ...).
	  (while range
	    (list-block-of-chars translate-method
				 0 (car range) (nth 1 range))
	    (setq range (nthcdr 2 range)))
	;; The form of RANGES is ((FROM1-1 TO1-1 ...) . (FROM2-1 TO2-1 ...)).
	(let ((row-range (car range))
	      row row-max
	      col-range col col-max)
	  (while row-range
	    (setq row (car row-range) row-max (nth 1 row-range)
		  row-range (nthcdr 2 row-range))
452
	    (while (<= row row-max)
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
	      (setq col-range (cdr range))
	      (while col-range
		(setq col (car col-range) col-max (nth 1 col-range)
		      col-range (nthcdr 2 col-range))
		(list-block-of-chars translate-method row col col-max))
	      (setq row (1+ row)))))))))


;;;###autoload
(defun list-charset-chars (charset)
  "Display a list of characters in the specified character set."
  (interactive (list (read-charset "Character set: ")))
  (with-output-to-temp-buffer "*Help*"
    (with-current-buffer standard-output
      (set-buffer-multibyte t)
      (cond ((charsetp charset)
	     (list-iso-charset-chars charset))
	    ((assq charset non-iso-charset-alist)
	     (list-non-iso-charset-chars charset))
	    (t
	     (error "Invalid charset %s" charset))))))

475

476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
;;;###autoload
(defun describe-character-set (charset)
  "Display information about character set CHARSET."
  (interactive (list (let ((non-iso-charset-alist nil))
		       (read-charset "Charset: "))))
  (or (charsetp charset)
      (error "Invalid charset: %S" charset))
  (let ((info (charset-info charset)))
    (with-output-to-temp-buffer "*Help*"
      (save-excursion
	(set-buffer standard-output)
	(insert "Character set: " (symbol-name charset)
		(format " (ID:%d)\n\n" (aref info 0)))
	(insert (aref info 13) "\n\n")	; description
	(insert "number of contained characters: "
		(if (= (aref info 2) 1)
		    (format "%d\n" (aref info 3))
		  (format "%dx%d\n" (aref info 3) (aref info 3))))
494
	(insert "the final char of ISO2022's designation sequence: ")
495 496 497 498 499 500 501 502 503
	(if (aref info 8)
	    (insert (format "`%c'\n" (aref info 8)))
	  (insert "not assigned\n"))
	(insert (format "width (how many columns on screen): %d\n"
			(aref info 4)))
	(insert (format "internal multibyte sequence: %s\n"
			(charset-multibyte-form-string charset)))
	(let ((coding (plist-get (aref info 14) 'preferred-coding-system)))
	  (when coding
504
	    (insert (format "preferred coding system: %s\n" coding))
505 506 507 508 509 510 511
	    (search-backward (symbol-name coding))
	    (help-xref-button 0 #'describe-coding-system coding
			      "mouse-2, RET: describe this coding system")))
	(help-setup-xref (list #'describe-character-set charset)
			 (interactive-p))
	))))

512 513
;;;###autoload
(defun describe-char-after (&optional pos)
Dave Love's avatar
Dave Love committed
514 515
  "Display information about the character at POS in the current buffer.
POS defaults to point.
516 517 518 519 520 521 522 523 524 525 526 527 528
The information includes character code, charset and code points in it,
syntax, category, how the character is encoded in a file,
which font is being used for displaying the character."
  (interactive)
  (or pos
      (setq pos (point)))
  (if (>= pos (point-max))
      (error "No character at point"))
  (let* ((char (char-after pos))
	 (charset (char-charset char))
	 (composition (find-composition (point) nil nil t))
	 (composed (if composition (buffer-substring (car composition)
						     (nth 1 composition))))
529
	 (multibyte-p enable-multibyte-characters)
530
	 item-list max-width)
Kenichi Handa's avatar
Kenichi Handa committed
531 532 533 534 535 536 537 538
    (if (eq charset 'unknown)
	(setq item-list
	      `(("character"
		 ,(format "%s (0%o, %d, 0x%x) -- invalid character code"
			  (if (< char 256)
			      (single-key-description char)
			    (char-to-string char))
			  char char char))))
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
      (setq item-list
	    `(("character"
	       ,(format "%s (0%o, %d, 0x%x)" (if (< char 256)
						 (single-key-description char)
					       (char-to-string char))
			char char char))
	      ("charset"
	       ,(symbol-name charset)
	       ,(format "(%s)" (charset-description charset)))
	      ("code point"
	       ,(let ((split (split-char char)))
		  (if (= (charset-dimension charset) 1)
		      (format "%d" (nth 1 split))
		    (format "%d %d" (nth 1 split) (nth 2 split)))))
	      ("syntax"
	       ,(nth 2 (assq (char-syntax char) syntax-code-table)))
	      ("category"
	       ,@(let ((category-set (char-category-set char)))
		   (if (not category-set)
		       '("-- none --")
		     (mapcar #'(lambda (x) (format "%c:%s  "
						   x (category-docstring x)))
			     (category-set-mnemonics category-set)))))
	      ("buffer code"
	       ,(encoded-string-description
		 (string-as-unibyte (char-to-string char)) nil))
	      ("file code"
	       ,@(let* ((coding buffer-file-coding-system)
			(encoded (encode-coding-char char coding)))
		   (if encoded
		       (list (encoded-string-description encoded coding)
			     (format "(encoded by coding system %S)" coding))
		     (list "not encodable by coding system"
			   (symbol-name coding)))))
Eli Zaretskii's avatar
Eli Zaretskii committed
573
	      ,(if (display-graphic-p (selected-frame))
Kenichi Handa's avatar
Kenichi Handa committed
574 575
		   (list "font" (or (internal-char-font (point))
				    "-- none --"))
576 577 578 579 580
		 (list "terminal code"
		       (let* ((coding (terminal-coding-system))
			      (encoded (encode-coding-char char coding)))
			 (if encoded
			     (encoded-string-description encoded coding)
Kenichi Handa's avatar
Kenichi Handa committed
581 582 583 584 585 586
			   "not encodable")))))))
    (setq max-width (apply #'max (mapcar #'(lambda (x) (length (car x)))
					 item-list)))
    (with-output-to-temp-buffer "*Help*"
      (save-excursion
	(set-buffer standard-output)
587
	(set-buffer-multibyte multibyte-p)
Kenichi Handa's avatar
Kenichi Handa committed
588 589 590 591 592 593 594 595 596 597 598
	(let ((formatter (format "%%%ds:" max-width)))
	  (dolist (elt item-list)
	    (insert (format formatter (car elt)))
	    (dolist (clm (cdr elt))
	      (when (>= (+ (current-column) (string-width clm) 1)
			(frame-width))
		(insert "\n")
		(indent-to (1+ max-width)))
	      (insert " " clm))
	    (insert "\n")))
	(when composition
599
	  (insert "\nComposed with the following character(s) "
Kenichi Handa's avatar
Kenichi Handa committed
600 601 602 603 604 605 606 607 608 609 610 611 612 613
		  (mapconcat (lambda (x) (format "`%c'" x))
			     (substring composed 1)
			     ", ")
		  " to form `" composed "'")
	  (if (nth 3 composition)
	      (insert ".\n")
	    (insert "\nby the rule ("
		    (mapconcat (lambda (x)
				 (format (if (consp x) "%S" "?%c") x))
			       (nth 2 composition)
			       " ")
		    ").\n"
		    "See the variable `reference-point-alist' for the meaning of the rule.\n")))
	))))
614

Karl Heuer's avatar
Karl Heuer committed
615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643

;;; CODING-SYSTEM

;; Print information of designation of each graphic register in FLAGS
;; in human readable format.  See the documentation of
;; `make-coding-system' for the meaning of FLAGS.
(defun print-designation (flags)
  (let ((graphic-register 0)
	charset)
    (while (< graphic-register 4)
      (setq charset (aref flags graphic-register))
      (princ (format
	      "  G%d -- %s\n"
	      graphic-register
	      (cond ((null charset)
		     "never used")
		    ((eq charset t)
		     "no initial designation, and used by any charsets")
		    ((symbolp charset)
		     (format "%s:%s"
			     charset (charset-description charset)))
		    ((listp charset)
		     (if (charsetp (car charset))
			 (format "%s:%s, and also used by the followings:"
				 (car charset)
				 (charset-description (car charset)))
		       "no initial designation, and used by the followings:"))
		    (t
		     "invalid designation information"))))
Kenichi Handa's avatar
Kenichi Handa committed
644 645 646 647 648 649 650 651 652 653
      (when (listp charset)
	(setq charset (cdr charset))
	(while charset
	  (cond ((eq (car charset) t)
		 (princ "\tany other charsets\n"))
		((charsetp (car charset))
		 (princ (format "\t%s:%s\n"
				(car charset)
				(charset-description (car charset)))))
		(t
654
		 "invalid designation information"))
Kenichi Handa's avatar
Kenichi Handa committed
655
	  (setq charset (cdr charset))))
Karl Heuer's avatar
Karl Heuer committed
656 657 658 659
      (setq graphic-register (1+ graphic-register)))))

;;;###autoload
(defun describe-coding-system (coding-system)
Karl Heuer's avatar
Karl Heuer committed
660
  "Display information about CODING-SYSTEM."
661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
  (interactive "zDescribe coding system (default, current choices): ")
  (if (null coding-system)
      (describe-current-coding-system)
    (with-output-to-temp-buffer "*Help*"
      (print-coding-system-briefly coding-system 'doc-string)
      (let ((coding-spec (coding-system-spec coding-system)))
	(princ "Type: ")
	(let ((type (coding-system-type coding-system))
	      (flags (coding-system-flags coding-system)))
	  (princ type)
	  (cond ((eq type nil)
		 (princ " (do no conversion)"))
		((eq type t)
		 (princ " (do automatic conversion)"))
		((eq type 0)
		 (princ " (Emacs internal multibyte form)"))
		((eq type 1)
		 (princ " (Shift-JIS, MS-KANJI)"))
		((eq type 2)
		 (princ " (variant of ISO-2022)\n")
		 (princ "Initial designations:\n")
		 (print-designation flags)
		 (princ "Other Form: \n  ")
		 (princ (if (aref flags 4) "short-form" "long-form"))
		 (if (aref flags 5) (princ ", ASCII@EOL"))
		 (if (aref flags 6) (princ ", ASCII@CNTL"))
		 (princ (if (aref flags 7) ", 7-bit" ", 8-bit"))
		 (if (aref flags 8) (princ ", use-locking-shift"))
		 (if (aref flags 9) (princ ", use-single-shift"))
		 (if (aref flags 10) (princ ", use-roman"))
691 692 693 694 695 696
		 (if (aref flags 11) (princ ", use-old-jis"))
		 (if (aref flags 12) (princ ", no-ISO6429"))
		 (if (aref flags 13) (princ ", init-bol"))
		 (if (aref flags 14) (princ ", designation-bol"))
		 (if (aref flags 15) (princ ", convert-unsafe"))
		 (if (aref flags 16) (princ ", accept-latin-extra-code"))
697 698 699 700 701
		 (princ "."))
		((eq type 3)
		 (princ " (Big5)"))
		((eq type 4)
		 (princ " (do conversion by CCL program)"))
702 703 704
		((eq type 5)
		 (princ " (text with random binary characters)"))
		(t (princ ": invalid coding-system."))))
705
	(princ "\nEOL type: ")
706 707 708 709 710 711 712 713 714
	(let ((eol-type (coding-system-eol-type coding-system)))
	  (cond ((vectorp eol-type)
		 (princ "Automatic selection from:\n\t")
		 (princ eol-type)
		 (princ "\n"))
		((or (null eol-type) (eq eol-type 0)) (princ "LF\n"))
		((eq eol-type 1) (princ "CRLF\n"))
		((eq eol-type 2) (princ "CR\n"))
		(t (princ "invalid\n")))))
715 716
      (let ((postread (coding-system-get coding-system 'post-read-conversion)))
	(when postread
Dave Love's avatar
Dave Love committed
717 718
	  (princ "After decoding text normally,")
	  (princ " perform post-conversion using the function: ")
719 720 721 722 723
	  (princ "\n  ")
	  (princ postread)
	  (princ "\n")))
      (let ((prewrite (coding-system-get coding-system 'pre-write-conversion)))
	(when prewrite
Dave Love's avatar
Dave Love committed
724 725
	  (princ "Before encoding text normally,")
	  (princ " perform pre-conversion using the function: ")
726 727 728
	  (princ "\n  ")
	  (princ prewrite)
	  (princ "\n")))
729 730
      (save-excursion
	(set-buffer standard-output)
731 732 733 734
	(let ((charsets (coding-system-get coding-system 'safe-charsets)))
	  (when charsets
	    (if (eq charsets t)
		(insert "This coding system can encode all charsets.\n")
Dave Love's avatar
Dave Love committed
735
	      (insert "This coding system encodes the following charsets:\n ")
736 737 738 739 740 741 742 743 744
	      (while charsets
		(insert " " (symbol-name (car charsets)))
		(search-backward (symbol-name (car charsets)))
		(help-xref-button 0 #'describe-character-set (car charsets))
		(goto-char (point-max))
		(setq charsets (cdr charsets))))))
	(help-setup-xref (list #'describe-coding-system coding-system)
			 (interactive-p))))))

Karl Heuer's avatar
Karl Heuer committed
745 746 747

;;;###autoload
(defun describe-current-coding-system-briefly ()
748
  "Display coding systems currently used in a brief format in echo area.
Karl Heuer's avatar
Karl Heuer committed
749

750
The format is \"F[..],K[..],T[..],P>[..],P<[..], default F[..],P<[..],P<[..]\",
Karl Heuer's avatar
Karl Heuer committed
751
where mnemonics of the following coding systems come in this order
752
at the place of `..':
753 754
  `buffer-file-coding-system' (of the current buffer)
  eol-type of `buffer-file-coding-system' (of the current buffer)
Karl Heuer's avatar
Karl Heuer committed
755
  Value returned by `keyboard-coding-system'
756 757 758
  eol-type of `keyboard-coding-system'
  Value returned by `terminal-coding-system'.
  eol-type of `terminal-coding-system'
Karl Heuer's avatar
Karl Heuer committed
759
  `process-coding-system' for read (of the current buffer, if any)
760
  eol-type of `process-coding-system' for read (of the current buffer, if any)
Karl Heuer's avatar
Karl Heuer committed
761
  `process-coding-system' for write (of the current buffer, if any)
762
  eol-type of `process-coding-system' for write (of the current buffer, if any)
Karl Heuer's avatar
Karl Heuer committed
763
  `default-buffer-file-coding-system'
764
  eol-type of `default-buffer-file-coding-system'
Karl Heuer's avatar
Karl Heuer committed
765
  `default-process-coding-system' for read
766
  eol-type of `default-process-coding-system' for read
Karl Heuer's avatar
Karl Heuer committed
767
  `default-process-coding-system' for write
768
  eol-type of `default-process-coding-system'"
Karl Heuer's avatar
Karl Heuer committed
769 770 771 772
  (interactive)
  (let* ((proc (get-buffer-process (current-buffer)))
	 (process-coding-systems (if proc (process-coding-system proc))))
    (message
773
     "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]"
Karl Heuer's avatar
Karl Heuer committed
774
     (coding-system-mnemonic buffer-file-coding-system)
775
     (coding-system-eol-type-mnemonic buffer-file-coding-system)
Karl Heuer's avatar
Karl Heuer committed
776
     (coding-system-mnemonic (keyboard-coding-system))
777
     (coding-system-eol-type-mnemonic (keyboard-coding-system))
Karl Heuer's avatar
Karl Heuer committed
778
     (coding-system-mnemonic (terminal-coding-system))
779
     (coding-system-eol-type-mnemonic (terminal-coding-system))
Karl Heuer's avatar
Karl Heuer committed
780
     (coding-system-mnemonic (car process-coding-systems))
781
     (coding-system-eol-type-mnemonic (car process-coding-systems))
Karl Heuer's avatar
Karl Heuer committed
782
     (coding-system-mnemonic (cdr process-coding-systems))
783 784 785
     (coding-system-eol-type-mnemonic (cdr process-coding-systems))
     (coding-system-mnemonic default-buffer-file-coding-system)
     (coding-system-eol-type-mnemonic default-buffer-file-coding-system)
Karl Heuer's avatar
Karl Heuer committed
786
     (coding-system-mnemonic (car default-process-coding-system))
787
     (coding-system-eol-type-mnemonic (car default-process-coding-system))
Karl Heuer's avatar
Karl Heuer committed
788
     (coding-system-mnemonic (cdr default-process-coding-system))
789
     (coding-system-eol-type-mnemonic (cdr default-process-coding-system))
Karl Heuer's avatar
Karl Heuer committed
790 791
     )))

Karl Heuer's avatar
Karl Heuer committed
792
;; Print symbol name and mnemonic letter of CODING-SYSTEM with `princ'.
793
(defun print-coding-system-briefly (coding-system &optional doc-string)
794 795 796 797 798
  (if (not coding-system)
      (princ "nil\n")
    (princ (format "%c -- %s"
		   (coding-system-mnemonic coding-system)
		   coding-system))
799 800 801 802 803 804
    (let ((aliases (coding-system-get coding-system 'alias-coding-systems)))
      (if (eq coding-system (car aliases))
	  (if (cdr aliases)
	      (princ (format " %S" (cons 'alias: (cdr aliases)))))
	(if (memq coding-system aliases)
	    (princ (format " (alias of %s)" (car aliases))))))
805 806 807 808
    (princ "\n")
    (if (and doc-string
	     (setq doc-string (coding-system-doc-string coding-system)))
	(princ (format "  %s\n" doc-string)))))
Karl Heuer's avatar
Karl Heuer committed
809 810 811

;;;###autoload
(defun describe-current-coding-system ()
Karl Heuer's avatar
Karl Heuer committed
812
  "Display coding systems currently used, in detail."
Karl Heuer's avatar
Karl Heuer committed
813 814 815 816
  (interactive)
  (with-output-to-temp-buffer "*Help*"
    (let* ((proc (get-buffer-process (current-buffer)))
	   (process-coding-systems (if proc (process-coding-system proc))))
817
      (princ "Coding system for saving this buffer:\n  ")
818 819
      (if (local-variable-p 'buffer-file-coding-system)
	  (print-coding-system-briefly buffer-file-coding-system)
820 821
	(princ "Not set locally, use the default.\n"))
      (princ "Default coding system (for new files):\n  ")
822
      (print-coding-system-briefly default-buffer-file-coding-system)
823
      (princ "Coding system for keyboard input:\n  ")
Karl Heuer's avatar
Karl Heuer committed
824
      (print-coding-system-briefly (keyboard-coding-system))
825
      (princ "Coding system for terminal output:\n  ")
Karl Heuer's avatar
Karl Heuer committed
826
      (print-coding-system-briefly (terminal-coding-system))
827 828 829 830 831 832
      (when (get-buffer-process (current-buffer))
	(princ "Coding systems for process I/O:\n")
	(princ "  encoding input to the process: ")
	(print-coding-system-briefly (cdr process-coding-systems))
	(princ "  decoding output from the process: ")
	(print-coding-system-briefly (car process-coding-systems)))
833
      (princ "Defaults for subprocess I/O:\n")
834
      (princ "  decoding: ")
Karl Heuer's avatar
Karl Heuer committed
835
      (print-coding-system-briefly (car default-process-coding-system))
836 837
      (princ "  encoding: ")
      (print-coding-system-briefly (cdr default-process-coding-system)))
838 839 840 841

    (save-excursion
      (set-buffer standard-output)

842
      (princ "\nPriority order for recognizing coding systems when reading files:\n")
843 844
      (let ((l coding-category-list)
	    (i 1)
845
	    (coding-list nil)
846 847 848
	    coding aliases)
	(while l
	  (setq coding (symbol-value (car l)))
849
	  ;; Do not list up the same coding system twice.
850
	  (when (and coding (not (memq coding coding-list)))
851
	    (setq coding-list (cons coding coding-list))
852 853 854 855 856 857 858
	    (princ (format "  %d. %s " i coding))
	    (setq aliases (coding-system-get coding 'alias-coding-systems))
	    (if (eq coding (car aliases))
		(if (cdr aliases)
		    (princ (cons 'alias: (cdr aliases))))
	      (if (memq coding aliases)
		  (princ (list 'alias 'of (car aliases)))))
859 860 861
	    (terpri)
	    (setq i (1+ i)))
	  (setq l (cdr l))))
862

863 864 865 866
      (princ "\n  Other coding systems cannot be distinguished automatically
  from these, and therefore cannot be recognized automatically
  with the present coding system priorities.\n\n")

867
      (let ((categories '(coding-category-iso-7 coding-category-iso-7-else))
868 869 870 871 872 873 874
	    coding-system codings)
	(while categories
	  (setq coding-system (symbol-value (car categories)))
	  (mapcar
	   (function
	    (lambda (x)
	      (if (and (not (eq x coding-system))
875
		       (coding-system-get x 'no-initial-designation)
876 877 878 879 880 881 882 883 884 885 886
		       (let ((flags (coding-system-flags x)))
			 (not (or (aref flags 10) (aref flags 11)))))
		  (setq codings (cons x codings)))))
	   (get (car categories) 'coding-systems))
	  (if codings
	      (let ((max-col (frame-width))
		    pos)
		(princ (format "  The followings are decoded correctly but recognized as %s:\n   " coding-system))
		(while codings
		  (setq pos (point))
		  (insert (format " %s" (car codings)))
Kenichi Handa's avatar
Kenichi Handa committed
887 888 889 890
		  (when (> (current-column) max-col)
		    (goto-char pos)
		    (insert "\n   ")
		    (goto-char (point-max)))
891 892 893 894
		  (setq codings (cdr codings)))
		(insert "\n\n")))
	  (setq categories (cdr categories))))

895
      (princ "Particular coding systems specified for certain file names:\n")
896 897 898 899 900 901 902 903 904 905 906
      (terpri)
      (princ "  OPERATION\tTARGET PATTERN\t\tCODING SYSTEM(s)\n")
      (princ "  ---------\t--------------\t\t----------------\n")
      (let ((func (lambda (operation alist)
		    (princ "  ")
		    (princ operation)
		    (if (not alist)
			(princ "\tnothing specified\n")
		      (while alist
			(indent-to 16)
			(prin1 (car (car alist)))
907 908
			(if (>= (current-column) 40)
			    (newline))
909 910 911 912 913 914 915 916
			(indent-to 40)
			(princ (cdr (car alist)))
			(princ "\n")
			(setq alist (cdr alist)))))))
	(funcall func "File I/O" file-coding-system-alist)
	(funcall func "Process I/O" process-coding-system-alist)
	(funcall func "Network I/O" network-coding-system-alist))
      (help-mode))))
Karl Heuer's avatar
Karl Heuer committed
917 918

;; Print detailed information on CODING-SYSTEM.
919
(defun print-coding-system (coding-system)
Karl Heuer's avatar
Karl Heuer committed
920
  (let ((type (coding-system-type coding-system))
921 922
	(eol-type (coding-system-eol-type coding-system))
	(flags (coding-system-flags coding-system))
923 924 925
	(aliases (coding-system-get coding-system 'alias-coding-systems)))
    (if (not (eq (car aliases) coding-system))
	(princ (format "%s (alias of %s)\n" coding-system (car aliases)))
926
      (princ coding-system)
927
      (setq aliases (cdr aliases))
928
      (while aliases
Kenichi Handa's avatar
Kenichi Handa committed
929 930 931
	(princ ",")
	(princ (car aliases))
	(setq aliases (cdr aliases)))
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948
      (princ (format ":%s:%c:%d:"
		     type
		     (coding-system-mnemonic coding-system)
		     (if (integerp eol-type) eol-type 3)))
      (cond ((eq type 2)		; ISO-2022
	     (let ((idx 0)
		   charset)
	       (while (< idx 4)
		 (setq charset (aref flags idx))
		 (cond ((null charset)
			(princ -1))
		       ((eq charset t)
			(princ -2))
		       ((charsetp charset)
			(princ charset))
		       ((listp charset)
			(princ "(")
Karl Heuer's avatar
Karl Heuer committed
949
			(princ (car charset))
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964
			(setq charset (cdr charset))
			(while charset
			  (princ ",")
			  (princ (car charset))
			  (setq charset (cdr charset)))
			(princ ")")))
		 (princ ",")
		 (setq idx (1+ idx)))
	       (while (< idx 12)
		 (princ (if (aref flags idx) 1 0))
		 (princ ",")
		 (setq idx (1+ idx)))
	       (princ (if (aref flags idx) 1 0))))
	    ((eq type 4)		; CCL
	     (let (i len)
Richard M. Stallman's avatar
Richard M. Stallman committed
965 966 967 968 969 970
	       (if (symbolp (car flags))
		   (princ (format " %s" (car flags)))
		 (setq i 0 len (length (car flags)))
		 (while (< i len)
		   (princ (format " %x" (aref (car flags) i)))
		   (setq i (1+ i))))
Karl Heuer's avatar
Karl Heuer committed
971
	       (princ ",")
Richard M. Stallman's avatar
Richard M. Stallman committed
972 973 974 975 976 977
	       (if (symbolp (cdr flags))
		   (princ (format "%s" (cdr flags)))
		 (setq i 0 len (length (cdr flags)))
		 (while (< i len)
		   (princ (format " %x" (aref (cdr flags) i)))
		   (setq i (1+ i))))))
978 979 980 981
	    (t (princ 0)))
      (princ ":")
      (princ (coding-system-doc-string coding-system))
      (princ "\n"))))
Karl Heuer's avatar
Karl Heuer committed
982

983
;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
984 985
(defun list-coding-systems (&optional arg)
  "Display a list of all coding systems.
Karl Heuer's avatar
Karl Heuer committed
986
This shows the mnemonic letter, name, and description of each coding system.
Kenichi Handa's avatar
Kenichi Handa committed
987 988

With prefix arg, the output format gets more cryptic,
Karl Heuer's avatar
Karl Heuer committed
989
but still contains full information about each coding system."
Kenichi Handa's avatar
Kenichi Handa committed
990
  (interactive "P")
Karl Heuer's avatar
Karl Heuer committed
991
  (with-output-to-temp-buffer "*Help*"
992 993 994 995 996
    (list-coding-systems-1 arg)))

(defun list-coding-systems-1 (arg)
  (if (null arg)
      (princ "\
997 998 999 1000 1001
###############################################
# List of coding systems in the following format:
# MNEMONIC-LETTER -- CODING-SYSTEM-NAME
#	DOC-STRING
")
1002
    (princ "\
Karl Heuer's avatar
Karl Heuer committed
1003 1004 1005 1006
#########################
## LIST OF CODING SYSTEMS
## Each line corresponds to one coding system
## Format of a line is:
1007 1008
##   NAME[,ALIAS...]:TYPE:MNEMONIC:EOL:FLAGS:POST-READ-CONVERSION
##	:PRE-WRITE-CONVERSION:DOC-STRING,
Karl Heuer's avatar
Karl Heuer committed
1009
## where
1010 1011 1012 1013
##  NAME = coding system name
##  ALIAS = alias of the coding system
##  TYPE = nil (no conversion), t (undecided or automatic detection),
##         0 (EMACS-MULE), 1 (SJIS), 2 (ISO2022), 3 (BIG5), or 4 (CCL)
Karl Heuer's avatar
Karl Heuer committed
1014 1015 1016 1017 1018 1019 1020 1021 1022 1023
##  EOL = 0 (LF), 1 (CRLF), 2 (CR), or 3 (Automatic detection)
##  FLAGS =
##    if TYPE = 2 then
##      comma (`,') separated data of the followings:
##        G0, G1, G2, G3, SHORT-FORM, ASCII-EOL, ASCII-CNTL, SEVEN,
##        LOCKING-SHIFT, SINGLE-SHIFT, USE-ROMAN, USE-OLDJIS, NO-ISO6429
##    else if TYPE = 4 then
##      comma (`,') separated CCL programs for read and write
##    else
##      0
1024
##  POST-READ-CONVERSION, PRE-WRITE-CONVERSION = function name to be called
Karl Heuer's avatar
Karl Heuer committed
1025
##
1026
"))
1027 1028 1029 1030 1031 1032 1033 1034
  (let ((bases (coding-system-list 'base-only))
	coding-system)
    (while bases
      (setq coding-system (car bases))
      (if (null arg)
	  (print-coding-system-briefly coding-system 'doc-string)
	(print-coding-system coding-system))
      (setq bases (cdr bases)))))
Kenichi Handa's avatar
Kenichi Handa committed
1035

1036
;;;###autoload
Kenichi Handa's avatar
Kenichi Handa committed
1037 1038 1039 1040
(defun list-coding-categories ()
  "Display a list of all coding categories."
  (with-output-to-temp-buffer "*Help*"
    (princ "\
Karl Heuer's avatar
Karl Heuer committed
1041 1042 1043 1044 1045
############################
## LIST OF CODING CATEGORIES (ordered by priority)
## CATEGORY:CODING-SYSTEM
##
")
Kenichi Handa's avatar
Kenichi Handa committed
1046 1047 1048 1049
    (let ((l coding-category-list))
      (while l
	(princ (format "%s:%s\n" (car l) (symbol-value (car l))))
	(setq l (cdr l))))))
Karl Heuer's avatar
Karl Heuer committed
1050 1051 1052 1053 1054 1055 1056

;;; FONT

;; Print information of a font in FONTINFO.
(defun describe-font-internal (font-info &optional verbose)
  (print-list "name (opened by):" (aref font-info 0))
  (print-list "       full name:" (aref font-info 1))
1057 1058 1059 1060
  (print-list "            size:" (format "%2d" (aref font-info 2)))
  (print-list "          height:" (format "%2d" (aref font-info 3)))
  (print-list " baseline-offset:" (format "%2d" (aref font-info 4)))
  (print-list "relative-compose:" (format "%2d" (aref font-info 5))))
Karl Heuer's avatar
Karl Heuer committed
1061 1062 1063 1064

;;;###autoload
(defun describe-font (fontname)
  "Display information about fonts which partially match FONTNAME."
Karl Heuer's avatar
Karl Heuer committed
1065
  (interactive "sFontname (default, current choice for ASCII chars): ")
1066
  (or (and window-system (fboundp 'fontset-list))
1067
      (error "No fontsets being used"))
1068 1069 1070 1071
  (when (or (not fontname) (= (length fontname) 0))
    (setq fontname (cdr (assq 'font (frame-parameters))))
    (if (query-fontset fontname)
	(setq fontname
1072
	      (nth 1 (assq 'ascii (aref (fontset-info fontname) 2))))))
Karl Heuer's avatar
Karl Heuer committed
1073 1074 1075 1076 1077 1078
  (let ((font-info (font-info fontname)))
    (if (null font-info)
	(message "No matching font")
      (with-output-to-temp-buffer "*Help*"
	(describe-font-internal font-info 'verbose)))))

Kenichi Handa's avatar
Kenichi Handa committed
1079
;; Print information of FONTSET.  If optional arg PRINT-FONTS is
1080 1081
;; non-nil, print also names of all opened fonts for FONTSET.  This
;; function actually INSERT such information in the current buffer.
Kenichi Handa's avatar
Kenichi Handa committed
1082
(defun print-fontset (fontset &optional print-fonts)
1083
  (let ((tail (aref (fontset-info fontset) 2))
1084
	elt chars font-spec opened prev-charset charset from to)
Kenichi Handa's avatar
Kenichi Handa committed
1085
    (beginning-of-line)
1086 1087
    (insert "Fontset: " fontset "\n")
    (insert "CHARSET or CHAR RANGE")
1088
    (indent-to 24)
1089 1090
    (insert "FONT NAME\n")
    (insert "---------------------")
1091
    (indent-to 24)
1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
    (insert "---------")
    (insert "\n")
    (while tail
      (setq elt (car tail) tail (cdr tail))
      (setq chars (car elt) font-spec (car (cdr elt)) opened (cdr (cdr elt)))
      (if (symbolp chars)
	  (setq charset chars from nil to nil)
	(if (integerp chars)
	    (setq charset (char-charset chars) from chars to chars)
	  (setq charset (char-charset (car chars))
		from (car chars) to (cdr chars))))
      (unless (eq charset prev-charset)
	(insert (symbol-name charset))
	(if from
	    (insert "\n")))
      (when from
	(let ((split (split-char from)))
	  (if (and (= (charset-dimension charset) 2)
		   (= (nth 2 split) 0))
	      (setq from
		    (make-char charset (nth 1 split)
			       (if (= (charset-chars charset) 94) 33 32))))
	  (insert "  " from))
	(when (/= from to)
	  (insert "-")
	  (let ((split (split-char to)))
	    (if (and (= (charset-dimension charset) 2)
		     (= (nth 2 split) 0))
		(setq to
		      (make-char charset (nth 1 split)
				 (if (= (charset-chars charset) 94) 126 127))))
	    (insert to))))
1124
      (indent-to 24)
1125 1126 1127 1128
      (if (stringp font-spec)
	  (insert font-spec)
	(if (car font-spec)
	    (if (string-match "-" (car font-spec))
1129 1130
		(insert "-" (car font-spec) "-*-")
	      (insert "-*-" (car font-spec) "-*-"))
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144
	  (insert "-*-"))
	(if (cdr font-spec)
	    (if (string-match "-" (cdr font-spec))
		(insert (cdr font-spec))
	      (insert (cdr font-spec) "-*"))
	  (insert "*")))
      (insert "\n")
      (when print-fonts
	(while opened
	  (indent-to 5)
	  (insert "[" (car opened) "]\n")
	  (setq opened (cdr opened))))
      (setq prev-charset charset)
      )))
Karl Heuer's avatar
Karl Heuer committed
1145 1146 1147

;;;###autoload
(defun describe-fontset (fontset)
Kenichi Handa's avatar
Kenichi Handa committed
1148
  "Display information of FONTSET.
1149
This shows which font is used for which character(s)."
Karl Heuer's avatar
Karl Heuer committed
1150
  (interactive
1151
   (if (not (and window-system (fboundp 'fontset-list)))
1152
       (error "No fontsets being used")
Dave Love's avatar
Dave Love committed
1153
     (let ((fontset-list (nconc
1154 1155
			  (mapcar 'list (fontset-list))
			  (mapcar (lambda (x) (list (cdr x)))
1156
				  fontset-alias-alist)))
Kenichi Handa's avatar
Kenichi Handa committed
1157 1158 1159 1160 1161 1162
	   (completion-ignore-case t))
       (list (completing-read
	      "Fontset (default, used by the current frame): "
	      fontset-list nil t)))))
  (if (= (length fontset) 0)
      (setq fontset (cdr (assq 'font (frame-parameters)))))
1163
  (if (not (setq fontset (query-fontset fontset)))
Kenichi Handa's avatar
Kenichi Handa committed
1164
      (error "Current frame is using font, not fontset"))
1165 1166 1167 1168
  (with-output-to-temp-buffer "*Help*"
    (save-excursion
      (set-buffer standard-output)
      (print-fontset fontset t))))
Kenichi Handa's avatar
Kenichi Handa committed
1169 1170 1171 1172

;;;###autoload
(defun list-fontsets (arg)
  "Display a list of all fontsets.
Karl Heuer's avatar
Karl Heuer committed
1173 1174 1175
This shows the name, size, and style of each fontset.
With prefix arg, it also list the fonts contained in each fontset;
see the function `describe-fontset' for the format of the list."
Kenichi Handa's avatar
Kenichi Handa committed
1176
  (interactive "P")
1177
  (if (not (and window-system (fboundp 'fontset-list)))
1178 1179 1180
      (error "No fontsets being used")
    (with-output-to-temp-buffer "*Help*"
      (save-excursion
1181
	;; This code is duplicated near the end of mule-diag.