2 * editor.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2009-2012 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Editor-related functions for @ref GeanyEditor.
26 * Geany uses the Scintilla editing widget, and this file is mostly built around
27 * Scintilla's functionality.
30 /* Callbacks for the Scintilla widget (ScintillaObject).
31 * Most important is the sci-notify callback, handled in on_editor_notification().
32 * This includes auto-indentation, comments, auto-completion, calltips, etc.
33 * Also some general Scintilla-related functions.
43 #include "callbacks.h"
45 #include "documentprivate.h"
46 #include "filetypesprivate.h"
47 #include "geanyobject.h"
48 #include "highlighting.h"
49 #include "keybindings.h"
52 #include "projectprivate.h"
53 #include "sciwrappers.h"
56 #include "templates.h"
62 #include "gtkcompat.h"
67 #include <gdk/gdkkeysyms.h>
70 /* Note: use sciwrappers.h instead where possible.
71 * Do not use SSM in files unrelated to scintilla. */
72 #define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
74 static GHashTable
*snippet_hash
= NULL
;
75 static GQueue
*snippet_offsets
= NULL
;
76 static gint snippet_cursor_insert_pos
;
77 static GtkAccelGroup
*snippet_accel_group
= NULL
;
78 static gboolean autocomplete_scope_shown
= FALSE
;
80 static const gchar geany_cursor_marker
[] = "__GEANY_CURSOR_MARKER__";
82 /* holds word under the mouse or keyboard cursor */
83 static gchar current_word
[GEANY_MAX_WORD_LENGTH
];
85 /* Initialised in keyfile.c. */
86 GeanyEditorPrefs editor_prefs
;
88 EditorInfo editor_info
= {current_word
, -1};
98 } calltip
= {NULL
, FALSE
, NULL
, 0, 0, NULL
};
100 static gchar indent
[100];
103 static void on_new_line_added(GeanyEditor
*editor
);
104 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
105 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
106 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
107 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
108 static void close_block(GeanyEditor
*editor
, gint pos
);
109 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
110 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
111 const gchar
*wc
, gboolean stem
);
112 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
);
113 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
);
114 static void snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
);
115 static gssize
replace_cursor_markers(GeanyEditor
*editor
, GString
*pattern
);
116 static GeanyFiletype
*editor_get_filetype_at_line(GeanyEditor
*editor
, gint line
);
117 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
);
120 void editor_snippets_free(void)
122 g_hash_table_destroy(snippet_hash
);
123 g_queue_free(snippet_offsets
);
124 gtk_window_remove_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
128 static void snippets_load(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
130 gsize i
, j
, len
= 0, len_keys
= 0;
131 gchar
**groups_user
, **groups_sys
;
132 gchar
**keys_user
, **keys_sys
;
136 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
138 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
140 /* first read all globally defined auto completions */
141 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
142 for (i
= 0; i
< len
; i
++)
144 if (strcmp(groups_sys
[i
], "Keybindings") == 0)
146 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
147 /* create new hash table for the read section (=> filetype) */
148 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
149 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
151 for (j
= 0; j
< len_keys
; j
++)
153 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
154 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
156 g_strfreev(keys_sys
);
158 g_strfreev(groups_sys
);
160 /* now read defined completions in user's configuration directory and add / replace them */
161 groups_user
= g_key_file_get_groups(userconfig
, &len
);
162 for (i
= 0; i
< len
; i
++)
164 if (strcmp(groups_user
[i
], "Keybindings") == 0)
166 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
168 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
170 { /* new key found, create hash table */
171 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
172 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
174 for (j
= 0; j
< len_keys
; j
++)
176 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
178 { /* value = NULL means the key doesn't yet exist, so insert */
179 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
180 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
183 { /* old key and value will be freed by destroy function (g_free) */
184 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
185 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
188 g_strfreev(keys_user
);
190 g_strfreev(groups_user
);
194 static void on_snippet_keybinding_activate(gchar
*key
)
196 GeanyDocument
*doc
= document_get_current();
198 GHashTable
*specials
;
200 if (!doc
|| !gtk_widget_has_focus(GTK_WIDGET(doc
->editor
->sci
)))
203 s
= snippets_find_completion_by_name(doc
->file_type
->name
, key
);
204 if (!s
) /* allow user to specify keybindings for "special" snippets */
206 specials
= g_hash_table_lookup(snippet_hash
, "Special");
207 if (G_LIKELY(specials
!= NULL
))
208 s
= g_hash_table_lookup(specials
, key
);
216 editor_insert_snippet(doc
->editor
, sci_get_current_position(doc
->editor
->sci
), s
);
217 sci_scroll_caret(doc
->editor
->sci
);
221 static void add_kb(GKeyFile
*keyfile
, const gchar
*group
, gchar
**keys
)
227 for (i
= 0; i
< g_strv_length(keys
); i
++)
230 GdkModifierType mods
;
231 gchar
*accel_string
= g_key_file_get_value(keyfile
, group
, keys
[i
], NULL
);
233 gtk_accelerator_parse(accel_string
, &key
, &mods
);
234 g_free(accel_string
);
236 if (key
== 0 && mods
== 0)
238 g_warning("Can not parse accelerator \"%s\" from user snippets.conf", accel_string
);
241 gtk_accel_group_connect(snippet_accel_group
, key
, mods
, 0,
242 g_cclosure_new_swap((GCallback
)on_snippet_keybinding_activate
,
243 g_strdup(keys
[i
]), (GClosureNotify
)g_free
));
248 static void load_kb(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
250 const gchar kb_group
[] = "Keybindings";
251 gchar
**keys
= g_key_file_get_keys(userconfig
, kb_group
, NULL
, NULL
);
254 /* remove overridden keys from system keyfile */
255 foreach_strv(ptr
, keys
)
256 g_key_file_remove_key(sysconfig
, kb_group
, *ptr
, NULL
);
258 add_kb(userconfig
, kb_group
, keys
);
261 keys
= g_key_file_get_keys(sysconfig
, kb_group
, NULL
, NULL
);
262 add_kb(sysconfig
, kb_group
, keys
);
267 void editor_snippets_init(void)
269 gchar
*sysconfigfile
, *userconfigfile
;
270 GKeyFile
*sysconfig
= g_key_file_new();
271 GKeyFile
*userconfig
= g_key_file_new();
273 snippet_offsets
= g_queue_new();
275 sysconfigfile
= g_build_filename(app
->datadir
, "snippets.conf", NULL
);
276 userconfigfile
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
278 /* check for old autocomplete.conf files (backwards compatibility) */
279 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
))
280 SETPTR(userconfigfile
, g_build_filename(app
->configdir
, "autocomplete.conf", NULL
));
282 /* load the actual config files */
283 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
284 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
286 snippets_load(sysconfig
, userconfig
);
288 /* setup snippet keybindings */
289 snippet_accel_group
= gtk_accel_group_new();
290 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
291 load_kb(sysconfig
, userconfig
);
293 g_free(sysconfigfile
);
294 g_free(userconfigfile
);
295 g_key_file_free(sysconfig
);
296 g_key_file_free(userconfig
);
300 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
303 GeanyEditor
*editor
= data
;
304 GeanyDocument
*doc
= editor
->document
;
306 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
307 * fake event to show the editor menu triggered by a key event where we want to use the
308 * text cursor position. */
309 if (event
->x
> 0.0 && event
->y
> 0.0)
310 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
311 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
313 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
315 if (event
->button
== 1)
317 guint state
= keybindings_get_modifiers(event
->state
);
319 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
321 gint ss
= sci_get_selection_start(editor
->sci
);
322 sci_set_selection_end(editor
->sci
, ss
);
324 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GEANY_PRIMARY_MOD_MASK
)
326 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
328 editor_find_current_word(editor
, editor_info
.click_pos
,
329 current_word
, sizeof current_word
, NULL
);
331 return symbols_goto_tag(current_word
, TRUE
);
333 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
336 return document_check_disk_status(doc
, FALSE
);
339 /* calls the edit popup menu in the editor */
340 if (event
->button
== 3)
344 /* ensure the editor widget has the focus after this operation */
345 gtk_widget_grab_focus(widget
);
347 editor_find_current_word(editor
, editor_info
.click_pos
,
348 current_word
, sizeof current_word
, NULL
);
350 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
351 ui_update_popup_goto_items(can_goto
);
352 ui_update_popup_copy_items(doc
);
353 ui_update_insert_include_item(doc
, 0);
355 g_signal_emit_by_name(geany_object
, "update-editor-menu",
356 current_word
, editor_info
.click_pos
, doc
);
358 gtk_menu_popup(GTK_MENU(main_widgets
.editor_menu
),
359 NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
367 static gboolean
is_style_php(gint style
)
369 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
370 style
== SCE_HPHP_COMPLEX_VARIABLE
)
379 static gint
editor_get_long_line_type(void)
382 switch (app
->project
->priv
->long_line_behaviour
)
384 case 0: /* marker disabled */
386 case 1: /* use global settings */
388 case 2: /* custom (enabled) */
389 return editor_prefs
.long_line_type
;
392 if (!editor_prefs
.long_line_enabled
)
395 return editor_prefs
.long_line_type
;
399 static gint
editor_get_long_line_column(void)
401 if (app
->project
&& app
->project
->priv
->long_line_behaviour
!= 1 /* use global settings */)
402 return app
->project
->priv
->long_line_column
;
404 return editor_prefs
.long_line_column
;
408 #define get_project_pref(id)\
409 (app->project ? app->project->priv->id : editor_prefs.id)
411 static const GeanyEditorPrefs
*
412 get_default_prefs(void)
414 static GeanyEditorPrefs eprefs
;
416 eprefs
= editor_prefs
;
418 /* project overrides */
419 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(NULL
);
420 eprefs
.long_line_type
= editor_get_long_line_type();
421 eprefs
.long_line_column
= editor_get_long_line_column();
422 eprefs
.line_wrapping
= get_project_pref(line_wrapping
);
423 eprefs
.line_break_column
= get_project_pref(line_break_column
);
424 eprefs
.auto_continue_multiline
= get_project_pref(auto_continue_multiline
);
429 /* Gets the prefs for the editor.
430 * Prefs can be different according to project or document.
431 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
432 * settings may have changed, or if this function has been called for a different editor.
433 * @param editor The editor, or @c NULL to get the default prefs.
434 * @return The prefs. */
435 const GeanyEditorPrefs
*editor_get_prefs(GeanyEditor
*editor
)
437 static GeanyEditorPrefs eprefs
;
438 const GeanyEditorPrefs
*dprefs
= get_default_prefs();
440 /* Return the address of the default prefs to allow returning default and editor
441 * pref pointers without invalidating the contents of either. */
446 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(editor
);
447 /* add other editor & document overrides as needed */
452 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
454 ScintillaObject
*sci
;
457 g_return_if_fail(editor
!= NULL
);
460 /* When collapsing a fold range whose starting line is offscreen,
461 * scroll the starting line to display at the top of the view.
462 * Otherwise it can be confusing when the document scrolls down to hide
463 * the folded lines. */
464 if ((sci_get_fold_level(sci
, line
) & SC_FOLDLEVELNUMBERMASK
) > SC_FOLDLEVELBASE
&&
465 !(sci_get_fold_level(sci
, line
) & SC_FOLDLEVELHEADERFLAG
))
467 gint parent
= sci_get_fold_parent(sci
, line
);
468 gint first
= sci_get_first_visible_line(sci
);
470 parent
= SSM(sci
, SCI_VISIBLEFROMDOCLINE
, parent
, 0);
472 SSM(sci
, SCI_SETFIRSTVISIBLELINE
, parent
, 0);
475 /* find the fold header of the given line in case the one clicked isn't a fold point */
476 if (sci_get_fold_level(sci
, line
) & SC_FOLDLEVELHEADERFLAG
)
479 header
= sci_get_fold_parent(sci
, line
);
481 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
482 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
484 SSM(sci
, SCI_FOLDCHILDREN
, header
, SC_FOLDACTION_TOGGLE
);
488 SSM(sci
, SCI_FOLDLINE
, header
, SC_FOLDACTION_TOGGLE
);
493 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
495 /* left click to marker margin marks the line */
498 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
500 /*sci_marker_delete_all(editor->sci, 1);*/
501 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
503 /* left click on the folding margin to toggle folding state of current line */
504 else if (nt
->margin
== 2 && editor_prefs
.folding
)
506 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
507 editor_toggle_fold(editor
, line
, nt
->modifiers
);
512 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
514 ScintillaObject
*sci
= editor
->sci
;
515 gint pos
= sci_get_current_position(sci
);
517 /* since Scintilla 2.24, SCN_UPDATEUI is also sent on scrolling though we don't need to handle
518 * this and so ignore every SCN_UPDATEUI events except for content and selection changes */
519 if (! (nt
->updated
& SC_UPDATE_CONTENT
) && ! (nt
->updated
& SC_UPDATE_SELECTION
))
522 /* undo / redo menu update */
523 ui_update_popup_reundo_items(editor
->document
);
525 /* brace highlighting */
526 editor_highlight_braces(editor
, pos
);
528 ui_update_statusbar(editor
->document
, pos
);
531 /** experimental code for inverting selections */
534 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
536 /* need to get colour from getstyleat(), but how? */
537 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
538 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
541 sci_get_style_at(sci
, pos
);
547 static void check_line_breaking(GeanyEditor
*editor
, gint pos
)
549 ScintillaObject
*sci
= editor
->sci
;
550 gint line
, lstart
, col
;
553 if (!editor
->line_breaking
)
556 col
= sci_get_col_from_position(sci
, pos
);
558 line
= sci_get_current_line(sci
);
560 lstart
= sci_get_position_from_line(sci
, line
);
562 /* use column instead of position which might be different with multibyte characters */
563 if (col
< get_project_pref(line_break_column
))
566 /* look for the last space before line_break_column */
567 pos
= MIN(pos
, lstart
+ get_project_pref(line_break_column
));
571 c
= sci_get_char_at(sci
, --pos
);
574 gint diff
, last_pos
, last_col
;
576 /* remember the distance between the current column and the last column on the line
577 * (we use column position in case the previous line gets altered, such as removing
578 * trailing spaces or in case it contains multibyte characters) */
579 last_pos
= sci_get_line_end_position(sci
, line
);
580 last_col
= sci_get_col_from_position(sci
, last_pos
);
581 diff
= last_col
- col
;
583 /* break the line after the space */
584 sci_set_current_position(sci
, pos
+ 1, FALSE
);
585 sci_cancel(sci
); /* don't select from completion list */
586 sci_send_command(sci
, SCI_NEWLINE
);
589 /* correct cursor position (might not be at line end) */
590 last_pos
= sci_get_line_end_position(sci
, line
);
591 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
592 /* last column - distance is the desired column, then retrieve its document position */
593 pos
= sci_get_position_from_col(sci
, line
, last_col
- diff
);
594 sci_set_current_position(sci
, pos
, FALSE
);
595 sci_scroll_caret(sci
);
602 static void show_autocomplete(ScintillaObject
*sci
, gsize rootlen
, GString
*words
)
604 /* hide autocompletion if only option is already typed */
605 if (rootlen
>= words
->len
||
606 (words
->str
[rootlen
] == '?' && rootlen
>= words
->len
- 2))
608 sci_send_command(sci
, SCI_AUTOCCANCEL
);
611 /* store whether a calltip is showing, so we can reshow it after autocompletion */
612 calltip
.set
= (gboolean
) SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
613 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
->str
);
617 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
619 ScintillaObject
*sci
= editor
->sci
;
621 g_return_if_fail(tags
);
625 GString
*words
= g_string_sized_new(150);
628 for (j
= 0; j
< tags
->len
; ++j
)
630 TMTag
*tag
= tags
->pdata
[j
];
633 g_string_append_c(words
, '\n');
635 if (j
== editor_prefs
.autocompletion_max_entries
)
637 g_string_append(words
, "...");
640 g_string_append(words
, tag
->name
);
642 /* for now, tag types don't all follow C, so just look at arglist */
643 if (!EMPTY(tag
->arglist
))
644 g_string_append(words
, "?2");
646 g_string_append(words
, "?1");
648 show_autocomplete(sci
, rootlen
, words
);
649 g_string_free(words
, TRUE
);
654 /* do not use with long strings */
655 static gboolean
match_last_chars(ScintillaObject
*sci
, gint pos
, const gchar
*str
)
657 gsize len
= strlen(str
);
660 g_return_val_if_fail(len
< 100, FALSE
);
665 buf
= g_alloca(len
+ 1);
666 sci_get_text_range(sci
, pos
- len
, pos
, buf
);
667 return strcmp(str
, buf
) == 0;
671 static gboolean
reshow_calltip(gpointer data
)
675 g_return_val_if_fail(calltip
.sci
!= NULL
, FALSE
);
677 SSM(calltip
.sci
, SCI_CALLTIPCANCEL
, 0, 0);
678 doc
= document_get_current();
680 if (doc
&& doc
->editor
->sci
== calltip
.sci
)
682 /* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
683 * may be completely wrong in case the user cancelled the auto completion with the mouse */
684 SSM(calltip
.sci
, SCI_CALLTIPSHOW
, calltip
.pos
, (sptr_t
) calltip
.text
);
690 static void request_reshowing_calltip(SCNotification
*nt
)
694 /* delay the reshow of the calltip window to make sure it is actually displayed,
695 * without it might be not visible on SCN_AUTOCCANCEL. the priority is set to
696 * low to hopefully make Scintilla's events happen before reshowing since they
697 * seem to re-cancel the calltip on autoc menu hiding too */
698 g_idle_add_full(G_PRIORITY_LOW
, reshow_calltip
, NULL
, NULL
);
703 static gboolean
autocomplete_scope(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
705 ScintillaObject
*sci
= editor
->sci
;
706 gint pos
= sci_get_current_position(editor
->sci
);
707 gchar typed
= sci_get_char_at(sci
, pos
- 1);
710 GeanyFiletype
*ft
= editor
->document
->file_type
;
712 gboolean function
= FALSE
;
714 gboolean ret
= FALSE
;
715 const gchar
*current_scope
;
716 const gchar
*context_sep
= tm_tag_context_separator(ft
->lang
);
718 if (autocomplete_scope_shown
)
720 /* move at the operator position */
723 /* allow for a space between word and operator */
724 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
728 typed
= sci_get_char_at(sci
, pos
- 1);
731 /* make sure to keep in sync with similar checks below */
734 else if (match_last_chars(sci
, pos
, context_sep
))
735 pos
-= strlen(context_sep
);
736 else if ((ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
) &&
737 match_last_chars(sci
, pos
, "->"))
739 else if (ft
->id
== GEANY_FILETYPES_CPP
&& match_last_chars(sci
, pos
, "->*"))
744 /* allow for a space between word and operator */
745 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
748 /* if function or array index, skip to matching brace */
749 brace_char
= sci_get_char_at(sci
, pos
- 1);
750 if (pos
> 0 && (brace_char
== ')' || brace_char
== ']'))
752 gint brace_pos
= sci_find_matching_brace(sci
, pos
- 1);
757 function
= brace_char
== ')';
760 /* allow for a space between opening brace and name */
761 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
765 name
= editor_get_word_at_pos(editor
, pos
, NULL
);
769 /* check if invoked on member */
771 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
773 /* make sure to keep in sync with similar checks above */
774 member
= match_last_chars(sci
, pos
, ".") || match_last_chars(sci
, pos
, context_sep
) ||
775 match_last_chars(sci
, pos
, "->") || match_last_chars(sci
, pos
, "->*");
777 if (symbols_get_current_scope(editor
->document
, ¤t_scope
) == -1)
779 tags
= tm_workspace_find_scope_members(editor
->document
->tm_file
, name
, function
,
780 member
, current_scope
);
783 GPtrArray
*filtered
= g_ptr_array_new();
787 foreach_ptr_array(tag
, i
, tags
)
789 if (g_str_has_prefix(tag
->name
, root
))
790 g_ptr_array_add(filtered
, tag
);
793 if (filtered
->len
> 0)
795 show_tags_list(editor
, filtered
, rootlen
);
799 g_ptr_array_free(tags
, TRUE
);
800 g_ptr_array_free(filtered
, TRUE
);
808 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
810 ScintillaObject
*sci
= editor
->sci
;
811 gint pos
= sci_get_current_position(sci
);
816 { /* simple indentation (only for CR format) */
817 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
818 on_new_line_added(editor
);
822 { /* simple indentation (for CR/LF and LF format) */
823 on_new_line_added(editor
);
827 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
830 { /* close xml-tags */
831 handle_xml(editor
, pos
, nt
->ch
);
836 auto_close_chars(sci
, pos
, nt
->ch
);
838 editor_show_calltip(editor
, --pos
);
842 { /* hide calltips */
843 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0))
845 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
847 g_free(calltip
.text
);
859 auto_close_chars(sci
, pos
, nt
->ch
);
863 { /* closing bracket handling */
864 if (editor
->auto_indent
)
865 close_block(editor
, pos
- 1);
868 /* scope autocompletion */
870 case ':': /* C/C++ class:: syntax */
871 /* tag autocompletion */
874 if (! editor_start_auto_complete(editor
, pos
, FALSE
))
875 request_reshowing_calltip(nt
);
877 editor_start_auto_complete(editor
, pos
, FALSE
);
880 check_line_breaking(editor
, pos
);
884 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
885 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
886 gboolean force
, gint visLevels
, gint level
)
888 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
889 gint levelLine
= level
;
891 while (*line
<= lineMaxSubord
)
896 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
898 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
903 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
906 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
907 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
912 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
914 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
915 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
921 if (!sci_get_fold_expanded(sci
, *line
))
922 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
923 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
927 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
939 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
941 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
943 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
945 /* Adding a fold point */
946 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
947 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0))
948 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
951 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
953 if (! sci_get_fold_expanded(sci
, line
))
954 { /* Removing the fold from one that has been contracted so should expand
955 * otherwise lines are left invisible with no way to make them visible */
956 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
957 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0))
958 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
961 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
962 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
964 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0)) {
965 /* See if should still be hidden */
966 gint parentLine
= sci_get_fold_parent(sci
, line
);
969 SSM(sci
, SCI_SHOWLINES
, line
, line
);
971 else if (sci_get_fold_expanded(sci
, parentLine
) &&
972 sci_get_line_is_visible(sci
, parentLine
))
974 SSM(sci
, SCI_SHOWLINES
, line
, line
);
981 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
982 gboolean enforcePolicy
)
984 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
985 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
988 for (line
= lineStart
; line
<= lineEnd
; line
++)
990 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
995 static void auto_update_margin_width(GeanyEditor
*editor
)
997 gint next_linecount
= 1;
998 gint linecount
= sci_get_line_count(editor
->sci
);
999 GeanyDocument
*doc
= editor
->document
;
1001 while (next_linecount
<= linecount
)
1002 next_linecount
*= 10;
1004 if (editor
->document
->priv
->line_count
!= next_linecount
)
1006 doc
->priv
->line_count
= next_linecount
;
1007 sci_set_line_numbers(editor
->sci
, TRUE
);
1012 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
1014 gint pos
= sci_get_current_position(sci
);
1016 sci_insert_text(sci
, pos
, text
);
1017 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
1021 /* Complete the next word part from @a entry */
1022 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
1024 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
1026 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
1027 stem
= current_word
;
1028 if (strstr(text
, stem
) != text
)
1029 return FALSE
; /* shouldn't happen */
1030 if (strlen(text
) <= strlen(stem
))
1033 text
+= strlen(stem
); /* skip stem */
1034 ptr
= strstr(text
+ 1, "_");
1038 partial_complete(editor
->sci
, text
);
1044 foreach_str(ptr
, text
+ 1)
1048 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
1051 partial_complete(editor
->sci
, text
);
1060 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
1061 * Plugins can connect to the "editor-notify" signal. */
1062 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
1063 gpointer scnt
, gpointer data
)
1065 GeanyEditor
*editor
= data
;
1068 g_return_if_fail(editor
!= NULL
);
1070 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
1074 static gboolean
on_editor_notify(G_GNUC_UNUSED GObject
*object
, GeanyEditor
*editor
,
1075 SCNotification
*nt
, G_GNUC_UNUSED gpointer data
)
1077 ScintillaObject
*sci
= editor
->sci
;
1078 GeanyDocument
*doc
= editor
->document
;
1080 switch (nt
->nmhdr
.code
)
1082 case SCN_SAVEPOINTLEFT
:
1083 document_set_text_changed(doc
, TRUE
);
1086 case SCN_SAVEPOINTREACHED
:
1087 document_set_text_changed(doc
, FALSE
);
1090 case SCN_MODIFYATTEMPTRO
:
1094 case SCN_MARGINCLICK
:
1095 on_margin_click(editor
, nt
);
1099 on_update_ui(editor
, nt
);
1103 /* Visible lines are only laid out accurately just before painting,
1104 * so we need to only call editor_scroll_to_line here, because the document
1105 * may have line wrapping and folding enabled.
1106 * http://scintilla.sourceforge.net/ScintillaDoc.html#LineWrapping
1107 * This is important e.g. when loading a session and switching pages
1108 * and having the cursor scroll in view. */
1109 /* FIXME: Really we want to do this just before painting, not after it
1110 * as it will cause repainting. */
1111 if (editor
->scroll_percent
> 0.0F
)
1113 editor_scroll_to_line(editor
, -1, editor
->scroll_percent
);
1114 /* disable further scrolling */
1115 editor
->scroll_percent
= -1.0F
;
1120 if (editor_prefs
.show_linenumber_margin
&& (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) && nt
->linesAdded
)
1122 /* automatically adjust Scintilla's line numbers margin width */
1123 auto_update_margin_width(editor
);
1125 if (nt
->modificationType
& SC_STARTACTION
&& ! ignore_callback
)
1127 /* get notified about undo changes */
1128 document_undo_add(doc
, UNDO_SCINTILLA
, NULL
);
1130 if (editor_prefs
.folding
&& (nt
->modificationType
& SC_MOD_CHANGEFOLD
) != 0)
1132 /* handle special fold cases, e.g. #1923350 */
1133 fold_changed(sci
, nt
->line
, nt
->foldLevelNow
, nt
->foldLevelPrev
);
1135 if (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
))
1137 document_update_tag_list_in_idle(doc
);
1142 on_char_added(editor
, nt
);
1145 case SCN_USERLISTSELECTION
:
1146 if (nt
->listType
== 1)
1148 sci_add_text(sci
, nt
->text
);
1152 case SCN_AUTOCSELECTION
:
1153 if (g_str_equal(nt
->text
, "..."))
1160 case SCN_AUTOCCANCELLED
:
1161 /* now that autocomplete is finishing or was cancelled, reshow calltips
1162 * if they were showing */
1163 autocomplete_scope_shown
= FALSE
;
1164 request_reshowing_calltip(nt
);
1167 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
1170 case SCN_URIDROPPED
:
1171 if (nt
->text
!= NULL
)
1173 document_open_file_list(nt
->text
, strlen(nt
->text
));
1177 case SCN_CALLTIPCLICK
:
1178 if (nt
->position
> 0)
1180 switch (nt
->position
)
1182 case 1: /* up arrow */
1183 if (calltip
.tag_index
> 0)
1184 calltip
.tag_index
--;
1187 case 2: calltip
.tag_index
++; break; /* down arrow */
1189 editor_show_calltip(editor
, -1);
1194 /* recalculate line margin width */
1195 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
);
1198 /* we always return FALSE here to let plugins handle the event too */
1203 /* Note: this is the same as sci_get_tab_width(), but is still useful when you don't have
1204 * a scintilla pointer. */
1205 static gint
get_tab_width(const GeanyIndentPrefs
*indent_prefs
)
1207 if (indent_prefs
->type
== GEANY_INDENT_TYPE_BOTH
)
1208 return indent_prefs
->hard_tab_width
;
1210 return indent_prefs
->width
; /* tab width = indent width */
1214 /* Returns a string containing width chars of whitespace, filled with simple space
1215 * characters or with the right number of tab characters, according to the indent prefs.
1216 * (Result is filled with tabs *and* spaces if width isn't a multiple of
1217 * the tab width). */
1219 get_whitespace(const GeanyIndentPrefs
*iprefs
, gint width
)
1221 g_return_val_if_fail(width
>= 0, NULL
);
1224 return g_strdup("");
1226 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
1228 return g_strnfill(width
, ' ');
1231 { /* first fill text with tabs and fill the rest with spaces */
1232 const gint tab_width
= get_tab_width(iprefs
);
1233 gint tabs
= width
/ tab_width
;
1234 gint spaces
= width
% tab_width
;
1235 gint len
= tabs
+ spaces
;
1238 str
= g_malloc(len
+ 1);
1240 memset(str
, '\t', tabs
);
1241 memset(str
+ tabs
, ' ', spaces
);
1248 static const GeanyIndentPrefs
*
1249 get_default_indent_prefs(void)
1251 static GeanyIndentPrefs iprefs
;
1253 iprefs
= app
->project
? *app
->project
->priv
->indentation
: *editor_prefs
.indentation
;
1258 /** Gets the indentation prefs for the editor.
1259 * Prefs can be different according to project or document.
1260 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
1261 * settings may have changed, or if this function has been called for a different editor.
1262 * @param editor The editor, or @c NULL to get the default indent prefs.
1263 * @return The indent prefs. */
1265 const GeanyIndentPrefs
*
1266 editor_get_indent_prefs(GeanyEditor
*editor
)
1268 static GeanyIndentPrefs iprefs
;
1269 const GeanyIndentPrefs
*dprefs
= get_default_indent_prefs();
1271 /* Return the address of the default prefs to allow returning default and editor
1272 * pref pointers without invalidating the contents of either. */
1277 iprefs
.type
= editor
->indent_type
;
1278 iprefs
.width
= editor
->indent_width
;
1280 /* if per-document auto-indent is enabled, but we don't have a global mode set,
1281 * just use basic auto-indenting */
1282 if (editor
->auto_indent
&& iprefs
.auto_indent_mode
== GEANY_AUTOINDENT_NONE
)
1283 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_BASIC
;
1285 if (!editor
->auto_indent
)
1286 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_NONE
;
1292 static void on_new_line_added(GeanyEditor
*editor
)
1294 ScintillaObject
*sci
= editor
->sci
;
1295 gint line
= sci_get_current_line(sci
);
1297 /* simple indentation */
1298 if (editor
->auto_indent
)
1300 insert_indent_after_line(editor
, line
- 1);
1303 if (get_project_pref(auto_continue_multiline
))
1304 { /* " * " auto completion in multiline C/C++/D/Java comments */
1305 auto_multiline(editor
, line
);
1308 if (editor_prefs
.newline_strip
)
1309 { /* strip the trailing spaces on the previous line */
1310 editor_strip_line_trailing_spaces(editor
, line
- 1);
1315 static gboolean
lexer_has_braces(ScintillaObject
*sci
)
1317 gint lexer
= sci_get_lexer(sci
);
1323 case SCLEX_HTML
: /* for PHP & JS */
1324 case SCLEX_PHPSCRIPT
:
1325 case SCLEX_PASCAL
: /* for multiline comments? */
1338 /* Read indent chars for the line that pos is on into indent global variable.
1339 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1340 * instead in any new code. */
1341 static void read_indent(GeanyEditor
*editor
, gint pos
)
1343 ScintillaObject
*sci
= editor
->sci
;
1344 guint i
, len
, j
= 0;
1348 line
= sci_get_line_from_position(sci
, pos
);
1350 len
= sci_get_line_length(sci
, line
);
1351 linebuf
= sci_get_line(sci
, line
);
1353 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1355 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1356 indent
[j
++] = linebuf
[i
];
1365 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1367 gint start
= sci_get_position_from_line(sci
, line
);
1368 gint end
= sci_get_line_end_position(sci
, line
) - 1;
1369 gint lexer
= sci_get_lexer(sci
);
1373 for (pos
= end
; pos
>= start
&& count
< 1; pos
--)
1375 if (highlighting_is_code_style(lexer
, sci_get_style_at(sci
, pos
)))
1377 gchar c
= sci_get_char_at(sci
, pos
);
1386 return count
> 0 ? 1 : 0;
1390 /* gets the last code position on a line
1391 * warning: if there is no code position on the line, returns the start position */
1392 static gint
get_sci_line_code_end_position(ScintillaObject
*sci
, gint line
)
1394 gint start
= sci_get_position_from_line(sci
, line
);
1395 gint lexer
= sci_get_lexer(sci
);
1398 for (pos
= sci_get_line_end_position(sci
, line
) - 1; pos
> start
; pos
--)
1400 gint style
= sci_get_style_at(sci
, pos
);
1402 if (highlighting_is_code_style(lexer
, style
) && ! isspace(sci_get_char_at(sci
, pos
)))
1410 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1412 gint last_char
= get_sci_line_code_end_position(sci
, line
);
1414 /* add extra indentation for Python after colon */
1415 if (sci_get_char_at(sci
, last_char
) == ':' &&
1416 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1424 static gint
get_xml_indent(ScintillaObject
*sci
, gint line
)
1426 gboolean need_close
= FALSE
;
1427 gint end
= get_sci_line_code_end_position(sci
, line
);
1430 /* don't indent if there's a closing tag to the right of the cursor */
1431 pos
= sci_get_current_position(sci
);
1432 if (sci_get_char_at(sci
, pos
) == '<' &&
1433 sci_get_char_at(sci
, pos
+ 1) == '/')
1436 if (sci_get_char_at(sci
, end
) == '>' &&
1437 sci_get_char_at(sci
, end
- 1) != '/')
1439 gint style
= sci_get_style_at(sci
, end
);
1441 if (style
== SCE_H_TAG
|| style
== SCE_H_TAGUNKNOWN
)
1443 gint start
= sci_get_position_from_line(sci
, line
);
1444 gchar
*line_contents
= sci_get_contents_range(sci
, start
, end
+ 1);
1445 gchar
*opened_tag_name
= utils_find_open_xml_tag(line_contents
, end
+ 1 - start
);
1447 if (!EMPTY(opened_tag_name
))
1450 if (sci_get_lexer(sci
) == SCLEX_HTML
&& utils_is_short_html_tag(opened_tag_name
))
1453 g_free(line_contents
);
1454 g_free(opened_tag_name
);
1458 return need_close
? 1 : 0;
1462 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1464 ScintillaObject
*sci
= editor
->sci
;
1466 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1468 g_return_val_if_fail(line
>= 0, 0);
1470 size
= sci_get_line_indentation(sci
, line
);
1472 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1474 gint additional_indent
= 0;
1476 if (lexer_has_braces(sci
))
1477 additional_indent
= iprefs
->width
* get_brace_indent(sci
, line
);
1478 else if (sci_get_lexer(sci
) == SCLEX_PYTHON
) /* Python/Cython */
1479 additional_indent
= iprefs
->width
* get_python_indent(sci
, line
);
1481 /* HTML lexer "has braces" because of PHP and JavaScript. If get_brace_indent() did not
1482 * recommend us to insert additional indent, we are probably not in PHP/JavaScript chunk and
1483 * should make the XML-related check */
1484 if (additional_indent
== 0 &&
1485 (sci_get_lexer(sci
) == SCLEX_HTML
||
1486 sci_get_lexer(sci
) == SCLEX_XML
) &&
1487 editor
->document
->file_type
->priv
->xml_indent_tags
)
1489 size
+= iprefs
->width
* get_xml_indent(sci
, line
);
1492 size
+= additional_indent
;
1498 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
)
1500 ScintillaObject
*sci
= editor
->sci
;
1501 gint line_indent
= sci_get_line_indentation(sci
, line
);
1502 gint size
= get_indent_size_after_line(editor
, line
);
1503 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1509 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
&& size
== line_indent
)
1511 /* support tab indents, space aligns style - copy last line 'indent' exactly */
1512 gint start
= sci_get_position_from_line(sci
, line
);
1513 gint end
= sci_get_line_indent_position(sci
, line
);
1515 text
= sci_get_contents_range(sci
, start
, end
);
1519 text
= get_whitespace(iprefs
, size
);
1521 sci_add_text(sci
, text
);
1526 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
)
1528 const gchar
*closing_char
= NULL
;
1531 if (utils_isbrace(c
, 0))
1532 end_pos
= sci_find_matching_brace(sci
, pos
- 1);
1537 if ((editor_prefs
.autoclose_chars
& GEANY_AC_PARENTHESIS
) && end_pos
== -1)
1541 if ((editor_prefs
.autoclose_chars
& GEANY_AC_CBRACKET
) && end_pos
== -1)
1545 if ((editor_prefs
.autoclose_chars
& GEANY_AC_SBRACKET
) && end_pos
== -1)
1549 if (editor_prefs
.autoclose_chars
& GEANY_AC_SQUOTE
)
1553 if (editor_prefs
.autoclose_chars
& GEANY_AC_DQUOTE
)
1554 closing_char
= "\"";
1558 if (closing_char
!= NULL
)
1560 sci_add_text(sci
, closing_char
);
1561 sci_set_current_position(sci
, pos
, TRUE
);
1566 /* Finds a corresponding matching brace to the given pos
1567 * (this is taken from Scintilla Editor.cxx,
1568 * fit to work with close_block) */
1569 static gint
brace_match(ScintillaObject
*sci
, gint pos
)
1571 gchar chBrace
= sci_get_char_at(sci
, pos
);
1572 gchar chSeek
= utils_brace_opposite(chBrace
);
1574 gint direction
= -1;
1579 /* Hack: we need the style at @p pos but it isn't computed yet, so force styling
1580 * of this very position */
1581 sci_colourise(sci
, pos
, pos
+ 1);
1583 styBrace
= sci_get_style_at(sci
, pos
);
1585 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1589 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1591 chAtPos
= sci_get_char_at(sci
, pos
);
1592 styAtPos
= sci_get_style_at(sci
, pos
);
1594 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1596 if (chAtPos
== chBrace
)
1598 if (chAtPos
== chSeek
)
1609 /* Called after typing '}'. */
1610 static void close_block(GeanyEditor
*editor
, gint pos
)
1612 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1613 gint x
= 0, cnt
= 0;
1614 gint line
, line_len
;
1615 gchar
*text
, *line_buf
;
1616 ScintillaObject
*sci
;
1617 gint line_indent
, last_indent
;
1619 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1621 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1625 if (! lexer_has_braces(sci
))
1628 line
= sci_get_line_from_position(sci
, pos
);
1629 line_len
= sci_get_line_end_position(sci
, line
) - sci_get_position_from_line(sci
, line
);
1631 /* check that the line is empty, to not kill text in the line */
1632 line_buf
= sci_get_line(sci
, line
);
1633 line_buf
[line_len
] = '\0';
1634 while (x
< line_len
)
1636 if (isspace(line_buf
[x
]))
1642 if ((line_len
- 1) != cnt
)
1645 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1647 gint start_brace
= brace_match(sci
, pos
);
1649 if (start_brace
>= 0)
1652 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1653 gint size
= sci_get_line_indentation(sci
, brace_line
);
1654 gchar
*ind
= get_whitespace(iprefs
, size
);
1656 text
= g_strconcat(ind
, "}", NULL
);
1657 line_start
= sci_get_position_from_line(sci
, line
);
1658 sci_set_anchor(sci
, line_start
);
1659 sci_replace_sel(sci
, text
);
1664 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1667 /* GEANY_AUTOINDENT_CURRENTCHARS */
1668 line_indent
= sci_get_line_indentation(sci
, line
);
1669 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1671 if (line_indent
< last_indent
)
1673 line_indent
-= iprefs
->width
;
1674 line_indent
= MAX(0, line_indent
);
1675 sci_set_line_indentation(sci
, line
, line_indent
);
1679 /* checks whether @p c is an ASCII character (e.g. < 0x80) */
1680 #define IS_ASCII(c) (((unsigned char)(c)) < 0x80)
1683 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1684 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1685 * position can be -1, then the current position is used.
1686 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1687 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
1688 const gchar
*wc
, gboolean stem
)
1690 gint line
, line_start
, startword
, endword
;
1692 ScintillaObject
*sci
;
1694 g_return_if_fail(editor
!= NULL
);
1698 pos
= sci_get_current_position(sci
);
1700 line
= sci_get_line_from_position(sci
, pos
);
1701 line_start
= sci_get_position_from_line(sci
, line
);
1702 startword
= pos
- line_start
;
1703 endword
= pos
- line_start
;
1706 chunk
= sci_get_line(sci
, line
);
1709 wc
= GEANY_WORDCHARS
;
1711 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1712 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1713 * TODO: improve this code */
1714 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || ! IS_ASCII(chunk
[startword
- 1])))
1718 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || ! IS_ASCII(chunk
[endword
])))
1722 if (startword
!= endword
)
1724 chunk
[endword
] = '\0';
1726 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1729 g_strlcpy(word
, "", wordlen
);
1735 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1736 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1737 * position can be -1, then the current position is used.
1738 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1739 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
1742 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1746 /* Same as editor_find_current_word() but uses editor's word boundaries to decide what the word
1747 * is. This should be used e.g. to get the word to search for */
1748 void editor_find_current_word_sciwc(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
)
1753 g_return_if_fail(editor
!= NULL
);
1756 pos
= sci_get_current_position(editor
->sci
);
1758 start
= sci_word_start_position(editor
->sci
, pos
, TRUE
);
1759 end
= sci_word_end_position(editor
->sci
, pos
, TRUE
);
1761 if (start
== end
) /* caret in whitespaces sequence */
1765 if ((guint
)(end
- start
) >= wordlen
)
1766 end
= start
+ (wordlen
- 1);
1767 sci_get_text_range(editor
->sci
, start
, end
, word
);
1773 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1774 * Otherwise NULL is returned.
1775 * Additional wordchars can be specified to define what to consider as a word.
1777 * @param editor The editor to operate on.
1778 * @param pos The position where the word should be read from.
1779 * May be @c -1 to use the current position.
1780 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1781 * as part of a word. May be @c NULL to use the default wordchars,
1782 * see @ref GEANY_WORDCHARS.
1784 * @return A newly-allocated string containing the word at the given @a pos or @c NULL.
1785 * Should be freed when no longer needed.
1790 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1792 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1794 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1796 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1798 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1802 /* Read the word up to position @a pos. */
1803 static const gchar
*
1804 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1806 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1808 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1810 return (*word
) ? word
: NULL
;
1814 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1816 gint orig_pos
= pos
;
1818 while (pos
>= 0 && pos
> orig_pos
- 300)
1820 gchar c
= sci_get_char_at(sci
, pos
);
1821 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1829 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1832 gint orig_pos
= pos
;
1834 while (pos
> 0 && pos
> orig_pos
- 300)
1836 gchar c
= sci_get_char_at(sci
, pos
);
1838 if (c
== ')') brackets
++;
1839 else if (c
== '(') brackets
--;
1840 if (brackets
< 0) return pos
; /* found start bracket */
1847 static gboolean
append_calltip(GString
*str
, const TMTag
*tag
, GeanyFiletypeID ft_id
)
1852 if (ft_id
!= GEANY_FILETYPES_PASCAL
&& ft_id
!= GEANY_FILETYPES_GO
)
1853 { /* usual calltips: "retval tagname (arglist)" */
1858 g_string_append(str
, tag
->var_type
);
1859 for (i
= 0; i
< tag
->pointerOrder
; i
++)
1861 g_string_append_c(str
, '*');
1863 g_string_append_c(str
, ' ');
1867 const gchar
*cosep
= symbols_get_context_separator(ft_id
);
1869 g_string_append(str
, tag
->scope
);
1870 g_string_append(str
, cosep
);
1872 g_string_append(str
, tag
->name
);
1873 g_string_append_c(str
, ' ');
1874 g_string_append(str
, tag
->arglist
);
1877 { /* special case Pascal/Go calltips: "tagname (arglist) : retval"
1878 * (with ':' omitted for Go) */
1879 g_string_append(str
, tag
->name
);
1880 g_string_append_c(str
, ' ');
1881 g_string_append(str
, tag
->arglist
);
1883 if (!EMPTY(tag
->var_type
))
1885 g_string_append(str
, ft_id
== GEANY_FILETYPES_PASCAL
? " : " : " ");
1886 g_string_append(str
, tag
->var_type
);
1894 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1897 const TMTagType arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1898 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1900 GString
*str
= NULL
;
1903 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1905 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1906 tags
= tm_workspace_find(word
, NULL
, tm_tag_max_t
, NULL
, ft
->lang
);
1909 g_ptr_array_free(tags
, TRUE
);
1913 tag
= TM_TAG(tags
->pdata
[0]);
1915 if (ft
->id
== GEANY_FILETYPES_D
&&
1916 (tag
->type
== tm_tag_class_t
|| tag
->type
== tm_tag_struct_t
))
1918 g_ptr_array_free(tags
, TRUE
);
1919 /* user typed e.g. 'new Classname(' so lookup D constructor Classname::this() */
1920 tags
= tm_workspace_find("this", tag
->name
, arg_types
, NULL
, ft
->lang
);
1923 g_ptr_array_free(tags
, TRUE
);
1928 /* remove tags with no argument list */
1929 for (i
= 0; i
< tags
->len
; i
++)
1931 tag
= TM_TAG(tags
->pdata
[i
]);
1934 tags
->pdata
[i
] = NULL
;
1936 tm_tags_prune((GPtrArray
*) tags
);
1939 g_ptr_array_free(tags
, TRUE
);
1943 { /* remove duplicate calltips */
1944 TMTagAttrType sort_attr
[] = {tm_tag_attr_name_t
, tm_tag_attr_scope_t
,
1945 tm_tag_attr_arglist_t
, 0};
1947 tm_tags_sort((GPtrArray
*) tags
, sort_attr
, TRUE
, FALSE
);
1950 /* if the current word has changed since last time, start with the first tag match */
1951 if (! utils_str_equal(word
, calltip
.last_word
))
1952 calltip
.tag_index
= 0;
1953 /* cache the current word for next time */
1954 g_free(calltip
.last_word
);
1955 calltip
.last_word
= g_strdup(word
);
1956 calltip
.tag_index
= MIN(calltip
.tag_index
, tags
->len
- 1); /* ensure tag_index is in range */
1958 for (i
= calltip
.tag_index
; i
< tags
->len
; i
++)
1960 tag
= TM_TAG(tags
->pdata
[i
]);
1964 str
= g_string_new(NULL
);
1965 if (calltip
.tag_index
> 0)
1966 g_string_prepend(str
, "\001 "); /* up arrow */
1967 append_calltip(str
, tag
, FILETYPE_ID(ft
));
1969 else /* add a down arrow */
1971 if (calltip
.tag_index
> 0) /* already have an up arrow */
1972 g_string_insert_c(str
, 1, '\002');
1974 g_string_prepend(str
, "\002 ");
1979 g_ptr_array_free(tags
, TRUE
);
1983 gchar
*result
= str
->str
;
1985 g_string_free(str
, FALSE
);
1992 /* use pos = -1 to search for the previous unmatched open bracket. */
1993 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
1995 gint orig_pos
= pos
; /* the position for the calltip */
1998 gchar word
[GEANY_MAX_WORD_LENGTH
];
2000 ScintillaObject
*sci
;
2002 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2003 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
2007 lexer
= sci_get_lexer(sci
);
2011 /* position of '(' is unknown, so go backwards from current position to find it */
2012 pos
= sci_get_current_position(sci
);
2015 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
2016 find_start_bracket(sci
, pos
);
2021 /* the style 1 before the brace (which may be highlighted) */
2022 style
= sci_get_style_at(sci
, pos
- 1);
2023 if (! highlighting_is_code_style(lexer
, style
))
2027 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
2028 if (word
[0] == '\0')
2031 str
= find_calltip(word
, editor
->document
->file_type
);
2034 g_free(calltip
.text
); /* free the old calltip */
2036 calltip
.pos
= orig_pos
;
2039 utils_wrap_string(calltip
.text
, -1);
2040 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
2047 gchar
*editor_get_calltip_text(GeanyEditor
*editor
, const TMTag
*tag
)
2051 g_return_val_if_fail(editor
!= NULL
, NULL
);
2053 str
= g_string_new(NULL
);
2054 if (append_calltip(str
, tag
, editor
->document
->file_type
->id
))
2055 return g_string_free(str
, FALSE
);
2057 return g_string_free(str
, TRUE
);
2061 /* HTML entities auto completion from html_entities.tags text file */
2063 autocomplete_html(ScintillaObject
*sci
, const gchar
*root
, gsize rootlen
)
2066 gboolean found
= FALSE
;
2068 const gchar
**entities
= symbols_get_html_entities();
2070 if (*root
!= '&' || entities
== NULL
)
2073 words
= g_string_sized_new(500);
2076 if (entities
[i
] == NULL
)
2078 else if (entities
[i
][0] == '#')
2081 if (! strncmp(entities
[i
], root
, rootlen
))
2084 g_string_append_c(words
, '\n');
2086 g_string_append(words
, entities
[i
]);
2091 show_autocomplete(sci
, rootlen
, words
);
2093 g_string_free(words
, TRUE
);
2098 /* Current document & global tags autocompletion */
2100 autocomplete_tags(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
2106 g_return_val_if_fail(editor
, FALSE
);
2108 doc
= editor
->document
;
2110 tags
= tm_workspace_find_prefix(root
, doc
->file_type
->lang
, editor_prefs
.autocompletion_max_entries
);
2111 found
= tags
->len
> 0;
2113 show_tags_list(editor
, tags
, rootlen
);
2114 g_ptr_array_free(tags
, TRUE
);
2120 static gboolean
autocomplete_check_html(GeanyEditor
*editor
, gint style
, gint pos
)
2122 GeanyFiletype
*ft
= editor
->document
->file_type
;
2123 gboolean
try = FALSE
;
2125 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
2126 * (everything after SCE_HJ_START is for embedded scripting languages) */
2127 if (ft
->id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
2129 else if (sci_get_lexer(editor
->sci
) == SCLEX_XML
&& style
< SCE_HJ_START
)
2131 else if (ft
->id
== GEANY_FILETYPES_PHP
)
2133 /* use entity completion when style is outside of PHP styles */
2134 if (! is_style_php(style
))
2139 gchar root
[GEANY_MAX_WORD_LENGTH
];
2142 read_current_word(editor
, pos
, root
, sizeof(root
), GEANY_WORDCHARS
"&", TRUE
);
2144 /* Allow something like ""some text"".
2145 * for entity completion we want to have completion for '&' within words. */
2146 tmp
= strchr(root
, '&');
2149 return autocomplete_html(editor
->sci
, tmp
, strlen(tmp
));
2156 /* Algorithm based on based on Scite's StartAutoCompleteWord()
2157 * @returns a sorted list of words matching @p root */
2158 static GSList
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
2161 gint len
, current
, word_end
;
2162 gint pos_find
, flags
;
2165 GSList
*words
= NULL
;
2166 struct Sci_TextToFind ttf
;
2168 len
= sci_get_length(sci
);
2169 current
= sci_get_current_position(sci
) - rootlen
;
2171 ttf
.lpstrText
= root
;
2173 ttf
.chrg
.cpMax
= len
;
2174 ttf
.chrgText
.cpMin
= 0;
2175 ttf
.chrgText
.cpMax
= 0;
2176 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
2178 /* search the whole document for the word root and collect results */
2179 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2180 while (pos_find
>= 0 && pos_find
< len
)
2182 word_end
= pos_find
+ rootlen
;
2183 if (pos_find
!= current
)
2185 word_end
= sci_word_end_position(sci
, word_end
, TRUE
);
2187 word_length
= word_end
- pos_find
;
2188 if (word_length
> rootlen
)
2190 word
= sci_get_contents_range(sci
, pos_find
, word_end
);
2191 /* search whether we already have the word in, otherwise add it */
2192 if (g_slist_find_custom(words
, word
, (GCompareFunc
)strcmp
) != NULL
)
2196 words
= g_slist_prepend(words
, word
);
2200 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
2204 ttf
.chrg
.cpMin
= word_end
;
2205 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2208 return g_slist_sort(words
, (GCompareFunc
)utils_str_casecmp
);
2212 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
2214 ScintillaObject
*sci
= editor
->sci
;
2215 GSList
*words
, *node
;
2219 words
= get_doc_words(sci
, root
, rootlen
);
2222 scintilla_send_message(sci
, SCI_AUTOCCANCEL
, 0, 0);
2226 str
= g_string_sized_new(rootlen
* 2 * 10);
2227 foreach_slist(node
, words
)
2229 g_string_append(str
, node
->data
);
2232 g_string_append_c(str
, '\n');
2235 if (n_words
>= editor_prefs
.autocompletion_max_entries
)
2236 g_string_append(str
, "\n...");
2238 g_slist_free(words
);
2240 show_autocomplete(sci
, rootlen
, str
);
2241 g_string_free(str
, TRUE
);
2246 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
2248 gint rootlen
, lexer
, style
;
2250 gchar cword
[GEANY_MAX_WORD_LENGTH
];
2251 ScintillaObject
*sci
;
2252 gboolean ret
= FALSE
;
2253 const gchar
*wordchars
;
2256 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2258 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
2261 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
2262 * necessary styling information */
2263 if (G_UNLIKELY(pos
< 2))
2267 ft
= editor
->document
->file_type
;
2269 lexer
= sci_get_lexer(sci
);
2270 style
= sci_get_style_at(sci
, pos
- 2);
2272 /* don't autocomplete in comments and strings */
2273 if (!force
&& !highlighting_is_code_style(lexer
, style
))
2276 ret
= autocomplete_check_html(editor
, style
, pos
);
2278 if (ft
->id
== GEANY_FILETYPES_LATEX
)
2279 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
2280 else if (ft
->id
== GEANY_FILETYPES_CSS
)
2281 wordchars
= GEANY_WORDCHARS
"-"; /* add - because they are part of property names */
2283 wordchars
= GEANY_WORDCHARS
;
2285 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, TRUE
);
2287 rootlen
= strlen(root
);
2291 if (autocomplete_scope_shown
)
2293 autocomplete_scope_shown
= FALSE
;
2295 sci_send_command(sci
, SCI_AUTOCCANCEL
);
2300 ret
= autocomplete_scope(editor
, root
, rootlen
);
2301 if (!ret
&& autocomplete_scope_shown
)
2302 sci_send_command(sci
, SCI_AUTOCCANCEL
);
2303 autocomplete_scope_shown
= ret
;
2306 if (!ret
&& rootlen
> 0)
2308 if (ft
->id
== GEANY_FILETYPES_PHP
&& style
== SCE_HPHP_DEFAULT
&&
2309 rootlen
== 3 && strcmp(root
, "php") == 0 && pos
>= 5 &&
2310 sci_get_char_at(sci
, pos
- 5) == '<' &&
2311 sci_get_char_at(sci
, pos
- 4) == '?')
2313 /* nothing, don't complete PHP open tags */
2317 /* force is set when called by keyboard shortcut, otherwise start at the
2318 * editor_prefs.symbolcompletion_min_chars'th char */
2319 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
2321 /* complete tags, except if forcing when completion is already visible */
2322 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
2323 ret
= autocomplete_tags(editor
, root
, rootlen
);
2325 /* If forcing and there's nothing else to show, complete from words in document */
2326 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2327 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2338 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2340 gchar
*result
= NULL
;
2343 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2345 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2348 result
= g_hash_table_lookup(tmp
, name
);
2350 /* whether nothing is set for the current filetype(tmp is NULL) or
2351 * the particular completion for this filetype is not set (result is NULL) */
2352 if (tmp
== NULL
|| result
== NULL
)
2354 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2357 result
= g_hash_table_lookup(tmp
, name
);
2360 /* if result is still NULL here, no completion could be found */
2362 /* result is owned by the hash table and will be freed when the table will destroyed */
2367 static void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2370 GString
*pattern
= user_data
;
2372 g_return_if_fail(key
!= NULL
);
2373 g_return_if_fail(value
!= NULL
);
2375 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2377 utils_string_replace_all(pattern
, needle
, (gchar
*) value
);
2382 static void fix_indentation(GeanyEditor
*editor
, GString
*buf
)
2384 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2387 gint cflags
= G_REGEX_MULTILINE
;
2389 /* transform leading tabs into indent widths (in spaces) */
2390 whitespace
= g_strnfill(iprefs
->width
, ' ');
2391 regex
= g_regex_new("^ *(\t)", cflags
, 0, NULL
);
2392 while (utils_string_regex_replace_all(buf
, regex
, 1, whitespace
, TRUE
));
2393 g_regex_unref(regex
);
2395 /* remaining tabs are for alignment */
2396 if (iprefs
->type
!= GEANY_INDENT_TYPE_TABS
)
2397 utils_string_replace_all(buf
, "\t", whitespace
);
2399 /* use leading tabs */
2400 if (iprefs
->type
!= GEANY_INDENT_TYPE_SPACES
)
2404 /* for tabs+spaces mode we want the real tab width, not indent width */
2405 SETPTR(whitespace
, g_strnfill(sci_get_tab_width(editor
->sci
), ' '));
2406 str
= g_strdup_printf("^\t*(%s)", whitespace
);
2408 regex
= g_regex_new(str
, cflags
, 0, NULL
);
2409 while (utils_string_regex_replace_all(buf
, regex
, 1, "\t", TRUE
));
2410 g_regex_unref(regex
);
2417 /** Inserts text, replacing \\t tab chars (@c 0x9) and \\n newline chars (@c 0xA)
2418 * accordingly for the document.
2419 * - Leading tabs are replaced with the correct indentation.
2420 * - Non-leading tabs are replaced with spaces (except when using 'Tabs' indent type).
2421 * - Newline chars are replaced with the correct line ending string.
2422 * This is very useful for inserting code without having to handle the indent
2423 * type yourself (Tabs & Spaces mode can be tricky).
2424 * @param editor Editor.
2425 * @param text Intended as e.g. @c "if (foo)\n\tbar();".
2426 * @param insert_pos Document position to insert text at.
2427 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2428 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2429 * -1 to read the indent size from the line with @a insert_pos on it.
2430 * @param replace_newlines Whether to replace newlines. If
2431 * newlines have been replaced already, this should be false, to avoid errors e.g. on Windows.
2432 * @warning Make sure all \\t tab chars in @a text are intended as indent widths or alignment,
2433 * not hard tabs, as those won't be preserved.
2434 * @note This doesn't scroll the cursor in view afterwards. **/
2436 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2437 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2439 ScintillaObject
*sci
= editor
->sci
;
2440 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2442 const gchar
*eol
= editor_get_eol_char(editor
);
2445 g_return_if_fail(text
);
2446 g_return_if_fail(editor
!= NULL
);
2447 g_return_if_fail(insert_pos
>= 0);
2449 buf
= g_string_new(text
);
2451 if (cursor_index
>= 0)
2452 g_string_insert(buf
, cursor_index
, geany_cursor_marker
); /* remember cursor pos */
2454 if (newline_indent_size
== -1)
2456 /* count indent size up to insert_pos instead of asking sci
2457 * because there may be spaces after it */
2458 gchar
*tmp
= sci_get_line(sci
, line_start
);
2460 idx
= insert_pos
- sci_get_position_from_line(sci
, line_start
);
2462 newline_indent_size
= count_indent_size(editor
, tmp
);
2466 /* Add line indents (in spaces) */
2467 if (newline_indent_size
> 0)
2469 const gchar
*nl
= replace_newlines
? "\n" : eol
;
2472 whitespace
= g_strnfill(newline_indent_size
, ' ');
2473 SETPTR(whitespace
, g_strconcat(nl
, whitespace
, NULL
));
2474 utils_string_replace_all(buf
, nl
, whitespace
);
2478 /* transform line endings */
2479 if (replace_newlines
)
2480 utils_string_replace_all(buf
, "\n", eol
);
2482 fix_indentation(editor
, buf
);
2484 idx
= replace_cursor_markers(editor
, buf
);
2487 sci_insert_text(sci
, insert_pos
, buf
->str
);
2488 sci_set_current_position(sci
, insert_pos
+ idx
, FALSE
);
2491 sci_insert_text(sci
, insert_pos
, buf
->str
);
2493 snippet_cursor_insert_pos
= sci_get_current_position(sci
);
2495 g_string_free(buf
, TRUE
);
2499 /* Move the cursor to the next specified cursor position in an inserted snippet.
2500 * Can, and should, be optimized to give better results */
2501 void editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2503 ScintillaObject
*sci
= editor
->sci
;
2504 gint current_pos
= sci_get_current_position(sci
);
2506 if (snippet_offsets
&& !g_queue_is_empty(snippet_offsets
))
2510 offset
= GPOINTER_TO_INT(g_queue_pop_head(snippet_offsets
));
2511 if (current_pos
> snippet_cursor_insert_pos
)
2512 snippet_cursor_insert_pos
= offset
+ current_pos
;
2514 snippet_cursor_insert_pos
+= offset
;
2516 sci_set_current_position(sci
, snippet_cursor_insert_pos
, TRUE
);
2525 static void snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
)
2527 GHashTable
*specials
;
2529 /* replace 'special' completions */
2530 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2531 if (G_LIKELY(specials
!= NULL
))
2533 g_hash_table_foreach(specials
, snippets_replace_specials
, pattern
);
2536 /* now transform other wildcards */
2537 utils_string_replace_all(pattern
, "%newline%", "\n");
2538 utils_string_replace_all(pattern
, "%ws%", "\t");
2540 /* replace %cursor% by a very unlikely string marker */
2541 utils_string_replace_all(pattern
, "%cursor%", geany_cursor_marker
);
2543 /* unescape '%' after all %wildcards% */
2544 templates_replace_valist(pattern
, "{pc}", "%", NULL
);
2546 /* replace any template {foo} wildcards */
2547 templates_replace_common(pattern
, editor
->document
->file_name
, editor
->document
->file_type
, NULL
);
2551 static gssize
replace_cursor_markers(GeanyEditor
*editor
, GString
*pattern
)
2553 gssize cur_index
= -1;
2555 GList
*temp_list
= NULL
;
2556 gint cursor_steps
= 0, old_cursor
= 0;
2561 cursor_steps
= utils_string_find(pattern
, cursor_steps
, -1, geany_cursor_marker
);
2562 if (cursor_steps
== -1)
2565 g_string_erase(pattern
, cursor_steps
, strlen(geany_cursor_marker
));
2569 /* save the relative offset to each cursor position */
2570 temp_list
= g_list_prepend(temp_list
, GINT_TO_POINTER(cursor_steps
- old_cursor
));
2574 /* first cursor already includes newline positions */
2575 cur_index
= cursor_steps
;
2577 old_cursor
= cursor_steps
;
2580 /* put the cursor positions for the most recent
2581 * parsed snippet first, followed by any remaining positions */
2587 temp_list
= g_list_reverse(temp_list
);
2588 foreach_list(node
, temp_list
)
2589 g_queue_push_nth(snippet_offsets
, node
->data
, i
++);
2591 /* limit length of queue */
2592 while (g_queue_get_length(snippet_offsets
) > 20)
2593 g_queue_pop_tail(snippet_offsets
);
2595 g_list_free(temp_list
);
2597 /* if there's no first cursor, skip whole snippet */
2599 cur_index
= pattern
->len
;
2605 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2607 ScintillaObject
*sci
= editor
->sci
;
2609 const gchar
*completion
;
2611 gint ft_id
= editor
->document
->file_type
->id
;
2613 str
= g_strdup(word
);
2616 completion
= snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
);
2617 if (completion
== NULL
)
2623 /* remove the typed word, it will be added again by the used auto completion
2624 * (not really necessary but this makes the auto completion more flexible,
2625 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2626 str_len
= strlen(str
);
2627 sci_set_selection_start(sci
, pos
- str_len
);
2628 sci_set_selection_end(sci
, pos
);
2629 sci_replace_sel(sci
, "");
2630 pos
-= str_len
; /* pos has changed while deleting */
2632 editor_insert_snippet(editor
, pos
, completion
);
2633 sci_scroll_caret(sci
);
2640 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2642 gint line
= sci_get_line_from_position(sci
, pos
);
2645 /* skip any trailing spaces */
2648 c
= sci_get_char_at(sci
, pos
);
2649 if (c
== ' ' || c
== '\t')
2655 return (pos
== sci_get_line_end_position(sci
, line
));
2659 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2661 gboolean result
= FALSE
;
2664 ScintillaObject
*sci
;
2666 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2669 if (sci_has_selection(sci
))
2671 /* return if we are editing an existing line (chars on right of cursor) */
2672 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2673 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_space
&&
2674 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2677 wc
= snippets_find_completion_by_name("Special", "wordchars");
2678 word
= editor_read_word_stem(editor
, pos
, wc
);
2680 /* prevent completion of "for " */
2682 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2684 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2685 result
= snippets_complete_constructs(editor
, pos
, word
);
2686 sci_end_undo_action(sci
);
2688 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2694 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2696 ScintillaObject
*sci
= editor
->sci
;
2697 gchar
*to_insert
= NULL
;
2701 const gchar
*gt
= ">";
2702 /* if there is already a '>' behind the cursor, don't add it */
2703 if (sci_get_char_at(sci
, pos
) == '>')
2706 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2709 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2711 sci_start_undo_action(sci
);
2712 sci_replace_sel(sci
, to_insert
);
2714 sci_set_selection(sci
, pos
, pos
);
2715 sci_end_undo_action(sci
);
2721 * (stolen from anjuta and heavily modified)
2722 * This routine will auto complete XML or HTML tags that are still open by closing them
2723 * @param ch The character we are dealing with, currently only works with the '>' character
2724 * @return True if handled, false otherwise
2726 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2728 ScintillaObject
*sci
= editor
->sci
;
2729 gint lexer
= sci_get_lexer(sci
);
2730 gint min
, size
, style
;
2731 gchar
*str_found
, sel
[512];
2732 gboolean result
= FALSE
;
2734 /* If the user has turned us off, quit now.
2735 * This may make sense only in certain languages */
2736 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2739 /* return if we are inside any embedded script */
2740 style
= sci_get_style_at(sci
, pos
);
2741 if (style
> SCE_H_XCCOMMENT
&& ! highlighting_is_string_style(lexer
, style
))
2744 /* if ch is /, check for </, else quit */
2745 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2748 /* Grab the last 512 characters or so */
2749 min
= pos
- (sizeof(sel
) - 1);
2750 if (min
< 0) min
= 0;
2753 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2755 sci_get_text_range(sci
, min
, pos
, sel
);
2756 sel
[sizeof(sel
) - 1] = '\0';
2758 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2759 /* User typed something like "<br/>" */
2764 size
-= 2; /* skip </ */
2765 str_found
= utils_find_open_xml_tag(sel
, size
);
2767 if (lexer
== SCLEX_HTML
&& utils_is_short_html_tag(str_found
))
2771 else if (!EMPTY(str_found
))
2773 insert_closing_tag(editor
, pos
, ch
, str_found
);
2781 /* like sci_get_line_indentation(), but for a string. */
2782 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2785 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2788 g_return_val_if_fail(base_indent
, 0);
2790 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2808 /* Handles special cases where HTML is embedded in another language or
2809 * another language is embedded in HTML */
2810 static GeanyFiletype
*editor_get_filetype_at_line(GeanyEditor
*editor
, gint line
)
2812 gint style
, line_start
;
2813 GeanyFiletype
*current_ft
;
2815 g_return_val_if_fail(editor
!= NULL
, NULL
);
2816 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, NULL
);
2818 current_ft
= editor
->document
->file_type
;
2819 line_start
= sci_get_position_from_line(editor
->sci
, line
);
2820 style
= sci_get_style_at(editor
->sci
, line_start
);
2822 /* Handle PHP filetype with embedded HTML */
2823 if (current_ft
->id
== GEANY_FILETYPES_PHP
&& ! is_style_php(style
))
2824 current_ft
= filetypes
[GEANY_FILETYPES_HTML
];
2826 /* Handle languages embedded in HTML */
2827 if (current_ft
->id
== GEANY_FILETYPES_HTML
)
2830 if (style
>= SCE_HJ_DEFAULT
&& style
<= SCE_HJ_REGEX
)
2831 current_ft
= filetypes
[GEANY_FILETYPES_JS
];
2833 else if (style
>= SCE_HJA_DEFAULT
&& style
<= SCE_HJA_REGEX
)
2834 current_ft
= filetypes
[GEANY_FILETYPES_JS
];
2836 else if (style
>= SCE_HB_DEFAULT
&& style
<= SCE_HB_STRINGEOL
)
2837 current_ft
= filetypes
[GEANY_FILETYPES_BASIC
];
2839 else if (style
>= SCE_HBA_DEFAULT
&& style
<= SCE_HBA_STRINGEOL
)
2840 current_ft
= filetypes
[GEANY_FILETYPES_BASIC
];
2841 /* Embedded Python */
2842 else if (style
>= SCE_HP_DEFAULT
&& style
<= SCE_HP_IDENTIFIER
)
2843 current_ft
= filetypes
[GEANY_FILETYPES_PYTHON
];
2845 else if (style
>= SCE_HPA_DEFAULT
&& style
<= SCE_HPA_IDENTIFIER
)
2846 current_ft
= filetypes
[GEANY_FILETYPES_PYTHON
];
2848 else if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
2849 style
== SCE_HPHP_COMPLEX_VARIABLE
)
2851 current_ft
= filetypes
[GEANY_FILETYPES_PHP
];
2855 /* Ensure the filetype's config is loaded */
2856 filetypes_load_config(current_ft
->id
, FALSE
);
2862 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2865 gchar
*str_begin
, *str_end
;
2866 const gchar
*co
, *cc
;
2870 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2872 ft
= editor_get_filetype_at_line(editor
, line_start
);
2874 eol
= editor_get_eol_char(editor
);
2875 if (! filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
))
2876 g_return_if_reached();
2877 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2878 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2880 /* insert the comment strings */
2881 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2882 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2883 sci_insert_text(editor
->sci
, line_len
, str_end
);
2890 /* find @p text inside the range of the current style */
2891 static gint
find_in_current_style(ScintillaObject
*sci
, const gchar
*text
, gboolean backwards
)
2893 gint start
= sci_get_current_position(sci
);
2895 gint len
= sci_get_length(sci
);
2896 gint current_style
= sci_get_style_at(sci
, start
);
2897 struct Sci_TextToFind ttf
;
2899 while (start
> 0 && sci_get_style_at(sci
, start
- 1) == current_style
)
2901 while (end
< len
&& sci_get_style_at(sci
, end
+ 1) == current_style
)
2904 ttf
.lpstrText
= (gchar
*) text
;
2905 ttf
.chrg
.cpMin
= backwards
? end
+ 1 : start
;
2906 ttf
.chrg
.cpMax
= backwards
? start
: end
+ 1;
2907 return sci_find_text(sci
, 0, &ttf
);
2911 static void sci_delete_line(ScintillaObject
*sci
, gint line
)
2913 gint start
= sci_get_position_from_line(sci
, line
);
2914 gint len
= sci_get_line_length(sci
, line
);
2915 SSM(sci
, SCI_DELETERANGE
, start
, len
);
2919 static gboolean
real_uncomment_multiline(GeanyEditor
*editor
)
2921 /* find the beginning of the multi line comment */
2922 gint start
, end
, start_line
, end_line
;
2924 const gchar
*co
, *cc
;
2926 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, FALSE
);
2928 ft
= editor_get_filetype_at_line(editor
, sci_get_current_line(editor
->sci
));
2929 if (! filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
))
2930 g_return_val_if_reached(FALSE
);
2932 start
= find_in_current_style(editor
->sci
, co
, TRUE
);
2933 end
= find_in_current_style(editor
->sci
, cc
, FALSE
);
2935 if (start
< 0 || end
< 0 || start
> end
/* who knows */)
2938 start_line
= sci_get_line_from_position(editor
->sci
, start
);
2939 end_line
= sci_get_line_from_position(editor
->sci
, end
);
2941 /* remove comment close chars */
2942 SSM(editor
->sci
, SCI_DELETERANGE
, end
, strlen(cc
));
2943 if (sci_is_blank_line(editor
->sci
, end_line
))
2944 sci_delete_line(editor
->sci
, end_line
);
2946 /* remove comment open chars (do it last since it would move the end position) */
2947 SSM(editor
->sci
, SCI_DELETERANGE
, start
, strlen(co
));
2948 if (sci_is_blank_line(editor
->sci
, start_line
))
2949 sci_delete_line(editor
->sci
, start_line
);
2955 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2957 gint lexer
= sci_get_lexer(editor
->sci
);
2960 /* List only those lexers which support multi line comments */
2965 case SCLEX_PHPSCRIPT
:
2967 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2968 style_comment
= SCE_HPHP_COMMENT
;
2970 style_comment
= SCE_H_COMMENT
;
2974 case SCLEX_LITERATEHASKELL
:
2975 style_comment
= SCE_HA_COMMENTBLOCK
; break;
2976 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2977 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2978 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2979 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2980 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2981 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2982 case SCLEX_RUST
: style_comment
= SCE_RUST_COMMENTBLOCK
; break;
2983 default: style_comment
= SCE_C_COMMENT
;
2986 return style_comment
;
2990 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2991 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2992 * it returns just 1 */
2993 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2995 gint first_line
, last_line
;
2996 gint x
, i
, line_start
, line_len
;
2997 gint sel_start
, sel_end
;
3001 const gchar
*co
, *cc
;
3002 gboolean single_line
= FALSE
;
3005 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
3008 { /* use selection or current line */
3009 sel_start
= sci_get_selection_start(editor
->sci
);
3010 sel_end
= sci_get_selection_end(editor
->sci
);
3012 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3013 /* Find the last line with chars selected (not EOL char) */
3014 last_line
= sci_get_line_from_position(editor
->sci
,
3015 sel_end
- editor_get_eol_char_len(editor
));
3016 last_line
= MAX(first_line
, last_line
);
3020 first_line
= last_line
= line
;
3021 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3024 ft
= editor_get_filetype_at_line(editor
, first_line
);
3026 if (! filetype_get_comment_open_close(ft
, TRUE
, &co
, &cc
))
3029 co_len
= strlen(co
);
3033 sci_start_undo_action(editor
->sci
);
3035 for (i
= first_line
; i
<= last_line
; i
++)
3039 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3040 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3043 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3046 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3047 sel
[buf_len
] = '\0';
3049 while (isspace(sel
[x
])) x
++;
3051 /* to skip blank lines */
3052 if (x
< line_len
&& sel
[x
] != '\0')
3054 /* use single line comment */
3061 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
3062 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
3063 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
3070 if (strncmp(sel
+ x
, co
, co_len
) != 0)
3074 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
3075 sci_replace_sel(editor
->sci
, "");
3078 /* use multi line comment */
3083 /* skip lines which are already comments */
3084 style_comment
= get_multiline_comment_style(editor
, line_start
);
3085 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3087 if (real_uncomment_multiline(editor
))
3091 /* break because we are already on the last line */
3096 sci_end_undo_action(editor
->sci
);
3098 /* restore selection if there is one
3099 * but don't touch the selection if caller is editor_do_comment_toggle */
3100 if (! toggle
&& sel_start
< sel_end
)
3104 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
3105 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
3109 gint eol_len
= editor_get_eol_char_len(editor
);
3110 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
3111 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
3119 void editor_do_comment_toggle(GeanyEditor
*editor
)
3121 gint first_line
, last_line
;
3122 gint x
, i
, line_start
, line_len
, first_line_start
, last_line_start
;
3123 gint sel_start
, sel_end
;
3124 gint count_commented
= 0, count_uncommented
= 0;
3126 const gchar
*co
, *cc
;
3127 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3128 gboolean first_line_was_comment
= FALSE
;
3129 gboolean last_line_was_comment
= FALSE
;
3131 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
3134 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3136 sel_start
= sci_get_selection_start(editor
->sci
);
3137 sel_end
= sci_get_selection_end(editor
->sci
);
3139 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3140 /* Find the last line with chars selected (not EOL char) */
3141 last_line
= sci_get_line_from_position(editor
->sci
,
3142 sel_end
- editor_get_eol_char_len(editor
));
3143 last_line
= MAX(first_line
, last_line
);
3145 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3146 last_line_start
= sci_get_position_from_line(editor
->sci
, last_line
);
3148 ft
= editor_get_filetype_at_line(editor
, first_line
);
3150 if (! filetype_get_comment_open_close(ft
, TRUE
, &co
, &cc
))
3153 co_len
= strlen(co
);
3157 sci_start_undo_action(editor
->sci
);
3159 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3163 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3164 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3167 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3170 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3171 sel
[buf_len
] = '\0';
3173 while (isspace(sel
[x
])) x
++;
3175 /* use single line comment */
3178 gboolean do_continue
= FALSE
;
3181 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
3182 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
3187 if (do_continue
&& i
== first_line
)
3188 first_line_was_comment
= TRUE
;
3189 last_line_was_comment
= do_continue
;
3193 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
3197 /* we are still here, so the above lines were not already comments, so comment it */
3198 count_commented
+= editor_do_comment(editor
, i
, FALSE
, TRUE
, TRUE
);
3200 /* use multi line comment */
3205 /* skip lines which are already comments */
3206 style_comment
= get_multiline_comment_style(editor
, line_start
);
3207 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3209 if (real_uncomment_multiline(editor
))
3210 count_uncommented
++;
3214 real_comment_multiline(editor
, line_start
, last_line
);
3218 /* break because we are already on the last line */
3224 sci_end_undo_action(editor
->sci
);
3228 /* restore selection or caret position */
3231 gint a
= (first_line_was_comment
) ? - (gint
) co_len
: (gint
) co_len
;
3234 /* don't modify sel_start when the selection starts within indentation */
3235 read_indent(editor
, sel_start
);
3236 indent_len
= (gint
) strlen(indent
);
3237 if ((sel_start
- first_line_start
) <= indent_len
)
3239 /* if the selection start was inside the comment mark, adjust the position */
3240 else if (first_line_was_comment
&&
3241 sel_start
>= (first_line_start
+ indent_len
) &&
3242 sel_start
<= (first_line_start
+ indent_len
+ (gint
) co_len
))
3244 a
= (first_line_start
+ indent_len
) - sel_start
;
3247 if (sel_start
< sel_end
)
3249 gint b
= (count_commented
* (gint
) co_len
) - (count_uncommented
* (gint
) co_len
);
3251 /* same for selection end, but here we add an offset on the offset above */
3252 read_indent(editor
, sel_end
+ b
);
3253 indent_len
= (gint
) strlen(indent
);
3254 if ((sel_end
- last_line_start
) < indent_len
)
3255 b
+= last_line_was_comment
? (gint
) co_len
: -(gint
) co_len
;
3256 else if (last_line_was_comment
&&
3257 sel_end
>= (last_line_start
+ indent_len
) &&
3258 sel_end
<= (last_line_start
+ indent_len
+ (gint
) co_len
))
3260 b
+= (gint
) co_len
- (sel_end
- (last_line_start
+ indent_len
));
3263 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
3264 sci_set_selection_end(editor
->sci
, sel_end
+ b
);
3267 sci_set_current_position(editor
->sci
, sel_start
+ a
, TRUE
);
3271 gint eol_len
= editor_get_eol_char_len(editor
);
3272 if (count_uncommented
> 0)
3274 sci_set_selection_start(editor
->sci
, sel_start
- (gint
) co_len
+ eol_len
);
3275 sci_set_selection_end(editor
->sci
, sel_end
- (gint
) co_len
+ eol_len
);
3277 else if (count_commented
> 0)
3279 sci_set_selection_start(editor
->sci
, sel_start
+ (gint
) co_len
- eol_len
);
3280 sci_set_selection_end(editor
->sci
, sel_end
+ (gint
) co_len
- eol_len
);
3282 if (sel_start
>= sel_end
)
3283 sci_scroll_caret(editor
->sci
);
3288 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
3289 gint
editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
,
3290 gboolean single_comment
)
3292 gint first_line
, last_line
;
3293 gint x
, i
, line_start
, line_len
;
3294 gint sel_start
, sel_end
, co_len
;
3297 const gchar
*co
, *cc
;
3298 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3301 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
3304 { /* use selection or current line */
3305 sel_start
= sci_get_selection_start(editor
->sci
);
3306 sel_end
= sci_get_selection_end(editor
->sci
);
3308 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3309 /* Find the last line with chars selected (not EOL char) */
3310 last_line
= sci_get_line_from_position(editor
->sci
,
3311 sel_end
- editor_get_eol_char_len(editor
));
3312 last_line
= MAX(first_line
, last_line
);
3316 first_line
= last_line
= line
;
3317 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3320 ft
= editor_get_filetype_at_line(editor
, first_line
);
3322 if (! filetype_get_comment_open_close(ft
, single_comment
, &co
, &cc
))
3325 co_len
= strlen(co
);
3329 sci_start_undo_action(editor
->sci
);
3331 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3335 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3336 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3339 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3342 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3343 sel
[buf_len
] = '\0';
3345 while (isspace(sel
[x
])) x
++;
3347 /* to skip blank lines */
3348 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3350 /* use single line comment */
3353 gint start
= line_start
;
3356 if (ft
->comment_use_indent
)
3357 start
= line_start
+ x
;
3361 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3362 sci_insert_text(editor
->sci
, start
, text
);
3366 sci_insert_text(editor
->sci
, start
, co
);
3369 /* use multi line comment */
3374 /* skip lines which are already comments */
3375 style_comment
= get_multiline_comment_style(editor
, line_start
);
3376 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3379 real_comment_multiline(editor
, line_start
, last_line
);
3382 /* break because we are already on the last line */
3388 sci_end_undo_action(editor
->sci
);
3390 /* restore selection if there is one
3391 * but don't touch the selection if caller is editor_do_comment_toggle */
3392 if (! toggle
&& sel_start
< sel_end
)
3396 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3397 sci_set_selection_end(editor
->sci
, sel_end
+ (count
* co_len
));
3401 gint eol_len
= editor_get_eol_char_len(editor
);
3402 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3403 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3410 static gboolean brace_timeout_active
= FALSE
;
3412 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3414 GeanyDocument
*doc
= document_get_current();
3415 GeanyEditor
*editor
;
3416 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3417 gint end_pos
, cur_pos
;
3419 brace_timeout_active
= FALSE
;
3423 editor
= doc
->editor
;
3424 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3426 if (cur_pos
!= brace_pos
)
3429 if (cur_pos
!= brace_pos
)
3431 /* we have moved past the original brace_pos, but after the timeout
3432 * we may now be on a new brace, so check again */
3433 editor_highlight_braces(editor
, cur_pos
);
3437 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3439 editor_highlight_braces(editor
, cur_pos
);
3442 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3446 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3447 sci_get_col_from_position(editor
->sci
, end_pos
));
3448 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3449 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3453 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3454 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3460 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3462 gint brace_pos
= cur_pos
- 1;
3464 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3465 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3467 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3470 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3475 if (!brace_timeout_active
)
3477 brace_timeout_active
= TRUE
;
3478 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3479 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3484 static gboolean
in_block_comment(gint lexer
, gint style
)
3490 return (style
== SCE_C_COMMENT
||
3491 style
== SCE_C_COMMENTDOC
);
3494 return (style
== SCE_PAS_COMMENT
||
3495 style
== SCE_PAS_COMMENT2
);
3498 return (style
== SCE_D_COMMENT
||
3499 style
== SCE_D_COMMENTDOC
||
3500 style
== SCE_D_COMMENTNESTED
);
3503 case SCLEX_PHPSCRIPT
:
3504 return (style
== SCE_HPHP_COMMENT
);
3507 return (style
== SCE_CSS_COMMENT
);
3510 return (style
== SCE_RUST_COMMENTBLOCK
||
3511 style
== SCE_RUST_COMMENTBLOCKDOC
);
3519 static gboolean
is_comment_char(gchar c
, gint lexer
)
3521 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3531 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3533 ScintillaObject
*sci
= editor
->sci
;
3534 gint indent_pos
, style
;
3535 gint lexer
= sci_get_lexer(sci
);
3537 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3538 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3539 style
= sci_get_style_at(sci
, indent_pos
);
3540 if (!in_block_comment(lexer
, style
))
3543 /* Check whether the comment block continues on this line */
3544 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3545 if (sci_get_style_at(sci
, indent_pos
) == style
|| indent_pos
>= sci_get_length(sci
))
3547 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3548 /* the type of comment, '*' (C/C++/Java), '+' and the others (D) */
3549 const gchar
*continuation
= "*";
3550 const gchar
*whitespace
= ""; /* to hold whitespace if needed */
3552 gint len
= strlen(previous_line
);
3555 /* find and stop at end of multi line comment */
3557 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3558 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3560 gint indent_len
, indent_width
;
3562 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3563 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3564 indent_width
= editor_get_indent_prefs(editor
)->width
;
3566 /* if there is one too many spaces, delete the last space,
3567 * to return to the indent used before the multiline comment was started. */
3568 if (indent_len
% indent_width
== 1)
3569 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3570 g_free(previous_line
);
3573 /* check whether we are on the second line of multi line comment */
3575 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3578 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3579 { /* we are on the second line of a multi line comment, so we have to insert white space */
3583 if (style
== SCE_D_COMMENTNESTED
)
3584 continuation
= "+"; /* for nested comments in D */
3586 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3587 sci_add_text(sci
, result
);
3590 g_free(previous_line
);
3596 static gboolean
editor_lexer_is_c_like(gint lexer
)
3611 /* inserts a three-line comment at one line above current cursor position */
3612 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3618 gboolean have_multiline_comment
= FALSE
;
3620 const gchar
*co
, *cc
;
3622 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3624 if (! filetype_get_comment_open_close(editor
->document
->file_type
, FALSE
, &co
, &cc
))
3625 g_return_if_reached();
3627 have_multiline_comment
= TRUE
;
3629 sci_start_undo_action(editor
->sci
);
3631 doc
= editor
->document
;
3633 /* insert three lines one line above of the current position */
3634 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3635 pos
= sci_get_position_from_line(editor
->sci
, line
);
3637 /* use the indent on the current line but only when comment indentation is used
3638 * and we don't have multi line comment characters */
3639 if (editor
->auto_indent
&&
3640 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3642 read_indent(editor
, editor_info
.click_pos
);
3643 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3644 text_len
= strlen(text
);
3648 text
= g_strdup("\n\n\n");
3651 sci_insert_text(editor
->sci
, pos
, text
);
3654 /* select the inserted lines for commenting */
3655 sci_set_selection_start(editor
->sci
, pos
);
3656 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3658 editor_do_comment(editor
, -1, TRUE
, FALSE
, FALSE
);
3660 /* set the current position to the start of the first inserted line */
3663 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3664 if (have_multiline_comment
)
3667 pos
+= strlen(indent
);
3669 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3670 /* reset the selection */
3671 sci_set_anchor(editor
->sci
, pos
);
3673 sci_end_undo_action(editor
->sci
);
3677 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3678 * Scroll the view to make line appear at percent_of_view.
3679 * line can be -1 to use the current position. */
3680 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3685 g_return_if_fail(editor
!= NULL
);
3687 wid
= GTK_WIDGET(editor
->sci
);
3689 if (! gtk_widget_get_window(wid
) || ! gdk_window_is_viewable(gtk_widget_get_window(wid
)))
3690 return; /* prevent gdk_window_scroll warning */
3693 line
= sci_get_current_line(editor
->sci
);
3695 /* sci 'visible line' != doc line number because of folding and line wrapping */
3696 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3697 * SCI_DOCLINEFROMVISIBLE for vis1. */
3698 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3699 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3700 line
= line
- los
* percent_of_view
;
3701 SSM(editor
->sci
, SCI_SETFIRSTVISIBLELINE
, line
, 0);
3702 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3706 /* creates and inserts one tab or whitespace of the amount of the tab width */
3707 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3710 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3712 g_return_if_fail(editor
!= NULL
);
3714 switch (iprefs
.type
)
3716 case GEANY_INDENT_TYPE_TABS
:
3717 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3719 case GEANY_INDENT_TYPE_SPACES
:
3720 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3721 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3724 text
= get_whitespace(&iprefs
, iprefs
.width
);
3725 sci_add_text(editor
->sci
, text
);
3730 void editor_select_word(GeanyEditor
*editor
)
3736 g_return_if_fail(editor
!= NULL
);
3738 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3739 start
= sci_word_start_position(editor
->sci
, pos
, TRUE
);
3740 end
= sci_word_end_position(editor
->sci
, pos
, TRUE
);
3742 if (start
== end
) /* caret in whitespaces sequence */
3744 /* look forward but reverse the selection direction,
3745 * so the caret end up stay as near as the original position. */
3746 end
= sci_word_end_position(editor
->sci
, pos
, FALSE
);
3747 start
= sci_word_end_position(editor
->sci
, end
, TRUE
);
3752 sci_set_selection(editor
->sci
, start
, end
);
3756 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3757 * when those lines have no selection (cursor at start of line). */
3758 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3760 gint start
, end
, line
;
3762 g_return_if_fail(editor
!= NULL
);
3764 start
= sci_get_selection_start(editor
->sci
);
3765 end
= sci_get_selection_end(editor
->sci
);
3767 /* check if whole lines are already selected */
3768 if (! extra_line
&& start
!= end
&&
3769 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3770 sci_get_col_from_position(editor
->sci
, end
) == 0)
3773 line
= sci_get_line_from_position(editor
->sci
, start
);
3774 start
= sci_get_position_from_line(editor
->sci
, line
);
3776 line
= sci_get_line_from_position(editor
->sci
, end
);
3777 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3779 sci_set_selection(editor
->sci
, start
, end
);
3783 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
)
3785 return sci_get_line_indent_position(sci
, line
) ==
3786 sci_get_line_end_position(sci
, line
);
3790 /* Returns first line of paragraph for GTK_DIR_UP, line after paragraph
3791 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3792 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3795 ScintillaObject
*sci
= editor
->sci
;
3797 /* first check current line and return -1 if it is empty to skip creating of a selection */
3798 if (sci_is_blank_line(sci
, line
))
3801 if (direction
== GTK_DIR_UP
)
3811 /* start of document */
3815 if (line
== sci_get_line_count(sci
))
3818 if (sci_is_blank_line(sci
, line
))
3820 /* return line paragraph starts on */
3821 if (direction
== GTK_DIR_UP
)
3830 void editor_select_paragraph(GeanyEditor
*editor
)
3832 gint pos_start
, pos_end
, line_start
, line_found
;
3834 g_return_if_fail(editor
!= NULL
);
3836 line_start
= sci_get_current_line(editor
->sci
);
3838 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3839 if (line_found
== -1)
3842 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3844 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3845 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3847 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3851 /* Returns first line of block for GTK_DIR_UP, line after block
3852 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3853 static gint
find_block_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3856 ScintillaObject
*sci
= editor
->sci
;
3858 /* first check current line and return -1 if it is empty to skip creating of a selection */
3859 if (sci_is_blank_line(sci
, line
))
3862 if (direction
== GTK_DIR_UP
)
3867 ind
= sci_get_line_indentation(sci
, line
);
3873 /* start of document */
3877 if (line
== sci_get_line_count(sci
))
3880 if (sci_get_line_indentation(sci
, line
) != ind
||
3881 sci_is_blank_line(sci
, line
))
3883 /* return line block starts on */
3884 if (direction
== GTK_DIR_UP
)
3893 void editor_select_indent_block(GeanyEditor
*editor
)
3895 gint pos_start
, pos_end
, line_start
, line_found
;
3897 g_return_if_fail(editor
!= NULL
);
3899 line_start
= sci_get_current_line(editor
->sci
);
3901 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_UP
);
3902 if (line_found
== -1)
3905 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3907 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_DOWN
);
3908 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3910 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3914 /* simple indentation to indent the current line with the same indent as the previous one */
3915 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
3917 gint i
, sel_start
= 0, sel_end
= 0;
3919 /* get previous line and use it for read_indent to use that line
3920 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
3921 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
3923 for (i
= first_line
; i
<= last_line
; i
++)
3925 /* skip the first line or if the indentation of the previous and current line are equal */
3927 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
3928 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
3931 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3932 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3933 if (sel_start
< sel_end
)
3935 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
3936 sci_replace_sel(editor
->sci
, "");
3938 sci_insert_text(editor
->sci
, sel_start
, indent
);
3943 /* simple indentation to indent the current line with the same indent as the previous one */
3944 void editor_smart_line_indentation(GeanyEditor
*editor
, gint pos
)
3946 gint first_line
, last_line
;
3947 gint first_sel_start
, first_sel_end
;
3948 ScintillaObject
*sci
;
3950 g_return_if_fail(editor
!= NULL
);
3954 first_sel_start
= sci_get_selection_start(sci
);
3955 first_sel_end
= sci_get_selection_end(sci
);
3957 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
3958 /* Find the last line with chars selected (not EOL char) */
3959 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
3960 last_line
= MAX(first_line
, last_line
);
3963 pos
= first_sel_start
;
3965 sci_start_undo_action(sci
);
3967 smart_line_indentation(editor
, first_line
, last_line
);
3969 /* set cursor position if there was no selection */
3970 if (first_sel_start
== first_sel_end
)
3972 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
3974 /* use indent position as user may wish to change indentation afterwards */
3975 sci_set_current_position(sci
, indent_pos
, FALSE
);
3979 /* fully select all the lines affected */
3980 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
3981 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
3984 sci_end_undo_action(sci
);
3988 /* increase / decrease current line or selection by one space */
3989 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
3991 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
3992 gint sel_start
, sel_end
, first_line_offset
= 0;
3994 g_return_if_fail(editor
!= NULL
);
3996 sel_start
= sci_get_selection_start(editor
->sci
);
3997 sel_end
= sci_get_selection_end(editor
->sci
);
3999 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
4000 /* Find the last line with chars selected (not EOL char) */
4001 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
4002 last_line
= MAX(first_line
, last_line
);
4007 sci_start_undo_action(editor
->sci
);
4009 for (i
= first_line
; i
<= last_line
; i
++)
4011 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
4014 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
4015 /* searching backwards for a space to remove */
4016 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
4019 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
4021 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
4022 sci_replace_sel(editor
->sci
, "");
4024 if (i
== first_line
)
4025 first_line_offset
= -1;
4030 sci_insert_text(editor
->sci
, indentation_end
, " ");
4032 if (i
== first_line
)
4033 first_line_offset
= 1;
4037 /* set cursor position */
4038 if (sel_start
< sel_end
)
4040 gint start
= sel_start
+ first_line_offset
;
4041 if (first_line_offset
< 0)
4042 start
= MAX(sel_start
+ first_line_offset
,
4043 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
4045 sci_set_selection_start(editor
->sci
, start
);
4046 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
4049 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
4051 sci_end_undo_action(editor
->sci
);
4055 void editor_finalize(void)
4057 scintilla_release_resources();
4061 /* wordchars: NULL or a string containing characters to match a word.
4062 * Returns: the current selection or the current word.
4064 * Passing NULL as wordchars is NOT the same as passing GEANY_WORDCHARS: NULL means
4065 * using Scintillas's word boundaries. */
4066 gchar
*editor_get_default_selection(GeanyEditor
*editor
, gboolean use_current_word
,
4067 const gchar
*wordchars
)
4071 g_return_val_if_fail(editor
!= NULL
, NULL
);
4073 if (sci_get_lines_selected(editor
->sci
) == 1)
4074 s
= sci_get_selection_contents(editor
->sci
);
4075 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
4076 { /* use the word at current cursor position */
4077 gchar word
[GEANY_MAX_WORD_LENGTH
];
4079 if (wordchars
!= NULL
)
4080 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
4082 editor_find_current_word_sciwc(editor
, -1, word
, sizeof(word
));
4084 if (word
[0] != '\0')
4091 /* Note: Usually the line should be made visible (not folded) before calling this.
4092 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4093 * outside the *vertical* view.
4094 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4095 * sci_scroll_caret() when this returns TRUE. */
4096 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4100 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4102 /* If line is wrapped the result may occur on another virtual line than the first and may be
4103 * still hidden, so increase the line number to check for the next document line */
4104 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4107 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4108 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4109 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4111 return (line
>= vis1
&& line
< vis1
+ los
);
4115 /* If the current line is outside the current view window, scroll the line
4116 * so it appears at percent_of_view. */
4117 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4121 g_return_if_fail(editor
!= NULL
);
4123 line
= sci_get_current_line(editor
->sci
);
4125 /* unfold maybe folded results */
4126 sci_ensure_line_is_visible(editor
->sci
, line
);
4128 /* scroll the line if it's off screen */
4129 if (! editor_line_in_view(editor
, line
))
4130 editor
->scroll_percent
= percent_of_view
;
4132 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4137 * Deletes all currently set indicators in the @a editor window.
4138 * Error indicators (red squiggly underlines) and usual line markers are removed.
4140 * @param editor The editor to operate on.
4142 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4144 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4145 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4150 * Deletes all currently set indicators matching @a indic in the @a editor window.
4152 * @param editor The editor to operate on.
4153 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4158 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4162 g_return_if_fail(editor
!= NULL
);
4164 last_pos
= sci_get_length(editor
->sci
);
4167 sci_indicator_set(editor
->sci
, indic
);
4168 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4174 * Sets an indicator @a indic on @a line.
4175 * Whitespace at the start and the end of the line is not marked.
4177 * @param editor The editor to operate on.
4178 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4179 * @param line The line number which should be marked.
4184 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4190 g_return_if_fail(editor
!= NULL
);
4191 g_return_if_fail(line
>= 0);
4193 start
= sci_get_position_from_line(editor
->sci
, line
);
4194 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4196 /* skip blank lines */
4197 if ((start
+ 1) == end
||
4199 (sci_get_line_end_position(editor
->sci
, line
) - start
) == 0)
4205 linebuf
= sci_get_line(editor
->sci
, line
);
4207 /* don't set the indicator on whitespace */
4208 while (isspace(linebuf
[i
]))
4210 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4217 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4222 * Sets an indicator on the range specified by @a start and @a end.
4223 * No error checking or whitespace removal is performed, this should be done by the calling
4224 * function if necessary.
4226 * @param editor The editor to operate on.
4227 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4228 * @param start The starting position for the marker.
4229 * @param end The ending position for the marker.
4234 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4236 g_return_if_fail(editor
!= NULL
);
4240 sci_indicator_set(editor
->sci
, indic
);
4241 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4245 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4246 * the replacement will also start with 0x... */
4247 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4249 g_return_if_fail(editor
!= NULL
);
4251 if (sci_has_selection(editor
->sci
))
4253 gint start
= sci_get_selection_start(editor
->sci
);
4254 const gchar
*replacement
= colour
;
4256 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4257 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4259 gint end
= sci_get_selection_end(editor
->sci
);
4261 sci_set_selection_start(editor
->sci
, start
+ 2);
4262 /* we need to also re-set the selection end in case the anchor was located before
4263 * the cursor, since set_selection_start() always moves the cursor, not the anchor */
4264 sci_set_selection_end(editor
->sci
, end
);
4265 replacement
++; /* skip the leading "0x" */
4267 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4268 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4269 replacement
++; /* so skip the '#' to only replace the colour value */
4271 sci_replace_sel(editor
->sci
, replacement
);
4274 sci_add_text(editor
->sci
, colour
);
4279 * Retrieves the end of line characters mode (LF, CR/LF, CR) in the given editor.
4280 * If @a editor is @c NULL, the default end of line characters are used.
4282 * @param editor The editor to operate on, or @c NULL to query the default value.
4283 * @return The used end of line characters mode.
4288 gint
editor_get_eol_char_mode(GeanyEditor
*editor
)
4290 gint mode
= file_prefs
.default_eol_character
;
4293 mode
= sci_get_eol_mode(editor
->sci
);
4300 * Retrieves the localized name (for displaying) of the used end of line characters
4301 * (LF, CR/LF, CR) in the given editor.
4302 * If @a editor is @c NULL, the default end of line characters are used.
4304 * @param editor The editor to operate on, or @c NULL to query the default value.
4305 * @return The name of the end of line characters.
4310 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4312 gint mode
= file_prefs
.default_eol_character
;
4315 mode
= sci_get_eol_mode(editor
->sci
);
4317 return utils_get_eol_name(mode
);
4322 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4323 * If @a editor is @c NULL, the default end of line characters are used.
4324 * The returned value is 1 for CR and LF and 2 for CR/LF.
4326 * @param editor The editor to operate on, or @c NULL to query the default value.
4327 * @return The length of the end of line characters.
4332 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4334 gint mode
= file_prefs
.default_eol_character
;
4337 mode
= sci_get_eol_mode(editor
->sci
);
4341 case SC_EOL_CRLF
: return 2; break;
4342 default: return 1; break;
4348 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4349 * If @a editor is @c NULL, the default end of line characters are used.
4350 * The returned value is either "\n", "\r\n" or "\r".
4352 * @param editor The editor to operate on, or @c NULL to query the default value.
4353 * @return The end of line characters.
4358 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4360 gint mode
= file_prefs
.default_eol_character
;
4363 mode
= sci_get_eol_mode(editor
->sci
);
4365 return utils_get_eol_char(mode
);
4369 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4371 gint lines
, first
, i
;
4373 if (editor
== NULL
|| ! editor_prefs
.folding
)
4376 lines
= sci_get_line_count(editor
->sci
);
4377 first
= sci_get_first_visible_line(editor
->sci
);
4379 for (i
= 0; i
< lines
; i
++)
4381 gint level
= sci_get_fold_level(editor
->sci
, i
);
4383 if (level
& SC_FOLDLEVELHEADERFLAG
)
4385 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4386 sci_toggle_fold(editor
->sci
, i
);
4389 editor_scroll_to_line(editor
, first
, 0.0F
);
4393 void editor_unfold_all(GeanyEditor
*editor
)
4395 fold_all(editor
, FALSE
);
4399 void editor_fold_all(GeanyEditor
*editor
)
4401 fold_all(editor
, TRUE
);
4405 void editor_replace_tabs(GeanyEditor
*editor
, gboolean ignore_selection
)
4407 gint search_pos
, pos_in_line
, current_tab_true_length
;
4408 gint anchor_pos
, caret_pos
;
4411 struct Sci_TextToFind ttf
;
4413 g_return_if_fail(editor
!= NULL
);
4415 sci_start_undo_action(editor
->sci
);
4416 tab_len
= sci_get_tab_width(editor
->sci
);
4417 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4419 ttf
.chrg
.cpMin
= sci_get_selection_start(editor
->sci
);
4420 ttf
.chrg
.cpMax
= sci_get_selection_end(editor
->sci
);
4425 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4427 ttf
.lpstrText
= (gchar
*) "\t";
4429 anchor_pos
= SSM(editor
->sci
, SCI_GETANCHOR
, 0, 0);
4430 caret_pos
= sci_get_current_position(editor
->sci
);
4433 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4434 if (search_pos
== -1)
4437 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4438 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4439 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4440 sci_set_target_start(editor
->sci
, search_pos
);
4441 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4442 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4443 /* next search starts after replacement */
4444 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4445 /* update end of range now text has changed */
4446 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4449 if (anchor_pos
> search_pos
)
4450 anchor_pos
+= current_tab_true_length
- 1;
4451 if (caret_pos
> search_pos
)
4452 caret_pos
+= current_tab_true_length
- 1;
4454 sci_set_selection(editor
->sci
, anchor_pos
, caret_pos
);
4455 sci_end_undo_action(editor
->sci
);
4459 /* Replaces all occurrences all spaces of the length of a given tab_width,
4460 * optionally restricting the search to the current selection. */
4461 void editor_replace_spaces(GeanyEditor
*editor
, gboolean ignore_selection
)
4464 gint anchor_pos
, caret_pos
;
4465 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4468 struct Sci_TextToFind ttf
;
4470 g_return_if_fail(editor
!= NULL
);
4472 if (tab_len_f
< 0.0)
4473 tab_len_f
= sci_get_tab_width(editor
->sci
);
4475 if (! dialogs_show_input_numeric(
4476 _("Enter Tab Width"),
4477 _("Enter the amount of spaces which should be replaced by a tab character."),
4478 &tab_len_f
, 1, 100, 1))
4482 tab_len
= (gint
) tab_len_f
;
4483 text
= g_strnfill(tab_len
, ' ');
4485 sci_start_undo_action(editor
->sci
);
4486 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4488 ttf
.chrg
.cpMin
= sci_get_selection_start(editor
->sci
);
4489 ttf
.chrg
.cpMax
= sci_get_selection_end(editor
->sci
);
4494 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4496 ttf
.lpstrText
= text
;
4498 anchor_pos
= SSM(editor
->sci
, SCI_GETANCHOR
, 0, 0);
4499 caret_pos
= sci_get_current_position(editor
->sci
);
4502 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4503 if (search_pos
== -1)
4505 /* only replace indentation because otherwise we can mess up alignment */
4506 if (search_pos
> sci_get_line_indent_position(editor
->sci
,
4507 sci_get_line_from_position(editor
->sci
, search_pos
)))
4509 ttf
.chrg
.cpMin
= search_pos
+ tab_len
;
4512 sci_set_target_start(editor
->sci
, search_pos
);
4513 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4514 sci_replace_target(editor
->sci
, "\t", FALSE
);
4515 ttf
.chrg
.cpMin
= search_pos
;
4516 /* update end of range now text has changed */
4517 ttf
.chrg
.cpMax
-= tab_len
- 1;
4519 if (anchor_pos
> search_pos
)
4520 anchor_pos
-= tab_len
- 1;
4521 if (caret_pos
> search_pos
)
4522 caret_pos
-= tab_len
- 1;
4524 sci_set_selection(editor
->sci
, anchor_pos
, caret_pos
);
4525 sci_end_undo_action(editor
->sci
);
4530 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4532 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4533 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4534 gint i
= line_end
- 1;
4535 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4537 /* Diff hunks should keep trailing spaces */
4538 if (sci_get_lexer(editor
->sci
) == SCLEX_DIFF
)
4541 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4544 ch
= sci_get_char_at(editor
->sci
, i
);
4546 if (i
< (line_end
- 1))
4548 sci_set_target_start(editor
->sci
, i
+ 1);
4549 sci_set_target_end(editor
->sci
, line_end
);
4550 sci_replace_target(editor
->sci
, "", FALSE
);
4555 void editor_strip_trailing_spaces(GeanyEditor
*editor
, gboolean ignore_selection
)
4561 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4563 gint selection_start
= sci_get_selection_start(editor
->sci
);
4564 gint selection_end
= sci_get_selection_end(editor
->sci
);
4566 start_line
= sci_get_line_from_position(editor
->sci
, selection_start
);
4567 end_line
= sci_get_line_from_position(editor
->sci
, selection_end
);
4569 if (sci_get_col_from_position(editor
->sci
, selection_end
) > 0)
4575 end_line
= sci_get_line_count(editor
->sci
);
4578 sci_start_undo_action(editor
->sci
);
4580 for (line
= start_line
; line
< end_line
; line
++)
4582 editor_strip_line_trailing_spaces(editor
, line
);
4584 sci_end_undo_action(editor
->sci
);
4588 void editor_ensure_final_newline(GeanyEditor
*editor
)
4590 gint max_lines
= sci_get_line_count(editor
->sci
);
4591 gboolean append_newline
= (max_lines
== 1);
4592 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4596 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4600 const gchar
*eol
= editor_get_eol_char(editor
);
4602 sci_insert_text(editor
->sci
, end_document
, eol
);
4607 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4611 PangoFontDescription
*pfd
;
4613 g_return_if_fail(editor
);
4615 pfd
= pango_font_description_from_string(font
);
4616 size
= pango_font_description_get_size(pfd
) / PANGO_SCALE
;
4617 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4618 pango_font_description_free(pfd
);
4620 for (style
= 0; style
<= STYLE_MAX
; style
++)
4621 sci_set_font(editor
->sci
, style
, font_name
, size
);
4625 /* zoom to 100% to prevent confusion */
4626 sci_zoom_off(editor
->sci
);
4630 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4632 g_return_if_fail(editor
!= NULL
);
4634 editor
->line_wrapping
= wrap
;
4635 sci_set_lines_wrapped(editor
->sci
, wrap
);
4639 /** Sets the indent type for @a editor.
4640 * @param editor Editor.
4641 * @param type Indent type.
4646 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4648 editor_set_indent(editor
, type
, editor
->indent_width
);
4652 void editor_set_indent_width(GeanyEditor
*editor
, gint width
)
4654 editor_set_indent(editor
, editor
->indent_type
, width
);
4658 void editor_set_indent(GeanyEditor
*editor
, GeanyIndentType type
, gint width
)
4660 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4661 ScintillaObject
*sci
= editor
->sci
;
4662 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4664 editor
->indent_type
= type
;
4665 editor
->indent_width
= width
;
4666 sci_set_use_tabs(sci
, use_tabs
);
4668 if (type
== GEANY_INDENT_TYPE_BOTH
)
4670 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4671 if (iprefs
->hard_tab_width
!= 8)
4673 static gboolean warn
= TRUE
;
4675 ui_set_statusbar(TRUE
, _("Warning: non-standard hard tab width: %d != 8!"),
4676 iprefs
->hard_tab_width
);
4681 sci_set_tab_width(sci
, width
);
4683 SSM(sci
, SCI_SETINDENT
, width
, 0);
4685 /* remove indent spaces on backspace, if using any spaces to indent */
4686 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, type
!= GEANY_INDENT_TYPE_TABS
, 0);
4690 /* Convenience function for editor_goto_pos() to pass in a line number. */
4691 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line_no
, gint offset
)
4695 g_return_val_if_fail(editor
, FALSE
);
4696 if (line_no
< 0 || line_no
>= sci_get_line_count(editor
->sci
))
4701 gint current_line
= sci_get_current_line(editor
->sci
);
4703 line_no
= current_line
+ line_no
;
4706 pos
= sci_get_position_from_line(editor
->sci
, line_no
);
4707 return editor_goto_pos(editor
, pos
, TRUE
);
4711 /** Moves to position @a pos, switching to the document if necessary,
4712 * setting a marker if @a mark is set.
4714 * @param editor Editor.
4715 * @param pos The position.
4716 * @param mark Whether to set a mark on the position.
4717 * @return @c TRUE if action has been performed, otherwise @c FALSE.
4722 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4724 g_return_val_if_fail(editor
, FALSE
);
4725 if (G_UNLIKELY(pos
< 0))
4730 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4732 /* mark the tag with the yellow arrow */
4733 sci_marker_delete_all(editor
->sci
, 0);
4734 sci_set_marker_at_line(editor
->sci
, line
, 0);
4737 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4738 editor
->scroll_percent
= 0.25F
;
4740 /* finally switch to the page */
4741 document_show_tab(editor
->document
);
4747 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4749 GeanyEditor
*editor
= user_data
;
4751 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4752 * few lines only, maybe this could/should be done in Scintilla directly */
4753 if (event
->state
& GDK_MOD1_MASK
)
4755 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4758 else if (event
->state
& GDK_SHIFT_MASK
)
4760 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4762 sci_scroll_columns(editor
->sci
, amount
);
4766 return FALSE
; /* let Scintilla handle all other cases */
4770 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4772 GeanyDocument
*doc
= editor
->document
;
4774 if (!doc
->priv
->colourise_needed
)
4777 doc
->priv
->colourise_needed
= FALSE
;
4778 sci_colourise(editor
->sci
, 0, -1);
4780 /* now that the current document is colourised, fold points are now accurate,
4781 * so force an update of the current function/tag. */
4782 symbols_get_current_function(NULL
, NULL
);
4783 ui_update_statusbar(NULL
, -1);
4789 /* We only want to colourise just before drawing, to save startup time and
4790 * prevent unnecessary recolouring other documents after one is saved.
4791 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4792 * and "show" doesn't work). */
4793 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4795 GeanyEditor
*editor
= user_data
;
4797 editor_check_colourise(editor
);
4802 static gboolean
on_editor_expose_event(GtkWidget
*widget
, GdkEventExpose
*event
,
4805 GeanyEditor
*editor
= user_data
;
4807 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4808 * for some reason, maybe it's not necessary but just in case. */
4809 editor_check_colourise(editor
);
4814 #if GTK_CHECK_VERSION(3, 0, 0)
4815 static gboolean
on_editor_draw(GtkWidget
*widget
, cairo_t
*cr
, gpointer user_data
)
4817 return on_editor_expose_event(widget
, NULL
, user_data
);
4822 static void setup_sci_keys(ScintillaObject
*sci
)
4824 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4825 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4826 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4827 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4828 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4829 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4830 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4831 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4832 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4833 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4834 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4835 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4836 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4837 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4838 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4840 if (editor_prefs
.use_gtk_word_boundaries
)
4842 /* use GtkEntry-like word boundaries */
4843 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4844 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4845 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4847 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4848 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4849 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4850 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4851 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4852 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4854 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4858 /* registers a Scintilla image from a named icon from the theme */
4859 static gboolean
register_named_icon(ScintillaObject
*sci
, guint id
, const gchar
*name
)
4861 GError
*error
= NULL
;
4863 gint n_channels
, rowstride
, width
, height
;
4866 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU
, &size
, NULL
);
4867 pixbuf
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), name
, size
, 0, &error
);
4870 g_warning("failed to load icon '%s': %s", name
, error
->message
);
4871 g_error_free(error
);
4875 n_channels
= gdk_pixbuf_get_n_channels(pixbuf
);
4876 rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
4877 width
= gdk_pixbuf_get_width(pixbuf
);
4878 height
= gdk_pixbuf_get_height(pixbuf
);
4880 if (gdk_pixbuf_get_bits_per_sample(pixbuf
) != 8 ||
4881 ! gdk_pixbuf_get_has_alpha(pixbuf
) ||
4883 rowstride
!= width
* n_channels
)
4885 g_warning("incompatible image data for icon '%s'", name
);
4886 g_object_unref(pixbuf
);
4890 SSM(sci
, SCI_RGBAIMAGESETWIDTH
, width
, 0);
4891 SSM(sci
, SCI_RGBAIMAGESETHEIGHT
, height
, 0);
4892 SSM(sci
, SCI_REGISTERRGBAIMAGE
, id
, (sptr_t
)gdk_pixbuf_get_pixels(pixbuf
));
4894 g_object_unref(pixbuf
);
4899 /* Create new editor widget (scintilla).
4900 * @note The @c "sci-notify" signal is connected separately. */
4901 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4903 ScintillaObject
*sci
;
4905 sci
= SCINTILLA(scintilla_new());
4907 /* Scintilla doesn't support RTL languages properly and is primarily
4908 * intended to be used with LTR source code, so override the
4909 * GTK+ default text direction for the Scintilla widget. */
4910 gtk_widget_set_direction(GTK_WIDGET(sci
), GTK_TEXT_DIR_LTR
);
4912 gtk_widget_show(GTK_WIDGET(sci
));
4914 sci_set_codepage(sci
, SC_CP_UTF8
);
4915 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4916 /* disable scintilla provided popup menu */
4917 sci_use_popup(sci
, FALSE
);
4919 setup_sci_keys(sci
);
4921 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4922 sci_set_lines_wrapped(sci
, editor
->line_wrapping
);
4923 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4924 /*sci_set_caret_policy_y(sci, CARET_JUMPS | CARET_EVEN, 0);*/
4925 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4926 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4928 /* tag autocompletion images */
4929 register_named_icon(sci
, 1, "classviewer-var");
4930 register_named_icon(sci
, 2, "classviewer-method");
4932 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4933 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4936 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4938 #ifdef GDK_WINDOWING_QUARTZ
4939 /* "retina" (HiDPI) display support on OS X - requires disabling buffered draw */
4940 SSM(sci
, SCI_SETBUFFEREDDRAW
, 0, 0);
4943 /* only connect signals if this is for the document notebook, not split window */
4944 if (editor
->sci
== NULL
)
4946 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4947 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4948 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4949 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4950 #if GTK_CHECK_VERSION(3, 0, 0)
4951 g_signal_connect(sci
, "draw", G_CALLBACK(on_editor_draw
), editor
);
4953 g_signal_connect(sci
, "expose-event", G_CALLBACK(on_editor_expose_event
), editor
);
4960 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4961 * @param editor Editor settings.
4962 * @return The new widget.
4967 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4969 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4970 ScintillaObject
*old
, *sci
;
4971 GeanyIndentType old_indent_type
= editor
->indent_type
;
4972 gint old_indent_width
= editor
->indent_width
;
4974 /* temporarily change editor to use the new sci widget */
4976 sci
= create_new_sci(editor
);
4979 editor_set_indent(editor
, iprefs
->type
, iprefs
->width
);
4980 editor_set_font(editor
, interface_prefs
.editor_font
);
4981 editor_apply_update_prefs(editor
);
4983 /* if editor already had a widget, restore it */
4986 editor
->indent_type
= old_indent_type
;
4987 editor
->indent_width
= old_indent_width
;
4994 GeanyEditor
*editor_create(GeanyDocument
*doc
)
4996 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4997 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
4999 editor
->document
= doc
;
5000 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
5002 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
5003 editor
->line_wrapping
= get_project_pref(line_wrapping
);
5004 editor
->scroll_percent
= -1.0F
;
5005 editor
->line_breaking
= FALSE
;
5007 editor
->sci
= editor_create_widget(editor
);
5012 /* in case we need to free some fields in future */
5013 void editor_destroy(GeanyEditor
*editor
)
5019 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
5021 gchar
*f
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
5023 if (utils_str_equal(doc
->real_path
, f
))
5025 /* reload snippets */
5026 editor_snippets_free();
5027 editor_snippets_init();
5033 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
5037 g_return_val_if_fail(editor
, FALSE
);
5039 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
5042 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
5044 /* if no word part, complete normally */
5045 if (!check_partial_completion(editor
, entry
))
5046 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
5053 void editor_init(void)
5055 static GeanyIndentPrefs indent_prefs
;
5058 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
5059 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
5060 editor_prefs
.indentation
= &indent_prefs
;
5062 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
5063 * handler (on_editor_notify) is called */
5064 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
5066 f
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
5067 ui_add_config_file_menu_item(f
, NULL
, NULL
);
5069 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
5073 /* TODO: Should these be user-defined instead of hard-coded? */
5074 void editor_set_indentation_guides(GeanyEditor
*editor
)
5079 g_return_if_fail(editor
!= NULL
);
5081 if (! editor_prefs
.show_indent_guide
)
5083 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
5087 lexer
= sci_get_lexer(editor
->sci
);
5090 /* Lines added/removed are prefixed with +/- characters, so
5091 * those lines will not be shown with any indentation guides.
5092 * It can be distracting that only a few of lines in a diff/patch
5093 * file will show the guides. */
5098 /* These languages use indentation for control blocks; the "look forward" method works
5102 case SCLEX_MAKEFILE
:
5106 case SCLEX_PROPERTIES
:
5107 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
5109 mode
= SC_IV_LOOKFORWARD
;
5112 /* C-like (structured) languages benefit from the "look both" method */
5115 case SCLEX_PHPSCRIPT
:
5127 case SCLEX_FREEBASIC
:
5131 mode
= SC_IV_LOOKBOTH
;
5139 sci_set_indentation_guides(editor
->sci
, mode
);
5143 /* Apply non-document prefs that can change in the Preferences dialog */
5144 void editor_apply_update_prefs(GeanyEditor
*editor
)
5146 ScintillaObject
*sci
;
5148 g_return_if_fail(editor
!= NULL
);
5150 if (main_status
.quitting
)
5155 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
5156 editor_get_long_line_column(), editor_prefs
.long_line_color
);
5158 /* update indent width, tab width */
5159 editor_set_indent(editor
, editor
->indent_type
, editor
->indent_width
);
5160 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
5162 sci_assign_cmdkey(sci
, SCK_HOME
| (SCMOD_SHIFT
<< 16),
5163 editor_prefs
.smart_home_key
? SCI_VCHOMEEXTEND
: SCI_HOMEEXTEND
);
5164 sci_assign_cmdkey(sci
, SCK_HOME
| ((SCMOD_SHIFT
| SCMOD_ALT
) << 16),
5165 editor_prefs
.smart_home_key
? SCI_VCHOMERECTEXTEND
: SCI_HOMERECTEXTEND
);
5167 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
5168 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
5170 editor_set_indentation_guides(editor
);
5172 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
5173 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
5174 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
5175 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
);
5177 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
5180 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
5182 /* (dis)allow scrolling past end of document */
5183 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
5185 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
5189 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
5190 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
5192 ScintillaObject
*sci
= editor
->sci
;
5193 gint pos
= sci_get_position_from_line(sci
, line
);
5197 sci_insert_text(sci
, pos
, "\t");
5201 if (sci_get_char_at(sci
, pos
) == '\t')
5203 sci_set_selection(sci
, pos
, pos
+ 1);
5204 sci_replace_sel(sci
, "");
5206 else /* remove spaces only if no tabs */
5208 gint width
= sci_get_line_indentation(sci
, line
);
5210 width
-= editor_get_indent_prefs(editor
)->width
;
5211 sci_set_line_indentation(sci
, line
, width
);
5217 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
5219 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
5220 ScintillaObject
*sci
= editor
->sci
;
5222 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
5223 change_tab_indentation(editor
, line
, increase
);
5226 gint width
= sci_get_line_indentation(sci
, line
);
5228 width
+= increase
? iprefs
->width
: -iprefs
->width
;
5229 sci_set_line_indentation(sci
, line
, width
);
5234 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
5236 ScintillaObject
*sci
= editor
->sci
;
5237 gint caret_pos
, caret_line
, caret_offset
, caret_indent_pos
, caret_line_len
;
5238 gint anchor_pos
, anchor_line
, anchor_offset
, anchor_indent_pos
, anchor_line_len
;
5240 /* backup information needed to restore caret and anchor */
5241 caret_pos
= sci_get_current_position(sci
);
5242 anchor_pos
= SSM(sci
, SCI_GETANCHOR
, 0, 0);
5243 caret_line
= sci_get_line_from_position(sci
, caret_pos
);
5244 anchor_line
= sci_get_line_from_position(sci
, anchor_pos
);
5245 caret_offset
= caret_pos
- sci_get_position_from_line(sci
, caret_line
);
5246 anchor_offset
= anchor_pos
- sci_get_position_from_line(sci
, anchor_line
);
5247 caret_indent_pos
= sci_get_line_indent_position(sci
, caret_line
);
5248 anchor_indent_pos
= sci_get_line_indent_position(sci
, anchor_line
);
5249 caret_line_len
= sci_get_line_length(sci
, caret_line
);
5250 anchor_line_len
= sci_get_line_length(sci
, anchor_line
);
5252 if (sci_get_lines_selected(sci
) <= 1)
5254 editor_change_line_indent(editor
, sci_get_current_line(sci
), increase
);
5259 gint line
, lstart
, lend
;
5261 editor_select_lines(editor
, FALSE
);
5262 start
= sci_get_selection_start(sci
);
5263 end
= sci_get_selection_end(sci
);
5264 lstart
= sci_get_line_from_position(sci
, start
);
5265 lend
= sci_get_line_from_position(sci
, end
);
5266 if (end
== sci_get_length(sci
))
5267 lend
++; /* for last line with text on it */
5269 sci_start_undo_action(sci
);
5270 for (line
= lstart
; line
< lend
; line
++)
5272 editor_change_line_indent(editor
, line
, increase
);
5274 sci_end_undo_action(sci
);
5277 /* restore caret and anchor position */
5278 if (caret_pos
>= caret_indent_pos
)
5279 caret_offset
+= sci_get_line_length(sci
, caret_line
) - caret_line_len
;
5280 if (anchor_pos
>= anchor_indent_pos
)
5281 anchor_offset
+= sci_get_line_length(sci
, anchor_line
) - anchor_line_len
;
5283 SSM(sci
, SCI_SETCURRENTPOS
, sci_get_position_from_line(sci
, caret_line
) + caret_offset
, 0);
5284 SSM(sci
, SCI_SETANCHOR
, sci_get_position_from_line(sci
, anchor_line
) + anchor_offset
, 0);
5288 /** Gets snippet by name.
5290 * If @a editor is passed, returns a snippet specific to the document filetype.
5291 * If @a editor is @c NULL, returns a snippet from the default set.
5293 * @param editor Editor or @c NULL.
5294 * @param snippet_name Snippet name.
5295 * @return snippet or @c NULL if it was not found. Must not be freed.
5298 const gchar
*editor_find_snippet(GeanyEditor
*editor
, const gchar
*snippet_name
)
5300 const gchar
*subhash_name
= editor
? editor
->document
->file_type
->name
: "Default";
5301 GHashTable
*subhash
= g_hash_table_lookup(snippet_hash
, subhash_name
);
5303 return subhash
? g_hash_table_lookup(subhash
, snippet_name
) : NULL
;
5307 /** Replaces all special sequences in @a snippet and inserts it at @a pos.
5308 * If you insert at the current position, consider calling @c sci_scroll_caret()
5309 * after this function.
5315 void editor_insert_snippet(GeanyEditor
*editor
, gint pos
, const gchar
*snippet
)
5319 pattern
= g_string_new(snippet
);
5320 snippets_make_replacements(editor
, pattern
);
5321 editor_insert_text_block(editor
, pattern
->str
, pos
, -1, -1, TRUE
);
5322 g_string_free(pattern
, TRUE
);