2 * editor.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2009-2012 Frank Lanitz <frank(at)frank(dot)uvena(dot)de>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
25 * Editor-related functions for @ref GeanyEditor.
26 * Geany uses the Scintilla editing widget, and this file is mostly built around
27 * Scintilla's functionality.
30 /* Callbacks for the Scintilla widget (ScintillaObject).
31 * Most important is the sci-notify callback, handled in on_editor_notification().
32 * This includes auto-indentation, comments, auto-completion, calltips, etc.
33 * Also some general Scintilla-related functions.
40 #include <gdk/gdkkeysyms.h>
48 #include "documentprivate.h"
49 #include "filetypes.h"
50 #include "filetypesprivate.h"
51 #include "sciwrappers.h"
56 #include "callbacks.h"
57 #include "templates.h"
58 #include "keybindings.h"
60 #include "projectprivate.h"
62 #include "highlighting.h"
63 #include "gtkcompat.h"
66 /* Note: use sciwrappers.h instead where possible.
67 * Do not use SSM in files unrelated to scintilla. */
68 #define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
71 static GHashTable
*snippet_hash
= NULL
;
72 static GQueue
*snippet_offsets
= NULL
;
73 static gint snippet_cursor_insert_pos
;
74 static GtkAccelGroup
*snippet_accel_group
= NULL
;
76 static const gchar geany_cursor_marker
[] = "__GEANY_CURSOR_MARKER__";
78 /* holds word under the mouse or keyboard cursor */
79 static gchar current_word
[GEANY_MAX_WORD_LENGTH
];
81 /* Initialised in keyfile.c. */
82 GeanyEditorPrefs editor_prefs
;
84 EditorInfo editor_info
= {current_word
, -1};
94 } calltip
= {NULL
, FALSE
, NULL
, 0, 0, NULL
};
96 static gchar indent
[100];
99 static void on_new_line_added(GeanyEditor
*editor
);
100 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
101 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
102 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
103 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
104 static void close_block(GeanyEditor
*editor
, gint pos
);
105 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
106 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
107 const gchar
*wc
, gboolean stem
);
108 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
);
109 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
);
110 static void snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
);
111 static gssize
replace_cursor_markers(GeanyEditor
*editor
, GString
*pattern
);
112 static GeanyFiletype
*editor_get_filetype_at_line(GeanyEditor
*editor
, gint line
);
113 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
);
116 void editor_snippets_free(void)
118 g_hash_table_destroy(snippet_hash
);
119 g_queue_free(snippet_offsets
);
120 gtk_window_remove_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
124 static void snippets_load(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
126 gsize i
, j
, len
= 0, len_keys
= 0;
127 gchar
**groups_user
, **groups_sys
;
128 gchar
**keys_user
, **keys_sys
;
132 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
134 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
136 /* first read all globally defined auto completions */
137 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
138 for (i
= 0; i
< len
; i
++)
140 if (strcmp(groups_sys
[i
], "Keybindings") == 0)
142 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
143 /* create new hash table for the read section (=> filetype) */
144 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
145 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
147 for (j
= 0; j
< len_keys
; j
++)
149 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
150 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
152 g_strfreev(keys_sys
);
154 g_strfreev(groups_sys
);
156 /* now read defined completions in user's configuration directory and add / replace them */
157 groups_user
= g_key_file_get_groups(userconfig
, &len
);
158 for (i
= 0; i
< len
; i
++)
160 if (strcmp(groups_user
[i
], "Keybindings") == 0)
162 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
164 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
166 { /* new key found, create hash table */
167 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
168 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
170 for (j
= 0; j
< len_keys
; j
++)
172 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
174 { /* value = NULL means the key doesn't yet exist, so insert */
175 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
176 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
179 { /* old key and value will be freed by destroy function (g_free) */
180 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
181 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
184 g_strfreev(keys_user
);
186 g_strfreev(groups_user
);
190 static void on_snippet_keybinding_activate(gchar
*key
)
192 GeanyDocument
*doc
= document_get_current();
194 GHashTable
*specials
;
196 if (!doc
|| !gtk_widget_has_focus(GTK_WIDGET(doc
->editor
->sci
)))
199 s
= snippets_find_completion_by_name(doc
->file_type
->name
, key
);
200 if (!s
) /* allow user to specify keybindings for "special" snippets */
202 specials
= g_hash_table_lookup(snippet_hash
, "Special");
203 if (G_LIKELY(specials
!= NULL
))
204 s
= g_hash_table_lookup(specials
, key
);
212 editor_insert_snippet(doc
->editor
, sci_get_current_position(doc
->editor
->sci
), s
);
213 sci_scroll_caret(doc
->editor
->sci
);
217 static void add_kb(GKeyFile
*keyfile
, const gchar
*group
, gchar
**keys
)
223 for (i
= 0; i
< g_strv_length(keys
); i
++)
226 GdkModifierType mods
;
227 gchar
*accel_string
= g_key_file_get_value(keyfile
, group
, keys
[i
], NULL
);
229 gtk_accelerator_parse(accel_string
, &key
, &mods
);
230 g_free(accel_string
);
232 if (key
== 0 && mods
== 0)
234 g_warning("Can not parse accelerator \"%s\" from user snippets.conf", accel_string
);
237 gtk_accel_group_connect(snippet_accel_group
, key
, mods
, 0,
238 g_cclosure_new_swap((GCallback
)on_snippet_keybinding_activate
,
239 g_strdup(keys
[i
]), (GClosureNotify
)g_free
));
244 static void load_kb(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
246 const gchar kb_group
[] = "Keybindings";
247 gchar
**keys
= g_key_file_get_keys(userconfig
, kb_group
, NULL
, NULL
);
250 /* remove overridden keys from system keyfile */
251 foreach_strv(ptr
, keys
)
252 g_key_file_remove_key(sysconfig
, kb_group
, *ptr
, NULL
);
254 add_kb(userconfig
, kb_group
, keys
);
257 keys
= g_key_file_get_keys(sysconfig
, kb_group
, NULL
, NULL
);
258 add_kb(sysconfig
, kb_group
, keys
);
263 void editor_snippets_init(void)
265 gchar
*sysconfigfile
, *userconfigfile
;
266 GKeyFile
*sysconfig
= g_key_file_new();
267 GKeyFile
*userconfig
= g_key_file_new();
269 snippet_offsets
= g_queue_new();
271 sysconfigfile
= g_build_filename(app
->datadir
, "snippets.conf", NULL
);
272 userconfigfile
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
274 /* check for old autocomplete.conf files (backwards compatibility) */
275 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
))
276 SETPTR(userconfigfile
, g_build_filename(app
->configdir
, "autocomplete.conf", NULL
));
278 /* load the actual config files */
279 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
280 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
282 snippets_load(sysconfig
, userconfig
);
284 /* setup snippet keybindings */
285 snippet_accel_group
= gtk_accel_group_new();
286 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
287 load_kb(sysconfig
, userconfig
);
289 g_free(sysconfigfile
);
290 g_free(userconfigfile
);
291 g_key_file_free(sysconfig
);
292 g_key_file_free(userconfig
);
296 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
299 GeanyEditor
*editor
= data
;
300 GeanyDocument
*doc
= editor
->document
;
302 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
303 * fake event to show the editor menu triggered by a key event where we want to use the
304 * text cursor position. */
305 if (event
->x
> 0.0 && event
->y
> 0.0)
306 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
307 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
309 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
311 if (event
->button
== 1)
313 guint state
= event
->state
& gtk_accelerator_get_default_mod_mask();
315 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
317 gint ss
= sci_get_selection_start(editor
->sci
);
318 sci_set_selection_end(editor
->sci
, ss
);
320 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GDK_CONTROL_MASK
)
322 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
324 editor_find_current_word(editor
, editor_info
.click_pos
,
325 current_word
, sizeof current_word
, NULL
);
327 return symbols_goto_tag(current_word
, TRUE
);
329 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
332 return document_check_disk_status(doc
, FALSE
);
335 /* calls the edit popup menu in the editor */
336 if (event
->button
== 3)
340 /* ensure the editor widget has the focus after this operation */
341 gtk_widget_grab_focus(widget
);
343 editor_find_current_word(editor
, editor_info
.click_pos
,
344 current_word
, sizeof current_word
, NULL
);
346 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
347 ui_update_popup_goto_items(can_goto
);
348 ui_update_popup_copy_items(doc
);
349 ui_update_insert_include_item(doc
, 0);
351 g_signal_emit_by_name(geany_object
, "update-editor-menu",
352 current_word
, editor_info
.click_pos
, doc
);
354 gtk_menu_popup(GTK_MENU(main_widgets
.editor_menu
),
355 NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
363 static gboolean
is_style_php(gint style
)
365 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
366 style
== SCE_HPHP_COMPLEX_VARIABLE
)
375 static gint
editor_get_long_line_type(void)
378 switch (app
->project
->long_line_behaviour
)
380 case 0: /* marker disabled */
382 case 1: /* use global settings */
384 case 2: /* custom (enabled) */
385 return editor_prefs
.long_line_type
;
388 if (!editor_prefs
.long_line_enabled
)
391 return editor_prefs
.long_line_type
;
395 static gint
editor_get_long_line_column(void)
397 if (app
->project
&& app
->project
->long_line_behaviour
!= 1 /* use global settings */)
398 return app
->project
->long_line_column
;
400 return editor_prefs
.long_line_column
;
404 static const GeanyEditorPrefs
*
405 get_default_prefs(void)
407 static GeanyEditorPrefs eprefs
;
409 eprefs
= editor_prefs
;
411 /* project overrides */
412 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(NULL
);
413 eprefs
.long_line_type
= editor_get_long_line_type();
414 eprefs
.long_line_column
= editor_get_long_line_column();
419 /* Gets the prefs for the editor.
420 * Prefs can be different according to project or document.
421 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
422 * settings may have changed, or if this function has been called for a different editor.
423 * @param editor The editor, or @c NULL to get the default prefs.
424 * @return The prefs. */
425 const GeanyEditorPrefs
*editor_get_prefs(GeanyEditor
*editor
)
427 static GeanyEditorPrefs eprefs
;
428 const GeanyEditorPrefs
*dprefs
= get_default_prefs();
430 /* Return the address of the default prefs to allow returning default and editor
431 * pref pointers without invalidating the contents of either. */
436 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(editor
);
437 /* add other editor & document overrides as needed */
442 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
444 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);
463 sci_toggle_fold(sci
, line
);
465 /* extra toggling of child fold points
466 * use when editor_prefs.unfold_all_children is set and Shift is NOT pressed or when
467 * editor_prefs.unfold_all_children is NOT set but Shift is pressed */
468 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
469 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
471 gint last_line
= SSM(sci
, SCI_GETLASTCHILD
, line
, -1);
474 if (sci_get_line_is_visible(sci
, line
+ 1))
475 { /* unfold all children of the current fold point */
476 for (i
= line
; i
< last_line
; i
++)
478 if (! sci_get_line_is_visible(sci
, i
))
480 sci_toggle_fold(sci
, sci_get_fold_parent(sci
, i
));
485 { /* fold all children of the current fold point */
486 for (i
= line
; i
< last_line
; i
++)
488 gint level
= sci_get_fold_level(sci
, i
);
489 if (level
& SC_FOLDLEVELHEADERFLAG
)
491 if (sci_get_fold_expanded(sci
, i
))
492 sci_toggle_fold(sci
, i
);
500 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
502 /* left click to marker margin marks the line */
505 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
507 /*sci_marker_delete_all(editor->sci, 1);*/
508 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
510 /* left click on the folding margin to toggle folding state of current line */
511 else if (nt
->margin
== 2 && editor_prefs
.folding
)
513 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
514 editor_toggle_fold(editor
, line
, nt
->modifiers
);
519 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
521 ScintillaObject
*sci
= editor
->sci
;
522 gint pos
= sci_get_current_position(sci
);
524 /* since Scintilla 2.24, SCN_UPDATEUI is also sent on scrolling though we don't need to handle
525 * this and so ignore every SCN_UPDATEUI events except for content and selection changes */
526 if (! (nt
->updated
& SC_UPDATE_CONTENT
) && ! (nt
->updated
& SC_UPDATE_SELECTION
))
529 /* undo / redo menu update */
530 ui_update_popup_reundo_items(editor
->document
);
532 /* brace highlighting */
533 editor_highlight_braces(editor
, pos
);
535 ui_update_statusbar(editor
->document
, pos
);
538 /** experimental code for inverting selections */
541 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
543 /* need to get colour from getstyleat(), but how? */
544 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
545 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
548 sci_get_style_at(sci
, pos
);
554 static void check_line_breaking(GeanyEditor
*editor
, gint pos
)
556 ScintillaObject
*sci
= editor
->sci
;
557 gint line
, lstart
, col
;
560 if (!editor
->line_breaking
)
563 col
= sci_get_col_from_position(sci
, pos
);
565 line
= sci_get_current_line(sci
);
567 lstart
= sci_get_position_from_line(sci
, line
);
569 /* use column instead of position which might be different with multibyte characters */
570 if (col
< editor_prefs
.line_break_column
)
573 /* look for the last space before line_break_column */
574 pos
= MIN(pos
, lstart
+ editor_prefs
.line_break_column
);
578 c
= sci_get_char_at(sci
, --pos
);
581 gint diff
, last_pos
, last_col
;
583 /* remember the distance between the current column and the last column on the line
584 * (we use column position in case the previous line gets altered, such as removing
585 * trailing spaces or in case it contains multibyte characters) */
586 last_pos
= sci_get_line_end_position(sci
, line
);
587 last_col
= sci_get_col_from_position(sci
, last_pos
);
588 diff
= last_col
- col
;
590 /* break the line after the space */
591 sci_set_current_position(sci
, pos
+ 1, FALSE
);
592 sci_cancel(sci
); /* don't select from completion list */
593 sci_send_command(sci
, SCI_NEWLINE
);
596 /* correct cursor position (might not be at line end) */
597 last_pos
= sci_get_line_end_position(sci
, line
);
598 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
599 /* last column - distance is the desired column, then retrieve its document position */
600 pos
= sci_get_position_from_col(sci
, line
, last_col
- diff
);
601 sci_set_current_position(sci
, pos
, FALSE
);
602 sci_scroll_caret(sci
);
609 static void show_autocomplete(ScintillaObject
*sci
, gsize rootlen
, GString
*words
)
611 /* hide autocompletion if only option is already typed */
612 if (rootlen
>= words
->len
||
613 (words
->str
[rootlen
] == '?' && rootlen
>= words
->len
- 2))
615 sci_send_command(sci
, SCI_AUTOCCANCEL
);
618 /* store whether a calltip is showing, so we can reshow it after autocompletion */
619 calltip
.set
= (gboolean
) SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
620 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
->str
);
624 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
626 ScintillaObject
*sci
= editor
->sci
;
628 g_return_if_fail(tags
);
632 GString
*words
= g_string_sized_new(150);
635 for (j
= 0; j
< tags
->len
; ++j
)
637 TMTag
*tag
= tags
->pdata
[j
];
640 g_string_append_c(words
, '\n');
642 if (j
== editor_prefs
.autocompletion_max_entries
)
644 g_string_append(words
, "...");
647 g_string_append(words
, tag
->name
);
649 /* for now, tag types don't all follow C, so just look at arglist */
650 if (NZV(tag
->atts
.entry
.arglist
))
651 g_string_append(words
, "?2");
653 g_string_append(words
, "?1");
655 show_autocomplete(sci
, rootlen
, words
);
656 g_string_free(words
, TRUE
);
661 /* do not use with long strings */
662 static gboolean
match_last_chars(ScintillaObject
*sci
, gint pos
, const gchar
*str
)
664 gsize len
= strlen(str
);
667 g_return_val_if_fail(len
< 100, FALSE
);
668 g_return_val_if_fail(len
<= pos
, FALSE
);
670 buf
= g_alloca(len
+ 1);
671 sci_get_text_range(sci
, pos
- len
, pos
, buf
);
672 return strcmp(str
, buf
) == 0;
676 static gboolean
reshow_calltip(gpointer data
)
680 g_return_val_if_fail(calltip
.sci
!= NULL
, FALSE
);
682 SSM(calltip
.sci
, SCI_CALLTIPCANCEL
, 0, 0);
683 doc
= document_get_current();
685 if (doc
&& doc
->editor
->sci
== calltip
.sci
)
687 /* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
688 * may be completely wrong in case the user cancelled the auto completion with the mouse */
689 SSM(calltip
.sci
, SCI_CALLTIPSHOW
, calltip
.pos
, (sptr_t
) calltip
.text
);
695 static void request_reshowing_calltip(SCNotification
*nt
)
699 /* delay the reshow of the calltip window to make sure it is actually displayed,
700 * without it might be not visible on SCN_AUTOCCANCEL. the priority is set to
701 * low to hopefully make Scintilla's events happen before reshowing since they
702 * seem to re-cancel the calltip on autoc menu hiding too */
703 g_idle_add_full(G_PRIORITY_LOW
, reshow_calltip
, NULL
, NULL
);
708 static void autocomplete_scope(GeanyEditor
*editor
)
710 ScintillaObject
*sci
= editor
->sci
;
711 gint pos
= sci_get_current_position(editor
->sci
);
712 gchar typed
= sci_get_char_at(sci
, pos
- 1);
714 const GPtrArray
*tags
= NULL
;
716 GeanyFiletype
*ft
= editor
->document
->file_type
;
718 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
720 if (match_last_chars(sci
, pos
, "->") || match_last_chars(sci
, pos
, "::"))
722 else if (typed
!= '.')
725 else if (typed
!= '.')
728 /* allow for a space between word and operator */
729 if (isspace(sci_get_char_at(sci
, pos
- 2)))
731 name
= editor_get_word_at_pos(editor
, pos
- 1, NULL
);
735 tags
= tm_workspace_find(name
, tm_tag_max_t
, NULL
, FALSE
, ft
->lang
);
737 if (!tags
|| tags
->len
== 0)
740 tag
= g_ptr_array_index(tags
, 0);
741 name
= tag
->atts
.entry
.var_type
;
744 TMWorkObject
*obj
= editor
->document
->tm_file
;
746 tags
= tm_workspace_find_scope_members(obj
? obj
->tags_array
: NULL
,
749 show_tags_list(editor
, tags
, 0);
754 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
756 ScintillaObject
*sci
= editor
->sci
;
757 gint pos
= sci_get_current_position(sci
);
762 { /* simple indentation (only for CR format) */
763 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
764 on_new_line_added(editor
);
768 { /* simple indentation (for CR/LF and LF format) */
769 on_new_line_added(editor
);
773 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
776 { /* close xml-tags */
777 handle_xml(editor
, pos
, nt
->ch
);
782 auto_close_chars(sci
, pos
, nt
->ch
);
784 editor_show_calltip(editor
, --pos
);
788 { /* hide calltips */
789 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0))
791 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
793 g_free(calltip
.text
);
805 auto_close_chars(sci
, pos
, nt
->ch
);
809 { /* closing bracket handling */
810 if (editor
->auto_indent
)
811 close_block(editor
, pos
- 1);
814 /* scope autocompletion */
816 case ':': /* C/C++ class:: syntax */
817 /* tag autocompletion */
820 if (! editor_start_auto_complete(editor
, pos
, FALSE
))
821 request_reshowing_calltip(nt
);
823 editor_start_auto_complete(editor
, pos
, FALSE
);
826 check_line_breaking(editor
, pos
);
830 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
831 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
832 gboolean force
, gint visLevels
, gint level
)
834 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
835 gint levelLine
= level
;
837 while (*line
<= lineMaxSubord
)
842 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
844 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
849 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
852 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
853 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
858 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
860 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
861 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
867 if (!sci_get_fold_expanded(sci
, *line
))
868 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
869 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
873 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
885 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
887 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
889 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
891 /* Adding a fold point */
892 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
893 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
896 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
898 if (! sci_get_fold_expanded(sci
, line
))
899 { /* Removing the fold from one that has been contracted so should expand
900 * otherwise lines are left invisible with no way to make them visible */
901 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
902 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
905 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
906 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
908 /* See if should still be hidden */
909 gint parentLine
= sci_get_fold_parent(sci
, line
);
912 SSM(sci
, SCI_SHOWLINES
, line
, line
);
914 else if (sci_get_fold_expanded(sci
, parentLine
) &&
915 sci_get_line_is_visible(sci
, parentLine
))
917 SSM(sci
, SCI_SHOWLINES
, line
, line
);
923 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
924 gboolean enforcePolicy
)
926 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
927 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
930 for (line
= lineStart
; line
<= lineEnd
; line
++)
932 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
937 static void auto_update_margin_width(GeanyEditor
*editor
)
939 gint next_linecount
= 1;
940 gint linecount
= sci_get_line_count(editor
->sci
);
941 GeanyDocument
*doc
= editor
->document
;
943 while (next_linecount
<= linecount
)
944 next_linecount
*= 10;
946 if (editor
->document
->priv
->line_count
!= next_linecount
)
948 doc
->priv
->line_count
= next_linecount
;
949 sci_set_line_numbers(editor
->sci
, TRUE
, 0);
954 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
956 gint pos
= sci_get_current_position(sci
);
958 sci_insert_text(sci
, pos
, text
);
959 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
963 /* Complete the next word part from @a entry */
964 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
966 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
968 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
970 if (strstr(text
, stem
) != text
)
971 return FALSE
; /* shouldn't happen */
972 if (strlen(text
) <= strlen(stem
))
975 text
+= strlen(stem
); /* skip stem */
976 ptr
= strstr(text
+ 1, "_");
980 partial_complete(editor
->sci
, text
);
986 foreach_str(ptr
, text
+ 1)
990 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
993 partial_complete(editor
->sci
, text
);
1002 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
1003 * Plugins can connect to the "editor-notify" signal. */
1004 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
1005 gpointer scnt
, gpointer data
)
1007 GeanyEditor
*editor
= data
;
1010 g_return_if_fail(editor
!= NULL
);
1012 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
1016 static gboolean
on_editor_notify(G_GNUC_UNUSED GObject
*object
, GeanyEditor
*editor
,
1017 SCNotification
*nt
, G_GNUC_UNUSED gpointer data
)
1019 ScintillaObject
*sci
= editor
->sci
;
1020 GeanyDocument
*doc
= editor
->document
;
1022 switch (nt
->nmhdr
.code
)
1024 case SCN_SAVEPOINTLEFT
:
1025 document_set_text_changed(doc
, TRUE
);
1028 case SCN_SAVEPOINTREACHED
:
1029 document_set_text_changed(doc
, FALSE
);
1032 case SCN_MODIFYATTEMPTRO
:
1036 case SCN_MARGINCLICK
:
1037 on_margin_click(editor
, nt
);
1041 on_update_ui(editor
, nt
);
1045 /* Visible lines are only laid out accurately just before painting,
1046 * so we need to only call editor_scroll_to_line here, because the document
1047 * may have line wrapping and folding enabled.
1048 * http://scintilla.sourceforge.net/ScintillaDoc.html#LineWrapping
1049 * This is important e.g. when loading a session and switching pages
1050 * and having the cursor scroll in view. */
1051 /* FIXME: Really we want to do this just before painting, not after it
1052 * as it will cause repainting. */
1053 if (editor
->scroll_percent
> 0.0F
)
1055 editor_scroll_to_line(editor
, -1, editor
->scroll_percent
);
1056 /* disable further scrolling */
1057 editor
->scroll_percent
= -1.0F
;
1062 if (editor_prefs
.show_linenumber_margin
&& (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) && nt
->linesAdded
)
1064 /* automatically adjust Scintilla's line numbers margin width */
1065 auto_update_margin_width(editor
);
1067 if (nt
->modificationType
& SC_STARTACTION
&& ! ignore_callback
)
1069 /* get notified about undo changes */
1070 document_undo_add(doc
, UNDO_SCINTILLA
, NULL
);
1072 if (editor_prefs
.folding
&& (nt
->modificationType
& SC_MOD_CHANGEFOLD
) != 0)
1074 /* handle special fold cases, e.g. #1923350 */
1075 fold_changed(sci
, nt
->line
, nt
->foldLevelNow
, nt
->foldLevelPrev
);
1077 if (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
))
1079 document_update_tag_list_in_idle(doc
);
1084 on_char_added(editor
, nt
);
1087 case SCN_USERLISTSELECTION
:
1088 if (nt
->listType
== 1)
1090 sci_add_text(sci
, nt
->text
);
1094 case SCN_AUTOCSELECTION
:
1095 if (g_str_equal(nt
->text
, "..."))
1102 case SCN_AUTOCCANCELLED
:
1103 /* now that autocomplete is finishing or was cancelled, reshow calltips
1104 * if they were showing */
1105 request_reshowing_calltip(nt
);
1109 case SCN_STYLENEEDED
:
1110 geany_debug("style");
1114 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
1117 case SCN_URIDROPPED
:
1118 if (nt
->text
!= NULL
)
1120 document_open_file_list(nt
->text
, strlen(nt
->text
));
1124 case SCN_CALLTIPCLICK
:
1125 if (nt
->position
> 0)
1127 switch (nt
->position
)
1129 case 1: /* up arrow */
1130 if (calltip
.tag_index
> 0)
1131 calltip
.tag_index
--;
1134 case 2: calltip
.tag_index
++; break; /* down arrow */
1136 editor_show_calltip(editor
, -1);
1141 /* recalculate line margin width */
1142 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
1145 /* we always return FALSE here to let plugins handle the event too */
1150 /* Note: this is the same as sci_get_tab_width(), but is still useful when you don't have
1151 * a scintilla pointer. */
1152 static gint
get_tab_width(const GeanyIndentPrefs
*indent_prefs
)
1154 if (indent_prefs
->type
== GEANY_INDENT_TYPE_BOTH
)
1155 return indent_prefs
->hard_tab_width
;
1157 return indent_prefs
->width
; /* tab width = indent width */
1161 /* Returns a string containing width chars of whitespace, filled with simple space
1162 * characters or with the right number of tab characters, according to the indent prefs.
1163 * (Result is filled with tabs *and* spaces if width isn't a multiple of
1164 * the tab width). */
1166 get_whitespace(const GeanyIndentPrefs
*iprefs
, gint width
)
1168 g_return_val_if_fail(width
>= 0, NULL
);
1171 return g_strdup("");
1173 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
1175 return g_strnfill(width
, ' ');
1178 { /* first fill text with tabs and fill the rest with spaces */
1179 const gint tab_width
= get_tab_width(iprefs
);
1180 gint tabs
= width
/ tab_width
;
1181 gint spaces
= width
% tab_width
;
1182 gint len
= tabs
+ spaces
;
1185 str
= g_malloc(len
+ 1);
1187 memset(str
, '\t', tabs
);
1188 memset(str
+ tabs
, ' ', spaces
);
1195 static const GeanyIndentPrefs
*
1196 get_default_indent_prefs(void)
1198 static GeanyIndentPrefs iprefs
;
1200 iprefs
= app
->project
? *app
->project
->priv
->indentation
: *editor_prefs
.indentation
;
1205 /** Gets the indentation prefs for the editor.
1206 * Prefs can be different according to project or document.
1207 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
1208 * settings may have changed, or if this function has been called for a different editor.
1209 * @param editor The editor, or @c NULL to get the default indent prefs.
1210 * @return The indent prefs. */
1211 const GeanyIndentPrefs
*
1212 editor_get_indent_prefs(GeanyEditor
*editor
)
1214 static GeanyIndentPrefs iprefs
;
1215 const GeanyIndentPrefs
*dprefs
= get_default_indent_prefs();
1217 /* Return the address of the default prefs to allow returning default and editor
1218 * pref pointers without invalidating the contents of either. */
1223 iprefs
.type
= editor
->indent_type
;
1224 iprefs
.width
= editor
->indent_width
;
1226 /* if per-document auto-indent is enabled, but we don't have a global mode set,
1227 * just use basic auto-indenting */
1228 if (editor
->auto_indent
&& iprefs
.auto_indent_mode
== GEANY_AUTOINDENT_NONE
)
1229 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_BASIC
;
1231 if (!editor
->auto_indent
)
1232 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_NONE
;
1238 static void on_new_line_added(GeanyEditor
*editor
)
1240 ScintillaObject
*sci
= editor
->sci
;
1241 gint line
= sci_get_current_line(sci
);
1243 /* simple indentation */
1244 if (editor
->auto_indent
)
1246 insert_indent_after_line(editor
, line
- 1);
1249 if (editor_prefs
.auto_continue_multiline
)
1250 { /* " * " auto completion in multiline C/C++/D/Java comments */
1251 auto_multiline(editor
, line
);
1254 if (editor_prefs
.newline_strip
)
1255 { /* strip the trailing spaces on the previous line */
1256 editor_strip_line_trailing_spaces(editor
, line
- 1);
1261 static gboolean
lexer_has_braces(ScintillaObject
*sci
)
1263 gint lexer
= sci_get_lexer(sci
);
1269 case SCLEX_HTML
: /* for PHP & JS */
1270 case SCLEX_PASCAL
: /* for multiline comments? */
1281 /* Read indent chars for the line that pos is on into indent global variable.
1282 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1283 * instead in any new code. */
1284 static void read_indent(GeanyEditor
*editor
, gint pos
)
1286 ScintillaObject
*sci
= editor
->sci
;
1287 guint i
, len
, j
= 0;
1291 line
= sci_get_line_from_position(sci
, pos
);
1293 len
= sci_get_line_length(sci
, line
);
1294 linebuf
= sci_get_line(sci
, line
);
1296 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1298 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1299 indent
[j
++] = linebuf
[i
];
1308 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1310 gint start
= sci_get_position_from_line(sci
, line
);
1311 gint end
= sci_get_line_end_position(sci
, line
) - 1;
1312 gint lexer
= sci_get_lexer(sci
);
1316 for (pos
= end
; pos
>= start
&& count
< 1; pos
--)
1318 if (highlighting_is_code_style(lexer
, sci_get_style_at(sci
, pos
)))
1320 gchar c
= sci_get_char_at(sci
, pos
);
1329 return count
> 0 ? 1 : 0;
1333 /* gets the last code position on a line
1334 * warning: if there is no code position on the line, returns the start position */
1335 static gint
get_sci_line_code_end_position(ScintillaObject
*sci
, gint line
)
1337 gint start
= sci_get_position_from_line(sci
, line
);
1338 gint lexer
= sci_get_lexer(sci
);
1341 for (pos
= sci_get_line_end_position(sci
, line
) - 1; pos
> start
; pos
--)
1343 gint style
= sci_get_style_at(sci
, pos
);
1345 if (highlighting_is_code_style(lexer
, style
) && ! isspace(sci_get_char_at(sci
, pos
)))
1353 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1355 gint last_char
= get_sci_line_code_end_position(sci
, line
);
1357 /* add extra indentation for Python after colon */
1358 if (sci_get_char_at(sci
, last_char
) == ':' &&
1359 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1367 static gint
get_xml_indent(ScintillaObject
*sci
, gint line
)
1369 gboolean need_close
= FALSE
;
1370 gint end
= get_sci_line_code_end_position(sci
, line
);
1373 /* don't indent if there's a closing tag to the right of the cursor */
1374 pos
= sci_get_current_position(sci
);
1375 if (sci_get_char_at(sci
, pos
) == '<' &&
1376 sci_get_char_at(sci
, pos
+ 1) == '/')
1379 if (sci_get_char_at(sci
, end
) == '>' &&
1380 sci_get_char_at(sci
, end
- 1) != '/')
1382 gint style
= sci_get_style_at(sci
, end
);
1384 if (style
== SCE_H_TAG
|| style
== SCE_H_TAGUNKNOWN
)
1386 gint start
= sci_get_position_from_line(sci
, line
);
1387 gchar
*line_contents
= sci_get_contents_range(sci
, start
, end
+ 1);
1388 gchar
*opened_tag_name
= utils_find_open_xml_tag(line_contents
, end
+ 1 - start
);
1390 if (NZV(opened_tag_name
))
1393 if (sci_get_lexer(sci
) == SCLEX_HTML
&& utils_is_short_html_tag(opened_tag_name
))
1396 g_free(line_contents
);
1397 g_free(opened_tag_name
);
1401 return need_close
? 1 : 0;
1405 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1407 ScintillaObject
*sci
= editor
->sci
;
1409 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1411 g_return_val_if_fail(line
>= 0, 0);
1413 size
= sci_get_line_indentation(sci
, line
);
1415 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1417 gint additional_indent
= 0;
1419 if (lexer_has_braces(sci
))
1420 additional_indent
= iprefs
->width
* get_brace_indent(sci
, line
);
1421 else if (sci_get_lexer(sci
) == SCLEX_PYTHON
) /* Python/Cython */
1422 additional_indent
= iprefs
->width
* get_python_indent(sci
, line
);
1424 /* HTML lexer "has braces" because of PHP and JavaScript. If get_brace_indent() did not
1425 * recommend us to insert additional indent, we are probably not in PHP/JavaScript chunk and
1426 * should make the XML-related check */
1427 if (additional_indent
== 0 &&
1428 (sci_get_lexer(sci
) == SCLEX_HTML
||
1429 sci_get_lexer(sci
) == SCLEX_XML
) &&
1430 editor
->document
->file_type
->priv
->xml_indent_tags
)
1432 size
+= iprefs
->width
* get_xml_indent(sci
, line
);
1435 size
+= additional_indent
;
1441 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
)
1443 ScintillaObject
*sci
= editor
->sci
;
1444 gint line_indent
= sci_get_line_indentation(sci
, line
);
1445 gint size
= get_indent_size_after_line(editor
, line
);
1446 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1452 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
&& size
== line_indent
)
1454 /* support tab indents, space aligns style - copy last line 'indent' exactly */
1455 gint start
= sci_get_position_from_line(sci
, line
);
1456 gint end
= sci_get_line_indent_position(sci
, line
);
1458 text
= sci_get_contents_range(sci
, start
, end
);
1462 text
= get_whitespace(iprefs
, size
);
1464 sci_add_text(sci
, text
);
1469 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
)
1471 const gchar
*closing_char
= NULL
;
1474 if (utils_isbrace(c
, 0))
1475 end_pos
= sci_find_matching_brace(sci
, pos
- 1);
1480 if ((editor_prefs
.autoclose_chars
& GEANY_AC_PARENTHESIS
) && end_pos
== -1)
1484 if ((editor_prefs
.autoclose_chars
& GEANY_AC_CBRACKET
) && end_pos
== -1)
1488 if ((editor_prefs
.autoclose_chars
& GEANY_AC_SBRACKET
) && end_pos
== -1)
1492 if (editor_prefs
.autoclose_chars
& GEANY_AC_SQUOTE
)
1496 if (editor_prefs
.autoclose_chars
& GEANY_AC_DQUOTE
)
1497 closing_char
= "\"";
1501 if (closing_char
!= NULL
)
1503 sci_add_text(sci
, closing_char
);
1504 sci_set_current_position(sci
, pos
, TRUE
);
1509 /* Finds a corresponding matching brace to the given pos
1510 * (this is taken from Scintilla Editor.cxx,
1511 * fit to work with close_block) */
1512 static gint
brace_match(ScintillaObject
*sci
, gint pos
)
1514 gchar chBrace
= sci_get_char_at(sci
, pos
);
1515 gchar chSeek
= utils_brace_opposite(chBrace
);
1517 gint direction
= -1;
1522 /* Hack: we need the style at @p pos but it isn't computed yet, so force styling
1523 * of this very position */
1524 sci_colourise(sci
, pos
, pos
+ 1);
1526 styBrace
= sci_get_style_at(sci
, pos
);
1528 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1532 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1534 chAtPos
= sci_get_char_at(sci
, pos
);
1535 styAtPos
= sci_get_style_at(sci
, pos
);
1537 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1539 if (chAtPos
== chBrace
)
1541 if (chAtPos
== chSeek
)
1552 /* Called after typing '}'. */
1553 static void close_block(GeanyEditor
*editor
, gint pos
)
1555 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1556 gint x
= 0, cnt
= 0;
1557 gint line
, line_len
;
1558 gchar
*text
, *line_buf
;
1559 ScintillaObject
*sci
;
1560 gint line_indent
, last_indent
;
1562 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1564 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1568 if (! lexer_has_braces(sci
))
1571 line
= sci_get_line_from_position(sci
, pos
);
1572 line_len
= sci_get_line_end_position(sci
, line
) - sci_get_position_from_line(sci
, line
);
1574 /* check that the line is empty, to not kill text in the line */
1575 line_buf
= sci_get_line(sci
, line
);
1576 line_buf
[line_len
] = '\0';
1577 while (x
< line_len
)
1579 if (isspace(line_buf
[x
]))
1585 if ((line_len
- 1) != cnt
)
1588 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1590 gint start_brace
= brace_match(sci
, pos
);
1592 if (start_brace
>= 0)
1595 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1596 gint size
= sci_get_line_indentation(sci
, brace_line
);
1597 gchar
*ind
= get_whitespace(iprefs
, size
);
1599 text
= g_strconcat(ind
, "}", NULL
);
1600 line_start
= sci_get_position_from_line(sci
, line
);
1601 sci_set_anchor(sci
, line_start
);
1602 sci_replace_sel(sci
, text
);
1607 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1610 /* GEANY_AUTOINDENT_CURRENTCHARS */
1611 line_indent
= sci_get_line_indentation(sci
, line
);
1612 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1614 if (line_indent
< last_indent
)
1616 line_indent
-= iprefs
->width
;
1617 line_indent
= MAX(0, line_indent
);
1618 sci_set_line_indentation(sci
, line
, line_indent
);
1622 /* checks whether @p c is an ASCII character (e.g. < 0x80) */
1623 #define IS_ASCII(c) (((unsigned char)(c)) < 0x80)
1626 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1627 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1628 * position can be -1, then the current position is used.
1629 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1630 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
1631 const gchar
*wc
, gboolean stem
)
1633 gint line
, line_start
, startword
, endword
;
1635 ScintillaObject
*sci
;
1637 g_return_if_fail(editor
!= NULL
);
1641 pos
= sci_get_current_position(sci
);
1643 line
= sci_get_line_from_position(sci
, pos
);
1644 line_start
= sci_get_position_from_line(sci
, line
);
1645 startword
= pos
- line_start
;
1646 endword
= pos
- line_start
;
1649 chunk
= sci_get_line(sci
, line
);
1652 wc
= GEANY_WORDCHARS
;
1654 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1655 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1656 * TODO: improve this code */
1657 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || ! IS_ASCII(chunk
[startword
- 1])))
1661 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || ! IS_ASCII(chunk
[endword
])))
1665 if (startword
!= endword
)
1667 chunk
[endword
] = '\0';
1669 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1672 g_strlcpy(word
, "", wordlen
);
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 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
,
1685 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1689 /* Same as editor_find_current_word() but uses editor's word boundaries to decide what the word
1690 * is. This should be used e.g. to get the word to search for */
1691 void editor_find_current_word_sciwc(GeanyEditor
*editor
, gint pos
, gchar
*word
, gsize wordlen
)
1696 g_return_if_fail(editor
!= NULL
);
1699 pos
= sci_get_current_position(editor
->sci
);
1701 start
= SSM(editor
->sci
, SCI_WORDSTARTPOSITION
, pos
, TRUE
);
1702 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, TRUE
);
1704 if (start
== end
) /* caret in whitespaces sequence */
1708 if ((guint
)(end
- start
) >= wordlen
)
1709 end
= start
+ (wordlen
- 1);
1710 sci_get_text_range(editor
->sci
, start
, end
, word
);
1716 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1717 * Otherwise NULL is returned.
1718 * Additional wordchars can be specified to define what to consider as a word.
1720 * @param editor The editor to operate on.
1721 * @param pos The position where the word should be read from.
1722 * May be @c -1 to use the current position.
1723 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1724 * as part of a word. May be @c NULL to use the default wordchars,
1725 * see @ref GEANY_WORDCHARS.
1727 * @return A newly-allocated string containing the word at the given @a pos or @c NULL.
1728 * Should be freed when no longer needed.
1732 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1734 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1736 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1738 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1740 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1744 /* Read the word up to position @a pos. */
1745 static const gchar
*
1746 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1748 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1750 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1752 return (*word
) ? word
: NULL
;
1756 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1758 gint orig_pos
= pos
;
1760 while (pos
>= 0 && pos
> orig_pos
- 300)
1762 gchar c
= sci_get_char_at(sci
, pos
);
1763 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1771 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1774 gint orig_pos
= pos
;
1776 while (pos
> 0 && pos
> orig_pos
- 300)
1778 gchar c
= sci_get_char_at(sci
, pos
);
1780 if (c
== ')') brackets
++;
1781 else if (c
== '(') brackets
--;
1782 if (brackets
< 0) return pos
; /* found start bracket */
1789 static gboolean
append_calltip(GString
*str
, const TMTag
*tag
, filetype_id ft_id
)
1791 if (! tag
->atts
.entry
.arglist
)
1794 if (ft_id
!= GEANY_FILETYPES_PASCAL
)
1795 { /* usual calltips: "retval tagname (arglist)" */
1796 if (tag
->atts
.entry
.var_type
)
1800 g_string_append(str
, tag
->atts
.entry
.var_type
);
1801 for (i
= 0; i
< tag
->atts
.entry
.pointerOrder
; i
++)
1803 g_string_append_c(str
, '*');
1805 g_string_append_c(str
, ' ');
1807 if (tag
->atts
.entry
.scope
)
1809 const gchar
*cosep
= symbols_get_context_separator(ft_id
);
1811 g_string_append(str
, tag
->atts
.entry
.scope
);
1812 g_string_append(str
, cosep
);
1814 g_string_append(str
, tag
->name
);
1815 g_string_append_c(str
, ' ');
1816 g_string_append(str
, tag
->atts
.entry
.arglist
);
1819 { /* special case Pascal calltips: "tagname (arglist) : retval" */
1820 g_string_append(str
, tag
->name
);
1821 g_string_append_c(str
, ' ');
1822 g_string_append(str
, tag
->atts
.entry
.arglist
);
1824 if (NZV(tag
->atts
.entry
.var_type
))
1826 g_string_append(str
, " : ");
1827 g_string_append(str
, tag
->atts
.entry
.var_type
);
1835 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1837 const GPtrArray
*tags
;
1838 const gint arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1839 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1840 TMTagAttrType
*attrs
= NULL
;
1842 GString
*str
= NULL
;
1845 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1847 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1848 tags
= tm_workspace_find(word
, tm_tag_max_t
, attrs
, FALSE
, ft
->lang
);
1852 tag
= TM_TAG(tags
->pdata
[0]);
1854 if (ft
->id
== GEANY_FILETYPES_D
&&
1855 (tag
->type
== tm_tag_class_t
|| tag
->type
== tm_tag_struct_t
))
1857 /* user typed e.g. 'new Classname(' so lookup D constructor Classname::this() */
1858 tags
= tm_workspace_find_scoped("this", tag
->name
,
1859 arg_types
, attrs
, FALSE
, ft
->lang
, TRUE
);
1864 /* remove tags with no argument list */
1865 for (i
= 0; i
< tags
->len
; i
++)
1867 tag
= TM_TAG(tags
->pdata
[i
]);
1869 if (! tag
->atts
.entry
.arglist
)
1870 tags
->pdata
[i
] = NULL
;
1872 tm_tags_prune((GPtrArray
*) tags
);
1876 { /* remove duplicate calltips */
1877 TMTagAttrType sort_attr
[] = {tm_tag_attr_name_t
, tm_tag_attr_scope_t
,
1878 tm_tag_attr_arglist_t
, 0};
1880 tm_tags_sort((GPtrArray
*) tags
, sort_attr
, TRUE
);
1883 /* if the current word has changed since last time, start with the first tag match */
1884 if (! utils_str_equal(word
, calltip
.last_word
))
1885 calltip
.tag_index
= 0;
1886 /* cache the current word for next time */
1887 g_free(calltip
.last_word
);
1888 calltip
.last_word
= g_strdup(word
);
1889 calltip
.tag_index
= MIN(calltip
.tag_index
, tags
->len
- 1); /* ensure tag_index is in range */
1891 for (i
= calltip
.tag_index
; i
< tags
->len
; i
++)
1893 tag
= TM_TAG(tags
->pdata
[i
]);
1897 str
= g_string_new(NULL
);
1898 if (calltip
.tag_index
> 0)
1899 g_string_prepend(str
, "\001 "); /* up arrow */
1900 append_calltip(str
, tag
, FILETYPE_ID(ft
));
1902 else /* add a down arrow */
1904 if (calltip
.tag_index
> 0) /* already have an up arrow */
1905 g_string_insert_c(str
, 1, '\002');
1907 g_string_prepend(str
, "\002 ");
1913 gchar
*result
= str
->str
;
1915 g_string_free(str
, FALSE
);
1922 /* use pos = -1 to search for the previous unmatched open bracket. */
1923 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
1925 gint orig_pos
= pos
; /* the position for the calltip */
1928 gchar word
[GEANY_MAX_WORD_LENGTH
];
1930 ScintillaObject
*sci
;
1932 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1933 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
1937 lexer
= sci_get_lexer(sci
);
1941 /* position of '(' is unknown, so go backwards from current position to find it */
1942 pos
= sci_get_current_position(sci
);
1945 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
1946 find_start_bracket(sci
, pos
);
1951 /* the style 1 before the brace (which may be highlighted) */
1952 style
= sci_get_style_at(sci
, pos
- 1);
1953 if (! highlighting_is_code_style(lexer
, style
))
1957 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
1958 if (word
[0] == '\0')
1961 str
= find_calltip(word
, editor
->document
->file_type
);
1964 g_free(calltip
.text
); /* free the old calltip */
1966 calltip
.pos
= orig_pos
;
1969 utils_wrap_string(calltip
.text
, -1);
1970 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
1977 gchar
*editor_get_calltip_text(GeanyEditor
*editor
, const TMTag
*tag
)
1981 g_return_val_if_fail(editor
!= NULL
, NULL
);
1983 str
= g_string_new(NULL
);
1984 if (append_calltip(str
, tag
, editor
->document
->file_type
->id
))
1985 return g_string_free(str
, FALSE
);
1987 return g_string_free(str
, TRUE
);
1991 /* HTML entities auto completion from html_entities.tags text file */
1993 autocomplete_html(ScintillaObject
*sci
, const gchar
*root
, gsize rootlen
)
1996 gboolean found
= FALSE
;
1998 const gchar
**entities
= symbols_get_html_entities();
2000 if (*root
!= '&' || entities
== NULL
)
2003 words
= g_string_sized_new(500);
2006 if (entities
[i
] == NULL
)
2008 else if (entities
[i
][0] == '#')
2011 if (! strncmp(entities
[i
], root
, rootlen
))
2014 g_string_append_c(words
, '\n');
2016 g_string_append(words
, entities
[i
]);
2021 show_autocomplete(sci
, rootlen
, words
);
2023 g_string_free(words
, TRUE
);
2028 /* Current document & global tags autocompletion */
2030 autocomplete_tags(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
2032 TMTagAttrType attrs
[] = { tm_tag_attr_name_t
, 0 };
2033 const GPtrArray
*tags
;
2036 g_return_val_if_fail(editor
, FALSE
);
2038 doc
= editor
->document
;
2040 tags
= tm_workspace_find(root
, tm_tag_max_t
, attrs
, TRUE
, doc
->file_type
->lang
);
2043 show_tags_list(editor
, tags
, rootlen
);
2044 return tags
->len
> 0;
2050 static gboolean
autocomplete_check_html(GeanyEditor
*editor
, gint style
, gint pos
)
2052 GeanyFiletype
*ft
= editor
->document
->file_type
;
2053 gboolean
try = FALSE
;
2055 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
2056 * (everything after SCE_HJ_START is for embedded scripting languages) */
2057 if (ft
->id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
2059 else if (sci_get_lexer(editor
->sci
) == SCLEX_XML
&& style
< SCE_HJ_START
)
2061 else if (ft
->id
== GEANY_FILETYPES_PHP
)
2063 /* use entity completion when style is outside of PHP styles */
2064 if (! is_style_php(style
))
2069 gchar root
[GEANY_MAX_WORD_LENGTH
];
2072 read_current_word(editor
, pos
, root
, sizeof(root
), GEANY_WORDCHARS
"&", TRUE
);
2074 /* Allow something like ""some text"".
2075 * for entity completion we want to have completion for '&' within words. */
2076 tmp
= strchr(root
, '&');
2079 return autocomplete_html(editor
->sci
, tmp
, strlen(tmp
));
2086 /* Algorithm based on based on Scite's StartAutoCompleteWord()
2087 * @returns a sorted list of words matching @p root */
2088 static GSList
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
2091 gint len
, current
, word_end
;
2092 gint pos_find
, flags
;
2095 GSList
*words
= NULL
;
2096 struct Sci_TextToFind ttf
;
2098 len
= sci_get_length(sci
);
2099 current
= sci_get_current_position(sci
) - rootlen
;
2101 ttf
.lpstrText
= root
;
2103 ttf
.chrg
.cpMax
= len
;
2104 ttf
.chrgText
.cpMin
= 0;
2105 ttf
.chrgText
.cpMax
= 0;
2106 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
2108 /* search the whole document for the word root and collect results */
2109 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2110 while (pos_find
>= 0 && pos_find
< len
)
2112 word_end
= pos_find
+ rootlen
;
2113 if (pos_find
!= current
)
2115 word_end
= SSM(sci
, SCI_WORDENDPOSITION
, word_end
, TRUE
);
2117 word_length
= word_end
- pos_find
;
2118 if (word_length
> rootlen
)
2120 word
= sci_get_contents_range(sci
, pos_find
, word_end
);
2121 /* search whether we already have the word in, otherwise add it */
2122 if (g_slist_find_custom(words
, word
, (GCompareFunc
)utils_str_casecmp
) != NULL
)
2126 words
= g_slist_prepend(words
, word
);
2130 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
2134 ttf
.chrg
.cpMin
= word_end
;
2135 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2138 return g_slist_sort(words
, (GCompareFunc
)utils_str_casecmp
);
2142 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
2144 ScintillaObject
*sci
= editor
->sci
;
2145 GSList
*words
, *node
;
2149 words
= get_doc_words(sci
, root
, rootlen
);
2152 scintilla_send_message(sci
, SCI_AUTOCCANCEL
, 0, 0);
2156 str
= g_string_sized_new(editor_prefs
.autocompletion_max_entries
* (rootlen
+ 1));
2157 foreach_slist(node
, words
)
2159 g_string_append(str
, node
->data
);
2162 g_string_append_c(str
, '\n');
2165 if (n_words
>= editor_prefs
.autocompletion_max_entries
)
2166 g_string_append(str
, "\n...");
2168 g_slist_free(words
);
2170 show_autocomplete(sci
, rootlen
, str
);
2171 g_string_free(str
, TRUE
);
2176 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
2178 gint rootlen
, lexer
, style
;
2180 gchar cword
[GEANY_MAX_WORD_LENGTH
];
2181 ScintillaObject
*sci
;
2182 gboolean ret
= FALSE
;
2183 const gchar
*wordchars
;
2186 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2188 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
2191 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
2192 * necessary styling information */
2193 if (G_UNLIKELY(pos
< 2))
2197 ft
= editor
->document
->file_type
;
2199 lexer
= sci_get_lexer(sci
);
2200 style
= sci_get_style_at(sci
, pos
- 2);
2202 /* don't autocomplete in comments and strings */
2203 if (!force
&& !highlighting_is_code_style(lexer
, style
))
2206 autocomplete_scope(editor
);
2207 ret
= autocomplete_check_html(editor
, style
, pos
);
2209 if (ft
->id
== GEANY_FILETYPES_LATEX
)
2210 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
2211 else if (ft
->id
== GEANY_FILETYPES_CSS
)
2212 wordchars
= GEANY_WORDCHARS
"-"; /* add - because they are part of property names */
2214 wordchars
= GEANY_WORDCHARS
;
2216 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, TRUE
);
2218 rootlen
= strlen(root
);
2220 if (!ret
&& rootlen
> 0)
2222 if (ft
->id
== GEANY_FILETYPES_PHP
&& style
== SCE_HPHP_DEFAULT
&&
2223 rootlen
== 3 && strcmp(root
, "php") == 0 && pos
>= 5 &&
2224 sci_get_char_at(sci
, pos
- 5) == '<' &&
2225 sci_get_char_at(sci
, pos
- 4) == '?')
2227 /* nothing, don't complete PHP open tags */
2231 /* force is set when called by keyboard shortcut, otherwise start at the
2232 * editor_prefs.symbolcompletion_min_chars'th char */
2233 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
2235 /* complete tags, except if forcing when completion is already visible */
2236 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
2237 ret
= autocomplete_tags(editor
, root
, rootlen
);
2239 /* If forcing and there's nothing else to show, complete from words in document */
2240 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2241 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2252 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2254 gchar
*result
= NULL
;
2257 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2259 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2262 result
= g_hash_table_lookup(tmp
, name
);
2264 /* whether nothing is set for the current filetype(tmp is NULL) or
2265 * the particular completion for this filetype is not set (result is NULL) */
2266 if (tmp
== NULL
|| result
== NULL
)
2268 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2271 result
= g_hash_table_lookup(tmp
, name
);
2274 /* if result is still NULL here, no completion could be found */
2276 /* result is owned by the hash table and will be freed when the table will destroyed */
2281 static void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2284 GString
*pattern
= user_data
;
2286 g_return_if_fail(key
!= NULL
);
2287 g_return_if_fail(value
!= NULL
);
2289 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2291 utils_string_replace_all(pattern
, needle
, (gchar
*) value
);
2296 static void fix_indentation(GeanyEditor
*editor
, GString
*buf
)
2298 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2301 gint cflags
= G_REGEX_MULTILINE
;
2303 /* transform leading tabs into indent widths (in spaces) */
2304 whitespace
= g_strnfill(iprefs
->width
, ' ');
2305 regex
= g_regex_new("^ *(\t)", cflags
, 0, NULL
);
2306 while (utils_string_regex_replace_all(buf
, regex
, 1, whitespace
, TRUE
));
2307 g_regex_unref(regex
);
2309 /* remaining tabs are for alignment */
2310 if (iprefs
->type
!= GEANY_INDENT_TYPE_TABS
)
2311 utils_string_replace_all(buf
, "\t", whitespace
);
2313 /* use leading tabs */
2314 if (iprefs
->type
!= GEANY_INDENT_TYPE_SPACES
)
2318 /* for tabs+spaces mode we want the real tab width, not indent width */
2319 SETPTR(whitespace
, g_strnfill(sci_get_tab_width(editor
->sci
), ' '));
2320 str
= g_strdup_printf("^\t*(%s)", whitespace
);
2322 regex
= g_regex_new(str
, cflags
, 0, NULL
);
2323 while (utils_string_regex_replace_all(buf
, regex
, 1, "\t", TRUE
));
2324 g_regex_unref(regex
);
2331 /** Inserts text, replacing \\t tab chars (@c 0x9) and \\n newline chars (@c 0xA)
2332 * accordingly for the document.
2333 * - Leading tabs are replaced with the correct indentation.
2334 * - Non-leading tabs are replaced with spaces (except when using 'Tabs' indent type).
2335 * - Newline chars are replaced with the correct line ending string.
2336 * This is very useful for inserting code without having to handle the indent
2337 * type yourself (Tabs & Spaces mode can be tricky).
2338 * @param editor Editor.
2339 * @param text Intended as e.g. @c "if (foo)\n\tbar();".
2340 * @param insert_pos Document position to insert text at.
2341 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2342 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2343 * -1 to read the indent size from the line with @a insert_pos on it.
2344 * @param replace_newlines Whether to replace newlines. If
2345 * newlines have been replaced already, this should be false, to avoid errors e.g. on Windows.
2346 * @warning Make sure all \\t tab chars in @a text are intended as indent widths or alignment,
2347 * not hard tabs, as those won't be preserved.
2348 * @note This doesn't scroll the cursor in view afterwards. **/
2349 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2350 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2352 ScintillaObject
*sci
= editor
->sci
;
2353 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2355 const gchar
*eol
= editor_get_eol_char(editor
);
2358 g_return_if_fail(text
);
2359 g_return_if_fail(editor
!= NULL
);
2360 g_return_if_fail(insert_pos
>= 0);
2362 buf
= g_string_new(text
);
2364 if (cursor_index
>= 0)
2365 g_string_insert(buf
, cursor_index
, geany_cursor_marker
); /* remember cursor pos */
2367 if (newline_indent_size
== -1)
2369 /* count indent size up to insert_pos instead of asking sci
2370 * because there may be spaces after it */
2371 gchar
*tmp
= sci_get_line(sci
, line_start
);
2373 idx
= insert_pos
- sci_get_position_from_line(sci
, line_start
);
2375 newline_indent_size
= count_indent_size(editor
, tmp
);
2379 /* Add line indents (in spaces) */
2380 if (newline_indent_size
> 0)
2382 const gchar
*nl
= replace_newlines
? "\n" : eol
;
2385 whitespace
= g_strnfill(newline_indent_size
, ' ');
2386 SETPTR(whitespace
, g_strconcat(nl
, whitespace
, NULL
));
2387 utils_string_replace_all(buf
, nl
, whitespace
);
2391 /* transform line endings */
2392 if (replace_newlines
)
2393 utils_string_replace_all(buf
, "\n", eol
);
2395 fix_indentation(editor
, buf
);
2397 idx
= replace_cursor_markers(editor
, buf
);
2400 sci_insert_text(sci
, insert_pos
, buf
->str
);
2401 sci_set_current_position(sci
, insert_pos
+ idx
, FALSE
);
2404 sci_insert_text(sci
, insert_pos
, buf
->str
);
2406 snippet_cursor_insert_pos
= sci_get_current_position(sci
);
2408 g_string_free(buf
, TRUE
);
2412 /* Move the cursor to the next specified cursor position in an inserted snippet.
2413 * Can, and should, be optimized to give better results */
2414 void editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2416 ScintillaObject
*sci
= editor
->sci
;
2417 gint current_pos
= sci_get_current_position(sci
);
2419 if (snippet_offsets
&& !g_queue_is_empty(snippet_offsets
))
2423 offset
= GPOINTER_TO_INT(g_queue_pop_head(snippet_offsets
));
2424 if (current_pos
> snippet_cursor_insert_pos
)
2425 snippet_cursor_insert_pos
= offset
+ current_pos
;
2427 snippet_cursor_insert_pos
+= offset
;
2429 sci_set_current_position(sci
, snippet_cursor_insert_pos
, TRUE
);
2438 static void snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
)
2440 GHashTable
*specials
;
2442 /* replace 'special' completions */
2443 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2444 if (G_LIKELY(specials
!= NULL
))
2446 g_hash_table_foreach(specials
, snippets_replace_specials
, pattern
);
2449 /* now transform other wildcards */
2450 utils_string_replace_all(pattern
, "%newline%", "\n");
2451 utils_string_replace_all(pattern
, "%ws%", "\t");
2453 /* replace %cursor% by a very unlikely string marker */
2454 utils_string_replace_all(pattern
, "%cursor%", geany_cursor_marker
);
2456 /* unescape '%' after all %wildcards% */
2457 templates_replace_valist(pattern
, "{pc}", "%", NULL
);
2459 /* replace any template {foo} wildcards */
2460 templates_replace_common(pattern
, editor
->document
->file_name
, editor
->document
->file_type
, NULL
);
2464 static gssize
replace_cursor_markers(GeanyEditor
*editor
, GString
*pattern
)
2466 gssize cur_index
= -1;
2468 GList
*temp_list
= NULL
;
2469 gint cursor_steps
= 0, old_cursor
= 0;
2474 cursor_steps
= utils_string_find(pattern
, cursor_steps
, -1, geany_cursor_marker
);
2475 if (cursor_steps
== -1)
2478 g_string_erase(pattern
, cursor_steps
, strlen(geany_cursor_marker
));
2482 /* save the relative offset to each cursor position */
2483 temp_list
= g_list_prepend(temp_list
, GINT_TO_POINTER(cursor_steps
- old_cursor
));
2487 /* first cursor already includes newline positions */
2488 cur_index
= cursor_steps
;
2490 old_cursor
= cursor_steps
;
2493 /* put the cursor positions for the most recent
2494 * parsed snippet first, followed by any remaining positions */
2500 temp_list
= g_list_reverse(temp_list
);
2501 foreach_list(node
, temp_list
)
2502 g_queue_push_nth(snippet_offsets
, node
->data
, i
++);
2504 /* limit length of queue */
2505 while (g_queue_get_length(snippet_offsets
) > 20)
2506 g_queue_pop_tail(snippet_offsets
);
2508 g_list_free(temp_list
);
2510 /* if there's no first cursor, skip whole snippet */
2512 cur_index
= pattern
->len
;
2518 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2520 ScintillaObject
*sci
= editor
->sci
;
2522 const gchar
*completion
;
2524 gint ft_id
= editor
->document
->file_type
->id
;
2526 str
= g_strdup(word
);
2529 completion
= snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
);
2530 if (completion
== NULL
)
2536 /* remove the typed word, it will be added again by the used auto completion
2537 * (not really necessary but this makes the auto completion more flexible,
2538 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2539 str_len
= strlen(str
);
2540 sci_set_selection_start(sci
, pos
- str_len
);
2541 sci_set_selection_end(sci
, pos
);
2542 sci_replace_sel(sci
, "");
2543 pos
-= str_len
; /* pos has changed while deleting */
2545 editor_insert_snippet(editor
, pos
, completion
);
2546 sci_scroll_caret(sci
);
2553 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2555 gint line
= sci_get_line_from_position(sci
, pos
);
2558 /* skip any trailing spaces */
2561 c
= sci_get_char_at(sci
, pos
);
2562 if (c
== ' ' || c
== '\t')
2568 return (pos
== sci_get_line_end_position(sci
, line
));
2572 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2574 gboolean result
= FALSE
;
2577 ScintillaObject
*sci
;
2579 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2582 if (sci_has_selection(sci
))
2584 /* return if we are editing an existing line (chars on right of cursor) */
2585 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2586 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_space
&&
2587 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2590 wc
= snippets_find_completion_by_name("Special", "wordchars");
2591 word
= editor_read_word_stem(editor
, pos
, wc
);
2593 /* prevent completion of "for " */
2595 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2597 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2598 result
= snippets_complete_constructs(editor
, pos
, word
);
2599 sci_end_undo_action(sci
);
2601 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2607 void editor_show_macro_list(GeanyEditor
*editor
)
2611 if (editor
== NULL
|| editor
->document
->file_type
== NULL
)
2614 words
= symbols_get_macro_list(editor
->document
->file_type
->lang
);
2618 SSM(editor
->sci
, SCI_USERLISTSHOW
, 1, (sptr_t
) words
->str
);
2619 g_string_free(words
, TRUE
);
2623 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2625 ScintillaObject
*sci
= editor
->sci
;
2626 gchar
*to_insert
= NULL
;
2630 const gchar
*gt
= ">";
2631 /* if there is already a '>' behind the cursor, don't add it */
2632 if (sci_get_char_at(sci
, pos
) == '>')
2635 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2638 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2640 sci_start_undo_action(sci
);
2641 sci_replace_sel(sci
, to_insert
);
2643 sci_set_selection(sci
, pos
, pos
);
2644 sci_end_undo_action(sci
);
2650 * (stolen from anjuta and heavily modified)
2651 * This routine will auto complete XML or HTML tags that are still open by closing them
2652 * @param ch The character we are dealing with, currently only works with the '>' character
2653 * @return True if handled, false otherwise
2655 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2657 ScintillaObject
*sci
= editor
->sci
;
2658 gint lexer
= sci_get_lexer(sci
);
2659 gint min
, size
, style
;
2660 gchar
*str_found
, sel
[512];
2661 gboolean result
= FALSE
;
2663 /* If the user has turned us off, quit now.
2664 * This may make sense only in certain languages */
2665 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2668 /* return if we are inside any embedded script */
2669 style
= sci_get_style_at(sci
, pos
);
2670 if (style
> SCE_H_XCCOMMENT
&& ! highlighting_is_string_style(lexer
, style
))
2673 /* if ch is /, check for </, else quit */
2674 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2677 /* Grab the last 512 characters or so */
2678 min
= pos
- (sizeof(sel
) - 1);
2679 if (min
< 0) min
= 0;
2682 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2684 sci_get_text_range(sci
, min
, pos
, sel
);
2685 sel
[sizeof(sel
) - 1] = '\0';
2687 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2688 /* User typed something like "<br/>" */
2693 size
-= 2; /* skip </ */
2694 str_found
= utils_find_open_xml_tag(sel
, size
);
2696 if (lexer
== SCLEX_HTML
&& utils_is_short_html_tag(str_found
))
2700 else if (NZV(str_found
))
2702 insert_closing_tag(editor
, pos
, ch
, str_found
);
2710 /* like sci_get_line_indentation(), but for a string. */
2711 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2714 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2717 g_return_val_if_fail(base_indent
, 0);
2719 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2737 /* Handles special cases where HTML is embedded in another language or
2738 * another language is embedded in HTML */
2739 static GeanyFiletype
*editor_get_filetype_at_line(GeanyEditor
*editor
, gint line
)
2741 gint style
, line_start
;
2742 GeanyFiletype
*current_ft
;
2744 g_return_val_if_fail(editor
!= NULL
, NULL
);
2745 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, NULL
);
2747 current_ft
= editor
->document
->file_type
;
2748 line_start
= sci_get_position_from_line(editor
->sci
, line
);
2749 style
= sci_get_style_at(editor
->sci
, line_start
);
2751 /* Handle PHP filetype with embedded HTML */
2752 if (current_ft
->id
== GEANY_FILETYPES_PHP
&& ! is_style_php(style
))
2753 current_ft
= filetypes
[GEANY_FILETYPES_HTML
];
2755 /* Handle languages embedded in HTML */
2756 if (current_ft
->id
== GEANY_FILETYPES_HTML
)
2759 if (style
>= SCE_HJ_DEFAULT
&& style
<= SCE_HJ_REGEX
)
2760 current_ft
= filetypes
[GEANY_FILETYPES_JS
];
2762 else if (style
>= SCE_HJA_DEFAULT
&& style
<= SCE_HJA_REGEX
)
2763 current_ft
= filetypes
[GEANY_FILETYPES_JS
];
2765 else if (style
>= SCE_HB_DEFAULT
&& style
<= SCE_HB_STRINGEOL
)
2766 current_ft
= filetypes
[GEANY_FILETYPES_BASIC
];
2768 else if (style
>= SCE_HBA_DEFAULT
&& style
<= SCE_HBA_STRINGEOL
)
2769 current_ft
= filetypes
[GEANY_FILETYPES_BASIC
];
2770 /* Embedded Python */
2771 else if (style
>= SCE_HP_DEFAULT
&& style
<= SCE_HP_IDENTIFIER
)
2772 current_ft
= filetypes
[GEANY_FILETYPES_PYTHON
];
2774 else if (style
>= SCE_HPA_DEFAULT
&& style
<= SCE_HPA_IDENTIFIER
)
2775 current_ft
= filetypes
[GEANY_FILETYPES_PYTHON
];
2777 else if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
2778 style
== SCE_HPHP_COMPLEX_VARIABLE
)
2780 current_ft
= filetypes
[GEANY_FILETYPES_PHP
];
2784 /* Ensure the filetype's config is loaded */
2785 filetypes_load_config(current_ft
->id
, FALSE
);
2791 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2794 gchar
*str_begin
, *str_end
;
2795 const gchar
*co
, *cc
;
2799 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2801 ft
= editor_get_filetype_at_line(editor
, line_start
);
2803 eol
= editor_get_eol_char(editor
);
2804 if (! filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
))
2805 g_return_if_reached();
2806 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2807 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2809 /* insert the comment strings */
2810 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2811 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2812 sci_insert_text(editor
->sci
, line_len
, str_end
);
2819 /* find @p text inside the range of the current style */
2820 static gint
find_in_current_style(ScintillaObject
*sci
, const gchar
*text
, gboolean backwards
)
2822 gint start
= sci_get_current_position(sci
);
2824 gint len
= sci_get_length(sci
);
2825 gint current_style
= sci_get_style_at(sci
, start
);
2826 struct Sci_TextToFind ttf
;
2828 while (start
> 0 && sci_get_style_at(sci
, start
- 1) == current_style
)
2830 while (end
< len
&& sci_get_style_at(sci
, end
+ 1) == current_style
)
2833 ttf
.lpstrText
= (gchar
*) text
;
2834 ttf
.chrg
.cpMin
= backwards
? end
+ 1 : start
;
2835 ttf
.chrg
.cpMax
= backwards
? start
: end
+ 1;
2836 return sci_find_text(sci
, 0, &ttf
);
2840 static void sci_delete_line(ScintillaObject
*sci
, gint line
)
2842 gint start
= sci_get_position_from_line(sci
, line
);
2843 gint len
= sci_get_line_length(sci
, line
);
2844 SSM(sci
, SCI_DELETERANGE
, start
, len
);
2848 static gboolean
real_uncomment_multiline(GeanyEditor
*editor
)
2850 /* find the beginning of the multi line comment */
2851 gint start
, end
, start_line
, end_line
;
2853 const gchar
*co
, *cc
;
2855 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, FALSE
);
2857 ft
= editor_get_filetype_at_line(editor
, sci_get_current_line(editor
->sci
));
2858 if (! filetype_get_comment_open_close(ft
, FALSE
, &co
, &cc
))
2859 g_return_val_if_reached(FALSE
);
2861 start
= find_in_current_style(editor
->sci
, co
, TRUE
);
2862 end
= find_in_current_style(editor
->sci
, cc
, FALSE
);
2864 if (start
< 0 || end
< 0 || start
> end
/* who knows */)
2867 start_line
= sci_get_line_from_position(editor
->sci
, start
);
2868 end_line
= sci_get_line_from_position(editor
->sci
, end
);
2870 /* remove comment close chars */
2871 SSM(editor
->sci
, SCI_DELETERANGE
, end
, strlen(cc
));
2872 if (sci_is_blank_line(editor
->sci
, end_line
))
2873 sci_delete_line(editor
->sci
, end_line
);
2875 /* remove comment open chars (do it last since it would move the end position) */
2876 SSM(editor
->sci
, SCI_DELETERANGE
, start
, strlen(co
));
2877 if (sci_is_blank_line(editor
->sci
, start_line
))
2878 sci_delete_line(editor
->sci
, start_line
);
2884 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2886 gint lexer
= sci_get_lexer(editor
->sci
);
2889 /* List only those lexers which support multi line comments */
2895 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2896 style_comment
= SCE_HPHP_COMMENT
;
2898 style_comment
= SCE_H_COMMENT
;
2901 case SCLEX_HASKELL
: style_comment
= SCE_HA_COMMENTBLOCK
; break;
2902 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2903 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2904 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2905 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2906 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2907 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2908 default: style_comment
= SCE_C_COMMENT
;
2911 return style_comment
;
2915 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2916 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2917 * it returns just 1 */
2918 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2920 gint first_line
, last_line
;
2921 gint x
, i
, line_start
, line_len
;
2922 gint sel_start
, sel_end
;
2926 const gchar
*co
, *cc
;
2927 gboolean single_line
= FALSE
;
2930 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
2933 { /* use selection or current line */
2934 sel_start
= sci_get_selection_start(editor
->sci
);
2935 sel_end
= sci_get_selection_end(editor
->sci
);
2937 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
2938 /* Find the last line with chars selected (not EOL char) */
2939 last_line
= sci_get_line_from_position(editor
->sci
,
2940 sel_end
- editor_get_eol_char_len(editor
));
2941 last_line
= MAX(first_line
, last_line
);
2945 first_line
= last_line
= line
;
2946 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
2949 ft
= editor_get_filetype_at_line(editor
, first_line
);
2951 if (! filetype_get_comment_open_close(ft
, TRUE
, &co
, &cc
))
2954 co_len
= strlen(co
);
2958 sci_start_undo_action(editor
->sci
);
2960 for (i
= first_line
; i
<= last_line
; i
++)
2964 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2965 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
2968 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
2971 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2972 sel
[buf_len
] = '\0';
2974 while (isspace(sel
[x
])) x
++;
2976 /* to skip blank lines */
2977 if (x
< line_len
&& sel
[x
] != '\0')
2979 /* use single line comment */
2986 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2987 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
2988 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
2995 if (strncmp(sel
+ x
, co
, co_len
) != 0)
2999 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
3000 sci_replace_sel(editor
->sci
, "");
3003 /* use multi line comment */
3008 /* skip lines which are already comments */
3009 style_comment
= get_multiline_comment_style(editor
, line_start
);
3010 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3012 if (real_uncomment_multiline(editor
))
3016 /* break because we are already on the last line */
3021 sci_end_undo_action(editor
->sci
);
3023 /* restore selection if there is one
3024 * but don't touch the selection if caller is editor_do_comment_toggle */
3025 if (! toggle
&& sel_start
< sel_end
)
3029 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
3030 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
3034 gint eol_len
= editor_get_eol_char_len(editor
);
3035 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
3036 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
3044 void editor_do_comment_toggle(GeanyEditor
*editor
)
3046 gint first_line
, last_line
;
3047 gint x
, i
, line_start
, line_len
, first_line_start
, last_line_start
;
3048 gint sel_start
, sel_end
;
3049 gint count_commented
= 0, count_uncommented
= 0;
3051 const gchar
*co
, *cc
;
3052 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3053 gboolean first_line_was_comment
= FALSE
;
3054 gboolean last_line_was_comment
= FALSE
;
3056 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
3059 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3061 sel_start
= sci_get_selection_start(editor
->sci
);
3062 sel_end
= sci_get_selection_end(editor
->sci
);
3064 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3065 /* Find the last line with chars selected (not EOL char) */
3066 last_line
= sci_get_line_from_position(editor
->sci
,
3067 sel_end
- editor_get_eol_char_len(editor
));
3068 last_line
= MAX(first_line
, last_line
);
3070 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3071 last_line_start
= sci_get_position_from_line(editor
->sci
, last_line
);
3073 ft
= editor_get_filetype_at_line(editor
, first_line
);
3075 if (! filetype_get_comment_open_close(ft
, TRUE
, &co
, &cc
))
3078 co_len
= strlen(co
);
3082 sci_start_undo_action(editor
->sci
);
3084 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3088 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3089 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3092 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3095 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3096 sel
[buf_len
] = '\0';
3098 while (isspace(sel
[x
])) x
++;
3100 /* use single line comment */
3103 gboolean do_continue
= FALSE
;
3106 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
3107 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
3112 if (do_continue
&& i
== first_line
)
3113 first_line_was_comment
= TRUE
;
3114 last_line_was_comment
= do_continue
;
3118 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
3122 /* we are still here, so the above lines were not already comments, so comment it */
3123 editor_do_comment(editor
, i
, TRUE
, TRUE
, TRUE
);
3126 /* use multi line comment */
3131 /* skip lines which are already comments */
3132 style_comment
= get_multiline_comment_style(editor
, line_start
);
3133 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3135 if (real_uncomment_multiline(editor
))
3136 count_uncommented
++;
3140 real_comment_multiline(editor
, line_start
, last_line
);
3144 /* break because we are already on the last line */
3150 sci_end_undo_action(editor
->sci
);
3154 /* restore selection or caret position */
3157 gint a
= (first_line_was_comment
) ? - (gint
) co_len
: (gint
) co_len
;
3160 /* don't modify sel_start when the selection starts within indentation */
3161 read_indent(editor
, sel_start
);
3162 indent_len
= (gint
) strlen(indent
);
3163 if ((sel_start
- first_line_start
) <= indent_len
)
3165 /* if the selection start was inside the comment mark, adjust the position */
3166 else if (first_line_was_comment
&&
3167 sel_start
>= (first_line_start
+ indent_len
) &&
3168 sel_start
<= (first_line_start
+ indent_len
+ (gint
) co_len
))
3170 a
= (first_line_start
+ indent_len
) - sel_start
;
3173 if (sel_start
< sel_end
)
3175 gint b
= (count_commented
* (gint
) co_len
) - (count_uncommented
* (gint
) co_len
);
3177 /* same for selection end, but here we add an offset on the offset above */
3178 read_indent(editor
, sel_end
+ b
);
3179 indent_len
= (gint
) strlen(indent
);
3180 if ((sel_end
- last_line_start
) < indent_len
)
3181 b
+= last_line_was_comment
? (gint
) co_len
: -(gint
) co_len
;
3182 else if (last_line_was_comment
&&
3183 sel_end
>= (last_line_start
+ indent_len
) &&
3184 sel_end
<= (last_line_start
+ indent_len
+ (gint
) co_len
))
3186 b
+= (gint
) co_len
- (sel_end
- (last_line_start
+ indent_len
));
3189 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
3190 sci_set_selection_end(editor
->sci
, sel_end
+ b
);
3193 sci_set_current_position(editor
->sci
, sel_start
+ a
, TRUE
);
3197 gint eol_len
= editor_get_eol_char_len(editor
);
3198 if (count_uncommented
> 0)
3200 sci_set_selection_start(editor
->sci
, sel_start
- (gint
) co_len
+ eol_len
);
3201 sci_set_selection_end(editor
->sci
, sel_end
- (gint
) co_len
+ eol_len
);
3203 else if (count_commented
> 0)
3205 sci_set_selection_start(editor
->sci
, sel_start
+ (gint
) co_len
- eol_len
);
3206 sci_set_selection_end(editor
->sci
, sel_end
+ (gint
) co_len
- eol_len
);
3208 if (sel_start
>= sel_end
)
3209 sci_scroll_caret(editor
->sci
);
3214 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
3215 void editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
,
3216 gboolean single_comment
)
3218 gint first_line
, last_line
;
3219 gint x
, i
, line_start
, line_len
;
3220 gint sel_start
, sel_end
, co_len
;
3222 const gchar
*co
, *cc
;
3223 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3226 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3229 { /* use selection or current line */
3230 sel_start
= sci_get_selection_start(editor
->sci
);
3231 sel_end
= sci_get_selection_end(editor
->sci
);
3233 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3234 /* Find the last line with chars selected (not EOL char) */
3235 last_line
= sci_get_line_from_position(editor
->sci
,
3236 sel_end
- editor_get_eol_char_len(editor
));
3237 last_line
= MAX(first_line
, last_line
);
3241 first_line
= last_line
= line
;
3242 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3245 ft
= editor_get_filetype_at_line(editor
, first_line
);
3247 if (! filetype_get_comment_open_close(ft
, single_comment
, &co
, &cc
))
3250 co_len
= strlen(co
);
3254 sci_start_undo_action(editor
->sci
);
3256 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3260 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3261 line_len
= sci_get_line_end_position(editor
->sci
, i
) - line_start
;
3264 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
);
3267 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3268 sel
[buf_len
] = '\0';
3270 while (isspace(sel
[x
])) x
++;
3272 /* to skip blank lines */
3273 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3275 /* use single line comment */
3278 gint start
= line_start
;
3281 if (ft
->comment_use_indent
)
3282 start
= line_start
+ x
;
3286 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3287 sci_insert_text(editor
->sci
, start
, text
);
3291 sci_insert_text(editor
->sci
, start
, co
);
3293 /* use multi line comment */
3298 /* skip lines which are already comments */
3299 style_comment
= get_multiline_comment_style(editor
, line_start
);
3300 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3303 real_comment_multiline(editor
, line_start
, last_line
);
3305 /* break because we are already on the last line */
3311 sci_end_undo_action(editor
->sci
);
3313 /* restore selection if there is one
3314 * but don't touch the selection if caller is editor_do_comment_toggle */
3315 if (! toggle
&& sel_start
< sel_end
)
3319 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3320 sci_set_selection_end(editor
->sci
, sel_end
+ ((i
- first_line
) * co_len
));
3324 gint eol_len
= editor_get_eol_char_len(editor
);
3325 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3326 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3332 static gboolean brace_timeout_active
= FALSE
;
3334 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3336 GeanyDocument
*doc
= document_get_current();
3337 GeanyEditor
*editor
;
3338 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3339 gint end_pos
, cur_pos
;
3341 brace_timeout_active
= FALSE
;
3345 editor
= doc
->editor
;
3346 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3348 if (cur_pos
!= brace_pos
)
3351 if (cur_pos
!= brace_pos
)
3353 /* we have moved past the original brace_pos, but after the timeout
3354 * we may now be on a new brace, so check again */
3355 editor_highlight_braces(editor
, cur_pos
);
3359 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3361 editor_highlight_braces(editor
, cur_pos
);
3364 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3368 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3369 sci_get_col_from_position(editor
->sci
, end_pos
));
3370 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3371 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3375 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3376 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3382 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3384 gint brace_pos
= cur_pos
- 1;
3386 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3387 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3389 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3392 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3397 if (!brace_timeout_active
)
3399 brace_timeout_active
= TRUE
;
3400 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3401 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3406 static gboolean
in_block_comment(gint lexer
, gint style
)
3412 return (style
== SCE_C_COMMENT
||
3413 style
== SCE_C_COMMENTDOC
);
3416 return (style
== SCE_PAS_COMMENT
||
3417 style
== SCE_PAS_COMMENT2
);
3420 return (style
== SCE_D_COMMENT
||
3421 style
== SCE_D_COMMENTDOC
||
3422 style
== SCE_D_COMMENTNESTED
);
3425 return (style
== SCE_HPHP_COMMENT
);
3428 return (style
== SCE_CSS_COMMENT
);
3436 static gboolean
is_comment_char(gchar c
, gint lexer
)
3438 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3448 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3450 ScintillaObject
*sci
= editor
->sci
;
3451 gint indent_pos
, style
;
3452 gint lexer
= sci_get_lexer(sci
);
3454 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3455 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3456 style
= sci_get_style_at(sci
, indent_pos
);
3457 if (!in_block_comment(lexer
, style
))
3460 /* Check whether the comment block continues on this line */
3461 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3462 if (sci_get_style_at(sci
, indent_pos
) == style
|| indent_pos
>= sci_get_length(sci
))
3464 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3465 /* the type of comment, '*' (C/C++/Java), '+' and the others (D) */
3466 const gchar
*continuation
= "*";
3467 const gchar
*whitespace
= ""; /* to hold whitespace if needed */
3469 gint len
= strlen(previous_line
);
3472 /* find and stop at end of multi line comment */
3474 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3475 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3477 gint indent_len
, indent_width
;
3479 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3480 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3481 indent_width
= editor_get_indent_prefs(editor
)->width
;
3483 /* if there is one too many spaces, delete the last space,
3484 * to return to the indent used before the multiline comment was started. */
3485 if (indent_len
% indent_width
== 1)
3486 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3487 g_free(previous_line
);
3490 /* check whether we are on the second line of multi line comment */
3492 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3495 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3496 { /* we are on the second line of a multi line comment, so we have to insert white space */
3500 if (style
== SCE_D_COMMENTNESTED
)
3501 continuation
= "+"; /* for nested comments in D */
3503 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3504 sci_add_text(sci
, result
);
3507 g_free(previous_line
);
3513 static gboolean
editor_lexer_is_c_like(gint lexer
)
3528 /* inserts a three-line comment at one line above current cursor position */
3529 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3535 gboolean have_multiline_comment
= FALSE
;
3537 const gchar
*co
, *cc
;
3539 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3541 if (! filetype_get_comment_open_close(editor
->document
->file_type
, FALSE
, &co
, &cc
))
3542 g_return_if_reached();
3544 have_multiline_comment
= TRUE
;
3546 sci_start_undo_action(editor
->sci
);
3548 doc
= editor
->document
;
3550 /* insert three lines one line above of the current position */
3551 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3552 pos
= sci_get_position_from_line(editor
->sci
, line
);
3554 /* use the indent on the current line but only when comment indentation is used
3555 * and we don't have multi line comment characters */
3556 if (editor
->auto_indent
&&
3557 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3559 read_indent(editor
, editor_info
.click_pos
);
3560 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3561 text_len
= strlen(text
);
3565 text
= g_strdup("\n\n\n");
3568 sci_insert_text(editor
->sci
, pos
, text
);
3571 /* select the inserted lines for commenting */
3572 sci_set_selection_start(editor
->sci
, pos
);
3573 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3575 editor_do_comment(editor
, -1, TRUE
, FALSE
, FALSE
);
3577 /* set the current position to the start of the first inserted line */
3580 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3581 if (have_multiline_comment
)
3584 pos
+= strlen(indent
);
3586 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3587 /* reset the selection */
3588 sci_set_anchor(editor
->sci
, pos
);
3590 sci_end_undo_action(editor
->sci
);
3594 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3595 * Scroll the view to make line appear at percent_of_view.
3596 * line can be -1 to use the current position. */
3597 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3602 g_return_if_fail(editor
!= NULL
);
3604 wid
= GTK_WIDGET(editor
->sci
);
3606 if (! gtk_widget_get_window(wid
) || ! gdk_window_is_viewable(gtk_widget_get_window(wid
)))
3607 return; /* prevent gdk_window_scroll warning */
3610 line
= sci_get_current_line(editor
->sci
);
3612 /* sci 'visible line' != doc line number because of folding and line wrapping */
3613 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3614 * SCI_DOCLINEFROMVISIBLE for vis1. */
3615 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3616 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3617 line
= line
- los
* percent_of_view
;
3618 SSM(editor
->sci
, SCI_SETFIRSTVISIBLELINE
, line
, 0);
3619 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3623 /* creates and inserts one tab or whitespace of the amount of the tab width */
3624 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3627 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3629 g_return_if_fail(editor
!= NULL
);
3631 switch (iprefs
.type
)
3633 case GEANY_INDENT_TYPE_TABS
:
3634 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3636 case GEANY_INDENT_TYPE_SPACES
:
3637 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3638 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3641 text
= get_whitespace(&iprefs
, iprefs
.width
);
3642 sci_add_text(editor
->sci
, text
);
3647 void editor_select_word(GeanyEditor
*editor
)
3653 g_return_if_fail(editor
!= NULL
);
3655 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3656 start
= SSM(editor
->sci
, SCI_WORDSTARTPOSITION
, pos
, TRUE
);
3657 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, TRUE
);
3659 if (start
== end
) /* caret in whitespaces sequence */
3661 /* look forward but reverse the selection direction,
3662 * so the caret end up stay as near as the original position. */
3663 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, FALSE
);
3664 start
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, end
, TRUE
);
3669 sci_set_selection(editor
->sci
, start
, end
);
3673 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3674 * when those lines have no selection (cursor at start of line). */
3675 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3677 gint start
, end
, line
;
3679 g_return_if_fail(editor
!= NULL
);
3681 start
= sci_get_selection_start(editor
->sci
);
3682 end
= sci_get_selection_end(editor
->sci
);
3684 /* check if whole lines are already selected */
3685 if (! extra_line
&& start
!= end
&&
3686 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3687 sci_get_col_from_position(editor
->sci
, end
) == 0)
3690 line
= sci_get_line_from_position(editor
->sci
, start
);
3691 start
= sci_get_position_from_line(editor
->sci
, line
);
3693 line
= sci_get_line_from_position(editor
->sci
, end
);
3694 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3696 sci_set_selection(editor
->sci
, start
, end
);
3700 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
)
3702 return sci_get_line_indent_position(sci
, line
) ==
3703 sci_get_line_end_position(sci
, line
);
3707 /* Returns first line of paragraph for GTK_DIR_UP, line after paragraph
3708 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3709 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3712 ScintillaObject
*sci
= editor
->sci
;
3714 /* first check current line and return -1 if it is empty to skip creating of a selection */
3715 if (sci_is_blank_line(sci
, line
))
3718 if (direction
== GTK_DIR_UP
)
3728 /* start of document */
3732 if (line
== sci_get_line_count(sci
))
3735 if (sci_is_blank_line(sci
, line
))
3737 /* return line paragraph starts on */
3738 if (direction
== GTK_DIR_UP
)
3747 void editor_select_paragraph(GeanyEditor
*editor
)
3749 gint pos_start
, pos_end
, line_start
, line_found
;
3751 g_return_if_fail(editor
!= NULL
);
3753 line_start
= sci_get_current_line(editor
->sci
);
3755 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3756 if (line_found
== -1)
3759 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3761 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3762 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3764 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3768 /* Returns first line of block for GTK_DIR_UP, line after block
3769 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3770 static gint
find_block_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3773 ScintillaObject
*sci
= editor
->sci
;
3775 /* first check current line and return -1 if it is empty to skip creating of a selection */
3776 if (sci_is_blank_line(sci
, line
))
3779 if (direction
== GTK_DIR_UP
)
3784 ind
= sci_get_line_indentation(sci
, line
);
3790 /* start of document */
3794 if (line
== sci_get_line_count(sci
))
3797 if (sci_get_line_indentation(sci
, line
) != ind
||
3798 sci_is_blank_line(sci
, line
))
3800 /* return line block starts on */
3801 if (direction
== GTK_DIR_UP
)
3810 void editor_select_indent_block(GeanyEditor
*editor
)
3812 gint pos_start
, pos_end
, line_start
, line_found
;
3814 g_return_if_fail(editor
!= NULL
);
3816 line_start
= sci_get_current_line(editor
->sci
);
3818 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_UP
);
3819 if (line_found
== -1)
3822 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3824 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_DOWN
);
3825 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3827 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3831 /* simple indentation to indent the current line with the same indent as the previous one */
3832 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
3834 gint i
, sel_start
= 0, sel_end
= 0;
3836 /* get previous line and use it for read_indent to use that line
3837 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
3838 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
3840 for (i
= first_line
; i
<= last_line
; i
++)
3842 /* skip the first line or if the indentation of the previous and current line are equal */
3844 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
3845 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
3848 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3849 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3850 if (sel_start
< sel_end
)
3852 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
3853 sci_replace_sel(editor
->sci
, "");
3855 sci_insert_text(editor
->sci
, sel_start
, indent
);
3860 /* simple indentation to indent the current line with the same indent as the previous one */
3861 void editor_smart_line_indentation(GeanyEditor
*editor
, gint pos
)
3863 gint first_line
, last_line
;
3864 gint first_sel_start
, first_sel_end
;
3865 ScintillaObject
*sci
;
3867 g_return_if_fail(editor
!= NULL
);
3871 first_sel_start
= sci_get_selection_start(sci
);
3872 first_sel_end
= sci_get_selection_end(sci
);
3874 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
3875 /* Find the last line with chars selected (not EOL char) */
3876 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
3877 last_line
= MAX(first_line
, last_line
);
3880 pos
= first_sel_start
;
3882 sci_start_undo_action(sci
);
3884 smart_line_indentation(editor
, first_line
, last_line
);
3886 /* set cursor position if there was no selection */
3887 if (first_sel_start
== first_sel_end
)
3889 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
3891 /* use indent position as user may wish to change indentation afterwards */
3892 sci_set_current_position(sci
, indent_pos
, FALSE
);
3896 /* fully select all the lines affected */
3897 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
3898 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
3901 sci_end_undo_action(sci
);
3905 /* increase / decrease current line or selection by one space */
3906 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
3908 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
3909 gint sel_start
, sel_end
, first_line_offset
= 0;
3911 g_return_if_fail(editor
!= NULL
);
3913 sel_start
= sci_get_selection_start(editor
->sci
);
3914 sel_end
= sci_get_selection_end(editor
->sci
);
3916 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3917 /* Find the last line with chars selected (not EOL char) */
3918 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
3919 last_line
= MAX(first_line
, last_line
);
3924 sci_start_undo_action(editor
->sci
);
3926 for (i
= first_line
; i
<= last_line
; i
++)
3928 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3931 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3932 /* searching backwards for a space to remove */
3933 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
3936 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
3938 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
3939 sci_replace_sel(editor
->sci
, "");
3941 if (i
== first_line
)
3942 first_line_offset
= -1;
3947 sci_insert_text(editor
->sci
, indentation_end
, " ");
3949 if (i
== first_line
)
3950 first_line_offset
= 1;
3954 /* set cursor position */
3955 if (sel_start
< sel_end
)
3957 gint start
= sel_start
+ first_line_offset
;
3958 if (first_line_offset
< 0)
3959 start
= MAX(sel_start
+ first_line_offset
,
3960 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
3962 sci_set_selection_start(editor
->sci
, start
);
3963 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
3966 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
3968 sci_end_undo_action(editor
->sci
);
3972 void editor_finalize()
3974 scintilla_release_resources();
3978 /* wordchars: NULL or a string containing characters to match a word.
3979 * Returns: the current selection or the current word.
3981 * Passing NULL as wordchars is NOT the same as passing GEANY_WORDCHARS: NULL means
3982 * using Scintillas's word boundaries. */
3983 gchar
*editor_get_default_selection(GeanyEditor
*editor
, gboolean use_current_word
,
3984 const gchar
*wordchars
)
3988 g_return_val_if_fail(editor
!= NULL
, NULL
);
3990 if (sci_get_lines_selected(editor
->sci
) == 1)
3991 s
= sci_get_selection_contents(editor
->sci
);
3992 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
3993 { /* use the word at current cursor position */
3994 gchar word
[GEANY_MAX_WORD_LENGTH
];
3996 if (wordchars
!= NULL
)
3997 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
3999 editor_find_current_word_sciwc(editor
, -1, word
, sizeof(word
));
4001 if (word
[0] != '\0')
4008 /* Note: Usually the line should be made visible (not folded) before calling this.
4009 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4010 * outside the *vertical* view.
4011 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4012 * sci_scroll_caret() when this returns TRUE. */
4013 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4017 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4019 /* If line is wrapped the result may occur on another virtual line than the first and may be
4020 * still hidden, so increase the line number to check for the next document line */
4021 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4024 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4025 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4026 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4028 return (line
>= vis1
&& line
< vis1
+ los
);
4032 /* If the current line is outside the current view window, scroll the line
4033 * so it appears at percent_of_view. */
4034 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4038 g_return_if_fail(editor
!= NULL
);
4040 line
= sci_get_current_line(editor
->sci
);
4042 /* unfold maybe folded results */
4043 sci_ensure_line_is_visible(editor
->sci
, line
);
4045 /* scroll the line if it's off screen */
4046 if (! editor_line_in_view(editor
, line
))
4047 editor
->scroll_percent
= percent_of_view
;
4049 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4054 * Deletes all currently set indicators in the @a editor window.
4055 * Error indicators (red squiggly underlines) and usual line markers are removed.
4057 * @param editor The editor to operate on.
4059 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4061 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4062 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4067 * Deletes all currently set indicators matching @a indic in the @a editor window.
4069 * @param editor The editor to operate on.
4070 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4074 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4078 g_return_if_fail(editor
!= NULL
);
4080 last_pos
= sci_get_length(editor
->sci
);
4083 sci_indicator_set(editor
->sci
, indic
);
4084 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4090 * Sets an indicator @a indic on @a line.
4091 * Whitespace at the start and the end of the line is not marked.
4093 * @param editor The editor to operate on.
4094 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4095 * @param line The line number which should be marked.
4099 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4105 g_return_if_fail(editor
!= NULL
);
4106 g_return_if_fail(line
>= 0);
4108 start
= sci_get_position_from_line(editor
->sci
, line
);
4109 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4111 /* skip blank lines */
4112 if ((start
+ 1) == end
||
4114 (sci_get_line_end_position(editor
->sci
, line
) - start
) == 0)
4120 linebuf
= sci_get_line(editor
->sci
, line
);
4122 /* don't set the indicator on whitespace */
4123 while (isspace(linebuf
[i
]))
4125 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4132 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4137 * Sets an indicator on the range specified by @a start and @a end.
4138 * No error checking or whitespace removal is performed, this should be done by the calling
4139 * function if necessary.
4141 * @param editor The editor to operate on.
4142 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4143 * @param start The starting position for the marker.
4144 * @param end The ending position for the marker.
4148 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4150 g_return_if_fail(editor
!= NULL
);
4154 sci_indicator_set(editor
->sci
, indic
);
4155 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4159 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4160 * the replacement will also start with 0x... */
4161 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4163 g_return_if_fail(editor
!= NULL
);
4165 if (sci_has_selection(editor
->sci
))
4167 gint start
= sci_get_selection_start(editor
->sci
);
4168 const gchar
*replacement
= colour
;
4170 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4171 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4173 sci_set_selection_start(editor
->sci
, start
+ 2);
4174 sci_set_selection_end(editor
->sci
, start
+ 8);
4175 replacement
++; /* skip the leading "0x" */
4177 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4178 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4179 replacement
++; /* so skip the '#' to only replace the colour value */
4181 sci_replace_sel(editor
->sci
, replacement
);
4184 sci_add_text(editor
->sci
, colour
);
4189 * Retrieves the end of line characters mode (LF, CR/LF, CR) in the given editor.
4190 * If @a editor is @c NULL, the default end of line characters are used.
4192 * @param editor The editor to operate on, or @c NULL to query the default value.
4193 * @return The used end of line characters mode.
4197 gint
editor_get_eol_char_mode(GeanyEditor
*editor
)
4199 gint mode
= file_prefs
.default_eol_character
;
4202 mode
= sci_get_eol_mode(editor
->sci
);
4209 * Retrieves the localized name (for displaying) of the used end of line characters
4210 * (LF, CR/LF, CR) in the given editor.
4211 * If @a editor is @c NULL, the default end of line characters are used.
4213 * @param editor The editor to operate on, or @c NULL to query the default value.
4214 * @return The name of the end of line characters.
4218 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4220 gint mode
= file_prefs
.default_eol_character
;
4223 mode
= sci_get_eol_mode(editor
->sci
);
4225 return utils_get_eol_name(mode
);
4230 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4231 * If @a editor is @c NULL, the default end of line characters are used.
4232 * The returned value is 1 for CR and LF and 2 for CR/LF.
4234 * @param editor The editor to operate on, or @c NULL to query the default value.
4235 * @return The length of the end of line characters.
4239 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4241 gint mode
= file_prefs
.default_eol_character
;
4244 mode
= sci_get_eol_mode(editor
->sci
);
4248 case SC_EOL_CRLF
: return 2; break;
4249 default: return 1; break;
4255 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4256 * If @a editor is @c NULL, the default end of line characters are used.
4257 * The returned value is either "\n", "\r\n" or "\r".
4259 * @param editor The editor to operate on, or @c NULL to query the default value.
4260 * @return The end of line characters.
4264 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4266 gint mode
= file_prefs
.default_eol_character
;
4269 mode
= sci_get_eol_mode(editor
->sci
);
4271 return utils_get_eol_char(mode
);
4275 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4277 gint lines
, first
, i
;
4279 if (editor
== NULL
|| ! editor_prefs
.folding
)
4282 lines
= sci_get_line_count(editor
->sci
);
4283 first
= sci_get_first_visible_line(editor
->sci
);
4285 for (i
= 0; i
< lines
; i
++)
4287 gint level
= sci_get_fold_level(editor
->sci
, i
);
4289 if (level
& SC_FOLDLEVELHEADERFLAG
)
4291 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4292 sci_toggle_fold(editor
->sci
, i
);
4295 editor_scroll_to_line(editor
, first
, 0.0F
);
4299 void editor_unfold_all(GeanyEditor
*editor
)
4301 fold_all(editor
, FALSE
);
4305 void editor_fold_all(GeanyEditor
*editor
)
4307 fold_all(editor
, TRUE
);
4311 void editor_replace_tabs(GeanyEditor
*editor
)
4313 gint search_pos
, pos_in_line
, current_tab_true_length
;
4316 struct Sci_TextToFind ttf
;
4318 g_return_if_fail(editor
!= NULL
);
4320 sci_start_undo_action(editor
->sci
);
4321 tab_len
= sci_get_tab_width(editor
->sci
);
4323 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4324 ttf
.lpstrText
= (gchar
*) "\t";
4328 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4329 if (search_pos
== -1)
4332 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4333 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4334 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4335 sci_set_target_start(editor
->sci
, search_pos
);
4336 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4337 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4338 /* next search starts after replacement */
4339 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4340 /* update end of range now text has changed */
4341 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4344 sci_end_undo_action(editor
->sci
);
4348 /* Replaces all occurrences all spaces of the length of a given tab_width. */
4349 void editor_replace_spaces(GeanyEditor
*editor
)
4352 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4354 struct Sci_TextToFind ttf
;
4356 g_return_if_fail(editor
!= NULL
);
4358 if (tab_len_f
< 0.0)
4359 tab_len_f
= sci_get_tab_width(editor
->sci
);
4361 if (! dialogs_show_input_numeric(
4362 _("Enter Tab Width"),
4363 _("Enter the amount of spaces which should be replaced by a tab character."),
4364 &tab_len_f
, 1, 100, 1))
4368 tab_len
= (gint
) tab_len_f
;
4370 sci_start_undo_action(editor
->sci
);
4372 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4373 ttf
.lpstrText
= g_strnfill(tab_len
, ' ');
4377 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4378 if (search_pos
== -1)
4380 /* only replace indentation because otherwise we can mess up alignment */
4381 if (search_pos
> sci_get_line_indent_position(editor
->sci
,
4382 sci_get_line_from_position(editor
->sci
, search_pos
)))
4384 ttf
.chrg
.cpMin
= search_pos
+ tab_len
;
4387 sci_set_target_start(editor
->sci
, search_pos
);
4388 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4389 sci_replace_target(editor
->sci
, "\t", FALSE
);
4390 ttf
.chrg
.cpMin
= search_pos
;
4391 /* update end of range now text has changed */
4392 ttf
.chrg
.cpMax
-= tab_len
- 1;
4394 sci_end_undo_action(editor
->sci
);
4395 g_free(ttf
.lpstrText
);
4399 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4401 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4402 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4403 gint i
= line_end
- 1;
4404 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4406 /* Diff hunks should keep trailing spaces */
4407 if (sci_get_lexer(editor
->sci
) == SCLEX_DIFF
)
4410 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4413 ch
= sci_get_char_at(editor
->sci
, i
);
4415 if (i
< (line_end
- 1))
4417 sci_set_target_start(editor
->sci
, i
+ 1);
4418 sci_set_target_end(editor
->sci
, line_end
);
4419 sci_replace_target(editor
->sci
, "", FALSE
);
4424 void editor_strip_trailing_spaces(GeanyEditor
*editor
)
4426 gint max_lines
= sci_get_line_count(editor
->sci
);
4429 sci_start_undo_action(editor
->sci
);
4431 for (line
= 0; line
< max_lines
; line
++)
4433 editor_strip_line_trailing_spaces(editor
, line
);
4435 sci_end_undo_action(editor
->sci
);
4439 void editor_ensure_final_newline(GeanyEditor
*editor
)
4441 gint max_lines
= sci_get_line_count(editor
->sci
);
4442 gboolean append_newline
= (max_lines
== 1);
4443 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4447 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4451 const gchar
*eol
= editor_get_eol_char(editor
);
4453 sci_insert_text(editor
->sci
, end_document
, eol
);
4458 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4462 PangoFontDescription
*pfd
;
4464 g_return_if_fail(editor
);
4466 pfd
= pango_font_description_from_string(font
);
4467 size
= pango_font_description_get_size(pfd
) / PANGO_SCALE
;
4468 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4469 pango_font_description_free(pfd
);
4471 for (style
= 0; style
<= STYLE_MAX
; style
++)
4472 sci_set_font(editor
->sci
, style
, font_name
, size
);
4476 /* zoom to 100% to prevent confusion */
4477 sci_zoom_off(editor
->sci
);
4481 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4483 g_return_if_fail(editor
!= NULL
);
4485 editor
->line_wrapping
= wrap
;
4486 sci_set_lines_wrapped(editor
->sci
, wrap
);
4490 /** Sets the indent type for @a editor.
4491 * @param editor Editor.
4492 * @param type Indent type.
4496 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4498 editor_set_indent(editor
, type
, editor
->indent_width
);
4502 void editor_set_indent_width(GeanyEditor
*editor
, gint width
)
4504 editor_set_indent(editor
, editor
->indent_type
, width
);
4508 void editor_set_indent(GeanyEditor
*editor
, GeanyIndentType type
, gint width
)
4510 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4511 ScintillaObject
*sci
= editor
->sci
;
4512 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4514 editor
->indent_type
= type
;
4515 editor
->indent_width
= width
;
4516 sci_set_use_tabs(sci
, use_tabs
);
4518 if (type
== GEANY_INDENT_TYPE_BOTH
)
4520 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4521 if (iprefs
->hard_tab_width
!= 8)
4523 static gboolean warn
= TRUE
;
4525 ui_set_statusbar(TRUE
, _("Warning: non-standard hard tab width: %d != 8!"),
4526 iprefs
->hard_tab_width
);
4531 sci_set_tab_width(sci
, width
);
4533 SSM(sci
, SCI_SETINDENT
, width
, 0);
4535 /* remove indent spaces on backspace, if using any spaces to indent */
4536 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, type
!= GEANY_INDENT_TYPE_TABS
, 0);
4540 /* Convenience function for editor_goto_pos() to pass in a line number. */
4541 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line_no
, gint offset
)
4545 g_return_val_if_fail(editor
, FALSE
);
4546 if (line_no
< 0 || line_no
>= sci_get_line_count(editor
->sci
))
4551 gint current_line
= sci_get_current_line(editor
->sci
);
4553 line_no
= current_line
+ line_no
;
4556 pos
= sci_get_position_from_line(editor
->sci
, line_no
);
4557 return editor_goto_pos(editor
, pos
, TRUE
);
4561 /** Moves to position @a pos, switching to the document if necessary,
4562 * setting a marker if @a mark is set.
4564 * @param editor Editor.
4565 * @param pos The position.
4566 * @param mark Whether to set a mark on the position.
4567 * @return @c TRUE if action has been performed, otherwise @c FALSE.
4571 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4573 g_return_val_if_fail(editor
, FALSE
);
4574 if (G_UNLIKELY(pos
< 0))
4579 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4581 /* mark the tag with the yellow arrow */
4582 sci_marker_delete_all(editor
->sci
, 0);
4583 sci_set_marker_at_line(editor
->sci
, line
, 0);
4586 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4587 editor
->scroll_percent
= 0.25F
;
4589 /* finally switch to the page */
4590 document_show_tab(editor
->document
);
4596 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4598 GeanyEditor
*editor
= user_data
;
4600 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4601 * few lines only, maybe this could/should be done in Scintilla directly */
4602 if (event
->state
& GDK_MOD1_MASK
)
4604 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4607 else if (event
->state
& GDK_SHIFT_MASK
)
4609 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4611 sci_scroll_columns(editor
->sci
, amount
);
4615 return FALSE
; /* let Scintilla handle all other cases */
4619 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4621 GeanyDocument
*doc
= editor
->document
;
4623 if (!doc
->priv
->colourise_needed
)
4626 doc
->priv
->colourise_needed
= FALSE
;
4627 sci_colourise(editor
->sci
, 0, -1);
4629 /* now that the current document is colourised, fold points are now accurate,
4630 * so force an update of the current function/tag. */
4631 symbols_get_current_function(NULL
, NULL
);
4632 ui_update_statusbar(NULL
, -1);
4638 /* We only want to colourise just before drawing, to save startup time and
4639 * prevent unnecessary recolouring other documents after one is saved.
4640 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4641 * and "show" doesn't work). */
4642 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4644 GeanyEditor
*editor
= user_data
;
4646 editor_check_colourise(editor
);
4651 static gboolean
on_editor_expose_event(GtkWidget
*widget
, GdkEventExpose
*event
,
4654 GeanyEditor
*editor
= user_data
;
4656 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4657 * for some reason, maybe it's not necessary but just in case. */
4658 editor_check_colourise(editor
);
4663 #if GTK_CHECK_VERSION(3, 0, 0)
4664 static gboolean
on_editor_draw(GtkWidget
*widget
, cairo_t
*cr
, gpointer user_data
)
4666 return on_editor_expose_event(widget
, NULL
, user_data
);
4671 static void setup_sci_keys(ScintillaObject
*sci
)
4673 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4674 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4675 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4676 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4677 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4678 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4679 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4680 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4681 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4682 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4683 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4684 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4685 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4686 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4687 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4689 if (editor_prefs
.use_gtk_word_boundaries
)
4691 /* use GtkEntry-like word boundaries */
4692 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4693 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4694 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4696 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4697 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4698 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4699 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4700 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4701 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4703 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4707 /* registers a Scintilla image from a named icon from the theme */
4708 static gboolean
register_named_icon(ScintillaObject
*sci
, guint id
, const gchar
*name
)
4710 GError
*error
= NULL
;
4712 gint n_channels
, rowstride
, width
, height
;
4715 gtk_icon_size_lookup(GTK_ICON_SIZE_MENU
, &size
, NULL
);
4716 pixbuf
= gtk_icon_theme_load_icon(gtk_icon_theme_get_default(), name
, size
, 0, &error
);
4719 g_warning("failed to load icon '%s': %s", name
, error
->message
);
4720 g_error_free(error
);
4724 n_channels
= gdk_pixbuf_get_n_channels(pixbuf
);
4725 rowstride
= gdk_pixbuf_get_rowstride(pixbuf
);
4726 width
= gdk_pixbuf_get_width(pixbuf
);
4727 height
= gdk_pixbuf_get_height(pixbuf
);
4729 if (gdk_pixbuf_get_bits_per_sample(pixbuf
) != 8 ||
4730 ! gdk_pixbuf_get_has_alpha(pixbuf
) ||
4732 rowstride
!= width
* n_channels
)
4734 g_warning("incompatible image data for icon '%s'", name
);
4735 g_object_unref(pixbuf
);
4739 SSM(sci
, SCI_RGBAIMAGESETWIDTH
, width
, 0);
4740 SSM(sci
, SCI_RGBAIMAGESETHEIGHT
, height
, 0);
4741 SSM(sci
, SCI_REGISTERRGBAIMAGE
, id
, (sptr_t
)gdk_pixbuf_get_pixels(pixbuf
));
4743 g_object_unref(pixbuf
);
4748 /* Create new editor widget (scintilla).
4749 * @note The @c "sci-notify" signal is connected separately. */
4750 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4752 ScintillaObject
*sci
;
4754 sci
= SCINTILLA(scintilla_new());
4756 /* Scintilla doesn't support RTL languages properly and is primarily
4757 * intended to be used with LTR source code, so override the
4758 * GTK+ default text direction for the Scintilla widget. */
4759 gtk_widget_set_direction(GTK_WIDGET(sci
), GTK_TEXT_DIR_LTR
);
4761 gtk_widget_show(GTK_WIDGET(sci
));
4763 sci_set_codepage(sci
, SC_CP_UTF8
);
4764 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4765 /* disable scintilla provided popup menu */
4766 sci_use_popup(sci
, FALSE
);
4768 setup_sci_keys(sci
);
4770 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4771 sci_set_lines_wrapped(sci
, editor_prefs
.line_wrapping
);
4772 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4773 /*sci_set_caret_policy_y(sci, CARET_JUMPS | CARET_EVEN, 0);*/
4774 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4775 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4777 /* tag autocompletion images */
4778 register_named_icon(sci
, 1, "classviewer-var");
4779 register_named_icon(sci
, 2, "classviewer-method");
4781 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4782 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4785 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4787 /* only connect signals if this is for the document notebook, not split window */
4788 if (editor
->sci
== NULL
)
4790 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4791 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4792 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4793 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4794 #if GTK_CHECK_VERSION(3, 0, 0)
4795 g_signal_connect(sci
, "draw", G_CALLBACK(on_editor_draw
), editor
);
4797 g_signal_connect(sci
, "expose-event", G_CALLBACK(on_editor_expose_event
), editor
);
4804 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4805 * @param editor Editor settings.
4806 * @return The new widget.
4810 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4812 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4813 ScintillaObject
*old
, *sci
;
4815 /* temporarily change editor to use the new sci widget */
4817 sci
= create_new_sci(editor
);
4820 editor_set_indent(editor
, iprefs
->type
, iprefs
->width
);
4821 editor_set_font(editor
, interface_prefs
.editor_font
);
4822 editor_apply_update_prefs(editor
);
4824 /* if editor already had a widget, restore it */
4831 GeanyEditor
*editor_create(GeanyDocument
*doc
)
4833 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4834 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
4836 editor
->document
= doc
;
4837 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
4839 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
4840 editor
->line_wrapping
= editor_prefs
.line_wrapping
;
4841 editor
->scroll_percent
= -1.0F
;
4842 editor
->line_breaking
= FALSE
;
4844 editor
->sci
= editor_create_widget(editor
);
4849 /* in case we need to free some fields in future */
4850 void editor_destroy(GeanyEditor
*editor
)
4856 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
4858 gchar
*f
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
4860 g_return_if_fail(NZV(doc
->real_path
));
4862 if (utils_str_equal(doc
->real_path
, f
))
4864 /* reload snippets */
4865 editor_snippets_free();
4866 editor_snippets_init();
4872 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
4876 g_return_val_if_fail(editor
, FALSE
);
4878 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
4881 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
4883 /* if no word part, complete normally */
4884 if (!check_partial_completion(editor
, entry
))
4885 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
4892 void editor_init(void)
4894 static GeanyIndentPrefs indent_prefs
;
4897 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
4898 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
4899 editor_prefs
.indentation
= &indent_prefs
;
4901 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
4902 * handler (on_editor_notify) is called */
4903 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
4905 f
= g_build_filename(app
->configdir
, "snippets.conf", NULL
);
4906 ui_add_config_file_menu_item(f
, NULL
, NULL
);
4908 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
4912 /* TODO: Should these be user-defined instead of hard-coded? */
4913 void editor_set_indentation_guides(GeanyEditor
*editor
)
4918 g_return_if_fail(editor
!= NULL
);
4920 if (! editor_prefs
.show_indent_guide
)
4922 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
4926 lexer
= sci_get_lexer(editor
->sci
);
4929 /* Lines added/removed are prefixed with +/- characters, so
4930 * those lines will not be shown with any indentation guides.
4931 * It can be distracting that only a few of lines in a diff/patch
4932 * file will show the guides. */
4937 /* These languages use indentation for control blocks; the "look forward" method works
4941 case SCLEX_MAKEFILE
:
4945 case SCLEX_PROPERTIES
:
4946 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
4948 mode
= SC_IV_LOOKFORWARD
;
4951 /* C-like (structured) languages benefit from the "look both" method */
4965 case SCLEX_FREEBASIC
:
4968 mode
= SC_IV_LOOKBOTH
;
4976 sci_set_indentation_guides(editor
->sci
, mode
);
4980 /* Apply just the prefs that can change in the Preferences dialog */
4981 void editor_apply_update_prefs(GeanyEditor
*editor
)
4983 ScintillaObject
*sci
;
4985 g_return_if_fail(editor
!= NULL
);
4987 if (main_status
.quitting
)
4992 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
4993 editor_get_long_line_column(), editor_prefs
.long_line_color
);
4995 /* update indent width, tab width */
4996 editor_set_indent(editor
, editor
->indent_type
, editor
->indent_width
);
4997 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
4999 sci_assign_cmdkey(sci
, SCK_HOME
| (SCMOD_SHIFT
<< 16),
5000 editor_prefs
.smart_home_key
? SCI_VCHOMEEXTEND
: SCI_HOMEEXTEND
);
5001 sci_assign_cmdkey(sci
, SCK_HOME
| ((SCMOD_SHIFT
| SCMOD_ALT
) << 16),
5002 editor_prefs
.smart_home_key
? SCI_VCHOMERECTEXTEND
: SCI_HOMERECTEXTEND
);
5004 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
5005 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
5007 editor_set_indentation_guides(editor
);
5009 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
5010 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
5011 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
5012 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
5014 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
5017 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
5019 /* (dis)allow scrolling past end of document */
5020 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
5022 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
5026 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
5027 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
5029 ScintillaObject
*sci
= editor
->sci
;
5030 gint pos
= sci_get_position_from_line(sci
, line
);
5034 sci_insert_text(sci
, pos
, "\t");
5038 if (sci_get_char_at(sci
, pos
) == '\t')
5040 sci_set_selection(sci
, pos
, pos
+ 1);
5041 sci_replace_sel(sci
, "");
5043 else /* remove spaces only if no tabs */
5045 gint width
= sci_get_line_indentation(sci
, line
);
5047 width
-= editor_get_indent_prefs(editor
)->width
;
5048 sci_set_line_indentation(sci
, line
, width
);
5054 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
5056 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
5057 ScintillaObject
*sci
= editor
->sci
;
5059 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
5060 change_tab_indentation(editor
, line
, increase
);
5063 gint width
= sci_get_line_indentation(sci
, line
);
5065 width
+= increase
? iprefs
->width
: -iprefs
->width
;
5066 sci_set_line_indentation(sci
, line
, width
);
5071 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
5073 ScintillaObject
*sci
= editor
->sci
;
5074 gint caret_pos
, caret_line
, caret_offset
, caret_indent_pos
, caret_line_len
;
5075 gint anchor_pos
, anchor_line
, anchor_offset
, anchor_indent_pos
, anchor_line_len
;
5077 /* backup information needed to restore caret and anchor */
5078 caret_pos
= sci_get_current_position(sci
);
5079 anchor_pos
= SSM(sci
, SCI_GETANCHOR
, 0, 0);
5080 caret_line
= sci_get_line_from_position(sci
, caret_pos
);
5081 anchor_line
= sci_get_line_from_position(sci
, anchor_pos
);
5082 caret_offset
= caret_pos
- sci_get_position_from_line(sci
, caret_line
);
5083 anchor_offset
= anchor_pos
- sci_get_position_from_line(sci
, anchor_line
);
5084 caret_indent_pos
= sci_get_line_indent_position(sci
, caret_line
);
5085 anchor_indent_pos
= sci_get_line_indent_position(sci
, anchor_line
);
5086 caret_line_len
= sci_get_line_length(sci
, caret_line
);
5087 anchor_line_len
= sci_get_line_length(sci
, anchor_line
);
5089 if (sci_get_lines_selected(sci
) <= 1)
5091 editor_change_line_indent(editor
, sci_get_current_line(sci
), increase
);
5096 gint line
, lstart
, lend
;
5098 editor_select_lines(editor
, FALSE
);
5099 start
= sci_get_selection_start(sci
);
5100 end
= sci_get_selection_end(sci
);
5101 lstart
= sci_get_line_from_position(sci
, start
);
5102 lend
= sci_get_line_from_position(sci
, end
);
5103 if (end
== sci_get_length(sci
))
5104 lend
++; /* for last line with text on it */
5106 sci_start_undo_action(sci
);
5107 for (line
= lstart
; line
< lend
; line
++)
5109 editor_change_line_indent(editor
, line
, increase
);
5111 sci_end_undo_action(sci
);
5114 /* restore caret and anchor position */
5115 if (caret_pos
>= caret_indent_pos
)
5116 caret_offset
+= sci_get_line_length(sci
, caret_line
) - caret_line_len
;
5117 if (anchor_pos
>= anchor_indent_pos
)
5118 anchor_offset
+= sci_get_line_length(sci
, anchor_line
) - anchor_line_len
;
5120 SSM(sci
, SCI_SETCURRENTPOS
, sci_get_position_from_line(sci
, caret_line
) + caret_offset
, 0);
5121 SSM(sci
, SCI_SETANCHOR
, sci_get_position_from_line(sci
, anchor_line
) + anchor_offset
, 0);
5125 /** Gets snippet by name.
5127 * If @a editor is passed, returns a snippet specific to the document filetype.
5128 * If @a editor is @c NULL, returns a snippet from the default set.
5130 * @param editor Editor or @c NULL.
5131 * @param snippet_name Snippet name.
5132 * @return snippet or @c NULL if it was not found. Must not be freed.
5134 const gchar
*editor_find_snippet(GeanyEditor
*editor
, const gchar
*snippet_name
)
5136 const gchar
*subhash_name
= editor
? editor
->document
->file_type
->name
: "Default";
5137 GHashTable
*subhash
= g_hash_table_lookup(snippet_hash
, subhash_name
);
5139 return subhash
? g_hash_table_lookup(subhash
, snippet_name
) : NULL
;
5143 /** Replaces all special sequences in @a snippet and inserts it at @a pos.
5144 * If you insert at the current position, consider calling @c sci_scroll_caret()
5145 * after this function.
5150 void editor_insert_snippet(GeanyEditor
*editor
, gint pos
, const gchar
*snippet
)
5154 pattern
= g_string_new(snippet
);
5155 snippets_make_replacements(editor
, pattern
);
5156 editor_insert_text_block(editor
, pattern
->str
, pos
, -1, -1, TRUE
);
5157 g_string_free(pattern
, TRUE
);