-
Notifications
You must be signed in to change notification settings - Fork 2
/
gtk_filebuf.c
2176 lines (1785 loc) · 75.8 KB
/
gtk_filebuf.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
#include "gtk_filebuf.h"
/** file to set file stat information for filename */
void file_get_stats (kinst_t *inst)
{
const gchar *filename = inst->filename;
struct stat sb;
inst->filemode = 0;
inst->fileuid = 0;
inst->filegid = 0;
/* validate inst and filename */
if (!inst || !filename)
return;
/* validate file exists */
if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
char *str;
str = g_strdup_printf ("error: file not found, file_get_stats() '%s'",
filename);
g_warning (str);
g_free (str);
return;
}
if (g_stat (filename, &sb)) { /* get file stats */
g_print ("error: stat of file failed '%s'\n", filename);
return;
}
inst->filemode = sb.st_mode;
inst->fileuid = sb.st_uid;
inst->filegid = sb.st_gid;
}
gboolean buffer_select_all (gpointer data)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextIter start, end;
if (!buffer)
return FALSE;
/* get iter at current insert position */
gtk_text_buffer_get_iter_at_mark (buffer, &start,
gtk_text_buffer_get_insert (buffer));
if (app->last_pos == NULL) /* set last_pos to restore on cancel */
app->last_pos = gtk_text_buffer_create_mark (buffer, "last_pos",
&start, FALSE);
else
gtk_text_buffer_move_mark (buffer, app->last_pos, &start);
/* get start and end iters, verify not the same */
gtk_text_buffer_get_bounds (buffer, &start, &end);
if (gtk_text_iter_equal (&start, &end))
return FALSE;
/* select range */
gtk_text_buffer_select_range (buffer, &start, &end);
return TRUE;
}
gboolean buffer_deselect_all (gpointer data)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextIter start, end;
if (!buffer)
return FALSE;
/* validate existing selection */
if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end))
return FALSE;
/* get iter at saved insert position, or start if not saved */
if (app->last_pos) {
gtk_text_buffer_get_iter_at_mark (buffer, &start, app->last_pos);
gtk_text_buffer_delete_mark (buffer, app->last_pos);
app->last_pos = NULL;
}
else
gtk_text_buffer_get_start_iter (buffer, &start);
/* place cursor at iter */
gtk_text_buffer_place_cursor (buffer, &start);
return TRUE;
}
/** buffer_select_to_next_char selects from cursor to next non-ws char.
* this corrects the gtk_text_view default selection of whitespace
* plus the next word on the ctrl+shift+right-arrow combination.
*
* the keypress handler must keep track of the number of left or right
* ctrl+shift select keypress sequences so using the arrow-key in the
* opposite direction will undo in the correct direction. the current
* bstack will only keep track of 128 simultaneous ctrl+shift keypress
* events in one direction at a time (or 256 if undoing 128 in the
* opposite direction. increase STKMAX if you need more.
*/
gboolean buffer_select_to_next_char (gpointer data)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextIter start, end;
gunichar c;
/* check existing selection, set start = end, otherwise
* get iter at cursor position ("Insert" mark), set end = start
*/
if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end)) {
gtk_text_buffer_get_iter_at_mark (buffer, &start,
gtk_text_buffer_get_insert (buffer));
end = start;
bstack_clear (app);
}
/* check if prev ctrl+shift keypress & if it was RIGHT */
if (!app->bindex || bstack_last (app) == RIGHT) {
/* get char and check if whitespace or non-whitespace */
c = gtk_text_iter_get_char (&end);
if (c == ' ' || c == '\t') {
/* read contiguous whitespace to next word */
while (c == ' ' || c == '\t') {
if (!gtk_text_iter_forward_char (&end))
break;
c = gtk_text_iter_get_char (&end);
}
}
else {
/* read contiguous non-whitespace */
while (c != ' ' && c != '\t') {
if (!gtk_text_iter_forward_char (&end))
break;
c = gtk_text_iter_get_char (&end);
}
/* read contiguous whitespace to next word */
while (c == ' ' || c == '\t') {
if (!gtk_text_iter_forward_char (&end))
break;
c = gtk_text_iter_get_char (&end);
}
}
}
else {
c = gtk_text_iter_get_char (&start);
if (c == ' ' || c == '\t') {
/* read contiguous whitespace to next word */
while (c == ' ' || c == '\t') {
if (!gtk_text_iter_forward_char (&start) ||
gtk_text_iter_equal (&start, &end))
goto nextrdlt;
c = gtk_text_iter_get_char (&start);
}
/* read contiguous non-whitespace */
while (c != ' ' && c != '\t') {
if (!gtk_text_iter_forward_char (&start) ||
gtk_text_iter_equal (&start, &end))
break;
c = gtk_text_iter_get_char (&start);
}
nextrdlt:;
}
else {
/* read contiguous non-whitespace */
while (c != ' ' && c != '\t') {
if (!gtk_text_iter_forward_char (&start) ||
gtk_text_iter_equal (&start, &end))
break;
c = gtk_text_iter_get_char (&start);
}
}
if (gtk_text_iter_equal (&start, &end)) {
gtk_text_buffer_place_cursor (buffer, &start);
bstack_clear (app);
return TRUE;
}
}
/* select range */
gtk_text_buffer_select_range (buffer, &start, &end);
return TRUE;
}
/** buffer_select_to_next_char selects from cursor to next non-ws char.
* this corrects the gtk_text_view default selection of whitespace
* plus the next word on the ctrl+shift+right-arrow combination.
*
* (see extended note on function above for bstack tracking of
* ctrl+shift keypress events.
*/
gboolean buffer_select_to_prev_char (gpointer data)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextIter start, end;
gunichar c;
/* check existing selection, set start = end, otherwise
* get iter at cursor position ("Insert" mark), set end = start
*/
if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end)) {
gtk_text_buffer_get_iter_at_mark (buffer, &start,
gtk_text_buffer_get_insert (buffer));
end = start;
bstack_clear (app);
}
/* check if prev ctrl+shift keypress & if it was LEFT */
if (!app->bindex || bstack_last (app) == LEFT) {
gtk_text_iter_backward_char (&start);
/* get char and check if whitespace or non-whitespace */
c = gtk_text_iter_get_char (&start);
if (c == ' ' || c == '\t') {
/* read contiguous whitespace to next word */
while (c == ' ' || c == '\t') {
if (!gtk_text_iter_backward_char (&start))
break;
c = gtk_text_iter_get_char (&start);
}
}
else {
/* read contiguous non-whitespace */
while (c != ' ' && c != '\t') {
if (!gtk_text_iter_backward_char (&start))
goto prevrdlt;
c = gtk_text_iter_get_char (&start);
}
/* read contiguous whitespace to next word */
while (c == ' ' || c == '\t') {
if (!gtk_text_iter_backward_char (&start))
break;
c = gtk_text_iter_get_char (&start);
}
prevrdlt:;
}
if (gtk_text_iter_equal (&start, &end)) {
gtk_text_buffer_place_cursor (buffer, &start);
bstack_clear (app);
return TRUE;
}
else
gtk_text_iter_forward_char (&start);
}
else {
gtk_text_iter_backward_char (&end);
/* get char and check if whitespace or non-whitespace */
c = gtk_text_iter_get_char (&end);
if (c == ' ' || c == '\t') {
/* read contiguous whitespace to next word */
while (c == ' ' || c == '\t') {
if (!gtk_text_iter_backward_char (&end) ||
gtk_text_iter_equal (&start, &end))
goto prevrdrt;
c = gtk_text_iter_get_char (&end);
}
/* read contiguous non-whitespace */
while (c != ' ' && c != '\t') {
if (!gtk_text_iter_backward_char (&end) ||
gtk_text_iter_equal (&start, &end))
break;
c = gtk_text_iter_get_char (&end);
}
prevrdrt:;
}
else {
/* read contiguous whitespace to next word */
while (c != ' ' || c != '\t') {
if (!gtk_text_iter_backward_char (&end) ||
gtk_text_iter_equal (&start, &end))
break;
c = gtk_text_iter_get_char (&end);
}
}
if (gtk_text_iter_equal (&start, &end)) {
gtk_text_buffer_place_cursor (buffer, &start);
bstack_clear (app);
return TRUE;
}
else
gtk_text_iter_forward_char (&end);
}
/* select range */
gtk_text_buffer_select_range (buffer, &start, &end);
return TRUE;
}
/** buffer reduce selection bounds by 1 at end */
gboolean buffer_reduce_selection (gpointer data)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextIter start, end;
gboolean selection = gtk_text_buffer_get_selection_bounds (buffer,
&start, &end);
/** check existing selection, bstack active and first movement RIGHT */
if (!selection || !app->bindex || !app->bstack[0]) {
bstack_clear (app);
return FALSE;
}
g_print ("selection: %s app->bindex: %u app->bstack[0]: %u\n",
selection ? "TRUE" : "FALSE", app->bindex, app->bstack[0]);
gtk_text_iter_backward_char (&end);
/* select range */
gtk_text_buffer_select_range (buffer, &start, &end);
return TRUE;
}
/** indent current/selected lines to the next softtab stop.
* text will be aligned to the next softtab on indent
* regardless of the number of spaces before the next
* softtab.
*/
void buffer_indent_lines (gpointer data, GtkTextIter *start, GtkTextIter *end)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextView *view = GTK_TEXT_VIEW(einst->view);
GtkTextBuffer *buf = GTK_TEXT_BUFFER(inst->buf);
GtkTextMark *start_mark, *end_mark;
gint i, start_line, end_line, nspaces;
gchar *ind_buffer = NULL;
gboolean bracket_hl;
bracket_hl = gtk_source_buffer_get_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf));
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf), FALSE);
start_mark = gtk_text_buffer_create_mark (buf, NULL, start, FALSE);
end_mark = gtk_text_buffer_create_mark (buf, NULL, end, FALSE);
start_line = gtk_text_iter_get_line (start);
end_line = gtk_text_iter_get_line (end);
if ((gtk_text_iter_get_visible_line_offset (end) == 0) &&
(end_line > start_line)) {
end_line--;
}
gtk_text_buffer_begin_user_action (buf);
for (i = start_line; i <= end_line; i++) {
GtkTextIter iter;
gint offset = 0;
gtk_text_buffer_get_iter_at_line (buf, &iter, i);
/* iterate past tabs computing character equivalent */
for (;;) {
gunichar c;
c = gtk_text_iter_get_char (&iter);
if (c == '\t' || c == ' ')
offset += (c == '\t') ? app->softtab : 1;
else
break;
gtk_text_iter_forward_char (&iter);
}
nspaces = app->softtab - offset % app->softtab;
ind_buffer = g_strdup_printf ("%*s", nspaces, " ");
gtk_text_buffer_insert (buf, &iter, ind_buffer, -1);
g_free (ind_buffer);
/* reset start iter and start mark to beginning of line
* so that selection continues to encompass entire first line.
*/
if (start_line != end_line && i == start_line &&
!gtk_text_iter_starts_line (&iter)) {
gtk_text_iter_set_line_offset (&iter, 0);
gtk_text_buffer_move_mark (buf, start_mark, &iter);
}
}
gtk_text_buffer_end_user_action (buf);
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf),
bracket_hl);
gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (view),
gtk_text_buffer_get_insert (buf));
/* revalidate iters */
gtk_text_buffer_get_iter_at_mark (buf, start, start_mark);
gtk_text_buffer_get_iter_at_mark (buf, end, end_mark);
/* adjust selection to cover entire first line */
gtk_text_buffer_select_range (buf, end, start);
/* delete GtkTextMark's */
gtk_text_buffer_delete_mark (buf, start_mark);
gtk_text_buffer_delete_mark (buf, end_mark);
}
/** unindent current/selected lines to the preceding softtab.
* text will be aligned to the prior softtab on unindent
* regardless of the number of spaces beyond the previous
* softtab text actually begins.
*/
void buffer_unindent_lines (gpointer data, GtkTextIter *start, GtkTextIter *end)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextView *view = GTK_TEXT_VIEW(einst->view);
GtkTextBuffer *buf = GTK_TEXT_BUFFER(inst->buf);
GtkTextMark *start_mark, *end_mark;
gint i, indent_width, start_line, end_line;
gboolean bracket_hl;
bracket_hl = gtk_source_buffer_get_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf));
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf), FALSE);
start_mark = gtk_text_buffer_create_mark (buf, NULL, start, FALSE);
end_mark = gtk_text_buffer_create_mark (buf, NULL, end, FALSE);
start_line = gtk_text_iter_get_line (start);
end_line = gtk_text_iter_get_line (end);
if ((gtk_text_iter_get_visible_line_offset (end) == 0) &&
(end_line > start_line)) {
end_line--;
}
indent_width = app->softtab;
gtk_text_buffer_begin_user_action (buf);
for (i = start_line; i <= end_line; i++) {
GtkTextIter iter_start, iter;
gint offset = 0, ndelete = 0;
gboolean hastabs = FALSE; /* special handling for tab chars */
gtk_text_buffer_get_iter_at_line (buf, &iter_start, i);
iter = iter_start;
while (!gtk_text_iter_ends_line (&iter))
{
gunichar c;
c = gtk_text_iter_get_char (&iter);
if (c == '\t') {
offset += indent_width;
hastabs = TRUE;
}
else if (c == ' ')
offset += 1;
else
break;
gtk_text_iter_forward_char (&iter);
}
ndelete = offset % indent_width;
/* tabs - del leading ws & insert correct spaces for unindent */
if (hastabs) {
char *ws = g_strdup_printf ("%*s", offset - ndelete, " ");
if (!ws) { /* handle error */
g_print ("buffer_unindent_lines() error: g_strdup_printf.\n");
}
gtk_text_buffer_delete (buf, &iter_start, &iter); /* del whitespace/tabs */
gtk_text_buffer_get_iter_at_line (buf, &iter, i); /* revalidate at start */
gtk_text_buffer_insert (buf, &iter, ws, -1); /* ins correct indent */
if (ws)
g_free (ws);
}
else { /* no tabs - all spaces */
if (offset <= indent_width) { /* within first softtab */
gtk_text_buffer_delete (buf, &iter_start, &iter);
}
else if (!ndelete) { /* backup full softtab width */
gtk_text_iter_set_line_offset (&iter_start, offset - indent_width);
gtk_text_buffer_delete (buf, &iter_start, &iter);
}
else { /* bring indention to nex prior softtab */
gtk_text_iter_set_line_offset (&iter_start, offset - ndelete);
gtk_text_buffer_delete (buf, &iter_start, &iter);
}
}
}
gtk_text_buffer_end_user_action (buf);
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf),
bracket_hl);
gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (view),
gtk_text_buffer_get_insert (buf));
/* revalidate iters */
gtk_text_buffer_get_iter_at_mark (buf, start, start_mark);
gtk_text_buffer_get_iter_at_mark (buf, end, end_mark);
gtk_text_buffer_delete_mark (buf, start_mark);
gtk_text_buffer_delete_mark (buf, end_mark);
}
/** indent current/selected lines by softtab spaces.
* text will be indented by softtab spaces on indent
* preserving the existing number of space in line.
*/
void buffer_indent_lines_fixed (gpointer data, GtkTextIter *start, GtkTextIter *end)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextView *view = GTK_TEXT_VIEW(einst->view);
GtkTextBuffer *buf = GTK_TEXT_BUFFER(inst->buf);
GtkTextMark *start_mark, *end_mark;
gint i, start_line, end_line;
gchar *ind_buffer = NULL;
gboolean bracket_hl;
bracket_hl = gtk_source_buffer_get_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf));
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf), FALSE);
start_mark = gtk_text_buffer_create_mark (buf, NULL, start, FALSE);
end_mark = gtk_text_buffer_create_mark (buf, NULL, end, FALSE);
start_line = gtk_text_iter_get_line (start);
end_line = gtk_text_iter_get_line (end);
if ((gtk_text_iter_get_visible_line_offset (end) == 0) &&
(end_line > start_line)) {
end_line--;
}
gtk_text_buffer_begin_user_action (buf);
for (i = start_line; i <= end_line; i++) {
GtkTextIter iter;
gtk_text_buffer_get_iter_at_line (buf, &iter, i);
ind_buffer = g_strdup_printf ("%*s", app->softtab, " ");
gtk_text_buffer_insert (buf, &iter, ind_buffer, -1);
g_free (ind_buffer);
/* reset start iter and start mark to beginning of line
* so that selection continues to encompass entire first line.
*/
if (start_line != end_line && i == start_line &&
!gtk_text_iter_starts_line (&iter)) {
gtk_text_iter_set_line_offset (&iter, 0);
gtk_text_buffer_move_mark (buf, start_mark, &iter);
}
}
gtk_text_buffer_end_user_action (buf);
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf),
bracket_hl);
gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (view),
gtk_text_buffer_get_insert (buf));
/* revalidate iters */
gtk_text_buffer_get_iter_at_mark (buf, start, start_mark);
gtk_text_buffer_get_iter_at_mark (buf, end, end_mark);
/* adjust selection to cover entire first line */
gtk_text_buffer_select_range (buf, end, start);
gtk_text_buffer_delete_mark (buf, start_mark);
gtk_text_buffer_delete_mark (buf, end_mark);
}
/** Unindent current/selected lines by softtab spaces.
* text will be unindented by softtab spaces on unindent
* preserving the existing number of space in line.
*/
void buffer_unindent_lines_fixed (gpointer data, GtkTextIter *start, GtkTextIter *end)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextView *view = GTK_TEXT_VIEW(einst->view);
GtkTextBuffer *buf = GTK_TEXT_BUFFER(inst->buf);
GtkTextMark *start_mark, *end_mark;
gint i, start_line, end_line, nspaces;
gboolean bracket_hl;
bracket_hl = gtk_source_buffer_get_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf));
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf), FALSE);
start_mark = gtk_text_buffer_create_mark (buf, NULL, start, FALSE);
end_mark = gtk_text_buffer_create_mark (buf, NULL, end, FALSE);
start_line = gtk_text_iter_get_line (start);
end_line = gtk_text_iter_get_line (end);
if ((gtk_text_iter_get_visible_line_offset (end) == 0) &&
(end_line > start_line)) {
end_line--;
}
gtk_text_buffer_begin_user_action (buf);
for (i = start_line; i <= end_line; i++) {
GtkTextIter iter_start, iter;
gint offset = 0;
gtk_text_buffer_get_iter_at_line (buf, &iter, i);
iter_start = iter;
/* iterate past tabs computing character equivalent */
for (;;) {
gunichar c;
c = gtk_text_iter_get_char (&iter);
if (c == '\t' || c == ' ')
offset += (c == '\t') ? app->softtab : 1;
else
break;
gtk_text_iter_forward_char (&iter);
}
nspaces = offset >= app->softtab ? app->softtab : offset;
/* set iter at nspaces and delete between iter_start - iter */
gtk_text_iter_set_line_offset (&iter, nspaces);
gtk_text_buffer_delete (buf, &iter_start, &iter);
}
gtk_text_buffer_end_user_action (buf);
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buf),
bracket_hl);
gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW (view),
gtk_text_buffer_get_insert (buf));
/* revalidate iters */
gtk_text_buffer_get_iter_at_mark (buf, start, start_mark);
gtk_text_buffer_get_iter_at_mark (buf, end, end_mark);
gtk_text_buffer_delete_mark (buf, start_mark);
gtk_text_buffer_delete_mark (buf, end_mark);
}
/** comment line based on comment syntax determined from SourceView language.
* start and end iterators indicating the start and end of the block (or single
* line) to be commented. Block comments are used if text is selected,
* otherwise single-line comments are used to comment entire line.
* TODO:
* provide setting to prefer single/multi-line comemnt, and update settings
* dialog to display current document comment syntax strings.
*/
void buffer_comment_lines (gpointer data, GtkTextIter *start, GtkTextIter *end)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextMark *start_mark, *end_mark;
gint i, start_line, end_line;
const gchar *commentstr = app->comment; /* set comment from settings */
gboolean bracket_hl;
gint start_pos, end_pos;
/* FIXME - update inst->comment_single so it reflects sourceview language
* and comemnt selection. Not being updated currently on open.
*/
if (inst->comment_single) { /* if comment based on lang_id */
commentstr = inst->comment_single; /* update commentstr from lang_id */
// if (!commentstr) { /* no effect until comment str updated on file open */
// const char *msg = "Error: single-comment not defined for language,\n"
// "but, use single-line comment selected in settings\n"
// "Editing -> Comment Settings.\n";
// err_dialog_win (app, msg);
// return;
// }
}
bracket_hl = gtk_source_buffer_get_highlight_matching_brackets (GTK_SOURCE_BUFFER (buffer));
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buffer), FALSE);
/* preserve marks for start/end to revalidate iterators before return
* using left gravity on start to mark stays left of inserted text.
*/
start_mark = gtk_text_buffer_create_mark (buffer, NULL, start, TRUE);
end_mark = gtk_text_buffer_create_mark (buffer, NULL, end, FALSE);
/* get lines indicated by start/end iters passed based on selection */
start_line = gtk_text_iter_get_line (start);
end_line = gtk_text_iter_get_line (end);
gtk_text_buffer_begin_user_action (buffer); /* begin user action */
/* get position within line for start/end iters */
start_pos = gtk_text_iter_get_line_offset (start);
end_pos = gtk_text_iter_get_line_offset (end);
/* insert multi-line block comments */
if (inst->comment_blk_beg && inst->comment_blk_end &&
!app->cmtusesingle &&
(end_line > start_line || end_pos > start_pos)) {
/* insert beginning comment for block */
gtk_text_buffer_insert (buffer, start, inst->comment_blk_beg, -1);
/* revalidate end iter from saved end_mark */
gtk_text_buffer_get_iter_at_mark (buffer, end, end_mark);
/* insert ending comment for block */
gtk_text_buffer_insert (buffer, end, inst->comment_blk_end, -1);
}
else { /* insert single-line comments */
GtkTextIter iter;
/* check if end is at beginning of multi-line - subtract 1-line */
if ((gtk_text_iter_get_visible_line_offset (end) == 0) &&
(end_line > start_line)) {
end_line--;
}
/* loop over each line inserting comment at beginning */
for (i = start_line; i <= end_line; i++) {
gtk_text_buffer_get_iter_at_line (buffer, &iter, i);
gtk_text_buffer_insert (buffer, &iter, commentstr, -1);
}
}
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buffer),
bracket_hl);
gtk_text_buffer_end_user_action (buffer); /* end user action */
/* revalidate iters */
gtk_text_buffer_get_iter_at_mark (buffer, start, start_mark);
gtk_text_buffer_get_iter_at_mark (buffer, end, end_mark);
/* extend selection to cover new comment syntax (for uncomment) */
gtk_text_buffer_select_range (buffer, end, start);
gtk_text_buffer_delete_mark (buffer, start_mark);
gtk_text_buffer_delete_mark (buffer, end_mark);
}
/** TODO add to selection bounds at end of block at the end so block comments
* can be enabled and removed without having to reselect the additional end
* comment in range.
*/
void buffer_uncomment_lines (gpointer data, GtkTextIter *start, GtkTextIter *end)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextMark *start_mark, *end_mark;
GtkTextIter iter = *start;
gint i, start_line, end_line;
const gchar *commentstr = app->comment; /* set comment from settings */
gboolean blockcmt = FALSE;
gboolean bracket_hl;
if (inst->comment_single) /* if comment based on lang_id */
commentstr = inst->comment_single; /* update commentstr from lang_id */
bracket_hl = gtk_source_buffer_get_highlight_matching_brackets (GTK_SOURCE_BUFFER (buffer));
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buffer), FALSE);
/* preserve marks for start/end to revalidate iterators before return */
start_mark = gtk_text_buffer_create_mark (buffer, NULL, start, FALSE);
end_mark = gtk_text_buffer_create_mark (buffer, NULL, end, FALSE);
/* get lines indicated by start/end iters passed based on selection */
start_line = gtk_text_iter_get_line (start);
end_line = gtk_text_iter_get_line (end);
/* check if end is at beginning of multi-line - subtract 1-line */
if ((gtk_text_iter_get_visible_line_offset (end) == 0) &&
(end_line > start_line)) {
end_line--;
}
gtk_text_buffer_begin_user_action (buffer); /* begin user action */
/* if block comment strings are set */
if (inst->comment_blk_beg && inst->comment_blk_end) {
const gchar *p = inst->comment_blk_beg;
gunichar c = gtk_text_iter_get_char (&iter);
while (*p && c == (gunichar)*p) { /* compare with chars in buffer */
if (!gtk_text_iter_forward_char (&iter))
break;
c = gtk_text_iter_get_char (&iter);
p++;
}
if (*p == '\0') /* all chars matched to nul-character, block comment */
blockcmt = TRUE;
}
if (blockcmt) { /* handle removal of block comments, which can be sin same line */
const gchar *p = inst->comment_blk_end; /* pointer to opening string */
gunichar c;
/* iter and start properly set from above block, just delete */
gtk_text_buffer_delete (buffer, start, &iter);
/* revalidate end iterator */
gtk_text_buffer_get_iter_at_mark (buffer, end, end_mark);
iter = *end; /* set iter to end */
while (*p) /* move to end char of comment_blk_end */
p++;
p--;
/* backup to last char before end iter */
if (!gtk_text_iter_backward_char (&iter)) {
/* handle error */
goto unmatchedblk;
}
c = gtk_text_iter_get_char (&iter); /* get last char before end */
/* iterate backwards until the beginning char in comment found */
while (p > inst->comment_blk_end && (gunichar)*p == c) {
if (!gtk_text_iter_backward_char (&iter)) {
/* handle error */
goto unmatchedblk;
}
c = gtk_text_iter_get_char (&iter);
p--;
}
if (p == inst->comment_blk_end) /* all chars matched as comment */
gtk_text_buffer_delete (buffer, &iter, end); /* delete comment */
else {
/* handle error */
goto unmatchedblk;
}
}
else { /* remove single-line comment from all lines in selection */
for (i = start_line; i <= end_line; i++) {
GtkTextIter iterstart;
gunichar c;
gchar *p = (gchar *)commentstr;
gtk_text_buffer_get_iter_at_line (buffer, &iterstart, i);
iter = iterstart;
c = gtk_text_iter_get_char (&iter);
/* move to start of comment string (displaced comment) */
while (c != (gunichar)*p && (c == ' ' || c == '\t')) {
if (!gtk_text_iter_forward_char (&iter))
goto nxtuncomment;
c = gtk_text_iter_get_char (&iter);
}
iterstart = iter; /* set start iter */
for (; *p; p++) { /* set characters to delete */
if ((gunichar)*p != gtk_text_iter_get_char (&iter) ||
!gtk_text_iter_forward_char (&iter))
break;
}
/* delete comment */
gtk_text_buffer_delete (buffer, &iterstart, &iter);
nxtuncomment:;
}
}
unmatchedblk:;
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buffer),
bracket_hl);
gtk_text_buffer_end_user_action (buffer); /* end user action */
/* revalidate iters */
gtk_text_buffer_get_iter_at_mark (buffer, start, start_mark);
gtk_text_buffer_get_iter_at_mark (buffer, end, end_mark);
gtk_text_buffer_delete_mark (buffer, start_mark);
gtk_text_buffer_delete_mark (buffer, end_mark);
}
/** helper function for insert_eol and indent_auto actions after "mark-set".
* needed because "mark-set" fires before indent and eol inserted causing
* app->{line,col,lines} not to update statusbar on [Enter]
*/
void buffer_update_pos (gpointer data)
{
/* Fri Jul 13 2018 18:52:36 CDT
* moved line/col update to status_set_default to avoid
* mark_set firing when gtk_text_view_scroll_mark_onscreen
* is called, causing line number to reset to 1 or jump by
* the number of lines scrolled in forward direction.
*/
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextMark *ins = gtk_text_buffer_get_insert (buffer);
/* scroll text-view to keep cursor position in window */
gtk_text_view_scroll_mark_onscreen (GTK_TEXT_VIEW(einst->view), ins);
}
/** auto-indent on return */
gboolean buffer_indent_auto (gpointer data)
{
mainwin_t *app = data;
einst_t *einst = app->einst[app->focused];
kinst_t *inst = einst->inst;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(inst->buf);
GtkTextMark *ins;
GtkTextIter end, iter;
gchar *indstr = NULL;
gint line, nspaces = 0;
ins = gtk_text_buffer_get_insert (buffer);
gtk_text_buffer_get_iter_at_mark (buffer, &end, ins);
line = gtk_text_iter_get_line (&end);
gtk_text_buffer_get_iter_at_line (buffer, &iter, line);
for (;;) {
gunichar c;
c = gtk_text_iter_get_char (&iter);
if (c == '\t' || c == ' ')
nspaces += (c == '\t') ? app->softtab : 1;
else
break;
gtk_text_iter_forward_char (&iter);
}
// app->indentpl = nspaces; /* set previous line indent */
if (nspaces) {
// indstr = g_strdup_printf ("\n%*s", nspaces, " ");
indstr = g_strdup_printf ("%s%*s", app->eolstr[app->eol], nspaces, " ");
gtk_text_buffer_insert_at_cursor (buffer, indstr, -1);