Capitalize, add mnemonics, sync with kb.c the Edit->Commands menu item
[geany-mirror.git] / src / keybindings.c
blob63feee399c318a7a24ae540b8d40cbc7f3f0646b
1 /*
2 * keybindings.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2009 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2009 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$
25 * Configurable keyboard shortcuts.
29 #include "geany.h"
31 #include <gdk/gdkkeysyms.h>
32 #include <string.h>
34 #include "keybindings.h"
35 #include "support.h"
36 #include "utils.h"
37 #include "ui_utils.h"
38 #include "document.h"
39 #include "documentprivate.h"
40 #include "filetypes.h"
41 #include "callbacks.h"
42 #include "prefs.h"
43 #include "msgwindow.h"
44 #include "editor.h"
45 #include "sciwrappers.h"
46 #include "build.h"
47 #include "tools.h"
48 #include "navqueue.h"
49 #include "symbols.h"
50 #include "vte.h"
51 #include "toolbar.h"
52 #include "sidebar.h"
53 #include "geanywraplabel.h"
54 #include "main.h"
55 #include "search.h"
58 GPtrArray *keybinding_groups; /* array of GeanyKeyGroup pointers */
60 /* keyfile group name for non-plugin KB groups */
61 const gchar keybindings_keyfile_group_name[] = "Bindings";
63 static GtkAccelGroup *kb_accel_group = NULL;
64 static const gboolean swap_alt_tab_order = FALSE;
66 const gsize MAX_MRU_DOCS = 20;
67 static GQueue *mru_docs = NULL;
68 static guint mru_pos = 0;
70 static gboolean switch_dialog_cancelled = TRUE;
71 static GtkWidget *switch_dialog = NULL;
72 static GtkWidget *switch_dialog_label = NULL;
75 /* central keypress event handler, almost all keypress events go to this function */
76 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
77 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *event, gpointer user_data);
79 static gboolean check_current_word(GeanyDocument *doc);
80 static gboolean read_current_word(GeanyDocument *doc);
81 static gchar *get_current_word_or_sel(GeanyDocument *doc);
83 static gboolean cb_func_file_action(guint key_id);
84 static gboolean cb_func_project_action(guint key_id);
85 static gboolean cb_func_editor_action(guint key_id);
86 static gboolean cb_func_select_action(guint key_id);
87 static gboolean cb_func_format_action(guint key_id);
88 static gboolean cb_func_insert_action(guint key_id);
89 static gboolean cb_func_search_action(guint key_id);
90 static gboolean cb_func_goto_action(guint key_id);
91 static gboolean cb_func_switch_action(guint key_id);
92 static gboolean cb_func_clipboard_action(guint key_id);
93 static gboolean cb_func_build_action(guint key_id);
94 static gboolean cb_func_document_action(guint key_id);
95 static gboolean cb_func_view_action(guint key_id);
97 /* note: new keybindings should normally use per group callbacks */
98 static void cb_func_menu_help(guint key_id);
99 static void cb_func_menu_preferences(guint key_id);
101 static void cb_func_menu_fullscreen(guint key_id);
102 static void cb_func_menu_messagewindow(guint key_id);
104 static void cb_func_menu_opencolorchooser(guint key_id);
106 static void cb_func_switch_tableft(guint key_id);
107 static void cb_func_switch_tabright(guint key_id);
108 static void cb_func_switch_tablastused(guint key_id);
109 static void cb_func_move_tab(guint key_id);
111 static void add_popup_menu_accels(void);
114 /** Lookup a keybinding item.
115 * @param group Group.
116 * @param key_id Keybinding index for the group.
117 * @return The keybinding.
118 * @since 0.19. */
119 GeanyKeyBinding *keybindings_get_item(GeanyKeyGroup *group, gsize key_id)
121 g_assert(key_id < group->count);
123 return &group->keys[key_id];
127 /* This is used to set default keybindings on startup.
128 * Menu accels are set in apply_kb_accel(). */
129 /** Simple convenience function to fill a GeanyKeyBinding struct item.
130 * @param group Group.
131 * @param key_id Keybinding index for the group.
132 * @param callback Function to call when activated, or @c NULL to use the group callback.
133 * Usually it's better to use the group callback instead - see plugin_set_key_group().
134 * @param key (Lower case) default key, e.g. @c GDK_j, but usually 0 for unset.
135 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
136 * @param kf_name Key name for the configuration file, such as @c "menu_new".
137 * @param label Label used in the preferences dialog keybindings tab. May contain
138 * underscores - these won't be displayed.
139 * @param menu_item Optional widget to set an accelerator for, or @c NULL.
140 * @return The keybinding - normally this is ignored. */
141 GeanyKeyBinding *keybindings_set_item(GeanyKeyGroup *group, gsize key_id,
142 GeanyKeyCallback callback, guint key, GdkModifierType mod,
143 gchar *kf_name, gchar *label, GtkWidget *menu_item)
145 GeanyKeyBinding *kb = keybindings_get_item(group, key_id);
147 if (group->plugin)
149 /* some plugins e.g. GeanyLua need these fields duplicated */
150 setptr(kb->name, g_strdup(kf_name));
151 setptr(kb->label, g_strdup(label));
153 else
155 kb->name = kf_name;
156 kb->label = label;
158 kb->key = key;
159 kb->mods = mod;
160 kb->callback = callback;
161 kb->menu_item = menu_item;
162 return kb;
166 static GeanyKeyGroup *add_kb_group(GeanyKeyGroup *group,
167 const gchar *name, const gchar *label, gsize count, GeanyKeyBinding *keys,
168 GeanyKeyGroupCallback callback)
170 g_ptr_array_add(keybinding_groups, group);
172 group->name = name;
173 group->label = label;
174 group->count = count;
175 group->keys = keys;
176 group->callback = callback;
177 return group;
181 /* Lookup a widget in the main window */
182 #define LW(widget_name) \
183 ui_lookup_widget(main_widgets.window, G_STRINGIFY(widget_name))
185 /* Expansion for group_id = FILE:
186 * static GeanyKeyBinding FILE_keys[GEANY_KEYS_FILE_COUNT]; */
187 #define DECLARE_KEYS(group_id) \
188 static GeanyKeyBinding group_id ## _keys[GEANY_KEYS_ ## group_id ## _COUNT]
190 /* Expansion for group_id = FILE:
191 * add_kb_group(&groups[GEANY_KEY_GROUP_FILE], NULL, _("File menu"),
192 * GEANY_KEYS_FILE_COUNT, FILE_keys, callback); */
193 #define ADD_KB_GROUP(group_id, label, callback) \
194 add_kb_group(&groups[GEANY_KEY_GROUP_ ## group_id], keybindings_keyfile_group_name, label, \
195 GEANY_KEYS_ ## group_id ## _COUNT, group_id ## _keys, callback)
197 /* Init all fields of keys with default values.
198 * The menu_item field is always the main menu item, popup menu accelerators are
199 * set in add_popup_menu_accels(). */
200 static void init_default_kb(void)
202 static GeanyKeyGroup groups[GEANY_KEY_GROUP_COUNT];
203 GeanyKeyGroup *group;
204 DECLARE_KEYS(FILE);
205 DECLARE_KEYS(PROJECT);
206 DECLARE_KEYS(EDITOR);
207 DECLARE_KEYS(CLIPBOARD);
208 DECLARE_KEYS(SELECT);
209 DECLARE_KEYS(FORMAT);
210 DECLARE_KEYS(INSERT);
211 DECLARE_KEYS(SETTINGS);
212 DECLARE_KEYS(SEARCH);
213 DECLARE_KEYS(GOTO);
214 DECLARE_KEYS(VIEW);
215 DECLARE_KEYS(FOCUS);
216 DECLARE_KEYS(NOTEBOOK);
217 DECLARE_KEYS(DOCUMENT);
218 DECLARE_KEYS(BUILD);
219 DECLARE_KEYS(TOOLS);
220 DECLARE_KEYS(HELP);
222 group = ADD_KB_GROUP(FILE, _("File"), cb_func_file_action);
224 keybindings_set_item(group, GEANY_KEYS_FILE_NEW, NULL,
225 GDK_n, GDK_CONTROL_MASK, "menu_new", _("New"), NULL);
226 keybindings_set_item(group, GEANY_KEYS_FILE_OPEN, NULL,
227 GDK_o, GDK_CONTROL_MASK, "menu_open", _("Open"), NULL);
228 keybindings_set_item(group, GEANY_KEYS_FILE_OPENSELECTED, NULL,
229 GDK_o, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_open_selected",
230 _("Open selected file"), LW(menu_open_selected_file1));
231 keybindings_set_item(group, GEANY_KEYS_FILE_SAVE, NULL,
232 GDK_s, GDK_CONTROL_MASK, "menu_save", _("Save"), NULL);
233 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEAS, NULL,
234 0, 0, "menu_saveas", _("Save as"), LW(menu_save_as1));
235 keybindings_set_item(group, GEANY_KEYS_FILE_SAVEALL, NULL,
236 GDK_S, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "menu_saveall", _("Save all"),
237 LW(menu_save_all1));
238 keybindings_set_item(group, GEANY_KEYS_FILE_PRINT, NULL,
239 GDK_p, GDK_CONTROL_MASK, "menu_print", _("Print"), LW(print1));
240 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSE, NULL,
241 GDK_w, GDK_CONTROL_MASK, "menu_close", _("Close"), LW(menu_close1));
242 keybindings_set_item(group, GEANY_KEYS_FILE_CLOSEALL, NULL,
243 GDK_w, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_closeall", _("Close all"),
244 LW(menu_close_all1));
245 keybindings_set_item(group, GEANY_KEYS_FILE_RELOAD, NULL,
246 GDK_r, GDK_CONTROL_MASK, "menu_reloadfile", _("Reload file"), LW(menu_reload1));
248 group = ADD_KB_GROUP(PROJECT, _("Project"), cb_func_project_action);
250 keybindings_set_item(group, GEANY_KEYS_PROJECT_PROPERTIES, NULL,
251 0, 0, "project_properties", _("Project properties"), LW(project_properties1));
253 group = ADD_KB_GROUP(EDITOR, _("Editor"), cb_func_editor_action);
255 keybindings_set_item(group, GEANY_KEYS_EDITOR_UNDO, NULL,
256 GDK_z, GDK_CONTROL_MASK, "menu_undo", _("Undo"), LW(menu_undo2));
257 keybindings_set_item(group, GEANY_KEYS_EDITOR_REDO, NULL,
258 GDK_y, GDK_CONTROL_MASK, "menu_redo", _("Redo"), LW(menu_redo2));
259 keybindings_set_item(group, GEANY_KEYS_EDITOR_DUPLICATELINE, NULL,
260 GDK_d, GDK_CONTROL_MASK, "edit_duplicateline", _("_Duplicate Line or Selection"),
261 LW(duplicate_line_or_selection1));
262 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINE, NULL,
263 GDK_k, GDK_CONTROL_MASK, "edit_deleteline", _("_Delete Current Line(s)"),
264 LW(delete_current_line_s_1));
265 keybindings_set_item(group, GEANY_KEYS_EDITOR_DELETELINETOEND, NULL,
266 GDK_Delete, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_deletelinetoend",
267 _("Delete to line end"), NULL);
268 /* transpose may fit better in format group */
269 keybindings_set_item(group, GEANY_KEYS_EDITOR_TRANSPOSELINE, NULL,
270 GDK_t, GDK_CONTROL_MASK, "edit_transposeline", _("_Transpose Current Line"),
271 LW(transpose_current_line1));
272 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLTOLINE, NULL,
273 GDK_l, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "edit_scrolltoline", _("Scroll to current line"), NULL);
274 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEUP, NULL,
275 GDK_Up, GDK_MOD1_MASK, "edit_scrolllineup", _("Scroll up the view by one line"), NULL);
276 keybindings_set_item(group, GEANY_KEYS_EDITOR_SCROLLLINEDOWN, NULL,
277 GDK_Down, GDK_MOD1_MASK, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL);
278 keybindings_set_item(group, GEANY_KEYS_EDITOR_COMPLETESNIPPET, NULL,
279 GDK_Tab, 0, "edit_completesnippet", _("Complete snippet"), NULL);
280 keybindings_set_item(group, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR, NULL,
281 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL);
282 keybindings_set_item(group, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION, NULL,
283 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL);
284 keybindings_set_item(group, GEANY_KEYS_EDITOR_CONTEXTACTION, NULL,
285 0, 0, "popup_contextaction", _("Context Action"), NULL);
286 keybindings_set_item(group, GEANY_KEYS_EDITOR_AUTOCOMPLETE, NULL,
287 GDK_space, GDK_CONTROL_MASK, "edit_autocomplete", _("Complete word"), NULL);
288 keybindings_set_item(group, GEANY_KEYS_EDITOR_CALLTIP, NULL,
289 GDK_space, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_calltip", _("Show calltip"), NULL);
290 keybindings_set_item(group, GEANY_KEYS_EDITOR_MACROLIST, NULL,
291 GDK_Return, GDK_CONTROL_MASK, "edit_macrolist", _("Show macro list"), NULL);
292 keybindings_set_item(group, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION, NULL,
293 GDK_Tab, 0, "edit_wordpartcompletion", _("Word part completion"), NULL);
294 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEUP, NULL,
295 0, 0, "edit_movelineup", _("Move line(s) up"), NULL);
296 keybindings_set_item(group, GEANY_KEYS_EDITOR_MOVELINEDOWN, NULL,
297 0, 0, "edit_movelinedown", _("Move line(s) down"), NULL);
299 group = ADD_KB_GROUP(CLIPBOARD, _("Clipboard"), cb_func_clipboard_action);
301 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUT, NULL,
302 GDK_x, GDK_CONTROL_MASK, "menu_cut", _("Cut"), NULL);
303 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPY, NULL,
304 GDK_c, GDK_CONTROL_MASK, "menu_copy", _("Copy"), NULL);
305 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_PASTE, NULL,
306 GDK_v, GDK_CONTROL_MASK, "menu_paste", _("Paste"), NULL);
307 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_COPYLINE, NULL,
308 GDK_c, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_copyline", _("_Copy Current Line(s)"),
309 LW(cut_current_line_s_1));
310 keybindings_set_item(group, GEANY_KEYS_CLIPBOARD_CUTLINE, NULL,
311 GDK_x, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "edit_cutline", _("_Cut Current Line(s)"),
312 LW(copy_current_line_s_1));
314 group = ADD_KB_GROUP(SELECT, _("Select"), cb_func_select_action);
316 keybindings_set_item(group, GEANY_KEYS_SELECT_ALL, NULL,
317 GDK_a, GDK_CONTROL_MASK, "menu_selectall", _("Select All"), LW(menu_select_all1));
318 keybindings_set_item(group, GEANY_KEYS_SELECT_WORD, NULL,
319 GDK_w, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectword", _("Select current word"), NULL);
320 keybindings_set_item(group, GEANY_KEYS_SELECT_LINE, NULL,
321 GDK_l, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectline", _("_Select Current Line(s)"),
322 LW(select_current_line_s_1));
323 keybindings_set_item(group, GEANY_KEYS_SELECT_PARAGRAPH, NULL,
324 GDK_p, GDK_SHIFT_MASK | GDK_MOD1_MASK, "edit_selectparagraph", _("_Select Current Paragraph"),
325 LW(select_current_paragraph1));
326 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTLEFT, NULL,
327 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL);
328 keybindings_set_item(group, GEANY_KEYS_SELECT_WORDPARTRIGHT, NULL,
329 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL);
331 group = ADD_KB_GROUP(FORMAT, _("Format"), cb_func_format_action);
333 keybindings_set_item(group, GEANY_KEYS_FORMAT_TOGGLECASE, NULL,
334 GDK_u, GDK_CONTROL_MASK | GDK_MOD1_MASK, "edit_togglecase",
335 _("Toggle Case of Selection"), LW(menu_toggle_case2));
336 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE, NULL,
337 GDK_e, GDK_CONTROL_MASK, "edit_commentlinetoggle", _("Toggle line commentation"),
338 LW(menu_toggle_line_commentation1));
339 keybindings_set_item(group, GEANY_KEYS_FORMAT_COMMENTLINE, NULL,
340 0, 0, "edit_commentline", _("Comment line(s)"), LW(menu_comment_line1));
341 keybindings_set_item(group, GEANY_KEYS_FORMAT_UNCOMMENTLINE, NULL,
342 0, 0, "edit_uncommentline", _("Uncomment line(s)"), LW(menu_uncomment_line1));
343 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENT, NULL,
344 GDK_i, GDK_CONTROL_MASK, "edit_increaseindent", _("Increase indent"),
345 LW(menu_increase_indent1));
346 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENT, NULL,
347 GDK_u, GDK_CONTROL_MASK, "edit_decreaseindent", _("Decrease indent"),
348 LW(menu_decrease_indent1));
349 keybindings_set_item(group, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE, NULL,
350 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL);
351 keybindings_set_item(group, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE, NULL,
352 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL);
353 keybindings_set_item(group, GEANY_KEYS_FORMAT_AUTOINDENT, NULL,
354 0, 0, "edit_autoindent", _("_Smart Line Indent"), LW(smart_line_indent1));
355 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD1, NULL,
356 GDK_1, GDK_CONTROL_MASK, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL);
357 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD2, NULL,
358 GDK_2, GDK_CONTROL_MASK, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL);
359 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOCMD3, NULL,
360 GDK_3, GDK_CONTROL_MASK, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL);
361 /* may fit better in editor group */
362 keybindings_set_item(group, GEANY_KEYS_FORMAT_SENDTOVTE, NULL,
363 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), LW(send_selection_to_vte1));
364 keybindings_set_item(group, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH, NULL,
365 GDK_j, GDK_CONTROL_MASK, "format_reflowparagraph", _("_Reflow Lines/Block"),
366 LW(reflow_lines_block1));
368 group = ADD_KB_GROUP(INSERT, _("Insert"), cb_func_insert_action);
370 keybindings_set_item(group, GEANY_KEYS_INSERT_DATE, NULL,
371 GDK_d, GDK_SHIFT_MASK | GDK_MOD1_MASK, "menu_insert_date", _("Insert date"),
372 LW(insert_date_custom1));
373 keybindings_set_item(group, GEANY_KEYS_INSERT_ALTWHITESPACE, NULL,
374 0, 0, "edit_insertwhitespace", _("_Insert Alternative White Space"),
375 LW(insert_alternative_white_space1));
377 group = ADD_KB_GROUP(SETTINGS, _("Settings"), NULL);
379 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PREFERENCES, cb_func_menu_preferences,
380 GDK_p, GDK_CONTROL_MASK | GDK_MOD1_MASK, "menu_preferences", _("Preferences"),
381 LW(preferences1));
382 keybindings_set_item(group, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES, cb_func_menu_preferences,
383 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), LW(plugin_preferences1));
385 group = ADD_KB_GROUP(SEARCH, _("Search"), cb_func_search_action);
387 keybindings_set_item(group, GEANY_KEYS_SEARCH_FIND, NULL,
388 GDK_f, GDK_CONTROL_MASK, "menu_find", _("Find"), LW(find1));
389 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXT, NULL,
390 GDK_g, GDK_CONTROL_MASK, "menu_findnext", _("Find Next"), LW(find_next1));
391 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVIOUS, NULL,
392 GDK_g, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findprevious", _("Find Previous"),
393 LW(find_previous1));
394 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDNEXTSEL, NULL,
395 0, 0, "menu_findnextsel", _("Find Next Selection"), LW(find_nextsel1));
396 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDPREVSEL, NULL,
397 0, 0, "menu_findprevsel", _("Find Previous Selection"), LW(find_prevsel1));
398 keybindings_set_item(group, GEANY_KEYS_SEARCH_REPLACE, NULL,
399 GDK_h, GDK_CONTROL_MASK, "menu_replace", _("Replace"), LW(replace1));
400 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDINFILES, NULL, GDK_f,
401 GDK_CONTROL_MASK | GDK_SHIFT_MASK, "menu_findinfiles", _("Find in Files"),
402 LW(find_in_files1));
403 keybindings_set_item(group, GEANY_KEYS_SEARCH_NEXTMESSAGE, NULL,
404 0, 0, "menu_nextmessage", _("Next Message"), LW(next_message1));
405 keybindings_set_item(group, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE, NULL,
406 0, 0, "menu_previousmessage", _("Previous Message"), LW(previous_message1));
407 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDUSAGE, NULL,
408 0, 0, "popup_findusage", _("Find Usage"), NULL);
409 keybindings_set_item(group, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, NULL,
410 0, 0, "popup_finddocumentusage", _("Find Document Usage"), NULL);
411 keybindings_set_item(group, GEANY_KEYS_SEARCH_MARKALL, NULL,
412 GDK_m, GDK_CONTROL_MASK | GDK_SHIFT_MASK, "find_markall", _("Mark All"), NULL);
414 group = ADD_KB_GROUP(GOTO, _("Go to"), cb_func_goto_action);
416 keybindings_set_item(group, GEANY_KEYS_GOTO_BACK, NULL,
417 0, 0, "nav_back", _("Navigate back a location"), NULL);
418 keybindings_set_item(group, GEANY_KEYS_GOTO_FORWARD, NULL,
419 0, 0, "nav_forward", _("Navigate forward a location"), NULL);
420 keybindings_set_item(group, GEANY_KEYS_GOTO_LINE, NULL,
421 GDK_l, GDK_CONTROL_MASK, "menu_gotoline", _("Go to Line"), LW(go_to_line1));
422 keybindings_set_item(group, GEANY_KEYS_GOTO_MATCHINGBRACE, NULL,
423 GDK_b, GDK_CONTROL_MASK, "edit_gotomatchingbrace",
424 _("Go to matching brace"), NULL);
425 keybindings_set_item(group, GEANY_KEYS_GOTO_TOGGLEMARKER, NULL,
426 GDK_m, GDK_CONTROL_MASK, "edit_togglemarker",
427 _("Toggle marker"), NULL);
428 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTMARKER, NULL,
429 GDK_period, GDK_CONTROL_MASK, "edit_gotonextmarker",
430 _("_Go to Next Marker"), LW(go_to_next_marker1));
431 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVIOUSMARKER, NULL,
432 GDK_comma, GDK_CONTROL_MASK, "edit_gotopreviousmarker",
433 _("_Go to Previous Marker"), LW(go_to_previous_marker1));
434 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDEFINITION, NULL,
435 0, 0, "popup_gototagdefinition", _("Go to Tag Definition"), NULL);
436 keybindings_set_item(group, GEANY_KEYS_GOTO_TAGDECLARATION, NULL,
437 0, 0, "popup_gototagdeclaration", _("Go to Tag Declaration"), NULL);
438 keybindings_set_item(group, GEANY_KEYS_GOTO_LINESTART, NULL,
439 GDK_Home, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL);
440 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEEND, NULL,
441 GDK_End, 0, "edit_gotolineend", _("Go to End of Line"), NULL);
442 keybindings_set_item(group, GEANY_KEYS_GOTO_LINEENDVISUAL, NULL,
443 GDK_End, GDK_MOD1_MASK, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL);
444 keybindings_set_item(group, GEANY_KEYS_GOTO_PREVWORDPART, NULL,
445 GDK_slash, GDK_CONTROL_MASK, "edit_prevwordstart", _("Go to Previous Word Part"), NULL);
446 keybindings_set_item(group, GEANY_KEYS_GOTO_NEXTWORDPART, NULL,
447 GDK_backslash, GDK_CONTROL_MASK, "edit_nextwordstart", _("Go to Next Word Part"), NULL);
449 group = ADD_KB_GROUP(VIEW, _("View"), cb_func_view_action);
451 keybindings_set_item(group, GEANY_KEYS_VIEW_TOGGLEALL, NULL,
452 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
453 LW(menu_toggle_all_additional_widgets1));
454 keybindings_set_item(group, GEANY_KEYS_VIEW_FULLSCREEN, cb_func_menu_fullscreen,
455 GDK_F11, 0, "menu_fullscreen", _("Fullscreen"), LW(menu_fullscreen1));
456 keybindings_set_item(group, GEANY_KEYS_VIEW_MESSAGEWINDOW, cb_func_menu_messagewindow,
457 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
458 LW(menu_show_messages_window1));
459 keybindings_set_item(group, GEANY_KEYS_VIEW_SIDEBAR, NULL,
460 0, 0, "toggle_sidebar", _("Toggle Sidebar"), LW(menu_show_sidebar1));
461 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMIN, NULL,
462 GDK_plus, GDK_CONTROL_MASK, "menu_zoomin", _("Zoom In"), LW(menu_zoom_in1));
463 keybindings_set_item(group, GEANY_KEYS_VIEW_ZOOMOUT, NULL,
464 GDK_minus, GDK_CONTROL_MASK, "menu_zoomout", _("Zoom Out"), LW(menu_zoom_out1));
466 group = ADD_KB_GROUP(FOCUS, _("Focus"), cb_func_switch_action);
468 keybindings_set_item(group, GEANY_KEYS_FOCUS_EDITOR, NULL,
469 GDK_F2, 0, "switch_editor", _("Switch to Editor"), NULL);
470 keybindings_set_item(group, GEANY_KEYS_FOCUS_SCRIBBLE, NULL,
471 GDK_F6, 0, "switch_scribble", _("Switch to Scribble"), NULL);
472 keybindings_set_item(group, GEANY_KEYS_FOCUS_VTE, NULL,
473 GDK_F4, 0, "switch_vte", _("Switch to VTE"), NULL);
474 keybindings_set_item(group, GEANY_KEYS_FOCUS_SEARCHBAR, NULL,
475 GDK_F7, 0, "switch_search_bar", _("Switch to Search Bar"), NULL);
476 keybindings_set_item(group, GEANY_KEYS_FOCUS_SIDEBAR, NULL,
477 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL);
478 keybindings_set_item(group, GEANY_KEYS_FOCUS_COMPILER, NULL,
479 0, 0, "switch_compiler", _("Switch to Compiler"), NULL);
480 keybindings_set_item(group, GEANY_KEYS_FOCUS_MESSAGES, NULL,
481 0, 0, "switch_messages", _("Switch to Messages"), NULL);
483 group = ADD_KB_GROUP(NOTEBOOK, _("Notebook tab"), NULL);
485 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT, cb_func_switch_tableft,
486 GDK_Page_Up, GDK_CONTROL_MASK, "switch_tableft", _("Switch to left document"), NULL);
487 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT, cb_func_switch_tabright,
488 GDK_Page_Down, GDK_CONTROL_MASK, "switch_tabright", _("Switch to right document"), NULL);
489 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED, cb_func_switch_tablastused,
490 GDK_Tab, GDK_CONTROL_MASK, "switch_tablastused", _("Switch to last used document"), NULL);
491 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLEFT, cb_func_move_tab,
492 GDK_Page_Up, GDK_MOD1_MASK, "move_tableft", _("Move document left"), NULL);
493 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT, cb_func_move_tab,
494 GDK_Page_Down, GDK_MOD1_MASK, "move_tabright", _("Move document right"), NULL);
495 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABFIRST, cb_func_move_tab,
496 0, 0, "move_tabfirst", _("Move document first"), NULL);
497 keybindings_set_item(group, GEANY_KEYS_NOTEBOOK_MOVETABLAST, cb_func_move_tab,
498 0, 0, "move_tablast", _("Move document last"), NULL);
500 group = ADD_KB_GROUP(DOCUMENT, _("Document"), cb_func_document_action);
502 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEWRAP, NULL,
503 0, 0, "menu_linewrap", _("Toggle Line wrapping"), LW(menu_line_wrapping1));
504 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_LINEBREAK, NULL,
505 0, 0, "menu_linebreak", _("Toggle Line breaking"), LW(line_breaking1));
506 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACETABS, NULL,
507 0, 0, "menu_replacetabs", _("Replace tabs by space"), LW(menu_replace_tabs));
508 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_REPLACESPACES, NULL,
509 0, 0, "menu_replacespaces", _("Replace spaces by tabs"), LW(menu_replace_spaces));
510 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_TOGGLEFOLD, NULL,
511 0, 0, "menu_togglefold", _("Toggle current fold"), NULL);
512 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_FOLDALL, NULL,
513 0, 0, "menu_foldall", _("Fold all"), LW(menu_fold_all1));
514 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_UNFOLDALL, NULL,
515 0, 0, "menu_unfoldall", _("Unfold all"), LW(menu_unfold_all1));
516 keybindings_set_item(group, GEANY_KEYS_DOCUMENT_RELOADTAGLIST, NULL,
517 GDK_r, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "reloadtaglist", _("Reload symbol list"), NULL);
519 group = ADD_KB_GROUP(BUILD, _("Build"), cb_func_build_action);
521 keybindings_set_item(group, GEANY_KEYS_BUILD_COMPILE, NULL,
522 GDK_F8, 0, "build_compile", _("Compile"), NULL);
523 keybindings_set_item(group, GEANY_KEYS_BUILD_LINK, NULL,
524 GDK_F9, 0, "build_link", _("Build"), NULL);
525 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKE, NULL,
526 GDK_F9, GDK_SHIFT_MASK, "build_make", _("Make all"), NULL);
527 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOWNTARGET, NULL,
528 GDK_F9, GDK_SHIFT_MASK | GDK_CONTROL_MASK, "build_makeowntarget",
529 _("Make custom target"), NULL);
530 keybindings_set_item(group, GEANY_KEYS_BUILD_MAKEOBJECT, NULL,
531 0, 0, "build_makeobject", _("Make object"), NULL);
532 keybindings_set_item(group, GEANY_KEYS_BUILD_NEXTERROR, NULL,
533 0, 0, "build_nexterror", _("Next error"), NULL);
534 keybindings_set_item(group, GEANY_KEYS_BUILD_PREVIOUSERROR, NULL,
535 0, 0, "build_previouserror", _("Previous error"), NULL);
536 keybindings_set_item(group, GEANY_KEYS_BUILD_RUN, NULL,
537 GDK_F5, 0, "build_run", _("Run"), NULL);
538 keybindings_set_item(group, GEANY_KEYS_BUILD_OPTIONS, NULL,
539 0, 0, "build_options", _("Build options"), NULL);
541 group = ADD_KB_GROUP(TOOLS, _("Tools"), NULL);
543 keybindings_set_item(group, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER, cb_func_menu_opencolorchooser,
544 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), LW(menu_choose_color1));
546 group = ADD_KB_GROUP(HELP, _("Help"), NULL);
548 keybindings_set_item(group, GEANY_KEYS_HELP_HELP, cb_func_menu_help,
549 GDK_F1, 0, "menu_help", _("Help"), LW(help1));
553 /* before the tab changes, add the current document to the MRU list */
554 static void on_notebook_switch_page(void)
556 GeanyDocument *old = document_get_current();
558 /* when closing current doc, old is NULL.
559 * Don't add to the mru list when switch dialog is visible. */
560 if (old && switch_dialog_cancelled)
562 g_queue_remove(mru_docs, old);
563 g_queue_push_head(mru_docs, old);
565 if (g_queue_get_length(mru_docs) > MAX_MRU_DOCS)
566 g_queue_pop_tail(mru_docs);
571 /* really this should be just after a document was closed, not idle */
572 static gboolean on_idle_close(gpointer data)
574 GeanyDocument *current;
576 current = document_get_current();
577 if (current && g_queue_peek_head(mru_docs) == current)
578 g_queue_pop_head(mru_docs);
580 return FALSE;
584 static void on_document_close(GObject *obj, GeanyDocument *doc)
586 if (! main_status.quitting)
588 g_queue_remove(mru_docs, doc);
589 g_idle_add(on_idle_close, NULL);
594 void keybindings_init(void)
596 mru_docs = g_queue_new();
597 g_signal_connect(main_widgets.notebook, "switch-page",
598 G_CALLBACK(on_notebook_switch_page), NULL);
599 g_signal_connect(geany_object, "document-close",
600 G_CALLBACK(on_document_close), NULL);
602 keybinding_groups = g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT);
604 kb_accel_group = gtk_accel_group_new();
606 init_default_kb();
608 gtk_window_add_accel_group(GTK_WINDOW(main_widgets.window), kb_accel_group);
610 g_signal_connect(main_widgets.window, "key-press-event", G_CALLBACK(on_key_press_event), NULL);
611 /* in case the switch dialog misses an event while drawing the dialog */
612 g_signal_connect(main_widgets.window, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
616 typedef void (*KBItemCallback) (GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data);
618 static void keybindings_foreach(KBItemCallback cb, gpointer user_data)
620 gsize g, i;
621 GeanyKeyGroup *group;
622 GeanyKeyBinding *kb;
624 for (g = 0; g < keybinding_groups->len; g++)
626 group = g_ptr_array_index(keybinding_groups, g);
627 for (i = 0; i < group->count; i++)
629 kb = &group->keys[i];
631 cb(group, kb, user_data);
637 static void load_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
639 GKeyFile *config = user_data;
640 gchar *val;
641 guint key;
642 GdkModifierType mods;
644 val = g_key_file_get_string(config, group->name, kb->name, NULL);
645 if (val != NULL)
647 gtk_accelerator_parse(val, &key, &mods);
648 kb->key = key;
649 kb->mods = mods;
650 g_free(val);
655 static void load_user_kb(void)
657 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
658 GKeyFile *config = g_key_file_new();
660 /* now load user defined keys */
661 if (g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
663 keybindings_foreach(load_kb, config);
665 g_free(configfile);
666 g_key_file_free(config);
670 static void apply_kb_accel(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
672 if (kb->key != 0 && kb->menu_item)
674 gtk_widget_add_accelerator(kb->menu_item, "activate", kb_accel_group,
675 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
680 void keybindings_load_keyfile(void)
682 load_user_kb();
683 add_popup_menu_accels();
685 /* set menu accels now, after user keybindings have been read */
686 keybindings_foreach(apply_kb_accel, NULL);
690 static void add_menu_accel(GeanyKeyGroup *group, guint kb_id, GtkWidget *menuitem)
692 GeanyKeyBinding *kb = &group->keys[kb_id];
694 if (kb->key != 0)
695 gtk_widget_add_accelerator(menuitem, "activate", kb_accel_group,
696 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
700 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
701 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
703 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
704 static void add_popup_menu_accels(void)
706 GeanyKeyGroup *group;
708 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_EDITOR);
709 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO, undo1);
710 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO, redo1);
711 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION, context_action1);
713 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_SELECT);
714 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL, menu_select_all2);
716 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_INSERT);
717 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE, insert_date_custom2);
719 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_FILE);
720 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED, menu_open_selected_file2);
722 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_SEARCH);
723 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE, find_usage1);
724 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE, find_document_usage1);
726 group = g_ptr_array_index(keybinding_groups, GEANY_KEY_GROUP_GOTO);
727 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_LINE, go_to_line);
728 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION, goto_tag_definition1);
729 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDECLARATION, goto_tag_declaration1);
731 /* Format and Commands share the menu bar submenus */
732 /* Build menu items are set if the build menus are created */
736 static void set_keyfile_kb(GeanyKeyGroup *group, GeanyKeyBinding *kb, gpointer user_data)
738 GKeyFile *config = user_data;
739 gchar *val;
741 val = gtk_accelerator_name(kb->key, kb->mods);
742 g_key_file_set_string(config, group->name, kb->name, val);
743 g_free(val);
747 /* just write the content of the keys array to the config file */
748 void keybindings_write_to_file(void)
750 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "keybindings.conf", NULL);
751 gchar *data;
752 GKeyFile *config = g_key_file_new();
754 /* add comment if the file is newly created */
755 if (! g_key_file_load_from_file(config, configfile, G_KEY_FILE_KEEP_COMMENTS, NULL))
757 g_key_file_set_comment(config, NULL, NULL,
758 "Keybindings for Geany\nThe format looks like \"<Control>a\" or \"<Shift><Alt>F1\".\n"
759 "But you can also change the keys in Geany's preferences dialog.", NULL);
762 keybindings_foreach(set_keyfile_kb, config);
764 /* write the file */
765 data = g_key_file_to_data(config, NULL, NULL);
766 utils_write_file(configfile, data);
768 g_free(data);
769 g_free(configfile);
770 g_key_file_free(config);
774 void keybindings_free(void)
776 g_ptr_array_free(keybinding_groups, TRUE);
777 g_queue_free(mru_docs);
781 gchar *keybindings_get_label(GeanyKeyBinding *kb)
783 return utils_str_remove_chars(g_strdup(kb->label), "_");
787 static void fill_shortcut_labels_treeview(GtkWidget *tree)
789 gsize g, i;
790 GeanyKeyBinding *kb;
791 GeanyKeyGroup *group;
792 GtkListStore *store;
793 GtkTreeIter iter;
795 store = gtk_list_store_new(3, G_TYPE_STRING, G_TYPE_STRING, PANGO_TYPE_WEIGHT);
797 for (g = 0; g < keybinding_groups->len; g++)
799 group = g_ptr_array_index(keybinding_groups, g);
801 if (g > 0)
803 gtk_list_store_append(store, &iter);
804 gtk_list_store_set(store, &iter, -1);
807 gtk_list_store_append(store, &iter);
808 gtk_list_store_set(store, &iter, 0, group->label, 2, PANGO_WEIGHT_BOLD, -1);
810 for (i = 0; i < group->count; i++)
812 gchar *shortcut, *label;
814 kb = &group->keys[i];
815 label = keybindings_get_label(kb);
816 shortcut = gtk_accelerator_get_label(kb->key, kb->mods);
818 gtk_list_store_append(store, &iter);
819 gtk_list_store_set(store, &iter, 0, label, 1, shortcut, 2, PANGO_WEIGHT_NORMAL, -1);
821 g_free(shortcut);
822 g_free(label);
826 gtk_tree_view_set_model(GTK_TREE_VIEW(tree), GTK_TREE_MODEL(store));
827 g_object_unref(store);
831 static GtkWidget *create_dialog(void)
833 GtkWidget *dialog, *tree, *label, *swin, *vbox;
834 GtkCellRenderer *text_renderer;
835 GtkTreeViewColumn *column;
836 gint height;
838 dialog = gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets.window),
839 GTK_DIALOG_DESTROY_WITH_PARENT,
840 GTK_STOCK_EDIT, GTK_RESPONSE_APPLY,
841 GTK_STOCK_CLOSE, GTK_RESPONSE_CANCEL, NULL);
842 vbox = ui_dialog_vbox_new(GTK_DIALOG(dialog));
843 gtk_box_set_spacing(GTK_BOX(vbox), 6);
844 gtk_widget_set_name(dialog, "GeanyDialog");
846 height = GEANY_WINDOW_MINIMAL_HEIGHT;
847 gtk_window_set_default_size(GTK_WINDOW(dialog), -1, height);
849 gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_CANCEL);
851 label = gtk_label_new(_("The following keyboard shortcuts are configurable:"));
852 gtk_misc_set_alignment(GTK_MISC(label), 0, 0.5);
854 tree = gtk_tree_view_new();
855 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree), TRUE);
856 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree), FALSE);
858 text_renderer = gtk_cell_renderer_text_new();
859 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
860 column = gtk_tree_view_column_new_with_attributes(
861 NULL, text_renderer, "text", 0, "weight", 2, NULL);
862 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
864 text_renderer = gtk_cell_renderer_text_new();
865 column = gtk_tree_view_column_new_with_attributes(NULL, text_renderer, "text", 1, NULL);
866 gtk_tree_view_append_column(GTK_TREE_VIEW(tree), column);
868 fill_shortcut_labels_treeview(tree);
870 swin = gtk_scrolled_window_new(NULL, NULL);
871 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin), GTK_POLICY_NEVER,
872 GTK_POLICY_AUTOMATIC);
873 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin), GTK_SHADOW_ETCHED_IN);
874 gtk_container_add(GTK_CONTAINER(swin), tree);
876 gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 6);
877 gtk_box_pack_start(GTK_BOX(vbox), swin, TRUE, TRUE, 0);
879 return dialog;
883 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
884 static GtkWidget *key_dialog = NULL;
886 static void on_dialog_response(GtkWidget *dialog, gint response, gpointer user_data)
888 if (response == GTK_RESPONSE_APPLY)
890 GtkWidget *wid;
892 prefs_show_dialog();
893 /* select the KB page */
894 wid = ui_lookup_widget(ui_widgets.prefs_dialog, "frame22");
895 if (wid != NULL)
897 GtkNotebook *nb = GTK_NOTEBOOK(ui_lookup_widget(ui_widgets.prefs_dialog, "notebook2"));
899 if (nb != NULL)
900 gtk_notebook_set_current_page(nb, gtk_notebook_page_num(nb, wid));
903 gtk_widget_destroy(dialog);
904 key_dialog = NULL;
908 void keybindings_show_shortcuts(void)
910 if (key_dialog)
911 gtk_widget_destroy(key_dialog); /* in case the key_dialog is still visible */
913 key_dialog = create_dialog();
914 g_signal_connect(key_dialog, "response", G_CALLBACK(on_dialog_response), NULL);
915 gtk_widget_show_all(key_dialog);
919 static gboolean check_fixed_kb(guint keyval, guint state)
921 /* check alt-0 to alt-9 for setting current notebook page */
922 if (state & GDK_MOD1_MASK && keyval >= GDK_0 && keyval <= GDK_9)
924 gint page = keyval - GDK_0 - 1;
925 gint npages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
927 /* alt-0 is for the rightmost tab */
928 if (keyval == GDK_0)
929 page = npages - 1;
930 /* invert the order if tabs are added on the other side */
931 if (swap_alt_tab_order && ! file_prefs.tab_order_ltr)
932 page = (npages - 1) - page;
934 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), page);
935 return TRUE;
937 if (keyval == GDK_Page_Up || keyval == GDK_Page_Down)
939 /* switch to first or last document */
940 if (state == (GDK_CONTROL_MASK | GDK_SHIFT_MASK))
942 if (keyval == GDK_Page_Up)
943 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), 0);
944 if (keyval == GDK_Page_Down)
945 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
946 gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) - 1);
947 return TRUE;
950 return FALSE;
954 static gboolean check_snippet_completion(GeanyDocument *doc)
956 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
958 g_return_val_if_fail(doc, FALSE);
960 /* keybinding only valid when scintilla widget has focus */
961 if (focusw == GTK_WIDGET(doc->editor->sci))
963 ScintillaObject *sci = doc->editor->sci;
964 gint pos = sci_get_current_position(sci);
966 if (editor_prefs.complete_snippets)
967 return editor_complete_snippet(doc->editor, pos);
969 return FALSE;
973 /* Transforms a GdkEventKey event into a GdkEventButton event */
974 static void trigger_button_event(GtkWidget *widget, guint32 event_time)
976 GdkEventButton *event;
977 gboolean ret;
979 event = g_new0(GdkEventButton, 1);
981 if (GTK_IS_TEXT_VIEW(widget))
982 event->window = gtk_text_view_get_window(GTK_TEXT_VIEW(widget), GTK_TEXT_WINDOW_TEXT);
983 else
984 event->window = widget->window;
985 event->time = event_time;
986 event->type = GDK_BUTTON_PRESS;
987 event->button = 3;
989 g_signal_emit_by_name(widget, "button-press-event", event, &ret);
990 g_signal_emit_by_name(widget, "button-release-event", event, &ret);
992 g_free(event);
996 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
997 * widgets. Without this special handling, the notebook tab list of the documents' notebook
998 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
999 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1000 * notebook tab list. */
1001 static gboolean check_menu_key(GeanyDocument *doc, guint keyval, guint state, guint32 event_time)
1003 if ((keyval == GDK_Menu && state == 0) || (keyval == GDK_F10 && state == GDK_SHIFT_MASK))
1005 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1006 static GtkWidget *scribble = NULL;
1008 if (scribble == NULL)
1009 scribble = ui_lookup_widget(main_widgets.window, "textview_scribble");
1011 if (doc != NULL)
1013 if (focusw == doc->priv->tag_tree)
1015 trigger_button_event(focusw, event_time);
1016 return TRUE;
1018 if (focusw == GTK_WIDGET(doc->editor->sci))
1020 if (keyval == GDK_Menu)
1021 { /* show editor popup menu */
1022 trigger_button_event(focusw, event_time);
1023 return TRUE;
1025 else
1026 { /* show tab bar menu */
1027 trigger_button_event(main_widgets.notebook, event_time);
1028 return TRUE;
1032 if (focusw == tv.tree_openfiles
1033 || focusw == msgwindow.tree_status
1034 || focusw == msgwindow.tree_compiler
1035 || focusw == msgwindow.tree_msg
1036 || focusw == scribble
1037 #ifdef HAVE_VTE
1038 || (vte_info.have_vte && focusw == vc->vte)
1039 #endif
1042 trigger_button_event(focusw, event_time);
1043 return TRUE;
1046 return FALSE;
1050 #ifdef HAVE_VTE
1051 static gboolean on_menu_expose_event(GtkWidget *widget, GdkEventExpose *event,
1052 gpointer user_data)
1054 if (!GTK_WIDGET_SENSITIVE(widget))
1055 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1056 return FALSE;
1060 static gboolean set_sensitive(gpointer widget)
1062 gtk_widget_set_sensitive(GTK_WIDGET(widget), TRUE);
1063 return FALSE;
1067 static gboolean check_vte(GdkModifierType state, guint keyval)
1069 guint i;
1070 GtkWidget *widget;
1072 if (! vc->enable_bash_keys)
1073 return FALSE;
1074 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != vc->vte)
1075 return FALSE;
1076 /* prevent menubar flickering: */
1077 if (state == GDK_SHIFT_MASK && (keyval >= GDK_a && keyval <= GDK_z))
1078 return FALSE;
1079 if (state == 0 && (keyval < GDK_F1 || keyval > GDK_F35)) /* e.g. backspace */
1080 return FALSE;
1082 /* make focus commands override any bash commands */
1083 for (i = 0; i < GEANY_KEYS_FOCUS_COUNT; i++)
1085 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_FOCUS, i);
1087 if (state == kb->mods && keyval == kb->key)
1088 return FALSE;
1091 /* Temporarily disable the menus to prevent conflicting menu accelerators
1092 * from overriding the VTE bash shortcuts.
1093 * Note: maybe there's a better way of doing this ;-) */
1094 widget = ui_lookup_widget(main_widgets.window, "menubar1");
1095 gtk_widget_set_sensitive(widget, FALSE);
1097 /* make the menubar sensitive before it is redrawn */
1098 static gboolean connected = FALSE;
1099 if (!connected)
1100 g_signal_connect(widget, "expose-event", G_CALLBACK(on_menu_expose_event), NULL);
1103 widget = main_widgets.editor_menu;
1104 gtk_widget_set_sensitive(widget, FALSE);
1105 g_idle_add(set_sensitive, widget);
1106 return TRUE;
1108 #endif
1111 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1112 static guint key_kp_translate(guint key_in)
1114 switch (key_in)
1116 case GDK_KP_Down:
1117 return GDK_Down;
1118 case GDK_KP_Up:
1119 return GDK_Up;
1120 case GDK_KP_Left:
1121 return GDK_Left;
1122 case GDK_KP_Right:
1123 return GDK_Right;
1124 case GDK_KP_Home:
1125 return GDK_Home;
1126 case GDK_KP_End:
1127 return GDK_End;
1128 case GDK_KP_Page_Up:
1129 return GDK_Page_Up;
1130 case GDK_KP_Page_Down:
1131 return GDK_Page_Down;
1132 case GDK_KP_Delete:
1133 return GDK_Delete;
1134 case GDK_KP_Insert:
1135 return GDK_Insert;
1136 default:
1137 return key_in;
1142 /* Stripped down version of the main keypress event handler which can be used
1143 * to process foreign events. Instead of executing the keybinding, a pointer to the
1144 * keybinding structure is returned.
1145 * Additionally, the group_id and binding_id are filled with the appropriate indexes
1146 * if non-NULL. */
1147 const GeanyKeyBinding *keybindings_check_event(GdkEventKey *ev, gint *group_id, gint *binding_id)
1149 guint state, keyval;
1150 gsize g, i;
1151 GeanyKeyGroup *group;
1152 GeanyKeyBinding *kb;
1154 if (ev->keyval == 0)
1155 return FALSE;
1157 keyval = ev->keyval;
1158 state = ev->state & gtk_accelerator_get_default_mod_mask();
1159 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1160 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1161 if (keyval >= GDK_A && keyval <= GDK_Z)
1162 keyval += GDK_a - GDK_A;
1164 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1165 keyval = key_kp_translate(keyval);
1167 for (g = 0; g < keybinding_groups->len; g++)
1169 group = g_ptr_array_index(keybinding_groups, g);
1171 for (i = 0; i < group->count; i++)
1173 kb = &group->keys[i];
1174 if (keyval == kb->key && state == kb->mods)
1176 if (group_id != NULL)
1177 *group_id = g;
1178 if (binding_id != NULL)
1179 *binding_id = i;
1180 return kb;
1184 return NULL;
1188 /* central keypress event handler, almost all keypress events go to this function */
1189 static gboolean on_key_press_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1191 guint state, keyval;
1192 gsize g, i;
1193 GeanyDocument *doc;
1194 GeanyKeyGroup *group;
1195 GeanyKeyBinding *kb;
1197 if (ev->keyval == 0)
1198 return FALSE;
1200 doc = document_get_current();
1201 if (doc)
1202 document_check_disk_status(doc, FALSE);
1204 keyval = ev->keyval;
1205 state = ev->state & gtk_accelerator_get_default_mod_mask();
1206 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1207 if ((ev->state & GDK_SHIFT_MASK) || (ev->state & GDK_LOCK_MASK))
1208 if (keyval >= GDK_A && keyval <= GDK_Z)
1209 keyval += GDK_a - GDK_A;
1211 if (keyval >= GDK_KP_Space && keyval < GDK_KP_Equal)
1212 keyval = key_kp_translate(keyval);
1214 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1216 /* special cases */
1217 #ifdef HAVE_VTE
1218 if (vte_info.have_vte && check_vte(state, keyval))
1219 return FALSE;
1220 #endif
1221 if (check_menu_key(doc, keyval, state, ev->time))
1222 return TRUE;
1224 for (g = 0; g < keybinding_groups->len; g++)
1226 group = g_ptr_array_index(keybinding_groups, g);
1228 for (i = 0; i < group->count; i++)
1230 kb = &group->keys[i];
1231 if (keyval == kb->key && state == kb->mods)
1233 /* call the corresponding callback function for this shortcut */
1234 if (kb->callback)
1236 kb->callback(i);
1237 return TRUE;
1239 else if (group->callback)
1241 if (group->callback(i))
1242 return TRUE;
1243 else
1244 continue; /* not handled */
1246 g_warning("No callback for keybinding %s: %s!", group->name, kb->name);
1250 /* fixed keybindings can be overridden by user bindings, so check them last */
1251 if (check_fixed_kb(keyval, state))
1252 return TRUE;
1253 return FALSE;
1257 static gboolean is_modifier_key(guint keyval)
1259 switch (keyval)
1261 case GDK_Shift_L:
1262 case GDK_Shift_R:
1263 case GDK_Control_L:
1264 case GDK_Control_R:
1265 case GDK_Meta_L:
1266 case GDK_Meta_R:
1267 case GDK_Alt_L:
1268 case GDK_Alt_R:
1269 case GDK_Super_L:
1270 case GDK_Super_R:
1271 case GDK_Hyper_L:
1272 case GDK_Hyper_R:
1273 return TRUE;
1274 default:
1275 return FALSE;
1280 GeanyKeyBinding *keybindings_lookup_item(guint group_id, guint key_id)
1282 GeanyKeyGroup *group;
1284 g_return_val_if_fail(group_id < keybinding_groups->len, NULL);
1286 group = g_ptr_array_index(keybinding_groups, group_id);
1288 g_return_val_if_fail(group, NULL);
1289 g_return_val_if_fail(key_id < group->count, NULL);
1291 return &group->keys[key_id];
1295 /** Mimic a (built-in only) keybinding action.
1296 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1297 * @param group_id The index for the key group that contains the @a key_id keybinding.
1298 * @param key_id The keybinding command index. */
1299 void keybindings_send_command(guint group_id, guint key_id)
1301 GeanyKeyBinding *kb;
1303 g_return_if_fail(group_id < GEANY_KEY_GROUP_COUNT); /* can't use this for plugin groups */
1305 kb = keybindings_lookup_item(group_id, key_id);
1306 if (kb)
1308 if (kb->callback)
1309 kb->callback(key_id);
1310 else
1312 GeanyKeyGroup *group = g_ptr_array_index(keybinding_groups, group_id);
1314 if (group->callback)
1315 group->callback(key_id);
1321 /* These are the callback functions, either each group or each shortcut has it's
1322 * own function. */
1325 static gboolean cb_func_file_action(guint key_id)
1327 switch (key_id)
1329 case GEANY_KEYS_FILE_NEW:
1330 document_new_file(NULL, NULL, NULL);
1331 break;
1332 case GEANY_KEYS_FILE_OPEN:
1333 on_open1_activate(NULL, NULL);
1334 break;
1335 case GEANY_KEYS_FILE_OPENSELECTED:
1336 on_menu_open_selected_file1_activate(NULL, NULL);
1337 break;
1338 case GEANY_KEYS_FILE_SAVE:
1339 on_save1_activate(NULL, NULL);
1340 break;
1341 case GEANY_KEYS_FILE_SAVEAS:
1342 on_save_as1_activate(NULL, NULL);
1343 break;
1344 case GEANY_KEYS_FILE_SAVEALL:
1345 on_save_all1_activate(NULL, NULL);
1346 break;
1347 case GEANY_KEYS_FILE_CLOSE:
1348 on_close1_activate(NULL, NULL);
1349 break;
1350 case GEANY_KEYS_FILE_CLOSEALL:
1351 on_close_all1_activate(NULL, NULL);
1352 break;
1353 case GEANY_KEYS_FILE_RELOAD:
1354 on_toolbutton_reload_clicked(NULL, NULL);
1355 break;
1356 case GEANY_KEYS_FILE_PRINT:
1357 on_print1_activate(NULL, NULL);
1358 break;
1360 return TRUE;
1364 static gboolean cb_func_project_action(guint key_id)
1366 switch (key_id)
1368 case GEANY_KEYS_PROJECT_PROPERTIES:
1369 if (app->project)
1370 on_project_properties1_activate(NULL, NULL);
1371 break;
1373 return TRUE;
1377 static void cb_func_menu_preferences(guint key_id)
1379 switch (key_id)
1381 case GEANY_KEYS_SETTINGS_PREFERENCES:
1382 on_preferences1_activate(NULL, NULL);
1383 break;
1384 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES:
1385 on_plugin_preferences1_activate(NULL, NULL);
1386 break;
1391 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id)
1393 on_help1_activate(NULL, NULL);
1397 static gboolean cb_func_search_action(guint key_id)
1399 GeanyDocument *doc = document_get_current();
1400 ScintillaObject *sci;
1402 if (key_id == GEANY_KEYS_SEARCH_FINDINFILES)
1404 on_find_in_files1_activate(NULL, NULL); /* works without docs too */
1405 return TRUE;
1407 if (!doc)
1408 return TRUE;
1409 sci = doc->editor->sci;
1411 switch (key_id)
1413 case GEANY_KEYS_SEARCH_FIND:
1414 on_find1_activate(NULL, NULL); break;
1415 case GEANY_KEYS_SEARCH_FINDNEXT:
1416 on_find_next1_activate(NULL, NULL); break;
1417 case GEANY_KEYS_SEARCH_FINDPREVIOUS:
1418 on_find_previous1_activate(NULL, NULL); break;
1419 case GEANY_KEYS_SEARCH_FINDPREVSEL:
1420 on_find_prevsel1_activate(NULL, NULL); break;
1421 case GEANY_KEYS_SEARCH_FINDNEXTSEL:
1422 on_find_nextsel1_activate(NULL, NULL); break;
1423 case GEANY_KEYS_SEARCH_REPLACE:
1424 on_replace1_activate(NULL, NULL); break;
1425 case GEANY_KEYS_SEARCH_NEXTMESSAGE:
1426 on_next_message1_activate(NULL, NULL); break;
1427 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE:
1428 on_previous_message1_activate(NULL, NULL); break;
1429 case GEANY_KEYS_SEARCH_FINDUSAGE:
1430 read_current_word(doc);
1431 on_find_usage1_activate(NULL, NULL);
1432 break;
1433 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE:
1434 read_current_word(doc);
1435 on_find_document_usage1_activate(NULL, NULL);
1436 break;
1437 case GEANY_KEYS_SEARCH_MARKALL:
1439 gchar *text = get_current_word_or_sel(doc);
1441 if (sci_has_selection(sci))
1442 search_mark_all(doc, text, SCFIND_MATCHCASE);
1443 else
1445 /* clears markers if text is null */
1446 search_mark_all(doc, text, SCFIND_MATCHCASE | SCFIND_WHOLEWORD);
1448 g_free(text);
1449 break;
1452 return TRUE;
1456 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id)
1458 on_show_color_chooser1_activate(NULL, NULL);
1462 static gboolean cb_func_view_action(guint key_id)
1464 switch (key_id)
1466 case GEANY_KEYS_VIEW_TOGGLEALL:
1467 on_menu_toggle_all_additional_widgets1_activate(NULL, NULL);
1468 break;
1469 case GEANY_KEYS_VIEW_SIDEBAR:
1470 on_menu_show_sidebar1_toggled(NULL, NULL);
1471 break;
1472 case GEANY_KEYS_VIEW_ZOOMIN:
1473 on_zoom_in1_activate(NULL, NULL);
1474 break;
1475 case GEANY_KEYS_VIEW_ZOOMOUT:
1476 on_zoom_out1_activate(NULL, NULL);
1477 break;
1478 default:
1479 break;
1481 return TRUE;
1485 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id)
1487 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1488 ui_lookup_widget(main_widgets.window, "menu_fullscreen1"));
1490 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1494 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id)
1496 GtkCheckMenuItem *c = GTK_CHECK_MENU_ITEM(
1497 ui_lookup_widget(main_widgets.window, "menu_show_messages_window1"));
1499 gtk_check_menu_item_set_active(c, ! gtk_check_menu_item_get_active(c));
1503 static gboolean cb_func_build_action(guint key_id)
1505 GtkWidget *item;
1506 BuildMenuItems *menu_items;
1507 GeanyDocument *doc = document_get_current();
1509 if (doc == NULL)
1510 return TRUE;
1512 if (!GTK_WIDGET_IS_SENSITIVE(ui_lookup_widget(main_widgets.window, "menu_build1")))
1513 return TRUE;
1515 menu_items = build_get_menu_items(doc->file_type->id);
1516 /* TODO make it a table??*/
1517 switch (key_id)
1519 case GEANY_KEYS_BUILD_COMPILE:
1520 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_COMPILE)];
1521 break;
1522 case GEANY_KEYS_BUILD_LINK:
1523 item = menu_items->menu_item[GEANY_GBG_FT][GBO_TO_CMD(GEANY_GBO_BUILD)];
1524 break;
1525 case GEANY_KEYS_BUILD_MAKE:
1526 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_ALL)];
1527 break;
1528 case GEANY_KEYS_BUILD_MAKEOWNTARGET:
1529 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_CUSTOM)];
1530 break;
1531 case GEANY_KEYS_BUILD_MAKEOBJECT:
1532 item = menu_items->menu_item[GEANY_GBG_NON_FT][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT)];
1533 break;
1534 case GEANY_KEYS_BUILD_NEXTERROR:
1535 item = menu_items->menu_item[GBG_FIXED][GBF_NEXT_ERROR];
1536 break;
1537 case GEANY_KEYS_BUILD_PREVIOUSERROR:
1538 item = menu_items->menu_item[GBG_FIXED][GBF_PREV_ERROR];
1539 break;
1540 case GEANY_KEYS_BUILD_RUN:
1541 item = menu_items->menu_item[GEANY_GBG_EXEC][GBO_TO_CMD(GEANY_GBO_EXEC)];
1542 break;
1543 case GEANY_KEYS_BUILD_OPTIONS:
1544 item = menu_items->menu_item[GBG_FIXED][GBF_COMMANDS];
1545 break;
1546 default:
1547 item = NULL;
1549 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1550 * sensitive state, but some other menus don't update the sensitive status until
1551 * they are redrawn. */
1552 if (item && GTK_WIDGET_IS_SENSITIVE(item))
1553 gtk_menu_item_activate(GTK_MENU_ITEM(item));
1554 return TRUE;
1558 static gboolean read_current_word(GeanyDocument *doc)
1560 gint pos;
1562 if (doc == NULL)
1563 return FALSE;
1565 pos = sci_get_current_position(doc->editor->sci);
1567 editor_find_current_word(doc->editor, pos,
1568 editor_info.current_word, GEANY_MAX_WORD_LENGTH, NULL);
1570 return (*editor_info.current_word != 0);
1574 static gboolean check_current_word(GeanyDocument *doc)
1576 if (!read_current_word(doc))
1578 utils_beep();
1579 return FALSE;
1581 return TRUE;
1585 static gchar *get_current_word_or_sel(GeanyDocument *doc)
1587 ScintillaObject *sci = doc->editor->sci;
1589 if (sci_has_selection(sci))
1590 return sci_get_selection_contents(sci);
1592 return read_current_word(doc) ? g_strdup(editor_info.current_word) : NULL;
1596 static void focus_sidebar(void)
1598 if (ui_prefs.sidebar_visible)
1600 gint page_num = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook));
1601 GtkWidget *page = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.sidebar_notebook), page_num);
1603 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1604 gtk_widget_child_focus(page, GTK_DIR_TAB_FORWARD);
1609 static gboolean cb_func_switch_action(guint key_id)
1611 switch (key_id)
1613 case GEANY_KEYS_FOCUS_EDITOR:
1615 GeanyDocument *doc = document_get_current();
1616 if (doc != NULL)
1617 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
1618 break;
1620 case GEANY_KEYS_FOCUS_SCRIBBLE:
1621 msgwin_switch_tab(MSG_SCRATCH, TRUE);
1622 break;
1623 case GEANY_KEYS_FOCUS_SEARCHBAR:
1624 if (toolbar_prefs.visible)
1626 GtkWidget *search_entry = toolbar_get_widget_child_by_name("SearchEntry");
1627 if (search_entry != NULL)
1628 gtk_widget_grab_focus(search_entry);
1630 break;
1631 case GEANY_KEYS_FOCUS_SIDEBAR:
1632 focus_sidebar();
1633 break;
1634 case GEANY_KEYS_FOCUS_VTE:
1635 msgwin_switch_tab(MSG_VTE, TRUE);
1636 break;
1637 case GEANY_KEYS_FOCUS_COMPILER:
1638 msgwin_switch_tab(MSG_COMPILER, TRUE);
1639 break;
1640 case GEANY_KEYS_FOCUS_MESSAGES:
1641 msgwin_switch_tab(MSG_MESSAGE, TRUE);
1642 break;
1644 return TRUE;
1648 static void switch_notebook_page(gint direction)
1650 gint page_count, cur_page;
1651 gboolean parent_is_notebook = FALSE;
1652 GtkNotebook *notebook;
1653 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
1655 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1658 parent_is_notebook = GTK_IS_NOTEBOOK(focusw);
1660 while (! parent_is_notebook && (focusw = gtk_widget_get_parent(focusw)) != NULL);
1662 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1663 if (parent_is_notebook)
1664 notebook = GTK_NOTEBOOK(focusw);
1665 else
1666 notebook = GTK_NOTEBOOK(main_widgets.notebook);
1668 /* now switch pages */
1669 page_count = gtk_notebook_get_n_pages(notebook);
1670 cur_page = gtk_notebook_get_current_page(notebook);
1672 if (direction == GTK_DIR_LEFT)
1674 if (cur_page > 0)
1675 gtk_notebook_set_current_page(notebook, cur_page - 1);
1676 else
1677 gtk_notebook_set_current_page(notebook, page_count - 1);
1679 else if (direction == GTK_DIR_RIGHT)
1681 if (cur_page < page_count - 1)
1682 gtk_notebook_set_current_page(notebook, cur_page + 1);
1683 else
1684 gtk_notebook_set_current_page(notebook, 0);
1689 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id)
1691 switch_notebook_page(GTK_DIR_LEFT);
1695 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id)
1697 switch_notebook_page(GTK_DIR_RIGHT);
1701 static gboolean on_key_release_event(GtkWidget *widget, GdkEventKey *ev, gpointer user_data)
1703 /* user may have rebound keybinding to a different modifier than Ctrl, so check all */
1704 if (!switch_dialog_cancelled && is_modifier_key(ev->keyval))
1706 switch_dialog_cancelled = TRUE;
1708 if (switch_dialog && GTK_WIDGET_VISIBLE(switch_dialog))
1709 gtk_widget_hide(switch_dialog);
1711 mru_pos = 0;
1713 return FALSE;
1717 static GtkWidget *ui_minimal_dialog_new(GtkWindow *parent, const gchar *title)
1719 GtkWidget *dialog;
1721 dialog = gtk_window_new(GTK_WINDOW_POPUP);
1723 if (parent)
1725 gtk_window_set_transient_for(GTK_WINDOW(dialog), parent);
1726 gtk_window_set_destroy_with_parent(GTK_WINDOW(dialog), TRUE);
1728 gtk_window_set_title(GTK_WINDOW(dialog), title);
1729 gtk_window_set_type_hint(GTK_WINDOW(dialog), GDK_WINDOW_TYPE_HINT_DIALOG);
1730 gtk_window_set_position(GTK_WINDOW(dialog), GTK_WIN_POS_CENTER_ON_PARENT);
1732 gtk_widget_set_name(dialog, "GeanyDialog");
1733 return dialog;
1737 static GtkWidget *create_switch_dialog(void)
1739 GtkWidget *dialog, *widget, *vbox;
1741 dialog = ui_minimal_dialog_new(GTK_WINDOW(main_widgets.window), _("Switch to Document"));
1742 gtk_window_set_decorated(GTK_WINDOW(dialog), FALSE);
1743 gtk_window_set_default_size(GTK_WINDOW(dialog), 150, -1);
1745 vbox = gtk_vbox_new(FALSE, 6);
1746 gtk_container_set_border_width(GTK_CONTAINER(vbox), 12);
1747 gtk_container_add(GTK_CONTAINER(dialog), vbox);
1749 widget = gtk_image_new_from_stock(GTK_STOCK_JUMP_TO, GTK_ICON_SIZE_BUTTON);
1750 gtk_container_add(GTK_CONTAINER(vbox), widget);
1752 widget = geany_wrap_label_new(NULL);
1753 gtk_label_set_justify(GTK_LABEL(widget), GTK_JUSTIFY_CENTER);
1754 gtk_container_add(GTK_CONTAINER(vbox), widget);
1755 switch_dialog_label = widget;
1757 g_signal_connect(dialog, "key-release-event", G_CALLBACK(on_key_release_event), NULL);
1758 return dialog;
1762 static gboolean on_switch_timeout(G_GNUC_UNUSED gpointer data)
1764 if (switch_dialog_cancelled)
1766 return FALSE;
1768 if (! switch_dialog || !GTK_WIDGET_VISIBLE(switch_dialog))
1769 mru_pos = 2; /* skip past the previous document */
1770 else
1771 mru_pos += 1;
1773 if (! switch_dialog)
1774 switch_dialog = create_switch_dialog();
1776 geany_wrap_label_set_text(GTK_LABEL(switch_dialog_label),
1777 DOC_FILENAME(document_get_current()));
1778 gtk_widget_show_all(switch_dialog);
1779 return FALSE;
1783 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id)
1785 GeanyDocument *last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1787 if (! DOC_VALID(last_doc))
1789 utils_beep();
1790 mru_pos = 0;
1791 last_doc = g_queue_peek_nth(mru_docs, mru_pos);
1793 if (! DOC_VALID(last_doc))
1794 return;
1796 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
1797 document_get_notebook_page(last_doc));
1799 /* if there's a modifier key, we can switch back in MRU order each time unless
1800 * the key is released */
1801 if (! switch_dialog_cancelled)
1803 on_switch_timeout(NULL); /* update filename label */
1805 else
1806 if (keybindings_lookup_item(GEANY_KEY_GROUP_NOTEBOOK,
1807 GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED)->mods)
1809 switch_dialog_cancelled = FALSE;
1811 /* delay showing dialog to give user time to let go of any modifier keys */
1812 g_timeout_add(600, on_switch_timeout, NULL);
1817 /* move document left/right/first/last */
1818 static void cb_func_move_tab(guint key_id)
1820 GtkWidget *sci;
1821 GtkNotebook *nb = GTK_NOTEBOOK(main_widgets.notebook);
1822 gint cur_page = gtk_notebook_get_current_page(nb);
1823 GeanyDocument *doc = document_get_current();
1825 if (doc == NULL)
1826 return;
1828 sci = GTK_WIDGET(doc->editor->sci);
1830 switch (key_id)
1832 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT:
1833 gtk_notebook_reorder_child(nb, sci, cur_page - 1); /* notebook wraps around by default */
1834 break;
1835 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT:
1837 gint npage = cur_page + 1;
1839 if (npage == gtk_notebook_get_n_pages(nb))
1840 npage = 0; /* wraparound */
1841 gtk_notebook_reorder_child(nb, sci, npage);
1842 break;
1844 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST:
1845 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? 0 : -1);
1846 break;
1847 case GEANY_KEYS_NOTEBOOK_MOVETABLAST:
1848 gtk_notebook_reorder_child(nb, sci, (file_prefs.tab_order_ltr) ? -1 : 0);
1849 break;
1851 return;
1855 static void goto_matching_brace(GeanyDocument *doc)
1857 gint pos, new_pos;
1859 if (doc == NULL)
1860 return;
1862 pos = sci_get_current_position(doc->editor->sci);
1863 if (! utils_isbrace(sci_get_char_at(doc->editor->sci, pos), TRUE))
1864 pos--; /* set pos to the brace */
1866 new_pos = sci_find_matching_brace(doc->editor->sci, pos);
1867 if (new_pos != -1)
1868 { /* set the cursor at the brace */
1869 sci_set_current_position(doc->editor->sci, new_pos, FALSE);
1870 editor_display_current_line(doc->editor, 0.5F);
1875 static gboolean cb_func_clipboard_action(guint key_id)
1877 GeanyDocument *doc = document_get_current();
1879 if (doc == NULL)
1880 return TRUE;
1882 switch (key_id)
1884 case GEANY_KEYS_CLIPBOARD_CUT:
1885 on_cut1_activate(NULL, NULL);
1886 break;
1887 case GEANY_KEYS_CLIPBOARD_COPY:
1888 on_copy1_activate(NULL, NULL);
1889 break;
1890 case GEANY_KEYS_CLIPBOARD_PASTE:
1891 on_paste1_activate(NULL, NULL);
1892 break;
1893 case GEANY_KEYS_CLIPBOARD_COPYLINE:
1894 sci_send_command(doc->editor->sci, SCI_LINECOPY);
1895 break;
1896 case GEANY_KEYS_CLIPBOARD_CUTLINE:
1897 sci_send_command(doc->editor->sci, SCI_LINECUT);
1898 break;
1900 return TRUE;
1904 static void goto_tag(GeanyDocument *doc, gboolean definition)
1906 gchar *text = get_current_word_or_sel(doc);
1908 if (text)
1909 symbols_goto_tag(text, definition);
1910 else
1911 utils_beep();
1913 g_free(text);
1917 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
1918 static gboolean cb_func_goto_action(guint key_id)
1920 gint cur_line;
1921 GeanyDocument *doc = document_get_current();
1923 if (doc == NULL)
1924 return TRUE;
1926 cur_line = sci_get_current_line(doc->editor->sci);
1928 switch (key_id)
1930 case GEANY_KEYS_GOTO_BACK:
1931 navqueue_go_back();
1932 return TRUE;
1933 case GEANY_KEYS_GOTO_FORWARD:
1934 navqueue_go_forward();
1935 return TRUE;
1936 case GEANY_KEYS_GOTO_LINE:
1938 if (toolbar_prefs.visible)
1940 GtkWidget *wid = toolbar_get_widget_child_by_name("GotoEntry");
1942 /* use toolbar item if shown */
1943 if (wid)
1945 gtk_widget_grab_focus(wid);
1946 return TRUE;
1949 on_go_to_line_activate(NULL, NULL);
1950 return TRUE;
1952 case GEANY_KEYS_GOTO_MATCHINGBRACE:
1953 goto_matching_brace(doc);
1954 return TRUE;
1955 case GEANY_KEYS_GOTO_TOGGLEMARKER:
1957 sci_toggle_marker_at_line(doc->editor->sci, cur_line, 1);
1958 return TRUE;
1960 case GEANY_KEYS_GOTO_NEXTMARKER:
1962 gint mline = sci_marker_next(doc->editor->sci, cur_line + 1, 1 << 1, TRUE);
1964 if (mline != -1)
1966 sci_set_current_line(doc->editor->sci, mline);
1967 editor_display_current_line(doc->editor, 0.5F);
1969 return TRUE;
1971 case GEANY_KEYS_GOTO_PREVIOUSMARKER:
1973 gint mline = sci_marker_previous(doc->editor->sci, cur_line - 1, 1 << 1, TRUE);
1975 if (mline != -1)
1977 sci_set_current_line(doc->editor->sci, mline);
1978 editor_display_current_line(doc->editor, 0.5F);
1980 return TRUE;
1982 case GEANY_KEYS_GOTO_TAGDEFINITION:
1983 goto_tag(doc, TRUE);
1984 return TRUE;
1985 case GEANY_KEYS_GOTO_TAGDECLARATION:
1986 goto_tag(doc, FALSE);
1987 return TRUE;
1989 /* only check editor-sensitive keybindings when editor has focus so home,end still
1990 * work in other widgets */
1991 if (gtk_window_get_focus(GTK_WINDOW(main_widgets.window)) != GTK_WIDGET(doc->editor->sci))
1992 return FALSE;
1994 switch (key_id)
1996 case GEANY_KEYS_GOTO_LINESTART:
1997 sci_send_command(doc->editor->sci, editor_prefs.smart_home_key ? SCI_VCHOME : SCI_HOME);
1998 break;
1999 case GEANY_KEYS_GOTO_LINEEND:
2000 sci_send_command(doc->editor->sci, SCI_LINEEND);
2001 break;
2002 case GEANY_KEYS_GOTO_LINEENDVISUAL:
2003 sci_send_command(doc->editor->sci, SCI_LINEENDDISPLAY);
2004 break;
2005 case GEANY_KEYS_GOTO_PREVWORDPART:
2006 sci_send_command(doc->editor->sci, SCI_WORDPARTLEFT);
2007 break;
2008 case GEANY_KEYS_GOTO_NEXTWORDPART:
2009 sci_send_command(doc->editor->sci, SCI_WORDPARTRIGHT);
2010 break;
2012 return TRUE;
2016 static void duplicate_lines(GeanyEditor *editor)
2018 if (sci_get_lines_selected(editor->sci) > 1)
2019 { /* ignore extra_line because of selecting lines from the line number column */
2020 editor_select_lines(editor, FALSE);
2021 sci_selection_duplicate(editor->sci);
2023 else if (sci_has_selection(editor->sci))
2024 sci_selection_duplicate(editor->sci);
2025 else
2026 sci_line_duplicate(editor->sci);
2030 static void delete_lines(GeanyEditor *editor)
2032 editor_select_lines(editor, TRUE); /* include last line (like cut lines, copy lines do) */
2033 sci_clear(editor->sci); /* (SCI_LINEDELETE only does 1 line) */
2037 static void move_lines(GeanyEditor *editor, gboolean down)
2039 ScintillaObject *sci = editor->sci;
2040 gchar *text;
2041 gint pos, line, len;
2043 sci_start_undo_action(sci);
2044 editor_select_lines(editor, FALSE);
2045 len = sci_get_selected_text_length(sci);
2047 pos = sci_get_selection_start(sci);
2048 line = sci_get_line_from_position(sci, pos);
2049 if (down)
2050 line++;
2051 else
2052 line--;
2054 text = sci_get_selection_contents(sci);
2055 sci_clear(sci);
2057 pos = sci_get_position_from_line(sci, line);
2058 sci_insert_text(sci, pos, text);
2059 g_free(text);
2061 sci_set_current_position(sci, pos, TRUE);
2062 sci_set_selection_end(sci, pos + len - 1);
2064 sci_end_undo_action(sci);
2068 /* common function for editor keybindings, only valid when scintilla has focus. */
2069 static gboolean cb_func_editor_action(guint key_id)
2071 GeanyDocument *doc = document_get_current();
2072 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2074 /* edit keybindings only valid when scintilla widget has focus */
2075 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2076 return FALSE; /* also makes tab work outside editor */
2078 switch (key_id)
2080 case GEANY_KEYS_EDITOR_UNDO:
2081 on_undo1_activate(NULL, NULL);
2082 break;
2083 case GEANY_KEYS_EDITOR_REDO:
2084 on_redo1_activate(NULL, NULL);
2085 break;
2086 case GEANY_KEYS_EDITOR_SCROLLTOLINE:
2087 editor_scroll_to_line(doc->editor, -1, 0.5F);
2088 break;
2089 case GEANY_KEYS_EDITOR_SCROLLLINEUP:
2090 sci_send_command(doc->editor->sci, SCI_LINESCROLLUP);
2091 break;
2092 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN:
2093 sci_send_command(doc->editor->sci, SCI_LINESCROLLDOWN);
2094 break;
2095 case GEANY_KEYS_EDITOR_DUPLICATELINE:
2096 duplicate_lines(doc->editor);
2097 break;
2098 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR:
2099 editor_goto_next_snippet_cursor(doc->editor);
2100 break;
2101 case GEANY_KEYS_EDITOR_DELETELINE:
2102 delete_lines(doc->editor);
2103 break;
2104 case GEANY_KEYS_EDITOR_DELETELINETOEND:
2105 sci_send_command(doc->editor->sci, SCI_DELLINERIGHT);
2106 break;
2107 case GEANY_KEYS_EDITOR_TRANSPOSELINE:
2108 sci_send_command(doc->editor->sci, SCI_LINETRANSPOSE);
2109 break;
2110 case GEANY_KEYS_EDITOR_AUTOCOMPLETE:
2111 editor_start_auto_complete(doc->editor, sci_get_current_position(doc->editor->sci), TRUE);
2112 break;
2113 case GEANY_KEYS_EDITOR_CALLTIP:
2114 editor_show_calltip(doc->editor, -1);
2115 break;
2116 case GEANY_KEYS_EDITOR_MACROLIST:
2117 editor_show_macro_list(doc->editor);
2118 break;
2119 case GEANY_KEYS_EDITOR_CONTEXTACTION:
2120 if (check_current_word(doc))
2121 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets.editor_menu,
2122 "context_action1")), NULL);
2123 break;
2124 case GEANY_KEYS_EDITOR_COMPLETESNIPPET:
2125 /* allow tab to be overloaded */
2126 return check_snippet_completion(doc);
2128 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION:
2130 GeanyKeyBinding *kb = keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR,
2131 GEANY_KEYS_EDITOR_COMPLETESNIPPET);
2133 switch (kb->key)
2135 case GDK_space:
2136 sci_add_text(doc->editor->sci, " ");
2137 break;
2138 case GDK_Tab:
2139 sci_send_command(doc->editor->sci, SCI_TAB);
2140 break;
2141 default:
2142 break;
2144 break;
2146 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION:
2147 return editor_complete_word_part(doc->editor);
2149 case GEANY_KEYS_EDITOR_MOVELINEUP:
2150 move_lines(doc->editor, FALSE);
2151 break;
2152 case GEANY_KEYS_EDITOR_MOVELINEDOWN:
2153 move_lines(doc->editor, TRUE);
2154 break;
2156 return TRUE;
2160 static void join_lines(GeanyEditor *editor)
2162 gint start, end, i;
2164 start = sci_get_line_from_position(editor->sci,
2165 sci_get_selection_start(editor->sci));
2166 end = sci_get_line_from_position(editor->sci,
2167 sci_get_selection_end(editor->sci));
2169 /* if there is only one line in selection, join it with the following one */
2170 if (end == start)
2171 end = start + 1;
2174 * remove trailing spaces for every line except the last one
2175 * so that these spaces won't appear within text after joining
2177 for (i = start; i < end; i++)
2178 editor_strip_line_trailing_spaces(editor, i);
2180 /* remove starting spaces from second and following lines due to the same reason */
2181 for (i = start + 1; i <= end; i++)
2182 sci_set_line_indentation(editor->sci, i, 0);
2185 * SCI_LINESJOIN automatically adds spaces between joined lines, including
2186 * empty ones. We should drop empty lines if we want only one space to be
2187 * inserted (see also example below). I don't think we should care of that.
2190 sci_set_target_start(editor->sci,
2191 sci_get_position_from_line(editor->sci, start));
2192 sci_set_target_end(editor->sci,
2193 sci_get_position_from_line(editor->sci, end));
2194 sci_lines_join(editor->sci);
2197 * Example: joining
2199 * [TAB]if (something_wrong)
2200 * [TAB]{
2201 * [TAB][TAB]
2202 * [TAB][TAB]exit(1);[SPACE][SPACE]
2203 * [TAB]}[SPACE]
2205 * gives
2207 * [TAB]if (something_wrong) { exit(1); }[SPACE]
2212 static void split_lines(GeanyEditor *editor, gint column)
2214 gint start, indent, linescount, i, end;
2215 gchar c;
2216 ScintillaObject *sci = editor->sci;
2218 /* don't include trailing newlines */
2219 end = sci_get_selection_end(sci);
2220 while ((c = sci_get_char_at(sci, end - 1)) == '\n' || c == '\r') end--;
2221 sci_set_selection_end(sci, end);
2223 start = sci_get_line_from_position(editor->sci,
2224 sci_get_selection_start(editor->sci));
2227 * If several lines are selected, first join them.
2228 * This allows to reformat text paragraphs easily.
2230 if (sci_get_lines_selected(editor->sci) > 1)
2231 join_lines(editor);
2234 * If this line is short enough, just return
2236 if (column > sci_get_line_end_position(editor->sci, start) -
2237 sci_get_position_from_line(editor->sci, start))
2239 return;
2243 * We have to manipulate line indentation so that indentation
2244 * of the resulting lines would be consistent. For example,
2245 * the result of splitting "[TAB]very long content":
2247 * +-------------+-------------+
2248 * | proper | wrong |
2249 * +-------------+-------------+
2250 * | [TAB]very | [TAB]very |
2251 * | [TAB]long | long |
2252 * | [TAB]content| content |
2253 * +-------------+-------------+
2255 indent = sci_get_line_indentation(editor->sci, start);
2256 sci_set_line_indentation(editor->sci, start, 0);
2259 * Use sci_get_line_count() to determine how many new lines
2260 * appeared during splitting. SCI_LINESSPLIT should better return
2261 * this value itself...
2263 sci_target_from_selection(editor->sci);
2264 linescount = sci_get_line_count(editor->sci);
2265 sci_lines_split(editor->sci,
2266 (column - indent) * sci_text_width(editor->sci, STYLE_DEFAULT, " "));
2267 linescount = sci_get_line_count(editor->sci) - linescount;
2269 /* Fix indentation. */
2270 for (i = start; i <= start + linescount; i++)
2271 sci_set_line_indentation(editor->sci, i, indent);
2275 /* if cursor < anchor, swap them */
2276 static void sci_fix_selection(ScintillaObject *sci)
2278 gint start, end;
2280 start = sci_get_selection_start(sci);
2281 end = sci_get_selection_end(sci);
2282 sci_set_selection(sci, start, end);
2286 static void reflow_paragraph(GeanyEditor *editor)
2288 ScintillaObject *sci = editor->sci;
2289 gboolean sel;
2290 gint column = -1;
2292 if (editor->line_breaking)
2294 /* use line break column if enabled */
2295 column = editor_prefs.line_break_column;
2297 else if (editor_prefs.long_line_type != 2)
2299 /* use long line if enabled */
2300 column = editor_prefs.long_line_column;
2302 else
2304 /* do nothing if no column is defined */
2305 utils_beep();
2306 return;
2308 sci_start_undo_action(sci);
2309 sel = sci_has_selection(sci);
2310 if (!sel)
2312 gint line, pos;
2314 keybindings_send_command(GEANY_KEY_GROUP_SELECT, GEANY_KEYS_SELECT_PARAGRAPH);
2315 /* deselect last line break */
2316 pos = sci_get_selection_end(sci);
2317 line = sci_get_line_from_position(sci, pos);
2318 if (line < sci_get_line_count(sci) - 1)
2320 /* not last line */
2321 pos = sci_get_line_end_position(sci, line - 1);
2322 sci_set_selection_end(sci, pos);
2325 sci_fix_selection(sci);
2326 split_lines(editor, column);
2327 if (!sel)
2328 sci_set_anchor(sci, -1);
2330 sci_end_undo_action(sci);
2334 /* common function for format keybindings, only valid when scintilla has focus. */
2335 static gboolean cb_func_format_action(guint key_id)
2337 GeanyDocument *doc = document_get_current();
2338 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2340 /* keybindings only valid when scintilla widget has focus */
2341 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2342 return TRUE;
2344 switch (key_id)
2346 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE:
2347 on_menu_toggle_line_commentation1_activate(NULL, NULL);
2348 break;
2349 case GEANY_KEYS_FORMAT_COMMENTLINE:
2350 on_menu_comment_line1_activate(NULL, NULL);
2351 break;
2352 case GEANY_KEYS_FORMAT_UNCOMMENTLINE:
2353 on_menu_uncomment_line1_activate(NULL, NULL);
2354 break;
2355 case GEANY_KEYS_FORMAT_INCREASEINDENT:
2356 on_menu_increase_indent1_activate(NULL, NULL);
2357 break;
2358 case GEANY_KEYS_FORMAT_DECREASEINDENT:
2359 on_menu_decrease_indent1_activate(NULL, NULL);
2360 break;
2361 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE:
2362 editor_indentation_by_one_space(doc->editor, -1, FALSE);
2363 break;
2364 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE:
2365 editor_indentation_by_one_space(doc->editor, -1, TRUE);
2366 break;
2367 case GEANY_KEYS_FORMAT_AUTOINDENT:
2368 editor_smart_line_indentation(doc->editor, -1);
2369 break;
2370 case GEANY_KEYS_FORMAT_TOGGLECASE:
2371 on_toggle_case1_activate(NULL, NULL);
2372 break;
2373 case GEANY_KEYS_FORMAT_SENDTOCMD1:
2374 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 0)
2375 tools_execute_custom_command(doc, ui_prefs.custom_commands[0]);
2376 break;
2377 case GEANY_KEYS_FORMAT_SENDTOCMD2:
2378 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 1)
2379 tools_execute_custom_command(doc, ui_prefs.custom_commands[1]);
2380 break;
2381 case GEANY_KEYS_FORMAT_SENDTOCMD3:
2382 if (ui_prefs.custom_commands && g_strv_length(ui_prefs.custom_commands) > 2)
2383 tools_execute_custom_command(doc, ui_prefs.custom_commands[2]);
2384 break;
2385 case GEANY_KEYS_FORMAT_SENDTOVTE:
2386 on_send_selection_to_vte1_activate(NULL, NULL);
2387 break;
2388 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH:
2389 reflow_paragraph(doc->editor);
2390 break;
2392 return TRUE;
2396 /* common function for select keybindings, only valid when scintilla has focus. */
2397 static gboolean cb_func_select_action(guint key_id)
2399 GeanyDocument *doc;
2400 ScintillaObject *sci;
2401 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2402 static GtkWidget *scribble_widget = NULL;
2404 /* special case for Select All in the scribble widget */
2405 if (scribble_widget == NULL) /* lookup the scribble widget only once */
2406 scribble_widget = ui_lookup_widget(main_widgets.window, "textview_scribble");
2407 if (key_id == GEANY_KEYS_SELECT_ALL && focusw == scribble_widget)
2409 g_signal_emit_by_name(scribble_widget, "select-all", TRUE);
2410 return TRUE;
2413 doc = document_get_current();
2414 /* keybindings only valid when scintilla widget has focus */
2415 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2416 return TRUE;
2417 sci = doc->editor->sci;
2419 switch (key_id)
2421 case GEANY_KEYS_SELECT_ALL:
2422 on_menu_select_all1_activate(NULL, NULL);
2423 break;
2424 case GEANY_KEYS_SELECT_WORD:
2425 editor_select_word(doc->editor);
2426 break;
2427 case GEANY_KEYS_SELECT_LINE:
2428 editor_select_lines(doc->editor, FALSE);
2429 break;
2430 case GEANY_KEYS_SELECT_PARAGRAPH:
2431 editor_select_paragraph(doc->editor);
2432 break;
2433 case GEANY_KEYS_SELECT_WORDPARTLEFT:
2434 sci_send_command(sci, SCI_WORDPARTLEFTEXTEND);
2435 break;
2436 case GEANY_KEYS_SELECT_WORDPARTRIGHT:
2437 sci_send_command(sci, SCI_WORDPARTRIGHTEXTEND);
2438 break;
2440 return TRUE;
2444 static gboolean cb_func_document_action(guint key_id)
2446 GeanyDocument *doc = document_get_current();
2448 if (doc == NULL)
2449 return TRUE;
2451 switch (key_id)
2453 case GEANY_KEYS_DOCUMENT_REPLACETABS:
2454 on_replace_tabs_activate(NULL, NULL);
2455 break;
2456 case GEANY_KEYS_DOCUMENT_REPLACESPACES:
2457 on_replace_spaces_activate(NULL, NULL);
2458 break;
2459 case GEANY_KEYS_DOCUMENT_LINEBREAK:
2460 on_line_breaking1_activate(NULL, NULL);
2461 ui_document_show_hide(doc);
2462 break;
2463 case GEANY_KEYS_DOCUMENT_LINEWRAP:
2464 on_line_wrapping1_toggled(NULL, NULL);
2465 ui_document_show_hide(doc);
2466 break;
2467 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST:
2468 document_update_tag_list(doc, TRUE);
2469 break;
2470 case GEANY_KEYS_DOCUMENT_FOLDALL:
2471 editor_fold_all(doc->editor);
2472 break;
2473 case GEANY_KEYS_DOCUMENT_UNFOLDALL:
2474 editor_unfold_all(doc->editor);
2475 break;
2476 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD:
2477 if (editor_prefs.folding)
2479 gint line = sci_get_current_line(doc->editor->sci);
2480 sci_toggle_fold(doc->editor->sci, line);
2481 break;
2484 return TRUE;
2488 /* common function for insert keybindings, only valid when scintilla has focus. */
2489 static gboolean cb_func_insert_action(guint key_id)
2491 GeanyDocument *doc = document_get_current();
2492 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
2494 /* keybindings only valid when scintilla widget has focus */
2495 if (doc == NULL || focusw != GTK_WIDGET(doc->editor->sci))
2496 return TRUE;
2498 switch (key_id)
2500 case GEANY_KEYS_INSERT_ALTWHITESPACE:
2501 editor_insert_alternative_whitespace(doc->editor);
2502 break;
2503 case GEANY_KEYS_INSERT_DATE:
2504 gtk_menu_item_activate(GTK_MENU_ITEM(
2505 ui_lookup_widget(main_widgets.window, "insert_date_custom1")));
2506 break;
2508 return TRUE;
2512 /* update key combination */
2513 void keybindings_update_combo(GeanyKeyBinding *kb, guint key, GdkModifierType mods)
2515 GtkWidget *widget = kb->menu_item;
2517 if (widget && kb->key)
2518 gtk_widget_remove_accelerator(widget, kb_accel_group, kb->key, kb->mods);
2520 kb->key = key;
2521 kb->mods = mods;
2523 if (widget && kb->key)
2524 gtk_widget_add_accelerator(widget, "activate", kb_accel_group,
2525 kb->key, kb->mods, GTK_ACCEL_VISIBLE);
2529 /* used for plugins */
2530 GeanyKeyGroup *keybindings_set_group(GeanyKeyGroup *group, const gchar *section_name,
2531 const gchar *label, gsize count, GeanyKeyGroupCallback callback)
2533 g_return_val_if_fail(section_name, NULL);
2534 g_return_val_if_fail(count, NULL);
2536 /* prevent conflict with core bindings */
2537 g_return_val_if_fail(!g_str_equal(section_name, keybindings_keyfile_group_name), NULL);
2539 if (!group)
2540 group = g_new0(GeanyKeyGroup, 1);
2542 if (!group->keys || count > group->count)
2544 /* allow resizing existing array of keys */
2545 group->keys = g_renew(GeanyKeyBinding, group->keys, count);
2546 memset(group->keys + group->count, 0, (count - group->count) * sizeof(GeanyKeyBinding));
2548 group->plugin = TRUE;
2549 add_kb_group(group, section_name, label, count, group->keys, callback);
2550 return group;
2554 /* used for plugins */
2555 void keybindings_free_group(GeanyKeyGroup *group)
2557 GeanyKeyBinding *kb;
2559 g_assert(group->plugin);
2561 foreach_c_array(kb, group->keys, group->count)
2563 g_free(kb->name);
2564 g_free(kb->label);
2566 g_free(group->keys);
2567 g_ptr_array_remove_fast(keybinding_groups, group);
2568 g_free(group);