2 * editor.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * Editor-related functions for @ref GeanyEditor.
24 * Geany uses the Scintilla editing widget, and this file is mostly built around
25 * Scintilla's functionality.
28 /* Callbacks for the Scintilla widget (ScintillaObject).
29 * Most important is the sci-notify callback, handled in on_editor_notification().
30 * This includes auto-indentation, comments, auto-completion, calltips, etc.
31 * Also some general Scintilla-related functions.
41 #include "callbacks.h"
43 #include "documentprivate.h"
44 #include "filetypesprivate.h"
45 #include "geanyobject.h"
46 #include "highlighting.h"
47 #include "keybindings.h"
49 #include "pluginextension.h"
51 #include "projectprivate.h"
52 #include "sciwrappers.h"
55 #include "templates.h"
65 #include <gdk/gdkkeysyms.h>
68 static GHashTable
*snippet_hash
= NULL
;
69 static GtkAccelGroup
*snippet_accel_group
= NULL
;
70 static gboolean autocomplete_scope_shown
= FALSE
;
72 static const gchar geany_cursor_marker
[] = "__GEANY_CURSOR_MARKER__";
74 /* holds word under the mouse or keyboard cursor */
75 static gchar current_word
[GEANY_MAX_WORD_LENGTH
];
77 /* Initialised in keyfile.c. */
78 GeanyEditorPrefs editor_prefs
;
80 EditorInfo editor_info
= {current_word
, -1};
90 } calltip
= {NULL
, FALSE
, NULL
, 0, 0, NULL
};
92 static gchar indent
[100];
95 static void on_new_line_added(GeanyEditor
*editor
);
96 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
97 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
98 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
99 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
100 static void close_block(GeanyEditor
*editor
, gint pos
);
101 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
102 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
103 const gchar
*wc
, gboolean stem
);
104 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
);
105 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
);
106 static void snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
);
107 static GeanyFiletype
*editor_get_filetype_at_line(GeanyEditor
*editor
, gint line
);
108 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
);
111 void editor_snippets_free(void)
113 g_hash_table_destroy(snippet_hash
);
114 gtk_window_remove_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
118 static void snippets_load(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
120 gsize i
, j
, len
= 0, len_keys
= 0;
121 gchar
**groups_user
, **groups_sys
;
122 gchar
**keys_user
, **keys_sys
;
126 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
128 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
130 /* first read all globally defined auto completions */
131 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
132 for (i
= 0; i
< len
; i
++)
134 if (strcmp(groups_sys
[i
], "Keybindings") == 0)
136 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
137 /* create new hash table for the read section (=> filetype) */
138 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
139 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
141 for (j
= 0; j
< len_keys
; j
++)
143 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
144 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
146 g_strfreev(keys_sys
);
148 g_strfreev(groups_sys
);
150 /* now read defined completions in user's configuration directory and add / replace them */
151 groups_user
= g_key_file_get_groups(userconfig
, &len
);
152 for (i
= 0; i
< len
; i
++)
154 if (strcmp(groups_user
[i
], "Keybindings") == 0)
156 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
158 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
160 { /* new key found, create hash table */
161 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
162 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
164 for (j
= 0; j
< len_keys
; j
++)
166 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
168 { /* value = NULL means the key doesn't yet exist, so insert */
169 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
170 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
173 { /* old key and value will be freed by destroy function (g_free) */
174 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
175 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
178 g_strfreev(keys_user
);
180 g_strfreev(groups_user
);
184 static gboolean
on_snippet_keybinding_activate(gchar
*key
)
186 GeanyDocument
*doc
= document_get_current();
189 if (!doc
|| !gtk_widget_has_focus(GTK_WIDGET(doc
->editor
->sci
)))
192 s
= snippets_find_completion_by_name(doc
->file_type
->name
, key
);
193 if (!s
) /* allow user to specify keybindings for "special" snippets */
195 GHashTable
*specials
= g_hash_table_lookup(snippet_hash
, "Special");
197 if (G_LIKELY(specials
!= NULL
))
198 s
= g_hash_table_lookup(specials
, key
);
206 editor_insert_snippet(doc
->editor
, sci_get_current_position(doc
->editor
->sci
), s
);
207 sci_scroll_caret(doc
->editor
->sci
);
213 static void add_kb(GKeyFile
*keyfile
, const gchar
*group
, gchar
**keys
)
219 for (i
= 0; i
< g_strv_length(keys
); i
++)
222 GdkModifierType mods
;
223 gchar
*accel_string
= g_key_file_get_value(keyfile
, group
, keys
[i
], NULL
);
225 gtk_accelerator_parse(accel_string
, &key
, &mods
);
227 if (key
== 0 && mods
== 0)
229 g_warning("Can not parse accelerator \"%s\" from user snippets.conf", accel_string
);
230 g_free(accel_string
);
233 g_free(accel_string
);
235 gtk_accel_group_connect(snippet_accel_group
, key
, mods
, 0,
236 g_cclosure_new_swap((GCallback
)on_snippet_keybinding_activate
,
237 g_strdup(keys
[i
]), CLOSURE_NOTIFY(g_free
)));
242 static void load_kb(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
244 const gchar kb_group
[] = "Keybindings";
245 gchar
**keys
= g_key_file_get_keys(userconfig
, kb_group
, NULL
, NULL
);
248 /* remove overridden keys from system keyfile */
249 foreach_strv(ptr
, keys
)
250 g_key_file_remove_key(sysconfig
, kb_group
, *ptr
, NULL
);
252 add_kb(userconfig
, kb_group
, keys
);
255 keys
= g_key_file_get_keys(sysconfig
, kb_group
, NULL
, NULL
);
256 add_kb(sysconfig
, kb_group
, keys
);
261 void editor_snippets_init(void)
263 gchar
*sysconfigfile
, *userconfigfile
;
264 GKeyFile
*sysconfig
= g_key_file_new();
265 GKeyFile
*userconfig
= g_key_file_new();
267 sysconfigfile
= g_build_filename(app
->datadir
, "snippets.conf", NULL
);
268 userconfigfile
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
270 /* check for old autocomplete.conf files (backwards compatibility) */
271 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
))
272 SETPTR(userconfigfile
, g_build_filename(app
->configdir
, "autocomplete.conf", NULL
));
274 /* load the actual config files */
275 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
276 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
278 snippets_load(sysconfig
, userconfig
);
280 /* setup snippet keybindings */
281 snippet_accel_group
= gtk_accel_group_new();
282 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
283 load_kb(sysconfig
, userconfig
);
285 g_free(sysconfigfile
);
286 g_free(userconfigfile
);
287 g_key_file_free(sysconfig
);
288 g_key_file_free(userconfig
);
292 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
295 GeanyEditor
*editor
= data
;
296 GeanyDocument
*doc
= editor
->document
;
298 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
299 * fake event to show the editor menu triggered by a key event where we want to use the
300 * text cursor position. */
301 if (event
->x
> 0.0 && event
->y
> 0.0)
302 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
303 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
305 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
307 if (event
->button
== 1)
309 guint state
= keybindings_get_modifiers(event
->state
);
311 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
313 gint ss
= sci_get_selection_start(editor
->sci
);
314 sci_set_selection_end(editor
->sci
, ss
);
316 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GEANY_PRIMARY_MOD_MASK
)
318 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
320 if (!symbols_goto_tag(doc
, editor_info
.click_pos
, TRUE
))
321 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
324 return document_check_disk_status(doc
, FALSE
);
327 /* calls the edit popup menu in the editor */
328 if (event
->button
== 3)
332 /* ensure the editor widget has the focus after this operation */
333 gtk_widget_grab_focus(widget
);
335 editor_find_current_word(editor
, editor_info
.click_pos
,
336 current_word
, sizeof current_word
, NULL
);
338 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
339 ui_update_popup_goto_items(can_goto
);
340 ui_update_popup_copy_items(doc
);
341 ui_update_insert_include_item(doc
, 0);
343 g_signal_emit_by_name(geany_object
, "update-editor-menu",
344 current_word
, editor_info
.click_pos
, doc
);
346 gtk_menu_popup_at_pointer(GTK_MENU(main_widgets
.editor_menu
), (GdkEvent
*) event
);
353 static gboolean
is_style_php(gint style
)
355 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
356 style
== SCE_HPHP_COMPLEX_VARIABLE
)
365 static gint
editor_get_long_line_type(void)
368 switch (app
->project
->priv
->long_line_behaviour
)
370 case 0: /* marker disabled */
372 case 1: /* use global settings */
374 case 2: /* custom (enabled) */
375 return editor_prefs
.long_line_type
;
378 if (!editor_prefs
.long_line_enabled
)
381 return editor_prefs
.long_line_type
;
385 static gint
editor_get_long_line_column(void)
387 if (app
->project
&& app
->project
->priv
->long_line_behaviour
!= 1 /* use global settings */)
388 return app
->project
->priv
->long_line_column
;
390 return editor_prefs
.long_line_column
;
394 #define get_project_pref(id)\
395 (app->project ? app->project->priv->id : editor_prefs.id)
397 static const GeanyEditorPrefs
*
398 get_default_prefs(void)
400 static GeanyEditorPrefs eprefs
;
402 eprefs
= editor_prefs
;
404 /* project overrides */
405 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(NULL
);
406 eprefs
.long_line_type
= editor_get_long_line_type();
407 eprefs
.long_line_column
= editor_get_long_line_column();
408 eprefs
.line_wrapping
= get_project_pref(line_wrapping
);
409 eprefs
.line_break_column
= get_project_pref(line_break_column
);
410 eprefs
.auto_continue_multiline
= get_project_pref(auto_continue_multiline
);
415 /* Gets the prefs for the editor.
416 * Prefs can be different according to project or document.
417 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
418 * settings may have changed, or if this function has been called for a different editor.
419 * @param editor The editor, or @c NULL to get the default prefs.
420 * @return The prefs. */
421 const GeanyEditorPrefs
*editor_get_prefs(GeanyEditor
*editor
)
423 static GeanyEditorPrefs eprefs
;
424 const GeanyEditorPrefs
*dprefs
= get_default_prefs();
426 /* Return the address of the default prefs to allow returning default and editor
427 * pref pointers without invalidating the contents of either. */
432 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(editor
);
433 /* add other editor & document overrides as needed */
438 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
440 ScintillaObject
*sci
;
443 g_return_if_fail(editor
!= NULL
);
446 /* When collapsing a fold range whose starting line is offscreen,
447 * scroll the starting line to display at the top of the view.
448 * Otherwise it can be confusing when the document scrolls down to hide
449 * the folded lines. */
450 if ((sci_get_fold_level(sci
, line
) & SC_FOLDLEVELNUMBERMASK
) > SC_FOLDLEVELBASE
&&
451 !(sci_get_fold_level(sci
, line
) & SC_FOLDLEVELHEADERFLAG
))
453 gint parent
= sci_get_fold_parent(sci
, line
);
454 gint first
= sci_get_first_visible_line(sci
);
456 parent
= SSM(sci
, SCI_VISIBLEFROMDOCLINE
, parent
, 0);
458 SSM(sci
, SCI_SETFIRSTVISIBLELINE
, parent
, 0);
461 /* find the fold header of the given line in case the one clicked isn't a fold point */
462 if (sci_get_fold_level(sci
, line
) & SC_FOLDLEVELHEADERFLAG
)
465 header
= sci_get_fold_parent(sci
, line
);
467 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
468 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
470 SSM(sci
, SCI_FOLDCHILDREN
, header
, SC_FOLDACTION_TOGGLE
);
474 SSM(sci
, SCI_FOLDLINE
, header
, SC_FOLDACTION_TOGGLE
);
479 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
481 /* left click to marker margin marks the line */
484 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
486 /*sci_marker_delete_all(editor->sci, 1);*/
487 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
489 /* left click on the folding margin to toggle folding state of current line */
490 else if (nt
->margin
== 2 && editor_prefs
.folding
)
492 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
493 editor_toggle_fold(editor
, line
, nt
->modifiers
);
498 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
500 ScintillaObject
*sci
= editor
->sci
;
501 gint pos
= sci_get_current_position(sci
);
503 if (nt
->updated
& (SC_UPDATE_H_SCROLL
| SC_UPDATE_V_SCROLL
))
505 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
506 SSM(sci
, SCI_AUTOCCANCEL
, 0, 0);
509 /* since Scintilla 2.24, SCN_UPDATEUI is also sent on scrolling though we don't need to handle
510 * this and so ignore every SCN_UPDATEUI events except for content and selection changes */
511 if (! (nt
->updated
& SC_UPDATE_CONTENT
) && ! (nt
->updated
& SC_UPDATE_SELECTION
))
514 /* undo / redo menu update */
515 ui_update_popup_reundo_items(editor
->document
);
517 /* brace highlighting */
518 editor_highlight_braces(editor
, pos
);
520 ui_update_statusbar(editor
->document
, pos
);
523 /** experimental code for inverting selections */
526 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
528 /* need to get colour from getstyleat(), but how? */
529 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
530 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
533 sci_get_style_at(sci
, pos
);
539 static void check_line_breaking(GeanyEditor
*editor
, gint pos
)
541 ScintillaObject
*sci
= editor
->sci
;
542 gint line
, lstart
, col
;
545 if (!editor
->line_breaking
|| sci_get_selection_mode(editor
->sci
) != SC_SEL_STREAM
)
548 col
= sci_get_col_from_position(sci
, pos
);
550 line
= sci_get_current_line(sci
);
552 lstart
= sci_get_position_from_line(sci
, line
);
554 /* use column instead of position which might be different with multibyte characters */
555 if (col
< get_project_pref(line_break_column
))
558 /* look for the last space before line_break_column */
559 pos
= sci_get_position_from_col(sci
, line
, get_project_pref(line_break_column
));
563 c
= sci_get_char_at(sci
, --pos
);
566 gint diff
, last_pos
, last_col
;
568 /* remember the distance between the current column and the last column on the line
569 * (we use column position in case the previous line gets altered, such as removing
570 * trailing spaces or in case it contains multibyte characters) */
571 last_pos
= sci_get_line_end_position(sci
, line
);
572 last_col
= sci_get_col_from_position(sci
, last_pos
);
573 diff
= last_col
- col
;
575 /* break the line after the space */
576 sci_set_current_position(sci
, pos
+ 1, FALSE
);
577 sci_cancel(sci
); /* don't select from completion list */
578 sci_send_command(sci
, SCI_NEWLINE
);
581 /* correct cursor position (might not be at line end) */
582 last_pos
= sci_get_line_end_position(sci
, line
);
583 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
584 /* last column - distance is the desired column, then retrieve its document position */
585 pos
= sci_get_position_from_col(sci
, line
, last_col
- diff
);
586 sci_set_current_position(sci
, pos
, FALSE
);
587 sci_scroll_caret(sci
);
594 static void show_autocomplete(ScintillaObject
*sci
, gsize rootlen
, GString
*words
)
596 /* hide autocompletion if only option is already typed */
597 if (rootlen
>= words
->len
||
598 (words
->str
[rootlen
] == '?' && rootlen
>= words
->len
- 2))
600 sci_send_command(sci
, SCI_AUTOCCANCEL
);
603 /* store whether a calltip is showing, so we can reshow it after autocompletion */
604 calltip
.set
= (gboolean
) SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
605 SSM(sci
, SCI_AUTOCSETORDER
, SC_ORDER_CUSTOM
, 0);
606 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
->str
);
610 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
612 ScintillaObject
*sci
= editor
->sci
;
614 g_return_if_fail(tags
);
618 GString
*words
= g_string_sized_new(150);
621 for (j
= 0; j
< tags
->len
; ++j
)
623 TMTag
*tag
= tags
->pdata
[j
];
628 g_string_append_c(words
, '\n');
630 if (j
== editor_prefs
.autocompletion_max_entries
)
632 g_string_append(words
, "...");
635 g_string_append(words
, tag
->name
);
637 group
= tm_parser_get_sidebar_group(tag
->lang
, tag
->type
);
638 if (group
>= 0 && tm_parser_get_sidebar_info(tag
->lang
, group
, &icon_id
))
641 sprintf(buf
, "?%u", icon_id
+ 1);
642 g_string_append(words
, buf
);
645 show_autocomplete(sci
, rootlen
, words
);
646 g_string_free(words
, TRUE
);
651 static gint
scope_autocomplete_suffix(ScintillaObject
*sci
, TMParserType lang
,
652 gint pos
, gboolean
*scope_sep
)
654 const gchar
*sep
= tm_parser_scope_separator(lang
);
655 const gsize max_len
= 3;
656 gboolean is_scope_sep
;
659 buf
= g_alloca(max_len
+ 1);
660 sci_get_text_range(sci
, pos
- max_len
, pos
, buf
);
662 is_scope_sep
= g_str_has_suffix(buf
, sep
);
664 *scope_sep
= is_scope_sep
;
667 return tm_parser_scope_autocomplete_suffix(lang
, buf
);
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 gint line
= sci_get_current_line(editor
->sci
) + 1;
710 GeanyFiletype
*ft
= editor
->document
->file_type
;
712 gboolean function
= FALSE
;
714 gboolean scope_sep_typed
= FALSE
;
715 gboolean ret
= FALSE
;
716 const gchar
*current_scope
;
717 gint autocomplete_suffix_len
;
719 if (autocomplete_scope_shown
)
721 /* move at the operator position */
724 /* allow for a space between word and operator */
725 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
729 autocomplete_suffix_len
= scope_autocomplete_suffix(sci
, ft
->lang
, pos
,
731 if (autocomplete_suffix_len
== 0)
734 pos
-= autocomplete_suffix_len
;
736 /* allow for a space between word and operator */
737 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
740 /* if function or array index, skip to matching brace */
741 brace_char
= sci_get_char_at(sci
, pos
- 1);
742 if (pos
> 0 && (brace_char
== ')' || brace_char
== ']'))
744 gint brace_pos
= sci_find_matching_brace(sci
, pos
- 1);
749 function
= brace_char
== ')';
752 /* allow for a space between opening brace and name */
753 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
757 name
= editor_get_word_at_pos(editor
, pos
, NULL
);
761 /* check if invoked on member */
763 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
765 member
= scope_autocomplete_suffix(sci
, ft
->lang
, pos
, NULL
) > 0;
767 if (symbols_get_current_scope(editor
->document
, ¤t_scope
) == -1)
769 tags
= tm_workspace_find_scope_members(editor
->document
->tm_file
, name
, function
,
770 member
, current_scope
, line
, scope_sep_typed
);
773 GPtrArray
*filtered
= g_ptr_array_new();
777 foreach_ptr_array(tag
, i
, tags
)
779 if (g_str_has_prefix(tag
->name
, root
))
780 g_ptr_array_add(filtered
, tag
);
783 if (filtered
->len
> 0)
785 show_tags_list(editor
, filtered
, rootlen
);
789 g_ptr_array_free(tags
, TRUE
);
790 g_ptr_array_free(filtered
, TRUE
);
798 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
800 ScintillaObject
*sci
= editor
->sci
;
801 gint pos
= sci_get_current_position(sci
);
806 { /* simple indentation (only for CR format) */
807 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
808 on_new_line_added(editor
);
812 { /* simple indentation (for CR/LF and LF format) */
813 on_new_line_added(editor
);
817 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
820 { /* close xml-tags */
821 handle_xml(editor
, pos
, nt
->ch
);
826 auto_close_chars(sci
, pos
, nt
->ch
);
827 if (!plugin_extension_calltips_provided(editor
->document
, NULL
))
829 editor_show_calltip(editor
, --pos
);
833 { /* hide calltips */
834 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0) &&
835 !plugin_extension_calltips_provided(editor
->document
, NULL
))
837 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
839 g_free(calltip
.text
);
851 auto_close_chars(sci
, pos
, nt
->ch
);
855 { /* closing bracket handling */
856 if (editor
->auto_indent
)
857 close_block(editor
, pos
- 1);
860 /* scope autocompletion */
862 case ':': /* C/C++ class:: syntax */
863 /* tag autocompletion */
865 editor_start_auto_complete(editor
, pos
, FALSE
);
868 plugin_extension_autocomplete_perform(editor
->document
, FALSE
);
869 plugin_extension_calltips_show(editor
->document
, FALSE
);
871 check_line_breaking(editor
, pos
);
875 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
876 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
877 gboolean force
, gint visLevels
, gint level
)
879 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
880 gint levelLine
= level
;
882 while (*line
<= lineMaxSubord
)
887 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
889 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
894 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
897 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
898 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
903 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
905 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
906 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
912 if (!sci_get_fold_expanded(sci
, *line
))
913 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
914 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
918 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
930 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
932 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
934 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
936 /* Adding a fold point */
937 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
938 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0))
939 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
942 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
944 if (! sci_get_fold_expanded(sci
, line
))
945 { /* Removing the fold from one that has been contracted so should expand
946 * otherwise lines are left invisible with no way to make them visible */
947 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
948 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0))
949 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
952 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
953 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
955 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0)) {
956 /* See if should still be hidden */
957 gint parentLine
= sci_get_fold_parent(sci
, line
);
960 SSM(sci
, SCI_SHOWLINES
, line
, line
);
962 else if (sci_get_fold_expanded(sci
, parentLine
) &&
963 sci_get_line_is_visible(sci
, parentLine
))
965 SSM(sci
, SCI_SHOWLINES
, line
, line
);
972 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
973 gboolean enforcePolicy
)
975 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
976 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
979 for (line
= lineStart
; line
<= lineEnd
; line
++)
981 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
986 static void auto_update_margin_width(GeanyEditor
*editor
)
988 gint next_linecount
= 1;
989 gint linecount
= sci_get_line_count(editor
->sci
);
990 GeanyDocument
*doc
= editor
->document
;
992 while (next_linecount
<= linecount
)
993 next_linecount
*= 10;
995 if (editor
->document
->priv
->line_count
!= next_linecount
)
997 doc
->priv
->line_count
= next_linecount
;
998 sci_set_line_numbers(editor
->sci
, TRUE
);
1003 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
1005 gint pos
= sci_get_current_position(sci
);
1007 sci_insert_text(sci
, pos
, text
);
1008 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
1012 /* Complete the next word part from @a entry */
1013 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
1015 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
1017 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
1018 stem
= current_word
;
1019 if (strstr(text
, stem
) != text
)
1020 return FALSE
; /* shouldn't happen */
1021 if (strlen(text
) <= strlen(stem
))
1024 text
+= strlen(stem
); /* skip stem */
1025 ptr
= strstr(text
+ 1, "_");
1029 partial_complete(editor
->sci
, text
);
1035 foreach_str(ptr
, text
+ 1)
1039 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
1042 partial_complete(editor
->sci
, text
);
1051 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
1052 * Plugins can connect to the "editor-notify" signal. */
1053 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
1054 gpointer scnt
, gpointer data
)
1056 GeanyEditor
*editor
= data
;
1059 g_return_if_fail(editor
!= NULL
);
1061 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
1065 /* recalculate margins width */
1066 static void update_margins(ScintillaObject
*sci
)
1068 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
);
1069 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
1070 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
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 * https://scintilla.sourceforge.io/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 if (!plugin_extension_calltips_provided(doc
, NULL
))
1165 request_reshowing_calltip(nt
);
1168 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
1171 case SCN_URIDROPPED
:
1172 if (nt
->text
!= NULL
)
1174 document_open_file_list(nt
->text
, strlen(nt
->text
));
1178 case SCN_CALLTIPCLICK
:
1179 if (!plugin_extension_calltips_provided(doc
, NULL
) && nt
->position
> 0)
1181 switch (nt
->position
)
1183 case 1: /* up arrow */
1184 if (calltip
.tag_index
> 0)
1185 calltip
.tag_index
--;
1188 case 2: calltip
.tag_index
++; break; /* down arrow */
1190 editor_show_calltip(editor
, -1);
1195 update_margins(sci
);
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 @nullable 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
);
1324 case SCLEX_HTML
: /* for PHP & JS */
1325 case SCLEX_PHPSCRIPT
:
1326 case SCLEX_PASCAL
: /* for multiline comments? */
1340 /* Read indent chars for the line that pos is on into indent global variable.
1341 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1342 * instead in any new code. */
1343 static void read_indent(GeanyEditor
*editor
, gint pos
)
1345 ScintillaObject
*sci
= editor
->sci
;
1346 guint i
, len
, j
= 0;
1350 line
= sci_get_line_from_position(sci
, pos
);
1352 len
= sci_get_line_length(sci
, line
);
1353 linebuf
= sci_get_line(sci
, line
);
1355 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1357 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1358 indent
[j
++] = linebuf
[i
];
1367 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1369 gint start
= sci_get_position_from_line(sci
, line
);
1370 gint end
= sci_get_line_end_position(sci
, line
) - 1;
1371 gint lexer
= sci_get_lexer(sci
);
1375 for (pos
= end
; pos
>= start
&& count
< 1; pos
--)
1377 if (highlighting_is_code_style(lexer
, sci_get_style_at(sci
, pos
)))
1379 gchar c
= sci_get_char_at(sci
, pos
);
1388 return count
> 0 ? 1 : 0;
1392 /* gets the last code position on a line
1393 * warning: if there is no code position on the line, returns the start position */
1394 static gint
get_sci_line_code_end_position(ScintillaObject
*sci
, gint line
)
1396 gint start
= sci_get_position_from_line(sci
, line
);
1397 gint lexer
= sci_get_lexer(sci
);
1400 for (pos
= sci_get_line_end_position(sci
, line
) - 1; pos
> start
; pos
--)
1402 gint style
= sci_get_style_at(sci
, pos
);
1404 if (highlighting_is_code_style(lexer
, style
) && ! isspace(sci_get_char_at(sci
, pos
)))
1412 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1414 gint last_char
= get_sci_line_code_end_position(sci
, line
);
1416 /* add extra indentation for Python after colon */
1417 if (sci_get_char_at(sci
, last_char
) == ':' &&
1418 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1426 static gint
get_xml_indent(ScintillaObject
*sci
, gint line
)
1428 gboolean need_close
= FALSE
;
1429 gint end
= get_sci_line_code_end_position(sci
, line
);
1432 /* don't indent if there's a closing tag to the right of the cursor */
1433 pos
= sci_get_current_position(sci
);
1434 if (sci_get_char_at(sci
, pos
) == '<' &&
1435 sci_get_char_at(sci
, pos
+ 1) == '/')
1438 if (sci_get_char_at(sci
, end
) == '>' &&
1439 sci_get_char_at(sci
, end
- 1) != '/')
1441 gint style
= sci_get_style_at(sci
, end
);
1443 if (style
== SCE_H_TAG
|| style
== SCE_H_TAGUNKNOWN
)
1445 gint start
= sci_get_position_from_line(sci
, line
);
1446 gchar
*line_contents
= sci_get_contents_range(sci
, start
, end
+ 1);
1447 gchar
*opened_tag_name
= utils_find_open_xml_tag(line_contents
, end
+ 1 - start
);
1449 if (!EMPTY(opened_tag_name
))
1452 if (sci_get_lexer(sci
) == SCLEX_HTML
&& utils_is_short_html_tag(opened_tag_name
))
1455 g_free(line_contents
);
1456 g_free(opened_tag_name
);
1460 return need_close
? 1 : 0;
1464 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1466 ScintillaObject
*sci
= editor
->sci
;
1468 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1470 g_return_val_if_fail(line
>= 0, 0);
1472 size
= sci_get_line_indentation(sci
, line
);
1474 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1476 gint additional_indent
= 0;
1478 if (lexer_has_braces(sci
))
1479 additional_indent
= iprefs
->width
* get_brace_indent(sci
, line
);
1480 else if (sci_get_lexer(sci
) == SCLEX_PYTHON
) /* Python/Cython */
1481 additional_indent
= iprefs
->width
* get_python_indent(sci
, line
);
1483 /* HTML lexer "has braces" because of PHP and JavaScript. If get_brace_indent() did not
1484 * recommend us to insert additional indent, we are probably not in PHP/JavaScript chunk and
1485 * should make the XML-related check */
1486 if (additional_indent
== 0 &&
1487 (sci_get_lexer(sci
) == SCLEX_HTML
||
1488 sci_get_lexer(sci
) == SCLEX_XML
) &&
1489 editor
->document
->file_type
->priv
->xml_indent_tags
)
1491 size
+= iprefs
->width
* get_xml_indent(sci
, line
);
1494 size
+= additional_indent
;
1500 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
)
1502 ScintillaObject
*sci
= editor
->sci
;
1503 gint line_indent
= sci_get_line_indentation(sci
, line
);
1504 gint size
= get_indent_size_after_line(editor
, line
);
1505 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1511 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
&& size
== line_indent
)
1513 /* support tab indents, space aligns style - copy last line 'indent' exactly */
1514 gint start
= sci_get_position_from_line(sci
, line
);
1515 gint end
= sci_get_line_indent_position(sci
, line
);
1517 text
= sci_get_contents_range(sci
, start
, end
);
1521 text
= get_whitespace(iprefs
, size
);
1523 sci_add_text(sci
, text
);
1528 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
)
1530 const gchar
*closing_char
= NULL
;
1533 if (utils_isbrace(c
, 0))
1534 end_pos
= sci_find_matching_brace(sci
, pos
- 1);
1539 if ((editor_prefs
.autoclose_chars
& GEANY_AC_PARENTHESIS
) && end_pos
== -1)
1543 if ((editor_prefs
.autoclose_chars
& GEANY_AC_CBRACKET
) && end_pos
== -1)
1547 if ((editor_prefs
.autoclose_chars
& GEANY_AC_SBRACKET
) && end_pos
== -1)
1551 if (editor_prefs
.autoclose_chars
& GEANY_AC_SQUOTE
)
1555 if (editor_prefs
.autoclose_chars
& GEANY_AC_DQUOTE
)
1556 closing_char
= "\"";
1560 if (closing_char
!= NULL
)
1562 sci_add_text(sci
, closing_char
);
1563 sci_set_current_position(sci
, pos
, TRUE
);
1568 /* Finds a corresponding matching brace to the given pos
1569 * (this is taken from Scintilla Editor.cxx,
1570 * fit to work with close_block) */
1571 static gint
brace_match(ScintillaObject
*sci
, gint pos
)
1573 gchar chBrace
= sci_get_char_at(sci
, pos
);
1574 gchar chSeek
= utils_brace_opposite(chBrace
);
1576 gint direction
= -1;
1581 /* Hack: we need the style at @p pos but it isn't computed yet, so force styling
1582 * of this very position */
1583 sci_colourise(sci
, pos
, pos
+ 1);
1585 styBrace
= sci_get_style_at(sci
, pos
);
1587 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1591 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1593 chAtPos
= sci_get_char_at(sci
, pos
);
1594 styAtPos
= sci_get_style_at(sci
, pos
);
1596 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1598 if (chAtPos
== chBrace
)
1600 if (chAtPos
== chSeek
)
1611 /* Called after typing '}'. */
1612 static void close_block(GeanyEditor
*editor
, gint pos
)
1614 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1615 gint x
= 0, cnt
= 0;
1616 gint line
, line_len
;
1618 ScintillaObject
*sci
;
1619 gint line_indent
, last_indent
;
1621 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1623 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1627 if (! lexer_has_braces(sci
))
1630 line
= sci_get_line_from_position(sci
, pos
);
1631 line_len
= sci_get_line_end_position(sci
, line
) - sci_get_position_from_line(sci
, line
);
1633 /* check that the line is empty, to not kill text in the line */
1634 line_buf
= sci_get_line(sci
, line
);
1635 line_buf
[line_len
] = '\0';
1636 while (x
< line_len
)
1638 if (isspace(line_buf
[x
]))
1644 if ((line_len
- 1) != cnt
)
1647 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1649 gint start_brace
= brace_match(sci
, pos
);
1651 if (start_brace
>= 0)
1654 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1655 gint size
= sci_get_line_indentation(sci
, brace_line
);
1656 gchar
*ind
= get_whitespace(iprefs
, size
);
1657 gchar
*text
= g_strconcat(ind
, "}", NULL
);
1659 line_start
= sci_get_position_from_line(sci
, line
);
1660 sci_set_anchor(sci
, line_start
);
1661 sci_replace_sel(sci
, text
);
1666 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1669 /* GEANY_AUTOINDENT_CURRENTCHARS */
1670 line_indent
= sci_get_line_indentation(sci
, line
);
1671 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1673 if (line_indent
< last_indent
)
1675 line_indent
-= iprefs
->width
;
1676 line_indent
= MAX(0, line_indent
);
1677 sci_set_line_indentation(sci
, line
, line_indent
);
1681 /* checks whether @p c is an ASCII character (e.g. < 0x80) */
1682 #define IS_ASCII(c) (((unsigned char)(c)) < 0x80)
1685 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1686 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1687 * position can be -1, then the current position is used.
1688 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1689 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
1690 const gchar
*wc
, gboolean stem
)
1692 gint line
, line_start
, startword
, endword
;
1694 ScintillaObject
*sci
;
1696 g_return_if_fail(editor
!= NULL
);
1700 pos
= sci_get_current_position(sci
);
1702 line
= sci_get_line_from_position(sci
, pos
);
1703 line_start
= sci_get_position_from_line(sci
, line
);
1704 startword
= pos
- line_start
;
1705 endword
= pos
- line_start
;
1708 chunk
= sci_get_line(sci
, line
);
1711 wc
= GEANY_WORDCHARS
;
1713 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1714 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1715 * TODO: improve this code */
1716 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || ! IS_ASCII(chunk
[startword
- 1])))
1720 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || ! IS_ASCII(chunk
[endword
])))
1724 if (startword
!= endword
)
1726 chunk
[endword
] = '\0';
1728 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1731 g_strlcpy(word
, "", wordlen
);
1737 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1738 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1739 * position can be -1, then the current position is used.
1740 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1741 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
1744 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1748 /* Same as editor_find_current_word() but uses editor's word boundaries to decide what the word
1749 * is. This should be used e.g. to get the word to search for */
1750 void editor_find_current_word_sciwc(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
)
1755 g_return_if_fail(editor
!= NULL
);
1758 pos
= sci_get_current_position(editor
->sci
);
1760 start
= sci_word_start_position(editor
->sci
, pos
, TRUE
);
1761 end
= sci_word_end_position(editor
->sci
, pos
, TRUE
);
1763 if (start
== end
) /* caret in whitespaces sequence */
1767 if ((guint
)(end
- start
) >= wordlen
)
1768 end
= start
+ (wordlen
- 1);
1769 sci_get_text_range(editor
->sci
, start
, end
, word
);
1775 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1776 * Otherwise NULL is returned.
1777 * Additional wordchars can be specified to define what to consider as a word.
1779 * @param editor The editor to operate on.
1780 * @param pos The position where the word should be read from.
1781 * May be @c -1 to use the current position.
1782 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1783 * as part of a word. May be @c NULL to use the default wordchars,
1784 * see @ref GEANY_WORDCHARS.
1786 * @return @nullable A newly-allocated string containing the word at the given @a pos or @c NULL.
1787 * Should be freed when no longer needed.
1792 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1794 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1796 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1798 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1800 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1804 /* Read the word up to position @a pos. */
1805 static const gchar
*
1806 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1808 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1810 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1812 return (*word
) ? word
: NULL
;
1816 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1818 gint orig_pos
= pos
;
1820 while (pos
>= 0 && pos
> orig_pos
- 300)
1822 gchar c
= sci_get_char_at(sci
, pos
);
1823 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1831 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1834 gint orig_pos
= pos
;
1836 while (pos
> 0 && pos
> orig_pos
- 300)
1838 gchar c
= sci_get_char_at(sci
, pos
);
1840 if (c
== ')') brackets
++;
1841 else if (c
== '(') brackets
--;
1842 if (brackets
< 0) return pos
; /* found start bracket */
1849 static GPtrArray
*get_constructor_tags(GeanyFiletype
*ft
, TMTag
*tag
,
1850 const gchar
*constructor_method
)
1852 if (constructor_method
&& (tag
->type
== tm_tag_class_t
|| tag
->type
== tm_tag_struct_t
))
1854 const TMTagType arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1855 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1856 const gchar
*scope_sep
= tm_parser_scope_separator(ft
->lang
);
1857 gchar
*scope
= EMPTY(tag
->scope
) ? g_strdup(tag
->name
) :
1858 g_strjoin(scope_sep
, tag
->scope
, tag
->name
, NULL
);
1859 GPtrArray
*constructor_tags
;
1861 constructor_tags
= tm_workspace_find(constructor_method
, scope
, arg_types
, NULL
, ft
->lang
);
1863 if (constructor_tags
->len
!= 0)
1864 { /* found constructor tag, so use it instead of the class tag */
1865 return constructor_tags
;
1869 g_ptr_array_free(constructor_tags
, TRUE
);
1876 static void update_tag_name_and_scope_for_calltip(const gchar
*word
, TMTag
*tag
,
1877 const gchar
*constructor_method
,
1878 const gchar
**tag_name
, const gchar
**scope
)
1880 if (tag_name
== NULL
|| scope
== NULL
)
1883 /* Remove scope and replace name with the current calltip word if the current tag
1884 * is the constructor method of the current calltip word, e.g. for Python:
1885 * "SomeClass.__init__ (self, arg1, ...)" will be changed to "SomeClass (self, arg1, ...)" */
1886 if (constructor_method
&&
1887 utils_str_equal(constructor_method
, tag
->name
) &&
1888 !utils_str_equal(word
, tag
->name
))
1896 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1898 const gchar
*constructor_method
;
1901 GString
*str
= NULL
;
1904 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1906 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1907 tags
= tm_workspace_find(word
, NULL
, tm_tag_max_t
, NULL
, ft
->lang
);
1910 g_ptr_array_free(tags
, TRUE
);
1914 tag
= TM_TAG(tags
->pdata
[0]);
1916 /* user typed e.g. 'a = Classname(' in Python so lookup __init__() arguments */
1917 constructor_method
= tm_parser_get_constructor_method(tag
->lang
);
1918 if (constructor_method
)
1920 GPtrArray
*constructor_tags
= get_constructor_tags(ft
, tag
, constructor_method
);
1921 if (constructor_tags
)
1923 g_ptr_array_free(tags
, TRUE
);
1924 tags
= constructor_tags
;
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 const gchar
*tag_name
= tag
->name
;
1965 const gchar
*scope
= tag
->scope
;
1968 update_tag_name_and_scope_for_calltip(word
, tag
, constructor_method
, &tag_name
, &scope
);
1969 f
= tm_parser_format_function(tag
->lang
, tag_name
, tag
->arglist
, tag
->var_type
, scope
);
1970 str
= g_string_new(NULL
);
1971 if (calltip
.tag_index
> 0)
1972 g_string_prepend(str
, "\001 "); /* up arrow */
1973 g_string_append(str
, f
);
1976 else /* add a down arrow */
1978 if (calltip
.tag_index
> 0) /* already have an up arrow */
1979 g_string_insert_c(str
, 1, '\002');
1981 g_string_prepend(str
, "\002 ");
1986 g_ptr_array_free(tags
, TRUE
);
1990 gchar
*result
= str
->str
;
1992 g_string_free(str
, FALSE
);
1999 /* use pos = -1 to search for the previous unmatched open bracket. */
2000 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
2002 gint orig_pos
= pos
; /* the position for the calltip */
2005 gchar word
[GEANY_MAX_WORD_LENGTH
];
2007 ScintillaObject
*sci
;
2009 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2010 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
2014 lexer
= sci_get_lexer(sci
);
2018 /* position of '(' is unknown, so go backwards from current position to find it */
2019 pos
= sci_get_current_position(sci
);
2022 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
2023 find_start_bracket(sci
, pos
);
2028 /* the style 1 before the brace (which may be highlighted) */
2029 style
= sci_get_style_at(sci
, pos
- 1);
2030 if (! highlighting_is_code_style(lexer
, style
))
2033 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
2036 /* skip possible generic/template specification, like foo<int>() */
2037 if (sci_get_char_at(sci
, pos
- 1) == '>')
2039 pos
= sci_find_matching_brace(sci
, pos
- 1);
2043 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
2048 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
2049 if (word
[0] == '\0')
2052 str
= find_calltip(word
, editor
->document
->file_type
);
2055 g_free(calltip
.text
); /* free the old calltip */
2057 calltip
.pos
= orig_pos
;
2060 utils_wrap_string(calltip
.text
, -1);
2061 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
2068 /* Current document & global tags autocompletion */
2070 autocomplete_tags(GeanyEditor
*editor
, GeanyFiletype
*ft
, const gchar
*root
, gsize rootlen
)
2072 GeanyDocument
*doc
= editor
->document
;
2073 const gchar
*current_scope
= NULL
;
2078 g_return_val_if_fail(editor
&& doc
, FALSE
);
2080 symbols_get_current_function(doc
, ¤t_scope
);
2081 current_line
= sci_get_current_line(editor
->sci
) + 1;
2083 tags
= tm_workspace_find_prefix(root
, doc
->tm_file
, current_line
, current_scope
,
2084 editor_prefs
.autocompletion_max_entries
);
2085 found
= tags
->len
> 0;
2087 show_tags_list(editor
, tags
, rootlen
);
2088 g_ptr_array_free(tags
, TRUE
);
2094 static gboolean
autocomplete_check_html(GeanyEditor
*editor
, gint style
, gint pos
)
2096 GeanyFiletype
*ft
= editor
->document
->file_type
;
2097 gboolean
try = FALSE
;
2099 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
2100 * (everything after SCE_HJ_START is for embedded scripting languages) */
2101 if (ft
->id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
2103 else if (sci_get_lexer(editor
->sci
) == SCLEX_XML
&& style
< SCE_HJ_START
)
2105 else if (ft
->id
== GEANY_FILETYPES_PHP
)
2107 /* use entity completion when style is outside of PHP styles */
2108 if (! is_style_php(style
))
2113 gchar root
[GEANY_MAX_WORD_LENGTH
];
2116 read_current_word(editor
, pos
, root
, sizeof(root
), GEANY_WORDCHARS
"&", TRUE
);
2118 /* Allow something like ""some text"".
2119 * for entity completion we want to have completion for '&' within words. */
2120 tmp
= strchr(root
, '&');
2123 return autocomplete_tags(editor
, filetypes_index(GEANY_FILETYPES_HTML
), tmp
, strlen(tmp
));
2130 /* Algorithm based on based on Scite's StartAutoCompleteWord()
2131 * @returns a sorted list of words matching @p root */
2132 static GSList
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
2135 gint len
, current
, word_end
;
2136 gint pos_find
, flags
;
2139 GSList
*words
= NULL
;
2140 struct Sci_TextToFind ttf
;
2142 len
= sci_get_length(sci
);
2143 current
= sci_get_current_position(sci
) - rootlen
;
2145 ttf
.lpstrText
= root
;
2147 ttf
.chrg
.cpMax
= len
;
2148 ttf
.chrgText
.cpMin
= 0;
2149 ttf
.chrgText
.cpMax
= 0;
2150 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
2152 /* search the whole document for the word root and collect results */
2153 pos_find
= SSM(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2154 while (pos_find
>= 0 && pos_find
< len
)
2156 word_end
= pos_find
+ rootlen
;
2157 if (pos_find
!= current
)
2159 word_end
= sci_word_end_position(sci
, word_end
, TRUE
);
2161 word_length
= word_end
- pos_find
;
2162 if (word_length
> rootlen
)
2164 word
= sci_get_contents_range(sci
, pos_find
, word_end
);
2165 /* search whether we already have the word in, otherwise add it */
2166 if (g_slist_find_custom(words
, word
, (GCompareFunc
)strcmp
) != NULL
)
2170 words
= g_slist_prepend(words
, word
);
2174 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
2178 ttf
.chrg
.cpMin
= word_end
;
2179 pos_find
= SSM(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2182 return g_slist_sort(words
, (GCompareFunc
)utils_str_casecmp
);
2186 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
2188 ScintillaObject
*sci
= editor
->sci
;
2189 GSList
*words
, *node
;
2193 words
= get_doc_words(sci
, root
, rootlen
);
2196 SSM(sci
, SCI_AUTOCCANCEL
, 0, 0);
2200 str
= g_string_sized_new(rootlen
* 2 * 10);
2201 foreach_slist(node
, words
)
2203 g_string_append(str
, node
->data
);
2206 g_string_append_c(str
, '\n');
2209 if (n_words
>= editor_prefs
.autocompletion_max_entries
)
2210 g_string_append(str
, "\n...");
2212 g_slist_free(words
);
2214 show_autocomplete(sci
, rootlen
, str
);
2215 g_string_free(str
, TRUE
);
2220 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
2222 gint rootlen
, lexer
, style
;
2224 gchar cword
[GEANY_MAX_WORD_LENGTH
];
2225 ScintillaObject
*sci
;
2226 gboolean ret
= FALSE
;
2227 const gchar
*wordchars
;
2230 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2232 if (plugin_extension_autocomplete_provided(editor
->document
, NULL
))
2235 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
2238 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
2239 * necessary styling information */
2240 if (G_UNLIKELY(pos
< 2))
2244 ft
= editor
->document
->file_type
;
2246 lexer
= sci_get_lexer(sci
);
2247 style
= sci_get_style_at(sci
, pos
- 2);
2249 /* don't autocomplete in comments and strings */
2250 if (!force
&& !highlighting_is_code_style(lexer
, style
))
2253 ret
= autocomplete_check_html(editor
, style
, pos
);
2255 if (ft
->id
== GEANY_FILETYPES_LATEX
)
2256 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
2257 else if (ft
->id
== GEANY_FILETYPES_CSS
)
2258 wordchars
= GEANY_WORDCHARS
"-"; /* add - because they are part of property names */
2260 wordchars
= GEANY_WORDCHARS
;
2262 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, TRUE
);
2264 rootlen
= strlen(root
);
2268 if (autocomplete_scope_shown
)
2270 autocomplete_scope_shown
= FALSE
;
2272 sci_send_command(sci
, SCI_AUTOCCANCEL
);
2277 ret
= autocomplete_scope(editor
, root
, rootlen
);
2278 if (!ret
&& autocomplete_scope_shown
)
2279 sci_send_command(sci
, SCI_AUTOCCANCEL
);
2280 autocomplete_scope_shown
= ret
;
2283 if (!ret
&& rootlen
> 0)
2285 if (ft
->id
== GEANY_FILETYPES_PHP
&& style
== SCE_HPHP_DEFAULT
&&
2286 rootlen
== 3 && strcmp(root
, "php") == 0 && pos
>= 5 &&
2287 sci_get_char_at(sci
, pos
- 5) == '<' &&
2288 sci_get_char_at(sci
, pos
- 4) == '?')
2290 /* nothing, don't complete PHP open tags */
2294 /* force is set when called by keyboard shortcut, otherwise start at the
2295 * editor_prefs.symbolcompletion_min_chars'th char */
2296 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
2298 /* complete tags, except if forcing when completion is already visible */
2299 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
2300 ret
= autocomplete_tags(editor
, editor
->document
->file_type
, root
, rootlen
);
2302 /* If forcing and there's nothing else to show, complete from words in document */
2303 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2304 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2315 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2317 gchar
*result
= NULL
;
2320 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2322 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2325 result
= g_hash_table_lookup(tmp
, name
);
2327 /* whether nothing is set for the current filetype(tmp is NULL) or
2328 * the particular completion for this filetype is not set (result is NULL) */
2329 if (tmp
== NULL
|| result
== NULL
)
2331 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2334 result
= g_hash_table_lookup(tmp
, name
);
2337 /* if result is still NULL here, no completion could be found */
2339 /* result is owned by the hash table and will be freed when the table will destroyed */
2344 static void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2347 GString
*pattern
= user_data
;
2349 g_return_if_fail(key
!= NULL
);
2350 g_return_if_fail(value
!= NULL
);
2352 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2354 utils_string_replace_all(pattern
, needle
, (gchar
*) value
);
2359 static void fix_indentation(GeanyEditor
*editor
, GString
*buf
)
2361 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2364 gint cflags
= G_REGEX_MULTILINE
;
2366 /* transform leading tabs into indent widths (in spaces) */
2367 whitespace
= g_strnfill(iprefs
->width
, ' ');
2368 regex
= g_regex_new("^ *(\t)", cflags
, 0, NULL
);
2369 while (utils_string_regex_replace_all(buf
, regex
, 1, whitespace
, TRUE
));
2370 g_regex_unref(regex
);
2372 /* remaining tabs are for alignment */
2373 if (iprefs
->type
!= GEANY_INDENT_TYPE_TABS
)
2374 utils_string_replace_all(buf
, "\t", whitespace
);
2376 /* use leading tabs */
2377 if (iprefs
->type
!= GEANY_INDENT_TYPE_SPACES
)
2381 /* for tabs+spaces mode we want the real tab width, not indent width */
2382 SETPTR(whitespace
, g_strnfill(sci_get_tab_width(editor
->sci
), ' '));
2383 str
= g_strdup_printf("^\t*(%s)", whitespace
);
2385 regex
= g_regex_new(str
, cflags
, 0, NULL
);
2386 while (utils_string_regex_replace_all(buf
, regex
, 1, "\t", TRUE
));
2387 g_regex_unref(regex
);
2396 Sci_Position start
, len
;
2400 #define CURSOR_PLACEHOLDER "_" /* Would rather use … but not all docs are unicode */
2403 /* Replaces the internal cursor markers with the placeholder suitable for
2404 * display. Except for the first cursor if indicator_for_first is FALSE,
2405 * which is simply deleted.
2407 * Returns insertion points as SelectionRange list, so that the caller
2408 * can use the positions (currently for indicators). */
2409 static GSList
*replace_cursor_markers(GeanyEditor
*editor
, GString
*template,
2410 gboolean indicator_for_first
)
2413 GSList
*temp_list
= NULL
;
2414 gint cursor_steps
= 0;
2415 SelectionRange
*sel
;
2419 cursor_steps
= utils_string_find(template, cursor_steps
, -1, geany_cursor_marker
);
2420 if (cursor_steps
== -1)
2423 sel
= g_new0(SelectionRange
, 1);
2424 sel
->start
= cursor_steps
;
2425 g_string_erase(template, cursor_steps
, strlen(geany_cursor_marker
));
2426 if (i
> 0 || indicator_for_first
)
2428 g_string_insert(template, cursor_steps
, CURSOR_PLACEHOLDER
);
2429 sel
->len
= sizeof(CURSOR_PLACEHOLDER
) - 1;
2432 temp_list
= g_slist_append(temp_list
, sel
);
2439 /** Inserts text, replacing \\t tab chars (@c 0x9) and \\n newline chars (@c 0xA)
2440 * accordingly for the document.
2441 * - Leading tabs are replaced with the correct indentation.
2442 * - Non-leading tabs are replaced with spaces (except when using 'Tabs' indent type).
2443 * - Newline chars are replaced with the correct line ending string.
2444 * This is very useful for inserting code without having to handle the indent
2445 * type yourself (Tabs & Spaces mode can be tricky).
2446 * @param editor Editor.
2447 * @param text Intended as e.g. @c "if (foo)\n\tbar();".
2448 * @param insert_pos Document position to insert text at.
2449 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2450 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2451 * -1 to read the indent size from the line with @a insert_pos on it.
2452 * @param replace_newlines Whether to replace newlines. If
2453 * newlines have been replaced already, this should be false, to avoid errors e.g. on Windows.
2454 * @warning Make sure all \\t tab chars in @a text are intended as indent widths or alignment,
2455 * not hard tabs, as those won't be preserved.
2456 * @note This doesn't scroll the cursor in view afterwards. **/
2458 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2459 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2461 ScintillaObject
*sci
= editor
->sci
;
2462 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2464 const gchar
*eol
= editor_get_eol_char(editor
);
2465 GSList
*jump_locs
, *item
;
2467 g_return_if_fail(text
);
2468 g_return_if_fail(editor
!= NULL
);
2469 g_return_if_fail(insert_pos
>= 0);
2471 buf
= g_string_new(text
);
2473 if (cursor_index
>= 0)
2474 g_string_insert(buf
, cursor_index
, geany_cursor_marker
); /* remember cursor pos */
2476 if (newline_indent_size
== -1)
2478 /* count indent size up to insert_pos instead of asking sci
2479 * because there may be spaces after it */
2480 gchar
*tmp
= sci_get_line(sci
, line_start
);
2483 idx
= insert_pos
- sci_get_position_from_line(sci
, line_start
);
2485 newline_indent_size
= count_indent_size(editor
, tmp
);
2489 /* Add line indents (in spaces) */
2490 if (newline_indent_size
> 0)
2492 const gchar
*nl
= replace_newlines
? "\n" : eol
;
2495 whitespace
= g_strnfill(newline_indent_size
, ' ');
2496 SETPTR(whitespace
, g_strconcat(nl
, whitespace
, NULL
));
2497 utils_string_replace_all(buf
, nl
, whitespace
);
2501 /* transform line endings */
2502 if (replace_newlines
)
2503 utils_string_replace_all(buf
, "\n", eol
);
2505 fix_indentation(editor
, buf
);
2507 jump_locs
= replace_cursor_markers(editor
, buf
, cursor_index
< 0);
2508 sci_insert_text(sci
, insert_pos
, buf
->str
);
2510 foreach_list(item
, jump_locs
)
2512 SelectionRange
*sel
= item
->data
;
2513 gint start
= insert_pos
+ sel
->start
;
2514 gint end
= start
+ sel
->len
;
2515 editor_indicator_set_on_range(editor
, GEANY_INDICATOR_SNIPPET
, start
, end
);
2516 /* jump to first cursor position initially */
2517 if (item
== jump_locs
)
2518 sci_set_selection(sci
, start
, end
);
2521 /* Set cursor to the requested index, or by default to after the snippet */
2522 if (cursor_index
>= 0)
2523 sci_set_current_position(sci
, insert_pos
+ cursor_index
, FALSE
);
2524 else if (jump_locs
== NULL
)
2525 sci_set_current_position(sci
, insert_pos
+ buf
->len
, FALSE
);
2527 g_slist_free_full(jump_locs
, g_free
);
2528 g_string_free(buf
, TRUE
);
2532 static gboolean
find_next_snippet_indicator(GeanyEditor
*editor
, SelectionRange
*sel
)
2534 ScintillaObject
*sci
= editor
->sci
;
2535 gint pos
= sci_get_current_position(sci
);
2537 if (pos
== sci_get_length(sci
))
2538 return FALSE
; /* EOF */
2540 /* Rewind the cursor a bit if we're in the middle (or start) of an indicator,
2541 * and treat that as the next indicator. */
2542 while (SSM(sci
, SCI_INDICATORVALUEAT
, GEANY_INDICATOR_SNIPPET
, pos
) && pos
> 0)
2545 /* Be careful at the beginning of the file */
2546 if (SSM(sci
, SCI_INDICATORVALUEAT
, GEANY_INDICATOR_SNIPPET
, pos
))
2549 sel
->start
= SSM(sci
, SCI_INDICATOREND
, GEANY_INDICATOR_SNIPPET
, pos
);
2550 sel
->len
= SSM(sci
, SCI_INDICATOREND
, GEANY_INDICATOR_SNIPPET
, sel
->start
) - sel
->start
;
2552 /* 0 if there is no remaining cursor */
2553 return sel
->len
> 0;
2557 /* Move the cursor to the next specified cursor position in an inserted snippet.
2558 * Can, and should, be optimized to give better results */
2559 gboolean
editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2561 ScintillaObject
*sci
= editor
->sci
;
2564 if (find_next_snippet_indicator(editor
, &sel
))
2566 sci_indicator_set(sci
, GEANY_INDICATOR_SNIPPET
);
2567 sci_set_selection(sci
, sel
.start
, sel
.start
+ sel
.len
);
2577 static void snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
)
2579 GHashTable
*specials
;
2581 /* replace 'special' completions */
2582 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2583 if (G_LIKELY(specials
!= NULL
))
2585 g_hash_table_foreach(specials
, snippets_replace_specials
, pattern
);
2588 /* now transform other wildcards */
2589 utils_string_replace_all(pattern
, "%newline%", "\n");
2590 utils_string_replace_all(pattern
, "%ws%", "\t");
2592 /* replace %cursor% by a very unlikely string marker */
2593 utils_string_replace_all(pattern
, "%cursor%", geany_cursor_marker
);
2595 /* unescape '%' after all %wildcards% */
2596 templates_replace_valist(pattern
, "{pc}", "%", NULL
);
2598 /* replace any template {foo} wildcards */
2599 templates_replace_common(pattern
, editor
->document
->file_name
, editor
->document
->file_type
, NULL
);
2603 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2605 ScintillaObject
*sci
= editor
->sci
;
2607 const gchar
*completion
;
2609 gint ft_id
= editor
->document
->file_type
->id
;
2611 str
= g_strdup(word
);
2614 completion
= snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
);
2615 if (completion
== NULL
)
2621 /* remove the typed word, it will be added again by the used auto completion
2622 * (not really necessary but this makes the auto completion more flexible,
2623 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2624 str_len
= strlen(str
);
2625 sci_set_selection_start(sci
, pos
- str_len
);
2626 sci_set_selection_end(sci
, pos
);
2627 sci_replace_sel(sci
, "");
2628 pos
-= str_len
; /* pos has changed while deleting */
2630 editor_insert_snippet(editor
, pos
, completion
);
2631 sci_scroll_caret(sci
);
2638 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2640 gint line
= sci_get_line_from_position(sci
, pos
);
2643 /* skip any trailing spaces */
2646 c
= sci_get_char_at(sci
, pos
);
2647 if (c
== ' ' || c
== '\t')
2653 return (pos
== sci_get_line_end_position(sci
, line
));
2657 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2659 gboolean result
= FALSE
;
2662 ScintillaObject
*sci
;
2664 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2667 if (sci_has_selection(sci
))
2669 /* return if we are editing an existing line (chars on right of cursor) */
2670 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2671 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_KEY_space
&&
2672 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2675 wc
= snippets_find_completion_by_name("Special", "wordchars");
2676 word
= editor_read_word_stem(editor
, pos
, wc
);
2678 /* prevent completion of "for " */
2680 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2682 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2683 result
= snippets_complete_constructs(editor
, pos
, word
);
2684 sci_end_undo_action(sci
);
2686 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2692 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2694 ScintillaObject
*sci
= editor
->sci
;
2695 gchar
*to_insert
= NULL
;
2699 const gchar
*gt
= ">";
2700 /* if there is already a '>' behind the cursor, don't add it */
2701 if (sci_get_char_at(sci
, pos
) == '>')
2704 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2707 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2709 sci_start_undo_action(sci
);
2710 sci_replace_sel(sci
, to_insert
);
2712 sci_set_selection(sci
, pos
, pos
);
2713 sci_end_undo_action(sci
);
2719 * (stolen from anjuta and heavily modified)
2720 * This routine will auto complete XML or HTML tags that are still open by closing them
2721 * @param ch The character we are dealing with, currently only works with the '>' character
2722 * @return True if handled, false otherwise
2724 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2726 ScintillaObject
*sci
= editor
->sci
;
2727 gint lexer
= sci_get_lexer(sci
);
2728 gint min
, size
, style
;
2729 gchar
*str_found
, sel
[512];
2730 gboolean result
= FALSE
;
2732 /* If the user has turned us off, quit now.
2733 * This may make sense only in certain languages */
2734 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2737 /* return if we are inside any embedded script */
2738 style
= sci_get_style_at(sci
, pos
);
2739 if (style
> SCE_H_XCCOMMENT
&& ! highlighting_is_string_style(lexer
, style
))
2742 /* if ch is /, check for </, else quit */
2743 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2746 /* Grab the last 512 characters or so */
2747 min
= pos
- (sizeof(sel
) - 1);
2748 if (min
< 0) min
= 0;
2751 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2753 sci_get_text_range(sci
, min
, pos
, sel
);
2754 sel
[sizeof(sel
) - 1] = '\0';
2756 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2757 /* User typed something like "<br/>" */
2762 size
-= 2; /* skip </ */
2763 str_found
= utils_find_open_xml_tag(sel
, size
);
2765 if (lexer
== SCLEX_HTML
&& utils_is_short_html_tag(str_found
))
2769 else if (!EMPTY(str_found
))
2771 insert_closing_tag(editor
, pos
, ch
, str_found
);
2779 /* like sci_get_line_indentation(), but for a string. */
2780 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2783 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2786 g_return_val_if_fail(base_indent
, 0);
2788 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2806 /* Handles special cases where HTML is embedded in another language or
2807 * another language is embedded in HTML */
2808 static GeanyFiletype
*editor_get_filetype_at_line(GeanyEditor
*editor
, gint line
)
2810 gint style
, line_start
;
2811 GeanyFiletype
*current_ft
;
2813 g_return_val_if_fail(editor
!= NULL
, NULL
);
2814 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, NULL
);
2816 current_ft
= editor
->document
->file_type
;
2817 line_start
= sci_get_position_from_line(editor
->sci
, line
);
2818 style
= sci_get_style_at(editor
->sci
, line_start
);
2820 /* Handle PHP filetype with embedded HTML */
2821 if (current_ft
->id
== GEANY_FILETYPES_PHP
&& ! is_style_php(style
))
2822 current_ft
= filetypes
[GEANY_FILETYPES_HTML
];
2824 /* Handle languages embedded in HTML */
2825 if (current_ft
->id
== GEANY_FILETYPES_HTML
)
2828 if (style
>= SCE_HJ_DEFAULT
&& style
<= SCE_HJ_REGEX
)
2829 current_ft
= filetypes
[GEANY_FILETYPES_JS
];
2831 else if (style
>= SCE_HJA_DEFAULT
&& style
<= SCE_HJA_REGEX
)
2832 current_ft
= filetypes
[GEANY_FILETYPES_JS
];
2834 else if (style
>= SCE_HB_DEFAULT
&& style
<= SCE_HB_STRINGEOL
)
2835 current_ft
= filetypes
[GEANY_FILETYPES_BASIC
];
2837 else if (style
>= SCE_HBA_DEFAULT
&& style
<= SCE_HBA_STRINGEOL
)
2838 current_ft
= filetypes
[GEANY_FILETYPES_BASIC
];
2839 /* Embedded Python */
2840 else if (style
>= SCE_HP_DEFAULT
&& style
<= SCE_HP_IDENTIFIER
)
2841 current_ft
= filetypes
[GEANY_FILETYPES_PYTHON
];
2843 else if (style
>= SCE_HPA_DEFAULT
&& style
<= SCE_HPA_IDENTIFIER
)
2844 current_ft
= filetypes
[GEANY_FILETYPES_PYTHON
];
2846 else if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
2847 style
== SCE_HPHP_COMPLEX_VARIABLE
)
2849 current_ft
= filetypes
[GEANY_FILETYPES_PHP
];
2853 /* Ensure the filetype's config is loaded */
2854 filetypes_load_config(current_ft
->id
, FALSE
);
2860 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2863 gchar
*str_begin
, *str_end
;
2864 const gchar
*co
, *cc
;
2868 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2870 ft
= editor_get_filetype_at_line(editor
, line_start
);
2872 eol
= editor_get_eol_char(editor
);
2873 if (! filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
))
2874 g_return_if_reached();
2875 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2876 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2878 /* insert the comment strings */
2879 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2880 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2881 sci_insert_text(editor
->sci
, line_len
, str_end
);
2888 /* find @p text inside the range of the current style */
2889 static gint
find_in_current_style(ScintillaObject
*sci
, const gchar
*text
, gboolean backwards
)
2891 gint start
= sci_get_current_position(sci
);
2893 gint len
= sci_get_length(sci
);
2894 gint current_style
= sci_get_style_at(sci
, start
);
2895 struct Sci_TextToFind ttf
;
2897 while (start
> 0 && sci_get_style_at(sci
, start
- 1) == current_style
)
2899 while (end
< len
&& sci_get_style_at(sci
, end
+ 1) == current_style
)
2902 ttf
.lpstrText
= (gchar
*) text
;
2903 ttf
.chrg
.cpMin
= backwards
? end
+ 1 : start
;
2904 ttf
.chrg
.cpMax
= backwards
? start
: end
+ 1;
2905 return sci_find_text(sci
, 0, &ttf
);
2909 static void sci_delete_line(ScintillaObject
*sci
, gint line
)
2911 gint start
= sci_get_position_from_line(sci
, line
);
2912 gint len
= sci_get_line_length(sci
, line
);
2913 SSM(sci
, SCI_DELETERANGE
, start
, len
);
2917 static gboolean
real_uncomment_multiline(GeanyEditor
*editor
)
2919 /* find the beginning of the multi line comment */
2920 gint start
, end
, start_line
, end_line
;
2922 const gchar
*co
, *cc
;
2924 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, FALSE
);
2926 ft
= editor_get_filetype_at_line(editor
, sci_get_current_line(editor
->sci
));
2927 if (! filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
))
2928 g_return_val_if_reached(FALSE
);
2930 start
= find_in_current_style(editor
->sci
, co
, TRUE
);
2931 end
= find_in_current_style(editor
->sci
, cc
, FALSE
);
2933 if (start
< 0 || end
< 0 || start
> end
/* who knows */)
2936 start_line
= sci_get_line_from_position(editor
->sci
, start
);
2937 end_line
= sci_get_line_from_position(editor
->sci
, end
);
2939 /* remove comment close chars */
2940 SSM(editor
->sci
, SCI_DELETERANGE
, end
, strlen(cc
));
2941 if (sci_is_blank_line(editor
->sci
, end_line
))
2942 sci_delete_line(editor
->sci
, end_line
);
2944 /* remove comment open chars (do it last since it would move the end position) */
2945 SSM(editor
->sci
, SCI_DELETERANGE
, start
, strlen(co
));
2946 if (sci_is_blank_line(editor
->sci
, start_line
))
2947 sci_delete_line(editor
->sci
, start_line
);
2953 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2955 gint lexer
= sci_get_lexer(editor
->sci
);
2958 /* List only those lexers which support multi line comments */
2963 case SCLEX_PHPSCRIPT
:
2965 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2966 style_comment
= SCE_HPHP_COMMENT
;
2968 style_comment
= SCE_H_COMMENT
;
2972 case SCLEX_LITERATEHASKELL
:
2973 style_comment
= SCE_HA_COMMENTBLOCK
; break;
2974 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2975 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2976 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2977 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2978 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2979 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2980 case SCLEX_RUST
: style_comment
= SCE_RUST_COMMENTBLOCK
; break;
2981 default: style_comment
= SCE_C_COMMENT
;
2984 return style_comment
;
2988 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2989 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2990 * it returns just 1 */
2991 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2993 gint first_line
, last_line
;
2994 gint x
, i
, line_start
, line_len
;
2995 gint sel_start
, sel_end
;
2999 const gchar
*co
, *cc
;
3000 gboolean single_line
= FALSE
;
3003 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
3006 { /* use selection or current line */
3007 sel_start
= sci_get_selection_start(editor
->sci
);
3008 sel_end
= sci_get_selection_end(editor
->sci
);
3010 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3011 /* Find the last line with chars selected (not EOL char) */
3012 last_line
= sci_get_line_from_position(editor
->sci
,
3013 sel_end
- editor_get_eol_char_len(editor
));
3014 last_line
= MAX(first_line
, last_line
);
3018 first_line
= last_line
= line
;
3019 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3022 ft
= editor_get_filetype_at_line(editor
, first_line
);
3024 if (! filetype_get_comment_open_close(ft
, TRUE
, &co
, &cc
))
3027 co_len
= strlen(co
);
3031 sci_start_undo_action(editor
->sci
);
3033 for (i
= first_line
; i
<= last_line
; i
++)
3037 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3038 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3041 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3044 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3045 sel
[buf_len
] = '\0';
3047 while (isspace(sel
[x
])) x
++;
3049 /* to skip blank lines */
3050 if (x
< line_len
&& sel
[x
] != '\0')
3052 /* use single line comment */
3059 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
3060 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
3061 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
3068 if (strncmp(sel
+ x
, co
, co_len
) != 0)
3072 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
3073 sci_replace_sel(editor
->sci
, "");
3076 /* use multi line comment */
3081 /* skip lines which are already comments */
3082 style_comment
= get_multiline_comment_style(editor
, line_start
);
3083 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3085 if (real_uncomment_multiline(editor
))
3089 /* break because we are already on the last line */
3094 sci_end_undo_action(editor
->sci
);
3096 /* restore selection if there is one
3097 * but don't touch the selection if caller is editor_do_comment_toggle */
3098 if (! toggle
&& sel_start
< sel_end
)
3102 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
3103 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
3107 gint eol_len
= editor_get_eol_char_len(editor
);
3108 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
3109 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
3117 void editor_do_comment_toggle(GeanyEditor
*editor
)
3119 gint first_line
, last_line
;
3120 gint x
, i
, line_start
, line_len
, first_line_start
, last_line_start
;
3121 gint sel_start
, sel_end
;
3122 gint count_commented
= 0, count_uncommented
= 0;
3124 const gchar
*co
, *cc
;
3125 gboolean single_line
= FALSE
;
3126 gboolean first_line_was_comment
= FALSE
;
3127 gboolean last_line_was_comment
= FALSE
;
3129 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
3132 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3134 sel_start
= sci_get_selection_start(editor
->sci
);
3135 sel_end
= sci_get_selection_end(editor
->sci
);
3137 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3138 /* Find the last line with chars selected (not EOL char) */
3139 last_line
= sci_get_line_from_position(editor
->sci
,
3140 sel_end
- editor_get_eol_char_len(editor
));
3141 last_line
= MAX(first_line
, last_line
);
3143 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3144 last_line_start
= sci_get_position_from_line(editor
->sci
, last_line
);
3146 ft
= editor_get_filetype_at_line(editor
, first_line
);
3148 if (! filetype_get_comment_open_close(ft
, TRUE
, &co
, &cc
))
3151 co_len
= strlen(co
);
3155 sci_start_undo_action(editor
->sci
);
3157 for (i
= first_line
; i
<= last_line
; i
++)
3161 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3162 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3165 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3168 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3169 sel
[buf_len
] = '\0';
3171 while (isspace(sel
[x
])) x
++;
3173 /* use single line comment */
3176 gboolean do_continue
= FALSE
;
3179 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
3180 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
3185 if (do_continue
&& i
== first_line
)
3186 first_line_was_comment
= TRUE
;
3187 last_line_was_comment
= do_continue
;
3191 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
3195 /* we are still here, so the above lines were not already comments, so comment it */
3196 count_commented
+= editor_do_comment(editor
, i
, FALSE
, TRUE
, TRUE
);
3198 /* use multi line comment */
3203 /* skip lines which are already comments */
3204 style_comment
= get_multiline_comment_style(editor
, line_start
);
3205 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3207 if (real_uncomment_multiline(editor
))
3208 count_uncommented
++;
3212 real_comment_multiline(editor
, line_start
, last_line
);
3216 /* break because we are already on the last line */
3221 sci_end_undo_action(editor
->sci
);
3225 /* restore selection or caret position */
3228 gint a
= (first_line_was_comment
) ? - (gint
) co_len
: (gint
) co_len
;
3231 /* don't modify sel_start when the selection starts within indentation */
3232 read_indent(editor
, sel_start
);
3233 indent_len
= (gint
) strlen(indent
);
3234 if ((sel_start
- first_line_start
) <= indent_len
)
3236 /* if the selection start was inside the comment mark, adjust the position */
3237 else if (first_line_was_comment
&&
3238 sel_start
>= (first_line_start
+ indent_len
) &&
3239 sel_start
<= (first_line_start
+ indent_len
+ (gint
) co_len
))
3241 a
= (first_line_start
+ indent_len
) - sel_start
;
3244 if (sel_start
< sel_end
)
3246 gint b
= (count_commented
* (gint
) co_len
) - (count_uncommented
* (gint
) co_len
);
3248 /* same for selection end, but here we add an offset on the offset above */
3249 read_indent(editor
, sel_end
+ b
);
3250 indent_len
= (gint
) strlen(indent
);
3251 if ((sel_end
- last_line_start
) < indent_len
)
3252 b
+= last_line_was_comment
? (gint
) co_len
: -(gint
) co_len
;
3253 else if (last_line_was_comment
&&
3254 sel_end
>= (last_line_start
+ indent_len
) &&
3255 sel_end
<= (last_line_start
+ indent_len
+ (gint
) co_len
))
3257 b
+= (gint
) co_len
- (sel_end
- (last_line_start
+ indent_len
));
3260 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
3261 sci_set_selection_end(editor
->sci
, sel_end
+ b
);
3264 sci_set_current_position(editor
->sci
, sel_start
+ a
, TRUE
);
3268 gint eol_len
= editor_get_eol_char_len(editor
);
3269 if (count_uncommented
> 0)
3271 sci_set_selection_start(editor
->sci
, sel_start
- (gint
) co_len
+ eol_len
);
3272 sci_set_selection_end(editor
->sci
, sel_end
- (gint
) co_len
+ eol_len
);
3274 else if (count_commented
> 0)
3276 sci_set_selection_start(editor
->sci
, sel_start
+ (gint
) co_len
- eol_len
);
3277 sci_set_selection_end(editor
->sci
, sel_end
+ (gint
) co_len
- eol_len
);
3279 if (sel_start
>= sel_end
)
3280 sci_scroll_caret(editor
->sci
);
3285 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
3286 gint
editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
,
3287 gboolean single_comment
)
3289 gint first_line
, last_line
;
3290 gint x
, i
, line_start
, line_len
;
3291 gint sel_start
, sel_end
, co_len
;
3294 const gchar
*co
, *cc
;
3295 gboolean single_line
= FALSE
;
3298 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
3301 { /* use selection or current line */
3302 sel_start
= sci_get_selection_start(editor
->sci
);
3303 sel_end
= sci_get_selection_end(editor
->sci
);
3305 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3306 /* Find the last line with chars selected (not EOL char) */
3307 last_line
= sci_get_line_from_position(editor
->sci
,
3308 sel_end
- editor_get_eol_char_len(editor
));
3309 last_line
= MAX(first_line
, last_line
);
3313 first_line
= last_line
= line
;
3314 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3317 ft
= editor_get_filetype_at_line(editor
, first_line
);
3319 if (! filetype_get_comment_open_close(ft
, single_comment
, &co
, &cc
))
3322 co_len
= strlen(co
);
3326 sci_start_undo_action(editor
->sci
);
3328 for (i
= first_line
; i
<= last_line
; i
++)
3332 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3333 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3336 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3339 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3340 sel
[buf_len
] = '\0';
3342 while (isspace(sel
[x
])) x
++;
3344 /* to skip blank lines */
3345 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3347 /* use single line comment */
3350 gint start
= line_start
;
3353 if (ft
->comment_use_indent
)
3354 start
= line_start
+ x
;
3358 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3359 sci_insert_text(editor
->sci
, start
, text
);
3363 sci_insert_text(editor
->sci
, start
, co
);
3366 /* use multi line comment */
3371 /* skip lines which are already comments */
3372 style_comment
= get_multiline_comment_style(editor
, line_start
);
3373 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3376 real_comment_multiline(editor
, line_start
, last_line
);
3379 /* break because we are already on the last line */
3384 sci_end_undo_action(editor
->sci
);
3386 /* restore selection if there is one
3387 * but don't touch the selection if caller is editor_do_comment_toggle */
3388 if (! toggle
&& sel_start
< sel_end
)
3392 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3393 sci_set_selection_end(editor
->sci
, sel_end
+ (count
* co_len
));
3397 gint eol_len
= editor_get_eol_char_len(editor
);
3398 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3399 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3406 static gboolean brace_timeout_active
= FALSE
;
3408 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3410 GeanyDocument
*doc
= document_get_current();
3411 GeanyEditor
*editor
;
3412 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3413 gint end_pos
, cur_pos
;
3415 brace_timeout_active
= FALSE
;
3419 editor
= doc
->editor
;
3420 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3422 if (cur_pos
!= brace_pos
)
3425 if (cur_pos
!= brace_pos
)
3427 /* we have moved past the original brace_pos, but after the timeout
3428 * we may now be on a new brace, so check again */
3429 editor_highlight_braces(editor
, cur_pos
);
3433 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3435 editor_highlight_braces(editor
, cur_pos
);
3438 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3442 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3443 sci_get_col_from_position(editor
->sci
, end_pos
));
3444 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3445 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3449 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3450 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3456 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3458 gint brace_pos
= cur_pos
- 1;
3460 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3461 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3463 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3466 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3471 if (!brace_timeout_active
)
3473 brace_timeout_active
= TRUE
;
3474 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3475 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3480 static gboolean
in_block_comment(gint lexer
, gint style
)
3486 return (style
== SCE_C_COMMENT
||
3487 style
== SCE_C_COMMENTDOC
);
3490 return (style
== SCE_PAS_COMMENT
||
3491 style
== SCE_PAS_COMMENT2
);
3494 return (style
== SCE_D_COMMENT
||
3495 style
== SCE_D_COMMENTDOC
||
3496 style
== SCE_D_COMMENTNESTED
);
3499 case SCLEX_PHPSCRIPT
:
3500 return (style
== SCE_HPHP_COMMENT
);
3503 return (style
== SCE_CSS_COMMENT
);
3506 return (style
== SCE_RUST_COMMENTBLOCK
||
3507 style
== SCE_RUST_COMMENTBLOCKDOC
);
3515 static gboolean
is_comment_char(gchar c
, gint lexer
)
3517 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3526 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3528 ScintillaObject
*sci
= editor
->sci
;
3529 gint indent_pos
, style
;
3530 gint lexer
= sci_get_lexer(sci
);
3532 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3533 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3534 style
= sci_get_style_at(sci
, indent_pos
);
3535 if (!in_block_comment(lexer
, style
))
3538 /* Check whether the comment block continues on this line */
3539 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3540 if (sci_get_style_at(sci
, indent_pos
) == style
|| indent_pos
>= sci_get_length(sci
))
3542 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3543 /* the type of comment, '*' (C/C++/Java), '+' D comment that nests */
3544 const gchar
*continuation
= (style
== SCE_D_COMMENTNESTED
) ? "+" : "*";
3545 const gchar
*whitespace
= ""; /* to hold whitespace if needed */
3547 gint len
= strlen(previous_line
);
3550 /* find and stop at end of multi line comment */
3552 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3553 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3555 gint indent_len
, indent_width
;
3557 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3558 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3559 indent_width
= editor_get_indent_prefs(editor
)->width
;
3561 /* if there is one too many spaces, delete the last space,
3562 * to return to the indent used before the multiline comment was started. */
3563 if (indent_len
% indent_width
== 1)
3564 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3565 g_free(previous_line
);
3568 /* check whether we are on the second line of multi line comment */
3570 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3573 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3574 { /* we are on the second line of a multi line comment, so we have to insert white space */
3577 else if (!(g_str_has_prefix(previous_line
+ i
, continuation
) &&
3578 (i
+ 1 == len
|| isspace(previous_line
[i
+ 1]))))
3580 // previous line isn't formatted so abort
3581 g_free(previous_line
);
3584 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3585 sci_add_text(sci
, result
);
3588 g_free(previous_line
);
3594 static gboolean
editor_lexer_is_c_like(gint lexer
)
3609 /* inserts a three-line comment at one line above current cursor position */
3610 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3616 gboolean have_multiline_comment
= FALSE
;
3618 const gchar
*co
, *cc
;
3620 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3622 if (! filetype_get_comment_open_close(editor
->document
->file_type
, FALSE
, &co
, &cc
))
3623 g_return_if_reached();
3625 have_multiline_comment
= TRUE
;
3627 sci_start_undo_action(editor
->sci
);
3629 doc
= editor
->document
;
3631 /* insert three lines one line above of the current position */
3632 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3633 pos
= sci_get_position_from_line(editor
->sci
, line
);
3635 /* use the indent on the current line but only when comment indentation is used
3636 * and we don't have multi line comment characters */
3637 if (editor
->auto_indent
&&
3638 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3640 read_indent(editor
, editor_info
.click_pos
);
3641 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3642 text_len
= strlen(text
);
3646 text
= g_strdup("\n\n\n");
3649 sci_insert_text(editor
->sci
, pos
, text
);
3652 /* select the inserted lines for commenting */
3653 sci_set_selection_start(editor
->sci
, pos
);
3654 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3656 editor_do_comment(editor
, -1, TRUE
, FALSE
, FALSE
);
3658 /* set the current position to the start of the first inserted line */
3661 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3662 if (have_multiline_comment
)
3665 pos
+= strlen(indent
);
3667 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3668 /* reset the selection */
3669 sci_set_anchor(editor
->sci
, pos
);
3671 sci_end_undo_action(editor
->sci
);
3675 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3676 * Scroll the view to make line appear at percent_of_view.
3677 * line can be -1 to use the current position. */
3678 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3683 g_return_if_fail(editor
!= NULL
);
3685 wid
= GTK_WIDGET(editor
->sci
);
3687 if (! gtk_widget_get_window(wid
) || ! gdk_window_is_viewable(gtk_widget_get_window(wid
)))
3688 return; /* prevent gdk_window_scroll warning */
3691 line
= sci_get_current_line(editor
->sci
);
3693 /* sci 'visible line' != doc line number because of folding and line wrapping */
3694 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3695 * SCI_DOCLINEFROMVISIBLE for vis1. */
3696 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3697 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3698 line
= line
- los
* percent_of_view
;
3699 SSM(editor
->sci
, SCI_SETFIRSTVISIBLELINE
, line
, 0);
3700 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3704 /* creates and inserts one tab or whitespace of the amount of the tab width */
3705 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3708 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3710 g_return_if_fail(editor
!= NULL
);
3712 switch (iprefs
.type
)
3714 case GEANY_INDENT_TYPE_TABS
:
3715 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3717 case GEANY_INDENT_TYPE_SPACES
:
3718 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3719 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3722 text
= get_whitespace(&iprefs
, iprefs
.width
);
3723 sci_add_text(editor
->sci
, text
);
3728 void editor_select_word(GeanyEditor
*editor
)
3734 g_return_if_fail(editor
!= NULL
);
3736 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3737 start
= sci_word_start_position(editor
->sci
, pos
, TRUE
);
3738 end
= sci_word_end_position(editor
->sci
, pos
, TRUE
);
3740 if (start
== end
) /* caret in whitespaces sequence */
3742 /* look forward but reverse the selection direction,
3743 * so the caret end up stay as near as the original position. */
3744 end
= sci_word_end_position(editor
->sci
, pos
, FALSE
);
3745 start
= sci_word_end_position(editor
->sci
, end
, TRUE
);
3750 sci_set_selection(editor
->sci
, start
, end
);
3754 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3755 * when those lines have no selection (cursor at start of line). */
3756 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3758 gint start
, end
, line
;
3760 g_return_if_fail(editor
!= NULL
);
3762 start
= sci_get_selection_start(editor
->sci
);
3763 end
= sci_get_selection_end(editor
->sci
);
3765 /* check if whole lines are already selected */
3766 if (! extra_line
&& start
!= end
&&
3767 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3768 sci_get_col_from_position(editor
->sci
, end
) == 0)
3771 line
= sci_get_line_from_position(editor
->sci
, start
);
3772 start
= sci_get_position_from_line(editor
->sci
, line
);
3774 line
= sci_get_line_from_position(editor
->sci
, end
);
3775 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3777 sci_set_selection(editor
->sci
, start
, end
);
3781 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
)
3783 return sci_get_line_indent_position(sci
, line
) ==
3784 sci_get_line_end_position(sci
, line
);
3788 /* Returns first line of paragraph for GTK_DIR_UP, line after paragraph
3789 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3790 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3793 ScintillaObject
*sci
= editor
->sci
;
3795 /* first check current line and return -1 if it is empty to skip creating of a selection */
3796 if (sci_is_blank_line(sci
, line
))
3799 if (direction
== GTK_DIR_UP
)
3809 /* start of document */
3813 if (line
== sci_get_line_count(sci
))
3816 if (sci_is_blank_line(sci
, line
))
3818 /* return line paragraph starts on */
3819 if (direction
== GTK_DIR_UP
)
3828 void editor_select_paragraph(GeanyEditor
*editor
)
3830 gint pos_start
, pos_end
, line_start
, line_found
;
3832 g_return_if_fail(editor
!= NULL
);
3834 line_start
= sci_get_current_line(editor
->sci
);
3836 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3837 if (line_found
== -1)
3840 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3842 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3843 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3845 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3849 /* Returns first line of block for GTK_DIR_UP, line after block
3850 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3851 static gint
find_block_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3854 ScintillaObject
*sci
= editor
->sci
;
3856 /* first check current line and return -1 if it is empty to skip creating of a selection */
3857 if (sci_is_blank_line(sci
, line
))
3860 if (direction
== GTK_DIR_UP
)
3865 ind
= sci_get_line_indentation(sci
, line
);
3871 /* start of document */
3875 if (line
== sci_get_line_count(sci
))
3878 if (sci_get_line_indentation(sci
, line
) != ind
||
3879 sci_is_blank_line(sci
, line
))
3881 /* return line block starts on */
3882 if (direction
== GTK_DIR_UP
)
3891 void editor_select_indent_block(GeanyEditor
*editor
)
3893 gint pos_start
, pos_end
, line_start
, line_found
;
3895 g_return_if_fail(editor
!= NULL
);
3897 line_start
= sci_get_current_line(editor
->sci
);
3899 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_UP
);
3900 if (line_found
== -1)
3903 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3905 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_DOWN
);
3906 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3908 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3912 /* simple indentation to indent the current line with the same indent as the previous one */
3913 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
3915 gint i
, sel_start
= 0, sel_end
= 0;
3917 /* get previous line and use it for read_indent to use that line
3918 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
3919 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
3921 for (i
= first_line
; i
<= last_line
; i
++)
3923 /* skip the first line or if the indentation of the previous and current line are equal */
3925 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
3926 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
3929 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3930 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3931 if (sel_start
< sel_end
)
3933 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
3934 sci_replace_sel(editor
->sci
, "");
3936 sci_insert_text(editor
->sci
, sel_start
, indent
);
3941 /* simple indentation to indent the current line with the same indent as the previous one */
3942 void editor_smart_line_indentation(GeanyEditor
*editor
)
3944 gint first_line
, last_line
;
3945 gint first_sel_start
, first_sel_end
;
3946 ScintillaObject
*sci
;
3948 g_return_if_fail(editor
!= NULL
);
3952 first_sel_start
= sci_get_selection_start(sci
);
3953 first_sel_end
= sci_get_selection_end(sci
);
3955 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
3956 /* Find the last line with chars selected (not EOL char) */
3957 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
3958 last_line
= MAX(first_line
, last_line
);
3960 sci_start_undo_action(sci
);
3962 smart_line_indentation(editor
, first_line
, last_line
);
3964 /* set cursor position if there was no selection */
3965 if (first_sel_start
== first_sel_end
)
3967 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
3969 /* use indent position as user may wish to change indentation afterwards */
3970 sci_set_current_position(sci
, indent_pos
, FALSE
);
3974 /* fully select all the lines affected */
3975 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
3976 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
3979 sci_end_undo_action(sci
);
3983 /* increase / decrease current line or selection by one space */
3984 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
3986 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
3987 gint sel_start
, sel_end
, first_line_offset
= 0;
3989 g_return_if_fail(editor
!= NULL
);
3991 sel_start
= sci_get_selection_start(editor
->sci
);
3992 sel_end
= sci_get_selection_end(editor
->sci
);
3994 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3995 /* Find the last line with chars selected (not EOL char) */
3996 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
3997 last_line
= MAX(first_line
, last_line
);
4002 sci_start_undo_action(editor
->sci
);
4004 for (i
= first_line
; i
<= last_line
; i
++)
4006 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
4009 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
4010 /* searching backwards for a space to remove */
4011 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
4014 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
4016 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
4017 sci_replace_sel(editor
->sci
, "");
4019 if (i
== first_line
)
4020 first_line_offset
= -1;
4025 sci_insert_text(editor
->sci
, indentation_end
, " ");
4027 if (i
== first_line
)
4028 first_line_offset
= 1;
4032 /* set cursor position */
4033 if (sel_start
< sel_end
)
4035 gint start
= sel_start
+ first_line_offset
;
4036 if (first_line_offset
< 0)
4037 start
= MAX(sel_start
+ first_line_offset
,
4038 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
4040 sci_set_selection_start(editor
->sci
, start
);
4041 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
4044 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
4046 sci_end_undo_action(editor
->sci
);
4050 void editor_finalize(void)
4052 scintilla_release_resources();
4056 /* wordchars: NULL or a string containing characters to match a word.
4057 * Returns: the current selection or the current word.
4059 * Passing NULL as wordchars is NOT the same as passing GEANY_WORDCHARS: NULL means
4060 * using Scintillas's word boundaries. */
4061 gchar
*editor_get_default_selection(GeanyEditor
*editor
, gboolean use_current_word
,
4062 const gchar
*wordchars
)
4066 g_return_val_if_fail(editor
!= NULL
, NULL
);
4068 if (sci_get_lines_selected(editor
->sci
) == 1)
4069 s
= sci_get_selection_contents(editor
->sci
);
4070 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
4071 { /* use the word at current cursor position */
4072 gchar word
[GEANY_MAX_WORD_LENGTH
];
4074 if (wordchars
!= NULL
)
4075 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
4077 editor_find_current_word_sciwc(editor
, -1, word
, sizeof(word
));
4079 if (word
[0] != '\0')
4086 /* Note: Usually the line should be made visible (not folded) before calling this.
4087 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4088 * outside the *vertical* view.
4089 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4090 * sci_scroll_caret() when this returns TRUE. */
4091 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4095 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4097 /* If line is wrapped the result may occur on another virtual line than the first and may be
4098 * still hidden, so increase the line number to check for the next document line */
4099 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4102 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4103 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4104 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4106 return (line
>= vis1
&& line
< vis1
+ los
);
4110 /* If the current line is outside the current view window, scroll the line
4111 * so it appears at percent_of_view. */
4112 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4116 g_return_if_fail(editor
!= NULL
);
4118 line
= sci_get_current_line(editor
->sci
);
4120 /* unfold maybe folded results */
4121 sci_ensure_line_is_visible(editor
->sci
, line
);
4123 /* scroll the line if it's off screen */
4124 if (! editor_line_in_view(editor
, line
))
4125 editor
->scroll_percent
= percent_of_view
;
4127 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4132 * Deletes all currently set indicators in the @a editor window.
4133 * Error indicators (red squiggly underlines) and usual line markers are removed.
4135 * @param editor The editor to operate on.
4137 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4139 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4140 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4145 * Deletes all currently set indicators matching @a indic in the @a editor window.
4147 * @param editor The editor to operate on.
4148 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4153 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4157 g_return_if_fail(editor
!= NULL
);
4159 last_pos
= sci_get_length(editor
->sci
);
4162 sci_indicator_set(editor
->sci
, indic
);
4163 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4169 * Sets an indicator @a indic on @a line.
4170 * Whitespace at the start and the end of the line is not marked.
4172 * @param editor The editor to operate on.
4173 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4174 * @param line The line number which should be marked.
4179 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4185 g_return_if_fail(editor
!= NULL
);
4186 g_return_if_fail(line
>= 0);
4188 start
= sci_get_position_from_line(editor
->sci
, line
);
4189 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4191 /* skip blank lines */
4192 if ((start
+ 1) == end
||
4194 (sci_get_line_end_position(editor
->sci
, line
) - start
) == 0)
4200 linebuf
= sci_get_line(editor
->sci
, line
);
4202 /* don't set the indicator on whitespace */
4203 while (isspace(linebuf
[i
]))
4205 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4212 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4217 * Sets an indicator on the range specified by @a start and @a end.
4218 * No error checking or whitespace removal is performed, this should be done by the calling
4219 * function if necessary.
4221 * @param editor The editor to operate on.
4222 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4223 * @param start The starting position for the marker.
4224 * @param end The ending position for the marker.
4229 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4231 g_return_if_fail(editor
!= NULL
);
4235 sci_indicator_set(editor
->sci
, indic
);
4236 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4240 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4241 * the replacement will also start with 0x... */
4242 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4244 g_return_if_fail(editor
!= NULL
);
4246 if (sci_has_selection(editor
->sci
))
4248 gint start
= sci_get_selection_start(editor
->sci
);
4249 const gchar
*replacement
= colour
;
4251 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4252 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4254 gint end
= sci_get_selection_end(editor
->sci
);
4256 sci_set_selection_start(editor
->sci
, start
+ 2);
4257 /* we need to also re-set the selection end in case the anchor was located before
4258 * the cursor, since set_selection_start() always moves the cursor, not the anchor */
4259 sci_set_selection_end(editor
->sci
, end
);
4260 replacement
++; /* skip the leading "0x" */
4262 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4263 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4264 replacement
++; /* so skip the '#' to only replace the colour value */
4266 sci_replace_sel(editor
->sci
, replacement
);
4269 sci_add_text(editor
->sci
, colour
);
4274 * Retrieves the end of line characters mode (LF, CR/LF, CR) in the given editor.
4275 * If @a editor is @c NULL, the default end of line characters are used.
4277 * @param editor @nullable The editor to operate on, or @c NULL to query the default value.
4278 * @return The used end of line characters mode.
4283 gint
editor_get_eol_char_mode(GeanyEditor
*editor
)
4285 gint mode
= file_prefs
.default_eol_character
;
4288 mode
= sci_get_eol_mode(editor
->sci
);
4295 * Retrieves the localized name (for displaying) of the used end of line characters
4296 * (LF, CR/LF, CR) in the given editor.
4297 * If @a editor is @c NULL, the default end of line characters are used.
4299 * @param editor @nullable The editor to operate on, or @c NULL to query the default value.
4300 * @return The name of the end of line characters.
4305 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4307 gint mode
= file_prefs
.default_eol_character
;
4310 mode
= sci_get_eol_mode(editor
->sci
);
4312 return utils_get_eol_name(mode
);
4317 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4318 * If @a editor is @c NULL, the default end of line characters are used.
4319 * The returned value is 1 for CR and LF and 2 for CR/LF.
4321 * @param editor @nullable The editor to operate on, or @c NULL to query the default value.
4322 * @return The length of the end of line characters.
4327 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4329 gint mode
= file_prefs
.default_eol_character
;
4332 mode
= sci_get_eol_mode(editor
->sci
);
4336 case SC_EOL_CRLF
: return 2; break;
4337 default: return 1; break;
4343 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4344 * If @a editor is @c NULL, the default end of line characters are used.
4345 * The returned value is either "\n", "\r\n" or "\r".
4347 * @param editor @nullable The editor to operate on, or @c NULL to query the default value.
4348 * @return The end of line characters.
4353 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4355 gint mode
= file_prefs
.default_eol_character
;
4358 mode
= sci_get_eol_mode(editor
->sci
);
4360 return utils_get_eol_char(mode
);
4364 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4366 gint lines
, first
, i
;
4368 if (editor
== NULL
|| ! editor_prefs
.folding
)
4371 lines
= sci_get_line_count(editor
->sci
);
4372 first
= sci_get_first_visible_line(editor
->sci
);
4374 for (i
= 0; i
< lines
; i
++)
4376 gint level
= sci_get_fold_level(editor
->sci
, i
);
4378 if (level
& SC_FOLDLEVELHEADERFLAG
)
4380 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4381 sci_toggle_fold(editor
->sci
, i
);
4384 editor_scroll_to_line(editor
, first
, 0.0F
);
4388 void editor_unfold_all(GeanyEditor
*editor
)
4390 fold_all(editor
, FALSE
);
4394 void editor_fold_all(GeanyEditor
*editor
)
4396 fold_all(editor
, TRUE
);
4400 void editor_replace_tabs(GeanyEditor
*editor
, gboolean ignore_selection
)
4402 gint anchor_pos
, caret_pos
;
4403 struct Sci_TextToFind ttf
;
4405 g_return_if_fail(editor
!= NULL
);
4407 sci_start_undo_action(editor
->sci
);
4408 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4410 ttf
.chrg
.cpMin
= sci_get_selection_start(editor
->sci
);
4411 ttf
.chrg
.cpMax
= sci_get_selection_end(editor
->sci
);
4416 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4418 ttf
.lpstrText
= (gchar
*) "\t";
4420 anchor_pos
= SSM(editor
->sci
, SCI_GETANCHOR
, 0, 0);
4421 caret_pos
= sci_get_current_position(editor
->sci
);
4424 gint search_pos
, pos_in_line
, current_tab_true_length
;
4428 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4429 if (search_pos
== -1)
4432 tab_len
= sci_get_tab_width(editor
->sci
);
4433 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4434 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4435 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4436 sci_set_target_start(editor
->sci
, search_pos
);
4437 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4438 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4439 /* next search starts after replacement */
4440 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4441 /* update end of range now text has changed */
4442 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4445 if (anchor_pos
> search_pos
)
4446 anchor_pos
+= current_tab_true_length
- 1;
4447 if (caret_pos
> search_pos
)
4448 caret_pos
+= current_tab_true_length
- 1;
4450 sci_set_selection(editor
->sci
, anchor_pos
, caret_pos
);
4451 sci_end_undo_action(editor
->sci
);
4455 /* Replaces all occurrences all spaces of the length of a given tab_width,
4456 * optionally restricting the search to the current selection. */
4457 void editor_replace_spaces(GeanyEditor
*editor
, gboolean ignore_selection
)
4460 gint anchor_pos
, caret_pos
;
4461 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4464 struct Sci_TextToFind ttf
;
4466 g_return_if_fail(editor
!= NULL
);
4468 if (tab_len_f
< 0.0)
4469 tab_len_f
= sci_get_tab_width(editor
->sci
);
4471 if (! dialogs_show_input_numeric(
4472 _("Enter Tab Width"),
4473 _("Enter the amount of spaces which should be replaced by a tab character."),
4474 &tab_len_f
, 1, 100, 1))
4478 tab_len
= (gint
) tab_len_f
;
4479 text
= g_strnfill(tab_len
, ' ');
4481 sci_start_undo_action(editor
->sci
);
4482 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4484 ttf
.chrg
.cpMin
= sci_get_selection_start(editor
->sci
);
4485 ttf
.chrg
.cpMax
= sci_get_selection_end(editor
->sci
);
4490 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4492 ttf
.lpstrText
= text
;
4494 anchor_pos
= SSM(editor
->sci
, SCI_GETANCHOR
, 0, 0);
4495 caret_pos
= sci_get_current_position(editor
->sci
);
4498 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4499 if (search_pos
== -1)
4501 /* only replace indentation because otherwise we can mess up alignment */
4502 if (search_pos
> sci_get_line_indent_position(editor
->sci
,
4503 sci_get_line_from_position(editor
->sci
, search_pos
)))
4505 ttf
.chrg
.cpMin
= search_pos
+ tab_len
;
4508 sci_set_target_start(editor
->sci
, search_pos
);
4509 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4510 sci_replace_target(editor
->sci
, "\t", FALSE
);
4511 ttf
.chrg
.cpMin
= search_pos
;
4512 /* update end of range now text has changed */
4513 ttf
.chrg
.cpMax
-= tab_len
- 1;
4515 if (anchor_pos
> search_pos
)
4516 anchor_pos
-= tab_len
- 1;
4517 if (caret_pos
> search_pos
)
4518 caret_pos
-= tab_len
- 1;
4520 sci_set_selection(editor
->sci
, anchor_pos
, caret_pos
);
4521 sci_end_undo_action(editor
->sci
);
4526 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4528 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4529 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4530 gint i
= line_end
- 1;
4531 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4533 /* Diff hunks should keep trailing spaces */
4534 if (editor
->document
->file_type
->id
== GEANY_FILETYPES_DIFF
)
4537 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4540 ch
= sci_get_char_at(editor
->sci
, i
);
4542 if (i
< (line_end
- 1))
4544 sci_set_target_start(editor
->sci
, i
+ 1);
4545 sci_set_target_end(editor
->sci
, line_end
);
4546 sci_replace_target(editor
->sci
, "", FALSE
);
4551 void editor_strip_trailing_spaces(GeanyEditor
*editor
, gboolean ignore_selection
)
4557 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4559 gint selection_start
= sci_get_selection_start(editor
->sci
);
4560 gint selection_end
= sci_get_selection_end(editor
->sci
);
4562 start_line
= sci_get_line_from_position(editor
->sci
, selection_start
);
4563 end_line
= sci_get_line_from_position(editor
->sci
, selection_end
);
4565 if (sci_get_col_from_position(editor
->sci
, selection_end
) > 0)
4571 end_line
= sci_get_line_count(editor
->sci
);
4574 sci_start_undo_action(editor
->sci
);
4576 for (line
= start_line
; line
< end_line
; line
++)
4578 editor_strip_line_trailing_spaces(editor
, line
);
4580 sci_end_undo_action(editor
->sci
);
4584 void editor_ensure_final_newline(GeanyEditor
*editor
)
4586 gint max_lines
= sci_get_line_count(editor
->sci
);
4587 gboolean append_newline
= (max_lines
== 1);
4588 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4592 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4596 const gchar
*eol
= editor_get_eol_char(editor
);
4598 sci_insert_text(editor
->sci
, end_document
, eol
);
4603 /* Similar to editor_set_font() but *only* sets the font, and doesn't take care
4604 * of updating properties that might depend on the font */
4605 static void set_font(ScintillaObject
*sci
, const gchar
*font
)
4609 PangoFontDescription
*pfd
;
4612 g_return_if_fail(sci
);
4614 pfd
= pango_font_description_from_string(font
);
4615 size
= pango_font_description_get_size(pfd
) / (gdouble
) PANGO_SCALE
;
4616 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4617 pango_font_description_free(pfd
);
4619 for (style
= 0; style
<= STYLE_MAX
; style
++)
4620 sci_set_font_fractional(sci
, style
, font_name
, size
);
4626 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4628 g_return_if_fail(editor
);
4630 set_font(editor
->sci
, font
);
4631 update_margins(editor
->sci
);
4632 /* zoom to 100% to prevent confusion */
4633 sci_zoom_off(editor
->sci
);
4637 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4639 g_return_if_fail(editor
!= NULL
);
4641 editor
->line_wrapping
= wrap
;
4642 sci_set_lines_wrapped(editor
->sci
, wrap
);
4646 /** Sets the indent type for @a editor.
4647 * @param editor Editor.
4648 * @param type Indent type.
4653 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4655 editor_set_indent(editor
, type
, editor
->indent_width
);
4659 /** Sets the indent width for @a editor.
4660 * @param editor Editor.
4661 * @param width New indent width.
4663 * @since 1.27 (API 227)
4666 void editor_set_indent_width(GeanyEditor
*editor
, gint width
)
4668 editor_set_indent(editor
, editor
->indent_type
, width
);
4672 void editor_set_indent(GeanyEditor
*editor
, GeanyIndentType type
, gint width
)
4674 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4675 ScintillaObject
*sci
= editor
->sci
;
4676 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4678 editor
->indent_type
= type
;
4679 editor
->indent_width
= width
;
4680 sci_set_use_tabs(sci
, use_tabs
);
4682 if (type
== GEANY_INDENT_TYPE_BOTH
)
4684 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4685 if (iprefs
->hard_tab_width
!= 8)
4687 static gboolean warn
= TRUE
;
4689 ui_set_statusbar(TRUE
, _("Warning: non-standard hard tab width: %d != 8!"),
4690 iprefs
->hard_tab_width
);
4695 sci_set_tab_width(sci
, width
);
4697 SSM(sci
, SCI_SETINDENT
, width
, 0);
4699 /* remove indent spaces on backspace, if using any spaces to indent */
4700 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, editor_prefs
.backspace_unindent
&& (type
!= GEANY_INDENT_TYPE_TABS
), 0);
4704 /* Convenience function for editor_goto_pos() to pass a line number.
4705 * line_no is 1 based */
4706 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line_no
, gboolean offset
)
4708 g_return_val_if_fail(editor
, FALSE
);
4709 gint line_count
= sci_get_line_count(editor
->sci
);
4712 line_no
+= sci_get_current_line(editor
->sci
) + 1;
4714 /* ensure line_no is in bounds and determine whether to set line marker */
4715 gboolean set_marker
= line_no
> 0 && line_no
< line_count
;
4716 line_no
= line_no
<= 0 ? 0
4717 : line_no
>= line_count
? line_count
- 1
4720 gint pos
= sci_get_position_from_line(editor
->sci
, line_no
);
4721 return editor_goto_pos(editor
, pos
, set_marker
);
4725 /** Moves to position @a pos, switching to the document if necessary,
4726 * setting a marker if @a mark is set.
4728 * @param editor Editor.
4729 * @param pos The position.
4730 * @param mark Whether to set a mark on the position.
4731 * @return @c TRUE if action has been performed, otherwise @c FALSE.
4736 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4738 g_return_val_if_fail(editor
, FALSE
);
4739 if (G_UNLIKELY(pos
< 0))
4744 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4746 /* mark the tag with the yellow arrow */
4747 sci_marker_delete_all(editor
->sci
, 0);
4748 sci_set_marker_at_line(editor
->sci
, line
, 0);
4751 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4752 editor
->scroll_percent
= 0.25F
;
4754 /* switch to the page */
4755 document_show_tab(editor
->document
);
4762 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4764 GeanyEditor
*editor
= user_data
;
4766 /* we only handle up and down, leave the rest to Scintilla */
4767 if (event
->direction
!= GDK_SCROLL_UP
&& event
->direction
!= GDK_SCROLL_DOWN
)
4770 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4771 * few lines only, maybe this could/should be done in Scintilla directly */
4772 if (event
->state
& GDK_MOD1_MASK
)
4774 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4777 else if (event
->state
& GDK_SHIFT_MASK
)
4779 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4781 sci_scroll_columns(editor
->sci
, amount
);
4785 return FALSE
; /* let Scintilla handle all other cases */
4789 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4791 GeanyDocument
*doc
= editor
->document
;
4793 if (!doc
->priv
->colourise_needed
)
4796 doc
->priv
->colourise_needed
= FALSE
;
4797 sci_colourise(editor
->sci
, 0, -1);
4799 /* now that the current document is colourised, fold points are now accurate,
4800 * so force an update of the current function/tag. */
4801 symbols_get_current_function(NULL
, NULL
);
4802 ui_update_statusbar(NULL
, -1);
4808 /* We only want to colourise just before drawing, to save startup time and
4809 * prevent unnecessary recolouring other documents after one is saved.
4810 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4811 * and "show" doesn't work). */
4812 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4814 GeanyEditor
*editor
= user_data
;
4816 editor_check_colourise(editor
);
4821 static gboolean
on_editor_draw(GtkWidget
*widget
, cairo_t
*cr
, gpointer user_data
)
4823 GeanyEditor
*editor
= user_data
;
4825 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4826 * for some reason, maybe it's not necessary but just in case. */
4827 editor_check_colourise(editor
);
4832 static void setup_sci_keys(ScintillaObject
*sci
)
4834 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4835 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4836 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4837 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4838 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4839 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4840 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4841 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4842 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to beginning delete */
4843 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4844 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4845 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4846 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4847 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4848 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4849 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4851 if (editor_prefs
.use_gtk_word_boundaries
)
4853 /* use GtkEntry-like word boundaries */
4854 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4855 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4856 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4858 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4859 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4860 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4861 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4862 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4863 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4865 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4869 /* registers a Scintilla image from a named icon from the theme */
4870 static gboolean
register_named_icon(ScintillaObject
*sci
, guint id
, const gchar
*name
)
4872 GError
*error
= NULL
;
4874 gint n_channels
, rowstride
, width
, height
;
4877 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU
, &size
, NULL
);
4878 pixbuf
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), name
, size
, 0, &error
);
4881 g_warning("failed to load icon '%s': %s", name
, error
->message
);
4882 g_error_free(error
);
4886 n_channels
= gdk_pixbuf_get_n_channels(pixbuf
);
4887 rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
4888 width
= gdk_pixbuf_get_width(pixbuf
);
4889 height
= gdk_pixbuf_get_height(pixbuf
);
4891 if (gdk_pixbuf_get_bits_per_sample(pixbuf
) != 8 ||
4892 ! gdk_pixbuf_get_has_alpha(pixbuf
) ||
4894 rowstride
!= width
* n_channels
)
4896 g_warning("incompatible image data for icon '%s'", name
);
4897 g_object_unref(pixbuf
);
4901 SSM(sci
, SCI_RGBAIMAGESETWIDTH
, width
, 0);
4902 SSM(sci
, SCI_RGBAIMAGESETHEIGHT
, height
, 0);
4903 SSM(sci
, SCI_REGISTERRGBAIMAGE
, id
, (sptr_t
)gdk_pixbuf_get_pixels(pixbuf
));
4905 g_object_unref(pixbuf
);
4910 /* Create new editor widget (scintilla).
4911 * @note The @c "sci-notify" signal is connected separately. */
4912 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4914 ScintillaObject
*sci
;
4915 int rectangular_selection_modifier
;
4918 sci
= SCINTILLA(scintilla_new());
4920 /* Scintilla doesn't support RTL languages properly and is primarily
4921 * intended to be used with LTR source code, so override the
4922 * GTK+ default text direction for the Scintilla widget. */
4923 gtk_widget_set_direction(GTK_WIDGET(sci
), GTK_TEXT_DIR_LTR
);
4925 gtk_widget_show(GTK_WIDGET(sci
));
4927 sci_set_codepage(sci
, SC_CP_UTF8
);
4928 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4929 /* disable scintilla provided popup menu */
4930 sci_use_popup(sci
, FALSE
);
4932 setup_sci_keys(sci
);
4934 sci_set_lines_wrapped(sci
, editor
->line_wrapping
);
4935 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4936 /* Y policy is set in editor_apply_update_prefs() */
4937 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4938 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4940 /* tag autocompletion images */
4941 for (i
= 0; i
< TM_N_ICONS
; i
++)
4943 const gchar
*icon_name
= symbols_get_icon_name(i
);
4944 register_named_icon(sci
, i
+ 1, icon_name
);
4947 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4948 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4950 /* rectangular selection modifier for creating rectangular selections with the mouse.
4951 * We use the historical Scintilla values by default. */
4953 rectangular_selection_modifier
= SCMOD_ALT
;
4955 rectangular_selection_modifier
= SCMOD_CTRL
;
4957 SSM(sci
, SCI_SETRECTANGULARSELECTIONMODIFIER
, rectangular_selection_modifier
, 0);
4960 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4962 /* input method editor's candidate window behaviour */
4963 SSM(sci
, SCI_SETIMEINTERACTION
, editor_prefs
.ime_interaction
, 0);
4965 /* only connect signals if this is for the document notebook, not split window */
4966 if (editor
->sci
== NULL
)
4968 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4969 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4970 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4971 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4972 g_signal_connect(sci
, "draw", G_CALLBACK(on_editor_draw
), editor
);
4978 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4979 * @param editor Editor settings.
4980 * @return @transfer{floating} The new widget.
4985 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4987 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4988 ScintillaObject
*old
, *sci
;
4989 GeanyIndentType old_indent_type
= editor
->indent_type
;
4990 gint old_indent_width
= editor
->indent_width
;
4992 /* temporarily change editor to use the new sci widget */
4994 sci
= create_new_sci(editor
);
4997 editor_set_indent(editor
, iprefs
->type
, iprefs
->width
);
4998 set_font(editor
->sci
, interface_prefs
.editor_font
);
4999 editor_apply_update_prefs(editor
);
5001 /* if editor already had a widget, restore it */
5004 editor
->indent_type
= old_indent_type
;
5005 editor
->indent_width
= old_indent_width
;
5012 GeanyEditor
*editor_create(GeanyDocument
*doc
)
5014 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
5015 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
5017 editor
->document
= doc
;
5018 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
5020 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
5021 editor
->line_wrapping
= get_project_pref(line_wrapping
);
5022 editor
->scroll_percent
= -1.0F
;
5023 editor
->line_breaking
= FALSE
;
5025 editor
->sci
= editor_create_widget(editor
);
5030 /* in case we need to free some fields in future */
5031 void editor_destroy(GeanyEditor
*editor
)
5037 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
5039 gchar
*f
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
5041 if (utils_str_equal(doc
->real_path
, f
))
5043 /* reload snippets */
5044 editor_snippets_free();
5045 editor_snippets_init();
5051 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
5055 g_return_val_if_fail(editor
, FALSE
);
5057 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
5060 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
5062 /* if no word part, complete normally */
5063 if (!check_partial_completion(editor
, entry
))
5064 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
5071 void editor_init(void)
5073 static GeanyIndentPrefs indent_prefs
;
5076 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
5077 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
5078 editor_prefs
.indentation
= &indent_prefs
;
5080 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
5081 * handler (on_editor_notify) is called */
5082 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
5084 f
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
5085 ui_add_config_file_menu_item(f
, NULL
, NULL
);
5087 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
5091 /* TODO: Should these be user-defined instead of hard-coded? */
5092 void editor_set_indentation_guides(GeanyEditor
*editor
)
5097 g_return_if_fail(editor
!= NULL
);
5099 if (! editor_prefs
.show_indent_guide
)
5101 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
5105 lexer
= sci_get_lexer(editor
->sci
);
5108 /* Lines added/removed are prefixed with +/- characters, so
5109 * those lines will not be shown with any indentation guides.
5110 * It can be distracting that only a few of lines in a diff/patch
5111 * file will show the guides. */
5116 /* These languages use indentation for control blocks; the "look forward" method works
5120 case SCLEX_MAKEFILE
:
5124 case SCLEX_PROPERTIES
:
5125 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
5127 mode
= SC_IV_LOOKFORWARD
;
5130 /* C-like (structured) languages benefit from the "look both" method */
5133 case SCLEX_PHPSCRIPT
:
5145 case SCLEX_FREEBASIC
:
5149 mode
= SC_IV_LOOKBOTH
;
5157 sci_set_indentation_guides(editor
->sci
, mode
);
5161 /* Apply non-document prefs that can change in the Preferences dialog */
5162 void editor_apply_update_prefs(GeanyEditor
*editor
)
5164 ScintillaObject
*sci
;
5167 g_return_if_fail(editor
!= NULL
);
5169 if (main_status
.quitting
)
5174 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
5175 editor_get_long_line_column(), editor_prefs
.long_line_color
);
5177 /* update indent width, tab width */
5178 editor_set_indent(editor
, editor
->indent_type
, editor
->indent_width
);
5179 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
5181 sci_assign_cmdkey(sci
, SCK_HOME
| (SCMOD_SHIFT
<< 16),
5182 editor_prefs
.smart_home_key
? SCI_VCHOMEEXTEND
: SCI_HOMEEXTEND
);
5183 sci_assign_cmdkey(sci
, SCK_HOME
| ((SCMOD_SHIFT
| SCMOD_ALT
) << 16),
5184 editor_prefs
.smart_home_key
? SCI_VCHOMERECTEXTEND
: SCI_HOMERECTEXTEND
);
5186 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
5187 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
5189 editor_set_indentation_guides(editor
);
5191 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
5192 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
5193 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
5194 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
);
5195 sci_set_eol_representation_characters(sci
, sci_get_eol_mode(sci
));
5197 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
5200 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
5202 /* Change history */
5203 guint change_history_mask
;
5204 change_history_mask
= SC_CHANGE_HISTORY_DISABLED
;
5205 if (editor_prefs
.change_history_markers
)
5206 change_history_mask
|= SC_CHANGE_HISTORY_ENABLED
|SC_CHANGE_HISTORY_MARKERS
;
5207 if (editor_prefs
.change_history_indicators
)
5208 change_history_mask
|= SC_CHANGE_HISTORY_ENABLED
|SC_CHANGE_HISTORY_INDICATORS
;
5209 SSM(sci
, SCI_SETCHANGEHISTORY
, change_history_mask
, 0);
5211 /* caret Y policy */
5212 caret_y_policy
= CARET_EVEN
;
5213 if (editor_prefs
.scroll_lines_around_cursor
> 0)
5214 caret_y_policy
|= CARET_SLOP
| CARET_STRICT
;
5215 sci_set_caret_policy_y(sci
, caret_y_policy
, editor_prefs
.scroll_lines_around_cursor
);
5217 /* (dis)allow scrolling past end of document */
5218 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
5220 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
5224 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
5225 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
5227 ScintillaObject
*sci
= editor
->sci
;
5228 gint pos
= sci_get_position_from_line(sci
, line
);
5232 sci_insert_text(sci
, pos
, "\t");
5236 if (sci_get_char_at(sci
, pos
) == '\t')
5238 sci_set_selection(sci
, pos
, pos
+ 1);
5239 sci_replace_sel(sci
, "");
5241 else /* remove spaces only if no tabs */
5243 gint width
= sci_get_line_indentation(sci
, line
);
5245 width
-= editor_get_indent_prefs(editor
)->width
;
5246 sci_set_line_indentation(sci
, line
, width
);
5252 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
5254 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
5255 ScintillaObject
*sci
= editor
->sci
;
5257 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
5258 change_tab_indentation(editor
, line
, increase
);
5261 gint width
= sci_get_line_indentation(sci
, line
);
5263 width
+= increase
? iprefs
->width
: -iprefs
->width
;
5264 sci_set_line_indentation(sci
, line
, width
);
5269 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
5271 ScintillaObject
*sci
= editor
->sci
;
5272 gint caret_pos
, caret_line
, caret_offset
, caret_indent_pos
, caret_line_len
;
5273 gint anchor_pos
, anchor_line
, anchor_offset
, anchor_indent_pos
, anchor_line_len
;
5275 /* backup information needed to restore caret and anchor */
5276 caret_pos
= sci_get_current_position(sci
);
5277 anchor_pos
= SSM(sci
, SCI_GETANCHOR
, 0, 0);
5278 caret_line
= sci_get_line_from_position(sci
, caret_pos
);
5279 anchor_line
= sci_get_line_from_position(sci
, anchor_pos
);
5280 caret_offset
= caret_pos
- sci_get_position_from_line(sci
, caret_line
);
5281 anchor_offset
= anchor_pos
- sci_get_position_from_line(sci
, anchor_line
);
5282 caret_indent_pos
= sci_get_line_indent_position(sci
, caret_line
);
5283 anchor_indent_pos
= sci_get_line_indent_position(sci
, anchor_line
);
5284 caret_line_len
= sci_get_line_length(sci
, caret_line
);
5285 anchor_line_len
= sci_get_line_length(sci
, anchor_line
);
5287 if (sci_get_lines_selected(sci
) <= 1)
5289 editor_change_line_indent(editor
, sci_get_current_line(sci
), increase
);
5294 gint line
, lstart
, lend
;
5296 editor_select_lines(editor
, FALSE
);
5297 start
= sci_get_selection_start(sci
);
5298 end
= sci_get_selection_end(sci
);
5299 lstart
= sci_get_line_from_position(sci
, start
);
5300 lend
= sci_get_line_from_position(sci
, end
);
5301 if (end
== sci_get_length(sci
))
5302 lend
++; /* for last line with text on it */
5304 sci_start_undo_action(sci
);
5305 for (line
= lstart
; line
< lend
; line
++)
5307 editor_change_line_indent(editor
, line
, increase
);
5309 sci_end_undo_action(sci
);
5312 /* restore caret and anchor position */
5313 if (caret_pos
>= caret_indent_pos
)
5314 caret_offset
+= sci_get_line_length(sci
, caret_line
) - caret_line_len
;
5315 if (anchor_pos
>= anchor_indent_pos
)
5316 anchor_offset
+= sci_get_line_length(sci
, anchor_line
) - anchor_line_len
;
5318 SSM(sci
, SCI_SETCURRENTPOS
, sci_get_position_from_line(sci
, caret_line
) + caret_offset
, 0);
5319 SSM(sci
, SCI_SETANCHOR
, sci_get_position_from_line(sci
, anchor_line
) + anchor_offset
, 0);
5323 /** Gets snippet by name.
5325 * If @a editor is passed, returns a snippet specific to the document filetype.
5326 * If @a editor is @c NULL, returns a snippet from the default set.
5328 * @param editor @nullable Editor or @c NULL.
5329 * @param snippet_name Snippet name.
5330 * @return @nullable snippet or @c NULL if it was not found. Must not be freed.
5333 const gchar
*editor_find_snippet(GeanyEditor
*editor
, const gchar
*snippet_name
)
5335 const gchar
*subhash_name
= editor
? editor
->document
->file_type
->name
: "Default";
5336 GHashTable
*subhash
= g_hash_table_lookup(snippet_hash
, subhash_name
);
5338 return subhash
? g_hash_table_lookup(subhash
, snippet_name
) : NULL
;
5342 /** Replaces all special sequences in @a snippet and inserts it at @a pos.
5343 * If you insert at the current position, consider calling @c sci_scroll_caret()
5344 * after this function.
5350 void editor_insert_snippet(GeanyEditor
*editor
, gint pos
, const gchar
*snippet
)
5354 pattern
= g_string_new(snippet
);
5355 snippets_make_replacements(editor
, pattern
);
5356 editor_insert_text_block(editor
, pattern
->str
, pos
, -1, -1, TRUE
);
5357 g_string_free(pattern
, TRUE
);
5360 static void *copy_(void *src
) { return src
; }
5361 static void free_(void *doc
) { }
5364 * Gets the GType of GeanyEditor
5366 * @return the GeanyEditor type */
5368 GType
editor_get_type (void);
5370 G_DEFINE_BOXED_TYPE(GeanyEditor
, editor
, copy_
, free_
);