2 * keybindings.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2006-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
24 * Configurable keyboard shortcuts.
25 * - keybindings_send_command() mimics a built-in keybinding action.
26 * - @ref GeanyKeyGroupID lists groups of built-in keybindings.
27 * @see plugin_set_key_group().
34 #include "keybindings.h"
38 #include "callbacks.h"
39 #include "documentprivate.h"
40 #include "filetypes.h"
41 #include "keybindingsprivate.h"
43 #include "msgwindow.h"
47 #include "sciwrappers.h"
57 #include "gtkcompat.h"
59 #include <gdk/gdkkeysyms.h>
63 GPtrArray
*keybinding_groups
; /* array of GeanyKeyGroup pointers, in visual order */
65 /* keyfile group name for non-plugin KB groups */
66 static const gchar keybindings_keyfile_group_name
[] = "Bindings";
68 /* core keybindings */
69 static GeanyKeyBinding binding_ids
[GEANY_KEYS_COUNT
];
71 static GtkAccelGroup
*kb_accel_group
= NULL
;
72 static const gboolean swap_alt_tab_order
= FALSE
;
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
);
78 static gboolean
check_current_word(GeanyDocument
*doc
, gboolean sci_word
);
79 static gboolean
read_current_word(GeanyDocument
*doc
, gboolean sci_word
);
80 static gchar
*get_current_word_or_sel(GeanyDocument
*doc
, gboolean sci_word
);
82 static gboolean
cb_func_file_action(guint key_id
);
83 static gboolean
cb_func_project_action(guint key_id
);
84 static gboolean
cb_func_editor_action(guint key_id
);
85 static gboolean
cb_func_select_action(guint key_id
);
86 static gboolean
cb_func_format_action(guint key_id
);
87 static gboolean
cb_func_insert_action(guint key_id
);
88 static gboolean
cb_func_search_action(guint key_id
);
89 static gboolean
cb_func_goto_action(guint key_id
);
90 static gboolean
cb_func_switch_action(guint key_id
);
91 static gboolean
cb_func_clipboard_action(guint key_id
);
92 static gboolean
cb_func_build_action(guint key_id
);
93 static gboolean
cb_func_document_action(guint key_id
);
94 static gboolean
cb_func_view_action(guint key_id
);
96 /* note: new keybindings should normally use per group callbacks */
97 static void cb_func_menu_help(guint key_id
);
98 static void cb_func_menu_preferences(guint key_id
);
100 static void cb_func_menu_fullscreen(guint key_id
);
101 static void cb_func_menu_messagewindow(guint key_id
);
103 static void cb_func_menu_opencolorchooser(guint key_id
);
105 static void cb_func_switch_tableft(guint key_id
);
106 static void cb_func_switch_tabright(guint key_id
);
107 static void cb_func_switch_tablastused(guint key_id
);
108 static void cb_func_move_tab(guint key_id
);
110 static void add_popup_menu_accels(void);
113 /** Gets significant modifiers from a GdkModifierType mask. The set of
114 * significant modifiers corresponds to the default modifier mask as returned
115 * by @c gtk_accelerator_get_default_mod_mask(). In addition, it improves
116 * the Command key handling on OS X by adding @c GEANY_PRIMARY_MOD_MASK
117 * when needed. For this reason it is preferred to use this function
118 * instead of @c gtk_accelerator_set_default_mod_mask().
119 * @param mods GdkModifierType mask.
120 * @return Significant modifiers from the mask.
123 GdkModifierType
keybindings_get_modifiers(GdkModifierType mods
)
126 if (mods
& GDK_MOD2_MASK
)
127 mods
|= GEANY_PRIMARY_MOD_MASK
;
129 return mods
& gtk_accelerator_get_default_mod_mask();
133 /** Looks up a keybinding item.
134 * @param group Group.
135 * @param key_id Keybinding index for the group.
136 * @return The keybinding.
139 GeanyKeyBinding
*keybindings_get_item(GeanyKeyGroup
*group
, gsize key_id
)
143 g_assert(key_id
< group
->plugin_key_count
);
144 return &group
->plugin_keys
[key_id
];
146 g_assert(key_id
< GEANY_KEYS_COUNT
);
147 return &binding_ids
[key_id
];
151 /* This is used to set default keybindings on startup.
152 * Menu accels are set in apply_kb_accel(). */
153 /** Fills a GeanyKeyBinding struct item.
154 * @note Always set @a key and @a mod to 0, otherwise you will likely
155 * cause conflicts with the user's custom, other plugin's keybindings or
156 * future default keybindings.
157 * @param group Group.
158 * @param key_id Keybinding index for the group.
159 * @param callback Function to call when activated, or @c NULL to use the group callback.
160 * Usually it's better to use the group callback instead - see plugin_set_key_group().
161 * @param key Default key, e.g. @c GDK_j (must be lower case), but usually 0 for unset.
162 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
163 * @param kf_name Key name for the configuration file, such as @c "menu_new".
164 * @param label Label used in the preferences dialog keybindings tab. May contain
165 * underscores - these won't be displayed.
166 * @param menu_item Optional widget to set an accelerator for, or @c NULL.
167 * @return The keybinding - normally this is ignored. */
169 GeanyKeyBinding
*keybindings_set_item(GeanyKeyGroup
*group
, gsize key_id
,
170 GeanyKeyCallback callback
, guint key
, GdkModifierType mod
,
171 const gchar
*kf_name
, const gchar
*label
, GtkWidget
*menu_item
)
175 g_assert(group
->name
);
176 kb
= keybindings_get_item(group
, key_id
);
178 g_ptr_array_add(group
->key_items
, kb
);
182 /* some plugins e.g. GeanyLua need these fields duplicated */
183 SETPTR(kb
->name
, g_strdup(kf_name
));
184 SETPTR(kb
->label
, g_strdup(label
));
188 /* we don't touch these strings unless group->plugin is set, const cast is safe */
189 kb
->name
= (gchar
*)kf_name
;
190 kb
->label
= (gchar
*)label
;
194 kb
->default_key
= key
;
195 kb
->default_mods
= mod
;
196 kb
->callback
= callback
;
199 kb
->menu_item
= menu_item
;
205 /** Creates a new keybinding using a GeanyKeyBindingFunc and attaches it to a keybinding group
207 * If given the callback should return @c TRUE if the keybinding was handled, otherwise @c FALSE
208 * to allow other callbacks to be run. This allows for multiplexing keybindings on the same keys,
209 * depending on the focused widget (or context). If the callback is NULL the group's callback will
210 * be invoked, but the same rule applies.
212 * @param group Group.
213 * @param key_id Keybinding index for the group.
214 * @param key Default key, e.g. @c GDK_j (must be lower case), but usually 0 for unset.
215 * @param mod Default modifier, e.g. @c GDK_CONTROL_MASK, but usually 0 for unset.
216 * @param kf_name Key name for the configuration file, such as @c "menu_new".
217 * @param label Label used in the preferences dialog keybindings tab. May contain
218 * underscores - these won't be displayed.
219 * @param menu_item Optional widget to set an accelerator for, or @c NULL.
220 * @param cb New-style callback to be called when activated, or @c NULL to use the group callback.
221 * @param pdata Plugin-specific data passed back to the callback.
222 * @param destroy_notify Function that is invoked to free the plugin data when not needed anymore.
223 * @return The keybinding - normally this is ignored.
225 * @since 1.26 (API 226)
226 * @see See plugin_set_key_group_full
229 GeanyKeyBinding
*keybindings_set_item_full(GeanyKeyGroup
*group
, gsize key_id
,
230 guint key
, GdkModifierType mod
, const gchar
*kf_name
, const gchar
*label
,
231 GtkWidget
*menu_item
, GeanyKeyBindingFunc cb
, gpointer pdata
,
232 GDestroyNotify destroy_notify
)
236 /* For now, this is intended for plugins only */
237 g_assert(group
->plugin
);
239 kb
= keybindings_set_item(group
, key_id
, NULL
, key
, mod
, kf_name
, label
, menu_item
);
242 kb
->cb_data_destroy
= destroy_notify
;
247 static void free_key_binding(gpointer item
)
249 GeanyKeyBinding
*kb
= item
;
254 if (kb
->cb_data_destroy
)
255 kb
->cb_data_destroy(kb
->cb_data
);
259 static void add_kb_group(GeanyKeyGroup
*group
,
260 const gchar
*name
, const gchar
*label
, GeanyKeyGroupCallback callback
, gboolean plugin
)
262 g_ptr_array_add(keybinding_groups
, group
);
265 group
->label
= label
;
266 group
->callback
= callback
;
267 group
->cb_func
= NULL
;
268 group
->cb_data
= NULL
;
269 group
->plugin
= plugin
;
270 /* Only plugins use the destroy notify thus far */
271 group
->key_items
= g_ptr_array_new_with_free_func(plugin
? free_key_binding
: NULL
);
275 GeanyKeyGroup
*keybindings_get_core_group(guint id
)
277 static GeanyKeyGroup groups
[GEANY_KEY_GROUP_COUNT
];
279 g_return_val_if_fail(id
< GEANY_KEY_GROUP_COUNT
, NULL
);
285 static void add_kb(GeanyKeyGroup
*group
, gsize key_id
,
286 GeanyKeyCallback callback
, guint key
, GdkModifierType mod
,
287 const gchar
*kf_name
, const gchar
*label
, const gchar
*widget_name
)
289 GtkWidget
*widget
= widget_name
?
290 ui_lookup_widget(main_widgets
.window
, widget_name
) : NULL
;
292 keybindings_set_item(group
, key_id
, callback
,
293 key
, mod
, kf_name
, label
, widget
);
297 #define ADD_KB_GROUP(group_id, label, callback) \
298 add_kb_group(keybindings_get_core_group(group_id),\
299 keybindings_keyfile_group_name, label, callback, FALSE)
301 static void init_default_kb(void)
303 GeanyKeyGroup
*group
;
305 /* visual group order */
306 ADD_KB_GROUP(GEANY_KEY_GROUP_FILE
, _("File"), cb_func_file_action
);
307 ADD_KB_GROUP(GEANY_KEY_GROUP_EDITOR
, _("Editor"), cb_func_editor_action
);
308 ADD_KB_GROUP(GEANY_KEY_GROUP_CLIPBOARD
, _("Clipboard"), cb_func_clipboard_action
);
309 ADD_KB_GROUP(GEANY_KEY_GROUP_SELECT
, _("Select"), cb_func_select_action
);
310 ADD_KB_GROUP(GEANY_KEY_GROUP_FORMAT
, _("Format"), cb_func_format_action
);
311 ADD_KB_GROUP(GEANY_KEY_GROUP_INSERT
, _("Insert"), cb_func_insert_action
);
312 ADD_KB_GROUP(GEANY_KEY_GROUP_SETTINGS
, _("Settings"), NULL
);
313 ADD_KB_GROUP(GEANY_KEY_GROUP_SEARCH
, _("Search"), cb_func_search_action
);
314 ADD_KB_GROUP(GEANY_KEY_GROUP_GOTO
, _("Go to"), cb_func_goto_action
);
315 ADD_KB_GROUP(GEANY_KEY_GROUP_VIEW
, _("View"), cb_func_view_action
);
316 ADD_KB_GROUP(GEANY_KEY_GROUP_DOCUMENT
, _("Document"), cb_func_document_action
);
317 ADD_KB_GROUP(GEANY_KEY_GROUP_PROJECT
, _("Project"), cb_func_project_action
);
318 ADD_KB_GROUP(GEANY_KEY_GROUP_BUILD
, _("Build"), cb_func_build_action
);
319 ADD_KB_GROUP(GEANY_KEY_GROUP_TOOLS
, _("Tools"), NULL
);
320 ADD_KB_GROUP(GEANY_KEY_GROUP_HELP
, _("Help"), NULL
);
321 ADD_KB_GROUP(GEANY_KEY_GROUP_FOCUS
, _("Focus"), cb_func_switch_action
);
322 ADD_KB_GROUP(GEANY_KEY_GROUP_NOTEBOOK
, _("Notebook tab"), NULL
);
324 /* Init all fields of keys with default values.
325 * The menu_item field is always the main menu item, popup menu accelerators are
326 * set in add_popup_menu_accels(). */
328 group
= keybindings_get_core_group(GEANY_KEY_GROUP_FILE
);
330 add_kb(group
, GEANY_KEYS_FILE_NEW
, NULL
,
331 GDK_n
, GEANY_PRIMARY_MOD_MASK
, "menu_new", _("New"), "menu_new1");
332 add_kb(group
, GEANY_KEYS_FILE_OPEN
, NULL
,
333 GDK_o
, GEANY_PRIMARY_MOD_MASK
, "menu_open", _("Open"), "menu_open1");
334 add_kb(group
, GEANY_KEYS_FILE_OPENSELECTED
, NULL
,
335 GDK_o
, GDK_SHIFT_MASK
| GEANY_PRIMARY_MOD_MASK
, "menu_open_selected",
336 _("Open selected file"), "menu_open_selected_file1");
337 add_kb(group
, GEANY_KEYS_FILE_SAVE
, NULL
,
338 GDK_s
, GEANY_PRIMARY_MOD_MASK
, "menu_save", _("Save"), "menu_save1");
339 add_kb(group
, GEANY_KEYS_FILE_SAVEAS
, NULL
,
340 0, 0, "menu_saveas", _("Save as"), "menu_save_as1");
341 add_kb(group
, GEANY_KEYS_FILE_SAVEALL
, NULL
,
342 GDK_s
, GDK_SHIFT_MASK
| GEANY_PRIMARY_MOD_MASK
, "menu_saveall", _("Save all"),
344 add_kb(group
, GEANY_KEYS_FILE_PROPERTIES
, NULL
,
345 0, 0, "file_properties", _("Properties"), "properties1");
346 add_kb(group
, GEANY_KEYS_FILE_PRINT
, NULL
,
347 GDK_p
, GEANY_PRIMARY_MOD_MASK
, "menu_print", _("Print"), "print1");
348 add_kb(group
, GEANY_KEYS_FILE_CLOSE
, NULL
,
349 GDK_w
, GEANY_PRIMARY_MOD_MASK
, "menu_close", _("Close"), "menu_close1");
350 add_kb(group
, GEANY_KEYS_FILE_CLOSEALL
, NULL
,
351 GDK_w
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "menu_closeall", _("Close all"),
353 add_kb(group
, GEANY_KEYS_FILE_RELOAD
, NULL
,
354 GDK_r
, GEANY_PRIMARY_MOD_MASK
, "menu_reloadfile", _("Reload file"), "menu_reload1");
355 add_kb(group
, GEANY_KEYS_FILE_OPENLASTTAB
, NULL
,
356 0, 0, "file_openlasttab", _("Re-open last closed tab"), NULL
);
357 add_kb(group
, GEANY_KEYS_FILE_QUIT
, NULL
,
358 GDK_q
, GEANY_PRIMARY_MOD_MASK
, "menu_quit", _("Quit"), "menu_quit1");
360 group
= keybindings_get_core_group(GEANY_KEY_GROUP_PROJECT
);
362 add_kb(group
, GEANY_KEYS_PROJECT_NEW
, NULL
,
363 0, 0, "project_new", _("New"), "project_new1");
364 add_kb(group
, GEANY_KEYS_PROJECT_OPEN
, NULL
,
365 0, 0, "project_open", _("Open"), "project_open1");
366 add_kb(group
, GEANY_KEYS_PROJECT_PROPERTIES
, NULL
,
367 0, 0, "project_properties",
368 ui_lookup_stock_label(GTK_STOCK_PROPERTIES
), "project_properties1");
369 add_kb(group
, GEANY_KEYS_PROJECT_CLOSE
, NULL
,
370 0, 0, "project_close", _("Close"), "project_close1");
372 group
= keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR
);
374 add_kb(group
, GEANY_KEYS_EDITOR_UNDO
, NULL
,
375 GDK_z
, GEANY_PRIMARY_MOD_MASK
, "menu_undo", _("Undo"), "menu_undo2");
376 add_kb(group
, GEANY_KEYS_EDITOR_REDO
, NULL
,
377 GDK_y
, GEANY_PRIMARY_MOD_MASK
, "menu_redo", _("Redo"), "menu_redo2");
378 add_kb(group
, GEANY_KEYS_EDITOR_DUPLICATELINE
, NULL
,
379 GDK_d
, GEANY_PRIMARY_MOD_MASK
, "edit_duplicateline", _("D_uplicate Line or Selection"),
380 "duplicate_line_or_selection1");
381 add_kb(group
, GEANY_KEYS_EDITOR_DELETELINE
, NULL
,
382 GDK_k
, GEANY_PRIMARY_MOD_MASK
, "edit_deleteline", _("_Delete Current Line(s)"),
383 "delete_current_lines1");
384 add_kb(group
, GEANY_KEYS_EDITOR_DELETELINETOEND
, NULL
,
385 GDK_Delete
, GDK_SHIFT_MASK
| GEANY_PRIMARY_MOD_MASK
, "edit_deletelinetoend",
386 _("Delete to line end"), NULL
);
387 /* Note: transpose may fit better in format group, but that would break the API */
388 add_kb(group
, GEANY_KEYS_EDITOR_TRANSPOSELINE
, NULL
,
389 0, 0, "edit_transposeline", _("_Transpose Current Line"), NULL
);
390 add_kb(group
, GEANY_KEYS_EDITOR_SCROLLTOLINE
, NULL
,
391 GDK_l
, GDK_SHIFT_MASK
| GEANY_PRIMARY_MOD_MASK
, "edit_scrolltoline", _("Scroll to current line"), NULL
);
392 add_kb(group
, GEANY_KEYS_EDITOR_SCROLLLINEUP
, NULL
,
393 GDK_Up
, GDK_MOD1_MASK
, "edit_scrolllineup", _("Scroll up the view by one line"), NULL
);
394 add_kb(group
, GEANY_KEYS_EDITOR_SCROLLLINEDOWN
, NULL
,
395 GDK_Down
, GDK_MOD1_MASK
, "edit_scrolllinedown", _("Scroll down the view by one line"), NULL
);
396 add_kb(group
, GEANY_KEYS_EDITOR_COMPLETESNIPPET
, NULL
,
397 GDK_Tab
, 0, "edit_completesnippet", _("Complete snippet"), NULL
);
398 add_kb(group
, GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR
, NULL
,
399 0, 0, "move_snippetnextcursor", _("Move cursor in snippet"), NULL
);
400 add_kb(group
, GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION
, NULL
,
401 0, 0, "edit_suppresssnippetcompletion", _("Suppress snippet completion"), NULL
);
402 add_kb(group
, GEANY_KEYS_EDITOR_CONTEXTACTION
, NULL
,
403 0, 0, "popup_contextaction", _("Context Action"), NULL
);
404 add_kb(group
, GEANY_KEYS_EDITOR_AUTOCOMPLETE
, NULL
,
405 GDK_space
, GEANY_PRIMARY_MOD_MASK
, "edit_autocomplete", _("Complete word"), NULL
);
406 add_kb(group
, GEANY_KEYS_EDITOR_CALLTIP
, NULL
,
407 GDK_space
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "edit_calltip", _("Show calltip"), NULL
);
408 add_kb(group
, GEANY_KEYS_EDITOR_WORDPARTCOMPLETION
, NULL
,
409 GDK_Tab
, 0, "edit_wordpartcompletion", _("Word part completion"), NULL
);
410 add_kb(group
, GEANY_KEYS_EDITOR_MOVELINEUP
, NULL
,
411 GDK_Page_Up
, GDK_MOD1_MASK
, "edit_movelineup",
412 _("Move line(s) up"), "move_lines_up1");
413 add_kb(group
, GEANY_KEYS_EDITOR_MOVELINEDOWN
, NULL
,
414 GDK_Page_Down
, GDK_MOD1_MASK
, "edit_movelinedown",
415 _("Move line(s) down"), "move_lines_down1");
417 group
= keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD
);
419 add_kb(group
, GEANY_KEYS_CLIPBOARD_CUT
, NULL
,
420 GDK_x
, GEANY_PRIMARY_MOD_MASK
, "menu_cut", _("Cut"), "menu_cut1");
421 add_kb(group
, GEANY_KEYS_CLIPBOARD_COPY
, NULL
,
422 GDK_c
, GEANY_PRIMARY_MOD_MASK
, "menu_copy", _("Copy"), "menu_copy1");
423 add_kb(group
, GEANY_KEYS_CLIPBOARD_PASTE
, NULL
,
424 GDK_v
, GEANY_PRIMARY_MOD_MASK
, "menu_paste", _("Paste"), "menu_paste1");
425 add_kb(group
, GEANY_KEYS_CLIPBOARD_COPYLINE
, NULL
,
426 GDK_c
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "edit_copyline", _("_Copy Current Line(s)"),
427 "copy_current_lines1");
428 add_kb(group
, GEANY_KEYS_CLIPBOARD_CUTLINE
, NULL
,
429 GDK_x
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "edit_cutline", _("Cu_t Current Line(s)"),
430 "cut_current_lines1");
432 group
= keybindings_get_core_group(GEANY_KEY_GROUP_SELECT
);
434 add_kb(group
, GEANY_KEYS_SELECT_ALL
, NULL
,
435 GDK_a
, GEANY_PRIMARY_MOD_MASK
, "menu_selectall", _("Select All"), "menu_select_all1");
436 add_kb(group
, GEANY_KEYS_SELECT_WORD
, NULL
,
437 GDK_w
, GDK_SHIFT_MASK
| GDK_MOD1_MASK
, "edit_selectword", _("Select current word"), NULL
);
438 add_kb(group
, GEANY_KEYS_SELECT_LINE
, NULL
,
439 GDK_l
, GDK_SHIFT_MASK
| GDK_MOD1_MASK
, "edit_selectline", _("S_elect Current Line(s)"),
440 "select_current_lines1");
441 add_kb(group
, GEANY_KEYS_SELECT_PARAGRAPH
, NULL
,
442 GDK_p
, GDK_SHIFT_MASK
| GDK_MOD1_MASK
, "edit_selectparagraph", _("Se_lect Current Paragraph"),
443 "select_current_paragraph1");
444 add_kb(group
, GEANY_KEYS_SELECT_WORDPARTLEFT
, NULL
,
445 0, 0, "edit_selectwordpartleft", _("Select to previous word part"), NULL
);
446 add_kb(group
, GEANY_KEYS_SELECT_WORDPARTRIGHT
, NULL
,
447 0, 0, "edit_selectwordpartright", _("Select to next word part"), NULL
);
449 group
= keybindings_get_core_group(GEANY_KEY_GROUP_FORMAT
);
451 add_kb(group
, GEANY_KEYS_FORMAT_TOGGLECASE
, NULL
,
452 GDK_u
, GEANY_PRIMARY_MOD_MASK
| GDK_MOD1_MASK
, "edit_togglecase",
453 _("T_oggle Case of Selection"), "menu_toggle_case2");
454 add_kb(group
, GEANY_KEYS_FORMAT_COMMENTLINETOGGLE
, NULL
,
455 GDK_e
, GEANY_PRIMARY_MOD_MASK
, "edit_commentlinetoggle", _("Toggle line commentation"),
456 "menu_toggle_line_commentation1");
457 add_kb(group
, GEANY_KEYS_FORMAT_COMMENTLINE
, NULL
,
458 0, 0, "edit_commentline", _("Comment line(s)"), "menu_comment_line1");
459 add_kb(group
, GEANY_KEYS_FORMAT_UNCOMMENTLINE
, NULL
,
460 0, 0, "edit_uncommentline", _("Uncomment line(s)"), "menu_uncomment_line1");
461 add_kb(group
, GEANY_KEYS_FORMAT_INCREASEINDENT
, NULL
,
462 GDK_i
, GEANY_PRIMARY_MOD_MASK
, "edit_increaseindent", _("Increase indent"),
463 "menu_increase_indent1");
464 add_kb(group
, GEANY_KEYS_FORMAT_DECREASEINDENT
, NULL
,
465 GDK_u
, GEANY_PRIMARY_MOD_MASK
, "edit_decreaseindent", _("Decrease indent"),
466 "menu_decrease_indent1");
467 add_kb(group
, GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE
, NULL
,
468 0, 0, "edit_increaseindentbyspace", _("Increase indent by one space"), NULL
);
469 add_kb(group
, GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE
, NULL
,
470 0, 0, "edit_decreaseindentbyspace", _("Decrease indent by one space"), NULL
);
471 add_kb(group
, GEANY_KEYS_FORMAT_AUTOINDENT
, NULL
,
472 0, 0, "edit_autoindent", _("S_mart Line Indent"), "smart_line_indent1");
473 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD1
, NULL
,
474 GDK_1
, GEANY_PRIMARY_MOD_MASK
, "edit_sendtocmd1", _("Send to Custom Command 1"), NULL
);
475 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD2
, NULL
,
476 GDK_2
, GEANY_PRIMARY_MOD_MASK
, "edit_sendtocmd2", _("Send to Custom Command 2"), NULL
);
477 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD3
, NULL
,
478 GDK_3
, GEANY_PRIMARY_MOD_MASK
, "edit_sendtocmd3", _("Send to Custom Command 3"), NULL
);
479 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD4
, NULL
,
480 0, 0, "edit_sendtocmd4", _("Send to Custom Command 4"), NULL
);
481 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD5
, NULL
,
482 0, 0, "edit_sendtocmd5", _("Send to Custom Command 5"), NULL
);
483 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD6
, NULL
,
484 0, 0, "edit_sendtocmd6", _("Send to Custom Command 6"), NULL
);
485 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD7
, NULL
,
486 0, 0, "edit_sendtocmd7", _("Send to Custom Command 7"), NULL
);
487 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD8
, NULL
,
488 0, 0, "edit_sendtocmd8", _("Send to Custom Command 8"), NULL
);
489 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOCMD9
, NULL
,
490 0, 0, "edit_sendtocmd9", _("Send to Custom Command 9"), NULL
);
491 /* may fit better in editor group */
492 add_kb(group
, GEANY_KEYS_FORMAT_SENDTOVTE
, NULL
,
493 0, 0, "edit_sendtovte", _("_Send Selection to Terminal"), "send_selection_to_vte1");
494 add_kb(group
, GEANY_KEYS_FORMAT_REFLOWPARAGRAPH
, NULL
,
495 GDK_j
, GEANY_PRIMARY_MOD_MASK
, "format_reflowparagraph", _("_Reflow Lines/Block"),
496 "reflow_lines_block1");
497 keybindings_set_item(group
, GEANY_KEYS_FORMAT_JOINLINES
, NULL
,
498 0, 0, "edit_joinlines", _("Join lines"), NULL
);
500 group
= keybindings_get_core_group(GEANY_KEY_GROUP_INSERT
);
502 add_kb(group
, GEANY_KEYS_INSERT_DATE
, NULL
,
503 GDK_d
, GDK_SHIFT_MASK
| GDK_MOD1_MASK
, "menu_insert_date", _("Insert date"),
504 "insert_date_custom1");
505 add_kb(group
, GEANY_KEYS_INSERT_ALTWHITESPACE
, NULL
,
506 0, 0, "edit_insertwhitespace", _("Insert Alternative _White Space"),
507 "insert_alternative_white_space1");
508 add_kb(group
, GEANY_KEYS_INSERT_LINEBEFORE
, NULL
,
509 0, 0, "edit_insertlinebefore", _("Insert New Line Before Current"), NULL
);
510 add_kb(group
, GEANY_KEYS_INSERT_LINEAFTER
, NULL
,
511 0, 0, "edit_insertlineafter", _("Insert New Line After Current"), NULL
);
513 group
= keybindings_get_core_group(GEANY_KEY_GROUP_SETTINGS
);
515 add_kb(group
, GEANY_KEYS_SETTINGS_PREFERENCES
, cb_func_menu_preferences
,
516 GDK_p
, GEANY_PRIMARY_MOD_MASK
| GDK_MOD1_MASK
, "menu_preferences", _("Preferences"),
518 add_kb(group
, GEANY_KEYS_SETTINGS_PLUGINPREFERENCES
, cb_func_menu_preferences
,
519 0, 0, "menu_pluginpreferences", _("P_lugin Preferences"), "plugin_preferences1");
521 group
= keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH
);
523 add_kb(group
, GEANY_KEYS_SEARCH_FIND
, NULL
,
524 GDK_f
, GEANY_PRIMARY_MOD_MASK
, "menu_find", _("Find"), "find1");
525 add_kb(group
, GEANY_KEYS_SEARCH_FINDNEXT
, NULL
,
526 GDK_g
, GEANY_PRIMARY_MOD_MASK
, "menu_findnext", _("Find Next"), "find_next1");
527 add_kb(group
, GEANY_KEYS_SEARCH_FINDPREVIOUS
, NULL
,
528 GDK_g
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "menu_findprevious", _("Find Previous"),
530 add_kb(group
, GEANY_KEYS_SEARCH_FINDNEXTSEL
, NULL
,
531 0, 0, "menu_findnextsel", _("Find Next _Selection"), "find_nextsel1");
532 add_kb(group
, GEANY_KEYS_SEARCH_FINDPREVSEL
, NULL
,
533 0, 0, "menu_findprevsel", _("Find Pre_vious Selection"), "find_prevsel1");
534 add_kb(group
, GEANY_KEYS_SEARCH_REPLACE
, NULL
,
535 GDK_h
, GEANY_PRIMARY_MOD_MASK
, "menu_replace", _("Replace"), "replace1");
536 add_kb(group
, GEANY_KEYS_SEARCH_FINDINFILES
, NULL
, GDK_f
,
537 GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "menu_findinfiles", _("Find in Files"),
539 add_kb(group
, GEANY_KEYS_SEARCH_NEXTMESSAGE
, NULL
,
540 0, 0, "menu_nextmessage", _("Next Message"), "next_message1");
541 add_kb(group
, GEANY_KEYS_SEARCH_PREVIOUSMESSAGE
, NULL
,
542 0, 0, "menu_previousmessage", _("Previous Message"), "previous_message1");
543 add_kb(group
, GEANY_KEYS_SEARCH_FINDUSAGE
, NULL
,
544 GDK_e
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "popup_findusage",
545 _("Find Usage"), "find_usage1");
546 add_kb(group
, GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE
, NULL
,
547 GDK_d
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "popup_finddocumentusage",
548 _("Find Document Usage"), "find_document_usage1");
549 add_kb(group
, GEANY_KEYS_SEARCH_MARKALL
, NULL
,
550 GDK_m
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "find_markall", _("_Mark All"), "mark_all1");
552 group
= keybindings_get_core_group(GEANY_KEY_GROUP_GOTO
);
554 add_kb(group
, GEANY_KEYS_GOTO_BACK
, NULL
,
555 GDK_Left
, GDK_MOD1_MASK
, "nav_back", _("Navigate back a location"), NULL
);
556 add_kb(group
, GEANY_KEYS_GOTO_FORWARD
, NULL
,
557 GDK_Right
, GDK_MOD1_MASK
, "nav_forward", _("Navigate forward a location"), NULL
);
558 add_kb(group
, GEANY_KEYS_GOTO_LINE
, NULL
,
559 GDK_l
, GEANY_PRIMARY_MOD_MASK
, "menu_gotoline", _("Go to Line"), "go_to_line1");
560 add_kb(group
, GEANY_KEYS_GOTO_MATCHINGBRACE
, NULL
,
561 GDK_b
, GEANY_PRIMARY_MOD_MASK
, "edit_gotomatchingbrace",
562 _("Go to matching brace"), NULL
);
563 add_kb(group
, GEANY_KEYS_GOTO_TOGGLEMARKER
, NULL
,
564 GDK_m
, GEANY_PRIMARY_MOD_MASK
, "edit_togglemarker",
565 _("Toggle marker"), NULL
);
566 add_kb(group
, GEANY_KEYS_GOTO_NEXTMARKER
, NULL
,
567 GDK_period
, GEANY_PRIMARY_MOD_MASK
, "edit_gotonextmarker",
568 _("Go to Ne_xt Marker"), "go_to_next_marker1");
569 add_kb(group
, GEANY_KEYS_GOTO_PREVIOUSMARKER
, NULL
,
570 GDK_comma
, GEANY_PRIMARY_MOD_MASK
, "edit_gotopreviousmarker",
571 _("Go to Pre_vious Marker"), "go_to_previous_marker1");
572 add_kb(group
, GEANY_KEYS_GOTO_TAGDEFINITION
, NULL
,
573 GDK_t
, GEANY_PRIMARY_MOD_MASK
, "popup_gototagdefinition",
574 _("Go to Tag Definition"), "goto_tag_definition1");
575 add_kb(group
, GEANY_KEYS_GOTO_TAGDECLARATION
, NULL
,
576 GDK_t
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "popup_gototagdeclaration",
577 _("Go to Tag Declaration"), "goto_tag_declaration1");
578 add_kb(group
, GEANY_KEYS_GOTO_LINESTART
, NULL
,
579 GDK_Home
, 0, "edit_gotolinestart", _("Go to Start of Line"), NULL
);
580 add_kb(group
, GEANY_KEYS_GOTO_LINEEND
, NULL
,
581 GDK_End
, 0, "edit_gotolineend", _("Go to End of Line"), NULL
);
582 add_kb(group
, GEANY_KEYS_GOTO_LINESTARTVISUAL
, NULL
,
583 GDK_Home
, GDK_MOD1_MASK
, "edit_gotolinestartvisual", _("Go to Start of Display Line"), NULL
);
584 add_kb(group
, GEANY_KEYS_GOTO_LINEENDVISUAL
, NULL
,
585 GDK_End
, GDK_MOD1_MASK
, "edit_gotolineendvisual", _("Go to End of Display Line"), NULL
);
586 add_kb(group
, GEANY_KEYS_GOTO_PREVWORDPART
, NULL
,
587 GDK_slash
, GEANY_PRIMARY_MOD_MASK
, "edit_prevwordstart", _("Go to Previous Word Part"), NULL
);
588 add_kb(group
, GEANY_KEYS_GOTO_NEXTWORDPART
, NULL
,
589 GDK_backslash
, GEANY_PRIMARY_MOD_MASK
, "edit_nextwordstart", _("Go to Next Word Part"), NULL
);
591 group
= keybindings_get_core_group(GEANY_KEY_GROUP_VIEW
);
593 add_kb(group
, GEANY_KEYS_VIEW_TOGGLEALL
, NULL
,
594 0, 0, "menu_toggleall", _("Toggle All Additional Widgets"),
595 "menu_toggle_all_additional_widgets1");
596 add_kb(group
, GEANY_KEYS_VIEW_FULLSCREEN
, cb_func_menu_fullscreen
,
597 GDK_F11
, 0, "menu_fullscreen", _("Fullscreen"), "menu_fullscreen1");
598 add_kb(group
, GEANY_KEYS_VIEW_MESSAGEWINDOW
, cb_func_menu_messagewindow
,
599 0, 0, "menu_messagewindow", _("Toggle Messages Window"),
600 "menu_show_messages_window1");
601 add_kb(group
, GEANY_KEYS_VIEW_SIDEBAR
, NULL
,
602 0, 0, "toggle_sidebar", _("Toggle Sidebar"), "menu_show_sidebar1");
603 add_kb(group
, GEANY_KEYS_VIEW_ZOOMIN
, NULL
,
604 GDK_plus
, GEANY_PRIMARY_MOD_MASK
, "menu_zoomin", _("Zoom In"), "menu_zoom_in1");
605 add_kb(group
, GEANY_KEYS_VIEW_ZOOMOUT
, NULL
,
606 GDK_minus
, GEANY_PRIMARY_MOD_MASK
, "menu_zoomout", _("Zoom Out"), "menu_zoom_out1");
607 add_kb(group
, GEANY_KEYS_VIEW_ZOOMRESET
, NULL
,
608 GDK_0
, GEANY_PRIMARY_MOD_MASK
, "normal_size", _("Zoom Reset"), "normal_size1");
610 group
= keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS
);
612 add_kb(group
, GEANY_KEYS_FOCUS_EDITOR
, NULL
,
613 GDK_F2
, 0, "switch_editor", _("Switch to Editor"), NULL
);
614 add_kb(group
, GEANY_KEYS_FOCUS_SEARCHBAR
, NULL
,
615 GDK_F7
, 0, "switch_search_bar", _("Switch to Search Bar"), NULL
);
616 add_kb(group
, GEANY_KEYS_FOCUS_MESSAGE_WINDOW
, NULL
,
617 0, 0, "switch_message_window", _("Switch to Message Window"), NULL
);
618 add_kb(group
, GEANY_KEYS_FOCUS_COMPILER
, NULL
,
619 0, 0, "switch_compiler", _("Switch to Compiler"), NULL
);
620 add_kb(group
, GEANY_KEYS_FOCUS_MESSAGES
, NULL
,
621 0, 0, "switch_messages", _("Switch to Messages"), NULL
);
622 add_kb(group
, GEANY_KEYS_FOCUS_SCRIBBLE
, NULL
,
623 GDK_F6
, 0, "switch_scribble", _("Switch to Scribble"), NULL
);
624 add_kb(group
, GEANY_KEYS_FOCUS_VTE
, NULL
,
625 GDK_F4
, 0, "switch_vte", _("Switch to VTE"), NULL
);
626 add_kb(group
, GEANY_KEYS_FOCUS_SIDEBAR
, NULL
,
627 0, 0, "switch_sidebar", _("Switch to Sidebar"), NULL
);
628 add_kb(group
, GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST
, NULL
,
629 0, 0, "switch_sidebar_symbol_list", _("Switch to Sidebar Symbol List"), NULL
);
630 add_kb(group
, GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST
, NULL
,
631 0, 0, "switch_sidebar_doc_list", _("Switch to Sidebar Document List"), NULL
);
633 group
= keybindings_get_core_group(GEANY_KEY_GROUP_NOTEBOOK
);
635 add_kb(group
, GEANY_KEYS_NOTEBOOK_SWITCHTABLEFT
, cb_func_switch_tableft
,
636 GDK_Page_Up
, GEANY_PRIMARY_MOD_MASK
, "switch_tableft", _("Switch to left document"), NULL
);
637 add_kb(group
, GEANY_KEYS_NOTEBOOK_SWITCHTABRIGHT
, cb_func_switch_tabright
,
638 GDK_Page_Down
, GEANY_PRIMARY_MOD_MASK
, "switch_tabright", _("Switch to right document"), NULL
);
639 add_kb(group
, GEANY_KEYS_NOTEBOOK_SWITCHTABLASTUSED
, cb_func_switch_tablastused
,
640 GDK_Tab
, GEANY_PRIMARY_MOD_MASK
, "switch_tablastused", _("Switch to last used document"), NULL
);
641 add_kb(group
, GEANY_KEYS_NOTEBOOK_MOVETABLEFT
, cb_func_move_tab
,
642 GDK_Page_Up
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "move_tableft",
643 _("Move document left"), NULL
);
644 add_kb(group
, GEANY_KEYS_NOTEBOOK_MOVETABRIGHT
, cb_func_move_tab
,
645 GDK_Page_Down
, GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
, "move_tabright",
646 _("Move document right"), NULL
);
647 add_kb(group
, GEANY_KEYS_NOTEBOOK_MOVETABFIRST
, cb_func_move_tab
,
648 0, 0, "move_tabfirst", _("Move document first"), NULL
);
649 add_kb(group
, GEANY_KEYS_NOTEBOOK_MOVETABLAST
, cb_func_move_tab
,
650 0, 0, "move_tablast", _("Move document last"), NULL
);
652 group
= keybindings_get_core_group(GEANY_KEY_GROUP_DOCUMENT
);
654 add_kb(group
, GEANY_KEYS_DOCUMENT_LINEWRAP
, NULL
,
655 0, 0, "menu_linewrap", _("Toggle Line wrapping"), "menu_line_wrapping1");
656 add_kb(group
, GEANY_KEYS_DOCUMENT_LINEBREAK
, NULL
,
657 0, 0, "menu_linebreak", _("Toggle Line breaking"), "line_breaking1");
658 add_kb(group
, GEANY_KEYS_DOCUMENT_CLONE
, NULL
,
659 0, 0, "menu_clone", _("_Clone"), "clone1");
660 add_kb(group
, GEANY_KEYS_DOCUMENT_REPLACETABS
, NULL
,
661 0, 0, "menu_replacetabs", _("Replace tabs with space"), "menu_replace_tabs");
662 add_kb(group
, GEANY_KEYS_DOCUMENT_REPLACESPACES
, NULL
,
663 0, 0, "menu_replacespaces", _("Replace spaces with tabs"), "menu_replace_spaces");
664 add_kb(group
, GEANY_KEYS_DOCUMENT_TOGGLEFOLD
, NULL
,
665 0, 0, "menu_togglefold", _("Toggle current fold"), NULL
);
666 add_kb(group
, GEANY_KEYS_DOCUMENT_FOLDALL
, NULL
,
667 0, 0, "menu_foldall", _("Fold all"), "menu_fold_all1");
668 add_kb(group
, GEANY_KEYS_DOCUMENT_UNFOLDALL
, NULL
,
669 0, 0, "menu_unfoldall", _("Unfold all"), "menu_unfold_all1");
670 add_kb(group
, GEANY_KEYS_DOCUMENT_RELOADTAGLIST
, NULL
,
671 GDK_r
, GDK_SHIFT_MASK
| GEANY_PRIMARY_MOD_MASK
, "reloadtaglist", _("Reload symbol list"), NULL
);
672 add_kb(group
, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS
, NULL
,
673 0, 0, "remove_markers", _("Remove Markers"), "remove_markers1");
674 add_kb(group
, GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS
, NULL
,
675 0, 0, "remove_error_indicators", _("Remove Error Indicators"), "menu_remove_indicators1");
676 add_kb(group
, GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS
, NULL
,
677 0, 0, "remove_markers_and_indicators", _("Remove Markers and Error Indicators"), NULL
);
679 group
= keybindings_get_core_group(GEANY_KEY_GROUP_BUILD
);
681 add_kb(group
, GEANY_KEYS_BUILD_COMPILE
, NULL
,
682 GDK_F8
, 0, "build_compile", _("Compile"), NULL
);
683 add_kb(group
, GEANY_KEYS_BUILD_LINK
, NULL
,
684 GDK_F9
, 0, "build_link", _("Build"), NULL
);
685 add_kb(group
, GEANY_KEYS_BUILD_MAKE
, NULL
,
686 GDK_F9
, GDK_SHIFT_MASK
, "build_make", _("Make all"), NULL
);
687 add_kb(group
, GEANY_KEYS_BUILD_MAKEOWNTARGET
, NULL
,
688 GDK_F9
, GDK_SHIFT_MASK
| GEANY_PRIMARY_MOD_MASK
, "build_makeowntarget",
689 _("Make custom target"), NULL
);
690 add_kb(group
, GEANY_KEYS_BUILD_MAKEOBJECT
, NULL
,
691 GDK_F8
, GDK_SHIFT_MASK
, "build_makeobject", _("Make object"), NULL
);
692 add_kb(group
, GEANY_KEYS_BUILD_NEXTERROR
, NULL
,
693 0, 0, "build_nexterror", _("Next error"), NULL
);
694 add_kb(group
, GEANY_KEYS_BUILD_PREVIOUSERROR
, NULL
,
695 0, 0, "build_previouserror", _("Previous error"), NULL
);
696 add_kb(group
, GEANY_KEYS_BUILD_RUN
, NULL
,
697 GDK_F5
, 0, "build_run", _("Run"), NULL
);
698 add_kb(group
, GEANY_KEYS_BUILD_OPTIONS
, NULL
,
699 0, 0, "build_options", _("Build options"), NULL
);
701 group
= keybindings_get_core_group(GEANY_KEY_GROUP_TOOLS
);
703 add_kb(group
, GEANY_KEYS_TOOLS_OPENCOLORCHOOSER
, cb_func_menu_opencolorchooser
,
704 0, 0, "menu_opencolorchooser", _("Show Color Chooser"), "menu_choose_color1");
706 group
= keybindings_get_core_group(GEANY_KEY_GROUP_HELP
);
708 add_kb(group
, GEANY_KEYS_HELP_HELP
, cb_func_menu_help
,
709 GDK_F1
, 0, "menu_help", _("Help"), "help1");
713 static void free_key_group(gpointer item
)
715 GeanyKeyGroup
*group
= item
;
717 g_ptr_array_free(group
->key_items
, TRUE
);
721 if (group
->cb_data_destroy
)
722 group
->cb_data_destroy(group
->cb_data
);
723 g_free(group
->plugin_keys
);
729 void keybindings_init(void)
731 memset(binding_ids
, 0, sizeof binding_ids
);
732 keybinding_groups
= g_ptr_array_sized_new(GEANY_KEY_GROUP_COUNT
);
733 g_ptr_array_set_free_func(keybinding_groups
, free_key_group
);
734 kb_accel_group
= gtk_accel_group_new();
737 gtk_window_add_accel_group(GTK_WINDOW(main_widgets
.window
), kb_accel_group
);
739 g_signal_connect(main_widgets
.window
, "key-press-event", G_CALLBACK(on_key_press_event
), NULL
);
743 typedef void (*KBItemCallback
) (GeanyKeyGroup
*group
, GeanyKeyBinding
*kb
, gpointer user_data
);
745 static void keybindings_foreach(KBItemCallback cb
, gpointer user_data
)
748 GeanyKeyGroup
*group
;
751 foreach_ptr_array(group
, g
, keybinding_groups
)
753 foreach_ptr_array(kb
, i
, group
->key_items
)
754 cb(group
, kb
, user_data
);
759 static void load_kb(GeanyKeyGroup
*group
, GeanyKeyBinding
*kb
, gpointer user_data
)
761 GKeyFile
*config
= user_data
;
764 GdkModifierType mods
;
766 val
= g_key_file_get_string(config
, group
->name
, kb
->name
, NULL
);
769 gtk_accelerator_parse(val
, &key
, &mods
);
777 static void load_user_kb(void)
779 gchar
*configfile
= g_build_filename(app
->configdir
, "keybindings.conf", NULL
);
780 GKeyFile
*config
= g_key_file_new();
782 /* backwards compatibility with Geany 0.21 defaults */
783 if (!g_file_test(configfile
, G_FILE_TEST_EXISTS
))
785 gchar
*geanyconf
= g_build_filename(app
->configdir
, "geany.conf", NULL
);
786 const gchar data
[] = "[Bindings]\n"
787 "popup_gototagdefinition=\n"
788 "edit_transposeline=<Control>t\n"
790 "edit_movelinedown=\n"
791 "move_tableft=<Alt>Page_Up\n"
792 "move_tabright=<Alt>Page_Down\n";
794 utils_write_file(configfile
, g_file_test(geanyconf
, G_FILE_TEST_EXISTS
) ?
799 /* now load user defined keys */
800 if (g_key_file_load_from_file(config
, configfile
, G_KEY_FILE_KEEP_COMMENTS
, NULL
))
802 keybindings_foreach(load_kb
, config
);
805 g_key_file_free(config
);
809 static void apply_kb_accel(GeanyKeyGroup
*group
, GeanyKeyBinding
*kb
, gpointer user_data
)
811 if (kb
->key
!= 0 && kb
->menu_item
)
813 gtk_widget_add_accelerator(kb
->menu_item
, "activate", kb_accel_group
,
814 kb
->key
, kb
->mods
, GTK_ACCEL_VISIBLE
);
819 void keybindings_load_keyfile(void)
822 add_popup_menu_accels();
824 /* set menu accels now, after user keybindings have been read */
825 keybindings_foreach(apply_kb_accel
, NULL
);
829 static void add_menu_accel(GeanyKeyGroup
*group
, guint kb_id
, GtkWidget
*menuitem
)
831 GeanyKeyBinding
*kb
= keybindings_get_item(group
, kb_id
);
834 gtk_widget_add_accelerator(menuitem
, "activate", kb_accel_group
,
835 kb
->key
, kb
->mods
, GTK_ACCEL_VISIBLE
);
839 #define GEANY_ADD_POPUP_ACCEL(kb_id, wid) \
840 add_menu_accel(group, kb_id, ui_lookup_widget(main_widgets.editor_menu, G_STRINGIFY(wid)))
842 /* set the menu item accelerator shortcuts (just for visibility, they are handled anyway) */
843 /* FIXME: update those during runtime */
844 static void add_popup_menu_accels(void)
846 GeanyKeyGroup
*group
;
848 group
= keybindings_get_core_group(GEANY_KEY_GROUP_EDITOR
);
849 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_UNDO
, undo1
);
850 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_REDO
, redo1
);
851 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_EDITOR_CONTEXTACTION
, context_action1
);
853 group
= keybindings_get_core_group(GEANY_KEY_GROUP_CLIPBOARD
);
854 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_CUT
, cut1
);
855 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_COPY
, copy1
);
856 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_CLIPBOARD_PASTE
, paste1
);
858 group
= keybindings_get_core_group(GEANY_KEY_GROUP_SELECT
);
859 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SELECT_ALL
, menu_select_all2
);
861 group
= keybindings_get_core_group(GEANY_KEY_GROUP_INSERT
);
862 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_DATE
, insert_date_custom2
);
863 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_INSERT_ALTWHITESPACE
, insert_alternative_white_space2
);
865 group
= keybindings_get_core_group(GEANY_KEY_GROUP_FILE
);
866 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_FILE_OPENSELECTED
, menu_open_selected_file2
);
868 group
= keybindings_get_core_group(GEANY_KEY_GROUP_SEARCH
);
869 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDUSAGE
, find_usage2
);
870 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE
, find_document_usage2
);
872 group
= keybindings_get_core_group(GEANY_KEY_GROUP_GOTO
);
873 GEANY_ADD_POPUP_ACCEL(GEANY_KEYS_GOTO_TAGDEFINITION
, goto_tag_definition2
);
875 /* Format and Commands share the menu bar submenus */
876 /* Build menu items are set if the build menus are created */
880 static void set_keyfile_kb(GeanyKeyGroup
*group
, GeanyKeyBinding
*kb
, gpointer user_data
)
882 GKeyFile
*config
= user_data
;
885 val
= gtk_accelerator_name(kb
->key
, kb
->mods
);
886 g_key_file_set_string(config
, group
->name
, kb
->name
, val
);
891 /* just write the content of the keys array to the config file */
892 void keybindings_write_to_file(void)
894 gchar
*configfile
= g_build_filename(app
->configdir
, "keybindings.conf", NULL
);
896 GKeyFile
*config
= g_key_file_new();
898 g_key_file_load_from_file(config
, configfile
, 0, NULL
);
899 keybindings_foreach(set_keyfile_kb
, config
);
902 data
= g_key_file_to_data(config
, NULL
, NULL
);
903 utils_write_file(configfile
, data
);
907 g_key_file_free(config
);
911 void keybindings_free(void)
913 GeanyKeyGroup
*group
;
916 foreach_ptr_array(group
, g
, keybinding_groups
)
917 keybindings_free_group(group
);
919 g_ptr_array_free(keybinding_groups
, TRUE
);
923 gchar
*keybindings_get_label(GeanyKeyBinding
*kb
)
925 return utils_str_remove_chars(g_strdup(kb
->label
), "_");
929 static void fill_shortcut_labels_treeview(GtkWidget
*tree
)
933 GeanyKeyGroup
*group
;
937 store
= gtk_list_store_new(3, G_TYPE_STRING
, G_TYPE_STRING
, PANGO_TYPE_WEIGHT
);
939 foreach_ptr_array(group
, g
, keybinding_groups
)
943 gtk_list_store_append(store
, &iter
);
944 gtk_list_store_set(store
, &iter
, -1);
946 gtk_list_store_append(store
, &iter
);
947 gtk_list_store_set(store
, &iter
, 0, group
->label
, 2, PANGO_WEIGHT_BOLD
, -1);
949 foreach_ptr_array(kb
, i
, group
->key_items
)
951 gchar
*shortcut
, *label
;
953 label
= keybindings_get_label(kb
);
954 shortcut
= gtk_accelerator_get_label(kb
->key
, kb
->mods
);
956 gtk_list_store_append(store
, &iter
);
957 gtk_list_store_set(store
, &iter
, 0, label
, 1, shortcut
, 2, PANGO_WEIGHT_NORMAL
, -1);
963 gtk_tree_view_set_model(GTK_TREE_VIEW(tree
), GTK_TREE_MODEL(store
));
964 g_object_unref(store
);
968 static GtkWidget
*create_dialog(void)
970 GtkWidget
*dialog
, *tree
, *label
, *swin
, *vbox
;
971 GtkCellRenderer
*text_renderer
;
972 GtkTreeViewColumn
*column
;
974 dialog
= gtk_dialog_new_with_buttons(_("Keyboard Shortcuts"), GTK_WINDOW(main_widgets
.window
),
975 GTK_DIALOG_DESTROY_WITH_PARENT
,
976 GTK_STOCK_EDIT
, GTK_RESPONSE_APPLY
,
977 GTK_STOCK_CLOSE
, GTK_RESPONSE_CANCEL
, NULL
);
978 vbox
= ui_dialog_vbox_new(GTK_DIALOG(dialog
));
979 gtk_box_set_spacing(GTK_BOX(vbox
), 6);
980 gtk_widget_set_name(dialog
, "GeanyDialog");
982 gtk_window_set_default_size(GTK_WINDOW(dialog
), -1, GEANY_DEFAULT_DIALOG_HEIGHT
);
984 gtk_dialog_set_default_response(GTK_DIALOG(dialog
), GTK_RESPONSE_CANCEL
);
986 label
= gtk_label_new(_("The following keyboard shortcuts are configurable:"));
987 gtk_misc_set_alignment(GTK_MISC(label
), 0, 0.5);
989 tree
= gtk_tree_view_new();
990 gtk_tree_view_set_rules_hint(GTK_TREE_VIEW(tree
), TRUE
);
991 gtk_tree_view_set_headers_visible(GTK_TREE_VIEW(tree
), FALSE
);
993 text_renderer
= gtk_cell_renderer_text_new();
994 /* we can't use "weight-set", see http://bugzilla.gnome.org/show_bug.cgi?id=355214 */
995 column
= gtk_tree_view_column_new_with_attributes(
996 NULL
, text_renderer
, "text", 0, "weight", 2, NULL
);
997 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
999 text_renderer
= gtk_cell_renderer_text_new();
1000 column
= gtk_tree_view_column_new_with_attributes(NULL
, text_renderer
, "text", 1, NULL
);
1001 gtk_tree_view_append_column(GTK_TREE_VIEW(tree
), column
);
1003 fill_shortcut_labels_treeview(tree
);
1005 swin
= gtk_scrolled_window_new(NULL
, NULL
);
1006 gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(swin
), GTK_POLICY_NEVER
,
1007 GTK_POLICY_AUTOMATIC
);
1008 gtk_scrolled_window_set_shadow_type(GTK_SCROLLED_WINDOW(swin
), GTK_SHADOW_IN
);
1009 gtk_container_add(GTK_CONTAINER(swin
), tree
);
1011 gtk_box_pack_start(GTK_BOX(vbox
), label
, FALSE
, FALSE
, 6);
1012 gtk_box_pack_start(GTK_BOX(vbox
), swin
, TRUE
, TRUE
, 0);
1018 static void key_dialog_show_prefs(void)
1022 prefs_show_dialog();
1023 /* select the KB page */
1024 wid
= ui_lookup_widget(ui_widgets
.prefs_dialog
, "frame22");
1027 GtkNotebook
*nb
= GTK_NOTEBOOK(ui_lookup_widget(ui_widgets
.prefs_dialog
, "notebook2"));
1030 gtk_notebook_set_current_page(nb
, gtk_notebook_page_num(nb
, wid
));
1036 void keybindings_dialog_show_prefs_scroll(const gchar
*name
)
1038 key_dialog_show_prefs();
1039 prefs_kb_search_name(name
);
1043 /* non-modal keyboard shortcuts dialog, so user can edit whilst seeing the shortcuts */
1044 static GtkWidget
*key_dialog
= NULL
;
1046 static void on_dialog_response(GtkWidget
*dialog
, gint response
, gpointer user_data
)
1048 if (response
== GTK_RESPONSE_APPLY
)
1050 key_dialog_show_prefs();
1052 gtk_widget_destroy(dialog
);
1057 void keybindings_show_shortcuts(void)
1060 gtk_widget_destroy(key_dialog
); /* in case the key_dialog is still visible */
1062 key_dialog
= create_dialog();
1063 g_signal_connect(key_dialog
, "response", G_CALLBACK(on_dialog_response
), NULL
);
1064 gtk_widget_show_all(key_dialog
);
1068 static gboolean
check_fixed_kb(guint keyval
, guint state
)
1070 /* check alt-0 to alt-9 for setting current notebook page */
1071 if (state
== GDK_MOD1_MASK
&& keyval
>= GDK_0
&& keyval
<= GDK_9
)
1073 gint page
= keyval
- GDK_0
- 1;
1074 gint npages
= gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets
.notebook
));
1076 /* alt-0 is for the rightmost tab */
1077 if (keyval
== GDK_0
)
1079 /* invert the order if tabs are added on the other side */
1080 if (swap_alt_tab_order
&& ! file_prefs
.tab_order_ltr
)
1081 page
= (npages
- 1) - page
;
1083 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), page
);
1086 /* note: these are now overridden by default with move tab bindings */
1087 if (keyval
== GDK_Page_Up
|| keyval
== GDK_Page_Down
)
1089 /* switch to first or last document */
1090 if (state
== (GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
))
1092 if (keyval
== GDK_Page_Up
)
1093 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), 0);
1094 if (keyval
== GDK_Page_Down
)
1095 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets
.notebook
), -1);
1103 static gboolean
check_snippet_completion(GeanyDocument
*doc
)
1105 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
1107 g_return_val_if_fail(doc
, FALSE
);
1109 /* keybinding only valid when scintilla widget has focus */
1110 if (focusw
== GTK_WIDGET(doc
->editor
->sci
))
1112 ScintillaObject
*sci
= doc
->editor
->sci
;
1113 gint pos
= sci_get_current_position(sci
);
1115 if (editor_prefs
.complete_snippets
)
1116 return editor_complete_snippet(doc
->editor
, pos
);
1122 /* Transforms a GdkEventKey event into a GdkEventButton event */
1123 static void trigger_button_event(GtkWidget
*widget
, guint32 event_time
)
1125 GdkEventButton
*event
;
1128 event
= g_new0(GdkEventButton
, 1);
1130 if (GTK_IS_TEXT_VIEW(widget
))
1131 event
->window
= gtk_text_view_get_window(GTK_TEXT_VIEW(widget
), GTK_TEXT_WINDOW_TEXT
);
1133 event
->window
= gtk_widget_get_window(widget
);
1134 event
->time
= event_time
;
1135 event
->type
= GDK_BUTTON_PRESS
;
1138 g_signal_emit_by_name(widget
, "button-press-event", event
, &ret
);
1139 g_signal_emit_by_name(widget
, "button-release-event", event
, &ret
);
1145 /* Special case for the Menu key and Shift-F10 to show the right-click popup menu for various
1146 * widgets. Without this special handling, the notebook tab list of the documents' notebook
1147 * would be shown. As a very special case, we differentiate between the Menu key and Shift-F10
1148 * if pressed in the editor widget: the Menu key opens the popup menu, Shift-F10 opens the
1149 * notebook tab list. */
1150 static gboolean
check_menu_key(GeanyDocument
*doc
, guint keyval
, guint state
, guint32 event_time
)
1152 g_return_val_if_fail(doc
== NULL
|| doc
->is_valid
, FALSE
);
1154 if ((keyval
== GDK_Menu
&& state
== 0) || (keyval
== GDK_F10
&& state
== GDK_SHIFT_MASK
))
1156 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
1159 if (focusw
== doc
->priv
->tag_tree
)
1161 trigger_button_event(focusw
, event_time
);
1164 if (focusw
== GTK_WIDGET(doc
->editor
->sci
))
1166 if (keyval
== GDK_Menu
)
1167 { /* show editor popup menu */
1168 trigger_button_event(focusw
, event_time
);
1172 { /* show tab bar menu */
1173 trigger_button_event(main_widgets
.notebook
, event_time
);
1178 if (focusw
== tv
.tree_openfiles
1179 || focusw
== msgwindow
.tree_status
1180 || focusw
== msgwindow
.tree_compiler
1181 || focusw
== msgwindow
.tree_msg
1182 || focusw
== msgwindow
.scribble
1184 || (vte_info
.have_vte
&& focusw
== vc
->vte
)
1188 trigger_button_event(focusw
, event_time
);
1197 static gboolean
set_sensitive(gpointer widget
)
1199 gtk_widget_set_sensitive(GTK_WIDGET(widget
), TRUE
);
1204 static gboolean
check_vte(GdkModifierType state
, guint keyval
)
1207 GeanyKeyBinding
*kb
;
1208 GeanyKeyGroup
*group
;
1211 if (gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
)) != vc
->vte
)
1213 /* let VTE copy/paste override any user keybinding */
1214 if (state
== (GEANY_PRIMARY_MOD_MASK
| GDK_SHIFT_MASK
) && (keyval
== GDK_c
|| keyval
== GDK_v
))
1216 if (! vc
->enable_bash_keys
)
1218 /* prevent menubar flickering: */
1219 if (state
== GDK_SHIFT_MASK
&& (keyval
>= GDK_a
&& keyval
<= GDK_z
))
1221 if (state
== 0 && (keyval
< GDK_F1
|| keyval
> GDK_F35
)) /* e.g. backspace */
1224 /* make focus commands override any bash commands */
1225 group
= keybindings_get_core_group(GEANY_KEY_GROUP_FOCUS
);
1226 foreach_ptr_array(kb
, i
, group
->key_items
)
1228 if (state
== kb
->mods
&& keyval
== kb
->key
)
1232 /* Temporarily disable the menus to prevent conflicting menu accelerators
1233 * from overriding the VTE bash shortcuts.
1234 * Note: maybe there's a better way of doing this ;-) */
1235 widget
= ui_lookup_widget(main_widgets
.window
, "menubar1");
1236 gtk_widget_set_sensitive(widget
, FALSE
);
1237 g_idle_add_full(G_PRIORITY_HIGH
, set_sensitive
, widget
, NULL
);
1239 widget
= main_widgets
.editor_menu
;
1240 gtk_widget_set_sensitive(widget
, FALSE
);
1241 g_idle_add(set_sensitive
, widget
);
1247 /* Map the keypad keys to their equivalent functions (taken from ScintillaGTK.cxx) */
1248 static guint
key_kp_translate(guint key_in
)
1264 case GDK_KP_Page_Up
:
1266 case GDK_KP_Page_Down
:
1267 return GDK_Page_Down
;
1278 /* Check if event keypress matches keybinding combo */
1279 gboolean
keybindings_check_event(GdkEventKey
*ev
, GeanyKeyBinding
*kb
)
1281 guint state
, keyval
;
1283 if (ev
->keyval
== 0)
1286 keyval
= ev
->keyval
;
1287 state
= keybindings_get_modifiers(ev
->state
);
1288 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1289 if ((ev
->state
& GDK_SHIFT_MASK
) || (ev
->state
& GDK_LOCK_MASK
))
1290 if (keyval
>= GDK_A
&& keyval
<= GDK_Z
)
1291 keyval
+= GDK_a
- GDK_A
;
1293 if (keyval
>= GDK_KP_Space
&& keyval
< GDK_KP_Equal
)
1294 keyval
= key_kp_translate(keyval
);
1296 return (keyval
== kb
->key
&& state
== kb
->mods
);
1300 static gboolean
run_kb(GeanyKeyBinding
*kb
, GeanyKeyGroup
*group
)
1302 gboolean handled
= TRUE
;
1303 /* call the corresponding handler/callback functions for this shortcut.
1304 * Check the individual keybindings first (handler first, callback second) and
1305 * group second (again handler first, callback second) */
1307 handled
= kb
->cb_func(kb
, kb
->id
, kb
->cb_data
);
1308 else if (kb
->callback
)
1309 kb
->callback(kb
->id
);
1310 else if (group
->cb_func
)
1311 handled
= group
->cb_func(group
, kb
->id
, group
->cb_data
);
1312 else if (group
->callback
)
1313 handled
= group
->callback(kb
->id
);
1316 g_warning("No callback or handler for keybinding %s: %s!", group
->name
, kb
->name
);
1324 /* central keypress event handler, almost all keypress events go to this function */
1325 static gboolean
on_key_press_event(GtkWidget
*widget
, GdkEventKey
*ev
, gpointer user_data
)
1327 guint state
, keyval
;
1330 GeanyKeyGroup
*group
;
1331 GeanyKeyBinding
*kb
;
1333 if (ev
->keyval
== 0)
1336 doc
= document_get_current();
1338 document_check_disk_status(doc
, FALSE
);
1340 keyval
= ev
->keyval
;
1341 state
= keybindings_get_modifiers(ev
->state
);
1342 /* hack to get around that CTRL+Shift+r results in GDK_R not GDK_r */
1343 if ((ev
->state
& GDK_SHIFT_MASK
) || (ev
->state
& GDK_LOCK_MASK
))
1344 if (keyval
>= GDK_A
&& keyval
<= GDK_Z
)
1345 keyval
+= GDK_a
- GDK_A
;
1347 if (keyval
>= GDK_KP_Space
&& keyval
< GDK_KP_Equal
)
1348 keyval
= key_kp_translate(keyval
);
1350 /*geany_debug("%d (%d) %d (%d)", keyval, ev->keyval, state, ev->state);*/
1354 if (vte_info
.have_vte
&& check_vte(state
, keyval
))
1357 if (check_menu_key(doc
, keyval
, state
, ev
->time
))
1360 foreach_ptr_array(group
, g
, keybinding_groups
)
1362 foreach_ptr_array(kb
, i
, group
->key_items
)
1364 if (keyval
== kb
->key
&& state
== kb
->mods
)
1366 if (run_kb(kb
, group
))
1371 /* fixed keybindings can be overridden by user bindings, so check them last */
1372 if (check_fixed_kb(keyval
, state
))
1378 /* group_id must be a core group, e.g. GEANY_KEY_GROUP_EDITOR
1379 * key_id e.g. GEANY_KEYS_EDITOR_CALLTIP */
1380 GeanyKeyBinding
*keybindings_lookup_item(guint group_id
, guint key_id
)
1382 GeanyKeyGroup
*group
;
1384 g_return_val_if_fail(group_id
< GEANY_KEY_GROUP_COUNT
, NULL
); /* can't use this for plugin groups */
1386 group
= keybindings_get_core_group(group_id
);
1388 g_return_val_if_fail(group
, NULL
);
1389 return keybindings_get_item(group
, key_id
);
1393 /** Mimics a (built-in only) keybinding action.
1394 * Example: @code keybindings_send_command(GEANY_KEY_GROUP_FILE, GEANY_KEYS_FILE_OPEN); @endcode
1395 * @param group_id @ref GeanyKeyGroupID keybinding group index that contains the @a key_id keybinding.
1396 * @param key_id @ref GeanyKeyBindingID keybinding index. */
1398 void keybindings_send_command(guint group_id
, guint key_id
)
1400 GeanyKeyBinding
*kb
;
1401 GeanyKeyGroup
*group
;
1403 kb
= keybindings_lookup_item(group_id
, key_id
);
1404 group
= keybindings_get_core_group(group_id
);
1410 /* These are the callback functions, either each group or each shortcut has it's
1414 static gboolean
cb_func_file_action(guint key_id
)
1418 case GEANY_KEYS_FILE_NEW
:
1419 document_new_file(NULL
, NULL
, NULL
);
1421 case GEANY_KEYS_FILE_OPEN
:
1422 on_open1_activate(NULL
, NULL
);
1424 case GEANY_KEYS_FILE_OPENSELECTED
:
1425 on_menu_open_selected_file1_activate(NULL
, NULL
);
1427 case GEANY_KEYS_FILE_OPENLASTTAB
:
1429 gchar
*utf8_filename
= g_queue_peek_head(ui_prefs
.recent_queue
);
1430 gchar
*locale_filename
= utils_get_locale_from_utf8(utf8_filename
);
1431 document_open_file(locale_filename
, FALSE
, NULL
, NULL
);
1432 g_free(locale_filename
);
1435 case GEANY_KEYS_FILE_SAVE
:
1436 on_save1_activate(NULL
, NULL
);
1438 case GEANY_KEYS_FILE_SAVEAS
:
1439 on_save_as1_activate(NULL
, NULL
);
1441 case GEANY_KEYS_FILE_SAVEALL
:
1442 on_save_all1_activate(NULL
, NULL
);
1444 case GEANY_KEYS_FILE_CLOSE
:
1445 on_close1_activate(NULL
, NULL
);
1447 case GEANY_KEYS_FILE_CLOSEALL
:
1448 on_close_all1_activate(NULL
, NULL
);
1450 case GEANY_KEYS_FILE_RELOAD
:
1451 on_toolbutton_reload_clicked(NULL
, NULL
);
1453 case GEANY_KEYS_FILE_PRINT
:
1454 on_print1_activate(NULL
, NULL
);
1456 case GEANY_KEYS_FILE_PROPERTIES
:
1457 on_file_properties_activate(NULL
, NULL
);
1459 case GEANY_KEYS_FILE_QUIT
:
1467 static gboolean
cb_func_project_action(guint key_id
)
1471 case GEANY_KEYS_PROJECT_NEW
:
1472 on_project_new1_activate(NULL
, NULL
);
1474 case GEANY_KEYS_PROJECT_OPEN
:
1475 on_project_open1_activate(NULL
, NULL
);
1477 case GEANY_KEYS_PROJECT_CLOSE
:
1479 on_project_close1_activate(NULL
, NULL
);
1481 case GEANY_KEYS_PROJECT_PROPERTIES
:
1483 on_project_properties1_activate(NULL
, NULL
);
1490 static void cb_func_menu_preferences(guint key_id
)
1494 case GEANY_KEYS_SETTINGS_PREFERENCES
:
1495 on_preferences1_activate(NULL
, NULL
);
1497 case GEANY_KEYS_SETTINGS_PLUGINPREFERENCES
:
1498 on_plugin_preferences1_activate(NULL
, NULL
);
1504 static void cb_func_menu_help(G_GNUC_UNUSED guint key_id
)
1506 on_help1_activate(NULL
, NULL
);
1510 static gboolean
cb_func_search_action(guint key_id
)
1512 GeanyDocument
*doc
= document_get_current();
1513 ScintillaObject
*sci
;
1515 /* these work without docs */
1518 case GEANY_KEYS_SEARCH_FINDINFILES
:
1519 on_find_in_files1_activate(NULL
, NULL
); return TRUE
;
1520 case GEANY_KEYS_SEARCH_NEXTMESSAGE
:
1521 on_next_message1_activate(NULL
, NULL
); return TRUE
;
1522 case GEANY_KEYS_SEARCH_PREVIOUSMESSAGE
:
1523 on_previous_message1_activate(NULL
, NULL
); return TRUE
;
1527 sci
= doc
->editor
->sci
;
1531 case GEANY_KEYS_SEARCH_FIND
:
1532 on_find1_activate(NULL
, NULL
); break;
1533 case GEANY_KEYS_SEARCH_FINDNEXT
:
1534 on_find_next1_activate(NULL
, NULL
); break;
1535 case GEANY_KEYS_SEARCH_FINDPREVIOUS
:
1536 on_find_previous1_activate(NULL
, NULL
); break;
1537 case GEANY_KEYS_SEARCH_FINDPREVSEL
:
1538 on_find_prevsel1_activate(NULL
, NULL
); break;
1539 case GEANY_KEYS_SEARCH_FINDNEXTSEL
:
1540 on_find_nextsel1_activate(NULL
, NULL
); break;
1541 case GEANY_KEYS_SEARCH_REPLACE
:
1542 on_replace1_activate(NULL
, NULL
); break;
1543 case GEANY_KEYS_SEARCH_FINDUSAGE
:
1544 on_find_usage1_activate(NULL
, NULL
); break;
1545 case GEANY_KEYS_SEARCH_FINDDOCUMENTUSAGE
:
1546 on_find_document_usage1_activate(NULL
, NULL
); break;
1547 case GEANY_KEYS_SEARCH_MARKALL
:
1550 gint pos
= sci_get_current_position(sci
);
1552 /* clear existing search indicators instead if next to cursor */
1553 if (scintilla_send_message(sci
, SCI_INDICATORVALUEAT
,
1554 GEANY_INDICATOR_SEARCH
, pos
) ||
1555 scintilla_send_message(sci
, SCI_INDICATORVALUEAT
,
1556 GEANY_INDICATOR_SEARCH
, MAX(pos
- 1, 0)))
1559 text
= get_current_word_or_sel(doc
, TRUE
);
1561 if (sci_has_selection(sci
))
1562 search_mark_all(doc
, text
, GEANY_FIND_MATCHCASE
);
1564 search_mark_all(doc
, text
, GEANY_FIND_MATCHCASE
| GEANY_FIND_WHOLEWORD
);
1574 static void cb_func_menu_opencolorchooser(G_GNUC_UNUSED guint key_id
)
1576 on_show_color_chooser1_activate(NULL
, NULL
);
1580 static gboolean
cb_func_view_action(guint key_id
)
1584 case GEANY_KEYS_VIEW_TOGGLEALL
:
1585 on_menu_toggle_all_additional_widgets1_activate(NULL
, NULL
);
1587 case GEANY_KEYS_VIEW_SIDEBAR
:
1588 on_menu_show_sidebar1_toggled(NULL
, NULL
);
1590 case GEANY_KEYS_VIEW_ZOOMIN
:
1591 on_zoom_in1_activate(NULL
, NULL
);
1593 case GEANY_KEYS_VIEW_ZOOMOUT
:
1594 on_zoom_out1_activate(NULL
, NULL
);
1596 case GEANY_KEYS_VIEW_ZOOMRESET
:
1597 on_normal_size1_activate(NULL
, NULL
);
1606 static void cb_func_menu_fullscreen(G_GNUC_UNUSED guint key_id
)
1608 GtkCheckMenuItem
*c
= GTK_CHECK_MENU_ITEM(
1609 ui_lookup_widget(main_widgets
.window
, "menu_fullscreen1"));
1611 gtk_check_menu_item_set_active(c
, ! gtk_check_menu_item_get_active(c
));
1615 static void cb_func_menu_messagewindow(G_GNUC_UNUSED guint key_id
)
1617 GtkCheckMenuItem
*c
= GTK_CHECK_MENU_ITEM(
1618 ui_lookup_widget(main_widgets
.window
, "menu_show_messages_window1"));
1620 gtk_check_menu_item_set_active(c
, ! gtk_check_menu_item_get_active(c
));
1624 static gboolean
cb_func_build_action(guint key_id
)
1627 BuildMenuItems
*menu_items
;
1628 GeanyDocument
*doc
= document_get_current();
1633 if (!gtk_widget_is_sensitive(ui_lookup_widget(main_widgets
.window
, "menu_build1")))
1636 menu_items
= build_get_menu_items(doc
->file_type
->id
);
1637 /* TODO make it a table??*/
1640 case GEANY_KEYS_BUILD_COMPILE
:
1641 item
= menu_items
->menu_item
[GEANY_GBG_FT
][GBO_TO_CMD(GEANY_GBO_COMPILE
)];
1643 case GEANY_KEYS_BUILD_LINK
:
1644 item
= menu_items
->menu_item
[GEANY_GBG_FT
][GBO_TO_CMD(GEANY_GBO_BUILD
)];
1646 case GEANY_KEYS_BUILD_MAKE
:
1647 item
= menu_items
->menu_item
[GEANY_GBG_NON_FT
][GBO_TO_CMD(GEANY_GBO_MAKE_ALL
)];
1649 case GEANY_KEYS_BUILD_MAKEOWNTARGET
:
1650 item
= menu_items
->menu_item
[GEANY_GBG_NON_FT
][GBO_TO_CMD(GEANY_GBO_CUSTOM
)];
1652 case GEANY_KEYS_BUILD_MAKEOBJECT
:
1653 item
= menu_items
->menu_item
[GEANY_GBG_NON_FT
][GBO_TO_CMD(GEANY_GBO_MAKE_OBJECT
)];
1655 case GEANY_KEYS_BUILD_NEXTERROR
:
1656 item
= menu_items
->menu_item
[GBG_FIXED
][GBF_NEXT_ERROR
];
1658 case GEANY_KEYS_BUILD_PREVIOUSERROR
:
1659 item
= menu_items
->menu_item
[GBG_FIXED
][GBF_PREV_ERROR
];
1661 case GEANY_KEYS_BUILD_RUN
:
1662 item
= menu_items
->menu_item
[GEANY_GBG_EXEC
][GBO_TO_CMD(GEANY_GBO_EXEC
)];
1664 case GEANY_KEYS_BUILD_OPTIONS
:
1665 item
= menu_items
->menu_item
[GBG_FIXED
][GBF_COMMANDS
];
1670 /* Note: For Build menu items it's OK (at the moment) to assume they are in the correct
1671 * sensitive state, but some other menus don't update the sensitive status until
1672 * they are redrawn. */
1673 if (item
&& gtk_widget_is_sensitive(item
))
1674 gtk_menu_item_activate(GTK_MENU_ITEM(item
));
1679 static gboolean
read_current_word(GeanyDocument
*doc
, gboolean sci_word
)
1681 g_return_val_if_fail(DOC_VALID(doc
), FALSE
);
1685 editor_find_current_word_sciwc(doc
->editor
, -1,
1686 editor_info
.current_word
, GEANY_MAX_WORD_LENGTH
);
1690 editor_find_current_word(doc
->editor
, -1,
1691 editor_info
.current_word
, GEANY_MAX_WORD_LENGTH
, NULL
);
1694 return (*editor_info
.current_word
!= 0);
1698 static gboolean
check_current_word(GeanyDocument
*doc
, gboolean sci_word
)
1700 if (! read_current_word(doc
, sci_word
))
1709 static gchar
*get_current_word_or_sel(GeanyDocument
*doc
, gboolean sci_word
)
1711 ScintillaObject
*sci
= doc
->editor
->sci
;
1713 if (sci_has_selection(sci
))
1714 return sci_get_selection_contents(sci
);
1716 return read_current_word(doc
, sci_word
) ? g_strdup(editor_info
.current_word
) : NULL
;
1720 static void focus_sidebar(void)
1722 if (ui_prefs
.sidebar_visible
)
1724 gint page_num
= gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
));
1725 GtkWidget
*page
= gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets
.sidebar_notebook
), page_num
);
1727 /* gtk_widget_grab_focus() won't work because of the scrolled window containers */
1728 gtk_widget_child_focus(page
, GTK_DIR_TAB_FORWARD
);
1733 static void focus_msgwindow(void)
1735 if (ui_prefs
.msgwindow_visible
)
1737 gint page_num
= gtk_notebook_get_current_page(GTK_NOTEBOOK(msgwindow
.notebook
));
1738 GtkWidget
*page
= gtk_notebook_get_nth_page(GTK_NOTEBOOK(msgwindow
.notebook
), page_num
);
1740 gtk_widget_grab_focus(gtk_bin_get_child(GTK_BIN(page
)));
1745 static gboolean
cb_func_switch_action(guint key_id
)
1749 case GEANY_KEYS_FOCUS_EDITOR
:
1751 GeanyDocument
*doc
= document_get_current();
1754 GtkWidget
*sci
= GTK_WIDGET(doc
->editor
->sci
);
1755 if (gtk_widget_has_focus(sci
))
1756 ui_update_statusbar(doc
, -1);
1758 gtk_widget_grab_focus(sci
);
1762 case GEANY_KEYS_FOCUS_SCRIBBLE
:
1763 msgwin_switch_tab(MSG_SCRATCH
, TRUE
);
1765 case GEANY_KEYS_FOCUS_SEARCHBAR
:
1766 if (toolbar_prefs
.visible
)
1768 GtkWidget
*search_entry
= toolbar_get_widget_child_by_name("SearchEntry");
1769 if (search_entry
!= NULL
)
1770 gtk_widget_grab_focus(search_entry
);
1773 case GEANY_KEYS_FOCUS_SIDEBAR
:
1776 case GEANY_KEYS_FOCUS_VTE
:
1777 msgwin_switch_tab(MSG_VTE
, TRUE
);
1779 case GEANY_KEYS_FOCUS_COMPILER
:
1780 msgwin_switch_tab(MSG_COMPILER
, TRUE
);
1782 case GEANY_KEYS_FOCUS_MESSAGES
:
1783 msgwin_switch_tab(MSG_MESSAGE
, TRUE
);
1785 case GEANY_KEYS_FOCUS_MESSAGE_WINDOW
:
1788 case GEANY_KEYS_FOCUS_SIDEBAR_DOCUMENT_LIST
:
1789 sidebar_focus_openfiles_tab();
1791 case GEANY_KEYS_FOCUS_SIDEBAR_SYMBOL_LIST
:
1792 sidebar_focus_symbols_tab();
1799 static void switch_notebook_page(gint direction
)
1801 gint page_count
, cur_page
, pass
;
1802 gboolean parent_is_notebook
= FALSE
;
1803 GtkNotebook
*notebook
;
1804 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
1806 /* check whether the current widget is a GtkNotebook or a child of a GtkNotebook */
1809 parent_is_notebook
= GTK_IS_NOTEBOOK(focusw
);
1811 while (! parent_is_notebook
&& (focusw
= gtk_widget_get_parent(focusw
)) != NULL
);
1813 /* if we found a GtkNotebook widget, use it. Otherwise fallback to the documents notebook */
1814 if (parent_is_notebook
)
1815 notebook
= GTK_NOTEBOOK(focusw
);
1817 notebook
= GTK_NOTEBOOK(main_widgets
.notebook
);
1819 /* now switch pages */
1820 page_count
= gtk_notebook_get_n_pages(notebook
);
1821 cur_page
= gtk_notebook_get_current_page(notebook
);
1823 /* find the next visible page in the wanted direction, but don't loop
1824 * indefinitely if no pages are visible */
1825 for (pass
= 0; pass
< page_count
; pass
++)
1829 if (direction
== GTK_DIR_LEFT
)
1834 cur_page
= page_count
- 1;
1836 else if (direction
== GTK_DIR_RIGHT
)
1838 if (cur_page
< page_count
- 1)
1844 child
= gtk_notebook_get_nth_page (notebook
, cur_page
);
1845 if (gtk_widget_get_visible (child
))
1847 gtk_notebook_set_current_page(notebook
, cur_page
);
1854 static void cb_func_switch_tableft(G_GNUC_UNUSED guint key_id
)
1856 switch_notebook_page(GTK_DIR_LEFT
);
1860 static void cb_func_switch_tabright(G_GNUC_UNUSED guint key_id
)
1862 switch_notebook_page(GTK_DIR_RIGHT
);
1866 static void cb_func_switch_tablastused(G_GNUC_UNUSED guint key_id
)
1868 notebook_switch_tablastused();
1872 /* move document left/right/first/last */
1873 static void cb_func_move_tab(guint key_id
)
1876 GtkNotebook
*nb
= GTK_NOTEBOOK(main_widgets
.notebook
);
1877 gint cur_page
= gtk_notebook_get_current_page(nb
);
1882 child
= gtk_notebook_get_nth_page(nb
, cur_page
);
1886 case GEANY_KEYS_NOTEBOOK_MOVETABLEFT
:
1887 gtk_notebook_reorder_child(nb
, child
, cur_page
- 1); /* notebook wraps around by default */
1889 case GEANY_KEYS_NOTEBOOK_MOVETABRIGHT
:
1891 gint npage
= cur_page
+ 1;
1893 if (npage
== gtk_notebook_get_n_pages(nb
))
1894 npage
= 0; /* wraparound */
1895 gtk_notebook_reorder_child(nb
, child
, npage
);
1898 case GEANY_KEYS_NOTEBOOK_MOVETABFIRST
:
1899 gtk_notebook_reorder_child(nb
, child
, (file_prefs
.tab_order_ltr
) ? 0 : -1);
1901 case GEANY_KEYS_NOTEBOOK_MOVETABLAST
:
1902 gtk_notebook_reorder_child(nb
, child
, (file_prefs
.tab_order_ltr
) ? -1 : 0);
1909 static void goto_matching_brace(GeanyDocument
*doc
)
1914 g_return_if_fail(DOC_VALID(doc
));
1916 pos
= sci_get_current_position(doc
->editor
->sci
);
1917 after_brace
= pos
> 0 && utils_isbrace(sci_get_char_at(doc
->editor
->sci
, pos
- 1), TRUE
);
1918 pos
-= after_brace
; /* set pos to the brace */
1920 new_pos
= sci_find_matching_brace(doc
->editor
->sci
, pos
);
1922 { /* set the cursor at/after the brace */
1923 sci_set_current_position(doc
->editor
->sci
, new_pos
+ (!after_brace
), FALSE
);
1924 editor_display_current_line(doc
->editor
, 0.5F
);
1929 static gboolean
cb_func_clipboard_action(guint key_id
)
1931 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
1935 case GEANY_KEYS_CLIPBOARD_CUT
:
1936 on_cut1_activate(NULL
, NULL
);
1938 case GEANY_KEYS_CLIPBOARD_COPY
:
1939 on_copy1_activate(NULL
, NULL
);
1941 case GEANY_KEYS_CLIPBOARD_PASTE
:
1942 on_paste1_activate(NULL
, NULL
);
1944 case GEANY_KEYS_CLIPBOARD_COPYLINE
:
1945 if (IS_SCINTILLA(focusw
))
1946 sci_send_command(SCINTILLA(focusw
), SCI_LINECOPY
);
1948 case GEANY_KEYS_CLIPBOARD_CUTLINE
:
1949 if (IS_SCINTILLA(focusw
))
1950 sci_send_command(SCINTILLA(focusw
), SCI_LINECUT
);
1957 static void goto_tag(GeanyDocument
*doc
, gboolean definition
)
1959 gchar
*text
= get_current_word_or_sel(doc
, FALSE
);
1962 symbols_goto_tag(text
, definition
);
1970 /* Common function for goto keybindings, useful even when sci doesn't have focus. */
1971 static gboolean
cb_func_goto_action(guint key_id
)
1974 GeanyDocument
*doc
= document_get_current();
1979 cur_line
= sci_get_current_line(doc
->editor
->sci
);
1983 case GEANY_KEYS_GOTO_BACK
:
1986 case GEANY_KEYS_GOTO_FORWARD
:
1987 navqueue_go_forward();
1989 case GEANY_KEYS_GOTO_LINE
:
1991 if (toolbar_prefs
.visible
)
1993 GtkWidget
*wid
= toolbar_get_widget_child_by_name("GotoEntry");
1995 /* use toolbar item if shown & not in the drop down overflow menu */
1996 if (wid
&& gtk_widget_get_mapped(wid
))
1998 gtk_widget_grab_focus(wid
);
2002 on_go_to_line_activate(NULL
, NULL
);
2005 case GEANY_KEYS_GOTO_MATCHINGBRACE
:
2006 goto_matching_brace(doc
);
2008 case GEANY_KEYS_GOTO_TOGGLEMARKER
:
2010 sci_toggle_marker_at_line(doc
->editor
->sci
, cur_line
, 1);
2013 case GEANY_KEYS_GOTO_NEXTMARKER
:
2015 gint mline
= sci_marker_next(doc
->editor
->sci
, cur_line
+ 1, 1 << 1, TRUE
);
2019 sci_set_current_line(doc
->editor
->sci
, mline
);
2020 editor_display_current_line(doc
->editor
, 0.5F
);
2024 case GEANY_KEYS_GOTO_PREVIOUSMARKER
:
2026 gint mline
= sci_marker_previous(doc
->editor
->sci
, cur_line
- 1, 1 << 1, TRUE
);
2030 sci_set_current_line(doc
->editor
->sci
, mline
);
2031 editor_display_current_line(doc
->editor
, 0.5F
);
2035 case GEANY_KEYS_GOTO_TAGDEFINITION
:
2036 goto_tag(doc
, TRUE
);
2038 case GEANY_KEYS_GOTO_TAGDECLARATION
:
2039 goto_tag(doc
, FALSE
);
2042 /* only check editor-sensitive keybindings when editor has focus so home,end still
2043 * work in other widgets */
2044 if (gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
)) != GTK_WIDGET(doc
->editor
->sci
))
2049 case GEANY_KEYS_GOTO_LINESTART
:
2050 sci_send_command(doc
->editor
->sci
, editor_prefs
.smart_home_key
? SCI_VCHOME
: SCI_HOME
);
2052 case GEANY_KEYS_GOTO_LINEEND
:
2053 sci_send_command(doc
->editor
->sci
, SCI_LINEEND
);
2055 case GEANY_KEYS_GOTO_LINESTARTVISUAL
:
2056 sci_send_command(doc
->editor
->sci
, editor_prefs
.smart_home_key
? SCI_VCHOMEDISPLAY
: SCI_HOMEDISPLAY
);
2058 case GEANY_KEYS_GOTO_LINEENDVISUAL
:
2059 sci_send_command(doc
->editor
->sci
, SCI_LINEENDDISPLAY
);
2061 case GEANY_KEYS_GOTO_PREVWORDPART
:
2062 sci_send_command(doc
->editor
->sci
, SCI_WORDPARTLEFT
);
2064 case GEANY_KEYS_GOTO_NEXTWORDPART
:
2065 sci_send_command(doc
->editor
->sci
, SCI_WORDPARTRIGHT
);
2072 static void duplicate_lines(GeanyEditor
*editor
)
2074 if (sci_get_lines_selected(editor
->sci
) > 1)
2075 { /* ignore extra_line because of selecting lines from the line number column */
2076 editor_select_lines(editor
, FALSE
);
2077 sci_selection_duplicate(editor
->sci
);
2079 else if (sci_has_selection(editor
->sci
))
2080 sci_selection_duplicate(editor
->sci
);
2082 sci_line_duplicate(editor
->sci
);
2086 static void delete_lines(GeanyEditor
*editor
)
2088 editor_select_lines(editor
, TRUE
); /* include last line (like cut lines, copy lines do) */
2089 sci_clear(editor
->sci
); /* (SCI_LINEDELETE only does 1 line) */
2093 /* common function for editor keybindings, only valid when scintilla has focus. */
2094 static gboolean
cb_func_editor_action(guint key_id
)
2096 GeanyDocument
*doc
= document_get_current();
2097 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
2099 /* edit keybindings only valid when scintilla widget has focus */
2100 if (doc
== NULL
|| focusw
!= GTK_WIDGET(doc
->editor
->sci
))
2101 return FALSE
; /* also makes tab work outside editor */
2105 case GEANY_KEYS_EDITOR_UNDO
:
2106 on_undo1_activate(NULL
, NULL
);
2108 case GEANY_KEYS_EDITOR_REDO
:
2109 on_redo1_activate(NULL
, NULL
);
2111 case GEANY_KEYS_EDITOR_SCROLLTOLINE
:
2112 editor_scroll_to_line(doc
->editor
, -1, 0.5F
);
2114 case GEANY_KEYS_EDITOR_SCROLLLINEUP
:
2115 sci_send_command(doc
->editor
->sci
, SCI_LINESCROLLUP
);
2117 case GEANY_KEYS_EDITOR_SCROLLLINEDOWN
:
2118 sci_send_command(doc
->editor
->sci
, SCI_LINESCROLLDOWN
);
2120 case GEANY_KEYS_EDITOR_DUPLICATELINE
:
2121 duplicate_lines(doc
->editor
);
2123 case GEANY_KEYS_EDITOR_SNIPPETNEXTCURSOR
:
2124 editor_goto_next_snippet_cursor(doc
->editor
);
2126 case GEANY_KEYS_EDITOR_DELETELINE
:
2127 delete_lines(doc
->editor
);
2129 case GEANY_KEYS_EDITOR_DELETELINETOEND
:
2130 sci_send_command(doc
->editor
->sci
, SCI_DELLINERIGHT
);
2132 case GEANY_KEYS_EDITOR_TRANSPOSELINE
:
2133 sci_send_command(doc
->editor
->sci
, SCI_LINETRANSPOSE
);
2135 case GEANY_KEYS_EDITOR_AUTOCOMPLETE
:
2136 editor_start_auto_complete(doc
->editor
, sci_get_current_position(doc
->editor
->sci
), TRUE
);
2138 case GEANY_KEYS_EDITOR_CALLTIP
:
2139 editor_show_calltip(doc
->editor
, -1);
2141 case GEANY_KEYS_EDITOR_CONTEXTACTION
:
2142 if (check_current_word(doc
, FALSE
))
2143 on_context_action1_activate(GTK_MENU_ITEM(ui_lookup_widget(main_widgets
.editor_menu
,
2144 "context_action1")), NULL
);
2146 case GEANY_KEYS_EDITOR_COMPLETESNIPPET
:
2147 /* allow tab to be overloaded */
2148 return check_snippet_completion(doc
);
2150 case GEANY_KEYS_EDITOR_SUPPRESSSNIPPETCOMPLETION
:
2152 GeanyKeyBinding
*kb
= keybindings_lookup_item(GEANY_KEY_GROUP_EDITOR
,
2153 GEANY_KEYS_EDITOR_COMPLETESNIPPET
);
2158 sci_add_text(doc
->editor
->sci
, " ");
2161 sci_send_command(doc
->editor
->sci
, SCI_TAB
);
2168 case GEANY_KEYS_EDITOR_WORDPARTCOMPLETION
:
2169 return editor_complete_word_part(doc
->editor
);
2171 case GEANY_KEYS_EDITOR_MOVELINEUP
:
2172 sci_move_selected_lines_up(doc
->editor
->sci
);
2174 case GEANY_KEYS_EDITOR_MOVELINEDOWN
:
2175 sci_move_selected_lines_down(doc
->editor
->sci
);
2182 static void join_lines(GeanyEditor
*editor
)
2186 start
= sci_get_line_from_position(editor
->sci
,
2187 sci_get_selection_start(editor
->sci
));
2188 end
= sci_get_line_from_position(editor
->sci
,
2189 sci_get_selection_end(editor
->sci
));
2191 /* remove spaces surrounding the lines so that these spaces
2192 * won't appear within text after joining */
2193 for (i
= start
; i
< end
; i
++)
2194 editor_strip_line_trailing_spaces(editor
, i
);
2195 for (i
= start
+ 1; i
<= end
; i
++)
2196 sci_set_line_indentation(editor
->sci
, i
, 0);
2198 sci_set_target_start(editor
->sci
,
2199 sci_get_position_from_line(editor
->sci
, start
));
2200 sci_set_target_end(editor
->sci
,
2201 sci_get_position_from_line(editor
->sci
, end
));
2202 sci_lines_join(editor
->sci
);
2206 static gint
get_reflow_column(GeanyEditor
*editor
)
2208 const GeanyEditorPrefs
*eprefs
= editor_get_prefs(editor
);
2209 if (editor
->line_breaking
)
2210 return eprefs
->line_break_column
;
2211 else if (eprefs
->long_line_type
!= 2)
2212 return eprefs
->long_line_column
;
2214 return -1; /* do nothing */
2218 /* Split the line where the cursor is positioned, on `column`,
2219 possibly many times if the line is long.
2220 Return the number of lines added because of the splitting. */
2221 static gint
split_line(GeanyEditor
*editor
, gint column
)
2223 ScintillaObject
*sci
= editor
->sci
;
2224 gint start_line
= sci_get_current_line(sci
);
2225 gint line
= start_line
;
2229 gint lstart
= sci_get_position_from_line(sci
, line
);
2230 gint lend
= sci_get_line_end_position(sci
, line
);
2231 gint edge
= sci_get_position_from_col(sci
, line
, column
);
2235 /* don't split on a trailing space of a line */
2236 if (sci_get_char_at(sci
, lend
- 1) == ' ')
2239 /* detect when the line is short enough and no more splitting is needed */
2240 if (sci_get_col_from_position(sci
, lend
) < column
)
2243 /* lookup split position */
2245 for (pos
= edge
- 1; pos
> lstart
; pos
--)
2247 if (sci_get_char_at(sci
, pos
) == ' ')
2255 for (pos
= edge
; pos
< lend
; pos
++)
2257 if (sci_get_char_at(sci
, pos
) == ' ')
2264 /* don't split right before a space */
2265 while (pos
+ 1 <= lend
&& sci_get_char_at(sci
, pos
+ 1) == ' ')
2268 if (!found
|| pos
>= lend
)
2271 sci_insert_text(sci
, pos
+ 1, editor_get_eol_char(editor
));
2274 return line
- start_line
;
2278 static void reflow_lines(GeanyEditor
*editor
, gint column
)
2280 gint start
, indent
, linescount
, i
;
2282 start
= sci_get_line_from_position(editor
->sci
,
2283 sci_get_selection_start(editor
->sci
));
2285 /* if several lines are selected, join them. */
2286 if (sci_get_lines_selected(editor
->sci
) > 1)
2289 /* if this line is short enough, do nothing */
2290 if (column
> sci_get_line_end_position(editor
->sci
, start
) -
2291 sci_get_position_from_line(editor
->sci
, start
))
2297 * We have to manipulate line indentation so that indentation
2298 * of the resulting lines would be consistent. For example,
2299 * the result of splitting "[TAB]very long content":
2301 * +-------------+-------------+
2302 * | proper | wrong |
2303 * +-------------+-------------+
2304 * | [TAB]very | [TAB]very |
2305 * | [TAB]long | long |
2306 * | [TAB]content| content |
2307 * +-------------+-------------+
2309 indent
= sci_get_line_indentation(editor
->sci
, start
);
2310 sci_set_line_indentation(editor
->sci
, start
, 0);
2312 linescount
= split_line(editor
, column
- indent
);
2314 /* Fix indentation. */
2315 for (i
= start
; i
<= start
+ linescount
; i
++)
2316 sci_set_line_indentation(editor
->sci
, i
, indent
);
2318 /* Remove trailing spaces. */
2319 if (editor_prefs
.newline_strip
|| file_prefs
.strip_trailing_spaces
)
2321 for (i
= start
; i
<= start
+ linescount
; i
++)
2322 editor_strip_line_trailing_spaces(editor
, i
);
2327 /* deselect last newline of selection, if any */
2328 static void sci_deselect_last_newline(ScintillaObject
*sci
)
2332 start
= sci_get_selection_start(sci
);
2333 end
= sci_get_selection_end(sci
);
2334 if (end
> start
&& sci_get_col_from_position(sci
, end
) == 0)
2336 end
= sci_get_line_end_position(sci
, sci_get_line_from_position(sci
, end
- 1));
2337 sci_set_selection(sci
, start
, end
);
2342 static void reflow_paragraph(GeanyEditor
*editor
)
2344 ScintillaObject
*sci
= editor
->sci
;
2348 column
= get_reflow_column(editor
);
2355 sci_start_undo_action(sci
);
2356 sel
= sci_has_selection(sci
);
2358 editor_select_indent_block(editor
);
2359 sci_deselect_last_newline(sci
);
2360 reflow_lines(editor
, column
);
2362 sci_set_anchor(sci
, -1);
2363 sci_goto_pos(sci
, sci_get_line_end_position(sci
, sci_get_current_line(sci
)), TRUE
);
2365 sci_end_undo_action(sci
);
2369 static void join_paragraph(GeanyEditor
*editor
)
2371 ScintillaObject
*sci
= editor
->sci
;
2375 column
= get_reflow_column(editor
);
2382 sci_start_undo_action(sci
);
2383 sel
= sci_has_selection(sci
);
2385 editor_select_indent_block(editor
);
2386 sci_deselect_last_newline(sci
);
2389 sci_set_anchor(sci
, -1);
2391 sci_end_undo_action(sci
);
2395 /* common function for format keybindings, only valid when scintilla has focus. */
2396 static gboolean
cb_func_format_action(guint key_id
)
2398 GeanyDocument
*doc
= document_get_current();
2399 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
2401 /* keybindings only valid when scintilla widget has focus */
2402 if (doc
== NULL
|| focusw
!= GTK_WIDGET(doc
->editor
->sci
))
2407 case GEANY_KEYS_FORMAT_COMMENTLINETOGGLE
:
2408 on_menu_toggle_line_commentation1_activate(NULL
, NULL
);
2410 case GEANY_KEYS_FORMAT_COMMENTLINE
:
2411 on_menu_comment_line1_activate(NULL
, NULL
);
2413 case GEANY_KEYS_FORMAT_UNCOMMENTLINE
:
2414 on_menu_uncomment_line1_activate(NULL
, NULL
);
2416 case GEANY_KEYS_FORMAT_INCREASEINDENT
:
2417 on_menu_increase_indent1_activate(NULL
, NULL
);
2419 case GEANY_KEYS_FORMAT_DECREASEINDENT
:
2420 on_menu_decrease_indent1_activate(NULL
, NULL
);
2422 case GEANY_KEYS_FORMAT_INCREASEINDENTBYSPACE
:
2423 editor_indentation_by_one_space(doc
->editor
, -1, FALSE
);
2425 case GEANY_KEYS_FORMAT_DECREASEINDENTBYSPACE
:
2426 editor_indentation_by_one_space(doc
->editor
, -1, TRUE
);
2428 case GEANY_KEYS_FORMAT_AUTOINDENT
:
2429 editor_smart_line_indentation(doc
->editor
, -1);
2431 case GEANY_KEYS_FORMAT_TOGGLECASE
:
2432 on_toggle_case1_activate(NULL
, NULL
);
2434 case GEANY_KEYS_FORMAT_SENDTOCMD1
:
2435 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 0)
2436 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[0]);
2438 case GEANY_KEYS_FORMAT_SENDTOCMD2
:
2439 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 1)
2440 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[1]);
2442 case GEANY_KEYS_FORMAT_SENDTOCMD3
:
2443 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 2)
2444 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[2]);
2446 case GEANY_KEYS_FORMAT_SENDTOCMD4
:
2447 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 3)
2448 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[3]);
2450 case GEANY_KEYS_FORMAT_SENDTOCMD5
:
2451 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 4)
2452 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[4]);
2454 case GEANY_KEYS_FORMAT_SENDTOCMD6
:
2455 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 5)
2456 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[5]);
2458 case GEANY_KEYS_FORMAT_SENDTOCMD7
:
2459 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 6)
2460 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[6]);
2462 case GEANY_KEYS_FORMAT_SENDTOCMD8
:
2463 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 7)
2464 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[7]);
2466 case GEANY_KEYS_FORMAT_SENDTOCMD9
:
2467 if (ui_prefs
.custom_commands
&& g_strv_length(ui_prefs
.custom_commands
) > 8)
2468 tools_execute_custom_command(doc
, ui_prefs
.custom_commands
[8]);
2470 case GEANY_KEYS_FORMAT_SENDTOVTE
:
2471 on_send_selection_to_vte1_activate(NULL
, NULL
);
2473 case GEANY_KEYS_FORMAT_REFLOWPARAGRAPH
:
2474 reflow_paragraph(doc
->editor
);
2476 case GEANY_KEYS_FORMAT_JOINLINES
:
2477 join_paragraph(doc
->editor
);
2484 /* common function for select keybindings, valid for scintilla and/or gtk_editable objects. */
2485 static gboolean
cb_func_select_action(guint key_id
)
2487 GeanyDocument
*doc
= document_get_current();
2488 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
2492 case GEANY_KEYS_SELECT_ALL
:
2493 on_menu_select_all1_activate(NULL
, NULL
);
2495 case GEANY_KEYS_SELECT_WORD
:
2497 editor_select_word(doc
->editor
);
2499 case GEANY_KEYS_SELECT_LINE
:
2501 editor_select_lines(doc
->editor
, FALSE
);
2503 case GEANY_KEYS_SELECT_PARAGRAPH
:
2505 editor_select_paragraph(doc
->editor
);
2507 case GEANY_KEYS_SELECT_WORDPARTLEFT
:
2508 if (IS_SCINTILLA(focusw
))
2509 sci_send_command(SCINTILLA(focusw
), SCI_WORDPARTLEFTEXTEND
);
2511 case GEANY_KEYS_SELECT_WORDPARTRIGHT
:
2512 if (IS_SCINTILLA(focusw
))
2513 sci_send_command(SCINTILLA(focusw
), SCI_WORDPARTRIGHTEXTEND
);
2520 static gboolean
cb_func_document_action(guint key_id
)
2522 GeanyDocument
*doc
= document_get_current();
2529 case GEANY_KEYS_DOCUMENT_REPLACETABS
:
2530 on_replace_tabs_activate(NULL
, NULL
);
2532 case GEANY_KEYS_DOCUMENT_REPLACESPACES
:
2533 on_replace_spaces_activate(NULL
, NULL
);
2535 case GEANY_KEYS_DOCUMENT_LINEBREAK
:
2536 on_line_breaking1_activate(NULL
, NULL
);
2537 ui_document_show_hide(doc
);
2539 case GEANY_KEYS_DOCUMENT_LINEWRAP
:
2540 on_line_wrapping1_toggled(NULL
, NULL
);
2541 ui_document_show_hide(doc
);
2543 case GEANY_KEYS_DOCUMENT_CLONE
:
2544 document_clone(doc
);
2546 case GEANY_KEYS_DOCUMENT_RELOADTAGLIST
:
2547 document_update_tags(doc
);
2549 case GEANY_KEYS_DOCUMENT_FOLDALL
:
2550 editor_fold_all(doc
->editor
);
2552 case GEANY_KEYS_DOCUMENT_UNFOLDALL
:
2553 editor_unfold_all(doc
->editor
);
2555 case GEANY_KEYS_DOCUMENT_TOGGLEFOLD
:
2556 if (editor_prefs
.folding
)
2558 gint line
= sci_get_current_line(doc
->editor
->sci
);
2559 editor_toggle_fold(doc
->editor
, line
, 0);
2562 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS
:
2563 on_remove_markers1_activate(NULL
, NULL
);
2565 case GEANY_KEYS_DOCUMENT_REMOVE_ERROR_INDICATORS
:
2566 on_menu_remove_indicators1_activate(NULL
, NULL
);
2568 case GEANY_KEYS_DOCUMENT_REMOVE_MARKERS_INDICATORS
:
2569 on_remove_markers1_activate(NULL
, NULL
);
2570 on_menu_remove_indicators1_activate(NULL
, NULL
);
2577 static void insert_line_after(GeanyEditor
*editor
)
2579 ScintillaObject
*sci
= editor
->sci
;
2581 sci_send_command(sci
, SCI_LINEEND
);
2582 sci_send_command(sci
, SCI_NEWLINE
);
2586 static void insert_line_before(GeanyEditor
*editor
)
2588 ScintillaObject
*sci
= editor
->sci
;
2589 gint line
= sci_get_current_line(sci
);
2590 gint indentpos
= sci_get_line_indent_position(sci
, line
);
2592 sci_set_current_position(sci
, indentpos
, TRUE
);
2593 sci_send_command(sci
, SCI_NEWLINE
);
2594 sci_send_command(sci
, SCI_LINEUP
);
2598 /* common function for insert keybindings, only valid when scintilla has focus. */
2599 static gboolean
cb_func_insert_action(guint key_id
)
2601 GeanyDocument
*doc
= document_get_current();
2602 GtkWidget
*focusw
= gtk_window_get_focus(GTK_WINDOW(main_widgets
.window
));
2604 /* keybindings only valid when scintilla widget has focus */
2605 if (doc
== NULL
|| focusw
!= GTK_WIDGET(doc
->editor
->sci
))
2610 case GEANY_KEYS_INSERT_ALTWHITESPACE
:
2611 editor_insert_alternative_whitespace(doc
->editor
);
2613 case GEANY_KEYS_INSERT_DATE
:
2614 gtk_menu_item_activate(GTK_MENU_ITEM(
2615 ui_lookup_widget(main_widgets
.window
, "insert_date_custom1")));
2617 case GEANY_KEYS_INSERT_LINEAFTER
:
2618 insert_line_after(doc
->editor
);
2620 case GEANY_KEYS_INSERT_LINEBEFORE
:
2621 insert_line_before(doc
->editor
);
2628 /* update key combination */
2629 void keybindings_update_combo(GeanyKeyBinding
*kb
, guint key
, GdkModifierType mods
)
2631 GtkWidget
*widget
= kb
->menu_item
;
2633 if (widget
&& kb
->key
)
2634 gtk_widget_remove_accelerator(widget
, kb_accel_group
, kb
->key
, kb
->mods
);
2639 if (widget
&& kb
->key
)
2640 gtk_widget_add_accelerator(widget
, "activate", kb_accel_group
,
2641 kb
->key
, kb
->mods
, GTK_ACCEL_VISIBLE
);
2645 /* used for plugins, can be called repeatedly. */
2646 GeanyKeyGroup
*keybindings_set_group(GeanyKeyGroup
*group
, const gchar
*section_name
,
2647 const gchar
*label
, gsize count
, GeanyKeyGroupCallback callback
)
2649 g_return_val_if_fail(section_name
, NULL
);
2650 g_return_val_if_fail(count
, NULL
);
2652 /* prevent conflict with core bindings */
2653 g_return_val_if_fail(!g_str_equal(section_name
, keybindings_keyfile_group_name
), NULL
);
2657 group
= g_new0(GeanyKeyGroup
, 1);
2658 add_kb_group(group
, section_name
, label
, callback
, TRUE
);
2660 g_free(group
->plugin_keys
);
2661 group
->plugin_keys
= g_new0(GeanyKeyBinding
, count
);
2662 group
->plugin_key_count
= count
;
2663 g_ptr_array_set_size(group
->key_items
, 0);
2668 void keybindings_free_group(GeanyKeyGroup
*group
)
2670 g_ptr_array_remove_fast(keybinding_groups
, group
);