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 ;; 2006 Free Software Foundation, Inc.
5 ;; Copyright (C) 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
7 ;; National Institute of Advanced Industrial Science and Technology (AIST)
8 ;; Registration Number H14PRO021
10 ;; National Institute of Advanced Industrial Science and Technology (AIST)
11 ;; Registration Number H13PRO009
13 ;; Keywords: mule, i18n
15 ;; This file is part of GNU Emacs.
17 ;; GNU Emacs is free software; you can redistribute it and/or modify
18 ;; it under the terms of the GNU General Public License as published by
19 ;; the Free Software Foundation; either version 2, or (at your option)
22 ;; GNU Emacs is distributed in the hope that it will be useful,
23 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
24 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 ;; GNU General Public License for more details.
27 ;; You should have received a copy of the GNU General Public License
28 ;; along with GNU Emacs; see the file COPYING. If not, write to the
29 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
30 ;; Boston, MA 02110-1301, USA.
38 (autoload 'widget-value "wid-edit"))
40 (defvar mac-system-coding-system)
41 (defvar mac-system-locale)
43 ;;; MULE related key bindings and menus.
45 (defvar mule-keymap (make-sparse-keymap)
46 "Keymap for Mule (Multilingual environment) specific commands.")
48 ;; Keep "C-x C-m ..." for mule specific commands.
49 (define-key ctl-x-map "\C-m" mule-keymap)
51 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
52 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
53 (define-key mule-keymap "F" 'set-file-name-coding-system)
54 (define-key mule-keymap "t" 'set-terminal-coding-system)
55 (define-key mule-keymap "k" 'set-keyboard-coding-system)
56 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
57 (define-key mule-keymap "x" 'set-selection-coding-system)
58 (define-key mule-keymap "X" 'set-next-selection-coding-system)
59 (define-key mule-keymap "\C-\\" 'set-input-method)
60 (define-key mule-keymap "c" 'universal-coding-system-argument)
61 (define-key mule-keymap "l" 'set-language-environment)
63 (defvar mule-menu-keymap
64 (make-sparse-keymap "Mule (Multilingual Environment)")
65 "Keymap for Mule (Multilingual environment) menu specific commands.")
67 (defvar describe-language-environment-map
68 (make-sparse-keymap "Describe Language Environment"))
70 (defvar setup-language-environment-map
71 (make-sparse-keymap "Set Language Environment"))
73 (defvar set-coding-system-map
74 (make-sparse-keymap "Set Coding System"))
76 (define-key-after mule-menu-keymap [set-language-environment]
77 (list 'menu-item "Set Language Environment" setup-language-environment-map))
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 ;; We call `coding-system-base' before `coding-system-eol-type',
224 ;; because the coding-system may not be initialized until then.
225 (let* ((base (coding-system-base coding-system))
226 (orig-eol-type (coding-system-eol-type coding-system)))
227 (cond ((vectorp orig-eol-type)
230 (aref orig-eol-type eol-type)))
233 ((= eol-type orig-eol-type)
235 ((progn (setq orig-eol-type (coding-system-eol-type base))
236 (vectorp orig-eol-type))
237 (aref orig-eol-type eol-type)))))
239 (defun coding-system-change-text-conversion (coding-system coding)
240 "Return a coding system which differs from CODING-SYSTEM in text conversion.
241 The returned coding system converts text by CODING
242 but end-of-line as the same way as CODING-SYSTEM.
243 If CODING is nil, the returned coding system detects
244 how text is formatted automatically while decoding."
245 (let ((eol-type (coding-system-eol-type coding-system)))
246 (coding-system-change-eol-conversion
247 (if coding coding 'undecided)
248 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
250 (defun toggle-enable-multibyte-characters (&optional arg)
251 "Change whether this buffer uses multibyte characters.
252 With arg, use multibyte characters if the arg is positive.
254 Note that this command does not convert the byte contents of
255 the buffer; it only changes the way those bytes are interpreted.
256 In general, therefore, this command *changes* the sequence of
257 characters that the current buffer contains.
259 We suggest you avoid using use this command unless you know what you
260 are doing. If you use it by mistake, and the buffer is now displayed
261 wrong, use this command again to toggle back to the right mode."
264 (if (null arg) (null enable-multibyte-characters)
265 (> (prefix-numeric-value arg) 0))))
266 (set-buffer-multibyte new-flag))
267 (force-mode-line-update))
269 (defun view-hello-file ()
270 "Display the HELLO file which list up many languages and characters."
272 ;; We have to decode the file in any environment.
273 (let ((default-enable-multibyte-characters t)
274 (coding-system-for-read 'iso-2022-7bit))
275 (view-file (expand-file-name "HELLO" data-directory))))
277 (defun universal-coding-system-argument (coding-system)
278 "Execute an I/O command using the specified coding system."
280 (let ((default (and buffer-file-coding-system
281 (not (eq (coding-system-type buffer-file-coding-system)
283 buffer-file-coding-system)))
284 (list (read-coding-system
286 (format "Coding system for following command (default %s): " default)
287 "Coding system for following command: ")
289 (let* ((keyseq (read-key-sequence
290 (format "Command to execute with %s:" coding-system)))
291 (cmd (key-binding keyseq))
294 (when (eq cmd 'universal-argument)
295 (call-interactively cmd)
297 ;; Process keys bound in `universal-argument-map'.
299 (setq keyseq (read-key-sequence nil t)
300 cmd (key-binding keyseq t))
301 (not (eq cmd 'universal-argument-other-key)))
302 (let ((current-prefix-arg prefix-arg)
303 ;; Have to bind `last-command-char' here so that
304 ;; `digit-argument', for instance, can compute the
306 (last-command-char (aref keyseq 0)))
307 (call-interactively cmd)))
309 ;; This is the final call to `universal-argument-other-key', which
310 ;; set's the final `prefix-arg.
311 (let ((current-prefix-arg prefix-arg))
312 (call-interactively cmd))
314 ;; Read the command to execute with the given prefix arg.
315 (setq prefix prefix-arg
316 keyseq (read-key-sequence nil t)
317 cmd (key-binding keyseq)))
319 (let ((coding-system-for-read coding-system)
320 (coding-system-for-write coding-system)
321 (coding-system-require-warning t)
322 (current-prefix-arg prefix))
324 (call-interactively cmd))))
326 (defun set-default-coding-systems (coding-system)
327 "Set default value of various coding systems to CODING-SYSTEM.
328 This sets the following coding systems:
329 o coding system of a newly created buffer
330 o default coding system for subprocess I/O
331 This also sets the following values:
332 o default value used as `file-name-coding-system' for converting file names
333 if CODING-SYSTEM is ASCII-compatible.
334 o default value for the command `set-terminal-coding-system' (not on MSDOS)
335 o default value for the command `set-keyboard-coding-system'
336 if CODING-SYSTEM is ASCII-compatible.."
337 (check-coding-system coding-system)
338 (setq-default buffer-file-coding-system coding-system)
339 (if (fboundp 'ucs-set-table-for-input)
340 (dolist (buffer (buffer-list))
341 (or (local-variable-p 'buffer-file-coding-system buffer)
342 (ucs-set-table-for-input buffer))))
344 (if (eq system-type 'darwin)
345 ;; The file-name coding system on Darwin systems is always utf-8.
346 (setq default-file-name-coding-system 'utf-8)
347 (if (and default-enable-multibyte-characters
348 (or (not coding-system)
349 (coding-system-get coding-system 'ascii-compatible-p)))
350 (setq default-file-name-coding-system coding-system)))
351 ;; If coding-system is nil, honor that on MS-DOS as well, so
352 ;; that they could reset the terminal coding system.
353 (unless (and (eq window-system 'pc) coding-system)
354 (setq default-terminal-coding-system coding-system))
355 (setq default-keyboard-coding-system coding-system)
356 ;; Preserve eol-type from existing default-process-coding-systems.
357 ;; On non-unix-like systems in particular, these may have been set
358 ;; carefully by the user, or by the startup code, to deal with the
359 ;; users shell appropriately, so should not be altered by changing
360 ;; language environment.
362 (coding-system-change-text-conversion
363 (car default-process-coding-system) coding-system))
365 (coding-system-change-text-conversion
366 (cdr default-process-coding-system) coding-system)))
367 (setq default-process-coding-system
368 (cons output-coding input-coding))))
370 (defun prefer-coding-system (coding-system)
371 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
372 This also sets the following coding systems:
373 o coding system of a newly created buffer
374 o default coding system for subprocess I/O
375 This also sets the following values:
376 o default value used as `file-name-coding-system' for converting file names.
377 o default value for the command `set-terminal-coding-system' (not on MSDOS)
378 o default value for the command `set-keyboard-coding-system'
380 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
381 systems set by this function will use that type of EOL conversion.
383 This command does not change the default value of terminal coding system
384 for MS-DOS terminal, because DOS terminals only support a single coding
385 system, and Emacs automatically sets the default to that coding system at
388 A coding system that requires automatic detection of text
389 +encoding (e.g. undecided, unix) can't be preferred.."
390 (interactive "zPrefer coding system: ")
391 (if (not (and coding-system (coding-system-p coding-system)))
392 (error "Invalid coding system `%s'" coding-system))
393 (if (memq (coding-system-type coding-system) '(raw-text undecided))
394 (error "Can't prefer the coding system `%s'" coding-system))
395 (let ((base (coding-system-base coding-system))
396 (eol-type (coding-system-eol-type coding-system)))
397 (set-coding-system-priority base)
399 (or (eq base coding-system)
400 (message "Highest priority is set to %s (base of %s)"
401 base coding-system)))
402 ;; If they asked for specific EOL conversion, honor that.
403 (if (memq eol-type '(0 1 2))
405 (coding-system-change-eol-conversion base eol-type)))
406 (set-default-coding-systems base)))
408 (defvar sort-coding-systems-predicate nil
409 "If non-nil, a predicate function to sort coding systems.
411 It is called with two coding systems, and should return t if the first
412 one is \"less\" than the second.
414 The function `sort-coding-systems' use it.")
416 (defun sort-coding-systems (codings)
417 "Sort coding system list CODINGS by a priority of each coding system.
418 Returns the sorted list. CODINGS is modified by side effects.
420 If a coding system is most preferred, it has the highest priority.
421 Otherwise, a coding system corresponds to some MIME charset has higher
422 priorities. Among them, a coding system included in `coding-system'
423 key of the current language environment has higher priorities. See
424 also the documentation of `language-info-alist'.
426 If the variable `sort-coding-systems-predicate' (which see) is
427 non-nil, it is used to sort CODINGS in the different way than above."
428 (if sort-coding-systems-predicate
429 (sort codings sort-coding-systems-predicate)
430 (let* ((from-priority (coding-system-priority-list))
431 (most-preferred (car from-priority))
432 (lang-preferred (get-language-info current-language-environment
436 (let ((base (coding-system-base x)))
437 ;; We calculate the priority number 0..255 by
438 ;; using the 8 bits PMMLCEII as this:
439 ;; P: 1 iff most preferred.
440 ;; MM: greater than 0 iff mime-charset.
441 ;; L: 1 iff one of the current lang. env.'s codings.
442 ;; C: 1 iff one of codings listed in the category list.
443 ;; E: 1 iff not XXX-with-esc
444 ;; II: if iso-2022 based, 0..3, else 1.
446 (lsh (if (eq base most-preferred) 1 0) 7)
448 (let ((mime (coding-system-get base :mime-charset)))
449 ;; Prefer coding systems corresponding to a
452 ;; Lower utf-16 priority so that we
453 ;; normally prefer utf-8 to it, and put
454 ;; x-ctext below that.
455 (cond ((string-match "utf-16"
458 ((string-match "^x-" (symbol-name mime))
463 (lsh (if (memq base lang-preferred) 1 0) 4)
464 (lsh (if (memq base from-priority) 1 0) 3)
465 (lsh (if (string-match "-with-esc\\'"
468 (if (eq (coding-system-type base) 'iso-2022)
469 (let ((category (coding-system-category base)))
470 ;; For ISO based coding systems, prefer
471 ;; one that doesn't use designation nor
472 ;; locking/single shifting.
474 ((or (eq category 'coding-category-iso-8-1)
475 (eq category 'coding-category-iso-8-2))
477 ((or (eq category 'coding-category-iso-7-tight)
478 (eq category 'coding-category-iso-7))
484 (sort codings (function (lambda (x y)
485 (> (funcall func x) (funcall func y))))))))
487 (defun find-coding-systems-region (from to)
488 "Return a list of proper coding systems to encode a text between FROM and TO.
490 If FROM is a string, find coding systems in that instead of the buffer.
491 All coding systems in the list can safely encode any multibyte characters
494 If the text contains no multibyte characters, return a list of a single
495 element `undecided'."
496 (let ((codings (find-coding-systems-region-internal from to)))
498 ;; The text contains only ASCII characters. Any coding
501 ;; We need copy-sequence because sorting will alter the argument.
502 (sort-coding-systems (copy-sequence codings)))))
504 (defun find-coding-systems-string (string)
505 "Return a list of proper coding systems to encode STRING.
506 All coding systems in the list can safely encode any multibyte characters
509 If STRING contains no multibyte characters, return a list of a single
510 element `undecided'."
511 (find-coding-systems-region string nil))
513 (defun find-coding-systems-for-charsets (charsets)
514 "Return a list of proper coding systems to encode characters of CHARSETS.
515 CHARSETS is a list of character sets.
517 This only finds coding systems of type `charset', whose
518 `:charset-list' property includes all of CHARSETS (plus `ascii' for
519 ascii-compatible coding systems). It was used in older versions of
520 Emacs, but is unlikely to be what you really want now."
521 ;; Deal with aliases.
522 (setq charsets (mapcar (lambda (c)
523 (get-charset-property c :name))
525 (cond ((or (null charsets)
526 (and (= (length charsets) 1)
527 (eq 'ascii (car charsets))))
529 ((or (memq 'eight-bit-control charsets)
530 (memq 'eight-bit-graphic charsets))
531 '(raw-text utf-8-emacs))
534 (dolist (cs (coding-system-list t))
535 (let ((cs-charsets (and (eq (coding-system-type cs) 'charset)
536 (coding-system-charset-list cs)))
538 (if (coding-system-get cs :ascii-compatible-p)
539 (add-to-list 'cs-charsets 'ascii))
543 (unless (memq (pop charsets) cs-charsets)
547 (nreverse codings)))))
549 (defun find-multibyte-characters (from to &optional maxcount excludes)
550 "Find multibyte characters in the region specified by FROM and TO.
551 If FROM is a string, find multibyte characters in the string.
552 The return value is an alist of the following format:
553 ((CHARSET COUNT CHAR ...) ...)
555 CHARSET is a character set,
556 COUNT is a number of characters,
557 CHARs are the characters found from the character set.
558 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
559 Optional 4th arg EXCLUDE is a list of character sets to be ignored."
563 (if (multibyte-string-p from)
565 (while (setq idx (string-match "[^\000-\177]" from idx))
566 (setq char (aref from idx)
567 charset (char-charset char))
568 (unless (memq charset excludes)
569 (let ((slot (assq charset chars)))
571 (if (not (memq char (nthcdr 2 slot)))
572 (let ((count (nth 1 slot)))
573 (setcar (cdr slot) (1+ count))
574 (if (or (not maxcount) (< count maxcount))
575 (nconc slot (list char)))))
576 (setq chars (cons (list charset 1 char) chars)))))
577 (setq idx (1+ idx)))))
578 (if enable-multibyte-characters
581 (while (re-search-forward "[^\000-\177]" to t)
582 (setq char (preceding-char)
583 charset (char-charset char))
584 (unless (memq charset excludes)
585 (let ((slot (assq charset chars)))
587 (if (not (member char (nthcdr 2 slot)))
588 (let ((count (nth 1 slot)))
589 (setcar (cdr slot) (1+ count))
590 (if (or (not maxcount) (< count maxcount))
591 (nconc slot (list char)))))
592 (setq chars (cons (list charset 1 char) chars)))))))))
595 (defun search-unencodable-char (coding-system)
596 "Search forward from point for a character that is not encodable.
597 It asks which coding system to check.
598 If such a character is found, set point after that character.
599 Otherwise, don't move point.
601 When called from a program, the value is a position of the found character,
602 or nil if all characters are encodable."
604 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
606 (format "Coding-system (default %s): " default)
608 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
611 (message "All following characters are encodable by %s" coding-system))
614 (defvar last-coding-system-specified nil
615 "Most recent coding system explicitly specified by the user when asked.
616 This variable is set whenever Emacs asks the user which coding system
617 to use in order to write a file. If you set it to nil explicitly,
618 then call `write-region', then afterward this variable will be non-nil
619 only if the user was explicitly asked and specified a coding system.")
621 (defvar select-safe-coding-system-accept-default-p nil
622 "If non-nil, a function to control the behavior of coding system selection.
623 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
624 function `select-safe-coding-system' (which see). This variable
625 overrides that argument.")
627 (defun select-safe-coding-system-interactively (from to codings unsafe
628 &optional rejected default)
629 "Select interactively a coding system for the region FROM ... TO.
630 FROM can be a string, as in `write-region'.
631 CODINGS is the list of base coding systems known to be safe for this region,
632 typically obtained with `find-coding-systems-region'.
633 UNSAFE is a list of coding systems known to be unsafe for this region.
634 REJECTED is a list of coding systems which were safe but for some reason
635 were not recommended in the particular context.
636 DEFAULT is the coding system to use by default in the query."
637 ;; At first, if some defaults are unsafe, record at most 11
638 ;; problematic characters and their positions for them by turning
641 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
644 (mapcar #'(lambda (coding)
647 (mapcar #'(lambda (pos)
648 (cons pos (aref from pos)))
649 (unencodable-char-position
650 0 (length from) coding
652 (mapcar #'(lambda (pos)
653 (cons pos (char-after pos)))
654 (unencodable-char-position
655 from to coding 11)))))
658 ;; Change each safe coding system to the corresponding
659 ;; mime-charset name if it is also a coding system. Such a name
660 ;; is more friendly to users.
664 (setq mime-charset (coding-system-get (car l) :mime-charset))
665 (if (and mime-charset (coding-system-p mime-charset)
666 (coding-system-equal (car l) mime-charset))
667 (setcar l mime-charset))
670 ;; Don't offer variations with locking shift, which you
671 ;; basically never want.
673 (dolist (elt codings (setq codings (nreverse l)))
674 (unless (or (eq 'coding-category-iso-7-else
675 (coding-system-category elt))
676 (eq 'coding-category-iso-8-else
677 (coding-system-category elt)))
680 ;; Remove raw-text, emacs-mule and no-conversion unless nothing
681 ;; else is available.
685 (delq 'no-conversion codings)))
686 '(raw-text emacs-mule no-conversion)))
688 (let ((window-configuration (current-window-configuration))
689 (bufname (buffer-name))
692 ;; If some defaults are unsafe, make sure the offending
693 ;; buffer is displayed.
694 (when (and unsafe (not (stringp from)))
695 (pop-to-buffer bufname)
696 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
698 ;; Then ask users to select one from CODINGS while showing
699 ;; the reason why none of the defaults are not used.
700 (with-output-to-temp-buffer "*Warning*"
701 (with-current-buffer standard-output
702 (if (and (null rejected) (null unsafe))
703 (insert "No default coding systems to try for "
705 (format "string \"%s\"." from)
706 (format "buffer `%s'." bufname)))
708 "These default coding systems were tried to encode"
710 (concat " \"" (if (> (length from) 10)
711 (concat (substring from 0 10) "...\"")
713 (format " text\nin the buffer `%s'" bufname))
717 (dolist (x (append rejected unsafe))
718 (princ " ") (princ (car x)))
720 (fill-region-as-paragraph pos (point)))
722 (insert "These safely encodes the target text,
723 but it is not recommended for encoding text in this context,
724 e.g., for sending an email message.\n ")
726 (princ " ") (princ x))
729 (insert (if rejected "And the others"
730 "However, each of them")
731 " encountered these problematic characters:\n")
732 (dolist (coding unsafe)
733 (insert (format " %s:" (car coding)))
736 #'(lambda (bufname pos)
737 (when (buffer-live-p (get-buffer bufname))
738 (pop-to-buffer bufname)
741 #'(lambda (bufname pos coding)
742 (when (buffer-live-p (get-buffer bufname))
743 (pop-to-buffer bufname)
747 (search-unencodable-char coding)
748 (forward-char -1))))))
749 (dolist (elt (cdr coding))
752 (insert (if (< i 10) (cdr elt) "..."))
758 "mouse-2, RET: jump to this character"
760 'help-args (list bufname (car elt)))
765 "mouse-2, RET: next unencodable character"
767 'help-args (list bufname (car elt)
772 The first problematic character is at point in the displayed buffer,\n"
773 (substitute-command-keys "\
774 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
776 one of the following safe coding systems, or edit the buffer:\n")
780 (princ " ") (princ x))
782 (fill-region-as-paragraph pos (point)))
783 (insert "Or specify any other coding system
784 at the risk of losing the problematic characters.\n")))
786 ;; Read a coding system.
789 (format "Select coding system (default %s): " default)
791 (setq last-coding-system-specified coding-system))
793 (kill-buffer "*Warning*")
794 (set-window-configuration window-configuration)
797 (defun select-safe-coding-system (from to &optional default-coding-system
798 accept-default-p file)
799 "Ask a user to select a safe coding system from candidates.
800 The candidates of coding systems which can safely encode a text
801 between FROM and TO are shown in a popup window. Among them, the most
802 proper one is suggested as the default.
804 The list of `buffer-file-coding-system' of the current buffer,
805 the `default-buffer-file-coding-system', and the
806 most preferred coding system (if it corresponds to a MIME charset) is
807 treated as the default coding system list. Among them, the first one
808 that safely encodes the text is normally selected silently and
809 returned without any user interaction. See also the command
810 `prefer-coding-system'.
812 However, the user is queried if the chosen coding system is
813 inconsistent with what would be selected by `find-auto-coding' from
814 coding cookies &c. if the contents of the region were read from a
815 file. (That could lead to data corruption in a file subsequently
816 re-visited and edited.)
818 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
819 list of coding systems to be prepended to the default coding system
820 list. However, if DEFAULT-CODING-SYSTEM is a list and the first
821 element is t, the cdr part is used as the default coding system list,
822 i.e. `buffer-file-coding-system', `default-buffer-file-coding-system',
823 and the most preferred coding system are not used.
825 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
826 determine the acceptability of the silently selected coding system.
827 It is called with that coding system, and should return nil if it
828 should not be silently selected and thus user interaction is required.
830 Optional 5th arg FILE is the file name to use for this purpose.
831 That is different from `buffer-file-name' when handling `write-region'
834 The variable `select-safe-coding-system-accept-default-p', if
835 non-nil, overrides ACCEPT-DEFAULT-P.
837 Kludgy feature: if FROM is a string, the string is the target text,
839 (if (and default-coding-system
840 (not (listp default-coding-system)))
841 (setq default-coding-system (list default-coding-system)))
843 (let ((no-other-defaults nil)
845 (unless (or (stringp from) find-file-literally)
846 ;; Find an auto-coding that is specified for the the current
847 ;; buffer and file from the region FROM and TO.
852 (setq auto-cs (find-auto-coding (or file buffer-file-name "")
855 (if (coding-system-p (car auto-cs))
856 (setq auto-cs (car auto-cs))
860 Invalid coding system `%s' is specified
861 for the current buffer/file by the %s.
862 It is highly recommended to fix it before writing to a file."
864 (if (eq (cdr auto-cs) :coding) ":coding tag"
865 (format "variable `%s'" (cdr auto-cs)))))
866 (or (yes-or-no-p "Really proceed with writing? ")
867 (error "Save aborted"))
868 (setq auto-cs nil))))))
870 (if (eq (car default-coding-system) t)
871 (setq no-other-defaults t
872 default-coding-system (cdr default-coding-system)))
874 ;; Change elements of the list to (coding . base-coding).
875 (setq default-coding-system
876 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
877 default-coding-system))
879 (if (and auto-cs (not no-other-defaults))
880 ;; If the file has a coding cookie, try to use it before anything
881 ;; else (i.e. before default-coding-system which will typically come
882 ;; from file-coding-system-alist).
883 (let ((base (coding-system-base auto-cs)))
884 (or (memq base '(nil undecided))
885 (rassq base default-coding-system)
886 (push (cons auto-cs base) default-coding-system))))
888 (unless no-other-defaults
889 ;; If buffer-file-coding-system is not nil nor undecided, append it
891 (if buffer-file-coding-system
892 (let ((base (coding-system-base buffer-file-coding-system)))
893 (or (eq base 'undecided)
894 (rassq base default-coding-system)
895 (setq default-coding-system
896 (append default-coding-system
897 (list (cons buffer-file-coding-system base)))))))
899 ;; If default-buffer-file-coding-system is not nil nor undecided,
900 ;; append it to the defaults.
901 (if default-buffer-file-coding-system
902 (let ((base (coding-system-base default-buffer-file-coding-system)))
903 (or (eq base 'undecided)
904 (rassq base default-coding-system)
905 (setq default-coding-system
906 (append default-coding-system
907 (list (cons default-buffer-file-coding-system
910 ;; If the most preferred coding system has the property mime-charset,
911 ;; append it to the defaults.
912 (let ((preferred (coding-system-priority-list t))
914 (and (coding-system-p preferred)
915 (setq base (coding-system-base preferred))
916 (coding-system-get preferred :mime-charset)
917 (not (rassq base default-coding-system))
918 (setq default-coding-system
919 (append default-coding-system
920 (list (cons preferred base)))))))
922 (if select-safe-coding-system-accept-default-p
923 (setq accept-default-p select-safe-coding-system-accept-default-p))
925 ;; Decide the eol-type from the top of the default codings,
926 ;; buffer-file-coding-system, or
927 ;; default-buffer-file-coding-system.
928 (if default-coding-system
929 (let ((default-eol-type (coding-system-eol-type
930 (caar default-coding-system))))
931 (if (and (vectorp default-eol-type) buffer-file-coding-system)
932 (setq default-eol-type (coding-system-eol-type
933 buffer-file-coding-system)))
934 (if (and (vectorp default-eol-type) default-buffer-file-coding-system)
935 (setq default-eol-type (coding-system-eol-type
936 default-buffer-file-coding-system)))
937 (if (and default-eol-type (not (vectorp default-eol-type)))
938 (dolist (elt default-coding-system)
939 (setcar elt (coding-system-change-eol-conversion
940 (car elt) default-eol-type))))))
942 (let ((codings (find-coding-systems-region from to))
944 safe rejected unsafe)
945 (if (eq (car codings) 'undecided)
946 ;; Any coding system is ok.
947 (setq coding-system (caar default-coding-system))
948 ;; Reverse the list so that elements are accumulated in safe,
949 ;; rejected, and unsafe in the correct order.
950 (setq default-coding-system (nreverse default-coding-system))
952 ;; Classify the defaults into safe, rejected, and unsafe.
953 (dolist (elt default-coding-system)
954 (if (or (eq (car codings) 'undecided)
955 (memq (cdr elt) codings))
956 (if (and (functionp accept-default-p)
957 (not (funcall accept-default-p (cdr elt))))
958 (push (car elt) rejected)
959 (push (car elt) safe))
960 (push (car elt) unsafe)))
962 (setq coding-system (car safe))))
964 ;; If all the defaults failed, ask a user.
965 (when (not coding-system)
966 (setq coding-system (select-safe-coding-system-interactively
967 from to codings unsafe rejected (car codings))))
969 ;; Check we're not inconsistent with what `coding:' spec &c would
970 ;; give when file is re-read.
971 ;; But don't do this if we explicitly ignored the cookie
972 ;; by using `find-file-literally'.
976 (memq (coding-system-type coding-system) '(0 5)))))
977 ;; Merge coding-system and auto-cs as far as possible.
978 (if (not coding-system)
979 (setq coding-system auto-cs)
981 (setq auto-cs coding-system)
982 (let ((eol-type-1 (coding-system-eol-type coding-system))
983 (eol-type-2 (coding-system-eol-type auto-cs)))
984 (if (eq (coding-system-base coding-system) 'undecided)
985 (setq coding-system (coding-system-change-text-conversion
986 coding-system auto-cs))
987 (if (eq (coding-system-base auto-cs) 'undecided)
988 (setq auto-cs (coding-system-change-text-conversion
989 auto-cs coding-system))))
990 (if (vectorp eol-type-1)
991 (or (vectorp eol-type-2)
992 (setq coding-system (coding-system-change-eol-conversion
993 coding-system eol-type-2)))
994 (if (vectorp eol-type-2)
995 (setq auto-cs (coding-system-change-eol-conversion
996 auto-cs eol-type-1)))))))
999 ;; Don't barf if writing a compressed file, say.
1000 ;; This check perhaps isn't ideal, but is probably
1001 ;; the best thing to do.
1002 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
1003 (not (coding-system-equal coding-system auto-cs)))
1004 (unless (yes-or-no-p
1005 (format "Selected encoding %s disagrees with \
1006 %s specified by file contents. Really save (else edit coding cookies \
1007 and try again)? " coding-system auto-cs))
1008 (error "Save aborted"))))
1011 (setq select-safe-coding-system-function 'select-safe-coding-system)
1013 (defun select-message-coding-system ()
1014 "Return a coding system to encode the outgoing message of the current buffer.
1015 It at first tries the first coding system found in these variables
1017 (1) local value of `buffer-file-coding-system'
1018 (2) value of `sendmail-coding-system'
1019 (3) value of `default-sendmail-coding-system'
1020 (4) value of `default-buffer-file-coding-system'
1021 If the found coding system can't encode the current buffer,
1022 or none of them are bound to a coding system,
1023 it asks the user to select a proper coding system."
1024 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
1025 buffer-file-coding-system)
1026 sendmail-coding-system
1027 default-sendmail-coding-system
1028 default-buffer-file-coding-system)))
1029 (if (eq coding 'no-conversion)
1030 ;; We should never use no-conversion for outgoing mail.
1032 (if (fboundp select-safe-coding-system-function)
1033 (funcall select-safe-coding-system-function
1034 (point-min) (point-max) coding
1035 (function (lambda (x) (coding-system-get x :mime-charset))))
1038 ;;; Language support stuff.
1040 (defvar language-info-alist nil
1041 "Alist of language environment definitions.
1042 Each element looks like:
1043 (LANGUAGE-NAME . ((KEY . INFO) ...))
1044 where LANGUAGE-NAME is a string, the name of the language environment,
1045 KEY is a symbol denoting the kind of information, and
1046 INFO is the data associated with KEY.
1047 Meaningful values for KEY include
1049 documentation value is documentation of what this language environment
1050 is meant for, and how to use it.
1051 charset value is a list of the character sets mainly used
1052 by this language environment.
1053 sample-text value is an expression which is evalled to generate
1054 a line of text written using characters appropriate
1055 for this language environment.
1056 setup-function value is a function to call to switch to this
1057 language environment.
1058 exit-function value is a function to call to leave this
1059 language environment.
1060 coding-system value is a list of coding systems that are good
1061 for saving text written in this language environment.
1062 This list serves as suggestions to the user;
1063 in effect, as a kind of documentation.
1064 coding-priority value is a list of coding systems for this language
1065 environment, in order of decreasing priority.
1066 This is used to set up the coding system priority
1067 list when you switch to this language environment.
1068 nonascii-translation
1069 value is a charset of dimension one to use for
1070 converting a unibyte character to multibyte
1072 input-method value is a default input method for this language
1074 features value is a list of features requested in this
1075 language environment.
1076 ctext-non-standard-encodings
1077 value is a list of non-standard encoding
1078 names used in extended segments of CTEXT.
1080 `ctext-non-standard-encodings' for more
1083 The following keys take effect only when multibyte characters are
1084 globally disabled, i.e. the value of `default-enable-multibyte-characters'
1087 unibyte-display value is a coding system to encode characters
1088 for the terminal. Characters in the range
1089 of 160 to 255 display not as octal escapes,
1090 but as non-ASCII characters in this language
1093 (defun get-language-info (lang-env key)
1094 "Return information listed under KEY for language environment LANG-ENV.
1095 KEY is a symbol denoting the kind of information.
1096 For a list of useful values for KEY and their meanings,
1097 see `language-info-alist'."
1098 (if (symbolp lang-env)
1099 (setq lang-env (symbol-name lang-env)))
1100 (let ((lang-slot (assoc-string lang-env language-info-alist t)))
1102 (cdr (assq key (cdr lang-slot))))))
1104 (defun set-language-info (lang-env key info)
1105 "Modify part of the definition of language environment LANG-ENV.
1106 Specifically, this stores the information INFO under KEY
1107 in the definition of this language environment.
1108 KEY is a symbol denoting the kind of information.
1109 INFO is the value for that information.
1111 For a list of useful values for KEY and their meanings,
1112 see `language-info-alist'."
1113 (if (symbolp lang-env)
1114 (setq lang-env (symbol-name lang-env)))
1115 (set-language-info-internal lang-env key info)
1116 (if (equal lang-env current-language-environment)
1117 (cond ((eq key 'coding-priority)
1118 (set-language-environment-coding-systems lang-env)
1119 (set-language-environment-charset lang-env))
1120 ((eq key 'input-method)
1121 (set-language-environment-input-method lang-env))
1122 ((eq key 'nonascii-translation)
1123 (set-language-environment-nonascii-translation lang-env))
1125 (set-language-environment-charset lang-env))
1126 ((and (not default-enable-multibyte-characters)
1127 (or (eq key 'unibyte-syntax) (eq key 'unibyte-display)))
1128 (set-language-environment-unibyte lang-env)))))
1130 (defun set-language-info-internal (lang-env key info)
1132 Arguments are the same as `set-language-info'."
1133 (let (lang-slot key-slot)
1134 (setq lang-slot (assoc lang-env language-info-alist))
1135 (if (null lang-slot) ; If no slot for the language, add it.
1136 (setq lang-slot (list lang-env)
1137 language-info-alist (cons lang-slot language-info-alist)))
1138 (setq key-slot (assq key lang-slot))
1139 (if (null key-slot) ; If no slot for the key, add it.
1141 (setq key-slot (list key))
1142 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
1143 (setcdr key-slot (purecopy info))
1144 ;; Update the custom-type of `current-language-environment'.
1145 (put 'current-language-environment 'custom-type
1146 (cons 'choice (mapcar
1149 (sort (mapcar 'car language-info-alist) 'string<))))))
1151 (defun set-language-info-alist (lang-env alist &optional parents)
1152 "Store ALIST as the definition of language environment LANG-ENV.
1153 ALIST is an alist of KEY and INFO values. See the documentation of
1154 `language-info-alist' for the meanings of KEY and INFO.
1156 Optional arg PARENTS is a list of parent menu names; it specifies
1157 where to put this language environment in the
1158 Describe Language Environment and Set Language Environment menus.
1159 For example, (\"European\") means to put this language environment
1160 in the European submenu in each of those two menus."
1161 (if (symbolp lang-env)
1162 (setq lang-env (symbol-name lang-env)))
1163 (let ((describe-map describe-language-environment-map)
1164 (setup-map setup-language-environment-map))
1167 map parent-symbol parent prompt)
1169 (if (symbolp (setq parent-symbol (car l)))
1170 (setq parent (symbol-name parent))
1171 (setq parent parent-symbol parent-symbol (intern parent)))
1172 (setq map (lookup-key describe-map (vector parent-symbol)))
1173 ;; This prompt string is for define-prefix-command, so
1174 ;; that the map it creates will be suitable for a menu.
1175 (or map (setq prompt (format "%s Environment" parent)))
1178 (setq map (intern (format "describe-%s-environment-map"
1179 (downcase parent))))
1180 (define-prefix-command map nil prompt)
1181 (define-key-after describe-map (vector parent-symbol)
1182 (cons parent map) t)))
1183 (setq describe-map (symbol-value map))
1184 (setq map (lookup-key setup-map (vector parent-symbol)))
1187 (setq map (intern (format "setup-%s-environment-map"
1188 (downcase parent))))
1189 (define-prefix-command map nil prompt)
1190 (define-key-after setup-map (vector parent-symbol)
1191 (cons parent map) t)))
1192 (setq setup-map (symbol-value map))
1195 ;; Set up menu items for this language env.
1196 (let ((doc (assq 'documentation alist)))
1198 (define-key-after describe-map (vector (intern lang-env))
1199 (cons lang-env 'describe-specified-language-support) t)))
1200 (define-key-after setup-map (vector (intern lang-env))
1201 (cons lang-env 'setup-specified-language-environment) t)
1204 (set-language-info-internal lang-env (car elt) (cdr elt)))
1206 (if (equal lang-env current-language-environment)
1207 (set-language-environment lang-env))))
1209 (defun read-language-name (key prompt &optional default)
1210 "Read a language environment name which has information for KEY.
1211 If KEY is nil, read any language environment.
1212 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1213 This returns a language environment name as a string."
1214 (let* ((completion-ignore-case t)
1215 (name (completing-read prompt
1218 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1219 t nil nil default)))
1220 (if (and (> (length name) 0)
1222 (get-language-info name key)))
1225 ;;; Multilingual input methods.
1227 "LEIM: Libraries of Emacs Input Methods."
1230 (defconst leim-list-file-name "leim-list.el"
1231 "Name of LEIM list file.
1232 This file contains a list of libraries of Emacs input methods (LEIM)
1233 in the format of Lisp expression for registering each input method.
1234 Emacs loads this file at startup time.")
1236 (defvar leim-list-header (format
1237 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1239 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1240 ;; methods in the same directory as this file. Loading this file
1241 ;; registers all the input methods in Emacs.
1243 ;; Each entry has the form:
1244 ;; (register-input-method
1245 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1246 ;; TITLE DESCRIPTION
1248 ;; See the function `register-input-method' for the meanings of the arguments.
1250 ;; If this directory is included in load-path, Emacs automatically
1251 ;; loads this file at startup time.
1254 leim-list-file-name)
1255 "Header to be inserted in LEIM list file.")
1257 (defvar leim-list-entry-regexp "^(register-input-method"
1258 "Regexp matching head of each entry in LEIM list file.
1259 See also the variable `leim-list-header'")
1261 (defvar update-leim-list-functions
1262 '(quail-update-leim-list-file)
1263 "List of functions to call to update LEIM list file.
1264 Each function is called with one arg, LEIM directory name.")
1266 (defun update-leim-list-file (&rest dirs)
1267 "Update LEIM list file in directories DIRS."
1268 (let ((functions update-leim-list-functions))
1270 (apply (car functions) dirs)
1271 (setq functions (cdr functions)))))
1273 (defvar current-input-method nil
1274 "The current input method for multilingual text.
1275 If nil, that means no input method is activated now.")
1276 (make-variable-buffer-local 'current-input-method)
1277 (put 'current-input-method 'permanent-local t)
1279 (defvar current-input-method-title nil
1280 "Title string of the current input method shown in mode line.")
1281 (make-variable-buffer-local 'current-input-method-title)
1282 (put 'current-input-method-title 'permanent-local t)
1284 (defcustom default-input-method nil
1285 "*Default input method for multilingual text (a string).
1286 This is the input method activated automatically by the command
1287 `toggle-input-method' (\\[toggle-input-method])."
1288 :link '(custom-manual "(emacs)Input Methods")
1290 :type '(choice (const nil) (string
1291 :completion-ignore-case t
1292 :complete-function widget-string-complete
1293 :completion-alist input-method-alist
1294 :prompt-history input-method-history))
1295 :set-after '(current-language-environment))
1297 (put 'input-method-function 'permanent-local t)
1299 (defvar input-method-history nil
1300 "History list for some commands that read input methods.")
1301 (make-variable-buffer-local 'input-method-history)
1302 (put 'input-method-history 'permanent-local t)
1304 (defvar inactivate-current-input-method-function nil
1305 "Function to call for inactivating the current input method.
1306 Every input method should set this to an appropriate value when activated.
1307 This function is called with no argument.
1309 This function should never change the value of `current-input-method'.
1310 It is set to nil by the function `inactivate-input-method'.")
1311 (make-variable-buffer-local 'inactivate-current-input-method-function)
1312 (put 'inactivate-current-input-method-function 'permanent-local t)
1314 (defvar describe-current-input-method-function nil
1315 "Function to call for describing the current input method.
1316 This function is called with no argument.")
1317 (make-variable-buffer-local 'describe-current-input-method-function)
1318 (put 'describe-current-input-method-function 'permanent-local t)
1320 (defvar input-method-alist nil
1321 "Alist of input method names vs how to use them.
1322 Each element has the form:
1323 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1324 See the function `register-input-method' for the meanings of the elements.")
1326 (defun register-input-method (input-method lang-env &rest args)
1327 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1329 INPUT-METHOD and LANG-ENV are symbols or strings.
1330 ACTIVATE-FUNC is a function to call to activate this method.
1331 TITLE is a string to show in the mode line when this method is active.
1332 DESCRIPTION is a string describing this method and what it is good for.
1333 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1334 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1336 This function is mainly used in the file \"leim-list.el\" which is
1337 created at Emacs build time, registering all Quail input methods
1338 contained in the Emacs distribution.
1340 In case you want to register a new Quail input method by yourself, be
1341 careful to use the same input method title as given in the third
1342 parameter of `quail-define-package'. (If the values are different, the
1343 string specified in this function takes precedence.)
1345 The commands `describe-input-method' and `list-input-methods' need
1346 these duplicated values to show some information about input methods
1347 without loading the relevant Quail packages.
1348 \n(fn INPUT-METHOD LANG-ENV ACTIVATE-FUNC TITLE DESCRIPTION &rest ARGS)"
1349 (if (symbolp lang-env)
1350 (setq lang-env (symbol-name lang-env)))
1351 (if (symbolp input-method)
1352 (setq input-method (symbol-name input-method)))
1353 (let ((info (cons lang-env args))
1354 (slot (assoc input-method input-method-alist)))
1357 (setq slot (cons input-method info))
1358 (setq input-method-alist (cons slot input-method-alist)))))
1360 (defun read-input-method-name (prompt &optional default inhibit-null)
1361 "Read a name of input method from a minibuffer prompting with PROMPT.
1362 If DEFAULT is non-nil, use that as the default,
1363 and substitute it into PROMPT at the first `%s'.
1364 If INHIBIT-NULL is non-nil, null input signals an error.
1366 The return value is a string."
1368 (setq prompt (format prompt default)))
1369 (let* ((completion-ignore-case t)
1370 ;; As it is quite normal to change input method in the
1371 ;; minibuffer, we must enable it even if
1372 ;; enable-recursive-minibuffers is currently nil.
1373 (enable-recursive-minibuffers t)
1374 ;; This binding is necessary because input-method-history is
1376 (input-method (completing-read prompt input-method-alist
1377 nil t nil 'input-method-history
1379 (if (and input-method (symbolp input-method))
1380 (setq input-method (symbol-name input-method)))
1381 (if (> (length input-method) 0)
1384 (error "No valid input method is specified")))))
1386 (defun activate-input-method (input-method)
1387 "Switch to input method INPUT-METHOD for the current buffer.
1388 If some other input method is already active, turn it off first.
1389 If INPUT-METHOD is nil, deactivate any current input method."
1390 (if (and input-method (symbolp input-method))
1391 (setq input-method (symbol-name input-method)))
1392 (if (and current-input-method
1393 (not (string= current-input-method input-method)))
1394 (inactivate-input-method))
1395 (unless (or current-input-method (null input-method))
1396 (let ((slot (assoc input-method input-method-alist)))
1398 (error "Can't activate input method `%s'" input-method))
1399 (setq current-input-method-title nil)
1400 (let ((func (nth 2 slot)))
1401 (if (functionp func)
1402 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1403 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1405 (require (cdr func))
1406 (apply (car func) input-method (nthcdr 5 slot)))
1407 (error "Can't activate input method `%s'" input-method))))
1408 (setq current-input-method input-method)
1409 (or (stringp current-input-method-title)
1410 (setq current-input-method-title (nth 3 slot)))
1412 (run-hooks 'input-method-activate-hook)
1413 (force-mode-line-update)))))
1415 (defun inactivate-input-method ()
1416 "Turn off the current input method."
1417 (when current-input-method
1418 (if input-method-history
1419 (unless (string= current-input-method (car input-method-history))
1420 (setq input-method-history
1421 (cons current-input-method
1422 (delete current-input-method input-method-history))))
1423 (setq input-method-history (list current-input-method)))
1425 (funcall inactivate-current-input-method-function)
1427 (run-hooks 'input-method-inactivate-hook)
1428 (setq current-input-method nil
1429 input-method-function nil
1430 current-input-method-title nil)
1431 (force-mode-line-update)))))
1433 (defun set-input-method (input-method &optional interactive)
1434 "Select and activate input method INPUT-METHOD for the current buffer.
1435 This also sets the default input method to the one you specify.
1436 If INPUT-METHOD is nil, this function turns off the input method, and
1437 also causes you to be prompted for a name of an input method the next
1438 time you invoke \\[toggle-input-method].
1439 When called interactively, the optional arg INTERACTIVE is non-nil,
1440 which marks the variable `default-input-method' as set for Custom buffers.
1442 To deactivate the input method interactively, use \\[toggle-input-method].
1443 To deactivate it programmatically, use \\[inactivate-input-method]."
1445 (let* ((default (or (car input-method-history) default-input-method)))
1446 (list (read-input-method-name
1447 (if default "Select input method (default %s): " "Select input method: ")
1450 (activate-input-method input-method)
1451 (setq default-input-method input-method)
1453 (customize-mark-as-set 'default-input-method))
1454 default-input-method)
1456 (defun toggle-input-method (&optional arg interactive)
1457 "Enable or disable multilingual text input method for the current buffer.
1458 Only one input method can be enabled at any time in a given buffer.
1460 The normal action is to enable an input method if none was
1461 enabled, and disable the current one otherwise. Which input method
1462 to enable can be determined in various ways--either the one most
1463 recently used, or the one specified by `default-input-method', or
1464 as a last resort by reading the name of an input method in the
1467 With a prefix argument, read an input method name with the minibuffer
1468 and enable that one. The default is the most recent input method specified
1469 \(not including the currently active input method, if any).
1471 When called interactively, the optional arg INTERACTIVE is non-nil,
1472 which marks the variable `default-input-method' as set for Custom buffers."
1474 (interactive "P\np")
1475 (if (and current-input-method (not arg))
1476 (inactivate-input-method)
1477 (let ((default (or (car input-method-history) default-input-method)))
1478 (if (and arg default (equal current-input-method default)
1479 (> (length input-method-history) 1))
1480 (setq default (nth 1 input-method-history)))
1481 (activate-input-method
1482 (if (or arg (not default))
1484 (read-input-method-name
1485 (if default "Input method (default %s): " "Input method: " )
1488 (unless default-input-method
1490 (setq default-input-method current-input-method)
1492 (customize-mark-as-set 'default-input-method)))))))
1494 (eval-when-compile (autoload 'help-buffer "help-mode"))
1496 (defun describe-input-method (input-method)
1497 "Describe input method INPUT-METHOD."
1499 (list (read-input-method-name
1500 "Describe input method (default current choice): ")))
1501 (if (and input-method (symbolp input-method))
1502 (setq input-method (symbol-name input-method)))
1503 (help-setup-xref (list #'describe-input-method
1504 (or input-method current-input-method))
1507 (if (null input-method)
1508 (describe-current-input-method)
1509 (let ((current current-input-method))
1513 (activate-input-method input-method)
1514 (describe-current-input-method))
1515 (activate-input-method current))
1517 (activate-input-method current)
1518 (help-setup-xref (list #'describe-input-method input-method)
1520 (with-output-to-temp-buffer (help-buffer)
1521 (let ((elt (assoc input-method input-method-alist)))
1523 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1524 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1526 (defun describe-current-input-method ()
1527 "Describe the input method currently in use.
1528 This is a subroutine for `describe-input-method'."
1529 (if current-input-method
1530 (if (and (symbolp describe-current-input-method-function)
1531 (fboundp describe-current-input-method-function))
1532 (funcall describe-current-input-method-function)
1533 (message "No way to describe the current input method `%s'"
1534 current-input-method)
1536 (error "No input method is activated now")))
1538 (defun read-multilingual-string (prompt &optional initial-input input-method)
1539 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1540 The input method selected last time is activated in minibuffer.
1541 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1543 Optional 3rd argument INPUT-METHOD specifies the input method
1544 to be activated instead of the one selected last time. It is a symbol
1548 current-input-method
1549 default-input-method
1550 (read-input-method-name "Input method: " nil t)))
1551 (if (and input-method (symbolp input-method))
1552 (setq input-method (symbol-name input-method)))
1553 (let ((prev-input-method current-input-method))
1556 (activate-input-method input-method)
1557 (read-string prompt initial-input nil nil t))
1558 (activate-input-method prev-input-method))))
1560 ;; Variables to control behavior of input methods. All input methods
1561 ;; should react to these variables.
1563 (defcustom input-method-verbose-flag 'default
1564 "*A flag to control extra guidance given by input methods.
1565 The value should be nil, t, `complex-only', or `default'.
1567 The extra guidance is done by showing list of available keys in echo
1568 area. When you use the input method in the minibuffer, the guidance
1569 is shown at the bottom short window (split from the existing window).
1571 If the value is t, extra guidance is always given, if the value is
1572 nil, extra guidance is always suppressed.
1574 If the value is `complex-only', only complex input methods such as
1575 `chinese-py' and `japanese' give extra guidance.
1577 If the value is `default', complex input methods always give extra
1578 guidance, but simple input methods give it only when you are not in
1581 See also the variable `input-method-highlight-flag'."
1582 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1583 (const complex-only) (const default))
1586 (defcustom input-method-highlight-flag t
1587 "*If this flag is non-nil, input methods highlight partially-entered text.
1588 For instance, while you are in the middle of a Quail input method sequence,
1589 the text inserted so far is temporarily underlined.
1590 The underlining goes away when you finish or abort the input method sequence.
1591 See also the variable `input-method-verbose-flag'."
1595 (defcustom input-method-activate-hook nil
1596 "Normal hook run just after an input method is activated.
1598 The variable `current-input-method' keeps the input method name
1603 (defcustom input-method-inactivate-hook nil
1604 "Normal hook run just after an input method is inactivated.
1606 The variable `current-input-method' still keeps the input method name
1611 (defcustom input-method-after-insert-chunk-hook nil
1612 "Normal hook run just after an input method insert some chunk of text."
1616 (defvar input-method-exit-on-first-char nil
1617 "This flag controls when an input method returns.
1618 Usually, the input method does not return while there's a possibility
1619 that it may find a different translation if a user types another key.
1620 But, it this flag is non-nil, the input method returns as soon as
1621 the current key sequence gets long enough to have some valid translation.")
1623 (defcustom input-method-use-echo-area nil
1624 "This flag controls how an input method shows an intermediate key sequence.
1625 Usually, the input method inserts the intermediate key sequence,
1626 or candidate translations corresponding to the sequence,
1627 at point in the current buffer.
1628 But, if this flag is non-nil, it displays them in echo area instead."
1632 (defvar input-method-exit-on-invalid-key nil
1633 "This flag controls the behavior of an input method on invalid key input.
1634 Usually, when a user types a key which doesn't start any character
1635 handled by the input method, the key is handled by turning off the
1636 input method temporarily. After that key, the input method is re-enabled.
1637 But, if this flag is non-nil, the input method is never back on.")
1640 (defcustom set-language-environment-hook nil
1641 "Normal hook run after some language environment is set.
1643 When you set some hook function here, that effect usually should not
1644 be inherited to another language environment. So, you had better set
1645 another function in `exit-language-environment-hook' (which see) to
1650 (defcustom exit-language-environment-hook nil
1651 "Normal hook run after exiting from some language environment.
1652 When this hook is run, the variable `current-language-environment'
1653 is still bound to the language environment being exited.
1655 This hook is mainly used for canceling the effect of
1656 `set-language-environment-hook' (which-see)."
1660 (put 'setup-specified-language-environment 'apropos-inhibit t)
1662 (defun setup-specified-language-environment ()
1663 "Switch to a specified language environment."
1665 (let (language-name)
1666 (if (and (symbolp last-command-event)
1667 (or (not (eq last-command-event 'Default))
1668 (setq last-command-event 'English))
1669 (setq language-name (symbol-name last-command-event)))
1671 (set-language-environment language-name)
1672 (customize-mark-as-set 'current-language-environment))
1673 (error "Bogus calling sequence"))))
1675 (defcustom current-language-environment "English"
1676 "The last language environment specified with `set-language-environment'.
1677 This variable should be set only with \\[customize], which is equivalent
1678 to using the function `set-language-environment'."
1679 :link '(custom-manual "(emacs)Language Environments")
1680 :set (lambda (symbol value) (set-language-environment value))
1682 (or (car-safe (assoc-string
1683 (if (symbolp current-language-environment)
1684 (symbol-name current-language-environment)
1685 current-language-environment)
1686 language-info-alist t))
1688 ;; custom type will be updated with `set-language-info'.
1689 :type (if language-info-alist
1690 (cons 'choice (mapcar
1693 (sort (mapcar 'car language-info-alist) 'string<)))
1695 :initialize 'custom-initialize-default
1698 (defun reset-language-environment ()
1699 "Reset multilingual environment of Emacs to the default status.
1701 The default status is as follows:
1703 The default value of `buffer-file-coding-system' is nil.
1704 The default coding system for process I/O is nil.
1705 The default value for the command `set-terminal-coding-system' is nil.
1706 The default value for the command `set-keyboard-coding-system' is nil.
1708 The order of priorities of coding systems are as follows:
1717 ;; This function formerly set default-enable-multibyte-characters to t,
1718 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1720 (set-coding-system-priority
1729 (set-default-coding-systems nil)
1730 (setq default-sendmail-coding-system 'iso-latin-1)
1731 ;; On Darwin systems, this should be utf-8, but when this file is loaded
1732 ;; utf-8 is not yet defined, so we set it in set-locale-environment instead.
1733 (setq default-file-name-coding-system 'iso-latin-1)
1734 ;; Preserve eol-type from existing default-process-coding-systems.
1735 ;; On non-unix-like systems in particular, these may have been set
1736 ;; carefully by the user, or by the startup code, to deal with the
1737 ;; users shell appropriately, so should not be altered by changing
1738 ;; language environment.
1739 (let ((output-coding
1740 ;; When bootstrapping, coding-systems are not defined yet, so
1741 ;; we need to catch the error from check-coding-system.
1743 (coding-system-change-text-conversion
1744 (car default-process-coding-system) 'undecided)
1745 (coding-system-error 'undecided)))
1748 (coding-system-change-text-conversion
1749 (cdr default-process-coding-system) 'iso-latin-1)
1750 (coding-system-error 'iso-latin-1))))
1751 (setq default-process-coding-system
1752 (cons output-coding input-coding)))
1754 ;; Don't alter the terminal and keyboard coding systems here.
1755 ;; The terminal still supports the same coding system
1756 ;; that it supported a minute ago.
1757 ;; (set-terminal-coding-system-internal nil)
1758 ;; (set-keyboard-coding-system-internal nil)
1760 (set-unibyte-charset 'iso-8859-1))
1762 (reset-language-environment)
1764 (defun set-display-table-and-terminal-coding-system (language-name &optional coding-system)
1765 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1766 (let ((coding (get-language-info language-name 'unibyte-display)))
1768 (or (not coding-system)
1769 (coding-system-equal coding coding-system)))
1770 (standard-display-european-internal)
1771 ;; The following 2 lines undo the 8-bit display that we set up
1772 ;; in standard-display-european-internal, which see. This is in
1773 ;; case the user has used standard-display-european earlier in
1774 ;; this session. (The MS-DOS port doesn't use that setup, so it
1775 ;; doesn't need to undo it.)
1776 (when standard-display-table
1778 (aset standard-display-table (+ i 128) nil))))
1779 (or (eq window-system 'pc)
1780 (set-terminal-coding-system (or coding-system coding)))))
1782 (defun set-language-environment (language-name)
1783 "Set up multi-lingual environment for using LANGUAGE-NAME.
1784 This sets the coding system priority and the default input method
1785 and sometimes other things. LANGUAGE-NAME should be a string
1786 which is the name of a language environment. For example, \"Latin-1\"
1787 specifies the character set for the major languages of Western Europe."
1788 (interactive (list (read-language-name
1790 "Set language environment (default English): ")))
1792 (if (symbolp language-name)
1793 (setq language-name (symbol-name language-name)))
1794 (setq language-name "English"))
1795 (let ((slot (assoc-string language-name language-info-alist t)))
1797 (error "Language environment not defined: %S" language-name))
1798 (setq language-name (car slot)))
1799 (if current-language-environment
1800 (let ((func (get-language-info current-language-environment
1802 (run-hooks 'exit-language-environment-hook)
1803 (if (functionp func) (funcall func))))
1805 (reset-language-environment)
1806 ;; The features might set up coding systems.
1807 (let ((required-features (get-language-info language-name 'features)))
1808 (while required-features
1809 (require (car required-features))
1810 (setq required-features (cdr required-features))))
1812 (setq current-language-environment language-name)
1814 (set-language-environment-coding-systems language-name)
1815 (set-language-environment-input-method language-name)
1816 (set-language-environment-nonascii-translation language-name)
1817 (set-language-environment-charset language-name)
1818 ;; Unibyte setups if necessary.
1819 (unless default-enable-multibyte-characters
1820 (set-language-environment-unibyte language-name))
1822 (let ((func (get-language-info language-name 'setup-function)))
1823 (if (functionp func)
1826 (run-hooks 'set-language-environment-hook)
1827 (force-mode-line-update t))
1829 (define-widget 'charset 'symbol
1832 :complete-function (lambda ()
1834 (lisp-complete-symbol 'charsetp))
1835 :completion-ignore-case t
1837 :validate (lambda (widget)
1838 (unless (charsetp (widget-value widget))
1839 (widget-put widget :error (format "Invalid charset: %S"
1840 (widget-value widget)))
1842 :prompt-history 'charset-history)
1844 (defcustom language-info-custom-alist nil
1845 "Customizations of language environment parameters.
1846 Value is an alist with elements like those of `language-info-alist'.
1847 These are used to set values in `language-info-alist' which replace
1848 the defaults. A typical use is replacing the default input method for
1849 the environment. Use \\[describe-language-environment] to find the environment's settings.
1851 This option is intended for use at startup. Removing items doesn't
1852 remove them from the language info until you next restart Emacs.
1854 Setting this variable directly does not take effect. See
1855 `set-language-info-alist' for use in programs."
1859 (custom-set-default s v)
1860 ;; Can't do this before language environments are set up.
1862 ;; modify language-info-alist
1864 (set-language-info-alist (car elt) (cdr elt)))
1865 ;; re-set the environment in case its parameters changed
1866 (set-language-environment current-language-environment)))
1868 :key-type (string :tag "Language environment"
1869 :completion-ignore-case t
1870 :complete-function widget-string-complete
1871 :completion-alist language-info-alist)
1873 (alist :key-type symbol
1874 :options ((documentation string)
1875 (charset (repeat charset))
1876 (sample-text string)
1877 (setup-function function)
1878 (exit-function function)
1879 (coding-system (repeat coding-system))
1880 (coding-priority (repeat coding-system))
1881 (nonascii-translation charset)
1884 :completion-ignore-case t
1885 :complete-function widget-string-complete
1886 :completion-alist input-method-alist
1887 :prompt-history input-method-history))
1888 (features (repeat symbol))
1889 (unibyte-display coding-system)))))
1891 (defun standard-display-european-internal ()
1892 ;; Actually set up direct output of non-ASCII characters.
1893 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1894 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1895 ;; the native font, and codes 160 and 146 stand for something very
1897 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1899 ;; Most X fonts used to do the wrong thing for latin-1 code 160.
1900 (unless (and (eq window-system 'x)
1901 ;; XFree86 4 has fixed the fonts.
1902 (string= "The XFree86 Project, Inc" (x-server-vendor))
1903 (> (aref (number-to-string (nth 2 (x-server-version))) 0)
1905 ;; Make non-line-break space display as a plain space.
1906 (aset standard-display-table 160 [32]))
1907 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1908 ;; don't contain a character at that position. Map it to the ASCII
1909 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1910 ;; U+2019, normally from the windows-1252 character set. XFree 4
1911 ;; fonts probably have the appropriate glyph at this position,
1912 ;; so they could use standard-display-8bit. It's better to use a
1913 ;; proper windows-1252 coding system. --fx]
1914 (aset standard-display-table 146 [39]))))
1916 (defun set-language-environment-coding-systems (language-name)
1917 "Do various coding system setups for language environment LANGUAGE-NAME."
1918 (let* ((priority (get-language-info language-name 'coding-priority))
1919 (default-coding (car priority))
1920 (eol-type (coding-system-eol-type default-buffer-file-coding-system)))
1922 (set-default-coding-systems
1923 (if (memq eol-type '(0 1 2 unix dos mac))
1924 (coding-system-change-eol-conversion default-coding eol-type)
1926 (setq default-sendmail-coding-system default-coding)
1927 (apply 'set-coding-system-priority priority))))
1929 (defun set-language-environment-input-method (language-name)
1930 "Do various input method setups for language environment LANGUAGE-NAME."
1931 (let ((input-method (get-language-info language-name 'input-method)))
1933 (setq default-input-method input-method)
1934 (if input-method-history
1935 (setq input-method-history
1937 (delete input-method input-method-history)))))))
1939 (defun set-language-environment-nonascii-translation (language-name)
1940 "Do unibyte/multibyte translation setup for language environment LANGUAGE-NAME."
1941 ;; Note: For DOS, we assumed that the charset cpXXX is already
1943 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
1944 (if (eq window-system 'pc)
1945 (setq nonascii (intern "cp%d" dos-codepage)))
1946 (or (and (charsetp nonascii)
1947 (get-charset-property nonascii :ascii-compatible-p))
1948 (setq nonascii 'iso-8859-1))
1949 (set-unibyte-charset nonascii)))
1951 (defun set-language-environment-charset (language-name)
1952 "Do various charset setups for language environment LANGUAGE-NAME."
1953 ;; Put higher priorities to such charsets that are supported by the
1954 ;; coding systems of higher priorities in this environment.
1955 (let ((charsets (get-language-info language-name 'charset)))
1956 (dolist (coding (get-language-info language-name 'coding-priority))
1957 (setq charsets (append charsets (coding-system-charset-list coding))))
1959 (apply 'set-charset-priority charsets))))
1961 (defun set-language-environment-unibyte (language-name)
1962 "Do various unibyte-mode setups for language environment LANGUAGE-NAME."
1963 (set-display-table-and-terminal-coding-system language-name))
1965 (defsubst princ-list (&rest args)
1966 "Print all arguments with `princ', then print \"\n\"."
1967 (while args (princ (car args)) (setq args (cdr args)))
1970 (put 'describe-specified-language-support 'apropos-inhibit t)
1972 ;; Print language-specific information such as input methods,
1973 ;; charsets, and coding systems. This function is intended to be
1974 ;; called from the menu:
1975 ;; [menu-bar mule describe-language-environment LANGUAGE]
1976 ;; and should not run it by `M-x describe-current-input-method-function'.
1977 (defun describe-specified-language-support ()
1978 "Describe how Emacs supports the specified language environment."
1980 (let (language-name)
1981 (if (not (and (symbolp last-command-event)
1982 (or (not (eq last-command-event 'Default))
1983 (setq last-command-event 'English))
1984 (setq language-name (symbol-name last-command-event))))
1985 (error "Bogus calling sequence"))
1986 (describe-language-environment language-name)))
1988 (defun describe-language-environment (language-name)
1989 "Describe how Emacs supports language environment LANGUAGE-NAME."
1991 (list (read-language-name
1993 "Describe language environment (default current choice): ")))
1994 (if (null language-name)
1995 (setq language-name current-language-environment))
1996 (if (or (null language-name)
1997 (null (get-language-info language-name 'documentation)))
1998 (error "No documentation for the specified language"))
1999 (if (symbolp language-name)
2000 (setq language-name (symbol-name language-name)))
2001 (dolist (feature (get-language-info language-name 'features))
2003 (let ((doc (get-language-info language-name 'documentation)))
2004 (help-setup-xref (list #'describe-language-environment language-name)
2006 (with-output-to-temp-buffer (help-buffer)
2008 (set-buffer standard-output)
2009 (insert language-name " language environment\n\n")
2011 (insert doc "\n\n"))
2013 (let ((str (eval (get-language-info language-name 'sample-text))))
2015 (insert "Sample text:\n " str "\n\n")))
2017 (let ((input-method (get-language-info language-name 'input-method))
2018 (l (copy-sequence input-method-alist)))
2019 (insert "Input methods")
2021 (insert " (default " input-method ")")
2022 (setq input-method (assoc input-method input-method-alist))
2023 (setq l (cons input-method (delete input-method l))))
2026 (when (eq t (compare-strings language-name nil nil
2027 (nth 1 (car l)) nil nil t))
2028 (insert " " (car (car l)))
2029 (search-backward (car (car l)))
2030 (help-xref-button 0 'help-input-method (car (car l)))
2031 (goto-char (point-max))
2033 (if (stringp (nth 3 (car l)))
2035 (car (nth 3 (car l))))
2036 "\" in mode line)\n"))
2039 (insert "Character sets:\n")
2040 (let ((l (get-language-info language-name 'charset)))
2042 (insert " nothing specific to " language-name "\n")
2044 (insert " " (symbol-name (car l)))
2045 (search-backward (symbol-name (car l)))
2046 (help-xref-button 0 'help-character-set (car l))
2047 (goto-char (point-max))
2048 (insert ": " (charset-description (car l)) "\n")
2051 (insert "Coding systems:\n")
2052 (let ((l (get-language-info language-name 'coding-system)))
2054 (insert " nothing specific to " language-name "\n")
2056 (insert " " (symbol-name (car l)))
2057 (search-backward (symbol-name (car l)))
2058 (help-xref-button 0 'help-coding-system (car l))
2059 (goto-char (point-max))
2061 (coding-system-mnemonic (car l))
2062 "' in mode line):\n\t"
2063 (coding-system-doc-string (car l))
2065 (let ((aliases (coding-system-aliases (car l))))
2067 (insert "\t(alias:")
2069 (insert " " (symbol-name (car aliases)))
2070 (setq aliases (cdr aliases)))
2072 (setq l (cdr l)))))))))
2076 (defvar locale-translation-file-name nil
2077 "File name for the system's file of locale-name aliases, or nil if none.")
2079 ;; The following definitions might as well be marked as constants and
2080 ;; purecopied, since they're normally used on startup, and probably
2081 ;; should reflect the facilities of the base Emacs.
2082 (defconst locale-language-names
2085 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
2086 ;; as specified in the Single Unix Spec, Version 2.
2087 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
2088 ;; with additions from ISO 639/RA Newsletter No.1/1989;
2089 ;; see Internet RFC 2165 (1997-06) and
2090 ;; http://www.evertype.com/standards/iso639/iso639-en.html
2091 ;; TERRITORY is a country code taken from ISO 3166
2092 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
2093 ;; CODESET and MODIFIER are implementation-dependent.
2095 ;; jasonr comments: MS Windows uses three letter codes for
2096 ;; languages instead of the two letter ISO codes that POSIX
2097 ;; uses. In most cases the first two letters are the same, so
2098 ;; most of the regexps in locale-language-names work. Japanese
2099 ;; and Chinese are exceptions, which are listed in the
2100 ;; non-standard section at the bottom of locale-language-names.
2102 ("aa_DJ" . "Latin-1") ; Afar
2105 ("af" . "Latin-1") ; Afrikaans
2106 ("am" "Ethiopic" utf-8) ; Amharic
2107 ("an" . "Latin-9") ; Aragonese
2108 ; ar Arabic glibc uses 8859-6
2111 ("az" . "UTF-8") ; Azerbaijani
2113 ("be" "Belarusian" cp1251) ; Belarusian [Byelorussian until early 1990s]
2114 ("bg" "Bulgarian" cp1251) ; Bulgarian
2117 ("bn" . "UTF-8") ; Bengali, Bangla
2119 ("br" . "Latin-1") ; Breton
2120 ("bs" . "Latin-2") ; Bosnian
2121 ("byn" . "UTF-8") ; Bilin; Blin
2122 ("ca" . "Latin-1") ; Catalan
2124 ("cs" "Czech" iso-8859-2)
2125 ("cy" "Welsh" iso-8859-14)
2126 ("da" . "Latin-1") ; Danish
2127 ("de" "German" iso-8859-1)
2130 ("el" "Greek" iso-8859-7)
2131 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
2132 ;; That's actually what the GNU locales define, modulo things like
2134 ("en_IN" "English" utf-8) ; glibc uses utf-8 for English in India
2135 ("en" "English" iso-8859-1) ; English
2136 ("eo" . "Latin-3") ; Esperanto
2137 ("es" "Spanish" iso-8859-1)
2138 ("et" . "Latin-1") ; Estonian
2139 ("eu" . "Latin-1") ; Basque
2140 ("fa" . "UTF-8") ; Persian
2141 ("fi" . "Latin-1") ; Finnish
2142 ("fj" . "Latin-1") ; Fiji
2143 ("fo" . "Latin-1") ; Faroese
2144 ("fr" "French" iso-8859-1) ; French
2145 ("fy" . "Latin-1") ; Frisian
2146 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
2147 ("gd" . "Latin-9") ; Scots Gaelic
2148 ("gez" "Ethiopic" utf-8) ; Geez
2149 ("gl" . "Latin-1") ; Gallegan; Galician
2151 ("gu" . "UTF-8") ; Gujarati
2152 ("gv" . "Latin-1") ; Manx Gaelic
2154 ("he" "Hebrew" iso-8859-8)
2155 ("hi" "Devanagari" utf-8) ; Hindi
2156 ("hr" "Croatian" iso-8859-2) ; Croatian
2157 ("hu" . "Latin-2") ; Hungarian
2160 ("id" . "Latin-1") ; Indonesian
2163 ("is" . "Latin-1") ; Icelandic
2164 ("it" "Italian" iso-8859-1) ; Italian
2166 ("iw" "Hebrew" iso-8859-8)
2167 ("ja" "Japanese" euc-jp)
2169 ("ka" "Georgian" georgian-ps) ; Georgian
2171 ("kl" . "Latin-1") ; Greenlandic
2173 ("kn" "Kannada" utf-8)
2174 ("ko" "Korean" euc-kr)
2177 ("kw" . "Latin-1") ; Cornish
2179 ("la" . "Latin-1") ; Latin
2180 ("lb" . "Latin-1") ; Luxemburgish
2181 ("lg" . "Laint-6") ; Ganda
2183 ("lo" "Lao" utf-8) ; Laothian
2184 ("lt" "Lithuanian" iso-8859-13)
2185 ("lv" . "Latvian") ; Latvian, Lettish
2187 ("mi" . "Latin-7") ; Maori
2188 ("mk" "Cyrillic-ISO" iso-8859-5) ; Macedonian
2189 ("ml" "Malayalam" utf-8)
2190 ("mn" . "UTF-8") ; Mongolian
2192 ("mr" "Devanagari" utf-8) ; Marathi
2193 ("ms" . "Latin-1") ; Malay
2194 ("mt" . "Latin-3") ; Maltese
2197 ("nb" . "Latin-1") ; Norwegian
2198 ("ne" "Devanagari" utf-8) ; Nepali
2199 ("nl" "Dutch" iso-8859-1)
2200 ("no" . "Latin-1") ; Norwegian
2201 ("oc" . "Latin-1") ; Occitan
2202 ("om_ET" . "UTF-8") ; (Afan) Oromo
2203 ("om" . "Latin-1") ; (Afan) Oromo
2205 ("pa" . "UTF-8") ; Punjabi
2206 ("pl" . "Latin-2") ; Polish
2208 ("pt" . "Latin-1") ; Portuguese
2210 ("rm" . "Latin-1") ; Rhaeto-Romanic
2212 ("ro" "Romanian" iso-8859-2)
2213 ("ru_RU" "Russian" iso-8859-5)
2214 ("ru_UA" "Russian" koi8-u)
2216 ("sa" . "Devanagari") ; Sanskrit
2218 ("se" . "UTF-8") ; Northern Sami
2220 ("sh" . "Latin-2") ; Serbo-Croatian
2222 ("sid" . "UTF-8") ; Sidamo
2223 ("sk" "Slovak" iso-8859-2)
2224 ("sl" "Slovenian" iso-8859-2)
2227 ("so_ET" "UTF-8") ; Somali
2228 ("so" "Latin-1") ; Somali
2229 ("sq" . "Latin-1") ; Albanian
2230 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2232 ("st" . "Latin-1") ; Sesotho
2234 ("sv" "Swedish" iso-8859-1) ; Swedish
2235 ("sw" . "Latin-1") ; Swahili
2236 ("ta" "Tamil" utf-8)
2237 ("te" . "UTF-8") ; Telugu
2238 ("tg" "Tajik" koi8-t)
2239 ("th" "Thai" tis-620)
2240 ("ti" "Ethiopic" utf-8) ; Tigrinya
2241 ("tig_ER" . "UTF-8") ; Tigre
2243 ("tl" . "Latin-1") ; Tagalog
2246 ("tr" "Turkish" iso-8859-9)
2248 ("tt" . "UTF-8") ; Tatar
2251 ("uk" "Ukrainian" koi8-u)
2252 ("ur" . "UTF-8") ; Urdu
2253 ("uz_UZ@cyrillic" . "UTF-8"); Uzbek
2254 ("uz" . "Latin-1") ; Uzbek
2255 ("vi" "Vietnamese" utf-8)
2257 ("wa" . "Latin-1") ; Walloon
2259 ("xh" . "Latin-1") ; Xhosa
2260 ("yi" . "Windows-1255") ; Yiddish
2263 ("zh_HK" . "Chinese-Big5")
2264 ; zh_HK/BIG5-HKSCS \
2265 ("zh_TW" . "Chinese-Big5")
2266 ("zh_CN.GB2312" "Chinese-GB")
2267 ("zh_CN.GBK" "Chinese-GBK")
2268 ("zh_CN.GB18030" "Chinese-GB18030")
2269 ("zh_CN.UTF-8" . "Chinese-GBK")
2270 ("zh_CN" . "Chinese-GB")
2271 ("zh" . "Chinese-GB")
2272 ("zu" . "Latin-1") ; Zulu
2274 ;; ISO standard locales
2276 ("posix$" . "ASCII")
2278 ;; The "IPA" Emacs language environment does not correspond
2279 ;; to any ISO 639 code, so let it stand for itself.
2282 ;; Nonstandard or obsolete language codes
2283 ("cz" . "Czech") ; e.g. Solaris 2.6
2284 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2285 ("iw" . "Hebrew") ; e.g. X11R6.4
2286 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2287 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2288 ("jp" . "Japanese") ; e.g. MS Windows
2289 ("chs" . "Chinese-GBK") ; MS Windows Chinese Simplified
2290 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2291 ("gbz" . "UTF-8") ; MS Windows Dari Persian
2292 ("div" . "UTF-8") ; MS Windows Divehi (Maldives)
2293 ("wee" . "Latin-2") ; MS Windows Lower Sorbian
2294 ("wen" . "Latin-2") ; MS Windows Upper Sorbian
2296 "Alist of locale regexps vs the corresponding languages and coding systems.
2297 Each element has these form:
2298 \(LOCALE-REGEXP LANG-ENV CODING-SYSTEM)
2299 The first element whose LOCALE-REGEXP matches the start of a
2300 downcased locale specifies the LANG-ENV \(language environtment)
2301 and CODING-SYSTEM corresponding to that locale. If there is no
2302 appropriate language environment, the element may have this form:
2303 \(LOCALE-REGEXP . LANG-ENV)
2304 In this case, LANG-ENV is one of generic language environments for an
2305 specific encoding such as \"Latin-1\" and \"UTF-8\".")
2307 (defconst locale-charset-language-names
2309 '((".*8859[-_]?1\\>" . "Latin-1")
2310 (".*8859[-_]?2\\>" . "Latin-2")
2311 (".*8859[-_]?3\\>" . "Latin-3")
2312 (".*8859[-_]?4\\>" . "Latin-4")
2313 (".*8859[-_]?9\\>" . "Latin-5")
2314 (".*8859[-_]?14\\>" . "Latin-8")
2315 (".*8859[-_]?15\\>" . "Latin-9")
2316 (".*utf\\(?:-?8\\)?\\>" . "UTF-8")
2317 ;; utf-8@euro exists, so put this last. (@euro really specifies
2318 ;; the currency, rather than the charset.)
2319 (".*@euro\\>" . "Latin-9")))
2320 "List of pairs of locale regexps and charset language names.
2321 The first element whose locale regexp matches the start of a downcased locale
2322 specifies the language name whose charset corresponds to that locale.
2323 This language name is used if the locale is not listed in
2324 `locale-language-names'")
2326 (defconst locale-preferred-coding-systems
2328 '((".*8859[-_]?1\\>" . iso-8859-1)
2329 (".*8859[-_]?2\\>" . iso-8859-2)
2330 (".*8859[-_]?3\\>" . iso-8859-3)
2331 (".*8859[-_]?4\\>" . iso-8859-4)
2332 (".*8859[-_]?9\\>" . iso-8859-9)
2333 (".*8859[-_]?14\\>" . iso-8859-14)
2334 (".*8859[-_]?15\\>" . iso-8859-15)
2335 (".*utf\\(?:-?8\\)?" . utf-8)
2336 ;; utf-8@euro exists, so put this after utf-8. (@euro really
2337 ;; specifies the currency, rather than the charset.)
2338 (".*@euro" . iso-8859-15)
2339 ("koi8-?r" . koi8-r)
2340 ("koi8-?u" . koi8-u)
2342 ("big5[-_]?hkscs" . big5-hkscs)
2344 ("euc-?tw" . euc-tw)
2348 ("gb18030" . gb18030)
2349 ("ja.*[._]euc" . japanese-iso-8bit)
2350 ("ja.*[._]jis7" . iso-2022-jp)
2351 ("ja.*[._]pck" . japanese-shift-jis)
2352 ("ja.*[._]sjis" . japanese-shift-jis)
2353 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2355 "List of pairs of locale regexps and preferred coding systems.
2356 The first element whose locale regexp matches the start of a downcased locale
2357 specifies the coding system to prefer when using that locale.
2358 This coding system is used if the locale specifies a specific charset.")
2360 (defun locale-name-match (key alist)
2361 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2362 Return the value corresponding to the first regexp that matches the
2363 start of KEY, or nil if there is no match."
2365 (while (and alist (not element))
2366 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2367 (setq element (car alist)))
2368 (setq alist (cdr alist)))
2371 (defun locale-charset-match-p (charset1 charset2)
2372 "Whether charset names (strings) CHARSET1 and CHARSET2 are equivalent.
2373 Matching is done ignoring case and any hyphens and underscores in the
2374 names. E.g. `ISO_8859-1' and `iso88591' both match `iso-8859-1'."
2375 (setq charset1 (replace-regexp-in-string "[-_]" "" charset1))
2376 (setq charset2 (replace-regexp-in-string "[-_]" "" charset2))
2377 (eq t (compare-strings charset1 nil nil charset2 nil nil t)))
2379 (defvar locale-charset-alist nil
2380 "Coding system alist keyed on locale-style charset name.
2381 Used by `locale-charset-to-coding-system'.")
2383 (defun locale-charset-to-coding-system (charset)
2384 "Find coding system corresponding to CHARSET.
2385 CHARSET is any sort of non-Emacs charset name, such as might be used
2386 in a locale codeset, or elsewhere. It is matched to a coding system
2387 first by case-insensitive lookup in `locale-charset-alist'. Then
2388 matches are looked for in the coding system list, treating case and
2389 the characters `-' and `_' as insignificant. The coding system base
2390 is returned. Thus, for instance, if charset \"ISO8859-2\",
2391 `iso-latin-2' is returned."
2392 (or (car (assoc-string charset locale-charset-alist t))
2393 (let ((cs coding-system-alist)
2395 (while (and (not c) cs)
2396 (if (locale-charset-match-p charset (caar cs))
2397 (setq c (intern (caar cs)))
2399 (if c (coding-system-base c)))))
2401 ;; Fixme: This ought to deal with the territory part of the locale
2402 ;; too, for setting things such as calendar holidays, ps-print paper
2403 ;; size, spelling dictionary.
2405 (defun set-locale-environment (&optional locale-name)
2406 "Set up multi-lingual environment for using LOCALE-NAME.
2407 This sets the language environment, the coding system priority,
2408 the default input method and sometimes other things.
2410 LOCALE-NAME should be a string which is the name of a locale supported
2411 by the system. Often it is of the form xx_XX.CODE, where xx is a
2412 language, XX is a country, and CODE specifies a character set and
2413 coding system. For example, the locale name \"ja_JP.EUC\" might name
2414 a locale for Japanese in Japan using the `japanese-iso-8bit'
2415 coding-system. The name may also have a modifier suffix, e.g. `@euro'
2418 If LOCALE-NAME is nil, its value is taken from the environment
2419 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2421 The locale names supported by your system can typically be found in a
2422 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2423 will be translated according to the table specified by
2424 `locale-translation-file-name'.
2426 See also `locale-charset-language-names', `locale-language-names',
2427 `locale-preferred-coding-systems' and `locale-coding-system'."
2428 (interactive "sSet environment for locale: ")
2430 ;; Do this at runtime for the sake of binaries possibly transported
2431 ;; to a system without X.
2432 (setq locale-translation-file-name
2434 '("/usr/share/X11/locale/locale.alias" ; e.g. X11R7
2435 "/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2436 "/usr/X11R6/lib/X11/locale/locale.alias" ; XFree86, e.g. RedHat 4.2
2437 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2439 ;; The following name appears after the X-related names above,
2440 ;; since the X-related names are what X actually uses.
2441 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2443 (while (and files (not (file-exists-p (car files))))
2444 (setq files (cdr files)))
2447 (let ((locale locale-name))
2450 ;; Use the first of these three environment variables
2451 ;; that has a nonempty value.
2452 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2454 (= 0 (length locale))) ; nil or empty string
2455 (setq locale (getenv (pop vars))))))
2458 ;; The two tests are kept separate so the byte-compiler sees
2459 ;; that mac-get-preference is only called after checking its existence.
2460 (when (fboundp 'mac-get-preference)
2461 (setq locale (mac-get-preference "AppleLocale"))
2463 (let ((languages (mac-get-preference "AppleLanguages")))
2464 (unless (= (length languages) 0) ; nil or empty vector
2465 (setq locale (aref languages 0)))))))
2466 (unless (or locale (not (boundp 'mac-system-locale)))
2467 (setq locale mac-system-locale))
2471 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2472 ;; using the translation file that many systems have.
2473 (when locale-translation-file-name
2475 (set-buffer-multibyte nil)
2476 (insert-file-contents locale-translation-file-name)
2477 (when (re-search-forward
2478 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2479 (setq locale (buffer-substring (point) (line-end-position))))))
2481 ;; Leave the system locales alone if the caller did not specify
2482 ;; an explicit locale name, as their defaults are set from
2483 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2484 ;; want to set them to the same value as LC_CTYPE.
2486 (setq system-messages-locale locale)
2487 (setq system-time-locale locale))
2489 (setq locale (downcase locale))
2491 (let ((language-name
2492 (locale-name-match locale locale-language-names))
2493 (charset-language-name
2494 (locale-name-match locale locale-charset-language-names))
2495 (default-eol-type (coding-system-eol-type
2496 default-buffer-file-coding-system))
2498 (or (locale-name-match locale locale-preferred-coding-systems)
2500 (if (string-match "\\.\\([^@]+\\)" locale)
2501 (locale-charset-to-coding-system
2502 (match-string 1 locale))))
2503 (and (eq system-type 'macos) mac-system-coding-system))))
2505 (if (consp language-name)
2506 ;; locale-language-names specify both lang-env and coding.
2507 ;; But, what specified in locale-preferred-coding-systems
2508 ;; has higher priority.
2509 (setq coding-system (or coding-system
2510 (nth 1 language-name))
2511 language-name (car language-name))
2512 ;; Otherwise, if locale is not listed in locale-language-names,
2513 ;; use what listed in locale-charset-language-names.
2514 (if (not language-name)
2515 (setq language-name charset-language-name)))
2517 ;; If a specific EOL conversion was specified in the default
2518 ;; buffer-file-coding-system, preserve it in the coding system
2519 ;; we will be using from now on.
2520 (if (and (memq default-eol-type '(0 1 2 unix dos mac))
2522 (coding-system-p coding-system))
2523 (setq coding-system (coding-system-change-eol-conversion
2524 coding-system default-eol-type)))
2528 ;; Set up for this character set. This is now the right way
2529 ;; to do it for both unibyte and multibyte modes.
2530 (set-language-environment language-name)
2532 ;; If default-enable-multibyte-characters is nil,
2533 ;; we are using single-byte characters,
2534 ;; so the display table and terminal coding system are irrelevant.
2535 (when default-enable-multibyte-characters
2536 (set-display-table-and-terminal-coding-system
2537 language-name coding-system))
2539 ;; Set the `keyboard-coding-system' if appropriate (tty
2540 ;; only). At least X and MS Windows can generate
2541 ;; multilingual input.
2542 (unless window-system
2543 (let ((kcs (or coding-system
2544 (car (get-language-info language-name
2546 (if kcs (set-keyboard-coding-system kcs))))
2548 (setq locale-coding-system
2549 (car (get-language-info language-name 'coding-priority))))
2551 (when (and coding-system
2552 (not (coding-system-equal coding-system
2553 locale-coding-system)))
2554 (prefer-coding-system coding-system)
2555 ;; Fixme: perhaps prefer-coding-system should set this too.
2556 ;; But it's not the time to do such a fundamental change.
2557 (setq default-sendmail-coding-system coding-system)
2558 (setq locale-coding-system coding-system))
2560 (when (get-language-info current-language-environment 'coding-priority)
2561 (let ((codeset (locale-info 'codeset))
2562 (coding-system (car (coding-system-priority-list))))
2564 (let ((cs (coding-system-aliases coding-system))
2566 (while (and cs (not result))
2568 (locale-charset-match-p (symbol-name (pop cs))
2569 (locale-info 'codeset))))
2571 (message "Warning: Default coding system `%s' disagrees with
2572 system codeset `%s' for this locale." coding-system codeset))))))))
2574 ;; On Windows, override locale-coding-system,
2575 ;; keyboard-coding-system with system codepage. Note:
2576 ;; selection-coding-system is already set in w32select.c.
2577 (when (boundp 'w32-ansi-code-page)
2578 (let ((code-page-coding (intern (format "cp%d" w32-ansi-code-page))))
2579 (when (coding-system-p code-page-coding)
2580 (setq locale-coding-system code-page-coding)
2581 (set-keyboard-coding-system code-page-coding)
2582 (set-terminal-coding-system code-page-coding))))
2584 (when (eq system-type 'darwin)
2585 ;; On Darwin, file names are always encoded in utf-8, no matter
2587 (setq default-file-name-coding-system 'utf-8)
2588 ;; Mac OS X's Terminal.app by default uses utf-8 regardless of
2590 (when (and (null window-system)
2591 (equal (getenv "TERM_PROGRAM") "Apple_Terminal"))
2592 (set-terminal-coding-system 'utf-8)
2593 (set-keyboard-coding-system 'utf-8)))
2595 ;; Default to A4 paper if we're not in a C, POSIX or US locale.
2596 ;; (See comments in Flocale_info.)
2597 (let ((locale locale)
2598 (paper (locale-info 'paper)))
2600 ;; This will always be null at the time of writing.
2602 ((equal paper '(216 279))
2603 (setq ps-paper-type 'letter))
2604 ((equal paper '(210 297))
2605 (setq ps-paper-type 'a4)))
2606 (let ((vars '("LC_ALL" "LC_PAPER" "LANG")))
2607 (while (and vars (= 0 (length locale)))
2608 (setq locale (getenv (pop vars)))))
2610 ;; As of glibc 2.2.5, these are the only US Letter locales,
2611 ;; and the rest are A4.
2613 (or (locale-name-match locale '(("c$" . letter)
2618 ("enu$" . letter) ; Windows
2625 ;;; Character property
2627 ;; Each element has the form (PROP . TABLE).
2628 ;; PROP is a symbol representing a character property.
2629 ;; TABLE is a char-table containing the property value for each character.
2630 ;; TABLE may be a name of file to load to build a char-table.
2631 ;; Don't modify this variable directly but use `define-char-code-property'.
2633 (defvar char-code-property-alist nil
2634 "Alist of character property name vs char-table containing property values.
2635 Internal use only.")
2637 (put 'char-code-property-table 'char-table-extra-slots 5)
2639 (defun define-char-code-property (name table &optional docstring)
2640 "Define NAME as a character code property given by TABLE.
2641 TABLE is a char-table of purpose `char-code-property-table' with
2644 2nd: Function to call to get a property value of a character.
2645 It is called with three arugments CHAR, VAL, and TABLE, where
2646 CHAR is a character, VAL is the value of (aref TABLE CHAR).
2647 3rd: Function to call to put a property value of a character.
2648 It is called with the same arguments as above.
2649 4th: Function to call to get a description string of a property value.
2650 It is called with one argument VALUE, a property value.
2651 5th: Data used by the above functions.
2653 TABLE may be a name of file to load to build a char-table. The
2654 file should contain a call of `define-char-code-property' with a
2655 char-table of the above format as the argument TABLE.
2657 TABLE may also be nil, in which case no property value is pre-assigned.
2659 Optional 3rd argment DOCSTRING is a documentation string of the property.
2661 See also the documentation of `get-char-code-property' and
2662 `put-char-code-property'."
2664 (error "Not a symbol: %s" name))
2665 (if (char-table-p table)
2666 (or (and (eq (char-table-subtype table) 'char-code-property-table)
2667 (eq (char-table-extra-slot table 0) name))
2668 (error "Invalid char-table: %s" table))
2670 (error "Not a char-table nor a file name: %s" table)))
2671 (let ((slot (assq name char-code-property-alist)))
2674 (setq char-code-property-alist
2675 (cons (cons name table) char-code-property-alist))))
2676 (put name 'char-code-property-documentation docstring))
2678 (defvar char-code-property-table
2679 (make-char-table 'char-code-property-table)
2680 "Char-table containing a property list of each character code.
2681 This table is used for properties not listed in `char-code-property-alist'.
2682 See also the documentation of `get-char-code-property' and
2683 `put-char-code-property'.")
2685 (defun get-char-code-property (char propname)
2686 "Return the value of CHAR's PROPNAME property."
2687 (let ((slot (assq propname char-code-property-alist)))
2689 (let (table value func)
2690 (if (stringp (cdr slot))
2692 (setq table (cdr slot)
2693 value (aref table char)
2694 func (char-table-extra-slot table 1))
2695 (if (functionp func)
2696 (setq value (funcall func char value table)))
2698 (plist-get (aref char-code-property-table char) propname))))
2700 (defun put-char-code-property (char propname value)
2701 "Store CHAR's PROPNAME property with VALUE.
2702 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2703 (let ((slot (assq propname char-code-property-alist)))
2706 (if (stringp (cdr slot))
2708 (setq table (cdr slot)
2709 func (char-table-extra-slot table 2))
2710 (if (functionp func)
2711 (funcall func char value table)
2712 (aset table char value)))
2713 (let* ((plist (aref char-code-property-table char))
2714 (x (plist-put plist propname value)))
2716 (aset char-code-property-table char x))))
2719 (defun char-code-property-description (prop value)
2720 "Return a description string of character property PROP's value VALUE.
2721 If there's no description string for VALUE, return nil."
2722 (let ((slot (assq prop char-code-property-alist)))
2725 (if (stringp (cdr slot))
2727 (setq table (cdr slot)
2728 func (char-table-extra-slot table 3))
2729 (if (functionp func)
2730 (funcall func value))))))
2733 ;; Pretty description of encoded string
2735 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2736 (defvar iso-2022-control-alist
2744 (defun encoded-string-description (str coding-system)
2745 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2746 (setq str (string-as-unibyte str))
2748 (if (and coding-system (eq (coding-system-type coding-system) 'iso-2022))
2749 ;; Try to get a pretty description for ISO 2022 escape sequences.
2750 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2751 (format "#x%02X" x))))
2752 (function (lambda (x) (format "#x%02X" x))))
2755 (defun encode-coding-char (char coding-system)
2756 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2757 If CODING-SYSTEM can't safely encode CHAR, return nil."
2758 (let ((str1 (string-as-multibyte (string char)))
2759 (str2 (string-as-multibyte (string char char)))
2761 (when (memq (coding-system-base coding-system)
2762 (find-coding-systems-string str1))
2763 ;; We must find the encoded string of CHAR. But, just encoding
2764 ;; CHAR will put extra control sequences (usually to designate
2765 ;; ASCII charset) at the tail if type of CODING is ISO 2022.
2766 ;; To exclude such tailing bytes, we at first encode one-char
2767 ;; string and two-char string, then check how many bytes at the
2768 ;; tail of both encoded strings are the same.
2770 (setq enc1 (encode-coding-string str1 coding-system)
2772 enc2 (encode-coding-string str2 coding-system)
2774 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2775 (setq i1 (1- i1) i2 (1- i2)))
2777 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2778 ;; and they are the extra control sequences at the tail to
2780 (substring enc2 0 i2))))
2782 ;; Backwards compatibility. These might be better with :init-value t,
2783 ;; but that breaks loadup.
2784 (define-minor-mode unify-8859-on-encoding-mode
2788 (define-minor-mode unify-8859-on-decoding-mode
2793 (defvar nonascii-insert-offset 0 "This variable is obsolete.")
2794 (defvar nonascii-translation-table nil "This variable is obsolete.")
2796 (defun ucs-insert (arg)
2797 "Insert a character of the given Unicode code point.
2798 Interactively, prompts for a hex string giving the code."
2799 (interactive "sUnicode (hex): ")
2801 (setq arg (string-to-number arg 16)))
2802 (if (or (< arg 0) (> arg #x10FFFF))
2803 (error "Not a Unicode character code: 0x%X" arg))
2807 ;; arch-tag: b382c432-4b36-460e-bf4c-05efd0bb18dc
2808 ;;; mule-cmds.el ends here