r5162 | eht16 | 2010-08-15 13:53:09 +0100 (Sun, 15 Aug 2010) | 1 line
[geany-mirror.git] / src / keybindings.c
blob4fe0fadc9171f4a1f572561ef60d13d4f7c44426
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 const gchar *kf_name, const 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 /* we don't touch them unless group->plugin is set, cast is safe */
160 kb->name = (gchar *)kf_name;
161 kb->label = (gchar *)label;
163 kb->key = key;
164 kb->mods = mod;
165 kb->callback = callback;
166 kb->menu_item = menu_item;
167 return kb;
171 static GeanyKeyGroup *add_kb_group(GeanyKeyGroup *group,
172 const gchar *name, const gchar *label, gsize count, GeanyKeyBinding *keys,
173 GeanyKeyGroupCallback callback)
175 g_ptr_array_add(keybinding_groups, group);
177 group->name = name;
178 group->label = label;
179 group->count = count;
180 group->keys = keys;
181 group->callback = callback;
182 return group;
186 /* Lookup a widget in the main window */
187 #define LW(widget_name) \
188 ui_lookup_widget(main_widgets.window, G_STRINGIFY(widget_name))
190 /* Expansion for group_id = FILE:
191 * static GeanyKeyBinding FILE_keys[GEANY_KEYS_FILE_COUNT]; */
192 #define DECLARE_KEYS(group_id) \
193 static GeanyKeyBinding group_id ## _keys[GEANY_KEYS_ ## group_id ## _COUNT]
195 /* Expansion for group_id = FILE:
196 * add_kb_group(&groups[GEANY_KEY_GROUP_FILE], NULL, _("File menu"),
197 * GEANY_KEYS_FILE_COUNT, FILE_keys, callback); */
198 #define ADD_KB_GROUP(group_id, label, callback) \
199 add_kb_group(&groups[GEANY_KEY_GROUP_ ## group_id], keybindings_keyfile_group_name, label, \
200 GEANY_KEYS_ ## group_id ## _COUNT, group_id ## _keys, callback)
202 /* Init all fields of keys with default values.
203 * The menu_item field is always the main menu item, popup menu accelerators are
204 * set in add_popup_menu_accels(). */
205 static void init_default_kb(void)
207 static GeanyKeyGroup groups[GEANY_KEY_GROUP_COUNT];
208 GeanyKeyGroup *group;
209 DECLARE_KEYS(FILE);
210 DECLARE_KEYS(PROJECT);
211 DECLARE_KEYS(EDITOR);
212 DECLARE_KEYS(CLIPBOARD);
213 DECLARE_KEYS(SELECT);
214 DECLARE_KEYS(FORMAT);
215 DECLARE_KEYS(INSERT);
216 DECLARE_KEYS(SETTINGS);
217 DECLARE_KEYS(SEARCH);
218 DECLARE_KEYS(GOTO);
219 DECLARE_KEYS(VIEW);
220 DECLARE_KEYS(FOCUS);
221 DECLARE_KEYS(NOTEBOOK);
222 DECLARE_KEYS(DOCUMENT);
223 DECLARE_KEYS(BUILD);
224 DECLARE_KEYS(TOOLS);
225 DECLARE_KEYS(HELP);
227 group = ADD_KB_GROUP(FILE, _("File"), cb_func_file_action);
229 keybindings_set_item(group, GEANY_KEYS_FILE_NEW, NULL,
230 GDK_n, GDK_CONTROL_MASK, "menu_new", _("New"), NULL);
231 keybindings_set_item(group, GEANY_KEYS_FILE_OPEN, NULL,
232 GDK_o, GDK_CONTROL_MASK, "menu_open", _("Open"), NULL);
233 keybindings_set_item(group, GEANY_KEYS_FILE_OPENSELECTED, NULL,
234 GDK_o, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_open_selected",
235 _("Open selected file"), LW(menu_open_selected_file1));
236 keybindings_set_item(group, GEANY_KEYS_FILE_SAVE, NULL,
237 GDK_s, GDK_CONTROL_MASK, "menu_save", _("Save"), NULL);
238 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEAS, NULL,
239 0, 0, "menu_saveas", _("Save as"), LW(menu_save_as1));
240 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEALL, NULL,
241 GDK_S, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_saveall", _("Save all"),
242 LW(menu_save_all1));
243 keybindings_set_item(group, GEANY_KEYS_FILE_PRINT, NULL,
244 GDK_p, GDK_CONTROL_MASK, "menu_print", _("Print"), LW(print1));
245 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSE, NULL,
246 GDK_w, GDK_CONTROL_MASK, "menu_close", _("Close"), LW(menu_close1));
247 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSEALL, NULL,
248 GDK_w, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_closeall", _("Close all"),
249 LW(menu_close_all1));
250 keybindings_set_item(group, GEANY_KEYS_FILE_RELOAD, NULL,
251 GDK_r, GDK_CONTROL_MASK, "menu_reloadfile", _("Reload file"), LW(menu_reload1));
252 keybindings_set_item(group, GEANY_KEYS_FILE_OPENLASTTAB, NULL,
253 0, 0, "file_openlasttab", _("Re-open last closed tab"), NULL);
255 group = ADD_KB_GROUP(PROJECT, _("Project"), cb_func_project_action);
257 keybindings_set_item(group, GEANY_KEYS_PROJECT_PROPERTIES, NULL,
258 0, 0, "project_properties", _("Project properties"), LW(project_properties1));
260 group = ADD_KB_GROUP(EDITOR, _("Editor"), cb_func_editor_action);
262 keybindings_set_item(group, GEANY_KEYS_EDITOR_UNDO, NULL,
263 GDK_z, GDK_CONTROL_MASK, "menu_undo", _("Undo"), LW(menu_undo2));
264 keybindings_set_item(group, GEANY_KEYS_EDITOR_REDO, NULL,
265 GDK_y, GDK_CONTROL_MASK, "menu_redo", _("Redo"), LW(menu_redo2));
266 keybindings_set_item(group, GEANY_KEYS_EDITOR_DUPLICATELINE, NULL,
267 GDK_d, GDK_CONTROL_MASK, "edit_duplicateline", _("_Duplicate Line or Selection"),
268 LW(duplicate_line_or_selection1));
269 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINE, NULL,
270 GDK_k, GDK_CONTROL_MASK, "edit_deleteline", _("_Delete Current Line(s)"),
271 LW(delete_current_line_s_1));
272 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINETOEND, NULL,
273 GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_deletelinetoend",
274 _("Delete to line end"), NULL);
275 /* transpose may fit better in format group */
276 keybindings_set_item(group, GEANY_KEYS_EDITOR_TRANSPOSELINE, NULL,
277 GDK_t, GDK_CONTROL_MASK, "edit_transposeline", _("_Transpose Current Line"),
278 LW(transpose_current_line1));
279 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLTOLINE, NULL,
280 GDK_l, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_scrolltoline", _("Scroll to current line"), NULL);
281 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEUP, NULL,
282 GDK_Up, GDK_MOD1_MASK, "edit_scrolllineup", _("Scroll up the view by one line"), NULL);
283 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEDOWN, NULL,
284 GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
285 keybindings_set_item(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL,
286 GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
287 keybindings_set_item(group, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR, NULL,
288 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
289 keybindings_set_item(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, NULL,
290 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
291 keybindings_set_item(group, GEANY_KEYS_EDITOR_CONTEXTACTION, NULL,
292 0, 0, "popup_contextaction", _("Context Action"), NULL);
293 keybindings_set_item(group, GEANY_KEYS_EDITOR_AUTOCOMPLETE, NULL,
294 GDK_space, GDK_CONTROL_MASK, "edit_autocomplete", _("Complete word"), NULL);
295 keybindings_set_item(group, GEANY_KEYS_EDITOR_CALLTIP, NULL,
296 GDK_space, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_calltip", _("Show calltip"), NULL);
297 keybindings_set_item(group, GEANY_KEYS_EDITOR_MACROLIST, NULL,
298 GDK_Return, GDK_CONTROL_MASK, "edit_macrolist", _("Show macro list"), NULL);
299 keybindings_set_item(group, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION, NULL,
300 GDK_Tab, 0, "edit_wordpartcompletion", _("Word part completion"), NULL);
301 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEUP, NULL,
302 0, 0, "edit_movelineup", _("Move line(s) up"), NULL);
303 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEDOWN, NULL,
304 0, 0, "edit_movelinedown", _("Move line(s) down"), NULL);
306 group = ADD_KB_GROUP(CLIPBOARD, _("Clipboard"), cb_func_clipboard_action);
308 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUT, NULL,
309 GDK_x, GDK_CONTROL_MASK, "menu_cut", _("Cut"), NULL);
310 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPY, NULL,
311 GDK_c, GDK_CONTROL_MASK, "menu_copy", _("Copy"), NULL);
312 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_PASTE, NULL,
313 GDK_v, GDK_CONTROL_MASK, "menu_paste", _("Paste"), NULL);
314 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPYLINE, NULL,
315 GDK_c, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_copyline", _("_Copy Current Line(s)"),
316 LW(copy_current_line_s_1));
317 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUTLINE, NULL,
318 GDK_x, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_cutline", _("_Cut Current Line(s)"),
319 LW(cut_current_line_s_1));
321 group = ADD_KB_GROUP(SELECT, _("Select"), cb_func_select_action);
323 keybindings_set_item(group, GEANY_KEYS_SELECT_ALL, NULL,
324 GDK_a, GDK_CONTROL_MASK, "menu_selectall", _("Select All"), LW(menu_select_all1));
325 keybindings_set_item(group, GEANY_KEYS_SELECT_WORD, NULL,
326 GDK_w, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectword", _("Select current word"), NULL);
327 keybindings_set_item(group, GEANY_KEYS_SELECT_LINE, NULL,
328 GDK_l, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectline", _("_Select Current Line(s)"),
329 LW(select_current_line_s_1));
330 keybindings_set_item(group, GEANY_KEYS_SELECT_PARAGRAPH, NULL,
331 GDK_p, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectparagraph", _("_Select Current Paragraph"),
332 LW(select_current_paragraph1));
333 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTLEFT, NULL,
334 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL);
335 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTRIGHT, NULL,
336 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL);
338 group = ADD_KB_GROUP(FORMAT, _("Format"), cb_func_format_action);
340 keybindings_set_item(group, GEANY_KEYS_FORMAT_TOGGLECASE, NULL,
341 GDK_u, GDK_CONTROL_MASK | GDK_MOD1_MASK, "edit_togglecase",
342 _("Toggle Case of Selection"), LW(menu_toggle_case2));
343 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE, NULL,
344 GDK_e, GDK_CONTROL_MASK, "edit_commentlinetoggle", _("Toggle line commentation"),
345 LW(menu_toggle_line_commentation1));
346 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINE, NULL,
347 0, 0, "edit_commentline", _("Comment line(s)"), LW(menu_comment_line1));
348 keybindings_set_item(group, GEANY_KEYS_FORMAT_UNCOMMENTLINE, NULL,
349 0, 0, "edit_uncommentline", _("Uncomment line(s)"), LW(menu_uncomment_line1));
350 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENT, NULL,
351 GDK_i, GDK_CONTROL_MASK, "edit_increaseindent", _("Increase indent"),
352 LW(menu_increase_indent1));
353 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENT, NULL,
354 GDK_u, GDK_CONTROL_MASK, "edit_decreaseindent", _("Decrease indent"),
355 LW(menu_decrease_indent1));
356 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE, NULL,
357 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL);
358 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE, NULL,
359 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL);
360 keybindings_set_item(group, GEANY_KEYS_FORMAT_AUTOINDENT, NULL,
361 0, 0, "edit_autoindent", _("_Smart Line Indent"), LW(smart_line_indent1));
362 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD1, NULL,
363 GDK_1, GDK_CONTROL_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL);
364 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD2, NULL,
365 GDK_2, GDK_CONTROL_MASK, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL);
366 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD3, NULL,
367 GDK_3, GDK_CONTROL_MASK, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL);
368 /* may fit better in editor group */
369 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOVTE, NULL,
370 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), LW(send_selection_to_vte1));
371 keybindings_set_item(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
372 GDK_j, GDK_CONTROL_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
373 LW(reflow_lines_block1));
375 group = ADD_KB_GROUP(INSERT, _("Insert"), cb_func_insert_action);
377 keybindings_set_item(group, GEANY_KEYS_INSERT_DATE, NULL,
378 GDK_d, GDK_SHIFT_MASK | GDK_MOD1_MASK, "menu_insert_date", _("Insert date"),
379 LW(insert_date_custom1));
380 keybindings_set_item(group, GEANY_KEYS_INSERT_ALTWHITESPACE, NULL,
381 0, 0, "edit_insertwhitespace", _("_Insert Alternative White Space"),
382 LW(insert_alternative_white_space1));
384 group = ADD_KB_GROUP(SETTINGS, _("Settings"), NULL);
386 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PREFERENCES, cb_func_menu_preferences,
387 GDK_p, GDK_CONTROL_MASK | GDK_MOD1_MASK, "menu_preferences", _("Preferences"),
388 LW(preferences1));
389 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES, cb_func_menu_preferences,
390 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), LW(plugin_preferences1));
392 group = ADD_KB_GROUP(SEARCH, _("Search"), cb_func_search_action);
394 keybindings_set_item(group, GEANY_KEYS_SEARCH_FIND, NULL,
395 GDK_f, GDK_CONTROL_MASK, "menu_find", _("Find"), LW(find1));
396 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXT, NULL,
397 GDK_g, GDK_CONTROL_MASK, "menu_findnext", _("Find Next"), LW(find_next1));
398 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVIOUS, NULL,
399 GDK_g, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findprevious", _("Find Previous"),
400 LW(find_previous1));
401 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXTSEL, NULL,
402 0, 0, "menu_findnextsel", _("Find Next Selection"), LW(find_nextsel1));
403 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVSEL, NULL,
404 0, 0, "menu_findprevsel", _("Find Previous Selection"), LW(find_prevsel1));
405 keybindings_set_item(group, GEANY_KEYS_SEARCH_REPLACE, NULL,
406 GDK_h, GDK_CONTROL_MASK, "menu_replace", _("Replace"), LW(replace1));
407 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDINFILES, NULL, GDK_f,
408 GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findinfiles", _("Find in Files"),
409 LW(find_in_files1));
410 keybindings_set_item(group, GEANY_KEYS_SEARCH_NEXTMESSAGE, NULL,
411 0, 0, "menu_nextmessage", _("Next Message"), LW(next_message1));
412 keybindings_set_item(group, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE, NULL,
413 0, 0, "menu_previousmessage", _("Previous Message"), LW(previous_message1));
414 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDUSAGE, NULL,
415 0, 0, "popup_findusage", _("Find Usage"), NULL);
416 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, NULL,
417 0, 0, "popup_finddocumentusage", _("Find Document Usage"), NULL);
418 keybindings_set_item(group, GEANY_KEYS_SEARCH_MARKALL, NULL,
419 GDK_m, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "find_markall", _("Mark All"), NULL);
421 group = ADD_KB_GROUP(GOTO, _("Go to"), cb_func_goto_action);
423 keybindings_set_item(group, GEANY_KEYS_GOTO_BACK, NULL,
424 0, 0, "nav_back", _("Navigate back a location"), NULL);
425 keybindings_set_item(group, GEANY_KEYS_GOTO_FORWARD, NULL,
426 0, 0, "nav_forward", _("Navigate forward a location"), NULL);
427 keybindings_set_item(group, GEANY_KEYS_GOTO_LINE, NULL,
428 GDK_l, GDK_CONTROL_MASK, "menu_gotoline", _("Go to Line"), LW(go_to_line1));
429 keybindings_set_item(group, GEANY_KEYS_GOTO_MATCHINGBRACE, NULL,
430 GDK_b, GDK_CONTROL_MASK, "edit_gotomatchingbrace",
431 _("Go to matching brace"), NULL);
432 keybindings_set_item(group, GEANY_KEYS_GOTO_TOGGLEMARKER, NULL,
433 GDK_m, GDK_CONTROL_MASK, "edit_togglemarker",
434 _("Toggle marker"), NULL);
435 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTMARKER, NULL,
436 GDK_period, GDK_CONTROL_MASK, "edit_gotonextmarker",
437 _("_Go to Next Marker"), LW(go_to_next_marker1));
438 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVIOUSMARKER, NULL,
439 GDK_comma, GDK_CONTROL_MASK, "edit_gotopreviousmarker",
440 _("_Go to Previous Marker"), LW(go_to_previous_marker1));
441 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDEFINITION, NULL,
442 0, 0, "popup_gototagdefinition", _("Go to Tag Definition"), NULL);
443 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDECLARATION, NULL,
444 0, 0, "popup_gototagdeclaration", _("Go to Tag Declaration"), NULL);
445 keybindings_set_item(group, GEANY_KEYS_GOTO_LINESTART, NULL,
446 GDK_Home, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL);
447 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEEND, NULL,
448 GDK_End, 0, "edit_gotolineend", _("Go to End of Line"), NULL);
449 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEENDVISUAL, NULL,
450 GDK_End, GDK_MOD1_MASK, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL);
451 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVWORDPART, NULL,
452 GDK_slash, GDK_CONTROL_MASK, "edit_prevwordstart", _("Go to Previous Word Part"), NULL);
453 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTWORDPART, NULL,
454 GDK_backslash, GDK_CONTROL_MASK, "edit_nextwordstart", _("Go to Next Word Part"), NULL);
456 group = ADD_KB_GROUP(VIEW, _("View"), cb_func_view_action);
458 keybindings_set_item(group, GEANY_KEYS_VIEW_TOGGLEALL, NULL,
459 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
460 LW(menu_toggle_all_additional_widgets1));
461 keybindings_set_item(group, GEANY_KEYS_VIEW_FULLSCREEN, cb_func_menu_fullscreen,
462 GDK_F11, 0, "menu_fullscreen", _("Fullscreen"), LW(menu_fullscreen1));
463 keybindings_set_item(group, GEANY_KEYS_VIEW_MESSAGEWINDOW, cb_func_menu_messagewindow,
464 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
465 LW(menu_show_messages_window1));
466 keybindings_set_item(group, GEANY_KEYS_VIEW_SIDEBAR, NULL,
467 0, 0, "toggle_sidebar", _("Toggle Sidebar"), LW(menu_show_sidebar1));
468 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMIN, NULL,
469 GDK_plus, GDK_CONTROL_MASK, "menu_zoomin", _("Zoom In"), LW(menu_zoom_in1));
470 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMOUT, NULL,
471 GDK_minus, GDK_CONTROL_MASK, "menu_zoomout", _("Zoom Out"), LW(menu_zoom_out1));
472 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMRESET, NULL,
473 GDK_0, GDK_CONTROL_MASK, "normal_size", _("Zoom Reset"), LW(normal_size1));
475 group = ADD_KB_GROUP(FOCUS, _("Focus"), cb_func_switch_action);
477 keybindings_set_item(group, GEANY_KEYS_FOCUS_EDITOR, NULL,
478 GDK_F2, 0, "switch_editor", _("Switch to Editor"), NULL);
479 keybindings_set_item(group, GEANY_KEYS_FOCUS_SCRIBBLE, NULL,
480 GDK_F6, 0, "switch_scribble", _("Switch to Scribble"), NULL);
481 keybindings_set_item(group, GEANY_KEYS_FOCUS_VTE, NULL,
482 GDK_F4, 0, "switch_vte", _("Switch to VTE"), NULL);
483 keybindings_set_item(group, GEANY_KEYS_FOCUS_SEARCHBAR, NULL,
484 GDK_F7, 0, "switch_search_bar", _("Switch to Search Bar"), NULL);
485 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR, NULL,
486 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL);
487 keybindings_set_item(group, GEANY_KEYS_FOCUS_COMPILER, NULL,
488 0, 0, "switch_compiler", _("Switch to Compiler"), NULL);
489 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGES, NULL,
490 0, 0, "switch_messages", _("Switch to Messages"), NULL);
491 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGE_WINDOW, NULL,
492 0, 0, "switch_message_window", _("Switch to Message Window"), NULL);
493 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST, NULL,
494 0, 0, "switch_sidebar_doc_list", _("Switch to Sidebar Document List"), NULL);
495 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST, NULL,
496 0, 0, "switch_sidebar_symbol_list", _("Switch to Sidebar Symbol List"), NULL);
498 group = ADD_KB_GROUP(NOTEBOOK, _("Notebook tab"), NULL);
500 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT, cb_func_switch_tableft,
501 GDK_Page_Up, GDK_CONTROL_MASK, "switch_tableft", _("Switch to left document"), NULL);
502 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
503 GDK_Page_Down, GDK_CONTROL_MASK, "switch_tabright", _("Switch to right document"), NULL);
504 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
505 GDK_Tab, GDK_CONTROL_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
506 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
507 GDK_Page_Up, GDK_MOD1_MASK, "move_tableft", _("Move document left"), NULL);
508 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
509 GDK_Page_Down, GDK_MOD1_MASK, "move_tabright", _("Move document right"), NULL);
510 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABFIRST, cb_func_move_tab,
511 0, 0, "move_tabfirst", _("Move document first"), NULL);
512 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLAST, cb_func_move_tab,
513 0, 0, "move_tablast", _("Move document last"), NULL);
515 group = ADD_KB_GROUP(DOCUMENT, _("Document"), cb_func_document_action);
517 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEWRAP, NULL,
518 0, 0, "menu_linewrap", _("Toggle Line wrapping"), LW(menu_line_wrapping1));
519 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEBREAK, NULL,
520 0, 0, "menu_linebreak", _("Toggle Line breaking"), LW(line_breaking1));
521 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACETABS, NULL,
522 0, 0, "menu_replacetabs", _("Replace tabs by space"), LW(menu_replace_tabs));
523 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACESPACES, NULL,
524 0, 0, "menu_replacespaces", _("Replace spaces by tabs"), LW(menu_replace_spaces));
525 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_TOGGLEFOLD, NULL,
526 0, 0, "menu_togglefold", _("Toggle current fold"), NULL);
527 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_FOLDALL, NULL,
528 0, 0, "menu_foldall", _("Fold all"), LW(menu_fold_all1));
529 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_UNFOLDALL, NULL,
530 0, 0, "menu_unfoldall", _("Unfold all"), LW(menu_unfold_all1));
531 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_RELOADTAGLIST, NULL,
532 GDK_r, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "reloadtaglist", _("Reload symbol list"), NULL);
533 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS, NULL,
534 0, 0, "remove_markers", _("Remove Markers"), LW(remove_markers1));
535 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS, NULL,
536 0, 0, "remove_error_indicators", _("Remove Error Indicators"), LW(menu_remove_indicators1));
538 group = ADD_KB_GROUP(BUILD, _("Build"), cb_func_build_action);
540 keybindings_set_item(group, GEANY_KEYS_BUILD_COMPILE, NULL,
541 GDK_F8, 0, "build_compile", _("Compile"), NULL);
542 keybindings_set_item(group, GEANY_KEYS_BUILD_LINK, NULL,
543 GDK_F9, 0, "build_link", _("Build"), NULL);
544 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKE, NULL,
545 GDK_F9, GDK_SHIFT_MASK, "build_make", _("Make all"), NULL);
546 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOWNTARGET, NULL,
547 GDK_F9, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "build_makeowntarget",
548 _("Make custom target"), NULL);
549 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOBJECT, NULL,
550 0, 0, "build_makeobject", _("Make object"), NULL);
551 keybindings_set_item(group, GEANY_KEYS_BUILD_NEXTERROR, NULL,
552 0, 0, "build_nexterror", _("Next error"), NULL);
553 keybindings_set_item(group, GEANY_KEYS_BUILD_PREVIOUSERROR, NULL,
554 0, 0, "build_previouserror", _("Previous error"), NULL);
555 keybindings_set_item(group, GEANY_KEYS_BUILD_RUN, NULL,
556 GDK_F5, 0, "build_run", _("Run"), NULL);
557 keybindings_set_item(group, GEANY_KEYS_BUILD_OPTIONS, NULL,
558 0, 0, "build_options", _("Build options"), NULL);
560 group = ADD_KB_GROUP(TOOLS, _("Tools"), NULL);
562 keybindings_set_item(group, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER, cb_func_menu_opencolorchooser,
563 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), LW(menu_choose_color1));
565 group = ADD_KB_GROUP(HELP, _("Help"), NULL);
567 keybindings_set_item(group, GEANY_KEYS_HELP_HELP, cb_func_menu_help,
568 GDK_F1, 0, "menu_help", _("Help"), LW(help1));
572 /* before the tab changes, add the current document to the MRU list */
573 static void on_notebook_switch_page(void)
575 GeanyDocument *old = document_get_current();
577 /* when closing current doc, old is NULL.
578 * Don't add to the mru list when switch dialog is visible. */
579 if (old && switch_dialog_cancelled)
581 g_queue_remove(mru_docs, old);
582 g_queue_push_head(mru_docs, old);
584 if (g_queue_get_length(mru_docs) > MAX_MRU_DOCS)
585 g_queue_pop_tail(mru_docs);
590 /* really this should be just after a document was closed, not idle */
591 static gboolean on_idle_close(gpointer data)
593 GeanyDocument *current;
595 current = document_get_current();
596 if (current && g_queue_peek_head(mru_docs) == current)
597 g_queue_pop_head(mru_docs);
599 return FALSE;
603 static void on_document_close(GObject *obj, GeanyDocument *doc)
605 if (! main_status.quitting)
607 g_queue_remove(mru_docs, doc);
608 g_idle_add(on_idle_close, NULL);
613 void keybindings_init(void)
615 mru_docs = g_queue_new();
616 g_signal_connect(main_widgets.notebook, "switch-page",
617 G_CALLBACK(on_notebook_switch_page), NULL);
618 g_signal_connect(geany_object, "document-close",
619 G_CALLBACK(on_document_close), NULL);
621 keybinding_groups = g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT);
623 kb_accel_group = gtk_accel_group_new();
625 init_default_kb();
627 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), kb_accel_group);
629 g_signal_connect(main_widgets.window, "key-press-event", G_CALLBACK(on_key_press_event), NULL);
630 /* in case the switch dialog misses an event while drawing the dialog */
631 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
635 typedef void (*KBItemCallback) (GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data);
637 static void keybindings_foreach(KBItemCallback cb, gpointer user_data)
639 gsize g, i;
640 GeanyKeyGroup *group;
641 GeanyKeyBinding *kb;
643 for (g = 0; g < keybinding_groups->len; g++)
645 group = g_ptr_array_index(keybinding_groups, g);
646 for (i = 0; i < group->count; i++)
648 kb = &group->keys[i];
650 cb(group, kb, user_data);
656 static void load_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
658 GKeyFile *config = user_data;
659 gchar *val;
660 guint key;
661 GdkModifierType mods;
663 val = g_key_file_get_string(config, group->name, kb->name, NULL);
664 if (val != NULL)
666 gtk_accelerator_parse(val, &key, &mods);
667 kb->key = key;
668 kb->mods = mods;
669 g_free(val);
674 static void load_user_kb(void)
676 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
677 GKeyFile *config = g_key_file_new();
679 /* now load user defined keys */
680 if (g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
682 keybindings_foreach(load_kb, config);
684 g_free(configfile);
685 g_key_file_free(config);
689 static void apply_kb_accel(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
691 if (kb->key != 0 && kb->menu_item)
693 gtk_widget_add_accelerator(kb->menu_item, "activate", kb_accel_group,
694 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
699 void keybindings_load_keyfile(void)
701 load_user_kb();
702 add_popup_menu_accels();
704 /* set menu accels now, after user keybindings have been read */
705 keybindings_foreach(apply_kb_accel, NULL);
709 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id, GtkWidget *menuitem)
711 GeanyKeyBinding *kb = &group->keys[kb_id];
713 if (kb->key != 0)
714 gtk_widget_add_accelerator(menuitem, "activate", kb_accel_group,
715 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
719 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
720 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
722 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
723 static void add_popup_menu_accels(void)
725 GeanyKeyGroup *group;
727 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_EDITOR);
728 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO, undo1);
729 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO, redo1);
730 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION, context_action1);
732 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_SELECT);
733 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL, menu_select_all2);
735 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_INSERT);
736 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE, insert_date_custom2);
738 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_FILE);
739 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED, menu_open_selected_file2);
741 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_SEARCH);
742 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE, find_usage1);
743 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, find_document_usage1);
745 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_GOTO);
746 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_LINE, go_to_line);
747 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION, goto_tag_definition1);
748 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDECLARATION, goto_tag_declaration1);
750 /* Format and Commands share the menu bar submenus */
751 /* Build menu items are set if the build menus are created */
755 static void set_keyfile_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
757 GKeyFile *config = user_data;
758 gchar *val;
760 val = gtk_accelerator_name(kb->key, kb->mods);
761 g_key_file_set_string(config, group->name, kb->name, val);
762 g_free(val);
766 /* just write the content of the keys array to the config file */
767 void keybindings_write_to_file(void)
769 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
770 gchar *data;
771 GKeyFile *config = g_key_file_new();
773 /* add comment if the file is newly created */
774 if (! g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
776 g_key_file_set_comment(config, NULL, NULL,
777 "Keybindings for Geany\nThe format looks like \"<Control>a\" or \"<Shift><Alt>F1\".\n"
778 "But you can also change the keys in Geany's preferences dialog.", NULL);
781 keybindings_foreach(set_keyfile_kb, config);
783 /* write the file */
784 data = g_key_file_to_data(config, NULL, NULL);
785 utils_write_file(configfile, data);
787 g_free(data);
788 g_free(configfile);
789 g_key_file_free(config);
793 void keybindings_free(void)
795 g_ptr_array_free(keybinding_groups, TRUE);
796 g_queue_free(mru_docs);
800 gchar *keybindings_get_label(GeanyKeyBinding *kb)
802 return utils_str_remove_chars(g_strdup(kb->label), "_");
806 static void fill_shortcut_labels_treeview(GtkWidget *tree)
808 gsize g, i;
809 GeanyKeyBinding *kb;
810 GeanyKeyGroup *group;
811 GtkListStore *store;
812 GtkTreeIter iter;
814 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, PANGO_TYPE_WEIGHT);
816 for (g = 0; g < keybinding_groups->len; g++)
818 group = g_ptr_array_index(keybinding_groups, g);
820 if (g > 0)
822 gtk_list_store_append(store, &iter);
823 gtk_list_store_set(store, &iter, -1);
826 gtk_list_store_append(store, &iter);
827 gtk_list_store_set(store, &iter, 0, group->label, 2, PANGO_WEIGHT_BOLD, -1);
829 for (i = 0; i < group->count; i++)
831 gchar *shortcut, *label;
833 kb = &group->keys[i];
834 label = keybindings_get_label(kb);
835 shortcut = gtk_accelerator_get_label(kb->key, kb->mods);
837 gtk_list_store_append(store, &iter);
838 gtk_list_store_set(store, &iter, 0, label, 1, shortcut, 2, PANGO_WEIGHT_NORMAL, -1);
840 g_free(shortcut);
841 g_free(label);
845 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
846 g_object_unref(store);
850 static GtkWidget *create_dialog(void)
852 GtkWidget *dialog, *tree, *label, *swin, *vbox;
853 GtkCellRenderer *text_renderer;
854 GtkTreeViewColumn *column;
856 dialog = gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets.window),
857 GTK_DIALOG_DESTROY_WITH_PARENT,
858 GTK_STOCK_EDIT, GTK_RESPONSE_APPLY,
859 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
860 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
861 gtk_box_set_spacing(GTK_BOX(vbox), 6);
862 gtk_widget_set_name(dialog, "GeanyDialog");
864 gtk_window_set_default_size(GTK_WINDOW(dialog), -1, GEANY_DEFAULT_DIALOG_HEIGHT);
866 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
868 label = gtk_label_new(_("The following keyboard shortcuts are configurable:"));
869 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
871 tree = gtk_tree_view_new();
872 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
873 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
875 text_renderer = gtk_cell_renderer_text_new();
876 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
877 column = gtk_tree_view_column_new_with_attributes(
878 NULL, text_renderer, "text", 0, "weight", 2, NULL);
879 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
881 text_renderer = gtk_cell_renderer_text_new();
882 column = gtk_tree_view_column_new_with_attributes(NULL, text_renderer, "text", 1, NULL);
883 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
885 fill_shortcut_labels_treeview(tree);
887 swin = gtk_scrolled_window_new(NULL, NULL);
888 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_NEVER,
889 GTK_POLICY_AUTOMATIC);
890 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_ETCHED_IN);
891 gtk_container_add(GTK_CONTAINER(swin), tree);
893 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
894 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
896 return dialog;
900 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
901 static GtkWidget *key_dialog = NULL;
903 static void on_dialog_response(GtkWidget *dialog, gint response, gpointer user_data)
905 if (response == GTK_RESPONSE_APPLY)
907 GtkWidget *wid;
909 prefs_show_dialog();
910 /* select the KB page */
911 wid = ui_lookup_widget(ui_widgets.prefs_dialog, "frame22");
912 if (wid != NULL)
914 GtkNotebook *nb = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
916 if (nb != NULL)
917 gtk_notebook_set_current_page(nb, gtk_notebook_page_num(nb, wid));
920 gtk_widget_destroy(dialog);
921 key_dialog = NULL;
925 void keybindings_show_shortcuts(void)
927 if (key_dialog)
928 gtk_widget_destroy(key_dialog); /* in case the key_dialog is still visible */
930 key_dialog = create_dialog();
931 g_signal_connect(key_dialog, "response", G_CALLBACK(on_dialog_response), NULL);
932 gtk_widget_show_all(key_dialog);
936 static gboolean check_fixed_kb(guint keyval, guint state)
938 /* check alt-0 to alt-9 for setting current notebook page */
939 if (state & GDK_MOD1_MASK && keyval >= GDK_0 && keyval <= GDK_9)
941 gint page = keyval - GDK_0 - 1;
942 gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
944 /* alt-0 is for the rightmost tab */
945 if (keyval == GDK_0)
946 page = npages - 1;
947 /* invert the order if tabs are added on the other side */
948 if (swap_alt_tab_order && ! file_prefs.tab_order_ltr)
949 page = (npages - 1) - page;
951 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
952 return TRUE;
954 if (keyval == GDK_Page_Up || keyval == GDK_Page_Down)
956 /* switch to first or last document */
957 if (state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
959 if (keyval == GDK_Page_Up)
960 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), 0);
961 if (keyval == GDK_Page_Down)
962 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
963 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) - 1);
964 return TRUE;
967 return FALSE;
971 static gboolean check_snippet_completion(GeanyDocument *doc)
973 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
975 g_return_val_if_fail(doc, FALSE);
977 /* keybinding only valid when scintilla widget has focus */
978 if (focusw == GTK_WIDGET(doc->editor->sci))
980 ScintillaObject *sci = doc->editor->sci;
981 gint pos = sci_get_current_position(sci);
983 if (editor_prefs.complete_snippets)
984 return editor_complete_snippet(doc->editor, pos);
986 return FALSE;
990 /* Transforms a GdkEventKey event into a GdkEventButton event */
991 static void trigger_button_event(GtkWidget *widget, guint32 event_time)
993 GdkEventButton *event;
994 gboolean ret;
996 event = g_new0(GdkEventButton, 1);
998 if (GTK_IS_TEXT_VIEW(widget))
999 event->window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT);
1000 else
1001 event->window = widget->window;
1002 event->time = event_time;
1003 event->type = GDK_BUTTON_PRESS;
1004 event->button = 3;
1006 g_signal_emit_by_name(widget, "button-press-event", event, &ret);
1007 g_signal_emit_by_name(widget, "button-release-event", event, &ret);
1009 g_free(event);
1013 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
1014 * widgets. Without this special handling, the notebook tab list of the documents' notebook
1015 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
1016 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1017 * notebook tab list. */
1018 static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
1020 if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
1022 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1023 if (doc != NULL)
1025 if (focusw == doc->priv->tag_tree)
1027 trigger_button_event(focusw, event_time);
1028 return TRUE;
1030 if (focusw == GTK_WIDGET(doc->editor->sci))
1032 if (keyval == GDK_Menu)
1033 { /* show editor popup menu */
1034 trigger_button_event(focusw, event_time);
1035 return TRUE;
1037 else
1038 { /* show tab bar menu */
1039 trigger_button_event(main_widgets.notebook, event_time);
1040 return TRUE;
1044 if (focusw == tv.tree_openfiles
1045 || focusw == msgwindow.tree_status
1046 || focusw == msgwindow.tree_compiler
1047 || focusw == msgwindow.tree_msg
1048 || focusw == msgwindow.scribble
1049 #ifdef HAVE_VTE
1050 || (vte_info.have_vte && focusw == vc->vte)
1051 #endif
1054 trigger_button_event(focusw, event_time);
1055 return TRUE;
1058 return FALSE;
1062 #ifdef HAVE_VTE
1063 static gboolean on_menu_expose_event(GtkWidget *widget, GdkEventExpose *event,
1064 gpointer user_data)
1066 if (!GTK_WIDGET_SENSITIVE(widget))
1067 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1068 return FALSE;
1072 static gboolean set_sensitive(gpointer widget)
1074 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1075 return FALSE;
1079 static gboolean check_vte(GdkModifierType state, guint keyval)
1081 guint i;
1082 GtkWidget *widget;
1084 if (! vc->enable_bash_keys)
1085 return FALSE;
1086 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != vc->vte)
1087 return FALSE;
1088 /* prevent menubar flickering: */
1089 if (state == GDK_SHIFT_MASK && (keyval >= GDK_a && keyval <= GDK_z))
1090 return FALSE;
1091 if (state == 0 && (keyval < GDK_F1 || keyval > GDK_F35)) /* e.g. backspace */
1092 return FALSE;
1094 /* make focus commands override any bash commands */
1095 for (i = 0; i < GEANY_KEYS_FOCUS_COUNT; i++)
1097 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FOCUS, i);
1099 if (state == kb->mods && keyval == kb->key)
1100 return FALSE;
1103 /* Temporarily disable the menus to prevent conflicting menu accelerators
1104 * from overriding the VTE bash shortcuts.
1105 * Note: maybe there's a better way of doing this ;-) */
1106 widget = ui_lookup_widget(main_widgets.window, "menubar1");
1107 gtk_widget_set_sensitive(widget, FALSE);
1109 /* make the menubar sensitive before it is redrawn */
1110 static gboolean connected = FALSE;
1111 if (!connected)
1112 g_signal_connect(widget, "expose-event", G_CALLBACK(on_menu_expose_event), NULL);
1115 widget = main_widgets.editor_menu;
1116 gtk_widget_set_sensitive(widget, FALSE);
1117 g_idle_add(set_sensitive, widget);
1118 return TRUE;
1120 #endif
1123 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1124 static guint key_kp_translate(guint key_in)
1126 switch (key_in)
1128 case GDK_KP_Down:
1129 return GDK_Down;
1130 case GDK_KP_Up:
1131 return GDK_Up;
1132 case GDK_KP_Left:
1133 return GDK_Left;
1134 case GDK_KP_Right:
1135 return GDK_Right;
1136 case GDK_KP_Home:
1137 return GDK_Home;
1138 case GDK_KP_End:
1139 return GDK_End;
1140 case GDK_KP_Page_Up:
1141 return GDK_Page_Up;
1142 case GDK_KP_Page_Down:
1143 return GDK_Page_Down;
1144 case GDK_KP_Delete:
1145 return GDK_Delete;
1146 case GDK_KP_Insert:
1147 return GDK_Insert;
1148 default:
1149 return key_in;
1154 /* Stripped down version of the main keypress event handler which can be used
1155 * to process foreign events. Instead of executing the keybinding, a pointer to the
1156 * keybinding structure is returned.
1157 * Additionally, the group_id and binding_id are filled with the appropriate indexes
1158 * if non-NULL. */
1159 const GeanyKeyBinding *keybindings_check_event(GdkEventKey *ev, gint *group_id, gint *binding_id)
1161 guint state, keyval;
1162 gsize g, i;
1163 GeanyKeyGroup *group;
1164 GeanyKeyBinding *kb;
1166 if (ev->keyval == 0)
1167 return FALSE;
1169 keyval = ev->keyval;
1170 state = ev->state & gtk_accelerator_get_default_mod_mask();
1171 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1172 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1173 if (keyval >= GDK_A && keyval <= GDK_Z)
1174 keyval += GDK_a - GDK_A;
1176 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1177 keyval = key_kp_translate(keyval);
1179 for (g = 0; g < keybinding_groups->len; g++)
1181 group = g_ptr_array_index(keybinding_groups, g);
1183 for (i = 0; i < group->count; i++)
1185 kb = &group->keys[i];
1186 if (keyval == kb->key && state == kb->mods)
1188 if (group_id != NULL)
1189 *group_id = g;
1190 if (binding_id != NULL)
1191 *binding_id = i;
1192 return kb;
1196 return NULL;
1200 /* central keypress event handler, almost all keypress events go to this function */
1201 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1203 guint state, keyval;
1204 gsize g, i;
1205 GeanyDocument *doc;
1206 GeanyKeyGroup *group;
1207 GeanyKeyBinding *kb;
1209 if (ev->keyval == 0)
1210 return FALSE;
1212 doc = document_get_current();
1213 if (doc)
1214 document_check_disk_status(doc, FALSE);
1216 keyval = ev->keyval;
1217 state = ev->state & gtk_accelerator_get_default_mod_mask();
1218 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1219 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1220 if (keyval >= GDK_A && keyval <= GDK_Z)
1221 keyval += GDK_a - GDK_A;
1223 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1224 keyval = key_kp_translate(keyval);
1226 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1228 /* special cases */
1229 #ifdef HAVE_VTE
1230 if (vte_info.have_vte && check_vte(state, keyval))
1231 return FALSE;
1232 #endif
1233 if (check_menu_key(doc, keyval, state, ev->time))
1234 return TRUE;
1236 for (g = 0; g < keybinding_groups->len; g++)
1238 group = g_ptr_array_index(keybinding_groups, g);
1240 for (i = 0; i < group->count; i++)
1242 kb = &group->keys[i];
1243 if (keyval == kb->key && state == kb->mods)
1245 /* call the corresponding callback function for this shortcut */
1246 if (kb->callback)
1248 kb->callback(i);
1249 return TRUE;
1251 else if (group->callback)
1253 if (group->callback(i))
1254 return TRUE;
1255 else
1256 continue; /* not handled */
1258 g_warning("No callback for keybinding %s: %s!", group->name, kb->name);
1262 /* fixed keybindings can be overridden by user bindings, so check them last */
1263 if (check_fixed_kb(keyval, state))
1264 return TRUE;
1265 return FALSE;
1269 static gboolean is_modifier_key(guint keyval)
1271 switch (keyval)
1273 case GDK_Shift_L:
1274 case GDK_Shift_R:
1275 case GDK_Control_L:
1276 case GDK_Control_R:
1277 case GDK_Meta_L:
1278 case GDK_Meta_R:
1279 case GDK_Alt_L:
1280 case GDK_Alt_R:
1281 case GDK_Super_L:
1282 case GDK_Super_R:
1283 case GDK_Hyper_L:
1284 case GDK_Hyper_R:
1285 return TRUE;
1286 default:
1287 return FALSE;
1292 GeanyKeyBinding *keybindings_lookup_item(guint group_id, guint key_id)
1294 GeanyKeyGroup *group;
1296 g_return_val_if_fail(group_id < keybinding_groups->len, NULL);
1298 group = g_ptr_array_index(keybinding_groups, group_id);
1300 g_return_val_if_fail(group, NULL);
1301 g_return_val_if_fail(key_id < group->count, NULL);
1303 return &group->keys[key_id];
1307 /** Mimics a (built-in only) keybinding action.
1308 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1309 * @param group_id The index for the key group that contains the @a key_id keybinding.
1310 * @param key_id The keybinding command index. */
1311 void keybindings_send_command(guint group_id, guint key_id)
1313 GeanyKeyBinding *kb;
1315 g_return_if_fail(group_id < GEANY_KEY_GROUP_COUNT); /* can't use this for plugin groups */
1317 kb = keybindings_lookup_item(group_id, key_id);
1318 if (kb)
1320 if (kb->callback)
1321 kb->callback(key_id);
1322 else
1324 GeanyKeyGroup *group = g_ptr_array_index(keybinding_groups, group_id);
1326 if (group->callback)
1327 group->callback(key_id);
1333 /* These are the callback functions, either each group or each shortcut has it's
1334 * own function. */
1337 static gboolean cb_func_file_action(guint key_id)
1339 switch (key_id)
1341 case GEANY_KEYS_FILE_NEW:
1342 document_new_file(NULL, NULL, NULL);
1343 break;
1344 case GEANY_KEYS_FILE_OPEN:
1345 on_open1_activate(NULL, NULL);
1346 break;
1347 case GEANY_KEYS_FILE_OPENSELECTED:
1348 on_menu_open_selected_file1_activate(NULL, NULL);
1349 break;
1350 case GEANY_KEYS_FILE_OPENLASTTAB:
1352 gchar *utf8_filename = g_queue_peek_head(ui_prefs.recent_queue);
1353 gchar *locale_filename = utils_get_locale_from_utf8(utf8_filename);
1354 document_open_file(locale_filename, FALSE, NULL, NULL);
1355 g_free(locale_filename);
1356 break;
1358 case GEANY_KEYS_FILE_SAVE:
1359 on_save1_activate(NULL, NULL);
1360 break;
1361 case GEANY_KEYS_FILE_SAVEAS:
1362 on_save_as1_activate(NULL, NULL);
1363 break;
1364 case GEANY_KEYS_FILE_SAVEALL:
1365 on_save_all1_activate(NULL, NULL);
1366 break;
1367 case GEANY_KEYS_FILE_CLOSE:
1368 on_close1_activate(NULL, NULL);
1369 break;
1370 case GEANY_KEYS_FILE_CLOSEALL:
1371 on_close_all1_activate(NULL, NULL);
1372 break;
1373 case GEANY_KEYS_FILE_RELOAD:
1374 on_toolbutton_reload_clicked(NULL, NULL);
1375 break;
1376 case GEANY_KEYS_FILE_PRINT:
1377 on_print1_activate(NULL, NULL);
1378 break;
1380 return TRUE;
1384 static gboolean cb_func_project_action(guint key_id)
1386 switch (key_id)
1388 case GEANY_KEYS_PROJECT_PROPERTIES:
1389 if (app->project)
1390 on_project_properties1_activate(NULL, NULL);
1391 break;
1393 return TRUE;
1397 static void cb_func_menu_preferences(guint key_id)
1399 switch (key_id)
1401 case GEANY_KEYS_SETTINGS_PREFERENCES:
1402 on_preferences1_activate(NULL, NULL);
1403 break;
1404 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES:
1405 on_plugin_preferences1_activate(NULL, NULL);
1406 break;
1411 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id)
1413 on_help1_activate(NULL, NULL);
1417 static gboolean cb_func_search_action(guint key_id)
1419 GeanyDocument *doc = document_get_current();
1420 ScintillaObject *sci;
1422 if (key_id == GEANY_KEYS_SEARCH_FINDINFILES)
1424 on_find_in_files1_activate(NULL, NULL); /* works without docs too */
1425 return TRUE;
1427 if (!doc)
1428 return TRUE;
1429 sci = doc->editor->sci;
1431 switch (key_id)
1433 case GEANY_KEYS_SEARCH_FIND:
1434 on_find1_activate(NULL, NULL); break;
1435 case GEANY_KEYS_SEARCH_FINDNEXT:
1436 on_find_next1_activate(NULL, NULL); break;
1437 case GEANY_KEYS_SEARCH_FINDPREVIOUS:
1438 on_find_previous1_activate(NULL, NULL); break;
1439 case GEANY_KEYS_SEARCH_FINDPREVSEL:
1440 on_find_prevsel1_activate(NULL, NULL); break;
1441 case GEANY_KEYS_SEARCH_FINDNEXTSEL:
1442 on_find_nextsel1_activate(NULL, NULL); break;
1443 case GEANY_KEYS_SEARCH_REPLACE:
1444 on_replace1_activate(NULL, NULL); break;
1445 case GEANY_KEYS_SEARCH_NEXTMESSAGE:
1446 on_next_message1_activate(NULL, NULL); break;
1447 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE:
1448 on_previous_message1_activate(NULL, NULL); break;
1449 case GEANY_KEYS_SEARCH_FINDUSAGE:
1450 read_current_word(doc);
1451 on_find_usage1_activate(NULL, NULL);
1452 break;
1453 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE:
1454 read_current_word(doc);
1455 on_find_document_usage1_activate(NULL, NULL);
1456 break;
1457 case GEANY_KEYS_SEARCH_MARKALL:
1459 gchar *text = get_current_word_or_sel(doc);
1461 if (sci_has_selection(sci))
1462 search_mark_all(doc, text, SCFIND_MATCHCASE);
1463 else
1465 /* clears markers if text is null */
1466 search_mark_all(doc, text, SCFIND_MATCHCASE | SCFIND_WHOLEWORD);
1468 g_free(text);
1469 break;
1472 return TRUE;
1476 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id)
1478 on_show_color_chooser1_activate(NULL, NULL);
1482 static gboolean cb_func_view_action(guint key_id)
1484 switch (key_id)
1486 case GEANY_KEYS_VIEW_TOGGLEALL:
1487 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
1488 break;
1489 case GEANY_KEYS_VIEW_SIDEBAR:
1490 on_menu_show_sidebar1_toggled(NULL, NULL);
1491 break;
1492 case GEANY_KEYS_VIEW_ZOOMIN:
1493 on_zoom_in1_activate(NULL, NULL);
1494 break;
1495 case GEANY_KEYS_VIEW_ZOOMOUT:
1496 on_zoom_out1_activate(NULL, NULL);
1497 break;
1498 case GEANY_KEYS_VIEW_ZOOMRESET:
1499 on_normal_size1_activate(NULL, NULL);
1500 break;
1501 default:
1502 break;
1504 return TRUE;
1508 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id)
1510 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1511 ui_lookup_widget(main_widgets.window, "menu_fullscreen1"));
1513 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1517 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id)
1519 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1520 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1522 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1526 static gboolean cb_func_build_action(guint key_id)
1528 GtkWidget *item;
1529 BuildMenuItems *menu_items;
1530 GeanyDocument *doc = document_get_current();
1532 if (doc == NULL)
1533 return TRUE;
1535 if (!GTK_WIDGET_IS_SENSITIVE(ui_lookup_widget(main_widgets.window, "menu_build1")))
1536 return TRUE;
1538 menu_items = build_get_menu_items(doc->file_type->id);
1539 /* TODO make it a table??*/
1540 switch (key_id)
1542 case GEANY_KEYS_BUILD_COMPILE:
1543 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_COMPILE)];
1544 break;
1545 case GEANY_KEYS_BUILD_LINK:
1546 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_BUILD)];
1547 break;
1548 case GEANY_KEYS_BUILD_MAKE:
1549 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_ALL)];
1550 break;
1551 case GEANY_KEYS_BUILD_MAKEOWNTARGET:
1552 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_CUSTOM)];
1553 break;
1554 case GEANY_KEYS_BUILD_MAKEOBJECT:
1555 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)];
1556 break;
1557 case GEANY_KEYS_BUILD_NEXTERROR:
1558 item = menu_items->menu_item[GBG_FIXED][GBF_NEXT_ERROR];
1559 break;
1560 case GEANY_KEYS_BUILD_PREVIOUSERROR:
1561 item = menu_items->menu_item[GBG_FIXED][GBF_PREV_ERROR];
1562 break;
1563 case GEANY_KEYS_BUILD_RUN:
1564 item = menu_items->menu_item[GEANY_GBG_EXEC][GBO_TO_CMD(GEANY_GBO_EXEC)];
1565 break;
1566 case GEANY_KEYS_BUILD_OPTIONS:
1567 item = menu_items->menu_item[GBG_FIXED][GBF_COMMANDS];
1568 break;
1569 default:
1570 item = NULL;
1572 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1573 * sensitive state, but some other menus don't update the sensitive status until
1574 * they are redrawn. */
1575 if (item && GTK_WIDGET_IS_SENSITIVE(item))
1576 gtk_menu_item_activate(GTK_MENU_ITEM(item));
1577 return TRUE;
1581 static gboolean read_current_word(GeanyDocument *doc)
1583 gint pos;
1585 if (doc == NULL)
1586 return FALSE;
1588 pos = sci_get_current_position(doc->editor->sci);
1590 editor_find_current_word(doc->editor, pos,
1591 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1593 return (*editor_info.current_word != 0);
1597 static gboolean check_current_word(GeanyDocument *doc)
1599 if (!read_current_word(doc))
1601 utils_beep();
1602 return FALSE;
1604 return TRUE;
1608 static gchar *get_current_word_or_sel(GeanyDocument *doc)
1610 ScintillaObject *sci = doc->editor->sci;
1612 if (sci_has_selection(sci))
1613 return sci_get_selection_contents(sci);
1615 return read_current_word(doc) ? g_strdup(editor_info.current_word) : NULL;
1619 static void focus_sidebar(void)
1621 if (ui_prefs.sidebar_visible)
1623 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook));
1624 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), page_num);
1626 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1627 gtk_widget_child_focus(page, GTK_DIR_TAB_FORWARD);
1632 static void focus_msgwindow(void)
1634 if (ui_prefs.msgwindow_visible)
1636 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow.notebook));
1637 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(msgwindow.notebook), page_num);
1639 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(page)));
1644 static gboolean cb_func_switch_action(guint key_id)
1646 switch (key_id)
1648 case GEANY_KEYS_FOCUS_EDITOR:
1650 GeanyDocument *doc = document_get_current();
1651 if (doc != NULL)
1653 GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
1654 if (GTK_WIDGET_HAS_FOCUS(sci))
1655 ui_update_statusbar(doc, -1);
1656 else
1657 gtk_widget_grab_focus(sci);
1659 break;
1661 case GEANY_KEYS_FOCUS_SCRIBBLE:
1662 msgwin_switch_tab(MSG_SCRATCH, TRUE);
1663 break;
1664 case GEANY_KEYS_FOCUS_SEARCHBAR:
1665 if (toolbar_prefs.visible)
1667 GtkWidget *search_entry = toolbar_get_widget_child_by_name("SearchEntry");
1668 if (search_entry != NULL)
1669 gtk_widget_grab_focus(search_entry);
1671 break;
1672 case GEANY_KEYS_FOCUS_SIDEBAR:
1673 focus_sidebar();
1674 break;
1675 case GEANY_KEYS_FOCUS_VTE:
1676 msgwin_switch_tab(MSG_VTE, TRUE);
1677 break;
1678 case GEANY_KEYS_FOCUS_COMPILER:
1679 msgwin_switch_tab(MSG_COMPILER, TRUE);
1680 break;
1681 case GEANY_KEYS_FOCUS_MESSAGES:
1682 msgwin_switch_tab(MSG_MESSAGE, TRUE);
1683 break;
1684 case GEANY_KEYS_FOCUS_MESSAGE_WINDOW:
1685 focus_msgwindow();
1686 break;
1687 case GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST:
1688 sidebar_focus_openfiles_tab();
1689 break;
1690 case GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST:
1691 sidebar_focus_symbols_tab();
1692 break;
1694 return TRUE;
1698 static void switch_notebook_page(gint direction)
1700 gint page_count, cur_page;
1701 gboolean parent_is_notebook = FALSE;
1702 GtkNotebook *notebook;
1703 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1705 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1708 parent_is_notebook = GTK_IS_NOTEBOOK(focusw);
1710 while (! parent_is_notebook && (focusw = gtk_widget_get_parent(focusw)) != NULL);
1712 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1713 if (parent_is_notebook)
1714 notebook = GTK_NOTEBOOK(focusw);
1715 else
1716 notebook = GTK_NOTEBOOK(main_widgets.notebook);
1718 /* now switch pages */
1719 page_count = gtk_notebook_get_n_pages(notebook);
1720 cur_page = gtk_notebook_get_current_page(notebook);
1722 if (direction == GTK_DIR_LEFT)
1724 if (cur_page > 0)
1725 gtk_notebook_set_current_page(notebook, cur_page - 1);
1726 else
1727 gtk_notebook_set_current_page(notebook, page_count - 1);
1729 else if (direction == GTK_DIR_RIGHT)
1731 if (cur_page < page_count - 1)
1732 gtk_notebook_set_current_page(notebook, cur_page + 1);
1733 else
1734 gtk_notebook_set_current_page(notebook, 0);
1739 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id)
1741 switch_notebook_page(GTK_DIR_LEFT);
1745 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id)
1747 switch_notebook_page(GTK_DIR_RIGHT);
1751 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1753 /* user may have rebound keybinding to a different modifier than Ctrl, so check all */
1754 if (!switch_dialog_cancelled && is_modifier_key(ev->keyval))
1756 switch_dialog_cancelled = TRUE;
1758 if (switch_dialog && GTK_WIDGET_VISIBLE(switch_dialog))
1759 gtk_widget_hide(switch_dialog);
1761 mru_pos = 0;
1763 return FALSE;
1767 static GtkWidget *ui_minimal_dialog_new(GtkWindow *parent, const gchar *title)
1769 GtkWidget *dialog;
1771 dialog = gtk_window_new(GTK_WINDOW_POPUP);
1773 if (parent)
1775 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
1776 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
1778 gtk_window_set_title(GTK_WINDOW(dialog), title);
1779 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
1780 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
1782 gtk_widget_set_name(dialog, "GeanyDialog");
1783 return dialog;
1787 static GtkWidget *create_switch_dialog(void)
1789 GtkWidget *dialog, *widget, *vbox;
1791 dialog = ui_minimal_dialog_new(GTK_WINDOW(main_widgets.window), _("Switch to Document"));
1792 gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE);
1793 gtk_window_set_default_size(GTK_WINDOW(dialog), 150, -1);
1795 vbox = gtk_vbox_new(FALSE, 6);
1796 gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
1797 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1799 widget = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
1800 gtk_container_add(GTK_CONTAINER(vbox), widget);
1802 widget = geany_wrap_label_new(NULL);
1803 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
1804 gtk_container_add(GTK_CONTAINER(vbox), widget);
1805 switch_dialog_label = widget;
1807 g_signal_connect(dialog, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
1808 return dialog;
1812 static gboolean on_switch_timeout(G_GNUC_UNUSED gpointer data)
1814 if (switch_dialog_cancelled)
1816 return FALSE;
1818 if (! switch_dialog || !GTK_WIDGET_VISIBLE(switch_dialog))
1819 mru_pos = 2; /* skip past the previous document */
1820 else
1821 mru_pos += 1;
1823 if (! switch_dialog)
1824 switch_dialog = create_switch_dialog();
1826 geany_wrap_label_set_text(GTK_LABEL(switch_dialog_label),
1827 DOC_FILENAME(document_get_current()));
1828 gtk_widget_show_all(switch_dialog);
1829 return FALSE;
1833 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id)
1835 GeanyDocument *last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1837 if (! DOC_VALID(last_doc))
1839 utils_beep();
1840 mru_pos = 0;
1841 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1843 if (! DOC_VALID(last_doc))
1844 return;
1846 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
1847 document_get_notebook_page(last_doc));
1849 /* if there's a modifier key, we can switch back in MRU order each time unless
1850 * the key is released */
1851 if (! switch_dialog_cancelled)
1853 on_switch_timeout(NULL); /* update filename label */
1855 else
1856 if (keybindings_lookup_item(GEANY_KEY_GROUP_NOTEBOOK,
1857 GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED)->mods)
1859 switch_dialog_cancelled = FALSE;
1861 /* delay showing dialog to give user time to let go of any modifier keys */
1862 g_timeout_add(600, on_switch_timeout, NULL);
1867 /* move document left/right/first/last */
1868 static void cb_func_move_tab(guint key_id)
1870 GtkWidget *sci;
1871 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
1872 gint cur_page = gtk_notebook_get_current_page(nb);
1873 GeanyDocument *doc = document_get_current();
1875 if (doc == NULL)
1876 return;
1878 sci = GTK_WIDGET(doc->editor->sci);
1880 switch (key_id)
1882 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT:
1883 gtk_notebook_reorder_child(nb, sci, cur_page - 1); /* notebook wraps around by default */
1884 break;
1885 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT:
1887 gint npage = cur_page + 1;
1889 if (npage == gtk_notebook_get_n_pages(nb))
1890 npage = 0; /* wraparound */
1891 gtk_notebook_reorder_child(nb, sci, npage);
1892 break;
1894 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST:
1895 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? 0 : -1);
1896 break;
1897 case GEANY_KEYS_NOTEBOOK_MOVETABLAST:
1898 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? -1 : 0);
1899 break;
1901 return;
1905 static void goto_matching_brace(GeanyDocument *doc)
1907 gint pos, new_pos;
1908 gint after_brace;
1910 if (doc == NULL)
1911 return;
1913 pos = sci_get_current_position(doc->editor->sci);
1914 after_brace = pos > 0 && utils_isbrace(sci_get_char_at(doc->editor->sci, pos - 1), TRUE);
1915 pos -= after_brace; /* set pos to the brace */
1917 new_pos = sci_find_matching_brace(doc->editor->sci, pos);
1918 if (new_pos != -1)
1919 { /* set the cursor at/after the brace */
1920 sci_set_current_position(doc->editor->sci, new_pos + (!after_brace), FALSE);
1921 editor_display_current_line(doc->editor, 0.5F);
1926 static gboolean cb_func_clipboard_action(guint key_id)
1928 GeanyDocument *doc = document_get_current();
1930 if (doc == NULL)
1931 return TRUE;
1933 switch (key_id)
1935 case GEANY_KEYS_CLIPBOARD_CUT:
1936 on_cut1_activate(NULL, NULL);
1937 break;
1938 case GEANY_KEYS_CLIPBOARD_COPY:
1939 on_copy1_activate(NULL, NULL);
1940 break;
1941 case GEANY_KEYS_CLIPBOARD_PASTE:
1942 on_paste1_activate(NULL, NULL);
1943 break;
1944 case GEANY_KEYS_CLIPBOARD_COPYLINE:
1945 sci_send_command(doc->editor->sci, SCI_LINECOPY);
1946 break;
1947 case GEANY_KEYS_CLIPBOARD_CUTLINE:
1948 sci_send_command(doc->editor->sci, SCI_LINECUT);
1949 break;
1951 return TRUE;
1955 static void goto_tag(GeanyDocument *doc, gboolean definition)
1957 gchar *text = get_current_word_or_sel(doc);
1959 if (text)
1960 symbols_goto_tag(text, definition);
1961 else
1962 utils_beep();
1964 g_free(text);
1968 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
1969 static gboolean cb_func_goto_action(guint key_id)
1971 gint cur_line;
1972 GeanyDocument *doc = document_get_current();
1974 if (doc == NULL)
1975 return TRUE;
1977 cur_line = sci_get_current_line(doc->editor->sci);
1979 switch (key_id)
1981 case GEANY_KEYS_GOTO_BACK:
1982 navqueue_go_back();
1983 return TRUE;
1984 case GEANY_KEYS_GOTO_FORWARD:
1985 navqueue_go_forward();
1986 return TRUE;
1987 case GEANY_KEYS_GOTO_LINE:
1989 if (toolbar_prefs.visible)
1991 GtkWidget *wid = toolbar_get_widget_child_by_name("GotoEntry");
1993 /* use toolbar item if shown & not in the drop down overflow menu */
1994 if (wid && GTK_WIDGET_MAPPED(wid))
1996 gtk_widget_grab_focus(wid);
1997 return TRUE;
2000 on_go_to_line_activate(NULL, NULL);
2001 return TRUE;
2003 case GEANY_KEYS_GOTO_MATCHINGBRACE:
2004 goto_matching_brace(doc);
2005 return TRUE;
2006 case GEANY_KEYS_GOTO_TOGGLEMARKER:
2008 sci_toggle_marker_at_line(doc->editor->sci, cur_line, 1);
2009 return TRUE;
2011 case GEANY_KEYS_GOTO_NEXTMARKER:
2013 gint mline = sci_marker_next(doc->editor->sci, cur_line + 1, 1 << 1, TRUE);
2015 if (mline != -1)
2017 sci_set_current_line(doc->editor->sci, mline);
2018 editor_display_current_line(doc->editor, 0.5F);
2020 return TRUE;
2022 case GEANY_KEYS_GOTO_PREVIOUSMARKER:
2024 gint mline = sci_marker_previous(doc->editor->sci, cur_line - 1, 1 << 1, TRUE);
2026 if (mline != -1)
2028 sci_set_current_line(doc->editor->sci, mline);
2029 editor_display_current_line(doc->editor, 0.5F);
2031 return TRUE;
2033 case GEANY_KEYS_GOTO_TAGDEFINITION:
2034 goto_tag(doc, TRUE);
2035 return TRUE;
2036 case GEANY_KEYS_GOTO_TAGDECLARATION:
2037 goto_tag(doc, FALSE);
2038 return TRUE;
2040 /* only check editor-sensitive keybindings when editor has focus so home,end still
2041 * work in other widgets */
2042 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
2043 return FALSE;
2045 switch (key_id)
2047 case GEANY_KEYS_GOTO_LINESTART:
2048 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
2049 break;
2050 case GEANY_KEYS_GOTO_LINEEND:
2051 sci_send_command(doc->editor->sci, SCI_LINEEND);
2052 break;
2053 case GEANY_KEYS_GOTO_LINEENDVISUAL:
2054 sci_send_command(doc->editor->sci, SCI_LINEENDDISPLAY);
2055 break;
2056 case GEANY_KEYS_GOTO_PREVWORDPART:
2057 sci_send_command(doc->editor->sci, SCI_WORDPARTLEFT);
2058 break;
2059 case GEANY_KEYS_GOTO_NEXTWORDPART:
2060 sci_send_command(doc->editor->sci, SCI_WORDPARTRIGHT);
2061 break;
2063 return TRUE;
2067 static void duplicate_lines(GeanyEditor *editor)
2069 if (sci_get_lines_selected(editor->sci) > 1)
2070 { /* ignore extra_line because of selecting lines from the line number column */
2071 editor_select_lines(editor, FALSE);
2072 sci_selection_duplicate(editor->sci);
2074 else if (sci_has_selection(editor->sci))
2075 sci_selection_duplicate(editor->sci);
2076 else
2077 sci_line_duplicate(editor->sci);
2081 static void delete_lines(GeanyEditor *editor)
2083 editor_select_lines(editor, TRUE); /* include last line (like cut lines, copy lines do) */
2084 sci_clear(editor->sci); /* (SCI_LINEDELETE only does 1 line) */
2088 static void move_lines(GeanyEditor *editor, gboolean down)
2090 ScintillaObject *sci = editor->sci;
2091 gchar *text;
2092 gint pos, line, len;
2094 sci_start_undo_action(sci);
2095 editor_select_lines(editor, FALSE);
2096 len = sci_get_selected_text_length(sci);
2098 pos = sci_get_selection_start(sci);
2099 line = sci_get_line_from_position(sci, pos);
2100 if (down)
2101 line++;
2102 else
2103 line--;
2105 text = sci_get_selection_contents(sci);
2106 sci_clear(sci);
2108 pos = sci_get_position_from_line(sci, line);
2109 sci_insert_text(sci, pos, text);
2110 g_free(text);
2112 sci_set_current_position(sci, pos, TRUE);
2113 sci_set_selection_end(sci, pos + len - 1);
2115 sci_end_undo_action(sci);
2119 /* common function for editor keybindings, only valid when scintilla has focus. */
2120 static gboolean cb_func_editor_action(guint key_id)
2122 GeanyDocument *doc = document_get_current();
2123 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2125 /* edit keybindings only valid when scintilla widget has focus */
2126 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2127 return FALSE; /* also makes tab work outside editor */
2129 switch (key_id)
2131 case GEANY_KEYS_EDITOR_UNDO:
2132 on_undo1_activate(NULL, NULL);
2133 break;
2134 case GEANY_KEYS_EDITOR_REDO:
2135 on_redo1_activate(NULL, NULL);
2136 break;
2137 case GEANY_KEYS_EDITOR_SCROLLTOLINE:
2138 editor_scroll_to_line(doc->editor, -1, 0.5F);
2139 break;
2140 case GEANY_KEYS_EDITOR_SCROLLLINEUP:
2141 sci_send_command(doc->editor->sci, SCI_LINESCROLLUP);
2142 break;
2143 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN:
2144 sci_send_command(doc->editor->sci, SCI_LINESCROLLDOWN);
2145 break;
2146 case GEANY_KEYS_EDITOR_DUPLICATELINE:
2147 duplicate_lines(doc->editor);
2148 break;
2149 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR:
2150 editor_goto_next_snippet_cursor(doc->editor);
2151 break;
2152 case GEANY_KEYS_EDITOR_DELETELINE:
2153 delete_lines(doc->editor);
2154 break;
2155 case GEANY_KEYS_EDITOR_DELETELINETOEND:
2156 sci_send_command(doc->editor->sci, SCI_DELLINERIGHT);
2157 break;
2158 case GEANY_KEYS_EDITOR_TRANSPOSELINE:
2159 sci_send_command(doc->editor->sci, SCI_LINETRANSPOSE);
2160 break;
2161 case GEANY_KEYS_EDITOR_AUTOCOMPLETE:
2162 editor_start_auto_complete(doc->editor, sci_get_current_position(doc->editor->sci), TRUE);
2163 break;
2164 case GEANY_KEYS_EDITOR_CALLTIP:
2165 editor_show_calltip(doc->editor, -1);
2166 break;
2167 case GEANY_KEYS_EDITOR_MACROLIST:
2168 editor_show_macro_list(doc->editor);
2169 break;
2170 case GEANY_KEYS_EDITOR_CONTEXTACTION:
2171 if (check_current_word(doc))
2172 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu,
2173 "context_action1")), NULL);
2174 break;
2175 case GEANY_KEYS_EDITOR_COMPLETESNIPPET:
2176 /* allow tab to be overloaded */
2177 return check_snippet_completion(doc);
2179 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION:
2181 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
2182 GEANY_KEYS_EDITOR_COMPLETESNIPPET);
2184 switch (kb->key)
2186 case GDK_space:
2187 sci_add_text(doc->editor->sci, " ");
2188 break;
2189 case GDK_Tab:
2190 sci_send_command(doc->editor->sci, SCI_TAB);
2191 break;
2192 default:
2193 break;
2195 break;
2197 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION:
2198 return editor_complete_word_part(doc->editor);
2200 case GEANY_KEYS_EDITOR_MOVELINEUP:
2201 move_lines(doc->editor, FALSE);
2202 break;
2203 case GEANY_KEYS_EDITOR_MOVELINEDOWN:
2204 move_lines(doc->editor, TRUE);
2205 break;
2207 return TRUE;
2211 static void join_lines(GeanyEditor *editor)
2213 gint start, end, i;
2215 start = sci_get_line_from_position(editor->sci,
2216 sci_get_selection_start(editor->sci));
2217 end = sci_get_line_from_position(editor->sci,
2218 sci_get_selection_end(editor->sci));
2220 /* if there is only one line in selection, join it with the following one */
2221 if (end == start)
2222 end = start + 1;
2225 * remove trailing spaces for every line except the last one
2226 * so that these spaces won't appear within text after joining
2228 for (i = start; i < end; i++)
2229 editor_strip_line_trailing_spaces(editor, i);
2231 /* remove starting spaces from second and following lines due to the same reason */
2232 for (i = start + 1; i <= end; i++)
2233 sci_set_line_indentation(editor->sci, i, 0);
2236 * SCI_LINESJOIN automatically adds spaces between joined lines, including
2237 * empty ones. We should drop empty lines if we want only one space to be
2238 * inserted (see also example below). I don't think we should care of that.
2241 sci_set_target_start(editor->sci,
2242 sci_get_position_from_line(editor->sci, start));
2243 sci_set_target_end(editor->sci,
2244 sci_get_position_from_line(editor->sci, end));
2245 sci_lines_join(editor->sci);
2248 * Example: joining
2250 * [TAB]if (something_wrong)
2251 * [TAB]{
2252 * [TAB][TAB]
2253 * [TAB][TAB]exit(1);[SPACE][SPACE]
2254 * [TAB]}[SPACE]
2256 * gives
2258 * [TAB]if (something_wrong) { exit(1); }[SPACE]
2263 static void split_lines(GeanyEditor *editor, gint column)
2265 gint start, indent, linescount, i, end;
2266 gchar c;
2267 ScintillaObject *sci = editor->sci;
2269 /* don't include trailing newlines */
2270 end = sci_get_selection_end(sci);
2271 while ((c = sci_get_char_at(sci, end - 1)) == '\n' || c == '\r') end--;
2272 sci_set_selection_end(sci, end);
2274 start = sci_get_line_from_position(editor->sci,
2275 sci_get_selection_start(editor->sci));
2278 * If several lines are selected, first join them.
2279 * This allows to reformat text paragraphs easily.
2281 if (sci_get_lines_selected(editor->sci) > 1)
2282 join_lines(editor);
2285 * If this line is short enough, just return
2287 if (column > sci_get_line_end_position(editor->sci, start) -
2288 sci_get_position_from_line(editor->sci, start))
2290 return;
2294 * We have to manipulate line indentation so that indentation
2295 * of the resulting lines would be consistent. For example,
2296 * the result of splitting "[TAB]very long content":
2298 * +-------------+-------------+
2299 * | proper | wrong |
2300 * +-------------+-------------+
2301 * | [TAB]very | [TAB]very |
2302 * | [TAB]long | long |
2303 * | [TAB]content| content |
2304 * +-------------+-------------+
2306 indent = sci_get_line_indentation(editor->sci, start);
2307 sci_set_line_indentation(editor->sci, start, 0);
2310 * Use sci_get_line_count() to determine how many new lines
2311 * appeared during splitting. SCI_LINESSPLIT should better return
2312 * this value itself...
2314 sci_target_from_selection(editor->sci);
2315 linescount = sci_get_line_count(editor->sci);
2316 sci_lines_split(editor->sci,
2317 (column - indent) * sci_text_width(editor->sci, STYLE_DEFAULT, " "));
2318 linescount = sci_get_line_count(editor->sci) - linescount;
2320 /* Fix indentation. */
2321 for (i = start; i <= start + linescount; i++)
2322 sci_set_line_indentation(editor->sci, i, indent);
2324 /* Remove trailing spaces. */
2325 if (editor_prefs.newline_strip || file_prefs.strip_trailing_spaces)
2327 for (i = start; i <= start + linescount; i++)
2328 editor_strip_line_trailing_spaces(editor, i);
2333 /* if cursor < anchor, swap them */
2334 static void sci_fix_selection(ScintillaObject *sci)
2336 gint start, end;
2338 start = sci_get_selection_start(sci);
2339 end = sci_get_selection_end(sci);
2340 sci_set_selection(sci, start, end);
2344 static void reflow_paragraph(GeanyEditor *editor)
2346 ScintillaObject *sci = editor->sci;
2347 gboolean sel;
2348 gint column = -1;
2350 if (editor->line_breaking)
2352 /* use line break column if enabled */
2353 column = editor_prefs.line_break_column;
2355 else if (editor_get_long_line_type() != 2)
2357 /* use long line if enabled */
2358 column = editor_get_long_line_column();
2360 else
2362 /* do nothing if no column is defined */
2363 utils_beep();
2364 return;
2366 sci_start_undo_action(sci);
2367 sel = sci_has_selection(sci);
2368 if (!sel)
2370 gint line, pos;
2372 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
2373 /* deselect last line break */
2374 pos = sci_get_selection_end(sci);
2375 line = sci_get_line_from_position(sci, pos);
2376 if (line < sci_get_line_count(sci) - 1)
2378 /* not last line */
2379 pos = sci_get_line_end_position(sci, line - 1);
2380 sci_set_selection_end(sci, pos);
2383 sci_fix_selection(sci);
2384 split_lines(editor, column);
2385 if (!sel)
2386 sci_set_anchor(sci, -1);
2388 sci_end_undo_action(sci);
2392 /* common function for format keybindings, only valid when scintilla has focus. */
2393 static gboolean cb_func_format_action(guint key_id)
2395 GeanyDocument *doc = document_get_current();
2396 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2398 /* keybindings only valid when scintilla widget has focus */
2399 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2400 return TRUE;
2402 switch (key_id)
2404 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE:
2405 on_menu_toggle_line_commentation1_activate(NULL, NULL);
2406 break;
2407 case GEANY_KEYS_FORMAT_COMMENTLINE:
2408 on_menu_comment_line1_activate(NULL, NULL);
2409 break;
2410 case GEANY_KEYS_FORMAT_UNCOMMENTLINE:
2411 on_menu_uncomment_line1_activate(NULL, NULL);
2412 break;
2413 case GEANY_KEYS_FORMAT_INCREASEINDENT:
2414 on_menu_increase_indent1_activate(NULL, NULL);
2415 break;
2416 case GEANY_KEYS_FORMAT_DECREASEINDENT:
2417 on_menu_decrease_indent1_activate(NULL, NULL);
2418 break;
2419 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE:
2420 editor_indentation_by_one_space(doc->editor, -1, FALSE);
2421 break;
2422 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE:
2423 editor_indentation_by_one_space(doc->editor, -1, TRUE);
2424 break;
2425 case GEANY_KEYS_FORMAT_AUTOINDENT:
2426 editor_smart_line_indentation(doc->editor, -1);
2427 break;
2428 case GEANY_KEYS_FORMAT_TOGGLECASE:
2429 on_toggle_case1_activate(NULL, NULL);
2430 break;
2431 case GEANY_KEYS_FORMAT_SENDTOCMD1:
2432 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 0)
2433 tools_execute_custom_command(doc, ui_prefs.custom_commands[0]);
2434 break;
2435 case GEANY_KEYS_FORMAT_SENDTOCMD2:
2436 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 1)
2437 tools_execute_custom_command(doc, ui_prefs.custom_commands[1]);
2438 break;
2439 case GEANY_KEYS_FORMAT_SENDTOCMD3:
2440 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 2)
2441 tools_execute_custom_command(doc, ui_prefs.custom_commands[2]);
2442 break;
2443 case GEANY_KEYS_FORMAT_SENDTOVTE:
2444 on_send_selection_to_vte1_activate(NULL, NULL);
2445 break;
2446 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
2447 reflow_paragraph(doc->editor);
2448 break;
2450 return TRUE;
2454 /* common function for select keybindings, only valid when scintilla has focus. */
2455 static gboolean cb_func_select_action(guint key_id)
2457 GeanyDocument *doc;
2458 ScintillaObject *sci;
2459 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2460 GtkWidget *toolbar_search_entry = toolbar_get_widget_child_by_name("SearchEntry");
2461 GtkWidget *toolbar_goto_entry = toolbar_get_widget_child_by_name("GotoEntry");
2463 /* special case for Select All in the scribble widget */
2464 if (key_id == GEANY_KEYS_SELECT_ALL && focusw == msgwindow.scribble)
2466 g_signal_emit_by_name(msgwindow.scribble, "select-all", TRUE);
2467 return TRUE;
2469 /* special case for Select All in the toolbar search widget */
2470 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_search_entry)
2472 gtk_editable_select_region(GTK_EDITABLE(toolbar_search_entry), 0, -1);
2473 return TRUE;
2475 else if (key_id == GEANY_KEYS_SELECT_ALL && focusw == toolbar_goto_entry)
2477 gtk_editable_select_region(GTK_EDITABLE(toolbar_goto_entry), 0, -1);
2478 return TRUE;
2481 doc = document_get_current();
2482 /* keybindings only valid when scintilla widget has focus */
2483 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2484 return TRUE;
2485 sci = doc->editor->sci;
2487 switch (key_id)
2489 case GEANY_KEYS_SELECT_ALL:
2490 on_menu_select_all1_activate(NULL, NULL);
2491 break;
2492 case GEANY_KEYS_SELECT_WORD:
2493 editor_select_word(doc->editor);
2494 break;
2495 case GEANY_KEYS_SELECT_LINE:
2496 editor_select_lines(doc->editor, FALSE);
2497 break;
2498 case GEANY_KEYS_SELECT_PARAGRAPH:
2499 editor_select_paragraph(doc->editor);
2500 break;
2501 case GEANY_KEYS_SELECT_WORDPARTLEFT:
2502 sci_send_command(sci, SCI_WORDPARTLEFTEXTEND);
2503 break;
2504 case GEANY_KEYS_SELECT_WORDPARTRIGHT:
2505 sci_send_command(sci, SCI_WORDPARTRIGHTEXTEND);
2506 break;
2508 return TRUE;
2512 static gboolean cb_func_document_action(guint key_id)
2514 GeanyDocument *doc = document_get_current();
2516 if (doc == NULL)
2517 return TRUE;
2519 switch (key_id)
2521 case GEANY_KEYS_DOCUMENT_REPLACETABS:
2522 on_replace_tabs_activate(NULL, NULL);
2523 break;
2524 case GEANY_KEYS_DOCUMENT_REPLACESPACES:
2525 on_replace_spaces_activate(NULL, NULL);
2526 break;
2527 case GEANY_KEYS_DOCUMENT_LINEBREAK:
2528 on_line_breaking1_activate(NULL, NULL);
2529 ui_document_show_hide(doc);
2530 break;
2531 case GEANY_KEYS_DOCUMENT_LINEWRAP:
2532 on_line_wrapping1_toggled(NULL, NULL);
2533 ui_document_show_hide(doc);
2534 break;
2535 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST:
2536 document_update_tag_list(doc, TRUE);
2537 break;
2538 case GEANY_KEYS_DOCUMENT_FOLDALL:
2539 editor_fold_all(doc->editor);
2540 break;
2541 case GEANY_KEYS_DOCUMENT_UNFOLDALL:
2542 editor_unfold_all(doc->editor);
2543 break;
2544 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD:
2545 if (editor_prefs.folding)
2547 gint line = sci_get_current_line(doc->editor->sci);
2548 editor_toggle_fold(doc->editor, line, 0);
2549 break;
2551 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS:
2552 on_remove_markers1_activate(NULL, NULL);
2553 break;
2554 case GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS:
2555 on_menu_remove_indicators1_activate(NULL, NULL);
2556 break;
2558 return TRUE;
2562 /* common function for insert keybindings, only valid when scintilla has focus. */
2563 static gboolean cb_func_insert_action(guint key_id)
2565 GeanyDocument *doc = document_get_current();
2566 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2568 /* keybindings only valid when scintilla widget has focus */
2569 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2570 return TRUE;
2572 switch (key_id)
2574 case GEANY_KEYS_INSERT_ALTWHITESPACE:
2575 editor_insert_alternative_whitespace(doc->editor);
2576 break;
2577 case GEANY_KEYS_INSERT_DATE:
2578 gtk_menu_item_activate(GTK_MENU_ITEM(
2579 ui_lookup_widget(main_widgets.window, "insert_date_custom1")));
2580 break;
2582 return TRUE;
2586 /* update key combination */
2587 void keybindings_update_combo(GeanyKeyBinding *kb, guint key, GdkModifierType mods)
2589 GtkWidget *widget = kb->menu_item;
2591 if (widget && kb->key)
2592 gtk_widget_remove_accelerator(widget, kb_accel_group, kb->key, kb->mods);
2594 kb->key = key;
2595 kb->mods = mods;
2597 if (widget && kb->key)
2598 gtk_widget_add_accelerator(widget, "activate", kb_accel_group,
2599 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
2603 /* used for plugins */
2604 GeanyKeyGroup *keybindings_set_group(GeanyKeyGroup *group, const gchar *section_name,
2605 const gchar *label, gsize count, GeanyKeyGroupCallback callback)
2607 g_return_val_if_fail(section_name, NULL);
2608 g_return_val_if_fail(count, NULL);
2610 /* prevent conflict with core bindings */
2611 g_return_val_if_fail(!g_str_equal(section_name, keybindings_keyfile_group_name), NULL);
2613 if (!group)
2614 group = g_new0(GeanyKeyGroup, 1);
2616 if (!group->keys || count > group->count)
2618 /* allow resizing existing array of keys */
2619 group->keys = g_renew(GeanyKeyBinding, group->keys, count);
2620 memset(group->keys + group->count, 0, (count - group->count) * sizeof(GeanyKeyBinding));
2622 group->plugin = TRUE;
2623 add_kb_group(group, section_name, label, count, group->keys, callback);
2624 return group;
2628 /* used for plugins */
2629 void keybindings_free_group(GeanyKeyGroup *group)
2631 GeanyKeyBinding *kb;
2633 g_assert(group->plugin);
2635 foreach_c_array(kb, group->keys, group->count)
2637 g_free(kb->name);
2638 g_free(kb->label);
2640 g_free(group->keys);
2641 g_ptr_array_remove_fast(keybinding_groups, group);
2642 g_free(group);