Commit | Line | Data |
---|---|---|
4ed46869 KH |
1 | ;;; mule-cmds.el --- Commands for mulitilingual environment |
2 | ||
4ed46869 | 3 | ;; Copyright (C) 1995 Electrotechnical Laboratory, JAPAN. |
fa526c4a | 4 | ;; Licensed to the Free Software Foundation. |
4ed46869 KH |
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 | |
369314dc KH |
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. | |
4ed46869 KH |
24 | |
25 | ;;; Code: | |
26 | ||
27 | ;;; MULE related key bindings and menus. | |
28 | ||
0709d285 | 29 | (defvar mule-keymap (make-sparse-keymap) |
33d17698 | 30 | "Keymap for Mule (Multilingual environment) specific commands.") |
4ed46869 | 31 | |
8f81f784 | 32 | ;; Keep "C-x C-m ..." for mule specific commands. |
0709d285 | 33 | (define-key ctl-x-map "\C-m" mule-keymap) |
ef8a8c8c | 34 | |
4ed46869 KH |
35 | (define-key mule-keymap "f" 'set-buffer-file-coding-system) |
36 | (define-key mule-keymap "t" 'set-terminal-coding-system) | |
15b3e511 KH |
37 | (define-key mule-keymap "k" 'set-keyboard-coding-system) |
38 | (define-key mule-keymap "p" 'set-buffer-process-coding-system) | |
7624ebb9 KH |
39 | (define-key mule-keymap "x" 'set-selection-coding-system) |
40 | (define-key mule-keymap "X" 'set-next-selection-coding-system) | |
8b784951 | 41 | (define-key mule-keymap "\C-\\" 'set-input-method) |
15b3e511 | 42 | (define-key mule-keymap "c" 'universal-coding-system-argument) |
b4fba33f | 43 | (define-key mule-keymap "l" 'set-language-environment) |
4ed46869 | 44 | |
281d03ec | 45 | (define-key help-map "\C-L" 'describe-language-environment) |
ac4a3a2d | 46 | (define-key help-map "L" 'describe-language-environment) |
4ed46869 | 47 | (define-key help-map "\C-\\" 'describe-input-method) |
ac4a3a2d | 48 | (define-key help-map "I" 'describe-input-method) |
d0b9c3ab | 49 | (define-key help-map "C" 'describe-coding-system) |
4ed46869 KH |
50 | (define-key help-map "h" 'view-hello-file) |
51 | ||
538d88fb EZ |
52 | (defvar mule-menu-keymap |
53 | (make-sparse-keymap "Mule (Multilingual Environment)") | |
33d17698 | 54 | "Keymap for Mule (Multilingual environment) menu specific commands.") |
15b3e511 | 55 | |
dcad02bc EZ |
56 | (defvar describe-language-environment-map |
57 | (make-sparse-keymap "Describe Language Environment")) | |
15b3e511 | 58 | |
dcad02bc EZ |
59 | (defvar setup-language-environment-map |
60 | (make-sparse-keymap "Set Language Environment")) | |
15b3e511 | 61 | |
dcad02bc EZ |
62 | (defvar set-coding-system-map |
63 | (make-sparse-keymap "Set Coding System")) | |
15b3e511 | 64 | |
15b3e511 | 65 | (define-key-after mule-menu-keymap [set-language-environment] |
538d88fb EZ |
66 | (list 'menu-item "Set Language Environment" setup-language-environment-map |
67 | :help "Multilingual environment suitable for specific language") | |
15b3e511 | 68 | t) |
a61f401d | 69 | (define-key-after mule-menu-keymap [mouse-set-font] |
538d88fb EZ |
70 | '(menu-item "Set Font/Fontset" mouse-set-font |
71 | :visible (fboundp 'generate-fontset-menu) | |
72 | :help "Select a font from list of known fonts/fontsets") | |
a61f401d | 73 | t) |
15b3e511 KH |
74 | (define-key-after mule-menu-keymap [separator-mule] |
75 | '("--") | |
76 | t) | |
77 | (define-key-after mule-menu-keymap [toggle-input-method] | |
538d88fb | 78 | '(menu-item "Toggle Input Method" toggle-input-method) |
15b3e511 | 79 | t) |
8b784951 | 80 | (define-key-after mule-menu-keymap [set-input-method] |
538d88fb | 81 | '(menu-item "Select Input Method..." set-input-method) |
15b3e511 KH |
82 | t) |
83 | (define-key-after mule-menu-keymap [separator-input-method] | |
84 | '("--") | |
85 | t) | |
15b3e511 | 86 | (define-key-after mule-menu-keymap [set-various-coding-system] |
538d88fb EZ |
87 | (list 'menu-item "Set Coding Systems" set-coding-system-map) |
88 | t) | |
89 | (define-key-after mule-menu-keymap [view-hello-file] | |
90 | '(menu-item "Show Multi-lingual Text" view-hello-file | |
91 | :enable (file-readable-p | |
92 | (expand-file-name "HELLO" data-directory)) | |
93 | :help "Display file which says HELLO in many languages") | |
15b3e511 KH |
94 | t) |
95 | (define-key-after mule-menu-keymap [separator-coding-system] | |
96 | '("--") | |
97 | t) | |
538d88fb EZ |
98 | (define-key-after mule-menu-keymap [describe-language-environment] |
99 | (list 'menu-item "Describe Language Environment" | |
100 | describe-language-environment-map | |
101 | :help "Show multilingual settings for specific language") | |
15b3e511 | 102 | t) |
538d88fb EZ |
103 | (define-key-after mule-menu-keymap [describe-input-method] |
104 | '(menu-item "Describe Input Method..." describe-input-method | |
105 | :help "Keyboard layout for specific input method") | |
106 | t) | |
107 | (define-key-after mule-menu-keymap [describe-coding-system] | |
108 | '(menu-item "Describe Coding System..." describe-coding-system) | |
109 | t) | |
110 | (define-key-after mule-menu-keymap [mule-diag] | |
111 | '(menu-item "Show All of Mule Status" mule-diag | |
112 | :help "Display multilingual environment settings") | |
15b3e511 KH |
113 | t) |
114 | ||
115 | (define-key-after set-coding-system-map [set-buffer-file-coding-system] | |
538d88fb EZ |
116 | '(menu-item "For Saving this Buffer" set-buffer-file-coding-system |
117 | :help "How to encode this buffer on disk") | |
15b3e511 | 118 | t) |
3a151e98 | 119 | (define-key-after set-coding-system-map [universal-coding-system-argument] |
538d88fb EZ |
120 | '(menu-item "For Next Command" universal-coding-system-argument |
121 | :help "Coding system to be used by next command") | |
3a151e98 | 122 | t) |
15b3e511 | 123 | (define-key-after set-coding-system-map [set-terminal-coding-system] |
538d88fb EZ |
124 | '(menu-item "For Terminal" set-terminal-coding-system |
125 | :enable (null (memq window-system '(x w32 mac))) | |
126 | :help "How to encode terminal output") | |
15b3e511 KH |
127 | t) |
128 | (define-key-after set-coding-system-map [set-keyboard-coding-system] | |
538d88fb EZ |
129 | '(menu-item "For Keyboard" set-keyboard-coding-system |
130 | :help "How to decode keyboard input") | |
15b3e511 KH |
131 | t) |
132 | (define-key-after set-coding-system-map [set-buffer-process-coding-system] | |
538d88fb EZ |
133 | '(menu-item "For I/O with Subprocess" set-buffer-process-coding-system |
134 | :visible (fboundp 'start-process) | |
135 | :enable (get-buffer-process (current-buffer)) | |
136 | :help "How to en/decode I/O from/to subprocess connected to this buffer") | |
15b3e511 | 137 | t) |
7624ebb9 | 138 | (define-key-after set-coding-system-map [set-selection-coding-system] |
538d88fb EZ |
139 | '(menu-item "For X Selections/Clipboard" set-selection-coding-system |
140 | :visible (display-selections-p) | |
141 | :help "How to en/decode data to/from selection/clipboard") | |
7624ebb9 KH |
142 | t) |
143 | (define-key-after set-coding-system-map [set-next-selection-coding-system] | |
538d88fb EZ |
144 | '(menu-item "For Next X Selection" set-next-selection-coding-system |
145 | :visible (display-selections-p) | |
146 | :help "How to en/decode next selection/clipboard operation") | |
7624ebb9 | 147 | t) |
15b3e511 | 148 | (define-key setup-language-environment-map |
538d88fb | 149 | [Default] '(menu-item "Default" setup-specified-language-environment)) |
4ed46869 | 150 | |
4ed46869 KH |
151 | ;; This should be a single character key binding because users use it |
152 | ;; very frequently while editing multilingual text. Now we can use | |
153 | ;; only two such keys: "\C-\\" and "\C-^", but the latter is not | |
154 | ;; convenient because it requires shifting on most keyboards. An | |
155 | ;; alternative is "\C-\]" which is now bound to `abort-recursive-edit' | |
156 | ;; but it won't be used that frequently. | |
157 | (define-key global-map "\C-\\" 'toggle-input-method) | |
158 | ||
a2ad45b9 RS |
159 | ;;; This is no good because people often type Shift-SPC |
160 | ;;; meaning to type SPC. -- rms. | |
161 | ;;; ;; Here's an alternative key binding for X users (Shift-SPACE). | |
162 | ;;; (define-key global-map [?\S- ] 'toggle-input-method) | |
b4fba33f | 163 | |
26d87040 EZ |
164 | (defun coding-system-change-eol-conversion (coding-system eol-type) |
165 | "Return a coding system which differs from CODING-SYSTEM in eol conversion. | |
166 | The returned coding system converts end-of-line by EOL-TYPE | |
167 | but text as the same way as CODING-SYSTEM. | |
168 | EOL-TYPE should be `unix', `dos', `mac', or nil. | |
169 | If EOL-TYPE is nil, the returned coding system detects | |
170 | how end-of-line is formatted automatically while decoding. | |
171 | ||
172 | EOL-TYPE can be specified by an integer 0, 1, or 2. | |
173 | They means `unix', `dos', and `mac' respectively." | |
174 | (if (symbolp eol-type) | |
175 | (setq eol-type (cond ((eq eol-type 'unix) 0) | |
176 | ((eq eol-type 'dos) 1) | |
177 | ((eq eol-type 'mac) 2) | |
178 | (t eol-type)))) | |
179 | (let ((orig-eol-type (coding-system-eol-type coding-system))) | |
180 | (if (vectorp orig-eol-type) | |
181 | (if (not eol-type) | |
182 | coding-system | |
183 | (aref orig-eol-type eol-type)) | |
184 | (let ((base (coding-system-base coding-system))) | |
185 | (if (not eol-type) | |
186 | base | |
187 | (if (= eol-type orig-eol-type) | |
188 | coding-system | |
189 | (setq orig-eol-type (coding-system-eol-type base)) | |
190 | (if (vectorp orig-eol-type) | |
191 | (aref orig-eol-type eol-type)))))))) | |
192 | ||
193 | (defun coding-system-change-text-conversion (coding-system coding) | |
194 | "Return a coding system which differs from CODING-SYSTEM in text conversion. | |
195 | The returned coding system converts text by CODING | |
196 | but end-of-line as the same way as CODING-SYSTEM. | |
197 | If CODING is nil, the returned coding system detects | |
198 | how text is formatted automatically while decoding." | |
199 | (if (not coding) | |
200 | (coding-system-base coding-system) | |
201 | (let ((eol-type (coding-system-eol-type coding-system))) | |
202 | (coding-system-change-eol-conversion | |
203 | coding | |
204 | (if (numberp eol-type) (aref [unix dos mac] eol-type)))))) | |
205 | ||
4ed46869 | 206 | (defun toggle-enable-multibyte-characters (&optional arg) |
6998e1a1 RS |
207 | "Change whether this buffer uses multibyte characters. |
208 | With arg, use multibyte characters if the arg is positive. | |
209 | ||
210 | Note that this command does not convert the byte contents of | |
211 | the buffer; it only changes the way those bytes are interpreted. | |
212 | In general, therefore, this command *changes* the sequence of | |
213 | characters that the current buffer contains. | |
214 | ||
215 | We suggest you avoid using use this command unless you know what you | |
216 | are doing. If you use it by mistake, and the buffer is now displayed | |
217 | wrong, use this command again to toggle back to the right mode." | |
4ed46869 | 218 | (interactive "P") |
b7079457 RS |
219 | (let ((new-flag |
220 | (if (null arg) (null enable-multibyte-characters) | |
221 | (> (prefix-numeric-value arg) 0)))) | |
222 | (set-buffer-multibyte new-flag)) | |
4ed46869 KH |
223 | (force-mode-line-update)) |
224 | ||
225 | (defun view-hello-file () | |
226 | "Display the HELLO file which list up many languages and characters." | |
227 | (interactive) | |
8f81f784 KH |
228 | ;; We have to decode the file in any environment. |
229 | (let ((default-enable-multibyte-characters t) | |
95fa03b4 | 230 | (coding-system-for-read 'iso-2022-7bit)) |
8f81f784 | 231 | (find-file-read-only (expand-file-name "HELLO" data-directory)))) |
4ed46869 | 232 | |
15b3e511 KH |
233 | (defun universal-coding-system-argument () |
234 | "Execute an I/O command using the specified coding system." | |
235 | (interactive) | |
34104362 KH |
236 | (let* ((default (and buffer-file-coding-system |
237 | (not (eq (coding-system-type buffer-file-coding-system) | |
238 | t)) | |
239 | buffer-file-coding-system)) | |
240 | (coding-system (read-coding-system | |
241 | (if default | |
242 | (format "Coding system for following command (default, %s): " default) | |
243 | "Coding system for following command: ") | |
244 | default)) | |
15b3e511 | 245 | (keyseq (read-key-sequence |
e14a8f4c | 246 | (format "Command to execute with %s:" coding-system))) |
15b3e511 KH |
247 | (cmd (key-binding keyseq))) |
248 | (let ((coding-system-for-read coding-system) | |
249 | (coding-system-for-write coding-system)) | |
250 | (message "") | |
251 | (call-interactively cmd)))) | |
252 | ||
de94d711 | 253 | (defun set-default-coding-systems (coding-system) |
0c3154d2 | 254 | "Set default value of various coding systems to CODING-SYSTEM. |
387136f6 | 255 | This sets the following coding systems: |
0c3154d2 | 256 | o coding system of a newly created buffer |
8efc03e1 KH |
257 | o default coding system for subprocess I/O |
258 | This also sets the following values: | |
387136f6 | 259 | o default value used as file-name-coding-system for converting file names. |
03c35c83 EZ |
260 | o default value for the command `set-terminal-coding-system' (not on MSDOS) |
261 | o default value for the command `set-keyboard-coding-system'." | |
de94d711 KH |
262 | (check-coding-system coding-system) |
263 | (setq-default buffer-file-coding-system coding-system) | |
716184d4 RS |
264 | (if default-enable-multibyte-characters |
265 | (setq default-file-name-coding-system coding-system)) | |
03c35c83 EZ |
266 | ;; If coding-system is nil, honor that on MS-DOS as well, so |
267 | ;; that they could reset the terminal coding system. | |
268 | (unless (and (eq window-system 'pc) coding-system) | |
269 | (setq default-terminal-coding-system coding-system)) | |
de94d711 KH |
270 | (setq default-keyboard-coding-system coding-system) |
271 | (setq default-process-coding-system (cons coding-system coding-system))) | |
272 | ||
45d08cb2 KH |
273 | (defalias 'update-iso-coding-systems 'update-coding-systems-internal) |
274 | (make-obsolete 'update-iso-coding-systems 'update-coding-systems-internal) | |
275 | ||
0c3154d2 KH |
276 | (defun prefer-coding-system (coding-system) |
277 | "Add CODING-SYSTEM at the front of the priority list for automatic detection. | |
387136f6 | 278 | This also sets the following coding systems: |
0c3154d2 | 279 | o coding system of a newly created buffer |
8efc03e1 KH |
280 | o default coding system for subprocess I/O |
281 | This also sets the following values: | |
387136f6 | 282 | o default value used as file-name-coding-system for converting file names. |
03c35c83 EZ |
283 | o default value for the command `set-terminal-coding-system' (not on MSDOS) |
284 | o default value for the command `set-keyboard-coding-system' | |
285 | ||
bd3ac67e EZ |
286 | If CODING-SYSTEM specifies a certain type of EOL conversion, the coding |
287 | systems set by this function will use that type of EOL conversion. | |
288 | ||
03c35c83 EZ |
289 | This command does not change the default value of terminal coding system |
290 | for MS-DOS terminal, because DOS terminals only support a single coding | |
291 | system, and Emacs automatically sets the default to that coding system at | |
292 | startup." | |
0c3154d2 KH |
293 | (interactive "zPrefer coding system: ") |
294 | (if (not (and coding-system (coding-system-p coding-system))) | |
295 | (error "Invalid coding system `%s'" coding-system)) | |
296 | (let ((coding-category (coding-system-category coding-system)) | |
bd3ac67e EZ |
297 | (base (coding-system-base coding-system)) |
298 | (eol-type (coding-system-eol-type coding-system))) | |
0c3154d2 KH |
299 | (if (not coding-category) |
300 | ;; CODING-SYSTEM is no-conversion or undecided. | |
301 | (error "Can't prefer the coding system `%s'" coding-system)) | |
8efc03e1 | 302 | (set coding-category (or base coding-system)) |
45d08cb2 | 303 | (update-coding-systems-internal) |
812cad80 | 304 | (or (eq coding-category (car coding-category-list)) |
0c3154d2 | 305 | ;; We must change the order. |
812cad80 | 306 | (set-coding-priority (list coding-category))) |
8efc03e1 KH |
307 | (if (and base (interactive-p)) |
308 | (message "Highest priority is set to %s (base of %s)" | |
309 | base coding-system)) | |
bd3ac67e | 310 | ;; If they asked for specific EOL conversion, honor that. |
6f9dc4fd | 311 | (if (memq eol-type '(0 1 2)) |
bd3ac67e EZ |
312 | (setq coding-system |
313 | (coding-system-change-eol-conversion base eol-type)) | |
314 | (setq coding-system base)) | |
315 | (set-default-coding-systems coding-system))) | |
0c3154d2 | 316 | |
2c395d56 | 317 | (defun find-coding-systems-region-subset-p (list1 list2) |
54b226f7 KH |
318 | "Return non-nil if all elements in LIST1 are included in LIST2. |
319 | Comparison done with EQ." | |
320 | (catch 'tag | |
321 | (while list1 | |
322 | (or (memq (car list1) list2) | |
323 | (throw 'tag nil)) | |
324 | (setq list1 (cdr list1))) | |
325 | t)) | |
326 | ||
3fc7dfe5 | 327 | (defun find-coding-systems-region (from to) |
54b226f7 KH |
328 | "Return a list of proper coding systems to encode a text between FROM and TO. |
329 | All coding systems in the list can safely encode any multibyte characters | |
330 | in the text. | |
331 | ||
e8dd0160 | 332 | If the text contains no multibyte characters, return a list of a single |
3fc7dfe5 KH |
333 | element `undecided'." |
334 | (find-coding-systems-for-charsets (find-charset-region from to))) | |
54b226f7 | 335 | |
3fc7dfe5 KH |
336 | (defun find-coding-systems-string (string) |
337 | "Return a list of proper coding systems to encode STRING. | |
338 | All coding systems in the list can safely encode any multibyte characters | |
339 | in STRING. | |
340 | ||
e8dd0160 | 341 | If STRING contains no multibyte characters, return a list of a single |
3fc7dfe5 KH |
342 | element `undecided'." |
343 | (find-coding-systems-for-charsets (find-charset-string string))) | |
344 | ||
345 | (defun find-coding-systems-for-charsets (charsets) | |
346 | "Return a list of proper coding systems to encode characters of CHARSETS. | |
347 | CHARSETS is a list of character sets." | |
348 | (if (or (null charsets) | |
349 | (and (= (length charsets) 1) | |
350 | (eq 'ascii (car charsets)))) | |
351 | '(undecided) | |
2a17b638 | 352 | (setq charsets (delq 'composition charsets)) |
251d4f4b | 353 | (let ((l (coding-system-list 'base-only)) |
0e0a9ac3 | 354 | (charset-preferred-codings |
3fc7dfe5 KH |
355 | (mapcar (function |
356 | (lambda (x) | |
251d4f4b KH |
357 | (if (eq x 'unknown) |
358 | 'raw-text | |
0e0a9ac3 | 359 | (get-charset-property x 'preferred-coding-system)))) |
3fc7dfe5 | 360 | charsets)) |
34104362 KH |
361 | (priorities (mapcar (function (lambda (x) (symbol-value x))) |
362 | coding-category-list)) | |
3fc7dfe5 | 363 | codings coding safe) |
251d4f4b KH |
364 | (if (memq 'unknown charsets) |
365 | ;; The region contains invalid multibyte characters. | |
366 | (setq l '(raw-text))) | |
3fc7dfe5 KH |
367 | (while l |
368 | (setq coding (car l) l (cdr l)) | |
251d4f4b | 369 | (if (and (setq safe (coding-system-get coding 'safe-charsets)) |
3fc7dfe5 | 370 | (or (eq safe t) |
2c395d56 | 371 | (find-coding-systems-region-subset-p charsets safe))) |
3fc7dfe5 | 372 | ;; We put the higher priority to coding systems included |
0e0a9ac3 | 373 | ;; in CHARSET-PREFERRED-CODINGS, and within them, put the |
34104362 | 374 | ;; higher priority to coding systems which support smaller |
3fc7dfe5 KH |
375 | ;; number of charsets. |
376 | (let ((priority | |
34104362 KH |
377 | (+ (if (coding-system-get coding 'mime-charset) 4096 0) |
378 | (lsh (length (memq coding priorities)) 7) | |
0e0a9ac3 | 379 | (if (memq coding charset-preferred-codings) 64 0) |
34104362 KH |
380 | (if (> (coding-system-type coding) 0) 32 0) |
381 | (if (consp safe) (- 32 (length safe)) 0)))) | |
3fc7dfe5 KH |
382 | (setq codings (cons (cons priority coding) codings))))) |
383 | (mapcar 'cdr | |
384 | (sort codings (function (lambda (x y) (> (car x) (car y)))))) | |
385 | ))) | |
54b226f7 | 386 | |
51ed58ea KH |
387 | (defun find-multibyte-characters (from to &optional maxcount excludes) |
388 | "Find multibyte characters in the region specified by FROM and TO. | |
389 | If FROM is a string, find multibyte characters in the string. | |
390 | The return value is an alist of the following format: | |
391 | ((CHARSET COUNT CHAR ...) ...) | |
392 | where | |
393 | CHARSET is a character set, | |
394 | COUNT is a number of characters, | |
395 | CHARs are found characters of the character set. | |
396 | Optional 3rd arg MAXCOUNT limits how many CHARs are put in the above list. | |
251d4f4b KH |
397 | Optional 4th arg EXCLUDE is a list of character sets to be ignored. |
398 | ||
399 | For invalid characters, CHARs are actually strings." | |
51ed58ea KH |
400 | (let ((chars nil) |
401 | charset char) | |
402 | (if (stringp from) | |
403 | (let ((idx 0)) | |
404 | (while (setq idx (string-match "[^\000-\177]" from idx)) | |
405 | (setq char (aref from idx) | |
406 | charset (char-charset char)) | |
251d4f4b KH |
407 | (if (eq charset 'unknown) |
408 | (setq char (match-string 0))) | |
409 | (if (or (eq charset 'unknown) | |
410 | (not (or (eq excludes t) (memq charset excludes)))) | |
51ed58ea KH |
411 | (let ((slot (assq charset chars))) |
412 | (if slot | |
413 | (if (not (memq char (nthcdr 2 slot))) | |
414 | (let ((count (nth 1 slot))) | |
415 | (setcar (cdr slot) (1+ count)) | |
416 | (if (or (not maxcount) (< count maxcount)) | |
417 | (nconc slot (list char))))) | |
418 | (setq chars (cons (list charset 1 char) chars))))) | |
419 | (setq idx (1+ idx)))) | |
420 | (save-excursion | |
421 | (goto-char from) | |
422 | (while (re-search-forward "[^\000-\177]" to t) | |
423 | (setq char (preceding-char) | |
424 | charset (char-charset char)) | |
251d4f4b KH |
425 | (if (eq charset 'unknown) |
426 | (setq char (match-string 0))) | |
427 | (if (or (eq charset 'unknown) | |
428 | (not (or (eq excludes t) (memq charset excludes)))) | |
51ed58ea KH |
429 | (let ((slot (assq charset chars))) |
430 | (if slot | |
251d4f4b | 431 | (if (not (member char (nthcdr 2 slot))) |
51ed58ea KH |
432 | (let ((count (nth 1 slot))) |
433 | (setcar (cdr slot) (1+ count)) | |
434 | (if (or (not maxcount) (< count maxcount)) | |
435 | (nconc slot (list char))))) | |
436 | (setq chars (cons (list charset 1 char) chars)))))))) | |
437 | (nreverse chars))) | |
438 | ||
c83c4f60 RS |
439 | (defvar last-coding-system-specified nil |
440 | "Most recent coding system explicitly specified by the user when asked. | |
441 | This variable is set whenever Emacs asks the user which coding system | |
442 | to use in order to write a file. If you set it to nil explicitly, | |
443 | then call `write-region', then afterward this variable will be non-nil | |
444 | only if the user was explicitly asked and specified a coding system.") | |
445 | ||
54b226f7 | 446 | (defun select-safe-coding-system (from to &optional default-coding-system) |
d5266ddf KH |
447 | "Ask a user to select a safe coding system from candidates. |
448 | The candidates of coding systems which can safely encode a text | |
449 | between FROM and TO are shown in a popup window. | |
54b226f7 KH |
450 | |
451 | Optional arg DEFAULT-CODING-SYSTEM specifies a coding system to be | |
452 | checked at first. If omitted, buffer-file-coding-system of the | |
453 | current buffer is used. | |
454 | ||
d5266ddf KH |
455 | If the text can be encoded safely by DEFAULT-CODING-SYSTEM, it is |
456 | returned without any user interaction. | |
54b226f7 KH |
457 | |
458 | Kludgy feature: if FROM is a string, the string is the target text, | |
459 | and TO is ignored." | |
460 | (or default-coding-system | |
461 | (setq default-coding-system buffer-file-coding-system)) | |
51ed58ea KH |
462 | (let* ((charsets (if (stringp from) (find-charset-string from) |
463 | (find-charset-region from to))) | |
464 | (safe-coding-systems (find-coding-systems-for-charsets charsets))) | |
251d4f4b KH |
465 | (if (or (not enable-multibyte-characters) |
466 | (eq (car safe-coding-systems) 'undecided) | |
902e3f77 | 467 | (eq default-coding-system 'no-conversion) |
54b226f7 KH |
468 | (and default-coding-system |
469 | (memq (coding-system-base default-coding-system) | |
470 | safe-coding-systems))) | |
471 | default-coding-system | |
472 | ||
34104362 KH |
473 | ;; At first, change each coding system to the corresponding |
474 | ;; mime-charset name if it is also a coding system. | |
475 | (let ((l safe-coding-systems) | |
476 | mime-charset) | |
477 | (while l | |
478 | (setq mime-charset (coding-system-get (car l) 'mime-charset)) | |
479 | (if (and mime-charset (coding-system-p mime-charset)) | |
480 | (setcar l mime-charset)) | |
481 | (setq l (cdr l)))) | |
482 | ||
51ed58ea KH |
483 | (let ((non-safe-chars (find-multibyte-characters |
484 | from to 3 | |
485 | (and default-coding-system | |
486 | (coding-system-get default-coding-system | |
487 | 'safe-charsets)))) | |
fcdc4d67 | 488 | show-position overlays) |
51ed58ea KH |
489 | (save-excursion |
490 | ;; Highlight characters that default-coding-system can't encode. | |
491 | (when (integerp from) | |
492 | (goto-char from) | |
493 | (let ((found nil)) | |
494 | (while (and (not found) | |
495 | (re-search-forward "[^\000-\177]" to t)) | |
496 | (setq found (assq (char-charset (preceding-char)) | |
497 | non-safe-chars)))) | |
fcdc4d67 KH |
498 | (forward-line -1) |
499 | (setq show-position (point)) | |
51ed58ea | 500 | (save-excursion |
265519e3 RS |
501 | (while (and (< (length overlays) 256) |
502 | (re-search-forward "[^\000-\177]" to t)) | |
51ed58ea KH |
503 | (let* ((char (preceding-char)) |
504 | (charset (char-charset char))) | |
505 | (when (assq charset non-safe-chars) | |
506 | (setq overlays (cons (make-overlay (1- (point)) (point)) | |
507 | overlays)) | |
508 | (overlay-put (car overlays) 'face 'highlight)))))) | |
509 | ||
510 | ;; At last, ask a user to select a proper coding system. | |
511 | (unwind-protect | |
512 | (save-window-excursion | |
fcdc4d67 KH |
513 | (when show-position |
514 | ;; At first, be sure to show the current buffer. | |
515 | (set-window-buffer (selected-window) (current-buffer)) | |
516 | (set-window-start (selected-window) show-position)) | |
517 | ;; Then, show a helpful message. | |
51ed58ea KH |
518 | (with-output-to-temp-buffer "*Warning*" |
519 | (save-excursion | |
520 | (set-buffer standard-output) | |
521 | (insert "The target text contains the following non ASCII character(s):\n") | |
522 | (let ((len (length non-safe-chars)) | |
523 | (shown 0)) | |
524 | (while (and non-safe-chars (< shown 3)) | |
525 | (when (> (length (car non-safe-chars)) 2) | |
526 | (setq shown (1+ shown)) | |
527 | (insert (format "%25s: " (car (car non-safe-chars)))) | |
528 | (let ((l (nthcdr 2 (car non-safe-chars)))) | |
529 | (while l | |
251d4f4b KH |
530 | (if (or (stringp (car l)) (char-valid-p (car l))) |
531 | (insert (car l))) | |
51ed58ea KH |
532 | (setq l (cdr l)))) |
533 | (if (> (nth 1 (car non-safe-chars)) 3) | |
534 | (insert "...")) | |
535 | (insert "\n")) | |
536 | (setq non-safe-chars (cdr non-safe-chars))) | |
537 | (if (< shown len) | |
538 | (insert (format "%27s\n" "...")))) | |
2e224638 SM |
539 | (insert (format |
540 | "These can't be encoded safely by the coding system %s. | |
54b226f7 KH |
541 | |
542 | Please select one from the following safe coding systems:\n" | |
51ed58ea KH |
543 | default-coding-system)) |
544 | (let ((pos (point)) | |
545 | (fill-prefix " ")) | |
546 | (mapcar (function (lambda (x) (princ " ") (princ x))) | |
547 | safe-coding-systems) | |
548 | (fill-region-as-paragraph pos (point))))) | |
549 | ||
550 | ;; Read a coding system. | |
551 | (let* ((safe-names (mapcar (lambda (x) (list (symbol-name x))) | |
552 | safe-coding-systems)) | |
553 | (name (completing-read | |
554 | (format "Select coding system (default %s): " | |
555 | (car safe-coding-systems)) | |
556 | safe-names nil t nil nil | |
557 | (car (car safe-names))))) | |
558 | (setq last-coding-system-specified (intern name)) | |
559 | (if (integerp (coding-system-eol-type default-coding-system)) | |
560 | (setq last-coding-system-specified | |
561 | (coding-system-change-eol-conversion | |
562 | last-coding-system-specified | |
563 | (coding-system-eol-type default-coding-system)))) | |
564 | last-coding-system-specified)) | |
565 | (kill-buffer "*Warning*") | |
566 | (while overlays | |
567 | (delete-overlay (car overlays)) | |
568 | (setq overlays (cdr overlays))))))))) | |
54b226f7 KH |
569 | |
570 | (setq select-safe-coding-system-function 'select-safe-coding-system) | |
571 | ||
46babb23 KH |
572 | (defun select-message-coding-system () |
573 | "Return a coding system to encode the outgoing message of the current buffer. | |
574 | It at first tries the first coding system found in these variables | |
575 | in this order: | |
576 | (1) local value of `buffer-file-coding-system' | |
577 | (2) value of `sendmail-coding-system' | |
578 | (3) value of `default-buffer-file-coding-system' | |
579 | (4) value of `default-sendmail-coding-system' | |
580 | If the found coding system can't encode the current buffer, | |
581 | or none of them are bound to a coding system, | |
48e41165 | 582 | it asks the user to select a proper coding system." |
46babb23 KH |
583 | (let ((coding (or (and (local-variable-p 'buffer-file-coding-system) |
584 | buffer-file-coding-system) | |
585 | sendmail-coding-system | |
586 | default-buffer-file-coding-system | |
587 | default-sendmail-coding-system))) | |
588 | (if (eq coding 'no-conversion) | |
589 | ;; We should never use no-conversion for outgoing mails. | |
590 | (setq coding nil)) | |
591 | (if (fboundp select-safe-coding-system-function) | |
592 | (funcall select-safe-coding-system-function | |
593 | (point-min) (point-max) coding) | |
594 | coding))) | |
4ed46869 | 595 | \f |
03c35c83 | 596 | ;;; Language support stuff. |
4ed46869 | 597 | |
4ed46869 | 598 | (defvar language-info-alist nil |
2c395d56 | 599 | "Alist of language environment definitions. |
4ed46869 KH |
600 | Each element looks like: |
601 | (LANGUAGE-NAME . ((KEY . INFO) ...)) | |
2c395d56 RS |
602 | where LANGUAGE-NAME is a string, the name of the language environment, |
603 | KEY is a symbol denoting the kind of information, and | |
604 | INFO is the data associated with KEY. | |
605 | Meaningful values for KEY include | |
606 | ||
607 | documentation value is documentation of what this language environment | |
608 | is meant for, and how to use it. | |
609 | charset value is a list of the character sets used by this | |
610 | language environment. | |
611 | sample-text value is one line of text, | |
612 | written using those character sets, | |
613 | appropriate for this language environment. | |
614 | setup-function value is a function to call to switch to this | |
615 | language environment. | |
616 | exit-function value is a function to call to leave this | |
617 | language environment. | |
618 | coding-system value is a list of coding systems that are good | |
619 | for saving text written in this language environment. | |
620 | This list serves as suggestions to the user; | |
621 | in effect, as a kind of documentation. | |
622 | coding-priority value is a list of coding systems for this language | |
623 | environment, in order of decreasing priority. | |
624 | This is used to set up the coding system priority | |
45d08cb2 | 625 | list when you switch to this language environment. |
ddb5c041 | 626 | nonascii-translation |
7624ebb9 | 627 | value is a translation table to be set in the |
45d08cb2 | 628 | variable `nonascii-translation-table' in this |
7624ebb9 KH |
629 | language environment, or a character set from |
630 | which `nonascii-insert-offset' is calculated. | |
ddb5c041 KH |
631 | input-method value is a default input method for this language |
632 | environment. | |
7624ebb9 KH |
633 | features value is a list of features requested in this |
634 | language environment. | |
ddb5c041 KH |
635 | |
636 | The following keys take effect only when multibyte characters are | |
637 | globally disabled, i.e. the value of `default-enable-multibyte-characters' | |
638 | is nil. | |
639 | ||
640 | unibyte-syntax value is a library name to load to set | |
e8dd0160 | 641 | unibyte 8-bit character syntaxes for this |
ddb5c041 KH |
642 | language environment. |
643 | ||
644 | unibyte-display value is a coding system to encode characters | |
645 | for the terminal. Characters in the range | |
646 | of 160 to 255 display not as octal escapes, | |
647 | but as non-ASCII characters in this language | |
648 | environment.") | |
2c395d56 RS |
649 | |
650 | (defun get-language-info (lang-env key) | |
651 | "Return information listed under KEY for language environment LANG-ENV. | |
652 | KEY is a symbol denoting the kind of information. | |
653 | For a list of useful values for KEY and their meanings, | |
654 | see `language-info-alist'." | |
655 | (if (symbolp lang-env) | |
656 | (setq lang-env (symbol-name lang-env))) | |
657 | (let ((lang-slot (assoc-ignore-case lang-env language-info-alist))) | |
4ed46869 KH |
658 | (if lang-slot |
659 | (cdr (assq key (cdr lang-slot)))))) | |
660 | ||
f08adf27 | 661 | (defun set-language-info (lang-env key info) |
2c395d56 RS |
662 | "Modify part of the definition of language environment LANG-ENV. |
663 | Specifically, this stores the information INFO under KEY | |
664 | in the definition of this language environment. | |
4ed46869 | 665 | KEY is a symbol denoting the kind of information. |
2c395d56 | 666 | INFO is the value for that information. |
281d03ec | 667 | |
2c395d56 | 668 | For a list of useful values for KEY and their meanings, |
f08adf27 | 669 | see `language-info-alist'." |
2c395d56 RS |
670 | (if (symbolp lang-env) |
671 | (setq lang-env (symbol-name lang-env))) | |
4ed46869 | 672 | (let (lang-slot key-slot) |
2c395d56 | 673 | (setq lang-slot (assoc lang-env language-info-alist)) |
4ed46869 | 674 | (if (null lang-slot) ; If no slot for the language, add it. |
2c395d56 | 675 | (setq lang-slot (list lang-env) |
4ed46869 KH |
676 | language-info-alist (cons lang-slot language-info-alist))) |
677 | (setq key-slot (assq key lang-slot)) | |
678 | (if (null key-slot) ; If no slot for the key, add it. | |
679 | (progn | |
680 | (setq key-slot (list key)) | |
681 | (setcdr lang-slot (cons key-slot (cdr lang-slot))))) | |
f08adf27 | 682 | (setcdr key-slot info))) |
4ed46869 | 683 | |
2c395d56 RS |
684 | (defun set-language-info-alist (lang-env alist &optional parents) |
685 | "Store ALIST as the definition of language environment LANG-ENV. | |
686 | ALIST is an alist of KEY and INFO values. See the documentation of | |
7624ebb9 | 687 | `set-language-info' for the meanings of KEY and INFO. |
54b226f7 | 688 | |
2c395d56 RS |
689 | Optional arg PARENTS is a list of parent menu names; it specifies |
690 | where to put this language environment in the | |
691 | Describe Language Environment and Set Language Environment menus. | |
692 | For example, (\"European\") means to put this language environment | |
693 | in the European submenu in each of those two menus." | |
694 | (if (symbolp lang-env) | |
695 | (setq lang-env (symbol-name lang-env))) | |
54b226f7 KH |
696 | (let ((describe-map describe-language-environment-map) |
697 | (setup-map setup-language-environment-map)) | |
698 | (if parents | |
699 | (let ((l parents) | |
700 | map parent-symbol parent) | |
701 | (while l | |
702 | (if (symbolp (setq parent-symbol (car l))) | |
703 | (setq parent (symbol-name parent)) | |
704 | (setq parent parent-symbol parent-symbol (intern parent))) | |
705 | (setq map (lookup-key describe-map (vector parent-symbol))) | |
706 | (if (not map) | |
707 | (progn | |
708 | (setq map (intern (format "describe-%s-environment-map" | |
709 | (downcase parent)))) | |
710 | (define-prefix-command map) | |
711 | (define-key-after describe-map (vector parent-symbol) | |
712 | (cons parent map) t))) | |
713 | (setq describe-map (symbol-value map)) | |
714 | (setq map (lookup-key setup-map (vector parent-symbol))) | |
715 | (if (not map) | |
716 | (progn | |
717 | (setq map (intern (format "setup-%s-environment-map" | |
718 | (downcase parent)))) | |
719 | (define-prefix-command map) | |
720 | (define-key-after setup-map (vector parent-symbol) | |
721 | (cons parent map) t))) | |
722 | (setq setup-map (symbol-value map)) | |
723 | (setq l (cdr l))))) | |
f08adf27 RS |
724 | |
725 | ;; Set up menu items for this language env. | |
7624ebb9 | 726 | (let ((doc (assq 'documentation alist))) |
f08adf27 RS |
727 | (when doc |
728 | (define-key-after describe-map (vector (intern lang-env)) | |
7624ebb9 KH |
729 | (cons lang-env 'describe-specified-language-support) t))) |
730 | (define-key-after setup-map (vector (intern lang-env)) | |
731 | (cons lang-env 'setup-specified-language-environment) t) | |
f08adf27 | 732 | |
54b226f7 | 733 | (while alist |
f08adf27 | 734 | (set-language-info lang-env (car (car alist)) (cdr (car alist))) |
54b226f7 | 735 | (setq alist (cdr alist))))) |
4ed46869 | 736 | |
ae302641 | 737 | (defun read-language-name (key prompt &optional default) |
2c395d56 | 738 | "Read a language environment name which has information for KEY. |
ddb5c041 | 739 | If KEY is nil, read any language environment. |
2c395d56 RS |
740 | Prompt with PROMPT. DEFAULT is the default choice of language environment. |
741 | This returns a language environment name as a string." | |
4ed46869 KH |
742 | (let* ((completion-ignore-case t) |
743 | (name (completing-read prompt | |
744 | language-info-alist | |
ddb5c041 KH |
745 | (and key |
746 | (function (lambda (elm) (assq key elm)))) | |
ae302641 | 747 | t nil nil default))) |
13e82c04 | 748 | (if (and (> (length name) 0) |
ddb5c041 KH |
749 | (or (not key) |
750 | (get-language-info name key))) | |
13e82c04 | 751 | name))) |
4ed46869 KH |
752 | \f |
753 | ;;; Multilingual input methods. | |
754 | ||
d0b9c3ab KH |
755 | (defconst leim-list-file-name "leim-list.el" |
756 | "Name of LEIM list file. | |
757 | This file contains a list of libraries of Emacs input methods (LEIM) | |
758 | in the format of Lisp expression for registering each input method. | |
759 | Emacs loads this file at startup time.") | |
760 | ||
2e224638 SM |
761 | (defvar leim-list-header (format |
762 | ";;; %s -- list of LEIM (Library of Emacs Input Method) | |
d0b9c3ab KH |
763 | ;; |
764 | ;; This file contains a list of LEIM (Library of Emacs Input Method) | |
e8dd0160 | 765 | ;; in the same directory as this file. Loading this file registers |
d0b9c3ab KH |
766 | ;; the whole input methods in Emacs. |
767 | ;; | |
d33d5fbe | 768 | ;; Each entry has the form: |
d0b9c3ab KH |
769 | ;; (register-input-method |
770 | ;; INPUT-METHOD LANGUAGE-NAME ACTIVATE-FUNC | |
771 | ;; TITLE DESCRIPTION | |
772 | ;; ARG ...) | |
773 | ;; See the function `register-input-method' for the meanings of arguments. | |
774 | ;; | |
775 | ;; If this directory is included in load-path, Emacs automatically | |
776 | ;; loads this file at startup time. | |
777 | ||
778 | " | |
779 | leim-list-file-name) | |
780 | "Header to be inserted in LEIM list file.") | |
781 | ||
e55e92ee | 782 | (defvar leim-list-entry-regexp "^(register-input-method" |
d0b9c3ab KH |
783 | "Regexp matching head of each entry in LEIM list file. |
784 | See also the variable `leim-list-header'") | |
785 | ||
786 | (defvar update-leim-list-functions | |
787 | '(quail-update-leim-list-file) | |
788 | "List of functions to call to update LEIM list file. | |
789 | Each function is called with one arg, LEIM directory name.") | |
790 | ||
a337fe7f RS |
791 | (defun update-leim-list-file (&rest dirs) |
792 | "Update LEIM list file in directories DIRS." | |
d0b9c3ab KH |
793 | (let ((functions update-leim-list-functions)) |
794 | (while functions | |
a337fe7f | 795 | (apply (car functions) dirs) |
d0b9c3ab KH |
796 | (setq functions (cdr functions))))) |
797 | ||
4ed46869 KH |
798 | (defvar current-input-method nil |
799 | "The current input method for multilingual text. | |
96db204a | 800 | If nil, that means no input method is activated now.") |
4ed46869 KH |
801 | (make-variable-buffer-local 'current-input-method) |
802 | (put 'current-input-method 'permanent-local t) | |
803 | ||
804 | (defvar current-input-method-title nil | |
d0b9c3ab | 805 | "Title string of the current input method shown in mode line.") |
4ed46869 KH |
806 | (make-variable-buffer-local 'current-input-method-title) |
807 | (put 'current-input-method-title 'permanent-local t) | |
808 | ||
b4fba33f | 809 | (defcustom default-input-method nil |
8861c593 | 810 | "*Default input method for multilingual text (a string). |
b4fba33f | 811 | This is the input method activated automatically by the command |
9b10b5a3 | 812 | `toggle-input-method' (\\[toggle-input-method])." |
8861c593 | 813 | :group 'mule |
5806e8a6 GM |
814 | :type '(choice (const nil) string) |
815 | :set-after '(current-language-environment)) | |
b4fba33f | 816 | |
0f835e87 KH |
817 | (put 'input-method-function 'permanent-local t) |
818 | ||
723a427a KH |
819 | (defvar input-method-history nil |
820 | "History list for some commands that read input methods.") | |
821 | (make-variable-buffer-local 'input-method-history) | |
822 | (put 'input-method-history 'permanent-local t) | |
4ed46869 KH |
823 | |
824 | (defvar inactivate-current-input-method-function nil | |
825 | "Function to call for inactivating the current input method. | |
826 | Every input method should set this to an appropriate value when activated. | |
f17ccaee KH |
827 | This function is called with no argument. |
828 | ||
829 | This function should never change the value of `current-input-method'. | |
830 | It is set to nil by the function `inactivate-input-method'.") | |
4ed46869 KH |
831 | (make-variable-buffer-local 'inactivate-current-input-method-function) |
832 | (put 'inactivate-current-input-method-function 'permanent-local t) | |
833 | ||
834 | (defvar describe-current-input-method-function nil | |
835 | "Function to call for describing the current input method. | |
836 | This function is called with no argument.") | |
837 | (make-variable-buffer-local 'describe-current-input-method-function) | |
838 | (put 'describe-current-input-method-function 'permanent-local t) | |
839 | ||
d0b9c3ab | 840 | (defvar input-method-alist nil |
2c395d56 | 841 | "Alist of input method names vs how to use them. |
d0b9c3ab | 842 | Each element has the form: |
2c395d56 RS |
843 | (INPUT-METHOD LANGUAGE-ENV ACTIVATE-FUNC TITLE DESCRIPTION ARGS...) |
844 | See the function `register-input-method' for the meanings of the elements.") | |
845 | ||
f08adf27 | 846 | (defun register-input-method (input-method lang-env &rest args) |
2c395d56 | 847 | "Register INPUT-METHOD as an input method for language environment ENV. |
f08adf27 | 848 | INPUT-METHOD and LANG-ENV are symbols or strings. |
d0b9c3ab | 849 | |
d0b9c3ab | 850 | The remaining arguments are: |
2c395d56 RS |
851 | ACTIVATE-FUNC, TITLE, DESCRIPTION, and ARGS... |
852 | ACTIVATE-FUNC is a function to call to activate this method. | |
853 | TITLE is a string to show in the mode line when this method is active. | |
854 | DESCRIPTION is a string describing this method and what it is good for. | |
855 | The ARGS, if any, are passed as arguments to ACTIVATE-FUNC. | |
205814ee KH |
856 | All told, the arguments to ACTIVATE-FUNC are INPUT-METHOD and the ARGS. |
857 | ||
858 | This function is mainly used in the file \"leim-list.el\" which is | |
859 | created at building time of emacs, registering all quail input methods | |
860 | contained in the emacs distribution. | |
861 | ||
862 | In case you want to register a new quail input method by yourself, be | |
863 | careful to use the same input method title as given in the third | |
864 | parameter of `quail-define-package' (if the values are different, the | |
865 | string specified in this function takes precedence). | |
866 | ||
867 | The commands `describe-input-method' and `list-input-methods' need | |
868 | this duplicated values to show some information about input methods | |
869 | without loading the affected quail packages." | |
f08adf27 RS |
870 | (if (symbolp lang-env) |
871 | (setq lang-env (symbol-name lang-env))) | |
4ef06f75 KH |
872 | (if (symbolp input-method) |
873 | (setq input-method (symbol-name input-method))) | |
f08adf27 | 874 | (let ((info (cons lang-env args)) |
d0b9c3ab KH |
875 | (slot (assoc input-method input-method-alist))) |
876 | (if slot | |
877 | (setcdr slot info) | |
878 | (setq slot (cons input-method info)) | |
879 | (setq input-method-alist (cons slot input-method-alist))))) | |
880 | ||
4d5ac029 | 881 | (defun read-input-method-name (prompt &optional default inhibit-null) |
d0b9c3ab | 882 | "Read a name of input method from a minibuffer prompting with PROMPT. |
4d5ac029 RS |
883 | If DEFAULT is non-nil, use that as the default, |
884 | and substitute it into PROMPT at the first `%s'. | |
4ef06f75 KH |
885 | If INHIBIT-NULL is non-nil, null input signals an error. |
886 | ||
887 | The return value is a string." | |
4d5ac029 RS |
888 | (if default |
889 | (setq prompt (format prompt default))) | |
d0b9c3ab | 890 | (let* ((completion-ignore-case t) |
723a427a KH |
891 | ;; This binding is necessary because input-method-history is |
892 | ;; buffer local. | |
d0b9c3ab | 893 | (input-method (completing-read prompt input-method-alist |
87505a98 RS |
894 | nil t nil 'input-method-history |
895 | default))) | |
bf294e6e KH |
896 | (if (and input-method (symbolp input-method)) |
897 | (setq input-method (symbol-name input-method))) | |
d0b9c3ab KH |
898 | (if (> (length input-method) 0) |
899 | input-method | |
900 | (if inhibit-null | |
43807b77 | 901 | (error "No valid input method is specified"))))) |
d0b9c3ab | 902 | |
d0b9c3ab | 903 | (defun activate-input-method (input-method) |
2c395d56 RS |
904 | "Switch to input method INPUT-METHOD for the current buffer. |
905 | If some other input method is already active, turn it off first. | |
906 | If INPUT-METHOD is nil, deactivate any current input method." | |
305a3cb6 | 907 | (if (and input-method (symbolp input-method)) |
4ef06f75 | 908 | (setq input-method (symbol-name input-method))) |
723a427a KH |
909 | (if (and current-input-method |
910 | (not (string= current-input-method input-method))) | |
305a3cb6 | 911 | (inactivate-input-method)) |
2c395d56 | 912 | (unless (or current-input-method (null input-method)) |
d0b9c3ab KH |
913 | (let ((slot (assoc input-method input-method-alist))) |
914 | (if (null slot) | |
723a427a | 915 | (error "Can't activate input method `%s'" input-method)) |
8efc03e1 KH |
916 | (let ((func (nth 2 slot))) |
917 | (if (functionp func) | |
918 | (apply (nth 2 slot) input-method (nthcdr 5 slot)) | |
919 | (if (and (consp func) (symbolp (car func)) (symbolp (cdr func))) | |
920 | (progn | |
921 | (require (cdr func)) | |
922 | (apply (car func) input-method (nthcdr 5 slot))) | |
923 | (error "Can't activate input method `%s'" input-method)))) | |
d0b9c3ab | 924 | (setq current-input-method input-method) |
723a427a | 925 | (setq current-input-method-title (nth 3 slot)) |
28885c0e KH |
926 | (unwind-protect |
927 | (run-hooks 'input-method-activate-hook) | |
928 | (force-mode-line-update))))) | |
15b3e511 | 929 | |
15b3e511 | 930 | (defun inactivate-input-method () |
f17ccaee | 931 | "Turn off the current input method." |
723a427a KH |
932 | (when current-input-method |
933 | (if input-method-history | |
934 | (unless (string= current-input-method (car input-method-history)) | |
935 | (setq input-method-history | |
936 | (cons current-input-method | |
937 | (delete current-input-method input-method-history)))) | |
938 | (setq input-method-history (list current-input-method))) | |
939 | (unwind-protect | |
940 | (funcall inactivate-current-input-method-function) | |
15b3e511 | 941 | (unwind-protect |
723a427a KH |
942 | (run-hooks 'input-method-inactivate-hook) |
943 | (setq current-input-method nil | |
28885c0e KH |
944 | current-input-method-title nil) |
945 | (force-mode-line-update))))) | |
4ed46869 | 946 | |
8b784951 | 947 | (defun set-input-method (input-method) |
2c395d56 RS |
948 | "Select and activate input method INPUT-METHOD for the current buffer. |
949 | This also sets the default input method to the one you specify." | |
d0b9c3ab | 950 | (interactive |
723a427a | 951 | (let* ((default (or (car input-method-history) default-input-method))) |
42395763 | 952 | (list (read-input-method-name |
87505a98 | 953 | (if default "Select input method (default %s): " "Select input method: ") |
42395763 | 954 | default t)))) |
d0b9c3ab | 955 | (activate-input-method input-method) |
42395763 | 956 | (setq default-input-method input-method)) |
4ed46869 KH |
957 | |
958 | (defun toggle-input-method (&optional arg) | |
15b3e511 | 959 | "Turn on or off a multilingual text input method for the current buffer. |
723a427a | 960 | |
f2979bdb KH |
961 | With no prefix argument, if an input method is currently activated, |
962 | turn it off. Otherwise, activate an input method -- the one most | |
963 | recently used, or the one specified in `default-input-method', or | |
964 | the one read from the minibuffer. | |
723a427a | 965 | |
f2979bdb KH |
966 | With a prefix argument, read an input method from the minibuffer and |
967 | turn it on. | |
723a427a | 968 | |
f2979bdb KH |
969 | The default is to use the most recent input method specified |
970 | \(not including the currently active input method, if any)." | |
4ed46869 | 971 | (interactive "P") |
7ddbb5bc RS |
972 | (if (and current-input-method (not arg)) |
973 | (inactivate-input-method) | |
974 | (let ((default (or (car input-method-history) default-input-method))) | |
975 | (if (and arg default (equal current-input-method default) | |
976 | (> (length input-method-history) 1)) | |
977 | (setq default (nth 1 input-method-history))) | |
723a427a KH |
978 | (activate-input-method |
979 | (if (or arg (not default)) | |
7ddbb5bc RS |
980 | (progn |
981 | (read-input-method-name | |
982 | (if default "Input method (default %s): " "Input method: " ) | |
983 | default t)) | |
723a427a KH |
984 | default)) |
985 | (or default-input-method | |
986 | (setq default-input-method current-input-method))))) | |
d0b9c3ab KH |
987 | |
988 | (defun describe-input-method (input-method) | |
2c395d56 | 989 | "Describe input method INPUT-METHOD." |
d0b9c3ab KH |
990 | (interactive |
991 | (list (read-input-method-name | |
992 | "Describe input method (default, current choice): "))) | |
78754934 | 993 | (if (and input-method (symbolp input-method)) |
4ef06f75 | 994 | (setq input-method (symbol-name input-method))) |
d0b9c3ab KH |
995 | (if (null input-method) |
996 | (describe-current-input-method) | |
997 | (with-output-to-temp-buffer "*Help*" | |
998 | (let ((elt (assoc input-method input-method-alist))) | |
999 | (princ (format "Input method: %s (`%s' in mode line) for %s\n %s\n" | |
1000 | input-method (nth 3 elt) (nth 1 elt) (nth 4 elt))))))) | |
1001 | ||
1002 | (defun describe-current-input-method () | |
96db204a | 1003 | "Describe the input method currently in use." |
4ed46869 KH |
1004 | (if current-input-method |
1005 | (if (and (symbolp describe-current-input-method-function) | |
1006 | (fboundp describe-current-input-method-function)) | |
1007 | (funcall describe-current-input-method-function) | |
1008 | (message "No way to describe the current input method `%s'" | |
f2979bdb | 1009 | current-input-method) |
4ed46869 | 1010 | (ding)) |
d0b9c3ab | 1011 | (error "No input method is activated now"))) |
4ed46869 | 1012 | |
d3459641 | 1013 | (defun read-multilingual-string (prompt &optional initial-input input-method) |
4ed46869 KH |
1014 | "Read a multilingual string from minibuffer, prompting with string PROMPT. |
1015 | The input method selected last time is activated in minibuffer. | |
15b3e511 | 1016 | If optional second arg INITIAL-INPUT is non-nil, insert it in the minibuffer |
d0b9c3ab KH |
1017 | initially. |
1018 | Optional 3rd argument INPUT-METHOD specifies the input method | |
4ef06f75 KH |
1019 | to be activated instead of the one selected last time. It is a symbol |
1020 | or a string." | |
88d559ec KH |
1021 | (setq input-method |
1022 | (or input-method | |
d3459641 | 1023 | current-input-method |
88d559ec KH |
1024 | default-input-method |
1025 | (read-input-method-name "Input method: " nil t))) | |
3df60841 | 1026 | (if (and input-method (symbolp input-method)) |
4ef06f75 | 1027 | (setq input-method (symbol-name input-method))) |
305a3cb6 KH |
1028 | (let ((prev-input-method current-input-method)) |
1029 | (unwind-protect | |
1030 | (progn | |
1031 | (activate-input-method input-method) | |
1032 | (read-string prompt initial-input nil nil t)) | |
1033 | (activate-input-method prev-input-method)))) | |
4ed46869 KH |
1034 | |
1035 | ;; Variables to control behavior of input methods. All input methods | |
1036 | ;; should react to these variables. | |
1037 | ||
8efc03e1 KH |
1038 | (defcustom input-method-verbose-flag 'default |
1039 | "*A flag to control extra guidance given by input methods. | |
1040 | The value should be nil, t, `complex-only', or `default'. | |
4ed46869 | 1041 | |
cb29dfb6 | 1042 | The extra guidance is done by showing list of available keys in echo |
8efc03e1 KH |
1043 | area. When you use the input method in the minibuffer, the guidance |
1044 | is shown at the bottom short window (split from the existing window). | |
c27c4ed8 | 1045 | |
8efc03e1 KH |
1046 | If the value is t, extra guidance is always given, if the value is |
1047 | nil, extra guidance is always suppressed. | |
1048 | ||
1049 | If the value is `complex-only', only complex input methods such as | |
1050 | `chinese-py' and `japanese' give extra guidance. | |
1051 | ||
1052 | If the value is `default', complex input methods always give extra | |
1053 | guidance, but simple input methods give it only when you are not in | |
1054 | the minibuffer. | |
1055 | ||
1056 | See also the variable `input-method-highlight-flag'." | |
1057 | :type '(choice (const t) (const nil) (const complex-only) (const default)) | |
42395763 RS |
1058 | :group 'mule) |
1059 | ||
1060 | (defcustom input-method-highlight-flag t | |
1061 | "*If this flag is non-nil, input methods highlight partially-entered text. | |
1062 | For instance, while you are in the middle of a Quail input method sequence, | |
1063 | the text inserted so far is temporarily underlined. | |
8efc03e1 KH |
1064 | The underlining goes away when you finish or abort the input method sequence. |
1065 | See also the variable `input-method-verbose-flag'." | |
42395763 RS |
1066 | :type 'boolean |
1067 | :group 'mule) | |
4ed46869 KH |
1068 | |
1069 | (defvar input-method-activate-hook nil | |
f17ccaee KH |
1070 | "Normal hook run just after an input method is activated. |
1071 | ||
1072 | The variable `current-input-method' keeps the input method name | |
1073 | just activated.") | |
4ed46869 KH |
1074 | |
1075 | (defvar input-method-inactivate-hook nil | |
f17ccaee KH |
1076 | "Normal hook run just after an input method is inactivated. |
1077 | ||
1078 | The variable `current-input-method' still keeps the input method name | |
4d0e6a11 | 1079 | just inactivated.") |
4ed46869 KH |
1080 | |
1081 | (defvar input-method-after-insert-chunk-hook nil | |
1082 | "Normal hook run just after an input method insert some chunk of text.") | |
1083 | ||
dccca980 KH |
1084 | (defvar input-method-exit-on-first-char nil |
1085 | "This flag controls a timing when an input method returns. | |
1086 | Usually, the input method does not return while there's a possibility | |
1087 | that it may find a different translation if a user types another key. | |
39e643e2 RS |
1088 | But, it this flag is non-nil, the input method returns as soon as |
1089 | the current key sequence gets long enough to have some valid translation.") | |
dccca980 KH |
1090 | |
1091 | (defvar input-method-use-echo-area nil | |
1092 | "This flag controls how an input method shows an intermediate key sequence. | |
39e643e2 RS |
1093 | Usually, the input method inserts the intermediate key sequence, |
1094 | or candidate translations corresponding to the sequence, | |
1095 | at point in the current buffer. | |
1096 | But, if this flag is non-nil, it displays them in echo area instead.") | |
dccca980 | 1097 | |
723a427a KH |
1098 | (defvar input-method-exit-on-invalid-key nil |
1099 | "This flag controls the behaviour of an input method on invalid key input. | |
1100 | Usually, when a user types a key which doesn't start any character | |
1101 | handled by the input method, the key is handled by turning off the | |
e8dd0160 | 1102 | input method temporarily. After that key, the input method is re-enabled. |
723a427a KH |
1103 | But, if this flag is non-nil, the input method is never back on.") |
1104 | ||
4ed46869 | 1105 | \f |
8efc03e1 KH |
1106 | (defvar set-language-environment-hook nil |
1107 | "Normal hook run after some language environment is set. | |
1108 | ||
1109 | When you set some hook function here, that effect usually should not | |
1110 | be inherited to another language environment. So, you had better set | |
1111 | another function in `exit-language-environment-hook' (which see) to | |
1112 | cancel the effect.") | |
1113 | ||
1114 | (defvar exit-language-environment-hook nil | |
1115 | "Normal hook run after exiting from some language environment. | |
1116 | When this hook is run, the variable `current-language-environment' | |
1117 | is still bound to the language environment being exited. | |
1118 | ||
e8dd0160 | 1119 | This hook is mainly used for canceling the effect of |
8efc03e1 KH |
1120 | `set-language-environment-hook' (which-see).") |
1121 | ||
b0648a00 RS |
1122 | (put 'setup-specified-language-environment 'apropos-inhibit t) |
1123 | ||
15b3e511 | 1124 | (defun setup-specified-language-environment () |
f08adf27 | 1125 | "Switch to a specified language environment." |
15b3e511 | 1126 | (interactive) |
f850d782 | 1127 | (let (language-name) |
15b3e511 KH |
1128 | (if (and (symbolp last-command-event) |
1129 | (or (not (eq last-command-event 'Default)) | |
1130 | (setq last-command-event 'English)) | |
f850d782 RS |
1131 | (setq language-name (symbol-name last-command-event))) |
1132 | (set-language-environment language-name) | |
15b3e511 | 1133 | (error "Bogus calling sequence")))) |
4ed46869 | 1134 | |
8861c593 | 1135 | (defcustom current-language-environment "English" |
94d04df6 | 1136 | "The last language environment specified with `set-language-environment'. |
ebef6d93 KH |
1137 | This variable should be set only with \\[customize], which is equivalent |
1138 | to using the function `set-language-environment'." | |
94d04df6 | 1139 | :link '(custom-manual "(emacs)Language Environments") |
dff1aa24 | 1140 | :set (lambda (symbol value) (set-language-environment value)) |
94d04df6 DL |
1141 | :get (lambda (x) |
1142 | (or (car-safe (assoc-ignore-case | |
1143 | (if (symbolp current-language-environment) | |
1144 | (symbol-name current-language-environment) | |
1145 | current-language-environment) | |
1146 | language-info-alist)) | |
1147 | "English")) | |
1148 | :type (cons 'choice (mapcar (lambda (lang) | |
1149 | (list 'const (car lang))) | |
1150 | language-info-alist)) | |
8861c593 RS |
1151 | :initialize 'custom-initialize-default |
1152 | :group 'mule | |
1153 | :type 'string) | |
f850d782 | 1154 | |
ddb5c041 KH |
1155 | (defun reset-language-environment () |
1156 | "Reset multilingual environment of Emacs to the default status. | |
1157 | ||
1158 | The default status is as follows: | |
1159 | ||
1160 | The default value of buffer-file-coding-system is nil. | |
1161 | The default coding system for process I/O is nil. | |
1162 | The default value for the command `set-terminal-coding-system' is nil. | |
1163 | The default value for the command `set-keyboard-coding-system' is nil. | |
1164 | ||
1165 | The order of priorities of coding categories and the coding system | |
1166 | bound to each category are as follows | |
1167 | coding category coding system | |
1168 | -------------------------------------------------- | |
1169 | coding-category-iso-8-2 iso-latin-1 | |
1170 | coding-category-iso-8-1 iso-latin-1 | |
1171 | coding-category-iso-7-tight iso-2022-jp | |
1172 | coding-category-iso-7 iso-2022-7bit | |
1173 | coding-category-iso-7-else iso-2022-7bit-lock | |
1174 | coding-category-iso-8-else iso-2022-8bit-ss2 | |
1175 | coding-category-emacs-mule emacs-mule | |
1176 | coding-category-raw-text raw-text | |
1177 | coding-category-sjis japanese-shift-jis | |
1178 | coding-category-big5 chinese-big5 | |
1179 | coding-category-ccl nil | |
e8dd0160 | 1180 | coding-category-binary no-conversion |
ddb5c041 KH |
1181 | " |
1182 | (interactive) | |
1183 | ;; This function formerly set default-enable-multibyte-characters to t, | |
1184 | ;; but that is incorrect. It should not alter the unibyte/multibyte choice. | |
1185 | ||
1186 | (setq coding-category-iso-7-tight 'iso-2022-jp | |
1187 | coding-category-iso-7 'iso-2022-7bit | |
1188 | coding-category-iso-8-1 'iso-latin-1 | |
1189 | coding-category-iso-8-2 'iso-latin-1 | |
1190 | coding-category-iso-7-else 'iso-2022-7bit-lock | |
1191 | coding-category-iso-8-else 'iso-2022-8bit-ss2 | |
1192 | coding-category-emacs-mule 'emacs-mule | |
1193 | coding-category-raw-text 'raw-text | |
1194 | coding-category-sjis 'japanese-shift-jis | |
1195 | coding-category-big5 'chinese-big5 | |
9bfcd269 KH |
1196 | coding-category-utf-8 nil |
1197 | coding-category-utf-16-be nil | |
1198 | coding-category-utf-16-le nil | |
ddb5c041 KH |
1199 | coding-category-ccl nil |
1200 | coding-category-binary 'no-conversion) | |
1201 | ||
1202 | (set-coding-priority | |
1203 | '(coding-category-iso-8-1 | |
1204 | coding-category-iso-8-2 | |
1205 | coding-category-iso-7-tight | |
1206 | coding-category-iso-7 | |
1207 | coding-category-iso-7-else | |
1208 | coding-category-iso-8-else | |
1209 | coding-category-emacs-mule | |
1210 | coding-category-raw-text | |
1211 | coding-category-sjis | |
1212 | coding-category-big5 | |
1213 | coding-category-ccl | |
9bfcd269 KH |
1214 | coding-category-binary |
1215 | coding-category-utf-16-be | |
1216 | coding-category-utf-16-le | |
1217 | coding-category-utf-8)) | |
ddb5c041 | 1218 | |
91693d18 KH |
1219 | (update-coding-systems-internal) |
1220 | ||
ddb5c041 KH |
1221 | (set-default-coding-systems nil) |
1222 | ;; Don't alter the terminal and keyboard coding systems here. | |
1223 | ;; The terminal still supports the same coding system | |
1224 | ;; that it supported a minute ago. | |
1225 | ;;; (set-terminal-coding-system-internal nil) | |
1226 | ;;; (set-keyboard-coding-system-internal nil) | |
1227 | ||
1228 | (setq nonascii-translation-table nil | |
1229 | nonascii-insert-offset 0)) | |
1230 | ||
40c81f74 PE |
1231 | (defun set-display-table-and-terminal-coding-system (language-name) |
1232 | "Set up the display table and terminal coding system for LANGUAGE-NAME." | |
1233 | (let ((coding (get-language-info language-name 'unibyte-display))) | |
1234 | (if coding | |
1235 | (standard-display-european-internal) | |
1236 | (standard-display-default (if (eq window-system 'pc) 128 160) 255) | |
1237 | (aset standard-display-table 146 nil)) | |
1238 | (or (eq window-system 'pc) | |
1239 | (set-terminal-coding-system coding)))) | |
1240 | ||
166246f7 | 1241 | (defun set-language-environment (language-name) |
6c05d680 RS |
1242 | "Set up multi-lingual environment for using LANGUAGE-NAME. |
1243 | This sets the coding system priority and the default input method | |
8861c593 RS |
1244 | and sometimes other things. LANGUAGE-NAME should be a string |
1245 | which is the name of a language environment. For example, \"Latin-1\" | |
1246 | specifies the character set for the major languages of Western Europe." | |
8efc03e1 | 1247 | (interactive (list (read-language-name |
ddb5c041 | 1248 | nil |
8efc03e1 | 1249 | "Set language environment (default, English): "))) |
4ef06f75 KH |
1250 | (if language-name |
1251 | (if (symbolp language-name) | |
1252 | (setq language-name (symbol-name language-name))) | |
1253 | (setq language-name "English")) | |
ddb5c041 | 1254 | (or (assoc-ignore-case language-name language-info-alist) |
f850d782 | 1255 | (error "Language environment not defined: %S" language-name)) |
8efc03e1 KH |
1256 | (if current-language-environment |
1257 | (let ((func (get-language-info current-language-environment | |
1258 | 'exit-function))) | |
e63645c2 KH |
1259 | (run-hooks 'exit-language-environment-hook) |
1260 | (if (fboundp func) (funcall func)))) | |
03c35c83 EZ |
1261 | (let ((default-eol-type (coding-system-eol-type |
1262 | default-buffer-file-coding-system))) | |
1263 | (reset-language-environment) | |
ddb5c041 | 1264 | |
03c35c83 EZ |
1265 | (setq current-language-environment language-name) |
1266 | (set-language-environment-coding-systems language-name default-eol-type)) | |
ddb5c041 KH |
1267 | (let ((input-method (get-language-info language-name 'input-method))) |
1268 | (when input-method | |
1269 | (setq default-input-method input-method) | |
1270 | (if input-method-history | |
1271 | (setq input-method-history | |
1272 | (cons input-method | |
1273 | (delete input-method input-method-history)))))) | |
ec241f58 EZ |
1274 | (let ((nonascii (get-language-info language-name 'nonascii-translation)) |
1275 | (dos-table | |
4e2ac2d9 EZ |
1276 | (if (eq window-system 'pc) |
1277 | (intern | |
1278 | (concat "cp" dos-codepage "-nonascii-translation-table"))))) | |
03c35c83 EZ |
1279 | (cond |
1280 | ((char-table-p nonascii) | |
1281 | (setq nonascii-translation-table nonascii)) | |
ec241f58 | 1282 | ((and (eq window-system 'pc) (boundp dos-table)) |
03c35c83 EZ |
1283 | ;; DOS terminals' default is to use a special non-ASCII translation |
1284 | ;; table as appropriate for the installed codepage. | |
ec241f58 | 1285 | (setq nonascii-translation-table (symbol-value dos-table))) |
03c35c83 EZ |
1286 | ((charsetp nonascii) |
1287 | (setq nonascii-insert-offset (- (make-char nonascii) 128))))) | |
ddb5c041 | 1288 | |
45d08cb2 KH |
1289 | (setq charset-origin-alist |
1290 | (get-language-info language-name 'charset-origin-alist)) | |
ddb5c041 | 1291 | |
63283a8f | 1292 | ;; Unibyte setups if necessary. |
ddb5c041 | 1293 | (unless default-enable-multibyte-characters |
63283a8f | 1294 | ;; Syntax and case table. |
ddb5c041 KH |
1295 | (let ((syntax (get-language-info language-name 'unibyte-syntax))) |
1296 | (if syntax | |
1297 | (let ((set-case-syntax-set-multibyte nil)) | |
63283a8f KH |
1298 | (load syntax nil t)) |
1299 | ;; No information for syntax and case. Reset to the defaults. | |
1300 | (let ((syntax-table (standard-syntax-table)) | |
1301 | (case-table (standard-case-table)) | |
03c35c83 | 1302 | (ch (if (eq window-system 'pc) 128 160))) |
63283a8f KH |
1303 | (while (< ch 256) |
1304 | (modify-syntax-entry ch " " syntax-table) | |
1305 | (aset case-table ch ch) | |
1306 | (setq ch (1+ ch))) | |
1307 | (set-char-table-extra-slot case-table 0 nil) | |
1308 | (set-char-table-extra-slot case-table 1 nil) | |
1309 | (set-char-table-extra-slot case-table 2 nil)) | |
1310 | (set-standard-case-table (standard-case-table)) | |
1311 | (let ((list (buffer-list))) | |
1312 | (while list | |
1313 | (with-current-buffer (car list) | |
1314 | (set-case-table (standard-case-table))) | |
1315 | (setq list (cdr list)))))) | |
40c81f74 | 1316 | (set-display-table-and-terminal-coding-system language-name)) |
ddb5c041 KH |
1317 | |
1318 | (let ((required-features (get-language-info language-name 'features))) | |
1319 | (while required-features | |
1320 | (require (car required-features)) | |
1321 | (setq required-features (cdr required-features)))) | |
1322 | (let ((func (get-language-info language-name 'setup-function))) | |
1323 | (if (fboundp func) | |
1324 | (funcall func))) | |
8efc03e1 | 1325 | (run-hooks 'set-language-environment-hook) |
f850d782 | 1326 | (force-mode-line-update t)) |
4ed46869 | 1327 | |
51a8fc1d RS |
1328 | (defun standard-display-european-internal () |
1329 | ;; Actually set up direct output of non-ASCII characters. | |
03c35c83 EZ |
1330 | (standard-display-8bit (if (eq window-system 'pc) 128 160) 255) |
1331 | ;; Unibyte Emacs on MS-DOS wants to display all 8-bit characters with | |
1332 | ;; the native font, and codes 160 and 146 stand for something very | |
1333 | ;; different there. | |
1334 | (or (and (eq window-system 'pc) (not default-enable-multibyte-characters)) | |
1335 | (progn | |
1336 | ;; Make non-line-break space display as a plain space. | |
1337 | ;; Most X fonts do the wrong thing for code 160. | |
1338 | (aset standard-display-table 160 [32]) | |
1339 | ;; Most Windows programs send out apostrophe's as \222. Most X fonts | |
1340 | ;; don't contain a character at that position. Map it to the ASCII | |
1341 | ;; apostrophe. | |
1342 | (aset standard-display-table 146 [39])))) | |
1343 | ||
1344 | (defun set-language-environment-coding-systems (language-name | |
1345 | &optional eol-type) | |
1346 | "Do various coding system setups for language environment LANGUAGE-NAME. | |
1347 | ||
1348 | The optional arg EOL-TYPE specifies the eol-type of the default value | |
1349 | of buffer-file-coding-system set by this function." | |
54b226f7 KH |
1350 | (let* ((priority (get-language-info language-name 'coding-priority)) |
1351 | (default-coding (car priority))) | |
1352 | (if priority | |
1353 | (let ((categories (mapcar 'coding-system-category priority))) | |
03c35c83 EZ |
1354 | (set-default-coding-systems |
1355 | (if (memq eol-type '(0 1 2 unix dos mac)) | |
1356 | (coding-system-change-eol-conversion default-coding eol-type) | |
1357 | default-coding)) | |
46babb23 | 1358 | (setq default-sendmail-coding-system default-coding) |
54b226f7 KH |
1359 | (set-coding-priority categories) |
1360 | (while priority | |
1361 | (set (car categories) (car priority)) | |
1362 | (setq priority (cdr priority) categories (cdr categories))) | |
45d08cb2 | 1363 | (update-coding-systems-internal))))) |
54b226f7 | 1364 | |
4ed46869 KH |
1365 | ;; Print all arguments with `princ', then print "\n". |
1366 | (defsubst princ-list (&rest args) | |
1367 | (while args (princ (car args)) (setq args (cdr args))) | |
1368 | (princ "\n")) | |
1369 | ||
b0648a00 RS |
1370 | (put 'describe-specified-language-support 'apropos-inhibit t) |
1371 | ||
48082651 | 1372 | ;; Print a language specific information such as input methods, |
13e82c04 | 1373 | ;; charsets, and coding systems. This function is intended to be |
48082651 | 1374 | ;; called from the menu: |
281d03ec | 1375 | ;; [menu-bar mule describe-language-environment LANGUAGE] |
48082651 KH |
1376 | ;; and should not run it by `M-x describe-current-input-method-function'. |
1377 | (defun describe-specified-language-support () | |
96db204a | 1378 | "Describe how Emacs supports the specified language environment." |
48082651 | 1379 | (interactive) |
281d03ec | 1380 | (let (language-name) |
48082651 | 1381 | (if (not (and (symbolp last-command-event) |
281d03ec | 1382 | (setq language-name (symbol-name last-command-event)))) |
48082651 | 1383 | (error "Bogus calling sequence")) |
281d03ec RS |
1384 | (describe-language-environment language-name))) |
1385 | ||
1386 | (defun describe-language-environment (language-name) | |
1387 | "Describe how Emacs supports language environment LANGUAGE-NAME." | |
78754934 KH |
1388 | (interactive |
1389 | (list (read-language-name | |
1390 | 'documentation | |
8adfa8be | 1391 | "Describe language environment (default, current choice): "))) |
f850d782 RS |
1392 | (if (null language-name) |
1393 | (setq language-name current-language-environment)) | |
281d03ec RS |
1394 | (if (or (null language-name) |
1395 | (null (get-language-info language-name 'documentation))) | |
1396 | (error "No documentation for the specified language")) | |
4ef06f75 KH |
1397 | (if (symbolp language-name) |
1398 | (setq language-name (symbol-name language-name))) | |
281d03ec | 1399 | (let ((doc (get-language-info language-name 'documentation))) |
48082651 | 1400 | (with-output-to-temp-buffer "*Help*" |
a33c9d6f | 1401 | (princ-list language-name " language environment" "\n") |
13e82c04 | 1402 | (if (stringp doc) |
d0b9c3ab KH |
1403 | (progn |
1404 | (princ-list doc) | |
1405 | (terpri))) | |
15b3e511 KH |
1406 | (let ((str (get-language-info language-name 'sample-text))) |
1407 | (if (stringp str) | |
1408 | (progn | |
281d03ec | 1409 | (princ "Sample text:\n") |
d0b9c3ab KH |
1410 | (princ-list " " str) |
1411 | (terpri)))) | |
ddb5c041 | 1412 | (let ((input-method (get-language-info language-name 'input-method)) |
00a069c6 | 1413 | (l (copy-sequence input-method-alist))) |
ddb5c041 KH |
1414 | (princ "Input methods") |
1415 | (when input-method | |
1416 | (princ (format " (default, %s)" input-method)) | |
1417 | (setq input-method (assoc input-method input-method-alist)) | |
1418 | (setq l (cons input-method (delete input-method l)))) | |
1419 | (princ ":\n") | |
15b3e511 | 1420 | (while l |
d0b9c3ab KH |
1421 | (if (string= language-name (nth 1 (car l))) |
1422 | (princ-list " " (car (car l)) | |
1423 | (format " (`%s' in mode line)" (nth 3 (car l))))) | |
15b3e511 | 1424 | (setq l (cdr l)))) |
281d03ec RS |
1425 | (terpri) |
1426 | (princ "Character sets:\n") | |
15b3e511 KH |
1427 | (let ((l (get-language-info language-name 'charset))) |
1428 | (if (null l) | |
1429 | (princ-list " nothing specific to " language-name) | |
1430 | (while l | |
1431 | (princ-list " " (car l) ": " | |
1432 | (charset-description (car l))) | |
1433 | (setq l (cdr l))))) | |
281d03ec RS |
1434 | (terpri) |
1435 | (princ "Coding systems:\n") | |
15b3e511 KH |
1436 | (let ((l (get-language-info language-name 'coding-system))) |
1437 | (if (null l) | |
1438 | (princ-list " nothing specific to " language-name) | |
48082651 | 1439 | (while l |
281d03ec RS |
1440 | (princ (format " %s (`%c' in mode line):\n\t%s\n" |
1441 | (car l) | |
1442 | (coding-system-mnemonic (car l)) | |
a904b20b | 1443 | (coding-system-doc-string (car l)))) |
8efc03e1 KH |
1444 | (let ((aliases (coding-system-get (car l) 'alias-coding-systems))) |
1445 | (when aliases | |
1446 | (princ "\t") | |
1447 | (princ (cons 'alias: (cdr aliases))) | |
1448 | (terpri))) | |
15b3e511 | 1449 | (setq l (cdr l)))))))) |
4ed46869 | 1450 | \f |
40c81f74 PE |
1451 | ;;; Locales. |
1452 | ||
1453 | (defvar locale-translation-file-name | |
1454 | (let ((files '("/usr/lib/X11/locale/locale.alias" ; e.g. X11R6.4 | |
1455 | "/usr/X11R6/lib/X11/locale/locale.alias" ; e.g. RedHat 4.2 | |
1456 | "/usr/openwin/lib/locale/locale.alias" ; e.g. Solaris 2.6 | |
1457 | ;; | |
1458 | ;; The following name appears after the X-related names above, | |
1459 | ;; since the X-related names are what X actually uses. | |
1460 | "/usr/share/locale/locale.alias" ; GNU/Linux sans X | |
1461 | ))) | |
1462 | (while (and files (not (file-exists-p (car files)))) | |
1463 | (setq files (cdr files))) | |
1464 | (car files)) | |
1465 | "*File name for the system's file of locale-name aliases, or nil if none.") | |
1466 | ||
1467 | (defvar locale-language-names | |
1468 | '( | |
1469 | ;; UTF-8 is not yet implemented. | |
1470 | ;; Put this first, so that e.g. "ko.UTF-8" does not match "ko" below. | |
1471 | (".*[._]utf" . nil) | |
1472 | ||
1473 | ;; Locale names of the form LANGUAGE[_TERRITORY][.CODESET][@MODIFIER] | |
1474 | ;; as specified in the Single Unix Spec, Version 2. | |
1475 | ;; LANGUAGE is a language code taken from ISO 639:1988 (E/F) | |
1476 | ;; with additions from ISO 639/RA Newsletter No.1/1989; | |
1477 | ;; see Internet RFC 2165 (1997-06). | |
1478 | ;; TERRITORY is a country code taken from ISO 3166. | |
1479 | ;; CODESET and MODIFIER are implementation-dependent. | |
1480 | ;; | |
1481 | ; aa Afar | |
1482 | ; ab Abkhazian | |
6ececc4d | 1483 | ("af" . "Latin-1") ; Afrikaans |
40c81f74 PE |
1484 | ("am" . "Ethiopic") ; Amharic |
1485 | ; ar Arabic | |
1486 | ; as Assamese | |
1487 | ; ay Aymara | |
1488 | ; az Azerbaijani | |
1489 | ; ba Bashkir | |
6ececc4d PE |
1490 | ("be" . "Latin-5") ; Byelorussian |
1491 | ("bg" . "Latin-5") ; Bulgarian | |
40c81f74 PE |
1492 | ; bh Bihari |
1493 | ; bi Bislama | |
1494 | ; bn Bengali, Bangla | |
1495 | ("bo" . "Tibetan") | |
1496 | ("br" . "Latin-1") ; Breton | |
1497 | ("ca" . "Latin-1") ; Catalan | |
1498 | ; co Corsican | |
1499 | ("cs" . "Czech") | |
6ececc4d | 1500 | ("cy" . "Latin-8") ; Welsh |
40c81f74 PE |
1501 | ("da" . "Latin-1") ; Danish |
1502 | ("de" . "German") | |
1503 | ; dz Bhutani | |
1504 | ("el" . "Greek") | |
6ececc4d PE |
1505 | ;; Users who specify "en" explicitly typically want Latin-1, not ASCII. |
1506 | ("en" . "Latin-1") ; English | |
40c81f74 PE |
1507 | ("eo" . "Latin-3") ; Esperanto |
1508 | ("es" . "Latin-1") ; Spanish | |
1509 | ("et" . "Latin-4") ; Estonian | |
1510 | ("eu" . "Latin-1") ; Basque | |
1511 | ; fa Persian | |
1512 | ("fi" . "Latin-1") ; Finnish | |
1513 | ; fj Fiji | |
1514 | ("fo" . "Latin-1") ; Faroese | |
1515 | ("fr" . "Latin-1") ; French | |
1516 | ("fy" . "Latin-1") ; Frisian | |
6ececc4d PE |
1517 | ("ga" . "Latin-1") ; Irish Gaelic (new orthography) |
1518 | ("gd" . "Latin-1") ; Scots Gaelic | |
1519 | ("gl" . "Latin-1") ; Galician | |
40c81f74 PE |
1520 | ; gn Guarani |
1521 | ; gu Gujarati | |
1522 | ; ha Hausa | |
1523 | ("he" . "Hebrew") | |
1524 | ("hi" . "Devanagari") ; Hindi | |
1525 | ("hr" . "Latin-2") ; Croatian | |
1526 | ("hu" . "Latin-2") ; Hungarian | |
1527 | ; hy Armenian | |
1528 | ; ia Interlingua | |
1529 | ("id" . "Latin-1") ; Indonesian | |
1530 | ; ie Interlingue | |
1531 | ; ik Inupiak | |
1532 | ("is" . "Latin-1") ; Icelandic | |
1533 | ("it" . "Latin-1") ; Italian | |
1534 | ; iu Inuktitut | |
1535 | ("ja" . "Japanese") | |
1536 | ; jw Javanese | |
1537 | ; ka Georgian | |
1538 | ; kk Kazakh | |
6ececc4d | 1539 | ("kl" . "Latin-1") ; Greenlandic |
40c81f74 PE |
1540 | ; km Cambodian |
1541 | ; kn Kannada | |
1542 | ("ko" . "Korean") | |
1543 | ; ks Kashmiri | |
1544 | ; ku Kurdish | |
1545 | ; ky Kirghiz | |
1546 | ("la" . "Latin-1") ; Latin | |
1547 | ; ln Lingala | |
1548 | ("lo" . "Lao") ; Laothian | |
1549 | ("lt" . "Latin-4") ; Lithuanian | |
1550 | ("lv" . "Latin-4") ; Latvian, Lettish | |
1551 | ; mg Malagasy | |
1552 | ; mi Maori | |
6ececc4d | 1553 | ("mk" . "Latin-5") ; Macedonian |
40c81f74 PE |
1554 | ; ml Malayalam |
1555 | ; mn Mongolian | |
1556 | ; mo Moldavian | |
1557 | ("mr" . "Devanagari") ; Marathi | |
1558 | ; ms Malay | |
1559 | ("mt" . "Latin-3") ; Maltese | |
1560 | ; my Burmese | |
1561 | ; na Nauru | |
1562 | ("ne" . "Devanagari") ; Nepali | |
1563 | ("nl" . "Latin-1") ; Dutch | |
1564 | ("no" . "Latin-1") ; Norwegian | |
1565 | ; oc Occitan | |
1566 | ; om (Afan) Oromo | |
1567 | ; or Oriya | |
1568 | ; pa Punjabi | |
1569 | ("pl" . "Latin-2") ; Polish | |
1570 | ; ps Pashto, Pushto | |
1571 | ("pt" . "Latin-1") ; Portuguese | |
1572 | ; qu Quechua | |
6ececc4d | 1573 | ("rm" . "Latin-1") ; Rhaeto-Romanic |
40c81f74 PE |
1574 | ; rn Kirundi |
1575 | ("ro" . "Romanian") | |
1576 | ("ru.*[_.]koi8" . "Cyrillic-KOI8") ; Russian | |
6ececc4d | 1577 | ("ru" . "Latin-5") ; Russian |
40c81f74 PE |
1578 | ; rw Kinyarwanda |
1579 | ("sa" . "Devanagari") ; Sanskrit | |
1580 | ; sd Sindhi | |
1581 | ; sg Sangho | |
1582 | ("sh" . "Latin-2") ; Serbo-Croatian | |
1583 | ; si Sinhalese | |
1584 | ("sk" . "Slovak") | |
1585 | ("sl" . "Slovenian") | |
1586 | ; sm Samoan | |
1587 | ; sn Shona | |
1588 | ; so Somali | |
6ececc4d | 1589 | ("sq" . "Latin-1") ; Albanian |
40c81f74 PE |
1590 | ("sr" . "Latin-2") ; Serbian (Latin alphabet) |
1591 | ; ss Siswati | |
1592 | ; st Sesotho | |
1593 | ; su Sundanese | |
1594 | ("sv" . "Latin-1") ; Swedish | |
1595 | ("sw" . "Latin-1") ; Swahili | |
1596 | ; ta Tamil | |
1597 | ; te Telugu | |
1598 | ; tg Tajik | |
1599 | ("th" . "Thai") | |
1600 | ; ti Tigrinya | |
1601 | ; tk Turkmen | |
6ececc4d | 1602 | ("tl" . "Latin-1") ; Tagalog |
40c81f74 PE |
1603 | ; tn Setswana |
1604 | ; to Tonga | |
1605 | ("tr" . "Latin-5") ; Turkish | |
1606 | ; ts Tsonga | |
1607 | ; tt Tatar | |
1608 | ; tw Twi | |
1609 | ; ug Uighur | |
6ececc4d | 1610 | ("uk" . "Latin-5") ; Ukrainian |
40c81f74 PE |
1611 | ; ur Urdu |
1612 | ; uz Uzbek | |
1613 | ("vi" . "Vietnamese") | |
1614 | ; vo Volapuk | |
1615 | ; wo Wolof | |
1616 | ; xh Xhosa | |
1617 | ; yi Yiddish | |
1618 | ; yo Yoruba | |
1619 | ; za Zhuang | |
1620 | ("zh.*[._]big5" . "Chinese-BIG5") | |
1621 | ("zh.*[._]gbk" . nil) ; Solaris 2.7; has gbk-0 as well as GB 2312.1980-0 | |
1622 | ("zh_tw" . "Chinese-CNS") | |
1623 | ("zh" . "Chinese-GB") | |
1624 | ; zu Zulu | |
1625 | ||
1626 | ;; ISO standard locales | |
1627 | ("c$" . "ASCII") | |
1628 | ("posix$" . "ASCII") | |
1629 | ||
40c81f74 PE |
1630 | ;; The "IPA" Emacs language environment does not correspond |
1631 | ;; to any ISO 639 code, so let it stand for itself. | |
1632 | ("ipa$" . "IPA") | |
1633 | ||
1634 | ;; Nonstandard or obsolete language codes | |
1635 | ("cz" . "Czech") ; e.g. Solaris 2.6 | |
1636 | ("ee" . "Latin-4") ; Estonian, e.g. X11R6.4 | |
1637 | ("iw" . "Hebrew") ; e.g. X11R6.4 | |
6ececc4d | 1638 | ("sp" . "Latin-5") ; Serbian (Cyrillic alphabet), e.g. X11R6.4 |
40c81f74 PE |
1639 | ("su" . "Latin-1") ; Finnish, e.g. Solaris 2.6 |
1640 | ) | |
1641 | "List of pairs of locale regexps and language names. | |
6ececc4d PE |
1642 | The first element whose locale regexp matches the start of a downcased locale |
1643 | specifies the language name corresponding to that locale. | |
40c81f74 PE |
1644 | If the language name is nil, there is no corresponding language environment.") |
1645 | ||
6ececc4d PE |
1646 | (defvar locale-charset-language-names |
1647 | '((".*8859[-_]?1\\>" . "Latin-1") | |
1648 | (".*8859[-_]?2\\>" . "Latin-2") | |
1649 | (".*8859[-_]?3\\>" . "Latin-3") | |
1650 | (".*8859[-_]?4\\>" . "Latin-4") | |
1651 | (".*8859[-_]?9\\>" . "Latin-5") | |
1652 | (".*8859[-_]?14\\>" . "Latin-8") | |
1653 | (".*8859[-_]?15\\>" . "Latin-9") | |
1654 | ) | |
1655 | "List of pairs of locale regexps and charset language names. | |
1656 | The first element whose locale regexp matches the start of a downcased locale | |
1657 | specifies the language name whose charsets corresponds to that locale. | |
1658 | This language name is used if its charsets disagree with the charsets of | |
1659 | the language name that would otherwise be used for this locale.") | |
1660 | ||
40c81f74 PE |
1661 | (defvar locale-preferred-coding-systems |
1662 | '(("ja.*[._]euc" . japanese-iso-8bit) | |
1663 | ("ja.*[._]jis7" . iso-2022-jp) | |
1664 | ("ja.*[._]pck" . japanese-shift-jis) | |
1665 | ("ja.*[._]sjis" . japanese-shift-jis) | |
40c81f74 | 1666 | ) |
6ececc4d PE |
1667 | "List of pairs of locale regexps and preferred coding systems. |
1668 | The first element whose locale regexp matches the start of a downcased locale | |
1669 | specifies the coding system to prefer when using that locale.") | |
40c81f74 PE |
1670 | |
1671 | (defun locale-name-match (key alist) | |
1672 | "Search for KEY in ALIST, which should be a list of regexp-value pairs. | |
1673 | Return the value corresponding to the first regexp that matches the | |
1674 | start of KEY, or nil if there is no match." | |
1675 | (let (element) | |
1676 | (while (and alist (not element)) | |
1677 | (if (string-match (concat "^\\(" (car (car alist)) "\\)") key) | |
1678 | (setq element (car alist))) | |
1679 | (setq alist (cdr alist))) | |
1680 | (cdr element))) | |
1681 | ||
1682 | (defun set-locale-environment (locale-name) | |
1683 | "Set up multi-lingual environment for using LOCALE-NAME. | |
1684 | This sets the coding system priority and the default input method | |
1685 | and sometimes other things. LOCALE-NAME should be a string | |
1686 | which is the name of a locale supported by the system; | |
1687 | often it is of the form xx_XX.CODE, where xx is a language, | |
1688 | XX is a country, and CODE specifies a character set and coding system. | |
1689 | For example, the locale name \"ja_JP.EUC\" might name a locale | |
1690 | for Japanese in Japan using the `japanese-iso-8bit' coding-system. | |
1691 | ||
1692 | If LOCALE-NAME is nil, its value is taken from the environment. | |
1693 | ||
1694 | The locale names supported by your system can typically be found in a | |
1695 | directory named `/usr/share/locale' or `/usr/lib/locale'." | |
1696 | ||
1697 | (unless locale-name | |
1698 | ;; Use the first of these three environment variables | |
1699 | ;; that has a nonempty value. | |
1700 | (let ((vars '("LC_ALL" "LC_CTYPE" "LANG"))) | |
1701 | (while (and vars (not (setq locale-name (getenv (car vars))))) | |
1702 | (setq vars (cdr vars))))) | |
1703 | ||
1704 | (when locale-name | |
1705 | ||
1706 | ;; Translate "swedish" into "sv_SE.ISO8859-1", and so on, | |
1707 | ;; using the translation file that many systems have. | |
1708 | (when locale-translation-file-name | |
1709 | (with-temp-buffer | |
1710 | (insert-file-contents locale-translation-file-name) | |
1711 | (when (re-search-forward | |
1712 | (concat "^" (regexp-quote locale-name) ":?[ \t]+") nil t) | |
1713 | (setq locale-name (buffer-substring (point) (line-end-position)))))) | |
1714 | ||
1715 | (setq locale-name (downcase locale-name)) | |
1716 | ||
6ececc4d PE |
1717 | (let ((language-name |
1718 | (locale-name-match locale-name locale-language-names)) | |
1719 | (charset-language-name | |
1720 | (locale-name-match locale-name locale-charset-language-names)) | |
1721 | (coding-system | |
1722 | (locale-name-match locale-name locale-preferred-coding-systems))) | |
1723 | ||
1724 | (if (and charset-language-name | |
1725 | (not | |
1726 | (equal (get-language-info language-name 'charset) | |
1727 | (get-language-info charset-language-name 'charset)))) | |
1728 | (setq language-name charset-language-name)) | |
1729 | ||
40c81f74 PE |
1730 | (when language-name |
1731 | ||
1732 | ;; Set up for this character set. This is now the right way | |
1733 | ;; to do it for both unibyte and multibyte modes. | |
1734 | (set-language-environment language-name) | |
1735 | ||
1736 | ;; If default-enable-multibyte-characters is nil, | |
1737 | ;; we are using single-byte characters, | |
1738 | ;; so the display table and terminal coding system are irrelevant. | |
1739 | (when default-enable-multibyte-characters | |
1740 | (set-display-table-and-terminal-coding-system language-name)) | |
1741 | ||
1742 | (setq locale-coding-system | |
1743 | (car (get-language-info language-name 'coding-priority)))) | |
1744 | ||
1745 | (when coding-system | |
1746 | (prefer-coding-system coding-system) | |
1747 | (setq locale-coding-system coding-system))))) | |
1748 | \f | |
4ed46869 KH |
1749 | ;;; Charset property |
1750 | ||
251d4f4b | 1751 | (defun get-charset-property (charset propname) |
4ed46869 KH |
1752 | "Return the value of CHARSET's PROPNAME property. |
1753 | This is the last value stored with | |
96db204a | 1754 | (put-charset-property CHARSET PROPNAME VALUE)." |
251d4f4b KH |
1755 | (and (not (eq charset 'composition)) |
1756 | (plist-get (charset-plist charset) propname))) | |
4ed46869 | 1757 | |
251d4f4b | 1758 | (defun put-charset-property (charset propname value) |
4ed46869 KH |
1759 | "Store CHARSETS's PROPNAME property with value VALUE. |
1760 | It can be retrieved with `(get-charset-property CHARSET PROPNAME)'." | |
1300d43f KH |
1761 | (or (eq charset 'composition) |
1762 | (set-charset-plist charset | |
1763 | (plist-put (charset-plist charset) propname value)))) | |
4ed46869 KH |
1764 | |
1765 | ;;; Character code property | |
1766 | (put 'char-code-property-table 'char-table-extra-slots 0) | |
1767 | ||
1768 | (defvar char-code-property-table | |
1769 | (make-char-table 'char-code-property-table) | |
1770 | "Char-table containing a property list of each character code. | |
1771 | ||
1772 | See also the documentation of `get-char-code-property' and | |
96db204a | 1773 | `put-char-code-property'.") |
4ed46869 KH |
1774 | |
1775 | (defun get-char-code-property (char propname) | |
1776 | "Return the value of CHAR's PROPNAME property in `char-code-property-table'." | |
1777 | (let ((plist (aref char-code-property-table char))) | |
1778 | (if (listp plist) | |
1779 | (car (cdr (memq propname plist)))))) | |
1780 | ||
1781 | (defun put-char-code-property (char propname value) | |
1782 | "Store CHAR's PROPNAME property with VALUE in `char-code-property-table'. | |
1783 | It can be retrieved with `(get-char-code-property CHAR PROPNAME)'." | |
1784 | (let ((plist (aref char-code-property-table char))) | |
1785 | (if plist | |
1786 | (let ((slot (memq propname plist))) | |
1787 | (if slot | |
1788 | (setcar (cdr slot) value) | |
1789 | (nconc plist (list propname value)))) | |
1790 | (aset char-code-property-table char (list propname value))))) | |
1791 | ||
a127b764 KH |
1792 | \f |
1793 | ;; Pretty description of encoded string | |
1794 | ||
1795 | ;; Alist of ISO 2022 control code vs the corresponding mnemonic string. | |
1796 | (defvar iso-2022-control-alist | |
1797 | '((?\x1b . "ESC") | |
1798 | (?\x0e . "SO") | |
1799 | (?\x0f . "SI") | |
1800 | (?\x8e . "SS2") | |
1801 | (?\x8f . "SS3") | |
1802 | (?\x9b . "CSI"))) | |
1803 | ||
1804 | (defun encoded-string-description (str coding-system) | |
1805 | "Return a pretty description of STR that is encoded by CODING-SYSTEM." | |
1806 | (setq str (string-as-unibyte str)) | |
1807 | (let ((char (aref str 0)) | |
1808 | desc) | |
1809 | (when (< char 128) | |
1810 | (setq desc (or (cdr (assq char iso-2022-control-alist)) | |
1811 | (char-to-string char))) | |
1812 | (let ((i 1) | |
1813 | (len (length str))) | |
1814 | (while (< i len) | |
1815 | (setq char (aref str i)) | |
1816 | (if (>= char 128) | |
1817 | (setq desc nil i len) | |
1818 | (setq desc (concat desc " " | |
1819 | (or (cdr (assq char iso-2022-control-alist)) | |
1820 | (char-to-string char))) | |
1821 | i (1+ i)))))) | |
1822 | (or desc | |
1823 | (mapconcat (function (lambda (x) (format "0x%02x" x))) str " ")))) | |
1824 | ||
1825 | (defun encode-coding-char (char coding-system) | |
1826 | "Encode CHAR by CODING-SYSTEM and return the resulting string. | |
1827 | If CODING-SYSTEM can't safely encode CHAR, return nil." | |
a127b764 KH |
1828 | (let ((str1 (char-to-string char)) |
1829 | (str2 (make-string 2 char)) | |
1830 | (safe-charsets (and coding-system | |
1831 | (coding-system-get coding-system 'safe-charsets))) | |
1832 | enc1 enc2 i1 i2) | |
1833 | (when (or (eq safe-charsets t) | |
1834 | (memq (char-charset char) safe-charsets)) | |
1835 | ;; We must find the encoded string of CHAR. But, just encoding | |
1836 | ;; CHAR will put extra control sequences (usually to designate | |
1837 | ;; ASCII charaset) at the tail if type of CODING is ISO 2022. | |
1838 | ;; To exclude such tailing bytes, we at first encode one-char | |
1839 | ;; string and two-char string, then check how many bytes at the | |
1840 | ;; tail of both encoded strings are the same. | |
1841 | ||
1842 | (setq enc1 (string-as-unibyte (encode-coding-string str1 coding-system)) | |
1843 | i1 (length enc1) | |
1844 | enc2 (string-as-unibyte (encode-coding-string str2 coding-system)) | |
1845 | i2 (length enc2)) | |
1846 | (while (and (> i1 0) (= (aref enc1 (1- i1)) (aref enc2 (1- i2)))) | |
1847 | (setq i1 (1- i1) i2 (1- i2))) | |
1848 | ||
1849 | ;; Now (substring enc1 i1) and (substring enc2 i2) are the same, | |
1850 | ;; and they are the extra control sequences at the tail to | |
1851 | ;; exclude. | |
1852 | (substring enc2 0 i2)))) | |
1853 | ||
1854 | ||
4ed46869 | 1855 | ;;; mule-cmds.el ends here |