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 "sciwrappers.h"
63 #include "callbacks.h"
64 #include "templates.h"
65 #include "keybindings.h"
67 #include "projectprivate.h"
71 /* Note: use sciwrappers.h instead where possible.
72 * Do not use SSM in files unrelated to scintilla. */
73 #define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
76 static GHashTable
*snippet_hash
= NULL
;
77 static GQueue
*snippet_offsets
= NULL
;
78 static gint snippet_cursor_insert_pos
;
80 /* holds word under the mouse or keyboard cursor */
81 static gchar current_word
[GEANY_MAX_WORD_LENGTH
];
83 /* Initialised in keyfile.c. */
84 GeanyEditorPrefs editor_prefs
;
86 EditorInfo editor_info
= {current_word
, -1};
96 } calltip
= {NULL
, FALSE
, NULL
, 0, 0, NULL
};
98 static gchar indent
[100];
101 static void on_new_line_added(GeanyEditor
*editor
);
102 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
103 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
104 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
105 static gboolean
is_code_style(gint lexer
, gint style
);
106 static gboolean
is_string_style(gint lexer
, gint style
);
107 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
108 static void auto_table(GeanyEditor
*editor
, gint pos
);
109 static void close_block(GeanyEditor
*editor
, gint pos
);
110 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
111 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
112 const gchar
*wc
, gboolean stem
);
113 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
);
116 void editor_snippets_free(void)
118 g_hash_table_destroy(snippet_hash
);
119 g_queue_free(snippet_offsets
);
123 void editor_snippets_init(void)
125 gsize i
, j
, len
= 0, len_keys
= 0;
126 gchar
*sysconfigfile
, *userconfigfile
;
127 gchar
**groups_user
, **groups_sys
;
128 gchar
**keys_user
, **keys_sys
;
130 GKeyFile
*sysconfig
= g_key_file_new();
131 GKeyFile
*userconfig
= g_key_file_new();
134 snippet_offsets
= g_queue_new();
136 sysconfigfile
= g_strconcat(app
->datadir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
137 userconfigfile
= g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
139 /* check for old autocomplete.conf files (backwards compatibility) */
140 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
| G_FILE_TEST_IS_SYMLINK
))
141 setptr(userconfigfile
,
142 g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "autocomplete.conf", NULL
));
144 /* load the actual config files */
145 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
146 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
148 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
150 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
152 /* first read all globally defined auto completions */
153 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
154 for (i
= 0; i
< len
; i
++)
156 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
157 /* create new hash table for the read section (=> filetype) */
158 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
159 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
161 for (j
= 0; j
< len_keys
; j
++)
163 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
164 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
166 g_strfreev(keys_sys
);
169 /* now read defined completions in user's configuration directory and add / replace them */
170 groups_user
= g_key_file_get_groups(userconfig
, &len
);
171 for (i
= 0; i
< len
; i
++)
173 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
175 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
177 { /* new key found, create hash table */
178 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
179 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
181 for (j
= 0; j
< len_keys
; j
++)
183 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
185 { /* value = NULL means the key doesn't yet exist, so insert */
186 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
187 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
190 { /* old key and value will be freed by destroy function (g_free) */
191 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
192 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
195 g_strfreev(keys_user
);
198 g_free(sysconfigfile
);
199 g_free(userconfigfile
);
200 g_strfreev(groups_sys
);
201 g_strfreev(groups_user
);
202 g_key_file_free(sysconfig
);
203 g_key_file_free(userconfig
);
207 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
210 GeanyEditor
*editor
= data
;
211 GeanyDocument
*doc
= editor
->document
;
213 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
214 * fake event to show the editor menu triggered by a key event where we want to use the
215 * text cursor position. */
216 if (event
->x
> 0.0 && event
->y
> 0.0)
217 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
218 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
220 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
222 if (event
->button
== 1)
224 guint state
= event
->state
& gtk_accelerator_get_default_mod_mask();
226 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
228 gint ss
= sci_get_selection_start(editor
->sci
);
229 sci_set_selection_end(editor
->sci
, ss
);
231 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GDK_CONTROL_MASK
)
233 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
235 editor_find_current_word(editor
, editor_info
.click_pos
,
236 current_word
, sizeof current_word
, NULL
);
238 return symbols_goto_tag(current_word
, TRUE
);
240 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
243 return document_check_disk_status(doc
, FALSE
);
246 /* calls the edit popup menu in the editor */
247 if (event
->button
== 3)
251 editor_find_current_word(editor
, editor_info
.click_pos
,
252 current_word
, sizeof current_word
, NULL
);
254 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
255 ui_update_popup_goto_items(can_goto
);
256 ui_update_popup_copy_items(doc
);
257 ui_update_insert_include_item(doc
, 0);
259 g_signal_emit_by_name(geany_object
, "update-editor-menu",
260 current_word
, editor_info
.click_pos
, doc
);
262 gtk_menu_popup(GTK_MENU(main_widgets
.editor_menu
),
263 NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
271 static gboolean
is_style_php(gint style
)
273 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
274 style
== SCE_HPHP_COMPLEX_VARIABLE
)
283 gint
editor_get_long_line_type(void)
286 switch (app
->project
->long_line_behaviour
)
288 case 0: /* marker disabled */
290 case 1: /* use global settings */
292 case 2: /* custom (enabled) */
293 return editor_prefs
.long_line_global_type
;
296 if (!editor_prefs
.long_line_global_enabled
)
299 return editor_prefs
.long_line_global_type
;
303 gint
editor_get_long_line_column(void)
305 if (app
->project
&& app
->project
->long_line_behaviour
!= 1 /* use global settings */)
306 return app
->project
->long_line_column
;
308 return editor_prefs
.long_line_global_column
;
312 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
314 ScintillaObject
*sci
;
316 g_return_if_fail(editor
!= NULL
);
320 sci_toggle_fold(sci
, line
);
322 /* extra toggling of child fold points
323 * use when editor_prefs.unfold_all_children is set and Shift is NOT pressed or when
324 * editor_prefs.unfold_all_children is NOT set but Shift is pressed */
325 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
326 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
328 gint last_line
= SSM(sci
, SCI_GETLASTCHILD
, line
, -1);
331 if (sci_get_line_is_visible(sci
, line
+ 1))
332 { /* unfold all children of the current fold point */
333 for (i
= line
; i
< last_line
; i
++)
335 if (! sci_get_line_is_visible(sci
, i
))
337 sci_toggle_fold(sci
, sci_get_fold_parent(sci
, i
));
342 { /* fold all children of the current fold point */
343 for (i
= line
; i
< last_line
; i
++)
345 gint level
= sci_get_fold_level(sci
, i
);
346 if (level
& SC_FOLDLEVELHEADERFLAG
)
348 if (sci_get_fold_expanded(sci
, i
))
349 sci_toggle_fold(sci
, i
);
357 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
359 /* left click to marker margin marks the line */
362 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
364 /*sci_marker_delete_all(editor->sci, 1);*/
365 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
367 /* left click on the folding margin to toggle folding state of current line */
368 else if (nt
->margin
== 2 && editor_prefs
.folding
)
370 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
371 editor_toggle_fold(editor
, line
, nt
->modifiers
);
376 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
378 ScintillaObject
*sci
= editor
->sci
;
379 gint pos
= sci_get_current_position(sci
);
381 /* undo / redo menu update */
382 ui_update_popup_reundo_items(editor
->document
);
384 /* brace highlighting */
385 editor_highlight_braces(editor
, pos
);
387 ui_update_statusbar(editor
->document
, pos
);
390 /** experimental code for inverting selections */
393 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
395 /* need to get colour from getstyleat(), but how? */
396 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
397 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
400 sci_get_style_at(sci
, pos
);
406 static void check_line_breaking(GeanyEditor
*editor
, gint pos
, gchar c
)
408 ScintillaObject
*sci
= editor
->sci
;
409 gint line
, lstart
, col
;
411 if (!editor
->line_breaking
)
414 col
= sci_get_col_from_position(sci
, pos
);
417 pos
--; /* Look for previous space, not the new one */
419 line
= sci_get_current_line(sci
);
421 lstart
= sci_get_position_from_line(sci
, line
);
423 /* use column instead of position which might be different with multibyte characters */
424 if (col
< editor_prefs
.line_break_column
)
427 /* look for the last space before line_break_column */
428 pos
= MIN(pos
, lstart
+ editor_prefs
.line_break_column
);
432 c
= sci_get_char_at(sci
, --pos
);
435 gint diff
, last_pos
, last_col
;
436 const gchar
*eol
= editor_get_eol_char(editor
);
438 /* remember the distance between the current column and the last column on the line
439 * (we use column position in case the previous line gets altered, such as removing
440 * trailing spaces or in case it contains multibyte characters) */
441 last_pos
= sci_get_line_end_position(sci
, line
);
442 last_col
= sci_get_col_from_position(sci
, last_pos
);
443 diff
= last_col
- col
;
445 /* break the line after the space */
446 sci_insert_text(sci
, pos
+ 1, eol
);
449 /* set position as if user had pressed return */
450 pos
= sci_get_position_from_line(sci
, line
);
451 sci_set_current_position(sci
, pos
, FALSE
);
452 /* add indentation, comment multilines, etc */
453 on_new_line_added(editor
);
455 /* correct cursor position (might not be at line end) */
456 last_pos
= sci_get_line_end_position(sci
, line
);
457 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
458 /* last column - distance is the desired column, then retrieve its document position */
459 pos
= SSM(sci
, SCI_FINDCOLUMN
, line
, last_col
- diff
);
460 sci_set_current_position(sci
, pos
, FALSE
);
468 static void show_autocomplete(ScintillaObject
*sci
, gint rootlen
, const gchar
*words
)
470 /* store whether a calltip is showing, so we can reshow it after autocompletion */
471 calltip
.set
= SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
472 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
);
476 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
478 ScintillaObject
*sci
= editor
->sci
;
480 g_return_if_fail(tags
);
484 GString
*words
= g_string_sized_new(150);
487 for (j
= 0; j
< tags
->len
; ++j
)
489 TMTag
*tag
= tags
->pdata
[j
];
492 g_string_append_c(words
, '\n');
494 if (j
== editor_prefs
.autocompletion_max_entries
)
496 g_string_append(words
, "...");
499 g_string_append(words
, tag
->name
);
501 /* for now, tag types don't all follow C, so just look at arglist */
502 if (NZV(tag
->atts
.entry
.arglist
))
503 g_string_append(words
, "?2");
505 g_string_append(words
, "?1");
507 show_autocomplete(sci
, rootlen
, words
->str
);
508 g_string_free(words
, TRUE
);
513 /* do not use with long strings */
514 static gboolean
match_last_chars(ScintillaObject
*sci
, gint pos
, const gchar
*str
)
516 gsize len
= strlen(str
);
519 g_return_val_if_fail(len
< 100, FALSE
);
521 buf
= g_alloca(len
+ 1);
522 sci_get_text_range(sci
, pos
- len
, pos
, buf
);
523 return strcmp(str
, buf
) == 0;
527 static gboolean
reshow_calltip(gpointer data
)
531 g_return_val_if_fail(calltip
.sci
!= NULL
, FALSE
);
533 SSM(calltip
.sci
, SCI_CALLTIPCANCEL
, 0, 0);
534 doc
= document_get_current();
536 if (doc
&& doc
->editor
->sci
== calltip
.sci
)
538 /* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
539 * may be completely wrong in case the user cancelled the auto completion with the mouse */
540 SSM(calltip
.sci
, SCI_CALLTIPSHOW
, calltip
.pos
, (sptr_t
) calltip
.text
);
546 static void request_reshowing_calltip(SCNotification
*nt
)
550 /* delay the reshow of the calltip window to make sure it is actually displayed,
551 * without it might be not visible on SCN_AUTOCCANCEL */
552 g_idle_add(reshow_calltip
, NULL
);
557 static void autocomplete_scope(GeanyEditor
*editor
)
559 ScintillaObject
*sci
= editor
->sci
;
560 gint pos
= sci_get_current_position(editor
->sci
);
561 gchar typed
= sci_get_char_at(sci
, pos
- 1);
563 const GPtrArray
*tags
= NULL
;
565 GeanyFiletype
*ft
= editor
->document
->file_type
;
567 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
569 if (match_last_chars(sci
, pos
, "->") || match_last_chars(sci
, pos
, "::"))
571 else if (typed
!= '.')
574 else if (typed
!= '.')
577 /* allow for a space between word and operator */
578 if (isspace(sci_get_char_at(sci
, pos
- 2)))
580 name
= editor_get_word_at_pos(editor
, pos
- 1, NULL
);
584 tags
= tm_workspace_find(name
, tm_tag_max_t
, NULL
, FALSE
, ft
->lang
);
586 if (!tags
|| tags
->len
== 0)
589 tag
= g_ptr_array_index(tags
, 0);
590 name
= tag
->atts
.entry
.var_type
;
593 TMWorkObject
*obj
= editor
->document
->tm_file
;
595 tags
= tm_workspace_find_scope_members(obj
? obj
->tags_array
: NULL
,
598 show_tags_list(editor
, tags
, 0);
603 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
605 ScintillaObject
*sci
= editor
->sci
;
606 gint pos
= sci_get_current_position(sci
);
611 { /* simple indentation (only for CR format) */
612 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
613 on_new_line_added(editor
);
617 { /* simple indentation (for CR/LF and LF format) */
618 on_new_line_added(editor
);
622 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
625 { /* close xml-tags */
626 handle_xml(editor
, pos
, nt
->ch
);
631 auto_close_chars(sci
, pos
, nt
->ch
);
633 editor_show_calltip(editor
, --pos
);
637 { /* hide calltips */
638 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0))
640 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
642 g_free(calltip
.text
);
654 auto_close_chars(sci
, pos
, nt
->ch
);
658 { /* closing bracket handling */
659 if (editor
->auto_indent
)
660 close_block(editor
, pos
- 1);
663 /* scope autocompletion */
665 case ':': /* C/C++ class:: syntax */
666 /* tag autocompletion */
669 if (! editor_start_auto_complete(editor
, pos
, FALSE
))
670 request_reshowing_calltip(nt
);
672 editor_start_auto_complete(editor
, pos
, FALSE
);
675 check_line_breaking(editor
, pos
, nt
->ch
);
679 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
680 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
681 gboolean force
, gint visLevels
, gint level
)
683 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
684 gint levelLine
= level
;
686 while (*line
<= lineMaxSubord
)
688 if (G_UNLIKELY(force
))
691 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
693 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
698 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
701 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
702 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
704 if (G_UNLIKELY(force
))
707 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
709 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
710 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
716 if (!sci_get_fold_expanded(sci
, *line
))
717 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
718 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
722 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
734 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
736 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
738 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
740 /* Adding a fold point */
741 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
742 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
745 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
747 if (! sci_get_fold_expanded(sci
, line
))
748 { /* Removing the fold from one that has been contracted so should expand
749 * otherwise lines are left invisible with no way to make them visible */
750 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
751 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
754 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
755 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
757 /* See if should still be hidden */
758 gint parentLine
= sci_get_fold_parent(sci
, line
);
761 SSM(sci
, SCI_SHOWLINES
, line
, line
);
763 else if (sci_get_fold_expanded(sci
, parentLine
) &&
764 sci_get_line_is_visible(sci
, parentLine
))
766 SSM(sci
, SCI_SHOWLINES
, line
, line
);
772 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
773 gboolean enforcePolicy
)
775 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
776 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
779 for (line
= lineStart
; line
<= lineEnd
; line
++)
781 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
786 static void auto_update_margin_width(GeanyEditor
*editor
)
788 gint next_linecount
= 1;
789 gint linecount
= sci_get_line_count(editor
->sci
);
790 GeanyDocument
*doc
= editor
->document
;
792 while (next_linecount
<= linecount
)
793 next_linecount
*= 10;
795 if (editor
->document
->priv
->line_count
!= next_linecount
)
797 doc
->priv
->line_count
= next_linecount
;
798 sci_set_line_numbers(editor
->sci
, TRUE
, 0);
803 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
805 gint pos
= sci_get_current_position(sci
);
807 sci_insert_text(sci
, pos
, text
);
808 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
812 /* Complete the next word part from @a entry */
813 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
815 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
817 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
819 if (strstr(text
, stem
) != text
)
820 return FALSE
; /* shouldn't happen */
821 if (strlen(text
) <= strlen(stem
))
824 text
+= strlen(stem
); /* skip stem */
825 ptr
= strstr(text
+ 1, "_");
829 partial_complete(editor
->sci
, text
);
835 foreach_str(ptr
, text
+ 1)
839 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
842 partial_complete(editor
->sci
, text
);
851 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
852 * Plugins can connect to the "editor-notify" signal. */
853 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
854 gpointer scnt
, gpointer data
)
856 GeanyEditor
*editor
= data
;
859 g_return_if_fail(editor
!= NULL
);
861 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
865 static gboolean
on_editor_notify(G_GNUC_UNUSED GObject
*object
, GeanyEditor
*editor
,
866 SCNotification
*nt
, G_GNUC_UNUSED gpointer data
)
868 ScintillaObject
*sci
= editor
->sci
;
869 GeanyDocument
*doc
= editor
->document
;
871 switch (nt
->nmhdr
.code
)
873 case SCN_SAVEPOINTLEFT
:
874 document_set_text_changed(doc
, TRUE
);
877 case SCN_SAVEPOINTREACHED
:
878 document_set_text_changed(doc
, FALSE
);
881 case SCN_MODIFYATTEMPTRO
:
885 case SCN_MARGINCLICK
:
886 on_margin_click(editor
, nt
);
890 on_update_ui(editor
, nt
);
894 /* Visible lines are only laid out accurately just before painting,
895 * so we need to only call editor_scroll_to_line here, because the document
896 * may have line wrapping and folding enabled.
897 * http://scintilla.sourceforge.net/ScintillaDoc.html#LineWrapping
898 * This is important e.g. when loading a session and switching pages
899 * and having the cursor scroll in view. */
900 /* FIXME: Really we want to do this just before painting, not after it
901 * as it will cause repainting. */
902 if (editor
->scroll_percent
> 0.0F
)
904 editor_scroll_to_line(editor
, -1, editor
->scroll_percent
);
905 /* disable further scrolling */
906 editor
->scroll_percent
= -1.0F
;
911 if (editor_prefs
.show_linenumber_margin
&& (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) && nt
->linesAdded
)
913 /* automatically adjust Scintilla's line numbers margin width */
914 auto_update_margin_width(editor
);
916 if (nt
->modificationType
& SC_STARTACTION
&& ! ignore_callback
)
918 /* get notified about undo changes */
919 document_undo_add(doc
, UNDO_SCINTILLA
, NULL
);
921 if (editor_prefs
.folding
&& (nt
->modificationType
& SC_MOD_CHANGEFOLD
) != 0)
923 /* handle special fold cases, e.g. #1923350 */
924 fold_changed(sci
, nt
->line
, nt
->foldLevelNow
, nt
->foldLevelPrev
);
929 on_char_added(editor
, nt
);
932 case SCN_USERLISTSELECTION
:
933 if (nt
->listType
== 1)
935 sci_add_text(sci
, nt
->text
);
939 case SCN_AUTOCSELECTION
:
940 if (g_str_equal(nt
->text
, "..."))
947 case SCN_AUTOCCANCELLED
:
948 /* now that autocomplete is finishing or was cancelled, reshow calltips
949 * if they were showing */
950 request_reshowing_calltip(nt
);
954 case SCN_STYLENEEDED
:
955 geany_debug("style");
959 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
963 if (nt
->text
!= NULL
)
965 document_open_file_list(nt
->text
, -1);
969 case SCN_CALLTIPCLICK
:
970 if (nt
->position
> 0)
972 switch (nt
->position
)
974 case 1: /* up arrow */
975 if (calltip
.tag_index
> 0)
979 case 2: calltip
.tag_index
++; break; /* down arrow */
981 editor_show_calltip(editor
, -1);
986 /* recalculate line margin width */
987 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
990 /* we always return FALSE here to let plugins handle the event too */
995 /* Note: this is the same as sci_get_tab_width(), but is still useful when you don't have
996 * a scintilla pointer. */
997 static gint
get_tab_width(const GeanyIndentPrefs
*indent_prefs
)
999 if (indent_prefs
->type
== GEANY_INDENT_TYPE_BOTH
)
1000 return indent_prefs
->hard_tab_width
;
1002 return indent_prefs
->width
; /* tab width = indent width */
1006 /* Returns a string containing width chars of whitespace, filled with simple space
1007 * characters or with the right number of tab characters, according to the indent prefs.
1008 * (Result is filled with tabs *and* spaces if width isn't a multiple of
1009 * the tab width). */
1011 get_whitespace(const GeanyIndentPrefs
*iprefs
, gint width
)
1013 g_return_val_if_fail(width
>= 0, NULL
);
1016 return g_strdup("");
1018 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
1020 return g_strnfill(width
, ' ');
1023 { /* first fill text with tabs and fill the rest with spaces */
1024 const gint tab_width
= get_tab_width(iprefs
);
1025 gint tabs
= width
/ tab_width
;
1026 gint spaces
= width
% tab_width
;
1027 gint len
= tabs
+ spaces
;
1030 str
= g_malloc(len
+ 1);
1032 memset(str
, '\t', tabs
);
1033 memset(str
+ tabs
, ' ', spaces
);
1040 static const GeanyIndentPrefs
*
1041 get_default_indent_prefs(void)
1043 static GeanyIndentPrefs iprefs
;
1045 iprefs
= app
->project
? *app
->project
->priv
->indentation
: *editor_prefs
.indentation
;
1050 /** Gets the indentation prefs for the editor.
1051 * In future, the prefs might be different according to project or filetype.
1052 * @warning Always get a fresh result instead of keeping a pointer to it if the editor
1053 * settings may have changed, or if this function has been called for a different @a editor.
1054 * @param editor The editor, or @c NULL to get the default indent prefs.
1055 * @return The indent prefs. */
1056 const GeanyIndentPrefs
*
1057 editor_get_indent_prefs(GeanyEditor
*editor
)
1059 static GeanyIndentPrefs iprefs
;
1061 iprefs
= *get_default_indent_prefs();
1066 iprefs
.type
= editor
->indent_type
;
1068 /* if per-document auto-indent is enabled, but we don't have a global mode set,
1069 * just use basic auto-indenting */
1070 if (editor
->auto_indent
&& iprefs
.auto_indent_mode
== GEANY_AUTOINDENT_NONE
)
1071 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_BASIC
;
1073 if (!editor
->auto_indent
)
1074 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_NONE
;
1080 static void on_new_line_added(GeanyEditor
*editor
)
1082 ScintillaObject
*sci
= editor
->sci
;
1083 gint line
= sci_get_current_line(sci
);
1085 /* simple indentation */
1086 if (editor
->auto_indent
)
1088 insert_indent_after_line(editor
, line
- 1);
1091 if (editor_prefs
.auto_continue_multiline
)
1092 { /* " * " auto completion in multiline C/C++/D/Java comments */
1093 auto_multiline(editor
, line
);
1096 if (editor_prefs
.newline_strip
)
1097 { /* strip the trailing spaces on the previous line */
1098 editor_strip_line_trailing_spaces(editor
, line
- 1);
1103 static gboolean
lexer_has_braces(ScintillaObject
*sci
)
1105 gint lexer
= sci_get_lexer(sci
);
1111 case SCLEX_HTML
: /* for PHP & JS */
1112 case SCLEX_PASCAL
: /* for multiline comments? */
1123 /* Read indent chars for the line that pos is on into indent global variable.
1124 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1125 * instead in any new code. */
1126 static void read_indent(GeanyEditor
*editor
, gint pos
)
1128 ScintillaObject
*sci
= editor
->sci
;
1129 guint i
, len
, j
= 0;
1133 line
= sci_get_line_from_position(sci
, pos
);
1135 len
= sci_get_line_length(sci
, line
);
1136 linebuf
= sci_get_line(sci
, line
);
1138 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1140 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1141 indent
[j
++] = linebuf
[i
];
1150 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1156 len
= sci_get_line_length(sci
, line
);
1157 linebuf
= sci_get_line(sci
, line
);
1159 for (i
= 0; i
< len
; i
++)
1161 /* i == (len - 1) prevents wrong indentation after lines like
1162 * " { return bless({}, shift); }" (Perl) */
1163 if (linebuf
[i
] == '{' && i
== (len
- 1))
1172 while (k
> 0 && isspace(linebuf
[k
])) k
--;
1174 /* if last non-whitespace character is a { increase indentation by a tab
1175 * e.g. for (...) { */
1176 if (linebuf
[k
] == '{')
1188 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1190 gint last_char
= sci_get_line_end_position(sci
, line
) - 1;
1192 /* add extra indentation for Python after colon */
1193 if (sci_get_char_at(sci
, last_char
) == ':' &&
1194 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1202 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1204 ScintillaObject
*sci
= editor
->sci
;
1206 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1208 g_return_val_if_fail(line
>= 0, 0);
1210 size
= sci_get_line_indentation(sci
, line
);
1212 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1214 if (lexer_has_braces(sci
))
1215 size
+= iprefs
->width
* get_brace_indent(sci
, line
);
1217 if (FILETYPE_ID(editor
->document
->file_type
) == GEANY_FILETYPES_PYTHON
)
1218 size
+= iprefs
->width
* get_python_indent(sci
, line
);
1224 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
)
1226 ScintillaObject
*sci
= editor
->sci
;
1227 gint line_indent
= sci_get_line_indentation(sci
, line
);
1228 gint size
= get_indent_size_after_line(editor
, line
);
1229 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1235 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
&& size
== line_indent
)
1237 /* support tab indents, space aligns style - copy last line 'indent' exactly */
1238 gint start
= sci_get_position_from_line(sci
, line
);
1239 gint end
= sci_get_line_indent_position(sci
, line
);
1241 text
= sci_get_contents_range(sci
, start
, end
);
1245 text
= get_whitespace(iprefs
, size
);
1247 sci_add_text(sci
, text
);
1252 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
)
1254 const gchar
*closing_char
= NULL
;
1257 if (utils_isbrace(c
, 0))
1258 end_pos
= sci_find_matching_brace(sci
, pos
- 1);
1263 if ((editor_prefs
.autoclose_chars
& GEANY_AC_PARENTHESIS
) && end_pos
== -1)
1267 if ((editor_prefs
.autoclose_chars
& GEANY_AC_CBRACKET
) && end_pos
== -1)
1271 if ((editor_prefs
.autoclose_chars
& GEANY_AC_SBRACKET
) && end_pos
== -1)
1275 if (editor_prefs
.autoclose_chars
& GEANY_AC_SQUOTE
)
1279 if (editor_prefs
.autoclose_chars
& GEANY_AC_DQUOTE
)
1280 closing_char
= "\"";
1284 if (closing_char
!= NULL
)
1286 sci_add_text(sci
, closing_char
);
1287 sci_set_current_position(sci
, pos
, TRUE
);
1292 /* Finds a corresponding matching brace to the given pos
1293 * (this is taken from Scintilla Editor.cxx,
1294 * fit to work with close_block) */
1295 static gint
brace_match(ScintillaObject
*sci
, gint pos
)
1297 gchar chBrace
= sci_get_char_at(sci
, pos
);
1298 gchar chSeek
= utils_brace_opposite(chBrace
);
1300 gint direction
= -1;
1305 styBrace
= sci_get_style_at(sci
, pos
);
1307 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1310 pos
= pos
+ direction
;
1311 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1313 chAtPos
= sci_get_char_at(sci
, pos
- 1);
1314 styAtPos
= sci_get_style_at(sci
, pos
);
1316 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1318 if (chAtPos
== chBrace
)
1320 if (chAtPos
== chSeek
)
1325 pos
= pos
+ direction
;
1331 /* Called after typing '}'. */
1332 static void close_block(GeanyEditor
*editor
, gint pos
)
1335 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1336 gint x
= 0, cnt
= 0;
1337 gint line
, line_len
, eol_char_len
;
1338 gchar
*text
, *line_buf
;
1339 ScintillaObject
*sci
;
1340 gint line_indent
, last_indent
;
1342 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1344 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1347 doc
= editor
->document
;
1349 if (! lexer_has_braces(sci
))
1352 line
= sci_get_line_from_position(sci
, pos
);
1353 line_len
= sci_get_line_length(sci
, line
);
1354 /* set eol_char_len to 0 if on last line, because there is no EOL char */
1355 eol_char_len
= (line
== (sci_get_line_count(sci
) - 1)) ? 0 :
1356 editor_get_eol_char_len(editor
);
1358 /* check that the line is empty, to not kill text in the line */
1359 line_buf
= sci_get_line(sci
, line
);
1360 line_buf
[line_len
- eol_char_len
] = '\0';
1361 while (x
< (line_len
- eol_char_len
))
1363 if (isspace(line_buf
[x
]))
1369 if ((line_len
- eol_char_len
- 1) != cnt
)
1372 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1374 gint start_brace
= brace_match(sci
, pos
);
1376 if (start_brace
>= 0)
1379 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1380 gint size
= sci_get_line_indentation(sci
, brace_line
);
1381 gchar
*ind
= get_whitespace(iprefs
, size
);
1383 text
= g_strconcat(ind
, "}", NULL
);
1384 line_start
= sci_get_position_from_line(sci
, line
);
1385 sci_set_anchor(sci
, line_start
);
1386 sci_replace_sel(sci
, text
);
1391 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1394 /* GEANY_AUTOINDENT_CURRENTCHARS */
1395 line_indent
= sci_get_line_indentation(sci
, line
);
1396 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1398 if (line_indent
< last_indent
)
1400 line_indent
-= iprefs
->width
;
1401 line_indent
= MAX(0, line_indent
);
1402 sci_set_line_indentation(sci
, line
, line_indent
);
1406 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1407 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1408 * position can be -1, then the current position is used.
1409 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1410 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1411 const gchar
*wc
, gboolean stem
)
1413 gint line
, line_start
, startword
, endword
;
1415 ScintillaObject
*sci
;
1417 g_return_if_fail(editor
!= NULL
);
1421 pos
= sci_get_current_position(sci
);
1423 line
= sci_get_line_from_position(sci
, pos
);
1424 line_start
= sci_get_position_from_line(sci
, line
);
1425 startword
= pos
- line_start
;
1426 endword
= pos
- line_start
;
1429 chunk
= sci_get_line(sci
, line
);
1432 wc
= GEANY_WORDCHARS
;
1434 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1435 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1436 * TODO: improve this code */
1437 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || chunk
[startword
- 1] < 0))
1441 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || chunk
[endword
] < 0))
1445 if (startword
!= endword
)
1447 chunk
[endword
] = '\0';
1449 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1452 g_strlcpy(word
, "", wordlen
);
1458 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1459 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1460 * position can be -1, then the current position is used.
1461 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1462 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1465 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1470 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1471 * Otherwise NULL is returned.
1472 * Additional wordchars can be specified to define what to consider as a word.
1474 * @param editor The editor to operate on.
1475 * @param pos The position where the word should be read from.
1476 * Maybe @c -1 to use the current position.
1477 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1478 * as part of a word. Maybe @c NULL to use the default wordchars,
1479 * see @ref GEANY_WORDCHARS.
1481 * @return A newly-allocated string containing the word at the given @a pos or @c NULL.
1482 * Should be freed when no longer needed.
1486 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1488 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1490 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1492 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1494 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1498 /* Read the word up to position @a pos. */
1499 static const gchar
*
1500 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1502 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1504 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1506 return (*word
) ? word
: NULL
;
1510 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1513 gint orig_pos
= pos
;
1515 c
= sci_get_char_at(sci
, pos
);
1516 while (pos
>= 0 && pos
> orig_pos
- 300)
1518 c
= sci_get_char_at(sci
, pos
);
1520 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1527 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1531 gint orig_pos
= pos
;
1533 c
= sci_get_char_at(sci
, pos
);
1534 while (pos
> 0 && pos
> orig_pos
- 300)
1536 c
= sci_get_char_at(sci
, pos
);
1537 if (c
== ')') brackets
++;
1538 else if (c
== '(') brackets
--;
1540 if (brackets
< 0) return pos
; /* found start bracket */
1546 static gboolean
append_calltip(GString
*str
, const TMTag
*tag
, filetype_id ft_id
)
1548 if (! tag
->atts
.entry
.arglist
)
1551 if (ft_id
!= GEANY_FILETYPES_PASCAL
)
1552 { /* usual calltips: "retval tagname (arglist)" */
1553 if (tag
->atts
.entry
.var_type
)
1557 g_string_append(str
, tag
->atts
.entry
.var_type
);
1558 for (i
= 0; i
< tag
->atts
.entry
.pointerOrder
; i
++)
1560 g_string_append_c(str
, '*');
1562 g_string_append_c(str
, ' ');
1564 if (tag
->atts
.entry
.scope
)
1566 const gchar
*cosep
= symbols_get_context_separator(ft_id
);
1568 g_string_append(str
, tag
->atts
.entry
.scope
);
1569 g_string_append(str
, cosep
);
1571 g_string_append(str
, tag
->name
);
1572 g_string_append_c(str
, ' ');
1573 g_string_append(str
, tag
->atts
.entry
.arglist
);
1576 { /* special case Pascal calltips: "tagname (arglist) : retval" */
1577 g_string_append(str
, tag
->name
);
1578 g_string_append_c(str
, ' ');
1579 g_string_append(str
, tag
->atts
.entry
.arglist
);
1581 if (NZV(tag
->atts
.entry
.var_type
))
1583 g_string_append(str
, " : ");
1584 g_string_append(str
, tag
->atts
.entry
.var_type
);
1592 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1594 const GPtrArray
*tags
;
1595 const gint arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1596 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1597 TMTagAttrType
*attrs
= NULL
;
1599 GString
*str
= NULL
;
1602 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1604 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1605 tags
= tm_workspace_find(word
, tm_tag_max_t
, attrs
, FALSE
, ft
->lang
);
1609 tag
= TM_TAG(tags
->pdata
[0]);
1611 if (tag
->type
== tm_tag_class_t
&& FILETYPE_ID(ft
) == GEANY_FILETYPES_D
)
1613 /* user typed e.g. 'new Classname(' so lookup D constructor Classname::this() */
1614 tags
= tm_workspace_find_scoped("this", tag
->name
,
1615 arg_types
, attrs
, FALSE
, ft
->lang
, TRUE
);
1620 /* remove tags with no argument list */
1621 for (i
= 0; i
< tags
->len
; i
++)
1623 tag
= TM_TAG(tags
->pdata
[i
]);
1625 if (! tag
->atts
.entry
.arglist
)
1626 tags
->pdata
[i
] = NULL
;
1628 tm_tags_prune((GPtrArray
*) tags
);
1632 { /* remove duplicate calltips */
1633 TMTagAttrType sort_attr
[] = {tm_tag_attr_name_t
, tm_tag_attr_scope_t
,
1634 tm_tag_attr_arglist_t
, 0};
1636 tm_tags_sort((GPtrArray
*) tags
, sort_attr
, TRUE
);
1639 /* if the current word has changed since last time, start with the first tag match */
1640 if (! utils_str_equal(word
, calltip
.last_word
))
1641 calltip
.tag_index
= 0;
1642 /* cache the current word for next time */
1643 g_free(calltip
.last_word
);
1644 calltip
.last_word
= g_strdup(word
);
1645 calltip
.tag_index
= MIN(calltip
.tag_index
, tags
->len
- 1); /* ensure tag_index is in range */
1647 for (i
= calltip
.tag_index
; i
< tags
->len
; i
++)
1649 tag
= TM_TAG(tags
->pdata
[i
]);
1653 str
= g_string_new(NULL
);
1654 if (calltip
.tag_index
> 0)
1655 g_string_prepend(str
, "\001 "); /* up arrow */
1656 append_calltip(str
, tag
, FILETYPE_ID(ft
));
1658 else /* add a down arrow */
1660 if (calltip
.tag_index
> 0) /* already have an up arrow */
1661 g_string_insert_c(str
, 1, '\002');
1663 g_string_prepend(str
, "\002 ");
1669 gchar
*result
= str
->str
;
1671 g_string_free(str
, FALSE
);
1678 /* use pos = -1 to search for the previous unmatched open bracket. */
1679 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
1681 gint orig_pos
= pos
; /* the position for the calltip */
1684 gchar word
[GEANY_MAX_WORD_LENGTH
];
1686 ScintillaObject
*sci
;
1688 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1689 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
1693 lexer
= sci_get_lexer(sci
);
1697 /* position of '(' is unknown, so go backwards from current position to find it */
1698 pos
= sci_get_current_position(sci
);
1701 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
1702 find_start_bracket(sci
, pos
);
1707 /* the style 1 before the brace (which may be highlighted) */
1708 style
= sci_get_style_at(sci
, pos
- 1);
1709 if (! is_code_style(lexer
, style
))
1713 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
1714 if (word
[0] == '\0')
1717 str
= find_calltip(word
, editor
->document
->file_type
);
1720 g_free(calltip
.text
); /* free the old calltip */
1722 calltip
.pos
= orig_pos
;
1725 utils_wrap_string(calltip
.text
, -1);
1726 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
1733 gchar
*editor_get_calltip_text(GeanyEditor
*editor
, const TMTag
*tag
)
1737 g_return_val_if_fail(editor
!= NULL
, NULL
);
1739 str
= g_string_new(NULL
);
1740 if (append_calltip(str
, tag
, FILETYPE_ID(editor
->document
->file_type
)))
1741 return g_string_free(str
, FALSE
);
1743 return g_string_free(str
, TRUE
);
1747 /* HTML entities auto completion from html_entities.tags text file */
1749 autocomplete_html(ScintillaObject
*sci
, const gchar
*root
, gsize rootlen
)
1752 gboolean found
= FALSE
;
1754 const gchar
**entities
= symbols_get_html_entities();
1756 if (*root
!= '&' || G_UNLIKELY(entities
== NULL
))
1759 words
= g_string_sized_new(500);
1762 if (entities
[i
] == NULL
)
1764 else if (entities
[i
][0] == '#')
1767 if (! strncmp(entities
[i
], root
, rootlen
))
1770 g_string_append_c(words
, '\n');
1771 g_string_append(words
, entities
[i
]);
1776 show_autocomplete(sci
, rootlen
, words
->str
);
1778 g_string_free(words
, TRUE
);
1783 /* Current document & global tags autocompletion */
1785 autocomplete_tags(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
1787 TMTagAttrType attrs
[] = { tm_tag_attr_name_t
, 0 };
1788 const GPtrArray
*tags
;
1791 g_return_val_if_fail(editor
, FALSE
);
1793 doc
= editor
->document
;
1795 tags
= tm_workspace_find(root
, tm_tag_max_t
, attrs
, TRUE
, doc
->file_type
->lang
);
1798 show_tags_list(editor
, tags
, rootlen
);
1799 return tags
->len
> 0;
1805 /* Check whether to use entity autocompletion:
1806 * - always in a HTML file except when inside embedded JavaScript, Python, ASP, ...
1807 * - in a PHP file only when we are outside of <? ?> */
1808 static gboolean
autocomplete_check_for_html(gint ft_id
, gint style
)
1810 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
1811 * (everything after SCE_HJ_START is for embedded scripting languages) */
1812 if (ft_id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
1815 if (ft_id
== GEANY_FILETYPES_PHP
)
1817 /* use entity completion when style is outside of PHP styles */
1818 if (! is_style_php(style
))
1826 /* Algorithm based on based on Scite's StartAutoCompleteWord() */
1827 static GString
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
1830 gint len
, current
, word_end
;
1831 gint pos_find
, flags
;
1835 struct Sci_TextToFind ttf
;
1837 len
= sci_get_length(sci
);
1838 current
= sci_get_current_position(sci
) - rootlen
;
1840 ttf
.lpstrText
= root
;
1842 ttf
.chrg
.cpMax
= len
;
1843 ttf
.chrgText
.cpMin
= 0;
1844 ttf
.chrgText
.cpMax
= 0;
1845 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
1847 words
= g_string_sized_new(256);
1848 /* put space before first entry to make searching with strstr easy */
1849 g_string_append_c(words
, ' ');
1851 /* search the whole document for the word root and collect results */
1852 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
1853 while (pos_find
>= 0 && pos_find
< len
)
1855 word_end
= pos_find
+ rootlen
;
1856 if (pos_find
!= current
)
1858 while (word_end
< len
&& strchr(GEANY_WORDCHARS
, sci_get_char_at(sci
, word_end
)) != NULL
)
1861 word_length
= word_end
- pos_find
;
1862 if (word_length
> rootlen
)
1864 word
= g_malloc0(word_length
+ 3);
1865 sci_get_text_range(sci
, pos_find
, word_end
, word
+ 1);
1867 word
[word_length
+ 1] = ' ';
1868 /* search the words string whether we already have the word in, otherwise add it */
1869 if (strstr(words
->str
, word
) == NULL
)
1871 g_string_append(words
, word
+ 1);
1876 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
1880 ttf
.chrg
.cpMin
= word_end
;
1881 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
1886 g_strdelimit(words
->str
, " ", '\n');
1887 words
->str
[words
->len
- 1] = '\0'; /* remove the trailing '\n' */
1890 g_string_free(words
, TRUE
);
1895 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
1897 ScintillaObject
*sci
= editor
->sci
;
1901 GSList
*node
, *list
= NULL
;
1903 words
= get_doc_words(sci
, root
, rootlen
);
1906 scintilla_send_message(sci
, SCI_AUTOCCANCEL
, 0, 0);
1910 /* words are unsorted, make list of words */
1911 foreach_str(ptr
, words
->str
)
1915 list
= g_slist_prepend(list
, ptr
+ 1);
1916 /* terminate previous string in list */
1921 list
= g_slist_sort(list
, (GCompareFunc
)utils_str_casecmp
);
1923 str
= g_string_sized_new(words
->len
);
1924 foreach_slist(node
, list
)
1926 g_string_append(str
, node
->data
);
1928 g_string_append_c(str
, '\n');
1930 if (g_slist_length(list
) >= editor_prefs
.autocompletion_max_entries
)
1931 g_string_append(str
, "\n...");
1934 g_string_free(words
, TRUE
);
1936 show_autocomplete(sci
, rootlen
, str
->str
);
1937 g_string_free(str
, TRUE
);
1942 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
1944 gint line
, line_start
, line_len
, line_pos
, current
, rootlen
, startword
, lexer
, style
;
1945 gchar
*linebuf
, *root
;
1946 ScintillaObject
*sci
;
1947 gboolean ret
= FALSE
;
1948 const gchar
*wordchars
;
1951 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
1954 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1956 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
1957 * necessary styling information */
1958 if (G_UNLIKELY(pos
< 2))
1962 ft
= editor
->document
->file_type
;
1964 line
= sci_get_line_from_position(sci
, pos
);
1965 line_start
= sci_get_position_from_line(sci
, line
);
1966 line_len
= sci_get_line_length(sci
, line
);
1967 line_pos
= pos
- line_start
- 1;
1968 current
= pos
- line_start
;
1969 startword
= current
;
1970 lexer
= sci_get_lexer(sci
);
1971 style
= sci_get_style_at(sci
, pos
- 2);
1973 /* don't autocomplete in comments and strings */
1974 if (!force
&& !is_code_style(lexer
, style
))
1977 autocomplete_scope(editor
);
1979 linebuf
= sci_get_line(sci
, line
);
1981 if (ft
->id
== GEANY_FILETYPES_LATEX
)
1982 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
1983 else if (ft
->id
== GEANY_FILETYPES_HTML
|| ft
->id
== GEANY_FILETYPES_PHP
)
1984 wordchars
= GEANY_WORDCHARS
"&"; /* add & to word chars if we are in a PHP or HTML file */
1986 wordchars
= GEANY_WORDCHARS
;
1988 /* find the start of the current word */
1989 while ((startword
> 0) && (strchr(wordchars
, linebuf
[startword
- 1])))
1991 linebuf
[current
] = '\0';
1992 root
= linebuf
+ startword
;
1993 rootlen
= current
- startword
;
1997 if (autocomplete_check_for_html(ft
->id
, style
))
1999 /* Allow something like ""some text"". The above startword calculation
2000 * only works on words but for HTML entity completion we also want to have completion
2001 * based on '&' within words. */
2002 gchar
*tmp
= strchr(root
, '&');
2006 rootlen
= strlen(tmp
);
2008 ret
= autocomplete_html(sci
, root
, rootlen
);
2012 /* force is set when called by keyboard shortcut, otherwise start at the
2013 * editor_prefs.symbolcompletion_min_chars'th char */
2014 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
2016 /* complete tags, except if forcing when completion is already visible */
2017 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
2018 ret
= autocomplete_tags(editor
, root
, rootlen
);
2020 /* If forcing and there's nothing else to show, complete from words in document */
2021 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2022 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2034 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2036 gchar
*result
= NULL
;
2039 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2041 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2044 result
= g_hash_table_lookup(tmp
, name
);
2046 /* whether nothing is set for the current filetype(tmp is NULL) or
2047 * the particular completion for this filetype is not set (result is NULL) */
2048 if (tmp
== NULL
|| result
== NULL
)
2050 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2053 result
= g_hash_table_lookup(tmp
, name
);
2056 /* if result is still NULL here, no completion could be found */
2058 /* result is owned by the hash table and will be freed when the table will destroyed */
2063 /* This is very ugly but passing the pattern to ac_replace_specials() doesn't work because it is
2064 * modified when replacing a completion but the foreach function still passes the old pointer
2065 * to ac_replace_specials, so we use a global pointer outside of ac_replace_specials and
2066 * ac_complete_constructs. Any hints to improve this are welcome. */
2067 static GString
*snippets_global_pattern
= NULL
;
2069 static void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2073 g_return_if_fail(key
!= NULL
);
2074 g_return_if_fail(value
!= NULL
);
2076 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2078 utils_string_replace_all(snippets_global_pattern
, needle
, (gchar
*) value
);
2083 /* this only works with spaces only indentation on the lines */
2084 static void fix_line_indents(GeanyEditor
*editor
, gint line_start
, gint line_end
)
2086 ScintillaObject
*sci
= editor
->sci
;
2087 gint line
, cur_line
, cur_col
, pos
;
2089 /* get the line, col position as fixing indentation will move cursor to start of line */
2090 pos
= sci_get_current_position(sci
);
2091 cur_col
= sci_get_col_from_position(sci
, pos
);
2092 cur_line
= sci_get_current_line(sci
);
2094 for (line
= line_start
; line
<= line_end
; line
++)
2096 gint size
= sci_get_line_indentation(sci
, line
);
2098 /* set to 0 first to trigger proper indent creation */
2099 sci_set_line_indentation(sci
, line
, 0);
2100 sci_set_line_indentation(sci
, line
, size
);
2102 pos
= scintilla_send_message(sci
, SCI_FINDCOLUMN
, cur_line
, cur_col
);
2103 sci_set_current_position(sci
, pos
, FALSE
);
2107 static void replace_leading_tabs(GString
*str
, const gchar
*whitespace
)
2111 regmatch_t matches
[2];
2114 if (regcomp(®ex
, "^ *(\t)", 0) != 0)
2116 g_return_if_fail(FALSE
);
2121 if (regexec(®ex
, ptr
,
2122 G_N_ELEMENTS(matches
), matches
, 0) != 0)
2125 pos
= matches
[1].rm_so
;
2126 g_return_if_fail(pos
>= 0);
2127 pos
+= ptr
- str
->str
;
2128 g_string_erase(str
, pos
, 1);
2129 g_string_insert(str
, pos
, whitespace
);
2130 ptr
= str
->str
+ pos
+ strlen(whitespace
);
2136 /** Inserts text, replacing \\t tab chars (@c 0x9) and \\n newline chars (@c 0xA)
2137 * accordingly for the document.
2138 * - Leading tabs are replaced with the correct indentation.
2139 * - Non-leading tabs are replaced with spaces (except when using 'Tabs' indent type).
2140 * - Newline chars are replaced with the correct line ending string.
2141 * This is very useful for inserting code without having to handle the indent
2142 * type yourself (Tabs & Spaces mode can be tricky).
2143 * @param editor Editor.
2144 * @param text Intended as e.g. @c "if (foo)\n\tbar();".
2145 * @param insert_pos Document position to insert text at.
2146 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2147 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2148 * -1 to read the indent size from the line with @a insert_pos on it.
2149 * @param replace_newlines Whether to replace newlines. If
2150 * newlines have been replaced already, this should be false, to avoid errors e.g. on Windows.
2151 * @warning Make sure all \\t tab chars in @a text are intended as indent widths or alignment,
2152 * not hard tabs, as those won't be preserved.
2153 * @note This doesn't scroll the cursor in view afterwards. **/
2154 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2155 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2157 ScintillaObject
*sci
= editor
->sci
;
2158 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2162 const gchar cur_marker
[] = "__GEANY_CURSOR_MARKER__";
2163 const gchar
*eol
= editor_get_eol_char(editor
);
2164 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2166 g_return_if_fail(text
);
2167 g_return_if_fail(editor
!= NULL
);
2168 g_return_if_fail(insert_pos
>= 0);
2170 buf
= g_string_new(text
);
2172 if (cursor_index
>= 0)
2173 g_string_insert(buf
, cursor_index
, cur_marker
); /* remember cursor pos */
2175 if (newline_indent_size
== -1)
2177 /* count indent size up to insert_pos instead of asking sci
2178 * because there may be spaces after it */
2179 gchar
*tmp
= sci_get_line(sci
, line_start
);
2180 gint idx
= insert_pos
- sci_get_position_from_line(sci
, line_start
);
2182 newline_indent_size
= count_indent_size(editor
, tmp
);
2186 /* Add line indents (in spaces) */
2187 if (newline_indent_size
> 0)
2189 whitespace
= g_strnfill(newline_indent_size
, ' ');
2190 setptr(whitespace
, g_strconcat(eol
, whitespace
, NULL
));
2191 utils_string_replace_all(buf
, eol
, whitespace
);
2195 /* transform line endings */
2196 if (replace_newlines
)
2197 utils_string_replace_all(buf
, "\n", eol
);
2199 /* transform leading tabs into indent widths (in spaces) */
2200 whitespace
= g_strnfill(iprefs
->width
, ' ');
2201 replace_leading_tabs(buf
, whitespace
);
2202 /* remaining tabs are for alignment */
2203 if (iprefs
->type
!= GEANY_INDENT_TYPE_TABS
)
2204 utils_string_replace_all(buf
, "\t", whitespace
);
2207 sci_start_undo_action(sci
);
2209 if (cursor_index
>= 0)
2211 gint idx
= utils_strpos(buf
->str
, cur_marker
);
2213 g_string_erase(buf
, idx
, strlen(cur_marker
));
2215 sci_insert_text(sci
, insert_pos
, buf
->str
);
2216 sci_set_current_position(sci
, insert_pos
+ idx
, FALSE
);
2219 sci_insert_text(sci
, insert_pos
, buf
->str
);
2221 /* fixup indentation (very useful for Tabs & Spaces indent type) */
2222 line_end
= sci_get_line_from_position(sci
, insert_pos
+ buf
->len
);
2223 fix_line_indents(editor
, line_start
, line_end
);
2224 snippet_cursor_insert_pos
= sci_get_current_position(sci
);
2226 sci_end_undo_action(sci
);
2227 g_string_free(buf
, TRUE
);
2231 /* Move the cursor to the next specified cursor position in an inserted snippet.
2232 * Can, and should, be optimized to give better results */
2233 void editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2235 ScintillaObject
*sci
= editor
->sci
;
2236 gint current_pos
= sci_get_current_position(sci
);
2238 if (snippet_offsets
&& !g_queue_is_empty(snippet_offsets
))
2242 offset
= GPOINTER_TO_INT(g_queue_pop_head(snippet_offsets
));
2243 if (current_pos
> snippet_cursor_insert_pos
)
2244 snippet_cursor_insert_pos
= offset
+ current_pos
;
2246 snippet_cursor_insert_pos
+= offset
;
2248 sci_set_current_position(sci
, snippet_cursor_insert_pos
, FALSE
);
2257 static gssize
snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
,
2260 gssize cur_index
= -1;
2262 gint i
, tmp_pos
, whitespace_len
, nl_count
= 0;
2263 GHashTable
*specials
;
2264 GList
*temp_list
= NULL
;
2265 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2266 gint cursor_steps
, old_cursor
= 0;
2268 /* replace 'special' completions */
2269 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2270 if (G_LIKELY(specials
!= NULL
))
2272 /* ugly hack using global_pattern */
2273 snippets_global_pattern
= pattern
;
2274 g_hash_table_foreach(specials
, snippets_replace_specials
, NULL
);
2277 /* replace any template {foo} wildcards */
2278 templates_replace_common(pattern
, editor
->document
->file_name
, editor
->document
->file_type
, NULL
);
2280 /* transform other wildcards */
2281 /* convert to %newlines%, else we get endless loops */
2282 utils_string_replace_all(pattern
, "\n", "%newline%");
2284 /* if spaces are used, replaces all tabs with %ws%, which is later replaced
2285 * by real whitespace characters
2286 * otherwise replace all %ws% by \t, which will be replaced later by tab
2288 * this makes seperating between tab and spaces intentation pretty easy */
2289 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
2290 utils_string_replace_all(pattern
, "\t", "%ws%");
2292 utils_string_replace_all(pattern
, "%ws%", "\t");
2294 whitespace
= g_strnfill(iprefs
->width
, ' '); /* use spaces for indentation, will be fixed up */
2295 whitespace_len
= strlen(whitespace
);
2297 while ((cursor_steps
= utils_strpos(pattern
->str
, "%cursor%")) >= 0)
2299 /* replace every %newline% (up to next %cursor%) with EOL,
2300 * and update cursor_steps after */
2301 while ((tmp_pos
= utils_strpos(pattern
->str
, "%newline%")) < cursor_steps
&& tmp_pos
!= -1)
2304 utils_string_replace_first(pattern
, "%newline%", editor_get_eol_char(editor
));
2305 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2307 /* replace every %ws% (up to next %cursor%) with whitespaces,
2308 * and update cursor_steps after */
2309 while ((tmp_pos
= utils_strpos(pattern
->str
, "%ws%")) < cursor_steps
&& tmp_pos
!= -1)
2311 utils_string_replace_first(pattern
, "%ws%", whitespace
);
2312 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2314 /* finally replace the next %cursor% */
2315 utils_string_replace_first(pattern
, "%cursor%", "");
2317 /* modify cursor_steps to take indentation count and type into account */
2319 /* We're saving the relative offset to each cursor position in a simple
2320 * linked list, including indentations between them. */
2323 cursor_steps
+= (nl_count
* indent_size
);
2324 temp_list
= g_list_append(temp_list
, GINT_TO_POINTER(cursor_steps
- old_cursor
));
2329 cur_index
= cursor_steps
;
2331 old_cursor
= cursor_steps
;
2333 /* replace remaining %ws% and %newline% which may occur after the last %cursor% */
2334 utils_string_replace_all(pattern
, "%newline%", editor_get_eol_char(editor
));
2335 utils_string_replace_all(pattern
, "%ws%", whitespace
);
2339 /* Bug: {ob}pc{cb} will be replaced by % too */
2340 templates_replace_valist(pattern
, "{pc}", "%", NULL
);
2342 /* We put the cursor positions for the most recent
2343 * parsed snippet first, followed by any remaining positions */
2349 foreach_list(node
, temp_list
)
2350 g_queue_push_nth(snippet_offsets
, node
->data
, i
++);
2352 /* limit length of queue */
2353 while (g_queue_get_length(snippet_offsets
) > 20)
2354 g_queue_pop_tail(snippet_offsets
);
2356 g_list_free(temp_list
);
2359 cur_index
= pattern
->len
;
2365 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2367 ScintillaObject
*sci
= editor
->sci
;
2370 gssize cur_index
= -1;
2372 gint ft_id
= FILETYPE_ID(editor
->document
->file_type
);
2374 str
= g_strdup(word
);
2376 pattern
= g_string_new(snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
));
2377 if (pattern
== NULL
|| pattern
->len
== 0)
2380 g_string_free(pattern
, TRUE
);
2384 read_indent(editor
, pos
);
2386 /* remove the typed word, it will be added again by the used auto completion
2387 * (not really necessary but this makes the auto completion more flexible,
2388 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2389 str_len
= strlen(str
);
2390 sci_set_selection_start(sci
, pos
- str_len
);
2391 sci_set_selection_end(sci
, pos
);
2392 sci_replace_sel(sci
, "");
2393 pos
-= str_len
; /* pos has changed while deleting */
2395 cur_index
= snippets_make_replacements(editor
, pattern
, strlen(indent
));
2397 /* finally insert the text and set the cursor */
2398 editor_insert_text_block(editor
, pattern
->str
, pos
, cur_index
, -1, FALSE
);
2399 sci_scroll_caret(sci
);
2402 g_string_free(pattern
, TRUE
);
2407 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2409 gint line
= sci_get_line_from_position(sci
, pos
);
2412 /* skip any trailing spaces */
2415 c
= sci_get_char_at(sci
, pos
);
2416 if (c
== ' ' || c
== '\t')
2422 return (pos
== sci_get_line_end_position(sci
, line
));
2426 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2428 gboolean result
= FALSE
;
2431 ScintillaObject
*sci
;
2433 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2436 if (sci_has_selection(sci
))
2438 /* return if we are editing an existing line (chars on right of cursor) */
2439 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2440 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_space
&&
2441 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2444 wc
= snippets_find_completion_by_name("Special", "wordchars");
2445 word
= editor_read_word_stem(editor
, pos
, wc
);
2447 /* prevent completion of "for " */
2449 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2451 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2452 result
= snippets_complete_constructs(editor
, pos
, word
);
2453 sci_end_undo_action(sci
);
2455 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2461 void editor_show_macro_list(GeanyEditor
*editor
)
2465 if (editor
== NULL
|| editor
->document
->file_type
== NULL
)
2468 words
= symbols_get_macro_list(editor
->document
->file_type
->lang
);
2472 SSM(editor
->sci
, SCI_USERLISTSHOW
, 1, (sptr_t
) words
->str
);
2473 g_string_free(words
, TRUE
);
2477 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2479 ScintillaObject
*sci
= editor
->sci
;
2480 gchar
*to_insert
= NULL
;
2484 const gchar
*gt
= ">";
2485 /* if there is already a '>' behind the cursor, don't add it */
2486 if (sci_get_char_at(sci
, pos
) == '>')
2489 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2492 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2494 sci_start_undo_action(sci
);
2495 sci_replace_sel(sci
, to_insert
);
2498 sci_set_selection(sci
, pos
, pos
);
2499 if (utils_str_equal(tag_name
, "table"))
2500 auto_table(editor
, pos
);
2502 sci_end_undo_action(sci
);
2508 * (stolen from anjuta and heavily modified)
2509 * This routine will auto complete XML or HTML tags that are still open by closing them
2510 * @param ch The character we are dealing with, currently only works with the '>' character
2511 * @return True if handled, false otherwise
2513 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2515 ScintillaObject
*sci
= editor
->sci
;
2516 gint lexer
= sci_get_lexer(sci
);
2518 gchar
*str_found
, sel
[512];
2519 gboolean result
= FALSE
;
2521 /* If the user has turned us off, quit now.
2522 * This may make sense only in certain languages */
2523 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2526 /* return if we are inside any embedded script */
2527 style
= sci_get_style_at(sci
, pos
);
2528 if (style
> SCE_H_XCCOMMENT
&& ! is_string_style(lexer
, style
))
2531 /* if ch is /, check for </, else quit */
2532 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2535 /* Grab the last 512 characters or so */
2536 min
= pos
- (sizeof(sel
) - 1);
2537 if (min
< 0) min
= 0;
2540 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2542 sci_get_text_range(sci
, min
, pos
, sel
);
2543 sel
[sizeof(sel
) - 1] = '\0';
2545 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2546 /* User typed something like "<br/>" */
2549 str_found
= utils_find_open_xml_tag(sel
, pos
- min
, (ch
== '/'));
2551 /* when found string is something like br, img or another short tag, quit */
2552 if (utils_str_equal(str_found
, "br")
2553 || utils_str_equal(str_found
, "hr")
2554 || utils_str_equal(str_found
, "img")
2555 || utils_str_equal(str_found
, "base")
2556 || utils_str_equal(str_found
, "basefont") /* < or not < */
2557 || utils_str_equal(str_found
, "frame")
2558 || utils_str_equal(str_found
, "input")
2559 || utils_str_equal(str_found
, "link")
2560 || utils_str_equal(str_found
, "area")
2561 || utils_str_equal(str_found
, "meta"))
2565 else if (NZV(str_found
))
2567 insert_closing_tag(editor
, pos
, ch
, str_found
);
2575 /* like sci_get_line_indentation(), but for a string. */
2576 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2579 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2582 g_return_val_if_fail(base_indent
, 0);
2584 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2600 static void auto_table(GeanyEditor
*editor
, gint pos
)
2602 ScintillaObject
*sci
= editor
->sci
;
2605 const gchar
*indent_str
;
2607 if (sci_get_lexer(sci
) != SCLEX_HTML
) return;
2609 read_indent(editor
, pos
);
2610 indent_pos
= sci_get_line_indent_position(sci
, sci_get_line_from_position(sci
, pos
));
2611 if ((pos
- 7) != indent_pos
) /* 7 == strlen("<table>") */
2617 /* find the start of the <table tag */
2619 while (i
<= pos
&& sci_get_char_at(sci
, pos
- i
) != '<') i
++;
2620 /* add all non whitespace before the tag to the indent string */
2621 while ((pos
- i
) != indent_pos
&& x
< sizeof(indent
) - 1)
2629 if (! editor
->auto_indent
)
2634 table
= g_strconcat("\n", indent_str
, "<tr>\n",
2635 indent_str
, indent_str
, "<td> </td>\n",
2636 indent_str
, "</tr>\n",
2638 editor_insert_text_block(editor
, table
, pos
, -1,
2639 count_indent_size(editor
, indent
), TRUE
);
2644 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2647 gchar
*str_begin
, *str_end
, *co
, *cc
;
2650 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2652 eol
= editor_get_eol_char(editor
);
2653 co
= editor
->document
->file_type
->comment_open
;
2654 cc
= editor
->document
->file_type
->comment_close
;
2655 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2656 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2658 /* insert the comment strings */
2659 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2660 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2661 sci_insert_text(editor
->sci
, line_len
, str_end
);
2668 static void real_uncomment_multiline(GeanyEditor
*editor
)
2670 /* find the beginning of the multi line comment */
2671 gint pos
, line
, len
, x
;
2675 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2676 doc
= editor
->document
;
2678 /* remove comment open chars */
2679 pos
= document_find_text(doc
, doc
->file_type
->comment_open
, 0, TRUE
, FALSE
, NULL
);
2680 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2682 /* check whether the line is empty and can be deleted */
2683 line
= sci_get_line_from_position(editor
->sci
, pos
);
2684 len
= sci_get_line_length(editor
->sci
, line
);
2685 linebuf
= sci_get_line(editor
->sci
, line
);
2687 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2688 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2691 /* remove comment close chars */
2692 pos
= document_find_text(doc
, doc
->file_type
->comment_close
, 0, FALSE
, FALSE
, NULL
);
2693 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2695 /* check whether the line is empty and can be deleted */
2696 line
= sci_get_line_from_position(editor
->sci
, pos
);
2697 len
= sci_get_line_length(editor
->sci
, line
);
2698 linebuf
= sci_get_line(editor
->sci
, line
);
2700 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2701 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2706 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2708 gint lexer
= sci_get_lexer(editor
->sci
);
2711 /* List only those lexers which support multi line comments */
2717 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2718 style_comment
= SCE_HPHP_COMMENT
;
2720 style_comment
= SCE_H_COMMENT
;
2723 case SCLEX_HASKELL
: style_comment
= SCE_HA_COMMENTBLOCK
; break;
2724 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2725 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2726 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2727 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2728 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2729 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2730 default: style_comment
= SCE_C_COMMENT
;
2733 return style_comment
;
2737 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2738 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2739 * it returns just 1 */
2740 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2742 gint first_line
, last_line
;
2743 gint x
, i
, line_start
, line_len
;
2744 gint sel_start
, sel_end
;
2747 gchar sel
[256], *co
, *cc
;
2748 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2751 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
2754 { /* use selection or current line */
2755 sel_start
= sci_get_selection_start(editor
->sci
);
2756 sel_end
= sci_get_selection_end(editor
->sci
);
2758 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
2759 /* Find the last line with chars selected (not EOL char) */
2760 last_line
= sci_get_line_from_position(editor
->sci
,
2761 sel_end
- editor_get_eol_char_len(editor
));
2762 last_line
= MAX(first_line
, last_line
);
2766 first_line
= last_line
= line
;
2767 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
2770 ft
= editor
->document
->file_type
;
2772 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
2773 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
2774 if (ft
->id
== GEANY_FILETYPES_PHP
)
2776 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2777 ft
= filetypes
[GEANY_FILETYPES_XML
];
2780 co
= ft
->comment_open
;
2781 cc
= ft
->comment_close
;
2785 co_len
= strlen(co
);
2789 sci_start_undo_action(editor
->sci
);
2791 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
2795 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2796 line_len
= sci_get_line_length(editor
->sci
, i
);
2799 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
2802 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2803 sel
[buf_len
] = '\0';
2805 while (isspace(sel
[x
])) x
++;
2807 /* to skip blank lines */
2808 if (x
< line_len
&& sel
[x
] != '\0')
2810 /* use single line comment */
2811 if (cc
== NULL
|| strlen(cc
) == 0)
2817 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2818 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
2819 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
2826 if (strncmp(sel
+ x
, co
, co_len
) != 0)
2830 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
2831 sci_replace_sel(editor
->sci
, "");
2834 /* use multi line comment */
2839 /* skip lines which are already comments */
2840 style_comment
= get_multiline_comment_style(editor
, line_start
);
2841 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
2843 real_uncomment_multiline(editor
);
2847 /* break because we are already on the last line */
2853 sci_end_undo_action(editor
->sci
);
2855 /* restore selection if there is one
2856 * but don't touch the selection if caller is editor_do_comment_toggle */
2857 if (! toggle
&& sel_start
< sel_end
)
2861 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
2862 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
2866 gint eol_len
= editor_get_eol_char_len(editor
);
2867 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
2868 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
2876 void editor_do_comment_toggle(GeanyEditor
*editor
)
2878 gint first_line
, last_line
;
2879 gint x
, i
, line_start
, line_len
, first_line_start
;
2880 gint sel_start
, sel_end
;
2881 gint count_commented
= 0, count_uncommented
= 0;
2882 gchar sel
[256], *co
, *cc
;
2883 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2884 gboolean first_line_was_comment
= FALSE
;
2886 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2889 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2891 sel_start
= sci_get_selection_start(editor
->sci
);
2892 sel_end
= sci_get_selection_end(editor
->sci
);
2894 ft
= editor
->document
->file_type
;
2896 first_line
= sci_get_line_from_position(editor
->sci
,
2897 sci_get_selection_start(editor
->sci
));
2898 /* Find the last line with chars selected (not EOL char) */
2899 last_line
= sci_get_line_from_position(editor
->sci
,
2900 sci_get_selection_end(editor
->sci
) - editor_get_eol_char_len(editor
));
2901 last_line
= MAX(first_line
, last_line
);
2903 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
2904 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
2905 if (ft
->id
== GEANY_FILETYPES_PHP
)
2907 if (! is_style_php(sci_get_style_at(editor
->sci
, first_line_start
)))
2908 ft
= filetypes
[GEANY_FILETYPES_XML
];
2911 co
= ft
->comment_open
;
2912 cc
= ft
->comment_close
;
2916 co_len
= strlen(co
);
2920 sci_start_undo_action(editor
->sci
);
2922 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
2926 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2927 line_len
= sci_get_line_length(editor
->sci
, i
);
2930 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
2933 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2934 sel
[buf_len
] = '\0';
2936 while (isspace(sel
[x
])) x
++;
2938 /* use single line comment */
2939 if (cc
== NULL
|| strlen(cc
) == 0)
2941 gboolean do_continue
= FALSE
;
2944 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
2945 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
2950 if (do_continue
&& i
== first_line
)
2951 first_line_was_comment
= TRUE
;
2955 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
2959 /* we are still here, so the above lines were not already comments, so comment it */
2960 editor_do_comment(editor
, i
, TRUE
, TRUE
);
2963 /* use multi line comment */
2968 /* skip lines which are already comments */
2969 style_comment
= get_multiline_comment_style(editor
, line_start
);
2970 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
2972 real_uncomment_multiline(editor
);
2973 count_uncommented
++;
2977 real_comment_multiline(editor
, line_start
, last_line
);
2981 /* break because we are already on the last line */
2987 sci_end_undo_action(editor
->sci
);
2991 /* restore selection if there is one */
2992 if (sel_start
< sel_end
)
2996 gint a
= (first_line_was_comment
) ? - co_len
: co_len
;
2998 /* don't modify sel_start when the selection starts within indentation */
2999 read_indent(editor
, sel_start
);
3000 if ((sel_start
- first_line_start
) <= (gint
) strlen(indent
))
3003 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
3004 sci_set_selection_end(editor
->sci
, sel_end
+
3005 (count_commented
* co_len
) - (count_uncommented
* co_len
));
3009 gint eol_len
= editor_get_eol_char_len(editor
);
3010 if (count_uncommented
> 0)
3012 sci_set_selection_start(editor
->sci
, sel_start
- co_len
+ eol_len
);
3013 sci_set_selection_end(editor
->sci
, sel_end
- co_len
+ eol_len
);
3015 else if (count_commented
> 0)
3017 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
- eol_len
);
3018 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
- eol_len
);
3022 else if (count_uncommented
> 0)
3024 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
3025 sci_set_current_position(editor
->sci
, sel_start
- co_len
+ eol_len
, TRUE
);
3027 else if (count_commented
> 0)
3029 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
3030 sci_set_current_position(editor
->sci
, sel_start
+ co_len
- eol_len
, TRUE
);
3035 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
3036 void editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
)
3038 gint first_line
, last_line
;
3039 gint x
, i
, line_start
, line_len
;
3040 gint sel_start
, sel_end
, co_len
;
3041 gchar sel
[256], *co
, *cc
;
3042 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3045 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3048 { /* use selection or current line */
3049 sel_start
= sci_get_selection_start(editor
->sci
);
3050 sel_end
= sci_get_selection_end(editor
->sci
);
3052 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3053 /* Find the last line with chars selected (not EOL char) */
3054 last_line
= sci_get_line_from_position(editor
->sci
,
3055 sel_end
- editor_get_eol_char_len(editor
));
3056 last_line
= MAX(first_line
, last_line
);
3060 first_line
= last_line
= line
;
3061 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3064 ft
= editor
->document
->file_type
;
3066 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
3067 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3068 if (ft
->id
== GEANY_FILETYPES_PHP
)
3070 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
3071 ft
= filetypes
[GEANY_FILETYPES_XML
];
3074 co
= ft
->comment_open
;
3075 cc
= ft
->comment_close
;
3079 co_len
= strlen(co
);
3083 sci_start_undo_action(editor
->sci
);
3085 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3089 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3090 line_len
= sci_get_line_length(editor
->sci
, i
);
3093 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
3096 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3097 sel
[buf_len
] = '\0';
3099 while (isspace(sel
[x
])) x
++;
3101 /* to skip blank lines */
3102 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3104 /* use single line comment */
3105 if (cc
== NULL
|| strlen(cc
) == 0)
3107 gint start
= line_start
;
3110 if (ft
->comment_use_indent
)
3111 start
= line_start
+ x
;
3115 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3116 sci_insert_text(editor
->sci
, start
, text
);
3120 sci_insert_text(editor
->sci
, start
, co
);
3122 /* use multi line comment */
3127 /* skip lines which are already comments */
3128 style_comment
= get_multiline_comment_style(editor
, line_start
);
3129 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3132 real_comment_multiline(editor
, line_start
, last_line
);
3134 /* break because we are already on the last line */
3140 sci_end_undo_action(editor
->sci
);
3142 /* restore selection if there is one
3143 * but don't touch the selection if caller is editor_do_comment_toggle */
3144 if (! toggle
&& sel_start
< sel_end
)
3148 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3149 sci_set_selection_end(editor
->sci
, sel_end
+ ((i
- first_line
) * co_len
));
3153 gint eol_len
= editor_get_eol_char_len(editor
);
3154 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3155 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3161 static gboolean brace_timeout_active
= FALSE
;
3163 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3165 GeanyDocument
*doc
= document_get_current();
3166 GeanyEditor
*editor
;
3167 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3168 gint end_pos
, cur_pos
;
3170 brace_timeout_active
= FALSE
;
3174 editor
= doc
->editor
;
3175 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3177 if (cur_pos
!= brace_pos
)
3180 if (cur_pos
!= brace_pos
)
3182 /* we have moved past the original brace_pos, but after the timeout
3183 * we may now be on a new brace, so check again */
3184 editor_highlight_braces(editor
, cur_pos
);
3188 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3190 editor_highlight_braces(editor
, cur_pos
);
3193 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3197 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3198 sci_get_col_from_position(editor
->sci
, end_pos
));
3199 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3200 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3204 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3205 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3211 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3213 gint brace_pos
= cur_pos
- 1;
3215 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3216 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3218 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3221 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3226 if (!brace_timeout_active
)
3228 brace_timeout_active
= TRUE
;
3229 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3230 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3235 static gboolean
in_block_comment(gint lexer
, gint style
)
3240 return (style
== SCE_C_COMMENT
||
3241 style
== SCE_C_COMMENTDOC
);
3244 return (style
== SCE_PAS_COMMENT
||
3245 style
== SCE_PAS_COMMENT2
);
3248 return (style
== SCE_D_COMMENT
||
3249 style
== SCE_D_COMMENTDOC
||
3250 style
== SCE_D_COMMENTNESTED
);
3253 return (style
== SCE_HPHP_COMMENT
);
3256 return (style
== SCE_CSS_COMMENT
);
3264 static gboolean
is_comment_char(gchar c
, gint lexer
)
3266 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3276 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3278 ScintillaObject
*sci
= editor
->sci
;
3279 gint indent_pos
, style
;
3280 gint lexer
= sci_get_lexer(sci
);
3282 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3283 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3284 style
= sci_get_style_at(sci
, indent_pos
);
3285 if (!in_block_comment(lexer
, style
))
3288 /* Check whether the comment block continues on this line */
3289 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3290 if (sci_get_style_at(sci
, indent_pos
) == style
)
3292 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3293 /* the type of comment, '*' (C/C++/Java), '+' and the others (D) */
3294 gchar
*continuation
= "*";
3295 gchar
*whitespace
= ""; /* to hold whitespace if needed */
3297 gint len
= strlen(previous_line
);
3300 /* find and stop at end of multi line comment */
3302 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3303 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3305 gint indent_len
, indent_width
;
3307 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3308 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3309 indent_width
= editor_get_indent_prefs(editor
)->width
;
3311 /* if there is one too many spaces, delete the last space,
3312 * to return to the indent used before the multiline comment was started. */
3313 if (indent_len
% indent_width
== 1)
3314 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3315 g_free(previous_line
);
3318 /* check whether we are on the second line of multi line comment */
3320 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3323 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3324 { /* we are on the second line of a multi line comment, so we have to insert white space */
3328 if (G_UNLIKELY(style
== SCE_D_COMMENTNESTED
))
3329 continuation
= "+"; /* for nested comments in D */
3331 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3332 sci_add_text(sci
, result
);
3335 g_free(previous_line
);
3340 /* Checks whether the given style is a string for the given lexer.
3341 * It doesn't handle LEX_HTML, this should be done by the caller.
3342 * Returns true if the style is a string, FALSE otherwise.
3344 * Don't forget STRINGEOL, to prevent completion whilst typing a string with no closing char.
3346 static gboolean
is_string_style(gint lexer
, gint style
)
3351 return (style
== SCE_C_CHARACTER
||
3352 style
== SCE_C_STRING
||
3353 style
== SCE_C_STRINGEOL
);
3356 return (style
== SCE_PAS_CHARACTER
||
3357 style
== SCE_PAS_STRING
||
3358 style
== SCE_PAS_STRINGEOL
);
3361 return (style
== SCE_D_STRING
||
3362 style
== SCE_D_STRINGEOL
||
3363 style
== SCE_D_CHARACTER
||
3364 style
== SCE_D_STRINGB
||
3365 style
== SCE_D_STRINGR
);
3368 return (style
== SCE_P_STRING
||
3369 style
== SCE_P_TRIPLE
||
3370 style
== SCE_P_TRIPLEDOUBLE
||
3371 style
== SCE_P_CHARACTER
||
3372 style
== SCE_P_STRINGEOL
);
3376 return (style
== SCE_F_STRING1
||
3377 style
== SCE_F_STRING2
||
3378 style
== SCE_F_STRINGEOL
);
3381 return (/*style == SCE_PL_STRING ||*/ /* may want variable autocompletion "$(foo)" */
3382 style
== SCE_PL_CHARACTER
||
3383 style
== SCE_PL_HERE_DELIM
||
3384 style
== SCE_PL_HERE_Q
||
3385 style
== SCE_PL_HERE_QQ
||
3386 style
== SCE_PL_HERE_QX
||
3387 style
== SCE_PL_POD
||
3388 style
== SCE_PL_STRING_Q
||
3389 style
== SCE_PL_STRING_QQ
||
3390 style
== SCE_PL_STRING_QX
||
3391 style
== SCE_PL_STRING_QR
||
3392 style
== SCE_PL_STRING_QW
||
3393 style
== SCE_PL_POD_VERB
);
3396 return (style
== SCE_R_STRING
);
3399 return (style
== SCE_RB_CHARACTER
||
3400 style
== SCE_RB_STRING
||
3401 style
== SCE_RB_HERE_DELIM
||
3402 style
== SCE_RB_HERE_Q
||
3403 style
== SCE_RB_HERE_QQ
||
3404 style
== SCE_RB_HERE_QX
||
3405 style
== SCE_RB_POD
);
3408 return (style
== SCE_SH_STRING
);
3411 return (style
== SCE_SQL_STRING
);
3414 return (style
== SCE_TCL_IN_QUOTE
);
3417 return (style
== SCE_LUA_LITERALSTRING
||
3418 style
== SCE_LUA_CHARACTER
||
3419 style
== SCE_LUA_STRINGEOL
||
3420 style
== SCE_LUA_STRING
);
3423 return (style
== SCE_HA_CHARACTER
||
3424 style
== SCE_HA_STRING
);
3426 case SCLEX_FREEBASIC
:
3427 return (style
== SCE_B_STRING
||
3428 style
== SCE_B_STRINGEOL
);
3431 return (style
== SCE_MATLAB_STRING
||
3432 style
== SCE_MATLAB_DOUBLEQUOTESTRING
);
3436 style
== SCE_HBA_STRING
||
3437 style
== SCE_HBA_STRINGEOL
||
3438 style
== SCE_HB_STRING
||
3439 style
== SCE_HB_STRINGEOL
||
3440 style
== SCE_H_CDATA
||
3441 style
== SCE_H_DOUBLESTRING
||
3442 style
== SCE_HJA_DOUBLESTRING
||
3443 style
== SCE_HJA_SINGLESTRING
||
3444 style
== SCE_HJA_STRINGEOL
||
3445 style
== SCE_HJ_DOUBLESTRING
||
3446 style
== SCE_HJ_SINGLESTRING
||
3447 style
== SCE_HJ_STRINGEOL
||
3448 style
== SCE_HPA_CHARACTER
||
3449 style
== SCE_HPA_STRING
||
3450 style
== SCE_HPA_TRIPLE
||
3451 style
== SCE_HPA_TRIPLEDOUBLE
||
3452 style
== SCE_HP_CHARACTER
||
3453 style
== SCE_HPHP_HSTRING
||
3454 style
== SCE_HPHP_HSTRING
|| /* HSTRING is a heredoc */
3455 style
== SCE_HPHP_HSTRING_VARIABLE
||
3456 style
== SCE_HPHP_SIMPLESTRING
||
3457 style
== SCE_HPHP_SIMPLESTRING
||
3458 style
== SCE_HP_STRING
||
3459 style
== SCE_HP_TRIPLE
||
3460 style
== SCE_HP_TRIPLEDOUBLE
||
3461 style
== SCE_H_SGML_DOUBLESTRING
||
3462 style
== SCE_H_SGML_SIMPLESTRING
||
3463 style
== SCE_H_SINGLESTRING
);
3466 return (style
== SCE_CMAKE_STRINGDQ
||
3467 style
== SCE_CMAKE_STRINGLQ
||
3468 style
== SCE_CMAKE_STRINGRQ
||
3469 style
== SCE_CMAKE_STRINGVAR
);
3472 return (style
== SCE_NSIS_STRINGDQ
||
3473 style
== SCE_NSIS_STRINGLQ
||
3474 style
== SCE_NSIS_STRINGRQ
||
3475 style
== SCE_NSIS_STRINGVAR
);
3478 return (style
== SCE_ADA_CHARACTER
||
3479 style
== SCE_ADA_STRING
||
3480 style
== SCE_ADA_CHARACTEREOL
||
3481 style
== SCE_ADA_STRINGEOL
);
3487 /* Checks whether the given style is a comment for the given lexer.
3488 * It doesn't handle LEX_HTML, this should be done by the caller.
3489 * Returns true if the style is a comment, FALSE otherwise.
3491 static gboolean
is_comment_style(gint lexer
, gint style
)
3496 return (style
== SCE_C_COMMENT
||
3497 style
== SCE_C_COMMENTLINE
||
3498 style
== SCE_C_COMMENTDOC
||
3499 style
== SCE_C_COMMENTLINEDOC
||
3500 style
== SCE_C_COMMENTDOCKEYWORD
||
3501 style
== SCE_C_COMMENTDOCKEYWORDERROR
);
3504 return (style
== SCE_PAS_COMMENT
||
3505 style
== SCE_PAS_COMMENT2
||
3506 style
== SCE_PAS_COMMENTLINE
);
3509 return (style
== SCE_D_COMMENT
||
3510 style
== SCE_D_COMMENTLINE
||
3511 style
== SCE_D_COMMENTDOC
||
3512 style
== SCE_D_COMMENTNESTED
||
3513 style
== SCE_D_COMMENTLINEDOC
||
3514 style
== SCE_D_COMMENTDOCKEYWORD
||
3515 style
== SCE_D_COMMENTDOCKEYWORDERROR
);
3518 return (style
== SCE_P_COMMENTLINE
||
3519 style
== SCE_P_COMMENTBLOCK
);
3523 return (style
== SCE_F_COMMENT
);
3526 return (style
== SCE_PL_COMMENTLINE
);
3528 case SCLEX_PROPERTIES
:
3529 return (style
== SCE_PROPS_COMMENT
);
3532 return (style
== SCE_PO_COMMENT
);
3535 return (style
== SCE_L_COMMENT
);
3537 case SCLEX_MAKEFILE
:
3538 return (style
== SCE_MAKE_COMMENT
);
3541 return (style
== SCE_RB_COMMENTLINE
);
3544 return (style
== SCE_SH_COMMENTLINE
);
3547 return (style
== SCE_R_COMMENT
);
3550 return (style
== SCE_SQL_COMMENT
||
3551 style
== SCE_SQL_COMMENTLINE
||
3552 style
== SCE_SQL_COMMENTDOC
);
3555 return (style
== SCE_TCL_COMMENT
||
3556 style
== SCE_TCL_COMMENTLINE
||
3557 style
== SCE_TCL_COMMENT_BOX
||
3558 style
== SCE_TCL_BLOCK_COMMENT
);
3561 return (style
== SCE_MATLAB_COMMENT
);
3564 return (style
== SCE_LUA_COMMENT
||
3565 style
== SCE_LUA_COMMENTLINE
||
3566 style
== SCE_LUA_COMMENTDOC
);
3569 return (style
== SCE_HA_COMMENTLINE
||
3570 style
== SCE_HA_COMMENTBLOCK
||
3571 style
== SCE_HA_COMMENTBLOCK2
||
3572 style
== SCE_HA_COMMENTBLOCK3
);
3574 case SCLEX_FREEBASIC
:
3575 return (style
== SCE_B_COMMENT
);
3578 return (style
== SCE_YAML_COMMENT
);
3582 style
== SCE_HBA_COMMENTLINE
||
3583 style
== SCE_HB_COMMENTLINE
||
3584 style
== SCE_H_COMMENT
||
3585 style
== SCE_HJA_COMMENT
||
3586 style
== SCE_HJA_COMMENTDOC
||
3587 style
== SCE_HJA_COMMENTLINE
||
3588 style
== SCE_HJ_COMMENT
||
3589 style
== SCE_HJ_COMMENTDOC
||
3590 style
== SCE_HJ_COMMENTLINE
||
3591 style
== SCE_HPA_COMMENTLINE
||
3592 style
== SCE_HP_COMMENTLINE
||
3593 style
== SCE_HPHP_COMMENT
||
3594 style
== SCE_HPHP_COMMENTLINE
||
3595 style
== SCE_H_SGML_COMMENT
);
3598 return (style
== SCE_CMAKE_COMMENT
);
3601 return (style
== SCE_NSIS_COMMENT
||
3602 style
== SCE_NSIS_COMMENTBOX
);
3605 return (style
== SCE_ADA_COMMENTLINE
||
3606 style
== SCE_NSIS_COMMENTBOX
);
3612 /* Checks whether the given style is normal code (not string, comment, preprocessor, etc).
3613 * It doesn't handle LEX_HTML, this should be done by the caller.
3615 static gboolean
is_code_style(gint lexer
, gint style
)
3620 if (style
== SCE_C_PREPROCESSOR
)
3624 return !(is_comment_style(lexer
, style
) ||
3625 is_string_style(lexer
, style
));
3630 static gboolean
editor_lexer_is_c_like(gint lexer
)
3645 /* Returns: -1 if lexer doesn't support type keywords */
3646 gint
editor_lexer_get_type_keyword_idx(gint lexer
)
3660 /* inserts a three-line comment at one line above current cursor position */
3661 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3667 gboolean have_multiline_comment
= FALSE
;
3670 g_return_if_fail(editor
!= NULL
&&
3671 editor
->document
->file_type
!= NULL
&& editor
->document
->file_type
->comment_open
!= NULL
);
3673 sci_start_undo_action(editor
->sci
);
3675 doc
= editor
->document
;
3677 if (doc
->file_type
->comment_close
!= NULL
&& strlen(doc
->file_type
->comment_close
) > 0)
3678 have_multiline_comment
= TRUE
;
3680 /* insert three lines one line above of the current position */
3681 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3682 pos
= sci_get_position_from_line(editor
->sci
, line
);
3684 /* use the indent on the current line but only when comment indentation is used
3685 * and we don't have multi line comment characters */
3686 if (editor
->auto_indent
&&
3687 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3689 read_indent(editor
, editor_info
.click_pos
);
3690 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3691 text_len
= strlen(text
);
3695 text
= g_strdup("\n\n\n");
3698 sci_insert_text(editor
->sci
, pos
, text
);
3701 /* select the inserted lines for commenting */
3702 sci_set_selection_start(editor
->sci
, pos
);
3703 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3705 editor_do_comment(editor
, -1, TRUE
, FALSE
);
3707 /* set the current position to the start of the first inserted line */
3708 pos
+= strlen(doc
->file_type
->comment_open
);
3710 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3711 if (have_multiline_comment
)
3714 pos
+= strlen(indent
);
3716 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3717 /* reset the selection */
3718 sci_set_anchor(editor
->sci
, pos
);
3720 sci_end_undo_action(editor
->sci
);
3724 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3725 * Scroll the view to make line appear at percent_of_view.
3726 * line can be -1 to use the current position. */
3727 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3729 gint vis1
, los
, delta
;
3732 g_return_if_fail(editor
!= NULL
);
3734 wid
= GTK_WIDGET(editor
->sci
);
3736 if (! wid
->window
|| ! gdk_window_is_viewable(wid
->window
))
3737 return; /* prevent gdk_window_scroll warning */
3740 line
= sci_get_current_line(editor
->sci
);
3742 /* sci 'visible line' != doc line number because of folding and line wrapping */
3743 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3744 * SCI_DOCLINEFROMVISIBLE for vis1. */
3745 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3746 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
3747 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3748 delta
= (line
- vis1
) - los
* percent_of_view
;
3749 sci_scroll_lines(editor
->sci
, delta
);
3750 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3754 /* creates and inserts one tab or whitespace of the amount of the tab width */
3755 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3758 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3760 g_return_if_fail(editor
!= NULL
);
3762 switch (iprefs
.type
)
3764 case GEANY_INDENT_TYPE_TABS
:
3765 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3767 case GEANY_INDENT_TYPE_SPACES
:
3768 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3769 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3772 text
= get_whitespace(&iprefs
, iprefs
.width
);
3773 sci_add_text(editor
->sci
, text
);
3778 void editor_select_word(GeanyEditor
*editor
)
3784 g_return_if_fail(editor
!= NULL
);
3786 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3787 start
= SSM(editor
->sci
, SCI_WORDSTARTPOSITION
, pos
, TRUE
);
3788 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, TRUE
);
3790 if (start
== end
) /* caret in whitespaces sequence */
3792 /* look forward but reverse the selection direction,
3793 * so the caret end up stay as near as the original position. */
3794 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, FALSE
);
3795 start
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, end
, TRUE
);
3800 sci_set_selection(editor
->sci
, start
, end
);
3804 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3805 * when those lines have no selection (cursor at start of line). */
3806 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3808 gint start
, end
, line
;
3810 g_return_if_fail(editor
!= NULL
);
3812 start
= sci_get_selection_start(editor
->sci
);
3813 end
= sci_get_selection_end(editor
->sci
);
3815 /* check if whole lines are already selected */
3816 if (! extra_line
&& start
!= end
&&
3817 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3818 sci_get_col_from_position(editor
->sci
, end
) == 0)
3821 line
= sci_get_line_from_position(editor
->sci
, start
);
3822 start
= sci_get_position_from_line(editor
->sci
, line
);
3824 line
= sci_get_line_from_position(editor
->sci
, end
);
3825 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3827 sci_set_selection(editor
->sci
, start
, end
);
3831 /* find the start or end of a paragraph by searching all lines in direction (UP or DOWN)
3832 * starting at the given line and return the found line or return -1 if called on an empty line */
3833 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3835 gboolean found_end
= FALSE
;
3837 gchar
*line_buf
, *x
;
3838 ScintillaObject
*sci
= editor
->sci
;
3840 /* first check current line and return -1 if it is empty to skip creating of a selection */
3841 line_buf
= x
= sci_get_line(sci
, line
);
3850 if (direction
== GTK_DIR_UP
)
3859 /* sci_get_line checks for sanity of the given line, sci_get_line always return a string
3860 * containing at least '\0' so no need to check for NULL */
3861 line_buf
= x
= sci_get_line(sci
, line
);
3863 /* check whether after skipping all whitespace we are at end of line and if so, assume
3864 * this line as end of paragraph */
3871 /* called on the first line but there is no previous line so return line 0 */
3880 void editor_select_paragraph(GeanyEditor
*editor
)
3882 gint pos_start
, pos_end
, line_start
, line_found
;
3884 g_return_if_fail(editor
!= NULL
);
3886 line_start
= SSM(editor
->sci
, SCI_LINEFROMPOSITION
,
3887 SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0), 0);
3889 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3890 if (line_found
== -1)
3893 /* find_paragraph_stop returns the emtpy line(previous to the real start of the paragraph),
3894 * so use the next line for selection start */
3898 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3900 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3901 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3903 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3907 /* simple indentation to indent the current line with the same indent as the previous one */
3908 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
3910 gint i
, sel_start
= 0, sel_end
= 0;
3912 /* get previous line and use it for read_indent to use that line
3913 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
3914 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
3916 for (i
= first_line
; i
<= last_line
; i
++)
3918 /* skip the first line or if the indentation of the previous and current line are equal */
3920 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
3921 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
3924 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3925 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3926 if (sel_start
< sel_end
)
3928 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
3929 sci_replace_sel(editor
->sci
, "");
3931 sci_insert_text(editor
->sci
, sel_start
, indent
);
3936 /* simple indentation to indent the current line with the same indent as the previous one */
3937 void editor_smart_line_indentation(GeanyEditor
*editor
, gint pos
)
3939 gint first_line
, last_line
;
3940 gint first_sel_start
, first_sel_end
;
3941 ScintillaObject
*sci
;
3943 g_return_if_fail(editor
!= NULL
);
3947 first_sel_start
= sci_get_selection_start(sci
);
3948 first_sel_end
= sci_get_selection_end(sci
);
3950 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
3951 /* Find the last line with chars selected (not EOL char) */
3952 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
3953 last_line
= MAX(first_line
, last_line
);
3956 pos
= first_sel_start
;
3958 sci_start_undo_action(sci
);
3960 smart_line_indentation(editor
, first_line
, last_line
);
3962 /* set cursor position if there was no selection */
3963 if (first_sel_start
== first_sel_end
)
3965 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
3967 /* use indent position as user may wish to change indentation afterwards */
3968 sci_set_current_position(sci
, indent_pos
, FALSE
);
3972 /* fully select all the lines affected */
3973 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
3974 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
3977 sci_end_undo_action(sci
);
3981 /* increase / decrease current line or selection by one space */
3982 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
3984 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
3985 gint sel_start
, sel_end
, first_line_offset
= 0;
3987 g_return_if_fail(editor
!= NULL
);
3989 sel_start
= sci_get_selection_start(editor
->sci
);
3990 sel_end
= sci_get_selection_end(editor
->sci
);
3992 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3993 /* Find the last line with chars selected (not EOL char) */
3994 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
3995 last_line
= MAX(first_line
, last_line
);
4000 sci_start_undo_action(editor
->sci
);
4002 for (i
= first_line
; i
<= last_line
; i
++)
4004 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
4007 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
4008 /* searching backwards for a space to remove */
4009 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
4012 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
4014 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
4015 sci_replace_sel(editor
->sci
, "");
4017 if (i
== first_line
)
4018 first_line_offset
= -1;
4023 sci_insert_text(editor
->sci
, indentation_end
, " ");
4025 if (i
== first_line
)
4026 first_line_offset
= 1;
4030 /* set cursor position */
4031 if (sel_start
< sel_end
)
4033 gint start
= sel_start
+ first_line_offset
;
4034 if (first_line_offset
< 0)
4035 start
= MAX(sel_start
+ first_line_offset
,
4036 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
4038 sci_set_selection_start(editor
->sci
, start
);
4039 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
4042 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
4044 sci_end_undo_action(editor
->sci
);
4048 void editor_finalize()
4050 scintilla_release_resources();
4054 /* wordchars: NULL or a string containing characters to match a word.
4055 * Returns: the current selection or the current word. */
4056 gchar
*editor_get_default_selection(GeanyEditor
*editor
, gboolean use_current_word
,
4057 const gchar
*wordchars
)
4061 g_return_val_if_fail(editor
!= NULL
, NULL
);
4063 if (sci_get_lines_selected(editor
->sci
) == 1)
4065 gint len
= sci_get_selected_text_length(editor
->sci
);
4067 s
= g_malloc(len
+ 1);
4068 sci_get_selected_text(editor
->sci
, s
);
4070 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
4071 { /* use the word at current cursor position */
4072 gchar word
[GEANY_MAX_WORD_LENGTH
];
4074 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
4075 if (word
[0] != '\0')
4082 /* Note: Usually the line should be made visible (not folded) before calling this.
4083 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4084 * outside the *vertical* view.
4085 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4086 * sci_scroll_caret() when this returns TRUE. */
4087 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4091 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4093 /* If line is wrapped the result may occur on another virtual line than the first and may be
4094 * still hidden, so increase the line number to check for the next document line */
4095 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4098 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4099 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4100 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4102 return (line
>= vis1
&& line
< vis1
+ los
);
4106 /* If the current line is outside the current view window, scroll the line
4107 * so it appears at percent_of_view. */
4108 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4112 g_return_if_fail(editor
!= NULL
);
4114 line
= sci_get_current_line(editor
->sci
);
4116 /* unfold maybe folded results */
4117 sci_ensure_line_is_visible(editor
->sci
, line
);
4119 /* scroll the line if it's off screen */
4120 if (! editor_line_in_view(editor
, line
))
4121 editor
->scroll_percent
= percent_of_view
;
4123 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4128 * Deletes all currently set indicators in the @a editor window.
4129 * Error indicators (red squiggly underlines) and usual line markers are removed.
4131 * @param editor The editor to operate on.
4133 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4135 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4136 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4141 * Deletes all currently set indicators matching @a indic in the @a editor window.
4143 * @param editor The editor to operate on.
4144 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4148 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4152 g_return_if_fail(editor
!= NULL
);
4154 last_pos
= sci_get_length(editor
->sci
);
4157 sci_indicator_set(editor
->sci
, indic
);
4158 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4164 * Sets an indicator @a indic on @a line.
4165 * Whitespace at the start and the end of the line is not marked.
4167 * @param editor The editor to operate on.
4168 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4169 * @param line The line number which should be marked.
4173 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4179 g_return_if_fail(editor
!= NULL
);
4180 g_return_if_fail(line
>= 0);
4182 start
= sci_get_position_from_line(editor
->sci
, line
);
4183 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4185 /* skip blank lines */
4186 if ((start
+ 1) == end
||
4188 sci_get_line_length(editor
->sci
, line
) == editor_get_eol_char_len(editor
))
4194 linebuf
= sci_get_line(editor
->sci
, line
);
4196 /* don't set the indicator on whitespace */
4197 while (isspace(linebuf
[i
]))
4199 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4206 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4211 * Sets an indicator on the range specified by @a start and @a end.
4212 * No error checking or whitespace removal is performed, this should be done by the calling
4213 * function if necessary.
4215 * @param editor The editor to operate on.
4216 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4217 * @param start The starting position for the marker.
4218 * @param end The ending position for the marker.
4222 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4224 g_return_if_fail(editor
!= NULL
);
4228 sci_indicator_set(editor
->sci
, indic
);
4229 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4233 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4234 * the replacement will also start with 0x... */
4235 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4237 g_return_if_fail(editor
!= NULL
);
4239 if (sci_has_selection(editor
->sci
))
4241 gint start
= sci_get_selection_start(editor
->sci
);
4242 const gchar
*replacement
= colour
;
4244 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4245 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4247 sci_set_selection_start(editor
->sci
, start
+ 2);
4248 sci_set_selection_end(editor
->sci
, start
+ 8);
4249 replacement
++; /* skip the leading "0x" */
4251 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4252 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4253 replacement
++; /* so skip the '#' to only replace the colour value */
4255 sci_replace_sel(editor
->sci
, replacement
);
4258 sci_add_text(editor
->sci
, colour
);
4263 * Retrieves the localized name (for displaying) of the used end of line characters
4264 * (LF, CR/LF, CR) in the given editor.
4265 * If @a editor is @c NULL, the default end of line characters are used.
4267 * @param editor The editor to operate on, or @c NULL to query the default value.
4268 * @return The name of the end of line characters.
4272 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4274 gint mode
= file_prefs
.default_eol_character
;
4277 mode
= sci_get_eol_mode(editor
->sci
);
4279 return utils_get_eol_name(mode
);
4284 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4285 * If @a editor is @c NULL, the default end of line characters are used.
4286 * The returned value is 1 for CR and LF and 2 for CR/LF.
4288 * @param editor The editor to operate on, or @c NULL to query the default value.
4289 * @return The length of the end of line characters.
4293 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4295 gint mode
= file_prefs
.default_eol_character
;
4298 mode
= sci_get_eol_mode(editor
->sci
);
4302 case SC_EOL_CRLF
: return 2; break;
4303 default: return 1; break;
4309 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4310 * If @a editor is @c NULL, the default end of line characters are used.
4311 * The returned value is either "\n", "\r\n" or "\r".
4313 * @param editor The editor to operate on, or @c NULL to query the default value.
4314 * @return The end of line characters.
4318 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4320 gint mode
= file_prefs
.default_eol_character
;
4323 mode
= sci_get_eol_mode(editor
->sci
);
4327 case SC_EOL_CRLF
: return "\r\n"; break;
4328 case SC_EOL_CR
: return "\r"; break;
4329 default: return "\n"; break;
4334 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4336 gint lines
, first
, i
;
4338 if (editor
== NULL
|| ! editor_prefs
.folding
)
4341 lines
= sci_get_line_count(editor
->sci
);
4342 first
= sci_get_first_visible_line(editor
->sci
);
4344 for (i
= 0; i
< lines
; i
++)
4346 gint level
= sci_get_fold_level(editor
->sci
, i
);
4348 if (level
& SC_FOLDLEVELHEADERFLAG
)
4350 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4351 sci_toggle_fold(editor
->sci
, i
);
4354 editor_scroll_to_line(editor
, first
, 0.0F
);
4358 void editor_unfold_all(GeanyEditor
*editor
)
4360 fold_all(editor
, FALSE
);
4364 void editor_fold_all(GeanyEditor
*editor
)
4366 fold_all(editor
, TRUE
);
4370 void editor_replace_tabs(GeanyEditor
*editor
)
4372 gint search_pos
, pos_in_line
, current_tab_true_length
;
4375 struct Sci_TextToFind ttf
;
4377 g_return_if_fail(editor
!= NULL
);
4379 sci_start_undo_action(editor
->sci
);
4380 tab_len
= sci_get_tab_width(editor
->sci
);
4382 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4383 ttf
.lpstrText
= (gchar
*) "\t";
4387 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4388 if (search_pos
== -1)
4391 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4392 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4393 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4394 sci_set_target_start(editor
->sci
, search_pos
);
4395 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4396 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4397 /* next search starts after replacement */
4398 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4399 /* update end of range now text has changed */
4400 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4403 sci_end_undo_action(editor
->sci
);
4407 /* Replaces all occurrences all spaces of the length of a given tab_width. */
4408 void editor_replace_spaces(GeanyEditor
*editor
)
4411 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4413 struct Sci_TextToFind ttf
;
4415 g_return_if_fail(editor
!= NULL
);
4417 if (tab_len_f
< 0.0)
4418 tab_len_f
= sci_get_tab_width(editor
->sci
);
4420 if (! dialogs_show_input_numeric(
4421 _("Enter Tab Width"),
4422 _("Enter the amount of spaces which should be replaced by a tab character."),
4423 &tab_len_f
, 1, 100, 1))
4427 tab_len
= (gint
) tab_len_f
;
4429 sci_start_undo_action(editor
->sci
);
4431 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4432 ttf
.lpstrText
= g_strnfill(tab_len
, ' ');
4436 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4437 if (search_pos
== -1)
4440 sci_set_target_start(editor
->sci
, search_pos
);
4441 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4442 sci_replace_target(editor
->sci
, "\t", FALSE
);
4443 ttf
.chrg
.cpMin
= search_pos
;
4444 /* update end of range now text has changed */
4445 ttf
.chrg
.cpMax
-= tab_len
- 1;
4447 sci_end_undo_action(editor
->sci
);
4448 g_free(ttf
.lpstrText
);
4452 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4454 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4455 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4456 gint i
= line_end
- 1;
4457 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4459 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4462 ch
= sci_get_char_at(editor
->sci
, i
);
4464 if (i
< (line_end
- 1))
4466 sci_set_target_start(editor
->sci
, i
+ 1);
4467 sci_set_target_end(editor
->sci
, line_end
);
4468 sci_replace_target(editor
->sci
, "", FALSE
);
4473 void editor_strip_trailing_spaces(GeanyEditor
*editor
)
4475 gint max_lines
= sci_get_line_count(editor
->sci
);
4478 sci_start_undo_action(editor
->sci
);
4480 for (line
= 0; line
< max_lines
; line
++)
4482 editor_strip_line_trailing_spaces(editor
, line
);
4484 sci_end_undo_action(editor
->sci
);
4488 void editor_ensure_final_newline(GeanyEditor
*editor
)
4490 gint max_lines
= sci_get_line_count(editor
->sci
);
4491 gboolean append_newline
= (max_lines
== 1);
4492 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4496 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4500 const gchar
*eol
= "\n";
4501 switch (sci_get_eol_mode(editor
->sci
))
4510 sci_insert_text(editor
->sci
, end_document
, eol
);
4515 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4519 PangoFontDescription
*pfd
;
4521 g_return_if_fail(editor
);
4523 pfd
= pango_font_description_from_string(font
);
4524 size
= pango_font_description_get_size(pfd
) / PANGO_SCALE
;
4525 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4526 pango_font_description_free(pfd
);
4528 for (style
= 0; style
<= 127; style
++)
4529 sci_set_font(editor
->sci
, style
, font_name
, size
);
4531 /* line number and braces */
4532 sci_set_font(editor
->sci
, STYLE_LINENUMBER
, font_name
, size
);
4533 sci_set_font(editor
->sci
, STYLE_BRACELIGHT
, font_name
, size
);
4534 sci_set_font(editor
->sci
, STYLE_BRACEBAD
, font_name
, size
);
4537 /* zoom to 100% to prevent confusion */
4538 sci_zoom_off(editor
->sci
);
4542 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4544 g_return_if_fail(editor
!= NULL
);
4546 editor
->line_wrapping
= wrap
;
4547 sci_set_lines_wrapped(editor
->sci
, wrap
);
4551 /** Sets the indent type for @a editor.
4552 * @param editor Editor.
4553 * @param type Indent type.
4557 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4559 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4560 ScintillaObject
*sci
= editor
->sci
;
4561 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4563 editor
->indent_type
= type
;
4564 sci_set_use_tabs(sci
, use_tabs
);
4566 if (type
== GEANY_INDENT_TYPE_BOTH
)
4568 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4569 if (iprefs
->hard_tab_width
!= 8)
4571 static gboolean warn
= TRUE
;
4573 ui_set_statusbar(TRUE
, _("Warning: non-standard hard tab width: %d != 8!"),
4574 iprefs
->hard_tab_width
);
4579 sci_set_tab_width(sci
, iprefs
->width
);
4581 SSM(sci
, SCI_SETINDENT
, iprefs
->width
, 0);
4583 /* remove indent spaces on backspace, if using any spaces to indent */
4584 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, type
!= GEANY_INDENT_TYPE_TABS
, 0);
4588 /* Convenience function for editor_goto_pos() to pass in a line number. */
4589 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line_no
, gint offset
)
4593 g_return_val_if_fail(editor
, FALSE
);
4594 if (line_no
< 0 || line_no
>= sci_get_line_count(editor
->sci
))
4599 gint current_line
= sci_get_current_line(editor
->sci
);
4601 line_no
= current_line
+ line_no
;
4604 pos
= sci_get_position_from_line(editor
->sci
, line_no
);
4605 return editor_goto_pos(editor
, pos
, TRUE
);
4609 /* Move to position @a pos, switching to the document if necessary,
4610 * setting a marker if @a mark is set. */
4611 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4615 g_return_val_if_fail(editor
, FALSE
);
4616 if (G_UNLIKELY(pos
< 0))
4621 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4623 /* mark the tag with the yellow arrow */
4624 sci_marker_delete_all(editor
->sci
, 0);
4625 sci_set_marker_at_line(editor
->sci
, line
, 0);
4628 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4629 editor
->scroll_percent
= 0.25F
;
4631 /* finally switch to the page */
4632 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets
.notebook
), GTK_WIDGET(editor
->sci
));
4633 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), page_num
);
4640 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4642 GeanyEditor
*editor
= user_data
;
4644 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4645 * few lines only, maybe this could/should be done in Scintilla directly */
4646 if (event
->state
& GDK_MOD1_MASK
)
4648 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4651 else if (event
->state
& GDK_SHIFT_MASK
)
4653 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4655 sci_scroll_columns(editor
->sci
, amount
);
4659 return FALSE
; /* let Scintilla handle all other cases */
4663 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4665 GeanyDocument
*doc
= editor
->document
;
4667 if (!doc
->priv
->colourise_needed
)
4670 doc
->priv
->colourise_needed
= FALSE
;
4671 sci_colourise(editor
->sci
, 0, -1);
4673 /* now that the current document is colourised, fold points are now accurate,
4674 * so force an update of the current function/tag. */
4675 symbols_get_current_function(NULL
, NULL
);
4676 ui_update_statusbar(NULL
, -1);
4682 /* We only want to colourise just before drawing, to save startup time and
4683 * prevent unnecessary recolouring other documents after one is saved.
4684 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4685 * and "show" doesn't work). */
4686 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4688 GeanyEditor
*editor
= user_data
;
4690 editor_check_colourise(editor
);
4695 static gboolean
on_editor_expose_event(GtkWidget
*widget
, GdkEventExpose
*event
,
4698 GeanyEditor
*editor
= user_data
;
4700 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4701 * for some reason, maybe it's not necessary but just in case. */
4702 editor_check_colourise(editor
);
4707 static void setup_sci_keys(ScintillaObject
*sci
)
4709 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4710 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4711 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4712 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4713 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4714 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4715 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4716 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4717 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4718 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4719 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4720 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4721 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4722 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4723 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4725 if (editor_prefs
.use_gtk_word_boundaries
)
4727 /* use GtkEntry-like word boundaries */
4728 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4729 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4730 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4732 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4733 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4734 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4735 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4736 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4737 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4739 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4743 #include "icons/16x16/classviewer-var.xpm"
4744 #include "icons/16x16/classviewer-method.xpm"
4746 /* Create new editor widget (scintilla).
4747 * @note The @c "sci-notify" signal is connected separately. */
4748 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4750 ScintillaObject
*sci
;
4752 sci
= SCINTILLA(scintilla_new());
4754 gtk_widget_show(GTK_WIDGET(sci
));
4756 sci_set_codepage(sci
, SC_CP_UTF8
);
4757 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4758 /* disable scintilla provided popup menu */
4759 sci_use_popup(sci
, FALSE
);
4761 setup_sci_keys(sci
);
4763 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4764 sci_set_lines_wrapped(sci
, editor_prefs
.line_wrapping
);
4765 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
4766 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4767 /*sci_set_caret_policy_y(sci, CARET_JUMPS | CARET_EVEN, 0);*/
4768 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4769 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4771 /* tag autocompletion images */
4772 SSM(sci
, SCI_REGISTERIMAGE
, 1, (sptr_t
)classviewer_var
);
4773 SSM(sci
, SCI_REGISTERIMAGE
, 2, (sptr_t
)classviewer_method
);
4775 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4776 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4779 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4781 /* only connect signals if this is for the document notebook, not split window */
4782 if (editor
->sci
== NULL
)
4784 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4785 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4786 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4787 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4788 g_signal_connect(sci
, "expose-event", G_CALLBACK(on_editor_expose_event
), editor
);
4794 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4795 * @param editor Editor settings.
4796 * @return The new widget.
4800 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4802 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4803 ScintillaObject
*old
, *sci
;
4805 /* temporarily change editor to use the new sci widget */
4807 sci
= create_new_sci(editor
);
4810 editor_set_indent_type(editor
, iprefs
->type
);
4811 editor_set_font(editor
, interface_prefs
.editor_font
);
4812 editor_apply_update_prefs(editor
);
4814 /* if editor already had a widget, restore it */
4821 GeanyEditor
*editor_create(GeanyDocument
*doc
)
4823 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4824 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
4826 editor
->document
= doc
;
4827 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
4829 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
4830 editor
->line_wrapping
= editor_prefs
.line_wrapping
;
4831 editor
->scroll_percent
= -1.0F
;
4832 editor
->line_breaking
= FALSE
;
4834 editor
->sci
= editor_create_widget(editor
);
4839 /* in case we need to free some fields in future */
4840 void editor_destroy(GeanyEditor
*editor
)
4846 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
4848 g_return_if_fail(NZV(doc
->real_path
));
4850 if (utils_str_equal(doc
->real_path
,
4851 utils_build_path(app
->configdir
, "snippets.conf", NULL
)))
4853 /* reload snippets */
4854 editor_snippets_free();
4855 editor_snippets_init();
4860 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
4864 g_return_val_if_fail(editor
, FALSE
);
4866 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
4869 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
4871 /* if no word part, complete normally */
4872 if (!check_partial_completion(editor
, entry
))
4873 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
4880 void editor_init(void)
4882 static GeanyIndentPrefs indent_prefs
;
4884 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
4885 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
4886 editor_prefs
.indentation
= &indent_prefs
;
4888 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
4889 * handler (on_editor_notify) is called */
4890 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
4892 ui_add_config_file_menu_item(utils_build_path(app
->configdir
, "snippets.conf", NULL
),
4894 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
4898 /* TODO: Should these be user-defined instead of hard-coded? */
4899 void editor_set_indentation_guides(GeanyEditor
*editor
)
4904 g_return_if_fail(editor
!= NULL
);
4906 if (! editor_prefs
.show_indent_guide
)
4908 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
4912 lexer
= sci_get_lexer(editor
->sci
);
4915 /* Lines added/removed are prefixed with +/- characters, so
4916 * those lines will not be shown with any indentation guides.
4917 * It can be distracting that only a few of lines in a diff/patch
4918 * file will show the guides. */
4923 /* These languages use indentation for control blocks; the "look forward" method works
4927 case SCLEX_MAKEFILE
:
4930 case SCLEX_PROPERTIES
:
4931 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
4933 mode
= SC_IV_LOOKFORWARD
;
4936 /* C-like (structured) languages benefit from the "look both" method */
4950 case SCLEX_FREEBASIC
:
4953 mode
= SC_IV_LOOKBOTH
;
4961 sci_set_indentation_guides(editor
->sci
, mode
);
4965 /* Apply just the prefs that can change in the Preferences dialog */
4966 void editor_apply_update_prefs(GeanyEditor
*editor
)
4968 ScintillaObject
*sci
;
4970 g_return_if_fail(editor
!= NULL
);
4972 if (main_status
.quitting
)
4977 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
4978 editor_get_long_line_column(), editor_prefs
.long_line_color
);
4980 /* update indent width, tab width */
4981 editor_set_indent_type(editor
, editor
->indent_type
);
4982 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
4984 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
4985 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
4987 editor_set_indentation_guides(editor
);
4989 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
4990 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
4991 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4992 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
4994 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
4997 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4999 /* (dis)allow scrolling past end of document */
5000 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
5004 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
5005 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
5007 ScintillaObject
*sci
= editor
->sci
;
5008 gint pos
= sci_get_position_from_line(sci
, line
);
5012 sci_insert_text(sci
, pos
, "\t");
5016 if (sci_get_char_at(sci
, pos
) == '\t')
5018 sci_set_selection(sci
, pos
, pos
+ 1);
5019 sci_replace_sel(sci
, "");
5021 else /* remove spaces only if no tabs */
5023 gint width
= sci_get_line_indentation(sci
, line
);
5025 width
-= editor_get_indent_prefs(editor
)->width
;
5026 sci_set_line_indentation(sci
, line
, width
);
5032 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
5034 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
5035 ScintillaObject
*sci
= editor
->sci
;
5037 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
5038 change_tab_indentation(editor
, line
, increase
);
5041 gint width
= sci_get_line_indentation(sci
, line
);
5043 width
+= increase
? iprefs
->width
: -iprefs
->width
;
5044 sci_set_line_indentation(sci
, line
, width
);
5049 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
5051 ScintillaObject
*sci
= editor
->sci
;
5053 gint line
, lstart
, lend
;
5055 if (sci_get_lines_selected(sci
) <= 1)
5057 line
= sci_get_current_line(sci
);
5058 editor_change_line_indent(editor
, line
, increase
);
5061 editor_select_lines(editor
, FALSE
);
5062 start
= sci_get_selection_start(sci
);
5063 end
= sci_get_selection_end(sci
);
5064 lstart
= sci_get_line_from_position(sci
, start
);
5065 lend
= sci_get_line_from_position(sci
, end
);
5066 if (end
== sci_get_length(sci
))
5067 lend
++; /* for last line with text on it */
5069 sci_start_undo_action(sci
);
5070 for (line
= lstart
; line
< lend
; line
++)
5072 editor_change_line_indent(editor
, line
, increase
);
5074 sci_end_undo_action(sci
);
5076 /* set cursor/selection */
5079 sci_set_selection_start(sci
, start
);
5080 end
= sci_get_position_from_line(sci
, lend
);
5081 sci_set_selection_end(sci
, end
);
5082 editor_select_lines(editor
, FALSE
);
5086 sci_set_current_line(sci
, lstart
);