Start to make it easier to compile the core in isolation
[geany-mirror.git] / src / keybindings.c
blob2462d7fa654d674980fa70bb8bc895e6012c2f41
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 **/
31 #include "geany.h"
33 #include <gdk/gdkkeysyms.h>
34 #include <string.h>
36 #include "keybindingsprivate.h"
37 #include "support.h"
38 #include "utils.h"
39 #include "ui_utils.h"
40 #include "document.h"
41 #include "documentprivate.h"
42 #include "filetypes.h"
43 #include "callbacks.h"
44 #include "prefs.h"
45 #include "msgwindow.h"
46 #include "editor.h"
47 #include "sciwrappers.h"
48 #include "build.h"
49 #include "tools.h"
50 #include "navqueue.h"
51 #include "symbols.h"
52 #include "vte.h"
53 #include "toolbar.h"
54 #include "sidebar.h"
55 #include "notebook.h"
56 #include "geanywraplabel.h"
57 #include "main.h"
58 #include "search.h"
59 #include "gtkcompat.h"
60 #ifdef HAVE_VTE
61 # include "vte.h"
62 #endif
65 GPtrArray *keybinding_groups; /* array of GeanyKeyGroup pointers, in visual order */
67 /* keyfile group name for non-plugin KB groups */
68 static const gchar keybindings_keyfile_group_name[] = "Bindings";
70 /* core keybindings */
71 static GeanyKeyBinding binding_ids[GEANY_KEYS_COUNT];
73 static GtkAccelGroup *kb_accel_group = NULL;
74 static const gboolean swap_alt_tab_order = FALSE;
77 /* central keypress event handler, almost all keypress events go to this function */
78 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
80 static gboolean check_current_word(GeanyDocument *doc, gboolean sci_word);
81 static gboolean read_current_word(GeanyDocument *doc, gboolean sci_word);
82 static gchar *get_current_word_or_sel(GeanyDocument *doc, gboolean sci_word);
84 static gboolean cb_func_file_action(guint key_id);
85 static gboolean cb_func_project_action(guint key_id);
86 static gboolean cb_func_editor_action(guint key_id);
87 static gboolean cb_func_select_action(guint key_id);
88 static gboolean cb_func_format_action(guint key_id);
89 static gboolean cb_func_insert_action(guint key_id);
90 static gboolean cb_func_search_action(guint key_id);
91 static gboolean cb_func_goto_action(guint key_id);
92 static gboolean cb_func_switch_action(guint key_id);
93 static gboolean cb_func_clipboard_action(guint key_id);
94 static gboolean cb_func_build_action(guint key_id);
95 static gboolean cb_func_document_action(guint key_id);
96 static gboolean cb_func_view_action(guint key_id);
98 /* note: new keybindings should normally use per group callbacks */
99 static void cb_func_menu_help(guint key_id);
100 static void cb_func_menu_preferences(guint key_id);
102 static void cb_func_menu_fullscreen(guint key_id);
103 static void cb_func_menu_messagewindow(guint key_id);
105 static void cb_func_menu_opencolorchooser(guint key_id);
107 static void cb_func_switch_tableft(guint key_id);
108 static void cb_func_switch_tabright(guint key_id);
109 static void cb_func_switch_tablastused(guint key_id);
110 static void cb_func_move_tab(guint key_id);
112 static void add_popup_menu_accels(void);
115 /** Looks up a keybinding item.
116 * @param group Group.
117 * @param key_id Keybinding index for the group.
118 * @return The keybinding.
119 * @since 0.19. */
120 GeanyKeyBinding *keybindings_get_item(GeanyKeyGroup *group, gsize key_id)
122 if (group->plugin)
124 g_assert(key_id < group->plugin_key_count);
125 return &group->plugin_keys[key_id];
127 g_assert(key_id < GEANY_KEYS_COUNT);
128 return &binding_ids[key_id];
132 /* This is used to set default keybindings on startup.
133 * Menu accels are set in apply_kb_accel(). */
134 /** Fills a GeanyKeyBinding struct item.
135 * @note Always set @a key and @a mod to 0, otherwise you will likely
136 * cause conflicts with the user's custom, other plugin's keybindings or
137 * future default keybindings.
138 * @param group Group.
139 * @param key_id Keybinding index for the group.
140 * @param callback Function to call when activated, or @c NULL to use the group callback.
141 * Usually it's better to use the group callback instead - see plugin_set_key_group().
142 * @param key (Lower case) default key, e.g. @c GDK_j, but usually 0 for unset.
143 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
144 * @param kf_name Key name for the configuration file, such as @c "menu_new".
145 * @param label Label used in the preferences dialog keybindings tab. May contain
146 * underscores - these won't be displayed.
147 * @param menu_item Optional widget to set an accelerator for, or @c NULL.
148 * @return The keybinding - normally this is ignored. */
149 GeanyKeyBinding *keybindings_set_item(GeanyKeyGroup *group, gsize key_id,
150 GeanyKeyCallback callback, guint key, GdkModifierType mod,
151 const gchar *kf_name, const gchar *label, GtkWidget *menu_item)
153 GeanyKeyBinding *kb;
155 g_assert(group->name);
156 kb = keybindings_get_item(group, key_id);
157 g_assert(!kb->name);
158 g_ptr_array_add(group->key_items, kb);
160 if (group->plugin)
162 /* some plugins e.g. GeanyLua need these fields duplicated */
163 SETPTR(kb->name, g_strdup(kf_name));
164 SETPTR(kb->label, g_strdup(label));
166 else
168 /* we don't touch these strings unless group->plugin is set, const cast is safe */
169 kb->name = (gchar *)kf_name;
170 kb->label = (gchar *)label;
172 kb->key = key;
173 kb->mods = mod;
174 kb->default_key = key;
175 kb->default_mods = mod;
176 kb->callback = callback;
177 kb->menu_item = menu_item;
178 kb->id = key_id;
179 return kb;
183 static void add_kb_group(GeanyKeyGroup *group,
184 const gchar *name, const gchar *label, GeanyKeyGroupCallback callback, gboolean plugin)
186 g_ptr_array_add(keybinding_groups, group);
188 group->name = name;
189 group->label = label;
190 group->callback = callback;
191 group->plugin = plugin;
192 group->key_items = g_ptr_array_new();
196 GeanyKeyGroup *keybindings_get_core_group(guint id)
198 static GeanyKeyGroup groups[GEANY_KEY_GROUP_COUNT];
200 g_return_val_if_fail(id < GEANY_KEY_GROUP_COUNT, NULL);
202 return &groups[id];
206 static void add_kb(GeanyKeyGroup *group, gsize key_id,
207 GeanyKeyCallback callback, guint key, GdkModifierType mod,
208 const gchar *kf_name, const gchar *label, const gchar *widget_name)
210 GtkWidget *widget = widget_name ?
211 ui_lookup_widget(main_widgets.window, widget_name) : NULL;
213 keybindings_set_item(group, key_id, callback,
214 key, mod, kf_name, label, widget);
218 #define ADD_KB_GROUP(group_id, label, callback) \
219 add_kb_group(keybindings_get_core_group(group_id),\
220 keybindings_keyfile_group_name, label, callback, FALSE)
222 static void init_default_kb(void)
224 GeanyKeyGroup *group;
226 /* visual group order */
227 ADD_KB_GROUP(GEANY_KEY_GROUP_FILE, _("File"), cb_func_file_action);
228 ADD_KB_GROUP(GEANY_KEY_GROUP_EDITOR, _("Editor"), cb_func_editor_action);
229 ADD_KB_GROUP(GEANY_KEY_GROUP_CLIPBOARD, _("Clipboard"), cb_func_clipboard_action);
230 ADD_KB_GROUP(GEANY_KEY_GROUP_SELECT, _("Select"), cb_func_select_action);
231 ADD_KB_GROUP(GEANY_KEY_GROUP_FORMAT, _("Format"), cb_func_format_action);
232 ADD_KB_GROUP(GEANY_KEY_GROUP_INSERT, _("Insert"), cb_func_insert_action);
233 ADD_KB_GROUP(GEANY_KEY_GROUP_SETTINGS, _("Settings"), NULL);
234 ADD_KB_GROUP(GEANY_KEY_GROUP_SEARCH, _("Search"), cb_func_search_action);
235 ADD_KB_GROUP(GEANY_KEY_GROUP_GOTO, _("Go to"), cb_func_goto_action);
236 ADD_KB_GROUP(GEANY_KEY_GROUP_VIEW, _("View"), cb_func_view_action);
237 ADD_KB_GROUP(GEANY_KEY_GROUP_DOCUMENT, _("Document"), cb_func_document_action);
238 ADD_KB_GROUP(GEANY_KEY_GROUP_PROJECT, _("Project"), cb_func_project_action);
239 ADD_KB_GROUP(GEANY_KEY_GROUP_BUILD, _("Build"), cb_func_build_action);
240 ADD_KB_GROUP(GEANY_KEY_GROUP_TOOLS, _("Tools"), NULL);
241 ADD_KB_GROUP(GEANY_KEY_GROUP_HELP, _("Help"), NULL);
242 ADD_KB_GROUP(GEANY_KEY_GROUP_FOCUS, _("Focus"), cb_func_switch_action);
243 ADD_KB_GROUP(GEANY_KEY_GROUP_NOTEBOOK, _("Notebook tab"), NULL);
245 /* Init all fields of keys with default values.
246 * The menu_item field is always the main menu item, popup menu accelerators are
247 * set in add_popup_menu_accels(). */
249 group = keybindings_get_core_group(GEANY_KEY_GROUP_FILE);
251 add_kb(group, GEANY_KEYS_FILE_NEW, NULL,
252 GDK_n, GDK_CONTROL_MASK, "menu_new", _("New"), "menu_new1");
253 add_kb(group, GEANY_KEYS_FILE_OPEN, NULL,
254 GDK_o, GDK_CONTROL_MASK, "menu_open", _("Open"), "menu_open1");
255 add_kb(group, GEANY_KEYS_FILE_OPENSELECTED, NULL,
256 GDK_o, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_open_selected",
257 _("Open selected file"), "menu_open_selected_file1");
258 add_kb(group, GEANY_KEYS_FILE_SAVE, NULL,
259 GDK_s, GDK_CONTROL_MASK, "menu_save", _("Save"), "menu_save1");
260 add_kb(group, GEANY_KEYS_FILE_SAVEAS, NULL,
261 0, 0, "menu_saveas", _("Save as"), "menu_save_as1");
262 add_kb(group, GEANY_KEYS_FILE_SAVEALL, NULL,
263 GDK_s, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_saveall", _("Save all"),
264 "menu_save_all1");
265 add_kb(group, GEANY_KEYS_FILE_PRINT, NULL,
266 GDK_p, GDK_CONTROL_MASK, "menu_print", _("Print"), "print1");
267 add_kb(group, GEANY_KEYS_FILE_CLOSE, NULL,
268 GDK_w, GDK_CONTROL_MASK, "menu_close", _("Close"), "menu_close1");
269 add_kb(group, GEANY_KEYS_FILE_CLOSEALL, NULL,
270 GDK_w, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_closeall", _("Close all"),
271 "menu_close_all1");
272 add_kb(group, GEANY_KEYS_FILE_RELOAD, NULL,
273 GDK_r, GDK_CONTROL_MASK, "menu_reloadfile", _("Reload file"), "menu_reload1");
274 add_kb(group, GEANY_KEYS_FILE_OPENLASTTAB, NULL,
275 0, 0, "file_openlasttab", _("Re-open last closed tab"), NULL);
276 add_kb(group, GEANY_KEYS_FILE_QUIT, NULL,
277 GDK_q, GDK_CONTROL_MASK, "menu_quit", _("Quit"), "menu_quit1");
279 group = keybindings_get_core_group(GEANY_KEY_GROUP_PROJECT);
281 add_kb(group, GEANY_KEYS_PROJECT_NEW, NULL,
282 0, 0, "project_new", _("New"), "project_new1");
283 add_kb(group, GEANY_KEYS_PROJECT_OPEN, NULL,
284 0, 0, "project_open", _("Open"), "project_open1");
285 add_kb(group, GEANY_KEYS_PROJECT_PROPERTIES, NULL,
286 0, 0, "project_properties",
287 ui_lookup_stock_label(GTK_STOCK_PROPERTIES), "project_properties1");
288 add_kb(group, GEANY_KEYS_PROJECT_CLOSE, NULL,
289 0, 0, "project_close", _("Close"), "project_close1");
291 group = keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR);
293 add_kb(group, GEANY_KEYS_EDITOR_UNDO, NULL,
294 GDK_z, GDK_CONTROL_MASK, "menu_undo", _("Undo"), "menu_undo2");
295 add_kb(group, GEANY_KEYS_EDITOR_REDO, NULL,
296 GDK_y, GDK_CONTROL_MASK, "menu_redo", _("Redo"), "menu_redo2");
297 add_kb(group, GEANY_KEYS_EDITOR_DUPLICATELINE, NULL,
298 GDK_d, GDK_CONTROL_MASK, "edit_duplicateline", _("D_uplicate Line or Selection"),
299 "duplicate_line_or_selection1");
300 add_kb(group, GEANY_KEYS_EDITOR_DELETELINE, NULL,
301 GDK_k, GDK_CONTROL_MASK, "edit_deleteline", _("_Delete Current Line(s)"),
302 "delete_current_lines1");
303 add_kb(group, GEANY_KEYS_EDITOR_DELETELINETOEND, NULL,
304 GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_deletelinetoend",
305 _("Delete to line end"), NULL);
306 /* Note: transpose may fit better in format group, but that would break the API */
307 add_kb(group, GEANY_KEYS_EDITOR_TRANSPOSELINE, NULL,
308 0, 0, "edit_transposeline", _("_Transpose Current Line"), NULL);
309 add_kb(group, GEANY_KEYS_EDITOR_SCROLLTOLINE, NULL,
310 GDK_l, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_scrolltoline", _("Scroll to current line"), NULL);
311 add_kb(group, GEANY_KEYS_EDITOR_SCROLLLINEUP, NULL,
312 GDK_Up, GDK_MOD1_MASK, "edit_scrolllineup", _("Scroll up the view by one line"), NULL);
313 add_kb(group, GEANY_KEYS_EDITOR_SCROLLLINEDOWN, NULL,
314 GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
315 add_kb(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL,
316 GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
317 add_kb(group, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR, NULL,
318 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
319 add_kb(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, NULL,
320 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
321 add_kb(group, GEANY_KEYS_EDITOR_CONTEXTACTION, NULL,
322 0, 0, "popup_contextaction", _("Context Action"), NULL);
323 add_kb(group, GEANY_KEYS_EDITOR_AUTOCOMPLETE, NULL,
324 GDK_space, GDK_CONTROL_MASK, "edit_autocomplete", _("Complete word"), NULL);
325 add_kb(group, GEANY_KEYS_EDITOR_CALLTIP, NULL,
326 GDK_space, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_calltip", _("Show calltip"), NULL);
327 add_kb(group, GEANY_KEYS_EDITOR_MACROLIST, NULL,
328 GDK_Return, GDK_CONTROL_MASK, "edit_macrolist", _("Show macro list"), NULL);
329 add_kb(group, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION, NULL,
330 GDK_Tab, 0, "edit_wordpartcompletion", _("Word part completion"), NULL);
331 add_kb(group, GEANY_KEYS_EDITOR_MOVELINEUP, NULL,
332 GDK_Page_Up, GDK_MOD1_MASK, "edit_movelineup",
333 _("Move line(s) up"), "move_lines_up1");
334 add_kb(group, GEANY_KEYS_EDITOR_MOVELINEDOWN, NULL,
335 GDK_Page_Down, GDK_MOD1_MASK, "edit_movelinedown",
336 _("Move line(s) down"), "move_lines_down1");
338 group = keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD);
340 add_kb(group, GEANY_KEYS_CLIPBOARD_CUT, NULL,
341 GDK_x, GDK_CONTROL_MASK, "menu_cut", _("Cut"), "menu_cut1");
342 add_kb(group, GEANY_KEYS_CLIPBOARD_COPY, NULL,
343 GDK_c, GDK_CONTROL_MASK, "menu_copy", _("Copy"), "menu_copy1");
344 add_kb(group, GEANY_KEYS_CLIPBOARD_PASTE, NULL,
345 GDK_v, GDK_CONTROL_MASK, "menu_paste", _("Paste"), "menu_paste1");
346 add_kb(group, GEANY_KEYS_CLIPBOARD_COPYLINE, NULL,
347 GDK_c, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_copyline", _("_Copy Current Line(s)"),
348 "copy_current_lines1");
349 add_kb(group, GEANY_KEYS_CLIPBOARD_CUTLINE, NULL,
350 GDK_x, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_cutline", _("Cu_t Current Line(s)"),
351 "cut_current_lines1");
353 group = keybindings_get_core_group(GEANY_KEY_GROUP_SELECT);
355 add_kb(group, GEANY_KEYS_SELECT_ALL, NULL,
356 GDK_a, GDK_CONTROL_MASK, "menu_selectall", _("Select All"), "menu_select_all1");
357 add_kb(group, GEANY_KEYS_SELECT_WORD, NULL,
358 GDK_w, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectword", _("Select current word"), NULL);
359 add_kb(group, GEANY_KEYS_SELECT_LINE, NULL,
360 GDK_l, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectline", _("S_elect Current Line(s)"),
361 "select_current_lines1");
362 add_kb(group, GEANY_KEYS_SELECT_PARAGRAPH, NULL,
363 GDK_p, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectparagraph", _("Se_lect Current Paragraph"),
364 "select_current_paragraph1");
365 add_kb(group, GEANY_KEYS_SELECT_WORDPARTLEFT, NULL,
366 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL);
367 add_kb(group, GEANY_KEYS_SELECT_WORDPARTRIGHT, NULL,
368 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL);
370 group = keybindings_get_core_group(GEANY_KEY_GROUP_FORMAT);
372 add_kb(group, GEANY_KEYS_FORMAT_TOGGLECASE, NULL,
373 GDK_u, GDK_CONTROL_MASK | GDK_MOD1_MASK, "edit_togglecase",
374 _("T_oggle Case of Selection"), "menu_toggle_case2");
375 add_kb(group, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE, NULL,
376 GDK_e, GDK_CONTROL_MASK, "edit_commentlinetoggle", _("Toggle line commentation"),
377 "menu_toggle_line_commentation1");
378 add_kb(group, GEANY_KEYS_FORMAT_COMMENTLINE, NULL,
379 0, 0, "edit_commentline", _("Comment line(s)"), "menu_comment_line1");
380 add_kb(group, GEANY_KEYS_FORMAT_UNCOMMENTLINE, NULL,
381 0, 0, "edit_uncommentline", _("Uncomment line(s)"), "menu_uncomment_line1");
382 add_kb(group, GEANY_KEYS_FORMAT_INCREASEINDENT, NULL,
383 GDK_i, GDK_CONTROL_MASK, "edit_increaseindent", _("Increase indent"),
384 "menu_increase_indent1");
385 add_kb(group, GEANY_KEYS_FORMAT_DECREASEINDENT, NULL,
386 GDK_u, GDK_CONTROL_MASK, "edit_decreaseindent", _("Decrease indent"),
387 "menu_decrease_indent1");
388 add_kb(group, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE, NULL,
389 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL);
390 add_kb(group, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE, NULL,
391 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL);
392 add_kb(group, GEANY_KEYS_FORMAT_AUTOINDENT, NULL,
393 0, 0, "edit_autoindent", _("S_mart Line Indent"), "smart_line_indent1");
394 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD1, NULL,
395 GDK_1, GDK_CONTROL_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL);
396 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD2, NULL,
397 GDK_2, GDK_CONTROL_MASK, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL);
398 add_kb(group, GEANY_KEYS_FORMAT_SENDTOCMD3, NULL,
399 GDK_3, GDK_CONTROL_MASK, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL);
400 /* may fit better in editor group */
401 add_kb(group, GEANY_KEYS_FORMAT_SENDTOVTE, NULL,
402 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), "send_selection_to_vte1");
403 add_kb(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
404 GDK_j, GDK_CONTROL_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
405 "reflow_lines_block1");
406 keybindings_set_item(group, GEANY_KEYS_FORMAT_JOINLINES, NULL,
407 0, 0, "edit_joinlines", _("Join lines"), NULL);
409 group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
411 add_kb(group, GEANY_KEYS_INSERT_DATE, NULL,
412 GDK_d, GDK_SHIFT_MASK | GDK_MOD1_MASK, "menu_insert_date", _("Insert date"),
413 "insert_date_custom1");
414 add_kb(group, GEANY_KEYS_INSERT_ALTWHITESPACE, NULL,
415 0, 0, "edit_insertwhitespace", _("Insert Alternative _White Space"),
416 "insert_alternative_white_space1");
417 add_kb(group, GEANY_KEYS_INSERT_LINEBEFORE, NULL,
418 0, 0, "edit_insertlinebefore", _("Insert New Line Before Current"), NULL);
419 add_kb(group, GEANY_KEYS_INSERT_LINEAFTER, NULL,
420 0, 0, "edit_insertlineafter", _("Insert New Line After Current"), NULL);
422 group = keybindings_get_core_group(GEANY_KEY_GROUP_SETTINGS);
424 add_kb(group, GEANY_KEYS_SETTINGS_PREFERENCES, cb_func_menu_preferences,
425 GDK_p, GDK_CONTROL_MASK | GDK_MOD1_MASK, "menu_preferences", _("Preferences"),
426 "preferences1");
427 add_kb(group, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES, cb_func_menu_preferences,
428 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), "plugin_preferences1");
430 group = keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH);
432 add_kb(group, GEANY_KEYS_SEARCH_FIND, NULL,
433 GDK_f, GDK_CONTROL_MASK, "menu_find", _("Find"), "find1");
434 add_kb(group, GEANY_KEYS_SEARCH_FINDNEXT, NULL,
435 GDK_g, GDK_CONTROL_MASK, "menu_findnext", _("Find Next"), "find_next1");
436 add_kb(group, GEANY_KEYS_SEARCH_FINDPREVIOUS, NULL,
437 GDK_g, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findprevious", _("Find Previous"),
438 "find_previous1");
439 add_kb(group, GEANY_KEYS_SEARCH_FINDNEXTSEL, NULL,
440 0, 0, "menu_findnextsel", _("Find Next _Selection"), "find_nextsel1");
441 add_kb(group, GEANY_KEYS_SEARCH_FINDPREVSEL, NULL,
442 0, 0, "menu_findprevsel", _("Find Pre_vious Selection"), "find_prevsel1");
443 add_kb(group, GEANY_KEYS_SEARCH_REPLACE, NULL,
444 GDK_h, GDK_CONTROL_MASK, "menu_replace", _("Replace"), "replace1");
445 add_kb(group, GEANY_KEYS_SEARCH_FINDINFILES, NULL, GDK_f,
446 GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findinfiles", _("Find in Files"),
447 "find_in_files1");
448 add_kb(group, GEANY_KEYS_SEARCH_NEXTMESSAGE, NULL,
449 0, 0, "menu_nextmessage", _("Next Message"), "next_message1");
450 add_kb(group, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE, NULL,
451 0, 0, "menu_previousmessage", _("Previous Message"), "previous_message1");
452 add_kb(group, GEANY_KEYS_SEARCH_FINDUSAGE, NULL,
453 GDK_e, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "popup_findusage",
454 _("Find Usage"), "find_usage1");
455 add_kb(group, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, NULL,
456 GDK_d, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "popup_finddocumentusage",
457 _("Find Document Usage"), "find_document_usage1");
458 add_kb(group, GEANY_KEYS_SEARCH_MARKALL, NULL,
459 GDK_m, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "find_markall", _("_Mark All"), "mark_all1");
461 group = keybindings_get_core_group(GEANY_KEY_GROUP_GOTO);
463 add_kb(group, GEANY_KEYS_GOTO_BACK, NULL,
464 GDK_Left, GDK_MOD1_MASK, "nav_back", _("Navigate back a location"), NULL);
465 add_kb(group, GEANY_KEYS_GOTO_FORWARD, NULL,
466 GDK_Right, GDK_MOD1_MASK, "nav_forward", _("Navigate forward a location"), NULL);
467 add_kb(group, GEANY_KEYS_GOTO_LINE, NULL,
468 GDK_l, GDK_CONTROL_MASK, "menu_gotoline", _("Go to Line"), "go_to_line1");
469 add_kb(group, GEANY_KEYS_GOTO_MATCHINGBRACE, NULL,
470 GDK_b, GDK_CONTROL_MASK, "edit_gotomatchingbrace",
471 _("Go to matching brace"), NULL);
472 add_kb(group, GEANY_KEYS_GOTO_TOGGLEMARKER, NULL,
473 GDK_m, GDK_CONTROL_MASK, "edit_togglemarker",
474 _("Toggle marker"), NULL);
475 add_kb(group, GEANY_KEYS_GOTO_NEXTMARKER, NULL,
476 GDK_period, GDK_CONTROL_MASK, "edit_gotonextmarker",
477 _("Go to Ne_xt Marker"), "go_to_next_marker1");
478 add_kb(group, GEANY_KEYS_GOTO_PREVIOUSMARKER, NULL,
479 GDK_comma, GDK_CONTROL_MASK, "edit_gotopreviousmarker",
480 _("Go to Pre_vious Marker"), "go_to_previous_marker1");
481 add_kb(group, GEANY_KEYS_GOTO_TAGDEFINITION, NULL,
482 GDK_t, GDK_CONTROL_MASK, "popup_gototagdefinition",
483 _("Go to Tag Definition"), "goto_tag_definition1");
484 add_kb(group, GEANY_KEYS_GOTO_TAGDECLARATION, NULL,
485 GDK_t, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "popup_gototagdeclaration",
486 _("Go to Tag Declaration"), "goto_tag_declaration1");
487 add_kb(group, GEANY_KEYS_GOTO_LINESTART, NULL,
488 GDK_Home, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL);
489 add_kb(group, GEANY_KEYS_GOTO_LINEEND, NULL,
490 GDK_End, 0, "edit_gotolineend", _("Go to End of Line"), NULL);
491 add_kb(group, GEANY_KEYS_GOTO_LINESTARTVISUAL, NULL,
492 GDK_Home, GDK_MOD1_MASK, "edit_gotolinestartvisual", _("Go to Start of Display Line"), NULL);
493 add_kb(group, GEANY_KEYS_GOTO_LINEENDVISUAL, NULL,
494 GDK_End, GDK_MOD1_MASK, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL);
495 add_kb(group, GEANY_KEYS_GOTO_PREVWORDPART, NULL,
496 GDK_slash, GDK_CONTROL_MASK, "edit_prevwordstart", _("Go to Previous Word Part"), NULL);
497 add_kb(group, GEANY_KEYS_GOTO_NEXTWORDPART, NULL,
498 GDK_backslash, GDK_CONTROL_MASK, "edit_nextwordstart", _("Go to Next Word Part"), NULL);
500 group = keybindings_get_core_group(GEANY_KEY_GROUP_VIEW);
502 add_kb(group, GEANY_KEYS_VIEW_TOGGLEALL, NULL,
503 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
504 "menu_toggle_all_additional_widgets1");
505 add_kb(group, GEANY_KEYS_VIEW_FULLSCREEN, cb_func_menu_fullscreen,
506 GDK_F11, 0, "menu_fullscreen", _("Fullscreen"), "menu_fullscreen1");
507 add_kb(group, GEANY_KEYS_VIEW_MESSAGEWINDOW, cb_func_menu_messagewindow,
508 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
509 "menu_show_messages_window1");
510 add_kb(group, GEANY_KEYS_VIEW_SIDEBAR, NULL,
511 0, 0, "toggle_sidebar", _("Toggle Sidebar"), "menu_show_sidebar1");
512 add_kb(group, GEANY_KEYS_VIEW_ZOOMIN, NULL,
513 GDK_plus, GDK_CONTROL_MASK, "menu_zoomin", _("Zoom In"), "menu_zoom_in1");
514 add_kb(group, GEANY_KEYS_VIEW_ZOOMOUT, NULL,
515 GDK_minus, GDK_CONTROL_MASK, "menu_zoomout", _("Zoom Out"), "menu_zoom_out1");
516 add_kb(group, GEANY_KEYS_VIEW_ZOOMRESET, NULL,
517 GDK_0, GDK_CONTROL_MASK, "normal_size", _("Zoom Reset"), "normal_size1");
519 group = keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS);
521 add_kb(group, GEANY_KEYS_FOCUS_EDITOR, NULL,
522 GDK_F2, 0, "switch_editor", _("Switch to Editor"), NULL);
523 add_kb(group, GEANY_KEYS_FOCUS_SEARCHBAR, NULL,
524 GDK_F7, 0, "switch_search_bar", _("Switch to Search Bar"), NULL);
525 add_kb(group, GEANY_KEYS_FOCUS_MESSAGE_WINDOW, NULL,
526 0, 0, "switch_message_window", _("Switch to Message Window"), NULL);
527 add_kb(group, GEANY_KEYS_FOCUS_COMPILER, NULL,
528 0, 0, "switch_compiler", _("Switch to Compiler"), NULL);
529 add_kb(group, GEANY_KEYS_FOCUS_MESSAGES, NULL,
530 0, 0, "switch_messages", _("Switch to Messages"), NULL);
531 add_kb(group, GEANY_KEYS_FOCUS_SCRIBBLE, NULL,
532 GDK_F6, 0, "switch_scribble", _("Switch to Scribble"), NULL);
533 add_kb(group, GEANY_KEYS_FOCUS_VTE, NULL,
534 GDK_F4, 0, "switch_vte", _("Switch to VTE"), NULL);
535 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR, NULL,
536 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL);
537 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST, NULL,
538 0, 0, "switch_sidebar_symbol_list", _("Switch to Sidebar Symbol List"), NULL);
539 add_kb(group, GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST, NULL,
540 0, 0, "switch_sidebar_doc_list", _("Switch to Sidebar Document List"), NULL);
542 group = keybindings_get_core_group(GEANY_KEY_GROUP_NOTEBOOK);
544 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT, cb_func_switch_tableft,
545 GDK_Page_Up, GDK_CONTROL_MASK, "switch_tableft", _("Switch to left document"), NULL);
546 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
547 GDK_Page_Down, GDK_CONTROL_MASK, "switch_tabright", _("Switch to right document"), NULL);
548 add_kb(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
549 GDK_Tab, GDK_CONTROL_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
550 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
551 GDK_Page_Up, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "move_tableft",
552 _("Move document left"), NULL);
553 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
554 GDK_Page_Down, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "move_tabright",
555 _("Move document right"), NULL);
556 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABFIRST, cb_func_move_tab,
557 0, 0, "move_tabfirst", _("Move document first"), NULL);
558 add_kb(group, GEANY_KEYS_NOTEBOOK_MOVETABLAST, cb_func_move_tab,
559 0, 0, "move_tablast", _("Move document last"), NULL);
561 group = keybindings_get_core_group(GEANY_KEY_GROUP_DOCUMENT);
563 add_kb(group, GEANY_KEYS_DOCUMENT_LINEWRAP, NULL,
564 0, 0, "menu_linewrap", _("Toggle Line wrapping"), "menu_line_wrapping1");
565 add_kb(group, GEANY_KEYS_DOCUMENT_LINEBREAK, NULL,
566 0, 0, "menu_linebreak", _("Toggle Line breaking"), "line_breaking1");
567 add_kb(group, GEANY_KEYS_DOCUMENT_CLONE, NULL,
568 0, 0, "menu_clone", _("_Clone"), "clone1");
569 add_kb(group, GEANY_KEYS_DOCUMENT_REPLACETABS, NULL,
570 0, 0, "menu_replacetabs", _("Replace tabs with space"), "menu_replace_tabs");
571 add_kb(group, GEANY_KEYS_DOCUMENT_REPLACESPACES, NULL,
572 0, 0, "menu_replacespaces", _("Replace spaces with tabs"), "menu_replace_spaces");
573 add_kb(group, GEANY_KEYS_DOCUMENT_TOGGLEFOLD, NULL,
574 0, 0, "menu_togglefold", _("Toggle current fold"), NULL);
575 add_kb(group, GEANY_KEYS_DOCUMENT_FOLDALL, NULL,
576 0, 0, "menu_foldall", _("Fold all"), "menu_fold_all1");
577 add_kb(group, GEANY_KEYS_DOCUMENT_UNFOLDALL, NULL,
578 0, 0, "menu_unfoldall", _("Unfold all"), "menu_unfold_all1");
579 add_kb(group, GEANY_KEYS_DOCUMENT_RELOADTAGLIST, NULL,
580 GDK_r, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "reloadtaglist", _("Reload symbol list"), NULL);
581 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS, NULL,
582 0, 0, "remove_markers", _("Remove Markers"), "remove_markers1");
583 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS, NULL,
584 0, 0, "remove_error_indicators", _("Remove Error Indicators"), "menu_remove_indicators1");
585 add_kb(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS, NULL,
586 0, 0, "remove_markers_and_indicators", _("Remove Markers and Error Indicators"), NULL);
588 group = keybindings_get_core_group(GEANY_KEY_GROUP_BUILD);
590 add_kb(group, GEANY_KEYS_BUILD_COMPILE, NULL,
591 GDK_F8, 0, "build_compile", _("Compile"), NULL);
592 add_kb(group, GEANY_KEYS_BUILD_LINK, NULL,
593 GDK_F9, 0, "build_link", _("Build"), NULL);
594 add_kb(group, GEANY_KEYS_BUILD_MAKE, NULL,
595 GDK_F9, GDK_SHIFT_MASK, "build_make", _("Make all"), NULL);
596 add_kb(group, GEANY_KEYS_BUILD_MAKEOWNTARGET, NULL,
597 GDK_F9, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "build_makeowntarget",
598 _("Make custom target"), NULL);
599 add_kb(group, GEANY_KEYS_BUILD_MAKEOBJECT, NULL,
600 GDK_F8, GDK_SHIFT_MASK, "build_makeobject", _("Make object"), NULL);
601 add_kb(group, GEANY_KEYS_BUILD_NEXTERROR, NULL,
602 0, 0, "build_nexterror", _("Next error"), NULL);
603 add_kb(group, GEANY_KEYS_BUILD_PREVIOUSERROR, NULL,
604 0, 0, "build_previouserror", _("Previous error"), NULL);
605 add_kb(group, GEANY_KEYS_BUILD_RUN, NULL,
606 GDK_F5, 0, "build_run", _("Run"), NULL);
607 add_kb(group, GEANY_KEYS_BUILD_OPTIONS, NULL,
608 0, 0, "build_options", _("Build options"), NULL);
610 group = keybindings_get_core_group(GEANY_KEY_GROUP_TOOLS);
612 add_kb(group, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER, cb_func_menu_opencolorchooser,
613 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), "menu_choose_color1");
615 group = keybindings_get_core_group(GEANY_KEY_GROUP_HELP);
617 add_kb(group, GEANY_KEYS_HELP_HELP, cb_func_menu_help,
618 GDK_F1, 0, "menu_help", _("Help"), "help1");
622 void keybindings_init(void)
624 memset(binding_ids, 0, sizeof binding_ids);
625 keybinding_groups = g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT);
626 kb_accel_group = gtk_accel_group_new();
628 init_default_kb();
629 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), kb_accel_group);
631 g_signal_connect(main_widgets.window, "key-press-event", G_CALLBACK(on_key_press_event), NULL);
635 typedef void (*KBItemCallback) (GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data);
637 static void keybindings_foreach(KBItemCallback cb, gpointer user_data)
639 gsize g, i;
640 GeanyKeyGroup *group;
641 GeanyKeyBinding *kb;
643 foreach_ptr_array(group, g, keybinding_groups)
645 foreach_ptr_array(kb, i, group->key_items)
646 cb(group, kb, user_data);
651 static void load_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
653 GKeyFile *config = user_data;
654 gchar *val;
655 guint key;
656 GdkModifierType mods;
658 val = g_key_file_get_string(config, group->name, kb->name, NULL);
659 if (val != NULL)
661 gtk_accelerator_parse(val, &key, &mods);
662 kb->key = key;
663 kb->mods = mods;
664 g_free(val);
669 static void load_user_kb(void)
671 gchar *configfile = g_build_filename(app->configdir, "keybindings.conf", NULL);
672 GKeyFile *config = g_key_file_new();
674 /* backwards compatibility with Geany 0.21 defaults */
675 if (!g_file_test(configfile, G_FILE_TEST_EXISTS))
677 gchar *geanyconf = g_build_filename(app->configdir, "geany.conf", NULL);
678 const gchar data[] = "[Bindings]\n"
679 "popup_gototagdefinition=\n"
680 "edit_transposeline=<Control>t\n"
681 "edit_movelineup=\n"
682 "edit_movelinedown=\n"
683 "move_tableft=<Alt>Page_Up\n"
684 "move_tabright=<Alt>Page_Down\n";
686 utils_write_file(configfile, g_file_test(geanyconf, G_FILE_TEST_EXISTS) ?
687 data : "");
688 g_free(geanyconf);
691 /* now load user defined keys */
692 if (g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
694 keybindings_foreach(load_kb, config);
696 g_free(configfile);
697 g_key_file_free(config);
701 static void apply_kb_accel(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
703 if (kb->key != 0 && kb->menu_item)
705 gtk_widget_add_accelerator(kb->menu_item, "activate", kb_accel_group,
706 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
711 void keybindings_load_keyfile(void)
713 load_user_kb();
714 add_popup_menu_accels();
716 /* set menu accels now, after user keybindings have been read */
717 keybindings_foreach(apply_kb_accel, NULL);
721 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id, GtkWidget *menuitem)
723 GeanyKeyBinding *kb = keybindings_get_item(group, kb_id);
725 if (kb->key != 0)
726 gtk_widget_add_accelerator(menuitem, "activate", kb_accel_group,
727 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
731 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
732 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
734 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
735 /* FIXME: update those during runtime */
736 static void add_popup_menu_accels(void)
738 GeanyKeyGroup *group;
740 group = keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR);
741 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO, undo1);
742 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO, redo1);
743 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION, context_action1);
745 group = keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD);
746 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_CUT, cut1);
747 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_COPY, copy1);
748 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_PASTE, paste1);
750 group = keybindings_get_core_group(GEANY_KEY_GROUP_SELECT);
751 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL, menu_select_all2);
753 group = keybindings_get_core_group(GEANY_KEY_GROUP_INSERT);
754 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE, insert_date_custom2);
755 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_ALTWHITESPACE, insert_alternative_white_space2);
757 group = keybindings_get_core_group(GEANY_KEY_GROUP_FILE);
758 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED, menu_open_selected_file2);
760 group = keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH);
761 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE, find_usage2);
762 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, find_document_usage2);
764 group = keybindings_get_core_group(GEANY_KEY_GROUP_GOTO);
765 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION, goto_tag_definition2);
767 /* Format and Commands share the menu bar submenus */
768 /* Build menu items are set if the build menus are created */
772 static void set_keyfile_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
774 GKeyFile *config = user_data;
775 gchar *val;
777 val = gtk_accelerator_name(kb->key, kb->mods);
778 g_key_file_set_string(config, group->name, kb->name, val);
779 g_free(val);
783 /* just write the content of the keys array to the config file */
784 void keybindings_write_to_file(void)
786 gchar *configfile = g_build_filename(app->configdir, "keybindings.conf", NULL);
787 gchar *data;
788 GKeyFile *config = g_key_file_new();
790 g_key_file_load_from_file(config, configfile, 0, NULL);
791 keybindings_foreach(set_keyfile_kb, config);
793 /* write the file */
794 data = g_key_file_to_data(config, NULL, NULL);
795 utils_write_file(configfile, data);
797 g_free(data);
798 g_free(configfile);
799 g_key_file_free(config);
803 void keybindings_free(void)
805 GeanyKeyGroup *group;
806 gsize g;
808 foreach_ptr_array(group, g, keybinding_groups)
809 keybindings_free_group(group);
811 g_ptr_array_free(keybinding_groups, TRUE);
815 gchar *keybindings_get_label(GeanyKeyBinding *kb)
817 return utils_str_remove_chars(g_strdup(kb->label), "_");
821 static void fill_shortcut_labels_treeview(GtkWidget *tree)
823 gsize g, i;
824 GeanyKeyBinding *kb;
825 GeanyKeyGroup *group;
826 GtkListStore *store;
827 GtkTreeIter iter;
829 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, PANGO_TYPE_WEIGHT);
831 foreach_ptr_array(group, g, keybinding_groups)
833 if (g > 0)
835 gtk_list_store_append(store, &iter);
836 gtk_list_store_set(store, &iter, -1);
838 gtk_list_store_append(store, &iter);
839 gtk_list_store_set(store, &iter, 0, group->label, 2, PANGO_WEIGHT_BOLD, -1);
841 foreach_ptr_array(kb, i, group->key_items)
843 gchar *shortcut, *label;
845 label = keybindings_get_label(kb);
846 shortcut = gtk_accelerator_get_label(kb->key, kb->mods);
848 gtk_list_store_append(store, &iter);
849 gtk_list_store_set(store, &iter, 0, label, 1, shortcut, 2, PANGO_WEIGHT_NORMAL, -1);
851 g_free(shortcut);
852 g_free(label);
855 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
856 g_object_unref(store);
860 static GtkWidget *create_dialog(void)
862 GtkWidget *dialog, *tree, *label, *swin, *vbox;
863 GtkCellRenderer *text_renderer;
864 GtkTreeViewColumn *column;
866 dialog = gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets.window),
867 GTK_DIALOG_DESTROY_WITH_PARENT,
868 GTK_STOCK_EDIT, GTK_RESPONSE_APPLY,
869 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
870 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
871 gtk_box_set_spacing(GTK_BOX(vbox), 6);
872 gtk_widget_set_name(dialog, "GeanyDialog");
874 gtk_window_set_default_size(GTK_WINDOW(dialog), -1, GEANY_DEFAULT_DIALOG_HEIGHT);
876 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
878 label = gtk_label_new(_("The following keyboard shortcuts are configurable:"));
879 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
881 tree = gtk_tree_view_new();
882 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
883 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
885 text_renderer = gtk_cell_renderer_text_new();
886 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
887 column = gtk_tree_view_column_new_with_attributes(
888 NULL, text_renderer, "text", 0, "weight", 2, NULL);
889 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
891 text_renderer = gtk_cell_renderer_text_new();
892 column = gtk_tree_view_column_new_with_attributes(NULL, text_renderer, "text", 1, NULL);
893 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
895 fill_shortcut_labels_treeview(tree);
897 swin = gtk_scrolled_window_new(NULL, NULL);
898 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_NEVER,
899 GTK_POLICY_AUTOMATIC);
900 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_ETCHED_IN);
901 gtk_container_add(GTK_CONTAINER(swin), tree);
903 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
904 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
906 return dialog;
910 static void key_dialog_show_prefs(void)
912 GtkWidget *wid;
914 prefs_show_dialog();
915 /* select the KB page */
916 wid = ui_lookup_widget(ui_widgets.prefs_dialog, "frame22");
917 if (wid != NULL)
919 GtkNotebook *nb = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
920 if (nb != NULL)
922 gtk_notebook_set_current_page(nb, gtk_notebook_page_num(nb, wid));
928 void keybindings_dialog_show_prefs_scroll(const gchar *name)
930 key_dialog_show_prefs();
931 prefs_kb_search_name(name);
935 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
936 static GtkWidget *key_dialog = NULL;
938 static void on_dialog_response(GtkWidget *dialog, gint response, gpointer user_data)
940 if (response == GTK_RESPONSE_APPLY)
942 key_dialog_show_prefs();
944 gtk_widget_destroy(dialog);
945 key_dialog = NULL;
949 void keybindings_show_shortcuts(void)
951 if (key_dialog)
952 gtk_widget_destroy(key_dialog); /* in case the key_dialog is still visible */
954 key_dialog = create_dialog();
955 g_signal_connect(key_dialog, "response", G_CALLBACK(on_dialog_response), NULL);
956 gtk_widget_show_all(key_dialog);
960 static gboolean check_fixed_kb(guint keyval, guint state)
962 /* check alt-0 to alt-9 for setting current notebook page */
963 if (state == GDK_MOD1_MASK && keyval >= GDK_0 && keyval <= GDK_9)
965 gint page = keyval - GDK_0 - 1;
966 gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
968 /* alt-0 is for the rightmost tab */
969 if (keyval == GDK_0)
970 page = npages - 1;
971 /* invert the order if tabs are added on the other side */
972 if (swap_alt_tab_order && ! file_prefs.tab_order_ltr)
973 page = (npages - 1) - page;
975 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
976 return TRUE;
978 /* note: these are now overridden by default with move tab bindings */
979 if (keyval == GDK_Page_Up || keyval == GDK_Page_Down)
981 /* switch to first or last document */
982 if (state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
984 if (keyval == GDK_Page_Up)
985 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), 0);
986 if (keyval == GDK_Page_Down)
987 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), -1);
988 return TRUE;
991 return FALSE;
995 static gboolean check_snippet_completion(GeanyDocument *doc)
997 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
999 g_return_val_if_fail(doc, FALSE);
1001 /* keybinding only valid when scintilla widget has focus */
1002 if (focusw == GTK_WIDGET(doc->editor->sci))
1004 ScintillaObject *sci = doc->editor->sci;
1005 gint pos = sci_get_current_position(sci);
1007 if (editor_prefs.complete_snippets)
1008 return editor_complete_snippet(doc->editor, pos);
1010 return FALSE;
1014 /* Transforms a GdkEventKey event into a GdkEventButton event */
1015 static void trigger_button_event(GtkWidget *widget, guint32 event_time)
1017 GdkEventButton *event;
1018 gboolean ret;
1020 event = g_new0(GdkEventButton, 1);
1022 if (GTK_IS_TEXT_VIEW(widget))
1023 event->window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT);
1024 else
1025 event->window = gtk_widget_get_window(widget);
1026 event->time = event_time;
1027 event->type = GDK_BUTTON_PRESS;
1028 event->button = 3;
1030 g_signal_emit_by_name(widget, "button-press-event", event, &ret);
1031 g_signal_emit_by_name(widget, "button-release-event", event, &ret);
1033 g_free(event);
1037 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
1038 * widgets. Without this special handling, the notebook tab list of the documents' notebook
1039 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
1040 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1041 * notebook tab list. */
1042 static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
1044 g_return_val_if_fail(doc == NULL || doc->is_valid, FALSE);
1046 if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
1048 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1049 if (doc != NULL)
1051 if (focusw == doc->priv->tag_tree)
1053 trigger_button_event(focusw, event_time);
1054 return TRUE;
1056 if (focusw == GTK_WIDGET(doc->editor->sci))
1058 if (keyval == GDK_Menu)
1059 { /* show editor popup menu */
1060 trigger_button_event(focusw, event_time);
1061 return TRUE;
1063 else
1064 { /* show tab bar menu */
1065 trigger_button_event(main_widgets.notebook, event_time);
1066 return TRUE;
1070 if (focusw == tv.tree_openfiles
1071 || focusw == msgwindow.tree_status
1072 || focusw == msgwindow.tree_compiler
1073 || focusw == msgwindow.tree_msg
1074 || focusw == msgwindow.scribble
1075 #ifdef HAVE_VTE
1076 || (vte_info.have_vte && focusw == vc->vte)
1077 #endif
1080 trigger_button_event(focusw, event_time);
1081 return TRUE;
1084 return FALSE;
1088 #ifdef HAVE_VTE
1089 static gboolean on_menu_expose_event(GtkWidget *widget, GdkEventExpose *event,
1090 gpointer user_data)
1092 if (!gtk_widget_get_sensitive(widget))
1093 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1094 return FALSE;
1098 #if GTK_CHECK_VERSION(3, 0, 0)
1099 static gboolean on_menu_draw(GtkWidget *widget, cairo_t *cr, gpointer user_data)
1101 return on_menu_expose_event(widget, NULL, user_data);
1103 #endif
1106 static gboolean set_sensitive(gpointer widget)
1108 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1109 return FALSE;
1113 static gboolean check_vte(GdkModifierType state, guint keyval)
1115 guint i;
1116 GeanyKeyBinding *kb;
1117 GeanyKeyGroup *group;
1118 GtkWidget *widget;
1120 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != vc->vte)
1121 return FALSE;
1122 /* let VTE copy/paste override any user keybinding */
1123 if (state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK) && (keyval == GDK_c || keyval == GDK_v))
1124 return TRUE;
1125 if (! vc->enable_bash_keys)
1126 return FALSE;
1127 /* prevent menubar flickering: */
1128 if (state == GDK_SHIFT_MASK && (keyval >= GDK_a && keyval <= GDK_z))
1129 return FALSE;
1130 if (state == 0 && (keyval < GDK_F1 || keyval > GDK_F35)) /* e.g. backspace */
1131 return FALSE;
1133 /* make focus commands override any bash commands */
1134 group = keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS);
1135 foreach_ptr_array(kb, i, group->key_items)
1137 if (state == kb->mods && keyval == kb->key)
1138 return FALSE;
1141 /* Temporarily disable the menus to prevent conflicting menu accelerators
1142 * from overriding the VTE bash shortcuts.
1143 * Note: maybe there's a better way of doing this ;-) */
1144 widget = ui_lookup_widget(main_widgets.window, "menubar1");
1145 gtk_widget_set_sensitive(widget, FALSE);
1147 /* make the menubar sensitive before it is redrawn */
1148 static gboolean connected = FALSE;
1149 if (!connected)
1150 #if GTK_CHECK_VERSION(3, 0, 0)
1151 g_signal_connect(widget, "draw", G_CALLBACK(on_menu_draw), NULL);
1152 #else
1153 g_signal_connect(widget, "expose-event", G_CALLBACK(on_menu_expose_event), NULL);
1154 #endif
1157 widget = main_widgets.editor_menu;
1158 gtk_widget_set_sensitive(widget, FALSE);
1159 g_idle_add(set_sensitive, widget);
1160 return TRUE;
1162 #endif
1165 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1166 static guint key_kp_translate(guint key_in)
1168 switch (key_in)
1170 case GDK_KP_Down:
1171 return GDK_Down;
1172 case GDK_KP_Up:
1173 return GDK_Up;
1174 case GDK_KP_Left:
1175 return GDK_Left;
1176 case GDK_KP_Right:
1177 return GDK_Right;
1178 case GDK_KP_Home:
1179 return GDK_Home;
1180 case GDK_KP_End:
1181 return GDK_End;
1182 case GDK_KP_Page_Up:
1183 return GDK_Page_Up;
1184 case GDK_KP_Page_Down:
1185 return GDK_Page_Down;
1186 case GDK_KP_Delete:
1187 return GDK_Delete;
1188 case GDK_KP_Insert:
1189 return GDK_Insert;
1190 default:
1191 return key_in;
1196 /* Check if event keypress matches keybinding combo */
1197 gboolean keybindings_check_event(GdkEventKey *ev, GeanyKeyBinding *kb)
1199 guint state, keyval;
1201 if (ev->keyval == 0)
1202 return FALSE;
1204 keyval = ev->keyval;
1205 state = ev->state & gtk_accelerator_get_default_mod_mask();
1206 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1207 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1208 if (keyval >= GDK_A && keyval <= GDK_Z)
1209 keyval += GDK_a - GDK_A;
1211 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1212 keyval = key_kp_translate(keyval);
1214 return (keyval == kb->key && state == kb->mods);
1218 /* central keypress event handler, almost all keypress events go to this function */
1219 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1221 guint state, keyval;
1222 gsize g, i;
1223 GeanyDocument *doc;
1224 GeanyKeyGroup *group;
1225 GeanyKeyBinding *kb;
1227 if (ev->keyval == 0)
1228 return FALSE;
1230 doc = document_get_current();
1231 if (doc)
1232 document_check_disk_status(doc, FALSE);
1234 keyval = ev->keyval;
1235 state = ev->state & gtk_accelerator_get_default_mod_mask();
1236 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1237 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1238 if (keyval >= GDK_A && keyval <= GDK_Z)
1239 keyval += GDK_a - GDK_A;
1241 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1242 keyval = key_kp_translate(keyval);
1244 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1246 /* special cases */
1247 #ifdef HAVE_VTE
1248 if (vte_info.have_vte && check_vte(state, keyval))
1249 return FALSE;
1250 #endif
1251 if (check_menu_key(doc, keyval, state, ev->time))
1252 return TRUE;
1254 foreach_ptr_array(group, g, keybinding_groups)
1256 foreach_ptr_array(kb, i, group->key_items)
1258 if (keyval == kb->key && state == kb->mods)
1260 /* call the corresponding callback function for this shortcut */
1261 if (kb->callback)
1263 kb->callback(kb->id);
1264 return TRUE;
1266 else if (group->callback)
1268 if (group->callback(kb->id))
1269 return TRUE;
1270 else
1271 continue; /* not handled */
1273 g_warning("No callback for keybinding %s: %s!", group->name, kb->name);
1277 /* fixed keybindings can be overridden by user bindings, so check them last */
1278 if (check_fixed_kb(keyval, state))
1279 return TRUE;
1280 return FALSE;
1284 /* group_id must be a core group, e.g. GEANY_KEY_GROUP_EDITOR
1285 * key_id e.g. GEANY_KEYS_EDITOR_CALLTIP */
1286 GeanyKeyBinding *keybindings_lookup_item(guint group_id, guint key_id)
1288 GeanyKeyGroup *group;
1290 g_return_val_if_fail(group_id < GEANY_KEY_GROUP_COUNT, NULL); /* can't use this for plugin groups */
1292 group = keybindings_get_core_group(group_id);
1294 g_return_val_if_fail(group, NULL);
1295 return keybindings_get_item(group, key_id);
1299 /** Mimics a (built-in only) keybinding action.
1300 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1301 * @param group_id @ref GeanyKeyGroupID keybinding group index that contains the @a key_id keybinding.
1302 * @param key_id @ref GeanyKeyBindingID keybinding index. */
1303 void keybindings_send_command(guint group_id, guint key_id)
1305 GeanyKeyBinding *kb;
1307 kb = keybindings_lookup_item(group_id, key_id);
1308 if (kb)
1310 if (kb->callback)
1311 kb->callback(key_id);
1312 else
1314 GeanyKeyGroup *group = keybindings_get_core_group(group_id);
1316 if (group->callback)
1317 group->callback(key_id);
1323 /* These are the callback functions, either each group or each shortcut has it's
1324 * own function. */
1327 static gboolean cb_func_file_action(guint key_id)
1329 switch (key_id)
1331 case GEANY_KEYS_FILE_NEW:
1332 document_new_file(NULL, NULL, NULL);
1333 break;
1334 case GEANY_KEYS_FILE_OPEN:
1335 on_open1_activate(NULL, NULL);
1336 break;
1337 case GEANY_KEYS_FILE_OPENSELECTED:
1338 on_menu_open_selected_file1_activate(NULL, NULL);
1339 break;
1340 case GEANY_KEYS_FILE_OPENLASTTAB:
1342 gchar *utf8_filename = g_queue_peek_head(ui_prefs.recent_queue);
1343 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1344 document_open_file(locale_filename, FALSE, NULL, NULL);
1345 g_free(locale_filename);
1346 break;
1348 case GEANY_KEYS_FILE_SAVE:
1349 on_save1_activate(NULL, NULL);
1350 break;
1351 case GEANY_KEYS_FILE_SAVEAS:
1352 on_save_as1_activate(NULL, NULL);
1353 break;
1354 case GEANY_KEYS_FILE_SAVEALL:
1355 on_save_all1_activate(NULL, NULL);
1356 break;
1357 case GEANY_KEYS_FILE_CLOSE:
1358 on_close1_activate(NULL, NULL);
1359 break;
1360 case GEANY_KEYS_FILE_CLOSEALL:
1361 on_close_all1_activate(NULL, NULL);
1362 break;
1363 case GEANY_KEYS_FILE_RELOAD:
1364 on_toolbutton_reload_clicked(NULL, NULL);
1365 break;
1366 case GEANY_KEYS_FILE_PRINT:
1367 on_print1_activate(NULL, NULL);
1368 break;
1369 case GEANY_KEYS_FILE_QUIT:
1370 on_quit1_activate(NULL, NULL);
1371 break;
1373 return TRUE;
1377 static gboolean cb_func_project_action(guint key_id)
1379 switch (key_id)
1381 case GEANY_KEYS_PROJECT_NEW:
1382 on_project_new1_activate(NULL, NULL);
1383 break;
1384 case GEANY_KEYS_PROJECT_OPEN:
1385 on_project_open1_activate(NULL, NULL);
1386 break;
1387 case GEANY_KEYS_PROJECT_CLOSE:
1388 if (app->project)
1389 on_project_close1_activate(NULL, NULL);
1390 break;
1391 case GEANY_KEYS_PROJECT_PROPERTIES:
1392 if (app->project)
1393 on_project_properties1_activate(NULL, NULL);
1394 break;
1396 return TRUE;
1400 static void cb_func_menu_preferences(guint key_id)
1402 switch (key_id)
1404 case GEANY_KEYS_SETTINGS_PREFERENCES:
1405 on_preferences1_activate(NULL, NULL);
1406 break;
1407 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES:
1408 on_plugin_preferences1_activate(NULL, NULL);
1409 break;
1414 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id)
1416 on_help1_activate(NULL, NULL);
1420 static gboolean cb_func_search_action(guint key_id)
1422 GeanyDocument *doc = document_get_current();
1423 ScintillaObject *sci;
1425 if (key_id == GEANY_KEYS_SEARCH_FINDINFILES)
1427 on_find_in_files1_activate(NULL, NULL); /* works without docs too */
1428 return TRUE;
1430 if (!doc)
1431 return TRUE;
1432 sci = doc->editor->sci;
1434 switch (key_id)
1436 case GEANY_KEYS_SEARCH_FIND:
1437 on_find1_activate(NULL, NULL); break;
1438 case GEANY_KEYS_SEARCH_FINDNEXT:
1439 on_find_next1_activate(NULL, NULL); break;
1440 case GEANY_KEYS_SEARCH_FINDPREVIOUS:
1441 on_find_previous1_activate(NULL, NULL); break;
1442 case GEANY_KEYS_SEARCH_FINDPREVSEL:
1443 on_find_prevsel1_activate(NULL, NULL); break;
1444 case GEANY_KEYS_SEARCH_FINDNEXTSEL:
1445 on_find_nextsel1_activate(NULL, NULL); break;
1446 case GEANY_KEYS_SEARCH_REPLACE:
1447 on_replace1_activate(NULL, NULL); break;
1448 case GEANY_KEYS_SEARCH_NEXTMESSAGE:
1449 on_next_message1_activate(NULL, NULL); break;
1450 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE:
1451 on_previous_message1_activate(NULL, NULL); break;
1452 case GEANY_KEYS_SEARCH_FINDUSAGE:
1453 on_find_usage1_activate(NULL, NULL); break;
1454 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE:
1455 on_find_document_usage1_activate(NULL, NULL); break;
1456 case GEANY_KEYS_SEARCH_MARKALL:
1458 gchar *text = NULL;
1459 gint pos = sci_get_current_position(sci);
1461 /* clear existing search indicators instead if next to cursor */
1462 if (scintilla_send_message(sci, SCI_INDICATORVALUEAT,
1463 GEANY_INDICATOR_SEARCH, pos) ||
1464 scintilla_send_message(sci, SCI_INDICATORVALUEAT,
1465 GEANY_INDICATOR_SEARCH, MAX(pos - 1, 0)))
1466 text = NULL;
1467 else
1468 text = get_current_word_or_sel(doc, TRUE);
1470 if (sci_has_selection(sci))
1471 search_mark_all(doc, text, SCFIND_MATCHCASE);
1472 else
1473 search_mark_all(doc, text, SCFIND_MATCHCASE | SCFIND_WHOLEWORD);
1475 g_free(text);
1476 break;
1479 return TRUE;
1483 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id)
1485 on_show_color_chooser1_activate(NULL, NULL);
1489 static gboolean cb_func_view_action(guint key_id)
1491 switch (key_id)
1493 case GEANY_KEYS_VIEW_TOGGLEALL:
1494 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
1495 break;
1496 case GEANY_KEYS_VIEW_SIDEBAR:
1497 on_menu_show_sidebar1_toggled(NULL, NULL);
1498 break;
1499 case GEANY_KEYS_VIEW_ZOOMIN:
1500 on_zoom_in1_activate(NULL, NULL);
1501 break;
1502 case GEANY_KEYS_VIEW_ZOOMOUT:
1503 on_zoom_out1_activate(NULL, NULL);
1504 break;
1505 case GEANY_KEYS_VIEW_ZOOMRESET:
1506 on_normal_size1_activate(NULL, NULL);
1507 break;
1508 default:
1509 break;
1511 return TRUE;
1515 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id)
1517 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1518 ui_lookup_widget(main_widgets.window, "menu_fullscreen1"));
1520 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1524 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id)
1526 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1527 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1529 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1533 static gboolean cb_func_build_action(guint key_id)
1535 GtkWidget *item;
1536 BuildMenuItems *menu_items;
1537 GeanyDocument *doc = document_get_current();
1539 if (doc == NULL)
1540 return TRUE;
1542 if (!gtk_widget_is_sensitive(ui_lookup_widget(main_widgets.window, "menu_build1")))
1543 return TRUE;
1545 menu_items = build_get_menu_items(doc->file_type->id);
1546 /* TODO make it a table??*/
1547 switch (key_id)
1549 case GEANY_KEYS_BUILD_COMPILE:
1550 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_COMPILE)];
1551 break;
1552 case GEANY_KEYS_BUILD_LINK:
1553 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_BUILD)];
1554 break;
1555 case GEANY_KEYS_BUILD_MAKE:
1556 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_ALL)];
1557 break;
1558 case GEANY_KEYS_BUILD_MAKEOWNTARGET:
1559 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_CUSTOM)];
1560 break;
1561 case GEANY_KEYS_BUILD_MAKEOBJECT:
1562 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)];
1563 break;
1564 case GEANY_KEYS_BUILD_NEXTERROR:
1565 item = menu_items->menu_item[GBG_FIXED][GBF_NEXT_ERROR];
1566 break;
1567 case GEANY_KEYS_BUILD_PREVIOUSERROR:
1568 item = menu_items->menu_item[GBG_FIXED][GBF_PREV_ERROR];
1569 break;
1570 case GEANY_KEYS_BUILD_RUN:
1571 item = menu_items->menu_item[GEANY_GBG_EXEC][GBO_TO_CMD(GEANY_GBO_EXEC)];
1572 break;
1573 case GEANY_KEYS_BUILD_OPTIONS:
1574 item = menu_items->menu_item[GBG_FIXED][GBF_COMMANDS];
1575 break;
1576 default:
1577 item = NULL;
1579 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1580 * sensitive state, but some other menus don't update the sensitive status until
1581 * they are redrawn. */
1582 if (item && gtk_widget_is_sensitive(item))
1583 gtk_menu_item_activate(GTK_MENU_ITEM(item));
1584 return TRUE;
1588 static gboolean read_current_word(GeanyDocument *doc, gboolean sci_word)
1590 g_return_val_if_fail(DOC_VALID(doc), FALSE);
1592 if (sci_word)
1594 editor_find_current_word_sciwc(doc->editor, -1,
1595 editor_info.current_word, GEANY_MAX_WORD_LENGTH);
1597 else
1599 editor_find_current_word(doc->editor, -1,
1600 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1603 return (*editor_info.current_word != 0);
1607 static gboolean check_current_word(GeanyDocument *doc, gboolean sci_word)
1609 if (! read_current_word(doc, sci_word))
1611 utils_beep();
1612 return FALSE;
1614 return TRUE;
1618 static gchar *get_current_word_or_sel(GeanyDocument *doc, gboolean sci_word)
1620 ScintillaObject *sci = doc->editor->sci;
1622 if (sci_has_selection(sci))
1623 return sci_get_selection_contents(sci);
1625 return read_current_word(doc, sci_word) ? g_strdup(editor_info.current_word) : NULL;
1629 static void focus_sidebar(void)
1631 if (ui_prefs.sidebar_visible)
1633 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook));
1634 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), page_num);
1636 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1637 gtk_widget_child_focus(page, GTK_DIR_TAB_FORWARD);
1642 static void focus_msgwindow(void)
1644 if (ui_prefs.msgwindow_visible)
1646 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook));
1647 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(msgwindow.notebook), page_num);
1649 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(page)));
1654 static gboolean cb_func_switch_action(guint key_id)
1656 switch (key_id)
1658 case GEANY_KEYS_FOCUS_EDITOR:
1660 GeanyDocument *doc = document_get_current();
1661 if (doc != NULL)
1663 GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
1664 if (gtk_widget_has_focus(sci))
1665 ui_update_statusbar(doc, -1);
1666 else
1667 gtk_widget_grab_focus(sci);
1669 break;
1671 case GEANY_KEYS_FOCUS_SCRIBBLE:
1672 msgwin_switch_tab(MSG_SCRATCH, TRUE);
1673 break;
1674 case GEANY_KEYS_FOCUS_SEARCHBAR:
1675 if (toolbar_prefs.visible)
1677 GtkWidget *search_entry = toolbar_get_widget_child_by_name("SearchEntry");
1678 if (search_entry != NULL)
1679 gtk_widget_grab_focus(search_entry);
1681 break;
1682 case GEANY_KEYS_FOCUS_SIDEBAR:
1683 focus_sidebar();
1684 break;
1685 case GEANY_KEYS_FOCUS_VTE:
1686 msgwin_switch_tab(MSG_VTE, TRUE);
1687 break;
1688 case GEANY_KEYS_FOCUS_COMPILER:
1689 msgwin_switch_tab(MSG_COMPILER, TRUE);
1690 break;
1691 case GEANY_KEYS_FOCUS_MESSAGES:
1692 msgwin_switch_tab(MSG_MESSAGE, TRUE);
1693 break;
1694 case GEANY_KEYS_FOCUS_MESSAGE_WINDOW:
1695 focus_msgwindow();
1696 break;
1697 case GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST:
1698 sidebar_focus_openfiles_tab();
1699 break;
1700 case GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST:
1701 sidebar_focus_symbols_tab();
1702 break;
1704 return TRUE;
1708 static void switch_notebook_page(gint direction)
1710 gint page_count, cur_page, pass;
1711 gboolean parent_is_notebook = FALSE;
1712 GtkNotebook *notebook;
1713 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1715 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1718 parent_is_notebook = GTK_IS_NOTEBOOK(focusw);
1720 while (! parent_is_notebook && (focusw = gtk_widget_get_parent(focusw)) != NULL);
1722 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1723 if (parent_is_notebook)
1724 notebook = GTK_NOTEBOOK(focusw);
1725 else
1726 notebook = GTK_NOTEBOOK(main_widgets.notebook);
1728 /* now switch pages */
1729 page_count = gtk_notebook_get_n_pages(notebook);
1730 cur_page = gtk_notebook_get_current_page(notebook);
1732 /* find the next visible page in the wanted direction, but don't loop
1733 * indefinitely if no pages are visible */
1734 for (pass = 0; pass < page_count; pass++)
1736 GtkWidget *child;
1738 if (direction == GTK_DIR_LEFT)
1740 if (cur_page > 0)
1741 cur_page--;
1742 else
1743 cur_page = page_count - 1;
1745 else if (direction == GTK_DIR_RIGHT)
1747 if (cur_page < page_count - 1)
1748 cur_page++;
1749 else
1750 cur_page = 0;
1753 child = gtk_notebook_get_nth_page (notebook, cur_page);
1754 if (gtk_widget_get_visible (child))
1756 gtk_notebook_set_current_page(notebook, cur_page);
1757 break;
1763 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id)
1765 switch_notebook_page(GTK_DIR_LEFT);
1769 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id)
1771 switch_notebook_page(GTK_DIR_RIGHT);
1775 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id)
1777 notebook_switch_tablastused();
1781 /* move document left/right/first/last */
1782 static void cb_func_move_tab(guint key_id)
1784 GtkWidget *sci;
1785 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
1786 gint cur_page = gtk_notebook_get_current_page(nb);
1787 GeanyDocument *doc = document_get_current();
1789 if (doc == NULL)
1790 return;
1792 sci = GTK_WIDGET(doc->editor->sci);
1794 switch (key_id)
1796 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT:
1797 gtk_notebook_reorder_child(nb, sci, cur_page - 1); /* notebook wraps around by default */
1798 break;
1799 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT:
1801 gint npage = cur_page + 1;
1803 if (npage == gtk_notebook_get_n_pages(nb))
1804 npage = 0; /* wraparound */
1805 gtk_notebook_reorder_child(nb, sci, npage);
1806 break;
1808 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST:
1809 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? 0 : -1);
1810 break;
1811 case GEANY_KEYS_NOTEBOOK_MOVETABLAST:
1812 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? -1 : 0);
1813 break;
1815 return;
1819 static void goto_matching_brace(GeanyDocument *doc)
1821 gint pos, new_pos;
1822 gint after_brace;
1824 g_return_if_fail(DOC_VALID(doc));
1826 pos = sci_get_current_position(doc->editor->sci);
1827 after_brace = pos > 0 && utils_isbrace(sci_get_char_at(doc->editor->sci, pos - 1), TRUE);
1828 pos -= after_brace; /* set pos to the brace */
1830 new_pos = sci_find_matching_brace(doc->editor->sci, pos);
1831 if (new_pos != -1)
1832 { /* set the cursor at/after the brace */
1833 sci_set_current_position(doc->editor->sci, new_pos + (!after_brace), FALSE);
1834 editor_display_current_line(doc->editor, 0.5F);
1839 static gboolean cb_func_clipboard_action(guint key_id)
1841 GeanyDocument *doc = document_get_current();
1843 if (doc == NULL)
1844 return TRUE;
1846 switch (key_id)
1848 case GEANY_KEYS_CLIPBOARD_CUT:
1849 on_cut1_activate(NULL, NULL);
1850 break;
1851 case GEANY_KEYS_CLIPBOARD_COPY:
1852 on_copy1_activate(NULL, NULL);
1853 break;
1854 case GEANY_KEYS_CLIPBOARD_PASTE:
1855 on_paste1_activate(NULL, NULL);
1856 break;
1857 case GEANY_KEYS_CLIPBOARD_COPYLINE:
1858 sci_send_command(doc->editor->sci, SCI_LINECOPY);
1859 break;
1860 case GEANY_KEYS_CLIPBOARD_CUTLINE:
1861 sci_send_command(doc->editor->sci, SCI_LINECUT);
1862 break;
1864 return TRUE;
1868 static void goto_tag(GeanyDocument *doc, gboolean definition)
1870 gchar *text = get_current_word_or_sel(doc, FALSE);
1872 if (text)
1873 symbols_goto_tag(text, definition);
1874 else
1875 utils_beep();
1877 g_free(text);
1881 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
1882 static gboolean cb_func_goto_action(guint key_id)
1884 gint cur_line;
1885 GeanyDocument *doc = document_get_current();
1887 if (doc == NULL)
1888 return TRUE;
1890 cur_line = sci_get_current_line(doc->editor->sci);
1892 switch (key_id)
1894 case GEANY_KEYS_GOTO_BACK:
1895 navqueue_go_back();
1896 return TRUE;
1897 case GEANY_KEYS_GOTO_FORWARD:
1898 navqueue_go_forward();
1899 return TRUE;
1900 case GEANY_KEYS_GOTO_LINE:
1902 if (toolbar_prefs.visible)
1904 GtkWidget *wid = toolbar_get_widget_child_by_name("GotoEntry");
1906 /* use toolbar item if shown & not in the drop down overflow menu */
1907 if (wid && gtk_widget_get_mapped(wid))
1909 gtk_widget_grab_focus(wid);
1910 return TRUE;
1913 on_go_to_line_activate(NULL, NULL);
1914 return TRUE;
1916 case GEANY_KEYS_GOTO_MATCHINGBRACE:
1917 goto_matching_brace(doc);
1918 return TRUE;
1919 case GEANY_KEYS_GOTO_TOGGLEMARKER:
1921 sci_toggle_marker_at_line(doc->editor->sci, cur_line, 1);
1922 return TRUE;
1924 case GEANY_KEYS_GOTO_NEXTMARKER:
1926 gint mline = sci_marker_next(doc->editor->sci, cur_line + 1, 1 << 1, TRUE);
1928 if (mline != -1)
1930 sci_set_current_line(doc->editor->sci, mline);
1931 editor_display_current_line(doc->editor, 0.5F);
1933 return TRUE;
1935 case GEANY_KEYS_GOTO_PREVIOUSMARKER:
1937 gint mline = sci_marker_previous(doc->editor->sci, cur_line - 1, 1 << 1, TRUE);
1939 if (mline != -1)
1941 sci_set_current_line(doc->editor->sci, mline);
1942 editor_display_current_line(doc->editor, 0.5F);
1944 return TRUE;
1946 case GEANY_KEYS_GOTO_TAGDEFINITION:
1947 goto_tag(doc, TRUE);
1948 return TRUE;
1949 case GEANY_KEYS_GOTO_TAGDECLARATION:
1950 goto_tag(doc, FALSE);
1951 return TRUE;
1953 /* only check editor-sensitive keybindings when editor has focus so home,end still
1954 * work in other widgets */
1955 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
1956 return FALSE;
1958 switch (key_id)
1960 case GEANY_KEYS_GOTO_LINESTART:
1961 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
1962 break;
1963 case GEANY_KEYS_GOTO_LINEEND:
1964 sci_send_command(doc->editor->sci, SCI_LINEEND);
1965 break;
1966 case GEANY_KEYS_GOTO_LINESTARTVISUAL:
1967 sci_send_command(doc->editor->sci, SCI_HOMEDISPLAY);
1968 break;
1969 case GEANY_KEYS_GOTO_LINEENDVISUAL:
1970 sci_send_command(doc->editor->sci, SCI_LINEENDDISPLAY);
1971 break;
1972 case GEANY_KEYS_GOTO_PREVWORDPART:
1973 sci_send_command(doc->editor->sci, SCI_WORDPARTLEFT);
1974 break;
1975 case GEANY_KEYS_GOTO_NEXTWORDPART:
1976 sci_send_command(doc->editor->sci, SCI_WORDPARTRIGHT);
1977 break;
1979 return TRUE;
1983 static void duplicate_lines(GeanyEditor *editor)
1985 if (sci_get_lines_selected(editor->sci) > 1)
1986 { /* ignore extra_line because of selecting lines from the line number column */
1987 editor_select_lines(editor, FALSE);
1988 sci_selection_duplicate(editor->sci);
1990 else if (sci_has_selection(editor->sci))
1991 sci_selection_duplicate(editor->sci);
1992 else
1993 sci_line_duplicate(editor->sci);
1997 static void delete_lines(GeanyEditor *editor)
1999 editor_select_lines(editor, TRUE); /* include last line (like cut lines, copy lines do) */
2000 sci_clear(editor->sci); /* (SCI_LINEDELETE only does 1 line) */
2004 /* common function for editor keybindings, only valid when scintilla has focus. */
2005 static gboolean cb_func_editor_action(guint key_id)
2007 GeanyDocument *doc = document_get_current();
2008 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2010 /* edit keybindings only valid when scintilla widget has focus */
2011 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2012 return FALSE; /* also makes tab work outside editor */
2014 switch (key_id)
2016 case GEANY_KEYS_EDITOR_UNDO:
2017 on_undo1_activate(NULL, NULL);
2018 break;
2019 case GEANY_KEYS_EDITOR_REDO:
2020 on_redo1_activate(NULL, NULL);
2021 break;
2022 case GEANY_KEYS_EDITOR_SCROLLTOLINE:
2023 editor_scroll_to_line(doc->editor, -1, 0.5F);
2024 break;
2025 case GEANY_KEYS_EDITOR_SCROLLLINEUP:
2026 sci_send_command(doc->editor->sci, SCI_LINESCROLLUP);
2027 break;
2028 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN:
2029 sci_send_command(doc->editor->sci, SCI_LINESCROLLDOWN);
2030 break;
2031 case GEANY_KEYS_EDITOR_DUPLICATELINE:
2032 duplicate_lines(doc->editor);
2033 break;
2034 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR:
2035 editor_goto_next_snippet_cursor(doc->editor);
2036 break;
2037 case GEANY_KEYS_EDITOR_DELETELINE:
2038 delete_lines(doc->editor);
2039 break;
2040 case GEANY_KEYS_EDITOR_DELETELINETOEND:
2041 sci_send_command(doc->editor->sci, SCI_DELLINERIGHT);
2042 break;
2043 case GEANY_KEYS_EDITOR_TRANSPOSELINE:
2044 sci_send_command(doc->editor->sci, SCI_LINETRANSPOSE);
2045 break;
2046 case GEANY_KEYS_EDITOR_AUTOCOMPLETE:
2047 editor_start_auto_complete(doc->editor, sci_get_current_position(doc->editor->sci), TRUE);
2048 break;
2049 case GEANY_KEYS_EDITOR_CALLTIP:
2050 editor_show_calltip(doc->editor, -1);
2051 break;
2052 case GEANY_KEYS_EDITOR_MACROLIST:
2053 editor_show_macro_list(doc->editor);
2054 break;
2055 case GEANY_KEYS_EDITOR_CONTEXTACTION:
2056 if (check_current_word(doc, FALSE))
2057 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu,
2058 "context_action1")), NULL);
2059 break;
2060 case GEANY_KEYS_EDITOR_COMPLETESNIPPET:
2061 /* allow tab to be overloaded */
2062 return check_snippet_completion(doc);
2064 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION:
2066 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
2067 GEANY_KEYS_EDITOR_COMPLETESNIPPET);
2069 switch (kb->key)
2071 case GDK_space:
2072 sci_add_text(doc->editor->sci, " ");
2073 break;
2074 case GDK_Tab:
2075 sci_send_command(doc->editor->sci, SCI_TAB);
2076 break;
2077 default:
2078 break;
2080 break;
2082 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION:
2083 return editor_complete_word_part(doc->editor);
2085 case GEANY_KEYS_EDITOR_MOVELINEUP:
2086 sci_move_selected_lines_up(doc->editor->sci);
2087 break;
2088 case GEANY_KEYS_EDITOR_MOVELINEDOWN:
2089 sci_move_selected_lines_down(doc->editor->sci);
2090 break;
2092 return TRUE;
2096 static void join_lines(GeanyEditor *editor)
2098 gint start, end, i;
2100 start = sci_get_line_from_position(editor->sci,
2101 sci_get_selection_start(editor->sci));
2102 end = sci_get_line_from_position(editor->sci,
2103 sci_get_selection_end(editor->sci));
2105 /* remove spaces surrounding the lines so that these spaces
2106 * won't appear within text after joining */
2107 for (i = start; i < end; i++)
2108 editor_strip_line_trailing_spaces(editor, i);
2109 for (i = start + 1; i <= end; i++)
2110 sci_set_line_indentation(editor->sci, i, 0);
2112 sci_set_target_start(editor->sci,
2113 sci_get_position_from_line(editor->sci, start));
2114 sci_set_target_end(editor->sci,
2115 sci_get_position_from_line(editor->sci, end));
2116 sci_lines_join(editor->sci);
2120 static gint get_reflow_column(GeanyEditor *editor)
2122 const GeanyEditorPrefs *eprefs = editor_get_prefs(editor);
2123 if (editor->line_breaking)
2124 return eprefs->line_break_column;
2125 else if (eprefs->long_line_type != 2)
2126 return eprefs->long_line_column;
2127 else
2128 return -1; /* do nothing */
2132 /* Split the line where the cursor is positioned, on `column`,
2133 possibly many times if the line is long.
2134 Return the number of lines added because of the splitting. */
2135 static gint split_line(GeanyEditor *editor, gint column)
2137 ScintillaObject *sci = editor->sci;
2138 gint start_line = sci_get_current_line(sci);
2139 gint line = start_line;
2141 while (TRUE)
2143 gint lstart = sci_get_position_from_line(sci, line);
2144 gint lend = sci_get_line_end_position(sci, line);
2145 gint edge = sci_get_position_from_col(sci, line, column);
2146 gboolean found;
2147 gint pos;
2149 /* don't split on a trailing space of a line */
2150 if (sci_get_char_at(sci, lend - 1) == GDK_space)
2151 lend--;
2153 /* detect when the line is short enough and no more splitting is needed */
2154 if (sci_get_col_from_position(sci, lend) < column)
2155 break;
2157 /* lookup split position */
2158 found = FALSE;
2159 for (pos = edge - 1; pos > lstart; pos--)
2161 if (sci_get_char_at(sci, pos) == GDK_space)
2163 found = TRUE;
2164 break;
2167 if (!found)
2169 for (pos = edge; pos < lend; pos++)
2171 if (sci_get_char_at(sci, pos) == GDK_space)
2173 found = TRUE;
2174 break;
2178 if (!found)
2179 break;
2181 sci_set_current_position(sci, pos + 1, FALSE);
2182 sci_cancel(sci); /* don't select from completion list */
2183 sci_send_command(sci, SCI_NEWLINE);
2184 line++;
2186 return line - start_line;
2190 static void reflow_lines(GeanyEditor *editor, gint column)
2192 gint start, indent, linescount, i;
2194 start = sci_get_line_from_position(editor->sci,
2195 sci_get_selection_start(editor->sci));
2197 /* if several lines are selected, join them. */
2198 if (sci_get_lines_selected(editor->sci) > 1)
2199 join_lines(editor);
2201 /* if this line is short enough, do nothing */
2202 if (column > sci_get_line_end_position(editor->sci, start) -
2203 sci_get_position_from_line(editor->sci, start))
2205 return;
2209 * We have to manipulate line indentation so that indentation
2210 * of the resulting lines would be consistent. For example,
2211 * the result of splitting "[TAB]very long content":
2213 * +-------------+-------------+
2214 * | proper | wrong |
2215 * +-------------+-------------+
2216 * | [TAB]very | [TAB]very |
2217 * | [TAB]long | long |
2218 * | [TAB]content| content |
2219 * +-------------+-------------+
2221 indent = sci_get_line_indentation(editor->sci, start);
2222 sci_set_line_indentation(editor->sci, start, 0);
2224 linescount = split_line(editor, column - indent);
2226 /* Fix indentation. */
2227 for (i = start; i <= start + linescount; i++)
2228 sci_set_line_indentation(editor->sci, i, indent);
2230 /* Remove trailing spaces. */
2231 if (editor_prefs.newline_strip || file_prefs.strip_trailing_spaces)
2233 for (i = start; i <= start + linescount; i++)
2234 editor_strip_line_trailing_spaces(editor, i);
2239 /* deselect last newline of selection, if any */
2240 static void sci_deselect_last_newline(ScintillaObject *sci)
2242 gint start, end;
2244 start = sci_get_selection_start(sci);
2245 end = sci_get_selection_end(sci);
2246 if (end > start && sci_get_col_from_position(sci, end) == 0)
2248 end = sci_get_line_end_position(sci, sci_get_line_from_position(sci, end-1));
2249 sci_set_selection(sci, start, end);
2254 static void reflow_paragraph(GeanyEditor *editor)
2256 ScintillaObject *sci = editor->sci;
2257 gboolean sel;
2258 gint column;
2260 column = get_reflow_column(editor);
2261 if (column == -1)
2263 utils_beep();
2264 return;
2267 sci_start_undo_action(sci);
2268 sel = sci_has_selection(sci);
2269 if (!sel)
2270 editor_select_indent_block(editor);
2271 sci_deselect_last_newline(sci);
2272 reflow_lines(editor, column);
2273 if (!sel)
2274 sci_set_anchor(sci, -1);
2276 sci_end_undo_action(sci);
2280 static void join_paragraph(GeanyEditor *editor)
2282 ScintillaObject *sci = editor->sci;
2283 gboolean sel;
2284 gint column;
2286 column = get_reflow_column(editor);
2287 if (column == -1)
2289 utils_beep();
2290 return;
2293 sci_start_undo_action(sci);
2294 sel = sci_has_selection(sci);
2295 if (!sel)
2296 editor_select_indent_block(editor);
2297 sci_deselect_last_newline(sci);
2298 join_lines(editor);
2299 if (!sel)
2300 sci_set_anchor(sci, -1);
2302 sci_end_undo_action(sci);
2306 /* common function for format keybindings, only valid when scintilla has focus. */
2307 static gboolean cb_func_format_action(guint key_id)
2309 GeanyDocument *doc = document_get_current();
2310 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2312 /* keybindings only valid when scintilla widget has focus */
2313 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2314 return TRUE;
2316 switch (key_id)
2318 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE:
2319 on_menu_toggle_line_commentation1_activate(NULL, NULL);
2320 break;
2321 case GEANY_KEYS_FORMAT_COMMENTLINE:
2322 on_menu_comment_line1_activate(NULL, NULL);
2323 break;
2324 case GEANY_KEYS_FORMAT_UNCOMMENTLINE:
2325 on_menu_uncomment_line1_activate(NULL, NULL);
2326 break;
2327 case GEANY_KEYS_FORMAT_INCREASEINDENT:
2328 on_menu_increase_indent1_activate(NULL, NULL);
2329 break;
2330 case GEANY_KEYS_FORMAT_DECREASEINDENT:
2331 on_menu_decrease_indent1_activate(NULL, NULL);
2332 break;
2333 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE:
2334 editor_indentation_by_one_space(doc->editor, -1, FALSE);
2335 break;
2336 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE:
2337 editor_indentation_by_one_space(doc->editor, -1, TRUE);
2338 break;
2339 case GEANY_KEYS_FORMAT_AUTOINDENT:
2340 editor_smart_line_indentation(doc->editor, -1);
2341 break;
2342 case GEANY_KEYS_FORMAT_TOGGLECASE:
2343 on_toggle_case1_activate(NULL, NULL);
2344 break;
2345 case GEANY_KEYS_FORMAT_SENDTOCMD1:
2346 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 0)
2347 tools_execute_custom_command(doc, ui_prefs.custom_commands[0]);
2348 break;
2349 case GEANY_KEYS_FORMAT_SENDTOCMD2:
2350 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 1)
2351 tools_execute_custom_command(doc, ui_prefs.custom_commands[1]);
2352 break;
2353 case GEANY_KEYS_FORMAT_SENDTOCMD3:
2354 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 2)
2355 tools_execute_custom_command(doc, ui_prefs.custom_commands[2]);
2356 break;
2357 case GEANY_KEYS_FORMAT_SENDTOVTE:
2358 on_send_selection_to_vte1_activate(NULL, NULL);
2359 break;
2360 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
2361 reflow_paragraph(doc->editor);
2362 break;
2363 case GEANY_KEYS_FORMAT_JOINLINES:
2364 join_paragraph(doc->editor);
2365 break;
2367 return TRUE;
2371 /* common function for select keybindings, only valid when scintilla has focus. */
2372 static gboolean cb_func_select_action(guint key_id)
2374 GeanyDocument *doc;
2375 ScintillaObject *sci;
2376 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2377 GtkWidget *toolbar_search_entry = toolbar_get_widget_child_by_name("SearchEntry");
2378 GtkWidget *toolbar_goto_entry = toolbar_get_widget_child_by_name("GotoEntry");
2380 /* special case for Select All in the scribble widget */
2381 if (key_id == GEANY_KEYS_SELECT_ALL && focusw == msgwindow.scribble)
2383 g_signal_emit_by_name(msgwindow.scribble, "select-all", TRUE);
2384 return TRUE;
2386 /* special case for Select All in the VTE widget */
2387 #ifdef HAVE_VTE
2388 else if (key_id == GEANY_KEYS_SELECT_ALL && vte_info.have_vte && focusw == vc->vte)
2390 vte_select_all();
2391 return TRUE;
2393 #endif
2394 /* special case for Select All in the toolbar search widget */
2395 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_search_entry)
2397 gtk_editable_select_region(GTK_EDITABLE(toolbar_search_entry), 0, -1);
2398 return TRUE;
2400 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_goto_entry)
2402 gtk_editable_select_region(GTK_EDITABLE(toolbar_goto_entry), 0, -1);
2403 return TRUE;
2406 doc = document_get_current();
2407 /* keybindings only valid when scintilla widget has focus */
2408 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2409 return TRUE;
2410 sci = doc->editor->sci;
2412 switch (key_id)
2414 case GEANY_KEYS_SELECT_ALL:
2415 on_menu_select_all1_activate(NULL, NULL);
2416 break;
2417 case GEANY_KEYS_SELECT_WORD:
2418 editor_select_word(doc->editor);
2419 break;
2420 case GEANY_KEYS_SELECT_LINE:
2421 editor_select_lines(doc->editor, FALSE);
2422 break;
2423 case GEANY_KEYS_SELECT_PARAGRAPH:
2424 editor_select_paragraph(doc->editor);
2425 break;
2426 case GEANY_KEYS_SELECT_WORDPARTLEFT:
2427 sci_send_command(sci, SCI_WORDPARTLEFTEXTEND);
2428 break;
2429 case GEANY_KEYS_SELECT_WORDPARTRIGHT:
2430 sci_send_command(sci, SCI_WORDPARTRIGHTEXTEND);
2431 break;
2433 return TRUE;
2437 static gboolean cb_func_document_action(guint key_id)
2439 GeanyDocument *doc = document_get_current();
2441 if (doc == NULL)
2442 return TRUE;
2444 switch (key_id)
2446 case GEANY_KEYS_DOCUMENT_REPLACETABS:
2447 on_replace_tabs_activate(NULL, NULL);
2448 break;
2449 case GEANY_KEYS_DOCUMENT_REPLACESPACES:
2450 on_replace_spaces_activate(NULL, NULL);
2451 break;
2452 case GEANY_KEYS_DOCUMENT_LINEBREAK:
2453 on_line_breaking1_activate(NULL, NULL);
2454 ui_document_show_hide(doc);
2455 break;
2456 case GEANY_KEYS_DOCUMENT_LINEWRAP:
2457 on_line_wrapping1_toggled(NULL, NULL);
2458 ui_document_show_hide(doc);
2459 break;
2460 case GEANY_KEYS_DOCUMENT_CLONE:
2461 document_clone(doc);
2462 break;
2463 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST:
2464 document_update_tags(doc);
2465 break;
2466 case GEANY_KEYS_DOCUMENT_FOLDALL:
2467 editor_fold_all(doc->editor);
2468 break;
2469 case GEANY_KEYS_DOCUMENT_UNFOLDALL:
2470 editor_unfold_all(doc->editor);
2471 break;
2472 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD:
2473 if (editor_prefs.folding)
2475 gint line = sci_get_current_line(doc->editor->sci);
2476 editor_toggle_fold(doc->editor, line, 0);
2477 break;
2479 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS:
2480 on_remove_markers1_activate(NULL, NULL);
2481 break;
2482 case GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS:
2483 on_menu_remove_indicators1_activate(NULL, NULL);
2484 break;
2485 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS:
2486 on_remove_markers1_activate(NULL, NULL);
2487 on_menu_remove_indicators1_activate(NULL, NULL);
2488 break;
2490 return TRUE;
2494 static void insert_line_after(GeanyEditor *editor)
2496 ScintillaObject *sci = editor->sci;
2498 sci_send_command(sci, SCI_LINEEND);
2499 sci_send_command(sci, SCI_NEWLINE);
2503 static void insert_line_before(GeanyEditor *editor)
2505 ScintillaObject *sci = editor->sci;
2506 gint line = sci_get_current_line(sci);
2507 gint indentpos = sci_get_line_indent_position(sci, line);
2509 sci_set_current_position(sci, indentpos, TRUE);
2510 sci_send_command(sci, SCI_NEWLINE);
2511 sci_send_command(sci, SCI_LINEUP);
2515 /* common function for insert keybindings, only valid when scintilla has focus. */
2516 static gboolean cb_func_insert_action(guint key_id)
2518 GeanyDocument *doc = document_get_current();
2519 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2521 /* keybindings only valid when scintilla widget has focus */
2522 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2523 return TRUE;
2525 switch (key_id)
2527 case GEANY_KEYS_INSERT_ALTWHITESPACE:
2528 editor_insert_alternative_whitespace(doc->editor);
2529 break;
2530 case GEANY_KEYS_INSERT_DATE:
2531 gtk_menu_item_activate(GTK_MENU_ITEM(
2532 ui_lookup_widget(main_widgets.window, "insert_date_custom1")));
2533 break;
2534 case GEANY_KEYS_INSERT_LINEAFTER:
2535 insert_line_after(doc->editor);
2536 break;
2537 case GEANY_KEYS_INSERT_LINEBEFORE:
2538 insert_line_before(doc->editor);
2539 break;
2541 return TRUE;
2545 /* update key combination */
2546 void keybindings_update_combo(GeanyKeyBinding *kb, guint key, GdkModifierType mods)
2548 GtkWidget *widget = kb->menu_item;
2550 if (widget && kb->key)
2551 gtk_widget_remove_accelerator(widget, kb_accel_group, kb->key, kb->mods);
2553 kb->key = key;
2554 kb->mods = mods;
2556 if (widget && kb->key)
2557 gtk_widget_add_accelerator(widget, "activate", kb_accel_group,
2558 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
2562 /* used for plugins, can be called repeatedly. */
2563 GeanyKeyGroup *keybindings_set_group(GeanyKeyGroup *group, const gchar *section_name,
2564 const gchar *label, gsize count, GeanyKeyGroupCallback callback)
2566 g_return_val_if_fail(section_name, NULL);
2567 g_return_val_if_fail(count, NULL);
2569 /* prevent conflict with core bindings */
2570 g_return_val_if_fail(!g_str_equal(section_name, keybindings_keyfile_group_name), NULL);
2572 if (!group)
2574 group = g_new0(GeanyKeyGroup, 1);
2575 add_kb_group(group, section_name, label, callback, TRUE);
2577 g_free(group->plugin_keys);
2578 group->plugin_keys = g_new0(GeanyKeyBinding, count);
2579 group->plugin_key_count = count;
2580 g_ptr_array_set_size(group->key_items, 0);
2581 return group;
2585 void keybindings_free_group(GeanyKeyGroup *group)
2587 GeanyKeyBinding *kb;
2589 g_ptr_array_free(group->key_items, TRUE);
2591 if (group->plugin)
2593 foreach_c_array(kb, group->plugin_keys, group->plugin_key_count)
2595 g_free(kb->name);
2596 g_free(kb->label);
2598 g_free(group->plugin_keys);
2599 g_ptr_array_remove_fast(keybinding_groups, group);
2600 g_free(group);