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