Commit | Line | Data |
---|---|---|
ca4c9455 | 1 | /* Composite sequence support. |
aaef169d | 2 | Copyright (C) 2001, 2002, 2003, 2004, 2005, |
114f9c96 GM |
3 | 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc. |
4 | Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010 | |
ce03bf76 KH |
5 | National Institute of Advanced Industrial Science and Technology (AIST) |
6 | Registration Number H14PRO021 | |
f30d8d94 | 7 | Copyright (C) 2003, 2006 |
1527c36e KH |
8 | National Institute of Advanced Industrial Science and Technology (AIST) |
9 | Registration Number H13PRO009 | |
ca4c9455 KH |
10 | |
11 | This file is part of GNU Emacs. | |
12 | ||
9ec0b715 | 13 | GNU Emacs is free software: you can redistribute it and/or modify |
ca4c9455 | 14 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
15 | the Free Software Foundation, either version 3 of the License, or |
16 | (at your option) any later version. | |
ca4c9455 KH |
17 | |
18 | GNU Emacs is distributed in the hope that it will be useful, | |
19 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
20 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
21 | GNU General Public License for more details. | |
22 | ||
23 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 24 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
ca4c9455 KH |
25 | |
26 | #include <config.h> | |
d7306fe6 | 27 | #include <setjmp.h> |
ca4c9455 KH |
28 | #include "lisp.h" |
29 | #include "buffer.h" | |
1527c36e | 30 | #include "character.h" |
f5199465 | 31 | #include "coding.h" |
ca4c9455 | 32 | #include "intervals.h" |
58753d74 KH |
33 | #include "window.h" |
34 | #include "frame.h" | |
35 | #include "dispextern.h" | |
36 | #include "font.h" | |
f5199465 KH |
37 | #include "termhooks.h" |
38 | ||
ca4c9455 KH |
39 | |
40 | /* Emacs uses special text property `composition' to support character | |
41 | composition. A sequence of characters that have the same (i.e. eq) | |
42 | `composition' property value is treated as a single composite | |
43 | sequence (we call it just `composition' here after). Characters in | |
44 | a composition are all composed somehow on the screen. | |
45 | ||
46 | The property value has this form when the composition is made: | |
47 | ((LENGTH . COMPONENTS) . MODIFICATION-FUNC) | |
48 | then turns to this form: | |
49 | (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC)) | |
50 | when the composition is registered in composition_hash_table and | |
51 | composition_table. These rather peculiar structures were designed | |
52 | to make it easy to distinguish them quickly (we can do that by | |
53 | checking only the first element) and to extract LENGTH (from the | |
54 | former form) and COMPOSITION-ID (from the latter form). | |
55 | ||
56 | We register a composition when it is displayed, or when the width | |
57 | is required (for instance, to calculate columns). | |
58 | ||
59 | LENGTH -- Length of the composition. This information is used to | |
60 | check the validity of the composition. | |
61 | ||
62 | COMPONENTS -- Character, string, vector, list, or nil. | |
63 | ||
64 | If it is nil, characters in the text are composed relatively | |
65 | according to their metrics in font glyphs. | |
66 | ||
67 | If it is a character or a string, the character or characters | |
68 | in the string are composed relatively. | |
69 | ||
70 | If it is a vector or list of integers, the element is a | |
71 | character or an encoded composition rule. The characters are | |
72 | composed according to the rules. (2N)th elements are | |
73 | characters to be composed and (2N+1)th elements are | |
74 | composition rules to tell how to compose (2N+2)th element with | |
75 | the previously composed 2N glyphs. | |
76 | ||
77 | COMPONENTS-VEC -- Vector of integers. In relative composition, the | |
78 | elements are characters to be composed. In rule-base | |
79 | composition, the elements are characters or encoded | |
80 | composition rules. | |
81 | ||
82 | MODIFICATION-FUNC -- If non nil, it is a function to call when the | |
83 | composition gets invalid after a modification in a buffer. If | |
84 | it is nil, a function in `composition-function-table' of the | |
85 | first character in the sequence is called. | |
86 | ||
87 | COMPOSITION-ID --Identification number of the composition. It is | |
88 | used as an index to composition_table for the composition. | |
89 | ||
90 | When Emacs has to display a composition or has to know its | |
91 | displaying width, the function get_composition_id is called. It | |
92 | returns COMPOSITION-ID so that the caller can access the | |
93 | information about the composition through composition_table. If a | |
94 | COMPOSITION-ID has not yet been assigned to the composition, | |
95 | get_composition_id checks the validity of `composition' property, | |
96 | and, if valid, assigns a new ID, registers the information in | |
97 | composition_hash_table and composition_table, and changes the form | |
98 | of the property value. If the property is invalid, return -1 | |
99 | without changing the property value. | |
100 | ||
101 | We use two tables to keep information about composition; | |
102 | composition_hash_table and composition_table. | |
103 | ||
104 | The former is a hash table in which keys are COMPONENTS-VECs and | |
105 | values are the corresponding COMPOSITION-IDs. This hash table is | |
4abc7470 | 106 | weak, but as each key (COMPONENTS-VEC) is also kept as a value of the |
ca4c9455 | 107 | `composition' property, it won't be collected as garbage until all |
4abc7470 | 108 | bits of text that have the same COMPONENTS-VEC are deleted. |
ca4c9455 KH |
109 | |
110 | The latter is a table of pointers to `struct composition' indexed | |
4abc7470 | 111 | by COMPOSITION-ID. This structure keeps the other information (see |
ca4c9455 KH |
112 | composite.h). |
113 | ||
114 | In general, a text property holds information about individual | |
115 | characters. But, a `composition' property holds information about | |
4abc7470 | 116 | a sequence of characters (in this sense, it is like the `intangible' |
ca4c9455 | 117 | property). That means that we should not share the property value |
4abc7470 | 118 | in adjacent compositions -- we can't distinguish them if they have the |
ca4c9455 KH |
119 | same property. So, after any changes, we call |
120 | `update_compositions' and change a property of one of adjacent | |
121 | compositions to a copy of it. This function also runs a proper | |
122 | composition modification function to make a composition that gets | |
123 | invalid by the change valid again. | |
124 | ||
4abc7470 | 125 | As the value of the `composition' property holds information about a |
ca4c9455 | 126 | specific range of text, the value gets invalid if we change the |
4abc7470 | 127 | text in the range. We treat the `composition' property as always |
ca4c9455 KH |
128 | rear-nonsticky (currently by setting default-text-properties to |
129 | (rear-nonsticky (composition))) and we never make properties of | |
130 | adjacent compositions identical. Thus, any such changes make the | |
4abc7470 | 131 | range just shorter. So, we can check the validity of the `composition' |
ca4c9455 KH |
132 | property by comparing LENGTH information with the actual length of |
133 | the composition. | |
134 | ||
135 | */ | |
136 | ||
137 | ||
138 | Lisp_Object Qcomposition; | |
139 | ||
140 | /* Table of pointers to the structure `composition' indexed by | |
141 | COMPOSITION-ID. This structure is for storing information about | |
142 | each composition except for COMPONENTS-VEC. */ | |
143 | struct composition **composition_table; | |
144 | ||
145 | /* The current size of `composition_table'. */ | |
146 | static int composition_table_size; | |
147 | ||
148 | /* Number of compositions currently made. */ | |
149 | int n_compositions; | |
150 | ||
151 | /* Hash table for compositions. The key is COMPONENTS-VEC of | |
152 | `composition' property. The value is the corresponding | |
153 | COMPOSITION-ID. */ | |
154 | Lisp_Object composition_hash_table; | |
155 | ||
156 | /* Function to call to adjust composition. */ | |
157 | Lisp_Object Vcompose_chars_after_function; | |
158 | ||
f96ba4c1 KH |
159 | Lisp_Object Qauto_composed; |
160 | Lisp_Object Vauto_composition_function; | |
161 | Lisp_Object Qauto_composition_function; | |
58753d74 | 162 | Lisp_Object Vcomposition_function_table; |
40add26d | 163 | |
895416e3 KH |
164 | /* Maxinum number of characters to lookback to check |
165 | auto-composition. */ | |
166 | #define MAX_AUTO_COMPOSITION_LOOKBACK 3 | |
167 | ||
ea058d2c DL |
168 | EXFUN (Fremove_list_of_text_properties, 4); |
169 | ||
ca4c9455 KH |
170 | /* Temporary variable used in macros COMPOSITION_XXX. */ |
171 | Lisp_Object composition_temp; | |
f30d8d94 | 172 | |
ca4c9455 | 173 | \f |
ca4c9455 KH |
174 | /* Return COMPOSITION-ID of a composition at buffer position |
175 | CHARPOS/BYTEPOS and length NCHARS. The `composition' property of | |
176 | the sequence is PROP. STRING, if non-nil, is a string that | |
177 | contains the composition instead of the current buffer. | |
178 | ||
179 | If the composition is invalid, return -1. */ | |
180 | ||
181 | int | |
182 | get_composition_id (charpos, bytepos, nchars, prop, string) | |
183 | int charpos, bytepos, nchars; | |
184 | Lisp_Object prop, string; | |
185 | { | |
186 | Lisp_Object id, length, components, key, *key_contents; | |
187 | int glyph_len; | |
188 | struct Lisp_Hash_Table *hash_table = XHASH_TABLE (composition_hash_table); | |
189 | int hash_index; | |
190 | unsigned hash_code; | |
191 | struct composition *cmp; | |
192 | int i, ch; | |
193 | ||
194 | /* PROP should be | |
195 | Form-A: ((LENGTH . COMPONENTS) . MODIFICATION-FUNC) | |
196 | or | |
197 | Form-B: (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC)) | |
198 | */ | |
199 | if (nchars == 0 || !CONSP (prop)) | |
200 | goto invalid_composition; | |
201 | ||
202 | id = XCAR (prop); | |
203 | if (INTEGERP (id)) | |
204 | { | |
205 | /* PROP should be Form-B. */ | |
206 | if (XINT (id) < 0 || XINT (id) >= n_compositions) | |
207 | goto invalid_composition; | |
208 | return XINT (id); | |
209 | } | |
210 | ||
211 | /* PROP should be Form-A. | |
212 | Thus, ID should be (LENGTH . COMPONENTS). */ | |
213 | if (!CONSP (id)) | |
214 | goto invalid_composition; | |
215 | length = XCAR (id); | |
216 | if (!INTEGERP (length) || XINT (length) != nchars) | |
217 | goto invalid_composition; | |
218 | ||
219 | components = XCDR (id); | |
220 | ||
221 | /* Check if the same composition has already been registered or not | |
222 | by consulting composition_hash_table. The key for this table is | |
223 | COMPONENTS (converted to a vector COMPONENTS-VEC) or, if it is | |
224 | nil, vector of characters in the composition range. */ | |
225 | if (INTEGERP (components)) | |
226 | key = Fmake_vector (make_number (1), components); | |
227 | else if (STRINGP (components) || CONSP (components)) | |
228 | key = Fvconcat (1, &components); | |
229 | else if (VECTORP (components)) | |
230 | key = components; | |
231 | else if (NILP (components)) | |
232 | { | |
233 | key = Fmake_vector (make_number (nchars), Qnil); | |
234 | if (STRINGP (string)) | |
235 | for (i = 0; i < nchars; i++) | |
236 | { | |
237 | FETCH_STRING_CHAR_ADVANCE (ch, string, charpos, bytepos); | |
238 | XVECTOR (key)->contents[i] = make_number (ch); | |
239 | } | |
240 | else | |
241 | for (i = 0; i < nchars; i++) | |
242 | { | |
243 | FETCH_CHAR_ADVANCE (ch, charpos, bytepos); | |
244 | XVECTOR (key)->contents[i] = make_number (ch); | |
245 | } | |
246 | } | |
247 | else | |
248 | goto invalid_composition; | |
249 | ||
250 | hash_index = hash_lookup (hash_table, key, &hash_code); | |
251 | if (hash_index >= 0) | |
252 | { | |
253 | /* We have already registered the same composition. Change PROP | |
254 | from Form-A above to Form-B while replacing COMPONENTS with | |
255 | COMPONENTS-VEC stored in the hash table. We can directly | |
256 | modify the cons cell of PROP because it is not shared. */ | |
257 | key = HASH_KEY (hash_table, hash_index); | |
258 | id = HASH_VALUE (hash_table, hash_index); | |
f3fbd155 KR |
259 | XSETCAR (prop, id); |
260 | XSETCDR (prop, Fcons (make_number (nchars), Fcons (key, XCDR (prop)))); | |
ca4c9455 KH |
261 | return XINT (id); |
262 | } | |
263 | ||
264 | /* This composition is a new one. We must register it. */ | |
177c0ea7 | 265 | |
ca4c9455 KH |
266 | /* Check if we have sufficient memory to store this information. */ |
267 | if (composition_table_size == 0) | |
268 | { | |
269 | composition_table_size = 256; | |
270 | composition_table | |
271 | = (struct composition **) xmalloc (sizeof (composition_table[0]) | |
272 | * composition_table_size); | |
273 | } | |
274 | else if (composition_table_size <= n_compositions) | |
275 | { | |
276 | composition_table_size += 256; | |
277 | composition_table | |
278 | = (struct composition **) xrealloc (composition_table, | |
279 | sizeof (composition_table[0]) | |
280 | * composition_table_size); | |
281 | } | |
282 | ||
283 | key_contents = XVECTOR (key)->contents; | |
284 | ||
285 | /* Check if the contents of COMPONENTS are valid if COMPONENTS is a | |
286 | vector or a list. It should be a sequence of: | |
287 | char1 rule1 char2 rule2 char3 ... ruleN charN+1 */ | |
f30d8d94 | 288 | |
dd5e1ed4 | 289 | if (VECTORP (components) |
f30d8d94 KH |
290 | && ASIZE (components) >= 2 |
291 | && VECTORP (AREF (components, 0))) | |
292 | { | |
293 | /* COMPONENTS is a glyph-string. */ | |
294 | int len = ASIZE (key); | |
295 | ||
296 | for (i = 1; i < len; i++) | |
297 | if (! VECTORP (AREF (key, i))) | |
298 | goto invalid_composition; | |
299 | } | |
dd5e1ed4 | 300 | else if (VECTORP (components) || CONSP (components)) |
ca4c9455 KH |
301 | { |
302 | int len = XVECTOR (key)->size; | |
303 | ||
304 | /* The number of elements should be odd. */ | |
305 | if ((len % 2) == 0) | |
306 | goto invalid_composition; | |
307 | /* All elements should be integers (character or encoded | |
308 | composition rule). */ | |
309 | for (i = 0; i < len; i++) | |
310 | { | |
311 | if (!INTEGERP (key_contents[i])) | |
312 | goto invalid_composition; | |
313 | } | |
314 | } | |
315 | ||
316 | /* Change PROP from Form-A above to Form-B. We can directly modify | |
317 | the cons cell of PROP because it is not shared. */ | |
318 | XSETFASTINT (id, n_compositions); | |
f3fbd155 KR |
319 | XSETCAR (prop, id); |
320 | XSETCDR (prop, Fcons (make_number (nchars), Fcons (key, XCDR (prop)))); | |
ca4c9455 KH |
321 | |
322 | /* Register the composition in composition_hash_table. */ | |
323 | hash_index = hash_put (hash_table, key, id, hash_code); | |
324 | ||
325 | /* Register the composition in composition_table. */ | |
326 | cmp = (struct composition *) xmalloc (sizeof (struct composition)); | |
327 | ||
328 | cmp->method = (NILP (components) | |
329 | ? COMPOSITION_RELATIVE | |
330 | : ((INTEGERP (components) || STRINGP (components)) | |
331 | ? COMPOSITION_WITH_ALTCHARS | |
332 | : COMPOSITION_WITH_RULE_ALTCHARS)); | |
333 | cmp->hash_index = hash_index; | |
334 | glyph_len = (cmp->method == COMPOSITION_WITH_RULE_ALTCHARS | |
335 | ? (XVECTOR (key)->size + 1) / 2 | |
336 | : XVECTOR (key)->size); | |
337 | cmp->glyph_len = glyph_len; | |
338 | cmp->offsets = (short *) xmalloc (sizeof (short) * glyph_len * 2); | |
339 | cmp->font = NULL; | |
340 | ||
58753d74 | 341 | if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS) |
ca4c9455 KH |
342 | { |
343 | /* Relative composition. */ | |
344 | cmp->width = 0; | |
345 | for (i = 0; i < glyph_len; i++) | |
346 | { | |
347 | int this_width; | |
348 | ch = XINT (key_contents[i]); | |
8e86803c | 349 | this_width = (ch == '\t' ? 1 : CHAR_WIDTH (ch)); |
ca4c9455 KH |
350 | if (cmp->width < this_width) |
351 | cmp->width = this_width; | |
352 | } | |
353 | } | |
354 | else | |
355 | { | |
356 | /* Rule-base composition. */ | |
357 | float leftmost = 0.0, rightmost; | |
358 | ||
359 | ch = XINT (key_contents[0]); | |
69df789a | 360 | rightmost = ch != '\t' ? CHAR_WIDTH (ch) : 1; |
ca4c9455 KH |
361 | |
362 | for (i = 1; i < glyph_len; i += 2) | |
363 | { | |
464f8566 | 364 | int rule, gref, nref, xoff, yoff; |
ca4c9455 KH |
365 | int this_width; |
366 | float this_left; | |
367 | ||
368 | rule = XINT (key_contents[i]); | |
369 | ch = XINT (key_contents[i + 1]); | |
69df789a | 370 | this_width = ch != '\t' ? CHAR_WIDTH (ch) : 1; |
ca4c9455 KH |
371 | |
372 | /* A composition rule is specified by an integer value | |
373 | that encodes global and new reference points (GREF and | |
374 | NREF). GREF and NREF are specified by numbers as | |
375 | below: | |
376 | 0---1---2 -- ascent | |
377 | | | | |
378 | | | | |
379 | | | | |
380 | 9--10--11 -- center | |
381 | | | | |
382 | ---3---4---5--- baseline | |
383 | | | | |
384 | 6---7---8 -- descent | |
385 | */ | |
464f8566 | 386 | COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff); |
ca4c9455 KH |
387 | this_left = (leftmost |
388 | + (gref % 3) * (rightmost - leftmost) / 2.0 | |
389 | - (nref % 3) * this_width / 2.0); | |
390 | ||
391 | if (this_left < leftmost) | |
392 | leftmost = this_left; | |
393 | if (this_left + this_width > rightmost) | |
394 | rightmost = this_left + this_width; | |
395 | } | |
396 | ||
397 | cmp->width = rightmost - leftmost; | |
398 | if (cmp->width < (rightmost - leftmost)) | |
399 | /* To get a ceiling integer value. */ | |
400 | cmp->width++; | |
401 | } | |
402 | ||
403 | composition_table[n_compositions] = cmp; | |
404 | ||
405 | return n_compositions++; | |
406 | ||
407 | invalid_composition: | |
408 | /* Would it be better to remove this `composition' property? */ | |
409 | return -1; | |
410 | } | |
411 | ||
412 | \f | |
90b3fe91 KH |
413 | /* Find a static composition at or nearest to position POS of OBJECT |
414 | (buffer or string). | |
ca4c9455 KH |
415 | |
416 | OBJECT defaults to the current buffer. If there's a composition at | |
417 | POS, set *START and *END to the start and end of the sequence, | |
418 | *PROP to the `composition' property, and return 1. | |
419 | ||
420 | If there's no composition at POS and LIMIT is negative, return 0. | |
421 | ||
422 | Otherwise, search for a composition forward (LIMIT > POS) or | |
423 | backward (LIMIT < POS). In this case, LIMIT bounds the search. | |
424 | ||
425 | If a composition is found, set *START, *END, and *PROP as above, | |
426 | and return 1, else return 0. | |
427 | ||
428 | This doesn't check the validity of composition. */ | |
429 | ||
430 | int | |
431 | find_composition (pos, limit, start, end, prop, object) | |
aaefca97 DL |
432 | int pos, limit; |
433 | EMACS_INT *start, *end; | |
ca4c9455 KH |
434 | Lisp_Object *prop, object; |
435 | { | |
436 | Lisp_Object val; | |
437 | ||
438 | if (get_property_and_range (pos, Qcomposition, prop, start, end, object)) | |
439 | return 1; | |
440 | ||
441 | if (limit < 0 || limit == pos) | |
442 | return 0; | |
443 | ||
444 | if (limit > pos) /* search forward */ | |
d279f620 KH |
445 | { |
446 | val = Fnext_single_property_change (make_number (pos), Qcomposition, | |
447 | object, make_number (limit)); | |
448 | pos = XINT (val); | |
449 | if (pos == limit) | |
450 | return 0; | |
451 | } | |
ca4c9455 | 452 | else /* search backward */ |
d279f620 KH |
453 | { |
454 | if (get_property_and_range (pos - 1, Qcomposition, prop, start, end, | |
455 | object)) | |
456 | return 1; | |
457 | val = Fprevious_single_property_change (make_number (pos), Qcomposition, | |
458 | object, make_number (limit)); | |
459 | pos = XINT (val); | |
460 | if (pos == limit) | |
461 | return 0; | |
462 | pos--; | |
463 | } | |
ca4c9455 KH |
464 | get_property_and_range (pos, Qcomposition, prop, start, end, object); |
465 | return 1; | |
466 | } | |
467 | ||
468 | /* Run a proper function to adjust the composition sitting between | |
469 | FROM and TO with property PROP. */ | |
470 | ||
471 | static void | |
472 | run_composition_function (from, to, prop) | |
473 | int from, to; | |
474 | Lisp_Object prop; | |
475 | { | |
7d019510 | 476 | Lisp_Object func; |
aaefca97 | 477 | EMACS_INT start, end; |
ca4c9455 KH |
478 | |
479 | func = COMPOSITION_MODIFICATION_FUNC (prop); | |
480 | /* If an invalid composition precedes or follows, try to make them | |
481 | valid too. */ | |
482 | if (from > BEGV | |
483 | && find_composition (from - 1, -1, &start, &end, &prop, Qnil) | |
484 | && !COMPOSITION_VALID_P (start, end, prop)) | |
485 | from = start; | |
486 | if (to < ZV | |
487 | && find_composition (to, -1, &start, &end, &prop, Qnil) | |
488 | && !COMPOSITION_VALID_P (start, end, prop)) | |
489 | to = end; | |
775b3d2d | 490 | if (!NILP (Ffboundp (func))) |
ca4c9455 | 491 | call2 (func, make_number (from), make_number (to)); |
ca4c9455 KH |
492 | } |
493 | ||
494 | /* Make invalid compositions adjacent to or inside FROM and TO valid. | |
495 | CHECK_MASK is bitwise `or' of mask bits defined by macros | |
496 | CHECK_XXX (see the comment in composite.h). | |
497 | ||
b418f8a6 | 498 | It also resets the text-property `auto-composed' to a proper region |
f96ba4c1 KH |
499 | so that automatic character composition works correctly later while |
500 | displaying the region. | |
8f924df7 | 501 | |
ca4c9455 KH |
502 | This function is called when a buffer text is changed. If the |
503 | change is deletion, FROM == TO. Otherwise, FROM < TO. */ | |
504 | ||
505 | void | |
506 | update_compositions (from, to, check_mask) | |
aaefca97 DL |
507 | EMACS_INT from, to; |
508 | int check_mask; | |
ca4c9455 | 509 | { |
7d019510 | 510 | Lisp_Object prop; |
aaefca97 | 511 | EMACS_INT start, end; |
f96ba4c1 KH |
512 | /* The beginning and end of the region to set the property |
513 | `auto-composed' to nil. */ | |
aaefca97 | 514 | EMACS_INT min_pos = from, max_pos = to; |
ca4c9455 | 515 | |
6c1aa7f1 GM |
516 | if (inhibit_modification_hooks) |
517 | return; | |
177c0ea7 | 518 | |
d3f40cbd KH |
519 | /* If FROM and TO are not in a valid range, do nothing. */ |
520 | if (! (BEGV <= from && from <= to && to <= ZV)) | |
521 | return; | |
522 | ||
ca4c9455 KH |
523 | if (check_mask & CHECK_HEAD) |
524 | { | |
525 | /* FROM should be at composition boundary. But, insertion or | |
526 | deletion will make two compositions adjacent and | |
527 | indistinguishable when they have same (eq) property. To | |
528 | avoid it, in such a case, we change the property of the | |
529 | latter to the copy of it. */ | |
530 | if (from > BEGV | |
553d3164 KH |
531 | && find_composition (from - 1, -1, &start, &end, &prop, Qnil) |
532 | && COMPOSITION_VALID_P (start, end, prop)) | |
ca4c9455 | 533 | { |
f96ba4c1 KH |
534 | min_pos = start; |
535 | if (end > to) | |
536 | max_pos = end; | |
ca4c9455 KH |
537 | if (from < end) |
538 | Fput_text_property (make_number (from), make_number (end), | |
539 | Qcomposition, | |
540 | Fcons (XCAR (prop), XCDR (prop)), Qnil); | |
541 | run_composition_function (start, end, prop); | |
542 | from = end; | |
543 | } | |
dd33cc56 | 544 | else if (from < ZV |
553d3164 | 545 | && find_composition (from, -1, &start, &from, &prop, Qnil) |
9657d668 | 546 | && COMPOSITION_VALID_P (start, from, prop)) |
f96ba4c1 KH |
547 | { |
548 | if (from > to) | |
549 | max_pos = from; | |
550 | run_composition_function (start, from, prop); | |
551 | } | |
ca4c9455 KH |
552 | } |
553 | ||
554 | if (check_mask & CHECK_INSIDE) | |
555 | { | |
556 | /* In this case, we are sure that (check & CHECK_TAIL) is also | |
557 | nonzero. Thus, here we should check only compositions before | |
558 | (to - 1). */ | |
559 | while (from < to - 1 | |
560 | && find_composition (from, to, &start, &from, &prop, Qnil) | |
9657d668 | 561 | && COMPOSITION_VALID_P (start, from, prop) |
ca4c9455 KH |
562 | && from < to - 1) |
563 | run_composition_function (start, from, prop); | |
564 | } | |
565 | ||
566 | if (check_mask & CHECK_TAIL) | |
567 | { | |
568 | if (from < to | |
553d3164 KH |
569 | && find_composition (to - 1, -1, &start, &end, &prop, Qnil) |
570 | && COMPOSITION_VALID_P (start, end, prop)) | |
ca4c9455 KH |
571 | { |
572 | /* TO should be also at composition boundary. But, | |
573 | insertion or deletion will make two compositions adjacent | |
574 | and indistinguishable when they have same (eq) property. | |
575 | To avoid it, in such a case, we change the property of | |
576 | the former to the copy of it. */ | |
577 | if (to < end) | |
f96ba4c1 KH |
578 | { |
579 | Fput_text_property (make_number (start), make_number (to), | |
580 | Qcomposition, | |
581 | Fcons (XCAR (prop), XCDR (prop)), Qnil); | |
582 | max_pos = end; | |
583 | } | |
ca4c9455 KH |
584 | run_composition_function (start, end, prop); |
585 | } | |
586 | else if (to < ZV | |
553d3164 KH |
587 | && find_composition (to, -1, &start, &end, &prop, Qnil) |
588 | && COMPOSITION_VALID_P (start, end, prop)) | |
f96ba4c1 KH |
589 | { |
590 | run_composition_function (start, end, prop); | |
591 | max_pos = end; | |
592 | } | |
ca4c9455 | 593 | } |
f96ba4c1 | 594 | if (min_pos < max_pos) |
9d440521 KH |
595 | { |
596 | int count = SPECPDL_INDEX (); | |
597 | ||
598 | specbind (Qinhibit_read_only, Qt); | |
599 | specbind (Qinhibit_modification_hooks, Qt); | |
600 | specbind (Qinhibit_point_motion_hooks, Qt); | |
601 | Fremove_list_of_text_properties (make_number (min_pos), | |
602 | make_number (max_pos), | |
603 | Fcons (Qauto_composed, Qnil), Qnil); | |
604 | unbind_to (count, Qnil); | |
605 | } | |
ca4c9455 KH |
606 | } |
607 | ||
c1361885 KH |
608 | |
609 | /* Modify composition property values in LIST destructively. LIST is | |
610 | a list as returned from text_property_list. Change values to the | |
611 | top-level copies of them so that none of them are `eq'. */ | |
612 | ||
613 | void | |
614 | make_composition_value_copy (list) | |
615 | Lisp_Object list; | |
616 | { | |
617 | Lisp_Object plist, val; | |
618 | ||
619 | for (; CONSP (list); list = XCDR (list)) | |
620 | { | |
621 | plist = XCAR (XCDR (XCDR (XCAR (list)))); | |
622 | while (CONSP (plist) && CONSP (XCDR (plist))) | |
623 | { | |
624 | if (EQ (XCAR (plist), Qcomposition) | |
625 | && (val = XCAR (XCDR (plist)), CONSP (val))) | |
f3fbd155 | 626 | XSETCAR (XCDR (plist), Fcons (XCAR (val), XCDR (val))); |
c1361885 KH |
627 | plist = XCDR (XCDR (plist)); |
628 | } | |
629 | } | |
630 | } | |
631 | ||
632 | ||
ca4c9455 KH |
633 | /* Make text in the region between START and END a composition that |
634 | has COMPONENTS and MODIFICATION-FUNC. | |
635 | ||
636 | If STRING is non-nil, then operate on characters contained between | |
637 | indices START and END in STRING. */ | |
638 | ||
639 | void | |
640 | compose_text (start, end, components, modification_func, string) | |
641 | int start, end; | |
642 | Lisp_Object components, modification_func, string; | |
643 | { | |
644 | Lisp_Object prop; | |
645 | ||
646 | prop = Fcons (Fcons (make_number (end - start), components), | |
647 | modification_func); | |
648 | Fput_text_property (make_number (start), make_number (end), | |
649 | Qcomposition, prop, string); | |
650 | } | |
58753d74 KH |
651 | |
652 | ||
653 | static Lisp_Object autocmp_chars P_ ((Lisp_Object, EMACS_INT, EMACS_INT, | |
654 | EMACS_INT, struct window *, | |
655 | struct face *, Lisp_Object)); | |
656 | ||
657 | \f | |
658 | /* Lisp glyph-string handlers */ | |
659 | ||
660 | /* Hash table for automatic composition. The key is a header of a | |
661 | lgstring (Lispy glyph-string), and the value is a body of a | |
662 | lgstring. */ | |
663 | ||
664 | static Lisp_Object gstring_hash_table; | |
665 | ||
666 | static Lisp_Object gstring_lookup_cache P_ ((Lisp_Object)); | |
667 | ||
668 | static Lisp_Object | |
669 | gstring_lookup_cache (header) | |
670 | Lisp_Object header; | |
671 | { | |
672 | struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table); | |
673 | int i = hash_lookup (h, header, NULL); | |
674 | ||
675 | return (i >= 0 ? HASH_VALUE (h, i) : Qnil); | |
676 | } | |
677 | ||
678 | Lisp_Object | |
679 | composition_gstring_put_cache (gstring, len) | |
680 | Lisp_Object gstring; | |
681 | int len; | |
682 | { | |
683 | struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table); | |
684 | unsigned hash; | |
685 | Lisp_Object header, copy; | |
686 | int i; | |
687 | ||
688 | header = LGSTRING_HEADER (gstring); | |
689 | hash = h->hashfn (h, header); | |
690 | if (len < 0) | |
691 | { | |
692 | len = LGSTRING_GLYPH_LEN (gstring); | |
693 | for (i = 0; i < len; i++) | |
694 | if (NILP (LGSTRING_GLYPH (gstring, i))) | |
695 | break; | |
696 | len = i; | |
697 | } | |
087e2ea9 | 698 | |
58753d74 KH |
699 | copy = Fmake_vector (make_number (len + 2), Qnil); |
700 | LGSTRING_SET_HEADER (copy, Fcopy_sequence (header)); | |
701 | for (i = 0; i < len; i++) | |
702 | LGSTRING_SET_GLYPH (copy, i, Fcopy_sequence (LGSTRING_GLYPH (gstring, i))); | |
703 | i = hash_put (h, LGSTRING_HEADER (copy), copy, hash); | |
704 | LGSTRING_SET_ID (copy, make_number (i)); | |
705 | return copy; | |
706 | } | |
707 | ||
708 | Lisp_Object | |
709 | composition_gstring_from_id (id) | |
710 | int id; | |
711 | { | |
712 | struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table); | |
713 | ||
714 | return HASH_VALUE (h, id); | |
715 | } | |
716 | ||
717 | static Lisp_Object fill_gstring_header P_ ((Lisp_Object, Lisp_Object, | |
718 | Lisp_Object, Lisp_Object, | |
719 | Lisp_Object)); | |
720 | ||
721 | int | |
722 | composition_gstring_p (gstring) | |
723 | Lisp_Object gstring; | |
724 | { | |
725 | Lisp_Object header; | |
726 | int i; | |
727 | ||
728 | if (! VECTORP (gstring) || ASIZE (gstring) < 2) | |
729 | return 0; | |
730 | header = LGSTRING_HEADER (gstring); | |
731 | if (! VECTORP (header) || ASIZE (header) < 2) | |
732 | return 0; | |
733 | if (! NILP (LGSTRING_FONT (gstring)) | |
f5199465 KH |
734 | && (! FONT_OBJECT_P (LGSTRING_FONT (gstring)) |
735 | && ! CODING_SYSTEM_P (LGSTRING_FONT (gstring)))) | |
58753d74 KH |
736 | return 0; |
737 | for (i = 1; i < ASIZE (LGSTRING_HEADER (gstring)); i++) | |
738 | if (! NATNUMP (AREF (LGSTRING_HEADER (gstring), i))) | |
739 | return 0; | |
740 | if (! NILP (LGSTRING_ID (gstring)) && ! NATNUMP (LGSTRING_ID (gstring))) | |
741 | return 0; | |
742 | for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++) | |
743 | { | |
744 | Lisp_Object glyph = LGSTRING_GLYPH (gstring, i); | |
745 | if (NILP (glyph)) | |
746 | break; | |
747 | if (! VECTORP (glyph) || ASIZE (glyph) != LGLYPH_SIZE) | |
748 | return 0; | |
749 | } | |
750 | return 1; | |
751 | } | |
752 | ||
753 | int | |
754 | composition_gstring_width (gstring, from, to, metrics) | |
755 | Lisp_Object gstring; | |
756 | int from, to; | |
757 | struct font_metrics *metrics; | |
758 | { | |
759 | Lisp_Object *glyph; | |
760 | int width = 0; | |
761 | ||
762 | if (metrics) | |
763 | { | |
764 | Lisp_Object font_object = LGSTRING_FONT (gstring); | |
58753d74 | 765 | |
f5199465 KH |
766 | if (FONT_OBJECT_P (font_object)) |
767 | { | |
768 | struct font *font = XFONT_OBJECT (font_object); | |
769 | ||
770 | metrics->ascent = font->ascent; | |
771 | metrics->descent = font->descent; | |
772 | } | |
773 | else | |
774 | { | |
775 | metrics->ascent = 1; | |
776 | metrics->descent = 0; | |
777 | } | |
58753d74 KH |
778 | metrics->width = metrics->lbearing = metrics->rbearing = 0; |
779 | } | |
780 | for (glyph = &LGSTRING_GLYPH (gstring, from); from < to; from++, glyph++) | |
781 | { | |
782 | int x; | |
783 | ||
784 | if (NILP (LGLYPH_ADJUSTMENT (*glyph))) | |
785 | width += LGLYPH_WIDTH (*glyph); | |
786 | else | |
787 | width += LGLYPH_WADJUST (*glyph); | |
788 | if (metrics) | |
789 | { | |
790 | x = metrics->width + LGLYPH_LBEARING (*glyph) + LGLYPH_XOFF (*glyph); | |
791 | if (metrics->lbearing > x) | |
792 | metrics->lbearing = x; | |
793 | x = metrics->width + LGLYPH_RBEARING (*glyph) + LGLYPH_XOFF (*glyph); | |
794 | if (metrics->rbearing < x) | |
795 | metrics->rbearing = x; | |
796 | metrics->width = width; | |
797 | x = LGLYPH_ASCENT (*glyph) - LGLYPH_YOFF (*glyph); | |
798 | if (metrics->ascent < x) | |
799 | metrics->ascent = x; | |
895416e3 | 800 | x = LGLYPH_DESCENT (*glyph) + LGLYPH_YOFF (*glyph); |
58753d74 KH |
801 | if (metrics->descent < x) |
802 | metrics->descent = x; | |
803 | } | |
804 | } | |
805 | return width; | |
806 | } | |
807 | ||
808 | ||
809 | static Lisp_Object gstring_work; | |
810 | static Lisp_Object gstring_work_headers; | |
811 | ||
812 | static Lisp_Object | |
813 | fill_gstring_header (header, start, end, font_object, string) | |
814 | Lisp_Object header, start, end, font_object, string; | |
815 | { | |
816 | EMACS_INT from, to, from_byte; | |
817 | EMACS_INT len, i; | |
818 | ||
819 | if (NILP (string)) | |
820 | { | |
821 | if (NILP (current_buffer->enable_multibyte_characters)) | |
822 | error ("Attempt to shape unibyte text"); | |
823 | validate_region (&start, &end); | |
824 | from = XFASTINT (start); | |
825 | to = XFASTINT (end); | |
826 | from_byte = CHAR_TO_BYTE (from); | |
827 | } | |
828 | else | |
829 | { | |
830 | CHECK_STRING (string); | |
793ffee8 | 831 | if (! STRING_MULTIBYTE (string)) |
58753d74 | 832 | error ("Attempt to shape unibyte text"); |
ea8ba975 | 833 | /* FROM and TO are checked by the caller. */ |
58753d74 | 834 | from = XINT (start); |
58753d74 KH |
835 | to = XINT (end); |
836 | if (from < 0 || from > to || to > SCHARS (string)) | |
837 | args_out_of_range_3 (string, start, end); | |
838 | from_byte = string_char_to_byte (string, from); | |
839 | } | |
840 | ||
841 | len = to - from; | |
842 | if (len == 0) | |
843 | error ("Attempt to shape zero-length text"); | |
844 | if (VECTORP (header)) | |
845 | { | |
846 | if (ASIZE (header) != len + 1) | |
847 | args_out_of_range (header, make_number (len + 1)); | |
848 | } | |
849 | else | |
850 | { | |
851 | if (len <= 8) | |
852 | header = AREF (gstring_work_headers, len - 1); | |
853 | else | |
854 | header = Fmake_vector (make_number (len + 1), Qnil); | |
855 | } | |
856 | ||
857 | ASET (header, 0, font_object); | |
858 | for (i = 0; i < len; i++) | |
859 | { | |
860 | int c; | |
861 | ||
862 | if (NILP (string)) | |
863 | FETCH_CHAR_ADVANCE_NO_CHECK (c, from, from_byte); | |
864 | else | |
865 | FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, from, from_byte); | |
866 | ASET (header, i + 1, make_number (c)); | |
867 | } | |
868 | return header; | |
869 | } | |
870 | ||
871 | extern void font_fill_lglyph_metrics P_ ((Lisp_Object, Lisp_Object)); | |
872 | ||
873 | static void | |
874 | fill_gstring_body (gstring) | |
875 | Lisp_Object gstring; | |
876 | { | |
877 | Lisp_Object font_object = LGSTRING_FONT (gstring); | |
878 | Lisp_Object header = AREF (gstring, 0); | |
879 | EMACS_INT len = LGSTRING_CHAR_LEN (gstring); | |
880 | EMACS_INT i; | |
881 | ||
882 | for (i = 0; i < len; i++) | |
883 | { | |
884 | Lisp_Object g = LGSTRING_GLYPH (gstring, i); | |
77fa4db2 | 885 | EMACS_INT c = XINT (AREF (header, i + 1)); |
58753d74 KH |
886 | |
887 | if (NILP (g)) | |
888 | { | |
889 | g = LGLYPH_NEW (); | |
890 | LGSTRING_SET_GLYPH (gstring, i, g); | |
891 | } | |
892 | LGLYPH_SET_FROM (g, i); | |
893 | LGLYPH_SET_TO (g, i); | |
894 | LGLYPH_SET_CHAR (g, c); | |
f5199465 | 895 | if (FONT_OBJECT_P (font_object)) |
58753d74 KH |
896 | { |
897 | font_fill_lglyph_metrics (g, font_object); | |
898 | } | |
899 | else | |
900 | { | |
901 | int width = XFASTINT (CHAR_TABLE_REF (Vchar_width_table, c)); | |
902 | ||
903 | LGLYPH_SET_CODE (g, c); | |
904 | LGLYPH_SET_LBEARING (g, 0); | |
905 | LGLYPH_SET_RBEARING (g, width); | |
906 | LGLYPH_SET_WIDTH (g, width); | |
907 | LGLYPH_SET_ASCENT (g, 1); | |
908 | LGLYPH_SET_DESCENT (g, 0); | |
909 | } | |
910 | LGLYPH_SET_ADJUSTMENT (g, Qnil); | |
911 | } | |
912 | if (i < LGSTRING_GLYPH_LEN (gstring)) | |
913 | LGSTRING_SET_GLYPH (gstring, i, Qnil); | |
914 | } | |
915 | ||
58753d74 KH |
916 | |
917 | /* Try to compose the characters at CHARPOS according to CFT_ELEMENT | |
087e2ea9 | 918 | which is an element of composition-function-table (which see). |
58753d74 KH |
919 | LIMIT limits the characters to compose. STRING, if not nil, is a |
920 | target string. WIN is a window where the characters are being | |
921 | displayed. */ | |
922 | ||
923 | static Lisp_Object | |
924 | autocmp_chars (cft_element, charpos, bytepos, limit, win, face, string) | |
925 | Lisp_Object cft_element; | |
926 | EMACS_INT charpos, bytepos, limit; | |
927 | struct window *win; | |
928 | struct face *face; | |
929 | Lisp_Object string; | |
930 | { | |
931 | int count = SPECPDL_INDEX (); | |
932 | FRAME_PTR f = XFRAME (win->frame); | |
933 | Lisp_Object pos = make_number (charpos); | |
934 | EMACS_INT pt = PT, pt_byte = PT_BYTE; | |
90b3fe91 | 935 | int lookback; |
087e2ea9 | 936 | |
58753d74 | 937 | record_unwind_save_match_data (); |
90b3fe91 | 938 | for (lookback = -1; CONSP (cft_element); cft_element = XCDR (cft_element)) |
58753d74 KH |
939 | { |
940 | Lisp_Object elt = XCAR (cft_element); | |
941 | Lisp_Object re; | |
942 | Lisp_Object font_object = Qnil, gstring; | |
895416e3 | 943 | EMACS_INT len, to; |
58753d74 KH |
944 | |
945 | if (! VECTORP (elt) || ASIZE (elt) != 3) | |
946 | continue; | |
90b3fe91 | 947 | if (lookback < 0) |
895416e3 KH |
948 | { |
949 | lookback = XFASTINT (AREF (elt, 1)); | |
950 | if (limit > charpos + MAX_COMPOSITION_COMPONENTS) | |
951 | limit = charpos + MAX_COMPOSITION_COMPONENTS; | |
952 | } | |
90b3fe91 KH |
953 | else if (lookback != XFASTINT (AREF (elt, 1))) |
954 | break; | |
58753d74 | 955 | re = AREF (elt, 0); |
895416e3 KH |
956 | if (NILP (re)) |
957 | len = 1; | |
958 | else if ((len = fast_looking_at (re, charpos, bytepos, limit, -1, string)) | |
959 | > 0) | |
58753d74 | 960 | { |
895416e3 KH |
961 | if (NILP (string)) |
962 | len = BYTE_TO_CHAR (bytepos + len) - charpos; | |
963 | else | |
964 | len = string_byte_to_char (string, bytepos + len) - charpos; | |
965 | } | |
966 | if (len > 0) | |
967 | { | |
968 | limit = to = charpos + len; | |
58753d74 KH |
969 | #ifdef HAVE_WINDOW_SYSTEM |
970 | if (FRAME_WINDOW_P (f)) | |
971 | { | |
972 | font_object = font_range (charpos, &to, win, face, string); | |
895416e3 KH |
973 | if (! FONT_OBJECT_P (font_object) |
974 | || (! NILP (re) | |
975 | && to < limit | |
976 | && (fast_looking_at (re, charpos, bytepos, to, -1, string) <= 0))) | |
58753d74 KH |
977 | { |
978 | if (NILP (string)) | |
979 | TEMP_SET_PT_BOTH (pt, pt_byte); | |
980 | return unbind_to (count, Qnil); | |
981 | } | |
982 | } | |
f5199465 | 983 | else |
58753d74 | 984 | #endif /* not HAVE_WINDOW_SYSTEM */ |
f5199465 | 985 | font_object = win->frame; |
58753d74 KH |
986 | gstring = Fcomposition_get_gstring (pos, make_number (to), |
987 | font_object, string); | |
988 | if (NILP (LGSTRING_ID (gstring))) | |
989 | { | |
990 | Lisp_Object args[6]; | |
991 | ||
992 | args[0] = Vauto_composition_function; | |
993 | args[1] = AREF (elt, 2); | |
994 | args[2] = pos; | |
995 | args[3] = make_number (to); | |
996 | args[4] = font_object; | |
997 | args[5] = string; | |
998 | gstring = safe_call (6, args); | |
999 | } | |
1000 | if (NILP (string)) | |
1001 | TEMP_SET_PT_BOTH (pt, pt_byte); | |
1002 | return unbind_to (count, gstring); | |
1003 | } | |
1004 | } | |
1005 | if (NILP (string)) | |
1006 | TEMP_SET_PT_BOTH (pt, pt_byte); | |
1007 | return unbind_to (count, Qnil); | |
1008 | } | |
1009 | ||
1010 | ||
1011 | /* Update cmp_it->stop_pos to the next position after CHARPOS (and | |
1012 | BYTEPOS) where character composition may happen. If BYTEPOS is | |
1013 | negative, compoute it. If it is a static composition, set | |
1014 | cmp_it->ch to -1. Otherwise, set cmp_it->ch to the character that | |
1015 | triggers a automatic composition. */ | |
1016 | ||
1017 | void | |
1018 | composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string) | |
1019 | struct composition_it *cmp_it; | |
1020 | EMACS_INT charpos, bytepos, endpos; | |
1021 | Lisp_Object string; | |
1022 | { | |
1023 | EMACS_INT start, end, c; | |
1024 | Lisp_Object prop, val; | |
3ffdafce KH |
1025 | /* This is from forward_to_next_line_start in xdisp.c. */ |
1026 | const int MAX_NEWLINE_DISTANCE = 500; | |
58753d74 | 1027 | |
3ffdafce KH |
1028 | if (endpos > charpos + MAX_NEWLINE_DISTANCE) |
1029 | endpos = charpos + MAX_NEWLINE_DISTANCE; | |
58753d74 | 1030 | cmp_it->stop_pos = endpos; |
053ca52b | 1031 | cmp_it->id = -1; |
44566dc7 | 1032 | cmp_it->ch = -2; |
58753d74 KH |
1033 | if (find_composition (charpos, endpos, &start, &end, &prop, string) |
1034 | && COMPOSITION_VALID_P (start, end, prop)) | |
1035 | { | |
1036 | cmp_it->stop_pos = endpos = start; | |
1037 | cmp_it->ch = -1; | |
1038 | } | |
f5199465 KH |
1039 | if (NILP (string) && PT > charpos && PT < endpos) |
1040 | cmp_it->stop_pos = PT; | |
58753d74 KH |
1041 | if (NILP (current_buffer->enable_multibyte_characters) |
1042 | || ! FUNCTIONP (Vauto_composition_function)) | |
1043 | return; | |
1044 | if (bytepos < 0) | |
1045 | { | |
1046 | if (STRINGP (string)) | |
1047 | bytepos = string_char_to_byte (string, charpos); | |
1048 | else | |
1049 | bytepos = CHAR_TO_BYTE (charpos); | |
1050 | } | |
1051 | ||
1052 | start = charpos; | |
1053 | while (charpos < endpos) | |
1054 | { | |
1055 | if (STRINGP (string)) | |
1056 | FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos); | |
1057 | else | |
1058 | FETCH_CHAR_ADVANCE (c, charpos, bytepos); | |
3ffdafce | 1059 | if (c == '\n') |
ea8ba975 KH |
1060 | { |
1061 | cmp_it->ch = -2; | |
1062 | break; | |
1063 | } | |
58753d74 KH |
1064 | val = CHAR_TABLE_REF (Vcomposition_function_table, c); |
1065 | if (! NILP (val)) | |
1066 | { | |
1067 | Lisp_Object elt; | |
1068 | ||
1069 | for (; CONSP (val); val = XCDR (val)) | |
1070 | { | |
1071 | elt = XCAR (val); | |
1072 | if (VECTORP (elt) && ASIZE (elt) == 3 && NATNUMP (AREF (elt, 1)) | |
1073 | && charpos - 1 - XFASTINT (AREF (elt, 1)) >= start) | |
1074 | break; | |
1075 | } | |
1076 | if (CONSP (val)) | |
1077 | { | |
90b3fe91 KH |
1078 | cmp_it->lookback = XFASTINT (AREF (elt, 1)); |
1079 | cmp_it->stop_pos = charpos - 1 - cmp_it->lookback; | |
58753d74 | 1080 | cmp_it->ch = c; |
44566dc7 | 1081 | return; |
58753d74 KH |
1082 | } |
1083 | } | |
1084 | } | |
44566dc7 | 1085 | cmp_it->stop_pos = charpos; |
58753d74 KH |
1086 | } |
1087 | ||
1088 | /* Check if the character at CHARPOS (and BYTEPOS) is composed | |
22e33406 | 1089 | (possibly with the following characters) on window W. ENDPOS limits |
58753d74 KH |
1090 | characters to be composed. FACE, in non-NULL, is a base face of |
1091 | the character. If STRING is not nil, it is a string containing the | |
1092 | character to check, and CHARPOS and BYTEPOS are indices in the | |
1093 | string. In that case, FACE must not be NULL. | |
1094 | ||
1095 | If the character is composed, setup members of CMP_IT (id, nglyphs, | |
1096 | and from), and return 1. Otherwise, update CMP_IT->stop_pos, and | |
1097 | return 0. */ | |
1098 | ||
1099 | int | |
1100 | composition_reseat_it (cmp_it, charpos, bytepos, endpos, w, face, string) | |
1101 | struct composition_it *cmp_it; | |
1102 | EMACS_INT charpos, bytepos, endpos; | |
1103 | struct window *w; | |
1104 | struct face *face; | |
1105 | Lisp_Object string; | |
1106 | { | |
dc954cb2 | 1107 | if (NILP (string) && charpos < PT && PT < endpos) |
e614ea00 KH |
1108 | endpos = PT; |
1109 | ||
3ffdafce KH |
1110 | if (cmp_it->ch == -2) |
1111 | { | |
1112 | composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string); | |
1113 | if (cmp_it->ch == -2) | |
1114 | return 0; | |
1115 | } | |
1116 | ||
58753d74 KH |
1117 | if (cmp_it->ch < 0) |
1118 | { | |
1119 | /* We are looking at a static composition. */ | |
1120 | EMACS_INT start, end; | |
1121 | Lisp_Object prop; | |
1122 | ||
1123 | find_composition (charpos, -1, &start, &end, &prop, string); | |
1124 | cmp_it->id = get_composition_id (charpos, bytepos, end - start, | |
1125 | prop, string); | |
1126 | if (cmp_it->id < 0) | |
1127 | goto no_composition; | |
1128 | cmp_it->nchars = end - start; | |
1129 | cmp_it->nglyphs = composition_table[cmp_it->id]->glyph_len; | |
1130 | } | |
29fb7306 | 1131 | else if (w) |
58753d74 | 1132 | { |
90b3fe91 | 1133 | Lisp_Object val, elt; |
58753d74 KH |
1134 | int i; |
1135 | ||
1136 | val = CHAR_TABLE_REF (Vcomposition_function_table, cmp_it->ch); | |
90b3fe91 KH |
1137 | for (; CONSP (val); val = XCDR (val)) |
1138 | { | |
1139 | elt = XCAR (val); | |
1140 | if (cmp_it->lookback == XFASTINT (AREF (elt, 1))) | |
1141 | break; | |
1142 | } | |
58753d74 KH |
1143 | if (NILP (val)) |
1144 | goto no_composition; | |
90b3fe91 | 1145 | |
58753d74 KH |
1146 | val = autocmp_chars (val, charpos, bytepos, endpos, w, face, string); |
1147 | if (! composition_gstring_p (val)) | |
1148 | goto no_composition; | |
1149 | if (NILP (LGSTRING_ID (val))) | |
1150 | val = composition_gstring_put_cache (val, -1); | |
1151 | cmp_it->id = XINT (LGSTRING_ID (val)); | |
1152 | for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++) | |
1153 | if (NILP (LGSTRING_GLYPH (val, i))) | |
1154 | break; | |
1155 | cmp_it->nglyphs = i; | |
1156 | } | |
29fb7306 KH |
1157 | else |
1158 | goto no_composition; | |
58753d74 KH |
1159 | cmp_it->from = 0; |
1160 | return 1; | |
1161 | ||
1162 | no_composition: | |
1163 | charpos++; | |
1164 | if (STRINGP (string)) | |
1165 | bytepos += MULTIBYTE_LENGTH_NO_CHECK (SDATA (string) + bytepos); | |
1166 | else | |
1167 | INC_POS (bytepos); | |
1168 | composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string); | |
1169 | return 0; | |
1170 | } | |
1171 | ||
1172 | int | |
1173 | composition_update_it (cmp_it, charpos, bytepos, string) | |
1174 | struct composition_it *cmp_it; | |
1175 | EMACS_INT charpos, bytepos; | |
1176 | Lisp_Object string; | |
1177 | { | |
1178 | int i, c; | |
1179 | ||
1180 | if (cmp_it->ch < 0) | |
1181 | { | |
1182 | struct composition *cmp = composition_table[cmp_it->id]; | |
1183 | ||
1184 | cmp_it->to = cmp_it->nglyphs; | |
1185 | if (cmp_it->nglyphs == 0) | |
1186 | c = -1; | |
1187 | else | |
1188 | { | |
1189 | for (i = 0; i < cmp->glyph_len; i++) | |
1190 | if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t') | |
1191 | break; | |
1192 | if (c == '\t') | |
1193 | c = ' '; | |
1194 | } | |
1195 | cmp_it->width = cmp->width; | |
1196 | } | |
1197 | else | |
1198 | { | |
1199 | Lisp_Object gstring = composition_gstring_from_id (cmp_it->id); | |
1200 | ||
1201 | if (cmp_it->nglyphs == 0) | |
1202 | { | |
1203 | c = -1; | |
1204 | cmp_it->nchars = LGSTRING_CHAR_LEN (gstring); | |
1205 | cmp_it->width = 0; | |
1206 | } | |
1207 | else | |
1208 | { | |
1209 | Lisp_Object glyph = LGSTRING_GLYPH (gstring, cmp_it->from); | |
1210 | int from = LGLYPH_FROM (glyph); | |
1211 | ||
c7c7a80c | 1212 | c = XINT (LGSTRING_CHAR (gstring, from)); |
58753d74 KH |
1213 | cmp_it->nchars = LGLYPH_TO (glyph) - from + 1; |
1214 | cmp_it->width = (LGLYPH_WIDTH (glyph) > 0 | |
1215 | ? CHAR_WIDTH (LGLYPH_CHAR (glyph)) : 0); | |
1216 | for (cmp_it->to = cmp_it->from + 1; cmp_it->to < cmp_it->nglyphs; | |
1217 | cmp_it->to++) | |
1218 | { | |
1219 | glyph = LGSTRING_GLYPH (gstring, cmp_it->to); | |
1220 | if (LGLYPH_FROM (glyph) != from) | |
1221 | break; | |
1222 | if (LGLYPH_WIDTH (glyph) > 0) | |
1223 | cmp_it->width += CHAR_WIDTH (LGLYPH_CHAR (glyph)); | |
1224 | } | |
1225 | } | |
1226 | } | |
1227 | ||
1228 | charpos += cmp_it->nchars; | |
1229 | if (STRINGP (string)) | |
1230 | cmp_it->nbytes = string_char_to_byte (string, charpos) - bytepos; | |
1231 | else | |
1232 | cmp_it->nbytes = CHAR_TO_BYTE (charpos) - bytepos; | |
1233 | return c; | |
1234 | } | |
1235 | ||
1236 | ||
90b3fe91 KH |
1237 | struct position_record |
1238 | { | |
1239 | EMACS_INT pos, pos_byte; | |
1240 | unsigned char *p; | |
1241 | }; | |
1242 | ||
1243 | /* Update the members of POSTION to the next character boundary. */ | |
1244 | #define FORWARD_CHAR(POSITION, STOP) \ | |
1245 | do { \ | |
90b3fe91 | 1246 | (POSITION).pos++; \ |
900c4486 KH |
1247 | if ((POSITION).pos == (STOP)) \ |
1248 | { \ | |
1249 | (POSITION).p = GAP_END_ADDR; \ | |
1250 | (POSITION).pos_byte = GPT_BYTE; \ | |
1251 | } \ | |
1252 | else \ | |
1253 | { \ | |
900c4486 | 1254 | (POSITION).pos_byte += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \ |
d45a49e3 | 1255 | (POSITION).p += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \ |
900c4486 | 1256 | } \ |
90b3fe91 KH |
1257 | } while (0) |
1258 | ||
1259 | /* Update the members of POSTION to the previous character boundary. */ | |
1260 | #define BACKWARD_CHAR(POSITION, STOP) \ | |
1261 | do { \ | |
1262 | if ((POSITION).pos == STOP) \ | |
1263 | (POSITION).p = GPT_ADDR; \ | |
1264 | do { \ | |
1265 | (POSITION).pos_byte--; \ | |
1266 | (POSITION).p--; \ | |
1267 | } while (! CHAR_HEAD_P (*((POSITION).p))); \ | |
1268 | (POSITION).pos--; \ | |
1269 | } while (0) | |
1270 | ||
1271 | static Lisp_Object _work_val; | |
1272 | static int _work_char; | |
1273 | ||
1274 | /* 1 iff the character C is composable. */ | |
e614ea00 KH |
1275 | #define CHAR_COMPOSABLE_P(C) \ |
1276 | ((C) == 0x200C || (C) == 0x200D \ | |
1277 | || (_work_val = CHAR_TABLE_REF (Vunicode_category_table, (C)), \ | |
1278 | (SYMBOLP (_work_val) \ | |
1279 | && (_work_char = SDATA (SYMBOL_NAME (_work_val))[0]) != 'C' \ | |
1280 | && _work_char != 'Z'))) | |
90b3fe91 KH |
1281 | |
1282 | /* This is like find_composition, but find an automatic composition | |
1283 | instead. If found, set *GSTRING to the glyph-string representing | |
1284 | the composition, and return 1. Otherwise, return 0. */ | |
1285 | ||
1286 | static int | |
1287 | find_automatic_composition (pos, limit, start, end, gstring, string) | |
1288 | EMACS_INT pos, limit, *start, *end; | |
1289 | Lisp_Object *gstring, string; | |
1290 | { | |
1291 | EMACS_INT head, tail, stop; | |
895416e3 KH |
1292 | /* Limit to check a composition after POS. */ |
1293 | EMACS_INT fore_check_limit; | |
90b3fe91 KH |
1294 | struct position_record orig, cur, check, prev; |
1295 | Lisp_Object check_val, val, elt; | |
1296 | int check_lookback; | |
1297 | int c; | |
29fb7306 | 1298 | Lisp_Object window; |
90b3fe91 KH |
1299 | struct window *w; |
1300 | ||
3eda4b19 | 1301 | window = Fget_buffer_window (Fcurrent_buffer (), Qnil); |
29fb7306 KH |
1302 | if (NILP (window)) |
1303 | return 0; | |
1304 | w = XWINDOW (window); | |
1305 | ||
90b3fe91 KH |
1306 | orig.pos = pos; |
1307 | if (NILP (string)) | |
1308 | { | |
1309 | head = BEGV, tail = ZV, stop = GPT; | |
1310 | orig.pos_byte = CHAR_TO_BYTE (orig.pos); | |
1311 | orig.p = BYTE_POS_ADDR (orig.pos_byte); | |
1312 | } | |
1313 | else | |
1314 | { | |
1315 | head = 0, tail = SCHARS (string), stop = -1; | |
1316 | orig.pos_byte = string_char_to_byte (string, orig.pos); | |
1317 | orig.p = SDATA (string) + orig.pos_byte; | |
1318 | } | |
1319 | if (limit < pos) | |
895416e3 | 1320 | fore_check_limit = min (tail, pos + MAX_AUTO_COMPOSITION_LOOKBACK); |
90b3fe91 | 1321 | else |
895416e3 | 1322 | fore_check_limit = min (tail, limit + MAX_AUTO_COMPOSITION_LOOKBACK); |
90b3fe91 KH |
1323 | cur = orig; |
1324 | ||
1325 | retry: | |
1326 | check_val = Qnil; | |
895416e3 | 1327 | /* At first, check if POS is composable. */ |
62a6e103 | 1328 | c = STRING_CHAR (cur.p); |
90b3fe91 KH |
1329 | if (! CHAR_COMPOSABLE_P (c)) |
1330 | { | |
1331 | if (limit < 0) | |
1332 | return 0; | |
1333 | if (limit >= cur.pos) | |
1334 | goto search_forward; | |
1335 | } | |
1336 | else | |
1337 | { | |
1338 | val = CHAR_TABLE_REF (Vcomposition_function_table, c); | |
1339 | if (! NILP (val)) | |
1340 | check_val = val, check = cur; | |
1341 | else | |
895416e3 | 1342 | while (cur.pos + 1 < fore_check_limit) |
90b3fe91 | 1343 | { |
895416e3 KH |
1344 | EMACS_INT b, e; |
1345 | ||
90b3fe91 | 1346 | FORWARD_CHAR (cur, stop); |
895416e3 KH |
1347 | if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e, |
1348 | Qnil) | |
1349 | && COMPOSITION_VALID_P (b, e, val)) | |
1350 | { | |
1351 | fore_check_limit = cur.pos; | |
1352 | break; | |
1353 | } | |
62a6e103 | 1354 | c = STRING_CHAR (cur.p); |
90b3fe91 KH |
1355 | if (! CHAR_COMPOSABLE_P (c)) |
1356 | break; | |
1357 | val = CHAR_TABLE_REF (Vcomposition_function_table, c); | |
1358 | if (NILP (val)) | |
1359 | continue; | |
1360 | check_val = val, check = cur; | |
1361 | break; | |
1362 | } | |
1363 | cur = orig; | |
1364 | } | |
1365 | /* Rewind back to the position where we can safely search forward | |
1366 | for compositions. */ | |
1367 | while (cur.pos > head) | |
1368 | { | |
895416e3 KH |
1369 | EMACS_INT b, e; |
1370 | ||
90b3fe91 | 1371 | BACKWARD_CHAR (cur, stop); |
895416e3 KH |
1372 | if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e, Qnil) |
1373 | && COMPOSITION_VALID_P (b, e, val)) | |
1374 | break; | |
62a6e103 | 1375 | c = STRING_CHAR (cur.p); |
90b3fe91 KH |
1376 | if (! CHAR_COMPOSABLE_P (c)) |
1377 | break; | |
1378 | val = CHAR_TABLE_REF (Vcomposition_function_table, c); | |
1379 | if (! NILP (val)) | |
1380 | check_val = val, check = cur; | |
1381 | } | |
1382 | prev = cur; | |
1383 | /* Now search forward. */ | |
087e2ea9 | 1384 | search_forward: |
90b3fe91 KH |
1385 | *gstring = Qnil; |
1386 | if (! NILP (check_val) || limit >= orig.pos) | |
1387 | { | |
1388 | if (NILP (check_val)) | |
1389 | cur = orig; | |
1390 | else | |
1391 | cur = check; | |
895416e3 | 1392 | while (cur.pos < fore_check_limit) |
90b3fe91 KH |
1393 | { |
1394 | int need_adjustment = 0; | |
1395 | ||
1396 | if (NILP (check_val)) | |
1397 | { | |
62a6e103 | 1398 | c = STRING_CHAR (cur.p); |
90b3fe91 KH |
1399 | check_val = CHAR_TABLE_REF (Vcomposition_function_table, c); |
1400 | } | |
1401 | for (; CONSP (check_val); check_val = XCDR (check_val)) | |
1402 | { | |
1403 | elt = XCAR (check_val); | |
1404 | if (VECTORP (elt) && ASIZE (elt) == 3 && NATNUMP (AREF (elt, 1)) | |
1405 | && cur.pos - XFASTINT (AREF (elt, 1)) >= head) | |
1406 | { | |
1407 | check.pos = cur.pos - XFASTINT (AREF (elt, 1)); | |
1408 | if (check.pos == cur.pos) | |
1409 | check.pos_byte = cur.pos_byte; | |
1410 | else | |
1411 | check.pos_byte = CHAR_TO_BYTE (check.pos); | |
1412 | val = autocmp_chars (check_val, check.pos, check.pos_byte, | |
1413 | tail, w, NULL, string); | |
1414 | need_adjustment = 1; | |
1415 | if (! NILP (val)) | |
1416 | { | |
1417 | *gstring = val; | |
1418 | *start = check.pos; | |
1419 | *end = check.pos + LGSTRING_CHAR_LEN (*gstring); | |
1420 | if (*start <= orig.pos ? *end > orig.pos | |
1421 | : limit >= orig.pos) | |
1422 | return 1; | |
1423 | cur.pos = *end; | |
1424 | cur.pos_byte = CHAR_TO_BYTE (cur.pos); | |
1425 | break; | |
1426 | } | |
1427 | } | |
1428 | } | |
1429 | if (need_adjustment) | |
1430 | { | |
1431 | /* As we have called Lisp, there's a possibilily that | |
1432 | buffer/string is relocated. */ | |
1433 | if (NILP (string)) | |
1434 | cur.p = BYTE_POS_ADDR (cur.pos_byte); | |
1435 | else | |
1436 | cur.p = SDATA (string) + cur.pos_byte; | |
1437 | } | |
1438 | if (! CONSP (check_val)) | |
1439 | FORWARD_CHAR (cur, stop); | |
1440 | check_val = Qnil; | |
1441 | } | |
1442 | } | |
1443 | if (! NILP (*gstring)) | |
1444 | return (limit >= 0 || (*start <= orig.pos && *end > orig.pos)); | |
1445 | if (limit >= 0 && limit < orig.pos && prev.pos > head) | |
1446 | { | |
1447 | cur = prev; | |
1448 | BACKWARD_CHAR (cur, stop); | |
1449 | orig = cur; | |
895416e3 | 1450 | fore_check_limit = orig.pos; |
90b3fe91 KH |
1451 | goto retry; |
1452 | } | |
1453 | return 0; | |
1454 | } | |
1455 | ||
e614ea00 KH |
1456 | /* Return the adjusted point provided that point is moved from LAST_PT |
1457 | to NEW_PT. */ | |
1458 | ||
58753d74 | 1459 | int |
e614ea00 KH |
1460 | composition_adjust_point (last_pt, new_pt) |
1461 | EMACS_INT last_pt, new_pt; | |
58753d74 | 1462 | { |
58753d74 | 1463 | EMACS_INT charpos, bytepos, startpos, beg, end, pos; |
90b3fe91 KH |
1464 | Lisp_Object val; |
1465 | int i; | |
58753d74 | 1466 | |
e614ea00 KH |
1467 | if (new_pt == BEGV || new_pt == ZV) |
1468 | return new_pt; | |
58753d74 | 1469 | |
90b3fe91 | 1470 | /* At first check the static composition. */ |
e614ea00 | 1471 | if (get_property_and_range (new_pt, Qcomposition, &val, &beg, &end, Qnil) |
895416e3 KH |
1472 | && COMPOSITION_VALID_P (beg, end, val)) |
1473 | { | |
e614ea00 | 1474 | if (beg < new_pt /* && end > new_pt <- It's always the case. */ |
895416e3 | 1475 | && (last_pt <= beg || last_pt >= end)) |
e614ea00 KH |
1476 | return (new_pt < last_pt ? beg : end); |
1477 | return new_pt; | |
895416e3 | 1478 | } |
58753d74 KH |
1479 | |
1480 | if (NILP (current_buffer->enable_multibyte_characters) | |
1481 | || ! FUNCTIONP (Vauto_composition_function)) | |
e614ea00 | 1482 | return new_pt; |
58753d74 | 1483 | |
90b3fe91 | 1484 | /* Next check the automatic composition. */ |
e614ea00 KH |
1485 | if (! find_automatic_composition (new_pt, (EMACS_INT) -1, &beg, &end, &val, |
1486 | Qnil) | |
1487 | || beg == new_pt) | |
1488 | return new_pt; | |
90b3fe91 | 1489 | for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++) |
58753d74 | 1490 | { |
90b3fe91 | 1491 | Lisp_Object glyph = LGSTRING_GLYPH (val, i); |
58753d74 | 1492 | |
90b3fe91 KH |
1493 | if (NILP (glyph)) |
1494 | break; | |
e614ea00 KH |
1495 | if (beg + LGLYPH_FROM (glyph) == new_pt) |
1496 | return new_pt; | |
1497 | if (beg + LGLYPH_TO (glyph) >= new_pt) | |
1498 | return (new_pt < last_pt | |
90b3fe91 KH |
1499 | ? beg + LGLYPH_FROM (glyph) |
1500 | : beg + LGLYPH_TO (glyph) + 1); | |
58753d74 | 1501 | } |
e614ea00 | 1502 | return new_pt; |
58753d74 KH |
1503 | } |
1504 | ||
1505 | DEFUN ("composition-get-gstring", Fcomposition_get_gstring, | |
1506 | Scomposition_get_gstring, 4, 4, 0, | |
1507 | doc: /* Return a glyph-string for characters between FROM and TO. | |
9d751859 | 1508 | If the glyph string is for graphic display, FONT-OBJECT must be |
58753d74 | 1509 | a font-object to use for those characters. |
f5199465 KH |
1510 | Otherwise (for terminal display), FONT-OBJECT must be a terminal ID, a |
1511 | frame, or nil for the selected frame's terminal device. | |
58753d74 KH |
1512 | |
1513 | If the optional 4th argument STRING is not nil, it is a string | |
1514 | containing the target characters between indices FROM and TO. | |
1515 | ||
9d751859 EZ |
1516 | A glyph-string is a vector containing information about how to display |
1517 | a specific character sequence. The format is: | |
58753d74 KH |
1518 | [HEADER ID GLYPH ...] |
1519 | ||
1520 | HEADER is a vector of this form: | |
1521 | [FONT-OBJECT CHAR ...] | |
1522 | where | |
1523 | FONT-OBJECT is a font-object for all glyphs in the glyph-string, | |
f5199465 | 1524 | or the terminal coding system of the specified terminal. |
58753d74 KH |
1525 | CHARs are characters to be composed by GLYPHs. |
1526 | ||
1527 | ID is an identification number of the glyph-string. It may be nil if | |
1528 | not yet shaped. | |
1529 | ||
9d751859 | 1530 | GLYPH is a vector whose elements have this form: |
58753d74 KH |
1531 | [ FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT |
1532 | [ [X-OFF Y-OFF WADJUST] | nil] ] | |
1533 | where | |
1534 | FROM-IDX and TO-IDX are used internally and should not be touched. | |
1535 | C is the character of the glyph. | |
1536 | CODE is the glyph-code of C in FONT-OBJECT. | |
1537 | WIDTH thru DESCENT are the metrics (in pixels) of the glyph. | |
9d751859 | 1538 | X-OFF and Y-OFF are offsets to the base position for the glyph. |
58753d74 KH |
1539 | WADJUST is the adjustment to the normal width of the glyph. |
1540 | ||
9d751859 EZ |
1541 | If GLYPH is nil, the remaining elements of the glyph-string vector |
1542 | should be ignored. */) | |
58753d74 KH |
1543 | (from, to, font_object, string) |
1544 | Lisp_Object font_object, from, to, string; | |
1545 | { | |
1546 | Lisp_Object gstring, header; | |
46f905e1 | 1547 | EMACS_INT frompos, topos; |
58753d74 | 1548 | |
ea8ba975 KH |
1549 | CHECK_NATNUM (from); |
1550 | CHECK_NATNUM (to); | |
895416e3 KH |
1551 | if (XINT (to) > XINT (from) + MAX_COMPOSITION_COMPONENTS) |
1552 | to = make_number (XINT (from) + MAX_COMPOSITION_COMPONENTS); | |
f5199465 KH |
1553 | if (! FONT_OBJECT_P (font_object)) |
1554 | { | |
1555 | struct coding_system *coding; | |
1556 | struct terminal *terminal = get_terminal (font_object, 1); | |
1557 | ||
1558 | coding = ((TERMINAL_TERMINAL_CODING (terminal)->common_flags | |
1559 | & CODING_REQUIRE_ENCODING_MASK) | |
1560 | ? TERMINAL_TERMINAL_CODING (terminal) : &safe_terminal_coding); | |
1561 | font_object = CODING_ID_NAME (coding->id); | |
1562 | } | |
1563 | ||
58753d74 KH |
1564 | header = fill_gstring_header (Qnil, from, to, font_object, string); |
1565 | gstring = gstring_lookup_cache (header); | |
1566 | if (! NILP (gstring)) | |
1567 | return gstring; | |
46f905e1 | 1568 | |
ea8ba975 KH |
1569 | frompos = XINT (from); |
1570 | topos = XINT (to); | |
46f905e1 SM |
1571 | if (LGSTRING_GLYPH_LEN (gstring_work) < topos - frompos) |
1572 | gstring_work = Fmake_vector (make_number (topos - frompos + 2), Qnil); | |
58753d74 KH |
1573 | LGSTRING_SET_HEADER (gstring_work, header); |
1574 | LGSTRING_SET_ID (gstring_work, Qnil); | |
1575 | fill_gstring_body (gstring_work); | |
1576 | return gstring_work; | |
1577 | } | |
1578 | ||
ca4c9455 KH |
1579 | \f |
1580 | /* Emacs Lisp APIs. */ | |
1581 | ||
1582 | DEFUN ("compose-region-internal", Fcompose_region_internal, | |
1583 | Scompose_region_internal, 2, 4, 0, | |
335c5470 PJ |
1584 | doc: /* Internal use only. |
1585 | ||
1586 | Compose text in the region between START and END. | |
1587 | Optional 3rd and 4th arguments are COMPONENTS and MODIFICATION-FUNC | |
9d751859 | 1588 | for the composition. See `compose-region' for more details. */) |
d562f8ab JB |
1589 | (start, end, components, modification_func) |
1590 | Lisp_Object start, end, components, modification_func; | |
ca4c9455 KH |
1591 | { |
1592 | validate_region (&start, &end); | |
1593 | if (!NILP (components) | |
1594 | && !INTEGERP (components) | |
1595 | && !CONSP (components) | |
1596 | && !STRINGP (components)) | |
b7826503 | 1597 | CHECK_VECTOR (components); |
ca4c9455 | 1598 | |
d562f8ab | 1599 | compose_text (XINT (start), XINT (end), components, modification_func, Qnil); |
ca4c9455 KH |
1600 | return Qnil; |
1601 | } | |
1602 | ||
1603 | DEFUN ("compose-string-internal", Fcompose_string_internal, | |
1604 | Scompose_string_internal, 3, 5, 0, | |
335c5470 PJ |
1605 | doc: /* Internal use only. |
1606 | ||
1607 | Compose text between indices START and END of STRING. | |
1608 | Optional 4th and 5th arguments are COMPONENTS and MODIFICATION-FUNC | |
9d751859 | 1609 | for the composition. See `compose-string' for more details. */) |
d562f8ab JB |
1610 | (string, start, end, components, modification_func) |
1611 | Lisp_Object string, start, end, components, modification_func; | |
ca4c9455 | 1612 | { |
b7826503 PJ |
1613 | CHECK_STRING (string); |
1614 | CHECK_NUMBER (start); | |
1615 | CHECK_NUMBER (end); | |
ca4c9455 KH |
1616 | |
1617 | if (XINT (start) < 0 || | |
1618 | XINT (start) > XINT (end) | |
d5db4077 | 1619 | || XINT (end) > SCHARS (string)) |
ca4c9455 KH |
1620 | args_out_of_range (start, end); |
1621 | ||
d562f8ab | 1622 | compose_text (XINT (start), XINT (end), components, modification_func, string); |
ca4c9455 KH |
1623 | return string; |
1624 | } | |
1625 | ||
1626 | DEFUN ("find-composition-internal", Ffind_composition_internal, | |
177c0ea7 | 1627 | Sfind_composition_internal, 4, 4, 0, |
335c5470 PJ |
1628 | doc: /* Internal use only. |
1629 | ||
1630 | Return information about composition at or nearest to position POS. | |
9d751859 | 1631 | See `find-composition' for more details. */) |
335c5470 | 1632 | (pos, limit, string, detail_p) |
ca4c9455 KH |
1633 | Lisp_Object pos, limit, string, detail_p; |
1634 | { | |
90b3fe91 KH |
1635 | Lisp_Object prop, tail, gstring; |
1636 | EMACS_INT start, end, from, to; | |
ca4c9455 KH |
1637 | int id; |
1638 | ||
b7826503 | 1639 | CHECK_NUMBER_COERCE_MARKER (pos); |
90b3fe91 | 1640 | from = XINT (pos); |
ca4c9455 KH |
1641 | if (!NILP (limit)) |
1642 | { | |
b7826503 | 1643 | CHECK_NUMBER_COERCE_MARKER (limit); |
90b3fe91 | 1644 | to = XINT (limit); |
ca4c9455 KH |
1645 | } |
1646 | else | |
90b3fe91 | 1647 | to = -1; |
177c0ea7 | 1648 | |
ca4c9455 | 1649 | if (!NILP (string)) |
e3b3e327 | 1650 | { |
b7826503 | 1651 | CHECK_STRING (string); |
d5db4077 | 1652 | if (XINT (pos) < 0 || XINT (pos) > SCHARS (string)) |
e3b3e327 GM |
1653 | args_out_of_range (string, pos); |
1654 | } | |
1655 | else | |
1656 | { | |
fa9090b8 | 1657 | if (XINT (pos) < BEGV || XINT (pos) > ZV) |
e3b3e327 GM |
1658 | args_out_of_range (Fcurrent_buffer (), pos); |
1659 | } | |
ca4c9455 | 1660 | |
90b3fe91 KH |
1661 | if (!find_composition (from, to, &start, &end, &prop, string)) |
1662 | { | |
1663 | if (!NILP (current_buffer->enable_multibyte_characters) | |
1664 | && FUNCTIONP (Vauto_composition_function) | |
1665 | && find_automatic_composition (from, to, &start, &end, &gstring, | |
1666 | string)) | |
1667 | return list3 (make_number (start), make_number (end), gstring); | |
1668 | return Qnil; | |
1669 | } | |
1670 | if ((end <= XINT (pos) || start > XINT (pos))) | |
1671 | { | |
1672 | EMACS_INT s, e; | |
1673 | ||
1674 | if (find_automatic_composition (from, to, &s, &e, &gstring, string) | |
1675 | && (e <= XINT (pos) ? e > end : s < start)) | |
1676 | return list3 (make_number (start), make_number (end), gstring); | |
1677 | } | |
ca4c9455 KH |
1678 | if (!COMPOSITION_VALID_P (start, end, prop)) |
1679 | return Fcons (make_number (start), Fcons (make_number (end), | |
1680 | Fcons (Qnil, Qnil))); | |
1681 | if (NILP (detail_p)) | |
1682 | return Fcons (make_number (start), Fcons (make_number (end), | |
1683 | Fcons (Qt, Qnil))); | |
1684 | ||
1685 | if (COMPOSITION_REGISTERD_P (prop)) | |
1686 | id = COMPOSITION_ID (prop); | |
1687 | else | |
1688 | { | |
1689 | int start_byte = (NILP (string) | |
1690 | ? CHAR_TO_BYTE (start) | |
1691 | : string_char_to_byte (string, start)); | |
1692 | id = get_composition_id (start, start_byte, end - start, prop, string); | |
1693 | } | |
1694 | ||
1695 | if (id >= 0) | |
1696 | { | |
1697 | Lisp_Object components, relative_p, mod_func; | |
1698 | enum composition_method method = COMPOSITION_METHOD (prop); | |
1699 | int width = composition_table[id]->width; | |
1700 | ||
1701 | components = Fcopy_sequence (COMPOSITION_COMPONENTS (prop)); | |
1702 | relative_p = (method == COMPOSITION_WITH_RULE_ALTCHARS | |
1703 | ? Qnil : Qt); | |
1704 | mod_func = COMPOSITION_MODIFICATION_FUNC (prop); | |
1705 | tail = Fcons (components, | |
1706 | Fcons (relative_p, | |
1707 | Fcons (mod_func, | |
1708 | Fcons (make_number (width), Qnil)))); | |
1709 | } | |
1710 | else | |
1711 | tail = Qnil; | |
1712 | ||
1713 | return Fcons (make_number (start), Fcons (make_number (end), tail)); | |
1714 | } | |
1715 | ||
1716 | \f | |
1717 | void | |
1718 | syms_of_composite () | |
1719 | { | |
58753d74 KH |
1720 | int i; |
1721 | ||
d67b4f80 | 1722 | Qcomposition = intern_c_string ("composition"); |
ca4c9455 KH |
1723 | staticpro (&Qcomposition); |
1724 | ||
58753d74 | 1725 | /* Make a hash table for static composition. */ |
ca4c9455 | 1726 | { |
09654086 | 1727 | Lisp_Object args[6]; |
ca4c9455 | 1728 | extern Lisp_Object QCsize; |
177c0ea7 | 1729 | |
ca4c9455 KH |
1730 | args[0] = QCtest; |
1731 | args[1] = Qequal; | |
1732 | args[2] = QCweakness; | |
dc47eccc | 1733 | /* We used to make the hash table weak so that unreferenced |
ca101cff | 1734 | compositions can be garbage-collected. But, usually once |
dc47eccc KH |
1735 | created compositions are repeatedly used in an Emacs session, |
1736 | and thus it's not worth to save memory in such a way. So, we | |
1737 | make the table not weak. */ | |
6a83ee8a | 1738 | args[3] = Qnil; |
ca4c9455 KH |
1739 | args[4] = QCsize; |
1740 | args[5] = make_number (311); | |
09654086 | 1741 | composition_hash_table = Fmake_hash_table (6, args); |
ca4c9455 KH |
1742 | staticpro (&composition_hash_table); |
1743 | } | |
1744 | ||
58753d74 KH |
1745 | /* Make a hash table for glyph-string. */ |
1746 | { | |
1747 | Lisp_Object args[6]; | |
1748 | extern Lisp_Object QCsize; | |
1749 | ||
1750 | args[0] = QCtest; | |
1751 | args[1] = Qequal; | |
1752 | args[2] = QCweakness; | |
1753 | args[3] = Qnil; | |
1754 | args[4] = QCsize; | |
1755 | args[5] = make_number (311); | |
1756 | gstring_hash_table = Fmake_hash_table (6, args); | |
1757 | staticpro (&gstring_hash_table); | |
1758 | } | |
1759 | ||
1760 | staticpro (&gstring_work_headers); | |
1761 | gstring_work_headers = Fmake_vector (make_number (8), Qnil); | |
1762 | for (i = 0; i < 8; i++) | |
1763 | ASET (gstring_work_headers, i, Fmake_vector (make_number (i + 2), Qnil)); | |
1764 | staticpro (&gstring_work); | |
1765 | gstring_work = Fmake_vector (make_number (10), Qnil); | |
1766 | ||
ca4c9455 KH |
1767 | /* Text property `composition' should be nonsticky by default. */ |
1768 | Vtext_property_default_nonsticky | |
1769 | = Fcons (Fcons (Qcomposition, Qt), Vtext_property_default_nonsticky); | |
1770 | ||
1771 | DEFVAR_LISP ("compose-chars-after-function", &Vcompose_chars_after_function, | |
335c5470 PJ |
1772 | doc: /* Function to adjust composition of buffer text. |
1773 | ||
9d751859 EZ |
1774 | This function is called with three arguments: FROM, TO, and OBJECT. |
1775 | FROM and TO specify the range of text whose composition should be | |
335c5470 PJ |
1776 | adjusted. OBJECT, if non-nil, is a string that contains the text. |
1777 | ||
1778 | This function is called after a text with `composition' property is | |
1779 | inserted or deleted to keep `composition' property of buffer text | |
1780 | valid. | |
1781 | ||
1782 | The default value is the function `compose-chars-after'. */); | |
d67b4f80 | 1783 | Vcompose_chars_after_function = intern_c_string ("compose-chars-after"); |
ca4c9455 | 1784 | |
d67b4f80 | 1785 | Qauto_composed = intern_c_string ("auto-composed"); |
f96ba4c1 | 1786 | staticpro (&Qauto_composed); |
40add26d | 1787 | |
d67b4f80 | 1788 | Qauto_composition_function = intern_c_string ("auto-composition-function"); |
f96ba4c1 | 1789 | staticpro (&Qauto_composition_function); |
40add26d | 1790 | |
f96ba4c1 KH |
1791 | DEFVAR_LISP ("auto-composition-function", &Vauto_composition_function, |
1792 | doc: /* Function to call to compose characters automatically. | |
9d751859 | 1793 | This function is called from the display routine with four arguments: |
ddc872ba | 1794 | FROM, TO, WINDOW, and STRING. |
335c5470 | 1795 | |
ddc872ba KH |
1796 | If STRING is nil, the function must compose characters in the region |
1797 | between FROM and TO in the current buffer. | |
335c5470 | 1798 | |
ddc872ba KH |
1799 | Otherwise, STRING is a string, and FROM and TO are indices into the |
1800 | string. In this case, the function must compose characters in the | |
1801 | string. */); | |
f96ba4c1 | 1802 | Vauto_composition_function = Qnil; |
40add26d | 1803 | |
58753d74 | 1804 | DEFVAR_LISP ("composition-function-table", &Vcomposition_function_table, |
9d751859 | 1805 | doc: /* Char-table of functions for automatic character composition. |
58753d74 KH |
1806 | For each character that has to be composed automatically with |
1807 | preceding and/or following characters, this char-table contains | |
1808 | a function to call to compose that character. | |
1809 | ||
1810 | The element at index C in the table, if non-nil, is a list of | |
1811 | this form: ([PATTERN PREV-CHARS FUNC] ...) | |
1812 | ||
9d751859 | 1813 | PATTERN is a regular expression which C and the surrounding |
58753d74 KH |
1814 | characters must match. |
1815 | ||
895416e3 KH |
1816 | PREV-CHARS is a non-negative integer (less than 4) specifying how many |
1817 | characters before C to check the matching with PATTERN. If it is 0, | |
1818 | PATTERN must match C and the following characters. If it is 1, | |
1819 | PATTERN must match a character before C and the following characters. | |
58753d74 KH |
1820 | |
1821 | If PREV-CHARS is 0, PATTERN can be nil, which means that the | |
1822 | single character C should be composed. | |
1823 | ||
1824 | FUNC is a function to return a glyph-string representing a | |
9d751859 | 1825 | composition of the characters that match PATTERN. It is |
58753d74 KH |
1826 | called with one argument GSTRING. |
1827 | ||
1828 | GSTRING is a template of a glyph-string to return. It is already | |
1829 | filled with a proper header for the characters to compose, and | |
1830 | glyphs corresponding to those characters one by one. The | |
9d751859 | 1831 | function must return a new glyph-string with the same header as |
58753d74 KH |
1832 | GSTRING, or modify GSTRING itself and return it. |
1833 | ||
1834 | See also the documentation of `auto-composition-mode'. */); | |
1835 | Vcomposition_function_table = Fmake_char_table (Qnil, Qnil); | |
1836 | ||
ca4c9455 KH |
1837 | defsubr (&Scompose_region_internal); |
1838 | defsubr (&Scompose_string_internal); | |
1839 | defsubr (&Sfind_composition_internal); | |
58753d74 | 1840 | defsubr (&Scomposition_get_gstring); |
ca4c9455 | 1841 | } |
ee6f9c59 KH |
1842 | |
1843 | /* arch-tag: 79cefaf8-ca48-4eed-97e5-d5afb290d272 | |
1844 | (do not change this comment) */ |