(widget_set_size): send MSG_DRAW after MSG_RESIZE.
[midnight-commander.git] / lib / keybind.c
blob3db3a7992ecc4baa3d3322a8a65ca57813a5048f
1 /*
2 Definitions of key bindings.
4 Copyright (C) 2005-2016
5 Free Software Foundation, Inc.
7 Written by:
8 Vitja Makarov, 2005
9 Ilia Maslakov <il.smind@gmail.com>, 2009, 2012
10 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2011, 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 #include <config.h>
30 #include <ctype.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <sys/types.h>
35 #include "lib/global.h"
36 #include "lib/tty/key.h" /* KEY_M_ */
37 #include "lib/keybind.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 static name_keymap_t command_names[] = {
48 /* common */
49 {"InsertChar", CK_InsertChar},
50 {"Enter", CK_Enter},
51 {"Up", CK_Up},
52 {"Down", CK_Down},
53 {"Left", CK_Left},
54 {"Right", CK_Right},
55 {"LeftQuick", CK_LeftQuick},
56 {"RightQuick", CK_RightQuick},
57 {"Home", CK_Home},
58 {"End", CK_End},
59 {"PageUp", CK_PageUp},
60 {"PageDown", CK_PageDown},
61 {"HalfPageUp", CK_HalfPageUp},
62 {"HalfPageDown", CK_HalfPageDown},
63 {"Top", CK_Top},
64 {"Bottom", CK_Bottom},
65 {"TopOnScreen", CK_TopOnScreen},
66 {"MiddleOnScreen", CK_MiddleOnScreen},
67 {"BottomOnScreen", CK_BottomOnScreen},
68 {"WordLeft", CK_WordLeft},
69 {"WordRight", CK_WordRight},
70 {"Copy", CK_Copy},
71 {"Move", CK_Move},
72 {"Delete", CK_Delete},
73 {"MakeDir", CK_MakeDir},
74 {"ChangeMode", CK_ChangeMode},
75 {"ChangeOwn", CK_ChangeOwn},
76 {"ChangeOwnAdvanced", CK_ChangeOwnAdvanced},
77 {"Remove", CK_Remove},
78 {"BackSpace", CK_BackSpace},
79 {"Redo", CK_Redo},
80 {"Clear", CK_Clear},
81 {"Menu", CK_Menu},
82 {"MenuLastSelected", CK_MenuLastSelected},
83 {"UserMenu", CK_UserMenu},
84 {"EditUserMenu", CK_EditUserMenu},
85 {"Search", CK_Search},
86 {"SearchContinue", CK_SearchContinue},
87 {"Replace", CK_Replace},
88 {"ReplaceContinue", CK_ReplaceContinue},
89 {"Help", CK_Help},
90 {"Shell", CK_Shell},
91 {"Edit", CK_Edit},
92 {"EditNew", CK_EditNew},
93 #ifdef HAVE_CHARSET
94 {"SelectCodepage", CK_SelectCodepage},
95 #endif
96 {"History", CK_History},
97 {"HistoryNext", CK_HistoryNext},
98 {"HistoryPrev", CK_HistoryPrev},
99 {"Complete", CK_Complete},
100 {"Save", CK_Save},
101 {"SaveAs", CK_SaveAs},
102 {"Goto", CK_Goto},
103 {"Reread", CK_Reread},
104 {"Refresh", CK_Refresh},
105 {"Suspend", CK_Suspend},
106 {"Swap", CK_Swap},
107 {"HotList", CK_HotList},
108 {"SelectInvert", CK_SelectInvert},
109 {"ScreenList", CK_ScreenList},
110 {"ScreenNext", CK_ScreenNext},
111 {"ScreenPrev", CK_ScreenPrev},
112 {"FileNext", CK_FileNext},
113 {"FilePrev", CK_FilePrev},
114 {"DeleteToHome", CK_DeleteToHome},
115 {"DeleteToEnd", CK_DeleteToEnd},
116 {"DeleteToWordBegin", CK_DeleteToWordBegin},
117 {"DeleteToWordEnd", CK_DeleteToWordEnd},
118 {"Cut", CK_Cut},
119 {"Store", CK_Store},
120 {"Paste", CK_Paste},
121 {"Mark", CK_Mark},
122 {"MarkLeft", CK_MarkLeft},
123 {"MarkRight", CK_MarkRight},
124 {"MarkUp", CK_MarkUp},
125 {"MarkDown", CK_MarkDown},
126 {"MarkToWordBegin", CK_MarkToWordBegin},
127 {"MarkToWordEnd", CK_MarkToWordEnd},
128 {"MarkToHome", CK_MarkToHome},
129 {"MarkToEnd", CK_MarkToEnd},
130 {"ToggleNavigation", CK_ToggleNavigation},
131 {"Sort", CK_Sort},
132 {"Options", CK_Options},
133 {"LearnKeys", CK_LearnKeys},
134 {"Bookmark", CK_Bookmark},
135 {"Quit", CK_Quit},
136 {"QuitQuiet", CK_QuitQuiet},
137 {"ExtendedKeyMap", CK_ExtendedKeyMap},
139 /* main commands */
140 #ifdef USE_INTERNAL_EDIT
141 {"EditForceInternal", CK_EditForceInternal},
142 #endif
143 {"View", CK_View},
144 {"ViewRaw", CK_ViewRaw},
145 {"ViewFile", CK_ViewFile},
146 {"ViewFiltered", CK_ViewFiltered},
147 {"Find", CK_Find},
148 {"DirSize", CK_DirSize},
149 {"PanelListingSwitch", CK_PanelListingSwitch},
150 {"CompareDirs", CK_CompareDirs},
151 #ifdef USE_DIFF_VIEW
152 {"CompareFiles", CK_CompareFiles},
153 #endif
154 {"OptionsVfs", CK_OptionsVfs},
155 {"OptionsConfirm", CK_OptionsConfirm},
156 {"OptionsDisplayBits", CK_OptionsDisplayBits},
157 {"EditExtensionsFile", CK_EditExtensionsFile},
158 {"EditFileHighlightFile", CK_EditFileHighlightFile},
159 {"LinkSymbolicEdit", CK_LinkSymbolicEdit},
160 {"ExternalPanelize", CK_ExternalPanelize},
161 {"Filter", CK_Filter},
162 #ifdef ENABLE_VFS_FISH
163 {"ConnectFish", CK_ConnectFish},
164 #endif
165 #ifdef ENABLE_VFS_FTP
166 {"ConnectFtp", CK_ConnectFtp},
167 #endif
168 #ifdef ENABLE_VFS_SFTP
169 {"ConnectSftp", CK_ConnectSftp},
170 #endif
171 #ifdef ENABLE_VFS_SMB
172 {"ConnectSmb", CK_ConnectSmb},
173 #endif
174 {"PanelInfo", CK_PanelInfo},
175 #ifdef ENABLE_BACKGROUND
176 {"Jobs", CK_Jobs},
177 #endif
178 {"OptionsLayout", CK_OptionsLayout},
179 {"OptionsAppearance", CK_OptionsAppearance},
180 {"Link", CK_Link},
181 {"PanelListingChange", CK_PanelListingChange},
182 {"PanelListing", CK_PanelListing},
183 #ifdef LISTMODE_EDITOR
184 {"ListMode", CK_ListMode}.
185 #endif
186 {"OptionsPanel", CK_OptionsPanel},
187 {"CdQuick", CK_CdQuick},
188 {"PanelQuickView", CK_PanelQuickView},
189 {"LinkSymbolicRelative", CK_LinkSymbolicRelative},
190 {"VfsList", CK_VfsList},
191 {"SaveSetup", CK_SaveSetup},
192 {"LinkSymbolic", CK_LinkSymbolic},
193 {"PanelTree", CK_PanelTree},
194 {"Tree", CK_Tree},
195 #ifdef ENABLE_VFS_UNDELFS
196 {"Undelete", CK_Undelete},
197 #endif
198 {"PutCurrentLink", CK_PutCurrentLink},
199 {"PutOtherLink", CK_PutOtherLink},
200 {"HotListAdd", CK_HotListAdd},
201 {"ShowHidden", CK_ShowHidden},
202 {"SplitVertHoriz", CK_SplitVertHoriz},
203 {"SplitEqual", CK_SplitEqual},
204 {"SplitMore", CK_SplitMore},
205 {"SplitLess", CK_SplitLess},
206 {"PutCurrentPath", CK_PutCurrentPath},
207 {"PutOtherPath", CK_PutOtherPath},
208 {"PutCurrentSelected", CK_PutCurrentSelected},
209 {"PutCurrentFullSelected", CK_PutCurrentFullSelected},
210 {"PutCurrentTagged", CK_PutCurrentTagged},
211 {"PutOtherTagged", CK_PutOtherTagged},
212 {"Select", CK_Select},
213 {"Unselect", CK_Unselect},
215 /* panel */
216 {"SelectExt", CK_SelectExt},
217 {"ScrollLeft", CK_ScrollLeft},
218 {"ScrollRight", CK_ScrollRight},
219 {"PanelOtherCd", CK_PanelOtherCd},
220 {"PanelOtherCdLink", CK_PanelOtherCdLink},
221 {"CopySingle", CK_CopySingle},
222 {"MoveSingle", CK_MoveSingle},
223 {"DeleteSingle", CK_DeleteSingle},
224 {"CdParent", CK_CdParent},
225 {"CdChild", CK_CdChild},
226 {"Panelize", CK_Panelize},
227 {"PanelOtherSync", CK_PanelOtherSync},
228 {"SortNext", CK_SortNext},
229 {"SortPrev", CK_SortPrev},
230 {"SortReverse", CK_SortReverse},
231 {"SortByName", CK_SortByName},
232 {"SortByExt", CK_SortByExt},
233 {"SortBySize", CK_SortBySize},
234 {"SortByMTime", CK_SortByMTime},
235 {"CdParentSmart", CK_CdParentSmart},
237 /* dialog */
238 {"Ok", CK_Ok},
239 {"Cancel", CK_Cancel},
241 /* input line */
242 {"Yank", CK_Yank},
244 /* help */
245 {"Index", CK_Index},
246 {"Back", CK_Back},
247 {"LinkNext", CK_LinkNext},
248 {"LinkPrev", CK_LinkPrev},
249 {"NodeNext", CK_NodeNext},
250 {"NodePrev", CK_NodePrev},
252 /* tree */
253 {"Forget", CK_Forget},
255 #if defined (USE_INTERNAL_EDIT) || defined (USE_DIFF_VIEW)
256 {"ShowNumbers", CK_ShowNumbers},
257 #endif
259 #ifdef USE_INTERNAL_EDIT
260 {"Close", CK_Close},
261 {"Tab", CK_Tab},
262 {"Undo", CK_Undo},
263 {"ScrollUp", CK_ScrollUp},
264 {"ScrollDown", CK_ScrollDown},
265 {"Return", CK_Return},
266 {"ParagraphUp", CK_ParagraphUp},
267 {"ParagraphDown", CK_ParagraphDown},
268 {"EditFile", CK_EditFile},
269 {"MarkWord", CK_MarkWord},
270 {"MarkLine", CK_MarkLine},
271 {"MarkAll", CK_MarkAll},
272 {"Unmark", CK_Unmark},
273 {"MarkColumn", CK_MarkColumn},
274 {"BlockSave", CK_BlockSave},
275 {"InsertFile", CK_InsertFile},
276 {"InsertOverwrite", CK_InsertOverwrite},
277 {"Date", CK_Date},
278 {"DeleteLine", CK_DeleteLine},
279 {"EditMail", CK_Mail},
280 {"ParagraphFormat", CK_ParagraphFormat},
281 {"MatchBracket", CK_MatchBracket},
282 {"ExternalCommand", CK_ExternalCommand},
283 {"MacroStartRecord", CK_MacroStartRecord},
284 {"MacroStopRecord", CK_MacroStopRecord},
285 {"MacroStartStopRecord", CK_MacroStartStopRecord},
286 {"MacroDelete", CK_MacroDelete},
287 {"RepeatStartStopRecord", CK_RepeatStartStopRecord},
288 #ifdef HAVE_ASPELL
289 {"SpellCheck", CK_SpellCheck},
290 {"SpellCheckCurrentWord", CK_SpellCheckCurrentWord},
291 {"SpellCheckSelectLang", CK_SpellCheckSelectLang},
292 #endif /* HAVE_ASPELL */
293 {"BookmarkFlush", CK_BookmarkFlush},
294 {"BookmarkNext", CK_BookmarkNext},
295 {"BookmarkPrev", CK_BookmarkPrev},
296 {"MarkPageUp", CK_MarkPageUp},
297 {"MarkPageDown", CK_MarkPageDown},
298 {"MarkToFileBegin", CK_MarkToFileBegin},
299 {"MarkToFileEnd", CK_MarkToFileEnd},
300 {"MarkToPageBegin", CK_MarkToPageBegin},
301 {"MarkToPageEnd", CK_MarkToPageEnd},
302 {"MarkScrollUp", CK_MarkScrollUp},
303 {"MarkScrollDown", CK_MarkScrollDown},
304 {"MarkParagraphUp", CK_MarkParagraphUp},
305 {"MarkParagraphDown", CK_MarkParagraphDown},
306 {"MarkColumnPageUp", CK_MarkColumnPageUp},
307 {"MarkColumnPageDown", CK_MarkColumnPageDown},
308 {"MarkColumnLeft", CK_MarkColumnLeft},
309 {"MarkColumnRight", CK_MarkColumnRight},
310 {"MarkColumnUp", CK_MarkColumnUp},
311 {"MarkColumnDown", CK_MarkColumnDown},
312 {"MarkColumnScrollUp", CK_MarkColumnScrollUp},
313 {"MarkColumnScrollDown", CK_MarkColumnScrollDown},
314 {"MarkColumnParagraphUp", CK_MarkColumnParagraphUp},
315 {"MarkColumnParagraphDown", CK_MarkColumnParagraphDown},
316 {"BlockShiftLeft", CK_BlockShiftLeft},
317 {"BlockShiftRight", CK_BlockShiftRight},
318 {"InsertLiteral", CK_InsertLiteral},
319 {"ShowTabTws", CK_ShowTabTws},
320 {"SyntaxOnOff", CK_SyntaxOnOff},
321 {"SyntaxChoose", CK_SyntaxChoose},
322 {"ShowMargin", CK_ShowMargin},
323 {"OptionsSaveMode", CK_OptionsSaveMode},
324 {"About", CK_About},
325 /* An action to run external script from macro */
326 {"ExecuteScript", CK_PipeBlock (0)},
327 {"WindowMove", CK_WindowMove},
328 {"WindowResize", CK_WindowResize},
329 {"WindowFullscreen", CK_WindowFullscreen},
330 {"WindowList", CK_WindowList},
331 {"WindowNext", CK_WindowNext},
332 {"WindowPrev", CK_WindowPrev},
333 #endif /* USE_INTERNAL_EDIT */
335 /* viewer */
336 {"WrapMode", CK_WrapMode},
337 {"HexEditMode", CK_HexEditMode},
338 {"HexMode", CK_HexMode},
339 {"MagicMode", CK_MagicMode},
340 {"NroffMode", CK_NroffMode},
341 {"BookmarkGoto", CK_BookmarkGoto},
342 {"Ruler", CK_Ruler},
343 {"SearchForward", CK_SearchForward},
344 {"SearchBackward", CK_SearchBackward},
345 {"SearchForwardContinue", CK_SearchForwardContinue},
346 {"SearchBackwardContinue", CK_SearchBackwardContinue},
348 #ifdef USE_DIFF_VIEW
349 /* diff viewer */
350 {"ShowSymbols", CK_ShowSymbols},
351 {"SplitFull", CK_SplitFull},
352 {"Tab2", CK_Tab2},
353 {"Tab3", CK_Tab3},
354 {"Tab4", CK_Tab4},
355 {"Tab8", CK_Tab8},
356 {"HunkNext", CK_HunkNext},
357 {"HunkPrev", CK_HunkPrev},
358 {"EditOther", CK_EditOther},
359 {"Merge", CK_Merge},
360 {"MergeOther", CK_MergeOther},
361 #endif /* USE_DIFF_VIEW */
363 {NULL, CK_IgnoreKey}
366 /* *INDENT-OFF* */
367 static const size_t num_command_names = G_N_ELEMENTS (command_names) - 1;
368 /* *INDENT-ON* */
370 /*** file scope functions ************************************************************************/
371 /* --------------------------------------------------------------------------------------------- */
373 static int
374 name_keymap_comparator (const void *p1, const void *p2)
376 const name_keymap_t *m1 = (const name_keymap_t *) p1;
377 const name_keymap_t *m2 = (const name_keymap_t *) p2;
379 return g_ascii_strcasecmp (m1->name, m2->name);
382 /* --------------------------------------------------------------------------------------------- */
384 static inline void
385 sort_command_names (void)
387 static gboolean has_been_sorted = FALSE;
389 if (!has_been_sorted)
391 qsort (command_names, num_command_names,
392 sizeof (command_names[0]), &name_keymap_comparator);
393 has_been_sorted = TRUE;
397 /* --------------------------------------------------------------------------------------------- */
399 static void
400 keymap_add (GArray * keymap, long key, long cmd, const char *caption)
402 if (key != 0 && cmd != CK_IgnoreKey)
404 global_keymap_t new_bind;
406 new_bind.key = key;
407 new_bind.command = cmd;
408 g_snprintf (new_bind.caption, sizeof (new_bind.caption), "%s", caption);
409 g_array_append_val (keymap, new_bind);
413 /* --------------------------------------------------------------------------------------------- */
414 /*** public functions ****************************************************************************/
415 /* --------------------------------------------------------------------------------------------- */
417 void
418 keybind_cmd_bind (GArray * keymap, const char *keybind, long action)
420 char *caption = NULL;
421 long key;
423 key = lookup_key (keybind, &caption);
424 keymap_add (keymap, key, action, caption);
425 g_free (caption);
428 /* --------------------------------------------------------------------------------------------- */
430 long
431 keybind_lookup_action (const char *name)
433 const name_keymap_t key = { name, 0 };
434 name_keymap_t *res;
436 sort_command_names ();
438 res = bsearch (&key, command_names, num_command_names,
439 sizeof (command_names[0]), name_keymap_comparator);
441 return (res != NULL) ? res->val : CK_IgnoreKey;
444 /* --------------------------------------------------------------------------------------------- */
446 const char *
447 keybind_lookup_actionname (long action)
449 size_t i;
451 for (i = 0; command_names[i].name != NULL; i++)
452 if (command_names[i].val == action)
453 return command_names[i].name;
455 return NULL;
458 /* --------------------------------------------------------------------------------------------- */
460 const char *
461 keybind_lookup_keymap_shortcut (const global_keymap_t * keymap, long action)
463 if (keymap != NULL)
465 size_t i;
467 for (i = 0; keymap[i].key != 0; i++)
468 if (keymap[i].command == action)
469 return (keymap[i].caption[0] != '\0') ? keymap[i].caption : NULL;
471 return NULL;
474 /* --------------------------------------------------------------------------------------------- */
476 long
477 keybind_lookup_keymap_command (const global_keymap_t * keymap, long key)
479 if (keymap != NULL)
481 size_t i;
483 for (i = 0; keymap[i].key != 0; i++)
484 if (keymap[i].key == key)
485 return keymap[i].command;
488 return CK_IgnoreKey;
491 /* --------------------------------------------------------------------------------------------- */