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