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