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