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