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