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