/
gtksourcecontextengine.c
7208 lines (6091 loc) · 175 KB
/
gtksourcecontextengine.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* -*- mode: c; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8; coding: utf-8 -*-
* gtksourcecontextengine.c
*
* Copyright (C) 2003 - Gustavo Giráldez <gustavo.giraldez@gmx.net>
* Copyright (C) 2005, 2006 - Marco Barisione, Emanuele Aina
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU Library General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include "gtksourceview-i18n.h"
#include "gtksourcecontextengine.h"
#include "gtktextregion.h"
#include "gtksourcelanguage-private.h"
#include "gtksourcebuffer.h"
#include "gtksourcestyle-private.h"
#include <glib.h>
#include <errno.h>
#include <string.h>
#undef ENABLE_DEBUG
#undef ENABLE_PROFILE
#undef ENABLE_CHECK_TREE
#undef ENABLE_MEMORY_DEBUG /* define it to make it print memory usage information */
/* it won't work with GRegex */
#ifdef ENABLE_DEBUG
#define DEBUG(x) (x)
#else
#define DEBUG(x)
#endif
#ifdef ENABLE_PROFILE
#define PROFILE(x) (x)
#else
#define PROFILE(x)
#endif
#if defined (ENABLE_DEBUG) || defined (ENABLE_PROFILE) || \
defined (ENABLE_CHECK_TREE)
#define NEED_DEBUG_ID
#endif
/* Regex used to match "\%{...@start}". */
#define START_REF_REGEX "(?<!\\\\)(\\\\\\\\)*\\\\%\\{(.*?)@start\\}"
/* Priority of one-time idle which is installed after buffer is modified. */
#define FIRST_UPDATE_PRIORITY G_PRIORITY_HIGH_IDLE
/* Maximal amount of time allowed to spent in this first idle. Should be
* small enough, since in worst case we block ui for this time after each keypress.
*/
#define FIRST_UPDATE_TIME_SLICE 10
/* Priority of long running idle which is used to analyze whole buffer, if
* the engine wasn't quick enough to analyze it in one shot. */
/* FIXME this priority is low, since we don't want to block other gui stuff.
* But, e.g. if we have a big file, and scroll down, we do want the engine
* to analyze quickly. Perhaps we want to reinstall first_update in case
* of expose events or something. */
#define INCREMENTAL_UPDATE_PRIORITY G_PRIORITY_LOW
/* Maximal amount of time allowed to spent in one cycle of background idle. */
#define INCREMENTAL_UPDATE_TIME_SLICE 30
/* Maximal amount of time allowed to spent highlihting a single line. If it
* is not enough, then highlighting is disabled. */
#define MAX_TIME_FOR_ONE_LINE 2000
#define GTK_SOURCE_CONTEXT_ENGINE_ERROR (gtk_source_context_engine_error_quark ())
/* Returns the definition corrsponding to the specified id. */
#define LOOKUP_DEFINITION(ctx_data, id) \
(g_hash_table_lookup ((ctx_data)->definitions, (id)))
#define HAS_OPTION(def,opt) (((def)->flags & GTK_SOURCE_CONTEXT_##opt) != 0)
/* Can the context be terminated by ancestor? */
/* Root context can't be terminated; its child may not be terminated by it;
* grandchildren look at the flag */
#define ANCESTOR_CAN_END_CONTEXT(ctx) \
((ctx)->parent != NULL && (ctx)->parent->parent != NULL && \
(!HAS_OPTION ((ctx)->definition, EXTEND_PARENT) || !(ctx)->all_ancestors_extend))
/* Root context and its children have this TRUE; grandchildren use the flag */
#define CONTEXT_EXTENDS_PARENT(ctx) \
((ctx)->parent == NULL || (ctx)->parent->parent == NULL || \
HAS_OPTION ((ctx)->definition, EXTEND_PARENT))
/* Root and its children have this FALSE; grandchildren use the flag */
#define CONTEXT_ENDS_PARENT(ctx) \
((ctx)->parent != NULL && (ctx)->parent->parent != NULL && \
HAS_OPTION ((ctx)->definition, END_PARENT))
#define SEGMENT_ENDS_PARENT(s) CONTEXT_ENDS_PARENT ((s)->context)
/* Does the segment terminate at line end? */
/* Root segment doesn't, children look at the flag */
#define CONTEXT_END_AT_LINE_END(ctx) \
((ctx)->parent != NULL && HAS_OPTION ((ctx)->definition, END_AT_LINE_END))
#define SEGMENT_END_AT_LINE_END(s) CONTEXT_END_AT_LINE_END((s)->context)
#define CONTEXT_IS_SIMPLE(c) ((c)->definition->type == CONTEXT_TYPE_SIMPLE)
#define CONTEXT_IS_CONTAINER(c) ((c)->definition->type == CONTEXT_TYPE_CONTAINER)
#define SEGMENT_IS_INVALID(s) ((s)->context == NULL)
#define SEGMENT_IS_SIMPLE(s) CONTEXT_IS_SIMPLE ((s)->context)
#define SEGMENT_IS_CONTAINER(s) CONTEXT_IS_CONTAINER ((s)->context)
#define ENGINE_ID(ce) ((ce)->priv->ctx_data->lang->priv->id)
#define ENGINE_STYLES_MAP(ce) ((ce)->priv->ctx_data->lang->priv->styles)
#define TAG_CONTEXT_CLASS_NAME "GtkSourceViewTagContextClassName"
typedef struct _RegexInfo RegexInfo;
typedef struct _RegexAndMatch RegexAndMatch;
typedef struct _Regex Regex;
typedef struct _SubPatternDefinition SubPatternDefinition;
typedef struct _SubPattern SubPattern;
typedef struct _Segment Segment;
typedef struct _Context Context;
typedef struct _ContextPtr ContextPtr;
typedef struct _ContextDefinition ContextDefinition;
typedef struct _DefinitionChild DefinitionChild;
typedef struct _DefinitionsIter DefinitionsIter;
typedef struct _LineInfo LineInfo;
typedef struct _InvalidRegion InvalidRegion;
typedef struct _ContextClassTag ContextClassTag;
typedef enum {
GTK_SOURCE_CONTEXT_ENGINE_ERROR_DUPLICATED_ID = 0,
GTK_SOURCE_CONTEXT_ENGINE_ERROR_INVALID_ARGS,
GTK_SOURCE_CONTEXT_ENGINE_ERROR_INVALID_PARENT,
GTK_SOURCE_CONTEXT_ENGINE_ERROR_INVALID_REF,
GTK_SOURCE_CONTEXT_ENGINE_ERROR_INVALID_WHERE,
GTK_SOURCE_CONTEXT_ENGINE_ERROR_INVALID_START_REF,
GTK_SOURCE_CONTEXT_ENGINE_ERROR_INVALID_REGEX,
GTK_SOURCE_CONTEXT_ENGINE_ERROR_INVALID_STYLE,
GTK_SOURCE_CONTEXT_ENGINE_ERROR_BAD_FILE
} GtkSourceContextEngineError;
typedef enum {
CONTEXT_TYPE_SIMPLE = 0,
CONTEXT_TYPE_CONTAINER
} ContextType;
typedef enum {
SUB_PATTERN_WHERE_DEFAULT = 0,
SUB_PATTERN_WHERE_START,
SUB_PATTERN_WHERE_END
} SubPatternWhere;
struct _RegexInfo
{
gchar *pattern;
GRegexCompileFlags flags;
};
/* glib has now so fscking nice API! */
struct _RegexAndMatch
{
GRegex *regex;
GMatchInfo *match;
};
/* We do not use directly GRegex to allow the use of "\%{...@start}". */
struct _Regex
{
union {
RegexAndMatch regex;
RegexInfo info;
} u;
guint ref_count;
guint resolved : 1;
};
struct _ContextDefinition
{
gchar *id;
ContextType type;
union
{
Regex *match;
struct {
Regex *start;
Regex *end;
} start_end;
} u;
/* Name of the style used for contexts of this type. */
gchar *default_style;
/* This is a list of DefinitionChild pointers. */
GSList *children;
/* Sub patterns (list of SubPatternDefinition pointers.) */
GSList *sub_patterns;
guint n_sub_patterns;
/* List of class definitions */
GSList *context_classes;
/* Union of every regular expression we can find from this
* context. */
Regex *reg_all;
guint flags : 8;
guint ref_count : 24;
};
struct _SubPatternDefinition
{
#ifdef NEED_DEBUG_ID
/* We need the id only for debugging. */
gchar *id;
#endif
gchar *style;
SubPatternWhere where;
/* List of class definitions */
GSList *context_classes;
/* index in the ContextDefinition's list */
guint index;
union
{
gint num;
gchar *name;
} u;
guint is_named : 1;
};
struct _DefinitionChild
{
union
{
/* Equal to definition->id, used when it's not resolved yet */
gchar *id;
ContextDefinition *definition;
} u;
gchar *style;
/* Whether this child is a reference to all child contexts of
* <definition>. */
guint is_ref_all : 1;
/* Whether it is resolved, i.e. points to actual context definition. */
guint resolved : 1;
/* Whether style is overridden, i.e. use child->style instead of what definition says. */
guint override_style : 1;
/* Whether style should be ignored for this and all child contexts. */
guint override_style_deep : 1;
};
struct _DefinitionsIter
{
GSList *children_stack;
};
struct _Context
{
/* Definition for the context. */
ContextDefinition *definition;
Context *parent;
ContextPtr *children;
/* This is the regex returned by regex_resolve() called on
* definition->start_end.end. */
Regex *end;
/* The regular expression containing every regular expression that
* could be matched in this context. */
Regex *reg_all;
/* Either definition->default_style or child_def->style, not copied. */
const gchar *style;
GtkTextTag *tag;
GtkTextTag **subpattern_tags;
/* Cache for generated list of class tags */
GSList *context_classes;
/* Cache for generated list of subpattern class tags */
GSList **subpattern_context_classes;
guint ref_count;
/* see context_freeze() */
guint frozen : 1;
/* Do all the ancestors extend their parent? */
guint all_ancestors_extend : 1;
/* Do not apply styles to children contexts */
guint ignore_children_style : 1;
};
struct _ContextPtr
{
ContextDefinition *definition;
ContextPtr *next;
union {
Context *context;
GHashTable *hash; /* char* -> Context* */
} u;
guint fixed : 1;
};
struct _GtkSourceContextReplace
{
gchar *id;
gchar *replace_with;
};
struct _Segment
{
Segment *parent;
Segment *next;
Segment *prev;
Segment *children;
Segment *last_child;
/* This is NULL if and only if it's a dummy segment which denotes
* inserted or deleted text. */
Context *context;
/* Subpatterns found in this segment. */
SubPattern *sub_patterns;
/* The context is used in the interval [start_at; end_at). */
gint start_at;
gint end_at;
/* In case of container contexts, start_len/end_len is length in chars
* of start/end match. */
gint start_len;
gint end_len;
/* Whether this segment is a whole good segment, or it's an
* an end of bigger one left after erase_segments() call. */
guint is_start : 1;
};
struct _SubPattern
{
SubPatternDefinition *definition;
gint start_at;
gint end_at;
SubPattern *next;
};
/* Line terminator characters (\n, \r, \r\n, or unicode paragraph separator)
* are removed from the line text. The problem is that pcre does not understand
* arbitrary line terminators, so $ in pcre means (?=\n) (not quite, it's also
* end of matched string), while we really need "((?=\r\n)|(?=[\r\n])|(?=\xE2\x80\xA9)|$)".
* It could be worked around by replacing line terminator in matched text with
* \n, but it's a good source of errors, since offsets (not all, unfortunately) returned
* from pcre need to be compared to line length, and adjusted when necessary.
* Not using line terminator only means that \n can't be in patterns, it's not a
* big deal: line end can't be highlighted anyway; if a rule needs to match it, it can
* can use "$" as start and "^" as end (not in a single pattern of course, "$^" will
* never match).
*
* UPDATE: the above isn't true anymore, pcre can do arbitrary line terminators.
* BUT: how do we know whether we should get one/two/N lines to match? Single-line
* case to highlight end of line is covered by above ($). I do not feel brave enough
* to modify this now for no real benefit. (muntyan)
*/
#define NEXT_LINE_OFFSET(l_) ((l_)->start_at + (l_)->char_length + (l_)->eol_length)
struct _LineInfo
{
/* Line text. */
gchar *text;
/* Character offset of the line in text buffer. */
gint start_at;
/* Character length of line terminator, or 0 if it's the
* last line in buffer. */
gint eol_length;
/* Length of the line text not including line terminator */
gint char_length;
gint byte_length;
};
struct _InvalidRegion
{
gboolean empty;
GtkTextMark *start;
GtkTextMark *end;
/* offset_at(end) - delta == original offset,
* i.e. offset in the tree */
gint delta;
};
struct _GtkSourceContextClass
{
gchar *name;
gboolean enabled;
};
struct _ContextClassTag
{
GtkTextTag *tag;
gboolean enabled;
};
struct _GtkSourceContextData
{
guint ref_count;
GtkSourceLanguage *lang;
/* Contains every ContextDefinition indexed by its id. */
GHashTable *definitions;
};
struct _GtkSourceContextEnginePrivate
{
GtkSourceContextData *ctx_data;
GtkTextBuffer *buffer;
GtkSourceStyleScheme *style_scheme;
/* All tags indexed by style name: values are GSList's of tags, ref()'ed. */
GHashTable *tags;
/* Number of all syntax tags created by the engine, needed to set correct
* tag priorities */
guint n_tags;
GHashTable *context_classes;
/* Whether or not to actually highlight the buffer. */
gboolean highlight;
/* Whether highlighting was disabled because of errors. */
gboolean disabled;
/* Region covering the unhighlighted text. */
GtkTextRegion *refresh_region;
/* Tree of contexts. */
Context *root_context;
Segment *root_segment;
Segment *hint;
Segment *hint2;
/* list of Segment* */
GSList *invalid;
InvalidRegion invalid_region;
guint first_update;
guint incremental_update;
/* Views highlight requests. */
GtkTextRegion *highlight_requests;
#ifdef ENABLE_MEMORY_DEBUG
guint mem_usage_timeout;
#endif
};
#ifdef ENABLE_CHECK_TREE
static void check_tree (GtkSourceContextEngine *ce);
static void check_segment_list (Segment *segment);
static void check_segment_children (Segment *segment);
#define CHECK_TREE check_tree
#define CHECK_SEGMENT_LIST check_segment_list
#define CHECK_SEGMENT_CHILDREN check_segment_children
#else
#define CHECK_TREE(ce)
#define CHECK_SEGMENT_LIST(s)
#define CHECK_SEGMENT_CHILDREN(s)
#endif
static GQuark gtk_source_context_engine_error_quark (void) G_GNUC_CONST;
static Segment *create_segment (GtkSourceContextEngine *ce,
Segment *parent,
Context *context,
gint start_at,
gint end_at,
gboolean is_start,
Segment *hint);
static Segment *segment_new (GtkSourceContextEngine *ce,
Segment *parent,
Context *context,
gint start_at,
gint end_at,
gboolean is_start);
static Context *context_new (Context *parent,
ContextDefinition *definition,
const gchar *line_text,
const gchar *style,
gboolean ignore_children_style);
static void context_unref (Context *context);
static void context_freeze (Context *context);
static void context_thaw (Context *context);
static void erase_segments (GtkSourceContextEngine *ce,
gint start,
gint end,
Segment *hint);
static void segment_remove (GtkSourceContextEngine *ce,
Segment *segment);
static void find_insertion_place (Segment *segment,
gint offset,
Segment **parent,
Segment **prev,
Segment **next,
Segment *hint);
static void segment_destroy (GtkSourceContextEngine *ce,
Segment *segment);
static ContextDefinition *context_definition_ref(ContextDefinition *definition);
static void context_definition_unref(ContextDefinition *definition);
static void segment_extend (Segment *state,
gint end_at);
static Context *ancestor_context_ends_here (Context *state,
LineInfo *line,
gint pos);
static void definition_iter_init (DefinitionsIter *iter,
ContextDefinition *definition);
static DefinitionChild *definition_iter_next (DefinitionsIter *iter);
static void definition_iter_destroy (DefinitionsIter *iter);
static void update_syntax (GtkSourceContextEngine *ce,
const GtkTextIter *end,
gint time);
static void install_idle_worker (GtkSourceContextEngine *ce);
static void install_first_update (GtkSourceContextEngine *ce);
#ifdef ENABLE_MEMORY_DEBUG
static gboolean mem_usage_timeout (GtkSourceContextEngine *ce);
#endif
/* TAGS AND STUFF -------------------------------------------------------------- */
GtkSourceContextClass *
gtk_source_context_class_new (gchar const *name,
gboolean enabled)
{
GtkSourceContextClass *def = g_slice_new (GtkSourceContextClass);
def->name = g_strdup (name);
def->enabled = enabled;
return def;
}
static GtkSourceContextClass *
gtk_source_context_class_copy (GtkSourceContextClass *cclass)
{
return gtk_source_context_class_new (cclass->name, cclass->enabled);
}
void
gtk_source_context_class_free (GtkSourceContextClass *cclass)
{
g_free (cclass->name);
g_slice_free (GtkSourceContextClass, cclass);
}
static ContextClassTag *
context_class_tag_new (GtkTextTag *tag,
gboolean enabled)
{
ContextClassTag *attrtag = g_slice_new (ContextClassTag);
attrtag->tag = tag;
attrtag->enabled = enabled;
return attrtag;
}
static void
context_class_tag_free (ContextClassTag *attrtag)
{
g_slice_free (ContextClassTag, attrtag);
}
struct BufAndIters {
GtkTextBuffer *buffer;
const GtkTextIter *start, *end;
};
static void
unhighlight_region_cb (G_GNUC_UNUSED gpointer style,
GSList *tags,
gpointer user_data)
{
struct BufAndIters *data = user_data;
while (tags != NULL)
{
gtk_text_buffer_remove_tag (data->buffer,
tags->data,
data->start,
data->end);
tags = tags->next;
}
}
static void
unhighlight_region (GtkSourceContextEngine *ce,
const GtkTextIter *start,
const GtkTextIter *end)
{
struct BufAndIters data;
data.buffer = ce->priv->buffer;
data.start = start;
data.end = end;
if (gtk_text_iter_equal (start, end))
return;
g_hash_table_foreach (ce->priv->tags, (GHFunc) unhighlight_region_cb, &data);
}
#define MAX_STYLE_DEPENDENCY_DEPTH 50
static void
set_tag_style (GtkSourceContextEngine *ce,
GtkTextTag *tag,
const gchar *style_id)
{
GtkSourceStyle *style;
const char *map_to = style_id;
int guard = 0;
g_return_if_fail (GTK_IS_TEXT_TAG (tag));
g_return_if_fail (style_id != NULL);
_gtk_source_style_apply (NULL, tag);
if (ce->priv->style_scheme == NULL)
return;
style = gtk_source_style_scheme_get_style (ce->priv->style_scheme, style_id);
while (style == NULL)
{
GtkSourceStyleInfo *info;
if (guard > MAX_STYLE_DEPENDENCY_DEPTH)
{
g_warning ("Potential circular dependency between styles detected for style '%s'", style_id);
break;
}
++guard;
/* FIXME Style references really must be fixed, both parser for
* sane use in lang files, and engine for safe use. */
info = g_hash_table_lookup (ENGINE_STYLES_MAP(ce), map_to);
map_to = (info != NULL) ? info->map_to : NULL;
if (!map_to)
break;
style = gtk_source_style_scheme_get_style (ce->priv->style_scheme, map_to);
}
/* not having style is fine, since parser checks validity of every style reference,
* so we don't need to spit a warning here */
if (style != NULL)
_gtk_source_style_apply (style, tag);
}
static GtkTextTag *
create_tag (GtkSourceContextEngine *ce,
const gchar *style_id)
{
GSList *tags;
GtkTextTag *new_tag;
g_assert (style_id != NULL);
tags = g_hash_table_lookup (ce->priv->tags, style_id);
new_tag = gtk_text_buffer_create_tag (ce->priv->buffer, NULL, NULL);
/* It must have priority lower than user tags but still
* higher than highlighting tags created before */
gtk_text_tag_set_priority (new_tag, ce->priv->n_tags);
set_tag_style (ce, new_tag, style_id);
ce->priv->n_tags += 1;
tags = g_slist_prepend (tags, g_object_ref (new_tag));
g_hash_table_insert (ce->priv->tags, g_strdup (style_id), tags);
return new_tag;
}
/* Find tag which has to be overridden. */
static GtkTextTag *
get_parent_tag (Context *context,
const char *style)
{
while (context != NULL)
{
/* Lang files may repeat same style for nested contexts,
* ignore them. */
if (context->style &&
strcmp (context->style, style) != 0)
{
g_assert (context->tag != NULL);
return context->tag;
}
context = context->parent;
}
return NULL;
}
static GtkTextTag *
get_tag_for_parent (GtkSourceContextEngine *ce,
const char *style,
Context *parent)
{
GSList *tags;
GtkTextTag *parent_tag = NULL;
GtkTextTag *tag;
g_return_val_if_fail (style != NULL, NULL);
parent_tag = get_parent_tag (parent, style);
tags = g_hash_table_lookup (ce->priv->tags, style);
if (tags && (!parent_tag ||
gtk_text_tag_get_priority (tags->data) > gtk_text_tag_get_priority (parent_tag)))
{
GSList *link;
tag = tags->data;
/* Now get the tag with lowest priority, so that tag lists do not grow
* indefinitely. */
for (link = tags->next; link != NULL; link = link->next)
{
if (parent_tag &&
gtk_text_tag_get_priority (link->data) < gtk_text_tag_get_priority (parent_tag))
break;
tag = link->data;
}
}
else
{
tag = create_tag (ce, style);
#ifdef ENABLE_DEBUG
{
GString *style_path = g_string_new (style);
gint n;
while (parent != NULL)
{
if (parent->style != NULL)
{
g_string_prepend (style_path, "/");
g_string_prepend (style_path,
parent->style);
}
parent = parent->parent;
}
tags = g_hash_table_lookup (ce->priv->tags, style);
n = g_slist_length (tags);
g_print ("created %d tag for style %s: %s\n", n, style, style_path->str);
g_string_free (style_path, TRUE);
}
#endif
}
return tag;
}
static GtkTextTag *
get_subpattern_tag (GtkSourceContextEngine *ce,
Context *context,
SubPatternDefinition *sp_def)
{
if (sp_def->style == NULL)
return NULL;
g_assert (sp_def->index < context->definition->n_sub_patterns);
if (context->subpattern_tags == NULL)
context->subpattern_tags = g_new0 (GtkTextTag*, context->definition->n_sub_patterns);
if (context->subpattern_tags[sp_def->index] == NULL)
context->subpattern_tags[sp_def->index] = get_tag_for_parent (ce, sp_def->style, context);
g_return_val_if_fail (context->subpattern_tags[sp_def->index] != NULL, NULL);
return context->subpattern_tags[sp_def->index];
}
static GtkTextTag *
get_context_tag (GtkSourceContextEngine *ce,
Context *context)
{
if (context->style != NULL && context->tag == NULL)
context->tag = get_tag_for_parent (ce,
context->style,
context->parent);
return context->tag;
}
static void
apply_tags (GtkSourceContextEngine *ce,
Segment *segment,
gint start_offset,
gint end_offset)
{
GtkTextTag *tag;
GtkTextIter start_iter, end_iter;
GtkTextBuffer *buffer = ce->priv->buffer;
SubPattern *sp;
Segment *child;
g_assert (segment != NULL);
if (SEGMENT_IS_INVALID (segment))
return;
if (segment->start_at >= end_offset || segment->end_at <= start_offset)
return;
start_offset = MAX (start_offset, segment->start_at);
end_offset = MIN (end_offset, segment->end_at);
tag = get_context_tag (ce, segment->context);
if (tag != NULL)
{
gint style_start_at, style_end_at;
style_start_at = start_offset;
style_end_at = end_offset;
if (HAS_OPTION (segment->context->definition, STYLE_INSIDE))
{
style_start_at = MAX (segment->start_at + segment->start_len, start_offset);
style_end_at = MIN (segment->end_at - segment->end_len, end_offset);
}
if (style_start_at > style_end_at)
{
g_critical ("%s: oops", G_STRLOC);
}
else
{
gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, style_start_at);
end_iter = start_iter;
gtk_text_iter_forward_chars (&end_iter, style_end_at - style_start_at);
gtk_text_buffer_apply_tag (ce->priv->buffer, tag, &start_iter, &end_iter);
}
}
for (sp = segment->sub_patterns; sp != NULL; sp = sp->next)
{
if (sp->start_at >= start_offset && sp->end_at <= end_offset)
{
gint start = MAX (start_offset, sp->start_at);
gint end = MIN (end_offset, sp->end_at);
tag = get_subpattern_tag (ce, segment->context, sp->definition);
if (tag != NULL)
{
gtk_text_buffer_get_iter_at_offset (buffer, &start_iter, start);
end_iter = start_iter;
gtk_text_iter_forward_chars (&end_iter, end - start);
gtk_text_buffer_apply_tag (ce->priv->buffer, tag, &start_iter, &end_iter);
}
}
}
for (child = segment->children;
child != NULL && child->start_at < end_offset;
child = child->next)
{
if (child->end_at > start_offset)
apply_tags (ce, child, start_offset, end_offset);
}
}
/**
* highlight_region:
*
* @ce: a #GtkSourceContextEngine.
* @start: the beginning of the region to highlight.
* @end: the end of the region to highlight.
*
* Highlights the specified region.
*/
static void
highlight_region (GtkSourceContextEngine *ce,
GtkTextIter *start,
GtkTextIter *end)
{
#ifdef ENABLE_PROFILE
GTimer *timer;
#endif
if (gtk_text_iter_starts_line (end))
gtk_text_iter_backward_char (end);
if (gtk_text_iter_compare (start, end) >= 0)
return;
#ifdef ENABLE_PROFILE
timer = g_timer_new ();
#endif
/* First we need to delete tags in the regions. */
unhighlight_region (ce, start, end);
apply_tags (ce, ce->priv->root_segment,
gtk_text_iter_get_offset (start),
gtk_text_iter_get_offset (end));
#ifdef ENABLE_PROFILE
g_print ("highlight (from %d to %d), %g ms elapsed\n",
gtk_text_iter_get_offset (start),
gtk_text_iter_get_offset (end),
g_timer_elapsed (timer, NULL) * 1000);
g_timer_destroy (timer);
#endif
}
/**
* ensure_highlighted:
*
* @ce: a #GtkSourceContextEngine.
* @start: the beginning of the region to highlight.
* @end: the end of the region to highlight.
*
* Updates text tags in reanalyzed parts of given area.
* It applies tags according to whatever is in the syntax
* tree currently, so highlighting may not be correct
* (gtk_source_context_engine_update_highlight is the method
* that actually ensures correct highlighting).
*/
static void
ensure_highlighted (GtkSourceContextEngine *ce,
const GtkTextIter *start,
const GtkTextIter *end)
{
GtkTextRegion *region;
GtkTextRegionIterator reg_iter;
/* Get the subregions not yet highlighted. */
region = gtk_text_region_intersect (ce->priv->refresh_region, start, end);
if (region == NULL)
return;
gtk_text_region_get_iterator (region, ®_iter, 0);
/* Highlight all subregions from the intersection.
* hopefully this will only be one subregion. */
while (!gtk_text_region_iterator_is_end (®_iter))
{
GtkTextIter s, e;
gtk_text_region_iterator_get_subregion (®_iter, &s, &e);
highlight_region (ce, &s, &e);
gtk_text_region_iterator_next (®_iter);
}
gtk_text_region_destroy (region, TRUE);
/* Remove the just highlighted region. */
gtk_text_region_subtract (ce->priv->refresh_region, start, end);
}
static GtkTextTag *
get_context_class_tag (GtkSourceContextEngine *ce,
gchar const *name)
{
GtkTextTag *ret;
ret = g_hash_table_lookup (ce->priv->context_classes, name);
if (ret == NULL)
{
ret = gtk_text_buffer_create_tag (ce->priv->buffer, NULL, NULL);