1 ;;; mule-cmds.el --- Commands for mulitilingual environment
3 ;; Copyright (C) 1995 Free Software Foundation, Inc.
4 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
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 "MULE")
30 "Keymap for MULE (Multilingual environment) specific commands.")
31 (fset 'mule-prefix mule-keymap)
33 ;; Keep "C-x C-m ..." for mule specific commands.
34 (define-key ctl-x-map "\C-m" 'mule-prefix)
36 (define-key global-map [menu-bar mule] (cons "Mule" mule-keymap))
38 (setq menu-bar-final-items (cons 'mule menu-bar-final-items))
40 (defvar mule-describe-language-support-map
41 (make-sparse-keymap "Describe Language Support"))
42 (fset 'mule-describe-language-support-prefix
43 mule-describe-language-support-map)
45 (defvar mule-set-language-environment-map
46 (make-sparse-keymap "Set Language Environment"))
47 (fset 'mule-set-language-environment-prefix
48 mule-set-language-environment-map)
50 (define-key mule-keymap "m" 'toggle-enable-multibyte-characters)
51 (define-key mule-keymap "f" 'set-buffer-file-coding-system)
52 (define-key mule-keymap "t" 'set-terminal-coding-system)
53 (define-key mule-keymap "k" 'set-keyboard-coding-system)
54 (define-key mule-keymap "p" 'set-current-process-coding-system)
55 (define-key mule-keymap "i" 'select-input-method)
57 (define-key help-map "\C-L" 'describe-language-support)
58 (define-key help-map "\C-\\" 'describe-input-method)
59 (define-key help-map "C" 'describe-current-coding-system)
60 (define-key help-map "h" 'view-hello-file)
62 (define-key mule-keymap [view-hello-file]
63 '("Show many languages" . view-hello-file))
64 (define-key mule-keymap [mule-diag]
65 '("Show diagnosis for MULE" . mule-diag))
66 (define-key mule-keymap [separator-coding-system]
68 (define-key mule-keymap [set-process-coding-system]
69 '("Set coding system of process" . set-current-process-coding-system))
70 (define-key mule-keymap [set-keyboard-coding-system]
71 '("Set coding system of keyboard" . set-keyboard-coding-system))
72 (define-key mule-keymap [set-terminal-coding-system]
73 '("Set coding system of terminal" . set-terminal-coding-system))
74 (define-key mule-keymap [set-buffer-file-coding-system]
75 '("Set coding system of buffer file" . set-buffer-file-coding-system))
76 (define-key mule-keymap [describe-current-coding-system]
77 '("Describe current coding systems" . describe-current-coding-system))
78 (define-key mule-keymap [separator-input-method]
80 (define-key mule-keymap [describe-input-method]
81 '("Describe input method" . describe-input-method))
82 (define-key mule-keymap [select-input-method]
83 '("Select input method" . select-input-method))
84 (define-key mule-keymap [toggle-input-method]
85 '("Toggle input method" . toggle-input-method))
86 (define-key mule-keymap [separator-mule]
88 (define-key mule-keymap [set-language-environment]
89 '("Set language environment" . mule-set-language-environment-prefix))
90 (define-key mule-keymap [describe-language-support]
91 '("Describe language support" . mule-describe-language-support-prefix))
92 (define-key mule-keymap [toggle-mule]
93 '("Disable/enable multibyte character" . toggle-enable-multibyte-characters))
95 ;; These are meaningless when running under X.
96 (put 'set-keyboard-coding-system 'menu-enable
97 '(null window-system))
98 (put 'set-terminal-coding-system 'menu-enable
99 '(null window-system))
102 ;; This should be a single character key binding because users use it
103 ;; very frequently while editing multilingual text. Now we can use
104 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
105 ;; convenient because it requires shifting on most keyboards. An
106 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
107 ;; but it won't be used that frequently.
108 (define-key global-map "\C-\\" 'toggle-input-method)
110 (defun toggle-enable-multibyte-characters (&optional arg)
111 "Change whether this buffer enables multibyte characters.
112 With arg, make them enable iff arg is positive."
114 (setq enable-multibyte-characters
115 (if (null arg) (null enable-multibyte-characters)
116 (> (prefix-numeric-value arg) 0)))
117 (force-mode-line-update))
119 (defun view-hello-file ()
120 "Display the HELLO file which list up many languages and characters."
122 ;; We have to decode the file in any environment.
123 (let ((default-enable-multibyte-characters t)
124 (coding-system-for-read 'iso-2022-7))
125 (find-file-read-only (expand-file-name "HELLO" data-directory))))
128 ;;; Language support staffs.
130 (defvar primary-language "English"
131 "Name of a user's primary language.
132 Emacs provide various language supports based on this variable.")
134 (defvar language-info-alist nil
135 "Alist of language names vs the corresponding information of various kind.
136 Each element looks like:
137 (LANGUAGE-NAME . ((KEY . INFO) ...))
138 where LANGUAGE-NAME is a string,
139 KEY is a symbol denoting the kind of information,
140 INFO is any Lisp object which contains the actual information related
143 (defun get-language-info (language-name key)
144 "Return the information for LANGUAGE-NAME of the kind KEY.
145 LANGUAGE-NAME is a string.
146 KEY is a symbol denoting the kind of required information."
147 (let ((lang-slot (assoc language-name language-info-alist)))
149 (cdr (assq key (cdr lang-slot))))))
151 ;; Return a lambda form which calls `describe-language-support' with
153 (defun build-describe-language-support-function (lang)
156 (describe-language-support ,lang)))
158 ;; Return a lambda form which calls `set-language-environment' with
160 (defun build-set-language-environment-function (lang)
163 (set-language-environment ,lang)))
165 (defun set-language-info (language-name key info)
166 "Set for LANGUAGE-NAME the information INFO under KEY.
167 LANGUAGE-NAME is a string
168 KEY is a symbol denoting the kind of information.
169 INFO is any Lisp object which contains the actual information.
171 Currently, the following KEYs are used by Emacs:
172 charset: list of symbols whose values are charsets specific to the language.
173 coding-system: list of coding systems specific to the langauge.
174 setup-function: see the documentation of `set-language-environment'.
175 tutorial: a tutorial file name written in the language.
176 sample-text: one line short text containing characters of the language.
177 documentation: a docstring describing how the language is supported,
178 or a fuction to call to describe it,
179 or t which means call `describe-language-support' to describe it.
180 input-method: alist of input method names for the language vs information
181 for activating them. Use `register-input-method' (which see)
182 to add a new input method to the alist.
184 Emacs will use more KEYs in the future. To avoid the conflition, users
185 should use prefix \"user-\" in the name of KEY."
186 (let (lang-slot key-slot)
187 (setq lang-slot (assoc language-name language-info-alist))
188 (if (null lang-slot) ; If no slot for the language, add it.
189 (setq lang-slot (list language-name)
190 language-info-alist (cons lang-slot language-info-alist)))
191 (setq key-slot (assq key lang-slot))
192 (if (null key-slot) ; If no slot for the key, add it.
194 (setq key-slot (list key))
195 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
196 (setcdr key-slot info)
198 (cond ((eq key 'documentation)
199 (define-key mule-describe-language-support-map
200 (vector (intern language-name))
202 (build-describe-language-support-function language-name))))
203 ((eq key 'setup-function)
204 (define-key mule-set-language-environment-map
205 (vector (intern language-name))
207 (build-set-language-environment-function language-name)))))
210 (defun set-language-info-alist (language-name alist)
211 "Set for LANGUAGE-NAME the information in ALIST.
212 ALIST is an alist of KEY and INFO. See the documentation of
213 `set-langauge-info' for the meanings of KEY and INFO."
215 (set-language-info language-name (car (car alist)) (cdr (car alist)))
216 (setq alist (cdr alist))))
218 (defun read-language-name (key prompt &optional initial-input)
219 "Read language name which has information for KEY, prompting with PROMPT."
220 (let* ((completion-ignore-case t)
221 (name (completing-read prompt
223 (function (lambda (elm) (assq key elm)))
226 (and (> (length name) 0)
227 (car (assoc-ignore-case (downcase name) language-info-alist)))))
229 ;;; Multilingual input methods.
231 (defvar current-input-method nil
232 "The current input method for multilingual text.
233 The value is a cons of language name and input method name.
234 If nil, it means no input method is activated now.")
235 (make-variable-buffer-local 'current-input-method)
236 (put 'current-input-method 'permanent-local t)
238 (defvar current-input-method-title nil
239 "Title string of the current input method shown in mode line.
240 Every input method should set this an appropriate value when activated.")
241 (make-variable-buffer-local 'current-input-method-title)
242 (put 'current-input-method-title 'permanent-local t)
244 (defvar default-input-method nil
245 "Default input method.
246 The default input method is the one activated automatically by the command
247 `toggle-input-method' (\\[toggle-input-method]).
248 The value is a cons of language name and input method name.")
250 (defvar default-input-method-title nil
251 "Title string of the default input method.")
253 (defvar previous-input-method nil
254 "Input method selected previously.
255 This is the one selected before the current input method is selected.
256 See also the documentation of `default-input-method'.")
258 (defvar inactivate-current-input-method-function nil
259 "Function to call for inactivating the current input method.
260 Every input method should set this to an appropriate value when activated.
261 This function is called with no argument.")
262 (make-variable-buffer-local 'inactivate-current-input-method-function)
263 (put 'inactivate-current-input-method-function 'permanent-local t)
265 (defvar describe-current-input-method-function nil
266 "Function to call for describing the current input method.
267 This function is called with no argument.")
268 (make-variable-buffer-local 'describe-current-input-method-function)
269 (put 'describe-current-input-method-function 'permanent-local t)
271 (defun register-input-method (language-name input-method)
272 "Register INPUT-METHOD as an input method of LANGUAGE-NAME.
273 LANGUAGE-NAME is a string.
274 INPUT-METHOD is a list of the form:
275 (METHOD-NAME ACTIVATE-FUNC ARG ...)
276 where METHOD-NAME is the name of this method,
277 ACTIVATE-FUNC is the function to call for activating this method.
278 Arguments for the function are METHOD-NAME and ARGs."
279 (let ((slot (get-language-info language-name 'input-method))
282 (set-language-info language-name 'input-method (list input-method))
283 (setq method-slot (assoc (car input-method) slot))
285 (setcdr method-slot (cdr input-method))
286 (set-language-info language-name 'input-method
287 (cons input-method slot))))))
289 (defun read-language-and-input-method-name ()
290 "Read a language names and the corresponding input method from a minibuffer.
291 Return a cons of those names."
292 (let ((language-name (read-language-name
295 (if previous-input-method
296 (cons (car previous-input-method) 0)))))
297 (if (null language-name)
298 (error "No input method for the specified language"))
299 (let* ((completion-ignore-case t)
300 (key-slot (cdr (assq 'input-method
301 (assoc language-name language-info-alist))))
303 (completing-read "Input method: " key-slot nil t
304 (if (and previous-input-method
305 (string= language-name
306 (car previous-input-method)))
307 (cons (cdr previous-input-method) 0)))))
308 (if (= (length method-name) 0)
309 (error "No input method specified"))
311 (car (assoc-ignore-case (downcase method-name) key-slot))))))
313 (defun set-default-input-method (language-name method-name)
314 "Set the default input method to METHOD-NAME for inputting LANGUAGE-NAME.
315 The default input method is the one activated automatically by the command
316 `toggle-input-method' (\\[toggle-input-method]).
317 This doesn't affect the currently activated input method."
318 (interactive (read-language-and-input-method-name))
319 (let* ((key-slot (get-language-info language-name 'input-method))
320 (method-slot (assoc method-name key-slot)))
321 (if (null method-slot)
322 (error "No input method `%s' for %s" method-name language-name))
323 (setq default-input-method (cons language-name method-name))))
325 (defun select-input-method (language-name method-name)
326 "Select and activate input method METHOD-NAME for inputting LANGUAGE-NAME.
327 The information for activating METHOD-NAME is stored
328 in `language-info-alist' under the key 'input-method.
329 The format of the information has the form:
330 ((METHOD-NAME ACTIVATE-FUNC ARG ...) ...)
331 where ACTIVATE-FUNC is a function to call for activating this method.
332 Arguments for the function are METHOD-NAME and ARGs."
333 (interactive (read-language-and-input-method-name))
334 (let* ((key-slot (get-language-info language-name 'input-method))
335 (method-slot (assoc method-name key-slot)))
336 (if (null method-slot)
337 (error "No input method `%s' for %s" method-name language-name))
338 (if current-input-method
340 (if (not (equal previous-input-method current-input-method))
341 (setq previous-input-method current-input-method))
342 (funcall inactivate-current-input-method-function)))
343 (setq method-slot (cdr method-slot))
344 (apply (car method-slot) method-name (cdr method-slot))
345 (setq default-input-method
346 (setq current-input-method (cons language-name method-name)))
347 (setq default-input-method-title current-input-method-title)
348 (setq current-input-method default-input-method)))
350 (defun toggle-input-method (&optional arg)
351 "Toggle whether a multilingual input method is activated in this buffer.
352 With arg, activate an input method specified interactively.
353 Without arg, the method being activated is the one selected most recently,
354 but if no input method has ever been selected, select one interactively."
357 (call-interactively 'select-input-method)
358 (if (null current-input-method)
359 (if default-input-method
360 (select-input-method (car default-input-method)
361 (cdr default-input-method))
362 (call-interactively 'select-input-method))
363 (funcall inactivate-current-input-method-function)
364 (setq current-input-method nil))))
366 (defun describe-input-method ()
367 "Describe the current input method."
369 (if current-input-method
370 (if (and (symbolp describe-current-input-method-function)
371 (fboundp describe-current-input-method-function))
372 (funcall describe-current-input-method-function)
373 (message "No way to describe the current input method `%s'"
374 (cdr current-input-method))
376 (message "No input method is activated now")
379 (defun read-multilingual-string (prompt &optional initial-input
380 language-name method-name)
381 "Read a multilingual string from minibuffer, prompting with string PROMPT.
382 The input method selected last time is activated in minibuffer.
383 If non-nil, second arg INITIAL-INPUT is a string to insert before reading.
384 Optional 3rd and 4th arguments LANGUAGE-NAME and METHOD-NAME specify
385 the input method to be activated instead of the one selected last time."
386 (let ((minibuffer-setup-hook '(toggle-input-method))
387 (default-input-method default-input-method))
388 (if (and language-name method-name)
389 (set-default-input-method language-name method-name))
390 (read-string prompt initial-input)))
392 ;; Variables to control behavior of input methods. All input methods
393 ;; should react to these variables.
395 (defvar input-method-tersely-flag nil
396 "*If this flag is non-nil, input method works rather tersely.
398 For instance, Quail input method does not show guidance buffer while
399 inputting at minibuffer if this flag is t.")
401 (defvar input-method-activate-hook nil
402 "Normal hook run just after an input method is activated.")
404 (defvar input-method-inactivate-hook nil
405 "Normal hook run just after an input method is inactivated.")
407 (defvar input-method-after-insert-chunk-hook nil
408 "Normal hook run just after an input method insert some chunk of text.")
411 ;;; Language specific setup functions.
412 (defun set-language-environment (language-name)
413 "Setup a user's environment for LANGUAGE-NAME.
415 To setup, a fucntion returned by:
416 (get-language-info LANGUAGE-NAME 'setup-function)
418 (interactive (list (read-language-name 'setup-function "Language: ")))
420 (if (or (null language-name)
422 (get-language-info language-name 'setup-function))))
423 (error "No way to setup environment for the specified language"))
426 ;; Print all arguments with `princ', then print "\n".
427 (defsubst princ-list (&rest args)
428 (while args (princ (car args)) (setq args (cdr args)))
431 (defun describe-language-support (language-name)
432 "Show documentation about how Emacs supports LANGUAGE-NAME."
433 (interactive (list (read-language-name 'documentation "Language: ")))
435 (if (or (null language-name)
437 (get-language-info language-name 'documentation))))
438 (error "No documentation for the specified language"))
439 (with-output-to-temp-buffer "*Help*"
443 ((and (symbolp doc) (fboundp doc))
446 (error "Invalid documentation data for %s" language-name)))
447 (princ-list "List of items specific to "
450 (princ "-----------------------------------------------------------\n")
451 (let ((str (get-language-info language-name 'sample-text)))
454 (princ "<sample text>\n")
455 (princ-list " " str))))
456 (princ "<input methods>\n")
457 (let ((l (get-language-info language-name 'input-method)))
459 (princ-list " " (car (car l)))
461 (princ "<character sets>\n")
462 (let ((l (get-language-info language-name 'charset)))
464 (princ-list " nothing specific to " language-name)
466 (princ-list " " (car l)
467 (format ":%3d:\n\t" (charset-id (car l)))
468 (charset-description (car l)))
470 (princ "<coding systems>\n")
471 (let ((l (get-language-info language-name 'coding-system)))
473 (princ-list " nothing specific to " language-name)
475 (princ-list " " (car l) ":\n\t"
476 (coding-system-docstring (car l)))
477 (setq l (cdr l)))))))))
481 (defsubst get-charset-property (charset propname)
482 "Return the value of CHARSET's PROPNAME property.
483 This is the last value stored with
484 `(put-charset-property CHARSET PROPNAME VALUE)'."
485 (plist-get (charset-plist charset) propname))
487 (defsubst put-charset-property (charset propname value)
488 "Store CHARSETS's PROPNAME property with value VALUE.
489 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
490 (set-charset-plist charset
491 (plist-put (charset-plist charset) propname value)))
493 ;;; Character code property
494 (put 'char-code-property-table 'char-table-extra-slots 0)
496 (defvar char-code-property-table
497 (make-char-table 'char-code-property-table)
498 "Char-table containing a property list of each character code.
500 See also the documentation of `get-char-code-property' and
501 `put-char-code-property'")
503 (defun get-char-code-property (char propname)
504 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
505 (let ((plist (aref char-code-property-table char)))
507 (car (cdr (memq propname plist))))))
509 (defun put-char-code-property (char propname value)
510 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
511 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
512 (let ((plist (aref char-code-property-table char)))
514 (let ((slot (memq propname plist)))
516 (setcar (cdr slot) value)
517 (nconc plist (list propname value))))
518 (aset char-code-property-table char (list propname value)))))
520 ;;; mule-cmds.el ends here