Update ebuild.syntax to include new EAPI 6 keywords
[midnight-commander.git] / lib / keybind.c
blob89fa9a02dcdc659c27dacb2dfbfe4b94c4a8b5fd
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 {"PutCurrentTagged", CK_PutCurrentTagged},
209 {"PutOtherTagged", CK_PutOtherTagged},
210 {"Select", CK_Select},
211 {"Unselect", CK_Unselect},
213 /* panel */
214 {"SelectExt", CK_SelectExt},
215 {"ScrollLeft", CK_ScrollLeft},
216 {"ScrollRight", CK_ScrollRight},
217 {"PanelOtherCd", CK_PanelOtherCd},
218 {"PanelOtherCdLink", CK_PanelOtherCdLink},
219 {"CopySingle", CK_CopySingle},
220 {"MoveSingle", CK_MoveSingle},
221 {"DeleteSingle", CK_DeleteSingle},
222 {"CdParent", CK_CdParent},
223 {"CdChild", CK_CdChild},
224 {"Panelize", CK_Panelize},
225 {"PanelOtherSync", CK_PanelOtherSync},
226 {"SortNext", CK_SortNext},
227 {"SortPrev", CK_SortPrev},
228 {"SortReverse", CK_SortReverse},
229 {"SortByName", CK_SortByName},
230 {"SortByExt", CK_SortByExt},
231 {"SortBySize", CK_SortBySize},
232 {"SortByMTime", CK_SortByMTime},
233 {"CdParentSmart", CK_CdParentSmart},
235 /* dialog */
236 {"Ok", CK_Ok},
237 {"Cancel", CK_Cancel},
239 /* input line */
240 {"Yank", CK_Yank},
242 /* help */
243 {"Index", CK_Index},
244 {"Back", CK_Back},
245 {"LinkNext", CK_LinkNext},
246 {"LinkPrev", CK_LinkPrev},
247 {"NodeNext", CK_NodeNext},
248 {"NodePrev", CK_NodePrev},
250 /* tree */
251 {"Forget", CK_Forget},
253 #if defined (USE_INTERNAL_EDIT) || defined (USE_DIFF_VIEW)
254 {"ShowNumbers", CK_ShowNumbers},
255 #endif
257 #ifdef USE_INTERNAL_EDIT
258 {"Close", CK_Close},
259 {"Tab", CK_Tab},
260 {"Undo", CK_Undo},
261 {"ScrollUp", CK_ScrollUp},
262 {"ScrollDown", CK_ScrollDown},
263 {"Return", CK_Return},
264 {"ParagraphUp", CK_ParagraphUp},
265 {"ParagraphDown", CK_ParagraphDown},
266 {"EditFile", CK_EditFile},
267 {"MarkWord", CK_MarkWord},
268 {"MarkLine", CK_MarkLine},
269 {"MarkAll", CK_MarkAll},
270 {"Unmark", CK_Unmark},
271 {"MarkColumn", CK_MarkColumn},
272 {"BlockSave", CK_BlockSave},
273 {"InsertFile", CK_InsertFile},
274 {"InsertOverwrite", CK_InsertOverwrite},
275 {"Date", CK_Date},
276 {"DeleteLine", CK_DeleteLine},
277 {"EditMail", CK_Mail},
278 {"ParagraphFormat", CK_ParagraphFormat},
279 {"MatchBracket", CK_MatchBracket},
280 {"ExternalCommand", CK_ExternalCommand},
281 {"MacroStartRecord", CK_MacroStartRecord},
282 {"MacroStopRecord", CK_MacroStopRecord},
283 {"MacroStartStopRecord", CK_MacroStartStopRecord},
284 {"MacroDelete", CK_MacroDelete},
285 {"RepeatStartStopRecord", CK_RepeatStartStopRecord},
286 #ifdef HAVE_ASPELL
287 {"SpellCheck", CK_SpellCheck},
288 {"SpellCheckCurrentWord", CK_SpellCheckCurrentWord},
289 {"SpellCheckSelectLang", CK_SpellCheckSelectLang},
290 #endif /* HAVE_ASPELL */
291 {"BookmarkFlush", CK_BookmarkFlush},
292 {"BookmarkNext", CK_BookmarkNext},
293 {"BookmarkPrev", CK_BookmarkPrev},
294 {"MarkPageUp", CK_MarkPageUp},
295 {"MarkPageDown", CK_MarkPageDown},
296 {"MarkToFileBegin", CK_MarkToFileBegin},
297 {"MarkToFileEnd", CK_MarkToFileEnd},
298 {"MarkToPageBegin", CK_MarkToPageBegin},
299 {"MarkToPageEnd", CK_MarkToPageEnd},
300 {"MarkScrollUp", CK_MarkScrollUp},
301 {"MarkScrollDown", CK_MarkScrollDown},
302 {"MarkParagraphUp", CK_MarkParagraphUp},
303 {"MarkParagraphDown", CK_MarkParagraphDown},
304 {"MarkColumnPageUp", CK_MarkColumnPageUp},
305 {"MarkColumnPageDown", CK_MarkColumnPageDown},
306 {"MarkColumnLeft", CK_MarkColumnLeft},
307 {"MarkColumnRight", CK_MarkColumnRight},
308 {"MarkColumnUp", CK_MarkColumnUp},
309 {"MarkColumnDown", CK_MarkColumnDown},
310 {"MarkColumnScrollUp", CK_MarkColumnScrollUp},
311 {"MarkColumnScrollDown", CK_MarkColumnScrollDown},
312 {"MarkColumnParagraphUp", CK_MarkColumnParagraphUp},
313 {"MarkColumnParagraphDown", CK_MarkColumnParagraphDown},
314 {"BlockShiftLeft", CK_BlockShiftLeft},
315 {"BlockShiftRight", CK_BlockShiftRight},
316 {"InsertLiteral", CK_InsertLiteral},
317 {"ShowTabTws", CK_ShowTabTws},
318 {"SyntaxOnOff", CK_SyntaxOnOff},
319 {"SyntaxChoose", CK_SyntaxChoose},
320 {"ShowMargin", CK_ShowMargin},
321 {"OptionsSaveMode", CK_OptionsSaveMode},
322 {"About", CK_About},
323 /* An action to run external script from macro */
324 {"ExecuteScript", CK_PipeBlock (0)},
325 {"WindowMove", CK_WindowMove},
326 {"WindowResize", CK_WindowResize},
327 {"WindowFullscreen", CK_WindowFullscreen},
328 {"WindowList", CK_WindowList},
329 {"WindowNext", CK_WindowNext},
330 {"WindowPrev", CK_WindowPrev},
331 #endif /* USE_INTERNAL_EDIT */
333 /* viewer */
334 {"WrapMode", CK_WrapMode},
335 {"HexEditMode", CK_HexEditMode},
336 {"HexMode", CK_HexMode},
337 {"MagicMode", CK_MagicMode},
338 {"NroffMode", CK_NroffMode},
339 {"BookmarkGoto", CK_BookmarkGoto},
340 {"Ruler", CK_Ruler},
341 {"SearchForward", CK_SearchForward},
342 {"SearchBackward", CK_SearchBackward},
343 {"SearchForwardContinue", CK_SearchForwardContinue},
344 {"SearchBackwardContinue", CK_SearchBackwardContinue},
346 #ifdef USE_DIFF_VIEW
347 /* diff viewer */
348 {"ShowSymbols", CK_ShowSymbols},
349 {"SplitFull", CK_SplitFull},
350 {"Tab2", CK_Tab2},
351 {"Tab3", CK_Tab3},
352 {"Tab4", CK_Tab4},
353 {"Tab8", CK_Tab8},
354 {"HunkNext", CK_HunkNext},
355 {"HunkPrev", CK_HunkPrev},
356 {"EditOther", CK_EditOther},
357 {"Merge", CK_Merge},
358 {"MergeOther", CK_MergeOther},
359 #endif /* USE_DIFF_VIEW */
361 {NULL, CK_IgnoreKey}
364 /* *INDENT-OFF* */
365 static const size_t num_command_names = G_N_ELEMENTS (command_names) - 1;
366 /* *INDENT-ON* */
368 /*** file scope functions ************************************************************************/
369 /* --------------------------------------------------------------------------------------------- */
371 static int
372 name_keymap_comparator (const void *p1, const void *p2)
374 const name_keymap_t *m1 = (const name_keymap_t *) p1;
375 const name_keymap_t *m2 = (const name_keymap_t *) p2;
377 return g_ascii_strcasecmp (m1->name, m2->name);
380 /* --------------------------------------------------------------------------------------------- */
382 static inline void
383 sort_command_names (void)
385 static gboolean has_been_sorted = FALSE;
387 if (!has_been_sorted)
389 qsort (command_names, num_command_names,
390 sizeof (command_names[0]), &name_keymap_comparator);
391 has_been_sorted = TRUE;
395 /* --------------------------------------------------------------------------------------------- */
397 static void
398 keymap_add (GArray * keymap, long key, long cmd, const char *caption)
400 if (key != 0 && cmd != CK_IgnoreKey)
402 global_keymap_t new_bind;
404 new_bind.key = key;
405 new_bind.command = cmd;
406 g_snprintf (new_bind.caption, sizeof (new_bind.caption), "%s", caption);
407 g_array_append_val (keymap, new_bind);
411 /* --------------------------------------------------------------------------------------------- */
412 /*** public functions ****************************************************************************/
413 /* --------------------------------------------------------------------------------------------- */
415 void
416 keybind_cmd_bind (GArray * keymap, const char *keybind, long action)
418 char *caption = NULL;
419 long key;
421 key = lookup_key (keybind, &caption);
422 keymap_add (keymap, key, action, caption);
423 g_free (caption);
426 /* --------------------------------------------------------------------------------------------- */
428 long
429 keybind_lookup_action (const char *name)
431 const name_keymap_t key = { name, 0 };
432 name_keymap_t *res;
434 sort_command_names ();
436 res = bsearch (&key, command_names, num_command_names,
437 sizeof (command_names[0]), name_keymap_comparator);
439 return (res != NULL) ? res->val : CK_IgnoreKey;
442 /* --------------------------------------------------------------------------------------------- */
444 const char *
445 keybind_lookup_actionname (long action)
447 size_t i;
449 for (i = 0; command_names[i].name != NULL; i++)
450 if (command_names[i].val == action)
451 return command_names[i].name;
453 return NULL;
456 /* --------------------------------------------------------------------------------------------- */
458 const char *
459 keybind_lookup_keymap_shortcut (const global_keymap_t * keymap, long action)
461 if (keymap != NULL)
463 size_t i;
465 for (i = 0; keymap[i].key != 0; i++)
466 if (keymap[i].command == action)
467 return (keymap[i].caption[0] != '\0') ? keymap[i].caption : NULL;
469 return NULL;
472 /* --------------------------------------------------------------------------------------------- */
474 long
475 keybind_lookup_keymap_command (const global_keymap_t * keymap, long key)
477 if (keymap != NULL)
479 size_t i;
481 for (i = 0; keymap[i].key != 0; i++)
482 if (keymap[i].key == key)
483 return keymap[i].command;
486 return CK_IgnoreKey;
489 /* --------------------------------------------------------------------------------------------- */