Merge branch 'master' of https://github.com/konsolebox/geany into konsolebox-master
[geany-mirror.git] / src / keybindings.c
blob5dcfabcc449598f3b0eb92b90f9a740135b13b0a
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 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 /** Fills a GeanyKeyBinding struct item.
154 * @note Always set @a key and @a mod to 0, otherwise you will likely
155 * cause conflicts with the user's custom, other plugin's keybindings or
156 * future default keybindings.
157 * @param group Group.
158 * @param key_id Keybinding index for the group.
159 * @param callback Function to call when activated, or @c NULL to use the group callback.
160 * Usually it's better to use the group callback instead - see plugin_set_key_group().
161 * @param key (Lower case) default key, e.g. @c GDK_j, but usually 0 for unset.
162 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
163 * @param kf_name Key name for the configuration file, such as @c "menu_new".
164 * @param label Label used in the preferences dialog keybindings tab. May contain
165 * underscores - these won't be displayed.
166 * @param menu_item Optional widget to set an accelerator for, or @c NULL.
167 * @return The keybinding - normally this is ignored. */
168 GEANY_API_SYMBOL
169 GeanyKeyBinding *keybindings_set_item(GeanyKeyGroup *group, gsize key_id,
170 GeanyKeyCallback callback, guint key, GdkModifierType mod,
171 const gchar *kf_name, const gchar *label, GtkWidget *menu_item)
173 GeanyKeyBinding *kb;
175 g_assert(group->name);
176 kb = keybindings_get_item(group, key_id);
177 g_assert(!kb->name);
178 g_ptr_array_add(group->key_items, kb);
180 if (group->plugin)
182 /* some plugins e.g. GeanyLua need these fields duplicated */
183 SETPTR(kb->name, g_strdup(kf_name));
184 SETPTR(kb->label, g_strdup(label));
186 else
188 /* we don't touch these strings unless group->plugin is set, const cast is safe */
189 kb->name = (gchar *)kf_name;
190 kb->label = (gchar *)label;
192 kb->key = key;
193 kb->mods = mod;
194 kb->default_key = key;
195 kb->default_mods = mod;
196 kb->callback = callback;
197 kb->menu_item = menu_item;
198 kb->id = key_id;
199 return kb;
203 static void add_kb_group(GeanyKeyGroup *group,
204 const gchar *name, const gchar *label, GeanyKeyGroupCallback callback, gboolean plugin)
206 g_ptr_array_add(keybinding_groups, group);
208 group->name = name;
209 group->label = label;
210 group->callback = callback;
211 group->plugin = plugin;
212 group->key_items = g_ptr_array_new();
216 GeanyKeyGroup *keybindings_get_core_group(guint id)
218 static GeanyKeyGroup groups[GEANY_KEY_GROUP_COUNT];
220 g_return_val_if_fail(id < GEANY_KEY_GROUP_COUNT, NULL);
222 return &groups[id];
226 static void add_kb(GeanyKeyGroup *group, gsize key_id,
227 GeanyKeyCallback callback, guint key, GdkModifierType mod,
228 const gchar *kf_name, const gchar *label, const gchar *widget_name)
230 GtkWidget *widget = widget_name ?
231 ui_lookup_widget(main_widgets.window, widget_name) : NULL;
233 keybindings_set_item(group, key_id, callback,
234 key, mod, kf_name, label, widget);
238 #define ADD_KB_GROUP(group_id, label, callback) \
239 add_kb_group(keybindings_get_core_group(group_id),\
240 keybindings_keyfile_group_name, label, callback, FALSE)
242 static void init_default_kb(void)
244 GeanyKeyGroup *group;
246 /* visual group order */
247 ADD_KB_GROUP(GEANY_KEY_GROUP_FILE, _("File"), cb_func_file_action);
248 ADD_KB_GROUP(GEANY_KEY_GROUP_EDITOR, _("Editor"), cb_func_editor_action);
249 ADD_KB_GROUP(GEANY_KEY_GROUP_CLIPBOARD, _("Clipboard"), cb_func_clipboard_action);
250 ADD_KB_GROUP(GEANY_KEY_GROUP_SELECT, _("Select"), cb_func_select_action);
251 ADD_KB_GROUP(GEANY_KEY_GROUP_FORMAT, _("Format"), cb_func_format_action);
252 ADD_KB_GROUP(GEANY_KEY_GROUP_INSERT, _("Insert"), cb_func_insert_action);
253 ADD_KB_GROUP(GEANY_KEY_GROUP_SETTINGS, _("Settings"), NULL);
254 ADD_KB_GROUP(GEANY_KEY_GROUP_SEARCH, _("Search"), cb_func_search_action);
255 ADD_KB_GROUP(GEANY_KEY_GROUP_GOTO, _("Go to"), cb_func_goto_action);
256 ADD_KB_GROUP(GEANY_KEY_GROUP_VIEW, _("View"), cb_func_view_action);
257 ADD_KB_GROUP(GEANY_KEY_GROUP_DOCUMENT, _("Document"), cb_func_document_action);
258 ADD_KB_GROUP(GEANY_KEY_GROUP_PROJECT, _("Project"), cb_func_project_action);
259 ADD_KB_GROUP(GEANY_KEY_GROUP_BUILD, _("Build"), cb_func_build_action);
260 ADD_KB_GROUP(GEANY_KEY_GROUP_TOOLS, _("Tools"), NULL);
261 ADD_KB_GROUP(GEANY_KEY_GROUP_HELP, _("Help"), NULL);
262 ADD_KB_GROUP(GEANY_KEY_GROUP_FOCUS, _("Focus"), cb_func_switch_action);
263 ADD_KB_GROUP(GEANY_KEY_GROUP_NOTEBOOK, _("Notebook tab"), NULL);
265 /* Init all fields of keys with default values.
266 * The menu_item field is always the main menu item, popup menu accelerators are
267 * set in add_popup_menu_accels(). */
269 group = keybindings_get_core_group(GEANY_KEY_GROUP_FILE);
271 add_kb(group, GEANY_KEYS_FILE_NEW, NULL,
272 GDK_n, GEANY_PRIMARY_MOD_MASK, "menu_new", _("New"), "menu_new1");
273 add_kb(group, GEANY_KEYS_FILE_OPEN, NULL,
274 GDK_o, GEANY_PRIMARY_MOD_MASK, "menu_open", _("Open"), "menu_open1");
275 add_kb(group, GEANY_KEYS_FILE_OPENSELECTED, NULL,
276 GDK_o, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "menu_open_selected",
277 _("Open selected file"), "menu_open_selected_file1");
278 add_kb(group, GEANY_KEYS_FILE_SAVE, NULL,
279 GDK_s, GEANY_PRIMARY_MOD_MASK, "menu_save", _("Save"), "menu_save1");
280 add_kb(group, GEANY_KEYS_FILE_SAVEAS, NULL,
281 0, 0, "menu_saveas", _("Save as"), "menu_save_as1");
282 add_kb(group, GEANY_KEYS_FILE_SAVEALL, NULL,
283 GDK_s, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "menu_saveall", _("Save all"),
284 "menu_save_all1");
285 add_kb(group, GEANY_KEYS_FILE_PRINT, NULL,
286 GDK_p, GEANY_PRIMARY_MOD_MASK, "menu_print", _("Print"), "print1");
287 add_kb(group, GEANY_KEYS_FILE_CLOSE, NULL,
288 GDK_w, GEANY_PRIMARY_MOD_MASK, "menu_close", _("Close"), "menu_close1");
289 add_kb(group, GEANY_KEYS_FILE_CLOSEALL, NULL,
290 GDK_w, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "menu_closeall", _("Close all"),
291 "menu_close_all1");
292 add_kb(group, GEANY_KEYS_FILE_RELOAD, NULL,
293 GDK_r, GEANY_PRIMARY_MOD_MASK, "menu_reloadfile", _("Reload file"), "menu_reload1");
294 add_kb(group, GEANY_KEYS_FILE_OPENLASTTAB, NULL,
295 0, 0, "file_openlasttab", _("Re-open last closed tab"), NULL);
296 add_kb(group, GEANY_KEYS_FILE_QUIT, NULL,
297 GDK_q, GEANY_PRIMARY_MOD_MASK, "menu_quit", _("Quit"), "menu_quit1");
299 group = keybindings_get_core_group(GEANY_KEY_GROUP_PROJECT);
301 add_kb(group, GEANY_KEYS_PROJECT_NEW, NULL,
302 0, 0, "project_new", _("New"), "project_new1");
303 add_kb(group, GEANY_KEYS_PROJECT_OPEN, NULL,
304 0, 0, "project_open", _("Open"), "project_open1");
305 add_kb(group, GEANY_KEYS_PROJECT_PROPERTIES, NULL,
306 0, 0, "project_properties",
307 ui_lookup_stock_label(GTK_STOCK_PROPERTIES), "project_properties1");
308 add_kb(group, GEANY_KEYS_PROJECT_CLOSE, NULL,
309 0, 0, "project_close", _("Close"), "project_close1");
311 group = keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR);
313 add_kb(group, GEANY_KEYS_EDITOR_UNDO, NULL,
314 GDK_z, GEANY_PRIMARY_MOD_MASK, "menu_undo", _("Undo"), "menu_undo2");
315 add_kb(group, GEANY_KEYS_EDITOR_REDO, NULL,
316 GDK_y, GEANY_PRIMARY_MOD_MASK, "menu_redo", _("Redo"), "menu_redo2");
317 add_kb(group, GEANY_KEYS_EDITOR_DUPLICATELINE, NULL,
318 GDK_d, GEANY_PRIMARY_MOD_MASK, "edit_duplicateline", _("D_uplicate Line or Selection"),
319 "duplicate_line_or_selection1");
320 add_kb(group, GEANY_KEYS_EDITOR_DELETELINE, NULL,
321 GDK_k, GEANY_PRIMARY_MOD_MASK, "edit_deleteline", _("_Delete Current Line(s)"),
322 "delete_current_lines1");
323 add_kb(group, GEANY_KEYS_EDITOR_DELETELINETOEND, NULL,
324 GDK_Delete, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "edit_deletelinetoend",
325 _("Delete to line end"), NULL);
326 /* Note: transpose may fit better in format group, but that would break the API */
327 add_kb(group, GEANY_KEYS_EDITOR_TRANSPOSELINE, NULL,
328 0, 0, "edit_transposeline", _("_Transpose Current Line"), NULL);
329 add_kb(group, GEANY_KEYS_EDITOR_SCROLLTOLINE, NULL,
330 GDK_l, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "edit_scrolltoline", _("Scroll to current line"), NULL);
331 add_kb(group, GEANY_KEYS_EDITOR_SCROLLLINEUP, NULL,
332 GDK_Up, GDK_MOD1_MASK, "edit_scrolllineup", _("Scroll up the view by one line"), NULL);
333 add_kb(group, GEANY_KEYS_EDITOR_SCROLLLINEDOWN, NULL,
334 GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
335 add_kb(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL,
336 GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
337 add_kb(group, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR, NULL,
338 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
339 add_kb(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, NULL,
340 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
341 add_kb(group, GEANY_KEYS_EDITOR_CONTEXTACTION, NULL,
342 0, 0, "popup_contextaction", _("Context Action"), NULL);
343 add_kb(group, GEANY_KEYS_EDITOR_AUTOCOMPLETE, NULL,
344 GDK_space, GEANY_PRIMARY_MOD_MASK, "edit_autocomplete", _("Complete word"), NULL);
345 add_kb(group, GEANY_KEYS_EDITOR_CALLTIP, NULL,
346 GDK_space, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "edit_calltip", _("Show calltip"), NULL);
347 add_kb(group, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION, NULL,
348 GDK_Tab, 0, "edit_wordpartcompletion", _("Word part completion"), NULL);
349 add_kb(group, GEANY_KEYS_EDITOR_MOVELINEUP, NULL,
350 GDK_Page_Up, GDK_MOD1_MASK, "edit_movelineup",
351 _("Move line(s) up"), "move_lines_up1");
352 add_kb(group, GEANY_KEYS_EDITOR_MOVELINEDOWN, NULL,
353 GDK_Page_Down, GDK_MOD1_MASK, "edit_movelinedown",
354 _("Move line(s) down"), "move_lines_down1");
356 group = keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD);
358 add_kb(group, GEANY_KEYS_CLIPBOARD_CUT, NULL,
359 GDK_x, GEANY_PRIMARY_MOD_MASK, "menu_cut", _("Cut"), "menu_cut1");
360 add_kb(group, GEANY_KEYS_CLIPBOARD_COPY, NULL,
361 GDK_c, GEANY_PRIMARY_MOD_MASK, "menu_copy", _("Copy"), "menu_copy1");
362 add_kb(group, GEANY_KEYS_CLIPBOARD_PASTE, NULL,
363 GDK_v, GEANY_PRIMARY_MOD_MASK, "menu_paste", _("Paste"), "menu_paste1");
364 add_kb(group, GEANY_KEYS_CLIPBOARD_COPYLINE, NULL,
365 GDK_c, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "edit_copyline", _("_Copy Current Line(s)"),
366 "copy_current_lines1");
367 add_kb(group, GEANY_KEYS_CLIPBOARD_CUTLINE, NULL,
368 GDK_x, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "edit_cutline", _("Cu_t Current Line(s)"),
369 "cut_current_lines1");
371 group = keybindings_get_core_group(GEANY_KEY_GROUP_SELECT);
373 add_kb(group, GEANY_KEYS_SELECT_ALL, NULL,
374 GDK_a, GEANY_PRIMARY_MOD_MASK, "menu_selectall", _("Select All"), "menu_select_all1");
375 add_kb(group, GEANY_KEYS_SELECT_WORD, NULL,
376 GDK_w, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectword", _("Select current word"), NULL);
377 add_kb(group, GEANY_KEYS_SELECT_LINE, NULL,
378 GDK_l, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectline", _("S_elect Current Line(s)"),
379 "select_current_lines1");
380 add_kb(group, GEANY_KEYS_SELECT_PARAGRAPH, NULL,
381 GDK_p, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectparagraph", _("Se_lect Current Paragraph"),
382 "select_current_paragraph1");
383 add_kb(group, GEANY_KEYS_SELECT_WORDPARTLEFT, NULL,
384 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL);
385 add_kb(group, GEANY_KEYS_SELECT_WORDPARTRIGHT, NULL,
386 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL);
388 group = keybindings_get_core_group(GEANY_KEY_GROUP_FORMAT);
390 add_kb(group, GEANY_KEYS_FORMAT_TOGGLECASE, NULL,
391 GDK_u, GEANY_PRIMARY_MOD_MASK | GDK_MOD1_MASK, "edit_togglecase",
392 _("T_oggle Case of Selection"), "menu_toggle_case2");
393 add_kb(group, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE, NULL,
394 GDK_e, GEANY_PRIMARY_MOD_MASK, "edit_commentlinetoggle", _("Toggle line commentation"),
395 "menu_toggle_line_commentation1");
396 add_kb(group, GEANY_KEYS_FORMAT_COMMENTLINE, NULL,
397 0, 0, "edit_commentline", _("Comment line(s)"), "menu_comment_line1");
398 add_kb(group, GEANY_KEYS_FORMAT_UNCOMMENTLINE, NULL,
399 0, 0, "edit_uncommentline", _("Uncomment line(s)"), "menu_uncomment_line1");
400 add_kb(group, GEANY_KEYS_FORMAT_INCREASEINDENT, NULL,
401 GDK_i, GEANY_PRIMARY_MOD_MASK, "edit_increaseindent", _("Increase indent"),
402 "menu_increase_indent1");
403 add_kb(group, GEANY_KEYS_FORMAT_DECREASEINDENT, NULL,
404 GDK_u, GEANY_PRIMARY_MOD_MASK, "edit_decreaseindent", _("Decrease indent"),
405 "menu_decrease_indent1");
406 add_kb(group, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE, NULL,
407 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL);
408 add_kb(group, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE, NULL,
409 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL);
410 add_kb(group, GEANY_KEYS_FORMAT_AUTOINDENT, NULL,
411 0, 0, "edit_autoindent", _("S_mart Line Indent"), "smart_line_indent1");
412 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD1, NULL,
413 GDK_1, GEANY_PRIMARY_MOD_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL);
414 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD2, NULL,
415 GDK_2, GEANY_PRIMARY_MOD_MASK, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL);
416 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD3, NULL,
417 GDK_3, GEANY_PRIMARY_MOD_MASK, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL);
418 /* may fit better in editor group */
419 add_kb(group, GEANY_KEYS_FORMAT_SENDTOVTE, NULL,
420 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), "send_selection_to_vte1");
421 add_kb(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
422 GDK_j, GEANY_PRIMARY_MOD_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
423 "reflow_lines_block1");
424 keybindings_set_item(group, GEANY_KEYS_FORMAT_JOINLINES, NULL,
425 0, 0, "edit_joinlines", _("Join lines"), NULL);
427 group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
429 add_kb(group, GEANY_KEYS_INSERT_DATE, NULL,
430 GDK_d, GDK_SHIFT_MASK | GDK_MOD1_MASK, "menu_insert_date", _("Insert date"),
431 "insert_date_custom1");
432 add_kb(group, GEANY_KEYS_INSERT_ALTWHITESPACE, NULL,
433 0, 0, "edit_insertwhitespace", _("Insert Alternative _White Space"),
434 "insert_alternative_white_space1");
435 add_kb(group, GEANY_KEYS_INSERT_LINEBEFORE, NULL,
436 0, 0, "edit_insertlinebefore", _("Insert New Line Before Current"), NULL);
437 add_kb(group, GEANY_KEYS_INSERT_LINEAFTER, NULL,
438 0, 0, "edit_insertlineafter", _("Insert New Line After Current"), NULL);
440 group = keybindings_get_core_group(GEANY_KEY_GROUP_SETTINGS);
442 add_kb(group, GEANY_KEYS_SETTINGS_PREFERENCES, cb_func_menu_preferences,
443 GDK_p, GEANY_PRIMARY_MOD_MASK | GDK_MOD1_MASK, "menu_preferences", _("Preferences"),
444 "preferences1");
445 add_kb(group, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES, cb_func_menu_preferences,
446 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), "plugin_preferences1");
448 group = keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH);
450 add_kb(group, GEANY_KEYS_SEARCH_FIND, NULL,
451 GDK_f, GEANY_PRIMARY_MOD_MASK, "menu_find", _("Find"), "find1");
452 add_kb(group, GEANY_KEYS_SEARCH_FINDNEXT, NULL,
453 GDK_g, GEANY_PRIMARY_MOD_MASK, "menu_findnext", _("Find Next"), "find_next1");
454 add_kb(group, GEANY_KEYS_SEARCH_FINDPREVIOUS, NULL,
455 GDK_g, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "menu_findprevious", _("Find Previous"),
456 "find_previous1");
457 add_kb(group, GEANY_KEYS_SEARCH_FINDNEXTSEL, NULL,
458 0, 0, "menu_findnextsel", _("Find Next _Selection"), "find_nextsel1");
459 add_kb(group, GEANY_KEYS_SEARCH_FINDPREVSEL, NULL,
460 0, 0, "menu_findprevsel", _("Find Pre_vious Selection"), "find_prevsel1");
461 add_kb(group, GEANY_KEYS_SEARCH_REPLACE, NULL,
462 GDK_h, GEANY_PRIMARY_MOD_MASK, "menu_replace", _("Replace"), "replace1");
463 add_kb(group, GEANY_KEYS_SEARCH_FINDINFILES, NULL, GDK_f,
464 GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "menu_findinfiles", _("Find in Files"),
465 "find_in_files1");
466 add_kb(group, GEANY_KEYS_SEARCH_NEXTMESSAGE, NULL,
467 0, 0, "menu_nextmessage", _("Next Message"), "next_message1");
468 add_kb(group, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE, NULL,
469 0, 0, "menu_previousmessage", _("Previous Message"), "previous_message1");
470 add_kb(group, GEANY_KEYS_SEARCH_FINDUSAGE, NULL,
471 GDK_e, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "popup_findusage",
472 _("Find Usage"), "find_usage1");
473 add_kb(group, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, NULL,
474 GDK_d, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "popup_finddocumentusage",
475 _("Find Document Usage"), "find_document_usage1");
476 add_kb(group, GEANY_KEYS_SEARCH_MARKALL, NULL,
477 GDK_m, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "find_markall", _("_Mark All"), "mark_all1");
479 group = keybindings_get_core_group(GEANY_KEY_GROUP_GOTO);
481 add_kb(group, GEANY_KEYS_GOTO_BACK, NULL,
482 GDK_Left, GDK_MOD1_MASK, "nav_back", _("Navigate back a location"), NULL);
483 add_kb(group, GEANY_KEYS_GOTO_FORWARD, NULL,
484 GDK_Right, GDK_MOD1_MASK, "nav_forward", _("Navigate forward a location"), NULL);
485 add_kb(group, GEANY_KEYS_GOTO_LINE, NULL,
486 GDK_l, GEANY_PRIMARY_MOD_MASK, "menu_gotoline", _("Go to Line"), "go_to_line1");
487 add_kb(group, GEANY_KEYS_GOTO_MATCHINGBRACE, NULL,
488 GDK_b, GEANY_PRIMARY_MOD_MASK, "edit_gotomatchingbrace",
489 _("Go to matching brace"), NULL);
490 add_kb(group, GEANY_KEYS_GOTO_TOGGLEMARKER, NULL,
491 GDK_m, GEANY_PRIMARY_MOD_MASK, "edit_togglemarker",
492 _("Toggle marker"), NULL);
493 add_kb(group, GEANY_KEYS_GOTO_NEXTMARKER, NULL,
494 GDK_period, GEANY_PRIMARY_MOD_MASK, "edit_gotonextmarker",
495 _("Go to Ne_xt Marker"), "go_to_next_marker1");
496 add_kb(group, GEANY_KEYS_GOTO_PREVIOUSMARKER, NULL,
497 GDK_comma, GEANY_PRIMARY_MOD_MASK, "edit_gotopreviousmarker",
498 _("Go to Pre_vious Marker"), "go_to_previous_marker1");
499 add_kb(group, GEANY_KEYS_GOTO_TAGDEFINITION, NULL,
500 GDK_t, GEANY_PRIMARY_MOD_MASK, "popup_gototagdefinition",
501 _("Go to Tag Definition"), "goto_tag_definition1");
502 add_kb(group, GEANY_KEYS_GOTO_TAGDECLARATION, NULL,
503 GDK_t, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "popup_gototagdeclaration",
504 _("Go to Tag Declaration"), "goto_tag_declaration1");
505 add_kb(group, GEANY_KEYS_GOTO_LINESTART, NULL,
506 GDK_Home, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL);
507 add_kb(group, GEANY_KEYS_GOTO_LINEEND, NULL,
508 GDK_End, 0, "edit_gotolineend", _("Go to End of Line"), NULL);
509 add_kb(group, GEANY_KEYS_GOTO_LINESTARTVISUAL, NULL,
510 GDK_Home, GDK_MOD1_MASK, "edit_gotolinestartvisual", _("Go to Start of Display Line"), NULL);
511 add_kb(group, GEANY_KEYS_GOTO_LINEENDVISUAL, NULL,
512 GDK_End, GDK_MOD1_MASK, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL);
513 add_kb(group, GEANY_KEYS_GOTO_PREVWORDPART, NULL,
514 GDK_slash, GEANY_PRIMARY_MOD_MASK, "edit_prevwordstart", _("Go to Previous Word Part"), NULL);
515 add_kb(group, GEANY_KEYS_GOTO_NEXTWORDPART, NULL,
516 GDK_backslash, GEANY_PRIMARY_MOD_MASK, "edit_nextwordstart", _("Go to Next Word Part"), NULL);
518 group = keybindings_get_core_group(GEANY_KEY_GROUP_VIEW);
520 add_kb(group, GEANY_KEYS_VIEW_TOGGLEALL, NULL,
521 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
522 "menu_toggle_all_additional_widgets1");
523 add_kb(group, GEANY_KEYS_VIEW_FULLSCREEN, cb_func_menu_fullscreen,
524 GDK_F11, 0, "menu_fullscreen", _("Fullscreen"), "menu_fullscreen1");
525 add_kb(group, GEANY_KEYS_VIEW_MESSAGEWINDOW, cb_func_menu_messagewindow,
526 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
527 "menu_show_messages_window1");
528 add_kb(group, GEANY_KEYS_VIEW_SIDEBAR, NULL,
529 0, 0, "toggle_sidebar", _("Toggle Sidebar"), "menu_show_sidebar1");
530 add_kb(group, GEANY_KEYS_VIEW_ZOOMIN, NULL,
531 GDK_plus, GEANY_PRIMARY_MOD_MASK, "menu_zoomin", _("Zoom In"), "menu_zoom_in1");
532 add_kb(group, GEANY_KEYS_VIEW_ZOOMOUT, NULL,
533 GDK_minus, GEANY_PRIMARY_MOD_MASK, "menu_zoomout", _("Zoom Out"), "menu_zoom_out1");
534 add_kb(group, GEANY_KEYS_VIEW_ZOOMRESET, NULL,
535 GDK_0, GEANY_PRIMARY_MOD_MASK, "normal_size", _("Zoom Reset"), "normal_size1");
537 group = keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS);
539 add_kb(group, GEANY_KEYS_FOCUS_EDITOR, NULL,
540 GDK_F2, 0, "switch_editor", _("Switch to Editor"), NULL);
541 add_kb(group, GEANY_KEYS_FOCUS_SEARCHBAR, NULL,
542 GDK_F7, 0, "switch_search_bar", _("Switch to Search Bar"), NULL);
543 add_kb(group, GEANY_KEYS_FOCUS_MESSAGE_WINDOW, NULL,
544 0, 0, "switch_message_window", _("Switch to Message Window"), NULL);
545 add_kb(group, GEANY_KEYS_FOCUS_COMPILER, NULL,
546 0, 0, "switch_compiler", _("Switch to Compiler"), NULL);
547 add_kb(group, GEANY_KEYS_FOCUS_MESSAGES, NULL,
548 0, 0, "switch_messages", _("Switch to Messages"), NULL);
549 add_kb(group, GEANY_KEYS_FOCUS_SCRIBBLE, NULL,
550 GDK_F6, 0, "switch_scribble", _("Switch to Scribble"), NULL);
551 add_kb(group, GEANY_KEYS_FOCUS_VTE, NULL,
552 GDK_F4, 0, "switch_vte", _("Switch to VTE"), NULL);
553 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR, NULL,
554 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL);
555 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST, NULL,
556 0, 0, "switch_sidebar_symbol_list", _("Switch to Sidebar Symbol List"), NULL);
557 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST, NULL,
558 0, 0, "switch_sidebar_doc_list", _("Switch to Sidebar Document List"), NULL);
560 group = keybindings_get_core_group(GEANY_KEY_GROUP_NOTEBOOK);
562 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT, cb_func_switch_tableft,
563 GDK_Page_Up, GEANY_PRIMARY_MOD_MASK, "switch_tableft", _("Switch to left document"), NULL);
564 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
565 GDK_Page_Down, GEANY_PRIMARY_MOD_MASK, "switch_tabright", _("Switch to right document"), NULL);
566 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
567 GDK_Tab, GEANY_PRIMARY_MOD_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
568 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
569 GDK_Page_Up, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "move_tableft",
570 _("Move document left"), NULL);
571 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
572 GDK_Page_Down, GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK, "move_tabright",
573 _("Move document right"), NULL);
574 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABFIRST, cb_func_move_tab,
575 0, 0, "move_tabfirst", _("Move document first"), NULL);
576 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABLAST, cb_func_move_tab,
577 0, 0, "move_tablast", _("Move document last"), NULL);
579 group = keybindings_get_core_group(GEANY_KEY_GROUP_DOCUMENT);
581 add_kb(group, GEANY_KEYS_DOCUMENT_LINEWRAP, NULL,
582 0, 0, "menu_linewrap", _("Toggle Line wrapping"), "menu_line_wrapping1");
583 add_kb(group, GEANY_KEYS_DOCUMENT_LINEBREAK, NULL,
584 0, 0, "menu_linebreak", _("Toggle Line breaking"), "line_breaking1");
585 add_kb(group, GEANY_KEYS_DOCUMENT_CLONE, NULL,
586 0, 0, "menu_clone", _("_Clone"), "clone1");
587 add_kb(group, GEANY_KEYS_DOCUMENT_REPLACETABS, NULL,
588 0, 0, "menu_replacetabs", _("Replace tabs with space"), "menu_replace_tabs");
589 add_kb(group, GEANY_KEYS_DOCUMENT_REPLACESPACES, NULL,
590 0, 0, "menu_replacespaces", _("Replace spaces with tabs"), "menu_replace_spaces");
591 add_kb(group, GEANY_KEYS_DOCUMENT_TOGGLEFOLD, NULL,
592 0, 0, "menu_togglefold", _("Toggle current fold"), NULL);
593 add_kb(group, GEANY_KEYS_DOCUMENT_FOLDALL, NULL,
594 0, 0, "menu_foldall", _("Fold all"), "menu_fold_all1");
595 add_kb(group, GEANY_KEYS_DOCUMENT_UNFOLDALL, NULL,
596 0, 0, "menu_unfoldall", _("Unfold all"), "menu_unfold_all1");
597 add_kb(group, GEANY_KEYS_DOCUMENT_RELOADTAGLIST, NULL,
598 GDK_r, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "reloadtaglist", _("Reload symbol list"), NULL);
599 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS, NULL,
600 0, 0, "remove_markers", _("Remove Markers"), "remove_markers1");
601 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS, NULL,
602 0, 0, "remove_error_indicators", _("Remove Error Indicators"), "menu_remove_indicators1");
603 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS, NULL,
604 0, 0, "remove_markers_and_indicators", _("Remove Markers and Error Indicators"), NULL);
606 group = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
608 add_kb(group, GEANY_KEYS_BUILD_COMPILE, NULL,
609 GDK_F8, 0, "build_compile", _("Compile"), NULL);
610 add_kb(group, GEANY_KEYS_BUILD_LINK, NULL,
611 GDK_F9, 0, "build_link", _("Build"), NULL);
612 add_kb(group, GEANY_KEYS_BUILD_MAKE, NULL,
613 GDK_F9, GDK_SHIFT_MASK, "build_make", _("Make all"), NULL);
614 add_kb(group, GEANY_KEYS_BUILD_MAKEOWNTARGET, NULL,
615 GDK_F9, GDK_SHIFT_MASK | GEANY_PRIMARY_MOD_MASK, "build_makeowntarget",
616 _("Make custom target"), NULL);
617 add_kb(group, GEANY_KEYS_BUILD_MAKEOBJECT, NULL,
618 GDK_F8, GDK_SHIFT_MASK, "build_makeobject", _("Make object"), NULL);
619 add_kb(group, GEANY_KEYS_BUILD_NEXTERROR, NULL,
620 0, 0, "build_nexterror", _("Next error"), NULL);
621 add_kb(group, GEANY_KEYS_BUILD_PREVIOUSERROR, NULL,
622 0, 0, "build_previouserror", _("Previous error"), NULL);
623 add_kb(group, GEANY_KEYS_BUILD_RUN, NULL,
624 GDK_F5, 0, "build_run", _("Run"), NULL);
625 add_kb(group, GEANY_KEYS_BUILD_OPTIONS, NULL,
626 0, 0, "build_options", _("Build options"), NULL);
628 group = keybindings_get_core_group(GEANY_KEY_GROUP_TOOLS);
630 add_kb(group, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER, cb_func_menu_opencolorchooser,
631 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), "menu_choose_color1");
633 group = keybindings_get_core_group(GEANY_KEY_GROUP_HELP);
635 add_kb(group, GEANY_KEYS_HELP_HELP, cb_func_menu_help,
636 GDK_F1, 0, "menu_help", _("Help"), "help1");
640 void keybindings_init(void)
642 memset(binding_ids, 0, sizeof binding_ids);
643 keybinding_groups = g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT);
644 kb_accel_group = gtk_accel_group_new();
646 init_default_kb();
647 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), kb_accel_group);
649 g_signal_connect(main_widgets.window, "key-press-event", G_CALLBACK(on_key_press_event), NULL);
653 typedef void (*KBItemCallback) (GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data);
655 static void keybindings_foreach(KBItemCallback cb, gpointer user_data)
657 gsize g, i;
658 GeanyKeyGroup *group;
659 GeanyKeyBinding *kb;
661 foreach_ptr_array(group, g, keybinding_groups)
663 foreach_ptr_array(kb, i, group->key_items)
664 cb(group, kb, user_data);
669 static void load_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
671 GKeyFile *config = user_data;
672 gchar *val;
673 guint key;
674 GdkModifierType mods;
676 val = g_key_file_get_string(config, group->name, kb->name, NULL);
677 if (val != NULL)
679 gtk_accelerator_parse(val, &key, &mods);
680 kb->key = key;
681 kb->mods = mods;
682 g_free(val);
687 static void load_user_kb(void)
689 gchar *configfile = g_build_filename(app->configdir, "keybindings.conf", NULL);
690 GKeyFile *config = g_key_file_new();
692 /* backwards compatibility with Geany 0.21 defaults */
693 if (!g_file_test(configfile, G_FILE_TEST_EXISTS))
695 gchar *geanyconf = g_build_filename(app->configdir, "geany.conf", NULL);
696 const gchar data[] = "[Bindings]\n"
697 "popup_gototagdefinition=\n"
698 "edit_transposeline=<Control>t\n"
699 "edit_movelineup=\n"
700 "edit_movelinedown=\n"
701 "move_tableft=<Alt>Page_Up\n"
702 "move_tabright=<Alt>Page_Down\n";
704 utils_write_file(configfile, g_file_test(geanyconf, G_FILE_TEST_EXISTS) ?
705 data : "");
706 g_free(geanyconf);
709 /* now load user defined keys */
710 if (g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
712 keybindings_foreach(load_kb, config);
714 g_free(configfile);
715 g_key_file_free(config);
719 static void apply_kb_accel(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
721 if (kb->key != 0 && kb->menu_item)
723 gtk_widget_add_accelerator(kb->menu_item, "activate", kb_accel_group,
724 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
729 void keybindings_load_keyfile(void)
731 load_user_kb();
732 add_popup_menu_accels();
734 /* set menu accels now, after user keybindings have been read */
735 keybindings_foreach(apply_kb_accel, NULL);
739 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id, GtkWidget *menuitem)
741 GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
743 if (kb->key != 0)
744 gtk_widget_add_accelerator(menuitem, "activate", kb_accel_group,
745 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
749 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
750 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
752 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
753 /* FIXME: update those during runtime */
754 static void add_popup_menu_accels(void)
756 GeanyKeyGroup *group;
758 group = keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR);
759 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO, undo1);
760 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO, redo1);
761 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION, context_action1);
763 group = keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD);
764 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_CUT, cut1);
765 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_COPY, copy1);
766 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_PASTE, paste1);
768 group = keybindings_get_core_group(GEANY_KEY_GROUP_SELECT);
769 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL, menu_select_all2);
771 group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
772 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE, insert_date_custom2);
773 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_ALTWHITESPACE, insert_alternative_white_space2);
775 group = keybindings_get_core_group(GEANY_KEY_GROUP_FILE);
776 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED, menu_open_selected_file2);
778 group = keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH);
779 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE, find_usage2);
780 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, find_document_usage2);
782 group = keybindings_get_core_group(GEANY_KEY_GROUP_GOTO);
783 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION, goto_tag_definition2);
785 /* Format and Commands share the menu bar submenus */
786 /* Build menu items are set if the build menus are created */
790 static void set_keyfile_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
792 GKeyFile *config = user_data;
793 gchar *val;
795 val = gtk_accelerator_name(kb->key, kb->mods);
796 g_key_file_set_string(config, group->name, kb->name, val);
797 g_free(val);
801 /* just write the content of the keys array to the config file */
802 void keybindings_write_to_file(void)
804 gchar *configfile = g_build_filename(app->configdir, "keybindings.conf", NULL);
805 gchar *data;
806 GKeyFile *config = g_key_file_new();
808 g_key_file_load_from_file(config, configfile, 0, NULL);
809 keybindings_foreach(set_keyfile_kb, config);
811 /* write the file */
812 data = g_key_file_to_data(config, NULL, NULL);
813 utils_write_file(configfile, data);
815 g_free(data);
816 g_free(configfile);
817 g_key_file_free(config);
821 void keybindings_free(void)
823 GeanyKeyGroup *group;
824 gsize g;
826 foreach_ptr_array(group, g, keybinding_groups)
827 keybindings_free_group(group);
829 g_ptr_array_free(keybinding_groups, TRUE);
833 gchar *keybindings_get_label(GeanyKeyBinding *kb)
835 return utils_str_remove_chars(g_strdup(kb->label), "_");
839 static void fill_shortcut_labels_treeview(GtkWidget *tree)
841 gsize g, i;
842 GeanyKeyBinding *kb;
843 GeanyKeyGroup *group;
844 GtkListStore *store;
845 GtkTreeIter iter;
847 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, PANGO_TYPE_WEIGHT);
849 foreach_ptr_array(group, g, keybinding_groups)
851 if (g > 0)
853 gtk_list_store_append(store, &iter);
854 gtk_list_store_set(store, &iter, -1);
856 gtk_list_store_append(store, &iter);
857 gtk_list_store_set(store, &iter, 0, group->label, 2, PANGO_WEIGHT_BOLD, -1);
859 foreach_ptr_array(kb, i, group->key_items)
861 gchar *shortcut, *label;
863 label = keybindings_get_label(kb);
864 shortcut = gtk_accelerator_get_label(kb->key, kb->mods);
866 gtk_list_store_append(store, &iter);
867 gtk_list_store_set(store, &iter, 0, label, 1, shortcut, 2, PANGO_WEIGHT_NORMAL, -1);
869 g_free(shortcut);
870 g_free(label);
873 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
874 g_object_unref(store);
878 static GtkWidget *create_dialog(void)
880 GtkWidget *dialog, *tree, *label, *swin, *vbox;
881 GtkCellRenderer *text_renderer;
882 GtkTreeViewColumn *column;
884 dialog = gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets.window),
885 GTK_DIALOG_DESTROY_WITH_PARENT,
886 GTK_STOCK_EDIT, GTK_RESPONSE_APPLY,
887 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
888 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
889 gtk_box_set_spacing(GTK_BOX(vbox), 6);
890 gtk_widget_set_name(dialog, "GeanyDialog");
892 gtk_window_set_default_size(GTK_WINDOW(dialog), -1, GEANY_DEFAULT_DIALOG_HEIGHT);
894 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
896 label = gtk_label_new(_("The following keyboard shortcuts are configurable:"));
897 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
899 tree = gtk_tree_view_new();
900 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
901 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
903 text_renderer = gtk_cell_renderer_text_new();
904 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
905 column = gtk_tree_view_column_new_with_attributes(
906 NULL, text_renderer, "text", 0, "weight", 2, NULL);
907 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
909 text_renderer = gtk_cell_renderer_text_new();
910 column = gtk_tree_view_column_new_with_attributes(NULL, text_renderer, "text", 1, NULL);
911 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
913 fill_shortcut_labels_treeview(tree);
915 swin = gtk_scrolled_window_new(NULL, NULL);
916 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_NEVER,
917 GTK_POLICY_AUTOMATIC);
918 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_IN);
919 gtk_container_add(GTK_CONTAINER(swin), tree);
921 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
922 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
924 return dialog;
928 static void key_dialog_show_prefs(void)
930 GtkWidget *wid;
932 prefs_show_dialog();
933 /* select the KB page */
934 wid = ui_lookup_widget(ui_widgets.prefs_dialog, "frame22");
935 if (wid != NULL)
937 GtkNotebook *nb = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
938 if (nb != NULL)
940 gtk_notebook_set_current_page(nb, gtk_notebook_page_num(nb, wid));
946 void keybindings_dialog_show_prefs_scroll(const gchar *name)
948 key_dialog_show_prefs();
949 prefs_kb_search_name(name);
953 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
954 static GtkWidget *key_dialog = NULL;
956 static void on_dialog_response(GtkWidget *dialog, gint response, gpointer user_data)
958 if (response == GTK_RESPONSE_APPLY)
960 key_dialog_show_prefs();
962 gtk_widget_destroy(dialog);
963 key_dialog = NULL;
967 void keybindings_show_shortcuts(void)
969 if (key_dialog)
970 gtk_widget_destroy(key_dialog); /* in case the key_dialog is still visible */
972 key_dialog = create_dialog();
973 g_signal_connect(key_dialog, "response", G_CALLBACK(on_dialog_response), NULL);
974 gtk_widget_show_all(key_dialog);
978 static gboolean check_fixed_kb(guint keyval, guint state)
980 /* check alt-0 to alt-9 for setting current notebook page */
981 if (state == GDK_MOD1_MASK && keyval >= GDK_0 && keyval <= GDK_9)
983 gint page = keyval - GDK_0 - 1;
984 gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
986 /* alt-0 is for the rightmost tab */
987 if (keyval == GDK_0)
988 page = npages - 1;
989 /* invert the order if tabs are added on the other side */
990 if (swap_alt_tab_order && ! file_prefs.tab_order_ltr)
991 page = (npages - 1) - page;
993 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
994 return TRUE;
996 /* note: these are now overridden by default with move tab bindings */
997 if (keyval == GDK_Page_Up || keyval == GDK_Page_Down)
999 /* switch to first or last document */
1000 if (state == (GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK))
1002 if (keyval == GDK_Page_Up)
1003 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), 0);
1004 if (keyval == GDK_Page_Down)
1005 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), -1);
1006 return TRUE;
1009 return FALSE;
1013 static gboolean check_snippet_completion(GeanyDocument *doc)
1015 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1017 g_return_val_if_fail(doc, FALSE);
1019 /* keybinding only valid when scintilla widget has focus */
1020 if (focusw == GTK_WIDGET(doc->editor->sci))
1022 ScintillaObject *sci = doc->editor->sci;
1023 gint pos = sci_get_current_position(sci);
1025 if (editor_prefs.complete_snippets)
1026 return editor_complete_snippet(doc->editor, pos);
1028 return FALSE;
1032 /* Transforms a GdkEventKey event into a GdkEventButton event */
1033 static void trigger_button_event(GtkWidget *widget, guint32 event_time)
1035 GdkEventButton *event;
1036 gboolean ret;
1038 event = g_new0(GdkEventButton, 1);
1040 if (GTK_IS_TEXT_VIEW(widget))
1041 event->window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT);
1042 else
1043 event->window = gtk_widget_get_window(widget);
1044 event->time = event_time;
1045 event->type = GDK_BUTTON_PRESS;
1046 event->button = 3;
1048 g_signal_emit_by_name(widget, "button-press-event", event, &ret);
1049 g_signal_emit_by_name(widget, "button-release-event", event, &ret);
1051 g_free(event);
1055 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
1056 * widgets. Without this special handling, the notebook tab list of the documents' notebook
1057 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
1058 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1059 * notebook tab list. */
1060 static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
1062 g_return_val_if_fail(doc == NULL || doc->is_valid, FALSE);
1064 if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
1066 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1067 if (doc != NULL)
1069 if (focusw == doc->priv->tag_tree)
1071 trigger_button_event(focusw, event_time);
1072 return TRUE;
1074 if (focusw == GTK_WIDGET(doc->editor->sci))
1076 if (keyval == GDK_Menu)
1077 { /* show editor popup menu */
1078 trigger_button_event(focusw, event_time);
1079 return TRUE;
1081 else
1082 { /* show tab bar menu */
1083 trigger_button_event(main_widgets.notebook, event_time);
1084 return TRUE;
1088 if (focusw == tv.tree_openfiles
1089 || focusw == msgwindow.tree_status
1090 || focusw == msgwindow.tree_compiler
1091 || focusw == msgwindow.tree_msg
1092 || focusw == msgwindow.scribble
1093 #ifdef HAVE_VTE
1094 || (vte_info.have_vte && focusw == vc->vte)
1095 #endif
1098 trigger_button_event(focusw, event_time);
1099 return TRUE;
1102 return FALSE;
1106 #ifdef HAVE_VTE
1107 static gboolean set_sensitive(gpointer widget)
1109 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1110 return FALSE;
1114 static gboolean check_vte(GdkModifierType state, guint keyval)
1116 guint i;
1117 GeanyKeyBinding *kb;
1118 GeanyKeyGroup *group;
1119 GtkWidget *widget;
1121 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != vc->vte)
1122 return FALSE;
1123 /* let VTE copy/paste override any user keybinding */
1124 if (state == (GEANY_PRIMARY_MOD_MASK | GDK_SHIFT_MASK) && (keyval == GDK_c || keyval == GDK_v))
1125 return TRUE;
1126 if (! vc->enable_bash_keys)
1127 return FALSE;
1128 /* prevent menubar flickering: */
1129 if (state == GDK_SHIFT_MASK && (keyval >= GDK_a && keyval <= GDK_z))
1130 return FALSE;
1131 if (state == 0 && (keyval < GDK_F1 || keyval > GDK_F35)) /* e.g. backspace */
1132 return FALSE;
1134 /* make focus commands override any bash commands */
1135 group = keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS);
1136 foreach_ptr_array(kb, i, group->key_items)
1138 if (state == kb->mods && keyval == kb->key)
1139 return FALSE;
1142 /* Temporarily disable the menus to prevent conflicting menu accelerators
1143 * from overriding the VTE bash shortcuts.
1144 * Note: maybe there's a better way of doing this ;-) */
1145 widget = ui_lookup_widget(main_widgets.window, "menubar1");
1146 gtk_widget_set_sensitive(widget, FALSE);
1147 g_idle_add_full(G_PRIORITY_HIGH, set_sensitive, widget, NULL);
1149 widget = main_widgets.editor_menu;
1150 gtk_widget_set_sensitive(widget, FALSE);
1151 g_idle_add(set_sensitive, widget);
1152 return TRUE;
1154 #endif
1157 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1158 static guint key_kp_translate(guint key_in)
1160 switch (key_in)
1162 case GDK_KP_Down:
1163 return GDK_Down;
1164 case GDK_KP_Up:
1165 return GDK_Up;
1166 case GDK_KP_Left:
1167 return GDK_Left;
1168 case GDK_KP_Right:
1169 return GDK_Right;
1170 case GDK_KP_Home:
1171 return GDK_Home;
1172 case GDK_KP_End:
1173 return GDK_End;
1174 case GDK_KP_Page_Up:
1175 return GDK_Page_Up;
1176 case GDK_KP_Page_Down:
1177 return GDK_Page_Down;
1178 case GDK_KP_Delete:
1179 return GDK_Delete;
1180 case GDK_KP_Insert:
1181 return GDK_Insert;
1182 default:
1183 return key_in;
1188 /* Check if event keypress matches keybinding combo */
1189 gboolean keybindings_check_event(GdkEventKey *ev, GeanyKeyBinding *kb)
1191 guint state, keyval;
1193 if (ev->keyval == 0)
1194 return FALSE;
1196 keyval = ev->keyval;
1197 state = keybindings_get_modifiers(ev->state);
1198 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1199 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1200 if (keyval >= GDK_A && keyval <= GDK_Z)
1201 keyval += GDK_a - GDK_A;
1203 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1204 keyval = key_kp_translate(keyval);
1206 return (keyval == kb->key && state == kb->mods);
1210 /* central keypress event handler, almost all keypress events go to this function */
1211 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1213 guint state, keyval;
1214 gsize g, i;
1215 GeanyDocument *doc;
1216 GeanyKeyGroup *group;
1217 GeanyKeyBinding *kb;
1219 if (ev->keyval == 0)
1220 return FALSE;
1222 doc = document_get_current();
1223 if (doc)
1224 document_check_disk_status(doc, FALSE);
1226 keyval = ev->keyval;
1227 state = keybindings_get_modifiers(ev->state);
1228 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1229 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1230 if (keyval >= GDK_A && keyval <= GDK_Z)
1231 keyval += GDK_a - GDK_A;
1233 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1234 keyval = key_kp_translate(keyval);
1236 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1238 /* special cases */
1239 #ifdef HAVE_VTE
1240 if (vte_info.have_vte && check_vte(state, keyval))
1241 return FALSE;
1242 #endif
1243 if (check_menu_key(doc, keyval, state, ev->time))
1244 return TRUE;
1246 foreach_ptr_array(group, g, keybinding_groups)
1248 foreach_ptr_array(kb, i, group->key_items)
1250 if (keyval == kb->key && state == kb->mods)
1252 /* call the corresponding callback function for this shortcut */
1253 if (kb->callback)
1255 kb->callback(kb->id);
1256 return TRUE;
1258 else if (group->callback)
1260 if (group->callback(kb->id))
1261 return TRUE;
1262 else
1263 continue; /* not handled */
1265 g_warning("No callback for keybinding %s: %s!", group->name, kb->name);
1269 /* fixed keybindings can be overridden by user bindings, so check them last */
1270 if (check_fixed_kb(keyval, state))
1271 return TRUE;
1272 return FALSE;
1276 /* group_id must be a core group, e.g. GEANY_KEY_GROUP_EDITOR
1277 * key_id e.g. GEANY_KEYS_EDITOR_CALLTIP */
1278 GeanyKeyBinding *keybindings_lookup_item(guint group_id, guint key_id)
1280 GeanyKeyGroup *group;
1282 g_return_val_if_fail(group_id < GEANY_KEY_GROUP_COUNT, NULL); /* can't use this for plugin groups */
1284 group = keybindings_get_core_group(group_id);
1286 g_return_val_if_fail(group, NULL);
1287 return keybindings_get_item(group, key_id);
1291 /** Mimics a (built-in only) keybinding action.
1292 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1293 * @param group_id @ref GeanyKeyGroupID keybinding group index that contains the @a key_id keybinding.
1294 * @param key_id @ref GeanyKeyBindingID keybinding index. */
1295 GEANY_API_SYMBOL
1296 void keybindings_send_command(guint group_id, guint key_id)
1298 GeanyKeyBinding *kb;
1300 kb = keybindings_lookup_item(group_id, key_id);
1301 if (kb)
1303 if (kb->callback)
1304 kb->callback(key_id);
1305 else
1307 GeanyKeyGroup *group = keybindings_get_core_group(group_id);
1309 if (group->callback)
1310 group->callback(key_id);
1316 /* These are the callback functions, either each group or each shortcut has it's
1317 * own function. */
1320 static gboolean cb_func_file_action(guint key_id)
1322 switch (key_id)
1324 case GEANY_KEYS_FILE_NEW:
1325 document_new_file(NULL, NULL, NULL);
1326 break;
1327 case GEANY_KEYS_FILE_OPEN:
1328 on_open1_activate(NULL, NULL);
1329 break;
1330 case GEANY_KEYS_FILE_OPENSELECTED:
1331 on_menu_open_selected_file1_activate(NULL, NULL);
1332 break;
1333 case GEANY_KEYS_FILE_OPENLASTTAB:
1335 gchar *utf8_filename = g_queue_peek_head(ui_prefs.recent_queue);
1336 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1337 document_open_file(locale_filename, FALSE, NULL, NULL);
1338 g_free(locale_filename);
1339 break;
1341 case GEANY_KEYS_FILE_SAVE:
1342 on_save1_activate(NULL, NULL);
1343 break;
1344 case GEANY_KEYS_FILE_SAVEAS:
1345 on_save_as1_activate(NULL, NULL);
1346 break;
1347 case GEANY_KEYS_FILE_SAVEALL:
1348 on_save_all1_activate(NULL, NULL);
1349 break;
1350 case GEANY_KEYS_FILE_CLOSE:
1351 on_close1_activate(NULL, NULL);
1352 break;
1353 case GEANY_KEYS_FILE_CLOSEALL:
1354 on_close_all1_activate(NULL, NULL);
1355 break;
1356 case GEANY_KEYS_FILE_RELOAD:
1357 on_toolbutton_reload_clicked(NULL, NULL);
1358 break;
1359 case GEANY_KEYS_FILE_PRINT:
1360 on_print1_activate(NULL, NULL);
1361 break;
1362 case GEANY_KEYS_FILE_QUIT:
1363 main_quit();
1364 break;
1366 return TRUE;
1370 static gboolean cb_func_project_action(guint key_id)
1372 switch (key_id)
1374 case GEANY_KEYS_PROJECT_NEW:
1375 on_project_new1_activate(NULL, NULL);
1376 break;
1377 case GEANY_KEYS_PROJECT_OPEN:
1378 on_project_open1_activate(NULL, NULL);
1379 break;
1380 case GEANY_KEYS_PROJECT_CLOSE:
1381 if (app->project)
1382 on_project_close1_activate(NULL, NULL);
1383 break;
1384 case GEANY_KEYS_PROJECT_PROPERTIES:
1385 if (app->project)
1386 on_project_properties1_activate(NULL, NULL);
1387 break;
1389 return TRUE;
1393 static void cb_func_menu_preferences(guint key_id)
1395 switch (key_id)
1397 case GEANY_KEYS_SETTINGS_PREFERENCES:
1398 on_preferences1_activate(NULL, NULL);
1399 break;
1400 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES:
1401 on_plugin_preferences1_activate(NULL, NULL);
1402 break;
1407 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id)
1409 on_help1_activate(NULL, NULL);
1413 static gboolean cb_func_search_action(guint key_id)
1415 GeanyDocument *doc = document_get_current();
1416 ScintillaObject *sci;
1418 /* these work without docs */
1419 switch (key_id)
1421 case GEANY_KEYS_SEARCH_FINDINFILES:
1422 on_find_in_files1_activate(NULL, NULL); return TRUE;
1423 case GEANY_KEYS_SEARCH_NEXTMESSAGE:
1424 on_next_message1_activate(NULL, NULL); return TRUE;
1425 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE:
1426 on_previous_message1_activate(NULL, NULL); return TRUE;
1428 if (!doc)
1429 return TRUE;
1430 sci = doc->editor->sci;
1432 switch (key_id)
1434 case GEANY_KEYS_SEARCH_FIND:
1435 on_find1_activate(NULL, NULL); break;
1436 case GEANY_KEYS_SEARCH_FINDNEXT:
1437 on_find_next1_activate(NULL, NULL); break;
1438 case GEANY_KEYS_SEARCH_FINDPREVIOUS:
1439 on_find_previous1_activate(NULL, NULL); break;
1440 case GEANY_KEYS_SEARCH_FINDPREVSEL:
1441 on_find_prevsel1_activate(NULL, NULL); break;
1442 case GEANY_KEYS_SEARCH_FINDNEXTSEL:
1443 on_find_nextsel1_activate(NULL, NULL); break;
1444 case GEANY_KEYS_SEARCH_REPLACE:
1445 on_replace1_activate(NULL, NULL); break;
1446 case GEANY_KEYS_SEARCH_FINDUSAGE:
1447 on_find_usage1_activate(NULL, NULL); break;
1448 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE:
1449 on_find_document_usage1_activate(NULL, NULL); break;
1450 case GEANY_KEYS_SEARCH_MARKALL:
1452 gchar *text = NULL;
1453 gint pos = sci_get_current_position(sci);
1455 /* clear existing search indicators instead if next to cursor */
1456 if (scintilla_send_message(sci, SCI_INDICATORVALUEAT,
1457 GEANY_INDICATOR_SEARCH, pos) ||
1458 scintilla_send_message(sci, SCI_INDICATORVALUEAT,
1459 GEANY_INDICATOR_SEARCH, MAX(pos - 1, 0)))
1460 text = NULL;
1461 else
1462 text = get_current_word_or_sel(doc, TRUE);
1464 if (sci_has_selection(sci))
1465 search_mark_all(doc, text, GEANY_FIND_MATCHCASE);
1466 else
1467 search_mark_all(doc, text, GEANY_FIND_MATCHCASE | GEANY_FIND_WHOLEWORD);
1469 g_free(text);
1470 break;
1473 return TRUE;
1477 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id)
1479 on_show_color_chooser1_activate(NULL, NULL);
1483 static gboolean cb_func_view_action(guint key_id)
1485 switch (key_id)
1487 case GEANY_KEYS_VIEW_TOGGLEALL:
1488 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
1489 break;
1490 case GEANY_KEYS_VIEW_SIDEBAR:
1491 on_menu_show_sidebar1_toggled(NULL, NULL);
1492 break;
1493 case GEANY_KEYS_VIEW_ZOOMIN:
1494 on_zoom_in1_activate(NULL, NULL);
1495 break;
1496 case GEANY_KEYS_VIEW_ZOOMOUT:
1497 on_zoom_out1_activate(NULL, NULL);
1498 break;
1499 case GEANY_KEYS_VIEW_ZOOMRESET:
1500 on_normal_size1_activate(NULL, NULL);
1501 break;
1502 default:
1503 break;
1505 return TRUE;
1509 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id)
1511 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1512 ui_lookup_widget(main_widgets.window, "menu_fullscreen1"));
1514 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1518 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id)
1520 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1521 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1523 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1527 static gboolean cb_func_build_action(guint key_id)
1529 GtkWidget *item;
1530 BuildMenuItems *menu_items;
1531 GeanyDocument *doc = document_get_current();
1533 if (doc == NULL)
1534 return TRUE;
1536 if (!gtk_widget_is_sensitive(ui_lookup_widget(main_widgets.window, "menu_build1")))
1537 return TRUE;
1539 menu_items = build_get_menu_items(doc->file_type->id);
1540 /* TODO make it a table??*/
1541 switch (key_id)
1543 case GEANY_KEYS_BUILD_COMPILE:
1544 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_COMPILE)];
1545 break;
1546 case GEANY_KEYS_BUILD_LINK:
1547 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_BUILD)];
1548 break;
1549 case GEANY_KEYS_BUILD_MAKE:
1550 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_ALL)];
1551 break;
1552 case GEANY_KEYS_BUILD_MAKEOWNTARGET:
1553 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_CUSTOM)];
1554 break;
1555 case GEANY_KEYS_BUILD_MAKEOBJECT:
1556 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)];
1557 break;
1558 case GEANY_KEYS_BUILD_NEXTERROR:
1559 item = menu_items->menu_item[GBG_FIXED][GBF_NEXT_ERROR];
1560 break;
1561 case GEANY_KEYS_BUILD_PREVIOUSERROR:
1562 item = menu_items->menu_item[GBG_FIXED][GBF_PREV_ERROR];
1563 break;
1564 case GEANY_KEYS_BUILD_RUN:
1565 item = menu_items->menu_item[GEANY_GBG_EXEC][GBO_TO_CMD(GEANY_GBO_EXEC)];
1566 break;
1567 case GEANY_KEYS_BUILD_OPTIONS:
1568 item = menu_items->menu_item[GBG_FIXED][GBF_COMMANDS];
1569 break;
1570 default:
1571 item = NULL;
1573 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1574 * sensitive state, but some other menus don't update the sensitive status until
1575 * they are redrawn. */
1576 if (item && gtk_widget_is_sensitive(item))
1577 gtk_menu_item_activate(GTK_MENU_ITEM(item));
1578 return TRUE;
1582 static gboolean read_current_word(GeanyDocument *doc, gboolean sci_word)
1584 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1586 if (sci_word)
1588 editor_find_current_word_sciwc(doc->editor, -1,
1589 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
1591 else
1593 editor_find_current_word(doc->editor, -1,
1594 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1597 return (*editor_info.current_word != 0);
1601 static gboolean check_current_word(GeanyDocument *doc, gboolean sci_word)
1603 if (! read_current_word(doc, sci_word))
1605 utils_beep();
1606 return FALSE;
1608 return TRUE;
1612 static gchar *get_current_word_or_sel(GeanyDocument *doc, gboolean sci_word)
1614 ScintillaObject *sci = doc->editor->sci;
1616 if (sci_has_selection(sci))
1617 return sci_get_selection_contents(sci);
1619 return read_current_word(doc, sci_word) ? g_strdup(editor_info.current_word) : NULL;
1623 static void focus_sidebar(void)
1625 if (ui_prefs.sidebar_visible)
1627 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook));
1628 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), page_num);
1630 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1631 gtk_widget_child_focus(page, GTK_DIR_TAB_FORWARD);
1636 static void focus_msgwindow(void)
1638 if (ui_prefs.msgwindow_visible)
1640 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook));
1641 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(msgwindow.notebook), page_num);
1643 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(page)));
1648 static gboolean cb_func_switch_action(guint key_id)
1650 switch (key_id)
1652 case GEANY_KEYS_FOCUS_EDITOR:
1654 GeanyDocument *doc = document_get_current();
1655 if (doc != NULL)
1657 GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
1658 if (gtk_widget_has_focus(sci))
1659 ui_update_statusbar(doc, -1);
1660 else
1661 gtk_widget_grab_focus(sci);
1663 break;
1665 case GEANY_KEYS_FOCUS_SCRIBBLE:
1666 msgwin_switch_tab(MSG_SCRATCH, TRUE);
1667 break;
1668 case GEANY_KEYS_FOCUS_SEARCHBAR:
1669 if (toolbar_prefs.visible)
1671 GtkWidget *search_entry = toolbar_get_widget_child_by_name("SearchEntry");
1672 if (search_entry != NULL)
1673 gtk_widget_grab_focus(search_entry);
1675 break;
1676 case GEANY_KEYS_FOCUS_SIDEBAR:
1677 focus_sidebar();
1678 break;
1679 case GEANY_KEYS_FOCUS_VTE:
1680 msgwin_switch_tab(MSG_VTE, TRUE);
1681 break;
1682 case GEANY_KEYS_FOCUS_COMPILER:
1683 msgwin_switch_tab(MSG_COMPILER, TRUE);
1684 break;
1685 case GEANY_KEYS_FOCUS_MESSAGES:
1686 msgwin_switch_tab(MSG_MESSAGE, TRUE);
1687 break;
1688 case GEANY_KEYS_FOCUS_MESSAGE_WINDOW:
1689 focus_msgwindow();
1690 break;
1691 case GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST:
1692 sidebar_focus_openfiles_tab();
1693 break;
1694 case GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST:
1695 sidebar_focus_symbols_tab();
1696 break;
1698 return TRUE;
1702 static void switch_notebook_page(gint direction)
1704 gint page_count, cur_page, pass;
1705 gboolean parent_is_notebook = FALSE;
1706 GtkNotebook *notebook;
1707 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1709 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1712 parent_is_notebook = GTK_IS_NOTEBOOK(focusw);
1714 while (! parent_is_notebook && (focusw = gtk_widget_get_parent(focusw)) != NULL);
1716 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1717 if (parent_is_notebook)
1718 notebook = GTK_NOTEBOOK(focusw);
1719 else
1720 notebook = GTK_NOTEBOOK(main_widgets.notebook);
1722 /* now switch pages */
1723 page_count = gtk_notebook_get_n_pages(notebook);
1724 cur_page = gtk_notebook_get_current_page(notebook);
1726 /* find the next visible page in the wanted direction, but don't loop
1727 * indefinitely if no pages are visible */
1728 for (pass = 0; pass < page_count; pass++)
1730 GtkWidget *child;
1732 if (direction == GTK_DIR_LEFT)
1734 if (cur_page > 0)
1735 cur_page--;
1736 else
1737 cur_page = page_count - 1;
1739 else if (direction == GTK_DIR_RIGHT)
1741 if (cur_page < page_count - 1)
1742 cur_page++;
1743 else
1744 cur_page = 0;
1747 child = gtk_notebook_get_nth_page (notebook, cur_page);
1748 if (gtk_widget_get_visible (child))
1750 gtk_notebook_set_current_page(notebook, cur_page);
1751 break;
1757 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id)
1759 switch_notebook_page(GTK_DIR_LEFT);
1763 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id)
1765 switch_notebook_page(GTK_DIR_RIGHT);
1769 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id)
1771 notebook_switch_tablastused();
1775 /* move document left/right/first/last */
1776 static void cb_func_move_tab(guint key_id)
1778 GtkWidget *child;
1779 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
1780 gint cur_page = gtk_notebook_get_current_page(nb);
1782 if (cur_page < 0)
1783 return;
1785 child = gtk_notebook_get_nth_page(nb, cur_page);
1787 switch (key_id)
1789 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT:
1790 gtk_notebook_reorder_child(nb, child, cur_page - 1); /* notebook wraps around by default */
1791 break;
1792 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT:
1794 gint npage = cur_page + 1;
1796 if (npage == gtk_notebook_get_n_pages(nb))
1797 npage = 0; /* wraparound */
1798 gtk_notebook_reorder_child(nb, child, npage);
1799 break;
1801 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST:
1802 gtk_notebook_reorder_child(nb, child, (file_prefs.tab_order_ltr) ? 0 : -1);
1803 break;
1804 case GEANY_KEYS_NOTEBOOK_MOVETABLAST:
1805 gtk_notebook_reorder_child(nb, child, (file_prefs.tab_order_ltr) ? -1 : 0);
1806 break;
1808 return;
1812 static void goto_matching_brace(GeanyDocument *doc)
1814 gint pos, new_pos;
1815 gint after_brace;
1817 g_return_if_fail(DOC_VALID(doc));
1819 pos = sci_get_current_position(doc->editor->sci);
1820 after_brace = pos > 0 && utils_isbrace(sci_get_char_at(doc->editor->sci, pos - 1), TRUE);
1821 pos -= after_brace; /* set pos to the brace */
1823 new_pos = sci_find_matching_brace(doc->editor->sci, pos);
1824 if (new_pos != -1)
1825 { /* set the cursor at/after the brace */
1826 sci_set_current_position(doc->editor->sci, new_pos + (!after_brace), FALSE);
1827 editor_display_current_line(doc->editor, 0.5F);
1832 static gboolean cb_func_clipboard_action(guint key_id)
1834 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1836 switch (key_id)
1838 case GEANY_KEYS_CLIPBOARD_CUT:
1839 on_cut1_activate(NULL, NULL);
1840 break;
1841 case GEANY_KEYS_CLIPBOARD_COPY:
1842 on_copy1_activate(NULL, NULL);
1843 break;
1844 case GEANY_KEYS_CLIPBOARD_PASTE:
1845 on_paste1_activate(NULL, NULL);
1846 break;
1847 case GEANY_KEYS_CLIPBOARD_COPYLINE:
1848 if (IS_SCINTILLA(focusw))
1849 sci_send_command(SCINTILLA(focusw), SCI_LINECOPY);
1850 break;
1851 case GEANY_KEYS_CLIPBOARD_CUTLINE:
1852 if (IS_SCINTILLA(focusw))
1853 sci_send_command(SCINTILLA(focusw), SCI_LINECUT);
1854 break;
1856 return TRUE;
1860 static void goto_tag(GeanyDocument *doc, gboolean definition)
1862 gchar *text = get_current_word_or_sel(doc, FALSE);
1864 if (text)
1865 symbols_goto_tag(text, definition);
1866 else
1867 utils_beep();
1869 g_free(text);
1873 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
1874 static gboolean cb_func_goto_action(guint key_id)
1876 gint cur_line;
1877 GeanyDocument *doc = document_get_current();
1879 if (doc == NULL)
1880 return TRUE;
1882 cur_line = sci_get_current_line(doc->editor->sci);
1884 switch (key_id)
1886 case GEANY_KEYS_GOTO_BACK:
1887 navqueue_go_back();
1888 return TRUE;
1889 case GEANY_KEYS_GOTO_FORWARD:
1890 navqueue_go_forward();
1891 return TRUE;
1892 case GEANY_KEYS_GOTO_LINE:
1894 if (toolbar_prefs.visible)
1896 GtkWidget *wid = toolbar_get_widget_child_by_name("GotoEntry");
1898 /* use toolbar item if shown & not in the drop down overflow menu */
1899 if (wid && gtk_widget_get_mapped(wid))
1901 gtk_widget_grab_focus(wid);
1902 return TRUE;
1905 on_go_to_line_activate(NULL, NULL);
1906 return TRUE;
1908 case GEANY_KEYS_GOTO_MATCHINGBRACE:
1909 goto_matching_brace(doc);
1910 return TRUE;
1911 case GEANY_KEYS_GOTO_TOGGLEMARKER:
1913 sci_toggle_marker_at_line(doc->editor->sci, cur_line, 1);
1914 return TRUE;
1916 case GEANY_KEYS_GOTO_NEXTMARKER:
1918 gint mline = sci_marker_next(doc->editor->sci, cur_line + 1, 1 << 1, TRUE);
1920 if (mline != -1)
1922 sci_set_current_line(doc->editor->sci, mline);
1923 editor_display_current_line(doc->editor, 0.5F);
1925 return TRUE;
1927 case GEANY_KEYS_GOTO_PREVIOUSMARKER:
1929 gint mline = sci_marker_previous(doc->editor->sci, cur_line - 1, 1 << 1, TRUE);
1931 if (mline != -1)
1933 sci_set_current_line(doc->editor->sci, mline);
1934 editor_display_current_line(doc->editor, 0.5F);
1936 return TRUE;
1938 case GEANY_KEYS_GOTO_TAGDEFINITION:
1939 goto_tag(doc, TRUE);
1940 return TRUE;
1941 case GEANY_KEYS_GOTO_TAGDECLARATION:
1942 goto_tag(doc, FALSE);
1943 return TRUE;
1945 /* only check editor-sensitive keybindings when editor has focus so home,end still
1946 * work in other widgets */
1947 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
1948 return FALSE;
1950 switch (key_id)
1952 case GEANY_KEYS_GOTO_LINESTART:
1953 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
1954 break;
1955 case GEANY_KEYS_GOTO_LINEEND:
1956 sci_send_command(doc->editor->sci, SCI_LINEEND);
1957 break;
1958 case GEANY_KEYS_GOTO_LINESTARTVISUAL:
1959 sci_send_command(doc->editor->sci, SCI_HOMEDISPLAY);
1960 break;
1961 case GEANY_KEYS_GOTO_LINEENDVISUAL:
1962 sci_send_command(doc->editor->sci, SCI_LINEENDDISPLAY);
1963 break;
1964 case GEANY_KEYS_GOTO_PREVWORDPART:
1965 sci_send_command(doc->editor->sci, SCI_WORDPARTLEFT);
1966 break;
1967 case GEANY_KEYS_GOTO_NEXTWORDPART:
1968 sci_send_command(doc->editor->sci, SCI_WORDPARTRIGHT);
1969 break;
1971 return TRUE;
1975 static void duplicate_lines(GeanyEditor *editor)
1977 if (sci_get_lines_selected(editor->sci) > 1)
1978 { /* ignore extra_line because of selecting lines from the line number column */
1979 editor_select_lines(editor, FALSE);
1980 sci_selection_duplicate(editor->sci);
1982 else if (sci_has_selection(editor->sci))
1983 sci_selection_duplicate(editor->sci);
1984 else
1985 sci_line_duplicate(editor->sci);
1989 static void delete_lines(GeanyEditor *editor)
1991 editor_select_lines(editor, TRUE); /* include last line (like cut lines, copy lines do) */
1992 sci_clear(editor->sci); /* (SCI_LINEDELETE only does 1 line) */
1996 /* common function for editor keybindings, only valid when scintilla has focus. */
1997 static gboolean cb_func_editor_action(guint key_id)
1999 GeanyDocument *doc = document_get_current();
2000 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2002 /* edit keybindings only valid when scintilla widget has focus */
2003 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2004 return FALSE; /* also makes tab work outside editor */
2006 switch (key_id)
2008 case GEANY_KEYS_EDITOR_UNDO:
2009 on_undo1_activate(NULL, NULL);
2010 break;
2011 case GEANY_KEYS_EDITOR_REDO:
2012 on_redo1_activate(NULL, NULL);
2013 break;
2014 case GEANY_KEYS_EDITOR_SCROLLTOLINE:
2015 editor_scroll_to_line(doc->editor, -1, 0.5F);
2016 break;
2017 case GEANY_KEYS_EDITOR_SCROLLLINEUP:
2018 sci_send_command(doc->editor->sci, SCI_LINESCROLLUP);
2019 break;
2020 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN:
2021 sci_send_command(doc->editor->sci, SCI_LINESCROLLDOWN);
2022 break;
2023 case GEANY_KEYS_EDITOR_DUPLICATELINE:
2024 duplicate_lines(doc->editor);
2025 break;
2026 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR:
2027 editor_goto_next_snippet_cursor(doc->editor);
2028 break;
2029 case GEANY_KEYS_EDITOR_DELETELINE:
2030 delete_lines(doc->editor);
2031 break;
2032 case GEANY_KEYS_EDITOR_DELETELINETOEND:
2033 sci_send_command(doc->editor->sci, SCI_DELLINERIGHT);
2034 break;
2035 case GEANY_KEYS_EDITOR_TRANSPOSELINE:
2036 sci_send_command(doc->editor->sci, SCI_LINETRANSPOSE);
2037 break;
2038 case GEANY_KEYS_EDITOR_AUTOCOMPLETE:
2039 editor_start_auto_complete(doc->editor, sci_get_current_position(doc->editor->sci), TRUE);
2040 break;
2041 case GEANY_KEYS_EDITOR_CALLTIP:
2042 editor_show_calltip(doc->editor, -1);
2043 break;
2044 case GEANY_KEYS_EDITOR_CONTEXTACTION:
2045 if (check_current_word(doc, FALSE))
2046 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu,
2047 "context_action1")), NULL);
2048 break;
2049 case GEANY_KEYS_EDITOR_COMPLETESNIPPET:
2050 /* allow tab to be overloaded */
2051 return check_snippet_completion(doc);
2053 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION:
2055 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
2056 GEANY_KEYS_EDITOR_COMPLETESNIPPET);
2058 switch (kb->key)
2060 case GDK_space:
2061 sci_add_text(doc->editor->sci, " ");
2062 break;
2063 case GDK_Tab:
2064 sci_send_command(doc->editor->sci, SCI_TAB);
2065 break;
2066 default:
2067 break;
2069 break;
2071 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION:
2072 return editor_complete_word_part(doc->editor);
2074 case GEANY_KEYS_EDITOR_MOVELINEUP:
2075 sci_move_selected_lines_up(doc->editor->sci);
2076 break;
2077 case GEANY_KEYS_EDITOR_MOVELINEDOWN:
2078 sci_move_selected_lines_down(doc->editor->sci);
2079 break;
2081 return TRUE;
2085 static void join_lines(GeanyEditor *editor)
2087 gint start, end, i;
2089 start = sci_get_line_from_position(editor->sci,
2090 sci_get_selection_start(editor->sci));
2091 end = sci_get_line_from_position(editor->sci,
2092 sci_get_selection_end(editor->sci));
2094 /* remove spaces surrounding the lines so that these spaces
2095 * won't appear within text after joining */
2096 for (i = start; i < end; i++)
2097 editor_strip_line_trailing_spaces(editor, i);
2098 for (i = start + 1; i <= end; i++)
2099 sci_set_line_indentation(editor->sci, i, 0);
2101 sci_set_target_start(editor->sci,
2102 sci_get_position_from_line(editor->sci, start));
2103 sci_set_target_end(editor->sci,
2104 sci_get_position_from_line(editor->sci, end));
2105 sci_lines_join(editor->sci);
2109 static gint get_reflow_column(GeanyEditor *editor)
2111 const GeanyEditorPrefs *eprefs = editor_get_prefs(editor);
2112 if (editor->line_breaking)
2113 return eprefs->line_break_column;
2114 else if (eprefs->long_line_type != 2)
2115 return eprefs->long_line_column;
2116 else
2117 return -1; /* do nothing */
2121 /* Split the line where the cursor is positioned, on `column`,
2122 possibly many times if the line is long.
2123 Return the number of lines added because of the splitting. */
2124 static gint split_line(GeanyEditor *editor, gint column)
2126 ScintillaObject *sci = editor->sci;
2127 gint start_line = sci_get_current_line(sci);
2128 gint line = start_line;
2130 while (TRUE)
2132 gint lstart = sci_get_position_from_line(sci, line);
2133 gint lend = sci_get_line_end_position(sci, line);
2134 gint edge = sci_get_position_from_col(sci, line, column);
2135 gboolean found;
2136 gint pos;
2138 /* don't split on a trailing space of a line */
2139 if (sci_get_char_at(sci, lend - 1) == GDK_space)
2140 lend--;
2142 /* detect when the line is short enough and no more splitting is needed */
2143 if (sci_get_col_from_position(sci, lend) < column)
2144 break;
2146 /* lookup split position */
2147 found = FALSE;
2148 for (pos = edge - 1; pos > lstart; pos--)
2150 if (sci_get_char_at(sci, pos) == GDK_space)
2152 found = TRUE;
2153 break;
2156 if (!found)
2158 for (pos = edge; pos < lend; pos++)
2160 if (sci_get_char_at(sci, pos) == GDK_space)
2162 found = TRUE;
2163 break;
2167 if (!found)
2168 break;
2170 sci_set_current_position(sci, pos + 1, FALSE);
2171 sci_cancel(sci); /* don't select from completion list */
2172 sci_send_command(sci, SCI_NEWLINE);
2173 line++;
2175 return line - start_line;
2179 static void reflow_lines(GeanyEditor *editor, gint column)
2181 gint start, indent, linescount, i;
2183 start = sci_get_line_from_position(editor->sci,
2184 sci_get_selection_start(editor->sci));
2186 /* if several lines are selected, join them. */
2187 if (sci_get_lines_selected(editor->sci) > 1)
2188 join_lines(editor);
2190 /* if this line is short enough, do nothing */
2191 if (column > sci_get_line_end_position(editor->sci, start) -
2192 sci_get_position_from_line(editor->sci, start))
2194 return;
2198 * We have to manipulate line indentation so that indentation
2199 * of the resulting lines would be consistent. For example,
2200 * the result of splitting "[TAB]very long content":
2202 * +-------------+-------------+
2203 * | proper | wrong |
2204 * +-------------+-------------+
2205 * | [TAB]very | [TAB]very |
2206 * | [TAB]long | long |
2207 * | [TAB]content| content |
2208 * +-------------+-------------+
2210 indent = sci_get_line_indentation(editor->sci, start);
2211 sci_set_line_indentation(editor->sci, start, 0);
2213 linescount = split_line(editor, column - indent);
2215 /* Fix indentation. */
2216 for (i = start; i <= start + linescount; i++)
2217 sci_set_line_indentation(editor->sci, i, indent);
2219 /* Remove trailing spaces. */
2220 if (editor_prefs.newline_strip || file_prefs.strip_trailing_spaces)
2222 for (i = start; i <= start + linescount; i++)
2223 editor_strip_line_trailing_spaces(editor, i);
2228 /* deselect last newline of selection, if any */
2229 static void sci_deselect_last_newline(ScintillaObject *sci)
2231 gint start, end;
2233 start = sci_get_selection_start(sci);
2234 end = sci_get_selection_end(sci);
2235 if (end > start && sci_get_col_from_position(sci, end) == 0)
2237 end = sci_get_line_end_position(sci, sci_get_line_from_position(sci, end - 1));
2238 sci_set_selection(sci, start, end);
2243 static void reflow_paragraph(GeanyEditor *editor)
2245 ScintillaObject *sci = editor->sci;
2246 gboolean sel;
2247 gint column;
2249 column = get_reflow_column(editor);
2250 if (column == -1)
2252 utils_beep();
2253 return;
2256 sci_start_undo_action(sci);
2257 sel = sci_has_selection(sci);
2258 if (!sel)
2259 editor_select_indent_block(editor);
2260 sci_deselect_last_newline(sci);
2261 reflow_lines(editor, column);
2262 if (!sel)
2263 sci_set_anchor(sci, -1);
2264 sci_goto_pos(sci, sci_get_line_end_position(sci, sci_get_current_line(sci)), TRUE);
2266 sci_end_undo_action(sci);
2270 static void join_paragraph(GeanyEditor *editor)
2272 ScintillaObject *sci = editor->sci;
2273 gboolean sel;
2274 gint column;
2276 column = get_reflow_column(editor);
2277 if (column == -1)
2279 utils_beep();
2280 return;
2283 sci_start_undo_action(sci);
2284 sel = sci_has_selection(sci);
2285 if (!sel)
2286 editor_select_indent_block(editor);
2287 sci_deselect_last_newline(sci);
2288 join_lines(editor);
2289 if (!sel)
2290 sci_set_anchor(sci, -1);
2292 sci_end_undo_action(sci);
2296 /* common function for format keybindings, only valid when scintilla has focus. */
2297 static gboolean cb_func_format_action(guint key_id)
2299 GeanyDocument *doc = document_get_current();
2300 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2302 /* keybindings only valid when scintilla widget has focus */
2303 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2304 return TRUE;
2306 switch (key_id)
2308 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE:
2309 on_menu_toggle_line_commentation1_activate(NULL, NULL);
2310 break;
2311 case GEANY_KEYS_FORMAT_COMMENTLINE:
2312 on_menu_comment_line1_activate(NULL, NULL);
2313 break;
2314 case GEANY_KEYS_FORMAT_UNCOMMENTLINE:
2315 on_menu_uncomment_line1_activate(NULL, NULL);
2316 break;
2317 case GEANY_KEYS_FORMAT_INCREASEINDENT:
2318 on_menu_increase_indent1_activate(NULL, NULL);
2319 break;
2320 case GEANY_KEYS_FORMAT_DECREASEINDENT:
2321 on_menu_decrease_indent1_activate(NULL, NULL);
2322 break;
2323 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE:
2324 editor_indentation_by_one_space(doc->editor, -1, FALSE);
2325 break;
2326 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE:
2327 editor_indentation_by_one_space(doc->editor, -1, TRUE);
2328 break;
2329 case GEANY_KEYS_FORMAT_AUTOINDENT:
2330 editor_smart_line_indentation(doc->editor, -1);
2331 break;
2332 case GEANY_KEYS_FORMAT_TOGGLECASE:
2333 on_toggle_case1_activate(NULL, NULL);
2334 break;
2335 case GEANY_KEYS_FORMAT_SENDTOCMD1:
2336 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 0)
2337 tools_execute_custom_command(doc, ui_prefs.custom_commands[0]);
2338 break;
2339 case GEANY_KEYS_FORMAT_SENDTOCMD2:
2340 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 1)
2341 tools_execute_custom_command(doc, ui_prefs.custom_commands[1]);
2342 break;
2343 case GEANY_KEYS_FORMAT_SENDTOCMD3:
2344 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 2)
2345 tools_execute_custom_command(doc, ui_prefs.custom_commands[2]);
2346 break;
2347 case GEANY_KEYS_FORMAT_SENDTOVTE:
2348 on_send_selection_to_vte1_activate(NULL, NULL);
2349 break;
2350 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
2351 reflow_paragraph(doc->editor);
2352 break;
2353 case GEANY_KEYS_FORMAT_JOINLINES:
2354 join_paragraph(doc->editor);
2355 break;
2357 return TRUE;
2361 /* common function for select keybindings, valid for scintilla and/or gtk_editable objects. */
2362 static gboolean cb_func_select_action(guint key_id)
2364 GeanyDocument *doc = document_get_current();
2365 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2367 switch (key_id)
2369 case GEANY_KEYS_SELECT_ALL:
2370 on_menu_select_all1_activate(NULL, NULL);
2371 break;
2372 case GEANY_KEYS_SELECT_WORD:
2373 if (doc != NULL)
2374 editor_select_word(doc->editor);
2375 break;
2376 case GEANY_KEYS_SELECT_LINE:
2377 if (doc != NULL)
2378 editor_select_lines(doc->editor, FALSE);
2379 break;
2380 case GEANY_KEYS_SELECT_PARAGRAPH:
2381 if (doc != NULL)
2382 editor_select_paragraph(doc->editor);
2383 break;
2384 case GEANY_KEYS_SELECT_WORDPARTLEFT:
2385 if (IS_SCINTILLA(focusw))
2386 sci_send_command(SCINTILLA(focusw), SCI_WORDPARTLEFTEXTEND);
2387 break;
2388 case GEANY_KEYS_SELECT_WORDPARTRIGHT:
2389 if (IS_SCINTILLA(focusw))
2390 sci_send_command(SCINTILLA(focusw), SCI_WORDPARTRIGHTEXTEND);
2391 break;
2393 return TRUE;
2397 static gboolean cb_func_document_action(guint key_id)
2399 GeanyDocument *doc = document_get_current();
2401 if (doc == NULL)
2402 return TRUE;
2404 switch (key_id)
2406 case GEANY_KEYS_DOCUMENT_REPLACETABS:
2407 on_replace_tabs_activate(NULL, NULL);
2408 break;
2409 case GEANY_KEYS_DOCUMENT_REPLACESPACES:
2410 on_replace_spaces_activate(NULL, NULL);
2411 break;
2412 case GEANY_KEYS_DOCUMENT_LINEBREAK:
2413 on_line_breaking1_activate(NULL, NULL);
2414 ui_document_show_hide(doc);
2415 break;
2416 case GEANY_KEYS_DOCUMENT_LINEWRAP:
2417 on_line_wrapping1_toggled(NULL, NULL);
2418 ui_document_show_hide(doc);
2419 break;
2420 case GEANY_KEYS_DOCUMENT_CLONE:
2421 document_clone(doc);
2422 break;
2423 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST:
2424 document_update_tags(doc);
2425 break;
2426 case GEANY_KEYS_DOCUMENT_FOLDALL:
2427 editor_fold_all(doc->editor);
2428 break;
2429 case GEANY_KEYS_DOCUMENT_UNFOLDALL:
2430 editor_unfold_all(doc->editor);
2431 break;
2432 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD:
2433 if (editor_prefs.folding)
2435 gint line = sci_get_current_line(doc->editor->sci);
2436 editor_toggle_fold(doc->editor, line, 0);
2437 break;
2439 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS:
2440 on_remove_markers1_activate(NULL, NULL);
2441 break;
2442 case GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS:
2443 on_menu_remove_indicators1_activate(NULL, NULL);
2444 break;
2445 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS:
2446 on_remove_markers1_activate(NULL, NULL);
2447 on_menu_remove_indicators1_activate(NULL, NULL);
2448 break;
2450 return TRUE;
2454 static void insert_line_after(GeanyEditor *editor)
2456 ScintillaObject *sci = editor->sci;
2458 sci_send_command(sci, SCI_LINEEND);
2459 sci_send_command(sci, SCI_NEWLINE);
2463 static void insert_line_before(GeanyEditor *editor)
2465 ScintillaObject *sci = editor->sci;
2466 gint line = sci_get_current_line(sci);
2467 gint indentpos = sci_get_line_indent_position(sci, line);
2469 sci_set_current_position(sci, indentpos, TRUE);
2470 sci_send_command(sci, SCI_NEWLINE);
2471 sci_send_command(sci, SCI_LINEUP);
2475 /* common function for insert keybindings, only valid when scintilla has focus. */
2476 static gboolean cb_func_insert_action(guint key_id)
2478 GeanyDocument *doc = document_get_current();
2479 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2481 /* keybindings only valid when scintilla widget has focus */
2482 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2483 return TRUE;
2485 switch (key_id)
2487 case GEANY_KEYS_INSERT_ALTWHITESPACE:
2488 editor_insert_alternative_whitespace(doc->editor);
2489 break;
2490 case GEANY_KEYS_INSERT_DATE:
2491 gtk_menu_item_activate(GTK_MENU_ITEM(
2492 ui_lookup_widget(main_widgets.window, "insert_date_custom1")));
2493 break;
2494 case GEANY_KEYS_INSERT_LINEAFTER:
2495 insert_line_after(doc->editor);
2496 break;
2497 case GEANY_KEYS_INSERT_LINEBEFORE:
2498 insert_line_before(doc->editor);
2499 break;
2501 return TRUE;
2505 /* update key combination */
2506 void keybindings_update_combo(GeanyKeyBinding *kb, guint key, GdkModifierType mods)
2508 GtkWidget *widget = kb->menu_item;
2510 if (widget && kb->key)
2511 gtk_widget_remove_accelerator(widget, kb_accel_group, kb->key, kb->mods);
2513 kb->key = key;
2514 kb->mods = mods;
2516 if (widget && kb->key)
2517 gtk_widget_add_accelerator(widget, "activate", kb_accel_group,
2518 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
2522 /* used for plugins, can be called repeatedly. */
2523 GeanyKeyGroup *keybindings_set_group(GeanyKeyGroup *group, const gchar *section_name,
2524 const gchar *label, gsize count, GeanyKeyGroupCallback callback)
2526 g_return_val_if_fail(section_name, NULL);
2527 g_return_val_if_fail(count, NULL);
2529 /* prevent conflict with core bindings */
2530 g_return_val_if_fail(!g_str_equal(section_name, keybindings_keyfile_group_name), NULL);
2532 if (!group)
2534 group = g_new0(GeanyKeyGroup, 1);
2535 add_kb_group(group, section_name, label, callback, TRUE);
2537 g_free(group->plugin_keys);
2538 group->plugin_keys = g_new0(GeanyKeyBinding, count);
2539 group->plugin_key_count = count;
2540 g_ptr_array_set_size(group->key_items, 0);
2541 return group;
2545 void keybindings_free_group(GeanyKeyGroup *group)
2547 GeanyKeyBinding *kb;
2549 g_ptr_array_free(group->key_items, TRUE);
2551 if (group->plugin)
2553 foreach_c_array(kb, group->plugin_keys, group->plugin_key_count)
2555 g_free(kb->name);
2556 g_free(kb->label);
2558 g_free(group->plugin_keys);
2559 g_ptr_array_remove_fast(keybinding_groups, group);
2560 g_free(group);