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