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 "documentprivate.h"
51 #include "filetypes.h"
52 #include "sciwrappers.h"
57 #include "callbacks.h"
58 #include "templates.h"
59 #include "keybindings.h"
61 #include "projectprivate.h"
64 /* Note: use sciwrappers.h instead where possible.
65 * Do not use SSM in files unrelated to scintilla. */
66 #define SSM(s, m, w, l) scintilla_send_message(s, m, w, l)
69 static GHashTable
*snippet_hash
= NULL
;
70 static GQueue
*snippet_offsets
= NULL
;
71 static gint snippet_cursor_insert_pos
;
73 /* holds word under the mouse or keyboard cursor */
74 static gchar current_word
[GEANY_MAX_WORD_LENGTH
];
76 /* Initialised in keyfile.c. */
77 GeanyEditorPrefs editor_prefs
;
79 EditorInfo editor_info
= {current_word
, -1};
89 } calltip
= {NULL
, FALSE
, NULL
, 0, 0, NULL
};
91 static gchar indent
[100];
94 static void on_new_line_added(GeanyEditor
*editor
);
95 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
);
96 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
);
97 static void auto_multiline(GeanyEditor
*editor
, gint pos
);
98 static gboolean
is_code_style(gint lexer
, gint style
);
99 static gboolean
is_string_style(gint lexer
, gint style
);
100 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
);
101 static void auto_table(GeanyEditor
*editor
, gint pos
);
102 static void close_block(GeanyEditor
*editor
, gint pos
);
103 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
);
104 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
105 const gchar
*wc
, gboolean stem
);
108 void editor_snippets_free(void)
110 g_hash_table_destroy(snippet_hash
);
111 g_queue_free(snippet_offsets
);
115 void editor_snippets_init(void)
117 gsize i
, j
, len
= 0, len_keys
= 0;
118 gchar
*sysconfigfile
, *userconfigfile
;
119 gchar
**groups_user
, **groups_sys
;
120 gchar
**keys_user
, **keys_sys
;
122 GKeyFile
*sysconfig
= g_key_file_new();
123 GKeyFile
*userconfig
= g_key_file_new();
126 snippet_offsets
= g_queue_new();
128 sysconfigfile
= g_strconcat(app
->datadir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
129 userconfigfile
= g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "snippets.conf", NULL
);
131 /* check for old autocomplete.conf files (backwards compatibility) */
132 if (! g_file_test(userconfigfile
, G_FILE_TEST_IS_REGULAR
| G_FILE_TEST_IS_SYMLINK
))
133 setptr(userconfigfile
,
134 g_strconcat(app
->configdir
, G_DIR_SEPARATOR_S
, "autocomplete.conf", NULL
));
136 /* load the actual config files */
137 g_key_file_load_from_file(sysconfig
, sysconfigfile
, G_KEY_FILE_NONE
, NULL
);
138 g_key_file_load_from_file(userconfig
, userconfigfile
, G_KEY_FILE_NONE
, NULL
);
140 /* keys are strings, values are GHashTables, so use g_free and g_hash_table_destroy */
142 g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, (GDestroyNotify
) g_hash_table_destroy
);
144 /* first read all globally defined auto completions */
145 groups_sys
= g_key_file_get_groups(sysconfig
, &len
);
146 for (i
= 0; i
< len
; i
++)
148 keys_sys
= g_key_file_get_keys(sysconfig
, groups_sys
[i
], &len_keys
, NULL
);
149 /* create new hash table for the read section (=> filetype) */
150 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
151 g_hash_table_insert(snippet_hash
, g_strdup(groups_sys
[i
]), tmp
);
153 for (j
= 0; j
< len_keys
; j
++)
155 g_hash_table_insert(tmp
, g_strdup(keys_sys
[j
]),
156 utils_get_setting_string(sysconfig
, groups_sys
[i
], keys_sys
[j
], ""));
158 g_strfreev(keys_sys
);
161 /* now read defined completions in user's configuration directory and add / replace them */
162 groups_user
= g_key_file_get_groups(userconfig
, &len
);
163 for (i
= 0; i
< len
; i
++)
165 keys_user
= g_key_file_get_keys(userconfig
, groups_user
[i
], &len_keys
, NULL
);
167 tmp
= g_hash_table_lookup(snippet_hash
, groups_user
[i
]);
169 { /* new key found, create hash table */
170 tmp
= g_hash_table_new_full(g_str_hash
, g_str_equal
, g_free
, g_free
);
171 g_hash_table_insert(snippet_hash
, g_strdup(groups_user
[i
]), tmp
);
173 for (j
= 0; j
< len_keys
; j
++)
175 value
= g_hash_table_lookup(tmp
, keys_user
[j
]);
177 { /* value = NULL means the key doesn't yet exist, so insert */
178 g_hash_table_insert(tmp
, g_strdup(keys_user
[j
]),
179 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
182 { /* old key and value will be freed by destroy function (g_free) */
183 g_hash_table_replace(tmp
, g_strdup(keys_user
[j
]),
184 utils_get_setting_string(userconfig
, groups_user
[i
], keys_user
[j
], ""));
187 g_strfreev(keys_user
);
190 g_free(sysconfigfile
);
191 g_free(userconfigfile
);
192 g_strfreev(groups_sys
);
193 g_strfreev(groups_user
);
194 g_key_file_free(sysconfig
);
195 g_key_file_free(userconfig
);
199 static gboolean
on_editor_button_press_event(GtkWidget
*widget
, GdkEventButton
*event
,
202 GeanyEditor
*editor
= data
;
203 GeanyDocument
*doc
= editor
->document
;
205 /* it's very unlikely we got a 'real' click even on 0, 0, so assume it is a
206 * fake event to show the editor menu triggered by a key event where we want to use the
207 * text cursor position. */
208 if (event
->x
> 0.0 && event
->y
> 0.0)
209 editor_info
.click_pos
= sci_get_position_from_xy(editor
->sci
,
210 (gint
)event
->x
, (gint
)event
->y
, FALSE
);
212 editor_info
.click_pos
= sci_get_current_position(editor
->sci
);
214 if (event
->button
== 1)
216 guint state
= event
->state
& gtk_accelerator_get_default_mod_mask();
218 if (event
->type
== GDK_BUTTON_PRESS
&& editor_prefs
.disable_dnd
)
220 gint ss
= sci_get_selection_start(editor
->sci
);
221 sci_set_selection_end(editor
->sci
, ss
);
223 if (event
->type
== GDK_BUTTON_PRESS
&& state
== GDK_CONTROL_MASK
)
225 sci_set_current_position(editor
->sci
, editor_info
.click_pos
, FALSE
);
227 editor_find_current_word(editor
, editor_info
.click_pos
,
228 current_word
, sizeof current_word
, NULL
);
230 return symbols_goto_tag(current_word
, TRUE
);
232 keybindings_send_command(GEANY_KEY_GROUP_GOTO
, GEANY_KEYS_GOTO_MATCHINGBRACE
);
235 return document_check_disk_status(doc
, FALSE
);
238 /* calls the edit popup menu in the editor */
239 if (event
->button
== 3)
243 editor_find_current_word(editor
, editor_info
.click_pos
,
244 current_word
, sizeof current_word
, NULL
);
246 can_goto
= sci_has_selection(editor
->sci
) || current_word
[0] != '\0';
247 ui_update_popup_goto_items(can_goto
);
248 ui_update_popup_copy_items(doc
);
249 ui_update_insert_include_item(doc
, 0);
251 g_signal_emit_by_name(geany_object
, "update-editor-menu",
252 current_word
, editor_info
.click_pos
, doc
);
254 gtk_menu_popup(GTK_MENU(main_widgets
.editor_menu
),
255 NULL
, NULL
, NULL
, NULL
, event
->button
, event
->time
);
263 static gboolean
is_style_php(gint style
)
265 if ((style
>= SCE_HPHP_DEFAULT
&& style
<= SCE_HPHP_OPERATOR
) ||
266 style
== SCE_HPHP_COMPLEX_VARIABLE
)
275 gint
editor_get_long_line_type(void)
278 switch (app
->project
->long_line_behaviour
)
280 case 0: /* marker disabled */
282 case 1: /* use global settings */
284 case 2: /* custom (enabled) */
285 return editor_prefs
.long_line_global_type
;
288 if (!editor_prefs
.long_line_global_enabled
)
291 return editor_prefs
.long_line_global_type
;
295 gint
editor_get_long_line_column(void)
297 if (app
->project
&& app
->project
->long_line_behaviour
!= 1 /* use global settings */)
298 return app
->project
->long_line_column
;
300 return editor_prefs
.long_line_global_column
;
304 void editor_toggle_fold(GeanyEditor
*editor
, gint line
, gint modifiers
)
306 ScintillaObject
*sci
;
308 g_return_if_fail(editor
!= NULL
);
312 sci_toggle_fold(sci
, line
);
314 /* extra toggling of child fold points
315 * use when editor_prefs.unfold_all_children is set and Shift is NOT pressed or when
316 * editor_prefs.unfold_all_children is NOT set but Shift is pressed */
317 if ((editor_prefs
.unfold_all_children
&& ! (modifiers
& SCMOD_SHIFT
)) ||
318 (! editor_prefs
.unfold_all_children
&& (modifiers
& SCMOD_SHIFT
)))
320 gint last_line
= SSM(sci
, SCI_GETLASTCHILD
, line
, -1);
323 if (sci_get_line_is_visible(sci
, line
+ 1))
324 { /* unfold all children of the current fold point */
325 for (i
= line
; i
< last_line
; i
++)
327 if (! sci_get_line_is_visible(sci
, i
))
329 sci_toggle_fold(sci
, sci_get_fold_parent(sci
, i
));
334 { /* fold all children of the current fold point */
335 for (i
= line
; i
< last_line
; i
++)
337 gint level
= sci_get_fold_level(sci
, i
);
338 if (level
& SC_FOLDLEVELHEADERFLAG
)
340 if (sci_get_fold_expanded(sci
, i
))
341 sci_toggle_fold(sci
, i
);
349 static void on_margin_click(GeanyEditor
*editor
, SCNotification
*nt
)
351 /* left click to marker margin marks the line */
354 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
356 /*sci_marker_delete_all(editor->sci, 1);*/
357 sci_toggle_marker_at_line(editor
->sci
, line
, 1); /* toggle the marker */
359 /* left click on the folding margin to toggle folding state of current line */
360 else if (nt
->margin
== 2 && editor_prefs
.folding
)
362 gint line
= sci_get_line_from_position(editor
->sci
, nt
->position
);
363 editor_toggle_fold(editor
, line
, nt
->modifiers
);
368 static void on_update_ui(GeanyEditor
*editor
, G_GNUC_UNUSED SCNotification
*nt
)
370 ScintillaObject
*sci
= editor
->sci
;
371 gint pos
= sci_get_current_position(sci
);
373 /* undo / redo menu update */
374 ui_update_popup_reundo_items(editor
->document
);
376 /* brace highlighting */
377 editor_highlight_braces(editor
, pos
);
379 ui_update_statusbar(editor
->document
, pos
);
381 /* Visible lines are only laid out accurately once [SCN_UPDATEUI] is sent,
382 * so we need to only call sci_scroll_to_line here, because the document
383 * may have line wrapping and folding enabled.
384 * http://scintilla.sourceforge.net/ScintillaDoc.html#LineWrapping */
385 if (editor
->scroll_percent
> 0.0F
)
387 editor_scroll_to_line(editor
, -1, editor
->scroll_percent
);
388 editor
->scroll_percent
= -1.0F
; /* disable further scrolling */
391 /** experimental code for inverting selections */
394 for (i
= SSM(sci
, SCI_GETSELECTIONSTART
, 0, 0); i
< SSM(sci
, SCI_GETSELECTIONEND
, 0, 0); i
++)
396 /* need to get colour from getstyleat(), but how? */
397 SSM(sci
, SCI_STYLESETFORE
, STYLE_DEFAULT
, 0);
398 SSM(sci
, SCI_STYLESETBACK
, STYLE_DEFAULT
, 0);
401 sci_get_style_at(sci
, pos
);
407 static void check_line_breaking(GeanyEditor
*editor
, gint pos
, gchar c
)
409 ScintillaObject
*sci
= editor
->sci
;
410 gint line
, lstart
, col
;
412 if (!editor
->line_breaking
)
415 col
= sci_get_col_from_position(sci
, pos
);
418 pos
--; /* Look for previous space, not the new one */
420 line
= sci_get_current_line(sci
);
422 lstart
= sci_get_position_from_line(sci
, line
);
424 /* use column instead of position which might be different with multibyte characters */
425 if (col
< editor_prefs
.line_break_column
)
428 /* look for the last space before line_break_column */
429 pos
= MIN(pos
, lstart
+ editor_prefs
.line_break_column
);
433 c
= sci_get_char_at(sci
, --pos
);
436 gint diff
, last_pos
, last_col
;
437 const gchar
*eol
= editor_get_eol_char(editor
);
439 /* remember the distance between the current column and the last column on the line
440 * (we use column position in case the previous line gets altered, such as removing
441 * trailing spaces or in case it contains multibyte characters) */
442 last_pos
= sci_get_line_end_position(sci
, line
);
443 last_col
= sci_get_col_from_position(sci
, last_pos
);
444 diff
= last_col
- col
;
446 /* break the line after the space */
447 sci_insert_text(sci
, pos
+ 1, eol
);
450 /* set position as if user had pressed return */
451 pos
= sci_get_position_from_line(sci
, line
);
452 sci_set_current_position(sci
, pos
, FALSE
);
453 /* add indentation, comment multilines, etc */
454 on_new_line_added(editor
);
456 /* correct cursor position (might not be at line end) */
457 last_pos
= sci_get_line_end_position(sci
, line
);
458 last_col
= sci_get_col_from_position(sci
, last_pos
); /* get last column on line */
459 /* last column - distance is the desired column, then retrieve its document position */
460 pos
= SSM(sci
, SCI_FINDCOLUMN
, line
, last_col
- diff
);
461 sci_set_current_position(sci
, pos
, FALSE
);
469 static void show_autocomplete(ScintillaObject
*sci
, gint rootlen
, const gchar
*words
)
471 /* store whether a calltip is showing, so we can reshow it after autocompletion */
472 calltip
.set
= SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0);
473 SSM(sci
, SCI_AUTOCSHOW
, rootlen
, (sptr_t
) words
);
477 static void show_tags_list(GeanyEditor
*editor
, const GPtrArray
*tags
, gsize rootlen
)
479 ScintillaObject
*sci
= editor
->sci
;
481 g_return_if_fail(tags
);
485 GString
*words
= g_string_sized_new(150);
488 for (j
= 0; j
< tags
->len
; ++j
)
490 TMTag
*tag
= tags
->pdata
[j
];
493 g_string_append_c(words
, '\n');
495 if (j
== editor_prefs
.autocompletion_max_entries
)
497 g_string_append(words
, "...");
500 g_string_append(words
, tag
->name
);
502 /* for now, tag types don't all follow C, so just look at arglist */
503 if (NZV(tag
->atts
.entry
.arglist
))
504 g_string_append(words
, "?2");
506 g_string_append(words
, "?1");
508 show_autocomplete(sci
, rootlen
, words
->str
);
509 g_string_free(words
, TRUE
);
514 /* do not use with long strings */
515 static gboolean
match_last_chars(ScintillaObject
*sci
, gint pos
, const gchar
*str
)
517 gsize len
= strlen(str
);
520 g_return_val_if_fail(len
< 100, FALSE
);
522 buf
= g_alloca(len
+ 1);
523 sci_get_text_range(sci
, pos
- len
, pos
, buf
);
524 return strcmp(str
, buf
) == 0;
528 static gboolean
reshow_calltip(gpointer data
)
532 g_return_val_if_fail(calltip
.sci
!= NULL
, FALSE
);
534 SSM(calltip
.sci
, SCI_CALLTIPCANCEL
, 0, 0);
535 doc
= document_get_current();
537 if (doc
&& doc
->editor
->sci
== calltip
.sci
)
539 /* we use the position where the calltip was previously started as SCI_GETCURRENTPOS
540 * may be completely wrong in case the user cancelled the auto completion with the mouse */
541 SSM(calltip
.sci
, SCI_CALLTIPSHOW
, calltip
.pos
, (sptr_t
) calltip
.text
);
547 static void request_reshowing_calltip(SCNotification
*nt
)
551 /* delay the reshow of the calltip window to make sure it is actually displayed,
552 * without it might be not visible on SCN_AUTOCCANCEL */
553 g_idle_add(reshow_calltip
, NULL
);
558 static void autocomplete_scope(GeanyEditor
*editor
)
560 ScintillaObject
*sci
= editor
->sci
;
561 gint pos
= sci_get_current_position(editor
->sci
);
562 gchar typed
= sci_get_char_at(sci
, pos
- 1);
564 const GPtrArray
*tags
= NULL
;
566 GeanyFiletype
*ft
= editor
->document
->file_type
;
568 if (ft
->id
== GEANY_FILETYPES_C
|| ft
->id
== GEANY_FILETYPES_CPP
)
570 if (match_last_chars(sci
, pos
, "->") || match_last_chars(sci
, pos
, "::"))
572 else if (typed
!= '.')
575 else if (typed
!= '.')
578 /* allow for a space between word and operator */
579 if (isspace(sci_get_char_at(sci
, pos
- 2)))
581 name
= editor_get_word_at_pos(editor
, pos
- 1, NULL
);
585 tags
= tm_workspace_find(name
, tm_tag_max_t
, NULL
, FALSE
, ft
->lang
);
587 if (!tags
|| tags
->len
== 0)
590 tag
= g_ptr_array_index(tags
, 0);
591 name
= tag
->atts
.entry
.var_type
;
594 TMWorkObject
*obj
= editor
->document
->tm_file
;
596 tags
= tm_workspace_find_scope_members(obj
? obj
->tags_array
: NULL
,
599 show_tags_list(editor
, tags
, 0);
604 static void on_char_added(GeanyEditor
*editor
, SCNotification
*nt
)
606 ScintillaObject
*sci
= editor
->sci
;
607 gint pos
= sci_get_current_position(sci
);
612 { /* simple indentation (only for CR format) */
613 if (sci_get_eol_mode(sci
) == SC_EOL_CR
)
614 on_new_line_added(editor
);
618 { /* simple indentation (for CR/LF and LF format) */
619 on_new_line_added(editor
);
623 editor_start_auto_complete(editor
, pos
, FALSE
); /* C/C++ ptr-> scope completion */
626 { /* close xml-tags */
627 handle_xml(editor
, pos
, nt
->ch
);
632 auto_close_chars(sci
, pos
, nt
->ch
);
634 editor_show_calltip(editor
, --pos
);
638 { /* hide calltips */
639 if (SSM(sci
, SCI_CALLTIPACTIVE
, 0, 0))
641 SSM(sci
, SCI_CALLTIPCANCEL
, 0, 0);
643 g_free(calltip
.text
);
655 auto_close_chars(sci
, pos
, nt
->ch
);
659 { /* closing bracket handling */
660 if (editor
->auto_indent
)
661 close_block(editor
, pos
- 1);
664 /* scope autocompletion */
666 case ':': /* C/C++ class:: syntax */
667 /* tag autocompletion */
670 if (! editor_start_auto_complete(editor
, pos
, FALSE
))
671 request_reshowing_calltip(nt
);
673 editor_start_auto_complete(editor
, pos
, FALSE
);
676 check_line_breaking(editor
, pos
, nt
->ch
);
680 /* expand() and fold_changed() are copied from SciTE (thanks) to fix #1923350. */
681 static void expand(ScintillaObject
*sci
, gint
*line
, gboolean doExpand
,
682 gboolean force
, gint visLevels
, gint level
)
684 gint lineMaxSubord
= SSM(sci
, SCI_GETLASTCHILD
, *line
, level
& SC_FOLDLEVELNUMBERMASK
);
685 gint levelLine
= level
;
687 while (*line
<= lineMaxSubord
)
689 if (G_UNLIKELY(force
))
692 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
694 SSM(sci
, SCI_HIDELINES
, *line
, *line
);
699 SSM(sci
, SCI_SHOWLINES
, *line
, *line
);
702 levelLine
= SSM(sci
, SCI_GETFOLDLEVEL
, *line
, 0);
703 if (levelLine
& SC_FOLDLEVELHEADERFLAG
)
705 if (G_UNLIKELY(force
))
708 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
710 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 0);
711 expand(sci
, line
, doExpand
, force
, visLevels
- 1, -1);
717 if (!sci_get_fold_expanded(sci
, *line
))
718 SSM(sci
, SCI_SETFOLDEXPANDED
, *line
, 1);
719 expand(sci
, line
, TRUE
, force
, visLevels
- 1, -1);
723 expand(sci
, line
, FALSE
, force
, visLevels
- 1, -1);
735 static void fold_changed(ScintillaObject
*sci
, gint line
, gint levelNow
, gint levelPrev
)
737 if (levelNow
& SC_FOLDLEVELHEADERFLAG
)
739 if (! (levelPrev
& SC_FOLDLEVELHEADERFLAG
))
741 /* Adding a fold point */
742 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
743 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
746 else if (levelPrev
& SC_FOLDLEVELHEADERFLAG
)
748 if (! sci_get_fold_expanded(sci
, line
))
749 { /* Removing the fold from one that has been contracted so should expand
750 * otherwise lines are left invisible with no way to make them visible */
751 SSM(sci
, SCI_SETFOLDEXPANDED
, line
, 1);
752 expand(sci
, &line
, TRUE
, FALSE
, 0, levelPrev
);
755 if (! (levelNow
& SC_FOLDLEVELWHITEFLAG
) &&
756 ((levelPrev
& SC_FOLDLEVELNUMBERMASK
) > (levelNow
& SC_FOLDLEVELNUMBERMASK
)))
758 /* See if should still be hidden */
759 gint parentLine
= sci_get_fold_parent(sci
, line
);
762 SSM(sci
, SCI_SHOWLINES
, line
, line
);
764 else if (sci_get_fold_expanded(sci
, parentLine
) &&
765 sci_get_line_is_visible(sci
, parentLine
))
767 SSM(sci
, SCI_SHOWLINES
, line
, line
);
773 static void ensure_range_visible(ScintillaObject
*sci
, gint posStart
, gint posEnd
,
774 gboolean enforcePolicy
)
776 gint lineStart
= sci_get_line_from_position(sci
, MIN(posStart
, posEnd
));
777 gint lineEnd
= sci_get_line_from_position(sci
, MAX(posStart
, posEnd
));
780 for (line
= lineStart
; line
<= lineEnd
; line
++)
782 SSM(sci
, enforcePolicy
? SCI_ENSUREVISIBLEENFORCEPOLICY
: SCI_ENSUREVISIBLE
, line
, 0);
787 static void auto_update_margin_width(GeanyEditor
*editor
)
789 gint next_linecount
= 1;
790 gint linecount
= sci_get_line_count(editor
->sci
);
791 GeanyDocument
*doc
= editor
->document
;
793 while (next_linecount
<= linecount
)
794 next_linecount
*= 10;
796 if (editor
->document
->priv
->line_count
!= next_linecount
)
798 doc
->priv
->line_count
= next_linecount
;
799 sci_set_line_numbers(editor
->sci
, TRUE
, 0);
804 static void partial_complete(ScintillaObject
*sci
, const gchar
*text
)
806 gint pos
= sci_get_current_position(sci
);
808 sci_insert_text(sci
, pos
, text
);
809 sci_set_current_position(sci
, pos
+ strlen(text
), TRUE
);
813 /* Complete the next word part from @a entry */
814 static gboolean
check_partial_completion(GeanyEditor
*editor
, const gchar
*entry
)
816 gchar
*stem
, *ptr
, *text
= utils_strdupa(entry
);
818 read_current_word(editor
, -1, current_word
, sizeof current_word
, NULL
, TRUE
);
820 if (strstr(text
, stem
) != text
)
821 return FALSE
; /* shouldn't happen */
822 if (strlen(text
) <= strlen(stem
))
825 text
+= strlen(stem
); /* skip stem */
826 ptr
= strstr(text
+ 1, "_");
830 partial_complete(editor
->sci
, text
);
836 foreach_str(ptr
, text
+ 1)
840 if (g_ascii_isupper(*ptr
) && g_ascii_islower(ptr
[1]))
843 partial_complete(editor
->sci
, text
);
852 /* Callback for the "sci-notify" signal to emit a "editor-notify" signal.
853 * Plugins can connect to the "editor-notify" signal. */
854 void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget
*widget
, G_GNUC_UNUSED gint scn
,
855 gpointer scnt
, gpointer data
)
857 GeanyEditor
*editor
= data
;
860 g_return_if_fail(editor
!= NULL
);
862 g_signal_emit_by_name(geany_object
, "editor-notify", editor
, scnt
, &retval
);
866 static gboolean
on_editor_notify(G_GNUC_UNUSED GObject
*object
, GeanyEditor
*editor
,
867 SCNotification
*nt
, G_GNUC_UNUSED gpointer data
)
869 ScintillaObject
*sci
= editor
->sci
;
870 GeanyDocument
*doc
= editor
->document
;
872 switch (nt
->nmhdr
.code
)
874 case SCN_SAVEPOINTLEFT
:
875 document_set_text_changed(doc
, TRUE
);
878 case SCN_SAVEPOINTREACHED
:
879 document_set_text_changed(doc
, FALSE
);
882 case SCN_MODIFYATTEMPTRO
:
886 case SCN_MARGINCLICK
:
887 on_margin_click(editor
, nt
);
891 on_update_ui(editor
, nt
);
895 if (editor_prefs
.show_linenumber_margin
&& (nt
->modificationType
& (SC_MOD_INSERTTEXT
| SC_MOD_DELETETEXT
)) && nt
->linesAdded
)
897 /* automatically adjust Scintilla's line numbers margin width */
898 auto_update_margin_width(editor
);
900 if (nt
->modificationType
& SC_STARTACTION
&& ! ignore_callback
)
902 /* get notified about undo changes */
903 document_undo_add(doc
, UNDO_SCINTILLA
, NULL
);
905 if (editor_prefs
.folding
&& (nt
->modificationType
& SC_MOD_CHANGEFOLD
) != 0)
907 /* handle special fold cases, e.g. #1923350 */
908 fold_changed(sci
, nt
->line
, nt
->foldLevelNow
, nt
->foldLevelPrev
);
913 on_char_added(editor
, nt
);
916 case SCN_USERLISTSELECTION
:
917 if (nt
->listType
== 1)
919 sci_add_text(sci
, nt
->text
);
923 case SCN_AUTOCSELECTION
:
924 if (g_str_equal(nt
->text
, "..."))
931 case SCN_AUTOCCANCELLED
:
932 /* now that autocomplete is finishing or was cancelled, reshow calltips
933 * if they were showing */
934 request_reshowing_calltip(nt
);
938 case SCN_STYLENEEDED
:
939 geany_debug("style");
943 ensure_range_visible(sci
, nt
->position
, nt
->position
+ nt
->length
, FALSE
);
947 if (nt
->text
!= NULL
)
949 document_open_file_list(nt
->text
, -1);
953 case SCN_CALLTIPCLICK
:
954 if (nt
->position
> 0)
956 switch (nt
->position
)
958 case 1: /* up arrow */
959 if (calltip
.tag_index
> 0)
963 case 2: calltip
.tag_index
++; break; /* down arrow */
965 editor_show_calltip(editor
, -1);
969 /* we always return FALSE here to let plugins handle the event too */
974 /* Note: this is the same as sci_get_tab_width(), but is still useful when you don't have
975 * a scintilla pointer. */
976 static gint
get_tab_width(const GeanyIndentPrefs
*indent_prefs
)
978 if (indent_prefs
->type
== GEANY_INDENT_TYPE_BOTH
)
979 return indent_prefs
->hard_tab_width
;
981 return indent_prefs
->width
; /* tab width = indent width */
985 /* Returns a string containing width chars of whitespace, filled with simple space
986 * characters or with the right number of tab characters, according to the indent prefs.
987 * (Result is filled with tabs *and* spaces if width isn't a multiple of
990 get_whitespace(const GeanyIndentPrefs
*iprefs
, gint width
)
992 g_return_val_if_fail(width
>= 0, NULL
);
997 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
999 return g_strnfill(width
, ' ');
1002 { /* first fill text with tabs and fill the rest with spaces */
1003 const gint tab_width
= get_tab_width(iprefs
);
1004 gint tabs
= width
/ tab_width
;
1005 gint spaces
= width
% tab_width
;
1006 gint len
= tabs
+ spaces
;
1009 str
= g_malloc(len
+ 1);
1011 memset(str
, '\t', tabs
);
1012 memset(str
+ tabs
, ' ', spaces
);
1019 static const GeanyIndentPrefs
*
1020 get_default_indent_prefs(void)
1022 static GeanyIndentPrefs iprefs
;
1024 iprefs
= app
->project
? *app
->project
->priv
->indentation
: *editor_prefs
.indentation
;
1029 /** Gets the indentation prefs for the editor.
1030 * In future, the prefs might be different according to project or filetype.
1031 * @warning Always get a fresh result instead of keeping a pointer to it if the editor
1032 * settings may have changed, or if this function has been called for a different @a editor.
1033 * @param editor The editor, or @c NULL to get the default indent prefs.
1034 * @return The indent prefs. */
1035 const GeanyIndentPrefs
*
1036 editor_get_indent_prefs(GeanyEditor
*editor
)
1038 static GeanyIndentPrefs iprefs
;
1040 iprefs
= *get_default_indent_prefs();
1045 iprefs
.type
= editor
->indent_type
;
1047 /* if per-document auto-indent is enabled, but we don't have a global mode set,
1048 * just use basic auto-indenting */
1049 if (editor
->auto_indent
&& iprefs
.auto_indent_mode
== GEANY_AUTOINDENT_NONE
)
1050 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_BASIC
;
1052 if (!editor
->auto_indent
)
1053 iprefs
.auto_indent_mode
= GEANY_AUTOINDENT_NONE
;
1059 static void on_new_line_added(GeanyEditor
*editor
)
1061 ScintillaObject
*sci
= editor
->sci
;
1062 gint line
= sci_get_current_line(sci
);
1064 /* simple indentation */
1065 if (editor
->auto_indent
)
1067 insert_indent_after_line(editor
, line
- 1);
1070 if (editor_prefs
.auto_continue_multiline
)
1071 { /* " * " auto completion in multiline C/C++/D/Java comments */
1072 auto_multiline(editor
, line
);
1075 if (editor_prefs
.newline_strip
)
1076 { /* strip the trailing spaces on the previous line */
1077 editor_strip_line_trailing_spaces(editor
, line
- 1);
1082 static gboolean
lexer_has_braces(ScintillaObject
*sci
)
1084 gint lexer
= sci_get_lexer(sci
);
1090 case SCLEX_HTML
: /* for PHP & JS */
1091 case SCLEX_PASCAL
: /* for multiline comments? */
1102 /* Read indent chars for the line that pos is on into indent global variable.
1103 * Note: Use sci_get_line_indentation() and get_whitespace()/editor_insert_text_block()
1104 * instead in any new code. */
1105 static void read_indent(GeanyEditor
*editor
, gint pos
)
1107 ScintillaObject
*sci
= editor
->sci
;
1108 guint i
, len
, j
= 0;
1112 line
= sci_get_line_from_position(sci
, pos
);
1114 len
= sci_get_line_length(sci
, line
);
1115 linebuf
= sci_get_line(sci
, line
);
1117 for (i
= 0; i
< len
&& j
<= (sizeof(indent
) - 1); i
++)
1119 if (linebuf
[i
] == ' ' || linebuf
[i
] == '\t') /* simple indentation */
1120 indent
[j
++] = linebuf
[i
];
1129 static gint
get_brace_indent(ScintillaObject
*sci
, gint line
)
1135 len
= sci_get_line_length(sci
, line
);
1136 linebuf
= sci_get_line(sci
, line
);
1138 for (i
= 0; i
< len
; i
++)
1140 /* i == (len - 1) prevents wrong indentation after lines like
1141 * " { return bless({}, shift); }" (Perl) */
1142 if (linebuf
[i
] == '{' && i
== (len
- 1))
1151 while (k
> 0 && isspace(linebuf
[k
])) k
--;
1153 /* if last non-whitespace character is a { increase indentation by a tab
1154 * e.g. for (...) { */
1155 if (linebuf
[k
] == '{')
1167 static gint
get_python_indent(ScintillaObject
*sci
, gint line
)
1169 gint last_char
= sci_get_line_end_position(sci
, line
) - 1;
1171 /* add extra indentation for Python after colon */
1172 if (sci_get_char_at(sci
, last_char
) == ':' &&
1173 sci_get_style_at(sci
, last_char
) == SCE_P_OPERATOR
)
1181 static gint
get_indent_size_after_line(GeanyEditor
*editor
, gint line
)
1183 ScintillaObject
*sci
= editor
->sci
;
1185 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1187 g_return_val_if_fail(line
>= 0, 0);
1189 size
= sci_get_line_indentation(sci
, line
);
1191 if (iprefs
->auto_indent_mode
> GEANY_AUTOINDENT_BASIC
)
1193 if (lexer_has_braces(sci
))
1194 size
+= iprefs
->width
* get_brace_indent(sci
, line
);
1196 if (FILETYPE_ID(editor
->document
->file_type
) == GEANY_FILETYPES_PYTHON
)
1197 size
+= iprefs
->width
* get_python_indent(sci
, line
);
1203 static void insert_indent_after_line(GeanyEditor
*editor
, gint line
)
1205 ScintillaObject
*sci
= editor
->sci
;
1206 gint line_indent
= sci_get_line_indentation(sci
, line
);
1207 gint size
= get_indent_size_after_line(editor
, line
);
1208 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1214 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
&& size
== line_indent
)
1216 /* support tab indents, space aligns style - copy last line 'indent' exactly */
1217 gint start
= sci_get_position_from_line(sci
, line
);
1218 gint end
= sci_get_line_indent_position(sci
, line
);
1220 text
= sci_get_contents_range(sci
, start
, end
);
1224 text
= get_whitespace(iprefs
, size
);
1226 sci_add_text(sci
, text
);
1231 static void auto_close_chars(ScintillaObject
*sci
, gint pos
, gchar c
)
1233 const gchar
*closing_char
= NULL
;
1236 if (utils_isbrace(c
, 0))
1237 end_pos
= sci_find_matching_brace(sci
, pos
- 1);
1242 if ((editor_prefs
.autoclose_chars
& GEANY_AC_PARENTHESIS
) && end_pos
== -1)
1246 if ((editor_prefs
.autoclose_chars
& GEANY_AC_CBRACKET
) && end_pos
== -1)
1250 if ((editor_prefs
.autoclose_chars
& GEANY_AC_SBRACKET
) && end_pos
== -1)
1254 if (editor_prefs
.autoclose_chars
& GEANY_AC_SQUOTE
)
1258 if (editor_prefs
.autoclose_chars
& GEANY_AC_DQUOTE
)
1259 closing_char
= "\"";
1263 if (closing_char
!= NULL
)
1265 sci_add_text(sci
, closing_char
);
1266 sci_set_current_position(sci
, pos
, TRUE
);
1271 /* Finds a corresponding matching brace to the given pos
1272 * (this is taken from Scintilla Editor.cxx,
1273 * fit to work with close_block) */
1274 static gint
brace_match(ScintillaObject
*sci
, gint pos
)
1276 gchar chBrace
= sci_get_char_at(sci
, pos
);
1277 gchar chSeek
= utils_brace_opposite(chBrace
);
1279 gint direction
= -1;
1284 styBrace
= sci_get_style_at(sci
, pos
);
1286 if (utils_is_opening_brace(chBrace
, editor_prefs
.brace_match_ltgt
))
1289 pos
= pos
+ direction
;
1290 while ((pos
>= 0) && (pos
< sci_get_length(sci
)))
1292 chAtPos
= sci_get_char_at(sci
, pos
- 1);
1293 styAtPos
= sci_get_style_at(sci
, pos
);
1295 if ((pos
> sci_get_end_styled(sci
)) || (styAtPos
== styBrace
))
1297 if (chAtPos
== chBrace
)
1299 if (chAtPos
== chSeek
)
1304 pos
= pos
+ direction
;
1310 /* Called after typing '}'. */
1311 static void close_block(GeanyEditor
*editor
, gint pos
)
1314 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
1315 gint x
= 0, cnt
= 0;
1316 gint line
, line_len
, eol_char_len
;
1317 gchar
*text
, *line_buf
;
1318 ScintillaObject
*sci
;
1319 gint line_indent
, last_indent
;
1321 if (iprefs
->auto_indent_mode
< GEANY_AUTOINDENT_CURRENTCHARS
)
1323 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
1326 doc
= editor
->document
;
1328 if (! lexer_has_braces(sci
))
1331 line
= sci_get_line_from_position(sci
, pos
);
1332 line_len
= sci_get_line_length(sci
, line
);
1333 /* set eol_char_len to 0 if on last line, because there is no EOL char */
1334 eol_char_len
= (line
== (sci_get_line_count(sci
) - 1)) ? 0 :
1335 editor_get_eol_char_len(editor
);
1337 /* check that the line is empty, to not kill text in the line */
1338 line_buf
= sci_get_line(sci
, line
);
1339 line_buf
[line_len
- eol_char_len
] = '\0';
1340 while (x
< (line_len
- eol_char_len
))
1342 if (isspace(line_buf
[x
]))
1348 if ((line_len
- eol_char_len
- 1) != cnt
)
1351 if (iprefs
->auto_indent_mode
== GEANY_AUTOINDENT_MATCHBRACES
)
1353 gint start_brace
= brace_match(sci
, pos
);
1355 if (start_brace
>= 0)
1358 gint brace_line
= sci_get_line_from_position(sci
, start_brace
);
1359 gint size
= sci_get_line_indentation(sci
, brace_line
);
1360 gchar
*ind
= get_whitespace(iprefs
, size
);
1362 text
= g_strconcat(ind
, "}", NULL
);
1363 line_start
= sci_get_position_from_line(sci
, line
);
1364 sci_set_anchor(sci
, line_start
);
1365 sci_replace_sel(sci
, text
);
1370 /* fall through - unmatched brace (possibly because of TCL, PHP lexer bugs) */
1373 /* GEANY_AUTOINDENT_CURRENTCHARS */
1374 line_indent
= sci_get_line_indentation(sci
, line
);
1375 last_indent
= sci_get_line_indentation(sci
, line
- 1);
1377 if (line_indent
< last_indent
)
1379 line_indent
-= iprefs
->width
;
1380 line_indent
= MAX(0, line_indent
);
1381 sci_set_line_indentation(sci
, line
, line_indent
);
1385 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1386 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1387 * position can be -1, then the current position is used.
1388 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1389 static void read_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1390 const gchar
*wc
, gboolean stem
)
1392 gint line
, line_start
, startword
, endword
;
1394 ScintillaObject
*sci
;
1396 g_return_if_fail(editor
!= NULL
);
1400 pos
= sci_get_current_position(sci
);
1402 line
= sci_get_line_from_position(sci
, pos
);
1403 line_start
= sci_get_position_from_line(sci
, line
);
1404 startword
= pos
- line_start
;
1405 endword
= pos
- line_start
;
1408 chunk
= sci_get_line(sci
, line
);
1411 wc
= GEANY_WORDCHARS
;
1413 /* the checks for "c < 0" are to allow any Unicode character which should make the code
1414 * a little bit more Unicode safe, anyway, this allows also any Unicode punctuation,
1415 * TODO: improve this code */
1416 while (startword
> 0 && (strchr(wc
, chunk
[startword
- 1]) || chunk
[startword
- 1] < 0))
1420 while (chunk
[endword
] != 0 && (strchr(wc
, chunk
[endword
]) || chunk
[endword
] < 0))
1424 if (startword
!= endword
)
1426 chunk
[endword
] = '\0';
1428 g_strlcpy(word
, chunk
+ startword
, wordlen
); /* ensure null terminated */
1431 g_strlcpy(word
, "", wordlen
);
1437 /* Reads the word at given cursor position and writes it into the given buffer. The buffer will be
1438 * NULL terminated in any case, even when the word is truncated because wordlen is too small.
1439 * position can be -1, then the current position is used.
1440 * wc are the wordchars to use, if NULL, GEANY_WORDCHARS will be used */
1441 void editor_find_current_word(GeanyEditor
*editor
, gint pos
, gchar
*word
, size_t wordlen
,
1444 read_current_word(editor
, pos
, word
, wordlen
, wc
, FALSE
);
1449 * Finds the word at the position specified by @a pos. If any word is found, it is returned.
1450 * Otherwise NULL is returned.
1451 * Additional wordchars can be specified to define what to consider as a word.
1453 * @param editor The editor to operate on.
1454 * @param pos The position where the word should be read from.
1455 * Maybe @c -1 to use the current position.
1456 * @param wordchars The wordchars to separate words. wordchars mean all characters to count
1457 * as part of a word. Maybe @c NULL to use the default wordchars,
1458 * see @ref GEANY_WORDCHARS.
1460 * @return A newly-allocated string containing the word at the given @a pos or @c NULL.
1461 * Should be freed when no longer needed.
1465 gchar
*editor_get_word_at_pos(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1467 static gchar cword
[GEANY_MAX_WORD_LENGTH
];
1469 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1471 read_current_word(editor
, pos
, cword
, sizeof(cword
), wordchars
, FALSE
);
1473 return (*cword
== '\0') ? NULL
: g_strdup(cword
);
1477 /* Read the word up to position @a pos. */
1478 static const gchar
*
1479 editor_read_word_stem(GeanyEditor
*editor
, gint pos
, const gchar
*wordchars
)
1481 static gchar word
[GEANY_MAX_WORD_LENGTH
];
1483 read_current_word(editor
, pos
, word
, sizeof word
, wordchars
, TRUE
);
1485 return (*word
) ? word
: NULL
;
1489 static gint
find_previous_brace(ScintillaObject
*sci
, gint pos
)
1492 gint orig_pos
= pos
;
1494 c
= sci_get_char_at(sci
, pos
);
1495 while (pos
>= 0 && pos
> orig_pos
- 300)
1497 c
= sci_get_char_at(sci
, pos
);
1499 if (utils_is_opening_brace(c
, editor_prefs
.brace_match_ltgt
))
1506 static gint
find_start_bracket(ScintillaObject
*sci
, gint pos
)
1510 gint orig_pos
= pos
;
1512 c
= sci_get_char_at(sci
, pos
);
1513 while (pos
> 0 && pos
> orig_pos
- 300)
1515 c
= sci_get_char_at(sci
, pos
);
1516 if (c
== ')') brackets
++;
1517 else if (c
== '(') brackets
--;
1519 if (brackets
< 0) return pos
; /* found start bracket */
1525 static gboolean
append_calltip(GString
*str
, const TMTag
*tag
, filetype_id ft_id
)
1527 if (! tag
->atts
.entry
.arglist
)
1530 if (ft_id
!= GEANY_FILETYPES_PASCAL
)
1531 { /* usual calltips: "retval tagname (arglist)" */
1532 if (tag
->atts
.entry
.var_type
)
1536 g_string_append(str
, tag
->atts
.entry
.var_type
);
1537 for (i
= 0; i
< tag
->atts
.entry
.pointerOrder
; i
++)
1539 g_string_append_c(str
, '*');
1541 g_string_append_c(str
, ' ');
1543 if (tag
->atts
.entry
.scope
)
1545 const gchar
*cosep
= symbols_get_context_separator(ft_id
);
1547 g_string_append(str
, tag
->atts
.entry
.scope
);
1548 g_string_append(str
, cosep
);
1550 g_string_append(str
, tag
->name
);
1551 g_string_append_c(str
, ' ');
1552 g_string_append(str
, tag
->atts
.entry
.arglist
);
1555 { /* special case Pascal calltips: "tagname (arglist) : retval" */
1556 g_string_append(str
, tag
->name
);
1557 g_string_append_c(str
, ' ');
1558 g_string_append(str
, tag
->atts
.entry
.arglist
);
1560 if (NZV(tag
->atts
.entry
.var_type
))
1562 g_string_append(str
, " : ");
1563 g_string_append(str
, tag
->atts
.entry
.var_type
);
1571 static gchar
*find_calltip(const gchar
*word
, GeanyFiletype
*ft
)
1573 const GPtrArray
*tags
;
1574 const gint arg_types
= tm_tag_function_t
| tm_tag_prototype_t
|
1575 tm_tag_method_t
| tm_tag_macro_with_arg_t
;
1576 TMTagAttrType
*attrs
= NULL
;
1578 GString
*str
= NULL
;
1581 g_return_val_if_fail(ft
&& word
&& *word
, NULL
);
1583 /* use all types in case language uses wrong tag type e.g. python "members" instead of "methods" */
1584 tags
= tm_workspace_find(word
, tm_tag_max_t
, attrs
, FALSE
, ft
->lang
);
1588 tag
= TM_TAG(tags
->pdata
[0]);
1590 if (tag
->type
== tm_tag_class_t
&& FILETYPE_ID(ft
) == GEANY_FILETYPES_D
)
1592 /* user typed e.g. 'new Classname(' so lookup D constructor Classname::this() */
1593 tags
= tm_workspace_find_scoped("this", tag
->name
,
1594 arg_types
, attrs
, FALSE
, ft
->lang
, TRUE
);
1599 /* remove tags with no argument list */
1600 for (i
= 0; i
< tags
->len
; i
++)
1602 tag
= TM_TAG(tags
->pdata
[i
]);
1604 if (! tag
->atts
.entry
.arglist
)
1605 tags
->pdata
[i
] = NULL
;
1607 tm_tags_prune((GPtrArray
*) tags
);
1611 { /* remove duplicate calltips */
1612 TMTagAttrType sort_attr
[] = {tm_tag_attr_name_t
, tm_tag_attr_scope_t
,
1613 tm_tag_attr_arglist_t
, 0};
1615 tm_tags_sort((GPtrArray
*) tags
, sort_attr
, TRUE
);
1618 /* if the current word has changed since last time, start with the first tag match */
1619 if (! utils_str_equal(word
, calltip
.last_word
))
1620 calltip
.tag_index
= 0;
1621 /* cache the current word for next time */
1622 g_free(calltip
.last_word
);
1623 calltip
.last_word
= g_strdup(word
);
1624 calltip
.tag_index
= MIN(calltip
.tag_index
, tags
->len
- 1); /* ensure tag_index is in range */
1626 for (i
= calltip
.tag_index
; i
< tags
->len
; i
++)
1628 tag
= TM_TAG(tags
->pdata
[i
]);
1632 str
= g_string_new(NULL
);
1633 if (calltip
.tag_index
> 0)
1634 g_string_prepend(str
, "\001 "); /* up arrow */
1635 append_calltip(str
, tag
, FILETYPE_ID(ft
));
1637 else /* add a down arrow */
1639 if (calltip
.tag_index
> 0) /* already have an up arrow */
1640 g_string_insert_c(str
, 1, '\002');
1642 g_string_prepend(str
, "\002 ");
1648 gchar
*result
= str
->str
;
1650 g_string_free(str
, FALSE
);
1657 /* use pos = -1 to search for the previous unmatched open bracket. */
1658 gboolean
editor_show_calltip(GeanyEditor
*editor
, gint pos
)
1660 gint orig_pos
= pos
; /* the position for the calltip */
1663 gchar word
[GEANY_MAX_WORD_LENGTH
];
1665 ScintillaObject
*sci
;
1667 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1668 g_return_val_if_fail(editor
->document
->file_type
!= NULL
, FALSE
);
1672 lexer
= sci_get_lexer(sci
);
1676 /* position of '(' is unknown, so go backwards from current position to find it */
1677 pos
= sci_get_current_position(sci
);
1680 pos
= (lexer
== SCLEX_LATEX
) ? find_previous_brace(sci
, pos
) :
1681 find_start_bracket(sci
, pos
);
1686 /* the style 1 before the brace (which may be highlighted) */
1687 style
= sci_get_style_at(sci
, pos
- 1);
1688 if (! is_code_style(lexer
, style
))
1692 editor_find_current_word(editor
, pos
- 1, word
, sizeof word
, NULL
);
1693 if (word
[0] == '\0')
1696 str
= find_calltip(word
, editor
->document
->file_type
);
1699 g_free(calltip
.text
); /* free the old calltip */
1701 calltip
.pos
= orig_pos
;
1704 utils_wrap_string(calltip
.text
, -1);
1705 SSM(sci
, SCI_CALLTIPSHOW
, orig_pos
, (sptr_t
) calltip
.text
);
1712 gchar
*editor_get_calltip_text(GeanyEditor
*editor
, const TMTag
*tag
)
1716 g_return_val_if_fail(editor
!= NULL
, NULL
);
1718 str
= g_string_new(NULL
);
1719 if (append_calltip(str
, tag
, FILETYPE_ID(editor
->document
->file_type
)))
1720 return g_string_free(str
, FALSE
);
1722 return g_string_free(str
, TRUE
);
1726 /* HTML entities auto completion from html_entities.tags text file */
1728 autocomplete_html(ScintillaObject
*sci
, const gchar
*root
, gsize rootlen
)
1731 gboolean found
= FALSE
;
1733 const gchar
**entities
= symbols_get_html_entities();
1735 if (*root
!= '&' || G_UNLIKELY(entities
== NULL
))
1738 words
= g_string_sized_new(500);
1741 if (entities
[i
] == NULL
)
1743 else if (entities
[i
][0] == '#')
1746 if (! strncmp(entities
[i
], root
, rootlen
))
1749 g_string_append_c(words
, '\n');
1750 g_string_append(words
, entities
[i
]);
1755 show_autocomplete(sci
, rootlen
, words
->str
);
1757 g_string_free(words
, TRUE
);
1762 /* Current document & global tags autocompletion */
1764 autocomplete_tags(GeanyEditor
*editor
, const gchar
*root
, gsize rootlen
)
1766 TMTagAttrType attrs
[] = { tm_tag_attr_name_t
, 0 };
1767 const GPtrArray
*tags
;
1770 g_return_val_if_fail(editor
, FALSE
);
1772 doc
= editor
->document
;
1774 tags
= tm_workspace_find(root
, tm_tag_max_t
, attrs
, TRUE
, doc
->file_type
->lang
);
1777 show_tags_list(editor
, tags
, rootlen
);
1778 return tags
->len
> 0;
1784 /* Check whether to use entity autocompletion:
1785 * - always in a HTML file except when inside embedded JavaScript, Python, ASP, ...
1786 * - in a PHP file only when we are outside of <? ?> */
1787 static gboolean
autocomplete_check_for_html(gint ft_id
, gint style
)
1789 /* use entity completion when style is not JavaScript, ASP, Python, PHP, ...
1790 * (everything after SCE_HJ_START is for embedded scripting languages) */
1791 if (ft_id
== GEANY_FILETYPES_HTML
&& style
< SCE_HJ_START
)
1794 if (ft_id
== GEANY_FILETYPES_PHP
)
1796 /* use entity completion when style is outside of PHP styles */
1797 if (! is_style_php(style
))
1805 /* Algorithm based on based on Scite's StartAutoCompleteWord() */
1806 static GString
*get_doc_words(ScintillaObject
*sci
, gchar
*root
, gsize rootlen
)
1809 gint len
, current
, word_end
;
1810 gint pos_find
, flags
;
1814 struct Sci_TextToFind ttf
;
1816 len
= sci_get_length(sci
);
1817 current
= sci_get_current_position(sci
) - rootlen
;
1819 ttf
.lpstrText
= root
;
1821 ttf
.chrg
.cpMax
= len
;
1822 ttf
.chrgText
.cpMin
= 0;
1823 ttf
.chrgText
.cpMax
= 0;
1824 flags
= SCFIND_WORDSTART
| SCFIND_MATCHCASE
;
1826 words
= g_string_sized_new(256);
1827 /* put space before first entry to make searching with strstr easy */
1828 g_string_append_c(words
, ' ');
1830 /* search the whole document for the word root and collect results */
1831 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
1832 while (pos_find
>= 0 && pos_find
< len
)
1834 word_end
= pos_find
+ rootlen
;
1835 if (pos_find
!= current
)
1837 while (word_end
< len
&& strchr(GEANY_WORDCHARS
, sci_get_char_at(sci
, word_end
)) != NULL
)
1840 word_length
= word_end
- pos_find
;
1841 if (word_length
> rootlen
)
1843 word
= g_malloc0(word_length
+ 3);
1844 sci_get_text_range(sci
, pos_find
, word_end
, word
+ 1);
1846 word
[word_length
+ 1] = ' ';
1847 /* search the words string whether we already have the word in, otherwise add it */
1848 if (strstr(words
->str
, word
) == NULL
)
1850 g_string_append(words
, word
+ 1);
1855 if (nmatches
== editor_prefs
.autocompletion_max_entries
)
1857 g_string_append(words
, "... ");
1862 ttf
.chrg
.cpMin
= word_end
;
1863 pos_find
= scintilla_send_message(sci
, SCI_FINDTEXT
, flags
, (uptr_t
) &ttf
);
1868 g_strdelimit(words
->str
, " ", '\n');
1869 words
->str
[words
->len
- 1] = '\0'; /* remove the trailing '\n' */
1872 g_string_free(words
, TRUE
);
1877 static gboolean
autocomplete_doc_word(GeanyEditor
*editor
, gchar
*root
, gsize rootlen
)
1879 ScintillaObject
*sci
= editor
->sci
;
1883 GSList
*node
, *list
= NULL
;
1885 words
= get_doc_words(sci
, root
, rootlen
);
1888 scintilla_send_message(sci
, SCI_AUTOCCANCEL
, 0, 0);
1892 /* words are unsorted, make list of words */
1893 foreach_str(ptr
, words
->str
)
1897 list
= g_slist_prepend(list
, ptr
+ 1);
1898 /* terminate previous string in list */
1903 list
= g_slist_sort(list
, (GCompareFunc
)utils_str_casecmp
);
1905 str
= g_string_sized_new(words
->len
);
1906 foreach_slist(node
, list
)
1908 g_string_append(str
, node
->data
);
1910 g_string_append_c(str
, '\n');
1913 g_string_free(words
, TRUE
);
1915 show_autocomplete(sci
, rootlen
, str
->str
);
1916 g_string_free(str
, TRUE
);
1921 gboolean
editor_start_auto_complete(GeanyEditor
*editor
, gint pos
, gboolean force
)
1923 gint line
, line_start
, line_len
, line_pos
, current
, rootlen
, startword
, lexer
, style
;
1924 gchar
*linebuf
, *root
;
1925 ScintillaObject
*sci
;
1926 gboolean ret
= FALSE
;
1930 if (! editor_prefs
.auto_complete_symbols
&& ! force
)
1933 g_return_val_if_fail(editor
!= NULL
, FALSE
);
1935 /* If we are at the beginning of the document, we skip autocompletion as we can't determine the
1936 * necessary styling information */
1937 if (G_UNLIKELY(pos
< 2))
1941 ft
= editor
->document
->file_type
;
1943 line
= sci_get_line_from_position(sci
, pos
);
1944 line_start
= sci_get_position_from_line(sci
, line
);
1945 line_len
= sci_get_line_length(sci
, line
);
1946 line_pos
= pos
- line_start
- 1;
1947 current
= pos
- line_start
;
1948 startword
= current
;
1949 lexer
= sci_get_lexer(sci
);
1950 style
= sci_get_style_at(sci
, pos
- 2);
1952 /* don't autocomplete in comments and strings */
1953 if (!force
&& !is_code_style(lexer
, style
))
1956 autocomplete_scope(editor
);
1958 linebuf
= sci_get_line(sci
, line
);
1960 if (ft
->id
== GEANY_FILETYPES_LATEX
)
1961 wordchars
= GEANY_WORDCHARS
"\\"; /* add \ to word chars if we are in a LaTeX file */
1962 else if (ft
->id
== GEANY_FILETYPES_HTML
|| ft
->id
== GEANY_FILETYPES_PHP
)
1963 wordchars
= GEANY_WORDCHARS
"&"; /* add & to word chars if we are in a PHP or HTML file */
1965 wordchars
= GEANY_WORDCHARS
;
1967 /* find the start of the current word */
1968 while ((startword
> 0) && (strchr(wordchars
, linebuf
[startword
- 1])))
1970 linebuf
[current
] = '\0';
1971 root
= linebuf
+ startword
;
1972 rootlen
= current
- startword
;
1976 if (autocomplete_check_for_html(ft
->id
, style
))
1978 /* Allow something like ""some text"". The above startword calculation
1979 * only works on words but for HTML entity completion we also want to have completion
1980 * based on '&' within words. */
1981 gchar
*tmp
= strchr(root
, '&');
1985 rootlen
= strlen(tmp
);
1987 ret
= autocomplete_html(sci
, root
, rootlen
);
1991 /* force is set when called by keyboard shortcut, otherwise start at the
1992 * editor_prefs.symbolcompletion_min_chars'th char */
1993 if (force
|| rootlen
>= editor_prefs
.symbolcompletion_min_chars
)
1995 /* complete tags, except if forcing when completion is already visible */
1996 if (!(force
&& SSM(sci
, SCI_AUTOCACTIVE
, 0, 0)))
1997 ret
= autocomplete_tags(editor
, root
, rootlen
);
1999 /* If forcing and there's nothing else to show, complete from words in document */
2000 if (!ret
&& (force
|| editor_prefs
.autocomplete_doc_words
))
2001 ret
= autocomplete_doc_word(editor
, root
, rootlen
);
2012 static gchar
*snippets_find_completion_by_name(const gchar
*type
, const gchar
*name
)
2014 gchar
*result
= NULL
;
2017 g_return_val_if_fail(type
!= NULL
&& name
!= NULL
, NULL
);
2019 tmp
= g_hash_table_lookup(snippet_hash
, type
);
2022 result
= g_hash_table_lookup(tmp
, name
);
2024 /* whether nothing is set for the current filetype(tmp is NULL) or
2025 * the particular completion for this filetype is not set (result is NULL) */
2026 if (tmp
== NULL
|| result
== NULL
)
2028 tmp
= g_hash_table_lookup(snippet_hash
, "Default");
2031 result
= g_hash_table_lookup(tmp
, name
);
2034 /* if result is still NULL here, no completion could be found */
2036 /* result is owned by the hash table and will be freed when the table will destroyed */
2037 return g_strdup(result
);
2041 /* This is very ugly but passing the pattern to ac_replace_specials() doesn't work because it is
2042 * modified when replacing a completion but the foreach function still passes the old pointer
2043 * to ac_replace_specials, so we use a global pointer outside of ac_replace_specials and
2044 * ac_complete_constructs. Any hints to improve this are welcome. */
2045 static GString
*snippets_global_pattern
= NULL
;
2047 void snippets_replace_specials(gpointer key
, gpointer value
, gpointer user_data
)
2051 g_return_if_fail(key
!= NULL
);
2052 g_return_if_fail(value
!= NULL
);
2054 needle
= g_strconcat("%", (gchar
*) key
, "%", NULL
);
2056 utils_string_replace_all(snippets_global_pattern
, needle
, (gchar
*) value
);
2061 static void snippets_replace_wildcards(GeanyEditor
*editor
, GString
*text
)
2063 const gchar
*file_name
= DOC_FILENAME(editor
->document
);
2064 gchar
*basename
= g_path_get_basename(file_name
);
2066 templates_replace_default_dates(text
);
2067 templates_replace_valist(text
, "{filename}", basename
, NULL
);
2068 templates_replace_command(text
, file_name
, editor
->document
->file_type
->name
, NULL
);
2074 /* this only works with spaces only indentation on the lines */
2075 static void fix_line_indents(GeanyEditor
*editor
, gint line_start
, gint line_end
)
2077 ScintillaObject
*sci
= editor
->sci
;
2078 gint line
, cur_line
, cur_col
, pos
;
2080 /* get the line, col position as fixing indentation will move cursor to start of line */
2081 pos
= sci_get_current_position(sci
);
2082 cur_col
= sci_get_col_from_position(sci
, pos
);
2083 cur_line
= sci_get_current_line(sci
);
2085 for (line
= line_start
; line
<= line_end
; line
++)
2087 gint size
= sci_get_line_indentation(sci
, line
);
2089 /* set to 0 first to trigger proper indent creation */
2090 sci_set_line_indentation(sci
, line
, 0);
2091 sci_set_line_indentation(sci
, line
, size
);
2093 pos
= scintilla_send_message(sci
, SCI_FINDCOLUMN
, cur_line
, cur_col
);
2094 sci_set_current_position(sci
, pos
, FALSE
);
2097 /* TODO: Fix \\t inside comment*/
2098 /** Inserts text, replacing \\t tab chars with the correct indent width, and \\n newline
2099 * chars with the correct line ending string.
2100 * @param editor The editor to operate on.
2101 * @param text Intended as e.g. "if (1)\n\tdo_something();"
2102 * @param insert_pos Position, where to start with inserting text block.
2103 * @param cursor_index If >= 0, the index into @a text to place the cursor.
2104 * @param newline_indent_size Indentation size (in spaces) to insert for each newline; use
2105 * -1 to read the indent size from the line with @a insert_pos on it.
2106 * @param replace_newlines Whether to replace newlines in text or not. If
2107 * newlines have been replaced before, this should be false, to avoid multiple
2108 * replacements of newlines, which is error prone on Windows.
2109 * @warning Make sure all \\t tab chars in @a text are intended as indent widths,
2110 * NOT any hard tabs (you get those when copying document text with the Tabs
2111 * & Spaces indent mode set).
2112 * @note This doesn't scroll the cursor in view afterwards. **/
2113 void editor_insert_text_block(GeanyEditor
*editor
, const gchar
*text
, gint insert_pos
,
2114 gint cursor_index
, gint newline_indent_size
, gboolean replace_newlines
)
2116 ScintillaObject
*sci
= editor
->sci
;
2117 gint line_start
= sci_get_line_from_position(sci
, insert_pos
);
2121 const gchar cur_marker
[] = "__GEANY_CURSOR_MARKER__";
2123 g_return_if_fail(text
);
2124 g_return_if_fail(editor
!= NULL
);
2126 buf
= g_string_new(text
);
2128 if (cursor_index
>= 0)
2129 g_string_insert(buf
, cursor_index
, cur_marker
); /* remember cursor pos */
2131 /* Add line indents (in spaces) */
2133 if (newline_indent_size
== -1)
2134 newline_indent_size
= sci_get_line_indentation(sci
, line_start
);
2136 if (newline_indent_size
> 0)
2138 whitespace
= g_strnfill(newline_indent_size
, ' ');
2139 setptr(whitespace
, g_strconcat("\n", whitespace
, NULL
));
2140 utils_string_replace_all(buf
, "\n", whitespace
);
2144 /* transform line endings */
2145 if (replace_newlines
)
2146 utils_string_replace_all(buf
, "\n", editor_get_eol_char(editor
));
2148 /* transform tabs into indent widths (in spaces) */
2149 whitespace
= g_strnfill(editor_get_indent_prefs(editor
)->width
, ' ');
2150 utils_string_replace_all(buf
, "\t", whitespace
);
2153 if (cursor_index
>= 0)
2155 gint idx
= utils_strpos(buf
->str
, cur_marker
);
2157 g_string_erase(buf
, idx
, strlen(cur_marker
));
2159 sci_insert_text(sci
, insert_pos
, buf
->str
);
2160 sci_set_current_position(sci
, insert_pos
+ idx
, FALSE
);
2163 sci_insert_text(sci
, insert_pos
, buf
->str
);
2165 /* fixup indentation (very useful for Tabs & Spaces indent type) */
2166 line_end
= sci_get_line_from_position(sci
, insert_pos
+ buf
->len
);
2167 fix_line_indents(editor
, line_start
, line_end
);
2168 snippet_cursor_insert_pos
= sci_get_current_position(sci
);
2170 g_string_free(buf
, TRUE
);
2174 /* Move the cursor to the next specified cursor position in an inserted snippet.
2175 * Can, and should, be optimized to give better results */
2176 void editor_goto_next_snippet_cursor(GeanyEditor
*editor
)
2178 ScintillaObject
*sci
= editor
->sci
;
2179 gint current_pos
= sci_get_current_position(sci
);
2181 if (snippet_offsets
&& !g_queue_is_empty(snippet_offsets
))
2185 offset
= GPOINTER_TO_INT(g_queue_pop_head(snippet_offsets
));
2186 if (current_pos
> snippet_cursor_insert_pos
)
2187 snippet_cursor_insert_pos
= offset
+ current_pos
;
2189 snippet_cursor_insert_pos
+= offset
;
2191 sci_set_current_position(sci
, snippet_cursor_insert_pos
, FALSE
);
2200 static gboolean
snippets_complete_constructs(GeanyEditor
*editor
, gint pos
, const gchar
*word
)
2202 ScintillaObject
*sci
= editor
->sci
;
2203 gchar
*str
, *whitespace
;
2205 gint i
, str_len
, tmp_pos
, whitespace_len
, nl_count
= 0;
2206 gssize cur_index
= -1;
2207 gint ft_id
= FILETYPE_ID(editor
->document
->file_type
);
2208 GHashTable
*specials
;
2209 GList
*temp_list
= NULL
;
2210 const GeanyIndentPrefs
*iprefs
;
2212 gint cursor_steps
, old_cursor
= 0;
2214 str
= g_strdup(word
);
2216 pattern
= g_string_new(snippets_find_completion_by_name(filetypes
[ft_id
]->name
, str
));
2217 if (pattern
== NULL
|| pattern
->len
== 0)
2220 g_string_free(pattern
, TRUE
);
2224 iprefs
= editor_get_indent_prefs(editor
);
2225 read_indent(editor
, pos
);
2226 indent_size
= strlen(indent
);
2228 /* remove the typed word, it will be added again by the used auto completion
2229 * (not really necessary but this makes the auto completion more flexible,
2230 * e.g. with a completion like hi=hello, so typing "hi<TAB>" will result in "hello") */
2231 str_len
= strlen(str
);
2232 sci_set_selection_start(sci
, pos
- str_len
);
2233 sci_set_selection_end(sci
, pos
);
2234 sci_replace_sel(sci
, "");
2235 pos
-= str_len
; /* pos has changed while deleting */
2237 /* replace 'special' completions */
2238 specials
= g_hash_table_lookup(snippet_hash
, "Special");
2239 if (G_LIKELY(specials
!= NULL
))
2241 /* ugly hack using global_pattern */
2242 snippets_global_pattern
= pattern
;
2243 g_hash_table_foreach(specials
, snippets_replace_specials
, NULL
);
2246 /* replace any %template% wildcards */
2247 snippets_replace_wildcards(editor
, pattern
);
2249 /* transform other wildcards */
2250 /* convert to %newlines%, else we get endless loops */
2251 utils_string_replace_all(pattern
, "\n", "%newline%");
2253 /* if spaces are used, replaces all tabs with %ws%, which is later replaced
2254 * by real whitespace characters
2255 * otherwise replace all %ws% by \t, which will be replaced later by tab
2257 * this makes seperating between tab and spaces intentation pretty easy */
2258 if (iprefs
->type
== GEANY_INDENT_TYPE_SPACES
)
2259 utils_string_replace_all(pattern
, "\t", "%ws%");
2261 utils_string_replace_all(pattern
, "%ws%", "\t");
2263 whitespace
= g_strnfill(iprefs
->width
, ' '); /* use spaces for indentation, will be fixed up */
2264 whitespace_len
= strlen(whitespace
);
2266 while ((cursor_steps
= utils_strpos(pattern
->str
, "%cursor%")) >= 0)
2268 /* replace every %newline% (up to next %cursor%) with EOL,
2269 * and update cursor_steps after */
2270 while ((tmp_pos
= utils_strpos(pattern
->str
, "%newline%")) < cursor_steps
&& tmp_pos
!= -1)
2273 utils_string_replace_first(pattern
, "%newline%", editor_get_eol_char(editor
));
2274 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2276 /* replace every %ws% (up to next %cursor%) with whitespaces,
2277 * and update cursor_steps after */
2278 while ((tmp_pos
= utils_strpos(pattern
->str
, "%ws%")) < cursor_steps
&& tmp_pos
!= -1)
2280 utils_string_replace_first(pattern
, "%ws%", whitespace
);
2281 cursor_steps
= utils_strpos(pattern
->str
, "%cursor%");
2283 /* finally replace the next %cursor% */
2284 utils_string_replace_first(pattern
, "%cursor%", "");
2286 /* modify cursor_steps to take indentation count and type into account */
2288 /* We're saving the relative offset to each cursor position in a simple
2289 * linked list, including intendations between them. */
2292 cursor_steps
+= (nl_count
* indent_size
);
2293 temp_list
= g_list_append(temp_list
, GINT_TO_POINTER(cursor_steps
- old_cursor
));
2298 cur_index
= cursor_steps
;
2300 old_cursor
= cursor_steps
;
2302 /* replace remaining %ws% and %newline% which may occur after the last %cursor% */
2303 utils_string_replace_all(pattern
, "%newline%", editor_get_eol_char(editor
));
2304 utils_string_replace_all(pattern
, "%ws%", whitespace
);
2306 /* We put the cursor positions for the most recent
2307 * parsed snippet first, followed by any remaining positions */
2313 foreach_list(node
, temp_list
)
2314 g_queue_push_nth(snippet_offsets
, node
->data
, i
++);
2316 /* limit length of queue */
2317 while (g_queue_get_length(snippet_offsets
) > 20)
2318 g_queue_pop_tail(snippet_offsets
);
2320 g_list_free(temp_list
);
2323 cur_index
= pattern
->len
;
2325 /* finally insert the text and set the cursor */
2326 editor_insert_text_block(editor
, pattern
->str
, pos
, cur_index
, -1, FALSE
);
2327 sci_scroll_caret(sci
);
2330 g_string_free(pattern
, TRUE
);
2336 static gboolean
at_eol(ScintillaObject
*sci
, gint pos
)
2338 gint line
= sci_get_line_from_position(sci
, pos
);
2341 /* skip any trailing spaces */
2344 c
= sci_get_char_at(sci
, pos
);
2345 if (c
== ' ' || c
== '\t')
2351 return (pos
== sci_get_line_end_position(sci
, line
));
2355 gboolean
editor_complete_snippet(GeanyEditor
*editor
, gint pos
)
2357 gboolean result
= FALSE
;
2361 ScintillaObject
*sci
;
2363 g_return_val_if_fail(editor
!= NULL
, FALSE
);
2366 if (sci_has_selection(sci
))
2368 /* return if we are editing an existing line (chars on right of cursor) */
2369 if (keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2370 GEANY_KEYS_EDITOR_COMPLETESNIPPET
)->key
== GDK_space
&&
2371 ! editor_prefs
.complete_snippets_whilst_editing
&& ! at_eol(sci
, pos
))
2374 lexer
= sci_get_lexer(sci
);
2375 style
= sci_get_style_at(sci
, pos
- 2);
2377 wc
= snippets_find_completion_by_name("Special", "wordchars");
2378 word
= editor_read_word_stem(editor
, pos
, wc
);
2380 /* prevent completion of "for " */
2382 ! isspace(sci_get_char_at(sci
, pos
- 1))) /* pos points to the line end char so use pos -1 */
2384 sci_start_undo_action(sci
); /* needed because we insert a space separately from construct */
2385 result
= snippets_complete_constructs(editor
, pos
, word
);
2386 sci_end_undo_action(sci
);
2388 sci_cancel(sci
); /* cancel any autocompletion list, etc */
2396 void editor_show_macro_list(GeanyEditor
*editor
)
2400 if (editor
== NULL
|| editor
->document
->file_type
== NULL
)
2403 words
= symbols_get_macro_list(editor
->document
->file_type
->lang
);
2407 SSM(editor
->sci
, SCI_USERLISTSHOW
, 1, (sptr_t
) words
->str
);
2408 g_string_free(words
, TRUE
);
2412 static void insert_closing_tag(GeanyEditor
*editor
, gint pos
, gchar ch
, const gchar
*tag_name
)
2414 ScintillaObject
*sci
= editor
->sci
;
2415 gchar
*to_insert
= NULL
;
2419 const gchar
*gt
= ">";
2420 /* if there is already a '>' behind the cursor, don't add it */
2421 if (sci_get_char_at(sci
, pos
) == '>')
2424 to_insert
= g_strconcat(tag_name
, gt
, NULL
);
2427 to_insert
= g_strconcat("</", tag_name
, ">", NULL
);
2429 sci_start_undo_action(sci
);
2430 sci_replace_sel(sci
, to_insert
);
2433 sci_set_selection(sci
, pos
, pos
);
2434 if (utils_str_equal(tag_name
, "table"))
2435 auto_table(editor
, pos
);
2437 sci_end_undo_action(sci
);
2443 * (stolen from anjuta and heavily modified)
2444 * This routine will auto complete XML or HTML tags that are still open by closing them
2445 * @param ch The character we are dealing with, currently only works with the '>' character
2446 * @return True if handled, false otherwise
2448 static gboolean
handle_xml(GeanyEditor
*editor
, gint pos
, gchar ch
)
2450 ScintillaObject
*sci
= editor
->sci
;
2451 gint lexer
= sci_get_lexer(sci
);
2453 gchar
*str_found
, sel
[512];
2454 gboolean result
= FALSE
;
2456 /* If the user has turned us off, quit now.
2457 * This may make sense only in certain languages */
2458 if (! editor_prefs
.auto_close_xml_tags
|| (lexer
!= SCLEX_HTML
&& lexer
!= SCLEX_XML
))
2461 /* return if we are inside any embedded script */
2462 style
= sci_get_style_at(sci
, pos
);
2463 if (style
> SCE_H_XCCOMMENT
&& ! is_string_style(lexer
, style
))
2466 /* if ch is /, check for </, else quit */
2467 if (ch
== '/' && sci_get_char_at(sci
, pos
- 2) != '<')
2470 /* Grab the last 512 characters or so */
2471 min
= pos
- (sizeof(sel
) - 1);
2472 if (min
< 0) min
= 0;
2475 return FALSE
; /* Smallest tag is 3 characters e.g. <p> */
2477 sci_get_text_range(sci
, min
, pos
, sel
);
2478 sel
[sizeof(sel
) - 1] = '\0';
2480 if (ch
== '>' && sel
[pos
- min
- 2] == '/')
2481 /* User typed something like "<br/>" */
2484 str_found
= utils_find_open_xml_tag(sel
, pos
- min
, (ch
== '/'));
2486 /* when found string is something like br, img or another short tag, quit */
2487 if (utils_str_equal(str_found
, "br")
2488 || utils_str_equal(str_found
, "hr")
2489 || utils_str_equal(str_found
, "img")
2490 || utils_str_equal(str_found
, "base")
2491 || utils_str_equal(str_found
, "basefont") /* < or not < */
2492 || utils_str_equal(str_found
, "frame")
2493 || utils_str_equal(str_found
, "input")
2494 || utils_str_equal(str_found
, "link")
2495 || utils_str_equal(str_found
, "area")
2496 || utils_str_equal(str_found
, "meta"))
2500 else if (NZV(str_found
))
2502 insert_closing_tag(editor
, pos
, ch
, str_found
);
2510 /* like sci_get_line_indentation(), but for a string. */
2511 static gsize
count_indent_size(GeanyEditor
*editor
, const gchar
*base_indent
)
2514 gsize tab_size
= sci_get_tab_width(editor
->sci
);
2517 g_return_val_if_fail(base_indent
, 0);
2519 for (ptr
= base_indent
; *ptr
!= 0; ptr
++)
2535 static void auto_table(GeanyEditor
*editor
, gint pos
)
2537 ScintillaObject
*sci
= editor
->sci
;
2540 const gchar
*indent_str
;
2542 if (sci_get_lexer(sci
) != SCLEX_HTML
) return;
2544 read_indent(editor
, pos
);
2545 indent_pos
= sci_get_line_indent_position(sci
, sci_get_line_from_position(sci
, pos
));
2546 if ((pos
- 7) != indent_pos
) /* 7 == strlen("<table>") */
2552 /* find the start of the <table tag */
2554 while (i
<= pos
&& sci_get_char_at(sci
, pos
- i
) != '<') i
++;
2555 /* add all non whitespace before the tag to the indent string */
2556 while ((pos
- i
) != indent_pos
&& x
< sizeof(indent
) - 1)
2564 if (! editor
->auto_indent
)
2569 table
= g_strconcat("\n", indent_str
, "<tr>\n",
2570 indent_str
, indent_str
, "<td> </td>\n",
2571 indent_str
, "</tr>\n",
2573 editor_insert_text_block(editor
, table
, pos
, -1,
2574 count_indent_size(editor
, indent
), TRUE
);
2579 static void real_comment_multiline(GeanyEditor
*editor
, gint line_start
, gint last_line
)
2582 gchar
*str_begin
, *str_end
, *co
, *cc
;
2585 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2587 eol
= editor_get_eol_char(editor
);
2588 co
= editor
->document
->file_type
->comment_open
;
2589 cc
= editor
->document
->file_type
->comment_close
;
2590 str_begin
= g_strdup_printf("%s%s", (co
!= NULL
) ? co
: "", eol
);
2591 str_end
= g_strdup_printf("%s%s", (cc
!= NULL
) ? cc
: "", eol
);
2593 /* insert the comment strings */
2594 sci_insert_text(editor
->sci
, line_start
, str_begin
);
2595 line_len
= sci_get_position_from_line(editor
->sci
, last_line
+ 2);
2596 sci_insert_text(editor
->sci
, line_len
, str_end
);
2603 static void real_uncomment_multiline(GeanyEditor
*editor
)
2605 /* find the beginning of the multi line comment */
2606 gint pos
, line
, len
, x
;
2610 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2611 doc
= editor
->document
;
2613 /* remove comment open chars */
2614 pos
= document_find_text(doc
, doc
->file_type
->comment_open
, 0, TRUE
, FALSE
, NULL
);
2615 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2617 /* check whether the line is empty and can be deleted */
2618 line
= sci_get_line_from_position(editor
->sci
, pos
);
2619 len
= sci_get_line_length(editor
->sci
, line
);
2620 linebuf
= sci_get_line(editor
->sci
, line
);
2622 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2623 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2626 /* remove comment close chars */
2627 pos
= document_find_text(doc
, doc
->file_type
->comment_close
, 0, FALSE
, FALSE
, NULL
);
2628 SSM(editor
->sci
, SCI_DELETEBACK
, 0, 0);
2630 /* check whether the line is empty and can be deleted */
2631 line
= sci_get_line_from_position(editor
->sci
, pos
);
2632 len
= sci_get_line_length(editor
->sci
, line
);
2633 linebuf
= sci_get_line(editor
->sci
, line
);
2635 while (linebuf
[x
] != '\0' && isspace(linebuf
[x
])) x
++;
2636 if (x
== len
) SSM(editor
->sci
, SCI_LINEDELETE
, 0, 0);
2641 static gint
get_multiline_comment_style(GeanyEditor
*editor
, gint line_start
)
2643 gint lexer
= sci_get_lexer(editor
->sci
);
2646 /* List only those lexers which support multi line comments */
2652 if (is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2653 style_comment
= SCE_HPHP_COMMENT
;
2655 style_comment
= SCE_H_COMMENT
;
2658 case SCLEX_HASKELL
: style_comment
= SCE_HA_COMMENTBLOCK
; break;
2659 case SCLEX_LUA
: style_comment
= SCE_LUA_COMMENT
; break;
2660 case SCLEX_CSS
: style_comment
= SCE_CSS_COMMENT
; break;
2661 case SCLEX_SQL
: style_comment
= SCE_SQL_COMMENT
; break;
2662 case SCLEX_CAML
: style_comment
= SCE_CAML_COMMENT
; break;
2663 case SCLEX_D
: style_comment
= SCE_D_COMMENT
; break;
2664 case SCLEX_PASCAL
: style_comment
= SCE_PAS_COMMENT
; break;
2665 default: style_comment
= SCE_C_COMMENT
;
2668 return style_comment
;
2672 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise
2673 * returns the amount of uncommented single comment lines, in case of multi line uncomment
2674 * it returns just 1 */
2675 gint
editor_do_uncomment(GeanyEditor
*editor
, gint line
, gboolean toggle
)
2677 gint first_line
, last_line
;
2678 gint x
, i
, line_start
, line_len
;
2679 gint sel_start
, sel_end
;
2682 gchar sel
[256], *co
, *cc
;
2683 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2686 g_return_val_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
, 0);
2689 { /* use selection or current line */
2690 sel_start
= sci_get_selection_start(editor
->sci
);
2691 sel_end
= sci_get_selection_end(editor
->sci
);
2693 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
2694 /* Find the last line with chars selected (not EOL char) */
2695 last_line
= sci_get_line_from_position(editor
->sci
,
2696 sel_end
- editor_get_eol_char_len(editor
));
2697 last_line
= MAX(first_line
, last_line
);
2701 first_line
= last_line
= line
;
2702 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
2705 ft
= editor
->document
->file_type
;
2707 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
2708 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
2709 if (ft
->id
== GEANY_FILETYPES_PHP
)
2711 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
2712 ft
= filetypes
[GEANY_FILETYPES_XML
];
2715 co
= ft
->comment_open
;
2716 cc
= ft
->comment_close
;
2720 co_len
= strlen(co
);
2724 sci_start_undo_action(editor
->sci
);
2726 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
2730 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2731 line_len
= sci_get_line_length(editor
->sci
, i
);
2734 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
2737 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2738 sel
[buf_len
] = '\0';
2740 while (isspace(sel
[x
])) x
++;
2742 /* to skip blank lines */
2743 if (x
< line_len
&& sel
[x
] != '\0')
2745 /* use single line comment */
2746 if (cc
== NULL
|| strlen(cc
) == 0)
2752 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2753 if (strncmp(sel
+ x
, co
, co_len
) != 0 ||
2754 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) != 0)
2761 if (strncmp(sel
+ x
, co
, co_len
) != 0)
2765 sci_set_selection(editor
->sci
, line_start
+ x
, line_start
+ x
+ co_len
);
2766 sci_replace_sel(editor
->sci
, "");
2769 /* use multi line comment */
2774 /* skip lines which are already comments */
2775 style_comment
= get_multiline_comment_style(editor
, line_start
);
2776 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
2778 real_uncomment_multiline(editor
);
2782 /* break because we are already on the last line */
2788 sci_end_undo_action(editor
->sci
);
2790 /* restore selection if there is one
2791 * but don't touch the selection if caller is editor_do_comment_toggle */
2792 if (! toggle
&& sel_start
< sel_end
)
2796 sci_set_selection_start(editor
->sci
, sel_start
- co_len
);
2797 sci_set_selection_end(editor
->sci
, sel_end
- (count
* co_len
));
2801 gint eol_len
= editor_get_eol_char_len(editor
);
2802 sci_set_selection_start(editor
->sci
, sel_start
- co_len
- eol_len
);
2803 sci_set_selection_end(editor
->sci
, sel_end
- co_len
- eol_len
);
2811 void editor_do_comment_toggle(GeanyEditor
*editor
)
2813 gint first_line
, last_line
;
2814 gint x
, i
, line_start
, line_len
, first_line_start
;
2815 gint sel_start
, sel_end
;
2816 gint count_commented
= 0, count_uncommented
= 0;
2817 gchar sel
[256], *co
, *cc
;
2818 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2819 gboolean first_line_was_comment
= FALSE
;
2821 gsize tm_len
= strlen(editor_prefs
.comment_toggle_mark
);
2824 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2826 sel_start
= sci_get_selection_start(editor
->sci
);
2827 sel_end
= sci_get_selection_end(editor
->sci
);
2829 ft
= editor
->document
->file_type
;
2831 first_line
= sci_get_line_from_position(editor
->sci
,
2832 sci_get_selection_start(editor
->sci
));
2833 /* Find the last line with chars selected (not EOL char) */
2834 last_line
= sci_get_line_from_position(editor
->sci
,
2835 sci_get_selection_end(editor
->sci
) - editor_get_eol_char_len(editor
));
2836 last_line
= MAX(first_line
, last_line
);
2838 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
2839 first_line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
2840 if (ft
->id
== GEANY_FILETYPES_PHP
)
2842 if (! is_style_php(sci_get_style_at(editor
->sci
, first_line_start
)))
2843 ft
= filetypes
[GEANY_FILETYPES_XML
];
2846 co
= ft
->comment_open
;
2847 cc
= ft
->comment_close
;
2851 co_len
= strlen(co
);
2855 sci_start_undo_action(editor
->sci
);
2857 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
2861 line_start
= sci_get_position_from_line(editor
->sci
, i
);
2862 line_len
= sci_get_line_length(editor
->sci
, i
);
2865 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
2868 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
2869 sel
[buf_len
] = '\0';
2871 while (isspace(sel
[x
])) x
++;
2873 /* use single line comment */
2874 if (cc
== NULL
|| strlen(cc
) == 0)
2876 gboolean do_continue
= FALSE
;
2879 if (strncmp(sel
+ x
, co
, co_len
) == 0 &&
2880 strncmp(sel
+ x
+ co_len
, editor_prefs
.comment_toggle_mark
, tm_len
) == 0)
2885 if (do_continue
&& i
== first_line
)
2886 first_line_was_comment
= TRUE
;
2890 count_uncommented
+= editor_do_uncomment(editor
, i
, TRUE
);
2894 /* we are still here, so the above lines were not already comments, so comment it */
2895 editor_do_comment(editor
, i
, TRUE
, TRUE
);
2898 /* use multi line comment */
2903 /* skip lines which are already comments */
2904 style_comment
= get_multiline_comment_style(editor
, line_start
);
2905 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
2907 real_uncomment_multiline(editor
);
2908 count_uncommented
++;
2912 real_comment_multiline(editor
, line_start
, last_line
);
2916 /* break because we are already on the last line */
2922 sci_end_undo_action(editor
->sci
);
2926 /* restore selection if there is one */
2927 if (sel_start
< sel_end
)
2931 gint a
= (first_line_was_comment
) ? - co_len
: co_len
;
2933 /* don't modify sel_start when the selection starts within indentation */
2934 read_indent(editor
, sel_start
);
2935 if ((sel_start
- first_line_start
) <= (gint
) strlen(indent
))
2938 sci_set_selection_start(editor
->sci
, sel_start
+ a
);
2939 sci_set_selection_end(editor
->sci
, sel_end
+
2940 (count_commented
* co_len
) - (count_uncommented
* co_len
));
2944 gint eol_len
= editor_get_eol_char_len(editor
);
2945 if (count_uncommented
> 0)
2947 sci_set_selection_start(editor
->sci
, sel_start
- co_len
+ eol_len
);
2948 sci_set_selection_end(editor
->sci
, sel_end
- co_len
+ eol_len
);
2950 else if (count_commented
> 0)
2952 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
- eol_len
);
2953 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
- eol_len
);
2957 else if (count_uncommented
> 0)
2959 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
2960 sci_set_current_position(editor
->sci
, sel_start
- co_len
+ eol_len
, TRUE
);
2962 else if (count_commented
> 0)
2964 gint eol_len
= single_line
? 0: editor_get_eol_char_len(editor
);
2965 sci_set_current_position(editor
->sci
, sel_start
+ co_len
- eol_len
, TRUE
);
2970 /* set toggle to TRUE if the caller is the toggle function, FALSE otherwise */
2971 void editor_do_comment(GeanyEditor
*editor
, gint line
, gboolean allow_empty_lines
, gboolean toggle
)
2973 gint first_line
, last_line
;
2974 gint x
, i
, line_start
, line_len
;
2975 gint sel_start
, sel_end
, co_len
;
2976 gchar sel
[256], *co
, *cc
;
2977 gboolean break_loop
= FALSE
, single_line
= FALSE
;
2980 g_return_if_fail(editor
!= NULL
&& editor
->document
->file_type
!= NULL
);
2983 { /* use selection or current line */
2984 sel_start
= sci_get_selection_start(editor
->sci
);
2985 sel_end
= sci_get_selection_end(editor
->sci
);
2987 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
2988 /* Find the last line with chars selected (not EOL char) */
2989 last_line
= sci_get_line_from_position(editor
->sci
,
2990 sel_end
- editor_get_eol_char_len(editor
));
2991 last_line
= MAX(first_line
, last_line
);
2995 first_line
= last_line
= line
;
2996 sel_start
= sel_end
= sci_get_position_from_line(editor
->sci
, line
);
2999 ft
= editor
->document
->file_type
;
3001 /* detection of HTML vs PHP code, if non-PHP set filetype to XML */
3002 line_start
= sci_get_position_from_line(editor
->sci
, first_line
);
3003 if (ft
->id
== GEANY_FILETYPES_PHP
)
3005 if (! is_style_php(sci_get_style_at(editor
->sci
, line_start
)))
3006 ft
= filetypes
[GEANY_FILETYPES_XML
];
3009 co
= ft
->comment_open
;
3010 cc
= ft
->comment_close
;
3014 co_len
= strlen(co
);
3018 sci_start_undo_action(editor
->sci
);
3020 for (i
= first_line
; (i
<= last_line
) && (! break_loop
); i
++)
3024 line_start
= sci_get_position_from_line(editor
->sci
, i
);
3025 line_len
= sci_get_line_length(editor
->sci
, i
);
3028 buf_len
= MIN((gint
)sizeof(sel
) - 1, line_len
- 1);
3031 sci_get_text_range(editor
->sci
, line_start
, line_start
+ buf_len
, sel
);
3032 sel
[buf_len
] = '\0';
3034 while (isspace(sel
[x
])) x
++;
3036 /* to skip blank lines */
3037 if (allow_empty_lines
|| (x
< line_len
&& sel
[x
] != '\0'))
3039 /* use single line comment */
3040 if (cc
== NULL
|| strlen(cc
) == 0)
3042 gint start
= line_start
;
3045 if (ft
->comment_use_indent
)
3046 start
= line_start
+ x
;
3050 gchar
*text
= g_strconcat(co
, editor_prefs
.comment_toggle_mark
, NULL
);
3051 sci_insert_text(editor
->sci
, start
, text
);
3055 sci_insert_text(editor
->sci
, start
, co
);
3057 /* use multi line comment */
3062 /* skip lines which are already comments */
3063 style_comment
= get_multiline_comment_style(editor
, line_start
);
3064 if (sci_get_style_at(editor
->sci
, line_start
+ x
) == style_comment
)
3067 real_comment_multiline(editor
, line_start
, last_line
);
3069 /* break because we are already on the last line */
3075 sci_end_undo_action(editor
->sci
);
3077 /* restore selection if there is one
3078 * but don't touch the selection if caller is editor_do_comment_toggle */
3079 if (! toggle
&& sel_start
< sel_end
)
3083 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
);
3084 sci_set_selection_end(editor
->sci
, sel_end
+ ((i
- first_line
) * co_len
));
3088 gint eol_len
= editor_get_eol_char_len(editor
);
3089 sci_set_selection_start(editor
->sci
, sel_start
+ co_len
+ eol_len
);
3090 sci_set_selection_end(editor
->sci
, sel_end
+ co_len
+ eol_len
);
3096 static gboolean brace_timeout_active
= FALSE
;
3098 static gboolean
delay_match_brace(G_GNUC_UNUSED gpointer user_data
)
3100 GeanyDocument
*doc
= document_get_current();
3101 GeanyEditor
*editor
;
3102 gint brace_pos
= GPOINTER_TO_INT(user_data
);
3103 gint end_pos
, cur_pos
;
3105 brace_timeout_active
= FALSE
;
3109 editor
= doc
->editor
;
3110 cur_pos
= sci_get_current_position(editor
->sci
) - 1;
3112 if (cur_pos
!= brace_pos
)
3115 if (cur_pos
!= brace_pos
)
3117 /* we have moved past the original brace_pos, but after the timeout
3118 * we may now be on a new brace, so check again */
3119 editor_highlight_braces(editor
, cur_pos
);
3123 if (!utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3125 editor_highlight_braces(editor
, cur_pos
);
3128 end_pos
= sci_find_matching_brace(editor
->sci
, brace_pos
);
3132 gint col
= MIN(sci_get_col_from_position(editor
->sci
, brace_pos
),
3133 sci_get_col_from_position(editor
->sci
, end_pos
));
3134 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, col
, 0);
3135 SSM(editor
->sci
, SCI_BRACEHIGHLIGHT
, brace_pos
, end_pos
);
3139 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3140 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, brace_pos
, 0);
3146 static void editor_highlight_braces(GeanyEditor
*editor
, gint cur_pos
)
3148 gint brace_pos
= cur_pos
- 1;
3150 SSM(editor
->sci
, SCI_SETHIGHLIGHTGUIDE
, 0, 0);
3151 SSM(editor
->sci
, SCI_BRACEBADLIGHT
, (uptr_t
)-1, 0);
3153 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3156 if (! utils_isbrace(sci_get_char_at(editor
->sci
, brace_pos
), editor_prefs
.brace_match_ltgt
))
3161 if (!brace_timeout_active
)
3163 brace_timeout_active
= TRUE
;
3164 /* delaying matching makes scrolling faster e.g. holding down arrow keys */
3165 g_timeout_add(100, delay_match_brace
, GINT_TO_POINTER(brace_pos
));
3170 static gboolean
in_block_comment(gint lexer
, gint style
)
3175 return (style
== SCE_C_COMMENT
||
3176 style
== SCE_C_COMMENTDOC
);
3179 return (style
== SCE_PAS_COMMENT
||
3180 style
== SCE_PAS_COMMENT2
);
3183 return (style
== SCE_D_COMMENT
||
3184 style
== SCE_D_COMMENTDOC
||
3185 style
== SCE_D_COMMENTNESTED
);
3188 return (style
== SCE_HPHP_COMMENT
);
3191 return (style
== SCE_CSS_COMMENT
);
3199 static gboolean
is_comment_char(gchar c
, gint lexer
)
3201 if ((c
== '*' || c
== '+') && lexer
== SCLEX_D
)
3211 static void auto_multiline(GeanyEditor
*editor
, gint cur_line
)
3213 ScintillaObject
*sci
= editor
->sci
;
3214 gint indent_pos
, style
;
3215 gint lexer
= sci_get_lexer(sci
);
3217 /* Use the start of the line enter was pressed on, to avoid any doc keyword styles */
3218 indent_pos
= sci_get_line_indent_position(sci
, cur_line
- 1);
3219 style
= sci_get_style_at(sci
, indent_pos
);
3220 if (!in_block_comment(lexer
, style
))
3223 /* Check whether the comment block continues on this line */
3224 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3225 if (sci_get_style_at(sci
, indent_pos
) == style
)
3227 gchar
*previous_line
= sci_get_line(sci
, cur_line
- 1);
3228 /* the type of comment, '*' (C/C++/Java), '+' and the others (D) */
3229 gchar
*continuation
= "*";
3230 gchar
*whitespace
= ""; /* to hold whitespace if needed */
3232 gint len
= strlen(previous_line
);
3235 /* find and stop at end of multi line comment */
3237 while (i
>= 0 && isspace(previous_line
[i
])) i
--;
3238 if (i
>= 1 && is_comment_char(previous_line
[i
- 1], lexer
) && previous_line
[i
] == '/')
3240 gint indent_len
, indent_width
;
3242 indent_pos
= sci_get_line_indent_position(sci
, cur_line
);
3243 indent_len
= sci_get_col_from_position(sci
, indent_pos
);
3244 indent_width
= editor_get_indent_prefs(editor
)->width
;
3246 /* if there is one too many spaces, delete the last space,
3247 * to return to the indent used before the multiline comment was started. */
3248 if (indent_len
% indent_width
== 1)
3249 SSM(sci
, SCI_DELETEBACKNOTLINE
, 0, 0); /* remove whitespace indent */
3250 g_free(previous_line
);
3253 /* check whether we are on the second line of multi line comment */
3255 while (i
< len
&& isspace(previous_line
[i
])) i
++; /* get to start of the line */
3258 previous_line
[i
] == '/' && is_comment_char(previous_line
[i
+ 1], lexer
))
3259 { /* we are on the second line of a multi line comment, so we have to insert white space */
3263 if (G_UNLIKELY(style
== SCE_D_COMMENTNESTED
))
3264 continuation
= "+"; /* for nested comments in D */
3266 result
= g_strconcat(whitespace
, continuation
, " ", NULL
);
3267 sci_add_text(sci
, result
);
3270 g_free(previous_line
);
3275 /* Checks whether the given style is a string for the given lexer.
3276 * It doesn't handle LEX_HTML, this should be done by the caller.
3277 * Returns true if the style is a string, FALSE otherwise.
3279 * Don't forget STRINGEOL, to prevent completion whilst typing a string with no closing char.
3281 static gboolean
is_string_style(gint lexer
, gint style
)
3286 return (style
== SCE_C_CHARACTER
||
3287 style
== SCE_C_STRING
||
3288 style
== SCE_C_STRINGEOL
);
3291 return (style
== SCE_PAS_CHARACTER
||
3292 style
== SCE_PAS_STRING
||
3293 style
== SCE_PAS_STRINGEOL
);
3296 return (style
== SCE_D_STRING
||
3297 style
== SCE_D_STRINGEOL
||
3298 style
== SCE_D_CHARACTER
||
3299 style
== SCE_D_STRINGB
||
3300 style
== SCE_D_STRINGR
);
3303 return (style
== SCE_P_STRING
||
3304 style
== SCE_P_TRIPLE
||
3305 style
== SCE_P_TRIPLEDOUBLE
||
3306 style
== SCE_P_CHARACTER
||
3307 style
== SCE_P_STRINGEOL
);
3311 return (style
== SCE_F_STRING1
||
3312 style
== SCE_F_STRING2
||
3313 style
== SCE_F_STRINGEOL
);
3316 return (/*style == SCE_PL_STRING ||*/ /* may want variable autocompletion "$(foo)" */
3317 style
== SCE_PL_CHARACTER
||
3318 style
== SCE_PL_HERE_DELIM
||
3319 style
== SCE_PL_HERE_Q
||
3320 style
== SCE_PL_HERE_QQ
||
3321 style
== SCE_PL_HERE_QX
||
3322 style
== SCE_PL_POD
||
3323 style
== SCE_PL_STRING_Q
||
3324 style
== SCE_PL_STRING_QQ
||
3325 style
== SCE_PL_STRING_QX
||
3326 style
== SCE_PL_STRING_QR
||
3327 style
== SCE_PL_STRING_QW
||
3328 style
== SCE_PL_POD_VERB
);
3331 return (style
== SCE_R_STRING
);
3334 return (style
== SCE_RB_CHARACTER
||
3335 style
== SCE_RB_STRING
||
3336 style
== SCE_RB_HERE_DELIM
||
3337 style
== SCE_RB_HERE_Q
||
3338 style
== SCE_RB_HERE_QQ
||
3339 style
== SCE_RB_HERE_QX
||
3340 style
== SCE_RB_POD
);
3343 return (style
== SCE_SH_STRING
);
3346 return (style
== SCE_SQL_STRING
);
3349 return (style
== SCE_TCL_IN_QUOTE
);
3352 return (style
== SCE_LUA_LITERALSTRING
||
3353 style
== SCE_LUA_CHARACTER
||
3354 style
== SCE_LUA_STRINGEOL
||
3355 style
== SCE_LUA_STRING
);
3358 return (style
== SCE_HA_CHARACTER
||
3359 style
== SCE_HA_STRING
);
3361 case SCLEX_FREEBASIC
:
3362 return (style
== SCE_B_STRING
||
3363 style
== SCE_B_STRINGEOL
);
3366 return (style
== SCE_MATLAB_STRING
||
3367 style
== SCE_MATLAB_DOUBLEQUOTESTRING
);
3371 style
== SCE_HBA_STRING
||
3372 style
== SCE_HBA_STRINGEOL
||
3373 style
== SCE_HB_STRING
||
3374 style
== SCE_HB_STRINGEOL
||
3375 style
== SCE_H_CDATA
||
3376 style
== SCE_H_DOUBLESTRING
||
3377 style
== SCE_HJA_DOUBLESTRING
||
3378 style
== SCE_HJA_SINGLESTRING
||
3379 style
== SCE_HJA_STRINGEOL
||
3380 style
== SCE_HJ_DOUBLESTRING
||
3381 style
== SCE_HJ_SINGLESTRING
||
3382 style
== SCE_HJ_STRINGEOL
||
3383 style
== SCE_HPA_CHARACTER
||
3384 style
== SCE_HPA_STRING
||
3385 style
== SCE_HPA_TRIPLE
||
3386 style
== SCE_HPA_TRIPLEDOUBLE
||
3387 style
== SCE_HP_CHARACTER
||
3388 style
== SCE_HPHP_HSTRING
||
3389 style
== SCE_HPHP_HSTRING
|| /* HSTRING is a heredoc */
3390 style
== SCE_HPHP_HSTRING_VARIABLE
||
3391 style
== SCE_HPHP_SIMPLESTRING
||
3392 style
== SCE_HPHP_SIMPLESTRING
||
3393 style
== SCE_HP_STRING
||
3394 style
== SCE_HP_TRIPLE
||
3395 style
== SCE_HP_TRIPLEDOUBLE
||
3396 style
== SCE_H_SGML_DOUBLESTRING
||
3397 style
== SCE_H_SGML_SIMPLESTRING
||
3398 style
== SCE_H_SINGLESTRING
);
3401 return (style
== SCE_CMAKE_STRINGDQ
||
3402 style
== SCE_CMAKE_STRINGLQ
||
3403 style
== SCE_CMAKE_STRINGRQ
||
3404 style
== SCE_CMAKE_STRINGVAR
);
3407 return (style
== SCE_NSIS_STRINGDQ
||
3408 style
== SCE_NSIS_STRINGLQ
||
3409 style
== SCE_NSIS_STRINGRQ
||
3410 style
== SCE_NSIS_STRINGVAR
);
3413 return (style
== SCE_ADA_CHARACTER
||
3414 style
== SCE_ADA_STRING
||
3415 style
== SCE_ADA_CHARACTEREOL
||
3416 style
== SCE_ADA_STRINGEOL
);
3422 /* Checks whether the given style is a comment for the given lexer.
3423 * It doesn't handle LEX_HTML, this should be done by the caller.
3424 * Returns true if the style is a comment, FALSE otherwise.
3426 static gboolean
is_comment_style(gint lexer
, gint style
)
3431 return (style
== SCE_C_COMMENT
||
3432 style
== SCE_C_COMMENTLINE
||
3433 style
== SCE_C_COMMENTDOC
||
3434 style
== SCE_C_COMMENTLINEDOC
||
3435 style
== SCE_C_COMMENTDOCKEYWORD
||
3436 style
== SCE_C_COMMENTDOCKEYWORDERROR
);
3439 return (style
== SCE_PAS_COMMENT
||
3440 style
== SCE_PAS_COMMENT2
||
3441 style
== SCE_PAS_COMMENTLINE
);
3444 return (style
== SCE_D_COMMENT
||
3445 style
== SCE_D_COMMENTLINE
||
3446 style
== SCE_D_COMMENTDOC
||
3447 style
== SCE_D_COMMENTNESTED
||
3448 style
== SCE_D_COMMENTLINEDOC
||
3449 style
== SCE_D_COMMENTDOCKEYWORD
||
3450 style
== SCE_D_COMMENTDOCKEYWORDERROR
);
3453 return (style
== SCE_P_COMMENTLINE
||
3454 style
== SCE_P_COMMENTBLOCK
);
3458 return (style
== SCE_F_COMMENT
);
3461 return (style
== SCE_PL_COMMENTLINE
);
3463 case SCLEX_PROPERTIES
:
3464 return (style
== SCE_PROPS_COMMENT
);
3467 return (style
== SCE_PO_COMMENT
);
3470 return (style
== SCE_L_COMMENT
);
3472 case SCLEX_MAKEFILE
:
3473 return (style
== SCE_MAKE_COMMENT
);
3476 return (style
== SCE_RB_COMMENTLINE
);
3479 return (style
== SCE_SH_COMMENTLINE
);
3482 return (style
== SCE_R_COMMENT
);
3485 return (style
== SCE_SQL_COMMENT
||
3486 style
== SCE_SQL_COMMENTLINE
||
3487 style
== SCE_SQL_COMMENTDOC
);
3490 return (style
== SCE_TCL_COMMENT
||
3491 style
== SCE_TCL_COMMENTLINE
||
3492 style
== SCE_TCL_COMMENT_BOX
||
3493 style
== SCE_TCL_BLOCK_COMMENT
);
3496 return (style
== SCE_MATLAB_COMMENT
);
3499 return (style
== SCE_LUA_COMMENT
||
3500 style
== SCE_LUA_COMMENTLINE
||
3501 style
== SCE_LUA_COMMENTDOC
);
3504 return (style
== SCE_HA_COMMENTLINE
||
3505 style
== SCE_HA_COMMENTBLOCK
||
3506 style
== SCE_HA_COMMENTBLOCK2
||
3507 style
== SCE_HA_COMMENTBLOCK3
);
3509 case SCLEX_FREEBASIC
:
3510 return (style
== SCE_B_COMMENT
);
3513 return (style
== SCE_YAML_COMMENT
);
3517 style
== SCE_HBA_COMMENTLINE
||
3518 style
== SCE_HB_COMMENTLINE
||
3519 style
== SCE_H_COMMENT
||
3520 style
== SCE_HJA_COMMENT
||
3521 style
== SCE_HJA_COMMENTDOC
||
3522 style
== SCE_HJA_COMMENTLINE
||
3523 style
== SCE_HJ_COMMENT
||
3524 style
== SCE_HJ_COMMENTDOC
||
3525 style
== SCE_HJ_COMMENTLINE
||
3526 style
== SCE_HPA_COMMENTLINE
||
3527 style
== SCE_HP_COMMENTLINE
||
3528 style
== SCE_HPHP_COMMENT
||
3529 style
== SCE_HPHP_COMMENTLINE
||
3530 style
== SCE_H_SGML_COMMENT
);
3533 return (style
== SCE_CMAKE_COMMENT
);
3536 return (style
== SCE_NSIS_COMMENT
||
3537 style
== SCE_NSIS_COMMENTBOX
);
3540 return (style
== SCE_ADA_COMMENTLINE
||
3541 style
== SCE_NSIS_COMMENTBOX
);
3547 /* Checks whether the given style is normal code (not string, comment, preprocessor, etc).
3548 * It doesn't handle LEX_HTML, this should be done by the caller.
3550 static gboolean
is_code_style(gint lexer
, gint style
)
3555 if (style
== SCE_C_PREPROCESSOR
)
3559 return !(is_comment_style(lexer
, style
) ||
3560 is_string_style(lexer
, style
));
3565 static gboolean
editor_lexer_is_c_like(gint lexer
)
3580 /* Returns: -1 if lexer doesn't support type keywords */
3581 gint
editor_lexer_get_type_keyword_idx(gint lexer
)
3595 /* inserts a three-line comment at one line above current cursor position */
3596 void editor_insert_multiline_comment(GeanyEditor
*editor
)
3602 gboolean have_multiline_comment
= FALSE
;
3605 g_return_if_fail(editor
!= NULL
&&
3606 editor
->document
->file_type
!= NULL
&& editor
->document
->file_type
->comment_open
!= NULL
);
3608 doc
= editor
->document
;
3610 if (doc
->file_type
->comment_close
!= NULL
&& strlen(doc
->file_type
->comment_close
) > 0)
3611 have_multiline_comment
= TRUE
;
3613 /* insert three lines one line above of the current position */
3614 line
= sci_get_line_from_position(editor
->sci
, editor_info
.click_pos
);
3615 pos
= sci_get_position_from_line(editor
->sci
, line
);
3617 /* use the indent on the current line but only when comment indentation is used
3618 * and we don't have multi line comment characters */
3619 if (editor
->auto_indent
&&
3620 ! have_multiline_comment
&& doc
->file_type
->comment_use_indent
)
3622 read_indent(editor
, editor_info
.click_pos
);
3623 text
= g_strdup_printf("%s\n%s\n%s\n", indent
, indent
, indent
);
3624 text_len
= strlen(text
);
3628 text
= g_strdup("\n\n\n");
3631 sci_insert_text(editor
->sci
, pos
, text
);
3635 /* select the inserted lines for commenting */
3636 sci_set_selection_start(editor
->sci
, pos
);
3637 sci_set_selection_end(editor
->sci
, pos
+ text_len
);
3639 editor_do_comment(editor
, -1, TRUE
, FALSE
);
3641 /* set the current position to the start of the first inserted line */
3642 pos
+= strlen(doc
->file_type
->comment_open
);
3644 /* on multi line comment jump to the next line, otherwise add the length of added indentation */
3645 if (have_multiline_comment
)
3648 pos
+= strlen(indent
);
3650 sci_set_current_position(editor
->sci
, pos
, TRUE
);
3651 /* reset the selection */
3652 sci_set_anchor(editor
->sci
, pos
);
3656 /* Note: If the editor is pending a redraw, set document::scroll_percent instead.
3657 * Scroll the view to make line appear at percent_of_view.
3658 * line can be -1 to use the current position. */
3659 void editor_scroll_to_line(GeanyEditor
*editor
, gint line
, gfloat percent_of_view
)
3661 gint vis1
, los
, delta
;
3664 g_return_if_fail(editor
!= NULL
);
3666 wid
= GTK_WIDGET(editor
->sci
);
3668 if (! wid
->window
|| ! gdk_window_is_viewable(wid
->window
))
3669 return; /* prevent gdk_window_scroll warning */
3672 line
= sci_get_current_line(editor
->sci
);
3674 /* sci 'visible line' != doc line number because of folding and line wrapping */
3675 /* calling SCI_VISIBLEFROMDOCLINE for line is more accurate than calling
3676 * SCI_DOCLINEFROMVISIBLE for vis1. */
3677 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0);
3678 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
3679 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
3680 delta
= (line
- vis1
) - los
* percent_of_view
;
3681 sci_scroll_lines(editor
->sci
, delta
);
3682 sci_scroll_caret(editor
->sci
); /* needed for horizontal scrolling */
3686 /* creates and inserts one tab or whitespace of the amount of the tab width */
3687 void editor_insert_alternative_whitespace(GeanyEditor
*editor
)
3690 GeanyIndentPrefs iprefs
= *editor_get_indent_prefs(editor
);
3692 g_return_if_fail(editor
!= NULL
);
3694 switch (iprefs
.type
)
3696 case GEANY_INDENT_TYPE_TABS
:
3697 iprefs
.type
= GEANY_INDENT_TYPE_SPACES
;
3699 case GEANY_INDENT_TYPE_SPACES
:
3700 case GEANY_INDENT_TYPE_BOTH
: /* most likely we want a tab */
3701 iprefs
.type
= GEANY_INDENT_TYPE_TABS
;
3704 text
= get_whitespace(&iprefs
, iprefs
.width
);
3705 sci_add_text(editor
->sci
, text
);
3710 void editor_select_word(GeanyEditor
*editor
)
3716 g_return_if_fail(editor
!= NULL
);
3718 pos
= SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0);
3719 start
= SSM(editor
->sci
, SCI_WORDSTARTPOSITION
, pos
, TRUE
);
3720 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, TRUE
);
3722 if (start
== end
) /* caret in whitespaces sequence */
3724 /* look forward but reverse the selection direction,
3725 * so the caret end up stay as near as the original position. */
3726 end
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, pos
, FALSE
);
3727 start
= SSM(editor
->sci
, SCI_WORDENDPOSITION
, end
, TRUE
);
3732 sci_set_selection(editor
->sci
, start
, end
);
3736 /* extra_line is for selecting the cursor line (or anchor line) at the bottom of a selection,
3737 * when those lines have no selection (cursor at start of line). */
3738 void editor_select_lines(GeanyEditor
*editor
, gboolean extra_line
)
3740 gint start
, end
, line
;
3742 g_return_if_fail(editor
!= NULL
);
3744 start
= sci_get_selection_start(editor
->sci
);
3745 end
= sci_get_selection_end(editor
->sci
);
3747 /* check if whole lines are already selected */
3748 if (! extra_line
&& start
!= end
&&
3749 sci_get_col_from_position(editor
->sci
, start
) == 0 &&
3750 sci_get_col_from_position(editor
->sci
, end
) == 0)
3753 line
= sci_get_line_from_position(editor
->sci
, start
);
3754 start
= sci_get_position_from_line(editor
->sci
, line
);
3756 line
= sci_get_line_from_position(editor
->sci
, end
);
3757 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
3759 sci_set_selection(editor
->sci
, start
, end
);
3763 /* find the start or end of a paragraph by searching all lines in direction (UP or DOWN)
3764 * starting at the given line and return the found line or return -1 if called on an empty line */
3765 static gint
find_paragraph_stop(GeanyEditor
*editor
, gint line
, gint direction
)
3767 gboolean found_end
= FALSE
;
3769 gchar
*line_buf
, *x
;
3770 ScintillaObject
*sci
= editor
->sci
;
3772 /* first check current line and return -1 if it is empty to skip creating of a selection */
3773 line_buf
= x
= sci_get_line(sci
, line
);
3782 if (direction
== GTK_DIR_UP
)
3791 /* sci_get_line checks for sanity of the given line, sci_get_line always return a string
3792 * containing at least '\0' so no need to check for NULL */
3793 line_buf
= x
= sci_get_line(sci
, line
);
3795 /* check whether after skipping all whitespace we are at end of line and if so, assume
3796 * this line as end of paragraph */
3803 /* called on the first line but there is no previous line so return line 0 */
3812 void editor_select_paragraph(GeanyEditor
*editor
)
3814 gint pos_start
, pos_end
, line_start
, line_found
;
3816 g_return_if_fail(editor
!= NULL
);
3818 line_start
= SSM(editor
->sci
, SCI_LINEFROMPOSITION
,
3819 SSM(editor
->sci
, SCI_GETCURRENTPOS
, 0, 0), 0);
3821 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_UP
);
3822 if (line_found
== -1)
3825 /* find_paragraph_stop returns the emtpy line(previous to the real start of the paragraph),
3826 * so use the next line for selection start */
3830 pos_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3832 line_found
= find_paragraph_stop(editor
, line_start
, GTK_DIR_DOWN
);
3833 pos_end
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, line_found
, 0);
3835 sci_set_selection(editor
->sci
, pos_start
, pos_end
);
3839 /* simple indentation to indent the current line with the same indent as the previous one */
3840 static void smart_line_indentation(GeanyEditor
*editor
, gint first_line
, gint last_line
)
3842 gint i
, sel_start
= 0, sel_end
= 0;
3844 /* get previous line and use it for read_indent to use that line
3845 * (otherwise it would fail on a line only containing "{" in advanced indentation mode) */
3846 read_indent(editor
, sci_get_position_from_line(editor
->sci
, first_line
- 1));
3848 for (i
= first_line
; i
<= last_line
; i
++)
3850 /* skip the first line or if the indentation of the previous and current line are equal */
3852 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
- 1, 0) ==
3853 SSM(editor
->sci
, SCI_GETLINEINDENTATION
, i
, 0))
3856 sel_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3857 sel_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3858 if (sel_start
< sel_end
)
3860 sci_set_selection(editor
->sci
, sel_start
, sel_end
);
3861 sci_replace_sel(editor
->sci
, "");
3863 sci_insert_text(editor
->sci
, sel_start
, indent
);
3868 /* simple indentation to indent the current line with the same indent as the previous one */
3869 void editor_smart_line_indentation(GeanyEditor
*editor
, gint pos
)
3871 gint first_line
, last_line
;
3872 gint first_sel_start
, first_sel_end
;
3873 ScintillaObject
*sci
;
3875 g_return_if_fail(editor
!= NULL
);
3879 first_sel_start
= sci_get_selection_start(sci
);
3880 first_sel_end
= sci_get_selection_end(sci
);
3882 first_line
= sci_get_line_from_position(sci
, first_sel_start
);
3883 /* Find the last line with chars selected (not EOL char) */
3884 last_line
= sci_get_line_from_position(sci
, first_sel_end
- editor_get_eol_char_len(editor
));
3885 last_line
= MAX(first_line
, last_line
);
3888 pos
= first_sel_start
;
3890 sci_start_undo_action(sci
);
3892 smart_line_indentation(editor
, first_line
, last_line
);
3894 /* set cursor position if there was no selection */
3895 if (first_sel_start
== first_sel_end
)
3897 gint indent_pos
= SSM(sci
, SCI_GETLINEINDENTPOSITION
, first_line
, 0);
3899 /* use indent position as user may wish to change indentation afterwards */
3900 sci_set_current_position(sci
, indent_pos
, FALSE
);
3904 /* fully select all the lines affected */
3905 sci_set_selection_start(sci
, sci_get_position_from_line(sci
, first_line
));
3906 sci_set_selection_end(sci
, sci_get_position_from_line(sci
, last_line
+ 1));
3909 sci_end_undo_action(sci
);
3913 /* increase / decrease current line or selection by one space */
3914 void editor_indentation_by_one_space(GeanyEditor
*editor
, gint pos
, gboolean decrease
)
3916 gint i
, first_line
, last_line
, line_start
, indentation_end
, count
= 0;
3917 gint sel_start
, sel_end
, first_line_offset
= 0;
3919 g_return_if_fail(editor
!= NULL
);
3921 sel_start
= sci_get_selection_start(editor
->sci
);
3922 sel_end
= sci_get_selection_end(editor
->sci
);
3924 first_line
= sci_get_line_from_position(editor
->sci
, sel_start
);
3925 /* Find the last line with chars selected (not EOL char) */
3926 last_line
= sci_get_line_from_position(editor
->sci
, sel_end
- editor_get_eol_char_len(editor
));
3927 last_line
= MAX(first_line
, last_line
);
3932 sci_start_undo_action(editor
->sci
);
3934 for (i
= first_line
; i
<= last_line
; i
++)
3936 indentation_end
= SSM(editor
->sci
, SCI_GETLINEINDENTPOSITION
, i
, 0);
3939 line_start
= SSM(editor
->sci
, SCI_POSITIONFROMLINE
, i
, 0);
3940 /* searching backwards for a space to remove */
3941 while (sci_get_char_at(editor
->sci
, indentation_end
) != ' ' && indentation_end
> line_start
)
3944 if (sci_get_char_at(editor
->sci
, indentation_end
) == ' ')
3946 sci_set_selection(editor
->sci
, indentation_end
, indentation_end
+ 1);
3947 sci_replace_sel(editor
->sci
, "");
3949 if (i
== first_line
)
3950 first_line_offset
= -1;
3955 sci_insert_text(editor
->sci
, indentation_end
, " ");
3957 if (i
== first_line
)
3958 first_line_offset
= 1;
3962 /* set cursor position */
3963 if (sel_start
< sel_end
)
3965 gint start
= sel_start
+ first_line_offset
;
3966 if (first_line_offset
< 0)
3967 start
= MAX(sel_start
+ first_line_offset
,
3968 SSM(editor
->sci
, SCI_POSITIONFROMLINE
, first_line
, 0));
3970 sci_set_selection_start(editor
->sci
, start
);
3971 sci_set_selection_end(editor
->sci
, sel_end
+ count
);
3974 sci_set_current_position(editor
->sci
, pos
+ count
, FALSE
);
3976 sci_end_undo_action(editor
->sci
);
3980 void editor_finalize()
3982 scintilla_release_resources();
3986 /* wordchars: NULL or a string containing characters to match a word.
3987 * Returns: the current selection or the current word. */
3988 gchar
*editor_get_default_selection(GeanyEditor
*editor
, gboolean use_current_word
,
3989 const gchar
*wordchars
)
3993 g_return_val_if_fail(editor
!= NULL
, NULL
);
3995 if (sci_get_lines_selected(editor
->sci
) == 1)
3997 gint len
= sci_get_selected_text_length(editor
->sci
);
3999 s
= g_malloc(len
+ 1);
4000 sci_get_selected_text(editor
->sci
, s
);
4002 else if (sci_get_lines_selected(editor
->sci
) == 0 && use_current_word
)
4003 { /* use the word at current cursor position */
4004 gchar word
[GEANY_MAX_WORD_LENGTH
];
4006 editor_find_current_word(editor
, -1, word
, sizeof(word
), wordchars
);
4007 if (word
[0] != '\0')
4014 /* Note: Usually the line should be made visible (not folded) before calling this.
4015 * Returns: TRUE if line is/will be displayed to the user, or FALSE if it is
4016 * outside the *vertical* view.
4017 * Warning: You may need horizontal scrolling to make the cursor visible - so always call
4018 * sci_scroll_caret() when this returns TRUE. */
4019 gboolean
editor_line_in_view(GeanyEditor
*editor
, gint line
)
4023 g_return_val_if_fail(editor
!= NULL
, FALSE
);
4025 /* If line is wrapped the result may occur on another virtual line than the first and may be
4026 * still hidden, so increase the line number to check for the next document line */
4027 if (SSM(editor
->sci
, SCI_WRAPCOUNT
, line
, 0) > 1)
4030 line
= SSM(editor
->sci
, SCI_VISIBLEFROMDOCLINE
, line
, 0); /* convert to visible line number */
4031 vis1
= SSM(editor
->sci
, SCI_GETFIRSTVISIBLELINE
, 0, 0);
4032 los
= SSM(editor
->sci
, SCI_LINESONSCREEN
, 0, 0);
4034 return (line
>= vis1
&& line
< vis1
+ los
);
4038 /* If the current line is outside the current view window, scroll the line
4039 * so it appears at percent_of_view. */
4040 void editor_display_current_line(GeanyEditor
*editor
, gfloat percent_of_view
)
4044 g_return_if_fail(editor
!= NULL
);
4046 line
= sci_get_current_line(editor
->sci
);
4048 /* unfold maybe folded results */
4049 sci_ensure_line_is_visible(editor
->sci
, line
);
4051 /* scroll the line if it's off screen */
4052 if (! editor_line_in_view(editor
, line
))
4053 editor
->scroll_percent
= percent_of_view
;
4055 sci_scroll_caret(editor
->sci
); /* may need horizontal scrolling */
4060 * Deletes all currently set indicators in the @a editor window.
4061 * Error indicators (red squiggly underlines) and usual line markers are removed.
4063 * @param editor The editor to operate on.
4065 void editor_indicator_clear_errors(GeanyEditor
*editor
)
4067 editor_indicator_clear(editor
, GEANY_INDICATOR_ERROR
);
4068 sci_marker_delete_all(editor
->sci
, 0); /* remove the yellow error line marker */
4073 * Deletes all currently set indicators matching @a indic in the @a editor window.
4075 * @param editor The editor to operate on.
4076 * @param indic The indicator number to clear, this is a value of @ref GeanyIndicator.
4080 void editor_indicator_clear(GeanyEditor
*editor
, gint indic
)
4084 g_return_if_fail(editor
!= NULL
);
4086 last_pos
= sci_get_length(editor
->sci
);
4089 sci_indicator_set(editor
->sci
, indic
);
4090 sci_indicator_clear(editor
->sci
, 0, last_pos
);
4096 * Sets an indicator @a indic on @a line.
4097 * Whitespace at the start and the end of the line is not marked.
4099 * @param editor The editor to operate on.
4100 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4101 * @param line The line number which should be marked.
4105 void editor_indicator_set_on_line(GeanyEditor
*editor
, gint indic
, gint line
)
4111 g_return_if_fail(editor
!= NULL
);
4112 g_return_if_fail(line
>= 0);
4114 start
= sci_get_position_from_line(editor
->sci
, line
);
4115 end
= sci_get_position_from_line(editor
->sci
, line
+ 1);
4117 /* skip blank lines */
4118 if ((start
+ 1) == end
||
4120 sci_get_line_length(editor
->sci
, line
) == editor_get_eol_char_len(editor
))
4126 linebuf
= sci_get_line(editor
->sci
, line
);
4128 /* don't set the indicator on whitespace */
4129 while (isspace(linebuf
[i
]))
4131 while (len
> 1 && len
> i
&& isspace(linebuf
[len
- 1]))
4138 editor_indicator_set_on_range(editor
, indic
, start
+ i
, end
);
4143 * Sets an indicator on the range specified by @a start and @a end.
4144 * No error checking or whitespace removal is performed, this should be done by the calling
4145 * function if necessary.
4147 * @param editor The editor to operate on.
4148 * @param indic The indicator number to use, this is a value of @ref GeanyIndicator.
4149 * @param start The starting position for the marker.
4150 * @param end The ending position for the marker.
4154 void editor_indicator_set_on_range(GeanyEditor
*editor
, gint indic
, gint start
, gint end
)
4156 g_return_if_fail(editor
!= NULL
);
4160 sci_indicator_set(editor
->sci
, indic
);
4161 sci_indicator_fill(editor
->sci
, start
, end
- start
);
4165 /* Inserts the given colour (format should be #...), if there is a selection starting with 0x...
4166 * the replacement will also start with 0x... */
4167 void editor_insert_color(GeanyEditor
*editor
, const gchar
*colour
)
4169 g_return_if_fail(editor
!= NULL
);
4171 if (sci_has_selection(editor
->sci
))
4173 gint start
= sci_get_selection_start(editor
->sci
);
4174 const gchar
*replacement
= colour
;
4176 if (sci_get_char_at(editor
->sci
, start
) == '0' &&
4177 sci_get_char_at(editor
->sci
, start
+ 1) == 'x')
4179 sci_set_selection_start(editor
->sci
, start
+ 2);
4180 sci_set_selection_end(editor
->sci
, start
+ 8);
4181 replacement
++; /* skip the leading "0x" */
4183 else if (sci_get_char_at(editor
->sci
, start
- 1) == '#')
4184 { /* double clicking something like #00ffff may only select 00ffff because of wordchars */
4185 replacement
++; /* so skip the '#' to only replace the colour value */
4187 sci_replace_sel(editor
->sci
, replacement
);
4190 sci_add_text(editor
->sci
, colour
);
4195 * Retrieves the localized name (for displaying) of the used end of line characters
4196 * (LF, CR/LF, CR) in the given editor.
4197 * If @a editor is @c NULL, the default end of line characters are used.
4199 * @param editor The editor to operate on, or @c NULL to query the default value.
4200 * @return The name of the end of line characters.
4204 const gchar
*editor_get_eol_char_name(GeanyEditor
*editor
)
4206 gint mode
= file_prefs
.default_eol_character
;
4209 mode
= sci_get_eol_mode(editor
->sci
);
4211 return utils_get_eol_name(mode
);
4216 * Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
4217 * If @a editor is @c NULL, the default end of line characters are used.
4218 * The returned value is 1 for CR and LF and 2 for CR/LF.
4220 * @param editor The editor to operate on, or @c NULL to query the default value.
4221 * @return The length of the end of line characters.
4225 gint
editor_get_eol_char_len(GeanyEditor
*editor
)
4227 gint mode
= file_prefs
.default_eol_character
;
4230 mode
= sci_get_eol_mode(editor
->sci
);
4234 case SC_EOL_CRLF
: return 2; break;
4235 default: return 1; break;
4241 * Retrieves the used end of line characters (LF, CR/LF, CR) in the given editor.
4242 * If @a editor is @c NULL, the default end of line characters are used.
4243 * The returned value is either "\n", "\r\n" or "\r".
4245 * @param editor The editor to operate on, or @c NULL to query the default value.
4246 * @return The end of line characters.
4250 const gchar
*editor_get_eol_char(GeanyEditor
*editor
)
4252 gint mode
= file_prefs
.default_eol_character
;
4255 mode
= sci_get_eol_mode(editor
->sci
);
4259 case SC_EOL_CRLF
: return "\r\n"; break;
4260 case SC_EOL_CR
: return "\r"; break;
4261 default: return "\n"; break;
4266 static void fold_all(GeanyEditor
*editor
, gboolean want_fold
)
4268 gint lines
, first
, i
;
4270 if (editor
== NULL
|| ! editor_prefs
.folding
)
4273 lines
= sci_get_line_count(editor
->sci
);
4274 first
= sci_get_first_visible_line(editor
->sci
);
4276 for (i
= 0; i
< lines
; i
++)
4278 gint level
= sci_get_fold_level(editor
->sci
, i
);
4280 if (level
& SC_FOLDLEVELHEADERFLAG
)
4282 if (sci_get_fold_expanded(editor
->sci
, i
) == want_fold
)
4283 sci_toggle_fold(editor
->sci
, i
);
4286 editor_scroll_to_line(editor
, first
, 0.0F
);
4290 void editor_unfold_all(GeanyEditor
*editor
)
4292 fold_all(editor
, FALSE
);
4296 void editor_fold_all(GeanyEditor
*editor
)
4298 fold_all(editor
, TRUE
);
4302 void editor_replace_tabs(GeanyEditor
*editor
)
4304 gint search_pos
, pos_in_line
, current_tab_true_length
;
4307 struct Sci_TextToFind ttf
;
4309 g_return_if_fail(editor
!= NULL
);
4311 sci_start_undo_action(editor
->sci
);
4312 tab_len
= sci_get_tab_width(editor
->sci
);
4314 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4315 ttf
.lpstrText
= (gchar
*) "\t";
4319 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4320 if (search_pos
== -1)
4323 pos_in_line
= sci_get_col_from_position(editor
->sci
, search_pos
);
4324 current_tab_true_length
= tab_len
- (pos_in_line
% tab_len
);
4325 tab_str
= g_strnfill(current_tab_true_length
, ' ');
4326 sci_set_target_start(editor
->sci
, search_pos
);
4327 sci_set_target_end(editor
->sci
, search_pos
+ 1);
4328 sci_replace_target(editor
->sci
, tab_str
, FALSE
);
4329 /* next search starts after replacement */
4330 ttf
.chrg
.cpMin
= search_pos
+ current_tab_true_length
- 1;
4331 /* update end of range now text has changed */
4332 ttf
.chrg
.cpMax
+= current_tab_true_length
- 1;
4335 sci_end_undo_action(editor
->sci
);
4339 /* Replaces all occurrences all spaces of the length of a given tab_width. */
4340 void editor_replace_spaces(GeanyEditor
*editor
)
4343 static gdouble tab_len_f
= -1.0; /* keep the last used value */
4345 struct Sci_TextToFind ttf
;
4347 g_return_if_fail(editor
!= NULL
);
4349 if (tab_len_f
< 0.0)
4350 tab_len_f
= sci_get_tab_width(editor
->sci
);
4352 if (! dialogs_show_input_numeric(
4353 _("Enter Tab Width"),
4354 _("Enter the amount of spaces which should be replaced by a tab character."),
4355 &tab_len_f
, 1, 100, 1))
4359 tab_len
= (gint
) tab_len_f
;
4361 sci_start_undo_action(editor
->sci
);
4363 ttf
.chrg
.cpMax
= sci_get_length(editor
->sci
);
4364 ttf
.lpstrText
= g_strnfill(tab_len
, ' ');
4368 search_pos
= sci_find_text(editor
->sci
, SCFIND_MATCHCASE
, &ttf
);
4369 if (search_pos
== -1)
4372 sci_set_target_start(editor
->sci
, search_pos
);
4373 sci_set_target_end(editor
->sci
, search_pos
+ tab_len
);
4374 sci_replace_target(editor
->sci
, "\t", FALSE
);
4375 ttf
.chrg
.cpMin
= search_pos
;
4376 /* update end of range now text has changed */
4377 ttf
.chrg
.cpMax
-= tab_len
- 1;
4379 sci_end_undo_action(editor
->sci
);
4380 g_free(ttf
.lpstrText
);
4384 void editor_strip_line_trailing_spaces(GeanyEditor
*editor
, gint line
)
4386 gint line_start
= sci_get_position_from_line(editor
->sci
, line
);
4387 gint line_end
= sci_get_line_end_position(editor
->sci
, line
);
4388 gint i
= line_end
- 1;
4389 gchar ch
= sci_get_char_at(editor
->sci
, i
);
4391 while ((i
>= line_start
) && ((ch
== ' ') || (ch
== '\t')))
4394 ch
= sci_get_char_at(editor
->sci
, i
);
4396 if (i
< (line_end
- 1))
4398 sci_set_target_start(editor
->sci
, i
+ 1);
4399 sci_set_target_end(editor
->sci
, line_end
);
4400 sci_replace_target(editor
->sci
, "", FALSE
);
4405 void editor_strip_trailing_spaces(GeanyEditor
*editor
)
4407 gint max_lines
= sci_get_line_count(editor
->sci
);
4410 sci_start_undo_action(editor
->sci
);
4412 for (line
= 0; line
< max_lines
; line
++)
4414 editor_strip_line_trailing_spaces(editor
, line
);
4416 sci_end_undo_action(editor
->sci
);
4420 void editor_ensure_final_newline(GeanyEditor
*editor
)
4422 gint max_lines
= sci_get_line_count(editor
->sci
);
4423 gboolean append_newline
= (max_lines
== 1);
4424 gint end_document
= sci_get_position_from_line(editor
->sci
, max_lines
);
4428 append_newline
= end_document
> sci_get_position_from_line(editor
->sci
, max_lines
- 1);
4432 const gchar
*eol
= "\n";
4433 switch (sci_get_eol_mode(editor
->sci
))
4442 sci_insert_text(editor
->sci
, end_document
, eol
);
4447 void editor_set_font(GeanyEditor
*editor
, const gchar
*font
)
4451 PangoFontDescription
*pfd
;
4453 g_return_if_fail(editor
);
4455 pfd
= pango_font_description_from_string(font
);
4456 size
= pango_font_description_get_size(pfd
) / PANGO_SCALE
;
4457 font_name
= g_strdup_printf("!%s", pango_font_description_get_family(pfd
));
4458 pango_font_description_free(pfd
);
4460 for (style
= 0; style
<= 127; style
++)
4461 sci_set_font(editor
->sci
, style
, font_name
, size
);
4463 /* line number and braces */
4464 sci_set_font(editor
->sci
, STYLE_LINENUMBER
, font_name
, size
);
4465 sci_set_font(editor
->sci
, STYLE_BRACELIGHT
, font_name
, size
);
4466 sci_set_font(editor
->sci
, STYLE_BRACEBAD
, font_name
, size
);
4469 /* zoom to 100% to prevent confusion */
4470 sci_zoom_off(editor
->sci
);
4474 void editor_set_line_wrapping(GeanyEditor
*editor
, gboolean wrap
)
4476 g_return_if_fail(editor
!= NULL
);
4478 editor
->line_wrapping
= wrap
;
4479 sci_set_lines_wrapped(editor
->sci
, wrap
);
4483 /** Sets the indent type for @a editor.
4484 * @param editor Editor.
4485 * @param type Indent type.
4489 void editor_set_indent_type(GeanyEditor
*editor
, GeanyIndentType type
)
4491 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4492 ScintillaObject
*sci
= editor
->sci
;
4493 gboolean use_tabs
= type
!= GEANY_INDENT_TYPE_SPACES
;
4495 editor
->indent_type
= type
;
4496 sci_set_use_tabs(sci
, use_tabs
);
4498 if (type
== GEANY_INDENT_TYPE_BOTH
)
4499 sci_set_tab_width(sci
, iprefs
->hard_tab_width
);
4501 sci_set_tab_width(sci
, iprefs
->width
);
4502 SSM(sci
, SCI_SETINDENT
, iprefs
->width
, 0);
4504 /* remove indent spaces on backspace, if using any spaces to indent */
4505 SSM(sci
, SCI_SETBACKSPACEUNINDENTS
, type
!= GEANY_INDENT_TYPE_TABS
, 0);
4509 /* Convenience function for editor_goto_pos() to pass in a line number. */
4510 gboolean
editor_goto_line(GeanyEditor
*editor
, gint line
)
4514 g_return_val_if_fail(editor
, FALSE
);
4515 if (line
< 0 || line
>= sci_get_line_count(editor
->sci
))
4518 pos
= sci_get_position_from_line(editor
->sci
, line
);
4519 return editor_goto_pos(editor
, pos
, TRUE
);
4523 /* Move to position @a pos, switching to the document if necessary,
4524 * setting a marker if @a mark is set. */
4525 gboolean
editor_goto_pos(GeanyEditor
*editor
, gint pos
, gboolean mark
)
4529 g_return_val_if_fail(editor
, FALSE
);
4530 if (G_UNLIKELY(pos
< 0))
4535 gint line
= sci_get_line_from_position(editor
->sci
, pos
);
4537 /* mark the tag with the yellow arrow */
4538 sci_marker_delete_all(editor
->sci
, 0);
4539 sci_set_marker_at_line(editor
->sci
, line
, 0);
4542 sci_goto_pos(editor
->sci
, pos
, TRUE
);
4543 editor
->scroll_percent
= 0.25F
;
4545 /* finally switch to the page */
4546 page_num
= gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets
.notebook
), GTK_WIDGET(editor
->sci
));
4547 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), page_num
);
4554 on_editor_scroll_event(GtkWidget
*widget
, GdkEventScroll
*event
, gpointer user_data
)
4556 GeanyEditor
*editor
= user_data
;
4558 /* Handle scroll events if Alt is pressed and scroll whole pages instead of a
4559 * few lines only, maybe this could/should be done in Scintilla directly */
4560 if (event
->state
& GDK_MOD1_MASK
)
4562 sci_send_command(editor
->sci
, (event
->direction
== GDK_SCROLL_DOWN
) ? SCI_PAGEDOWN
: SCI_PAGEUP
);
4565 else if (event
->state
& GDK_SHIFT_MASK
)
4567 gint amount
= (event
->direction
== GDK_SCROLL_DOWN
) ? 8 : -8;
4569 sci_scroll_columns(editor
->sci
, amount
);
4573 return FALSE
; /* let Scintilla handle all other cases */
4577 static gboolean
editor_check_colourise(GeanyEditor
*editor
)
4579 GeanyDocument
*doc
= editor
->document
;
4581 if (!doc
->priv
->colourise_needed
)
4584 doc
->priv
->colourise_needed
= FALSE
;
4585 sci_colourise(editor
->sci
, 0, -1);
4587 /* now that the current document is colourised, fold points are now accurate,
4588 * so force an update of the current function/tag. */
4589 symbols_get_current_function(NULL
, NULL
);
4590 ui_update_statusbar(NULL
, -1);
4596 /* We only want to colourise just before drawing, to save startup time and
4597 * prevent unnecessary recolouring other documents after one is saved.
4598 * Really we want a "draw" signal but there doesn't seem to be one (expose is too late,
4599 * and "show" doesn't work). */
4600 static gboolean
on_editor_focus_in(GtkWidget
*widget
, GdkEventFocus
*event
, gpointer user_data
)
4602 GeanyEditor
*editor
= user_data
;
4604 editor_check_colourise(editor
);
4609 /* This is just to catch any uncolourised documents being drawn that didn't receive focus
4610 * for some reason, maybe it's not necessary but just in case. */
4611 static gboolean
on_editor_expose_event(GtkWidget
*widget
, GdkEventExpose
*event
,
4614 GeanyEditor
*editor
= user_data
;
4616 editor_check_colourise(editor
);
4621 static void setup_sci_keys(ScintillaObject
*sci
)
4623 /* disable some Scintilla keybindings to be able to redefine them cleanly */
4624 sci_clear_cmdkey(sci
, 'A' | (SCMOD_CTRL
<< 16)); /* select all */
4625 sci_clear_cmdkey(sci
, 'D' | (SCMOD_CTRL
<< 16)); /* duplicate */
4626 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16)); /* line transpose */
4627 sci_clear_cmdkey(sci
, 'T' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line copy */
4628 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16)); /* line cut */
4629 sci_clear_cmdkey(sci
, 'L' | (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line delete */
4630 sci_clear_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16)); /* line to end delete */
4631 sci_clear_cmdkey(sci
, '/' | (SCMOD_CTRL
<< 16)); /* Previous word part */
4632 sci_clear_cmdkey(sci
, '\\' | (SCMOD_CTRL
<< 16)); /* Next word part */
4633 sci_clear_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16)); /* scroll line up */
4634 sci_clear_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16)); /* scroll line down */
4635 sci_clear_cmdkey(sci
, SCK_HOME
); /* line start */
4636 sci_clear_cmdkey(sci
, SCK_END
); /* line end */
4637 sci_clear_cmdkey(sci
, SCK_END
| (SCMOD_ALT
<< 16)); /* visual line end */
4639 if (editor_prefs
.use_gtk_word_boundaries
)
4641 /* use GtkEntry-like word boundaries */
4642 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16), SCI_WORDRIGHTEND
);
4643 sci_assign_cmdkey(sci
, SCK_RIGHT
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_WORDRIGHTENDEXTEND
);
4644 sci_assign_cmdkey(sci
, SCK_DELETE
| (SCMOD_CTRL
<< 16), SCI_DELWORDRIGHTEND
);
4646 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_ALT
<< 16), SCI_LINESCROLLUP
);
4647 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_ALT
<< 16), SCI_LINESCROLLDOWN
);
4648 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16), SCI_PARAUP
);
4649 sci_assign_cmdkey(sci
, SCK_UP
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARAUPEXTEND
);
4650 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16), SCI_PARADOWN
);
4651 sci_assign_cmdkey(sci
, SCK_DOWN
| (SCMOD_CTRL
<< 16) | (SCMOD_SHIFT
<< 16), SCI_PARADOWNEXTEND
);
4653 sci_clear_cmdkey(sci
, SCK_BACK
| (SCMOD_ALT
<< 16)); /* clear Alt-Backspace (Undo) */
4657 #include "icons/16x16/classviewer-var.xpm"
4658 #include "icons/16x16/classviewer-method.xpm"
4660 /* Create new editor widget (scintilla).
4661 * @note The @c "sci-notify" signal is connected separately. */
4662 static ScintillaObject
*create_new_sci(GeanyEditor
*editor
)
4664 ScintillaObject
*sci
;
4666 sci
= SCINTILLA(scintilla_new());
4668 gtk_widget_show(GTK_WIDGET(sci
));
4670 sci_set_codepage(sci
, SC_CP_UTF8
);
4671 /*SSM(sci, SCI_SETWRAPSTARTINDENT, 4, 0);*/
4672 /* disable scintilla provided popup menu */
4673 sci_use_popup(sci
, FALSE
);
4675 setup_sci_keys(sci
);
4677 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4678 sci_set_lines_wrapped(sci
, editor_prefs
.line_wrapping
);
4679 sci_set_scrollbar_mode(sci
, editor_prefs
.show_scrollbars
);
4680 sci_set_caret_policy_x(sci
, CARET_JUMPS
| CARET_EVEN
, 0);
4681 /*sci_set_caret_policy_y(sci, CARET_JUMPS | CARET_EVEN, 0);*/
4682 SSM(sci
, SCI_AUTOCSETSEPARATOR
, '\n', 0);
4683 SSM(sci
, SCI_SETSCROLLWIDTHTRACKING
, 1, 0);
4685 /* tag autocompletion images */
4686 SSM(sci
, SCI_REGISTERIMAGE
, 1, (sptr_t
)classviewer_var
);
4687 SSM(sci
, SCI_REGISTERIMAGE
, 2, (sptr_t
)classviewer_method
);
4689 /* necessary for column mode editing, implemented in Scintilla since 2.0 */
4690 SSM(sci
, SCI_SETADDITIONALSELECTIONTYPING
, 1, 0);
4693 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4695 /* only connect signals if this is for the document notebook, not split window */
4696 if (editor
->sci
== NULL
)
4698 g_signal_connect(sci
, "button-press-event", G_CALLBACK(on_editor_button_press_event
), editor
);
4699 g_signal_connect(sci
, "scroll-event", G_CALLBACK(on_editor_scroll_event
), editor
);
4700 g_signal_connect(sci
, "motion-notify-event", G_CALLBACK(on_motion_event
), NULL
);
4701 g_signal_connect(sci
, "focus-in-event", G_CALLBACK(on_editor_focus_in
), editor
);
4702 g_signal_connect(sci
, "expose-event", G_CALLBACK(on_editor_expose_event
), editor
);
4708 /** Creates a new Scintilla @c GtkWidget based on the settings for @a editor.
4709 * @param editor Editor settings.
4710 * @return The new widget.
4714 ScintillaObject
*editor_create_widget(GeanyEditor
*editor
)
4716 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4717 ScintillaObject
*old
, *sci
;
4719 /* temporarily change editor to use the new sci widget */
4721 sci
= create_new_sci(editor
);
4724 editor_set_indent_type(editor
, iprefs
->type
);
4725 editor_set_font(editor
, interface_prefs
.editor_font
);
4726 editor_apply_update_prefs(editor
);
4728 /* if editor already had a widget, restore it */
4735 GeanyEditor
*editor_create(GeanyDocument
*doc
)
4737 const GeanyIndentPrefs
*iprefs
= get_default_indent_prefs();
4738 GeanyEditor
*editor
= g_new0(GeanyEditor
, 1);
4740 editor
->document
= doc
;
4741 doc
->editor
= editor
; /* needed in case some editor functions/callbacks expect it */
4743 editor
->auto_indent
= (iprefs
->auto_indent_mode
!= GEANY_AUTOINDENT_NONE
);
4744 editor
->line_wrapping
= editor_prefs
.line_wrapping
;
4745 editor
->scroll_percent
= -1.0F
;
4746 editor
->line_breaking
= FALSE
;
4748 editor
->sci
= editor_create_widget(editor
);
4753 /* in case we need to free some fields in future */
4754 void editor_destroy(GeanyEditor
*editor
)
4760 static void on_document_save(GObject
*obj
, GeanyDocument
*doc
)
4762 g_return_if_fail(NZV(doc
->real_path
));
4764 if (utils_str_equal(doc
->real_path
,
4765 utils_build_path(app
->configdir
, "snippets.conf", NULL
)))
4767 /* reload snippets */
4768 editor_snippets_free();
4769 editor_snippets_init();
4774 gboolean
editor_complete_word_part(GeanyEditor
*editor
)
4778 g_return_val_if_fail(editor
, FALSE
);
4780 if (!SSM(editor
->sci
, SCI_AUTOCACTIVE
, 0, 0))
4783 entry
= sci_get_string(editor
->sci
, SCI_AUTOCGETCURRENTTEXT
, 0);
4785 /* if no word part, complete normally */
4786 if (!check_partial_completion(editor
, entry
))
4787 SSM(editor
->sci
, SCI_AUTOCCOMPLETE
, 0, 0);
4794 void editor_init(void)
4796 static GeanyIndentPrefs indent_prefs
;
4798 memset(&editor_prefs
, 0, sizeof(GeanyEditorPrefs
));
4799 memset(&indent_prefs
, 0, sizeof(GeanyIndentPrefs
));
4800 editor_prefs
.indentation
= &indent_prefs
;
4802 /* use g_signal_connect_after() to allow plugins connecting to the signal before the default
4803 * handler (on_editor_notify) is called */
4804 g_signal_connect_after(geany_object
, "editor-notify", G_CALLBACK(on_editor_notify
), NULL
);
4806 ui_add_config_file_menu_item(utils_build_path(app
->configdir
, "snippets.conf", NULL
),
4808 g_signal_connect(geany_object
, "document-save", G_CALLBACK(on_document_save
), NULL
);
4812 /* TODO: Should these be user-defined instead of hard-coded? */
4813 void editor_set_indentation_guides(GeanyEditor
*editor
)
4818 g_return_if_fail(editor
!= NULL
);
4820 if (! editor_prefs
.show_indent_guide
)
4822 sci_set_indentation_guides(editor
->sci
, SC_IV_NONE
);
4826 lexer
= sci_get_lexer(editor
->sci
);
4829 /* Lines added/removed are prefixed with +/- characters, so
4830 * those lines will not be shown with any indentation guides.
4831 * It can be distracting that only a few of lines in a diff/patch
4832 * file will show the guides. */
4837 /* These languages use indentation for control blocks; the "look forward" method works
4841 case SCLEX_MAKEFILE
:
4844 case SCLEX_PROPERTIES
:
4845 case SCLEX_FORTRAN
: /* Is this the best option for Fortran? */
4847 mode
= SC_IV_LOOKFORWARD
;
4850 /* C-like (structured) languages benefit from the "look both" method */
4864 case SCLEX_FREEBASIC
:
4867 mode
= SC_IV_LOOKBOTH
;
4875 sci_set_indentation_guides(editor
->sci
, mode
);
4879 /* Apply just the prefs that can change in the Preferences dialog */
4880 void editor_apply_update_prefs(GeanyEditor
*editor
)
4882 ScintillaObject
*sci
;
4884 g_return_if_fail(editor
!= NULL
);
4888 sci_set_mark_long_lines(sci
, editor_get_long_line_type(),
4889 editor_get_long_line_column(), editor_prefs
.long_line_color
);
4891 /* update indent width, tab width */
4892 editor_set_indent_type(editor
, editor
->indent_type
);
4893 sci_set_tab_indents(sci
, editor_prefs
.use_tab_to_indent
);
4895 sci_set_autoc_max_height(sci
, editor_prefs
.symbolcompletion_max_height
);
4896 SSM(sci
, SCI_AUTOCSETDROPRESTOFWORD
, editor_prefs
.completion_drops_rest_of_word
, 0);
4898 editor_set_indentation_guides(editor
);
4900 sci_set_visible_white_spaces(sci
, editor_prefs
.show_white_space
);
4901 sci_set_visible_eols(sci
, editor_prefs
.show_line_endings
);
4902 sci_set_symbol_margin(sci
, editor_prefs
.show_markers_margin
);
4903 sci_set_line_numbers(sci
, editor_prefs
.show_linenumber_margin
, 0);
4905 sci_set_folding_margin_visible(sci
, editor_prefs
.folding
);
4908 SSM(sci
, SCI_SETVIRTUALSPACEOPTIONS
, editor_prefs
.show_virtual_space
, 0);
4910 /* (dis)allow scrolling past end of document */
4911 sci_set_scroll_stop_at_last_line(sci
, editor_prefs
.scroll_stop_at_last_line
);
4915 /* This is for tab-indents, space aligns formatted code. Spaces should be preserved. */
4916 static void change_tab_indentation(GeanyEditor
*editor
, gint line
, gboolean increase
)
4918 ScintillaObject
*sci
= editor
->sci
;
4919 gint pos
= sci_get_position_from_line(sci
, line
);
4923 sci_insert_text(sci
, pos
, "\t");
4927 if (sci_get_char_at(sci
, pos
) == '\t')
4929 sci_set_selection(sci
, pos
, pos
+ 1);
4930 sci_replace_sel(sci
, "");
4932 else /* remove spaces only if no tabs */
4934 gint width
= sci_get_line_indentation(sci
, line
);
4936 width
-= editor_get_indent_prefs(editor
)->width
;
4937 sci_set_line_indentation(sci
, line
, width
);
4943 static void editor_change_line_indent(GeanyEditor
*editor
, gint line
, gboolean increase
)
4945 const GeanyIndentPrefs
*iprefs
= editor_get_indent_prefs(editor
);
4946 ScintillaObject
*sci
= editor
->sci
;
4948 if (iprefs
->type
== GEANY_INDENT_TYPE_TABS
)
4949 change_tab_indentation(editor
, line
, increase
);
4952 gint width
= sci_get_line_indentation(sci
, line
);
4954 width
+= increase
? iprefs
->width
: -iprefs
->width
;
4955 sci_set_line_indentation(sci
, line
, width
);
4960 void editor_indent(GeanyEditor
*editor
, gboolean increase
)
4962 ScintillaObject
*sci
= editor
->sci
;
4964 gint line
, lstart
, lend
;
4966 if (sci_get_lines_selected(sci
) <= 1)
4968 line
= sci_get_current_line(sci
);
4969 editor_change_line_indent(editor
, line
, increase
);
4972 editor_select_lines(editor
, FALSE
);
4973 start
= sci_get_selection_start(sci
);
4974 end
= sci_get_selection_end(sci
);
4975 lstart
= sci_get_line_from_position(sci
, start
);
4976 lend
= sci_get_line_from_position(sci
, end
);
4977 if (end
== sci_get_length(sci
))
4978 lend
++; /* for last line with text on it */
4980 sci_start_undo_action(sci
);
4981 for (line
= lstart
; line
< lend
; line
++)
4983 editor_change_line_indent(editor
, line
, increase
);
4985 sci_end_undo_action(sci
);
4987 /* set cursor/selection */
4990 sci_set_selection_start(sci
, start
);
4991 end
= sci_get_position_from_line(sci
, lend
);
4992 sci_set_selection_end(sci
, end
);
4993 editor_select_lines(editor
, FALSE
);
4997 sci_set_current_line(sci
, lstart
);