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 "x" 'set-selection-coding-system)
40 (define-key mule-keymap "X" 'set-next-selection-coding-system)
41 (define-key mule-keymap "\C-\\" 'set-input-method)
42 (define-key mule-keymap "c" 'universal-coding-system-argument)
43 (define-key mule-keymap "l" 'set-language-environment)
45 (define-key help-map "\C-L" 'describe-language-environment)
46 (define-key help-map "L" 'describe-language-environment)
47 (define-key help-map "\C-\\" 'describe-input-method)
48 (define-key help-map "I" 'describe-input-method)
49 (define-key help-map "C" 'describe-coding-system)
50 (define-key help-map "h" 'view-hello-file)
52 (defvar mule-menu-keymap (make-sparse-keymap "Mule")
53 "Keymap for MULE (Multilingual environment) menu specific commands.")
55 (define-key global-map [menu-bar mule]
56 `(menu-item "Mule" ,mule-menu-keymap
57 :visible default-enable-multibyte-characters))
59 (setq menu-bar-final-items (cons 'mule menu-bar-final-items))
61 (defvar describe-language-environment-map
62 (make-sparse-keymap "Describe Language Environment"))
64 (defvar setup-language-environment-map
65 (make-sparse-keymap "Set Language Environment"))
67 (defvar set-coding-system-map
68 (make-sparse-keymap "Set Coding System"))
70 (define-key-after mule-menu-keymap [describe-language-environment]
71 (cons "Describe Language Environment" describe-language-environment-map)
73 (define-key-after mule-menu-keymap [set-language-environment]
74 (cons "Set Language Environment" setup-language-environment-map)
76 (define-key-after mule-menu-keymap [mouse-set-font]
77 '("Set Font/Fontset" . mouse-set-font)
79 (define-key-after mule-menu-keymap [separator-mule]
82 (define-key-after mule-menu-keymap [toggle-input-method]
83 '("Toggle Input Method" . toggle-input-method)
85 (define-key-after mule-menu-keymap [set-input-method]
86 '("Select Input Method" . set-input-method)
88 (define-key-after mule-menu-keymap [describe-input-method]
89 '("Describe Input Method" . describe-input-method)
91 (define-key-after mule-menu-keymap [separator-input-method]
94 (define-key-after mule-menu-keymap [describe-coding-system]
95 '("Describe Coding Systems" . describe-coding-system)
97 (define-key-after mule-menu-keymap [set-various-coding-system]
98 (cons "Set Coding System" set-coding-system-map)
100 (define-key-after mule-menu-keymap [separator-coding-system]
103 (define-key-after mule-menu-keymap [mule-diag]
104 '("Show All of MULE Status" . mule-diag)
106 (define-key-after mule-menu-keymap [view-hello-file]
107 '("Show Script Examples" . view-hello-file)
110 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
111 '("Buffer File" . set-buffer-file-coding-system)
113 (define-key-after set-coding-system-map [universal-coding-system-argument]
114 '("Next Command" . universal-coding-system-argument)
116 (define-key-after set-coding-system-map [set-terminal-coding-system]
117 '("Terminal" . set-terminal-coding-system)
119 (define-key-after set-coding-system-map [set-keyboard-coding-system]
120 '("Keyboard" . set-keyboard-coding-system)
122 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
123 '("Buffer Process" . set-buffer-process-coding-system)
125 (define-key-after set-coding-system-map [set-selection-coding-system]
126 '("X Selection" . set-selection-coding-system)
128 (define-key-after set-coding-system-map [set-next-selection-coding-system]
129 '("Next X Selection" . set-next-selection-coding-system)
131 (define-key setup-language-environment-map
132 [Default] '("Default" . setup-specified-language-environment))
134 ;; These are meaningless when running under X and W32.
135 (put 'set-terminal-coding-system 'menu-enable
136 '(or (not window-system) (eq window-system 'pc)))
137 (put 'set-keyboard-coding-system 'menu-enable
138 '(or (not window-system) (eq window-system 'pc)))
139 ;; This is meaningless when the current buffer has no process.
140 (put 'set-buffer-process-coding-system 'menu-enable
141 '(get-buffer-process (current-buffer)))
142 ;; These are meaningless when running under terminal.
143 (put 'set-selection-coding-system 'menu-enable
145 (put 'set-next-selection-coding-system 'menu-enable
148 ;; This should be a single character key binding because users use it
149 ;; very frequently while editing multilingual text. Now we can use
150 ;; only two such keys: "\C-\\" and "\C-^", but the latter is not
151 ;; convenient because it requires shifting on most keyboards. An
152 ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit'
153 ;; but it won't be used that frequently.
154 (define-key global-map "\C-\\" 'toggle-input-method)
156 ;;; This is no good because people often type Shift-SPC
157 ;;; meaning to type SPC. -- rms.
158 ;;; ;; Here's an alternative key binding for X users (Shift-SPACE).
159 ;;; (define-key global-map [?\S- ] 'toggle-input-method)
161 (defun coding-system-change-eol-conversion (coding-system eol-type)
162 "Return a coding system which differs from CODING-SYSTEM in eol conversion.
163 The returned coding system converts end-of-line by EOL-TYPE
164 but text as the same way as CODING-SYSTEM.
165 EOL-TYPE should be `unix', `dos', `mac', or nil.
166 If EOL-TYPE is nil, the returned coding system detects
167 how end-of-line is formatted automatically while decoding.
169 EOL-TYPE can be specified by an integer 0, 1, or 2.
170 They means `unix', `dos', and `mac' respectively."
171 (if (symbolp eol-type)
172 (setq eol-type (cond ((eq eol-type 'unix) 0)
173 ((eq eol-type 'dos) 1)
174 ((eq eol-type 'mac) 2)
176 (let ((orig-eol-type (coding-system-eol-type coding-system)))
177 (if (vectorp orig-eol-type)
180 (aref orig-eol-type eol-type))
181 (let ((base (coding-system-base coding-system)))
184 (if (= eol-type orig-eol-type)
186 (setq orig-eol-type (coding-system-eol-type base))
187 (if (vectorp orig-eol-type)
188 (aref orig-eol-type eol-type))))))))
190 (defun coding-system-change-text-conversion (coding-system coding)
191 "Return a coding system which differs from CODING-SYSTEM in text conversion.
192 The returned coding system converts text by CODING
193 but end-of-line as the same way as CODING-SYSTEM.
194 If CODING is nil, the returned coding system detects
195 how text is formatted automatically while decoding."
197 (coding-system-base coding-system)
198 (let ((eol-type (coding-system-eol-type coding-system)))
199 (coding-system-change-eol-conversion
201 (if (numberp eol-type) (aref [unix dos mac] eol-type))))))
203 (defun toggle-enable-multibyte-characters (&optional arg)
204 "Change whether this buffer uses multibyte characters.
205 With arg, use multibyte characters if the arg is positive.
207 Note that this command does not convert the byte contents of
208 the buffer; it only changes the way those bytes are interpreted.
209 In general, therefore, this command *changes* the sequence of
210 characters that the current buffer contains.
212 We suggest you avoid using use this command unless you know what you
213 are doing. If you use it by mistake, and the buffer is now displayed
214 wrong, use this command again to toggle back to the right mode."
217 (if (null arg) (null enable-multibyte-characters)
218 (> (prefix-numeric-value arg) 0))))
219 (set-buffer-multibyte new-flag))
220 (force-mode-line-update))
222 (defun view-hello-file ()
223 "Display the HELLO file which list up many languages and characters."
225 ;; We have to decode the file in any environment.
226 (let ((default-enable-multibyte-characters t)
227 (coding-system-for-read 'iso-2022-7bit))
228 (find-file-read-only (expand-file-name "HELLO" data-directory))))
230 (defun universal-coding-system-argument ()
231 "Execute an I/O command using the specified coding system."
233 (let* ((default (and buffer-file-coding-system
234 (not (eq (coding-system-type buffer-file-coding-system)
236 buffer-file-coding-system))
237 (coding-system (read-coding-system
239 (format "Coding system for following command (default, %s): " default)
240 "Coding system for following command: ")
242 (keyseq (read-key-sequence
243 (format "Command to execute with %s:" coding-system)))
244 (cmd (key-binding keyseq)))
245 (let ((coding-system-for-read coding-system)
246 (coding-system-for-write coding-system))
248 (call-interactively cmd))))
250 (defun set-default-coding-systems (coding-system)
251 "Set default value of various coding systems to CODING-SYSTEM.
252 This sets the following coding systems:
253 o coding system of a newly created buffer
254 o default coding system for subprocess I/O
255 This also sets the following values:
256 o default value used as file-name-coding-system for converting file names.
257 o default value for the command `set-terminal-coding-system' (not on MSDOS)
258 o default value for the command `set-keyboard-coding-system'."
259 (check-coding-system coding-system)
260 (setq-default buffer-file-coding-system coding-system)
261 (if default-enable-multibyte-characters
262 (setq default-file-name-coding-system coding-system))
263 ;; If coding-system is nil, honor that on MS-DOS as well, so
264 ;; that they could reset the terminal coding system.
265 (unless (and (eq window-system 'pc) coding-system)
266 (setq default-terminal-coding-system coding-system))
267 (setq default-keyboard-coding-system coding-system)
268 (setq default-process-coding-system (cons coding-system coding-system)))
270 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
271 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal)
273 (defun prefer-coding-system (coding-system)
274 "Add CODING-SYSTEM at the front of the priority list for automatic detection.
275 This also sets the following coding systems:
276 o coding system of a newly created buffer
277 o default coding system for subprocess I/O
278 This also sets the following values:
279 o default value used as file-name-coding-system for converting file names.
280 o default value for the command `set-terminal-coding-system' (not on MSDOS)
281 o default value for the command `set-keyboard-coding-system'
283 If CODING-SYSTEM specifies a certain type of EOL conversion, the coding
284 systems set by this function will use that type of EOL conversion.
286 This command does not change the default value of terminal coding system
287 for MS-DOS terminal, because DOS terminals only support a single coding
288 system, and Emacs automatically sets the default to that coding system at
290 (interactive "zPrefer coding system: ")
291 (if (not (and coding-system (coding-system-p coding-system)))
292 (error "Invalid coding system `%s'" coding-system))
293 (let ((coding-category (coding-system-category coding-system))
294 (base (coding-system-base coding-system))
295 (eol-type (coding-system-eol-type coding-system)))
296 (if (not coding-category)
297 ;; CODING-SYSTEM is no-conversion or undecided.
298 (error "Can't prefer the coding system `%s'" coding-system))
299 (set coding-category (or base coding-system))
300 (update-coding-systems-internal)
301 (or (eq coding-category (car coding-category-list))
302 ;; We must change the order.
303 (set-coding-priority (list coding-category)))
304 (if (and base (interactive-p))
305 (message "Highest priority is set to %s (base of %s)"
307 ;; If they asked for specific EOL conversion, honor that.
308 (if (memq eol-type '(0 1 2))
310 (coding-system-change-eol-conversion base eol-type))
311 (setq coding-system base))
312 (set-default-coding-systems coding-system)))
314 (defun find-coding-systems-region-subset-p (list1 list2)
315 "Return non-nil if all elements in LIST1 are included in LIST2.
316 Comparison done with EQ."
319 (or (memq (car list1) list2)
321 (setq list1 (cdr list1)))
324 (defun find-coding-systems-region (from to)
325 "Return a list of proper coding systems to encode a text between FROM and TO.
326 All coding systems in the list can safely encode any multibyte characters
329 If the text contains no multibyte characters, return a list of a single
330 element `undecided'."
331 (find-coding-systems-for-charsets (find-charset-region from to)))
333 (defun find-coding-systems-string (string)
334 "Return a list of proper coding systems to encode STRING.
335 All coding systems in the list can safely encode any multibyte characters
338 If STRING contains no multibyte characters, return a list of a single
339 element `undecided'."
340 (find-coding-systems-for-charsets (find-charset-string string)))
342 (defun find-coding-systems-for-charsets (charsets)
343 "Return a list of proper coding systems to encode characters of CHARSETS.
344 CHARSETS is a list of character sets."
345 (if (or (null charsets)
346 (and (= (length charsets) 1)
347 (eq 'ascii (car charsets))))
349 (setq charsets (delq 'composition charsets))
350 (let ((l (coding-system-list 'base-only))
351 (charset-prefered-codings
356 (get-charset-property x 'prefered-coding-system))))
358 (priorities (mapcar (function (lambda (x) (symbol-value x)))
359 coding-category-list))
361 (if (memq 'unknown charsets)
362 ;; The region contains invalid multibyte characters.
363 (setq l '(raw-text)))
365 (setq coding (car l) l (cdr l))
366 (if (and (setq safe (coding-system-get coding 'safe-charsets))
368 (find-coding-systems-region-subset-p charsets safe)))
369 ;; We put the higher priority to coding systems included
370 ;; in CHARSET-PREFERED-CODINGS, and within them, put the
371 ;; higher priority to coding systems which support smaller
372 ;; number of charsets.
374 (+ (if (coding-system-get coding 'mime-charset) 4096 0)
375 (lsh (length (memq coding priorities)) 7)
376 (if (memq coding charset-prefered-codings) 64 0)
377 (if (> (coding-system-type coding) 0) 32 0)
378 (if (consp safe) (- 32 (length safe)) 0))))
379 (setq codings (cons (cons priority coding) codings)))))
381 (sort codings (function (lambda (x y) (> (car x) (car y))))))
384 (defun find-multibyte-characters (from to &optional maxcount excludes)
385 "Find multibyte characters in the region specified by FROM and TO.
386 If FROM is a string, find multibyte characters in the string.
387 The return value is an alist of the following format:
388 ((CHARSET COUNT CHAR ...) ...)
390 CHARSET is a character set,
391 COUNT is a number of characters,
392 CHARs are found characters of the character set.
393 Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list.
394 Optional 4th arg EXCLUDE is a list of character sets to be ignored.
396 For invalid characters, CHARs are actually strings."
401 (while (setq idx (string-match "[^\000-\177]" from idx))
402 (setq char (aref from idx)
403 charset (char-charset char))
404 (if (eq charset 'unknown)
405 (setq char (match-string 0)))
406 (if (or (eq charset 'unknown)
407 (not (or (eq excludes t) (memq charset excludes))))
408 (let ((slot (assq charset chars)))
410 (if (not (memq char (nthcdr 2 slot)))
411 (let ((count (nth 1 slot)))
412 (setcar (cdr slot) (1+ count))
413 (if (or (not maxcount) (< count maxcount))
414 (nconc slot (list char)))))
415 (setq chars (cons (list charset 1 char) chars)))))
416 (setq idx (1+ idx))))
419 (while (re-search-forward "[^\000-\177]" to t)
420 (setq char (preceding-char)
421 charset (char-charset char))
422 (if (eq charset 'unknown)
423 (setq char (match-string 0)))
424 (if (or (eq charset 'unknown)
425 (not (or (eq excludes t) (memq charset excludes))))
426 (let ((slot (assq charset chars)))
428 (if (not (member char (nthcdr 2 slot)))
429 (let ((count (nth 1 slot)))
430 (setcar (cdr slot) (1+ count))
431 (if (or (not maxcount) (< count maxcount))
432 (nconc slot (list char)))))
433 (setq chars (cons (list charset 1 char) chars))))))))
436 (defvar last-coding-system-specified nil
437 "Most recent coding system explicitly specified by the user when asked.
438 This variable is set whenever Emacs asks the user which coding system
439 to use in order to write a file. If you set it to nil explicitly,
440 then call `write-region', then afterward this variable will be non-nil
441 only if the user was explicitly asked and specified a coding system.")
443 (defun select-safe-coding-system (from to &optional default-coding-system)
444 "Ask a user to select a safe coding system from candidates.
445 The candidates of coding systems which can safely encode a text
446 between FROM and TO are shown in a popup window.
448 Optional arg DEFAULT-CODING-SYSTEM specifies a coding system to be
449 checked at first. If omitted, buffer-file-coding-system of the
450 current buffer is used.
452 If the text can be encoded safely by DEFAULT-CODING-SYSTEM, it is
453 returned without any user interaction.
455 Kludgy feature: if FROM is a string, the string is the target text,
457 (or default-coding-system
458 (setq default-coding-system buffer-file-coding-system))
459 (let* ((charsets (if (stringp from) (find-charset-string from)
460 (find-charset-region from to)))
461 (safe-coding-systems (find-coding-systems-for-charsets charsets)))
462 (if (or (not enable-multibyte-characters)
463 (eq (car safe-coding-systems) 'undecided)
464 (eq default-coding-system 'no-conversion)
465 (and default-coding-system
466 (memq (coding-system-base default-coding-system)
467 safe-coding-systems)))
468 default-coding-system
470 ;; At first, change each coding system to the corresponding
471 ;; mime-charset name if it is also a coding system.
472 (let ((l safe-coding-systems)
475 (setq mime-charset (coding-system-get (car l) 'mime-charset))
476 (if (and mime-charset (coding-system-p mime-charset))
477 (setcar l mime-charset))
480 (let ((non-safe-chars (find-multibyte-characters
482 (and default-coding-system
483 (coding-system-get default-coding-system
485 show-position overlays)
487 ;; Highlight characters that default-coding-system can't encode.
488 (when (integerp from)
491 (while (and (not found)
492 (re-search-forward "[^\000-\177]" to t))
493 (setq found (assq (char-charset (preceding-char))
496 (setq show-position (point))
498 (while (and (< (length overlays) 256)
499 (re-search-forward "[^\000-\177]" to t))
500 (let* ((char (preceding-char))
501 (charset (char-charset char)))
502 (when (assq charset non-safe-chars)
503 (setq overlays (cons (make-overlay (1- (point)) (point))
505 (overlay-put (car overlays) 'face 'highlight))))))
507 ;; At last, ask a user to select a proper coding system.
509 (save-window-excursion
511 ;; At first, be sure to show the current buffer.
512 (set-window-buffer (selected-window) (current-buffer))
513 (set-window-start (selected-window) show-position))
514 ;; Then, show a helpful message.
515 (with-output-to-temp-buffer "*Warning*"
517 (set-buffer standard-output)
518 (insert "The target text contains the following non ASCII character(s):\n")
519 (let ((len (length non-safe-chars))
521 (while (and non-safe-chars (< shown 3))
522 (when (> (length (car non-safe-chars)) 2)
523 (setq shown (1+ shown))
524 (insert (format "%25s: " (car (car non-safe-chars))))
525 (let ((l (nthcdr 2 (car non-safe-chars))))
527 (if (or (stringp (car l)) (char-valid-p (car l)))
530 (if (> (nth 1 (car non-safe-chars)) 3)
533 (setq non-safe-chars (cdr non-safe-chars)))
535 (insert (format "%27s\n" "..."))))
537 These can't be encoded safely by the coding system %s.
539 Please select one from the following safe coding systems:\n"
540 default-coding-system))
543 (mapcar (function (lambda (x) (princ " ") (princ x)))
545 (fill-region-as-paragraph pos (point)))))
547 ;; Read a coding system.
548 (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x)))
549 safe-coding-systems))
550 (name (completing-read
551 (format "Select coding system (default %s): "
552 (car safe-coding-systems))
553 safe-names nil t nil nil
554 (car (car safe-names)))))
555 (setq last-coding-system-specified (intern name))
556 (if (integerp (coding-system-eol-type default-coding-system))
557 (setq last-coding-system-specified
558 (coding-system-change-eol-conversion
559 last-coding-system-specified
560 (coding-system-eol-type default-coding-system))))
561 last-coding-system-specified))
562 (kill-buffer "*Warning*")
564 (delete-overlay (car overlays))
565 (setq overlays (cdr overlays)))))))))
567 (setq select-safe-coding-system-function 'select-safe-coding-system)
569 (defun select-message-coding-system ()
570 "Return a coding system to encode the outgoing message of the current buffer.
571 It at first tries the first coding system found in these variables
573 (1) local value of `buffer-file-coding-system'
574 (2) value of `sendmail-coding-system'
575 (3) value of `default-buffer-file-coding-system'
576 (4) value of `default-sendmail-coding-system'
577 If the found coding system can't encode the current buffer,
578 or none of them are bound to a coding system,
579 it asks the user to select a proper coding system."
580 (let ((coding (or (and (local-variable-p 'buffer-file-coding-system)
581 buffer-file-coding-system)
582 sendmail-coding-system
583 default-buffer-file-coding-system
584 default-sendmail-coding-system)))
585 (if (eq coding 'no-conversion)
586 ;; We should never use no-conversion for outgoing mails.
588 (if (fboundp select-safe-coding-system-function)
589 (funcall select-safe-coding-system-function
590 (point-min) (point-max) coding)
593 ;;; Language support stuff.
595 (defvar language-info-alist nil
596 "Alist of language environment definitions.
597 Each element looks like:
598 (LANGUAGE-NAME . ((KEY . INFO) ...))
599 where LANGUAGE-NAME is a string, the name of the language environment,
600 KEY is a symbol denoting the kind of information, and
601 INFO is the data associated with KEY.
602 Meaningful values for KEY include
604 documentation value is documentation of what this language environment
605 is meant for, and how to use it.
606 charset value is a list of the character sets used by this
607 language environment.
608 sample-text value is one line of text,
609 written using those character sets,
610 appropriate for this language environment.
611 setup-function value is a function to call to switch to this
612 language environment.
613 exit-function value is a function to call to leave this
614 language environment.
615 coding-system value is a list of coding systems that are good
616 for saving text written in this language environment.
617 This list serves as suggestions to the user;
618 in effect, as a kind of documentation.
619 coding-priority value is a list of coding systems for this language
620 environment, in order of decreasing priority.
621 This is used to set up the coding system priority
622 list when you switch to this language environment.
624 value is a translation table to be set in the
625 variable `nonascii-translation-table' in this
626 language environment, or a character set from
627 which `nonascii-insert-offset' is calculated.
628 input-method value is a default input method for this language
630 features value is a list of features requested in this
631 language environment.
633 The following keys take effect only when multibyte characters are
634 globally disabled, i.e. the value of `default-enable-multibyte-characters'
637 unibyte-syntax value is a library name to load to set
638 unibyte 8-bit character syntaxes for this
639 language environment.
641 unibyte-display value is a coding system to encode characters
642 for the terminal. Characters in the range
643 of 160 to 255 display not as octal escapes,
644 but as non-ASCII characters in this language
647 (defun get-language-info (lang-env key)
648 "Return information listed under KEY for language environment LANG-ENV.
649 KEY is a symbol denoting the kind of information.
650 For a list of useful values for KEY and their meanings,
651 see `language-info-alist'."
652 (if (symbolp lang-env)
653 (setq lang-env (symbol-name lang-env)))
654 (let ((lang-slot (assoc-ignore-case lang-env language-info-alist)))
656 (cdr (assq key (cdr lang-slot))))))
658 (defun set-language-info (lang-env key info)
659 "Modify part of the definition of language environment LANG-ENV.
660 Specifically, this stores the information INFO under KEY
661 in the definition of this language environment.
662 KEY is a symbol denoting the kind of information.
663 INFO is the value for that information.
665 For a list of useful values for KEY and their meanings,
666 see `language-info-alist'."
667 (if (symbolp lang-env)
668 (setq lang-env (symbol-name lang-env)))
669 (let (lang-slot key-slot)
670 (setq lang-slot (assoc lang-env language-info-alist))
671 (if (null lang-slot) ; If no slot for the language, add it.
672 (setq lang-slot (list lang-env)
673 language-info-alist (cons lang-slot language-info-alist)))
674 (setq key-slot (assq key lang-slot))
675 (if (null key-slot) ; If no slot for the key, add it.
677 (setq key-slot (list key))
678 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
679 (setcdr key-slot info)))
681 (defun set-language-info-alist (lang-env alist &optional parents)
682 "Store ALIST as the definition of language environment LANG-ENV.
683 ALIST is an alist of KEY and INFO values. See the documentation of
684 `set-language-info' for the meanings of KEY and INFO.
686 Optional arg PARENTS is a list of parent menu names; it specifies
687 where to put this language environment in the
688 Describe Language Environment and Set Language Environment menus.
689 For example, (\"European\") means to put this language environment
690 in the European submenu in each of those two menus."
691 (if (symbolp lang-env)
692 (setq lang-env (symbol-name lang-env)))
693 (let ((describe-map describe-language-environment-map)
694 (setup-map setup-language-environment-map))
697 map parent-symbol parent)
699 (if (symbolp (setq parent-symbol (car l)))
700 (setq parent (symbol-name parent))
701 (setq parent parent-symbol parent-symbol (intern parent)))
702 (setq map (lookup-key describe-map (vector parent-symbol)))
705 (setq map (intern (format "describe-%s-environment-map"
707 (define-prefix-command map)
708 (define-key-after describe-map (vector parent-symbol)
709 (cons parent map) t)))
710 (setq describe-map (symbol-value map))
711 (setq map (lookup-key setup-map (vector parent-symbol)))
714 (setq map (intern (format "setup-%s-environment-map"
716 (define-prefix-command map)
717 (define-key-after setup-map (vector parent-symbol)
718 (cons parent map) t)))
719 (setq setup-map (symbol-value map))
722 ;; Set up menu items for this language env.
723 (let ((doc (assq 'documentation alist)))
725 (define-key-after describe-map (vector (intern lang-env))
726 (cons lang-env 'describe-specified-language-support) t)))
727 (define-key-after setup-map (vector (intern lang-env))
728 (cons lang-env 'setup-specified-language-environment) t)
731 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
732 (setq alist (cdr alist)))))
734 (defun read-language-name (key prompt &optional default)
735 "Read a language environment name which has information for KEY.
736 If KEY is nil, read any language environment.
737 Prompt with PROMPT. DEFAULT is the default choice of language environment.
738 This returns a language environment name as a string."
739 (let* ((completion-ignore-case t)
740 (name (completing-read prompt
743 (function (lambda (elm) (assq key elm))))
745 (if (and (> (length name) 0)
747 (get-language-info name key)))
750 ;;; Multilingual input methods.
752 (defconst leim-list-file-name "leim-list.el"
753 "Name of LEIM list file.
754 This file contains a list of libraries of Emacs input methods (LEIM)
755 in the format of Lisp expression for registering each input method.
756 Emacs loads this file at startup time.")
758 (defvar leim-list-header (format "\
759 ;;; %s -- list of LEIM (Library of Emacs Input Method)
761 ;; This file contains a list of LEIM (Library of Emacs Input Method)
762 ;; in the same directory as this file. Loading this file registers
763 ;; the whole input methods in Emacs.
765 ;; Each entry has the form:
766 ;; (register-input-method
767 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
770 ;; See the function `register-input-method' for the meanings of arguments.
772 ;; If this directory is included in load-path, Emacs automatically
773 ;; loads this file at startup time.
777 "Header to be inserted in LEIM list file.")
779 (defvar leim-list-entry-regexp "^(register-input-method"
780 "Regexp matching head of each entry in LEIM list file.
781 See also the variable `leim-list-header'")
783 (defvar update-leim-list-functions
784 '(quail-update-leim-list-file)
785 "List of functions to call to update LEIM list file.
786 Each function is called with one arg, LEIM directory name.")
788 (defun update-leim-list-file (&rest dirs)
789 "Update LEIM list file in directories DIRS."
790 (let ((functions update-leim-list-functions))
792 (apply (car functions) dirs)
793 (setq functions (cdr functions)))))
795 (defvar current-input-method nil
796 "The current input method for multilingual text.
797 If nil, that means no input method is activated now.")
798 (make-variable-buffer-local 'current-input-method)
799 (put 'current-input-method 'permanent-local t)
801 (defvar current-input-method-title nil
802 "Title string of the current input method shown in mode line.")
803 (make-variable-buffer-local 'current-input-method-title)
804 (put 'current-input-method-title 'permanent-local t)
806 (defcustom default-input-method nil
807 "*Default input method for multilingual text (a string).
808 This is the input method activated automatically by the command
809 `toggle-input-method' (\\[toggle-input-method])."
811 :type '(choice (const nil) string))
813 (defvar input-method-history nil
814 "History list for some commands that read input methods.")
815 (make-variable-buffer-local 'input-method-history)
816 (put 'input-method-history 'permanent-local t)
818 (defvar inactivate-current-input-method-function nil
819 "Function to call for inactivating the current input method.
820 Every input method should set this to an appropriate value when activated.
821 This function is called with no argument.
823 This function should never change the value of `current-input-method'.
824 It is set to nil by the function `inactivate-input-method'.")
825 (make-variable-buffer-local 'inactivate-current-input-method-function)
826 (put 'inactivate-current-input-method-function 'permanent-local t)
828 (defvar describe-current-input-method-function nil
829 "Function to call for describing the current input method.
830 This function is called with no argument.")
831 (make-variable-buffer-local 'describe-current-input-method-function)
832 (put 'describe-current-input-method-function 'permanent-local t)
834 (defvar input-method-alist nil
835 "Alist of input method names vs how to use them.
836 Each element has the form:
837 (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...)
838 See the function `register-input-method' for the meanings of the elements.")
840 (defun register-input-method (input-method lang-env &rest args)
841 "Register INPUT-METHOD as an input method for language environment ENV.
842 INPUT-METHOD and LANG-ENV are symbols or strings.
844 The remaining arguments are:
845 ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS...
846 ACTIVATE-FUNC is a function to call to activate this method.
847 TITLE is a string to show in the mode line when this method is active.
848 DESCRIPTION is a string describing this method and what it is good for.
849 The ARGS, if any, are passed as arguments to ACTIVATE-FUNC.
850 All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS.
852 This function is mainly used in the file \"leim-list.el\" which is
853 created at building time of emacs, registering all quail input methods
854 contained in the emacs distribution.
856 In case you want to register a new quail input method by yourself, be
857 careful to use the same input method title as given in the third
858 parameter of `quail-define-package' (if the values are different, the
859 string specified in this function takes precedence).
861 The commands `describe-input-method' and `list-input-methods' need
862 this duplicated values to show some information about input methods
863 without loading the affected quail packages."
864 (if (symbolp lang-env)
865 (setq lang-env (symbol-name lang-env)))
866 (if (symbolp input-method)
867 (setq input-method (symbol-name input-method)))
868 (let ((info (cons lang-env args))
869 (slot (assoc input-method input-method-alist)))
872 (setq slot (cons input-method info))
873 (setq input-method-alist (cons slot input-method-alist)))))
875 (defun read-input-method-name (prompt &optional default inhibit-null)
876 "Read a name of input method from a minibuffer prompting with PROMPT.
877 If DEFAULT is non-nil, use that as the default,
878 and substitute it into PROMPT at the first `%s'.
879 If INHIBIT-NULL is non-nil, null input signals an error.
881 The return value is a string."
883 (setq prompt (format prompt default)))
884 (let* ((completion-ignore-case t)
885 ;; This binding is necessary because input-method-history is
887 (input-method (completing-read prompt input-method-alist
888 nil t nil 'input-method-history
890 (if (> (length input-method) 0)
893 (error "No valid input method is specified")))))
895 (defun activate-input-method (input-method)
896 "Switch to input method INPUT-METHOD for the current buffer.
897 If some other input method is already active, turn it off first.
898 If INPUT-METHOD is nil, deactivate any current input method."
899 (if (and input-method (symbolp input-method))
900 (setq input-method (symbol-name input-method)))
901 (if (and current-input-method
902 (not (string= current-input-method input-method)))
903 (inactivate-input-method))
904 (unless (or current-input-method (null input-method))
905 (let ((slot (assoc input-method input-method-alist)))
907 (error "Can't activate input method `%s'" input-method))
908 (let ((func (nth 2 slot)))
910 (apply (nth 2 slot) input-method (nthcdr 5 slot))
911 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
914 (apply (car func) input-method (nthcdr 5 slot)))
915 (error "Can't activate input method `%s'" input-method))))
916 (setq current-input-method input-method)
917 (setq current-input-method-title (nth 3 slot))
919 (run-hooks 'input-method-activate-hook)
920 (force-mode-line-update)))))
922 (defun inactivate-input-method ()
923 "Turn off the current input method."
924 (when current-input-method
925 (if input-method-history
926 (unless (string= current-input-method (car input-method-history))
927 (setq input-method-history
928 (cons current-input-method
929 (delete current-input-method input-method-history))))
930 (setq input-method-history (list current-input-method)))
932 (funcall inactivate-current-input-method-function)
934 (run-hooks 'input-method-inactivate-hook)
935 (setq current-input-method nil
936 current-input-method-title nil)
937 (force-mode-line-update)))))
939 (defun set-input-method (input-method)
940 "Select and activate input method INPUT-METHOD for the current buffer.
941 This also sets the default input method to the one you specify."
943 (let* ((default (or (car input-method-history) default-input-method)))
944 (list (read-input-method-name
945 (if default "Select input method (default %s): " "Select input method: ")
947 (activate-input-method input-method)
948 (setq default-input-method input-method))
950 (defun toggle-input-method (&optional arg)
951 "Turn on or off a multilingual text input method for the current buffer.
953 With no prefix argument, if some input method is currently activated,
954 turn it off. Otherwise, activate an input method--the one most recently used,
955 or the one specified in `default-input-method', or one read from the
958 With a prefix arg, read an input method from minibuffer and turn it on.
959 The default is the most recent input method specified
960 \(not including the currently active input method, if any).
962 When there's no input method to turn on, turn on what read from minibuffer."
964 (if (and current-input-method (not arg))
965 (inactivate-input-method)
966 (let ((default (or (car input-method-history) default-input-method)))
967 (if (and arg default (equal current-input-method default)
968 (> (length input-method-history) 1))
969 (setq default (nth 1 input-method-history)))
970 (activate-input-method
971 (if (or arg (not default))
973 (read-input-method-name
974 (if default "Input method (default %s): " "Input method: " )
977 (or default-input-method
978 (setq default-input-method current-input-method)))))
980 (defun describe-input-method (input-method)
981 "Describe input method INPUT-METHOD."
983 (list (read-input-method-name
984 "Describe input method (default, current choice): ")))
985 (if (and input-method (symbolp input-method))
986 (setq input-method (symbol-name input-method)))
987 (if (null input-method)
988 (describe-current-input-method)
989 (with-output-to-temp-buffer "*Help*"
990 (let ((elt (assoc input-method input-method-alist)))
991 (princ (format "Input method: %s (`%s' in mode line) for %s\n %s\n"
992 input-method (nth 3 elt) (nth 1 elt) (nth 4 elt)))))))
994 (defun describe-current-input-method ()
995 "Describe the input method currently in use."
996 (if current-input-method
997 (if (and (symbolp describe-current-input-method-function)
998 (fboundp describe-current-input-method-function))
999 (funcall describe-current-input-method-function)
1000 (message "No way to describe the current input method `%s'"
1001 (cdr current-input-method))
1003 (error "No input method is activated now")))
1005 (defun read-multilingual-string (prompt &optional initial-input input-method)
1006 "Read a multilingual string from minibuffer, prompting with string PROMPT.
1007 The input method selected last time is activated in minibuffer.
1008 If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer
1010 Optional 3rd argument INPUT-METHOD specifies the input method
1011 to be activated instead of the one selected last time. It is a symbol
1015 current-input-method
1016 default-input-method
1017 (read-input-method-name "Input method: " nil t)))
1018 (if (and input-method (symbolp input-method))
1019 (setq input-method (symbol-name input-method)))
1020 (let ((prev-input-method current-input-method))
1023 (activate-input-method input-method)
1024 (read-string prompt initial-input nil nil t))
1025 (activate-input-method prev-input-method))))
1027 ;; Variables to control behavior of input methods. All input methods
1028 ;; should react to these variables.
1030 (defcustom input-method-verbose-flag 'default
1031 "*A flag to control extra guidance given by input methods.
1032 The value should be nil, t, `complex-only', or `default'.
1034 The extra guidance is done by showing list of available keys in echo
1035 area. When you use the input method in the minibuffer, the guidance
1036 is shown at the bottom short window (split from the existing window).
1038 If the value is t, extra guidance is always given, if the value is
1039 nil, extra guidance is always suppressed.
1041 If the value is `complex-only', only complex input methods such as
1042 `chinese-py' and `japanese' give extra guidance.
1044 If the value is `default', complex input methods always give extra
1045 guidance, but simple input methods give it only when you are not in
1048 See also the variable `input-method-highlight-flag'."
1049 :type '(choice (const t) (const nil) (const complex-only) (const default))
1052 (defcustom input-method-highlight-flag t
1053 "*If this flag is non-nil, input methods highlight partially-entered text.
1054 For instance, while you are in the middle of a Quail input method sequence,
1055 the text inserted so far is temporarily underlined.
1056 The underlining goes away when you finish or abort the input method sequence.
1057 See also the variable `input-method-verbose-flag'."
1061 (defvar input-method-activate-hook nil
1062 "Normal hook run just after an input method is activated.
1064 The variable `current-input-method' keeps the input method name
1067 (defvar input-method-inactivate-hook nil
1068 "Normal hook run just after an input method is inactivated.
1070 The variable `current-input-method' still keeps the input method name
1073 (defvar input-method-after-insert-chunk-hook nil
1074 "Normal hook run just after an input method insert some chunk of text.")
1076 (defvar input-method-exit-on-first-char nil
1077 "This flag controls a timing when an input method returns.
1078 Usually, the input method does not return while there's a possibility
1079 that it may find a different translation if a user types another key.
1080 But, it this flag is non-nil, the input method returns as soon as
1081 the current key sequence gets long enough to have some valid translation.")
1083 (defvar input-method-use-echo-area nil
1084 "This flag controls how an input method shows an intermediate key sequence.
1085 Usually, the input method inserts the intermediate key sequence,
1086 or candidate translations corresponding to the sequence,
1087 at point in the current buffer.
1088 But, if this flag is non-nil, it displays them in echo area instead.")
1090 (defvar input-method-exit-on-invalid-key nil
1091 "This flag controls the behaviour of an input method on invalid key input.
1092 Usually, when a user types a key which doesn't start any character
1093 handled by the input method, the key is handled by turning off the
1094 input method temporarily. After that key, the input method is re-enabled.
1095 But, if this flag is non-nil, the input method is never back on.")
1098 (defvar set-language-environment-hook nil
1099 "Normal hook run after some language environment is set.
1101 When you set some hook function here, that effect usually should not
1102 be inherited to another language environment. So, you had better set
1103 another function in `exit-language-environment-hook' (which see) to
1104 cancel the effect.")
1106 (defvar exit-language-environment-hook nil
1107 "Normal hook run after exiting from some language environment.
1108 When this hook is run, the variable `current-language-environment'
1109 is still bound to the language environment being exited.
1111 This hook is mainly used for canceling the effect of
1112 `set-language-environment-hook' (which-see).")
1114 (put 'setup-specified-language-environment 'apropos-inhibit t)
1116 (defun setup-specified-language-environment ()
1117 "Switch to a specified language environment."
1119 (let (language-name)
1120 (if (and (symbolp last-command-event)
1121 (or (not (eq last-command-event 'Default))
1122 (setq last-command-event 'English))
1123 (setq language-name (symbol-name last-command-event)))
1124 (set-language-environment language-name)
1125 (error "Bogus calling sequence"))))
1127 (defcustom current-language-environment "English"
1128 "The last language environment specified with `set-language-environment'.
1129 This variable should be set only with \\[customize], which is equivalent
1130 to using the function `set-language-environment'."
1131 :link '(custom-manual "(emacs)Language Environments")
1132 :set (lambda (symbol value) (set-language-environment value))
1134 (or (car-safe (assoc-ignore-case
1135 (if (symbolp current-language-environment)
1136 (symbol-name current-language-environment)
1137 current-language-environment)
1138 language-info-alist))
1140 :type (cons 'choice (mapcar (lambda (lang)
1141 (list 'const (car lang)))
1142 language-info-alist))
1143 :initialize 'custom-initialize-default
1147 (defun reset-language-environment ()
1148 "Reset multilingual environment of Emacs to the default status.
1150 The default status is as follows:
1152 The default value of buffer-file-coding-system is nil.
1153 The default coding system for process I/O is nil.
1154 The default value for the command `set-terminal-coding-system' is nil.
1155 The default value for the command `set-keyboard-coding-system' is nil.
1157 The order of priorities of coding categories and the coding system
1158 bound to each category are as follows
1159 coding category coding system
1160 --------------------------------------------------
1161 coding-category-iso-8-2 iso-latin-1
1162 coding-category-iso-8-1 iso-latin-1
1163 coding-category-iso-7-tight iso-2022-jp
1164 coding-category-iso-7 iso-2022-7bit
1165 coding-category-iso-7-else iso-2022-7bit-lock
1166 coding-category-iso-8-else iso-2022-8bit-ss2
1167 coding-category-emacs-mule emacs-mule
1168 coding-category-raw-text raw-text
1169 coding-category-sjis japanese-shift-jis
1170 coding-category-big5 chinese-big5
1171 coding-category-ccl nil
1172 coding-category-binary no-conversion
1175 ;; This function formerly set default-enable-multibyte-characters to t,
1176 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1178 (setq coding-category-iso-7-tight 'iso-2022-jp
1179 coding-category-iso-7 'iso-2022-7bit
1180 coding-category-iso-8-1 'iso-latin-1
1181 coding-category-iso-8-2 'iso-latin-1
1182 coding-category-iso-7-else 'iso-2022-7bit-lock
1183 coding-category-iso-8-else 'iso-2022-8bit-ss2
1184 coding-category-emacs-mule 'emacs-mule
1185 coding-category-raw-text 'raw-text
1186 coding-category-sjis 'japanese-shift-jis
1187 coding-category-big5 'chinese-big5
1188 coding-category-ccl nil
1189 coding-category-binary 'no-conversion)
1191 (set-coding-priority
1192 '(coding-category-iso-8-1
1193 coding-category-iso-8-2
1194 coding-category-iso-7-tight
1195 coding-category-iso-7
1196 coding-category-iso-7-else
1197 coding-category-iso-8-else
1198 coding-category-emacs-mule
1199 coding-category-raw-text
1200 coding-category-sjis
1201 coding-category-big5
1203 coding-category-binary))
1205 (update-coding-systems-internal)
1207 (set-default-coding-systems nil)
1208 ;; Don't alter the terminal and keyboard coding systems here.
1209 ;; The terminal still supports the same coding system
1210 ;; that it supported a minute ago.
1211 ;;; (set-terminal-coding-system-internal nil)
1212 ;;; (set-keyboard-coding-system-internal nil)
1214 (setq nonascii-translation-table nil
1215 nonascii-insert-offset 0))
1217 (defun set-language-environment (language-name)
1218 "Set up multi-lingual environment for using LANGUAGE-NAME.
1219 This sets the coding system priority and the default input method
1220 and sometimes other things. LANGUAGE-NAME should be a string
1221 which is the name of a language environment. For example, \"Latin-1\"
1222 specifies the character set for the major languages of Western Europe."
1223 (interactive (list (read-language-name
1225 "Set language environment (default, English): ")))
1227 (if (symbolp language-name)
1228 (setq language-name (symbol-name language-name)))
1229 (setq language-name "English"))
1230 (or (assoc-ignore-case language-name language-info-alist)
1231 (error "Language environment not defined: %S" language-name))
1232 (if current-language-environment
1233 (let ((func (get-language-info current-language-environment
1235 (run-hooks 'exit-language-environment-hook)
1236 (if (fboundp func) (funcall func))))
1237 (let ((default-eol-type (coding-system-eol-type
1238 default-buffer-file-coding-system)))
1239 (reset-language-environment)
1241 (setq current-language-environment language-name)
1242 (set-language-environment-coding-systems language-name default-eol-type))
1243 (let ((input-method (get-language-info language-name 'input-method)))
1245 (setq default-input-method input-method)
1246 (if input-method-history
1247 (setq input-method-history
1249 (delete input-method input-method-history))))))
1250 (let ((nonascii (get-language-info language-name 'nonascii-translation))
1252 (if (eq window-system 'pc)
1254 (concat "cp" dos-codepage "-nonascii-translation-table")))))
1256 ((char-table-p nonascii)
1257 (setq nonascii-translation-table nonascii))
1258 ((and (eq window-system 'pc) (boundp dos-table))
1259 ;; DOS terminals' default is to use a special non-ASCII translation
1260 ;; table as appropriate for the installed codepage.
1261 (setq nonascii-translation-table (symbol-value dos-table)))
1262 ((charsetp nonascii)
1263 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1265 (setq charset-origin-alist
1266 (get-language-info language-name 'charset-origin-alist))
1268 ;; Unibyte setups if necessary.
1269 (unless default-enable-multibyte-characters
1270 ;; Syntax and case table.
1271 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1273 (let ((set-case-syntax-set-multibyte nil))
1274 (load syntax nil t))
1275 ;; No information for syntax and case. Reset to the defaults.
1276 (let ((syntax-table (standard-syntax-table))
1277 (case-table (standard-case-table))
1278 (ch (if (eq window-system 'pc) 128 160)))
1280 (modify-syntax-entry ch " " syntax-table)
1281 (aset case-table ch ch)
1283 (set-char-table-extra-slot case-table 0 nil)
1284 (set-char-table-extra-slot case-table 1 nil)
1285 (set-char-table-extra-slot case-table 2 nil))
1286 (set-standard-case-table (standard-case-table))
1287 (let ((list (buffer-list)))
1289 (with-current-buffer (car list)
1290 (set-case-table (standard-case-table)))
1291 (setq list (cdr list))))))
1292 ;; Display table and coding system for terminal.
1293 (let ((coding (get-language-info language-name 'unibyte-display)))
1295 (standard-display-european-internal)
1296 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1297 (aset standard-display-table 146 nil))
1298 (or (eq window-system 'pc)
1299 (set-terminal-coding-system coding))))
1301 (let ((required-features (get-language-info language-name 'features)))
1302 (while required-features
1303 (require (car required-features))
1304 (setq required-features (cdr required-features))))
1305 (let ((func (get-language-info language-name 'setup-function)))
1308 (run-hooks 'set-language-environment-hook)
1309 (force-mode-line-update t))
1311 (defun standard-display-european-internal ()
1312 ;; Actually set up direct output of non-ASCII characters.
1313 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1314 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1315 ;; the native font, and codes 160 and 146 stand for something very
1317 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1319 ;; Make non-line-break space display as a plain space.
1320 ;; Most X fonts do the wrong thing for code 160.
1321 (aset standard-display-table 160 [32])
1322 ;; Most Windows programs send out apostrophe's as \222. Most X fonts
1323 ;; don't contain a character at that position. Map it to the ASCII
1325 (aset standard-display-table 146 [39]))))
1327 (defun set-language-environment-coding-systems (language-name
1329 "Do various coding system setups for language environment LANGUAGE-NAME.
1331 The optional arg EOL-TYPE specifies the eol-type of the default value
1332 of buffer-file-coding-system set by this function."
1333 (let* ((priority (get-language-info language-name 'coding-priority))
1334 (default-coding (car priority)))
1336 (let ((categories (mapcar 'coding-system-category priority)))
1337 (set-default-coding-systems
1338 (if (memq eol-type '(0 1 2 unix dos mac))
1339 (coding-system-change-eol-conversion default-coding eol-type)
1341 (setq default-sendmail-coding-system default-coding)
1342 (set-coding-priority categories)
1344 (set (car categories) (car priority))
1345 (setq priority (cdr priority) categories (cdr categories)))
1346 (update-coding-systems-internal)))))
1348 ;; Print all arguments with `princ', then print "\n".
1349 (defsubst princ-list (&rest args)
1350 (while args (princ (car args)) (setq args (cdr args)))
1353 (put 'describe-specified-language-support 'apropos-inhibit t)
1355 ;; Print a language specific information such as input methods,
1356 ;; charsets, and coding systems. This function is intended to be
1357 ;; called from the menu:
1358 ;; [menu-bar mule describe-language-environment LANGUAGE]
1359 ;; and should not run it by `M-x describe-current-input-method-function'.
1360 (defun describe-specified-language-support ()
1361 "Describe how Emacs supports the specified language environment."
1363 (let (language-name)
1364 (if (not (and (symbolp last-command-event)
1365 (setq language-name (symbol-name last-command-event))))
1366 (error "Bogus calling sequence"))
1367 (describe-language-environment language-name)))
1369 (defun describe-language-environment (language-name)
1370 "Describe how Emacs supports language environment LANGUAGE-NAME."
1372 (list (read-language-name
1374 "Describe language environment (default, current choice): ")))
1375 (if (null language-name)
1376 (setq language-name current-language-environment))
1377 (if (or (null language-name)
1378 (null (get-language-info language-name 'documentation)))
1379 (error "No documentation for the specified language"))
1380 (if (symbolp language-name)
1381 (setq language-name (symbol-name language-name)))
1382 (let ((doc (get-language-info language-name 'documentation)))
1383 (with-output-to-temp-buffer "*Help*"
1384 (princ-list language-name " language environment" "\n")
1389 (let ((str (get-language-info language-name 'sample-text)))
1392 (princ "Sample text:\n")
1393 (princ-list " " str)
1395 (let ((input-method (get-language-info language-name 'input-method))
1396 (l (copy-sequence input-method-alist)))
1397 (princ "Input methods")
1399 (princ (format " (default, %s)" input-method))
1400 (setq input-method (assoc input-method input-method-alist))
1401 (setq l (cons input-method (delete input-method l))))
1404 (if (string= language-name (nth 1 (car l)))
1405 (princ-list " " (car (car l))
1406 (format " (`%s' in mode line)" (nth 3 (car l)))))
1409 (princ "Character sets:\n")
1410 (let ((l (get-language-info language-name 'charset)))
1412 (princ-list " nothing specific to " language-name)
1414 (princ-list " " (car l) ": "
1415 (charset-description (car l)))
1418 (princ "Coding systems:\n")
1419 (let ((l (get-language-info language-name 'coding-system)))
1421 (princ-list " nothing specific to " language-name)
1423 (princ (format " %s (`%c' in mode line):\n\t%s\n"
1425 (coding-system-mnemonic (car l))
1426 (coding-system-doc-string (car l))))
1427 (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
1430 (princ (cons 'alias: (cdr aliases)))
1432 (setq l (cdr l))))))))
1434 ;;; Charset property
1436 (defun get-charset-property (charset propname)
1437 "Return the value of CHARSET's PROPNAME property.
1438 This is the last value stored with
1439 (put-charset-property CHARSET PROPNAME VALUE)."
1440 (and (not (eq charset 'composition))
1441 (plist-get (charset-plist charset) propname)))
1443 (defun put-charset-property (charset propname value)
1444 "Store CHARSETS's PROPNAME property with value VALUE.
1445 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1446 (or (eq charset 'composition)
1447 (set-charset-plist charset
1448 (plist-put (charset-plist charset) propname value))))
1450 ;;; Character code property
1451 (put 'char-code-property-table 'char-table-extra-slots 0)
1453 (defvar char-code-property-table
1454 (make-char-table 'char-code-property-table)
1455 "Char-table containing a property list of each character code.
1457 See also the documentation of `get-char-code-property' and
1458 `put-char-code-property'.")
1460 (defun get-char-code-property (char propname)
1461 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1462 (let ((plist (aref char-code-property-table char)))
1464 (car (cdr (memq propname plist))))))
1466 (defun put-char-code-property (char propname value)
1467 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1468 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1469 (let ((plist (aref char-code-property-table char)))
1471 (let ((slot (memq propname plist)))
1473 (setcar (cdr slot) value)
1474 (nconc plist (list propname value))))
1475 (aset char-code-property-table char (list propname value)))))
1478 ;; Pretty description of encoded string
1480 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
1481 (defvar iso-2022-control-alist
1489 (defun encoded-string-description (str coding-system)
1490 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
1491 (setq str (string-as-unibyte str))
1492 (let ((char (aref str 0))
1495 (setq desc (or (cdr (assq char iso-2022-control-alist))
1496 (char-to-string char)))
1500 (setq char (aref str i))
1502 (setq desc nil i len)
1503 (setq desc (concat desc " "
1504 (or (cdr (assq char iso-2022-control-alist))
1505 (char-to-string char)))
1508 (mapconcat (function (lambda (x) (format "0x%02x" x))) str " "))))
1510 (defun encode-coding-char (char coding-system)
1511 "Encode CHAR by CODING-SYSTEM and return the resulting string.
1512 If CODING-SYSTEM can't safely encode CHAR, return nil."
1514 (setq char (car (decompose-composite-char char 'list))))
1515 (let ((str1 (char-to-string char))
1516 (str2 (make-string 2 char))
1517 (safe-charsets (and coding-system
1518 (coding-system-get coding-system 'safe-charsets)))
1520 (when (or (eq safe-charsets t)
1521 (memq (char-charset char) safe-charsets))
1522 ;; We must find the encoded string of CHAR. But, just encoding
1523 ;; CHAR will put extra control sequences (usually to designate
1524 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
1525 ;; To exclude such tailing bytes, we at first encode one-char
1526 ;; string and two-char string, then check how many bytes at the
1527 ;; tail of both encoded strings are the same.
1529 (setq enc1 (string-as-unibyte (encode-coding-string str1 coding-system))
1531 enc2 (string-as-unibyte (encode-coding-string str2 coding-system))
1533 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
1534 (setq i1 (1- i1) i2 (1- i2)))
1536 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
1537 ;; and they are the extra control sequences at the tail to
1539 (substring enc2 0 i2))))
1542 ;;; mule-cmds.el ends here