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"
50 #include "projectprivate.h"
51 #include "sciwrappers.h"
54 #include "templates.h"
64 #include <gdk/gdkkeysyms.h>
67 static GHashTable
*snippet_hash
= NULL
;
68 static GtkAccelGroup
*snippet_accel_group
= NULL
;
69 static gboolean autocomplete_scope_shown
= FALSE
;
71 static const gchar geany_cursor_marker
[] = "__GEANY_CURSOR_MARKER__";
73 /* holds word under the mouse or keyboard cursor */
74 static gchar current_word
[GEANY_MAX_WORD_LENGTH
];
76 /* Initialised in keyfile.c. */
77 GeanyEditorPrefs editor_prefs
;
79 EditorInfo editor_info
= {current_word
, -1};
89 } calltip
= {NULL
, FALSE
, NULL
, 0, 0, NULL
};
91 static gchar indent
[100];
94 static void on_new_line_added(GeanyEditor
*editor
);
95 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
96 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
97 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
98 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
99 static void close_block(GeanyEditor
*editor
, gint pos
);
100 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
101 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
102 const gchar
*wc
, gboolean stem
);
103 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
);
104 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
);
105 static void snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
);
106 static GeanyFiletype
*editor_get_filetype_at_line(GeanyEditor
*editor
, gint line
);
107 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
);
110 void editor_snippets_free(void)
112 g_hash_table_destroy(snippet_hash
);
113 gtk_window_remove_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
117 static void snippets_load(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
119 gsize i
, j
, len
= 0, len_keys
= 0;
120 gchar
**groups_user
, **groups_sys
;
121 gchar
**keys_user
, **keys_sys
;
125 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
127 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
129 /* first read all globally defined auto completions */
130 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
131 for (i
= 0; i
< len
; i
++)
133 if (strcmp(groups_sys
[i
], "Keybindings") == 0)
135 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
136 /* create new hash table for the read section (=> filetype) */
137 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
138 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
140 for (j
= 0; j
< len_keys
; j
++)
142 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
143 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
145 g_strfreev(keys_sys
);
147 g_strfreev(groups_sys
);
149 /* now read defined completions in user's configuration directory and add / replace them */
150 groups_user
= g_key_file_get_groups(userconfig
, &len
);
151 for (i
= 0; i
< len
; i
++)
153 if (strcmp(groups_user
[i
], "Keybindings") == 0)
155 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
157 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
159 { /* new key found, create hash table */
160 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
161 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
163 for (j
= 0; j
< len_keys
; j
++)
165 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
167 { /* value = NULL means the key doesn't yet exist, so insert */
168 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
169 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
172 { /* old key and value will be freed by destroy function (g_free) */
173 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
174 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
177 g_strfreev(keys_user
);
179 g_strfreev(groups_user
);
183 static gboolean
on_snippet_keybinding_activate(gchar
*key
)
185 GeanyDocument
*doc
= document_get_current();
188 if (!doc
|| !gtk_widget_has_focus(GTK_WIDGET(doc
->editor
->sci
)))
191 s
= snippets_find_completion_by_name(doc
->file_type
->name
, key
);
192 if (!s
) /* allow user to specify keybindings for "special" snippets */
194 GHashTable
*specials
= g_hash_table_lookup(snippet_hash
, "Special");
196 if (G_LIKELY(specials
!= NULL
))
197 s
= g_hash_table_lookup(specials
, key
);
205 editor_insert_snippet(doc
->editor
, sci_get_current_position(doc
->editor
->sci
), s
);
206 sci_scroll_caret(doc
->editor
->sci
);
212 static void add_kb(GKeyFile
*keyfile
, const gchar
*group
, gchar
**keys
)
218 for (i
= 0; i
< g_strv_length(keys
); i
++)
221 GdkModifierType mods
;
222 gchar
*accel_string
= g_key_file_get_value(keyfile
, group
, keys
[i
], NULL
);
224 gtk_accelerator_parse(accel_string
, &key
, &mods
);
226 if (key
== 0 && mods
== 0)
228 g_warning("Can not parse accelerator \"%s\" from user snippets.conf", accel_string
);
229 g_free(accel_string
);
232 g_free(accel_string
);
234 gtk_accel_group_connect(snippet_accel_group
, key
, mods
, 0,
235 g_cclosure_new_swap((GCallback
)on_snippet_keybinding_activate
,
236 g_strdup(keys
[i
]), (GClosureNotify
)g_free
));
241 static void load_kb(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
243 const gchar kb_group
[] = "Keybindings";
244 gchar
**keys
= g_key_file_get_keys(userconfig
, kb_group
, NULL
, NULL
);
247 /* remove overridden keys from system keyfile */
248 foreach_strv(ptr
, keys
)
249 g_key_file_remove_key(sysconfig
, kb_group
, *ptr
, NULL
);
251 add_kb(userconfig
, kb_group
, keys
);
254 keys
= g_key_file_get_keys(sysconfig
, kb_group
, NULL
, NULL
);
255 add_kb(sysconfig
, kb_group
, keys
);
260 void editor_snippets_init(void)
262 gchar
*sysconfigfile
, *userconfigfile
;
263 GKeyFile
*sysconfig
= g_key_file_new();
264 GKeyFile
*userconfig
= g_key_file_new();
266 sysconfigfile
= g_build_filename(app
->datadir
, "snippets.conf", NULL
);
267 userconfigfile
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
269 /* check for old autocomplete.conf files (backwards compatibility) */
270 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
))
271 SETPTR(userconfigfile
, g_build_filename(app
->configdir
, "autocomplete.conf", NULL
));
273 /* load the actual config files */
274 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
275 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
277 snippets_load(sysconfig
, userconfig
);
279 /* setup snippet keybindings */
280 snippet_accel_group
= gtk_accel_group_new();
281 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
282 load_kb(sysconfig
, userconfig
);
284 g_free(sysconfigfile
);
285 g_free(userconfigfile
);
286 g_key_file_free(sysconfig
);
287 g_key_file_free(userconfig
);
291 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
294 GeanyEditor
*editor
= data
;
295 GeanyDocument
*doc
= editor
->document
;
297 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
298 * fake event to show the editor menu triggered by a key event where we want to use the
299 * text cursor position. */
300 if (event
->x
> 0.0 && event
->y
> 0.0)
301 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
302 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
304 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
306 if (event
->button
== 1)
308 guint state
= keybindings_get_modifiers(event
->state
);
310 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
312 gint ss
= sci_get_selection_start(editor
->sci
);
313 sci_set_selection_end(editor
->sci
, ss
);
315 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GEANY_PRIMARY_MOD_MASK
)
317 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
319 editor_find_current_word(editor
, editor_info
.click_pos
,
320 current_word
, sizeof current_word
, NULL
);
322 return symbols_goto_tag(current_word
, TRUE
);
324 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
327 return document_check_disk_status(doc
, FALSE
);
330 /* calls the edit popup menu in the editor */
331 if (event
->button
== 3)
335 /* ensure the editor widget has the focus after this operation */
336 gtk_widget_grab_focus(widget
);
338 editor_find_current_word(editor
, editor_info
.click_pos
,
339 current_word
, sizeof current_word
, NULL
);
341 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
342 ui_update_popup_goto_items(can_goto
);
343 ui_update_popup_copy_items(doc
);
344 ui_update_insert_include_item(doc
, 0);
346 g_signal_emit_by_name(geany_object
, "update-editor-menu",
347 current_word
, editor_info
.click_pos
, doc
);
349 gtk_menu_popup_at_pointer(GTK_MENU(main_widgets
.editor_menu
), (GdkEvent
*) event
);
356 static gboolean
is_style_php(gint style
)
358 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
359 style
== SCE_HPHP_COMPLEX_VARIABLE
)
368 static gint
editor_get_long_line_type(void)
371 switch (app
->project
->priv
->long_line_behaviour
)
373 case 0: /* marker disabled */
375 case 1: /* use global settings */
377 case 2: /* custom (enabled) */
378 return editor_prefs
.long_line_type
;
381 if (!editor_prefs
.long_line_enabled
)
384 return editor_prefs
.long_line_type
;
388 static gint
editor_get_long_line_column(void)
390 if (app
->project
&& app
->project
->priv
->long_line_behaviour
!= 1 /* use global settings */)
391 return app
->project
->priv
->long_line_column
;
393 return editor_prefs
.long_line_column
;
397 #define get_project_pref(id)\
398 (app->project ? app->project->priv->id : editor_prefs.id)
400 static const GeanyEditorPrefs
*
401 get_default_prefs(void)
403 static GeanyEditorPrefs eprefs
;
405 eprefs
= editor_prefs
;
407 /* project overrides */
408 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(NULL
);
409 eprefs
.long_line_type
= editor_get_long_line_type();
410 eprefs
.long_line_column
= editor_get_long_line_column();
411 eprefs
.line_wrapping
= get_project_pref(line_wrapping
);
412 eprefs
.line_break_column
= get_project_pref(line_break_column
);
413 eprefs
.auto_continue_multiline
= get_project_pref(auto_continue_multiline
);
418 /* Gets the prefs for the editor.
419 * Prefs can be different according to project or document.
420 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
421 * settings may have changed, or if this function has been called for a different editor.
422 * @param editor The editor, or @c NULL to get the default prefs.
423 * @return The prefs. */
424 const GeanyEditorPrefs
*editor_get_prefs(GeanyEditor
*editor
)
426 static GeanyEditorPrefs eprefs
;
427 const GeanyEditorPrefs
*dprefs
= get_default_prefs();
429 /* Return the address of the default prefs to allow returning default and editor
430 * pref pointers without invalidating the contents of either. */
435 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(editor
);
436 /* add other editor & document overrides as needed */
441 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
443 ScintillaObject
*sci
;
446 g_return_if_fail(editor
!= NULL
);
449 /* When collapsing a fold range whose starting line is offscreen,
450 * scroll the starting line to display at the top of the view.
451 * Otherwise it can be confusing when the document scrolls down to hide
452 * the folded lines. */
453 if ((sci_get_fold_level(sci
, line
) & SC_FOLDLEVELNUMBERMASK
) > SC_FOLDLEVELBASE
&&
454 !(sci_get_fold_level(sci
, line
) & SC_FOLDLEVELHEADERFLAG
))
456 gint parent
= sci_get_fold_parent(sci
, line
);
457 gint first
= sci_get_first_visible_line(sci
);
459 parent
= SSM(sci
, SCI_VISIBLEFROMDOCLINE
, parent
, 0);
461 SSM(sci
, SCI_SETFIRSTVISIBLELINE
, parent
, 0);
464 /* find the fold header of the given line in case the one clicked isn't a fold point */
465 if (sci_get_fold_level(sci
, line
) & SC_FOLDLEVELHEADERFLAG
)
468 header
= sci_get_fold_parent(sci
, line
);
470 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
471 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
473 SSM(sci
, SCI_FOLDCHILDREN
, header
, SC_FOLDACTION_TOGGLE
);
477 SSM(sci
, SCI_FOLDLINE
, header
, SC_FOLDACTION_TOGGLE
);
482 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
484 /* left click to marker margin marks the line */
487 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
489 /*sci_marker_delete_all(editor->sci, 1);*/
490 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
492 /* left click on the folding margin to toggle folding state of current line */
493 else if (nt
->margin
== 2 && editor_prefs
.folding
)
495 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
496 editor_toggle_fold(editor
, line
, nt
->modifiers
);
501 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
503 ScintillaObject
*sci
= editor
->sci
;
504 gint pos
= sci_get_current_position(sci
);
506 /* since Scintilla 2.24, SCN_UPDATEUI is also sent on scrolling though we don't need to handle
507 * this and so ignore every SCN_UPDATEUI events except for content and selection changes */
508 if (! (nt
->updated
& SC_UPDATE_CONTENT
) && ! (nt
->updated
& SC_UPDATE_SELECTION
))
511 /* undo / redo menu update */
512 ui_update_popup_reundo_items(editor
->document
);
514 /* brace highlighting */
515 editor_highlight_braces(editor
, pos
);
517 ui_update_statusbar(editor
->document
, pos
);
520 /** experimental code for inverting selections */
523 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
525 /* need to get colour from getstyleat(), but how? */
526 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
527 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
530 sci_get_style_at(sci
, pos
);
536 static void check_line_breaking(GeanyEditor
*editor
, gint pos
)
538 ScintillaObject
*sci
= editor
->sci
;
539 gint line
, lstart
, col
;
542 if (!editor
->line_breaking
|| sci_get_selection_mode(editor
->sci
) != SC_SEL_STREAM
)
545 col
= sci_get_col_from_position(sci
, pos
);
547 line
= sci_get_current_line(sci
);
549 lstart
= sci_get_position_from_line(sci
, line
);
551 /* use column instead of position which might be different with multibyte characters */
552 if (col
< get_project_pref(line_break_column
))
555 /* look for the last space before line_break_column */
556 pos
= sci_get_position_from_col(sci
, line
, get_project_pref(line_break_column
));
560 c
= sci_get_char_at(sci
, --pos
);
563 gint diff
, last_pos
, last_col
;
565 /* remember the distance between the current column and the last column on the line
566 * (we use column position in case the previous line gets altered, such as removing
567 * trailing spaces or in case it contains multibyte characters) */
568 last_pos
= sci_get_line_end_position(sci
, line
);
569 last_col
= sci_get_col_from_position(sci
, last_pos
);
570 diff
= last_col
- col
;
572 /* break the line after the space */
573 sci_set_current_position(sci
, pos
+ 1, FALSE
);
574 sci_cancel(sci
); /* don't select from completion list */
575 sci_send_command(sci
, SCI_NEWLINE
);
578 /* correct cursor position (might not be at line end) */
579 last_pos
= sci_get_line_end_position(sci
, line
);
580 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
581 /* last column - distance is the desired column, then retrieve its document position */
582 pos
= sci_get_position_from_col(sci
, line
, last_col
- diff
);
583 sci_set_current_position(sci
, pos
, FALSE
);
584 sci_scroll_caret(sci
);
591 static void show_autocomplete(ScintillaObject
*sci
, gsize rootlen
, GString
*words
)
593 /* hide autocompletion if only option is already typed */
594 if (rootlen
>= words
->len
||
595 (words
->str
[rootlen
] == '?' && rootlen
>= words
->len
- 2))
597 sci_send_command(sci
, SCI_AUTOCCANCEL
);
600 /* store whether a calltip is showing, so we can reshow it after autocompletion */
601 calltip
.set
= (gboolean
) SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
602 SSM(sci
, SCI_AUTOCSETORDER
, SC_ORDER_CUSTOM
, 0);
603 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
->str
);
607 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
609 ScintillaObject
*sci
= editor
->sci
;
611 g_return_if_fail(tags
);
615 GString
*words
= g_string_sized_new(150);
618 for (j
= 0; j
< tags
->len
; ++j
)
620 TMTag
*tag
= tags
->pdata
[j
];
625 g_string_append_c(words
, '\n');
627 if (j
== editor_prefs
.autocompletion_max_entries
)
629 g_string_append(words
, "...");
632 g_string_append(words
, tag
->name
);
634 group
= tm_parser_get_sidebar_group(tag
->lang
, tag
->type
);
635 if (group
>= 0 && tm_parser_get_sidebar_info(tag
->lang
, group
, &icon_id
))
638 sprintf(buf
, "?%u", icon_id
+ 1);
639 g_string_append(words
, buf
);
642 show_autocomplete(sci
, rootlen
, words
);
643 g_string_free(words
, TRUE
);
648 static gint
scope_autocomplete_suffix(ScintillaObject
*sci
, TMParserType lang
,
649 gint pos
, gboolean
*scope_sep
)
651 const gchar
*sep
= tm_parser_scope_separator(lang
);
652 const gsize max_len
= 3;
653 gboolean is_scope_sep
;
656 buf
= g_alloca(max_len
+ 1);
657 sci_get_text_range(sci
, pos
- max_len
, pos
, buf
);
659 is_scope_sep
= g_str_has_suffix(buf
, sep
);
661 *scope_sep
= is_scope_sep
;
664 return tm_parser_scope_autocomplete_suffix(lang
, buf
);
668 static gboolean
reshow_calltip(gpointer data
)
672 g_return_val_if_fail(calltip
.sci
!= NULL
, FALSE
);
674 SSM(calltip
.sci
, SCI_CALLTIPCANCEL
, 0, 0);
675 doc
= document_get_current();
677 if (doc
&& doc
->editor
->sci
== calltip
.sci
)
679 /* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
680 * may be completely wrong in case the user cancelled the auto completion with the mouse */
681 SSM(calltip
.sci
, SCI_CALLTIPSHOW
, calltip
.pos
, (sptr_t
) calltip
.text
);
687 static void request_reshowing_calltip(SCNotification
*nt
)
691 /* delay the reshow of the calltip window to make sure it is actually displayed,
692 * without it might be not visible on SCN_AUTOCCANCEL. the priority is set to
693 * low to hopefully make Scintilla's events happen before reshowing since they
694 * seem to re-cancel the calltip on autoc menu hiding too */
695 g_idle_add_full(G_PRIORITY_LOW
, reshow_calltip
, NULL
, NULL
);
700 static gboolean
autocomplete_scope(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
702 ScintillaObject
*sci
= editor
->sci
;
703 gint pos
= sci_get_current_position(editor
->sci
);
704 gint line
= sci_get_current_line(editor
->sci
) + 1;
707 GeanyFiletype
*ft
= editor
->document
->file_type
;
709 gboolean function
= FALSE
;
711 gboolean scope_sep_typed
= FALSE
;
712 gboolean ret
= FALSE
;
713 const gchar
*current_scope
;
714 gint autocomplete_suffix_len
;
716 if (autocomplete_scope_shown
)
718 /* move at the operator position */
721 /* allow for a space between word and operator */
722 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
726 autocomplete_suffix_len
= scope_autocomplete_suffix(sci
, ft
->lang
, pos
,
728 if (autocomplete_suffix_len
== 0)
731 pos
-= autocomplete_suffix_len
;
733 /* allow for a space between word and operator */
734 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
737 /* if function or array index, skip to matching brace */
738 brace_char
= sci_get_char_at(sci
, pos
- 1);
739 if (pos
> 0 && (brace_char
== ')' || brace_char
== ']'))
741 gint brace_pos
= sci_find_matching_brace(sci
, pos
- 1);
746 function
= brace_char
== ')';
749 /* allow for a space between opening brace and name */
750 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
754 name
= editor_get_word_at_pos(editor
, pos
, NULL
);
758 /* check if invoked on member */
760 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
762 member
= scope_autocomplete_suffix(sci
, ft
->lang
, pos
, NULL
) > 0;
764 if (symbols_get_current_scope(editor
->document
, ¤t_scope
) == -1)
766 tags
= tm_workspace_find_scope_members(editor
->document
->tm_file
, name
, function
,
767 member
, current_scope
, line
, scope_sep_typed
);
770 GPtrArray
*filtered
= g_ptr_array_new();
774 foreach_ptr_array(tag
, i
, tags
)
776 if (g_str_has_prefix(tag
->name
, root
))
777 g_ptr_array_add(filtered
, tag
);
780 if (filtered
->len
> 0)
782 show_tags_list(editor
, filtered
, rootlen
);
786 g_ptr_array_free(tags
, TRUE
);
787 g_ptr_array_free(filtered
, TRUE
);
795 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
797 ScintillaObject
*sci
= editor
->sci
;
798 gint pos
= sci_get_current_position(sci
);
803 { /* simple indentation (only for CR format) */
804 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
805 on_new_line_added(editor
);
809 { /* simple indentation (for CR/LF and LF format) */
810 on_new_line_added(editor
);
814 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
817 { /* close xml-tags */
818 handle_xml(editor
, pos
, nt
->ch
);
823 auto_close_chars(sci
, pos
, nt
->ch
);
825 editor_show_calltip(editor
, --pos
);
829 { /* hide calltips */
830 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0))
832 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
834 g_free(calltip
.text
);
846 auto_close_chars(sci
, pos
, nt
->ch
);
850 { /* closing bracket handling */
851 if (editor
->auto_indent
)
852 close_block(editor
, pos
- 1);
855 /* scope autocompletion */
857 case ':': /* C/C++ class:: syntax */
858 /* tag autocompletion */
861 if (! editor_start_auto_complete(editor
, pos
, FALSE
))
862 request_reshowing_calltip(nt
);
864 editor_start_auto_complete(editor
, pos
, FALSE
);
867 check_line_breaking(editor
, pos
);
871 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
872 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
873 gboolean force
, gint visLevels
, gint level
)
875 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
876 gint levelLine
= level
;
878 while (*line
<= lineMaxSubord
)
883 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
885 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
890 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
893 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
894 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
899 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
901 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
902 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
908 if (!sci_get_fold_expanded(sci
, *line
))
909 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
910 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
914 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
926 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
928 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
930 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
932 /* Adding a fold point */
933 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
934 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0))
935 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
938 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
940 if (! sci_get_fold_expanded(sci
, line
))
941 { /* Removing the fold from one that has been contracted so should expand
942 * otherwise lines are left invisible with no way to make them visible */
943 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
944 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0))
945 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
948 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
949 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
951 if (!SSM(sci
, SCI_GETALLLINESVISIBLE
, 0, 0)) {
952 /* See if should still be hidden */
953 gint parentLine
= sci_get_fold_parent(sci
, line
);
956 SSM(sci
, SCI_SHOWLINES
, line
, line
);
958 else if (sci_get_fold_expanded(sci
, parentLine
) &&
959 sci_get_line_is_visible(sci
, parentLine
))
961 SSM(sci
, SCI_SHOWLINES
, line
, line
);
968 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
969 gboolean enforcePolicy
)
971 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
972 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
975 for (line
= lineStart
; line
<= lineEnd
; line
++)
977 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
982 static void auto_update_margin_width(GeanyEditor
*editor
)
984 gint next_linecount
= 1;
985 gint linecount
= sci_get_line_count(editor
->sci
);
986 GeanyDocument
*doc
= editor
->document
;
988 while (next_linecount
<= linecount
)
989 next_linecount
*= 10;
991 if (editor
->document
->priv
->line_count
!= next_linecount
)
993 doc
->priv
->line_count
= next_linecount
;
994 sci_set_line_numbers(editor
->sci
, TRUE
);
999 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
1001 gint pos
= sci_get_current_position(sci
);
1003 sci_insert_text(sci
, pos
, text
);
1004 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
1008 /* Complete the next word part from @a entry */
1009 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
1011 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
1013 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
1014 stem
= current_word
;
1015 if (strstr(text
, stem
) != text
)
1016 return FALSE
; /* shouldn't happen */
1017 if (strlen(text
) <= strlen(stem
))
1020 text
+= strlen(stem
); /* skip stem */
1021 ptr
= strstr(text
+ 1, "_");
1025 partial_complete(editor
->sci
, text
);
1031 foreach_str(ptr
, text
+ 1)
1035 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
1038 partial_complete(editor
->sci
, text
);
1047 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
1048 * Plugins can connect to the "editor-notify" signal. */
1049 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
1050 gpointer scnt
, gpointer data
)
1052 GeanyEditor
*editor
= data
;
1055 g_return_if_fail(editor
!= NULL
);
1057 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
1061 /* recalculate margins width */
1062 static void update_margins(ScintillaObject
*sci
)
1064 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
);
1065 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
1066 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
1070 static gboolean
on_editor_notify(G_GNUC_UNUSED GObject
*object
, GeanyEditor
*editor
,
1071 SCNotification
*nt
, G_GNUC_UNUSED gpointer data
)
1073 ScintillaObject
*sci
= editor
->sci
;
1074 GeanyDocument
*doc
= editor
->document
;
1076 switch (nt
->nmhdr
.code
)
1078 case SCN_SAVEPOINTLEFT
:
1079 document_set_text_changed(doc
, TRUE
);
1082 case SCN_SAVEPOINTREACHED
:
1083 document_set_text_changed(doc
, FALSE
);
1086 case SCN_MODIFYATTEMPTRO
:
1090 case SCN_MARGINCLICK
:
1091 on_margin_click(editor
, nt
);
1095 on_update_ui(editor
, nt
);
1099 /* Visible lines are only laid out accurately just before painting,
1100 * so we need to only call editor_scroll_to_line here, because the document
1101 * may have line wrapping and folding enabled.
1102 * https://scintilla.sourceforge.io/ScintillaDoc.html#LineWrapping
1103 * This is important e.g. when loading a session and switching pages
1104 * and having the cursor scroll in view. */
1105 /* FIXME: Really we want to do this just before painting, not after it
1106 * as it will cause repainting. */
1107 if (editor
->scroll_percent
> 0.0F
)
1109 editor_scroll_to_line(editor
, -1, editor
->scroll_percent
);
1110 /* disable further scrolling */
1111 editor
->scroll_percent
= -1.0F
;
1116 if (editor_prefs
.show_linenumber_margin
&& (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) && nt
->linesAdded
)
1118 /* automatically adjust Scintilla's line numbers margin width */
1119 auto_update_margin_width(editor
);
1121 if (nt
->modificationType
& SC_STARTACTION
&& ! ignore_callback
)
1123 /* get notified about undo changes */
1124 document_undo_add(doc
, UNDO_SCINTILLA
, NULL
);
1126 if (editor_prefs
.folding
&& (nt
->modificationType
& SC_MOD_CHANGEFOLD
) != 0)
1128 /* handle special fold cases, e.g. #1923350 */
1129 fold_changed(sci
, nt
->line
, nt
->foldLevelNow
, nt
->foldLevelPrev
);
1131 if (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
))
1133 document_update_tag_list_in_idle(doc
);
1138 on_char_added(editor
, nt
);
1141 case SCN_USERLISTSELECTION
:
1142 if (nt
->listType
== 1)
1144 sci_add_text(sci
, nt
->text
);
1148 case SCN_AUTOCSELECTION
:
1149 if (g_str_equal(nt
->text
, "..."))
1156 case SCN_AUTOCCANCELLED
:
1157 /* now that autocomplete is finishing or was cancelled, reshow calltips
1158 * if they were showing */
1159 autocomplete_scope_shown
= FALSE
;
1160 request_reshowing_calltip(nt
);
1163 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
1166 case SCN_URIDROPPED
:
1167 if (nt
->text
!= NULL
)
1169 document_open_file_list(nt
->text
, strlen(nt
->text
));
1173 case SCN_CALLTIPCLICK
:
1174 if (nt
->position
> 0)
1176 switch (nt
->position
)
1178 case 1: /* up arrow */
1179 if (calltip
.tag_index
> 0)
1180 calltip
.tag_index
--;
1183 case 2: calltip
.tag_index
++; break; /* down arrow */
1185 editor_show_calltip(editor
, -1);
1190 update_margins(sci
);
1193 /* we always return FALSE here to let plugins handle the event too */
1198 /* Note: this is the same as sci_get_tab_width(), but is still useful when you don't have
1199 * a scintilla pointer. */
1200 static gint
get_tab_width(const GeanyIndentPrefs
*indent_prefs
)
1202 if (indent_prefs
->type
== GEANY_INDENT_TYPE_BOTH
)
1203 return indent_prefs
->hard_tab_width
;
1205 return indent_prefs
->width
; /* tab width = indent width */
1209 /* Returns a string containing width chars of whitespace, filled with simple space
1210 * characters or with the right number of tab characters, according to the indent prefs.
1211 * (Result is filled with tabs *and* spaces if width isn't a multiple of
1212 * the tab width). */
1214 get_whitespace(const GeanyIndentPrefs
*iprefs
, gint width
)
1216 g_return_val_if_fail(width
>= 0, NULL
);
1219 return g_strdup("");
1221 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
1223 return g_strnfill(width
, ' ');
1226 { /* first fill text with tabs and fill the rest with spaces */
1227 const gint tab_width
= get_tab_width(iprefs
);
1228 gint tabs
= width
/ tab_width
;
1229 gint spaces
= width
% tab_width
;
1230 gint len
= tabs
+ spaces
;
1233 str
= g_malloc(len
+ 1);
1235 memset(str
, '\t', tabs
);
1236 memset(str
+ tabs
, ' ', spaces
);
1243 static const GeanyIndentPrefs
*
1244 get_default_indent_prefs(void)
1246 static GeanyIndentPrefs iprefs
;
1248 iprefs
= app
->project
? *app
->project
->priv
->indentation
: *editor_prefs
.indentation
;
1253 /** Gets the indentation prefs for the editor.
1254 * Prefs can be different according to project or document.
1255 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
1256 * settings may have changed, or if this function has been called for a different editor.
1257 * @param editor @nullable The editor, or @c NULL to get the default indent prefs.
1258 * @return The indent prefs. */
1260 const GeanyIndentPrefs
*
1261 editor_get_indent_prefs(GeanyEditor
*editor
)
1263 static GeanyIndentPrefs iprefs
;
1264 const GeanyIndentPrefs
*dprefs
= get_default_indent_prefs();
1266 /* Return the address of the default prefs to allow returning default and editor
1267 * pref pointers without invalidating the contents of either. */
1272 iprefs
.type
= editor
->indent_type
;
1273 iprefs
.width
= editor
->indent_width
;
1275 /* if per-document auto-indent is enabled, but we don't have a global mode set,
1276 * just use basic auto-indenting */
1277 if (editor
->auto_indent
&& iprefs
.auto_indent_mode
== GEANY_AUTOINDENT_NONE
)
1278 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_BASIC
;
1280 if (!editor
->auto_indent
)
1281 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_NONE
;
1287 static void on_new_line_added(GeanyEditor
*editor
)
1289 ScintillaObject
*sci
= editor
->sci
;
1290 gint line
= sci_get_current_line(sci
);
1292 /* simple indentation */
1293 if (editor
->auto_indent
)
1295 insert_indent_after_line(editor
, line
- 1);
1298 if (get_project_pref(auto_continue_multiline
))
1299 { /* " * " auto completion in multiline C/C++/D/Java comments */
1300 auto_multiline(editor
, line
);
1303 if (editor_prefs
.newline_strip
)
1304 { /* strip the trailing spaces on the previous line */
1305 editor_strip_line_trailing_spaces(editor
, line
- 1);
1310 static gboolean
lexer_has_braces(ScintillaObject
*sci
)
1312 gint lexer
= sci_get_lexer(sci
);
1318 case SCLEX_HTML
: /* for PHP & JS */
1319 case SCLEX_PHPSCRIPT
:
1320 case SCLEX_PASCAL
: /* for multiline comments? */
1333 /* Read indent chars for the line that pos is on into indent global variable.
1334 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1335 * instead in any new code. */
1336 static void read_indent(GeanyEditor
*editor
, gint pos
)
1338 ScintillaObject
*sci
= editor
->sci
;
1339 guint i
, len
, j
= 0;
1343 line
= sci_get_line_from_position(sci
, pos
);
1345 len
= sci_get_line_length(sci
, line
);
1346 linebuf
= sci_get_line(sci
, line
);
1348 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1350 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1351 indent
[j
++] = linebuf
[i
];
1360 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1362 gint start
= sci_get_position_from_line(sci
, line
);
1363 gint end
= sci_get_line_end_position(sci
, line
) - 1;
1364 gint lexer
= sci_get_lexer(sci
);
1368 for (pos
= end
; pos
>= start
&& count
< 1; pos
--)
1370 if (highlighting_is_code_style(lexer
, sci_get_style_at(sci
, pos
)))
1372 gchar c
= sci_get_char_at(sci
, pos
);
1381 return count
> 0 ? 1 : 0;
1385 /* gets the last code position on a line
1386 * warning: if there is no code position on the line, returns the start position */
1387 static gint
get_sci_line_code_end_position(ScintillaObject
*sci
, gint line
)
1389 gint start
= sci_get_position_from_line(sci
, line
);
1390 gint lexer
= sci_get_lexer(sci
);
1393 for (pos
= sci_get_line_end_position(sci
, line
) - 1; pos
> start
; pos
--)
1395 gint style
= sci_get_style_at(sci
, pos
);
1397 if (highlighting_is_code_style(lexer
, style
) && ! isspace(sci_get_char_at(sci
, pos
)))
1405 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1407 gint last_char
= get_sci_line_code_end_position(sci
, line
);
1409 /* add extra indentation for Python after colon */
1410 if (sci_get_char_at(sci
, last_char
) == ':' &&
1411 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1419 static gint
get_xml_indent(ScintillaObject
*sci
, gint line
)
1421 gboolean need_close
= FALSE
;
1422 gint end
= get_sci_line_code_end_position(sci
, line
);
1425 /* don't indent if there's a closing tag to the right of the cursor */
1426 pos
= sci_get_current_position(sci
);
1427 if (sci_get_char_at(sci
, pos
) == '<' &&
1428 sci_get_char_at(sci
, pos
+ 1) == '/')
1431 if (sci_get_char_at(sci
, end
) == '>' &&
1432 sci_get_char_at(sci
, end
- 1) != '/')
1434 gint style
= sci_get_style_at(sci
, end
);
1436 if (style
== SCE_H_TAG
|| style
== SCE_H_TAGUNKNOWN
)
1438 gint start
= sci_get_position_from_line(sci
, line
);
1439 gchar
*line_contents
= sci_get_contents_range(sci
, start
, end
+ 1);
1440 gchar
*opened_tag_name
= utils_find_open_xml_tag(line_contents
, end
+ 1 - start
);
1442 if (!EMPTY(opened_tag_name
))
1445 if (sci_get_lexer(sci
) == SCLEX_HTML
&& utils_is_short_html_tag(opened_tag_name
))
1448 g_free(line_contents
);
1449 g_free(opened_tag_name
);
1453 return need_close
? 1 : 0;
1457 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1459 ScintillaObject
*sci
= editor
->sci
;
1461 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1463 g_return_val_if_fail(line
>= 0, 0);
1465 size
= sci_get_line_indentation(sci
, line
);
1467 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1469 gint additional_indent
= 0;
1471 if (lexer_has_braces(sci
))
1472 additional_indent
= iprefs
->width
* get_brace_indent(sci
, line
);
1473 else if (sci_get_lexer(sci
) == SCLEX_PYTHON
) /* Python/Cython */
1474 additional_indent
= iprefs
->width
* get_python_indent(sci
, line
);
1476 /* HTML lexer "has braces" because of PHP and JavaScript. If get_brace_indent() did not
1477 * recommend us to insert additional indent, we are probably not in PHP/JavaScript chunk and
1478 * should make the XML-related check */
1479 if (additional_indent
== 0 &&
1480 (sci_get_lexer(sci
) == SCLEX_HTML
||
1481 sci_get_lexer(sci
) == SCLEX_XML
) &&
1482 editor
->document
->file_type
->priv
->xml_indent_tags
)
1484 size
+= iprefs
->width
* get_xml_indent(sci
, line
);
1487 size
+= additional_indent
;
1493 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
)
1495 ScintillaObject
*sci
= editor
->sci
;
1496 gint line_indent
= sci_get_line_indentation(sci
, line
);
1497 gint size
= get_indent_size_after_line(editor
, line
);
1498 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1504 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
&& size
== line_indent
)
1506 /* support tab indents, space aligns style - copy last line 'indent' exactly */
1507 gint start
= sci_get_position_from_line(sci
, line
);
1508 gint end
= sci_get_line_indent_position(sci
, line
);
1510 text
= sci_get_contents_range(sci
, start
, end
);
1514 text
= get_whitespace(iprefs
, size
);
1516 sci_add_text(sci
, text
);
1521 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
)
1523 const gchar
*closing_char
= NULL
;
1526 if (utils_isbrace(c
, 0))
1527 end_pos
= sci_find_matching_brace(sci
, pos
- 1);
1532 if ((editor_prefs
.autoclose_chars
& GEANY_AC_PARENTHESIS
) && end_pos
== -1)
1536 if ((editor_prefs
.autoclose_chars
& GEANY_AC_CBRACKET
) && end_pos
== -1)
1540 if ((editor_prefs
.autoclose_chars
& GEANY_AC_SBRACKET
) && end_pos
== -1)
1544 if (editor_prefs
.autoclose_chars
& GEANY_AC_SQUOTE
)
1548 if (editor_prefs
.autoclose_chars
& GEANY_AC_DQUOTE
)
1549 closing_char
= "\"";
1553 if (closing_char
!= NULL
)
1555 sci_add_text(sci
, closing_char
);
1556 sci_set_current_position(sci
, pos
, TRUE
);
1561 /* Finds a corresponding matching brace to the given pos
1562 * (this is taken from Scintilla Editor.cxx,
1563 * fit to work with close_block) */
1564 static gint
brace_match(ScintillaObject
*sci
, gint pos
)
1566 gchar chBrace
= sci_get_char_at(sci
, pos
);
1567 gchar chSeek
= utils_brace_opposite(chBrace
);
1569 gint direction
= -1;
1574 /* Hack: we need the style at @p pos but it isn't computed yet, so force styling
1575 * of this very position */
1576 sci_colourise(sci
, pos
, pos
+ 1);
1578 styBrace
= sci_get_style_at(sci
, pos
);
1580 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1584 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1586 chAtPos
= sci_get_char_at(sci
, pos
);
1587 styAtPos
= sci_get_style_at(sci
, pos
);
1589 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1591 if (chAtPos
== chBrace
)
1593 if (chAtPos
== chSeek
)
1604 /* Called after typing '}'. */
1605 static void close_block(GeanyEditor
*editor
, gint pos
)
1607 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1608 gint x
= 0, cnt
= 0;
1609 gint line
, line_len
;
1611 ScintillaObject
*sci
;
1612 gint line_indent
, last_indent
;
1614 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1616 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1620 if (! lexer_has_braces(sci
))
1623 line
= sci_get_line_from_position(sci
, pos
);
1624 line_len
= sci_get_line_end_position(sci
, line
) - sci_get_position_from_line(sci
, line
);
1626 /* check that the line is empty, to not kill text in the line */
1627 line_buf
= sci_get_line(sci
, line
);
1628 line_buf
[line_len
] = '\0';
1629 while (x
< line_len
)
1631 if (isspace(line_buf
[x
]))
1637 if ((line_len
- 1) != cnt
)
1640 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1642 gint start_brace
= brace_match(sci
, pos
);
1644 if (start_brace
>= 0)
1647 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1648 gint size
= sci_get_line_indentation(sci
, brace_line
);
1649 gchar
*ind
= get_whitespace(iprefs
, size
);
1650 gchar
*text
= g_strconcat(ind
, "}", NULL
);
1652 line_start
= sci_get_position_from_line(sci
, line
);
1653 sci_set_anchor(sci
, line_start
);
1654 sci_replace_sel(sci
, text
);
1659 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1662 /* GEANY_AUTOINDENT_CURRENTCHARS */
1663 line_indent
= sci_get_line_indentation(sci
, line
);
1664 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1666 if (line_indent
< last_indent
)
1668 line_indent
-= iprefs
->width
;
1669 line_indent
= MAX(0, line_indent
);
1670 sci_set_line_indentation(sci
, line
, line_indent
);
1674 /* checks whether @p c is an ASCII character (e.g. < 0x80) */
1675 #define IS_ASCII(c) (((unsigned char)(c)) < 0x80)
1678 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1679 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1680 * position can be -1, then the current position is used.
1681 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1682 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
1683 const gchar
*wc
, gboolean stem
)
1685 gint line
, line_start
, startword
, endword
;
1687 ScintillaObject
*sci
;
1689 g_return_if_fail(editor
!= NULL
);
1693 pos
= sci_get_current_position(sci
);
1695 line
= sci_get_line_from_position(sci
, pos
);
1696 line_start
= sci_get_position_from_line(sci
, line
);
1697 startword
= pos
- line_start
;
1698 endword
= pos
- line_start
;
1701 chunk
= sci_get_line(sci
, line
);
1704 wc
= GEANY_WORDCHARS
;
1706 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1707 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1708 * TODO: improve this code */
1709 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || ! IS_ASCII(chunk
[startword
- 1])))
1713 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || ! IS_ASCII(chunk
[endword
])))
1717 if (startword
!= endword
)
1719 chunk
[endword
] = '\0';
1721 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1724 g_strlcpy(word
, "", wordlen
);
1730 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1731 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1732 * position can be -1, then the current position is used.
1733 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1734 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
1737 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1741 /* Same as editor_find_current_word() but uses editor's word boundaries to decide what the word
1742 * is. This should be used e.g. to get the word to search for */
1743 void editor_find_current_word_sciwc(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
)
1748 g_return_if_fail(editor
!= NULL
);
1751 pos
= sci_get_current_position(editor
->sci
);
1753 start
= sci_word_start_position(editor
->sci
, pos
, TRUE
);
1754 end
= sci_word_end_position(editor
->sci
, pos
, TRUE
);
1756 if (start
== end
) /* caret in whitespaces sequence */
1760 if ((guint
)(end
- start
) >= wordlen
)
1761 end
= start
+ (wordlen
- 1);
1762 sci_get_text_range(editor
->sci
, start
, end
, word
);
1768 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1769 * Otherwise NULL is returned.
1770 * Additional wordchars can be specified to define what to consider as a word.
1772 * @param editor The editor to operate on.
1773 * @param pos The position where the word should be read from.
1774 * May be @c -1 to use the current position.
1775 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1776 * as part of a word. May be @c NULL to use the default wordchars,
1777 * see @ref GEANY_WORDCHARS.
1779 * @return @nullable A newly-allocated string containing the word at the given @a pos or @c NULL.
1780 * Should be freed when no longer needed.
1785 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1787 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1789 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1791 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1793 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1797 /* Read the word up to position @a pos. */
1798 static const gchar
*
1799 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1801 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1803 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1805 return (*word
) ? word
: NULL
;
1809 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1811 gint orig_pos
= pos
;
1813 while (pos
>= 0 && pos
> orig_pos
- 300)
1815 gchar c
= sci_get_char_at(sci
, pos
);
1816 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1824 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1827 gint orig_pos
= pos
;
1829 while (pos
> 0 && pos
> orig_pos
- 300)
1831 gchar c
= sci_get_char_at(sci
, pos
);
1833 if (c
== ')') brackets
++;
1834 else if (c
== '(') brackets
--;
1835 if (brackets
< 0) return pos
; /* found start bracket */
1842 static GPtrArray
*get_constructor_tags(GeanyFiletype
*ft
, TMTag
*tag
,
1843 const gchar
*constructor_method
)
1845 if (constructor_method
&& (tag
->type
== tm_tag_class_t
|| tag
->type
== tm_tag_struct_t
))
1847 const TMTagType arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1848 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1849 const gchar
*scope_sep
= tm_parser_scope_separator(ft
->lang
);
1850 gchar
*scope
= EMPTY(tag
->scope
) ? g_strdup(tag
->name
) :
1851 g_strjoin(scope_sep
, tag
->scope
, tag
->name
, NULL
);
1852 GPtrArray
*constructor_tags
;
1854 constructor_tags
= tm_workspace_find(constructor_method
, scope
, arg_types
, NULL
, ft
->lang
);
1856 if (constructor_tags
->len
!= 0)
1857 { /* found constructor tag, so use it instead of the class tag */
1858 return constructor_tags
;
1862 g_ptr_array_free(constructor_tags
, TRUE
);
1869 static void update_tag_name_and_scope_for_calltip(const gchar
*word
, TMTag
*tag
,
1870 const gchar
*constructor_method
,
1871 const gchar
**tag_name
, const gchar
**scope
)
1873 if (tag_name
== NULL
|| scope
== NULL
)
1876 /* Remove scope and replace name with the current calltip word if the current tag
1877 * is the constructor method of the current calltip word, e.g. for Python:
1878 * "SomeClass.__init__ (self, arg1, ...)" will be changed to "SomeClass (self, arg1, ...)" */
1879 if (constructor_method
&&
1880 utils_str_equal(constructor_method
, tag
->name
) &&
1881 !utils_str_equal(word
, tag
->name
))
1889 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1891 const gchar
*constructor_method
;
1894 GString
*str
= NULL
;
1897 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1899 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1900 tags
= tm_workspace_find(word
, NULL
, tm_tag_max_t
, NULL
, ft
->lang
);
1903 g_ptr_array_free(tags
, TRUE
);
1907 tag
= TM_TAG(tags
->pdata
[0]);
1909 /* user typed e.g. 'a = Classname(' in Python so lookup __init__() arguments */
1910 constructor_method
= tm_parser_get_constructor_method(tag
->lang
);
1911 if (constructor_method
)
1913 GPtrArray
*constructor_tags
= get_constructor_tags(ft
, tag
, constructor_method
);
1914 if (constructor_tags
)
1916 g_ptr_array_free(tags
, TRUE
);
1917 tags
= constructor_tags
;
1921 /* remove tags with no argument list */
1922 for (i
= 0; i
< tags
->len
; i
++)
1924 tag
= TM_TAG(tags
->pdata
[i
]);
1927 tags
->pdata
[i
] = NULL
;
1929 tm_tags_prune((GPtrArray
*) tags
);
1932 g_ptr_array_free(tags
, TRUE
);
1936 { /* remove duplicate calltips */
1937 TMTagAttrType sort_attr
[] = {tm_tag_attr_name_t
, tm_tag_attr_scope_t
,
1938 tm_tag_attr_arglist_t
, 0};
1940 tm_tags_sort((GPtrArray
*) tags
, sort_attr
, TRUE
, FALSE
);
1943 /* if the current word has changed since last time, start with the first tag match */
1944 if (! utils_str_equal(word
, calltip
.last_word
))
1945 calltip
.tag_index
= 0;
1946 /* cache the current word for next time */
1947 g_free(calltip
.last_word
);
1948 calltip
.last_word
= g_strdup(word
);
1949 calltip
.tag_index
= MIN(calltip
.tag_index
, tags
->len
- 1); /* ensure tag_index is in range */
1951 for (i
= calltip
.tag_index
; i
< tags
->len
; i
++)
1953 tag
= TM_TAG(tags
->pdata
[i
]);
1957 const gchar
*tag_name
= tag
->name
;
1958 const gchar
*scope
= tag
->scope
;
1961 update_tag_name_and_scope_for_calltip(word
, tag
, constructor_method
, &tag_name
, &scope
);
1962 f
= tm_parser_format_function(tag
->lang
, tag_name
, tag
->arglist
, tag
->var_type
, scope
);
1963 str
= g_string_new(NULL
);
1964 if (calltip
.tag_index
> 0)
1965 g_string_prepend(str
, "\001 "); /* up arrow */
1966 g_string_append(str
, f
);
1969 else /* add a down arrow */
1971 if (calltip
.tag_index
> 0) /* already have an up arrow */
1972 g_string_insert_c(str
, 1, '\002');
1974 g_string_prepend(str
, "\002 ");
1979 g_ptr_array_free(tags
, TRUE
);
1983 gchar
*result
= str
->str
;
1985 g_string_free(str
, FALSE
);
1992 /* use pos = -1 to search for the previous unmatched open bracket. */
1993 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
1995 gint orig_pos
= pos
; /* the position for the calltip */
1998 gchar word
[GEANY_MAX_WORD_LENGTH
];
2000 ScintillaObject
*sci
;
2002 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2003 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
2007 lexer
= sci_get_lexer(sci
);
2011 /* position of '(' is unknown, so go backwards from current position to find it */
2012 pos
= sci_get_current_position(sci
);
2015 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
2016 find_start_bracket(sci
, pos
);
2021 /* the style 1 before the brace (which may be highlighted) */
2022 style
= sci_get_style_at(sci
, pos
- 1);
2023 if (! highlighting_is_code_style(lexer
, style
))
2026 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
2029 /* skip possible generic/template specification, like foo<int>() */
2030 if (sci_get_char_at(sci
, pos
- 1) == '>')
2032 pos
= sci_find_matching_brace(sci
, pos
- 1);
2036 while (pos
> 0 && isspace(sci_get_char_at(sci
, pos
- 1)))
2041 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
2042 if (word
[0] == '\0')
2045 str
= find_calltip(word
, editor
->document
->file_type
);
2048 g_free(calltip
.text
); /* free the old calltip */
2050 calltip
.pos
= orig_pos
;
2053 utils_wrap_string(calltip
.text
, -1);
2054 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
2061 /* Current document & global tags autocompletion */
2063 autocomplete_tags(GeanyEditor
*editor
, GeanyFiletype
*ft
, const gchar
*root
, gsize rootlen
)
2065 GeanyDocument
*doc
= editor
->document
;
2066 const gchar
*current_scope
= NULL
;
2071 g_return_val_if_fail(editor
&& doc
, FALSE
);
2073 symbols_get_current_function(doc
, ¤t_scope
);
2074 current_line
= sci_get_current_line(editor
->sci
) + 1;
2076 tags
= tm_workspace_find_prefix(root
, doc
->tm_file
, current_line
, current_scope
,
2077 editor_prefs
.autocompletion_max_entries
);
2078 found
= tags
->len
> 0;
2080 show_tags_list(editor
, tags
, rootlen
);
2081 g_ptr_array_free(tags
, TRUE
);
2087 static gboolean
autocomplete_check_html(GeanyEditor
*editor
, gint style
, gint pos
)
2089 GeanyFiletype
*ft
= editor
->document
->file_type
;
2090 gboolean
try = FALSE
;
2092 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
2093 * (everything after SCE_HJ_START is for embedded scripting languages) */
2094 if (ft
->id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
2096 else if (sci_get_lexer(editor
->sci
) == SCLEX_XML
&& style
< SCE_HJ_START
)
2098 else if (ft
->id
== GEANY_FILETYPES_PHP
)
2100 /* use entity completion when style is outside of PHP styles */
2101 if (! is_style_php(style
))
2106 gchar root
[GEANY_MAX_WORD_LENGTH
];
2109 read_current_word(editor
, pos
, root
, sizeof(root
), GEANY_WORDCHARS
"&", TRUE
);
2111 /* Allow something like ""some text"".
2112 * for entity completion we want to have completion for '&' within words. */
2113 tmp
= strchr(root
, '&');
2116 return autocomplete_tags(editor
, filetypes_index(GEANY_FILETYPES_HTML
), tmp
, strlen(tmp
));
2123 /* Algorithm based on based on Scite's StartAutoCompleteWord()
2124 * @returns a sorted list of words matching @p root */
2125 static GSList
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
2128 gint len
, current
, word_end
;
2129 gint pos_find
, flags
;
2132 GSList
*words
= NULL
;
2133 struct Sci_TextToFind ttf
;
2135 len
= sci_get_length(sci
);
2136 current
= sci_get_current_position(sci
) - rootlen
;
2138 ttf
.lpstrText
= root
;
2140 ttf
.chrg
.cpMax
= len
;
2141 ttf
.chrgText
.cpMin
= 0;
2142 ttf
.chrgText
.cpMax
= 0;
2143 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
2145 /* search the whole document for the word root and collect results */
2146 pos_find
= SSM(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2147 while (pos_find
>= 0 && pos_find
< len
)
2149 word_end
= pos_find
+ rootlen
;
2150 if (pos_find
!= current
)
2152 word_end
= sci_word_end_position(sci
, word_end
, TRUE
);
2154 word_length
= word_end
- pos_find
;
2155 if (word_length
> rootlen
)
2157 word
= sci_get_contents_range(sci
, pos_find
, word_end
);
2158 /* search whether we already have the word in, otherwise add it */
2159 if (g_slist_find_custom(words
, word
, (GCompareFunc
)strcmp
) != NULL
)
2163 words
= g_slist_prepend(words
, word
);
2167 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
2171 ttf
.chrg
.cpMin
= word_end
;
2172 pos_find
= SSM(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2175 return g_slist_sort(words
, (GCompareFunc
)utils_str_casecmp
);
2179 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
2181 ScintillaObject
*sci
= editor
->sci
;
2182 GSList
*words
, *node
;
2186 words
= get_doc_words(sci
, root
, rootlen
);
2189 SSM(sci
, SCI_AUTOCCANCEL
, 0, 0);
2193 str
= g_string_sized_new(rootlen
* 2 * 10);
2194 foreach_slist(node
, words
)
2196 g_string_append(str
, node
->data
);
2199 g_string_append_c(str
, '\n');
2202 if (n_words
>= editor_prefs
.autocompletion_max_entries
)
2203 g_string_append(str
, "\n...");
2205 g_slist_free(words
);
2207 show_autocomplete(sci
, rootlen
, str
);
2208 g_string_free(str
, TRUE
);
2213 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
2215 gint rootlen
, lexer
, style
;
2217 gchar cword
[GEANY_MAX_WORD_LENGTH
];
2218 ScintillaObject
*sci
;
2219 gboolean ret
= FALSE
;
2220 const gchar
*wordchars
;
2223 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2225 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
2228 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
2229 * necessary styling information */
2230 if (G_UNLIKELY(pos
< 2))
2234 ft
= editor
->document
->file_type
;
2236 lexer
= sci_get_lexer(sci
);
2237 style
= sci_get_style_at(sci
, pos
- 2);
2239 /* don't autocomplete in comments and strings */
2240 if (!force
&& !highlighting_is_code_style(lexer
, style
))
2243 ret
= autocomplete_check_html(editor
, style
, pos
);
2245 if (ft
->id
== GEANY_FILETYPES_LATEX
)
2246 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
2247 else if (ft
->id
== GEANY_FILETYPES_CSS
)
2248 wordchars
= GEANY_WORDCHARS
"-"; /* add - because they are part of property names */
2250 wordchars
= GEANY_WORDCHARS
;
2252 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, TRUE
);
2254 rootlen
= strlen(root
);
2258 if (autocomplete_scope_shown
)
2260 autocomplete_scope_shown
= FALSE
;
2262 sci_send_command(sci
, SCI_AUTOCCANCEL
);
2267 ret
= autocomplete_scope(editor
, root
, rootlen
);
2268 if (!ret
&& autocomplete_scope_shown
)
2269 sci_send_command(sci
, SCI_AUTOCCANCEL
);
2270 autocomplete_scope_shown
= ret
;
2273 if (!ret
&& rootlen
> 0)
2275 if (ft
->id
== GEANY_FILETYPES_PHP
&& style
== SCE_HPHP_DEFAULT
&&
2276 rootlen
== 3 && strcmp(root
, "php") == 0 && pos
>= 5 &&
2277 sci_get_char_at(sci
, pos
- 5) == '<' &&
2278 sci_get_char_at(sci
, pos
- 4) == '?')
2280 /* nothing, don't complete PHP open tags */
2284 /* force is set when called by keyboard shortcut, otherwise start at the
2285 * editor_prefs.symbolcompletion_min_chars'th char */
2286 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
2288 /* complete tags, except if forcing when completion is already visible */
2289 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
2290 ret
= autocomplete_tags(editor
, editor
->document
->file_type
, root
, rootlen
);
2292 /* If forcing and there's nothing else to show, complete from words in document */
2293 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2294 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2305 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2307 gchar
*result
= NULL
;
2310 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2312 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2315 result
= g_hash_table_lookup(tmp
, name
);
2317 /* whether nothing is set for the current filetype(tmp is NULL) or
2318 * the particular completion for this filetype is not set (result is NULL) */
2319 if (tmp
== NULL
|| result
== NULL
)
2321 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2324 result
= g_hash_table_lookup(tmp
, name
);
2327 /* if result is still NULL here, no completion could be found */
2329 /* result is owned by the hash table and will be freed when the table will destroyed */
2334 static void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2337 GString
*pattern
= user_data
;
2339 g_return_if_fail(key
!= NULL
);
2340 g_return_if_fail(value
!= NULL
);
2342 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2344 utils_string_replace_all(pattern
, needle
, (gchar
*) value
);
2349 static void fix_indentation(GeanyEditor
*editor
, GString
*buf
)
2351 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2354 gint cflags
= G_REGEX_MULTILINE
;
2356 /* transform leading tabs into indent widths (in spaces) */
2357 whitespace
= g_strnfill(iprefs
->width
, ' ');
2358 regex
= g_regex_new("^ *(\t)", cflags
, 0, NULL
);
2359 while (utils_string_regex_replace_all(buf
, regex
, 1, whitespace
, TRUE
));
2360 g_regex_unref(regex
);
2362 /* remaining tabs are for alignment */
2363 if (iprefs
->type
!= GEANY_INDENT_TYPE_TABS
)
2364 utils_string_replace_all(buf
, "\t", whitespace
);
2366 /* use leading tabs */
2367 if (iprefs
->type
!= GEANY_INDENT_TYPE_SPACES
)
2371 /* for tabs+spaces mode we want the real tab width, not indent width */
2372 SETPTR(whitespace
, g_strnfill(sci_get_tab_width(editor
->sci
), ' '));
2373 str
= g_strdup_printf("^\t*(%s)", whitespace
);
2375 regex
= g_regex_new(str
, cflags
, 0, NULL
);
2376 while (utils_string_regex_replace_all(buf
, regex
, 1, "\t", TRUE
));
2377 g_regex_unref(regex
);
2386 Sci_Position start
, len
;
2390 #define CURSOR_PLACEHOLDER "_" /* Would rather use … but not all docs are unicode */
2393 /* Replaces the internal cursor markers with the placeholder suitable for
2394 * display. Except for the first cursor if indicator_for_first is FALSE,
2395 * which is simply deleted.
2397 * Returns insertion points as SelectionRange list, so that the caller
2398 * can use the positions (currently for indicators). */
2399 static GSList
*replace_cursor_markers(GeanyEditor
*editor
, GString
*template,
2400 gboolean indicator_for_first
)
2403 GSList
*temp_list
= NULL
;
2404 gint cursor_steps
= 0;
2405 SelectionRange
*sel
;
2409 cursor_steps
= utils_string_find(template, cursor_steps
, -1, geany_cursor_marker
);
2410 if (cursor_steps
== -1)
2413 sel
= g_new0(SelectionRange
, 1);
2414 sel
->start
= cursor_steps
;
2415 g_string_erase(template, cursor_steps
, strlen(geany_cursor_marker
));
2416 if (i
> 0 || indicator_for_first
)
2418 g_string_insert(template, cursor_steps
, CURSOR_PLACEHOLDER
);
2419 sel
->len
= sizeof(CURSOR_PLACEHOLDER
) - 1;
2422 temp_list
= g_slist_append(temp_list
, sel
);
2429 /** Inserts text, replacing \\t tab chars (@c 0x9) and \\n newline chars (@c 0xA)
2430 * accordingly for the document.
2431 * - Leading tabs are replaced with the correct indentation.
2432 * - Non-leading tabs are replaced with spaces (except when using 'Tabs' indent type).
2433 * - Newline chars are replaced with the correct line ending string.
2434 * This is very useful for inserting code without having to handle the indent
2435 * type yourself (Tabs & Spaces mode can be tricky).
2436 * @param editor Editor.
2437 * @param text Intended as e.g. @c "if (foo)\n\tbar();".
2438 * @param insert_pos Document position to insert text at.
2439 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2440 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2441 * -1 to read the indent size from the line with @a insert_pos on it.
2442 * @param replace_newlines Whether to replace newlines. If
2443 * newlines have been replaced already, this should be false, to avoid errors e.g. on Windows.
2444 * @warning Make sure all \\t tab chars in @a text are intended as indent widths or alignment,
2445 * not hard tabs, as those won't be preserved.
2446 * @note This doesn't scroll the cursor in view afterwards. **/
2448 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2449 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2451 ScintillaObject
*sci
= editor
->sci
;
2452 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2454 const gchar
*eol
= editor_get_eol_char(editor
);
2455 GSList
*jump_locs
, *item
;
2457 g_return_if_fail(text
);
2458 g_return_if_fail(editor
!= NULL
);
2459 g_return_if_fail(insert_pos
>= 0);
2461 buf
= g_string_new(text
);
2463 if (cursor_index
>= 0)
2464 g_string_insert(buf
, cursor_index
, geany_cursor_marker
); /* remember cursor pos */
2466 if (newline_indent_size
== -1)
2468 /* count indent size up to insert_pos instead of asking sci
2469 * because there may be spaces after it */
2470 gchar
*tmp
= sci_get_line(sci
, line_start
);
2473 idx
= insert_pos
- sci_get_position_from_line(sci
, line_start
);
2475 newline_indent_size
= count_indent_size(editor
, tmp
);
2479 /* Add line indents (in spaces) */
2480 if (newline_indent_size
> 0)
2482 const gchar
*nl
= replace_newlines
? "\n" : eol
;
2485 whitespace
= g_strnfill(newline_indent_size
, ' ');
2486 SETPTR(whitespace
, g_strconcat(nl
, whitespace
, NULL
));
2487 utils_string_replace_all(buf
, nl
, whitespace
);
2491 /* transform line endings */
2492 if (replace_newlines
)
2493 utils_string_replace_all(buf
, "\n", eol
);
2495 fix_indentation(editor
, buf
);
2497 jump_locs
= replace_cursor_markers(editor
, buf
, cursor_index
< 0);
2498 sci_insert_text(sci
, insert_pos
, buf
->str
);
2500 foreach_list(item
, jump_locs
)
2502 SelectionRange
*sel
= item
->data
;
2503 gint start
= insert_pos
+ sel
->start
;
2504 gint end
= start
+ sel
->len
;
2505 editor_indicator_set_on_range(editor
, GEANY_INDICATOR_SNIPPET
, start
, end
);
2506 /* jump to first cursor position initially */
2507 if (item
== jump_locs
)
2508 sci_set_selection(sci
, start
, end
);
2511 /* Set cursor to the requested index, or by default to after the snippet */
2512 if (cursor_index
>= 0)
2513 sci_set_current_position(sci
, insert_pos
+ cursor_index
, FALSE
);
2514 else if (jump_locs
== NULL
)
2515 sci_set_current_position(sci
, insert_pos
+ buf
->len
, FALSE
);
2517 g_slist_free_full(jump_locs
, g_free
);
2518 g_string_free(buf
, TRUE
);
2522 static gboolean
find_next_snippet_indicator(GeanyEditor
*editor
, SelectionRange
*sel
)
2524 ScintillaObject
*sci
= editor
->sci
;
2525 gint pos
= sci_get_current_position(sci
);
2527 if (pos
== sci_get_length(sci
))
2528 return FALSE
; /* EOF */
2530 /* Rewind the cursor a bit if we're in the middle (or start) of an indicator,
2531 * and treat that as the next indicator. */
2532 while (SSM(sci
, SCI_INDICATORVALUEAT
, GEANY_INDICATOR_SNIPPET
, pos
) && pos
> 0)
2535 /* Be careful at the beginning of the file */
2536 if (SSM(sci
, SCI_INDICATORVALUEAT
, GEANY_INDICATOR_SNIPPET
, pos
))
2539 sel
->start
= SSM(sci
, SCI_INDICATOREND
, GEANY_INDICATOR_SNIPPET
, pos
);
2540 sel
->len
= SSM(sci
, SCI_INDICATOREND
, GEANY_INDICATOR_SNIPPET
, sel
->start
) - sel
->start
;
2542 /* 0 if there is no remaining cursor */
2543 return sel
->len
> 0;
2547 /* Move the cursor to the next specified cursor position in an inserted snippet.
2548 * Can, and should, be optimized to give better results */
2549 gboolean
editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2551 ScintillaObject
*sci
= editor
->sci
;
2554 if (find_next_snippet_indicator(editor
, &sel
))
2556 sci_indicator_set(sci
, GEANY_INDICATOR_SNIPPET
);
2557 sci_set_selection(sci
, sel
.start
, sel
.start
+ sel
.len
);
2567 static void snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
)
2569 GHashTable
*specials
;
2571 /* replace 'special' completions */
2572 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2573 if (G_LIKELY(specials
!= NULL
))
2575 g_hash_table_foreach(specials
, snippets_replace_specials
, pattern
);
2578 /* now transform other wildcards */
2579 utils_string_replace_all(pattern
, "%newline%", "\n");
2580 utils_string_replace_all(pattern
, "%ws%", "\t");
2582 /* replace %cursor% by a very unlikely string marker */
2583 utils_string_replace_all(pattern
, "%cursor%", geany_cursor_marker
);
2585 /* unescape '%' after all %wildcards% */
2586 templates_replace_valist(pattern
, "{pc}", "%", NULL
);
2588 /* replace any template {foo} wildcards */
2589 templates_replace_common(pattern
, editor
->document
->file_name
, editor
->document
->file_type
, NULL
);
2593 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2595 ScintillaObject
*sci
= editor
->sci
;
2597 const gchar
*completion
;
2599 gint ft_id
= editor
->document
->file_type
->id
;
2601 str
= g_strdup(word
);
2604 completion
= snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
);
2605 if (completion
== NULL
)
2611 /* remove the typed word, it will be added again by the used auto completion
2612 * (not really necessary but this makes the auto completion more flexible,
2613 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2614 str_len
= strlen(str
);
2615 sci_set_selection_start(sci
, pos
- str_len
);
2616 sci_set_selection_end(sci
, pos
);
2617 sci_replace_sel(sci
, "");
2618 pos
-= str_len
; /* pos has changed while deleting */
2620 editor_insert_snippet(editor
, pos
, completion
);
2621 sci_scroll_caret(sci
);
2628 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2630 gint line
= sci_get_line_from_position(sci
, pos
);
2633 /* skip any trailing spaces */
2636 c
= sci_get_char_at(sci
, pos
);
2637 if (c
== ' ' || c
== '\t')
2643 return (pos
== sci_get_line_end_position(sci
, line
));
2647 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2649 gboolean result
= FALSE
;
2652 ScintillaObject
*sci
;
2654 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2657 if (sci_has_selection(sci
))
2659 /* return if we are editing an existing line (chars on right of cursor) */
2660 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2661 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_KEY_space
&&
2662 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2665 wc
= snippets_find_completion_by_name("Special", "wordchars");
2666 word
= editor_read_word_stem(editor
, pos
, wc
);
2668 /* prevent completion of "for " */
2670 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2672 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2673 result
= snippets_complete_constructs(editor
, pos
, word
);
2674 sci_end_undo_action(sci
);
2676 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2682 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2684 ScintillaObject
*sci
= editor
->sci
;
2685 gchar
*to_insert
= NULL
;
2689 const gchar
*gt
= ">";
2690 /* if there is already a '>' behind the cursor, don't add it */
2691 if (sci_get_char_at(sci
, pos
) == '>')
2694 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2697 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2699 sci_start_undo_action(sci
);
2700 sci_replace_sel(sci
, to_insert
);
2702 sci_set_selection(sci
, pos
, pos
);
2703 sci_end_undo_action(sci
);
2709 * (stolen from anjuta and heavily modified)
2710 * This routine will auto complete XML or HTML tags that are still open by closing them
2711 * @param ch The character we are dealing with, currently only works with the '>' character
2712 * @return True if handled, false otherwise
2714 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2716 ScintillaObject
*sci
= editor
->sci
;
2717 gint lexer
= sci_get_lexer(sci
);
2718 gint min
, size
, style
;
2719 gchar
*str_found
, sel
[512];
2720 gboolean result
= FALSE
;
2722 /* If the user has turned us off, quit now.
2723 * This may make sense only in certain languages */
2724 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2727 /* return if we are inside any embedded script */
2728 style
= sci_get_style_at(sci
, pos
);
2729 if (style
> SCE_H_XCCOMMENT
&& ! highlighting_is_string_style(lexer
, style
))
2732 /* if ch is /, check for </, else quit */
2733 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2736 /* Grab the last 512 characters or so */
2737 min
= pos
- (sizeof(sel
) - 1);
2738 if (min
< 0) min
= 0;
2741 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2743 sci_get_text_range(sci
, min
, pos
, sel
);
2744 sel
[sizeof(sel
) - 1] = '\0';
2746 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2747 /* User typed something like "<br/>" */
2752 size
-= 2; /* skip </ */
2753 str_found
= utils_find_open_xml_tag(sel
, size
);
2755 if (lexer
== SCLEX_HTML
&& utils_is_short_html_tag(str_found
))
2759 else if (!EMPTY(str_found
))
2761 insert_closing_tag(editor
, pos
, ch
, str_found
);
2769 /* like sci_get_line_indentation(), but for a string. */
2770 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2773 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2776 g_return_val_if_fail(base_indent
, 0);
2778 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2796 /* Handles special cases where HTML is embedded in another language or
2797 * another language is embedded in HTML */
2798 static GeanyFiletype
*editor_get_filetype_at_line(GeanyEditor
*editor
, gint line
)
2800 gint style
, line_start
;
2801 GeanyFiletype
*current_ft
;
2803 g_return_val_if_fail(editor
!= NULL
, NULL
);
2804 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, NULL
);
2806 current_ft
= editor
->document
->file_type
;
2807 line_start
= sci_get_position_from_line(editor
->sci
, line
);
2808 style
= sci_get_style_at(editor
->sci
, line_start
);
2810 /* Handle PHP filetype with embedded HTML */
2811 if (current_ft
->id
== GEANY_FILETYPES_PHP
&& ! is_style_php(style
))
2812 current_ft
= filetypes
[GEANY_FILETYPES_HTML
];
2814 /* Handle languages embedded in HTML */
2815 if (current_ft
->id
== GEANY_FILETYPES_HTML
)
2818 if (style
>= SCE_HJ_DEFAULT
&& style
<= SCE_HJ_REGEX
)
2819 current_ft
= filetypes
[GEANY_FILETYPES_JS
];
2821 else if (style
>= SCE_HJA_DEFAULT
&& style
<= SCE_HJA_REGEX
)
2822 current_ft
= filetypes
[GEANY_FILETYPES_JS
];
2824 else if (style
>= SCE_HB_DEFAULT
&& style
<= SCE_HB_STRINGEOL
)
2825 current_ft
= filetypes
[GEANY_FILETYPES_BASIC
];
2827 else if (style
>= SCE_HBA_DEFAULT
&& style
<= SCE_HBA_STRINGEOL
)
2828 current_ft
= filetypes
[GEANY_FILETYPES_BASIC
];
2829 /* Embedded Python */
2830 else if (style
>= SCE_HP_DEFAULT
&& style
<= SCE_HP_IDENTIFIER
)
2831 current_ft
= filetypes
[GEANY_FILETYPES_PYTHON
];
2833 else if (style
>= SCE_HPA_DEFAULT
&& style
<= SCE_HPA_IDENTIFIER
)
2834 current_ft
= filetypes
[GEANY_FILETYPES_PYTHON
];
2836 else if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
2837 style
== SCE_HPHP_COMPLEX_VARIABLE
)
2839 current_ft
= filetypes
[GEANY_FILETYPES_PHP
];
2843 /* Ensure the filetype's config is loaded */
2844 filetypes_load_config(current_ft
->id
, FALSE
);
2850 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2853 gchar
*str_begin
, *str_end
;
2854 const gchar
*co
, *cc
;
2858 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2860 ft
= editor_get_filetype_at_line(editor
, line_start
);
2862 eol
= editor_get_eol_char(editor
);
2863 if (! filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
))
2864 g_return_if_reached();
2865 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2866 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2868 /* insert the comment strings */
2869 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2870 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2871 sci_insert_text(editor
->sci
, line_len
, str_end
);
2878 /* find @p text inside the range of the current style */
2879 static gint
find_in_current_style(ScintillaObject
*sci
, const gchar
*text
, gboolean backwards
)
2881 gint start
= sci_get_current_position(sci
);
2883 gint len
= sci_get_length(sci
);
2884 gint current_style
= sci_get_style_at(sci
, start
);
2885 struct Sci_TextToFind ttf
;
2887 while (start
> 0 && sci_get_style_at(sci
, start
- 1) == current_style
)
2889 while (end
< len
&& sci_get_style_at(sci
, end
+ 1) == current_style
)
2892 ttf
.lpstrText
= (gchar
*) text
;
2893 ttf
.chrg
.cpMin
= backwards
? end
+ 1 : start
;
2894 ttf
.chrg
.cpMax
= backwards
? start
: end
+ 1;
2895 return sci_find_text(sci
, 0, &ttf
);
2899 static void sci_delete_line(ScintillaObject
*sci
, gint line
)
2901 gint start
= sci_get_position_from_line(sci
, line
);
2902 gint len
= sci_get_line_length(sci
, line
);
2903 SSM(sci
, SCI_DELETERANGE
, start
, len
);
2907 static gboolean
real_uncomment_multiline(GeanyEditor
*editor
)
2909 /* find the beginning of the multi line comment */
2910 gint start
, end
, start_line
, end_line
;
2912 const gchar
*co
, *cc
;
2914 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, FALSE
);
2916 ft
= editor_get_filetype_at_line(editor
, sci_get_current_line(editor
->sci
));
2917 if (! filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
))
2918 g_return_val_if_reached(FALSE
);
2920 start
= find_in_current_style(editor
->sci
, co
, TRUE
);
2921 end
= find_in_current_style(editor
->sci
, cc
, FALSE
);
2923 if (start
< 0 || end
< 0 || start
> end
/* who knows */)
2926 start_line
= sci_get_line_from_position(editor
->sci
, start
);
2927 end_line
= sci_get_line_from_position(editor
->sci
, end
);
2929 /* remove comment close chars */
2930 SSM(editor
->sci
, SCI_DELETERANGE
, end
, strlen(cc
));
2931 if (sci_is_blank_line(editor
->sci
, end_line
))
2932 sci_delete_line(editor
->sci
, end_line
);
2934 /* remove comment open chars (do it last since it would move the end position) */
2935 SSM(editor
->sci
, SCI_DELETERANGE
, start
, strlen(co
));
2936 if (sci_is_blank_line(editor
->sci
, start_line
))
2937 sci_delete_line(editor
->sci
, start_line
);
2943 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2945 gint lexer
= sci_get_lexer(editor
->sci
);
2948 /* List only those lexers which support multi line comments */
2953 case SCLEX_PHPSCRIPT
:
2955 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2956 style_comment
= SCE_HPHP_COMMENT
;
2958 style_comment
= SCE_H_COMMENT
;
2962 case SCLEX_LITERATEHASKELL
:
2963 style_comment
= SCE_HA_COMMENTBLOCK
; break;
2964 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2965 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2966 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2967 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2968 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2969 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2970 case SCLEX_RUST
: style_comment
= SCE_RUST_COMMENTBLOCK
; break;
2971 default: style_comment
= SCE_C_COMMENT
;
2974 return style_comment
;
2978 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2979 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2980 * it returns just 1 */
2981 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2983 gint first_line
, last_line
;
2984 gint x
, i
, line_start
, line_len
;
2985 gint sel_start
, sel_end
;
2989 const gchar
*co
, *cc
;
2990 gboolean single_line
= FALSE
;
2993 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
2996 { /* use selection or current line */
2997 sel_start
= sci_get_selection_start(editor
->sci
);
2998 sel_end
= sci_get_selection_end(editor
->sci
);
3000 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3001 /* Find the last line with chars selected (not EOL char) */
3002 last_line
= sci_get_line_from_position(editor
->sci
,
3003 sel_end
- editor_get_eol_char_len(editor
));
3004 last_line
= MAX(first_line
, last_line
);
3008 first_line
= last_line
= line
;
3009 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3012 ft
= editor_get_filetype_at_line(editor
, first_line
);
3014 if (! filetype_get_comment_open_close(ft
, TRUE
, &co
, &cc
))
3017 co_len
= strlen(co
);
3021 sci_start_undo_action(editor
->sci
);
3023 for (i
= first_line
; i
<= last_line
; i
++)
3027 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3028 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3031 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3034 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3035 sel
[buf_len
] = '\0';
3037 while (isspace(sel
[x
])) x
++;
3039 /* to skip blank lines */
3040 if (x
< line_len
&& sel
[x
] != '\0')
3042 /* use single line comment */
3049 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
3050 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
3051 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
3058 if (strncmp(sel
+ x
, co
, co_len
) != 0)
3062 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
3063 sci_replace_sel(editor
->sci
, "");
3066 /* use multi line comment */
3071 /* skip lines which are already comments */
3072 style_comment
= get_multiline_comment_style(editor
, line_start
);
3073 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3075 if (real_uncomment_multiline(editor
))
3079 /* break because we are already on the last line */
3084 sci_end_undo_action(editor
->sci
);
3086 /* restore selection if there is one
3087 * but don't touch the selection if caller is editor_do_comment_toggle */
3088 if (! toggle
&& sel_start
< sel_end
)
3092 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
3093 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
3097 gint eol_len
= editor_get_eol_char_len(editor
);
3098 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
3099 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
3107 void editor_do_comment_toggle(GeanyEditor
*editor
)
3109 gint first_line
, last_line
;
3110 gint x
, i
, line_start
, line_len
, first_line_start
, last_line_start
;
3111 gint sel_start
, sel_end
;
3112 gint count_commented
= 0, count_uncommented
= 0;
3114 const gchar
*co
, *cc
;
3115 gboolean single_line
= FALSE
;
3116 gboolean first_line_was_comment
= FALSE
;
3117 gboolean last_line_was_comment
= FALSE
;
3119 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
3122 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3124 sel_start
= sci_get_selection_start(editor
->sci
);
3125 sel_end
= sci_get_selection_end(editor
->sci
);
3127 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3128 /* Find the last line with chars selected (not EOL char) */
3129 last_line
= sci_get_line_from_position(editor
->sci
,
3130 sel_end
- editor_get_eol_char_len(editor
));
3131 last_line
= MAX(first_line
, last_line
);
3133 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3134 last_line_start
= sci_get_position_from_line(editor
->sci
, last_line
);
3136 ft
= editor_get_filetype_at_line(editor
, first_line
);
3138 if (! filetype_get_comment_open_close(ft
, TRUE
, &co
, &cc
))
3141 co_len
= strlen(co
);
3145 sci_start_undo_action(editor
->sci
);
3147 for (i
= first_line
; i
<= last_line
; i
++)
3151 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3152 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3155 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3158 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3159 sel
[buf_len
] = '\0';
3161 while (isspace(sel
[x
])) x
++;
3163 /* use single line comment */
3166 gboolean do_continue
= FALSE
;
3169 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
3170 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
3175 if (do_continue
&& i
== first_line
)
3176 first_line_was_comment
= TRUE
;
3177 last_line_was_comment
= do_continue
;
3181 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
3185 /* we are still here, so the above lines were not already comments, so comment it */
3186 count_commented
+= editor_do_comment(editor
, i
, FALSE
, TRUE
, TRUE
);
3188 /* use multi line comment */
3193 /* skip lines which are already comments */
3194 style_comment
= get_multiline_comment_style(editor
, line_start
);
3195 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3197 if (real_uncomment_multiline(editor
))
3198 count_uncommented
++;
3202 real_comment_multiline(editor
, line_start
, last_line
);
3206 /* break because we are already on the last line */
3211 sci_end_undo_action(editor
->sci
);
3215 /* restore selection or caret position */
3218 gint a
= (first_line_was_comment
) ? - (gint
) co_len
: (gint
) co_len
;
3221 /* don't modify sel_start when the selection starts within indentation */
3222 read_indent(editor
, sel_start
);
3223 indent_len
= (gint
) strlen(indent
);
3224 if ((sel_start
- first_line_start
) <= indent_len
)
3226 /* if the selection start was inside the comment mark, adjust the position */
3227 else if (first_line_was_comment
&&
3228 sel_start
>= (first_line_start
+ indent_len
) &&
3229 sel_start
<= (first_line_start
+ indent_len
+ (gint
) co_len
))
3231 a
= (first_line_start
+ indent_len
) - sel_start
;
3234 if (sel_start
< sel_end
)
3236 gint b
= (count_commented
* (gint
) co_len
) - (count_uncommented
* (gint
) co_len
);
3238 /* same for selection end, but here we add an offset on the offset above */
3239 read_indent(editor
, sel_end
+ b
);
3240 indent_len
= (gint
) strlen(indent
);
3241 if ((sel_end
- last_line_start
) < indent_len
)
3242 b
+= last_line_was_comment
? (gint
) co_len
: -(gint
) co_len
;
3243 else if (last_line_was_comment
&&
3244 sel_end
>= (last_line_start
+ indent_len
) &&
3245 sel_end
<= (last_line_start
+ indent_len
+ (gint
) co_len
))
3247 b
+= (gint
) co_len
- (sel_end
- (last_line_start
+ indent_len
));
3250 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
3251 sci_set_selection_end(editor
->sci
, sel_end
+ b
);
3254 sci_set_current_position(editor
->sci
, sel_start
+ a
, TRUE
);
3258 gint eol_len
= editor_get_eol_char_len(editor
);
3259 if (count_uncommented
> 0)
3261 sci_set_selection_start(editor
->sci
, sel_start
- (gint
) co_len
+ eol_len
);
3262 sci_set_selection_end(editor
->sci
, sel_end
- (gint
) co_len
+ eol_len
);
3264 else if (count_commented
> 0)
3266 sci_set_selection_start(editor
->sci
, sel_start
+ (gint
) co_len
- eol_len
);
3267 sci_set_selection_end(editor
->sci
, sel_end
+ (gint
) co_len
- eol_len
);
3269 if (sel_start
>= sel_end
)
3270 sci_scroll_caret(editor
->sci
);
3275 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
3276 gint
editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
,
3277 gboolean single_comment
)
3279 gint first_line
, last_line
;
3280 gint x
, i
, line_start
, line_len
;
3281 gint sel_start
, sel_end
, co_len
;
3284 const gchar
*co
, *cc
;
3285 gboolean single_line
= FALSE
;
3288 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
3291 { /* use selection or current line */
3292 sel_start
= sci_get_selection_start(editor
->sci
);
3293 sel_end
= sci_get_selection_end(editor
->sci
);
3295 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3296 /* Find the last line with chars selected (not EOL char) */
3297 last_line
= sci_get_line_from_position(editor
->sci
,
3298 sel_end
- editor_get_eol_char_len(editor
));
3299 last_line
= MAX(first_line
, last_line
);
3303 first_line
= last_line
= line
;
3304 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3307 ft
= editor_get_filetype_at_line(editor
, first_line
);
3309 if (! filetype_get_comment_open_close(ft
, single_comment
, &co
, &cc
))
3312 co_len
= strlen(co
);
3316 sci_start_undo_action(editor
->sci
);
3318 for (i
= first_line
; i
<= last_line
; i
++)
3322 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3323 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3326 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3329 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3330 sel
[buf_len
] = '\0';
3332 while (isspace(sel
[x
])) x
++;
3334 /* to skip blank lines */
3335 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3337 /* use single line comment */
3340 gint start
= line_start
;
3343 if (ft
->comment_use_indent
)
3344 start
= line_start
+ x
;
3348 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3349 sci_insert_text(editor
->sci
, start
, text
);
3353 sci_insert_text(editor
->sci
, start
, co
);
3356 /* use multi line comment */
3361 /* skip lines which are already comments */
3362 style_comment
= get_multiline_comment_style(editor
, line_start
);
3363 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3366 real_comment_multiline(editor
, line_start
, last_line
);
3369 /* break because we are already on the last line */
3374 sci_end_undo_action(editor
->sci
);
3376 /* restore selection if there is one
3377 * but don't touch the selection if caller is editor_do_comment_toggle */
3378 if (! toggle
&& sel_start
< sel_end
)
3382 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3383 sci_set_selection_end(editor
->sci
, sel_end
+ (count
* co_len
));
3387 gint eol_len
= editor_get_eol_char_len(editor
);
3388 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3389 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3396 static gboolean brace_timeout_active
= FALSE
;
3398 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3400 GeanyDocument
*doc
= document_get_current();
3401 GeanyEditor
*editor
;
3402 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3403 gint end_pos
, cur_pos
;
3405 brace_timeout_active
= FALSE
;
3409 editor
= doc
->editor
;
3410 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3412 if (cur_pos
!= brace_pos
)
3415 if (cur_pos
!= brace_pos
)
3417 /* we have moved past the original brace_pos, but after the timeout
3418 * we may now be on a new brace, so check again */
3419 editor_highlight_braces(editor
, cur_pos
);
3423 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3425 editor_highlight_braces(editor
, cur_pos
);
3428 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3432 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3433 sci_get_col_from_position(editor
->sci
, end_pos
));
3434 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3435 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3439 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3440 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3446 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3448 gint brace_pos
= cur_pos
- 1;
3450 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3451 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3453 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3456 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3461 if (!brace_timeout_active
)
3463 brace_timeout_active
= TRUE
;
3464 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3465 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3470 static gboolean
in_block_comment(gint lexer
, gint style
)
3476 return (style
== SCE_C_COMMENT
||
3477 style
== SCE_C_COMMENTDOC
);
3480 return (style
== SCE_PAS_COMMENT
||
3481 style
== SCE_PAS_COMMENT2
);
3484 return (style
== SCE_D_COMMENT
||
3485 style
== SCE_D_COMMENTDOC
||
3486 style
== SCE_D_COMMENTNESTED
);
3489 case SCLEX_PHPSCRIPT
:
3490 return (style
== SCE_HPHP_COMMENT
);
3493 return (style
== SCE_CSS_COMMENT
);
3496 return (style
== SCE_RUST_COMMENTBLOCK
||
3497 style
== SCE_RUST_COMMENTBLOCKDOC
);
3505 static gboolean
is_comment_char(gchar c
, gint lexer
)
3507 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3517 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3519 ScintillaObject
*sci
= editor
->sci
;
3520 gint indent_pos
, style
;
3521 gint lexer
= sci_get_lexer(sci
);
3523 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3524 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3525 style
= sci_get_style_at(sci
, indent_pos
);
3526 if (!in_block_comment(lexer
, style
))
3529 /* Check whether the comment block continues on this line */
3530 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3531 if (sci_get_style_at(sci
, indent_pos
) == style
|| indent_pos
>= sci_get_length(sci
))
3533 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3534 /* the type of comment, '*' (C/C++/Java), '+' D comment that nests */
3535 const gchar
*continuation
= (style
== SCE_D_COMMENTNESTED
) ? "+" : "*";
3536 const gchar
*whitespace
= ""; /* to hold whitespace if needed */
3538 gint len
= strlen(previous_line
);
3541 /* find and stop at end of multi line comment */
3543 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3544 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3546 gint indent_len
, indent_width
;
3548 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3549 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3550 indent_width
= editor_get_indent_prefs(editor
)->width
;
3552 /* if there is one too many spaces, delete the last space,
3553 * to return to the indent used before the multiline comment was started. */
3554 if (indent_len
% indent_width
== 1)
3555 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3556 g_free(previous_line
);
3559 /* check whether we are on the second line of multi line comment */
3561 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3564 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3565 { /* we are on the second line of a multi line comment, so we have to insert white space */
3568 else if (!(g_str_has_prefix(previous_line
+ i
, continuation
) &&
3569 (i
+ 1 == len
|| isspace(previous_line
[i
+ 1]))))
3571 // previous line isn't formatted so abort
3572 g_free(previous_line
);
3575 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3576 sci_add_text(sci
, result
);
3579 g_free(previous_line
);
3585 static gboolean
editor_lexer_is_c_like(gint lexer
)
3600 /* inserts a three-line comment at one line above current cursor position */
3601 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3607 gboolean have_multiline_comment
= FALSE
;
3609 const gchar
*co
, *cc
;
3611 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3613 if (! filetype_get_comment_open_close(editor
->document
->file_type
, FALSE
, &co
, &cc
))
3614 g_return_if_reached();
3616 have_multiline_comment
= TRUE
;
3618 sci_start_undo_action(editor
->sci
);
3620 doc
= editor
->document
;
3622 /* insert three lines one line above of the current position */
3623 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3624 pos
= sci_get_position_from_line(editor
->sci
, line
);
3626 /* use the indent on the current line but only when comment indentation is used
3627 * and we don't have multi line comment characters */
3628 if (editor
->auto_indent
&&
3629 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3631 read_indent(editor
, editor_info
.click_pos
);
3632 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3633 text_len
= strlen(text
);
3637 text
= g_strdup("\n\n\n");
3640 sci_insert_text(editor
->sci
, pos
, text
);
3643 /* select the inserted lines for commenting */
3644 sci_set_selection_start(editor
->sci
, pos
);
3645 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3647 editor_do_comment(editor
, -1, TRUE
, FALSE
, FALSE
);
3649 /* set the current position to the start of the first inserted line */
3652 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3653 if (have_multiline_comment
)
3656 pos
+= strlen(indent
);
3658 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3659 /* reset the selection */
3660 sci_set_anchor(editor
->sci
, pos
);
3662 sci_end_undo_action(editor
->sci
);
3666 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3667 * Scroll the view to make line appear at percent_of_view.
3668 * line can be -1 to use the current position. */
3669 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3674 g_return_if_fail(editor
!= NULL
);
3676 wid
= GTK_WIDGET(editor
->sci
);
3678 if (! gtk_widget_get_window(wid
) || ! gdk_window_is_viewable(gtk_widget_get_window(wid
)))
3679 return; /* prevent gdk_window_scroll warning */
3682 line
= sci_get_current_line(editor
->sci
);
3684 /* sci 'visible line' != doc line number because of folding and line wrapping */
3685 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3686 * SCI_DOCLINEFROMVISIBLE for vis1. */
3687 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3688 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3689 line
= line
- los
* percent_of_view
;
3690 SSM(editor
->sci
, SCI_SETFIRSTVISIBLELINE
, line
, 0);
3691 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3695 /* creates and inserts one tab or whitespace of the amount of the tab width */
3696 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3699 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3701 g_return_if_fail(editor
!= NULL
);
3703 switch (iprefs
.type
)
3705 case GEANY_INDENT_TYPE_TABS
:
3706 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3708 case GEANY_INDENT_TYPE_SPACES
:
3709 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3710 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3713 text
= get_whitespace(&iprefs
, iprefs
.width
);
3714 sci_add_text(editor
->sci
, text
);
3719 void editor_select_word(GeanyEditor
*editor
)
3725 g_return_if_fail(editor
!= NULL
);
3727 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3728 start
= sci_word_start_position(editor
->sci
, pos
, TRUE
);
3729 end
= sci_word_end_position(editor
->sci
, pos
, TRUE
);
3731 if (start
== end
) /* caret in whitespaces sequence */
3733 /* look forward but reverse the selection direction,
3734 * so the caret end up stay as near as the original position. */
3735 end
= sci_word_end_position(editor
->sci
, pos
, FALSE
);
3736 start
= sci_word_end_position(editor
->sci
, end
, TRUE
);
3741 sci_set_selection(editor
->sci
, start
, end
);
3745 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3746 * when those lines have no selection (cursor at start of line). */
3747 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3749 gint start
, end
, line
;
3751 g_return_if_fail(editor
!= NULL
);
3753 start
= sci_get_selection_start(editor
->sci
);
3754 end
= sci_get_selection_end(editor
->sci
);
3756 /* check if whole lines are already selected */
3757 if (! extra_line
&& start
!= end
&&
3758 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3759 sci_get_col_from_position(editor
->sci
, end
) == 0)
3762 line
= sci_get_line_from_position(editor
->sci
, start
);
3763 start
= sci_get_position_from_line(editor
->sci
, line
);
3765 line
= sci_get_line_from_position(editor
->sci
, end
);
3766 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3768 sci_set_selection(editor
->sci
, start
, end
);
3772 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
)
3774 return sci_get_line_indent_position(sci
, line
) ==
3775 sci_get_line_end_position(sci
, line
);
3779 /* Returns first line of paragraph for GTK_DIR_UP, line after paragraph
3780 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3781 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3784 ScintillaObject
*sci
= editor
->sci
;
3786 /* first check current line and return -1 if it is empty to skip creating of a selection */
3787 if (sci_is_blank_line(sci
, line
))
3790 if (direction
== GTK_DIR_UP
)
3800 /* start of document */
3804 if (line
== sci_get_line_count(sci
))
3807 if (sci_is_blank_line(sci
, line
))
3809 /* return line paragraph starts on */
3810 if (direction
== GTK_DIR_UP
)
3819 void editor_select_paragraph(GeanyEditor
*editor
)
3821 gint pos_start
, pos_end
, line_start
, line_found
;
3823 g_return_if_fail(editor
!= NULL
);
3825 line_start
= sci_get_current_line(editor
->sci
);
3827 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3828 if (line_found
== -1)
3831 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3833 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3834 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3836 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3840 /* Returns first line of block for GTK_DIR_UP, line after block
3841 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3842 static gint
find_block_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3845 ScintillaObject
*sci
= editor
->sci
;
3847 /* first check current line and return -1 if it is empty to skip creating of a selection */
3848 if (sci_is_blank_line(sci
, line
))
3851 if (direction
== GTK_DIR_UP
)
3856 ind
= sci_get_line_indentation(sci
, line
);
3862 /* start of document */
3866 if (line
== sci_get_line_count(sci
))
3869 if (sci_get_line_indentation(sci
, line
) != ind
||
3870 sci_is_blank_line(sci
, line
))
3872 /* return line block starts on */
3873 if (direction
== GTK_DIR_UP
)
3882 void editor_select_indent_block(GeanyEditor
*editor
)
3884 gint pos_start
, pos_end
, line_start
, line_found
;
3886 g_return_if_fail(editor
!= NULL
);
3888 line_start
= sci_get_current_line(editor
->sci
);
3890 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_UP
);
3891 if (line_found
== -1)
3894 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3896 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_DOWN
);
3897 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3899 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3903 /* simple indentation to indent the current line with the same indent as the previous one */
3904 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
3906 gint i
, sel_start
= 0, sel_end
= 0;
3908 /* get previous line and use it for read_indent to use that line
3909 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
3910 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
3912 for (i
= first_line
; i
<= last_line
; i
++)
3914 /* skip the first line or if the indentation of the previous and current line are equal */
3916 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
3917 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
3920 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3921 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3922 if (sel_start
< sel_end
)
3924 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
3925 sci_replace_sel(editor
->sci
, "");
3927 sci_insert_text(editor
->sci
, sel_start
, indent
);
3932 /* simple indentation to indent the current line with the same indent as the previous one */
3933 void editor_smart_line_indentation(GeanyEditor
*editor
)
3935 gint first_line
, last_line
;
3936 gint first_sel_start
, first_sel_end
;
3937 ScintillaObject
*sci
;
3939 g_return_if_fail(editor
!= NULL
);
3943 first_sel_start
= sci_get_selection_start(sci
);
3944 first_sel_end
= sci_get_selection_end(sci
);
3946 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
3947 /* Find the last line with chars selected (not EOL char) */
3948 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
3949 last_line
= MAX(first_line
, last_line
);
3951 sci_start_undo_action(sci
);
3953 smart_line_indentation(editor
, first_line
, last_line
);
3955 /* set cursor position if there was no selection */
3956 if (first_sel_start
== first_sel_end
)
3958 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
3960 /* use indent position as user may wish to change indentation afterwards */
3961 sci_set_current_position(sci
, indent_pos
, FALSE
);
3965 /* fully select all the lines affected */
3966 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
3967 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
3970 sci_end_undo_action(sci
);
3974 /* increase / decrease current line or selection by one space */
3975 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
3977 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
3978 gint sel_start
, sel_end
, first_line_offset
= 0;
3980 g_return_if_fail(editor
!= NULL
);
3982 sel_start
= sci_get_selection_start(editor
->sci
);
3983 sel_end
= sci_get_selection_end(editor
->sci
);
3985 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3986 /* Find the last line with chars selected (not EOL char) */
3987 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
3988 last_line
= MAX(first_line
, last_line
);
3993 sci_start_undo_action(editor
->sci
);
3995 for (i
= first_line
; i
<= last_line
; i
++)
3997 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
4000 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
4001 /* searching backwards for a space to remove */
4002 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
4005 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
4007 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
4008 sci_replace_sel(editor
->sci
, "");
4010 if (i
== first_line
)
4011 first_line_offset
= -1;
4016 sci_insert_text(editor
->sci
, indentation_end
, " ");
4018 if (i
== first_line
)
4019 first_line_offset
= 1;
4023 /* set cursor position */
4024 if (sel_start
< sel_end
)
4026 gint start
= sel_start
+ first_line_offset
;
4027 if (first_line_offset
< 0)
4028 start
= MAX(sel_start
+ first_line_offset
,
4029 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
4031 sci_set_selection_start(editor
->sci
, start
);
4032 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
4035 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
4037 sci_end_undo_action(editor
->sci
);
4041 void editor_finalize(void)
4043 scintilla_release_resources();
4047 /* wordchars: NULL or a string containing characters to match a word.
4048 * Returns: the current selection or the current word.
4050 * Passing NULL as wordchars is NOT the same as passing GEANY_WORDCHARS: NULL means
4051 * using Scintillas's word boundaries. */
4052 gchar
*editor_get_default_selection(GeanyEditor
*editor
, gboolean use_current_word
,
4053 const gchar
*wordchars
)
4057 g_return_val_if_fail(editor
!= NULL
, NULL
);
4059 if (sci_get_lines_selected(editor
->sci
) == 1)
4060 s
= sci_get_selection_contents(editor
->sci
);
4061 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
4062 { /* use the word at current cursor position */
4063 gchar word
[GEANY_MAX_WORD_LENGTH
];
4065 if (wordchars
!= NULL
)
4066 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
4068 editor_find_current_word_sciwc(editor
, -1, word
, sizeof(word
));
4070 if (word
[0] != '\0')
4077 /* Note: Usually the line should be made visible (not folded) before calling this.
4078 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4079 * outside the *vertical* view.
4080 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4081 * sci_scroll_caret() when this returns TRUE. */
4082 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4086 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4088 /* If line is wrapped the result may occur on another virtual line than the first and may be
4089 * still hidden, so increase the line number to check for the next document line */
4090 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4093 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4094 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4095 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4097 return (line
>= vis1
&& line
< vis1
+ los
);
4101 /* If the current line is outside the current view window, scroll the line
4102 * so it appears at percent_of_view. */
4103 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4107 g_return_if_fail(editor
!= NULL
);
4109 line
= sci_get_current_line(editor
->sci
);
4111 /* unfold maybe folded results */
4112 sci_ensure_line_is_visible(editor
->sci
, line
);
4114 /* scroll the line if it's off screen */
4115 if (! editor_line_in_view(editor
, line
))
4116 editor
->scroll_percent
= percent_of_view
;
4118 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4123 * Deletes all currently set indicators in the @a editor window.
4124 * Error indicators (red squiggly underlines) and usual line markers are removed.
4126 * @param editor The editor to operate on.
4128 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4130 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4131 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4136 * Deletes all currently set indicators matching @a indic in the @a editor window.
4138 * @param editor The editor to operate on.
4139 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4144 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4148 g_return_if_fail(editor
!= NULL
);
4150 last_pos
= sci_get_length(editor
->sci
);
4153 sci_indicator_set(editor
->sci
, indic
);
4154 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4160 * Sets an indicator @a indic on @a line.
4161 * Whitespace at the start and the end of the line is not marked.
4163 * @param editor The editor to operate on.
4164 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4165 * @param line The line number which should be marked.
4170 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4176 g_return_if_fail(editor
!= NULL
);
4177 g_return_if_fail(line
>= 0);
4179 start
= sci_get_position_from_line(editor
->sci
, line
);
4180 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4182 /* skip blank lines */
4183 if ((start
+ 1) == end
||
4185 (sci_get_line_end_position(editor
->sci
, line
) - start
) == 0)
4191 linebuf
= sci_get_line(editor
->sci
, line
);
4193 /* don't set the indicator on whitespace */
4194 while (isspace(linebuf
[i
]))
4196 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4203 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4208 * Sets an indicator on the range specified by @a start and @a end.
4209 * No error checking or whitespace removal is performed, this should be done by the calling
4210 * function if necessary.
4212 * @param editor The editor to operate on.
4213 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4214 * @param start The starting position for the marker.
4215 * @param end The ending position for the marker.
4220 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4222 g_return_if_fail(editor
!= NULL
);
4226 sci_indicator_set(editor
->sci
, indic
);
4227 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4231 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4232 * the replacement will also start with 0x... */
4233 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4235 g_return_if_fail(editor
!= NULL
);
4237 if (sci_has_selection(editor
->sci
))
4239 gint start
= sci_get_selection_start(editor
->sci
);
4240 const gchar
*replacement
= colour
;
4242 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4243 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4245 gint end
= sci_get_selection_end(editor
->sci
);
4247 sci_set_selection_start(editor
->sci
, start
+ 2);
4248 /* we need to also re-set the selection end in case the anchor was located before
4249 * the cursor, since set_selection_start() always moves the cursor, not the anchor */
4250 sci_set_selection_end(editor
->sci
, end
);
4251 replacement
++; /* skip the leading "0x" */
4253 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4254 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4255 replacement
++; /* so skip the '#' to only replace the colour value */
4257 sci_replace_sel(editor
->sci
, replacement
);
4260 sci_add_text(editor
->sci
, colour
);
4265 * Retrieves the end of line characters mode (LF, CR/LF, CR) in the given editor.
4266 * If @a editor is @c NULL, the default end of line characters are used.
4268 * @param editor @nullable The editor to operate on, or @c NULL to query the default value.
4269 * @return The used end of line characters mode.
4274 gint
editor_get_eol_char_mode(GeanyEditor
*editor
)
4276 gint mode
= file_prefs
.default_eol_character
;
4279 mode
= sci_get_eol_mode(editor
->sci
);
4286 * Retrieves the localized name (for displaying) of the used end of line characters
4287 * (LF, CR/LF, CR) in the given editor.
4288 * If @a editor is @c NULL, the default end of line characters are used.
4290 * @param editor @nullable The editor to operate on, or @c NULL to query the default value.
4291 * @return The name of the end of line characters.
4296 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4298 gint mode
= file_prefs
.default_eol_character
;
4301 mode
= sci_get_eol_mode(editor
->sci
);
4303 return utils_get_eol_name(mode
);
4308 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4309 * If @a editor is @c NULL, the default end of line characters are used.
4310 * The returned value is 1 for CR and LF and 2 for CR/LF.
4312 * @param editor @nullable The editor to operate on, or @c NULL to query the default value.
4313 * @return The length of the end of line characters.
4318 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4320 gint mode
= file_prefs
.default_eol_character
;
4323 mode
= sci_get_eol_mode(editor
->sci
);
4327 case SC_EOL_CRLF
: return 2; break;
4328 default: return 1; break;
4334 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4335 * If @a editor is @c NULL, the default end of line characters are used.
4336 * The returned value is either "\n", "\r\n" or "\r".
4338 * @param editor @nullable The editor to operate on, or @c NULL to query the default value.
4339 * @return The end of line characters.
4344 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4346 gint mode
= file_prefs
.default_eol_character
;
4349 mode
= sci_get_eol_mode(editor
->sci
);
4351 return utils_get_eol_char(mode
);
4355 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4357 gint lines
, first
, i
;
4359 if (editor
== NULL
|| ! editor_prefs
.folding
)
4362 lines
= sci_get_line_count(editor
->sci
);
4363 first
= sci_get_first_visible_line(editor
->sci
);
4365 for (i
= 0; i
< lines
; i
++)
4367 gint level
= sci_get_fold_level(editor
->sci
, i
);
4369 if (level
& SC_FOLDLEVELHEADERFLAG
)
4371 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4372 sci_toggle_fold(editor
->sci
, i
);
4375 editor_scroll_to_line(editor
, first
, 0.0F
);
4379 void editor_unfold_all(GeanyEditor
*editor
)
4381 fold_all(editor
, FALSE
);
4385 void editor_fold_all(GeanyEditor
*editor
)
4387 fold_all(editor
, TRUE
);
4391 void editor_replace_tabs(GeanyEditor
*editor
, gboolean ignore_selection
)
4393 gint anchor_pos
, caret_pos
;
4394 struct Sci_TextToFind ttf
;
4396 g_return_if_fail(editor
!= NULL
);
4398 sci_start_undo_action(editor
->sci
);
4399 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4401 ttf
.chrg
.cpMin
= sci_get_selection_start(editor
->sci
);
4402 ttf
.chrg
.cpMax
= sci_get_selection_end(editor
->sci
);
4407 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4409 ttf
.lpstrText
= (gchar
*) "\t";
4411 anchor_pos
= SSM(editor
->sci
, SCI_GETANCHOR
, 0, 0);
4412 caret_pos
= sci_get_current_position(editor
->sci
);
4415 gint search_pos
, pos_in_line
, current_tab_true_length
;
4419 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4420 if (search_pos
== -1)
4423 tab_len
= sci_get_tab_width(editor
->sci
);
4424 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4425 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4426 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4427 sci_set_target_start(editor
->sci
, search_pos
);
4428 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4429 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4430 /* next search starts after replacement */
4431 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4432 /* update end of range now text has changed */
4433 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4436 if (anchor_pos
> search_pos
)
4437 anchor_pos
+= current_tab_true_length
- 1;
4438 if (caret_pos
> search_pos
)
4439 caret_pos
+= current_tab_true_length
- 1;
4441 sci_set_selection(editor
->sci
, anchor_pos
, caret_pos
);
4442 sci_end_undo_action(editor
->sci
);
4446 /* Replaces all occurrences all spaces of the length of a given tab_width,
4447 * optionally restricting the search to the current selection. */
4448 void editor_replace_spaces(GeanyEditor
*editor
, gboolean ignore_selection
)
4451 gint anchor_pos
, caret_pos
;
4452 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4455 struct Sci_TextToFind ttf
;
4457 g_return_if_fail(editor
!= NULL
);
4459 if (tab_len_f
< 0.0)
4460 tab_len_f
= sci_get_tab_width(editor
->sci
);
4462 if (! dialogs_show_input_numeric(
4463 _("Enter Tab Width"),
4464 _("Enter the amount of spaces which should be replaced by a tab character."),
4465 &tab_len_f
, 1, 100, 1))
4469 tab_len
= (gint
) tab_len_f
;
4470 text
= g_strnfill(tab_len
, ' ');
4472 sci_start_undo_action(editor
->sci
);
4473 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4475 ttf
.chrg
.cpMin
= sci_get_selection_start(editor
->sci
);
4476 ttf
.chrg
.cpMax
= sci_get_selection_end(editor
->sci
);
4481 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4483 ttf
.lpstrText
= text
;
4485 anchor_pos
= SSM(editor
->sci
, SCI_GETANCHOR
, 0, 0);
4486 caret_pos
= sci_get_current_position(editor
->sci
);
4489 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4490 if (search_pos
== -1)
4492 /* only replace indentation because otherwise we can mess up alignment */
4493 if (search_pos
> sci_get_line_indent_position(editor
->sci
,
4494 sci_get_line_from_position(editor
->sci
, search_pos
)))
4496 ttf
.chrg
.cpMin
= search_pos
+ tab_len
;
4499 sci_set_target_start(editor
->sci
, search_pos
);
4500 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4501 sci_replace_target(editor
->sci
, "\t", FALSE
);
4502 ttf
.chrg
.cpMin
= search_pos
;
4503 /* update end of range now text has changed */
4504 ttf
.chrg
.cpMax
-= tab_len
- 1;
4506 if (anchor_pos
> search_pos
)
4507 anchor_pos
-= tab_len
- 1;
4508 if (caret_pos
> search_pos
)
4509 caret_pos
-= tab_len
- 1;
4511 sci_set_selection(editor
->sci
, anchor_pos
, caret_pos
);
4512 sci_end_undo_action(editor
->sci
);
4517 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4519 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4520 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4521 gint i
= line_end
- 1;
4522 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4524 /* Diff hunks should keep trailing spaces */
4525 if (editor
->document
->file_type
->id
== GEANY_FILETYPES_DIFF
)
4528 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4531 ch
= sci_get_char_at(editor
->sci
, i
);
4533 if (i
< (line_end
- 1))
4535 sci_set_target_start(editor
->sci
, i
+ 1);
4536 sci_set_target_end(editor
->sci
, line_end
);
4537 sci_replace_target(editor
->sci
, "", FALSE
);
4542 void editor_strip_trailing_spaces(GeanyEditor
*editor
, gboolean ignore_selection
)
4548 if (sci_has_selection(editor
->sci
) && !ignore_selection
)
4550 gint selection_start
= sci_get_selection_start(editor
->sci
);
4551 gint selection_end
= sci_get_selection_end(editor
->sci
);
4553 start_line
= sci_get_line_from_position(editor
->sci
, selection_start
);
4554 end_line
= sci_get_line_from_position(editor
->sci
, selection_end
);
4556 if (sci_get_col_from_position(editor
->sci
, selection_end
) > 0)
4562 end_line
= sci_get_line_count(editor
->sci
);
4565 sci_start_undo_action(editor
->sci
);
4567 for (line
= start_line
; line
< end_line
; line
++)
4569 editor_strip_line_trailing_spaces(editor
, line
);
4571 sci_end_undo_action(editor
->sci
);
4575 void editor_ensure_final_newline(GeanyEditor
*editor
)
4577 gint max_lines
= sci_get_line_count(editor
->sci
);
4578 gboolean append_newline
= (max_lines
== 1);
4579 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4583 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4587 const gchar
*eol
= editor_get_eol_char(editor
);
4589 sci_insert_text(editor
->sci
, end_document
, eol
);
4594 /* Similar to editor_set_font() but *only* sets the font, and doesn't take care
4595 * of updating properties that might depend on the font */
4596 static void set_font(ScintillaObject
*sci
, const gchar
*font
)
4600 PangoFontDescription
*pfd
;
4603 g_return_if_fail(sci
);
4605 pfd
= pango_font_description_from_string(font
);
4606 size
= pango_font_description_get_size(pfd
) / (gdouble
) PANGO_SCALE
;
4607 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4608 pango_font_description_free(pfd
);
4610 for (style
= 0; style
<= STYLE_MAX
; style
++)
4611 sci_set_font_fractional(sci
, style
, font_name
, size
);
4617 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4619 g_return_if_fail(editor
);
4621 set_font(editor
->sci
, font
);
4622 update_margins(editor
->sci
);
4623 /* zoom to 100% to prevent confusion */
4624 sci_zoom_off(editor
->sci
);
4628 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4630 g_return_if_fail(editor
!= NULL
);
4632 editor
->line_wrapping
= wrap
;
4633 sci_set_lines_wrapped(editor
->sci
, wrap
);
4637 /** Sets the indent type for @a editor.
4638 * @param editor Editor.
4639 * @param type Indent type.
4644 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4646 editor_set_indent(editor
, type
, editor
->indent_width
);
4650 /** Sets the indent width for @a editor.
4651 * @param editor Editor.
4652 * @param width New indent width.
4654 * @since 1.27 (API 227)
4657 void editor_set_indent_width(GeanyEditor
*editor
, gint width
)
4659 editor_set_indent(editor
, editor
->indent_type
, width
);
4663 void editor_set_indent(GeanyEditor
*editor
, GeanyIndentType type
, gint width
)
4665 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4666 ScintillaObject
*sci
= editor
->sci
;
4667 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4669 editor
->indent_type
= type
;
4670 editor
->indent_width
= width
;
4671 sci_set_use_tabs(sci
, use_tabs
);
4673 if (type
== GEANY_INDENT_TYPE_BOTH
)
4675 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4676 if (iprefs
->hard_tab_width
!= 8)
4678 static gboolean warn
= TRUE
;
4680 ui_set_statusbar(TRUE
, _("Warning: non-standard hard tab width: %d != 8!"),
4681 iprefs
->hard_tab_width
);
4686 sci_set_tab_width(sci
, width
);
4688 SSM(sci
, SCI_SETINDENT
, width
, 0);
4690 /* remove indent spaces on backspace, if using any spaces to indent */
4691 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, editor_prefs
.backspace_unindent
&& (type
!= GEANY_INDENT_TYPE_TABS
), 0);
4695 /* Convenience function for editor_goto_pos() to pass a line number.
4696 * line_no is 1 based */
4697 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line_no
, gboolean offset
)
4699 g_return_val_if_fail(editor
, FALSE
);
4700 gint line_count
= sci_get_line_count(editor
->sci
);
4703 line_no
+= sci_get_current_line(editor
->sci
) + 1;
4705 /* ensure line_no is in bounds and determine whether to set line marker */
4706 gboolean set_marker
= line_no
> 0 && line_no
< line_count
;
4707 line_no
= line_no
<= 0 ? 0
4708 : line_no
>= line_count
? line_count
- 1
4711 gint pos
= sci_get_position_from_line(editor
->sci
, line_no
);
4712 return editor_goto_pos(editor
, pos
, set_marker
);
4716 /** Moves to position @a pos, switching to the document if necessary,
4717 * setting a marker if @a mark is set.
4719 * @param editor Editor.
4720 * @param pos The position.
4721 * @param mark Whether to set a mark on the position.
4722 * @return @c TRUE if action has been performed, otherwise @c FALSE.
4727 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4729 g_return_val_if_fail(editor
, FALSE
);
4730 if (G_UNLIKELY(pos
< 0))
4735 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4737 /* mark the tag with the yellow arrow */
4738 sci_marker_delete_all(editor
->sci
, 0);
4739 sci_set_marker_at_line(editor
->sci
, line
, 0);
4742 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4743 editor
->scroll_percent
= 0.25F
;
4745 /* switch to the page, via idle callback in case of batch-opening */
4746 if (main_status
.opening_session_files
)
4747 document_show_tab_idle(editor
->document
);
4749 document_show_tab(editor
->document
);
4756 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4758 GeanyEditor
*editor
= user_data
;
4760 /* we only handle up and down, leave the rest to Scintilla */
4761 if (event
->direction
!= GDK_SCROLL_UP
&& event
->direction
!= GDK_SCROLL_DOWN
)
4764 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4765 * few lines only, maybe this could/should be done in Scintilla directly */
4766 if (event
->state
& GDK_MOD1_MASK
)
4768 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4771 else if (event
->state
& GDK_SHIFT_MASK
)
4773 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4775 sci_scroll_columns(editor
->sci
, amount
);
4779 return FALSE
; /* let Scintilla handle all other cases */
4783 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4785 GeanyDocument
*doc
= editor
->document
;
4787 if (!doc
->priv
->colourise_needed
)
4790 doc
->priv
->colourise_needed
= FALSE
;
4791 sci_colourise(editor
->sci
, 0, -1);
4793 /* now that the current document is colourised, fold points are now accurate,
4794 * so force an update of the current function/tag. */
4795 symbols_get_current_function(NULL
, NULL
);
4796 ui_update_statusbar(NULL
, -1);
4802 /* We only want to colourise just before drawing, to save startup time and
4803 * prevent unnecessary recolouring other documents after one is saved.
4804 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4805 * and "show" doesn't work). */
4806 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4808 GeanyEditor
*editor
= user_data
;
4810 editor_check_colourise(editor
);
4815 static gboolean
on_editor_draw(GtkWidget
*widget
, cairo_t
*cr
, gpointer user_data
)
4817 GeanyEditor
*editor
= user_data
;
4819 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4820 * for some reason, maybe it's not necessary but just in case. */
4821 editor_check_colourise(editor
);
4826 static void setup_sci_keys(ScintillaObject
*sci
)
4828 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4829 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4830 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4831 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4832 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4833 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4834 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4835 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4836 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to beginning delete */
4837 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4838 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4839 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4840 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4841 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4842 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4843 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4845 if (editor_prefs
.use_gtk_word_boundaries
)
4847 /* use GtkEntry-like word boundaries */
4848 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4849 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4850 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4852 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4853 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4854 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4855 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4856 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4857 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4859 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4863 /* registers a Scintilla image from a named icon from the theme */
4864 static gboolean
register_named_icon(ScintillaObject
*sci
, guint id
, const gchar
*name
)
4866 GError
*error
= NULL
;
4868 gint n_channels
, rowstride
, width
, height
;
4871 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU
, &size
, NULL
);
4872 pixbuf
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), name
, size
, 0, &error
);
4875 g_warning("failed to load icon '%s': %s", name
, error
->message
);
4876 g_error_free(error
);
4880 n_channels
= gdk_pixbuf_get_n_channels(pixbuf
);
4881 rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
4882 width
= gdk_pixbuf_get_width(pixbuf
);
4883 height
= gdk_pixbuf_get_height(pixbuf
);
4885 if (gdk_pixbuf_get_bits_per_sample(pixbuf
) != 8 ||
4886 ! gdk_pixbuf_get_has_alpha(pixbuf
) ||
4888 rowstride
!= width
* n_channels
)
4890 g_warning("incompatible image data for icon '%s'", name
);
4891 g_object_unref(pixbuf
);
4895 SSM(sci
, SCI_RGBAIMAGESETWIDTH
, width
, 0);
4896 SSM(sci
, SCI_RGBAIMAGESETHEIGHT
, height
, 0);
4897 SSM(sci
, SCI_REGISTERRGBAIMAGE
, id
, (sptr_t
)gdk_pixbuf_get_pixels(pixbuf
));
4899 g_object_unref(pixbuf
);
4904 /* Create new editor widget (scintilla).
4905 * @note The @c "sci-notify" signal is connected separately. */
4906 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4908 ScintillaObject
*sci
;
4909 int rectangular_selection_modifier
;
4912 sci
= SCINTILLA(scintilla_new());
4914 /* Scintilla doesn't support RTL languages properly and is primarily
4915 * intended to be used with LTR source code, so override the
4916 * GTK+ default text direction for the Scintilla widget. */
4917 gtk_widget_set_direction(GTK_WIDGET(sci
), GTK_TEXT_DIR_LTR
);
4919 gtk_widget_show(GTK_WIDGET(sci
));
4921 sci_set_codepage(sci
, SC_CP_UTF8
);
4922 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4923 /* disable scintilla provided popup menu */
4924 sci_use_popup(sci
, FALSE
);
4926 setup_sci_keys(sci
);
4928 sci_set_lines_wrapped(sci
, editor
->line_wrapping
);
4929 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4930 /* Y policy is set in editor_apply_update_prefs() */
4931 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4932 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4934 /* tag autocompletion images */
4935 for (i
= 0; i
< TM_N_ICONS
; i
++)
4937 const gchar
*icon_name
= symbols_get_icon_name(i
);
4938 register_named_icon(sci
, i
+ 1, icon_name
);
4941 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4942 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4944 /* rectangular selection modifier for creating rectangular selections with the mouse.
4945 * We use the historical Scintilla values by default. */
4947 rectangular_selection_modifier
= SCMOD_ALT
;
4949 rectangular_selection_modifier
= SCMOD_CTRL
;
4951 SSM(sci
, SCI_SETRECTANGULARSELECTIONMODIFIER
, rectangular_selection_modifier
, 0);
4954 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4956 /* input method editor's candidate window behaviour */
4957 SSM(sci
, SCI_SETIMEINTERACTION
, editor_prefs
.ime_interaction
, 0);
4959 /* only connect signals if this is for the document notebook, not split window */
4960 if (editor
->sci
== NULL
)
4962 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4963 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4964 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4965 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4966 g_signal_connect(sci
, "draw", G_CALLBACK(on_editor_draw
), editor
);
4972 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4973 * @param editor Editor settings.
4974 * @return @transfer{floating} The new widget.
4979 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4981 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4982 ScintillaObject
*old
, *sci
;
4983 GeanyIndentType old_indent_type
= editor
->indent_type
;
4984 gint old_indent_width
= editor
->indent_width
;
4986 /* temporarily change editor to use the new sci widget */
4988 sci
= create_new_sci(editor
);
4991 editor_set_indent(editor
, iprefs
->type
, iprefs
->width
);
4992 set_font(editor
->sci
, interface_prefs
.editor_font
);
4993 editor_apply_update_prefs(editor
);
4995 /* if editor already had a widget, restore it */
4998 editor
->indent_type
= old_indent_type
;
4999 editor
->indent_width
= old_indent_width
;
5006 GeanyEditor
*editor_create(GeanyDocument
*doc
)
5008 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
5009 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
5011 editor
->document
= doc
;
5012 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
5014 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
5015 editor
->line_wrapping
= get_project_pref(line_wrapping
);
5016 editor
->scroll_percent
= -1.0F
;
5017 editor
->line_breaking
= FALSE
;
5019 editor
->sci
= editor_create_widget(editor
);
5024 /* in case we need to free some fields in future */
5025 void editor_destroy(GeanyEditor
*editor
)
5031 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
5033 gchar
*f
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
5035 if (utils_str_equal(doc
->real_path
, f
))
5037 /* reload snippets */
5038 editor_snippets_free();
5039 editor_snippets_init();
5045 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
5049 g_return_val_if_fail(editor
, FALSE
);
5051 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
5054 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
5056 /* if no word part, complete normally */
5057 if (!check_partial_completion(editor
, entry
))
5058 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
5065 void editor_init(void)
5067 static GeanyIndentPrefs indent_prefs
;
5070 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
5071 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
5072 editor_prefs
.indentation
= &indent_prefs
;
5074 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
5075 * handler (on_editor_notify) is called */
5076 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
5078 f
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
5079 ui_add_config_file_menu_item(f
, NULL
, NULL
);
5081 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
5085 /* TODO: Should these be user-defined instead of hard-coded? */
5086 void editor_set_indentation_guides(GeanyEditor
*editor
)
5091 g_return_if_fail(editor
!= NULL
);
5093 if (! editor_prefs
.show_indent_guide
)
5095 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
5099 lexer
= sci_get_lexer(editor
->sci
);
5102 /* Lines added/removed are prefixed with +/- characters, so
5103 * those lines will not be shown with any indentation guides.
5104 * It can be distracting that only a few of lines in a diff/patch
5105 * file will show the guides. */
5110 /* These languages use indentation for control blocks; the "look forward" method works
5114 case SCLEX_MAKEFILE
:
5118 case SCLEX_PROPERTIES
:
5119 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
5121 mode
= SC_IV_LOOKFORWARD
;
5124 /* C-like (structured) languages benefit from the "look both" method */
5127 case SCLEX_PHPSCRIPT
:
5139 case SCLEX_FREEBASIC
:
5143 mode
= SC_IV_LOOKBOTH
;
5151 sci_set_indentation_guides(editor
->sci
, mode
);
5155 /* Apply non-document prefs that can change in the Preferences dialog */
5156 void editor_apply_update_prefs(GeanyEditor
*editor
)
5158 ScintillaObject
*sci
;
5161 g_return_if_fail(editor
!= NULL
);
5163 if (main_status
.quitting
)
5168 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
5169 editor_get_long_line_column(), editor_prefs
.long_line_color
);
5171 /* update indent width, tab width */
5172 editor_set_indent(editor
, editor
->indent_type
, editor
->indent_width
);
5173 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
5175 sci_assign_cmdkey(sci
, SCK_HOME
| (SCMOD_SHIFT
<< 16),
5176 editor_prefs
.smart_home_key
? SCI_VCHOMEEXTEND
: SCI_HOMEEXTEND
);
5177 sci_assign_cmdkey(sci
, SCK_HOME
| ((SCMOD_SHIFT
| SCMOD_ALT
) << 16),
5178 editor_prefs
.smart_home_key
? SCI_VCHOMERECTEXTEND
: SCI_HOMERECTEXTEND
);
5180 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
5181 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
5183 editor_set_indentation_guides(editor
);
5185 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
5186 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
5187 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
5188 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
);
5189 sci_set_eol_representation_characters(sci
, sci_get_eol_mode(sci
));
5191 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
5194 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
5196 /* Change history */
5197 guint change_history_mask
;
5198 change_history_mask
= SC_CHANGE_HISTORY_DISABLED
;
5199 if (editor_prefs
.change_history_markers
)
5200 change_history_mask
|= SC_CHANGE_HISTORY_ENABLED
|SC_CHANGE_HISTORY_MARKERS
;
5201 if (editor_prefs
.change_history_indicators
)
5202 change_history_mask
|= SC_CHANGE_HISTORY_ENABLED
|SC_CHANGE_HISTORY_INDICATORS
;
5203 SSM(sci
, SCI_SETCHANGEHISTORY
, change_history_mask
, 0);
5205 /* caret Y policy */
5206 caret_y_policy
= CARET_EVEN
;
5207 if (editor_prefs
.scroll_lines_around_cursor
> 0)
5208 caret_y_policy
|= CARET_SLOP
| CARET_STRICT
;
5209 sci_set_caret_policy_y(sci
, caret_y_policy
, editor_prefs
.scroll_lines_around_cursor
);
5211 /* (dis)allow scrolling past end of document */
5212 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
5214 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
5218 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
5219 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
5221 ScintillaObject
*sci
= editor
->sci
;
5222 gint pos
= sci_get_position_from_line(sci
, line
);
5226 sci_insert_text(sci
, pos
, "\t");
5230 if (sci_get_char_at(sci
, pos
) == '\t')
5232 sci_set_selection(sci
, pos
, pos
+ 1);
5233 sci_replace_sel(sci
, "");
5235 else /* remove spaces only if no tabs */
5237 gint width
= sci_get_line_indentation(sci
, line
);
5239 width
-= editor_get_indent_prefs(editor
)->width
;
5240 sci_set_line_indentation(sci
, line
, width
);
5246 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
5248 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
5249 ScintillaObject
*sci
= editor
->sci
;
5251 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
5252 change_tab_indentation(editor
, line
, increase
);
5255 gint width
= sci_get_line_indentation(sci
, line
);
5257 width
+= increase
? iprefs
->width
: -iprefs
->width
;
5258 sci_set_line_indentation(sci
, line
, width
);
5263 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
5265 ScintillaObject
*sci
= editor
->sci
;
5266 gint caret_pos
, caret_line
, caret_offset
, caret_indent_pos
, caret_line_len
;
5267 gint anchor_pos
, anchor_line
, anchor_offset
, anchor_indent_pos
, anchor_line_len
;
5269 /* backup information needed to restore caret and anchor */
5270 caret_pos
= sci_get_current_position(sci
);
5271 anchor_pos
= SSM(sci
, SCI_GETANCHOR
, 0, 0);
5272 caret_line
= sci_get_line_from_position(sci
, caret_pos
);
5273 anchor_line
= sci_get_line_from_position(sci
, anchor_pos
);
5274 caret_offset
= caret_pos
- sci_get_position_from_line(sci
, caret_line
);
5275 anchor_offset
= anchor_pos
- sci_get_position_from_line(sci
, anchor_line
);
5276 caret_indent_pos
= sci_get_line_indent_position(sci
, caret_line
);
5277 anchor_indent_pos
= sci_get_line_indent_position(sci
, anchor_line
);
5278 caret_line_len
= sci_get_line_length(sci
, caret_line
);
5279 anchor_line_len
= sci_get_line_length(sci
, anchor_line
);
5281 if (sci_get_lines_selected(sci
) <= 1)
5283 editor_change_line_indent(editor
, sci_get_current_line(sci
), increase
);
5288 gint line
, lstart
, lend
;
5290 editor_select_lines(editor
, FALSE
);
5291 start
= sci_get_selection_start(sci
);
5292 end
= sci_get_selection_end(sci
);
5293 lstart
= sci_get_line_from_position(sci
, start
);
5294 lend
= sci_get_line_from_position(sci
, end
);
5295 if (end
== sci_get_length(sci
))
5296 lend
++; /* for last line with text on it */
5298 sci_start_undo_action(sci
);
5299 for (line
= lstart
; line
< lend
; line
++)
5301 editor_change_line_indent(editor
, line
, increase
);
5303 sci_end_undo_action(sci
);
5306 /* restore caret and anchor position */
5307 if (caret_pos
>= caret_indent_pos
)
5308 caret_offset
+= sci_get_line_length(sci
, caret_line
) - caret_line_len
;
5309 if (anchor_pos
>= anchor_indent_pos
)
5310 anchor_offset
+= sci_get_line_length(sci
, anchor_line
) - anchor_line_len
;
5312 SSM(sci
, SCI_SETCURRENTPOS
, sci_get_position_from_line(sci
, caret_line
) + caret_offset
, 0);
5313 SSM(sci
, SCI_SETANCHOR
, sci_get_position_from_line(sci
, anchor_line
) + anchor_offset
, 0);
5317 /** Gets snippet by name.
5319 * If @a editor is passed, returns a snippet specific to the document filetype.
5320 * If @a editor is @c NULL, returns a snippet from the default set.
5322 * @param editor @nullable Editor or @c NULL.
5323 * @param snippet_name Snippet name.
5324 * @return @nullable snippet or @c NULL if it was not found. Must not be freed.
5327 const gchar
*editor_find_snippet(GeanyEditor
*editor
, const gchar
*snippet_name
)
5329 const gchar
*subhash_name
= editor
? editor
->document
->file_type
->name
: "Default";
5330 GHashTable
*subhash
= g_hash_table_lookup(snippet_hash
, subhash_name
);
5332 return subhash
? g_hash_table_lookup(subhash
, snippet_name
) : NULL
;
5336 /** Replaces all special sequences in @a snippet and inserts it at @a pos.
5337 * If you insert at the current position, consider calling @c sci_scroll_caret()
5338 * after this function.
5344 void editor_insert_snippet(GeanyEditor
*editor
, gint pos
, const gchar
*snippet
)
5348 pattern
= g_string_new(snippet
);
5349 snippets_make_replacements(editor
, pattern
);
5350 editor_insert_text_block(editor
, pattern
->str
, pos
, -1, -1, TRUE
);
5351 g_string_free(pattern
, TRUE
);
5354 static void *copy_(void *src
) { return src
; }
5355 static void free_(void *doc
) { }
5358 * Gets the GType of GeanyEditor
5360 * @return the GeanyEditor type */
5362 GType
editor_get_type (void);
5364 G_DEFINE_BOXED_TYPE(GeanyEditor
, editor
, copy_
, free_
);