]> code.delx.au - gnu-emacs/blob - lisp/international/mule-cmds.el
1cc43e8b640f5329490de78d1b673f57340d91d3
[gnu-emacs] / lisp / international / mule-cmds.el
1 ;;; mule-cmds.el --- Commands for mulitilingual environment
2
3 ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN.
4 ;; Licensed to the Free Software Foundation.
5
6 ;; Keywords: mule, multilingual
7
8 ;; This file is part of GNU Emacs.
9
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)
13 ;; any later version.
14
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.
19
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.
24
25 ;;; Code:
26
27 ;;; MULE related key bindings and menus.
28
29 (defvar mule-keymap (make-sparse-keymap)
30 "Keymap for MULE (Multilingual environment) specific commands.")
31
32 ;; Keep "C-x C-m ..." for mule specific commands.
33 (define-key ctl-x-map "\C-m" mule-keymap)
34
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)
44
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)
51
52 (defvar mule-menu-keymap (make-sparse-keymap "Mule")
53 "Keymap for MULE (Multilingual environment) menu specific commands.")
54
55 (define-key global-map [menu-bar mule]
56 `(menu-item "Mule" ,mule-menu-keymap
57 :visible default-enable-multibyte-characters))
58
59 (setq menu-bar-final-items (cons 'mule menu-bar-final-items))
60
61 (defvar describe-language-environment-map
62 (make-sparse-keymap "Describe Language Environment"))
63
64 (defvar setup-language-environment-map
65 (make-sparse-keymap "Set Language Environment"))
66
67 (defvar set-coding-system-map
68 (make-sparse-keymap "Set Coding System"))
69
70 (define-key-after mule-menu-keymap [describe-language-environment]
71 (cons "Describe Language Environment" describe-language-environment-map)
72 t)
73 (define-key-after mule-menu-keymap [set-language-environment]
74 (cons "Set Language Environment" setup-language-environment-map)
75 t)
76 (define-key-after mule-menu-keymap [mouse-set-font]
77 '("Set Font/Fontset" . mouse-set-font)
78 t)
79 (define-key-after mule-menu-keymap [separator-mule]
80 '("--")
81 t)
82 (define-key-after mule-menu-keymap [toggle-input-method]
83 '("Toggle Input Method" . toggle-input-method)
84 t)
85 (define-key-after mule-menu-keymap [set-input-method]
86 '("Select Input Method" . set-input-method)
87 t)
88 (define-key-after mule-menu-keymap [describe-input-method]
89 '("Describe Input Method" . describe-input-method)
90 t)
91 (define-key-after mule-menu-keymap [separator-input-method]
92 '("--")
93 t)
94 (define-key-after mule-menu-keymap [describe-coding-system]
95 '("Describe Coding Systems" . describe-coding-system)
96 t)
97 (define-key-after mule-menu-keymap [set-various-coding-system]
98 (cons "Set Coding System" set-coding-system-map)
99 t)
100 (define-key-after mule-menu-keymap [separator-coding-system]
101 '("--")
102 t)
103 (define-key-after mule-menu-keymap [mule-diag]
104 '("Show All of MULE Status" . mule-diag)
105 t)
106 (define-key-after mule-menu-keymap [view-hello-file]
107 '("Show Script Examples" . view-hello-file)
108 t)
109
110 (define-key-after set-coding-system-map [set-buffer-file-coding-system]
111 '("Buffer File" . set-buffer-file-coding-system)
112 t)
113 (define-key-after set-coding-system-map [universal-coding-system-argument]
114 '("Next Command" . universal-coding-system-argument)
115 t)
116 (define-key-after set-coding-system-map [set-terminal-coding-system]
117 '("Terminal" . set-terminal-coding-system)
118 t)
119 (define-key-after set-coding-system-map [set-keyboard-coding-system]
120 '("Keyboard" . set-keyboard-coding-system)
121 t)
122 (define-key-after set-coding-system-map [set-buffer-process-coding-system]
123 '("Buffer Process" . set-buffer-process-coding-system)
124 t)
125 (define-key-after set-coding-system-map [set-selection-coding-system]
126 '("X Selection" . set-selection-coding-system)
127 t)
128 (define-key-after set-coding-system-map [set-next-selection-coding-system]
129 '("Next X Selection" . set-next-selection-coding-system)
130 t)
131 (define-key setup-language-environment-map
132 [Default] '("Default" . setup-specified-language-environment))
133
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
144 'window-system)
145 (put 'set-next-selection-coding-system 'menu-enable
146 'window-system)
147
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)
155
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)
160
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.
168
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)
175 (t eol-type))))
176 (let ((orig-eol-type (coding-system-eol-type coding-system)))
177 (if (vectorp orig-eol-type)
178 (if (not eol-type)
179 coding-system
180 (aref orig-eol-type eol-type))
181 (let ((base (coding-system-base coding-system)))
182 (if (not eol-type)
183 base
184 (if (= eol-type orig-eol-type)
185 coding-system
186 (setq orig-eol-type (coding-system-eol-type base))
187 (if (vectorp orig-eol-type)
188 (aref orig-eol-type eol-type))))))))
189
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."
196 (if (not coding)
197 (coding-system-base coding-system)
198 (let ((eol-type (coding-system-eol-type coding-system)))
199 (coding-system-change-eol-conversion
200 coding
201 (if (numberp eol-type) (aref [unix dos mac] eol-type))))))
202
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.
206
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.
211
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."
215 (interactive "P")
216 (let ((new-flag
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))
221
222 (defun view-hello-file ()
223 "Display the HELLO file which list up many languages and characters."
224 (interactive)
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))))
229
230 (defun universal-coding-system-argument ()
231 "Execute an I/O command using the specified coding system."
232 (interactive)
233 (let* ((default (and buffer-file-coding-system
234 (not (eq (coding-system-type buffer-file-coding-system)
235 t))
236 buffer-file-coding-system))
237 (coding-system (read-coding-system
238 (if default
239 (format "Coding system for following command (default, %s): " default)
240 "Coding system for following command: ")
241 default))
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))
247 (message "")
248 (call-interactively cmd))))
249
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)))
269
270 (defalias 'update-iso-coding-systems 'update-coding-systems-internal)
271 (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal)
272
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'
282
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.
285
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
289 startup."
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)"
306 base coding-system))
307 ;; If they asked for specific EOL conversion, honor that.
308 (if (memq eol-type '(0 1 2))
309 (setq coding-system
310 (coding-system-change-eol-conversion base eol-type))
311 (setq coding-system base))
312 (set-default-coding-systems coding-system)))
313
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."
317 (catch 'tag
318 (while list1
319 (or (memq (car list1) list2)
320 (throw 'tag nil))
321 (setq list1 (cdr list1)))
322 t))
323
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
327 in the text.
328
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)))
332
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
336 in STRING.
337
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)))
341
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))))
348 '(undecided)
349 (setq charsets (delq 'composition charsets))
350 (let ((l (coding-system-list 'base-only))
351 (charset-prefered-codings
352 (mapcar (function
353 (lambda (x)
354 (if (eq x 'unknown)
355 'raw-text
356 (get-charset-property x 'prefered-coding-system))))
357 charsets))
358 (priorities (mapcar (function (lambda (x) (symbol-value x)))
359 coding-category-list))
360 codings coding safe)
361 (if (memq 'unknown charsets)
362 ;; The region contains invalid multibyte characters.
363 (setq l '(raw-text)))
364 (while l
365 (setq coding (car l) l (cdr l))
366 (if (and (setq safe (coding-system-get coding 'safe-charsets))
367 (or (eq safe t)
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.
373 (let ((priority
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)))))
380 (mapcar 'cdr
381 (sort codings (function (lambda (x y) (> (car x) (car y))))))
382 )))
383
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 ...) ...)
389 where
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.
395
396 For invalid characters, CHARs are actually strings."
397 (let ((chars nil)
398 charset char)
399 (if (stringp from)
400 (let ((idx 0))
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)))
409 (if slot
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))))
417 (save-excursion
418 (goto-char from)
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)))
427 (if slot
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))))))))
434 (nreverse chars)))
435
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.")
442
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.
447
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.
451
452 If the text can be encoded safely by DEFAULT-CODING-SYSTEM, it is
453 returned without any user interaction.
454
455 Kludgy feature: if FROM is a string, the string is the target text,
456 and TO is ignored."
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
469
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)
473 mime-charset)
474 (while l
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))
478 (setq l (cdr l))))
479
480 (let ((non-safe-chars (find-multibyte-characters
481 from to 3
482 (and default-coding-system
483 (coding-system-get default-coding-system
484 'safe-charsets))))
485 show-position overlays)
486 (save-excursion
487 ;; Highlight characters that default-coding-system can't encode.
488 (when (integerp from)
489 (goto-char from)
490 (let ((found nil))
491 (while (and (not found)
492 (re-search-forward "[^\000-\177]" to t))
493 (setq found (assq (char-charset (preceding-char))
494 non-safe-chars))))
495 (forward-line -1)
496 (setq show-position (point))
497 (save-excursion
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))
504 overlays))
505 (overlay-put (car overlays) 'face 'highlight))))))
506
507 ;; At last, ask a user to select a proper coding system.
508 (unwind-protect
509 (save-window-excursion
510 (when show-position
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*"
516 (save-excursion
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))
520 (shown 0))
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))))
526 (while l
527 (if (or (stringp (car l)) (char-valid-p (car l)))
528 (insert (car l)))
529 (setq l (cdr l))))
530 (if (> (nth 1 (car non-safe-chars)) 3)
531 (insert "..."))
532 (insert "\n"))
533 (setq non-safe-chars (cdr non-safe-chars)))
534 (if (< shown len)
535 (insert (format "%27s\n" "..."))))
536 (insert (format "\
537 These can't be encoded safely by the coding system %s.
538
539 Please select one from the following safe coding systems:\n"
540 default-coding-system))
541 (let ((pos (point))
542 (fill-prefix " "))
543 (mapcar (function (lambda (x) (princ " ") (princ x)))
544 safe-coding-systems)
545 (fill-region-as-paragraph pos (point)))))
546
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*")
563 (while overlays
564 (delete-overlay (car overlays))
565 (setq overlays (cdr overlays)))))))))
566
567 (setq select-safe-coding-system-function 'select-safe-coding-system)
568
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
572 in this order:
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.
587 (setq coding nil))
588 (if (fboundp select-safe-coding-system-function)
589 (funcall select-safe-coding-system-function
590 (point-min) (point-max) coding)
591 coding)))
592 \f
593 ;;; Language support stuff.
594
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
603
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.
623 nonascii-translation
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
629 environment.
630 features value is a list of features requested in this
631 language environment.
632
633 The following keys take effect only when multibyte characters are
634 globally disabled, i.e. the value of `default-enable-multibyte-characters'
635 is nil.
636
637 unibyte-syntax value is a library name to load to set
638 unibyte 8-bit character syntaxes for this
639 language environment.
640
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
645 environment.")
646
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)))
655 (if lang-slot
656 (cdr (assq key (cdr lang-slot))))))
657
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.
664
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.
676 (progn
677 (setq key-slot (list key))
678 (setcdr lang-slot (cons key-slot (cdr lang-slot)))))
679 (setcdr key-slot info)))
680
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.
685
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))
695 (if parents
696 (let ((l parents)
697 map parent-symbol parent)
698 (while l
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)))
703 (if (not map)
704 (progn
705 (setq map (intern (format "describe-%s-environment-map"
706 (downcase parent))))
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)))
712 (if (not map)
713 (progn
714 (setq map (intern (format "setup-%s-environment-map"
715 (downcase parent))))
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))
720 (setq l (cdr l)))))
721
722 ;; Set up menu items for this language env.
723 (let ((doc (assq 'documentation alist)))
724 (when doc
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)
729
730 (while alist
731 (set-language-info lang-env (car (car alist)) (cdr (car alist)))
732 (setq alist (cdr alist)))))
733
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
741 language-info-alist
742 (and key
743 (function (lambda (elm) (assq key elm))))
744 t nil nil default)))
745 (if (and (> (length name) 0)
746 (or (not key)
747 (get-language-info name key)))
748 name)))
749 \f
750 ;;; Multilingual input methods.
751
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.")
757
758 (defvar leim-list-header (format "\
759 ;;; %s -- list of LEIM (Library of Emacs Input Method)
760 ;;
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.
764 ;;
765 ;; Each entry has the form:
766 ;; (register-input-method
767 ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC
768 ;; TITLE DESCRIPTION
769 ;; ARG ...)
770 ;; See the function `register-input-method' for the meanings of arguments.
771 ;;
772 ;; If this directory is included in load-path, Emacs automatically
773 ;; loads this file at startup time.
774
775 "
776 leim-list-file-name)
777 "Header to be inserted in LEIM list file.")
778
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'")
782
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.")
787
788 (defun update-leim-list-file (&rest dirs)
789 "Update LEIM list file in directories DIRS."
790 (let ((functions update-leim-list-functions))
791 (while functions
792 (apply (car functions) dirs)
793 (setq functions (cdr functions)))))
794
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)
800
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)
805
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])."
810 :group 'mule
811 :type '(choice (const nil) string))
812
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)
817
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.
822
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)
827
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)
833
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.")
839
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.
843
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.
851
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.
855
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).
860
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)))
870 (if slot
871 (setcdr slot info)
872 (setq slot (cons input-method info))
873 (setq input-method-alist (cons slot input-method-alist)))))
874
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.
880
881 The return value is a string."
882 (if default
883 (setq prompt (format prompt default)))
884 (let* ((completion-ignore-case t)
885 ;; This binding is necessary because input-method-history is
886 ;; buffer local.
887 (input-method (completing-read prompt input-method-alist
888 nil t nil 'input-method-history
889 default)))
890 (if (> (length input-method) 0)
891 input-method
892 (if inhibit-null
893 (error "No valid input method is specified")))))
894
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)))
906 (if (null slot)
907 (error "Can't activate input method `%s'" input-method))
908 (let ((func (nth 2 slot)))
909 (if (functionp func)
910 (apply (nth 2 slot) input-method (nthcdr 5 slot))
911 (if (and (consp func) (symbolp (car func)) (symbolp (cdr func)))
912 (progn
913 (require (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))
918 (unwind-protect
919 (run-hooks 'input-method-activate-hook)
920 (force-mode-line-update)))))
921
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)))
931 (unwind-protect
932 (funcall inactivate-current-input-method-function)
933 (unwind-protect
934 (run-hooks 'input-method-inactivate-hook)
935 (setq current-input-method nil
936 current-input-method-title nil)
937 (force-mode-line-update)))))
938
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."
942 (interactive
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: ")
946 default t))))
947 (activate-input-method input-method)
948 (setq default-input-method input-method))
949
950 (defun toggle-input-method (&optional arg)
951 "Turn on or off a multilingual text input method for the current buffer.
952
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
956 minibuffer.
957
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).
961
962 When there's no input method to turn on, turn on what read from minibuffer."
963 (interactive "P")
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))
972 (progn
973 (read-input-method-name
974 (if default "Input method (default %s): " "Input method: " )
975 default t))
976 default))
977 (or default-input-method
978 (setq default-input-method current-input-method)))))
979
980 (defun describe-input-method (input-method)
981 "Describe input method INPUT-METHOD."
982 (interactive
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)))))))
993
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))
1002 (ding))
1003 (error "No input method is activated now")))
1004
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
1009 initially.
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
1012 or a string."
1013 (setq input-method
1014 (or input-method
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))
1021 (unwind-protect
1022 (progn
1023 (activate-input-method input-method)
1024 (read-string prompt initial-input nil nil t))
1025 (activate-input-method prev-input-method))))
1026
1027 ;; Variables to control behavior of input methods. All input methods
1028 ;; should react to these variables.
1029
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'.
1033
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).
1037
1038 If the value is t, extra guidance is always given, if the value is
1039 nil, extra guidance is always suppressed.
1040
1041 If the value is `complex-only', only complex input methods such as
1042 `chinese-py' and `japanese' give extra guidance.
1043
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
1046 the minibuffer.
1047
1048 See also the variable `input-method-highlight-flag'."
1049 :type '(choice (const t) (const nil) (const complex-only) (const default))
1050 :group 'mule)
1051
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'."
1058 :type 'boolean
1059 :group 'mule)
1060
1061 (defvar input-method-activate-hook nil
1062 "Normal hook run just after an input method is activated.
1063
1064 The variable `current-input-method' keeps the input method name
1065 just activated.")
1066
1067 (defvar input-method-inactivate-hook nil
1068 "Normal hook run just after an input method is inactivated.
1069
1070 The variable `current-input-method' still keeps the input method name
1071 just inactivated.")
1072
1073 (defvar input-method-after-insert-chunk-hook nil
1074 "Normal hook run just after an input method insert some chunk of text.")
1075
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.")
1082
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.")
1089
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.")
1096
1097 \f
1098 (defvar set-language-environment-hook nil
1099 "Normal hook run after some language environment is set.
1100
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.")
1105
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.
1110
1111 This hook is mainly used for canceling the effect of
1112 `set-language-environment-hook' (which-see).")
1113
1114 (put 'setup-specified-language-environment 'apropos-inhibit t)
1115
1116 (defun setup-specified-language-environment ()
1117 "Switch to a specified language environment."
1118 (interactive)
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"))))
1126
1127 (defcustom current-language-environment "English"
1128 "The last language environment specified with `set-language-environment'.
1129 This variable should only be set with Customize, which is equivalent
1130 to using `set-language-environment'."
1131 :link '(custom-manual "(emacs)Language Environments")
1132 :set (lambda (symbol value) (set-language-environment value))
1133 :get (lambda (x)
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))
1139 "English"))
1140 :type (cons 'choice (mapcar (lambda (lang)
1141 (list 'const (car lang)))
1142 language-info-alist))
1143 :initialize 'custom-initialize-default
1144 :group 'mule
1145 :type 'string)
1146
1147 (defun reset-language-environment ()
1148 "Reset multilingual environment of Emacs to the default status.
1149
1150 The default status is as follows:
1151
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.
1156
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
1173 "
1174 (interactive)
1175 ;; This function formerly set default-enable-multibyte-characters to t,
1176 ;; but that is incorrect. It should not alter the unibyte/multibyte choice.
1177
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)
1190
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
1202 coding-category-ccl
1203 coding-category-binary))
1204
1205 (update-coding-systems-internal)
1206
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)
1213
1214 (setq nonascii-translation-table nil
1215 nonascii-insert-offset 0))
1216
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
1224 nil
1225 "Set language environment (default, English): ")))
1226 (if language-name
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
1234 'exit-function)))
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)
1240
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)))
1244 (when input-method
1245 (setq default-input-method input-method)
1246 (if input-method-history
1247 (setq input-method-history
1248 (cons input-method
1249 (delete input-method input-method-history))))))
1250 (let ((nonascii (get-language-info language-name 'nonascii-translation)))
1251 (cond
1252 ((char-table-p nonascii)
1253 (setq nonascii-translation-table nonascii))
1254 ((eq window-system 'pc)
1255 ;; DOS terminals' default is to use a special non-ASCII translation
1256 ;; table as appropriate for the installed codepage.
1257 (setq
1258 nonascii-translation-table (symbol-value
1259 (intern
1260 (concat "cp"
1261 dos-codepage
1262 "-nonascii-translation-table")))))
1263 ((charsetp nonascii)
1264 (setq nonascii-insert-offset (- (make-char nonascii) 128)))))
1265
1266 (setq charset-origin-alist
1267 (get-language-info language-name 'charset-origin-alist))
1268
1269 ;; Unibyte setups if necessary.
1270 (unless default-enable-multibyte-characters
1271 ;; Syntax and case table.
1272 (let ((syntax (get-language-info language-name 'unibyte-syntax)))
1273 (if syntax
1274 (let ((set-case-syntax-set-multibyte nil))
1275 (load syntax nil t))
1276 ;; No information for syntax and case. Reset to the defaults.
1277 (let ((syntax-table (standard-syntax-table))
1278 (case-table (standard-case-table))
1279 (ch (if (eq window-system 'pc) 128 160)))
1280 (while (< ch 256)
1281 (modify-syntax-entry ch " " syntax-table)
1282 (aset case-table ch ch)
1283 (setq ch (1+ ch)))
1284 (set-char-table-extra-slot case-table 0 nil)
1285 (set-char-table-extra-slot case-table 1 nil)
1286 (set-char-table-extra-slot case-table 2 nil))
1287 (set-standard-case-table (standard-case-table))
1288 (let ((list (buffer-list)))
1289 (while list
1290 (with-current-buffer (car list)
1291 (set-case-table (standard-case-table)))
1292 (setq list (cdr list))))))
1293 ;; Display table and coding system for terminal.
1294 (let ((coding (get-language-info language-name 'unibyte-display)))
1295 (if coding
1296 (standard-display-european-internal)
1297 (standard-display-default (if (eq window-system 'pc) 128 160) 255)
1298 (aset standard-display-table 146 nil))
1299 (or (eq window-system 'pc)
1300 (set-terminal-coding-system coding))))
1301
1302 (let ((required-features (get-language-info language-name 'features)))
1303 (while required-features
1304 (require (car required-features))
1305 (setq required-features (cdr required-features))))
1306 (let ((func (get-language-info language-name 'setup-function)))
1307 (if (fboundp func)
1308 (funcall func)))
1309 (run-hooks 'set-language-environment-hook)
1310 (force-mode-line-update t))
1311
1312 (defun standard-display-european-internal ()
1313 ;; Actually set up direct output of non-ASCII characters.
1314 (standard-display-8bit (if (eq window-system 'pc) 128 160) 255)
1315 ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with
1316 ;; the native font, and codes 160 and 146 stand for something very
1317 ;; different there.
1318 (or (and (eq window-system 'pc) (not default-enable-multibyte-characters))
1319 (progn
1320 ;; Make non-line-break space display as a plain space.
1321 ;; Most X fonts do the wrong thing for code 160.
1322 (aset standard-display-table 160 [32])
1323 ;; Most Windows programs send out apostrophe's as \222. Most X fonts
1324 ;; don't contain a character at that position. Map it to the ASCII
1325 ;; apostrophe.
1326 (aset standard-display-table 146 [39]))))
1327
1328 (defun set-language-environment-coding-systems (language-name
1329 &optional eol-type)
1330 "Do various coding system setups for language environment LANGUAGE-NAME.
1331
1332 The optional arg EOL-TYPE specifies the eol-type of the default value
1333 of buffer-file-coding-system set by this function."
1334 (let* ((priority (get-language-info language-name 'coding-priority))
1335 (default-coding (car priority)))
1336 (if priority
1337 (let ((categories (mapcar 'coding-system-category priority)))
1338 (set-default-coding-systems
1339 (if (memq eol-type '(0 1 2 unix dos mac))
1340 (coding-system-change-eol-conversion default-coding eol-type)
1341 default-coding))
1342 (setq default-sendmail-coding-system default-coding)
1343 (set-coding-priority categories)
1344 (while priority
1345 (set (car categories) (car priority))
1346 (setq priority (cdr priority) categories (cdr categories)))
1347 (update-coding-systems-internal)))))
1348
1349 ;; Print all arguments with `princ', then print "\n".
1350 (defsubst princ-list (&rest args)
1351 (while args (princ (car args)) (setq args (cdr args)))
1352 (princ "\n"))
1353
1354 (put 'describe-specified-language-support 'apropos-inhibit t)
1355
1356 ;; Print a language specific information such as input methods,
1357 ;; charsets, and coding systems. This function is intended to be
1358 ;; called from the menu:
1359 ;; [menu-bar mule describe-language-environment LANGUAGE]
1360 ;; and should not run it by `M-x describe-current-input-method-function'.
1361 (defun describe-specified-language-support ()
1362 "Describe how Emacs supports the specified language environment."
1363 (interactive)
1364 (let (language-name)
1365 (if (not (and (symbolp last-command-event)
1366 (setq language-name (symbol-name last-command-event))))
1367 (error "Bogus calling sequence"))
1368 (describe-language-environment language-name)))
1369
1370 (defun describe-language-environment (language-name)
1371 "Describe how Emacs supports language environment LANGUAGE-NAME."
1372 (interactive
1373 (list (read-language-name
1374 'documentation
1375 "Describe language environment (default, current choice): ")))
1376 (if (null language-name)
1377 (setq language-name current-language-environment))
1378 (if (or (null language-name)
1379 (null (get-language-info language-name 'documentation)))
1380 (error "No documentation for the specified language"))
1381 (if (symbolp language-name)
1382 (setq language-name (symbol-name language-name)))
1383 (let ((doc (get-language-info language-name 'documentation)))
1384 (with-output-to-temp-buffer "*Help*"
1385 (princ-list language-name " language environment" "\n")
1386 (if (stringp doc)
1387 (progn
1388 (princ-list doc)
1389 (terpri)))
1390 (let ((str (get-language-info language-name 'sample-text)))
1391 (if (stringp str)
1392 (progn
1393 (princ "Sample text:\n")
1394 (princ-list " " str)
1395 (terpri))))
1396 (let ((input-method (get-language-info language-name 'input-method))
1397 (l (copy-sequence input-method-alist)))
1398 (princ "Input methods")
1399 (when input-method
1400 (princ (format " (default, %s)" input-method))
1401 (setq input-method (assoc input-method input-method-alist))
1402 (setq l (cons input-method (delete input-method l))))
1403 (princ ":\n")
1404 (while l
1405 (if (string= language-name (nth 1 (car l)))
1406 (princ-list " " (car (car l))
1407 (format " (`%s' in mode line)" (nth 3 (car l)))))
1408 (setq l (cdr l))))
1409 (terpri)
1410 (princ "Character sets:\n")
1411 (let ((l (get-language-info language-name 'charset)))
1412 (if (null l)
1413 (princ-list " nothing specific to " language-name)
1414 (while l
1415 (princ-list " " (car l) ": "
1416 (charset-description (car l)))
1417 (setq l (cdr l)))))
1418 (terpri)
1419 (princ "Coding systems:\n")
1420 (let ((l (get-language-info language-name 'coding-system)))
1421 (if (null l)
1422 (princ-list " nothing specific to " language-name)
1423 (while l
1424 (princ (format " %s (`%c' in mode line):\n\t%s\n"
1425 (car l)
1426 (coding-system-mnemonic (car l))
1427 (coding-system-doc-string (car l))))
1428 (let ((aliases (coding-system-get (car l) 'alias-coding-systems)))
1429 (when aliases
1430 (princ "\t")
1431 (princ (cons 'alias: (cdr aliases)))
1432 (terpri)))
1433 (setq l (cdr l))))))))
1434 \f
1435 ;;; Charset property
1436
1437 (defun get-charset-property (charset propname)
1438 "Return the value of CHARSET's PROPNAME property.
1439 This is the last value stored with
1440 (put-charset-property CHARSET PROPNAME VALUE)."
1441 (and (not (eq charset 'composition))
1442 (plist-get (charset-plist charset) propname)))
1443
1444 (defun put-charset-property (charset propname value)
1445 "Store CHARSETS's PROPNAME property with value VALUE.
1446 It can be retrieved with `(get-charset-property CHARSET PROPNAME)'."
1447 (or (eq charset 'composition)
1448 (set-charset-plist charset
1449 (plist-put (charset-plist charset) propname value))))
1450
1451 ;;; Character code property
1452 (put 'char-code-property-table 'char-table-extra-slots 0)
1453
1454 (defvar char-code-property-table
1455 (make-char-table 'char-code-property-table)
1456 "Char-table containing a property list of each character code.
1457
1458 See also the documentation of `get-char-code-property' and
1459 `put-char-code-property'.")
1460
1461 (defun get-char-code-property (char propname)
1462 "Return the value of CHAR's PROPNAME property in `char-code-property-table'."
1463 (let ((plist (aref char-code-property-table char)))
1464 (if (listp plist)
1465 (car (cdr (memq propname plist))))))
1466
1467 (defun put-char-code-property (char propname value)
1468 "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'.
1469 It can be retrieved with `(get-char-code-property CHAR PROPNAME)'."
1470 (let ((plist (aref char-code-property-table char)))
1471 (if plist
1472 (let ((slot (memq propname plist)))
1473 (if slot
1474 (setcar (cdr slot) value)
1475 (nconc plist (list propname value))))
1476 (aset char-code-property-table char (list propname value)))))
1477
1478 \f
1479 ;; Pretty description of encoded string
1480
1481 ;; Alist of ISO 2022 control code vs the corresponding mnemonic string.
1482 (defvar iso-2022-control-alist
1483 '((?\x1b . "ESC")
1484 (?\x0e . "SO")
1485 (?\x0f . "SI")
1486 (?\x8e . "SS2")
1487 (?\x8f . "SS3")
1488 (?\x9b . "CSI")))
1489
1490 (defun encoded-string-description (str coding-system)
1491 "Return a pretty description of STR that is encoded by CODING-SYSTEM."
1492 (setq str (string-as-unibyte str))
1493 (let ((char (aref str 0))
1494 desc)
1495 (when (< char 128)
1496 (setq desc (or (cdr (assq char iso-2022-control-alist))
1497 (char-to-string char)))
1498 (let ((i 1)
1499 (len (length str)))
1500 (while (< i len)
1501 (setq char (aref str i))
1502 (if (>= char 128)
1503 (setq desc nil i len)
1504 (setq desc (concat desc " "
1505 (or (cdr (assq char iso-2022-control-alist))
1506 (char-to-string char)))
1507 i (1+ i))))))
1508 (or desc
1509 (mapconcat (function (lambda (x) (format "0x%02x" x))) str " "))))
1510
1511 (defun encode-coding-char (char coding-system)
1512 "Encode CHAR by CODING-SYSTEM and return the resulting string.
1513 If CODING-SYSTEM can't safely encode CHAR, return nil."
1514 (if (cmpcharp char)
1515 (setq char (car (decompose-composite-char char 'list))))
1516 (let ((str1 (char-to-string char))
1517 (str2 (make-string 2 char))
1518 (safe-charsets (and coding-system
1519 (coding-system-get coding-system 'safe-charsets)))
1520 enc1 enc2 i1 i2)
1521 (when (or (eq safe-charsets t)
1522 (memq (char-charset char) safe-charsets))
1523 ;; We must find the encoded string of CHAR. But, just encoding
1524 ;; CHAR will put extra control sequences (usually to designate
1525 ;; ASCII charaset) at the tail if type of CODING is ISO 2022.
1526 ;; To exclude such tailing bytes, we at first encode one-char
1527 ;; string and two-char string, then check how many bytes at the
1528 ;; tail of both encoded strings are the same.
1529
1530 (setq enc1 (string-as-unibyte (encode-coding-string str1 coding-system))
1531 i1 (length enc1)
1532 enc2 (string-as-unibyte (encode-coding-string str2 coding-system))
1533 i2 (length enc2))
1534 (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2))))
1535 (setq i1 (1- i1) i2 (1- i2)))
1536
1537 ;; Now (substring enc1 i1) and (substring enc2 i2) are the same,
1538 ;; and they are the extra control sequences at the tail to
1539 ;; exclude.
1540 (substring enc2 0 i2))))
1541
1542
1543 ;;; mule-cmds.el ends here