/
editor.c
5350 lines (4373 loc) · 146 KB
/
editor.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
/*
* editor.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
* Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
* Copyright 2009-2012 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
*
* 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.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/**
* @file editor.h
* Editor-related functions for @ref GeanyEditor.
* Geany uses the Scintilla editing widget, and this file is mostly built around
* Scintilla's functionality.
* @see sciwrappers.h.
*/
/* Callbacks for the Scintilla widget (ScintillaObject).
* Most important is the sci-notify callback, handled in on_editor_notification().
* This includes auto-indentation, comments, auto-completion, calltips, etc.
* Also some general Scintilla-related functions.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "editor.h"
#include "app.h"
#include "callbacks.h"
#include "dialogs.h"
#include "documentprivate.h"
#include "filetypesprivate.h"
#include "geanyobject.h"
#include "highlighting.h"
#include "keybindings.h"
#include "main.h"
#include "prefs.h"
#include "projectprivate.h"
#include "sciwrappers.h"
#include "support.h"
#include "symbols.h"
#include "templates.h"
#include "ui_utils.h"
#include "utils.h"
#include "SciLexer.h"
#include "gtkcompat.h"
#include <ctype.h>
#include <string.h>
#include <gdk/gdkkeysyms.h>
static GHashTable *snippet_hash = NULL;
static GtkAccelGroup *snippet_accel_group = NULL;
static gboolean autocomplete_scope_shown = FALSE;
static const gchar geany_cursor_marker[] = "__GEANY_CURSOR_MARKER__";
/* holds word under the mouse or keyboard cursor */
static gchar current_word[GEANY_MAX_WORD_LENGTH];
/* Initialised in keyfile.c. */
GeanyEditorPrefs editor_prefs;
EditorInfo editor_info = {current_word, -1};
static struct
{
gchar *text;
gboolean set;
gchar *last_word;
guint tag_index;
gint pos;
ScintillaObject *sci;
} calltip = {NULL, FALSE, NULL, 0, 0, NULL};
static gchar indent[100];
static void on_new_line_added(GeanyEditor *editor);
static gboolean handle_xml(GeanyEditor *editor, gint pos, gchar ch);
static void insert_indent_after_line(GeanyEditor *editor, gint line);
static void auto_multiline(GeanyEditor *editor, gint pos);
static void auto_close_chars(ScintillaObject *sci, gint pos, gchar c);
static void close_block(GeanyEditor *editor, gint pos);
static void editor_highlight_braces(GeanyEditor *editor, gint cur_pos);
static void read_current_word(GeanyEditor *editor, gint pos, gchar *word, gsize wordlen,
const gchar *wc, gboolean stem);
static gsize count_indent_size(GeanyEditor *editor, const gchar *base_indent);
static const gchar *snippets_find_completion_by_name(const gchar *type, const gchar *name);
static void snippets_make_replacements(GeanyEditor *editor, GString *pattern);
static GeanyFiletype *editor_get_filetype_at_line(GeanyEditor *editor, gint line);
static gboolean sci_is_blank_line(ScintillaObject *sci, gint line);
void editor_snippets_free(void)
{
g_hash_table_destroy(snippet_hash);
gtk_window_remove_accel_group(GTK_WINDOW(main_widgets.window), snippet_accel_group);
}
static void snippets_load(GKeyFile *sysconfig, GKeyFile *userconfig)
{
gsize i, j, len = 0, len_keys = 0;
gchar **groups_user, **groups_sys;
gchar **keys_user, **keys_sys;
gchar *value;
GHashTable *tmp;
/* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
snippet_hash =
g_hash_table_new_full(g_str_hash, g_str_equal, g_free, (GDestroyNotify) g_hash_table_destroy);
/* first read all globally defined auto completions */
groups_sys = g_key_file_get_groups(sysconfig, &len);
for (i = 0; i < len; i++)
{
if (strcmp(groups_sys[i], "Keybindings") == 0)
continue;
keys_sys = g_key_file_get_keys(sysconfig, groups_sys[i], &len_keys, NULL);
/* create new hash table for the read section (=> filetype) */
tmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert(snippet_hash, g_strdup(groups_sys[i]), tmp);
for (j = 0; j < len_keys; j++)
{
g_hash_table_insert(tmp, g_strdup(keys_sys[j]),
utils_get_setting_string(sysconfig, groups_sys[i], keys_sys[j], ""));
}
g_strfreev(keys_sys);
}
g_strfreev(groups_sys);
/* now read defined completions in user's configuration directory and add / replace them */
groups_user = g_key_file_get_groups(userconfig, &len);
for (i = 0; i < len; i++)
{
if (strcmp(groups_user[i], "Keybindings") == 0)
continue;
keys_user = g_key_file_get_keys(userconfig, groups_user[i], &len_keys, NULL);
tmp = g_hash_table_lookup(snippet_hash, groups_user[i]);
if (tmp == NULL)
{ /* new key found, create hash table */
tmp = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free);
g_hash_table_insert(snippet_hash, g_strdup(groups_user[i]), tmp);
}
for (j = 0; j < len_keys; j++)
{
value = g_hash_table_lookup(tmp, keys_user[j]);
if (value == NULL)
{ /* value = NULL means the key doesn't yet exist, so insert */
g_hash_table_insert(tmp, g_strdup(keys_user[j]),
utils_get_setting_string(userconfig, groups_user[i], keys_user[j], ""));
}
else
{ /* old key and value will be freed by destroy function (g_free) */
g_hash_table_replace(tmp, g_strdup(keys_user[j]),
utils_get_setting_string(userconfig, groups_user[i], keys_user[j], ""));
}
}
g_strfreev(keys_user);
}
g_strfreev(groups_user);
}
static gboolean on_snippet_keybinding_activate(gchar *key)
{
GeanyDocument *doc = document_get_current();
const gchar *s;
if (!doc || !gtk_widget_has_focus(GTK_WIDGET(doc->editor->sci)))
return FALSE;
s = snippets_find_completion_by_name(doc->file_type->name, key);
if (!s) /* allow user to specify keybindings for "special" snippets */
{
GHashTable *specials = g_hash_table_lookup(snippet_hash, "Special");
if (G_LIKELY(specials != NULL))
s = g_hash_table_lookup(specials, key);
}
if (!s)
{
utils_beep();
return FALSE;
}
editor_insert_snippet(doc->editor, sci_get_current_position(doc->editor->sci), s);
sci_scroll_caret(doc->editor->sci);
return TRUE;
}
static void add_kb(GKeyFile *keyfile, const gchar *group, gchar **keys)
{
gsize i;
if (!keys)
return;
for (i = 0; i < g_strv_length(keys); i++)
{
guint key;
GdkModifierType mods;
gchar *accel_string = g_key_file_get_value(keyfile, group, keys[i], NULL);
gtk_accelerator_parse(accel_string, &key, &mods);
g_free(accel_string);
if (key == 0 && mods == 0)
{
g_warning("Can not parse accelerator \"%s\" from user snippets.conf", accel_string);
continue;
}
gtk_accel_group_connect(snippet_accel_group, key, mods, 0,
g_cclosure_new_swap((GCallback)on_snippet_keybinding_activate,
g_strdup(keys[i]), (GClosureNotify)g_free));
}
}
static void load_kb(GKeyFile *sysconfig, GKeyFile *userconfig)
{
const gchar kb_group[] = "Keybindings";
gchar **keys = g_key_file_get_keys(userconfig, kb_group, NULL, NULL);
gchar **ptr;
/* remove overridden keys from system keyfile */
foreach_strv(ptr, keys)
g_key_file_remove_key(sysconfig, kb_group, *ptr, NULL);
add_kb(userconfig, kb_group, keys);
g_strfreev(keys);
keys = g_key_file_get_keys(sysconfig, kb_group, NULL, NULL);
add_kb(sysconfig, kb_group, keys);
g_strfreev(keys);
}
void editor_snippets_init(void)
{
gchar *sysconfigfile, *userconfigfile;
GKeyFile *sysconfig = g_key_file_new();
GKeyFile *userconfig = g_key_file_new();
sysconfigfile = g_build_filename(app->datadir, "snippets.conf", NULL);
userconfigfile = g_build_filename(app->configdir, "snippets.conf", NULL);
/* check for old autocomplete.conf files (backwards compatibility) */
if (! g_file_test(userconfigfile, G_FILE_TEST_IS_REGULAR))
SETPTR(userconfigfile, g_build_filename(app->configdir, "autocomplete.conf", NULL));
/* load the actual config files */
g_key_file_load_from_file(sysconfig, sysconfigfile, G_KEY_FILE_NONE, NULL);
g_key_file_load_from_file(userconfig, userconfigfile, G_KEY_FILE_NONE, NULL);
snippets_load(sysconfig, userconfig);
/* setup snippet keybindings */
snippet_accel_group = gtk_accel_group_new();
gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), snippet_accel_group);
load_kb(sysconfig, userconfig);
g_free(sysconfigfile);
g_free(userconfigfile);
g_key_file_free(sysconfig);
g_key_file_free(userconfig);
}
static gboolean on_editor_button_press_event(GtkWidget *widget, GdkEventButton *event,
gpointer data)
{
GeanyEditor *editor = data;
GeanyDocument *doc = editor->document;
/* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
* fake event to show the editor menu triggered by a key event where we want to use the
* text cursor position. */
if (event->x > 0.0 && event->y > 0.0)
editor_info.click_pos = sci_get_position_from_xy(editor->sci,
(gint)event->x, (gint)event->y, FALSE);
else
editor_info.click_pos = sci_get_current_position(editor->sci);
if (event->button == 1)
{
guint state = keybindings_get_modifiers(event->state);
if (event->type == GDK_BUTTON_PRESS && editor_prefs.disable_dnd)
{
gint ss = sci_get_selection_start(editor->sci);
sci_set_selection_end(editor->sci, ss);
}
if (event->type == GDK_BUTTON_PRESS && state == GEANY_PRIMARY_MOD_MASK)
{
sci_set_current_position(editor->sci, editor_info.click_pos, FALSE);
editor_find_current_word(editor, editor_info.click_pos,
current_word, sizeof current_word, NULL);
if (*current_word)
return symbols_goto_tag(current_word, TRUE);
else
keybindings_send_command(GEANY_KEY_GROUP_GOTO, GEANY_KEYS_GOTO_MATCHINGBRACE);
return TRUE;
}
return document_check_disk_status(doc, FALSE);
}
/* calls the edit popup menu in the editor */
if (event->button == 3)
{
gboolean can_goto;
/* ensure the editor widget has the focus after this operation */
gtk_widget_grab_focus(widget);
editor_find_current_word(editor, editor_info.click_pos,
current_word, sizeof current_word, NULL);
can_goto = sci_has_selection(editor->sci) || current_word[0] != '\0';
ui_update_popup_goto_items(can_goto);
ui_update_popup_copy_items(doc);
ui_update_insert_include_item(doc, 0);
g_signal_emit_by_name(geany_object, "update-editor-menu",
current_word, editor_info.click_pos, doc);
gtk_menu_popup(GTK_MENU(main_widgets.editor_menu),
NULL, NULL, NULL, NULL, event->button, event->time);
return TRUE;
}
return FALSE;
}
static gboolean is_style_php(gint style)
{
if ((style >= SCE_HPHP_DEFAULT && style <= SCE_HPHP_OPERATOR) ||
style == SCE_HPHP_COMPLEX_VARIABLE)
{
return TRUE;
}
return FALSE;
}
static gint editor_get_long_line_type(void)
{
if (app->project)
switch (app->project->priv->long_line_behaviour)
{
case 0: /* marker disabled */
return 2;
case 1: /* use global settings */
break;
case 2: /* custom (enabled) */
return editor_prefs.long_line_type;
}
if (!editor_prefs.long_line_enabled)
return 2;
else
return editor_prefs.long_line_type;
}
static gint editor_get_long_line_column(void)
{
if (app->project && app->project->priv->long_line_behaviour != 1 /* use global settings */)
return app->project->priv->long_line_column;
else
return editor_prefs.long_line_column;
}
#define get_project_pref(id)\
(app->project ? app->project->priv->id : editor_prefs.id)
static const GeanyEditorPrefs *
get_default_prefs(void)
{
static GeanyEditorPrefs eprefs;
eprefs = editor_prefs;
/* project overrides */
eprefs.indentation = (GeanyIndentPrefs*)editor_get_indent_prefs(NULL);
eprefs.long_line_type = editor_get_long_line_type();
eprefs.long_line_column = editor_get_long_line_column();
eprefs.line_wrapping = get_project_pref(line_wrapping);
eprefs.line_break_column = get_project_pref(line_break_column);
eprefs.auto_continue_multiline = get_project_pref(auto_continue_multiline);
return &eprefs;
}
/* Gets the prefs for the editor.
* Prefs can be different according to project or document.
* @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
* settings may have changed, or if this function has been called for a different editor.
* @param editor The editor, or @c NULL to get the default prefs.
* @return The prefs. */
const GeanyEditorPrefs *editor_get_prefs(GeanyEditor *editor)
{
static GeanyEditorPrefs eprefs;
const GeanyEditorPrefs *dprefs = get_default_prefs();
/* Return the address of the default prefs to allow returning default and editor
* pref pointers without invalidating the contents of either. */
if (editor == NULL)
return dprefs;
eprefs = *dprefs;
eprefs.indentation = (GeanyIndentPrefs*)editor_get_indent_prefs(editor);
/* add other editor & document overrides as needed */
return &eprefs;
}
void editor_toggle_fold(GeanyEditor *editor, gint line, gint modifiers)
{
ScintillaObject *sci;
gint header;
g_return_if_fail(editor != NULL);
sci = editor->sci;
/* When collapsing a fold range whose starting line is offscreen,
* scroll the starting line to display at the top of the view.
* Otherwise it can be confusing when the document scrolls down to hide
* the folded lines. */
if ((sci_get_fold_level(sci, line) & SC_FOLDLEVELNUMBERMASK) > SC_FOLDLEVELBASE &&
!(sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG))
{
gint parent = sci_get_fold_parent(sci, line);
gint first = sci_get_first_visible_line(sci);
parent = SSM(sci, SCI_VISIBLEFROMDOCLINE, parent, 0);
if (first > parent)
SSM(sci, SCI_SETFIRSTVISIBLELINE, parent, 0);
}
/* find the fold header of the given line in case the one clicked isn't a fold point */
if (sci_get_fold_level(sci, line) & SC_FOLDLEVELHEADERFLAG)
header = line;
else
header = sci_get_fold_parent(sci, line);
if ((editor_prefs.unfold_all_children && ! (modifiers & SCMOD_SHIFT)) ||
(! editor_prefs.unfold_all_children && (modifiers & SCMOD_SHIFT)))
{
SSM(sci, SCI_FOLDCHILDREN, header, SC_FOLDACTION_TOGGLE);
}
else
{
SSM(sci, SCI_FOLDLINE, header, SC_FOLDACTION_TOGGLE);
}
}
static void on_margin_click(GeanyEditor *editor, SCNotification *nt)
{
/* left click to marker margin marks the line */
if (nt->margin == 1)
{
gint line = sci_get_line_from_position(editor->sci, nt->position);
/*sci_marker_delete_all(editor->sci, 1);*/
sci_toggle_marker_at_line(editor->sci, line, 1); /* toggle the marker */
}
/* left click on the folding margin to toggle folding state of current line */
else if (nt->margin == 2 && editor_prefs.folding)
{
gint line = sci_get_line_from_position(editor->sci, nt->position);
editor_toggle_fold(editor, line, nt->modifiers);
}
}
static void on_update_ui(GeanyEditor *editor, G_GNUC_UNUSED SCNotification *nt)
{
ScintillaObject *sci = editor->sci;
gint pos = sci_get_current_position(sci);
/* since Scintilla 2.24, SCN_UPDATEUI is also sent on scrolling though we don't need to handle
* this and so ignore every SCN_UPDATEUI events except for content and selection changes */
if (! (nt->updated & SC_UPDATE_CONTENT) && ! (nt->updated & SC_UPDATE_SELECTION))
return;
/* undo / redo menu update */
ui_update_popup_reundo_items(editor->document);
/* brace highlighting */
editor_highlight_braces(editor, pos);
ui_update_statusbar(editor->document, pos);
#if 0
/** experimental code for inverting selections */
{
gint i;
for (i = SSM(sci, SCI_GETSELECTIONSTART, 0, 0); i < SSM(sci, SCI_GETSELECTIONEND, 0, 0); i++)
{
/* need to get colour from getstyleat(), but how? */
SSM(sci, SCI_STYLESETFORE, STYLE_DEFAULT, 0);
SSM(sci, SCI_STYLESETBACK, STYLE_DEFAULT, 0);
}
sci_get_style_at(sci, pos);
}
#endif
}
static void check_line_breaking(GeanyEditor *editor, gint pos)
{
ScintillaObject *sci = editor->sci;
gint line, lstart, col;
gchar c;
if (!editor->line_breaking)
return;
col = sci_get_col_from_position(sci, pos);
line = sci_get_current_line(sci);
lstart = sci_get_position_from_line(sci, line);
/* use column instead of position which might be different with multibyte characters */
if (col < get_project_pref(line_break_column))
return;
/* look for the last space before line_break_column */
pos = MIN(pos, lstart + get_project_pref(line_break_column));
while (pos > lstart)
{
c = sci_get_char_at(sci, --pos);
if (c == GDK_space)
{
gint diff, last_pos, last_col;
/* remember the distance between the current column and the last column on the line
* (we use column position in case the previous line gets altered, such as removing
* trailing spaces or in case it contains multibyte characters) */
last_pos = sci_get_line_end_position(sci, line);
last_col = sci_get_col_from_position(sci, last_pos);
diff = last_col - col;
/* break the line after the space */
sci_set_current_position(sci, pos + 1, FALSE);
sci_cancel(sci); /* don't select from completion list */
sci_send_command(sci, SCI_NEWLINE);
line++;
/* correct cursor position (might not be at line end) */
last_pos = sci_get_line_end_position(sci, line);
last_col = sci_get_col_from_position(sci, last_pos); /* get last column on line */
/* last column - distance is the desired column, then retrieve its document position */
pos = sci_get_position_from_col(sci, line, last_col - diff);
sci_set_current_position(sci, pos, FALSE);
sci_scroll_caret(sci);
return;
}
}
}
static void show_autocomplete(ScintillaObject *sci, gsize rootlen, GString *words)
{
/* hide autocompletion if only option is already typed */
if (rootlen >= words->len ||
(words->str[rootlen] == '?' && rootlen >= words->len - 2))
{
sci_send_command(sci, SCI_AUTOCCANCEL);
return;
}
/* store whether a calltip is showing, so we can reshow it after autocompletion */
calltip.set = (gboolean) SSM(sci, SCI_CALLTIPACTIVE, 0, 0);
SSM(sci, SCI_AUTOCSHOW, rootlen, (sptr_t) words->str);
}
static void show_tags_list(GeanyEditor *editor, const GPtrArray *tags, gsize rootlen)
{
ScintillaObject *sci = editor->sci;
g_return_if_fail(tags);
if (tags->len > 0)
{
GString *words = g_string_sized_new(150);
guint j;
for (j = 0; j < tags->len; ++j)
{
TMTag *tag = tags->pdata[j];
if (j > 0)
g_string_append_c(words, '\n');
if (j == editor_prefs.autocompletion_max_entries)
{
g_string_append(words, "...");
break;
}
g_string_append(words, tag->name);
/* for now, tag types don't all follow C, so just look at arglist */
if (!EMPTY(tag->arglist))
g_string_append(words, "?2");
else
g_string_append(words, "?1");
}
show_autocomplete(sci, rootlen, words);
g_string_free(words, TRUE);
}
}
/* do not use with long strings */
static gboolean match_last_chars(ScintillaObject *sci, gint pos, const gchar *str)
{
gsize len = strlen(str);
gchar *buf;
g_return_val_if_fail(len < 100, FALSE);
if ((gint)len > pos)
return FALSE;
buf = g_alloca(len + 1);
sci_get_text_range(sci, pos - len, pos, buf);
return strcmp(str, buf) == 0;
}
static gboolean reshow_calltip(gpointer data)
{
GeanyDocument *doc;
g_return_val_if_fail(calltip.sci != NULL, FALSE);
SSM(calltip.sci, SCI_CALLTIPCANCEL, 0, 0);
doc = document_get_current();
if (doc && doc->editor->sci == calltip.sci)
{
/* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
* may be completely wrong in case the user cancelled the auto completion with the mouse */
SSM(calltip.sci, SCI_CALLTIPSHOW, calltip.pos, (sptr_t) calltip.text);
}
return FALSE;
}
static void request_reshowing_calltip(SCNotification *nt)
{
if (calltip.set)
{
/* delay the reshow of the calltip window to make sure it is actually displayed,
* without it might be not visible on SCN_AUTOCCANCEL. the priority is set to
* low to hopefully make Scintilla's events happen before reshowing since they
* seem to re-cancel the calltip on autoc menu hiding too */
g_idle_add_full(G_PRIORITY_LOW, reshow_calltip, NULL, NULL);
}
}
static gboolean autocomplete_scope(GeanyEditor *editor, const gchar *root, gsize rootlen)
{
ScintillaObject *sci = editor->sci;
gint pos = sci_get_current_position(editor->sci);
gchar typed = sci_get_char_at(sci, pos - 1);
gchar brace_char;
gchar *name;
GeanyFiletype *ft = editor->document->file_type;
GPtrArray *tags;
gboolean function = FALSE;
gboolean member;
gboolean scope_sep_typed = FALSE;
gboolean ret = FALSE;
const gchar *current_scope;
const gchar *context_sep = tm_tag_context_separator(ft->lang);
if (autocomplete_scope_shown)
{
/* move at the operator position */
pos -= rootlen;
/* allow for a space between word and operator */
while (pos > 0 && isspace(sci_get_char_at(sci, pos - 1)))
pos--;
if (pos > 0)
typed = sci_get_char_at(sci, pos - 1);
}
/* make sure to keep in sync with similar checks below */
if (match_last_chars(sci, pos, context_sep))
{
pos -= strlen(context_sep);
scope_sep_typed = TRUE;
}
else if (typed == '.')
pos -= 1;
else if ((ft->id == GEANY_FILETYPES_C || ft->id == GEANY_FILETYPES_CPP) &&
match_last_chars(sci, pos, "->"))
pos -= 2;
else if (ft->id == GEANY_FILETYPES_CPP && match_last_chars(sci, pos, "->*"))
pos -= 3;
else
return FALSE;
/* allow for a space between word and operator */
while (pos > 0 && isspace(sci_get_char_at(sci, pos - 1)))
pos--;
/* if function or array index, skip to matching brace */
brace_char = sci_get_char_at(sci, pos - 1);
if (pos > 0 && (brace_char == ')' || brace_char == ']'))
{
gint brace_pos = sci_find_matching_brace(sci, pos - 1);
if (brace_pos != -1)
{
pos = brace_pos;
function = brace_char == ')';
}
/* allow for a space between opening brace and name */
while (pos > 0 && isspace(sci_get_char_at(sci, pos - 1)))
pos--;
}
name = editor_get_word_at_pos(editor, pos, NULL);
if (!name)
return FALSE;
/* check if invoked on member */
pos -= strlen(name);
while (pos > 0 && isspace(sci_get_char_at(sci, pos - 1)))
pos--;
/* make sure to keep in sync with similar checks above */
member = match_last_chars(sci, pos, ".") || match_last_chars(sci, pos, context_sep) ||
match_last_chars(sci, pos, "->") || match_last_chars(sci, pos, "->*");
if (symbols_get_current_scope(editor->document, ¤t_scope) == -1)
current_scope = "";
tags = tm_workspace_find_scope_members(editor->document->tm_file, name, function,
member, current_scope, scope_sep_typed);
if (tags)
{
GPtrArray *filtered = g_ptr_array_new();
TMTag *tag;
guint i;
foreach_ptr_array(tag, i, tags)
{
if (g_str_has_prefix(tag->name, root))
g_ptr_array_add(filtered, tag);
}
if (filtered->len > 0)
{
show_tags_list(editor, filtered, rootlen);
ret = TRUE;
}
g_ptr_array_free(tags, TRUE);
g_ptr_array_free(filtered, TRUE);
}
g_free(name);
return ret;
}
static void on_char_added(GeanyEditor *editor, SCNotification *nt)
{
ScintillaObject *sci = editor->sci;
gint pos = sci_get_current_position(sci);
switch (nt->ch)
{
case '\r':
{ /* simple indentation (only for CR format) */
if (sci_get_eol_mode(sci) == SC_EOL_CR)
on_new_line_added(editor);
break;
}
case '\n':
{ /* simple indentation (for CR/LF and LF format) */
on_new_line_added(editor);
break;
}
case '>':
editor_start_auto_complete(editor, pos, FALSE); /* C/C++ ptr-> scope completion */
/* fall through */
case '/':
{ /* close xml-tags */
handle_xml(editor, pos, nt->ch);
break;
}
case '(':
{
auto_close_chars(sci, pos, nt->ch);
/* show calltips */
editor_show_calltip(editor, --pos);
break;
}
case ')':
{ /* hide calltips */
if (SSM(sci, SCI_CALLTIPACTIVE, 0, 0))
{
SSM(sci, SCI_CALLTIPCANCEL, 0, 0);
}
g_free(calltip.text);
calltip.text = NULL;
calltip.pos = 0;
calltip.sci = NULL;
calltip.set = FALSE;
break;
}
case '{':
case '[':
case '"':
case '\'':
{
auto_close_chars(sci, pos, nt->ch);
break;
}
case '}':
{ /* closing bracket handling */
if (editor->auto_indent)
close_block(editor, pos - 1);
break;
}
/* scope autocompletion */
case '.':
case ':': /* C/C++ class:: syntax */
/* tag autocompletion */
default:
#if 0
if (! editor_start_auto_complete(editor, pos, FALSE))
request_reshowing_calltip(nt);
#else
editor_start_auto_complete(editor, pos, FALSE);
#endif
}
check_line_breaking(editor, pos);
}
/* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
static void expand(ScintillaObject *sci, gint *line, gboolean doExpand,
gboolean force, gint visLevels, gint level)
{
gint lineMaxSubord = SSM(sci, SCI_GETLASTCHILD, *line, level & SC_FOLDLEVELNUMBERMASK);
gint levelLine = level;
(*line)++;
while (*line <= lineMaxSubord)
{
if (force)
{
if (visLevels > 0)
SSM(sci, SCI_SHOWLINES, *line, *line);
else
SSM(sci, SCI_HIDELINES, *line, *line);
}
else
{
if (doExpand)
SSM(sci, SCI_SHOWLINES, *line, *line);
}
if (levelLine == -1)
levelLine = SSM(sci, SCI_GETFOLDLEVEL, *line, 0);
if (levelLine & SC_FOLDLEVELHEADERFLAG)
{
if (force)
{
if (visLevels > 1)
SSM(sci, SCI_SETFOLDEXPANDED, *line, 1);
else
SSM(sci, SCI_SETFOLDEXPANDED, *line, 0);
expand(sci, line, doExpand, force, visLevels - 1, -1);
}
else
{
if (doExpand)
{
if (!sci_get_fold_expanded(sci, *line))
SSM(sci, SCI_SETFOLDEXPANDED, *line, 1);
expand(sci, line, TRUE, force, visLevels - 1, -1);
}
else
{
expand(sci, line, FALSE, force, visLevels - 1, -1);
}
}
}
else
{
(*line)++;
}
}
}
static void fold_changed(ScintillaObject *sci, gint line, gint levelNow, gint levelPrev)
{
if (levelNow & SC_FOLDLEVELHEADERFLAG)
{
if (! (levelPrev & SC_FOLDLEVELHEADERFLAG))
{
/* Adding a fold point */
SSM(sci, SCI_SETFOLDEXPANDED, line, 1);
if (!SSM(sci, SCI_GETALLLINESVISIBLE, 0, 0))
expand(sci, &line, TRUE, FALSE, 0, levelPrev);
}
}
else if (levelPrev & SC_FOLDLEVELHEADERFLAG)
{
if (! sci_get_fold_expanded(sci, line))
{ /* Removing the fold from one that has been contracted so should expand
* otherwise lines are left invisible with no way to make them visible */
SSM(sci, SCI_SETFOLDEXPANDED, line, 1);
if (!SSM(sci, SCI_GETALLLINESVISIBLE, 0, 0))
expand(sci, &line, TRUE, FALSE, 0, levelPrev);
}
}
if (! (levelNow & SC_FOLDLEVELWHITEFLAG) &&
((levelPrev & SC_FOLDLEVELNUMBERMASK) > (levelNow & SC_FOLDLEVELNUMBERMASK)))
{
if (!SSM(sci, SCI_GETALLLINESVISIBLE, 0, 0)) {
/* See if should still be hidden */
gint parentLine = sci_get_fold_parent(sci, line);
if (parentLine < 0)
{
SSM(sci, SCI_SHOWLINES, line, line);
}
else if (sci_get_fold_expanded(sci, parentLine) &&
sci_get_line_is_visible(sci, parentLine))
{
SSM(sci, SCI_SHOWLINES, line, line);
}
}
}
}
static void ensure_range_visible(ScintillaObject *sci, gint posStart, gint posEnd,
gboolean enforcePolicy)
{
gint lineStart = sci_get_line_from_position(sci, MIN(posStart, posEnd));
gint lineEnd = sci_get_line_from_position(sci, MAX(posStart, posEnd));
gint line;
for (line = lineStart; line <= lineEnd; line++)
{
SSM(sci, enforcePolicy ? SCI_ENSUREVISIBLEENFORCEPOLICY : SCI_ENSUREVISIBLE, line, 0);
}
}
static void auto_update_margin_width(GeanyEditor *editor)
{
gint next_linecount = 1;
gint linecount = sci_get_line_count(editor->sci);
GeanyDocument *doc = editor->document;
while (next_linecount <= linecount)
next_linecount *= 10;
if (editor->document->priv->line_count != next_linecount)