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