1 ;;; mule-cmds.el --- commands for mulitilingual environment -*-coding: iso-2022-7bit -*-
3 ;; Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
4 ;; Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005
6 ;; National Institute of Advanced Industrial Science and Technology (AIST)
7 ;; Registration Number H14PRO021
9 ;; National Institute of Advanced Industrial Science and Technology (AIST)
10 ;; Registration Number H13PRO009
12 ;; Keywords: mule, i18n
14 ;; This file is part of GNU Emacs.
16 ;; GNU Emacs is free software; you can redistribute it and/or modify
17 ;; it under the terms of the GNU General Public License as published by
18 ;; the Free Software Foundation; either version 2, or (at your option)
21 ;; GNU Emacs is distributed in the hope that it will be useful,
22 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
23 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 ;; GNU General Public License for more details.
26 ;; You should have received a copy of the GNU General Public License
27 ;; along with GNU Emacs; see the file COPYING. If not, write to the
28 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
29 ;; Boston, MA 02110-1301, USA.
37 (autoload 'widget-value "wid-edit"))
39 (defvar mac-system-coding-system)
40 (defvar mac-system-locale)
42 ;;; MULE related key bindings and menus.
44 (defvar mule-keymap (make-sparse-keymap)
45 "Keymap for Mule (Multilingual environment) specific commands.")
47 ;; Keep "C-x C-m ..." for mule specific commands.
48 (define-key ctl-x-map "\C-m" mule-keymap)
50 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
51 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
52 (define-key mule-keymap "F" 'set-file-name-coding-system)
53 (define-key mule-keymap "t" 'set-terminal-coding-system)
54 (define-key mule-keymap "k" 'set-keyboard-coding-system)
55 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
56 (define-key mule-keymap "x" 'set-selection-coding-system)
57 (define-key mule-keymap "X" 'set-next-selection-coding-system)
58 (define-key mule-keymap "\C-\\" 'set-input-method)
59 (define-key mule-keymap "c" 'universal-coding-system-argument)
60 (define-key mule-keymap "l" 'set-language-environment)
62 (defvar mule-menu-keymap
63 (make-sparse-keymap "Mule (Multilingual Environment)")
64 "Keymap for Mule (Multilingual environment) menu specific commands.")
66 (defvar describe-language-environment-map
67 (make-sparse-keymap "Describe Language Environment"))
69 (defvar setup-language-environment-map
70 (make-sparse-keymap "Set Language Environment"))
72 (defvar set-coding-system-map
73 (make-sparse-keymap "Set Coding System"))
75 (define-key-after mule-menu-keymap [set-language-environment]
76 (list 'menu-item "Set Language Environment" setup-language-environment-map
77 :help "Multilingual environment suitable for a specific language"))
78 (define-key-after mule-menu-keymap [separator-mule]
81 (define-key-after mule-menu-keymap [toggle-input-method]
82 '(menu-item "Toggle Input Method" toggle-input-method)
84 (define-key-after mule-menu-keymap [set-input-method]
85 '(menu-item "Select Input Method..." set-input-method)
87 (define-key-after mule-menu-keymap [describe-input-method]
88 '(menu-item "Describe Input Method" describe-input-method))
89 (define-key-after mule-menu-keymap [separator-input-method]
92 (define-key-after mule-menu-keymap [set-various-coding-system]
93 (list 'menu-item "Set Coding Systems" set-coding-system-map
94 :enable 'default-enable-multibyte-characters))
95 (define-key-after mule-menu-keymap [view-hello-file]
96 '(menu-item "Show Multi-lingual Text" view-hello-file
97 :enable (file-readable-p
98 (expand-file-name "HELLO" data-directory))
99 :help "Display file which says HELLO in many languages")
101 (define-key-after mule-menu-keymap [separator-coding-system]
104 (define-key-after mule-menu-keymap [describe-language-environment]
105 (list 'menu-item "Describe Language Environment"
106 describe-language-environment-map
107 :help "Show multilingual settings for a specific language")
109 (define-key-after mule-menu-keymap [describe-input-method]
110 '(menu-item "Describe Input Method..." describe-input-method
111 :help "Keyboard layout for a specific input method")
113 (define-key-after mule-menu-keymap [describe-coding-system]
114 '(menu-item "Describe Coding System..." describe-coding-system)
116 (define-key-after mule-menu-keymap [list-character-sets]
117 '(menu-item "List Character Sets" list-character-sets
118 :help "Show table of available character sets"))
119 (define-key-after mule-menu-keymap [mule-diag]
120 '(menu-item "Show All of Mule Status" mule-diag
121 :help "Display multilingual environment settings")
124 (define-key-after set-coding-system-map [universal-coding-system-argument]
125 '(menu-item "For Next Command" universal-coding-system-argument
126 :help "Coding system to be used by next command")
128 (define-key-after set-coding-system-map [separator-1]
131 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
132 '(menu-item "For Saving This Buffer" set-buffer-file-coding-system
133 :help "How to encode this buffer when saved")
135 (define-key-after set-coding-system-map [revert-buffer-with-coding-system]
136 '(menu-item "For Reverting This File Now" revert-buffer-with-coding-system
137 :enable buffer-file-name
138 :help "Revisit this file immediately using specified coding system")
140 (define-key-after set-coding-system-map [set-file-name-coding-system]
141 '(menu-item "For File Name" set-file-name-coding-system
142 :help "How to decode/encode file names")
144 (define-key-after set-coding-system-map [separator-2]
148 (define-key-after set-coding-system-map [set-keyboard-coding-system]
149 '(menu-item "For Keyboard" set-keyboard-coding-system
150 :help "How to decode keyboard input")
152 (define-key-after set-coding-system-map [set-terminal-coding-system]
153 '(menu-item "For Terminal" set-terminal-coding-system
154 :enable (null (memq window-system '(x w32 mac)))
155 :help "How to encode terminal output")
157 (define-key-after set-coding-system-map [separator-3]
160 (define-key-after set-coding-system-map [set-selection-coding-system]
161 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
162 :visible (display-selections-p)
163 :help "How to en/decode data to/from selection/clipboard")
165 (define-key-after set-coding-system-map [set-next-selection-coding-system]
166 '(menu-item "For Next X Selection" set-next-selection-coding-system
167 :visible (display-selections-p)
168 :help "How to en/decode next selection/clipboard operation")
170 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
171 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
172 :visible (fboundp 'start-process)
173 :enable (get-buffer-process (current-buffer))
174 :help "How to en/decode I/O from/to subprocess connected to this buffer")
178 (define-key setup-language-environment-map
179 [Default] '(menu-item "Default" setup-specified-language-environment))
181 (define-key describe-language-environment-map
182 [Default] '(menu-item "Default" describe-specified-language-support))
184 ;; This should be a single character key binding because users use it
185 ;; very frequently while editing multilingual text. Now we can use
186 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
187 ;; convenient because it requires shifting on most keyboards. An
188 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
189 ;; but it won't be used that frequently.
190 (define-key global-map "\C-\\" 'toggle-input-method)
192 ;; This is no good because people often type Shift-SPC
193 ;; meaning to type SPC. -- rms.
194 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
195 ;; (define-key global-map [?\S- ] 'toggle-input-method)
197 ;;; Mule related hyperlinks.
198 (defconst help-xref-mule-regexp-template
199 (purecopy (concat "\\(\\<\\("
200 "\\(coding system\\)\\|"
201 "\\(input method\\)\\|"
202 "\\(character set\\)\\|"
205 ;; Note starting with word-syntax character:
206 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
208 (defun coding-system-change-eol-conversion (coding-system eol-type)
209 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
210 The returned coding system converts end-of-line by EOL-TYPE
211 but text as the same way as CODING-SYSTEM.
212 EOL-TYPE should be `unix', `dos', `mac', or nil.
213 If EOL-TYPE is nil, the returned coding system detects
214 how end-of-line is formatted automatically while decoding.
216 EOL-TYPE can be specified by an integer 0, 1, or 2.
217 They means `unix', `dos', and `mac' respectively."
218 (if (symbolp eol-type)
219 (setq eol-type (cond ((eq eol-type 'unix) 0)
220 ((eq eol-type 'dos) 1)
221 ((eq eol-type 'mac) 2)
223 (let ((orig-eol-type (coding-system-eol-type coding-system)))
224 (if (vectorp orig-eol-type)
227 (aref orig-eol-type eol-type))
228 (let ((base (coding-system-base coding-system)))
231 (if (= eol-type orig-eol-type)
233 (setq orig-eol-type (coding-system-eol-type base))
234 (if (vectorp orig-eol-type)
235 (aref orig-eol-type eol-type))))))))
237 (defun coding-system-change-text-conversion (coding-system coding)
238 "Return a coding system which differs from CODING-SYSTEM in text conversion.
239 The returned coding system converts text by CODING
240 but end-of-line as the same way as CODING-SYSTEM.
241 If CODING is nil, the returned coding system detects
242 how text is formatted automatically while decoding."
243 (let ((eol-type (coding-system-eol-type coding-system)))
244 (coding-system-change-eol-conversion
245 (if coding coding 'undecided)
246 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
248 (defun toggle-enable-multibyte-characters (&optional arg)
249 "Change whether this buffer uses multibyte characters.
250 With arg, use multibyte characters if the arg is positive.
252 Note that this command does not convert the byte contents of
253 the buffer; it only changes the way those bytes are interpreted.
254 In general, therefore, this command *changes* the sequence of
255 characters that the current buffer contains.
257 We suggest you avoid using use this command unless you know what you
258 are doing. If you use it by mistake, and the buffer is now displayed
259 wrong, use this command again to toggle back to the right mode."
262 (if (null arg) (null enable-multibyte-characters)
263 (> (prefix-numeric-value arg) 0))))
264 (set-buffer-multibyte new-flag))
265 (force-mode-line-update))
267 (defun view-hello-file ()
268 "Display the HELLO file which list up many languages and characters."
270 ;; We have to decode the file in any environment.
271 (let ((default-enable-multibyte-characters t)
272 (coding-system-for-read 'iso-2022-7bit))
273 (view-file (expand-file-name "HELLO" data-directory))))
275 (defun universal-coding-system-argument (coding-system)
276 "Execute an I/O command using the specified coding system."
278 (let ((default (and buffer-file-coding-system
279 (not (eq (coding-system-type buffer-file-coding-system)
281 buffer-file-coding-system)))
282 (list (read-coding-system
284 (format "Coding system for following command (default %s): " default)
285 "Coding system for following command: ")
287 (let* ((keyseq (read-key-sequence
288 (format "Command to execute with %s:" coding-system)))
289 (cmd (key-binding keyseq))
292 (when (eq cmd 'universal-argument)
293 (call-interactively cmd)
295 ;; Process keys bound in `universal-argument-map'.
297 (setq keyseq (read-key-sequence nil t)
298 cmd (key-binding keyseq t))
299 (not (eq cmd 'universal-argument-other-key)))
300 (let ((current-prefix-arg prefix-arg)
301 ;; Have to bind `last-command-char' here so that
302 ;; `digit-argument', for instance, can compute the
304 (last-command-char (aref keyseq 0)))
305 (call-interactively cmd)))
307 ;; This is the final call to `universal-argument-other-key', which
308 ;; set's the final `prefix-arg.
309 (let ((current-prefix-arg prefix-arg))
310 (call-interactively cmd))
312 ;; Read the command to execute with the given prefix arg.
313 (setq prefix prefix-arg
314 keyseq (read-key-sequence nil t)
315 cmd (key-binding keyseq)))
317 (let ((coding-system-for-read coding-system)
318 (coding-system-for-write coding-system)
319 (coding-system-require-warning t)
320 (current-prefix-arg prefix))
322 (call-interactively cmd))))
324 (defun set-default-coding-systems (coding-system)
325 "Set default value of various coding systems to CODING-SYSTEM.
326 This sets the following coding systems:
327 o coding system of a newly created buffer
328 o default coding system for subprocess I/O
329 This also sets the following values:
330 o default value used as `file-name-coding-system' for converting file names
331 if CODING-SYSTEM is ASCII-compatible.
332 o default value for the command `set-terminal-coding-system' (not on MSDOS)
333 o default value for the command `set-keyboard-coding-system'
334 if CODING-SYSTEM is ASCII-compatible.."
335 (check-coding-system coding-system)
336 (setq-default buffer-file-coding-system coding-system)
337 (if (fboundp 'ucs-set-table-for-input)
338 (dolist (buffer (buffer-list))
339 (or (local-variable-p 'buffer-file-coding-system buffer)
340 (ucs-set-table-for-input buffer))))
342 (if (and default-enable-multibyte-characters (not (eq system-type 'darwin)))
343 ;; The file-name coding system on Darwin systems is always utf-8.
344 (setq default-file-name-coding-system coding-system))
345 ;; If coding-system is nil, honor that on MS-DOS as well, so
346 ;; that they could reset the terminal coding system.
347 (unless (and (eq window-system 'pc) coding-system)
348 (setq default-terminal-coding-system coding-system))
349 (setq default-keyboard-coding-system coding-system)
350 ;; Preserve eol-type from existing default-process-coding-systems.
351 ;; On non-unix-like systems in particular, these may have been set
352 ;; carefully by the user, or by the startup code, to deal with the
353 ;; users shell appropriately, so should not be altered by changing
354 ;; language environment.
356 (coding-system-change-text-conversion
357 (car default-process-coding-system) coding-system))
359 (coding-system-change-text-conversion
360 (cdr default-process-coding-system) coding-system)))
361 (setq default-process-coding-system
362 (cons output-coding input-coding))))
364 (defun prefer-coding-system (coding-system)
365 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
366 This also sets the following coding systems:
367 o coding system of a newly created buffer
368 o default coding system for subprocess I/O
369 This also sets the following values:
370 o default value used as `file-name-coding-system' for converting file names.
371 o default value for the command `set-terminal-coding-system' (not on MSDOS)
372 o default value for the command `set-keyboard-coding-system'
374 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
375 systems set by this function will use that type of EOL conversion.
377 This command does not change the default value of terminal coding system
378 for MS-DOS terminal, because DOS terminals only support a single coding
379 system, and Emacs automatically sets the default to that coding system at
382 A coding system that requires automatic detection of text
383 +encoding (e.g. undecided, unix) can't be preferred.."
384 (interactive "zPrefer coding system: ")
385 (if (not (and coding-system (coding-system-p coding-system)))
386 (error "Invalid coding system `%s'" coding-system))
387 (if (memq (coding-system-type coding-system) '(raw-text undecided))
388 (error "Can't prefer the coding system `%s'" coding-system))
389 (let ((base (coding-system-base coding-system))
390 (eol-type (coding-system-eol-type coding-system)))
391 (set-coding-system-priority base)
393 (or (eq base coding-system)
394 (message "Highest priority is set to %s (base of %s)"
395 base coding-system)))
396 ;; If they asked for specific EOL conversion, honor that.
397 (if (memq eol-type '(0 1 2))
399 (coding-system-change-eol-conversion base eol-type)))
400 (set-default-coding-systems base)))
402 (defvar sort-coding-systems-predicate nil
403 "If non-nil, a predicate function to sort coding systems.
405 It is called with two coding systems, and should return t if the first
406 one is \"less\" than the second.
408 The function `sort-coding-systems' use it.")
410 (defun sort-coding-systems (codings)
411 "Sort coding system list CODINGS by a priority of each coding system.
412 Returns the sorted list. CODINGS is modified by side effects.
414 If a coding system is most preferred, it has the highest priority.
415 Otherwise, a coding system corresponds to some MIME charset has higher
416 priorities. Among them, a coding system included in `coding-system'
417 key of the current language environment has higher priorities. See
418 also the documentation of `language-info-alist'.
420 If the variable `sort-coding-systems-predicate' (which see) is
421 non-nil, it is used to sort CODINGS in the different way than above."
422 (if sort-coding-systems-predicate
423 (sort codings sort-coding-systems-predicate)
424 (let* ((from-priority (coding-system-priority-list))
425 (most-preferred (car from-priority))
426 (lang-preferred (get-language-info current-language-environment
430 (let ((base (coding-system-base x)))
431 ;; We calculate the priority number 0..255 by
432 ;; using the 8 bits PMMLCEII as this:
433 ;; P: 1 iff most preferred.
434 ;; MM: greater than 0 iff mime-charset.
435 ;; L: 1 iff one of the current lang. env.'s codings.
436 ;; C: 1 iff one of codings listed in the category list.
437 ;; E: 1 iff not XXX-with-esc
438 ;; II: if iso-2022 based, 0..3, else 1.
440 (lsh (if (eq base most-preferred) 1 0) 7)
442 (let ((mime (coding-system-get base :mime-charset)))
443 ;; Prefer coding systems corresponding to a
446 ;; Lower utf-16 priority so that we
447 ;; normally prefer utf-8 to it, and put
448 ;; x-ctext below that.
449 (cond ((string-match "utf-16"
452 ((string-match "^x-" (symbol-name mime))
457 (lsh (if (memq base lang-preferred) 1 0) 4)
458 (lsh (if (memq base from-priority) 1 0) 3)
459 (lsh (if (string-match "-with-esc\\'"
462 (if (eq (coding-system-type base) 'iso-2022)
463 (let ((category (coding-system-category base)))
464 ;; For ISO based coding systems, prefer
465 ;; one that doesn't use designation nor
466 ;; locking/single shifting.
468 ((or (eq category 'coding-category-iso-8-1)
469 (eq category 'coding-category-iso-8-2))
471 ((or (eq category 'coding-category-iso-7-tight)
472 (eq category 'coding-category-iso-7))
478 (sort codings (function (lambda (x y)
479 (> (funcall func x) (funcall func y))))))))
481 (defun find-coding-systems-region (from to)
482 "Return a list of proper coding systems to encode a text between FROM and TO.
484 If FROM is a string, find coding systems in that instead of the buffer.
485 All coding systems in the list can safely encode any multibyte characters
488 If the text contains no multibyte characters, return a list of a single
489 element `undecided'."
490 (let ((codings (find-coding-systems-region-internal from to)))
492 ;; The text contains only ASCII characters. Any coding
495 ;; We need copy-sequence because sorting will alter the argument.
496 (sort-coding-systems (copy-sequence codings)))))
498 (defun find-coding-systems-string (string)
499 "Return a list of proper coding systems to encode STRING.
500 All coding systems in the list can safely encode any multibyte characters
503 If STRING contains no multibyte characters, return a list of a single
504 element `undecided'."
505 (find-coding-systems-region string nil))
507 (defun find-coding-systems-for-charsets (charsets)
508 "Return a list of proper coding systems to encode characters of CHARSETS.
509 CHARSETS is a list of character sets.
511 This only finds coding systems of type `charset', whose
512 `:charset-list' property includes all of CHARSETS (plus `ascii' for
513 ascii-compatible coding systems). It was used in older versions of
514 Emacs, but is unlikely to be what you really want now."
515 ;; Deal with aliases.
516 (setq charsets (mapcar (lambda (c)
517 (get-charset-property c :name))
519 (cond ((or (null charsets)
520 (and (= (length charsets) 1)
521 (eq 'ascii (car charsets))))
523 ((or (memq 'eight-bit-control charsets)
524 (memq 'eight-bit-graphic charsets))
525 '(raw-text utf-8-emacs))
528 (dolist (cs (coding-system-list t))
529 (let ((cs-charsets (and (eq (coding-system-type cs) 'charset)
530 (coding-system-charset-list cs)))
532 (if (coding-system-get cs :ascii-compatible-p)
533 (add-to-list 'cs-charsets 'ascii))
537 (unless (memq (pop charsets) cs-charsets)
541 (nreverse codings)))))
543 (defun find-multibyte-characters (from to &optional maxcount excludes)
544 "Find multibyte characters in the region specified by FROM and TO.
545 If FROM is a string, find multibyte characters in the string.
546 The return value is an alist of the following format:
547 ((CHARSET COUNT CHAR ...) ...)
549 CHARSET is a character set,
550 COUNT is a number of characters,
551 CHARs are the characters found from the character set.
552 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
553 Optional 4th arg EXCLUDE is a list of character sets to be ignored."
557 (if (multibyte-string-p from)
559 (while (setq idx (string-match "[^\000-\177]" from idx))
560 (setq char (aref from idx)
561 charset (char-charset char))
562 (unless (memq charset excludes)
563 (let ((slot (assq charset chars)))
565 (if (not (memq char (nthcdr 2 slot)))
566 (let ((count (nth 1 slot)))
567 (setcar (cdr slot) (1+ count))
568 (if (or (not maxcount) (< count maxcount))
569 (nconc slot (list char)))))
570 (setq chars (cons (list charset 1 char) chars)))))
571 (setq idx (1+ idx)))))
572 (if enable-multibyte-characters
575 (while (re-search-forward "[^\000-\177]" to t)
576 (setq char (preceding-char)
577 charset (char-charset char))
578 (unless (memq charset excludes)
579 (let ((slot (assq charset chars)))
581 (if (not (member char (nthcdr 2 slot)))
582 (let ((count (nth 1 slot)))
583 (setcar (cdr slot) (1+ count))
584 (if (or (not maxcount) (< count maxcount))
585 (nconc slot (list char)))))
586 (setq chars (cons (list charset 1 char) chars)))))))))
589 (defun search-unencodable-char (coding-system)
590 "Search forward from point for a character that is not encodable.
591 It asks which coding system to check.
592 If such a character is found, set point after that character.
593 Otherwise, don't move point.
595 When called from a program, the value is a position of the found character,
596 or nil if all characters are encodable."
598 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
600 (format "Coding-system (default %s): " default)
602 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
605 (message "All following characters are encodable by %s" coding-system))
608 (defvar last-coding-system-specified nil
609 "Most recent coding system explicitly specified by the user when asked.
610 This variable is set whenever Emacs asks the user which coding system
611 to use in order to write a file. If you set it to nil explicitly,
612 then call `write-region', then afterward this variable will be non-nil
613 only if the user was explicitly asked and specified a coding system.")
615 (defvar select-safe-coding-system-accept-default-p nil
616 "If non-nil, a function to control the behavior of coding system selection.
617 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
618 function `select-safe-coding-system' (which see). This variable
619 overrides that argument.")
621 (defun select-safe-coding-system-interactively (from to codings unsafe
622 &optional rejected default)
623 "Select interactively a coding system for the region FROM ... TO.
624 FROM can be a string, as in `write-region'.
625 CODINGS is the list of base coding systems known to be safe for this region,
626 typically obtained with `find-coding-systems-region'.
627 UNSAFE is a list of coding systems known to be unsafe for this region.
628 REJECTED is a list of coding systems which were safe but for some reason
629 were not recommended in the particular context.
630 DEFAULT is the coding system to use by default in the query."
631 ;; At first, if some defaults are unsafe, record at most 11
632 ;; problematic characters and their positions for them by turning
635 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
638 (mapcar #'(lambda (coding)
641 (mapcar #'(lambda (pos)
642 (cons pos (aref from pos)))
643 (unencodable-char-position
644 0 (length from) coding
646 (mapcar #'(lambda (pos)
647 (cons pos (char-after pos)))
648 (unencodable-char-position
649 from to coding 11)))))
652 ;; Change each safe coding system to the corresponding
653 ;; mime-charset name if it is also a coding system. Such a name
654 ;; is more friendly to users.
658 (setq mime-charset (coding-system-get (car l) :mime-charset))
659 (if (and mime-charset (coding-system-p mime-charset)
660 (coding-system-equal (car l) mime-charset))
661 (setcar l mime-charset))
664 ;; Don't offer variations with locking shift, which you
665 ;; basically never want.
667 (dolist (elt codings (setq codings (nreverse l)))
668 (unless (or (eq 'coding-category-iso-7-else
669 (coding-system-category elt))
670 (eq 'coding-category-iso-8-else
671 (coding-system-category elt)))
674 ;; Remove raw-text, emacs-mule and no-conversion unless nothing
675 ;; else is available.
679 (delq 'no-conversion codings)))
680 '(raw-text emacs-mule no-conversion)))
682 (let ((window-configuration (current-window-configuration))
683 (bufname (buffer-name))
686 ;; If some defaults are unsafe, make sure the offending
687 ;; buffer is displayed.
688 (when (and unsafe (not (stringp from)))
689 (pop-to-buffer bufname)
690 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
692 ;; Then ask users to select one from CODINGS while showing
693 ;; the reason why none of the defaults are not used.
694 (with-output-to-temp-buffer "*Warning*"
695 (with-current-buffer standard-output
696 (if (and (null rejected) (null unsafe))
697 (insert "No default coding systems to try for "
699 (format "string \"%s\"." from)
700 (format "buffer `%s'." bufname)))
702 "These default coding systems were tried to encode"
704 (concat " \"" (if (> (length from) 10)
705 (concat (substring from 0 10) "...\"")
707 (format " text\nin the buffer `%s'" bufname))
711 (dolist (x (append rejected unsafe))
712 (princ " ") (princ (car x)))
714 (fill-region-as-paragraph pos (point)))
716 (insert "These safely encodes the target text,
717 but it is not recommended for encoding text in this context,
718 e.g., for sending an email message.\n ")
720 (princ " ") (princ x))
723 (insert (if rejected "And the others"
724 "However, each of them")
725 " encountered these problematic characters:\n")
726 (dolist (coding unsafe)
727 (insert (format " %s:" (car coding)))
730 #'(lambda (bufname pos)
731 (when (buffer-live-p (get-buffer bufname))
732 (pop-to-buffer bufname)
735 #'(lambda (bufname pos coding)
736 (when (buffer-live-p (get-buffer bufname))
737 (pop-to-buffer bufname)
741 (search-unencodable-char coding)
742 (forward-char -1))))))
743 (dolist (elt (cdr coding))
746 (insert (if (< i 10) (cdr elt) "..."))
752 "mouse-2, RET: jump to this character"
754 'help-args (list bufname (car elt)))
759 "mouse-2, RET: next unencodable character"
761 'help-args (list bufname (car elt)
766 The first problematic character is at point in the displayed buffer,\n"
767 (substitute-command-keys "\
768 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
770 one of the following safe coding systems, or edit the buffer:\n")
774 (princ " ") (princ x))
776 (fill-region-as-paragraph pos (point)))
777 (insert "Or specify any other coding system
778 at the risk of losing the problematic characters.\n")))
780 ;; Read a coding system.
783 (format "Select coding system (default %s): " default)
785 (setq last-coding-system-specified coding-system))
787 (kill-buffer "*Warning*")
788 (set-window-configuration window-configuration)
791 (defun select-safe-coding-system (from to &optional default-coding-system
792 accept-default-p file)
793 "Ask a user to select a safe coding system from candidates.
794 The candidates of coding systems which can safely encode a text
795 between FROM and TO are shown in a popup window. Among them, the most
796 proper one is suggested as the default.
798 The list of `buffer-file-coding-system' of the current buffer,
799 the `default-buffer-file-coding-system', and the
800 most preferred coding system (if it corresponds to a MIME charset) is
801 treated as the default coding system list. Among them, the first one
802 that safely encodes the text is normally selected silently and
803 returned without any user interaction. See also the command
804 `prefer-coding-system'.
806 However, the user is queried if the chosen coding system is
807 inconsistent with what would be selected by `find-auto-coding' from
808 coding cookies &c. if the contents of the region were read from a
809 file. (That could lead to data corruption in a file subsequently
810 re-visited and edited.)
812 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
813 list of coding systems to be prepended to the default coding system
814 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
815 element is t, the cdr part is used as the defualt coding system list,
816 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
817 and the most preferred coding system are not used.
819 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
820 determine the acceptability of the silently selected coding system.
821 It is called with that coding system, and should return nil if it
822 should not be silently selected and thus user interaction is required.
824 Optional 5th arg FILE is the file name to use for this purpose.
825 That is different from `buffer-file-name' when handling `write-region'
828 The variable `select-safe-coding-system-accept-default-p', if
829 non-nil, overrides ACCEPT-DEFAULT-P.
831 Kludgy feature: if FROM is a string, the string is the target text,
833 (if (and default-coding-system
834 (not (listp default-coding-system)))
835 (setq default-coding-system (list default-coding-system)))
837 (let ((no-other-defaults nil)
839 (unless (or (stringp from) find-file-literally)
840 ;; Find an auto-coding that is specified for the the current
841 ;; buffer and file from the region FROM and TO.
846 (setq auto-cs (find-auto-coding (or file buffer-file-name "")
849 (if (coding-system-p (car auto-cs))
850 (setq auto-cs (car auto-cs))
854 Invalid coding system `%s' is specified
855 for the current buffer/file by the %s.
856 It is highly recommended to fix it before writing to a file."
858 (if (eq (cdr auto-cs) :coding) ":coding tag"
859 (format "variable `%s'" (cdr auto-cs)))))
860 (or (yes-or-no-p "Really proceed with writing? ")
861 (error "Save aborted"))
862 (setq auto-cs nil))))))
864 (if (eq (car default-coding-system) t)
865 (setq no-other-defaults t
866 default-coding-system (cdr default-coding-system)))
868 ;; Change elements of the list to (coding . base-coding).
869 (setq default-coding-system
870 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
871 default-coding-system))
873 (if (and auto-cs (not no-other-defaults))
874 ;; If the file has a coding cookie, try to use it before anything
875 ;; else (i.e. before default-coding-system which will typically come
876 ;; from file-coding-system-alist).
877 (let ((base (coding-system-base auto-cs)))
878 (or (memq base '(nil undecided))
879 (rassq base default-coding-system)
880 (push (cons auto-cs base) default-coding-system))))
882 ;; From now on, the list of defaults is reversed.
883 (setq default-coding-system (nreverse default-coding-system))
885 (unless no-other-defaults
886 ;; If buffer-file-coding-system is not nil nor undecided, append it
888 (if buffer-file-coding-system
889 (let ((base (coding-system-base buffer-file-coding-system)))
890 (or (eq base 'undecided)
891 (rassq base default-coding-system)
892 (push (cons buffer-file-coding-system base)
893 default-coding-system))))
895 ;; If default-buffer-file-coding-system is not nil nor undecided,
896 ;; append it to the defaults.
897 (if default-buffer-file-coding-system
898 (let ((base (coding-system-base default-buffer-file-coding-system)))
899 (or (eq base 'undecided)
900 (rassq base default-coding-system)
901 (push (cons default-buffer-file-coding-system base)
902 default-coding-system))))
904 ;; If the most preferred coding system has the property mime-charset,
905 ;; append it to the defaults.
906 (let ((preferred (coding-system-priority-list t))
908 (and (coding-system-p preferred)
909 (setq base (coding-system-base preferred))
910 (coding-system-get preferred :mime-charset)
911 (not (rassq base default-coding-system))
912 (push (cons preferred base)
913 default-coding-system))))
915 (if select-safe-coding-system-accept-default-p
916 (setq accept-default-p select-safe-coding-system-accept-default-p))
918 (let ((codings (find-coding-systems-region from to))
920 safe rejected unsafe)
921 ;; Classify the defaults into safe, rejected, and unsafe.
922 (dolist (elt default-coding-system)
923 (if (or (eq (car codings) 'undecided)
924 (memq (cdr elt) codings))
925 (if (and (functionp accept-default-p)
926 (not (funcall accept-default-p (cdr elt))))
927 (push (car elt) rejected)
928 (push (car elt) safe))
929 (push (car elt) unsafe)))
931 (setq coding-system (car safe)))
933 ;; If all the defaults failed, ask a user.
934 (unless coding-system
935 (setq coding-system (select-safe-coding-system-interactively
936 from to codings unsafe rejected (car codings))))
938 (if (and coding-system (vectorp (coding-system-eol-type coding-system)))
939 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
942 (coding-system-change-eol-conversion coding-system eol)))))
944 ;; Check we're not inconsistent with what `coding:' spec &c would
945 ;; give when file is re-read.
946 ;; But don't do this if we explicitly ignored the cookie
947 ;; by using `find-file-literally'.
951 (memq (coding-system-type coding-system) '(0 5)))))
952 ;; Merge coding-system and auto-cs as far as possible.
953 (if (not coding-system)
954 (setq coding-system auto-cs)
956 (setq auto-cs coding-system)
957 (let ((eol-type-1 (coding-system-eol-type coding-system))
958 (eol-type-2 (coding-system-eol-type auto-cs)))
959 (if (eq (coding-system-base coding-system) 'undecided)
960 (setq coding-system (coding-system-change-text-conversion
961 coding-system auto-cs))
962 (if (eq (coding-system-base auto-cs) 'undecided)
963 (setq auto-cs (coding-system-change-text-conversion
964 auto-cs coding-system))))
965 (if (vectorp eol-type-1)
966 (or (vectorp eol-type-2)
967 (setq coding-system (coding-system-change-eol-conversion
968 coding-system eol-type-2)))
969 (if (vectorp eol-type-2)
970 (setq auto-cs (coding-system-change-eol-conversion
971 auto-cs eol-type-1)))))))
974 ;; Don't barf if writing a compressed file, say.
975 ;; This check perhaps isn't ideal, but is probably
976 ;; the best thing to do.
977 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
978 (not (coding-system-equal coding-system auto-cs)))
980 (format "Selected encoding %s disagrees with \
981 %s specified by file contents. Really save (else edit coding cookies \
982 and try again)? " coding-system auto-cs))
983 (error "Save aborted"))))
986 (setq select-safe-coding-system-function 'select-safe-coding-system)
988 (defun select-message-coding-system ()
989 "Return a coding system to encode the outgoing message of the current buffer.
990 It at first tries the first coding system found in these variables
992 (1) local value of `buffer-file-coding-system'
993 (2) value of `sendmail-coding-system'
994 (3) value of `default-sendmail-coding-system'
995 (4) value of `default-buffer-file-coding-system'
996 If the found coding system can't encode the current buffer,
997 or none of them are bound to a coding system,
998 it asks the user to select a proper coding system."
999 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
1000 buffer-file-coding-system)
1001 sendmail-coding-system
1002 default-sendmail-coding-system
1003 default-buffer-file-coding-system)))
1004 (if (eq coding 'no-conversion)
1005 ;; We should never use no-conversion for outgoing mail.
1007 (if (fboundp select-safe-coding-system-function)
1008 (funcall select-safe-coding-system-function
1009 (point-min) (point-max) coding
1010 (function (lambda (x) (coding-system-get x :mime-charset))))
1013 ;;; Language support stuff.
1015 (defvar language-info-alist nil
1016 "Alist of language environment definitions.
1017 Each element looks like:
1018 (LANGUAGE-NAME . ((KEY . INFO) ...))
1019 where LANGUAGE-NAME is a string, the name of the language environment,
1020 KEY is a symbol denoting the kind of information, and
1021 INFO is the data associated with KEY.
1022 Meaningful values for KEY include
1024 documentation value is documentation of what this language environment
1025 is meant for, and how to use it.
1026 charset value is a list of the character sets mainly used
1027 by this language environment.
1028 sample-text value is an expression which is evalled to generate
1029 a line of text written using characters appropriate
1030 for this language environment.
1031 setup-function value is a function to call to switch to this
1032 language environment.
1033 exit-function value is a function to call to leave this
1034 language environment.
1035 coding-system value is a list of coding systems that are good
1036 for saving text written in this language environment.
1037 This list serves as suggestions to the user;
1038 in effect, as a kind of documentation.
1039 coding-priority value is a list of coding systems for this language
1040 environment, in order of decreasing priority.
1041 This is used to set up the coding system priority
1042 list when you switch to this language environment.
1043 nonascii-translation
1044 value is a charset of dimension one to use for
1045 converting a unibyte character to multibyte
1047 input-method value is a default input method for this language
1049 features value is a list of features requested in this
1050 language environment.
1051 ctext-non-standard-encodings
1052 value is a list of non-standard encoding
1053 names used in extended segments of CTEXT.
1055 `ctext-non-standard-encodings' for more
1058 The following keys take effect only when multibyte characters are
1059 globally disabled, i.e. the value of `default-enable-multibyte-characters'
1062 unibyte-display value is a coding system to encode characters
1063 for the terminal. Characters in the range
1064 of 160 to 255 display not as octal escapes,
1065 but as non-ASCII characters in this language
1068 (defun get-language-info (lang-env key)
1069 "Return information listed under KEY for language environment LANG-ENV.
1070 KEY is a symbol denoting the kind of information.
1071 For a list of useful values for KEY and their meanings,
1072 see `language-info-alist'."
1073 (if (symbolp lang-env)
1074 (setq lang-env (symbol-name lang-env)))
1075 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1077 (cdr (assq key (cdr lang-slot))))))
1079 (defun set-language-info (lang-env key info)
1080 "Modify part of the definition of language environment LANG-ENV.
1081 Specifically, this stores the information INFO under KEY
1082 in the definition of this language environment.
1083 KEY is a symbol denoting the kind of information.
1084 INFO is the value for that information.
1086 For a list of useful values for KEY and their meanings,
1087 see `language-info-alist'."
1088 (if (symbolp lang-env)
1089 (setq lang-env (symbol-name lang-env)))
1090 (set-language-info-internal lang-env key info)
1091 (if (equal lang-env current-language-environment)
1092 (set-language-environment lang-env)))
1094 (defun set-language-info-internal (lang-env key info)
1096 Arguments are the same as `set-language-info'."
1097 (let (lang-slot key-slot)
1098 (setq lang-slot (assoc lang-env language-info-alist))
1099 (if (null lang-slot) ; If no slot for the language, add it.
1100 (setq lang-slot (list lang-env)
1101 language-info-alist (cons lang-slot language-info-alist)))
1102 (setq key-slot (assq key lang-slot))
1103 (if (null key-slot) ; If no slot for the key, add it.
1105 (setq key-slot (list key))
1106 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1107 (setcdr key-slot (purecopy info))
1108 ;; Update the custom-type of `current-language-environment'.
1109 (put 'current-language-environment 'custom-type
1110 (cons 'choice (mapcar
1113 (sort (mapcar 'car language-info-alist) 'string<))))))
1115 (defun set-language-info-alist (lang-env alist &optional parents)
1116 "Store ALIST as the definition of language environment LANG-ENV.
1117 ALIST is an alist of KEY and INFO values. See the documentation of
1118 `language-info-alist' for the meanings of KEY and INFO.
1120 Optional arg PARENTS is a list of parent menu names; it specifies
1121 where to put this language environment in the
1122 Describe Language Environment and Set Language Environment menus.
1123 For example, (\"European\") means to put this language environment
1124 in the European submenu in each of those two menus."
1125 (if (symbolp lang-env)
1126 (setq lang-env (symbol-name lang-env)))
1127 (let ((describe-map describe-language-environment-map)
1128 (setup-map setup-language-environment-map))
1131 map parent-symbol parent prompt)
1133 (if (symbolp (setq parent-symbol (car l)))
1134 (setq parent (symbol-name parent))
1135 (setq parent parent-symbol parent-symbol (intern parent)))
1136 (setq map (lookup-key describe-map (vector parent-symbol)))
1137 ;; This prompt string is for define-prefix-command, so
1138 ;; that the map it creates will be suitable for a menu.
1139 (or map (setq prompt (format "%s Environment" parent)))
1142 (setq map (intern (format "describe-%s-environment-map"
1143 (downcase parent))))
1144 (define-prefix-command map nil prompt)
1145 (define-key-after describe-map (vector parent-symbol)
1146 (cons parent map) t)))
1147 (setq describe-map (symbol-value map))
1148 (setq map (lookup-key setup-map (vector parent-symbol)))
1151 (setq map (intern (format "setup-%s-environment-map"
1152 (downcase parent))))
1153 (define-prefix-command map nil prompt)
1154 (define-key-after setup-map (vector parent-symbol)
1155 (cons parent map) t)))
1156 (setq setup-map (symbol-value map))
1159 ;; Set up menu items for this language env.
1160 (let ((doc (assq 'documentation alist)))
1162 (define-key-after describe-map (vector (intern lang-env))
1163 (cons lang-env 'describe-specified-language-support) t)))
1164 (define-key-after setup-map (vector (intern lang-env))
1165 (cons lang-env 'setup-specified-language-environment) t)
1168 (set-language-info-internal lang-env (car elt) (cdr elt)))
1170 (if (equal lang-env current-language-environment)
1171 (set-language-environment lang-env))))
1173 (defun read-language-name (key prompt &optional default)
1174 "Read a language environment name which has information for KEY.
1175 If KEY is nil, read any language environment.
1176 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1177 This returns a language environment name as a string."
1178 (let* ((completion-ignore-case t)
1179 (name (completing-read prompt
1182 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1183 t nil nil default)))
1184 (if (and (> (length name) 0)
1186 (get-language-info name key)))
1189 ;;; Multilingual input methods.
1191 "LEIM: Libraries of Emacs Input Methods."
1194 (defconst leim-list-file-name "leim-list.el"
1195 "Name of LEIM list file.
1196 This file contains a list of libraries of Emacs input methods (LEIM)
1197 in the format of Lisp expression for registering each input method.
1198 Emacs loads this file at startup time.")
1200 (defvar leim-list-header (format
1201 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1203 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1204 ;; methods in the same directory as this file. Loading this file
1205 ;; registers all the input methods in Emacs.
1207 ;; Each entry has the form:
1208 ;; (register-input-method
1209 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1210 ;; TITLE DESCRIPTION
1212 ;; See the function `register-input-method' for the meanings of the arguments.
1214 ;; If this directory is included in load-path, Emacs automatically
1215 ;; loads this file at startup time.
1218 leim-list-file-name)
1219 "Header to be inserted in LEIM list file.")
1221 (defvar leim-list-entry-regexp "^(register-input-method"
1222 "Regexp matching head of each entry in LEIM list file.
1223 See also the variable `leim-list-header'")
1225 (defvar update-leim-list-functions
1226 '(quail-update-leim-list-file)
1227 "List of functions to call to update LEIM list file.
1228 Each function is called with one arg, LEIM directory name.")
1230 (defun update-leim-list-file (&rest dirs)
1231 "Update LEIM list file in directories DIRS."
1232 (let ((functions update-leim-list-functions))
1234 (apply (car functions) dirs)
1235 (setq functions (cdr functions)))))
1237 (defvar current-input-method nil
1238 "The current input method for multilingual text.
1239 If nil, that means no input method is activated now.")
1240 (make-variable-buffer-local 'current-input-method)
1241 (put 'current-input-method 'permanent-local t)
1243 (defvar current-input-method-title nil
1244 "Title string of the current input method shown in mode line.")
1245 (make-variable-buffer-local 'current-input-method-title)
1246 (put 'current-input-method-title 'permanent-local t)
1248 (defcustom default-input-method nil
1249 "*Default input method for multilingual text (a string).
1250 This is the input method activated automatically by the command
1251 `toggle-input-method' (\\[toggle-input-method])."
1252 :link '(custom-manual "(emacs)Input Methods")
1254 :type '(choice (const nil) (string
1255 :completion-ignore-case t
1256 :complete-function widget-string-complete
1257 :completion-alist input-method-alist
1258 :prompt-history input-method-history))
1259 :set-after '(current-language-environment))
1261 (put 'input-method-function 'permanent-local t)
1263 (defvar input-method-history nil
1264 "History list for some commands that read input methods.")
1265 (make-variable-buffer-local 'input-method-history)
1266 (put 'input-method-history 'permanent-local t)
1268 (defvar inactivate-current-input-method-function nil
1269 "Function to call for inactivating the current input method.
1270 Every input method should set this to an appropriate value when activated.
1271 This function is called with no argument.
1273 This function should never change the value of `current-input-method'.
1274 It is set to nil by the function `inactivate-input-method'.")
1275 (make-variable-buffer-local 'inactivate-current-input-method-function)
1276 (put 'inactivate-current-input-method-function 'permanent-local t)
1278 (defvar describe-current-input-method-function nil
1279 "Function to call for describing the current input method.
1280 This function is called with no argument.")
1281 (make-variable-buffer-local 'describe-current-input-method-function)
1282 (put 'describe-current-input-method-function 'permanent-local t)
1284 (defvar input-method-alist nil
1285 "Alist of input method names vs how to use them.
1286 Each element has the form:
1287 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1288 See the function `register-input-method' for the meanings of the elements.")
1290 (defun register-input-method (input-method lang-env &rest args)
1291 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1292 INPUT-METHOD and LANG-ENV are symbols or strings.
1294 The remaining arguments are:
1295 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
1296 ACTIVATE-FUNC is a function to call to activate this method.
1297 TITLE is a string to show in the mode line when this method is active.
1298 DESCRIPTION is a string describing this method and what it is good for.
1299 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1300 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1302 This function is mainly used in the file \"leim-list.el\" which is
1303 created at Emacs build time, registering all Quail input methods
1304 contained in the Emacs distribution.
1306 In case you want to register a new Quail input method by yourself, be
1307 careful to use the same input method title as given in the third
1308 parameter of `quail-define-package'. (If the values are different, the
1309 string specified in this function takes precedence.)
1311 The commands `describe-input-method' and `list-input-methods' need
1312 these duplicated values to show some information about input methods
1313 without loading the relevant Quail packages."
1314 (if (symbolp lang-env)
1315 (setq lang-env (symbol-name lang-env)))
1316 (if (symbolp input-method)
1317 (setq input-method (symbol-name input-method)))
1318 (let ((info (cons lang-env args))
1319 (slot (assoc input-method input-method-alist)))
1322 (setq slot (cons input-method info))
1323 (setq input-method-alist (cons slot input-method-alist)))))
1325 (defun read-input-method-name (prompt &optional default inhibit-null)
1326 "Read a name of input method from a minibuffer prompting with PROMPT.
1327 If DEFAULT is non-nil, use that as the default,
1328 and substitute it into PROMPT at the first `%s'.
1329 If INHIBIT-NULL is non-nil, null input signals an error.
1331 The return value is a string."
1333 (setq prompt (format prompt default)))
1334 (let* ((completion-ignore-case t)
1335 ;; As it is quite normal to change input method in the
1336 ;; minibuffer, we must enable it even if
1337 ;; enable-recursive-minibuffers is currently nil.
1338 (enable-recursive-minibuffers t)
1339 ;; This binding is necessary because input-method-history is
1341 (input-method (completing-read prompt input-method-alist
1342 nil t nil 'input-method-history
1344 (if (and input-method (symbolp input-method))
1345 (setq input-method (symbol-name input-method)))
1346 (if (> (length input-method) 0)
1349 (error "No valid input method is specified")))))
1351 (defun activate-input-method (input-method)
1352 "Switch to input method INPUT-METHOD for the current buffer.
1353 If some other input method is already active, turn it off first.
1354 If INPUT-METHOD is nil, deactivate any current input method."
1355 (if (and input-method (symbolp input-method))
1356 (setq input-method (symbol-name input-method)))
1357 (if (and current-input-method
1358 (not (string= current-input-method input-method)))
1359 (inactivate-input-method))
1360 (unless (or current-input-method (null input-method))
1361 (let ((slot (assoc input-method input-method-alist)))
1363 (error "Can't activate input method `%s'" input-method))
1364 (setq current-input-method-title nil)
1365 (let ((func (nth 2 slot)))
1366 (if (functionp func)
1367 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1368 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1370 (require (cdr func))
1371 (apply (car func) input-method (nthcdr 5 slot)))
1372 (error "Can't activate input method `%s'" input-method))))
1373 (setq current-input-method input-method)
1374 (or (stringp current-input-method-title)
1375 (setq current-input-method-title (nth 3 slot)))
1377 (run-hooks 'input-method-activate-hook)
1378 (force-mode-line-update)))))
1380 (defun inactivate-input-method ()
1381 "Turn off the current input method."
1382 (when current-input-method
1383 (if input-method-history
1384 (unless (string= current-input-method (car input-method-history))
1385 (setq input-method-history
1386 (cons current-input-method
1387 (delete current-input-method input-method-history))))
1388 (setq input-method-history (list current-input-method)))
1390 (funcall inactivate-current-input-method-function)
1392 (run-hooks 'input-method-inactivate-hook)
1393 (setq current-input-method nil
1394 input-method-function nil
1395 current-input-method-title nil)
1396 (force-mode-line-update)))))
1398 (defun set-input-method (input-method &optional interactive)
1399 "Select and activate input method INPUT-METHOD for the current buffer.
1400 This also sets the default input method to the one you specify.
1401 If INPUT-METHOD is nil, this function turns off the input method, and
1402 also causes you to be prompted for a name of an input method the next
1403 time you invoke \\[toggle-input-method].
1404 When called interactively, the optional arg INTERACTIVE is non-nil,
1405 which marks the variable `default-input-method' as set for Custom buffers.
1407 To deactivate the input method interactively, use \\[toggle-input-method].
1408 To deactivate it programmatically, use \\[inactivate-input-method]."
1410 (let* ((default (or (car input-method-history) default-input-method)))
1411 (list (read-input-method-name
1412 (if default "Select input method (default %s): " "Select input method: ")
1415 (activate-input-method input-method)
1416 (setq default-input-method input-method)
1418 (customize-mark-as-set 'default-input-method))
1419 default-input-method)
1421 (defun toggle-input-method (&optional arg interactive)
1422 "Enable or disable multilingual text input method for the current buffer.
1423 Only one input method can be enabled at any time in a given buffer.
1425 The normal action is to enable an input method if none was
1426 enabled, and disable the current one otherwise. Which input method
1427 to enable can be determined in various ways--either the one most
1428 recently used, or the one specified by `default-input-method', or
1429 as a last resort by reading the name of an input method in the
1432 With a prefix argument, read an input method name with the minibuffer
1433 and enable that one. The default is the most recent input method specified
1434 \(not including the currently active input method, if any).
1436 When called interactively, the optional arg INTERACTIVE is non-nil,
1437 which marks the variable `default-input-method' as set for Custom buffers."
1439 (interactive "P\np")
1440 (if (and current-input-method (not arg))
1441 (inactivate-input-method)
1442 (let ((default (or (car input-method-history) default-input-method)))
1443 (if (and arg default (equal current-input-method default)
1444 (> (length input-method-history) 1))
1445 (setq default (nth 1 input-method-history)))
1446 (activate-input-method
1447 (if (or arg (not default))
1449 (read-input-method-name
1450 (if default "Input method (default %s): " "Input method: " )
1453 (unless default-input-method
1455 (setq default-input-method current-input-method)
1457 (customize-mark-as-set 'default-input-method)))))))
1459 (eval-when-compile (autoload 'help-buffer "help-mode"))
1461 (defun describe-input-method (input-method)
1462 "Describe input method INPUT-METHOD."
1464 (list (read-input-method-name
1465 "Describe input method (default current choice): ")))
1466 (if (and input-method (symbolp input-method))
1467 (setq input-method (symbol-name input-method)))
1468 (help-setup-xref (list #'describe-input-method
1469 (or input-method current-input-method))
1472 (if (null input-method)
1473 (describe-current-input-method)
1474 (let ((current current-input-method))
1478 (activate-input-method input-method)
1479 (describe-current-input-method))
1480 (activate-input-method current))
1482 (activate-input-method current)
1483 (help-setup-xref (list #'describe-input-method input-method)
1485 (with-output-to-temp-buffer (help-buffer)
1486 (let ((elt (assoc input-method input-method-alist)))
1488 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1489 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1491 (defun describe-current-input-method ()
1492 "Describe the input method currently in use.
1493 This is a subroutine for `describe-input-method'."
1494 (if current-input-method
1495 (if (and (symbolp describe-current-input-method-function)
1496 (fboundp describe-current-input-method-function))
1497 (funcall describe-current-input-method-function)
1498 (message "No way to describe the current input method `%s'"
1499 current-input-method)
1501 (error "No input method is activated now")))
1503 (defun read-multilingual-string (prompt &optional initial-input input-method)
1504 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1505 The input method selected last time is activated in minibuffer.
1506 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1508 Optional 3rd argument INPUT-METHOD specifies the input method
1509 to be activated instead of the one selected last time. It is a symbol
1513 current-input-method
1514 default-input-method
1515 (read-input-method-name "Input method: " nil t)))
1516 (if (and input-method (symbolp input-method))
1517 (setq input-method (symbol-name input-method)))
1518 (let ((prev-input-method current-input-method))
1521 (activate-input-method input-method)
1522 (read-string prompt initial-input nil nil t))
1523 (activate-input-method prev-input-method))))
1525 ;; Variables to control behavior of input methods. All input methods
1526 ;; should react to these variables.
1528 (defcustom input-method-verbose-flag 'default
1529 "*A flag to control extra guidance given by input methods.
1530 The value should be nil, t, `complex-only', or `default'.
1532 The extra guidance is done by showing list of available keys in echo
1533 area. When you use the input method in the minibuffer, the guidance
1534 is shown at the bottom short window (split from the existing window).
1536 If the value is t, extra guidance is always given, if the value is
1537 nil, extra guidance is always suppressed.
1539 If the value is `complex-only', only complex input methods such as
1540 `chinese-py' and `japanese' give extra guidance.
1542 If the value is `default', complex input methods always give extra
1543 guidance, but simple input methods give it only when you are not in
1546 See also the variable `input-method-highlight-flag'."
1547 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1548 (const complex-only) (const default))
1551 (defcustom input-method-highlight-flag t
1552 "*If this flag is non-nil, input methods highlight partially-entered text.
1553 For instance, while you are in the middle of a Quail input method sequence,
1554 the text inserted so far is temporarily underlined.
1555 The underlining goes away when you finish or abort the input method sequence.
1556 See also the variable `input-method-verbose-flag'."
1560 (defcustom input-method-activate-hook nil
1561 "Normal hook run just after an input method is activated.
1563 The variable `current-input-method' keeps the input method name
1568 (defcustom input-method-inactivate-hook nil
1569 "Normal hook run just after an input method is inactivated.
1571 The variable `current-input-method' still keeps the input method name
1576 (defcustom input-method-after-insert-chunk-hook nil
1577 "Normal hook run just after an input method insert some chunk of text."
1581 (defvar input-method-exit-on-first-char nil
1582 "This flag controls when an input method returns.
1583 Usually, the input method does not return while there's a possibility
1584 that it may find a different translation if a user types another key.
1585 But, it this flag is non-nil, the input method returns as soon as
1586 the current key sequence gets long enough to have some valid translation.")
1588 (defcustom input-method-use-echo-area nil
1589 "This flag controls how an input method shows an intermediate key sequence.
1590 Usually, the input method inserts the intermediate key sequence,
1591 or candidate translations corresponding to the sequence,
1592 at point in the current buffer.
1593 But, if this flag is non-nil, it displays them in echo area instead."
1597 (defvar input-method-exit-on-invalid-key nil
1598 "This flag controls the behavior of an input method on invalid key input.
1599 Usually, when a user types a key which doesn't start any character
1600 handled by the input method, the key is handled by turning off the
1601 input method temporarily. After that key, the input method is re-enabled.
1602 But, if this flag is non-nil, the input method is never back on.")
1605 (defcustom set-language-environment-hook nil
1606 "Normal hook run after some language environment is set.
1608 When you set some hook function here, that effect usually should not
1609 be inherited to another language environment. So, you had better set
1610 another function in `exit-language-environment-hook' (which see) to
1615 (defcustom exit-language-environment-hook nil
1616 "Normal hook run after exiting from some language environment.
1617 When this hook is run, the variable `current-language-environment'
1618 is still bound to the language environment being exited.
1620 This hook is mainly used for canceling the effect of
1621 `set-language-environment-hook' (which-see)."
1625 (put 'setup-specified-language-environment 'apropos-inhibit t)
1627 (defun setup-specified-language-environment ()
1628 "Switch to a specified language environment."
1630 (let (language-name)
1631 (if (and (symbolp last-command-event)
1632 (or (not (eq last-command-event 'Default))
1633 (setq last-command-event 'English))
1634 (setq language-name (symbol-name last-command-event)))
1636 (set-language-environment language-name)
1637 (customize-mark-as-set 'current-language-environment))
1638 (error "Bogus calling sequence"))))
1640 (defcustom current-language-environment "English"
1641 "The last language environment specified with `set-language-environment'.
1642 This variable should be set only with \\[customize], which is equivalent
1643 to using the function `set-language-environment'."
1644 :link '(custom-manual "(emacs)Language Environments")
1645 :set (lambda (symbol value) (set-language-environment value))
1647 (or (car-safe (assoc-string
1648 (if (symbolp current-language-environment)
1649 (symbol-name current-language-environment)
1650 current-language-environment)
1651 language-info-alist t))
1653 ;; custom type will be updated with `set-language-info'.
1654 :type (if language-info-alist
1655 (cons 'choice (mapcar
1658 (sort (mapcar 'car language-info-alist) 'string<)))
1660 :initialize 'custom-initialize-default
1663 (defun reset-language-environment ()
1664 "Reset multilingual environment of Emacs to the default status.
1666 The default status is as follows:
1668 The default value of `buffer-file-coding-system' is nil.
1669 The default coding system for process I/O is nil.
1670 The default value for the command `set-terminal-coding-system' is nil.
1671 The default value for the command `set-keyboard-coding-system' is nil.
1673 The order of priorities of coding systems are as follows:
1682 ;; This function formerly set default-enable-multibyte-characters to t,
1683 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1685 (set-coding-system-priority
1694 (set-default-coding-systems nil)
1695 (setq default-sendmail-coding-system 'iso-latin-1)
1696 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1697 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1698 (setq default-file-name-coding-system 'iso-latin-1)
1699 ;; Preserve eol-type from existing default-process-coding-systems.
1700 ;; On non-unix-like systems in particular, these may have been set
1701 ;; carefully by the user, or by the startup code, to deal with the
1702 ;; users shell appropriately, so should not be altered by changing
1703 ;; language environment.
1704 (let ((output-coding
1705 ;; When bootstrapping, coding-systems are not defined yet, so
1706 ;; we need to catch the error from check-coding-system.
1708 (coding-system-change-text-conversion
1709 (car default-process-coding-system) 'undecided)
1710 (coding-system-error 'undecided)))
1713 (coding-system-change-text-conversion
1714 (cdr default-process-coding-system) 'iso-latin-1)
1715 (coding-system-error 'iso-latin-1))))
1716 (setq default-process-coding-system
1717 (cons output-coding input-coding)))
1719 ;; Don't alter the terminal and keyboard coding systems here.
1720 ;; The terminal still supports the same coding system
1721 ;; that it supported a minute ago.
1722 ;; (set-terminal-coding-system-internal nil)
1723 ;; (set-keyboard-coding-system-internal nil)
1725 (set-unibyte-charset 'iso-8859-1))
1727 (reset-language-environment)
1729 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system)
1730 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1731 (let ((coding (get-language-info language-name 'unibyte-display)))
1733 (or (not coding-system)
1734 (coding-system-equal coding coding-system)))
1735 (standard-display-european-internal)
1736 ;; The following 2 lines undo the 8-bit display that we set up
1737 ;; in standard-display-european-internal, which see. This is in
1738 ;; case the user has used standard-display-european earlier in
1739 ;; this session. (The MS-DOS port doesn't use that setup, so it
1740 ;; doesn't need to undo it.)
1741 (when standard-display-table
1743 (aset standard-display-table (+ i 128) nil))))
1744 (or (eq window-system 'pc)
1745 (set-terminal-coding-system (or coding-system coding)))))
1747 (defun set-language-environment (language-name)
1748 "Set up multi-lingual environment for using LANGUAGE-NAME.
1749 This sets the coding system priority and the default input method
1750 and sometimes other things. LANGUAGE-NAME should be a string
1751 which is the name of a language environment. For example, \"Latin-1\"
1752 specifies the character set for the major languages of Western Europe."
1753 (interactive (list (read-language-name
1755 "Set language environment (default English): ")))
1757 (if (symbolp language-name)
1758 (setq language-name (symbol-name language-name)))
1759 (setq language-name "English"))
1760 (let ((slot (assoc-string language-name language-info-alist t)))
1762 (error "Language environment not defined: %S" language-name))
1763 (setq language-name (car slot)))
1764 (if current-language-environment
1765 (let ((func (get-language-info current-language-environment
1767 (run-hooks 'exit-language-environment-hook)
1768 (if (functionp func) (funcall func))))
1769 (let ((default-eol-type (coding-system-eol-type
1770 default-buffer-file-coding-system)))
1771 (reset-language-environment)
1773 ;; The features might set up coding systems.
1774 (let ((required-features (get-language-info language-name 'features)))
1775 (while required-features
1776 (require (car required-features))
1777 (setq required-features (cdr required-features))))
1779 (setq current-language-environment language-name)
1780 (set-language-environment-coding-systems language-name default-eol-type))
1781 (let ((input-method (get-language-info language-name 'input-method)))
1783 (setq default-input-method input-method)
1784 (if input-method-history
1785 (setq input-method-history
1787 (delete input-method input-method-history))))))
1789 ;; Put higher priorities to such charsets that are supported by the
1790 ;; coding systems of higher priorities in this environment.
1791 (let ((charsets (get-language-info language-name 'charset)))
1792 (dolist (coding (get-language-info language-name 'coding-priority))
1793 (setq charsets (append charsets (coding-system-charset-list coding))))
1795 (apply 'set-charset-priority charsets)))
1797 ;; Note: For DOS, we assumed that the charset cpXXX is already
1799 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
1800 (if (eq window-system 'pc)
1801 (setq nonascii (intern "cp%d" dos-codepage)))
1802 (or (and (charsetp nonascii)
1803 (get-charset-property nonascii :ascii-compatible-p))
1804 (setq nonascii 'iso-8859-1))
1805 (set-unibyte-charset nonascii))
1807 ;; Unibyte setups if necessary.
1808 (or default-enable-multibyte-characters
1809 (set-display-table-and-terminal-coding-system language-name))
1811 (let ((required-features (get-language-info language-name 'features)))
1812 (while required-features
1813 (require (car required-features))
1814 (setq required-features (cdr required-features))))
1816 (let ((func (get-language-info language-name 'setup-function)))
1817 (if (functionp func)
1819 (run-hooks 'set-language-environment-hook)
1820 (force-mode-line-update t))
1822 (define-widget 'charset 'symbol
1825 :complete-function (lambda ()
1827 (lisp-complete-symbol 'charsetp))
1828 :completion-ignore-case t
1830 :validate (lambda (widget)
1831 (unless (charsetp (widget-value widget))
1832 (widget-put widget :error (format "Invalid charset: %S"
1833 (widget-value widget)))
1835 :prompt-history 'charset-history)
1837 (defcustom language-info-custom-alist nil
1838 "Customizations of language environment parameters.
1839 Value is an alist with elements like those of `language-info-alist'.
1840 These are used to set values in `language-info-alist' which replace
1841 the defaults. A typical use is replacing the default input method for
1842 the environment. Use \\[describe-language-environment] to find the environment's settings.
1844 This option is intended for use at startup. Removing items doesn't
1845 remove them from the language info until you next restart Emacs.
1847 Setting this variable directly does not take effect. See
1848 `set-language-info-alist' for use in programs."
1852 (custom-set-default s v)
1853 ;; Can't do this before language environments are set up.
1855 ;; modify language-info-alist
1857 (set-language-info-alist (car elt) (cdr elt)))
1858 ;; re-set the environment in case its parameters changed
1859 (set-language-environment current-language-environment)))
1861 :key-type (string :tag "Language environment"
1862 :completion-ignore-case t
1863 :complete-function widget-string-complete
1864 :completion-alist language-info-alist)
1866 (alist :key-type symbol
1867 :options ((documentation string)
1868 (charset (repeat charset))
1869 (sample-text string)
1870 (setup-function function)
1871 (exit-function function)
1872 (coding-system (repeat coding-system))
1873 (coding-priority (repeat coding-system))
1874 (nonascii-translation charset)
1877 :completion-ignore-case t
1878 :complete-function widget-string-complete
1879 :completion-alist input-method-alist
1880 :prompt-history input-method-history))
1881 (features (repeat symbol))
1882 (unibyte-display coding-system)))))
1884 (defun standard-display-european-internal ()
1885 ;; Actually set up direct output of non-ASCII characters.
1886 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1887 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1888 ;; the native font, and codes 160 and 146 stand for something very
1890 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1892 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1893 (unless (and (eq window-system 'x)
1894 ;; XFree86 4 has fixed the fonts.
1895 (string= "The XFree86 Project, Inc" (x-server-vendor))
1896 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1898 ;; Make non-line-break space display as a plain space.
1899 (aset standard-display-table 160 [32]))
1900 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1901 ;; don't contain a character at that position. Map it to the ASCII
1902 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1903 ;; U+2019, normally from the windows-1252 character set. XFree 4
1904 ;; fonts probably have the appropriate glyph at this position,
1905 ;; so they could use standard-display-8bit. It's better to use a
1906 ;; proper windows-1252 coding system. --fx]
1907 (aset standard-display-table 146 [39]))))
1909 (defun set-language-environment-coding-systems (language-name
1911 "Do various coding system setups for language environment LANGUAGE-NAME.
1913 The optional arg EOL-TYPE specifies the eol-type of the default value
1914 of `buffer-file-coding-system' set by this function."
1915 (let* ((priority (get-language-info language-name 'coding-priority))
1916 (default-coding (car priority)))
1918 (set-default-coding-systems
1919 (if (memq eol-type '(0 1 2 unix dos mac))
1920 (coding-system-change-eol-conversion default-coding eol-type)
1922 (setq default-sendmail-coding-system default-coding)
1923 (apply 'set-coding-system-priority priority))))
1925 (defsubst princ-list (&rest args)
1926 "Print all arguments with `princ', then print \"\n\"."
1927 (while args (princ (car args)) (setq args (cdr args)))
1930 (put 'describe-specified-language-support 'apropos-inhibit t)
1932 ;; Print language-specific information such as input methods,
1933 ;; charsets, and coding systems. This function is intended to be
1934 ;; called from the menu:
1935 ;; [menu-bar mule describe-language-environment LANGUAGE]
1936 ;; and should not run it by `M-x describe-current-input-method-function'.
1937 (defun describe-specified-language-support ()
1938 "Describe how Emacs supports the specified language environment."
1940 (let (language-name)
1941 (if (not (and (symbolp last-command-event)
1942 (or (not (eq last-command-event 'Default))
1943 (setq last-command-event 'English))
1944 (setq language-name (symbol-name last-command-event))))
1945 (error "Bogus calling sequence"))
1946 (describe-language-environment language-name)))
1948 (defun describe-language-environment (language-name)
1949 "Describe how Emacs supports language environment LANGUAGE-NAME."
1951 (list (read-language-name
1953 "Describe language environment (default current choice): ")))
1954 (if (null language-name)
1955 (setq language-name current-language-environment))
1956 (if (or (null language-name)
1957 (null (get-language-info language-name 'documentation)))
1958 (error "No documentation for the specified language"))
1959 (if (symbolp language-name)
1960 (setq language-name (symbol-name language-name)))
1961 (dolist (feature (get-language-info language-name 'features))
1963 (let ((doc (get-language-info language-name 'documentation)))
1964 (help-setup-xref (list #'describe-language-environment language-name)
1966 (with-output-to-temp-buffer (help-buffer)
1968 (set-buffer standard-output)
1969 (insert language-name " language environment\n\n")
1971 (insert doc "\n\n"))
1973 (let ((str (eval (get-language-info language-name 'sample-text))))
1975 (insert "Sample text:\n " str "\n\n")))
1977 (let ((input-method (get-language-info language-name 'input-method))
1978 (l (copy-sequence input-method-alist)))
1979 (insert "Input methods")
1981 (insert " (default " input-method ")")
1982 (setq input-method (assoc input-method input-method-alist))
1983 (setq l (cons input-method (delete input-method l))))
1986 (when (eq t (compare-strings language-name nil nil
1987 (nth 1 (car l)) nil nil t))
1988 (insert " " (car (car l)))
1989 (search-backward (car (car l)))
1990 (help-xref-button 0 'help-input-method (car (car l)))
1991 (goto-char (point-max))
1993 (if (stringp (nth 3 (car l)))
1995 (car (nth 3 (car l))))
1996 "\" in mode line)\n"))
1999 (insert "Character sets:\n")
2000 (let ((l (get-language-info language-name 'charset)))
2002 (insert " nothing specific to " language-name "\n")
2004 (insert " " (symbol-name (car l)))
2005 (search-backward (symbol-name (car l)))
2006 (help-xref-button 0 'help-character-set (car l))
2007 (goto-char (point-max))
2008 (insert ": " (charset-description (car l)) "\n")
2011 (insert "Coding systems:\n")
2012 (let ((l (get-language-info language-name 'coding-system)))
2014 (insert " nothing specific to " language-name "\n")
2016 (insert " " (symbol-name (car l)))
2017 (search-backward (symbol-name (car l)))
2018 (help-xref-button 0 'help-coding-system (car l))
2019 (goto-char (point-max))
2021 (coding-system-mnemonic (car l))
2022 "' in mode line):\n\t"
2023 (coding-system-doc-string (car l))
2025 (let ((aliases (coding-system-aliases (car l))))
2027 (insert "\t(alias:")
2029 (insert " " (symbol-name (car aliases)))
2030 (setq aliases (cdr aliases)))
2032 (setq l (cdr l)))))))))
2036 (defvar locale-translation-file-name nil
2037 "File name for the system's file of locale-name aliases, or nil if none.")
2039 ;; The following definitions might as well be marked as constants and
2040 ;; purecopied, since they're normally used on startup, and probably
2041 ;; should reflect the facilities of the base Emacs.
2042 (defconst locale-language-names
2045 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2046 ;; as specified in the Single Unix Spec, Version 2.
2047 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2048 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2049 ;; see Internet RFC 2165 (1997-06) and
2050 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2051 ;; TERRITORY is a country code taken from ISO 3166
2052 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2053 ;; CODESET and MODIFIER are implementation-dependent.
2055 ;; jasonr comments: MS Windows uses three letter codes for
2056 ;; languages instead of the two letter ISO codes that POSIX
2057 ;; uses. In most cases the first two letters are the same, so
2058 ;; most of the regexps in locale-language-names work. Japanese
2059 ;; and Chinese are exceptions, which are listed in the
2060 ;; non-standard section at the bottom of locale-language-names.
2062 ("aa_DJ" . "Latin-1") ; Afar
2065 ("af" . "Latin-1") ; Afrikaans
2066 ("am" "Ethiopic" utf-8) ; Amharic
2067 ("an" . "Latin-9") ; Aragonese
2068 ; ar Arabic glibc uses 8859-6
2071 ("az" . "UTF-8") ; Azerbaijani
2073 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2074 ("bg" "Bulgarian" cp1251) ; Bulgarian
2077 ("bn" . "UTF-8") ; Bengali, Bangla
2079 ("br" . "Latin-1") ; Breton
2080 ("bs" . "Latin-2") ; Bosnian
2081 ("byn" . "UTF-8") ; Bilin; Blin
2082 ("ca" . "Latin-1") ; Catalan
2084 ("cs" "Czech" iso-8859-2)
2085 ("cy" "Welsh" iso-8859-14)
2086 ("da" . "Latin-1") ; Danish
2087 ("de" "German" iso-8859-1)
2089 ("el" "Greek" iso-8859-7)
2090 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2091 ;; That's actually what the GNU locales define, modulo things like
2093 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2094 ("en" "English" iso-8859-1) ; English
2095 ("eo" . "Latin-3") ; Esperanto
2096 ("es" "Spanish" iso-8859-1)
2097 ("et" . "Latin-1") ; Estonian
2098 ("eu" . "Latin-1") ; Basque
2099 ("fa" . "UTF-8") ; Persian
2100 ("fi" . "Latin-1") ; Finnish
2101 ("fj" . "Latin-1") ; Fiji
2102 ("fo" . "Latin-1") ; Faroese
2103 ("fr" "French" iso-8859-1) ; French
2104 ("fy" . "Latin-1") ; Frisian
2105 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2106 ("gd" . "Latin-9") ; Scots Gaelic
2107 ("gez" "Ethiopic" utf-8) ; Geez
2108 ("gl" . "Latin-1") ; Gallegan; Galician
2110 ("gu" . "UTF-8") ; Gujarati
2111 ("gv" . "Latin-1") ; Manx Gaelic
2113 ("he" "Hebrew" iso-8859-8)
2114 ("hi" "Devanagari" utf-8) ; Hindi
2115 ("hr" "Croatian" iso-8859-2) ; Croatian
2116 ("hu" . "Latin-2") ; Hungarian
2119 ("id" . "Latin-1") ; Indonesian
2122 ("is" . "Latin-1") ; Icelandic
2123 ("it" "Italian" iso-8859-1) ; Italian
2125 ("iw" "Hebrew" iso-8859-8)
2126 ("ja" "Japanese" euc-jp)
2128 ("ka" "Georgian" georgian-ps) ; Georgian
2130 ("kl" . "Latin-1") ; Greenlandic
2132 ("kn" "Kannada" utf-8)
2133 ("ko" "Korean" euc-kr)
2136 ("kw" . "Latin-1") ; Cornish
2138 ("la" . "Latin-1") ; Latin
2139 ("lb" . "Latin-1") ; Luxemburgish
2140 ("lg" . "Laint-6") ; Ganda
2142 ("lo" "Lao" utf-8) ; Laothian
2143 ("lt" "Lithuanian" iso-8859-13)
2144 ("lv" . "Latvian") ; Latvian, Lettish
2146 ("mi" . "Latin-7") ; Maori
2147 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2148 ("ml" "Malayalam" utf-8)
2149 ("mn" . "UTF-8") ; Mongolian
2151 ("mr" "Devanagari" utf-8) ; Marathi
2152 ("ms" . "Latin-1") ; Malay
2153 ("mt" . "Latin-3") ; Maltese
2156 ("nb" . "Latin-1") ; Norwegian
2157 ("ne" "Devanagari" utf-8) ; Nepali
2158 ("nl" "Dutch" iso-8859-1)
2159 ("no" . "Latin-1") ; Norwegian
2160 ("oc" . "Latin-1") ; Occitan
2161 ("om_ET" . "UTF-8") ; (Afan) Oromo
2162 ("om" . "Latin-1") ; (Afan) Oromo
2164 ("pa" . "UTF-8") ; Punjabi
2165 ("pl" . "Latin-2") ; Polish
2167 ("pt" . "Latin-1") ; Portuguese
2169 ("rm" . "Latin-1") ; Rhaeto-Romanic
2171 ("ro" "Romanian" iso-8859-2)
2172 ("ru_RU" "Russian" iso-8859-5)
2173 ("ru_UA" "Russian" koi8-u)
2175 ("sa" . "Devanagari") ; Sanskrit
2177 ("se" . "UTF-8") ; Northern Sami
2179 ("sh" . "Latin-2") ; Serbo-Croatian
2181 ("sid" . "UTF-8") ; Sidamo
2182 ("sk" "Slovak" iso-8859-2)
2183 ("sl" "Slovenian" iso-8859-2)
2186 ("so_ET" "UTF-8") ; Somali
2187 ("so" "Latin-1") ; Somali
2188 ("sq" . "Latin-1") ; Albanian
2189 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2191 ("st" . "Latin-1") ; Sesotho
2193 ("sv" "Swedish" iso-8859-1) ; Swedish
2194 ("sw" . "Latin-1") ; Swahili
2195 ("ta" "Tamil" utf-8)
2196 ("te" . "UTF-8") ; Telugu
2197 ("tg" "Tajik" koi8-t)
2198 ("th" "Thai" tis-620)
2199 ("ti" "Ethiopic" utf-8) ; Tigrinya
2200 ("tig_ER" . "UTF-8") ; Tigre
2202 ("tl" . "Latin-1") ; Tagalog
2205 ("tr" "Turkish" iso-8859-9)
2207 ("tt" . "UTF-8") ; Tatar
2210 ("uk" "Ukrainian" koi8-u)
2211 ("ur" . "UTF-8") ; Urdu
2212 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2213 ("uz" . "Latin-1") ; Uzbek
2214 ("vi" "Vietnamese" utf-8)
2216 ("wa" . "Latin-1") ; Walloon
2218 ("xh" . "Latin-1") ; Xhosa
2219 ("yi" . "Windows-1255") ; Yiddish
2222 ("zh_HK" . "Chinese-Big5")
2223 ; zh_HK/BIG5-HKSCS \
2224 ("zh_TW" . "Chinese-Big5")
2225 ("zh_CN.GB2312" "Chinese-GB")
2226 ("zh_CN.GBK" "Chinese-GBK")
2227 ("zh_CN.GB18030" "Chinese-GB18030")
2228 ("zh_CN" . "Chinese-GB")
2229 ("zh" . "Chinese-GB")
2230 ("zu" . "Latin-1") ; Zulu
2232 ;; ISO standard locales
2234 ("posix$" . "ASCII")
2236 ;; The "IPA" Emacs language environment does not correspond
2237 ;; to any ISO 639 code, so let it stand for itself.
2240 ;; Nonstandard or obsolete language codes
2241 ("cz" . "Czech") ; e.g. Solaris 2.6
2242 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2243 ("iw" . "Hebrew") ; e.g. X11R6.4
2244 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2245 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2246 ("jp" . "Japanese") ; e.g. MS Windows
2247 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2248 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2250 "Alist of locale regexps vs the corresponding languages and coding systems.
2251 Each element has these form:
2252 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2253 The first element whose LOCALE-REGEXP matches the start of a
2254 downcased locale specifies the LANG-ENV \(language environtment)
2255 and CODING-SYSTEM corresponding to that locale. If there is no
2256 appropriate language environment, the element may have this form:
2257 \(LOCALE-REGEXP . LANG-ENV)
2258 In this case, LANG-ENV is one of generic language environments for an
2259 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2261 (defconst locale-charset-language-names
2263 '((".*8859[-_]?1\\>" . "Latin-1")
2264 (".*8859[-_]?2\\>" . "Latin-2")
2265 (".*8859[-_]?3\\>" . "Latin-3")
2266 (".*8859[-_]?4\\>" . "Latin-4")
2267 (".*8859[-_]?9\\>" . "Latin-5")
2268 (".*8859[-_]?14\\>" . "Latin-8")
2269 (".*8859[-_]?15\\>" . "Latin-9")
2270 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2271 ;; utf-8@euro exists, so put this last. (@euro really specifies
2272 ;; the currency, rather than the charset.)
2273 (".*@euro\\>" . "Latin-9")))
2274 "List of pairs of locale regexps and charset language names.
2275 The first element whose locale regexp matches the start of a downcased locale
2276 specifies the language name whose charset corresponds to that locale.
2277 This language name is used if the locale is not listed in
2278 `locale-language-names'")
2280 (defconst locale-preferred-coding-systems
2282 '((".*8859[-_]?1\\>" . iso-8859-1)
2283 (".*8859[-_]?2\\>" . iso-8859-2)
2284 (".*8859[-_]?3\\>" . iso-8859-3)
2285 (".*8859[-_]?4\\>" . iso-8859-4)
2286 (".*8859[-_]?9\\>" . iso-8859-9)
2287 (".*8859[-_]?14\\>" . iso-8859-14)
2288 (".*8859[-_]?15\\>" . iso-8859-15)
2289 (".*utf\\(?:-?8\\)?" . utf-8)
2290 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2291 ;; specifies the currency, rather than the charset.)
2292 (".*@euro" . iso-8859-15)
2293 ("koi8-?r" . koi8-r)
2294 ("koi8-?u" . koi8-u)
2296 ("big5[-_]?hkscs" . big5-hkscs)
2298 ("euc-?tw" . euc-tw)
2302 ("gb18030" . gb18030)
2303 ("ja.*[._]euc" . japanese-iso-8bit)
2304 ("ja.*[._]jis7" . iso-2022-jp)
2305 ("ja.*[._]pck" . japanese-shift-jis)
2306 ("ja.*[._]sjis" . japanese-shift-jis)
2307 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2309 "List of pairs of locale regexps and preferred coding systems.
2310 The first element whose locale regexp matches the start of a downcased locale
2311 specifies the coding system to prefer when using that locale.
2312 This coding system is used if the locale specifies a specific charset.")
2314 (defun locale-name-match (key alist)
2315 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2316 Return the value corresponding to the first regexp that matches the
2317 start of KEY, or nil if there is no match."
2319 (while (and alist (not element))
2320 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2321 (setq element (car alist)))
2322 (setq alist (cdr alist)))
2325 (defun locale-charset-match-p (charset1 charset2)
2326 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2327 Matching is done ignoring case and any hyphens and underscores in the
2328 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2329 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2330 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2331 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2333 (defvar locale-charset-alist nil
2334 "Coding system alist keyed on locale-style charset name.
2335 Used by `locale-charset-to-coding-system'.")
2337 (defun locale-charset-to-coding-system (charset)
2338 "Find coding system corresponding to CHARSET.
2339 CHARSET is any sort of non-Emacs charset name, such as might be used
2340 in a locale codeset, or elsewhere. It is matched to a coding system
2341 first by case-insensitive lookup in `locale-charset-alist'. Then
2342 matches are looked for in the coding system list, treating case and
2343 the characters `-' and `_' as insignificant. The coding system base
2344 is returned. Thus, for instance, if charset \"ISO8859-2\",
2345 `iso-latin-2' is returned."
2346 (or (car (assoc-string charset locale-charset-alist t))
2347 (let ((cs coding-system-alist)
2349 (while (and (not c) cs)
2350 (if (locale-charset-match-p charset (caar cs))
2351 (setq c (intern (caar cs)))
2353 (if c (coding-system-base c)))))
2355 ;; Fixme: This ought to deal with the territory part of the locale
2356 ;; too, for setting things such as calendar holidays, ps-print paper
2357 ;; size, spelling dictionary.
2359 (defun set-locale-environment (&optional locale-name)
2360 "Set up multi-lingual environment for using LOCALE-NAME.
2361 This sets the language environment, the coding system priority,
2362 the default input method and sometimes other things.
2364 LOCALE-NAME should be a string which is the name of a locale supported
2365 by the system. Often it is of the form xx_XX.CODE, where xx is a
2366 language, XX is a country, and CODE specifies a character set and
2367 coding system. For example, the locale name \"ja_JP.EUC\" might name
2368 a locale for Japanese in Japan using the `japanese-iso-8bit'
2369 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2372 If LOCALE-NAME is nil, its value is taken from the environment
2373 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2375 The locale names supported by your system can typically be found in a
2376 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2377 will be translated according to the table specified by
2378 `locale-translation-file-name'.
2380 See also `locale-charset-language-names', `locale-language-names',
2381 `locale-preferred-coding-systems' and `locale-coding-system'."
2382 (interactive "sSet environment for locale: ")
2384 ;; Do this at runtime for the sake of binaries possibly transported
2385 ;; to a system without X.
2386 (setq locale-translation-file-name
2388 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2389 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2390 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2391 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2393 ;; The following name appears after the X-related names above,
2394 ;; since the X-related names are what X actually uses.
2395 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2397 (while (and files (not (file-exists-p (car files))))
2398 (setq files (cdr files)))
2401 (let ((locale locale-name))
2404 ;; Use the first of these three environment variables
2405 ;; that has a nonempty value.
2406 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2408 (= 0 (length locale))) ; nil or empty string
2409 (setq locale (getenv (pop vars))))))
2412 ;; The two tests are kept separate so the byte-compiler sees
2413 ;; that mac-get-preference is only called after checking its existence.
2414 (when (fboundp 'mac-get-preference)
2415 (setq locale (mac-get-preference "AppleLocale"))
2417 (let ((languages (mac-get-preference "AppleLanguages")))
2418 (unless (= (length languages) 0) ; nil or empty vector
2419 (setq locale (aref languages 0)))))))
2420 (unless (or locale (not (boundp 'mac-system-locale)))
2421 (setq locale mac-system-locale))
2425 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2426 ;; using the translation file that many systems have.
2427 (when locale-translation-file-name
2429 (set-buffer-multibyte nil)
2430 (insert-file-contents locale-translation-file-name)
2431 (when (re-search-forward
2432 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2433 (setq locale (buffer-substring (point) (line-end-position))))))
2435 ;; Leave the system locales alone if the caller did not specify
2436 ;; an explicit locale name, as their defaults are set from
2437 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2438 ;; want to set them to the same value as LC_CTYPE.
2440 (setq system-messages-locale locale)
2441 (setq system-time-locale locale))
2443 (setq locale (downcase locale))
2445 (let ((language-name
2446 (locale-name-match locale locale-language-names))
2447 (charset-language-name
2448 (locale-name-match locale locale-charset-language-names))
2450 (or (locale-name-match locale locale-preferred-coding-systems)
2452 (if (string-match "\\.\\([^@]+\\)" locale)
2453 (locale-charset-to-coding-system
2454 (match-string 1 locale))))
2455 (and (eq system-type 'macos) mac-system-coding-system))))
2457 (if (consp language-name)
2458 ;; locale-language-names specify both lang-env and coding.
2459 ;; But, what specified in locale-preferred-coding-systems
2460 ;; has higher priority.
2461 (setq coding-system (or coding-system
2462 (nth 1 language-name))
2463 language-name (car language-name))
2464 ;; Otherwise, if locale is not listed in locale-language-names,
2465 ;; use what listed in locale-charset-language-names.
2466 (if (not language-name)
2467 (setq language-name charset-language-name)))
2471 ;; Set up for this character set. This is now the right way
2472 ;; to do it for both unibyte and multibyte modes.
2473 (set-language-environment language-name)
2475 ;; If default-enable-multibyte-characters is nil,
2476 ;; we are using single-byte characters,
2477 ;; so the display table and terminal coding system are irrelevant.
2478 (when default-enable-multibyte-characters
2479 (set-display-table-and-terminal-coding-system
2480 language-name coding-system))
2482 ;; Set the `keyboard-coding-system' if appropriate (tty
2483 ;; only). At least X and MS Windows can generate
2484 ;; multilingual input.
2485 (unless window-system
2486 (let ((kcs (or coding-system
2487 (car (get-language-info language-name
2489 (if kcs (set-keyboard-coding-system kcs))))
2491 (setq locale-coding-system
2492 (car (get-language-info language-name 'coding-priority))))
2494 (when (and coding-system
2495 (not (coding-system-equal coding-system
2496 locale-coding-system)))
2497 (prefer-coding-system coding-system)
2498 (setq locale-coding-system coding-system))
2499 (when (get-language-info current-language-environment 'coding-priority)
2500 (let ((codeset (locale-info 'codeset))
2501 (coding-system (car (coding-system-priority-list))))
2503 (let ((cs (coding-system-aliases coding-system))
2505 (while (and cs (not result))
2507 (locale-charset-match-p (symbol-name (pop cs))
2508 (locale-info 'codeset))))
2510 (message "Warning: Default coding system `%s' disagrees with
2511 system codeset `%s' for this locale." coding-system codeset))))))))
2513 ;; On Windows, override locale-coding-system,
2514 ;; keyboard-coding-system with system codepage. Note:
2515 ;; selection-coding-system is already set in w32select.c.
2516 (when (boundp 'w32-ansi-code-page)
2517 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2518 (when (coding-system-p code-page-coding)
2519 (setq locale-coding-system code-page-coding)
2520 (set-keyboard-coding-system code-page-coding)
2521 (set-terminal-coding-system code-page-coding))))
2523 (when (eq system-type 'darwin)
2524 ;; On Darwin, file names are always encoded in utf-8, no matter
2526 (setq default-file-name-coding-system 'utf-8)
2527 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2529 (when (and (null window-system)
2530 (equal (getenv "TERM_PROGRAM") "Apple_Terminal"))
2531 (set-terminal-coding-system 'utf-8)
2532 (set-keyboard-coding-system 'utf-8)))
2534 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2535 ;; (See comments in Flocale_info.)
2536 (let ((locale locale)
2537 (paper (locale-info 'paper)))
2539 ;; This will always be null at the time of writing.
2541 ((equal paper '(216 279))
2542 (setq ps-paper-type 'letter))
2543 ((equal paper '(210 297))
2544 (setq ps-paper-type 'a4)))
2545 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2546 (while (and vars (= 0 (length locale)))
2547 (setq locale (getenv (pop vars)))))
2549 ;; As of glibc 2.2.5, these are the only US Letter locales,
2550 ;; and the rest are A4.
2552 (or (locale-name-match locale '(("c$" . letter)
2557 ("enu$" . letter) ; Windows
2564 ;;; Character property
2566 ;; Each element has the form (PROP . TABLE).
2567 ;; PROP is a symbol representing a character property.
2568 ;; TABLE is a char-table containing the property value for each character.
2569 ;; TABLE may be a name of file to load to build a char-table.
2570 ;; Don't modify this variable directly but use `define-char-code-property'.
2572 (defvar char-code-property-alist nil
2573 "Alist of character property name vs char-table containing property values.
2574 Internal use only.")
2576 (put 'char-code-property-table 'char-table-extra-slots 5)
2578 (defun define-char-code-property (name table &optional docstring)
2579 "Define NAME as a character code property given by TABLE.
2580 TABLE is a char-table of purpose `char-code-property-table' with
2583 2nd: Function to call to get a property value of a character.
2584 It is called with three arugments CHAR, VAL, and TABLE, where
2585 CHAR is a character, VAL is the value of (aref TABLE CHAR).
2586 3rd: Function to call to put a property value of a character.
2587 It is called with the same arguments as above.
2588 4th: Function to call to get a description string of a property value.
2589 It is called with one argument VALUE, a property value.
2590 5th: Data used by the above functions.
2592 TABLE may be a name of file to load to build a char-table. The
2593 file should contain a call of `define-char-code-property' with a
2594 char-table of the above format as the argument TABLE.
2596 TABLE may also be nil, in which case no property value is pre-assigned.
2598 Optional 3rd argment DOCSTRING is a documentation string of the property.
2600 See also the documentation of `get-char-code-property' and
2601 `put-char-code-property'."
2603 (error "Not a symbol: %s" name))
2604 (if (char-table-p table)
2605 (or (and (eq (char-table-subtype table) 'char-code-property-table)
2606 (eq (char-table-extra-slot table 0) name))
2607 (error "Invalid char-table: %s" table))
2609 (error "Not a char-table nor a file name: %s" table)))
2610 (let ((slot (assq name char-code-property-alist)))
2613 (setq char-code-property-alist
2614 (cons (cons name table) char-code-property-alist))))
2615 (put name 'char-code-property-documentation docstring))
2617 (defvar char-code-property-table
2618 (make-char-table 'char-code-property-table)
2619 "Char-table containing a property list of each character code.
2620 This table is used for properties not listed in `char-code-property-alist'.
2621 See also the documentation of `get-char-code-property' and
2622 `put-char-code-property'.")
2624 (defun get-char-code-property (char propname)
2625 "Return the value of CHAR's PROPNAME property."
2626 (let ((slot (assq propname char-code-property-alist)))
2628 (let (table value func)
2629 (if (stringp (cdr slot))
2631 (setq table (cdr slot)
2632 value (aref table char)
2633 func (char-table-extra-slot table 1))
2634 (if (functionp func)
2635 (setq value (funcall func char value table)))
2637 (plist-get (aref char-code-property-table char) propname))))
2639 (defun put-char-code-property (char propname value)
2640 "Store CHAR's PROPNAME property with VALUE.
2641 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2642 (let ((slot (assq propname char-code-property-alist)))
2645 (if (stringp (cdr slot))
2647 (setq table (cdr slot)
2648 func (char-table-extra-slot table 2))
2649 (if (functionp func)
2650 (funcall func char value table)
2651 (aset table char value)))
2652 (let* ((plist (aref char-code-property-table char))
2653 (x (plist-put plist propname value)))
2655 (aset char-code-property-table char x))))
2658 (defun char-code-property-description (prop value)
2659 "Return a description string of character property PROP's value VALUE.
2660 If there's no description string for VALUE, return nil."
2661 (let ((slot (assq prop char-code-property-alist)))
2664 (if (stringp (cdr slot))
2666 (setq table (cdr slot)
2667 func (char-table-extra-slot table 3))
2668 (if (functionp func)
2669 (funcall func value))))))
2672 ;; Pretty description of encoded string
2674 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2675 (defvar iso-2022-control-alist
2683 (defun encoded-string-description (str coding-system)
2684 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2685 (setq str (string-as-unibyte str))
2687 (if (and coding-system (eq (coding-system-type coding-system) 'iso-2022))
2688 ;; Try to get a pretty description for ISO 2022 escape sequences.
2689 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2690 (format "#x%02X" x))))
2691 (function (lambda (x) (format "#x%02X" x))))
2694 (defun encode-coding-char (char coding-system)
2695 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2696 If CODING-SYSTEM can't safely encode CHAR, return nil."
2697 (let ((str1 (string-as-multibyte (string char)))
2698 (str2 (string-as-multibyte (string char char)))
2700 (when (memq (coding-system-base coding-system)
2701 (find-coding-systems-string str1))
2702 ;; We must find the encoded string of CHAR. But, just encoding
2703 ;; CHAR will put extra control sequences (usually to designate
2704 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2705 ;; To exclude such tailing bytes, we at first encode one-char
2706 ;; string and two-char string, then check how many bytes at the
2707 ;; tail of both encoded strings are the same.
2709 (setq enc1 (encode-coding-string str1 coding-system)
2711 enc2 (encode-coding-string str2 coding-system)
2713 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2714 (setq i1 (1- i1) i2 (1- i2)))
2716 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2717 ;; and they are the extra control sequences at the tail to
2719 (substring enc2 0 i2))))
2721 ;; Backwards compatibility. These might be better with :init-value t,
2722 ;; but that breaks loadup.
2723 (define-minor-mode unify-8859-on-encoding-mode
2727 (define-minor-mode unify-8859-on-decoding-mode
2732 (defvar nonascii-insert-offset 0 "This variable is obsolete.")
2733 (defvar nonascii-translation-table nil "This variable is obsolete.")
2736 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2737 ;;; mule-cmds.el ends here