]> code.delx.au - gnu-emacs/blob - lisp/international/mule-cmds.el
(register-input-method): Doc fix.
[gnu-emacs] / lisp / international / mule-cmds.el
1 ;;; mule-cmds.el --- commands for mulitilingual environment -*-coding: iso-2022-7bit -*-
2
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
8
9 ;; Keywords: mule, multilingual
10
11 ;; This file is part of GNU Emacs.
12
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
17
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
22
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
27
28 ;;; Commentary:
29
30 ;;; Code:
31
32 (eval-when-compile
33 (defvar dos-codepage)
34 (autoload 'widget-value "wid-edit"))
35
36 (defvar mac-system-coding-system)
37 (defvar mac-system-locale)
38
39 ;;; MULE related key bindings and menus.
40
41 (defvar mule-keymap (make-sparse-keymap)
42 "Keymap for Mule (Multilingual environment) specific commands.")
43
44 ;; Keep "C-x C-m ..." for mule specific commands.
45 (define-key ctl-x-map "\C-m" mule-keymap)
46
47 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
48 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
49 (define-key mule-keymap "F" 'set-file-name-coding-system)
50 (define-key mule-keymap "t" 'set-terminal-coding-system)
51 (define-key mule-keymap "k" 'set-keyboard-coding-system)
52 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
53 (define-key mule-keymap "x" 'set-selection-coding-system)
54 (define-key mule-keymap "X" 'set-next-selection-coding-system)
55 (define-key mule-keymap "\C-\\" 'set-input-method)
56 (define-key mule-keymap "c" 'universal-coding-system-argument)
57 (define-key mule-keymap "l" 'set-language-environment)
58
59 (defvar mule-menu-keymap
60 (make-sparse-keymap "Mule (Multilingual Environment)")
61 "Keymap for Mule (Multilingual environment) menu specific commands.")
62
63 (defvar describe-language-environment-map
64 (make-sparse-keymap "Describe Language Environment"))
65
66 (defvar setup-language-environment-map
67 (make-sparse-keymap "Set Language Environment"))
68
69 (defvar set-coding-system-map
70 (make-sparse-keymap "Set Coding System"))
71
72 (define-key-after mule-menu-keymap [set-language-environment]
73 (list 'menu-item "Set Language Environment" setup-language-environment-map))
74 (define-key-after mule-menu-keymap [separator-mule]
75 '("--")
76 t)
77 (define-key-after mule-menu-keymap [toggle-input-method]
78 '(menu-item "Toggle Input Method" toggle-input-method)
79 t)
80 (define-key-after mule-menu-keymap [set-input-method]
81 '(menu-item "Select Input Method..." set-input-method)
82 t)
83 (define-key-after mule-menu-keymap [describe-input-method]
84 '(menu-item "Describe Input Method" describe-input-method))
85 (define-key-after mule-menu-keymap [separator-input-method]
86 '("--")
87 t)
88 (define-key-after mule-menu-keymap [set-various-coding-system]
89 (list 'menu-item "Set Coding Systems" set-coding-system-map
90 :enable 'default-enable-multibyte-characters))
91 (define-key-after mule-menu-keymap [view-hello-file]
92 '(menu-item "Show Multi-lingual Text" view-hello-file
93 :enable (file-readable-p
94 (expand-file-name "HELLO" data-directory))
95 :help "Display file which says HELLO in many languages")
96 t)
97 (define-key-after mule-menu-keymap [separator-coding-system]
98 '("--")
99 t)
100 (define-key-after mule-menu-keymap [describe-language-environment]
101 (list 'menu-item "Describe Language Environment"
102 describe-language-environment-map
103 :help "Show multilingual settings for a specific language")
104 t)
105 (define-key-after mule-menu-keymap [describe-input-method]
106 '(menu-item "Describe Input Method..." describe-input-method
107 :help "Keyboard layout for a specific input method")
108 t)
109 (define-key-after mule-menu-keymap [describe-coding-system]
110 '(menu-item "Describe Coding System..." describe-coding-system)
111 t)
112 (define-key-after mule-menu-keymap [list-character-sets]
113 '(menu-item "List Character Sets" list-character-sets
114 :help "Show table of available character sets"))
115 (define-key-after mule-menu-keymap [mule-diag]
116 '(menu-item "Show All of Mule Status" mule-diag
117 :help "Display multilingual environment settings")
118 t)
119
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 [separator-1]
125 '("--")
126 t)
127 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
128 '(menu-item "For Saving This Buffer" set-buffer-file-coding-system
129 :help "How to encode this buffer when saved")
130 t)
131 (define-key-after set-coding-system-map [revert-buffer-with-coding-system]
132 '(menu-item "For Reverting This File Now" revert-buffer-with-coding-system
133 :enable buffer-file-name
134 :help "Revisit this file immediately using specified coding system")
135 t)
136 (define-key-after set-coding-system-map [set-file-name-coding-system]
137 '(menu-item "For File Name" set-file-name-coding-system
138 :help "How to decode/encode file names")
139 t)
140 (define-key-after set-coding-system-map [separator-2]
141 '("--")
142 t)
143
144 (define-key-after set-coding-system-map [set-keyboard-coding-system]
145 '(menu-item "For Keyboard" set-keyboard-coding-system
146 :help "How to decode keyboard input")
147 t)
148 (define-key-after set-coding-system-map [set-terminal-coding-system]
149 '(menu-item "For Terminal" set-terminal-coding-system
150 :enable (null (memq window-system '(x w32 mac)))
151 :help "How to encode terminal output")
152 t)
153 (define-key-after set-coding-system-map [separator-3]
154 '("--")
155 t)
156 (define-key-after set-coding-system-map [set-selection-coding-system]
157 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
158 :visible (display-selections-p)
159 :help "How to en/decode data to/from selection/clipboard")
160 t)
161 (define-key-after set-coding-system-map [set-next-selection-coding-system]
162 '(menu-item "For Next X Selection" set-next-selection-coding-system
163 :visible (display-selections-p)
164 :help "How to en/decode next selection/clipboard operation")
165 t)
166 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
167 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
168 :visible (fboundp 'start-process)
169 :enable (get-buffer-process (current-buffer))
170 :help "How to en/decode I/O from/to subprocess connected to this buffer")
171 t)
172
173
174 (define-key setup-language-environment-map
175 [Default] '(menu-item "Default" setup-specified-language-environment))
176
177 (define-key describe-language-environment-map
178 [Default] '(menu-item "Default" describe-specified-language-support))
179
180 ;; This should be a single character key binding because users use it
181 ;; very frequently while editing multilingual text. Now we can use
182 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
183 ;; convenient because it requires shifting on most keyboards. An
184 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
185 ;; but it won't be used that frequently.
186 (define-key global-map "\C-\\" 'toggle-input-method)
187
188 ;; This is no good because people often type Shift-SPC
189 ;; meaning to type SPC. -- rms.
190 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
191 ;; (define-key global-map [?\S- ] 'toggle-input-method)
192
193 ;;; Mule related hyperlinks.
194 (defconst help-xref-mule-regexp-template
195 (purecopy (concat "\\(\\<\\("
196 "\\(coding system\\)\\|"
197 "\\(input method\\)\\|"
198 "\\(character set\\)\\|"
199 "\\(charset\\)"
200 "\\)\\s-+\\)?"
201 ;; Note starting with word-syntax character:
202 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
203
204 (defun coding-system-change-eol-conversion (coding-system eol-type)
205 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
206 The returned coding system converts end-of-line by EOL-TYPE
207 but text as the same way as CODING-SYSTEM.
208 EOL-TYPE should be `unix', `dos', `mac', or nil.
209 If EOL-TYPE is nil, the returned coding system detects
210 how end-of-line is formatted automatically while decoding.
211
212 EOL-TYPE can be specified by an integer 0, 1, or 2.
213 They means `unix', `dos', and `mac' respectively."
214 (if (symbolp eol-type)
215 (setq eol-type (cond ((eq eol-type 'unix) 0)
216 ((eq eol-type 'dos) 1)
217 ((eq eol-type 'mac) 2)
218 (t eol-type))))
219 ;; We call `coding-system-base' before `coding-system-eol-type',
220 ;; because the coding-system may not be initialized until then.
221 (let* ((base (coding-system-base coding-system))
222 (orig-eol-type (coding-system-eol-type coding-system)))
223 (cond ((vectorp orig-eol-type)
224 (if (not eol-type)
225 coding-system
226 (aref orig-eol-type eol-type)))
227 ((not eol-type)
228 base)
229 ((= eol-type orig-eol-type)
230 coding-system)
231 ((progn (setq orig-eol-type (coding-system-eol-type base))
232 (vectorp orig-eol-type))
233 (aref orig-eol-type eol-type)))))
234
235 (defun coding-system-change-text-conversion (coding-system coding)
236 "Return a coding system which differs from CODING-SYSTEM in text conversion.
237 The returned coding system converts text by CODING
238 but end-of-line as the same way as CODING-SYSTEM.
239 If CODING is nil, the returned coding system detects
240 how text is formatted automatically while decoding."
241 (let ((eol-type (coding-system-eol-type coding-system)))
242 (coding-system-change-eol-conversion
243 (if coding coding 'undecided)
244 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
245
246 (defun toggle-enable-multibyte-characters (&optional arg)
247 "Change whether this buffer uses multibyte characters.
248 With arg, use multibyte characters if the arg is positive.
249
250 Note that this command does not convert the byte contents of
251 the buffer; it only changes the way those bytes are interpreted.
252 In general, therefore, this command *changes* the sequence of
253 characters that the current buffer contains.
254
255 We suggest you avoid using use this command unless you know what you
256 are doing. If you use it by mistake, and the buffer is now displayed
257 wrong, use this command again to toggle back to the right mode."
258 (interactive "P")
259 (let ((new-flag
260 (if (null arg) (null enable-multibyte-characters)
261 (> (prefix-numeric-value arg) 0))))
262 (set-buffer-multibyte new-flag))
263 (force-mode-line-update))
264
265 (defun view-hello-file ()
266 "Display the HELLO file which list up many languages and characters."
267 (interactive)
268 ;; We have to decode the file in any environment.
269 (let ((default-enable-multibyte-characters t)
270 (coding-system-for-read 'iso-2022-7bit))
271 (view-file (expand-file-name "HELLO" data-directory))))
272
273 (defun universal-coding-system-argument (coding-system)
274 "Execute an I/O command using the specified coding system."
275 (interactive
276 (let ((default (and buffer-file-coding-system
277 (not (eq (coding-system-type buffer-file-coding-system)
278 t))
279 buffer-file-coding-system)))
280 (list (read-coding-system
281 (if default
282 (format "Coding system for following command (default %s): " default)
283 "Coding system for following command: ")
284 default))))
285 (let* ((keyseq (read-key-sequence
286 (format "Command to execute with %s:" coding-system)))
287 (cmd (key-binding keyseq))
288 prefix)
289
290 (when (eq cmd 'universal-argument)
291 (call-interactively cmd)
292
293 ;; Process keys bound in `universal-argument-map'.
294 (while (progn
295 (setq keyseq (read-key-sequence nil t)
296 cmd (key-binding keyseq t))
297 (not (eq cmd 'universal-argument-other-key)))
298 (let ((current-prefix-arg prefix-arg)
299 ;; Have to bind `last-command-char' here so that
300 ;; `digit-argument', for instance, can compute the
301 ;; prefix arg.
302 (last-command-char (aref keyseq 0)))
303 (call-interactively cmd)))
304
305 ;; This is the final call to `universal-argument-other-key', which
306 ;; set's the final `prefix-arg.
307 (let ((current-prefix-arg prefix-arg))
308 (call-interactively cmd))
309
310 ;; Read the command to execute with the given prefix arg.
311 (setq prefix prefix-arg
312 keyseq (read-key-sequence nil t)
313 cmd (key-binding keyseq)))
314
315 (let ((coding-system-for-read coding-system)
316 (coding-system-for-write coding-system)
317 (coding-system-require-warning t)
318 (current-prefix-arg prefix))
319 (message "")
320 (call-interactively cmd))))
321
322 (defun set-default-coding-systems (coding-system)
323 "Set default value of various coding systems to CODING-SYSTEM.
324 This sets the following coding systems:
325 o coding system of a newly created buffer
326 o default coding system for subprocess I/O
327 This also sets the following values:
328 o default value used as `file-name-coding-system' for converting file names
329 if CODING-SYSTEM is ASCII-compatible.
330 o default value for the command `set-terminal-coding-system' (not on MSDOS)
331 o default value for the command `set-keyboard-coding-system'
332 if CODING-SYSTEM is ASCII-compatible.."
333 (check-coding-system coding-system)
334 (setq-default buffer-file-coding-system coding-system)
335 (if (fboundp 'ucs-set-table-for-input)
336 (dolist (buffer (buffer-list))
337 (or (local-variable-p 'buffer-file-coding-system buffer)
338 (ucs-set-table-for-input buffer))))
339
340 (if (eq system-type 'darwin)
341 ;; The file-name coding system on Darwin systems is always utf-8.
342 (setq default-file-name-coding-system 'utf-8)
343 (if (and default-enable-multibyte-characters
344 (or (not coding-system)
345 (not (coding-system-get coding-system 'ascii-incompatible))))
346 (setq default-file-name-coding-system coding-system)))
347 ;; If coding-system is nil, honor that on MS-DOS as well, so
348 ;; that they could reset the terminal coding system.
349 (unless (and (eq window-system 'pc) coding-system)
350 (setq default-terminal-coding-system coding-system))
351 (if (or (not coding-system)
352 (not (coding-system-get coding-system 'ascii-incompatible)))
353 (setq default-keyboard-coding-system coding-system))
354 ;; Preserve eol-type from existing default-process-coding-systems.
355 ;; On non-unix-like systems in particular, these may have been set
356 ;; carefully by the user, or by the startup code, to deal with the
357 ;; users shell appropriately, so should not be altered by changing
358 ;; language environment.
359 (let ((output-coding
360 (coding-system-change-text-conversion
361 (car default-process-coding-system) coding-system))
362 (input-coding
363 (coding-system-change-text-conversion
364 (cdr default-process-coding-system) coding-system)))
365 (setq default-process-coding-system
366 (cons output-coding input-coding))))
367
368 (defun prefer-coding-system (coding-system)
369 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
370 This also sets the following coding systems:
371 o coding system of a newly created buffer
372 o default coding system for subprocess I/O
373 This also sets the following values:
374 o default value used as `file-name-coding-system' for converting file names.
375 o default value for the command `set-terminal-coding-system' (not on MSDOS)
376 o default value for the command `set-keyboard-coding-system'
377
378 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
379 systems set by this function will use that type of EOL conversion.
380
381 This command does not change the default value of terminal coding system
382 for MS-DOS terminal, because DOS terminals only support a single coding
383 system, and Emacs automatically sets the default to that coding system at
384 startup.
385
386 A coding system that requires automatic detection of text
387 encoding (e.g. undecided, unix) can't be preferred.
388
389 See also `coding-category-list' and `coding-system-category'."
390 (interactive "zPrefer coding system: ")
391 (if (not (and coding-system (coding-system-p coding-system)))
392 (error "Invalid coding system `%s'" coding-system))
393 (let ((coding-category (coding-system-category coding-system))
394 (base (coding-system-base coding-system))
395 (eol-type (coding-system-eol-type coding-system)))
396 (if (not coding-category)
397 ;; CODING-SYSTEM is no-conversion or undecided.
398 (error "Can't prefer the coding system `%s'" coding-system))
399 (set coding-category (or base coding-system))
400 ;; Changing the binding of a coding category requires this call.
401 (update-coding-systems-internal)
402 (or (eq coding-category (car coding-category-list))
403 ;; We must change the order.
404 (set-coding-priority (list coding-category)))
405 (if (and base (interactive-p))
406 (message "Highest priority is set to %s (base of %s)"
407 base coding-system))
408 ;; If they asked for specific EOL conversion, honor that.
409 (if (memq eol-type '(0 1 2))
410 (setq coding-system
411 (coding-system-change-eol-conversion base eol-type))
412 (setq coding-system base))
413 (set-default-coding-systems coding-system)))
414
415 (defvar sort-coding-systems-predicate nil
416 "If non-nil, a predicate function to sort coding systems.
417
418 It is called with two coding systems, and should return t if the first
419 one is \"less\" than the second.
420
421 The function `sort-coding-systems' use it.")
422
423 (defun sort-coding-systems (codings)
424 "Sort coding system list CODINGS by a priority of each coding system.
425 Returns the sorted list. CODINGS is modified by side effects.
426
427 If a coding system is most preferred, it has the highest priority.
428 Otherwise, a coding system corresponds to some MIME charset has higher
429 priorities. Among them, a coding system included in `coding-system'
430 key of the current language environment has higher priorities. See
431 also the documentation of `language-info-alist'.
432
433 If the variable `sort-coding-systems-predicate' (which see) is
434 non-nil, it is used to sort CODINGS in the different way than above."
435 (if sort-coding-systems-predicate
436 (sort codings sort-coding-systems-predicate)
437 (let* ((from-categories (mapcar #'(lambda (x) (symbol-value x))
438 coding-category-list))
439 (most-preferred (car from-categories))
440 (lang-preferred (get-language-info current-language-environment
441 'coding-system))
442 (func (function
443 (lambda (x)
444 (let ((base (coding-system-base x)))
445 ;; We calculate the priority number 0..255 by
446 ;; using the 8 bits PMMLCEII as this:
447 ;; P: 1 iff most preferred.
448 ;; MM: greater than 0 iff mime-charset.
449 ;; L: 1 iff one of the current lang. env.'s codings.
450 ;; C: 1 iff one of codings listed in the category list.
451 ;; E: 1 iff not XXX-with-esc
452 ;; II: if iso-2022 based, 0..3, else 1.
453 (logior
454 (lsh (if (eq base most-preferred) 1 0) 7)
455 (lsh
456 (let ((mime (coding-system-get base 'mime-charset)))
457 ;; Prefer coding systems corresponding to a
458 ;; MIME charset.
459 (if mime
460 ;; Lower utf-16 priority so that we
461 ;; normally prefer utf-8 to it, and put
462 ;; x-ctext below that.
463 (cond ((string-match "utf-16"
464 (symbol-name mime))
465 2)
466 ((string-match "^x-" (symbol-name mime))
467 1)
468 (t 3))
469 0))
470 5)
471 (lsh (if (memq base lang-preferred) 1 0) 4)
472 (lsh (if (memq base from-categories) 1 0) 3)
473 (lsh (if (string-match "-with-esc\\'"
474 (symbol-name base))
475 0 1) 2)
476 (if (eq (coding-system-type base) 2)
477 ;; For ISO based coding systems, prefer
478 ;; one that doesn't use escape sequences.
479 (let ((flags (coding-system-flags base)))
480 (if (or (consp (aref flags 0))
481 (consp (aref flags 1))
482 (consp (aref flags 2))
483 (consp (aref flags 3)))
484 (if (or (aref flags 8) (aref flags 9))
485 0
486 1)
487 2))
488 1)))))))
489 (sort codings (function (lambda (x y)
490 (> (funcall func x) (funcall func y))))))))
491
492 (defun find-coding-systems-region (from to)
493 "Return a list of proper coding systems to encode a text between FROM and TO.
494 If FROM is a string, find coding systems in that instead of the buffer.
495 All coding systems in the list can safely encode any multibyte characters
496 in the text.
497
498 If the text contains no multibyte characters, return a list of a single
499 element `undecided'."
500 (let ((codings (find-coding-systems-region-internal from to)))
501 (if (eq codings t)
502 ;; The text contains only ASCII characters. Any coding
503 ;; systems are safe.
504 '(undecided)
505 ;; We need copy-sequence because sorting will alter the argument.
506 (sort-coding-systems (copy-sequence codings)))))
507
508 (defun find-coding-systems-string (string)
509 "Return a list of proper coding systems to encode STRING.
510 All coding systems in the list can safely encode any multibyte characters
511 in STRING.
512
513 If STRING contains no multibyte characters, return a list of a single
514 element `undecided'."
515 (find-coding-systems-region string nil))
516
517 (defun find-coding-systems-for-charsets (charsets)
518 "Return a list of proper coding systems to encode characters of CHARSETS.
519 CHARSETS is a list of character sets.
520 It actually checks at most the first 96 characters of each charset.
521 So, if a charset of dimension two is included in CHARSETS, the value may
522 contain a coding system that can't encode all characters of the charset."
523 (cond ((or (null charsets)
524 (and (= (length charsets) 1)
525 (eq 'ascii (car charsets))))
526 '(undecided))
527 ((or (memq 'eight-bit-control charsets)
528 (memq 'eight-bit-graphic charsets))
529 '(raw-text emacs-mule))
530 (t
531 (let ((codings t)
532 charset l str)
533 (while (and codings charsets)
534 (setq charset (car charsets) charsets (cdr charsets))
535 (unless (eq charset 'ascii)
536 (setq str (make-string 96 32))
537 (if (= (charset-dimension charset) 1)
538 (if (= (charset-chars charset) 96)
539 (dotimes (i 96)
540 (aset str i (make-char charset (+ i 32))))
541 (dotimes (i 94)
542 (aset str i (make-char charset (+ i 33)))))
543 (if (= (charset-chars charset) 96)
544 (dotimes (i 96)
545 (aset str i (make-char charset 32 (+ i 32))))
546 (dotimes (i 94)
547 (aset str i (make-char charset 33 (+ i 33))))))
548 (setq l (find-coding-systems-string str))
549 (if (eq codings t)
550 (setq codings l)
551 (let ((ll nil))
552 (dolist (elt codings)
553 (if (memq elt l)
554 (setq ll (cons elt ll))))
555 (setq codings ll)))))
556 codings))))
557
558 (defun find-multibyte-characters (from to &optional maxcount excludes)
559 "Find multibyte characters in the region specified by FROM and TO.
560 If FROM is a string, find multibyte characters in the string.
561 The return value is an alist of the following format:
562 ((CHARSET COUNT CHAR ...) ...)
563 where
564 CHARSET is a character set,
565 COUNT is a number of characters,
566 CHARs are the characters found from the character set.
567 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
568 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
569
570 For invalid characters, CHARs are actually strings."
571 (let ((chars nil)
572 charset char)
573 (if (stringp from)
574 (let ((idx 0))
575 (while (setq idx (string-match "[^\000-\177]" from idx))
576 (setq char (aref from idx)
577 charset (char-charset char))
578 (if (eq charset 'unknown)
579 (setq char (match-string 0)))
580 (if (or (memq charset '(unknown
581 eight-bit-control eight-bit-graphic))
582 (not (or (eq excludes t) (memq charset excludes))))
583 (let ((slot (assq charset chars)))
584 (if slot
585 (if (not (memq char (nthcdr 2 slot)))
586 (let ((count (nth 1 slot)))
587 (setcar (cdr slot) (1+ count))
588 (if (or (not maxcount) (< count maxcount))
589 (nconc slot (list char)))))
590 (setq chars (cons (list charset 1 char) chars)))))
591 (setq idx (1+ idx))))
592 (save-excursion
593 (goto-char from)
594 (while (re-search-forward "[^\000-\177]" to t)
595 (setq char (preceding-char)
596 charset (char-charset char))
597 (if (eq charset 'unknown)
598 (setq char (match-string 0)))
599 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
600 (not (or (eq excludes t) (memq charset excludes))))
601 (let ((slot (assq charset chars)))
602 (if slot
603 (if (not (member char (nthcdr 2 slot)))
604 (let ((count (nth 1 slot)))
605 (setcar (cdr slot) (1+ count))
606 (if (or (not maxcount) (< count maxcount))
607 (nconc slot (list char)))))
608 (setq chars (cons (list charset 1 char) chars))))))))
609 (nreverse chars)))
610
611
612 (defun search-unencodable-char (coding-system)
613 "Search forward from point for a character that is not encodable.
614 It asks which coding system to check.
615 If such a character is found, set point after that character.
616 Otherwise, don't move point.
617
618 When called from a program, the value is a position of the found character,
619 or nil if all characters are encodable."
620 (interactive
621 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
622 (read-coding-system
623 (format "Coding-system (default %s): " default)
624 default))))
625 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
626 (if pos
627 (goto-char (1+ pos))
628 (message "All following characters are encodable by %s" coding-system))
629 pos))
630
631
632 (defvar last-coding-system-specified nil
633 "Most recent coding system explicitly specified by the user when asked.
634 This variable is set whenever Emacs asks the user which coding system
635 to use in order to write a file. If you set it to nil explicitly,
636 then call `write-region', then afterward this variable will be non-nil
637 only if the user was explicitly asked and specified a coding system.")
638
639 (defvar select-safe-coding-system-accept-default-p nil
640 "If non-nil, a function to control the behavior of coding system selection.
641 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
642 function `select-safe-coding-system' (which see). This variable
643 overrides that argument.")
644
645 (defun select-safe-coding-system-interactively (from to codings unsafe
646 &optional rejected default)
647 "Select interactively a coding system for the region FROM ... TO.
648 FROM can be a string, as in `write-region'.
649 CODINGS is the list of base coding systems known to be safe for this region,
650 typically obtained with `find-coding-systems-region'.
651 UNSAFE is a list of coding systems known to be unsafe for this region.
652 REJECTED is a list of coding systems which were safe but for some reason
653 were not recommended in the particular context.
654 DEFAULT is the coding system to use by default in the query."
655 ;; At first, if some defaults are unsafe, record at most 11
656 ;; problematic characters and their positions for them by turning
657 ;; (CODING ...)
658 ;; into
659 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
660 (if unsafe
661 (setq unsafe
662 (mapcar #'(lambda (coding)
663 (cons coding
664 (if (stringp from)
665 (mapcar #'(lambda (pos)
666 (cons pos (aref from pos)))
667 (unencodable-char-position
668 0 (length from) coding
669 11 from))
670 (mapcar #'(lambda (pos)
671 (cons pos (char-after pos)))
672 (unencodable-char-position
673 from to coding 11)))))
674 unsafe)))
675
676 ;; Change each safe coding system to the corresponding
677 ;; mime-charset name if it is also a coding system. Such a name
678 ;; is more friendly to users.
679 (let ((l codings)
680 mime-charset)
681 (while l
682 (setq mime-charset (coding-system-get (car l) 'mime-charset))
683 (if (and mime-charset (coding-system-p mime-charset))
684 (setcar l mime-charset))
685 (setq l (cdr l))))
686
687 ;; Don't offer variations with locking shift, which you
688 ;; basically never want.
689 (let (l)
690 (dolist (elt codings (setq codings (nreverse l)))
691 (unless (or (eq 'coding-category-iso-7-else
692 (coding-system-category elt))
693 (eq 'coding-category-iso-8-else
694 (coding-system-category elt)))
695 (push elt l))))
696
697 ;; Remove raw-text, emacs-mule and no-conversion unless nothing
698 ;; else is available.
699 (setq codings
700 (or (delq 'raw-text
701 (delq 'emacs-mule
702 (delq 'no-conversion codings)))
703 '(raw-text emacs-mule no-conversion)))
704
705 (let ((window-configuration (current-window-configuration))
706 (bufname (buffer-name))
707 coding-system)
708 (save-excursion
709 ;; If some defaults are unsafe, make sure the offending
710 ;; buffer is displayed.
711 (when (and unsafe (not (stringp from)))
712 (pop-to-buffer bufname)
713 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
714 unsafe))))
715 ;; Then ask users to select one from CODINGS while showing
716 ;; the reason why none of the defaults are not used.
717 (with-output-to-temp-buffer "*Warning*"
718 (with-current-buffer standard-output
719 (if (and (null rejected) (null unsafe))
720 (insert "No default coding systems to try for "
721 (if (stringp from)
722 (format "string \"%s\"." from)
723 (format "buffer `%s'." bufname)))
724 (insert
725 "These default coding systems were tried to encode"
726 (if (stringp from)
727 (concat " \"" (if (> (length from) 10)
728 (concat (substring from 0 10) "...\"")
729 (concat from "\"")))
730 (format " text\nin the buffer `%s'" bufname))
731 ":\n")
732 (let ((pos (point))
733 (fill-prefix " "))
734 (dolist (x (append rejected unsafe))
735 (princ " ") (princ (car x)))
736 (insert "\n")
737 (fill-region-as-paragraph pos (point)))
738 (when rejected
739 (insert "These safely encodes the target text,
740 but it is not recommended for encoding text in this context,
741 e.g., for sending an email message.\n ")
742 (dolist (x rejected)
743 (princ " ") (princ x))
744 (insert "\n"))
745 (when unsafe
746 (insert (if rejected "And the others"
747 "However, each of them")
748 " encountered these problematic characters:\n")
749 (dolist (coding unsafe)
750 (insert (format " %s:" (car coding)))
751 (let ((i 0)
752 (func1
753 #'(lambda (bufname pos)
754 (when (buffer-live-p (get-buffer bufname))
755 (pop-to-buffer bufname)
756 (goto-char pos))))
757 (func2
758 #'(lambda (bufname pos coding)
759 (when (buffer-live-p (get-buffer bufname))
760 (pop-to-buffer bufname)
761 (if (< (point) pos)
762 (goto-char pos)
763 (forward-char 1)
764 (search-unencodable-char coding)
765 (forward-char -1))))))
766 (dolist (elt (cdr coding))
767 (insert " ")
768 (if (stringp from)
769 (insert (if (< i 10) (cdr elt) "..."))
770 (if (< i 10)
771 (insert-text-button
772 (cdr elt)
773 :type 'help-xref
774 'help-echo
775 "mouse-2, RET: jump to this character"
776 'help-function func1
777 'help-args (list bufname (car elt)))
778 (insert-text-button
779 "..."
780 :type 'help-xref
781 'help-echo
782 "mouse-2, RET: next unencodable character"
783 'help-function func2
784 'help-args (list bufname (car elt)
785 (car coding)))))
786 (setq i (1+ i))))
787 (insert "\n"))
788 (insert "\
789 The first problematic character is at point in the displayed buffer,\n"
790 (substitute-command-keys "\
791 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
792 (insert "\nSelect \
793 one of the following safe coding systems, or edit the buffer:\n")
794 (let ((pos (point))
795 (fill-prefix " "))
796 (dolist (x codings)
797 (princ " ") (princ x))
798 (insert "\n")
799 (fill-region-as-paragraph pos (point)))
800 (insert "Or specify any other coding system
801 at the risk of losing the problematic characters.\n")))
802
803 ;; Read a coding system.
804 (setq coding-system
805 (read-coding-system
806 (format "Select coding system (default %s): " default)
807 default))
808 (setq last-coding-system-specified coding-system))
809
810 (kill-buffer "*Warning*")
811 (set-window-configuration window-configuration)
812 coding-system))
813
814 (defun select-safe-coding-system (from to &optional default-coding-system
815 accept-default-p file)
816 "Ask a user to select a safe coding system from candidates.
817 The candidates of coding systems which can safely encode a text
818 between FROM and TO are shown in a popup window. Among them, the most
819 proper one is suggested as the default.
820
821 The list of `buffer-file-coding-system' of the current buffer,
822 the `default-buffer-file-coding-system', and the
823 most preferred coding system (if it corresponds to a MIME charset) is
824 treated as the default coding system list. Among them, the first one
825 that safely encodes the text is normally selected silently and
826 returned without any user interaction. See also the command
827 `prefer-coding-system'.
828
829 However, the user is queried if the chosen coding system is
830 inconsistent with what would be selected by `find-auto-coding' from
831 coding cookies &c. if the contents of the region were read from a
832 file. (That could lead to data corruption in a file subsequently
833 re-visited and edited.)
834
835 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
836 list of coding systems to be prepended to the default coding system
837 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
838 element is t, the cdr part is used as the default coding system list,
839 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
840 and the most preferred coding system are not used.
841
842 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
843 determine the acceptability of the silently selected coding system.
844 It is called with that coding system, and should return nil if it
845 should not be silently selected and thus user interaction is required.
846
847 Optional 5th arg FILE is the file name to use for this purpose.
848 That is different from `buffer-file-name' when handling `write-region'
849 \(for example).
850
851 The variable `select-safe-coding-system-accept-default-p', if
852 non-nil, overrides ACCEPT-DEFAULT-P.
853
854 Kludgy feature: if FROM is a string, the string is the target text,
855 and TO is ignored."
856 (if (and default-coding-system
857 (not (listp default-coding-system)))
858 (setq default-coding-system (list default-coding-system)))
859
860 (let ((no-other-defaults nil)
861 auto-cs)
862 (unless (or (stringp from) find-file-literally)
863 ;; Find an auto-coding that is specified for the the current
864 ;; buffer and file from the region FROM and TO.
865 (save-excursion
866 (save-restriction
867 (widen)
868 (goto-char from)
869 (setq auto-cs (find-auto-coding (or file buffer-file-name "")
870 (- to from)))
871 (if auto-cs
872 (if (coding-system-p (car auto-cs))
873 (setq auto-cs (car auto-cs))
874 (display-warning
875 :warning
876 (format "\
877 Invalid coding system `%s' is specified
878 for the current buffer/file by the %s.
879 It is highly recommended to fix it before writing to a file."
880 (car auto-cs)
881 (if (eq (cdr auto-cs) :coding) ":coding tag"
882 (format "variable `%s'" (cdr auto-cs)))))
883 (or (yes-or-no-p "Really proceed with writing? ")
884 (error "Save aborted"))
885 (setq auto-cs nil))))))
886
887 (if (eq (car default-coding-system) t)
888 (setq no-other-defaults t
889 default-coding-system (cdr default-coding-system)))
890
891 ;; Change elements of the list to (coding . base-coding).
892 (setq default-coding-system
893 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
894 default-coding-system))
895
896 (if (and auto-cs (not no-other-defaults))
897 ;; If the file has a coding cookie, try to use it before anything
898 ;; else (i.e. before default-coding-system which will typically come
899 ;; from file-coding-system-alist).
900 (let ((base (coding-system-base auto-cs)))
901 (or (memq base '(nil undecided))
902 (rassq base default-coding-system)
903 (push (cons auto-cs base) default-coding-system))))
904
905 (unless no-other-defaults
906 ;; If buffer-file-coding-system is not nil nor undecided, append it
907 ;; to the defaults.
908 (if buffer-file-coding-system
909 (let ((base (coding-system-base buffer-file-coding-system)))
910 (or (eq base 'undecided)
911 (rassq base default-coding-system)
912 (setq default-coding-system
913 (append default-coding-system
914 (list (cons buffer-file-coding-system base)))))))
915
916 ;; If default-buffer-file-coding-system is not nil nor undecided,
917 ;; append it to the defaults.
918 (if default-buffer-file-coding-system
919 (let ((base (coding-system-base default-buffer-file-coding-system)))
920 (or (eq base 'undecided)
921 (rassq base default-coding-system)
922 (setq default-coding-system
923 (append default-coding-system
924 (list (cons default-buffer-file-coding-system
925 base)))))))
926
927 ;; If the most preferred coding system has the property mime-charset,
928 ;; append it to the defaults.
929 (let ((tail coding-category-list)
930 preferred base)
931 (while (and tail (not (setq preferred (symbol-value (car tail)))))
932 (setq tail (cdr tail)))
933 (and (coding-system-p preferred)
934 (setq base (coding-system-base preferred))
935 (coding-system-get preferred 'mime-charset)
936 (not (rassq base default-coding-system))
937 (setq default-coding-system
938 (append default-coding-system
939 (list (cons preferred base)))))))
940
941 (if select-safe-coding-system-accept-default-p
942 (setq accept-default-p select-safe-coding-system-accept-default-p))
943
944 ;; Decide the eol-type from the top of the default codings,
945 ;; buffer-file-coding-system, or
946 ;; default-buffer-file-coding-system.
947 (if default-coding-system
948 (let ((default-eol-type (coding-system-eol-type
949 (caar default-coding-system))))
950 (if (and (vectorp default-eol-type) buffer-file-coding-system)
951 (setq default-eol-type (coding-system-eol-type
952 buffer-file-coding-system)))
953 (if (and (vectorp default-eol-type) default-buffer-file-coding-system)
954 (setq default-eol-type (coding-system-eol-type
955 default-buffer-file-coding-system)))
956 (if (and default-eol-type (not (vectorp default-eol-type)))
957 (dolist (elt default-coding-system)
958 (setcar elt (coding-system-change-eol-conversion
959 (car elt) default-eol-type))))))
960
961 (let ((codings (find-coding-systems-region from to))
962 (coding-system nil)
963 safe rejected unsafe)
964 (if (eq (car codings) 'undecided)
965 ;; Any coding system is ok.
966 (setq coding-system (caar default-coding-system))
967 ;; Reverse the list so that elements are accumulated in safe,
968 ;; rejected, and unsafe in the correct order.
969 (setq default-coding-system (nreverse default-coding-system))
970
971 ;; Classify the defaults into safe, rejected, and unsafe.
972 (dolist (elt default-coding-system)
973 (if (memq (cdr elt) codings)
974 (if (and (functionp accept-default-p)
975 (not (funcall accept-default-p (cdr elt))))
976 (push (car elt) rejected)
977 (push (car elt) safe))
978 (push (car elt) unsafe)))
979 (if safe
980 (setq coding-system (car safe))))
981
982 ;; If all the defaults failed, ask a user.
983 (when (not coding-system)
984 (setq coding-system (select-safe-coding-system-interactively
985 from to codings unsafe rejected (car codings))))
986
987 ;; Check we're not inconsistent with what `coding:' spec &c would
988 ;; give when file is re-read.
989 ;; But don't do this if we explicitly ignored the cookie
990 ;; by using `find-file-literally'.
991 (when (and auto-cs
992 (not (and
993 coding-system
994 (memq (coding-system-type coding-system) '(0 5)))))
995 ;; Merge coding-system and auto-cs as far as possible.
996 (if (not coding-system)
997 (setq coding-system auto-cs)
998 (if (not auto-cs)
999 (setq auto-cs coding-system)
1000 (let ((eol-type-1 (coding-system-eol-type coding-system))
1001 (eol-type-2 (coding-system-eol-type auto-cs)))
1002 (if (eq (coding-system-base coding-system) 'undecided)
1003 (setq coding-system (coding-system-change-text-conversion
1004 coding-system auto-cs))
1005 (if (eq (coding-system-base auto-cs) 'undecided)
1006 (setq auto-cs (coding-system-change-text-conversion
1007 auto-cs coding-system))))
1008 (if (vectorp eol-type-1)
1009 (or (vectorp eol-type-2)
1010 (setq coding-system (coding-system-change-eol-conversion
1011 coding-system eol-type-2)))
1012 (if (vectorp eol-type-2)
1013 (setq auto-cs (coding-system-change-eol-conversion
1014 auto-cs eol-type-1)))))))
1015
1016 (if (and auto-cs
1017 ;; Don't barf if writing a compressed file, say.
1018 ;; This check perhaps isn't ideal, but is probably
1019 ;; the best thing to do.
1020 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
1021 (not (coding-system-equal coding-system auto-cs)))
1022 (unless (yes-or-no-p
1023 (format "Selected encoding %s disagrees with \
1024 %s specified by file contents. Really save (else edit coding cookies \
1025 and try again)? " coding-system auto-cs))
1026 (error "Save aborted"))))
1027 coding-system)))
1028
1029 (setq select-safe-coding-system-function 'select-safe-coding-system)
1030
1031 (defun select-message-coding-system ()
1032 "Return a coding system to encode the outgoing message of the current buffer.
1033 It at first tries the first coding system found in these variables
1034 in this order:
1035 (1) local value of `buffer-file-coding-system'
1036 (2) value of `sendmail-coding-system'
1037 (3) value of `default-sendmail-coding-system'
1038 (4) value of `default-buffer-file-coding-system'
1039 If the found coding system can't encode the current buffer,
1040 or none of them are bound to a coding system,
1041 it asks the user to select a proper coding system."
1042 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
1043 buffer-file-coding-system)
1044 sendmail-coding-system
1045 default-sendmail-coding-system
1046 default-buffer-file-coding-system)))
1047 (if (eq coding 'no-conversion)
1048 ;; We should never use no-conversion for outgoing mail.
1049 (setq coding nil))
1050 (if (fboundp select-safe-coding-system-function)
1051 (setq coding
1052 (funcall select-safe-coding-system-function
1053 (point-min) (point-max) coding
1054 (function (lambda (x)
1055 (coding-system-get x 'mime-charset))))))
1056 (if coding
1057 ;; Be sure to use LF for end-of-line.
1058 (setq coding (coding-system-change-eol-conversion coding 'unix))
1059 ;; No coding system is decided. Usually this is the case that
1060 ;; the current buffer contains only ASCII. So, we hope
1061 ;; iso-8859-1 works.
1062 (setq coding 'iso-8859-1-unix))
1063 coding))
1064 \f
1065 ;;; Language support stuff.
1066
1067 (defvar language-info-alist nil
1068 "Alist of language environment definitions.
1069 Each element looks like:
1070 (LANGUAGE-NAME . ((KEY . INFO) ...))
1071 where LANGUAGE-NAME is a string, the name of the language environment,
1072 KEY is a symbol denoting the kind of information, and
1073 INFO is the data associated with KEY.
1074 Meaningful values for KEY include
1075
1076 documentation value is documentation of what this language environment
1077 is meant for, and how to use it.
1078 charset value is a list of the character sets used by this
1079 language environment.
1080 sample-text value is an expression which is evalled to generate
1081 a line of text written using characters appropriate
1082 for this language environment.
1083 setup-function value is a function to call to switch to this
1084 language environment.
1085 exit-function value is a function to call to leave this
1086 language environment.
1087 coding-system value is a list of coding systems that are good
1088 for saving text written in this language environment.
1089 This list serves as suggestions to the user;
1090 in effect, as a kind of documentation.
1091 coding-priority value is a list of coding systems for this language
1092 environment, in order of decreasing priority.
1093 This is used to set up the coding system priority
1094 list when you switch to this language environment.
1095 nonascii-translation
1096 value is a translation table to be set in the
1097 variable `nonascii-translation-table' in this
1098 language environment, or a character set from
1099 which `nonascii-insert-offset' is calculated.
1100 input-method value is a default input method for this language
1101 environment.
1102 features value is a list of features requested in this
1103 language environment.
1104 ctext-non-standard-encodings
1105 value is a list of non-standard encoding
1106 names used in extended segments of CTEXT.
1107 See the variable
1108 `ctext-non-standard-encodings' for more
1109 detail.
1110
1111 The following keys take effect only when multibyte characters are
1112 globally disabled, i.e. the value of `default-enable-multibyte-characters'
1113 is nil.
1114
1115 unibyte-syntax value is a library name to load to set
1116 unibyte 8-bit character syntaxes for this
1117 language environment.
1118
1119 unibyte-display value is a coding system to encode characters
1120 for the terminal. Characters in the range
1121 of 160 to 255 display not as octal escapes,
1122 but as non-ASCII characters in this language
1123 environment.")
1124
1125 (defun get-language-info (lang-env key)
1126 "Return information listed under KEY for language environment LANG-ENV.
1127 KEY is a symbol denoting the kind of information.
1128 For a list of useful values for KEY and their meanings,
1129 see `language-info-alist'."
1130 (if (symbolp lang-env)
1131 (setq lang-env (symbol-name lang-env)))
1132 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1133 (if lang-slot
1134 (cdr (assq key (cdr lang-slot))))))
1135
1136 (defun set-language-info (lang-env key info)
1137 "Modify part of the definition of language environment LANG-ENV.
1138 Specifically, this stores the information INFO under KEY
1139 in the definition of this language environment.
1140 KEY is a symbol denoting the kind of information.
1141 INFO is the value for that information.
1142
1143 For a list of useful values for KEY and their meanings,
1144 see `language-info-alist'."
1145 (if (symbolp lang-env)
1146 (setq lang-env (symbol-name lang-env)))
1147 (set-language-info-internal lang-env key info)
1148 (if (equal lang-env current-language-environment)
1149 (cond ((eq key 'coding-priority)
1150 (set-language-environment-coding-systems lang-env))
1151 ((eq key 'input-method)
1152 (set-language-environment-input-method lang-env))
1153 ((eq key 'nonascii-translation)
1154 (set-language-environment-nonascii-translation lang-env))
1155 ((eq key 'charset)
1156 (set-language-environment-charset lang-env))
1157 ((eq key 'overriding-fontspec)
1158 (set-language-environment-fontset lang-env))
1159 ((and (not default-enable-multibyte-characters)
1160 (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
1161 (set-language-environment-unibyte lang-env)))))
1162
1163 (defun set-language-info-internal (lang-env key info)
1164 "Internal use only.
1165 Arguments are the same as `set-language-info'."
1166 (let (lang-slot key-slot)
1167 (setq lang-slot (assoc lang-env language-info-alist))
1168 (if (null lang-slot) ; If no slot for the language, add it.
1169 (setq lang-slot (list lang-env)
1170 language-info-alist (cons lang-slot language-info-alist)))
1171 (setq key-slot (assq key lang-slot))
1172 (if (null key-slot) ; If no slot for the key, add it.
1173 (progn
1174 (setq key-slot (list key))
1175 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1176 (setcdr key-slot (purecopy info))
1177 ;; Update the custom-type of `current-language-environment'.
1178 (put 'current-language-environment 'custom-type
1179 (cons 'choice (mapcar
1180 (lambda (lang)
1181 (list 'const lang))
1182 (sort (mapcar 'car language-info-alist) 'string<))))))
1183
1184 (defun set-language-info-alist (lang-env alist &optional parents)
1185 "Store ALIST as the definition of language environment LANG-ENV.
1186 ALIST is an alist of KEY and INFO values. See the documentation of
1187 `language-info-alist' for the meanings of KEY and INFO.
1188
1189 Optional arg PARENTS is a list of parent menu names; it specifies
1190 where to put this language environment in the
1191 Describe Language Environment and Set Language Environment menus.
1192 For example, (\"European\") means to put this language environment
1193 in the European submenu in each of those two menus."
1194 (if (symbolp lang-env)
1195 (setq lang-env (symbol-name lang-env)))
1196 (let ((describe-map describe-language-environment-map)
1197 (setup-map setup-language-environment-map))
1198 (if parents
1199 (let ((l parents)
1200 map parent-symbol parent prompt)
1201 (while l
1202 (if (symbolp (setq parent-symbol (car l)))
1203 (setq parent (symbol-name parent))
1204 (setq parent parent-symbol parent-symbol (intern parent)))
1205 (setq map (lookup-key describe-map (vector parent-symbol)))
1206 ;; This prompt string is for define-prefix-command, so
1207 ;; that the map it creates will be suitable for a menu.
1208 (or map (setq prompt (format "%s Environment" parent)))
1209 (if (not map)
1210 (progn
1211 (setq map (intern (format "describe-%s-environment-map"
1212 (downcase parent))))
1213 (define-prefix-command map nil prompt)
1214 (define-key-after describe-map (vector parent-symbol)
1215 (cons parent map) t)))
1216 (setq describe-map (symbol-value map))
1217 (setq map (lookup-key setup-map (vector parent-symbol)))
1218 (if (not map)
1219 (progn
1220 (setq map (intern (format "setup-%s-environment-map"
1221 (downcase parent))))
1222 (define-prefix-command map nil prompt)
1223 (define-key-after setup-map (vector parent-symbol)
1224 (cons parent map) t)))
1225 (setq setup-map (symbol-value map))
1226 (setq l (cdr l)))))
1227
1228 ;; Set up menu items for this language env.
1229 (let ((doc (assq 'documentation alist)))
1230 (when doc
1231 (define-key-after describe-map (vector (intern lang-env))
1232 (cons lang-env 'describe-specified-language-support) t)))
1233 (define-key-after setup-map (vector (intern lang-env))
1234 (cons lang-env 'setup-specified-language-environment) t)
1235
1236 (dolist (elt alist)
1237 (set-language-info-internal lang-env (car elt) (cdr elt)))
1238
1239 (if (equal lang-env current-language-environment)
1240 (set-language-environment lang-env))))
1241
1242 (defun read-language-name (key prompt &optional default)
1243 "Read a language environment name which has information for KEY.
1244 If KEY is nil, read any language environment.
1245 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1246 This returns a language environment name as a string."
1247 (let* ((completion-ignore-case t)
1248 (name (completing-read prompt
1249 language-info-alist
1250 (and key
1251 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1252 t nil nil default)))
1253 (if (and (> (length name) 0)
1254 (or (not key)
1255 (get-language-info name key)))
1256 name)))
1257 \f
1258 ;;; Multilingual input methods.
1259 (defgroup leim nil
1260 "LEIM: Libraries of Emacs Input Methods."
1261 :group 'mule)
1262
1263 (defconst leim-list-file-name "leim-list.el"
1264 "Name of LEIM list file.
1265 This file contains a list of libraries of Emacs input methods (LEIM)
1266 in the format of Lisp expression for registering each input method.
1267 Emacs loads this file at startup time.")
1268
1269 (defvar leim-list-header (format
1270 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1271 ;;
1272 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1273 ;; methods in the same directory as this file. Loading this file
1274 ;; registers all the input methods in Emacs.
1275 ;;
1276 ;; Each entry has the form:
1277 ;; (register-input-method
1278 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1279 ;; TITLE DESCRIPTION
1280 ;; ARG ...)
1281 ;; See the function `register-input-method' for the meanings of the arguments.
1282 ;;
1283 ;; If this directory is included in load-path, Emacs automatically
1284 ;; loads this file at startup time.
1285
1286 "
1287 leim-list-file-name)
1288 "Header to be inserted in LEIM list file.")
1289
1290 (defvar leim-list-entry-regexp "^(register-input-method"
1291 "Regexp matching head of each entry in LEIM list file.
1292 See also the variable `leim-list-header'")
1293
1294 (defvar update-leim-list-functions
1295 '(quail-update-leim-list-file)
1296 "List of functions to call to update LEIM list file.
1297 Each function is called with one arg, LEIM directory name.")
1298
1299 (defun update-leim-list-file (&rest dirs)
1300 "Update LEIM list file in directories DIRS."
1301 (let ((functions update-leim-list-functions))
1302 (while functions
1303 (apply (car functions) dirs)
1304 (setq functions (cdr functions)))))
1305
1306 (defvar current-input-method nil
1307 "The current input method for multilingual text.
1308 If nil, that means no input method is activated now.")
1309 (make-variable-buffer-local 'current-input-method)
1310 (put 'current-input-method 'permanent-local t)
1311
1312 (defvar current-input-method-title nil
1313 "Title string of the current input method shown in mode line.")
1314 (make-variable-buffer-local 'current-input-method-title)
1315 (put 'current-input-method-title 'permanent-local t)
1316
1317 (defcustom default-input-method nil
1318 "*Default input method for multilingual text (a string).
1319 This is the input method activated automatically by the command
1320 `toggle-input-method' (\\[toggle-input-method])."
1321 :link '(custom-manual "(emacs)Input Methods")
1322 :group 'mule
1323 :type '(choice (const nil) (string
1324 :completion-ignore-case t
1325 :complete-function widget-string-complete
1326 :completion-alist input-method-alist
1327 :prompt-history input-method-history))
1328 :set-after '(current-language-environment))
1329
1330 (put 'input-method-function 'permanent-local t)
1331
1332 (defvar input-method-history nil
1333 "History list for some commands that read input methods.")
1334 (make-variable-buffer-local 'input-method-history)
1335 (put 'input-method-history 'permanent-local t)
1336
1337 (defvar inactivate-current-input-method-function nil
1338 "Function to call for inactivating the current input method.
1339 Every input method should set this to an appropriate value when activated.
1340 This function is called with no argument.
1341
1342 This function should never change the value of `current-input-method'.
1343 It is set to nil by the function `inactivate-input-method'.")
1344 (make-variable-buffer-local 'inactivate-current-input-method-function)
1345 (put 'inactivate-current-input-method-function 'permanent-local t)
1346
1347 (defvar describe-current-input-method-function nil
1348 "Function to call for describing the current input method.
1349 This function is called with no argument.")
1350 (make-variable-buffer-local 'describe-current-input-method-function)
1351 (put 'describe-current-input-method-function 'permanent-local t)
1352
1353 (defvar input-method-alist nil
1354 "Alist of input method names vs how to use them.
1355 Each element has the form:
1356 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1357 See the function `register-input-method' for the meanings of the elements.")
1358
1359 (defun register-input-method (input-method lang-env &rest args)
1360 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1361
1362 INPUT-METHOD and LANG-ENV are symbols or strings.
1363 ACTIVATE-FUNC is a function to call to activate this method.
1364 TITLE is a string to show in the mode line when this method is active.
1365 DESCRIPTION is a string describing this method and what it is good for.
1366 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1367 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1368
1369 This function is mainly used in the file \"leim-list.el\" which is
1370 created at Emacs build time, registering all Quail input methods
1371 contained in the Emacs distribution.
1372
1373 In case you want to register a new Quail input method by yourself, be
1374 careful to use the same input method title as given in the third
1375 parameter of `quail-define-package'. (If the values are different, the
1376 string specified in this function takes precedence.)
1377
1378 The commands `describe-input-method' and `list-input-methods' need
1379 these duplicated values to show some information about input methods
1380 without loading the relevant Quail packages.
1381 \n(fn INPUT-METHOD LANG-ENV ACTIVATE-FUNC TITLE DESCRIPTION &rest ARGS)"
1382 (if (symbolp lang-env)
1383 (setq lang-env (symbol-name lang-env)))
1384 (if (symbolp input-method)
1385 (setq input-method (symbol-name input-method)))
1386 (let ((info (cons lang-env args))
1387 (slot (assoc input-method input-method-alist)))
1388 (if slot
1389 (setcdr slot info)
1390 (setq slot (cons input-method info))
1391 (setq input-method-alist (cons slot input-method-alist)))))
1392
1393 (defun read-input-method-name (prompt &optional default inhibit-null)
1394 "Read a name of input method from a minibuffer prompting with PROMPT.
1395 If DEFAULT is non-nil, use that as the default,
1396 and substitute it into PROMPT at the first `%s'.
1397 If INHIBIT-NULL is non-nil, null input signals an error.
1398
1399 The return value is a string."
1400 (if default
1401 (setq prompt (format prompt default)))
1402 (let* ((completion-ignore-case t)
1403 ;; As it is quite normal to change input method in the
1404 ;; minibuffer, we must enable it even if
1405 ;; enable-recursive-minibuffers is currently nil.
1406 (enable-recursive-minibuffers t)
1407 ;; This binding is necessary because input-method-history is
1408 ;; buffer local.
1409 (input-method (completing-read prompt input-method-alist
1410 nil t nil 'input-method-history
1411 default)))
1412 (if (and input-method (symbolp input-method))
1413 (setq input-method (symbol-name input-method)))
1414 (if (> (length input-method) 0)
1415 input-method
1416 (if inhibit-null
1417 (error "No valid input method is specified")))))
1418
1419 (defun activate-input-method (input-method)
1420 "Switch to input method INPUT-METHOD for the current buffer.
1421 If some other input method is already active, turn it off first.
1422 If INPUT-METHOD is nil, deactivate any current input method."
1423 (if (and input-method (symbolp input-method))
1424 (setq input-method (symbol-name input-method)))
1425 (if (and current-input-method
1426 (not (string= current-input-method input-method)))
1427 (inactivate-input-method))
1428 (unless (or current-input-method (null input-method))
1429 (let ((slot (assoc input-method input-method-alist)))
1430 (if (null slot)
1431 (error "Can't activate input method `%s'" input-method))
1432 (setq current-input-method-title nil)
1433 (let ((func (nth 2 slot)))
1434 (if (functionp func)
1435 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1436 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1437 (progn
1438 (require (cdr func))
1439 (apply (car func) input-method (nthcdr 5 slot)))
1440 (error "Can't activate input method `%s'" input-method))))
1441 (setq current-input-method input-method)
1442 (or (stringp current-input-method-title)
1443 (setq current-input-method-title (nth 3 slot)))
1444 (unwind-protect
1445 (run-hooks 'input-method-activate-hook)
1446 (force-mode-line-update)))))
1447
1448 (defun inactivate-input-method ()
1449 "Turn off the current input method."
1450 (when current-input-method
1451 (if input-method-history
1452 (unless (string= current-input-method (car input-method-history))
1453 (setq input-method-history
1454 (cons current-input-method
1455 (delete current-input-method input-method-history))))
1456 (setq input-method-history (list current-input-method)))
1457 (unwind-protect
1458 (funcall inactivate-current-input-method-function)
1459 (unwind-protect
1460 (run-hooks 'input-method-inactivate-hook)
1461 (setq current-input-method nil
1462 input-method-function nil
1463 current-input-method-title nil)
1464 (force-mode-line-update)))))
1465
1466 (defun set-input-method (input-method &optional interactive)
1467 "Select and activate input method INPUT-METHOD for the current buffer.
1468 This also sets the default input method to the one you specify.
1469 If INPUT-METHOD is nil, this function turns off the input method, and
1470 also causes you to be prompted for a name of an input method the next
1471 time you invoke \\[toggle-input-method].
1472 When called interactively, the optional arg INTERACTIVE is non-nil,
1473 which marks the variable `default-input-method' as set for Custom buffers.
1474
1475 To deactivate the input method interactively, use \\[toggle-input-method].
1476 To deactivate it programmatically, use \\[inactivate-input-method]."
1477 (interactive
1478 (let* ((default (or (car input-method-history) default-input-method)))
1479 (list (read-input-method-name
1480 (if default "Select input method (default %s): " "Select input method: ")
1481 default t)
1482 t)))
1483 (activate-input-method input-method)
1484 (setq default-input-method input-method)
1485 (when interactive
1486 (customize-mark-as-set 'default-input-method))
1487 default-input-method)
1488
1489 (defun toggle-input-method (&optional arg interactive)
1490 "Enable or disable multilingual text input method for the current buffer.
1491 Only one input method can be enabled at any time in a given buffer.
1492
1493 The normal action is to enable an input method if none was
1494 enabled, and disable the current one otherwise. Which input method
1495 to enable can be determined in various ways--either the one most
1496 recently used, or the one specified by `default-input-method', or
1497 as a last resort by reading the name of an input method in the
1498 minibuffer.
1499
1500 With a prefix argument, read an input method name with the minibuffer
1501 and enable that one. The default is the most recent input method specified
1502 \(not including the currently active input method, if any).
1503
1504 When called interactively, the optional arg INTERACTIVE is non-nil,
1505 which marks the variable `default-input-method' as set for Custom buffers."
1506
1507 (interactive "P\np")
1508 (if (and current-input-method (not arg))
1509 (inactivate-input-method)
1510 (let ((default (or (car input-method-history) default-input-method)))
1511 (if (and arg default (equal current-input-method default)
1512 (> (length input-method-history) 1))
1513 (setq default (nth 1 input-method-history)))
1514 (activate-input-method
1515 (if (or arg (not default))
1516 (progn
1517 (read-input-method-name
1518 (if default "Input method (default %s): " "Input method: " )
1519 default t))
1520 default))
1521 (unless default-input-method
1522 (prog1
1523 (setq default-input-method current-input-method)
1524 (when interactive
1525 (customize-mark-as-set 'default-input-method)))))))
1526
1527 (defun describe-input-method (input-method)
1528 "Describe input method INPUT-METHOD."
1529 (interactive
1530 (list (read-input-method-name
1531 "Describe input method (default current choice): ")))
1532 (if (and input-method (symbolp input-method))
1533 (setq input-method (symbol-name input-method)))
1534 (help-setup-xref (list #'describe-input-method
1535 (or input-method current-input-method))
1536 (interactive-p))
1537
1538 (if (null input-method)
1539 (describe-current-input-method)
1540 (let ((current current-input-method))
1541 (condition-case nil
1542 (progn
1543 (save-excursion
1544 (activate-input-method input-method)
1545 (describe-current-input-method))
1546 (activate-input-method current))
1547 (error
1548 (activate-input-method current)
1549 (help-setup-xref (list #'describe-input-method input-method)
1550 (interactive-p))
1551 (with-output-to-temp-buffer (help-buffer)
1552 (let ((elt (assoc input-method input-method-alist)))
1553 (princ (format
1554 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1555 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1556
1557 (defun describe-current-input-method ()
1558 "Describe the input method currently in use.
1559 This is a subroutine for `describe-input-method'."
1560 (if current-input-method
1561 (if (and (symbolp describe-current-input-method-function)
1562 (fboundp describe-current-input-method-function))
1563 (funcall describe-current-input-method-function)
1564 (message "No way to describe the current input method `%s'"
1565 current-input-method)
1566 (ding))
1567 (error "No input method is activated now")))
1568
1569 (defun read-multilingual-string (prompt &optional initial-input input-method)
1570 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1571 The input method selected last time is activated in minibuffer.
1572 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1573 initially.
1574 Optional 3rd argument INPUT-METHOD specifies the input method
1575 to be activated instead of the one selected last time. It is a symbol
1576 or a string."
1577 (setq input-method
1578 (or input-method
1579 current-input-method
1580 default-input-method
1581 (read-input-method-name "Input method: " nil t)))
1582 (if (and input-method (symbolp input-method))
1583 (setq input-method (symbol-name input-method)))
1584 (let ((prev-input-method current-input-method))
1585 (unwind-protect
1586 (progn
1587 (activate-input-method input-method)
1588 (read-string prompt initial-input nil nil t))
1589 (activate-input-method prev-input-method))))
1590
1591 ;; Variables to control behavior of input methods. All input methods
1592 ;; should react to these variables.
1593
1594 (defcustom input-method-verbose-flag 'default
1595 "*A flag to control extra guidance given by input methods.
1596 The value should be nil, t, `complex-only', or `default'.
1597
1598 The extra guidance is done by showing list of available keys in echo
1599 area. When you use the input method in the minibuffer, the guidance
1600 is shown at the bottom short window (split from the existing window).
1601
1602 If the value is t, extra guidance is always given, if the value is
1603 nil, extra guidance is always suppressed.
1604
1605 If the value is `complex-only', only complex input methods such as
1606 `chinese-py' and `japanese' give extra guidance.
1607
1608 If the value is `default', complex input methods always give extra
1609 guidance, but simple input methods give it only when you are not in
1610 the minibuffer.
1611
1612 See also the variable `input-method-highlight-flag'."
1613 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1614 (const complex-only) (const default))
1615 :group 'mule)
1616
1617 (defcustom input-method-highlight-flag t
1618 "*If this flag is non-nil, input methods highlight partially-entered text.
1619 For instance, while you are in the middle of a Quail input method sequence,
1620 the text inserted so far is temporarily underlined.
1621 The underlining goes away when you finish or abort the input method sequence.
1622 See also the variable `input-method-verbose-flag'."
1623 :type 'boolean
1624 :group 'mule)
1625
1626 (defvar input-method-activate-hook nil
1627 "Normal hook run just after an input method is activated.
1628
1629 The variable `current-input-method' keeps the input method name
1630 just activated.")
1631
1632 (defvar input-method-inactivate-hook nil
1633 "Normal hook run just after an input method is inactivated.
1634
1635 The variable `current-input-method' still keeps the input method name
1636 just inactivated.")
1637
1638 (defvar input-method-after-insert-chunk-hook nil
1639 "Normal hook run just after an input method insert some chunk of text.")
1640
1641 (defvar input-method-exit-on-first-char nil
1642 "This flag controls when an input method returns.
1643 Usually, the input method does not return while there's a possibility
1644 that it may find a different translation if a user types another key.
1645 But, it this flag is non-nil, the input method returns as soon as
1646 the current key sequence gets long enough to have some valid translation.")
1647
1648 (defvar input-method-use-echo-area nil
1649 "This flag controls how an input method shows an intermediate key sequence.
1650 Usually, the input method inserts the intermediate key sequence,
1651 or candidate translations corresponding to the sequence,
1652 at point in the current buffer.
1653 But, if this flag is non-nil, it displays them in echo area instead.")
1654
1655 (defvar input-method-exit-on-invalid-key nil
1656 "This flag controls the behavior of an input method on invalid key input.
1657 Usually, when a user types a key which doesn't start any character
1658 handled by the input method, the key is handled by turning off the
1659 input method temporarily. After that key, the input method is re-enabled.
1660 But, if this flag is non-nil, the input method is never back on.")
1661
1662 \f
1663 (defvar set-language-environment-hook nil
1664 "Normal hook run after some language environment is set.
1665
1666 When you set some hook function here, that effect usually should not
1667 be inherited to another language environment. So, you had better set
1668 another function in `exit-language-environment-hook' (which see) to
1669 cancel the effect.")
1670
1671 (defvar exit-language-environment-hook nil
1672 "Normal hook run after exiting from some language environment.
1673 When this hook is run, the variable `current-language-environment'
1674 is still bound to the language environment being exited.
1675
1676 This hook is mainly used for canceling the effect of
1677 `set-language-environment-hook' (which-see).")
1678
1679 (put 'setup-specified-language-environment 'apropos-inhibit t)
1680
1681 (defun setup-specified-language-environment ()
1682 "Switch to a specified language environment."
1683 (interactive)
1684 (let (language-name)
1685 (if (and (symbolp last-command-event)
1686 (or (not (eq last-command-event 'Default))
1687 (setq last-command-event 'English))
1688 (setq language-name (symbol-name last-command-event)))
1689 (prog1
1690 (set-language-environment language-name)
1691 (customize-mark-as-set 'current-language-environment))
1692 (error "Bogus calling sequence"))))
1693
1694 (defcustom current-language-environment "English"
1695 "The last language environment specified with `set-language-environment'.
1696 This variable should be set only with \\[customize], which is equivalent
1697 to using the function `set-language-environment'."
1698 :link '(custom-manual "(emacs)Language Environments")
1699 :set (lambda (symbol value) (set-language-environment value))
1700 :get (lambda (x)
1701 (or (car-safe (assoc-string
1702 (if (symbolp current-language-environment)
1703 (symbol-name current-language-environment)
1704 current-language-environment)
1705 language-info-alist t))
1706 "English"))
1707 ;; custom type will be updated with `set-language-info'.
1708 :type (if language-info-alist
1709 (cons 'choice (mapcar
1710 (lambda (lang)
1711 (list 'const lang))
1712 (sort (mapcar 'car language-info-alist) 'string<)))
1713 'string)
1714 :initialize 'custom-initialize-default
1715 :group 'mule)
1716
1717 (defun reset-language-environment ()
1718 "Reset multilingual environment of Emacs to the default status.
1719
1720 The default status is as follows:
1721
1722 The default value of `buffer-file-coding-system' is nil.
1723 The default coding system for process I/O is nil.
1724 The default value for the command `set-terminal-coding-system' is nil.
1725 The default value for the command `set-keyboard-coding-system' is nil.
1726
1727 The order of priorities of coding categories and the coding system
1728 bound to each category are as follows
1729 coding category coding system
1730 --------------------------------------------------
1731 coding-category-iso-8-1 iso-latin-1
1732 coding-category-iso-8-2 iso-latin-1
1733 coding-category-utf-8 mule-utf-8
1734 coding-category-utf-16-be mule-utf-16be-with-signature
1735 coding-category-utf-16-le mule-utf-16le-with-signature
1736 coding-category-iso-7-tight iso-2022-jp
1737 coding-category-iso-7 iso-2022-7bit
1738 coding-category-iso-7-else iso-2022-7bit-lock
1739 coding-category-iso-8-else iso-2022-8bit-ss2
1740 coding-category-emacs-mule emacs-mule
1741 coding-category-raw-text raw-text
1742 coding-category-sjis japanese-shift-jis
1743 coding-category-big5 chinese-big5
1744 coding-category-ccl nil
1745 coding-category-binary no-conversion"
1746 (interactive)
1747 ;; This function formerly set default-enable-multibyte-characters to t,
1748 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1749
1750 (setq coding-category-iso-7-tight 'iso-2022-jp
1751 coding-category-iso-7 'iso-2022-7bit
1752 coding-category-iso-8-1 'iso-latin-1
1753 coding-category-iso-8-2 'iso-latin-1
1754 coding-category-iso-7-else 'iso-2022-7bit-lock
1755 coding-category-iso-8-else 'iso-2022-8bit-ss2
1756 coding-category-emacs-mule 'emacs-mule
1757 coding-category-raw-text 'raw-text
1758 coding-category-sjis 'japanese-shift-jis
1759 coding-category-big5 'chinese-big5
1760 coding-category-utf-16-be 'mule-utf-16be-with-signature
1761 coding-category-utf-16-le 'mule-utf-16le-with-signature
1762 coding-category-utf-8 'mule-utf-8
1763 coding-category-ccl nil
1764 coding-category-binary 'no-conversion)
1765
1766 (set-coding-priority
1767 '(coding-category-iso-8-1
1768 coding-category-iso-8-2
1769 coding-category-utf-8
1770 coding-category-utf-16-be
1771 coding-category-utf-16-le
1772 coding-category-iso-7-tight
1773 coding-category-iso-7
1774 coding-category-iso-7-else
1775 coding-category-iso-8-else
1776 coding-category-emacs-mule
1777 coding-category-raw-text
1778 coding-category-sjis
1779 coding-category-big5
1780 coding-category-ccl
1781 coding-category-binary))
1782
1783 ;; Changing the binding of a coding category requires this call.
1784 (update-coding-systems-internal)
1785
1786 (set-default-coding-systems nil)
1787 (setq default-sendmail-coding-system 'iso-latin-1)
1788 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1789 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1790 (setq default-file-name-coding-system 'iso-latin-1)
1791 ;; Preserve eol-type from existing default-process-coding-systems.
1792 ;; On non-unix-like systems in particular, these may have been set
1793 ;; carefully by the user, or by the startup code, to deal with the
1794 ;; users shell appropriately, so should not be altered by changing
1795 ;; language environment.
1796 (let ((output-coding
1797 ;; When bootstrapping, coding-systems are not defined yet, so
1798 ;; we need to catch the error from check-coding-system.
1799 (condition-case nil
1800 (coding-system-change-text-conversion
1801 (car default-process-coding-system) 'undecided)
1802 (coding-system-error 'undecided)))
1803 (input-coding
1804 (condition-case nil
1805 (coding-system-change-text-conversion
1806 (cdr default-process-coding-system) 'iso-latin-1)
1807 (coding-system-error 'iso-latin-1))))
1808 (setq default-process-coding-system
1809 (cons output-coding input-coding)))
1810
1811 ;; Don't alter the terminal and keyboard coding systems here.
1812 ;; The terminal still supports the same coding system
1813 ;; that it supported a minute ago.
1814 ;; (set-terminal-coding-system-internal nil)
1815 ;; (set-keyboard-coding-system-internal nil)
1816
1817 (setq nonascii-translation-table nil
1818 nonascii-insert-offset 0)
1819
1820 ;; Don't invoke fontset-related functions if fontsets aren't
1821 ;; supported in this build of Emacs.
1822 (and (fboundp 'fontset-list)
1823 (set-overriding-fontspec-internal nil)))
1824
1825 (reset-language-environment)
1826
1827 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system)
1828 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1829 (let ((coding (get-language-info language-name 'unibyte-display)))
1830 (if (and coding
1831 (or (not coding-system)
1832 (coding-system-equal coding coding-system)))
1833 (standard-display-european-internal)
1834 ;; The following 2 lines undo the 8-bit display that we set up
1835 ;; in standard-display-european-internal, which see. This is in
1836 ;; case the user has used standard-display-european earlier in
1837 ;; this session. (The MS-DOS port doesn't use that setup, so it
1838 ;; doesn't need to undo it.)
1839 (when standard-display-table
1840 (dotimes (i 128)
1841 (aset standard-display-table (+ i 128) nil))))
1842 (or (eq window-system 'pc)
1843 (set-terminal-coding-system (or coding-system coding)))))
1844
1845 (defun set-language-environment (language-name)
1846 "Set up multi-lingual environment for using LANGUAGE-NAME.
1847 This sets the coding system priority and the default input method
1848 and sometimes other things. LANGUAGE-NAME should be a string
1849 which is the name of a language environment. For example, \"Latin-1\"
1850 specifies the character set for the major languages of Western Europe."
1851 (interactive (list (read-language-name
1852 nil
1853 "Set language environment (default English): ")))
1854 (if language-name
1855 (if (symbolp language-name)
1856 (setq language-name (symbol-name language-name)))
1857 (setq language-name "English"))
1858 (let ((slot (assoc-string language-name language-info-alist t)))
1859 (unless slot
1860 (error "Language environment not defined: %S" language-name))
1861 (setq language-name (car slot)))
1862 (if current-language-environment
1863 (let ((func (get-language-info current-language-environment
1864 'exit-function)))
1865 (run-hooks 'exit-language-environment-hook)
1866 (if (functionp func) (funcall func))))
1867
1868 (reset-language-environment)
1869 ;; The features might set up coding systems.
1870 (let ((required-features (get-language-info language-name 'features)))
1871 (while required-features
1872 (require (car required-features))
1873 (setq required-features (cdr required-features))))
1874
1875 (setq current-language-environment language-name)
1876
1877 (set-language-environment-coding-systems language-name)
1878 (set-language-environment-input-method language-name)
1879 (set-language-environment-nonascii-translation language-name)
1880 (set-language-environment-charset language-name)
1881 (set-language-environment-fontset language-name)
1882 ;; Unibyte setups if necessary.
1883 (unless default-enable-multibyte-characters
1884 (set-language-environment-unibyte language-name))
1885
1886 (let ((func (get-language-info language-name 'setup-function)))
1887 (if (functionp func)
1888 (funcall func)))
1889
1890 (run-hooks 'set-language-environment-hook)
1891 (force-mode-line-update t))
1892
1893 (defun standard-display-european-internal ()
1894 ;; Actually set up direct output of non-ASCII characters.
1895 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1896 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1897 ;; the native font, and codes 160 and 146 stand for something very
1898 ;; different there.
1899 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1900 (progn
1901 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1902 (unless (and (eq window-system 'x)
1903 ;; XFree86 4 has fixed the fonts.
1904 (string= "The XFree86 Project, Inc" (x-server-vendor))
1905 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1906 ?3))
1907 ;; Make non-line-break space display as a plain space.
1908 (aset standard-display-table 160 [32]))
1909 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1910 ;; don't contain a character at that position. Map it to the ASCII
1911 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1912 ;; U+2019, normally from the windows-1252 character set. XFree 4
1913 ;; fonts probably have the appropriate glyph at this position,
1914 ;; so they could use standard-display-8bit. It's better to use a
1915 ;; proper windows-1252 coding system. --fx]
1916 (aset standard-display-table 146 [39]))))
1917
1918 (defun set-language-environment-coding-systems (language-name)
1919 "Do various coding system setups for language environment LANGUAGE-NAME."
1920 (let* ((priority (get-language-info language-name 'coding-priority))
1921 (default-coding (car priority))
1922 (eol-type (coding-system-eol-type default-buffer-file-coding-system)))
1923 (if priority
1924 (let ((categories (mapcar 'coding-system-category priority)))
1925 (set-default-coding-systems
1926 (if (memq eol-type '(0 1 2 unix dos mac))
1927 (coding-system-change-eol-conversion default-coding eol-type)
1928 default-coding))
1929 (setq default-sendmail-coding-system default-coding)
1930 (set-coding-priority categories)
1931 (while priority
1932 (set (car categories) (car priority))
1933 (setq priority (cdr priority) categories (cdr categories)))
1934 ;; Changing the binding of a coding category requires this call.
1935 (update-coding-systems-internal)))))
1936
1937 (defun set-language-environment-input-method (language-name)
1938 "Do various input method setups for language environment LANGUAGE-NAME."
1939 (let ((input-method (get-language-info language-name 'input-method)))
1940 (when input-method
1941 (setq default-input-method input-method)
1942 (if input-method-history
1943 (setq input-method-history
1944 (cons input-method
1945 (delete input-method input-method-history)))))))
1946
1947 (defun set-language-environment-nonascii-translation (language-name)
1948 "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
1949 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1950 (dos-table
1951 (if (eq window-system 'pc)
1952 (intern
1953 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1954 (cond
1955 ((char-table-p nonascii)
1956 (setq nonascii-translation-table nonascii))
1957 ((and (eq window-system 'pc) (boundp dos-table))
1958 ;; DOS terminals' default is to use a special non-ASCII translation
1959 ;; table as appropriate for the installed codepage.
1960 (setq nonascii-translation-table (symbol-value dos-table)))
1961 ((charsetp nonascii)
1962 (setq nonascii-insert-offset (- (make-char nonascii) 128))))))
1963
1964 (defun set-language-environment-charset (language-name)
1965 "Do various charset setups for language environment LANGUAGE-NAME."
1966 (if (and utf-translate-cjk-mode
1967 (not (eq utf-translate-cjk-lang-env language-name))
1968 (catch 'tag
1969 (dolist (charset (get-language-info language-name 'charset))
1970 (if (memq charset utf-translate-cjk-charsets)
1971 (throw 'tag t)))
1972 nil))
1973 (utf-translate-cjk-load-tables)))
1974
1975 (defun set-language-environment-fontset (language-name)
1976 "Do various fontset setups for language environment LANGUAGE-NAME."
1977 ;; Don't invoke fontset-related functions if fontsets aren't
1978 ;; supported in this build of Emacs.
1979 (if (fboundp 'fontset-list)
1980 (set-overriding-fontspec-internal
1981 (get-language-info language-name 'overriding-fontspec))))
1982
1983 (defun set-language-environment-unibyte (language-name)
1984 "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
1985 ;; Syntax and case table.
1986 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1987 (if syntax
1988 (let ((set-case-syntax-set-multibyte nil))
1989 (load syntax nil t))
1990 ;; No information for syntax and case. Reset to the defaults.
1991 (let ((syntax-table (standard-syntax-table))
1992 (standard-table (standard-case-table))
1993 (case-table (make-char-table 'case-table))
1994 (ch (if (eq window-system 'pc) 128 160)))
1995 (while (< ch 256)
1996 (modify-syntax-entry ch " " syntax-table)
1997 (setq ch (1+ ch)))
1998 (dotimes (i 128)
1999 (aset case-table i (aref standard-table i)))
2000 (set-char-table-extra-slot case-table 0 nil)
2001 (set-char-table-extra-slot case-table 1 nil)
2002 (set-char-table-extra-slot case-table 2 nil)
2003 (set-standard-case-table case-table))
2004 (let ((list (buffer-list)))
2005 (while list
2006 (with-current-buffer (car list)
2007 (set-case-table (standard-case-table)))
2008 (setq list (cdr list))))))
2009 (set-display-table-and-terminal-coding-system language-name))
2010
2011 (defsubst princ-list (&rest args)
2012 "Print all arguments with `princ', then print \"\n\"."
2013 (while args (princ (car args)) (setq args (cdr args)))
2014 (princ "\n"))
2015
2016 (put 'describe-specified-language-support 'apropos-inhibit t)
2017
2018 ;; Print language-specific information such as input methods,
2019 ;; charsets, and coding systems. This function is intended to be
2020 ;; called from the menu:
2021 ;; [menu-bar mule describe-language-environment LANGUAGE]
2022 ;; and should not run it by `M-x describe-current-input-method-function'.
2023 (defun describe-specified-language-support ()
2024 "Describe how Emacs supports the specified language environment."
2025 (interactive)
2026 (let (language-name)
2027 (if (not (and (symbolp last-command-event)
2028 (or (not (eq last-command-event 'Default))
2029 (setq last-command-event 'English))
2030 (setq language-name (symbol-name last-command-event))))
2031 (error "Bogus calling sequence"))
2032 (describe-language-environment language-name)))
2033
2034 (defun describe-language-environment (language-name)
2035 "Describe how Emacs supports language environment LANGUAGE-NAME."
2036 (interactive
2037 (list (read-language-name
2038 'documentation
2039 "Describe language environment (default current choice): ")))
2040 (if (null language-name)
2041 (setq language-name current-language-environment))
2042 (if (or (null language-name)
2043 (null (get-language-info language-name 'documentation)))
2044 (error "No documentation for the specified language"))
2045 (if (symbolp language-name)
2046 (setq language-name (symbol-name language-name)))
2047 (dolist (feature (get-language-info language-name 'features))
2048 (require feature))
2049 (let ((doc (get-language-info language-name 'documentation)))
2050 (help-setup-xref (list #'describe-language-environment language-name)
2051 (interactive-p))
2052 (with-output-to-temp-buffer (help-buffer)
2053 (save-excursion
2054 (set-buffer standard-output)
2055 (insert language-name " language environment\n\n")
2056 (if (stringp doc)
2057 (insert doc "\n\n"))
2058 (condition-case nil
2059 (let ((str (eval (get-language-info language-name 'sample-text))))
2060 (if (stringp str)
2061 (insert "Sample text:\n " str "\n\n")))
2062 (error nil))
2063 (let ((input-method (get-language-info language-name 'input-method))
2064 (l (copy-sequence input-method-alist)))
2065 (insert "Input methods")
2066 (when input-method
2067 (insert " (default " input-method ")")
2068 (setq input-method (assoc input-method input-method-alist))
2069 (setq l (cons input-method (delete input-method l))))
2070 (insert ":\n")
2071 (while l
2072 (when (string= language-name (nth 1 (car l)))
2073 (insert " " (car (car l)))
2074 (search-backward (car (car l)))
2075 (help-xref-button 0 'help-input-method (car (car l)))
2076 (goto-char (point-max))
2077 (insert " (\""
2078 (if (stringp (nth 3 (car l)))
2079 (nth 3 (car l))
2080 (car (nth 3 (car l))))
2081 "\" in mode line)\n"))
2082 (setq l (cdr l)))
2083 (insert "\n"))
2084 (insert "Character sets:\n")
2085 (let ((l (get-language-info language-name 'charset)))
2086 (if (null l)
2087 (insert " nothing specific to " language-name "\n")
2088 (while l
2089 (insert " " (symbol-name (car l)))
2090 (search-backward (symbol-name (car l)))
2091 (help-xref-button 0 'help-character-set (car l))
2092 (goto-char (point-max))
2093 (insert ": " (charset-description (car l)) "\n")
2094 (setq l (cdr l)))))
2095 (insert "\n")
2096 (insert "Coding systems:\n")
2097 (let ((l (get-language-info language-name 'coding-system)))
2098 (if (null l)
2099 (insert " nothing specific to " language-name "\n")
2100 (while l
2101 (insert " " (symbol-name (car l)))
2102 (search-backward (symbol-name (car l)))
2103 (help-xref-button 0 'help-coding-system (car l))
2104 (goto-char (point-max))
2105 (insert " (`"
2106 (coding-system-mnemonic (car l))
2107 "' in mode line):\n\t"
2108 (coding-system-doc-string (car l))
2109 "\n")
2110 (let ((aliases (coding-system-get (car l)
2111 'alias-coding-systems)))
2112 (when aliases
2113 (insert "\t(alias:")
2114 (while aliases
2115 (insert " " (symbol-name (car aliases)))
2116 (setq aliases (cdr aliases)))
2117 (insert ")\n")))
2118 (setq l (cdr l)))))))))
2119 \f
2120 ;;; Locales.
2121
2122 (defvar locale-translation-file-name nil
2123 "File name for the system's file of locale-name aliases, or nil if none.")
2124
2125 ;; The following definitions might as well be marked as constants and
2126 ;; purecopied, since they're normally used on startup, and probably
2127 ;; should reflect the facilities of the base Emacs.
2128 (defconst locale-language-names
2129 (purecopy
2130 '(
2131 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2132 ;; as specified in the Single Unix Spec, Version 2.
2133 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2134 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2135 ;; see Internet RFC 2165 (1997-06) and
2136 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2137 ;; TERRITORY is a country code taken from ISO 3166
2138 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2139 ;; CODESET and MODIFIER are implementation-dependent.
2140
2141 ;; jasonr comments: MS Windows uses three letter codes for
2142 ;; languages instead of the two letter ISO codes that POSIX
2143 ;; uses. In most cases the first two letters are the same, so
2144 ;; most of the regexps in locale-language-names work. Japanese
2145 ;; and Chinese are exceptions, which are listed in the
2146 ;; non-standard section at the bottom of locale-language-names.
2147
2148 ("aa_DJ" . "Latin-1") ; Afar
2149 ("aa" . "UTF-8")
2150 ;; ab Abkhazian
2151 ("af" . "Latin-1") ; Afrikaans
2152 ("am" "Ethiopic" utf-8) ; Amharic
2153 ("an" . "Latin-9") ; Aragonese
2154 ; ar Arabic glibc uses 8859-6
2155 ; as Assamese
2156 ; ay Aymara
2157 ("az" . "UTF-8") ; Azerbaijani
2158 ; ba Bashkir
2159 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2160 ("bg" "Bulgarian" cp1251) ; Bulgarian
2161 ; bh Bihari
2162 ; bi Bislama
2163 ("bn" . "UTF-8") ; Bengali, Bangla
2164 ("bo" . "Tibetan")
2165 ("br" . "Latin-1") ; Breton
2166 ("bs" . "Latin-2") ; Bosnian
2167 ("byn" . "UTF-8") ; Bilin; Blin
2168 ("ca" . "Latin-1") ; Catalan
2169 ; co Corsican
2170 ("cs" "Czech" iso-8859-2)
2171 ("cy" "Welsh" iso-8859-14)
2172 ("da" . "Latin-1") ; Danish
2173 ("de" "German" iso-8859-1)
2174 ; dv Divehi
2175 ; dz Bhutani
2176 ("el" "Greek" iso-8859-7)
2177 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2178 ;; That's actually what the GNU locales define, modulo things like
2179 ;; en_IN -- fx.
2180 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2181 ("en" "English" iso-8859-1) ; English
2182 ("eo" . "Latin-3") ; Esperanto
2183 ("es" "Spanish" iso-8859-1)
2184 ("et" . "Latin-1") ; Estonian
2185 ("eu" . "Latin-1") ; Basque
2186 ("fa" . "UTF-8") ; Persian
2187 ("fi" . "Latin-1") ; Finnish
2188 ("fj" . "Latin-1") ; Fiji
2189 ("fo" . "Latin-1") ; Faroese
2190 ("fr" "French" iso-8859-1) ; French
2191 ("fy" . "Latin-1") ; Frisian
2192 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2193 ("gd" . "Latin-9") ; Scots Gaelic
2194 ("gez" "Ethiopic" utf-8) ; Geez
2195 ("gl" . "Latin-1") ; Gallegan; Galician
2196 ; gn Guarani
2197 ("gu" . "UTF-8") ; Gujarati
2198 ("gv" . "Latin-1") ; Manx Gaelic
2199 ; ha Hausa
2200 ("he" "Hebrew" iso-8859-8)
2201 ("hi" "Devanagari" utf-8) ; Hindi
2202 ("hr" "Croatian" iso-8859-2) ; Croatian
2203 ("hu" . "Latin-2") ; Hungarian
2204 ; hy Armenian
2205 ; ia Interlingua
2206 ("id" . "Latin-1") ; Indonesian
2207 ; ie Interlingue
2208 ; ik Inupiak
2209 ("is" . "Latin-1") ; Icelandic
2210 ("it" "Italian" iso-8859-1) ; Italian
2211 ; iu Inuktitut
2212 ("iw" "Hebrew" iso-8859-8)
2213 ("ja" "Japanese" euc-jp)
2214 ; jw Javanese
2215 ("ka" "Georgian" georgian-ps) ; Georgian
2216 ; kk Kazakh
2217 ("kl" . "Latin-1") ; Greenlandic
2218 ; km Cambodian
2219 ("kn" "Kannada" utf-8)
2220 ("ko" "Korean" euc-kr)
2221 ; ks Kashmiri
2222 ; ku Kurdish
2223 ("kw" . "Latin-1") ; Cornish
2224 ; ky Kirghiz
2225 ("la" . "Latin-1") ; Latin
2226 ("lb" . "Latin-1") ; Luxemburgish
2227 ("lg" . "Laint-6") ; Ganda
2228 ; ln Lingala
2229 ("lo" "Lao" utf-8) ; Laothian
2230 ("lt" "Lithuanian" iso-8859-13)
2231 ("lv" . "Latvian") ; Latvian, Lettish
2232 ; mg Malagasy
2233 ("mi" . "Latin-7") ; Maori
2234 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2235 ("ml" "Malayalam" utf-8)
2236 ("mn" . "UTF-8") ; Mongolian
2237 ; mo Moldavian
2238 ("mr" "Devanagari" utf-8) ; Marathi
2239 ("ms" . "Latin-1") ; Malay
2240 ("mt" . "Latin-3") ; Maltese
2241 ; my Burmese
2242 ; na Nauru
2243 ("nb" . "Latin-1") ; Norwegian
2244 ("ne" "Devanagari" utf-8) ; Nepali
2245 ("nl" "Dutch" iso-8859-1)
2246 ("no" . "Latin-1") ; Norwegian
2247 ("oc" . "Latin-1") ; Occitan
2248 ("om_ET" . "UTF-8") ; (Afan) Oromo
2249 ("om" . "Latin-1") ; (Afan) Oromo
2250 ; or Oriya
2251 ("pa" . "UTF-8") ; Punjabi
2252 ("pl" . "Latin-2") ; Polish
2253 ; ps Pashto, Pushto
2254 ("pt" . "Latin-1") ; Portuguese
2255 ; qu Quechua
2256 ("rm" . "Latin-1") ; Rhaeto-Romanic
2257 ; rn Kirundi
2258 ("ro" "Romanian" iso-8859-2)
2259 ("ru_RU" "Russian" iso-8859-5)
2260 ("ru_UA" "Russian" koi8-u)
2261 ; rw Kinyarwanda
2262 ("sa" . "Devanagari") ; Sanskrit
2263 ; sd Sindhi
2264 ("se" . "UTF-8") ; Northern Sami
2265 ; sg Sangho
2266 ("sh" . "Latin-2") ; Serbo-Croatian
2267 ; si Sinhalese
2268 ("sid" . "UTF-8") ; Sidamo
2269 ("sk" "Slovak" iso-8859-2)
2270 ("sl" "Slovenian" iso-8859-2)
2271 ; sm Samoan
2272 ; sn Shona
2273 ("so_ET" "UTF-8") ; Somali
2274 ("so" "Latin-1") ; Somali
2275 ("sq" . "Latin-1") ; Albanian
2276 ("sr_YU@cyrillic" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet)
2277 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2278 ; ss Siswati
2279 ("st" . "Latin-1") ; Sesotho
2280 ; su Sundanese
2281 ("sv" "Swedish" iso-8859-1) ; Swedish
2282 ("sw" . "Latin-1") ; Swahili
2283 ("ta" "Tamil" utf-8)
2284 ("te" . "UTF-8") ; Telugu
2285 ("tg" "Tajik" koi8-t)
2286 ("th" "Thai" tis-620)
2287 ("ti" "Ethiopic" utf-8) ; Tigrinya
2288 ("tig_ER" . "UTF-8") ; Tigre
2289 ; tk Turkmen
2290 ("tl" . "Latin-1") ; Tagalog
2291 ; tn Setswana
2292 ; to Tonga
2293 ("tr" "Turkish" iso-8859-9)
2294 ; ts Tsonga
2295 ("tt" . "UTF-8") ; Tatar
2296 ; tw Twi
2297 ; ug Uighur
2298 ("uk" "Ukrainian" koi8-u)
2299 ("ur" . "UTF-8") ; Urdu
2300 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2301 ("uz" . "Latin-1") ; Uzbek
2302 ("vi" "Vietnamese" utf-8)
2303 ; vo Volapuk
2304 ("wa" . "Latin-1") ; Walloon
2305 ; wo Wolof
2306 ("xh" . "Latin-1") ; Xhosa
2307 ("yi" . "Windows-1255") ; Yiddish
2308 ; yo Yoruba
2309 ; za Zhuang
2310 ("zh_HK" . "Chinese-Big5")
2311 ("zh_TW" . "Chinese-Big5")
2312 ("zh_CN" . "Chinese-GB")
2313 ("zh" . "Chinese-GB")
2314 ; zh_CN.GB18030/GB18030 \
2315 ; zh_CN.GBK/GBK \
2316 ; zh_HK/BIG5-HKSCS \
2317 ("zu" . "Latin-1") ; Zulu
2318
2319 ;; ISO standard locales
2320 ("c$" . "ASCII")
2321 ("posix$" . "ASCII")
2322
2323 ;; The "IPA" Emacs language environment does not correspond
2324 ;; to any ISO 639 code, so let it stand for itself.
2325 ("ipa$" . "IPA")
2326
2327 ;; Nonstandard or obsolete language codes
2328 ("cz" . "Czech") ; e.g. Solaris 2.6
2329 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2330 ("iw" . "Hebrew") ; e.g. X11R6.4
2331 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2332 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2333 ("jp" . "Japanese") ; e.g. MS Windows
2334 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2335 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2336 ("gbz" . "UTF-8") ; MS Windows Dari Persian
2337 ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
2338 ("wee" . "Latin-2") ; MS Windows Lower Sorbian
2339 ("wen" . "Latin-2") ; MS Windows Upper Sorbian
2340 ))
2341 "Alist of locale regexps vs the corresponding languages and coding systems.
2342 Each element has these form:
2343 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2344 The first element whose LOCALE-REGEXP matches the start of a
2345 downcased locale specifies the LANG-ENV \(language environtment)
2346 and CODING-SYSTEM corresponding to that locale. If there is no
2347 appropriate language environment, the element may have this form:
2348 \(LOCALE-REGEXP . LANG-ENV)
2349 In this case, LANG-ENV is one of generic language environments for an
2350 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2351
2352 (defconst locale-charset-language-names
2353 (purecopy
2354 '((".*8859[-_]?1\\>" . "Latin-1")
2355 (".*8859[-_]?2\\>" . "Latin-2")
2356 (".*8859[-_]?3\\>" . "Latin-3")
2357 (".*8859[-_]?4\\>" . "Latin-4")
2358 (".*8859[-_]?9\\>" . "Latin-5")
2359 (".*8859[-_]?14\\>" . "Latin-8")
2360 (".*8859[-_]?15\\>" . "Latin-9")
2361 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2362 ;; utf-8@euro exists, so put this last. (@euro really specifies
2363 ;; the currency, rather than the charset.)
2364 (".*@euro\\>" . "Latin-9")))
2365 "List of pairs of locale regexps and charset language names.
2366 The first element whose locale regexp matches the start of a downcased locale
2367 specifies the language name whose charset corresponds to that locale.
2368 This language name is used if the locale is not listed in
2369 `locale-language-names'")
2370
2371 (defconst locale-preferred-coding-systems
2372 (purecopy
2373 '((".*8859[-_]?1\\>" . iso-8859-1)
2374 (".*8859[-_]?2\\>" . iso-8859-2)
2375 (".*8859[-_]?3\\>" . iso-8859-3)
2376 (".*8859[-_]?4\\>" . iso-8859-4)
2377 (".*8859[-_]?9\\>" . iso-8859-9)
2378 (".*8859[-_]?14\\>" . iso-8859-14)
2379 (".*8859[-_]?15\\>" . iso-8859-15)
2380 (".*utf\\(?:-?8\\)?" . utf-8)
2381 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2382 ;; specifies the currency, rather than the charset.)
2383 (".*@euro" . iso-8859-15)
2384 ("koi8-?r" . koi8-r)
2385 ("koi8-?u" . koi8-u)
2386 ("tcvn" . tcvn)
2387 ("big5" . big5)
2388 ("euc-?tw" . euc-tw)
2389 ;; We don't support GBK, but as it is upper compatible with
2390 ;; GB-2312, we setup the default coding system to gb2312.
2391 ("gbk" . gb2312)
2392 ;; We don't support BIG5-HKSCS, but as it is upper compatible with
2393 ;; BIG5, we setup the default coding system to big5.
2394 ("big5hkscs" . big5)
2395 ("ja.*[._]euc" . japanese-iso-8bit)
2396 ("ja.*[._]jis7" . iso-2022-jp)
2397 ("ja.*[._]pck" . japanese-shift-jis)
2398 ("ja.*[._]sjis" . japanese-shift-jis)
2399 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2400 ))
2401 "List of pairs of locale regexps and preferred coding systems.
2402 The first element whose locale regexp matches the start of a downcased locale
2403 specifies the coding system to prefer when using that locale.
2404 This coding system is used if the locale specifies a specific charset.")
2405
2406 (defun locale-name-match (key alist)
2407 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2408 Return the value corresponding to the first regexp that matches the
2409 start of KEY, or nil if there is no match."
2410 (let (element)
2411 (while (and alist (not element))
2412 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2413 (setq element (car alist)))
2414 (setq alist (cdr alist)))
2415 (cdr element)))
2416
2417 (defun locale-charset-match-p (charset1 charset2)
2418 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2419 Matching is done ignoring case and any hyphens and underscores in the
2420 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2421 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2422 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2423 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2424
2425 (defvar locale-charset-alist nil
2426 "Coding system alist keyed on locale-style charset name.
2427 Used by `locale-charset-to-coding-system'.")
2428
2429 (defun locale-charset-to-coding-system (charset)
2430 "Find coding system corresponding to CHARSET.
2431 CHARSET is any sort of non-Emacs charset name, such as might be used
2432 in a locale codeset, or elsewhere. It is matched to a coding system
2433 first by case-insensitive lookup in `locale-charset-alist'. Then
2434 matches are looked for in the coding system list, treating case and
2435 the characters `-' and `_' as insignificant. The coding system base
2436 is returned. Thus, for instance, if charset \"ISO8859-2\",
2437 `iso-latin-2' is returned."
2438 (or (car (assoc-string charset locale-charset-alist t))
2439 (let ((cs coding-system-alist)
2440 c)
2441 (while (and (not c) cs)
2442 (if (locale-charset-match-p charset (caar cs))
2443 (setq c (intern (caar cs)))
2444 (pop cs)))
2445 (if c (coding-system-base c)))))
2446
2447 ;; Fixme: This ought to deal with the territory part of the locale
2448 ;; too, for setting things such as calendar holidays, ps-print paper
2449 ;; size, spelling dictionary.
2450
2451 (defun set-locale-environment (&optional locale-name)
2452 "Set up multi-lingual environment for using LOCALE-NAME.
2453 This sets the language environment, the coding system priority,
2454 the default input method and sometimes other things.
2455
2456 LOCALE-NAME should be a string which is the name of a locale supported
2457 by the system. Often it is of the form xx_XX.CODE, where xx is a
2458 language, XX is a country, and CODE specifies a character set and
2459 coding system. For example, the locale name \"ja_JP.EUC\" might name
2460 a locale for Japanese in Japan using the `japanese-iso-8bit'
2461 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2462 or `@cyrillic'.
2463
2464 If LOCALE-NAME is nil, its value is taken from the environment
2465 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2466
2467 The locale names supported by your system can typically be found in a
2468 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2469 will be translated according to the table specified by
2470 `locale-translation-file-name'.
2471
2472 See also `locale-charset-language-names', `locale-language-names',
2473 `locale-preferred-coding-systems' and `locale-coding-system'."
2474 (interactive "sSet environment for locale: ")
2475
2476 ;; Do this at runtime for the sake of binaries possibly transported
2477 ;; to a system without X.
2478 (setq locale-translation-file-name
2479 (let ((files
2480 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2481 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2482 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2483 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2484 ;;
2485 ;; The following name appears after the X-related names above,
2486 ;; since the X-related names are what X actually uses.
2487 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2488 )))
2489 (while (and files (not (file-exists-p (car files))))
2490 (setq files (cdr files)))
2491 (car files)))
2492
2493 (let ((locale locale-name))
2494
2495 (unless locale
2496 ;; Use the first of these three environment variables
2497 ;; that has a nonempty value.
2498 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2499 (while (and vars
2500 (= 0 (length locale))) ; nil or empty string
2501 (setq locale (getenv (pop vars))))))
2502
2503 (unless locale
2504 ;; The two tests are kept separate so the byte-compiler sees
2505 ;; that mac-get-preference is only called after checking its existence.
2506 (when (fboundp 'mac-get-preference)
2507 (setq locale (mac-get-preference "AppleLocale"))
2508 (unless locale
2509 (let ((languages (mac-get-preference "AppleLanguages")))
2510 (unless (= (length languages) 0) ; nil or empty vector
2511 (setq locale (aref languages 0)))))))
2512 (unless (or locale (not (boundp 'mac-system-locale)))
2513 (setq locale mac-system-locale))
2514
2515 (when locale
2516
2517 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2518 ;; using the translation file that many systems have.
2519 (when locale-translation-file-name
2520 (with-temp-buffer
2521 (insert-file-contents locale-translation-file-name)
2522 (when (re-search-forward
2523 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2524 (setq locale (buffer-substring (point) (line-end-position))))))
2525
2526 ;; Leave the system locales alone if the caller did not specify
2527 ;; an explicit locale name, as their defaults are set from
2528 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2529 ;; want to set them to the same value as LC_CTYPE.
2530 (when locale-name
2531 (setq system-messages-locale locale)
2532 (setq system-time-locale locale))
2533
2534 (setq locale (downcase locale))
2535
2536 (let ((language-name
2537 (locale-name-match locale locale-language-names))
2538 (charset-language-name
2539 (locale-name-match locale locale-charset-language-names))
2540 (default-eol-type (coding-system-eol-type
2541 default-buffer-file-coding-system))
2542 (coding-system
2543 (or (locale-name-match locale locale-preferred-coding-systems)
2544 (when locale
2545 (if (string-match "\\.\\([^@]+\\)" locale)
2546 (locale-charset-to-coding-system
2547 (match-string 1 locale))))
2548 (and (eq system-type 'macos) mac-system-coding-system))))
2549
2550 (if (consp language-name)
2551 ;; locale-language-names specify both lang-env and coding.
2552 ;; But, what specified in locale-preferred-coding-systems
2553 ;; has higher priority.
2554 (setq coding-system (or coding-system
2555 (nth 1 language-name))
2556 language-name (car language-name))
2557 ;; Otherwise, if locale is not listed in locale-language-names,
2558 ;; use what listed in locale-charset-language-names.
2559 (if (not language-name)
2560 (setq language-name charset-language-name)))
2561
2562 ;; If a specific EOL conversion was specified in the default
2563 ;; buffer-file-coding-system, preserve it in the coding system
2564 ;; we will be using from now on.
2565 (if (and (memq default-eol-type '(0 1 2 unix dos mac))
2566 coding-system
2567 (coding-system-p coding-system))
2568 (setq coding-system (coding-system-change-eol-conversion
2569 coding-system default-eol-type)))
2570
2571 (when language-name
2572
2573 ;; Set up for this character set. This is now the right way
2574 ;; to do it for both unibyte and multibyte modes.
2575 (set-language-environment language-name)
2576
2577 ;; If default-enable-multibyte-characters is nil,
2578 ;; we are using single-byte characters,
2579 ;; so the display table and terminal coding system are irrelevant.
2580 (when default-enable-multibyte-characters
2581 (set-display-table-and-terminal-coding-system
2582 language-name coding-system))
2583
2584 ;; Set the `keyboard-coding-system' if appropriate (tty
2585 ;; only). At least X and MS Windows can generate
2586 ;; multilingual input.
2587 (unless window-system
2588 (let ((kcs (or coding-system
2589 (car (get-language-info language-name
2590 'coding-system)))))
2591 (if kcs (set-keyboard-coding-system kcs))))
2592
2593 (setq locale-coding-system
2594 (car (get-language-info language-name 'coding-priority))))
2595
2596 (when (and coding-system
2597 (not (coding-system-equal coding-system
2598 locale-coding-system)))
2599 (prefer-coding-system coding-system)
2600 (setq locale-coding-system coding-system))))
2601
2602 ;; On Windows, override locale-coding-system,
2603 ;; keyboard-coding-system with system codepage. Note:
2604 ;; selection-coding-system is already set in w32select.c.
2605 (when (boundp 'w32-ansi-code-page)
2606 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2607 (when (coding-system-p code-page-coding)
2608 (setq locale-coding-system code-page-coding)
2609 (set-keyboard-coding-system code-page-coding)
2610 (set-terminal-coding-system code-page-coding))))
2611
2612 (when (eq system-type 'darwin)
2613 ;; On Darwin, file names are always encoded in utf-8, no matter
2614 ;; the locale.
2615 (setq default-file-name-coding-system 'utf-8)
2616 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2617 ;; the locale.
2618 (when (and (null window-system)
2619 (equal (getenv "TERM_PROGRAM") "Apple_Terminal"))
2620 (set-terminal-coding-system 'utf-8)
2621 (set-keyboard-coding-system 'utf-8)))
2622
2623 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2624 ;; (See comments in Flocale_info.)
2625 (let ((locale locale)
2626 (paper (locale-info 'paper)))
2627 (if paper
2628 ;; This will always be null at the time of writing.
2629 (cond
2630 ((equal paper '(216 279))
2631 (setq ps-paper-type 'letter))
2632 ((equal paper '(210 297))
2633 (setq ps-paper-type 'a4)))
2634 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2635 (while (and vars (= 0 (length locale)))
2636 (setq locale (getenv (pop vars)))))
2637 (when locale
2638 ;; As of glibc 2.2.5, these are the only US Letter locales,
2639 ;; and the rest are A4.
2640 (setq ps-paper-type
2641 (or (locale-name-match locale '(("c$" . letter)
2642 ("posix$" . letter)
2643 (".._us" . letter)
2644 (".._pr" . letter)
2645 (".._ca" . letter)
2646 ("enu$" . letter) ; Windows
2647 ("esu$" . letter)
2648 ("enc$" . letter)
2649 ("frc$" . letter)))
2650 'a4))))))
2651 nil)
2652 \f
2653 ;;; Charset property
2654
2655 (defun get-charset-property (charset propname)
2656 "Return the value of CHARSET's PROPNAME property.
2657 This is the last value stored with
2658 (put-charset-property CHARSET PROPNAME VALUE)."
2659 (and (not (eq charset 'composition))
2660 (plist-get (charset-plist charset) propname)))
2661
2662 (defun put-charset-property (charset propname value)
2663 "Store CHARSETS's PROPNAME property with value VALUE.
2664 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
2665 (or (eq charset 'composition)
2666 (set-charset-plist charset
2667 (plist-put (charset-plist charset) propname value))))
2668
2669 ;;; Character code property
2670 (put 'char-code-property-table 'char-table-extra-slots 0)
2671
2672 (defvar char-code-property-table
2673 (make-char-table 'char-code-property-table)
2674 "Char-table containing a property list of each character code.
2675
2676 See also the documentation of `get-char-code-property' and
2677 `put-char-code-property'.")
2678
2679 (defun get-char-code-property (char propname)
2680 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2681 (let ((plist (aref char-code-property-table char)))
2682 (if (listp plist)
2683 (car (cdr (memq propname plist))))))
2684
2685 (defun put-char-code-property (char propname value)
2686 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2687 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2688 (let ((plist (aref char-code-property-table char)))
2689 (if plist
2690 (let ((slot (memq propname plist)))
2691 (if slot
2692 (setcar (cdr slot) value)
2693 (nconc plist (list propname value))))
2694 (aset char-code-property-table char (list propname value)))))
2695
2696 \f
2697 ;; Pretty description of encoded string
2698
2699 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2700 (defvar iso-2022-control-alist
2701 '((?\x1b . "ESC")
2702 (?\x0e . "SO")
2703 (?\x0f . "SI")
2704 (?\x8e . "SS2")
2705 (?\x8f . "SS3")
2706 (?\x9b . "CSI")))
2707
2708 (defun encoded-string-description (str coding-system)
2709 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2710 (setq str (string-as-unibyte str))
2711 (mapconcat
2712 (if (and coding-system (eq (coding-system-type coding-system) 2))
2713 ;; Try to get a pretty description for ISO 2022 escape sequences.
2714 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2715 (format "#x%02X" x))))
2716 (function (lambda (x) (format "#x%02X" x))))
2717 str " "))
2718
2719 (defun encode-coding-char (char coding-system)
2720 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2721 If CODING-SYSTEM can't safely encode CHAR, return nil."
2722 (let ((str1 (string-as-multibyte (char-to-string char)))
2723 (str2 (string-as-multibyte (make-string 2 char)))
2724 (safe-chars (and coding-system
2725 (coding-system-get coding-system 'safe-chars)))
2726 (charset (char-charset char))
2727 enc1 enc2 i1 i2)
2728 (when (or (eq safe-chars t)
2729 (eq charset 'ascii)
2730 (and safe-chars (aref safe-chars char)))
2731 ;; We must find the encoded string of CHAR. But, just encoding
2732 ;; CHAR will put extra control sequences (usually to designate
2733 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2734 ;; To exclude such tailing bytes, we at first encode one-char
2735 ;; string and two-char string, then check how many bytes at the
2736 ;; tail of both encoded strings are the same.
2737
2738 (setq enc1 (encode-coding-string str1 coding-system)
2739 i1 (length enc1)
2740 enc2 (encode-coding-string str2 coding-system)
2741 i2 (length enc2))
2742 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2743 (setq i1 (1- i1) i2 (1- i2)))
2744
2745 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2746 ;; and they are the extra control sequences at the tail to
2747 ;; exclude.
2748 (substring enc2 0 i2))))
2749
2750
2751 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2752 ;;; mule-cmds.el ends here