Add support for defun in thingatpt.el
[emacs.git] / src / composite.c
blobbc5644a4612477bff62fe14e16827afebb889280
1 /* Composite sequence support.
2 Copyright (C) 2001-2011 Free Software Foundation, Inc.
3 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
4 National Institute of Advanced Industrial Science and Technology (AIST)
5 Registration Number H14PRO021
6 Copyright (C) 2003, 2006
7 National Institute of Advanced Industrial Science and Technology (AIST)
8 Registration Number H13PRO009
10 This file is part of GNU Emacs.
12 GNU Emacs is free software: you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation, either version 3 of the License, or
15 (at your option) any later version.
17 GNU Emacs is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 GNU General Public License for more details.
22 You should have received a copy of the GNU General Public License
23 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
25 #include <config.h>
26 #include <setjmp.h>
27 #include "lisp.h"
28 #include "buffer.h"
29 #include "character.h"
30 #include "coding.h"
31 #include "intervals.h"
32 #include "window.h"
33 #include "frame.h"
34 #include "dispextern.h"
35 #include "font.h"
36 #include "termhooks.h"
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.
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).
55 We register a composition when it is displayed, or when the width
56 is required (for instance, to calculate columns).
58 LENGTH -- Length of the composition. This information is used to
59 check the validity of the composition.
61 COMPONENTS -- Character, string, vector, list, or nil.
63 If it is nil, characters in the text are composed relatively
64 according to their metrics in font glyphs.
66 If it is a character or a string, the character or characters
67 in the string are composed relatively.
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.
76 COMPONENTS-VEC -- Vector of integers. In a relative composition,
77 the elements are the characters to be composed. In a rule-base
78 composition, the elements are characters or encoded
79 composition rules.
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.
86 COMPOSITION-ID --Identification number of the composition. It is
87 used as an index to composition_table for the composition.
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
97 of the property value. If the property is invalid,
98 get_composition_id returns -1 without changing the property value.
100 We use two tables to keep the information about composition;
101 composition_hash_table and composition_table.
103 The former is a hash table whose keys are COMPONENTS-VECs and
104 values are the corresponding COMPOSITION-IDs. This hash table is
105 weak, but as each key (COMPONENTS-VEC) is also kept as a value of the
106 `composition' property, it won't be collected as garbage until all
107 bits of text that have the same COMPONENTS-VEC are deleted.
109 The latter is a table of pointers to `struct composition' indexed
110 by COMPOSITION-ID. This structure keeps the other information (see
111 composite.h).
113 In general, a text property holds information about individual
114 characters. But, a `composition' property holds information about
115 a sequence of characters (in this sense, it is like the `intangible'
116 property). That means that we should not share the property value
117 in adjacent compositions -- we can't distinguish them if they have the
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.
124 As the value of the `composition' property holds information about a
125 specific range of text, the value gets invalid if we change the
126 text in the range. We treat the `composition' property as always
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
130 range just shorter. So, we can check the validity of the `composition'
131 property by comparing LENGTH information with the actual length of
132 the composition.
137 Lisp_Object Qcomposition;
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. */
142 struct composition **composition_table;
144 /* The current size of `composition_table'. */
145 static int composition_table_size;
147 /* Number of compositions currently made. */
148 int n_compositions;
150 /* Hash table for compositions. The key is COMPONENTS-VEC of
151 `composition' property. The value is the corresponding
152 COMPOSITION-ID. */
153 Lisp_Object composition_hash_table;
155 Lisp_Object Qauto_composed;
156 Lisp_Object Qauto_composition_function;
157 /* Maximum number of characters to look back for
158 auto-compositions. */
159 #define MAX_AUTO_COMPOSITION_LOOKBACK 3
161 EXFUN (Fremove_list_of_text_properties, 4);
163 /* Temporary variable used in macros COMPOSITION_XXX. */
164 Lisp_Object composition_temp;
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.
172 If the composition is invalid, return -1. */
175 get_composition_id (EMACS_INT charpos, EMACS_INT bytepos, EMACS_INT nchars,
176 Lisp_Object prop, Lisp_Object string)
178 Lisp_Object id, length, components, key, *key_contents;
179 int glyph_len;
180 struct Lisp_Hash_Table *hash_table = XHASH_TABLE (composition_hash_table);
181 int hash_index;
182 unsigned hash_code;
183 struct composition *cmp;
184 EMACS_INT i;
185 int ch;
187 /* PROP should be
188 Form-A: ((LENGTH . COMPONENTS) . MODIFICATION-FUNC)
190 Form-B: (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC))
192 if (nchars == 0 || !CONSP (prop))
193 goto invalid_composition;
195 id = XCAR (prop);
196 if (INTEGERP (id))
198 /* PROP should be Form-B. */
199 if (XINT (id) < 0 || XINT (id) >= n_compositions)
200 goto invalid_composition;
201 return XINT (id);
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;
212 components = XCDR (id);
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))
226 key = Fmake_vector (make_number (nchars), Qnil);
227 if (STRINGP (string))
228 for (i = 0; i < nchars; i++)
230 FETCH_STRING_CHAR_ADVANCE (ch, string, charpos, bytepos);
231 XVECTOR (key)->contents[i] = make_number (ch);
233 else
234 for (i = 0; i < nchars; i++)
236 FETCH_CHAR_ADVANCE (ch, charpos, bytepos);
237 XVECTOR (key)->contents[i] = make_number (ch);
240 else
241 goto invalid_composition;
243 hash_index = hash_lookup (hash_table, key, &hash_code);
244 if (hash_index >= 0)
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);
252 XSETCAR (prop, id);
253 XSETCDR (prop, Fcons (make_number (nchars), Fcons (key, XCDR (prop))));
254 return XINT (id);
257 /* This composition is a new one. We must register it. */
259 /* Check if we have sufficient memory to store this information. */
260 if (composition_table_size == 0)
262 composition_table_size = 256;
263 composition_table
264 = (struct composition **) xmalloc (sizeof (composition_table[0])
265 * composition_table_size);
267 else if (composition_table_size <= n_compositions)
269 composition_table_size += 256;
270 composition_table
271 = (struct composition **) xrealloc (composition_table,
272 sizeof (composition_table[0])
273 * composition_table_size);
276 key_contents = XVECTOR (key)->contents;
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 */
282 if (VECTORP (components)
283 && ASIZE (components) >= 2
284 && VECTORP (AREF (components, 0)))
286 /* COMPONENTS is a glyph-string. */
287 EMACS_UINT len = ASIZE (key);
289 for (i = 1; i < len; i++)
290 if (! VECTORP (AREF (key, i)))
291 goto invalid_composition;
293 else if (VECTORP (components) || CONSP (components))
295 EMACS_UINT len = XVECTOR (key)->size;
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++)
304 if (!INTEGERP (key_contents[i]))
305 goto invalid_composition;
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);
312 XSETCAR (prop, id);
313 XSETCDR (prop, Fcons (make_number (nchars), Fcons (key, XCDR (prop))));
315 /* Register the composition in composition_hash_table. */
316 hash_index = hash_put (hash_table, key, id, hash_code);
318 /* Register the composition in composition_table. */
319 cmp = (struct composition *) xmalloc (sizeof (struct composition));
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;
334 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
336 /* Relative composition. */
337 cmp->width = 0;
338 for (i = 0; i < glyph_len; i++)
340 int this_width;
341 ch = XINT (key_contents[i]);
342 this_width = (ch == '\t' ? 1 : CHAR_WIDTH (ch));
343 if (cmp->width < this_width)
344 cmp->width = this_width;
347 else
349 /* Rule-base composition. */
350 float leftmost = 0.0, rightmost;
352 ch = XINT (key_contents[0]);
353 rightmost = ch != '\t' ? CHAR_WIDTH (ch) : 1;
355 for (i = 1; i < glyph_len; i += 2)
357 int rule, gref, nref, xoff, yoff;
358 int this_width;
359 float this_left;
361 rule = XINT (key_contents[i]);
362 ch = XINT (key_contents[i + 1]);
363 this_width = ch != '\t' ? CHAR_WIDTH (ch) : 1;
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
373 9--10--11 -- center
375 ---3---4---5--- baseline
377 6---7---8 -- descent
379 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
380 this_left = (leftmost
381 + (gref % 3) * (rightmost - leftmost) / 2.0
382 - (nref % 3) * this_width / 2.0);
384 if (this_left < leftmost)
385 leftmost = this_left;
386 if (this_left + this_width > rightmost)
387 rightmost = this_left + this_width;
390 cmp->width = rightmost - leftmost;
391 if (cmp->width < (rightmost - leftmost))
392 /* To get a ceiling integer value. */
393 cmp->width++;
396 composition_table[n_compositions] = cmp;
398 return n_compositions++;
400 invalid_composition:
401 /* Would it be better to remove this `composition' property? */
402 return -1;
406 /* Find a static composition at or nearest to position POS of OBJECT
407 (buffer or string).
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.
413 If there's no composition at POS and LIMIT is negative, return 0.
415 Otherwise, search for a composition forward (LIMIT > POS) or
416 backward (LIMIT < POS). In this case, LIMIT bounds the search.
418 If a composition is found, set *START, *END, and *PROP as above,
419 and return 1, else return 0.
421 This doesn't check the validity of composition. */
424 find_composition (EMACS_INT pos, EMACS_INT limit,
425 EMACS_INT *start, EMACS_INT *end,
426 Lisp_Object *prop, Lisp_Object object)
428 Lisp_Object val;
430 if (get_property_and_range (pos, Qcomposition, prop, start, end, object))
431 return 1;
433 if (limit < 0 || limit == pos)
434 return 0;
436 if (limit > pos) /* search forward */
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;
444 else /* search backward */
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--;
456 get_property_and_range (pos, Qcomposition, prop, start, end, object);
457 return 1;
460 /* Run a proper function to adjust the composition sitting between
461 FROM and TO with property PROP. */
463 static void
464 run_composition_function (EMACS_INT from, EMACS_INT to, Lisp_Object prop)
466 Lisp_Object func;
467 EMACS_INT start, end;
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;
480 if (!NILP (Ffboundp (func)))
481 call2 (func, make_number (from), make_number (to));
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).
488 It also resets the text-property `auto-composed' to a proper region
489 so that automatic character composition works correctly later while
490 displaying the region.
492 This function is called when a buffer text is changed. If the
493 change is deletion, FROM == TO. Otherwise, FROM < TO. */
495 void
496 update_compositions (EMACS_INT from, EMACS_INT to, int check_mask)
498 Lisp_Object prop;
499 EMACS_INT start, end;
500 /* The beginning and end of the region to set the property
501 `auto-composed' to nil. */
502 EMACS_INT min_pos = from, max_pos = to;
504 if (inhibit_modification_hooks)
505 return;
507 /* If FROM and TO are not in a valid range, do nothing. */
508 if (! (BEGV <= from && from <= to && to <= ZV))
509 return;
511 if (check_mask & CHECK_HEAD)
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
519 && find_composition (from - 1, -1, &start, &end, &prop, Qnil)
520 && COMPOSITION_VALID_P (start, end, prop))
522 min_pos = start;
523 if (end > to)
524 max_pos = end;
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;
532 else if (from < ZV
533 && find_composition (from, -1, &start, &from, &prop, Qnil)
534 && COMPOSITION_VALID_P (start, from, prop))
536 if (from > to)
537 max_pos = from;
538 run_composition_function (start, from, prop);
542 if (check_mask & CHECK_INSIDE)
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)
549 && COMPOSITION_VALID_P (start, from, prop)
550 && from < to - 1)
551 run_composition_function (start, from, prop);
554 if (check_mask & CHECK_TAIL)
556 if (from < to
557 && find_composition (to - 1, -1, &start, &end, &prop, Qnil)
558 && COMPOSITION_VALID_P (start, end, prop))
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)
567 Fput_text_property (make_number (start), make_number (to),
568 Qcomposition,
569 Fcons (XCAR (prop), XCDR (prop)), Qnil);
570 max_pos = end;
572 run_composition_function (start, end, prop);
574 else if (to < ZV
575 && find_composition (to, -1, &start, &end, &prop, Qnil)
576 && COMPOSITION_VALID_P (start, end, prop))
578 run_composition_function (start, end, prop);
579 max_pos = end;
582 if (min_pos < max_pos)
584 int count = SPECPDL_INDEX ();
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);
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'. */
601 void
602 make_composition_value_copy (Lisp_Object list)
604 Lisp_Object plist, val;
606 for (; CONSP (list); list = XCDR (list))
608 plist = XCAR (XCDR (XCDR (XCAR (list))));
609 while (CONSP (plist) && CONSP (XCDR (plist)))
611 if (EQ (XCAR (plist), Qcomposition)
612 && (val = XCAR (XCDR (plist)), CONSP (val)))
613 XSETCAR (XCDR (plist), Fcons (XCAR (val), XCDR (val)));
614 plist = XCDR (XCDR (plist));
620 /* Make text in the region between START and END a composition that
621 has COMPONENTS and MODIFICATION-FUNC.
623 If STRING is non-nil, then operate on characters contained between
624 indices START and END in STRING. */
626 void
627 compose_text (EMACS_INT start, EMACS_INT end, Lisp_Object components,
628 Lisp_Object modification_func, Lisp_Object string)
630 Lisp_Object prop;
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);
639 static Lisp_Object autocmp_chars (Lisp_Object, EMACS_INT, EMACS_INT,
640 EMACS_INT, struct window *,
641 struct face *, Lisp_Object);
644 /* Lisp glyph-string handlers */
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. */
650 static Lisp_Object gstring_hash_table;
652 static Lisp_Object gstring_lookup_cache (Lisp_Object);
654 static Lisp_Object
655 gstring_lookup_cache (Lisp_Object header)
657 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
658 int i = hash_lookup (h, header, NULL);
660 return (i >= 0 ? HASH_VALUE (h, i) : Qnil);
663 Lisp_Object
664 composition_gstring_put_cache (Lisp_Object gstring, int len)
666 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
667 unsigned hash;
668 Lisp_Object header, copy;
669 int i;
671 header = LGSTRING_HEADER (gstring);
672 hash = h->hashfn (h, header);
673 if (len < 0)
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;
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;
691 Lisp_Object
692 composition_gstring_from_id (int id)
694 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
696 return HASH_VALUE (h, id);
699 static Lisp_Object fill_gstring_header (Lisp_Object, Lisp_Object,
700 Lisp_Object, Lisp_Object,
701 Lisp_Object);
704 composition_gstring_p (Lisp_Object gstring)
706 Lisp_Object header;
707 int i;
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))
715 && (! FONT_OBJECT_P (LGSTRING_FONT (gstring))
716 && ! CODING_SYSTEM_P (LGSTRING_FONT (gstring))))
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++)
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;
731 return 1;
735 composition_gstring_width (Lisp_Object gstring, EMACS_INT from, EMACS_INT to,
736 struct font_metrics *metrics)
738 Lisp_Object *glyph;
739 int width = 0;
741 if (metrics)
743 Lisp_Object font_object = LGSTRING_FONT (gstring);
745 if (FONT_OBJECT_P (font_object))
747 struct font *font = XFONT_OBJECT (font_object);
749 metrics->ascent = font->ascent;
750 metrics->descent = font->descent;
752 else
754 metrics->ascent = 1;
755 metrics->descent = 0;
757 metrics->width = metrics->lbearing = metrics->rbearing = 0;
759 for (glyph = &LGSTRING_GLYPH (gstring, from); from < to; from++, glyph++)
761 int x;
763 if (NILP (LGLYPH_ADJUSTMENT (*glyph)))
764 width += LGLYPH_WIDTH (*glyph);
765 else
766 width += LGLYPH_WADJUST (*glyph);
767 if (metrics)
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;
779 x = LGLYPH_DESCENT (*glyph) + LGLYPH_YOFF (*glyph);
780 if (metrics->descent < x)
781 metrics->descent = x;
784 return width;
788 static Lisp_Object gstring_work;
789 static Lisp_Object gstring_work_headers;
791 static Lisp_Object
792 fill_gstring_header (Lisp_Object header, Lisp_Object start, Lisp_Object end, Lisp_Object font_object, Lisp_Object string)
794 EMACS_INT from, to, from_byte;
795 EMACS_INT len, i;
797 if (NILP (string))
799 if (NILP (BVAR (current_buffer, enable_multibyte_characters)))
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);
806 else
808 CHECK_STRING (string);
809 if (! STRING_MULTIBYTE (string))
810 error ("Attempt to shape unibyte text");
811 /* FROM and TO are checked by the caller. */
812 from = XINT (start);
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);
819 len = to - from;
820 if (len == 0)
821 error ("Attempt to shape zero-length text");
822 if (VECTORP (header))
824 if (ASIZE (header) != len + 1)
825 args_out_of_range (header, make_number (len + 1));
827 else
829 if (len <= 8)
830 header = AREF (gstring_work_headers, len - 1);
831 else
832 header = Fmake_vector (make_number (len + 1), Qnil);
835 ASET (header, 0, font_object);
836 for (i = 0; i < len; i++)
838 int c;
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));
846 return header;
849 static void
850 fill_gstring_body (Lisp_Object gstring)
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;
857 for (i = 0; i < len; i++)
859 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
860 EMACS_INT c = XINT (AREF (header, i + 1));
862 if (NILP (g))
864 g = LGLYPH_NEW ();
865 LGSTRING_SET_GLYPH (gstring, i, g);
867 LGLYPH_SET_FROM (g, i);
868 LGLYPH_SET_TO (g, i);
869 LGLYPH_SET_CHAR (g, c);
870 if (FONT_OBJECT_P (font_object))
872 font_fill_lglyph_metrics (g, font_object);
874 else
876 int width = XFASTINT (CHAR_TABLE_REF (Vchar_width_table, c));
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);
885 LGLYPH_SET_ADJUSTMENT (g, Qnil);
887 if (i < LGSTRING_GLYPH_LEN (gstring))
888 LGSTRING_SET_GLYPH (gstring, i, Qnil);
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. */
899 static Lisp_Object
900 autocmp_chars (Lisp_Object rule, EMACS_INT charpos, EMACS_INT bytepos, EMACS_INT limit, struct window *win, struct face *face, Lisp_Object string)
902 int count = SPECPDL_INDEX ();
903 FRAME_PTR f = XFRAME (win->frame);
904 Lisp_Object pos = make_number (charpos);
905 EMACS_INT to;
906 EMACS_INT pt = PT, pt_byte = PT_BYTE;
907 Lisp_Object re, font_object, lgstring;
908 EMACS_INT len;
910 record_unwind_save_match_data ();
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)
919 if (NILP (string))
920 len = BYTE_TO_CHAR (bytepos + len) - charpos;
921 else
922 len = string_byte_to_char (string, bytepos + len) - charpos;
924 if (len <= 0)
925 return unbind_to (count, Qnil);
926 to = limit = charpos + len;
927 #ifdef HAVE_WINDOW_SYSTEM
928 if (FRAME_WINDOW_P (f))
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);
937 else
938 #endif /* not HAVE_WINDOW_SYSTEM */
939 font_object = win->frame;
940 lgstring = Fcomposition_get_gstring (pos, make_number (to), font_object,
941 string);
942 if (NILP (LGSTRING_ID (lgstring)))
944 Lisp_Object args[6];
946 /* Save point as marker before calling out to lisp. */
947 if (NILP (string))
949 Lisp_Object m = Fmake_marker ();
950 set_marker_both (m, Qnil, pt, pt_byte);
951 record_unwind_protect (restore_point_unwind, m);
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);
964 return unbind_to (count, lgstring);
967 static Lisp_Object _work_val;
968 static int _work_char;
970 /* 1 iff the character C is composable. Characters of general
971 category Z? or C? are not composable except for ZWNJ and ZWJ. */
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')))
980 /* Update cmp_it->stop_pos to the next position after CHARPOS (and
981 BYTEPOS) where character composition may happen. If BYTEPOS is
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.
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. */
993 void
994 composition_compute_stop_pos (struct composition_it *cmp_it, EMACS_INT charpos, EMACS_INT bytepos, EMACS_INT endpos, Lisp_Object string)
996 EMACS_INT start, end, c;
997 Lisp_Object prop, val;
998 /* This is from forward_to_next_line_start in xdisp.c. */
999 const int MAX_NEWLINE_DISTANCE = 500;
1001 if (charpos < endpos)
1003 if (endpos > charpos + MAX_NEWLINE_DISTANCE)
1004 endpos = charpos + MAX_NEWLINE_DISTANCE;
1006 else if (endpos < charpos)
1008 /* We search backward for a position to check composition. */
1009 if (endpos < 0)
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). */
1017 cmp_it->id = -1;
1018 cmp_it->ch = -2;
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)
1026 && COMPOSITION_VALID_P (start, end, prop))
1028 cmp_it->stop_pos = endpos = start;
1029 cmp_it->ch = -1;
1031 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
1032 || NILP (Vauto_composition_mode))
1033 return;
1034 if (bytepos < 0)
1036 if (NILP (string))
1037 bytepos = CHAR_TO_BYTE (charpos);
1038 else
1039 bytepos = string_char_to_byte (string, charpos);
1042 start = charpos;
1043 if (charpos < endpos)
1045 /* Forward search. */
1046 while (charpos < endpos)
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')
1054 cmp_it->ch = -2;
1055 break;
1057 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1058 if (! NILP (val))
1060 Lisp_Object elt;
1061 int ridx;
1063 for (ridx = 0; CONSP (val); val = XCDR (val), ridx++)
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;
1071 if (CONSP (val))
1073 cmp_it->rule_idx = ridx;
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;
1081 if (charpos == endpos)
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;
1092 else if (charpos > endpos)
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
1097 character is that set_iterator_to_next works in reverse order,
1098 and thus we must stop at the last character for composition
1099 check. */
1100 unsigned char *p;
1101 int len;
1102 /* Limit byte position used in fast_looking_at. This is the
1103 byte position of the character after START. */
1104 EMACS_INT limit;
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))
1114 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1115 if (! NILP (val))
1117 Lisp_Object elt;
1118 int ridx, back, blen;
1120 for (ridx = 0; CONSP (val); val = XCDR (val), ridx++)
1122 elt = XCAR (val);
1123 if (VECTORP (elt) && ASIZE (elt) == 3
1124 && NATNUMP (AREF (elt, 1))
1125 && charpos - (back = XFASTINT (AREF (elt, 1))) > endpos)
1127 EMACS_INT cpos = charpos - back, bpos;
1129 if (back == 0)
1130 bpos = bytepos;
1131 else
1132 bpos = (NILP (string) ? CHAR_TO_BYTE (cpos)
1133 : string_char_to_byte (string, cpos));
1134 if (STRINGP (AREF (elt, 0)))
1135 blen = fast_looking_at (AREF (elt, 0), cpos, bpos,
1136 start + 1, limit, string);
1137 else
1138 blen = 1;
1139 if (blen > 0)
1141 /* Make CPOS point to the last character of
1142 match. Note that BLEN is byte-length. */
1143 if (blen > 1)
1145 bpos += blen;
1146 if (NILP (string))
1147 cpos = BYTE_TO_CHAR (bpos) - 1;
1148 else
1149 cpos = string_byte_to_char (string, bpos) - 1;
1151 back = cpos - (charpos - back);
1152 if (cmp_it->stop_pos < cpos
1153 || (cmp_it->stop_pos == cpos
1154 && cmp_it->lookback < back))
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;
1166 if (charpos - 1 == endpos)
1167 break;
1168 if (STRINGP (string))
1170 p--, bytepos--;
1171 while (! CHAR_HEAD_P (*p))
1172 p--, bytepos--;
1173 charpos--;
1175 else
1177 DEC_BOTH (charpos, bytepos);
1178 p = BYTE_POS_ADDR (bytepos);
1180 c = STRING_CHAR (p);
1182 if (cmp_it->ch >= 0)
1183 /* We found a position to check. */
1184 return;
1185 /* Skip all uncomposable characters. */
1186 if (NILP (string))
1188 while (charpos - 1 > endpos && ! CHAR_COMPOSABLE_P (c))
1190 DEC_BOTH (charpos, bytepos);
1191 c = FETCH_MULTIBYTE_CHAR (bytepos);
1194 else
1196 while (charpos - 1 > endpos && ! CHAR_COMPOSABLE_P (c))
1198 p--;
1199 while (! CHAR_HEAD_P (*p))
1200 p--;
1201 charpos--;
1202 c = STRING_CHAR (p);
1206 cmp_it->stop_pos = charpos;
1209 /* Check if the character at CHARPOS (and BYTEPOS) is composed
1210 (possibly with the following characters) on window W. ENDPOS limits
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.
1216 If the character is composed, setup members of CMP_IT (id, nglyphs,
1217 from, to, reversed_p), and return 1. Otherwise, update
1218 CMP_IT->stop_pos, and return 0. */
1221 composition_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)
1223 if (endpos < 0)
1224 endpos = NILP (string) ? BEGV : 0;
1226 if (cmp_it->ch == -2)
1228 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string);
1229 if (cmp_it->ch == -2 || cmp_it->stop_pos != charpos)
1230 /* The current position is not composed. */
1231 return 0;
1234 if (cmp_it->ch < 0)
1236 /* We are looking at a static composition. */
1237 EMACS_INT start, end;
1238 Lisp_Object prop;
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;
1248 else if (w)
1250 Lisp_Object lgstring = Qnil;
1251 Lisp_Object val, elt;
1252 int i;
1254 val = CHAR_TABLE_REF (Vcomposition_function_table, cmp_it->ch);
1255 for (i = 0; i < cmp_it->rule_idx; i++, val = XCDR (val));
1256 if (charpos < endpos)
1258 for (; CONSP (val); val = XCDR (val))
1260 elt = XCAR (val);
1261 if (! VECTORP (elt) || ASIZE (elt) != 3
1262 || ! INTEGERP (AREF (elt, 1)))
1263 continue;
1264 if (XFASTINT (AREF (elt, 1)) != cmp_it->lookback)
1265 goto no_composition;
1266 lgstring = autocmp_chars (elt, charpos, bytepos, endpos,
1267 w, face, string);
1268 if (composition_gstring_p (lgstring))
1269 break;
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. */
1275 cmp_it->reversed_p = 0;
1277 else
1279 EMACS_INT cpos = charpos, bpos = bytepos;
1281 while (1)
1283 elt = XCAR (val);
1284 if (cmp_it->lookback > 0)
1286 cpos = charpos - cmp_it->lookback;
1287 if (STRINGP (string))
1288 bpos = string_char_to_byte (string, cpos);
1289 else
1290 bpos = CHAR_TO_BYTE (cpos);
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)
1300 goto no_composition;
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));
1310 cmp_it->reversed_p = 1;
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)))
1319 break;
1320 cmp_it->nglyphs = i;
1321 cmp_it->from = 0;
1322 cmp_it->to = i;
1324 else
1325 goto no_composition;
1326 return 1;
1328 no_composition:
1329 if (charpos == endpos)
1330 return 0;
1331 if (charpos < endpos)
1333 charpos++;
1334 if (NILP (string))
1335 INC_POS (bytepos);
1336 else
1337 bytepos += BYTES_BY_CHAR_HEAD (*(SDATA (string) + bytepos));
1339 else
1341 charpos--;
1342 /* BYTEPOS is calculated in composition_compute_stop_pos */
1343 bytepos = -1;
1345 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string);
1346 return 0;
1349 /* Update charpos, nchars, nbytes, and width of the current grapheme
1350 cluster.
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.
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.
1361 The return value is the character code of the first character of
1362 the cluster, or -1 if the composition is somehow broken. */
1365 composition_update_it (struct composition_it *cmp_it, EMACS_INT charpos, EMACS_INT bytepos, Lisp_Object string)
1367 int i, c IF_LINT (= 0);
1369 if (cmp_it->ch < 0)
1371 /* static composition */
1372 struct composition *cmp = composition_table[cmp_it->id];
1374 cmp_it->charpos = charpos;
1375 cmp_it->to = cmp_it->nglyphs;
1376 if (cmp_it->nglyphs == 0)
1377 c = -1;
1378 else
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 = ' ';
1386 cmp_it->width = cmp->width;
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;
1393 else
1395 /* automatic composition */
1396 Lisp_Object gstring = composition_gstring_from_id (cmp_it->id);
1397 Lisp_Object glyph;
1398 EMACS_INT from;
1400 if (cmp_it->nglyphs == 0)
1402 cmp_it->nchars = LGSTRING_CHAR_LEN (gstring);
1403 cmp_it->width = 0;
1404 cmp_it->from = cmp_it->to = 0;
1405 return -1;
1407 if (! cmp_it->reversed_p)
1409 glyph = LGSTRING_GLYPH (gstring, cmp_it->from);
1410 from = LGLYPH_FROM (glyph);
1411 for (cmp_it->to = cmp_it->from + 1; cmp_it->to < cmp_it->nglyphs;
1412 cmp_it->to++)
1414 glyph = LGSTRING_GLYPH (gstring, cmp_it->to);
1415 if (LGLYPH_FROM (glyph) != from)
1416 break;
1418 cmp_it->charpos = charpos;
1420 else
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--)
1428 glyph = LGSTRING_GLYPH (gstring, cmp_it->from - 1);
1429 if (LGLYPH_FROM (glyph) != from)
1430 break;
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--)
1439 c = XINT (LGSTRING_CHAR (gstring, i));
1440 cmp_it->nbytes += CHAR_BYTES (c);
1441 cmp_it->width += CHAR_WIDTH (c);
1444 return c;
1448 struct position_record
1450 EMACS_INT pos, pos_byte;
1451 unsigned char *p;
1454 /* Update the members of POSITION to the next character boundary. */
1455 #define FORWARD_CHAR(POSITION, STOP) \
1456 do { \
1457 (POSITION).pos++; \
1458 if ((POSITION).pos == (STOP)) \
1460 (POSITION).p = GAP_END_ADDR; \
1461 (POSITION).pos_byte = GPT_BYTE; \
1463 else \
1465 (POSITION).pos_byte += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \
1466 (POSITION).p += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \
1468 } while (0)
1470 /* Update the members of POSITION to the previous character boundary. */
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)
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. */
1486 static int
1487 find_automatic_composition (EMACS_INT pos, EMACS_INT limit, EMACS_INT *start, EMACS_INT *end, Lisp_Object *gstring, Lisp_Object string)
1489 EMACS_INT head, tail, stop;
1490 /* Limit to check a composition after POS. */
1491 EMACS_INT fore_check_limit;
1492 struct position_record orig, cur;
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});
1499 Lisp_Object check_val, val, elt;
1500 int c;
1501 Lisp_Object window;
1502 struct window *w;
1504 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1505 if (NILP (window))
1506 return 0;
1507 w = XWINDOW (window);
1509 orig.pos = pos;
1510 if (NILP (string))
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);
1516 else
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;
1522 if (limit < pos)
1523 fore_check_limit = min (tail, pos + MAX_AUTO_COMPOSITION_LOOKBACK);
1524 else
1525 fore_check_limit = min (tail, limit + MAX_AUTO_COMPOSITION_LOOKBACK);
1526 cur = orig;
1528 retry:
1529 check_val = Qnil;
1530 /* At first, check if POS is composable. */
1531 c = STRING_CHAR (cur.p);
1532 if (! CHAR_COMPOSABLE_P (c))
1534 if (limit < 0)
1535 return 0;
1536 if (limit >= cur.pos)
1537 goto search_forward;
1539 else
1541 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1542 if (! NILP (val))
1543 check_val = val, check = cur;
1544 else
1545 while (cur.pos + 1 < fore_check_limit)
1547 EMACS_INT b, e;
1549 FORWARD_CHAR (cur, stop);
1550 if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e,
1551 Qnil)
1552 && COMPOSITION_VALID_P (b, e, val))
1554 fore_check_limit = cur.pos;
1555 break;
1557 c = STRING_CHAR (cur.p);
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;
1566 cur = orig;
1568 /* Rewind back to the position where we can safely search forward
1569 for compositions. */
1570 while (cur.pos > head)
1572 EMACS_INT b, e;
1574 BACKWARD_CHAR (cur, stop);
1575 if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e, Qnil)
1576 && COMPOSITION_VALID_P (b, e, val))
1577 break;
1578 c = STRING_CHAR (cur.p);
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;
1585 prev = cur;
1586 /* Now search forward. */
1587 search_forward:
1588 *gstring = Qnil;
1589 if (! NILP (check_val) || limit >= orig.pos)
1591 if (NILP (check_val))
1592 cur = orig;
1593 else
1594 cur = check;
1595 while (cur.pos < fore_check_limit)
1597 int need_adjustment = 0;
1599 if (NILP (check_val))
1601 c = STRING_CHAR (cur.p);
1602 check_val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1604 for (; CONSP (check_val); check_val = XCDR (check_val))
1606 elt = XCAR (check_val);
1607 if (VECTORP (elt) && ASIZE (elt) == 3 && NATNUMP (AREF (elt, 1))
1608 && cur.pos - XFASTINT (AREF (elt, 1)) >= head)
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);
1615 val = autocmp_chars (elt, check.pos, check.pos_byte,
1616 tail, w, NULL, string);
1617 need_adjustment = 1;
1618 if (! NILP (val))
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;
1632 if (need_adjustment)
1634 /* As we have called Lisp, there's a possibility that
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;
1641 if (! CONSP (check_val))
1642 FORWARD_CHAR (cur, stop);
1643 check_val = Qnil;
1646 if (! NILP (*gstring))
1647 return (limit >= 0 || (*start <= orig.pos && *end > orig.pos));
1648 if (limit >= 0 && limit < orig.pos && prev.pos > head)
1650 cur = prev;
1651 BACKWARD_CHAR (cur, stop);
1652 orig = cur;
1653 fore_check_limit = orig.pos;
1654 goto retry;
1656 return 0;
1659 /* Return the adjusted point provided that point is moved from LAST_PT
1660 to NEW_PT. */
1662 EMACS_INT
1663 composition_adjust_point (EMACS_INT last_pt, EMACS_INT new_pt)
1665 EMACS_INT beg, end;
1666 Lisp_Object val;
1667 int i;
1669 if (new_pt == BEGV || new_pt == ZV)
1670 return new_pt;
1672 /* At first check the static composition. */
1673 if (get_property_and_range (new_pt, Qcomposition, &val, &beg, &end, Qnil)
1674 && COMPOSITION_VALID_P (beg, end, val))
1676 if (beg < new_pt /* && end > new_pt <- It's always the case. */
1677 && (last_pt <= beg || last_pt >= end))
1678 return (new_pt < last_pt ? beg : end);
1679 return new_pt;
1682 if (NILP (BVAR (current_buffer, enable_multibyte_characters))
1683 || NILP (Vauto_composition_mode))
1684 return new_pt;
1686 /* Next check the automatic composition. */
1687 if (! find_automatic_composition (new_pt, (EMACS_INT) -1, &beg, &end, &val,
1688 Qnil)
1689 || beg == new_pt)
1690 return new_pt;
1691 for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++)
1693 Lisp_Object glyph = LGSTRING_GLYPH (val, i);
1695 if (NILP (glyph))
1696 break;
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
1701 ? beg + LGLYPH_FROM (glyph)
1702 : beg + LGLYPH_TO (glyph) + 1);
1704 return new_pt;
1707 DEFUN ("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.
1710 If the glyph string is for graphic display, FONT-OBJECT must be
1711 a font-object to use for those characters.
1712 Otherwise (for terminal display), FONT-OBJECT must be a terminal ID, a
1713 frame, or nil for the selected frame's terminal device.
1715 If the optional 4th argument STRING is not nil, it is a string
1716 containing the target characters between indices FROM and TO.
1718 A glyph-string is a vector containing information about how to display
1719 a specific character sequence. The format is:
1720 [HEADER ID GLYPH ...]
1722 HEADER is a vector of this form:
1723 [FONT-OBJECT CHAR ...]
1724 where
1725 FONT-OBJECT is a font-object for all glyphs in the glyph-string,
1726 or the terminal coding system of the specified terminal.
1727 CHARs are characters to be composed by GLYPHs.
1729 ID is an identification number of the glyph-string. It may be nil if
1730 not yet shaped.
1732 GLYPH is a vector whose elements have this form:
1733 [ FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT
1734 [ [X-OFF Y-OFF WADJUST] | nil] ]
1735 where
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.
1740 X-OFF and Y-OFF are offsets to the base position for the glyph.
1741 WADJUST is the adjustment to the normal width of the glyph.
1743 If GLYPH is nil, the remaining elements of the glyph-string vector
1744 should be ignored. */)
1745 (Lisp_Object from, Lisp_Object to, Lisp_Object font_object, Lisp_Object string)
1747 Lisp_Object gstring, header;
1748 EMACS_INT frompos, topos;
1750 CHECK_NATNUM (from);
1751 CHECK_NATNUM (to);
1752 if (! FONT_OBJECT_P (font_object))
1754 struct coding_system *coding;
1755 struct terminal *terminal = get_terminal (font_object, 1);
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);
1763 header = fill_gstring_header (Qnil, from, to, font_object, string);
1764 gstring = gstring_lookup_cache (header);
1765 if (! NILP (gstring))
1766 return gstring;
1768 frompos = XINT (from);
1769 topos = XINT (to);
1770 if (LGSTRING_GLYPH_LEN (gstring_work) < topos - frompos)
1771 gstring_work = Fmake_vector (make_number (topos - frompos + 2), Qnil);
1772 LGSTRING_SET_HEADER (gstring_work, header);
1773 LGSTRING_SET_ID (gstring_work, Qnil);
1774 fill_gstring_body (gstring_work);
1775 return gstring_work;
1779 /* Emacs Lisp APIs. */
1781 DEFUN ("compose-region-internal", Fcompose_region_internal,
1782 Scompose_region_internal, 2, 4, 0,
1783 doc: /* Internal use only.
1785 Compose text in the region between START and END.
1786 Optional 3rd and 4th arguments are COMPONENTS and MODIFICATION-FUNC
1787 for the composition. See `compose-region' for more details. */)
1788 (Lisp_Object start, Lisp_Object end, Lisp_Object components, Lisp_Object modification_func)
1790 validate_region (&start, &end);
1791 if (!NILP (components)
1792 && !INTEGERP (components)
1793 && !CONSP (components)
1794 && !STRINGP (components))
1795 CHECK_VECTOR (components);
1797 compose_text (XINT (start), XINT (end), components, modification_func, Qnil);
1798 return Qnil;
1801 DEFUN ("compose-string-internal", Fcompose_string_internal,
1802 Scompose_string_internal, 3, 5, 0,
1803 doc: /* Internal use only.
1805 Compose text between indices START and END of STRING.
1806 Optional 4th and 5th arguments are COMPONENTS and MODIFICATION-FUNC
1807 for the composition. See `compose-string' for more details. */)
1808 (Lisp_Object string, Lisp_Object start, Lisp_Object end, Lisp_Object components, Lisp_Object modification_func)
1810 CHECK_STRING (string);
1811 CHECK_NUMBER (start);
1812 CHECK_NUMBER (end);
1814 if (XINT (start) < 0 ||
1815 XINT (start) > XINT (end)
1816 || XINT (end) > SCHARS (string))
1817 args_out_of_range (start, end);
1819 compose_text (XINT (start), XINT (end), components, modification_func, string);
1820 return string;
1823 DEFUN ("find-composition-internal", Ffind_composition_internal,
1824 Sfind_composition_internal, 4, 4, 0,
1825 doc: /* Internal use only.
1827 Return information about composition at or nearest to position POS.
1828 See `find-composition' for more details. */)
1829 (Lisp_Object pos, Lisp_Object limit, Lisp_Object string, Lisp_Object detail_p)
1831 Lisp_Object prop, tail, gstring;
1832 EMACS_INT start, end, from, to;
1833 int id;
1835 CHECK_NUMBER_COERCE_MARKER (pos);
1836 from = XINT (pos);
1837 if (!NILP (limit))
1839 CHECK_NUMBER_COERCE_MARKER (limit);
1840 to = XINT (limit);
1842 else
1843 to = -1;
1845 if (!NILP (string))
1847 CHECK_STRING (string);
1848 if (XINT (pos) < 0 || XINT (pos) > SCHARS (string))
1849 args_out_of_range (string, pos);
1851 else
1853 if (XINT (pos) < BEGV || XINT (pos) > ZV)
1854 args_out_of_range (Fcurrent_buffer (), pos);
1857 if (!find_composition (from, to, &start, &end, &prop, string))
1859 if (!NILP (BVAR (current_buffer, enable_multibyte_characters))
1860 && ! NILP (Vauto_composition_mode)
1861 && find_automatic_composition (from, to, &start, &end, &gstring,
1862 string))
1863 return list3 (make_number (start), make_number (end), gstring);
1864 return Qnil;
1866 if ((end <= XINT (pos) || start > XINT (pos)))
1868 EMACS_INT s, e;
1870 if (find_automatic_composition (from, to, &s, &e, &gstring, string)
1871 && (e <= XINT (pos) ? e > end : s < start))
1872 return list3 (make_number (s), make_number (e), gstring);
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)));
1881 if (COMPOSITION_REGISTERD_P (prop))
1882 id = COMPOSITION_ID (prop);
1883 else
1885 EMACS_INT start_byte = (NILP (string)
1886 ? CHAR_TO_BYTE (start)
1887 : string_char_to_byte (string, start));
1888 id = get_composition_id (start, start_byte, end - start, prop, string);
1891 if (id >= 0)
1893 Lisp_Object components, relative_p, mod_func;
1894 enum composition_method method = COMPOSITION_METHOD (prop);
1895 int width = composition_table[id]->width;
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))));
1906 else
1907 tail = Qnil;
1909 return Fcons (make_number (start), Fcons (make_number (end), tail));
1913 void
1914 syms_of_composite (void)
1916 int i;
1918 Qcomposition = intern_c_string ("composition");
1919 staticpro (&Qcomposition);
1921 /* Make a hash table for static composition. */
1923 Lisp_Object args[6];
1925 args[0] = QCtest;
1926 args[1] = Qequal;
1927 args[2] = QCweakness;
1928 /* We used to make the hash table weak so that unreferenced
1929 compositions can be garbage-collected. But, usually once
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. */
1933 args[3] = Qnil;
1934 args[4] = QCsize;
1935 args[5] = make_number (311);
1936 composition_hash_table = Fmake_hash_table (6, args);
1937 staticpro (&composition_hash_table);
1940 /* Make a hash table for glyph-string. */
1942 Lisp_Object args[6];
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);
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);
1960 /* Text property `composition' should be nonsticky by default. */
1961 Vtext_property_default_nonsticky
1962 = Fcons (Fcons (Qcomposition, Qt), Vtext_property_default_nonsticky);
1964 DEFVAR_LISP ("compose-chars-after-function", Vcompose_chars_after_function,
1965 doc: /* Function to adjust composition of buffer text.
1967 This function is called with three arguments: FROM, TO, and OBJECT.
1968 FROM and TO specify the range of text whose composition should be
1969 adjusted. OBJECT, if non-nil, is a string that contains the text.
1971 This function is called after a text with `composition' property is
1972 inserted or deleted to keep `composition' property of buffer text
1973 valid.
1975 The default value is the function `compose-chars-after'. */);
1976 Vcompose_chars_after_function = intern_c_string ("compose-chars-after");
1978 Qauto_composed = intern_c_string ("auto-composed");
1979 staticpro (&Qauto_composed);
1981 Qauto_composition_function = intern_c_string ("auto-composition-function");
1982 staticpro (&Qauto_composition_function);
1984 DEFVAR_LISP ("auto-composition-mode", Vauto_composition_mode,
1985 doc: /* Non-nil if Auto-Composition mode is enabled.
1986 Use the command `auto-composition-mode' to change this variable. */);
1987 Vauto_composition_mode = Qt;
1989 DEFVAR_LISP ("auto-composition-function", Vauto_composition_function,
1990 doc: /* Function to call to compose characters automatically.
1991 This function is called from the display routine with four arguments:
1992 FROM, TO, WINDOW, and STRING.
1994 If STRING is nil, the function must compose characters in the region
1995 between FROM and TO in the current buffer.
1997 Otherwise, STRING is a string, and FROM and TO are indices into the
1998 string. In this case, the function must compose characters in the
1999 string. */);
2000 Vauto_composition_function = Qnil;
2002 DEFVAR_LISP ("composition-function-table", Vcomposition_function_table,
2003 doc: /* Char-table of functions for automatic character composition.
2004 For each character that has to be composed automatically with
2005 preceding and/or following characters, this char-table contains
2006 a function to call to compose that character.
2008 The element at index C in the table, if non-nil, is a list of
2009 composition rules of this form: ([PATTERN PREV-CHARS FUNC] ...)
2011 PATTERN is a regular expression which C and the surrounding
2012 characters must match.
2014 PREV-CHARS is a non-negative integer (less than 4) specifying how many
2015 characters before C to check the matching with PATTERN. If it is 0,
2016 PATTERN must match C and the following characters. If it is 1,
2017 PATTERN must match a character before C and the following characters.
2019 If PREV-CHARS is 0, PATTERN can be nil, which means that the
2020 single character C should be composed.
2022 FUNC is a function to return a glyph-string representing a
2023 composition of the characters that match PATTERN. It is
2024 called with one argument GSTRING.
2026 GSTRING is a template of a glyph-string to return. It is already
2027 filled with a proper header for the characters to compose, and
2028 glyphs corresponding to those characters one by one. The
2029 function must return a new glyph-string with the same header as
2030 GSTRING, or modify GSTRING itself and return it.
2032 See also the documentation of `auto-composition-mode'. */);
2033 Vcomposition_function_table = Fmake_char_table (Qnil, Qnil);
2035 defsubr (&Scompose_region_internal);
2036 defsubr (&Scompose_string_internal);
2037 defsubr (&Sfind_composition_internal);
2038 defsubr (&Scomposition_get_gstring);