]> code.delx.au - gnu-emacs/blob - lisp/international/mule-cmds.el
Merged from emacs@sv.gnu.org. Last-minute emacsclient rewrites be damned!
[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 initial-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 INPUT-METHOD and LANG-ENV are symbols or strings.
1362
1363 The remaining arguments are:
1364 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
1365 ACTIVATE-FUNC is a function to call to activate this method.
1366 TITLE is a string to show in the mode line when this method is active.
1367 DESCRIPTION is a string describing this method and what it is good for.
1368 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1369 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1370
1371 This function is mainly used in the file \"leim-list.el\" which is
1372 created at Emacs build time, registering all Quail input methods
1373 contained in the Emacs distribution.
1374
1375 In case you want to register a new Quail input method by yourself, be
1376 careful to use the same input method title as given in the third
1377 parameter of `quail-define-package'. (If the values are different, the
1378 string specified in this function takes precedence.)
1379
1380 The commands `describe-input-method' and `list-input-methods' need
1381 these duplicated values to show some information about input methods
1382 without loading the relevant Quail packages."
1383 (if (symbolp lang-env)
1384 (setq lang-env (symbol-name lang-env)))
1385 (if (symbolp input-method)
1386 (setq input-method (symbol-name input-method)))
1387 (let ((info (cons lang-env args))
1388 (slot (assoc input-method input-method-alist)))
1389 (if slot
1390 (setcdr slot info)
1391 (setq slot (cons input-method info))
1392 (setq input-method-alist (cons slot input-method-alist)))))
1393
1394 (defun read-input-method-name (prompt &optional default inhibit-null)
1395 "Read a name of input method from a minibuffer prompting with PROMPT.
1396 If DEFAULT is non-nil, use that as the default,
1397 and substitute it into PROMPT at the first `%s'.
1398 If INHIBIT-NULL is non-nil, null input signals an error.
1399
1400 The return value is a string."
1401 (if default
1402 (setq prompt (format prompt default)))
1403 (let* ((completion-ignore-case t)
1404 ;; As it is quite normal to change input method in the
1405 ;; minibuffer, we must enable it even if
1406 ;; enable-recursive-minibuffers is currently nil.
1407 (enable-recursive-minibuffers t)
1408 ;; This binding is necessary because input-method-history is
1409 ;; buffer local.
1410 (input-method (completing-read prompt input-method-alist
1411 nil t nil 'input-method-history
1412 default)))
1413 (if (and input-method (symbolp input-method))
1414 (setq input-method (symbol-name input-method)))
1415 (if (> (length input-method) 0)
1416 input-method
1417 (if inhibit-null
1418 (error "No valid input method is specified")))))
1419
1420 (defun activate-input-method (input-method)
1421 "Switch to input method INPUT-METHOD for the current buffer.
1422 If some other input method is already active, turn it off first.
1423 If INPUT-METHOD is nil, deactivate any current input method."
1424 (if (and input-method (symbolp input-method))
1425 (setq input-method (symbol-name input-method)))
1426 (if (and current-input-method
1427 (not (string= current-input-method input-method)))
1428 (inactivate-input-method))
1429 (unless (or current-input-method (null input-method))
1430 (let ((slot (assoc input-method input-method-alist)))
1431 (if (null slot)
1432 (error "Can't activate input method `%s'" input-method))
1433 (setq current-input-method-title nil)
1434 (let ((func (nth 2 slot)))
1435 (if (functionp func)
1436 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1437 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1438 (progn
1439 (require (cdr func))
1440 (apply (car func) input-method (nthcdr 5 slot)))
1441 (error "Can't activate input method `%s'" input-method))))
1442 (setq current-input-method input-method)
1443 (or (stringp current-input-method-title)
1444 (setq current-input-method-title (nth 3 slot)))
1445 (unwind-protect
1446 (run-hooks 'input-method-activate-hook)
1447 (force-mode-line-update)))))
1448
1449 (defun inactivate-input-method ()
1450 "Turn off the current input method."
1451 (when current-input-method
1452 (if input-method-history
1453 (unless (string= current-input-method (car input-method-history))
1454 (setq input-method-history
1455 (cons current-input-method
1456 (delete current-input-method input-method-history))))
1457 (setq input-method-history (list current-input-method)))
1458 (unwind-protect
1459 (funcall inactivate-current-input-method-function)
1460 (unwind-protect
1461 (run-hooks 'input-method-inactivate-hook)
1462 (setq current-input-method nil
1463 input-method-function nil
1464 current-input-method-title nil)
1465 (force-mode-line-update)))))
1466
1467 (defun set-input-method (input-method &optional interactive)
1468 "Select and activate input method INPUT-METHOD for the current buffer.
1469 This also sets the default input method to the one you specify.
1470 If INPUT-METHOD is nil, this function turns off the input method, and
1471 also causes you to be prompted for a name of an input method the next
1472 time you invoke \\[toggle-input-method].
1473 When called interactively, the optional arg INTERACTIVE is non-nil,
1474 which marks the variable `default-input-method' as set for Custom buffers.
1475
1476 To deactivate the input method interactively, use \\[toggle-input-method].
1477 To deactivate it programmatically, use \\[inactivate-input-method]."
1478 (interactive
1479 (let* ((default (or (car input-method-history) default-input-method)))
1480 (list (read-input-method-name
1481 (if default "Select input method (default %s): " "Select input method: ")
1482 default t)
1483 t)))
1484 (activate-input-method input-method)
1485 (setq default-input-method input-method)
1486 (when interactive
1487 (customize-mark-as-set 'default-input-method))
1488 default-input-method)
1489
1490 (defun toggle-input-method (&optional arg interactive)
1491 "Enable or disable multilingual text input method for the current buffer.
1492 Only one input method can be enabled at any time in a given buffer.
1493
1494 The normal action is to enable an input method if none was
1495 enabled, and disable the current one otherwise. Which input method
1496 to enable can be determined in various ways--either the one most
1497 recently used, or the one specified by `default-input-method', or
1498 as a last resort by reading the name of an input method in the
1499 minibuffer.
1500
1501 With a prefix argument, read an input method name with the minibuffer
1502 and enable that one. The default is the most recent input method specified
1503 \(not including the currently active input method, if any).
1504
1505 When called interactively, the optional arg INTERACTIVE is non-nil,
1506 which marks the variable `default-input-method' as set for Custom buffers."
1507
1508 (interactive "P\np")
1509 (if (and current-input-method (not arg))
1510 (inactivate-input-method)
1511 (let ((default (or (car input-method-history) default-input-method)))
1512 (if (and arg default (equal current-input-method default)
1513 (> (length input-method-history) 1))
1514 (setq default (nth 1 input-method-history)))
1515 (activate-input-method
1516 (if (or arg (not default))
1517 (progn
1518 (read-input-method-name
1519 (if default "Input method (default %s): " "Input method: " )
1520 default t))
1521 default))
1522 (unless default-input-method
1523 (prog1
1524 (setq default-input-method current-input-method)
1525 (when interactive
1526 (customize-mark-as-set 'default-input-method)))))))
1527
1528 (defun describe-input-method (input-method)
1529 "Describe input method INPUT-METHOD."
1530 (interactive
1531 (list (read-input-method-name
1532 "Describe input method (default current choice): ")))
1533 (if (and input-method (symbolp input-method))
1534 (setq input-method (symbol-name input-method)))
1535 (help-setup-xref (list #'describe-input-method
1536 (or input-method current-input-method))
1537 (interactive-p))
1538
1539 (if (null input-method)
1540 (describe-current-input-method)
1541 (let ((current current-input-method))
1542 (condition-case nil
1543 (progn
1544 (save-excursion
1545 (activate-input-method input-method)
1546 (describe-current-input-method))
1547 (activate-input-method current))
1548 (error
1549 (activate-input-method current)
1550 (help-setup-xref (list #'describe-input-method input-method)
1551 (interactive-p))
1552 (with-output-to-temp-buffer (help-buffer)
1553 (let ((elt (assoc input-method input-method-alist)))
1554 (princ (format
1555 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1556 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1557
1558 (defun describe-current-input-method ()
1559 "Describe the input method currently in use.
1560 This is a subroutine for `describe-input-method'."
1561 (if current-input-method
1562 (if (and (symbolp describe-current-input-method-function)
1563 (fboundp describe-current-input-method-function))
1564 (funcall describe-current-input-method-function)
1565 (message "No way to describe the current input method `%s'"
1566 current-input-method)
1567 (ding))
1568 (error "No input method is activated now")))
1569
1570 (defun read-multilingual-string (prompt &optional initial-input input-method)
1571 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1572 The input method selected last time is activated in minibuffer.
1573 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1574 initially.
1575 Optional 3rd argument INPUT-METHOD specifies the input method
1576 to be activated instead of the one selected last time. It is a symbol
1577 or a string."
1578 (setq input-method
1579 (or input-method
1580 current-input-method
1581 default-input-method
1582 (read-input-method-name "Input method: " nil t)))
1583 (if (and input-method (symbolp input-method))
1584 (setq input-method (symbol-name input-method)))
1585 (let ((prev-input-method current-input-method))
1586 (unwind-protect
1587 (progn
1588 (activate-input-method input-method)
1589 (read-string prompt initial-input nil nil t))
1590 (activate-input-method prev-input-method))))
1591
1592 ;; Variables to control behavior of input methods. All input methods
1593 ;; should react to these variables.
1594
1595 (defcustom input-method-verbose-flag 'default
1596 "*A flag to control extra guidance given by input methods.
1597 The value should be nil, t, `complex-only', or `default'.
1598
1599 The extra guidance is done by showing list of available keys in echo
1600 area. When you use the input method in the minibuffer, the guidance
1601 is shown at the bottom short window (split from the existing window).
1602
1603 If the value is t, extra guidance is always given, if the value is
1604 nil, extra guidance is always suppressed.
1605
1606 If the value is `complex-only', only complex input methods such as
1607 `chinese-py' and `japanese' give extra guidance.
1608
1609 If the value is `default', complex input methods always give extra
1610 guidance, but simple input methods give it only when you are not in
1611 the minibuffer.
1612
1613 See also the variable `input-method-highlight-flag'."
1614 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1615 (const complex-only) (const default))
1616 :group 'mule)
1617
1618 (defcustom input-method-highlight-flag t
1619 "*If this flag is non-nil, input methods highlight partially-entered text.
1620 For instance, while you are in the middle of a Quail input method sequence,
1621 the text inserted so far is temporarily underlined.
1622 The underlining goes away when you finish or abort the input method sequence.
1623 See also the variable `input-method-verbose-flag'."
1624 :type 'boolean
1625 :group 'mule)
1626
1627 (defvar input-method-activate-hook nil
1628 "Normal hook run just after an input method is activated.
1629
1630 The variable `current-input-method' keeps the input method name
1631 just activated.")
1632
1633 (defvar input-method-inactivate-hook nil
1634 "Normal hook run just after an input method is inactivated.
1635
1636 The variable `current-input-method' still keeps the input method name
1637 just inactivated.")
1638
1639 (defvar input-method-after-insert-chunk-hook nil
1640 "Normal hook run just after an input method insert some chunk of text.")
1641
1642 (defvar input-method-exit-on-first-char nil
1643 "This flag controls when an input method returns.
1644 Usually, the input method does not return while there's a possibility
1645 that it may find a different translation if a user types another key.
1646 But, it this flag is non-nil, the input method returns as soon as
1647 the current key sequence gets long enough to have some valid translation.")
1648
1649 (defvar input-method-use-echo-area nil
1650 "This flag controls how an input method shows an intermediate key sequence.
1651 Usually, the input method inserts the intermediate key sequence,
1652 or candidate translations corresponding to the sequence,
1653 at point in the current buffer.
1654 But, if this flag is non-nil, it displays them in echo area instead.")
1655
1656 (defvar input-method-exit-on-invalid-key nil
1657 "This flag controls the behavior of an input method on invalid key input.
1658 Usually, when a user types a key which doesn't start any character
1659 handled by the input method, the key is handled by turning off the
1660 input method temporarily. After that key, the input method is re-enabled.
1661 But, if this flag is non-nil, the input method is never back on.")
1662
1663 \f
1664 (defvar set-language-environment-hook nil
1665 "Normal hook run after some language environment is set.
1666
1667 When you set some hook function here, that effect usually should not
1668 be inherited to another language environment. So, you had better set
1669 another function in `exit-language-environment-hook' (which see) to
1670 cancel the effect.")
1671
1672 (defvar exit-language-environment-hook nil
1673 "Normal hook run after exiting from some language environment.
1674 When this hook is run, the variable `current-language-environment'
1675 is still bound to the language environment being exited.
1676
1677 This hook is mainly used for canceling the effect of
1678 `set-language-environment-hook' (which-see).")
1679
1680 (put 'setup-specified-language-environment 'apropos-inhibit t)
1681
1682 (defun setup-specified-language-environment ()
1683 "Switch to a specified language environment."
1684 (interactive)
1685 (let (language-name)
1686 (if (and (symbolp last-command-event)
1687 (or (not (eq last-command-event 'Default))
1688 (setq last-command-event 'English))
1689 (setq language-name (symbol-name last-command-event)))
1690 (prog1
1691 (set-language-environment language-name)
1692 (customize-mark-as-set 'current-language-environment))
1693 (error "Bogus calling sequence"))))
1694
1695 (defcustom current-language-environment "English"
1696 "The last language environment specified with `set-language-environment'.
1697 This variable should be set only with \\[customize], which is equivalent
1698 to using the function `set-language-environment'."
1699 :link '(custom-manual "(emacs)Language Environments")
1700 :set (lambda (symbol value) (set-language-environment value))
1701 :get (lambda (x)
1702 (or (car-safe (assoc-string
1703 (if (symbolp current-language-environment)
1704 (symbol-name current-language-environment)
1705 current-language-environment)
1706 language-info-alist t))
1707 "English"))
1708 ;; custom type will be updated with `set-language-info'.
1709 :type (if language-info-alist
1710 (cons 'choice (mapcar
1711 (lambda (lang)
1712 (list 'const lang))
1713 (sort (mapcar 'car language-info-alist) 'string<)))
1714 'string)
1715 :initialize 'custom-initialize-default
1716 :group 'mule)
1717
1718 (defun reset-language-environment ()
1719 "Reset multilingual environment of Emacs to the default status.
1720
1721 The default status is as follows:
1722
1723 The default value of `buffer-file-coding-system' is nil.
1724 The default coding system for process I/O is nil.
1725 The default value for the command `set-terminal-coding-system' is nil.
1726 The default value for the command `set-keyboard-coding-system' is nil.
1727
1728 The order of priorities of coding categories and the coding system
1729 bound to each category are as follows
1730 coding category coding system
1731 --------------------------------------------------
1732 coding-category-iso-8-1 iso-latin-1
1733 coding-category-iso-8-2 iso-latin-1
1734 coding-category-utf-8 mule-utf-8
1735 coding-category-utf-16-be mule-utf-16be-with-signature
1736 coding-category-utf-16-le mule-utf-16le-with-signature
1737 coding-category-iso-7-tight iso-2022-jp
1738 coding-category-iso-7 iso-2022-7bit
1739 coding-category-iso-7-else iso-2022-7bit-lock
1740 coding-category-iso-8-else iso-2022-8bit-ss2
1741 coding-category-emacs-mule emacs-mule
1742 coding-category-raw-text raw-text
1743 coding-category-sjis japanese-shift-jis
1744 coding-category-big5 chinese-big5
1745 coding-category-ccl nil
1746 coding-category-binary no-conversion"
1747 (interactive)
1748 ;; This function formerly set default-enable-multibyte-characters to t,
1749 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1750
1751 (setq coding-category-iso-7-tight 'iso-2022-jp
1752 coding-category-iso-7 'iso-2022-7bit
1753 coding-category-iso-8-1 'iso-latin-1
1754 coding-category-iso-8-2 'iso-latin-1
1755 coding-category-iso-7-else 'iso-2022-7bit-lock
1756 coding-category-iso-8-else 'iso-2022-8bit-ss2
1757 coding-category-emacs-mule 'emacs-mule
1758 coding-category-raw-text 'raw-text
1759 coding-category-sjis 'japanese-shift-jis
1760 coding-category-big5 'chinese-big5
1761 coding-category-utf-16-be 'mule-utf-16be-with-signature
1762 coding-category-utf-16-le 'mule-utf-16le-with-signature
1763 coding-category-utf-8 'mule-utf-8
1764 coding-category-ccl nil
1765 coding-category-binary 'no-conversion)
1766
1767 (set-coding-priority
1768 '(coding-category-iso-8-1
1769 coding-category-iso-8-2
1770 coding-category-utf-8
1771 coding-category-utf-16-be
1772 coding-category-utf-16-le
1773 coding-category-iso-7-tight
1774 coding-category-iso-7
1775 coding-category-iso-7-else
1776 coding-category-iso-8-else
1777 coding-category-emacs-mule
1778 coding-category-raw-text
1779 coding-category-sjis
1780 coding-category-big5
1781 coding-category-ccl
1782 coding-category-binary))
1783
1784 ;; Changing the binding of a coding category requires this call.
1785 (update-coding-systems-internal)
1786
1787 (set-default-coding-systems nil)
1788 (setq default-sendmail-coding-system 'iso-latin-1)
1789 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1790 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1791 (setq default-file-name-coding-system 'iso-latin-1)
1792 ;; Preserve eol-type from existing default-process-coding-systems.
1793 ;; On non-unix-like systems in particular, these may have been set
1794 ;; carefully by the user, or by the startup code, to deal with the
1795 ;; users shell appropriately, so should not be altered by changing
1796 ;; language environment.
1797 (let ((output-coding
1798 ;; When bootstrapping, coding-systems are not defined yet, so
1799 ;; we need to catch the error from check-coding-system.
1800 (condition-case nil
1801 (coding-system-change-text-conversion
1802 (car default-process-coding-system) 'undecided)
1803 (coding-system-error 'undecided)))
1804 (input-coding
1805 (condition-case nil
1806 (coding-system-change-text-conversion
1807 (cdr default-process-coding-system) 'iso-latin-1)
1808 (coding-system-error 'iso-latin-1))))
1809 (setq default-process-coding-system
1810 (cons output-coding input-coding)))
1811
1812 ;; Don't alter the terminal and keyboard coding systems here.
1813 ;; The terminal still supports the same coding system
1814 ;; that it supported a minute ago.
1815 ;; (set-terminal-coding-system-internal nil)
1816 ;; (set-keyboard-coding-system-internal nil)
1817
1818 (setq nonascii-translation-table nil
1819 nonascii-insert-offset 0)
1820
1821 ;; Don't invoke fontset-related functions if fontsets aren't
1822 ;; supported in this build of Emacs.
1823 (and (fboundp 'fontset-list)
1824 (set-overriding-fontspec-internal nil)))
1825
1826 (reset-language-environment)
1827
1828 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system display)
1829 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1830 (let ((coding (get-language-info language-name 'unibyte-display)))
1831 (if (and coding
1832 (or (not coding-system)
1833 (coding-system-equal coding coding-system)))
1834 (standard-display-european-internal)
1835 ;; The following 2 lines undo the 8-bit display that we set up
1836 ;; in standard-display-european-internal, which see. This is in
1837 ;; case the user has used standard-display-european earlier in
1838 ;; this session. (The MS-DOS port doesn't use that setup, so it
1839 ;; doesn't need to undo it.)
1840 (when standard-display-table
1841 (dotimes (i 128)
1842 (aset standard-display-table (+ i 128) nil))))
1843 (or (eq window-system 'pc)
1844 (set-terminal-coding-system (or coding-system coding) display))))
1845
1846 (defun set-language-environment (language-name)
1847 "Set up multi-lingual environment for using LANGUAGE-NAME.
1848 This sets the coding system priority and the default input method
1849 and sometimes other things. LANGUAGE-NAME should be a string
1850 which is the name of a language environment. For example, \"Latin-1\"
1851 specifies the character set for the major languages of Western Europe."
1852 (interactive (list (read-language-name
1853 nil
1854 "Set language environment (default English): ")))
1855 (if language-name
1856 (if (symbolp language-name)
1857 (setq language-name (symbol-name language-name)))
1858 (setq language-name "English"))
1859 (let ((slot (assoc-string language-name language-info-alist t)))
1860 (unless slot
1861 (error "Language environment not defined: %S" language-name))
1862 (setq language-name (car slot)))
1863 (if current-language-environment
1864 (let ((func (get-language-info current-language-environment
1865 'exit-function)))
1866 (run-hooks 'exit-language-environment-hook)
1867 (if (functionp func) (funcall func))))
1868
1869 (reset-language-environment)
1870 ;; The features might set up coding systems.
1871 (let ((required-features (get-language-info language-name 'features)))
1872 (while required-features
1873 (require (car required-features))
1874 (setq required-features (cdr required-features))))
1875
1876 (setq current-language-environment language-name)
1877
1878 (set-language-environment-coding-systems language-name)
1879 (set-language-environment-input-method language-name)
1880 (set-language-environment-nonascii-translation language-name)
1881 (set-language-environment-charset language-name)
1882 (set-language-environment-fontset language-name)
1883 ;; Unibyte setups if necessary.
1884 (unless default-enable-multibyte-characters
1885 (set-language-environment-unibyte language-name))
1886
1887 (let ((func (get-language-info language-name 'setup-function)))
1888 (if (functionp func)
1889 (funcall func)))
1890
1891 (run-hooks 'set-language-environment-hook)
1892 (force-mode-line-update t))
1893
1894 (defun standard-display-european-internal ()
1895 ;; Actually set up direct output of non-ASCII characters.
1896 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1897 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1898 ;; the native font, and codes 160 and 146 stand for something very
1899 ;; different there.
1900 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1901 (progn
1902 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1903 (unless (and (eq window-system 'x)
1904 ;; XFree86 4 has fixed the fonts.
1905 (string= "The XFree86 Project, Inc" (x-server-vendor))
1906 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1907 ?3))
1908 ;; Make non-line-break space display as a plain space.
1909 (aset standard-display-table 160 [32]))
1910 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1911 ;; don't contain a character at that position. Map it to the ASCII
1912 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1913 ;; U+2019, normally from the windows-1252 character set. XFree 4
1914 ;; fonts probably have the appropriate glyph at this position,
1915 ;; so they could use standard-display-8bit. It's better to use a
1916 ;; proper windows-1252 coding system. --fx]
1917 (aset standard-display-table 146 [39]))))
1918
1919 (defun set-language-environment-coding-systems (language-name)
1920 "Do various coding system setups for language environment LANGUAGE-NAME."
1921 (let* ((priority (get-language-info language-name 'coding-priority))
1922 (default-coding (car priority))
1923 (eol-type (coding-system-eol-type default-buffer-file-coding-system)))
1924 (if priority
1925 (let ((categories (mapcar 'coding-system-category priority)))
1926 (set-default-coding-systems
1927 (if (memq eol-type '(0 1 2 unix dos mac))
1928 (coding-system-change-eol-conversion default-coding eol-type)
1929 default-coding))
1930 (setq default-sendmail-coding-system default-coding)
1931 (set-coding-priority categories)
1932 (while priority
1933 (set (car categories) (car priority))
1934 (setq priority (cdr priority) categories (cdr categories)))
1935 ;; Changing the binding of a coding category requires this call.
1936 (update-coding-systems-internal)))))
1937
1938 (defun set-language-environment-input-method (language-name)
1939 "Do various input method setups for language environment LANGUAGE-NAME."
1940 (let ((input-method (get-language-info language-name 'input-method)))
1941 (when input-method
1942 (setq default-input-method input-method)
1943 (if input-method-history
1944 (setq input-method-history
1945 (cons input-method
1946 (delete input-method input-method-history)))))))
1947
1948 (defun set-language-environment-nonascii-translation (language-name)
1949 "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
1950 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1951 (dos-table
1952 (if (eq window-system 'pc)
1953 (intern
1954 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1955 (cond
1956 ((char-table-p nonascii)
1957 (setq nonascii-translation-table nonascii))
1958 ((and (eq window-system 'pc) (boundp dos-table))
1959 ;; DOS terminals' default is to use a special non-ASCII translation
1960 ;; table as appropriate for the installed codepage.
1961 (setq nonascii-translation-table (symbol-value dos-table)))
1962 ((charsetp nonascii)
1963 (setq nonascii-insert-offset (- (make-char nonascii) 128))))))
1964
1965 (defun set-language-environment-charset (language-name)
1966 "Do various charset setups for language environment LANGUAGE-NAME."
1967 (if (and utf-translate-cjk-mode
1968 (not (eq utf-translate-cjk-lang-env language-name))
1969 (catch 'tag
1970 (dolist (charset (get-language-info language-name 'charset))
1971 (if (memq charset utf-translate-cjk-charsets)
1972 (throw 'tag t)))
1973 nil))
1974 (utf-translate-cjk-load-tables)))
1975
1976 (defun set-language-environment-fontset (language-name)
1977 "Do various fontset setups for language environment LANGUAGE-NAME."
1978 ;; Don't invoke fontset-related functions if fontsets aren't
1979 ;; supported in this build of Emacs.
1980 (if (fboundp 'fontset-list)
1981 (set-overriding-fontspec-internal
1982 (get-language-info language-name 'overriding-fontspec))))
1983
1984 (defun set-language-environment-unibyte (language-name)
1985 "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
1986 ;; Syntax and case table.
1987 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1988 (if syntax
1989 (let ((set-case-syntax-set-multibyte nil))
1990 (load syntax nil t))
1991 ;; No information for syntax and case. Reset to the defaults.
1992 (let ((syntax-table (standard-syntax-table))
1993 (standard-table (standard-case-table))
1994 (case-table (make-char-table 'case-table))
1995 (ch (if (eq window-system 'pc) 128 160)))
1996 (while (< ch 256)
1997 (modify-syntax-entry ch " " syntax-table)
1998 (setq ch (1+ ch)))
1999 (dotimes (i 128)
2000 (aset case-table i (aref standard-table i)))
2001 (set-char-table-extra-slot case-table 0 nil)
2002 (set-char-table-extra-slot case-table 1 nil)
2003 (set-char-table-extra-slot case-table 2 nil)
2004 (set-standard-case-table case-table))
2005 (let ((list (buffer-list)))
2006 (while list
2007 (with-current-buffer (car list)
2008 (set-case-table (standard-case-table)))
2009 (setq list (cdr list))))))
2010 (set-display-table-and-terminal-coding-system language-name))
2011
2012 (defsubst princ-list (&rest args)
2013 "Print all arguments with `princ', then print \"\n\"."
2014 (while args (princ (car args)) (setq args (cdr args)))
2015 (princ "\n"))
2016
2017 (put 'describe-specified-language-support 'apropos-inhibit t)
2018
2019 ;; Print language-specific information such as input methods,
2020 ;; charsets, and coding systems. This function is intended to be
2021 ;; called from the menu:
2022 ;; [menu-bar mule describe-language-environment LANGUAGE]
2023 ;; and should not run it by `M-x describe-current-input-method-function'.
2024 (defun describe-specified-language-support ()
2025 "Describe how Emacs supports the specified language environment."
2026 (interactive)
2027 (let (language-name)
2028 (if (not (and (symbolp last-command-event)
2029 (or (not (eq last-command-event 'Default))
2030 (setq last-command-event 'English))
2031 (setq language-name (symbol-name last-command-event))))
2032 (error "Bogus calling sequence"))
2033 (describe-language-environment language-name)))
2034
2035 (defun describe-language-environment (language-name)
2036 "Describe how Emacs supports language environment LANGUAGE-NAME."
2037 (interactive
2038 (list (read-language-name
2039 'documentation
2040 "Describe language environment (default current choice): ")))
2041 (if (null language-name)
2042 (setq language-name current-language-environment))
2043 (if (or (null language-name)
2044 (null (get-language-info language-name 'documentation)))
2045 (error "No documentation for the specified language"))
2046 (if (symbolp language-name)
2047 (setq language-name (symbol-name language-name)))
2048 (dolist (feature (get-language-info language-name 'features))
2049 (require feature))
2050 (let ((doc (get-language-info language-name 'documentation)))
2051 (help-setup-xref (list #'describe-language-environment language-name)
2052 (interactive-p))
2053 (with-output-to-temp-buffer (help-buffer)
2054 (save-excursion
2055 (set-buffer standard-output)
2056 (insert language-name " language environment\n\n")
2057 (if (stringp doc)
2058 (insert doc "\n\n"))
2059 (condition-case nil
2060 (let ((str (eval (get-language-info language-name 'sample-text))))
2061 (if (stringp str)
2062 (insert "Sample text:\n " str "\n\n")))
2063 (error nil))
2064 (let ((input-method (get-language-info language-name 'input-method))
2065 (l (copy-sequence input-method-alist)))
2066 (insert "Input methods")
2067 (when input-method
2068 (insert " (default " input-method ")")
2069 (setq input-method (assoc input-method input-method-alist))
2070 (setq l (cons input-method (delete input-method l))))
2071 (insert ":\n")
2072 (while l
2073 (when (string= language-name (nth 1 (car l)))
2074 (insert " " (car (car l)))
2075 (search-backward (car (car l)))
2076 (help-xref-button 0 'help-input-method (car (car l)))
2077 (goto-char (point-max))
2078 (insert " (\""
2079 (if (stringp (nth 3 (car l)))
2080 (nth 3 (car l))
2081 (car (nth 3 (car l))))
2082 "\" in mode line)\n"))
2083 (setq l (cdr l)))
2084 (insert "\n"))
2085 (insert "Character sets:\n")
2086 (let ((l (get-language-info language-name 'charset)))
2087 (if (null l)
2088 (insert " nothing specific to " language-name "\n")
2089 (while l
2090 (insert " " (symbol-name (car l)))
2091 (search-backward (symbol-name (car l)))
2092 (help-xref-button 0 'help-character-set (car l))
2093 (goto-char (point-max))
2094 (insert ": " (charset-description (car l)) "\n")
2095 (setq l (cdr l)))))
2096 (insert "\n")
2097 (insert "Coding systems:\n")
2098 (let ((l (get-language-info language-name 'coding-system)))
2099 (if (null l)
2100 (insert " nothing specific to " language-name "\n")
2101 (while l
2102 (insert " " (symbol-name (car l)))
2103 (search-backward (symbol-name (car l)))
2104 (help-xref-button 0 'help-coding-system (car l))
2105 (goto-char (point-max))
2106 (insert " (`"
2107 (coding-system-mnemonic (car l))
2108 "' in mode line):\n\t"
2109 (coding-system-doc-string (car l))
2110 "\n")
2111 (let ((aliases (coding-system-get (car l)
2112 'alias-coding-systems)))
2113 (when aliases
2114 (insert "\t(alias:")
2115 (while aliases
2116 (insert " " (symbol-name (car aliases)))
2117 (setq aliases (cdr aliases)))
2118 (insert ")\n")))
2119 (setq l (cdr l)))))))))
2120 \f
2121 ;;; Locales.
2122
2123 (defvar locale-translation-file-name nil
2124 "File name for the system's file of locale-name aliases, or nil if none.")
2125
2126 ;; The following definitions might as well be marked as constants and
2127 ;; purecopied, since they're normally used on startup, and probably
2128 ;; should reflect the facilities of the base Emacs.
2129 (defconst locale-language-names
2130 (purecopy
2131 '(
2132 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2133 ;; as specified in the Single Unix Spec, Version 2.
2134 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2135 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2136 ;; see Internet RFC 2165 (1997-06) and
2137 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2138 ;; TERRITORY is a country code taken from ISO 3166
2139 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2140 ;; CODESET and MODIFIER are implementation-dependent.
2141
2142 ;; jasonr comments: MS Windows uses three letter codes for
2143 ;; languages instead of the two letter ISO codes that POSIX
2144 ;; uses. In most cases the first two letters are the same, so
2145 ;; most of the regexps in locale-language-names work. Japanese
2146 ;; and Chinese are exceptions, which are listed in the
2147 ;; non-standard section at the bottom of locale-language-names.
2148
2149 ("aa_DJ" . "Latin-1") ; Afar
2150 ("aa" . "UTF-8")
2151 ;; ab Abkhazian
2152 ("af" . "Latin-1") ; Afrikaans
2153 ("am" "Ethiopic" utf-8) ; Amharic
2154 ("an" . "Latin-9") ; Aragonese
2155 ; ar Arabic glibc uses 8859-6
2156 ; as Assamese
2157 ; ay Aymara
2158 ("az" . "UTF-8") ; Azerbaijani
2159 ; ba Bashkir
2160 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2161 ("bg" "Bulgarian" cp1251) ; Bulgarian
2162 ; bh Bihari
2163 ; bi Bislama
2164 ("bn" . "UTF-8") ; Bengali, Bangla
2165 ("bo" . "Tibetan")
2166 ("br" . "Latin-1") ; Breton
2167 ("bs" . "Latin-2") ; Bosnian
2168 ("byn" . "UTF-8") ; Bilin; Blin
2169 ("ca" . "Latin-1") ; Catalan
2170 ; co Corsican
2171 ("cs" "Czech" iso-8859-2)
2172 ("cy" "Welsh" iso-8859-14)
2173 ("da" . "Latin-1") ; Danish
2174 ("de" "German" iso-8859-1)
2175 ; dv Divehi
2176 ; dz Bhutani
2177 ("el" "Greek" iso-8859-7)
2178 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2179 ;; That's actually what the GNU locales define, modulo things like
2180 ;; en_IN -- fx.
2181 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2182 ("en" "English" iso-8859-1) ; English
2183 ("eo" . "Latin-3") ; Esperanto
2184 ("es" "Spanish" iso-8859-1)
2185 ("et" . "Latin-1") ; Estonian
2186 ("eu" . "Latin-1") ; Basque
2187 ("fa" . "UTF-8") ; Persian
2188 ("fi" . "Latin-1") ; Finnish
2189 ("fj" . "Latin-1") ; Fiji
2190 ("fo" . "Latin-1") ; Faroese
2191 ("fr" "French" iso-8859-1) ; French
2192 ("fy" . "Latin-1") ; Frisian
2193 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2194 ("gd" . "Latin-9") ; Scots Gaelic
2195 ("gez" "Ethiopic" utf-8) ; Geez
2196 ("gl" . "Latin-1") ; Gallegan; Galician
2197 ; gn Guarani
2198 ("gu" . "UTF-8") ; Gujarati
2199 ("gv" . "Latin-1") ; Manx Gaelic
2200 ; ha Hausa
2201 ("he" "Hebrew" iso-8859-8)
2202 ("hi" "Devanagari" utf-8) ; Hindi
2203 ("hr" "Croatian" iso-8859-2) ; Croatian
2204 ("hu" . "Latin-2") ; Hungarian
2205 ; hy Armenian
2206 ; ia Interlingua
2207 ("id" . "Latin-1") ; Indonesian
2208 ; ie Interlingue
2209 ; ik Inupiak
2210 ("is" . "Latin-1") ; Icelandic
2211 ("it" "Italian" iso-8859-1) ; Italian
2212 ; iu Inuktitut
2213 ("iw" "Hebrew" iso-8859-8)
2214 ("ja" "Japanese" euc-jp)
2215 ; jw Javanese
2216 ("ka" "Georgian" georgian-ps) ; Georgian
2217 ; kk Kazakh
2218 ("kl" . "Latin-1") ; Greenlandic
2219 ; km Cambodian
2220 ("kn" "Kannada" utf-8)
2221 ("ko" "Korean" euc-kr)
2222 ; ks Kashmiri
2223 ; ku Kurdish
2224 ("kw" . "Latin-1") ; Cornish
2225 ; ky Kirghiz
2226 ("la" . "Latin-1") ; Latin
2227 ("lb" . "Latin-1") ; Luxemburgish
2228 ("lg" . "Laint-6") ; Ganda
2229 ; ln Lingala
2230 ("lo" "Lao" utf-8) ; Laothian
2231 ("lt" "Lithuanian" iso-8859-13)
2232 ("lv" . "Latvian") ; Latvian, Lettish
2233 ; mg Malagasy
2234 ("mi" . "Latin-7") ; Maori
2235 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2236 ("ml" "Malayalam" utf-8)
2237 ("mn" . "UTF-8") ; Mongolian
2238 ; mo Moldavian
2239 ("mr" "Devanagari" utf-8) ; Marathi
2240 ("ms" . "Latin-1") ; Malay
2241 ("mt" . "Latin-3") ; Maltese
2242 ; my Burmese
2243 ; na Nauru
2244 ("nb" . "Latin-1") ; Norwegian
2245 ("ne" "Devanagari" utf-8) ; Nepali
2246 ("nl" "Dutch" iso-8859-1)
2247 ("no" . "Latin-1") ; Norwegian
2248 ("oc" . "Latin-1") ; Occitan
2249 ("om_ET" . "UTF-8") ; (Afan) Oromo
2250 ("om" . "Latin-1") ; (Afan) Oromo
2251 ; or Oriya
2252 ("pa" . "UTF-8") ; Punjabi
2253 ("pl" . "Latin-2") ; Polish
2254 ; ps Pashto, Pushto
2255 ("pt" . "Latin-1") ; Portuguese
2256 ; qu Quechua
2257 ("rm" . "Latin-1") ; Rhaeto-Romanic
2258 ; rn Kirundi
2259 ("ro" "Romanian" iso-8859-2)
2260 ("ru_RU" "Russian" iso-8859-5)
2261 ("ru_UA" "Russian" koi8-u)
2262 ; rw Kinyarwanda
2263 ("sa" . "Devanagari") ; Sanskrit
2264 ; sd Sindhi
2265 ("se" . "UTF-8") ; Northern Sami
2266 ; sg Sangho
2267 ("sh" . "Latin-2") ; Serbo-Croatian
2268 ; si Sinhalese
2269 ("sid" . "UTF-8") ; Sidamo
2270 ("sk" "Slovak" iso-8859-2)
2271 ("sl" "Slovenian" iso-8859-2)
2272 ; sm Samoan
2273 ; sn Shona
2274 ("so_ET" "UTF-8") ; Somali
2275 ("so" "Latin-1") ; Somali
2276 ("sq" . "Latin-1") ; Albanian
2277 ("sr_YU@cyrillic" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet)
2278 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2279 ; ss Siswati
2280 ("st" . "Latin-1") ; Sesotho
2281 ; su Sundanese
2282 ("sv" "Swedish" iso-8859-1) ; Swedish
2283 ("sw" . "Latin-1") ; Swahili
2284 ("ta" "Tamil" utf-8)
2285 ("te" . "UTF-8") ; Telugu
2286 ("tg" "Tajik" koi8-t)
2287 ("th" "Thai" tis-620)
2288 ("ti" "Ethiopic" utf-8) ; Tigrinya
2289 ("tig_ER" . "UTF-8") ; Tigre
2290 ; tk Turkmen
2291 ("tl" . "Latin-1") ; Tagalog
2292 ; tn Setswana
2293 ; to Tonga
2294 ("tr" "Turkish" iso-8859-9)
2295 ; ts Tsonga
2296 ("tt" . "UTF-8") ; Tatar
2297 ; tw Twi
2298 ; ug Uighur
2299 ("uk" "Ukrainian" koi8-u)
2300 ("ur" . "UTF-8") ; Urdu
2301 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2302 ("uz" . "Latin-1") ; Uzbek
2303 ("vi" "Vietnamese" utf-8)
2304 ; vo Volapuk
2305 ("wa" . "Latin-1") ; Walloon
2306 ; wo Wolof
2307 ("xh" . "Latin-1") ; Xhosa
2308 ("yi" . "Windows-1255") ; Yiddish
2309 ; yo Yoruba
2310 ; za Zhuang
2311 ("zh_HK" . "Chinese-Big5")
2312 ("zh_TW" . "Chinese-Big5")
2313 ("zh_CN" . "Chinese-GB")
2314 ("zh" . "Chinese-GB")
2315 ; zh_CN.GB18030/GB18030 \
2316 ; zh_CN.GBK/GBK \
2317 ; zh_HK/BIG5-HKSCS \
2318 ("zu" . "Latin-1") ; Zulu
2319
2320 ;; ISO standard locales
2321 ("c$" . "ASCII")
2322 ("posix$" . "ASCII")
2323
2324 ;; The "IPA" Emacs language environment does not correspond
2325 ;; to any ISO 639 code, so let it stand for itself.
2326 ("ipa$" . "IPA")
2327
2328 ;; Nonstandard or obsolete language codes
2329 ("cz" . "Czech") ; e.g. Solaris 2.6
2330 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2331 ("iw" . "Hebrew") ; e.g. X11R6.4
2332 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2333 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2334 ("jp" . "Japanese") ; e.g. MS Windows
2335 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2336 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2337 ("gbz" . "UTF-8") ; MS Windows Dari Persian
2338 ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
2339 ("wee" . "Latin-2") ; MS Windows Lower Sorbian
2340 ("wen" . "Latin-2") ; MS Windows Upper Sorbian
2341 ))
2342 "Alist of locale regexps vs the corresponding languages and coding systems.
2343 Each element has these form:
2344 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2345 The first element whose LOCALE-REGEXP matches the start of a
2346 downcased locale specifies the LANG-ENV \(language environtment)
2347 and CODING-SYSTEM corresponding to that locale. If there is no
2348 appropriate language environment, the element may have this form:
2349 \(LOCALE-REGEXP . LANG-ENV)
2350 In this case, LANG-ENV is one of generic language environments for an
2351 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2352
2353 (defconst locale-charset-language-names
2354 (purecopy
2355 '((".*8859[-_]?1\\>" . "Latin-1")
2356 (".*8859[-_]?2\\>" . "Latin-2")
2357 (".*8859[-_]?3\\>" . "Latin-3")
2358 (".*8859[-_]?4\\>" . "Latin-4")
2359 (".*8859[-_]?9\\>" . "Latin-5")
2360 (".*8859[-_]?14\\>" . "Latin-8")
2361 (".*8859[-_]?15\\>" . "Latin-9")
2362 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2363 ;; utf-8@euro exists, so put this last. (@euro really specifies
2364 ;; the currency, rather than the charset.)
2365 (".*@euro\\>" . "Latin-9")))
2366 "List of pairs of locale regexps and charset language names.
2367 The first element whose locale regexp matches the start of a downcased locale
2368 specifies the language name whose charset corresponds to that locale.
2369 This language name is used if the locale is not listed in
2370 `locale-language-names'")
2371
2372 (defconst locale-preferred-coding-systems
2373 (purecopy
2374 '((".*8859[-_]?1\\>" . iso-8859-1)
2375 (".*8859[-_]?2\\>" . iso-8859-2)
2376 (".*8859[-_]?3\\>" . iso-8859-3)
2377 (".*8859[-_]?4\\>" . iso-8859-4)
2378 (".*8859[-_]?9\\>" . iso-8859-9)
2379 (".*8859[-_]?14\\>" . iso-8859-14)
2380 (".*8859[-_]?15\\>" . iso-8859-15)
2381 (".*utf\\(?:-?8\\)?" . utf-8)
2382 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2383 ;; specifies the currency, rather than the charset.)
2384 (".*@euro" . iso-8859-15)
2385 ("koi8-?r" . koi8-r)
2386 ("koi8-?u" . koi8-u)
2387 ("tcvn" . tcvn)
2388 ("big5" . big5)
2389 ("euc-?tw" . euc-tw)
2390 ;; We don't support GBK, but as it is upper compatible with
2391 ;; GB-2312, we setup the default coding system to gb2312.
2392 ("gbk" . gb2312)
2393 ;; We don't support BIG5-HKSCS, but as it is upper compatible with
2394 ;; BIG5, we setup the default coding system to big5.
2395 ("big5hkscs" . big5)
2396 ("ja.*[._]euc" . japanese-iso-8bit)
2397 ("ja.*[._]jis7" . iso-2022-jp)
2398 ("ja.*[._]pck" . japanese-shift-jis)
2399 ("ja.*[._]sjis" . japanese-shift-jis)
2400 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2401 ))
2402 "List of pairs of locale regexps and preferred coding systems.
2403 The first element whose locale regexp matches the start of a downcased locale
2404 specifies the coding system to prefer when using that locale.
2405 This coding system is used if the locale specifies a specific charset.")
2406
2407 (defun locale-name-match (key alist)
2408 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2409 Return the value corresponding to the first regexp that matches the
2410 start of KEY, or nil if there is no match."
2411 (let (element)
2412 (while (and alist (not element))
2413 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2414 (setq element (car alist)))
2415 (setq alist (cdr alist)))
2416 (cdr element)))
2417
2418 (defun locale-charset-match-p (charset1 charset2)
2419 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2420 Matching is done ignoring case and any hyphens and underscores in the
2421 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2422 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2423 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2424 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2425
2426 (defvar locale-charset-alist nil
2427 "Coding system alist keyed on locale-style charset name.
2428 Used by `locale-charset-to-coding-system'.")
2429
2430 (defun locale-charset-to-coding-system (charset)
2431 "Find coding system corresponding to CHARSET.
2432 CHARSET is any sort of non-Emacs charset name, such as might be used
2433 in a locale codeset, or elsewhere. It is matched to a coding system
2434 first by case-insensitive lookup in `locale-charset-alist'. Then
2435 matches are looked for in the coding system list, treating case and
2436 the characters `-' and `_' as insignificant. The coding system base
2437 is returned. Thus, for instance, if charset \"ISO8859-2\",
2438 `iso-latin-2' is returned."
2439 (or (car (assoc-string charset locale-charset-alist t))
2440 (let ((cs coding-system-alist)
2441 c)
2442 (while (and (not c) cs)
2443 (if (locale-charset-match-p charset (caar cs))
2444 (setq c (intern (caar cs)))
2445 (pop cs)))
2446 (if c (coding-system-base c)))))
2447
2448 ;; Fixme: This ought to deal with the territory part of the locale
2449 ;; too, for setting things such as calendar holidays, ps-print paper
2450 ;; size, spelling dictionary.
2451
2452 (defun set-locale-environment (&optional locale-name display)
2453 "Set up multi-lingual environment for using LOCALE-NAME.
2454 This sets the language environment, the coding system priority,
2455 the default input method and sometimes other things.
2456
2457 LOCALE-NAME should be a string which is the name of a locale supported
2458 by the system. Often it is of the form xx_XX.CODE, where xx is a
2459 language, XX is a country, and CODE specifies a character set and
2460 coding system. For example, the locale name \"ja_JP.EUC\" might name
2461 a locale for Japanese in Japan using the `japanese-iso-8bit'
2462 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2463 or `@cyrillic'.
2464
2465 If LOCALE-NAME is nil, its value is taken from the environment
2466 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2467
2468 The locale names supported by your system can typically be found in a
2469 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2470 will be translated according to the table specified by
2471 `locale-translation-file-name'.
2472
2473 If DISPLAY is non-nil, only set the keyboard coding system and
2474 the terminal coding system for the given display, and don't touch
2475 session-global parameters like the language environment. DISPLAY
2476 may be a display id or a frame.
2477
2478 See also `locale-charset-language-names', `locale-language-names',
2479 `locale-preferred-coding-systems' and `locale-coding-system'."
2480 (interactive "sSet environment for locale: ")
2481
2482 ;; Do this at runtime for the sake of binaries possibly transported
2483 ;; to a system without X.
2484 (setq locale-translation-file-name
2485 (let ((files
2486 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2487 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2488 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2489 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2490 ;;
2491 ;; The following name appears after the X-related names above,
2492 ;; since the X-related names are what X actually uses.
2493 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2494 )))
2495 (while (and files (not (file-exists-p (car files))))
2496 (setq files (cdr files)))
2497 (car files)))
2498
2499 (let ((locale locale-name))
2500
2501 (unless locale
2502 ;; Use the first of these three environment variables
2503 ;; that has a nonempty value.
2504 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2505 (while (and vars
2506 (= 0 (length locale))) ; nil or empty string
2507 (setq locale (getenv (pop vars) display)))))
2508
2509 (unless locale
2510 ;; The two tests are kept separate so the byte-compiler sees
2511 ;; that mac-get-preference is only called after checking its existence.
2512 (when (fboundp 'mac-get-preference)
2513 (setq locale (mac-get-preference "AppleLocale"))
2514 (unless locale
2515 (let ((languages (mac-get-preference "AppleLanguages")))
2516 (unless (= (length languages) 0) ; nil or empty vector
2517 (setq locale (aref languages 0)))))))
2518 (unless (or locale (not (boundp 'mac-system-locale)))
2519 (setq locale mac-system-locale))
2520
2521 (when locale
2522
2523 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2524 ;; using the translation file that many systems have.
2525 (when locale-translation-file-name
2526 (with-temp-buffer
2527 (insert-file-contents locale-translation-file-name)
2528 (when (re-search-forward
2529 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2530 (setq locale (buffer-substring (point) (line-end-position))))))
2531
2532 ;; Leave the system locales alone if the caller did not specify
2533 ;; an explicit locale name, as their defaults are set from
2534 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2535 ;; want to set them to the same value as LC_CTYPE.
2536 (when locale-name
2537 (setq system-messages-locale locale)
2538 (setq system-time-locale locale))
2539
2540 (setq locale (downcase locale))
2541
2542 (let ((language-name
2543 (locale-name-match locale locale-language-names))
2544 (charset-language-name
2545 (locale-name-match locale locale-charset-language-names))
2546 (default-eol-type (coding-system-eol-type
2547 default-buffer-file-coding-system))
2548 (coding-system
2549 (or (locale-name-match locale locale-preferred-coding-systems)
2550 (when locale
2551 (if (string-match "\\.\\([^@]+\\)" locale)
2552 (locale-charset-to-coding-system
2553 (match-string 1 locale))))
2554 (and (eq system-type 'macos) mac-system-coding-system))))
2555
2556 (if (consp language-name)
2557 ;; locale-language-names specify both lang-env and coding.
2558 ;; But, what specified in locale-preferred-coding-systems
2559 ;; has higher priority.
2560 (setq coding-system (or coding-system
2561 (nth 1 language-name))
2562 language-name (car language-name))
2563 ;; Otherwise, if locale is not listed in locale-language-names,
2564 ;; use what listed in locale-charset-language-names.
2565 (if (not language-name)
2566 (setq language-name charset-language-name)))
2567
2568 ;; If a specific EOL conversion was specified in the default
2569 ;; buffer-file-coding-system, preserve it in the coding system
2570 ;; we will be using from now on.
2571 (if (and (memq default-eol-type '(0 1 2 unix dos mac))
2572 coding-system
2573 (coding-system-p coding-system))
2574 (setq coding-system (coding-system-change-eol-conversion
2575 coding-system default-eol-type)))
2576
2577 (when language-name
2578
2579 ;; Set up for this character set. This is now the right way
2580 ;; to do it for both unibyte and multibyte modes.
2581 (unless display
2582 (set-language-environment language-name))
2583
2584 ;; If default-enable-multibyte-characters is nil,
2585 ;; we are using single-byte characters,
2586 ;; so the display table and terminal coding system are irrelevant.
2587 (when default-enable-multibyte-characters
2588 (set-display-table-and-terminal-coding-system
2589 language-name coding-system display))
2590
2591 ;; Set the `keyboard-coding-system' if appropriate (tty
2592 ;; only). At least X and MS Windows can generate
2593 ;; multilingual input.
2594 ;; XXX This was disabled unless `window-system', but that
2595 ;; leads to buggy behaviour when a tty frame is opened
2596 ;; later. Setting the keyboard coding system has no adverse
2597 ;; effect on X, so let's do it anyway. -- Lorentey
2598 (let ((kcs (or coding-system
2599 (car (get-language-info language-name
2600 'coding-system)))))
2601 (if kcs (set-keyboard-coding-system kcs display)))
2602
2603 (unless display
2604 (setq locale-coding-system
2605 (car (get-language-info language-name 'coding-priority)))))
2606
2607 (when (and (not display)
2608 coding-system
2609 (not (coding-system-equal coding-system
2610 locale-coding-system)))
2611 (prefer-coding-system coding-system)
2612 (setq locale-coding-system coding-system))))
2613
2614 ;; On Windows, override locale-coding-system,
2615 ;; keyboard-coding-system with system codepage. Note:
2616 ;; selection-coding-system is already set in w32select.c.
2617 (when (boundp 'w32-ansi-code-page)
2618 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2619 (when (coding-system-p code-page-coding)
2620 (unless display (setq locale-coding-system code-page-coding))
2621 (set-keyboard-coding-system code-page-coding display)
2622 (set-terminal-coding-system code-page-coding display))))
2623
2624 (when (eq system-type 'darwin)
2625 ;; On Darwin, file names are always encoded in utf-8, no matter
2626 ;; the locale.
2627 (setq default-file-name-coding-system 'utf-8)
2628 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2629 ;; the locale.
2630 (when (and (null window-system)
2631 (equal (getenv "TERM_PROGRAM" display) "Apple_Terminal"))
2632 (set-terminal-coding-system 'utf-8)
2633 (set-keyboard-coding-system 'utf-8)))
2634
2635 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2636 ;; (See comments in Flocale_info.)
2637 (unless display
2638 (let ((locale locale)
2639 (paper (locale-info 'paper)))
2640 (if paper
2641 ;; This will always be null at the time of writing.
2642 (cond
2643 ((equal paper '(216 279))
2644 (setq ps-paper-type 'letter))
2645 ((equal paper '(210 297))
2646 (setq ps-paper-type 'a4)))
2647 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2648 (while (and vars (= 0 (length locale)))
2649 (setq locale (getenv (pop vars) display))))
2650 (when locale
2651 ;; As of glibc 2.2.5, these are the only US Letter locales,
2652 ;; and the rest are A4.
2653 (setq ps-paper-type
2654 (or (locale-name-match locale '(("c$" . letter)
2655 ("posix$" . letter)
2656 (".._us" . letter)
2657 (".._pr" . letter)
2658 (".._ca" . letter)
2659 ("enu$" . letter) ; Windows
2660 ("esu$" . letter)
2661 ("enc$" . letter)
2662 ("frc$" . letter)))
2663 'a4)))))))
2664 nil)
2665 \f
2666 ;;; Charset property
2667
2668 (defun get-charset-property (charset propname)
2669 "Return the value of CHARSET's PROPNAME property.
2670 This is the last value stored with
2671 (put-charset-property CHARSET PROPNAME VALUE)."
2672 (and (not (eq charset 'composition))
2673 (plist-get (charset-plist charset) propname)))
2674
2675 (defun put-charset-property (charset propname value)
2676 "Store CHARSETS's PROPNAME property with value VALUE.
2677 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
2678 (or (eq charset 'composition)
2679 (set-charset-plist charset
2680 (plist-put (charset-plist charset) propname value))))
2681
2682 ;;; Character code property
2683 (put 'char-code-property-table 'char-table-extra-slots 0)
2684
2685 (defvar char-code-property-table
2686 (make-char-table 'char-code-property-table)
2687 "Char-table containing a property list of each character code.
2688
2689 See also the documentation of `get-char-code-property' and
2690 `put-char-code-property'.")
2691
2692 (defun get-char-code-property (char propname)
2693 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2694 (let ((plist (aref char-code-property-table char)))
2695 (if (listp plist)
2696 (car (cdr (memq propname plist))))))
2697
2698 (defun put-char-code-property (char propname value)
2699 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2700 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2701 (let ((plist (aref char-code-property-table char)))
2702 (if plist
2703 (let ((slot (memq propname plist)))
2704 (if slot
2705 (setcar (cdr slot) value)
2706 (nconc plist (list propname value))))
2707 (aset char-code-property-table char (list propname value)))))
2708
2709 \f
2710 ;; Pretty description of encoded string
2711
2712 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2713 (defvar iso-2022-control-alist
2714 '((?\x1b . "ESC")
2715 (?\x0e . "SO")
2716 (?\x0f . "SI")
2717 (?\x8e . "SS2")
2718 (?\x8f . "SS3")
2719 (?\x9b . "CSI")))
2720
2721 (defun encoded-string-description (str coding-system)
2722 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2723 (setq str (string-as-unibyte str))
2724 (mapconcat
2725 (if (and coding-system (eq (coding-system-type coding-system) 2))
2726 ;; Try to get a pretty description for ISO 2022 escape sequences.
2727 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2728 (format "#x%02X" x))))
2729 (function (lambda (x) (format "#x%02X" x))))
2730 str " "))
2731
2732 (defun encode-coding-char (char coding-system)
2733 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2734 If CODING-SYSTEM can't safely encode CHAR, return nil."
2735 (let ((str1 (string-as-multibyte (char-to-string char)))
2736 (str2 (string-as-multibyte (make-string 2 char)))
2737 (safe-chars (and coding-system
2738 (coding-system-get coding-system 'safe-chars)))
2739 (charset (char-charset char))
2740 enc1 enc2 i1 i2)
2741 (when (or (eq safe-chars t)
2742 (eq charset 'ascii)
2743 (and safe-chars (aref safe-chars char)))
2744 ;; We must find the encoded string of CHAR. But, just encoding
2745 ;; CHAR will put extra control sequences (usually to designate
2746 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2747 ;; To exclude such tailing bytes, we at first encode one-char
2748 ;; string and two-char string, then check how many bytes at the
2749 ;; tail of both encoded strings are the same.
2750
2751 (setq enc1 (encode-coding-string str1 coding-system)
2752 i1 (length enc1)
2753 enc2 (encode-coding-string str2 coding-system)
2754 i2 (length enc2))
2755 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2756 (setq i1 (1- i1) i2 (1- i2)))
2757
2758 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2759 ;; and they are the extra control sequences at the tail to
2760 ;; exclude.
2761 (substring enc2 0 i2))))
2762
2763
2764 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2765 ;;; mule-cmds.el ends here