2 * editor.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2009-2011 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
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
27 * Editor-related functions for @ref GeanyEditor.
28 * Geany uses the Scintilla editing widget, and this file is mostly built around
29 * Scintilla's functionality.
32 /* Callbacks for the Scintilla widget (ScintillaObject).
33 * Most important is the sci-notify callback, handled in on_editor_notification().
34 * This includes auto-indentation, comments, auto-completion, calltips, etc.
35 * Also some general Scintilla-related functions.
42 #include <gdk/gdkkeysyms.h>
50 # include "gnuregex.h"
56 #include "documentprivate.h"
57 #include "filetypes.h"
58 #include "filetypesprivate.h"
59 #include "sciwrappers.h"
64 #include "callbacks.h"
65 #include "templates.h"
66 #include "keybindings.h"
68 #include "projectprivate.h"
70 #include "highlighting.h"
73 /* Note: use sciwrappers.h instead where possible.
74 * Do not use SSM in files unrelated to scintilla. */
75 #define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
78 static GHashTable
*snippet_hash
= NULL
;
79 static GQueue
*snippet_offsets
= NULL
;
80 static gint snippet_cursor_insert_pos
;
81 static GtkAccelGroup
*snippet_accel_group
= NULL
;
83 /* holds word under the mouse or keyboard cursor */
84 static gchar current_word
[GEANY_MAX_WORD_LENGTH
];
86 /* whether there is a tag list update pending */
87 static gboolean document_tags_update_pending
= FALSE
;
89 /* Initialised in keyfile.c. */
90 GeanyEditorPrefs editor_prefs
;
92 EditorInfo editor_info
= {current_word
, -1};
101 ScintillaObject
*sci
;
102 } calltip
= {NULL
, FALSE
, NULL
, 0, 0, NULL
};
104 static gchar indent
[100];
107 static void on_new_line_added(GeanyEditor
*editor
);
108 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
109 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
110 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
111 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
112 static void close_block(GeanyEditor
*editor
, gint pos
);
113 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
114 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
115 const gchar
*wc
, gboolean stem
);
116 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
);
117 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
);
118 static gssize
snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
,
122 void editor_snippets_free(void)
124 g_hash_table_destroy(snippet_hash
);
125 g_queue_free(snippet_offsets
);
126 gtk_window_remove_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
130 static void snippets_load(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
132 gsize i
, j
, len
= 0, len_keys
= 0;
133 gchar
**groups_user
, **groups_sys
;
134 gchar
**keys_user
, **keys_sys
;
138 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
140 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
142 /* first read all globally defined auto completions */
143 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
144 for (i
= 0; i
< len
; i
++)
146 if (strcmp(groups_sys
[i
], "Keybindings") == 0)
148 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
149 /* create new hash table for the read section (=> filetype) */
150 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
151 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
153 for (j
= 0; j
< len_keys
; j
++)
155 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
156 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
158 g_strfreev(keys_sys
);
160 g_strfreev(groups_sys
);
162 /* now read defined completions in user's configuration directory and add / replace them */
163 groups_user
= g_key_file_get_groups(userconfig
, &len
);
164 for (i
= 0; i
< len
; i
++)
166 if (strcmp(groups_user
[i
], "Keybindings") == 0)
168 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
170 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
172 { /* new key found, create hash table */
173 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
174 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
176 for (j
= 0; j
< len_keys
; j
++)
178 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
180 { /* value = NULL means the key doesn't yet exist, so insert */
181 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
182 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
185 { /* old key and value will be freed by destroy function (g_free) */
186 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
187 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
190 g_strfreev(keys_user
);
192 g_strfreev(groups_user
);
196 static void on_snippet_keybinding_activate(gchar
*key
)
198 GeanyDocument
*doc
= document_get_current();
200 GHashTable
*specials
;
202 if (!doc
|| !GTK_WIDGET_HAS_FOCUS(doc
->editor
->sci
))
205 s
= snippets_find_completion_by_name(doc
->file_type
->name
, key
);
206 if (!s
) /* allow user to specify keybindings for "special" snippets */
208 specials
= g_hash_table_lookup(snippet_hash
, "Special");
209 if (G_LIKELY(specials
!= NULL
))
210 s
= g_hash_table_lookup(specials
, key
);
218 editor_insert_snippet(doc
->editor
, sci_get_current_position(doc
->editor
->sci
), s
);
219 sci_scroll_caret(doc
->editor
->sci
);
223 static void add_kb(GKeyFile
*keyfile
, const gchar
*group
, gchar
**keys
)
229 for (i
= 0; i
< g_strv_length(keys
); i
++)
232 GdkModifierType mods
;
233 gchar
*accel_string
= g_key_file_get_value(keyfile
, group
, keys
[i
], NULL
);
235 gtk_accelerator_parse(accel_string
, &key
, &mods
);
236 g_free(accel_string
);
238 if (key
== 0 && mods
== 0)
240 g_warning("Can not parse accelerator \"%s\" from user snippets.conf", accel_string
);
243 gtk_accel_group_connect(snippet_accel_group
, key
, mods
, 0,
244 g_cclosure_new_swap((GCallback
)on_snippet_keybinding_activate
,
245 g_strdup(keys
[i
]), (GClosureNotify
)g_free
));
250 static void load_kb(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
252 const gchar kb_group
[] = "Keybindings";
253 gchar
**keys
= g_key_file_get_keys(userconfig
, kb_group
, NULL
, NULL
);
256 /* remove overridden keys from system keyfile */
257 foreach_strv(ptr
, keys
)
258 g_key_file_remove_key(sysconfig
, kb_group
, *ptr
, NULL
);
260 add_kb(userconfig
, kb_group
, keys
);
263 keys
= g_key_file_get_keys(sysconfig
, kb_group
, NULL
, NULL
);
264 add_kb(sysconfig
, kb_group
, keys
);
269 void editor_snippets_init(void)
271 gchar
*sysconfigfile
, *userconfigfile
;
272 GKeyFile
*sysconfig
= g_key_file_new();
273 GKeyFile
*userconfig
= g_key_file_new();
275 snippet_offsets
= g_queue_new();
277 sysconfigfile
= g_strconcat(app
->datadir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
278 userconfigfile
= g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
280 /* check for old autocomplete.conf files (backwards compatibility) */
281 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
))
282 setptr(userconfigfile
,
283 g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "autocomplete.conf", NULL
));
285 /* load the actual config files */
286 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
287 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
289 snippets_load(sysconfig
, userconfig
);
291 /* setup snippet keybindings */
292 snippet_accel_group
= gtk_accel_group_new();
293 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
294 load_kb(sysconfig
, userconfig
);
296 g_free(sysconfigfile
);
297 g_free(userconfigfile
);
298 g_key_file_free(sysconfig
);
299 g_key_file_free(userconfig
);
303 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
306 GeanyEditor
*editor
= data
;
307 GeanyDocument
*doc
= editor
->document
;
309 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
310 * fake event to show the editor menu triggered by a key event where we want to use the
311 * text cursor position. */
312 if (event
->x
> 0.0 && event
->y
> 0.0)
313 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
314 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
316 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
318 if (event
->button
== 1)
320 guint state
= event
->state
& gtk_accelerator_get_default_mod_mask();
322 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
324 gint ss
= sci_get_selection_start(editor
->sci
);
325 sci_set_selection_end(editor
->sci
, ss
);
327 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GDK_CONTROL_MASK
)
329 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
331 editor_find_current_word(editor
, editor_info
.click_pos
,
332 current_word
, sizeof current_word
, NULL
);
334 return symbols_goto_tag(current_word
, TRUE
);
336 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
339 return document_check_disk_status(doc
, FALSE
);
342 /* calls the edit popup menu in the editor */
343 if (event
->button
== 3)
347 /* ensure the editor widget has the focus after this operation */
348 gtk_widget_grab_focus(widget
);
350 editor_find_current_word(editor
, editor_info
.click_pos
,
351 current_word
, sizeof current_word
, NULL
);
353 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
354 ui_update_popup_goto_items(can_goto
);
355 ui_update_popup_copy_items(doc
);
356 ui_update_insert_include_item(doc
, 0);
358 g_signal_emit_by_name(geany_object
, "update-editor-menu",
359 current_word
, editor_info
.click_pos
, doc
);
361 gtk_menu_popup(GTK_MENU(main_widgets
.editor_menu
),
362 NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
370 static gboolean
is_style_php(gint style
)
372 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
373 style
== SCE_HPHP_COMPLEX_VARIABLE
)
382 static gint
editor_get_long_line_type(void)
385 switch (app
->project
->long_line_behaviour
)
387 case 0: /* marker disabled */
389 case 1: /* use global settings */
391 case 2: /* custom (enabled) */
392 return editor_prefs
.long_line_type
;
395 if (!editor_prefs
.long_line_enabled
)
398 return editor_prefs
.long_line_type
;
402 static gint
editor_get_long_line_column(void)
404 if (app
->project
&& app
->project
->long_line_behaviour
!= 1 /* use global settings */)
405 return app
->project
->long_line_column
;
407 return editor_prefs
.long_line_column
;
411 static const GeanyEditorPrefs
*
412 get_default_prefs(void)
414 static GeanyEditorPrefs eprefs
;
416 eprefs
= editor_prefs
;
418 /* project overrides */
419 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(NULL
);
420 eprefs
.long_line_type
= editor_get_long_line_type();
421 eprefs
.long_line_column
= editor_get_long_line_column();
426 /* Gets the prefs for the editor.
427 * Prefs can be different according to project or document.
428 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
429 * settings may have changed, or if this function has been called for a different editor.
430 * @param editor The editor, or @c NULL to get the default prefs.
431 * @return The prefs. */
432 const GeanyEditorPrefs
*editor_get_prefs(GeanyEditor
*editor
)
434 static GeanyEditorPrefs eprefs
;
435 const GeanyEditorPrefs
*dprefs
= get_default_prefs();
437 /* Return the address of the default prefs to allow returning default and editor
438 * pref pointers without invalidating the contents of either. */
443 eprefs
.indentation
= (GeanyIndentPrefs
*)editor_get_indent_prefs(editor
);
444 /* add other editor & document overrides as needed */
449 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
451 ScintillaObject
*sci
;
453 g_return_if_fail(editor
!= NULL
);
457 sci_toggle_fold(sci
, line
);
459 /* extra toggling of child fold points
460 * use when editor_prefs.unfold_all_children is set and Shift is NOT pressed or when
461 * editor_prefs.unfold_all_children is NOT set but Shift is pressed */
462 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
463 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
465 gint last_line
= SSM(sci
, SCI_GETLASTCHILD
, line
, -1);
468 if (sci_get_line_is_visible(sci
, line
+ 1))
469 { /* unfold all children of the current fold point */
470 for (i
= line
; i
< last_line
; i
++)
472 if (! sci_get_line_is_visible(sci
, i
))
474 sci_toggle_fold(sci
, sci_get_fold_parent(sci
, i
));
479 { /* fold all children of the current fold point */
480 for (i
= line
; i
< last_line
; i
++)
482 gint level
= sci_get_fold_level(sci
, i
);
483 if (level
& SC_FOLDLEVELHEADERFLAG
)
485 if (sci_get_fold_expanded(sci
, i
))
486 sci_toggle_fold(sci
, i
);
494 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
496 /* left click to marker margin marks the line */
499 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
501 /*sci_marker_delete_all(editor->sci, 1);*/
502 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
504 /* left click on the folding margin to toggle folding state of current line */
505 else if (nt
->margin
== 2 && editor_prefs
.folding
)
507 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
508 editor_toggle_fold(editor
, line
, nt
->modifiers
);
513 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
515 ScintillaObject
*sci
= editor
->sci
;
516 gint pos
= sci_get_current_position(sci
);
518 /* undo / redo menu update */
519 ui_update_popup_reundo_items(editor
->document
);
521 /* brace highlighting */
522 editor_highlight_braces(editor
, pos
);
524 ui_update_statusbar(editor
->document
, pos
);
527 /** experimental code for inverting selections */
530 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
532 /* need to get colour from getstyleat(), but how? */
533 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
534 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
537 sci_get_style_at(sci
, pos
);
543 static void check_line_breaking(GeanyEditor
*editor
, gint pos
, gchar c
)
545 ScintillaObject
*sci
= editor
->sci
;
546 gint line
, lstart
, col
;
548 if (!editor
->line_breaking
)
551 col
= sci_get_col_from_position(sci
, pos
);
554 pos
--; /* Look for previous space, not the new one */
556 line
= sci_get_current_line(sci
);
558 lstart
= sci_get_position_from_line(sci
, line
);
560 /* use column instead of position which might be different with multibyte characters */
561 if (col
< editor_prefs
.line_break_column
)
564 /* look for the last space before line_break_column */
565 pos
= MIN(pos
, lstart
+ editor_prefs
.line_break_column
);
569 c
= sci_get_char_at(sci
, --pos
);
572 gint diff
, last_pos
, last_col
;
574 /* remember the distance between the current column and the last column on the line
575 * (we use column position in case the previous line gets altered, such as removing
576 * trailing spaces or in case it contains multibyte characters) */
577 last_pos
= sci_get_line_end_position(sci
, line
);
578 last_col
= sci_get_col_from_position(sci
, last_pos
);
579 diff
= last_col
- col
;
581 /* break the line after the space */
582 sci_set_current_position(sci
, pos
+ 1, FALSE
);
583 sci_cancel(sci
); /* don't select from completion list */
584 sci_send_command(sci
, SCI_NEWLINE
);
587 /* correct cursor position (might not be at line end) */
588 last_pos
= sci_get_line_end_position(sci
, line
);
589 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
590 /* last column - distance is the desired column, then retrieve its document position */
591 pos
= SSM(sci
, SCI_FINDCOLUMN
, line
, last_col
- diff
);
592 sci_set_current_position(sci
, pos
, FALSE
);
600 static void show_autocomplete(ScintillaObject
*sci
, gint rootlen
, const gchar
*words
)
602 /* store whether a calltip is showing, so we can reshow it after autocompletion */
603 calltip
.set
= SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
604 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
);
608 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
610 ScintillaObject
*sci
= editor
->sci
;
612 g_return_if_fail(tags
);
616 GString
*words
= g_string_sized_new(150);
619 for (j
= 0; j
< tags
->len
; ++j
)
621 TMTag
*tag
= tags
->pdata
[j
];
624 g_string_append_c(words
, '\n');
626 if (j
== editor_prefs
.autocompletion_max_entries
)
628 g_string_append(words
, "...");
631 g_string_append(words
, tag
->name
);
633 /* for now, tag types don't all follow C, so just look at arglist */
634 if (NZV(tag
->atts
.entry
.arglist
))
635 g_string_append(words
, "?2");
637 g_string_append(words
, "?1");
639 show_autocomplete(sci
, rootlen
, words
->str
);
640 g_string_free(words
, TRUE
);
645 /* do not use with long strings */
646 static gboolean
match_last_chars(ScintillaObject
*sci
, gint pos
, const gchar
*str
)
648 gsize len
= strlen(str
);
651 g_return_val_if_fail(len
< 100, FALSE
);
653 buf
= g_alloca(len
+ 1);
654 sci_get_text_range(sci
, pos
- len
, pos
, buf
);
655 return strcmp(str
, buf
) == 0;
659 static gboolean
reshow_calltip(gpointer data
)
663 g_return_val_if_fail(calltip
.sci
!= NULL
, FALSE
);
665 SSM(calltip
.sci
, SCI_CALLTIPCANCEL
, 0, 0);
666 doc
= document_get_current();
668 if (doc
&& doc
->editor
->sci
== calltip
.sci
)
670 /* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
671 * may be completely wrong in case the user cancelled the auto completion with the mouse */
672 SSM(calltip
.sci
, SCI_CALLTIPSHOW
, calltip
.pos
, (sptr_t
) calltip
.text
);
678 static void request_reshowing_calltip(SCNotification
*nt
)
682 /* delay the reshow of the calltip window to make sure it is actually displayed,
683 * without it might be not visible on SCN_AUTOCCANCEL */
684 g_idle_add(reshow_calltip
, NULL
);
689 static void autocomplete_scope(GeanyEditor
*editor
)
691 ScintillaObject
*sci
= editor
->sci
;
692 gint pos
= sci_get_current_position(editor
->sci
);
693 gchar typed
= sci_get_char_at(sci
, pos
- 1);
695 const GPtrArray
*tags
= NULL
;
697 GeanyFiletype
*ft
= editor
->document
->file_type
;
699 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
701 if (match_last_chars(sci
, pos
, "->") || match_last_chars(sci
, pos
, "::"))
703 else if (typed
!= '.')
706 else if (typed
!= '.')
709 /* allow for a space between word and operator */
710 if (isspace(sci_get_char_at(sci
, pos
- 2)))
712 name
= editor_get_word_at_pos(editor
, pos
- 1, NULL
);
716 tags
= tm_workspace_find(name
, tm_tag_max_t
, NULL
, FALSE
, ft
->lang
);
718 if (!tags
|| tags
->len
== 0)
721 tag
= g_ptr_array_index(tags
, 0);
722 name
= tag
->atts
.entry
.var_type
;
725 TMWorkObject
*obj
= editor
->document
->tm_file
;
727 tags
= tm_workspace_find_scope_members(obj
? obj
->tags_array
: NULL
,
730 show_tags_list(editor
, tags
, 0);
735 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
737 ScintillaObject
*sci
= editor
->sci
;
738 gint pos
= sci_get_current_position(sci
);
743 { /* simple indentation (only for CR format) */
744 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
745 on_new_line_added(editor
);
749 { /* simple indentation (for CR/LF and LF format) */
750 on_new_line_added(editor
);
754 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
757 { /* close xml-tags */
758 handle_xml(editor
, pos
, nt
->ch
);
763 auto_close_chars(sci
, pos
, nt
->ch
);
765 editor_show_calltip(editor
, --pos
);
769 { /* hide calltips */
770 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0))
772 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
774 g_free(calltip
.text
);
786 auto_close_chars(sci
, pos
, nt
->ch
);
790 { /* closing bracket handling */
791 if (editor
->auto_indent
)
792 close_block(editor
, pos
- 1);
795 /* scope autocompletion */
797 case ':': /* C/C++ class:: syntax */
798 /* tag autocompletion */
801 if (! editor_start_auto_complete(editor
, pos
, FALSE
))
802 request_reshowing_calltip(nt
);
804 editor_start_auto_complete(editor
, pos
, FALSE
);
807 check_line_breaking(editor
, pos
, nt
->ch
);
811 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
812 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
813 gboolean force
, gint visLevels
, gint level
)
815 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
816 gint levelLine
= level
;
818 while (*line
<= lineMaxSubord
)
820 if (G_UNLIKELY(force
))
823 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
825 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
830 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
833 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
834 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
836 if (G_UNLIKELY(force
))
839 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
841 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
842 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
848 if (!sci_get_fold_expanded(sci
, *line
))
849 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
850 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
854 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
866 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
868 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
870 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
872 /* Adding a fold point */
873 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
874 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
877 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
879 if (! sci_get_fold_expanded(sci
, line
))
880 { /* Removing the fold from one that has been contracted so should expand
881 * otherwise lines are left invisible with no way to make them visible */
882 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
883 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
886 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
887 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
889 /* See if should still be hidden */
890 gint parentLine
= sci_get_fold_parent(sci
, line
);
893 SSM(sci
, SCI_SHOWLINES
, line
, line
);
895 else if (sci_get_fold_expanded(sci
, parentLine
) &&
896 sci_get_line_is_visible(sci
, parentLine
))
898 SSM(sci
, SCI_SHOWLINES
, line
, line
);
904 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
905 gboolean enforcePolicy
)
907 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
908 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
911 for (line
= lineStart
; line
<= lineEnd
; line
++)
913 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
918 static void auto_update_margin_width(GeanyEditor
*editor
)
920 gint next_linecount
= 1;
921 gint linecount
= sci_get_line_count(editor
->sci
);
922 GeanyDocument
*doc
= editor
->document
;
924 while (next_linecount
<= linecount
)
925 next_linecount
*= 10;
927 if (editor
->document
->priv
->line_count
!= next_linecount
)
929 doc
->priv
->line_count
= next_linecount
;
930 sci_set_line_numbers(editor
->sci
, TRUE
, 0);
935 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
937 gint pos
= sci_get_current_position(sci
);
939 sci_insert_text(sci
, pos
, text
);
940 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
944 /* Complete the next word part from @a entry */
945 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
947 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
949 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
951 if (strstr(text
, stem
) != text
)
952 return FALSE
; /* shouldn't happen */
953 if (strlen(text
) <= strlen(stem
))
956 text
+= strlen(stem
); /* skip stem */
957 ptr
= strstr(text
+ 1, "_");
961 partial_complete(editor
->sci
, text
);
967 foreach_str(ptr
, text
+ 1)
971 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
974 partial_complete(editor
->sci
, text
);
983 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
984 * Plugins can connect to the "editor-notify" signal. */
985 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
986 gpointer scnt
, gpointer data
)
988 GeanyEditor
*editor
= data
;
991 g_return_if_fail(editor
!= NULL
);
993 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
997 static gboolean
on_document_update_tags_idle(gpointer data
)
999 GeanyDocument
*doc
= data
;
1001 if (!main_status
.quitting
&& DOC_VALID(doc
))
1002 document_update_tag_list(doc
, TRUE
);
1004 document_tags_update_pending
= FALSE
;
1009 static void request_tag_list_update(GeanyDocument
*doc
)
1011 if (!document_tags_update_pending
)
1013 document_tags_update_pending
= TRUE
;
1014 g_timeout_add_full(G_PRIORITY_LOW
, editor_prefs
.autocompletion_update_freq
,
1015 on_document_update_tags_idle
, doc
, NULL
);
1020 static gboolean
on_editor_notify(G_GNUC_UNUSED GObject
*object
, GeanyEditor
*editor
,
1021 SCNotification
*nt
, G_GNUC_UNUSED gpointer data
)
1023 ScintillaObject
*sci
= editor
->sci
;
1024 GeanyDocument
*doc
= editor
->document
;
1026 switch (nt
->nmhdr
.code
)
1028 case SCN_SAVEPOINTLEFT
:
1029 document_set_text_changed(doc
, TRUE
);
1032 case SCN_SAVEPOINTREACHED
:
1033 document_set_text_changed(doc
, FALSE
);
1036 case SCN_MODIFYATTEMPTRO
:
1040 case SCN_MARGINCLICK
:
1041 on_margin_click(editor
, nt
);
1045 on_update_ui(editor
, nt
);
1049 /* Visible lines are only laid out accurately just before painting,
1050 * so we need to only call editor_scroll_to_line here, because the document
1051 * may have line wrapping and folding enabled.
1052 * http://scintilla.sourceforge.net/ScintillaDoc.html#LineWrapping
1053 * This is important e.g. when loading a session and switching pages
1054 * and having the cursor scroll in view. */
1055 /* FIXME: Really we want to do this just before painting, not after it
1056 * as it will cause repainting. */
1057 if (editor
->scroll_percent
> 0.0F
)
1059 editor_scroll_to_line(editor
, -1, editor
->scroll_percent
);
1060 /* disable further scrolling */
1061 editor
->scroll_percent
= -1.0F
;
1066 if (editor_prefs
.show_linenumber_margin
&& (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) && nt
->linesAdded
)
1068 /* automatically adjust Scintilla's line numbers margin width */
1069 auto_update_margin_width(editor
);
1071 if (nt
->modificationType
& SC_STARTACTION
&& ! ignore_callback
)
1073 /* get notified about undo changes */
1074 document_undo_add(doc
, UNDO_SCINTILLA
, NULL
);
1076 if (editor_prefs
.folding
&& (nt
->modificationType
& SC_MOD_CHANGEFOLD
) != 0)
1078 /* handle special fold cases, e.g. #1923350 */
1079 fold_changed(sci
, nt
->line
, nt
->foldLevelNow
, nt
->foldLevelPrev
);
1081 if (editor_prefs
.autocompletion_update_freq
> 0 &&
1082 (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) &&
1083 filetype_has_tags(doc
->file_type
))
1085 request_tag_list_update(doc
);
1090 on_char_added(editor
, nt
);
1093 case SCN_USERLISTSELECTION
:
1094 if (nt
->listType
== 1)
1096 sci_add_text(sci
, nt
->text
);
1100 case SCN_AUTOCSELECTION
:
1101 if (g_str_equal(nt
->text
, "..."))
1108 case SCN_AUTOCCANCELLED
:
1109 /* now that autocomplete is finishing or was cancelled, reshow calltips
1110 * if they were showing */
1111 request_reshowing_calltip(nt
);
1115 case SCN_STYLENEEDED
:
1116 geany_debug("style");
1120 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
1123 case SCN_URIDROPPED
:
1124 if (nt
->text
!= NULL
)
1126 document_open_file_list(nt
->text
, -1);
1130 case SCN_CALLTIPCLICK
:
1131 if (nt
->position
> 0)
1133 switch (nt
->position
)
1135 case 1: /* up arrow */
1136 if (calltip
.tag_index
> 0)
1137 calltip
.tag_index
--;
1140 case 2: calltip
.tag_index
++; break; /* down arrow */
1142 editor_show_calltip(editor
, -1);
1147 /* recalculate line margin width */
1148 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
1151 /* we always return FALSE here to let plugins handle the event too */
1156 /* Note: this is the same as sci_get_tab_width(), but is still useful when you don't have
1157 * a scintilla pointer. */
1158 static gint
get_tab_width(const GeanyIndentPrefs
*indent_prefs
)
1160 if (indent_prefs
->type
== GEANY_INDENT_TYPE_BOTH
)
1161 return indent_prefs
->hard_tab_width
;
1163 return indent_prefs
->width
; /* tab width = indent width */
1167 /* Returns a string containing width chars of whitespace, filled with simple space
1168 * characters or with the right number of tab characters, according to the indent prefs.
1169 * (Result is filled with tabs *and* spaces if width isn't a multiple of
1170 * the tab width). */
1172 get_whitespace(const GeanyIndentPrefs
*iprefs
, gint width
)
1174 g_return_val_if_fail(width
>= 0, NULL
);
1177 return g_strdup("");
1179 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
1181 return g_strnfill(width
, ' ');
1184 { /* first fill text with tabs and fill the rest with spaces */
1185 const gint tab_width
= get_tab_width(iprefs
);
1186 gint tabs
= width
/ tab_width
;
1187 gint spaces
= width
% tab_width
;
1188 gint len
= tabs
+ spaces
;
1191 str
= g_malloc(len
+ 1);
1193 memset(str
, '\t', tabs
);
1194 memset(str
+ tabs
, ' ', spaces
);
1201 static const GeanyIndentPrefs
*
1202 get_default_indent_prefs(void)
1204 static GeanyIndentPrefs iprefs
;
1206 iprefs
= app
->project
? *app
->project
->priv
->indentation
: *editor_prefs
.indentation
;
1211 /** Gets the indentation prefs for the editor.
1212 * Prefs can be different according to project or document.
1213 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
1214 * settings may have changed, or if this function has been called for a different editor.
1215 * @param editor The editor, or @c NULL to get the default indent prefs.
1216 * @return The indent prefs. */
1217 const GeanyIndentPrefs
*
1218 editor_get_indent_prefs(GeanyEditor
*editor
)
1220 static GeanyIndentPrefs iprefs
;
1221 const GeanyIndentPrefs
*dprefs
= get_default_indent_prefs();
1223 /* Return the address of the default prefs to allow returning default and editor
1224 * pref pointers without invalidating the contents of either. */
1229 iprefs
.type
= editor
->indent_type
;
1230 iprefs
.width
= editor
->indent_width
;
1232 /* if per-document auto-indent is enabled, but we don't have a global mode set,
1233 * just use basic auto-indenting */
1234 if (editor
->auto_indent
&& iprefs
.auto_indent_mode
== GEANY_AUTOINDENT_NONE
)
1235 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_BASIC
;
1237 if (!editor
->auto_indent
)
1238 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_NONE
;
1244 static void on_new_line_added(GeanyEditor
*editor
)
1246 ScintillaObject
*sci
= editor
->sci
;
1247 gint line
= sci_get_current_line(sci
);
1249 /* simple indentation */
1250 if (editor
->auto_indent
)
1252 insert_indent_after_line(editor
, line
- 1);
1255 if (editor_prefs
.auto_continue_multiline
)
1256 { /* " * " auto completion in multiline C/C++/D/Java comments */
1257 auto_multiline(editor
, line
);
1260 if (editor_prefs
.newline_strip
)
1261 { /* strip the trailing spaces on the previous line */
1262 editor_strip_line_trailing_spaces(editor
, line
- 1);
1267 static gboolean
lexer_has_braces(ScintillaObject
*sci
)
1269 gint lexer
= sci_get_lexer(sci
);
1275 case SCLEX_HTML
: /* for PHP & JS */
1276 case SCLEX_PASCAL
: /* for multiline comments? */
1287 /* Read indent chars for the line that pos is on into indent global variable.
1288 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1289 * instead in any new code. */
1290 static void read_indent(GeanyEditor
*editor
, gint pos
)
1292 ScintillaObject
*sci
= editor
->sci
;
1293 guint i
, len
, j
= 0;
1297 line
= sci_get_line_from_position(sci
, pos
);
1299 len
= sci_get_line_length(sci
, line
);
1300 linebuf
= sci_get_line(sci
, line
);
1302 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1304 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1305 indent
[j
++] = linebuf
[i
];
1314 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1320 len
= sci_get_line_length(sci
, line
);
1321 linebuf
= sci_get_line(sci
, line
);
1323 for (i
= 0; i
< len
; i
++)
1325 /* i == (len - 1) prevents wrong indentation after lines like
1326 * " { return bless({}, shift); }" (Perl) */
1327 if (linebuf
[i
] == '{' && i
== (len
- 1))
1336 while (k
> 0 && isspace(linebuf
[k
])) k
--;
1338 /* if last non-whitespace character is a { increase indentation by a tab
1339 * e.g. for (...) { */
1340 if (linebuf
[k
] == '{')
1352 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1354 gint last_char
= sci_get_line_end_position(sci
, line
) - 1;
1356 /* add extra indentation for Python after colon */
1357 if (sci_get_char_at(sci
, last_char
) == ':' &&
1358 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1366 static gint
get_xml_indent(ScintillaObject
*sci
, gint line
)
1368 gboolean need_close
= FALSE
;
1369 gint end
= sci_get_line_end_position(sci
, line
) - 1;
1372 /* don't indent if there's a closing tag to the right of the cursor */
1373 pos
= sci_get_current_position(sci
);
1374 if (sci_get_char_at(sci
, pos
) == '<' &&
1375 sci_get_char_at(sci
, pos
+ 1) == '/')
1378 if (sci_get_char_at(sci
, end
) == '>' &&
1379 sci_get_char_at(sci
, end
- 1) != '/')
1381 gint style
= sci_get_style_at(sci
, end
);
1383 if (style
== SCE_H_TAG
|| style
== SCE_H_TAGUNKNOWN
)
1385 gint start
= sci_get_position_from_line(sci
, line
);
1386 gchar
*line_contents
= sci_get_contents_range(sci
, start
, end
+ 1);
1387 gchar
*opened_tag_name
= utils_find_open_xml_tag(line_contents
, end
+ 1 - start
);
1389 if (NZV(opened_tag_name
))
1392 if (sci_get_lexer(sci
) == SCLEX_HTML
&& utils_is_short_html_tag(opened_tag_name
))
1395 g_free(line_contents
);
1396 g_free(opened_tag_name
);
1400 return need_close
? 1 : 0;
1404 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1406 ScintillaObject
*sci
= editor
->sci
;
1408 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1410 g_return_val_if_fail(line
>= 0, 0);
1412 size
= sci_get_line_indentation(sci
, line
);
1414 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1416 gint additional_indent
= 0;
1418 if (lexer_has_braces(sci
))
1419 additional_indent
= iprefs
->width
* get_brace_indent(sci
, line
);
1421 if (editor
->document
->file_type
->id
== GEANY_FILETYPES_PYTHON
)
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 styBrace
= sci_get_style_at(sci
, pos
);
1524 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1527 pos
= pos
+ direction
;
1528 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1530 chAtPos
= sci_get_char_at(sci
, pos
- 1);
1531 styAtPos
= sci_get_style_at(sci
, pos
);
1533 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1535 if (chAtPos
== chBrace
)
1537 if (chAtPos
== chSeek
)
1542 pos
= pos
+ direction
;
1548 /* Called after typing '}'. */
1549 static void close_block(GeanyEditor
*editor
, gint pos
)
1552 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1553 gint x
= 0, cnt
= 0;
1554 gint line
, line_len
, eol_char_len
;
1555 gchar
*text
, *line_buf
;
1556 ScintillaObject
*sci
;
1557 gint line_indent
, last_indent
;
1559 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1561 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1564 doc
= editor
->document
;
1566 if (! lexer_has_braces(sci
))
1569 line
= sci_get_line_from_position(sci
, pos
);
1570 line_len
= sci_get_line_length(sci
, line
);
1571 /* set eol_char_len to 0 if on last line, because there is no EOL char */
1572 eol_char_len
= (line
== (sci_get_line_count(sci
) - 1)) ? 0 :
1573 editor_get_eol_char_len(editor
);
1575 /* check that the line is empty, to not kill text in the line */
1576 line_buf
= sci_get_line(sci
, line
);
1577 line_buf
[line_len
- eol_char_len
] = '\0';
1578 while (x
< (line_len
- eol_char_len
))
1580 if (isspace(line_buf
[x
]))
1586 if ((line_len
- eol_char_len
- 1) != cnt
)
1589 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1591 gint start_brace
= brace_match(sci
, pos
);
1593 if (start_brace
>= 0)
1596 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1597 gint size
= sci_get_line_indentation(sci
, brace_line
);
1598 gchar
*ind
= get_whitespace(iprefs
, size
);
1600 text
= g_strconcat(ind
, "}", NULL
);
1601 line_start
= sci_get_position_from_line(sci
, line
);
1602 sci_set_anchor(sci
, line_start
);
1603 sci_replace_sel(sci
, text
);
1608 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1611 /* GEANY_AUTOINDENT_CURRENTCHARS */
1612 line_indent
= sci_get_line_indentation(sci
, line
);
1613 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1615 if (line_indent
< last_indent
)
1617 line_indent
-= iprefs
->width
;
1618 line_indent
= MAX(0, line_indent
);
1619 sci_set_line_indentation(sci
, line
, line_indent
);
1623 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1624 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1625 * position can be -1, then the current position is used.
1626 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1627 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1628 const gchar
*wc
, gboolean stem
)
1630 gint line
, line_start
, startword
, endword
;
1632 ScintillaObject
*sci
;
1634 g_return_if_fail(editor
!= NULL
);
1638 pos
= sci_get_current_position(sci
);
1640 line
= sci_get_line_from_position(sci
, pos
);
1641 line_start
= sci_get_position_from_line(sci
, line
);
1642 startword
= pos
- line_start
;
1643 endword
= pos
- line_start
;
1646 chunk
= sci_get_line(sci
, line
);
1649 wc
= GEANY_WORDCHARS
;
1651 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1652 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1653 * TODO: improve this code */
1654 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || chunk
[startword
- 1] < 0))
1658 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || chunk
[endword
] < 0))
1662 if (startword
!= endword
)
1664 chunk
[endword
] = '\0';
1666 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1669 g_strlcpy(word
, "", wordlen
);
1675 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1676 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1677 * position can be -1, then the current position is used.
1678 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1679 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1682 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1687 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1688 * Otherwise NULL is returned.
1689 * Additional wordchars can be specified to define what to consider as a word.
1691 * @param editor The editor to operate on.
1692 * @param pos The position where the word should be read from.
1693 * Maybe @c -1 to use the current position.
1694 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1695 * as part of a word. Maybe @c NULL to use the default wordchars,
1696 * see @ref GEANY_WORDCHARS.
1698 * @return A newly-allocated string containing the word at the given @a pos or @c NULL.
1699 * Should be freed when no longer needed.
1703 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1705 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1707 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1709 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1711 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1715 /* Read the word up to position @a pos. */
1716 static const gchar
*
1717 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1719 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1721 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1723 return (*word
) ? word
: NULL
;
1727 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1730 gint orig_pos
= pos
;
1732 c
= sci_get_char_at(sci
, pos
);
1733 while (pos
>= 0 && pos
> orig_pos
- 300)
1735 c
= sci_get_char_at(sci
, pos
);
1737 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1744 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1748 gint orig_pos
= pos
;
1750 c
= sci_get_char_at(sci
, pos
);
1751 while (pos
> 0 && pos
> orig_pos
- 300)
1753 c
= sci_get_char_at(sci
, pos
);
1754 if (c
== ')') brackets
++;
1755 else if (c
== '(') brackets
--;
1757 if (brackets
< 0) return pos
; /* found start bracket */
1763 static gboolean
append_calltip(GString
*str
, const TMTag
*tag
, filetype_id ft_id
)
1765 if (! tag
->atts
.entry
.arglist
)
1768 if (ft_id
!= GEANY_FILETYPES_PASCAL
)
1769 { /* usual calltips: "retval tagname (arglist)" */
1770 if (tag
->atts
.entry
.var_type
)
1774 g_string_append(str
, tag
->atts
.entry
.var_type
);
1775 for (i
= 0; i
< tag
->atts
.entry
.pointerOrder
; i
++)
1777 g_string_append_c(str
, '*');
1779 g_string_append_c(str
, ' ');
1781 if (tag
->atts
.entry
.scope
)
1783 const gchar
*cosep
= symbols_get_context_separator(ft_id
);
1785 g_string_append(str
, tag
->atts
.entry
.scope
);
1786 g_string_append(str
, cosep
);
1788 g_string_append(str
, tag
->name
);
1789 g_string_append_c(str
, ' ');
1790 g_string_append(str
, tag
->atts
.entry
.arglist
);
1793 { /* special case Pascal calltips: "tagname (arglist) : retval" */
1794 g_string_append(str
, tag
->name
);
1795 g_string_append_c(str
, ' ');
1796 g_string_append(str
, tag
->atts
.entry
.arglist
);
1798 if (NZV(tag
->atts
.entry
.var_type
))
1800 g_string_append(str
, " : ");
1801 g_string_append(str
, tag
->atts
.entry
.var_type
);
1809 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1811 const GPtrArray
*tags
;
1812 const gint arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1813 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1814 TMTagAttrType
*attrs
= NULL
;
1816 GString
*str
= NULL
;
1819 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1821 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1822 tags
= tm_workspace_find(word
, tm_tag_max_t
, attrs
, FALSE
, ft
->lang
);
1826 tag
= TM_TAG(tags
->pdata
[0]);
1828 if (tag
->type
== tm_tag_class_t
&& FILETYPE_ID(ft
) == GEANY_FILETYPES_D
)
1830 /* user typed e.g. 'new Classname(' so lookup D constructor Classname::this() */
1831 tags
= tm_workspace_find_scoped("this", tag
->name
,
1832 arg_types
, attrs
, FALSE
, ft
->lang
, TRUE
);
1837 /* remove tags with no argument list */
1838 for (i
= 0; i
< tags
->len
; i
++)
1840 tag
= TM_TAG(tags
->pdata
[i
]);
1842 if (! tag
->atts
.entry
.arglist
)
1843 tags
->pdata
[i
] = NULL
;
1845 tm_tags_prune((GPtrArray
*) tags
);
1849 { /* remove duplicate calltips */
1850 TMTagAttrType sort_attr
[] = {tm_tag_attr_name_t
, tm_tag_attr_scope_t
,
1851 tm_tag_attr_arglist_t
, 0};
1853 tm_tags_sort((GPtrArray
*) tags
, sort_attr
, TRUE
);
1856 /* if the current word has changed since last time, start with the first tag match */
1857 if (! utils_str_equal(word
, calltip
.last_word
))
1858 calltip
.tag_index
= 0;
1859 /* cache the current word for next time */
1860 g_free(calltip
.last_word
);
1861 calltip
.last_word
= g_strdup(word
);
1862 calltip
.tag_index
= MIN(calltip
.tag_index
, tags
->len
- 1); /* ensure tag_index is in range */
1864 for (i
= calltip
.tag_index
; i
< tags
->len
; i
++)
1866 tag
= TM_TAG(tags
->pdata
[i
]);
1870 str
= g_string_new(NULL
);
1871 if (calltip
.tag_index
> 0)
1872 g_string_prepend(str
, "\001 "); /* up arrow */
1873 append_calltip(str
, tag
, FILETYPE_ID(ft
));
1875 else /* add a down arrow */
1877 if (calltip
.tag_index
> 0) /* already have an up arrow */
1878 g_string_insert_c(str
, 1, '\002');
1880 g_string_prepend(str
, "\002 ");
1886 gchar
*result
= str
->str
;
1888 g_string_free(str
, FALSE
);
1895 /* use pos = -1 to search for the previous unmatched open bracket. */
1896 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
1898 gint orig_pos
= pos
; /* the position for the calltip */
1901 gchar word
[GEANY_MAX_WORD_LENGTH
];
1903 ScintillaObject
*sci
;
1905 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1906 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
1910 lexer
= sci_get_lexer(sci
);
1914 /* position of '(' is unknown, so go backwards from current position to find it */
1915 pos
= sci_get_current_position(sci
);
1918 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
1919 find_start_bracket(sci
, pos
);
1924 /* the style 1 before the brace (which may be highlighted) */
1925 style
= sci_get_style_at(sci
, pos
- 1);
1926 if (! highlighting_is_code_style(lexer
, style
))
1930 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
1931 if (word
[0] == '\0')
1934 str
= find_calltip(word
, editor
->document
->file_type
);
1937 g_free(calltip
.text
); /* free the old calltip */
1939 calltip
.pos
= orig_pos
;
1942 utils_wrap_string(calltip
.text
, -1);
1943 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
1950 gchar
*editor_get_calltip_text(GeanyEditor
*editor
, const TMTag
*tag
)
1954 g_return_val_if_fail(editor
!= NULL
, NULL
);
1956 str
= g_string_new(NULL
);
1957 if (append_calltip(str
, tag
, editor
->document
->file_type
->id
))
1958 return g_string_free(str
, FALSE
);
1960 return g_string_free(str
, TRUE
);
1964 /* HTML entities auto completion from html_entities.tags text file */
1966 autocomplete_html(ScintillaObject
*sci
, const gchar
*root
, gsize rootlen
)
1969 gboolean found
= FALSE
;
1971 const gchar
**entities
= symbols_get_html_entities();
1973 if (*root
!= '&' || G_UNLIKELY(entities
== NULL
))
1976 words
= g_string_sized_new(500);
1979 if (entities
[i
] == NULL
)
1981 else if (entities
[i
][0] == '#')
1984 if (! strncmp(entities
[i
], root
, rootlen
))
1987 g_string_append_c(words
, '\n');
1988 g_string_append(words
, entities
[i
]);
1993 show_autocomplete(sci
, rootlen
, words
->str
);
1995 g_string_free(words
, TRUE
);
2000 /* Current document & global tags autocompletion */
2002 autocomplete_tags(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
2004 TMTagAttrType attrs
[] = { tm_tag_attr_name_t
, 0 };
2005 const GPtrArray
*tags
;
2008 g_return_val_if_fail(editor
, FALSE
);
2010 doc
= editor
->document
;
2012 tags
= tm_workspace_find(root
, tm_tag_max_t
, attrs
, TRUE
, doc
->file_type
->lang
);
2015 show_tags_list(editor
, tags
, rootlen
);
2016 return tags
->len
> 0;
2022 /* Check whether to use entity autocompletion:
2023 * - always in a HTML file except when inside embedded JavaScript, Python, ASP, ...
2024 * - in a PHP file only when we are outside of <? ?> */
2025 static gboolean
autocomplete_check_for_html(gint ft_id
, gint style
)
2027 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
2028 * (everything after SCE_HJ_START is for embedded scripting languages) */
2029 if (ft_id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
2032 if (ft_id
== GEANY_FILETYPES_PHP
)
2034 /* use entity completion when style is outside of PHP styles */
2035 if (! is_style_php(style
))
2043 /* Algorithm based on based on Scite's StartAutoCompleteWord() */
2044 static GString
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
2047 gint len
, current
, word_end
;
2048 gint pos_find
, flags
;
2052 struct Sci_TextToFind ttf
;
2054 len
= sci_get_length(sci
);
2055 current
= sci_get_current_position(sci
) - rootlen
;
2057 ttf
.lpstrText
= root
;
2059 ttf
.chrg
.cpMax
= len
;
2060 ttf
.chrgText
.cpMin
= 0;
2061 ttf
.chrgText
.cpMax
= 0;
2062 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
2064 words
= g_string_sized_new(256);
2065 /* put space before first entry to make searching with strstr easy */
2066 g_string_append_c(words
, ' ');
2068 /* search the whole document for the word root and collect results */
2069 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2070 while (pos_find
>= 0 && pos_find
< len
)
2072 word_end
= pos_find
+ rootlen
;
2073 if (pos_find
!= current
)
2075 while (word_end
< len
&& strchr(GEANY_WORDCHARS
, sci_get_char_at(sci
, word_end
)) != NULL
)
2078 word_length
= word_end
- pos_find
;
2079 if (word_length
> rootlen
)
2081 word
= g_malloc0(word_length
+ 3);
2082 sci_get_text_range(sci
, pos_find
, word_end
, word
+ 1);
2084 word
[word_length
+ 1] = ' ';
2085 /* search the words string whether we already have the word in, otherwise add it */
2086 if (strstr(words
->str
, word
) == NULL
)
2088 g_string_append(words
, word
+ 1);
2093 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
2097 ttf
.chrg
.cpMin
= word_end
;
2098 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2103 g_strdelimit(words
->str
, " ", '\n');
2104 words
->str
[words
->len
- 1] = '\0'; /* remove the trailing '\n' */
2107 g_string_free(words
, TRUE
);
2112 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
2114 ScintillaObject
*sci
= editor
->sci
;
2118 GSList
*node
, *list
= NULL
;
2120 words
= get_doc_words(sci
, root
, rootlen
);
2123 scintilla_send_message(sci
, SCI_AUTOCCANCEL
, 0, 0);
2127 /* words are unsorted, make list of words */
2128 foreach_str(ptr
, words
->str
)
2132 list
= g_slist_prepend(list
, ptr
+ 1);
2133 /* terminate previous string in list */
2138 list
= g_slist_sort(list
, (GCompareFunc
)utils_str_casecmp
);
2140 str
= g_string_sized_new(words
->len
);
2141 foreach_slist(node
, list
)
2143 g_string_append(str
, node
->data
);
2145 g_string_append_c(str
, '\n');
2147 if (g_slist_length(list
) >= editor_prefs
.autocompletion_max_entries
)
2148 g_string_append(str
, "\n...");
2151 g_string_free(words
, TRUE
);
2153 show_autocomplete(sci
, rootlen
, str
->str
);
2154 g_string_free(str
, TRUE
);
2159 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
2161 gint line
, line_start
, line_len
, line_pos
, current
, rootlen
, startword
, lexer
, style
;
2162 gchar
*linebuf
, *root
;
2163 ScintillaObject
*sci
;
2164 gboolean ret
= FALSE
;
2165 const gchar
*wordchars
;
2168 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
2171 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2173 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
2174 * necessary styling information */
2175 if (G_UNLIKELY(pos
< 2))
2179 ft
= editor
->document
->file_type
;
2181 line
= sci_get_line_from_position(sci
, pos
);
2182 line_start
= sci_get_position_from_line(sci
, line
);
2183 line_len
= sci_get_line_length(sci
, line
);
2184 line_pos
= pos
- line_start
- 1;
2185 current
= pos
- line_start
;
2186 startword
= current
;
2187 lexer
= sci_get_lexer(sci
);
2188 style
= sci_get_style_at(sci
, pos
- 2);
2190 /* don't autocomplete in comments and strings */
2191 if (!force
&& !highlighting_is_code_style(lexer
, style
))
2194 autocomplete_scope(editor
);
2196 linebuf
= sci_get_line(sci
, line
);
2198 if (ft
->id
== GEANY_FILETYPES_LATEX
)
2199 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
2200 else if (ft
->id
== GEANY_FILETYPES_HTML
|| ft
->id
== GEANY_FILETYPES_PHP
)
2201 wordchars
= GEANY_WORDCHARS
"&"; /* add & to word chars if we are in a PHP or HTML file */
2203 wordchars
= GEANY_WORDCHARS
;
2205 /* find the start of the current word */
2206 while ((startword
> 0) && (strchr(wordchars
, linebuf
[startword
- 1])))
2208 linebuf
[current
] = '\0';
2209 root
= linebuf
+ startword
;
2210 rootlen
= current
- startword
;
2214 if (autocomplete_check_for_html(ft
->id
, style
))
2216 /* Allow something like ""some text"". The above startword calculation
2217 * only works on words but for HTML entity completion we also want to have completion
2218 * based on '&' within words. */
2219 gchar
*tmp
= strchr(root
, '&');
2223 rootlen
= strlen(tmp
);
2225 ret
= autocomplete_html(sci
, root
, rootlen
);
2229 /* force is set when called by keyboard shortcut, otherwise start at the
2230 * editor_prefs.symbolcompletion_min_chars'th char */
2231 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
2233 /* complete tags, except if forcing when completion is already visible */
2234 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
2235 ret
= autocomplete_tags(editor
, root
, rootlen
);
2237 /* If forcing and there's nothing else to show, complete from words in document */
2238 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2239 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2251 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2253 gchar
*result
= NULL
;
2256 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2258 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2261 result
= g_hash_table_lookup(tmp
, name
);
2263 /* whether nothing is set for the current filetype(tmp is NULL) or
2264 * the particular completion for this filetype is not set (result is NULL) */
2265 if (tmp
== NULL
|| result
== NULL
)
2267 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2270 result
= g_hash_table_lookup(tmp
, name
);
2273 /* if result is still NULL here, no completion could be found */
2275 /* result is owned by the hash table and will be freed when the table will destroyed */
2280 /* This is very ugly but passing the pattern to ac_replace_specials() doesn't work because it is
2281 * modified when replacing a completion but the foreach function still passes the old pointer
2282 * to ac_replace_specials, so we use a global pointer outside of ac_replace_specials and
2283 * ac_complete_constructs. Any hints to improve this are welcome. */
2284 static GString
*snippets_global_pattern
= NULL
;
2286 static void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2290 g_return_if_fail(key
!= NULL
);
2291 g_return_if_fail(value
!= NULL
);
2293 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2295 utils_string_replace_all(snippets_global_pattern
, needle
, (gchar
*) value
);
2300 /* this only works with spaces only indentation on the lines */
2301 static void fix_line_indents(GeanyEditor
*editor
, gint line_start
, gint line_end
)
2303 ScintillaObject
*sci
= editor
->sci
;
2304 gint line
, cur_line
, cur_col
, pos
;
2306 /* get the line, col position as fixing indentation will move cursor to start of line */
2307 pos
= sci_get_current_position(sci
);
2308 cur_col
= sci_get_col_from_position(sci
, pos
);
2309 cur_line
= sci_get_current_line(sci
);
2311 for (line
= line_start
; line
<= line_end
; line
++)
2313 gint size
= sci_get_line_indentation(sci
, line
);
2315 /* set to 0 first to trigger proper indent creation */
2316 sci_set_line_indentation(sci
, line
, 0);
2317 sci_set_line_indentation(sci
, line
, size
);
2319 pos
= scintilla_send_message(sci
, SCI_FINDCOLUMN
, cur_line
, cur_col
);
2320 sci_set_current_position(sci
, pos
, FALSE
);
2324 static void replace_leading_tabs(GString
*str
, const gchar
*whitespace
)
2328 regmatch_t matches
[2];
2331 if (regcomp(®ex
, "^ *(\t)", 0) != 0)
2333 g_return_if_fail(FALSE
);
2338 if (regexec(®ex
, ptr
,
2339 G_N_ELEMENTS(matches
), matches
, 0) != 0)
2342 pos
= matches
[1].rm_so
;
2343 g_return_if_fail(pos
>= 0);
2344 pos
+= ptr
- str
->str
;
2345 g_string_erase(str
, pos
, 1);
2346 g_string_insert(str
, pos
, whitespace
);
2347 ptr
= str
->str
+ pos
+ strlen(whitespace
);
2353 /** Inserts text, replacing \\t tab chars (@c 0x9) and \\n newline chars (@c 0xA)
2354 * accordingly for the document.
2355 * - Leading tabs are replaced with the correct indentation.
2356 * - Non-leading tabs are replaced with spaces (except when using 'Tabs' indent type).
2357 * - Newline chars are replaced with the correct line ending string.
2358 * This is very useful for inserting code without having to handle the indent
2359 * type yourself (Tabs & Spaces mode can be tricky).
2360 * @param editor Editor.
2361 * @param text Intended as e.g. @c "if (foo)\n\tbar();".
2362 * @param insert_pos Document position to insert text at.
2363 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2364 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2365 * -1 to read the indent size from the line with @a insert_pos on it.
2366 * @param replace_newlines Whether to replace newlines. If
2367 * newlines have been replaced already, this should be false, to avoid errors e.g. on Windows.
2368 * @warning Make sure all \\t tab chars in @a text are intended as indent widths or alignment,
2369 * not hard tabs, as those won't be preserved.
2370 * @note This doesn't scroll the cursor in view afterwards. **/
2371 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2372 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2374 ScintillaObject
*sci
= editor
->sci
;
2375 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2379 const gchar cur_marker
[] = "__GEANY_CURSOR_MARKER__";
2380 const gchar
*eol
= editor_get_eol_char(editor
);
2381 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2383 g_return_if_fail(text
);
2384 g_return_if_fail(editor
!= NULL
);
2385 g_return_if_fail(insert_pos
>= 0);
2387 buf
= g_string_new(text
);
2389 if (cursor_index
>= 0)
2390 g_string_insert(buf
, cursor_index
, cur_marker
); /* remember cursor pos */
2392 if (newline_indent_size
== -1)
2394 /* count indent size up to insert_pos instead of asking sci
2395 * because there may be spaces after it */
2396 gchar
*tmp
= sci_get_line(sci
, line_start
);
2397 gint idx
= insert_pos
- sci_get_position_from_line(sci
, line_start
);
2399 newline_indent_size
= count_indent_size(editor
, tmp
);
2403 /* Add line indents (in spaces) */
2404 if (newline_indent_size
> 0)
2406 whitespace
= g_strnfill(newline_indent_size
, ' ');
2407 setptr(whitespace
, g_strconcat(eol
, whitespace
, NULL
));
2408 utils_string_replace_all(buf
, eol
, whitespace
);
2412 /* transform line endings */
2413 if (replace_newlines
)
2414 utils_string_replace_all(buf
, "\n", eol
);
2416 /* transform leading tabs into indent widths (in spaces) */
2417 whitespace
= g_strnfill(iprefs
->width
, ' ');
2418 replace_leading_tabs(buf
, whitespace
);
2419 /* remaining tabs are for alignment */
2420 if (iprefs
->type
!= GEANY_INDENT_TYPE_TABS
)
2421 utils_string_replace_all(buf
, "\t", whitespace
);
2424 sci_start_undo_action(sci
);
2426 if (cursor_index
>= 0)
2428 gint idx
= utils_strpos(buf
->str
, cur_marker
);
2430 g_string_erase(buf
, idx
, strlen(cur_marker
));
2432 sci_insert_text(sci
, insert_pos
, buf
->str
);
2433 sci_set_current_position(sci
, insert_pos
+ idx
, FALSE
);
2436 sci_insert_text(sci
, insert_pos
, buf
->str
);
2438 /* fixup indentation (very useful for Tabs & Spaces indent type) */
2439 line_end
= sci_get_line_from_position(sci
, insert_pos
+ buf
->len
);
2440 fix_line_indents(editor
, line_start
, line_end
);
2441 snippet_cursor_insert_pos
= sci_get_current_position(sci
);
2443 sci_end_undo_action(sci
);
2444 g_string_free(buf
, TRUE
);
2448 /* Move the cursor to the next specified cursor position in an inserted snippet.
2449 * Can, and should, be optimized to give better results */
2450 void editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2452 ScintillaObject
*sci
= editor
->sci
;
2453 gint current_pos
= sci_get_current_position(sci
);
2455 if (snippet_offsets
&& !g_queue_is_empty(snippet_offsets
))
2459 offset
= GPOINTER_TO_INT(g_queue_pop_head(snippet_offsets
));
2460 if (current_pos
> snippet_cursor_insert_pos
)
2461 snippet_cursor_insert_pos
= offset
+ current_pos
;
2463 snippet_cursor_insert_pos
+= offset
;
2465 sci_set_current_position(sci
, snippet_cursor_insert_pos
, TRUE
);
2474 static gssize
snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
,
2477 gssize cur_index
= -1;
2479 gint i
, tmp_pos
, whitespace_len
, nl_count
= 0;
2480 GHashTable
*specials
;
2481 GList
*temp_list
= NULL
;
2482 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2483 gint cursor_steps
, old_cursor
= 0;
2485 /* replace 'special' completions */
2486 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2487 if (G_LIKELY(specials
!= NULL
))
2489 /* ugly hack using global_pattern */
2490 snippets_global_pattern
= pattern
;
2491 g_hash_table_foreach(specials
, snippets_replace_specials
, NULL
);
2494 /* replace any template {foo} wildcards */
2495 templates_replace_common(pattern
, editor
->document
->file_name
, editor
->document
->file_type
, NULL
);
2497 /* transform other wildcards */
2498 /* convert to %newlines%, else we get endless loops */
2499 utils_string_replace_all(pattern
, "\n", "%newline%");
2501 /* if spaces are used, replaces all tabs with %ws%, which is later replaced
2502 * by real whitespace characters
2503 * otherwise replace all %ws% by \t, which will be replaced later by tab
2505 * this makes seperating between tab and spaces intentation pretty easy */
2506 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
2507 utils_string_replace_all(pattern
, "\t", "%ws%");
2509 utils_string_replace_all(pattern
, "%ws%", "\t");
2511 whitespace
= g_strnfill(iprefs
->width
, ' '); /* use spaces for indentation, will be fixed up */
2512 whitespace_len
= strlen(whitespace
);
2514 while ((cursor_steps
= utils_strpos(pattern
->str
, "%cursor%")) >= 0)
2516 /* replace every %newline% (up to next %cursor%) with EOL,
2517 * and update cursor_steps after */
2518 while ((tmp_pos
= utils_strpos(pattern
->str
, "%newline%")) < cursor_steps
&& tmp_pos
!= -1)
2521 utils_string_replace_first(pattern
, "%newline%", editor_get_eol_char(editor
));
2522 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2524 /* replace every %ws% (up to next %cursor%) with whitespaces,
2525 * and update cursor_steps after */
2526 while ((tmp_pos
= utils_strpos(pattern
->str
, "%ws%")) < cursor_steps
&& tmp_pos
!= -1)
2528 utils_string_replace_first(pattern
, "%ws%", whitespace
);
2529 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2531 /* finally replace the next %cursor% */
2532 utils_string_replace_first(pattern
, "%cursor%", "");
2534 /* modify cursor_steps to take indentation count and type into account */
2536 /* We're saving the relative offset to each cursor position in a simple
2537 * linked list, including indentations between them. */
2540 cursor_steps
+= (nl_count
* indent_size
);
2541 temp_list
= g_list_prepend(temp_list
, GINT_TO_POINTER(cursor_steps
- old_cursor
));
2546 cur_index
= cursor_steps
;
2548 old_cursor
= cursor_steps
;
2550 /* replace remaining %ws% and %newline% which may occur after the last %cursor% */
2551 utils_string_replace_all(pattern
, "%newline%", editor_get_eol_char(editor
));
2552 utils_string_replace_all(pattern
, "%ws%", whitespace
);
2556 /* Bug: {ob}pc{cb} will be replaced by % too */
2557 templates_replace_valist(pattern
, "{pc}", "%", NULL
);
2559 /* We put the cursor positions for the most recent
2560 * parsed snippet first, followed by any remaining positions */
2566 temp_list
= g_list_reverse(temp_list
);
2567 foreach_list(node
, temp_list
)
2568 g_queue_push_nth(snippet_offsets
, node
->data
, i
++);
2570 /* limit length of queue */
2571 while (g_queue_get_length(snippet_offsets
) > 20)
2572 g_queue_pop_tail(snippet_offsets
);
2574 g_list_free(temp_list
);
2577 cur_index
= pattern
->len
;
2583 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2585 ScintillaObject
*sci
= editor
->sci
;
2587 const gchar
*completion
;
2589 gint ft_id
= editor
->document
->file_type
->id
;
2591 str
= g_strdup(word
);
2594 completion
= snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
);
2595 if (completion
== NULL
)
2601 /* remove the typed word, it will be added again by the used auto completion
2602 * (not really necessary but this makes the auto completion more flexible,
2603 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2604 str_len
= strlen(str
);
2605 sci_set_selection_start(sci
, pos
- str_len
);
2606 sci_set_selection_end(sci
, pos
);
2607 sci_replace_sel(sci
, "");
2608 pos
-= str_len
; /* pos has changed while deleting */
2610 editor_insert_snippet(editor
, pos
, completion
);
2611 sci_scroll_caret(sci
);
2618 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2620 gint line
= sci_get_line_from_position(sci
, pos
);
2623 /* skip any trailing spaces */
2626 c
= sci_get_char_at(sci
, pos
);
2627 if (c
== ' ' || c
== '\t')
2633 return (pos
== sci_get_line_end_position(sci
, line
));
2637 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2639 gboolean result
= FALSE
;
2642 ScintillaObject
*sci
;
2644 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2647 if (sci_has_selection(sci
))
2649 /* return if we are editing an existing line (chars on right of cursor) */
2650 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2651 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_space
&&
2652 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2655 wc
= snippets_find_completion_by_name("Special", "wordchars");
2656 word
= editor_read_word_stem(editor
, pos
, wc
);
2658 /* prevent completion of "for " */
2660 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2662 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2663 result
= snippets_complete_constructs(editor
, pos
, word
);
2664 sci_end_undo_action(sci
);
2666 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2672 void editor_show_macro_list(GeanyEditor
*editor
)
2676 if (editor
== NULL
|| editor
->document
->file_type
== NULL
)
2679 words
= symbols_get_macro_list(editor
->document
->file_type
->lang
);
2683 SSM(editor
->sci
, SCI_USERLISTSHOW
, 1, (sptr_t
) words
->str
);
2684 g_string_free(words
, TRUE
);
2688 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2690 ScintillaObject
*sci
= editor
->sci
;
2691 gchar
*to_insert
= NULL
;
2695 const gchar
*gt
= ">";
2696 /* if there is already a '>' behind the cursor, don't add it */
2697 if (sci_get_char_at(sci
, pos
) == '>')
2700 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2703 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2705 sci_start_undo_action(sci
);
2706 sci_replace_sel(sci
, to_insert
);
2708 sci_set_selection(sci
, pos
, pos
);
2709 sci_end_undo_action(sci
);
2715 * (stolen from anjuta and heavily modified)
2716 * This routine will auto complete XML or HTML tags that are still open by closing them
2717 * @param ch The character we are dealing with, currently only works with the '>' character
2718 * @return True if handled, false otherwise
2720 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2722 ScintillaObject
*sci
= editor
->sci
;
2723 gint lexer
= sci_get_lexer(sci
);
2724 gint min
, size
, style
;
2725 gchar
*str_found
, sel
[512];
2726 gboolean result
= FALSE
;
2728 /* If the user has turned us off, quit now.
2729 * This may make sense only in certain languages */
2730 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2733 /* return if we are inside any embedded script */
2734 style
= sci_get_style_at(sci
, pos
);
2735 if (style
> SCE_H_XCCOMMENT
&& ! highlighting_is_string_style(lexer
, style
))
2738 /* if ch is /, check for </, else quit */
2739 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2742 /* Grab the last 512 characters or so */
2743 min
= pos
- (sizeof(sel
) - 1);
2744 if (min
< 0) min
= 0;
2747 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2749 sci_get_text_range(sci
, min
, pos
, sel
);
2750 sel
[sizeof(sel
) - 1] = '\0';
2752 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2753 /* User typed something like "<br/>" */
2758 size
-= 2; /* skip </ */
2759 str_found
= utils_find_open_xml_tag(sel
, size
);
2761 if (lexer
== SCLEX_HTML
&& utils_is_short_html_tag(str_found
))
2765 else if (NZV(str_found
))
2767 insert_closing_tag(editor
, pos
, ch
, str_found
);
2775 /* like sci_get_line_indentation(), but for a string. */
2776 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2779 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2782 g_return_val_if_fail(base_indent
, 0);
2784 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2802 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2805 gchar
*str_begin
, *str_end
, *co
, *cc
;
2808 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2810 eol
= editor_get_eol_char(editor
);
2811 co
= editor
->document
->file_type
->comment_open
;
2812 cc
= editor
->document
->file_type
->comment_close
;
2813 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2814 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2816 /* insert the comment strings */
2817 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2818 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2819 sci_insert_text(editor
->sci
, line_len
, str_end
);
2826 static void real_uncomment_multiline(GeanyEditor
*editor
)
2828 /* find the beginning of the multi line comment */
2829 gint pos
, line
, len
, x
;
2833 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2834 doc
= editor
->document
;
2836 /* remove comment open chars */
2837 pos
= document_find_text(doc
, doc
->file_type
->comment_open
, 0, TRUE
, FALSE
, NULL
);
2838 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2840 /* check whether the line is empty and can be deleted */
2841 line
= sci_get_line_from_position(editor
->sci
, pos
);
2842 len
= sci_get_line_length(editor
->sci
, line
);
2843 linebuf
= sci_get_line(editor
->sci
, line
);
2845 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2846 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2849 /* remove comment close chars */
2850 pos
= document_find_text(doc
, doc
->file_type
->comment_close
, 0, FALSE
, FALSE
, NULL
);
2851 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2853 /* check whether the line is empty and can be deleted */
2854 line
= sci_get_line_from_position(editor
->sci
, pos
);
2855 len
= sci_get_line_length(editor
->sci
, line
);
2856 linebuf
= sci_get_line(editor
->sci
, line
);
2858 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2859 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2864 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2866 gint lexer
= sci_get_lexer(editor
->sci
);
2869 /* List only those lexers which support multi line comments */
2875 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2876 style_comment
= SCE_HPHP_COMMENT
;
2878 style_comment
= SCE_H_COMMENT
;
2881 case SCLEX_HASKELL
: style_comment
= SCE_HA_COMMENTBLOCK
; break;
2882 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2883 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2884 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2885 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2886 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2887 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2888 default: style_comment
= SCE_C_COMMENT
;
2891 return style_comment
;
2895 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2896 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2897 * it returns just 1 */
2898 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2900 gint first_line
, last_line
, eol_char_len
;
2901 gint x
, i
, line_start
, line_len
;
2902 gint sel_start
, sel_end
;
2905 gchar sel
[256], *co
, *cc
;
2906 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2909 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
2912 { /* use selection or current line */
2913 sel_start
= sci_get_selection_start(editor
->sci
);
2914 sel_end
= sci_get_selection_end(editor
->sci
);
2916 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
2917 /* Find the last line with chars selected (not EOL char) */
2918 last_line
= sci_get_line_from_position(editor
->sci
,
2919 sel_end
- editor_get_eol_char_len(editor
));
2920 last_line
= MAX(first_line
, last_line
);
2924 first_line
= last_line
= line
;
2925 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
2928 ft
= editor
->document
->file_type
;
2929 eol_char_len
= editor_get_eol_char_len(editor
);
2931 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
2932 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
2933 if (ft
->id
== GEANY_FILETYPES_PHP
)
2935 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2936 ft
= filetypes
[GEANY_FILETYPES_XML
];
2939 co
= ft
->comment_open
;
2940 cc
= ft
->comment_close
;
2944 co_len
= strlen(co
);
2948 sci_start_undo_action(editor
->sci
);
2950 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
2954 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2955 line_len
= sci_get_line_length(editor
->sci
, i
);
2958 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- eol_char_len
);
2961 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2962 sel
[buf_len
] = '\0';
2964 while (isspace(sel
[x
])) x
++;
2966 /* to skip blank lines */
2967 if (x
< line_len
&& sel
[x
] != '\0')
2969 /* use single line comment */
2970 if (cc
== NULL
|| strlen(cc
) == 0)
2976 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2977 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
2978 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
2985 if (strncmp(sel
+ x
, co
, co_len
) != 0)
2989 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
2990 sci_replace_sel(editor
->sci
, "");
2993 /* use multi line comment */
2998 /* skip lines which are already comments */
2999 style_comment
= get_multiline_comment_style(editor
, line_start
);
3000 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3002 real_uncomment_multiline(editor
);
3006 /* break because we are already on the last line */
3012 sci_end_undo_action(editor
->sci
);
3014 /* restore selection if there is one
3015 * but don't touch the selection if caller is editor_do_comment_toggle */
3016 if (! toggle
&& sel_start
< sel_end
)
3020 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
3021 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
3025 gint eol_len
= editor_get_eol_char_len(editor
);
3026 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
3027 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
3035 void editor_do_comment_toggle(GeanyEditor
*editor
)
3037 gint first_line
, last_line
, eol_char_len
;
3038 gint x
, i
, line_start
, line_len
, first_line_start
;
3039 gint sel_start
, sel_end
;
3040 gint count_commented
= 0, count_uncommented
= 0;
3041 gchar sel
[256], *co
, *cc
;
3042 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3043 gboolean first_line_was_comment
= FALSE
;
3045 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
3048 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3050 sel_start
= sci_get_selection_start(editor
->sci
);
3051 sel_end
= sci_get_selection_end(editor
->sci
);
3053 ft
= editor
->document
->file_type
;
3054 eol_char_len
= editor_get_eol_char_len(editor
);
3056 first_line
= sci_get_line_from_position(editor
->sci
,
3057 sci_get_selection_start(editor
->sci
));
3058 /* Find the last line with chars selected (not EOL char) */
3059 last_line
= sci_get_line_from_position(editor
->sci
,
3060 sci_get_selection_end(editor
->sci
) - editor_get_eol_char_len(editor
));
3061 last_line
= MAX(first_line
, last_line
);
3063 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
3064 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3065 if (ft
->id
== GEANY_FILETYPES_PHP
)
3067 if (! is_style_php(sci_get_style_at(editor
->sci
, first_line_start
)))
3068 ft
= filetypes
[GEANY_FILETYPES_XML
];
3071 co
= ft
->comment_open
;
3072 cc
= ft
->comment_close
;
3076 co_len
= strlen(co
);
3080 sci_start_undo_action(editor
->sci
);
3082 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3086 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3087 line_len
= sci_get_line_length(editor
->sci
, i
);
3090 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- eol_char_len
);
3093 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3094 sel
[buf_len
] = '\0';
3096 while (isspace(sel
[x
])) x
++;
3098 /* use single line comment */
3099 if (cc
== NULL
|| strlen(cc
) == 0)
3101 gboolean do_continue
= FALSE
;
3104 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
3105 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
3110 if (do_continue
&& i
== first_line
)
3111 first_line_was_comment
= TRUE
;
3115 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
3119 /* we are still here, so the above lines were not already comments, so comment it */
3120 editor_do_comment(editor
, i
, TRUE
, TRUE
);
3123 /* use multi line comment */
3128 /* skip lines which are already comments */
3129 style_comment
= get_multiline_comment_style(editor
, line_start
);
3130 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3132 real_uncomment_multiline(editor
);
3133 count_uncommented
++;
3137 real_comment_multiline(editor
, line_start
, last_line
);
3141 /* break because we are already on the last line */
3147 sci_end_undo_action(editor
->sci
);
3151 /* restore selection if there is one */
3152 if (sel_start
< sel_end
)
3156 gint a
= (first_line_was_comment
) ? - co_len
: co_len
;
3158 /* don't modify sel_start when the selection starts within indentation */
3159 read_indent(editor
, sel_start
);
3160 if ((sel_start
- first_line_start
) <= (gint
) strlen(indent
))
3163 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
3164 sci_set_selection_end(editor
->sci
, sel_end
+
3165 (count_commented
* co_len
) - (count_uncommented
* co_len
));
3169 gint eol_len
= editor_get_eol_char_len(editor
);
3170 if (count_uncommented
> 0)
3172 sci_set_selection_start(editor
->sci
, sel_start
- co_len
+ eol_len
);
3173 sci_set_selection_end(editor
->sci
, sel_end
- co_len
+ eol_len
);
3175 else if (count_commented
> 0)
3177 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
- eol_len
);
3178 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
- eol_len
);
3182 else if (count_uncommented
> 0)
3184 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
3185 sci_set_current_position(editor
->sci
, sel_start
- co_len
+ eol_len
, TRUE
);
3187 else if (count_commented
> 0)
3189 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
3190 sci_set_current_position(editor
->sci
, sel_start
+ co_len
- eol_len
, TRUE
);
3195 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
3196 void editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
)
3198 gint first_line
, last_line
, eol_char_len
;
3199 gint x
, i
, line_start
, line_len
;
3200 gint sel_start
, sel_end
, co_len
;
3201 gchar sel
[256], *co
, *cc
;
3202 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3205 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3208 { /* use selection or current line */
3209 sel_start
= sci_get_selection_start(editor
->sci
);
3210 sel_end
= sci_get_selection_end(editor
->sci
);
3212 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3213 /* Find the last line with chars selected (not EOL char) */
3214 last_line
= sci_get_line_from_position(editor
->sci
,
3215 sel_end
- editor_get_eol_char_len(editor
));
3216 last_line
= MAX(first_line
, last_line
);
3220 first_line
= last_line
= line
;
3221 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3224 ft
= editor
->document
->file_type
;
3225 eol_char_len
= editor_get_eol_char_len(editor
);
3227 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
3228 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3229 if (ft
->id
== GEANY_FILETYPES_PHP
)
3231 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
3232 ft
= filetypes
[GEANY_FILETYPES_XML
];
3235 co
= ft
->comment_open
;
3236 cc
= ft
->comment_close
;
3240 co_len
= strlen(co
);
3244 sci_start_undo_action(editor
->sci
);
3246 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3250 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3251 line_len
= sci_get_line_length(editor
->sci
, i
);
3254 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- eol_char_len
);
3257 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3258 sel
[buf_len
] = '\0';
3260 while (isspace(sel
[x
])) x
++;
3262 /* to skip blank lines */
3263 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3265 /* use single line comment */
3266 if (cc
== NULL
|| strlen(cc
) == 0)
3268 gint start
= line_start
;
3271 if (ft
->comment_use_indent
)
3272 start
= line_start
+ x
;
3276 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3277 sci_insert_text(editor
->sci
, start
, text
);
3281 sci_insert_text(editor
->sci
, start
, co
);
3283 /* use multi line comment */
3288 /* skip lines which are already comments */
3289 style_comment
= get_multiline_comment_style(editor
, line_start
);
3290 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3293 real_comment_multiline(editor
, line_start
, last_line
);
3295 /* break because we are already on the last line */
3301 sci_end_undo_action(editor
->sci
);
3303 /* restore selection if there is one
3304 * but don't touch the selection if caller is editor_do_comment_toggle */
3305 if (! toggle
&& sel_start
< sel_end
)
3309 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3310 sci_set_selection_end(editor
->sci
, sel_end
+ ((i
- first_line
) * co_len
));
3314 gint eol_len
= editor_get_eol_char_len(editor
);
3315 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3316 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3322 static gboolean brace_timeout_active
= FALSE
;
3324 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3326 GeanyDocument
*doc
= document_get_current();
3327 GeanyEditor
*editor
;
3328 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3329 gint end_pos
, cur_pos
;
3331 brace_timeout_active
= FALSE
;
3335 editor
= doc
->editor
;
3336 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3338 if (cur_pos
!= brace_pos
)
3341 if (cur_pos
!= brace_pos
)
3343 /* we have moved past the original brace_pos, but after the timeout
3344 * we may now be on a new brace, so check again */
3345 editor_highlight_braces(editor
, cur_pos
);
3349 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3351 editor_highlight_braces(editor
, cur_pos
);
3354 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3358 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3359 sci_get_col_from_position(editor
->sci
, end_pos
));
3360 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3361 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3365 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3366 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3372 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3374 gint brace_pos
= cur_pos
- 1;
3376 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3377 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3379 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3382 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3387 if (!brace_timeout_active
)
3389 brace_timeout_active
= TRUE
;
3390 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3391 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3396 static gboolean
in_block_comment(gint lexer
, gint style
)
3402 return (style
== SCE_C_COMMENT
||
3403 style
== SCE_C_COMMENTDOC
);
3406 return (style
== SCE_PAS_COMMENT
||
3407 style
== SCE_PAS_COMMENT2
);
3410 return (style
== SCE_D_COMMENT
||
3411 style
== SCE_D_COMMENTDOC
||
3412 style
== SCE_D_COMMENTNESTED
);
3415 return (style
== SCE_HPHP_COMMENT
);
3418 return (style
== SCE_CSS_COMMENT
);
3426 static gboolean
is_comment_char(gchar c
, gint lexer
)
3428 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3438 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3440 ScintillaObject
*sci
= editor
->sci
;
3441 gint indent_pos
, style
;
3442 gint lexer
= sci_get_lexer(sci
);
3444 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3445 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3446 style
= sci_get_style_at(sci
, indent_pos
);
3447 if (!in_block_comment(lexer
, style
))
3450 /* Check whether the comment block continues on this line */
3451 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3452 if (sci_get_style_at(sci
, indent_pos
) == style
)
3454 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3455 /* the type of comment, '*' (C/C++/Java), '+' and the others (D) */
3456 gchar
*continuation
= "*";
3457 gchar
*whitespace
= ""; /* to hold whitespace if needed */
3459 gint len
= strlen(previous_line
);
3462 /* find and stop at end of multi line comment */
3464 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3465 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3467 gint indent_len
, indent_width
;
3469 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3470 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3471 indent_width
= editor_get_indent_prefs(editor
)->width
;
3473 /* if there is one too many spaces, delete the last space,
3474 * to return to the indent used before the multiline comment was started. */
3475 if (indent_len
% indent_width
== 1)
3476 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3477 g_free(previous_line
);
3480 /* check whether we are on the second line of multi line comment */
3482 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3485 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3486 { /* we are on the second line of a multi line comment, so we have to insert white space */
3490 if (G_UNLIKELY(style
== SCE_D_COMMENTNESTED
))
3491 continuation
= "+"; /* for nested comments in D */
3493 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3494 sci_add_text(sci
, result
);
3497 g_free(previous_line
);
3503 static gboolean
editor_lexer_is_c_like(gint lexer
)
3518 /* Returns: -1 if lexer doesn't support type keywords */
3519 gint
editor_lexer_get_type_keyword_idx(gint lexer
)
3533 /* inserts a three-line comment at one line above current cursor position */
3534 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3540 gboolean have_multiline_comment
= FALSE
;
3543 g_return_if_fail(editor
!= NULL
&&
3544 editor
->document
->file_type
!= NULL
&& editor
->document
->file_type
->comment_open
!= NULL
);
3546 sci_start_undo_action(editor
->sci
);
3548 doc
= editor
->document
;
3550 if (doc
->file_type
->comment_close
!= NULL
&& strlen(doc
->file_type
->comment_close
) > 0)
3551 have_multiline_comment
= TRUE
;
3553 /* insert three lines one line above of the current position */
3554 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3555 pos
= sci_get_position_from_line(editor
->sci
, line
);
3557 /* use the indent on the current line but only when comment indentation is used
3558 * and we don't have multi line comment characters */
3559 if (editor
->auto_indent
&&
3560 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3562 read_indent(editor
, editor_info
.click_pos
);
3563 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3564 text_len
= strlen(text
);
3568 text
= g_strdup("\n\n\n");
3571 sci_insert_text(editor
->sci
, pos
, text
);
3574 /* select the inserted lines for commenting */
3575 sci_set_selection_start(editor
->sci
, pos
);
3576 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3578 editor_do_comment(editor
, -1, TRUE
, FALSE
);
3580 /* set the current position to the start of the first inserted line */
3581 pos
+= strlen(doc
->file_type
->comment_open
);
3583 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3584 if (have_multiline_comment
)
3587 pos
+= strlen(indent
);
3589 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3590 /* reset the selection */
3591 sci_set_anchor(editor
->sci
, pos
);
3593 sci_end_undo_action(editor
->sci
);
3597 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3598 * Scroll the view to make line appear at percent_of_view.
3599 * line can be -1 to use the current position. */
3600 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3605 g_return_if_fail(editor
!= NULL
);
3607 wid
= GTK_WIDGET(editor
->sci
);
3609 if (! wid
->window
|| ! gdk_window_is_viewable(wid
->window
))
3610 return; /* prevent gdk_window_scroll warning */
3613 line
= sci_get_current_line(editor
->sci
);
3615 /* sci 'visible line' != doc line number because of folding and line wrapping */
3616 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3617 * SCI_DOCLINEFROMVISIBLE for vis1. */
3618 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3619 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3620 line
= line
- los
* percent_of_view
;
3621 SSM(editor
->sci
, SCI_SETFIRSTVISIBLELINE
, line
, 0);
3622 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3626 /* creates and inserts one tab or whitespace of the amount of the tab width */
3627 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3630 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3632 g_return_if_fail(editor
!= NULL
);
3634 switch (iprefs
.type
)
3636 case GEANY_INDENT_TYPE_TABS
:
3637 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3639 case GEANY_INDENT_TYPE_SPACES
:
3640 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3641 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3644 text
= get_whitespace(&iprefs
, iprefs
.width
);
3645 sci_add_text(editor
->sci
, text
);
3650 void editor_select_word(GeanyEditor
*editor
)
3656 g_return_if_fail(editor
!= NULL
);
3658 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3659 start
= SSM(editor
->sci
, SCI_WORDSTARTPOSITION
, pos
, TRUE
);
3660 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, TRUE
);
3662 if (start
== end
) /* caret in whitespaces sequence */
3664 /* look forward but reverse the selection direction,
3665 * so the caret end up stay as near as the original position. */
3666 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, FALSE
);
3667 start
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, end
, TRUE
);
3672 sci_set_selection(editor
->sci
, start
, end
);
3676 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3677 * when those lines have no selection (cursor at start of line). */
3678 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3680 gint start
, end
, line
;
3682 g_return_if_fail(editor
!= NULL
);
3684 start
= sci_get_selection_start(editor
->sci
);
3685 end
= sci_get_selection_end(editor
->sci
);
3687 /* check if whole lines are already selected */
3688 if (! extra_line
&& start
!= end
&&
3689 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3690 sci_get_col_from_position(editor
->sci
, end
) == 0)
3693 line
= sci_get_line_from_position(editor
->sci
, start
);
3694 start
= sci_get_position_from_line(editor
->sci
, line
);
3696 line
= sci_get_line_from_position(editor
->sci
, end
);
3697 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3699 sci_set_selection(editor
->sci
, start
, end
);
3703 static gboolean
sci_is_blank_line(ScintillaObject
*sci
, gint line
)
3705 return sci_get_line_indent_position(sci
, line
) ==
3706 sci_get_line_end_position(sci
, line
);
3710 /* Returns first line of paragraph for GTK_DIR_UP, line after paragraph
3711 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3712 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3715 ScintillaObject
*sci
= editor
->sci
;
3717 /* first check current line and return -1 if it is empty to skip creating of a selection */
3718 if (sci_is_blank_line(sci
, line
))
3721 if (direction
== GTK_DIR_UP
)
3731 /* start of document */
3735 if (line
== sci_get_line_count(sci
))
3738 if (sci_is_blank_line(sci
, line
))
3740 /* return line paragraph starts on */
3741 if (direction
== GTK_DIR_UP
)
3750 void editor_select_paragraph(GeanyEditor
*editor
)
3752 gint pos_start
, pos_end
, line_start
, line_found
;
3754 g_return_if_fail(editor
!= NULL
);
3756 line_start
= sci_get_current_line(editor
->sci
);
3758 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3759 if (line_found
== -1)
3762 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3764 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3765 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3767 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3771 /* Returns first line of block for GTK_DIR_UP, line after block
3772 * ends for GTK_DIR_DOWN or -1 if called on an empty line. */
3773 static gint
find_block_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3776 ScintillaObject
*sci
= editor
->sci
;
3778 /* first check current line and return -1 if it is empty to skip creating of a selection */
3779 if (sci_is_blank_line(sci
, line
))
3782 if (direction
== GTK_DIR_UP
)
3787 ind
= sci_get_line_indentation(sci
, line
);
3793 /* start of document */
3797 if (line
== sci_get_line_count(sci
))
3800 if (sci_get_line_indentation(sci
, line
) != ind
||
3801 sci_is_blank_line(sci
, line
))
3803 /* return line block starts on */
3804 if (direction
== GTK_DIR_UP
)
3813 void editor_select_indent_block(GeanyEditor
*editor
)
3815 gint pos_start
, pos_end
, line_start
, line_found
;
3817 g_return_if_fail(editor
!= NULL
);
3819 line_start
= sci_get_current_line(editor
->sci
);
3821 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_UP
);
3822 if (line_found
== -1)
3825 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3827 line_found
= find_block_stop(editor
, line_start
, GTK_DIR_DOWN
);
3828 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3830 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3834 /* simple indentation to indent the current line with the same indent as the previous one */
3835 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
3837 gint i
, sel_start
= 0, sel_end
= 0;
3839 /* get previous line and use it for read_indent to use that line
3840 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
3841 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
3843 for (i
= first_line
; i
<= last_line
; i
++)
3845 /* skip the first line or if the indentation of the previous and current line are equal */
3847 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
3848 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
3851 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3852 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3853 if (sel_start
< sel_end
)
3855 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
3856 sci_replace_sel(editor
->sci
, "");
3858 sci_insert_text(editor
->sci
, sel_start
, indent
);
3863 /* simple indentation to indent the current line with the same indent as the previous one */
3864 void editor_smart_line_indentation(GeanyEditor
*editor
, gint pos
)
3866 gint first_line
, last_line
;
3867 gint first_sel_start
, first_sel_end
;
3868 ScintillaObject
*sci
;
3870 g_return_if_fail(editor
!= NULL
);
3874 first_sel_start
= sci_get_selection_start(sci
);
3875 first_sel_end
= sci_get_selection_end(sci
);
3877 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
3878 /* Find the last line with chars selected (not EOL char) */
3879 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
3880 last_line
= MAX(first_line
, last_line
);
3883 pos
= first_sel_start
;
3885 sci_start_undo_action(sci
);
3887 smart_line_indentation(editor
, first_line
, last_line
);
3889 /* set cursor position if there was no selection */
3890 if (first_sel_start
== first_sel_end
)
3892 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
3894 /* use indent position as user may wish to change indentation afterwards */
3895 sci_set_current_position(sci
, indent_pos
, FALSE
);
3899 /* fully select all the lines affected */
3900 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
3901 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
3904 sci_end_undo_action(sci
);
3908 /* increase / decrease current line or selection by one space */
3909 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
3911 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
3912 gint sel_start
, sel_end
, first_line_offset
= 0;
3914 g_return_if_fail(editor
!= NULL
);
3916 sel_start
= sci_get_selection_start(editor
->sci
);
3917 sel_end
= sci_get_selection_end(editor
->sci
);
3919 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3920 /* Find the last line with chars selected (not EOL char) */
3921 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
3922 last_line
= MAX(first_line
, last_line
);
3927 sci_start_undo_action(editor
->sci
);
3929 for (i
= first_line
; i
<= last_line
; i
++)
3931 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3934 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3935 /* searching backwards for a space to remove */
3936 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
3939 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
3941 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
3942 sci_replace_sel(editor
->sci
, "");
3944 if (i
== first_line
)
3945 first_line_offset
= -1;
3950 sci_insert_text(editor
->sci
, indentation_end
, " ");
3952 if (i
== first_line
)
3953 first_line_offset
= 1;
3957 /* set cursor position */
3958 if (sel_start
< sel_end
)
3960 gint start
= sel_start
+ first_line_offset
;
3961 if (first_line_offset
< 0)
3962 start
= MAX(sel_start
+ first_line_offset
,
3963 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
3965 sci_set_selection_start(editor
->sci
, start
);
3966 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
3969 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
3971 sci_end_undo_action(editor
->sci
);
3975 void editor_finalize()
3977 scintilla_release_resources();
3981 /* wordchars: NULL or a string containing characters to match a word.
3982 * Returns: the current selection or the current word. */
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)
3992 gint len
= sci_get_selected_text_length(editor
->sci
);
3994 s
= g_malloc(len
+ 1);
3995 sci_get_selected_text(editor
->sci
, s
);
3997 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
3998 { /* use the word at current cursor position */
3999 gchar word
[GEANY_MAX_WORD_LENGTH
];
4001 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
4002 if (word
[0] != '\0')
4009 /* Note: Usually the line should be made visible (not folded) before calling this.
4010 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4011 * outside the *vertical* view.
4012 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4013 * sci_scroll_caret() when this returns TRUE. */
4014 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4018 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4020 /* If line is wrapped the result may occur on another virtual line than the first and may be
4021 * still hidden, so increase the line number to check for the next document line */
4022 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4025 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4026 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4027 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4029 return (line
>= vis1
&& line
< vis1
+ los
);
4033 /* If the current line is outside the current view window, scroll the line
4034 * so it appears at percent_of_view. */
4035 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4039 g_return_if_fail(editor
!= NULL
);
4041 line
= sci_get_current_line(editor
->sci
);
4043 /* unfold maybe folded results */
4044 sci_ensure_line_is_visible(editor
->sci
, line
);
4046 /* scroll the line if it's off screen */
4047 if (! editor_line_in_view(editor
, line
))
4048 editor
->scroll_percent
= percent_of_view
;
4050 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4055 * Deletes all currently set indicators in the @a editor window.
4056 * Error indicators (red squiggly underlines) and usual line markers are removed.
4058 * @param editor The editor to operate on.
4060 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4062 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4063 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4068 * Deletes all currently set indicators matching @a indic in the @a editor window.
4070 * @param editor The editor to operate on.
4071 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4075 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4079 g_return_if_fail(editor
!= NULL
);
4081 last_pos
= sci_get_length(editor
->sci
);
4084 sci_indicator_set(editor
->sci
, indic
);
4085 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4091 * Sets an indicator @a indic on @a line.
4092 * Whitespace at the start and the end of the line is not marked.
4094 * @param editor The editor to operate on.
4095 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4096 * @param line The line number which should be marked.
4100 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4106 g_return_if_fail(editor
!= NULL
);
4107 g_return_if_fail(line
>= 0);
4109 start
= sci_get_position_from_line(editor
->sci
, line
);
4110 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4112 /* skip blank lines */
4113 if ((start
+ 1) == end
||
4115 sci_get_line_length(editor
->sci
, line
) == editor_get_eol_char_len(editor
))
4121 linebuf
= sci_get_line(editor
->sci
, line
);
4123 /* don't set the indicator on whitespace */
4124 while (isspace(linebuf
[i
]))
4126 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4133 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4138 * Sets an indicator on the range specified by @a start and @a end.
4139 * No error checking or whitespace removal is performed, this should be done by the calling
4140 * function if necessary.
4142 * @param editor The editor to operate on.
4143 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4144 * @param start The starting position for the marker.
4145 * @param end The ending position for the marker.
4149 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4151 g_return_if_fail(editor
!= NULL
);
4155 sci_indicator_set(editor
->sci
, indic
);
4156 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4160 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4161 * the replacement will also start with 0x... */
4162 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4164 g_return_if_fail(editor
!= NULL
);
4166 if (sci_has_selection(editor
->sci
))
4168 gint start
= sci_get_selection_start(editor
->sci
);
4169 const gchar
*replacement
= colour
;
4171 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4172 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4174 sci_set_selection_start(editor
->sci
, start
+ 2);
4175 sci_set_selection_end(editor
->sci
, start
+ 8);
4176 replacement
++; /* skip the leading "0x" */
4178 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4179 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4180 replacement
++; /* so skip the '#' to only replace the colour value */
4182 sci_replace_sel(editor
->sci
, replacement
);
4185 sci_add_text(editor
->sci
, colour
);
4190 * Retrieves the end of line characters mode (LF, CR/LF, CR) in the given editor.
4191 * If @a editor is @c NULL, the default end of line characters are used.
4193 * @param editor The editor to operate on, or @c NULL to query the default value.
4194 * @return The used end of line characters mode.
4198 gint
editor_get_eol_char_mode(GeanyEditor
*editor
)
4200 gint mode
= file_prefs
.default_eol_character
;
4203 mode
= sci_get_eol_mode(editor
->sci
);
4210 * Retrieves the localized name (for displaying) of the used end of line characters
4211 * (LF, CR/LF, CR) in the given editor.
4212 * If @a editor is @c NULL, the default end of line characters are used.
4214 * @param editor The editor to operate on, or @c NULL to query the default value.
4215 * @return The name of the end of line characters.
4219 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4221 gint mode
= file_prefs
.default_eol_character
;
4224 mode
= sci_get_eol_mode(editor
->sci
);
4226 return utils_get_eol_name(mode
);
4231 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4232 * If @a editor is @c NULL, the default end of line characters are used.
4233 * The returned value is 1 for CR and LF and 2 for CR/LF.
4235 * @param editor The editor to operate on, or @c NULL to query the default value.
4236 * @return The length of the end of line characters.
4240 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4242 gint mode
= file_prefs
.default_eol_character
;
4245 mode
= sci_get_eol_mode(editor
->sci
);
4249 case SC_EOL_CRLF
: return 2; break;
4250 default: return 1; break;
4256 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4257 * If @a editor is @c NULL, the default end of line characters are used.
4258 * The returned value is either "\n", "\r\n" or "\r".
4260 * @param editor The editor to operate on, or @c NULL to query the default value.
4261 * @return The end of line characters.
4265 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4267 gint mode
= file_prefs
.default_eol_character
;
4270 mode
= sci_get_eol_mode(editor
->sci
);
4272 return utils_get_eol_char(mode
);
4276 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4278 gint lines
, first
, i
;
4280 if (editor
== NULL
|| ! editor_prefs
.folding
)
4283 lines
= sci_get_line_count(editor
->sci
);
4284 first
= sci_get_first_visible_line(editor
->sci
);
4286 for (i
= 0; i
< lines
; i
++)
4288 gint level
= sci_get_fold_level(editor
->sci
, i
);
4290 if (level
& SC_FOLDLEVELHEADERFLAG
)
4292 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4293 sci_toggle_fold(editor
->sci
, i
);
4296 editor_scroll_to_line(editor
, first
, 0.0F
);
4300 void editor_unfold_all(GeanyEditor
*editor
)
4302 fold_all(editor
, FALSE
);
4306 void editor_fold_all(GeanyEditor
*editor
)
4308 fold_all(editor
, TRUE
);
4312 void editor_replace_tabs(GeanyEditor
*editor
)
4314 gint search_pos
, pos_in_line
, current_tab_true_length
;
4317 struct Sci_TextToFind ttf
;
4319 g_return_if_fail(editor
!= NULL
);
4321 sci_start_undo_action(editor
->sci
);
4322 tab_len
= sci_get_tab_width(editor
->sci
);
4324 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4325 ttf
.lpstrText
= (gchar
*) "\t";
4329 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4330 if (search_pos
== -1)
4333 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4334 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4335 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4336 sci_set_target_start(editor
->sci
, search_pos
);
4337 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4338 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4339 /* next search starts after replacement */
4340 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4341 /* update end of range now text has changed */
4342 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4345 sci_end_undo_action(editor
->sci
);
4349 /* Replaces all occurrences all spaces of the length of a given tab_width. */
4350 void editor_replace_spaces(GeanyEditor
*editor
)
4353 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4355 struct Sci_TextToFind ttf
;
4357 g_return_if_fail(editor
!= NULL
);
4359 if (tab_len_f
< 0.0)
4360 tab_len_f
= sci_get_tab_width(editor
->sci
);
4362 if (! dialogs_show_input_numeric(
4363 _("Enter Tab Width"),
4364 _("Enter the amount of spaces which should be replaced by a tab character."),
4365 &tab_len_f
, 1, 100, 1))
4369 tab_len
= (gint
) tab_len_f
;
4371 sci_start_undo_action(editor
->sci
);
4373 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4374 ttf
.lpstrText
= g_strnfill(tab_len
, ' ');
4378 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4379 if (search_pos
== -1)
4382 sci_set_target_start(editor
->sci
, search_pos
);
4383 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4384 sci_replace_target(editor
->sci
, "\t", FALSE
);
4385 ttf
.chrg
.cpMin
= search_pos
;
4386 /* update end of range now text has changed */
4387 ttf
.chrg
.cpMax
-= tab_len
- 1;
4389 sci_end_undo_action(editor
->sci
);
4390 g_free(ttf
.lpstrText
);
4394 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4396 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4397 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4398 gint i
= line_end
- 1;
4399 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4401 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4404 ch
= sci_get_char_at(editor
->sci
, i
);
4406 if (i
< (line_end
- 1))
4408 sci_set_target_start(editor
->sci
, i
+ 1);
4409 sci_set_target_end(editor
->sci
, line_end
);
4410 sci_replace_target(editor
->sci
, "", FALSE
);
4415 void editor_strip_trailing_spaces(GeanyEditor
*editor
)
4417 gint max_lines
= sci_get_line_count(editor
->sci
);
4420 sci_start_undo_action(editor
->sci
);
4422 for (line
= 0; line
< max_lines
; line
++)
4424 editor_strip_line_trailing_spaces(editor
, line
);
4426 sci_end_undo_action(editor
->sci
);
4430 void editor_ensure_final_newline(GeanyEditor
*editor
)
4432 gint max_lines
= sci_get_line_count(editor
->sci
);
4433 gboolean append_newline
= (max_lines
== 1);
4434 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4438 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4442 const gchar
*eol
= editor_get_eol_char(editor
);
4444 sci_insert_text(editor
->sci
, end_document
, eol
);
4449 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4453 PangoFontDescription
*pfd
;
4455 g_return_if_fail(editor
);
4457 pfd
= pango_font_description_from_string(font
);
4458 size
= pango_font_description_get_size(pfd
) / PANGO_SCALE
;
4459 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4460 pango_font_description_free(pfd
);
4462 for (style
= 0; style
<= 127; style
++)
4463 sci_set_font(editor
->sci
, style
, font_name
, size
);
4465 /* line number and braces */
4466 sci_set_font(editor
->sci
, STYLE_LINENUMBER
, font_name
, size
);
4467 sci_set_font(editor
->sci
, STYLE_BRACELIGHT
, font_name
, size
);
4468 sci_set_font(editor
->sci
, STYLE_BRACEBAD
, font_name
, size
);
4471 /* zoom to 100% to prevent confusion */
4472 sci_zoom_off(editor
->sci
);
4476 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4478 g_return_if_fail(editor
!= NULL
);
4480 editor
->line_wrapping
= wrap
;
4481 sci_set_lines_wrapped(editor
->sci
, wrap
);
4485 /** Sets the indent type for @a editor.
4486 * @param editor Editor.
4487 * @param type Indent type.
4491 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4493 editor_set_indent(editor
, type
, editor
->indent_width
);
4497 void editor_set_indent(GeanyEditor
*editor
, GeanyIndentType type
, gint width
)
4499 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4500 ScintillaObject
*sci
= editor
->sci
;
4501 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4503 editor
->indent_type
= type
;
4504 editor
->indent_width
= width
;
4505 sci_set_use_tabs(sci
, use_tabs
);
4507 if (type
== GEANY_INDENT_TYPE_BOTH
)
4509 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4510 if (iprefs
->hard_tab_width
!= 8)
4512 static gboolean warn
= TRUE
;
4514 ui_set_statusbar(TRUE
, _("Warning: non-standard hard tab width: %d != 8!"),
4515 iprefs
->hard_tab_width
);
4520 sci_set_tab_width(sci
, width
);
4522 SSM(sci
, SCI_SETINDENT
, width
, 0);
4524 /* remove indent spaces on backspace, if using any spaces to indent */
4525 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, type
!= GEANY_INDENT_TYPE_TABS
, 0);
4529 /* Convenience function for editor_goto_pos() to pass in a line number. */
4530 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line_no
, gint offset
)
4534 g_return_val_if_fail(editor
, FALSE
);
4535 if (line_no
< 0 || line_no
>= sci_get_line_count(editor
->sci
))
4540 gint current_line
= sci_get_current_line(editor
->sci
);
4542 line_no
= current_line
+ line_no
;
4545 pos
= sci_get_position_from_line(editor
->sci
, line_no
);
4546 return editor_goto_pos(editor
, pos
, TRUE
);
4550 /** Moves to position @a pos, switching to the document if necessary,
4551 * setting a marker if @a mark is set.
4553 * @param editor Editor.
4554 * @param pos The position.
4555 * @param mark Whether to set a mark on the position.
4556 * @return @c TRUE if action has been performed, otherwise @c FALSE.
4560 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4564 g_return_val_if_fail(editor
, FALSE
);
4565 if (G_UNLIKELY(pos
< 0))
4570 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4572 /* mark the tag with the yellow arrow */
4573 sci_marker_delete_all(editor
->sci
, 0);
4574 sci_set_marker_at_line(editor
->sci
, line
, 0);
4577 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4578 editor
->scroll_percent
= 0.25F
;
4580 /* finally switch to the page */
4581 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets
.notebook
), GTK_WIDGET(editor
->sci
));
4582 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), page_num
);
4589 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4591 GeanyEditor
*editor
= user_data
;
4593 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4594 * few lines only, maybe this could/should be done in Scintilla directly */
4595 if (event
->state
& GDK_MOD1_MASK
)
4597 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4600 else if (event
->state
& GDK_SHIFT_MASK
)
4602 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4604 sci_scroll_columns(editor
->sci
, amount
);
4608 return FALSE
; /* let Scintilla handle all other cases */
4612 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4614 GeanyDocument
*doc
= editor
->document
;
4616 if (!doc
->priv
->colourise_needed
)
4619 doc
->priv
->colourise_needed
= FALSE
;
4620 sci_colourise(editor
->sci
, 0, -1);
4622 /* now that the current document is colourised, fold points are now accurate,
4623 * so force an update of the current function/tag. */
4624 symbols_get_current_function(NULL
, NULL
);
4625 ui_update_statusbar(NULL
, -1);
4631 /* We only want to colourise just before drawing, to save startup time and
4632 * prevent unnecessary recolouring other documents after one is saved.
4633 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4634 * and "show" doesn't work). */
4635 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4637 GeanyEditor
*editor
= user_data
;
4639 editor_check_colourise(editor
);
4644 static gboolean
on_editor_expose_event(GtkWidget
*widget
, GdkEventExpose
*event
,
4647 GeanyEditor
*editor
= user_data
;
4649 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4650 * for some reason, maybe it's not necessary but just in case. */
4651 editor_check_colourise(editor
);
4656 static void setup_sci_keys(ScintillaObject
*sci
)
4658 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4659 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4660 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4661 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4662 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4663 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4664 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4665 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4666 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4667 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4668 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4669 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4670 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4671 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4672 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4674 if (editor_prefs
.use_gtk_word_boundaries
)
4676 /* use GtkEntry-like word boundaries */
4677 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4678 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4679 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4681 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4682 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4683 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4684 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4685 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4686 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4688 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4692 #include "icons/16x16/classviewer-var.xpm"
4693 #include "icons/16x16/classviewer-method.xpm"
4695 /* Create new editor widget (scintilla).
4696 * @note The @c "sci-notify" signal is connected separately. */
4697 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4699 ScintillaObject
*sci
;
4701 sci
= SCINTILLA(scintilla_new());
4703 gtk_widget_show(GTK_WIDGET(sci
));
4705 sci_set_codepage(sci
, SC_CP_UTF8
);
4706 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4707 /* disable scintilla provided popup menu */
4708 sci_use_popup(sci
, FALSE
);
4710 setup_sci_keys(sci
);
4712 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4713 sci_set_lines_wrapped(sci
, editor_prefs
.line_wrapping
);
4714 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
4715 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4716 /*sci_set_caret_policy_y(sci, CARET_JUMPS | CARET_EVEN, 0);*/
4717 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4718 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4720 /* tag autocompletion images */
4721 SSM(sci
, SCI_REGISTERIMAGE
, 1, (sptr_t
)classviewer_var
);
4722 SSM(sci
, SCI_REGISTERIMAGE
, 2, (sptr_t
)classviewer_method
);
4724 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4725 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4728 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4730 /* only connect signals if this is for the document notebook, not split window */
4731 if (editor
->sci
== NULL
)
4733 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4734 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4735 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4736 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4737 g_signal_connect(sci
, "expose-event", G_CALLBACK(on_editor_expose_event
), editor
);
4743 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4744 * @param editor Editor settings.
4745 * @return The new widget.
4749 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4751 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4752 ScintillaObject
*old
, *sci
;
4754 /* temporarily change editor to use the new sci widget */
4756 sci
= create_new_sci(editor
);
4759 editor_set_indent(editor
, iprefs
->type
, iprefs
->width
);
4760 editor_set_font(editor
, interface_prefs
.editor_font
);
4761 editor_apply_update_prefs(editor
);
4763 /* if editor already had a widget, restore it */
4770 GeanyEditor
*editor_create(GeanyDocument
*doc
)
4772 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4773 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
4775 editor
->document
= doc
;
4776 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
4778 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
4779 editor
->line_wrapping
= editor_prefs
.line_wrapping
;
4780 editor
->scroll_percent
= -1.0F
;
4781 editor
->line_breaking
= FALSE
;
4783 editor
->sci
= editor_create_widget(editor
);
4788 /* in case we need to free some fields in future */
4789 void editor_destroy(GeanyEditor
*editor
)
4795 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
4797 g_return_if_fail(NZV(doc
->real_path
));
4799 if (utils_str_equal(doc
->real_path
,
4800 utils_build_path(app
->configdir
, "snippets.conf", NULL
)))
4802 /* reload snippets */
4803 editor_snippets_free();
4804 editor_snippets_init();
4809 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
4813 g_return_val_if_fail(editor
, FALSE
);
4815 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
4818 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
4820 /* if no word part, complete normally */
4821 if (!check_partial_completion(editor
, entry
))
4822 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
4829 void editor_init(void)
4831 static GeanyIndentPrefs indent_prefs
;
4833 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
4834 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
4835 editor_prefs
.indentation
= &indent_prefs
;
4837 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
4838 * handler (on_editor_notify) is called */
4839 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
4841 ui_add_config_file_menu_item(utils_build_path(app
->configdir
, "snippets.conf", NULL
),
4843 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
4847 /* TODO: Should these be user-defined instead of hard-coded? */
4848 void editor_set_indentation_guides(GeanyEditor
*editor
)
4853 g_return_if_fail(editor
!= NULL
);
4855 if (! editor_prefs
.show_indent_guide
)
4857 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
4861 lexer
= sci_get_lexer(editor
->sci
);
4864 /* Lines added/removed are prefixed with +/- characters, so
4865 * those lines will not be shown with any indentation guides.
4866 * It can be distracting that only a few of lines in a diff/patch
4867 * file will show the guides. */
4872 /* These languages use indentation for control blocks; the "look forward" method works
4876 case SCLEX_MAKEFILE
:
4880 case SCLEX_PROPERTIES
:
4881 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
4883 mode
= SC_IV_LOOKFORWARD
;
4886 /* C-like (structured) languages benefit from the "look both" method */
4900 case SCLEX_FREEBASIC
:
4903 mode
= SC_IV_LOOKBOTH
;
4911 sci_set_indentation_guides(editor
->sci
, mode
);
4915 /* Apply just the prefs that can change in the Preferences dialog */
4916 void editor_apply_update_prefs(GeanyEditor
*editor
)
4918 ScintillaObject
*sci
;
4920 g_return_if_fail(editor
!= NULL
);
4922 if (main_status
.quitting
)
4927 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
4928 editor_get_long_line_column(), editor_prefs
.long_line_color
);
4930 /* update indent width, tab width */
4931 editor_set_indent(editor
, editor
->indent_type
, editor
->indent_width
);
4932 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
4934 sci_assign_cmdkey(sci
, SCK_HOME
| (SCMOD_SHIFT
<< 16),
4935 editor_prefs
.smart_home_key
? SCI_VCHOMEEXTEND
: SCI_HOMEEXTEND
);
4936 sci_assign_cmdkey(sci
, SCK_HOME
| ((SCMOD_SHIFT
| SCMOD_ALT
) << 16),
4937 editor_prefs
.smart_home_key
? SCI_VCHOMERECTEXTEND
: SCI_HOMERECTEXTEND
);
4939 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
4940 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
4942 editor_set_indentation_guides(editor
);
4944 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
4945 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
4946 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4947 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
4949 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
4952 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4954 /* (dis)allow scrolling past end of document */
4955 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
4959 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
4960 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
4962 ScintillaObject
*sci
= editor
->sci
;
4963 gint pos
= sci_get_position_from_line(sci
, line
);
4967 sci_insert_text(sci
, pos
, "\t");
4971 if (sci_get_char_at(sci
, pos
) == '\t')
4973 sci_set_selection(sci
, pos
, pos
+ 1);
4974 sci_replace_sel(sci
, "");
4976 else /* remove spaces only if no tabs */
4978 gint width
= sci_get_line_indentation(sci
, line
);
4980 width
-= editor_get_indent_prefs(editor
)->width
;
4981 sci_set_line_indentation(sci
, line
, width
);
4987 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
4989 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4990 ScintillaObject
*sci
= editor
->sci
;
4992 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
4993 change_tab_indentation(editor
, line
, increase
);
4996 gint width
= sci_get_line_indentation(sci
, line
);
4998 width
+= increase
? iprefs
->width
: -iprefs
->width
;
4999 sci_set_line_indentation(sci
, line
, width
);
5004 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
5006 ScintillaObject
*sci
= editor
->sci
;
5008 gint line
, lstart
, lend
;
5010 if (sci_get_lines_selected(sci
) <= 1)
5012 line
= sci_get_current_line(sci
);
5013 editor_change_line_indent(editor
, line
, increase
);
5016 editor_select_lines(editor
, FALSE
);
5017 start
= sci_get_selection_start(sci
);
5018 end
= sci_get_selection_end(sci
);
5019 lstart
= sci_get_line_from_position(sci
, start
);
5020 lend
= sci_get_line_from_position(sci
, end
);
5021 if (end
== sci_get_length(sci
))
5022 lend
++; /* for last line with text on it */
5024 sci_start_undo_action(sci
);
5025 for (line
= lstart
; line
< lend
; line
++)
5027 editor_change_line_indent(editor
, line
, increase
);
5029 sci_end_undo_action(sci
);
5031 /* set cursor/selection */
5034 sci_set_selection_start(sci
, start
);
5035 end
= sci_get_position_from_line(sci
, lend
);
5036 sci_set_selection_end(sci
, end
);
5037 editor_select_lines(editor
, FALSE
);
5041 sci_set_current_line(sci
, lstart
);
5046 /** Gets snippet by name.
5048 * If @a editor is passed, returns a snippet specific to the document filetype.
5049 * If @a editor is @c NULL, returns a snippet from the default set.
5051 * @param editor Editor or @c NULL.
5052 * @param snippet_name Snippet name.
5053 * @return snippet or @c NULL if it was not found. Must not be freed.
5055 const gchar
*editor_find_snippet(GeanyEditor
*editor
, const gchar
*snippet_name
)
5057 const gchar
*subhash_name
= editor
? editor
->document
->file_type
->name
: "Default";
5058 GHashTable
*subhash
= g_hash_table_lookup(snippet_hash
, subhash_name
);
5060 return subhash
? g_hash_table_lookup(subhash
, snippet_name
) : NULL
;
5064 /** Replaces all special sequences in @a snippet and inserts it at @a pos.
5065 * If you insert at the current position, consider calling @c sci_scroll_caret()
5066 * after this function.
5071 void editor_insert_snippet(GeanyEditor
*editor
, gint pos
, const gchar
*snippet
)
5076 pattern
= g_string_new(snippet
);
5077 read_indent(editor
, pos
);
5078 cursor_pos
= snippets_make_replacements(editor
, pattern
, strlen(indent
));
5079 editor_insert_text_block(editor
, pattern
->str
, pos
, cursor_pos
, -1, FALSE
);
5080 g_string_free(pattern
, TRUE
);