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