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
};
104 } autocompletion_mode
= AUTOC_CANCELLED
;
106 static gchar indent
[100];
109 static void on_new_line_added(GeanyEditor
*editor
);
110 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
111 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
112 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
113 static gboolean
is_code_style(gint lexer
, gint style
);
114 static gboolean
is_string_style(gint lexer
, gint style
);
115 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
116 static void auto_table(GeanyEditor
*editor
, gint pos
);
117 static void close_block(GeanyEditor
*editor
, gint pos
);
118 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
119 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
120 const gchar
*wc
, gboolean stem
);
121 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
);
124 void editor_snippets_free(void)
126 g_hash_table_destroy(snippet_hash
);
127 g_queue_free(snippet_offsets
);
131 void editor_snippets_init(void)
133 gsize i
, j
, len
= 0, len_keys
= 0;
134 gchar
*sysconfigfile
, *userconfigfile
;
135 gchar
**groups_user
, **groups_sys
;
136 gchar
**keys_user
, **keys_sys
;
138 GKeyFile
*sysconfig
= g_key_file_new();
139 GKeyFile
*userconfig
= g_key_file_new();
142 snippet_offsets
= g_queue_new();
144 sysconfigfile
= g_strconcat(app
->datadir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
145 userconfigfile
= g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
147 /* check for old autocomplete.conf files (backwards compatibility) */
148 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
| G_FILE_TEST_IS_SYMLINK
))
149 setptr(userconfigfile
,
150 g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "autocomplete.conf", NULL
));
152 /* load the actual config files */
153 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
154 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
156 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
158 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
160 /* first read all globally defined auto completions */
161 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
162 for (i
= 0; i
< len
; i
++)
164 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
165 /* create new hash table for the read section (=> filetype) */
166 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
167 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
169 for (j
= 0; j
< len_keys
; j
++)
171 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
172 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
174 g_strfreev(keys_sys
);
177 /* now read defined completions in user's configuration directory and add / replace them */
178 groups_user
= g_key_file_get_groups(userconfig
, &len
);
179 for (i
= 0; i
< len
; i
++)
181 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
183 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
185 { /* new key found, create hash table */
186 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
187 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
189 for (j
= 0; j
< len_keys
; j
++)
191 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
193 { /* value = NULL means the key doesn't yet exist, so insert */
194 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
195 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
198 { /* old key and value will be freed by destroy function (g_free) */
199 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
200 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
203 g_strfreev(keys_user
);
206 g_free(sysconfigfile
);
207 g_free(userconfigfile
);
208 g_strfreev(groups_sys
);
209 g_strfreev(groups_user
);
210 g_key_file_free(sysconfig
);
211 g_key_file_free(userconfig
);
215 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
218 GeanyEditor
*editor
= data
;
219 GeanyDocument
*doc
= editor
->document
;
221 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
222 * fake event to show the editor menu triggered by a key event where we want to use the
223 * text cursor position. */
224 if (event
->x
> 0.0 && event
->y
> 0.0)
225 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
226 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
228 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
230 if (event
->button
== 1)
232 guint state
= event
->state
& gtk_accelerator_get_default_mod_mask();
234 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
236 gint ss
= sci_get_selection_start(editor
->sci
);
237 sci_set_selection_end(editor
->sci
, ss
);
239 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GDK_CONTROL_MASK
)
241 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
243 editor_find_current_word(editor
, editor_info
.click_pos
,
244 current_word
, sizeof current_word
, NULL
);
246 return symbols_goto_tag(current_word
, TRUE
);
248 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
251 return document_check_disk_status(doc
, FALSE
);
254 /* calls the edit popup menu in the editor */
255 if (event
->button
== 3)
259 editor_find_current_word(editor
, editor_info
.click_pos
,
260 current_word
, sizeof current_word
, NULL
);
262 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
263 ui_update_popup_goto_items(can_goto
);
264 ui_update_popup_copy_items(doc
);
265 ui_update_insert_include_item(doc
, 0);
267 g_signal_emit_by_name(geany_object
, "update-editor-menu",
268 current_word
, editor_info
.click_pos
, doc
);
270 gtk_menu_popup(GTK_MENU(main_widgets
.editor_menu
),
271 NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
279 static gboolean
is_style_php(gint style
)
281 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
282 style
== SCE_HPHP_COMPLEX_VARIABLE
)
291 gint
editor_get_long_line_type(void)
294 switch (app
->project
->long_line_behaviour
)
296 case 0: /* marker disabled */
298 case 1: /* use global settings */
300 case 2: /* custom (enabled) */
301 return editor_prefs
.long_line_global_type
;
304 if (!editor_prefs
.long_line_global_enabled
)
307 return editor_prefs
.long_line_global_type
;
311 gint
editor_get_long_line_column(void)
313 if (app
->project
&& app
->project
->long_line_behaviour
!= 1 /* use global settings */)
314 return app
->project
->long_line_column
;
316 return editor_prefs
.long_line_global_column
;
320 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
322 ScintillaObject
*sci
;
324 g_return_if_fail(editor
!= NULL
);
328 sci_toggle_fold(sci
, line
);
330 /* extra toggling of child fold points
331 * use when editor_prefs.unfold_all_children is set and Shift is NOT pressed or when
332 * editor_prefs.unfold_all_children is NOT set but Shift is pressed */
333 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
334 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
336 gint last_line
= SSM(sci
, SCI_GETLASTCHILD
, line
, -1);
339 if (sci_get_line_is_visible(sci
, line
+ 1))
340 { /* unfold all children of the current fold point */
341 for (i
= line
; i
< last_line
; i
++)
343 if (! sci_get_line_is_visible(sci
, i
))
345 sci_toggle_fold(sci
, sci_get_fold_parent(sci
, i
));
350 { /* fold all children of the current fold point */
351 for (i
= line
; i
< last_line
; i
++)
353 gint level
= sci_get_fold_level(sci
, i
);
354 if (level
& SC_FOLDLEVELHEADERFLAG
)
356 if (sci_get_fold_expanded(sci
, i
))
357 sci_toggle_fold(sci
, i
);
365 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
367 /* left click to marker margin marks the line */
370 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
372 /*sci_marker_delete_all(editor->sci, 1);*/
373 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
375 /* left click on the folding margin to toggle folding state of current line */
376 else if (nt
->margin
== 2 && editor_prefs
.folding
)
378 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
379 editor_toggle_fold(editor
, line
, nt
->modifiers
);
384 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
386 ScintillaObject
*sci
= editor
->sci
;
387 gint pos
= sci_get_current_position(sci
);
389 /* undo / redo menu update */
390 ui_update_popup_reundo_items(editor
->document
);
392 /* brace highlighting */
393 editor_highlight_braces(editor
, pos
);
395 ui_update_statusbar(editor
->document
, pos
);
397 /* Visible lines are only laid out accurately once [SCN_UPDATEUI] is sent,
398 * so we need to only call sci_scroll_to_line here, because the document
399 * may have line wrapping and folding enabled.
400 * http://scintilla.sourceforge.net/ScintillaDoc.html#LineWrapping */
401 if (editor
->scroll_percent
> 0.0F
)
403 editor_scroll_to_line(editor
, -1, editor
->scroll_percent
);
404 editor
->scroll_percent
= -1.0F
; /* disable further scrolling */
407 /** experimental code for inverting selections */
410 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
412 /* need to get colour from getstyleat(), but how? */
413 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
414 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
417 sci_get_style_at(sci
, pos
);
423 static void check_line_breaking(GeanyEditor
*editor
, gint pos
, gchar c
)
425 ScintillaObject
*sci
= editor
->sci
;
426 gint line
, lstart
, col
;
428 if (!editor
->line_breaking
)
431 col
= sci_get_col_from_position(sci
, pos
);
434 pos
--; /* Look for previous space, not the new one */
436 line
= sci_get_current_line(sci
);
438 lstart
= sci_get_position_from_line(sci
, line
);
440 /* use column instead of position which might be different with multibyte characters */
441 if (col
< editor_prefs
.line_break_column
)
444 /* look for the last space before line_break_column */
445 pos
= MIN(pos
, lstart
+ editor_prefs
.line_break_column
);
449 c
= sci_get_char_at(sci
, --pos
);
452 gint diff
, last_pos
, last_col
;
453 const gchar
*eol
= editor_get_eol_char(editor
);
455 /* remember the distance between the current column and the last column on the line
456 * (we use column position in case the previous line gets altered, such as removing
457 * trailing spaces or in case it contains multibyte characters) */
458 last_pos
= sci_get_line_end_position(sci
, line
);
459 last_col
= sci_get_col_from_position(sci
, last_pos
);
460 diff
= last_col
- col
;
462 /* break the line after the space */
463 sci_insert_text(sci
, pos
+ 1, eol
);
466 /* set position as if user had pressed return */
467 pos
= sci_get_position_from_line(sci
, line
);
468 sci_set_current_position(sci
, pos
, FALSE
);
469 /* add indentation, comment multilines, etc */
470 on_new_line_added(editor
);
472 /* correct cursor position (might not be at line end) */
473 last_pos
= sci_get_line_end_position(sci
, line
);
474 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
475 /* last column - distance is the desired column, then retrieve its document position */
476 pos
= SSM(sci
, SCI_FINDCOLUMN
, line
, last_col
- diff
);
477 sci_set_current_position(sci
, pos
, FALSE
);
485 static void show_autocomplete(ScintillaObject
*sci
, gint rootlen
, const gchar
*words
)
487 /* store whether a calltip is showing, so we can reshow it after autocompletion */
488 calltip
.set
= SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
489 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
);
493 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
495 ScintillaObject
*sci
= editor
->sci
;
497 g_return_if_fail(tags
);
501 GString
*words
= g_string_sized_new(150);
504 for (j
= 0; j
< tags
->len
; ++j
)
506 TMTag
*tag
= tags
->pdata
[j
];
509 g_string_append_c(words
, '\n');
511 if (j
== editor_prefs
.autocompletion_max_entries
)
513 g_string_append(words
, "...");
516 g_string_append(words
, tag
->name
);
518 /* for now, tag types don't all follow C, so just look at arglist */
519 if (NZV(tag
->atts
.entry
.arglist
))
520 g_string_append(words
, "?2");
522 g_string_append(words
, "?1");
524 show_autocomplete(sci
, rootlen
, words
->str
);
525 g_string_free(words
, TRUE
);
530 /* do not use with long strings */
531 static gboolean
match_last_chars(ScintillaObject
*sci
, gint pos
, const gchar
*str
)
533 gsize len
= strlen(str
);
536 g_return_val_if_fail(len
< 100, FALSE
);
538 buf
= g_alloca(len
+ 1);
539 sci_get_text_range(sci
, pos
- len
, pos
, buf
);
540 return strcmp(str
, buf
) == 0;
544 static gboolean
reshow_calltip(gpointer data
)
548 g_return_val_if_fail(calltip
.sci
!= NULL
, FALSE
);
550 SSM(calltip
.sci
, SCI_CALLTIPCANCEL
, 0, 0);
551 doc
= document_get_current();
553 if (doc
&& doc
->editor
->sci
== calltip
.sci
)
555 /* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
556 * may be completely wrong in case the user cancelled the auto completion with the mouse */
557 SSM(calltip
.sci
, SCI_CALLTIPSHOW
, calltip
.pos
, (sptr_t
) calltip
.text
);
563 static void request_reshowing_calltip(SCNotification
*nt
)
567 /* delay the reshow of the calltip window to make sure it is actually displayed,
568 * without it might be not visible on SCN_AUTOCCANCEL */
569 g_idle_add(reshow_calltip
, NULL
);
574 static void autocomplete_scope(GeanyEditor
*editor
)
576 ScintillaObject
*sci
= editor
->sci
;
577 gint pos
= sci_get_current_position(editor
->sci
);
578 gchar typed
= sci_get_char_at(sci
, pos
- 1);
580 const GPtrArray
*tags
= NULL
;
582 GeanyFiletype
*ft
= editor
->document
->file_type
;
584 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
586 if (match_last_chars(sci
, pos
, "->") || match_last_chars(sci
, pos
, "::"))
588 else if (typed
!= '.')
591 else if (typed
!= '.')
594 /* allow for a space between word and operator */
595 if (isspace(sci_get_char_at(sci
, pos
- 2)))
597 name
= editor_get_word_at_pos(editor
, pos
- 1, NULL
);
601 tags
= tm_workspace_find(name
, tm_tag_max_t
, NULL
, FALSE
, ft
->lang
);
603 if (!tags
|| tags
->len
== 0)
606 tag
= g_ptr_array_index(tags
, 0);
607 name
= tag
->atts
.entry
.var_type
;
610 TMWorkObject
*obj
= editor
->document
->tm_file
;
612 tags
= tm_workspace_find_scope_members(obj
? obj
->tags_array
: NULL
,
616 autocompletion_mode
= AUTOC_SCOPE
;
617 show_tags_list(editor
, tags
, 0);
623 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
625 ScintillaObject
*sci
= editor
->sci
;
626 gint pos
= sci_get_current_position(sci
);
631 { /* simple indentation (only for CR format) */
632 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
633 on_new_line_added(editor
);
637 { /* simple indentation (for CR/LF and LF format) */
638 on_new_line_added(editor
);
642 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
645 { /* close xml-tags */
646 handle_xml(editor
, pos
, nt
->ch
);
651 auto_close_chars(sci
, pos
, nt
->ch
);
653 editor_show_calltip(editor
, --pos
);
657 { /* hide calltips */
658 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0))
660 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
662 g_free(calltip
.text
);
674 auto_close_chars(sci
, pos
, nt
->ch
);
678 { /* closing bracket handling */
679 if (editor
->auto_indent
)
680 close_block(editor
, pos
- 1);
683 /* scope autocompletion */
685 case ':': /* C/C++ class:: syntax */
686 /* tag autocompletion */
689 if (! editor_start_auto_complete(editor
, pos
, FALSE
))
690 request_reshowing_calltip(nt
);
692 editor_start_auto_complete(editor
, pos
, FALSE
);
695 check_line_breaking(editor
, pos
, nt
->ch
);
699 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
700 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
701 gboolean force
, gint visLevels
, gint level
)
703 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
704 gint levelLine
= level
;
706 while (*line
<= lineMaxSubord
)
708 if (G_UNLIKELY(force
))
711 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
713 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
718 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
721 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
722 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
724 if (G_UNLIKELY(force
))
727 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
729 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
730 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
736 if (!sci_get_fold_expanded(sci
, *line
))
737 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
738 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
742 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
754 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
756 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
758 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
760 /* Adding a fold point */
761 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
762 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
765 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
767 if (! sci_get_fold_expanded(sci
, line
))
768 { /* Removing the fold from one that has been contracted so should expand
769 * otherwise lines are left invisible with no way to make them visible */
770 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
771 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
774 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
775 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
777 /* See if should still be hidden */
778 gint parentLine
= sci_get_fold_parent(sci
, line
);
781 SSM(sci
, SCI_SHOWLINES
, line
, line
);
783 else if (sci_get_fold_expanded(sci
, parentLine
) &&
784 sci_get_line_is_visible(sci
, parentLine
))
786 SSM(sci
, SCI_SHOWLINES
, line
, line
);
792 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
793 gboolean enforcePolicy
)
795 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
796 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
799 for (line
= lineStart
; line
<= lineEnd
; line
++)
801 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
806 static void auto_update_margin_width(GeanyEditor
*editor
)
808 gint next_linecount
= 1;
809 gint linecount
= sci_get_line_count(editor
->sci
);
810 GeanyDocument
*doc
= editor
->document
;
812 while (next_linecount
<= linecount
)
813 next_linecount
*= 10;
815 if (editor
->document
->priv
->line_count
!= next_linecount
)
817 doc
->priv
->line_count
= next_linecount
;
818 sci_set_line_numbers(editor
->sci
, TRUE
, 0);
823 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
825 gint pos
= sci_get_current_position(sci
);
827 sci_insert_text(sci
, pos
, text
);
828 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
832 /* Complete the next word part from @a entry */
833 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
835 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
837 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
839 if (strstr(text
, stem
) != text
)
840 return FALSE
; /* shouldn't happen */
841 if (strlen(text
) <= strlen(stem
))
844 text
+= strlen(stem
); /* skip stem */
845 ptr
= strstr(text
+ 1, "_");
849 partial_complete(editor
->sci
, text
);
855 foreach_str(ptr
, text
+ 1)
859 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
862 partial_complete(editor
->sci
, text
);
871 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
872 * Plugins can connect to the "editor-notify" signal. */
873 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
874 gpointer scnt
, gpointer data
)
876 GeanyEditor
*editor
= data
;
879 g_return_if_fail(editor
!= NULL
);
881 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
885 static gboolean
on_editor_notify(G_GNUC_UNUSED GObject
*object
, GeanyEditor
*editor
,
886 SCNotification
*nt
, G_GNUC_UNUSED gpointer data
)
888 ScintillaObject
*sci
= editor
->sci
;
889 GeanyDocument
*doc
= editor
->document
;
891 switch (nt
->nmhdr
.code
)
893 case SCN_SAVEPOINTLEFT
:
894 document_set_text_changed(doc
, TRUE
);
897 case SCN_SAVEPOINTREACHED
:
898 document_set_text_changed(doc
, FALSE
);
901 case SCN_MODIFYATTEMPTRO
:
905 case SCN_MARGINCLICK
:
906 on_margin_click(editor
, nt
);
910 on_update_ui(editor
, nt
);
914 if (editor_prefs
.show_linenumber_margin
&& (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) && nt
->linesAdded
)
916 /* automatically adjust Scintilla's line numbers margin width */
917 auto_update_margin_width(editor
);
919 if (nt
->modificationType
& SC_STARTACTION
&& ! ignore_callback
)
921 /* get notified about undo changes */
922 document_undo_add(doc
, UNDO_SCINTILLA
, NULL
);
924 if (editor_prefs
.folding
&& (nt
->modificationType
& SC_MOD_CHANGEFOLD
) != 0)
926 /* handle special fold cases, e.g. #1923350 */
927 fold_changed(sci
, nt
->line
, nt
->foldLevelNow
, nt
->foldLevelPrev
);
932 on_char_added(editor
, nt
);
935 case SCN_USERLISTSELECTION
:
936 if (nt
->listType
== 1)
938 sci_add_text(sci
, nt
->text
);
942 case SCN_AUTOCSELECTION
:
943 if (g_str_equal(nt
->text
, "..."))
950 case SCN_AUTOCCANCELLED
:
951 /* now that autocomplete is finishing or was cancelled, reshow calltips
952 * if they were showing */
953 autocompletion_mode
= AUTOC_CANCELLED
;
954 request_reshowing_calltip(nt
);
958 case SCN_STYLENEEDED
:
959 geany_debug("style");
963 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
967 if (nt
->text
!= NULL
)
969 document_open_file_list(nt
->text
, -1);
973 case SCN_CALLTIPCLICK
:
974 if (nt
->position
> 0)
976 switch (nt
->position
)
978 case 1: /* up arrow */
979 if (calltip
.tag_index
> 0)
983 case 2: calltip
.tag_index
++; break; /* down arrow */
985 editor_show_calltip(editor
, -1);
990 /* recalculate line margin width */
991 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
994 /* we always return FALSE here to let plugins handle the event too */
999 /* Note: this is the same as sci_get_tab_width(), but is still useful when you don't have
1000 * a scintilla pointer. */
1001 static gint
get_tab_width(const GeanyIndentPrefs
*indent_prefs
)
1003 if (indent_prefs
->type
== GEANY_INDENT_TYPE_BOTH
)
1004 return indent_prefs
->hard_tab_width
;
1006 return indent_prefs
->width
; /* tab width = indent width */
1010 /* Returns a string containing width chars of whitespace, filled with simple space
1011 * characters or with the right number of tab characters, according to the indent prefs.
1012 * (Result is filled with tabs *and* spaces if width isn't a multiple of
1013 * the tab width). */
1015 get_whitespace(const GeanyIndentPrefs
*iprefs
, gint width
)
1017 g_return_val_if_fail(width
>= 0, NULL
);
1020 return g_strdup("");
1022 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
1024 return g_strnfill(width
, ' ');
1027 { /* first fill text with tabs and fill the rest with spaces */
1028 const gint tab_width
= get_tab_width(iprefs
);
1029 gint tabs
= width
/ tab_width
;
1030 gint spaces
= width
% tab_width
;
1031 gint len
= tabs
+ spaces
;
1034 str
= g_malloc(len
+ 1);
1036 memset(str
, '\t', tabs
);
1037 memset(str
+ tabs
, ' ', spaces
);
1044 static const GeanyIndentPrefs
*
1045 get_default_indent_prefs(void)
1047 static GeanyIndentPrefs iprefs
;
1049 iprefs
= app
->project
? *app
->project
->priv
->indentation
: *editor_prefs
.indentation
;
1054 /** Gets the indentation prefs for the editor.
1055 * In future, the prefs might be different according to project or filetype.
1056 * @warning Always get a fresh result instead of keeping a pointer to it if the editor
1057 * settings may have changed, or if this function has been called for a different @a editor.
1058 * @param editor The editor, or @c NULL to get the default indent prefs.
1059 * @return The indent prefs. */
1060 const GeanyIndentPrefs
*
1061 editor_get_indent_prefs(GeanyEditor
*editor
)
1063 static GeanyIndentPrefs iprefs
;
1065 iprefs
= *get_default_indent_prefs();
1070 iprefs
.type
= editor
->indent_type
;
1072 /* if per-document auto-indent is enabled, but we don't have a global mode set,
1073 * just use basic auto-indenting */
1074 if (editor
->auto_indent
&& iprefs
.auto_indent_mode
== GEANY_AUTOINDENT_NONE
)
1075 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_BASIC
;
1077 if (!editor
->auto_indent
)
1078 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_NONE
;
1084 static void on_new_line_added(GeanyEditor
*editor
)
1086 ScintillaObject
*sci
= editor
->sci
;
1087 gint line
= sci_get_current_line(sci
);
1089 /* simple indentation */
1090 if (editor
->auto_indent
)
1092 insert_indent_after_line(editor
, line
- 1);
1095 if (editor_prefs
.auto_continue_multiline
)
1096 { /* " * " auto completion in multiline C/C++/D/Java comments */
1097 auto_multiline(editor
, line
);
1100 if (editor_prefs
.newline_strip
)
1101 { /* strip the trailing spaces on the previous line */
1102 editor_strip_line_trailing_spaces(editor
, line
- 1);
1107 static gboolean
lexer_has_braces(ScintillaObject
*sci
)
1109 gint lexer
= sci_get_lexer(sci
);
1115 case SCLEX_HTML
: /* for PHP & JS */
1116 case SCLEX_PASCAL
: /* for multiline comments? */
1127 /* Read indent chars for the line that pos is on into indent global variable.
1128 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1129 * instead in any new code. */
1130 static void read_indent(GeanyEditor
*editor
, gint pos
)
1132 ScintillaObject
*sci
= editor
->sci
;
1133 guint i
, len
, j
= 0;
1137 line
= sci_get_line_from_position(sci
, pos
);
1139 len
= sci_get_line_length(sci
, line
);
1140 linebuf
= sci_get_line(sci
, line
);
1142 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1144 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1145 indent
[j
++] = linebuf
[i
];
1154 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1160 len
= sci_get_line_length(sci
, line
);
1161 linebuf
= sci_get_line(sci
, line
);
1163 for (i
= 0; i
< len
; i
++)
1165 /* i == (len - 1) prevents wrong indentation after lines like
1166 * " { return bless({}, shift); }" (Perl) */
1167 if (linebuf
[i
] == '{' && i
== (len
- 1))
1176 while (k
> 0 && isspace(linebuf
[k
])) k
--;
1178 /* if last non-whitespace character is a { increase indentation by a tab
1179 * e.g. for (...) { */
1180 if (linebuf
[k
] == '{')
1192 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1194 gint last_char
= sci_get_line_end_position(sci
, line
) - 1;
1196 /* add extra indentation for Python after colon */
1197 if (sci_get_char_at(sci
, last_char
) == ':' &&
1198 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1206 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1208 ScintillaObject
*sci
= editor
->sci
;
1210 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1212 g_return_val_if_fail(line
>= 0, 0);
1214 size
= sci_get_line_indentation(sci
, line
);
1216 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1218 if (lexer_has_braces(sci
))
1219 size
+= iprefs
->width
* get_brace_indent(sci
, line
);
1221 if (FILETYPE_ID(editor
->document
->file_type
) == GEANY_FILETYPES_PYTHON
)
1222 size
+= iprefs
->width
* get_python_indent(sci
, line
);
1228 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
)
1230 ScintillaObject
*sci
= editor
->sci
;
1231 gint line_indent
= sci_get_line_indentation(sci
, line
);
1232 gint size
= get_indent_size_after_line(editor
, line
);
1233 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1239 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
&& size
== line_indent
)
1241 /* support tab indents, space aligns style - copy last line 'indent' exactly */
1242 gint start
= sci_get_position_from_line(sci
, line
);
1243 gint end
= sci_get_line_indent_position(sci
, line
);
1245 text
= sci_get_contents_range(sci
, start
, end
);
1249 text
= get_whitespace(iprefs
, size
);
1251 sci_add_text(sci
, text
);
1256 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
)
1258 const gchar
*closing_char
= NULL
;
1261 if (utils_isbrace(c
, 0))
1262 end_pos
= sci_find_matching_brace(sci
, pos
- 1);
1267 if ((editor_prefs
.autoclose_chars
& GEANY_AC_PARENTHESIS
) && end_pos
== -1)
1271 if ((editor_prefs
.autoclose_chars
& GEANY_AC_CBRACKET
) && end_pos
== -1)
1275 if ((editor_prefs
.autoclose_chars
& GEANY_AC_SBRACKET
) && end_pos
== -1)
1279 if (editor_prefs
.autoclose_chars
& GEANY_AC_SQUOTE
)
1283 if (editor_prefs
.autoclose_chars
& GEANY_AC_DQUOTE
)
1284 closing_char
= "\"";
1288 if (closing_char
!= NULL
)
1290 sci_add_text(sci
, closing_char
);
1291 sci_set_current_position(sci
, pos
, TRUE
);
1296 /* Finds a corresponding matching brace to the given pos
1297 * (this is taken from Scintilla Editor.cxx,
1298 * fit to work with close_block) */
1299 static gint
brace_match(ScintillaObject
*sci
, gint pos
)
1301 gchar chBrace
= sci_get_char_at(sci
, pos
);
1302 gchar chSeek
= utils_brace_opposite(chBrace
);
1304 gint direction
= -1;
1309 styBrace
= sci_get_style_at(sci
, pos
);
1311 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1314 pos
= pos
+ direction
;
1315 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1317 chAtPos
= sci_get_char_at(sci
, pos
- 1);
1318 styAtPos
= sci_get_style_at(sci
, pos
);
1320 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1322 if (chAtPos
== chBrace
)
1324 if (chAtPos
== chSeek
)
1329 pos
= pos
+ direction
;
1335 /* Called after typing '}'. */
1336 static void close_block(GeanyEditor
*editor
, gint pos
)
1339 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1340 gint x
= 0, cnt
= 0;
1341 gint line
, line_len
, eol_char_len
;
1342 gchar
*text
, *line_buf
;
1343 ScintillaObject
*sci
;
1344 gint line_indent
, last_indent
;
1346 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1348 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1351 doc
= editor
->document
;
1353 if (! lexer_has_braces(sci
))
1356 line
= sci_get_line_from_position(sci
, pos
);
1357 line_len
= sci_get_line_length(sci
, line
);
1358 /* set eol_char_len to 0 if on last line, because there is no EOL char */
1359 eol_char_len
= (line
== (sci_get_line_count(sci
) - 1)) ? 0 :
1360 editor_get_eol_char_len(editor
);
1362 /* check that the line is empty, to not kill text in the line */
1363 line_buf
= sci_get_line(sci
, line
);
1364 line_buf
[line_len
- eol_char_len
] = '\0';
1365 while (x
< (line_len
- eol_char_len
))
1367 if (isspace(line_buf
[x
]))
1373 if ((line_len
- eol_char_len
- 1) != cnt
)
1376 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1378 gint start_brace
= brace_match(sci
, pos
);
1380 if (start_brace
>= 0)
1383 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1384 gint size
= sci_get_line_indentation(sci
, brace_line
);
1385 gchar
*ind
= get_whitespace(iprefs
, size
);
1387 text
= g_strconcat(ind
, "}", NULL
);
1388 line_start
= sci_get_position_from_line(sci
, line
);
1389 sci_set_anchor(sci
, line_start
);
1390 sci_replace_sel(sci
, text
);
1395 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1398 /* GEANY_AUTOINDENT_CURRENTCHARS */
1399 line_indent
= sci_get_line_indentation(sci
, line
);
1400 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1402 if (line_indent
< last_indent
)
1404 line_indent
-= iprefs
->width
;
1405 line_indent
= MAX(0, line_indent
);
1406 sci_set_line_indentation(sci
, line
, line_indent
);
1410 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1411 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1412 * position can be -1, then the current position is used.
1413 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1414 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1415 const gchar
*wc
, gboolean stem
)
1417 gint line
, line_start
, startword
, endword
;
1419 ScintillaObject
*sci
;
1421 g_return_if_fail(editor
!= NULL
);
1425 pos
= sci_get_current_position(sci
);
1427 line
= sci_get_line_from_position(sci
, pos
);
1428 line_start
= sci_get_position_from_line(sci
, line
);
1429 startword
= pos
- line_start
;
1430 endword
= pos
- line_start
;
1433 chunk
= sci_get_line(sci
, line
);
1436 wc
= GEANY_WORDCHARS
;
1438 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1439 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1440 * TODO: improve this code */
1441 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || chunk
[startword
- 1] < 0))
1445 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || chunk
[endword
] < 0))
1449 if (startword
!= endword
)
1451 chunk
[endword
] = '\0';
1453 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1456 g_strlcpy(word
, "", wordlen
);
1462 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1463 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1464 * position can be -1, then the current position is used.
1465 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1466 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1469 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1474 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1475 * Otherwise NULL is returned.
1476 * Additional wordchars can be specified to define what to consider as a word.
1478 * @param editor The editor to operate on.
1479 * @param pos The position where the word should be read from.
1480 * Maybe @c -1 to use the current position.
1481 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1482 * as part of a word. Maybe @c NULL to use the default wordchars,
1483 * see @ref GEANY_WORDCHARS.
1485 * @return A newly-allocated string containing the word at the given @a pos or @c NULL.
1486 * Should be freed when no longer needed.
1490 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1492 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1494 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1496 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1498 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1502 /* Read the word up to position @a pos. */
1503 static const gchar
*
1504 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1506 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1508 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1510 return (*word
) ? word
: NULL
;
1514 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1517 gint orig_pos
= pos
;
1519 c
= sci_get_char_at(sci
, pos
);
1520 while (pos
>= 0 && pos
> orig_pos
- 300)
1522 c
= sci_get_char_at(sci
, pos
);
1524 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1531 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1535 gint orig_pos
= pos
;
1537 c
= sci_get_char_at(sci
, pos
);
1538 while (pos
> 0 && pos
> orig_pos
- 300)
1540 c
= sci_get_char_at(sci
, pos
);
1541 if (c
== ')') brackets
++;
1542 else if (c
== '(') brackets
--;
1544 if (brackets
< 0) return pos
; /* found start bracket */
1550 static gboolean
append_calltip(GString
*str
, const TMTag
*tag
, filetype_id ft_id
)
1552 if (! tag
->atts
.entry
.arglist
)
1555 if (ft_id
!= GEANY_FILETYPES_PASCAL
)
1556 { /* usual calltips: "retval tagname (arglist)" */
1557 if (tag
->atts
.entry
.var_type
)
1561 g_string_append(str
, tag
->atts
.entry
.var_type
);
1562 for (i
= 0; i
< tag
->atts
.entry
.pointerOrder
; i
++)
1564 g_string_append_c(str
, '*');
1566 g_string_append_c(str
, ' ');
1568 if (tag
->atts
.entry
.scope
)
1570 const gchar
*cosep
= symbols_get_context_separator(ft_id
);
1572 g_string_append(str
, tag
->atts
.entry
.scope
);
1573 g_string_append(str
, cosep
);
1575 g_string_append(str
, tag
->name
);
1576 g_string_append_c(str
, ' ');
1577 g_string_append(str
, tag
->atts
.entry
.arglist
);
1580 { /* special case Pascal calltips: "tagname (arglist) : retval" */
1581 g_string_append(str
, tag
->name
);
1582 g_string_append_c(str
, ' ');
1583 g_string_append(str
, tag
->atts
.entry
.arglist
);
1585 if (NZV(tag
->atts
.entry
.var_type
))
1587 g_string_append(str
, " : ");
1588 g_string_append(str
, tag
->atts
.entry
.var_type
);
1596 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1598 const GPtrArray
*tags
;
1599 const gint arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1600 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1601 TMTagAttrType
*attrs
= NULL
;
1603 GString
*str
= NULL
;
1606 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1608 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1609 tags
= tm_workspace_find(word
, tm_tag_max_t
, attrs
, FALSE
, ft
->lang
);
1613 tag
= TM_TAG(tags
->pdata
[0]);
1615 if (tag
->type
== tm_tag_class_t
&& FILETYPE_ID(ft
) == GEANY_FILETYPES_D
)
1617 /* user typed e.g. 'new Classname(' so lookup D constructor Classname::this() */
1618 tags
= tm_workspace_find_scoped("this", tag
->name
,
1619 arg_types
, attrs
, FALSE
, ft
->lang
, TRUE
);
1624 /* remove tags with no argument list */
1625 for (i
= 0; i
< tags
->len
; i
++)
1627 tag
= TM_TAG(tags
->pdata
[i
]);
1629 if (! tag
->atts
.entry
.arglist
)
1630 tags
->pdata
[i
] = NULL
;
1632 tm_tags_prune((GPtrArray
*) tags
);
1636 { /* remove duplicate calltips */
1637 TMTagAttrType sort_attr
[] = {tm_tag_attr_name_t
, tm_tag_attr_scope_t
,
1638 tm_tag_attr_arglist_t
, 0};
1640 tm_tags_sort((GPtrArray
*) tags
, sort_attr
, TRUE
);
1643 /* if the current word has changed since last time, start with the first tag match */
1644 if (! utils_str_equal(word
, calltip
.last_word
))
1645 calltip
.tag_index
= 0;
1646 /* cache the current word for next time */
1647 g_free(calltip
.last_word
);
1648 calltip
.last_word
= g_strdup(word
);
1649 calltip
.tag_index
= MIN(calltip
.tag_index
, tags
->len
- 1); /* ensure tag_index is in range */
1651 for (i
= calltip
.tag_index
; i
< tags
->len
; i
++)
1653 tag
= TM_TAG(tags
->pdata
[i
]);
1657 str
= g_string_new(NULL
);
1658 if (calltip
.tag_index
> 0)
1659 g_string_prepend(str
, "\001 "); /* up arrow */
1660 append_calltip(str
, tag
, FILETYPE_ID(ft
));
1662 else /* add a down arrow */
1664 if (calltip
.tag_index
> 0) /* already have an up arrow */
1665 g_string_insert_c(str
, 1, '\002');
1667 g_string_prepend(str
, "\002 ");
1673 gchar
*result
= str
->str
;
1675 g_string_free(str
, FALSE
);
1682 /* use pos = -1 to search for the previous unmatched open bracket. */
1683 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
1685 gint orig_pos
= pos
; /* the position for the calltip */
1688 gchar word
[GEANY_MAX_WORD_LENGTH
];
1690 ScintillaObject
*sci
;
1692 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1693 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
1697 lexer
= sci_get_lexer(sci
);
1701 /* position of '(' is unknown, so go backwards from current position to find it */
1702 pos
= sci_get_current_position(sci
);
1705 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
1706 find_start_bracket(sci
, pos
);
1711 /* the style 1 before the brace (which may be highlighted) */
1712 style
= sci_get_style_at(sci
, pos
- 1);
1713 if (! is_code_style(lexer
, style
))
1717 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
1718 if (word
[0] == '\0')
1721 str
= find_calltip(word
, editor
->document
->file_type
);
1724 g_free(calltip
.text
); /* free the old calltip */
1726 calltip
.pos
= orig_pos
;
1729 utils_wrap_string(calltip
.text
, -1);
1730 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
1737 gchar
*editor_get_calltip_text(GeanyEditor
*editor
, const TMTag
*tag
)
1741 g_return_val_if_fail(editor
!= NULL
, NULL
);
1743 str
= g_string_new(NULL
);
1744 if (append_calltip(str
, tag
, FILETYPE_ID(editor
->document
->file_type
)))
1745 return g_string_free(str
, FALSE
);
1747 return g_string_free(str
, TRUE
);
1751 /* HTML entities auto completion from html_entities.tags text file */
1753 autocomplete_html(ScintillaObject
*sci
, const gchar
*root
, gsize rootlen
)
1756 gboolean found
= FALSE
;
1758 const gchar
**entities
= symbols_get_html_entities();
1760 if (*root
!= '&' || G_UNLIKELY(entities
== NULL
))
1763 words
= g_string_sized_new(500);
1766 if (entities
[i
] == NULL
)
1768 else if (entities
[i
][0] == '#')
1771 if (! strncmp(entities
[i
], root
, rootlen
))
1774 g_string_append_c(words
, '\n');
1775 g_string_append(words
, entities
[i
]);
1780 show_autocomplete(sci
, rootlen
, words
->str
);
1782 g_string_free(words
, TRUE
);
1787 /* Current document & global tags autocompletion */
1789 autocomplete_tags(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
1791 TMTagAttrType attrs
[] = { tm_tag_attr_name_t
, 0 };
1792 const GPtrArray
*tags
;
1795 g_return_val_if_fail(editor
, FALSE
);
1797 doc
= editor
->document
;
1799 tags
= tm_workspace_find(root
, tm_tag_max_t
, attrs
, TRUE
, doc
->file_type
->lang
);
1802 autocompletion_mode
= AUTOC_TAGS
;
1803 show_tags_list(editor
, tags
, rootlen
);
1804 return tags
->len
> 0;
1810 /* Check whether to use entity autocompletion:
1811 * - always in a HTML file except when inside embedded JavaScript, Python, ASP, ...
1812 * - in a PHP file only when we are outside of <? ?> */
1813 static gboolean
autocomplete_check_for_html(gint ft_id
, gint style
)
1815 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
1816 * (everything after SCE_HJ_START is for embedded scripting languages) */
1817 if (ft_id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
1820 if (ft_id
== GEANY_FILETYPES_PHP
)
1822 /* use entity completion when style is outside of PHP styles */
1823 if (! is_style_php(style
))
1831 /* Algorithm based on based on Scite's StartAutoCompleteWord() */
1832 static GString
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
1835 gint len
, current
, word_end
;
1836 gint pos_find
, flags
;
1840 struct Sci_TextToFind ttf
;
1842 len
= sci_get_length(sci
);
1843 current
= sci_get_current_position(sci
) - rootlen
;
1845 ttf
.lpstrText
= root
;
1847 ttf
.chrg
.cpMax
= len
;
1848 ttf
.chrgText
.cpMin
= 0;
1849 ttf
.chrgText
.cpMax
= 0;
1850 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
1852 words
= g_string_sized_new(256);
1853 /* put space before first entry to make searching with strstr easy */
1854 g_string_append_c(words
, ' ');
1856 /* search the whole document for the word root and collect results */
1857 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
1858 while (pos_find
>= 0 && pos_find
< len
)
1860 word_end
= pos_find
+ rootlen
;
1861 if (pos_find
!= current
)
1863 while (word_end
< len
&& strchr(GEANY_WORDCHARS
, sci_get_char_at(sci
, word_end
)) != NULL
)
1866 word_length
= word_end
- pos_find
;
1867 if (word_length
> rootlen
)
1869 word
= g_malloc0(word_length
+ 3);
1870 sci_get_text_range(sci
, pos_find
, word_end
, word
+ 1);
1872 word
[word_length
+ 1] = ' ';
1873 /* search the words string whether we already have the word in, otherwise add it */
1874 if (strstr(words
->str
, word
) == NULL
)
1876 g_string_append(words
, word
+ 1);
1881 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
1885 ttf
.chrg
.cpMin
= word_end
;
1886 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
1891 g_strdelimit(words
->str
, " ", '\n');
1892 words
->str
[words
->len
- 1] = '\0'; /* remove the trailing '\n' */
1895 g_string_free(words
, TRUE
);
1900 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
1902 ScintillaObject
*sci
= editor
->sci
;
1906 GSList
*node
, *list
= NULL
;
1908 words
= get_doc_words(sci
, root
, rootlen
);
1911 scintilla_send_message(sci
, SCI_AUTOCCANCEL
, 0, 0);
1912 autocompletion_mode
= AUTOC_CANCELLED
;
1916 /* words are unsorted, make list of words */
1917 foreach_str(ptr
, words
->str
)
1921 list
= g_slist_prepend(list
, ptr
+ 1);
1922 /* terminate previous string in list */
1927 list
= g_slist_sort(list
, (GCompareFunc
)utils_str_casecmp
);
1929 str
= g_string_sized_new(words
->len
);
1930 foreach_slist(node
, list
)
1932 g_string_append(str
, node
->data
);
1934 g_string_append_c(str
, '\n');
1936 if (g_slist_length(list
) >= editor_prefs
.autocompletion_max_entries
)
1937 g_string_append(str
, "\n...");
1940 g_string_free(words
, TRUE
);
1942 autocompletion_mode
= AUTOC_DOC_WORDS
;
1943 show_autocomplete(sci
, rootlen
, str
->str
);
1944 g_string_free(str
, TRUE
);
1949 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
1951 gint line
, line_start
, line_len
, line_pos
, current
, rootlen
, startword
, lexer
, style
;
1952 gchar
*linebuf
, *root
;
1953 ScintillaObject
*sci
;
1954 gboolean ret
= FALSE
;
1955 const gchar
*wordchars
;
1958 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
1961 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1963 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
1964 * necessary styling information */
1965 if (G_UNLIKELY(pos
< 2))
1969 ft
= editor
->document
->file_type
;
1971 line
= sci_get_line_from_position(sci
, pos
);
1972 line_start
= sci_get_position_from_line(sci
, line
);
1973 line_len
= sci_get_line_length(sci
, line
);
1974 line_pos
= pos
- line_start
- 1;
1975 current
= pos
- line_start
;
1976 startword
= current
;
1977 lexer
= sci_get_lexer(sci
);
1978 style
= sci_get_style_at(sci
, pos
- 2);
1980 /* don't autocomplete in comments and strings */
1981 if (!force
&& !is_code_style(lexer
, style
))
1984 autocomplete_scope(editor
);
1986 linebuf
= sci_get_line(sci
, line
);
1988 if (ft
->id
== GEANY_FILETYPES_LATEX
)
1989 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
1990 else if (ft
->id
== GEANY_FILETYPES_HTML
|| ft
->id
== GEANY_FILETYPES_PHP
)
1991 wordchars
= GEANY_WORDCHARS
"&"; /* add & to word chars if we are in a PHP or HTML file */
1993 wordchars
= GEANY_WORDCHARS
;
1995 /* find the start of the current word */
1996 while ((startword
> 0) && (strchr(wordchars
, linebuf
[startword
- 1])))
1998 linebuf
[current
] = '\0';
1999 root
= linebuf
+ startword
;
2000 rootlen
= current
- startword
;
2002 if (rootlen
> 0 && autocompletion_mode
!= AUTOC_SCOPE
)
2004 if (autocomplete_check_for_html(ft
->id
, style
))
2006 /* Allow something like ""some text"". The above startword calculation
2007 * only works on words but for HTML entity completion we also want to have completion
2008 * based on '&' within words. */
2009 gchar
*tmp
= strchr(root
, '&');
2013 rootlen
= strlen(tmp
);
2015 ret
= autocomplete_html(sci
, root
, rootlen
);
2019 /* force is set when called by keyboard shortcut, otherwise start at the
2020 * editor_prefs.symbolcompletion_min_chars'th char */
2021 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
2023 /* complete tags, except if forcing when completion is already visible */
2024 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
2025 ret
= autocomplete_tags(editor
, root
, rootlen
);
2027 /* If forcing and there's nothing else to show, complete from words in document */
2028 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2029 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2041 static const gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2043 gchar
*result
= NULL
;
2046 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2048 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2051 result
= g_hash_table_lookup(tmp
, name
);
2053 /* whether nothing is set for the current filetype(tmp is NULL) or
2054 * the particular completion for this filetype is not set (result is NULL) */
2055 if (tmp
== NULL
|| result
== NULL
)
2057 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2060 result
= g_hash_table_lookup(tmp
, name
);
2063 /* if result is still NULL here, no completion could be found */
2065 /* result is owned by the hash table and will be freed when the table will destroyed */
2070 /* This is very ugly but passing the pattern to ac_replace_specials() doesn't work because it is
2071 * modified when replacing a completion but the foreach function still passes the old pointer
2072 * to ac_replace_specials, so we use a global pointer outside of ac_replace_specials and
2073 * ac_complete_constructs. Any hints to improve this are welcome. */
2074 static GString
*snippets_global_pattern
= NULL
;
2076 static void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2080 g_return_if_fail(key
!= NULL
);
2081 g_return_if_fail(value
!= NULL
);
2083 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2085 utils_string_replace_all(snippets_global_pattern
, needle
, (gchar
*) value
);
2090 /* this only works with spaces only indentation on the lines */
2091 static void fix_line_indents(GeanyEditor
*editor
, gint line_start
, gint line_end
)
2093 ScintillaObject
*sci
= editor
->sci
;
2094 gint line
, cur_line
, cur_col
, pos
;
2096 /* get the line, col position as fixing indentation will move cursor to start of line */
2097 pos
= sci_get_current_position(sci
);
2098 cur_col
= sci_get_col_from_position(sci
, pos
);
2099 cur_line
= sci_get_current_line(sci
);
2101 for (line
= line_start
; line
<= line_end
; line
++)
2103 gint size
= sci_get_line_indentation(sci
, line
);
2105 /* set to 0 first to trigger proper indent creation */
2106 sci_set_line_indentation(sci
, line
, 0);
2107 sci_set_line_indentation(sci
, line
, size
);
2109 pos
= scintilla_send_message(sci
, SCI_FINDCOLUMN
, cur_line
, cur_col
);
2110 sci_set_current_position(sci
, pos
, FALSE
);
2114 static void replace_leading_tabs(GString
*str
, const gchar
*whitespace
)
2118 regmatch_t matches
[2];
2121 if (regcomp(®ex
, "^ *(\t)", 0) != 0)
2123 g_return_if_fail(FALSE
);
2128 if (regexec(®ex
, ptr
,
2129 G_N_ELEMENTS(matches
), matches
, 0) != 0)
2132 pos
= matches
[1].rm_so
;
2133 g_return_if_fail(pos
>= 0);
2134 pos
+= ptr
- str
->str
;
2135 g_string_erase(str
, pos
, 1);
2136 g_string_insert(str
, pos
, whitespace
);
2137 ptr
= str
->str
+ pos
+ strlen(whitespace
);
2143 /** Inserts text, replacing \\t tab chars (@c 0x9) and \\n newline chars (@c 0xA)
2144 * accordingly for the document.
2145 * - Leading tabs are replaced with the correct indentation.
2146 * - Non-leading tabs are replaced with spaces (except when using 'Tabs' indent type).
2147 * - Newline chars are replaced with the correct line ending string.
2148 * This is very useful for inserting code without having to handle the indent
2149 * type yourself (Tabs & Spaces mode can be tricky).
2150 * @param editor Editor.
2151 * @param text Intended as e.g. @c "if (foo)\n\tbar();".
2152 * @param insert_pos Document position to insert text at.
2153 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2154 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2155 * -1 to read the indent size from the line with @a insert_pos on it.
2156 * @param replace_newlines Whether to replace newlines. If
2157 * newlines have been replaced already, this should be false, to avoid errors e.g. on Windows.
2158 * @warning Make sure all \\t tab chars in @a text are intended as indent widths or alignment,
2159 * not hard tabs, as those won't be preserved.
2160 * @note This doesn't scroll the cursor in view afterwards. **/
2161 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2162 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2164 ScintillaObject
*sci
= editor
->sci
;
2165 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2169 const gchar cur_marker
[] = "__GEANY_CURSOR_MARKER__";
2170 const gchar
*eol
= editor_get_eol_char(editor
);
2171 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2173 g_return_if_fail(text
);
2174 g_return_if_fail(editor
!= NULL
);
2175 g_return_if_fail(insert_pos
>= 0);
2177 buf
= g_string_new(text
);
2179 if (cursor_index
>= 0)
2180 g_string_insert(buf
, cursor_index
, cur_marker
); /* remember cursor pos */
2182 if (newline_indent_size
== -1)
2184 /* count indent size up to insert_pos instead of asking sci
2185 * because there may be spaces after it */
2186 gchar
*tmp
= sci_get_line(sci
, line_start
);
2187 gint idx
= insert_pos
- sci_get_position_from_line(sci
, line_start
);
2189 newline_indent_size
= count_indent_size(editor
, tmp
);
2193 /* Add line indents (in spaces) */
2194 if (newline_indent_size
> 0)
2196 whitespace
= g_strnfill(newline_indent_size
, ' ');
2197 setptr(whitespace
, g_strconcat(eol
, whitespace
, NULL
));
2198 utils_string_replace_all(buf
, eol
, whitespace
);
2202 /* transform line endings */
2203 if (replace_newlines
)
2204 utils_string_replace_all(buf
, "\n", eol
);
2206 /* transform leading tabs into indent widths (in spaces) */
2207 whitespace
= g_strnfill(iprefs
->width
, ' ');
2208 replace_leading_tabs(buf
, whitespace
);
2209 /* remaining tabs are for alignment */
2210 if (iprefs
->type
!= GEANY_INDENT_TYPE_TABS
)
2211 utils_string_replace_all(buf
, "\t", whitespace
);
2214 sci_start_undo_action(sci
);
2216 if (cursor_index
>= 0)
2218 gint idx
= utils_strpos(buf
->str
, cur_marker
);
2220 g_string_erase(buf
, idx
, strlen(cur_marker
));
2222 sci_insert_text(sci
, insert_pos
, buf
->str
);
2223 sci_set_current_position(sci
, insert_pos
+ idx
, FALSE
);
2226 sci_insert_text(sci
, insert_pos
, buf
->str
);
2228 /* fixup indentation (very useful for Tabs & Spaces indent type) */
2229 line_end
= sci_get_line_from_position(sci
, insert_pos
+ buf
->len
);
2230 fix_line_indents(editor
, line_start
, line_end
);
2231 snippet_cursor_insert_pos
= sci_get_current_position(sci
);
2233 sci_end_undo_action(sci
);
2234 g_string_free(buf
, TRUE
);
2238 /* Move the cursor to the next specified cursor position in an inserted snippet.
2239 * Can, and should, be optimized to give better results */
2240 void editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2242 ScintillaObject
*sci
= editor
->sci
;
2243 gint current_pos
= sci_get_current_position(sci
);
2245 if (snippet_offsets
&& !g_queue_is_empty(snippet_offsets
))
2249 offset
= GPOINTER_TO_INT(g_queue_pop_head(snippet_offsets
));
2250 if (current_pos
> snippet_cursor_insert_pos
)
2251 snippet_cursor_insert_pos
= offset
+ current_pos
;
2253 snippet_cursor_insert_pos
+= offset
;
2255 sci_set_current_position(sci
, snippet_cursor_insert_pos
, FALSE
);
2264 static gssize
snippets_make_replacements(GeanyEditor
*editor
, GString
*pattern
,
2267 gssize cur_index
= -1;
2269 gint i
, tmp_pos
, whitespace_len
, nl_count
= 0;
2270 GHashTable
*specials
;
2271 GList
*temp_list
= NULL
;
2272 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
2273 gint cursor_steps
, old_cursor
= 0;
2275 /* replace 'special' completions */
2276 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2277 if (G_LIKELY(specials
!= NULL
))
2279 /* ugly hack using global_pattern */
2280 snippets_global_pattern
= pattern
;
2281 g_hash_table_foreach(specials
, snippets_replace_specials
, NULL
);
2284 /* replace any template {foo} wildcards */
2285 templates_replace_common(pattern
, editor
->document
->file_name
, editor
->document
->file_type
, NULL
);
2287 /* transform other wildcards */
2288 /* convert to %newlines%, else we get endless loops */
2289 utils_string_replace_all(pattern
, "\n", "%newline%");
2291 /* if spaces are used, replaces all tabs with %ws%, which is later replaced
2292 * by real whitespace characters
2293 * otherwise replace all %ws% by \t, which will be replaced later by tab
2295 * this makes seperating between tab and spaces intentation pretty easy */
2296 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
2297 utils_string_replace_all(pattern
, "\t", "%ws%");
2299 utils_string_replace_all(pattern
, "%ws%", "\t");
2301 whitespace
= g_strnfill(iprefs
->width
, ' '); /* use spaces for indentation, will be fixed up */
2302 whitespace_len
= strlen(whitespace
);
2304 while ((cursor_steps
= utils_strpos(pattern
->str
, "%cursor%")) >= 0)
2306 /* replace every %newline% (up to next %cursor%) with EOL,
2307 * and update cursor_steps after */
2308 while ((tmp_pos
= utils_strpos(pattern
->str
, "%newline%")) < cursor_steps
&& tmp_pos
!= -1)
2311 utils_string_replace_first(pattern
, "%newline%", editor_get_eol_char(editor
));
2312 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2314 /* replace every %ws% (up to next %cursor%) with whitespaces,
2315 * and update cursor_steps after */
2316 while ((tmp_pos
= utils_strpos(pattern
->str
, "%ws%")) < cursor_steps
&& tmp_pos
!= -1)
2318 utils_string_replace_first(pattern
, "%ws%", whitespace
);
2319 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2321 /* finally replace the next %cursor% */
2322 utils_string_replace_first(pattern
, "%cursor%", "");
2324 /* modify cursor_steps to take indentation count and type into account */
2326 /* We're saving the relative offset to each cursor position in a simple
2327 * linked list, including indentations between them. */
2330 cursor_steps
+= (nl_count
* indent_size
);
2331 temp_list
= g_list_append(temp_list
, GINT_TO_POINTER(cursor_steps
- old_cursor
));
2336 cur_index
= cursor_steps
;
2338 old_cursor
= cursor_steps
;
2340 /* replace remaining %ws% and %newline% which may occur after the last %cursor% */
2341 utils_string_replace_all(pattern
, "%newline%", editor_get_eol_char(editor
));
2342 utils_string_replace_all(pattern
, "%ws%", whitespace
);
2346 /* Bug: {ob}pc{cb} will be replaced by % too */
2347 templates_replace_valist(pattern
, "{pc}", "%", NULL
);
2349 /* We put the cursor positions for the most recent
2350 * parsed snippet first, followed by any remaining positions */
2356 foreach_list(node
, temp_list
)
2357 g_queue_push_nth(snippet_offsets
, node
->data
, i
++);
2359 /* limit length of queue */
2360 while (g_queue_get_length(snippet_offsets
) > 20)
2361 g_queue_pop_tail(snippet_offsets
);
2363 g_list_free(temp_list
);
2366 cur_index
= pattern
->len
;
2372 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2374 ScintillaObject
*sci
= editor
->sci
;
2377 gssize cur_index
= -1;
2379 gint ft_id
= FILETYPE_ID(editor
->document
->file_type
);
2381 str
= g_strdup(word
);
2383 pattern
= g_string_new(snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
));
2384 if (pattern
== NULL
|| pattern
->len
== 0)
2387 g_string_free(pattern
, TRUE
);
2391 read_indent(editor
, pos
);
2393 /* remove the typed word, it will be added again by the used auto completion
2394 * (not really necessary but this makes the auto completion more flexible,
2395 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2396 str_len
= strlen(str
);
2397 sci_set_selection_start(sci
, pos
- str_len
);
2398 sci_set_selection_end(sci
, pos
);
2399 sci_replace_sel(sci
, "");
2400 pos
-= str_len
; /* pos has changed while deleting */
2402 cur_index
= snippets_make_replacements(editor
, pattern
, strlen(indent
));
2404 /* finally insert the text and set the cursor */
2405 editor_insert_text_block(editor
, pattern
->str
, pos
, cur_index
, -1, FALSE
);
2406 sci_scroll_caret(sci
);
2409 g_string_free(pattern
, TRUE
);
2414 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2416 gint line
= sci_get_line_from_position(sci
, pos
);
2419 /* skip any trailing spaces */
2422 c
= sci_get_char_at(sci
, pos
);
2423 if (c
== ' ' || c
== '\t')
2429 return (pos
== sci_get_line_end_position(sci
, line
));
2433 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2435 gboolean result
= FALSE
;
2438 ScintillaObject
*sci
;
2440 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2443 if (sci_has_selection(sci
))
2445 /* return if we are editing an existing line (chars on right of cursor) */
2446 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2447 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_space
&&
2448 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2451 wc
= snippets_find_completion_by_name("Special", "wordchars");
2452 word
= editor_read_word_stem(editor
, pos
, wc
);
2454 /* prevent completion of "for " */
2456 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2458 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2459 result
= snippets_complete_constructs(editor
, pos
, word
);
2460 sci_end_undo_action(sci
);
2462 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2468 void editor_show_macro_list(GeanyEditor
*editor
)
2472 if (editor
== NULL
|| editor
->document
->file_type
== NULL
)
2475 words
= symbols_get_macro_list(editor
->document
->file_type
->lang
);
2479 SSM(editor
->sci
, SCI_USERLISTSHOW
, 1, (sptr_t
) words
->str
);
2480 g_string_free(words
, TRUE
);
2484 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2486 ScintillaObject
*sci
= editor
->sci
;
2487 gchar
*to_insert
= NULL
;
2491 const gchar
*gt
= ">";
2492 /* if there is already a '>' behind the cursor, don't add it */
2493 if (sci_get_char_at(sci
, pos
) == '>')
2496 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2499 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2501 sci_start_undo_action(sci
);
2502 sci_replace_sel(sci
, to_insert
);
2505 sci_set_selection(sci
, pos
, pos
);
2506 if (utils_str_equal(tag_name
, "table"))
2507 auto_table(editor
, pos
);
2509 sci_end_undo_action(sci
);
2515 * (stolen from anjuta and heavily modified)
2516 * This routine will auto complete XML or HTML tags that are still open by closing them
2517 * @param ch The character we are dealing with, currently only works with the '>' character
2518 * @return True if handled, false otherwise
2520 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2522 ScintillaObject
*sci
= editor
->sci
;
2523 gint lexer
= sci_get_lexer(sci
);
2525 gchar
*str_found
, sel
[512];
2526 gboolean result
= FALSE
;
2528 /* If the user has turned us off, quit now.
2529 * This may make sense only in certain languages */
2530 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2533 /* return if we are inside any embedded script */
2534 style
= sci_get_style_at(sci
, pos
);
2535 if (style
> SCE_H_XCCOMMENT
&& ! is_string_style(lexer
, style
))
2538 /* if ch is /, check for </, else quit */
2539 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2542 /* Grab the last 512 characters or so */
2543 min
= pos
- (sizeof(sel
) - 1);
2544 if (min
< 0) min
= 0;
2547 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2549 sci_get_text_range(sci
, min
, pos
, sel
);
2550 sel
[sizeof(sel
) - 1] = '\0';
2552 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2553 /* User typed something like "<br/>" */
2556 str_found
= utils_find_open_xml_tag(sel
, pos
- min
, (ch
== '/'));
2558 /* when found string is something like br, img or another short tag, quit */
2559 if (utils_str_equal(str_found
, "br")
2560 || utils_str_equal(str_found
, "hr")
2561 || utils_str_equal(str_found
, "img")
2562 || utils_str_equal(str_found
, "base")
2563 || utils_str_equal(str_found
, "basefont") /* < or not < */
2564 || utils_str_equal(str_found
, "frame")
2565 || utils_str_equal(str_found
, "input")
2566 || utils_str_equal(str_found
, "link")
2567 || utils_str_equal(str_found
, "area")
2568 || utils_str_equal(str_found
, "meta"))
2572 else if (NZV(str_found
))
2574 insert_closing_tag(editor
, pos
, ch
, str_found
);
2582 /* like sci_get_line_indentation(), but for a string. */
2583 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2586 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2589 g_return_val_if_fail(base_indent
, 0);
2591 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2607 static void auto_table(GeanyEditor
*editor
, gint pos
)
2609 ScintillaObject
*sci
= editor
->sci
;
2612 const gchar
*indent_str
;
2614 if (sci_get_lexer(sci
) != SCLEX_HTML
) return;
2616 read_indent(editor
, pos
);
2617 indent_pos
= sci_get_line_indent_position(sci
, sci_get_line_from_position(sci
, pos
));
2618 if ((pos
- 7) != indent_pos
) /* 7 == strlen("<table>") */
2624 /* find the start of the <table tag */
2626 while (i
<= pos
&& sci_get_char_at(sci
, pos
- i
) != '<') i
++;
2627 /* add all non whitespace before the tag to the indent string */
2628 while ((pos
- i
) != indent_pos
&& x
< sizeof(indent
) - 1)
2636 if (! editor
->auto_indent
)
2641 table
= g_strconcat("\n", indent_str
, "<tr>\n",
2642 indent_str
, indent_str
, "<td> </td>\n",
2643 indent_str
, "</tr>\n",
2645 editor_insert_text_block(editor
, table
, pos
, -1,
2646 count_indent_size(editor
, indent
), TRUE
);
2651 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2654 gchar
*str_begin
, *str_end
, *co
, *cc
;
2657 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2659 eol
= editor_get_eol_char(editor
);
2660 co
= editor
->document
->file_type
->comment_open
;
2661 cc
= editor
->document
->file_type
->comment_close
;
2662 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2663 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2665 /* insert the comment strings */
2666 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2667 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2668 sci_insert_text(editor
->sci
, line_len
, str_end
);
2675 static void real_uncomment_multiline(GeanyEditor
*editor
)
2677 /* find the beginning of the multi line comment */
2678 gint pos
, line
, len
, x
;
2682 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2683 doc
= editor
->document
;
2685 /* remove comment open chars */
2686 pos
= document_find_text(doc
, doc
->file_type
->comment_open
, 0, TRUE
, FALSE
, NULL
);
2687 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2689 /* check whether the line is empty and can be deleted */
2690 line
= sci_get_line_from_position(editor
->sci
, pos
);
2691 len
= sci_get_line_length(editor
->sci
, line
);
2692 linebuf
= sci_get_line(editor
->sci
, line
);
2694 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2695 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2698 /* remove comment close chars */
2699 pos
= document_find_text(doc
, doc
->file_type
->comment_close
, 0, FALSE
, FALSE
, NULL
);
2700 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2702 /* check whether the line is empty and can be deleted */
2703 line
= sci_get_line_from_position(editor
->sci
, pos
);
2704 len
= sci_get_line_length(editor
->sci
, line
);
2705 linebuf
= sci_get_line(editor
->sci
, line
);
2707 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2708 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2713 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2715 gint lexer
= sci_get_lexer(editor
->sci
);
2718 /* List only those lexers which support multi line comments */
2724 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2725 style_comment
= SCE_HPHP_COMMENT
;
2727 style_comment
= SCE_H_COMMENT
;
2730 case SCLEX_HASKELL
: style_comment
= SCE_HA_COMMENTBLOCK
; break;
2731 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2732 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2733 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2734 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2735 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2736 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2737 default: style_comment
= SCE_C_COMMENT
;
2740 return style_comment
;
2744 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2745 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2746 * it returns just 1 */
2747 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2749 gint first_line
, last_line
;
2750 gint x
, i
, line_start
, line_len
;
2751 gint sel_start
, sel_end
;
2754 gchar sel
[256], *co
, *cc
;
2755 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2758 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
2761 { /* use selection or current line */
2762 sel_start
= sci_get_selection_start(editor
->sci
);
2763 sel_end
= sci_get_selection_end(editor
->sci
);
2765 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
2766 /* Find the last line with chars selected (not EOL char) */
2767 last_line
= sci_get_line_from_position(editor
->sci
,
2768 sel_end
- editor_get_eol_char_len(editor
));
2769 last_line
= MAX(first_line
, last_line
);
2773 first_line
= last_line
= line
;
2774 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
2777 ft
= editor
->document
->file_type
;
2779 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
2780 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
2781 if (ft
->id
== GEANY_FILETYPES_PHP
)
2783 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2784 ft
= filetypes
[GEANY_FILETYPES_XML
];
2787 co
= ft
->comment_open
;
2788 cc
= ft
->comment_close
;
2792 co_len
= strlen(co
);
2796 sci_start_undo_action(editor
->sci
);
2798 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
2802 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2803 line_len
= sci_get_line_length(editor
->sci
, i
);
2806 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
2809 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2810 sel
[buf_len
] = '\0';
2812 while (isspace(sel
[x
])) x
++;
2814 /* to skip blank lines */
2815 if (x
< line_len
&& sel
[x
] != '\0')
2817 /* use single line comment */
2818 if (cc
== NULL
|| strlen(cc
) == 0)
2824 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2825 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
2826 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
2833 if (strncmp(sel
+ x
, co
, co_len
) != 0)
2837 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
2838 sci_replace_sel(editor
->sci
, "");
2841 /* use multi line comment */
2846 /* skip lines which are already comments */
2847 style_comment
= get_multiline_comment_style(editor
, line_start
);
2848 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
2850 real_uncomment_multiline(editor
);
2854 /* break because we are already on the last line */
2860 sci_end_undo_action(editor
->sci
);
2862 /* restore selection if there is one
2863 * but don't touch the selection if caller is editor_do_comment_toggle */
2864 if (! toggle
&& sel_start
< sel_end
)
2868 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
2869 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
2873 gint eol_len
= editor_get_eol_char_len(editor
);
2874 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
2875 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
2883 void editor_do_comment_toggle(GeanyEditor
*editor
)
2885 gint first_line
, last_line
;
2886 gint x
, i
, line_start
, line_len
, first_line_start
;
2887 gint sel_start
, sel_end
;
2888 gint count_commented
= 0, count_uncommented
= 0;
2889 gchar sel
[256], *co
, *cc
;
2890 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2891 gboolean first_line_was_comment
= FALSE
;
2893 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2896 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2898 sel_start
= sci_get_selection_start(editor
->sci
);
2899 sel_end
= sci_get_selection_end(editor
->sci
);
2901 ft
= editor
->document
->file_type
;
2903 first_line
= sci_get_line_from_position(editor
->sci
,
2904 sci_get_selection_start(editor
->sci
));
2905 /* Find the last line with chars selected (not EOL char) */
2906 last_line
= sci_get_line_from_position(editor
->sci
,
2907 sci_get_selection_end(editor
->sci
) - editor_get_eol_char_len(editor
));
2908 last_line
= MAX(first_line
, last_line
);
2910 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
2911 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
2912 if (ft
->id
== GEANY_FILETYPES_PHP
)
2914 if (! is_style_php(sci_get_style_at(editor
->sci
, first_line_start
)))
2915 ft
= filetypes
[GEANY_FILETYPES_XML
];
2918 co
= ft
->comment_open
;
2919 cc
= ft
->comment_close
;
2923 co_len
= strlen(co
);
2927 sci_start_undo_action(editor
->sci
);
2929 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
2933 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2934 line_len
= sci_get_line_length(editor
->sci
, i
);
2937 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
2940 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2941 sel
[buf_len
] = '\0';
2943 while (isspace(sel
[x
])) x
++;
2945 /* use single line comment */
2946 if (cc
== NULL
|| strlen(cc
) == 0)
2948 gboolean do_continue
= FALSE
;
2951 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
2952 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
2957 if (do_continue
&& i
== first_line
)
2958 first_line_was_comment
= TRUE
;
2962 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
2966 /* we are still here, so the above lines were not already comments, so comment it */
2967 editor_do_comment(editor
, i
, TRUE
, TRUE
);
2970 /* use multi line comment */
2975 /* skip lines which are already comments */
2976 style_comment
= get_multiline_comment_style(editor
, line_start
);
2977 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
2979 real_uncomment_multiline(editor
);
2980 count_uncommented
++;
2984 real_comment_multiline(editor
, line_start
, last_line
);
2988 /* break because we are already on the last line */
2994 sci_end_undo_action(editor
->sci
);
2998 /* restore selection if there is one */
2999 if (sel_start
< sel_end
)
3003 gint a
= (first_line_was_comment
) ? - co_len
: co_len
;
3005 /* don't modify sel_start when the selection starts within indentation */
3006 read_indent(editor
, sel_start
);
3007 if ((sel_start
- first_line_start
) <= (gint
) strlen(indent
))
3010 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
3011 sci_set_selection_end(editor
->sci
, sel_end
+
3012 (count_commented
* co_len
) - (count_uncommented
* co_len
));
3016 gint eol_len
= editor_get_eol_char_len(editor
);
3017 if (count_uncommented
> 0)
3019 sci_set_selection_start(editor
->sci
, sel_start
- co_len
+ eol_len
);
3020 sci_set_selection_end(editor
->sci
, sel_end
- co_len
+ eol_len
);
3022 else if (count_commented
> 0)
3024 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
- eol_len
);
3025 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
- eol_len
);
3029 else if (count_uncommented
> 0)
3031 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
3032 sci_set_current_position(editor
->sci
, sel_start
- co_len
+ eol_len
, TRUE
);
3034 else if (count_commented
> 0)
3036 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
3037 sci_set_current_position(editor
->sci
, sel_start
+ co_len
- eol_len
, TRUE
);
3042 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
3043 void editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
)
3045 gint first_line
, last_line
;
3046 gint x
, i
, line_start
, line_len
;
3047 gint sel_start
, sel_end
, co_len
;
3048 gchar sel
[256], *co
, *cc
;
3049 gboolean break_loop
= FALSE
, single_line
= FALSE
;
3052 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
3055 { /* use selection or current line */
3056 sel_start
= sci_get_selection_start(editor
->sci
);
3057 sel_end
= sci_get_selection_end(editor
->sci
);
3059 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3060 /* Find the last line with chars selected (not EOL char) */
3061 last_line
= sci_get_line_from_position(editor
->sci
,
3062 sel_end
- editor_get_eol_char_len(editor
));
3063 last_line
= MAX(first_line
, last_line
);
3067 first_line
= last_line
= line
;
3068 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
3071 ft
= editor
->document
->file_type
;
3073 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
3074 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3075 if (ft
->id
== GEANY_FILETYPES_PHP
)
3077 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
3078 ft
= filetypes
[GEANY_FILETYPES_XML
];
3081 co
= ft
->comment_open
;
3082 cc
= ft
->comment_close
;
3086 co_len
= strlen(co
);
3090 sci_start_undo_action(editor
->sci
);
3092 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3096 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3097 line_len
= sci_get_line_length(editor
->sci
, i
);
3100 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
3103 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3104 sel
[buf_len
] = '\0';
3106 while (isspace(sel
[x
])) x
++;
3108 /* to skip blank lines */
3109 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3111 /* use single line comment */
3112 if (cc
== NULL
|| strlen(cc
) == 0)
3114 gint start
= line_start
;
3117 if (ft
->comment_use_indent
)
3118 start
= line_start
+ x
;
3122 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3123 sci_insert_text(editor
->sci
, start
, text
);
3127 sci_insert_text(editor
->sci
, start
, co
);
3129 /* use multi line comment */
3134 /* skip lines which are already comments */
3135 style_comment
= get_multiline_comment_style(editor
, line_start
);
3136 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3139 real_comment_multiline(editor
, line_start
, last_line
);
3141 /* break because we are already on the last line */
3147 sci_end_undo_action(editor
->sci
);
3149 /* restore selection if there is one
3150 * but don't touch the selection if caller is editor_do_comment_toggle */
3151 if (! toggle
&& sel_start
< sel_end
)
3155 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3156 sci_set_selection_end(editor
->sci
, sel_end
+ ((i
- first_line
) * co_len
));
3160 gint eol_len
= editor_get_eol_char_len(editor
);
3161 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3162 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3168 static gboolean brace_timeout_active
= FALSE
;
3170 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3172 GeanyDocument
*doc
= document_get_current();
3173 GeanyEditor
*editor
;
3174 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3175 gint end_pos
, cur_pos
;
3177 brace_timeout_active
= FALSE
;
3181 editor
= doc
->editor
;
3182 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3184 if (cur_pos
!= brace_pos
)
3187 if (cur_pos
!= brace_pos
)
3189 /* we have moved past the original brace_pos, but after the timeout
3190 * we may now be on a new brace, so check again */
3191 editor_highlight_braces(editor
, cur_pos
);
3195 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3197 editor_highlight_braces(editor
, cur_pos
);
3200 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3204 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3205 sci_get_col_from_position(editor
->sci
, end_pos
));
3206 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3207 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3211 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3212 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3218 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3220 gint brace_pos
= cur_pos
- 1;
3222 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3223 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3225 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3228 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3233 if (!brace_timeout_active
)
3235 brace_timeout_active
= TRUE
;
3236 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3237 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3242 static gboolean
in_block_comment(gint lexer
, gint style
)
3247 return (style
== SCE_C_COMMENT
||
3248 style
== SCE_C_COMMENTDOC
);
3251 return (style
== SCE_PAS_COMMENT
||
3252 style
== SCE_PAS_COMMENT2
);
3255 return (style
== SCE_D_COMMENT
||
3256 style
== SCE_D_COMMENTDOC
||
3257 style
== SCE_D_COMMENTNESTED
);
3260 return (style
== SCE_HPHP_COMMENT
);
3263 return (style
== SCE_CSS_COMMENT
);
3271 static gboolean
is_comment_char(gchar c
, gint lexer
)
3273 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3283 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3285 ScintillaObject
*sci
= editor
->sci
;
3286 gint indent_pos
, style
;
3287 gint lexer
= sci_get_lexer(sci
);
3289 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3290 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3291 style
= sci_get_style_at(sci
, indent_pos
);
3292 if (!in_block_comment(lexer
, style
))
3295 /* Check whether the comment block continues on this line */
3296 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3297 if (sci_get_style_at(sci
, indent_pos
) == style
)
3299 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3300 /* the type of comment, '*' (C/C++/Java), '+' and the others (D) */
3301 gchar
*continuation
= "*";
3302 gchar
*whitespace
= ""; /* to hold whitespace if needed */
3304 gint len
= strlen(previous_line
);
3307 /* find and stop at end of multi line comment */
3309 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3310 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3312 gint indent_len
, indent_width
;
3314 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3315 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3316 indent_width
= editor_get_indent_prefs(editor
)->width
;
3318 /* if there is one too many spaces, delete the last space,
3319 * to return to the indent used before the multiline comment was started. */
3320 if (indent_len
% indent_width
== 1)
3321 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3322 g_free(previous_line
);
3325 /* check whether we are on the second line of multi line comment */
3327 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3330 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3331 { /* we are on the second line of a multi line comment, so we have to insert white space */
3335 if (G_UNLIKELY(style
== SCE_D_COMMENTNESTED
))
3336 continuation
= "+"; /* for nested comments in D */
3338 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3339 sci_add_text(sci
, result
);
3342 g_free(previous_line
);
3347 /* Checks whether the given style is a string for the given lexer.
3348 * It doesn't handle LEX_HTML, this should be done by the caller.
3349 * Returns true if the style is a string, FALSE otherwise.
3351 * Don't forget STRINGEOL, to prevent completion whilst typing a string with no closing char.
3353 static gboolean
is_string_style(gint lexer
, gint style
)
3358 return (style
== SCE_C_CHARACTER
||
3359 style
== SCE_C_STRING
||
3360 style
== SCE_C_STRINGEOL
);
3363 return (style
== SCE_PAS_CHARACTER
||
3364 style
== SCE_PAS_STRING
||
3365 style
== SCE_PAS_STRINGEOL
);
3368 return (style
== SCE_D_STRING
||
3369 style
== SCE_D_STRINGEOL
||
3370 style
== SCE_D_CHARACTER
||
3371 style
== SCE_D_STRINGB
||
3372 style
== SCE_D_STRINGR
);
3375 return (style
== SCE_P_STRING
||
3376 style
== SCE_P_TRIPLE
||
3377 style
== SCE_P_TRIPLEDOUBLE
||
3378 style
== SCE_P_CHARACTER
||
3379 style
== SCE_P_STRINGEOL
);
3383 return (style
== SCE_F_STRING1
||
3384 style
== SCE_F_STRING2
||
3385 style
== SCE_F_STRINGEOL
);
3388 return (/*style == SCE_PL_STRING ||*/ /* may want variable autocompletion "$(foo)" */
3389 style
== SCE_PL_CHARACTER
||
3390 style
== SCE_PL_HERE_DELIM
||
3391 style
== SCE_PL_HERE_Q
||
3392 style
== SCE_PL_HERE_QQ
||
3393 style
== SCE_PL_HERE_QX
||
3394 style
== SCE_PL_POD
||
3395 style
== SCE_PL_STRING_Q
||
3396 style
== SCE_PL_STRING_QQ
||
3397 style
== SCE_PL_STRING_QX
||
3398 style
== SCE_PL_STRING_QR
||
3399 style
== SCE_PL_STRING_QW
||
3400 style
== SCE_PL_POD_VERB
);
3403 return (style
== SCE_R_STRING
);
3406 return (style
== SCE_RB_CHARACTER
||
3407 style
== SCE_RB_STRING
||
3408 style
== SCE_RB_HERE_DELIM
||
3409 style
== SCE_RB_HERE_Q
||
3410 style
== SCE_RB_HERE_QQ
||
3411 style
== SCE_RB_HERE_QX
||
3412 style
== SCE_RB_POD
);
3415 return (style
== SCE_SH_STRING
);
3418 return (style
== SCE_SQL_STRING
);
3421 return (style
== SCE_TCL_IN_QUOTE
);
3424 return (style
== SCE_LUA_LITERALSTRING
||
3425 style
== SCE_LUA_CHARACTER
||
3426 style
== SCE_LUA_STRINGEOL
||
3427 style
== SCE_LUA_STRING
);
3430 return (style
== SCE_HA_CHARACTER
||
3431 style
== SCE_HA_STRING
);
3433 case SCLEX_FREEBASIC
:
3434 return (style
== SCE_B_STRING
||
3435 style
== SCE_B_STRINGEOL
);
3438 return (style
== SCE_MATLAB_STRING
||
3439 style
== SCE_MATLAB_DOUBLEQUOTESTRING
);
3443 style
== SCE_HBA_STRING
||
3444 style
== SCE_HBA_STRINGEOL
||
3445 style
== SCE_HB_STRING
||
3446 style
== SCE_HB_STRINGEOL
||
3447 style
== SCE_H_CDATA
||
3448 style
== SCE_H_DOUBLESTRING
||
3449 style
== SCE_HJA_DOUBLESTRING
||
3450 style
== SCE_HJA_SINGLESTRING
||
3451 style
== SCE_HJA_STRINGEOL
||
3452 style
== SCE_HJ_DOUBLESTRING
||
3453 style
== SCE_HJ_SINGLESTRING
||
3454 style
== SCE_HJ_STRINGEOL
||
3455 style
== SCE_HPA_CHARACTER
||
3456 style
== SCE_HPA_STRING
||
3457 style
== SCE_HPA_TRIPLE
||
3458 style
== SCE_HPA_TRIPLEDOUBLE
||
3459 style
== SCE_HP_CHARACTER
||
3460 style
== SCE_HPHP_HSTRING
||
3461 style
== SCE_HPHP_HSTRING
|| /* HSTRING is a heredoc */
3462 style
== SCE_HPHP_HSTRING_VARIABLE
||
3463 style
== SCE_HPHP_SIMPLESTRING
||
3464 style
== SCE_HPHP_SIMPLESTRING
||
3465 style
== SCE_HP_STRING
||
3466 style
== SCE_HP_TRIPLE
||
3467 style
== SCE_HP_TRIPLEDOUBLE
||
3468 style
== SCE_H_SGML_DOUBLESTRING
||
3469 style
== SCE_H_SGML_SIMPLESTRING
||
3470 style
== SCE_H_SINGLESTRING
);
3473 return (style
== SCE_CMAKE_STRINGDQ
||
3474 style
== SCE_CMAKE_STRINGLQ
||
3475 style
== SCE_CMAKE_STRINGRQ
||
3476 style
== SCE_CMAKE_STRINGVAR
);
3479 return (style
== SCE_NSIS_STRINGDQ
||
3480 style
== SCE_NSIS_STRINGLQ
||
3481 style
== SCE_NSIS_STRINGRQ
||
3482 style
== SCE_NSIS_STRINGVAR
);
3485 return (style
== SCE_ADA_CHARACTER
||
3486 style
== SCE_ADA_STRING
||
3487 style
== SCE_ADA_CHARACTEREOL
||
3488 style
== SCE_ADA_STRINGEOL
);
3494 /* Checks whether the given style is a comment for the given lexer.
3495 * It doesn't handle LEX_HTML, this should be done by the caller.
3496 * Returns true if the style is a comment, FALSE otherwise.
3498 static gboolean
is_comment_style(gint lexer
, gint style
)
3503 return (style
== SCE_C_COMMENT
||
3504 style
== SCE_C_COMMENTLINE
||
3505 style
== SCE_C_COMMENTDOC
||
3506 style
== SCE_C_COMMENTLINEDOC
||
3507 style
== SCE_C_COMMENTDOCKEYWORD
||
3508 style
== SCE_C_COMMENTDOCKEYWORDERROR
);
3511 return (style
== SCE_PAS_COMMENT
||
3512 style
== SCE_PAS_COMMENT2
||
3513 style
== SCE_PAS_COMMENTLINE
);
3516 return (style
== SCE_D_COMMENT
||
3517 style
== SCE_D_COMMENTLINE
||
3518 style
== SCE_D_COMMENTDOC
||
3519 style
== SCE_D_COMMENTNESTED
||
3520 style
== SCE_D_COMMENTLINEDOC
||
3521 style
== SCE_D_COMMENTDOCKEYWORD
||
3522 style
== SCE_D_COMMENTDOCKEYWORDERROR
);
3525 return (style
== SCE_P_COMMENTLINE
||
3526 style
== SCE_P_COMMENTBLOCK
);
3530 return (style
== SCE_F_COMMENT
);
3533 return (style
== SCE_PL_COMMENTLINE
);
3535 case SCLEX_PROPERTIES
:
3536 return (style
== SCE_PROPS_COMMENT
);
3539 return (style
== SCE_PO_COMMENT
);
3542 return (style
== SCE_L_COMMENT
);
3544 case SCLEX_MAKEFILE
:
3545 return (style
== SCE_MAKE_COMMENT
);
3548 return (style
== SCE_RB_COMMENTLINE
);
3551 return (style
== SCE_SH_COMMENTLINE
);
3554 return (style
== SCE_R_COMMENT
);
3557 return (style
== SCE_SQL_COMMENT
||
3558 style
== SCE_SQL_COMMENTLINE
||
3559 style
== SCE_SQL_COMMENTDOC
);
3562 return (style
== SCE_TCL_COMMENT
||
3563 style
== SCE_TCL_COMMENTLINE
||
3564 style
== SCE_TCL_COMMENT_BOX
||
3565 style
== SCE_TCL_BLOCK_COMMENT
);
3568 return (style
== SCE_MATLAB_COMMENT
);
3571 return (style
== SCE_LUA_COMMENT
||
3572 style
== SCE_LUA_COMMENTLINE
||
3573 style
== SCE_LUA_COMMENTDOC
);
3576 return (style
== SCE_HA_COMMENTLINE
||
3577 style
== SCE_HA_COMMENTBLOCK
||
3578 style
== SCE_HA_COMMENTBLOCK2
||
3579 style
== SCE_HA_COMMENTBLOCK3
);
3581 case SCLEX_FREEBASIC
:
3582 return (style
== SCE_B_COMMENT
);
3585 return (style
== SCE_YAML_COMMENT
);
3589 style
== SCE_HBA_COMMENTLINE
||
3590 style
== SCE_HB_COMMENTLINE
||
3591 style
== SCE_H_COMMENT
||
3592 style
== SCE_HJA_COMMENT
||
3593 style
== SCE_HJA_COMMENTDOC
||
3594 style
== SCE_HJA_COMMENTLINE
||
3595 style
== SCE_HJ_COMMENT
||
3596 style
== SCE_HJ_COMMENTDOC
||
3597 style
== SCE_HJ_COMMENTLINE
||
3598 style
== SCE_HPA_COMMENTLINE
||
3599 style
== SCE_HP_COMMENTLINE
||
3600 style
== SCE_HPHP_COMMENT
||
3601 style
== SCE_HPHP_COMMENTLINE
||
3602 style
== SCE_H_SGML_COMMENT
);
3605 return (style
== SCE_CMAKE_COMMENT
);
3608 return (style
== SCE_NSIS_COMMENT
||
3609 style
== SCE_NSIS_COMMENTBOX
);
3612 return (style
== SCE_ADA_COMMENTLINE
||
3613 style
== SCE_NSIS_COMMENTBOX
);
3619 /* Checks whether the given style is normal code (not string, comment, preprocessor, etc).
3620 * It doesn't handle LEX_HTML, this should be done by the caller.
3622 static gboolean
is_code_style(gint lexer
, gint style
)
3627 if (style
== SCE_C_PREPROCESSOR
)
3631 return !(is_comment_style(lexer
, style
) ||
3632 is_string_style(lexer
, style
));
3637 static gboolean
editor_lexer_is_c_like(gint lexer
)
3652 /* Returns: -1 if lexer doesn't support type keywords */
3653 gint
editor_lexer_get_type_keyword_idx(gint lexer
)
3667 /* inserts a three-line comment at one line above current cursor position */
3668 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3674 gboolean have_multiline_comment
= FALSE
;
3677 g_return_if_fail(editor
!= NULL
&&
3678 editor
->document
->file_type
!= NULL
&& editor
->document
->file_type
->comment_open
!= NULL
);
3680 sci_start_undo_action(editor
->sci
);
3682 doc
= editor
->document
;
3684 if (doc
->file_type
->comment_close
!= NULL
&& strlen(doc
->file_type
->comment_close
) > 0)
3685 have_multiline_comment
= TRUE
;
3687 /* insert three lines one line above of the current position */
3688 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3689 pos
= sci_get_position_from_line(editor
->sci
, line
);
3691 /* use the indent on the current line but only when comment indentation is used
3692 * and we don't have multi line comment characters */
3693 if (editor
->auto_indent
&&
3694 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3696 read_indent(editor
, editor_info
.click_pos
);
3697 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3698 text_len
= strlen(text
);
3702 text
= g_strdup("\n\n\n");
3705 sci_insert_text(editor
->sci
, pos
, text
);
3708 /* select the inserted lines for commenting */
3709 sci_set_selection_start(editor
->sci
, pos
);
3710 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3712 editor_do_comment(editor
, -1, TRUE
, FALSE
);
3714 /* set the current position to the start of the first inserted line */
3715 pos
+= strlen(doc
->file_type
->comment_open
);
3717 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3718 if (have_multiline_comment
)
3721 pos
+= strlen(indent
);
3723 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3724 /* reset the selection */
3725 sci_set_anchor(editor
->sci
, pos
);
3727 sci_end_undo_action(editor
->sci
);
3731 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3732 * Scroll the view to make line appear at percent_of_view.
3733 * line can be -1 to use the current position. */
3734 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3736 gint vis1
, los
, delta
;
3739 g_return_if_fail(editor
!= NULL
);
3741 wid
= GTK_WIDGET(editor
->sci
);
3743 if (! wid
->window
|| ! gdk_window_is_viewable(wid
->window
))
3744 return; /* prevent gdk_window_scroll warning */
3747 line
= sci_get_current_line(editor
->sci
);
3749 /* sci 'visible line' != doc line number because of folding and line wrapping */
3750 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3751 * SCI_DOCLINEFROMVISIBLE for vis1. */
3752 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3753 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
3754 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3755 delta
= (line
- vis1
) - los
* percent_of_view
;
3756 sci_scroll_lines(editor
->sci
, delta
);
3757 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3761 /* creates and inserts one tab or whitespace of the amount of the tab width */
3762 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3765 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3767 g_return_if_fail(editor
!= NULL
);
3769 switch (iprefs
.type
)
3771 case GEANY_INDENT_TYPE_TABS
:
3772 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3774 case GEANY_INDENT_TYPE_SPACES
:
3775 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3776 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3779 text
= get_whitespace(&iprefs
, iprefs
.width
);
3780 sci_add_text(editor
->sci
, text
);
3785 void editor_select_word(GeanyEditor
*editor
)
3791 g_return_if_fail(editor
!= NULL
);
3793 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3794 start
= SSM(editor
->sci
, SCI_WORDSTARTPOSITION
, pos
, TRUE
);
3795 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, TRUE
);
3797 if (start
== end
) /* caret in whitespaces sequence */
3799 /* look forward but reverse the selection direction,
3800 * so the caret end up stay as near as the original position. */
3801 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, FALSE
);
3802 start
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, end
, TRUE
);
3807 sci_set_selection(editor
->sci
, start
, end
);
3811 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3812 * when those lines have no selection (cursor at start of line). */
3813 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3815 gint start
, end
, line
;
3817 g_return_if_fail(editor
!= NULL
);
3819 start
= sci_get_selection_start(editor
->sci
);
3820 end
= sci_get_selection_end(editor
->sci
);
3822 /* check if whole lines are already selected */
3823 if (! extra_line
&& start
!= end
&&
3824 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3825 sci_get_col_from_position(editor
->sci
, end
) == 0)
3828 line
= sci_get_line_from_position(editor
->sci
, start
);
3829 start
= sci_get_position_from_line(editor
->sci
, line
);
3831 line
= sci_get_line_from_position(editor
->sci
, end
);
3832 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3834 sci_set_selection(editor
->sci
, start
, end
);
3838 /* find the start or end of a paragraph by searching all lines in direction (UP or DOWN)
3839 * starting at the given line and return the found line or return -1 if called on an empty line */
3840 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3842 gboolean found_end
= FALSE
;
3844 gchar
*line_buf
, *x
;
3845 ScintillaObject
*sci
= editor
->sci
;
3847 /* first check current line and return -1 if it is empty to skip creating of a selection */
3848 line_buf
= x
= sci_get_line(sci
, line
);
3857 if (direction
== GTK_DIR_UP
)
3866 /* sci_get_line checks for sanity of the given line, sci_get_line always return a string
3867 * containing at least '\0' so no need to check for NULL */
3868 line_buf
= x
= sci_get_line(sci
, line
);
3870 /* check whether after skipping all whitespace we are at end of line and if so, assume
3871 * this line as end of paragraph */
3878 /* called on the first line but there is no previous line so return line 0 */
3887 void editor_select_paragraph(GeanyEditor
*editor
)
3889 gint pos_start
, pos_end
, line_start
, line_found
;
3891 g_return_if_fail(editor
!= NULL
);
3893 line_start
= SSM(editor
->sci
, SCI_LINEFROMPOSITION
,
3894 SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0), 0);
3896 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3897 if (line_found
== -1)
3900 /* find_paragraph_stop returns the emtpy line(previous to the real start of the paragraph),
3901 * so use the next line for selection start */
3905 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3907 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3908 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3910 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3914 /* simple indentation to indent the current line with the same indent as the previous one */
3915 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
3917 gint i
, sel_start
= 0, sel_end
= 0;
3919 /* get previous line and use it for read_indent to use that line
3920 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
3921 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
3923 for (i
= first_line
; i
<= last_line
; i
++)
3925 /* skip the first line or if the indentation of the previous and current line are equal */
3927 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
3928 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
3931 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3932 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3933 if (sel_start
< sel_end
)
3935 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
3936 sci_replace_sel(editor
->sci
, "");
3938 sci_insert_text(editor
->sci
, sel_start
, indent
);
3943 /* simple indentation to indent the current line with the same indent as the previous one */
3944 void editor_smart_line_indentation(GeanyEditor
*editor
, gint pos
)
3946 gint first_line
, last_line
;
3947 gint first_sel_start
, first_sel_end
;
3948 ScintillaObject
*sci
;
3950 g_return_if_fail(editor
!= NULL
);
3954 first_sel_start
= sci_get_selection_start(sci
);
3955 first_sel_end
= sci_get_selection_end(sci
);
3957 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
3958 /* Find the last line with chars selected (not EOL char) */
3959 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
3960 last_line
= MAX(first_line
, last_line
);
3963 pos
= first_sel_start
;
3965 sci_start_undo_action(sci
);
3967 smart_line_indentation(editor
, first_line
, last_line
);
3969 /* set cursor position if there was no selection */
3970 if (first_sel_start
== first_sel_end
)
3972 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
3974 /* use indent position as user may wish to change indentation afterwards */
3975 sci_set_current_position(sci
, indent_pos
, FALSE
);
3979 /* fully select all the lines affected */
3980 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
3981 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
3984 sci_end_undo_action(sci
);
3988 /* increase / decrease current line or selection by one space */
3989 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
3991 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
3992 gint sel_start
, sel_end
, first_line_offset
= 0;
3994 g_return_if_fail(editor
!= NULL
);
3996 sel_start
= sci_get_selection_start(editor
->sci
);
3997 sel_end
= sci_get_selection_end(editor
->sci
);
3999 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
4000 /* Find the last line with chars selected (not EOL char) */
4001 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
4002 last_line
= MAX(first_line
, last_line
);
4007 sci_start_undo_action(editor
->sci
);
4009 for (i
= first_line
; i
<= last_line
; i
++)
4011 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
4014 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
4015 /* searching backwards for a space to remove */
4016 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
4019 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
4021 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
4022 sci_replace_sel(editor
->sci
, "");
4024 if (i
== first_line
)
4025 first_line_offset
= -1;
4030 sci_insert_text(editor
->sci
, indentation_end
, " ");
4032 if (i
== first_line
)
4033 first_line_offset
= 1;
4037 /* set cursor position */
4038 if (sel_start
< sel_end
)
4040 gint start
= sel_start
+ first_line_offset
;
4041 if (first_line_offset
< 0)
4042 start
= MAX(sel_start
+ first_line_offset
,
4043 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
4045 sci_set_selection_start(editor
->sci
, start
);
4046 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
4049 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
4051 sci_end_undo_action(editor
->sci
);
4055 void editor_finalize()
4057 scintilla_release_resources();
4061 /* wordchars: NULL or a string containing characters to match a word.
4062 * Returns: the current selection or the current word. */
4063 gchar
*editor_get_default_selection(GeanyEditor
*editor
, gboolean use_current_word
,
4064 const gchar
*wordchars
)
4068 g_return_val_if_fail(editor
!= NULL
, NULL
);
4070 if (sci_get_lines_selected(editor
->sci
) == 1)
4072 gint len
= sci_get_selected_text_length(editor
->sci
);
4074 s
= g_malloc(len
+ 1);
4075 sci_get_selected_text(editor
->sci
, s
);
4077 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
4078 { /* use the word at current cursor position */
4079 gchar word
[GEANY_MAX_WORD_LENGTH
];
4081 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
4082 if (word
[0] != '\0')
4089 /* Note: Usually the line should be made visible (not folded) before calling this.
4090 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4091 * outside the *vertical* view.
4092 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4093 * sci_scroll_caret() when this returns TRUE. */
4094 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4098 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4100 /* If line is wrapped the result may occur on another virtual line than the first and may be
4101 * still hidden, so increase the line number to check for the next document line */
4102 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4105 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4106 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4107 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4109 return (line
>= vis1
&& line
< vis1
+ los
);
4113 /* If the current line is outside the current view window, scroll the line
4114 * so it appears at percent_of_view. */
4115 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4119 g_return_if_fail(editor
!= NULL
);
4121 line
= sci_get_current_line(editor
->sci
);
4123 /* unfold maybe folded results */
4124 sci_ensure_line_is_visible(editor
->sci
, line
);
4126 /* scroll the line if it's off screen */
4127 if (! editor_line_in_view(editor
, line
))
4128 editor
->scroll_percent
= percent_of_view
;
4130 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4135 * Deletes all currently set indicators in the @a editor window.
4136 * Error indicators (red squiggly underlines) and usual line markers are removed.
4138 * @param editor The editor to operate on.
4140 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4142 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4143 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4148 * Deletes all currently set indicators matching @a indic in the @a editor window.
4150 * @param editor The editor to operate on.
4151 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4155 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4159 g_return_if_fail(editor
!= NULL
);
4161 last_pos
= sci_get_length(editor
->sci
);
4164 sci_indicator_set(editor
->sci
, indic
);
4165 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4171 * Sets an indicator @a indic on @a line.
4172 * Whitespace at the start and the end of the line is not marked.
4174 * @param editor The editor to operate on.
4175 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4176 * @param line The line number which should be marked.
4180 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4186 g_return_if_fail(editor
!= NULL
);
4187 g_return_if_fail(line
>= 0);
4189 start
= sci_get_position_from_line(editor
->sci
, line
);
4190 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4192 /* skip blank lines */
4193 if ((start
+ 1) == end
||
4195 sci_get_line_length(editor
->sci
, line
) == editor_get_eol_char_len(editor
))
4201 linebuf
= sci_get_line(editor
->sci
, line
);
4203 /* don't set the indicator on whitespace */
4204 while (isspace(linebuf
[i
]))
4206 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4213 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4218 * Sets an indicator on the range specified by @a start and @a end.
4219 * No error checking or whitespace removal is performed, this should be done by the calling
4220 * function if necessary.
4222 * @param editor The editor to operate on.
4223 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4224 * @param start The starting position for the marker.
4225 * @param end The ending position for the marker.
4229 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4231 g_return_if_fail(editor
!= NULL
);
4235 sci_indicator_set(editor
->sci
, indic
);
4236 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4240 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4241 * the replacement will also start with 0x... */
4242 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4244 g_return_if_fail(editor
!= NULL
);
4246 if (sci_has_selection(editor
->sci
))
4248 gint start
= sci_get_selection_start(editor
->sci
);
4249 const gchar
*replacement
= colour
;
4251 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4252 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4254 sci_set_selection_start(editor
->sci
, start
+ 2);
4255 sci_set_selection_end(editor
->sci
, start
+ 8);
4256 replacement
++; /* skip the leading "0x" */
4258 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4259 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4260 replacement
++; /* so skip the '#' to only replace the colour value */
4262 sci_replace_sel(editor
->sci
, replacement
);
4265 sci_add_text(editor
->sci
, colour
);
4270 * Retrieves the localized name (for displaying) of the used end of line characters
4271 * (LF, CR/LF, CR) in the given editor.
4272 * If @a editor is @c NULL, the default end of line characters are used.
4274 * @param editor The editor to operate on, or @c NULL to query the default value.
4275 * @return The name of the end of line characters.
4279 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4281 gint mode
= file_prefs
.default_eol_character
;
4284 mode
= sci_get_eol_mode(editor
->sci
);
4286 return utils_get_eol_name(mode
);
4291 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4292 * If @a editor is @c NULL, the default end of line characters are used.
4293 * The returned value is 1 for CR and LF and 2 for CR/LF.
4295 * @param editor The editor to operate on, or @c NULL to query the default value.
4296 * @return The length of the end of line characters.
4300 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4302 gint mode
= file_prefs
.default_eol_character
;
4305 mode
= sci_get_eol_mode(editor
->sci
);
4309 case SC_EOL_CRLF
: return 2; break;
4310 default: return 1; break;
4316 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4317 * If @a editor is @c NULL, the default end of line characters are used.
4318 * The returned value is either "\n", "\r\n" or "\r".
4320 * @param editor The editor to operate on, or @c NULL to query the default value.
4321 * @return The end of line characters.
4325 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4327 gint mode
= file_prefs
.default_eol_character
;
4330 mode
= sci_get_eol_mode(editor
->sci
);
4334 case SC_EOL_CRLF
: return "\r\n"; break;
4335 case SC_EOL_CR
: return "\r"; break;
4336 default: return "\n"; break;
4341 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4343 gint lines
, first
, i
;
4345 if (editor
== NULL
|| ! editor_prefs
.folding
)
4348 lines
= sci_get_line_count(editor
->sci
);
4349 first
= sci_get_first_visible_line(editor
->sci
);
4351 for (i
= 0; i
< lines
; i
++)
4353 gint level
= sci_get_fold_level(editor
->sci
, i
);
4355 if (level
& SC_FOLDLEVELHEADERFLAG
)
4357 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4358 sci_toggle_fold(editor
->sci
, i
);
4361 editor_scroll_to_line(editor
, first
, 0.0F
);
4365 void editor_unfold_all(GeanyEditor
*editor
)
4367 fold_all(editor
, FALSE
);
4371 void editor_fold_all(GeanyEditor
*editor
)
4373 fold_all(editor
, TRUE
);
4377 void editor_replace_tabs(GeanyEditor
*editor
)
4379 gint search_pos
, pos_in_line
, current_tab_true_length
;
4382 struct Sci_TextToFind ttf
;
4384 g_return_if_fail(editor
!= NULL
);
4386 sci_start_undo_action(editor
->sci
);
4387 tab_len
= sci_get_tab_width(editor
->sci
);
4389 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4390 ttf
.lpstrText
= (gchar
*) "\t";
4394 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4395 if (search_pos
== -1)
4398 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4399 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4400 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4401 sci_set_target_start(editor
->sci
, search_pos
);
4402 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4403 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4404 /* next search starts after replacement */
4405 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4406 /* update end of range now text has changed */
4407 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4410 sci_end_undo_action(editor
->sci
);
4414 /* Replaces all occurrences all spaces of the length of a given tab_width. */
4415 void editor_replace_spaces(GeanyEditor
*editor
)
4418 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4420 struct Sci_TextToFind ttf
;
4422 g_return_if_fail(editor
!= NULL
);
4424 if (tab_len_f
< 0.0)
4425 tab_len_f
= sci_get_tab_width(editor
->sci
);
4427 if (! dialogs_show_input_numeric(
4428 _("Enter Tab Width"),
4429 _("Enter the amount of spaces which should be replaced by a tab character."),
4430 &tab_len_f
, 1, 100, 1))
4434 tab_len
= (gint
) tab_len_f
;
4436 sci_start_undo_action(editor
->sci
);
4438 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4439 ttf
.lpstrText
= g_strnfill(tab_len
, ' ');
4443 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4444 if (search_pos
== -1)
4447 sci_set_target_start(editor
->sci
, search_pos
);
4448 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4449 sci_replace_target(editor
->sci
, "\t", FALSE
);
4450 ttf
.chrg
.cpMin
= search_pos
;
4451 /* update end of range now text has changed */
4452 ttf
.chrg
.cpMax
-= tab_len
- 1;
4454 sci_end_undo_action(editor
->sci
);
4455 g_free(ttf
.lpstrText
);
4459 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4461 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4462 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4463 gint i
= line_end
- 1;
4464 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4466 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4469 ch
= sci_get_char_at(editor
->sci
, i
);
4471 if (i
< (line_end
- 1))
4473 sci_set_target_start(editor
->sci
, i
+ 1);
4474 sci_set_target_end(editor
->sci
, line_end
);
4475 sci_replace_target(editor
->sci
, "", FALSE
);
4480 void editor_strip_trailing_spaces(GeanyEditor
*editor
)
4482 gint max_lines
= sci_get_line_count(editor
->sci
);
4485 sci_start_undo_action(editor
->sci
);
4487 for (line
= 0; line
< max_lines
; line
++)
4489 editor_strip_line_trailing_spaces(editor
, line
);
4491 sci_end_undo_action(editor
->sci
);
4495 void editor_ensure_final_newline(GeanyEditor
*editor
)
4497 gint max_lines
= sci_get_line_count(editor
->sci
);
4498 gboolean append_newline
= (max_lines
== 1);
4499 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4503 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4507 const gchar
*eol
= "\n";
4508 switch (sci_get_eol_mode(editor
->sci
))
4517 sci_insert_text(editor
->sci
, end_document
, eol
);
4522 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4526 PangoFontDescription
*pfd
;
4528 g_return_if_fail(editor
);
4530 pfd
= pango_font_description_from_string(font
);
4531 size
= pango_font_description_get_size(pfd
) / PANGO_SCALE
;
4532 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4533 pango_font_description_free(pfd
);
4535 for (style
= 0; style
<= 127; style
++)
4536 sci_set_font(editor
->sci
, style
, font_name
, size
);
4538 /* line number and braces */
4539 sci_set_font(editor
->sci
, STYLE_LINENUMBER
, font_name
, size
);
4540 sci_set_font(editor
->sci
, STYLE_BRACELIGHT
, font_name
, size
);
4541 sci_set_font(editor
->sci
, STYLE_BRACEBAD
, font_name
, size
);
4544 /* zoom to 100% to prevent confusion */
4545 sci_zoom_off(editor
->sci
);
4549 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4551 g_return_if_fail(editor
!= NULL
);
4553 editor
->line_wrapping
= wrap
;
4554 sci_set_lines_wrapped(editor
->sci
, wrap
);
4558 /** Sets the indent type for @a editor.
4559 * @param editor Editor.
4560 * @param type Indent type.
4564 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4566 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4567 ScintillaObject
*sci
= editor
->sci
;
4568 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4570 editor
->indent_type
= type
;
4571 sci_set_use_tabs(sci
, use_tabs
);
4573 if (type
== GEANY_INDENT_TYPE_BOTH
)
4575 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4576 if (iprefs
->hard_tab_width
!= 8)
4578 static gboolean warn
= TRUE
;
4580 ui_set_statusbar(TRUE
, _("Warning: non-standard hard tab width: %d != 8!"),
4581 iprefs
->hard_tab_width
);
4586 sci_set_tab_width(sci
, iprefs
->width
);
4588 SSM(sci
, SCI_SETINDENT
, iprefs
->width
, 0);
4590 /* remove indent spaces on backspace, if using any spaces to indent */
4591 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, type
!= GEANY_INDENT_TYPE_TABS
, 0);
4595 /* Convenience function for editor_goto_pos() to pass in a line number. */
4596 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line_no
, gint offset
)
4600 g_return_val_if_fail(editor
, FALSE
);
4601 if (line_no
< 0 || line_no
>= sci_get_line_count(editor
->sci
))
4606 gint current_line
= sci_get_current_line(editor
->sci
);
4608 line_no
= current_line
+ line_no
;
4611 pos
= sci_get_position_from_line(editor
->sci
, line_no
);
4612 return editor_goto_pos(editor
, pos
, TRUE
);
4616 /* Move to position @a pos, switching to the document if necessary,
4617 * setting a marker if @a mark is set. */
4618 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4622 g_return_val_if_fail(editor
, FALSE
);
4623 if (G_UNLIKELY(pos
< 0))
4628 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4630 /* mark the tag with the yellow arrow */
4631 sci_marker_delete_all(editor
->sci
, 0);
4632 sci_set_marker_at_line(editor
->sci
, line
, 0);
4635 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4636 editor
->scroll_percent
= 0.25F
;
4638 /* finally switch to the page */
4639 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets
.notebook
), GTK_WIDGET(editor
->sci
));
4640 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), page_num
);
4647 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4649 GeanyEditor
*editor
= user_data
;
4651 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4652 * few lines only, maybe this could/should be done in Scintilla directly */
4653 if (event
->state
& GDK_MOD1_MASK
)
4655 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4658 else if (event
->state
& GDK_SHIFT_MASK
)
4660 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4662 sci_scroll_columns(editor
->sci
, amount
);
4666 return FALSE
; /* let Scintilla handle all other cases */
4670 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4672 GeanyDocument
*doc
= editor
->document
;
4674 if (!doc
->priv
->colourise_needed
)
4677 doc
->priv
->colourise_needed
= FALSE
;
4678 sci_colourise(editor
->sci
, 0, -1);
4680 /* now that the current document is colourised, fold points are now accurate,
4681 * so force an update of the current function/tag. */
4682 symbols_get_current_function(NULL
, NULL
);
4683 ui_update_statusbar(NULL
, -1);
4689 /* We only want to colourise just before drawing, to save startup time and
4690 * prevent unnecessary recolouring other documents after one is saved.
4691 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4692 * and "show" doesn't work). */
4693 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4695 GeanyEditor
*editor
= user_data
;
4697 editor_check_colourise(editor
);
4702 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4703 * for some reason, maybe it's not necessary but just in case. */
4704 static gboolean
on_editor_expose_event(GtkWidget
*widget
, GdkEventExpose
*event
,
4707 GeanyEditor
*editor
= user_data
;
4709 editor_check_colourise(editor
);
4714 static void setup_sci_keys(ScintillaObject
*sci
)
4716 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4717 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4718 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4719 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4720 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4721 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4722 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4723 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4724 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4725 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4726 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4727 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4728 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4729 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4730 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4732 if (editor_prefs
.use_gtk_word_boundaries
)
4734 /* use GtkEntry-like word boundaries */
4735 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4736 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4737 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4739 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4740 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4741 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4742 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4743 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4744 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4746 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4750 #include "icons/16x16/classviewer-var.xpm"
4751 #include "icons/16x16/classviewer-method.xpm"
4753 /* Create new editor widget (scintilla).
4754 * @note The @c "sci-notify" signal is connected separately. */
4755 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4757 ScintillaObject
*sci
;
4759 sci
= SCINTILLA(scintilla_new());
4761 gtk_widget_show(GTK_WIDGET(sci
));
4763 sci_set_codepage(sci
, SC_CP_UTF8
);
4764 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4765 /* disable scintilla provided popup menu */
4766 sci_use_popup(sci
, FALSE
);
4768 setup_sci_keys(sci
);
4770 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4771 sci_set_lines_wrapped(sci
, editor_prefs
.line_wrapping
);
4772 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
4773 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4774 /*sci_set_caret_policy_y(sci, CARET_JUMPS | CARET_EVEN, 0);*/
4775 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4776 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4778 /* tag autocompletion images */
4779 SSM(sci
, SCI_REGISTERIMAGE
, 1, (sptr_t
)classviewer_var
);
4780 SSM(sci
, SCI_REGISTERIMAGE
, 2, (sptr_t
)classviewer_method
);
4782 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4783 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4786 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4788 /* only connect signals if this is for the document notebook, not split window */
4789 if (editor
->sci
== NULL
)
4791 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4792 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4793 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4794 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4795 g_signal_connect(sci
, "expose-event", G_CALLBACK(on_editor_expose_event
), editor
);
4801 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4802 * @param editor Editor settings.
4803 * @return The new widget.
4807 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4809 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4810 ScintillaObject
*old
, *sci
;
4812 /* temporarily change editor to use the new sci widget */
4814 sci
= create_new_sci(editor
);
4817 editor_set_indent_type(editor
, iprefs
->type
);
4818 editor_set_font(editor
, interface_prefs
.editor_font
);
4819 editor_apply_update_prefs(editor
);
4821 /* if editor already had a widget, restore it */
4828 GeanyEditor
*editor_create(GeanyDocument
*doc
)
4830 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4831 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
4833 editor
->document
= doc
;
4834 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
4836 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
4837 editor
->line_wrapping
= editor_prefs
.line_wrapping
;
4838 editor
->scroll_percent
= -1.0F
;
4839 editor
->line_breaking
= FALSE
;
4841 editor
->sci
= editor_create_widget(editor
);
4846 /* in case we need to free some fields in future */
4847 void editor_destroy(GeanyEditor
*editor
)
4853 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
4855 g_return_if_fail(NZV(doc
->real_path
));
4857 if (utils_str_equal(doc
->real_path
,
4858 utils_build_path(app
->configdir
, "snippets.conf", NULL
)))
4860 /* reload snippets */
4861 editor_snippets_free();
4862 editor_snippets_init();
4867 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
4871 g_return_val_if_fail(editor
, FALSE
);
4873 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
4876 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
4878 /* if no word part, complete normally */
4879 if (!check_partial_completion(editor
, entry
))
4880 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
4887 void editor_init(void)
4889 static GeanyIndentPrefs indent_prefs
;
4891 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
4892 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
4893 editor_prefs
.indentation
= &indent_prefs
;
4895 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
4896 * handler (on_editor_notify) is called */
4897 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
4899 ui_add_config_file_menu_item(utils_build_path(app
->configdir
, "snippets.conf", NULL
),
4901 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
4905 /* TODO: Should these be user-defined instead of hard-coded? */
4906 void editor_set_indentation_guides(GeanyEditor
*editor
)
4911 g_return_if_fail(editor
!= NULL
);
4913 if (! editor_prefs
.show_indent_guide
)
4915 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
4919 lexer
= sci_get_lexer(editor
->sci
);
4922 /* Lines added/removed are prefixed with +/- characters, so
4923 * those lines will not be shown with any indentation guides.
4924 * It can be distracting that only a few of lines in a diff/patch
4925 * file will show the guides. */
4930 /* These languages use indentation for control blocks; the "look forward" method works
4934 case SCLEX_MAKEFILE
:
4937 case SCLEX_PROPERTIES
:
4938 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
4940 mode
= SC_IV_LOOKFORWARD
;
4943 /* C-like (structured) languages benefit from the "look both" method */
4957 case SCLEX_FREEBASIC
:
4960 mode
= SC_IV_LOOKBOTH
;
4968 sci_set_indentation_guides(editor
->sci
, mode
);
4972 /* Apply just the prefs that can change in the Preferences dialog */
4973 void editor_apply_update_prefs(GeanyEditor
*editor
)
4975 ScintillaObject
*sci
;
4977 g_return_if_fail(editor
!= NULL
);
4979 if (main_status
.quitting
)
4984 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
4985 editor_get_long_line_column(), editor_prefs
.long_line_color
);
4987 /* update indent width, tab width */
4988 editor_set_indent_type(editor
, editor
->indent_type
);
4989 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
4991 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
4992 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
4994 editor_set_indentation_guides(editor
);
4996 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
4997 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
4998 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4999 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
5001 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
5004 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
5006 /* (dis)allow scrolling past end of document */
5007 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
5011 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
5012 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
5014 ScintillaObject
*sci
= editor
->sci
;
5015 gint pos
= sci_get_position_from_line(sci
, line
);
5019 sci_insert_text(sci
, pos
, "\t");
5023 if (sci_get_char_at(sci
, pos
) == '\t')
5025 sci_set_selection(sci
, pos
, pos
+ 1);
5026 sci_replace_sel(sci
, "");
5028 else /* remove spaces only if no tabs */
5030 gint width
= sci_get_line_indentation(sci
, line
);
5032 width
-= editor_get_indent_prefs(editor
)->width
;
5033 sci_set_line_indentation(sci
, line
, width
);
5039 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
5041 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
5042 ScintillaObject
*sci
= editor
->sci
;
5044 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
5045 change_tab_indentation(editor
, line
, increase
);
5048 gint width
= sci_get_line_indentation(sci
, line
);
5050 width
+= increase
? iprefs
->width
: -iprefs
->width
;
5051 sci_set_line_indentation(sci
, line
, width
);
5056 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
5058 ScintillaObject
*sci
= editor
->sci
;
5060 gint line
, lstart
, lend
;
5062 if (sci_get_lines_selected(sci
) <= 1)
5064 line
= sci_get_current_line(sci
);
5065 editor_change_line_indent(editor
, line
, increase
);
5068 editor_select_lines(editor
, FALSE
);
5069 start
= sci_get_selection_start(sci
);
5070 end
= sci_get_selection_end(sci
);
5071 lstart
= sci_get_line_from_position(sci
, start
);
5072 lend
= sci_get_line_from_position(sci
, end
);
5073 if (end
== sci_get_length(sci
))
5074 lend
++; /* for last line with text on it */
5076 sci_start_undo_action(sci
);
5077 for (line
= lstart
; line
< lend
; line
++)
5079 editor_change_line_indent(editor
, line
, increase
);
5081 sci_end_undo_action(sci
);
5083 /* set cursor/selection */
5086 sci_set_selection_start(sci
, start
);
5087 end
= sci_get_position_from_line(sci
, lend
);
5088 sci_set_selection_end(sci
, end
);
5089 editor_select_lines(editor
, FALSE
);
5093 sci_set_current_line(sci
, lstart
);