Add Bug#.
[emacs.git] / src / composite.c
bloba654b5e8088d7db704182c408cf178acd93b51bd
1 /* Composite sequence support.
2 Copyright (C) 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2008, 2009 Free Software Foundation, Inc.
4 Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
5 National Institute of Advanced Industrial Science and Technology (AIST)
6 Registration Number H14PRO021
7 Copyright (C) 2003, 2006
8 National Institute of Advanced Industrial Science and Technology (AIST)
9 Registration Number H13PRO009
11 This file is part of GNU Emacs.
13 GNU Emacs is free software: you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation, either version 3 of the License, or
16 (at your option) any later version.
18 GNU Emacs is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */
26 #include <config.h>
27 #include <setjmp.h>
28 #include "lisp.h"
29 #include "buffer.h"
30 #include "character.h"
31 #include "coding.h"
32 #include "intervals.h"
33 #include "window.h"
34 #include "frame.h"
35 #include "dispextern.h"
36 #include "font.h"
37 #include "termhooks.h"
40 /* Emacs uses special text property `composition' to support character
41 composition. A sequence of characters that have the same (i.e. eq)
42 `composition' property value is treated as a single composite
43 sequence (we call it just `composition' here after). Characters in
44 a composition are all composed somehow on the screen.
46 The property value has this form when the composition is made:
47 ((LENGTH . COMPONENTS) . MODIFICATION-FUNC)
48 then turns to this form:
49 (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC))
50 when the composition is registered in composition_hash_table and
51 composition_table. These rather peculiar structures were designed
52 to make it easy to distinguish them quickly (we can do that by
53 checking only the first element) and to extract LENGTH (from the
54 former form) and COMPOSITION-ID (from the latter form).
56 We register a composition when it is displayed, or when the width
57 is required (for instance, to calculate columns).
59 LENGTH -- Length of the composition. This information is used to
60 check the validity of the composition.
62 COMPONENTS -- Character, string, vector, list, or nil.
64 If it is nil, characters in the text are composed relatively
65 according to their metrics in font glyphs.
67 If it is a character or a string, the character or characters
68 in the string are composed relatively.
70 If it is a vector or list of integers, the element is a
71 character or an encoded composition rule. The characters are
72 composed according to the rules. (2N)th elements are
73 characters to be composed and (2N+1)th elements are
74 composition rules to tell how to compose (2N+2)th element with
75 the previously composed 2N glyphs.
77 COMPONENTS-VEC -- Vector of integers. In relative composition, the
78 elements are characters to be composed. In rule-base
79 composition, the elements are characters or encoded
80 composition rules.
82 MODIFICATION-FUNC -- If non nil, it is a function to call when the
83 composition gets invalid after a modification in a buffer. If
84 it is nil, a function in `composition-function-table' of the
85 first character in the sequence is called.
87 COMPOSITION-ID --Identification number of the composition. It is
88 used as an index to composition_table for the composition.
90 When Emacs has to display a composition or has to know its
91 displaying width, the function get_composition_id is called. It
92 returns COMPOSITION-ID so that the caller can access the
93 information about the composition through composition_table. If a
94 COMPOSITION-ID has not yet been assigned to the composition,
95 get_composition_id checks the validity of `composition' property,
96 and, if valid, assigns a new ID, registers the information in
97 composition_hash_table and composition_table, and changes the form
98 of the property value. If the property is invalid, return -1
99 without changing the property value.
101 We use two tables to keep information about composition;
102 composition_hash_table and composition_table.
104 The former is a hash table in which keys are COMPONENTS-VECs and
105 values are the corresponding COMPOSITION-IDs. This hash table is
106 weak, but as each key (COMPONENTS-VEC) is also kept as a value of the
107 `composition' property, it won't be collected as garbage until all
108 bits of text that have the same COMPONENTS-VEC are deleted.
110 The latter is a table of pointers to `struct composition' indexed
111 by COMPOSITION-ID. This structure keeps the other information (see
112 composite.h).
114 In general, a text property holds information about individual
115 characters. But, a `composition' property holds information about
116 a sequence of characters (in this sense, it is like the `intangible'
117 property). That means that we should not share the property value
118 in adjacent compositions -- we can't distinguish them if they have the
119 same property. So, after any changes, we call
120 `update_compositions' and change a property of one of adjacent
121 compositions to a copy of it. This function also runs a proper
122 composition modification function to make a composition that gets
123 invalid by the change valid again.
125 As the value of the `composition' property holds information about a
126 specific range of text, the value gets invalid if we change the
127 text in the range. We treat the `composition' property as always
128 rear-nonsticky (currently by setting default-text-properties to
129 (rear-nonsticky (composition))) and we never make properties of
130 adjacent compositions identical. Thus, any such changes make the
131 range just shorter. So, we can check the validity of the `composition'
132 property by comparing LENGTH information with the actual length of
133 the composition.
138 Lisp_Object Qcomposition;
140 /* Table of pointers to the structure `composition' indexed by
141 COMPOSITION-ID. This structure is for storing information about
142 each composition except for COMPONENTS-VEC. */
143 struct composition **composition_table;
145 /* The current size of `composition_table'. */
146 static int composition_table_size;
148 /* Number of compositions currently made. */
149 int n_compositions;
151 /* Hash table for compositions. The key is COMPONENTS-VEC of
152 `composition' property. The value is the corresponding
153 COMPOSITION-ID. */
154 Lisp_Object composition_hash_table;
156 /* Function to call to adjust composition. */
157 Lisp_Object Vcompose_chars_after_function;
159 Lisp_Object Qauto_composed;
160 Lisp_Object Vauto_composition_function;
161 Lisp_Object Qauto_composition_function;
162 Lisp_Object Vcomposition_function_table;
164 /* Maxinum number of characters to lookback to check
165 auto-composition. */
166 #define MAX_AUTO_COMPOSITION_LOOKBACK 3
168 EXFUN (Fremove_list_of_text_properties, 4);
170 /* Temporary variable used in macros COMPOSITION_XXX. */
171 Lisp_Object composition_temp;
174 /* Return COMPOSITION-ID of a composition at buffer position
175 CHARPOS/BYTEPOS and length NCHARS. The `composition' property of
176 the sequence is PROP. STRING, if non-nil, is a string that
177 contains the composition instead of the current buffer.
179 If the composition is invalid, return -1. */
182 get_composition_id (charpos, bytepos, nchars, prop, string)
183 int charpos, bytepos, nchars;
184 Lisp_Object prop, string;
186 Lisp_Object id, length, components, key, *key_contents;
187 int glyph_len;
188 struct Lisp_Hash_Table *hash_table = XHASH_TABLE (composition_hash_table);
189 int hash_index;
190 unsigned hash_code;
191 struct composition *cmp;
192 int i, ch;
194 /* PROP should be
195 Form-A: ((LENGTH . COMPONENTS) . MODIFICATION-FUNC)
197 Form-B: (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC))
199 if (nchars == 0 || !CONSP (prop))
200 goto invalid_composition;
202 id = XCAR (prop);
203 if (INTEGERP (id))
205 /* PROP should be Form-B. */
206 if (XINT (id) < 0 || XINT (id) >= n_compositions)
207 goto invalid_composition;
208 return XINT (id);
211 /* PROP should be Form-A.
212 Thus, ID should be (LENGTH . COMPONENTS). */
213 if (!CONSP (id))
214 goto invalid_composition;
215 length = XCAR (id);
216 if (!INTEGERP (length) || XINT (length) != nchars)
217 goto invalid_composition;
219 components = XCDR (id);
221 /* Check if the same composition has already been registered or not
222 by consulting composition_hash_table. The key for this table is
223 COMPONENTS (converted to a vector COMPONENTS-VEC) or, if it is
224 nil, vector of characters in the composition range. */
225 if (INTEGERP (components))
226 key = Fmake_vector (make_number (1), components);
227 else if (STRINGP (components) || CONSP (components))
228 key = Fvconcat (1, &components);
229 else if (VECTORP (components))
230 key = components;
231 else if (NILP (components))
233 key = Fmake_vector (make_number (nchars), Qnil);
234 if (STRINGP (string))
235 for (i = 0; i < nchars; i++)
237 FETCH_STRING_CHAR_ADVANCE (ch, string, charpos, bytepos);
238 XVECTOR (key)->contents[i] = make_number (ch);
240 else
241 for (i = 0; i < nchars; i++)
243 FETCH_CHAR_ADVANCE (ch, charpos, bytepos);
244 XVECTOR (key)->contents[i] = make_number (ch);
247 else
248 goto invalid_composition;
250 hash_index = hash_lookup (hash_table, key, &hash_code);
251 if (hash_index >= 0)
253 /* We have already registered the same composition. Change PROP
254 from Form-A above to Form-B while replacing COMPONENTS with
255 COMPONENTS-VEC stored in the hash table. We can directly
256 modify the cons cell of PROP because it is not shared. */
257 key = HASH_KEY (hash_table, hash_index);
258 id = HASH_VALUE (hash_table, hash_index);
259 XSETCAR (prop, id);
260 XSETCDR (prop, Fcons (make_number (nchars), Fcons (key, XCDR (prop))));
261 return XINT (id);
264 /* This composition is a new one. We must register it. */
266 /* Check if we have sufficient memory to store this information. */
267 if (composition_table_size == 0)
269 composition_table_size = 256;
270 composition_table
271 = (struct composition **) xmalloc (sizeof (composition_table[0])
272 * composition_table_size);
274 else if (composition_table_size <= n_compositions)
276 composition_table_size += 256;
277 composition_table
278 = (struct composition **) xrealloc (composition_table,
279 sizeof (composition_table[0])
280 * composition_table_size);
283 key_contents = XVECTOR (key)->contents;
285 /* Check if the contents of COMPONENTS are valid if COMPONENTS is a
286 vector or a list. It should be a sequence of:
287 char1 rule1 char2 rule2 char3 ... ruleN charN+1 */
289 if (VECTORP (components)
290 && ASIZE (components) >= 2
291 && VECTORP (AREF (components, 0)))
293 /* COMPONENTS is a glyph-string. */
294 int len = ASIZE (key);
296 for (i = 1; i < len; i++)
297 if (! VECTORP (AREF (key, i)))
298 goto invalid_composition;
300 else if (VECTORP (components) || CONSP (components))
302 int len = XVECTOR (key)->size;
304 /* The number of elements should be odd. */
305 if ((len % 2) == 0)
306 goto invalid_composition;
307 /* All elements should be integers (character or encoded
308 composition rule). */
309 for (i = 0; i < len; i++)
311 if (!INTEGERP (key_contents[i]))
312 goto invalid_composition;
316 /* Change PROP from Form-A above to Form-B. We can directly modify
317 the cons cell of PROP because it is not shared. */
318 XSETFASTINT (id, n_compositions);
319 XSETCAR (prop, id);
320 XSETCDR (prop, Fcons (make_number (nchars), Fcons (key, XCDR (prop))));
322 /* Register the composition in composition_hash_table. */
323 hash_index = hash_put (hash_table, key, id, hash_code);
325 /* Register the composition in composition_table. */
326 cmp = (struct composition *) xmalloc (sizeof (struct composition));
328 cmp->method = (NILP (components)
329 ? COMPOSITION_RELATIVE
330 : ((INTEGERP (components) || STRINGP (components))
331 ? COMPOSITION_WITH_ALTCHARS
332 : COMPOSITION_WITH_RULE_ALTCHARS));
333 cmp->hash_index = hash_index;
334 glyph_len = (cmp->method == COMPOSITION_WITH_RULE_ALTCHARS
335 ? (XVECTOR (key)->size + 1) / 2
336 : XVECTOR (key)->size);
337 cmp->glyph_len = glyph_len;
338 cmp->offsets = (short *) xmalloc (sizeof (short) * glyph_len * 2);
339 cmp->font = NULL;
341 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
343 /* Relative composition. */
344 cmp->width = 0;
345 for (i = 0; i < glyph_len; i++)
347 int this_width;
348 ch = XINT (key_contents[i]);
349 this_width = (ch == '\t' ? 1 : CHAR_WIDTH (ch));
350 if (cmp->width < this_width)
351 cmp->width = this_width;
354 else
356 /* Rule-base composition. */
357 float leftmost = 0.0, rightmost;
359 ch = XINT (key_contents[0]);
360 rightmost = ch != '\t' ? CHAR_WIDTH (ch) : 1;
362 for (i = 1; i < glyph_len; i += 2)
364 int rule, gref, nref, xoff, yoff;
365 int this_width;
366 float this_left;
368 rule = XINT (key_contents[i]);
369 ch = XINT (key_contents[i + 1]);
370 this_width = ch != '\t' ? CHAR_WIDTH (ch) : 1;
372 /* A composition rule is specified by an integer value
373 that encodes global and new reference points (GREF and
374 NREF). GREF and NREF are specified by numbers as
375 below:
376 0---1---2 -- ascent
380 9--10--11 -- center
382 ---3---4---5--- baseline
384 6---7---8 -- descent
386 COMPOSITION_DECODE_RULE (rule, gref, nref, xoff, yoff);
387 this_left = (leftmost
388 + (gref % 3) * (rightmost - leftmost) / 2.0
389 - (nref % 3) * this_width / 2.0);
391 if (this_left < leftmost)
392 leftmost = this_left;
393 if (this_left + this_width > rightmost)
394 rightmost = this_left + this_width;
397 cmp->width = rightmost - leftmost;
398 if (cmp->width < (rightmost - leftmost))
399 /* To get a ceiling integer value. */
400 cmp->width++;
403 composition_table[n_compositions] = cmp;
405 return n_compositions++;
407 invalid_composition:
408 /* Would it be better to remove this `composition' property? */
409 return -1;
413 /* Find a static composition at or nearest to position POS of OBJECT
414 (buffer or string).
416 OBJECT defaults to the current buffer. If there's a composition at
417 POS, set *START and *END to the start and end of the sequence,
418 *PROP to the `composition' property, and return 1.
420 If there's no composition at POS and LIMIT is negative, return 0.
422 Otherwise, search for a composition forward (LIMIT > POS) or
423 backward (LIMIT < POS). In this case, LIMIT bounds the search.
425 If a composition is found, set *START, *END, and *PROP as above,
426 and return 1, else return 0.
428 This doesn't check the validity of composition. */
431 find_composition (pos, limit, start, end, prop, object)
432 int pos, limit;
433 EMACS_INT *start, *end;
434 Lisp_Object *prop, object;
436 Lisp_Object val;
438 if (get_property_and_range (pos, Qcomposition, prop, start, end, object))
439 return 1;
441 if (limit < 0 || limit == pos)
442 return 0;
444 if (limit > pos) /* search forward */
446 val = Fnext_single_property_change (make_number (pos), Qcomposition,
447 object, make_number (limit));
448 pos = XINT (val);
449 if (pos == limit)
450 return 0;
452 else /* search backward */
454 if (get_property_and_range (pos - 1, Qcomposition, prop, start, end,
455 object))
456 return 1;
457 val = Fprevious_single_property_change (make_number (pos), Qcomposition,
458 object, make_number (limit));
459 pos = XINT (val);
460 if (pos == limit)
461 return 0;
462 pos--;
464 get_property_and_range (pos, Qcomposition, prop, start, end, object);
465 return 1;
468 /* Run a proper function to adjust the composition sitting between
469 FROM and TO with property PROP. */
471 static void
472 run_composition_function (from, to, prop)
473 int from, to;
474 Lisp_Object prop;
476 Lisp_Object func;
477 EMACS_INT start, end;
479 func = COMPOSITION_MODIFICATION_FUNC (prop);
480 /* If an invalid composition precedes or follows, try to make them
481 valid too. */
482 if (from > BEGV
483 && find_composition (from - 1, -1, &start, &end, &prop, Qnil)
484 && !COMPOSITION_VALID_P (start, end, prop))
485 from = start;
486 if (to < ZV
487 && find_composition (to, -1, &start, &end, &prop, Qnil)
488 && !COMPOSITION_VALID_P (start, end, prop))
489 to = end;
490 if (!NILP (Ffboundp (func)))
491 call2 (func, make_number (from), make_number (to));
494 /* Make invalid compositions adjacent to or inside FROM and TO valid.
495 CHECK_MASK is bitwise `or' of mask bits defined by macros
496 CHECK_XXX (see the comment in composite.h).
498 It also resets the text-property `auto-composed' to a proper region
499 so that automatic character composition works correctly later while
500 displaying the region.
502 This function is called when a buffer text is changed. If the
503 change is deletion, FROM == TO. Otherwise, FROM < TO. */
505 void
506 update_compositions (from, to, check_mask)
507 EMACS_INT from, to;
508 int check_mask;
510 Lisp_Object prop;
511 EMACS_INT start, end;
512 /* The beginning and end of the region to set the property
513 `auto-composed' to nil. */
514 EMACS_INT min_pos = from, max_pos = to;
516 if (inhibit_modification_hooks)
517 return;
519 /* If FROM and TO are not in a valid range, do nothing. */
520 if (! (BEGV <= from && from <= to && to <= ZV))
521 return;
523 if (check_mask & CHECK_HEAD)
525 /* FROM should be at composition boundary. But, insertion or
526 deletion will make two compositions adjacent and
527 indistinguishable when they have same (eq) property. To
528 avoid it, in such a case, we change the property of the
529 latter to the copy of it. */
530 if (from > BEGV
531 && find_composition (from - 1, -1, &start, &end, &prop, Qnil)
532 && COMPOSITION_VALID_P (start, end, prop))
534 min_pos = start;
535 if (end > to)
536 max_pos = end;
537 if (from < end)
538 Fput_text_property (make_number (from), make_number (end),
539 Qcomposition,
540 Fcons (XCAR (prop), XCDR (prop)), Qnil);
541 run_composition_function (start, end, prop);
542 from = end;
544 else if (from < ZV
545 && find_composition (from, -1, &start, &from, &prop, Qnil)
546 && COMPOSITION_VALID_P (start, from, prop))
548 if (from > to)
549 max_pos = from;
550 run_composition_function (start, from, prop);
554 if (check_mask & CHECK_INSIDE)
556 /* In this case, we are sure that (check & CHECK_TAIL) is also
557 nonzero. Thus, here we should check only compositions before
558 (to - 1). */
559 while (from < to - 1
560 && find_composition (from, to, &start, &from, &prop, Qnil)
561 && COMPOSITION_VALID_P (start, from, prop)
562 && from < to - 1)
563 run_composition_function (start, from, prop);
566 if (check_mask & CHECK_TAIL)
568 if (from < to
569 && find_composition (to - 1, -1, &start, &end, &prop, Qnil)
570 && COMPOSITION_VALID_P (start, end, prop))
572 /* TO should be also at composition boundary. But,
573 insertion or deletion will make two compositions adjacent
574 and indistinguishable when they have same (eq) property.
575 To avoid it, in such a case, we change the property of
576 the former to the copy of it. */
577 if (to < end)
579 Fput_text_property (make_number (start), make_number (to),
580 Qcomposition,
581 Fcons (XCAR (prop), XCDR (prop)), Qnil);
582 max_pos = end;
584 run_composition_function (start, end, prop);
586 else if (to < ZV
587 && find_composition (to, -1, &start, &end, &prop, Qnil)
588 && COMPOSITION_VALID_P (start, end, prop))
590 run_composition_function (start, end, prop);
591 max_pos = end;
594 if (min_pos < max_pos)
596 int count = SPECPDL_INDEX ();
598 specbind (Qinhibit_read_only, Qt);
599 specbind (Qinhibit_modification_hooks, Qt);
600 specbind (Qinhibit_point_motion_hooks, Qt);
601 Fremove_list_of_text_properties (make_number (min_pos),
602 make_number (max_pos),
603 Fcons (Qauto_composed, Qnil), Qnil);
604 unbind_to (count, Qnil);
609 /* Modify composition property values in LIST destructively. LIST is
610 a list as returned from text_property_list. Change values to the
611 top-level copies of them so that none of them are `eq'. */
613 void
614 make_composition_value_copy (list)
615 Lisp_Object list;
617 Lisp_Object plist, val;
619 for (; CONSP (list); list = XCDR (list))
621 plist = XCAR (XCDR (XCDR (XCAR (list))));
622 while (CONSP (plist) && CONSP (XCDR (plist)))
624 if (EQ (XCAR (plist), Qcomposition)
625 && (val = XCAR (XCDR (plist)), CONSP (val)))
626 XSETCAR (XCDR (plist), Fcons (XCAR (val), XCDR (val)));
627 plist = XCDR (XCDR (plist));
633 /* Make text in the region between START and END a composition that
634 has COMPONENTS and MODIFICATION-FUNC.
636 If STRING is non-nil, then operate on characters contained between
637 indices START and END in STRING. */
639 void
640 compose_text (start, end, components, modification_func, string)
641 int start, end;
642 Lisp_Object components, modification_func, string;
644 Lisp_Object prop;
646 prop = Fcons (Fcons (make_number (end - start), components),
647 modification_func);
648 Fput_text_property (make_number (start), make_number (end),
649 Qcomposition, prop, string);
653 static Lisp_Object autocmp_chars P_ ((Lisp_Object, EMACS_INT, EMACS_INT,
654 EMACS_INT, struct window *,
655 struct face *, Lisp_Object));
658 /* Lisp glyph-string handlers */
660 /* Hash table for automatic composition. The key is a header of a
661 lgstring (Lispy glyph-string), and the value is a body of a
662 lgstring. */
664 static Lisp_Object gstring_hash_table;
666 static Lisp_Object gstring_lookup_cache P_ ((Lisp_Object));
668 static Lisp_Object
669 gstring_lookup_cache (header)
670 Lisp_Object header;
672 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
673 int i = hash_lookup (h, header, NULL);
675 return (i >= 0 ? HASH_VALUE (h, i) : Qnil);
678 Lisp_Object
679 composition_gstring_put_cache (gstring, len)
680 Lisp_Object gstring;
681 int len;
683 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
684 unsigned hash;
685 Lisp_Object header, copy;
686 int i;
688 header = LGSTRING_HEADER (gstring);
689 hash = h->hashfn (h, header);
690 if (len < 0)
692 len = LGSTRING_GLYPH_LEN (gstring);
693 for (i = 0; i < len; i++)
694 if (NILP (LGSTRING_GLYPH (gstring, i)))
695 break;
696 len = i;
699 copy = Fmake_vector (make_number (len + 2), Qnil);
700 LGSTRING_SET_HEADER (copy, Fcopy_sequence (header));
701 for (i = 0; i < len; i++)
702 LGSTRING_SET_GLYPH (copy, i, Fcopy_sequence (LGSTRING_GLYPH (gstring, i)));
703 i = hash_put (h, LGSTRING_HEADER (copy), copy, hash);
704 LGSTRING_SET_ID (copy, make_number (i));
705 return copy;
708 Lisp_Object
709 composition_gstring_from_id (id)
710 int id;
712 struct Lisp_Hash_Table *h = XHASH_TABLE (gstring_hash_table);
714 return HASH_VALUE (h, id);
717 static Lisp_Object fill_gstring_header P_ ((Lisp_Object, Lisp_Object,
718 Lisp_Object, Lisp_Object,
719 Lisp_Object));
722 composition_gstring_p (gstring)
723 Lisp_Object gstring;
725 Lisp_Object header;
726 int i;
728 if (! VECTORP (gstring) || ASIZE (gstring) < 2)
729 return 0;
730 header = LGSTRING_HEADER (gstring);
731 if (! VECTORP (header) || ASIZE (header) < 2)
732 return 0;
733 if (! NILP (LGSTRING_FONT (gstring))
734 && (! FONT_OBJECT_P (LGSTRING_FONT (gstring))
735 && ! CODING_SYSTEM_P (LGSTRING_FONT (gstring))))
736 return 0;
737 for (i = 1; i < ASIZE (LGSTRING_HEADER (gstring)); i++)
738 if (! NATNUMP (AREF (LGSTRING_HEADER (gstring), i)))
739 return 0;
740 if (! NILP (LGSTRING_ID (gstring)) && ! NATNUMP (LGSTRING_ID (gstring)))
741 return 0;
742 for (i = 0; i < LGSTRING_GLYPH_LEN (gstring); i++)
744 Lisp_Object glyph = LGSTRING_GLYPH (gstring, i);
745 if (NILP (glyph))
746 break;
747 if (! VECTORP (glyph) || ASIZE (glyph) != LGLYPH_SIZE)
748 return 0;
750 return 1;
754 composition_gstring_width (gstring, from, to, metrics)
755 Lisp_Object gstring;
756 int from, to;
757 struct font_metrics *metrics;
759 Lisp_Object *glyph;
760 int width = 0;
762 if (metrics)
764 Lisp_Object font_object = LGSTRING_FONT (gstring);
766 if (FONT_OBJECT_P (font_object))
768 struct font *font = XFONT_OBJECT (font_object);
770 metrics->ascent = font->ascent;
771 metrics->descent = font->descent;
773 else
775 metrics->ascent = 1;
776 metrics->descent = 0;
778 metrics->width = metrics->lbearing = metrics->rbearing = 0;
780 for (glyph = &LGSTRING_GLYPH (gstring, from); from < to; from++, glyph++)
782 int x;
784 if (NILP (LGLYPH_ADJUSTMENT (*glyph)))
785 width += LGLYPH_WIDTH (*glyph);
786 else
787 width += LGLYPH_WADJUST (*glyph);
788 if (metrics)
790 x = metrics->width + LGLYPH_LBEARING (*glyph) + LGLYPH_XOFF (*glyph);
791 if (metrics->lbearing > x)
792 metrics->lbearing = x;
793 x = metrics->width + LGLYPH_RBEARING (*glyph) + LGLYPH_XOFF (*glyph);
794 if (metrics->rbearing < x)
795 metrics->rbearing = x;
796 metrics->width = width;
797 x = LGLYPH_ASCENT (*glyph) - LGLYPH_YOFF (*glyph);
798 if (metrics->ascent < x)
799 metrics->ascent = x;
800 x = LGLYPH_DESCENT (*glyph) + LGLYPH_YOFF (*glyph);
801 if (metrics->descent < x)
802 metrics->descent = x;
805 return width;
809 static Lisp_Object gstring_work;
810 static Lisp_Object gstring_work_headers;
812 static Lisp_Object
813 fill_gstring_header (header, start, end, font_object, string)
814 Lisp_Object header, start, end, font_object, string;
816 EMACS_INT from, to, from_byte;
817 EMACS_INT len, i;
819 if (NILP (string))
821 if (NILP (current_buffer->enable_multibyte_characters))
822 error ("Attempt to shape unibyte text");
823 validate_region (&start, &end);
824 from = XFASTINT (start);
825 to = XFASTINT (end);
826 from_byte = CHAR_TO_BYTE (from);
828 else
830 CHECK_STRING (string);
831 if (! STRING_MULTIBYTE (string))
832 error ("Attempt to shape unibyte text");
833 /* FROM and TO are checked by the caller. */
834 from = XINT (start);
835 to = XINT (end);
836 if (from < 0 || from > to || to > SCHARS (string))
837 args_out_of_range_3 (string, start, end);
838 from_byte = string_char_to_byte (string, from);
841 len = to - from;
842 if (len == 0)
843 error ("Attempt to shape zero-length text");
844 if (VECTORP (header))
846 if (ASIZE (header) != len + 1)
847 args_out_of_range (header, make_number (len + 1));
849 else
851 if (len <= 8)
852 header = AREF (gstring_work_headers, len - 1);
853 else
854 header = Fmake_vector (make_number (len + 1), Qnil);
857 ASET (header, 0, font_object);
858 for (i = 0; i < len; i++)
860 int c;
862 if (NILP (string))
863 FETCH_CHAR_ADVANCE_NO_CHECK (c, from, from_byte);
864 else
865 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, string, from, from_byte);
866 ASET (header, i + 1, make_number (c));
868 return header;
871 extern void font_fill_lglyph_metrics P_ ((Lisp_Object, Lisp_Object));
873 static void
874 fill_gstring_body (gstring)
875 Lisp_Object gstring;
877 Lisp_Object font_object = LGSTRING_FONT (gstring);
878 Lisp_Object header = AREF (gstring, 0);
879 EMACS_INT len = LGSTRING_CHAR_LEN (gstring);
880 EMACS_INT i;
882 for (i = 0; i < len; i++)
884 Lisp_Object g = LGSTRING_GLYPH (gstring, i);
885 EMACS_INT c = XINT (AREF (header, i + 1));
887 if (NILP (g))
889 g = LGLYPH_NEW ();
890 LGSTRING_SET_GLYPH (gstring, i, g);
892 LGLYPH_SET_FROM (g, i);
893 LGLYPH_SET_TO (g, i);
894 LGLYPH_SET_CHAR (g, c);
895 if (FONT_OBJECT_P (font_object))
897 font_fill_lglyph_metrics (g, font_object);
899 else
901 int width = XFASTINT (CHAR_TABLE_REF (Vchar_width_table, c));
903 LGLYPH_SET_CODE (g, c);
904 LGLYPH_SET_LBEARING (g, 0);
905 LGLYPH_SET_RBEARING (g, width);
906 LGLYPH_SET_WIDTH (g, width);
907 LGLYPH_SET_ASCENT (g, 1);
908 LGLYPH_SET_DESCENT (g, 0);
910 LGLYPH_SET_ADJUSTMENT (g, Qnil);
912 if (i < LGSTRING_GLYPH_LEN (gstring))
913 LGSTRING_SET_GLYPH (gstring, i, Qnil);
917 /* Try to compose the characters at CHARPOS according to CFT_ELEMENT
918 which is an element of composition-function-table (which see).
919 LIMIT limits the characters to compose. STRING, if not nil, is a
920 target string. WIN is a window where the characters are being
921 displayed. */
923 static Lisp_Object
924 autocmp_chars (cft_element, charpos, bytepos, limit, win, face, string)
925 Lisp_Object cft_element;
926 EMACS_INT charpos, bytepos, limit;
927 struct window *win;
928 struct face *face;
929 Lisp_Object string;
931 int count = SPECPDL_INDEX ();
932 FRAME_PTR f = XFRAME (win->frame);
933 Lisp_Object pos = make_number (charpos);
934 EMACS_INT pt = PT, pt_byte = PT_BYTE;
935 int lookback;
937 record_unwind_save_match_data ();
938 for (lookback = -1; CONSP (cft_element); cft_element = XCDR (cft_element))
940 Lisp_Object elt = XCAR (cft_element);
941 Lisp_Object re;
942 Lisp_Object font_object = Qnil, gstring;
943 EMACS_INT len, to;
945 if (! VECTORP (elt) || ASIZE (elt) != 3)
946 continue;
947 if (lookback < 0)
949 lookback = XFASTINT (AREF (elt, 1));
950 if (limit > charpos + MAX_COMPOSITION_COMPONENTS)
951 limit = charpos + MAX_COMPOSITION_COMPONENTS;
953 else if (lookback != XFASTINT (AREF (elt, 1)))
954 break;
955 re = AREF (elt, 0);
956 if (NILP (re))
957 len = 1;
958 else if ((len = fast_looking_at (re, charpos, bytepos, limit, -1, string))
959 > 0)
961 if (NILP (string))
962 len = BYTE_TO_CHAR (bytepos + len) - charpos;
963 else
964 len = string_byte_to_char (string, bytepos + len) - charpos;
966 if (len > 0)
968 limit = to = charpos + len;
969 #ifdef HAVE_WINDOW_SYSTEM
970 if (FRAME_WINDOW_P (f))
972 font_object = font_range (charpos, &to, win, face, string);
973 if (! FONT_OBJECT_P (font_object)
974 || (! NILP (re)
975 && to < limit
976 && (fast_looking_at (re, charpos, bytepos, to, -1, string) <= 0)))
978 if (NILP (string))
979 TEMP_SET_PT_BOTH (pt, pt_byte);
980 return unbind_to (count, Qnil);
983 else
984 #endif /* not HAVE_WINDOW_SYSTEM */
985 font_object = win->frame;
986 gstring = Fcomposition_get_gstring (pos, make_number (to),
987 font_object, string);
988 if (NILP (LGSTRING_ID (gstring)))
990 Lisp_Object args[6];
992 args[0] = Vauto_composition_function;
993 args[1] = AREF (elt, 2);
994 args[2] = pos;
995 args[3] = make_number (to);
996 args[4] = font_object;
997 args[5] = string;
998 gstring = safe_call (6, args);
1000 if (NILP (string))
1001 TEMP_SET_PT_BOTH (pt, pt_byte);
1002 return unbind_to (count, gstring);
1005 if (NILP (string))
1006 TEMP_SET_PT_BOTH (pt, pt_byte);
1007 return unbind_to (count, Qnil);
1011 /* Update cmp_it->stop_pos to the next position after CHARPOS (and
1012 BYTEPOS) where character composition may happen. If BYTEPOS is
1013 negative, compoute it. If it is a static composition, set
1014 cmp_it->ch to -1. Otherwise, set cmp_it->ch to the character that
1015 triggers a automatic composition. */
1017 void
1018 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string)
1019 struct composition_it *cmp_it;
1020 EMACS_INT charpos, bytepos, endpos;
1021 Lisp_Object string;
1023 EMACS_INT start, end, c;
1024 Lisp_Object prop, val;
1025 /* This is from forward_to_next_line_start in xdisp.c. */
1026 const int MAX_NEWLINE_DISTANCE = 500;
1028 if (endpos > charpos + MAX_NEWLINE_DISTANCE)
1029 endpos = charpos + MAX_NEWLINE_DISTANCE;
1030 cmp_it->stop_pos = endpos;
1031 cmp_it->id = -1;
1032 cmp_it->ch = -2;
1033 if (find_composition (charpos, endpos, &start, &end, &prop, string)
1034 && COMPOSITION_VALID_P (start, end, prop))
1036 cmp_it->stop_pos = endpos = start;
1037 cmp_it->ch = -1;
1039 if (NILP (string) && PT > charpos && PT < endpos)
1040 cmp_it->stop_pos = PT;
1041 if (NILP (current_buffer->enable_multibyte_characters)
1042 || ! FUNCTIONP (Vauto_composition_function))
1043 return;
1044 if (bytepos < 0)
1046 if (STRINGP (string))
1047 bytepos = string_char_to_byte (string, charpos);
1048 else
1049 bytepos = CHAR_TO_BYTE (charpos);
1052 start = charpos;
1053 while (charpos < endpos)
1055 if (STRINGP (string))
1056 FETCH_STRING_CHAR_ADVANCE (c, string, charpos, bytepos);
1057 else
1058 FETCH_CHAR_ADVANCE (c, charpos, bytepos);
1059 if (c == '\n')
1061 cmp_it->ch = -2;
1062 break;
1064 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1065 if (! NILP (val))
1067 Lisp_Object elt;
1069 for (; CONSP (val); val = XCDR (val))
1071 elt = XCAR (val);
1072 if (VECTORP (elt) && ASIZE (elt) == 3 && NATNUMP (AREF (elt, 1))
1073 && charpos - 1 - XFASTINT (AREF (elt, 1)) >= start)
1074 break;
1076 if (CONSP (val))
1078 cmp_it->lookback = XFASTINT (AREF (elt, 1));
1079 cmp_it->stop_pos = charpos - 1 - cmp_it->lookback;
1080 cmp_it->ch = c;
1081 return;
1085 cmp_it->stop_pos = charpos;
1088 /* Check if the character at CHARPOS (and BYTEPOS) is composed
1089 (possibly with the following characters) on window W. ENDPOS limits
1090 characters to be composed. FACE, in non-NULL, is a base face of
1091 the character. If STRING is not nil, it is a string containing the
1092 character to check, and CHARPOS and BYTEPOS are indices in the
1093 string. In that case, FACE must not be NULL.
1095 If the character is composed, setup members of CMP_IT (id, nglyphs,
1096 and from), and return 1. Otherwise, update CMP_IT->stop_pos, and
1097 return 0. */
1100 composition_reseat_it (cmp_it, charpos, bytepos, endpos, w, face, string)
1101 struct composition_it *cmp_it;
1102 EMACS_INT charpos, bytepos, endpos;
1103 struct window *w;
1104 struct face *face;
1105 Lisp_Object string;
1107 if (cmp_it->ch == -2)
1109 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string);
1110 if (cmp_it->ch == -2)
1111 return 0;
1114 if (cmp_it->ch < 0)
1116 /* We are looking at a static composition. */
1117 EMACS_INT start, end;
1118 Lisp_Object prop;
1120 find_composition (charpos, -1, &start, &end, &prop, string);
1121 cmp_it->id = get_composition_id (charpos, bytepos, end - start,
1122 prop, string);
1123 if (cmp_it->id < 0)
1124 goto no_composition;
1125 cmp_it->nchars = end - start;
1126 cmp_it->nglyphs = composition_table[cmp_it->id]->glyph_len;
1128 else if (w)
1130 Lisp_Object val, elt;
1131 int i;
1133 val = CHAR_TABLE_REF (Vcomposition_function_table, cmp_it->ch);
1134 for (; CONSP (val); val = XCDR (val))
1136 elt = XCAR (val);
1137 if (cmp_it->lookback == XFASTINT (AREF (elt, 1)))
1138 break;
1140 if (NILP (val))
1141 goto no_composition;
1143 val = autocmp_chars (val, charpos, bytepos, endpos, w, face, string);
1144 if (! composition_gstring_p (val))
1145 goto no_composition;
1146 if (NILP (LGSTRING_ID (val)))
1147 val = composition_gstring_put_cache (val, -1);
1148 cmp_it->id = XINT (LGSTRING_ID (val));
1149 for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++)
1150 if (NILP (LGSTRING_GLYPH (val, i)))
1151 break;
1152 cmp_it->nglyphs = i;
1154 else
1155 goto no_composition;
1156 cmp_it->from = 0;
1157 return 1;
1159 no_composition:
1160 charpos++;
1161 if (STRINGP (string))
1162 bytepos += MULTIBYTE_LENGTH_NO_CHECK (SDATA (string) + bytepos);
1163 else
1164 INC_POS (bytepos);
1165 composition_compute_stop_pos (cmp_it, charpos, bytepos, endpos, string);
1166 return 0;
1170 composition_update_it (cmp_it, charpos, bytepos, string)
1171 struct composition_it *cmp_it;
1172 EMACS_INT charpos, bytepos;
1173 Lisp_Object string;
1175 int i, c;
1177 if (cmp_it->ch < 0)
1179 struct composition *cmp = composition_table[cmp_it->id];
1181 cmp_it->to = cmp_it->nglyphs;
1182 if (cmp_it->nglyphs == 0)
1183 c = -1;
1184 else
1186 for (i = 0; i < cmp->glyph_len; i++)
1187 if ((c = COMPOSITION_GLYPH (cmp, i)) != '\t')
1188 break;
1189 if (c == '\t')
1190 c = ' ';
1192 cmp_it->width = cmp->width;
1194 else
1196 Lisp_Object gstring = composition_gstring_from_id (cmp_it->id);
1198 if (cmp_it->nglyphs == 0)
1200 c = -1;
1201 cmp_it->nchars = LGSTRING_CHAR_LEN (gstring);
1202 cmp_it->width = 0;
1204 else
1206 Lisp_Object glyph = LGSTRING_GLYPH (gstring, cmp_it->from);
1207 int from = LGLYPH_FROM (glyph);
1209 c = XINT (LGSTRING_CHAR (gstring, from));
1210 cmp_it->nchars = LGLYPH_TO (glyph) - from + 1;
1211 cmp_it->width = (LGLYPH_WIDTH (glyph) > 0
1212 ? CHAR_WIDTH (LGLYPH_CHAR (glyph)) : 0);
1213 for (cmp_it->to = cmp_it->from + 1; cmp_it->to < cmp_it->nglyphs;
1214 cmp_it->to++)
1216 glyph = LGSTRING_GLYPH (gstring, cmp_it->to);
1217 if (LGLYPH_FROM (glyph) != from)
1218 break;
1219 if (LGLYPH_WIDTH (glyph) > 0)
1220 cmp_it->width += CHAR_WIDTH (LGLYPH_CHAR (glyph));
1225 charpos += cmp_it->nchars;
1226 if (STRINGP (string))
1227 cmp_it->nbytes = string_char_to_byte (string, charpos) - bytepos;
1228 else
1229 cmp_it->nbytes = CHAR_TO_BYTE (charpos) - bytepos;
1230 return c;
1234 struct position_record
1236 EMACS_INT pos, pos_byte;
1237 unsigned char *p;
1240 /* Update the members of POSTION to the next character boundary. */
1241 #define FORWARD_CHAR(POSITION, STOP) \
1242 do { \
1243 (POSITION).pos++; \
1244 if ((POSITION).pos == (STOP)) \
1246 (POSITION).p = GAP_END_ADDR; \
1247 (POSITION).pos_byte = GPT_BYTE; \
1249 else \
1251 (POSITION).pos_byte += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \
1252 (POSITION).p += BYTES_BY_CHAR_HEAD (*((POSITION).p)); \
1254 } while (0)
1256 /* Update the members of POSTION to the previous character boundary. */
1257 #define BACKWARD_CHAR(POSITION, STOP) \
1258 do { \
1259 if ((POSITION).pos == STOP) \
1260 (POSITION).p = GPT_ADDR; \
1261 do { \
1262 (POSITION).pos_byte--; \
1263 (POSITION).p--; \
1264 } while (! CHAR_HEAD_P (*((POSITION).p))); \
1265 (POSITION).pos--; \
1266 } while (0)
1268 static Lisp_Object _work_val;
1269 static int _work_char;
1271 /* 1 iff the character C is composable. */
1272 #define CHAR_COMPOSABLE_P(C) \
1273 (_work_val = CHAR_TABLE_REF (Vunicode_category_table, (C)), \
1274 (SYMBOLP (_work_val) \
1275 && (_work_char = SDATA (SYMBOL_NAME (_work_val))[0]) != 'C' \
1276 && _work_char != 'Z'))
1278 /* This is like find_composition, but find an automatic composition
1279 instead. If found, set *GSTRING to the glyph-string representing
1280 the composition, and return 1. Otherwise, return 0. */
1282 static int
1283 find_automatic_composition (pos, limit, start, end, gstring, string)
1284 EMACS_INT pos, limit, *start, *end;
1285 Lisp_Object *gstring, string;
1287 EMACS_INT head, tail, stop;
1288 /* Limit to check a composition after POS. */
1289 EMACS_INT fore_check_limit;
1290 struct position_record orig, cur, check, prev;
1291 Lisp_Object check_val, val, elt;
1292 int check_lookback;
1293 int c;
1294 Lisp_Object window;
1295 struct window *w;
1297 window = Fget_buffer_window (Fcurrent_buffer (), Qnil);
1298 if (NILP (window))
1299 return 0;
1300 w = XWINDOW (window);
1302 orig.pos = pos;
1303 if (NILP (string))
1305 head = BEGV, tail = ZV, stop = GPT;
1306 orig.pos_byte = CHAR_TO_BYTE (orig.pos);
1307 orig.p = BYTE_POS_ADDR (orig.pos_byte);
1309 else
1311 head = 0, tail = SCHARS (string), stop = -1;
1312 orig.pos_byte = string_char_to_byte (string, orig.pos);
1313 orig.p = SDATA (string) + orig.pos_byte;
1315 if (limit < pos)
1316 fore_check_limit = min (tail, pos + MAX_AUTO_COMPOSITION_LOOKBACK);
1317 else
1318 fore_check_limit = min (tail, limit + MAX_AUTO_COMPOSITION_LOOKBACK);
1319 cur = orig;
1321 retry:
1322 check_val = Qnil;
1323 /* At first, check if POS is composable. */
1324 c = STRING_CHAR (cur.p, 0);
1325 if (! CHAR_COMPOSABLE_P (c))
1327 if (limit < 0)
1328 return 0;
1329 if (limit >= cur.pos)
1330 goto search_forward;
1332 else
1334 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1335 if (! NILP (val))
1336 check_val = val, check = cur;
1337 else
1338 while (cur.pos + 1 < fore_check_limit)
1340 EMACS_INT b, e;
1342 FORWARD_CHAR (cur, stop);
1343 if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e,
1344 Qnil)
1345 && COMPOSITION_VALID_P (b, e, val))
1347 fore_check_limit = cur.pos;
1348 break;
1350 c = STRING_CHAR (cur.p, 0);
1351 if (! CHAR_COMPOSABLE_P (c))
1352 break;
1353 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1354 if (NILP (val))
1355 continue;
1356 check_val = val, check = cur;
1357 break;
1359 cur = orig;
1361 /* Rewind back to the position where we can safely search forward
1362 for compositions. */
1363 while (cur.pos > head)
1365 EMACS_INT b, e;
1367 BACKWARD_CHAR (cur, stop);
1368 if (get_property_and_range (cur.pos, Qcomposition, &val, &b, &e, Qnil)
1369 && COMPOSITION_VALID_P (b, e, val))
1370 break;
1371 c = STRING_CHAR (cur.p, 0);
1372 if (! CHAR_COMPOSABLE_P (c))
1373 break;
1374 val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1375 if (! NILP (val))
1376 check_val = val, check = cur;
1378 prev = cur;
1379 /* Now search forward. */
1380 search_forward:
1381 *gstring = Qnil;
1382 if (! NILP (check_val) || limit >= orig.pos)
1384 if (NILP (check_val))
1385 cur = orig;
1386 else
1387 cur = check;
1388 while (cur.pos < fore_check_limit)
1390 int need_adjustment = 0;
1392 if (NILP (check_val))
1394 c = STRING_CHAR (cur.p, 0);
1395 check_val = CHAR_TABLE_REF (Vcomposition_function_table, c);
1397 for (; CONSP (check_val); check_val = XCDR (check_val))
1399 elt = XCAR (check_val);
1400 if (VECTORP (elt) && ASIZE (elt) == 3 && NATNUMP (AREF (elt, 1))
1401 && cur.pos - XFASTINT (AREF (elt, 1)) >= head)
1403 check.pos = cur.pos - XFASTINT (AREF (elt, 1));
1404 if (check.pos == cur.pos)
1405 check.pos_byte = cur.pos_byte;
1406 else
1407 check.pos_byte = CHAR_TO_BYTE (check.pos);
1408 val = autocmp_chars (check_val, check.pos, check.pos_byte,
1409 tail, w, NULL, string);
1410 need_adjustment = 1;
1411 if (! NILP (val))
1413 *gstring = val;
1414 *start = check.pos;
1415 *end = check.pos + LGSTRING_CHAR_LEN (*gstring);
1416 if (*start <= orig.pos ? *end > orig.pos
1417 : limit >= orig.pos)
1418 return 1;
1419 cur.pos = *end;
1420 cur.pos_byte = CHAR_TO_BYTE (cur.pos);
1421 break;
1425 if (need_adjustment)
1427 /* As we have called Lisp, there's a possibilily that
1428 buffer/string is relocated. */
1429 if (NILP (string))
1430 cur.p = BYTE_POS_ADDR (cur.pos_byte);
1431 else
1432 cur.p = SDATA (string) + cur.pos_byte;
1434 if (! CONSP (check_val))
1435 FORWARD_CHAR (cur, stop);
1436 check_val = Qnil;
1439 if (! NILP (*gstring))
1440 return (limit >= 0 || (*start <= orig.pos && *end > orig.pos));
1441 if (limit >= 0 && limit < orig.pos && prev.pos > head)
1443 cur = prev;
1444 BACKWARD_CHAR (cur, stop);
1445 orig = cur;
1446 fore_check_limit = orig.pos;
1447 goto retry;
1449 return 0;
1453 composition_adjust_point (last_pt)
1454 EMACS_INT last_pt;
1456 EMACS_INT charpos, bytepos, startpos, beg, end, pos;
1457 Lisp_Object val;
1458 int i;
1460 if (PT == BEGV || PT == ZV)
1461 return PT;
1463 /* At first check the static composition. */
1464 if (get_property_and_range (PT, Qcomposition, &val, &beg, &end, Qnil)
1465 && COMPOSITION_VALID_P (beg, end, val))
1467 if (beg < PT /* && end > PT <- It's always the case. */
1468 && (last_pt <= beg || last_pt >= end))
1469 return (PT < last_pt ? beg : end);
1470 return PT;
1473 if (NILP (current_buffer->enable_multibyte_characters)
1474 || ! FUNCTIONP (Vauto_composition_function))
1475 return PT;
1477 /* Next check the automatic composition. */
1478 if (! find_automatic_composition (PT, (EMACS_INT) -1, &beg, &end, &val, Qnil)
1479 || beg == PT)
1480 return PT;
1481 for (i = 0; i < LGSTRING_GLYPH_LEN (val); i++)
1483 Lisp_Object glyph = LGSTRING_GLYPH (val, i);
1485 if (NILP (glyph))
1486 break;
1487 if (beg + LGLYPH_FROM (glyph) == PT)
1488 return PT;
1489 if (beg + LGLYPH_TO (glyph) >= PT)
1490 return (PT < last_pt
1491 ? beg + LGLYPH_FROM (glyph)
1492 : beg + LGLYPH_TO (glyph) + 1);
1494 return PT;
1497 DEFUN ("composition-get-gstring", Fcomposition_get_gstring,
1498 Scomposition_get_gstring, 4, 4, 0,
1499 doc: /* Return a glyph-string for characters between FROM and TO.
1500 If the glyph string is for graphic display, FONT-OBJECT must be
1501 a font-object to use for those characters.
1502 Otherwise (for terminal display), FONT-OBJECT must be a terminal ID, a
1503 frame, or nil for the selected frame's terminal device.
1505 If the optional 4th argument STRING is not nil, it is a string
1506 containing the target characters between indices FROM and TO.
1508 A glyph-string is a vector containing information about how to display
1509 a specific character sequence. The format is:
1510 [HEADER ID GLYPH ...]
1512 HEADER is a vector of this form:
1513 [FONT-OBJECT CHAR ...]
1514 where
1515 FONT-OBJECT is a font-object for all glyphs in the glyph-string,
1516 or the terminal coding system of the specified terminal.
1517 CHARs are characters to be composed by GLYPHs.
1519 ID is an identification number of the glyph-string. It may be nil if
1520 not yet shaped.
1522 GLYPH is a vector whose elements have this form:
1523 [ FROM-IDX TO-IDX C CODE WIDTH LBEARING RBEARING ASCENT DESCENT
1524 [ [X-OFF Y-OFF WADJUST] | nil] ]
1525 where
1526 FROM-IDX and TO-IDX are used internally and should not be touched.
1527 C is the character of the glyph.
1528 CODE is the glyph-code of C in FONT-OBJECT.
1529 WIDTH thru DESCENT are the metrics (in pixels) of the glyph.
1530 X-OFF and Y-OFF are offsets to the base position for the glyph.
1531 WADJUST is the adjustment to the normal width of the glyph.
1533 If GLYPH is nil, the remaining elements of the glyph-string vector
1534 should be ignored. */)
1535 (from, to, font_object, string)
1536 Lisp_Object font_object, from, to, string;
1538 Lisp_Object gstring, header;
1539 EMACS_INT frompos, topos;
1541 CHECK_NATNUM (from);
1542 CHECK_NATNUM (to);
1543 if (XINT (to) > XINT (from) + MAX_COMPOSITION_COMPONENTS)
1544 to = make_number (XINT (from) + MAX_COMPOSITION_COMPONENTS);
1545 if (! FONT_OBJECT_P (font_object))
1547 struct coding_system *coding;
1548 struct terminal *terminal = get_terminal (font_object, 1);
1550 coding = ((TERMINAL_TERMINAL_CODING (terminal)->common_flags
1551 & CODING_REQUIRE_ENCODING_MASK)
1552 ? TERMINAL_TERMINAL_CODING (terminal) : &safe_terminal_coding);
1553 font_object = CODING_ID_NAME (coding->id);
1556 header = fill_gstring_header (Qnil, from, to, font_object, string);
1557 gstring = gstring_lookup_cache (header);
1558 if (! NILP (gstring))
1559 return gstring;
1561 frompos = XINT (from);
1562 topos = XINT (to);
1563 if (LGSTRING_GLYPH_LEN (gstring_work) < topos - frompos)
1564 gstring_work = Fmake_vector (make_number (topos - frompos + 2), Qnil);
1565 LGSTRING_SET_HEADER (gstring_work, header);
1566 LGSTRING_SET_ID (gstring_work, Qnil);
1567 fill_gstring_body (gstring_work);
1568 return gstring_work;
1572 /* Emacs Lisp APIs. */
1574 DEFUN ("compose-region-internal", Fcompose_region_internal,
1575 Scompose_region_internal, 2, 4, 0,
1576 doc: /* Internal use only.
1578 Compose text in the region between START and END.
1579 Optional 3rd and 4th arguments are COMPONENTS and MODIFICATION-FUNC
1580 for the composition. See `compose-region' for more details. */)
1581 (start, end, components, modification_func)
1582 Lisp_Object start, end, components, modification_func;
1584 validate_region (&start, &end);
1585 if (!NILP (components)
1586 && !INTEGERP (components)
1587 && !CONSP (components)
1588 && !STRINGP (components))
1589 CHECK_VECTOR (components);
1591 compose_text (XINT (start), XINT (end), components, modification_func, Qnil);
1592 return Qnil;
1595 DEFUN ("compose-string-internal", Fcompose_string_internal,
1596 Scompose_string_internal, 3, 5, 0,
1597 doc: /* Internal use only.
1599 Compose text between indices START and END of STRING.
1600 Optional 4th and 5th arguments are COMPONENTS and MODIFICATION-FUNC
1601 for the composition. See `compose-string' for more details. */)
1602 (string, start, end, components, modification_func)
1603 Lisp_Object string, start, end, components, modification_func;
1605 CHECK_STRING (string);
1606 CHECK_NUMBER (start);
1607 CHECK_NUMBER (end);
1609 if (XINT (start) < 0 ||
1610 XINT (start) > XINT (end)
1611 || XINT (end) > SCHARS (string))
1612 args_out_of_range (start, end);
1614 compose_text (XINT (start), XINT (end), components, modification_func, string);
1615 return string;
1618 DEFUN ("find-composition-internal", Ffind_composition_internal,
1619 Sfind_composition_internal, 4, 4, 0,
1620 doc: /* Internal use only.
1622 Return information about composition at or nearest to position POS.
1623 See `find-composition' for more details. */)
1624 (pos, limit, string, detail_p)
1625 Lisp_Object pos, limit, string, detail_p;
1627 Lisp_Object prop, tail, gstring;
1628 EMACS_INT start, end, from, to;
1629 int id;
1631 CHECK_NUMBER_COERCE_MARKER (pos);
1632 from = XINT (pos);
1633 if (!NILP (limit))
1635 CHECK_NUMBER_COERCE_MARKER (limit);
1636 to = XINT (limit);
1638 else
1639 to = -1;
1641 if (!NILP (string))
1643 CHECK_STRING (string);
1644 if (XINT (pos) < 0 || XINT (pos) > SCHARS (string))
1645 args_out_of_range (string, pos);
1647 else
1649 if (XINT (pos) < BEGV || XINT (pos) > ZV)
1650 args_out_of_range (Fcurrent_buffer (), pos);
1653 if (!find_composition (from, to, &start, &end, &prop, string))
1655 if (!NILP (current_buffer->enable_multibyte_characters)
1656 && FUNCTIONP (Vauto_composition_function)
1657 && find_automatic_composition (from, to, &start, &end, &gstring,
1658 string))
1659 return list3 (make_number (start), make_number (end), gstring);
1660 return Qnil;
1662 if ((end <= XINT (pos) || start > XINT (pos)))
1664 EMACS_INT s, e;
1666 if (find_automatic_composition (from, to, &s, &e, &gstring, string)
1667 && (e <= XINT (pos) ? e > end : s < start))
1668 return list3 (make_number (start), make_number (end), gstring);
1670 if (!COMPOSITION_VALID_P (start, end, prop))
1671 return Fcons (make_number (start), Fcons (make_number (end),
1672 Fcons (Qnil, Qnil)));
1673 if (NILP (detail_p))
1674 return Fcons (make_number (start), Fcons (make_number (end),
1675 Fcons (Qt, Qnil)));
1677 if (COMPOSITION_REGISTERD_P (prop))
1678 id = COMPOSITION_ID (prop);
1679 else
1681 int start_byte = (NILP (string)
1682 ? CHAR_TO_BYTE (start)
1683 : string_char_to_byte (string, start));
1684 id = get_composition_id (start, start_byte, end - start, prop, string);
1687 if (id >= 0)
1689 Lisp_Object components, relative_p, mod_func;
1690 enum composition_method method = COMPOSITION_METHOD (prop);
1691 int width = composition_table[id]->width;
1693 components = Fcopy_sequence (COMPOSITION_COMPONENTS (prop));
1694 relative_p = (method == COMPOSITION_WITH_RULE_ALTCHARS
1695 ? Qnil : Qt);
1696 mod_func = COMPOSITION_MODIFICATION_FUNC (prop);
1697 tail = Fcons (components,
1698 Fcons (relative_p,
1699 Fcons (mod_func,
1700 Fcons (make_number (width), Qnil))));
1702 else
1703 tail = Qnil;
1705 return Fcons (make_number (start), Fcons (make_number (end), tail));
1709 void
1710 syms_of_composite ()
1712 int i;
1714 Qcomposition = intern ("composition");
1715 staticpro (&Qcomposition);
1717 /* Make a hash table for static composition. */
1719 Lisp_Object args[6];
1720 extern Lisp_Object QCsize;
1722 args[0] = QCtest;
1723 args[1] = Qequal;
1724 args[2] = QCweakness;
1725 /* We used to make the hash table weak so that unreferenced
1726 compositions can be garbage-collected. But, usually once
1727 created compositions are repeatedly used in an Emacs session,
1728 and thus it's not worth to save memory in such a way. So, we
1729 make the table not weak. */
1730 args[3] = Qnil;
1731 args[4] = QCsize;
1732 args[5] = make_number (311);
1733 composition_hash_table = Fmake_hash_table (6, args);
1734 staticpro (&composition_hash_table);
1737 /* Make a hash table for glyph-string. */
1739 Lisp_Object args[6];
1740 extern Lisp_Object QCsize;
1742 args[0] = QCtest;
1743 args[1] = Qequal;
1744 args[2] = QCweakness;
1745 args[3] = Qnil;
1746 args[4] = QCsize;
1747 args[5] = make_number (311);
1748 gstring_hash_table = Fmake_hash_table (6, args);
1749 staticpro (&gstring_hash_table);
1752 staticpro (&gstring_work_headers);
1753 gstring_work_headers = Fmake_vector (make_number (8), Qnil);
1754 for (i = 0; i < 8; i++)
1755 ASET (gstring_work_headers, i, Fmake_vector (make_number (i + 2), Qnil));
1756 staticpro (&gstring_work);
1757 gstring_work = Fmake_vector (make_number (10), Qnil);
1759 /* Text property `composition' should be nonsticky by default. */
1760 Vtext_property_default_nonsticky
1761 = Fcons (Fcons (Qcomposition, Qt), Vtext_property_default_nonsticky);
1763 DEFVAR_LISP ("compose-chars-after-function", &Vcompose_chars_after_function,
1764 doc: /* Function to adjust composition of buffer text.
1766 This function is called with three arguments: FROM, TO, and OBJECT.
1767 FROM and TO specify the range of text whose composition should be
1768 adjusted. OBJECT, if non-nil, is a string that contains the text.
1770 This function is called after a text with `composition' property is
1771 inserted or deleted to keep `composition' property of buffer text
1772 valid.
1774 The default value is the function `compose-chars-after'. */);
1775 Vcompose_chars_after_function = intern ("compose-chars-after");
1777 Qauto_composed = intern ("auto-composed");
1778 staticpro (&Qauto_composed);
1780 Qauto_composition_function = intern ("auto-composition-function");
1781 staticpro (&Qauto_composition_function);
1783 DEFVAR_LISP ("auto-composition-function", &Vauto_composition_function,
1784 doc: /* Function to call to compose characters automatically.
1785 This function is called from the display routine with four arguments:
1786 FROM, TO, WINDOW, and STRING.
1788 If STRING is nil, the function must compose characters in the region
1789 between FROM and TO in the current buffer.
1791 Otherwise, STRING is a string, and FROM and TO are indices into the
1792 string. In this case, the function must compose characters in the
1793 string. */);
1794 Vauto_composition_function = Qnil;
1796 DEFVAR_LISP ("composition-function-table", &Vcomposition_function_table,
1797 doc: /* Char-table of functions for automatic character composition.
1798 For each character that has to be composed automatically with
1799 preceding and/or following characters, this char-table contains
1800 a function to call to compose that character.
1802 The element at index C in the table, if non-nil, is a list of
1803 this form: ([PATTERN PREV-CHARS FUNC] ...)
1805 PATTERN is a regular expression which C and the surrounding
1806 characters must match.
1808 PREV-CHARS is a non-negative integer (less than 4) specifying how many
1809 characters before C to check the matching with PATTERN. If it is 0,
1810 PATTERN must match C and the following characters. If it is 1,
1811 PATTERN must match a character before C and the following characters.
1813 If PREV-CHARS is 0, PATTERN can be nil, which means that the
1814 single character C should be composed.
1816 FUNC is a function to return a glyph-string representing a
1817 composition of the characters that match PATTERN. It is
1818 called with one argument GSTRING.
1820 GSTRING is a template of a glyph-string to return. It is already
1821 filled with a proper header for the characters to compose, and
1822 glyphs corresponding to those characters one by one. The
1823 function must return a new glyph-string with the same header as
1824 GSTRING, or modify GSTRING itself and return it.
1826 See also the documentation of `auto-composition-mode'. */);
1827 Vcomposition_function_table = Fmake_char_table (Qnil, Qnil);
1829 defsubr (&Scompose_region_internal);
1830 defsubr (&Scompose_string_internal);
1831 defsubr (&Sfind_composition_internal);
1832 defsubr (&Scomposition_get_gstring);
1835 /* arch-tag: 79cefaf8-ca48-4eed-97e5-d5afb290d272
1836 (do not change this comment) */