Beep if the user hasn't entered a class name on pressing OK.
[geany-mirror.git] / src / keybindings.c
blobee50ff58a36230aed8225d25384541ea597b1e82
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 * @see plugin_set_key_group().
28 **/
31 #include "geany.h"
33 #include <gdk/gdkkeysyms.h>
34 #include <string.h>
36 #include "keybindings.h"
37 #include "support.h"
38 #include "utils.h"
39 #include "ui_utils.h"
40 #include "document.h"
41 #include "documentprivate.h"
42 #include "filetypes.h"
43 #include "callbacks.h"
44 #include "prefs.h"
45 #include "msgwindow.h"
46 #include "editor.h"
47 #include "sciwrappers.h"
48 #include "build.h"
49 #include "tools.h"
50 #include "navqueue.h"
51 #include "symbols.h"
52 #include "vte.h"
53 #include "toolbar.h"
54 #include "sidebar.h"
55 #include "geanywraplabel.h"
56 #include "main.h"
57 #include "search.h"
60 GPtrArray *keybinding_groups; /* array of GeanyKeyGroup pointers */
62 /* keyfile group name for non-plugin KB groups */
63 const gchar keybindings_keyfile_group_name[] = "Bindings";
65 static GtkAccelGroup *kb_accel_group = NULL;
66 static const gboolean swap_alt_tab_order = FALSE;
68 const gsize MAX_MRU_DOCS = 20;
69 static GQueue *mru_docs = NULL;
70 static guint mru_pos = 0;
72 static gboolean switch_dialog_cancelled = TRUE;
73 static GtkWidget *switch_dialog = NULL;
74 static GtkWidget *switch_dialog_label = NULL;
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);
79 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
81 static gboolean check_current_word(GeanyDocument *doc);
82 static gboolean read_current_word(GeanyDocument *doc);
83 static gchar *get_current_word_or_sel(GeanyDocument *doc);
85 static gboolean cb_func_file_action(guint key_id);
86 static gboolean cb_func_project_action(guint key_id);
87 static gboolean cb_func_editor_action(guint key_id);
88 static gboolean cb_func_select_action(guint key_id);
89 static gboolean cb_func_format_action(guint key_id);
90 static gboolean cb_func_insert_action(guint key_id);
91 static gboolean cb_func_search_action(guint key_id);
92 static gboolean cb_func_goto_action(guint key_id);
93 static gboolean cb_func_switch_action(guint key_id);
94 static gboolean cb_func_clipboard_action(guint key_id);
95 static gboolean cb_func_build_action(guint key_id);
96 static gboolean cb_func_document_action(guint key_id);
97 static gboolean cb_func_view_action(guint key_id);
99 /* note: new keybindings should normally use per group callbacks */
100 static void cb_func_menu_help(guint key_id);
101 static void cb_func_menu_preferences(guint key_id);
103 static void cb_func_menu_fullscreen(guint key_id);
104 static void cb_func_menu_messagewindow(guint key_id);
106 static void cb_func_menu_opencolorchooser(guint key_id);
108 static void cb_func_switch_tableft(guint key_id);
109 static void cb_func_switch_tabright(guint key_id);
110 static void cb_func_switch_tablastused(guint key_id);
111 static void cb_func_move_tab(guint key_id);
113 static void add_popup_menu_accels(void);
116 /** Looks up a keybinding item.
117 * @param group Group.
118 * @param key_id Keybinding index for the group.
119 * @return The keybinding.
120 * @since 0.19. */
121 GeanyKeyBinding *keybindings_get_item(GeanyKeyGroup *group, gsize key_id)
123 g_assert(key_id < group->count);
125 return &group->keys[key_id];
129 /* This is used to set default keybindings on startup.
130 * Menu accels are set in apply_kb_accel(). */
131 /** Fills a GeanyKeyBinding struct item.
132 * @param group Group.
133 * @param key_id Keybinding index for the group.
134 * @param callback Function to call when activated, or @c NULL to use the group callback.
135 * Usually it's better to use the group callback instead - see plugin_set_key_group().
136 * @param key (Lower case) default key, e.g. @c GDK_j, but usually 0 for unset.
137 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
138 * @param kf_name Key name for the configuration file, such as @c "menu_new".
139 * @param label Label used in the preferences dialog keybindings tab. May contain
140 * underscores - these won't be displayed.
141 * @param menu_item Optional widget to set an accelerator for, or @c NULL.
142 * @return The keybinding - normally this is ignored. */
143 GeanyKeyBinding *keybindings_set_item(GeanyKeyGroup *group, gsize key_id,
144 GeanyKeyCallback callback, guint key, GdkModifierType mod,
145 gchar *kf_name, gchar *label, GtkWidget *menu_item)
147 GeanyKeyBinding *kb = keybindings_get_item(group, key_id);
149 if (group->plugin)
151 /* some plugins e.g. GeanyLua need these fields duplicated */
152 setptr(kb->name, g_strdup(kf_name));
153 setptr(kb->label, g_strdup(label));
155 else
157 kb->name = kf_name;
158 kb->label = label;
160 kb->key = key;
161 kb->mods = mod;
162 kb->callback = callback;
163 kb->menu_item = menu_item;
164 return kb;
168 static GeanyKeyGroup *add_kb_group(GeanyKeyGroup *group,
169 const gchar *name, const gchar *label, gsize count, GeanyKeyBinding *keys,
170 GeanyKeyGroupCallback callback)
172 g_ptr_array_add(keybinding_groups, group);
174 group->name = name;
175 group->label = label;
176 group->count = count;
177 group->keys = keys;
178 group->callback = callback;
179 return group;
183 /* Lookup a widget in the main window */
184 #define LW(widget_name) \
185 ui_lookup_widget(main_widgets.window, G_STRINGIFY(widget_name))
187 /* Expansion for group_id = FILE:
188 * static GeanyKeyBinding FILE_keys[GEANY_KEYS_FILE_COUNT]; */
189 #define DECLARE_KEYS(group_id) \
190 static GeanyKeyBinding group_id ## _keys[GEANY_KEYS_ ## group_id ## _COUNT]
192 /* Expansion for group_id = FILE:
193 * add_kb_group(&groups[GEANY_KEY_GROUP_FILE], NULL, _("File menu"),
194 * GEANY_KEYS_FILE_COUNT, FILE_keys, callback); */
195 #define ADD_KB_GROUP(group_id, label, callback) \
196 add_kb_group(&groups[GEANY_KEY_GROUP_ ## group_id], keybindings_keyfile_group_name, label, \
197 GEANY_KEYS_ ## group_id ## _COUNT, group_id ## _keys, callback)
199 /* Init all fields of keys with default values.
200 * The menu_item field is always the main menu item, popup menu accelerators are
201 * set in add_popup_menu_accels(). */
202 static void init_default_kb(void)
204 static GeanyKeyGroup groups[GEANY_KEY_GROUP_COUNT];
205 GeanyKeyGroup *group;
206 DECLARE_KEYS(FILE);
207 DECLARE_KEYS(PROJECT);
208 DECLARE_KEYS(EDITOR);
209 DECLARE_KEYS(CLIPBOARD);
210 DECLARE_KEYS(SELECT);
211 DECLARE_KEYS(FORMAT);
212 DECLARE_KEYS(INSERT);
213 DECLARE_KEYS(SETTINGS);
214 DECLARE_KEYS(SEARCH);
215 DECLARE_KEYS(GOTO);
216 DECLARE_KEYS(VIEW);
217 DECLARE_KEYS(FOCUS);
218 DECLARE_KEYS(NOTEBOOK);
219 DECLARE_KEYS(DOCUMENT);
220 DECLARE_KEYS(BUILD);
221 DECLARE_KEYS(TOOLS);
222 DECLARE_KEYS(HELP);
224 group = ADD_KB_GROUP(FILE, _("File"), cb_func_file_action);
226 keybindings_set_item(group, GEANY_KEYS_FILE_NEW, NULL,
227 GDK_n, GDK_CONTROL_MASK, "menu_new", _("New"), NULL);
228 keybindings_set_item(group, GEANY_KEYS_FILE_OPEN, NULL,
229 GDK_o, GDK_CONTROL_MASK, "menu_open", _("Open"), NULL);
230 keybindings_set_item(group, GEANY_KEYS_FILE_OPENSELECTED, NULL,
231 GDK_o, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_open_selected",
232 _("Open selected file"), LW(menu_open_selected_file1));
233 keybindings_set_item(group, GEANY_KEYS_FILE_SAVE, NULL,
234 GDK_s, GDK_CONTROL_MASK, "menu_save", _("Save"), NULL);
235 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEAS, NULL,
236 0, 0, "menu_saveas", _("Save as"), LW(menu_save_as1));
237 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEALL, NULL,
238 GDK_S, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_saveall", _("Save all"),
239 LW(menu_save_all1));
240 keybindings_set_item(group, GEANY_KEYS_FILE_PRINT, NULL,
241 GDK_p, GDK_CONTROL_MASK, "menu_print", _("Print"), LW(print1));
242 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSE, NULL,
243 GDK_w, GDK_CONTROL_MASK, "menu_close", _("Close"), LW(menu_close1));
244 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSEALL, NULL,
245 GDK_w, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_closeall", _("Close all"),
246 LW(menu_close_all1));
247 keybindings_set_item(group, GEANY_KEYS_FILE_RELOAD, NULL,
248 GDK_r, GDK_CONTROL_MASK, "menu_reloadfile", _("Reload file"), LW(menu_reload1));
249 keybindings_set_item(group, GEANY_KEYS_FILE_OPENLASTTAB, NULL,
250 0, 0, "file_openlasttab", _("Re-open last closed tab"), NULL);
252 group = ADD_KB_GROUP(PROJECT, _("Project"), cb_func_project_action);
254 keybindings_set_item(group, GEANY_KEYS_PROJECT_PROPERTIES, NULL,
255 0, 0, "project_properties", _("Project properties"), LW(project_properties1));
257 group = ADD_KB_GROUP(EDITOR, _("Editor"), cb_func_editor_action);
259 keybindings_set_item(group, GEANY_KEYS_EDITOR_UNDO, NULL,
260 GDK_z, GDK_CONTROL_MASK, "menu_undo", _("Undo"), LW(menu_undo2));
261 keybindings_set_item(group, GEANY_KEYS_EDITOR_REDO, NULL,
262 GDK_y, GDK_CONTROL_MASK, "menu_redo", _("Redo"), LW(menu_redo2));
263 keybindings_set_item(group, GEANY_KEYS_EDITOR_DUPLICATELINE, NULL,
264 GDK_d, GDK_CONTROL_MASK, "edit_duplicateline", _("_Duplicate Line or Selection"),
265 LW(duplicate_line_or_selection1));
266 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINE, NULL,
267 GDK_k, GDK_CONTROL_MASK, "edit_deleteline", _("_Delete Current Line(s)"),
268 LW(delete_current_line_s_1));
269 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINETOEND, NULL,
270 GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_deletelinetoend",
271 _("Delete to line end"), NULL);
272 /* transpose may fit better in format group */
273 keybindings_set_item(group, GEANY_KEYS_EDITOR_TRANSPOSELINE, NULL,
274 GDK_t, GDK_CONTROL_MASK, "edit_transposeline", _("_Transpose Current Line"),
275 LW(transpose_current_line1));
276 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLTOLINE, NULL,
277 GDK_l, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_scrolltoline", _("Scroll to current line"), NULL);
278 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEUP, NULL,
279 GDK_Up, GDK_MOD1_MASK, "edit_scrolllineup", _("Scroll up the view by one line"), NULL);
280 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEDOWN, NULL,
281 GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
282 keybindings_set_item(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL,
283 GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
284 keybindings_set_item(group, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR, NULL,
285 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
286 keybindings_set_item(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, NULL,
287 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
288 keybindings_set_item(group, GEANY_KEYS_EDITOR_CONTEXTACTION, NULL,
289 0, 0, "popup_contextaction", _("Context Action"), NULL);
290 keybindings_set_item(group, GEANY_KEYS_EDITOR_AUTOCOMPLETE, NULL,
291 GDK_space, GDK_CONTROL_MASK, "edit_autocomplete", _("Complete word"), NULL);
292 keybindings_set_item(group, GEANY_KEYS_EDITOR_CALLTIP, NULL,
293 GDK_space, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_calltip", _("Show calltip"), NULL);
294 keybindings_set_item(group, GEANY_KEYS_EDITOR_MACROLIST, NULL,
295 GDK_Return, GDK_CONTROL_MASK, "edit_macrolist", _("Show macro list"), NULL);
296 keybindings_set_item(group, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION, NULL,
297 GDK_Tab, 0, "edit_wordpartcompletion", _("Word part completion"), NULL);
298 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEUP, NULL,
299 0, 0, "edit_movelineup", _("Move line(s) up"), NULL);
300 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEDOWN, NULL,
301 0, 0, "edit_movelinedown", _("Move line(s) down"), NULL);
303 group = ADD_KB_GROUP(CLIPBOARD, _("Clipboard"), cb_func_clipboard_action);
305 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUT, NULL,
306 GDK_x, GDK_CONTROL_MASK, "menu_cut", _("Cut"), NULL);
307 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPY, NULL,
308 GDK_c, GDK_CONTROL_MASK, "menu_copy", _("Copy"), NULL);
309 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_PASTE, NULL,
310 GDK_v, GDK_CONTROL_MASK, "menu_paste", _("Paste"), NULL);
311 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPYLINE, NULL,
312 GDK_c, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_copyline", _("_Copy Current Line(s)"),
313 LW(cut_current_line_s_1));
314 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUTLINE, NULL,
315 GDK_x, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_cutline", _("_Cut Current Line(s)"),
316 LW(copy_current_line_s_1));
318 group = ADD_KB_GROUP(SELECT, _("Select"), cb_func_select_action);
320 keybindings_set_item(group, GEANY_KEYS_SELECT_ALL, NULL,
321 GDK_a, GDK_CONTROL_MASK, "menu_selectall", _("Select All"), LW(menu_select_all1));
322 keybindings_set_item(group, GEANY_KEYS_SELECT_WORD, NULL,
323 GDK_w, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectword", _("Select current word"), NULL);
324 keybindings_set_item(group, GEANY_KEYS_SELECT_LINE, NULL,
325 GDK_l, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectline", _("_Select Current Line(s)"),
326 LW(select_current_line_s_1));
327 keybindings_set_item(group, GEANY_KEYS_SELECT_PARAGRAPH, NULL,
328 GDK_p, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectparagraph", _("_Select Current Paragraph"),
329 LW(select_current_paragraph1));
330 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTLEFT, NULL,
331 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL);
332 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTRIGHT, NULL,
333 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL);
335 group = ADD_KB_GROUP(FORMAT, _("Format"), cb_func_format_action);
337 keybindings_set_item(group, GEANY_KEYS_FORMAT_TOGGLECASE, NULL,
338 GDK_u, GDK_CONTROL_MASK | GDK_MOD1_MASK, "edit_togglecase",
339 _("Toggle Case of Selection"), LW(menu_toggle_case2));
340 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE, NULL,
341 GDK_e, GDK_CONTROL_MASK, "edit_commentlinetoggle", _("Toggle line commentation"),
342 LW(menu_toggle_line_commentation1));
343 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINE, NULL,
344 0, 0, "edit_commentline", _("Comment line(s)"), LW(menu_comment_line1));
345 keybindings_set_item(group, GEANY_KEYS_FORMAT_UNCOMMENTLINE, NULL,
346 0, 0, "edit_uncommentline", _("Uncomment line(s)"), LW(menu_uncomment_line1));
347 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENT, NULL,
348 GDK_i, GDK_CONTROL_MASK, "edit_increaseindent", _("Increase indent"),
349 LW(menu_increase_indent1));
350 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENT, NULL,
351 GDK_u, GDK_CONTROL_MASK, "edit_decreaseindent", _("Decrease indent"),
352 LW(menu_decrease_indent1));
353 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE, NULL,
354 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL);
355 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE, NULL,
356 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL);
357 keybindings_set_item(group, GEANY_KEYS_FORMAT_AUTOINDENT, NULL,
358 0, 0, "edit_autoindent", _("_Smart Line Indent"), LW(smart_line_indent1));
359 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD1, NULL,
360 GDK_1, GDK_CONTROL_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL);
361 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD2, NULL,
362 GDK_2, GDK_CONTROL_MASK, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL);
363 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD3, NULL,
364 GDK_3, GDK_CONTROL_MASK, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL);
365 /* may fit better in editor group */
366 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOVTE, NULL,
367 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), LW(send_selection_to_vte1));
368 keybindings_set_item(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
369 GDK_j, GDK_CONTROL_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
370 LW(reflow_lines_block1));
372 group = ADD_KB_GROUP(INSERT, _("Insert"), cb_func_insert_action);
374 keybindings_set_item(group, GEANY_KEYS_INSERT_DATE, NULL,
375 GDK_d, GDK_SHIFT_MASK | GDK_MOD1_MASK, "menu_insert_date", _("Insert date"),
376 LW(insert_date_custom1));
377 keybindings_set_item(group, GEANY_KEYS_INSERT_ALTWHITESPACE, NULL,
378 0, 0, "edit_insertwhitespace", _("_Insert Alternative White Space"),
379 LW(insert_alternative_white_space1));
381 group = ADD_KB_GROUP(SETTINGS, _("Settings"), NULL);
383 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PREFERENCES, cb_func_menu_preferences,
384 GDK_p, GDK_CONTROL_MASK | GDK_MOD1_MASK, "menu_preferences", _("Preferences"),
385 LW(preferences1));
386 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES, cb_func_menu_preferences,
387 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), LW(plugin_preferences1));
389 group = ADD_KB_GROUP(SEARCH, _("Search"), cb_func_search_action);
391 keybindings_set_item(group, GEANY_KEYS_SEARCH_FIND, NULL,
392 GDK_f, GDK_CONTROL_MASK, "menu_find", _("Find"), LW(find1));
393 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXT, NULL,
394 GDK_g, GDK_CONTROL_MASK, "menu_findnext", _("Find Next"), LW(find_next1));
395 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVIOUS, NULL,
396 GDK_g, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findprevious", _("Find Previous"),
397 LW(find_previous1));
398 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXTSEL, NULL,
399 0, 0, "menu_findnextsel", _("Find Next Selection"), LW(find_nextsel1));
400 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVSEL, NULL,
401 0, 0, "menu_findprevsel", _("Find Previous Selection"), LW(find_prevsel1));
402 keybindings_set_item(group, GEANY_KEYS_SEARCH_REPLACE, NULL,
403 GDK_h, GDK_CONTROL_MASK, "menu_replace", _("Replace"), LW(replace1));
404 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDINFILES, NULL, GDK_f,
405 GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findinfiles", _("Find in Files"),
406 LW(find_in_files1));
407 keybindings_set_item(group, GEANY_KEYS_SEARCH_NEXTMESSAGE, NULL,
408 0, 0, "menu_nextmessage", _("Next Message"), LW(next_message1));
409 keybindings_set_item(group, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE, NULL,
410 0, 0, "menu_previousmessage", _("Previous Message"), LW(previous_message1));
411 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDUSAGE, NULL,
412 0, 0, "popup_findusage", _("Find Usage"), NULL);
413 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, NULL,
414 0, 0, "popup_finddocumentusage", _("Find Document Usage"), NULL);
415 keybindings_set_item(group, GEANY_KEYS_SEARCH_MARKALL, NULL,
416 GDK_m, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "find_markall", _("Mark All"), NULL);
418 group = ADD_KB_GROUP(GOTO, _("Go to"), cb_func_goto_action);
420 keybindings_set_item(group, GEANY_KEYS_GOTO_BACK, NULL,
421 0, 0, "nav_back", _("Navigate back a location"), NULL);
422 keybindings_set_item(group, GEANY_KEYS_GOTO_FORWARD, NULL,
423 0, 0, "nav_forward", _("Navigate forward a location"), NULL);
424 keybindings_set_item(group, GEANY_KEYS_GOTO_LINE, NULL,
425 GDK_l, GDK_CONTROL_MASK, "menu_gotoline", _("Go to Line"), LW(go_to_line1));
426 keybindings_set_item(group, GEANY_KEYS_GOTO_MATCHINGBRACE, NULL,
427 GDK_b, GDK_CONTROL_MASK, "edit_gotomatchingbrace",
428 _("Go to matching brace"), NULL);
429 keybindings_set_item(group, GEANY_KEYS_GOTO_TOGGLEMARKER, NULL,
430 GDK_m, GDK_CONTROL_MASK, "edit_togglemarker",
431 _("Toggle marker"), NULL);
432 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTMARKER, NULL,
433 GDK_period, GDK_CONTROL_MASK, "edit_gotonextmarker",
434 _("_Go to Next Marker"), LW(go_to_next_marker1));
435 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVIOUSMARKER, NULL,
436 GDK_comma, GDK_CONTROL_MASK, "edit_gotopreviousmarker",
437 _("_Go to Previous Marker"), LW(go_to_previous_marker1));
438 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDEFINITION, NULL,
439 0, 0, "popup_gototagdefinition", _("Go to Tag Definition"), NULL);
440 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDECLARATION, NULL,
441 0, 0, "popup_gototagdeclaration", _("Go to Tag Declaration"), NULL);
442 keybindings_set_item(group, GEANY_KEYS_GOTO_LINESTART, NULL,
443 GDK_Home, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL);
444 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEEND, NULL,
445 GDK_End, 0, "edit_gotolineend", _("Go to End of Line"), NULL);
446 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEENDVISUAL, NULL,
447 GDK_End, GDK_MOD1_MASK, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL);
448 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVWORDPART, NULL,
449 GDK_slash, GDK_CONTROL_MASK, "edit_prevwordstart", _("Go to Previous Word Part"), NULL);
450 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTWORDPART, NULL,
451 GDK_backslash, GDK_CONTROL_MASK, "edit_nextwordstart", _("Go to Next Word Part"), NULL);
453 group = ADD_KB_GROUP(VIEW, _("View"), cb_func_view_action);
455 keybindings_set_item(group, GEANY_KEYS_VIEW_TOGGLEALL, NULL,
456 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
457 LW(menu_toggle_all_additional_widgets1));
458 keybindings_set_item(group, GEANY_KEYS_VIEW_FULLSCREEN, cb_func_menu_fullscreen,
459 GDK_F11, 0, "menu_fullscreen", _("Fullscreen"), LW(menu_fullscreen1));
460 keybindings_set_item(group, GEANY_KEYS_VIEW_MESSAGEWINDOW, cb_func_menu_messagewindow,
461 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
462 LW(menu_show_messages_window1));
463 keybindings_set_item(group, GEANY_KEYS_VIEW_SIDEBAR, NULL,
464 0, 0, "toggle_sidebar", _("Toggle Sidebar"), LW(menu_show_sidebar1));
465 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMIN, NULL,
466 GDK_plus, GDK_CONTROL_MASK, "menu_zoomin", _("Zoom In"), LW(menu_zoom_in1));
467 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMOUT, NULL,
468 GDK_minus, GDK_CONTROL_MASK, "menu_zoomout", _("Zoom Out"), LW(menu_zoom_out1));
469 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMRESET, NULL,
470 GDK_0, GDK_CONTROL_MASK, "normal_size", _("Zoom Reset"), LW(normal_size1));
472 group = ADD_KB_GROUP(FOCUS, _("Focus"), cb_func_switch_action);
474 keybindings_set_item(group, GEANY_KEYS_FOCUS_EDITOR, NULL,
475 GDK_F2, 0, "switch_editor", _("Switch to Editor"), NULL);
476 keybindings_set_item(group, GEANY_KEYS_FOCUS_SCRIBBLE, NULL,
477 GDK_F6, 0, "switch_scribble", _("Switch to Scribble"), NULL);
478 keybindings_set_item(group, GEANY_KEYS_FOCUS_VTE, NULL,
479 GDK_F4, 0, "switch_vte", _("Switch to VTE"), NULL);
480 keybindings_set_item(group, GEANY_KEYS_FOCUS_SEARCHBAR, NULL,
481 GDK_F7, 0, "switch_search_bar", _("Switch to Search Bar"), NULL);
482 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR, NULL,
483 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL);
484 keybindings_set_item(group, GEANY_KEYS_FOCUS_COMPILER, NULL,
485 0, 0, "switch_compiler", _("Switch to Compiler"), NULL);
486 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGES, NULL,
487 0, 0, "switch_messages", _("Switch to Messages"), NULL);
488 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGE_WINDOW, NULL,
489 0, 0, "switch_message_window", _("Switch to Message Window"), NULL);
490 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST, NULL,
491 0, 0, "switch_sidebar_doc_list", _("Switch to Sidebar Document List"), NULL);
492 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST, NULL,
493 0, 0, "switch_sidebar_symbol_list", _("Switch to Sidebar Symbol List"), NULL);
495 group = ADD_KB_GROUP(NOTEBOOK, _("Notebook tab"), NULL);
497 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT, cb_func_switch_tableft,
498 GDK_Page_Up, GDK_CONTROL_MASK, "switch_tableft", _("Switch to left document"), NULL);
499 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
500 GDK_Page_Down, GDK_CONTROL_MASK, "switch_tabright", _("Switch to right document"), NULL);
501 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
502 GDK_Tab, GDK_CONTROL_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
503 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
504 GDK_Page_Up, GDK_MOD1_MASK, "move_tableft", _("Move document left"), NULL);
505 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
506 GDK_Page_Down, GDK_MOD1_MASK, "move_tabright", _("Move document right"), NULL);
507 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABFIRST, cb_func_move_tab,
508 0, 0, "move_tabfirst", _("Move document first"), NULL);
509 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLAST, cb_func_move_tab,
510 0, 0, "move_tablast", _("Move document last"), NULL);
512 group = ADD_KB_GROUP(DOCUMENT, _("Document"), cb_func_document_action);
514 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEWRAP, NULL,
515 0, 0, "menu_linewrap", _("Toggle Line wrapping"), LW(menu_line_wrapping1));
516 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEBREAK, NULL,
517 0, 0, "menu_linebreak", _("Toggle Line breaking"), LW(line_breaking1));
518 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACETABS, NULL,
519 0, 0, "menu_replacetabs", _("Replace tabs by space"), LW(menu_replace_tabs));
520 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACESPACES, NULL,
521 0, 0, "menu_replacespaces", _("Replace spaces by tabs"), LW(menu_replace_spaces));
522 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_TOGGLEFOLD, NULL,
523 0, 0, "menu_togglefold", _("Toggle current fold"), NULL);
524 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_FOLDALL, NULL,
525 0, 0, "menu_foldall", _("Fold all"), LW(menu_fold_all1));
526 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_UNFOLDALL, NULL,
527 0, 0, "menu_unfoldall", _("Unfold all"), LW(menu_unfold_all1));
528 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_RELOADTAGLIST, NULL,
529 GDK_r, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "reloadtaglist", _("Reload symbol list"), NULL);
531 group = ADD_KB_GROUP(BUILD, _("Build"), cb_func_build_action);
533 keybindings_set_item(group, GEANY_KEYS_BUILD_COMPILE, NULL,
534 GDK_F8, 0, "build_compile", _("Compile"), NULL);
535 keybindings_set_item(group, GEANY_KEYS_BUILD_LINK, NULL,
536 GDK_F9, 0, "build_link", _("Build"), NULL);
537 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKE, NULL,
538 GDK_F9, GDK_SHIFT_MASK, "build_make", _("Make all"), NULL);
539 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOWNTARGET, NULL,
540 GDK_F9, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "build_makeowntarget",
541 _("Make custom target"), NULL);
542 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOBJECT, NULL,
543 0, 0, "build_makeobject", _("Make object"), NULL);
544 keybindings_set_item(group, GEANY_KEYS_BUILD_NEXTERROR, NULL,
545 0, 0, "build_nexterror", _("Next error"), NULL);
546 keybindings_set_item(group, GEANY_KEYS_BUILD_PREVIOUSERROR, NULL,
547 0, 0, "build_previouserror", _("Previous error"), NULL);
548 keybindings_set_item(group, GEANY_KEYS_BUILD_RUN, NULL,
549 GDK_F5, 0, "build_run", _("Run"), NULL);
550 keybindings_set_item(group, GEANY_KEYS_BUILD_OPTIONS, NULL,
551 0, 0, "build_options", _("Build options"), NULL);
553 group = ADD_KB_GROUP(TOOLS, _("Tools"), NULL);
555 keybindings_set_item(group, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER, cb_func_menu_opencolorchooser,
556 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), LW(menu_choose_color1));
558 group = ADD_KB_GROUP(HELP, _("Help"), NULL);
560 keybindings_set_item(group, GEANY_KEYS_HELP_HELP, cb_func_menu_help,
561 GDK_F1, 0, "menu_help", _("Help"), LW(help1));
565 /* before the tab changes, add the current document to the MRU list */
566 static void on_notebook_switch_page(void)
568 GeanyDocument *old = document_get_current();
570 /* when closing current doc, old is NULL.
571 * Don't add to the mru list when switch dialog is visible. */
572 if (old && switch_dialog_cancelled)
574 g_queue_remove(mru_docs, old);
575 g_queue_push_head(mru_docs, old);
577 if (g_queue_get_length(mru_docs) > MAX_MRU_DOCS)
578 g_queue_pop_tail(mru_docs);
583 /* really this should be just after a document was closed, not idle */
584 static gboolean on_idle_close(gpointer data)
586 GeanyDocument *current;
588 current = document_get_current();
589 if (current && g_queue_peek_head(mru_docs) == current)
590 g_queue_pop_head(mru_docs);
592 return FALSE;
596 static void on_document_close(GObject *obj, GeanyDocument *doc)
598 if (! main_status.quitting)
600 g_queue_remove(mru_docs, doc);
601 g_idle_add(on_idle_close, NULL);
606 void keybindings_init(void)
608 mru_docs = g_queue_new();
609 g_signal_connect(main_widgets.notebook, "switch-page",
610 G_CALLBACK(on_notebook_switch_page), NULL);
611 g_signal_connect(geany_object, "document-close",
612 G_CALLBACK(on_document_close), NULL);
614 keybinding_groups = g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT);
616 kb_accel_group = gtk_accel_group_new();
618 init_default_kb();
620 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), kb_accel_group);
622 g_signal_connect(main_widgets.window, "key-press-event", G_CALLBACK(on_key_press_event), NULL);
623 /* in case the switch dialog misses an event while drawing the dialog */
624 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
628 typedef void (*KBItemCallback) (GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data);
630 static void keybindings_foreach(KBItemCallback cb, gpointer user_data)
632 gsize g, i;
633 GeanyKeyGroup *group;
634 GeanyKeyBinding *kb;
636 for (g = 0; g < keybinding_groups->len; g++)
638 group = g_ptr_array_index(keybinding_groups, g);
639 for (i = 0; i < group->count; i++)
641 kb = &group->keys[i];
643 cb(group, kb, user_data);
649 static void load_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
651 GKeyFile *config = user_data;
652 gchar *val;
653 guint key;
654 GdkModifierType mods;
656 val = g_key_file_get_string(config, group->name, kb->name, NULL);
657 if (val != NULL)
659 gtk_accelerator_parse(val, &key, &mods);
660 kb->key = key;
661 kb->mods = mods;
662 g_free(val);
667 static void load_user_kb(void)
669 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
670 GKeyFile *config = g_key_file_new();
672 /* now load user defined keys */
673 if (g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
675 keybindings_foreach(load_kb, config);
677 g_free(configfile);
678 g_key_file_free(config);
682 static void apply_kb_accel(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
684 if (kb->key != 0 && kb->menu_item)
686 gtk_widget_add_accelerator(kb->menu_item, "activate", kb_accel_group,
687 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
692 void keybindings_load_keyfile(void)
694 load_user_kb();
695 add_popup_menu_accels();
697 /* set menu accels now, after user keybindings have been read */
698 keybindings_foreach(apply_kb_accel, NULL);
702 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id, GtkWidget *menuitem)
704 GeanyKeyBinding *kb = &group->keys[kb_id];
706 if (kb->key != 0)
707 gtk_widget_add_accelerator(menuitem, "activate", kb_accel_group,
708 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
712 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
713 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
715 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
716 static void add_popup_menu_accels(void)
718 GeanyKeyGroup *group;
720 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_EDITOR);
721 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO, undo1);
722 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO, redo1);
723 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION, context_action1);
725 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_SELECT);
726 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL, menu_select_all2);
728 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_INSERT);
729 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE, insert_date_custom2);
731 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_FILE);
732 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED, menu_open_selected_file2);
734 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_SEARCH);
735 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE, find_usage1);
736 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, find_document_usage1);
738 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_GOTO);
739 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_LINE, go_to_line);
740 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION, goto_tag_definition1);
741 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDECLARATION, goto_tag_declaration1);
743 /* Format and Commands share the menu bar submenus */
744 /* Build menu items are set if the build menus are created */
748 static void set_keyfile_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
750 GKeyFile *config = user_data;
751 gchar *val;
753 val = gtk_accelerator_name(kb->key, kb->mods);
754 g_key_file_set_string(config, group->name, kb->name, val);
755 g_free(val);
759 /* just write the content of the keys array to the config file */
760 void keybindings_write_to_file(void)
762 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
763 gchar *data;
764 GKeyFile *config = g_key_file_new();
766 /* add comment if the file is newly created */
767 if (! g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
769 g_key_file_set_comment(config, NULL, NULL,
770 "Keybindings for Geany\nThe format looks like \"<Control>a\" or \"<Shift><Alt>F1\".\n"
771 "But you can also change the keys in Geany's preferences dialog.", NULL);
774 keybindings_foreach(set_keyfile_kb, config);
776 /* write the file */
777 data = g_key_file_to_data(config, NULL, NULL);
778 utils_write_file(configfile, data);
780 g_free(data);
781 g_free(configfile);
782 g_key_file_free(config);
786 void keybindings_free(void)
788 g_ptr_array_free(keybinding_groups, TRUE);
789 g_queue_free(mru_docs);
793 gchar *keybindings_get_label(GeanyKeyBinding *kb)
795 return utils_str_remove_chars(g_strdup(kb->label), "_");
799 static void fill_shortcut_labels_treeview(GtkWidget *tree)
801 gsize g, i;
802 GeanyKeyBinding *kb;
803 GeanyKeyGroup *group;
804 GtkListStore *store;
805 GtkTreeIter iter;
807 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, PANGO_TYPE_WEIGHT);
809 for (g = 0; g < keybinding_groups->len; g++)
811 group = g_ptr_array_index(keybinding_groups, g);
813 if (g > 0)
815 gtk_list_store_append(store, &iter);
816 gtk_list_store_set(store, &iter, -1);
819 gtk_list_store_append(store, &iter);
820 gtk_list_store_set(store, &iter, 0, group->label, 2, PANGO_WEIGHT_BOLD, -1);
822 for (i = 0; i < group->count; i++)
824 gchar *shortcut, *label;
826 kb = &group->keys[i];
827 label = keybindings_get_label(kb);
828 shortcut = gtk_accelerator_get_label(kb->key, kb->mods);
830 gtk_list_store_append(store, &iter);
831 gtk_list_store_set(store, &iter, 0, label, 1, shortcut, 2, PANGO_WEIGHT_NORMAL, -1);
833 g_free(shortcut);
834 g_free(label);
838 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
839 g_object_unref(store);
843 static GtkWidget *create_dialog(void)
845 GtkWidget *dialog, *tree, *label, *swin, *vbox;
846 GtkCellRenderer *text_renderer;
847 GtkTreeViewColumn *column;
849 dialog = gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets.window),
850 GTK_DIALOG_DESTROY_WITH_PARENT,
851 GTK_STOCK_EDIT, GTK_RESPONSE_APPLY,
852 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
853 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
854 gtk_box_set_spacing(GTK_BOX(vbox), 6);
855 gtk_widget_set_name(dialog, "GeanyDialog");
857 gtk_window_set_default_size(GTK_WINDOW(dialog), -1, GEANY_DEFAULT_DIALOG_HEIGHT);
859 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
861 label = gtk_label_new(_("The following keyboard shortcuts are configurable:"));
862 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
864 tree = gtk_tree_view_new();
865 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
866 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
868 text_renderer = gtk_cell_renderer_text_new();
869 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
870 column = gtk_tree_view_column_new_with_attributes(
871 NULL, text_renderer, "text", 0, "weight", 2, NULL);
872 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
874 text_renderer = gtk_cell_renderer_text_new();
875 column = gtk_tree_view_column_new_with_attributes(NULL, text_renderer, "text", 1, NULL);
876 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
878 fill_shortcut_labels_treeview(tree);
880 swin = gtk_scrolled_window_new(NULL, NULL);
881 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_NEVER,
882 GTK_POLICY_AUTOMATIC);
883 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_ETCHED_IN);
884 gtk_container_add(GTK_CONTAINER(swin), tree);
886 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
887 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
889 return dialog;
893 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
894 static GtkWidget *key_dialog = NULL;
896 static void on_dialog_response(GtkWidget *dialog, gint response, gpointer user_data)
898 if (response == GTK_RESPONSE_APPLY)
900 GtkWidget *wid;
902 prefs_show_dialog();
903 /* select the KB page */
904 wid = ui_lookup_widget(ui_widgets.prefs_dialog, "frame22");
905 if (wid != NULL)
907 GtkNotebook *nb = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
909 if (nb != NULL)
910 gtk_notebook_set_current_page(nb, gtk_notebook_page_num(nb, wid));
913 gtk_widget_destroy(dialog);
914 key_dialog = NULL;
918 void keybindings_show_shortcuts(void)
920 if (key_dialog)
921 gtk_widget_destroy(key_dialog); /* in case the key_dialog is still visible */
923 key_dialog = create_dialog();
924 g_signal_connect(key_dialog, "response", G_CALLBACK(on_dialog_response), NULL);
925 gtk_widget_show_all(key_dialog);
929 static gboolean check_fixed_kb(guint keyval, guint state)
931 /* check alt-0 to alt-9 for setting current notebook page */
932 if (state & GDK_MOD1_MASK && keyval >= GDK_0 && keyval <= GDK_9)
934 gint page = keyval - GDK_0 - 1;
935 gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
937 /* alt-0 is for the rightmost tab */
938 if (keyval == GDK_0)
939 page = npages - 1;
940 /* invert the order if tabs are added on the other side */
941 if (swap_alt_tab_order && ! file_prefs.tab_order_ltr)
942 page = (npages - 1) - page;
944 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
945 return TRUE;
947 if (keyval == GDK_Page_Up || keyval == GDK_Page_Down)
949 /* switch to first or last document */
950 if (state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
952 if (keyval == GDK_Page_Up)
953 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), 0);
954 if (keyval == GDK_Page_Down)
955 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
956 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) - 1);
957 return TRUE;
960 return FALSE;
964 static gboolean check_snippet_completion(GeanyDocument *doc)
966 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
968 g_return_val_if_fail(doc, FALSE);
970 /* keybinding only valid when scintilla widget has focus */
971 if (focusw == GTK_WIDGET(doc->editor->sci))
973 ScintillaObject *sci = doc->editor->sci;
974 gint pos = sci_get_current_position(sci);
976 if (editor_prefs.complete_snippets)
977 return editor_complete_snippet(doc->editor, pos);
979 return FALSE;
983 /* Transforms a GdkEventKey event into a GdkEventButton event */
984 static void trigger_button_event(GtkWidget *widget, guint32 event_time)
986 GdkEventButton *event;
987 gboolean ret;
989 event = g_new0(GdkEventButton, 1);
991 if (GTK_IS_TEXT_VIEW(widget))
992 event->window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT);
993 else
994 event->window = widget->window;
995 event->time = event_time;
996 event->type = GDK_BUTTON_PRESS;
997 event->button = 3;
999 g_signal_emit_by_name(widget, "button-press-event", event, &ret);
1000 g_signal_emit_by_name(widget, "button-release-event", event, &ret);
1002 g_free(event);
1006 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
1007 * widgets. Without this special handling, the notebook tab list of the documents' notebook
1008 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
1009 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1010 * notebook tab list. */
1011 static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
1013 if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
1015 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1016 if (doc != NULL)
1018 if (focusw == doc->priv->tag_tree)
1020 trigger_button_event(focusw, event_time);
1021 return TRUE;
1023 if (focusw == GTK_WIDGET(doc->editor->sci))
1025 if (keyval == GDK_Menu)
1026 { /* show editor popup menu */
1027 trigger_button_event(focusw, event_time);
1028 return TRUE;
1030 else
1031 { /* show tab bar menu */
1032 trigger_button_event(main_widgets.notebook, event_time);
1033 return TRUE;
1037 if (focusw == tv.tree_openfiles
1038 || focusw == msgwindow.tree_status
1039 || focusw == msgwindow.tree_compiler
1040 || focusw == msgwindow.tree_msg
1041 || focusw == msgwindow.scribble
1042 #ifdef HAVE_VTE
1043 || (vte_info.have_vte && focusw == vc->vte)
1044 #endif
1047 trigger_button_event(focusw, event_time);
1048 return TRUE;
1051 return FALSE;
1055 #ifdef HAVE_VTE
1056 static gboolean on_menu_expose_event(GtkWidget *widget, GdkEventExpose *event,
1057 gpointer user_data)
1059 if (!GTK_WIDGET_SENSITIVE(widget))
1060 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1061 return FALSE;
1065 static gboolean set_sensitive(gpointer widget)
1067 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1068 return FALSE;
1072 static gboolean check_vte(GdkModifierType state, guint keyval)
1074 guint i;
1075 GtkWidget *widget;
1077 if (! vc->enable_bash_keys)
1078 return FALSE;
1079 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != vc->vte)
1080 return FALSE;
1081 /* prevent menubar flickering: */
1082 if (state == GDK_SHIFT_MASK && (keyval >= GDK_a && keyval <= GDK_z))
1083 return FALSE;
1084 if (state == 0 && (keyval < GDK_F1 || keyval > GDK_F35)) /* e.g. backspace */
1085 return FALSE;
1087 /* make focus commands override any bash commands */
1088 for (i = 0; i < GEANY_KEYS_FOCUS_COUNT; i++)
1090 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FOCUS, i);
1092 if (state == kb->mods && keyval == kb->key)
1093 return FALSE;
1096 /* Temporarily disable the menus to prevent conflicting menu accelerators
1097 * from overriding the VTE bash shortcuts.
1098 * Note: maybe there's a better way of doing this ;-) */
1099 widget = ui_lookup_widget(main_widgets.window, "menubar1");
1100 gtk_widget_set_sensitive(widget, FALSE);
1102 /* make the menubar sensitive before it is redrawn */
1103 static gboolean connected = FALSE;
1104 if (!connected)
1105 g_signal_connect(widget, "expose-event", G_CALLBACK(on_menu_expose_event), NULL);
1108 widget = main_widgets.editor_menu;
1109 gtk_widget_set_sensitive(widget, FALSE);
1110 g_idle_add(set_sensitive, widget);
1111 return TRUE;
1113 #endif
1116 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1117 static guint key_kp_translate(guint key_in)
1119 switch (key_in)
1121 case GDK_KP_Down:
1122 return GDK_Down;
1123 case GDK_KP_Up:
1124 return GDK_Up;
1125 case GDK_KP_Left:
1126 return GDK_Left;
1127 case GDK_KP_Right:
1128 return GDK_Right;
1129 case GDK_KP_Home:
1130 return GDK_Home;
1131 case GDK_KP_End:
1132 return GDK_End;
1133 case GDK_KP_Page_Up:
1134 return GDK_Page_Up;
1135 case GDK_KP_Page_Down:
1136 return GDK_Page_Down;
1137 case GDK_KP_Delete:
1138 return GDK_Delete;
1139 case GDK_KP_Insert:
1140 return GDK_Insert;
1141 default:
1142 return key_in;
1147 /* Stripped down version of the main keypress event handler which can be used
1148 * to process foreign events. Instead of executing the keybinding, a pointer to the
1149 * keybinding structure is returned.
1150 * Additionally, the group_id and binding_id are filled with the appropriate indexes
1151 * if non-NULL. */
1152 const GeanyKeyBinding *keybindings_check_event(GdkEventKey *ev, gint *group_id, gint *binding_id)
1154 guint state, keyval;
1155 gsize g, i;
1156 GeanyKeyGroup *group;
1157 GeanyKeyBinding *kb;
1159 if (ev->keyval == 0)
1160 return FALSE;
1162 keyval = ev->keyval;
1163 state = ev->state & gtk_accelerator_get_default_mod_mask();
1164 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1165 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1166 if (keyval >= GDK_A && keyval <= GDK_Z)
1167 keyval += GDK_a - GDK_A;
1169 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1170 keyval = key_kp_translate(keyval);
1172 for (g = 0; g < keybinding_groups->len; g++)
1174 group = g_ptr_array_index(keybinding_groups, g);
1176 for (i = 0; i < group->count; i++)
1178 kb = &group->keys[i];
1179 if (keyval == kb->key && state == kb->mods)
1181 if (group_id != NULL)
1182 *group_id = g;
1183 if (binding_id != NULL)
1184 *binding_id = i;
1185 return kb;
1189 return NULL;
1193 /* central keypress event handler, almost all keypress events go to this function */
1194 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1196 guint state, keyval;
1197 gsize g, i;
1198 GeanyDocument *doc;
1199 GeanyKeyGroup *group;
1200 GeanyKeyBinding *kb;
1202 if (ev->keyval == 0)
1203 return FALSE;
1205 doc = document_get_current();
1206 if (doc)
1207 document_check_disk_status(doc, FALSE);
1209 keyval = ev->keyval;
1210 state = ev->state & gtk_accelerator_get_default_mod_mask();
1211 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1212 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1213 if (keyval >= GDK_A && keyval <= GDK_Z)
1214 keyval += GDK_a - GDK_A;
1216 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1217 keyval = key_kp_translate(keyval);
1219 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1221 /* special cases */
1222 #ifdef HAVE_VTE
1223 if (vte_info.have_vte && check_vte(state, keyval))
1224 return FALSE;
1225 #endif
1226 if (check_menu_key(doc, keyval, state, ev->time))
1227 return TRUE;
1229 for (g = 0; g < keybinding_groups->len; g++)
1231 group = g_ptr_array_index(keybinding_groups, g);
1233 for (i = 0; i < group->count; i++)
1235 kb = &group->keys[i];
1236 if (keyval == kb->key && state == kb->mods)
1238 /* call the corresponding callback function for this shortcut */
1239 if (kb->callback)
1241 kb->callback(i);
1242 return TRUE;
1244 else if (group->callback)
1246 if (group->callback(i))
1247 return TRUE;
1248 else
1249 continue; /* not handled */
1251 g_warning("No callback for keybinding %s: %s!", group->name, kb->name);
1255 /* fixed keybindings can be overridden by user bindings, so check them last */
1256 if (check_fixed_kb(keyval, state))
1257 return TRUE;
1258 return FALSE;
1262 static gboolean is_modifier_key(guint keyval)
1264 switch (keyval)
1266 case GDK_Shift_L:
1267 case GDK_Shift_R:
1268 case GDK_Control_L:
1269 case GDK_Control_R:
1270 case GDK_Meta_L:
1271 case GDK_Meta_R:
1272 case GDK_Alt_L:
1273 case GDK_Alt_R:
1274 case GDK_Super_L:
1275 case GDK_Super_R:
1276 case GDK_Hyper_L:
1277 case GDK_Hyper_R:
1278 return TRUE;
1279 default:
1280 return FALSE;
1285 GeanyKeyBinding *keybindings_lookup_item(guint group_id, guint key_id)
1287 GeanyKeyGroup *group;
1289 g_return_val_if_fail(group_id < keybinding_groups->len, NULL);
1291 group = g_ptr_array_index(keybinding_groups, group_id);
1293 g_return_val_if_fail(group, NULL);
1294 g_return_val_if_fail(key_id < group->count, NULL);
1296 return &group->keys[key_id];
1300 /** Mimics a (built-in only) keybinding action.
1301 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1302 * @param group_id The index for the key group that contains the @a key_id keybinding.
1303 * @param key_id The keybinding command index. */
1304 void keybindings_send_command(guint group_id, guint key_id)
1306 GeanyKeyBinding *kb;
1308 g_return_if_fail(group_id < GEANY_KEY_GROUP_COUNT); /* can't use this for plugin groups */
1310 kb = keybindings_lookup_item(group_id, key_id);
1311 if (kb)
1313 if (kb->callback)
1314 kb->callback(key_id);
1315 else
1317 GeanyKeyGroup *group = g_ptr_array_index(keybinding_groups, group_id);
1319 if (group->callback)
1320 group->callback(key_id);
1326 /* These are the callback functions, either each group or each shortcut has it's
1327 * own function. */
1330 static gboolean cb_func_file_action(guint key_id)
1332 switch (key_id)
1334 case GEANY_KEYS_FILE_NEW:
1335 document_new_file(NULL, NULL, NULL);
1336 break;
1337 case GEANY_KEYS_FILE_OPEN:
1338 on_open1_activate(NULL, NULL);
1339 break;
1340 case GEANY_KEYS_FILE_OPENSELECTED:
1341 on_menu_open_selected_file1_activate(NULL, NULL);
1342 break;
1343 case GEANY_KEYS_FILE_OPENLASTTAB:
1345 gchar *utf8_filename = g_queue_peek_head(ui_prefs.recent_queue);
1346 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1347 document_open_file(locale_filename, FALSE, NULL, NULL);
1348 g_free(locale_filename);
1349 break;
1351 case GEANY_KEYS_FILE_SAVE:
1352 on_save1_activate(NULL, NULL);
1353 break;
1354 case GEANY_KEYS_FILE_SAVEAS:
1355 on_save_as1_activate(NULL, NULL);
1356 break;
1357 case GEANY_KEYS_FILE_SAVEALL:
1358 on_save_all1_activate(NULL, NULL);
1359 break;
1360 case GEANY_KEYS_FILE_CLOSE:
1361 on_close1_activate(NULL, NULL);
1362 break;
1363 case GEANY_KEYS_FILE_CLOSEALL:
1364 on_close_all1_activate(NULL, NULL);
1365 break;
1366 case GEANY_KEYS_FILE_RELOAD:
1367 on_toolbutton_reload_clicked(NULL, NULL);
1368 break;
1369 case GEANY_KEYS_FILE_PRINT:
1370 on_print1_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_PROPERTIES:
1382 if (app->project)
1383 on_project_properties1_activate(NULL, NULL);
1384 break;
1386 return TRUE;
1390 static void cb_func_menu_preferences(guint key_id)
1392 switch (key_id)
1394 case GEANY_KEYS_SETTINGS_PREFERENCES:
1395 on_preferences1_activate(NULL, NULL);
1396 break;
1397 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES:
1398 on_plugin_preferences1_activate(NULL, NULL);
1399 break;
1404 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id)
1406 on_help1_activate(NULL, NULL);
1410 static gboolean cb_func_search_action(guint key_id)
1412 GeanyDocument *doc = document_get_current();
1413 ScintillaObject *sci;
1415 if (key_id == GEANY_KEYS_SEARCH_FINDINFILES)
1417 on_find_in_files1_activate(NULL, NULL); /* works without docs too */
1418 return TRUE;
1420 if (!doc)
1421 return TRUE;
1422 sci = doc->editor->sci;
1424 switch (key_id)
1426 case GEANY_KEYS_SEARCH_FIND:
1427 on_find1_activate(NULL, NULL); break;
1428 case GEANY_KEYS_SEARCH_FINDNEXT:
1429 on_find_next1_activate(NULL, NULL); break;
1430 case GEANY_KEYS_SEARCH_FINDPREVIOUS:
1431 on_find_previous1_activate(NULL, NULL); break;
1432 case GEANY_KEYS_SEARCH_FINDPREVSEL:
1433 on_find_prevsel1_activate(NULL, NULL); break;
1434 case GEANY_KEYS_SEARCH_FINDNEXTSEL:
1435 on_find_nextsel1_activate(NULL, NULL); break;
1436 case GEANY_KEYS_SEARCH_REPLACE:
1437 on_replace1_activate(NULL, NULL); break;
1438 case GEANY_KEYS_SEARCH_NEXTMESSAGE:
1439 on_next_message1_activate(NULL, NULL); break;
1440 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE:
1441 on_previous_message1_activate(NULL, NULL); break;
1442 case GEANY_KEYS_SEARCH_FINDUSAGE:
1443 read_current_word(doc);
1444 on_find_usage1_activate(NULL, NULL);
1445 break;
1446 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE:
1447 read_current_word(doc);
1448 on_find_document_usage1_activate(NULL, NULL);
1449 break;
1450 case GEANY_KEYS_SEARCH_MARKALL:
1452 gchar *text = get_current_word_or_sel(doc);
1454 if (sci_has_selection(sci))
1455 search_mark_all(doc, text, SCFIND_MATCHCASE);
1456 else
1458 /* clears markers if text is null */
1459 search_mark_all(doc, text, SCFIND_MATCHCASE | SCFIND_WHOLEWORD);
1461 g_free(text);
1462 break;
1465 return TRUE;
1469 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id)
1471 on_show_color_chooser1_activate(NULL, NULL);
1475 static gboolean cb_func_view_action(guint key_id)
1477 switch (key_id)
1479 case GEANY_KEYS_VIEW_TOGGLEALL:
1480 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
1481 break;
1482 case GEANY_KEYS_VIEW_SIDEBAR:
1483 on_menu_show_sidebar1_toggled(NULL, NULL);
1484 break;
1485 case GEANY_KEYS_VIEW_ZOOMIN:
1486 on_zoom_in1_activate(NULL, NULL);
1487 break;
1488 case GEANY_KEYS_VIEW_ZOOMOUT:
1489 on_zoom_out1_activate(NULL, NULL);
1490 break;
1491 case GEANY_KEYS_VIEW_ZOOMRESET:
1492 on_normal_size1_activate(NULL, NULL);
1493 break;
1494 default:
1495 break;
1497 return TRUE;
1501 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id)
1503 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1504 ui_lookup_widget(main_widgets.window, "menu_fullscreen1"));
1506 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1510 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id)
1512 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1513 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1515 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1519 static gboolean cb_func_build_action(guint key_id)
1521 GtkWidget *item;
1522 BuildMenuItems *menu_items;
1523 GeanyDocument *doc = document_get_current();
1525 if (doc == NULL)
1526 return TRUE;
1528 if (!GTK_WIDGET_IS_SENSITIVE(ui_lookup_widget(main_widgets.window, "menu_build1")))
1529 return TRUE;
1531 menu_items = build_get_menu_items(doc->file_type->id);
1532 /* TODO make it a table??*/
1533 switch (key_id)
1535 case GEANY_KEYS_BUILD_COMPILE:
1536 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_COMPILE)];
1537 break;
1538 case GEANY_KEYS_BUILD_LINK:
1539 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_BUILD)];
1540 break;
1541 case GEANY_KEYS_BUILD_MAKE:
1542 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_ALL)];
1543 break;
1544 case GEANY_KEYS_BUILD_MAKEOWNTARGET:
1545 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_CUSTOM)];
1546 break;
1547 case GEANY_KEYS_BUILD_MAKEOBJECT:
1548 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)];
1549 break;
1550 case GEANY_KEYS_BUILD_NEXTERROR:
1551 item = menu_items->menu_item[GBG_FIXED][GBF_NEXT_ERROR];
1552 break;
1553 case GEANY_KEYS_BUILD_PREVIOUSERROR:
1554 item = menu_items->menu_item[GBG_FIXED][GBF_PREV_ERROR];
1555 break;
1556 case GEANY_KEYS_BUILD_RUN:
1557 item = menu_items->menu_item[GEANY_GBG_EXEC][GBO_TO_CMD(GEANY_GBO_EXEC)];
1558 break;
1559 case GEANY_KEYS_BUILD_OPTIONS:
1560 item = menu_items->menu_item[GBG_FIXED][GBF_COMMANDS];
1561 break;
1562 default:
1563 item = NULL;
1565 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1566 * sensitive state, but some other menus don't update the sensitive status until
1567 * they are redrawn. */
1568 if (item && GTK_WIDGET_IS_SENSITIVE(item))
1569 gtk_menu_item_activate(GTK_MENU_ITEM(item));
1570 return TRUE;
1574 static gboolean read_current_word(GeanyDocument *doc)
1576 gint pos;
1578 if (doc == NULL)
1579 return FALSE;
1581 pos = sci_get_current_position(doc->editor->sci);
1583 editor_find_current_word(doc->editor, pos,
1584 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1586 return (*editor_info.current_word != 0);
1590 static gboolean check_current_word(GeanyDocument *doc)
1592 if (!read_current_word(doc))
1594 utils_beep();
1595 return FALSE;
1597 return TRUE;
1601 static gchar *get_current_word_or_sel(GeanyDocument *doc)
1603 ScintillaObject *sci = doc->editor->sci;
1605 if (sci_has_selection(sci))
1606 return sci_get_selection_contents(sci);
1608 return read_current_word(doc) ? g_strdup(editor_info.current_word) : NULL;
1612 static void focus_sidebar(void)
1614 if (ui_prefs.sidebar_visible)
1616 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook));
1617 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), page_num);
1619 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1620 gtk_widget_child_focus(page, GTK_DIR_TAB_FORWARD);
1625 static void focus_msgwindow(void)
1627 if (ui_prefs.msgwindow_visible)
1629 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook));
1630 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(msgwindow.notebook), page_num);
1632 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(page)));
1637 static gboolean cb_func_switch_action(guint key_id)
1639 switch (key_id)
1641 case GEANY_KEYS_FOCUS_EDITOR:
1643 GeanyDocument *doc = document_get_current();
1644 if (doc != NULL)
1646 GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
1647 if (GTK_WIDGET_HAS_FOCUS(sci))
1648 ui_update_statusbar(doc, -1);
1649 else
1650 gtk_widget_grab_focus(sci);
1652 break;
1654 case GEANY_KEYS_FOCUS_SCRIBBLE:
1655 msgwin_switch_tab(MSG_SCRATCH, TRUE);
1656 break;
1657 case GEANY_KEYS_FOCUS_SEARCHBAR:
1658 if (toolbar_prefs.visible)
1660 GtkWidget *search_entry = toolbar_get_widget_child_by_name("SearchEntry");
1661 if (search_entry != NULL)
1662 gtk_widget_grab_focus(search_entry);
1664 break;
1665 case GEANY_KEYS_FOCUS_SIDEBAR:
1666 focus_sidebar();
1667 break;
1668 case GEANY_KEYS_FOCUS_VTE:
1669 msgwin_switch_tab(MSG_VTE, TRUE);
1670 break;
1671 case GEANY_KEYS_FOCUS_COMPILER:
1672 msgwin_switch_tab(MSG_COMPILER, TRUE);
1673 break;
1674 case GEANY_KEYS_FOCUS_MESSAGES:
1675 msgwin_switch_tab(MSG_MESSAGE, TRUE);
1676 break;
1677 case GEANY_KEYS_FOCUS_MESSAGE_WINDOW:
1678 focus_msgwindow();
1679 break;
1680 case GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST:
1681 sidebar_focus_openfiles_tab();
1682 break;
1683 case GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST:
1684 sidebar_focus_symbols_tab();
1685 break;
1687 return TRUE;
1691 static void switch_notebook_page(gint direction)
1693 gint page_count, cur_page;
1694 gboolean parent_is_notebook = FALSE;
1695 GtkNotebook *notebook;
1696 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1698 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1701 parent_is_notebook = GTK_IS_NOTEBOOK(focusw);
1703 while (! parent_is_notebook && (focusw = gtk_widget_get_parent(focusw)) != NULL);
1705 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1706 if (parent_is_notebook)
1707 notebook = GTK_NOTEBOOK(focusw);
1708 else
1709 notebook = GTK_NOTEBOOK(main_widgets.notebook);
1711 /* now switch pages */
1712 page_count = gtk_notebook_get_n_pages(notebook);
1713 cur_page = gtk_notebook_get_current_page(notebook);
1715 if (direction == GTK_DIR_LEFT)
1717 if (cur_page > 0)
1718 gtk_notebook_set_current_page(notebook, cur_page - 1);
1719 else
1720 gtk_notebook_set_current_page(notebook, page_count - 1);
1722 else if (direction == GTK_DIR_RIGHT)
1724 if (cur_page < page_count - 1)
1725 gtk_notebook_set_current_page(notebook, cur_page + 1);
1726 else
1727 gtk_notebook_set_current_page(notebook, 0);
1732 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id)
1734 switch_notebook_page(GTK_DIR_LEFT);
1738 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id)
1740 switch_notebook_page(GTK_DIR_RIGHT);
1744 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1746 /* user may have rebound keybinding to a different modifier than Ctrl, so check all */
1747 if (!switch_dialog_cancelled && is_modifier_key(ev->keyval))
1749 switch_dialog_cancelled = TRUE;
1751 if (switch_dialog && GTK_WIDGET_VISIBLE(switch_dialog))
1752 gtk_widget_hide(switch_dialog);
1754 mru_pos = 0;
1756 return FALSE;
1760 static GtkWidget *ui_minimal_dialog_new(GtkWindow *parent, const gchar *title)
1762 GtkWidget *dialog;
1764 dialog = gtk_window_new(GTK_WINDOW_POPUP);
1766 if (parent)
1768 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
1769 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
1771 gtk_window_set_title(GTK_WINDOW(dialog), title);
1772 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
1773 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
1775 gtk_widget_set_name(dialog, "GeanyDialog");
1776 return dialog;
1780 static GtkWidget *create_switch_dialog(void)
1782 GtkWidget *dialog, *widget, *vbox;
1784 dialog = ui_minimal_dialog_new(GTK_WINDOW(main_widgets.window), _("Switch to Document"));
1785 gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE);
1786 gtk_window_set_default_size(GTK_WINDOW(dialog), 150, -1);
1788 vbox = gtk_vbox_new(FALSE, 6);
1789 gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
1790 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1792 widget = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
1793 gtk_container_add(GTK_CONTAINER(vbox), widget);
1795 widget = geany_wrap_label_new(NULL);
1796 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
1797 gtk_container_add(GTK_CONTAINER(vbox), widget);
1798 switch_dialog_label = widget;
1800 g_signal_connect(dialog, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
1801 return dialog;
1805 static gboolean on_switch_timeout(G_GNUC_UNUSED gpointer data)
1807 if (switch_dialog_cancelled)
1809 return FALSE;
1811 if (! switch_dialog || !GTK_WIDGET_VISIBLE(switch_dialog))
1812 mru_pos = 2; /* skip past the previous document */
1813 else
1814 mru_pos += 1;
1816 if (! switch_dialog)
1817 switch_dialog = create_switch_dialog();
1819 geany_wrap_label_set_text(GTK_LABEL(switch_dialog_label),
1820 DOC_FILENAME(document_get_current()));
1821 gtk_widget_show_all(switch_dialog);
1822 return FALSE;
1826 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id)
1828 GeanyDocument *last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1830 if (! DOC_VALID(last_doc))
1832 utils_beep();
1833 mru_pos = 0;
1834 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1836 if (! DOC_VALID(last_doc))
1837 return;
1839 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
1840 document_get_notebook_page(last_doc));
1842 /* if there's a modifier key, we can switch back in MRU order each time unless
1843 * the key is released */
1844 if (! switch_dialog_cancelled)
1846 on_switch_timeout(NULL); /* update filename label */
1848 else
1849 if (keybindings_lookup_item(GEANY_KEY_GROUP_NOTEBOOK,
1850 GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED)->mods)
1852 switch_dialog_cancelled = FALSE;
1854 /* delay showing dialog to give user time to let go of any modifier keys */
1855 g_timeout_add(600, on_switch_timeout, NULL);
1860 /* move document left/right/first/last */
1861 static void cb_func_move_tab(guint key_id)
1863 GtkWidget *sci;
1864 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
1865 gint cur_page = gtk_notebook_get_current_page(nb);
1866 GeanyDocument *doc = document_get_current();
1868 if (doc == NULL)
1869 return;
1871 sci = GTK_WIDGET(doc->editor->sci);
1873 switch (key_id)
1875 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT:
1876 gtk_notebook_reorder_child(nb, sci, cur_page - 1); /* notebook wraps around by default */
1877 break;
1878 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT:
1880 gint npage = cur_page + 1;
1882 if (npage == gtk_notebook_get_n_pages(nb))
1883 npage = 0; /* wraparound */
1884 gtk_notebook_reorder_child(nb, sci, npage);
1885 break;
1887 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST:
1888 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? 0 : -1);
1889 break;
1890 case GEANY_KEYS_NOTEBOOK_MOVETABLAST:
1891 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? -1 : 0);
1892 break;
1894 return;
1898 static void goto_matching_brace(GeanyDocument *doc)
1900 gint pos, new_pos;
1902 if (doc == NULL)
1903 return;
1905 pos = sci_get_current_position(doc->editor->sci);
1906 if (! utils_isbrace(sci_get_char_at(doc->editor->sci, pos), TRUE))
1907 pos--; /* set pos to the brace */
1909 new_pos = sci_find_matching_brace(doc->editor->sci, pos);
1910 if (new_pos != -1)
1911 { /* set the cursor at the brace */
1912 sci_set_current_position(doc->editor->sci, new_pos, FALSE);
1913 editor_display_current_line(doc->editor, 0.5F);
1918 static gboolean cb_func_clipboard_action(guint key_id)
1920 GeanyDocument *doc = document_get_current();
1922 if (doc == NULL)
1923 return TRUE;
1925 switch (key_id)
1927 case GEANY_KEYS_CLIPBOARD_CUT:
1928 on_cut1_activate(NULL, NULL);
1929 break;
1930 case GEANY_KEYS_CLIPBOARD_COPY:
1931 on_copy1_activate(NULL, NULL);
1932 break;
1933 case GEANY_KEYS_CLIPBOARD_PASTE:
1934 on_paste1_activate(NULL, NULL);
1935 break;
1936 case GEANY_KEYS_CLIPBOARD_COPYLINE:
1937 sci_send_command(doc->editor->sci, SCI_LINECOPY);
1938 break;
1939 case GEANY_KEYS_CLIPBOARD_CUTLINE:
1940 sci_send_command(doc->editor->sci, SCI_LINECUT);
1941 break;
1943 return TRUE;
1947 static void goto_tag(GeanyDocument *doc, gboolean definition)
1949 gchar *text = get_current_word_or_sel(doc);
1951 if (text)
1952 symbols_goto_tag(text, definition);
1953 else
1954 utils_beep();
1956 g_free(text);
1960 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
1961 static gboolean cb_func_goto_action(guint key_id)
1963 gint cur_line;
1964 GeanyDocument *doc = document_get_current();
1966 if (doc == NULL)
1967 return TRUE;
1969 cur_line = sci_get_current_line(doc->editor->sci);
1971 switch (key_id)
1973 case GEANY_KEYS_GOTO_BACK:
1974 navqueue_go_back();
1975 return TRUE;
1976 case GEANY_KEYS_GOTO_FORWARD:
1977 navqueue_go_forward();
1978 return TRUE;
1979 case GEANY_KEYS_GOTO_LINE:
1981 if (toolbar_prefs.visible)
1983 GtkWidget *wid = toolbar_get_widget_child_by_name("GotoEntry");
1985 /* use toolbar item if shown */
1986 if (wid)
1988 gtk_widget_grab_focus(wid);
1989 return TRUE;
1992 on_go_to_line_activate(NULL, NULL);
1993 return TRUE;
1995 case GEANY_KEYS_GOTO_MATCHINGBRACE:
1996 goto_matching_brace(doc);
1997 return TRUE;
1998 case GEANY_KEYS_GOTO_TOGGLEMARKER:
2000 sci_toggle_marker_at_line(doc->editor->sci, cur_line, 1);
2001 return TRUE;
2003 case GEANY_KEYS_GOTO_NEXTMARKER:
2005 gint mline = sci_marker_next(doc->editor->sci, cur_line + 1, 1 << 1, TRUE);
2007 if (mline != -1)
2009 sci_set_current_line(doc->editor->sci, mline);
2010 editor_display_current_line(doc->editor, 0.5F);
2012 return TRUE;
2014 case GEANY_KEYS_GOTO_PREVIOUSMARKER:
2016 gint mline = sci_marker_previous(doc->editor->sci, cur_line - 1, 1 << 1, TRUE);
2018 if (mline != -1)
2020 sci_set_current_line(doc->editor->sci, mline);
2021 editor_display_current_line(doc->editor, 0.5F);
2023 return TRUE;
2025 case GEANY_KEYS_GOTO_TAGDEFINITION:
2026 goto_tag(doc, TRUE);
2027 return TRUE;
2028 case GEANY_KEYS_GOTO_TAGDECLARATION:
2029 goto_tag(doc, FALSE);
2030 return TRUE;
2032 /* only check editor-sensitive keybindings when editor has focus so home,end still
2033 * work in other widgets */
2034 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
2035 return FALSE;
2037 switch (key_id)
2039 case GEANY_KEYS_GOTO_LINESTART:
2040 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
2041 break;
2042 case GEANY_KEYS_GOTO_LINEEND:
2043 sci_send_command(doc->editor->sci, SCI_LINEEND);
2044 break;
2045 case GEANY_KEYS_GOTO_LINEENDVISUAL:
2046 sci_send_command(doc->editor->sci, SCI_LINEENDDISPLAY);
2047 break;
2048 case GEANY_KEYS_GOTO_PREVWORDPART:
2049 sci_send_command(doc->editor->sci, SCI_WORDPARTLEFT);
2050 break;
2051 case GEANY_KEYS_GOTO_NEXTWORDPART:
2052 sci_send_command(doc->editor->sci, SCI_WORDPARTRIGHT);
2053 break;
2055 return TRUE;
2059 static void duplicate_lines(GeanyEditor *editor)
2061 if (sci_get_lines_selected(editor->sci) > 1)
2062 { /* ignore extra_line because of selecting lines from the line number column */
2063 editor_select_lines(editor, FALSE);
2064 sci_selection_duplicate(editor->sci);
2066 else if (sci_has_selection(editor->sci))
2067 sci_selection_duplicate(editor->sci);
2068 else
2069 sci_line_duplicate(editor->sci);
2073 static void delete_lines(GeanyEditor *editor)
2075 editor_select_lines(editor, TRUE); /* include last line (like cut lines, copy lines do) */
2076 sci_clear(editor->sci); /* (SCI_LINEDELETE only does 1 line) */
2080 static void move_lines(GeanyEditor *editor, gboolean down)
2082 ScintillaObject *sci = editor->sci;
2083 gchar *text;
2084 gint pos, line, len;
2086 sci_start_undo_action(sci);
2087 editor_select_lines(editor, FALSE);
2088 len = sci_get_selected_text_length(sci);
2090 pos = sci_get_selection_start(sci);
2091 line = sci_get_line_from_position(sci, pos);
2092 if (down)
2093 line++;
2094 else
2095 line--;
2097 text = sci_get_selection_contents(sci);
2098 sci_clear(sci);
2100 pos = sci_get_position_from_line(sci, line);
2101 sci_insert_text(sci, pos, text);
2102 g_free(text);
2104 sci_set_current_position(sci, pos, TRUE);
2105 sci_set_selection_end(sci, pos + len - 1);
2107 sci_end_undo_action(sci);
2111 /* common function for editor keybindings, only valid when scintilla has focus. */
2112 static gboolean cb_func_editor_action(guint key_id)
2114 GeanyDocument *doc = document_get_current();
2115 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2117 /* edit keybindings only valid when scintilla widget has focus */
2118 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2119 return FALSE; /* also makes tab work outside editor */
2121 switch (key_id)
2123 case GEANY_KEYS_EDITOR_UNDO:
2124 on_undo1_activate(NULL, NULL);
2125 break;
2126 case GEANY_KEYS_EDITOR_REDO:
2127 on_redo1_activate(NULL, NULL);
2128 break;
2129 case GEANY_KEYS_EDITOR_SCROLLTOLINE:
2130 editor_scroll_to_line(doc->editor, -1, 0.5F);
2131 break;
2132 case GEANY_KEYS_EDITOR_SCROLLLINEUP:
2133 sci_send_command(doc->editor->sci, SCI_LINESCROLLUP);
2134 break;
2135 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN:
2136 sci_send_command(doc->editor->sci, SCI_LINESCROLLDOWN);
2137 break;
2138 case GEANY_KEYS_EDITOR_DUPLICATELINE:
2139 duplicate_lines(doc->editor);
2140 break;
2141 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR:
2142 editor_goto_next_snippet_cursor(doc->editor);
2143 break;
2144 case GEANY_KEYS_EDITOR_DELETELINE:
2145 delete_lines(doc->editor);
2146 break;
2147 case GEANY_KEYS_EDITOR_DELETELINETOEND:
2148 sci_send_command(doc->editor->sci, SCI_DELLINERIGHT);
2149 break;
2150 case GEANY_KEYS_EDITOR_TRANSPOSELINE:
2151 sci_send_command(doc->editor->sci, SCI_LINETRANSPOSE);
2152 break;
2153 case GEANY_KEYS_EDITOR_AUTOCOMPLETE:
2154 editor_start_auto_complete(doc->editor, sci_get_current_position(doc->editor->sci), TRUE);
2155 break;
2156 case GEANY_KEYS_EDITOR_CALLTIP:
2157 editor_show_calltip(doc->editor, -1);
2158 break;
2159 case GEANY_KEYS_EDITOR_MACROLIST:
2160 editor_show_macro_list(doc->editor);
2161 break;
2162 case GEANY_KEYS_EDITOR_CONTEXTACTION:
2163 if (check_current_word(doc))
2164 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu,
2165 "context_action1")), NULL);
2166 break;
2167 case GEANY_KEYS_EDITOR_COMPLETESNIPPET:
2168 /* allow tab to be overloaded */
2169 return check_snippet_completion(doc);
2171 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION:
2173 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
2174 GEANY_KEYS_EDITOR_COMPLETESNIPPET);
2176 switch (kb->key)
2178 case GDK_space:
2179 sci_add_text(doc->editor->sci, " ");
2180 break;
2181 case GDK_Tab:
2182 sci_send_command(doc->editor->sci, SCI_TAB);
2183 break;
2184 default:
2185 break;
2187 break;
2189 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION:
2190 return editor_complete_word_part(doc->editor);
2192 case GEANY_KEYS_EDITOR_MOVELINEUP:
2193 move_lines(doc->editor, FALSE);
2194 break;
2195 case GEANY_KEYS_EDITOR_MOVELINEDOWN:
2196 move_lines(doc->editor, TRUE);
2197 break;
2199 return TRUE;
2203 static void join_lines(GeanyEditor *editor)
2205 gint start, end, i;
2207 start = sci_get_line_from_position(editor->sci,
2208 sci_get_selection_start(editor->sci));
2209 end = sci_get_line_from_position(editor->sci,
2210 sci_get_selection_end(editor->sci));
2212 /* if there is only one line in selection, join it with the following one */
2213 if (end == start)
2214 end = start + 1;
2217 * remove trailing spaces for every line except the last one
2218 * so that these spaces won't appear within text after joining
2220 for (i = start; i < end; i++)
2221 editor_strip_line_trailing_spaces(editor, i);
2223 /* remove starting spaces from second and following lines due to the same reason */
2224 for (i = start + 1; i <= end; i++)
2225 sci_set_line_indentation(editor->sci, i, 0);
2228 * SCI_LINESJOIN automatically adds spaces between joined lines, including
2229 * empty ones. We should drop empty lines if we want only one space to be
2230 * inserted (see also example below). I don't think we should care of that.
2233 sci_set_target_start(editor->sci,
2234 sci_get_position_from_line(editor->sci, start));
2235 sci_set_target_end(editor->sci,
2236 sci_get_position_from_line(editor->sci, end));
2237 sci_lines_join(editor->sci);
2240 * Example: joining
2242 * [TAB]if (something_wrong)
2243 * [TAB]{
2244 * [TAB][TAB]
2245 * [TAB][TAB]exit(1);[SPACE][SPACE]
2246 * [TAB]}[SPACE]
2248 * gives
2250 * [TAB]if (something_wrong) { exit(1); }[SPACE]
2255 static void split_lines(GeanyEditor *editor, gint column)
2257 gint start, indent, linescount, i, end;
2258 gchar c;
2259 ScintillaObject *sci = editor->sci;
2261 /* don't include trailing newlines */
2262 end = sci_get_selection_end(sci);
2263 while ((c = sci_get_char_at(sci, end - 1)) == '\n' || c == '\r') end--;
2264 sci_set_selection_end(sci, end);
2266 start = sci_get_line_from_position(editor->sci,
2267 sci_get_selection_start(editor->sci));
2270 * If several lines are selected, first join them.
2271 * This allows to reformat text paragraphs easily.
2273 if (sci_get_lines_selected(editor->sci) > 1)
2274 join_lines(editor);
2277 * If this line is short enough, just return
2279 if (column > sci_get_line_end_position(editor->sci, start) -
2280 sci_get_position_from_line(editor->sci, start))
2282 return;
2286 * We have to manipulate line indentation so that indentation
2287 * of the resulting lines would be consistent. For example,
2288 * the result of splitting "[TAB]very long content":
2290 * +-------------+-------------+
2291 * | proper | wrong |
2292 * +-------------+-------------+
2293 * | [TAB]very | [TAB]very |
2294 * | [TAB]long | long |
2295 * | [TAB]content| content |
2296 * +-------------+-------------+
2298 indent = sci_get_line_indentation(editor->sci, start);
2299 sci_set_line_indentation(editor->sci, start, 0);
2302 * Use sci_get_line_count() to determine how many new lines
2303 * appeared during splitting. SCI_LINESSPLIT should better return
2304 * this value itself...
2306 sci_target_from_selection(editor->sci);
2307 linescount = sci_get_line_count(editor->sci);
2308 sci_lines_split(editor->sci,
2309 (column - indent) * sci_text_width(editor->sci, STYLE_DEFAULT, " "));
2310 linescount = sci_get_line_count(editor->sci) - linescount;
2312 /* Fix indentation. */
2313 for (i = start; i <= start + linescount; i++)
2314 sci_set_line_indentation(editor->sci, i, indent);
2316 /* Remove trailing spaces. */
2317 if (editor_prefs.newline_strip || file_prefs.strip_trailing_spaces)
2319 for (i = start; i <= start + linescount; i++)
2320 editor_strip_line_trailing_spaces(editor, i);
2325 /* if cursor < anchor, swap them */
2326 static void sci_fix_selection(ScintillaObject *sci)
2328 gint start, end;
2330 start = sci_get_selection_start(sci);
2331 end = sci_get_selection_end(sci);
2332 sci_set_selection(sci, start, end);
2336 static void reflow_paragraph(GeanyEditor *editor)
2338 ScintillaObject *sci = editor->sci;
2339 gboolean sel;
2340 gint column = -1;
2342 if (editor->line_breaking)
2344 /* use line break column if enabled */
2345 column = editor_prefs.line_break_column;
2347 else if (editor_get_long_line_type() != 2)
2349 /* use long line if enabled */
2350 column = editor_get_long_line_column();
2352 else
2354 /* do nothing if no column is defined */
2355 utils_beep();
2356 return;
2358 sci_start_undo_action(sci);
2359 sel = sci_has_selection(sci);
2360 if (!sel)
2362 gint line, pos;
2364 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
2365 /* deselect last line break */
2366 pos = sci_get_selection_end(sci);
2367 line = sci_get_line_from_position(sci, pos);
2368 if (line < sci_get_line_count(sci) - 1)
2370 /* not last line */
2371 pos = sci_get_line_end_position(sci, line - 1);
2372 sci_set_selection_end(sci, pos);
2375 sci_fix_selection(sci);
2376 split_lines(editor, column);
2377 if (!sel)
2378 sci_set_anchor(sci, -1);
2380 sci_end_undo_action(sci);
2384 /* common function for format keybindings, only valid when scintilla has focus. */
2385 static gboolean cb_func_format_action(guint key_id)
2387 GeanyDocument *doc = document_get_current();
2388 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2390 /* keybindings only valid when scintilla widget has focus */
2391 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2392 return TRUE;
2394 switch (key_id)
2396 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE:
2397 on_menu_toggle_line_commentation1_activate(NULL, NULL);
2398 break;
2399 case GEANY_KEYS_FORMAT_COMMENTLINE:
2400 on_menu_comment_line1_activate(NULL, NULL);
2401 break;
2402 case GEANY_KEYS_FORMAT_UNCOMMENTLINE:
2403 on_menu_uncomment_line1_activate(NULL, NULL);
2404 break;
2405 case GEANY_KEYS_FORMAT_INCREASEINDENT:
2406 on_menu_increase_indent1_activate(NULL, NULL);
2407 break;
2408 case GEANY_KEYS_FORMAT_DECREASEINDENT:
2409 on_menu_decrease_indent1_activate(NULL, NULL);
2410 break;
2411 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE:
2412 editor_indentation_by_one_space(doc->editor, -1, FALSE);
2413 break;
2414 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE:
2415 editor_indentation_by_one_space(doc->editor, -1, TRUE);
2416 break;
2417 case GEANY_KEYS_FORMAT_AUTOINDENT:
2418 editor_smart_line_indentation(doc->editor, -1);
2419 break;
2420 case GEANY_KEYS_FORMAT_TOGGLECASE:
2421 on_toggle_case1_activate(NULL, NULL);
2422 break;
2423 case GEANY_KEYS_FORMAT_SENDTOCMD1:
2424 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 0)
2425 tools_execute_custom_command(doc, ui_prefs.custom_commands[0]);
2426 break;
2427 case GEANY_KEYS_FORMAT_SENDTOCMD2:
2428 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 1)
2429 tools_execute_custom_command(doc, ui_prefs.custom_commands[1]);
2430 break;
2431 case GEANY_KEYS_FORMAT_SENDTOCMD3:
2432 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 2)
2433 tools_execute_custom_command(doc, ui_prefs.custom_commands[2]);
2434 break;
2435 case GEANY_KEYS_FORMAT_SENDTOVTE:
2436 on_send_selection_to_vte1_activate(NULL, NULL);
2437 break;
2438 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
2439 reflow_paragraph(doc->editor);
2440 break;
2442 return TRUE;
2446 /* common function for select keybindings, only valid when scintilla has focus. */
2447 static gboolean cb_func_select_action(guint key_id)
2449 GeanyDocument *doc;
2450 ScintillaObject *sci;
2451 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2452 GtkWidget *toolbar_search_entry = toolbar_get_widget_child_by_name("SearchEntry");
2453 GtkWidget *toolbar_goto_entry = toolbar_get_widget_child_by_name("GotoEntry");
2455 /* special case for Select All in the scribble widget */
2456 if (key_id == GEANY_KEYS_SELECT_ALL && focusw == msgwindow.scribble)
2458 g_signal_emit_by_name(msgwindow.scribble, "select-all", TRUE);
2459 return TRUE;
2461 /* special case for Select All in the toolbar search widget */
2462 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_search_entry)
2464 gtk_editable_select_region(GTK_EDITABLE(toolbar_search_entry), 0, -1);
2465 return TRUE;
2467 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_goto_entry)
2469 gtk_editable_select_region(GTK_EDITABLE(toolbar_goto_entry), 0, -1);
2470 return TRUE;
2473 doc = document_get_current();
2474 /* keybindings only valid when scintilla widget has focus */
2475 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2476 return TRUE;
2477 sci = doc->editor->sci;
2479 switch (key_id)
2481 case GEANY_KEYS_SELECT_ALL:
2482 on_menu_select_all1_activate(NULL, NULL);
2483 break;
2484 case GEANY_KEYS_SELECT_WORD:
2485 editor_select_word(doc->editor);
2486 break;
2487 case GEANY_KEYS_SELECT_LINE:
2488 editor_select_lines(doc->editor, FALSE);
2489 break;
2490 case GEANY_KEYS_SELECT_PARAGRAPH:
2491 editor_select_paragraph(doc->editor);
2492 break;
2493 case GEANY_KEYS_SELECT_WORDPARTLEFT:
2494 sci_send_command(sci, SCI_WORDPARTLEFTEXTEND);
2495 break;
2496 case GEANY_KEYS_SELECT_WORDPARTRIGHT:
2497 sci_send_command(sci, SCI_WORDPARTRIGHTEXTEND);
2498 break;
2500 return TRUE;
2504 static gboolean cb_func_document_action(guint key_id)
2506 GeanyDocument *doc = document_get_current();
2508 if (doc == NULL)
2509 return TRUE;
2511 switch (key_id)
2513 case GEANY_KEYS_DOCUMENT_REPLACETABS:
2514 on_replace_tabs_activate(NULL, NULL);
2515 break;
2516 case GEANY_KEYS_DOCUMENT_REPLACESPACES:
2517 on_replace_spaces_activate(NULL, NULL);
2518 break;
2519 case GEANY_KEYS_DOCUMENT_LINEBREAK:
2520 on_line_breaking1_activate(NULL, NULL);
2521 ui_document_show_hide(doc);
2522 break;
2523 case GEANY_KEYS_DOCUMENT_LINEWRAP:
2524 on_line_wrapping1_toggled(NULL, NULL);
2525 ui_document_show_hide(doc);
2526 break;
2527 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST:
2528 document_update_tag_list(doc, TRUE);
2529 break;
2530 case GEANY_KEYS_DOCUMENT_FOLDALL:
2531 editor_fold_all(doc->editor);
2532 break;
2533 case GEANY_KEYS_DOCUMENT_UNFOLDALL:
2534 editor_unfold_all(doc->editor);
2535 break;
2536 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD:
2537 if (editor_prefs.folding)
2539 gint line = sci_get_current_line(doc->editor->sci);
2540 editor_toggle_fold(doc->editor, line, 0);
2541 break;
2544 return TRUE;
2548 /* common function for insert keybindings, only valid when scintilla has focus. */
2549 static gboolean cb_func_insert_action(guint key_id)
2551 GeanyDocument *doc = document_get_current();
2552 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2554 /* keybindings only valid when scintilla widget has focus */
2555 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2556 return TRUE;
2558 switch (key_id)
2560 case GEANY_KEYS_INSERT_ALTWHITESPACE:
2561 editor_insert_alternative_whitespace(doc->editor);
2562 break;
2563 case GEANY_KEYS_INSERT_DATE:
2564 gtk_menu_item_activate(GTK_MENU_ITEM(
2565 ui_lookup_widget(main_widgets.window, "insert_date_custom1")));
2566 break;
2568 return TRUE;
2572 /* update key combination */
2573 void keybindings_update_combo(GeanyKeyBinding *kb, guint key, GdkModifierType mods)
2575 GtkWidget *widget = kb->menu_item;
2577 if (widget && kb->key)
2578 gtk_widget_remove_accelerator(widget, kb_accel_group, kb->key, kb->mods);
2580 kb->key = key;
2581 kb->mods = mods;
2583 if (widget && kb->key)
2584 gtk_widget_add_accelerator(widget, "activate", kb_accel_group,
2585 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
2589 /* used for plugins */
2590 GeanyKeyGroup *keybindings_set_group(GeanyKeyGroup *group, const gchar *section_name,
2591 const gchar *label, gsize count, GeanyKeyGroupCallback callback)
2593 g_return_val_if_fail(section_name, NULL);
2594 g_return_val_if_fail(count, NULL);
2596 /* prevent conflict with core bindings */
2597 g_return_val_if_fail(!g_str_equal(section_name, keybindings_keyfile_group_name), NULL);
2599 if (!group)
2600 group = g_new0(GeanyKeyGroup, 1);
2602 if (!group->keys || count > group->count)
2604 /* allow resizing existing array of keys */
2605 group->keys = g_renew(GeanyKeyBinding, group->keys, count);
2606 memset(group->keys + group->count, 0, (count - group->count) * sizeof(GeanyKeyBinding));
2608 group->plugin = TRUE;
2609 add_kb_group(group, section_name, label, count, group->keys, callback);
2610 return group;
2614 /* used for plugins */
2615 void keybindings_free_group(GeanyKeyGroup *group)
2617 GeanyKeyBinding *kb;
2619 g_assert(group->plugin);
2621 foreach_c_array(kb, group->keys, group->count)
2623 g_free(kb->name);
2624 g_free(kb->label);
2626 g_free(group->keys);
2627 g_ptr_array_remove_fast(keybinding_groups, group);
2628 g_free(group);