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