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