(struct x_output): Add member wait_for_wm.
[emacs.git] / src / composite.c
blobc18d8eb749ce4bb1b39e60219acb715e4a2e2153
1 /* Composite sequence support.
2 Copyright (C) 1999 Electrotechnical Laboratory, JAPAN.
3 Licensed to the Free Software Foundation.
4 Copyright (C) 2001 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2, or (at your option)
11 any later version.
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21 Boston, MA 02111-1307, USA. */
23 #include <config.h>
24 #include "lisp.h"
25 #include "buffer.h"
26 #include "charset.h"
27 #include "intervals.h"
29 /* Emacs uses special text property `composition' to support character
30 composition. A sequence of characters that have the same (i.e. eq)
31 `composition' property value is treated as a single composite
32 sequence (we call it just `composition' here after). Characters in
33 a composition are all composed somehow on the screen.
35 The property value has this form when the composition is made:
36 ((LENGTH . COMPONENTS) . MODIFICATION-FUNC)
37 then turns to this form:
38 (COMPOSITION-ID . (LENGTH COMPONENTS-VEC . MODIFICATION-FUNC))
39 when the composition is registered in composition_hash_table and
40 composition_table. These rather peculiar structures were designed
41 to make it easy to distinguish them quickly (we can do that by
42 checking only the first element) and to extract LENGTH (from the
43 former form) and COMPOSITION-ID (from the latter form).
45 We register a composition when it is displayed, or when the width
46 is required (for instance, to calculate columns).
48 LENGTH -- Length of the composition. This information is used to
49 check the validity of the composition.
51 COMPONENTS -- Character, string, vector, list, or nil.
53 If it is nil, characters in the text are composed relatively
54 according to their metrics in font glyphs.
56 If it is a character or a string, the character or characters
57 in the string are composed relatively.
59 If it is a vector or list of integers, the element is a
60 character or an encoded composition rule. The characters are
61 composed according to the rules. (2N)th elements are
62 characters to be composed and (2N+1)th elements are
63 composition rules to tell how to compose (2N+2)th element with
64 the previously composed 2N glyphs.
66 COMPONENTS-VEC -- Vector of integers. In relative composition, the
67 elements are characters to be composed. In rule-base
68 composition, the elements are characters or encoded
69 composition rules.
71 MODIFICATION-FUNC -- If non nil, it is a function to call when the
72 composition gets invalid after a modification in a buffer. If
73 it is nil, a function in `composition-function-table' of the
74 first character in the sequence is called.
76 COMPOSITION-ID --Identification number of the composition. It is
77 used as an index to composition_table for the composition.
79 When Emacs has to display a composition or has to know its
80 displaying width, the function get_composition_id is called. It
81 returns COMPOSITION-ID so that the caller can access the
82 information about the composition through composition_table. If a
83 COMPOSITION-ID has not yet been assigned to the composition,
84 get_composition_id checks the validity of `composition' property,
85 and, if valid, assigns a new ID, registers the information in
86 composition_hash_table and composition_table, and changes the form
87 of the property value. If the property is invalid, return -1
88 without changing the property value.
90 We use two tables to keep information about composition;
91 composition_hash_table and composition_table.
93 The former is a hash table in which keys are COMPONENTS-VECs and
94 values are the corresponding COMPOSITION-IDs. This hash table is
95 weak, but as each key (COMPONENTS-VEC) is also kept as a value of
96 `composition' property, it won't be collected as garbage until all
97 text that have the same COMPONENTS-VEC are deleted.
99 The latter is a table of pointers to `struct composition' indexed
100 by COMPOSITION-ID. This structure keep the other information (see
101 composite.h).
103 In general, a text property holds information about individual
104 characters. But, a `composition' property holds information about
105 a sequence of characters (in this sense, it is like `intangible'
106 property). That means that we should not share the property value
107 in adjacent compositions we can't distinguish them if they have the
108 same property. So, after any changes, we call
109 `update_compositions' and change a property of one of adjacent
110 compositions to a copy of it. This function also runs a proper
111 composition modification function to make a composition that gets
112 invalid by the change valid again.
114 As a value of `composition' property holds information about a
115 specific range of text, the value gets invalid if we change the
116 text in the range. We treat `composition' property always
117 rear-nonsticky (currently by setting default-text-properties to
118 (rear-nonsticky (composition))) and we never make properties of
119 adjacent compositions identical. Thus, any such changes make the
120 range just shorter. So, we can check the validity of `composition'
121 property by comparing LENGTH information with the actual length of
122 the composition.
127 Lisp_Object Qcomposition;
129 /* Table of pointers to the structure `composition' indexed by
130 COMPOSITION-ID. This structure is for storing information about
131 each composition except for COMPONENTS-VEC. */
132 struct composition **composition_table;
134 /* The current size of `composition_table'. */
135 static int composition_table_size;
137 /* Number of compositions currently made. */
138 int n_compositions;
140 /* Hash table for compositions. The key is COMPONENTS-VEC of
141 `composition' property. The value is the corresponding
142 COMPOSITION-ID. */
143 Lisp_Object composition_hash_table;
145 /* Function to call to adjust composition. */
146 Lisp_Object Vcompose_chars_after_function;
148 /* Char-table of patterns and functions to make a composition. */
149 Lisp_Object Vcomposition_function_table;
150 Lisp_Object Qcomposition_function_table;
152 /* Temporary variable used in macros COMPOSITION_XXX. */
153 Lisp_Object composition_temp;
155 /* Return how many columns C will occupy on the screen. It always
156 returns 1 for control characters and 8-bit characters because those
157 are just ignored in a composition. */
158 #define CHAR_WIDTH(c) \
159 (SINGLE_BYTE_CHAR_P (c) ? 1 : CHARSET_WIDTH (CHAR_CHARSET (c)))
161 /* The following macros for hash table are copied from fns.c. */
162 /* Value is the key part of entry IDX in hash table H. */
163 #define HASH_KEY(H, IDX) AREF ((H)->key_and_value, 2 * (IDX))
164 /* Value is the value part of entry IDX in hash table H. */
165 #define HASH_VALUE(H, IDX) AREF ((H)->key_and_value, 2 * (IDX) + 1)
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 (charpos, bytepos, nchars, prop, string)
176 int charpos, bytepos, nchars;
177 Lisp_Object prop, string;
179 Lisp_Object id, length, components, key, *key_contents;
180 int glyph_len;
181 struct Lisp_Hash_Table *hash_table = XHASH_TABLE (composition_hash_table);
182 int hash_index;
183 unsigned hash_code;
184 struct composition *cmp;
185 int i, 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 XCAR (prop) = id;
253 XCDR (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 */
281 if (VECTORP (components) || CONSP (components))
283 int len = XVECTOR (key)->size;
285 /* The number of elements should be odd. */
286 if ((len % 2) == 0)
287 goto invalid_composition;
288 /* All elements should be integers (character or encoded
289 composition rule). */
290 for (i = 0; i < len; i++)
292 if (!INTEGERP (key_contents[i]))
293 goto invalid_composition;
297 /* Change PROP from Form-A above to Form-B. We can directly modify
298 the cons cell of PROP because it is not shared. */
299 XSETFASTINT (id, n_compositions);
300 XCAR (prop) = id;
301 XCDR (prop) = Fcons (make_number (nchars), Fcons (key, XCDR (prop)));
303 /* Register the composition in composition_hash_table. */
304 hash_index = hash_put (hash_table, key, id, hash_code);
306 /* Register the composition in composition_table. */
307 cmp = (struct composition *) xmalloc (sizeof (struct composition));
309 cmp->method = (NILP (components)
310 ? COMPOSITION_RELATIVE
311 : ((INTEGERP (components) || STRINGP (components))
312 ? COMPOSITION_WITH_ALTCHARS
313 : COMPOSITION_WITH_RULE_ALTCHARS));
314 cmp->hash_index = hash_index;
315 glyph_len = (cmp->method == COMPOSITION_WITH_RULE_ALTCHARS
316 ? (XVECTOR (key)->size + 1) / 2
317 : XVECTOR (key)->size);
318 cmp->glyph_len = glyph_len;
319 cmp->offsets = (short *) xmalloc (sizeof (short) * glyph_len * 2);
320 cmp->font = NULL;
322 /* Calculate the width of overall glyphs of the composition. */
323 if (cmp->method != COMPOSITION_WITH_RULE_ALTCHARS)
325 /* Relative composition. */
326 cmp->width = 0;
327 for (i = 0; i < glyph_len; i++)
329 int this_width;
330 ch = XINT (key_contents[i]);
331 this_width = CHAR_WIDTH (ch);
332 if (cmp->width < this_width)
333 cmp->width = this_width;
336 else
338 /* Rule-base composition. */
339 float leftmost = 0.0, rightmost;
341 ch = XINT (key_contents[0]);
342 rightmost = CHAR_WIDTH (ch);
344 for (i = 1; i < glyph_len; i += 2)
346 int rule, gref, nref;
347 int this_width;
348 float this_left;
350 rule = XINT (key_contents[i]);
351 ch = XINT (key_contents[i + 1]);
352 this_width = CHAR_WIDTH (ch);
354 /* A composition rule is specified by an integer value
355 that encodes global and new reference points (GREF and
356 NREF). GREF and NREF are specified by numbers as
357 below:
358 0---1---2 -- ascent
362 9--10--11 -- center
364 ---3---4---5--- baseline
366 6---7---8 -- descent
368 COMPOSITION_DECODE_RULE (rule, gref, nref);
369 this_left = (leftmost
370 + (gref % 3) * (rightmost - leftmost) / 2.0
371 - (nref % 3) * this_width / 2.0);
373 if (this_left < leftmost)
374 leftmost = this_left;
375 if (this_left + this_width > rightmost)
376 rightmost = this_left + this_width;
379 cmp->width = rightmost - leftmost;
380 if (cmp->width < (rightmost - leftmost))
381 /* To get a ceiling integer value. */
382 cmp->width++;
385 composition_table[n_compositions] = cmp;
387 return n_compositions++;
389 invalid_composition:
390 /* Would it be better to remove this `composition' property? */
391 return -1;
395 /* Find a composition at or nearest to position POS of OBJECT (buffer
396 or string).
398 OBJECT defaults to the current buffer. If there's a composition at
399 POS, set *START and *END to the start and end of the sequence,
400 *PROP to the `composition' property, and return 1.
402 If there's no composition at POS and LIMIT is negative, return 0.
404 Otherwise, search for a composition forward (LIMIT > POS) or
405 backward (LIMIT < POS). In this case, LIMIT bounds the search.
407 If a composition is found, set *START, *END, and *PROP as above,
408 and return 1, else return 0.
410 This doesn't check the validity of composition. */
413 find_composition (pos, limit, start, end, prop, object)
414 int pos, limit, *start, *end;
415 Lisp_Object *prop, object;
417 Lisp_Object val;
419 if (get_property_and_range (pos, Qcomposition, prop, start, end, object))
420 return 1;
422 if (limit < 0 || limit == pos)
423 return 0;
425 if (limit > pos) /* search forward */
427 val = Fnext_single_property_change (make_number (pos), Qcomposition,
428 object, make_number (limit));
429 pos = XINT (val);
430 if (pos == limit)
431 return 0;
433 else /* search backward */
435 if (get_property_and_range (pos - 1, Qcomposition, prop, start, end,
436 object))
437 return 1;
438 val = Fprevious_single_property_change (make_number (pos), Qcomposition,
439 object, make_number (limit));
440 pos = XINT (val);
441 if (pos == limit)
442 return 0;
443 pos--;
445 get_property_and_range (pos, Qcomposition, prop, start, end, object);
446 return 1;
449 /* Run a proper function to adjust the composition sitting between
450 FROM and TO with property PROP. */
452 static void
453 run_composition_function (from, to, prop)
454 int from, to;
455 Lisp_Object prop;
457 Lisp_Object func;
458 int start, end;
460 func = COMPOSITION_MODIFICATION_FUNC (prop);
461 /* If an invalid composition precedes or follows, try to make them
462 valid too. */
463 if (from > BEGV
464 && find_composition (from - 1, -1, &start, &end, &prop, Qnil)
465 && !COMPOSITION_VALID_P (start, end, prop))
466 from = start;
467 if (to < ZV
468 && find_composition (to, -1, &start, &end, &prop, Qnil)
469 && !COMPOSITION_VALID_P (start, end, prop))
470 to = end;
471 if (!NILP (func))
472 call2 (func, make_number (from), make_number (to));
473 else if (!NILP (Ffboundp (Vcompose_chars_after_function)))
474 call3 (Vcompose_chars_after_function,
475 make_number (from), make_number (to), Qnil);
478 /* Make invalid compositions adjacent to or inside FROM and TO valid.
479 CHECK_MASK is bitwise `or' of mask bits defined by macros
480 CHECK_XXX (see the comment in composite.h).
482 This function is called when a buffer text is changed. If the
483 change is deletion, FROM == TO. Otherwise, FROM < TO. */
485 void
486 update_compositions (from, to, check_mask)
487 int from, to;
489 Lisp_Object prop;
490 int start, end;
492 /* If FROM and TO are not in a valid range, do nothing. */
493 if (! (BEGV <= from && from <= to && to <= ZV))
494 return;
496 if (check_mask & CHECK_HEAD)
498 /* FROM should be at composition boundary. But, insertion or
499 deletion will make two compositions adjacent and
500 indistinguishable when they have same (eq) property. To
501 avoid it, in such a case, we change the property of the
502 latter to the copy of it. */
503 if (from > BEGV
504 && find_composition (from - 1, -1, &start, &end, &prop, Qnil))
506 if (from < end)
507 Fput_text_property (make_number (from), make_number (end),
508 Qcomposition,
509 Fcons (XCAR (prop), XCDR (prop)), Qnil);
510 run_composition_function (start, end, prop);
511 from = end;
513 else if (from < ZV
514 && find_composition (from, -1, &start, &from, &prop, Qnil))
515 run_composition_function (start, from, prop);
518 if (check_mask & CHECK_INSIDE)
520 /* In this case, we are sure that (check & CHECK_TAIL) is also
521 nonzero. Thus, here we should check only compositions before
522 (to - 1). */
523 while (from < to - 1
524 && find_composition (from, to, &start, &from, &prop, Qnil)
525 && from < to - 1)
526 run_composition_function (start, from, prop);
529 if (check_mask & CHECK_TAIL)
531 if (from < to
532 && find_composition (to - 1, -1, &start, &end, &prop, Qnil))
534 /* TO should be also at composition boundary. But,
535 insertion or deletion will make two compositions adjacent
536 and indistinguishable when they have same (eq) property.
537 To avoid it, in such a case, we change the property of
538 the former to the copy of it. */
539 if (to < end)
540 Fput_text_property (make_number (start), make_number (to),
541 Qcomposition,
542 Fcons (XCAR (prop), XCDR (prop)), Qnil);
543 run_composition_function (start, end, prop);
545 else if (to < ZV
546 && find_composition (to, -1, &start, &end, &prop, Qnil))
547 run_composition_function (start, end, prop);
552 /* Modify composition property values in LIST destructively. LIST is
553 a list as returned from text_property_list. Change values to the
554 top-level copies of them so that none of them are `eq'. */
556 void
557 make_composition_value_copy (list)
558 Lisp_Object list;
560 Lisp_Object plist, val;
562 for (; CONSP (list); list = XCDR (list))
564 plist = XCAR (XCDR (XCDR (XCAR (list))));
565 while (CONSP (plist) && CONSP (XCDR (plist)))
567 if (EQ (XCAR (plist), Qcomposition)
568 && (val = XCAR (XCDR (plist)), CONSP (val)))
569 XCAR (XCDR (plist)) = Fcons (XCAR (val), XCDR (val));
570 plist = XCDR (XCDR (plist));
576 /* Make text in the region between START and END a composition that
577 has COMPONENTS and MODIFICATION-FUNC.
579 If STRING is non-nil, then operate on characters contained between
580 indices START and END in STRING. */
582 void
583 compose_text (start, end, components, modification_func, string)
584 int start, end;
585 Lisp_Object components, modification_func, string;
587 Lisp_Object prop;
589 prop = Fcons (Fcons (make_number (end - start), components),
590 modification_func);
591 Fput_text_property (make_number (start), make_number (end),
592 Qcomposition, prop, string);
595 /* Compose sequences of characters in the region between START and END
596 by functions registered in Vcomposition_function_table. If STRING
597 is non-nil, operate on characters contained between indices START
598 and END in STRING. */
600 void
601 compose_chars_in_text (start, end, string)
602 int start, end;
603 Lisp_Object string;
605 int count;
606 struct gcpro gcpro1;
607 Lisp_Object tail, elt, val, to;
608 /* Set to nonzero if we don't have to compose ASCII characters. */
609 int skip_ascii;
610 int i, len, stop, c;
611 unsigned char *ptr, *pend;
613 if (! CHAR_TABLE_P (Vcomposition_function_table))
614 return;
616 if (STRINGP (string))
618 count = specpdl_ptr - specpdl;
619 GCPRO1 (string);
620 stop = end;
621 ptr = XSTRING (string)->data + string_char_to_byte (string, start);
622 pend = ptr + STRING_BYTES (XSTRING (string));
624 else
626 record_unwind_protect (save_excursion_restore, save_excursion_save ());
627 TEMP_SET_PT (start);
628 stop = (start < GPT && GPT < end ? GPT : end);
629 ptr = CHAR_POS_ADDR (start);
630 pend = CHAR_POS_ADDR (end);
633 /* Preserve the match data. */
634 record_unwind_protect (Fset_match_data, Fmatch_data (Qnil, Qnil));
636 /* If none of ASCII characters have composition functions, we can
637 skip them quickly. */
638 for (i = 0; i < 128; i++)
639 if (!NILP (CHAR_TABLE_REF (Vcomposition_function_table, i)))
640 break;
641 skip_ascii = (i == 128);
644 while (1)
646 if (skip_ascii)
647 while (start < stop && ASCII_BYTE_P (*ptr))
648 start++, ptr++;
650 if (start >= stop)
652 if (stop == end || start >= end)
653 break;
654 stop = end;
655 if (STRINGP (string))
656 ptr = XSTRING (string)->data + string_char_to_byte (string, start);
657 else
658 ptr = CHAR_POS_ADDR (start);
661 c = STRING_CHAR_AND_LENGTH (ptr, pend - ptr, len);
662 tail = CHAR_TABLE_REF (Vcomposition_function_table, c);
663 while (CONSP (tail))
665 elt = XCAR (tail);
666 if (CONSP (elt)
667 && STRINGP (XCAR (elt))
668 && !NILP (Ffboundp (XCDR (elt))))
670 if (STRINGP (string))
671 val = Fstring_match (XCAR (elt), string, make_number (start));
672 else
674 val = Flooking_at (XCAR (elt));
675 if (!NILP (val))
676 val = make_number (start);
678 if (INTEGERP (val) && XFASTINT (val) == start)
680 to = Fmatch_end (make_number (0));
681 val = call4 (XCDR (elt), val, to, XCAR (elt), string);
682 if (INTEGERP (val) && XINT (val) > 1)
684 start += XINT (val);
685 if (STRINGP (string))
686 ptr = XSTRING (string)->data + string_char_to_byte (string, start);
687 else
688 ptr = CHAR_POS_ADDR (start);
690 else
692 start++;
693 ptr += len;
695 break;
698 tail = XCDR (tail);
700 if (!CONSP (tail))
702 /* No composition done. Try the next character. */
703 start++;
704 ptr += len;
708 unbind_to (count, Qnil);
709 if (STRINGP (string))
710 UNGCPRO;
713 /* Emacs Lisp APIs. */
715 DEFUN ("compose-region-internal", Fcompose_region_internal,
716 Scompose_region_internal, 2, 4, 0,
717 "Internal use only.\n\
719 Compose text in the region between START and END.\n\
720 Optional 3rd and 4th arguments are COMPONENTS and MODIFICATION-FUNC\n\
721 for the composition. See `compose-region' for more detial.")
722 (start, end, components, mod_func)
723 Lisp_Object start, end, components, mod_func;
725 validate_region (&start, &end);
726 if (!NILP (components)
727 && !INTEGERP (components)
728 && !CONSP (components)
729 && !STRINGP (components))
730 CHECK_VECTOR (components, 2);
732 compose_text (XINT (start), XINT (end), components, mod_func, Qnil);
733 return Qnil;
736 DEFUN ("compose-string-internal", Fcompose_string_internal,
737 Scompose_string_internal, 3, 5, 0,
738 "Internal use only.\n\
740 Compose text between indices START and END of STRING.\n\
741 Optional 4th and 5th arguments are COMPONENTS and MODIFICATION-FUNC\n\
742 for the composition. See `compose-string' for more detial.")
743 (string, start, end, components, mod_func)
744 Lisp_Object string, start, end, components, mod_func;
746 CHECK_STRING (string, 0);
747 CHECK_NUMBER (start, 1);
748 CHECK_NUMBER (end, 2);
750 if (XINT (start) < 0 ||
751 XINT (start) > XINT (end)
752 || XINT (end) > XSTRING (string)->size)
753 args_out_of_range (start, end);
755 compose_text (XINT (start), XINT (end), components, mod_func, string);
756 return string;
759 DEFUN ("find-composition-internal", Ffind_composition_internal,
760 Sfind_composition_internal, 4, 4, 0,
761 "Internal use only.\n\
763 Return information about composition at or nearest to position POS.\n\
764 See `find-composition' for more detail.")
765 (pos, limit, string, detail_p)
766 Lisp_Object pos, limit, string, detail_p;
768 Lisp_Object prop, tail;
769 int start, end;
770 int id;
772 CHECK_NUMBER_COERCE_MARKER (pos, 0);
773 start = XINT (pos);
774 if (!NILP (limit))
776 CHECK_NUMBER_COERCE_MARKER (limit, 1);
777 end = XINT (limit);
779 else
780 end = -1;
782 if (!NILP (string))
784 CHECK_STRING (string, 2);
785 if (XINT (pos) < 0 || XINT (pos) > XSTRING (string)->size)
786 args_out_of_range (string, pos);
788 else
790 if (XINT (pos) < BEGV || XINT (pos) > ZV)
791 args_out_of_range (Fcurrent_buffer (), pos);
794 if (!find_composition (start, end, &start, &end, &prop, string))
795 return Qnil;
796 if (!COMPOSITION_VALID_P (start, end, prop))
797 return Fcons (make_number (start), Fcons (make_number (end),
798 Fcons (Qnil, Qnil)));
799 if (NILP (detail_p))
800 return Fcons (make_number (start), Fcons (make_number (end),
801 Fcons (Qt, Qnil)));
803 if (COMPOSITION_REGISTERD_P (prop))
804 id = COMPOSITION_ID (prop);
805 else
807 int start_byte = (NILP (string)
808 ? CHAR_TO_BYTE (start)
809 : string_char_to_byte (string, start));
810 id = get_composition_id (start, start_byte, end - start, prop, string);
813 if (id >= 0)
815 Lisp_Object components, relative_p, mod_func;
816 enum composition_method method = COMPOSITION_METHOD (prop);
817 int width = composition_table[id]->width;
819 components = Fcopy_sequence (COMPOSITION_COMPONENTS (prop));
820 relative_p = (method == COMPOSITION_WITH_RULE_ALTCHARS
821 ? Qnil : Qt);
822 mod_func = COMPOSITION_MODIFICATION_FUNC (prop);
823 tail = Fcons (components,
824 Fcons (relative_p,
825 Fcons (mod_func,
826 Fcons (make_number (width), Qnil))));
828 else
829 tail = Qnil;
831 return Fcons (make_number (start), Fcons (make_number (end), tail));
835 void
836 syms_of_composite ()
838 Qcomposition = intern ("composition");
839 staticpro (&Qcomposition);
841 /* Make a hash table for composition. */
843 Lisp_Object args[6];
844 extern Lisp_Object QCsize;
846 args[0] = QCtest;
847 args[1] = Qequal;
848 args[2] = QCweakness;
849 args[3] = Qnil;
850 args[4] = QCsize;
851 args[5] = make_number (311);
852 composition_hash_table = Fmake_hash_table (6, args);
853 staticpro (&composition_hash_table);
856 /* Text property `composition' should be nonsticky by default. */
857 Vtext_property_default_nonsticky
858 = Fcons (Fcons (Qcomposition, Qt), Vtext_property_default_nonsticky);
860 DEFVAR_LISP ("compose-chars-after-function", &Vcompose_chars_after_function,
861 "Function to adjust composition of buffer text.\n\
863 The function is called with three arguments FROM, TO, and OBJECT.\n\
864 FROM and TO specify the range of text of which composition should be\n\
865 adjusted. OBJECT, if non-nil, is a string that contains the text.\n\
867 This function is called after a text with `composition' property is\n\
868 inserted or deleted to keep `composition' property of buffer text\n\
869 valid.\n\
871 The default value is the function `compose-chars-after'.");
872 Vcompose_chars_after_function = intern ("compose-chars-after");
874 Qcomposition_function_table = intern ("composition-function-table");
875 staticpro (&Qcomposition_function_table);
877 /* Intern this now in case it isn't already done.
878 Setting this variable twice is harmless.
879 But don't staticpro it here--that is done in alloc.c. */
880 Qchar_table_extra_slots = intern ("char-table-extra-slots");
882 Fput (Qcomposition_function_table, Qchar_table_extra_slots, make_number (0));
884 DEFVAR_LISP ("composition-function-table", &Vcomposition_function_table,
885 "Char table of patterns and functions to make a composition.\n\
887 Each element is nil or an alist of PATTERNs vs FUNCs, where PATTERNs\n\
888 are regular expressions and FUNCs are functions. FUNC is responsible\n\
889 for composing text matching the corresponding PATTERN. FUNC is called\n\
890 with three arguments FROM, TO, and PATTERN. See the function\n\
891 `compose-chars-after' for more detail.\n\
893 This table is looked up by the first character of a composition when\n\
894 the composition gets invalid after a change in a buffer.");
895 Vcomposition_function_table
896 = Fmake_char_table (Qcomposition_function_table, Qnil);
898 defsubr (&Scompose_region_internal);
899 defsubr (&Scompose_string_internal);
900 defsubr (&Sfind_composition_internal);