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