2 * editor.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
6 * Copyright 2009-2010 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"
72 /* Note: use sciwrappers.h instead where possible.
73 * Do not use SSM in files unrelated to scintilla. */
74 #define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
77 static GHashTable
*snippet_hash
= NULL
;
78 static GQueue
*snippet_offsets
= NULL
;
79 static gint snippet_cursor_insert_pos
;
80 static GtkAccelGroup
*snippet_accel_group
= NULL
;
82 /* holds word under the mouse or keyboard cursor */
83 static gchar current_word
[GEANY_MAX_WORD_LENGTH
];
85 /* Initialised in keyfile.c. */
86 GeanyEditorPrefs editor_prefs
;
88 EditorInfo editor_info
= {current_word
, -1};
98 } calltip
= {NULL
, FALSE
, NULL
, 0, 0, NULL
};
100 static gchar indent
[100];
103 static void on_new_line_added(GeanyEditor
*editor
);
104 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
105 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
106 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
107 static gboolean
is_code_style(gint lexer
, gint style
);
108 static gboolean
is_string_style(gint lexer
, gint style
);
109 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
110 static void close_block(GeanyEditor
*editor
, gint pos
);
111 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
112 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
113 const gchar
*wc
, gboolean stem
);
114 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
);
115 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
);
116 static gssize
snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
,
120 void editor_snippets_free(void)
122 g_hash_table_destroy(snippet_hash
);
123 g_queue_free(snippet_offsets
);
124 gtk_window_remove_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
128 static void snippets_load(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
130 gsize i
, j
, len
= 0, len_keys
= 0;
131 gchar
**groups_user
, **groups_sys
;
132 gchar
**keys_user
, **keys_sys
;
136 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
138 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
140 /* first read all globally defined auto completions */
141 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
142 for (i
= 0; i
< len
; i
++)
144 if (strcmp(groups_sys
[i
], "Keybindings") == 0)
146 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
147 /* create new hash table for the read section (=> filetype) */
148 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
149 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
151 for (j
= 0; j
< len_keys
; j
++)
153 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
154 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
156 g_strfreev(keys_sys
);
158 g_strfreev(groups_sys
);
160 /* now read defined completions in user's configuration directory and add / replace them */
161 groups_user
= g_key_file_get_groups(userconfig
, &len
);
162 for (i
= 0; i
< len
; i
++)
164 if (strcmp(groups_user
[i
], "Keybindings") == 0)
166 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
168 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
170 { /* new key found, create hash table */
171 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
172 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
174 for (j
= 0; j
< len_keys
; j
++)
176 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
178 { /* value = NULL means the key doesn't yet exist, so insert */
179 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
180 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
183 { /* old key and value will be freed by destroy function (g_free) */
184 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
185 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
188 g_strfreev(keys_user
);
190 g_strfreev(groups_user
);
194 static void on_snippet_keybinding_activate(gchar
*key
)
196 GeanyDocument
*doc
= document_get_current();
198 GHashTable
*specials
;
200 gint pos
, line
, indent_width
, cursor_pos
;
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 pos
= sci_get_current_position(doc
->editor
->sci
);
219 line
= sci_get_line_from_position(doc
->editor
->sci
, pos
);
220 indent_width
= sci_get_line_indentation(doc
->editor
->sci
, line
);
222 pattern
= g_string_new(s
);
223 cursor_pos
= snippets_make_replacements(doc
->editor
, pattern
, indent_width
);
225 editor_insert_text_block(doc
->editor
, pattern
->str
, pos
, cursor_pos
, indent_width
, FALSE
);
226 sci_scroll_caret(doc
->editor
->sci
);
228 g_string_free(pattern
, TRUE
);
232 static void add_kb(GKeyFile
*keyfile
, const gchar
*group
, gchar
**keys
)
238 for (i
= 0; i
< g_strv_length(keys
); i
++)
241 GdkModifierType mods
;
242 gchar
*accel_string
= g_key_file_get_value(keyfile
, group
, keys
[i
], NULL
);
244 gtk_accelerator_parse(accel_string
, &key
, &mods
);
245 g_free(accel_string
);
247 if (key
== 0 && mods
== 0)
249 g_warning("Can not parse accelerator \"%s\" from user snippets.conf", accel_string
);
252 gtk_accel_group_connect(snippet_accel_group
, key
, mods
, 0,
253 g_cclosure_new_swap((GCallback
)on_snippet_keybinding_activate
,
254 g_strdup(keys
[i
]), (GClosureNotify
)g_free
));
259 static void load_kb(GKeyFile
*sysconfig
, GKeyFile
*userconfig
)
261 const gchar kb_group
[] = "Keybindings";
262 gchar
**keys
= g_key_file_get_keys(userconfig
, kb_group
, NULL
, NULL
);
265 /* remove overridden keys from system keyfile */
266 foreach_strv(ptr
, keys
)
267 g_key_file_remove_key(sysconfig
, kb_group
, *ptr
, NULL
);
269 add_kb(userconfig
, kb_group
, keys
);
272 keys
= g_key_file_get_keys(sysconfig
, kb_group
, NULL
, NULL
);
273 add_kb(sysconfig
, kb_group
, keys
);
278 void editor_snippets_init(void)
280 gchar
*sysconfigfile
, *userconfigfile
;
281 GKeyFile
*sysconfig
= g_key_file_new();
282 GKeyFile
*userconfig
= g_key_file_new();
284 snippet_offsets
= g_queue_new();
286 sysconfigfile
= g_strconcat(app
->datadir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
287 userconfigfile
= g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
289 /* check for old autocomplete.conf files (backwards compatibility) */
290 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
))
291 setptr(userconfigfile
,
292 g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "autocomplete.conf", NULL
));
294 /* load the actual config files */
295 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
296 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
298 snippets_load(sysconfig
, userconfig
);
300 /* setup snippet keybindings */
301 snippet_accel_group
= gtk_accel_group_new();
302 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), snippet_accel_group
);
303 load_kb(sysconfig
, userconfig
);
305 g_free(sysconfigfile
);
306 g_free(userconfigfile
);
307 g_key_file_free(sysconfig
);
308 g_key_file_free(userconfig
);
312 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
315 GeanyEditor
*editor
= data
;
316 GeanyDocument
*doc
= editor
->document
;
318 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
319 * fake event to show the editor menu triggered by a key event where we want to use the
320 * text cursor position. */
321 if (event
->x
> 0.0 && event
->y
> 0.0)
322 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
323 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
325 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
327 if (event
->button
== 1)
329 guint state
= event
->state
& gtk_accelerator_get_default_mod_mask();
331 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
333 gint ss
= sci_get_selection_start(editor
->sci
);
334 sci_set_selection_end(editor
->sci
, ss
);
336 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GDK_CONTROL_MASK
)
338 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
340 editor_find_current_word(editor
, editor_info
.click_pos
,
341 current_word
, sizeof current_word
, NULL
);
343 return symbols_goto_tag(current_word
, TRUE
);
345 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
348 return document_check_disk_status(doc
, FALSE
);
351 /* calls the edit popup menu in the editor */
352 if (event
->button
== 3)
356 editor_find_current_word(editor
, editor_info
.click_pos
,
357 current_word
, sizeof current_word
, NULL
);
359 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
360 ui_update_popup_goto_items(can_goto
);
361 ui_update_popup_copy_items(doc
);
362 ui_update_insert_include_item(doc
, 0);
364 g_signal_emit_by_name(geany_object
, "update-editor-menu",
365 current_word
, editor_info
.click_pos
, doc
);
367 gtk_menu_popup(GTK_MENU(main_widgets
.editor_menu
),
368 NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
376 static gboolean
is_style_php(gint style
)
378 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
379 style
== SCE_HPHP_COMPLEX_VARIABLE
)
388 gint
editor_get_long_line_type(void)
391 switch (app
->project
->long_line_behaviour
)
393 case 0: /* marker disabled */
395 case 1: /* use global settings */
397 case 2: /* custom (enabled) */
398 return editor_prefs
.long_line_global_type
;
401 if (!editor_prefs
.long_line_global_enabled
)
404 return editor_prefs
.long_line_global_type
;
408 gint
editor_get_long_line_column(void)
410 if (app
->project
&& app
->project
->long_line_behaviour
!= 1 /* use global settings */)
411 return app
->project
->long_line_column
;
413 return editor_prefs
.long_line_global_column
;
417 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
419 ScintillaObject
*sci
;
421 g_return_if_fail(editor
!= NULL
);
425 sci_toggle_fold(sci
, line
);
427 /* extra toggling of child fold points
428 * use when editor_prefs.unfold_all_children is set and Shift is NOT pressed or when
429 * editor_prefs.unfold_all_children is NOT set but Shift is pressed */
430 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
431 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
433 gint last_line
= SSM(sci
, SCI_GETLASTCHILD
, line
, -1);
436 if (sci_get_line_is_visible(sci
, line
+ 1))
437 { /* unfold all children of the current fold point */
438 for (i
= line
; i
< last_line
; i
++)
440 if (! sci_get_line_is_visible(sci
, i
))
442 sci_toggle_fold(sci
, sci_get_fold_parent(sci
, i
));
447 { /* fold all children of the current fold point */
448 for (i
= line
; i
< last_line
; i
++)
450 gint level
= sci_get_fold_level(sci
, i
);
451 if (level
& SC_FOLDLEVELHEADERFLAG
)
453 if (sci_get_fold_expanded(sci
, i
))
454 sci_toggle_fold(sci
, i
);
462 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
464 /* left click to marker margin marks the line */
467 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
469 /*sci_marker_delete_all(editor->sci, 1);*/
470 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
472 /* left click on the folding margin to toggle folding state of current line */
473 else if (nt
->margin
== 2 && editor_prefs
.folding
)
475 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
476 editor_toggle_fold(editor
, line
, nt
->modifiers
);
481 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
483 ScintillaObject
*sci
= editor
->sci
;
484 gint pos
= sci_get_current_position(sci
);
486 /* undo / redo menu update */
487 ui_update_popup_reundo_items(editor
->document
);
489 /* brace highlighting */
490 editor_highlight_braces(editor
, pos
);
492 ui_update_statusbar(editor
->document
, pos
);
495 /** experimental code for inverting selections */
498 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
500 /* need to get colour from getstyleat(), but how? */
501 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
502 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
505 sci_get_style_at(sci
, pos
);
511 static void check_line_breaking(GeanyEditor
*editor
, gint pos
, gchar c
)
513 ScintillaObject
*sci
= editor
->sci
;
514 gint line
, lstart
, col
;
516 if (!editor
->line_breaking
)
519 col
= sci_get_col_from_position(sci
, pos
);
522 pos
--; /* Look for previous space, not the new one */
524 line
= sci_get_current_line(sci
);
526 lstart
= sci_get_position_from_line(sci
, line
);
528 /* use column instead of position which might be different with multibyte characters */
529 if (col
< editor_prefs
.line_break_column
)
532 /* look for the last space before line_break_column */
533 pos
= MIN(pos
, lstart
+ editor_prefs
.line_break_column
);
537 c
= sci_get_char_at(sci
, --pos
);
540 gint diff
, last_pos
, last_col
;
542 /* remember the distance between the current column and the last column on the line
543 * (we use column position in case the previous line gets altered, such as removing
544 * trailing spaces or in case it contains multibyte characters) */
545 last_pos
= sci_get_line_end_position(sci
, line
);
546 last_col
= sci_get_col_from_position(sci
, last_pos
);
547 diff
= last_col
- col
;
549 /* break the line after the space */
550 sci_set_current_position(sci
, pos
+ 1, FALSE
);
551 sci_cancel(sci
); /* don't select from completion list */
552 sci_send_command(sci
, SCI_NEWLINE
);
555 /* correct cursor position (might not be at line end) */
556 last_pos
= sci_get_line_end_position(sci
, line
);
557 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
558 /* last column - distance is the desired column, then retrieve its document position */
559 pos
= SSM(sci
, SCI_FINDCOLUMN
, line
, last_col
- diff
);
560 sci_set_current_position(sci
, pos
, FALSE
);
568 static void show_autocomplete(ScintillaObject
*sci
, gint rootlen
, const gchar
*words
)
570 /* store whether a calltip is showing, so we can reshow it after autocompletion */
571 calltip
.set
= SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
572 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
);
576 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
578 ScintillaObject
*sci
= editor
->sci
;
580 g_return_if_fail(tags
);
584 GString
*words
= g_string_sized_new(150);
587 for (j
= 0; j
< tags
->len
; ++j
)
589 TMTag
*tag
= tags
->pdata
[j
];
592 g_string_append_c(words
, '\n');
594 if (j
== editor_prefs
.autocompletion_max_entries
)
596 g_string_append(words
, "...");
599 g_string_append(words
, tag
->name
);
601 /* for now, tag types don't all follow C, so just look at arglist */
602 if (NZV(tag
->atts
.entry
.arglist
))
603 g_string_append(words
, "?2");
605 g_string_append(words
, "?1");
607 show_autocomplete(sci
, rootlen
, words
->str
);
608 g_string_free(words
, TRUE
);
613 /* do not use with long strings */
614 static gboolean
match_last_chars(ScintillaObject
*sci
, gint pos
, const gchar
*str
)
616 gsize len
= strlen(str
);
619 g_return_val_if_fail(len
< 100, FALSE
);
621 buf
= g_alloca(len
+ 1);
622 sci_get_text_range(sci
, pos
- len
, pos
, buf
);
623 return strcmp(str
, buf
) == 0;
627 static gboolean
reshow_calltip(gpointer data
)
631 g_return_val_if_fail(calltip
.sci
!= NULL
, FALSE
);
633 SSM(calltip
.sci
, SCI_CALLTIPCANCEL
, 0, 0);
634 doc
= document_get_current();
636 if (doc
&& doc
->editor
->sci
== calltip
.sci
)
638 /* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
639 * may be completely wrong in case the user cancelled the auto completion with the mouse */
640 SSM(calltip
.sci
, SCI_CALLTIPSHOW
, calltip
.pos
, (sptr_t
) calltip
.text
);
646 static void request_reshowing_calltip(SCNotification
*nt
)
650 /* delay the reshow of the calltip window to make sure it is actually displayed,
651 * without it might be not visible on SCN_AUTOCCANCEL */
652 g_idle_add(reshow_calltip
, NULL
);
657 static void autocomplete_scope(GeanyEditor
*editor
)
659 ScintillaObject
*sci
= editor
->sci
;
660 gint pos
= sci_get_current_position(editor
->sci
);
661 gchar typed
= sci_get_char_at(sci
, pos
- 1);
663 const GPtrArray
*tags
= NULL
;
665 GeanyFiletype
*ft
= editor
->document
->file_type
;
667 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
669 if (match_last_chars(sci
, pos
, "->") || match_last_chars(sci
, pos
, "::"))
671 else if (typed
!= '.')
674 else if (typed
!= '.')
677 /* allow for a space between word and operator */
678 if (isspace(sci_get_char_at(sci
, pos
- 2)))
680 name
= editor_get_word_at_pos(editor
, pos
- 1, NULL
);
684 tags
= tm_workspace_find(name
, tm_tag_max_t
, NULL
, FALSE
, ft
->lang
);
686 if (!tags
|| tags
->len
== 0)
689 tag
= g_ptr_array_index(tags
, 0);
690 name
= tag
->atts
.entry
.var_type
;
693 TMWorkObject
*obj
= editor
->document
->tm_file
;
695 tags
= tm_workspace_find_scope_members(obj
? obj
->tags_array
: NULL
,
698 show_tags_list(editor
, tags
, 0);
703 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
705 ScintillaObject
*sci
= editor
->sci
;
706 gint pos
= sci_get_current_position(sci
);
711 { /* simple indentation (only for CR format) */
712 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
713 on_new_line_added(editor
);
717 { /* simple indentation (for CR/LF and LF format) */
718 on_new_line_added(editor
);
722 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
725 { /* close xml-tags */
726 handle_xml(editor
, pos
, nt
->ch
);
731 auto_close_chars(sci
, pos
, nt
->ch
);
733 editor_show_calltip(editor
, --pos
);
737 { /* hide calltips */
738 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0))
740 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
742 g_free(calltip
.text
);
754 auto_close_chars(sci
, pos
, nt
->ch
);
758 { /* closing bracket handling */
759 if (editor
->auto_indent
)
760 close_block(editor
, pos
- 1);
763 /* scope autocompletion */
765 case ':': /* C/C++ class:: syntax */
766 /* tag autocompletion */
769 if (! editor_start_auto_complete(editor
, pos
, FALSE
))
770 request_reshowing_calltip(nt
);
772 editor_start_auto_complete(editor
, pos
, FALSE
);
775 check_line_breaking(editor
, pos
, nt
->ch
);
779 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
780 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
781 gboolean force
, gint visLevels
, gint level
)
783 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
784 gint levelLine
= level
;
786 while (*line
<= lineMaxSubord
)
788 if (G_UNLIKELY(force
))
791 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
793 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
798 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
801 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
802 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
804 if (G_UNLIKELY(force
))
807 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
809 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
810 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
816 if (!sci_get_fold_expanded(sci
, *line
))
817 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
818 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
822 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
834 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
836 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
838 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
840 /* Adding a fold point */
841 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
842 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
845 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
847 if (! sci_get_fold_expanded(sci
, line
))
848 { /* Removing the fold from one that has been contracted so should expand
849 * otherwise lines are left invisible with no way to make them visible */
850 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
851 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
854 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
855 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
857 /* See if should still be hidden */
858 gint parentLine
= sci_get_fold_parent(sci
, line
);
861 SSM(sci
, SCI_SHOWLINES
, line
, line
);
863 else if (sci_get_fold_expanded(sci
, parentLine
) &&
864 sci_get_line_is_visible(sci
, parentLine
))
866 SSM(sci
, SCI_SHOWLINES
, line
, line
);
872 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
873 gboolean enforcePolicy
)
875 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
876 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
879 for (line
= lineStart
; line
<= lineEnd
; line
++)
881 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
886 static void auto_update_margin_width(GeanyEditor
*editor
)
888 gint next_linecount
= 1;
889 gint linecount
= sci_get_line_count(editor
->sci
);
890 GeanyDocument
*doc
= editor
->document
;
892 while (next_linecount
<= linecount
)
893 next_linecount
*= 10;
895 if (editor
->document
->priv
->line_count
!= next_linecount
)
897 doc
->priv
->line_count
= next_linecount
;
898 sci_set_line_numbers(editor
->sci
, TRUE
, 0);
903 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
905 gint pos
= sci_get_current_position(sci
);
907 sci_insert_text(sci
, pos
, text
);
908 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
912 /* Complete the next word part from @a entry */
913 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
915 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
917 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
919 if (strstr(text
, stem
) != text
)
920 return FALSE
; /* shouldn't happen */
921 if (strlen(text
) <= strlen(stem
))
924 text
+= strlen(stem
); /* skip stem */
925 ptr
= strstr(text
+ 1, "_");
929 partial_complete(editor
->sci
, text
);
935 foreach_str(ptr
, text
+ 1)
939 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
942 partial_complete(editor
->sci
, text
);
951 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
952 * Plugins can connect to the "editor-notify" signal. */
953 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
954 gpointer scnt
, gpointer data
)
956 GeanyEditor
*editor
= data
;
959 g_return_if_fail(editor
!= NULL
);
961 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
965 static gboolean
on_editor_notify(G_GNUC_UNUSED GObject
*object
, GeanyEditor
*editor
,
966 SCNotification
*nt
, G_GNUC_UNUSED gpointer data
)
968 ScintillaObject
*sci
= editor
->sci
;
969 GeanyDocument
*doc
= editor
->document
;
971 switch (nt
->nmhdr
.code
)
973 case SCN_SAVEPOINTLEFT
:
974 document_set_text_changed(doc
, TRUE
);
977 case SCN_SAVEPOINTREACHED
:
978 document_set_text_changed(doc
, FALSE
);
981 case SCN_MODIFYATTEMPTRO
:
985 case SCN_MARGINCLICK
:
986 on_margin_click(editor
, nt
);
990 on_update_ui(editor
, nt
);
994 /* Visible lines are only laid out accurately just before painting,
995 * so we need to only call editor_scroll_to_line here, because the document
996 * may have line wrapping and folding enabled.
997 * http://scintilla.sourceforge.net/ScintillaDoc.html#LineWrapping
998 * This is important e.g. when loading a session and switching pages
999 * and having the cursor scroll in view. */
1000 /* FIXME: Really we want to do this just before painting, not after it
1001 * as it will cause repainting. */
1002 if (editor
->scroll_percent
> 0.0F
)
1004 editor_scroll_to_line(editor
, -1, editor
->scroll_percent
);
1005 /* disable further scrolling */
1006 editor
->scroll_percent
= -1.0F
;
1011 if (editor_prefs
.show_linenumber_margin
&& (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) && nt
->linesAdded
)
1013 /* automatically adjust Scintilla's line numbers margin width */
1014 auto_update_margin_width(editor
);
1016 if (nt
->modificationType
& SC_STARTACTION
&& ! ignore_callback
)
1018 /* get notified about undo changes */
1019 document_undo_add(doc
, UNDO_SCINTILLA
, NULL
);
1021 if (editor_prefs
.folding
&& (nt
->modificationType
& SC_MOD_CHANGEFOLD
) != 0)
1023 /* handle special fold cases, e.g. #1923350 */
1024 fold_changed(sci
, nt
->line
, nt
->foldLevelNow
, nt
->foldLevelPrev
);
1029 on_char_added(editor
, nt
);
1032 case SCN_USERLISTSELECTION
:
1033 if (nt
->listType
== 1)
1035 sci_add_text(sci
, nt
->text
);
1039 case SCN_AUTOCSELECTION
:
1040 if (g_str_equal(nt
->text
, "..."))
1047 case SCN_AUTOCCANCELLED
:
1048 /* now that autocomplete is finishing or was cancelled, reshow calltips
1049 * if they were showing */
1050 request_reshowing_calltip(nt
);
1054 case SCN_STYLENEEDED
:
1055 geany_debug("style");
1059 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
1062 case SCN_URIDROPPED
:
1063 if (nt
->text
!= NULL
)
1065 document_open_file_list(nt
->text
, -1);
1069 case SCN_CALLTIPCLICK
:
1070 if (nt
->position
> 0)
1072 switch (nt
->position
)
1074 case 1: /* up arrow */
1075 if (calltip
.tag_index
> 0)
1076 calltip
.tag_index
--;
1079 case 2: calltip
.tag_index
++; break; /* down arrow */
1081 editor_show_calltip(editor
, -1);
1086 /* recalculate line margin width */
1087 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
1090 /* we always return FALSE here to let plugins handle the event too */
1095 /* Note: this is the same as sci_get_tab_width(), but is still useful when you don't have
1096 * a scintilla pointer. */
1097 static gint
get_tab_width(const GeanyIndentPrefs
*indent_prefs
)
1099 if (indent_prefs
->type
== GEANY_INDENT_TYPE_BOTH
)
1100 return indent_prefs
->hard_tab_width
;
1102 return indent_prefs
->width
; /* tab width = indent width */
1106 /* Returns a string containing width chars of whitespace, filled with simple space
1107 * characters or with the right number of tab characters, according to the indent prefs.
1108 * (Result is filled with tabs *and* spaces if width isn't a multiple of
1109 * the tab width). */
1111 get_whitespace(const GeanyIndentPrefs
*iprefs
, gint width
)
1113 g_return_val_if_fail(width
>= 0, NULL
);
1116 return g_strdup("");
1118 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
1120 return g_strnfill(width
, ' ');
1123 { /* first fill text with tabs and fill the rest with spaces */
1124 const gint tab_width
= get_tab_width(iprefs
);
1125 gint tabs
= width
/ tab_width
;
1126 gint spaces
= width
% tab_width
;
1127 gint len
= tabs
+ spaces
;
1130 str
= g_malloc(len
+ 1);
1132 memset(str
, '\t', tabs
);
1133 memset(str
+ tabs
, ' ', spaces
);
1140 static const GeanyIndentPrefs
*
1141 get_default_indent_prefs(void)
1143 static GeanyIndentPrefs iprefs
;
1145 iprefs
= app
->project
? *app
->project
->priv
->indentation
: *editor_prefs
.indentation
;
1150 /** Gets the indentation prefs for the editor.
1151 * In future, the prefs might be different according to project or filetype.
1152 * @warning Always get a fresh result instead of keeping a pointer to it if the editor/project
1153 * settings may have changed, or if this function has been called for a different editor.
1154 * @param editor The editor, or @c NULL to get the default indent prefs.
1155 * @return The indent prefs. */
1156 const GeanyIndentPrefs
*
1157 editor_get_indent_prefs(GeanyEditor
*editor
)
1159 static GeanyIndentPrefs iprefs
;
1160 const GeanyIndentPrefs
*dprefs
= get_default_indent_prefs();
1162 /* Return the address of the default prefs to allow returning default and editor
1163 * pref pointers without invalidating the contents of either. */
1168 iprefs
.type
= editor
->indent_type
;
1169 iprefs
.width
= editor
->indent_width
;
1171 /* if per-document auto-indent is enabled, but we don't have a global mode set,
1172 * just use basic auto-indenting */
1173 if (editor
->auto_indent
&& iprefs
.auto_indent_mode
== GEANY_AUTOINDENT_NONE
)
1174 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_BASIC
;
1176 if (!editor
->auto_indent
)
1177 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_NONE
;
1183 static void on_new_line_added(GeanyEditor
*editor
)
1185 ScintillaObject
*sci
= editor
->sci
;
1186 gint line
= sci_get_current_line(sci
);
1188 /* simple indentation */
1189 if (editor
->auto_indent
)
1191 insert_indent_after_line(editor
, line
- 1);
1194 if (editor_prefs
.auto_continue_multiline
)
1195 { /* " * " auto completion in multiline C/C++/D/Java comments */
1196 auto_multiline(editor
, line
);
1199 if (editor_prefs
.newline_strip
)
1200 { /* strip the trailing spaces on the previous line */
1201 editor_strip_line_trailing_spaces(editor
, line
- 1);
1206 static gboolean
lexer_has_braces(ScintillaObject
*sci
)
1208 gint lexer
= sci_get_lexer(sci
);
1214 case SCLEX_HTML
: /* for PHP & JS */
1215 case SCLEX_PASCAL
: /* for multiline comments? */
1226 /* Read indent chars for the line that pos is on into indent global variable.
1227 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1228 * instead in any new code. */
1229 static void read_indent(GeanyEditor
*editor
, gint pos
)
1231 ScintillaObject
*sci
= editor
->sci
;
1232 guint i
, len
, j
= 0;
1236 line
= sci_get_line_from_position(sci
, pos
);
1238 len
= sci_get_line_length(sci
, line
);
1239 linebuf
= sci_get_line(sci
, line
);
1241 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1243 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1244 indent
[j
++] = linebuf
[i
];
1253 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1259 len
= sci_get_line_length(sci
, line
);
1260 linebuf
= sci_get_line(sci
, line
);
1262 for (i
= 0; i
< len
; i
++)
1264 /* i == (len - 1) prevents wrong indentation after lines like
1265 * " { return bless({}, shift); }" (Perl) */
1266 if (linebuf
[i
] == '{' && i
== (len
- 1))
1275 while (k
> 0 && isspace(linebuf
[k
])) k
--;
1277 /* if last non-whitespace character is a { increase indentation by a tab
1278 * e.g. for (...) { */
1279 if (linebuf
[k
] == '{')
1291 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1293 gint last_char
= sci_get_line_end_position(sci
, line
) - 1;
1295 /* add extra indentation for Python after colon */
1296 if (sci_get_char_at(sci
, last_char
) == ':' &&
1297 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1305 static gint
get_xml_indent(ScintillaObject
*sci
, gint line
)
1307 gboolean need_close
= FALSE
;
1308 gint end
= sci_get_line_end_position(sci
, line
) - 1;
1310 if (sci_get_char_at(sci
, end
) == '>' &&
1311 sci_get_char_at(sci
, end
- 1) != '/')
1313 gint style
= sci_get_style_at(sci
, end
);
1315 if (style
== SCE_H_TAG
|| style
== SCE_H_TAGUNKNOWN
)
1317 gint start
= sci_get_position_from_line(sci
, line
);
1318 gchar
*line_contents
= sci_get_contents_range(sci
, start
, end
+ 1);
1319 gchar
*opened_tag_name
= utils_find_open_xml_tag(line_contents
, end
+ 1 - start
);
1321 if (NZV(opened_tag_name
))
1324 if (sci_get_lexer(sci
) == SCLEX_HTML
&& utils_is_short_html_tag(opened_tag_name
))
1327 g_free(line_contents
);
1328 g_free(opened_tag_name
);
1332 return need_close
? 1 : 0;
1336 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1338 ScintillaObject
*sci
= editor
->sci
;
1340 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1342 g_return_val_if_fail(line
>= 0, 0);
1344 size
= sci_get_line_indentation(sci
, line
);
1346 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1348 gint additional_indent
= 0;
1350 if (lexer_has_braces(sci
))
1351 additional_indent
= iprefs
->width
* get_brace_indent(sci
, line
);
1353 if (editor
->document
->file_type
->id
== GEANY_FILETYPES_PYTHON
)
1354 additional_indent
= iprefs
->width
* get_python_indent(sci
, line
);
1356 /* HTML lexer "has braces" because of PHP and JavaScript. If get_brace_indent() did not
1357 * recommend us to insert additional indent, we are probably not in PHP/JavaScript chunk and
1358 * should make the XML-related check */
1359 if (additional_indent
== 0 && !editor_prefs
.auto_close_xml_tags
&&
1360 (sci_get_lexer(sci
) == SCLEX_HTML
||
1361 sci_get_lexer(sci
) == SCLEX_XML
) &&
1362 editor
->document
->file_type
->priv
->xml_indent_tags
)
1364 size
+= iprefs
->width
* get_xml_indent(sci
, line
);
1367 size
+= additional_indent
;
1373 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
)
1375 ScintillaObject
*sci
= editor
->sci
;
1376 gint line_indent
= sci_get_line_indentation(sci
, line
);
1377 gint size
= get_indent_size_after_line(editor
, line
);
1378 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1384 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
&& size
== line_indent
)
1386 /* support tab indents, space aligns style - copy last line 'indent' exactly */
1387 gint start
= sci_get_position_from_line(sci
, line
);
1388 gint end
= sci_get_line_indent_position(sci
, line
);
1390 text
= sci_get_contents_range(sci
, start
, end
);
1394 text
= get_whitespace(iprefs
, size
);
1396 sci_add_text(sci
, text
);
1401 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
)
1403 const gchar
*closing_char
= NULL
;
1406 if (utils_isbrace(c
, 0))
1407 end_pos
= sci_find_matching_brace(sci
, pos
- 1);
1412 if ((editor_prefs
.autoclose_chars
& GEANY_AC_PARENTHESIS
) && end_pos
== -1)
1416 if ((editor_prefs
.autoclose_chars
& GEANY_AC_CBRACKET
) && end_pos
== -1)
1420 if ((editor_prefs
.autoclose_chars
& GEANY_AC_SBRACKET
) && end_pos
== -1)
1424 if (editor_prefs
.autoclose_chars
& GEANY_AC_SQUOTE
)
1428 if (editor_prefs
.autoclose_chars
& GEANY_AC_DQUOTE
)
1429 closing_char
= "\"";
1433 if (closing_char
!= NULL
)
1435 sci_add_text(sci
, closing_char
);
1436 sci_set_current_position(sci
, pos
, TRUE
);
1441 /* Finds a corresponding matching brace to the given pos
1442 * (this is taken from Scintilla Editor.cxx,
1443 * fit to work with close_block) */
1444 static gint
brace_match(ScintillaObject
*sci
, gint pos
)
1446 gchar chBrace
= sci_get_char_at(sci
, pos
);
1447 gchar chSeek
= utils_brace_opposite(chBrace
);
1449 gint direction
= -1;
1454 styBrace
= sci_get_style_at(sci
, pos
);
1456 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1459 pos
= pos
+ direction
;
1460 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1462 chAtPos
= sci_get_char_at(sci
, pos
- 1);
1463 styAtPos
= sci_get_style_at(sci
, pos
);
1465 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1467 if (chAtPos
== chBrace
)
1469 if (chAtPos
== chSeek
)
1474 pos
= pos
+ direction
;
1480 /* Called after typing '}'. */
1481 static void close_block(GeanyEditor
*editor
, gint pos
)
1484 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1485 gint x
= 0, cnt
= 0;
1486 gint line
, line_len
, eol_char_len
;
1487 gchar
*text
, *line_buf
;
1488 ScintillaObject
*sci
;
1489 gint line_indent
, last_indent
;
1491 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1493 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1496 doc
= editor
->document
;
1498 if (! lexer_has_braces(sci
))
1501 line
= sci_get_line_from_position(sci
, pos
);
1502 line_len
= sci_get_line_length(sci
, line
);
1503 /* set eol_char_len to 0 if on last line, because there is no EOL char */
1504 eol_char_len
= (line
== (sci_get_line_count(sci
) - 1)) ? 0 :
1505 editor_get_eol_char_len(editor
);
1507 /* check that the line is empty, to not kill text in the line */
1508 line_buf
= sci_get_line(sci
, line
);
1509 line_buf
[line_len
- eol_char_len
] = '\0';
1510 while (x
< (line_len
- eol_char_len
))
1512 if (isspace(line_buf
[x
]))
1518 if ((line_len
- eol_char_len
- 1) != cnt
)
1521 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1523 gint start_brace
= brace_match(sci
, pos
);
1525 if (start_brace
>= 0)
1528 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1529 gint size
= sci_get_line_indentation(sci
, brace_line
);
1530 gchar
*ind
= get_whitespace(iprefs
, size
);
1532 text
= g_strconcat(ind
, "}", NULL
);
1533 line_start
= sci_get_position_from_line(sci
, line
);
1534 sci_set_anchor(sci
, line_start
);
1535 sci_replace_sel(sci
, text
);
1540 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1543 /* GEANY_AUTOINDENT_CURRENTCHARS */
1544 line_indent
= sci_get_line_indentation(sci
, line
);
1545 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1547 if (line_indent
< last_indent
)
1549 line_indent
-= iprefs
->width
;
1550 line_indent
= MAX(0, line_indent
);
1551 sci_set_line_indentation(sci
, line
, line_indent
);
1555 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1556 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1557 * position can be -1, then the current position is used.
1558 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1559 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1560 const gchar
*wc
, gboolean stem
)
1562 gint line
, line_start
, startword
, endword
;
1564 ScintillaObject
*sci
;
1566 g_return_if_fail(editor
!= NULL
);
1570 pos
= sci_get_current_position(sci
);
1572 line
= sci_get_line_from_position(sci
, pos
);
1573 line_start
= sci_get_position_from_line(sci
, line
);
1574 startword
= pos
- line_start
;
1575 endword
= pos
- line_start
;
1578 chunk
= sci_get_line(sci
, line
);
1581 wc
= GEANY_WORDCHARS
;
1583 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1584 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1585 * TODO: improve this code */
1586 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || chunk
[startword
- 1] < 0))
1590 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || chunk
[endword
] < 0))
1594 if (startword
!= endword
)
1596 chunk
[endword
] = '\0';
1598 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1601 g_strlcpy(word
, "", wordlen
);
1607 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1608 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1609 * position can be -1, then the current position is used.
1610 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1611 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1614 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1619 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1620 * Otherwise NULL is returned.
1621 * Additional wordchars can be specified to define what to consider as a word.
1623 * @param editor The editor to operate on.
1624 * @param pos The position where the word should be read from.
1625 * Maybe @c -1 to use the current position.
1626 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1627 * as part of a word. Maybe @c NULL to use the default wordchars,
1628 * see @ref GEANY_WORDCHARS.
1630 * @return A newly-allocated string containing the word at the given @a pos or @c NULL.
1631 * Should be freed when no longer needed.
1635 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1637 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1639 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1641 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1643 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1647 /* Read the word up to position @a pos. */
1648 static const gchar
*
1649 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1651 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1653 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1655 return (*word
) ? word
: NULL
;
1659 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1662 gint orig_pos
= pos
;
1664 c
= sci_get_char_at(sci
, pos
);
1665 while (pos
>= 0 && pos
> orig_pos
- 300)
1667 c
= sci_get_char_at(sci
, pos
);
1669 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1676 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1680 gint orig_pos
= pos
;
1682 c
= sci_get_char_at(sci
, pos
);
1683 while (pos
> 0 && pos
> orig_pos
- 300)
1685 c
= sci_get_char_at(sci
, pos
);
1686 if (c
== ')') brackets
++;
1687 else if (c
== '(') brackets
--;
1689 if (brackets
< 0) return pos
; /* found start bracket */
1695 static gboolean
append_calltip(GString
*str
, const TMTag
*tag
, filetype_id ft_id
)
1697 if (! tag
->atts
.entry
.arglist
)
1700 if (ft_id
!= GEANY_FILETYPES_PASCAL
)
1701 { /* usual calltips: "retval tagname (arglist)" */
1702 if (tag
->atts
.entry
.var_type
)
1706 g_string_append(str
, tag
->atts
.entry
.var_type
);
1707 for (i
= 0; i
< tag
->atts
.entry
.pointerOrder
; i
++)
1709 g_string_append_c(str
, '*');
1711 g_string_append_c(str
, ' ');
1713 if (tag
->atts
.entry
.scope
)
1715 const gchar
*cosep
= symbols_get_context_separator(ft_id
);
1717 g_string_append(str
, tag
->atts
.entry
.scope
);
1718 g_string_append(str
, cosep
);
1720 g_string_append(str
, tag
->name
);
1721 g_string_append_c(str
, ' ');
1722 g_string_append(str
, tag
->atts
.entry
.arglist
);
1725 { /* special case Pascal calltips: "tagname (arglist) : retval" */
1726 g_string_append(str
, tag
->name
);
1727 g_string_append_c(str
, ' ');
1728 g_string_append(str
, tag
->atts
.entry
.arglist
);
1730 if (NZV(tag
->atts
.entry
.var_type
))
1732 g_string_append(str
, " : ");
1733 g_string_append(str
, tag
->atts
.entry
.var_type
);
1741 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1743 const GPtrArray
*tags
;
1744 const gint arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1745 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1746 TMTagAttrType
*attrs
= NULL
;
1748 GString
*str
= NULL
;
1751 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1753 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1754 tags
= tm_workspace_find(word
, tm_tag_max_t
, attrs
, FALSE
, ft
->lang
);
1758 tag
= TM_TAG(tags
->pdata
[0]);
1760 if (tag
->type
== tm_tag_class_t
&& FILETYPE_ID(ft
) == GEANY_FILETYPES_D
)
1762 /* user typed e.g. 'new Classname(' so lookup D constructor Classname::this() */
1763 tags
= tm_workspace_find_scoped("this", tag
->name
,
1764 arg_types
, attrs
, FALSE
, ft
->lang
, TRUE
);
1769 /* remove tags with no argument list */
1770 for (i
= 0; i
< tags
->len
; i
++)
1772 tag
= TM_TAG(tags
->pdata
[i
]);
1774 if (! tag
->atts
.entry
.arglist
)
1775 tags
->pdata
[i
] = NULL
;
1777 tm_tags_prune((GPtrArray
*) tags
);
1781 { /* remove duplicate calltips */
1782 TMTagAttrType sort_attr
[] = {tm_tag_attr_name_t
, tm_tag_attr_scope_t
,
1783 tm_tag_attr_arglist_t
, 0};
1785 tm_tags_sort((GPtrArray
*) tags
, sort_attr
, TRUE
);
1788 /* if the current word has changed since last time, start with the first tag match */
1789 if (! utils_str_equal(word
, calltip
.last_word
))
1790 calltip
.tag_index
= 0;
1791 /* cache the current word for next time */
1792 g_free(calltip
.last_word
);
1793 calltip
.last_word
= g_strdup(word
);
1794 calltip
.tag_index
= MIN(calltip
.tag_index
, tags
->len
- 1); /* ensure tag_index is in range */
1796 for (i
= calltip
.tag_index
; i
< tags
->len
; i
++)
1798 tag
= TM_TAG(tags
->pdata
[i
]);
1802 str
= g_string_new(NULL
);
1803 if (calltip
.tag_index
> 0)
1804 g_string_prepend(str
, "\001 "); /* up arrow */
1805 append_calltip(str
, tag
, FILETYPE_ID(ft
));
1807 else /* add a down arrow */
1809 if (calltip
.tag_index
> 0) /* already have an up arrow */
1810 g_string_insert_c(str
, 1, '\002');
1812 g_string_prepend(str
, "\002 ");
1818 gchar
*result
= str
->str
;
1820 g_string_free(str
, FALSE
);
1827 /* use pos = -1 to search for the previous unmatched open bracket. */
1828 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
1830 gint orig_pos
= pos
; /* the position for the calltip */
1833 gchar word
[GEANY_MAX_WORD_LENGTH
];
1835 ScintillaObject
*sci
;
1837 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1838 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
1842 lexer
= sci_get_lexer(sci
);
1846 /* position of '(' is unknown, so go backwards from current position to find it */
1847 pos
= sci_get_current_position(sci
);
1850 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
1851 find_start_bracket(sci
, pos
);
1856 /* the style 1 before the brace (which may be highlighted) */
1857 style
= sci_get_style_at(sci
, pos
- 1);
1858 if (! is_code_style(lexer
, style
))
1862 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
1863 if (word
[0] == '\0')
1866 str
= find_calltip(word
, editor
->document
->file_type
);
1869 g_free(calltip
.text
); /* free the old calltip */
1871 calltip
.pos
= orig_pos
;
1874 utils_wrap_string(calltip
.text
, -1);
1875 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
1882 gchar
*editor_get_calltip_text(GeanyEditor
*editor
, const TMTag
*tag
)
1886 g_return_val_if_fail(editor
!= NULL
, NULL
);
1888 str
= g_string_new(NULL
);
1889 if (append_calltip(str
, tag
, editor
->document
->file_type
->id
))
1890 return g_string_free(str
, FALSE
);
1892 return g_string_free(str
, TRUE
);
1896 /* HTML entities auto completion from html_entities.tags text file */
1898 autocomplete_html(ScintillaObject
*sci
, const gchar
*root
, gsize rootlen
)
1901 gboolean found
= FALSE
;
1903 const gchar
**entities
= symbols_get_html_entities();
1905 if (*root
!= '&' || G_UNLIKELY(entities
== NULL
))
1908 words
= g_string_sized_new(500);
1911 if (entities
[i
] == NULL
)
1913 else if (entities
[i
][0] == '#')
1916 if (! strncmp(entities
[i
], root
, rootlen
))
1919 g_string_append_c(words
, '\n');
1920 g_string_append(words
, entities
[i
]);
1925 show_autocomplete(sci
, rootlen
, words
->str
);
1927 g_string_free(words
, TRUE
);
1932 /* Current document & global tags autocompletion */
1934 autocomplete_tags(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
1936 TMTagAttrType attrs
[] = { tm_tag_attr_name_t
, 0 };
1937 const GPtrArray
*tags
;
1940 g_return_val_if_fail(editor
, FALSE
);
1942 doc
= editor
->document
;
1944 tags
= tm_workspace_find(root
, tm_tag_max_t
, attrs
, TRUE
, doc
->file_type
->lang
);
1947 show_tags_list(editor
, tags
, rootlen
);
1948 return tags
->len
> 0;
1954 /* Check whether to use entity autocompletion:
1955 * - always in a HTML file except when inside embedded JavaScript, Python, ASP, ...
1956 * - in a PHP file only when we are outside of <? ?> */
1957 static gboolean
autocomplete_check_for_html(gint ft_id
, gint style
)
1959 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
1960 * (everything after SCE_HJ_START is for embedded scripting languages) */
1961 if (ft_id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
1964 if (ft_id
== GEANY_FILETYPES_PHP
)
1966 /* use entity completion when style is outside of PHP styles */
1967 if (! is_style_php(style
))
1975 /* Algorithm based on based on Scite's StartAutoCompleteWord() */
1976 static GString
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
1979 gint len
, current
, word_end
;
1980 gint pos_find
, flags
;
1984 struct Sci_TextToFind ttf
;
1986 len
= sci_get_length(sci
);
1987 current
= sci_get_current_position(sci
) - rootlen
;
1989 ttf
.lpstrText
= root
;
1991 ttf
.chrg
.cpMax
= len
;
1992 ttf
.chrgText
.cpMin
= 0;
1993 ttf
.chrgText
.cpMax
= 0;
1994 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
1996 words
= g_string_sized_new(256);
1997 /* put space before first entry to make searching with strstr easy */
1998 g_string_append_c(words
, ' ');
2000 /* search the whole document for the word root and collect results */
2001 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2002 while (pos_find
>= 0 && pos_find
< len
)
2004 word_end
= pos_find
+ rootlen
;
2005 if (pos_find
!= current
)
2007 while (word_end
< len
&& strchr(GEANY_WORDCHARS
, sci_get_char_at(sci
, word_end
)) != NULL
)
2010 word_length
= word_end
- pos_find
;
2011 if (word_length
> rootlen
)
2013 word
= g_malloc0(word_length
+ 3);
2014 sci_get_text_range(sci
, pos_find
, word_end
, word
+ 1);
2016 word
[word_length
+ 1] = ' ';
2017 /* search the words string whether we already have the word in, otherwise add it */
2018 if (strstr(words
->str
, word
) == NULL
)
2020 g_string_append(words
, word
+ 1);
2025 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
2029 ttf
.chrg
.cpMin
= word_end
;
2030 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
2035 g_strdelimit(words
->str
, " ", '\n');
2036 words
->str
[words
->len
- 1] = '\0'; /* remove the trailing '\n' */
2039 g_string_free(words
, TRUE
);
2044 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
2046 ScintillaObject
*sci
= editor
->sci
;
2050 GSList
*node
, *list
= NULL
;
2052 words
= get_doc_words(sci
, root
, rootlen
);
2055 scintilla_send_message(sci
, SCI_AUTOCCANCEL
, 0, 0);
2059 /* words are unsorted, make list of words */
2060 foreach_str(ptr
, words
->str
)
2064 list
= g_slist_prepend(list
, ptr
+ 1);
2065 /* terminate previous string in list */
2070 list
= g_slist_sort(list
, (GCompareFunc
)utils_str_casecmp
);
2072 str
= g_string_sized_new(words
->len
);
2073 foreach_slist(node
, list
)
2075 g_string_append(str
, node
->data
);
2077 g_string_append_c(str
, '\n');
2079 if (g_slist_length(list
) >= editor_prefs
.autocompletion_max_entries
)
2080 g_string_append(str
, "\n...");
2083 g_string_free(words
, TRUE
);
2085 show_autocomplete(sci
, rootlen
, str
->str
);
2086 g_string_free(str
, TRUE
);
2091 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
2093 gint line
, line_start
, line_len
, line_pos
, current
, rootlen
, startword
, lexer
, style
;
2094 gchar
*linebuf
, *root
;
2095 ScintillaObject
*sci
;
2096 gboolean ret
= FALSE
;
2097 const gchar
*wordchars
;
2100 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
2103 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2105 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
2106 * necessary styling information */
2107 if (G_UNLIKELY(pos
< 2))
2111 ft
= editor
->document
->file_type
;
2113 line
= sci_get_line_from_position(sci
, pos
);
2114 line_start
= sci_get_position_from_line(sci
, line
);
2115 line_len
= sci_get_line_length(sci
, line
);
2116 line_pos
= pos
- line_start
- 1;
2117 current
= pos
- line_start
;
2118 startword
= current
;
2119 lexer
= sci_get_lexer(sci
);
2120 style
= sci_get_style_at(sci
, pos
- 2);
2122 /* don't autocomplete in comments and strings */
2123 if (!force
&& !is_code_style(lexer
, style
))
2126 autocomplete_scope(editor
);
2128 linebuf
= sci_get_line(sci
, line
);
2130 if (ft
->id
== GEANY_FILETYPES_LATEX
)
2131 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
2132 else if (ft
->id
== GEANY_FILETYPES_HTML
|| ft
->id
== GEANY_FILETYPES_PHP
)
2133 wordchars
= GEANY_WORDCHARS
"&"; /* add & to word chars if we are in a PHP or HTML file */
2135 wordchars
= GEANY_WORDCHARS
;
2137 /* find the start of the current word */
2138 while ((startword
> 0) && (strchr(wordchars
, linebuf
[startword
- 1])))
2140 linebuf
[current
] = '\0';
2141 root
= linebuf
+ startword
;
2142 rootlen
= current
- startword
;
2146 if (autocomplete_check_for_html(ft
->id
, style
))
2148 /* Allow something like ""some text"". The above startword calculation
2149 * only works on words but for HTML entity completion we also want to have completion
2150 * based on '&' within words. */
2151 gchar
*tmp
= strchr(root
, '&');
2155 rootlen
= strlen(tmp
);
2157 ret
= autocomplete_html(sci
, root
, rootlen
);
2161 /* force is set when called by keyboard shortcut, otherwise start at the
2162 * editor_prefs.symbolcompletion_min_chars'th char */
2163 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
2165 /* complete tags, except if forcing when completion is already visible */
2166 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
2167 ret
= autocomplete_tags(editor
, root
, rootlen
);
2169 /* If forcing and there's nothing else to show, complete from words in document */
2170 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2171 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2183 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2185 gchar
*result
= NULL
;
2188 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2190 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2193 result
= g_hash_table_lookup(tmp
, name
);
2195 /* whether nothing is set for the current filetype(tmp is NULL) or
2196 * the particular completion for this filetype is not set (result is NULL) */
2197 if (tmp
== NULL
|| result
== NULL
)
2199 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2202 result
= g_hash_table_lookup(tmp
, name
);
2205 /* if result is still NULL here, no completion could be found */
2207 /* result is owned by the hash table and will be freed when the table will destroyed */
2212 /* This is very ugly but passing the pattern to ac_replace_specials() doesn't work because it is
2213 * modified when replacing a completion but the foreach function still passes the old pointer
2214 * to ac_replace_specials, so we use a global pointer outside of ac_replace_specials and
2215 * ac_complete_constructs. Any hints to improve this are welcome. */
2216 static GString
*snippets_global_pattern
= NULL
;
2218 static void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2222 g_return_if_fail(key
!= NULL
);
2223 g_return_if_fail(value
!= NULL
);
2225 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2227 utils_string_replace_all(snippets_global_pattern
, needle
, (gchar
*) value
);
2232 /* this only works with spaces only indentation on the lines */
2233 static void fix_line_indents(GeanyEditor
*editor
, gint line_start
, gint line_end
)
2235 ScintillaObject
*sci
= editor
->sci
;
2236 gint line
, cur_line
, cur_col
, pos
;
2238 /* get the line, col position as fixing indentation will move cursor to start of line */
2239 pos
= sci_get_current_position(sci
);
2240 cur_col
= sci_get_col_from_position(sci
, pos
);
2241 cur_line
= sci_get_current_line(sci
);
2243 for (line
= line_start
; line
<= line_end
; line
++)
2245 gint size
= sci_get_line_indentation(sci
, line
);
2247 /* set to 0 first to trigger proper indent creation */
2248 sci_set_line_indentation(sci
, line
, 0);
2249 sci_set_line_indentation(sci
, line
, size
);
2251 pos
= scintilla_send_message(sci
, SCI_FINDCOLUMN
, cur_line
, cur_col
);
2252 sci_set_current_position(sci
, pos
, FALSE
);
2256 static void replace_leading_tabs(GString
*str
, const gchar
*whitespace
)
2260 regmatch_t matches
[2];
2263 if (regcomp(®ex
, "^ *(\t)", 0) != 0)
2265 g_return_if_fail(FALSE
);
2270 if (regexec(®ex
, ptr
,
2271 G_N_ELEMENTS(matches
), matches
, 0) != 0)
2274 pos
= matches
[1].rm_so
;
2275 g_return_if_fail(pos
>= 0);
2276 pos
+= ptr
- str
->str
;
2277 g_string_erase(str
, pos
, 1);
2278 g_string_insert(str
, pos
, whitespace
);
2279 ptr
= str
->str
+ pos
+ strlen(whitespace
);
2285 /** Inserts text, replacing \\t tab chars (@c 0x9) and \\n newline chars (@c 0xA)
2286 * accordingly for the document.
2287 * - Leading tabs are replaced with the correct indentation.
2288 * - Non-leading tabs are replaced with spaces (except when using 'Tabs' indent type).
2289 * - Newline chars are replaced with the correct line ending string.
2290 * This is very useful for inserting code without having to handle the indent
2291 * type yourself (Tabs & Spaces mode can be tricky).
2292 * @param editor Editor.
2293 * @param text Intended as e.g. @c "if (foo)\n\tbar();".
2294 * @param insert_pos Document position to insert text at.
2295 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2296 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2297 * -1 to read the indent size from the line with @a insert_pos on it.
2298 * @param replace_newlines Whether to replace newlines. If
2299 * newlines have been replaced already, this should be false, to avoid errors e.g. on Windows.
2300 * @warning Make sure all \\t tab chars in @a text are intended as indent widths or alignment,
2301 * not hard tabs, as those won't be preserved.
2302 * @note This doesn't scroll the cursor in view afterwards. **/
2303 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2304 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2306 ScintillaObject
*sci
= editor
->sci
;
2307 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2311 const gchar cur_marker
[] = "__GEANY_CURSOR_MARKER__";
2312 const gchar
*eol
= editor_get_eol_char(editor
);
2313 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2315 g_return_if_fail(text
);
2316 g_return_if_fail(editor
!= NULL
);
2317 g_return_if_fail(insert_pos
>= 0);
2319 buf
= g_string_new(text
);
2321 if (cursor_index
>= 0)
2322 g_string_insert(buf
, cursor_index
, cur_marker
); /* remember cursor pos */
2324 if (newline_indent_size
== -1)
2326 /* count indent size up to insert_pos instead of asking sci
2327 * because there may be spaces after it */
2328 gchar
*tmp
= sci_get_line(sci
, line_start
);
2329 gint idx
= insert_pos
- sci_get_position_from_line(sci
, line_start
);
2331 newline_indent_size
= count_indent_size(editor
, tmp
);
2335 /* Add line indents (in spaces) */
2336 if (newline_indent_size
> 0)
2338 whitespace
= g_strnfill(newline_indent_size
, ' ');
2339 setptr(whitespace
, g_strconcat(eol
, whitespace
, NULL
));
2340 utils_string_replace_all(buf
, eol
, whitespace
);
2344 /* transform line endings */
2345 if (replace_newlines
)
2346 utils_string_replace_all(buf
, "\n", eol
);
2348 /* transform leading tabs into indent widths (in spaces) */
2349 whitespace
= g_strnfill(iprefs
->width
, ' ');
2350 replace_leading_tabs(buf
, whitespace
);
2351 /* remaining tabs are for alignment */
2352 if (iprefs
->type
!= GEANY_INDENT_TYPE_TABS
)
2353 utils_string_replace_all(buf
, "\t", whitespace
);
2356 sci_start_undo_action(sci
);
2358 if (cursor_index
>= 0)
2360 gint idx
= utils_strpos(buf
->str
, cur_marker
);
2362 g_string_erase(buf
, idx
, strlen(cur_marker
));
2364 sci_insert_text(sci
, insert_pos
, buf
->str
);
2365 sci_set_current_position(sci
, insert_pos
+ idx
, FALSE
);
2368 sci_insert_text(sci
, insert_pos
, buf
->str
);
2370 /* fixup indentation (very useful for Tabs & Spaces indent type) */
2371 line_end
= sci_get_line_from_position(sci
, insert_pos
+ buf
->len
);
2372 fix_line_indents(editor
, line_start
, line_end
);
2373 snippet_cursor_insert_pos
= sci_get_current_position(sci
);
2375 sci_end_undo_action(sci
);
2376 g_string_free(buf
, TRUE
);
2380 /* Move the cursor to the next specified cursor position in an inserted snippet.
2381 * Can, and should, be optimized to give better results */
2382 void editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2384 ScintillaObject
*sci
= editor
->sci
;
2385 gint current_pos
= sci_get_current_position(sci
);
2387 if (snippet_offsets
&& !g_queue_is_empty(snippet_offsets
))
2391 offset
= GPOINTER_TO_INT(g_queue_pop_head(snippet_offsets
));
2392 if (current_pos
> snippet_cursor_insert_pos
)
2393 snippet_cursor_insert_pos
= offset
+ current_pos
;
2395 snippet_cursor_insert_pos
+= offset
;
2397 sci_set_current_position(sci
, snippet_cursor_insert_pos
, FALSE
);
2406 static gssize
snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
,
2409 gssize cur_index
= -1;
2411 gint i
, tmp_pos
, whitespace_len
, nl_count
= 0;
2412 GHashTable
*specials
;
2413 GList
*temp_list
= NULL
;
2414 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2415 gint cursor_steps
, old_cursor
= 0;
2417 /* replace 'special' completions */
2418 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2419 if (G_LIKELY(specials
!= NULL
))
2421 /* ugly hack using global_pattern */
2422 snippets_global_pattern
= pattern
;
2423 g_hash_table_foreach(specials
, snippets_replace_specials
, NULL
);
2426 /* replace any template {foo} wildcards */
2427 templates_replace_common(pattern
, editor
->document
->file_name
, editor
->document
->file_type
, NULL
);
2429 /* transform other wildcards */
2430 /* convert to %newlines%, else we get endless loops */
2431 utils_string_replace_all(pattern
, "\n", "%newline%");
2433 /* if spaces are used, replaces all tabs with %ws%, which is later replaced
2434 * by real whitespace characters
2435 * otherwise replace all %ws% by \t, which will be replaced later by tab
2437 * this makes seperating between tab and spaces intentation pretty easy */
2438 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
2439 utils_string_replace_all(pattern
, "\t", "%ws%");
2441 utils_string_replace_all(pattern
, "%ws%", "\t");
2443 whitespace
= g_strnfill(iprefs
->width
, ' '); /* use spaces for indentation, will be fixed up */
2444 whitespace_len
= strlen(whitespace
);
2446 while ((cursor_steps
= utils_strpos(pattern
->str
, "%cursor%")) >= 0)
2448 /* replace every %newline% (up to next %cursor%) with EOL,
2449 * and update cursor_steps after */
2450 while ((tmp_pos
= utils_strpos(pattern
->str
, "%newline%")) < cursor_steps
&& tmp_pos
!= -1)
2453 utils_string_replace_first(pattern
, "%newline%", editor_get_eol_char(editor
));
2454 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2456 /* replace every %ws% (up to next %cursor%) with whitespaces,
2457 * and update cursor_steps after */
2458 while ((tmp_pos
= utils_strpos(pattern
->str
, "%ws%")) < cursor_steps
&& tmp_pos
!= -1)
2460 utils_string_replace_first(pattern
, "%ws%", whitespace
);
2461 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2463 /* finally replace the next %cursor% */
2464 utils_string_replace_first(pattern
, "%cursor%", "");
2466 /* modify cursor_steps to take indentation count and type into account */
2468 /* We're saving the relative offset to each cursor position in a simple
2469 * linked list, including indentations between them. */
2472 cursor_steps
+= (nl_count
* indent_size
);
2473 temp_list
= g_list_append(temp_list
, GINT_TO_POINTER(cursor_steps
- old_cursor
));
2478 cur_index
= cursor_steps
;
2480 old_cursor
= cursor_steps
;
2482 /* replace remaining %ws% and %newline% which may occur after the last %cursor% */
2483 utils_string_replace_all(pattern
, "%newline%", editor_get_eol_char(editor
));
2484 utils_string_replace_all(pattern
, "%ws%", whitespace
);
2488 /* Bug: {ob}pc{cb} will be replaced by % too */
2489 templates_replace_valist(pattern
, "{pc}", "%", NULL
);
2491 /* We put the cursor positions for the most recent
2492 * parsed snippet first, followed by any remaining positions */
2498 foreach_list(node
, temp_list
)
2499 g_queue_push_nth(snippet_offsets
, node
->data
, i
++);
2501 /* limit length of queue */
2502 while (g_queue_get_length(snippet_offsets
) > 20)
2503 g_queue_pop_tail(snippet_offsets
);
2505 g_list_free(temp_list
);
2508 cur_index
= pattern
->len
;
2514 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2516 ScintillaObject
*sci
= editor
->sci
;
2519 gssize cur_index
= -1;
2521 gint ft_id
= editor
->document
->file_type
->id
;
2523 str
= g_strdup(word
);
2525 pattern
= g_string_new(snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
));
2526 if (pattern
== NULL
|| pattern
->len
== 0)
2529 g_string_free(pattern
, TRUE
);
2533 read_indent(editor
, pos
);
2535 /* remove the typed word, it will be added again by the used auto completion
2536 * (not really necessary but this makes the auto completion more flexible,
2537 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2538 str_len
= strlen(str
);
2539 sci_set_selection_start(sci
, pos
- str_len
);
2540 sci_set_selection_end(sci
, pos
);
2541 sci_replace_sel(sci
, "");
2542 pos
-= str_len
; /* pos has changed while deleting */
2544 cur_index
= snippets_make_replacements(editor
, pattern
, strlen(indent
));
2546 /* finally insert the text and set the cursor */
2547 editor_insert_text_block(editor
, pattern
->str
, pos
, cur_index
, -1, FALSE
);
2548 sci_scroll_caret(sci
);
2551 g_string_free(pattern
, TRUE
);
2556 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2558 gint line
= sci_get_line_from_position(sci
, pos
);
2561 /* skip any trailing spaces */
2564 c
= sci_get_char_at(sci
, pos
);
2565 if (c
== ' ' || c
== '\t')
2571 return (pos
== sci_get_line_end_position(sci
, line
));
2575 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2577 gboolean result
= FALSE
;
2580 ScintillaObject
*sci
;
2582 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2585 if (sci_has_selection(sci
))
2587 /* return if we are editing an existing line (chars on right of cursor) */
2588 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2589 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_space
&&
2590 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2593 wc
= snippets_find_completion_by_name("Special", "wordchars");
2594 word
= editor_read_word_stem(editor
, pos
, wc
);
2596 /* prevent completion of "for " */
2598 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2600 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2601 result
= snippets_complete_constructs(editor
, pos
, word
);
2602 sci_end_undo_action(sci
);
2604 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2610 void editor_show_macro_list(GeanyEditor
*editor
)
2614 if (editor
== NULL
|| editor
->document
->file_type
== NULL
)
2617 words
= symbols_get_macro_list(editor
->document
->file_type
->lang
);
2621 SSM(editor
->sci
, SCI_USERLISTSHOW
, 1, (sptr_t
) words
->str
);
2622 g_string_free(words
, TRUE
);
2626 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2628 ScintillaObject
*sci
= editor
->sci
;
2629 gchar
*to_insert
= NULL
;
2633 const gchar
*gt
= ">";
2634 /* if there is already a '>' behind the cursor, don't add it */
2635 if (sci_get_char_at(sci
, pos
) == '>')
2638 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2641 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2643 sci_start_undo_action(sci
);
2644 sci_replace_sel(sci
, to_insert
);
2646 sci_set_selection(sci
, pos
, pos
);
2647 sci_end_undo_action(sci
);
2653 * (stolen from anjuta and heavily modified)
2654 * This routine will auto complete XML or HTML tags that are still open by closing them
2655 * @param ch The character we are dealing with, currently only works with the '>' character
2656 * @return True if handled, false otherwise
2658 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2660 ScintillaObject
*sci
= editor
->sci
;
2661 gint lexer
= sci_get_lexer(sci
);
2662 gint min
, size
, style
;
2663 gchar
*str_found
, sel
[512];
2664 gboolean result
= FALSE
;
2666 /* If the user has turned us off, quit now.
2667 * This may make sense only in certain languages */
2668 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2671 /* return if we are inside any embedded script */
2672 style
= sci_get_style_at(sci
, pos
);
2673 if (style
> SCE_H_XCCOMMENT
&& ! is_string_style(lexer
, style
))
2676 /* if ch is /, check for </, else quit */
2677 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2680 /* Grab the last 512 characters or so */
2681 min
= pos
- (sizeof(sel
) - 1);
2682 if (min
< 0) min
= 0;
2685 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2687 sci_get_text_range(sci
, min
, pos
, sel
);
2688 sel
[sizeof(sel
) - 1] = '\0';
2690 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2691 /* User typed something like "<br/>" */
2696 size
-= 2; /* skip </ */
2697 str_found
= utils_find_open_xml_tag(sel
, size
);
2699 if (lexer
== SCLEX_HTML
&& utils_is_short_html_tag(str_found
))
2703 else if (NZV(str_found
))
2705 insert_closing_tag(editor
, pos
, ch
, str_found
);
2713 /* like sci_get_line_indentation(), but for a string. */
2714 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2717 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2720 g_return_val_if_fail(base_indent
, 0);
2722 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2740 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2743 gchar
*str_begin
, *str_end
, *co
, *cc
;
2746 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2748 eol
= editor_get_eol_char(editor
);
2749 co
= editor
->document
->file_type
->comment_open
;
2750 cc
= editor
->document
->file_type
->comment_close
;
2751 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2752 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2754 /* insert the comment strings */
2755 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2756 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2757 sci_insert_text(editor
->sci
, line_len
, str_end
);
2764 static void real_uncomment_multiline(GeanyEditor
*editor
)
2766 /* find the beginning of the multi line comment */
2767 gint pos
, line
, len
, x
;
2771 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2772 doc
= editor
->document
;
2774 /* remove comment open chars */
2775 pos
= document_find_text(doc
, doc
->file_type
->comment_open
, 0, TRUE
, FALSE
, NULL
);
2776 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2778 /* check whether the line is empty and can be deleted */
2779 line
= sci_get_line_from_position(editor
->sci
, pos
);
2780 len
= sci_get_line_length(editor
->sci
, line
);
2781 linebuf
= sci_get_line(editor
->sci
, line
);
2783 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2784 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2787 /* remove comment close chars */
2788 pos
= document_find_text(doc
, doc
->file_type
->comment_close
, 0, FALSE
, FALSE
, NULL
);
2789 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2791 /* check whether the line is empty and can be deleted */
2792 line
= sci_get_line_from_position(editor
->sci
, pos
);
2793 len
= sci_get_line_length(editor
->sci
, line
);
2794 linebuf
= sci_get_line(editor
->sci
, line
);
2796 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2797 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2802 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2804 gint lexer
= sci_get_lexer(editor
->sci
);
2807 /* List only those lexers which support multi line comments */
2813 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2814 style_comment
= SCE_HPHP_COMMENT
;
2816 style_comment
= SCE_H_COMMENT
;
2819 case SCLEX_HASKELL
: style_comment
= SCE_HA_COMMENTBLOCK
; break;
2820 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2821 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2822 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2823 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2824 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2825 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2826 default: style_comment
= SCE_C_COMMENT
;
2829 return style_comment
;
2833 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2834 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2835 * it returns just 1 */
2836 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2838 gint first_line
, last_line
, eol_char_len
;
2839 gint x
, i
, line_start
, line_len
;
2840 gint sel_start
, sel_end
;
2843 gchar sel
[256], *co
, *cc
;
2844 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2847 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
2850 { /* use selection or current line */
2851 sel_start
= sci_get_selection_start(editor
->sci
);
2852 sel_end
= sci_get_selection_end(editor
->sci
);
2854 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
2855 /* Find the last line with chars selected (not EOL char) */
2856 last_line
= sci_get_line_from_position(editor
->sci
,
2857 sel_end
- editor_get_eol_char_len(editor
));
2858 last_line
= MAX(first_line
, last_line
);
2862 first_line
= last_line
= line
;
2863 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
2866 ft
= editor
->document
->file_type
;
2867 eol_char_len
= editor_get_eol_char_len(editor
);
2869 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
2870 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
2871 if (ft
->id
== GEANY_FILETYPES_PHP
)
2873 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2874 ft
= filetypes
[GEANY_FILETYPES_XML
];
2877 co
= ft
->comment_open
;
2878 cc
= ft
->comment_close
;
2882 co_len
= strlen(co
);
2886 sci_start_undo_action(editor
->sci
);
2888 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
2892 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2893 line_len
= sci_get_line_length(editor
->sci
, i
);
2896 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- eol_char_len
);
2899 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2900 sel
[buf_len
] = '\0';
2902 while (isspace(sel
[x
])) x
++;
2904 /* to skip blank lines */
2905 if (x
< line_len
&& sel
[x
] != '\0')
2907 /* use single line comment */
2908 if (cc
== NULL
|| strlen(cc
) == 0)
2914 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2915 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
2916 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
2923 if (strncmp(sel
+ x
, co
, co_len
) != 0)
2927 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
2928 sci_replace_sel(editor
->sci
, "");
2931 /* use multi line comment */
2936 /* skip lines which are already comments */
2937 style_comment
= get_multiline_comment_style(editor
, line_start
);
2938 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
2940 real_uncomment_multiline(editor
);
2944 /* break because we are already on the last line */
2950 sci_end_undo_action(editor
->sci
);
2952 /* restore selection if there is one
2953 * but don't touch the selection if caller is editor_do_comment_toggle */
2954 if (! toggle
&& sel_start
< sel_end
)
2958 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
2959 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
2963 gint eol_len
= editor_get_eol_char_len(editor
);
2964 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
2965 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
2973 void editor_do_comment_toggle(GeanyEditor
*editor
)
2975 gint first_line
, last_line
, eol_char_len
;
2976 gint x
, i
, line_start
, line_len
, first_line_start
;
2977 gint sel_start
, sel_end
;
2978 gint count_commented
= 0, count_uncommented
= 0;
2979 gchar sel
[256], *co
, *cc
;
2980 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2981 gboolean first_line_was_comment
= FALSE
;
2983 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2986 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2988 sel_start
= sci_get_selection_start(editor
->sci
);
2989 sel_end
= sci_get_selection_end(editor
->sci
);
2991 ft
= editor
->document
->file_type
;
2992 eol_char_len
= editor_get_eol_char_len(editor
);
2994 first_line
= sci_get_line_from_position(editor
->sci
,
2995 sci_get_selection_start(editor
->sci
));
2996 /* Find the last line with chars selected (not EOL char) */
2997 last_line
= sci_get_line_from_position(editor
->sci
,
2998 sci_get_selection_end(editor
->sci
) - editor_get_eol_char_len(editor
));
2999 last_line
= MAX(first_line
, last_line
);
3001 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
3002 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3003 if (ft
->id
== GEANY_FILETYPES_PHP
)
3005 if (! is_style_php(sci_get_style_at(editor
->sci
, first_line_start
)))
3006 ft
= filetypes
[GEANY_FILETYPES_XML
];
3009 co
= ft
->comment_open
;
3010 cc
= ft
->comment_close
;
3014 co_len
= strlen(co
);
3018 sci_start_undo_action(editor
->sci
);
3020 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3024 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3025 line_len
= sci_get_line_length(editor
->sci
, i
);
3028 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- eol_char_len
);
3031 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3032 sel
[buf_len
] = '\0';
3034 while (isspace(sel
[x
])) x
++;
3036 /* use single line comment */
3037 if (cc
== NULL
|| strlen(cc
) == 0)
3039 gboolean do_continue
= FALSE
;
3042 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
3043 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
3048 if (do_continue
&& i
== first_line
)
3049 first_line_was_comment
= TRUE
;
3053 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
3057 /* we are still here, so the above lines were not already comments, so comment it */
3058 editor_do_comment(editor
, i
, TRUE
, TRUE
);
3061 /* use multi line comment */
3066 /* skip lines which are already comments */
3067 style_comment
= get_multiline_comment_style(editor
, line_start
);
3068 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3070 real_uncomment_multiline(editor
);
3071 count_uncommented
++;
3075 real_comment_multiline(editor
, line_start
, last_line
);
3079 /* break because we are already on the last line */
3085 sci_end_undo_action(editor
->sci
);
3089 /* restore selection if there is one */
3090 if (sel_start
< sel_end
)
3094 gint a
= (first_line_was_comment
) ? - co_len
: co_len
;
3096 /* don't modify sel_start when the selection starts within indentation */
3097 read_indent(editor
, sel_start
);
3098 if ((sel_start
- first_line_start
) <= (gint
) strlen(indent
))
3101 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
3102 sci_set_selection_end(editor
->sci
, sel_end
+
3103 (count_commented
* co_len
) - (count_uncommented
* co_len
));
3107 gint eol_len
= editor_get_eol_char_len(editor
);
3108 if (count_uncommented
> 0)
3110 sci_set_selection_start(editor
->sci
, sel_start
- co_len
+ eol_len
);
3111 sci_set_selection_end(editor
->sci
, sel_end
- co_len
+ eol_len
);
3113 else if (count_commented
> 0)
3115 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
- eol_len
);
3116 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
- eol_len
);
3120 else if (count_uncommented
> 0)
3122 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
3123 sci_set_current_position(editor
->sci
, sel_start
- co_len
+ eol_len
, TRUE
);
3125 else if (count_commented
> 0)
3127 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
3128 sci_set_current_position(editor
->sci
, sel_start
+ co_len
- eol_len
, TRUE
);
3133 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
3134 void editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
)
3136 gint first_line
, last_line
, eol_char_len
;
3137 gint x
, i
, line_start
, line_len
;
3138 gint sel_start
, sel_end
, co_len
;
3139 gchar sel
[256], *co
, *cc
;
3140 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3143 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3146 { /* use selection or current line */
3147 sel_start
= sci_get_selection_start(editor
->sci
);
3148 sel_end
= sci_get_selection_end(editor
->sci
);
3150 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3151 /* Find the last line with chars selected (not EOL char) */
3152 last_line
= sci_get_line_from_position(editor
->sci
,
3153 sel_end
- editor_get_eol_char_len(editor
));
3154 last_line
= MAX(first_line
, last_line
);
3158 first_line
= last_line
= line
;
3159 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3162 ft
= editor
->document
->file_type
;
3163 eol_char_len
= editor_get_eol_char_len(editor
);
3165 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
3166 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3167 if (ft
->id
== GEANY_FILETYPES_PHP
)
3169 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
3170 ft
= filetypes
[GEANY_FILETYPES_XML
];
3173 co
= ft
->comment_open
;
3174 cc
= ft
->comment_close
;
3178 co_len
= strlen(co
);
3182 sci_start_undo_action(editor
->sci
);
3184 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3188 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3189 line_len
= sci_get_line_length(editor
->sci
, i
);
3192 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- eol_char_len
);
3195 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3196 sel
[buf_len
] = '\0';
3198 while (isspace(sel
[x
])) x
++;
3200 /* to skip blank lines */
3201 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3203 /* use single line comment */
3204 if (cc
== NULL
|| strlen(cc
) == 0)
3206 gint start
= line_start
;
3209 if (ft
->comment_use_indent
)
3210 start
= line_start
+ x
;
3214 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3215 sci_insert_text(editor
->sci
, start
, text
);
3219 sci_insert_text(editor
->sci
, start
, co
);
3221 /* use multi line comment */
3226 /* skip lines which are already comments */
3227 style_comment
= get_multiline_comment_style(editor
, line_start
);
3228 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3231 real_comment_multiline(editor
, line_start
, last_line
);
3233 /* break because we are already on the last line */
3239 sci_end_undo_action(editor
->sci
);
3241 /* restore selection if there is one
3242 * but don't touch the selection if caller is editor_do_comment_toggle */
3243 if (! toggle
&& sel_start
< sel_end
)
3247 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3248 sci_set_selection_end(editor
->sci
, sel_end
+ ((i
- first_line
) * co_len
));
3252 gint eol_len
= editor_get_eol_char_len(editor
);
3253 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3254 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3260 static gboolean brace_timeout_active
= FALSE
;
3262 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3264 GeanyDocument
*doc
= document_get_current();
3265 GeanyEditor
*editor
;
3266 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3267 gint end_pos
, cur_pos
;
3269 brace_timeout_active
= FALSE
;
3273 editor
= doc
->editor
;
3274 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3276 if (cur_pos
!= brace_pos
)
3279 if (cur_pos
!= brace_pos
)
3281 /* we have moved past the original brace_pos, but after the timeout
3282 * we may now be on a new brace, so check again */
3283 editor_highlight_braces(editor
, cur_pos
);
3287 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3289 editor_highlight_braces(editor
, cur_pos
);
3292 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3296 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3297 sci_get_col_from_position(editor
->sci
, end_pos
));
3298 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3299 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3303 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3304 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3310 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3312 gint brace_pos
= cur_pos
- 1;
3314 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3315 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3317 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3320 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3325 if (!brace_timeout_active
)
3327 brace_timeout_active
= TRUE
;
3328 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3329 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3334 static gboolean
in_block_comment(gint lexer
, gint style
)
3339 return (style
== SCE_C_COMMENT
||
3340 style
== SCE_C_COMMENTDOC
);
3343 return (style
== SCE_PAS_COMMENT
||
3344 style
== SCE_PAS_COMMENT2
);
3347 return (style
== SCE_D_COMMENT
||
3348 style
== SCE_D_COMMENTDOC
||
3349 style
== SCE_D_COMMENTNESTED
);
3352 return (style
== SCE_HPHP_COMMENT
);
3355 return (style
== SCE_CSS_COMMENT
);
3363 static gboolean
is_comment_char(gchar c
, gint lexer
)
3365 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3375 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3377 ScintillaObject
*sci
= editor
->sci
;
3378 gint indent_pos
, style
;
3379 gint lexer
= sci_get_lexer(sci
);
3381 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3382 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3383 style
= sci_get_style_at(sci
, indent_pos
);
3384 if (!in_block_comment(lexer
, style
))
3387 /* Check whether the comment block continues on this line */
3388 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3389 if (sci_get_style_at(sci
, indent_pos
) == style
)
3391 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3392 /* the type of comment, '*' (C/C++/Java), '+' and the others (D) */
3393 gchar
*continuation
= "*";
3394 gchar
*whitespace
= ""; /* to hold whitespace if needed */
3396 gint len
= strlen(previous_line
);
3399 /* find and stop at end of multi line comment */
3401 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3402 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3404 gint indent_len
, indent_width
;
3406 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3407 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3408 indent_width
= editor_get_indent_prefs(editor
)->width
;
3410 /* if there is one too many spaces, delete the last space,
3411 * to return to the indent used before the multiline comment was started. */
3412 if (indent_len
% indent_width
== 1)
3413 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3414 g_free(previous_line
);
3417 /* check whether we are on the second line of multi line comment */
3419 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3422 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3423 { /* we are on the second line of a multi line comment, so we have to insert white space */
3427 if (G_UNLIKELY(style
== SCE_D_COMMENTNESTED
))
3428 continuation
= "+"; /* for nested comments in D */
3430 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3431 sci_add_text(sci
, result
);
3434 g_free(previous_line
);
3439 /* Checks whether the given style is a string for the given lexer.
3440 * It doesn't handle LEX_HTML, this should be done by the caller.
3441 * Returns true if the style is a string, FALSE otherwise.
3443 * Don't forget STRINGEOL, to prevent completion whilst typing a string with no closing char.
3445 static gboolean
is_string_style(gint lexer
, gint style
)
3450 return (style
== SCE_C_CHARACTER
||
3451 style
== SCE_C_STRING
||
3452 style
== SCE_C_STRINGEOL
);
3455 return (style
== SCE_PAS_CHARACTER
||
3456 style
== SCE_PAS_STRING
||
3457 style
== SCE_PAS_STRINGEOL
);
3460 return (style
== SCE_D_STRING
||
3461 style
== SCE_D_STRINGEOL
||
3462 style
== SCE_D_CHARACTER
||
3463 style
== SCE_D_STRINGB
||
3464 style
== SCE_D_STRINGR
);
3467 return (style
== SCE_P_STRING
||
3468 style
== SCE_P_TRIPLE
||
3469 style
== SCE_P_TRIPLEDOUBLE
||
3470 style
== SCE_P_CHARACTER
||
3471 style
== SCE_P_STRINGEOL
);
3475 return (style
== SCE_F_STRING1
||
3476 style
== SCE_F_STRING2
||
3477 style
== SCE_F_STRINGEOL
);
3480 return (/*style == SCE_PL_STRING ||*/ /* may want variable autocompletion "$(foo)" */
3481 style
== SCE_PL_CHARACTER
||
3482 style
== SCE_PL_HERE_DELIM
||
3483 style
== SCE_PL_HERE_Q
||
3484 style
== SCE_PL_HERE_QQ
||
3485 style
== SCE_PL_HERE_QX
||
3486 style
== SCE_PL_POD
||
3487 style
== SCE_PL_STRING_Q
||
3488 style
== SCE_PL_STRING_QQ
||
3489 style
== SCE_PL_STRING_QX
||
3490 style
== SCE_PL_STRING_QR
||
3491 style
== SCE_PL_STRING_QW
||
3492 style
== SCE_PL_POD_VERB
);
3495 return (style
== SCE_R_STRING
);
3498 return (style
== SCE_RB_CHARACTER
||
3499 style
== SCE_RB_STRING
||
3500 style
== SCE_RB_HERE_DELIM
||
3501 style
== SCE_RB_HERE_Q
||
3502 style
== SCE_RB_HERE_QQ
||
3503 style
== SCE_RB_HERE_QX
||
3504 style
== SCE_RB_POD
);
3507 return (style
== SCE_SH_STRING
);
3510 return (style
== SCE_SQL_STRING
);
3513 return (style
== SCE_TCL_IN_QUOTE
);
3516 return (style
== SCE_LUA_LITERALSTRING
||
3517 style
== SCE_LUA_CHARACTER
||
3518 style
== SCE_LUA_STRINGEOL
||
3519 style
== SCE_LUA_STRING
);
3522 return (style
== SCE_HA_CHARACTER
||
3523 style
== SCE_HA_STRING
);
3525 case SCLEX_FREEBASIC
:
3526 return (style
== SCE_B_STRING
||
3527 style
== SCE_B_STRINGEOL
);
3530 return (style
== SCE_MATLAB_STRING
||
3531 style
== SCE_MATLAB_DOUBLEQUOTESTRING
);
3535 style
== SCE_HBA_STRING
||
3536 style
== SCE_HBA_STRINGEOL
||
3537 style
== SCE_HB_STRING
||
3538 style
== SCE_HB_STRINGEOL
||
3539 style
== SCE_H_CDATA
||
3540 style
== SCE_H_DOUBLESTRING
||
3541 style
== SCE_HJA_DOUBLESTRING
||
3542 style
== SCE_HJA_SINGLESTRING
||
3543 style
== SCE_HJA_STRINGEOL
||
3544 style
== SCE_HJ_DOUBLESTRING
||
3545 style
== SCE_HJ_SINGLESTRING
||
3546 style
== SCE_HJ_STRINGEOL
||
3547 style
== SCE_HPA_CHARACTER
||
3548 style
== SCE_HPA_STRING
||
3549 style
== SCE_HPA_TRIPLE
||
3550 style
== SCE_HPA_TRIPLEDOUBLE
||
3551 style
== SCE_HP_CHARACTER
||
3552 style
== SCE_HPHP_HSTRING
|| /* HSTRING is a heredoc */
3553 style
== SCE_HPHP_HSTRING_VARIABLE
||
3554 style
== SCE_HPHP_SIMPLESTRING
||
3555 style
== SCE_HP_STRING
||
3556 style
== SCE_HP_TRIPLE
||
3557 style
== SCE_HP_TRIPLEDOUBLE
||
3558 style
== SCE_H_SGML_DOUBLESTRING
||
3559 style
== SCE_H_SGML_SIMPLESTRING
||
3560 style
== SCE_H_SINGLESTRING
);
3563 return (style
== SCE_CMAKE_STRINGDQ
||
3564 style
== SCE_CMAKE_STRINGLQ
||
3565 style
== SCE_CMAKE_STRINGRQ
||
3566 style
== SCE_CMAKE_STRINGVAR
);
3569 return (style
== SCE_NSIS_STRINGDQ
||
3570 style
== SCE_NSIS_STRINGLQ
||
3571 style
== SCE_NSIS_STRINGRQ
||
3572 style
== SCE_NSIS_STRINGVAR
);
3575 return (style
== SCE_ADA_CHARACTER
||
3576 style
== SCE_ADA_STRING
||
3577 style
== SCE_ADA_CHARACTEREOL
||
3578 style
== SCE_ADA_STRINGEOL
);
3584 /* Checks whether the given style is a comment for the given lexer.
3585 * It doesn't handle LEX_HTML, this should be done by the caller.
3586 * Returns true if the style is a comment, FALSE otherwise.
3588 static gboolean
is_comment_style(gint lexer
, gint style
)
3593 return (style
== SCE_C_COMMENT
||
3594 style
== SCE_C_COMMENTLINE
||
3595 style
== SCE_C_COMMENTDOC
||
3596 style
== SCE_C_COMMENTLINEDOC
||
3597 style
== SCE_C_COMMENTDOCKEYWORD
||
3598 style
== SCE_C_COMMENTDOCKEYWORDERROR
);
3601 return (style
== SCE_PAS_COMMENT
||
3602 style
== SCE_PAS_COMMENT2
||
3603 style
== SCE_PAS_COMMENTLINE
);
3606 return (style
== SCE_D_COMMENT
||
3607 style
== SCE_D_COMMENTLINE
||
3608 style
== SCE_D_COMMENTDOC
||
3609 style
== SCE_D_COMMENTNESTED
||
3610 style
== SCE_D_COMMENTLINEDOC
||
3611 style
== SCE_D_COMMENTDOCKEYWORD
||
3612 style
== SCE_D_COMMENTDOCKEYWORDERROR
);
3615 return (style
== SCE_P_COMMENTLINE
||
3616 style
== SCE_P_COMMENTBLOCK
);
3620 return (style
== SCE_F_COMMENT
);
3623 return (style
== SCE_PL_COMMENTLINE
);
3625 case SCLEX_PROPERTIES
:
3626 return (style
== SCE_PROPS_COMMENT
);
3629 return (style
== SCE_PO_COMMENT
);
3632 return (style
== SCE_L_COMMENT
);
3634 case SCLEX_MAKEFILE
:
3635 return (style
== SCE_MAKE_COMMENT
);
3638 return (style
== SCE_RB_COMMENTLINE
);
3641 return (style
== SCE_SH_COMMENTLINE
);
3644 return (style
== SCE_R_COMMENT
);
3647 return (style
== SCE_SQL_COMMENT
||
3648 style
== SCE_SQL_COMMENTLINE
||
3649 style
== SCE_SQL_COMMENTDOC
);
3652 return (style
== SCE_TCL_COMMENT
||
3653 style
== SCE_TCL_COMMENTLINE
||
3654 style
== SCE_TCL_COMMENT_BOX
||
3655 style
== SCE_TCL_BLOCK_COMMENT
);
3658 return (style
== SCE_MATLAB_COMMENT
);
3661 return (style
== SCE_LUA_COMMENT
||
3662 style
== SCE_LUA_COMMENTLINE
||
3663 style
== SCE_LUA_COMMENTDOC
);
3666 return (style
== SCE_HA_COMMENTLINE
||
3667 style
== SCE_HA_COMMENTBLOCK
||
3668 style
== SCE_HA_COMMENTBLOCK2
||
3669 style
== SCE_HA_COMMENTBLOCK3
);
3671 case SCLEX_FREEBASIC
:
3672 return (style
== SCE_B_COMMENT
);
3675 return (style
== SCE_YAML_COMMENT
);
3679 style
== SCE_HBA_COMMENTLINE
||
3680 style
== SCE_HB_COMMENTLINE
||
3681 style
== SCE_H_COMMENT
||
3682 style
== SCE_HJA_COMMENT
||
3683 style
== SCE_HJA_COMMENTDOC
||
3684 style
== SCE_HJA_COMMENTLINE
||
3685 style
== SCE_HJ_COMMENT
||
3686 style
== SCE_HJ_COMMENTDOC
||
3687 style
== SCE_HJ_COMMENTLINE
||
3688 style
== SCE_HPA_COMMENTLINE
||
3689 style
== SCE_HP_COMMENTLINE
||
3690 style
== SCE_HPHP_COMMENT
||
3691 style
== SCE_HPHP_COMMENTLINE
||
3692 style
== SCE_H_SGML_COMMENT
);
3695 return (style
== SCE_CMAKE_COMMENT
);
3698 return (style
== SCE_NSIS_COMMENT
||
3699 style
== SCE_NSIS_COMMENTBOX
);
3702 return (style
== SCE_ADA_COMMENTLINE
||
3703 style
== SCE_NSIS_COMMENTBOX
);
3709 /* Checks whether the given style is normal code (not string, comment, preprocessor, etc).
3710 * It doesn't handle LEX_HTML, this should be done by the caller.
3712 static gboolean
is_code_style(gint lexer
, gint style
)
3717 if (style
== SCE_C_PREPROCESSOR
)
3721 return !(is_comment_style(lexer
, style
) ||
3722 is_string_style(lexer
, style
));
3727 static gboolean
editor_lexer_is_c_like(gint lexer
)
3742 /* Returns: -1 if lexer doesn't support type keywords */
3743 gint
editor_lexer_get_type_keyword_idx(gint lexer
)
3757 /* inserts a three-line comment at one line above current cursor position */
3758 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3764 gboolean have_multiline_comment
= FALSE
;
3767 g_return_if_fail(editor
!= NULL
&&
3768 editor
->document
->file_type
!= NULL
&& editor
->document
->file_type
->comment_open
!= NULL
);
3770 sci_start_undo_action(editor
->sci
);
3772 doc
= editor
->document
;
3774 if (doc
->file_type
->comment_close
!= NULL
&& strlen(doc
->file_type
->comment_close
) > 0)
3775 have_multiline_comment
= TRUE
;
3777 /* insert three lines one line above of the current position */
3778 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3779 pos
= sci_get_position_from_line(editor
->sci
, line
);
3781 /* use the indent on the current line but only when comment indentation is used
3782 * and we don't have multi line comment characters */
3783 if (editor
->auto_indent
&&
3784 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3786 read_indent(editor
, editor_info
.click_pos
);
3787 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3788 text_len
= strlen(text
);
3792 text
= g_strdup("\n\n\n");
3795 sci_insert_text(editor
->sci
, pos
, text
);
3798 /* select the inserted lines for commenting */
3799 sci_set_selection_start(editor
->sci
, pos
);
3800 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3802 editor_do_comment(editor
, -1, TRUE
, FALSE
);
3804 /* set the current position to the start of the first inserted line */
3805 pos
+= strlen(doc
->file_type
->comment_open
);
3807 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3808 if (have_multiline_comment
)
3811 pos
+= strlen(indent
);
3813 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3814 /* reset the selection */
3815 sci_set_anchor(editor
->sci
, pos
);
3817 sci_end_undo_action(editor
->sci
);
3821 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3822 * Scroll the view to make line appear at percent_of_view.
3823 * line can be -1 to use the current position. */
3824 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3829 g_return_if_fail(editor
!= NULL
);
3831 wid
= GTK_WIDGET(editor
->sci
);
3833 if (! wid
->window
|| ! gdk_window_is_viewable(wid
->window
))
3834 return; /* prevent gdk_window_scroll warning */
3837 line
= sci_get_current_line(editor
->sci
);
3839 /* sci 'visible line' != doc line number because of folding and line wrapping */
3840 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3841 * SCI_DOCLINEFROMVISIBLE for vis1. */
3842 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3843 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3844 line
= line
- los
* percent_of_view
;
3845 SSM(editor
->sci
, SCI_SETFIRSTVISIBLELINE
, line
, 0);
3846 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3850 /* creates and inserts one tab or whitespace of the amount of the tab width */
3851 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3854 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3856 g_return_if_fail(editor
!= NULL
);
3858 switch (iprefs
.type
)
3860 case GEANY_INDENT_TYPE_TABS
:
3861 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3863 case GEANY_INDENT_TYPE_SPACES
:
3864 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3865 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3868 text
= get_whitespace(&iprefs
, iprefs
.width
);
3869 sci_add_text(editor
->sci
, text
);
3874 void editor_select_word(GeanyEditor
*editor
)
3880 g_return_if_fail(editor
!= NULL
);
3882 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3883 start
= SSM(editor
->sci
, SCI_WORDSTARTPOSITION
, pos
, TRUE
);
3884 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, TRUE
);
3886 if (start
== end
) /* caret in whitespaces sequence */
3888 /* look forward but reverse the selection direction,
3889 * so the caret end up stay as near as the original position. */
3890 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, FALSE
);
3891 start
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, end
, TRUE
);
3896 sci_set_selection(editor
->sci
, start
, end
);
3900 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3901 * when those lines have no selection (cursor at start of line). */
3902 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3904 gint start
, end
, line
;
3906 g_return_if_fail(editor
!= NULL
);
3908 start
= sci_get_selection_start(editor
->sci
);
3909 end
= sci_get_selection_end(editor
->sci
);
3911 /* check if whole lines are already selected */
3912 if (! extra_line
&& start
!= end
&&
3913 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3914 sci_get_col_from_position(editor
->sci
, end
) == 0)
3917 line
= sci_get_line_from_position(editor
->sci
, start
);
3918 start
= sci_get_position_from_line(editor
->sci
, line
);
3920 line
= sci_get_line_from_position(editor
->sci
, end
);
3921 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3923 sci_set_selection(editor
->sci
, start
, end
);
3927 /* find the start or end of a paragraph by searching all lines in direction (UP or DOWN)
3928 * starting at the given line and return the found line or return -1 if called on an empty line */
3929 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3931 gboolean found_end
= FALSE
;
3933 gchar
*line_buf
, *x
;
3934 ScintillaObject
*sci
= editor
->sci
;
3936 /* first check current line and return -1 if it is empty to skip creating of a selection */
3937 line_buf
= x
= sci_get_line(sci
, line
);
3946 if (direction
== GTK_DIR_UP
)
3955 /* sci_get_line checks for sanity of the given line, sci_get_line always return a string
3956 * containing at least '\0' so no need to check for NULL */
3957 line_buf
= x
= sci_get_line(sci
, line
);
3959 /* check whether after skipping all whitespace we are at end of line and if so, assume
3960 * this line as end of paragraph */
3967 /* called on the first line but there is no previous line so return line 0 */
3976 void editor_select_paragraph(GeanyEditor
*editor
)
3978 gint pos_start
, pos_end
, line_start
, line_found
;
3980 g_return_if_fail(editor
!= NULL
);
3982 line_start
= SSM(editor
->sci
, SCI_LINEFROMPOSITION
,
3983 SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0), 0);
3985 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3986 if (line_found
== -1)
3989 /* find_paragraph_stop returns the emtpy line(previous to the real start of the paragraph),
3990 * so use the next line for selection start */
3994 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3996 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3997 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3999 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
4003 /* simple indentation to indent the current line with the same indent as the previous one */
4004 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
4006 gint i
, sel_start
= 0, sel_end
= 0;
4008 /* get previous line and use it for read_indent to use that line
4009 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
4010 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
4012 for (i
= first_line
; i
<= last_line
; i
++)
4014 /* skip the first line or if the indentation of the previous and current line are equal */
4016 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
4017 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
4020 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
4021 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
4022 if (sel_start
< sel_end
)
4024 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
4025 sci_replace_sel(editor
->sci
, "");
4027 sci_insert_text(editor
->sci
, sel_start
, indent
);
4032 /* simple indentation to indent the current line with the same indent as the previous one */
4033 void editor_smart_line_indentation(GeanyEditor
*editor
, gint pos
)
4035 gint first_line
, last_line
;
4036 gint first_sel_start
, first_sel_end
;
4037 ScintillaObject
*sci
;
4039 g_return_if_fail(editor
!= NULL
);
4043 first_sel_start
= sci_get_selection_start(sci
);
4044 first_sel_end
= sci_get_selection_end(sci
);
4046 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
4047 /* Find the last line with chars selected (not EOL char) */
4048 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
4049 last_line
= MAX(first_line
, last_line
);
4052 pos
= first_sel_start
;
4054 sci_start_undo_action(sci
);
4056 smart_line_indentation(editor
, first_line
, last_line
);
4058 /* set cursor position if there was no selection */
4059 if (first_sel_start
== first_sel_end
)
4061 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
4063 /* use indent position as user may wish to change indentation afterwards */
4064 sci_set_current_position(sci
, indent_pos
, FALSE
);
4068 /* fully select all the lines affected */
4069 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
4070 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
4073 sci_end_undo_action(sci
);
4077 /* increase / decrease current line or selection by one space */
4078 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
4080 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
4081 gint sel_start
, sel_end
, first_line_offset
= 0;
4083 g_return_if_fail(editor
!= NULL
);
4085 sel_start
= sci_get_selection_start(editor
->sci
);
4086 sel_end
= sci_get_selection_end(editor
->sci
);
4088 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
4089 /* Find the last line with chars selected (not EOL char) */
4090 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
4091 last_line
= MAX(first_line
, last_line
);
4096 sci_start_undo_action(editor
->sci
);
4098 for (i
= first_line
; i
<= last_line
; i
++)
4100 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
4103 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
4104 /* searching backwards for a space to remove */
4105 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
4108 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
4110 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
4111 sci_replace_sel(editor
->sci
, "");
4113 if (i
== first_line
)
4114 first_line_offset
= -1;
4119 sci_insert_text(editor
->sci
, indentation_end
, " ");
4121 if (i
== first_line
)
4122 first_line_offset
= 1;
4126 /* set cursor position */
4127 if (sel_start
< sel_end
)
4129 gint start
= sel_start
+ first_line_offset
;
4130 if (first_line_offset
< 0)
4131 start
= MAX(sel_start
+ first_line_offset
,
4132 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
4134 sci_set_selection_start(editor
->sci
, start
);
4135 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
4138 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
4140 sci_end_undo_action(editor
->sci
);
4144 void editor_finalize()
4146 scintilla_release_resources();
4150 /* wordchars: NULL or a string containing characters to match a word.
4151 * Returns: the current selection or the current word. */
4152 gchar
*editor_get_default_selection(GeanyEditor
*editor
, gboolean use_current_word
,
4153 const gchar
*wordchars
)
4157 g_return_val_if_fail(editor
!= NULL
, NULL
);
4159 if (sci_get_lines_selected(editor
->sci
) == 1)
4161 gint len
= sci_get_selected_text_length(editor
->sci
);
4163 s
= g_malloc(len
+ 1);
4164 sci_get_selected_text(editor
->sci
, s
);
4166 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
4167 { /* use the word at current cursor position */
4168 gchar word
[GEANY_MAX_WORD_LENGTH
];
4170 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
4171 if (word
[0] != '\0')
4178 /* Note: Usually the line should be made visible (not folded) before calling this.
4179 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4180 * outside the *vertical* view.
4181 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4182 * sci_scroll_caret() when this returns TRUE. */
4183 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4187 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4189 /* If line is wrapped the result may occur on another virtual line than the first and may be
4190 * still hidden, so increase the line number to check for the next document line */
4191 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4194 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4195 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4196 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4198 return (line
>= vis1
&& line
< vis1
+ los
);
4202 /* If the current line is outside the current view window, scroll the line
4203 * so it appears at percent_of_view. */
4204 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4208 g_return_if_fail(editor
!= NULL
);
4210 line
= sci_get_current_line(editor
->sci
);
4212 /* unfold maybe folded results */
4213 sci_ensure_line_is_visible(editor
->sci
, line
);
4215 /* scroll the line if it's off screen */
4216 if (! editor_line_in_view(editor
, line
))
4217 editor
->scroll_percent
= percent_of_view
;
4219 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4224 * Deletes all currently set indicators in the @a editor window.
4225 * Error indicators (red squiggly underlines) and usual line markers are removed.
4227 * @param editor The editor to operate on.
4229 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4231 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4232 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4237 * Deletes all currently set indicators matching @a indic in the @a editor window.
4239 * @param editor The editor to operate on.
4240 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4244 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4248 g_return_if_fail(editor
!= NULL
);
4250 last_pos
= sci_get_length(editor
->sci
);
4253 sci_indicator_set(editor
->sci
, indic
);
4254 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4260 * Sets an indicator @a indic on @a line.
4261 * Whitespace at the start and the end of the line is not marked.
4263 * @param editor The editor to operate on.
4264 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4265 * @param line The line number which should be marked.
4269 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4275 g_return_if_fail(editor
!= NULL
);
4276 g_return_if_fail(line
>= 0);
4278 start
= sci_get_position_from_line(editor
->sci
, line
);
4279 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4281 /* skip blank lines */
4282 if ((start
+ 1) == end
||
4284 sci_get_line_length(editor
->sci
, line
) == editor_get_eol_char_len(editor
))
4290 linebuf
= sci_get_line(editor
->sci
, line
);
4292 /* don't set the indicator on whitespace */
4293 while (isspace(linebuf
[i
]))
4295 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4302 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4307 * Sets an indicator on the range specified by @a start and @a end.
4308 * No error checking or whitespace removal is performed, this should be done by the calling
4309 * function if necessary.
4311 * @param editor The editor to operate on.
4312 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4313 * @param start The starting position for the marker.
4314 * @param end The ending position for the marker.
4318 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4320 g_return_if_fail(editor
!= NULL
);
4324 sci_indicator_set(editor
->sci
, indic
);
4325 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4329 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4330 * the replacement will also start with 0x... */
4331 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4333 g_return_if_fail(editor
!= NULL
);
4335 if (sci_has_selection(editor
->sci
))
4337 gint start
= sci_get_selection_start(editor
->sci
);
4338 const gchar
*replacement
= colour
;
4340 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4341 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4343 sci_set_selection_start(editor
->sci
, start
+ 2);
4344 sci_set_selection_end(editor
->sci
, start
+ 8);
4345 replacement
++; /* skip the leading "0x" */
4347 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4348 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4349 replacement
++; /* so skip the '#' to only replace the colour value */
4351 sci_replace_sel(editor
->sci
, replacement
);
4354 sci_add_text(editor
->sci
, colour
);
4359 * Retrieves the end of line characters mode (LF, CR/LF, CR) in the given editor.
4360 * If @a editor is @c NULL, the default end of line characters are used.
4362 * @param editor The editor to operate on, or @c NULL to query the default value.
4363 * @return The used end of line characters mode.
4367 gint
editor_get_eol_char_mode(GeanyEditor
*editor
)
4369 gint mode
= file_prefs
.default_eol_character
;
4372 mode
= sci_get_eol_mode(editor
->sci
);
4379 * Retrieves the localized name (for displaying) of the used end of line characters
4380 * (LF, CR/LF, CR) in the given editor.
4381 * If @a editor is @c NULL, the default end of line characters are used.
4383 * @param editor The editor to operate on, or @c NULL to query the default value.
4384 * @return The name of the end of line characters.
4388 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4390 gint mode
= file_prefs
.default_eol_character
;
4393 mode
= sci_get_eol_mode(editor
->sci
);
4395 return utils_get_eol_name(mode
);
4400 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4401 * If @a editor is @c NULL, the default end of line characters are used.
4402 * The returned value is 1 for CR and LF and 2 for CR/LF.
4404 * @param editor The editor to operate on, or @c NULL to query the default value.
4405 * @return The length of the end of line characters.
4409 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4411 gint mode
= file_prefs
.default_eol_character
;
4414 mode
= sci_get_eol_mode(editor
->sci
);
4418 case SC_EOL_CRLF
: return 2; break;
4419 default: return 1; break;
4425 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4426 * If @a editor is @c NULL, the default end of line characters are used.
4427 * The returned value is either "\n", "\r\n" or "\r".
4429 * @param editor The editor to operate on, or @c NULL to query the default value.
4430 * @return The end of line characters.
4434 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4436 gint mode
= file_prefs
.default_eol_character
;
4439 mode
= sci_get_eol_mode(editor
->sci
);
4441 return utils_get_eol_char(mode
);
4445 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4447 gint lines
, first
, i
;
4449 if (editor
== NULL
|| ! editor_prefs
.folding
)
4452 lines
= sci_get_line_count(editor
->sci
);
4453 first
= sci_get_first_visible_line(editor
->sci
);
4455 for (i
= 0; i
< lines
; i
++)
4457 gint level
= sci_get_fold_level(editor
->sci
, i
);
4459 if (level
& SC_FOLDLEVELHEADERFLAG
)
4461 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4462 sci_toggle_fold(editor
->sci
, i
);
4465 editor_scroll_to_line(editor
, first
, 0.0F
);
4469 void editor_unfold_all(GeanyEditor
*editor
)
4471 fold_all(editor
, FALSE
);
4475 void editor_fold_all(GeanyEditor
*editor
)
4477 fold_all(editor
, TRUE
);
4481 void editor_replace_tabs(GeanyEditor
*editor
)
4483 gint search_pos
, pos_in_line
, current_tab_true_length
;
4486 struct Sci_TextToFind ttf
;
4488 g_return_if_fail(editor
!= NULL
);
4490 sci_start_undo_action(editor
->sci
);
4491 tab_len
= sci_get_tab_width(editor
->sci
);
4493 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4494 ttf
.lpstrText
= (gchar
*) "\t";
4498 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4499 if (search_pos
== -1)
4502 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4503 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4504 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4505 sci_set_target_start(editor
->sci
, search_pos
);
4506 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4507 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4508 /* next search starts after replacement */
4509 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4510 /* update end of range now text has changed */
4511 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4514 sci_end_undo_action(editor
->sci
);
4518 /* Replaces all occurrences all spaces of the length of a given tab_width. */
4519 void editor_replace_spaces(GeanyEditor
*editor
)
4522 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4524 struct Sci_TextToFind ttf
;
4526 g_return_if_fail(editor
!= NULL
);
4528 if (tab_len_f
< 0.0)
4529 tab_len_f
= sci_get_tab_width(editor
->sci
);
4531 if (! dialogs_show_input_numeric(
4532 _("Enter Tab Width"),
4533 _("Enter the amount of spaces which should be replaced by a tab character."),
4534 &tab_len_f
, 1, 100, 1))
4538 tab_len
= (gint
) tab_len_f
;
4540 sci_start_undo_action(editor
->sci
);
4542 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4543 ttf
.lpstrText
= g_strnfill(tab_len
, ' ');
4547 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4548 if (search_pos
== -1)
4551 sci_set_target_start(editor
->sci
, search_pos
);
4552 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4553 sci_replace_target(editor
->sci
, "\t", FALSE
);
4554 ttf
.chrg
.cpMin
= search_pos
;
4555 /* update end of range now text has changed */
4556 ttf
.chrg
.cpMax
-= tab_len
- 1;
4558 sci_end_undo_action(editor
->sci
);
4559 g_free(ttf
.lpstrText
);
4563 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4565 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4566 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4567 gint i
= line_end
- 1;
4568 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4570 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4573 ch
= sci_get_char_at(editor
->sci
, i
);
4575 if (i
< (line_end
- 1))
4577 sci_set_target_start(editor
->sci
, i
+ 1);
4578 sci_set_target_end(editor
->sci
, line_end
);
4579 sci_replace_target(editor
->sci
, "", FALSE
);
4584 void editor_strip_trailing_spaces(GeanyEditor
*editor
)
4586 gint max_lines
= sci_get_line_count(editor
->sci
);
4589 sci_start_undo_action(editor
->sci
);
4591 for (line
= 0; line
< max_lines
; line
++)
4593 editor_strip_line_trailing_spaces(editor
, line
);
4595 sci_end_undo_action(editor
->sci
);
4599 void editor_ensure_final_newline(GeanyEditor
*editor
)
4601 gint max_lines
= sci_get_line_count(editor
->sci
);
4602 gboolean append_newline
= (max_lines
== 1);
4603 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4607 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4611 const gchar
*eol
= editor_get_eol_char(editor
);
4613 sci_insert_text(editor
->sci
, end_document
, eol
);
4618 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4622 PangoFontDescription
*pfd
;
4624 g_return_if_fail(editor
);
4626 pfd
= pango_font_description_from_string(font
);
4627 size
= pango_font_description_get_size(pfd
) / PANGO_SCALE
;
4628 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4629 pango_font_description_free(pfd
);
4631 for (style
= 0; style
<= 127; style
++)
4632 sci_set_font(editor
->sci
, style
, font_name
, size
);
4634 /* line number and braces */
4635 sci_set_font(editor
->sci
, STYLE_LINENUMBER
, font_name
, size
);
4636 sci_set_font(editor
->sci
, STYLE_BRACELIGHT
, font_name
, size
);
4637 sci_set_font(editor
->sci
, STYLE_BRACEBAD
, font_name
, size
);
4640 /* zoom to 100% to prevent confusion */
4641 sci_zoom_off(editor
->sci
);
4645 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4647 g_return_if_fail(editor
!= NULL
);
4649 editor
->line_wrapping
= wrap
;
4650 sci_set_lines_wrapped(editor
->sci
, wrap
);
4654 /** Sets the indent type for @a editor.
4655 * @param editor Editor.
4656 * @param type Indent type.
4660 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4662 editor_set_indent(editor
, type
, editor
->indent_width
);
4666 void editor_set_indent(GeanyEditor
*editor
, GeanyIndentType type
, gint width
)
4668 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4669 ScintillaObject
*sci
= editor
->sci
;
4670 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4672 editor
->indent_type
= type
;
4673 editor
->indent_width
= width
;
4674 sci_set_use_tabs(sci
, use_tabs
);
4676 if (type
== GEANY_INDENT_TYPE_BOTH
)
4678 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4679 if (iprefs
->hard_tab_width
!= 8)
4681 static gboolean warn
= TRUE
;
4683 ui_set_statusbar(TRUE
, _("Warning: non-standard hard tab width: %d != 8!"),
4684 iprefs
->hard_tab_width
);
4689 sci_set_tab_width(sci
, width
);
4691 SSM(sci
, SCI_SETINDENT
, width
, 0);
4693 /* remove indent spaces on backspace, if using any spaces to indent */
4694 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, type
!= GEANY_INDENT_TYPE_TABS
, 0);
4698 /* Convenience function for editor_goto_pos() to pass in a line number. */
4699 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line_no
, gint offset
)
4703 g_return_val_if_fail(editor
, FALSE
);
4704 if (line_no
< 0 || line_no
>= sci_get_line_count(editor
->sci
))
4709 gint current_line
= sci_get_current_line(editor
->sci
);
4711 line_no
= current_line
+ line_no
;
4714 pos
= sci_get_position_from_line(editor
->sci
, line_no
);
4715 return editor_goto_pos(editor
, pos
, TRUE
);
4719 /** Moves to position @a pos, switching to the document if necessary,
4720 * setting a marker if @a mark is set.
4722 * @param editor Editor.
4723 * @param pos The position.
4724 * @param mark Whether to set a mark on the position.
4725 * @return @c TRUE if action has been performed, otherwise @c FALSE.
4729 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4733 g_return_val_if_fail(editor
, FALSE
);
4734 if (G_UNLIKELY(pos
< 0))
4739 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4741 /* mark the tag with the yellow arrow */
4742 sci_marker_delete_all(editor
->sci
, 0);
4743 sci_set_marker_at_line(editor
->sci
, line
, 0);
4746 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4747 editor
->scroll_percent
= 0.25F
;
4749 /* finally switch to the page */
4750 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets
.notebook
), GTK_WIDGET(editor
->sci
));
4751 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), page_num
);
4758 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4760 GeanyEditor
*editor
= user_data
;
4762 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4763 * few lines only, maybe this could/should be done in Scintilla directly */
4764 if (event
->state
& GDK_MOD1_MASK
)
4766 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4769 else if (event
->state
& GDK_SHIFT_MASK
)
4771 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4773 sci_scroll_columns(editor
->sci
, amount
);
4777 return FALSE
; /* let Scintilla handle all other cases */
4781 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4783 GeanyDocument
*doc
= editor
->document
;
4785 if (!doc
->priv
->colourise_needed
)
4788 doc
->priv
->colourise_needed
= FALSE
;
4789 sci_colourise(editor
->sci
, 0, -1);
4791 /* now that the current document is colourised, fold points are now accurate,
4792 * so force an update of the current function/tag. */
4793 symbols_get_current_function(NULL
, NULL
);
4794 ui_update_statusbar(NULL
, -1);
4800 /* We only want to colourise just before drawing, to save startup time and
4801 * prevent unnecessary recolouring other documents after one is saved.
4802 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4803 * and "show" doesn't work). */
4804 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4806 GeanyEditor
*editor
= user_data
;
4808 editor_check_colourise(editor
);
4813 static gboolean
on_editor_expose_event(GtkWidget
*widget
, GdkEventExpose
*event
,
4816 GeanyEditor
*editor
= user_data
;
4818 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4819 * for some reason, maybe it's not necessary but just in case. */
4820 editor_check_colourise(editor
);
4825 static void setup_sci_keys(ScintillaObject
*sci
)
4827 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4828 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4829 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4830 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4831 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4832 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4833 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4834 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4835 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4836 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4837 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4838 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4839 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4840 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4841 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4843 if (editor_prefs
.use_gtk_word_boundaries
)
4845 /* use GtkEntry-like word boundaries */
4846 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4847 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4848 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4850 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4851 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4852 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4853 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4854 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4855 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4857 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4861 #include "icons/16x16/classviewer-var.xpm"
4862 #include "icons/16x16/classviewer-method.xpm"
4864 /* Create new editor widget (scintilla).
4865 * @note The @c "sci-notify" signal is connected separately. */
4866 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4868 ScintillaObject
*sci
;
4870 sci
= SCINTILLA(scintilla_new());
4872 gtk_widget_show(GTK_WIDGET(sci
));
4874 sci_set_codepage(sci
, SC_CP_UTF8
);
4875 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4876 /* disable scintilla provided popup menu */
4877 sci_use_popup(sci
, FALSE
);
4879 setup_sci_keys(sci
);
4881 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4882 sci_set_lines_wrapped(sci
, editor_prefs
.line_wrapping
);
4883 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
4884 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4885 /*sci_set_caret_policy_y(sci, CARET_JUMPS | CARET_EVEN, 0);*/
4886 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4887 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4889 /* tag autocompletion images */
4890 SSM(sci
, SCI_REGISTERIMAGE
, 1, (sptr_t
)classviewer_var
);
4891 SSM(sci
, SCI_REGISTERIMAGE
, 2, (sptr_t
)classviewer_method
);
4893 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4894 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4897 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4899 /* only connect signals if this is for the document notebook, not split window */
4900 if (editor
->sci
== NULL
)
4902 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4903 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4904 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4905 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4906 g_signal_connect(sci
, "expose-event", G_CALLBACK(on_editor_expose_event
), editor
);
4912 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4913 * @param editor Editor settings.
4914 * @return The new widget.
4918 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4920 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4921 ScintillaObject
*old
, *sci
;
4923 /* temporarily change editor to use the new sci widget */
4925 sci
= create_new_sci(editor
);
4928 editor_set_indent(editor
, iprefs
->type
, iprefs
->width
);
4929 editor_set_font(editor
, interface_prefs
.editor_font
);
4930 editor_apply_update_prefs(editor
);
4932 /* if editor already had a widget, restore it */
4939 GeanyEditor
*editor_create(GeanyDocument
*doc
)
4941 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4942 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
4944 editor
->document
= doc
;
4945 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
4947 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
4948 editor
->line_wrapping
= editor_prefs
.line_wrapping
;
4949 editor
->scroll_percent
= -1.0F
;
4950 editor
->line_breaking
= FALSE
;
4952 editor
->sci
= editor_create_widget(editor
);
4957 /* in case we need to free some fields in future */
4958 void editor_destroy(GeanyEditor
*editor
)
4964 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
4966 g_return_if_fail(NZV(doc
->real_path
));
4968 if (utils_str_equal(doc
->real_path
,
4969 utils_build_path(app
->configdir
, "snippets.conf", NULL
)))
4971 /* reload snippets */
4972 editor_snippets_free();
4973 editor_snippets_init();
4978 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
4982 g_return_val_if_fail(editor
, FALSE
);
4984 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
4987 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
4989 /* if no word part, complete normally */
4990 if (!check_partial_completion(editor
, entry
))
4991 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
4998 void editor_init(void)
5000 static GeanyIndentPrefs indent_prefs
;
5002 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
5003 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
5004 editor_prefs
.indentation
= &indent_prefs
;
5006 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
5007 * handler (on_editor_notify) is called */
5008 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
5010 ui_add_config_file_menu_item(utils_build_path(app
->configdir
, "snippets.conf", NULL
),
5012 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
5016 /* TODO: Should these be user-defined instead of hard-coded? */
5017 void editor_set_indentation_guides(GeanyEditor
*editor
)
5022 g_return_if_fail(editor
!= NULL
);
5024 if (! editor_prefs
.show_indent_guide
)
5026 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
5030 lexer
= sci_get_lexer(editor
->sci
);
5033 /* Lines added/removed are prefixed with +/- characters, so
5034 * those lines will not be shown with any indentation guides.
5035 * It can be distracting that only a few of lines in a diff/patch
5036 * file will show the guides. */
5041 /* These languages use indentation for control blocks; the "look forward" method works
5045 case SCLEX_MAKEFILE
:
5048 case SCLEX_PROPERTIES
:
5049 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
5051 mode
= SC_IV_LOOKFORWARD
;
5054 /* C-like (structured) languages benefit from the "look both" method */
5068 case SCLEX_FREEBASIC
:
5071 mode
= SC_IV_LOOKBOTH
;
5079 sci_set_indentation_guides(editor
->sci
, mode
);
5083 /* Apply just the prefs that can change in the Preferences dialog */
5084 void editor_apply_update_prefs(GeanyEditor
*editor
)
5086 ScintillaObject
*sci
;
5088 g_return_if_fail(editor
!= NULL
);
5090 if (main_status
.quitting
)
5095 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
5096 editor_get_long_line_column(), editor_prefs
.long_line_color
);
5098 /* update indent width, tab width */
5099 editor_set_indent(editor
, editor
->indent_type
, editor
->indent_width
);
5100 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
5102 sci_assign_cmdkey(sci
, SCK_HOME
| (SCMOD_SHIFT
<< 16),
5103 editor_prefs
.smart_home_key
? SCI_VCHOMEEXTEND
: SCI_HOMEEXTEND
);
5104 sci_assign_cmdkey(sci
, SCK_HOME
| ((SCMOD_SHIFT
| SCMOD_ALT
) << 16),
5105 editor_prefs
.smart_home_key
? SCI_VCHOMERECTEXTEND
: SCI_HOMERECTEXTEND
);
5107 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
5108 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
5110 editor_set_indentation_guides(editor
);
5112 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
5113 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
5114 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
5115 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
5117 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
5120 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
5122 /* (dis)allow scrolling past end of document */
5123 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
5127 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
5128 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
5130 ScintillaObject
*sci
= editor
->sci
;
5131 gint pos
= sci_get_position_from_line(sci
, line
);
5135 sci_insert_text(sci
, pos
, "\t");
5139 if (sci_get_char_at(sci
, pos
) == '\t')
5141 sci_set_selection(sci
, pos
, pos
+ 1);
5142 sci_replace_sel(sci
, "");
5144 else /* remove spaces only if no tabs */
5146 gint width
= sci_get_line_indentation(sci
, line
);
5148 width
-= editor_get_indent_prefs(editor
)->width
;
5149 sci_set_line_indentation(sci
, line
, width
);
5155 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
5157 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
5158 ScintillaObject
*sci
= editor
->sci
;
5160 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
5161 change_tab_indentation(editor
, line
, increase
);
5164 gint width
= sci_get_line_indentation(sci
, line
);
5166 width
+= increase
? iprefs
->width
: -iprefs
->width
;
5167 sci_set_line_indentation(sci
, line
, width
);
5172 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
5174 ScintillaObject
*sci
= editor
->sci
;
5176 gint line
, lstart
, lend
;
5178 if (sci_get_lines_selected(sci
) <= 1)
5180 line
= sci_get_current_line(sci
);
5181 editor_change_line_indent(editor
, line
, increase
);
5184 editor_select_lines(editor
, FALSE
);
5185 start
= sci_get_selection_start(sci
);
5186 end
= sci_get_selection_end(sci
);
5187 lstart
= sci_get_line_from_position(sci
, start
);
5188 lend
= sci_get_line_from_position(sci
, end
);
5189 if (end
== sci_get_length(sci
))
5190 lend
++; /* for last line with text on it */
5192 sci_start_undo_action(sci
);
5193 for (line
= lstart
; line
< lend
; line
++)
5195 editor_change_line_indent(editor
, line
, increase
);
5197 sci_end_undo_action(sci
);
5199 /* set cursor/selection */
5202 sci_set_selection_start(sci
, start
);
5203 end
= sci_get_position_from_line(sci
, lend
);
5204 sci_set_selection_end(sci
, end
);
5205 editor_select_lines(editor
, FALSE
);
5209 sci_set_current_line(sci
, lstart
);