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