Re-enable SIGTERM handling
[geany-mirror.git] / src / keybindings.c
blob77f868c239eec8fee3b21d472564424cbb3b34ef
1 /*
2 * keybindings.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 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 along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 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 **/
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include "keybindings.h"
36 #include "app.h"
37 #include "build.h"
38 #include "callbacks.h"
39 #include "documentprivate.h"
40 #include "filetypes.h"
41 #include "keybindingsprivate.h"
42 #include "main.h"
43 #include "msgwindow.h"
44 #include "navqueue.h"
45 #include "notebook.h"
46 #include "prefs.h"
47 #include "sciwrappers.h"
48 #include "sidebar.h"
49 #include "support.h"
50 #include "symbols.h"
51 #include "toolbar.h"
52 #include "tools.h"
53 #include "ui_utils.h"
54 #include "utils.h"
55 #include "vte.h"
57 #include "gtkcompat.h"
59 #include <gdk/gdkkeysyms.h>
60 #include <string.h>
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;
75 /* central keypress event handler, almost all keypress events go to this function */
76 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
78 static gboolean check_current_word(GeanyDocument *doc, gboolean sci_word);
79 static gboolean read_current_word(GeanyDocument *doc, gboolean sci_word);
80 static gchar *get_current_word_or_sel(GeanyDocument *doc, gboolean sci_word);
82 static gboolean cb_func_file_action(guint key_id);
83 static gboolean cb_func_project_action(guint key_id);
84 static gboolean cb_func_editor_action(guint key_id);
85 static gboolean cb_func_select_action(guint key_id);
86 static gboolean cb_func_format_action(guint key_id);
87 static gboolean cb_func_insert_action(guint key_id);
88 static gboolean cb_func_search_action(guint key_id);
89 static gboolean cb_func_goto_action(guint key_id);
90 static gboolean cb_func_switch_action(guint key_id);
91 static gboolean cb_func_clipboard_action(guint key_id);
92 static gboolean cb_func_build_action(guint key_id);
93 static gboolean cb_func_document_action(guint key_id);
94 static gboolean cb_func_view_action(guint key_id);
96 /* note: new keybindings should normally use per group callbacks */
97 static void cb_func_menu_help(guint key_id);
98 static void cb_func_menu_preferences(guint key_id);
100 static void cb_func_menu_fullscreen(guint key_id);
101 static void cb_func_menu_messagewindow(guint key_id);
103 static void cb_func_menu_opencolorchooser(guint key_id);
105 static void cb_func_switch_tableft(guint key_id);
106 static void cb_func_switch_tabright(guint key_id);
107 static void cb_func_switch_tablastused(guint key_id);
108 static void cb_func_move_tab(guint key_id);
110 static void add_popup_menu_accels(void);
113 /** Gets significant modifiers from a GdkModifierType mask. The set of
114 * significant modifiers corresponds to the default modifier mask as returned
115 * by @c gtk_accelerator_get_default_mod_mask(). In addition, it improves
116 * the Command key handling on OS X by adding @c GEANY_PRIMARY_MOD_MASK
117 * when needed. For this reason it is preferred to use this function
118 * instead of @c gtk_accelerator_set_default_mod_mask().
119 * @param mods GdkModifierType mask.
120 * @return Significant modifiers from the mask.
121 * @since 1.25. */
122 GEANY_API_SYMBOL
123 GdkModifierType keybindings_get_modifiers(GdkModifierType mods)
125 #ifdef __APPLE__
126 if (mods & GDK_MOD2_MASK)
127 mods |= GEANY_PRIMARY_MOD_MASK;
128 #endif
129 return mods & gtk_accelerator_get_default_mod_mask();
133 /** Looks up a keybinding item.
134 * @param group Group.
135 * @param key_id Keybinding index for the group.
136 * @return @transfer{none} The keybinding.
137 * @since 0.19. */
138 GEANY_API_SYMBOL
139 GeanyKeyBinding *keybindings_get_item(GeanyKeyGroup *group, gsize key_id)
141 if (group->plugin)
143 g_assert(key_id < group->plugin_key_count);
144 return &group->plugin_keys[key_id];
146 g_assert(key_id < GEANY_KEYS_COUNT);
147 return &binding_ids[key_id];
151 /* This is used to set default keybindings on startup.
152 * Menu accels are set in apply_kb_accel(). */
153 /** @girskip
154 * Fills a GeanyKeyBinding struct item.
155 * @note Always set @a key and @a mod to 0, otherwise you will likely
156 * cause conflicts with the user's custom, other plugin's keybindings or
157 * future default keybindings.
158 * @param group Group.
159 * @param key_id Keybinding index for the group.
160 * @param callback @nullable Function to call when activated, or @c NULL to use the group callback.
161 * Usually it's better to use the group callback instead - see plugin_set_key_group().
162 * @param key Default key, e.g. @c GDK_j (must be lower case), but usually 0 for unset.
163 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
164 * @param kf_name Key name used for this item in the keybindings configuration file, i.e. @c "menu_new".
165 * @param label Label used in the preferences dialog keybindings tab. May contain
166 * underscores - these won't be displayed.
167 * @param menu_item @nullable Optional widget to set an accelerator for, or @c NULL.
168 * @return The keybinding - normally this is ignored. */
169 GEANY_API_SYMBOL
170 GeanyKeyBinding *keybindings_set_item(GeanyKeyGroup *group, gsize key_id,
171 GeanyKeyCallback callback, guint key, GdkModifierType mod,
172 const gchar *kf_name, const gchar *label, GtkWidget *menu_item)
174 GeanyKeyBinding *kb;
176 g_assert(group->name);
177 kb = keybindings_get_item(group, key_id);
178 g_assert(!kb->name);
179 g_ptr_array_add(group->key_items, kb);
181 if (group->plugin)
183 /* some plugins e.g. GeanyLua need these fields duplicated */
184 SETPTR(kb->name, g_strdup(kf_name));
185 SETPTR(kb->label, g_strdup(label));
187 else
189 /* we don't touch these strings unless group->plugin is set, const cast is safe */
190 kb->name = (gchar *)kf_name;
191 kb->label = (gchar *)label;
193 kb->key = key;
194 kb->mods = mod;
195 kb->default_key = key;
196 kb->default_mods = mod;
197 kb->callback = callback;
198 kb->cb_func = NULL;
199 kb->cb_data = NULL;
200 kb->menu_item = menu_item;
201 kb->id = key_id;
202 return kb;
206 /** Creates a new keybinding using a GeanyKeyBindingFunc and attaches it to a keybinding group
208 * If given the callback should return @c TRUE if the keybinding was handled, otherwise @c FALSE
209 * to allow other callbacks to be run. This allows for multiplexing keybindings on the same keys,
210 * depending on the focused widget (or context). If the callback is NULL the group's callback will
211 * be invoked, but the same rule applies.
213 * @param group Group.
214 * @param key_id Keybinding index for the group.
215 * @param key Default key, e.g. @c GDK_j (must be lower case), but usually 0 for unset.
216 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
217 * @param kf_name Key name used for this item in the keybindings configuration file, i.e. @c "menu_new".
218 * @param label Label used in the preferences dialog keybindings tab. May contain
219 * underscores - these won't be displayed.
220 * @param menu_item @nullable Optional widget to set an accelerator for, or @c NULL.
221 * @param cb @nullable New-style callback to be called when activated, or @c NULL to use the group callback.
222 * @param pdata Plugin-specific data passed back to the callback @a cb.
223 * @param destroy_notify Function that is invoked to free the plugin data when not needed anymore.
224 * @return @transfer{none} The keybinding - normally this is ignored.
226 * @since 1.26 (API 226)
227 * @see See plugin_set_key_group_full
229 GEANY_API_SYMBOL
230 GeanyKeyBinding *keybindings_set_item_full(GeanyKeyGroup *group, gsize key_id,
231 guint key, GdkModifierType mod, const gchar *kf_name, const gchar *label,
232 GtkWidget *menu_item, GeanyKeyBindingFunc cb, gpointer pdata,
233 GDestroyNotify destroy_notify)
235 GeanyKeyBinding *kb;
237 /* For now, this is intended for plugins only */
238 g_assert(group->plugin);
240 kb = keybindings_set_item(group, key_id, NULL, key, mod, kf_name, label, menu_item);
241 kb->cb_func = cb;
242 kb->cb_data = pdata;
243 kb->cb_data_destroy = destroy_notify;
244 return kb;
248 static void free_key_binding(gpointer item)
250 GeanyKeyBinding *kb = item;
252 g_free(kb->name);
253 g_free(kb->label);
255 if (kb->cb_data_destroy)
256 kb->cb_data_destroy(kb->cb_data);
260 static void add_kb_group(GeanyKeyGroup *group,
261 const gchar *name, const gchar *label, GeanyKeyGroupCallback callback, gboolean plugin)
263 g_ptr_array_add(keybinding_groups, group);
265 /* as for items, we only require duplicated name and label for plugins */
266 group->name = plugin ? g_strdup(name) : name;
267 group->label = plugin ? g_strdup(label) : label;
268 group->callback = callback;
269 group->cb_func = NULL;
270 group->cb_data = NULL;
271 group->plugin = plugin;
272 /* Only plugins use the destroy notify thus far */
273 group->key_items = g_ptr_array_new_with_free_func(plugin ? free_key_binding : NULL);
277 GeanyKeyGroup *keybindings_get_core_group(guint id)
279 static GeanyKeyGroup groups[GEANY_KEY_GROUP_COUNT];
281 g_return_val_if_fail(id < GEANY_KEY_GROUP_COUNT, NULL);
283 return &groups[id];
287 static void add_kb(GeanyKeyGroup *group, gsize key_id,
288 GeanyKeyCallback callback, guint key, GdkModifierType mod,
289 const gchar *kf_name, const gchar *label, const gchar *widget_name)
291 GtkWidget *widget = widget_name ?
292 ui_lookup_widget(main_widgets.window, widget_name) : NULL;
294 keybindings_set_item(group, key_id, callback,
295 key, mod, kf_name, label, widget);
299 #define ADD_KB_GROUP(group_id, label, callback) \
300 add_kb_group(keybindings_get_core_group(group_id),\
301 keybindings_keyfile_group_name, label, callback, FALSE)
303 static void init_default_kb(void)
305 GeanyKeyGroup *group;
307 /* visual group order */
308 ADD_KB_GROUP(GEANY_KEY_GROUP_FILE, _("File"), cb_func_file_action);
309 ADD_KB_GROUP(GEANY_KEY_GROUP_EDITOR, _("Editor"), cb_func_editor_action);
310 ADD_KB_GROUP(GEANY_KEY_GROUP_CLIPBOARD, _("Clipboard"), cb_func_clipboard_action);
311 ADD_KB_GROUP(GEANY_KEY_GROUP_SELECT, _("Select"), cb_func_select_action);
312 ADD_KB_GROUP(GEANY_KEY_GROUP_FORMAT, _("Format"), cb_func_format_action);
313 ADD_KB_GROUP(GEANY_KEY_GROUP_INSERT, _("Insert"), cb_func_insert_action);
314 ADD_KB_GROUP(GEANY_KEY_GROUP_SETTINGS, _("Settings"), NULL);
315 ADD_KB_GROUP(GEANY_KEY_GROUP_SEARCH, _("Search"), cb_func_search_action);
316 ADD_KB_GROUP(GEANY_KEY_GROUP_GOTO, _("Go to"), cb_func_goto_action);
317 ADD_KB_GROUP(GEANY_KEY_GROUP_VIEW, _("View"), cb_func_view_action);
318 ADD_KB_GROUP(GEANY_KEY_GROUP_DOCUMENT, _("Document"), cb_func_document_action);
319 ADD_KB_GROUP(GEANY_KEY_GROUP_PROJECT, _("Project"), cb_func_project_action);
320 ADD_KB_GROUP(GEANY_KEY_GROUP_BUILD, _("Build"), cb_func_build_action);
321 ADD_KB_GROUP(GEANY_KEY_GROUP_TOOLS, _("Tools"), NULL);
322 ADD_KB_GROUP(GEANY_KEY_GROUP_HELP, _("Help"), NULL);
323 ADD_KB_GROUP(GEANY_KEY_GROUP_FOCUS, _("Focus"), cb_func_switch_action);
324 ADD_KB_GROUP(GEANY_KEY_GROUP_NOTEBOOK, _("Notebook tab"), NULL);
326 /* Init all fields of keys with default values.
327 * The menu_item field is always the main menu item, popup menu accelerators are
328 * set in add_popup_menu_accels(). */
330 group = keybindings_get_core_group(GEANY_KEY_GROUP_FILE);
332 add_kb(group, GEANY_KEYS_FILE_NEW, NULL,
333 GDK_n, GEANY_PRIMARY_MOD_MASK, "menu_new", _("New"), "menu_new1");
334 add_kb(group, GEANY_KEYS_FILE_OPEN, NULL,
335 GDK_o, GEANY_PRIMARY_MOD_MASK, "menu_open", _("Open"), "menu_open1");
336 add_kb(group, GEANY_KEYS_FILE_OPENSELECTED, NULL,
337 GDK_o, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "menu_open_selected",
338 _("Open selected file"), "menu_open_selected_file1");
339 add_kb(group, GEANY_KEYS_FILE_SAVE, NULL,
340 GDK_s, GEANY_PRIMARY_MOD_MASK, "menu_save", _("Save"), "menu_save1");
341 add_kb(group, GEANY_KEYS_FILE_SAVEAS, NULL,
342 0, 0, "menu_saveas", _("Save as"), "menu_save_as1");
343 add_kb(group, GEANY_KEYS_FILE_SAVEALL, NULL,
344 GDK_s, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "menu_saveall", _("Save all"),
345 "menu_save_all1");
346 add_kb(group, GEANY_KEYS_FILE_PROPERTIES, NULL,
347 0, 0, "file_properties", _("Properties"), "properties1");
348 add_kb(group, GEANY_KEYS_FILE_PRINT, NULL,
349 GDK_p, GEANY_PRIMARY_MOD_MASK, "menu_print", _("Print"), "print1");
350 add_kb(group, GEANY_KEYS_FILE_CLOSE, NULL,
351 GDK_w, GEANY_PRIMARY_MOD_MASK, "menu_close", _("Close"), "menu_close1");
352 add_kb(group, GEANY_KEYS_FILE_CLOSEALL, NULL,
353 GDK_w, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "menu_closeall", _("Close all"),
354 "menu_close_all1");
355 add_kb(group, GEANY_KEYS_FILE_RELOAD, NULL,
356 GDK_r, GEANY_PRIMARY_MOD_MASK, "menu_reloadfile", _("Reload file"), "menu_reload1");
357 add_kb(group, GEANY_KEYS_FILE_OPENLASTTAB, NULL,
358 0, 0, "file_openlasttab", _("Re-open last closed tab"), NULL);
359 add_kb(group, GEANY_KEYS_FILE_QUIT, NULL,
360 GDK_q, GEANY_PRIMARY_MOD_MASK, "menu_quit", _("Quit"), "menu_quit1");
362 group = keybindings_get_core_group(GEANY_KEY_GROUP_PROJECT);
364 add_kb(group, GEANY_KEYS_PROJECT_NEW, NULL,
365 0, 0, "project_new", _("New"), "project_new1");
366 add_kb(group, GEANY_KEYS_PROJECT_OPEN, NULL,
367 0, 0, "project_open", _("Open"), "project_open1");
368 add_kb(group, GEANY_KEYS_PROJECT_PROPERTIES, NULL,
369 0, 0, "project_properties",
370 ui_lookup_stock_label(GTK_STOCK_PROPERTIES), "project_properties1");
371 add_kb(group, GEANY_KEYS_PROJECT_CLOSE, NULL,
372 0, 0, "project_close", _("Close"), "project_close1");
374 group = keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR);
376 add_kb(group, GEANY_KEYS_EDITOR_UNDO, NULL,
377 GDK_z, GEANY_PRIMARY_MOD_MASK, "menu_undo", _("Undo"), "menu_undo2");
378 add_kb(group, GEANY_KEYS_EDITOR_REDO, NULL,
379 GDK_y, GEANY_PRIMARY_MOD_MASK, "menu_redo", _("Redo"), "menu_redo2");
380 add_kb(group, GEANY_KEYS_EDITOR_DUPLICATELINE, NULL,
381 GDK_d, GEANY_PRIMARY_MOD_MASK, "edit_duplicateline", _("D_uplicate Line or Selection"),
382 "duplicate_line_or_selection1");
383 add_kb(group, GEANY_KEYS_EDITOR_DELETELINE, NULL,
384 GDK_k, GEANY_PRIMARY_MOD_MASK, "edit_deleteline", _("_Delete Current Line(s)"),
385 "delete_current_lines1");
386 add_kb(group, GEANY_KEYS_EDITOR_DELETELINETOEND, NULL,
387 GDK_Delete, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "edit_deletelinetoend",
388 _("Delete to line end"), NULL);
389 add_kb(group, GEANY_KEYS_EDITOR_DELETELINETOBEGINNING, NULL,
390 GDK_BackSpace, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "edit_deletelinetobegin",
391 _("Delete to beginning of line"), NULL);
392 /* Note: transpose may fit better in format group, but that would break the API */
393 add_kb(group, GEANY_KEYS_EDITOR_TRANSPOSELINE, NULL,
394 0, 0, "edit_transposeline", _("_Transpose Current Line"), NULL);
395 add_kb(group, GEANY_KEYS_EDITOR_SCROLLTOLINE, NULL,
396 GDK_l, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "edit_scrolltoline", _("Scroll to current line"), NULL);
397 add_kb(group, GEANY_KEYS_EDITOR_SCROLLLINEUP, NULL,
398 GDK_Up, GDK_MOD1_MASK, "edit_scrolllineup", _("Scroll up the view by one line"), NULL);
399 add_kb(group, GEANY_KEYS_EDITOR_SCROLLLINEDOWN, NULL,
400 GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
401 add_kb(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL,
402 GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
403 add_kb(group, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR, NULL,
404 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
405 add_kb(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, NULL,
406 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
407 add_kb(group, GEANY_KEYS_EDITOR_CONTEXTACTION, NULL,
408 0, 0, "popup_contextaction", _("Context Action"), NULL);
409 add_kb(group, GEANY_KEYS_EDITOR_AUTOCOMPLETE, NULL,
410 GDK_space, GEANY_PRIMARY_MOD_MASK, "edit_autocomplete", _("Complete word"), NULL);
411 add_kb(group, GEANY_KEYS_EDITOR_CALLTIP, NULL,
412 GDK_space, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "edit_calltip", _("Show calltip"), NULL);
413 add_kb(group, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION, NULL,
414 GDK_Tab, 0, "edit_wordpartcompletion", _("Word part completion"), NULL);
415 add_kb(group, GEANY_KEYS_EDITOR_MOVELINEUP, NULL,
416 GDK_Page_Up, GDK_MOD1_MASK, "edit_movelineup",
417 _("Move line(s) up"), "move_lines_up1");
418 add_kb(group, GEANY_KEYS_EDITOR_MOVELINEDOWN, NULL,
419 GDK_Page_Down, GDK_MOD1_MASK, "edit_movelinedown",
420 _("Move line(s) down"), "move_lines_down1");
422 group = keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD);
424 add_kb(group, GEANY_KEYS_CLIPBOARD_CUT, NULL,
425 GDK_x, GEANY_PRIMARY_MOD_MASK, "menu_cut", _("Cut"), "menu_cut1");
426 add_kb(group, GEANY_KEYS_CLIPBOARD_COPY, NULL,
427 GDK_c, GEANY_PRIMARY_MOD_MASK, "menu_copy", _("Copy"), "menu_copy1");
428 add_kb(group, GEANY_KEYS_CLIPBOARD_PASTE, NULL,
429 GDK_v, GEANY_PRIMARY_MOD_MASK, "menu_paste", _("Paste"), "menu_paste1");
430 add_kb(group, GEANY_KEYS_CLIPBOARD_COPYLINE, NULL,
431 GDK_c, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "edit_copyline", _("_Copy Current Line(s)"),
432 "copy_current_lines1");
433 add_kb(group, GEANY_KEYS_CLIPBOARD_CUTLINE, NULL,
434 GDK_x, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "edit_cutline", _("Cu_t Current Line(s)"),
435 "cut_current_lines1");
437 group = keybindings_get_core_group(GEANY_KEY_GROUP_SELECT);
439 add_kb(group, GEANY_KEYS_SELECT_ALL, NULL,
440 GDK_a, GEANY_PRIMARY_MOD_MASK, "menu_selectall", _("Select All"), "menu_select_all1");
441 add_kb(group, GEANY_KEYS_SELECT_WORD, NULL,
442 GDK_w, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectword", _("Select current word"), NULL);
443 add_kb(group, GEANY_KEYS_SELECT_LINE, NULL,
444 GDK_l, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectline", _("S_elect Current Line(s)"),
445 "select_current_lines1");
446 add_kb(group, GEANY_KEYS_SELECT_PARAGRAPH, NULL,
447 GDK_p, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectparagraph", _("Se_lect Current Paragraph"),
448 "select_current_paragraph1");
449 add_kb(group, GEANY_KEYS_SELECT_WORDPARTLEFT, NULL,
450 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL);
451 add_kb(group, GEANY_KEYS_SELECT_WORDPARTRIGHT, NULL,
452 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL);
454 group = keybindings_get_core_group(GEANY_KEY_GROUP_FORMAT);
456 add_kb(group, GEANY_KEYS_FORMAT_TOGGLECASE, NULL,
457 GDK_u, GEANY_PRIMARY_MOD_MASK | GDK_MOD1_MASK, "edit_togglecase",
458 _("T_oggle Case of Selection"), "menu_toggle_case2");
459 add_kb(group, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE, NULL,
460 GDK_e, GEANY_PRIMARY_MOD_MASK, "edit_commentlinetoggle", _("Toggle line commentation"),
461 "menu_toggle_line_commentation1");
462 add_kb(group, GEANY_KEYS_FORMAT_COMMENTLINE, NULL,
463 0, 0, "edit_commentline", _("Comment line(s)"), "menu_comment_line1");
464 add_kb(group, GEANY_KEYS_FORMAT_UNCOMMENTLINE, NULL,
465 0, 0, "edit_uncommentline", _("Uncomment line(s)"), "menu_uncomment_line1");
466 add_kb(group, GEANY_KEYS_FORMAT_INCREASEINDENT, NULL,
467 GDK_i, GEANY_PRIMARY_MOD_MASK, "edit_increaseindent", _("Increase indent"),
468 "menu_increase_indent1");
469 add_kb(group, GEANY_KEYS_FORMAT_DECREASEINDENT, NULL,
470 GDK_u, GEANY_PRIMARY_MOD_MASK, "edit_decreaseindent", _("Decrease indent"),
471 "menu_decrease_indent1");
472 add_kb(group, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE, NULL,
473 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL);
474 add_kb(group, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE, NULL,
475 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL);
476 add_kb(group, GEANY_KEYS_FORMAT_AUTOINDENT, NULL,
477 0, 0, "edit_autoindent", _("S_mart Line Indent"), "smart_line_indent1");
478 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD1, NULL,
479 GDK_1, GEANY_PRIMARY_MOD_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL);
480 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD2, NULL,
481 GDK_2, GEANY_PRIMARY_MOD_MASK, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL);
482 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD3, NULL,
483 GDK_3, GEANY_PRIMARY_MOD_MASK, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL);
484 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD4, NULL,
485 0, 0, "edit_sendtocmd4", _("Send to Custom Command 4"), NULL);
486 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD5, NULL,
487 0, 0, "edit_sendtocmd5", _("Send to Custom Command 5"), NULL);
488 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD6, NULL,
489 0, 0, "edit_sendtocmd6", _("Send to Custom Command 6"), NULL);
490 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD7, NULL,
491 0, 0, "edit_sendtocmd7", _("Send to Custom Command 7"), NULL);
492 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD8, NULL,
493 0, 0, "edit_sendtocmd8", _("Send to Custom Command 8"), NULL);
494 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD9, NULL,
495 0, 0, "edit_sendtocmd9", _("Send to Custom Command 9"), NULL);
496 /* may fit better in editor group */
497 add_kb(group, GEANY_KEYS_FORMAT_SENDTOVTE, NULL,
498 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), "send_selection_to_vte1");
499 add_kb(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
500 GDK_j, GEANY_PRIMARY_MOD_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
501 "reflow_lines_block1");
502 keybindings_set_item(group, GEANY_KEYS_FORMAT_JOINLINES, NULL,
503 0, 0, "edit_joinlines", _("Join lines"), NULL);
505 group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
507 add_kb(group, GEANY_KEYS_INSERT_DATE, NULL,
508 GDK_d, GDK_SHIFT_MASK | GDK_MOD1_MASK, "menu_insert_date", _("Insert date"),
509 "insert_date_custom1");
510 add_kb(group, GEANY_KEYS_INSERT_ALTWHITESPACE, NULL,
511 0, 0, "edit_insertwhitespace", _("Insert Alternative _White Space"),
512 "insert_alternative_white_space1");
513 add_kb(group, GEANY_KEYS_INSERT_LINEBEFORE, NULL,
514 0, 0, "edit_insertlinebefore", _("Insert New Line Before Current"), NULL);
515 add_kb(group, GEANY_KEYS_INSERT_LINEAFTER, NULL,
516 0, 0, "edit_insertlineafter", _("Insert New Line After Current"), NULL);
518 group = keybindings_get_core_group(GEANY_KEY_GROUP_SETTINGS);
520 add_kb(group, GEANY_KEYS_SETTINGS_PREFERENCES, cb_func_menu_preferences,
521 GDK_p, GEANY_PRIMARY_MOD_MASK | GDK_MOD1_MASK, "menu_preferences", _("Preferences"),
522 "preferences1");
523 add_kb(group, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES, cb_func_menu_preferences,
524 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), "plugin_preferences1");
526 group = keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH);
528 add_kb(group, GEANY_KEYS_SEARCH_FIND, NULL,
529 GDK_f, GEANY_PRIMARY_MOD_MASK, "menu_find", _("Find"), "find1");
530 add_kb(group, GEANY_KEYS_SEARCH_FINDNEXT, NULL,
531 GDK_g, GEANY_PRIMARY_MOD_MASK, "menu_findnext", _("Find Next"), "find_next1");
532 add_kb(group, GEANY_KEYS_SEARCH_FINDPREVIOUS, NULL,
533 GDK_g, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "menu_findprevious", _("Find Previous"),
534 "find_previous1");
535 add_kb(group, GEANY_KEYS_SEARCH_FINDNEXTSEL, NULL,
536 0, 0, "menu_findnextsel", _("Find Next _Selection"), "find_nextsel1");
537 add_kb(group, GEANY_KEYS_SEARCH_FINDPREVSEL, NULL,
538 0, 0, "menu_findprevsel", _("Find Pre_vious Selection"), "find_prevsel1");
539 add_kb(group, GEANY_KEYS_SEARCH_REPLACE, NULL,
540 GDK_h, GEANY_PRIMARY_MOD_MASK, "menu_replace", _("Replace"), "replace1");
541 add_kb(group, GEANY_KEYS_SEARCH_FINDINFILES, NULL, GDK_f,
542 GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "menu_findinfiles", _("Find in Files"),
543 "find_in_files1");
544 add_kb(group, GEANY_KEYS_SEARCH_NEXTMESSAGE, NULL,
545 0, 0, "menu_nextmessage", _("Next Message"), "next_message1");
546 add_kb(group, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE, NULL,
547 0, 0, "menu_previousmessage", _("Previous Message"), "previous_message1");
548 add_kb(group, GEANY_KEYS_SEARCH_FINDUSAGE, NULL,
549 GDK_e, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "popup_findusage",
550 _("Find Usage"), "find_usage1");
551 add_kb(group, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, NULL,
552 GDK_d, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "popup_finddocumentusage",
553 _("Find Document Usage"), "find_document_usage1");
554 add_kb(group, GEANY_KEYS_SEARCH_MARKALL, NULL,
555 GDK_m, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "find_markall", _("_Mark All"), "mark_all1");
557 group = keybindings_get_core_group(GEANY_KEY_GROUP_GOTO);
559 add_kb(group, GEANY_KEYS_GOTO_BACK, NULL,
560 GDK_Left, GDK_MOD1_MASK, "nav_back", _("Navigate back a location"), NULL);
561 add_kb(group, GEANY_KEYS_GOTO_FORWARD, NULL,
562 GDK_Right, GDK_MOD1_MASK, "nav_forward", _("Navigate forward a location"), NULL);
563 add_kb(group, GEANY_KEYS_GOTO_LINE, NULL,
564 GDK_l, GEANY_PRIMARY_MOD_MASK, "menu_gotoline", _("Go to Line"), "go_to_line1");
565 add_kb(group, GEANY_KEYS_GOTO_MATCHINGBRACE, NULL,
566 GDK_b, GEANY_PRIMARY_MOD_MASK, "edit_gotomatchingbrace",
567 _("Go to matching brace"), NULL);
568 add_kb(group, GEANY_KEYS_GOTO_TOGGLEMARKER, NULL,
569 GDK_m, GEANY_PRIMARY_MOD_MASK, "edit_togglemarker",
570 _("Toggle marker"), NULL);
571 add_kb(group, GEANY_KEYS_GOTO_NEXTMARKER, NULL,
572 GDK_period, GEANY_PRIMARY_MOD_MASK, "edit_gotonextmarker",
573 _("Go to Ne_xt Marker"), "go_to_next_marker1");
574 add_kb(group, GEANY_KEYS_GOTO_PREVIOUSMARKER, NULL,
575 GDK_comma, GEANY_PRIMARY_MOD_MASK, "edit_gotopreviousmarker",
576 _("Go to Pre_vious Marker"), "go_to_previous_marker1");
577 add_kb(group, GEANY_KEYS_GOTO_TAGDEFINITION, NULL,
578 GDK_t, GEANY_PRIMARY_MOD_MASK, "popup_gototagdefinition",
579 _("Go to Symbol Definition"), "goto_tag_definition1");
580 add_kb(group, GEANY_KEYS_GOTO_TAGDECLARATION, NULL,
581 GDK_t, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "popup_gototagdeclaration",
582 _("Go to Symbol Declaration"), "goto_tag_declaration1");
583 add_kb(group, GEANY_KEYS_GOTO_LINESTART, NULL,
584 GDK_Home, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL);
585 add_kb(group, GEANY_KEYS_GOTO_LINEEND, NULL,
586 GDK_End, 0, "edit_gotolineend", _("Go to End of Line"), NULL);
587 add_kb(group, GEANY_KEYS_GOTO_LINESTARTVISUAL, NULL,
588 GDK_Home, GDK_MOD1_MASK, "edit_gotolinestartvisual", _("Go to Start of Display Line"), NULL);
589 add_kb(group, GEANY_KEYS_GOTO_LINEENDVISUAL, NULL,
590 GDK_End, GDK_MOD1_MASK, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL);
591 add_kb(group, GEANY_KEYS_GOTO_PREVWORDPART, NULL,
592 GDK_slash, GEANY_PRIMARY_MOD_MASK, "edit_prevwordstart", _("Go to Previous Word Part"), NULL);
593 add_kb(group, GEANY_KEYS_GOTO_NEXTWORDPART, NULL,
594 GDK_backslash, GEANY_PRIMARY_MOD_MASK, "edit_nextwordstart", _("Go to Next Word Part"), NULL);
596 group = keybindings_get_core_group(GEANY_KEY_GROUP_VIEW);
598 add_kb(group, GEANY_KEYS_VIEW_TOGGLEALL, NULL,
599 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
600 "menu_toggle_all_additional_widgets1");
601 add_kb(group, GEANY_KEYS_VIEW_FULLSCREEN, cb_func_menu_fullscreen,
602 GDK_F11, 0, "menu_fullscreen", _("Fullscreen"), "menu_fullscreen1");
603 add_kb(group, GEANY_KEYS_VIEW_MESSAGEWINDOW, cb_func_menu_messagewindow,
604 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
605 "menu_show_messages_window1");
606 add_kb(group, GEANY_KEYS_VIEW_SIDEBAR, NULL,
607 0, 0, "toggle_sidebar", _("Toggle Sidebar"), "menu_show_sidebar1");
608 add_kb(group, GEANY_KEYS_VIEW_ZOOMIN, NULL,
609 GDK_plus, GEANY_PRIMARY_MOD_MASK, "menu_zoomin", _("Zoom In"), "menu_zoom_in1");
610 add_kb(group, GEANY_KEYS_VIEW_ZOOMOUT, NULL,
611 GDK_minus, GEANY_PRIMARY_MOD_MASK, "menu_zoomout", _("Zoom Out"), "menu_zoom_out1");
612 add_kb(group, GEANY_KEYS_VIEW_ZOOMRESET, NULL,
613 GDK_0, GEANY_PRIMARY_MOD_MASK, "normal_size", _("Zoom Reset"), "normal_size1");
615 group = keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS);
617 add_kb(group, GEANY_KEYS_FOCUS_EDITOR, NULL,
618 GDK_F2, 0, "switch_editor", _("Switch to Editor"), NULL);
619 add_kb(group, GEANY_KEYS_FOCUS_SEARCHBAR, NULL,
620 GDK_F7, 0, "switch_search_bar", _("Switch to Search Bar"), NULL);
621 add_kb(group, GEANY_KEYS_FOCUS_MESSAGE_WINDOW, NULL,
622 0, 0, "switch_message_window", _("Switch to Message Window"), NULL);
623 add_kb(group, GEANY_KEYS_FOCUS_COMPILER, NULL,
624 0, 0, "switch_compiler", _("Switch to Compiler"), NULL);
625 add_kb(group, GEANY_KEYS_FOCUS_MESSAGES, NULL,
626 0, 0, "switch_messages", _("Switch to Messages"), NULL);
627 add_kb(group, GEANY_KEYS_FOCUS_SCRIBBLE, NULL,
628 GDK_F6, 0, "switch_scribble", _("Switch to Scribble"), NULL);
629 add_kb(group, GEANY_KEYS_FOCUS_VTE, NULL,
630 GDK_F4, 0, "switch_vte", _("Switch to VTE"), NULL);
631 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR, NULL,
632 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL);
633 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST, NULL,
634 0, 0, "switch_sidebar_symbol_list", _("Switch to Sidebar Symbol List"), NULL);
635 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST, NULL,
636 0, 0, "switch_sidebar_doc_list", _("Switch to Sidebar Document List"), NULL);
638 group = keybindings_get_core_group(GEANY_KEY_GROUP_NOTEBOOK);
640 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT, cb_func_switch_tableft,
641 GDK_Page_Up, GEANY_PRIMARY_MOD_MASK, "switch_tableft", _("Switch to left document"), NULL);
642 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
643 GDK_Page_Down, GEANY_PRIMARY_MOD_MASK, "switch_tabright", _("Switch to right document"), NULL);
644 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
645 GDK_Tab, GEANY_PRIMARY_MOD_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
646 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
647 GDK_Page_Up, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "move_tableft",
648 _("Move document left"), NULL);
649 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
650 GDK_Page_Down, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "move_tabright",
651 _("Move document right"), NULL);
652 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABFIRST, cb_func_move_tab,
653 0, 0, "move_tabfirst", _("Move document first"), NULL);
654 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABLAST, cb_func_move_tab,
655 0, 0, "move_tablast", _("Move document last"), NULL);
657 group = keybindings_get_core_group(GEANY_KEY_GROUP_DOCUMENT);
659 add_kb(group, GEANY_KEYS_DOCUMENT_LINEWRAP, NULL,
660 0, 0, "menu_linewrap", _("Toggle Line wrapping"), "menu_line_wrapping1");
661 add_kb(group, GEANY_KEYS_DOCUMENT_LINEBREAK, NULL,
662 0, 0, "menu_linebreak", _("Toggle Line breaking"), "line_breaking1");
663 add_kb(group, GEANY_KEYS_DOCUMENT_CLONE, NULL,
664 0, 0, "menu_clone", _("_Clone"), "clone1");
665 add_kb(group, GEANY_KEYS_DOCUMENT_REPLACETABS, NULL,
666 0, 0, "menu_replacetabs", _("Replace tabs with space"), "menu_replace_tabs");
667 add_kb(group, GEANY_KEYS_DOCUMENT_REPLACESPACES, NULL,
668 0, 0, "menu_replacespaces", _("Replace spaces with tabs"), "menu_replace_spaces");
669 add_kb(group, GEANY_KEYS_DOCUMENT_TOGGLEFOLD, NULL,
670 0, 0, "menu_togglefold", _("Toggle current fold"), NULL);
671 add_kb(group, GEANY_KEYS_DOCUMENT_FOLDALL, NULL,
672 0, 0, "menu_foldall", _("Fold all"), "menu_fold_all1");
673 add_kb(group, GEANY_KEYS_DOCUMENT_UNFOLDALL, NULL,
674 0, 0, "menu_unfoldall", _("Unfold all"), "menu_unfold_all1");
675 add_kb(group, GEANY_KEYS_DOCUMENT_RELOADTAGLIST, NULL,
676 GDK_r, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "reloadtaglist", _("Reload symbol list"), NULL);
677 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS, NULL,
678 0, 0, "remove_markers", _("Remove Markers"), "remove_markers1");
679 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS, NULL,
680 0, 0, "remove_error_indicators", _("Remove Error Indicators"), "menu_remove_indicators1");
681 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS, NULL,
682 0, 0, "remove_markers_and_indicators", _("Remove Markers and Error Indicators"), NULL);
684 group = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
686 add_kb(group, GEANY_KEYS_BUILD_COMPILE, NULL,
687 GDK_F8, 0, "build_compile", _("Compile"), NULL);
688 add_kb(group, GEANY_KEYS_BUILD_LINK, NULL,
689 GDK_F9, 0, "build_link", _("Build"), NULL);
690 add_kb(group, GEANY_KEYS_BUILD_MAKE, NULL,
691 GDK_F9, GDK_SHIFT_MASK, "build_make", _("Make all"), NULL);
692 add_kb(group, GEANY_KEYS_BUILD_MAKEOWNTARGET, NULL,
693 GDK_F9, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "build_makeowntarget",
694 _("Make custom target"), NULL);
695 add_kb(group, GEANY_KEYS_BUILD_MAKEOBJECT, NULL,
696 GDK_F8, GDK_SHIFT_MASK, "build_makeobject", _("Make object"), NULL);
697 add_kb(group, GEANY_KEYS_BUILD_NEXTERROR, NULL,
698 0, 0, "build_nexterror", _("Next error"), NULL);
699 add_kb(group, GEANY_KEYS_BUILD_PREVIOUSERROR, NULL,
700 0, 0, "build_previouserror", _("Previous error"), NULL);
701 add_kb(group, GEANY_KEYS_BUILD_RUN, NULL,
702 GDK_F5, 0, "build_run", _("Run"), NULL);
703 add_kb(group, GEANY_KEYS_BUILD_OPTIONS, NULL,
704 0, 0, "build_options", _("Build options"), NULL);
706 group = keybindings_get_core_group(GEANY_KEY_GROUP_TOOLS);
708 add_kb(group, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER, cb_func_menu_opencolorchooser,
709 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), "menu_choose_color1");
711 group = keybindings_get_core_group(GEANY_KEY_GROUP_HELP);
713 add_kb(group, GEANY_KEYS_HELP_HELP, cb_func_menu_help,
714 GDK_F1, 0, "menu_help", _("Help"), "help1");
718 static void free_key_group(gpointer item)
720 GeanyKeyGroup *group = item;
722 g_ptr_array_free(group->key_items, TRUE);
724 if (group->plugin)
726 if (group->cb_data_destroy)
727 group->cb_data_destroy(group->cb_data);
728 g_free(group->plugin_keys);
729 /* we allocated those in add_kb_group() as it's a plugin group */
730 g_free((gchar *) group->name);
731 g_free((gchar *) group->label);
732 g_free(group);
737 void keybindings_init(void)
739 memset(binding_ids, 0, sizeof binding_ids);
740 keybinding_groups = g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT);
741 g_ptr_array_set_free_func(keybinding_groups, free_key_group);
742 kb_accel_group = gtk_accel_group_new();
744 init_default_kb();
745 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), kb_accel_group);
747 g_signal_connect(main_widgets.window, "key-press-event", G_CALLBACK(on_key_press_event), NULL);
751 typedef void (*KBItemCallback) (GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data);
753 static void keybindings_foreach(KBItemCallback cb, gpointer user_data)
755 gsize g, i;
756 GeanyKeyGroup *group;
757 GeanyKeyBinding *kb;
759 foreach_ptr_array(group, g, keybinding_groups)
761 foreach_ptr_array(kb, i, group->key_items)
762 cb(group, kb, user_data);
767 static void load_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
769 GKeyFile *config = user_data;
770 gchar *val;
771 guint key;
772 GdkModifierType mods;
774 val = g_key_file_get_string(config, group->name, kb->name, NULL);
775 if (val != NULL)
777 gtk_accelerator_parse(val, &key, &mods);
778 kb->key = key;
779 kb->mods = mods;
780 g_free(val);
785 static void load_user_kb(void)
787 gchar *configfile = g_build_filename(app->configdir, "keybindings.conf", NULL);
788 GKeyFile *config = g_key_file_new();
790 /* backwards compatibility with Geany 0.21 defaults */
791 if (!g_file_test(configfile, G_FILE_TEST_EXISTS))
793 gchar *geanyconf = g_build_filename(app->configdir, "geany.conf", NULL);
794 const gchar data[] = "[Bindings]\n"
795 "popup_gototagdefinition=\n"
796 "edit_transposeline=<Control>t\n"
797 "edit_movelineup=\n"
798 "edit_movelinedown=\n"
799 "move_tableft=<Alt>Page_Up\n"
800 "move_tabright=<Alt>Page_Down\n";
802 utils_write_file(configfile, g_file_test(geanyconf, G_FILE_TEST_EXISTS) ?
803 data : "");
804 g_free(geanyconf);
807 /* now load user defined keys */
808 if (g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
810 keybindings_foreach(load_kb, config);
812 g_free(configfile);
813 g_key_file_free(config);
817 static void apply_kb_accel(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
819 if (kb->key != 0 && kb->menu_item)
821 gtk_widget_add_accelerator(kb->menu_item, "activate", kb_accel_group,
822 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
827 /** Reloads keybinding settings from configuration file.
829 * Normally plugins do not need to call this function as it is called automatically when a
830 * the plugin is activated. However, plugins which need to create keybindings dynamically
831 * and reload them when needed should call this function after all keybindings have been
832 * updated with plugin_set_key_group() and keybindings_set_item() calls - this makes sure
833 * that the corresponding user keybinding shortcuts are applied.
835 * @since 1.32 (API 233) */
836 GEANY_API_SYMBOL
837 void keybindings_load_keyfile(void)
839 load_user_kb();
840 add_popup_menu_accels();
842 /* set menu accels now, after user keybindings have been read */
843 keybindings_foreach(apply_kb_accel, NULL);
847 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id, GtkWidget *menuitem)
849 GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
851 if (kb->key != 0)
852 gtk_widget_add_accelerator(menuitem, "activate", kb_accel_group,
853 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
857 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
858 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
860 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
861 /* FIXME: update those during runtime */
862 static void add_popup_menu_accels(void)
864 GeanyKeyGroup *group;
866 group = keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR);
867 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO, undo1);
868 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO, redo1);
869 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION, context_action1);
871 group = keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD);
872 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_CUT, cut1);
873 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_COPY, copy1);
874 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_PASTE, paste1);
876 group = keybindings_get_core_group(GEANY_KEY_GROUP_SELECT);
877 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL, menu_select_all2);
879 group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
880 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE, insert_date_custom2);
881 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_ALTWHITESPACE, insert_alternative_white_space2);
883 group = keybindings_get_core_group(GEANY_KEY_GROUP_FILE);
884 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED, menu_open_selected_file2);
886 group = keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH);
887 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE, find_usage2);
888 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, find_document_usage2);
890 group = keybindings_get_core_group(GEANY_KEY_GROUP_GOTO);
891 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION, goto_tag_definition2);
893 /* Format and Commands share the menu bar submenus */
894 /* Build menu items are set if the build menus are created */
898 static void set_keyfile_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
900 GKeyFile *config = user_data;
901 gchar *val;
903 val = gtk_accelerator_name(kb->key, kb->mods);
904 g_key_file_set_string(config, group->name, kb->name, val);
905 g_free(val);
909 /* just write the content of the keys array to the config file */
910 void keybindings_write_to_file(void)
912 gchar *configfile = g_build_filename(app->configdir, "keybindings.conf", NULL);
913 gchar *data;
914 GKeyFile *config = g_key_file_new();
916 g_key_file_load_from_file(config, configfile, 0, NULL);
917 keybindings_foreach(set_keyfile_kb, config);
919 /* write the file */
920 data = g_key_file_to_data(config, NULL, NULL);
921 utils_write_file(configfile, data);
923 g_free(data);
924 g_free(configfile);
925 g_key_file_free(config);
929 void keybindings_free(void)
931 GeanyKeyGroup *group;
932 gsize g;
934 foreach_ptr_array(group, g, keybinding_groups)
935 keybindings_free_group(group);
937 g_ptr_array_free(keybinding_groups, TRUE);
941 gchar *keybindings_get_label(GeanyKeyBinding *kb)
943 return utils_str_remove_chars(g_strdup(kb->label), "_");
947 static void fill_shortcut_labels_treeview(GtkWidget *tree)
949 gsize g, i;
950 GeanyKeyBinding *kb;
951 GeanyKeyGroup *group;
952 GtkListStore *store;
953 GtkTreeIter iter;
955 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, PANGO_TYPE_WEIGHT);
957 foreach_ptr_array(group, g, keybinding_groups)
959 if (g > 0)
961 gtk_list_store_append(store, &iter);
962 gtk_list_store_set(store, &iter, -1);
964 gtk_list_store_append(store, &iter);
965 gtk_list_store_set(store, &iter, 0, group->label, 2, PANGO_WEIGHT_BOLD, -1);
967 foreach_ptr_array(kb, i, group->key_items)
969 gchar *shortcut, *label;
971 label = keybindings_get_label(kb);
972 shortcut = gtk_accelerator_get_label(kb->key, kb->mods);
974 gtk_list_store_append(store, &iter);
975 gtk_list_store_set(store, &iter, 0, label, 1, shortcut, 2, PANGO_WEIGHT_NORMAL, -1);
977 g_free(shortcut);
978 g_free(label);
981 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
982 g_object_unref(store);
986 static GtkWidget *create_dialog(void)
988 GtkWidget *dialog, *tree, *label, *swin, *vbox;
989 GtkCellRenderer *text_renderer;
990 GtkTreeViewColumn *column;
992 dialog = gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets.window),
993 GTK_DIALOG_DESTROY_WITH_PARENT,
994 GTK_STOCK_EDIT, GTK_RESPONSE_APPLY,
995 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
996 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
997 gtk_box_set_spacing(GTK_BOX(vbox), 6);
998 gtk_widget_set_name(dialog, "GeanyDialog");
1000 gtk_window_set_default_size(GTK_WINDOW(dialog), -1, GEANY_DEFAULT_DIALOG_HEIGHT);
1002 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
1004 label = gtk_label_new(_("The following keyboard shortcuts are configurable:"));
1005 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
1007 tree = gtk_tree_view_new();
1008 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
1009 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
1011 text_renderer = gtk_cell_renderer_text_new();
1012 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
1013 column = gtk_tree_view_column_new_with_attributes(
1014 NULL, text_renderer, "text", 0, "weight", 2, NULL);
1015 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1017 text_renderer = gtk_cell_renderer_text_new();
1018 column = gtk_tree_view_column_new_with_attributes(NULL, text_renderer, "text", 1, NULL);
1019 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
1021 fill_shortcut_labels_treeview(tree);
1023 swin = gtk_scrolled_window_new(NULL, NULL);
1024 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_NEVER,
1025 GTK_POLICY_AUTOMATIC);
1026 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
1027 gtk_container_add(GTK_CONTAINER(swin), tree);
1029 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
1030 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
1032 return dialog;
1036 static void key_dialog_show_prefs(void)
1038 GtkWidget *wid;
1040 prefs_show_dialog();
1041 /* select the KB page */
1042 wid = ui_lookup_widget(ui_widgets.prefs_dialog, "frame22");
1043 if (wid != NULL)
1045 GtkNotebook *nb = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
1046 if (nb != NULL)
1048 gtk_notebook_set_current_page(nb, gtk_notebook_page_num(nb, wid));
1054 void keybindings_dialog_show_prefs_scroll(const gchar *name)
1056 key_dialog_show_prefs();
1057 prefs_kb_search_name(name);
1061 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
1062 static GtkWidget *key_dialog = NULL;
1064 static void on_dialog_response(GtkWidget *dialog, gint response, gpointer user_data)
1066 if (response == GTK_RESPONSE_APPLY)
1068 key_dialog_show_prefs();
1070 gtk_widget_destroy(dialog);
1071 key_dialog = NULL;
1075 void keybindings_show_shortcuts(void)
1077 if (key_dialog)
1078 gtk_widget_destroy(key_dialog); /* in case the key_dialog is still visible */
1080 key_dialog = create_dialog();
1081 g_signal_connect(key_dialog, "response", G_CALLBACK(on_dialog_response), NULL);
1082 gtk_widget_show_all(key_dialog);
1086 static gboolean check_fixed_kb(guint keyval, guint state)
1088 /* check alt-0 to alt-9 for setting current notebook page */
1089 if (state == GDK_MOD1_MASK && keyval >= GDK_0 && keyval <= GDK_9)
1091 gint page = keyval - GDK_0 - 1;
1092 gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1094 /* alt-0 is for the rightmost tab */
1095 if (keyval == GDK_0)
1096 page = npages - 1;
1097 /* invert the order if tabs are added on the other side */
1098 if (swap_alt_tab_order && ! file_prefs.tab_order_ltr)
1099 page = (npages - 1) - page;
1101 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
1102 return TRUE;
1104 /* note: these are now overridden by default with move tab bindings */
1105 if (keyval == GDK_Page_Up || keyval == GDK_Page_Down)
1107 /* switch to first or last document */
1108 if (state == (GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK))
1110 if (keyval == GDK_Page_Up)
1111 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), 0);
1112 if (keyval == GDK_Page_Down)
1113 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), -1);
1114 return TRUE;
1117 return FALSE;
1121 static gboolean check_snippet_completion(GeanyDocument *doc)
1123 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1125 g_return_val_if_fail(doc, FALSE);
1127 /* keybinding only valid when scintilla widget has focus */
1128 if (focusw == GTK_WIDGET(doc->editor->sci))
1130 ScintillaObject *sci = doc->editor->sci;
1131 gint pos = sci_get_current_position(sci);
1133 if (editor_prefs.complete_snippets)
1134 return editor_complete_snippet(doc->editor, pos);
1136 return FALSE;
1140 /* Transforms a GdkEventKey event into a GdkEventButton event */
1141 static void trigger_button_event(GtkWidget *widget, guint32 event_time)
1143 GdkEventButton *event;
1144 gboolean ret;
1146 event = g_new0(GdkEventButton, 1);
1148 if (GTK_IS_TEXT_VIEW(widget))
1149 event->window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT);
1150 else
1151 event->window = gtk_widget_get_window(widget);
1152 event->time = event_time;
1153 event->type = GDK_BUTTON_PRESS;
1154 event->button = 3;
1156 g_signal_emit_by_name(widget, "button-press-event", event, &ret);
1157 g_signal_emit_by_name(widget, "button-release-event", event, &ret);
1159 g_free(event);
1163 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
1164 * widgets. Without this special handling, the notebook tab list of the documents' notebook
1165 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
1166 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1167 * notebook tab list. */
1168 static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
1170 g_return_val_if_fail(doc == NULL || doc->is_valid, FALSE);
1172 if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
1174 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1175 if (doc != NULL)
1177 if (focusw == doc->priv->tag_tree)
1179 trigger_button_event(focusw, event_time);
1180 return TRUE;
1182 if (focusw == GTK_WIDGET(doc->editor->sci))
1184 if (keyval == GDK_Menu)
1185 { /* show editor popup menu */
1186 trigger_button_event(focusw, event_time);
1187 return TRUE;
1189 else
1190 { /* show tab bar menu */
1191 trigger_button_event(main_widgets.notebook, event_time);
1192 return TRUE;
1196 if (focusw == tv.tree_openfiles
1197 || focusw == msgwindow.tree_status
1198 || focusw == msgwindow.tree_compiler
1199 || focusw == msgwindow.tree_msg
1200 || focusw == msgwindow.scribble
1201 #ifdef HAVE_VTE
1202 || (vte_info.have_vte && focusw == vc->vte)
1203 #endif
1206 trigger_button_event(focusw, event_time);
1207 return TRUE;
1210 return FALSE;
1214 #ifdef HAVE_VTE
1215 static gboolean set_sensitive(gpointer widget)
1217 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1218 return FALSE;
1222 static gboolean check_vte(GdkModifierType state, guint keyval)
1224 guint i;
1225 GeanyKeyBinding *kb;
1226 GeanyKeyGroup *group;
1227 GtkWidget *widget;
1229 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != vc->vte)
1230 return FALSE;
1231 /* let VTE copy/paste override any user keybinding */
1232 if (state == (GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK) && (keyval == GDK_c || keyval == GDK_v))
1233 return TRUE;
1234 if (! vc->enable_bash_keys)
1235 return FALSE;
1236 /* prevent menubar flickering: */
1237 if (state == GDK_SHIFT_MASK && (keyval >= GDK_a && keyval <= GDK_z))
1238 return FALSE;
1239 if (state == 0 && (keyval < GDK_F1 || keyval > GDK_F35)) /* e.g. backspace */
1240 return FALSE;
1242 /* make focus commands override any bash commands */
1243 group = keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS);
1244 foreach_ptr_array(kb, i, group->key_items)
1246 if (state == kb->mods && keyval == kb->key)
1247 return FALSE;
1250 /* Temporarily disable the menus to prevent conflicting menu accelerators
1251 * from overriding the VTE bash shortcuts.
1252 * Note: maybe there's a better way of doing this ;-) */
1253 widget = ui_lookup_widget(main_widgets.window, "menubar1");
1254 gtk_widget_set_sensitive(widget, FALSE);
1255 g_idle_add_full(G_PRIORITY_HIGH, set_sensitive, widget, NULL);
1257 widget = main_widgets.editor_menu;
1258 gtk_widget_set_sensitive(widget, FALSE);
1259 g_idle_add(set_sensitive, widget);
1260 return TRUE;
1262 #endif
1265 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1266 static guint key_kp_translate(guint key_in)
1268 switch (key_in)
1270 case GDK_KP_Down:
1271 return GDK_Down;
1272 case GDK_KP_Up:
1273 return GDK_Up;
1274 case GDK_KP_Left:
1275 return GDK_Left;
1276 case GDK_KP_Right:
1277 return GDK_Right;
1278 case GDK_KP_Home:
1279 return GDK_Home;
1280 case GDK_KP_End:
1281 return GDK_End;
1282 case GDK_KP_Page_Up:
1283 return GDK_Page_Up;
1284 case GDK_KP_Page_Down:
1285 return GDK_Page_Down;
1286 case GDK_KP_Delete:
1287 return GDK_Delete;
1288 case GDK_KP_Insert:
1289 return GDK_Insert;
1290 default:
1291 return key_in;
1296 /* Check if event keypress matches keybinding combo */
1297 gboolean keybindings_check_event(GdkEventKey *ev, GeanyKeyBinding *kb)
1299 guint state, keyval;
1301 if (ev->keyval == 0)
1302 return FALSE;
1304 keyval = ev->keyval;
1305 state = keybindings_get_modifiers(ev->state);
1306 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1307 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1308 if (keyval >= GDK_A && keyval <= GDK_Z)
1309 keyval += GDK_a - GDK_A;
1311 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1312 keyval = key_kp_translate(keyval);
1314 return (keyval == kb->key && state == kb->mods);
1318 static gboolean run_kb(GeanyKeyBinding *kb, GeanyKeyGroup *group)
1320 gboolean handled = TRUE;
1321 /* call the corresponding handler/callback functions for this shortcut.
1322 * Check the individual keybindings first (handler first, callback second) and
1323 * group second (again handler first, callback second) */
1324 if (kb->cb_func)
1325 handled = kb->cb_func(kb, kb->id, kb->cb_data);
1326 else if (kb->callback)
1327 kb->callback(kb->id);
1328 else if (group->cb_func)
1329 handled = group->cb_func(group, kb->id, group->cb_data);
1330 else if (group->callback)
1331 handled = group->callback(kb->id);
1332 else
1334 g_warning("No callback or handler for keybinding %s: %s!", group->name, kb->name);
1335 return FALSE;
1338 return handled;
1342 /* central keypress event handler, almost all keypress events go to this function */
1343 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1345 guint state, keyval;
1346 gsize g, i;
1347 GeanyDocument *doc;
1348 GeanyKeyGroup *group;
1349 GeanyKeyBinding *kb;
1351 if (ev->keyval == 0)
1352 return FALSE;
1354 doc = document_get_current();
1355 if (doc)
1356 document_check_disk_status(doc, FALSE);
1358 keyval = ev->keyval;
1359 state = keybindings_get_modifiers(ev->state);
1360 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1361 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1362 if (keyval >= GDK_A && keyval <= GDK_Z)
1363 keyval += GDK_a - GDK_A;
1365 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1366 keyval = key_kp_translate(keyval);
1368 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1370 /* special cases */
1371 #ifdef HAVE_VTE
1372 if (vte_info.have_vte && check_vte(state, keyval))
1373 return FALSE;
1374 #endif
1375 if (check_menu_key(doc, keyval, state, ev->time))
1376 return TRUE;
1378 foreach_ptr_array(group, g, keybinding_groups)
1380 foreach_ptr_array(kb, i, group->key_items)
1382 if (keyval == kb->key && state == kb->mods)
1384 if (run_kb(kb, group))
1385 return TRUE;
1389 /* fixed keybindings can be overridden by user bindings, so check them last */
1390 if (check_fixed_kb(keyval, state))
1391 return TRUE;
1392 return FALSE;
1396 /* group_id must be a core group, e.g. GEANY_KEY_GROUP_EDITOR
1397 * key_id e.g. GEANY_KEYS_EDITOR_CALLTIP */
1398 GeanyKeyBinding *keybindings_lookup_item(guint group_id, guint key_id)
1400 GeanyKeyGroup *group;
1402 g_return_val_if_fail(group_id < GEANY_KEY_GROUP_COUNT, NULL); /* can't use this for plugin groups */
1404 group = keybindings_get_core_group(group_id);
1406 g_return_val_if_fail(group, NULL);
1407 return keybindings_get_item(group, key_id);
1411 /** Mimics a (built-in only) keybinding action.
1412 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1413 * @param group_id @ref GeanyKeyGroupID keybinding group index that contains the @a key_id keybinding.
1414 * @param key_id @ref GeanyKeyBindingID keybinding index. */
1415 GEANY_API_SYMBOL
1416 void keybindings_send_command(guint group_id, guint key_id)
1418 GeanyKeyBinding *kb;
1419 GeanyKeyGroup *group;
1421 kb = keybindings_lookup_item(group_id, key_id);
1422 group = keybindings_get_core_group(group_id);
1423 if (kb && group)
1424 run_kb(kb, group);
1428 /* These are the callback functions, either each group or each shortcut has it's
1429 * own function. */
1432 static gboolean cb_func_file_action(guint key_id)
1434 switch (key_id)
1436 case GEANY_KEYS_FILE_NEW:
1437 document_new_file(NULL, NULL, NULL);
1438 break;
1439 case GEANY_KEYS_FILE_OPEN:
1440 on_open1_activate(NULL, NULL);
1441 break;
1442 case GEANY_KEYS_FILE_OPENSELECTED:
1443 on_menu_open_selected_file1_activate(NULL, NULL);
1444 break;
1445 case GEANY_KEYS_FILE_OPENLASTTAB:
1447 gchar *utf8_filename = g_queue_peek_head(ui_prefs.recent_queue);
1448 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1449 document_open_file(locale_filename, FALSE, NULL, NULL);
1450 g_free(locale_filename);
1451 break;
1453 case GEANY_KEYS_FILE_SAVE:
1454 on_save1_activate(NULL, NULL);
1455 break;
1456 case GEANY_KEYS_FILE_SAVEAS:
1457 on_save_as1_activate(NULL, NULL);
1458 break;
1459 case GEANY_KEYS_FILE_SAVEALL:
1460 on_save_all1_activate(NULL, NULL);
1461 break;
1462 case GEANY_KEYS_FILE_CLOSE:
1463 on_close1_activate(NULL, NULL);
1464 break;
1465 case GEANY_KEYS_FILE_CLOSEALL:
1466 on_close_all1_activate(NULL, NULL);
1467 break;
1468 case GEANY_KEYS_FILE_RELOAD:
1469 on_toolbutton_reload_clicked(NULL, NULL);
1470 break;
1471 case GEANY_KEYS_FILE_PRINT:
1472 on_print1_activate(NULL, NULL);
1473 break;
1474 case GEANY_KEYS_FILE_PROPERTIES:
1475 on_file_properties_activate(NULL, NULL);
1476 break;
1477 case GEANY_KEYS_FILE_QUIT:
1478 main_quit();
1479 break;
1481 return TRUE;
1485 static gboolean cb_func_project_action(guint key_id)
1487 switch (key_id)
1489 case GEANY_KEYS_PROJECT_NEW:
1490 on_project_new1_activate(NULL, NULL);
1491 break;
1492 case GEANY_KEYS_PROJECT_OPEN:
1493 on_project_open1_activate(NULL, NULL);
1494 break;
1495 case GEANY_KEYS_PROJECT_CLOSE:
1496 if (app->project)
1497 on_project_close1_activate(NULL, NULL);
1498 break;
1499 case GEANY_KEYS_PROJECT_PROPERTIES:
1500 if (app->project)
1501 on_project_properties1_activate(NULL, NULL);
1502 break;
1504 return TRUE;
1508 static void cb_func_menu_preferences(guint key_id)
1510 switch (key_id)
1512 case GEANY_KEYS_SETTINGS_PREFERENCES:
1513 on_preferences1_activate(NULL, NULL);
1514 break;
1515 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES:
1516 on_plugin_preferences1_activate(NULL, NULL);
1517 break;
1522 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id)
1524 on_help1_activate(NULL, NULL);
1528 static gboolean cb_func_search_action(guint key_id)
1530 GeanyDocument *doc = document_get_current();
1531 ScintillaObject *sci;
1533 /* these work without docs */
1534 switch (key_id)
1536 case GEANY_KEYS_SEARCH_FINDINFILES:
1537 on_find_in_files1_activate(NULL, NULL); return TRUE;
1538 case GEANY_KEYS_SEARCH_NEXTMESSAGE:
1539 on_next_message1_activate(NULL, NULL); return TRUE;
1540 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE:
1541 on_previous_message1_activate(NULL, NULL); return TRUE;
1543 if (!doc)
1544 return TRUE;
1545 sci = doc->editor->sci;
1547 switch (key_id)
1549 case GEANY_KEYS_SEARCH_FIND:
1550 on_find1_activate(NULL, NULL); break;
1551 case GEANY_KEYS_SEARCH_FINDNEXT:
1552 on_find_next1_activate(NULL, NULL); break;
1553 case GEANY_KEYS_SEARCH_FINDPREVIOUS:
1554 on_find_previous1_activate(NULL, NULL); break;
1555 case GEANY_KEYS_SEARCH_FINDPREVSEL:
1556 on_find_prevsel1_activate(NULL, NULL); break;
1557 case GEANY_KEYS_SEARCH_FINDNEXTSEL:
1558 on_find_nextsel1_activate(NULL, NULL); break;
1559 case GEANY_KEYS_SEARCH_REPLACE:
1560 on_replace1_activate(NULL, NULL); break;
1561 case GEANY_KEYS_SEARCH_FINDUSAGE:
1562 on_find_usage1_activate(NULL, NULL); break;
1563 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE:
1564 on_find_document_usage1_activate(NULL, NULL); break;
1565 case GEANY_KEYS_SEARCH_MARKALL:
1567 gchar *text = NULL;
1568 gint pos = sci_get_current_position(sci);
1570 /* clear existing search indicators instead if next to cursor */
1571 if (SSM(sci, SCI_INDICATORVALUEAT,
1572 GEANY_INDICATOR_SEARCH, pos) ||
1573 SSM(sci, SCI_INDICATORVALUEAT,
1574 GEANY_INDICATOR_SEARCH, MAX(pos - 1, 0)))
1576 text = NULL;
1578 else
1580 text = get_current_word_or_sel(doc, TRUE);
1583 if (sci_has_selection(sci))
1584 search_mark_all(doc, text, GEANY_FIND_MATCHCASE);
1585 else
1586 search_mark_all(doc, text, GEANY_FIND_MATCHCASE | GEANY_FIND_WHOLEWORD);
1588 g_free(text);
1589 break;
1592 return TRUE;
1596 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id)
1598 on_show_color_chooser1_activate(NULL, NULL);
1602 static gboolean cb_func_view_action(guint key_id)
1604 switch (key_id)
1606 case GEANY_KEYS_VIEW_TOGGLEALL:
1607 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
1608 break;
1609 case GEANY_KEYS_VIEW_SIDEBAR:
1610 on_menu_show_sidebar1_toggled(NULL, NULL);
1611 break;
1612 case GEANY_KEYS_VIEW_ZOOMIN:
1613 on_zoom_in1_activate(NULL, NULL);
1614 break;
1615 case GEANY_KEYS_VIEW_ZOOMOUT:
1616 on_zoom_out1_activate(NULL, NULL);
1617 break;
1618 case GEANY_KEYS_VIEW_ZOOMRESET:
1619 on_normal_size1_activate(NULL, NULL);
1620 break;
1621 default:
1622 break;
1624 return TRUE;
1628 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id)
1630 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1631 ui_lookup_widget(main_widgets.window, "menu_fullscreen1"));
1633 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1637 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id)
1639 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1640 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1642 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1646 static gboolean cb_func_build_action(guint key_id)
1648 GtkWidget *item;
1649 BuildMenuItems *menu_items;
1650 GeanyDocument *doc = document_get_current();
1652 if (doc == NULL)
1653 return TRUE;
1655 if (!gtk_widget_is_sensitive(ui_lookup_widget(main_widgets.window, "menu_build1")))
1656 return TRUE;
1658 menu_items = build_get_menu_items(doc->file_type->id);
1659 /* TODO make it a table??*/
1660 switch (key_id)
1662 case GEANY_KEYS_BUILD_COMPILE:
1663 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_COMPILE)];
1664 break;
1665 case GEANY_KEYS_BUILD_LINK:
1666 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_BUILD)];
1667 break;
1668 case GEANY_KEYS_BUILD_MAKE:
1669 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_ALL)];
1670 break;
1671 case GEANY_KEYS_BUILD_MAKEOWNTARGET:
1672 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_CUSTOM)];
1673 break;
1674 case GEANY_KEYS_BUILD_MAKEOBJECT:
1675 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)];
1676 break;
1677 case GEANY_KEYS_BUILD_NEXTERROR:
1678 item = menu_items->menu_item[GBG_FIXED][GBF_NEXT_ERROR];
1679 break;
1680 case GEANY_KEYS_BUILD_PREVIOUSERROR:
1681 item = menu_items->menu_item[GBG_FIXED][GBF_PREV_ERROR];
1682 break;
1683 case GEANY_KEYS_BUILD_RUN:
1684 item = menu_items->menu_item[GEANY_GBG_EXEC][GBO_TO_CMD(GEANY_GBO_EXEC)];
1685 break;
1686 case GEANY_KEYS_BUILD_OPTIONS:
1687 item = menu_items->menu_item[GBG_FIXED][GBF_COMMANDS];
1688 break;
1689 default:
1690 item = NULL;
1692 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1693 * sensitive state, but some other menus don't update the sensitive status until
1694 * they are redrawn. */
1695 if (item && gtk_widget_is_sensitive(item))
1696 gtk_menu_item_activate(GTK_MENU_ITEM(item));
1697 return TRUE;
1701 static gboolean read_current_word(GeanyDocument *doc, gboolean sci_word)
1703 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1705 if (sci_word)
1707 editor_find_current_word_sciwc(doc->editor, -1,
1708 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
1710 else
1712 editor_find_current_word(doc->editor, -1,
1713 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1716 return (*editor_info.current_word != 0);
1720 static gboolean check_current_word(GeanyDocument *doc, gboolean sci_word)
1722 if (! read_current_word(doc, sci_word))
1724 utils_beep();
1725 return FALSE;
1727 return TRUE;
1731 static gchar *get_current_word_or_sel(GeanyDocument *doc, gboolean sci_word)
1733 ScintillaObject *sci = doc->editor->sci;
1735 if (sci_has_selection(sci))
1736 return sci_get_selection_contents(sci);
1738 return read_current_word(doc, sci_word) ? g_strdup(editor_info.current_word) : NULL;
1742 static void focus_sidebar(void)
1744 if (ui_prefs.sidebar_visible)
1746 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook));
1747 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), page_num);
1749 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1750 gtk_widget_child_focus(page, GTK_DIR_TAB_FORWARD);
1755 static GtkWidget *find_focus_widget(GtkWidget *widget)
1757 GtkWidget *focus = NULL;
1759 if (GTK_IS_BIN(widget)) /* optimized simple case */
1760 focus = find_focus_widget(gtk_bin_get_child(GTK_BIN(widget)));
1761 else if (GTK_IS_CONTAINER(widget))
1763 GList *children = gtk_container_get_children(GTK_CONTAINER(widget));
1764 GList *node;
1766 for (node = children; node && ! focus; node = node->next)
1767 focus = find_focus_widget(node->data);
1768 g_list_free(children);
1771 /* Some containers handled above might not have children and be what we want to focus
1772 * (e.g. GtkTreeView), so focus that if possible and we don't have anything better */
1773 if (! focus && gtk_widget_get_can_focus(widget))
1774 focus = widget;
1776 return focus;
1780 static void focus_msgwindow(void)
1782 if (ui_prefs.msgwindow_visible)
1784 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook));
1785 GtkWidget *widget = gtk_notebook_get_nth_page(GTK_NOTEBOOK(msgwindow.notebook), page_num);
1787 widget = find_focus_widget(widget);
1788 if (widget)
1789 gtk_widget_grab_focus(widget);
1790 else
1791 utils_beep();
1796 static gboolean cb_func_switch_action(guint key_id)
1798 switch (key_id)
1800 case GEANY_KEYS_FOCUS_EDITOR:
1802 GeanyDocument *doc = document_get_current();
1803 if (doc != NULL)
1805 GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
1806 if (gtk_widget_has_focus(sci))
1807 ui_update_statusbar(doc, -1);
1808 else
1809 gtk_widget_grab_focus(sci);
1811 break;
1813 case GEANY_KEYS_FOCUS_SCRIBBLE:
1814 msgwin_switch_tab(MSG_SCRATCH, TRUE);
1815 break;
1816 case GEANY_KEYS_FOCUS_SEARCHBAR:
1817 if (toolbar_prefs.visible)
1819 GtkWidget *search_entry = toolbar_get_widget_child_by_name("SearchEntry");
1820 if (search_entry != NULL)
1821 gtk_widget_grab_focus(search_entry);
1823 break;
1824 case GEANY_KEYS_FOCUS_SIDEBAR:
1825 focus_sidebar();
1826 break;
1827 case GEANY_KEYS_FOCUS_VTE:
1828 msgwin_switch_tab(MSG_VTE, TRUE);
1829 break;
1830 case GEANY_KEYS_FOCUS_COMPILER:
1831 msgwin_switch_tab(MSG_COMPILER, TRUE);
1832 break;
1833 case GEANY_KEYS_FOCUS_MESSAGES:
1834 msgwin_switch_tab(MSG_MESSAGE, TRUE);
1835 break;
1836 case GEANY_KEYS_FOCUS_MESSAGE_WINDOW:
1837 focus_msgwindow();
1838 break;
1839 case GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST:
1840 sidebar_focus_openfiles_tab();
1841 break;
1842 case GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST:
1843 sidebar_focus_symbols_tab();
1844 break;
1846 return TRUE;
1850 static void switch_notebook_page(gint direction)
1852 gint page_count, cur_page, pass;
1853 gboolean parent_is_notebook = FALSE;
1854 GtkNotebook *notebook;
1855 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1857 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1860 parent_is_notebook = GTK_IS_NOTEBOOK(focusw);
1862 while (! parent_is_notebook && (focusw = gtk_widget_get_parent(focusw)) != NULL);
1864 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1865 if (parent_is_notebook)
1866 notebook = GTK_NOTEBOOK(focusw);
1867 else
1868 notebook = GTK_NOTEBOOK(main_widgets.notebook);
1870 /* now switch pages */
1871 page_count = gtk_notebook_get_n_pages(notebook);
1872 cur_page = gtk_notebook_get_current_page(notebook);
1874 /* find the next visible page in the wanted direction, but don't loop
1875 * indefinitely if no pages are visible */
1876 for (pass = 0; pass < page_count; pass++)
1878 GtkWidget *child;
1880 if (direction == GTK_DIR_LEFT)
1882 if (cur_page > 0)
1883 cur_page--;
1884 else
1885 cur_page = page_count - 1;
1887 else if (direction == GTK_DIR_RIGHT)
1889 if (cur_page < page_count - 1)
1890 cur_page++;
1891 else
1892 cur_page = 0;
1895 child = gtk_notebook_get_nth_page (notebook, cur_page);
1896 if (gtk_widget_get_visible (child))
1898 gtk_notebook_set_current_page(notebook, cur_page);
1899 break;
1905 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id)
1907 switch_notebook_page(GTK_DIR_LEFT);
1911 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id)
1913 switch_notebook_page(GTK_DIR_RIGHT);
1917 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id)
1919 notebook_switch_tablastused();
1923 /* move document left/right/first/last */
1924 static void cb_func_move_tab(guint key_id)
1926 GtkWidget *child;
1927 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
1928 gint cur_page = gtk_notebook_get_current_page(nb);
1930 if (cur_page < 0)
1931 return;
1933 child = gtk_notebook_get_nth_page(nb, cur_page);
1935 switch (key_id)
1937 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT:
1938 gtk_notebook_reorder_child(nb, child, cur_page - 1); /* notebook wraps around by default */
1939 break;
1940 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT:
1942 gint npage = cur_page + 1;
1944 if (npage == gtk_notebook_get_n_pages(nb))
1945 npage = 0; /* wraparound */
1946 gtk_notebook_reorder_child(nb, child, npage);
1947 break;
1949 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST:
1950 gtk_notebook_reorder_child(nb, child, (file_prefs.tab_order_ltr) ? 0 : -1);
1951 break;
1952 case GEANY_KEYS_NOTEBOOK_MOVETABLAST:
1953 gtk_notebook_reorder_child(nb, child, (file_prefs.tab_order_ltr) ? -1 : 0);
1954 break;
1956 return;
1960 static void goto_matching_brace(GeanyDocument *doc)
1962 gint pos, new_pos;
1963 gint after_brace;
1965 g_return_if_fail(DOC_VALID(doc));
1967 pos = sci_get_current_position(doc->editor->sci);
1968 after_brace = pos > 0 && utils_isbrace(sci_get_char_at(doc->editor->sci, pos - 1), TRUE);
1969 pos -= after_brace; /* set pos to the brace */
1971 new_pos = sci_find_matching_brace(doc->editor->sci, pos);
1972 if (new_pos != -1)
1973 { /* set the cursor at/after the brace */
1974 sci_set_current_position(doc->editor->sci, new_pos + (!after_brace), FALSE);
1975 editor_display_current_line(doc->editor, 0.5F);
1980 static gboolean cb_func_clipboard_action(guint key_id)
1982 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1984 switch (key_id)
1986 case GEANY_KEYS_CLIPBOARD_CUT:
1987 on_cut1_activate(NULL, NULL);
1988 break;
1989 case GEANY_KEYS_CLIPBOARD_COPY:
1990 on_copy1_activate(NULL, NULL);
1991 break;
1992 case GEANY_KEYS_CLIPBOARD_PASTE:
1993 on_paste1_activate(NULL, NULL);
1994 break;
1995 case GEANY_KEYS_CLIPBOARD_COPYLINE:
1996 if (IS_SCINTILLA(focusw))
1997 sci_send_command(SCINTILLA(focusw), SCI_LINECOPY);
1998 break;
1999 case GEANY_KEYS_CLIPBOARD_CUTLINE:
2000 if (IS_SCINTILLA(focusw))
2001 sci_send_command(SCINTILLA(focusw), SCI_LINECUT);
2002 break;
2004 return TRUE;
2008 static void goto_tag(GeanyDocument *doc, gboolean definition)
2010 gchar *text = get_current_word_or_sel(doc, FALSE);
2012 if (text)
2013 symbols_goto_tag(text, definition);
2014 else
2015 utils_beep();
2017 g_free(text);
2021 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
2022 static gboolean cb_func_goto_action(guint key_id)
2024 gint cur_line;
2025 GeanyDocument *doc = document_get_current();
2027 if (doc == NULL)
2028 return TRUE;
2030 cur_line = sci_get_current_line(doc->editor->sci);
2032 switch (key_id)
2034 case GEANY_KEYS_GOTO_BACK:
2035 navqueue_go_back();
2036 return TRUE;
2037 case GEANY_KEYS_GOTO_FORWARD:
2038 navqueue_go_forward();
2039 return TRUE;
2040 case GEANY_KEYS_GOTO_LINE:
2042 if (toolbar_prefs.visible)
2044 GtkWidget *wid = toolbar_get_widget_child_by_name("GotoEntry");
2046 /* use toolbar item if shown & not in the drop down overflow menu */
2047 if (wid && gtk_widget_get_mapped(wid))
2049 gtk_widget_grab_focus(wid);
2050 return TRUE;
2053 on_go_to_line_activate(NULL, NULL);
2054 return TRUE;
2056 case GEANY_KEYS_GOTO_MATCHINGBRACE:
2057 goto_matching_brace(doc);
2058 return TRUE;
2059 case GEANY_KEYS_GOTO_TOGGLEMARKER:
2061 sci_toggle_marker_at_line(doc->editor->sci, cur_line, 1);
2062 return TRUE;
2064 case GEANY_KEYS_GOTO_NEXTMARKER:
2066 gint mline = sci_marker_next(doc->editor->sci, cur_line + 1, 1 << 1, TRUE);
2068 if (mline != -1)
2070 sci_set_current_line(doc->editor->sci, mline);
2071 editor_display_current_line(doc->editor, 0.5F);
2073 return TRUE;
2075 case GEANY_KEYS_GOTO_PREVIOUSMARKER:
2077 gint mline = sci_marker_previous(doc->editor->sci, cur_line - 1, 1 << 1, TRUE);
2079 if (mline != -1)
2081 sci_set_current_line(doc->editor->sci, mline);
2082 editor_display_current_line(doc->editor, 0.5F);
2084 return TRUE;
2086 case GEANY_KEYS_GOTO_TAGDEFINITION:
2087 goto_tag(doc, TRUE);
2088 return TRUE;
2089 case GEANY_KEYS_GOTO_TAGDECLARATION:
2090 goto_tag(doc, FALSE);
2091 return TRUE;
2093 /* only check editor-sensitive keybindings when editor has focus so home,end still
2094 * work in other widgets */
2095 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
2096 return FALSE;
2098 switch (key_id)
2100 case GEANY_KEYS_GOTO_LINESTART:
2101 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
2102 break;
2103 case GEANY_KEYS_GOTO_LINEEND:
2104 sci_send_command(doc->editor->sci, SCI_LINEEND);
2105 break;
2106 case GEANY_KEYS_GOTO_LINESTARTVISUAL:
2107 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOMEDISPLAY : SCI_HOMEDISPLAY);
2108 break;
2109 case GEANY_KEYS_GOTO_LINEENDVISUAL:
2110 sci_send_command(doc->editor->sci, SCI_LINEENDDISPLAY);
2111 break;
2112 case GEANY_KEYS_GOTO_PREVWORDPART:
2113 sci_send_command(doc->editor->sci, SCI_WORDPARTLEFT);
2114 break;
2115 case GEANY_KEYS_GOTO_NEXTWORDPART:
2116 sci_send_command(doc->editor->sci, SCI_WORDPARTRIGHT);
2117 break;
2119 return TRUE;
2123 static void duplicate_lines(GeanyEditor *editor)
2125 if (sci_get_lines_selected(editor->sci) > 1)
2126 { /* ignore extra_line because of selecting lines from the line number column */
2127 editor_select_lines(editor, FALSE);
2128 sci_selection_duplicate(editor->sci);
2130 else if (sci_has_selection(editor->sci))
2131 sci_selection_duplicate(editor->sci);
2132 else
2133 sci_line_duplicate(editor->sci);
2137 static void delete_lines(GeanyEditor *editor)
2139 editor_select_lines(editor, TRUE); /* include last line (like cut lines, copy lines do) */
2140 sci_clear(editor->sci); /* (SCI_LINEDELETE only does 1 line) */
2144 /* common function for editor keybindings, only valid when scintilla has focus. */
2145 static gboolean cb_func_editor_action(guint key_id)
2147 GeanyDocument *doc = document_get_current();
2148 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2150 /* edit keybindings only valid when scintilla widget has focus */
2151 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2152 return FALSE; /* also makes tab work outside editor */
2154 switch (key_id)
2156 case GEANY_KEYS_EDITOR_UNDO:
2157 on_undo1_activate(NULL, NULL);
2158 break;
2159 case GEANY_KEYS_EDITOR_REDO:
2160 on_redo1_activate(NULL, NULL);
2161 break;
2162 case GEANY_KEYS_EDITOR_SCROLLTOLINE:
2163 editor_scroll_to_line(doc->editor, -1, 0.5F);
2164 break;
2165 case GEANY_KEYS_EDITOR_SCROLLLINEUP:
2166 sci_send_command(doc->editor->sci, SCI_LINESCROLLUP);
2167 break;
2168 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN:
2169 sci_send_command(doc->editor->sci, SCI_LINESCROLLDOWN);
2170 break;
2171 case GEANY_KEYS_EDITOR_DUPLICATELINE:
2172 duplicate_lines(doc->editor);
2173 break;
2174 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR:
2175 /* allow overloading */
2176 return editor_goto_next_snippet_cursor(doc->editor);
2177 case GEANY_KEYS_EDITOR_DELETELINE:
2178 delete_lines(doc->editor);
2179 break;
2180 case GEANY_KEYS_EDITOR_DELETELINETOEND:
2181 sci_send_command(doc->editor->sci, SCI_DELLINERIGHT);
2182 break;
2183 case GEANY_KEYS_EDITOR_DELETELINETOBEGINNING:
2184 sci_send_command(doc->editor->sci, SCI_DELLINELEFT);
2185 break;
2186 case GEANY_KEYS_EDITOR_TRANSPOSELINE:
2187 sci_send_command(doc->editor->sci, SCI_LINETRANSPOSE);
2188 break;
2189 case GEANY_KEYS_EDITOR_AUTOCOMPLETE:
2190 editor_start_auto_complete(doc->editor, sci_get_current_position(doc->editor->sci), TRUE);
2191 break;
2192 case GEANY_KEYS_EDITOR_CALLTIP:
2193 editor_show_calltip(doc->editor, -1);
2194 break;
2195 case GEANY_KEYS_EDITOR_CONTEXTACTION:
2196 if (check_current_word(doc, FALSE))
2197 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu,
2198 "context_action1")), NULL);
2199 break;
2200 case GEANY_KEYS_EDITOR_COMPLETESNIPPET:
2201 /* allow tab to be overloaded */
2202 return check_snippet_completion(doc);
2204 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION:
2206 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
2207 GEANY_KEYS_EDITOR_COMPLETESNIPPET);
2209 switch (kb->key)
2211 case GDK_space:
2212 sci_add_text(doc->editor->sci, " ");
2213 break;
2214 case GDK_Tab:
2215 sci_send_command(doc->editor->sci, SCI_TAB);
2216 break;
2217 default:
2218 break;
2220 break;
2222 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION:
2223 return editor_complete_word_part(doc->editor);
2225 case GEANY_KEYS_EDITOR_MOVELINEUP:
2226 sci_move_selected_lines_up(doc->editor->sci);
2227 break;
2228 case GEANY_KEYS_EDITOR_MOVELINEDOWN:
2229 sci_move_selected_lines_down(doc->editor->sci);
2230 break;
2232 return TRUE;
2236 static void join_lines(GeanyEditor *editor)
2238 gint start, end, i;
2240 start = sci_get_line_from_position(editor->sci,
2241 sci_get_selection_start(editor->sci));
2242 end = sci_get_line_from_position(editor->sci,
2243 sci_get_selection_end(editor->sci));
2245 /* remove spaces surrounding the lines so that these spaces
2246 * won't appear within text after joining */
2247 for (i = start; i < end; i++)
2248 editor_strip_line_trailing_spaces(editor, i);
2249 for (i = start + 1; i <= end; i++)
2250 sci_set_line_indentation(editor->sci, i, 0);
2252 sci_set_target_start(editor->sci,
2253 sci_get_position_from_line(editor->sci, start));
2254 sci_set_target_end(editor->sci,
2255 sci_get_position_from_line(editor->sci, end));
2256 sci_lines_join(editor->sci);
2260 static gint get_reflow_column(GeanyEditor *editor)
2262 const GeanyEditorPrefs *eprefs = editor_get_prefs(editor);
2263 if (editor->line_breaking)
2264 return eprefs->line_break_column;
2265 else if (eprefs->long_line_type != 2)
2266 return eprefs->long_line_column;
2267 else
2268 return -1; /* do nothing */
2272 /* Split the line where the cursor is positioned, on `column`,
2273 possibly many times if the line is long.
2274 Return the number of lines added because of the splitting. */
2275 static gint split_line(GeanyEditor *editor, gint column)
2277 ScintillaObject *sci = editor->sci;
2278 gint start_line = sci_get_current_line(sci);
2279 gint line = start_line;
2281 while (TRUE)
2283 gint lstart = sci_get_position_from_line(sci, line);
2284 gint lend = sci_get_line_end_position(sci, line);
2285 gint edge = sci_get_position_from_col(sci, line, column);
2286 gboolean found;
2287 gint pos;
2289 /* don't split on a trailing space of a line */
2290 if (sci_get_char_at(sci, lend - 1) == ' ')
2291 lend--;
2293 /* detect when the line is short enough and no more splitting is needed */
2294 if (sci_get_col_from_position(sci, lend) < column)
2295 break;
2297 /* lookup split position */
2298 found = FALSE;
2299 for (pos = edge - 1; pos > lstart; pos--)
2301 if (sci_get_char_at(sci, pos) == ' ')
2303 found = TRUE;
2304 break;
2307 if (!found)
2309 for (pos = edge; pos < lend; pos++)
2311 if (sci_get_char_at(sci, pos) == ' ')
2313 found = TRUE;
2314 break;
2318 /* don't split right before a space */
2319 while (pos + 1 <= lend && sci_get_char_at(sci, pos + 1) == ' ')
2320 pos++;
2322 if (!found || pos >= lend)
2323 break;
2325 sci_insert_text(sci, pos + 1, editor_get_eol_char(editor));
2326 line++;
2328 return line - start_line;
2332 static void reflow_lines(GeanyEditor *editor, gint column)
2334 gint start, indent, linescount, i;
2336 start = sci_get_line_from_position(editor->sci,
2337 sci_get_selection_start(editor->sci));
2339 /* if several lines are selected, join them. */
2340 if (sci_get_lines_selected(editor->sci) > 1)
2341 join_lines(editor);
2343 /* if this line is short enough, do nothing */
2344 if (column > sci_get_line_end_position(editor->sci, start) -
2345 sci_get_position_from_line(editor->sci, start))
2347 return;
2351 * We have to manipulate line indentation so that indentation
2352 * of the resulting lines would be consistent. For example,
2353 * the result of splitting "[TAB]very long content":
2355 * +-------------+-------------+
2356 * | proper | wrong |
2357 * +-------------+-------------+
2358 * | [TAB]very | [TAB]very |
2359 * | [TAB]long | long |
2360 * | [TAB]content| content |
2361 * +-------------+-------------+
2363 indent = sci_get_line_indentation(editor->sci, start);
2364 sci_set_line_indentation(editor->sci, start, 0);
2366 linescount = split_line(editor, column - indent);
2368 /* Fix indentation. */
2369 for (i = start; i <= start + linescount; i++)
2370 sci_set_line_indentation(editor->sci, i, indent);
2372 /* Remove trailing spaces. */
2373 if (editor_prefs.newline_strip || file_prefs.strip_trailing_spaces)
2375 for (i = start; i <= start + linescount; i++)
2376 editor_strip_line_trailing_spaces(editor, i);
2381 /* deselect last newline of selection, if any */
2382 static void sci_deselect_last_newline(ScintillaObject *sci)
2384 gint start, end;
2386 start = sci_get_selection_start(sci);
2387 end = sci_get_selection_end(sci);
2388 if (end > start && sci_get_col_from_position(sci, end) == 0)
2390 end = sci_get_line_end_position(sci, sci_get_line_from_position(sci, end - 1));
2391 sci_set_selection(sci, start, end);
2396 static void reflow_paragraph(GeanyEditor *editor)
2398 ScintillaObject *sci = editor->sci;
2399 gboolean sel;
2400 gint column;
2402 column = get_reflow_column(editor);
2403 if (column == -1)
2405 utils_beep();
2406 return;
2409 sci_start_undo_action(sci);
2410 sel = sci_has_selection(sci);
2411 if (!sel)
2412 editor_select_indent_block(editor);
2413 sci_deselect_last_newline(sci);
2414 reflow_lines(editor, column);
2415 if (!sel)
2416 sci_set_anchor(sci, -1);
2417 sci_goto_pos(sci, sci_get_line_end_position(sci, sci_get_current_line(sci)), TRUE);
2419 sci_end_undo_action(sci);
2423 static void join_paragraph(GeanyEditor *editor)
2425 ScintillaObject *sci = editor->sci;
2426 gboolean sel;
2427 gint column;
2429 column = get_reflow_column(editor);
2430 if (column == -1)
2432 utils_beep();
2433 return;
2436 sci_start_undo_action(sci);
2437 sel = sci_has_selection(sci);
2438 if (!sel)
2439 editor_select_indent_block(editor);
2440 sci_deselect_last_newline(sci);
2441 join_lines(editor);
2442 if (!sel)
2443 sci_set_anchor(sci, -1);
2445 sci_end_undo_action(sci);
2449 /* common function for format keybindings, only valid when scintilla has focus. */
2450 static gboolean cb_func_format_action(guint key_id)
2452 GeanyDocument *doc = document_get_current();
2453 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2455 /* keybindings only valid when scintilla widget has focus */
2456 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2457 return TRUE;
2459 switch (key_id)
2461 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE:
2462 on_menu_toggle_line_commentation1_activate(NULL, NULL);
2463 break;
2464 case GEANY_KEYS_FORMAT_COMMENTLINE:
2465 on_menu_comment_line1_activate(NULL, NULL);
2466 break;
2467 case GEANY_KEYS_FORMAT_UNCOMMENTLINE:
2468 on_menu_uncomment_line1_activate(NULL, NULL);
2469 break;
2470 case GEANY_KEYS_FORMAT_INCREASEINDENT:
2471 on_menu_increase_indent1_activate(NULL, NULL);
2472 break;
2473 case GEANY_KEYS_FORMAT_DECREASEINDENT:
2474 on_menu_decrease_indent1_activate(NULL, NULL);
2475 break;
2476 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE:
2477 editor_indentation_by_one_space(doc->editor, -1, FALSE);
2478 break;
2479 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE:
2480 editor_indentation_by_one_space(doc->editor, -1, TRUE);
2481 break;
2482 case GEANY_KEYS_FORMAT_AUTOINDENT:
2483 editor_smart_line_indentation(doc->editor, -1);
2484 break;
2485 case GEANY_KEYS_FORMAT_TOGGLECASE:
2486 on_toggle_case1_activate(NULL, NULL);
2487 break;
2488 case GEANY_KEYS_FORMAT_SENDTOCMD1:
2489 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 0)
2490 tools_execute_custom_command(doc, ui_prefs.custom_commands[0]);
2491 break;
2492 case GEANY_KEYS_FORMAT_SENDTOCMD2:
2493 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 1)
2494 tools_execute_custom_command(doc, ui_prefs.custom_commands[1]);
2495 break;
2496 case GEANY_KEYS_FORMAT_SENDTOCMD3:
2497 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 2)
2498 tools_execute_custom_command(doc, ui_prefs.custom_commands[2]);
2499 break;
2500 case GEANY_KEYS_FORMAT_SENDTOCMD4:
2501 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 3)
2502 tools_execute_custom_command(doc, ui_prefs.custom_commands[3]);
2503 break;
2504 case GEANY_KEYS_FORMAT_SENDTOCMD5:
2505 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 4)
2506 tools_execute_custom_command(doc, ui_prefs.custom_commands[4]);
2507 break;
2508 case GEANY_KEYS_FORMAT_SENDTOCMD6:
2509 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 5)
2510 tools_execute_custom_command(doc, ui_prefs.custom_commands[5]);
2511 break;
2512 case GEANY_KEYS_FORMAT_SENDTOCMD7:
2513 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 6)
2514 tools_execute_custom_command(doc, ui_prefs.custom_commands[6]);
2515 break;
2516 case GEANY_KEYS_FORMAT_SENDTOCMD8:
2517 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 7)
2518 tools_execute_custom_command(doc, ui_prefs.custom_commands[7]);
2519 break;
2520 case GEANY_KEYS_FORMAT_SENDTOCMD9:
2521 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 8)
2522 tools_execute_custom_command(doc, ui_prefs.custom_commands[8]);
2523 break;
2524 case GEANY_KEYS_FORMAT_SENDTOVTE:
2525 on_send_selection_to_vte1_activate(NULL, NULL);
2526 break;
2527 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
2528 reflow_paragraph(doc->editor);
2529 break;
2530 case GEANY_KEYS_FORMAT_JOINLINES:
2531 join_paragraph(doc->editor);
2532 break;
2534 return TRUE;
2538 /* common function for select keybindings, valid for scintilla and/or gtk_editable objects. */
2539 static gboolean cb_func_select_action(guint key_id)
2541 GeanyDocument *doc = document_get_current();
2542 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2544 switch (key_id)
2546 case GEANY_KEYS_SELECT_ALL:
2547 on_menu_select_all1_activate(NULL, NULL);
2548 break;
2549 case GEANY_KEYS_SELECT_WORD:
2550 if (doc != NULL)
2551 editor_select_word(doc->editor);
2552 break;
2553 case GEANY_KEYS_SELECT_LINE:
2554 if (doc != NULL)
2555 editor_select_lines(doc->editor, FALSE);
2556 break;
2557 case GEANY_KEYS_SELECT_PARAGRAPH:
2558 if (doc != NULL)
2559 editor_select_paragraph(doc->editor);
2560 break;
2561 case GEANY_KEYS_SELECT_WORDPARTLEFT:
2562 if (IS_SCINTILLA(focusw))
2563 sci_send_command(SCINTILLA(focusw), SCI_WORDPARTLEFTEXTEND);
2564 break;
2565 case GEANY_KEYS_SELECT_WORDPARTRIGHT:
2566 if (IS_SCINTILLA(focusw))
2567 sci_send_command(SCINTILLA(focusw), SCI_WORDPARTRIGHTEXTEND);
2568 break;
2570 return TRUE;
2574 static gboolean cb_func_document_action(guint key_id)
2576 GeanyDocument *doc = document_get_current();
2578 if (doc == NULL)
2579 return TRUE;
2581 switch (key_id)
2583 case GEANY_KEYS_DOCUMENT_REPLACETABS:
2584 on_replace_tabs_activate(NULL, NULL);
2585 break;
2586 case GEANY_KEYS_DOCUMENT_REPLACESPACES:
2587 on_replace_spaces_activate(NULL, NULL);
2588 break;
2589 case GEANY_KEYS_DOCUMENT_LINEBREAK:
2590 on_line_breaking1_activate(NULL, NULL);
2591 ui_document_show_hide(doc);
2592 break;
2593 case GEANY_KEYS_DOCUMENT_LINEWRAP:
2594 on_line_wrapping1_toggled(NULL, NULL);
2595 ui_document_show_hide(doc);
2596 break;
2597 case GEANY_KEYS_DOCUMENT_CLONE:
2598 document_clone(doc);
2599 break;
2600 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST:
2601 document_update_tags(doc);
2602 break;
2603 case GEANY_KEYS_DOCUMENT_FOLDALL:
2604 editor_fold_all(doc->editor);
2605 break;
2606 case GEANY_KEYS_DOCUMENT_UNFOLDALL:
2607 editor_unfold_all(doc->editor);
2608 break;
2609 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD:
2610 if (editor_prefs.folding)
2612 gint line = sci_get_current_line(doc->editor->sci);
2613 editor_toggle_fold(doc->editor, line, 0);
2614 break;
2616 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS:
2617 on_remove_markers1_activate(NULL, NULL);
2618 break;
2619 case GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS:
2620 on_menu_remove_indicators1_activate(NULL, NULL);
2621 break;
2622 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS:
2623 on_remove_markers1_activate(NULL, NULL);
2624 on_menu_remove_indicators1_activate(NULL, NULL);
2625 break;
2627 return TRUE;
2631 static void insert_line_after(GeanyEditor *editor)
2633 ScintillaObject *sci = editor->sci;
2635 sci_send_command(sci, SCI_LINEEND);
2636 sci_send_command(sci, SCI_NEWLINE);
2640 static void insert_line_before(GeanyEditor *editor)
2642 ScintillaObject *sci = editor->sci;
2643 gint line = sci_get_current_line(sci);
2644 gint indentpos = sci_get_line_indent_position(sci, line);
2646 sci_set_current_position(sci, indentpos, TRUE);
2647 sci_send_command(sci, SCI_NEWLINE);
2648 sci_send_command(sci, SCI_LINEUP);
2652 /* common function for insert keybindings, only valid when scintilla has focus. */
2653 static gboolean cb_func_insert_action(guint key_id)
2655 GeanyDocument *doc = document_get_current();
2656 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2658 /* keybindings only valid when scintilla widget has focus */
2659 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2660 return TRUE;
2662 switch (key_id)
2664 case GEANY_KEYS_INSERT_ALTWHITESPACE:
2665 editor_insert_alternative_whitespace(doc->editor);
2666 break;
2667 case GEANY_KEYS_INSERT_DATE:
2668 gtk_menu_item_activate(GTK_MENU_ITEM(
2669 ui_lookup_widget(main_widgets.window, "insert_date_custom1")));
2670 break;
2671 case GEANY_KEYS_INSERT_LINEAFTER:
2672 insert_line_after(doc->editor);
2673 break;
2674 case GEANY_KEYS_INSERT_LINEBEFORE:
2675 insert_line_before(doc->editor);
2676 break;
2678 return TRUE;
2682 /* update key combination */
2683 void keybindings_update_combo(GeanyKeyBinding *kb, guint key, GdkModifierType mods)
2685 GtkWidget *widget = kb->menu_item;
2687 if (widget && kb->key)
2688 gtk_widget_remove_accelerator(widget, kb_accel_group, kb->key, kb->mods);
2690 kb->key = key;
2691 kb->mods = mods;
2693 if (widget && kb->key)
2694 gtk_widget_add_accelerator(widget, "activate", kb_accel_group,
2695 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
2699 /* used for plugins, can be called repeatedly. */
2700 GeanyKeyGroup *keybindings_set_group(GeanyKeyGroup *group, const gchar *section_name,
2701 const gchar *label, gsize count, GeanyKeyGroupCallback callback)
2703 g_return_val_if_fail(section_name, NULL);
2704 g_return_val_if_fail(count, NULL);
2706 /* prevent conflict with core bindings */
2707 g_return_val_if_fail(!g_str_equal(section_name, keybindings_keyfile_group_name), NULL);
2709 if (!group)
2711 group = g_new0(GeanyKeyGroup, 1);
2712 add_kb_group(group, section_name, label, callback, TRUE);
2714 /* Calls free_key_binding() for individual entries for plugins - has to be
2715 * called before g_free(group->plugin_keys) */
2716 g_ptr_array_set_size(group->key_items, 0);
2717 g_free(group->plugin_keys);
2718 group->plugin_keys = g_new0(GeanyKeyBinding, count);
2719 group->plugin_key_count = count;
2720 return group;
2724 void keybindings_free_group(GeanyKeyGroup *group)
2726 g_ptr_array_remove_fast(keybinding_groups, group);