* puresize.h (BASE_PURESIZE): Increase to 1430000.
[emacs.git] / lisp / international / mule-cmds.el
blob0064f40e28749f60634580bdbd2e660e78e4b100
1 ;;; mule-cmds.el --- commands for multilingual environment -*-coding: iso-2022-7bit -*-
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
13 ;; Keywords: mule, i18n
15 ;; This file is part of GNU Emacs.
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.
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.
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/>.
30 ;;; Commentary:
32 ;;; Code:
34 (eval-when-compile (require 'cl)) ; letf
36 (defvar dos-codepage)
37 (autoload 'widget-value "wid-edit")
39 (defvar mac-system-coding-system)
41 ;;; MULE related key bindings and menus.
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.")
59 ;; Keep "C-x C-m ..." for mule specific commands.
60 (define-key ctl-x-map "\C-m" mule-keymap)
62 (defvar describe-language-environment-map
63 (let ((map (make-sparse-keymap "Describe Language Environment")))
64 (define-key map
65 [Default] `(menu-item ,(purecopy "Default") describe-specified-language-support))
66 map))
68 (defvar setup-language-environment-map
69 (let ((map (make-sparse-keymap "Set Language Environment")))
70 (define-key map
71 [Default] `(menu-item ,(purecopy "Default") setup-specified-language-environment))
72 map))
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 ,(purecopy "For Next Command") universal-coding-system-argument
78 :help ,(purecopy "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 ,(purecopy "For Saving This Buffer") set-buffer-file-coding-system
82 :help ,(purecopy "How to encode this buffer when saved")))
83 (define-key-after map [revert-buffer-with-coding-system]
84 `(menu-item ,(purecopy "For Reverting This File Now")
85 revert-buffer-with-coding-system
86 :enable buffer-file-name
87 :help ,(purecopy "Revisit this file immediately using specified coding system")))
88 (define-key-after map [set-file-name-coding-system]
89 `(menu-item ,(purecopy "For File Name") set-file-name-coding-system
90 :help ,(purecopy "How to decode/encode file names")))
91 (define-key-after map [separator-2] '("--"))
93 (define-key-after map [set-keyboard-coding-system]
94 `(menu-item ,(purecopy "For Keyboard") set-keyboard-coding-system
95 :help ,(purecopy "How to decode keyboard input")))
96 (define-key-after map [set-terminal-coding-system]
97 `(menu-item ,(purecopy "For Terminal") set-terminal-coding-system
98 :enable (null (memq initial-window-system '(x w32 ns)))
99 :help ,(purecopy "How to encode terminal output")))
100 (define-key-after map [separator-3] '("--"))
102 (define-key-after map [set-selection-coding-system]
103 `(menu-item ,(purecopy "For X Selections/Clipboard") set-selection-coding-system
104 :visible (display-selections-p)
105 :help ,(purecopy "How to en/decode data to/from selection/clipboard")))
106 (define-key-after map [set-next-selection-coding-system]
107 `(menu-item ,(purecopy "For Next X Selection") set-next-selection-coding-system
108 :visible (display-selections-p)
109 :help ,(purecopy "How to en/decode next selection/clipboard operation")))
110 (define-key-after map [set-buffer-process-coding-system]
111 `(menu-item ,(purecopy "For I/O with Subprocess") set-buffer-process-coding-system
112 :visible (fboundp 'start-process)
113 :enable (get-buffer-process (current-buffer))
114 :help ,(purecopy "How to en/decode I/O from/to subprocess connected to this buffer")))
115 map))
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 ,(purecopy "Set Language Environment") ,setup-language-environment-map))
121 (define-key-after map [separator-mule] '("--"))
123 (define-key-after map [toggle-input-method]
124 `(menu-item ,(purecopy "Toggle Input Method") toggle-input-method))
125 (define-key-after map [set-input-method]
126 `(menu-item ,(purecopy "Select Input Method...") set-input-method))
127 (define-key-after map [describe-input-method]
128 `(menu-item ,(purecopy "Describe Input Method") describe-input-method))
129 (define-key-after map [separator-input-method] '("--"))
131 (define-key-after map [set-various-coding-system]
132 `(menu-item ,(purecopy "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 ,(purecopy "Show Multi-lingual Text") view-hello-file
136 :enable (file-readable-p
137 (expand-file-name "HELLO" data-directory))
138 :help ,(purecopy "Display file which says HELLO in many languages")))
139 (define-key-after map [separator-coding-system] '("--"))
141 (define-key-after map [describe-language-environment]
142 `(menu-item ,(purecopy "Describe Language Environment")
143 describe-language-environment-map
144 :help ,(purecopy "Show multilingual settings for a specific language")))
145 (define-key-after map [describe-input-method]
146 `(menu-item ,(purecopy "Describe Input Method...") describe-input-method
147 :help ,(purecopy "Keyboard layout for a specific input method")))
148 (define-key-after map [describe-coding-system]
149 `(menu-item ,(purecopy "Describe Coding System...") describe-coding-system))
150 (define-key-after map [list-character-sets]
151 `(menu-item ,(purecopy "List Character Sets") list-character-sets
152 :help ,(purecopy "Show table of available character sets")))
153 (define-key-after map [mule-diag]
154 `(menu-item ,(purecopy "Show All of Mule Status") mule-diag
155 :help ,(purecopy "Display multilingual environment settings")))
156 map)
157 "Keymap for Mule (Multilingual environment) menu specific commands.")
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)
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)
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_\\)+\\)'")))
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.
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)))))
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)))))
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))
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)
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)))))))))
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.
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.
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))
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))))
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)
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)))
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))
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)))
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))))
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))))
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))))
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'
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.
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)))
416 (defvar sort-coding-systems-predicate nil
417 "If non-nil, a predicate function to sort coding systems.
419 It is called with two coding systems, and should return t if the first
420 one is \"less\" than the second.
422 The function `sort-coding-systems' use it.")
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.
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'.
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))
466 ((string-match-p "^x-" (symbol-name mime))
468 (t 3))
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))
485 ((or (eq category 'coding-category-iso-7-tight)
486 (eq category 'coding-category-iso-7))
489 0)))
491 ))))))
492 (sort codings (function (lambda (x y)
493 (> (funcall func x) (funcall func y))))))))
495 (defun find-coding-systems-region (from to)
496 "Return a list of proper coding systems to encode a text between FROM and TO.
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.
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)))))
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.
517 If STRING contains no multibyte characters, return a list of a single
518 element `undecided'."
519 (find-coding-systems-region string nil))
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.
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))
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)))
554 (push cs codings))))
555 (nreverse codings)))))
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)))
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.
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))
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.")
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.")
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)))
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))))
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))))
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)))
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 "\
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)))))
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))
806 (kill-buffer "*Warning*")
807 (set-window-configuration window-configuration)
808 coding-system))
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.
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'.
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.)
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.
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.
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).
846 The variable `select-safe-coding-system-accept-default-p', if non-nil,
847 overrides ACCEPT-DEFAULT-P.
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)))
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))))))
882 (if (eq (car default-coding-system) t)
883 (setq no-other-defaults t
884 default-coding-system (cdr default-coding-system)))
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))
891 (if (and auto-cs (not no-other-defaults))
892 ;; If the file has a coding cookie, use it regardless of any
893 ;; other setting.
894 (let ((base (coding-system-base auto-cs)))
895 (unless (memq base '(nil undecided))
896 (setq default-coding-system (list (cons auto-cs base)))
897 (setq no-other-defaults t))))
899 (unless no-other-defaults
900 ;; If buffer-file-coding-system is not nil nor undecided, append it
901 ;; to the defaults.
902 (if buffer-file-coding-system
903 (let ((base (coding-system-base buffer-file-coding-system)))
904 (or (eq base 'undecided)
905 (rassq base default-coding-system)
906 (setq default-coding-system
907 (append default-coding-system
908 (list (cons buffer-file-coding-system base)))))))
910 (unless (and buffer-file-coding-system-explicit
911 (cdr buffer-file-coding-system-explicit))
912 ;; If default buffer-file-coding-system is not nil nor undecided,
913 ;; append it to the defaults.
914 (when (default-value 'buffer-file-coding-system)
915 (let ((base (coding-system-base
916 (default-value 'buffer-file-coding-system))))
917 (or (eq base 'undecided)
918 (rassq base default-coding-system)
919 (setq default-coding-system
920 (append default-coding-system
921 (list (cons (default-value
922 'buffer-file-coding-system)
923 base)))))))
925 ;; If the most preferred coding system has the property mime-charset,
926 ;; append it to the defaults.
927 (let ((preferred (coding-system-priority-list t))
928 base)
929 (and (coding-system-p preferred)
930 (setq base (coding-system-base preferred))
931 (coding-system-get preferred :mime-charset)
932 (not (rassq base default-coding-system))
933 (setq default-coding-system
934 (append default-coding-system
935 (list (cons preferred base))))))))
937 (if select-safe-coding-system-accept-default-p
938 (setq accept-default-p select-safe-coding-system-accept-default-p))
940 ;; Decide the eol-type from the top of the default codings,
941 ;; current buffer-file-coding-system, or default buffer-file-coding-system.
942 (if default-coding-system
943 (let ((default-eol-type (coding-system-eol-type
944 (caar default-coding-system))))
945 (if (and (vectorp default-eol-type) buffer-file-coding-system)
946 (setq default-eol-type (coding-system-eol-type
947 buffer-file-coding-system)))
948 (if (and (vectorp default-eol-type)
949 (default-value 'buffer-file-coding-system))
950 (setq default-eol-type
951 (coding-system-eol-type
952 (default-value 'buffer-file-coding-system))))
953 (if (and default-eol-type (not (vectorp default-eol-type)))
954 (dolist (elt default-coding-system)
955 (setcar elt (coding-system-change-eol-conversion
956 (car elt) default-eol-type))))))
958 (let ((codings (find-coding-systems-region from to))
959 (coding-system nil)
960 (tick (if (not (stringp from)) (buffer-chars-modified-tick)))
961 safe rejected unsafe)
962 (if (eq (car codings) 'undecided)
963 ;; Any coding system is ok.
964 (setq coding-system (caar default-coding-system))
965 ;; Reverse the list so that elements are accumulated in safe,
966 ;; rejected, and unsafe in the correct order.
967 (setq default-coding-system (nreverse default-coding-system))
969 ;; Classify the defaults into safe, rejected, and unsafe.
970 (dolist (elt default-coding-system)
971 (if (or (eq (car codings) 'undecided)
972 (memq (cdr elt) codings))
973 (if (and (functionp accept-default-p)
974 (not (funcall accept-default-p (cdr elt))))
975 (push (car elt) rejected)
976 (push (car elt) safe))
977 (push (car elt) unsafe)))
978 (if safe
979 (setq coding-system (car safe))))
981 ;; If all the defaults failed, ask a user.
982 (when (not coding-system)
983 (setq coding-system (select-safe-coding-system-interactively
984 from to codings unsafe rejected (car codings))))
986 ;; Check we're not inconsistent with what `coding:' spec &c would
987 ;; give when file is re-read.
988 ;; But don't do this if we explicitly ignored the cookie
989 ;; by using `find-file-literally'.
990 (when (and auto-cs
991 (not (and
992 coding-system
993 (memq (coding-system-type coding-system) '(0 5)))))
994 ;; Merge coding-system and auto-cs as far as possible.
995 (if (not coding-system)
996 (setq coding-system auto-cs)
997 (if (not auto-cs)
998 (setq auto-cs coding-system)
999 (let ((eol-type-1 (coding-system-eol-type coding-system))
1000 (eol-type-2 (coding-system-eol-type auto-cs)))
1001 (if (eq (coding-system-base coding-system) 'undecided)
1002 (setq coding-system (coding-system-change-text-conversion
1003 coding-system auto-cs))
1004 (if (eq (coding-system-base auto-cs) 'undecided)
1005 (setq auto-cs (coding-system-change-text-conversion
1006 auto-cs coding-system))))
1007 (if (vectorp eol-type-1)
1008 (or (vectorp eol-type-2)
1009 (setq coding-system (coding-system-change-eol-conversion
1010 coding-system eol-type-2)))
1011 (if (vectorp eol-type-2)
1012 (setq auto-cs (coding-system-change-eol-conversion
1013 auto-cs eol-type-1)))))))
1015 (if (and auto-cs
1016 ;; Don't barf if writing a compressed file, say.
1017 ;; This check perhaps isn't ideal, but is probably
1018 ;; the best thing to do.
1019 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
1020 (not (coding-system-equal coding-system auto-cs)))
1021 (unless (yes-or-no-p
1022 (format "Selected encoding %s disagrees with \
1023 %s specified by file contents. Really save (else edit coding cookies \
1024 and try again)? " coding-system auto-cs))
1025 (error "Save aborted"))))
1026 (when (and tick (/= tick (buffer-chars-modified-tick)))
1027 (error "Cancelled because the buffer was modified"))
1028 coding-system)))
1030 (setq select-safe-coding-system-function 'select-safe-coding-system)
1032 (defun select-message-coding-system ()
1033 "Return a coding system to encode the outgoing message of the current buffer.
1034 It at first tries the first coding system found in these variables
1035 in this order:
1036 (1) local value of `buffer-file-coding-system'
1037 (2) value of `sendmail-coding-system'
1038 (3) value of `default-sendmail-coding-system'
1039 (4) default value of `buffer-file-coding-system'
1040 If the found coding system can't encode the current buffer,
1041 or none of them are bound to a coding system,
1042 it asks the user to select a proper coding system."
1043 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
1044 buffer-file-coding-system)
1045 sendmail-coding-system
1046 default-sendmail-coding-system
1047 (default-value 'buffer-file-coding-system))))
1048 (if (eq coding 'no-conversion)
1049 ;; We should never use no-conversion for outgoing mail.
1050 (setq coding nil))
1051 (if (fboundp select-safe-coding-system-function)
1052 (funcall select-safe-coding-system-function
1053 (point-min) (point-max) coding
1054 (function (lambda (x) (coding-system-get x :mime-charset))))
1055 coding)))
1057 ;;; Language support stuff.
1059 (defvar language-info-alist nil
1060 "Alist of language environment definitions.
1061 Each element looks like:
1062 (LANGUAGE-NAME . ((KEY . INFO) ...))
1063 where LANGUAGE-NAME is a string, the name of the language environment,
1064 KEY is a symbol denoting the kind of information, and
1065 INFO is the data associated with KEY.
1066 Meaningful values for KEY include
1068 documentation value is documentation of what this language environment
1069 is meant for, and how to use it.
1070 charset value is a list of the character sets mainly used
1071 by this language environment.
1072 sample-text value is an expression which is evalled to generate
1073 a line of text written using characters appropriate
1074 for this language environment.
1075 setup-function value is a function to call to switch to this
1076 language environment.
1077 exit-function value is a function to call to leave this
1078 language environment.
1079 coding-system value is a list of coding systems that are good for
1080 saving text written in this language environment.
1081 This list serves as suggestions to the user;
1082 in effect, as a kind of documentation.
1083 coding-priority value is a list of coding systems for this language
1084 environment, in order of decreasing priority.
1085 This is used to set up the coding system priority
1086 list when you switch to this language environment.
1087 nonascii-translation
1088 value is a charset of dimension one to use for
1089 converting a unibyte character to multibyte
1090 and vice versa.
1091 input-method value is a default input method for this language
1092 environment.
1093 features value is a list of features requested in this
1094 language environment.
1095 ctext-non-standard-encodings
1096 value is a list of non-standard encoding names used
1097 in extended segments of CTEXT. See the variable
1098 `ctext-non-standard-encodings' for more detail.
1100 The following key takes effect only when multibyte characters are
1101 globally disabled, i.e. the default value of `enable-multibyte-characters'
1102 is nil (which is an obsolete and deprecated use):
1104 unibyte-display value is a coding system to encode characters for
1105 the terminal. Characters in the range of 160 to
1106 255 display not as octal escapes, but as non-ASCII
1107 characters in this language environment.")
1109 (defun get-language-info (lang-env key)
1110 "Return information listed under KEY for language environment LANG-ENV.
1111 KEY is a symbol denoting the kind of information.
1112 For a list of useful values for KEY and their meanings,
1113 see `language-info-alist'."
1114 (if (symbolp lang-env)
1115 (setq lang-env (symbol-name lang-env)))
1116 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1117 (if lang-slot
1118 (cdr (assq key (cdr lang-slot))))))
1120 (defun set-language-info (lang-env key info)
1121 "Modify part of the definition of language environment LANG-ENV.
1122 Specifically, this stores the information INFO under KEY
1123 in the definition of this language environment.
1124 KEY is a symbol denoting the kind of information.
1125 INFO is the value for that information.
1127 For a list of useful values for KEY and their meanings,
1128 see `language-info-alist'."
1129 (if (symbolp lang-env)
1130 (setq lang-env (symbol-name lang-env)))
1131 (set-language-info-internal lang-env key info)
1132 (if (equal lang-env current-language-environment)
1133 (cond ((eq key 'coding-priority)
1134 (set-language-environment-coding-systems lang-env)
1135 (set-language-environment-charset lang-env))
1136 ((eq key 'input-method)
1137 (set-language-environment-input-method lang-env))
1138 ((eq key 'nonascii-translation)
1139 (set-language-environment-nonascii-translation lang-env))
1140 ((eq key 'charset)
1141 (set-language-environment-charset lang-env))
1142 ((and (not (default-value 'enable-multibyte-characters))
1143 (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
1144 (set-language-environment-unibyte lang-env)))))
1146 (defun set-language-info-internal (lang-env key info)
1147 "Internal use only.
1148 Arguments are the same as `set-language-info'."
1149 (let (lang-slot key-slot)
1150 (setq lang-slot (assoc lang-env language-info-alist))
1151 (if (null lang-slot) ; If no slot for the language, add it.
1152 (setq lang-slot (list lang-env)
1153 language-info-alist (cons lang-slot language-info-alist)))
1154 (setq key-slot (assq key lang-slot))
1155 (if (null key-slot) ; If no slot for the key, add it.
1156 (progn
1157 (setq key-slot (list key))
1158 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1159 (setcdr key-slot (purecopy info))
1160 ;; Update the custom-type of `current-language-environment'.
1161 (put 'current-language-environment 'custom-type
1162 (cons 'choice (mapcar
1163 (lambda (lang)
1164 (list 'const lang))
1165 (sort (mapcar 'car language-info-alist) 'string<))))))
1167 (defun set-language-info-alist (lang-env alist &optional parents)
1168 "Store ALIST as the definition of language environment LANG-ENV.
1169 ALIST is an alist of KEY and INFO values. See the documentation of
1170 `language-info-alist' for the meanings of KEY and INFO.
1172 Optional arg PARENTS is a list of parent menu names; it specifies
1173 where to put this language environment in the
1174 Describe Language Environment and Set Language Environment menus.
1175 For example, (\"European\") means to put this language environment
1176 in the European submenu in each of those two menus."
1177 (if (symbolp lang-env)
1178 (setq lang-env (symbol-name lang-env)))
1179 (let ((describe-map describe-language-environment-map)
1180 (setup-map setup-language-environment-map))
1181 (if parents
1182 (let ((l parents)
1183 map parent-symbol parent prompt)
1184 (while l
1185 (if (symbolp (setq parent-symbol (car l)))
1186 (setq parent (symbol-name parent))
1187 (setq parent parent-symbol parent-symbol (intern parent)))
1188 (setq map (lookup-key describe-map (vector parent-symbol)))
1189 ;; This prompt string is for define-prefix-command, so
1190 ;; that the map it creates will be suitable for a menu.
1191 (or map (setq prompt (format "%s Environment" parent)))
1192 (if (not map)
1193 (progn
1194 (setq map (intern (format "describe-%s-environment-map"
1195 (downcase parent))))
1196 (define-prefix-command map nil prompt)
1197 (define-key-after describe-map (vector parent-symbol)
1198 (cons parent map))))
1199 (setq describe-map (symbol-value map))
1200 (setq map (lookup-key setup-map (vector parent-symbol)))
1201 (if (not map)
1202 (progn
1203 (setq map (intern (format "setup-%s-environment-map"
1204 (downcase parent))))
1205 (define-prefix-command map nil prompt)
1206 (define-key-after setup-map (vector parent-symbol)
1207 (cons parent map))))
1208 (setq setup-map (symbol-value map))
1209 (setq l (cdr l)))))
1211 ;; Set up menu items for this language env.
1212 (let ((doc (assq 'documentation alist)))
1213 (when doc
1214 (define-key-after describe-map (vector (intern lang-env))
1215 (cons lang-env 'describe-specified-language-support))))
1216 (define-key-after setup-map (vector (intern lang-env))
1217 (cons lang-env 'setup-specified-language-environment))
1219 (dolist (elt alist)
1220 (set-language-info-internal lang-env (car elt) (cdr elt)))
1222 (if (equal lang-env current-language-environment)
1223 (set-language-environment lang-env))))
1225 (defun read-language-name (key prompt &optional default)
1226 "Read a language environment name which has information for KEY.
1227 If KEY is nil, read any language environment.
1228 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1229 This returns a language environment name as a string."
1230 (let* ((completion-ignore-case t)
1231 (name (completing-read prompt
1232 language-info-alist
1233 (and key
1234 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1235 t nil nil default)))
1236 (if (and (> (length name) 0)
1237 (or (not key)
1238 (get-language-info name key)))
1239 name)))
1241 ;;; Multilingual input methods.
1242 (defgroup leim nil
1243 "LEIM: Libraries of Emacs Input Methods."
1244 :group 'mule)
1246 (defconst leim-list-file-name "leim-list.el"
1247 "Name of LEIM list file.
1248 This file contains a list of libraries of Emacs input methods (LEIM)
1249 in the format of Lisp expression for registering each input method.
1250 Emacs loads this file at startup time.")
1252 (defvar leim-list-header (format
1253 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: utf-8;-*-
1255 ;; This file is automatically generated.
1257 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1258 ;; methods in the same directory as this file. Loading this file
1259 ;; registers all the input methods in Emacs.
1261 ;; Each entry has the form:
1262 ;; (register-input-method
1263 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1264 ;; TITLE DESCRIPTION
1265 ;; ARG ...)
1266 ;; See the function `register-input-method' for the meanings of the arguments.
1268 ;; If this directory is included in `load-path', Emacs automatically
1269 ;; loads this file at startup time.
1272 leim-list-file-name)
1273 "Header to be inserted in LEIM list file.")
1275 (defvar leim-list-entry-regexp "^(register-input-method"
1276 "Regexp matching head of each entry in LEIM list file.
1277 See also the variable `leim-list-header'.")
1279 (defvar update-leim-list-functions
1280 '(quail-update-leim-list-file)
1281 "List of functions to call to update LEIM list file.
1282 Each function is called with one arg, LEIM directory name.")
1284 (defun update-leim-list-file (&rest dirs)
1285 "Update LEIM list file in directories DIRS."
1286 (dolist (function update-leim-list-functions)
1287 (apply function dirs)))
1289 (defvar current-input-method nil
1290 "The current input method for multilingual text.
1291 If nil, that means no input method is activated now.")
1292 (make-variable-buffer-local 'current-input-method)
1293 (put 'current-input-method 'permanent-local t)
1295 (defvar current-input-method-title nil
1296 "Title string of the current input method shown in mode line.")
1297 (make-variable-buffer-local 'current-input-method-title)
1298 (put 'current-input-method-title 'permanent-local t)
1300 (defcustom default-input-method nil
1301 "Default input method for multilingual text (a string).
1302 This is the input method activated automatically by the command
1303 `toggle-input-method' (\\[toggle-input-method])."
1304 :link '(custom-manual "(emacs)Input Methods")
1305 :group 'mule
1306 :type '(choice (const nil) (string
1307 :completion-ignore-case t
1308 :complete-function widget-string-complete
1309 :completion-alist input-method-alist
1310 :prompt-history input-method-history))
1311 :set-after '(current-language-environment))
1313 (put 'input-method-function 'permanent-local t)
1315 (defvar input-method-history nil
1316 "History list of input methods read from the minibuffer.
1318 Maximum length of the history list is determined by the value
1319 of `history-length', which see.")
1320 (make-variable-buffer-local 'input-method-history)
1321 (put 'input-method-history 'permanent-local t)
1323 (defvar inactivate-current-input-method-function nil
1324 "Function to call for inactivating the current input method.
1325 Every input method should set this to an appropriate value when activated.
1326 This function is called with no argument.
1328 This function should never change the value of `current-input-method'.
1329 It is set to nil by the function `inactivate-input-method'.")
1330 (make-variable-buffer-local 'inactivate-current-input-method-function)
1331 (put 'inactivate-current-input-method-function 'permanent-local t)
1333 (defvar describe-current-input-method-function nil
1334 "Function to call for describing the current input method.
1335 This function is called with no argument.")
1336 (make-variable-buffer-local 'describe-current-input-method-function)
1337 (put 'describe-current-input-method-function 'permanent-local t)
1339 (defvar input-method-alist nil
1340 "Alist of input method names vs how to use them.
1341 Each element has the form:
1342 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1343 See the function `register-input-method' for the meanings of the elements.")
1344 ;;;###autoload
1345 (put 'input-method-alist 'risky-local-variable t)
1347 (defun register-input-method (input-method lang-env &rest args)
1348 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1350 INPUT-METHOD and LANG-ENV are symbols or strings.
1351 ACTIVATE-FUNC is a function to call to activate this method.
1352 TITLE is a string to show in the mode line when this method is active.
1353 DESCRIPTION is a string describing this method and what it is good for.
1354 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1355 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1357 This function is mainly used in the file \"leim-list.el\" which is
1358 created at Emacs build time, registering all Quail input methods
1359 contained in the Emacs distribution.
1361 In case you want to register a new Quail input method by yourself, be
1362 careful to use the same input method title as given in the third
1363 parameter of `quail-define-package'. (If the values are different, the
1364 string specified in this function takes precedence.)
1366 The commands `describe-input-method' and `list-input-methods' need
1367 these duplicated values to show some information about input methods
1368 without loading the relevant Quail packages.
1369 \n(fn INPUT-METHOD LANG-ENV ACTIVATE-FUNC TITLE DESCRIPTION &rest ARGS)"
1370 (if (symbolp lang-env)
1371 (setq lang-env (symbol-name lang-env)))
1372 (if (symbolp input-method)
1373 (setq input-method (symbol-name input-method)))
1374 (let ((info (cons lang-env args))
1375 (slot (assoc input-method input-method-alist)))
1376 (if slot
1377 (setcdr slot info)
1378 (setq slot (cons input-method info))
1379 (setq input-method-alist (cons slot input-method-alist)))))
1381 (defun read-input-method-name (prompt &optional default inhibit-null)
1382 "Read a name of input method from a minibuffer prompting with PROMPT.
1383 If DEFAULT is non-nil, use that as the default,
1384 and substitute it into PROMPT at the first `%s'.
1385 If INHIBIT-NULL is non-nil, null input signals an error.
1387 The return value is a string."
1388 (if default
1389 (setq prompt (format prompt default)))
1390 (let* ((completion-ignore-case t)
1391 ;; As it is quite normal to change input method in the
1392 ;; minibuffer, we must enable it even if
1393 ;; enable-recursive-minibuffers is currently nil.
1394 (enable-recursive-minibuffers t)
1395 ;; This binding is necessary because input-method-history is
1396 ;; buffer local.
1397 (input-method (completing-read prompt input-method-alist
1398 nil t nil 'input-method-history
1399 default)))
1400 (if (and input-method (symbolp input-method))
1401 (setq input-method (symbol-name input-method)))
1402 (if (> (length input-method) 0)
1403 input-method
1404 (if inhibit-null
1405 (error "No valid input method is specified")))))
1407 (defun activate-input-method (input-method)
1408 "Switch to input method INPUT-METHOD for the current buffer.
1409 If some other input method is already active, turn it off first.
1410 If INPUT-METHOD is nil, deactivate any current input method."
1411 (if (and input-method (symbolp input-method))
1412 (setq input-method (symbol-name input-method)))
1413 (if (and current-input-method
1414 (not (string= current-input-method input-method)))
1415 (inactivate-input-method))
1416 (unless (or current-input-method (null input-method))
1417 (let ((slot (assoc input-method input-method-alist)))
1418 (if (null slot)
1419 (error "Can't activate input method `%s'" input-method))
1420 (setq current-input-method-title nil)
1421 (let ((func (nth 2 slot)))
1422 (if (functionp func)
1423 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1424 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1425 (progn
1426 (require (cdr func))
1427 (apply (car func) input-method (nthcdr 5 slot)))
1428 (error "Can't activate input method `%s'" input-method))))
1429 (setq current-input-method input-method)
1430 (or (stringp current-input-method-title)
1431 (setq current-input-method-title (nth 3 slot)))
1432 (unwind-protect
1433 (run-hooks 'input-method-activate-hook)
1434 (force-mode-line-update)))))
1436 (defun inactivate-input-method ()
1437 "Turn off the current input method."
1438 (when current-input-method
1439 (if input-method-history
1440 (unless (string= current-input-method (car input-method-history))
1441 (setq input-method-history
1442 (cons current-input-method
1443 (delete current-input-method input-method-history))))
1444 (setq input-method-history (list current-input-method)))
1445 (unwind-protect
1446 (progn
1447 (setq input-method-function nil
1448 current-input-method-title nil)
1449 (funcall inactivate-current-input-method-function))
1450 (unwind-protect
1451 (run-hooks 'input-method-inactivate-hook)
1452 (setq current-input-method nil)
1453 (force-mode-line-update)))))
1455 (defun set-input-method (input-method &optional interactive)
1456 "Select and activate input method INPUT-METHOD for the current buffer.
1457 This also sets the default input method to the one you specify.
1458 If INPUT-METHOD is nil, this function turns off the input method, and
1459 also causes you to be prompted for a name of an input method the next
1460 time you invoke \\[toggle-input-method].
1461 When called interactively, the optional arg INTERACTIVE is non-nil,
1462 which marks the variable `default-input-method' as set for Custom buffers.
1464 To deactivate the input method interactively, use \\[toggle-input-method].
1465 To deactivate it programmatically, use `inactivate-input-method'."
1466 (interactive
1467 (let* ((default (or (car input-method-history) default-input-method)))
1468 (list (read-input-method-name
1469 (if default "Select input method (default %s): " "Select input method: ")
1470 default t)
1471 t)))
1472 (activate-input-method input-method)
1473 (setq default-input-method input-method)
1474 (when interactive
1475 (customize-mark-as-set 'default-input-method))
1476 default-input-method)
1478 (defvar toggle-input-method-active nil
1479 "Non-nil inside `toggle-input-method'.")
1481 (defun toggle-input-method (&optional arg interactive)
1482 "Enable or disable multilingual text input method for the current buffer.
1483 Only one input method can be enabled at any time in a given buffer.
1485 The normal action is to enable an input method if none was enabled,
1486 and disable the current one otherwise. Which input method to enable
1487 can be determined in various ways--either the one most recently used,
1488 or the one specified by `default-input-method', or as a last resort
1489 by reading the name of an input method in the minibuffer.
1491 With a prefix argument ARG, read an input method name with the minibuffer
1492 and enable that one. The default is the most recent input method specified
1493 \(not including the currently active input method, if any).
1495 When called interactively, the optional argument INTERACTIVE is non-nil,
1496 which marks the variable `default-input-method' as set for Custom buffers."
1498 (interactive "P\np")
1499 (if toggle-input-method-active
1500 (error "Recursive use of `toggle-input-method'"))
1501 (if (and current-input-method (not arg))
1502 (inactivate-input-method)
1503 (let ((toggle-input-method-active t)
1504 (default (or (car input-method-history) default-input-method)))
1505 (if (and arg default (equal current-input-method default)
1506 (> (length input-method-history) 1))
1507 (setq default (nth 1 input-method-history)))
1508 (activate-input-method
1509 (if (or arg (not default))
1510 (progn
1511 (read-input-method-name
1512 (if default "Input method (default %s): " "Input method: " )
1513 default t))
1514 default))
1515 (unless default-input-method
1516 (prog1
1517 (setq default-input-method current-input-method)
1518 (when interactive
1519 (customize-mark-as-set 'default-input-method)))))))
1521 (autoload 'help-buffer "help-mode")
1523 (defun describe-input-method (input-method)
1524 "Describe input method INPUT-METHOD."
1525 (interactive
1526 (list (read-input-method-name
1527 "Describe input method (default current choice): ")))
1528 (if (and input-method (symbolp input-method))
1529 (setq input-method (symbol-name input-method)))
1530 (help-setup-xref (list #'describe-input-method
1531 (or input-method current-input-method))
1532 (called-interactively-p 'interactive))
1534 (if (null input-method)
1535 (describe-current-input-method)
1536 (let ((current current-input-method))
1537 (condition-case nil
1538 (progn
1539 (save-excursion
1540 (activate-input-method input-method)
1541 (describe-current-input-method))
1542 (activate-input-method current))
1543 (error
1544 (activate-input-method current)
1545 (help-setup-xref (list #'describe-input-method input-method)
1546 (called-interactively-p 'interactive))
1547 (with-output-to-temp-buffer (help-buffer)
1548 (let ((elt (assoc input-method input-method-alist)))
1549 (princ (format
1550 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1551 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1553 (defun describe-current-input-method ()
1554 "Describe the input method currently in use.
1555 This is a subroutine for `describe-input-method'."
1556 (if current-input-method
1557 (if (and (symbolp describe-current-input-method-function)
1558 (fboundp describe-current-input-method-function))
1559 (funcall describe-current-input-method-function)
1560 (message "No way to describe the current input method `%s'"
1561 current-input-method)
1562 (ding))
1563 (error "No input method is activated now")))
1565 (defun read-multilingual-string (prompt &optional initial-input input-method)
1566 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1567 The input method selected last time is activated in minibuffer.
1568 If optional second argument INITIAL-INPUT is non-nil, insert it in the
1569 minibuffer initially.
1570 Optional 3rd argument INPUT-METHOD specifies the input method to be activated
1571 instead of the one selected last time. It is a symbol or a string."
1572 (setq input-method
1573 (or input-method
1574 current-input-method
1575 default-input-method
1576 (read-input-method-name "Input method: " nil t)))
1577 (if (and input-method (symbolp input-method))
1578 (setq input-method (symbol-name input-method)))
1579 (let ((prev-input-method current-input-method))
1580 (unwind-protect
1581 (progn
1582 (activate-input-method input-method)
1583 (read-string prompt initial-input nil nil t))
1584 (activate-input-method prev-input-method))))
1586 ;; Variables to control behavior of input methods. All input methods
1587 ;; should react to these variables.
1589 (defcustom input-method-verbose-flag 'default
1590 "A flag to control extra guidance given by input methods.
1591 The value should be nil, t, `complex-only', or `default'.
1593 The extra guidance is done by showing list of available keys in echo
1594 area. When you use the input method in the minibuffer, the guidance
1595 is shown at the bottom short window (split from the existing window).
1597 If the value is t, extra guidance is always given, if the value is
1598 nil, extra guidance is always suppressed.
1600 If the value is `complex-only', only complex input methods such as
1601 `chinese-py' and `japanese' give extra guidance.
1603 If the value is `default', complex input methods always give extra
1604 guidance, but simple input methods give it only when you are not in
1605 the minibuffer.
1607 See also the variable `input-method-highlight-flag'."
1608 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1609 (const complex-only) (const default))
1610 :group 'mule)
1612 (defcustom input-method-highlight-flag t
1613 "If this flag is non-nil, input methods highlight partially-entered text.
1614 For instance, while you are in the middle of a Quail input method sequence,
1615 the text inserted so far is temporarily underlined.
1616 The underlining goes away when you finish or abort the input method sequence.
1617 See also the variable `input-method-verbose-flag'."
1618 :type 'boolean
1619 :group 'mule)
1621 (defcustom input-method-activate-hook nil
1622 "Normal hook run just after an input method is activated.
1624 The variable `current-input-method' keeps the input method name
1625 just activated."
1626 :type 'hook
1627 :group 'mule)
1629 (defcustom input-method-inactivate-hook nil
1630 "Normal hook run just after an input method is inactivated.
1632 The variable `current-input-method' still keeps the input method name
1633 just inactivated."
1634 :type 'hook
1635 :group 'mule)
1637 (defcustom input-method-after-insert-chunk-hook nil
1638 "Normal hook run just after an input method insert some chunk of text."
1639 :type 'hook
1640 :group 'mule)
1642 (defvar input-method-exit-on-first-char nil
1643 "This flag controls when an input method returns.
1644 Usually, the input method does not return while there's a possibility
1645 that it may find a different translation if a user types another key.
1646 But, if this flag is non-nil, the input method returns as soon as the
1647 current key sequence gets long enough to have some valid translation.")
1649 (defcustom input-method-use-echo-area nil
1650 "This flag controls how an input method shows an intermediate key sequence.
1651 Usually, the input method inserts the intermediate key sequence,
1652 or candidate translations corresponding to the sequence,
1653 at point in the current buffer.
1654 But, if this flag is non-nil, it displays them in echo area instead."
1655 :type 'hook
1656 :group 'mule)
1658 (defvar input-method-exit-on-invalid-key nil
1659 "This flag controls the behavior of an input method on invalid key input.
1660 Usually, when a user types a key which doesn't start any character
1661 handled by the input method, the key is handled by turning off the
1662 input method temporarily. After that key, the input method is re-enabled.
1663 But, if this flag is non-nil, the input method is never back on.")
1666 (defcustom set-language-environment-hook nil
1667 "Normal hook run after some language environment is set.
1669 When you set some hook function here, that effect usually should not
1670 be inherited to another language environment. So, you had better set
1671 another function in `exit-language-environment-hook' (which see) to
1672 cancel the effect."
1673 :type 'hook
1674 :group 'mule)
1676 (defcustom exit-language-environment-hook nil
1677 "Normal hook run after exiting from some language environment.
1678 When this hook is run, the variable `current-language-environment'
1679 is still bound to the language environment being exited.
1681 This hook is mainly used for canceling the effect of
1682 `set-language-environment-hook' (which see)."
1683 :type 'hook
1684 :group 'mule)
1686 (put 'setup-specified-language-environment 'apropos-inhibit t)
1688 (defun setup-specified-language-environment ()
1689 "Switch to a specified language environment."
1690 (interactive)
1691 (let (language-name)
1692 (if (and (symbolp last-command-event)
1693 (or (not (eq last-command-event 'Default))
1694 (setq last-command-event 'English))
1695 (setq language-name (symbol-name last-command-event)))
1696 (prog1
1697 (set-language-environment language-name)
1698 (customize-mark-as-set 'current-language-environment))
1699 (error "Bogus calling sequence"))))
1701 (defcustom current-language-environment "English"
1702 "The last language environment specified with `set-language-environment'.
1703 This variable should be set only with \\[customize], which is equivalent
1704 to using the function `set-language-environment'."
1705 :link '(custom-manual "(emacs)Language Environments")
1706 :set (lambda (symbol value) (set-language-environment value))
1707 :get (lambda (x)
1708 (or (car-safe (assoc-string
1709 (if (symbolp current-language-environment)
1710 (symbol-name current-language-environment)
1711 current-language-environment)
1712 language-info-alist t))
1713 "English"))
1714 ;; custom type will be updated with `set-language-info'.
1715 :type (if language-info-alist
1716 (cons 'choice (mapcar
1717 (lambda (lang)
1718 (list 'const lang))
1719 (sort (mapcar 'car language-info-alist) 'string<)))
1720 'string)
1721 :initialize 'custom-initialize-default
1722 :group 'mule)
1724 (defun reset-language-environment ()
1725 "Reset multilingual environment of Emacs to the default status.
1727 The default status is as follows:
1729 The default value of `buffer-file-coding-system' is nil.
1730 The default coding system for process I/O is nil.
1731 The default value for the command `set-terminal-coding-system' is nil.
1732 The default value for the command `set-keyboard-coding-system' is nil.
1734 The order of priorities of coding systems are as follows:
1735 utf-8
1736 iso-2022-7bit
1737 iso-latin-1
1738 iso-2022-7bit-lock
1739 iso-2022-8bit-ss2
1740 emacs-mule
1741 raw-text"
1742 (interactive)
1743 ;; This function formerly set default-enable-multibyte-characters to t,
1744 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1746 (set-coding-system-priority
1747 'utf-8
1748 'iso-2022-7bit
1749 'iso-latin-1
1750 'iso-2022-7bit-lock
1751 'iso-2022-8bit-ss2
1752 'emacs-mule
1753 'raw-text)
1755 (set-default-coding-systems nil)
1756 (setq default-sendmail-coding-system 'iso-latin-1)
1757 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1758 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1759 (setq default-file-name-coding-system 'iso-latin-1)
1760 ;; Preserve eol-type from existing default-process-coding-systems.
1761 ;; On non-unix-like systems in particular, these may have been set
1762 ;; carefully by the user, or by the startup code, to deal with the
1763 ;; users shell appropriately, so should not be altered by changing
1764 ;; language environment.
1765 (let ((output-coding
1766 ;; When bootstrapping, coding-systems are not defined yet, so
1767 ;; we need to catch the error from check-coding-system.
1768 (condition-case nil
1769 (coding-system-change-text-conversion
1770 (car default-process-coding-system) 'undecided)
1771 (coding-system-error 'undecided)))
1772 (input-coding
1773 (condition-case nil
1774 (coding-system-change-text-conversion
1775 (cdr default-process-coding-system) 'iso-latin-1)
1776 (coding-system-error 'iso-latin-1))))
1777 (setq default-process-coding-system
1778 (cons output-coding input-coding)))
1780 ;; Put the highest priority to the charset iso-8859-1 to prefer the
1781 ;; registry iso8859-1 over iso8859-2 in font selection. It also
1782 ;; makes unibyte-display-via-language-environment to use iso-8859-1
1783 ;; as the unibyte charset.
1784 (set-charset-priority 'iso-8859-1)
1786 ;; Don't alter the terminal and keyboard coding systems here.
1787 ;; The terminal still supports the same coding system
1788 ;; that it supported a minute ago.
1789 ;; (set-terminal-coding-system-internal nil)
1790 ;; (set-keyboard-coding-system-internal nil)
1792 ;; Back in Emacs-20, it was necessary to provide some fallback implicit
1793 ;; conversion, because almost no packages handled coding-system issues.
1794 ;; Nowadays it'd just paper over bugs.
1795 ;; (set-unibyte-charset 'iso-8859-1)
1798 (reset-language-environment)
1800 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system display)
1801 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1802 (let ((coding (get-language-info language-name 'unibyte-display)))
1803 (if (and coding
1804 (or (not coding-system)
1805 (coding-system-equal coding coding-system)))
1806 (standard-display-european-internal)
1807 ;; The following 2 lines undo the 8-bit display that we set up
1808 ;; in standard-display-european-internal, which see. This is in
1809 ;; case the user has used standard-display-european earlier in
1810 ;; this session.
1811 (when standard-display-table
1812 (dotimes (i 128)
1813 (aset standard-display-table (+ i 128) nil))))
1814 (set-terminal-coding-system (or coding-system coding) display)))
1816 (defun set-language-environment (language-name)
1817 "Set up multi-lingual environment for using LANGUAGE-NAME.
1818 This sets the coding system priority and the default input method
1819 and sometimes other things. LANGUAGE-NAME should be a string
1820 which is the name of a language environment. For example, \"Latin-1\"
1821 specifies the character set for the major languages of Western Europe."
1822 (interactive (list (read-language-name
1824 "Set language environment (default English): ")))
1825 (if language-name
1826 (if (symbolp language-name)
1827 (setq language-name (symbol-name language-name)))
1828 (setq language-name "English"))
1829 (let ((slot (assoc-string language-name language-info-alist t)))
1830 (unless slot
1831 (error "Language environment not defined: %S" language-name))
1832 (setq language-name (car slot)))
1833 (if current-language-environment
1834 (let ((func (get-language-info current-language-environment
1835 'exit-function)))
1836 (run-hooks 'exit-language-environment-hook)
1837 (if (functionp func) (funcall func))))
1839 (reset-language-environment)
1840 ;; The features might set up coding systems.
1841 (let ((required-features (get-language-info language-name 'features)))
1842 (while required-features
1843 (require (car required-features))
1844 (setq required-features (cdr required-features))))
1846 (setq current-language-environment language-name)
1848 (set-language-environment-coding-systems language-name)
1849 (set-language-environment-input-method language-name)
1850 (set-language-environment-nonascii-translation language-name)
1851 (set-language-environment-charset language-name)
1852 ;; Unibyte setups if necessary.
1853 (unless (default-value 'enable-multibyte-characters)
1854 (set-language-environment-unibyte language-name))
1856 (let ((func (get-language-info language-name 'setup-function)))
1857 (if (functionp func)
1858 (funcall func)))
1860 (setq current-iso639-language
1861 (or (get-language-info language-name 'iso639-language)
1862 current-iso639-language))
1864 (run-hooks 'set-language-environment-hook)
1865 (force-mode-line-update t))
1867 (define-widget 'charset 'symbol
1868 "An Emacs charset."
1869 :tag "Charset"
1870 :complete-function (lambda ()
1871 (interactive)
1872 (lisp-complete-symbol 'charsetp))
1873 :completion-ignore-case t
1874 :value 'ascii
1875 :validate (lambda (widget)
1876 (unless (charsetp (widget-value widget))
1877 (widget-put widget :error (format "Invalid charset: %S"
1878 (widget-value widget)))
1879 widget))
1880 :prompt-history 'charset-history)
1882 (defcustom language-info-custom-alist nil
1883 "Customizations of language environment parameters.
1884 Value is an alist with elements like those of `language-info-alist'.
1885 These are used to set values in `language-info-alist' which replace
1886 the defaults. A typical use is replacing the default input method for
1887 the environment. Use \\[describe-language-environment] to find the environment's settings.
1889 This option is intended for use at startup. Removing items doesn't
1890 remove them from the language info until you next restart Emacs.
1892 Setting this variable directly does not take effect.
1893 See `set-language-info-alist' for use in programs."
1894 :group 'mule
1895 :version "23.1"
1896 :set (lambda (s v)
1897 (custom-set-default s v)
1898 ;; Can't do this before language environments are set up.
1899 (when v
1900 ;; modify language-info-alist
1901 (dolist (elt v)
1902 (set-language-info-alist (car elt) (cdr elt)))
1903 ;; re-set the environment in case its parameters changed
1904 (set-language-environment current-language-environment)))
1905 :type `(alist
1906 :key-type (string :tag "Language environment"
1907 :completion-ignore-case t
1908 :complete-function widget-string-complete
1909 :completion-alist language-info-alist)
1910 :value-type
1911 (alist :key-type symbol
1912 :options ((documentation string)
1913 (charset (repeat charset))
1914 (sample-text string)
1915 (setup-function function)
1916 (exit-function function)
1917 (coding-system (repeat coding-system))
1918 (coding-priority (repeat coding-system))
1919 (nonascii-translation charset)
1920 (input-method
1921 (string
1922 :completion-ignore-case t
1923 :complete-function widget-string-complete
1924 :completion-alist input-method-alist
1925 :prompt-history input-method-history))
1926 (features (repeat symbol))
1927 (unibyte-display coding-system)))))
1929 (declare-function x-server-vendor "xfns.c" (&optional terminal))
1930 (declare-function x-server-version "xfns.c" (&optional terminal))
1932 (defun standard-display-european-internal ()
1933 ;; Actually set up direct output of non-ASCII characters.
1934 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1935 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1936 ;; the native font, and codes 160 and 146 stand for something very
1937 ;; different there.
1938 (or (and (eq window-system 'pc) (not (default-value
1939 'enable-multibyte-characters)))
1940 (progn
1941 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1942 (unless (and (eq window-system 'x)
1943 ;; XFree86 4 has fixed the fonts.
1944 (string= "The XFree86 Project, Inc" (x-server-vendor))
1945 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1946 ?3))
1947 ;; Make non-line-break space display as a plain space.
1948 (aset standard-display-table 160 [32]))
1949 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1950 ;; don't contain a character at that position. Map it to the ASCII
1951 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1952 ;; U+2019, normally from the windows-1252 character set. XFree 4
1953 ;; fonts probably have the appropriate glyph at this position,
1954 ;; so they could use standard-display-8bit. It's better to use a
1955 ;; proper windows-1252 coding system. --fx]
1956 (aset standard-display-table 146 [39]))))
1958 (defun set-language-environment-coding-systems (language-name)
1959 "Do various coding system setups for language environment LANGUAGE-NAME."
1960 (let* ((priority (get-language-info language-name 'coding-priority))
1961 (default-coding (car priority))
1962 ;; If the default buffer-file-coding-system is nil, don't use
1963 ;; coding-system-eol-type, because it treats nil as
1964 ;; `no-conversion'. The default buffer-file-coding-system is set
1965 ;; to nil by reset-language-environment, and in that case we
1966 ;; want to have here the native EOL type for each platform.
1967 ;; FIXME: there should be a common code that runs both on
1968 ;; startup and here to set the default EOL type correctly.
1969 ;; Right now, DOS/Windows platforms set this on dos-w32.el,
1970 ;; which works only as long as the order of loading files at
1971 ;; dump time and calling functions at startup is not modified
1972 ;; significantly, i.e. as long as this function is called
1973 ;; _after_ the default buffer-file-coding-system was set by
1974 ;; dos-w32.el.
1975 (eol-type
1976 (coding-system-eol-type
1977 (or (default-value 'buffer-file-coding-system)
1978 (if (memq system-type '(windows-nt ms-dos)) 'dos 'unix)))))
1979 (when priority
1980 (set-default-coding-systems
1981 (if (memq eol-type '(0 1 2 unix dos mac))
1982 (coding-system-change-eol-conversion default-coding eol-type)
1983 default-coding))
1984 (setq default-sendmail-coding-system default-coding)
1985 (apply 'set-coding-system-priority priority))))
1987 (defun set-language-environment-input-method (language-name)
1988 "Do various input method setups for language environment LANGUAGE-NAME."
1989 (let ((input-method (get-language-info language-name 'input-method)))
1990 (when input-method
1991 (setq default-input-method input-method)
1992 (if input-method-history
1993 (setq input-method-history
1994 (cons input-method
1995 (delete input-method input-method-history)))))))
1997 (defun set-language-environment-nonascii-translation (language-name)
1998 "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
1999 ;; Note: For DOS, we assumed that the charset cpXXX is already
2000 ;; defined.
2001 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
2002 (if (eq window-system 'pc)
2003 (setq nonascii (intern (format "cp%d" dos-codepage))))
2004 (or (and (charsetp nonascii)
2005 (get-charset-property nonascii :ascii-compatible-p))
2006 (setq nonascii 'iso-8859-1))
2007 ;; Back in Emacs-20, it was necessary to provide some fallback implicit
2008 ;; conversion, because almost no packages handled coding-system issues.
2009 ;; Nowadays it'd just paper over bugs.
2010 ;; (set-unibyte-charset nonascii)
2013 (defun set-language-environment-charset (language-name)
2014 "Do various charset setups for language environment LANGUAGE-NAME."
2015 ;; Put higher priorities to such charsets that are supported by the
2016 ;; coding systems of higher priorities in this environment.
2017 (let ((charsets (get-language-info language-name 'charset)))
2018 (dolist (coding (get-language-info language-name 'coding-priority))
2019 (let ((list (coding-system-charset-list coding)))
2020 (if (consp list)
2021 (setq charsets (append charsets list)))))
2022 (if charsets
2023 (apply 'set-charset-priority charsets))))
2025 (defun set-language-environment-unibyte (language-name)
2026 "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
2027 (set-display-table-and-terminal-coding-system language-name))
2029 (defsubst princ-list (&rest args)
2030 "Print all arguments with `princ', then print \"\\n\"."
2031 (while args (princ (car args)) (setq args (cdr args)))
2032 (princ "\n"))
2034 (put 'describe-specified-language-support 'apropos-inhibit t)
2036 ;; Print language-specific information such as input methods,
2037 ;; charsets, and coding systems. This function is intended to be
2038 ;; called from the menu:
2039 ;; [menu-bar mule describe-language-environment LANGUAGE]
2040 ;; and should not run it by `M-x describe-current-input-method-function'.
2041 (defun describe-specified-language-support ()
2042 "Describe how Emacs supports the specified language environment."
2043 (interactive)
2044 (let (language-name)
2045 (if (not (and (symbolp last-command-event)
2046 (or (not (eq last-command-event 'Default))
2047 (setq last-command-event 'English))
2048 (setq language-name (symbol-name last-command-event))))
2049 (error "Bogus calling sequence"))
2050 (describe-language-environment language-name)))
2052 (defun describe-language-environment (language-name)
2053 "Describe how Emacs supports language environment LANGUAGE-NAME."
2054 (interactive
2055 (list (read-language-name
2056 'documentation
2057 "Describe language environment (default current choice): ")))
2058 (if (null language-name)
2059 (setq language-name current-language-environment))
2060 (if (or (null language-name)
2061 (null (get-language-info language-name 'documentation)))
2062 (error "No documentation for the specified language"))
2063 (if (symbolp language-name)
2064 (setq language-name (symbol-name language-name)))
2065 (dolist (feature (get-language-info language-name 'features))
2066 (require feature))
2067 (let ((doc (get-language-info language-name 'documentation)))
2068 (help-setup-xref (list #'describe-language-environment language-name)
2069 (called-interactively-p 'interactive))
2070 (with-output-to-temp-buffer (help-buffer)
2071 (save-excursion
2072 (set-buffer standard-output)
2073 (insert language-name " language environment\n\n")
2074 (if (stringp doc)
2075 (insert doc "\n\n"))
2076 (condition-case nil
2077 (let ((str (eval (get-language-info language-name 'sample-text))))
2078 (if (stringp str)
2079 (insert "Sample text:\n "
2080 (replace-regexp-in-string "\n" "\n " str)
2081 "\n\n")))
2082 (error nil))
2083 (let ((input-method (get-language-info language-name 'input-method))
2084 (l (copy-sequence input-method-alist))
2085 (first t))
2086 (when (and input-method
2087 (setq input-method (assoc input-method l)))
2088 (insert "Input methods (default " (car input-method) ")\n")
2089 (setq l (cons input-method (delete input-method l))
2090 first nil))
2091 (dolist (elt l)
2092 (when (or (eq input-method elt)
2093 (eq t (compare-strings language-name nil nil
2094 (nth 1 elt) nil nil t)))
2095 (when first
2096 (insert "Input methods:\n")
2097 (setq first nil))
2098 (insert " " (car elt))
2099 (search-backward (car elt))
2100 (help-xref-button 0 'help-input-method (car elt))
2101 (goto-char (point-max))
2102 (insert " (\""
2103 (if (stringp (nth 3 elt)) (nth 3 elt) (car (nth 3 elt)))
2104 "\" in mode line)\n")))
2105 (or first
2106 (insert "\n")))
2107 (insert "Character sets:\n")
2108 (let ((l (get-language-info language-name 'charset)))
2109 (if (null l)
2110 (insert " nothing specific to " language-name "\n")
2111 (while l
2112 (insert " " (symbol-name (car l)))
2113 (search-backward (symbol-name (car l)))
2114 (help-xref-button 0 'help-character-set (car l))
2115 (goto-char (point-max))
2116 (insert ": " (charset-description (car l)) "\n")
2117 (setq l (cdr l)))))
2118 (insert "\n")
2119 (insert "Coding systems:\n")
2120 (let ((l (get-language-info language-name 'coding-system)))
2121 (if (null l)
2122 (insert " nothing specific to " language-name "\n")
2123 (while l
2124 (insert " " (symbol-name (car l)))
2125 (search-backward (symbol-name (car l)))
2126 (help-xref-button 0 'help-coding-system (car l))
2127 (goto-char (point-max))
2128 (insert " (`"
2129 (coding-system-mnemonic (car l))
2130 "' in mode line):\n\t"
2131 (coding-system-doc-string (car l))
2132 "\n")
2133 (let ((aliases (coding-system-aliases (car l))))
2134 (when aliases
2135 (insert "\t(alias:")
2136 (while aliases
2137 (insert " " (symbol-name (car aliases)))
2138 (setq aliases (cdr aliases)))
2139 (insert ")\n")))
2140 (setq l (cdr l)))))))))
2142 ;;; Locales.
2144 (defvar locale-translation-file-name nil
2145 "File name for the system's file of locale-name aliases, or nil if none.")
2147 ;; The following definitions might as well be marked as constants and
2148 ;; purecopied, since they're normally used on startup, and probably
2149 ;; should reflect the facilities of the base Emacs.
2150 (defconst locale-language-names
2151 (purecopy
2153 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2154 ;; as specified in the Single Unix Spec, Version 2.
2155 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2156 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2157 ;; see Internet RFC 2165 (1997-06) and
2158 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2159 ;; TERRITORY is a country code taken from ISO 3166
2160 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2161 ;; CODESET and MODIFIER are implementation-dependent.
2163 ;; jasonr comments: MS Windows uses three letter codes for
2164 ;; languages instead of the two letter ISO codes that POSIX
2165 ;; uses. In most cases the first two letters are the same, so
2166 ;; most of the regexps in locale-language-names work. Japanese
2167 ;; and Chinese are exceptions, which are listed in the
2168 ;; non-standard section at the bottom of locale-language-names.
2170 ("aa_DJ" . "Latin-1") ; Afar
2171 ("aa" . "UTF-8")
2172 ;; ab Abkhazian
2173 ("af" . "Latin-1") ; Afrikaans
2174 ("am" "Ethiopic" utf-8) ; Amharic
2175 ("an" . "Latin-9") ; Aragonese
2176 ; ar Arabic glibc uses 8859-6
2177 ; as Assamese
2178 ; ay Aymara
2179 ("az" . "UTF-8") ; Azerbaijani
2180 ; ba Bashkir
2181 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2182 ("bg" "Bulgarian" cp1251) ; Bulgarian
2183 ; bh Bihari
2184 ; bi Bislama
2185 ("bn" . "UTF-8") ; Bengali, Bangla
2186 ("bo" . "Tibetan")
2187 ("br" . "Latin-1") ; Breton
2188 ("bs" . "Latin-2") ; Bosnian
2189 ("byn" . "UTF-8") ; Bilin; Blin
2190 ("ca" . "Latin-1") ; Catalan
2191 ; co Corsican
2192 ("cs" "Czech" iso-8859-2)
2193 ("cy" "Welsh" iso-8859-14)
2194 ("da" . "Latin-1") ; Danish
2195 ("de" "German" iso-8859-1)
2196 ; dv Divehi
2197 ; dz Bhutani
2198 ("el" "Greek" iso-8859-7)
2199 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2200 ;; That's actually what the GNU locales define, modulo things like
2201 ;; en_IN -- fx.
2202 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2203 ("en" "English" iso-8859-1) ; English
2204 ("eo" . "Esperanto") ; Esperanto
2205 ("es" "Spanish" iso-8859-1)
2206 ("et" . "Latin-1") ; Estonian
2207 ("eu" . "Latin-1") ; Basque
2208 ("fa" . "UTF-8") ; Persian
2209 ("fi" . "Latin-1") ; Finnish
2210 ("fj" . "Latin-1") ; Fiji
2211 ("fo" . "Latin-1") ; Faroese
2212 ("fr" "French" iso-8859-1) ; French
2213 ("fy" . "Latin-1") ; Frisian
2214 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2215 ("gd" . "Latin-9") ; Scots Gaelic
2216 ("gez" "Ethiopic" utf-8) ; Geez
2217 ("gl" . "Latin-1") ; Gallegan; Galician
2218 ; gn Guarani
2219 ("gu" . "UTF-8") ; Gujarati
2220 ("gv" . "Latin-1") ; Manx Gaelic
2221 ; ha Hausa
2222 ("he" "Hebrew" iso-8859-8)
2223 ("hi" "Devanagari" utf-8) ; Hindi
2224 ("hr" "Croatian" iso-8859-2) ; Croatian
2225 ("hu" . "Latin-2") ; Hungarian
2226 ; hy Armenian
2227 ; ia Interlingua
2228 ("id" . "Latin-1") ; Indonesian
2229 ; ie Interlingue
2230 ; ik Inupiak
2231 ("is" . "Latin-1") ; Icelandic
2232 ("it" "Italian" iso-8859-1) ; Italian
2233 ; iu Inuktitut
2234 ("iw" "Hebrew" iso-8859-8)
2235 ("ja" "Japanese" euc-jp)
2236 ; jw Javanese
2237 ("ka" "Georgian" georgian-ps) ; Georgian
2238 ; kk Kazakh
2239 ("kl" . "Latin-1") ; Greenlandic
2240 ; km Cambodian
2241 ("kn" "Kannada" utf-8)
2242 ("ko" "Korean" euc-kr)
2243 ; ks Kashmiri
2244 ; ku Kurdish
2245 ("kw" . "Latin-1") ; Cornish
2246 ; ky Kirghiz
2247 ("la" . "Latin-1") ; Latin
2248 ("lb" . "Latin-1") ; Luxemburgish
2249 ("lg" . "Laint-6") ; Ganda
2250 ; ln Lingala
2251 ("lo" "Lao" utf-8) ; Laothian
2252 ("lt" "Lithuanian" iso-8859-13)
2253 ("lv" . "Latvian") ; Latvian, Lettish
2254 ; mg Malagasy
2255 ("mi" . "Latin-7") ; Maori
2256 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2257 ("ml" "Malayalam" utf-8)
2258 ("mn" . "UTF-8") ; Mongolian
2259 ; mo Moldavian
2260 ("mr" "Devanagari" utf-8) ; Marathi
2261 ("ms" . "Latin-1") ; Malay
2262 ("mt" . "Latin-3") ; Maltese
2263 ; my Burmese
2264 ; na Nauru
2265 ("nb" . "Latin-1") ; Norwegian
2266 ("ne" "Devanagari" utf-8) ; Nepali
2267 ("nl" "Dutch" iso-8859-1)
2268 ("no" . "Latin-1") ; Norwegian
2269 ("oc" . "Latin-1") ; Occitan
2270 ("om_ET" . "UTF-8") ; (Afan) Oromo
2271 ("om" . "Latin-1") ; (Afan) Oromo
2272 ; or Oriya
2273 ("pa" . "UTF-8") ; Punjabi
2274 ("pl" . "Latin-2") ; Polish
2275 ; ps Pashto, Pushto
2276 ("pt" . "Latin-1") ; Portuguese
2277 ; qu Quechua
2278 ("rm" . "Latin-1") ; Rhaeto-Romanic
2279 ; rn Kirundi
2280 ("ro" "Romanian" iso-8859-2)
2281 ("ru_RU" "Russian" iso-8859-5)
2282 ("ru_UA" "Russian" koi8-u)
2283 ; rw Kinyarwanda
2284 ("sa" . "Devanagari") ; Sanskrit
2285 ; sd Sindhi
2286 ("se" . "UTF-8") ; Northern Sami
2287 ; sg Sangho
2288 ("sh" . "Latin-2") ; Serbo-Croatian
2289 ; si Sinhalese
2290 ("sid" . "UTF-8") ; Sidamo
2291 ("sk" "Slovak" iso-8859-2)
2292 ("sl" "Slovenian" iso-8859-2)
2293 ; sm Samoan
2294 ; sn Shona
2295 ("so_ET" "UTF-8") ; Somali
2296 ("so" "Latin-1") ; Somali
2297 ("sq" . "Latin-1") ; Albanian
2298 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2299 ; ss Siswati
2300 ("st" . "Latin-1") ; Sesotho
2301 ; su Sundanese
2302 ("sv" "Swedish" iso-8859-1) ; Swedish
2303 ("sw" . "Latin-1") ; Swahili
2304 ("ta" "Tamil" utf-8)
2305 ("te" . "UTF-8") ; Telugu
2306 ("tg" "Tajik" koi8-t)
2307 ("th" "Thai" tis-620)
2308 ("ti" "Ethiopic" utf-8) ; Tigrinya
2309 ("tig_ER" . "UTF-8") ; Tigre
2310 ; tk Turkmen
2311 ("tl" . "Latin-1") ; Tagalog
2312 ; tn Setswana
2313 ; to Tonga
2314 ("tr" "Turkish" iso-8859-9)
2315 ; ts Tsonga
2316 ("tt" . "UTF-8") ; Tatar
2317 ; tw Twi
2318 ; ug Uighur
2319 ("uk" "Ukrainian" koi8-u)
2320 ("ur" . "UTF-8") ; Urdu
2321 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2322 ("uz" . "Latin-1") ; Uzbek
2323 ("vi" "Vietnamese" utf-8)
2324 ; vo Volapuk
2325 ("wa" . "Latin-1") ; Walloon
2326 ; wo Wolof
2327 ("xh" . "Latin-1") ; Xhosa
2328 ("yi" . "Windows-1255") ; Yiddish
2329 ; yo Yoruba
2330 ; za Zhuang
2331 ("zh_HK" . "Chinese-Big5")
2332 ; zh_HK/BIG5-HKSCS \
2333 ("zh_TW" . "Chinese-Big5")
2334 ("zh_CN.GB2312" "Chinese-GB")
2335 ("zh_CN.GBK" "Chinese-GBK")
2336 ("zh_CN.GB18030" "Chinese-GB18030")
2337 ("zh_CN.UTF-8" . "Chinese-GBK")
2338 ("zh_CN" . "Chinese-GB")
2339 ("zh" . "Chinese-GB")
2340 ("zu" . "Latin-1") ; Zulu
2342 ;; ISO standard locales
2343 ("c$" . "ASCII")
2344 ("posix$" . "ASCII")
2346 ;; The "IPA" Emacs language environment does not correspond
2347 ;; to any ISO 639 code, so let it stand for itself.
2348 ("ipa$" . "IPA")
2350 ;; Nonstandard or obsolete language codes
2351 ("cz" . "Czech") ; e.g. Solaris 2.6
2352 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2353 ("iw" . "Hebrew") ; e.g. X11R6.4
2354 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2355 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2356 ("jp" . "Japanese") ; e.g. MS Windows
2357 ("chs" . "Chinese-GBK") ; MS Windows Chinese Simplified
2358 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2359 ("gbz" . "UTF-8") ; MS Windows Dari Persian
2360 ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
2361 ("wee" . "Latin-2") ; MS Windows Lower Sorbian
2362 ("wen" . "Latin-2") ; MS Windows Upper Sorbian
2364 "Alist of locale regexps vs the corresponding languages and coding systems.
2365 Each element has this form:
2366 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2367 The first element whose LOCALE-REGEXP matches the start of a
2368 downcased locale specifies the LANG-ENV \(language environment)
2369 and CODING-SYSTEM corresponding to that locale. If there is no
2370 appropriate language environment, the element may have this form:
2371 \(LOCALE-REGEXP . LANG-ENV)
2372 In this case, LANG-ENV is one of generic language environments for an
2373 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2375 (defconst locale-charset-language-names
2376 (purecopy
2377 '((".*8859[-_]?1\\>" . "Latin-1")
2378 (".*8859[-_]?2\\>" . "Latin-2")
2379 (".*8859[-_]?3\\>" . "Latin-3")
2380 (".*8859[-_]?4\\>" . "Latin-4")
2381 (".*8859[-_]?9\\>" . "Latin-5")
2382 (".*8859[-_]?14\\>" . "Latin-8")
2383 (".*8859[-_]?15\\>" . "Latin-9")
2384 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2385 ;; utf-8@euro exists, so put this last. (@euro really specifies
2386 ;; the currency, rather than the charset.)
2387 (".*@euro\\>" . "Latin-9")))
2388 "List of pairs of locale regexps and charset language names.
2389 The first element whose locale regexp matches the start of a downcased locale
2390 specifies the language name whose charset corresponds to that locale.
2391 This language name is used if the locale is not listed in
2392 `locale-language-names'.")
2394 (defconst locale-preferred-coding-systems
2395 (purecopy
2396 '((".*8859[-_]?1\\>" . iso-8859-1)
2397 (".*8859[-_]?2\\>" . iso-8859-2)
2398 (".*8859[-_]?3\\>" . iso-8859-3)
2399 (".*8859[-_]?4\\>" . iso-8859-4)
2400 (".*8859[-_]?9\\>" . iso-8859-9)
2401 (".*8859[-_]?14\\>" . iso-8859-14)
2402 (".*8859[-_]?15\\>" . iso-8859-15)
2403 (".*utf\\(?:-?8\\)?" . utf-8)
2404 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2405 ;; specifies the currency, rather than the charset.)
2406 (".*@euro" . iso-8859-15)
2407 ("koi8-?r" . koi8-r)
2408 ("koi8-?u" . koi8-u)
2409 ("tcvn" . tcvn)
2410 ("big5[-_]?hkscs" . big5-hkscs)
2411 ("big5" . big5)
2412 ("euc-?tw" . euc-tw)
2413 ("euc-?cn" . euc-cn)
2414 ("gb2312" . gb2312)
2415 ("gbk" . gbk)
2416 ("gb18030" . gb18030)
2417 ("ja.*[._]euc" . japanese-iso-8bit)
2418 ("ja.*[._]jis7" . iso-2022-jp)
2419 ("ja.*[._]pck" . japanese-shift-jis)
2420 ("ja.*[._]sjis" . japanese-shift-jis)
2421 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2423 "List of pairs of locale regexps and preferred coding systems.
2424 The first element whose locale regexp matches the start of a downcased locale
2425 specifies the coding system to prefer when using that locale.
2426 This coding system is used if the locale specifies a specific charset.")
2428 (defun locale-name-match (key alist)
2429 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2430 Return the value corresponding to the first regexp that matches the
2431 start of KEY, or nil if there is no match."
2432 (let (element)
2433 (while (and alist (not element))
2434 (if (string-match-p (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2435 (setq element (car alist)))
2436 (setq alist (cdr alist)))
2437 (cdr element)))
2439 (defun locale-charset-match-p (charset1 charset2)
2440 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2441 Matching is done ignoring case and any hyphens and underscores in the
2442 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2443 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2444 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2445 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2447 (defvar locale-charset-alist nil
2448 "Coding system alist keyed on locale-style charset name.
2449 Used by `locale-charset-to-coding-system'.")
2451 (defun locale-charset-to-coding-system (charset)
2452 "Find coding system corresponding to CHARSET.
2453 CHARSET is any sort of non-Emacs charset name, such as might be used
2454 in a locale codeset, or elsewhere. It is matched to a coding system
2455 first by case-insensitive lookup in `locale-charset-alist'. Then
2456 matches are looked for in the coding system list, treating case and
2457 the characters `-' and `_' as insignificant. The coding system base
2458 is returned. Thus, for instance, if charset \"ISO8859-2\",
2459 `iso-latin-2' is returned."
2460 (or (car (assoc-string charset locale-charset-alist t))
2461 (let ((cs coding-system-alist)
2463 (while (and (not c) cs)
2464 (if (locale-charset-match-p charset (caar cs))
2465 (setq c (intern (caar cs)))
2466 (pop cs)))
2467 (if c (coding-system-base c)))))
2469 ;; Fixme: This ought to deal with the territory part of the locale
2470 ;; too, for setting things such as calendar holidays, ps-print paper
2471 ;; size, spelling dictionary.
2473 (defun locale-translate (locale)
2474 "Expand LOCALE according to `locale-translation-file-name', if possible.
2475 For example, translate \"swedish\" into \"sv_SE.ISO8859-1\"."
2476 (if locale-translation-file-name
2477 (with-temp-buffer
2478 (set-buffer-multibyte nil)
2479 (insert-file-contents locale-translation-file-name)
2480 (if (re-search-forward
2481 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2482 (buffer-substring (point) (line-end-position))
2483 locale))
2484 locale))
2486 (defun set-locale-environment (&optional locale-name frame)
2487 "Set up multi-lingual environment for using LOCALE-NAME.
2488 This sets the language environment, the coding system priority,
2489 the default input method and sometimes other things.
2491 LOCALE-NAME should be a string which is the name of a locale supported
2492 by the system. Often it is of the form xx_XX.CODE, where xx is a
2493 language, XX is a country, and CODE specifies a character set and
2494 coding system. For example, the locale name \"ja_JP.EUC\" might name
2495 a locale for Japanese in Japan using the `japanese-iso-8bit'
2496 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2497 or `@cyrillic'.
2499 If LOCALE-NAME is nil, its value is taken from the environment
2500 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2502 The locale names supported by your system can typically be found in a
2503 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2504 will be translated according to the table specified by
2505 `locale-translation-file-name'.
2507 If FRAME is non-nil, only set the keyboard coding system and the
2508 terminal coding system for the terminal of that frame, and don't
2509 touch session-global parameters like the language environment.
2511 See also `locale-charset-language-names', `locale-language-names',
2512 `locale-preferred-coding-systems' and `locale-coding-system'."
2513 (interactive "sSet environment for locale: ")
2515 ;; Do this at runtime for the sake of binaries possibly transported
2516 ;; to a system without X.
2517 (setq locale-translation-file-name
2518 (let ((files
2519 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2520 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2521 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2522 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2524 ;; The following name appears after the X-related names above,
2525 ;; since the X-related names are what X actually uses.
2526 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2528 (while (and files (not (file-exists-p (car files))))
2529 (setq files (cdr files)))
2530 (car files)))
2532 (let ((locale locale-name))
2534 (unless locale
2535 ;; Use the first of these three environment variables
2536 ;; that has a nonempty value.
2537 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2538 (while (and vars
2539 (= 0 (length locale))) ; nil or empty string
2540 (setq locale (getenv (pop vars) frame)))))
2542 (when locale
2543 (setq locale (locale-translate locale))
2545 ;; Leave the system locales alone if the caller did not specify
2546 ;; an explicit locale name, as their defaults are set from
2547 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2548 ;; want to set them to the same value as LC_CTYPE.
2549 (when locale-name
2550 (setq system-messages-locale locale)
2551 (setq system-time-locale locale))
2553 (if (string-match "^[a-z][a-z]" locale)
2554 (setq current-iso639-language (intern (match-string 0 locale)))))
2556 (setq woman-locale
2557 (or system-messages-locale
2558 (let ((msglocale (getenv "LC_MESSAGES" frame)))
2559 (if (zerop (length msglocale))
2560 locale
2561 (locale-translate msglocale)))))
2563 (when locale
2564 (setq locale (downcase locale))
2566 (let ((language-name
2567 (locale-name-match locale locale-language-names))
2568 (charset-language-name
2569 (locale-name-match locale locale-charset-language-names))
2570 (default-eol-type (coding-system-eol-type
2571 (default-value 'buffer-file-coding-system)))
2572 (coding-system
2573 (or (locale-name-match locale locale-preferred-coding-systems)
2574 (when locale
2575 (if (string-match "\\.\\([^@]+\\)" locale)
2576 (locale-charset-to-coding-system
2577 (match-string 1 locale)))))))
2579 (if (consp language-name)
2580 ;; locale-language-names specify both lang-env and coding.
2581 ;; But, what specified in locale-preferred-coding-systems
2582 ;; has higher priority.
2583 (setq coding-system (or coding-system
2584 (nth 1 language-name))
2585 language-name (car language-name))
2586 ;; Otherwise, if locale is not listed in locale-language-names,
2587 ;; use what listed in locale-charset-language-names.
2588 (if (not language-name)
2589 (setq language-name charset-language-name)))
2591 ;; If a specific EOL conversion was specified in the default
2592 ;; buffer-file-coding-system, preserve it in the coding system
2593 ;; we will be using from now on.
2594 (if (and (memq default-eol-type '(0 1 2 unix dos mac))
2595 coding-system
2596 (coding-system-p coding-system))
2597 (setq coding-system (coding-system-change-eol-conversion
2598 coding-system default-eol-type)))
2600 (when language-name
2602 ;; Set up for this character set. This is now the right way
2603 ;; to do it for both unibyte and multibyte modes.
2604 (unless frame
2605 (set-language-environment language-name))
2607 ;; If the default enable-multibyte-characters is nil,
2608 ;; we are using single-byte characters,
2609 ;; so the display table and terminal coding system are irrelevant.
2610 (when (default-value 'enable-multibyte-characters)
2611 (set-display-table-and-terminal-coding-system
2612 language-name coding-system frame))
2614 ;; Set the `keyboard-coding-system' if appropriate (tty
2615 ;; only). At least X and MS Windows can generate
2616 ;; multilingual input.
2617 ;; XXX This was disabled unless `window-system', but that
2618 ;; leads to buggy behavior when a tty frame is opened
2619 ;; later. Setting the keyboard coding system has no adverse
2620 ;; effect on X, so let's do it anyway. -- Lorentey
2621 (let ((kcs (or coding-system
2622 (car (get-language-info language-name
2623 'coding-system)))))
2624 (if kcs (set-keyboard-coding-system kcs frame)))
2626 (unless frame
2627 (setq locale-coding-system
2628 (car (get-language-info language-name 'coding-priority)))))
2630 (when (and (not frame)
2631 coding-system
2632 (not (coding-system-equal coding-system
2633 locale-coding-system)))
2634 (prefer-coding-system coding-system)
2635 ;; Fixme: perhaps prefer-coding-system should set this too.
2636 ;; But it's not the time to do such a fundamental change.
2637 (setq default-sendmail-coding-system coding-system)
2638 (setq locale-coding-system coding-system))))
2640 ;; On Windows, override locale-coding-system,
2641 ;; default-file-name-coding-system, keyboard-coding-system,
2642 ;; terminal-coding-system with system codepage.
2643 (when (boundp 'w32-ansi-code-page)
2644 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2645 (when (coding-system-p code-page-coding)
2646 (unless frame (setq locale-coding-system code-page-coding))
2647 (set-keyboard-coding-system code-page-coding frame)
2648 (set-terminal-coding-system code-page-coding frame)
2649 ;; Set default-file-name-coding-system last, so that Emacs
2650 ;; doesn't try to use cpNNNN when it defines keyboard and
2651 ;; terminal encoding. That's because the above two lines
2652 ;; will want to load code-pages.el, where cpNNNN are
2653 ;; defined; if default-file-name-coding-system were set to
2654 ;; cpNNNN while these two lines run, Emacs will want to use
2655 ;; it for encoding the file name it wants to load. And that
2656 ;; will fail, since cpNNNN is not yet usable until
2657 ;; code-pages.el finishes loading.
2658 (setq default-file-name-coding-system code-page-coding))))
2660 (when (eq system-type 'darwin)
2661 ;; On Darwin, file names are always encoded in utf-8, no matter
2662 ;; the locale.
2663 (setq default-file-name-coding-system 'utf-8)
2664 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2665 ;; the locale.
2666 (when (and (null window-system)
2667 (equal (getenv "TERM_PROGRAM" frame) "Apple_Terminal"))
2668 (set-terminal-coding-system 'utf-8)
2669 (set-keyboard-coding-system 'utf-8)))
2671 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2672 ;; (See comments in Flocale_info.)
2673 (unless frame
2674 (let ((locale locale)
2675 (paper (locale-info 'paper)))
2676 (if paper
2677 ;; This will always be null at the time of writing.
2678 (cond
2679 ((equal paper '(216 279))
2680 (setq ps-paper-type 'letter))
2681 ((equal paper '(210 297))
2682 (setq ps-paper-type 'a4)))
2683 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2684 (while (and vars (= 0 (length locale)))
2685 (setq locale (getenv (pop vars) frame))))
2686 (when locale
2687 ;; As of glibc 2.2.5, these are the only US Letter locales,
2688 ;; and the rest are A4.
2689 (setq ps-paper-type
2690 (or (locale-name-match locale '(("c$" . letter)
2691 ("posix$" . letter)
2692 (".._us" . letter)
2693 (".._pr" . letter)
2694 (".._ca" . letter)
2695 ("enu$" . letter) ; Windows
2696 ("esu$" . letter)
2697 ("enc$" . letter)
2698 ("frc$" . letter)))
2699 'a4)))))))
2700 nil)
2702 ;;; Character property
2704 ;; Each element has the form (PROP . TABLE).
2705 ;; PROP is a symbol representing a character property.
2706 ;; TABLE is a char-table containing the property value for each character.
2707 ;; TABLE may be a name of file to load to build a char-table.
2708 ;; Don't modify this variable directly but use `define-char-code-property'.
2710 (defvar char-code-property-alist nil
2711 "Alist of character property name vs char-table containing property values.
2712 Internal use only.")
2714 (put 'char-code-property-table 'char-table-extra-slots 5)
2716 (defun define-char-code-property (name table &optional docstring)
2717 "Define NAME as a character code property given by TABLE.
2718 TABLE is a char-table of purpose `char-code-property-table' with
2719 these extra slots:
2720 1st: NAME.
2721 2nd: Function to call to get a property value of a character.
2722 It is called with three arguments CHAR, VAL, and TABLE, where
2723 CHAR is a character, VAL is the value of (aref TABLE CHAR).
2724 3rd: Function to call to put a property value of a character.
2725 It is called with the same arguments as above.
2726 4th: Function to call to get a description string of a property value.
2727 It is called with one argument VALUE, a property value.
2728 5th: Data used by the above functions.
2730 TABLE may be a name of file to load to build a char-table. The
2731 file should contain a call of `define-char-code-property' with a
2732 char-table of the above format as the argument TABLE.
2734 TABLE may also be nil, in which case no property value is pre-assigned.
2736 Optional 3rd argument DOCSTRING is a documentation string of the property.
2738 See also the documentation of `get-char-code-property' and
2739 `put-char-code-property'."
2740 (or (symbolp name)
2741 (error "Not a symbol: %s" name))
2742 (if (char-table-p table)
2743 (or (and (eq (char-table-subtype table) 'char-code-property-table)
2744 (eq (char-table-extra-slot table 0) name))
2745 (error "Invalid char-table: %s" table))
2746 (or (stringp table)
2747 (error "Not a char-table nor a file name: %s" table)))
2748 (let ((slot (assq name char-code-property-alist)))
2749 (if slot
2750 (setcdr slot table)
2751 (setq char-code-property-alist
2752 (cons (cons name table) char-code-property-alist))))
2753 (put name 'char-code-property-documentation docstring))
2755 (defvar char-code-property-table
2756 (make-char-table 'char-code-property-table)
2757 "Char-table containing a property list of each character code.
2758 This table is used for properties not listed in `char-code-property-alist'.
2759 See also the documentation of `get-char-code-property' and
2760 `put-char-code-property'.")
2762 (defun get-char-code-property (char propname)
2763 "Return the value of CHAR's PROPNAME property."
2764 (let ((slot (assq propname char-code-property-alist)))
2765 (if slot
2766 (let (table value func)
2767 (if (stringp (cdr slot))
2768 (load (cdr slot) nil t))
2769 (setq table (cdr slot)
2770 value (aref table char)
2771 func (char-table-extra-slot table 1))
2772 (if (functionp func)
2773 (setq value (funcall func char value table)))
2774 value)
2775 (plist-get (aref char-code-property-table char) propname))))
2777 (defun put-char-code-property (char propname value)
2778 "Store CHAR's PROPNAME property with VALUE.
2779 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2780 (let ((slot (assq propname char-code-property-alist)))
2781 (if slot
2782 (let (table func)
2783 (if (stringp (cdr slot))
2784 (load (cdr slot) nil t))
2785 (setq table (cdr slot)
2786 func (char-table-extra-slot table 2))
2787 (if (functionp func)
2788 (funcall func char value table)
2789 (aset table char value)))
2790 (let* ((plist (aref char-code-property-table char))
2791 (x (plist-put plist propname value)))
2792 (or (eq x plist)
2793 (aset char-code-property-table char x))))
2794 value))
2796 (defun char-code-property-description (prop value)
2797 "Return a description string of character property PROP's value VALUE.
2798 If there's no description string for VALUE, return nil."
2799 (let ((slot (assq prop char-code-property-alist)))
2800 (if slot
2801 (let (table func)
2802 (if (stringp (cdr slot))
2803 (load (cdr slot) nil t))
2804 (setq table (cdr slot)
2805 func (char-table-extra-slot table 3))
2806 (if (functionp func)
2807 (funcall func value))))))
2810 ;; Pretty description of encoded string
2812 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2813 (defvar iso-2022-control-alist
2814 '((?\x1b . "ESC")
2815 (?\x0e . "SO")
2816 (?\x0f . "SI")
2817 (?\x8e . "SS2")
2818 (?\x8f . "SS3")
2819 (?\x9b . "CSI")))
2821 (defun encoded-string-description (str coding-system)
2822 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2823 (setq str (string-as-unibyte str))
2824 (mapconcat
2825 (if (and coding-system (eq (coding-system-type coding-system) 'iso-2022))
2826 ;; Try to get a pretty description for ISO 2022 escape sequences.
2827 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2828 (format "#x%02X" x))))
2829 (function (lambda (x) (format "#x%02X" x))))
2830 str " "))
2832 (defun encode-coding-char (char coding-system &optional charset)
2833 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2834 If CODING-SYSTEM can't safely encode CHAR, return nil.
2835 The 3rd optional argument CHARSET, if non-nil, is a charset preferred
2836 on encoding."
2837 (let* ((str1 (string-as-multibyte (string char)))
2838 (str2 (string-as-multibyte (string char char)))
2839 (found (find-coding-systems-string str1))
2840 enc1 enc2 i1 i2)
2841 (if (and (consp found)
2842 (eq (car found) 'undecided))
2843 str1
2844 (when (memq (coding-system-base coding-system) found)
2845 ;; We must find the encoded string of CHAR. But, just encoding
2846 ;; CHAR will put extra control sequences (usually to designate
2847 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2848 ;; To exclude such tailing bytes, we at first encode one-char
2849 ;; string and two-char string, then check how many bytes at the
2850 ;; tail of both encoded strings are the same.
2852 (when charset
2853 (put-text-property 0 1 'charset charset str1)
2854 (put-text-property 0 2 'charset charset str2))
2855 (setq enc1 (encode-coding-string str1 coding-system)
2856 i1 (length enc1)
2857 enc2 (encode-coding-string str2 coding-system)
2858 i2 (length enc2))
2859 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2860 (setq i1 (1- i1) i2 (1- i2)))
2862 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2863 ;; and they are the extra control sequences at the tail to
2864 ;; exclude.
2865 (substring enc2 0 i2)))))
2867 ;; Backwards compatibility. These might be better with :init-value t,
2868 ;; but that breaks loadup.
2869 (define-minor-mode unify-8859-on-encoding-mode
2870 "Obsolete."
2871 :group 'mule
2872 :global t)
2873 (define-minor-mode unify-8859-on-decoding-mode
2874 "Obsolete."
2875 :group 'mule
2876 :global t)
2878 (defvar nonascii-insert-offset 0 "This variable is obsolete.")
2879 (defvar nonascii-translation-table nil "This variable is obsolete.")
2881 (defvar ucs-names nil
2882 "Alist of cached (CHAR-NAME . CHAR-CODE) pairs.")
2884 (defun ucs-names ()
2885 "Return alist of (CHAR-NAME . CHAR-CODE) pairs cached in `ucs-names'."
2886 (or ucs-names
2887 (setq ucs-names
2888 (let (name names)
2889 (dotimes-with-progress-reporter (c #xEFFFF)
2890 "Loading Unicode character names..."
2891 (unless (or
2892 (and (>= c #x3400 ) (<= c #x4dbf )) ; CJK Ideograph Extension A
2893 (and (>= c #x4e00 ) (<= c #x9fff )) ; CJK Ideograph
2894 (and (>= c #xd800 ) (<= c #xfaff )) ; Private/Surrogate
2895 (and (>= c #x20000) (<= c #x2ffff)) ; CJK Ideograph Extensions B, C
2897 (if (setq name (get-char-code-property c 'name))
2898 (setq names (cons (cons name c) names)))
2899 (if (setq name (get-char-code-property c 'old-name))
2900 (setq names (cons (cons name c) names)))))
2901 names))))
2903 (defvar ucs-completions (lazy-completion-table ucs-completions ucs-names)
2904 "Lazy completion table for completing on Unicode character names.")
2905 (put 'ucs-completions 'risky-local-variable t)
2907 (defun read-char-by-name (prompt)
2908 "Read a character by its Unicode name or hex number string.
2909 Display PROMPT and read a string that represents a character by its
2910 Unicode property `name' or `old-name'. You can type a few of first
2911 letters of the Unicode name and use completion. This function also
2912 accepts a hexadecimal number of Unicode code point or a number in
2913 hash notation, e.g. #o21430 for octal, #x2318 for hex, or #10r8984
2914 for decimal. Returns a character as a number."
2915 (let* ((completion-ignore-case t)
2916 (input (completing-read prompt ucs-completions)))
2917 (cond
2918 ((string-match-p "^[0-9a-fA-F]+$" input)
2919 (string-to-number input 16))
2920 ((string-match-p "^#" input)
2921 (read input))
2923 (cdr (assoc-string input (ucs-names) t))))))
2925 (defun ucs-insert (character &optional count inherit)
2926 "Insert COUNT copies of CHARACTER of the given Unicode code point.
2927 Interactively, prompts for a Unicode character name or a hex number
2928 using `read-char-by-name'.
2929 The optional third arg INHERIT (non-nil when called interactively),
2930 says to inherit text properties from adjoining text, if those
2931 properties are sticky."
2932 (interactive
2933 (list (read-char-by-name "Unicode (name or hex): ")
2934 (prefix-numeric-value current-prefix-arg)
2936 (unless count (setq count 1))
2937 (if (stringp character)
2938 (setq character (string-to-number character 16)))
2939 (cond
2940 ((not (integerp character))
2941 (error "Not a Unicode character code: %S" character))
2942 ((or (< character 0) (> character #x10FFFF))
2943 (error "Not a Unicode character code: 0x%X" character)))
2944 (if inherit
2945 (dotimes (i count) (insert-and-inherit character))
2946 (dotimes (i count) (insert character))))
2948 (define-key ctl-x-map "8\r" 'ucs-insert)
2950 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2951 ;;; mule-cmds.el ends here