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