Doc fixes.
[bpt/emacs.git] / lisp / international / mule-cmds.el
CommitLineData
60370d40 1;;; mule-cmds.el --- commands for mulitilingual environment
4ed46869 2
4ed46869 3;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
fa526c4a 4;; Licensed to the Free Software Foundation.
7008ccac 5;; Copyright (C) 2000, 2001 Free Software Foundation, Inc.
4ed46869
KH
6
7;; Keywords: mule, multilingual
8
9;; This file is part of GNU Emacs.
10
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
15
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
20
21;; You should have received a copy of the GNU General Public License
369314dc
KH
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
4ed46869 25
60370d40
PJ
26;;; Commentary:
27
4ed46869
KH
28;;; Code:
29
cda74479
DL
30(eval-when-compile (defvar dos-codepage))
31
4ed46869
KH
32;;; MULE related key bindings and menus.
33
0709d285 34(defvar mule-keymap (make-sparse-keymap)
33d17698 35 "Keymap for Mule (Multilingual environment) specific commands.")
4ed46869 36
8f81f784 37;; Keep "C-x C-m ..." for mule specific commands.
0709d285 38(define-key ctl-x-map "\C-m" mule-keymap)
ef8a8c8c 39
4ed46869
KH
40(define-key mule-keymap "f" 'set-buffer-file-coding-system)
41(define-key mule-keymap "t" 'set-terminal-coding-system)
15b3e511
KH
42(define-key mule-keymap "k" 'set-keyboard-coding-system)
43(define-key mule-keymap "p" 'set-buffer-process-coding-system)
7624ebb9
KH
44(define-key mule-keymap "x" 'set-selection-coding-system)
45(define-key mule-keymap "X" 'set-next-selection-coding-system)
8b784951 46(define-key mule-keymap "\C-\\" 'set-input-method)
15b3e511 47(define-key mule-keymap "c" 'universal-coding-system-argument)
b4fba33f 48(define-key mule-keymap "l" 'set-language-environment)
4ed46869 49
281d03ec 50(define-key help-map "\C-L" 'describe-language-environment)
ac4a3a2d 51(define-key help-map "L" 'describe-language-environment)
4ed46869 52(define-key help-map "\C-\\" 'describe-input-method)
ac4a3a2d 53(define-key help-map "I" 'describe-input-method)
d0b9c3ab 54(define-key help-map "C" 'describe-coding-system)
4ed46869
KH
55(define-key help-map "h" 'view-hello-file)
56
538d88fb
EZ
57(defvar mule-menu-keymap
58 (make-sparse-keymap "Mule (Multilingual Environment)")
33d17698 59 "Keymap for Mule (Multilingual environment) menu specific commands.")
15b3e511 60
dcad02bc
EZ
61(defvar describe-language-environment-map
62 (make-sparse-keymap "Describe Language Environment"))
15b3e511 63
dcad02bc
EZ
64(defvar setup-language-environment-map
65 (make-sparse-keymap "Set Language Environment"))
15b3e511 66
dcad02bc
EZ
67(defvar set-coding-system-map
68 (make-sparse-keymap "Set Coding System"))
15b3e511 69
15b3e511 70(define-key-after mule-menu-keymap [set-language-environment]
cda74479
DL
71 (list 'menu-item "Set Language Environment" setup-language-environment-map
72 :help "Multilingual environment suitable for a specific language"))
a61f401d 73(define-key-after mule-menu-keymap [mouse-set-font]
538d88fb 74 '(menu-item "Set Font/Fontset" mouse-set-font
cda74479
DL
75 :visible (fboundp 'generate-fontset-menu)
76 :help "Select a font from list of known fonts/fontsets"))
15b3e511
KH
77(define-key-after mule-menu-keymap [separator-mule]
78 '("--")
79 t)
80(define-key-after mule-menu-keymap [toggle-input-method]
538d88fb 81 '(menu-item "Toggle Input Method" toggle-input-method)
15b3e511 82 t)
8b784951 83(define-key-after mule-menu-keymap [set-input-method]
538d88fb 84 '(menu-item "Select Input Method..." set-input-method)
15b3e511 85 t)
cda74479
DL
86(define-key-after mule-menu-keymap [describe-input-method]
87 '(menu-item "Describe Input Method" describe-input-method))
15b3e511
KH
88(define-key-after mule-menu-keymap [separator-input-method]
89 '("--")
90 t)
15b3e511 91(define-key-after mule-menu-keymap [set-various-coding-system]
cda74479
DL
92 (list 'menu-item "Set Coding Systems" set-coding-system-map
93 :enable 'enable-multibyte-characters))
538d88fb
EZ
94(define-key-after mule-menu-keymap [view-hello-file]
95 '(menu-item "Show Multi-lingual Text" view-hello-file
96 :enable (file-readable-p
97 (expand-file-name "HELLO" data-directory))
98 :help "Display file which says HELLO in many languages")
15b3e511
KH
99 t)
100(define-key-after mule-menu-keymap [separator-coding-system]
101 '("--")
102 t)
538d88fb
EZ
103(define-key-after mule-menu-keymap [describe-language-environment]
104 (list 'menu-item "Describe Language Environment"
105 describe-language-environment-map
cda74479 106 :help "Show multilingual settings for a specific language")
15b3e511 107 t)
538d88fb
EZ
108(define-key-after mule-menu-keymap [describe-input-method]
109 '(menu-item "Describe Input Method..." describe-input-method
cda74479 110 :help "Keyboard layout for a specific input method")
538d88fb
EZ
111 t)
112(define-key-after mule-menu-keymap [describe-coding-system]
113 '(menu-item "Describe Coding System..." describe-coding-system)
114 t)
cda74479
DL
115(define-key-after mule-menu-keymap [list-character-sets]
116 '(menu-item "List Character Sets" list-character-sets
117 :help "Show table of available character sets"))
538d88fb
EZ
118(define-key-after mule-menu-keymap [mule-diag]
119 '(menu-item "Show All of Mule Status" mule-diag
120 :help "Display multilingual environment settings")
15b3e511
KH
121 t)
122
123(define-key-after set-coding-system-map [set-buffer-file-coding-system]
538d88fb
EZ
124 '(menu-item "For Saving this Buffer" set-buffer-file-coding-system
125 :help "How to encode this buffer on disk")
15b3e511 126 t)
3a151e98 127(define-key-after set-coding-system-map [universal-coding-system-argument]
538d88fb
EZ
128 '(menu-item "For Next Command" universal-coding-system-argument
129 :help "Coding system to be used by next command")
3a151e98 130 t)
15b3e511 131(define-key-after set-coding-system-map [set-terminal-coding-system]
538d88fb
EZ
132 '(menu-item "For Terminal" set-terminal-coding-system
133 :enable (null (memq window-system '(x w32 mac)))
134 :help "How to encode terminal output")
15b3e511
KH
135 t)
136(define-key-after set-coding-system-map [set-keyboard-coding-system]
538d88fb
EZ
137 '(menu-item "For Keyboard" set-keyboard-coding-system
138 :help "How to decode keyboard input")
15b3e511
KH
139 t)
140(define-key-after set-coding-system-map [set-buffer-process-coding-system]
538d88fb
EZ
141 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
142 :visible (fboundp 'start-process)
143 :enable (get-buffer-process (current-buffer))
144 :help "How to en/decode I/O from/to subprocess connected to this buffer")
15b3e511 145 t)
7624ebb9 146(define-key-after set-coding-system-map [set-selection-coding-system]
538d88fb
EZ
147 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
148 :visible (display-selections-p)
149 :help "How to en/decode data to/from selection/clipboard")
7624ebb9
KH
150 t)
151(define-key-after set-coding-system-map [set-next-selection-coding-system]
538d88fb
EZ
152 '(menu-item "For Next X Selection" set-next-selection-coding-system
153 :visible (display-selections-p)
154 :help "How to en/decode next selection/clipboard operation")
7624ebb9 155 t)
15b3e511 156(define-key setup-language-environment-map
538d88fb 157 [Default] '(menu-item "Default" setup-specified-language-environment))
4ed46869 158
cda74479
DL
159(define-key describe-language-environment-map
160 [Default] '(menu-item "Default" describe-specified-language-support))
161
4ed46869
KH
162;; This should be a single character key binding because users use it
163;; very frequently while editing multilingual text. Now we can use
164;; only two such keys: "\C-\\" and "\C-^", but the latter is not
165;; convenient because it requires shifting on most keyboards. An
166;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
167;; but it won't be used that frequently.
168(define-key global-map "\C-\\" 'toggle-input-method)
169
5f395df3
SM
170;; This is no good because people often type Shift-SPC
171;; meaning to type SPC. -- rms.
172;; ;; Here's an alternative key binding for X users (Shift-SPACE).
173;; (define-key global-map [?\S- ] 'toggle-input-method)
b4fba33f 174
464cc130
KH
175;;; Mule related hyperlinks.
176(defconst help-xref-mule-regexp-template
177 (purecopy (concat "\\(\\<\\("
178 "\\(coding system\\)\\|"
d0c40faa
KH
179 "\\(input method\\)\\|"
180 "\\(character set\\)\\|"
181 "\\(charset\\)"
464cc130
KH
182 "\\)\\s-+\\)?"
183 ;; Note starting with word-syntax character:
184 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
185
26d87040
EZ
186(defun coding-system-change-eol-conversion (coding-system eol-type)
187 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
188The returned coding system converts end-of-line by EOL-TYPE
189but text as the same way as CODING-SYSTEM.
190EOL-TYPE should be `unix', `dos', `mac', or nil.
191If EOL-TYPE is nil, the returned coding system detects
192how end-of-line is formatted automatically while decoding.
193
194EOL-TYPE can be specified by an integer 0, 1, or 2.
195They means `unix', `dos', and `mac' respectively."
196 (if (symbolp eol-type)
197 (setq eol-type (cond ((eq eol-type 'unix) 0)
198 ((eq eol-type 'dos) 1)
199 ((eq eol-type 'mac) 2)
200 (t eol-type))))
201 (let ((orig-eol-type (coding-system-eol-type coding-system)))
202 (if (vectorp orig-eol-type)
203 (if (not eol-type)
204 coding-system
205 (aref orig-eol-type eol-type))
206 (let ((base (coding-system-base coding-system)))
207 (if (not eol-type)
208 base
209 (if (= eol-type orig-eol-type)
210 coding-system
211 (setq orig-eol-type (coding-system-eol-type base))
212 (if (vectorp orig-eol-type)
213 (aref orig-eol-type eol-type))))))))
214
215(defun coding-system-change-text-conversion (coding-system coding)
216 "Return a coding system which differs from CODING-SYSTEM in text conversion.
217The returned coding system converts text by CODING
218but end-of-line as the same way as CODING-SYSTEM.
219If CODING is nil, the returned coding system detects
220how text is formatted automatically while decoding."
221 (if (not coding)
222 (coding-system-base coding-system)
223 (let ((eol-type (coding-system-eol-type coding-system)))
224 (coding-system-change-eol-conversion
225 coding
226 (if (numberp eol-type) (aref [unix dos mac] eol-type))))))
227
4ed46869 228(defun toggle-enable-multibyte-characters (&optional arg)
6998e1a1
RS
229 "Change whether this buffer uses multibyte characters.
230With arg, use multibyte characters if the arg is positive.
231
232Note that this command does not convert the byte contents of
233the buffer; it only changes the way those bytes are interpreted.
234In general, therefore, this command *changes* the sequence of
235characters that the current buffer contains.
236
237We suggest you avoid using use this command unless you know what you
238are doing. If you use it by mistake, and the buffer is now displayed
239wrong, use this command again to toggle back to the right mode."
4ed46869 240 (interactive "P")
b7079457
RS
241 (let ((new-flag
242 (if (null arg) (null enable-multibyte-characters)
243 (> (prefix-numeric-value arg) 0))))
244 (set-buffer-multibyte new-flag))
4ed46869
KH
245 (force-mode-line-update))
246
247(defun view-hello-file ()
248 "Display the HELLO file which list up many languages and characters."
249 (interactive)
8f81f784
KH
250 ;; We have to decode the file in any environment.
251 (let ((default-enable-multibyte-characters t)
95fa03b4 252 (coding-system-for-read 'iso-2022-7bit))
8f81f784 253 (find-file-read-only (expand-file-name "HELLO" data-directory))))
4ed46869 254
15b3e511
KH
255(defun universal-coding-system-argument ()
256 "Execute an I/O command using the specified coding system."
257 (interactive)
34104362
KH
258 (let* ((default (and buffer-file-coding-system
259 (not (eq (coding-system-type buffer-file-coding-system)
260 t))
261 buffer-file-coding-system))
262 (coding-system (read-coding-system
263 (if default
264 (format "Coding system for following command (default, %s): " default)
265 "Coding system for following command: ")
266 default))
15b3e511 267 (keyseq (read-key-sequence
e14a8f4c 268 (format "Command to execute with %s:" coding-system)))
04363179
GM
269 (cmd (key-binding keyseq))
270 prefix)
271
272 (when (eq cmd 'universal-argument)
273 (call-interactively cmd)
274
275 ;; Process keys bound in `universal-argument-map'.
276 (while (progn
277 (setq keyseq (read-key-sequence nil t)
278 cmd (key-binding keyseq t))
279 (not (eq cmd 'universal-argument-other-key)))
280 (let ((current-prefix-arg prefix-arg)
281 ;; Have to bind `last-command-char' here so that
282 ;; `digit-argument', for isntance, can compute the
283 ;; prefix arg.
284 (last-command-char (aref keyseq 0)))
285 (call-interactively cmd)))
286
287 ;; This is the final call to `univeral-argument-other-key', which
288 ;; set's the final `prefix-arg.
289 (let ((current-prefix-arg prefix-arg))
290 (call-interactively cmd))
291
292 ;; Read the command to execute with the given prefix arg.
293 (setq prefix prefix-arg
294 keyseq (read-key-sequence nil t)
295 cmd (key-binding keyseq)))
296
15b3e511 297 (let ((coding-system-for-read coding-system)
04363179
GM
298 (coding-system-for-write coding-system)
299 (current-prefix-arg prefix))
15b3e511
KH
300 (message "")
301 (call-interactively cmd))))
302
de94d711 303(defun set-default-coding-systems (coding-system)
0c3154d2 304 "Set default value of various coding systems to CODING-SYSTEM.
387136f6 305This sets the following coding systems:
0c3154d2 306 o coding system of a newly created buffer
8efc03e1
KH
307 o default coding system for subprocess I/O
308This also sets the following values:
387136f6 309 o default value used as file-name-coding-system for converting file names.
03c35c83
EZ
310 o default value for the command `set-terminal-coding-system' (not on MSDOS)
311 o default value for the command `set-keyboard-coding-system'."
de94d711
KH
312 (check-coding-system coding-system)
313 (setq-default buffer-file-coding-system coding-system)
716184d4
RS
314 (if default-enable-multibyte-characters
315 (setq default-file-name-coding-system coding-system))
03c35c83
EZ
316 ;; If coding-system is nil, honor that on MS-DOS as well, so
317 ;; that they could reset the terminal coding system.
318 (unless (and (eq window-system 'pc) coding-system)
319 (setq default-terminal-coding-system coding-system))
de94d711
KH
320 (setq default-keyboard-coding-system coding-system)
321 (setq default-process-coding-system (cons coding-system coding-system)))
322
45d08cb2 323(defalias 'update-iso-coding-systems 'update-coding-systems-internal)
2598a293 324(make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal "20.3")
45d08cb2 325
0c3154d2
KH
326(defun prefer-coding-system (coding-system)
327 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
387136f6 328This also sets the following coding systems:
0c3154d2 329 o coding system of a newly created buffer
8efc03e1
KH
330 o default coding system for subprocess I/O
331This also sets the following values:
c654de1d 332 o default value used as `file-name-coding-system' for converting file names.
03c35c83
EZ
333 o default value for the command `set-terminal-coding-system' (not on MSDOS)
334 o default value for the command `set-keyboard-coding-system'
335
bd3ac67e
EZ
336If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
337systems set by this function will use that type of EOL conversion.
338
03c35c83
EZ
339This command does not change the default value of terminal coding system
340for MS-DOS terminal, because DOS terminals only support a single coding
341system, and Emacs automatically sets the default to that coding system at
5beaa0d1
KH
342startup.
343
c654de1d
DL
344A coding system that requires automatic detection of text
345encoding (e.g. undecided, unix) can't be preferred.
346
347See also `coding-category-list' and `coding-system-category'."
0c3154d2
KH
348 (interactive "zPrefer coding system: ")
349 (if (not (and coding-system (coding-system-p coding-system)))
350 (error "Invalid coding system `%s'" coding-system))
351 (let ((coding-category (coding-system-category coding-system))
bd3ac67e
EZ
352 (base (coding-system-base coding-system))
353 (eol-type (coding-system-eol-type coding-system)))
0c3154d2
KH
354 (if (not coding-category)
355 ;; CODING-SYSTEM is no-conversion or undecided.
356 (error "Can't prefer the coding system `%s'" coding-system))
8efc03e1 357 (set coding-category (or base coding-system))
45d08cb2 358 (update-coding-systems-internal)
812cad80 359 (or (eq coding-category (car coding-category-list))
0c3154d2 360 ;; We must change the order.
812cad80 361 (set-coding-priority (list coding-category)))
8efc03e1
KH
362 (if (and base (interactive-p))
363 (message "Highest priority is set to %s (base of %s)"
364 base coding-system))
bd3ac67e 365 ;; If they asked for specific EOL conversion, honor that.
6f9dc4fd 366 (if (memq eol-type '(0 1 2))
bd3ac67e
EZ
367 (setq coding-system
368 (coding-system-change-eol-conversion base eol-type))
369 (setq coding-system base))
370 (set-default-coding-systems coding-system)))
0c3154d2 371
b5edd1d1
KH
372(defvar sort-coding-systems-predicate nil
373 "If non-nil, a predicate function to sort coding systems.
374
375It is called with two coding systems, and should return t if the first
376one is \"less\" than the second.
377
378The function `sort-coding-systems' use it.")
379
380(defun sort-coding-systems (codings)
381 "Sort coding system list CODINGS by a priority of each coding system.
382
383If a coding system is most preferred, it has the highest priority.
384Otherwise, a coding system corresponds to some MIME charset has higher
385priorities. Among them, a coding system included in `coding-system'
386key of the current language environment has higher priorities. See
387also the documentation of `language-info-alist'.
388
389If the variable `sort-coding-systems-predicate' (which see) is
390non-nil, it is used to sort CODINGS in the different way than above."
391 (if sort-coding-systems-predicate
392 (sort codings sort-coding-systems-predicate)
393 (let* ((most-preferred (symbol-value (car coding-category-list)))
394 (lang-preferred (get-language-info current-language-environment
395 'coding-system))
396 (func (function
397 (lambda (x)
398 (let ((base (coding-system-base x)))
399 (+ (if (eq base most-preferred) 64 0)
400 (let ((mime (coding-system-get base 'mime-charset)))
401 (if mime
402 (if (string-match "^x-" (symbol-name mime))
403 16 32)
404 0))
405 (if (memq base lang-preferred) 8 0)
406 (if (string-match "-with-esc$" (symbol-name base))
407 0 4)
408 (if (eq (coding-system-type base) 2)
409 ;; For ISO based coding systems, prefer
410 ;; one that doesn't use escape sequences.
411 (let ((flags (coding-system-flags base)))
412 (if (or (consp (aref flags 0))
413 (consp (aref flags 1))
414 (consp (aref flags 2))
415 (consp (aref flags 3)))
416 (if (or (aref flags 8) (aref flags 9))
417 0
418 1)
419 2))
420 1)))))))
421 (sort codings (function (lambda (x y)
422 (> (funcall func x) (funcall func y))))))))
54b226f7 423
3fc7dfe5 424(defun find-coding-systems-region (from to)
54b226f7
KH
425 "Return a list of proper coding systems to encode a text between FROM and TO.
426All coding systems in the list can safely encode any multibyte characters
427in the text.
428
e8dd0160 429If the text contains no multibyte characters, return a list of a single
3fc7dfe5 430element `undecided'."
b5edd1d1
KH
431 (let ((codings (find-coding-systems-region-internal from to)))
432 (if (eq codings t)
433 ;; The text contains only ASCII characters. Any coding
434 ;; systems are safe.
435 '(undecided)
436 ;; We need copy-sequence because sorting will alter the argument.
437 (sort-coding-systems (copy-sequence codings)))))
54b226f7 438
3fc7dfe5
KH
439(defun find-coding-systems-string (string)
440 "Return a list of proper coding systems to encode STRING.
441All coding systems in the list can safely encode any multibyte characters
442in STRING.
443
e8dd0160 444If STRING contains no multibyte characters, return a list of a single
3fc7dfe5 445element `undecided'."
b5edd1d1 446 (find-coding-systems-region string nil))
3fc7dfe5
KH
447
448(defun find-coding-systems-for-charsets (charsets)
449 "Return a list of proper coding systems to encode characters of CHARSETS.
450CHARSETS is a list of character sets."
b5edd1d1
KH
451 (cond ((or (null charsets)
452 (and (= (length charsets) 1)
453 (eq 'ascii (car charsets))))
454 '(undecided))
455 ((or (memq 'eight-bit-control charsets)
456 (memq 'eight-bit-graphic charsets))
457 '(raw-text emacs-mule))
458 (t
459 (let ((codings t)
460 charset l ll)
461 (while (and codings charsets)
462 (setq charset (car charsets) charsets (cdr charsets))
463 (unless (eq charset 'ascii)
464 (setq l (aref char-coding-system-table (make-char charset)))
465 (if (eq codings t)
466 (setq codings l)
467 (let ((ll nil))
468 (while codings
469 (if (memq (car codings) l)
470 (setq ll (cons (car codings) ll)))
471 (setq codings (cdr codings)))
472 (setq codings ll)))))
473 (append codings
474 (char-table-extra-slot char-coding-system-table 0))))))
54b226f7 475
51ed58ea
KH
476(defun find-multibyte-characters (from to &optional maxcount excludes)
477 "Find multibyte characters in the region specified by FROM and TO.
478If FROM is a string, find multibyte characters in the string.
479The return value is an alist of the following format:
480 ((CHARSET COUNT CHAR ...) ...)
481where
482 CHARSET is a character set,
483 COUNT is a number of characters,
484 CHARs are found characters of the character set.
485Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
251d4f4b
KH
486Optional 4th arg EXCLUDE is a list of character sets to be ignored.
487
488For invalid characters, CHARs are actually strings."
51ed58ea
KH
489 (let ((chars nil)
490 charset char)
491 (if (stringp from)
492 (let ((idx 0))
493 (while (setq idx (string-match "[^\000-\177]" from idx))
494 (setq char (aref from idx)
495 charset (char-charset char))
251d4f4b
KH
496 (if (eq charset 'unknown)
497 (setq char (match-string 0)))
14333e31
KH
498 (if (or (memq charset '(unknown
499 eight-bit-control eight-bit-graphic))
251d4f4b 500 (not (or (eq excludes t) (memq charset excludes))))
51ed58ea
KH
501 (let ((slot (assq charset chars)))
502 (if slot
503 (if (not (memq char (nthcdr 2 slot)))
504 (let ((count (nth 1 slot)))
505 (setcar (cdr slot) (1+ count))
506 (if (or (not maxcount) (< count maxcount))
507 (nconc slot (list char)))))
508 (setq chars (cons (list charset 1 char) chars)))))
509 (setq idx (1+ idx))))
510 (save-excursion
511 (goto-char from)
512 (while (re-search-forward "[^\000-\177]" to t)
513 (setq char (preceding-char)
514 charset (char-charset char))
251d4f4b
KH
515 (if (eq charset 'unknown)
516 (setq char (match-string 0)))
14333e31 517 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
251d4f4b 518 (not (or (eq excludes t) (memq charset excludes))))
51ed58ea
KH
519 (let ((slot (assq charset chars)))
520 (if slot
251d4f4b 521 (if (not (member char (nthcdr 2 slot)))
51ed58ea
KH
522 (let ((count (nth 1 slot)))
523 (setcar (cdr slot) (1+ count))
524 (if (or (not maxcount) (< count maxcount))
525 (nconc slot (list char)))))
526 (setq chars (cons (list charset 1 char) chars))))))))
527 (nreverse chars)))
528
c83c4f60
RS
529(defvar last-coding-system-specified nil
530 "Most recent coding system explicitly specified by the user when asked.
531This variable is set whenever Emacs asks the user which coding system
532to use in order to write a file. If you set it to nil explicitly,
533then call `write-region', then afterward this variable will be non-nil
534only if the user was explicitly asked and specified a coding system.")
535
b5edd1d1
KH
536(defvar select-safe-coding-system-accept-default-p nil
537 "If non-nil, a function to control the behaviour of coding system selection.
538The meaning is the same as the argument ACCEPT-DEFAULT-P of the
539function `select-safe-coding-system' (which see). This variable
540overrides that argument.")
541
542(defun select-safe-coding-system (from to &optional default-coding-system
543 accept-default-p)
d5266ddf
KH
544 "Ask a user to select a safe coding system from candidates.
545The candidates of coding systems which can safely encode a text
b5edd1d1
KH
546between FROM and TO are shown in a popup window. Among them, the most
547proper one is suggested as the default.
548
549The list of `buffer-file-coding-system' of the current buffer and the
550most preferred coding system (if it corresponds to a MIME charset) is
551treated as the default coding system list. Among them, the first one
552that safely encodes the text is silently selected and returned without
553any user interaction. See also the command `prefer-coding-system'.
54b226f7 554
b5edd1d1
KH
555Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
556list of coding systems to be prepended to the default coding system
557list.
54b226f7 558
b5edd1d1
KH
559Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
560determine the acceptability of the silently selected coding system.
561It is called with that coding system, and should return nil if it
562should not be silently selected and thus user interaction is required.
563
564The variable `select-safe-coding-system-accept-default-p', if
565non-nil, overrides ACCEPT-DEFAULT-P.
54b226f7
KH
566
567Kludgy feature: if FROM is a string, the string is the target text,
568and TO is ignored."
b5edd1d1
KH
569 (if (and default-coding-system
570 (not (listp default-coding-system)))
571 (setq default-coding-system (list default-coding-system)))
572
573 ;; Change elements of the list to (coding . base-coding).
574 (setq default-coding-system
575 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
576 default-coding-system))
577
578 ;; If buffer-file-coding-system is not nil nor undecided, append it
579 ;; to the defaults.
580 (if buffer-file-coding-system
581 (let ((base (coding-system-base buffer-file-coding-system)))
582 (or (eq base 'undecided)
583 (assq buffer-file-coding-system default-coding-system)
584 (rassq base default-coding-system)
e56d7900 585 (setq default-coding-system
b5edd1d1
KH
586 (append default-coding-system
587 (list (cons buffer-file-coding-system base)))))))
588
589 ;; If the most preferred coding system has the property mime-charset,
590 ;; append it to the defaults.
c24e49a8
KH
591 (let ((tail coding-category-list)
592 preferred base)
593 (while (and tail
87ad2c90 594 (not (setq preferred (symbol-value (car tail)))))
c24e49a8
KH
595 (setq tail (cdr tail)))
596 (and (coding-system-p preferred)
597 (setq base (coding-system-base preferred))
598 (coding-system-get preferred 'mime-charset)
b5edd1d1
KH
599 (not (assq preferred default-coding-system))
600 (not (rassq base default-coding-system))
601 (setq default-coding-system
602 (append default-coding-system (list (cons preferred base))))))
603
604 (if select-safe-coding-system-accept-default-p
605 (setq accept-default-p select-safe-coding-system-accept-default-p))
606
607 (let ((codings (find-coding-systems-region from to))
608 (coding-system nil)
609 (l default-coding-system))
610 (if (eq (car codings) 'undecided)
611 ;; Any coding system is ok.
612 (setq coding-system t)
613 ;; Try the defaults.
614 (while (and l (not coding-system))
615 (if (memq (cdr (car l)) codings)
616 (setq coding-system (car (car l)))
617 (setq l (cdr l))))
618 (if (and coding-system accept-default-p)
619 (or (funcall accept-default-p coding-system)
620 (setq coding-system (list coding-system)))))
621
622 ;; If all the defaults failed, ask a user.
623 (when (or (not coding-system) (consp coding-system))
34104362 624 ;; At first, change each coding system to the corresponding
b5edd1d1
KH
625 ;; mime-charset name if it is also a coding system. Such a name
626 ;; is more friendly to users.
627 (let ((l codings)
34104362
KH
628 mime-charset)
629 (while l
630 (setq mime-charset (coding-system-get (car l) 'mime-charset))
631 (if (and mime-charset (coding-system-p mime-charset))
632 (setcar l mime-charset))
633 (setq l (cdr l))))
634
b5edd1d1
KH
635 ;; Then ask users to select one form CODINGS.
636 (unwind-protect
637 (save-window-excursion
638 (with-output-to-temp-buffer "*Warning*"
639 (save-excursion
640 (set-buffer standard-output)
a672b7cd
KH
641 (if (not default-coding-system)
642 (insert "No default coding systems to try.")
643 (insert "These default coding systems were tried")
644 (if (stringp from)
645 (insert " to encode \""
646 (if (> (length from) 10)
647 (substring from 0 10)
648 from)
649 "...\""))
650 (insert ":\n")
651 (let ((pos (point))
652 (fill-prefix " "))
653 (mapcar (function (lambda (x)
654 (princ " ") (princ (car x))))
655 default-coding-system)
656 (insert "\n")
657 (fill-region-as-paragraph pos (point)))
658 (insert
659 (if (consp coding-system)
660 (concat (format "%s safely encodes the target text,\n"
661 (car coding-system))
662 "but it is not recommended for encoding text in this context,\n"
663 "e.g., for sending an email message.\n")
664 "However, none of them safely encodes the target text.\n")))
b5edd1d1 665 (insert (if (consp coding-system)
2f1fa038
EZ
666 "\nSelect the above, or "
667 "\nSelect ")
668 "one of the following safe coding systems:\n")
b5edd1d1
KH
669 (let ((pos (point))
670 (fill-prefix " "))
671 (mapcar (function (lambda (x) (princ " ") (princ x)))
672 codings)
673 (insert "\n")
674 (fill-region-as-paragraph pos (point)))))
675
676 ;; Read a coding system.
677 (if (consp coding-system)
678 (setq codings (cons (car coding-system) codings)))
679 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
680 codings))
681 (name (completing-read
682 (format "Select coding system (default %s): "
683 (car codings))
684 safe-names nil t nil nil
685 (car (car safe-names)))))
686 (setq last-coding-system-specified (intern name)
687 coding-system last-coding-system-specified)))
688 (kill-buffer "*Warning*")))
689
690 (if (vectorp (coding-system-eol-type coding-system))
691 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
692 (if (numberp eol)
693 (setq coding-system
694 (coding-system-change-eol-conversion coding-system eol)))))
695
696 (if (eq coding-system t)
697 (setq coding-system buffer-file-coding-system))
e56d7900 698 coding-system))
54b226f7
KH
699
700(setq select-safe-coding-system-function 'select-safe-coding-system)
701
46babb23
KH
702(defun select-message-coding-system ()
703 "Return a coding system to encode the outgoing message of the current buffer.
704It at first tries the first coding system found in these variables
705in this order:
706 (1) local value of `buffer-file-coding-system'
707 (2) value of `sendmail-coding-system'
b5edd1d1
KH
708 (3) value of `default-sendmail-coding-system'
709 (4) value of `default-buffer-file-coding-system'
46babb23
KH
710If the found coding system can't encode the current buffer,
711or none of them are bound to a coding system,
48e41165 712it asks the user to select a proper coding system."
46babb23 713 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
b5edd1d1
KH
714 buffer-file-coding-system)
715 sendmail-coding-system
716 default-sendmail-coding-system
717 default-buffer-file-coding-system)))
46babb23
KH
718 (if (eq coding 'no-conversion)
719 ;; We should never use no-conversion for outgoing mails.
720 (setq coding nil))
721 (if (fboundp select-safe-coding-system-function)
722 (funcall select-safe-coding-system-function
b5edd1d1
KH
723 (point-min) (point-max) coding
724 (function (lambda (x) (coding-system-get x 'mime-charset))))
46babb23 725 coding)))
4ed46869 726\f
03c35c83 727;;; Language support stuff.
4ed46869 728
4ed46869 729(defvar language-info-alist nil
2c395d56 730 "Alist of language environment definitions.
4ed46869
KH
731Each element looks like:
732 (LANGUAGE-NAME . ((KEY . INFO) ...))
2c395d56
RS
733where LANGUAGE-NAME is a string, the name of the language environment,
734KEY is a symbol denoting the kind of information, and
735INFO is the data associated with KEY.
736Meaningful values for KEY include
737
738 documentation value is documentation of what this language environment
739 is meant for, and how to use it.
740 charset value is a list of the character sets used by this
741 language environment.
742 sample-text value is one line of text,
743 written using those character sets,
744 appropriate for this language environment.
745 setup-function value is a function to call to switch to this
746 language environment.
747 exit-function value is a function to call to leave this
748 language environment.
749 coding-system value is a list of coding systems that are good
750 for saving text written in this language environment.
751 This list serves as suggestions to the user;
752 in effect, as a kind of documentation.
753 coding-priority value is a list of coding systems for this language
754 environment, in order of decreasing priority.
755 This is used to set up the coding system priority
45d08cb2 756 list when you switch to this language environment.
ddb5c041 757 nonascii-translation
7624ebb9 758 value is a translation table to be set in the
45d08cb2 759 variable `nonascii-translation-table' in this
7624ebb9
KH
760 language environment, or a character set from
761 which `nonascii-insert-offset' is calculated.
ddb5c041
KH
762 input-method value is a default input method for this language
763 environment.
7624ebb9
KH
764 features value is a list of features requested in this
765 language environment.
ddb5c041
KH
766
767The following keys take effect only when multibyte characters are
768globally disabled, i.e. the value of `default-enable-multibyte-characters'
769is nil.
770
771 unibyte-syntax value is a library name to load to set
e8dd0160 772 unibyte 8-bit character syntaxes for this
ddb5c041
KH
773 language environment.
774
775 unibyte-display value is a coding system to encode characters
776 for the terminal. Characters in the range
777 of 160 to 255 display not as octal escapes,
778 but as non-ASCII characters in this language
779 environment.")
2c395d56
RS
780
781(defun get-language-info (lang-env key)
782 "Return information listed under KEY for language environment LANG-ENV.
783KEY is a symbol denoting the kind of information.
784For a list of useful values for KEY and their meanings,
785see `language-info-alist'."
786 (if (symbolp lang-env)
787 (setq lang-env (symbol-name lang-env)))
788 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
4ed46869
KH
789 (if lang-slot
790 (cdr (assq key (cdr lang-slot))))))
791
f08adf27 792(defun set-language-info (lang-env key info)
2c395d56
RS
793 "Modify part of the definition of language environment LANG-ENV.
794Specifically, this stores the information INFO under KEY
795in the definition of this language environment.
4ed46869 796KEY is a symbol denoting the kind of information.
2c395d56 797INFO is the value for that information.
281d03ec 798
2c395d56 799For a list of useful values for KEY and their meanings,
f08adf27 800see `language-info-alist'."
2c395d56
RS
801 (if (symbolp lang-env)
802 (setq lang-env (symbol-name lang-env)))
4ed46869 803 (let (lang-slot key-slot)
2c395d56 804 (setq lang-slot (assoc lang-env language-info-alist))
4ed46869 805 (if (null lang-slot) ; If no slot for the language, add it.
2c395d56 806 (setq lang-slot (list lang-env)
4ed46869
KH
807 language-info-alist (cons lang-slot language-info-alist)))
808 (setq key-slot (assq key lang-slot))
809 (if (null key-slot) ; If no slot for the key, add it.
810 (progn
811 (setq key-slot (list key))
812 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
cda74479 813 (setcdr key-slot (purecopy info))))
4ed46869 814
2c395d56
RS
815(defun set-language-info-alist (lang-env alist &optional parents)
816 "Store ALIST as the definition of language environment LANG-ENV.
817ALIST is an alist of KEY and INFO values. See the documentation of
98c6d6ed 818`language-info-alist' for the meanings of KEY and INFO.
54b226f7 819
2c395d56
RS
820Optional arg PARENTS is a list of parent menu names; it specifies
821where to put this language environment in the
822Describe Language Environment and Set Language Environment menus.
823For example, (\"European\") means to put this language environment
824in the European submenu in each of those two menus."
825 (if (symbolp lang-env)
826 (setq lang-env (symbol-name lang-env)))
54b226f7
KH
827 (let ((describe-map describe-language-environment-map)
828 (setup-map setup-language-environment-map))
829 (if parents
830 (let ((l parents)
9deed82f 831 map parent-symbol parent prompt)
54b226f7
KH
832 (while l
833 (if (symbolp (setq parent-symbol (car l)))
834 (setq parent (symbol-name parent))
835 (setq parent parent-symbol parent-symbol (intern parent)))
836 (setq map (lookup-key describe-map (vector parent-symbol)))
9deed82f
EZ
837 ;; This prompt string is for define-prefix-command, so
838 ;; that the map it creates will be suitable for a menu.
839 (or map (setq prompt (format "%s Environment" parent)))
54b226f7
KH
840 (if (not map)
841 (progn
842 (setq map (intern (format "describe-%s-environment-map"
843 (downcase parent))))
9deed82f 844 (define-prefix-command map nil prompt)
54b226f7
KH
845 (define-key-after describe-map (vector parent-symbol)
846 (cons parent map) t)))
847 (setq describe-map (symbol-value map))
848 (setq map (lookup-key setup-map (vector parent-symbol)))
849 (if (not map)
850 (progn
851 (setq map (intern (format "setup-%s-environment-map"
852 (downcase parent))))
9deed82f 853 (define-prefix-command map nil prompt)
54b226f7
KH
854 (define-key-after setup-map (vector parent-symbol)
855 (cons parent map) t)))
856 (setq setup-map (symbol-value map))
857 (setq l (cdr l)))))
f08adf27
RS
858
859 ;; Set up menu items for this language env.
7624ebb9 860 (let ((doc (assq 'documentation alist)))
f08adf27
RS
861 (when doc
862 (define-key-after describe-map (vector (intern lang-env))
7624ebb9
KH
863 (cons lang-env 'describe-specified-language-support) t)))
864 (define-key-after setup-map (vector (intern lang-env))
865 (cons lang-env 'setup-specified-language-environment) t)
f08adf27 866
54b226f7 867 (while alist
f08adf27 868 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
54b226f7 869 (setq alist (cdr alist)))))
4ed46869 870
ae302641 871(defun read-language-name (key prompt &optional default)
2c395d56 872 "Read a language environment name which has information for KEY.
ddb5c041 873If KEY is nil, read any language environment.
2c395d56
RS
874Prompt with PROMPT. DEFAULT is the default choice of language environment.
875This returns a language environment name as a string."
4ed46869
KH
876 (let* ((completion-ignore-case t)
877 (name (completing-read prompt
878 language-info-alist
ddb5c041
KH
879 (and key
880 (function (lambda (elm) (assq key elm))))
ae302641 881 t nil nil default)))
13e82c04 882 (if (and (> (length name) 0)
ddb5c041
KH
883 (or (not key)
884 (get-language-info name key)))
13e82c04 885 name)))
4ed46869
KH
886\f
887;;; Multilingual input methods.
d0c40faa
KH
888(defgroup leim nil
889 "LEIM: Libraries of Emacs Input Methods."
890 :group 'mule)
4ed46869 891
d0b9c3ab
KH
892(defconst leim-list-file-name "leim-list.el"
893 "Name of LEIM list file.
894This file contains a list of libraries of Emacs input methods (LEIM)
895in the format of Lisp expression for registering each input method.
896Emacs loads this file at startup time.")
897
2e224638
SM
898(defvar leim-list-header (format
899";;; %s -- list of LEIM (Library of Emacs Input Method)
d0b9c3ab
KH
900;;
901;; This file contains a list of LEIM (Library of Emacs Input Method)
c654de1d
DL
902;; methods in the same directory as this file. Loading this file
903;; registers all the input methods in Emacs.
d0b9c3ab 904;;
d33d5fbe 905;; Each entry has the form:
d0b9c3ab
KH
906;; (register-input-method
907;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
908;; TITLE DESCRIPTION
909;; ARG ...)
c654de1d 910;; See the function `register-input-method' for the meanings of the arguments.
d0b9c3ab
KH
911;;
912;; If this directory is included in load-path, Emacs automatically
913;; loads this file at startup time.
914
915"
916 leim-list-file-name)
917 "Header to be inserted in LEIM list file.")
918
e55e92ee 919(defvar leim-list-entry-regexp "^(register-input-method"
d0b9c3ab
KH
920 "Regexp matching head of each entry in LEIM list file.
921See also the variable `leim-list-header'")
922
923(defvar update-leim-list-functions
924 '(quail-update-leim-list-file)
925 "List of functions to call to update LEIM list file.
926Each function is called with one arg, LEIM directory name.")
927
a337fe7f
RS
928(defun update-leim-list-file (&rest dirs)
929 "Update LEIM list file in directories DIRS."
d0b9c3ab
KH
930 (let ((functions update-leim-list-functions))
931 (while functions
a337fe7f 932 (apply (car functions) dirs)
d0b9c3ab
KH
933 (setq functions (cdr functions)))))
934
4ed46869
KH
935(defvar current-input-method nil
936 "The current input method for multilingual text.
96db204a 937If nil, that means no input method is activated now.")
4ed46869
KH
938(make-variable-buffer-local 'current-input-method)
939(put 'current-input-method 'permanent-local t)
940
941(defvar current-input-method-title nil
d0b9c3ab 942 "Title string of the current input method shown in mode line.")
4ed46869
KH
943(make-variable-buffer-local 'current-input-method-title)
944(put 'current-input-method-title 'permanent-local t)
945
b4fba33f 946(defcustom default-input-method nil
8861c593 947 "*Default input method for multilingual text (a string).
b4fba33f 948This is the input method activated automatically by the command
9b10b5a3 949`toggle-input-method' (\\[toggle-input-method])."
8861c593 950 :group 'mule
5806e8a6
GM
951 :type '(choice (const nil) string)
952 :set-after '(current-language-environment))
b4fba33f 953
0f835e87
KH
954(put 'input-method-function 'permanent-local t)
955
723a427a
KH
956(defvar input-method-history nil
957 "History list for some commands that read input methods.")
958(make-variable-buffer-local 'input-method-history)
959(put 'input-method-history 'permanent-local t)
4ed46869
KH
960
961(defvar inactivate-current-input-method-function nil
962 "Function to call for inactivating the current input method.
963Every input method should set this to an appropriate value when activated.
f17ccaee
KH
964This function is called with no argument.
965
966This function should never change the value of `current-input-method'.
967It is set to nil by the function `inactivate-input-method'.")
4ed46869
KH
968(make-variable-buffer-local 'inactivate-current-input-method-function)
969(put 'inactivate-current-input-method-function 'permanent-local t)
970
971(defvar describe-current-input-method-function nil
972 "Function to call for describing the current input method.
973This function is called with no argument.")
974(make-variable-buffer-local 'describe-current-input-method-function)
975(put 'describe-current-input-method-function 'permanent-local t)
976
d0b9c3ab 977(defvar input-method-alist nil
2c395d56 978 "Alist of input method names vs how to use them.
d0b9c3ab 979Each element has the form:
2c395d56
RS
980 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
981See the function `register-input-method' for the meanings of the elements.")
982
f08adf27 983(defun register-input-method (input-method lang-env &rest args)
bf42aa15 984 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
f08adf27 985INPUT-METHOD and LANG-ENV are symbols or strings.
d0b9c3ab 986
d0b9c3ab 987The remaining arguments are:
2c395d56
RS
988 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
989ACTIVATE-FUNC is a function to call to activate this method.
990TITLE is a string to show in the mode line when this method is active.
991DESCRIPTION is a string describing this method and what it is good for.
992The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
205814ee
KH
993All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
994
c654de1d
DL
995This function is mainly used in the file \"leim-list.el\" which is
996created at Emacs build time, registering all Quail input methods
0b6cadff 997contained in the Emacs distribution.
205814ee 998
0b6cadff 999In case you want to register a new Quail input method by yourself, be
205814ee 1000careful to use the same input method title as given in the third
0b6cadff
DL
1001parameter of `quail-define-package'. (If the values are different, the
1002string specified in this function takes precedence.)
205814ee
KH
1003
1004The commands `describe-input-method' and `list-input-methods' need
0b6cadff
DL
1005these duplicated values to show some information about input methods
1006without loading the relevant Quail packages."
f08adf27
RS
1007 (if (symbolp lang-env)
1008 (setq lang-env (symbol-name lang-env)))
4ef06f75
KH
1009 (if (symbolp input-method)
1010 (setq input-method (symbol-name input-method)))
f08adf27 1011 (let ((info (cons lang-env args))
d0b9c3ab
KH
1012 (slot (assoc input-method input-method-alist)))
1013 (if slot
1014 (setcdr slot info)
1015 (setq slot (cons input-method info))
1016 (setq input-method-alist (cons slot input-method-alist)))))
1017
4d5ac029 1018(defun read-input-method-name (prompt &optional default inhibit-null)
d0b9c3ab 1019 "Read a name of input method from a minibuffer prompting with PROMPT.
4d5ac029 1020If DEFAULT is non-nil, use that as the default,
0b6cadff 1021and substitute it into PROMPT at the first `%s'.
4ef06f75
KH
1022If INHIBIT-NULL is non-nil, null input signals an error.
1023
1024The return value is a string."
4d5ac029
RS
1025 (if default
1026 (setq prompt (format prompt default)))
d0b9c3ab 1027 (let* ((completion-ignore-case t)
c54044ff
KH
1028 ;; As it is quite normal to change input method in the
1029 ;; minibuffer, we must enable it even if
1030 ;; enable-recursive-minibuffers is currently nil.
1031 (enable-recursive-minibuffers t)
723a427a
KH
1032 ;; This binding is necessary because input-method-history is
1033 ;; buffer local.
d0b9c3ab 1034 (input-method (completing-read prompt input-method-alist
87505a98
RS
1035 nil t nil 'input-method-history
1036 default)))
bf294e6e
KH
1037 (if (and input-method (symbolp input-method))
1038 (setq input-method (symbol-name input-method)))
d0b9c3ab
KH
1039 (if (> (length input-method) 0)
1040 input-method
1041 (if inhibit-null
43807b77 1042 (error "No valid input method is specified")))))
d0b9c3ab 1043
d0b9c3ab 1044(defun activate-input-method (input-method)
2c395d56
RS
1045 "Switch to input method INPUT-METHOD for the current buffer.
1046If some other input method is already active, turn it off first.
1047If INPUT-METHOD is nil, deactivate any current input method."
305a3cb6 1048 (if (and input-method (symbolp input-method))
4ef06f75 1049 (setq input-method (symbol-name input-method)))
723a427a
KH
1050 (if (and current-input-method
1051 (not (string= current-input-method input-method)))
305a3cb6 1052 (inactivate-input-method))
2c395d56 1053 (unless (or current-input-method (null input-method))
d0b9c3ab
KH
1054 (let ((slot (assoc input-method input-method-alist)))
1055 (if (null slot)
723a427a 1056 (error "Can't activate input method `%s'" input-method))
278dd6ac 1057 (setq current-input-method-title nil)
8efc03e1
KH
1058 (let ((func (nth 2 slot)))
1059 (if (functionp func)
1060 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1061 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1062 (progn
1063 (require (cdr func))
1064 (apply (car func) input-method (nthcdr 5 slot)))
1065 (error "Can't activate input method `%s'" input-method))))
d0b9c3ab 1066 (setq current-input-method input-method)
278dd6ac
KH
1067 (or (stringp current-input-method-title)
1068 (setq current-input-method-title (nth 3 slot)))
28885c0e
KH
1069 (unwind-protect
1070 (run-hooks 'input-method-activate-hook)
1071 (force-mode-line-update)))))
15b3e511 1072
15b3e511 1073(defun inactivate-input-method ()
f17ccaee 1074 "Turn off the current input method."
723a427a
KH
1075 (when current-input-method
1076 (if input-method-history
1077 (unless (string= current-input-method (car input-method-history))
1078 (setq input-method-history
1079 (cons current-input-method
1080 (delete current-input-method input-method-history))))
1081 (setq input-method-history (list current-input-method)))
1082 (unwind-protect
1083 (funcall inactivate-current-input-method-function)
15b3e511 1084 (unwind-protect
723a427a
KH
1085 (run-hooks 'input-method-inactivate-hook)
1086 (setq current-input-method nil
2b1ec973 1087 input-method-function nil
28885c0e
KH
1088 current-input-method-title nil)
1089 (force-mode-line-update)))))
4ed46869 1090
8b784951 1091(defun set-input-method (input-method)
2c395d56 1092 "Select and activate input method INPUT-METHOD for the current buffer.
bc406911 1093This also sets the default input method to the one you specify.
402dbbd1
EZ
1094If INPUT-METHOD is nil, this function turns off the input method, and
1095also causes you to be prompted for a name of an input method the next
1096time you invoke \\[toggle-input-method].
1097
bc406911
EZ
1098To deactivate the input method interactively, use \\[toggle-input-method].
1099To deactivate it programmatically, use \\[inactivate-input-method]."
d0b9c3ab 1100 (interactive
723a427a 1101 (let* ((default (or (car input-method-history) default-input-method)))
42395763 1102 (list (read-input-method-name
87505a98 1103 (if default "Select input method (default %s): " "Select input method: ")
42395763 1104 default t))))
d0b9c3ab 1105 (activate-input-method input-method)
42395763 1106 (setq default-input-method input-method))
4ed46869
KH
1107
1108(defun toggle-input-method (&optional arg)
f8ec20be
RS
1109 "Enable or disable multilingual text input method for the current buffer.
1110Only one input method can be enabled at any time in a given buffer.
1111
1112The normal action is to enable an input method if none was
1113enabled, and disable the current one otherwise. Which input method
1114to enable can be determined in various ways--either the one most
1115recently used, or the one specified by `default-input-method', or
1116as a last resort by reading the name of an input method in the
1117minibuffer.
1118
1119With a prefix argument, read an input method name with the minibuffer
1120and enable that one. The default is the most recent input method specified
f2979bdb 1121\(not including the currently active input method, if any)."
f8ec20be 1122
4ed46869 1123 (interactive "P")
7ddbb5bc
RS
1124 (if (and current-input-method (not arg))
1125 (inactivate-input-method)
1126 (let ((default (or (car input-method-history) default-input-method)))
1127 (if (and arg default (equal current-input-method default)
1128 (> (length input-method-history) 1))
1129 (setq default (nth 1 input-method-history)))
723a427a
KH
1130 (activate-input-method
1131 (if (or arg (not default))
7ddbb5bc
RS
1132 (progn
1133 (read-input-method-name
1134 (if default "Input method (default %s): " "Input method: " )
1135 default t))
723a427a
KH
1136 default))
1137 (or default-input-method
1138 (setq default-input-method current-input-method)))))
d0b9c3ab
KH
1139
1140(defun describe-input-method (input-method)
2c395d56 1141 "Describe input method INPUT-METHOD."
d0b9c3ab
KH
1142 (interactive
1143 (list (read-input-method-name
1144 "Describe input method (default, current choice): ")))
78754934 1145 (if (and input-method (symbolp input-method))
4ef06f75 1146 (setq input-method (symbol-name input-method)))
d0b9c3ab
KH
1147 (if (null input-method)
1148 (describe-current-input-method)
464cc130
KH
1149 (let ((current current-input-method))
1150 (condition-case nil
1151 (progn
1152 (save-excursion
1153 (activate-input-method input-method)
1154 (describe-current-input-method))
1155 (activate-input-method current))
1156 (error
1157 (activate-input-method current)
5f395df3
SM
1158 (help-setup-xref (list #'describe-input-method input-method)
1159 (interactive-p))
1160 (with-output-to-temp-buffer (help-buffer)
464cc130
KH
1161 (let ((elt (assoc input-method input-method-alist)))
1162 (princ (format
1163 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1164 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
d0b9c3ab
KH
1165
1166(defun describe-current-input-method ()
96db204a 1167 "Describe the input method currently in use."
4ed46869
KH
1168 (if current-input-method
1169 (if (and (symbolp describe-current-input-method-function)
1170 (fboundp describe-current-input-method-function))
1171 (funcall describe-current-input-method-function)
1172 (message "No way to describe the current input method `%s'"
f2979bdb 1173 current-input-method)
4ed46869 1174 (ding))
d0b9c3ab 1175 (error "No input method is activated now")))
4ed46869 1176
d3459641 1177(defun read-multilingual-string (prompt &optional initial-input input-method)
4ed46869
KH
1178 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1179The input method selected last time is activated in minibuffer.
15b3e511 1180If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
d0b9c3ab
KH
1181initially.
1182Optional 3rd argument INPUT-METHOD specifies the input method
4ef06f75
KH
1183to be activated instead of the one selected last time. It is a symbol
1184or a string."
88d559ec
KH
1185 (setq input-method
1186 (or input-method
d3459641 1187 current-input-method
88d559ec
KH
1188 default-input-method
1189 (read-input-method-name "Input method: " nil t)))
3df60841 1190 (if (and input-method (symbolp input-method))
4ef06f75 1191 (setq input-method (symbol-name input-method)))
305a3cb6
KH
1192 (let ((prev-input-method current-input-method))
1193 (unwind-protect
1194 (progn
1195 (activate-input-method input-method)
1196 (read-string prompt initial-input nil nil t))
1197 (activate-input-method prev-input-method))))
4ed46869
KH
1198
1199;; Variables to control behavior of input methods. All input methods
1200;; should react to these variables.
1201
8efc03e1
KH
1202(defcustom input-method-verbose-flag 'default
1203 "*A flag to control extra guidance given by input methods.
1204The value should be nil, t, `complex-only', or `default'.
4ed46869 1205
cb29dfb6 1206The extra guidance is done by showing list of available keys in echo
8efc03e1
KH
1207area. When you use the input method in the minibuffer, the guidance
1208is shown at the bottom short window (split from the existing window).
c27c4ed8 1209
8efc03e1
KH
1210If the value is t, extra guidance is always given, if the value is
1211nil, extra guidance is always suppressed.
1212
1213If the value is `complex-only', only complex input methods such as
1214`chinese-py' and `japanese' give extra guidance.
1215
1216If the value is `default', complex input methods always give extra
1217guidance, but simple input methods give it only when you are not in
1218the minibuffer.
1219
1220See also the variable `input-method-highlight-flag'."
1221 :type '(choice (const t) (const nil) (const complex-only) (const default))
42395763
RS
1222 :group 'mule)
1223
1224(defcustom input-method-highlight-flag t
1225 "*If this flag is non-nil, input methods highlight partially-entered text.
1226For instance, while you are in the middle of a Quail input method sequence,
1227the text inserted so far is temporarily underlined.
8efc03e1
KH
1228The underlining goes away when you finish or abort the input method sequence.
1229See also the variable `input-method-verbose-flag'."
42395763
RS
1230 :type 'boolean
1231 :group 'mule)
4ed46869
KH
1232
1233(defvar input-method-activate-hook nil
f17ccaee
KH
1234 "Normal hook run just after an input method is activated.
1235
1236The variable `current-input-method' keeps the input method name
1237just activated.")
4ed46869
KH
1238
1239(defvar input-method-inactivate-hook nil
f17ccaee
KH
1240 "Normal hook run just after an input method is inactivated.
1241
1242The variable `current-input-method' still keeps the input method name
4d0e6a11 1243just inactivated.")
4ed46869
KH
1244
1245(defvar input-method-after-insert-chunk-hook nil
1246 "Normal hook run just after an input method insert some chunk of text.")
1247
dccca980 1248(defvar input-method-exit-on-first-char nil
0b6cadff 1249 "This flag controls when an input method returns.
dccca980
KH
1250Usually, the input method does not return while there's a possibility
1251that it may find a different translation if a user types another key.
39e643e2
RS
1252But, it this flag is non-nil, the input method returns as soon as
1253the current key sequence gets long enough to have some valid translation.")
dccca980
KH
1254
1255(defvar input-method-use-echo-area nil
1256 "This flag controls how an input method shows an intermediate key sequence.
39e643e2
RS
1257Usually, the input method inserts the intermediate key sequence,
1258or candidate translations corresponding to the sequence,
1259at point in the current buffer.
1260But, if this flag is non-nil, it displays them in echo area instead.")
dccca980 1261
723a427a
KH
1262(defvar input-method-exit-on-invalid-key nil
1263 "This flag controls the behaviour of an input method on invalid key input.
1264Usually, when a user types a key which doesn't start any character
1265handled by the input method, the key is handled by turning off the
e8dd0160 1266input method temporarily. After that key, the input method is re-enabled.
723a427a
KH
1267But, if this flag is non-nil, the input method is never back on.")
1268
4ed46869 1269\f
8efc03e1
KH
1270(defvar set-language-environment-hook nil
1271 "Normal hook run after some language environment is set.
1272
1273When you set some hook function here, that effect usually should not
1274be inherited to another language environment. So, you had better set
1275another function in `exit-language-environment-hook' (which see) to
1276cancel the effect.")
1277
1278(defvar exit-language-environment-hook nil
1279 "Normal hook run after exiting from some language environment.
1280When this hook is run, the variable `current-language-environment'
1281is still bound to the language environment being exited.
1282
e8dd0160 1283This hook is mainly used for canceling the effect of
8efc03e1
KH
1284`set-language-environment-hook' (which-see).")
1285
b0648a00
RS
1286(put 'setup-specified-language-environment 'apropos-inhibit t)
1287
15b3e511 1288(defun setup-specified-language-environment ()
f08adf27 1289 "Switch to a specified language environment."
15b3e511 1290 (interactive)
f850d782 1291 (let (language-name)
15b3e511
KH
1292 (if (and (symbolp last-command-event)
1293 (or (not (eq last-command-event 'Default))
1294 (setq last-command-event 'English))
f850d782
RS
1295 (setq language-name (symbol-name last-command-event)))
1296 (set-language-environment language-name)
15b3e511 1297 (error "Bogus calling sequence"))))
4ed46869 1298
8861c593 1299(defcustom current-language-environment "English"
94d04df6 1300 "The last language environment specified with `set-language-environment'.
ebef6d93
KH
1301This variable should be set only with \\[customize], which is equivalent
1302to using the function `set-language-environment'."
94d04df6 1303 :link '(custom-manual "(emacs)Language Environments")
dff1aa24 1304 :set (lambda (symbol value) (set-language-environment value))
94d04df6
DL
1305 :get (lambda (x)
1306 (or (car-safe (assoc-ignore-case
1307 (if (symbolp current-language-environment)
1308 (symbol-name current-language-environment)
1309 current-language-environment)
1310 language-info-alist))
1311 "English"))
1312 :type (cons 'choice (mapcar (lambda (lang)
1313 (list 'const (car lang)))
1314 language-info-alist))
8861c593
RS
1315 :initialize 'custom-initialize-default
1316 :group 'mule
1317 :type 'string)
f850d782 1318
ddb5c041
KH
1319(defun reset-language-environment ()
1320 "Reset multilingual environment of Emacs to the default status.
1321
1322The default status is as follows:
1323
1324 The default value of buffer-file-coding-system is nil.
1325 The default coding system for process I/O is nil.
1326 The default value for the command `set-terminal-coding-system' is nil.
1327 The default value for the command `set-keyboard-coding-system' is nil.
1328
1329 The order of priorities of coding categories and the coding system
1330 bound to each category are as follows
1331 coding category coding system
1332 --------------------------------------------------
1333 coding-category-iso-8-2 iso-latin-1
1334 coding-category-iso-8-1 iso-latin-1
1335 coding-category-iso-7-tight iso-2022-jp
1336 coding-category-iso-7 iso-2022-7bit
1337 coding-category-iso-7-else iso-2022-7bit-lock
1338 coding-category-iso-8-else iso-2022-8bit-ss2
1339 coding-category-emacs-mule emacs-mule
1340 coding-category-raw-text raw-text
1341 coding-category-sjis japanese-shift-jis
1342 coding-category-big5 chinese-big5
1343 coding-category-ccl nil
e8dd0160 1344 coding-category-binary no-conversion
631f62fd
KH
1345 coding-category-utf-16-be nil
1346 coding-category-utf-16-le nil
c654de1d 1347 coding-category-utf-8 mule-utf-8"
ddb5c041
KH
1348 (interactive)
1349 ;; This function formerly set default-enable-multibyte-characters to t,
1350 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1351
1352 (setq coding-category-iso-7-tight 'iso-2022-jp
1353 coding-category-iso-7 'iso-2022-7bit
1354 coding-category-iso-8-1 'iso-latin-1
1355 coding-category-iso-8-2 'iso-latin-1
1356 coding-category-iso-7-else 'iso-2022-7bit-lock
1357 coding-category-iso-8-else 'iso-2022-8bit-ss2
1358 coding-category-emacs-mule 'emacs-mule
1359 coding-category-raw-text 'raw-text
1360 coding-category-sjis 'japanese-shift-jis
1361 coding-category-big5 'chinese-big5
631f62fd 1362 coding-category-utf-16-be nil
b909cfc0
KH
1363 coding-category-utf-16-le nil
1364 coding-category-utf-8 'mule-utf-8
1365 coding-category-ccl nil
1366 coding-category-binary 'no-conversion)
ddb5c041
KH
1367
1368 (set-coding-priority
1369 '(coding-category-iso-8-1
1370 coding-category-iso-8-2
1371 coding-category-iso-7-tight
1372 coding-category-iso-7
1373 coding-category-iso-7-else
1374 coding-category-iso-8-else
1375 coding-category-emacs-mule
1376 coding-category-raw-text
1377 coding-category-sjis
1378 coding-category-big5
1379 coding-category-ccl
9bfcd269
KH
1380 coding-category-binary
1381 coding-category-utf-16-be
b909cfc0
KH
1382 coding-category-utf-16-le
1383 coding-category-utf-8))
ddb5c041 1384
91693d18
KH
1385 (update-coding-systems-internal)
1386
ddb5c041 1387 (set-default-coding-systems nil)
b5edd1d1 1388 (setq default-sendmail-coding-system 'iso-latin-1)
0c47a7c8 1389 (setq default-process-coding-system '(undecided . iso-latin-1))
b5edd1d1 1390
ddb5c041
KH
1391 ;; Don't alter the terminal and keyboard coding systems here.
1392 ;; The terminal still supports the same coding system
1393 ;; that it supported a minute ago.
843f91fd
RS
1394;;; (set-terminal-coding-system-internal nil)
1395;;; (set-keyboard-coding-system-internal nil)
ddb5c041
KH
1396
1397 (setq nonascii-translation-table nil
1398 nonascii-insert-offset 0))
1399
0c47a7c8
KH
1400(reset-language-environment)
1401
40c81f74
PE
1402(defun set-display-table-and-terminal-coding-system (language-name)
1403 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1404 (let ((coding (get-language-info language-name 'unibyte-display)))
1405 (if coding
1406 (standard-display-european-internal)
1407 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1408 (aset standard-display-table 146 nil))
1409 (or (eq window-system 'pc)
1410 (set-terminal-coding-system coding))))
1411
166246f7 1412(defun set-language-environment (language-name)
6c05d680
RS
1413 "Set up multi-lingual environment for using LANGUAGE-NAME.
1414This sets the coding system priority and the default input method
8861c593
RS
1415and sometimes other things. LANGUAGE-NAME should be a string
1416which is the name of a language environment. For example, \"Latin-1\"
1417specifies the character set for the major languages of Western Europe."
8efc03e1 1418 (interactive (list (read-language-name
ddb5c041 1419 nil
8efc03e1 1420 "Set language environment (default, English): ")))
4ef06f75
KH
1421 (if language-name
1422 (if (symbolp language-name)
1423 (setq language-name (symbol-name language-name)))
1424 (setq language-name "English"))
ddb5c041 1425 (or (assoc-ignore-case language-name language-info-alist)
f850d782 1426 (error "Language environment not defined: %S" language-name))
8efc03e1
KH
1427 (if current-language-environment
1428 (let ((func (get-language-info current-language-environment
1429 'exit-function)))
e63645c2 1430 (run-hooks 'exit-language-environment-hook)
5f395df3 1431 (if (functionp func) (funcall func))))
03c35c83
EZ
1432 (let ((default-eol-type (coding-system-eol-type
1433 default-buffer-file-coding-system)))
1434 (reset-language-environment)
ddb5c041 1435
03c35c83
EZ
1436 (setq current-language-environment language-name)
1437 (set-language-environment-coding-systems language-name default-eol-type))
ddb5c041
KH
1438 (let ((input-method (get-language-info language-name 'input-method)))
1439 (when input-method
1440 (setq default-input-method input-method)
1441 (if input-method-history
1442 (setq input-method-history
1443 (cons input-method
1444 (delete input-method input-method-history))))))
ec241f58
EZ
1445 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1446 (dos-table
4e2ac2d9
EZ
1447 (if (eq window-system 'pc)
1448 (intern
d9c0a50e 1449 (format "cp%d-nonascii-translation-table" dos-codepage)))))
03c35c83
EZ
1450 (cond
1451 ((char-table-p nonascii)
1452 (setq nonascii-translation-table nonascii))
ec241f58 1453 ((and (eq window-system 'pc) (boundp dos-table))
03c35c83
EZ
1454 ;; DOS terminals' default is to use a special non-ASCII translation
1455 ;; table as appropriate for the installed codepage.
ec241f58 1456 (setq nonascii-translation-table (symbol-value dos-table)))
03c35c83
EZ
1457 ((charsetp nonascii)
1458 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
ddb5c041 1459
63283a8f 1460 ;; Unibyte setups if necessary.
ddb5c041 1461 (unless default-enable-multibyte-characters
63283a8f 1462 ;; Syntax and case table.
ddb5c041
KH
1463 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1464 (if syntax
1465 (let ((set-case-syntax-set-multibyte nil))
63283a8f
KH
1466 (load syntax nil t))
1467 ;; No information for syntax and case. Reset to the defaults.
1468 (let ((syntax-table (standard-syntax-table))
1469 (case-table (standard-case-table))
03c35c83 1470 (ch (if (eq window-system 'pc) 128 160)))
63283a8f
KH
1471 (while (< ch 256)
1472 (modify-syntax-entry ch " " syntax-table)
1473 (aset case-table ch ch)
1474 (setq ch (1+ ch)))
1475 (set-char-table-extra-slot case-table 0 nil)
1476 (set-char-table-extra-slot case-table 1 nil)
1477 (set-char-table-extra-slot case-table 2 nil))
1478 (set-standard-case-table (standard-case-table))
1479 (let ((list (buffer-list)))
1480 (while list
1481 (with-current-buffer (car list)
1482 (set-case-table (standard-case-table)))
1483 (setq list (cdr list))))))
40c81f74 1484 (set-display-table-and-terminal-coding-system language-name))
ddb5c041
KH
1485
1486 (let ((required-features (get-language-info language-name 'features)))
1487 (while required-features
1488 (require (car required-features))
1489 (setq required-features (cdr required-features))))
1490 (let ((func (get-language-info language-name 'setup-function)))
5f395df3 1491 (if (functionp func)
ddb5c041 1492 (funcall func)))
8efc03e1 1493 (run-hooks 'set-language-environment-hook)
f850d782 1494 (force-mode-line-update t))
4ed46869 1495
51a8fc1d
RS
1496(defun standard-display-european-internal ()
1497 ;; Actually set up direct output of non-ASCII characters.
03c35c83
EZ
1498 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1499 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1500 ;; the native font, and codes 160 and 146 stand for something very
1501 ;; different there.
1502 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1503 (progn
1504 ;; Make non-line-break space display as a plain space.
1505 ;; Most X fonts do the wrong thing for code 160.
1506 (aset standard-display-table 160 [32])
1492f7ac
DL
1507 ;; With luck, non-Latin-1 fonts are more recent and so don't
1508 ;; have this bug.
1509 (aset standard-display-table 2208 [32]) ; Latin-1 NBSP
1510 ;; Most Windows programs send out apostrophes as \222. Most X fonts
03c35c83 1511 ;; don't contain a character at that position. Map it to the ASCII
5f395df3
SM
1512 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1513 ;; U+2019, normally from the windows-1252 character set. XFree 4
1514 ;; fonts probably have the appropriate glyph at this position,
1515 ;; so they could use standard-display-8bit. It's better to use a
1516 ;; proper windows-1252 coding system. --fx]
03c35c83
EZ
1517 (aset standard-display-table 146 [39]))))
1518
1519(defun set-language-environment-coding-systems (language-name
1520 &optional eol-type)
1521 "Do various coding system setups for language environment LANGUAGE-NAME.
1522
1523The optional arg EOL-TYPE specifies the eol-type of the default value
1524of buffer-file-coding-system set by this function."
54b226f7
KH
1525 (let* ((priority (get-language-info language-name 'coding-priority))
1526 (default-coding (car priority)))
1527 (if priority
1528 (let ((categories (mapcar 'coding-system-category priority)))
03c35c83
EZ
1529 (set-default-coding-systems
1530 (if (memq eol-type '(0 1 2 unix dos mac))
1531 (coding-system-change-eol-conversion default-coding eol-type)
1532 default-coding))
46babb23 1533 (setq default-sendmail-coding-system default-coding)
54b226f7
KH
1534 (set-coding-priority categories)
1535 (while priority
1536 (set (car categories) (car priority))
1537 (setq priority (cdr priority) categories (cdr categories)))
45d08cb2 1538 (update-coding-systems-internal)))))
54b226f7 1539
4ed46869
KH
1540;; Print all arguments with `princ', then print "\n".
1541(defsubst princ-list (&rest args)
1542 (while args (princ (car args)) (setq args (cdr args)))
1543 (princ "\n"))
1544
b0648a00
RS
1545(put 'describe-specified-language-support 'apropos-inhibit t)
1546
48082651 1547;; Print a language specific information such as input methods,
13e82c04 1548;; charsets, and coding systems. This function is intended to be
48082651 1549;; called from the menu:
281d03ec 1550;; [menu-bar mule describe-language-environment LANGUAGE]
48082651
KH
1551;; and should not run it by `M-x describe-current-input-method-function'.
1552(defun describe-specified-language-support ()
96db204a 1553 "Describe how Emacs supports the specified language environment."
48082651 1554 (interactive)
281d03ec 1555 (let (language-name)
48082651 1556 (if (not (and (symbolp last-command-event)
cda74479
DL
1557 (or (not (eq last-command-event 'Default))
1558 (setq last-command-event 'English))
281d03ec 1559 (setq language-name (symbol-name last-command-event))))
48082651 1560 (error "Bogus calling sequence"))
281d03ec
RS
1561 (describe-language-environment language-name)))
1562
1563(defun describe-language-environment (language-name)
1564 "Describe how Emacs supports language environment LANGUAGE-NAME."
78754934
KH
1565 (interactive
1566 (list (read-language-name
1567 'documentation
8adfa8be 1568 "Describe language environment (default, current choice): ")))
f850d782
RS
1569 (if (null language-name)
1570 (setq language-name current-language-environment))
281d03ec
RS
1571 (if (or (null language-name)
1572 (null (get-language-info language-name 'documentation)))
1573 (error "No documentation for the specified language"))
4ef06f75
KH
1574 (if (symbolp language-name)
1575 (setq language-name (symbol-name language-name)))
464cc130
KH
1576 (let ((doc (get-language-info language-name 'documentation))
1577 pos)
c3034e84
SM
1578 (help-setup-xref (list #'describe-language-environment language-name)
1579 (interactive-p))
1580 (with-output-to-temp-buffer (help-buffer)
464cc130
KH
1581 (save-excursion
1582 (set-buffer standard-output)
1583 (insert language-name " language environment\n\n")
1584 (if (stringp doc)
1585 (insert doc "\n\n"))
e036b0a6
KH
1586 (condition-case nil
1587 (let ((str (eval (get-language-info language-name 'sample-text))))
1588 (if (stringp str)
1589 (insert "Sample text:\n " str "\n\n")))
1590 (error nil))
464cc130
KH
1591 (let ((input-method (get-language-info language-name 'input-method))
1592 (l (copy-sequence input-method-alist)))
1593 (insert "Input methods")
1594 (when input-method
1595 (insert " (default, " input-method ")")
1596 (setq input-method (assoc input-method input-method-alist))
1597 (setq l (cons input-method (delete input-method l))))
1598 (insert ":\n")
48082651 1599 (while l
464cc130
KH
1600 (when (string= language-name (nth 1 (car l)))
1601 (insert " " (car (car l)))
1602 (search-backward (car (car l)))
1603 (help-xref-button 0 #'describe-input-method (car (car l))
1604 "mouse-2, RET: describe this input method")
1605 (goto-char (point-max))
2fa7e202
KH
1606 (insert " (\""
1607 (if (stringp (nth 3 (car l)))
1608 (nth 3 (car l))
1609 (car (nth 3 (car l))))
1610 "\" in mode line)\n"))
464cc130
KH
1611 (setq l (cdr l)))
1612 (insert "\n"))
1613 (insert "Character sets:\n")
1614 (let ((l (get-language-info language-name 'charset)))
1615 (if (null l)
1616 (insert " nothing specific to " language-name "\n")
1617 (while l
1618 (insert " " (symbol-name (car l)))
1619 (search-backward (symbol-name (car l)))
1620 (help-xref-button 0 #'describe-character-set (car l)
1621 "mouse-2, RET: describe this character set")
1622 (goto-char (point-max))
1623 (insert ": " (charset-description (car l)) "\n")
1624 (setq l (cdr l)))))
1625 (insert "\n")
1626 (insert "Coding systems:\n")
1627 (let ((l (get-language-info language-name 'coding-system)))
1628 (if (null l)
1629 (insert " nothing specific to " language-name "\n")
1630 (while l
1631 (insert " " (symbol-name (car l)))
1632 (search-backward (symbol-name (car l)))
1633 (help-xref-button 0 #'describe-coding-system (car l)
1634 "mouse-2, RET: describe this coding system")
1635 (goto-char (point-max))
1636 (insert " (`"
1637 (coding-system-mnemonic (car l))
1638 "' in mode line):\n\t"
1639 (coding-system-doc-string (car l))
1640 "\n")
1641 (let ((aliases (coding-system-get (car l)
1642 'alias-coding-systems)))
1643 (when aliases
1644 (insert "\t(alias:")
1645 (while aliases
1646 (insert " " (symbol-name (car aliases)))
1647 (setq aliases (cdr aliases)))
1648 (insert ")\n")))
c3034e84 1649 (setq l (cdr l)))))))))
4ed46869 1650\f
40c81f74
PE
1651;;; Locales.
1652
0d7c5bb9
DL
1653(defvar locale-translation-file-name nil
1654 "File name for the system's file of locale-name aliases, or nil if none.")
40c81f74 1655
5f395df3
SM
1656;; The following definitions might as well be marked as constants and
1657;; purecopied, since they're normally used on startup, and probably
1658;; should reflect the facilities of the base Emacs.
1659(defconst locale-language-names
1660 (purecopy
1661 '(
1662 ;; UTF-8 is not yet implemented.
1663 ;; Put this first, so that e.g. "ko.UTF-8" does not match "ko" below.
1664 (".*[._]utf" . nil)
40c81f74
PE
1665
1666 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
1667 ;; as specified in the Single Unix Spec, Version 2.
1668 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
1669 ;; with additions from ISO 639/RA Newsletter No.1/1989;
5f395df3
SM
1670 ;; see Internet RFC 2165 (1997-06) and
1671 ;; http://www.evertype.com/standards/iso639/iso639-en.html
1672 ;; TERRITORY is a country code taken from ISO 3166
1673 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
40c81f74 1674 ;; CODESET and MODIFIER are implementation-dependent.
5f395df3 1675
40c81f74
PE
1676 ; aa Afar
1677 ; ab Abkhazian
6ececc4d 1678 ("af" . "Latin-1") ; Afrikaans
40c81f74 1679 ("am" . "Ethiopic") ; Amharic
5f395df3 1680 ; ar Arabic glibc uses 8859-6
40c81f74
PE
1681 ; as Assamese
1682 ; ay Aymara
1683 ; az Azerbaijani
1684 ; ba Bashkir
6ececc4d
PE
1685 ("be" . "Latin-5") ; Byelorussian
1686 ("bg" . "Latin-5") ; Bulgarian
40c81f74
PE
1687 ; bh Bihari
1688 ; bi Bislama
1689 ; bn Bengali, Bangla
1690 ("bo" . "Tibetan")
1691 ("br" . "Latin-1") ; Breton
1692 ("ca" . "Latin-1") ; Catalan
1693 ; co Corsican
1694 ("cs" . "Czech")
6ececc4d 1695 ("cy" . "Latin-8") ; Welsh
40c81f74
PE
1696 ("da" . "Latin-1") ; Danish
1697 ("de" . "German")
1698 ; dz Bhutani
1699 ("el" . "Greek")
6ececc4d
PE
1700 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
1701 ("en" . "Latin-1") ; English
40c81f74 1702 ("eo" . "Latin-3") ; Esperanto
8c4b6822 1703 ("es" . "Spanish")
40c81f74
PE
1704 ("et" . "Latin-4") ; Estonian
1705 ("eu" . "Latin-1") ; Basque
1706 ; fa Persian
1707 ("fi" . "Latin-1") ; Finnish
1708 ; fj Fiji
1709 ("fo" . "Latin-1") ; Faroese
8d738cfd 1710 ("fr" . "French") ; French
40c81f74 1711 ("fy" . "Latin-1") ; Frisian
6ececc4d
PE
1712 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
1713 ("gd" . "Latin-1") ; Scots Gaelic
1714 ("gl" . "Latin-1") ; Galician
40c81f74
PE
1715 ; gn Guarani
1716 ; gu Gujarati
5f395df3 1717 ("gv" . "Latin-8") ; Manx Gaelic
40c81f74
PE
1718 ; ha Hausa
1719 ("he" . "Hebrew")
5f395df3 1720 ("hi" . "Devanagari") ; Hindi glibc uses utf-8
40c81f74
PE
1721 ("hr" . "Latin-2") ; Croatian
1722 ("hu" . "Latin-2") ; Hungarian
1723 ; hy Armenian
1724 ; ia Interlingua
1725 ("id" . "Latin-1") ; Indonesian
1726 ; ie Interlingue
1727 ; ik Inupiak
1728 ("is" . "Latin-1") ; Icelandic
1729 ("it" . "Latin-1") ; Italian
1730 ; iu Inuktitut
1731 ("ja" . "Japanese")
1732 ; jw Javanese
1733 ; ka Georgian
1734 ; kk Kazakh
6ececc4d 1735 ("kl" . "Latin-1") ; Greenlandic
40c81f74
PE
1736 ; km Cambodian
1737 ; kn Kannada
1738 ("ko" . "Korean")
1739 ; ks Kashmiri
1740 ; ku Kurdish
5f395df3 1741 ("kw" . "Latin-1") ; Cornish
40c81f74
PE
1742 ; ky Kirghiz
1743 ("la" . "Latin-1") ; Latin
5f395df3 1744 ("lb" . "Latin-1") ; Luxemburgish
40c81f74
PE
1745 ; ln Lingala
1746 ("lo" . "Lao") ; Laothian
1747 ("lt" . "Latin-4") ; Lithuanian
1748 ("lv" . "Latin-4") ; Latvian, Lettish
1749 ; mg Malagasy
5f395df3 1750 ("mi" . "Latin-7") ; Maori
6ececc4d 1751 ("mk" . "Latin-5") ; Macedonian
40c81f74
PE
1752 ; ml Malayalam
1753 ; mn Mongolian
1754 ; mo Moldavian
5f395df3
SM
1755 ("mr" . "Devanagari") ; Marathi glibc uses utf-8
1756 ("ms" . "Latin-1") ; Malay
40c81f74
PE
1757 ("mt" . "Latin-3") ; Maltese
1758 ; my Burmese
1759 ; na Nauru
1760 ("ne" . "Devanagari") ; Nepali
8c4b6822 1761 ("nl" . "Dutch")
40c81f74 1762 ("no" . "Latin-1") ; Norwegian
5f395df3 1763 ("oc" . "Latin-1") ; Occitan
40c81f74
PE
1764 ; om (Afan) Oromo
1765 ; or Oriya
1766 ; pa Punjabi
1767 ("pl" . "Latin-2") ; Polish
1768 ; ps Pashto, Pushto
1769 ("pt" . "Latin-1") ; Portuguese
1770 ; qu Quechua
6ececc4d 1771 ("rm" . "Latin-1") ; Rhaeto-Romanic
40c81f74
PE
1772 ; rn Kirundi
1773 ("ro" . "Romanian")
1774 ("ru.*[_.]koi8" . "Cyrillic-KOI8") ; Russian
6ececc4d 1775 ("ru" . "Latin-5") ; Russian
40c81f74
PE
1776 ; rw Kinyarwanda
1777 ("sa" . "Devanagari") ; Sanskrit
1778 ; sd Sindhi
5f395df3 1779 ; se Northern Sami
40c81f74
PE
1780 ; sg Sangho
1781 ("sh" . "Latin-2") ; Serbo-Croatian
1782 ; si Sinhalese
1783 ("sk" . "Slovak")
1784 ("sl" . "Slovenian")
1785 ; sm Samoan
1786 ; sn Shona
1787 ; so Somali
6ececc4d 1788 ("sq" . "Latin-1") ; Albanian
40c81f74
PE
1789 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
1790 ; ss Siswati
1791 ; st Sesotho
1792 ; su Sundanese
1793 ("sv" . "Latin-1") ; Swedish
1794 ("sw" . "Latin-1") ; Swahili
5f395df3
SM
1795 ; ta Tamil glibc uses utf-8
1796 ; te Telugu glibc uses utf-8
1797 ; tg Tajik "Cyrillic-KOI8-T"
40c81f74
PE
1798 ("th" . "Thai")
1799 ; ti Tigrinya
1800 ; tk Turkmen
6ececc4d 1801 ("tl" . "Latin-1") ; Tagalog
40c81f74
PE
1802 ; tn Setswana
1803 ; to Tonga
1804 ("tr" . "Latin-5") ; Turkish
1805 ; ts Tsonga
1806 ; tt Tatar
1807 ; tw Twi
1808 ; ug Uighur
6ececc4d 1809 ("uk" . "Latin-5") ; Ukrainian
5f395df3
SM
1810 ; ur Urdu glibc uses utf-8
1811 ("uz" . "Latin-1") ; Uzbek
1812 ("vi" . "Vietnamese") ; glibc uses utf-8
40c81f74
PE
1813 ; vo Volapuk
1814 ; wo Wolof
1815 ; xh Xhosa
1816 ; yi Yiddish
1817 ; yo Yoruba
1818 ; za Zhuang
5f395df3
SM
1819
1820 ; glibc:
1821 ; zh_CN.GB18030/GB18030 \
1822 ; zh_CN.GBK/GBK \
1823 ; zh_HK/BIG5-HKSCS \
1824 ; zh_TW/BIG5 \
1825 ; zh_TW.EUC-TW/EUC-TW \
1826
40c81f74
PE
1827 ("zh.*[._]big5" . "Chinese-BIG5")
1828 ("zh.*[._]gbk" . nil) ; Solaris 2.7; has gbk-0 as well as GB 2312.1980-0
1829 ("zh_tw" . "Chinese-CNS")
1830 ("zh" . "Chinese-GB")
1831 ; zu Zulu
1832
1833 ;; ISO standard locales
1834 ("c$" . "ASCII")
1835 ("posix$" . "ASCII")
1836
40c81f74
PE
1837 ;; The "IPA" Emacs language environment does not correspond
1838 ;; to any ISO 639 code, so let it stand for itself.
1839 ("ipa$" . "IPA")
1840
1841 ;; Nonstandard or obsolete language codes
1842 ("cz" . "Czech") ; e.g. Solaris 2.6
1843 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
1844 ("iw" . "Hebrew") ; e.g. X11R6.4
6ececc4d 1845 ("sp" . "Latin-5") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
40c81f74 1846 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2e86ceaa 1847 ("jp" . "Japanese") ; e.g. MS Windows
86d9e628
JR
1848 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
1849 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
367ca50f 1850 ))
40c81f74 1851 "List of pairs of locale regexps and language names.
6ececc4d
PE
1852The first element whose locale regexp matches the start of a downcased locale
1853specifies the language name corresponding to that locale.
40c81f74
PE
1854If the language name is nil, there is no corresponding language environment.")
1855
5f395df3
SM
1856(defconst locale-charset-language-names
1857 (purecopy
1858 '((".*8859[-_]?1\\>" . "Latin-1")
1859 (".*8859[-_]?2\\>" . "Latin-2")
1860 (".*8859[-_]?3\\>" . "Latin-3")
1861 (".*8859[-_]?4\\>" . "Latin-4")
1862 (".*8859[-_]?9\\>" . "Latin-5")
1863 (".*8859[-_]?14\\>" . "Latin-8")
1864 (".*8859[-_]?15\\>" . "Latin-9")
1865 (".*@euro\\>" . "Latin-9")
1866 ))
6ececc4d
PE
1867 "List of pairs of locale regexps and charset language names.
1868The first element whose locale regexp matches the start of a downcased locale
1869specifies the language name whose charsets corresponds to that locale.
1870This language name is used if its charsets disagree with the charsets of
1871the language name that would otherwise be used for this locale.")
1872
5f395df3
SM
1873(defconst locale-preferred-coding-systems
1874 (purecopy
1875 '(("ja.*[._]euc" . japanese-iso-8bit)
1876 ("ja.*[._]jis7" . iso-2022-jp)
1877 ("ja.*[._]pck" . japanese-shift-jis)
1878 ("ja.*[._]sjis" . japanese-shift-jis)
1879 ))
6ececc4d
PE
1880 "List of pairs of locale regexps and preferred coding systems.
1881The first element whose locale regexp matches the start of a downcased locale
1882specifies the coding system to prefer when using that locale.")
40c81f74 1883
ddf6b836
SM
1884(defconst standard-keyboard-coding-systems
1885 (purecopy
1886 '(iso-latin-1 iso-latin-2 iso-latin-3 iso-latin-4 iso-latin-5
1887 iso-latin-6 iso-latin-7 iso-latin-8 iso-latin-9))
1888 "Coding systems that are commonly used for keyboards.
1889`set-locale-environment' will set the `keyboard-coding-system' if the
1890coding-system specified by the locale setting is a member of this list.")
1891
40c81f74
PE
1892(defun locale-name-match (key alist)
1893 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
1894Return the value corresponding to the first regexp that matches the
1895start of KEY, or nil if there is no match."
1896 (let (element)
1897 (while (and alist (not element))
ddf6b836 1898 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
40c81f74
PE
1899 (setq element (car alist)))
1900 (setq alist (cdr alist)))
1901 (cdr element)))
1902
758f07de 1903(defun set-locale-environment (&optional locale-name)
40c81f74 1904 "Set up multi-lingual environment for using LOCALE-NAME.
758f07de
RS
1905This sets the language environment, the coding system priority,
1906the default input method and sometimes other things.
1907
1908LOCALE-NAME should be a string
40c81f74
PE
1909which is the name of a locale supported by the system;
1910often it is of the form xx_XX.CODE, where xx is a language,
1911XX is a country, and CODE specifies a character set and coding system.
1912For example, the locale name \"ja_JP.EUC\" might name a locale
1913for Japanese in Japan using the `japanese-iso-8bit' coding-system.
1914
758f07de
RS
1915If LOCALE-NAME is nil, its value is taken from the environment
1916variables LC_ALL, LC_CTYLE and LANG (the first one that is set).
40c81f74
PE
1917
1918The locale names supported by your system can typically be found in a
0812c1e8 1919directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
758f07de 1920will be translated according to the table specified by
0812c1e8
DL
1921`locale-translation-file-name'.
1922
1923See also `locale-charset-language-names', `locale-language-names',
1924`locale-preferred-coding-systems' and `locale-coding-system'."
758f07de
RS
1925 (interactive "sSet environment for locale: ")
1926
0d7c5bb9
DL
1927 ;; Do this at runtime for the sake of binaries possibly transported
1928 ;; to a system without X.
1929 (setq locale-translation-file-name
1930 (let ((files
1931 '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
1932 "/usr/X11R6/lib/X11/locale/locale.alias" ; e.g. RedHat 4.2
1933 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
1934 ;;
1935 ;; The following name appears after the X-related names above,
1936 ;; since the X-related names are what X actually uses.
1937 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
1938 )))
1939 (while (and files (not (file-exists-p (car files))))
1940 (setq files (cdr files)))
1941 (car files)))
1942
7008ccac
GM
1943 (let ((locale locale-name))
1944
1945 (unless locale
1946 ;; Use the first of these three environment variables
1947 ;; that has a nonempty value.
1948 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
1949 (while (and vars (not (setq locale (getenv (car vars)))))
1950 (setq vars (cdr vars)))))
1951
1952 (when locale
1953
1954 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
1955 ;; using the translation file that many systems have.
1956 (when locale-translation-file-name
1957 (with-temp-buffer
1958 (insert-file-contents locale-translation-file-name)
1959 (when (re-search-forward
1960 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
1961 (setq locale (buffer-substring (point) (line-end-position))))))
1962
1963 ;; Leave the system locales alone if the caller did not specify
1964 ;; an explicit locale name, as their defaults are set from
1965 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
1966 ;; want to set them to the same value as LC_CTYPE.
1967 (when locale-name
1968 (setq system-messages-locale locale)
1969 (setq system-time-locale locale))
1970
1971 (setq locale (downcase locale))
1972
1973 (let ((language-name
1974 (locale-name-match locale locale-language-names))
1975 (charset-language-name
1976 (locale-name-match locale locale-charset-language-names))
1977 (coding-system
1978 (locale-name-match locale locale-preferred-coding-systems)))
1979
5f395df3 1980 ;; Give preference to charset-language-name over language-name.
7008ccac
GM
1981 (if (and charset-language-name
1982 (not
1983 (equal (get-language-info language-name 'charset)
1984 (get-language-info charset-language-name 'charset))))
1985 (setq language-name charset-language-name))
1986
1987 (when language-name
1988
1989 ;; Set up for this character set. This is now the right way
1990 ;; to do it for both unibyte and multibyte modes.
1991 (set-language-environment language-name)
1992
1993 ;; If default-enable-multibyte-characters is nil,
1994 ;; we are using single-byte characters,
1995 ;; so the display table and terminal coding system are irrelevant.
1996 (when default-enable-multibyte-characters
1997 (set-display-table-and-terminal-coding-system language-name))
1998
ddf6b836
SM
1999 ;; Set the `keyboard-coding-system' if appropriate.
2000 (let ((kcs (or coding-system
2001 (car (get-language-info language-name
2002 'coding-system)))))
2003 (if (memq kcs standard-keyboard-coding-systems)
2004 (set-keyboard-coding-system kcs)))
2005
7008ccac
GM
2006 (setq locale-coding-system
2007 (car (get-language-info language-name 'coding-priority))))
2008
2009 (when coding-system
2010 (prefer-coding-system coding-system)
2011 (setq locale-coding-system coding-system))))))
40c81f74 2012\f
4ed46869
KH
2013;;; Charset property
2014
251d4f4b 2015(defun get-charset-property (charset propname)
4ed46869
KH
2016 "Return the value of CHARSET's PROPNAME property.
2017This is the last value stored with
96db204a 2018 (put-charset-property CHARSET PROPNAME VALUE)."
251d4f4b
KH
2019 (and (not (eq charset 'composition))
2020 (plist-get (charset-plist charset) propname)))
4ed46869 2021
251d4f4b 2022(defun put-charset-property (charset propname value)
4ed46869
KH
2023 "Store CHARSETS's PROPNAME property with value VALUE.
2024It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1300d43f
KH
2025 (or (eq charset 'composition)
2026 (set-charset-plist charset
2027 (plist-put (charset-plist charset) propname value))))
4ed46869
KH
2028
2029;;; Character code property
2030(put 'char-code-property-table 'char-table-extra-slots 0)
2031
2032(defvar char-code-property-table
2033 (make-char-table 'char-code-property-table)
2034 "Char-table containing a property list of each character code.
2035
2036See also the documentation of `get-char-code-property' and
96db204a 2037`put-char-code-property'.")
4ed46869
KH
2038
2039(defun get-char-code-property (char propname)
2040 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2041 (let ((plist (aref char-code-property-table char)))
2042 (if (listp plist)
2043 (car (cdr (memq propname plist))))))
2044
2045(defun put-char-code-property (char propname value)
2046 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2047It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2048 (let ((plist (aref char-code-property-table char)))
2049 (if plist
2050 (let ((slot (memq propname plist)))
2051 (if slot
2052 (setcar (cdr slot) value)
2053 (nconc plist (list propname value))))
2054 (aset char-code-property-table char (list propname value)))))
2055
a127b764
KH
2056\f
2057;; Pretty description of encoded string
2058
2059;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2060(defvar iso-2022-control-alist
2061 '((?\x1b . "ESC")
2062 (?\x0e . "SO")
2063 (?\x0f . "SI")
2064 (?\x8e . "SS2")
2065 (?\x8f . "SS3")
2066 (?\x9b . "CSI")))
2067
2068(defun encoded-string-description (str coding-system)
2069 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2070 (setq str (string-as-unibyte str))
993b2a7d 2071 (mapconcat
8c9d55a9 2072 (if (and coding-system (eq (coding-system-type coding-system) 2))
993b2a7d
KH
2073 ;; Try to get a pretty description for ISO 2022 escape sequences.
2074 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2075 (format "%02X" x))))
5de75f53 2076 (function (lambda (x) (format "0x%02X" x))))
993b2a7d 2077 str " "))
a127b764
KH
2078
2079(defun encode-coding-char (char coding-system)
2080 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2081If CODING-SYSTEM can't safely encode CHAR, return nil."
bd953173
KH
2082 (let ((str1 (string-as-multibyte (char-to-string char)))
2083 (str2 (string-as-multibyte (make-string 2 char)))
a89f541b
KH
2084 (safe-chars (and coding-system
2085 (coding-system-get coding-system 'safe-chars)))
8c9d55a9 2086 (charset (char-charset char))
a127b764 2087 enc1 enc2 i1 i2)
a89f541b 2088 (when (or (eq safe-chars t)
8c9d55a9 2089 (eq charset 'ascii)
a89f541b 2090 (and safe-chars (aref safe-chars char)))
a127b764
KH
2091 ;; We must find the encoded string of CHAR. But, just encoding
2092 ;; CHAR will put extra control sequences (usually to designate
2093 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
2094 ;; To exclude such tailing bytes, we at first encode one-char
2095 ;; string and two-char string, then check how many bytes at the
2096 ;; tail of both encoded strings are the same.
2097
bd953173 2098 (setq enc1 (encode-coding-string str1 coding-system)
a127b764 2099 i1 (length enc1)
bd953173 2100 enc2 (encode-coding-string str2 coding-system)
a127b764
KH
2101 i2 (length enc2))
2102 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2103 (setq i1 (1- i1) i2 (1- i2)))
2104
2105 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2106 ;; and they are the extra control sequences at the tail to
2107 ;; exclude.
2108 (substring enc2 0 i2))))
2109
2110
4ed46869 2111;;; mule-cmds.el ends here