1 ;;; mule-cmds.el --- Commands for mulitilingual environment
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
6 ;; Keywords: mule, multilingual
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;;; MULE related key bindings and menus.
29 (defvar mule-keymap
(make-sparse-keymap)
30 "Keymap for MULE (Multilingual environment) specific commands.")
32 ;; Keep "C-x C-m ..." for mule specific commands.
33 (define-key ctl-x-map
"\C-m" mule-keymap
)
35 (define-key mule-keymap
"f" 'set-buffer-file-coding-system
)
36 (define-key mule-keymap
"t" 'set-terminal-coding-system
)
37 (define-key mule-keymap
"k" 'set-keyboard-coding-system
)
38 (define-key mule-keymap
"p" 'set-buffer-process-coding-system
)
39 (define-key mule-keymap
"\C-\\" 'set-input-method
)
40 (define-key mule-keymap
"c" 'universal-coding-system-argument
)
41 (define-key mule-keymap
"l" 'set-language-environment
)
43 (define-key help-map
"\C-L" 'describe-language-environment
)
44 (define-key help-map
"L" 'describe-language-environment
)
45 (define-key help-map
"\C-\\" 'describe-input-method
)
46 (define-key help-map
"I" 'describe-input-method
)
47 (define-key help-map
"C" 'describe-coding-system
)
48 (define-key help-map
"h" 'view-hello-file
)
50 (defvar mule-menu-keymap
(make-sparse-keymap "Mule")
51 "Keymap for MULE (Multilingual environment) menu specific commands.")
53 (define-key global-map
[menu-bar mule
]
54 `(menu-item "Mule" ,mule-menu-keymap
55 :visible default-enable-multibyte-characters
))
57 (setq menu-bar-final-items
(cons 'mule menu-bar-final-items
))
59 (defvar describe-language-environment-map nil
)
60 (define-prefix-command 'describe-language-environment-map
)
62 (defvar setup-language-environment-map nil
)
63 (define-prefix-command 'setup-language-environment-map
)
65 (defvar set-coding-system-map nil
)
66 (define-prefix-command 'set-coding-system-map
)
68 (define-key-after mule-menu-keymap
[describe-language-environment
]
69 '("Describe Language Environment" . describe-language-environment-map
)
71 (define-key-after mule-menu-keymap
[set-language-environment
]
72 '("Set Language Environment" . setup-language-environment-map
)
74 (define-key-after mule-menu-keymap
[mouse-set-font
]
75 '("Set Font/Fontset" . mouse-set-font
)
77 (define-key-after mule-menu-keymap
[separator-mule
]
80 (define-key-after mule-menu-keymap
[toggle-input-method
]
81 '("Toggle Input Method" . toggle-input-method
)
83 (define-key-after mule-menu-keymap
[set-input-method
]
84 '("Select Input Method" . set-input-method
)
86 (define-key-after mule-menu-keymap
[describe-input-method
]
87 '("Describe Input Method" . describe-input-method
)
89 (define-key-after mule-menu-keymap
[separator-input-method
]
92 (define-key-after mule-menu-keymap
[describe-coding-system
]
93 '("Describe Coding Systems" . describe-coding-system
)
95 (define-key-after mule-menu-keymap
[set-various-coding-system
]
96 '("Set Coding System" . set-coding-system-map
)
98 (define-key-after mule-menu-keymap
[separator-coding-system
]
101 (define-key-after mule-menu-keymap
[mule-diag
]
102 '("Show All of MULE Status" . mule-diag
)
104 (define-key-after mule-menu-keymap
[view-hello-file
]
105 '("Show Script Examples" . view-hello-file
)
108 (define-key-after set-coding-system-map
[set-buffer-file-coding-system
]
109 '("Buffer File" . set-buffer-file-coding-system
)
111 (define-key-after set-coding-system-map
[universal-coding-system-argument
]
112 '("Next Command" . universal-coding-system-argument
)
114 (define-key-after set-coding-system-map
[set-terminal-coding-system
]
115 '("Terminal" . set-terminal-coding-system
)
117 (define-key-after set-coding-system-map
[set-keyboard-coding-system
]
118 '("Keyboard" . set-keyboard-coding-system
)
120 (define-key-after set-coding-system-map
[set-buffer-process-coding-system
]
121 '("Buffer Process" . set-buffer-process-coding-system
)
124 (define-key setup-language-environment-map
125 [Default] '("Default" . setup-specified-language-environment))
127 ;; These are meaningless when running under X.
128 (put 'set-terminal-coding-system 'menu-enable
129 '(not (eq window-system 'x)))
130 (put 'set-keyboard-coding-system 'menu-enable
131 '(not (eq window-system 'x)))
132 ;; This is meaningless when the current buffer has no process.
133 (put 'set-buffer-process-coding-system 'menu-enable
134 '(get-buffer-process (current-buffer)))
136 ;; This should be a single character key binding because users use it
137 ;; very frequently while editing multilingual text. Now we can use
138 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
139 ;; convenient because it requires shifting on most keyboards. An
140 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
141 ;; but it won't be used that frequently.
142 (define-key global-map "\C-\\" 'toggle-input-method)
144 ;;; This is no good because people often type Shift-SPC
145 ;;; meaning to type SPC. -- rms.
146 ;;; ;; Here's an alternative key binding for X users (Shift-SPACE).
147 ;;; (define-key global-map [?\S- ] 'toggle-input-method)
149 (defun toggle-enable-multibyte-characters (&optional arg)
150 "Change whether this buffer uses multibyte characters.
151 With arg, use multibyte characters if the arg is positive.
153 Note that this command does not convert the byte contents of
154 the buffer; it only changes the way those bytes are interpreted.
155 In general, therefore, this command *changes* the sequence of
156 characters that the current buffer contains.
158 We suggest you avoid using use this command unless you know what you
159 are doing. If you use it by mistake, and the buffer is now displayed
160 wrong, use this command again to toggle back to the right mode."
163 (if (null arg) (null enable-multibyte-characters)
164 (> (prefix-numeric-value arg) 0))))
165 (set-buffer-multibyte new-flag))
166 (force-mode-line-update))
168 (defun view-hello-file ()
169 "Display the HELLO file which list up many languages and characters."
171 ;; We have to decode the file in any environment.
172 (let ((default-enable-multibyte-characters t)
173 (coding-system-for-read 'iso-2022-7bit))
174 (find-file-read-only (expand-file-name "HELLO" data-directory))))
176 (defun universal-coding-system-argument ()
177 "Execute an I/O command using the specified coding system."
179 (let* ((default (and buffer-file-coding-system
180 (not (eq (coding-system-type buffer-file-coding-system)
182 buffer-file-coding-system))
183 (coding-system (read-coding-system
185 (format "Coding system for following command (default, %s): " default)
186 "Coding system for following command: ")
188 (keyseq (read-key-sequence
189 (format "Command to execute with %s:" coding-system)))
190 (cmd (key-binding keyseq)))
191 (let ((coding-system-for-read coding-system)
192 (coding-system-for-write coding-system))
194 (call-interactively cmd))))
196 (defun set-default-coding-systems (coding-system)
197 "Set default value of various coding systems to CODING-SYSTEM.
198 This sets the following coding systems:
199 o coding system of a newly created buffer
200 o default coding system for subprocess I/O
201 This also sets the following values:
202 o default value used as file-name-coding-system for converting file names.
203 o default value for the command `set-terminal-coding-system'
204 o default value for the command `set-keyboard-coding-system'"
205 (check-coding-system coding-system)
206 (setq-default buffer-file-coding-system coding-system)
207 (if default-enable-multibyte-characters
208 (setq default-file-name-coding-system coding-system))
209 (setq default-terminal-coding-system coding-system)
210 (setq default-keyboard-coding-system coding-system)
211 (setq default-process-coding-system (cons coding-system coding-system)))
213 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
214 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal)
216 (defun prefer-coding-system (coding-system)
217 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
218 This also sets the following coding systems:
219 o coding system of a newly created buffer
220 o default coding system for subprocess I/O
221 This also sets the following values:
222 o default value used as file-name-coding-system for converting file names.
223 o default value for the command `set-terminal-coding-system'
224 o default value for the command `set-keyboard-coding-system'"
225 (interactive "zPrefer coding system: ")
226 (if (not (and coding-system (coding-system-p coding-system)))
227 (error "Invalid coding system `%s'" coding-system))
228 (let ((coding-category (coding-system-category coding-system))
229 (base (coding-system-base coding-system)))
230 (if (not coding-category)
231 ;; CODING-SYSTEM is no-conversion or undecided.
232 (error "Can't prefer the coding system `%s'" coding-system))
233 (set coding-category (or base coding-system))
234 (update-coding-systems-internal)
235 (if (not (eq coding-category (car coding-category-list)))
236 ;; We must change the order.
237 (setq coding-category-list
238 (cons coding-category
239 (delq coding-category coding-category-list))))
240 (if (and base (interactive-p))
241 (message "Highest priority is set to %s (base of %s)"
243 (set-default-coding-systems (or base coding-system))))
245 (defun find-coding-systems-region-subset-p (list1 list2)
246 "Return non-nil if all elements in LIST1 are included in LIST2.
247 Comparison done with EQ."
250 (or (memq (car list1) list2)
252 (setq list1 (cdr list1)))
255 (defun find-coding-systems-region (from to)
256 "Return a list of proper coding systems to encode a text between FROM and TO.
257 All coding systems in the list can safely encode any multibyte characters
260 If the text contains no multibyte charcters, return a list of a single
261 element `undecided'."
262 (find-coding-systems-for-charsets (find-charset-region from to)))
264 (defun find-coding-systems-string (string)
265 "Return a list of proper coding systems to encode STRING.
266 All coding systems in the list can safely encode any multibyte characters
269 If STRING contains no multibyte charcters, return a list of a single
270 element `undecided'."
271 (find-coding-systems-for-charsets (find-charset-string string)))
273 (defun find-coding-systems-for-charsets (charsets)
274 "Return a list of proper coding systems to encode characters of CHARSETS.
275 CHARSETS is a list of character sets."
276 (if (or (null charsets)
277 (and (= (length charsets) 1)
278 (eq 'ascii (car charsets))))
280 (let ((l coding-system-list)
281 (charset-prefered-codings
284 (get-charset-property x 'prefered-coding-system)))
286 (priorities (mapcar (function (lambda (x) (symbol-value x)))
287 coding-category-list))
290 (setq coding (car l) l (cdr l))
291 (if (and (eq coding (coding-system-base coding))
292 (setq safe (coding-system-get coding 'safe-charsets))
294 (find-coding-systems-region-subset-p charsets safe)))
295 ;; We put the higher priority to coding systems included
296 ;; in CHARSET-PREFERED-CODINGS, and within them, put the
297 ;; higher priority to coding systems which support smaller
298 ;; number of charsets.
300 (+ (if (coding-system-get coding 'mime-charset) 4096 0)
301 (lsh (length (memq coding priorities)) 7)
302 (if (memq coding charset-prefered-codings) 64 0)
303 (if (> (coding-system-type coding) 0) 32 0)
304 (if (consp safe) (- 32 (length safe)) 0))))
305 (setq codings (cons (cons priority coding) codings)))))
307 (sort codings (function (lambda (x y) (> (car x) (car y))))))
310 (defun find-multibyte-characters (from to &optional maxcount excludes)
311 "Find multibyte characters in the region specified by FROM and TO.
312 If FROM is a string, find multibyte characters in the string.
313 The return value is an alist of the following format:
314 ((CHARSET COUNT CHAR ...) ...)
316 CHARSET is a character set,
317 COUNT is a number of characters,
318 CHARs are found characters of the character set.
319 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
320 Optioanl 4th arg EXCLUDE is a list of character sets to be ignored."
325 (while (setq idx (string-match "[^\000-\177]" from idx))
326 (setq char (aref from idx)
327 charset (char-charset char))
328 (if (not (memq charset excludes))
329 (let ((slot (assq charset chars)))
331 (if (not (memq char (nthcdr 2 slot)))
332 (let ((count (nth 1 slot)))
333 (setcar (cdr slot) (1+ count))
334 (if (or (not maxcount) (< count maxcount))
335 (nconc slot (list char)))))
336 (setq chars (cons (list charset 1 char) chars)))))
337 (setq idx (1+ idx))))
340 (while (re-search-forward "[^\000-\177]" to t)
341 (setq char (preceding-char)
342 charset (char-charset char))
343 (if (not (memq charset excludes))
344 (let ((slot (assq charset chars)))
346 (if (not (memq char (nthcdr 2 slot)))
347 (let ((count (nth 1 slot)))
348 (setcar (cdr slot) (1+ count))
349 (if (or (not maxcount) (< count maxcount))
350 (nconc slot (list char)))))
351 (setq chars (cons (list charset 1 char) chars))))))))
354 (defvar last-coding-system-specified nil
355 "Most recent coding system explicitly specified by the user when asked.
356 This variable is set whenever Emacs asks the user which coding system
357 to use in order to write a file. If you set it to nil explicitly,
358 then call `write-region', then afterward this variable will be non-nil
359 only if the user was explicitly asked and specified a coding system.")
361 (defun select-safe-coding-system (from to &optional default-coding-system)
362 "Ask a user to select a safe coding system from candidates.
363 The candidates of coding systems which can safely encode a text
364 between FROM and TO are shown in a popup window.
366 Optional arg DEFAULT-CODING-SYSTEM specifies a coding system to be
367 checked at first. If omitted, buffer-file-coding-system of the
368 current buffer is used.
370 If the text can be encoded safely by DEFAULT-CODING-SYSTEM, it is
371 returned without any user interaction.
373 Kludgy feature: if FROM is a string, the string is the target text,
375 (or default-coding-system
376 (setq default-coding-system buffer-file-coding-system))
377 (let* ((charsets (if (stringp from) (find-charset-string from)
378 (find-charset-region from to)))
379 (safe-coding-systems (find-coding-systems-for-charsets charsets)))
380 (if (or (eq (car safe-coding-systems) 'undecided)
381 (and default-coding-system
382 (memq (coding-system-base default-coding-system)
383 safe-coding-systems)))
384 default-coding-system
386 ;; At first, change each coding system to the corresponding
387 ;; mime-charset name if it is also a coding system.
388 (let ((l safe-coding-systems)
391 (setq mime-charset (coding-system-get (car l) 'mime-charset))
392 (if (and mime-charset (coding-system-p mime-charset))
393 (setcar l mime-charset))
396 (let ((non-safe-chars (find-multibyte-characters
398 (and default-coding-system
399 (coding-system-get default-coding-system
403 ;; Highlight characters that default-coding-system can't encode.
404 (when (integerp from)
407 (while (and (not found)
408 (re-search-forward "[^\000-\177]" to t))
409 (setq found (assq (char-charset (preceding-char))
412 (set-window-start (selected-window) (point))
414 (while (re-search-forward "[^\000-\177]" to t)
415 (let* ((char (preceding-char))
416 (charset (char-charset char)))
417 (when (assq charset non-safe-chars)
418 (setq overlays (cons (make-overlay (1- (point)) (point))
420 (overlay-put (car overlays) 'face 'highlight))))))
422 ;; At last, ask a user to select a proper coding system.
424 (save-window-excursion
425 ;; At first, show a helpful message.
426 (with-output-to-temp-buffer "*Warning*"
428 (set-buffer standard-output)
429 (insert "The target text contains the following non ASCII character(s):\n")
430 (let ((len (length non-safe-chars))
432 (while (and non-safe-chars (< shown 3))
433 (when (> (length (car non-safe-chars)) 2)
434 (setq shown (1+ shown))
435 (insert (format "%25s: " (car (car non-safe-chars))))
436 (let ((l (nthcdr 2 (car non-safe-chars))))
440 (if (> (nth 1 (car non-safe-chars)) 3)
443 (setq non-safe-chars (cdr non-safe-chars)))
445 (insert (format "%27s\n" "..."))))
447 These can't be encoded safely by the coding system %s.
449 Please select one from the following safe coding systems:\n"
450 default-coding-system))
453 (mapcar (function (lambda (x) (princ " ") (princ x)))
455 (fill-region-as-paragraph pos (point)))))
457 ;; Read a coding system.
458 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
459 safe-coding-systems))
460 (name (completing-read
461 (format "Select coding system (default %s): "
462 (car safe-coding-systems))
463 safe-names nil t nil nil
464 (car (car safe-names)))))
465 (setq last-coding-system-specified (intern name))
466 (if (integerp (coding-system-eol-type default-coding-system))
467 (setq last-coding-system-specified
468 (coding-system-change-eol-conversion
469 last-coding-system-specified
470 (coding-system-eol-type default-coding-system))))
471 last-coding-system-specified))
472 (kill-buffer "*Warning*")
474 (delete-overlay (car overlays))
475 (setq overlays (cdr overlays)))))))))
477 (setq select-safe-coding-system-function 'select-safe-coding-system)
480 ;;; Language support staffs.
482 (defvar language-info-alist nil
483 "Alist of language environment definitions.
484 Each element looks like:
485 (LANGUAGE-NAME . ((KEY . INFO) ...))
486 where LANGUAGE-NAME is a string, the name of the language environment,
487 KEY is a symbol denoting the kind of information, and
488 INFO is the data associated with KEY.
489 Meaningful values for KEY include
491 documentation value is documentation of what this language environment
492 is meant for, and how to use it.
493 charset value is a list of the character sets used by this
494 language environment.
495 sample-text value is one line of text,
496 written using those character sets,
497 appropriate for this language environment.
498 setup-function value is a function to call to switch to this
499 language environment.
500 exit-function value is a function to call to leave this
501 language environment.
502 coding-system value is a list of coding systems that are good
503 for saving text written in this language environment.
504 This list serves as suggestions to the user;
505 in effect, as a kind of documentation.
506 coding-priority value is a list of coding systems for this language
507 environment, in order of decreasing priority.
508 This is used to set up the coding system priority
509 list when you switch to this language environment.
511 value is a translation table to be set to the
512 variable `nonascii-translation-table' in this
513 language environment.
515 value is an alist to be set to the variable
516 `charset-origin-alist' in this language environment.
517 input-method value is a default input method for this language
520 The following keys take effect only when multibyte characters are
521 globally disabled, i.e. the value of `default-enable-multibyte-characters'
524 unibyte-syntax value is a library name to load to set
525 unibyte 8-bit charcater syntaxes for this
526 language environment.
528 unibyte-display value is a coding system to encode characters
529 for the terminal. Characters in the range
530 of 160 to 255 display not as octal escapes,
531 but as non-ASCII characters in this language
534 (defun get-language-info (lang-env key)
535 "Return information listed under KEY for language environment LANG-ENV.
536 KEY is a symbol denoting the kind of information.
537 For a list of useful values for KEY and their meanings,
538 see `language-info-alist'."
539 (if (symbolp lang-env)
540 (setq lang-env (symbol-name lang-env)))
541 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
543 (cdr (assq key (cdr lang-slot))))))
545 (defun set-language-info (lang-env key info)
546 "Modify part of the definition of language environment LANG-ENV.
547 Specifically, this stores the information INFO under KEY
548 in the definition of this language environment.
549 KEY is a symbol denoting the kind of information.
550 INFO is the value for that information.
552 For a list of useful values for KEY and their meanings,
553 see `language-info-alist'."
554 (if (symbolp lang-env)
555 (setq lang-env (symbol-name lang-env)))
556 (let (lang-slot key-slot)
557 (setq lang-slot (assoc lang-env language-info-alist))
558 (if (null lang-slot) ; If no slot for the language, add it.
559 (setq lang-slot (list lang-env)
560 language-info-alist (cons lang-slot language-info-alist)))
561 (setq key-slot (assq key lang-slot))
562 (if (null key-slot) ; If no slot for the key, add it.
564 (setq key-slot (list key))
565 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
566 (setcdr key-slot info)))
568 (defun set-language-info-alist (lang-env alist &optional parents)
569 "Store ALIST as the definition of language environment LANG-ENV.
570 ALIST is an alist of KEY and INFO values. See the documentation of
571 `set-langauge-info' for the meanings of KEY and INFO.
573 Optional arg PARENTS is a list of parent menu names; it specifies
574 where to put this language environment in the
575 Describe Language Environment and Set Language Environment menus.
576 For example, (\"European\") means to put this language environment
577 in the European submenu in each of those two menus."
578 (if (symbolp lang-env)
579 (setq lang-env (symbol-name lang-env)))
580 (let ((describe-map describe-language-environment-map)
581 (setup-map setup-language-environment-map))
584 map parent-symbol parent)
586 (if (symbolp (setq parent-symbol (car l)))
587 (setq parent (symbol-name parent))
588 (setq parent parent-symbol parent-symbol (intern parent)))
589 (setq map (lookup-key describe-map (vector parent-symbol)))
592 (setq map (intern (format "describe-%s-environment-map"
594 (define-prefix-command map)
595 (define-key-after describe-map (vector parent-symbol)
596 (cons parent map) t)))
597 (setq describe-map (symbol-value map))
598 (setq map (lookup-key setup-map (vector parent-symbol)))
601 (setq map (intern (format "setup-%s-environment-map"
603 (define-prefix-command map)
604 (define-key-after setup-map (vector parent-symbol)
605 (cons parent map) t)))
606 (setq setup-map (symbol-value map))
609 ;; Set up menu items for this language env.
610 (let ((doc (assq 'documentation alist))
611 (setup-function (assq 'setup-function alist)))
613 (define-key-after describe-map (vector (intern lang-env))
614 (cons lang-env 'describe-specified-language-support) t))
616 (define-key-after setup-map (vector (intern lang-env))
617 (cons lang-env 'setup-specified-language-environment) t)))
620 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
621 (setq alist (cdr alist)))))
623 (defun read-language-name (key prompt &optional default)
624 "Read a language environment name which has information for KEY.
625 If KEY is nil, read any language environment.
626 Prompt with PROMPT. DEFAULT is the default choice of language environment.
627 This returns a language environment name as a string."
628 (let* ((completion-ignore-case t)
629 (name (completing-read prompt
632 (function (lambda (elm) (assq key elm))))
634 (if (and (> (length name) 0)
636 (get-language-info name key)))
639 ;;; Multilingual input methods.
641 (defconst leim-list-file-name "leim-list.el"
642 "Name of LEIM list file.
643 This file contains a list of libraries of Emacs input methods (LEIM)
644 in the format of Lisp expression for registering each input method.
645 Emacs loads this file at startup time.")
647 (defvar leim-list-header (format "\
648 ;;; %s -- list of LEIM (Library of Emacs Input Method)
650 ;; This file contains a list of LEIM (Library of Emacs Input Method)
651 ;; in the same directory as this file. Loading this file registeres
652 ;; the whole input methods in Emacs.
654 ;; Each entry has the form:
655 ;; (register-input-method
656 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
659 ;; See the function `register-input-method' for the meanings of arguments.
661 ;; If this directory is included in load-path, Emacs automatically
662 ;; loads this file at startup time.
666 "Header to be inserted in LEIM list file.")
668 (defvar leim-list-entry-regexp "^(register-input-method"
669 "Regexp matching head of each entry in LEIM list file.
670 See also the variable `leim-list-header'")
672 (defvar update-leim-list-functions
673 '(quail-update-leim-list-file)
674 "List of functions to call to update LEIM list file.
675 Each function is called with one arg, LEIM directory name.")
677 (defun update-leim-list-file (&rest dirs)
678 "Update LEIM list file in directories DIRS."
679 (let ((functions update-leim-list-functions))
681 (apply (car functions) dirs)
682 (setq functions (cdr functions)))))
684 (defvar current-input-method nil
685 "The current input method for multilingual text.
686 If nil, that means no input method is activated now.")
687 (make-variable-buffer-local 'current-input-method)
688 (put 'current-input-method 'permanent-local t)
690 (defvar current-input-method-title nil
691 "Title string of the current input method shown in mode line.")
692 (make-variable-buffer-local 'current-input-method-title)
693 (put 'current-input-method-title 'permanent-local t)
695 (defcustom default-input-method nil
696 "*Default input method for multilingual text (a string).
697 This is the input method activated automatically by the command
698 `toggle-input-method' (\\[toggle-input-method])."
702 (defvar input-method-history nil
703 "History list for some commands that read input methods.")
704 (make-variable-buffer-local 'input-method-history)
705 (put 'input-method-history 'permanent-local t)
707 (defvar inactivate-current-input-method-function nil
708 "Function to call for inactivating the current input method.
709 Every input method should set this to an appropriate value when activated.
710 This function is called with no argument.
712 This function should never change the value of `current-input-method'.
713 It is set to nil by the function `inactivate-input-method'.")
714 (make-variable-buffer-local 'inactivate-current-input-method-function)
715 (put 'inactivate-current-input-method-function 'permanent-local t)
717 (defvar describe-current-input-method-function nil
718 "Function to call for describing the current input method.
719 This function is called with no argument.")
720 (make-variable-buffer-local 'describe-current-input-method-function)
721 (put 'describe-current-input-method-function 'permanent-local t)
723 (defvar input-method-alist nil
724 "Alist of input method names vs how to use them.
725 Each element has the form:
726 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
727 See the function `register-input-method' for the meanings of the elements.")
729 (defun register-input-method (input-method lang-env &rest args)
730 "Register INPUT-METHOD as an input method for language environment ENV.
731 INPUT-METHOD and LANG-ENV are symbols or strings.
733 The remaining arguments are:
734 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
735 ACTIVATE-FUNC is a function to call to activate this method.
736 TITLE is a string to show in the mode line when this method is active.
737 DESCRIPTION is a string describing this method and what it is good for.
738 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
739 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS."
740 (if (symbolp lang-env)
741 (setq lang-env (symbol-name lang-env)))
742 (if (symbolp input-method)
743 (setq input-method (symbol-name input-method)))
744 (let ((info (cons lang-env args))
745 (slot (assoc input-method input-method-alist)))
748 (setq slot (cons input-method info))
749 (setq input-method-alist (cons slot input-method-alist)))))
751 (defun read-input-method-name (prompt &optional default inhibit-null)
752 "Read a name of input method from a minibuffer prompting with PROMPT.
753 If DEFAULT is non-nil, use that as the default,
754 and substitute it into PROMPT at the first `%s'.
755 If INHIBIT-NULL is non-nil, null input signals an error.
757 The return value is a string."
759 (setq prompt (format prompt default)))
760 (let* ((completion-ignore-case t)
761 ;; This binding is necessary because input-method-history is
763 (input-method (completing-read prompt input-method-alist
764 nil t nil 'input-method-history
766 (if (> (length input-method) 0)
769 (error "No valid input method is specified")))))
771 (defun activate-input-method (input-method)
772 "Switch to input method INPUT-METHOD for the current buffer.
773 If some other input method is already active, turn it off first.
774 If INPUT-METHOD is nil, deactivate any current input method."
775 (if (symbolp input-method)
776 (setq input-method (symbol-name input-method)))
777 (if (and current-input-method
778 (not (string= current-input-method input-method)))
779 (inactivate-input-method))
780 (unless (or current-input-method (null input-method))
781 (let ((slot (assoc input-method input-method-alist)))
783 (error "Can't activate input method `%s'" input-method))
784 (let ((func (nth 2 slot)))
786 (apply (nth 2 slot) input-method (nthcdr 5 slot))
787 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
790 (apply (car func) input-method (nthcdr 5 slot)))
791 (error "Can't activate input method `%s'" input-method))))
792 (setq current-input-method input-method)
793 (setq current-input-method-title (nth 3 slot))
795 (run-hooks 'input-method-activate-hook)
796 (force-mode-line-update)))))
798 (defun inactivate-input-method ()
799 "Turn off the current input method."
800 (when current-input-method
801 (if input-method-history
802 (unless (string= current-input-method (car input-method-history))
803 (setq input-method-history
804 (cons current-input-method
805 (delete current-input-method input-method-history))))
806 (setq input-method-history (list current-input-method)))
808 (funcall inactivate-current-input-method-function)
810 (run-hooks 'input-method-inactivate-hook)
811 (setq current-input-method nil
812 current-input-method-title nil)
813 (force-mode-line-update)))))
815 (defun set-input-method (input-method)
816 "Select and activate input method INPUT-METHOD for the current buffer.
817 This also sets the default input method to the one you specify."
819 (let* ((default (or (car input-method-history) default-input-method)))
820 (list (read-input-method-name
821 (if default "Select input method (default %s): " "Select input method: ")
823 (activate-input-method input-method)
824 (setq default-input-method input-method))
826 (defun toggle-input-method (&optional arg)
827 "Turn on or off a multilingual text input method for the current buffer.
829 With no prefix argument, if some input method is currently activated,
830 turn it off. Otherwise, activate an input method--the one most recently used,
831 or the one specified in `default-input-method', or one read from the
834 With a prefix arg, read an input method from minibuffer and turn it on.
835 The default is the most recent input method specified
836 \(not including the currently active input method, if any).
838 When there's no input method to turn on, turn on what read from minibuffer."
840 (if (and current-input-method (not arg))
841 (inactivate-input-method)
842 (let ((default (or (car input-method-history) default-input-method)))
843 (if (and arg default (equal current-input-method default)
844 (> (length input-method-history) 1))
845 (setq default (nth 1 input-method-history)))
846 (activate-input-method
847 (if (or arg (not default))
849 (read-input-method-name
850 (if default "Input method (default %s): " "Input method: " )
853 (or default-input-method
854 (setq default-input-method current-input-method)))))
856 (defun describe-input-method (input-method)
857 "Describe input method INPUT-METHOD."
859 (list (read-input-method-name
860 "Describe input method (default, current choice): ")))
861 (if (and input-method (symbolp input-method))
862 (setq input-method (symbol-name input-method)))
863 (if (null input-method)
864 (describe-current-input-method)
865 (with-output-to-temp-buffer "*Help*"
866 (let ((elt (assoc input-method input-method-alist)))
867 (princ (format "Input method: %s (`%s' in mode line) for %s\n %s\n"
868 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt)))))))
870 (defun describe-current-input-method ()
871 "Describe the input method currently in use."
872 (if current-input-method
873 (if (and (symbolp describe-current-input-method-function)
874 (fboundp describe-current-input-method-function))
875 (funcall describe-current-input-method-function)
876 (message "No way to describe the current input method `%s'"
877 (cdr current-input-method))
879 (error "No input method is activated now")))
881 (defun read-multilingual-string (prompt &optional initial-input input-method)
882 "Read a multilingual string from minibuffer, prompting with string PROMPT.
883 The input method selected last time is activated in minibuffer.
884 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
886 Optional 3rd argument INPUT-METHOD specifies the input method
887 to be activated instead of the one selected last time. It is a symbol
893 (read-input-method-name "Input method: " nil t)))
894 (if (and input-method (symbolp input-method))
895 (setq input-method (symbol-name input-method)))
896 (let ((current-input-method input-method))
897 (read-string prompt initial-input nil nil t)))
899 ;; Variables to control behavior of input methods. All input methods
900 ;; should react to these variables.
902 (defcustom input-method-verbose-flag 'default
903 "*A flag to control extra guidance given by input methods.
904 The value should be nil, t, `complex-only', or `default'.
906 The extra guidance is done by showing list of available keys in echo
907 area. When you use the input method in the minibuffer, the guidance
908 is shown at the bottom short window (split from the existing window).
910 If the value is t, extra guidance is always given, if the value is
911 nil, extra guidance is always suppressed.
913 If the value is `complex-only', only complex input methods such as
914 `chinese-py' and `japanese' give extra guidance.
916 If the value is `default', complex input methods always give extra
917 guidance, but simple input methods give it only when you are not in
920 See also the variable `input-method-highlight-flag'."
921 :type '(choice (const t) (const nil) (const complex-only) (const default))
924 (defcustom input-method-highlight-flag t
925 "*If this flag is non-nil, input methods highlight partially-entered text.
926 For instance, while you are in the middle of a Quail input method sequence,
927 the text inserted so far is temporarily underlined.
928 The underlining goes away when you finish or abort the input method sequence.
929 See also the variable `input-method-verbose-flag'."
933 (defvar input-method-activate-hook nil
934 "Normal hook run just after an input method is activated.
936 The variable `current-input-method' keeps the input method name
939 (defvar input-method-inactivate-hook nil
940 "Normal hook run just after an input method is inactivated.
942 The variable `current-input-method' still keeps the input method name
945 (defvar input-method-after-insert-chunk-hook nil
946 "Normal hook run just after an input method insert some chunk of text.")
948 (defvar input-method-exit-on-first-char nil
949 "This flag controls a timing when an input method returns.
950 Usually, the input method does not return while there's a possibility
951 that it may find a different translation if a user types another key.
952 But, it this flag is non-nil, the input method returns as soon as
953 the current key sequence gets long enough to have some valid translation.")
955 (defvar input-method-use-echo-area nil
956 "This flag controls how an input method shows an intermediate key sequence.
957 Usually, the input method inserts the intermediate key sequence,
958 or candidate translations corresponding to the sequence,
959 at point in the current buffer.
960 But, if this flag is non-nil, it displays them in echo area instead.")
962 (defvar input-method-exit-on-invalid-key nil
963 "This flag controls the behaviour of an input method on invalid key input.
964 Usually, when a user types a key which doesn't start any character
965 handled by the input method, the key is handled by turning off the
966 input method temporarily. After that key, the input method is renabled.
967 But, if this flag is non-nil, the input method is never back on.")
970 (defvar set-language-environment-hook nil
971 "Normal hook run after some language environment is set.
973 When you set some hook function here, that effect usually should not
974 be inherited to another language environment. So, you had better set
975 another function in `exit-language-environment-hook' (which see) to
978 (defvar exit-language-environment-hook nil
979 "Normal hook run after exiting from some language environment.
980 When this hook is run, the variable `current-language-environment'
981 is still bound to the language environment being exited.
983 This hook is mainly used for cancelling the effect of
984 `set-language-environment-hook' (which-see).")
986 (defun setup-specified-language-environment ()
987 "Switch to a specified language environment."
990 (if (and (symbolp last-command-event)
991 (or (not (eq last-command-event 'Default))
992 (setq last-command-event 'English))
993 (setq language-name (symbol-name last-command-event)))
994 (set-language-environment language-name)
995 (error "Bogus calling sequence"))))
997 (defcustom current-language-environment "English"
998 "The last language environment specified with `set-language-environment'."
999 :set 'set-language-environment
1000 :initialize 'custom-initialize-default
1004 (defun reset-language-environment ()
1005 "Reset multilingual environment of Emacs to the default status.
1007 The default status is as follows:
1009 The default value of buffer-file-coding-system is nil.
1010 The default coding system for process I/O is nil.
1011 The default value for the command `set-terminal-coding-system' is nil.
1012 The default value for the command `set-keyboard-coding-system' is nil.
1014 The order of priorities of coding categories and the coding system
1015 bound to each category are as follows
1016 coding category coding system
1017 --------------------------------------------------
1018 coding-category-iso-8-2 iso-latin-1
1019 coding-category-iso-8-1 iso-latin-1
1020 coding-category-iso-7-tight iso-2022-jp
1021 coding-category-iso-7 iso-2022-7bit
1022 coding-category-iso-7-else iso-2022-7bit-lock
1023 coding-category-iso-8-else iso-2022-8bit-ss2
1024 coding-category-emacs-mule emacs-mule
1025 coding-category-raw-text raw-text
1026 coding-category-sjis japanese-shift-jis
1027 coding-category-big5 chinese-big5
1028 coding-category-ccl nil
1029 coding-category-binarry no-conversion
1032 ;; This function formerly set default-enable-multibyte-characters to t,
1033 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1035 (setq coding-category-iso-7-tight 'iso-2022-jp
1036 coding-category-iso-7 'iso-2022-7bit
1037 coding-category-iso-8-1 'iso-latin-1
1038 coding-category-iso-8-2 'iso-latin-1
1039 coding-category-iso-7-else 'iso-2022-7bit-lock
1040 coding-category-iso-8-else 'iso-2022-8bit-ss2
1041 coding-category-emacs-mule 'emacs-mule
1042 coding-category-raw-text 'raw-text
1043 coding-category-sjis 'japanese-shift-jis
1044 coding-category-big5 'chinese-big5
1045 coding-category-ccl nil
1046 coding-category-binary 'no-conversion)
1048 (set-coding-priority
1049 '(coding-category-iso-8-1
1050 coding-category-iso-8-2
1051 coding-category-iso-7-tight
1052 coding-category-iso-7
1053 coding-category-iso-7-else
1054 coding-category-iso-8-else
1055 coding-category-emacs-mule
1056 coding-category-raw-text
1057 coding-category-sjis
1058 coding-category-big5
1060 coding-category-binary))
1062 (set-default-coding-systems nil)
1063 ;; Don't alter the terminal and keyboard coding systems here.
1064 ;; The terminal still supports the same coding system
1065 ;; that it supported a minute ago.
1066 ;;; (set-terminal-coding-system-internal nil)
1067 ;;; (set-keyboard-coding-system-internal nil)
1069 (setq nonascii-translation-table nil
1070 nonascii-insert-offset 0))
1072 (defun set-language-environment (language-name)
1073 "Set up multi-lingual environment for using LANGUAGE-NAME.
1074 This sets the coding system priority and the default input method
1075 and sometimes other things. LANGUAGE-NAME should be a string
1076 which is the name of a language environment. For example, \"Latin-1\"
1077 specifies the character set for the major languages of Western Europe."
1078 (interactive (list (read-language-name
1080 "Set language environment (default, English): ")))
1082 (if (symbolp language-name)
1083 (setq language-name (symbol-name language-name)))
1084 (setq language-name "English"))
1085 (or (assoc-ignore-case language-name language-info-alist)
1086 (error "Language environment not defined: %S" language-name))
1087 (if current-language-environment
1088 (let ((func (get-language-info current-language-environment
1090 (run-hooks 'exit-language-environment-hook)
1091 (if (fboundp func) (funcall func))))
1092 (reset-language-environment)
1094 (setq current-language-environment language-name)
1095 (set-language-environment-coding-systems language-name)
1096 (let ((input-method (get-language-info language-name 'input-method)))
1098 (setq default-input-method input-method)
1099 (if input-method-history
1100 (setq input-method-history
1102 (delete input-method input-method-history))))))
1103 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
1104 (if (char-table-p nonascii)
1105 (setq nonascii-translation-table nonascii)
1106 (if (charsetp nonascii)
1107 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1109 (setq charset-origin-alist
1110 (get-language-info language-name 'charset-origin-alist))
1112 (unless default-enable-multibyte-characters
1114 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1116 (let ((set-case-syntax-set-multibyte nil))
1118 (set-standard-case-table (standard-case-table))
1119 (let ((list (buffer-list)))
1121 (with-current-buffer (car list)
1122 (set-case-table (standard-case-table)))
1123 (setq list (cdr list)))))))
1124 (let ((coding (get-language-info language-name 'unibyte-display)))
1127 (standard-display-european-internal)
1128 (set-terminal-coding-system coding))
1129 (standard-display-default 160 255)
1130 (aset standard-display-table 146 nil)
1131 (set-terminal-coding-system nil))))
1133 (let ((required-features (get-language-info language-name 'features)))
1134 (while required-features
1135 (require (car required-features))
1136 (setq required-features (cdr required-features))))
1137 (let ((func (get-language-info language-name 'setup-function)))
1140 (run-hooks 'set-language-environment-hook)
1141 (force-mode-line-update t))
1143 (defun standard-display-european-internal ()
1144 ;; Actually set up direct output of non-ASCII characters.
1145 (standard-display-8bit 160 255)
1146 ;; Make non-line-break space display as a plain space.
1147 ;; Most X fonts do the wrong thing for code 160.
1148 (aset standard-display-table 160 [32])
1149 ;; Most Windows programs send out apostrophe's as \222. Most X fonts
1150 ;; don't contain a character at that position. Map it to the ASCII
1152 (aset standard-display-table 146 [39]))
1154 (defun set-language-environment-coding-systems (language-name)
1155 "Do various coding system setups for language environment LANGUAGE-NAME."
1156 (let* ((priority (get-language-info language-name 'coding-priority))
1157 (default-coding (car priority)))
1159 (let ((categories (mapcar 'coding-system-category priority)))
1160 (set-default-coding-systems default-coding)
1161 (set-coding-priority categories)
1163 (set (car categories) (car priority))
1164 (setq priority (cdr priority) categories (cdr categories)))
1165 (update-coding-systems-internal)))))
1167 ;; Print all arguments with `princ', then print "\n".
1168 (defsubst princ-list (&rest args)
1169 (while args (princ (car args)) (setq args (cdr args)))
1172 ;; Print a language specific information such as input methods,
1173 ;; charsets, and coding systems. This function is intended to be
1174 ;; called from the menu:
1175 ;; [menu-bar mule describe-language-environment LANGUAGE]
1176 ;; and should not run it by `M-x describe-current-input-method-function'.
1177 (defun describe-specified-language-support ()
1178 "Describe how Emacs supports the specified language environment."
1180 (let (language-name)
1181 (if (not (and (symbolp last-command-event)
1182 (setq language-name (symbol-name last-command-event))))
1183 (error "Bogus calling sequence"))
1184 (describe-language-environment language-name)))
1186 (defun describe-language-environment (language-name)
1187 "Describe how Emacs supports language environment LANGUAGE-NAME."
1189 (list (read-language-name
1191 "Describe language environment (default, current choice): ")))
1192 (if (null language-name)
1193 (setq language-name current-language-environment))
1194 (if (or (null language-name)
1195 (null (get-language-info language-name 'documentation)))
1196 (error "No documentation for the specified language"))
1197 (if (symbolp language-name)
1198 (setq language-name (symbol-name language-name)))
1199 (let ((doc (get-language-info language-name 'documentation)))
1200 (with-output-to-temp-buffer "*Help*"
1201 (princ-list language-name " language environment" "\n")
1206 (let ((str (get-language-info language-name 'sample-text)))
1209 (princ "Sample text:\n")
1210 (princ-list " " str)
1212 (let ((input-method (get-language-info language-name 'input-method))
1213 (l input-method-alist))
1214 (princ "Input methods")
1216 (princ (format " (default, %s)" input-method))
1217 (setq input-method (assoc input-method input-method-alist))
1218 (setq l (cons input-method (delete input-method l))))
1221 (if (string= language-name (nth 1 (car l)))
1222 (princ-list " " (car (car l))
1223 (format " (`%s' in mode line)" (nth 3 (car l)))))
1226 (princ "Character sets:\n")
1227 (let ((l (get-language-info language-name 'charset)))
1229 (princ-list " nothing specific to " language-name)
1231 (princ-list " " (car l) ": "
1232 (charset-description (car l)))
1235 (princ "Coding systems:\n")
1236 (let ((l (get-language-info language-name 'coding-system)))
1238 (princ-list " nothing specific to " language-name)
1240 (princ (format " %s (`%c' in mode line):\n\t%s\n"
1242 (coding-system-mnemonic (car l))
1243 (coding-system-doc-string (car l))))
1244 (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
1247 (princ (cons 'alias: (cdr aliases)))
1249 (setq l (cdr l))))))))
1251 ;;; Charset property
1253 (defsubst get-charset-property (charset propname)
1254 "Return the value of CHARSET's PROPNAME property.
1255 This is the last value stored with
1256 (put-charset-property CHARSET PROPNAME VALUE)."
1257 (plist-get (charset-plist charset) propname))
1259 (defsubst put-charset-property (charset propname value)
1260 "Store CHARSETS's PROPNAME property with value VALUE.
1261 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1262 (set-charset-plist charset
1263 (plist-put (charset-plist charset) propname value)))
1265 ;;; Character code property
1266 (put 'char-code-property-table 'char-table-extra-slots 0)
1268 (defvar char-code-property-table
1269 (make-char-table 'char-code-property-table)
1270 "Char-table containing a property list of each character code.
1272 See also the documentation of `get-char-code-property' and
1273 `put-char-code-property'.")
1275 (defun get-char-code-property (char propname)
1276 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1277 (let ((plist (aref char-code-property-table char)))
1279 (car (cdr (memq propname plist))))))
1281 (defun put-char-code-property (char propname value)
1282 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1283 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1284 (let ((plist (aref char-code-property-table char)))
1286 (let ((slot (memq propname plist)))
1288 (setcar (cdr slot) value)
1289 (nconc plist (list propname value))))
1290 (aset char-code-property-table char (list propname value)))))
1292 ;;; mule-cmds.el ends here