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