Add API docs for keybinding enums.
[geany-mirror.git] / src / keybindings.c
blobe048df4bd63cbc9cf1d19beb29f99a2b459bf0a3
1 /*
2 * keybindings.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2010 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2010 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
21 * $Id$
24 /**
25 * @file keybindings.h
26 * Configurable keyboard shortcuts.
27 * - keybindings_send_command() mimics a built-in keybinding.
28 * - @ref GeanyKeyGroupID lists groups of built-in keybindings.
29 * @see plugin_set_key_group().
30 **/
33 #include "geany.h"
35 #include <gdk/gdkkeysyms.h>
36 #include <string.h>
38 #include "keybindings.h"
39 #include "support.h"
40 #include "utils.h"
41 #include "ui_utils.h"
42 #include "document.h"
43 #include "documentprivate.h"
44 #include "filetypes.h"
45 #include "callbacks.h"
46 #include "prefs.h"
47 #include "msgwindow.h"
48 #include "editor.h"
49 #include "sciwrappers.h"
50 #include "build.h"
51 #include "tools.h"
52 #include "navqueue.h"
53 #include "symbols.h"
54 #include "vte.h"
55 #include "toolbar.h"
56 #include "sidebar.h"
57 #include "geanywraplabel.h"
58 #include "main.h"
59 #include "search.h"
62 GPtrArray *keybinding_groups; /* array of GeanyKeyGroup pointers */
64 /* keyfile group name for non-plugin KB groups */
65 const gchar keybindings_keyfile_group_name[] = "Bindings";
67 static GtkAccelGroup *kb_accel_group = NULL;
68 static const gboolean swap_alt_tab_order = FALSE;
70 const gsize MAX_MRU_DOCS = 20;
71 static GQueue *mru_docs = NULL;
72 static guint mru_pos = 0;
74 static gboolean switch_dialog_cancelled = TRUE;
75 static GtkWidget *switch_dialog = NULL;
76 static GtkWidget *switch_dialog_label = NULL;
79 /* central keypress event handler, almost all keypress events go to this function */
80 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
81 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
83 static gboolean check_current_word(GeanyDocument *doc);
84 static gboolean read_current_word(GeanyDocument *doc);
85 static gchar *get_current_word_or_sel(GeanyDocument *doc);
87 static gboolean cb_func_file_action(guint key_id);
88 static gboolean cb_func_project_action(guint key_id);
89 static gboolean cb_func_editor_action(guint key_id);
90 static gboolean cb_func_select_action(guint key_id);
91 static gboolean cb_func_format_action(guint key_id);
92 static gboolean cb_func_insert_action(guint key_id);
93 static gboolean cb_func_search_action(guint key_id);
94 static gboolean cb_func_goto_action(guint key_id);
95 static gboolean cb_func_switch_action(guint key_id);
96 static gboolean cb_func_clipboard_action(guint key_id);
97 static gboolean cb_func_build_action(guint key_id);
98 static gboolean cb_func_document_action(guint key_id);
99 static gboolean cb_func_view_action(guint key_id);
101 /* note: new keybindings should normally use per group callbacks */
102 static void cb_func_menu_help(guint key_id);
103 static void cb_func_menu_preferences(guint key_id);
105 static void cb_func_menu_fullscreen(guint key_id);
106 static void cb_func_menu_messagewindow(guint key_id);
108 static void cb_func_menu_opencolorchooser(guint key_id);
110 static void cb_func_switch_tableft(guint key_id);
111 static void cb_func_switch_tabright(guint key_id);
112 static void cb_func_switch_tablastused(guint key_id);
113 static void cb_func_move_tab(guint key_id);
115 static void add_popup_menu_accels(void);
118 /** Looks up a keybinding item.
119 * @param group Group.
120 * @param key_id Keybinding index for the group.
121 * @return The keybinding.
122 * @since 0.19. */
123 GeanyKeyBinding *keybindings_get_item(GeanyKeyGroup *group, gsize key_id)
125 g_assert(key_id < group->count);
127 return &group->keys[key_id];
131 /* This is used to set default keybindings on startup.
132 * Menu accels are set in apply_kb_accel(). */
133 /** Fills a GeanyKeyBinding struct item.
134 * @param group Group.
135 * @param key_id Keybinding index for the group.
136 * @param callback Function to call when activated, or @c NULL to use the group callback.
137 * Usually it's better to use the group callback instead - see plugin_set_key_group().
138 * @param key (Lower case) default key, e.g. @c GDK_j, but usually 0 for unset.
139 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
140 * @param kf_name Key name for the configuration file, such as @c "menu_new".
141 * @param label Label used in the preferences dialog keybindings tab. May contain
142 * underscores - these won't be displayed.
143 * @param menu_item Optional widget to set an accelerator for, or @c NULL.
144 * @return The keybinding - normally this is ignored. */
145 GeanyKeyBinding *keybindings_set_item(GeanyKeyGroup *group, gsize key_id,
146 GeanyKeyCallback callback, guint key, GdkModifierType mod,
147 gchar *kf_name, gchar *label, GtkWidget *menu_item)
149 GeanyKeyBinding *kb = keybindings_get_item(group, key_id);
151 if (group->plugin)
153 /* some plugins e.g. GeanyLua need these fields duplicated */
154 setptr(kb->name, g_strdup(kf_name));
155 setptr(kb->label, g_strdup(label));
157 else
159 kb->name = kf_name;
160 kb->label = label;
162 kb->key = key;
163 kb->mods = mod;
164 kb->callback = callback;
165 kb->menu_item = menu_item;
166 return kb;
170 static GeanyKeyGroup *add_kb_group(GeanyKeyGroup *group,
171 const gchar *name, const gchar *label, gsize count, GeanyKeyBinding *keys,
172 GeanyKeyGroupCallback callback)
174 g_ptr_array_add(keybinding_groups, group);
176 group->name = name;
177 group->label = label;
178 group->count = count;
179 group->keys = keys;
180 group->callback = callback;
181 return group;
185 /* Lookup a widget in the main window */
186 #define LW(widget_name) \
187 ui_lookup_widget(main_widgets.window, G_STRINGIFY(widget_name))
189 /* Expansion for group_id = FILE:
190 * static GeanyKeyBinding FILE_keys[GEANY_KEYS_FILE_COUNT]; */
191 #define DECLARE_KEYS(group_id) \
192 static GeanyKeyBinding group_id ## _keys[GEANY_KEYS_ ## group_id ## _COUNT]
194 /* Expansion for group_id = FILE:
195 * add_kb_group(&groups[GEANY_KEY_GROUP_FILE], NULL, _("File menu"),
196 * GEANY_KEYS_FILE_COUNT, FILE_keys, callback); */
197 #define ADD_KB_GROUP(group_id, label, callback) \
198 add_kb_group(&groups[GEANY_KEY_GROUP_ ## group_id], keybindings_keyfile_group_name, label, \
199 GEANY_KEYS_ ## group_id ## _COUNT, group_id ## _keys, callback)
201 /* Init all fields of keys with default values.
202 * The menu_item field is always the main menu item, popup menu accelerators are
203 * set in add_popup_menu_accels(). */
204 static void init_default_kb(void)
206 static GeanyKeyGroup groups[GEANY_KEY_GROUP_COUNT];
207 GeanyKeyGroup *group;
208 DECLARE_KEYS(FILE);
209 DECLARE_KEYS(PROJECT);
210 DECLARE_KEYS(EDITOR);
211 DECLARE_KEYS(CLIPBOARD);
212 DECLARE_KEYS(SELECT);
213 DECLARE_KEYS(FORMAT);
214 DECLARE_KEYS(INSERT);
215 DECLARE_KEYS(SETTINGS);
216 DECLARE_KEYS(SEARCH);
217 DECLARE_KEYS(GOTO);
218 DECLARE_KEYS(VIEW);
219 DECLARE_KEYS(FOCUS);
220 DECLARE_KEYS(NOTEBOOK);
221 DECLARE_KEYS(DOCUMENT);
222 DECLARE_KEYS(BUILD);
223 DECLARE_KEYS(TOOLS);
224 DECLARE_KEYS(HELP);
226 group = ADD_KB_GROUP(FILE, _("File"), cb_func_file_action);
228 keybindings_set_item(group, GEANY_KEYS_FILE_NEW, NULL,
229 GDK_n, GDK_CONTROL_MASK, "menu_new", _("New"), NULL);
230 keybindings_set_item(group, GEANY_KEYS_FILE_OPEN, NULL,
231 GDK_o, GDK_CONTROL_MASK, "menu_open", _("Open"), NULL);
232 keybindings_set_item(group, GEANY_KEYS_FILE_OPENSELECTED, NULL,
233 GDK_o, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_open_selected",
234 _("Open selected file"), LW(menu_open_selected_file1));
235 keybindings_set_item(group, GEANY_KEYS_FILE_SAVE, NULL,
236 GDK_s, GDK_CONTROL_MASK, "menu_save", _("Save"), NULL);
237 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEAS, NULL,
238 0, 0, "menu_saveas", _("Save as"), LW(menu_save_as1));
239 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEALL, NULL,
240 GDK_S, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_saveall", _("Save all"),
241 LW(menu_save_all1));
242 keybindings_set_item(group, GEANY_KEYS_FILE_PRINT, NULL,
243 GDK_p, GDK_CONTROL_MASK, "menu_print", _("Print"), LW(print1));
244 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSE, NULL,
245 GDK_w, GDK_CONTROL_MASK, "menu_close", _("Close"), LW(menu_close1));
246 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSEALL, NULL,
247 GDK_w, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_closeall", _("Close all"),
248 LW(menu_close_all1));
249 keybindings_set_item(group, GEANY_KEYS_FILE_RELOAD, NULL,
250 GDK_r, GDK_CONTROL_MASK, "menu_reloadfile", _("Reload file"), LW(menu_reload1));
251 keybindings_set_item(group, GEANY_KEYS_FILE_OPENLASTTAB, NULL,
252 0, 0, "file_openlasttab", _("Re-open last closed tab"), NULL);
254 group = ADD_KB_GROUP(PROJECT, _("Project"), cb_func_project_action);
256 keybindings_set_item(group, GEANY_KEYS_PROJECT_PROPERTIES, NULL,
257 0, 0, "project_properties", _("Project properties"), LW(project_properties1));
259 group = ADD_KB_GROUP(EDITOR, _("Editor"), cb_func_editor_action);
261 keybindings_set_item(group, GEANY_KEYS_EDITOR_UNDO, NULL,
262 GDK_z, GDK_CONTROL_MASK, "menu_undo", _("Undo"), LW(menu_undo2));
263 keybindings_set_item(group, GEANY_KEYS_EDITOR_REDO, NULL,
264 GDK_y, GDK_CONTROL_MASK, "menu_redo", _("Redo"), LW(menu_redo2));
265 keybindings_set_item(group, GEANY_KEYS_EDITOR_DUPLICATELINE, NULL,
266 GDK_d, GDK_CONTROL_MASK, "edit_duplicateline", _("_Duplicate Line or Selection"),
267 LW(duplicate_line_or_selection1));
268 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINE, NULL,
269 GDK_k, GDK_CONTROL_MASK, "edit_deleteline", _("_Delete Current Line(s)"),
270 LW(delete_current_line_s_1));
271 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINETOEND, NULL,
272 GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_deletelinetoend",
273 _("Delete to line end"), NULL);
274 /* transpose may fit better in format group */
275 keybindings_set_item(group, GEANY_KEYS_EDITOR_TRANSPOSELINE, NULL,
276 GDK_t, GDK_CONTROL_MASK, "edit_transposeline", _("_Transpose Current Line"),
277 LW(transpose_current_line1));
278 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLTOLINE, NULL,
279 GDK_l, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_scrolltoline", _("Scroll to current line"), NULL);
280 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEUP, NULL,
281 GDK_Up, GDK_MOD1_MASK, "edit_scrolllineup", _("Scroll up the view by one line"), NULL);
282 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEDOWN, NULL,
283 GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
284 keybindings_set_item(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL,
285 GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
286 keybindings_set_item(group, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR, NULL,
287 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
288 keybindings_set_item(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, NULL,
289 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
290 keybindings_set_item(group, GEANY_KEYS_EDITOR_CONTEXTACTION, NULL,
291 0, 0, "popup_contextaction", _("Context Action"), NULL);
292 keybindings_set_item(group, GEANY_KEYS_EDITOR_AUTOCOMPLETE, NULL,
293 GDK_space, GDK_CONTROL_MASK, "edit_autocomplete", _("Complete word"), NULL);
294 keybindings_set_item(group, GEANY_KEYS_EDITOR_CALLTIP, NULL,
295 GDK_space, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_calltip", _("Show calltip"), NULL);
296 keybindings_set_item(group, GEANY_KEYS_EDITOR_MACROLIST, NULL,
297 GDK_Return, GDK_CONTROL_MASK, "edit_macrolist", _("Show macro list"), NULL);
298 keybindings_set_item(group, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION, NULL,
299 GDK_Tab, 0, "edit_wordpartcompletion", _("Word part completion"), NULL);
300 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEUP, NULL,
301 0, 0, "edit_movelineup", _("Move line(s) up"), NULL);
302 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEDOWN, NULL,
303 0, 0, "edit_movelinedown", _("Move line(s) down"), NULL);
305 group = ADD_KB_GROUP(CLIPBOARD, _("Clipboard"), cb_func_clipboard_action);
307 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUT, NULL,
308 GDK_x, GDK_CONTROL_MASK, "menu_cut", _("Cut"), NULL);
309 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPY, NULL,
310 GDK_c, GDK_CONTROL_MASK, "menu_copy", _("Copy"), NULL);
311 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_PASTE, NULL,
312 GDK_v, GDK_CONTROL_MASK, "menu_paste", _("Paste"), NULL);
313 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPYLINE, NULL,
314 GDK_c, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_copyline", _("_Copy Current Line(s)"),
315 LW(cut_current_line_s_1));
316 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUTLINE, NULL,
317 GDK_x, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_cutline", _("_Cut Current Line(s)"),
318 LW(copy_current_line_s_1));
320 group = ADD_KB_GROUP(SELECT, _("Select"), cb_func_select_action);
322 keybindings_set_item(group, GEANY_KEYS_SELECT_ALL, NULL,
323 GDK_a, GDK_CONTROL_MASK, "menu_selectall", _("Select All"), LW(menu_select_all1));
324 keybindings_set_item(group, GEANY_KEYS_SELECT_WORD, NULL,
325 GDK_w, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectword", _("Select current word"), NULL);
326 keybindings_set_item(group, GEANY_KEYS_SELECT_LINE, NULL,
327 GDK_l, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectline", _("_Select Current Line(s)"),
328 LW(select_current_line_s_1));
329 keybindings_set_item(group, GEANY_KEYS_SELECT_PARAGRAPH, NULL,
330 GDK_p, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectparagraph", _("_Select Current Paragraph"),
331 LW(select_current_paragraph1));
332 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTLEFT, NULL,
333 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL);
334 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTRIGHT, NULL,
335 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL);
337 group = ADD_KB_GROUP(FORMAT, _("Format"), cb_func_format_action);
339 keybindings_set_item(group, GEANY_KEYS_FORMAT_TOGGLECASE, NULL,
340 GDK_u, GDK_CONTROL_MASK | GDK_MOD1_MASK, "edit_togglecase",
341 _("Toggle Case of Selection"), LW(menu_toggle_case2));
342 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE, NULL,
343 GDK_e, GDK_CONTROL_MASK, "edit_commentlinetoggle", _("Toggle line commentation"),
344 LW(menu_toggle_line_commentation1));
345 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINE, NULL,
346 0, 0, "edit_commentline", _("Comment line(s)"), LW(menu_comment_line1));
347 keybindings_set_item(group, GEANY_KEYS_FORMAT_UNCOMMENTLINE, NULL,
348 0, 0, "edit_uncommentline", _("Uncomment line(s)"), LW(menu_uncomment_line1));
349 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENT, NULL,
350 GDK_i, GDK_CONTROL_MASK, "edit_increaseindent", _("Increase indent"),
351 LW(menu_increase_indent1));
352 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENT, NULL,
353 GDK_u, GDK_CONTROL_MASK, "edit_decreaseindent", _("Decrease indent"),
354 LW(menu_decrease_indent1));
355 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE, NULL,
356 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL);
357 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE, NULL,
358 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL);
359 keybindings_set_item(group, GEANY_KEYS_FORMAT_AUTOINDENT, NULL,
360 0, 0, "edit_autoindent", _("_Smart Line Indent"), LW(smart_line_indent1));
361 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD1, NULL,
362 GDK_1, GDK_CONTROL_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL);
363 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD2, NULL,
364 GDK_2, GDK_CONTROL_MASK, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL);
365 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD3, NULL,
366 GDK_3, GDK_CONTROL_MASK, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL);
367 /* may fit better in editor group */
368 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOVTE, NULL,
369 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), LW(send_selection_to_vte1));
370 keybindings_set_item(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
371 GDK_j, GDK_CONTROL_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
372 LW(reflow_lines_block1));
374 group = ADD_KB_GROUP(INSERT, _("Insert"), cb_func_insert_action);
376 keybindings_set_item(group, GEANY_KEYS_INSERT_DATE, NULL,
377 GDK_d, GDK_SHIFT_MASK | GDK_MOD1_MASK, "menu_insert_date", _("Insert date"),
378 LW(insert_date_custom1));
379 keybindings_set_item(group, GEANY_KEYS_INSERT_ALTWHITESPACE, NULL,
380 0, 0, "edit_insertwhitespace", _("_Insert Alternative White Space"),
381 LW(insert_alternative_white_space1));
383 group = ADD_KB_GROUP(SETTINGS, _("Settings"), NULL);
385 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PREFERENCES, cb_func_menu_preferences,
386 GDK_p, GDK_CONTROL_MASK | GDK_MOD1_MASK, "menu_preferences", _("Preferences"),
387 LW(preferences1));
388 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES, cb_func_menu_preferences,
389 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), LW(plugin_preferences1));
391 group = ADD_KB_GROUP(SEARCH, _("Search"), cb_func_search_action);
393 keybindings_set_item(group, GEANY_KEYS_SEARCH_FIND, NULL,
394 GDK_f, GDK_CONTROL_MASK, "menu_find", _("Find"), LW(find1));
395 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXT, NULL,
396 GDK_g, GDK_CONTROL_MASK, "menu_findnext", _("Find Next"), LW(find_next1));
397 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVIOUS, NULL,
398 GDK_g, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findprevious", _("Find Previous"),
399 LW(find_previous1));
400 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXTSEL, NULL,
401 0, 0, "menu_findnextsel", _("Find Next Selection"), LW(find_nextsel1));
402 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVSEL, NULL,
403 0, 0, "menu_findprevsel", _("Find Previous Selection"), LW(find_prevsel1));
404 keybindings_set_item(group, GEANY_KEYS_SEARCH_REPLACE, NULL,
405 GDK_h, GDK_CONTROL_MASK, "menu_replace", _("Replace"), LW(replace1));
406 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDINFILES, NULL, GDK_f,
407 GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findinfiles", _("Find in Files"),
408 LW(find_in_files1));
409 keybindings_set_item(group, GEANY_KEYS_SEARCH_NEXTMESSAGE, NULL,
410 0, 0, "menu_nextmessage", _("Next Message"), LW(next_message1));
411 keybindings_set_item(group, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE, NULL,
412 0, 0, "menu_previousmessage", _("Previous Message"), LW(previous_message1));
413 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDUSAGE, NULL,
414 0, 0, "popup_findusage", _("Find Usage"), NULL);
415 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, NULL,
416 0, 0, "popup_finddocumentusage", _("Find Document Usage"), NULL);
417 keybindings_set_item(group, GEANY_KEYS_SEARCH_MARKALL, NULL,
418 GDK_m, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "find_markall", _("Mark All"), NULL);
420 group = ADD_KB_GROUP(GOTO, _("Go to"), cb_func_goto_action);
422 keybindings_set_item(group, GEANY_KEYS_GOTO_BACK, NULL,
423 0, 0, "nav_back", _("Navigate back a location"), NULL);
424 keybindings_set_item(group, GEANY_KEYS_GOTO_FORWARD, NULL,
425 0, 0, "nav_forward", _("Navigate forward a location"), NULL);
426 keybindings_set_item(group, GEANY_KEYS_GOTO_LINE, NULL,
427 GDK_l, GDK_CONTROL_MASK, "menu_gotoline", _("Go to Line"), LW(go_to_line1));
428 keybindings_set_item(group, GEANY_KEYS_GOTO_MATCHINGBRACE, NULL,
429 GDK_b, GDK_CONTROL_MASK, "edit_gotomatchingbrace",
430 _("Go to matching brace"), NULL);
431 keybindings_set_item(group, GEANY_KEYS_GOTO_TOGGLEMARKER, NULL,
432 GDK_m, GDK_CONTROL_MASK, "edit_togglemarker",
433 _("Toggle marker"), NULL);
434 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTMARKER, NULL,
435 GDK_period, GDK_CONTROL_MASK, "edit_gotonextmarker",
436 _("_Go to Next Marker"), LW(go_to_next_marker1));
437 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVIOUSMARKER, NULL,
438 GDK_comma, GDK_CONTROL_MASK, "edit_gotopreviousmarker",
439 _("_Go to Previous Marker"), LW(go_to_previous_marker1));
440 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDEFINITION, NULL,
441 0, 0, "popup_gototagdefinition", _("Go to Tag Definition"), NULL);
442 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDECLARATION, NULL,
443 0, 0, "popup_gototagdeclaration", _("Go to Tag Declaration"), NULL);
444 keybindings_set_item(group, GEANY_KEYS_GOTO_LINESTART, NULL,
445 GDK_Home, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL);
446 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEEND, NULL,
447 GDK_End, 0, "edit_gotolineend", _("Go to End of Line"), NULL);
448 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEENDVISUAL, NULL,
449 GDK_End, GDK_MOD1_MASK, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL);
450 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVWORDPART, NULL,
451 GDK_slash, GDK_CONTROL_MASK, "edit_prevwordstart", _("Go to Previous Word Part"), NULL);
452 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTWORDPART, NULL,
453 GDK_backslash, GDK_CONTROL_MASK, "edit_nextwordstart", _("Go to Next Word Part"), NULL);
455 group = ADD_KB_GROUP(VIEW, _("View"), cb_func_view_action);
457 keybindings_set_item(group, GEANY_KEYS_VIEW_TOGGLEALL, NULL,
458 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
459 LW(menu_toggle_all_additional_widgets1));
460 keybindings_set_item(group, GEANY_KEYS_VIEW_FULLSCREEN, cb_func_menu_fullscreen,
461 GDK_F11, 0, "menu_fullscreen", _("Fullscreen"), LW(menu_fullscreen1));
462 keybindings_set_item(group, GEANY_KEYS_VIEW_MESSAGEWINDOW, cb_func_menu_messagewindow,
463 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
464 LW(menu_show_messages_window1));
465 keybindings_set_item(group, GEANY_KEYS_VIEW_SIDEBAR, NULL,
466 0, 0, "toggle_sidebar", _("Toggle Sidebar"), LW(menu_show_sidebar1));
467 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMIN, NULL,
468 GDK_plus, GDK_CONTROL_MASK, "menu_zoomin", _("Zoom In"), LW(menu_zoom_in1));
469 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMOUT, NULL,
470 GDK_minus, GDK_CONTROL_MASK, "menu_zoomout", _("Zoom Out"), LW(menu_zoom_out1));
471 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMRESET, NULL,
472 GDK_0, GDK_CONTROL_MASK, "normal_size", _("Zoom Reset"), LW(normal_size1));
474 group = ADD_KB_GROUP(FOCUS, _("Focus"), cb_func_switch_action);
476 keybindings_set_item(group, GEANY_KEYS_FOCUS_EDITOR, NULL,
477 GDK_F2, 0, "switch_editor", _("Switch to Editor"), NULL);
478 keybindings_set_item(group, GEANY_KEYS_FOCUS_SCRIBBLE, NULL,
479 GDK_F6, 0, "switch_scribble", _("Switch to Scribble"), NULL);
480 keybindings_set_item(group, GEANY_KEYS_FOCUS_VTE, NULL,
481 GDK_F4, 0, "switch_vte", _("Switch to VTE"), NULL);
482 keybindings_set_item(group, GEANY_KEYS_FOCUS_SEARCHBAR, NULL,
483 GDK_F7, 0, "switch_search_bar", _("Switch to Search Bar"), NULL);
484 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR, NULL,
485 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL);
486 keybindings_set_item(group, GEANY_KEYS_FOCUS_COMPILER, NULL,
487 0, 0, "switch_compiler", _("Switch to Compiler"), NULL);
488 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGES, NULL,
489 0, 0, "switch_messages", _("Switch to Messages"), NULL);
490 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGE_WINDOW, NULL,
491 0, 0, "switch_message_window", _("Switch to Message Window"), NULL);
492 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST, NULL,
493 0, 0, "switch_sidebar_doc_list", _("Switch to Sidebar Document List"), NULL);
494 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST, NULL,
495 0, 0, "switch_sidebar_symbol_list", _("Switch to Sidebar Symbol List"), NULL);
497 group = ADD_KB_GROUP(NOTEBOOK, _("Notebook tab"), NULL);
499 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT, cb_func_switch_tableft,
500 GDK_Page_Up, GDK_CONTROL_MASK, "switch_tableft", _("Switch to left document"), NULL);
501 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
502 GDK_Page_Down, GDK_CONTROL_MASK, "switch_tabright", _("Switch to right document"), NULL);
503 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
504 GDK_Tab, GDK_CONTROL_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
505 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
506 GDK_Page_Up, GDK_MOD1_MASK, "move_tableft", _("Move document left"), NULL);
507 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
508 GDK_Page_Down, GDK_MOD1_MASK, "move_tabright", _("Move document right"), NULL);
509 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABFIRST, cb_func_move_tab,
510 0, 0, "move_tabfirst", _("Move document first"), NULL);
511 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLAST, cb_func_move_tab,
512 0, 0, "move_tablast", _("Move document last"), NULL);
514 group = ADD_KB_GROUP(DOCUMENT, _("Document"), cb_func_document_action);
516 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEWRAP, NULL,
517 0, 0, "menu_linewrap", _("Toggle Line wrapping"), LW(menu_line_wrapping1));
518 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEBREAK, NULL,
519 0, 0, "menu_linebreak", _("Toggle Line breaking"), LW(line_breaking1));
520 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACETABS, NULL,
521 0, 0, "menu_replacetabs", _("Replace tabs by space"), LW(menu_replace_tabs));
522 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACESPACES, NULL,
523 0, 0, "menu_replacespaces", _("Replace spaces by tabs"), LW(menu_replace_spaces));
524 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_TOGGLEFOLD, NULL,
525 0, 0, "menu_togglefold", _("Toggle current fold"), NULL);
526 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_FOLDALL, NULL,
527 0, 0, "menu_foldall", _("Fold all"), LW(menu_fold_all1));
528 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_UNFOLDALL, NULL,
529 0, 0, "menu_unfoldall", _("Unfold all"), LW(menu_unfold_all1));
530 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_RELOADTAGLIST, NULL,
531 GDK_r, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "reloadtaglist", _("Reload symbol list"), NULL);
533 group = ADD_KB_GROUP(BUILD, _("Build"), cb_func_build_action);
535 keybindings_set_item(group, GEANY_KEYS_BUILD_COMPILE, NULL,
536 GDK_F8, 0, "build_compile", _("Compile"), NULL);
537 keybindings_set_item(group, GEANY_KEYS_BUILD_LINK, NULL,
538 GDK_F9, 0, "build_link", _("Build"), NULL);
539 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKE, NULL,
540 GDK_F9, GDK_SHIFT_MASK, "build_make", _("Make all"), NULL);
541 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOWNTARGET, NULL,
542 GDK_F9, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "build_makeowntarget",
543 _("Make custom target"), NULL);
544 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOBJECT, NULL,
545 0, 0, "build_makeobject", _("Make object"), NULL);
546 keybindings_set_item(group, GEANY_KEYS_BUILD_NEXTERROR, NULL,
547 0, 0, "build_nexterror", _("Next error"), NULL);
548 keybindings_set_item(group, GEANY_KEYS_BUILD_PREVIOUSERROR, NULL,
549 0, 0, "build_previouserror", _("Previous error"), NULL);
550 keybindings_set_item(group, GEANY_KEYS_BUILD_RUN, NULL,
551 GDK_F5, 0, "build_run", _("Run"), NULL);
552 keybindings_set_item(group, GEANY_KEYS_BUILD_OPTIONS, NULL,
553 0, 0, "build_options", _("Build options"), NULL);
555 group = ADD_KB_GROUP(TOOLS, _("Tools"), NULL);
557 keybindings_set_item(group, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER, cb_func_menu_opencolorchooser,
558 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), LW(menu_choose_color1));
560 group = ADD_KB_GROUP(HELP, _("Help"), NULL);
562 keybindings_set_item(group, GEANY_KEYS_HELP_HELP, cb_func_menu_help,
563 GDK_F1, 0, "menu_help", _("Help"), LW(help1));
567 /* before the tab changes, add the current document to the MRU list */
568 static void on_notebook_switch_page(void)
570 GeanyDocument *old = document_get_current();
572 /* when closing current doc, old is NULL.
573 * Don't add to the mru list when switch dialog is visible. */
574 if (old && switch_dialog_cancelled)
576 g_queue_remove(mru_docs, old);
577 g_queue_push_head(mru_docs, old);
579 if (g_queue_get_length(mru_docs) > MAX_MRU_DOCS)
580 g_queue_pop_tail(mru_docs);
585 /* really this should be just after a document was closed, not idle */
586 static gboolean on_idle_close(gpointer data)
588 GeanyDocument *current;
590 current = document_get_current();
591 if (current && g_queue_peek_head(mru_docs) == current)
592 g_queue_pop_head(mru_docs);
594 return FALSE;
598 static void on_document_close(GObject *obj, GeanyDocument *doc)
600 if (! main_status.quitting)
602 g_queue_remove(mru_docs, doc);
603 g_idle_add(on_idle_close, NULL);
608 void keybindings_init(void)
610 mru_docs = g_queue_new();
611 g_signal_connect(main_widgets.notebook, "switch-page",
612 G_CALLBACK(on_notebook_switch_page), NULL);
613 g_signal_connect(geany_object, "document-close",
614 G_CALLBACK(on_document_close), NULL);
616 keybinding_groups = g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT);
618 kb_accel_group = gtk_accel_group_new();
620 init_default_kb();
622 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), kb_accel_group);
624 g_signal_connect(main_widgets.window, "key-press-event", G_CALLBACK(on_key_press_event), NULL);
625 /* in case the switch dialog misses an event while drawing the dialog */
626 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
630 typedef void (*KBItemCallback) (GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data);
632 static void keybindings_foreach(KBItemCallback cb, gpointer user_data)
634 gsize g, i;
635 GeanyKeyGroup *group;
636 GeanyKeyBinding *kb;
638 for (g = 0; g < keybinding_groups->len; g++)
640 group = g_ptr_array_index(keybinding_groups, g);
641 for (i = 0; i < group->count; i++)
643 kb = &group->keys[i];
645 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_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
672 GKeyFile *config = g_key_file_new();
674 /* now load user defined keys */
675 if (g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
677 keybindings_foreach(load_kb, config);
679 g_free(configfile);
680 g_key_file_free(config);
684 static void apply_kb_accel(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
686 if (kb->key != 0 && kb->menu_item)
688 gtk_widget_add_accelerator(kb->menu_item, "activate", kb_accel_group,
689 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
694 void keybindings_load_keyfile(void)
696 load_user_kb();
697 add_popup_menu_accels();
699 /* set menu accels now, after user keybindings have been read */
700 keybindings_foreach(apply_kb_accel, NULL);
704 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id, GtkWidget *menuitem)
706 GeanyKeyBinding *kb = &group->keys[kb_id];
708 if (kb->key != 0)
709 gtk_widget_add_accelerator(menuitem, "activate", kb_accel_group,
710 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
714 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
715 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
717 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
718 static void add_popup_menu_accels(void)
720 GeanyKeyGroup *group;
722 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_EDITOR);
723 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO, undo1);
724 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO, redo1);
725 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION, context_action1);
727 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_SELECT);
728 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL, menu_select_all2);
730 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_INSERT);
731 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE, insert_date_custom2);
733 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_FILE);
734 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED, menu_open_selected_file2);
736 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_SEARCH);
737 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE, find_usage1);
738 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, find_document_usage1);
740 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_GOTO);
741 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_LINE, go_to_line);
742 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION, goto_tag_definition1);
743 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDECLARATION, goto_tag_declaration1);
745 /* Format and Commands share the menu bar submenus */
746 /* Build menu items are set if the build menus are created */
750 static void set_keyfile_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
752 GKeyFile *config = user_data;
753 gchar *val;
755 val = gtk_accelerator_name(kb->key, kb->mods);
756 g_key_file_set_string(config, group->name, kb->name, val);
757 g_free(val);
761 /* just write the content of the keys array to the config file */
762 void keybindings_write_to_file(void)
764 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
765 gchar *data;
766 GKeyFile *config = g_key_file_new();
768 /* add comment if the file is newly created */
769 if (! g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
771 g_key_file_set_comment(config, NULL, NULL,
772 "Keybindings for Geany\nThe format looks like \"<Control>a\" or \"<Shift><Alt>F1\".\n"
773 "But you can also change the keys in Geany's preferences dialog.", NULL);
776 keybindings_foreach(set_keyfile_kb, config);
778 /* write the file */
779 data = g_key_file_to_data(config, NULL, NULL);
780 utils_write_file(configfile, data);
782 g_free(data);
783 g_free(configfile);
784 g_key_file_free(config);
788 void keybindings_free(void)
790 g_ptr_array_free(keybinding_groups, TRUE);
791 g_queue_free(mru_docs);
795 gchar *keybindings_get_label(GeanyKeyBinding *kb)
797 return utils_str_remove_chars(g_strdup(kb->label), "_");
801 static void fill_shortcut_labels_treeview(GtkWidget *tree)
803 gsize g, i;
804 GeanyKeyBinding *kb;
805 GeanyKeyGroup *group;
806 GtkListStore *store;
807 GtkTreeIter iter;
809 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, PANGO_TYPE_WEIGHT);
811 for (g = 0; g < keybinding_groups->len; g++)
813 group = g_ptr_array_index(keybinding_groups, g);
815 if (g > 0)
817 gtk_list_store_append(store, &iter);
818 gtk_list_store_set(store, &iter, -1);
821 gtk_list_store_append(store, &iter);
822 gtk_list_store_set(store, &iter, 0, group->label, 2, PANGO_WEIGHT_BOLD, -1);
824 for (i = 0; i < group->count; i++)
826 gchar *shortcut, *label;
828 kb = &group->keys[i];
829 label = keybindings_get_label(kb);
830 shortcut = gtk_accelerator_get_label(kb->key, kb->mods);
832 gtk_list_store_append(store, &iter);
833 gtk_list_store_set(store, &iter, 0, label, 1, shortcut, 2, PANGO_WEIGHT_NORMAL, -1);
835 g_free(shortcut);
836 g_free(label);
840 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
841 g_object_unref(store);
845 static GtkWidget *create_dialog(void)
847 GtkWidget *dialog, *tree, *label, *swin, *vbox;
848 GtkCellRenderer *text_renderer;
849 GtkTreeViewColumn *column;
851 dialog = gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets.window),
852 GTK_DIALOG_DESTROY_WITH_PARENT,
853 GTK_STOCK_EDIT, GTK_RESPONSE_APPLY,
854 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
855 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
856 gtk_box_set_spacing(GTK_BOX(vbox), 6);
857 gtk_widget_set_name(dialog, "GeanyDialog");
859 gtk_window_set_default_size(GTK_WINDOW(dialog), -1, GEANY_DEFAULT_DIALOG_HEIGHT);
861 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
863 label = gtk_label_new(_("The following keyboard shortcuts are configurable:"));
864 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
866 tree = gtk_tree_view_new();
867 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
868 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
870 text_renderer = gtk_cell_renderer_text_new();
871 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
872 column = gtk_tree_view_column_new_with_attributes(
873 NULL, text_renderer, "text", 0, "weight", 2, NULL);
874 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
876 text_renderer = gtk_cell_renderer_text_new();
877 column = gtk_tree_view_column_new_with_attributes(NULL, text_renderer, "text", 1, NULL);
878 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
880 fill_shortcut_labels_treeview(tree);
882 swin = gtk_scrolled_window_new(NULL, NULL);
883 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_NEVER,
884 GTK_POLICY_AUTOMATIC);
885 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_ETCHED_IN);
886 gtk_container_add(GTK_CONTAINER(swin), tree);
888 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
889 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
891 return dialog;
895 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
896 static GtkWidget *key_dialog = NULL;
898 static void on_dialog_response(GtkWidget *dialog, gint response, gpointer user_data)
900 if (response == GTK_RESPONSE_APPLY)
902 GtkWidget *wid;
904 prefs_show_dialog();
905 /* select the KB page */
906 wid = ui_lookup_widget(ui_widgets.prefs_dialog, "frame22");
907 if (wid != NULL)
909 GtkNotebook *nb = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
911 if (nb != NULL)
912 gtk_notebook_set_current_page(nb, gtk_notebook_page_num(nb, wid));
915 gtk_widget_destroy(dialog);
916 key_dialog = NULL;
920 void keybindings_show_shortcuts(void)
922 if (key_dialog)
923 gtk_widget_destroy(key_dialog); /* in case the key_dialog is still visible */
925 key_dialog = create_dialog();
926 g_signal_connect(key_dialog, "response", G_CALLBACK(on_dialog_response), NULL);
927 gtk_widget_show_all(key_dialog);
931 static gboolean check_fixed_kb(guint keyval, guint state)
933 /* check alt-0 to alt-9 for setting current notebook page */
934 if (state & GDK_MOD1_MASK && keyval >= GDK_0 && keyval <= GDK_9)
936 gint page = keyval - GDK_0 - 1;
937 gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
939 /* alt-0 is for the rightmost tab */
940 if (keyval == GDK_0)
941 page = npages - 1;
942 /* invert the order if tabs are added on the other side */
943 if (swap_alt_tab_order && ! file_prefs.tab_order_ltr)
944 page = (npages - 1) - page;
946 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
947 return TRUE;
949 if (keyval == GDK_Page_Up || keyval == GDK_Page_Down)
951 /* switch to first or last document */
952 if (state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
954 if (keyval == GDK_Page_Up)
955 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), 0);
956 if (keyval == GDK_Page_Down)
957 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
958 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) - 1);
959 return TRUE;
962 return FALSE;
966 static gboolean check_snippet_completion(GeanyDocument *doc)
968 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
970 g_return_val_if_fail(doc, FALSE);
972 /* keybinding only valid when scintilla widget has focus */
973 if (focusw == GTK_WIDGET(doc->editor->sci))
975 ScintillaObject *sci = doc->editor->sci;
976 gint pos = sci_get_current_position(sci);
978 if (editor_prefs.complete_snippets)
979 return editor_complete_snippet(doc->editor, pos);
981 return FALSE;
985 /* Transforms a GdkEventKey event into a GdkEventButton event */
986 static void trigger_button_event(GtkWidget *widget, guint32 event_time)
988 GdkEventButton *event;
989 gboolean ret;
991 event = g_new0(GdkEventButton, 1);
993 if (GTK_IS_TEXT_VIEW(widget))
994 event->window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT);
995 else
996 event->window = widget->window;
997 event->time = event_time;
998 event->type = GDK_BUTTON_PRESS;
999 event->button = 3;
1001 g_signal_emit_by_name(widget, "button-press-event", event, &ret);
1002 g_signal_emit_by_name(widget, "button-release-event", event, &ret);
1004 g_free(event);
1008 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
1009 * widgets. Without this special handling, the notebook tab list of the documents' notebook
1010 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
1011 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1012 * notebook tab list. */
1013 static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
1015 if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
1017 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1018 if (doc != NULL)
1020 if (focusw == doc->priv->tag_tree)
1022 trigger_button_event(focusw, event_time);
1023 return TRUE;
1025 if (focusw == GTK_WIDGET(doc->editor->sci))
1027 if (keyval == GDK_Menu)
1028 { /* show editor popup menu */
1029 trigger_button_event(focusw, event_time);
1030 return TRUE;
1032 else
1033 { /* show tab bar menu */
1034 trigger_button_event(main_widgets.notebook, event_time);
1035 return TRUE;
1039 if (focusw == tv.tree_openfiles
1040 || focusw == msgwindow.tree_status
1041 || focusw == msgwindow.tree_compiler
1042 || focusw == msgwindow.tree_msg
1043 || focusw == msgwindow.scribble
1044 #ifdef HAVE_VTE
1045 || (vte_info.have_vte && focusw == vc->vte)
1046 #endif
1049 trigger_button_event(focusw, event_time);
1050 return TRUE;
1053 return FALSE;
1057 #ifdef HAVE_VTE
1058 static gboolean on_menu_expose_event(GtkWidget *widget, GdkEventExpose *event,
1059 gpointer user_data)
1061 if (!GTK_WIDGET_SENSITIVE(widget))
1062 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1063 return FALSE;
1067 static gboolean set_sensitive(gpointer widget)
1069 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1070 return FALSE;
1074 static gboolean check_vte(GdkModifierType state, guint keyval)
1076 guint i;
1077 GtkWidget *widget;
1079 if (! vc->enable_bash_keys)
1080 return FALSE;
1081 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != vc->vte)
1082 return FALSE;
1083 /* prevent menubar flickering: */
1084 if (state == GDK_SHIFT_MASK && (keyval >= GDK_a && keyval <= GDK_z))
1085 return FALSE;
1086 if (state == 0 && (keyval < GDK_F1 || keyval > GDK_F35)) /* e.g. backspace */
1087 return FALSE;
1089 /* make focus commands override any bash commands */
1090 for (i = 0; i < GEANY_KEYS_FOCUS_COUNT; i++)
1092 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FOCUS, i);
1094 if (state == kb->mods && keyval == kb->key)
1095 return FALSE;
1098 /* Temporarily disable the menus to prevent conflicting menu accelerators
1099 * from overriding the VTE bash shortcuts.
1100 * Note: maybe there's a better way of doing this ;-) */
1101 widget = ui_lookup_widget(main_widgets.window, "menubar1");
1102 gtk_widget_set_sensitive(widget, FALSE);
1104 /* make the menubar sensitive before it is redrawn */
1105 static gboolean connected = FALSE;
1106 if (!connected)
1107 g_signal_connect(widget, "expose-event", G_CALLBACK(on_menu_expose_event), NULL);
1110 widget = main_widgets.editor_menu;
1111 gtk_widget_set_sensitive(widget, FALSE);
1112 g_idle_add(set_sensitive, widget);
1113 return TRUE;
1115 #endif
1118 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1119 static guint key_kp_translate(guint key_in)
1121 switch (key_in)
1123 case GDK_KP_Down:
1124 return GDK_Down;
1125 case GDK_KP_Up:
1126 return GDK_Up;
1127 case GDK_KP_Left:
1128 return GDK_Left;
1129 case GDK_KP_Right:
1130 return GDK_Right;
1131 case GDK_KP_Home:
1132 return GDK_Home;
1133 case GDK_KP_End:
1134 return GDK_End;
1135 case GDK_KP_Page_Up:
1136 return GDK_Page_Up;
1137 case GDK_KP_Page_Down:
1138 return GDK_Page_Down;
1139 case GDK_KP_Delete:
1140 return GDK_Delete;
1141 case GDK_KP_Insert:
1142 return GDK_Insert;
1143 default:
1144 return key_in;
1149 /* Stripped down version of the main keypress event handler which can be used
1150 * to process foreign events. Instead of executing the keybinding, a pointer to the
1151 * keybinding structure is returned.
1152 * Additionally, the group_id and binding_id are filled with the appropriate indexes
1153 * if non-NULL. */
1154 const GeanyKeyBinding *keybindings_check_event(GdkEventKey *ev, gint *group_id, gint *binding_id)
1156 guint state, keyval;
1157 gsize g, i;
1158 GeanyKeyGroup *group;
1159 GeanyKeyBinding *kb;
1161 if (ev->keyval == 0)
1162 return FALSE;
1164 keyval = ev->keyval;
1165 state = ev->state & gtk_accelerator_get_default_mod_mask();
1166 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1167 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1168 if (keyval >= GDK_A && keyval <= GDK_Z)
1169 keyval += GDK_a - GDK_A;
1171 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1172 keyval = key_kp_translate(keyval);
1174 for (g = 0; g < keybinding_groups->len; g++)
1176 group = g_ptr_array_index(keybinding_groups, g);
1178 for (i = 0; i < group->count; i++)
1180 kb = &group->keys[i];
1181 if (keyval == kb->key && state == kb->mods)
1183 if (group_id != NULL)
1184 *group_id = g;
1185 if (binding_id != NULL)
1186 *binding_id = i;
1187 return kb;
1191 return NULL;
1195 /* central keypress event handler, almost all keypress events go to this function */
1196 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1198 guint state, keyval;
1199 gsize g, i;
1200 GeanyDocument *doc;
1201 GeanyKeyGroup *group;
1202 GeanyKeyBinding *kb;
1204 if (ev->keyval == 0)
1205 return FALSE;
1207 doc = document_get_current();
1208 if (doc)
1209 document_check_disk_status(doc, FALSE);
1211 keyval = ev->keyval;
1212 state = ev->state & gtk_accelerator_get_default_mod_mask();
1213 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1214 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1215 if (keyval >= GDK_A && keyval <= GDK_Z)
1216 keyval += GDK_a - GDK_A;
1218 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1219 keyval = key_kp_translate(keyval);
1221 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1223 /* special cases */
1224 #ifdef HAVE_VTE
1225 if (vte_info.have_vte && check_vte(state, keyval))
1226 return FALSE;
1227 #endif
1228 if (check_menu_key(doc, keyval, state, ev->time))
1229 return TRUE;
1231 for (g = 0; g < keybinding_groups->len; g++)
1233 group = g_ptr_array_index(keybinding_groups, g);
1235 for (i = 0; i < group->count; i++)
1237 kb = &group->keys[i];
1238 if (keyval == kb->key && state == kb->mods)
1240 /* call the corresponding callback function for this shortcut */
1241 if (kb->callback)
1243 kb->callback(i);
1244 return TRUE;
1246 else if (group->callback)
1248 if (group->callback(i))
1249 return TRUE;
1250 else
1251 continue; /* not handled */
1253 g_warning("No callback for keybinding %s: %s!", group->name, kb->name);
1257 /* fixed keybindings can be overridden by user bindings, so check them last */
1258 if (check_fixed_kb(keyval, state))
1259 return TRUE;
1260 return FALSE;
1264 static gboolean is_modifier_key(guint keyval)
1266 switch (keyval)
1268 case GDK_Shift_L:
1269 case GDK_Shift_R:
1270 case GDK_Control_L:
1271 case GDK_Control_R:
1272 case GDK_Meta_L:
1273 case GDK_Meta_R:
1274 case GDK_Alt_L:
1275 case GDK_Alt_R:
1276 case GDK_Super_L:
1277 case GDK_Super_R:
1278 case GDK_Hyper_L:
1279 case GDK_Hyper_R:
1280 return TRUE;
1281 default:
1282 return FALSE;
1287 GeanyKeyBinding *keybindings_lookup_item(guint group_id, guint key_id)
1289 GeanyKeyGroup *group;
1291 g_return_val_if_fail(group_id < keybinding_groups->len, NULL);
1293 group = g_ptr_array_index(keybinding_groups, group_id);
1295 g_return_val_if_fail(group, NULL);
1296 g_return_val_if_fail(key_id < group->count, NULL);
1298 return &group->keys[key_id];
1302 /** Mimics a (built-in only) keybinding action.
1303 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1304 * @param group_id The index for the key group that contains the @a key_id keybinding.
1305 * @param key_id The keybinding command index. */
1306 void keybindings_send_command(guint group_id, guint key_id)
1308 GeanyKeyBinding *kb;
1310 g_return_if_fail(group_id < GEANY_KEY_GROUP_COUNT); /* can't use this for plugin groups */
1312 kb = keybindings_lookup_item(group_id, key_id);
1313 if (kb)
1315 if (kb->callback)
1316 kb->callback(key_id);
1317 else
1319 GeanyKeyGroup *group = g_ptr_array_index(keybinding_groups, group_id);
1321 if (group->callback)
1322 group->callback(key_id);
1328 /* These are the callback functions, either each group or each shortcut has it's
1329 * own function. */
1332 static gboolean cb_func_file_action(guint key_id)
1334 switch (key_id)
1336 case GEANY_KEYS_FILE_NEW:
1337 document_new_file(NULL, NULL, NULL);
1338 break;
1339 case GEANY_KEYS_FILE_OPEN:
1340 on_open1_activate(NULL, NULL);
1341 break;
1342 case GEANY_KEYS_FILE_OPENSELECTED:
1343 on_menu_open_selected_file1_activate(NULL, NULL);
1344 break;
1345 case GEANY_KEYS_FILE_OPENLASTTAB:
1347 gchar *utf8_filename = g_queue_peek_head(ui_prefs.recent_queue);
1348 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1349 document_open_file(locale_filename, FALSE, NULL, NULL);
1350 g_free(locale_filename);
1351 break;
1353 case GEANY_KEYS_FILE_SAVE:
1354 on_save1_activate(NULL, NULL);
1355 break;
1356 case GEANY_KEYS_FILE_SAVEAS:
1357 on_save_as1_activate(NULL, NULL);
1358 break;
1359 case GEANY_KEYS_FILE_SAVEALL:
1360 on_save_all1_activate(NULL, NULL);
1361 break;
1362 case GEANY_KEYS_FILE_CLOSE:
1363 on_close1_activate(NULL, NULL);
1364 break;
1365 case GEANY_KEYS_FILE_CLOSEALL:
1366 on_close_all1_activate(NULL, NULL);
1367 break;
1368 case GEANY_KEYS_FILE_RELOAD:
1369 on_toolbutton_reload_clicked(NULL, NULL);
1370 break;
1371 case GEANY_KEYS_FILE_PRINT:
1372 on_print1_activate(NULL, NULL);
1373 break;
1375 return TRUE;
1379 static gboolean cb_func_project_action(guint key_id)
1381 switch (key_id)
1383 case GEANY_KEYS_PROJECT_PROPERTIES:
1384 if (app->project)
1385 on_project_properties1_activate(NULL, NULL);
1386 break;
1388 return TRUE;
1392 static void cb_func_menu_preferences(guint key_id)
1394 switch (key_id)
1396 case GEANY_KEYS_SETTINGS_PREFERENCES:
1397 on_preferences1_activate(NULL, NULL);
1398 break;
1399 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES:
1400 on_plugin_preferences1_activate(NULL, NULL);
1401 break;
1406 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id)
1408 on_help1_activate(NULL, NULL);
1412 static gboolean cb_func_search_action(guint key_id)
1414 GeanyDocument *doc = document_get_current();
1415 ScintillaObject *sci;
1417 if (key_id == GEANY_KEYS_SEARCH_FINDINFILES)
1419 on_find_in_files1_activate(NULL, NULL); /* works without docs too */
1420 return TRUE;
1422 if (!doc)
1423 return TRUE;
1424 sci = doc->editor->sci;
1426 switch (key_id)
1428 case GEANY_KEYS_SEARCH_FIND:
1429 on_find1_activate(NULL, NULL); break;
1430 case GEANY_KEYS_SEARCH_FINDNEXT:
1431 on_find_next1_activate(NULL, NULL); break;
1432 case GEANY_KEYS_SEARCH_FINDPREVIOUS:
1433 on_find_previous1_activate(NULL, NULL); break;
1434 case GEANY_KEYS_SEARCH_FINDPREVSEL:
1435 on_find_prevsel1_activate(NULL, NULL); break;
1436 case GEANY_KEYS_SEARCH_FINDNEXTSEL:
1437 on_find_nextsel1_activate(NULL, NULL); break;
1438 case GEANY_KEYS_SEARCH_REPLACE:
1439 on_replace1_activate(NULL, NULL); break;
1440 case GEANY_KEYS_SEARCH_NEXTMESSAGE:
1441 on_next_message1_activate(NULL, NULL); break;
1442 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE:
1443 on_previous_message1_activate(NULL, NULL); break;
1444 case GEANY_KEYS_SEARCH_FINDUSAGE:
1445 read_current_word(doc);
1446 on_find_usage1_activate(NULL, NULL);
1447 break;
1448 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE:
1449 read_current_word(doc);
1450 on_find_document_usage1_activate(NULL, NULL);
1451 break;
1452 case GEANY_KEYS_SEARCH_MARKALL:
1454 gchar *text = get_current_word_or_sel(doc);
1456 if (sci_has_selection(sci))
1457 search_mark_all(doc, text, SCFIND_MATCHCASE);
1458 else
1460 /* clears markers if text is null */
1461 search_mark_all(doc, text, SCFIND_MATCHCASE | SCFIND_WHOLEWORD);
1463 g_free(text);
1464 break;
1467 return TRUE;
1471 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id)
1473 on_show_color_chooser1_activate(NULL, NULL);
1477 static gboolean cb_func_view_action(guint key_id)
1479 switch (key_id)
1481 case GEANY_KEYS_VIEW_TOGGLEALL:
1482 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
1483 break;
1484 case GEANY_KEYS_VIEW_SIDEBAR:
1485 on_menu_show_sidebar1_toggled(NULL, NULL);
1486 break;
1487 case GEANY_KEYS_VIEW_ZOOMIN:
1488 on_zoom_in1_activate(NULL, NULL);
1489 break;
1490 case GEANY_KEYS_VIEW_ZOOMOUT:
1491 on_zoom_out1_activate(NULL, NULL);
1492 break;
1493 case GEANY_KEYS_VIEW_ZOOMRESET:
1494 on_normal_size1_activate(NULL, NULL);
1495 break;
1496 default:
1497 break;
1499 return TRUE;
1503 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id)
1505 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1506 ui_lookup_widget(main_widgets.window, "menu_fullscreen1"));
1508 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1512 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id)
1514 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1515 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1517 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1521 static gboolean cb_func_build_action(guint key_id)
1523 GtkWidget *item;
1524 BuildMenuItems *menu_items;
1525 GeanyDocument *doc = document_get_current();
1527 if (doc == NULL)
1528 return TRUE;
1530 if (!GTK_WIDGET_IS_SENSITIVE(ui_lookup_widget(main_widgets.window, "menu_build1")))
1531 return TRUE;
1533 menu_items = build_get_menu_items(doc->file_type->id);
1534 /* TODO make it a table??*/
1535 switch (key_id)
1537 case GEANY_KEYS_BUILD_COMPILE:
1538 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_COMPILE)];
1539 break;
1540 case GEANY_KEYS_BUILD_LINK:
1541 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_BUILD)];
1542 break;
1543 case GEANY_KEYS_BUILD_MAKE:
1544 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_ALL)];
1545 break;
1546 case GEANY_KEYS_BUILD_MAKEOWNTARGET:
1547 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_CUSTOM)];
1548 break;
1549 case GEANY_KEYS_BUILD_MAKEOBJECT:
1550 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)];
1551 break;
1552 case GEANY_KEYS_BUILD_NEXTERROR:
1553 item = menu_items->menu_item[GBG_FIXED][GBF_NEXT_ERROR];
1554 break;
1555 case GEANY_KEYS_BUILD_PREVIOUSERROR:
1556 item = menu_items->menu_item[GBG_FIXED][GBF_PREV_ERROR];
1557 break;
1558 case GEANY_KEYS_BUILD_RUN:
1559 item = menu_items->menu_item[GEANY_GBG_EXEC][GBO_TO_CMD(GEANY_GBO_EXEC)];
1560 break;
1561 case GEANY_KEYS_BUILD_OPTIONS:
1562 item = menu_items->menu_item[GBG_FIXED][GBF_COMMANDS];
1563 break;
1564 default:
1565 item = NULL;
1567 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1568 * sensitive state, but some other menus don't update the sensitive status until
1569 * they are redrawn. */
1570 if (item && GTK_WIDGET_IS_SENSITIVE(item))
1571 gtk_menu_item_activate(GTK_MENU_ITEM(item));
1572 return TRUE;
1576 static gboolean read_current_word(GeanyDocument *doc)
1578 gint pos;
1580 if (doc == NULL)
1581 return FALSE;
1583 pos = sci_get_current_position(doc->editor->sci);
1585 editor_find_current_word(doc->editor, pos,
1586 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1588 return (*editor_info.current_word != 0);
1592 static gboolean check_current_word(GeanyDocument *doc)
1594 if (!read_current_word(doc))
1596 utils_beep();
1597 return FALSE;
1599 return TRUE;
1603 static gchar *get_current_word_or_sel(GeanyDocument *doc)
1605 ScintillaObject *sci = doc->editor->sci;
1607 if (sci_has_selection(sci))
1608 return sci_get_selection_contents(sci);
1610 return read_current_word(doc) ? g_strdup(editor_info.current_word) : NULL;
1614 static void focus_sidebar(void)
1616 if (ui_prefs.sidebar_visible)
1618 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook));
1619 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), page_num);
1621 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1622 gtk_widget_child_focus(page, GTK_DIR_TAB_FORWARD);
1627 static void focus_msgwindow(void)
1629 if (ui_prefs.msgwindow_visible)
1631 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook));
1632 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(msgwindow.notebook), page_num);
1634 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(page)));
1639 static gboolean cb_func_switch_action(guint key_id)
1641 switch (key_id)
1643 case GEANY_KEYS_FOCUS_EDITOR:
1645 GeanyDocument *doc = document_get_current();
1646 if (doc != NULL)
1648 GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
1649 if (GTK_WIDGET_HAS_FOCUS(sci))
1650 ui_update_statusbar(doc, -1);
1651 else
1652 gtk_widget_grab_focus(sci);
1654 break;
1656 case GEANY_KEYS_FOCUS_SCRIBBLE:
1657 msgwin_switch_tab(MSG_SCRATCH, TRUE);
1658 break;
1659 case GEANY_KEYS_FOCUS_SEARCHBAR:
1660 if (toolbar_prefs.visible)
1662 GtkWidget *search_entry = toolbar_get_widget_child_by_name("SearchEntry");
1663 if (search_entry != NULL)
1664 gtk_widget_grab_focus(search_entry);
1666 break;
1667 case GEANY_KEYS_FOCUS_SIDEBAR:
1668 focus_sidebar();
1669 break;
1670 case GEANY_KEYS_FOCUS_VTE:
1671 msgwin_switch_tab(MSG_VTE, TRUE);
1672 break;
1673 case GEANY_KEYS_FOCUS_COMPILER:
1674 msgwin_switch_tab(MSG_COMPILER, TRUE);
1675 break;
1676 case GEANY_KEYS_FOCUS_MESSAGES:
1677 msgwin_switch_tab(MSG_MESSAGE, TRUE);
1678 break;
1679 case GEANY_KEYS_FOCUS_MESSAGE_WINDOW:
1680 focus_msgwindow();
1681 break;
1682 case GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST:
1683 sidebar_focus_openfiles_tab();
1684 break;
1685 case GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST:
1686 sidebar_focus_symbols_tab();
1687 break;
1689 return TRUE;
1693 static void switch_notebook_page(gint direction)
1695 gint page_count, cur_page;
1696 gboolean parent_is_notebook = FALSE;
1697 GtkNotebook *notebook;
1698 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1700 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1703 parent_is_notebook = GTK_IS_NOTEBOOK(focusw);
1705 while (! parent_is_notebook && (focusw = gtk_widget_get_parent(focusw)) != NULL);
1707 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1708 if (parent_is_notebook)
1709 notebook = GTK_NOTEBOOK(focusw);
1710 else
1711 notebook = GTK_NOTEBOOK(main_widgets.notebook);
1713 /* now switch pages */
1714 page_count = gtk_notebook_get_n_pages(notebook);
1715 cur_page = gtk_notebook_get_current_page(notebook);
1717 if (direction == GTK_DIR_LEFT)
1719 if (cur_page > 0)
1720 gtk_notebook_set_current_page(notebook, cur_page - 1);
1721 else
1722 gtk_notebook_set_current_page(notebook, page_count - 1);
1724 else if (direction == GTK_DIR_RIGHT)
1726 if (cur_page < page_count - 1)
1727 gtk_notebook_set_current_page(notebook, cur_page + 1);
1728 else
1729 gtk_notebook_set_current_page(notebook, 0);
1734 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id)
1736 switch_notebook_page(GTK_DIR_LEFT);
1740 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id)
1742 switch_notebook_page(GTK_DIR_RIGHT);
1746 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1748 /* user may have rebound keybinding to a different modifier than Ctrl, so check all */
1749 if (!switch_dialog_cancelled && is_modifier_key(ev->keyval))
1751 switch_dialog_cancelled = TRUE;
1753 if (switch_dialog && GTK_WIDGET_VISIBLE(switch_dialog))
1754 gtk_widget_hide(switch_dialog);
1756 mru_pos = 0;
1758 return FALSE;
1762 static GtkWidget *ui_minimal_dialog_new(GtkWindow *parent, const gchar *title)
1764 GtkWidget *dialog;
1766 dialog = gtk_window_new(GTK_WINDOW_POPUP);
1768 if (parent)
1770 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
1771 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
1773 gtk_window_set_title(GTK_WINDOW(dialog), title);
1774 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
1775 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
1777 gtk_widget_set_name(dialog, "GeanyDialog");
1778 return dialog;
1782 static GtkWidget *create_switch_dialog(void)
1784 GtkWidget *dialog, *widget, *vbox;
1786 dialog = ui_minimal_dialog_new(GTK_WINDOW(main_widgets.window), _("Switch to Document"));
1787 gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE);
1788 gtk_window_set_default_size(GTK_WINDOW(dialog), 150, -1);
1790 vbox = gtk_vbox_new(FALSE, 6);
1791 gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
1792 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1794 widget = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
1795 gtk_container_add(GTK_CONTAINER(vbox), widget);
1797 widget = geany_wrap_label_new(NULL);
1798 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
1799 gtk_container_add(GTK_CONTAINER(vbox), widget);
1800 switch_dialog_label = widget;
1802 g_signal_connect(dialog, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
1803 return dialog;
1807 static gboolean on_switch_timeout(G_GNUC_UNUSED gpointer data)
1809 if (switch_dialog_cancelled)
1811 return FALSE;
1813 if (! switch_dialog || !GTK_WIDGET_VISIBLE(switch_dialog))
1814 mru_pos = 2; /* skip past the previous document */
1815 else
1816 mru_pos += 1;
1818 if (! switch_dialog)
1819 switch_dialog = create_switch_dialog();
1821 geany_wrap_label_set_text(GTK_LABEL(switch_dialog_label),
1822 DOC_FILENAME(document_get_current()));
1823 gtk_widget_show_all(switch_dialog);
1824 return FALSE;
1828 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id)
1830 GeanyDocument *last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1832 if (! DOC_VALID(last_doc))
1834 utils_beep();
1835 mru_pos = 0;
1836 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1838 if (! DOC_VALID(last_doc))
1839 return;
1841 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
1842 document_get_notebook_page(last_doc));
1844 /* if there's a modifier key, we can switch back in MRU order each time unless
1845 * the key is released */
1846 if (! switch_dialog_cancelled)
1848 on_switch_timeout(NULL); /* update filename label */
1850 else
1851 if (keybindings_lookup_item(GEANY_KEY_GROUP_NOTEBOOK,
1852 GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED)->mods)
1854 switch_dialog_cancelled = FALSE;
1856 /* delay showing dialog to give user time to let go of any modifier keys */
1857 g_timeout_add(600, on_switch_timeout, NULL);
1862 /* move document left/right/first/last */
1863 static void cb_func_move_tab(guint key_id)
1865 GtkWidget *sci;
1866 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
1867 gint cur_page = gtk_notebook_get_current_page(nb);
1868 GeanyDocument *doc = document_get_current();
1870 if (doc == NULL)
1871 return;
1873 sci = GTK_WIDGET(doc->editor->sci);
1875 switch (key_id)
1877 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT:
1878 gtk_notebook_reorder_child(nb, sci, cur_page - 1); /* notebook wraps around by default */
1879 break;
1880 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT:
1882 gint npage = cur_page + 1;
1884 if (npage == gtk_notebook_get_n_pages(nb))
1885 npage = 0; /* wraparound */
1886 gtk_notebook_reorder_child(nb, sci, npage);
1887 break;
1889 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST:
1890 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? 0 : -1);
1891 break;
1892 case GEANY_KEYS_NOTEBOOK_MOVETABLAST:
1893 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? -1 : 0);
1894 break;
1896 return;
1900 static void goto_matching_brace(GeanyDocument *doc)
1902 gint pos, new_pos;
1904 if (doc == NULL)
1905 return;
1907 pos = sci_get_current_position(doc->editor->sci);
1908 if (! utils_isbrace(sci_get_char_at(doc->editor->sci, pos), TRUE))
1909 pos--; /* set pos to the brace */
1911 new_pos = sci_find_matching_brace(doc->editor->sci, pos);
1912 if (new_pos != -1)
1913 { /* set the cursor at the brace */
1914 sci_set_current_position(doc->editor->sci, new_pos, FALSE);
1915 editor_display_current_line(doc->editor, 0.5F);
1920 static gboolean cb_func_clipboard_action(guint key_id)
1922 GeanyDocument *doc = document_get_current();
1924 if (doc == NULL)
1925 return TRUE;
1927 switch (key_id)
1929 case GEANY_KEYS_CLIPBOARD_CUT:
1930 on_cut1_activate(NULL, NULL);
1931 break;
1932 case GEANY_KEYS_CLIPBOARD_COPY:
1933 on_copy1_activate(NULL, NULL);
1934 break;
1935 case GEANY_KEYS_CLIPBOARD_PASTE:
1936 on_paste1_activate(NULL, NULL);
1937 break;
1938 case GEANY_KEYS_CLIPBOARD_COPYLINE:
1939 sci_send_command(doc->editor->sci, SCI_LINECOPY);
1940 break;
1941 case GEANY_KEYS_CLIPBOARD_CUTLINE:
1942 sci_send_command(doc->editor->sci, SCI_LINECUT);
1943 break;
1945 return TRUE;
1949 static void goto_tag(GeanyDocument *doc, gboolean definition)
1951 gchar *text = get_current_word_or_sel(doc);
1953 if (text)
1954 symbols_goto_tag(text, definition);
1955 else
1956 utils_beep();
1958 g_free(text);
1962 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
1963 static gboolean cb_func_goto_action(guint key_id)
1965 gint cur_line;
1966 GeanyDocument *doc = document_get_current();
1968 if (doc == NULL)
1969 return TRUE;
1971 cur_line = sci_get_current_line(doc->editor->sci);
1973 switch (key_id)
1975 case GEANY_KEYS_GOTO_BACK:
1976 navqueue_go_back();
1977 return TRUE;
1978 case GEANY_KEYS_GOTO_FORWARD:
1979 navqueue_go_forward();
1980 return TRUE;
1981 case GEANY_KEYS_GOTO_LINE:
1983 if (toolbar_prefs.visible)
1985 GtkWidget *wid = toolbar_get_widget_child_by_name("GotoEntry");
1987 /* use toolbar item if shown */
1988 if (wid)
1990 gtk_widget_grab_focus(wid);
1991 return TRUE;
1994 on_go_to_line_activate(NULL, NULL);
1995 return TRUE;
1997 case GEANY_KEYS_GOTO_MATCHINGBRACE:
1998 goto_matching_brace(doc);
1999 return TRUE;
2000 case GEANY_KEYS_GOTO_TOGGLEMARKER:
2002 sci_toggle_marker_at_line(doc->editor->sci, cur_line, 1);
2003 return TRUE;
2005 case GEANY_KEYS_GOTO_NEXTMARKER:
2007 gint mline = sci_marker_next(doc->editor->sci, cur_line + 1, 1 << 1, TRUE);
2009 if (mline != -1)
2011 sci_set_current_line(doc->editor->sci, mline);
2012 editor_display_current_line(doc->editor, 0.5F);
2014 return TRUE;
2016 case GEANY_KEYS_GOTO_PREVIOUSMARKER:
2018 gint mline = sci_marker_previous(doc->editor->sci, cur_line - 1, 1 << 1, TRUE);
2020 if (mline != -1)
2022 sci_set_current_line(doc->editor->sci, mline);
2023 editor_display_current_line(doc->editor, 0.5F);
2025 return TRUE;
2027 case GEANY_KEYS_GOTO_TAGDEFINITION:
2028 goto_tag(doc, TRUE);
2029 return TRUE;
2030 case GEANY_KEYS_GOTO_TAGDECLARATION:
2031 goto_tag(doc, FALSE);
2032 return TRUE;
2034 /* only check editor-sensitive keybindings when editor has focus so home,end still
2035 * work in other widgets */
2036 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
2037 return FALSE;
2039 switch (key_id)
2041 case GEANY_KEYS_GOTO_LINESTART:
2042 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
2043 break;
2044 case GEANY_KEYS_GOTO_LINEEND:
2045 sci_send_command(doc->editor->sci, SCI_LINEEND);
2046 break;
2047 case GEANY_KEYS_GOTO_LINEENDVISUAL:
2048 sci_send_command(doc->editor->sci, SCI_LINEENDDISPLAY);
2049 break;
2050 case GEANY_KEYS_GOTO_PREVWORDPART:
2051 sci_send_command(doc->editor->sci, SCI_WORDPARTLEFT);
2052 break;
2053 case GEANY_KEYS_GOTO_NEXTWORDPART:
2054 sci_send_command(doc->editor->sci, SCI_WORDPARTRIGHT);
2055 break;
2057 return TRUE;
2061 static void duplicate_lines(GeanyEditor *editor)
2063 if (sci_get_lines_selected(editor->sci) > 1)
2064 { /* ignore extra_line because of selecting lines from the line number column */
2065 editor_select_lines(editor, FALSE);
2066 sci_selection_duplicate(editor->sci);
2068 else if (sci_has_selection(editor->sci))
2069 sci_selection_duplicate(editor->sci);
2070 else
2071 sci_line_duplicate(editor->sci);
2075 static void delete_lines(GeanyEditor *editor)
2077 editor_select_lines(editor, TRUE); /* include last line (like cut lines, copy lines do) */
2078 sci_clear(editor->sci); /* (SCI_LINEDELETE only does 1 line) */
2082 static void move_lines(GeanyEditor *editor, gboolean down)
2084 ScintillaObject *sci = editor->sci;
2085 gchar *text;
2086 gint pos, line, len;
2088 sci_start_undo_action(sci);
2089 editor_select_lines(editor, FALSE);
2090 len = sci_get_selected_text_length(sci);
2092 pos = sci_get_selection_start(sci);
2093 line = sci_get_line_from_position(sci, pos);
2094 if (down)
2095 line++;
2096 else
2097 line--;
2099 text = sci_get_selection_contents(sci);
2100 sci_clear(sci);
2102 pos = sci_get_position_from_line(sci, line);
2103 sci_insert_text(sci, pos, text);
2104 g_free(text);
2106 sci_set_current_position(sci, pos, TRUE);
2107 sci_set_selection_end(sci, pos + len - 1);
2109 sci_end_undo_action(sci);
2113 /* common function for editor keybindings, only valid when scintilla has focus. */
2114 static gboolean cb_func_editor_action(guint key_id)
2116 GeanyDocument *doc = document_get_current();
2117 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2119 /* edit keybindings only valid when scintilla widget has focus */
2120 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2121 return FALSE; /* also makes tab work outside editor */
2123 switch (key_id)
2125 case GEANY_KEYS_EDITOR_UNDO:
2126 on_undo1_activate(NULL, NULL);
2127 break;
2128 case GEANY_KEYS_EDITOR_REDO:
2129 on_redo1_activate(NULL, NULL);
2130 break;
2131 case GEANY_KEYS_EDITOR_SCROLLTOLINE:
2132 editor_scroll_to_line(doc->editor, -1, 0.5F);
2133 break;
2134 case GEANY_KEYS_EDITOR_SCROLLLINEUP:
2135 sci_send_command(doc->editor->sci, SCI_LINESCROLLUP);
2136 break;
2137 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN:
2138 sci_send_command(doc->editor->sci, SCI_LINESCROLLDOWN);
2139 break;
2140 case GEANY_KEYS_EDITOR_DUPLICATELINE:
2141 duplicate_lines(doc->editor);
2142 break;
2143 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR:
2144 editor_goto_next_snippet_cursor(doc->editor);
2145 break;
2146 case GEANY_KEYS_EDITOR_DELETELINE:
2147 delete_lines(doc->editor);
2148 break;
2149 case GEANY_KEYS_EDITOR_DELETELINETOEND:
2150 sci_send_command(doc->editor->sci, SCI_DELLINERIGHT);
2151 break;
2152 case GEANY_KEYS_EDITOR_TRANSPOSELINE:
2153 sci_send_command(doc->editor->sci, SCI_LINETRANSPOSE);
2154 break;
2155 case GEANY_KEYS_EDITOR_AUTOCOMPLETE:
2156 editor_start_auto_complete(doc->editor, sci_get_current_position(doc->editor->sci), TRUE);
2157 break;
2158 case GEANY_KEYS_EDITOR_CALLTIP:
2159 editor_show_calltip(doc->editor, -1);
2160 break;
2161 case GEANY_KEYS_EDITOR_MACROLIST:
2162 editor_show_macro_list(doc->editor);
2163 break;
2164 case GEANY_KEYS_EDITOR_CONTEXTACTION:
2165 if (check_current_word(doc))
2166 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu,
2167 "context_action1")), NULL);
2168 break;
2169 case GEANY_KEYS_EDITOR_COMPLETESNIPPET:
2170 /* allow tab to be overloaded */
2171 return check_snippet_completion(doc);
2173 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION:
2175 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
2176 GEANY_KEYS_EDITOR_COMPLETESNIPPET);
2178 switch (kb->key)
2180 case GDK_space:
2181 sci_add_text(doc->editor->sci, " ");
2182 break;
2183 case GDK_Tab:
2184 sci_send_command(doc->editor->sci, SCI_TAB);
2185 break;
2186 default:
2187 break;
2189 break;
2191 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION:
2192 return editor_complete_word_part(doc->editor);
2194 case GEANY_KEYS_EDITOR_MOVELINEUP:
2195 move_lines(doc->editor, FALSE);
2196 break;
2197 case GEANY_KEYS_EDITOR_MOVELINEDOWN:
2198 move_lines(doc->editor, TRUE);
2199 break;
2201 return TRUE;
2205 static void join_lines(GeanyEditor *editor)
2207 gint start, end, i;
2209 start = sci_get_line_from_position(editor->sci,
2210 sci_get_selection_start(editor->sci));
2211 end = sci_get_line_from_position(editor->sci,
2212 sci_get_selection_end(editor->sci));
2214 /* if there is only one line in selection, join it with the following one */
2215 if (end == start)
2216 end = start + 1;
2219 * remove trailing spaces for every line except the last one
2220 * so that these spaces won't appear within text after joining
2222 for (i = start; i < end; i++)
2223 editor_strip_line_trailing_spaces(editor, i);
2225 /* remove starting spaces from second and following lines due to the same reason */
2226 for (i = start + 1; i <= end; i++)
2227 sci_set_line_indentation(editor->sci, i, 0);
2230 * SCI_LINESJOIN automatically adds spaces between joined lines, including
2231 * empty ones. We should drop empty lines if we want only one space to be
2232 * inserted (see also example below). I don't think we should care of that.
2235 sci_set_target_start(editor->sci,
2236 sci_get_position_from_line(editor->sci, start));
2237 sci_set_target_end(editor->sci,
2238 sci_get_position_from_line(editor->sci, end));
2239 sci_lines_join(editor->sci);
2242 * Example: joining
2244 * [TAB]if (something_wrong)
2245 * [TAB]{
2246 * [TAB][TAB]
2247 * [TAB][TAB]exit(1);[SPACE][SPACE]
2248 * [TAB]}[SPACE]
2250 * gives
2252 * [TAB]if (something_wrong) { exit(1); }[SPACE]
2257 static void split_lines(GeanyEditor *editor, gint column)
2259 gint start, indent, linescount, i, end;
2260 gchar c;
2261 ScintillaObject *sci = editor->sci;
2263 /* don't include trailing newlines */
2264 end = sci_get_selection_end(sci);
2265 while ((c = sci_get_char_at(sci, end - 1)) == '\n' || c == '\r') end--;
2266 sci_set_selection_end(sci, end);
2268 start = sci_get_line_from_position(editor->sci,
2269 sci_get_selection_start(editor->sci));
2272 * If several lines are selected, first join them.
2273 * This allows to reformat text paragraphs easily.
2275 if (sci_get_lines_selected(editor->sci) > 1)
2276 join_lines(editor);
2279 * If this line is short enough, just return
2281 if (column > sci_get_line_end_position(editor->sci, start) -
2282 sci_get_position_from_line(editor->sci, start))
2284 return;
2288 * We have to manipulate line indentation so that indentation
2289 * of the resulting lines would be consistent. For example,
2290 * the result of splitting "[TAB]very long content":
2292 * +-------------+-------------+
2293 * | proper | wrong |
2294 * +-------------+-------------+
2295 * | [TAB]very | [TAB]very |
2296 * | [TAB]long | long |
2297 * | [TAB]content| content |
2298 * +-------------+-------------+
2300 indent = sci_get_line_indentation(editor->sci, start);
2301 sci_set_line_indentation(editor->sci, start, 0);
2304 * Use sci_get_line_count() to determine how many new lines
2305 * appeared during splitting. SCI_LINESSPLIT should better return
2306 * this value itself...
2308 sci_target_from_selection(editor->sci);
2309 linescount = sci_get_line_count(editor->sci);
2310 sci_lines_split(editor->sci,
2311 (column - indent) * sci_text_width(editor->sci, STYLE_DEFAULT, " "));
2312 linescount = sci_get_line_count(editor->sci) - linescount;
2314 /* Fix indentation. */
2315 for (i = start; i <= start + linescount; i++)
2316 sci_set_line_indentation(editor->sci, i, indent);
2318 /* Remove trailing spaces. */
2319 if (editor_prefs.newline_strip || file_prefs.strip_trailing_spaces)
2321 for (i = start; i <= start + linescount; i++)
2322 editor_strip_line_trailing_spaces(editor, i);
2327 /* if cursor < anchor, swap them */
2328 static void sci_fix_selection(ScintillaObject *sci)
2330 gint start, end;
2332 start = sci_get_selection_start(sci);
2333 end = sci_get_selection_end(sci);
2334 sci_set_selection(sci, start, end);
2338 static void reflow_paragraph(GeanyEditor *editor)
2340 ScintillaObject *sci = editor->sci;
2341 gboolean sel;
2342 gint column = -1;
2344 if (editor->line_breaking)
2346 /* use line break column if enabled */
2347 column = editor_prefs.line_break_column;
2349 else if (editor_get_long_line_type() != 2)
2351 /* use long line if enabled */
2352 column = editor_get_long_line_column();
2354 else
2356 /* do nothing if no column is defined */
2357 utils_beep();
2358 return;
2360 sci_start_undo_action(sci);
2361 sel = sci_has_selection(sci);
2362 if (!sel)
2364 gint line, pos;
2366 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
2367 /* deselect last line break */
2368 pos = sci_get_selection_end(sci);
2369 line = sci_get_line_from_position(sci, pos);
2370 if (line < sci_get_line_count(sci) - 1)
2372 /* not last line */
2373 pos = sci_get_line_end_position(sci, line - 1);
2374 sci_set_selection_end(sci, pos);
2377 sci_fix_selection(sci);
2378 split_lines(editor, column);
2379 if (!sel)
2380 sci_set_anchor(sci, -1);
2382 sci_end_undo_action(sci);
2386 /* common function for format keybindings, only valid when scintilla has focus. */
2387 static gboolean cb_func_format_action(guint key_id)
2389 GeanyDocument *doc = document_get_current();
2390 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2392 /* keybindings only valid when scintilla widget has focus */
2393 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2394 return TRUE;
2396 switch (key_id)
2398 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE:
2399 on_menu_toggle_line_commentation1_activate(NULL, NULL);
2400 break;
2401 case GEANY_KEYS_FORMAT_COMMENTLINE:
2402 on_menu_comment_line1_activate(NULL, NULL);
2403 break;
2404 case GEANY_KEYS_FORMAT_UNCOMMENTLINE:
2405 on_menu_uncomment_line1_activate(NULL, NULL);
2406 break;
2407 case GEANY_KEYS_FORMAT_INCREASEINDENT:
2408 on_menu_increase_indent1_activate(NULL, NULL);
2409 break;
2410 case GEANY_KEYS_FORMAT_DECREASEINDENT:
2411 on_menu_decrease_indent1_activate(NULL, NULL);
2412 break;
2413 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE:
2414 editor_indentation_by_one_space(doc->editor, -1, FALSE);
2415 break;
2416 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE:
2417 editor_indentation_by_one_space(doc->editor, -1, TRUE);
2418 break;
2419 case GEANY_KEYS_FORMAT_AUTOINDENT:
2420 editor_smart_line_indentation(doc->editor, -1);
2421 break;
2422 case GEANY_KEYS_FORMAT_TOGGLECASE:
2423 on_toggle_case1_activate(NULL, NULL);
2424 break;
2425 case GEANY_KEYS_FORMAT_SENDTOCMD1:
2426 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 0)
2427 tools_execute_custom_command(doc, ui_prefs.custom_commands[0]);
2428 break;
2429 case GEANY_KEYS_FORMAT_SENDTOCMD2:
2430 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 1)
2431 tools_execute_custom_command(doc, ui_prefs.custom_commands[1]);
2432 break;
2433 case GEANY_KEYS_FORMAT_SENDTOCMD3:
2434 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 2)
2435 tools_execute_custom_command(doc, ui_prefs.custom_commands[2]);
2436 break;
2437 case GEANY_KEYS_FORMAT_SENDTOVTE:
2438 on_send_selection_to_vte1_activate(NULL, NULL);
2439 break;
2440 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
2441 reflow_paragraph(doc->editor);
2442 break;
2444 return TRUE;
2448 /* common function for select keybindings, only valid when scintilla has focus. */
2449 static gboolean cb_func_select_action(guint key_id)
2451 GeanyDocument *doc;
2452 ScintillaObject *sci;
2453 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2454 GtkWidget *toolbar_search_entry = toolbar_get_widget_child_by_name("SearchEntry");
2455 GtkWidget *toolbar_goto_entry = toolbar_get_widget_child_by_name("GotoEntry");
2457 /* special case for Select All in the scribble widget */
2458 if (key_id == GEANY_KEYS_SELECT_ALL && focusw == msgwindow.scribble)
2460 g_signal_emit_by_name(msgwindow.scribble, "select-all", TRUE);
2461 return TRUE;
2463 /* special case for Select All in the toolbar search widget */
2464 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_search_entry)
2466 gtk_editable_select_region(GTK_EDITABLE(toolbar_search_entry), 0, -1);
2467 return TRUE;
2469 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_goto_entry)
2471 gtk_editable_select_region(GTK_EDITABLE(toolbar_goto_entry), 0, -1);
2472 return TRUE;
2475 doc = document_get_current();
2476 /* keybindings only valid when scintilla widget has focus */
2477 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2478 return TRUE;
2479 sci = doc->editor->sci;
2481 switch (key_id)
2483 case GEANY_KEYS_SELECT_ALL:
2484 on_menu_select_all1_activate(NULL, NULL);
2485 break;
2486 case GEANY_KEYS_SELECT_WORD:
2487 editor_select_word(doc->editor);
2488 break;
2489 case GEANY_KEYS_SELECT_LINE:
2490 editor_select_lines(doc->editor, FALSE);
2491 break;
2492 case GEANY_KEYS_SELECT_PARAGRAPH:
2493 editor_select_paragraph(doc->editor);
2494 break;
2495 case GEANY_KEYS_SELECT_WORDPARTLEFT:
2496 sci_send_command(sci, SCI_WORDPARTLEFTEXTEND);
2497 break;
2498 case GEANY_KEYS_SELECT_WORDPARTRIGHT:
2499 sci_send_command(sci, SCI_WORDPARTRIGHTEXTEND);
2500 break;
2502 return TRUE;
2506 static gboolean cb_func_document_action(guint key_id)
2508 GeanyDocument *doc = document_get_current();
2510 if (doc == NULL)
2511 return TRUE;
2513 switch (key_id)
2515 case GEANY_KEYS_DOCUMENT_REPLACETABS:
2516 on_replace_tabs_activate(NULL, NULL);
2517 break;
2518 case GEANY_KEYS_DOCUMENT_REPLACESPACES:
2519 on_replace_spaces_activate(NULL, NULL);
2520 break;
2521 case GEANY_KEYS_DOCUMENT_LINEBREAK:
2522 on_line_breaking1_activate(NULL, NULL);
2523 ui_document_show_hide(doc);
2524 break;
2525 case GEANY_KEYS_DOCUMENT_LINEWRAP:
2526 on_line_wrapping1_toggled(NULL, NULL);
2527 ui_document_show_hide(doc);
2528 break;
2529 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST:
2530 document_update_tag_list(doc, TRUE);
2531 break;
2532 case GEANY_KEYS_DOCUMENT_FOLDALL:
2533 editor_fold_all(doc->editor);
2534 break;
2535 case GEANY_KEYS_DOCUMENT_UNFOLDALL:
2536 editor_unfold_all(doc->editor);
2537 break;
2538 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD:
2539 if (editor_prefs.folding)
2541 gint line = sci_get_current_line(doc->editor->sci);
2542 editor_toggle_fold(doc->editor, line, 0);
2543 break;
2546 return TRUE;
2550 /* common function for insert keybindings, only valid when scintilla has focus. */
2551 static gboolean cb_func_insert_action(guint key_id)
2553 GeanyDocument *doc = document_get_current();
2554 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2556 /* keybindings only valid when scintilla widget has focus */
2557 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2558 return TRUE;
2560 switch (key_id)
2562 case GEANY_KEYS_INSERT_ALTWHITESPACE:
2563 editor_insert_alternative_whitespace(doc->editor);
2564 break;
2565 case GEANY_KEYS_INSERT_DATE:
2566 gtk_menu_item_activate(GTK_MENU_ITEM(
2567 ui_lookup_widget(main_widgets.window, "insert_date_custom1")));
2568 break;
2570 return TRUE;
2574 /* update key combination */
2575 void keybindings_update_combo(GeanyKeyBinding *kb, guint key, GdkModifierType mods)
2577 GtkWidget *widget = kb->menu_item;
2579 if (widget && kb->key)
2580 gtk_widget_remove_accelerator(widget, kb_accel_group, kb->key, kb->mods);
2582 kb->key = key;
2583 kb->mods = mods;
2585 if (widget && kb->key)
2586 gtk_widget_add_accelerator(widget, "activate", kb_accel_group,
2587 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
2591 /* used for plugins */
2592 GeanyKeyGroup *keybindings_set_group(GeanyKeyGroup *group, const gchar *section_name,
2593 const gchar *label, gsize count, GeanyKeyGroupCallback callback)
2595 g_return_val_if_fail(section_name, NULL);
2596 g_return_val_if_fail(count, NULL);
2598 /* prevent conflict with core bindings */
2599 g_return_val_if_fail(!g_str_equal(section_name, keybindings_keyfile_group_name), NULL);
2601 if (!group)
2602 group = g_new0(GeanyKeyGroup, 1);
2604 if (!group->keys || count > group->count)
2606 /* allow resizing existing array of keys */
2607 group->keys = g_renew(GeanyKeyBinding, group->keys, count);
2608 memset(group->keys + group->count, 0, (count - group->count) * sizeof(GeanyKeyBinding));
2610 group->plugin = TRUE;
2611 add_kb_group(group, section_name, label, count, group->keys, callback);
2612 return group;
2616 /* used for plugins */
2617 void keybindings_free_group(GeanyKeyGroup *group)
2619 GeanyKeyBinding *kb;
2621 g_assert(group->plugin);
2623 foreach_c_array(kb, group->keys, group->count)
2625 g_free(kb->name);
2626 g_free(kb->label);
2628 g_free(group->keys);
2629 g_ptr_array_remove_fast(keybinding_groups, group);
2630 g_free(group);