(FIXNUM_OVERFLOW_P): Fix last change to handle unsigned
[bpt/emacs.git] / src / composite.c
CommitLineData
ca4c9455 1/* Composite sequence support.
aaef169d 2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
76b6f707
GM
3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
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
11This file is part of GNU Emacs.
12
9ec0b715 13GNU Emacs is free software: you can redistribute it and/or modify
ca4c9455 14it under the terms of the GNU General Public License as published by
9ec0b715
GM
15the Free Software Foundation, either version 3 of the License, or
16(at your option) any later version.
ca4c9455
KH
17
18GNU Emacs is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
9ec0b715 24along 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
138Lisp_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. */
143struct composition **composition_table;
144
145/* The current size of `composition_table'. */
146static int composition_table_size;
147
148/* Number of compositions currently made. */
149int 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. */
154Lisp_Object composition_hash_table;
155
156/* Function to call to adjust composition. */
157Lisp_Object Vcompose_chars_after_function;
158
f96ba4c1
KH
159Lisp_Object Qauto_composed;
160Lisp_Object Vauto_composition_function;
161Lisp_Object Qauto_composition_function;
58753d74 162Lisp_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
168EXFUN (Fremove_list_of_text_properties, 4);
169
ca4c9455
KH
170/* Temporary variable used in macros COMPOSITION_XXX. */
171Lisp_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
181int
182get_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
430int
431find_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
471static void
472run_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
505void
506update_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
613void
614make_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
639void
640compose_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
653static 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
664static Lisp_Object gstring_hash_table;
665
666static Lisp_Object gstring_lookup_cache P_ ((Lisp_Object));
667
668static Lisp_Object
669gstring_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
678Lisp_Object
679composition_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
708Lisp_Object
709composition_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
717static Lisp_Object fill_gstring_header P_ ((Lisp_Object, Lisp_Object,
718 Lisp_Object, Lisp_Object,
719 Lisp_Object));
720
721int
722composition_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
753int
754composition_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
809static Lisp_Object gstring_work;
810static Lisp_Object gstring_work_headers;
811
812static Lisp_Object
813fill_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
871extern void font_fill_lglyph_metrics P_ ((Lisp_Object, Lisp_Object));
872
873static void
874fill_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
923static Lisp_Object
924autocmp_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
1017void
1018composition_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
1099int
1100composition_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{
3ffdafce
KH
1107 if (cmp_it->ch == -2)
1108 {
1109 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string);
1110 if (cmp_it->ch == -2)
1111 return 0;
1112 }
1113
58753d74
KH
1114 if (cmp_it->ch < 0)
1115 {
1116 /* We are looking at a static composition. */
1117 EMACS_INT start, end;
1118 Lisp_Object prop;
1119
1120 find_composition (charpos, -1, &start, &end, &prop, string);
1121 cmp_it->id = get_composition_id (charpos, bytepos, end - start,
1122 prop, string);
1123 if (cmp_it->id < 0)
1124 goto no_composition;
1125 cmp_it->nchars = end - start;
1126 cmp_it->nglyphs = composition_table[cmp_it->id]->glyph_len;
1127 }
29fb7306 1128 else if (w)
58753d74 1129 {
90b3fe91 1130 Lisp_Object val, elt;
58753d74
KH
1131 int i;
1132
1133 val = CHAR_TABLE_REF (Vcomposition_function_table, cmp_it->ch);
90b3fe91
KH
1134 for (; CONSP (val); val = XCDR (val))
1135 {
1136 elt = XCAR (val);
1137 if (cmp_it->lookback == XFASTINT (AREF (elt, 1)))
1138 break;
1139 }
58753d74
KH
1140 if (NILP (val))
1141 goto no_composition;
90b3fe91 1142
58753d74
KH
1143 val = autocmp_chars (val, charpos, bytepos, endpos, w, face, string);
1144 if (! composition_gstring_p (val))
1145 goto no_composition;
1146 if (NILP (LGSTRING_ID (val)))
1147 val = composition_gstring_put_cache (val, -1);
1148 cmp_it->id = XINT (LGSTRING_ID (val));
1149 for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++)
1150 if (NILP (LGSTRING_GLYPH (val, i)))
1151 break;
1152 cmp_it->nglyphs = i;
1153 }
29fb7306
KH
1154 else
1155 goto no_composition;
58753d74
KH
1156 cmp_it->from = 0;
1157 return 1;
1158
1159 no_composition:
1160 charpos++;
1161 if (STRINGP (string))
1162 bytepos += MULTIBYTE_LENGTH_NO_CHECK (SDATA (string) + bytepos);
1163 else
1164 INC_POS (bytepos);
1165 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string);
1166 return 0;
1167}
1168
1169int
1170composition_update_it (cmp_it, charpos, bytepos, string)
1171 struct composition_it *cmp_it;
1172 EMACS_INT charpos, bytepos;
1173 Lisp_Object string;
1174{
1175 int i, c;
1176
1177 if (cmp_it->ch < 0)
1178 {
1179 struct composition *cmp = composition_table[cmp_it->id];
1180
1181 cmp_it->to = cmp_it->nglyphs;
1182 if (cmp_it->nglyphs == 0)
1183 c = -1;
1184 else
1185 {
1186 for (i = 0; i < cmp->glyph_len; i++)
1187 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
1188 break;
1189 if (c == '\t')
1190 c = ' ';
1191 }
1192 cmp_it->width = cmp->width;
1193 }
1194 else
1195 {
1196 Lisp_Object gstring = composition_gstring_from_id (cmp_it->id);
1197
1198 if (cmp_it->nglyphs == 0)
1199 {
1200 c = -1;
1201 cmp_it->nchars = LGSTRING_CHAR_LEN (gstring);
1202 cmp_it->width = 0;
1203 }
1204 else
1205 {
1206 Lisp_Object glyph = LGSTRING_GLYPH (gstring, cmp_it->from);
1207 int from = LGLYPH_FROM (glyph);
1208
c7c7a80c 1209 c = XINT (LGSTRING_CHAR (gstring, from));
58753d74
KH
1210 cmp_it->nchars = LGLYPH_TO (glyph) - from + 1;
1211 cmp_it->width = (LGLYPH_WIDTH (glyph) > 0
1212 ? CHAR_WIDTH (LGLYPH_CHAR (glyph)) : 0);
1213 for (cmp_it->to = cmp_it->from + 1; cmp_it->to < cmp_it->nglyphs;
1214 cmp_it->to++)
1215 {
1216 glyph = LGSTRING_GLYPH (gstring, cmp_it->to);
1217 if (LGLYPH_FROM (glyph) != from)
1218 break;
1219 if (LGLYPH_WIDTH (glyph) > 0)
1220 cmp_it->width += CHAR_WIDTH (LGLYPH_CHAR (glyph));
1221 }
1222 }
1223 }
1224
1225 charpos += cmp_it->nchars;
1226 if (STRINGP (string))
1227 cmp_it->nbytes = string_char_to_byte (string, charpos) - bytepos;
1228 else
1229 cmp_it->nbytes = CHAR_TO_BYTE (charpos) - bytepos;
1230 return c;
1231}
1232
1233
90b3fe91
KH
1234struct position_record
1235{
1236 EMACS_INT pos, pos_byte;
1237 unsigned char *p;
1238};
1239
1240/* Update the members of POSTION to the next character boundary. */
1241#define FORWARD_CHAR(POSITION, STOP) \
1242 do { \
90b3fe91 1243 (POSITION).pos++; \
900c4486
KH
1244 if ((POSITION).pos == (STOP)) \
1245 { \
1246 (POSITION).p = GAP_END_ADDR; \
1247 (POSITION).pos_byte = GPT_BYTE; \
1248 } \
1249 else \
1250 { \
900c4486 1251 (POSITION).pos_byte += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \
d45a49e3 1252 (POSITION).p += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \
900c4486 1253 } \
90b3fe91
KH
1254 } while (0)
1255
1256/* Update the members of POSTION to the previous character boundary. */
1257#define BACKWARD_CHAR(POSITION, STOP) \
1258 do { \
1259 if ((POSITION).pos == STOP) \
1260 (POSITION).p = GPT_ADDR; \
1261 do { \
1262 (POSITION).pos_byte--; \
1263 (POSITION).p--; \
1264 } while (! CHAR_HEAD_P (*((POSITION).p))); \
1265 (POSITION).pos--; \
1266 } while (0)
1267
1268static Lisp_Object _work_val;
1269static int _work_char;
1270
1271/* 1 iff the character C is composable. */
1272#define CHAR_COMPOSABLE_P(C) \
1273 (_work_val = CHAR_TABLE_REF (Vunicode_category_table, (C)), \
1274 (SYMBOLP (_work_val) \
1275 && (_work_char = SDATA (SYMBOL_NAME (_work_val))[0]) != 'C' \
1276 && _work_char != 'Z'))
1277
1278/* This is like find_composition, but find an automatic composition
1279 instead. If found, set *GSTRING to the glyph-string representing
1280 the composition, and return 1. Otherwise, return 0. */
1281
1282static int
1283find_automatic_composition (pos, limit, start, end, gstring, string)
1284 EMACS_INT pos, limit, *start, *end;
1285 Lisp_Object *gstring, string;
1286{
1287 EMACS_INT head, tail, stop;
895416e3
KH
1288 /* Limit to check a composition after POS. */
1289 EMACS_INT fore_check_limit;
90b3fe91
KH
1290 struct position_record orig, cur, check, prev;
1291 Lisp_Object check_val, val, elt;
1292 int check_lookback;
1293 int c;
29fb7306 1294 Lisp_Object window;
90b3fe91
KH
1295 struct window *w;
1296
3eda4b19 1297 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
29fb7306
KH
1298 if (NILP (window))
1299 return 0;
1300 w = XWINDOW (window);
1301
90b3fe91
KH
1302 orig.pos = pos;
1303 if (NILP (string))
1304 {
1305 head = BEGV, tail = ZV, stop = GPT;
1306 orig.pos_byte = CHAR_TO_BYTE (orig.pos);
1307 orig.p = BYTE_POS_ADDR (orig.pos_byte);
1308 }
1309 else
1310 {
1311 head = 0, tail = SCHARS (string), stop = -1;
1312 orig.pos_byte = string_char_to_byte (string, orig.pos);
1313 orig.p = SDATA (string) + orig.pos_byte;
1314 }
1315 if (limit < pos)
895416e3 1316 fore_check_limit = min (tail, pos + MAX_AUTO_COMPOSITION_LOOKBACK);
90b3fe91 1317 else
895416e3 1318 fore_check_limit = min (tail, limit + MAX_AUTO_COMPOSITION_LOOKBACK);
90b3fe91
KH
1319 cur = orig;
1320
1321 retry:
1322 check_val = Qnil;
895416e3 1323 /* At first, check if POS is composable. */
90b3fe91
KH
1324 c = STRING_CHAR (cur.p, 0);
1325 if (! CHAR_COMPOSABLE_P (c))
1326 {
1327 if (limit < 0)
1328 return 0;
1329 if (limit >= cur.pos)
1330 goto search_forward;
1331 }
1332 else
1333 {
1334 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1335 if (! NILP (val))
1336 check_val = val, check = cur;
1337 else
895416e3 1338 while (cur.pos + 1 < fore_check_limit)
90b3fe91 1339 {
895416e3
KH
1340 EMACS_INT b, e;
1341
90b3fe91 1342 FORWARD_CHAR (cur, stop);
895416e3
KH
1343 if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e,
1344 Qnil)
1345 && COMPOSITION_VALID_P (b, e, val))
1346 {
1347 fore_check_limit = cur.pos;
1348 break;
1349 }
90b3fe91
KH
1350 c = STRING_CHAR (cur.p, 0);
1351 if (! CHAR_COMPOSABLE_P (c))
1352 break;
1353 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1354 if (NILP (val))
1355 continue;
1356 check_val = val, check = cur;
1357 break;
1358 }
1359 cur = orig;
1360 }
1361 /* Rewind back to the position where we can safely search forward
1362 for compositions. */
1363 while (cur.pos > head)
1364 {
895416e3
KH
1365 EMACS_INT b, e;
1366
90b3fe91 1367 BACKWARD_CHAR (cur, stop);
895416e3
KH
1368 if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e, Qnil)
1369 && COMPOSITION_VALID_P (b, e, val))
1370 break;
90b3fe91
KH
1371 c = STRING_CHAR (cur.p, 0);
1372 if (! CHAR_COMPOSABLE_P (c))
1373 break;
1374 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1375 if (! NILP (val))
1376 check_val = val, check = cur;
1377 }
1378 prev = cur;
1379 /* Now search forward. */
087e2ea9 1380 search_forward:
90b3fe91
KH
1381 *gstring = Qnil;
1382 if (! NILP (check_val) || limit >= orig.pos)
1383 {
1384 if (NILP (check_val))
1385 cur = orig;
1386 else
1387 cur = check;
895416e3 1388 while (cur.pos < fore_check_limit)
90b3fe91
KH
1389 {
1390 int need_adjustment = 0;
1391
1392 if (NILP (check_val))
1393 {
1394 c = STRING_CHAR (cur.p, 0);
1395 check_val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1396 }
1397 for (; CONSP (check_val); check_val = XCDR (check_val))
1398 {
1399 elt = XCAR (check_val);
1400 if (VECTORP (elt) && ASIZE (elt) == 3 && NATNUMP (AREF (elt, 1))
1401 && cur.pos - XFASTINT (AREF (elt, 1)) >= head)
1402 {
1403 check.pos = cur.pos - XFASTINT (AREF (elt, 1));
1404 if (check.pos == cur.pos)
1405 check.pos_byte = cur.pos_byte;
1406 else
1407 check.pos_byte = CHAR_TO_BYTE (check.pos);
1408 val = autocmp_chars (check_val, check.pos, check.pos_byte,
1409 tail, w, NULL, string);
1410 need_adjustment = 1;
1411 if (! NILP (val))
1412 {
1413 *gstring = val;
1414 *start = check.pos;
1415 *end = check.pos + LGSTRING_CHAR_LEN (*gstring);
1416 if (*start <= orig.pos ? *end > orig.pos
1417 : limit >= orig.pos)
1418 return 1;
1419 cur.pos = *end;
1420 cur.pos_byte = CHAR_TO_BYTE (cur.pos);
1421 break;
1422 }
1423 }
1424 }
1425 if (need_adjustment)
1426 {
1427 /* As we have called Lisp, there's a possibilily that
1428 buffer/string is relocated. */
1429 if (NILP (string))
1430 cur.p = BYTE_POS_ADDR (cur.pos_byte);
1431 else
1432 cur.p = SDATA (string) + cur.pos_byte;
1433 }
1434 if (! CONSP (check_val))
1435 FORWARD_CHAR (cur, stop);
1436 check_val = Qnil;
1437 }
1438 }
1439 if (! NILP (*gstring))
1440 return (limit >= 0 || (*start <= orig.pos && *end > orig.pos));
1441 if (limit >= 0 && limit < orig.pos && prev.pos > head)
1442 {
1443 cur = prev;
1444 BACKWARD_CHAR (cur, stop);
1445 orig = cur;
895416e3 1446 fore_check_limit = orig.pos;
90b3fe91
KH
1447 goto retry;
1448 }
1449 return 0;
1450}
1451
58753d74
KH
1452int
1453composition_adjust_point (last_pt)
1454 EMACS_INT last_pt;
1455{
58753d74 1456 EMACS_INT charpos, bytepos, startpos, beg, end, pos;
90b3fe91
KH
1457 Lisp_Object val;
1458 int i;
58753d74
KH
1459
1460 if (PT == BEGV || PT == ZV)
1461 return PT;
1462
90b3fe91 1463 /* At first check the static composition. */
58753d74 1464 if (get_property_and_range (PT, Qcomposition, &val, &beg, &end, Qnil)
895416e3
KH
1465 && COMPOSITION_VALID_P (beg, end, val))
1466 {
1467 if (beg < PT /* && end > PT <- It's always the case. */
1468 && (last_pt <= beg || last_pt >= end))
1469 return (PT < last_pt ? beg : end);
1470 return PT;
1471 }
58753d74
KH
1472
1473 if (NILP (current_buffer->enable_multibyte_characters)
1474 || ! FUNCTIONP (Vauto_composition_function))
1475 return PT;
1476
90b3fe91 1477 /* Next check the automatic composition. */
939c679e 1478 if (! find_automatic_composition (PT, (EMACS_INT) -1, &beg, &end, &val, Qnil)
90b3fe91 1479 || beg == PT)
58753d74 1480 return PT;
90b3fe91 1481 for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++)
58753d74 1482 {
90b3fe91 1483 Lisp_Object glyph = LGSTRING_GLYPH (val, i);
58753d74 1484
90b3fe91
KH
1485 if (NILP (glyph))
1486 break;
1487 if (beg + LGLYPH_FROM (glyph) == PT)
1488 return PT;
1489 if (beg + LGLYPH_TO (glyph) >= PT)
1490 return (PT < last_pt
1491 ? beg + LGLYPH_FROM (glyph)
1492 : beg + LGLYPH_TO (glyph) + 1);
58753d74
KH
1493 }
1494 return PT;
1495}
1496
1497DEFUN ("composition-get-gstring", Fcomposition_get_gstring,
1498 Scomposition_get_gstring, 4, 4, 0,
1499 doc: /* Return a glyph-string for characters between FROM and TO.
9d751859 1500If the glyph string is for graphic display, FONT-OBJECT must be
58753d74 1501a font-object to use for those characters.
f5199465
KH
1502Otherwise (for terminal display), FONT-OBJECT must be a terminal ID, a
1503frame, or nil for the selected frame's terminal device.
58753d74
KH
1504
1505If the optional 4th argument STRING is not nil, it is a string
1506containing the target characters between indices FROM and TO.
1507
9d751859
EZ
1508A glyph-string is a vector containing information about how to display
1509a specific character sequence. The format is:
58753d74
KH
1510 [HEADER ID GLYPH ...]
1511
1512HEADER is a vector of this form:
1513 [FONT-OBJECT CHAR ...]
1514where
1515 FONT-OBJECT is a font-object for all glyphs in the glyph-string,
f5199465 1516 or the terminal coding system of the specified terminal.
58753d74
KH
1517 CHARs are characters to be composed by GLYPHs.
1518
1519ID is an identification number of the glyph-string. It may be nil if
1520not yet shaped.
1521
9d751859 1522GLYPH is a vector whose elements have this form:
58753d74
KH
1523 [ FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT
1524 [ [X-OFF Y-OFF WADJUST] | nil] ]
1525where
1526 FROM-IDX and TO-IDX are used internally and should not be touched.
1527 C is the character of the glyph.
1528 CODE is the glyph-code of C in FONT-OBJECT.
1529 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
9d751859 1530 X-OFF and Y-OFF are offsets to the base position for the glyph.
58753d74
KH
1531 WADJUST is the adjustment to the normal width of the glyph.
1532
9d751859
EZ
1533If GLYPH is nil, the remaining elements of the glyph-string vector
1534should be ignored. */)
58753d74
KH
1535 (from, to, font_object, string)
1536 Lisp_Object font_object, from, to, string;
1537{
1538 Lisp_Object gstring, header;
46f905e1 1539 EMACS_INT frompos, topos;
58753d74 1540
ea8ba975
KH
1541 CHECK_NATNUM (from);
1542 CHECK_NATNUM (to);
895416e3
KH
1543 if (XINT (to) > XINT (from) + MAX_COMPOSITION_COMPONENTS)
1544 to = make_number (XINT (from) + MAX_COMPOSITION_COMPONENTS);
f5199465
KH
1545 if (! FONT_OBJECT_P (font_object))
1546 {
1547 struct coding_system *coding;
1548 struct terminal *terminal = get_terminal (font_object, 1);
1549
1550 coding = ((TERMINAL_TERMINAL_CODING (terminal)->common_flags
1551 & CODING_REQUIRE_ENCODING_MASK)
1552 ? TERMINAL_TERMINAL_CODING (terminal) : &safe_terminal_coding);
1553 font_object = CODING_ID_NAME (coding->id);
1554 }
1555
58753d74
KH
1556 header = fill_gstring_header (Qnil, from, to, font_object, string);
1557 gstring = gstring_lookup_cache (header);
1558 if (! NILP (gstring))
1559 return gstring;
46f905e1 1560
ea8ba975
KH
1561 frompos = XINT (from);
1562 topos = XINT (to);
46f905e1
SM
1563 if (LGSTRING_GLYPH_LEN (gstring_work) < topos - frompos)
1564 gstring_work = Fmake_vector (make_number (topos - frompos + 2), Qnil);
58753d74
KH
1565 LGSTRING_SET_HEADER (gstring_work, header);
1566 LGSTRING_SET_ID (gstring_work, Qnil);
1567 fill_gstring_body (gstring_work);
1568 return gstring_work;
1569}
1570
ca4c9455
KH
1571\f
1572/* Emacs Lisp APIs. */
1573
1574DEFUN ("compose-region-internal", Fcompose_region_internal,
1575 Scompose_region_internal, 2, 4, 0,
335c5470
PJ
1576 doc: /* Internal use only.
1577
1578Compose text in the region between START and END.
1579Optional 3rd and 4th arguments are COMPONENTS and MODIFICATION-FUNC
9d751859 1580for the composition. See `compose-region' for more details. */)
d562f8ab
JB
1581 (start, end, components, modification_func)
1582 Lisp_Object start, end, components, modification_func;
ca4c9455
KH
1583{
1584 validate_region (&start, &end);
1585 if (!NILP (components)
1586 && !INTEGERP (components)
1587 && !CONSP (components)
1588 && !STRINGP (components))
b7826503 1589 CHECK_VECTOR (components);
ca4c9455 1590
d562f8ab 1591 compose_text (XINT (start), XINT (end), components, modification_func, Qnil);
ca4c9455
KH
1592 return Qnil;
1593}
1594
1595DEFUN ("compose-string-internal", Fcompose_string_internal,
1596 Scompose_string_internal, 3, 5, 0,
335c5470
PJ
1597 doc: /* Internal use only.
1598
1599Compose text between indices START and END of STRING.
1600Optional 4th and 5th arguments are COMPONENTS and MODIFICATION-FUNC
9d751859 1601for the composition. See `compose-string' for more details. */)
d562f8ab
JB
1602 (string, start, end, components, modification_func)
1603 Lisp_Object string, start, end, components, modification_func;
ca4c9455 1604{
b7826503
PJ
1605 CHECK_STRING (string);
1606 CHECK_NUMBER (start);
1607 CHECK_NUMBER (end);
ca4c9455
KH
1608
1609 if (XINT (start) < 0 ||
1610 XINT (start) > XINT (end)
d5db4077 1611 || XINT (end) > SCHARS (string))
ca4c9455
KH
1612 args_out_of_range (start, end);
1613
d562f8ab 1614 compose_text (XINT (start), XINT (end), components, modification_func, string);
ca4c9455
KH
1615 return string;
1616}
1617
1618DEFUN ("find-composition-internal", Ffind_composition_internal,
177c0ea7 1619 Sfind_composition_internal, 4, 4, 0,
335c5470
PJ
1620 doc: /* Internal use only.
1621
1622Return information about composition at or nearest to position POS.
9d751859 1623See `find-composition' for more details. */)
335c5470 1624 (pos, limit, string, detail_p)
ca4c9455
KH
1625 Lisp_Object pos, limit, string, detail_p;
1626{
90b3fe91
KH
1627 Lisp_Object prop, tail, gstring;
1628 EMACS_INT start, end, from, to;
ca4c9455
KH
1629 int id;
1630
b7826503 1631 CHECK_NUMBER_COERCE_MARKER (pos);
90b3fe91 1632 from = XINT (pos);
ca4c9455
KH
1633 if (!NILP (limit))
1634 {
b7826503 1635 CHECK_NUMBER_COERCE_MARKER (limit);
90b3fe91 1636 to = XINT (limit);
ca4c9455
KH
1637 }
1638 else
90b3fe91 1639 to = -1;
177c0ea7 1640
ca4c9455 1641 if (!NILP (string))
e3b3e327 1642 {
b7826503 1643 CHECK_STRING (string);
d5db4077 1644 if (XINT (pos) < 0 || XINT (pos) > SCHARS (string))
e3b3e327
GM
1645 args_out_of_range (string, pos);
1646 }
1647 else
1648 {
fa9090b8 1649 if (XINT (pos) < BEGV || XINT (pos) > ZV)
e3b3e327
GM
1650 args_out_of_range (Fcurrent_buffer (), pos);
1651 }
ca4c9455 1652
90b3fe91
KH
1653 if (!find_composition (from, to, &start, &end, &prop, string))
1654 {
1655 if (!NILP (current_buffer->enable_multibyte_characters)
1656 && FUNCTIONP (Vauto_composition_function)
1657 && find_automatic_composition (from, to, &start, &end, &gstring,
1658 string))
1659 return list3 (make_number (start), make_number (end), gstring);
1660 return Qnil;
1661 }
1662 if ((end <= XINT (pos) || start > XINT (pos)))
1663 {
1664 EMACS_INT s, e;
1665
1666 if (find_automatic_composition (from, to, &s, &e, &gstring, string)
1667 && (e <= XINT (pos) ? e > end : s < start))
1668 return list3 (make_number (start), make_number (end), gstring);
1669 }
ca4c9455
KH
1670 if (!COMPOSITION_VALID_P (start, end, prop))
1671 return Fcons (make_number (start), Fcons (make_number (end),
1672 Fcons (Qnil, Qnil)));
1673 if (NILP (detail_p))
1674 return Fcons (make_number (start), Fcons (make_number (end),
1675 Fcons (Qt, Qnil)));
1676
1677 if (COMPOSITION_REGISTERD_P (prop))
1678 id = COMPOSITION_ID (prop);
1679 else
1680 {
1681 int start_byte = (NILP (string)
1682 ? CHAR_TO_BYTE (start)
1683 : string_char_to_byte (string, start));
1684 id = get_composition_id (start, start_byte, end - start, prop, string);
1685 }
1686
1687 if (id >= 0)
1688 {
1689 Lisp_Object components, relative_p, mod_func;
1690 enum composition_method method = COMPOSITION_METHOD (prop);
1691 int width = composition_table[id]->width;
1692
1693 components = Fcopy_sequence (COMPOSITION_COMPONENTS (prop));
1694 relative_p = (method == COMPOSITION_WITH_RULE_ALTCHARS
1695 ? Qnil : Qt);
1696 mod_func = COMPOSITION_MODIFICATION_FUNC (prop);
1697 tail = Fcons (components,
1698 Fcons (relative_p,
1699 Fcons (mod_func,
1700 Fcons (make_number (width), Qnil))));
1701 }
1702 else
1703 tail = Qnil;
1704
1705 return Fcons (make_number (start), Fcons (make_number (end), tail));
1706}
1707
1708\f
1709void
1710syms_of_composite ()
1711{
58753d74
KH
1712 int i;
1713
ca4c9455
KH
1714 Qcomposition = intern ("composition");
1715 staticpro (&Qcomposition);
1716
58753d74 1717 /* Make a hash table for static composition. */
ca4c9455 1718 {
09654086 1719 Lisp_Object args[6];
ca4c9455 1720 extern Lisp_Object QCsize;
177c0ea7 1721
ca4c9455
KH
1722 args[0] = QCtest;
1723 args[1] = Qequal;
1724 args[2] = QCweakness;
dc47eccc 1725 /* We used to make the hash table weak so that unreferenced
ca101cff 1726 compositions can be garbage-collected. But, usually once
dc47eccc
KH
1727 created compositions are repeatedly used in an Emacs session,
1728 and thus it's not worth to save memory in such a way. So, we
1729 make the table not weak. */
6a83ee8a 1730 args[3] = Qnil;
ca4c9455
KH
1731 args[4] = QCsize;
1732 args[5] = make_number (311);
09654086 1733 composition_hash_table = Fmake_hash_table (6, args);
ca4c9455
KH
1734 staticpro (&composition_hash_table);
1735 }
1736
58753d74
KH
1737 /* Make a hash table for glyph-string. */
1738 {
1739 Lisp_Object args[6];
1740 extern Lisp_Object QCsize;
1741
1742 args[0] = QCtest;
1743 args[1] = Qequal;
1744 args[2] = QCweakness;
1745 args[3] = Qnil;
1746 args[4] = QCsize;
1747 args[5] = make_number (311);
1748 gstring_hash_table = Fmake_hash_table (6, args);
1749 staticpro (&gstring_hash_table);
1750 }
1751
1752 staticpro (&gstring_work_headers);
1753 gstring_work_headers = Fmake_vector (make_number (8), Qnil);
1754 for (i = 0; i < 8; i++)
1755 ASET (gstring_work_headers, i, Fmake_vector (make_number (i + 2), Qnil));
1756 staticpro (&gstring_work);
1757 gstring_work = Fmake_vector (make_number (10), Qnil);
1758
ca4c9455
KH
1759 /* Text property `composition' should be nonsticky by default. */
1760 Vtext_property_default_nonsticky
1761 = Fcons (Fcons (Qcomposition, Qt), Vtext_property_default_nonsticky);
1762
1763 DEFVAR_LISP ("compose-chars-after-function", &Vcompose_chars_after_function,
335c5470
PJ
1764 doc: /* Function to adjust composition of buffer text.
1765
9d751859
EZ
1766This function is called with three arguments: FROM, TO, and OBJECT.
1767FROM and TO specify the range of text whose composition should be
335c5470
PJ
1768adjusted. OBJECT, if non-nil, is a string that contains the text.
1769
1770This function is called after a text with `composition' property is
1771inserted or deleted to keep `composition' property of buffer text
1772valid.
1773
1774The default value is the function `compose-chars-after'. */);
ca4c9455
KH
1775 Vcompose_chars_after_function = intern ("compose-chars-after");
1776
f96ba4c1
KH
1777 Qauto_composed = intern ("auto-composed");
1778 staticpro (&Qauto_composed);
40add26d 1779
f96ba4c1
KH
1780 Qauto_composition_function = intern ("auto-composition-function");
1781 staticpro (&Qauto_composition_function);
40add26d 1782
f96ba4c1
KH
1783 DEFVAR_LISP ("auto-composition-function", &Vauto_composition_function,
1784 doc: /* Function to call to compose characters automatically.
9d751859 1785This function is called from the display routine with four arguments:
ddc872ba 1786FROM, TO, WINDOW, and STRING.
335c5470 1787
ddc872ba
KH
1788If STRING is nil, the function must compose characters in the region
1789between FROM and TO in the current buffer.
335c5470 1790
ddc872ba
KH
1791Otherwise, STRING is a string, and FROM and TO are indices into the
1792string. In this case, the function must compose characters in the
1793string. */);
f96ba4c1 1794 Vauto_composition_function = Qnil;
40add26d 1795
58753d74 1796 DEFVAR_LISP ("composition-function-table", &Vcomposition_function_table,
9d751859 1797 doc: /* Char-table of functions for automatic character composition.
58753d74
KH
1798For each character that has to be composed automatically with
1799preceding and/or following characters, this char-table contains
1800a function to call to compose that character.
1801
1802The element at index C in the table, if non-nil, is a list of
1803this form: ([PATTERN PREV-CHARS FUNC] ...)
1804
9d751859 1805PATTERN is a regular expression which C and the surrounding
58753d74
KH
1806characters must match.
1807
895416e3
KH
1808PREV-CHARS is a non-negative integer (less than 4) specifying how many
1809characters before C to check the matching with PATTERN. If it is 0,
1810PATTERN must match C and the following characters. If it is 1,
1811PATTERN must match a character before C and the following characters.
58753d74
KH
1812
1813If PREV-CHARS is 0, PATTERN can be nil, which means that the
1814single character C should be composed.
1815
1816FUNC is a function to return a glyph-string representing a
9d751859 1817composition of the characters that match PATTERN. It is
58753d74
KH
1818called with one argument GSTRING.
1819
1820GSTRING is a template of a glyph-string to return. It is already
1821filled with a proper header for the characters to compose, and
1822glyphs corresponding to those characters one by one. The
9d751859 1823function must return a new glyph-string with the same header as
58753d74
KH
1824GSTRING, or modify GSTRING itself and return it.
1825
1826See also the documentation of `auto-composition-mode'. */);
1827 Vcomposition_function_table = Fmake_char_table (Qnil, Qnil);
1828
ca4c9455
KH
1829 defsubr (&Scompose_region_internal);
1830 defsubr (&Scompose_string_internal);
1831 defsubr (&Sfind_composition_internal);
58753d74 1832 defsubr (&Scomposition_get_gstring);
ca4c9455 1833}
ee6f9c59
KH
1834
1835/* arch-tag: 79cefaf8-ca48-4eed-97e5-d5afb290d272
1836 (do not change this comment) */