(current-language-environment): Set type according to
[emacs.git] / lisp / international / mule-cmds.el
blob2ee7c484bea7d4265de5df8ff97c90777d43a67e
1 ;;; mule-cmds.el --- commands for mulitilingual environment
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5 ;; Copyright (C) 2000, 2001, 2002 Free Software Foundation, Inc.
7 ;; Keywords: mule, multilingual
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
26 ;;; Commentary:
28 ;;; Code:
30 (eval-when-compile (defvar dos-codepage))
32 ;;; MULE related key bindings and menus.
34 (defvar mule-keymap (make-sparse-keymap)
35 "Keymap for Mule (Multilingual environment) specific commands.")
37 ;; Keep "C-x C-m ..." for mule specific commands.
38 (define-key ctl-x-map "\C-m" mule-keymap)
40 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
41 (define-key mule-keymap "r" 'revert-buffer-with-coding-system)
42 (define-key mule-keymap "t" 'set-terminal-coding-system)
43 (define-key mule-keymap "k" 'set-keyboard-coding-system)
44 (define-key mule-keymap "p" 'set-buffer-process-coding-system)
45 (define-key mule-keymap "x" 'set-selection-coding-system)
46 (define-key mule-keymap "X" 'set-next-selection-coding-system)
47 (define-key mule-keymap "\C-\\" 'set-input-method)
48 (define-key mule-keymap "c" 'universal-coding-system-argument)
49 (define-key mule-keymap "l" 'set-language-environment)
51 (defvar mule-menu-keymap
52 (make-sparse-keymap "Mule (Multilingual Environment)")
53 "Keymap for Mule (Multilingual environment) menu specific commands.")
55 (defvar describe-language-environment-map
56 (make-sparse-keymap "Describe Language Environment"))
58 (defvar setup-language-environment-map
59 (make-sparse-keymap "Set Language Environment"))
61 (defvar set-coding-system-map
62 (make-sparse-keymap "Set Coding System"))
64 (define-key-after mule-menu-keymap [set-language-environment]
65 (list 'menu-item "Set Language Environment" setup-language-environment-map
66 :help "Multilingual environment suitable for a specific language"))
67 (define-key-after mule-menu-keymap [mouse-set-font]
68 '(menu-item "Set Font/Fontset" mouse-set-font
69 :visible (fboundp 'generate-fontset-menu)
70 :help "Select a font from list of known fonts/fontsets"))
71 (define-key-after mule-menu-keymap [separator-mule]
72 '("--")
74 (define-key-after mule-menu-keymap [toggle-input-method]
75 '(menu-item "Toggle Input Method" toggle-input-method)
77 (define-key-after mule-menu-keymap [set-input-method]
78 '(menu-item "Select Input Method..." set-input-method)
80 (define-key-after mule-menu-keymap [describe-input-method]
81 '(menu-item "Describe Input Method" describe-input-method))
82 (define-key-after mule-menu-keymap [separator-input-method]
83 '("--")
85 (define-key-after mule-menu-keymap [set-various-coding-system]
86 (list 'menu-item "Set Coding Systems" set-coding-system-map
87 :enable 'enable-multibyte-characters))
88 (define-key-after mule-menu-keymap [view-hello-file]
89 '(menu-item "Show Multi-lingual Text" view-hello-file
90 :enable (file-readable-p
91 (expand-file-name "HELLO" data-directory))
92 :help "Display file which says HELLO in many languages")
94 (define-key-after mule-menu-keymap [separator-coding-system]
95 '("--")
97 (define-key-after mule-menu-keymap [describe-language-environment]
98 (list 'menu-item "Describe Language Environment"
99 describe-language-environment-map
100 :help "Show multilingual settings for a specific language")
102 (define-key-after mule-menu-keymap [describe-input-method]
103 '(menu-item "Describe Input Method..." describe-input-method
104 :help "Keyboard layout for a specific input method")
106 (define-key-after mule-menu-keymap [describe-coding-system]
107 '(menu-item "Describe Coding System..." describe-coding-system)
109 (define-key-after mule-menu-keymap [list-character-sets]
110 '(menu-item "List Character Sets" list-character-sets
111 :help "Show table of available character sets"))
112 (define-key-after mule-menu-keymap [mule-diag]
113 '(menu-item "Show All of Mule Status" mule-diag
114 :help "Display multilingual environment settings")
117 (define-key-after set-coding-system-map [universal-coding-system-argument]
118 '(menu-item "For Next Command" universal-coding-system-argument
119 :help "Coding system to be used by next command")
121 (define-key-after set-coding-system-map [separator-1]
122 '("--")
124 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
125 '(menu-item "For Saving This Buffer" set-buffer-file-coding-system
126 :help "How to encode this buffer when saved")
128 (define-key-after set-coding-system-map [revert-buffer-with-coding-system]
129 '(menu-item "For Reverting This File Now" revert-buffer-with-coding-system
130 :enable buffer-file-name
131 :help "Revisit this file immediately using specified coding system")
133 (define-key-after set-coding-system-map [separator-2]
134 '("--")
137 (define-key-after set-coding-system-map [set-keyboard-coding-system]
138 '(menu-item "For Keyboard" set-keyboard-coding-system
139 :help "How to decode keyboard input")
141 (define-key-after set-coding-system-map [set-terminal-coding-system]
142 '(menu-item "For Terminal" set-terminal-coding-system
143 :enable (null (memq window-system '(x w32 mac)))
144 :help "How to encode terminal output")
146 (define-key-after set-coding-system-map [separator-3]
147 '("--")
149 (define-key-after set-coding-system-map [set-selection-coding-system]
150 '(menu-item "For X Selections/Clipboard" set-selection-coding-system
151 :visible (display-selections-p)
152 :help "How to en/decode data to/from selection/clipboard")
154 (define-key-after set-coding-system-map [set-next-selection-coding-system]
155 '(menu-item "For Next X Selection" set-next-selection-coding-system
156 :visible (display-selections-p)
157 :help "How to en/decode next selection/clipboard operation")
159 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
160 '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system
161 :visible (fboundp 'start-process)
162 :enable (get-buffer-process (current-buffer))
163 :help "How to en/decode I/O from/to subprocess connected to this buffer")
167 (define-key setup-language-environment-map
168 [Default] '(menu-item "Default" setup-specified-language-environment))
170 (define-key describe-language-environment-map
171 [Default] '(menu-item "Default" describe-specified-language-support))
173 ;; This should be a single character key binding because users use it
174 ;; very frequently while editing multilingual text. Now we can use
175 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
176 ;; convenient because it requires shifting on most keyboards. An
177 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
178 ;; but it won't be used that frequently.
179 (define-key global-map "\C-\\" 'toggle-input-method)
181 ;; This is no good because people often type Shift-SPC
182 ;; meaning to type SPC. -- rms.
183 ;; ;; Here's an alternative key binding for X users (Shift-SPACE).
184 ;; (define-key global-map [?\S- ] 'toggle-input-method)
186 ;;; Mule related hyperlinks.
187 (defconst help-xref-mule-regexp-template
188 (purecopy (concat "\\(\\<\\("
189 "\\(coding system\\)\\|"
190 "\\(input method\\)\\|"
191 "\\(character set\\)\\|"
192 "\\(charset\\)"
193 "\\)\\s-+\\)?"
194 ;; Note starting with word-syntax character:
195 "`\\(\\sw\\(\\sw\\|\\s_\\)+\\)'")))
197 (defun coding-system-change-eol-conversion (coding-system eol-type)
198 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
199 The returned coding system converts end-of-line by EOL-TYPE
200 but text as the same way as CODING-SYSTEM.
201 EOL-TYPE should be `unix', `dos', `mac', or nil.
202 If EOL-TYPE is nil, the returned coding system detects
203 how end-of-line is formatted automatically while decoding.
205 EOL-TYPE can be specified by an integer 0, 1, or 2.
206 They means `unix', `dos', and `mac' respectively."
207 (if (symbolp eol-type)
208 (setq eol-type (cond ((eq eol-type 'unix) 0)
209 ((eq eol-type 'dos) 1)
210 ((eq eol-type 'mac) 2)
211 (t eol-type))))
212 (let ((orig-eol-type (coding-system-eol-type coding-system)))
213 (if (vectorp orig-eol-type)
214 (if (not eol-type)
215 coding-system
216 (aref orig-eol-type eol-type))
217 (let ((base (coding-system-base coding-system)))
218 (if (not eol-type)
219 base
220 (if (= eol-type orig-eol-type)
221 coding-system
222 (setq orig-eol-type (coding-system-eol-type base))
223 (if (vectorp orig-eol-type)
224 (aref orig-eol-type eol-type))))))))
226 (defun coding-system-change-text-conversion (coding-system coding)
227 "Return a coding system which differs from CODING-SYSTEM in text conversion.
228 The returned coding system converts text by CODING
229 but end-of-line as the same way as CODING-SYSTEM.
230 If CODING is nil, the returned coding system detects
231 how text is formatted automatically while decoding."
232 (let ((eol-type (coding-system-eol-type coding-system)))
233 (coding-system-change-eol-conversion
234 (if coding coding 'undecided)
235 (if (numberp eol-type) (aref [unix dos mac] eol-type)))))
237 (defun toggle-enable-multibyte-characters (&optional arg)
238 "Change whether this buffer uses multibyte characters.
239 With arg, use multibyte characters if the arg is positive.
241 Note that this command does not convert the byte contents of
242 the buffer; it only changes the way those bytes are interpreted.
243 In general, therefore, this command *changes* the sequence of
244 characters that the current buffer contains.
246 We suggest you avoid using use this command unless you know what you
247 are doing. If you use it by mistake, and the buffer is now displayed
248 wrong, use this command again to toggle back to the right mode."
249 (interactive "P")
250 (let ((new-flag
251 (if (null arg) (null enable-multibyte-characters)
252 (> (prefix-numeric-value arg) 0))))
253 (set-buffer-multibyte new-flag))
254 (force-mode-line-update))
256 (defun view-hello-file ()
257 "Display the HELLO file which list up many languages and characters."
258 (interactive)
259 ;; We have to decode the file in any environment.
260 (let ((default-enable-multibyte-characters t)
261 (coding-system-for-read 'iso-2022-7bit))
262 (find-file-read-only (expand-file-name "HELLO" data-directory))))
264 (defun universal-coding-system-argument (coding-system)
265 "Execute an I/O command using the specified coding system."
266 (interactive
267 (let ((default (and buffer-file-coding-system
268 (not (eq (coding-system-type buffer-file-coding-system)
270 buffer-file-coding-system)))
271 (list (read-coding-system
272 (if default
273 (format "Coding system for following command (default, %s): " default)
274 "Coding system for following command: ")
275 default))))
276 (let* ((keyseq (read-key-sequence
277 (format "Command to execute with %s:" coding-system)))
278 (cmd (key-binding keyseq))
279 prefix)
281 (when (eq cmd 'universal-argument)
282 (call-interactively cmd)
284 ;; Process keys bound in `universal-argument-map'.
285 (while (progn
286 (setq keyseq (read-key-sequence nil t)
287 cmd (key-binding keyseq t))
288 (not (eq cmd 'universal-argument-other-key)))
289 (let ((current-prefix-arg prefix-arg)
290 ;; Have to bind `last-command-char' here so that
291 ;; `digit-argument', for isntance, can compute the
292 ;; prefix arg.
293 (last-command-char (aref keyseq 0)))
294 (call-interactively cmd)))
296 ;; This is the final call to `univeral-argument-other-key', which
297 ;; set's the final `prefix-arg.
298 (let ((current-prefix-arg prefix-arg))
299 (call-interactively cmd))
301 ;; Read the command to execute with the given prefix arg.
302 (setq prefix prefix-arg
303 keyseq (read-key-sequence nil t)
304 cmd (key-binding keyseq)))
306 (let ((coding-system-for-read coding-system)
307 (coding-system-for-write coding-system)
308 (current-prefix-arg prefix))
309 (message "")
310 (call-interactively cmd))))
312 (defun set-default-coding-systems (coding-system)
313 "Set default value of various coding systems to CODING-SYSTEM.
314 This sets the following coding systems:
315 o coding system of a newly created buffer
316 o default coding system for subprocess I/O
317 This also sets the following values:
318 o default value used as `file-name-coding-system' for converting file names.
319 o default value for the command `set-terminal-coding-system' (not on MSDOS)
320 o default value for the command `set-keyboard-coding-system'."
321 (check-coding-system coding-system)
322 (setq-default buffer-file-coding-system coding-system)
323 (if default-enable-multibyte-characters
324 (setq default-file-name-coding-system coding-system))
325 ;; If coding-system is nil, honor that on MS-DOS as well, so
326 ;; that they could reset the terminal coding system.
327 (unless (and (eq window-system 'pc) coding-system)
328 (setq default-terminal-coding-system coding-system))
329 (setq default-keyboard-coding-system coding-system)
330 ;; Preserve eol-type from existing default-process-coding-systems.
331 ;; On non-unix-like systems in particular, these may have been set
332 ;; carefully by the user, or by the startup code, to deal with the
333 ;; users shell appropriately, so should not be altered by changing
334 ;; language environment.
335 (let ((output-coding
336 (coding-system-change-text-conversion
337 (car default-process-coding-system) coding-system))
338 (input-coding
339 (coding-system-change-text-conversion
340 (cdr default-process-coding-system) coding-system)))
341 (setq default-process-coding-system
342 (cons output-coding input-coding))))
344 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
345 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal "20.3")
347 (defun prefer-coding-system (coding-system)
348 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
349 This also sets the following coding systems:
350 o coding system of a newly created buffer
351 o default coding system for subprocess I/O
352 This also sets the following values:
353 o default value used as `file-name-coding-system' for converting file names.
354 o default value for the command `set-terminal-coding-system' (not on MSDOS)
355 o default value for the command `set-keyboard-coding-system'
357 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
358 systems set by this function will use that type of EOL conversion.
360 This command does not change the default value of terminal coding system
361 for MS-DOS terminal, because DOS terminals only support a single coding
362 system, and Emacs automatically sets the default to that coding system at
363 startup.
365 A coding system that requires automatic detection of text
366 encoding (e.g. undecided, unix) can't be preferred.
368 See also `coding-category-list' and `coding-system-category'."
369 (interactive "zPrefer coding system: ")
370 (if (not (and coding-system (coding-system-p coding-system)))
371 (error "Invalid coding system `%s'" coding-system))
372 (let ((coding-category (coding-system-category coding-system))
373 (base (coding-system-base coding-system))
374 (eol-type (coding-system-eol-type coding-system)))
375 (if (not coding-category)
376 ;; CODING-SYSTEM is no-conversion or undecided.
377 (error "Can't prefer the coding system `%s'" coding-system))
378 (set coding-category (or base coding-system))
379 (update-coding-systems-internal)
380 (or (eq coding-category (car coding-category-list))
381 ;; We must change the order.
382 (set-coding-priority (list coding-category)))
383 (if (and base (interactive-p))
384 (message "Highest priority is set to %s (base of %s)"
385 base coding-system))
386 ;; If they asked for specific EOL conversion, honor that.
387 (if (memq eol-type '(0 1 2))
388 (setq coding-system
389 (coding-system-change-eol-conversion base eol-type))
390 (setq coding-system base))
391 (set-default-coding-systems coding-system)))
393 (defvar sort-coding-systems-predicate nil
394 "If non-nil, a predicate function to sort coding systems.
396 It is called with two coding systems, and should return t if the first
397 one is \"less\" than the second.
399 The function `sort-coding-systems' use it.")
401 (defun sort-coding-systems (codings)
402 "Sort coding system list CODINGS by a priority of each coding system.
404 If a coding system is most preferred, it has the highest priority.
405 Otherwise, a coding system corresponds to some MIME charset has higher
406 priorities. Among them, a coding system included in `coding-system'
407 key of the current language environment has higher priorities. See
408 also the documentation of `language-info-alist'.
410 If the variable `sort-coding-systems-predicate' (which see) is
411 non-nil, it is used to sort CODINGS in the different way than above."
412 (if sort-coding-systems-predicate
413 (sort codings sort-coding-systems-predicate)
414 (let* ((most-preferred (symbol-value (car coding-category-list)))
415 (lang-preferred (get-language-info current-language-environment
416 'coding-system))
417 (func (function
418 (lambda (x)
419 (let ((base (coding-system-base x)))
420 (+ (if (eq base most-preferred) 64 0)
421 (let ((mime (coding-system-get base 'mime-charset)))
422 (if mime
423 (if (string-match "^x-" (symbol-name mime))
424 16 32)
426 (if (memq base lang-preferred) 8 0)
427 (if (string-match "-with-esc$" (symbol-name base))
428 0 4)
429 (if (eq (coding-system-type base) 2)
430 ;; For ISO based coding systems, prefer
431 ;; one that doesn't use escape sequences.
432 (let ((flags (coding-system-flags base)))
433 (if (or (consp (aref flags 0))
434 (consp (aref flags 1))
435 (consp (aref flags 2))
436 (consp (aref flags 3)))
437 (if (or (aref flags 8) (aref flags 9))
441 1)))))))
442 (sort codings (function (lambda (x y)
443 (> (funcall func x) (funcall func y))))))))
445 (defun find-coding-systems-region (from to)
446 "Return a list of proper coding systems to encode a text between FROM and TO.
447 If FROM is a string, find coding systems in that instead of the buffer.
448 All coding systems in the list can safely encode any multibyte characters
449 in the text.
451 If the text contains no multibyte characters, return a list of a single
452 element `undecided'."
453 (let ((codings (find-coding-systems-region-internal from to)))
454 (if (eq codings t)
455 ;; The text contains only ASCII characters. Any coding
456 ;; systems are safe.
457 '(undecided)
458 ;; We need copy-sequence because sorting will alter the argument.
459 (sort-coding-systems (copy-sequence codings)))))
461 (defun find-coding-systems-string (string)
462 "Return a list of proper coding systems to encode STRING.
463 All coding systems in the list can safely encode any multibyte characters
464 in STRING.
466 If STRING contains no multibyte characters, return a list of a single
467 element `undecided'."
468 (find-coding-systems-region string nil))
470 (defun find-coding-systems-for-charsets (charsets)
471 "Return a list of proper coding systems to encode characters of CHARSETS.
472 CHARSETS is a list of character sets."
473 (cond ((or (null charsets)
474 (and (= (length charsets) 1)
475 (eq 'ascii (car charsets))))
476 '(undecided))
477 ((or (memq 'eight-bit-control charsets)
478 (memq 'eight-bit-graphic charsets))
479 '(raw-text emacs-mule))
481 (let ((codings t)
482 charset l ll)
483 (while (and codings charsets)
484 (setq charset (car charsets) charsets (cdr charsets))
485 (unless (eq charset 'ascii)
486 (setq l (aref char-coding-system-table (make-char charset)))
487 (if (eq codings t)
488 (setq codings l)
489 (let ((ll nil))
490 (while codings
491 (if (memq (car codings) l)
492 (setq ll (cons (car codings) ll)))
493 (setq codings (cdr codings)))
494 (setq codings ll)))))
495 (append codings
496 (char-table-extra-slot char-coding-system-table 0))))))
498 (defun find-multibyte-characters (from to &optional maxcount excludes)
499 "Find multibyte characters in the region specified by FROM and TO.
500 If FROM is a string, find multibyte characters in the string.
501 The return value is an alist of the following format:
502 ((CHARSET COUNT CHAR ...) ...)
503 where
504 CHARSET is a character set,
505 COUNT is a number of characters,
506 CHARs are found characters of the character set.
507 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
508 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
510 For invalid characters, CHARs are actually strings."
511 (let ((chars nil)
512 charset char)
513 (if (stringp from)
514 (let ((idx 0))
515 (while (setq idx (string-match "[^\000-\177]" from idx))
516 (setq char (aref from idx)
517 charset (char-charset char))
518 (if (eq charset 'unknown)
519 (setq char (match-string 0)))
520 (if (or (memq charset '(unknown
521 eight-bit-control eight-bit-graphic))
522 (not (or (eq excludes t) (memq charset excludes))))
523 (let ((slot (assq charset chars)))
524 (if slot
525 (if (not (memq char (nthcdr 2 slot)))
526 (let ((count (nth 1 slot)))
527 (setcar (cdr slot) (1+ count))
528 (if (or (not maxcount) (< count maxcount))
529 (nconc slot (list char)))))
530 (setq chars (cons (list charset 1 char) chars)))))
531 (setq idx (1+ idx))))
532 (save-excursion
533 (goto-char from)
534 (while (re-search-forward "[^\000-\177]" to t)
535 (setq char (preceding-char)
536 charset (char-charset char))
537 (if (eq charset 'unknown)
538 (setq char (match-string 0)))
539 (if (or (memq charset '(unknown eight-bit-control eight-bit-graphic))
540 (not (or (eq excludes t) (memq charset excludes))))
541 (let ((slot (assq charset chars)))
542 (if slot
543 (if (not (member char (nthcdr 2 slot)))
544 (let ((count (nth 1 slot)))
545 (setcar (cdr slot) (1+ count))
546 (if (or (not maxcount) (< count maxcount))
547 (nconc slot (list char)))))
548 (setq chars (cons (list charset 1 char) chars))))))))
549 (nreverse chars)))
552 (defun search-unencodable-char (coding-system)
553 "Search forward from point for a character that is not encodable.
554 It asks which coding system to check.
555 If such a character is found, set point after that character.
556 Otherwise, don't move point.
558 When called from a program, the value is a position of the found character,
559 or nil if all characters are encodable."
560 (interactive
561 (list (let ((default (or buffer-file-coding-system 'us-ascii)))
562 (read-coding-system
563 (format "Coding-system (default, %s): " default)
564 default))))
565 (let ((pos (unencodable-char-position (point) (point-max) coding-system)))
566 (if pos
567 (goto-char (1+ pos))
568 (message "All following characters are encodable by %s" coding-system))
569 pos))
572 (defvar last-coding-system-specified nil
573 "Most recent coding system explicitly specified by the user when asked.
574 This variable is set whenever Emacs asks the user which coding system
575 to use in order to write a file. If you set it to nil explicitly,
576 then call `write-region', then afterward this variable will be non-nil
577 only if the user was explicitly asked and specified a coding system.")
579 (defvar select-safe-coding-system-accept-default-p nil
580 "If non-nil, a function to control the behaviour of coding system selection.
581 The meaning is the same as the argument ACCEPT-DEFAULT-P of the
582 function `select-safe-coding-system' (which see). This variable
583 overrides that argument.")
585 (defun select-safe-coding-system (from to &optional default-coding-system
586 accept-default-p file)
587 "Ask a user to select a safe coding system from candidates.
588 The candidates of coding systems which can safely encode a text
589 between FROM and TO are shown in a popup window. Among them, the most
590 proper one is suggested as the default.
592 The list of `buffer-file-coding-system' of the current buffer and the
593 most preferred coding system (if it corresponds to a MIME charset) is
594 treated as the default coding system list. Among them, the first one
595 that safely encodes the text is normally selected silently and
596 returned without any user interaction. See also the command
597 `prefer-coding-system'.
599 However, the user is queried if the chosen coding system is
600 inconsistent with what would be selected by `set-auto-coding' from
601 coding cookies &c. if the contents of the region were read from a
602 file. (That could lead to data corruption in a file subsequently
603 re-visited and edited.)
605 Optional 3rd arg DEFAULT-CODING-SYSTEM specifies a coding system or a
606 list of coding systems to be prepended to the default coding system
607 list.
609 Optional 4th arg ACCEPT-DEFAULT-P, if non-nil, is a function to
610 determine the acceptability of the silently selected coding system.
611 It is called with that coding system, and should return nil if it
612 should not be silently selected and thus user interaction is required.
614 Optional 5th arg FILE is the file name to use for this purpose.
615 That is different from `buffer-file-name' when handling `write-region'
616 \(for example).
618 The variable `select-safe-coding-system-accept-default-p', if
619 non-nil, overrides ACCEPT-DEFAULT-P.
621 Kludgy feature: if FROM is a string, the string is the target text,
622 and TO is ignored."
623 (if (and default-coding-system
624 (not (listp default-coding-system)))
625 (setq default-coding-system (list default-coding-system)))
627 ;; Change elements of the list to (coding . base-coding).
628 (setq default-coding-system
629 (mapcar (function (lambda (x) (cons x (coding-system-base x))))
630 default-coding-system))
632 ;; If buffer-file-coding-system is not nil nor undecided, append it
633 ;; to the defaults.
634 (if buffer-file-coding-system
635 (let ((base (coding-system-base buffer-file-coding-system)))
636 (or (eq base 'undecided)
637 (assq buffer-file-coding-system default-coding-system)
638 (rassq base default-coding-system)
639 (setq default-coding-system
640 (append default-coding-system
641 (list (cons buffer-file-coding-system base)))))))
643 ;; If the most preferred coding system has the property mime-charset,
644 ;; append it to the defaults.
645 (let ((tail coding-category-list)
646 preferred base)
647 (while (and tail
648 (not (setq preferred (symbol-value (car tail)))))
649 (setq tail (cdr tail)))
650 (and (coding-system-p preferred)
651 (setq base (coding-system-base preferred))
652 (coding-system-get preferred 'mime-charset)
653 (not (assq preferred default-coding-system))
654 (not (rassq base default-coding-system))
655 (setq default-coding-system
656 (append default-coding-system (list (cons preferred base))))))
658 (if select-safe-coding-system-accept-default-p
659 (setq accept-default-p select-safe-coding-system-accept-default-p))
661 (let ((codings (find-coding-systems-region from to))
662 (coding-system nil)
663 (bufname (buffer-name))
664 safe rejected unsafe)
665 (if (eq (car codings) 'undecided)
666 ;; Any coding system is ok.
667 (setq coding-system t)
668 ;; Classify the defaults into safe, rejected, and unsafe.
669 (dolist (elt default-coding-system)
670 (if (memq (cdr elt) codings)
671 (if (and (functionp accept-default-p)
672 (not (funcall accept-default-p (cdr elt))))
673 (push (car elt) rejected)
674 (push (car elt) safe))
675 (push (car elt) unsafe)))
676 (if safe
677 (setq coding-system (car (last safe)))))
679 ;; If all the defaults failed, ask a user.
680 (when (not coding-system)
681 ;; At first, if some defaults are unsafe, record at most 11
682 ;; problematic characters and their positions for them by turning
683 ;; (CODING ...)
684 ;; into
685 ;; ((CODING (POS . CHAR) (POS . CHAR) ...) ...)
686 (if unsafe
687 (if (stringp from)
688 (setq unsafe
689 (mapcar #'(lambda (coding)
690 (cons coding
691 (mapcar #'(lambda (pos)
692 (cons pos (aref from pos)))
693 (unencodable-char-position
694 0 (length from) coding
695 11 from))))
696 unsafe))
697 (setq unsafe
698 (mapcar #'(lambda (coding)
699 (cons coding
700 (mapcar #'(lambda (pos)
701 (cons pos (char-after pos)))
702 (unencodable-char-position
703 from to coding 11))))
704 unsafe))))
706 ;; Change each safe coding system to the corresponding
707 ;; mime-charset name if it is also a coding system. Such a name
708 ;; is more friendly to users.
709 (let ((l codings)
710 mime-charset)
711 (while l
712 (setq mime-charset (coding-system-get (car l) 'mime-charset))
713 (if (and mime-charset (coding-system-p mime-charset))
714 (setcar l mime-charset))
715 (setq l (cdr l))))
717 ;; Don't offer variations with locking shift, which you
718 ;; basically never want.
719 (let (l)
720 (dolist (elt codings (setq codings (nreverse l)))
721 (unless (or (eq 'coding-category-iso-7-else
722 (coding-system-category elt))
723 (eq 'coding-category-iso-8-else
724 (coding-system-category elt)))
725 (push elt l))))
727 (let ((window-configuration (current-window-configuration)))
728 (save-excursion
729 ;; If some defaults are unsafe, make sure the offending
730 ;; buffer is displayed.
731 (when (and unsafe (not (stringp from)))
732 (pop-to-buffer bufname)
733 (goto-char (apply 'min (mapcar #'(lambda (x) (car (cadr x)))
734 unsafe))))
735 ;; Then ask users to select one from CODINGS while showing
736 ;; the reason why none of the defaults are not used.
737 (with-output-to-temp-buffer "*Warning*"
738 (save-excursion
739 (set-buffer standard-output)
740 (if (not default-coding-system)
741 (insert "No default coding systems to try for "
742 (if (stringp from)
743 (format "string \"%s\"." from)
744 (format "buffer `%s'." bufname)))
745 (insert
746 "These default coding systems were tried to encode"
747 (if (stringp from)
748 (concat " \"" (if (> (length from) 10)
749 (concat (substring from 0 10) "...\"")
750 (concat from "\"")))
751 (format " text\nin the buffer `%s'" bufname))
752 ":\n")
753 (let ((pos (point))
754 (fill-prefix " "))
755 (mapc #'(lambda (x) (princ " ") (princ (car x)))
756 default-coding-system)
757 (insert "\n")
758 (fill-region-as-paragraph pos (point)))
759 (when rejected
760 (insert "These safely encodes the target text,
761 but it is not recommended for encoding text in this context,
762 e.g., for sending an email message.\n ")
763 (mapc #'(lambda (x) (princ " ") (princ x)) rejected)
764 (insert "\n"))
765 (when unsafe
766 (insert (if rejected "And the others"
767 "However, each of them")
768 " encountered these problematic characters:\n")
769 (mapc
770 #'(lambda (coding)
771 (insert (format " %s:" (car coding)))
772 (let ((i 0)
773 (func1
774 #'(lambda (bufname pos)
775 (when (buffer-live-p (get-buffer bufname))
776 (pop-to-buffer bufname)
777 (goto-char pos))))
778 (func2
779 #'(lambda (bufname pos coding)
780 (when (buffer-live-p (get-buffer bufname))
781 (pop-to-buffer bufname)
782 (if (< (point) pos)
783 (goto-char pos)
784 (forward-char 1)
785 (search-unencodable-char coding)
786 (forward-char -1))))))
787 (dolist (elt (cdr coding))
788 (insert " ")
789 (if (stringp from)
790 (insert (if (< i 10) (cdr elt) "..."))
791 (if (< i 10)
792 (insert-text-button
793 (cdr elt)
794 :type 'help-xref
795 'help-echo
796 "mouse-2, RET: jump to this character"
797 'help-function func1
798 'help-args (list bufname (car elt)))
799 (insert-text-button
800 "..."
801 :type 'help-xref
802 'help-echo
803 "mouse-2, RET: next unencodable character"
804 'help-function func2
805 'help-args (list bufname (car elt)
806 (car coding)))))
807 (setq i (1+ i))))
808 (insert "\n"))
809 unsafe)
810 (insert "\
811 The first problematic character is at point in the displayed buffer,\n"
812 (substitute-command-keys "\
813 and \\[universal-argument] \\[what-cursor-position] will give information about it.\n"))))
814 (insert (if safe
815 "\nSelect the above, or "
816 "\nSelect ")
818 one of the following safe coding systems, or edit the buffer:\n")
819 (let ((pos (point))
820 (fill-prefix " "))
821 (mapcar (function (lambda (x) (princ " ") (princ x)))
822 codings)
823 (insert "\n")
824 (fill-region-as-paragraph pos (point)))))
826 ;; Read a coding system.
827 (if safe
828 (setq codings (append safe codings)))
829 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
830 codings))
831 (name (completing-read
832 (format "Select coding system (default %s): "
833 (car codings))
834 safe-names nil t nil nil
835 (car (car safe-names)))))
836 (setq last-coding-system-specified (intern name)
837 coding-system last-coding-system-specified)))
838 (kill-buffer "*Warning*")
839 (set-window-configuration window-configuration)))
841 (if (vectorp (coding-system-eol-type coding-system))
842 (let ((eol (coding-system-eol-type buffer-file-coding-system)))
843 (if (numberp eol)
844 (setq coding-system
845 (coding-system-change-eol-conversion coding-system eol)))))
847 (if (eq coding-system t)
848 (setq coding-system buffer-file-coding-system))
849 ;; Check we're not inconsistent with what `coding:' spec &c would
850 ;; give when file is re-read.
851 ;; But don't do this if we explicitly ignored the cookie
852 ;; by using `find-file-literally'.
853 (unless (or (stringp from) find-file-literally)
854 (let ((auto-cs (save-excursion
855 (save-restriction
856 (widen)
857 (narrow-to-region from to)
858 (goto-char (point-min))
859 (set-auto-coding (or file buffer-file-name "")
860 (buffer-size))))))
861 (if (and auto-cs coding-system
862 ;; Don't barf if writing a compressed file, say.
863 ;; This check perhaps isn't ideal, but is probably
864 ;; the best thing to do.
865 (not (auto-coding-alist-lookup (or file buffer-file-name "")))
866 (not (coding-system-equal (coding-system-base coding-system)
867 (coding-system-base auto-cs))))
868 (unless (yes-or-no-p
869 (format "Selected encoding %s disagrees with \
870 %s specified by file contents. Really save (else edit coding cookies \
871 and try again)? " coding-system auto-cs))
872 (error "Save aborted")))))
873 coding-system))
875 (setq select-safe-coding-system-function 'select-safe-coding-system)
877 (defun select-message-coding-system ()
878 "Return a coding system to encode the outgoing message of the current buffer.
879 It at first tries the first coding system found in these variables
880 in this order:
881 (1) local value of `buffer-file-coding-system'
882 (2) value of `sendmail-coding-system'
883 (3) value of `default-sendmail-coding-system'
884 (4) value of `default-buffer-file-coding-system'
885 If the found coding system can't encode the current buffer,
886 or none of them are bound to a coding system,
887 it asks the user to select a proper coding system."
888 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
889 buffer-file-coding-system)
890 sendmail-coding-system
891 default-sendmail-coding-system
892 default-buffer-file-coding-system)))
893 (if (eq coding 'no-conversion)
894 ;; We should never use no-conversion for outgoing mail.
895 (setq coding nil))
896 (if (fboundp select-safe-coding-system-function)
897 (funcall select-safe-coding-system-function
898 (point-min) (point-max) coding
899 (function (lambda (x) (coding-system-get x 'mime-charset))))
900 coding)))
902 ;;; Language support stuff.
904 (defvar language-info-alist nil
905 "Alist of language environment definitions.
906 Each element looks like:
907 (LANGUAGE-NAME . ((KEY . INFO) ...))
908 where LANGUAGE-NAME is a string, the name of the language environment,
909 KEY is a symbol denoting the kind of information, and
910 INFO is the data associated with KEY.
911 Meaningful values for KEY include
913 documentation value is documentation of what this language environment
914 is meant for, and how to use it.
915 charset value is a list of the character sets used by this
916 language environment.
917 sample-text value is an expression which is evalled to generate
918 a line of text written using characters appropriate
919 for this language environment.
920 setup-function value is a function to call to switch to this
921 language environment.
922 exit-function value is a function to call to leave this
923 language environment.
924 coding-system value is a list of coding systems that are good
925 for saving text written in this language environment.
926 This list serves as suggestions to the user;
927 in effect, as a kind of documentation.
928 coding-priority value is a list of coding systems for this language
929 environment, in order of decreasing priority.
930 This is used to set up the coding system priority
931 list when you switch to this language environment.
932 nonascii-translation
933 value is a translation table to be set in the
934 variable `nonascii-translation-table' in this
935 language environment, or a character set from
936 which `nonascii-insert-offset' is calculated.
937 input-method value is a default input method for this language
938 environment.
939 features value is a list of features requested in this
940 language environment.
942 The following keys take effect only when multibyte characters are
943 globally disabled, i.e. the value of `default-enable-multibyte-characters'
944 is nil.
946 unibyte-syntax value is a library name to load to set
947 unibyte 8-bit character syntaxes for this
948 language environment.
950 unibyte-display value is a coding system to encode characters
951 for the terminal. Characters in the range
952 of 160 to 255 display not as octal escapes,
953 but as non-ASCII characters in this language
954 environment.")
956 (defun get-language-info (lang-env key)
957 "Return information listed under KEY for language environment LANG-ENV.
958 KEY is a symbol denoting the kind of information.
959 For a list of useful values for KEY and their meanings,
960 see `language-info-alist'."
961 (if (symbolp lang-env)
962 (setq lang-env (symbol-name lang-env)))
963 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
964 (if lang-slot
965 (cdr (assq key (cdr lang-slot))))))
967 (defun set-language-info (lang-env key info)
968 "Modify part of the definition of language environment LANG-ENV.
969 Specifically, this stores the information INFO under KEY
970 in the definition of this language environment.
971 KEY is a symbol denoting the kind of information.
972 INFO is the value for that information.
974 For a list of useful values for KEY and their meanings,
975 see `language-info-alist'."
976 (if (symbolp lang-env)
977 (setq lang-env (symbol-name lang-env)))
978 (let (lang-slot key-slot)
979 (setq lang-slot (assoc lang-env language-info-alist))
980 (if (null lang-slot) ; If no slot for the language, add it.
981 (setq lang-slot (list lang-env)
982 language-info-alist (cons lang-slot language-info-alist)))
983 (setq key-slot (assq key lang-slot))
984 (if (null key-slot) ; If no slot for the key, add it.
985 (progn
986 (setq key-slot (list key))
987 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
988 (setcdr key-slot (purecopy info))
989 ;; Update the custom-type of `current-language-environment'.
990 (put 'current-language-environment 'custom-type
991 (cons 'choice (mapcar
992 (lambda (lang)
993 (list 'const (car lang)))
994 (sort (copy-sequence language-info-alist)
995 (lambda (x y) (string< (car x) (car y)))))))))
997 (defun set-language-info-alist (lang-env alist &optional parents)
998 "Store ALIST as the definition of language environment LANG-ENV.
999 ALIST is an alist of KEY and INFO values. See the documentation of
1000 `language-info-alist' for the meanings of KEY and INFO.
1002 Optional arg PARENTS is a list of parent menu names; it specifies
1003 where to put this language environment in the
1004 Describe Language Environment and Set Language Environment menus.
1005 For example, (\"European\") means to put this language environment
1006 in the European submenu in each of those two menus."
1007 (if (symbolp lang-env)
1008 (setq lang-env (symbol-name lang-env)))
1009 (let ((describe-map describe-language-environment-map)
1010 (setup-map setup-language-environment-map))
1011 (if parents
1012 (let ((l parents)
1013 map parent-symbol parent prompt)
1014 (while l
1015 (if (symbolp (setq parent-symbol (car l)))
1016 (setq parent (symbol-name parent))
1017 (setq parent parent-symbol parent-symbol (intern parent)))
1018 (setq map (lookup-key describe-map (vector parent-symbol)))
1019 ;; This prompt string is for define-prefix-command, so
1020 ;; that the map it creates will be suitable for a menu.
1021 (or map (setq prompt (format "%s Environment" parent)))
1022 (if (not map)
1023 (progn
1024 (setq map (intern (format "describe-%s-environment-map"
1025 (downcase parent))))
1026 (define-prefix-command map nil prompt)
1027 (define-key-after describe-map (vector parent-symbol)
1028 (cons parent map) t)))
1029 (setq describe-map (symbol-value map))
1030 (setq map (lookup-key setup-map (vector parent-symbol)))
1031 (if (not map)
1032 (progn
1033 (setq map (intern (format "setup-%s-environment-map"
1034 (downcase parent))))
1035 (define-prefix-command map nil prompt)
1036 (define-key-after setup-map (vector parent-symbol)
1037 (cons parent map) t)))
1038 (setq setup-map (symbol-value map))
1039 (setq l (cdr l)))))
1041 ;; Set up menu items for this language env.
1042 (let ((doc (assq 'documentation alist)))
1043 (when doc
1044 (define-key-after describe-map (vector (intern lang-env))
1045 (cons lang-env 'describe-specified-language-support) t)))
1046 (define-key-after setup-map (vector (intern lang-env))
1047 (cons lang-env 'setup-specified-language-environment) t)
1049 (while alist
1050 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
1051 (setq alist (cdr alist)))))
1053 (defun read-language-name (key prompt &optional default)
1054 "Read a language environment name which has information for KEY.
1055 If KEY is nil, read any language environment.
1056 Prompt with PROMPT. DEFAULT is the default choice of language environment.
1057 This returns a language environment name as a string."
1058 (let* ((completion-ignore-case t)
1059 (name (completing-read prompt
1060 language-info-alist
1061 (and key
1062 (function (lambda (elm) (and (listp elm) (assq key elm)))))
1063 t nil nil default)))
1064 (if (and (> (length name) 0)
1065 (or (not key)
1066 (get-language-info name key)))
1067 name)))
1069 ;;; Multilingual input methods.
1070 (defgroup leim nil
1071 "LEIM: Libraries of Emacs Input Methods."
1072 :group 'mule)
1074 (defconst leim-list-file-name "leim-list.el"
1075 "Name of LEIM list file.
1076 This file contains a list of libraries of Emacs input methods (LEIM)
1077 in the format of Lisp expression for registering each input method.
1078 Emacs loads this file at startup time.")
1080 (defvar leim-list-header (format
1081 ";;; %s -- list of LEIM (Library of Emacs Input Method) -*-coding: iso-2022-7bit;-*-
1083 ;; This file contains a list of LEIM (Library of Emacs Input Method)
1084 ;; methods in the same directory as this file. Loading this file
1085 ;; registers all the input methods in Emacs.
1087 ;; Each entry has the form:
1088 ;; (register-input-method
1089 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
1090 ;; TITLE DESCRIPTION
1091 ;; ARG ...)
1092 ;; See the function `register-input-method' for the meanings of the arguments.
1094 ;; If this directory is included in load-path, Emacs automatically
1095 ;; loads this file at startup time.
1098 leim-list-file-name)
1099 "Header to be inserted in LEIM list file.")
1101 (defvar leim-list-entry-regexp "^(register-input-method"
1102 "Regexp matching head of each entry in LEIM list file.
1103 See also the variable `leim-list-header'")
1105 (defvar update-leim-list-functions
1106 '(quail-update-leim-list-file)
1107 "List of functions to call to update LEIM list file.
1108 Each function is called with one arg, LEIM directory name.")
1110 (defun update-leim-list-file (&rest dirs)
1111 "Update LEIM list file in directories DIRS."
1112 (let ((functions update-leim-list-functions))
1113 (while functions
1114 (apply (car functions) dirs)
1115 (setq functions (cdr functions)))))
1117 (defvar current-input-method nil
1118 "The current input method for multilingual text.
1119 If nil, that means no input method is activated now.")
1120 (make-variable-buffer-local 'current-input-method)
1121 (put 'current-input-method 'permanent-local t)
1123 (defvar current-input-method-title nil
1124 "Title string of the current input method shown in mode line.")
1125 (make-variable-buffer-local 'current-input-method-title)
1126 (put 'current-input-method-title 'permanent-local t)
1128 (defcustom default-input-method nil
1129 "*Default input method for multilingual text (a string).
1130 This is the input method activated automatically by the command
1131 `toggle-input-method' (\\[toggle-input-method])."
1132 :group 'mule
1133 :type '(choice (const nil) string)
1134 :set-after '(current-language-environment))
1136 (put 'input-method-function 'permanent-local t)
1138 (defvar input-method-history nil
1139 "History list for some commands that read input methods.")
1140 (make-variable-buffer-local 'input-method-history)
1141 (put 'input-method-history 'permanent-local t)
1143 (defvar inactivate-current-input-method-function nil
1144 "Function to call for inactivating the current input method.
1145 Every input method should set this to an appropriate value when activated.
1146 This function is called with no argument.
1148 This function should never change the value of `current-input-method'.
1149 It is set to nil by the function `inactivate-input-method'.")
1150 (make-variable-buffer-local 'inactivate-current-input-method-function)
1151 (put 'inactivate-current-input-method-function 'permanent-local t)
1153 (defvar describe-current-input-method-function nil
1154 "Function to call for describing the current input method.
1155 This function is called with no argument.")
1156 (make-variable-buffer-local 'describe-current-input-method-function)
1157 (put 'describe-current-input-method-function 'permanent-local t)
1159 (defvar input-method-alist nil
1160 "Alist of input method names vs how to use them.
1161 Each element has the form:
1162 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
1163 See the function `register-input-method' for the meanings of the elements.")
1165 (defun register-input-method (input-method lang-env &rest args)
1166 "Register INPUT-METHOD as an input method for language environment LANG-ENV.
1167 INPUT-METHOD and LANG-ENV are symbols or strings.
1169 The remaining arguments are:
1170 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
1171 ACTIVATE-FUNC is a function to call to activate this method.
1172 TITLE is a string to show in the mode line when this method is active.
1173 DESCRIPTION is a string describing this method and what it is good for.
1174 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
1175 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
1177 This function is mainly used in the file \"leim-list.el\" which is
1178 created at Emacs build time, registering all Quail input methods
1179 contained in the Emacs distribution.
1181 In case you want to register a new Quail input method by yourself, be
1182 careful to use the same input method title as given in the third
1183 parameter of `quail-define-package'. (If the values are different, the
1184 string specified in this function takes precedence.)
1186 The commands `describe-input-method' and `list-input-methods' need
1187 these duplicated values to show some information about input methods
1188 without loading the relevant Quail packages."
1189 (if (symbolp lang-env)
1190 (setq lang-env (symbol-name lang-env)))
1191 (if (symbolp input-method)
1192 (setq input-method (symbol-name input-method)))
1193 (let ((info (cons lang-env args))
1194 (slot (assoc input-method input-method-alist)))
1195 (if slot
1196 (setcdr slot info)
1197 (setq slot (cons input-method info))
1198 (setq input-method-alist (cons slot input-method-alist)))))
1200 (defun read-input-method-name (prompt &optional default inhibit-null)
1201 "Read a name of input method from a minibuffer prompting with PROMPT.
1202 If DEFAULT is non-nil, use that as the default,
1203 and substitute it into PROMPT at the first `%s'.
1204 If INHIBIT-NULL is non-nil, null input signals an error.
1206 The return value is a string."
1207 (if default
1208 (setq prompt (format prompt default)))
1209 (let* ((completion-ignore-case t)
1210 ;; As it is quite normal to change input method in the
1211 ;; minibuffer, we must enable it even if
1212 ;; enable-recursive-minibuffers is currently nil.
1213 (enable-recursive-minibuffers t)
1214 ;; This binding is necessary because input-method-history is
1215 ;; buffer local.
1216 (input-method (completing-read prompt input-method-alist
1217 nil t nil 'input-method-history
1218 default)))
1219 (if (and input-method (symbolp input-method))
1220 (setq input-method (symbol-name input-method)))
1221 (if (> (length input-method) 0)
1222 input-method
1223 (if inhibit-null
1224 (error "No valid input method is specified")))))
1226 (defun activate-input-method (input-method)
1227 "Switch to input method INPUT-METHOD for the current buffer.
1228 If some other input method is already active, turn it off first.
1229 If INPUT-METHOD is nil, deactivate any current input method."
1230 (if (and input-method (symbolp input-method))
1231 (setq input-method (symbol-name input-method)))
1232 (if (and current-input-method
1233 (not (string= current-input-method input-method)))
1234 (inactivate-input-method))
1235 (unless (or current-input-method (null input-method))
1236 (let ((slot (assoc input-method input-method-alist)))
1237 (if (null slot)
1238 (error "Can't activate input method `%s'" input-method))
1239 (setq current-input-method-title nil)
1240 (let ((func (nth 2 slot)))
1241 (if (functionp func)
1242 (apply (nth 2 slot) input-method (nthcdr 5 slot))
1243 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
1244 (progn
1245 (require (cdr func))
1246 (apply (car func) input-method (nthcdr 5 slot)))
1247 (error "Can't activate input method `%s'" input-method))))
1248 (setq current-input-method input-method)
1249 (or (stringp current-input-method-title)
1250 (setq current-input-method-title (nth 3 slot)))
1251 (unwind-protect
1252 (run-hooks 'input-method-activate-hook)
1253 (force-mode-line-update)))))
1255 (defun inactivate-input-method ()
1256 "Turn off the current input method."
1257 (when current-input-method
1258 (if input-method-history
1259 (unless (string= current-input-method (car input-method-history))
1260 (setq input-method-history
1261 (cons current-input-method
1262 (delete current-input-method input-method-history))))
1263 (setq input-method-history (list current-input-method)))
1264 (unwind-protect
1265 (funcall inactivate-current-input-method-function)
1266 (unwind-protect
1267 (run-hooks 'input-method-inactivate-hook)
1268 (setq current-input-method nil
1269 input-method-function nil
1270 current-input-method-title nil)
1271 (force-mode-line-update)))))
1273 (defun set-input-method (input-method)
1274 "Select and activate input method INPUT-METHOD for the current buffer.
1275 This also sets the default input method to the one you specify.
1276 If INPUT-METHOD is nil, this function turns off the input method, and
1277 also causes you to be prompted for a name of an input method the next
1278 time you invoke \\[toggle-input-method].
1280 To deactivate the input method interactively, use \\[toggle-input-method].
1281 To deactivate it programmatically, use \\[inactivate-input-method]."
1282 (interactive
1283 (let* ((default (or (car input-method-history) default-input-method)))
1284 (list (read-input-method-name
1285 (if default "Select input method (default %s): " "Select input method: ")
1286 default t))))
1287 (activate-input-method input-method)
1288 (setq default-input-method input-method)
1289 (when (interactive-p)
1290 (customize-mark-as-set 'default-input-method))
1291 default-input-method)
1293 (defun toggle-input-method (&optional arg)
1294 "Enable or disable multilingual text input method for the current buffer.
1295 Only one input method can be enabled at any time in a given buffer.
1297 The normal action is to enable an input method if none was
1298 enabled, and disable the current one otherwise. Which input method
1299 to enable can be determined in various ways--either the one most
1300 recently used, or the one specified by `default-input-method', or
1301 as a last resort by reading the name of an input method in the
1302 minibuffer.
1304 With a prefix argument, read an input method name with the minibuffer
1305 and enable that one. The default is the most recent input method specified
1306 \(not including the currently active input method, if any)."
1308 (interactive "P")
1309 (if (and current-input-method (not arg))
1310 (inactivate-input-method)
1311 (let ((default (or (car input-method-history) default-input-method)))
1312 (if (and arg default (equal current-input-method default)
1313 (> (length input-method-history) 1))
1314 (setq default (nth 1 input-method-history)))
1315 (activate-input-method
1316 (if (or arg (not default))
1317 (progn
1318 (read-input-method-name
1319 (if default "Input method (default %s): " "Input method: " )
1320 default t))
1321 default))
1322 (unless default-input-method
1323 (prog1
1324 (setq default-input-method current-input-method)
1325 (when (interactive-p)
1326 (customize-mark-as-set 'default-input-method)))))))
1328 (defun describe-input-method (input-method)
1329 "Describe input method INPUT-METHOD."
1330 (interactive
1331 (list (read-input-method-name
1332 "Describe input method (default, current choice): ")))
1333 (if (and input-method (symbolp input-method))
1334 (setq input-method (symbol-name input-method)))
1335 (help-setup-xref (list #'describe-input-method
1336 (or input-method current-input-method))
1337 (interactive-p))
1339 (if (null input-method)
1340 (describe-current-input-method)
1341 (let ((current current-input-method))
1342 (condition-case nil
1343 (progn
1344 (save-excursion
1345 (activate-input-method input-method)
1346 (describe-current-input-method))
1347 (activate-input-method current))
1348 (error
1349 (activate-input-method current)
1350 (help-setup-xref (list #'describe-input-method input-method)
1351 (interactive-p))
1352 (with-output-to-temp-buffer (help-buffer)
1353 (let ((elt (assoc input-method input-method-alist)))
1354 (princ (format
1355 "Input method: %s (`%s' in mode line) for %s\n %s\n"
1356 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))))))
1358 (defun describe-current-input-method ()
1359 "Describe the input method currently in use.
1360 This is a subroutine for `describe-input-method'."
1361 (if current-input-method
1362 (if (and (symbolp describe-current-input-method-function)
1363 (fboundp describe-current-input-method-function))
1364 (funcall describe-current-input-method-function)
1365 (message "No way to describe the current input method `%s'"
1366 current-input-method)
1367 (ding))
1368 (error "No input method is activated now")))
1370 (defun read-multilingual-string (prompt &optional initial-input input-method)
1371 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1372 The input method selected last time is activated in minibuffer.
1373 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1374 initially.
1375 Optional 3rd argument INPUT-METHOD specifies the input method
1376 to be activated instead of the one selected last time. It is a symbol
1377 or a string."
1378 (setq input-method
1379 (or input-method
1380 current-input-method
1381 default-input-method
1382 (read-input-method-name "Input method: " nil t)))
1383 (if (and input-method (symbolp input-method))
1384 (setq input-method (symbol-name input-method)))
1385 (let ((prev-input-method current-input-method))
1386 (unwind-protect
1387 (progn
1388 (activate-input-method input-method)
1389 (read-string prompt initial-input nil nil t))
1390 (activate-input-method prev-input-method))))
1392 ;; Variables to control behavior of input methods. All input methods
1393 ;; should react to these variables.
1395 (defcustom input-method-verbose-flag 'default
1396 "*A flag to control extra guidance given by input methods.
1397 The value should be nil, t, `complex-only', or `default'.
1399 The extra guidance is done by showing list of available keys in echo
1400 area. When you use the input method in the minibuffer, the guidance
1401 is shown at the bottom short window (split from the existing window).
1403 If the value is t, extra guidance is always given, if the value is
1404 nil, extra guidance is always suppressed.
1406 If the value is `complex-only', only complex input methods such as
1407 `chinese-py' and `japanese' give extra guidance.
1409 If the value is `default', complex input methods always give extra
1410 guidance, but simple input methods give it only when you are not in
1411 the minibuffer.
1413 See also the variable `input-method-highlight-flag'."
1414 :type '(choice (const :tag "Always" t) (const :tag "Never" nil)
1415 (const complex-only) (const default))
1416 :group 'mule)
1418 (defcustom input-method-highlight-flag t
1419 "*If this flag is non-nil, input methods highlight partially-entered text.
1420 For instance, while you are in the middle of a Quail input method sequence,
1421 the text inserted so far is temporarily underlined.
1422 The underlining goes away when you finish or abort the input method sequence.
1423 See also the variable `input-method-verbose-flag'."
1424 :type 'boolean
1425 :group 'mule)
1427 (defvar input-method-activate-hook nil
1428 "Normal hook run just after an input method is activated.
1430 The variable `current-input-method' keeps the input method name
1431 just activated.")
1433 (defvar input-method-inactivate-hook nil
1434 "Normal hook run just after an input method is inactivated.
1436 The variable `current-input-method' still keeps the input method name
1437 just inactivated.")
1439 (defvar input-method-after-insert-chunk-hook nil
1440 "Normal hook run just after an input method insert some chunk of text.")
1442 (defvar input-method-exit-on-first-char nil
1443 "This flag controls when an input method returns.
1444 Usually, the input method does not return while there's a possibility
1445 that it may find a different translation if a user types another key.
1446 But, it this flag is non-nil, the input method returns as soon as
1447 the current key sequence gets long enough to have some valid translation.")
1449 (defvar input-method-use-echo-area nil
1450 "This flag controls how an input method shows an intermediate key sequence.
1451 Usually, the input method inserts the intermediate key sequence,
1452 or candidate translations corresponding to the sequence,
1453 at point in the current buffer.
1454 But, if this flag is non-nil, it displays them in echo area instead.")
1456 (defvar input-method-exit-on-invalid-key nil
1457 "This flag controls the behaviour of an input method on invalid key input.
1458 Usually, when a user types a key which doesn't start any character
1459 handled by the input method, the key is handled by turning off the
1460 input method temporarily. After that key, the input method is re-enabled.
1461 But, if this flag is non-nil, the input method is never back on.")
1464 (defvar set-language-environment-hook nil
1465 "Normal hook run after some language environment is set.
1467 When you set some hook function here, that effect usually should not
1468 be inherited to another language environment. So, you had better set
1469 another function in `exit-language-environment-hook' (which see) to
1470 cancel the effect.")
1472 (defvar exit-language-environment-hook nil
1473 "Normal hook run after exiting from some language environment.
1474 When this hook is run, the variable `current-language-environment'
1475 is still bound to the language environment being exited.
1477 This hook is mainly used for canceling the effect of
1478 `set-language-environment-hook' (which-see).")
1480 (put 'setup-specified-language-environment 'apropos-inhibit t)
1482 (defun setup-specified-language-environment ()
1483 "Switch to a specified language environment."
1484 (interactive)
1485 (let (language-name)
1486 (if (and (symbolp last-command-event)
1487 (or (not (eq last-command-event 'Default))
1488 (setq last-command-event 'English))
1489 (setq language-name (symbol-name last-command-event)))
1490 (prog1
1491 (set-language-environment language-name)
1492 (customize-mark-as-set 'current-language-environment))
1493 (error "Bogus calling sequence"))))
1495 (defcustom current-language-environment "English"
1496 "The last language environment specified with `set-language-environment'.
1497 This variable should be set only with \\[customize], which is equivalent
1498 to using the function `set-language-environment'."
1499 :link '(custom-manual "(emacs)Language Environments")
1500 :set (lambda (symbol value) (set-language-environment value))
1501 :get (lambda (x)
1502 (or (car-safe (assoc-ignore-case
1503 (if (symbolp current-language-environment)
1504 (symbol-name current-language-environment)
1505 current-language-environment)
1506 language-info-alist))
1507 "English"))
1508 ;; custom type will be updated with `set-language-info'.
1509 :type (if language-info-alist
1510 (cons 'choice (mapcar
1511 (lambda (lang)
1512 (list 'const (car lang)))
1513 (sort (copy-sequence language-info-alist)
1514 (lambda (x y) (string< (car x) (car y))))))
1515 'string)
1516 :initialize 'custom-initialize-default
1517 :group 'mule)
1519 (defun reset-language-environment ()
1520 "Reset multilingual environment of Emacs to the default status.
1522 The default status is as follows:
1524 The default value of `buffer-file-coding-system' is nil.
1525 The default coding system for process I/O is nil.
1526 The default value for the command `set-terminal-coding-system' is nil.
1527 The default value for the command `set-keyboard-coding-system' is nil.
1529 The order of priorities of coding categories and the coding system
1530 bound to each category are as follows
1531 coding category coding system
1532 --------------------------------------------------
1533 coding-category-iso-8-2 iso-latin-1
1534 coding-category-iso-8-1 iso-latin-1
1535 coding-category-iso-7-tight iso-2022-jp
1536 coding-category-iso-7 iso-2022-7bit
1537 coding-category-iso-7-else iso-2022-7bit-lock
1538 coding-category-iso-8-else iso-2022-8bit-ss2
1539 coding-category-emacs-mule emacs-mule
1540 coding-category-raw-text raw-text
1541 coding-category-sjis japanese-shift-jis
1542 coding-category-big5 chinese-big5
1543 coding-category-ccl nil
1544 coding-category-binary no-conversion
1545 coding-category-utf-16-be nil
1546 coding-category-utf-16-le nil
1547 coding-category-utf-8 mule-utf-8"
1548 (interactive)
1549 ;; This function formerly set default-enable-multibyte-characters to t,
1550 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1552 (setq coding-category-iso-7-tight 'iso-2022-jp
1553 coding-category-iso-7 'iso-2022-7bit
1554 coding-category-iso-8-1 'iso-latin-1
1555 coding-category-iso-8-2 'iso-latin-1
1556 coding-category-iso-7-else 'iso-2022-7bit-lock
1557 coding-category-iso-8-else 'iso-2022-8bit-ss2
1558 coding-category-emacs-mule 'emacs-mule
1559 coding-category-raw-text 'raw-text
1560 coding-category-sjis 'japanese-shift-jis
1561 coding-category-big5 'chinese-big5
1562 coding-category-utf-16-be nil
1563 coding-category-utf-16-le nil
1564 coding-category-utf-8 'mule-utf-8
1565 coding-category-ccl nil
1566 coding-category-binary 'no-conversion)
1568 (set-coding-priority
1569 '(coding-category-iso-8-1
1570 coding-category-iso-8-2
1571 coding-category-iso-7-tight
1572 coding-category-iso-7
1573 coding-category-iso-7-else
1574 coding-category-iso-8-else
1575 coding-category-emacs-mule
1576 coding-category-raw-text
1577 coding-category-sjis
1578 coding-category-big5
1579 coding-category-ccl
1580 coding-category-binary
1581 coding-category-utf-16-be
1582 coding-category-utf-16-le
1583 coding-category-utf-8))
1585 (update-coding-systems-internal)
1587 (set-default-coding-systems nil)
1588 (setq default-sendmail-coding-system 'iso-latin-1)
1589 ;; Preserve eol-type from existing default-process-coding-systems.
1590 ;; On non-unix-like systems in particular, these may have been set
1591 ;; carefully by the user, or by the startup code, to deal with the
1592 ;; users shell appropriately, so should not be altered by changing
1593 ;; language environment.
1594 (let ((output-coding
1595 ;; When bootstrapping, coding-systems are not defined yet, so
1596 ;; we need to catch the error from check-coding-system.
1597 (condition-case nil
1598 (coding-system-change-text-conversion
1599 (car default-process-coding-system) 'undecided)
1600 (coding-system-error 'undecided)))
1601 (input-coding
1602 (condition-case nil
1603 (coding-system-change-text-conversion
1604 (cdr default-process-coding-system) 'iso-latin-1)
1605 (coding-system-error 'iso-latin-1))))
1606 (setq default-process-coding-system
1607 (cons output-coding input-coding)))
1609 ;; Don't alter the terminal and keyboard coding systems here.
1610 ;; The terminal still supports the same coding system
1611 ;; that it supported a minute ago.
1612 ;; (set-terminal-coding-system-internal nil)
1613 ;; (set-keyboard-coding-system-internal nil)
1615 (setq nonascii-translation-table nil
1616 nonascii-insert-offset 0))
1618 (reset-language-environment)
1620 (defun set-display-table-and-terminal-coding-system (language-name)
1621 "Set up the display table and terminal coding system for LANGUAGE-NAME."
1622 (let ((coding (get-language-info language-name 'unibyte-display)))
1623 (if coding
1624 (standard-display-european-internal)
1625 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1626 (aset standard-display-table 146 nil))
1627 (or (eq window-system 'pc)
1628 (set-terminal-coding-system coding))))
1630 (defun set-language-environment (language-name)
1631 "Set up multi-lingual environment for using LANGUAGE-NAME.
1632 This sets the coding system priority and the default input method
1633 and sometimes other things. LANGUAGE-NAME should be a string
1634 which is the name of a language environment. For example, \"Latin-1\"
1635 specifies the character set for the major languages of Western Europe."
1636 (interactive (list (read-language-name
1638 "Set language environment (default, English): ")))
1639 (if language-name
1640 (if (symbolp language-name)
1641 (setq language-name (symbol-name language-name)))
1642 (setq language-name "English"))
1643 (or (assoc-ignore-case language-name language-info-alist)
1644 (error "Language environment not defined: %S" language-name))
1645 (if current-language-environment
1646 (let ((func (get-language-info current-language-environment
1647 'exit-function)))
1648 (run-hooks 'exit-language-environment-hook)
1649 (if (functionp func) (funcall func))))
1650 (let ((default-eol-type (coding-system-eol-type
1651 default-buffer-file-coding-system)))
1652 (reset-language-environment)
1654 ;; The features might set up coding systems.
1655 (let ((required-features (get-language-info language-name 'features)))
1656 (while required-features
1657 (require (car required-features))
1658 (setq required-features (cdr required-features))))
1660 (setq current-language-environment language-name)
1661 (set-language-environment-coding-systems language-name default-eol-type))
1662 (let ((input-method (get-language-info language-name 'input-method)))
1663 (when input-method
1664 (setq default-input-method input-method)
1665 (if input-method-history
1666 (setq input-method-history
1667 (cons input-method
1668 (delete input-method input-method-history))))))
1669 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1670 (dos-table
1671 (if (eq window-system 'pc)
1672 (intern
1673 (format "cp%d-nonascii-translation-table" dos-codepage)))))
1674 (cond
1675 ((char-table-p nonascii)
1676 (setq nonascii-translation-table nonascii))
1677 ((and (eq window-system 'pc) (boundp dos-table))
1678 ;; DOS terminals' default is to use a special non-ASCII translation
1679 ;; table as appropriate for the installed codepage.
1680 (setq nonascii-translation-table (symbol-value dos-table)))
1681 ((charsetp nonascii)
1682 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1684 ;; Unibyte setups if necessary.
1685 (unless default-enable-multibyte-characters
1686 ;; Syntax and case table.
1687 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1688 (if syntax
1689 (let ((set-case-syntax-set-multibyte nil))
1690 (load syntax nil t))
1691 ;; No information for syntax and case. Reset to the defaults.
1692 (let ((syntax-table (standard-syntax-table))
1693 (case-table (standard-case-table))
1694 (ch (if (eq window-system 'pc) 128 160)))
1695 (while (< ch 256)
1696 (modify-syntax-entry ch " " syntax-table)
1697 (aset case-table ch ch)
1698 (setq ch (1+ ch)))
1699 (set-char-table-extra-slot case-table 0 nil)
1700 (set-char-table-extra-slot case-table 1 nil)
1701 (set-char-table-extra-slot case-table 2 nil))
1702 (set-standard-case-table (standard-case-table))
1703 (let ((list (buffer-list)))
1704 (while list
1705 (with-current-buffer (car list)
1706 (set-case-table (standard-case-table)))
1707 (setq list (cdr list))))))
1708 (set-display-table-and-terminal-coding-system language-name))
1710 (let ((required-features (get-language-info language-name 'features)))
1711 (while required-features
1712 (require (car required-features))
1713 (setq required-features (cdr required-features))))
1714 (let ((func (get-language-info language-name 'setup-function)))
1715 (if (functionp func)
1716 (funcall func)))
1717 (run-hooks 'set-language-environment-hook)
1718 (force-mode-line-update t))
1720 (defun standard-display-european-internal ()
1721 ;; Actually set up direct output of non-ASCII characters.
1722 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1723 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1724 ;; the native font, and codes 160 and 146 stand for something very
1725 ;; different there.
1726 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1727 (progn
1728 ;; Make non-line-break space display as a plain space.
1729 ;; Most X fonts do the wrong thing for code 160.
1730 (aset standard-display-table 160 [32])
1731 ;; With luck, non-Latin-1 fonts are more recent and so don't
1732 ;; have this bug.
1733 (aset standard-display-table 2208 [32]) ; Latin-1 NBSP
1734 ;; Most Windows programs send out apostrophes as \222. Most X fonts
1735 ;; don't contain a character at that position. Map it to the ASCII
1736 ;; apostrophe. [This is actually RIGHT SINGLE QUOTATION MARK,
1737 ;; U+2019, normally from the windows-1252 character set. XFree 4
1738 ;; fonts probably have the appropriate glyph at this position,
1739 ;; so they could use standard-display-8bit. It's better to use a
1740 ;; proper windows-1252 coding system. --fx]
1741 (aset standard-display-table 146 [39]))))
1743 (defun set-language-environment-coding-systems (language-name
1744 &optional eol-type)
1745 "Do various coding system setups for language environment LANGUAGE-NAME.
1747 The optional arg EOL-TYPE specifies the eol-type of the default value
1748 of `buffer-file-coding-system' set by this function."
1749 (let* ((priority (get-language-info language-name 'coding-priority))
1750 (default-coding (car priority)))
1751 (if priority
1752 (let ((categories (mapcar 'coding-system-category priority)))
1753 (set-default-coding-systems
1754 (if (memq eol-type '(0 1 2 unix dos mac))
1755 (coding-system-change-eol-conversion default-coding eol-type)
1756 default-coding))
1757 (setq default-sendmail-coding-system default-coding)
1758 (set-coding-priority categories)
1759 (while priority
1760 (set (car categories) (car priority))
1761 (setq priority (cdr priority) categories (cdr categories)))
1762 (update-coding-systems-internal)))))
1764 (defsubst princ-list (&rest args)
1765 "Print all arguments with `princ', then print \"\n\"."
1766 (while args (princ (car args)) (setq args (cdr args)))
1767 (princ "\n"))
1769 (put 'describe-specified-language-support 'apropos-inhibit t)
1771 ;; Print a language specific information such as input methods,
1772 ;; charsets, and coding systems. This function is intended to be
1773 ;; called from the menu:
1774 ;; [menu-bar mule describe-language-environment LANGUAGE]
1775 ;; and should not run it by `M-x describe-current-input-method-function'.
1776 (defun describe-specified-language-support ()
1777 "Describe how Emacs supports the specified language environment."
1778 (interactive)
1779 (let (language-name)
1780 (if (not (and (symbolp last-command-event)
1781 (or (not (eq last-command-event 'Default))
1782 (setq last-command-event 'English))
1783 (setq language-name (symbol-name last-command-event))))
1784 (error "Bogus calling sequence"))
1785 (describe-language-environment language-name)))
1787 (defun describe-language-environment (language-name)
1788 "Describe how Emacs supports language environment LANGUAGE-NAME."
1789 (interactive
1790 (list (read-language-name
1791 'documentation
1792 "Describe language environment (default, current choice): ")))
1793 (if (null language-name)
1794 (setq language-name current-language-environment))
1795 (if (or (null language-name)
1796 (null (get-language-info language-name 'documentation)))
1797 (error "No documentation for the specified language"))
1798 (if (symbolp language-name)
1799 (setq language-name (symbol-name language-name)))
1800 (let ((doc (get-language-info language-name 'documentation))
1801 pos)
1802 (help-setup-xref (list #'describe-language-environment language-name)
1803 (interactive-p))
1804 (with-output-to-temp-buffer (help-buffer)
1805 (save-excursion
1806 (set-buffer standard-output)
1807 (insert language-name " language environment\n\n")
1808 (if (stringp doc)
1809 (insert doc "\n\n"))
1810 (condition-case nil
1811 (let ((str (eval (get-language-info language-name 'sample-text))))
1812 (if (stringp str)
1813 (insert "Sample text:\n " str "\n\n")))
1814 (error nil))
1815 (let ((input-method (get-language-info language-name 'input-method))
1816 (l (copy-sequence input-method-alist)))
1817 (insert "Input methods")
1818 (when input-method
1819 (insert " (default, " input-method ")")
1820 (setq input-method (assoc input-method input-method-alist))
1821 (setq l (cons input-method (delete input-method l))))
1822 (insert ":\n")
1823 (while l
1824 (when (string= language-name (nth 1 (car l)))
1825 (insert " " (car (car l)))
1826 (search-backward (car (car l)))
1827 (help-xref-button 0 'help-input-method (car (car l)))
1828 (goto-char (point-max))
1829 (insert " (\""
1830 (if (stringp (nth 3 (car l)))
1831 (nth 3 (car l))
1832 (car (nth 3 (car l))))
1833 "\" in mode line)\n"))
1834 (setq l (cdr l)))
1835 (insert "\n"))
1836 (insert "Character sets:\n")
1837 (let ((l (get-language-info language-name 'charset)))
1838 (if (null l)
1839 (insert " nothing specific to " language-name "\n")
1840 (while l
1841 (insert " " (symbol-name (car l)))
1842 (search-backward (symbol-name (car l)))
1843 (help-xref-button 0 'help-character-set (car l))
1844 (goto-char (point-max))
1845 (insert ": " (charset-description (car l)) "\n")
1846 (setq l (cdr l)))))
1847 (insert "\n")
1848 (insert "Coding systems:\n")
1849 (let ((l (get-language-info language-name 'coding-system)))
1850 (if (null l)
1851 (insert " nothing specific to " language-name "\n")
1852 (while l
1853 (insert " " (symbol-name (car l)))
1854 (search-backward (symbol-name (car l)))
1855 (help-xref-button 0 'help-coding-system (car l))
1856 (goto-char (point-max))
1857 (insert " (`"
1858 (coding-system-mnemonic (car l))
1859 "' in mode line):\n\t"
1860 (coding-system-doc-string (car l))
1861 "\n")
1862 (let ((aliases (coding-system-get (car l)
1863 'alias-coding-systems)))
1864 (when aliases
1865 (insert "\t(alias:")
1866 (while aliases
1867 (insert " " (symbol-name (car aliases)))
1868 (setq aliases (cdr aliases)))
1869 (insert ")\n")))
1870 (setq l (cdr l)))))))))
1872 ;;; Locales.
1874 (defvar locale-translation-file-name nil
1875 "File name for the system's file of locale-name aliases, or nil if none.")
1877 ;; The following definitions might as well be marked as constants and
1878 ;; purecopied, since they're normally used on startup, and probably
1879 ;; should reflect the facilities of the base Emacs.
1880 (defconst locale-language-names
1881 (purecopy
1883 ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER]
1884 ;; as specified in the Single Unix Spec, Version 2.
1885 ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F)
1886 ;; with additions from ISO 639/RA Newsletter No.1/1989;
1887 ;; see Internet RFC 2165 (1997-06) and
1888 ;; http://www.evertype.com/standards/iso639/iso639-en.html
1889 ;; TERRITORY is a country code taken from ISO 3166
1890 ;; http://www.din.de/gremien/nas/nabd/iso3166ma/codlstp1/en_listp1.html.
1891 ;; CODESET and MODIFIER are implementation-dependent.
1893 ; aa Afar
1894 ; ab Abkhazian
1895 ("af" . "Latin-1") ; Afrikaans
1896 ("am" . "Ethiopic") ; Amharic
1897 ; ar Arabic glibc uses 8859-6
1898 ; as Assamese
1899 ; ay Aymara
1900 ; az Azerbaijani
1901 ; ba Bashkir
1902 ("be" . "Belarusian") ; Belarusian [Byelorussian until early 1990s]
1903 ("bg" . "Bulgarian") ; Bulgarian
1904 ; bh Bihari
1905 ; bi Bislama
1906 ; bn Bengali, Bangla
1907 ("bo" . "Tibetan")
1908 ("br" . "Latin-1") ; Breton
1909 ("bs" . "Latin-2") ; Bosnian
1910 ("ca" . "Latin-1") ; Catalan
1911 ; co Corsican
1912 ("cs" . "Czech")
1913 ("cy" . "Welsh") ; Welsh
1914 ("da" . "Latin-1") ; Danish
1915 ("de" . "German")
1916 ; dz Bhutani
1917 ("el" . "Greek")
1918 ;; Users who specify "en" explicitly typically want Latin-1, not ASCII.
1919 ;; That's actually what the GNU locales define, modulo things like
1920 ;; en_IN -- fx.
1921 ("en" . "Latin-1") ; English
1922 ("eo" . "Latin-3") ; Esperanto
1923 ("es" . "Spanish")
1924 ("et" . "Latin-4") ; Estonian
1925 ("eu" . "Latin-1") ; Basque
1926 ; fa Persian glibc uses utf-8
1927 ("fi" . "Latin-1") ; Finnish
1928 ; fj Fiji
1929 ("fo" . "Latin-1") ; Faroese
1930 ("fr" . "French") ; French
1931 ("fy" . "Latin-1") ; Frisian
1932 ("ga" . "Latin-1") ; Irish Gaelic (new orthography)
1933 ("gd" . "Latin-1") ; Scots Gaelic
1934 ("gl" . "Latin-1") ; Galician
1935 ; gn Guarani
1936 ; gu Gujarati
1937 ("gv" . "Latin-8") ; Manx Gaelic glibc uses 8859-1
1938 ; ha Hausa
1939 ("he" . "Hebrew")
1940 ("hi" . "Devanagari") ; Hindi glibc uses utf-8
1941 ("hr" . "Latin-2") ; Croatian
1942 ("hu" . "Latin-2") ; Hungarian
1943 ; hy Armenian
1944 ; ia Interlingua
1945 ("id" . "Latin-1") ; Indonesian
1946 ; ie Interlingue
1947 ; ik Inupiak
1948 ("is" . "Latin-1") ; Icelandic
1949 ("it" . "Latin-1") ; Italian
1950 ; iu Inuktitut
1951 ("ja" . "Japanese")
1952 ; jw Javanese
1953 ("ka" . "Georgian") ; Georgian
1954 ; kk Kazakh
1955 ("kl" . "Latin-1") ; Greenlandic
1956 ; km Cambodian
1957 ; kn Kannada
1958 ("ko" . "Korean")
1959 ; ks Kashmiri
1960 ; ku Kurdish
1961 ("kw" . "Latin-1") ; Cornish
1962 ; ky Kirghiz
1963 ("la" . "Latin-1") ; Latin
1964 ("lb" . "Latin-1") ; Luxemburgish
1965 ; ln Lingala
1966 ("lo" . "Lao") ; Laothian
1967 ("lt" . "Lithuanian")
1968 ("lv" . "Latvian") ; Latvian, Lettish
1969 ; mg Malagasy
1970 ("mi" . "Latin-7") ; Maori
1971 ("mk" . "Cyrillic-ISO") ; Macedonian
1972 ; ml Malayalam
1973 ; mn Mongolian
1974 ; mo Moldavian
1975 ("mr" . "Devanagari") ; Marathi glibc uses utf-8
1976 ("ms" . "Latin-1") ; Malay
1977 ("mt" . "Latin-3") ; Maltese
1978 ; my Burmese
1979 ; na Nauru
1980 ("ne" . "Devanagari") ; Nepali
1981 ("nl" . "Dutch")
1982 ("no" . "Latin-1") ; Norwegian
1983 ("oc" . "Latin-1") ; Occitan
1984 ; om (Afan) Oromo
1985 ; or Oriya
1986 ; pa Punjabi
1987 ("pl" . "Latin-2") ; Polish
1988 ; ps Pashto, Pushto
1989 ("pt" . "Latin-1") ; Portuguese
1990 ; qu Quechua
1991 ("rm" . "Latin-1") ; Rhaeto-Romanic
1992 ; rn Kirundi
1993 ("ro" . "Romanian")
1994 ("ru.*[_.]koi8" . "Cyrillic-KOI8") ; Russian
1995 ("ru" . "Cyrillic-ISO") ; Russian
1996 ; rw Kinyarwanda
1997 ("sa" . "Devanagari") ; Sanskrit
1998 ; sd Sindhi
1999 ; se Northern Sami
2000 ; sg Sangho
2001 ("sh" . "Latin-2") ; Serbo-Croatian
2002 ; si Sinhalese
2003 ("sk" . "Slovak")
2004 ("sl" . "Slovenian")
2005 ; sm Samoan
2006 ; sn Shona
2007 ; so Somali
2008 ("sq" . "Latin-1") ; Albanian
2009 ("sr" . "Latin-2") ; Serbian (Latin alphabet)
2010 ("sr_YU@cyrillic" . "Cyrillic-ISO") ; per glibc
2011 ; ss Siswati
2012 ; st Sesotho
2013 ; su Sundanese
2014 ("sv" . "Latin-1") ; Swedish
2015 ("sw" . "Latin-1") ; Swahili
2016 ; ta Tamil glibc uses utf-8
2017 ; te Telugu glibc uses utf-8
2018 ("tg" . "Tajik")
2019 ("th" . "Thai")
2020 ; ti Tigrinya
2021 ; tk Turkmen
2022 ("tl" . "Latin-1") ; Tagalog
2023 ; tn Setswana
2024 ; to Tonga
2025 ("tr" . "Latin-5") ; Turkish
2026 ; ts Tsonga
2027 ; tt Tatar
2028 ; tw Twi
2029 ; ug Uighur
2030 ("uk" . "Ukrainian") ; Ukrainian
2031 ; ur Urdu glibc uses utf-8
2032 ("uz" . "Latin-1") ; Uzbek
2033 ("vi" . "Vietnamese") ; glibc uses utf-8
2034 ; vo Volapuk
2035 ("wa" . "Latin-1") ; Walloon
2036 ; wo Wolof
2037 ; xh Xhosa
2038 ("yi" . "Windows-1255") ; Yiddish
2039 ; yo Yoruba
2040 ; za Zhuang
2042 ; glibc:
2043 ; zh_CN.GB18030/GB18030 \
2044 ; zh_CN.GBK/GBK \
2045 ; zh_HK/BIG5-HKSCS \
2046 ; zh_TW/BIG5 \
2047 ; zh_TW.EUC-TW/EUC-TW \
2049 ("zh.*[._]big5" . "Chinese-BIG5")
2050 ("zh.*[._]gbk" . nil) ; Solaris 2.7; has gbk-0 as well as GB 2312.1980-0
2051 ("zh_tw" . "Chinese-CNS")
2052 ("zh" . "Chinese-GB")
2053 ; zu Zulu
2055 ;; ISO standard locales
2056 ("c$" . "ASCII")
2057 ("posix$" . "ASCII")
2059 ;; The "IPA" Emacs language environment does not correspond
2060 ;; to any ISO 639 code, so let it stand for itself.
2061 ("ipa$" . "IPA")
2063 ;; Nonstandard or obsolete language codes
2064 ("cz" . "Czech") ; e.g. Solaris 2.6
2065 ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4
2066 ("iw" . "Hebrew") ; e.g. X11R6.4
2067 ("sp" . "Cyrillic-ISO") ; Serbian (Cyrillic alphabet), e.g. X11R6.4
2068 ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6
2069 ("jp" . "Japanese") ; e.g. MS Windows
2070 ("chs" . "Chinese-GB") ; MS Windows Chinese Simplified
2071 ("cht" . "Chinese-BIG5") ; MS Windows Chinese Traditional
2073 "List of pairs of locale regexps and language names.
2074 The first element whose locale regexp matches the start of a downcased locale
2075 specifies the language name corresponding to that locale.
2076 If the language name is nil, there is no corresponding language environment.")
2078 (defconst locale-charset-language-names
2079 (purecopy
2080 '((".*8859[-_]?1\\>" . "Latin-1")
2081 (".*8859[-_]?2\\>" . "Latin-2")
2082 (".*8859[-_]?3\\>" . "Latin-3")
2083 (".*8859[-_]?4\\>" . "Latin-4")
2084 (".*8859[-_]?9\\>" . "Latin-5")
2085 (".*8859[-_]?14\\>" . "Latin-8")
2086 (".*8859[-_]?15\\>" . "Latin-9")
2087 (".*utf\\(-?8\\)\\>" . "UTF-8")
2088 ;; utf-8@euro exists, so put this last. (@euro really specifies
2089 ;; the currency, rather than the charset.)
2090 (".*@euro\\>" . "Latin-9")))
2091 "List of pairs of locale regexps and charset language names.
2092 The first element whose locale regexp matches the start of a downcased locale
2093 specifies the language name whose charsets corresponds to that locale.
2094 This language name is used if its charsets disagree with the charsets of
2095 the language name that would otherwise be used for this locale.")
2097 (defconst locale-preferred-coding-systems
2098 (purecopy
2099 '(("ja.*[._]euc" . japanese-iso-8bit)
2100 ("ja.*[._]jis7" . iso-2022-jp)
2101 ("ja.*[._]pck" . japanese-shift-jis)
2102 ("ja.*[._]sjis" . japanese-shift-jis)
2103 ("jpn" . japanese-shift-jis) ; MS-Windows uses this.
2104 (".*[._]utf" . utf-8)))
2105 "List of pairs of locale regexps and preferred coding systems.
2106 The first element whose locale regexp matches the start of a downcased locale
2107 specifies the coding system to prefer when using that locale.")
2109 (defconst standard-keyboard-coding-systems
2110 (purecopy
2111 '(iso-latin-1 iso-latin-2 iso-latin-3 iso-latin-4 iso-latin-5
2112 iso-latin-6 iso-latin-7 iso-latin-8 iso-latin-9 koi8-u koi8-r))
2113 "Coding systems that are commonly used for keyboards.
2114 `set-locale-environment' will set the `keyboard-coding-system' if the
2115 coding-system specified by the locale setting is a member of this list.")
2117 (defun locale-name-match (key alist)
2118 "Search for KEY in ALIST, which should be a list of regexp-value pairs.
2119 Return the value corresponding to the first regexp that matches the
2120 start of KEY, or nil if there is no match."
2121 (let (element)
2122 (while (and alist (not element))
2123 (if (string-match (concat "\\`\\(?:" (car (car alist)) "\\)") key)
2124 (setq element (car alist)))
2125 (setq alist (cdr alist)))
2126 (cdr element)))
2128 (defun set-locale-environment (&optional locale-name)
2129 "Set up multi-lingual environment for using LOCALE-NAME.
2130 This sets the language environment, the coding system priority,
2131 the default input method and sometimes other things.
2133 LOCALE-NAME should be a string
2134 which is the name of a locale supported by the system;
2135 often it is of the form xx_XX.CODE, where xx is a language,
2136 XX is a country, and CODE specifies a character set and coding system.
2137 For example, the locale name \"ja_JP.EUC\" might name a locale
2138 for Japanese in Japan using the `japanese-iso-8bit' coding-system.
2140 If LOCALE-NAME is nil, its value is taken from the environment
2141 variables LC_ALL, LC_CTYPE and LANG (the first one that is set).
2143 The locale names supported by your system can typically be found in a
2144 directory named `/usr/share/locale' or `/usr/lib/locale'. LOCALE-NAME
2145 will be translated according to the table specified by
2146 `locale-translation-file-name'.
2148 See also `locale-charset-language-names', `locale-language-names',
2149 `locale-preferred-coding-systems' and `locale-coding-system'."
2150 (interactive "sSet environment for locale: ")
2152 ;; Do this at runtime for the sake of binaries possibly transported
2153 ;; to a system without X.
2154 (setq locale-translation-file-name
2155 (let ((files
2156 '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4
2157 "/usr/X11R6/lib/X11/locale/locale.alias" ; e.g. RedHat 4.2
2158 "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6
2160 ;; The following name appears after the X-related names above,
2161 ;; since the X-related names are what X actually uses.
2162 "/usr/share/locale/locale.alias" ; GNU/Linux sans X
2164 (while (and files (not (file-exists-p (car files))))
2165 (setq files (cdr files)))
2166 (car files)))
2168 (let ((locale locale-name))
2170 (unless locale
2171 ;; Use the first of these three environment variables
2172 ;; that has a nonempty value.
2173 (let ((vars '("LC_ALL" "LC_CTYPE" "LANG")))
2174 (while (and vars (not (setq locale (getenv (car vars)))))
2175 (setq vars (cdr vars)))))
2177 (when locale
2179 ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on,
2180 ;; using the translation file that many systems have.
2181 (when locale-translation-file-name
2182 (with-temp-buffer
2183 (insert-file-contents locale-translation-file-name)
2184 (when (re-search-forward
2185 (concat "^" (regexp-quote locale) ":?[ \t]+") nil t)
2186 (setq locale (buffer-substring (point) (line-end-position))))))
2188 ;; Leave the system locales alone if the caller did not specify
2189 ;; an explicit locale name, as their defaults are set from
2190 ;; LC_MESSAGES and LC_TIME, not LC_CTYPE, and the user might not
2191 ;; want to set them to the same value as LC_CTYPE.
2192 (when locale-name
2193 (setq system-messages-locale locale)
2194 (setq system-time-locale locale))
2196 (setq locale (downcase locale))
2198 (let ((language-name
2199 (locale-name-match locale locale-language-names))
2200 (charset-language-name
2201 (locale-name-match locale locale-charset-language-names))
2202 (coding-system
2203 (locale-name-match locale locale-preferred-coding-systems)))
2205 ;; Give preference to charset-language-name over language-name.
2206 (if (and charset-language-name
2207 (not
2208 (equal (get-language-info language-name 'charset)
2209 (get-language-info charset-language-name 'charset))))
2210 (setq language-name charset-language-name))
2212 (when language-name
2214 ;; Set up for this character set. This is now the right way
2215 ;; to do it for both unibyte and multibyte modes.
2216 (set-language-environment language-name)
2218 ;; If default-enable-multibyte-characters is nil,
2219 ;; we are using single-byte characters,
2220 ;; so the display table and terminal coding system are irrelevant.
2221 (when default-enable-multibyte-characters
2222 (set-display-table-and-terminal-coding-system language-name))
2224 ;; Set the `keyboard-coding-system' if appropriate.
2225 (let ((kcs (or coding-system
2226 (car (get-language-info language-name
2227 'coding-system)))))
2228 (if (memq kcs standard-keyboard-coding-systems)
2229 (set-keyboard-coding-system kcs)))
2231 (setq locale-coding-system
2232 (car (get-language-info language-name 'coding-priority))))
2234 (when coding-system
2235 (prefer-coding-system coding-system)
2236 (setq locale-coding-system coding-system))))))
2238 ;;; Charset property
2240 (defun get-charset-property (charset propname)
2241 "Return the value of CHARSET's PROPNAME property.
2242 This is the last value stored with
2243 (put-charset-property CHARSET PROPNAME VALUE)."
2244 (and (not (eq charset 'composition))
2245 (plist-get (charset-plist charset) propname)))
2247 (defun put-charset-property (charset propname value)
2248 "Store CHARSETS's PROPNAME property with value VALUE.
2249 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
2250 (or (eq charset 'composition)
2251 (set-charset-plist charset
2252 (plist-put (charset-plist charset) propname value))))
2254 ;;; Character code property
2255 (put 'char-code-property-table 'char-table-extra-slots 0)
2257 (defvar char-code-property-table
2258 (make-char-table 'char-code-property-table)
2259 "Char-table containing a property list of each character code.
2261 See also the documentation of `get-char-code-property' and
2262 `put-char-code-property'.")
2264 (defun get-char-code-property (char propname)
2265 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
2266 (let ((plist (aref char-code-property-table char)))
2267 (if (listp plist)
2268 (car (cdr (memq propname plist))))))
2270 (defun put-char-code-property (char propname value)
2271 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
2272 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
2273 (let ((plist (aref char-code-property-table char)))
2274 (if plist
2275 (let ((slot (memq propname plist)))
2276 (if slot
2277 (setcar (cdr slot) value)
2278 (nconc plist (list propname value))))
2279 (aset char-code-property-table char (list propname value)))))
2282 ;; Pretty description of encoded string
2284 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
2285 (defvar iso-2022-control-alist
2286 '((?\x1b . "ESC")
2287 (?\x0e . "SO")
2288 (?\x0f . "SI")
2289 (?\x8e . "SS2")
2290 (?\x8f . "SS3")
2291 (?\x9b . "CSI")))
2293 (defun encoded-string-description (str coding-system)
2294 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
2295 (setq str (string-as-unibyte str))
2296 (mapconcat
2297 (if (and coding-system (eq (coding-system-type coding-system) 2))
2298 ;; Try to get a pretty description for ISO 2022 escape sequences.
2299 (function (lambda (x) (or (cdr (assq x iso-2022-control-alist))
2300 (format "%02X" x))))
2301 (function (lambda (x) (format "0x%02X" x))))
2302 str " "))
2304 (defun encode-coding-char (char coding-system)
2305 "Encode CHAR by CODING-SYSTEM and return the resulting string.
2306 If CODING-SYSTEM can't safely encode CHAR, return nil."
2307 (let ((str1 (string-as-multibyte (char-to-string char)))
2308 (str2 (string-as-multibyte (make-string 2 char)))
2309 (safe-chars (and coding-system
2310 (coding-system-get coding-system 'safe-chars)))
2311 (charset (char-charset char))
2312 enc1 enc2 i1 i2)
2313 (when (or (eq safe-chars t)
2314 (eq charset 'ascii)
2315 (and safe-chars (aref safe-chars char)))
2316 ;; We must find the encoded string of CHAR. But, just encoding
2317 ;; CHAR will put extra control sequences (usually to designate
2318 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
2319 ;; To exclude such tailing bytes, we at first encode one-char
2320 ;; string and two-char string, then check how many bytes at the
2321 ;; tail of both encoded strings are the same.
2323 (setq enc1 (encode-coding-string str1 coding-system)
2324 i1 (length enc1)
2325 enc2 (encode-coding-string str2 coding-system)
2326 i2 (length enc2))
2327 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
2328 (setq i1 (1- i1) i2 (1- i2)))
2330 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
2331 ;; and they are the extra control sequences at the tail to
2332 ;; exclude.
2333 (substring enc2 0 i2))))
2336 ;;; mule-cmds.el ends here