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