-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathgtk_filebuf.c
2478 lines (2079 loc) · 85.2 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"
#include <glib.h>
#include <glib/gstdio.h>
// gboolean buffer_chk_mod (kwinst *app)
// {
// // GtkTextBuffer *buffer;
// //
// // buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->view));
//
// return (app->modified = gtk_text_buffer_get_modified (app->buffer));
// }
void file_get_stats (const gchar *filename, kwinst *app)
{
struct stat sb;
app->filemode = 0;
app->fileuid = 0;
app->filegid = 0;
if (!filename) return; /* validate filename */
/* validate file exists */
if (!g_file_test (filename, G_FILE_TEST_EXISTS)) {
g_print ("error: file not found in file_get_stats() '%s'\n", filename);
return;
}
if (g_stat (filename, &sb)) { /* get file stats */
g_print ("error: stat of file failed '%s'\n", filename);
return;
}
app->filemode = sb.st_mode;
app->fileuid = sb.st_uid;
app->filegid = sb.st_gid;
}
void buffer_clear (kwinst *app)
{
/* if buffer changed, prompt for save */
if (buffer_prompt_on_mod (app) == TRUE)
buffer_save_file (app, NULL);
/* free existing filename, set NULL */
if (app->filename)
app_free_filename (app);
/* clear exising buffer, set modified to FALSE */
gtk_text_buffer_set_text (GTK_TEXT_BUFFER(app->buffer), "", -1);
gtk_text_buffer_set_modified (GTK_TEXT_BUFFER(app->buffer), FALSE);
app->modified = FALSE;
gtkwrite_window_set_title (NULL, app);
app->filemode = 0;
app->fileuid = 0;
app->filegid = 0;
#ifdef HAVESOURCEVIEW
app->language = NULL; /* reset syntax language */
#endif
/* reset values to default */
status_set_default (app);
}
/** file_open 'filename' in existing or new editor instance.
* 'filename' is opened in current instance if buffer is
* empty & not modified, otherwise open in new instance.
*/
void file_open (kwinst *app, gchar *filename)
{
if (!filename) return;
gint cc = gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER(app->buffer));
gchar *posixfn = get_posix_filename (filename);
if (!posixfn) {
dlg_info_win (app, "get_posix_filename() failed.",
"Error: filename conversion failure");
return;
}
/* if no chars in buffer and not modified, open in current window */
if (!cc && !gtk_text_buffer_get_modified (GTK_TEXT_BUFFER(app->buffer))) {
if (app->filename) /* free existing filename */
app_free_filename (app);
app->filename = posixfn; /* assign new filename */
split_fname (app); /* split path, name, ext */
buffer_insert_file (app, NULL); /* insert file in buffer */
ibar_eol_chk_default (app); /* check non-default eol */
// buffer_eol_chk_default (app); /* check non-default eol */
}
else { /* open in new instance */
if (!create_new_editor_inst (app, posixfn)) {
/* clear current file, use current window if spawn fails */
buffer_clear (app); /* check for save and clear */
status_set_default (app); /* statusbar default values */
/* dialog advising of failure and consequences */
gchar *msg = g_strdup_printf ("Error: failed to spawn separate\n"
"instance of %s\n", app->exename);
dlg_info_win (app, msg, "Error - Unable to Create 2nd Instance");
g_free (msg);
}
g_free (posixfn);
}
/* final call - return focus to textview */
gtk_widget_grab_focus (app->view);
}
/** open file or insert file at cursor.
* if filename is given, the file is inserted at the cursor without
* changing the current filename within the editor, otherwise,
* simply opens the file contained in app->filename.
*/
void buffer_insert_file (kwinst *app, gchar *filename)
{
gchar *filebuf = NULL;
gchar *fnameok = filename;
// GtkTextView *view = GTK_TEXT_VIEW (app->view);
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
if (!filename) {
filename = app->filename;
// split_fname (app);
}
if (g_file_get_contents (filename, &filebuf, &(app->fsize), NULL)) {
// buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->view));
/* TODO: handle BOM here, currently only ASCII/utf-8 supported */
if (buffer_file_get_bom (filebuf, app) > 1) {
extern const gchar *bomstr[];
gchar *errstr = g_strdup_printf ("'%s' contains unsupported %s.",
app->fname, bomstr[app->bom]);
status_set_default (app);
err_dialog_win (app, errstr);
g_free (errstr);
if (filebuf) g_free (filebuf);
return;
}
gtk_text_buffer_insert_at_cursor (buffer, filebuf, -1);
/* get iter and check last char and if not '\n', then insert
* (app->posixeol)
*/
// gtk_text_buffer_insert_at_cursor (app->buffer, "\n", -1);
if (filebuf) g_free (filebuf);
if (app->poscurend) {
/* scroll to insert mark */
gtk_text_view_scroll_to_mark (GTK_TEXT_VIEW(app->view),
gtk_text_buffer_get_insert (buffer),
0.0, TRUE, 0.0, 1.0);
}
else {
/* place cursor "insert_mark" at start of file */
GtkTextIter iter;
gtk_text_buffer_get_start_iter (buffer, &iter);
gtk_text_buffer_place_cursor (buffer, &iter);
}
if (fnameok) { /* inserting file at cursor */
gtk_text_buffer_set_modified (buffer , TRUE); /* inserted */
app->modified = TRUE;
// status = g_strdup_printf ("inserted : '%s'", filename); /* remove */
}
else { /* opening file */
file_get_stats (filename, app); /* save file mode, UID, GID */
// buffer_get_eol (app); /* detect EOL, LF, CRLF, CR (moved down) */
gtk_text_buffer_set_modified (buffer , FALSE); /* opened */
app->modified = FALSE;
buffer_get_eol (app); /* detect EOL, LF, CRLF, CR */
// status = g_strdup_printf ("loaded : '%s'", app->fname); /* remove */
gtkwrite_window_set_title (NULL, app); /* set window title */
/* add watch on file */
if (!app->filemonfn) {
file_monitor_add (app);
}
else if (!g_strcmp0 (app->filemonfn, app->filename) == 0) {
if (file_monitor_cancel (app))
file_monitor_add (app);
}
}
}
else {
gchar *errstr = g_strdup_printf ("read of file '%s' failed.", app->fname);
err_dialog_win (app, errstr);
g_free (errstr);
}
// status_update_str (app, status); /* remove */
// g_free (status);
/* reset values to default */
status_set_default (app);
/* set syntax highlighting language */
#ifdef HAVESOURCEVIEW
if (app->highlight)
sourceview_guess_language (app);
#endif
/* temporary dump of language information */
// sourceview_get_languange_info (app);
/*
if (app->language)
g_print ("lang_id: %s\n", gtk_source_language_get_id (app->language));
g_print ("comment single '%s' blk_beg: '%s' blk_end: '%s'\n",
app->comment_single, app->comment_blk_beg, app->comment_blk_end);
*/
}
gboolean buffer_select_all (kwinst *app)
{
GtkTextIter start, end;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
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 (kwinst *app)
{
GtkTextIter start, end;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
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 (kwinst *app)
{
GtkTextBuffer *buf = GTK_TEXT_BUFFER(app->buffer);
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 (buf, &start, &end)) {
gtk_text_buffer_get_iter_at_mark (buf, &start,
gtk_text_buffer_get_insert (buf));
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 (buf, &start);
bstack_clear (app);
return TRUE;
}
}
/* select range */
gtk_text_buffer_select_range (buf, &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 (kwinst *app)
{
GtkTextBuffer *buf = GTK_TEXT_BUFFER(app->buffer);
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 (buf, &start, &end)) {
gtk_text_buffer_get_iter_at_mark (buf, &start,
gtk_text_buffer_get_insert (buf));
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 (buf, &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 (buf, &start);
bstack_clear (app);
return TRUE;
}
else
gtk_text_iter_forward_char (&end);
}
/* select range */
gtk_text_buffer_select_range (buf, &start, &end);
return TRUE;
}
/** buffer reduce selection bounds by 1 at end */
gboolean buffer_reduce_selection (kwinst *app)
{
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
GtkTextIter start, end;
/** check existing selection, bstack active and first movement RIGHT */
if (!gtk_text_buffer_get_selection_bounds (buffer, &start, &end) ||
!app->bindex || !app->bstack[0]) {
bstack_clear (app);
return FALSE;
}
gtk_text_iter_backward_char (&end);
/* select range */
gtk_text_buffer_select_range (buffer, &start, &end);
return TRUE;
}
/** buffer_find_words() in buffer, add to ternary tree for word completion.
* TODO - add wcomplen to app.
*/
void buffer_find_words (kwinst *app)
{
gint idx = 0, wcomplen = 3;
GtkTextIter start, end;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
gtk_text_buffer_begin_user_action (buffer); /* begin user action */
gtk_text_buffer_get_start_iter (buffer, &start);
while (!gtk_text_iter_starts_word (&start))
gtk_text_iter_forward_char (&start);
end = start;
while (gtk_text_iter_forward_word_end (&end)) {
const gchar *word = gtk_text_buffer_get_text (buffer,
&start, &end, FALSE);
const gchar *p = word;
for (; *p; p++) {}
if (p - word > wcomplen)
g_print ("[%4d] : %s\n", idx++, word);
start = end;
for (;;) {
if (!gtk_text_iter_forward_char (&start))
goto wordseldone;
if (gtk_text_iter_starts_word (&start))
break;
}
end = start; /* not 100% needed, fwd_word_end will jumpt to next end */
}
wordseldone:;
gtk_text_buffer_end_user_action (buffer); /* end user action */
}
/** 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 (kwinst *app,
GtkTextIter *start,
GtkTextIter *end)
{
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
GtkTextMark *start_mark, *end_mark;
gint i, start_line, end_line;
const gchar *commentstr = app->comment; /* set comment from settings */
#ifdef HAVESOURCEVIEW
gboolean bracket_hl;
gint start_pos, end_pos;
if (app->comment_single) /* if comment based on lang_id */
commentstr = app->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);
#endif
/* 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 */
#ifdef HAVESOURCEVIEW
/* 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 (app->comment_blk_beg && app->comment_blk_end &&
!app->cmtusesingle &&
(end_line > start_line || end_pos > start_pos)) {
/* insert beginning comment for block */
gtk_text_buffer_insert (buffer, start, app->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, app->comment_blk_end, -1);
}
else { /* insert single-line comments */
#endif
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);
}
#ifdef HAVESOURCEVIEW
}
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buffer),
bracket_hl);
#endif
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 (kwinst *app,
GtkTextIter *start,
GtkTextIter *end)
{
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
GtkTextMark *start_mark, *end_mark;
GtkTextIter iter = *start;
gint i, start_line, end_line;
const gchar *commentstr = app->comment; /* set comment from settings */
#ifdef HAVESOURCEVIEW
gboolean blockcmt = FALSE;
gboolean bracket_hl;
if (app->comment_single) /* if comment based on lang_id */
commentstr = app->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);
#endif
/* 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 */
#ifdef HAVESOURCEVIEW
/* if block comment strings are set */
if (app->comment_blk_beg && app->comment_blk_end) {
const gchar *p = app->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 = app->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 > app->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 == app->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 */
#endif
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:;
}
#ifdef HAVESOURCEVIEW
}
unmatchedblk:;
gtk_source_buffer_set_highlight_matching_brackets (GTK_SOURCE_BUFFER (buffer),
bracket_hl);
#endif
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);
}
/** callback for ibar_handle_quit, handling save, exit or cancel. */
void ib_handle_quit (GtkInfoBar *bar, gint response_id, kwinst *app)
{
switch (response_id) {
case GTK_RESPONSE_CANCEL: /* return to editor */
gtk_widget_hide (GTK_WIDGET(bar));
break;
case GTK_RESPONSE_NO: /* quit */
gtk_main_quit ();
break;
case GTK_RESPONSE_YES: /* check modified, prompt for save */
gtk_widget_hide (GTK_WIDGET(bar));
if (!app->filename) {
gchar *filename;
while (!(filename = get_save_filename (app))) {
if (dlg_yes_no_msg (app, "Warning: Do you want to cancel save?",
"Warning - Save Canceled", FALSE))
goto cancel_save; /* return to editor */
}
/* on exit cleanups */
if (app->trimendws)
buffer_remove_trailing_ws (GTK_TEXT_BUFFER(app->buffer));
if (app->posixeof)
buffer_require_posix_eof (app);
buffer_write_file (app, filename);
g_free (filename);
}
else {
/* on exit cleanups */
if (app->trimendws)
buffer_remove_trailing_ws (GTK_TEXT_BUFFER(app->buffer));
if (app->posixeof)
buffer_require_posix_eof (app);
buffer_write_file (app, NULL);
}
gtk_main_quit ();
break;
}
cancel_save:;
/* set text_view sensitive TRUE */
if (!gtk_widget_get_sensitive (app->view))
gtk_widget_set_sensitive (app->view, TRUE);
/* grab focus for textview */
gtk_widget_grab_focus (app->view);
/* reset flags */
app->ibflags = 0;
}
/** ibar_handle_quit catches File->Close and on_window_delete_event.
* checks gtk_text_buffer_get_modified and presents an infobar
* prompting for save (Save, Discard, Cancel). callback ib_handle_quit
* receives the response_id from the user choice and acts accordingly.
*/
void ibar_handle_quit (kwinst *app)
{
/* create button definitions for infobar */
ibbtndef btndef[] = { { .btntext = "_Save", .response_id = GTK_RESPONSE_YES },
{ .btntext = "_Discard", .response_id = GTK_RESPONSE_NO },
{ .btntext = "_Cancel", .response_id = GTK_RESPONSE_CANCEL },
{ .btntext = "", .response_id = 0 } };
if (app->ibflags & IBAR_VISIBLE) /* prevent duplicate infobars */
return;
app->ibflags |= IBAR_VIEW_SENSITIVE; /* use |= to preserve other flags */
if (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER(app->buffer)) == TRUE)
{
/* create message with markup */
gchar *msg = g_markup_printf_escaped (
"<span font_weight=\"bold\" font_size='large'>"
"File Modified!</span>\n\n"
"Do you want to save the changes you have made?");
/* show message and buttons in infobar */
show_info_bar_choice (msg, GTK_MESSAGE_WARNING, btndef, ib_handle_quit, app);
g_free (msg); /* free msg */
}
else if (app->filename) /* not modified, apply on-exit cleanups */
{
if (app->trimendws &&
gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER(app->buffer)))
buffer_remove_trailing_ws (GTK_TEXT_BUFFER(app->buffer));
if (app->posixeof)
buffer_require_posix_eof (app);
if (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER(app->buffer)))
buffer_write_file (app, NULL);
gtk_main_quit ();
}
else
gtk_main_quit ();
}
/** presents dialog on quit if file modified
* (replaced by ibar_handle_quit)
*/
gboolean buffer_chk_save_on_exit (GtkTextBuffer *buffer)
{
if (!buffer) return FALSE;
if (gtk_text_buffer_get_modified (buffer) == TRUE)
return dlg_yes_no_msg (NULL, "Do you want to save the changes you have made?",
"Save File?", TRUE);
return FALSE;
}
/** presents dialog on quit (or WM_CLOSE) if file modified
* (replaced by ibar_handle_quit)
*/
void buffer_handle_quit (kwinst *app)
{
/* check changed, prompt yes/no */
if (buffer_chk_save_on_exit (GTK_TEXT_BUFFER(app->buffer)))
{
if (!app->filename) {
gchar *filename;
while (!(filename = get_save_filename (app))) {
if (dlg_yes_no_msg (app, "Warning: Do you want to cancel save?",
"Warning - Save Canceled", FALSE))
goto cancel_save;
}
if (app->trimendws)
buffer_remove_trailing_ws (GTK_TEXT_BUFFER(app->buffer));
if (app->posixeof)
buffer_require_posix_eof (app);
buffer_write_file (app, filename);
g_free (filename);
return;
} /*
else {
if (app->trimendws)
buffer_remove_trailing_ws (GTK_TEXT_BUFFER(app->buffer));
if (app->posixeof)
buffer_require_posix_eof (app);
buffer_write_file (app, app->filename);
} */
}
else if (app->filename)
{
if (app->trimendws &&
gtk_text_buffer_get_char_count (GTK_TEXT_BUFFER(app->buffer)))
buffer_remove_trailing_ws (GTK_TEXT_BUFFER(app->buffer));
if (app->posixeof)
buffer_require_posix_eof (app);
if (gtk_text_buffer_get_modified (GTK_TEXT_BUFFER(app->buffer)))
buffer_write_file (app, NULL);
}
cancel_save:;
}
/** buffer_save_file in new 'filename' or in existing if NULL.
* if 'filename' is provided, Save-As 'filename', otherwise if
* 'filename' is NULL, Save in existing app->filename or prompt
* with file_save dialog. cancel and add file monitoring as
* required. save file stats for existing file, update status.
* if HAVESOURCEVIEW, guess language syntax.
*/
void buffer_save_file (kwinst *app, gchar *filename)
{
if (filename) { /* file_save_as new filename */
if (app->filename) { /* if existing file, free filename */
app_free_filename (app);
if (!file_monitor_cancel (app)) /* cancel existing monitor */
{ /* handle error */ }
}
app->filename = filename; /* assign to app->filename */
split_fname (app);
}
else {
if (!app->filename) { /* fix critical error: if -> while, remove return */
while (!(app->filename = get_save_filename (app))) {
if (dlg_yes_no_msg (app, "Error: Get save filename canceled!\n"
"Do you want to cancel save?",
"Warning - Save Canceled", FALSE))
return;
}
split_fname (app);
}
}
status_save_filename (app, NULL); /* update statusbar Saving....*/
app->mfp_savecmd = TRUE; /* set flag TRUE - save in progress */
buffer_write_file (app, filename); /* write to file app->filename */
if (!app->mfp_handler) /* if not monitoring file */
file_monitor_add (app); /* setup monitoring on new name */
file_get_stats (app->filename, app); /* check/save file stats */
status_set_default (app); /* restore statusbar */
#ifdef HAVESOURCEVIEW
if (app->highlight) /* if syntax highlight enabled */
sourceview_guess_language (app); /* guess source language */
#endif
}
/** TODO: this is a mess, need to split duplicated functionality
* from buffer_save_file. This should be a write only. The rest
* of the functionality should be in buffer_save_file or save-as.
* Move all 'filename' condiitonals above, just save 'filename'.
*/
void buffer_write_file (kwinst *app, gchar *filename)
{
GError *err=NULL;
gchar *text;
gboolean result;
GtkWidget *view = app->view;
GtkTextBuffer *buffer = GTK_TEXT_BUFFER(app->buffer);
GtkTextIter start, end;
while (gtk_events_pending()) /* process all pending events */
gtk_main_iteration();
/* disable text view and get contents of buffer */
gtk_widget_set_sensitive (view, FALSE);
/* using app->buffer, so call to get buffer from view unneeded */
// buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (app->view));
/* get start and end iters for buffer */
gtk_text_buffer_get_start_iter (buffer, &start);
gtk_text_buffer_get_end_iter (buffer, &end);