Add default keybindings for Go to Tag, remove Transpose default
[geany-mirror.git] / src / keybindings.c
blob730bef5775ce7b2a1b9ebb9a2bf4e31d8af4ec67
1 /*
2 * keybindings.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 /**
23 * @file keybindings.h
24 * Configurable keyboard shortcuts.
25 * - keybindings_send_command() mimics a built-in keybinding action.
26 * - @ref GeanyKeyGroupID lists groups of built-in keybindings.
27 * @see plugin_set_key_group().
28 **/
31 #include "geany.h"
33 #include <gdk/gdkkeysyms.h>
34 #include <string.h>
36 #include "keybindings.h"
37 #include "support.h"
38 #include "utils.h"
39 #include "ui_utils.h"
40 #include "document.h"
41 #include "documentprivate.h"
42 #include "filetypes.h"
43 #include "callbacks.h"
44 #include "prefs.h"
45 #include "msgwindow.h"
46 #include "editor.h"
47 #include "sciwrappers.h"
48 #include "build.h"
49 #include "tools.h"
50 #include "navqueue.h"
51 #include "symbols.h"
52 #include "vte.h"
53 #include "toolbar.h"
54 #include "sidebar.h"
55 #include "geanywraplabel.h"
56 #include "main.h"
57 #include "search.h"
58 #ifdef HAVE_VTE
59 # include "vte.h"
60 #endif
63 GPtrArray *keybinding_groups; /* array of GeanyKeyGroup pointers, in visual order */
65 /* keyfile group name for non-plugin KB groups */
66 static const gchar keybindings_keyfile_group_name[] = "Bindings";
68 /* core keybindings */
69 static GeanyKeyBinding binding_ids[GEANY_KEYS_COUNT];
71 static GtkAccelGroup *kb_accel_group = NULL;
72 static const gboolean swap_alt_tab_order = FALSE;
74 static const gsize MAX_MRU_DOCS = 20;
75 static GQueue *mru_docs = NULL;
76 static guint mru_pos = 0;
78 static gboolean switch_in_progress = FALSE;
79 static GtkWidget *switch_dialog = NULL;
80 static GtkWidget *switch_dialog_label = NULL;
83 /* central keypress event handler, almost all keypress events go to this function */
84 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
85 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
87 static gboolean check_current_word(GeanyDocument *doc, gboolean sci_word);
88 static gboolean read_current_word(GeanyDocument *doc, gboolean sci_word);
89 static gchar *get_current_word_or_sel(GeanyDocument *doc, gboolean sci_word);
91 static gboolean cb_func_file_action(guint key_id);
92 static gboolean cb_func_project_action(guint key_id);
93 static gboolean cb_func_editor_action(guint key_id);
94 static gboolean cb_func_select_action(guint key_id);
95 static gboolean cb_func_format_action(guint key_id);
96 static gboolean cb_func_insert_action(guint key_id);
97 static gboolean cb_func_search_action(guint key_id);
98 static gboolean cb_func_goto_action(guint key_id);
99 static gboolean cb_func_switch_action(guint key_id);
100 static gboolean cb_func_clipboard_action(guint key_id);
101 static gboolean cb_func_build_action(guint key_id);
102 static gboolean cb_func_document_action(guint key_id);
103 static gboolean cb_func_view_action(guint key_id);
105 /* note: new keybindings should normally use per group callbacks */
106 static void cb_func_menu_help(guint key_id);
107 static void cb_func_menu_preferences(guint key_id);
109 static void cb_func_menu_fullscreen(guint key_id);
110 static void cb_func_menu_messagewindow(guint key_id);
112 static void cb_func_menu_opencolorchooser(guint key_id);
114 static void cb_func_switch_tableft(guint key_id);
115 static void cb_func_switch_tabright(guint key_id);
116 static void cb_func_switch_tablastused(guint key_id);
117 static void cb_func_move_tab(guint key_id);
119 static void add_popup_menu_accels(void);
122 /** Looks up a keybinding item.
123 * @param group Group.
124 * @param key_id Keybinding index for the group.
125 * @return The keybinding.
126 * @since 0.19. */
127 GeanyKeyBinding *keybindings_get_item(GeanyKeyGroup *group, gsize key_id)
129 if (group->plugin)
131 g_assert(key_id < group->plugin_key_count);
132 return &group->plugin_keys[key_id];
134 g_assert(key_id < GEANY_KEYS_COUNT);
135 return &binding_ids[key_id];
139 /* This is used to set default keybindings on startup.
140 * Menu accels are set in apply_kb_accel(). */
141 /** Fills a GeanyKeyBinding struct item.
142 * @param group Group.
143 * @param key_id Keybinding index for the group.
144 * @param callback Function to call when activated, or @c NULL to use the group callback.
145 * Usually it's better to use the group callback instead - see plugin_set_key_group().
146 * @param key (Lower case) default key, e.g. @c GDK_j, but usually 0 for unset.
147 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
148 * @param kf_name Key name for the configuration file, such as @c "menu_new".
149 * @param label Label used in the preferences dialog keybindings tab. May contain
150 * underscores - these won't be displayed.
151 * @param menu_item Optional widget to set an accelerator for, or @c NULL.
152 * @return The keybinding - normally this is ignored. */
153 GeanyKeyBinding *keybindings_set_item(GeanyKeyGroup *group, gsize key_id,
154 GeanyKeyCallback callback, guint key, GdkModifierType mod,
155 const gchar *kf_name, const gchar *label, GtkWidget *menu_item)
157 GeanyKeyBinding *kb;
159 g_assert(group->name);
160 kb = keybindings_get_item(group, key_id);
161 g_assert(!kb->name);
162 g_ptr_array_add(group->key_items, kb);
164 if (group->plugin)
166 /* some plugins e.g. GeanyLua need these fields duplicated */
167 setptr(kb->name, g_strdup(kf_name));
168 setptr(kb->label, g_strdup(label));
170 else
172 /* we don't touch these strings unless group->plugin is set, const cast is safe */
173 kb->name = (gchar *)kf_name;
174 kb->label = (gchar *)label;
176 kb->key = key;
177 kb->mods = mod;
178 kb->callback = callback;
179 kb->menu_item = menu_item;
180 kb->id = key_id;
181 return kb;
185 static void add_kb_group(GeanyKeyGroup *group,
186 const gchar *name, const gchar *label, GeanyKeyGroupCallback callback, gboolean plugin)
188 g_ptr_array_add(keybinding_groups, group);
190 group->name = name;
191 group->label = label;
192 group->callback = callback;
193 group->plugin = plugin;
194 group->key_items = g_ptr_array_new();
198 GeanyKeyGroup *keybindings_get_core_group(guint id)
200 static GeanyKeyGroup groups[GEANY_KEY_GROUP_COUNT];
202 g_return_val_if_fail(id < GEANY_KEY_GROUP_COUNT, NULL);
204 return &groups[id];
208 /* Lookup a widget in the main window */
209 #define LW(widget_name) \
210 ui_lookup_widget(main_widgets.window, G_STRINGIFY(widget_name))
212 #define ADD_KB_GROUP(group_id, label, callback) \
213 add_kb_group(keybindings_get_core_group(group_id),\
214 keybindings_keyfile_group_name, label, callback, FALSE)
216 static void init_default_kb(void)
218 GeanyKeyGroup *group;
220 /* visual group order */
221 ADD_KB_GROUP(GEANY_KEY_GROUP_FILE, _("File"), cb_func_file_action);
222 ADD_KB_GROUP(GEANY_KEY_GROUP_EDITOR, _("Editor"), cb_func_editor_action);
223 ADD_KB_GROUP(GEANY_KEY_GROUP_CLIPBOARD, _("Clipboard"), cb_func_clipboard_action);
224 ADD_KB_GROUP(GEANY_KEY_GROUP_SELECT, _("Select"), cb_func_select_action);
225 ADD_KB_GROUP(GEANY_KEY_GROUP_FORMAT, _("Format"), cb_func_format_action);
226 ADD_KB_GROUP(GEANY_KEY_GROUP_INSERT, _("Insert"), cb_func_insert_action);
227 ADD_KB_GROUP(GEANY_KEY_GROUP_SETTINGS, _("Settings"), NULL);
228 ADD_KB_GROUP(GEANY_KEY_GROUP_SEARCH, _("Search"), cb_func_search_action);
229 ADD_KB_GROUP(GEANY_KEY_GROUP_GOTO, _("Go to"), cb_func_goto_action);
230 ADD_KB_GROUP(GEANY_KEY_GROUP_VIEW, _("View"), cb_func_view_action);
231 ADD_KB_GROUP(GEANY_KEY_GROUP_DOCUMENT, _("Document"), cb_func_document_action);
232 ADD_KB_GROUP(GEANY_KEY_GROUP_PROJECT, _("Project"), cb_func_project_action);
233 ADD_KB_GROUP(GEANY_KEY_GROUP_BUILD, _("Build"), cb_func_build_action);
234 ADD_KB_GROUP(GEANY_KEY_GROUP_TOOLS, _("Tools"), NULL);
235 ADD_KB_GROUP(GEANY_KEY_GROUP_HELP, _("Help"), NULL);
236 ADD_KB_GROUP(GEANY_KEY_GROUP_FOCUS, _("Focus"), cb_func_switch_action);
237 ADD_KB_GROUP(GEANY_KEY_GROUP_NOTEBOOK, _("Notebook tab"), NULL);
239 /* Init all fields of keys with default values.
240 * The menu_item field is always the main menu item, popup menu accelerators are
241 * set in add_popup_menu_accels(). */
243 group = keybindings_get_core_group(GEANY_KEY_GROUP_FILE);
245 keybindings_set_item(group, GEANY_KEYS_FILE_NEW, NULL,
246 GDK_n, GDK_CONTROL_MASK, "menu_new", _("New"), NULL);
247 keybindings_set_item(group, GEANY_KEYS_FILE_OPEN, NULL,
248 GDK_o, GDK_CONTROL_MASK, "menu_open", _("Open"), NULL);
249 keybindings_set_item(group, GEANY_KEYS_FILE_OPENSELECTED, NULL,
250 GDK_o, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_open_selected",
251 _("Open selected file"), LW(menu_open_selected_file1));
252 keybindings_set_item(group, GEANY_KEYS_FILE_SAVE, NULL,
253 GDK_s, GDK_CONTROL_MASK, "menu_save", _("Save"), NULL);
254 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEAS, NULL,
255 0, 0, "menu_saveas", _("Save as"), LW(menu_save_as1));
256 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEALL, NULL,
257 GDK_S, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_saveall", _("Save all"),
258 LW(menu_save_all1));
259 keybindings_set_item(group, GEANY_KEYS_FILE_PRINT, NULL,
260 GDK_p, GDK_CONTROL_MASK, "menu_print", _("Print"), LW(print1));
261 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSE, NULL,
262 GDK_w, GDK_CONTROL_MASK, "menu_close", _("Close"), LW(menu_close1));
263 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSEALL, NULL,
264 GDK_w, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_closeall", _("Close all"),
265 LW(menu_close_all1));
266 keybindings_set_item(group, GEANY_KEYS_FILE_RELOAD, NULL,
267 GDK_r, GDK_CONTROL_MASK, "menu_reloadfile", _("Reload file"), LW(menu_reload1));
268 keybindings_set_item(group, GEANY_KEYS_FILE_OPENLASTTAB, NULL,
269 0, 0, "file_openlasttab", _("Re-open last closed tab"), NULL);
271 group = keybindings_get_core_group(GEANY_KEY_GROUP_PROJECT);
273 keybindings_set_item(group, GEANY_KEYS_PROJECT_PROPERTIES, NULL,
274 0, 0, "project_properties", _("Project properties"), LW(project_properties1));
276 group = keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR);
278 keybindings_set_item(group, GEANY_KEYS_EDITOR_UNDO, NULL,
279 GDK_z, GDK_CONTROL_MASK, "menu_undo", _("Undo"), LW(menu_undo2));
280 keybindings_set_item(group, GEANY_KEYS_EDITOR_REDO, NULL,
281 GDK_y, GDK_CONTROL_MASK, "menu_redo", _("Redo"), LW(menu_redo2));
282 keybindings_set_item(group, GEANY_KEYS_EDITOR_DUPLICATELINE, NULL,
283 GDK_d, GDK_CONTROL_MASK, "edit_duplicateline", _("_Duplicate Line or Selection"),
284 LW(duplicate_line_or_selection1));
285 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINE, NULL,
286 GDK_k, GDK_CONTROL_MASK, "edit_deleteline", _("_Delete Current Line(s)"),
287 LW(delete_current_line_s_1));
288 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINETOEND, NULL,
289 GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_deletelinetoend",
290 _("Delete to line end"), NULL);
291 /* transpose may fit better in format group */
292 keybindings_set_item(group, GEANY_KEYS_EDITOR_TRANSPOSELINE, NULL,
293 0, 0, "edit_transposeline", _("_Transpose Current Line"),
294 LW(transpose_current_line1));
295 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLTOLINE, NULL,
296 GDK_l, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_scrolltoline", _("Scroll to current line"), NULL);
297 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEUP, NULL,
298 GDK_Up, GDK_MOD1_MASK, "edit_scrolllineup", _("Scroll up the view by one line"), NULL);
299 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEDOWN, NULL,
300 GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
301 keybindings_set_item(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL,
302 GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
303 keybindings_set_item(group, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR, NULL,
304 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
305 keybindings_set_item(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, NULL,
306 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
307 keybindings_set_item(group, GEANY_KEYS_EDITOR_CONTEXTACTION, NULL,
308 0, 0, "popup_contextaction", _("Context Action"), NULL);
309 keybindings_set_item(group, GEANY_KEYS_EDITOR_AUTOCOMPLETE, NULL,
310 GDK_space, GDK_CONTROL_MASK, "edit_autocomplete", _("Complete word"), NULL);
311 keybindings_set_item(group, GEANY_KEYS_EDITOR_CALLTIP, NULL,
312 GDK_space, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_calltip", _("Show calltip"), NULL);
313 keybindings_set_item(group, GEANY_KEYS_EDITOR_MACROLIST, NULL,
314 GDK_Return, GDK_CONTROL_MASK, "edit_macrolist", _("Show macro list"), NULL);
315 keybindings_set_item(group, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION, NULL,
316 GDK_Tab, 0, "edit_wordpartcompletion", _("Word part completion"), NULL);
317 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEUP, NULL,
318 GDK_Page_Up, GDK_MOD1_MASK, "edit_movelineup", _("Move line(s) up"), NULL);
319 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEDOWN, NULL,
320 GDK_Page_Down, GDK_MOD1_MASK, "edit_movelinedown", _("Move line(s) down"), NULL);
322 group = keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD);
324 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUT, NULL,
325 GDK_x, GDK_CONTROL_MASK, "menu_cut", _("Cut"), NULL);
326 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPY, NULL,
327 GDK_c, GDK_CONTROL_MASK, "menu_copy", _("Copy"), NULL);
328 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_PASTE, NULL,
329 GDK_v, GDK_CONTROL_MASK, "menu_paste", _("Paste"), NULL);
330 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPYLINE, NULL,
331 GDK_c, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_copyline", _("_Copy Current Line(s)"),
332 LW(copy_current_line_s_1));
333 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUTLINE, NULL,
334 GDK_x, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_cutline", _("_Cut Current Line(s)"),
335 LW(cut_current_line_s_1));
337 group = keybindings_get_core_group(GEANY_KEY_GROUP_SELECT);
339 keybindings_set_item(group, GEANY_KEYS_SELECT_ALL, NULL,
340 GDK_a, GDK_CONTROL_MASK, "menu_selectall", _("Select All"), LW(menu_select_all1));
341 keybindings_set_item(group, GEANY_KEYS_SELECT_WORD, NULL,
342 GDK_w, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectword", _("Select current word"), NULL);
343 keybindings_set_item(group, GEANY_KEYS_SELECT_LINE, NULL,
344 GDK_l, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectline", _("_Select Current Line(s)"),
345 LW(select_current_line_s_1));
346 keybindings_set_item(group, GEANY_KEYS_SELECT_PARAGRAPH, NULL,
347 GDK_p, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectparagraph", _("_Select Current Paragraph"),
348 LW(select_current_paragraph1));
349 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTLEFT, NULL,
350 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL);
351 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTRIGHT, NULL,
352 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL);
354 group = keybindings_get_core_group(GEANY_KEY_GROUP_FORMAT);
356 keybindings_set_item(group, GEANY_KEYS_FORMAT_TOGGLECASE, NULL,
357 GDK_u, GDK_CONTROL_MASK | GDK_MOD1_MASK, "edit_togglecase",
358 _("T_oggle Case of Selection"), LW(menu_toggle_case2));
359 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE, NULL,
360 GDK_e, GDK_CONTROL_MASK, "edit_commentlinetoggle", _("Toggle line commentation"),
361 LW(menu_toggle_line_commentation1));
362 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINE, NULL,
363 0, 0, "edit_commentline", _("Comment line(s)"), LW(menu_comment_line1));
364 keybindings_set_item(group, GEANY_KEYS_FORMAT_UNCOMMENTLINE, NULL,
365 0, 0, "edit_uncommentline", _("Uncomment line(s)"), LW(menu_uncomment_line1));
366 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENT, NULL,
367 GDK_i, GDK_CONTROL_MASK, "edit_increaseindent", _("Increase indent"),
368 LW(menu_increase_indent1));
369 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENT, NULL,
370 GDK_u, GDK_CONTROL_MASK, "edit_decreaseindent", _("Decrease indent"),
371 LW(menu_decrease_indent1));
372 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE, NULL,
373 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL);
374 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE, NULL,
375 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL);
376 keybindings_set_item(group, GEANY_KEYS_FORMAT_AUTOINDENT, NULL,
377 0, 0, "edit_autoindent", _("_Smart Line Indent"), LW(smart_line_indent1));
378 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD1, NULL,
379 GDK_1, GDK_CONTROL_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL);
380 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD2, NULL,
381 GDK_2, GDK_CONTROL_MASK, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL);
382 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD3, NULL,
383 GDK_3, GDK_CONTROL_MASK, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL);
384 /* may fit better in editor group */
385 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOVTE, NULL,
386 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), LW(send_selection_to_vte1));
387 keybindings_set_item(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
388 GDK_j, GDK_CONTROL_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
389 LW(reflow_lines_block1));
391 group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
393 keybindings_set_item(group, GEANY_KEYS_INSERT_DATE, NULL,
394 GDK_d, GDK_SHIFT_MASK | GDK_MOD1_MASK, "menu_insert_date", _("Insert date"),
395 LW(insert_date_custom1));
396 keybindings_set_item(group, GEANY_KEYS_INSERT_ALTWHITESPACE, NULL,
397 0, 0, "edit_insertwhitespace", _("_Insert Alternative White Space"),
398 LW(insert_alternative_white_space1));
399 keybindings_set_item(group, GEANY_KEYS_INSERT_LINEBEFORE, NULL,
400 0, 0, "edit_insertlinebefore", _("Insert New Line Before Current"), NULL);
401 keybindings_set_item(group, GEANY_KEYS_INSERT_LINEAFTER, NULL,
402 0, 0, "edit_insertlineafter", _("Insert New Line After Current"), NULL);
404 group = keybindings_get_core_group(GEANY_KEY_GROUP_SETTINGS);
406 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PREFERENCES, cb_func_menu_preferences,
407 GDK_p, GDK_CONTROL_MASK | GDK_MOD1_MASK, "menu_preferences", _("Preferences"),
408 LW(preferences1));
409 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES, cb_func_menu_preferences,
410 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), LW(plugin_preferences1));
412 group = keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH);
414 keybindings_set_item(group, GEANY_KEYS_SEARCH_FIND, NULL,
415 GDK_f, GDK_CONTROL_MASK, "menu_find", _("Find"), LW(find1));
416 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXT, NULL,
417 GDK_g, GDK_CONTROL_MASK, "menu_findnext", _("Find Next"), LW(find_next1));
418 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVIOUS, NULL,
419 GDK_g, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findprevious", _("Find Previous"),
420 LW(find_previous1));
421 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXTSEL, NULL,
422 0, 0, "menu_findnextsel", _("Find Next _Selection"), LW(find_nextsel1));
423 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVSEL, NULL,
424 0, 0, "menu_findprevsel", _("Find Pre_vious Selection"), LW(find_prevsel1));
425 keybindings_set_item(group, GEANY_KEYS_SEARCH_REPLACE, NULL,
426 GDK_h, GDK_CONTROL_MASK, "menu_replace", _("Replace"), LW(replace1));
427 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDINFILES, NULL, GDK_f,
428 GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findinfiles", _("Find in Files"),
429 LW(find_in_files1));
430 keybindings_set_item(group, GEANY_KEYS_SEARCH_NEXTMESSAGE, NULL,
431 0, 0, "menu_nextmessage", _("Next Message"), LW(next_message1));
432 keybindings_set_item(group, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE, NULL,
433 0, 0, "menu_previousmessage", _("Previous Message"), LW(previous_message1));
434 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDUSAGE, NULL,
435 GDK_e, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "popup_findusage",
436 _("Find Usage"), LW(find_usage1));
437 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, NULL,
438 GDK_d, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "popup_finddocumentusage",
439 _("Find Document Usage"), LW(find_document_usage1));
440 keybindings_set_item(group, GEANY_KEYS_SEARCH_MARKALL, NULL,
441 GDK_m, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "find_markall", _("_Mark All"), LW(mark_all1));
443 group = keybindings_get_core_group(GEANY_KEY_GROUP_GOTO);
445 keybindings_set_item(group, GEANY_KEYS_GOTO_BACK, NULL,
446 GDK_Left, GDK_MOD1_MASK, "nav_back", _("Navigate back a location"), NULL);
447 keybindings_set_item(group, GEANY_KEYS_GOTO_FORWARD, NULL,
448 GDK_Right, GDK_MOD1_MASK, "nav_forward", _("Navigate forward a location"), NULL);
449 keybindings_set_item(group, GEANY_KEYS_GOTO_LINE, NULL,
450 GDK_l, GDK_CONTROL_MASK, "menu_gotoline", _("Go to Line"), LW(go_to_line1));
451 keybindings_set_item(group, GEANY_KEYS_GOTO_MATCHINGBRACE, NULL,
452 GDK_b, GDK_CONTROL_MASK, "edit_gotomatchingbrace",
453 _("Go to matching brace"), NULL);
454 keybindings_set_item(group, GEANY_KEYS_GOTO_TOGGLEMARKER, NULL,
455 GDK_m, GDK_CONTROL_MASK, "edit_togglemarker",
456 _("Toggle marker"), NULL);
457 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTMARKER, NULL,
458 GDK_period, GDK_CONTROL_MASK, "edit_gotonextmarker",
459 _("_Go to Next Marker"), LW(go_to_next_marker1));
460 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVIOUSMARKER, NULL,
461 GDK_comma, GDK_CONTROL_MASK, "edit_gotopreviousmarker",
462 _("_Go to Previous Marker"), LW(go_to_previous_marker1));
463 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDEFINITION, NULL,
464 GDK_t, GDK_CONTROL_MASK, "popup_gototagdefinition",
465 _("Go to Tag Definition"), LW(goto_tag_definition1));
466 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDECLARATION, NULL,
467 GDK_t, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "popup_gototagdeclaration",
468 _("Go to Tag Declaration"), LW(goto_tag_declaration1));
469 keybindings_set_item(group, GEANY_KEYS_GOTO_LINESTART, NULL,
470 GDK_Home, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL);
471 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEEND, NULL,
472 GDK_End, 0, "edit_gotolineend", _("Go to End of Line"), NULL);
473 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEENDVISUAL, NULL,
474 GDK_End, GDK_MOD1_MASK, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL);
475 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVWORDPART, NULL,
476 GDK_slash, GDK_CONTROL_MASK, "edit_prevwordstart", _("Go to Previous Word Part"), NULL);
477 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTWORDPART, NULL,
478 GDK_backslash, GDK_CONTROL_MASK, "edit_nextwordstart", _("Go to Next Word Part"), NULL);
480 group = keybindings_get_core_group(GEANY_KEY_GROUP_VIEW);
482 keybindings_set_item(group, GEANY_KEYS_VIEW_TOGGLEALL, NULL,
483 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
484 LW(menu_toggle_all_additional_widgets1));
485 keybindings_set_item(group, GEANY_KEYS_VIEW_FULLSCREEN, cb_func_menu_fullscreen,
486 GDK_F11, 0, "menu_fullscreen", _("Fullscreen"), LW(menu_fullscreen1));
487 keybindings_set_item(group, GEANY_KEYS_VIEW_MESSAGEWINDOW, cb_func_menu_messagewindow,
488 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
489 LW(menu_show_messages_window1));
490 keybindings_set_item(group, GEANY_KEYS_VIEW_SIDEBAR, NULL,
491 0, 0, "toggle_sidebar", _("Toggle Sidebar"), LW(menu_show_sidebar1));
492 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMIN, NULL,
493 GDK_plus, GDK_CONTROL_MASK, "menu_zoomin", _("Zoom In"), LW(menu_zoom_in1));
494 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMOUT, NULL,
495 GDK_minus, GDK_CONTROL_MASK, "menu_zoomout", _("Zoom Out"), LW(menu_zoom_out1));
496 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMRESET, NULL,
497 GDK_0, GDK_CONTROL_MASK, "normal_size", _("Zoom Reset"), LW(normal_size1));
499 group = keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS);
501 keybindings_set_item(group, GEANY_KEYS_FOCUS_EDITOR, NULL,
502 GDK_F2, 0, "switch_editor", _("Switch to Editor"), NULL);
503 keybindings_set_item(group, GEANY_KEYS_FOCUS_SEARCHBAR, NULL,
504 GDK_F7, 0, "switch_search_bar", _("Switch to Search Bar"), NULL);
505 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGE_WINDOW, NULL,
506 0, 0, "switch_message_window", _("Switch to Message Window"), NULL);
507 keybindings_set_item(group, GEANY_KEYS_FOCUS_COMPILER, NULL,
508 0, 0, "switch_compiler", _("Switch to Compiler"), NULL);
509 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGES, NULL,
510 0, 0, "switch_messages", _("Switch to Messages"), NULL);
511 keybindings_set_item(group, GEANY_KEYS_FOCUS_SCRIBBLE, NULL,
512 GDK_F6, 0, "switch_scribble", _("Switch to Scribble"), NULL);
513 keybindings_set_item(group, GEANY_KEYS_FOCUS_VTE, NULL,
514 GDK_F4, 0, "switch_vte", _("Switch to VTE"), NULL);
515 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR, NULL,
516 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL);
517 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST, NULL,
518 0, 0, "switch_sidebar_symbol_list", _("Switch to Sidebar Symbol List"), NULL);
519 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST, NULL,
520 0, 0, "switch_sidebar_doc_list", _("Switch to Sidebar Document List"), NULL);
522 group = keybindings_get_core_group(GEANY_KEY_GROUP_NOTEBOOK);
524 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT, cb_func_switch_tableft,
525 GDK_Page_Up, GDK_CONTROL_MASK, "switch_tableft", _("Switch to left document"), NULL);
526 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
527 GDK_Page_Down, GDK_CONTROL_MASK, "switch_tabright", _("Switch to right document"), NULL);
528 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
529 GDK_Tab, GDK_CONTROL_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
530 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
531 GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "move_tableft",
532 _("Move document left"), NULL);
533 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
534 GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "move_tabright",
535 _("Move document right"), NULL);
536 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABFIRST, cb_func_move_tab,
537 0, 0, "move_tabfirst", _("Move document first"), NULL);
538 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLAST, cb_func_move_tab,
539 0, 0, "move_tablast", _("Move document last"), NULL);
541 group = keybindings_get_core_group(GEANY_KEY_GROUP_DOCUMENT);
543 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEWRAP, NULL,
544 0, 0, "menu_linewrap", _("Toggle Line wrapping"), LW(menu_line_wrapping1));
545 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEBREAK, NULL,
546 0, 0, "menu_linebreak", _("Toggle Line breaking"), LW(line_breaking1));
547 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACETABS, NULL,
548 0, 0, "menu_replacetabs", _("Replace tabs by space"), LW(menu_replace_tabs));
549 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACESPACES, NULL,
550 0, 0, "menu_replacespaces", _("Replace spaces by tabs"), LW(menu_replace_spaces));
551 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_TOGGLEFOLD, NULL,
552 0, 0, "menu_togglefold", _("Toggle current fold"), NULL);
553 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_FOLDALL, NULL,
554 0, 0, "menu_foldall", _("Fold all"), LW(menu_fold_all1));
555 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_UNFOLDALL, NULL,
556 0, 0, "menu_unfoldall", _("Unfold all"), LW(menu_unfold_all1));
557 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_RELOADTAGLIST, NULL,
558 GDK_r, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "reloadtaglist", _("Reload symbol list"), NULL);
559 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS, NULL,
560 0, 0, "remove_markers", _("Remove Markers"), LW(remove_markers1));
561 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS, NULL,
562 0, 0, "remove_error_indicators", _("Remove Error Indicators"), LW(menu_remove_indicators1));
563 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS, NULL,
564 0, 0, "remove_markers_and_indicators", _("Remove Markers and Error Indicators"), NULL);
566 group = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
568 keybindings_set_item(group, GEANY_KEYS_BUILD_COMPILE, NULL,
569 GDK_F8, 0, "build_compile", _("Compile"), NULL);
570 keybindings_set_item(group, GEANY_KEYS_BUILD_LINK, NULL,
571 GDK_F9, 0, "build_link", _("Build"), NULL);
572 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKE, NULL,
573 GDK_F9, GDK_SHIFT_MASK, "build_make", _("Make all"), NULL);
574 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOWNTARGET, NULL,
575 GDK_F9, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "build_makeowntarget",
576 _("Make custom target"), NULL);
577 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOBJECT, NULL,
578 GDK_F8, GDK_SHIFT_MASK, "build_makeobject", _("Make object"), NULL);
579 keybindings_set_item(group, GEANY_KEYS_BUILD_NEXTERROR, NULL,
580 0, 0, "build_nexterror", _("Next error"), NULL);
581 keybindings_set_item(group, GEANY_KEYS_BUILD_PREVIOUSERROR, NULL,
582 0, 0, "build_previouserror", _("Previous error"), NULL);
583 keybindings_set_item(group, GEANY_KEYS_BUILD_RUN, NULL,
584 GDK_F5, 0, "build_run", _("Run"), NULL);
585 keybindings_set_item(group, GEANY_KEYS_BUILD_OPTIONS, NULL,
586 0, 0, "build_options", _("Build options"), NULL);
588 group = keybindings_get_core_group(GEANY_KEY_GROUP_TOOLS);
590 keybindings_set_item(group, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER, cb_func_menu_opencolorchooser,
591 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), LW(menu_choose_color1));
593 group = keybindings_get_core_group(GEANY_KEY_GROUP_HELP);
595 keybindings_set_item(group, GEANY_KEYS_HELP_HELP, cb_func_menu_help,
596 GDK_F1, 0, "menu_help", _("Help"), LW(help1));
600 /* before the tab changes, add the current document to the MRU list */
601 static void on_notebook_switch_page(void)
603 GeanyDocument *old = document_get_current();
605 /* when closing current doc, old is NULL.
606 * Don't add to the mru list when switch dialog is visible. */
607 if (old && !switch_in_progress)
609 g_queue_remove(mru_docs, old);
610 g_queue_push_head(mru_docs, old);
612 if (g_queue_get_length(mru_docs) > MAX_MRU_DOCS)
613 g_queue_pop_tail(mru_docs);
618 /* really this should be just after a document was closed, not idle */
619 static gboolean on_idle_close(gpointer data)
621 GeanyDocument *current;
623 current = document_get_current();
624 if (current && g_queue_peek_head(mru_docs) == current)
625 g_queue_pop_head(mru_docs);
627 return FALSE;
631 static void on_document_close(GObject *obj, GeanyDocument *doc)
633 if (! main_status.quitting)
635 g_queue_remove(mru_docs, doc);
636 g_idle_add(on_idle_close, NULL);
641 void keybindings_init(void)
643 mru_docs = g_queue_new();
644 g_signal_connect(main_widgets.notebook, "switch-page",
645 G_CALLBACK(on_notebook_switch_page), NULL);
646 g_signal_connect(geany_object, "document-close",
647 G_CALLBACK(on_document_close), NULL);
649 memset(binding_ids, 0, sizeof binding_ids);
650 keybinding_groups = g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT);
651 kb_accel_group = gtk_accel_group_new();
653 init_default_kb();
654 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), kb_accel_group);
656 g_signal_connect(main_widgets.window, "key-press-event", G_CALLBACK(on_key_press_event), NULL);
657 /* in case the switch dialog misses an event while drawing the dialog */
658 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
662 typedef void (*KBItemCallback) (GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data);
664 static void keybindings_foreach(KBItemCallback cb, gpointer user_data)
666 gsize g, i;
667 GeanyKeyGroup *group;
668 GeanyKeyBinding *kb;
670 foreach_ptr_array(group, g, keybinding_groups)
672 foreach_ptr_array(kb, i, group->key_items)
673 cb(group, kb, user_data);
678 static void load_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
680 GKeyFile *config = user_data;
681 gchar *val;
682 guint key;
683 GdkModifierType mods;
685 val = g_key_file_get_string(config, group->name, kb->name, NULL);
686 if (val != NULL)
688 gtk_accelerator_parse(val, &key, &mods);
689 kb->key = key;
690 kb->mods = mods;
691 g_free(val);
696 static void load_user_kb(void)
698 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
699 GKeyFile *config = g_key_file_new();
701 /* now load user defined keys */
702 if (g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
704 keybindings_foreach(load_kb, config);
706 g_free(configfile);
707 g_key_file_free(config);
711 static void apply_kb_accel(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
713 if (kb->key != 0 && kb->menu_item)
715 gtk_widget_add_accelerator(kb->menu_item, "activate", kb_accel_group,
716 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
721 void keybindings_load_keyfile(void)
723 load_user_kb();
724 add_popup_menu_accels();
726 /* set menu accels now, after user keybindings have been read */
727 keybindings_foreach(apply_kb_accel, NULL);
731 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id, GtkWidget *menuitem)
733 GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
735 if (kb->key != 0)
736 gtk_widget_add_accelerator(menuitem, "activate", kb_accel_group,
737 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
741 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
742 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
744 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
745 static void add_popup_menu_accels(void)
747 GeanyKeyGroup *group;
749 group = keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR);
750 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO, undo1);
751 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO, redo1);
752 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION, context_action1);
754 group = keybindings_get_core_group(GEANY_KEY_GROUP_SELECT);
755 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL, menu_select_all2);
757 group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
758 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE, insert_date_custom2);
759 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_ALTWHITESPACE, insert_alternative_white_space1);
761 group = keybindings_get_core_group(GEANY_KEY_GROUP_FILE);
762 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED, menu_open_selected_file2);
764 group = keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH);
765 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE, find_usage1);
766 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, find_document_usage1);
768 group = keybindings_get_core_group(GEANY_KEY_GROUP_GOTO);
769 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION, goto_tag_definition1);
771 /* Format and Commands share the menu bar submenus */
772 /* Build menu items are set if the build menus are created */
776 static void set_keyfile_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
778 GKeyFile *config = user_data;
779 gchar *val;
781 val = gtk_accelerator_name(kb->key, kb->mods);
782 g_key_file_set_string(config, group->name, kb->name, val);
783 g_free(val);
787 /* just write the content of the keys array to the config file */
788 void keybindings_write_to_file(void)
790 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
791 gchar *data;
792 GKeyFile *config = g_key_file_new();
794 /* add comment if the file is newly created */
795 if (! g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
797 g_key_file_set_comment(config, NULL, NULL,
798 "Keybindings for Geany\nThe format looks like \"<Control>a\" or \"<Shift><Alt>F1\".\n"
799 "But you can also change the keys in Geany's preferences dialog.", NULL);
802 keybindings_foreach(set_keyfile_kb, config);
804 /* write the file */
805 data = g_key_file_to_data(config, NULL, NULL);
806 utils_write_file(configfile, data);
808 g_free(data);
809 g_free(configfile);
810 g_key_file_free(config);
814 void keybindings_free(void)
816 GeanyKeyGroup *group;
817 gsize g;
819 foreach_ptr_array(group, g, keybinding_groups)
820 keybindings_free_group(group);
822 g_ptr_array_free(keybinding_groups, TRUE);
823 g_queue_free(mru_docs);
827 gchar *keybindings_get_label(GeanyKeyBinding *kb)
829 return utils_str_remove_chars(g_strdup(kb->label), "_");
833 static void fill_shortcut_labels_treeview(GtkWidget *tree)
835 gsize g, i;
836 GeanyKeyBinding *kb;
837 GeanyKeyGroup *group;
838 GtkListStore *store;
839 GtkTreeIter iter;
841 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, PANGO_TYPE_WEIGHT);
843 foreach_ptr_array(group, g, keybinding_groups)
845 if (g > 0)
847 gtk_list_store_append(store, &iter);
848 gtk_list_store_set(store, &iter, -1);
850 gtk_list_store_append(store, &iter);
851 gtk_list_store_set(store, &iter, 0, group->label, 2, PANGO_WEIGHT_BOLD, -1);
853 foreach_ptr_array(kb, i, group->key_items)
855 gchar *shortcut, *label;
857 label = keybindings_get_label(kb);
858 shortcut = gtk_accelerator_get_label(kb->key, kb->mods);
860 gtk_list_store_append(store, &iter);
861 gtk_list_store_set(store, &iter, 0, label, 1, shortcut, 2, PANGO_WEIGHT_NORMAL, -1);
863 g_free(shortcut);
864 g_free(label);
867 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
868 g_object_unref(store);
872 static GtkWidget *create_dialog(void)
874 GtkWidget *dialog, *tree, *label, *swin, *vbox;
875 GtkCellRenderer *text_renderer;
876 GtkTreeViewColumn *column;
878 dialog = gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets.window),
879 GTK_DIALOG_DESTROY_WITH_PARENT,
880 GTK_STOCK_EDIT, GTK_RESPONSE_APPLY,
881 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
882 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
883 gtk_box_set_spacing(GTK_BOX(vbox), 6);
884 gtk_widget_set_name(dialog, "GeanyDialog");
886 gtk_window_set_default_size(GTK_WINDOW(dialog), -1, GEANY_DEFAULT_DIALOG_HEIGHT);
888 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
890 label = gtk_label_new(_("The following keyboard shortcuts are configurable:"));
891 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
893 tree = gtk_tree_view_new();
894 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
895 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
897 text_renderer = gtk_cell_renderer_text_new();
898 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
899 column = gtk_tree_view_column_new_with_attributes(
900 NULL, text_renderer, "text", 0, "weight", 2, NULL);
901 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
903 text_renderer = gtk_cell_renderer_text_new();
904 column = gtk_tree_view_column_new_with_attributes(NULL, text_renderer, "text", 1, NULL);
905 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
907 fill_shortcut_labels_treeview(tree);
909 swin = gtk_scrolled_window_new(NULL, NULL);
910 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_NEVER,
911 GTK_POLICY_AUTOMATIC);
912 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_ETCHED_IN);
913 gtk_container_add(GTK_CONTAINER(swin), tree);
915 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
916 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
918 return dialog;
922 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
923 static GtkWidget *key_dialog = NULL;
925 static void on_dialog_response(GtkWidget *dialog, gint response, gpointer user_data)
927 if (response == GTK_RESPONSE_APPLY)
929 GtkWidget *wid;
931 prefs_show_dialog();
932 /* select the KB page */
933 wid = ui_lookup_widget(ui_widgets.prefs_dialog, "frame22");
934 if (wid != NULL)
936 GtkNotebook *nb = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
938 if (nb != NULL)
939 gtk_notebook_set_current_page(nb, gtk_notebook_page_num(nb, wid));
942 gtk_widget_destroy(dialog);
943 key_dialog = NULL;
947 void keybindings_show_shortcuts(void)
949 if (key_dialog)
950 gtk_widget_destroy(key_dialog); /* in case the key_dialog is still visible */
952 key_dialog = create_dialog();
953 g_signal_connect(key_dialog, "response", G_CALLBACK(on_dialog_response), NULL);
954 gtk_widget_show_all(key_dialog);
958 static gboolean check_fixed_kb(guint keyval, guint state)
960 /* check alt-0 to alt-9 for setting current notebook page */
961 if (state == GDK_MOD1_MASK && keyval >= GDK_0 && keyval <= GDK_9)
963 gint page = keyval - GDK_0 - 1;
964 gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
966 /* alt-0 is for the rightmost tab */
967 if (keyval == GDK_0)
968 page = npages - 1;
969 /* invert the order if tabs are added on the other side */
970 if (swap_alt_tab_order && ! file_prefs.tab_order_ltr)
971 page = (npages - 1) - page;
973 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
974 return TRUE;
976 /* note: these are now overridden by default with move tab bindings */
977 if (keyval == GDK_Page_Up || keyval == GDK_Page_Down)
979 /* switch to first or last document */
980 if (state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
982 if (keyval == GDK_Page_Up)
983 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), 0);
984 if (keyval == GDK_Page_Down)
985 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), -1);
986 return TRUE;
989 return FALSE;
993 static gboolean check_snippet_completion(GeanyDocument *doc)
995 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
997 g_return_val_if_fail(doc, FALSE);
999 /* keybinding only valid when scintilla widget has focus */
1000 if (focusw == GTK_WIDGET(doc->editor->sci))
1002 ScintillaObject *sci = doc->editor->sci;
1003 gint pos = sci_get_current_position(sci);
1005 if (editor_prefs.complete_snippets)
1006 return editor_complete_snippet(doc->editor, pos);
1008 return FALSE;
1012 /* Transforms a GdkEventKey event into a GdkEventButton event */
1013 static void trigger_button_event(GtkWidget *widget, guint32 event_time)
1015 GdkEventButton *event;
1016 gboolean ret;
1018 event = g_new0(GdkEventButton, 1);
1020 if (GTK_IS_TEXT_VIEW(widget))
1021 event->window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT);
1022 else
1023 event->window = gtk_widget_get_window(widget);
1024 event->time = event_time;
1025 event->type = GDK_BUTTON_PRESS;
1026 event->button = 3;
1028 g_signal_emit_by_name(widget, "button-press-event", event, &ret);
1029 g_signal_emit_by_name(widget, "button-release-event", event, &ret);
1031 g_free(event);
1035 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
1036 * widgets. Without this special handling, the notebook tab list of the documents' notebook
1037 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
1038 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1039 * notebook tab list. */
1040 static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
1042 if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
1044 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1045 if (doc != NULL)
1047 if (focusw == doc->priv->tag_tree)
1049 trigger_button_event(focusw, event_time);
1050 return TRUE;
1052 if (focusw == GTK_WIDGET(doc->editor->sci))
1054 if (keyval == GDK_Menu)
1055 { /* show editor popup menu */
1056 trigger_button_event(focusw, event_time);
1057 return TRUE;
1059 else
1060 { /* show tab bar menu */
1061 trigger_button_event(main_widgets.notebook, event_time);
1062 return TRUE;
1066 if (focusw == tv.tree_openfiles
1067 || focusw == msgwindow.tree_status
1068 || focusw == msgwindow.tree_compiler
1069 || focusw == msgwindow.tree_msg
1070 || focusw == msgwindow.scribble
1071 #ifdef HAVE_VTE
1072 || (vte_info.have_vte && focusw == vc->vte)
1073 #endif
1076 trigger_button_event(focusw, event_time);
1077 return TRUE;
1080 return FALSE;
1084 #ifdef HAVE_VTE
1085 static gboolean on_menu_expose_event(GtkWidget *widget, GdkEventExpose *event,
1086 gpointer user_data)
1088 if (!GTK_WIDGET_SENSITIVE(widget))
1089 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1090 return FALSE;
1094 static gboolean set_sensitive(gpointer widget)
1096 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1097 return FALSE;
1101 static gboolean check_vte(GdkModifierType state, guint keyval)
1103 guint i;
1104 GeanyKeyBinding *kb;
1105 GeanyKeyGroup *group;
1106 GtkWidget *widget;
1108 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != vc->vte)
1109 return FALSE;
1110 /* let VTE copy/paste override any user keybinding */
1111 if (state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) && (keyval == GDK_c || keyval == GDK_v))
1112 return TRUE;
1113 if (! vc->enable_bash_keys)
1114 return FALSE;
1115 /* prevent menubar flickering: */
1116 if (state == GDK_SHIFT_MASK && (keyval >= GDK_a && keyval <= GDK_z))
1117 return FALSE;
1118 if (state == 0 && (keyval < GDK_F1 || keyval > GDK_F35)) /* e.g. backspace */
1119 return FALSE;
1121 /* make focus commands override any bash commands */
1122 group = keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS);
1123 foreach_ptr_array(kb, i, group->key_items)
1125 if (state == kb->mods && keyval == kb->key)
1126 return FALSE;
1129 /* Temporarily disable the menus to prevent conflicting menu accelerators
1130 * from overriding the VTE bash shortcuts.
1131 * Note: maybe there's a better way of doing this ;-) */
1132 widget = ui_lookup_widget(main_widgets.window, "menubar1");
1133 gtk_widget_set_sensitive(widget, FALSE);
1135 /* make the menubar sensitive before it is redrawn */
1136 static gboolean connected = FALSE;
1137 if (!connected)
1138 g_signal_connect(widget, "expose-event", G_CALLBACK(on_menu_expose_event), NULL);
1141 widget = main_widgets.editor_menu;
1142 gtk_widget_set_sensitive(widget, FALSE);
1143 g_idle_add(set_sensitive, widget);
1144 return TRUE;
1146 #endif
1149 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1150 static guint key_kp_translate(guint key_in)
1152 switch (key_in)
1154 case GDK_KP_Down:
1155 return GDK_Down;
1156 case GDK_KP_Up:
1157 return GDK_Up;
1158 case GDK_KP_Left:
1159 return GDK_Left;
1160 case GDK_KP_Right:
1161 return GDK_Right;
1162 case GDK_KP_Home:
1163 return GDK_Home;
1164 case GDK_KP_End:
1165 return GDK_End;
1166 case GDK_KP_Page_Up:
1167 return GDK_Page_Up;
1168 case GDK_KP_Page_Down:
1169 return GDK_Page_Down;
1170 case GDK_KP_Delete:
1171 return GDK_Delete;
1172 case GDK_KP_Insert:
1173 return GDK_Insert;
1174 default:
1175 return key_in;
1180 /* Check if event keypress matches keybinding combo */
1181 gboolean keybindings_check_event(GdkEventKey *ev, GeanyKeyBinding *kb)
1183 guint state, keyval;
1185 if (ev->keyval == 0)
1186 return FALSE;
1188 keyval = ev->keyval;
1189 state = ev->state & gtk_accelerator_get_default_mod_mask();
1190 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1191 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1192 if (keyval >= GDK_A && keyval <= GDK_Z)
1193 keyval += GDK_a - GDK_A;
1195 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1196 keyval = key_kp_translate(keyval);
1198 return (keyval == kb->key && state == kb->mods);
1202 /* central keypress event handler, almost all keypress events go to this function */
1203 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1205 guint state, keyval;
1206 gsize g, i;
1207 GeanyDocument *doc;
1208 GeanyKeyGroup *group;
1209 GeanyKeyBinding *kb;
1211 if (ev->keyval == 0)
1212 return FALSE;
1214 doc = document_get_current();
1215 if (doc)
1216 document_check_disk_status(doc, FALSE);
1218 keyval = ev->keyval;
1219 state = ev->state & gtk_accelerator_get_default_mod_mask();
1220 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1221 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1222 if (keyval >= GDK_A && keyval <= GDK_Z)
1223 keyval += GDK_a - GDK_A;
1225 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1226 keyval = key_kp_translate(keyval);
1228 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1230 /* special cases */
1231 #ifdef HAVE_VTE
1232 if (vte_info.have_vte && check_vte(state, keyval))
1233 return FALSE;
1234 #endif
1235 if (check_menu_key(doc, keyval, state, ev->time))
1236 return TRUE;
1238 foreach_ptr_array(group, g, keybinding_groups)
1240 foreach_ptr_array(kb, i, group->key_items)
1242 if (keyval == kb->key && state == kb->mods)
1244 /* call the corresponding callback function for this shortcut */
1245 if (kb->callback)
1247 kb->callback(kb->id);
1248 return TRUE;
1250 else if (group->callback)
1252 if (group->callback(kb->id))
1253 return TRUE;
1254 else
1255 continue; /* not handled */
1257 g_warning("No callback for keybinding %s: %s!", group->name, kb->name);
1261 /* fixed keybindings can be overridden by user bindings, so check them last */
1262 if (check_fixed_kb(keyval, state))
1263 return TRUE;
1264 return FALSE;
1268 static gboolean is_modifier_key(guint keyval)
1270 switch (keyval)
1272 case GDK_Shift_L:
1273 case GDK_Shift_R:
1274 case GDK_Control_L:
1275 case GDK_Control_R:
1276 case GDK_Meta_L:
1277 case GDK_Meta_R:
1278 case GDK_Alt_L:
1279 case GDK_Alt_R:
1280 case GDK_Super_L:
1281 case GDK_Super_R:
1282 case GDK_Hyper_L:
1283 case GDK_Hyper_R:
1284 return TRUE;
1285 default:
1286 return FALSE;
1291 /* group_id must be a core group, e.g. GEANY_KEY_GROUP_EDITOR
1292 * key_id e.g. GEANY_KEYS_EDITOR_CALLTIP */
1293 GeanyKeyBinding *keybindings_lookup_item(guint group_id, guint key_id)
1295 GeanyKeyGroup *group;
1297 g_return_val_if_fail(group_id < GEANY_KEY_GROUP_COUNT, NULL); /* can't use this for plugin groups */
1299 group = keybindings_get_core_group(group_id);
1301 g_return_val_if_fail(group, NULL);
1302 return keybindings_get_item(group, key_id);
1306 /** Mimics a (built-in only) keybinding action.
1307 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1308 * @param group_id @ref GeanyKeyGroupID keybinding group index that contains the @a key_id keybinding.
1309 * @param key_id @ref GeanyKeyBindingID keybinding index. */
1310 void keybindings_send_command(guint group_id, guint key_id)
1312 GeanyKeyBinding *kb;
1314 kb = keybindings_lookup_item(group_id, key_id);
1315 if (kb)
1317 if (kb->callback)
1318 kb->callback(key_id);
1319 else
1321 GeanyKeyGroup *group = keybindings_get_core_group(group_id);
1323 if (group->callback)
1324 group->callback(key_id);
1330 /* These are the callback functions, either each group or each shortcut has it's
1331 * own function. */
1334 static gboolean cb_func_file_action(guint key_id)
1336 switch (key_id)
1338 case GEANY_KEYS_FILE_NEW:
1339 document_new_file(NULL, NULL, NULL);
1340 break;
1341 case GEANY_KEYS_FILE_OPEN:
1342 on_open1_activate(NULL, NULL);
1343 break;
1344 case GEANY_KEYS_FILE_OPENSELECTED:
1345 on_menu_open_selected_file1_activate(NULL, NULL);
1346 break;
1347 case GEANY_KEYS_FILE_OPENLASTTAB:
1349 gchar *utf8_filename = g_queue_peek_head(ui_prefs.recent_queue);
1350 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1351 document_open_file(locale_filename, FALSE, NULL, NULL);
1352 g_free(locale_filename);
1353 break;
1355 case GEANY_KEYS_FILE_SAVE:
1356 on_save1_activate(NULL, NULL);
1357 break;
1358 case GEANY_KEYS_FILE_SAVEAS:
1359 on_save_as1_activate(NULL, NULL);
1360 break;
1361 case GEANY_KEYS_FILE_SAVEALL:
1362 on_save_all1_activate(NULL, NULL);
1363 break;
1364 case GEANY_KEYS_FILE_CLOSE:
1365 on_close1_activate(NULL, NULL);
1366 break;
1367 case GEANY_KEYS_FILE_CLOSEALL:
1368 on_close_all1_activate(NULL, NULL);
1369 break;
1370 case GEANY_KEYS_FILE_RELOAD:
1371 on_toolbutton_reload_clicked(NULL, NULL);
1372 break;
1373 case GEANY_KEYS_FILE_PRINT:
1374 on_print1_activate(NULL, NULL);
1375 break;
1377 return TRUE;
1381 static gboolean cb_func_project_action(guint key_id)
1383 switch (key_id)
1385 case GEANY_KEYS_PROJECT_PROPERTIES:
1386 if (app->project)
1387 on_project_properties1_activate(NULL, NULL);
1388 break;
1390 return TRUE;
1394 static void cb_func_menu_preferences(guint key_id)
1396 switch (key_id)
1398 case GEANY_KEYS_SETTINGS_PREFERENCES:
1399 on_preferences1_activate(NULL, NULL);
1400 break;
1401 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES:
1402 on_plugin_preferences1_activate(NULL, NULL);
1403 break;
1408 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id)
1410 on_help1_activate(NULL, NULL);
1414 static gboolean cb_func_search_action(guint key_id)
1416 GeanyDocument *doc = document_get_current();
1417 ScintillaObject *sci;
1419 if (key_id == GEANY_KEYS_SEARCH_FINDINFILES)
1421 on_find_in_files1_activate(NULL, NULL); /* works without docs too */
1422 return TRUE;
1424 if (!doc)
1425 return TRUE;
1426 sci = doc->editor->sci;
1428 switch (key_id)
1430 case GEANY_KEYS_SEARCH_FIND:
1431 on_find1_activate(NULL, NULL); break;
1432 case GEANY_KEYS_SEARCH_FINDNEXT:
1433 on_find_next1_activate(NULL, NULL); break;
1434 case GEANY_KEYS_SEARCH_FINDPREVIOUS:
1435 on_find_previous1_activate(NULL, NULL); break;
1436 case GEANY_KEYS_SEARCH_FINDPREVSEL:
1437 on_find_prevsel1_activate(NULL, NULL); break;
1438 case GEANY_KEYS_SEARCH_FINDNEXTSEL:
1439 on_find_nextsel1_activate(NULL, NULL); break;
1440 case GEANY_KEYS_SEARCH_REPLACE:
1441 on_replace1_activate(NULL, NULL); break;
1442 case GEANY_KEYS_SEARCH_NEXTMESSAGE:
1443 on_next_message1_activate(NULL, NULL); break;
1444 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE:
1445 on_previous_message1_activate(NULL, NULL); break;
1446 case GEANY_KEYS_SEARCH_FINDUSAGE:
1447 on_find_usage1_activate(NULL, NULL); break;
1448 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE:
1449 on_find_document_usage1_activate(NULL, NULL); break;
1450 case GEANY_KEYS_SEARCH_MARKALL:
1452 gchar *text = get_current_word_or_sel(doc, TRUE);
1454 if (sci_has_selection(sci))
1455 search_mark_all(doc, text, SCFIND_MATCHCASE);
1456 else
1458 /* clears markers if text is null */
1459 search_mark_all(doc, text, SCFIND_MATCHCASE | SCFIND_WHOLEWORD);
1461 g_free(text);
1462 break;
1465 return TRUE;
1469 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id)
1471 on_show_color_chooser1_activate(NULL, NULL);
1475 static gboolean cb_func_view_action(guint key_id)
1477 switch (key_id)
1479 case GEANY_KEYS_VIEW_TOGGLEALL:
1480 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
1481 break;
1482 case GEANY_KEYS_VIEW_SIDEBAR:
1483 on_menu_show_sidebar1_toggled(NULL, NULL);
1484 break;
1485 case GEANY_KEYS_VIEW_ZOOMIN:
1486 on_zoom_in1_activate(NULL, NULL);
1487 break;
1488 case GEANY_KEYS_VIEW_ZOOMOUT:
1489 on_zoom_out1_activate(NULL, NULL);
1490 break;
1491 case GEANY_KEYS_VIEW_ZOOMRESET:
1492 on_normal_size1_activate(NULL, NULL);
1493 break;
1494 default:
1495 break;
1497 return TRUE;
1501 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id)
1503 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1504 ui_lookup_widget(main_widgets.window, "menu_fullscreen1"));
1506 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1510 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id)
1512 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1513 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1515 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1519 static gboolean cb_func_build_action(guint key_id)
1521 GtkWidget *item;
1522 BuildMenuItems *menu_items;
1523 GeanyDocument *doc = document_get_current();
1525 if (doc == NULL)
1526 return TRUE;
1528 if (!GTK_WIDGET_IS_SENSITIVE(ui_lookup_widget(main_widgets.window, "menu_build1")))
1529 return TRUE;
1531 menu_items = build_get_menu_items(doc->file_type->id);
1532 /* TODO make it a table??*/
1533 switch (key_id)
1535 case GEANY_KEYS_BUILD_COMPILE:
1536 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_COMPILE)];
1537 break;
1538 case GEANY_KEYS_BUILD_LINK:
1539 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_BUILD)];
1540 break;
1541 case GEANY_KEYS_BUILD_MAKE:
1542 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_ALL)];
1543 break;
1544 case GEANY_KEYS_BUILD_MAKEOWNTARGET:
1545 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_CUSTOM)];
1546 break;
1547 case GEANY_KEYS_BUILD_MAKEOBJECT:
1548 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)];
1549 break;
1550 case GEANY_KEYS_BUILD_NEXTERROR:
1551 item = menu_items->menu_item[GBG_FIXED][GBF_NEXT_ERROR];
1552 break;
1553 case GEANY_KEYS_BUILD_PREVIOUSERROR:
1554 item = menu_items->menu_item[GBG_FIXED][GBF_PREV_ERROR];
1555 break;
1556 case GEANY_KEYS_BUILD_RUN:
1557 item = menu_items->menu_item[GEANY_GBG_EXEC][GBO_TO_CMD(GEANY_GBO_EXEC)];
1558 break;
1559 case GEANY_KEYS_BUILD_OPTIONS:
1560 item = menu_items->menu_item[GBG_FIXED][GBF_COMMANDS];
1561 break;
1562 default:
1563 item = NULL;
1565 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1566 * sensitive state, but some other menus don't update the sensitive status until
1567 * they are redrawn. */
1568 if (item && GTK_WIDGET_IS_SENSITIVE(item))
1569 gtk_menu_item_activate(GTK_MENU_ITEM(item));
1570 return TRUE;
1574 static gboolean read_current_word(GeanyDocument *doc, gboolean sci_word)
1576 if (doc == NULL)
1577 return FALSE;
1579 if (sci_word)
1581 editor_find_current_word_sciwc(doc->editor, -1,
1582 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
1584 else
1586 editor_find_current_word(doc->editor, -1,
1587 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1590 return (*editor_info.current_word != 0);
1594 static gboolean check_current_word(GeanyDocument *doc, gboolean sci_word)
1596 if (! read_current_word(doc, sci_word))
1598 utils_beep();
1599 return FALSE;
1601 return TRUE;
1605 static gchar *get_current_word_or_sel(GeanyDocument *doc, gboolean sci_word)
1607 ScintillaObject *sci = doc->editor->sci;
1609 if (sci_has_selection(sci))
1610 return sci_get_selection_contents(sci);
1612 return read_current_word(doc, sci_word) ? g_strdup(editor_info.current_word) : NULL;
1616 static void focus_sidebar(void)
1618 if (ui_prefs.sidebar_visible)
1620 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook));
1621 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), page_num);
1623 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1624 gtk_widget_child_focus(page, GTK_DIR_TAB_FORWARD);
1629 static void focus_msgwindow(void)
1631 if (ui_prefs.msgwindow_visible)
1633 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook));
1634 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(msgwindow.notebook), page_num);
1636 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(page)));
1641 static gboolean cb_func_switch_action(guint key_id)
1643 switch (key_id)
1645 case GEANY_KEYS_FOCUS_EDITOR:
1647 GeanyDocument *doc = document_get_current();
1648 if (doc != NULL)
1650 GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
1651 if (GTK_WIDGET_HAS_FOCUS(sci))
1652 ui_update_statusbar(doc, -1);
1653 else
1654 gtk_widget_grab_focus(sci);
1656 break;
1658 case GEANY_KEYS_FOCUS_SCRIBBLE:
1659 msgwin_switch_tab(MSG_SCRATCH, TRUE);
1660 break;
1661 case GEANY_KEYS_FOCUS_SEARCHBAR:
1662 if (toolbar_prefs.visible)
1664 GtkWidget *search_entry = toolbar_get_widget_child_by_name("SearchEntry");
1665 if (search_entry != NULL)
1666 gtk_widget_grab_focus(search_entry);
1668 break;
1669 case GEANY_KEYS_FOCUS_SIDEBAR:
1670 focus_sidebar();
1671 break;
1672 case GEANY_KEYS_FOCUS_VTE:
1673 msgwin_switch_tab(MSG_VTE, TRUE);
1674 break;
1675 case GEANY_KEYS_FOCUS_COMPILER:
1676 msgwin_switch_tab(MSG_COMPILER, TRUE);
1677 break;
1678 case GEANY_KEYS_FOCUS_MESSAGES:
1679 msgwin_switch_tab(MSG_MESSAGE, TRUE);
1680 break;
1681 case GEANY_KEYS_FOCUS_MESSAGE_WINDOW:
1682 focus_msgwindow();
1683 break;
1684 case GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST:
1685 sidebar_focus_openfiles_tab();
1686 break;
1687 case GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST:
1688 sidebar_focus_symbols_tab();
1689 break;
1691 return TRUE;
1695 static void switch_notebook_page(gint direction)
1697 gint page_count, cur_page;
1698 gboolean parent_is_notebook = FALSE;
1699 GtkNotebook *notebook;
1700 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1702 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1705 parent_is_notebook = GTK_IS_NOTEBOOK(focusw);
1707 while (! parent_is_notebook && (focusw = gtk_widget_get_parent(focusw)) != NULL);
1709 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1710 if (parent_is_notebook)
1711 notebook = GTK_NOTEBOOK(focusw);
1712 else
1713 notebook = GTK_NOTEBOOK(main_widgets.notebook);
1715 /* now switch pages */
1716 page_count = gtk_notebook_get_n_pages(notebook);
1717 cur_page = gtk_notebook_get_current_page(notebook);
1719 if (direction == GTK_DIR_LEFT)
1721 if (cur_page > 0)
1722 gtk_notebook_set_current_page(notebook, cur_page - 1);
1723 else
1724 gtk_notebook_set_current_page(notebook, page_count - 1);
1726 else if (direction == GTK_DIR_RIGHT)
1728 if (cur_page < page_count - 1)
1729 gtk_notebook_set_current_page(notebook, cur_page + 1);
1730 else
1731 gtk_notebook_set_current_page(notebook, 0);
1736 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id)
1738 switch_notebook_page(GTK_DIR_LEFT);
1742 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id)
1744 switch_notebook_page(GTK_DIR_RIGHT);
1748 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1750 /* user may have rebound keybinding to a different modifier than Ctrl, so check all */
1751 if (switch_in_progress && is_modifier_key(ev->keyval))
1753 switch_in_progress = FALSE;
1755 if (switch_dialog)
1757 gtk_widget_destroy(switch_dialog);
1758 switch_dialog = NULL;
1761 mru_pos = 0;
1763 return FALSE;
1767 static GtkWidget *ui_minimal_dialog_new(GtkWindow *parent, const gchar *title)
1769 GtkWidget *dialog;
1771 dialog = gtk_window_new(GTK_WINDOW_POPUP);
1773 if (parent)
1775 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
1776 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
1778 gtk_window_set_title(GTK_WINDOW(dialog), title);
1779 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
1780 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
1782 gtk_widget_set_name(dialog, "GeanyDialog");
1783 return dialog;
1787 static GtkWidget *create_switch_dialog(void)
1789 GtkWidget *dialog, *widget, *vbox;
1791 dialog = ui_minimal_dialog_new(GTK_WINDOW(main_widgets.window), _("Switch to Document"));
1792 gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE);
1793 gtk_window_set_default_size(GTK_WINDOW(dialog), 150, -1);
1795 vbox = gtk_vbox_new(FALSE, 6);
1796 gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
1797 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1799 widget = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
1800 gtk_container_add(GTK_CONTAINER(vbox), widget);
1802 widget = geany_wrap_label_new(NULL);
1803 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
1804 gtk_container_add(GTK_CONTAINER(vbox), widget);
1805 switch_dialog_label = widget;
1807 g_signal_connect(dialog, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
1808 return dialog;
1812 static void update_filename_label(void)
1814 if (!switch_dialog)
1816 switch_dialog = create_switch_dialog();
1817 gtk_widget_show_all(switch_dialog);
1820 gtk_label_set_text(GTK_LABEL(switch_dialog_label), DOC_FILENAME(document_get_current()));
1824 static gboolean on_switch_timeout(G_GNUC_UNUSED gpointer data)
1826 if (!switch_in_progress || switch_dialog)
1828 return FALSE;
1831 update_filename_label();
1832 return FALSE;
1836 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id)
1838 GeanyDocument *last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1840 if (! DOC_VALID(last_doc))
1842 utils_beep();
1843 mru_pos = 0;
1844 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1846 if (! DOC_VALID(last_doc))
1847 return;
1849 document_show_tab(last_doc);
1851 /* if there's a modifier key, we can switch back in MRU order each time unless
1852 * the key is released */
1853 if (!switch_in_progress)
1855 switch_in_progress = TRUE;
1857 /* because switch_in_progress was not set when we called
1858 * gtk_notebook_set_current_page() above, this function inserted last_doc
1859 * into the queue => on mru_pos = 0 there is last_doc, on mru_pos = 1
1860 * there is the currently displayed doc, so we want to continue from 2
1861 * next time this function is called */
1862 mru_pos = 2;
1864 /* delay showing dialog to give user time to let go of any modifier keys */
1865 g_timeout_add(600, on_switch_timeout, NULL);
1867 else
1869 update_filename_label();
1870 mru_pos += 1;
1875 /* move document left/right/first/last */
1876 static void cb_func_move_tab(guint key_id)
1878 GtkWidget *sci;
1879 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
1880 gint cur_page = gtk_notebook_get_current_page(nb);
1881 GeanyDocument *doc = document_get_current();
1883 if (doc == NULL)
1884 return;
1886 sci = GTK_WIDGET(doc->editor->sci);
1888 switch (key_id)
1890 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT:
1891 gtk_notebook_reorder_child(nb, sci, cur_page - 1); /* notebook wraps around by default */
1892 break;
1893 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT:
1895 gint npage = cur_page + 1;
1897 if (npage == gtk_notebook_get_n_pages(nb))
1898 npage = 0; /* wraparound */
1899 gtk_notebook_reorder_child(nb, sci, npage);
1900 break;
1902 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST:
1903 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? 0 : -1);
1904 break;
1905 case GEANY_KEYS_NOTEBOOK_MOVETABLAST:
1906 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? -1 : 0);
1907 break;
1909 return;
1913 static void goto_matching_brace(GeanyDocument *doc)
1915 gint pos, new_pos;
1916 gint after_brace;
1918 if (doc == NULL)
1919 return;
1921 pos = sci_get_current_position(doc->editor->sci);
1922 after_brace = pos > 0 && utils_isbrace(sci_get_char_at(doc->editor->sci, pos - 1), TRUE);
1923 pos -= after_brace; /* set pos to the brace */
1925 new_pos = sci_find_matching_brace(doc->editor->sci, pos);
1926 if (new_pos != -1)
1927 { /* set the cursor at/after the brace */
1928 sci_set_current_position(doc->editor->sci, new_pos + (!after_brace), FALSE);
1929 editor_display_current_line(doc->editor, 0.5F);
1934 static gboolean cb_func_clipboard_action(guint key_id)
1936 GeanyDocument *doc = document_get_current();
1938 if (doc == NULL)
1939 return TRUE;
1941 switch (key_id)
1943 case GEANY_KEYS_CLIPBOARD_CUT:
1944 on_cut1_activate(NULL, NULL);
1945 break;
1946 case GEANY_KEYS_CLIPBOARD_COPY:
1947 on_copy1_activate(NULL, NULL);
1948 break;
1949 case GEANY_KEYS_CLIPBOARD_PASTE:
1950 on_paste1_activate(NULL, NULL);
1951 break;
1952 case GEANY_KEYS_CLIPBOARD_COPYLINE:
1953 sci_send_command(doc->editor->sci, SCI_LINECOPY);
1954 break;
1955 case GEANY_KEYS_CLIPBOARD_CUTLINE:
1956 sci_send_command(doc->editor->sci, SCI_LINECUT);
1957 break;
1959 return TRUE;
1963 static void goto_tag(GeanyDocument *doc, gboolean definition)
1965 gchar *text = get_current_word_or_sel(doc, FALSE);
1967 if (text)
1968 symbols_goto_tag(text, definition);
1969 else
1970 utils_beep();
1972 g_free(text);
1976 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
1977 static gboolean cb_func_goto_action(guint key_id)
1979 gint cur_line;
1980 GeanyDocument *doc = document_get_current();
1982 if (doc == NULL)
1983 return TRUE;
1985 cur_line = sci_get_current_line(doc->editor->sci);
1987 switch (key_id)
1989 case GEANY_KEYS_GOTO_BACK:
1990 navqueue_go_back();
1991 return TRUE;
1992 case GEANY_KEYS_GOTO_FORWARD:
1993 navqueue_go_forward();
1994 return TRUE;
1995 case GEANY_KEYS_GOTO_LINE:
1997 if (toolbar_prefs.visible)
1999 GtkWidget *wid = toolbar_get_widget_child_by_name("GotoEntry");
2001 /* use toolbar item if shown & not in the drop down overflow menu */
2002 if (wid && GTK_WIDGET_MAPPED(wid))
2004 gtk_widget_grab_focus(wid);
2005 return TRUE;
2008 on_go_to_line_activate(NULL, NULL);
2009 return TRUE;
2011 case GEANY_KEYS_GOTO_MATCHINGBRACE:
2012 goto_matching_brace(doc);
2013 return TRUE;
2014 case GEANY_KEYS_GOTO_TOGGLEMARKER:
2016 sci_toggle_marker_at_line(doc->editor->sci, cur_line, 1);
2017 return TRUE;
2019 case GEANY_KEYS_GOTO_NEXTMARKER:
2021 gint mline = sci_marker_next(doc->editor->sci, cur_line + 1, 1 << 1, TRUE);
2023 if (mline != -1)
2025 sci_set_current_line(doc->editor->sci, mline);
2026 editor_display_current_line(doc->editor, 0.5F);
2028 return TRUE;
2030 case GEANY_KEYS_GOTO_PREVIOUSMARKER:
2032 gint mline = sci_marker_previous(doc->editor->sci, cur_line - 1, 1 << 1, TRUE);
2034 if (mline != -1)
2036 sci_set_current_line(doc->editor->sci, mline);
2037 editor_display_current_line(doc->editor, 0.5F);
2039 return TRUE;
2041 case GEANY_KEYS_GOTO_TAGDEFINITION:
2042 goto_tag(doc, TRUE);
2043 return TRUE;
2044 case GEANY_KEYS_GOTO_TAGDECLARATION:
2045 goto_tag(doc, FALSE);
2046 return TRUE;
2048 /* only check editor-sensitive keybindings when editor has focus so home,end still
2049 * work in other widgets */
2050 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
2051 return FALSE;
2053 switch (key_id)
2055 case GEANY_KEYS_GOTO_LINESTART:
2056 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
2057 break;
2058 case GEANY_KEYS_GOTO_LINEEND:
2059 sci_send_command(doc->editor->sci, SCI_LINEEND);
2060 break;
2061 case GEANY_KEYS_GOTO_LINEENDVISUAL:
2062 sci_send_command(doc->editor->sci, SCI_LINEENDDISPLAY);
2063 break;
2064 case GEANY_KEYS_GOTO_PREVWORDPART:
2065 sci_send_command(doc->editor->sci, SCI_WORDPARTLEFT);
2066 break;
2067 case GEANY_KEYS_GOTO_NEXTWORDPART:
2068 sci_send_command(doc->editor->sci, SCI_WORDPARTRIGHT);
2069 break;
2071 return TRUE;
2075 static void duplicate_lines(GeanyEditor *editor)
2077 if (sci_get_lines_selected(editor->sci) > 1)
2078 { /* ignore extra_line because of selecting lines from the line number column */
2079 editor_select_lines(editor, FALSE);
2080 sci_selection_duplicate(editor->sci);
2082 else if (sci_has_selection(editor->sci))
2083 sci_selection_duplicate(editor->sci);
2084 else
2085 sci_line_duplicate(editor->sci);
2089 static void delete_lines(GeanyEditor *editor)
2091 editor_select_lines(editor, TRUE); /* include last line (like cut lines, copy lines do) */
2092 sci_clear(editor->sci); /* (SCI_LINEDELETE only does 1 line) */
2096 static void move_lines(GeanyEditor *editor, gboolean down)
2098 ScintillaObject *sci = editor->sci;
2099 gchar *text;
2100 gint pos, line, len;
2102 sci_start_undo_action(sci);
2103 editor_select_lines(editor, FALSE);
2104 len = sci_get_selected_text_length(sci);
2106 pos = sci_get_selection_start(sci);
2107 line = sci_get_line_from_position(sci, pos);
2108 if (down)
2109 line++;
2110 else
2111 line--;
2113 text = sci_get_selection_contents(sci);
2114 sci_clear(sci);
2116 pos = sci_get_position_from_line(sci, line);
2117 sci_insert_text(sci, pos, text);
2118 g_free(text);
2120 sci_set_current_position(sci, pos, TRUE);
2121 sci_set_selection_end(sci, pos + len - 1);
2123 sci_end_undo_action(sci);
2127 /* common function for editor keybindings, only valid when scintilla has focus. */
2128 static gboolean cb_func_editor_action(guint key_id)
2130 GeanyDocument *doc = document_get_current();
2131 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2133 /* edit keybindings only valid when scintilla widget has focus */
2134 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2135 return FALSE; /* also makes tab work outside editor */
2137 switch (key_id)
2139 case GEANY_KEYS_EDITOR_UNDO:
2140 on_undo1_activate(NULL, NULL);
2141 break;
2142 case GEANY_KEYS_EDITOR_REDO:
2143 on_redo1_activate(NULL, NULL);
2144 break;
2145 case GEANY_KEYS_EDITOR_SCROLLTOLINE:
2146 editor_scroll_to_line(doc->editor, -1, 0.5F);
2147 break;
2148 case GEANY_KEYS_EDITOR_SCROLLLINEUP:
2149 sci_send_command(doc->editor->sci, SCI_LINESCROLLUP);
2150 break;
2151 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN:
2152 sci_send_command(doc->editor->sci, SCI_LINESCROLLDOWN);
2153 break;
2154 case GEANY_KEYS_EDITOR_DUPLICATELINE:
2155 duplicate_lines(doc->editor);
2156 break;
2157 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR:
2158 editor_goto_next_snippet_cursor(doc->editor);
2159 break;
2160 case GEANY_KEYS_EDITOR_DELETELINE:
2161 delete_lines(doc->editor);
2162 break;
2163 case GEANY_KEYS_EDITOR_DELETELINETOEND:
2164 sci_send_command(doc->editor->sci, SCI_DELLINERIGHT);
2165 break;
2166 case GEANY_KEYS_EDITOR_TRANSPOSELINE:
2167 sci_send_command(doc->editor->sci, SCI_LINETRANSPOSE);
2168 break;
2169 case GEANY_KEYS_EDITOR_AUTOCOMPLETE:
2170 editor_start_auto_complete(doc->editor, sci_get_current_position(doc->editor->sci), TRUE);
2171 break;
2172 case GEANY_KEYS_EDITOR_CALLTIP:
2173 editor_show_calltip(doc->editor, -1);
2174 break;
2175 case GEANY_KEYS_EDITOR_MACROLIST:
2176 editor_show_macro_list(doc->editor);
2177 break;
2178 case GEANY_KEYS_EDITOR_CONTEXTACTION:
2179 if (check_current_word(doc, FALSE))
2180 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu,
2181 "context_action1")), NULL);
2182 break;
2183 case GEANY_KEYS_EDITOR_COMPLETESNIPPET:
2184 /* allow tab to be overloaded */
2185 return check_snippet_completion(doc);
2187 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION:
2189 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
2190 GEANY_KEYS_EDITOR_COMPLETESNIPPET);
2192 switch (kb->key)
2194 case GDK_space:
2195 sci_add_text(doc->editor->sci, " ");
2196 break;
2197 case GDK_Tab:
2198 sci_send_command(doc->editor->sci, SCI_TAB);
2199 break;
2200 default:
2201 break;
2203 break;
2205 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION:
2206 return editor_complete_word_part(doc->editor);
2208 case GEANY_KEYS_EDITOR_MOVELINEUP:
2209 move_lines(doc->editor, FALSE);
2210 break;
2211 case GEANY_KEYS_EDITOR_MOVELINEDOWN:
2212 move_lines(doc->editor, TRUE);
2213 break;
2215 return TRUE;
2219 static void join_lines(GeanyEditor *editor)
2221 gint start, end, i;
2223 start = sci_get_line_from_position(editor->sci,
2224 sci_get_selection_start(editor->sci));
2225 end = sci_get_line_from_position(editor->sci,
2226 sci_get_selection_end(editor->sci));
2228 /* if there is only one line in selection, join it with the following one */
2229 if (end == start)
2230 end = start + 1;
2233 * remove trailing spaces for every line except the last one
2234 * so that these spaces won't appear within text after joining
2236 for (i = start; i < end; i++)
2237 editor_strip_line_trailing_spaces(editor, i);
2239 /* remove starting spaces from second and following lines due to the same reason */
2240 for (i = start + 1; i <= end; i++)
2241 sci_set_line_indentation(editor->sci, i, 0);
2244 * SCI_LINESJOIN automatically adds spaces between joined lines, including
2245 * empty ones. We should drop empty lines if we want only one space to be
2246 * inserted (see also example below). I don't think we should care of that.
2249 sci_set_target_start(editor->sci,
2250 sci_get_position_from_line(editor->sci, start));
2251 sci_set_target_end(editor->sci,
2252 sci_get_position_from_line(editor->sci, end));
2253 sci_lines_join(editor->sci);
2256 * Example: joining
2258 * [TAB]if (something_wrong)
2259 * [TAB]{
2260 * [TAB][TAB]
2261 * [TAB][TAB]exit(1);[SPACE][SPACE]
2262 * [TAB]}[SPACE]
2264 * gives
2266 * [TAB]if (something_wrong) { exit(1); }[SPACE]
2271 static void split_lines(GeanyEditor *editor, gint column)
2273 gint start, indent, linescount, i, end;
2274 gchar c;
2275 ScintillaObject *sci = editor->sci;
2277 /* don't include trailing newlines */
2278 end = sci_get_selection_end(sci);
2279 while ((c = sci_get_char_at(sci, end - 1)) == '\n' || c == '\r') end--;
2280 sci_set_selection_end(sci, end);
2282 start = sci_get_line_from_position(editor->sci,
2283 sci_get_selection_start(editor->sci));
2286 * If several lines are selected, first join them.
2287 * This allows to reformat text paragraphs easily.
2289 if (sci_get_lines_selected(editor->sci) > 1)
2290 join_lines(editor);
2293 * If this line is short enough, just return
2295 if (column > sci_get_line_end_position(editor->sci, start) -
2296 sci_get_position_from_line(editor->sci, start))
2298 return;
2302 * We have to manipulate line indentation so that indentation
2303 * of the resulting lines would be consistent. For example,
2304 * the result of splitting "[TAB]very long content":
2306 * +-------------+-------------+
2307 * | proper | wrong |
2308 * +-------------+-------------+
2309 * | [TAB]very | [TAB]very |
2310 * | [TAB]long | long |
2311 * | [TAB]content| content |
2312 * +-------------+-------------+
2314 indent = sci_get_line_indentation(editor->sci, start);
2315 sci_set_line_indentation(editor->sci, start, 0);
2318 * Use sci_get_line_count() to determine how many new lines
2319 * appeared during splitting. SCI_LINESSPLIT should better return
2320 * this value itself...
2322 sci_target_from_selection(editor->sci);
2323 linescount = sci_get_line_count(editor->sci);
2324 sci_lines_split(editor->sci,
2325 (column - indent) * sci_text_width(editor->sci, STYLE_DEFAULT, " "));
2326 linescount = sci_get_line_count(editor->sci) - linescount;
2328 /* Fix indentation. */
2329 for (i = start; i <= start + linescount; i++)
2330 sci_set_line_indentation(editor->sci, i, indent);
2332 /* Remove trailing spaces. */
2333 if (editor_prefs.newline_strip || file_prefs.strip_trailing_spaces)
2335 for (i = start; i <= start + linescount; i++)
2336 editor_strip_line_trailing_spaces(editor, i);
2341 /* if cursor < anchor, swap them */
2342 static void sci_fix_selection(ScintillaObject *sci)
2344 gint start, end;
2346 start = sci_get_selection_start(sci);
2347 end = sci_get_selection_end(sci);
2348 sci_set_selection(sci, start, end);
2352 static void reflow_paragraph(GeanyEditor *editor)
2354 ScintillaObject *sci = editor->sci;
2355 gboolean sel;
2356 gint column = -1;
2357 const GeanyEditorPrefs *eprefs = editor_get_prefs(editor);
2359 if (editor->line_breaking)
2361 /* use line break column if enabled */
2362 column = eprefs->line_break_column;
2364 else if (eprefs->long_line_type != 2)
2366 /* use long line if enabled */
2367 column = eprefs->long_line_column;
2369 else
2371 /* do nothing if no column is defined */
2372 utils_beep();
2373 return;
2375 sci_start_undo_action(sci);
2376 sel = sci_has_selection(sci);
2377 if (!sel)
2379 gint line, pos;
2381 editor_select_indent_block(editor);
2383 /* deselect last line break */
2384 pos = sci_get_selection_end(sci);
2385 line = sci_get_line_from_position(sci, pos);
2386 if (line < sci_get_line_count(sci) - 1)
2388 /* not last line */
2389 pos = sci_get_line_end_position(sci, line - 1);
2390 sci_set_selection_end(sci, pos);
2393 sci_fix_selection(sci);
2394 split_lines(editor, column);
2395 if (!sel)
2396 sci_set_anchor(sci, -1);
2398 sci_end_undo_action(sci);
2402 /* common function for format keybindings, only valid when scintilla has focus. */
2403 static gboolean cb_func_format_action(guint key_id)
2405 GeanyDocument *doc = document_get_current();
2406 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2408 /* keybindings only valid when scintilla widget has focus */
2409 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2410 return TRUE;
2412 switch (key_id)
2414 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE:
2415 on_menu_toggle_line_commentation1_activate(NULL, NULL);
2416 break;
2417 case GEANY_KEYS_FORMAT_COMMENTLINE:
2418 on_menu_comment_line1_activate(NULL, NULL);
2419 break;
2420 case GEANY_KEYS_FORMAT_UNCOMMENTLINE:
2421 on_menu_uncomment_line1_activate(NULL, NULL);
2422 break;
2423 case GEANY_KEYS_FORMAT_INCREASEINDENT:
2424 on_menu_increase_indent1_activate(NULL, NULL);
2425 break;
2426 case GEANY_KEYS_FORMAT_DECREASEINDENT:
2427 on_menu_decrease_indent1_activate(NULL, NULL);
2428 break;
2429 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE:
2430 editor_indentation_by_one_space(doc->editor, -1, FALSE);
2431 break;
2432 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE:
2433 editor_indentation_by_one_space(doc->editor, -1, TRUE);
2434 break;
2435 case GEANY_KEYS_FORMAT_AUTOINDENT:
2436 editor_smart_line_indentation(doc->editor, -1);
2437 break;
2438 case GEANY_KEYS_FORMAT_TOGGLECASE:
2439 on_toggle_case1_activate(NULL, NULL);
2440 break;
2441 case GEANY_KEYS_FORMAT_SENDTOCMD1:
2442 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 0)
2443 tools_execute_custom_command(doc, ui_prefs.custom_commands[0]);
2444 break;
2445 case GEANY_KEYS_FORMAT_SENDTOCMD2:
2446 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 1)
2447 tools_execute_custom_command(doc, ui_prefs.custom_commands[1]);
2448 break;
2449 case GEANY_KEYS_FORMAT_SENDTOCMD3:
2450 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 2)
2451 tools_execute_custom_command(doc, ui_prefs.custom_commands[2]);
2452 break;
2453 case GEANY_KEYS_FORMAT_SENDTOVTE:
2454 on_send_selection_to_vte1_activate(NULL, NULL);
2455 break;
2456 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
2457 reflow_paragraph(doc->editor);
2458 break;
2460 return TRUE;
2464 /* common function for select keybindings, only valid when scintilla has focus. */
2465 static gboolean cb_func_select_action(guint key_id)
2467 GeanyDocument *doc;
2468 ScintillaObject *sci;
2469 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2470 GtkWidget *toolbar_search_entry = toolbar_get_widget_child_by_name("SearchEntry");
2471 GtkWidget *toolbar_goto_entry = toolbar_get_widget_child_by_name("GotoEntry");
2473 /* special case for Select All in the scribble widget */
2474 if (key_id == GEANY_KEYS_SELECT_ALL && focusw == msgwindow.scribble)
2476 g_signal_emit_by_name(msgwindow.scribble, "select-all", TRUE);
2477 return TRUE;
2479 /* special case for Select All in the VTE widget */
2480 #ifdef HAVE_VTE
2481 else if (key_id == GEANY_KEYS_SELECT_ALL && vte_info.have_vte && focusw == vc->vte)
2483 vte_select_all();
2484 return TRUE;
2486 #endif
2487 /* special case for Select All in the toolbar search widget */
2488 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_search_entry)
2490 gtk_editable_select_region(GTK_EDITABLE(toolbar_search_entry), 0, -1);
2491 return TRUE;
2493 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_goto_entry)
2495 gtk_editable_select_region(GTK_EDITABLE(toolbar_goto_entry), 0, -1);
2496 return TRUE;
2499 doc = document_get_current();
2500 /* keybindings only valid when scintilla widget has focus */
2501 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2502 return TRUE;
2503 sci = doc->editor->sci;
2505 switch (key_id)
2507 case GEANY_KEYS_SELECT_ALL:
2508 on_menu_select_all1_activate(NULL, NULL);
2509 break;
2510 case GEANY_KEYS_SELECT_WORD:
2511 editor_select_word(doc->editor);
2512 break;
2513 case GEANY_KEYS_SELECT_LINE:
2514 editor_select_lines(doc->editor, FALSE);
2515 break;
2516 case GEANY_KEYS_SELECT_PARAGRAPH:
2517 editor_select_paragraph(doc->editor);
2518 break;
2519 case GEANY_KEYS_SELECT_WORDPARTLEFT:
2520 sci_send_command(sci, SCI_WORDPARTLEFTEXTEND);
2521 break;
2522 case GEANY_KEYS_SELECT_WORDPARTRIGHT:
2523 sci_send_command(sci, SCI_WORDPARTRIGHTEXTEND);
2524 break;
2526 return TRUE;
2530 static gboolean cb_func_document_action(guint key_id)
2532 GeanyDocument *doc = document_get_current();
2534 if (doc == NULL)
2535 return TRUE;
2537 switch (key_id)
2539 case GEANY_KEYS_DOCUMENT_REPLACETABS:
2540 on_replace_tabs_activate(NULL, NULL);
2541 break;
2542 case GEANY_KEYS_DOCUMENT_REPLACESPACES:
2543 on_replace_spaces_activate(NULL, NULL);
2544 break;
2545 case GEANY_KEYS_DOCUMENT_LINEBREAK:
2546 on_line_breaking1_activate(NULL, NULL);
2547 ui_document_show_hide(doc);
2548 break;
2549 case GEANY_KEYS_DOCUMENT_LINEWRAP:
2550 on_line_wrapping1_toggled(NULL, NULL);
2551 ui_document_show_hide(doc);
2552 break;
2553 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST:
2554 document_update_tags(doc);
2555 break;
2556 case GEANY_KEYS_DOCUMENT_FOLDALL:
2557 editor_fold_all(doc->editor);
2558 break;
2559 case GEANY_KEYS_DOCUMENT_UNFOLDALL:
2560 editor_unfold_all(doc->editor);
2561 break;
2562 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD:
2563 if (editor_prefs.folding)
2565 gint line = sci_get_current_line(doc->editor->sci);
2566 editor_toggle_fold(doc->editor, line, 0);
2567 break;
2569 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS:
2570 on_remove_markers1_activate(NULL, NULL);
2571 break;
2572 case GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS:
2573 on_menu_remove_indicators1_activate(NULL, NULL);
2574 break;
2575 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS:
2576 on_remove_markers1_activate(NULL, NULL);
2577 on_menu_remove_indicators1_activate(NULL, NULL);
2578 break;
2580 return TRUE;
2584 static void insert_line_after(GeanyEditor *editor)
2586 ScintillaObject *sci = editor->sci;
2588 sci_send_command(sci, SCI_LINEEND);
2589 sci_send_command(sci, SCI_NEWLINE);
2593 static void insert_line_before(GeanyEditor *editor)
2595 ScintillaObject *sci = editor->sci;
2596 gint line = sci_get_current_line(sci);
2597 gint indentpos = sci_get_line_indent_position(sci, line);
2599 sci_set_current_position(sci, indentpos, TRUE);
2600 sci_send_command(sci, SCI_NEWLINE);
2601 sci_send_command(sci, SCI_LINEUP);
2605 /* common function for insert keybindings, only valid when scintilla has focus. */
2606 static gboolean cb_func_insert_action(guint key_id)
2608 GeanyDocument *doc = document_get_current();
2609 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2611 /* keybindings only valid when scintilla widget has focus */
2612 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2613 return TRUE;
2615 switch (key_id)
2617 case GEANY_KEYS_INSERT_ALTWHITESPACE:
2618 editor_insert_alternative_whitespace(doc->editor);
2619 break;
2620 case GEANY_KEYS_INSERT_DATE:
2621 gtk_menu_item_activate(GTK_MENU_ITEM(
2622 ui_lookup_widget(main_widgets.window, "insert_date_custom1")));
2623 break;
2624 case GEANY_KEYS_INSERT_LINEAFTER:
2625 insert_line_after(doc->editor);
2626 break;
2627 case GEANY_KEYS_INSERT_LINEBEFORE:
2628 insert_line_before(doc->editor);
2629 break;
2631 return TRUE;
2635 /* update key combination */
2636 void keybindings_update_combo(GeanyKeyBinding *kb, guint key, GdkModifierType mods)
2638 GtkWidget *widget = kb->menu_item;
2640 if (widget && kb->key)
2641 gtk_widget_remove_accelerator(widget, kb_accel_group, kb->key, kb->mods);
2643 kb->key = key;
2644 kb->mods = mods;
2646 if (widget && kb->key)
2647 gtk_widget_add_accelerator(widget, "activate", kb_accel_group,
2648 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
2652 /* used for plugins, can be called repeatedly. */
2653 GeanyKeyGroup *keybindings_set_group(GeanyKeyGroup *group, const gchar *section_name,
2654 const gchar *label, gsize count, GeanyKeyGroupCallback callback)
2656 g_return_val_if_fail(section_name, NULL);
2657 g_return_val_if_fail(count, NULL);
2659 /* prevent conflict with core bindings */
2660 g_return_val_if_fail(!g_str_equal(section_name, keybindings_keyfile_group_name), NULL);
2662 if (!group)
2664 group = g_new0(GeanyKeyGroup, 1);
2665 add_kb_group(group, section_name, label, callback, TRUE);
2667 g_free(group->plugin_keys);
2668 group->plugin_keys = g_new0(GeanyKeyBinding, count);
2669 group->plugin_key_count = count;
2670 g_ptr_array_set_size(group->key_items, 0);
2671 return group;
2675 void keybindings_free_group(GeanyKeyGroup *group)
2677 GeanyKeyBinding *kb;
2679 g_ptr_array_free(group->key_items, TRUE);
2681 if (group->plugin)
2683 foreach_c_array(kb, group->plugin_keys, group->plugin_key_count)
2685 g_free(kb->name);
2686 g_free(kb->label);
2688 g_free(group->plugin_keys);
2689 g_ptr_array_remove_fast(keybinding_groups, group);
2690 g_free(group);