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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
|
/*
textbuffer-view.c : Text buffer handling
Copyright (C) 1999-2001 Timo Sirainen
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU 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 General Public License for more details.
You should have received a copy of the GNU 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
*/
#define G_LOG_DOMAIN "TextBufferView"
#include "module.h"
#include "textbuffer-view.h"
#include "utf8.h"
typedef struct {
char *name;
LINE_REC *line;
} BOOKMARK_REC;
/* how often to scan line cache for lines not accessed for a while (ms) */
#define LINE_CACHE_CHECK_TIME (5*60*1000)
/* how long to keep line cache in memory (seconds) */
#define LINE_CACHE_KEEP_TIME (10*60)
static int linecache_tag;
static GSList *views;
#define view_is_bottom(view) \
((view)->ypos >= -1 && (view)->ypos < (view)->height)
#define view_get_linecount(view, line) \
textbuffer_view_get_line_cache(view, line)->count
static GSList *textbuffer_get_views(TEXT_BUFFER_REC *buffer)
{
GSList *tmp, *list;
for (tmp = views; tmp != NULL; tmp = tmp->next) {
TEXT_BUFFER_VIEW_REC *view = tmp->data;
if (view->buffer == buffer) {
list = g_slist_copy(view->siblings);
return g_slist_prepend(list, view);
}
}
return NULL;
}
static TEXT_BUFFER_CACHE_REC *
textbuffer_cache_get(GSList *views, int width)
{
TEXT_BUFFER_CACHE_REC *cache;
/* check if there's existing cache with correct width */
while (views != NULL) {
TEXT_BUFFER_VIEW_REC *view = views->data;
if (view->width == width) {
view->cache->refcount++;
return view->cache;
}
views = views->next;
}
/* create new cache */
cache = g_new0(TEXT_BUFFER_CACHE_REC, 1);
cache->refcount = 1;
cache->width = width;
cache->line_cache = g_hash_table_new((GHashFunc) g_direct_hash,
(GCompareFunc) g_direct_equal);
return cache;
}
static int line_cache_destroy(void *key, LINE_CACHE_REC *cache)
{
g_free(cache);
return TRUE;
}
static void textbuffer_cache_destroy(TEXT_BUFFER_CACHE_REC *cache)
{
g_hash_table_foreach(cache->line_cache,
(GHFunc) line_cache_destroy, NULL);
g_hash_table_destroy(cache->line_cache);
g_free(cache);
}
static void textbuffer_cache_unref(TEXT_BUFFER_CACHE_REC *cache)
{
if (--cache->refcount == 0)
textbuffer_cache_destroy(cache);
}
#define FGATTR (ATTR_NOCOLORS | ATTR_RESETFG | ATTR_BOLD | 0x0f)
#define BGATTR (ATTR_NOCOLORS | ATTR_RESETBG | ATTR_BLINK | 0xf0)
static void update_cmd_color(unsigned char cmd, int *color)
{
if ((cmd & 0x80) == 0) {
if (cmd & LINE_COLOR_BG) {
/* set background color */
*color &= FGATTR;
if ((cmd & LINE_COLOR_DEFAULT) == 0)
*color |= (cmd & 0x0f) << 4;
else {
*color = (*color & FGATTR) | ATTR_RESETBG;
if (cmd & LINE_COLOR_BLINK)
*color |= ATTR_BLINK;
}
} else {
/* set foreground color */
*color &= BGATTR;
if ((cmd & LINE_COLOR_DEFAULT) == 0)
*color |= cmd & 0x0f;
else {
*color = (*color & BGATTR) | ATTR_RESETFG;
if (cmd & LINE_COLOR_BOLD)
*color |= ATTR_BOLD;
}
}
} else switch (cmd) {
case LINE_CMD_UNDERLINE:
*color ^= ATTR_UNDERLINE;
break;
case LINE_CMD_REVERSE:
*color ^= ATTR_REVERSE;
break;
case LINE_CMD_COLOR0:
*color &= BGATTR;
break;
}
}
static LINE_CACHE_REC *
view_update_line_cache(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line)
{
INDENT_FUNC indent_func;
LINE_CACHE_REC *rec;
LINE_CACHE_SUB_REC *sub;
GSList *lines;
unsigned char cmd;
const unsigned char *ptr, *last_space_ptr;
int xpos, pos, indent_pos, last_space, last_color, color, linecount;
g_return_val_if_fail(line->text != NULL, NULL);
color = ATTR_RESETFG | ATTR_RESETBG;
xpos = 0; indent_pos = view->default_indent;
last_space = last_color = 0; last_space_ptr = NULL; sub = NULL;
indent_func = view->default_indent_func;
linecount = 1;
lines = NULL;
for (ptr = line->text;;) {
if (*ptr == '\0') {
/* command */
ptr++;
cmd = *ptr;
ptr++;
if (cmd == LINE_CMD_EOL || cmd == LINE_CMD_FORMAT)
break;
if (cmd == LINE_CMD_CONTINUE) {
unsigned char *tmp;
memcpy(&tmp, ptr, sizeof(char *));
ptr = tmp;
continue;
}
if (cmd == LINE_CMD_INDENT) {
/* set indentation position here - don't do
it if we're too close to right border */
if (xpos < view->width-5) indent_pos = xpos;
} else if (cmd == LINE_CMD_INDENT_FUNC) {
memcpy(&indent_func, ptr, sizeof(INDENT_FUNC));
ptr += sizeof(INDENT_FUNC);
if (indent_func == NULL)
indent_func = view->default_indent_func;
} else
update_cmd_color(cmd, &color);
continue;
}
if (xpos == view->width && sub != NULL &&
(last_space <= indent_pos || last_space <= 10) &&
view->longword_noindent) {
/* long word, remove the indentation from this line */
xpos -= sub->indent;
sub->indent = 0;
}
if (xpos == view->width) {
xpos = indent_func == NULL ? indent_pos :
indent_func(view, line, -1);
sub = g_new0(LINE_CACHE_SUB_REC, 1);
if (last_space > indent_pos && last_space > 10) {
/* go back to last space */
color = last_color;
ptr = last_space_ptr;
while (*ptr == ' ') ptr++;
} else if (view->longword_noindent) {
/* long word, no indentation in next line */
xpos = 0;
sub->continues = TRUE;
}
sub->start = ptr;
sub->indent = xpos;
sub->indent_func = indent_func;
sub->color = color;
lines = g_slist_append(lines, sub);
linecount++;
last_space = 0;
continue;
}
if (view->utf8)
get_utf8_char(&ptr, 6);
xpos++;
if (*ptr++ == ' ') {
last_space = xpos-1;
last_space_ptr = ptr;
last_color = color;
}
}
rec = g_malloc(sizeof(LINE_CACHE_REC)-sizeof(LINE_CACHE_SUB_REC) +
sizeof(LINE_CACHE_SUB_REC) * (linecount-1));
rec->last_access = time(NULL);
rec->count = linecount;
if (rec->count > 1) {
for (pos = 0; lines != NULL; pos++) {
memcpy(&rec->lines[pos], lines->data,
sizeof(LINE_CACHE_SUB_REC));
g_free(lines->data);
lines = g_slist_remove(lines, lines->data);
}
}
g_hash_table_insert(view->cache->line_cache, line, rec);
return rec;
}
static void view_remove_cache(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line,
unsigned char update_counter)
{
LINE_CACHE_REC *cache;
if (view->cache->update_counter == update_counter)
return;
view->cache->update_counter = update_counter;
cache = g_hash_table_lookup(view->cache->line_cache, line);
if (cache != NULL) {
g_free(cache);
g_hash_table_remove(view->cache->line_cache, line);
}
}
static void view_update_cache(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line,
unsigned char update_counter)
{
view_remove_cache(view, line, update_counter);
if (view->buffer->cur_line == line)
view->cache->last_linecount = view_get_linecount(view, line);
}
static void view_reset_cache(TEXT_BUFFER_VIEW_REC *view)
{
GSList *tmp;
/* destroy line caches - note that you can't do simultaneously
unrefs + cache_get()s or it will keep using the old caches */
textbuffer_cache_unref(view->cache);
g_slist_foreach(view->siblings, (GFunc) textbuffer_cache_unref, NULL);
view->cache = textbuffer_cache_get(view->siblings, view->width);
for (tmp = view->siblings; tmp != NULL; tmp = tmp->next) {
TEXT_BUFFER_VIEW_REC *rec = tmp->data;
rec->cache = textbuffer_cache_get(rec->siblings, rec->width);
}
}
static int view_line_draw(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line,
int subline, int ypos, int max)
{
INDENT_FUNC indent_func;
LINE_CACHE_REC *cache;
const unsigned char *text, *text_newline;
unsigned char *tmp;
int xpos, color, drawcount, first, need_move, need_clrtoeol;
if (view->dirty) /* don't bother drawing anything - redraw is coming */
return 0;
cache = textbuffer_view_get_line_cache(view, line);
if (subline >= cache->count)
return 0;
color = ATTR_RESET;
need_move = TRUE; need_clrtoeol = FALSE;
xpos = drawcount = 0; first = TRUE;
text_newline = text =
subline == 0 ? line->text : cache->lines[subline-1].start;
for (;;) {
if (text == text_newline) {
if (need_clrtoeol && xpos < term_width) {
term_set_color(view->window, ATTR_RESET);
term_clrtoeol(view->window);
}
if (first)
first = FALSE;
else {
ypos++;
if (--max == 0)
break;
}
if (subline > 0) {
/* continuing previous line - indent it */
indent_func = cache->lines[subline-1].indent_func;
if (indent_func == NULL)
xpos = cache->lines[subline-1].indent;
color = cache->lines[subline-1].color;
} else {
indent_func = NULL;
}
if (xpos == 0 && indent_func == NULL)
need_clrtoeol = TRUE;
else {
/* line was indented - need to clear the
indented area first */
term_set_color(view->window, ATTR_RESET);
term_move(view->window, 0, ypos);
term_clrtoeol(view->window);
if (indent_func != NULL)
xpos = indent_func(view, line, ypos);
}
if (need_move || xpos > 0)
term_move(view->window, xpos, ypos);
term_set_color(view->window, color);
if (subline == cache->count-1) {
text_newline = NULL;
need_move = FALSE;
} else {
/* get the beginning of the next subline */
text_newline = cache->lines[subline].start;
need_move = !cache->lines[subline].continues;
}
drawcount++;
subline++;
}
if (*text == '\0') {
/* command */
text++;
if (*text == LINE_CMD_EOL || *text == LINE_CMD_FORMAT)
break;
if (*text == LINE_CMD_CONTINUE) {
/* jump to next block */
memcpy(&tmp, text+1, sizeof(unsigned char *));
text = tmp;
continue;
} else if (*text == LINE_CMD_INDENT_FUNC) {
text += sizeof(INDENT_FUNC);
} else {
update_cmd_color(*text, &color);
term_set_color(view->window, color);
}
text++;
continue;
}
if (xpos < term_width) {
const unsigned char *end = text;
if (view->utf8)
get_utf8_char(&end, 6);
if (*text >= 32 &&
(end != text || (*text & 127) >= 32)) {
for (; text < end; text++)
term_addch(view->window, *text);
term_addch(view->window, *text);
} else {
/* low-ascii */
term_set_color(view->window, ATTR_RESET|ATTR_REVERSE);
term_addch(view->window, (*text & 127)+'A'-1);
term_set_color(view->window, color);
}
}
text++;
xpos++;
}
if (need_clrtoeol && xpos < term_width) {
term_set_color(view->window, ATTR_RESET);
term_clrtoeol(view->window);
}
return drawcount;
}
/* Recalculate view's bottom line information - try to keep the
original if possible */
static void textbuffer_view_init_bottom(TEXT_BUFFER_VIEW_REC *view)
{
LINE_REC *line;
int linecount, total;
if (view->empty_linecount == 0) {
/* no empty lines in screen, no need to try to keep
the old bottom startline */
view->bottom_startline = NULL;
}
total = 0;
line = textbuffer_line_last(view->buffer);
for (; line != NULL; line = line->prev) {
linecount = view_get_linecount(view, line);
if (line == view->bottom_startline) {
/* keep the old one, make sure that subline is ok */
if (view->bottom_subline > linecount)
view->bottom_subline = linecount;
view->empty_linecount = view->height - total -
(linecount-view->bottom_subline);
return;
}
total += linecount;
if (total >= view->height) {
view->bottom_startline = line;
view->bottom_subline = total - view->height;
view->empty_linecount = 0;
return;
}
}
/* not enough lines so we must be at the beginning of the buffer */
view->bottom_startline = view->buffer->first_line;
view->bottom_subline = 0;
view->empty_linecount = view->height - total;
}
static void textbuffer_view_init_ypos(TEXT_BUFFER_VIEW_REC *view)
{
LINE_REC *line;
g_return_if_fail(view != NULL);
view->ypos = -view->subline-1;
for (line = view->startline; line != NULL; line = line->next)
view->ypos += view_get_linecount(view, line);
}
/* Create new view. */
TEXT_BUFFER_VIEW_REC *textbuffer_view_create(TEXT_BUFFER_REC *buffer,
int width, int height,
int scroll, int utf8)
{
TEXT_BUFFER_VIEW_REC *view;
g_return_val_if_fail(buffer != NULL, NULL);
g_return_val_if_fail(width > 0, NULL);
view = g_new0(TEXT_BUFFER_VIEW_REC, 1);
view->buffer = buffer;
view->siblings = textbuffer_get_views(buffer);
view->width = width;
view->height = height;
view->scroll = scroll;
view->utf8 = utf8;
view->cache = textbuffer_cache_get(view->siblings, width);
textbuffer_view_init_bottom(view);
view->startline = view->bottom_startline;
view->subline = view->bottom_subline;
view->bottom = TRUE;
textbuffer_view_init_ypos(view);
view->bookmarks = g_hash_table_new((GHashFunc) g_str_hash,
(GCompareFunc) g_str_equal);
views = g_slist_append(views, view);
return view;
}
/* Destroy the view. */
void textbuffer_view_destroy(TEXT_BUFFER_VIEW_REC *view)
{
GSList *tmp;
g_return_if_fail(view != NULL);
views = g_slist_remove(views, view);
if (view->siblings == NULL) {
/* last view for textbuffer, destroy */
textbuffer_destroy(view->buffer);
} else {
/* remove ourself from siblings lists */
for (tmp = view->siblings; tmp != NULL; tmp = tmp->next) {
TEXT_BUFFER_VIEW_REC *rec = tmp->data;
rec->siblings = g_slist_remove(rec->siblings, view);
}
g_slist_free(view->siblings);
}
g_hash_table_foreach(view->bookmarks, (GHFunc) g_free, NULL);
g_hash_table_destroy(view->bookmarks);
textbuffer_cache_unref(view->cache);
g_free(view);
}
/* Change the default indent position */
void textbuffer_view_set_default_indent(TEXT_BUFFER_VIEW_REC *view,
int default_indent,
int longword_noindent,
INDENT_FUNC indent_func)
{
if (default_indent != -1)
view->default_indent = default_indent;
if (longword_noindent != -1)
view->longword_noindent = longword_noindent;
view->default_indent_func = indent_func;
}
static void view_unregister_indent_func(TEXT_BUFFER_VIEW_REC *view,
INDENT_FUNC indent_func)
{
INDENT_FUNC func;
LINE_REC *line;
const unsigned char *text, *tmp;
if (view->default_indent_func == indent_func)
view->default_indent_func = NULL;
/* recreate cache so it won't contain references
to the indent function */
view_reset_cache(view);
view->cache = textbuffer_cache_get(view->siblings, view->width);
/* remove all references to the indent function from buffer */
line = view->buffer->first_line;
while (line != NULL) {
text = line->text;
for (text = line->text;; text++) {
if (*text != '\0')
continue;
text++;
if (*text == LINE_CMD_EOL)
break;
if (*text == LINE_CMD_INDENT_FUNC) {
text++;
memcpy(&func, text, sizeof(INDENT_FUNC));
if (func == indent_func)
memset(&func, 0, sizeof(INDENT_FUNC));
text += sizeof(INDENT_FUNC);
} else if (*text == LINE_CMD_CONTINUE) {
memcpy(&tmp, text+1, sizeof(char *));
text = tmp-1;
}
}
line = line->next;
}
}
void textbuffer_views_unregister_indent_func(INDENT_FUNC indent_func)
{
g_slist_foreach(views, (GFunc) view_unregister_indent_func,
(void *) indent_func);
}
void textbuffer_view_set_scroll(TEXT_BUFFER_VIEW_REC *view, int scroll)
{
view->scroll = scroll;
}
void textbuffer_view_set_utf8(TEXT_BUFFER_VIEW_REC *view, int utf8)
{
view->utf8 = utf8;
}
static int view_get_linecount_all(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line)
{
int linecount;
linecount = 0;
while (line != NULL) {
linecount += view_get_linecount(view, line);
line = line->next;
}
return linecount;
}
static void view_draw(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line,
int subline, int ypos, int lines, int fill_bottom)
{
int linecount;
if (view->dirty) /* don't bother drawing anything - redraw is coming */
return;
while (line != NULL && lines > 0) {
linecount = view_line_draw(view, line, subline, ypos, lines);
ypos += linecount; lines -= linecount;
subline = 0;
line = line->next;
}
if (fill_bottom) {
/* clear the rest of the view */
term_set_color(view->window, ATTR_RESET);
while (lines > 0) {
term_move(view->window, 0, ypos);
term_clrtoeol(view->window);
ypos++; lines--;
}
}
}
#define view_draw_top(view, lines, fill_bottom) \
view_draw(view, (view)->startline, (view)->subline, \
0, lines, fill_bottom)
static void view_draw_bottom(TEXT_BUFFER_VIEW_REC *view, int lines)
{
LINE_REC *line;
int ypos, maxline, subline, linecount;
maxline = view->height-lines;
line = view->startline; ypos = -view->subline; subline = 0;
while (line != NULL && ypos < maxline) {
linecount = view_get_linecount(view, line);
ypos += linecount;
if (ypos > maxline) {
subline = maxline-(ypos-linecount);
break;
}
line = line->next;
}
view_draw(view, line, subline, maxline, lines, TRUE);
}
/* Returns number of lines actually scrolled */
static int view_scroll(TEXT_BUFFER_VIEW_REC *view, LINE_REC **lines,
int *subline, int scrollcount, int draw_nonclean)
{
int linecount, realcount, scroll_visible;
if (*lines == NULL)
return 0;
/* scroll down */
scroll_visible = lines == &view->startline;
realcount = -*subline;
scrollcount += *subline;
*subline = 0;
while (scrollcount > 0) {
linecount = view_get_linecount(view, *lines);
if ((scroll_visible && *lines == view->bottom_startline) &&
(scrollcount >= view->bottom_subline)) {
*subline = view->bottom_subline;
realcount += view->bottom_subline;
scrollcount = 0;
break;
}
realcount += linecount;
scrollcount -= linecount;
if (scrollcount < 0) {
realcount += scrollcount;
*subline = linecount+scrollcount;
scrollcount = 0;
break;
}
if ((*lines)->next == NULL)
break;
*lines = (*lines)->next;
}
/* scroll up */
while (scrollcount < 0 && (*lines)->prev != NULL) {
*lines = (*lines)->prev;
linecount = view_get_linecount(view, *lines);
realcount -= linecount;
scrollcount += linecount;
if (scrollcount > 0) {
realcount += scrollcount;
*subline = scrollcount;
break;
}
}
if (scroll_visible && realcount != 0 && view->window != NULL) {
if (realcount <= -view->height || realcount >= view->height) {
/* scrolled more than screenful, redraw the
whole view */
textbuffer_view_redraw(view);
} else {
term_set_color(view->window, ATTR_RESET);
term_window_scroll(view->window, realcount);
if (draw_nonclean) {
if (realcount < 0)
view_draw_top(view, -realcount, TRUE);
else
view_draw_bottom(view, realcount);
}
term_refresh(view->window);
}
}
return realcount >= 0 ? realcount : -realcount;
}
/* Resize the view. */
void textbuffer_view_resize(TEXT_BUFFER_VIEW_REC *view, int width, int height)
{
int linecount;
g_return_if_fail(view != NULL);
g_return_if_fail(width > 0);
if (view->width != width) {
/* line cache needs to be recreated */
textbuffer_cache_unref(view->cache);
view->cache = textbuffer_cache_get(view->siblings, width);
}
view->width = width > 10 ? width : 10;
view->height = height > 1 ? height : 1;
if (view->buffer->first_line == NULL) {
view->empty_linecount = height;
return;
}
textbuffer_view_init_bottom(view);
/* check that we didn't scroll lower than bottom startline.. */
if (textbuffer_line_exists_after(view->bottom_startline->next,
view->startline)) {
view->startline = view->bottom_startline;
view->subline = view->bottom_subline;
} else if (view->startline == view->bottom_startline &&
view->subline > view->bottom_subline) {
view->subline = view->bottom_subline;
} else {
/* make sure the subline is still in allowed range */
linecount = view_get_linecount(view, view->startline);
if (view->subline > linecount)
view->subline = linecount;
}
textbuffer_view_init_ypos(view);
if (view->bottom && !view_is_bottom(view)) {
/* we scrolled to far up, need to get down. go right over
the empty lines if there's any */
view->startline = view->bottom_startline;
view->subline = view->bottom_subline;
if (view->empty_linecount > 0) {
view_scroll(view, &view->startline, &view->subline,
-view->empty_linecount, FALSE);
}
textbuffer_view_init_ypos(view);
}
view->bottom = view_is_bottom(view);
if (view->bottom) {
/* check if we left empty space at the bottom.. */
linecount = view_get_linecount_all(view, view->startline) -
view->subline;
if (view->empty_linecount < view->height-linecount)
view->empty_linecount = view->height-linecount;
view->more_text = FALSE;
}
view->dirty = TRUE;
}
/* Clear the view, don't actually remove any lines from buffer. */
void textbuffer_view_clear(TEXT_BUFFER_VIEW_REC *view)
{
g_return_if_fail(view != NULL);
view->ypos = -1;
view->bottom_startline = view->startline =
textbuffer_line_last(view->buffer);
view->bottom_subline = view->subline =
view->buffer->cur_line == NULL ? 0 :
view_get_linecount(view, view->buffer->cur_line);
view->empty_linecount = view->height;
view->bottom = TRUE;
view->more_text = FALSE;
textbuffer_view_redraw(view);
}
/* Scroll the view up/down */
void textbuffer_view_scroll(TEXT_BUFFER_VIEW_REC *view, int lines)
{
int count;
g_return_if_fail(view != NULL);
count = view_scroll(view, &view->startline, &view->subline,
lines, TRUE);
view->ypos += lines < 0 ? count : -count;
view->bottom = view_is_bottom(view);
if (view->bottom) view->more_text = FALSE;
if (view->window != NULL)
term_refresh(view->window);
}
/* Scroll to specified line */
void textbuffer_view_scroll_line(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line)
{
g_return_if_fail(view != NULL);
if (textbuffer_line_exists_after(view->bottom_startline->next, line)) {
view->startline = view->bottom_startline;
view->subline = view->bottom_subline;
} else {
view->startline = line;
view->subline = 0;
}
textbuffer_view_init_ypos(view);
view->bottom = view_is_bottom(view);
if (view->bottom) view->more_text = FALSE;
textbuffer_view_redraw(view);
}
/* Return line cache */
LINE_CACHE_REC *textbuffer_view_get_line_cache(TEXT_BUFFER_VIEW_REC *view,
LINE_REC *line)
{
LINE_CACHE_REC *cache;
g_assert(view != NULL);
g_assert(line != NULL);
cache = g_hash_table_lookup(view->cache->line_cache, line);
if (cache == NULL)
cache = view_update_line_cache(view, line);
else
cache->last_access = time(NULL);
return cache;
}
static void view_insert_line(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line)
{
int linecount, ypos, subline;
if (!view->bottom)
view->more_text = TRUE;
if (view->bottom_startline == NULL) {
view->startline = view->bottom_startline =
view->buffer->first_line;
}
if (view->buffer->cur_line != line &&
!textbuffer_line_exists_after(view->bottom_startline, line))
return;
linecount = view->cache->last_linecount;
view->ypos += linecount;
if (view->empty_linecount > 0) {
view->empty_linecount -= linecount;
if (view->empty_linecount >= 0)
linecount = 0;
else {
linecount = -view->empty_linecount;
view->empty_linecount = 0;
}
}
if (linecount > 0) {
view_scroll(view, &view->bottom_startline,
&view->bottom_subline, linecount, FALSE);
}
if (view->bottom) {
if (view->scroll && view->ypos >= view->height) {
linecount = view->ypos-view->height+1;
view_scroll(view, &view->startline,
&view->subline, linecount, FALSE);
view->ypos -= linecount;
} else {
view->bottom = view_is_bottom(view);
}
if (view->window != NULL) {
ypos = view->ypos+1 - view->cache->last_linecount;
if (ypos >= 0)
subline = 0;
else {
subline = -ypos;
ypos = 0;
}
if (ypos < view->height) {
view_line_draw(view, line, subline, ypos,
view->height - ypos);
}
}
}
if (view->window != NULL)
term_refresh(view->window);
}
/* Update some line in the buffer which has been modified using
textbuffer_append() or textbuffer_insert(). */
void textbuffer_view_insert_line(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line)
{
GSList *tmp;
unsigned char update_counter;
g_return_if_fail(view != NULL);
g_return_if_fail(line != NULL);
if (!view->buffer->last_eol)
return;
update_counter = view->cache->update_counter+1;
view_update_cache(view, line, update_counter);
view_insert_line(view, line);
for (tmp = view->siblings; tmp != NULL; tmp = tmp->next) {
TEXT_BUFFER_VIEW_REC *rec = tmp->data;
view_update_cache(rec, line, update_counter);
view_insert_line(rec, line);
}
}
typedef struct {
LINE_REC *remove_line;
GSList *remove_list;
} BOOKMARK_FIND_REC;
static void bookmark_check_remove(char *key, LINE_REC *line,
BOOKMARK_FIND_REC *rec)
{
if (line == rec->remove_line)
rec->remove_list = g_slist_append(rec->remove_list, key);
}
static void view_bookmarks_check(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line)
{
BOOKMARK_FIND_REC rec;
LINE_REC *new_line;
GSList *tmp;
rec.remove_line = line;
rec.remove_list = NULL;
g_hash_table_foreach(view->bookmarks,
(GHFunc) bookmark_check_remove, &rec);
if (rec.remove_list != NULL) {
new_line = line->prev == NULL ? NULL :
(line->next == NULL ? line->prev : line->next);
for (tmp = rec.remove_list; tmp != NULL; tmp = tmp->next) {
g_hash_table_remove(view->bookmarks, tmp->data);
if (new_line != NULL) {
g_hash_table_insert(view->bookmarks,
tmp->data, new_line);
}
}
g_slist_free(rec.remove_list);
}
}
/* Return number of real lines `lines' list takes -
stops counting when the height reaches the view height */
static int view_get_lines_height(TEXT_BUFFER_VIEW_REC *view,
LINE_REC *line, int subline,
LINE_REC *skip_line)
{
int height, linecount;
height = -subline;
while (line != NULL && height < view->height) {
if (line != skip_line) {
linecount = view_get_linecount(view, line);
height += linecount;
}
line = line->next;
}
return height < view->height ? height : view->height;
}
static void view_remove_line_update_startline(TEXT_BUFFER_VIEW_REC *view,
LINE_REC *line, int linecount)
{
int scroll;
if (view->startline == line) {
view->startline = view->startline->prev != NULL ?
view->startline->prev : view->startline->next;
view->subline = 0;
} else {
scroll = view->height -
view_get_lines_height(view, view->startline,
view->subline, line);
if (scroll > 0) {
view_scroll(view, &view->startline,
&view->subline, -scroll, FALSE);
}
}
/* FIXME: this is slow and unnecessary, but it's easy and
really works :) */
textbuffer_view_init_ypos(view);
if (textbuffer_line_exists_after(view->startline, line))
view->ypos -= linecount;
}
static void view_remove_line(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line,
int linecount)
{
int realcount;
view_bookmarks_check(view, line);
if (view->buffer->cur_line == line) {
/* the last line is being removed */
LINE_REC *prevline;
prevline = view->buffer->first_line == line ? NULL :
textbuffer_line_last(view->buffer);
view->cache->last_linecount = prevline == NULL ? 0 :
view_get_linecount(view, prevline);
}
if (view->buffer->first_line == line) {
/* first line in the buffer - this is the most commonly
removed line.. */
if (view->bottom_startline == line) {
/* very small scrollback.. */
view->bottom_startline = view->bottom_startline->next;
view->bottom_subline = 0;
}
if (view->startline == line) {
/* removing the first line in screen */
realcount = view_scroll(view, &view->startline,
&view->subline,
linecount, FALSE);
view->ypos -= realcount;
view->empty_linecount += linecount-realcount;
}
} else {
if (textbuffer_line_exists_after(view->bottom_startline,
line)) {
realcount = view_scroll(view, &view->bottom_startline,
&view->bottom_subline,
-linecount, FALSE);
view->empty_linecount += linecount-realcount;
}
if (textbuffer_line_exists_after(view->startline,
line)) {
view_remove_line_update_startline(view, line,
linecount);
}
}
view->bottom = view_is_bottom(view);
if (view->bottom) view->more_text = FALSE;
if (view->window != NULL)
term_refresh(view->window);
}
/* Remove one line from buffer. */
void textbuffer_view_remove_line(TEXT_BUFFER_VIEW_REC *view, LINE_REC *line)
{
GSList *tmp;
unsigned char update_counter;
int linecount;
g_return_if_fail(view != NULL);
g_return_if_fail(line != NULL);
linecount = view_get_linecount(view, line);
update_counter = view->cache->update_counter+1;
view_remove_line(view, line, linecount);
view_remove_cache(view, line, update_counter);
for (tmp = view->siblings; tmp != NULL; tmp = tmp->next) {
TEXT_BUFFER_VIEW_REC *rec = tmp->data;
view_remove_line(rec, line, linecount);
view_remove_cache(rec, line, update_counter);
}
textbuffer_remove(view->buffer, line);
}
static int g_free_true(void *data)
{
g_free(data);
return TRUE;
}
/* Remove all lines from buffer. */
void textbuffer_view_remove_all_lines(TEXT_BUFFER_VIEW_REC *view)
{
g_return_if_fail(view != NULL);
textbuffer_remove_all_lines(view->buffer);
g_hash_table_foreach_remove(view->bookmarks,
(GHRFunc) g_free_true, NULL);
view_reset_cache(view);
textbuffer_view_clear(view);
g_slist_foreach(view->siblings, (GFunc) textbuffer_view_clear, NULL);
}
/* Set a bookmark in view */
void textbuffer_view_set_bookmark(TEXT_BUFFER_VIEW_REC *view,
const char *name, LINE_REC *line)
{
gpointer key, value;
g_return_if_fail(view != NULL);
g_return_if_fail(name != NULL);
if (g_hash_table_lookup_extended(view->bookmarks, name,
&key, &value)) {
g_hash_table_remove(view->bookmarks, key);
g_free(key);
}
g_hash_table_insert(view->bookmarks, g_strdup(name), line);
}
/* Set a bookmark in view to the bottom line */
void textbuffer_view_set_bookmark_bottom(TEXT_BUFFER_VIEW_REC *view,
const char *name)
{
LINE_REC *line;
g_return_if_fail(view != NULL);
g_return_if_fail(name != NULL);
if (view->bottom_startline != NULL) {
line = textbuffer_line_last(view->buffer);
textbuffer_view_set_bookmark(view, name, line);
}
}
/* Return the line for bookmark */
LINE_REC *textbuffer_view_get_bookmark(TEXT_BUFFER_VIEW_REC *view,
const char *name)
{
g_return_val_if_fail(view != NULL, NULL);
g_return_val_if_fail(name != NULL, NULL);
return g_hash_table_lookup(view->bookmarks, name);
}
/* Specify window where the changes in view should be drawn,
NULL disables it. */
void textbuffer_view_set_window(TEXT_BUFFER_VIEW_REC *view,
TERM_WINDOW *window)
{
g_return_if_fail(view != NULL);
if (view->window != window) {
view->window = window;
if (window != NULL)
view->dirty = TRUE;
}
}
/* Redraw a view to window */
void textbuffer_view_redraw(TEXT_BUFFER_VIEW_REC *view)
{
g_return_if_fail(view != NULL);
if (view->window != NULL) {
view->dirty = FALSE;
view_draw_top(view, view->height, TRUE);
term_refresh(view->window);
}
}
static int line_cache_check_remove(void *key, LINE_CACHE_REC *cache,
time_t *now)
{
if (cache->last_access+LINE_CACHE_KEEP_TIME > *now)
return FALSE;
line_cache_destroy(NULL, cache);
return TRUE;
}
static int sig_check_linecache(void)
{
GSList *tmp, *caches;
time_t now;
now = time(NULL); caches = NULL;
for (tmp = views; tmp != NULL; tmp = tmp->next) {
TEXT_BUFFER_VIEW_REC *rec = tmp->data;
if (g_slist_find(caches, rec->cache) != NULL)
continue;
caches = g_slist_append(caches, rec->cache);
g_hash_table_foreach_remove(rec->cache->line_cache,
(GHRFunc) line_cache_check_remove,
&now);
}
g_slist_free(caches);
return 1;
}
void textbuffer_view_init(void)
{
linecache_tag = g_timeout_add(LINE_CACHE_CHECK_TIME, (GSourceFunc) sig_check_linecache, NULL);
}
void textbuffer_view_deinit(void)
{
g_source_remove(linecache_tag);
}
|