mc_config_get_string_raw(): if default parameter is NULL, then no any config entities...
[midnight-commander.git] / lib / keybind.c
blobeedea6baeded6c2ebcdd471a0f4df4e08e310de7
1 /*
2 Definitions of key bindings.
4 Copyright (C) 2005, 2009, 2010, 2011, 2011, 2012
5 The 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 {"Link", CK_Link},
180 {"PanelListingChange", CK_PanelListingChange},
181 {"PanelListing", CK_PanelListing},
182 #ifdef LISTMODE_EDITOR
183 {"ListMode", CK_ListMode}.
184 #endif
185 {"OptionsPanel", CK_OptionsPanel},
186 {"CdQuick", CK_CdQuick},
187 {"PanelQuickView", CK_PanelQuickView},
188 {"LinkSymbolicRelative", CK_LinkSymbolicRelative},
189 {"VfsList", CK_VfsList},
190 {"SaveSetup", CK_SaveSetup},
191 {"LinkSymbolic", CK_LinkSymbolic},
192 {"PanelTree", CK_PanelTree},
193 {"Tree", CK_Tree},
194 #ifdef ENABLE_VFS_UNDELFS
195 {"Undelete", CK_Undelete},
196 #endif
197 {"PutCurrentLink", CK_PutCurrentLink},
198 {"PutOtherLink", CK_PutOtherLink},
199 {"HotListAdd", CK_HotListAdd},
200 {"ShowHidden", CK_ShowHidden},
201 {"SplitVertHoriz", CK_SplitVertHoriz},
202 {"SplitEqual", CK_SplitEqual},
203 {"SplitMore", CK_SplitMore},
204 {"SplitLess", CK_SplitLess},
205 {"PutCurrentPath", CK_PutCurrentPath},
206 {"PutOtherPath", CK_PutOtherPath},
207 {"PutCurrentTagged", CK_PutCurrentTagged},
208 {"PutOtherTagged", CK_PutOtherTagged},
209 {"Select", CK_Select},
210 {"Unselect", CK_Unselect},
212 /* panel */
213 {"ScrollLeft", CK_ScrollLeft},
214 {"ScrollRight", CK_ScrollRight},
215 {"PanelOtherCd", CK_PanelOtherCd},
216 {"PanelOtherCdLink", CK_PanelOtherCdLink},
217 {"CopySingle", CK_CopySingle},
218 {"MoveSingle", CK_MoveSingle},
219 {"DeleteSingle", CK_DeleteSingle},
220 {"CdParent", CK_CdParent},
221 {"CdChild", CK_CdChild},
222 {"Panelize", CK_Panelize},
223 {"PanelOtherSync", CK_PanelOtherSync},
224 {"SortNext", CK_SortNext},
225 {"SortPrev", CK_SortPrev},
226 {"SortReverse", CK_SortReverse},
227 {"SortByName", CK_SortByName},
228 {"SortByExt", CK_SortByExt},
229 {"SortBySize", CK_SortBySize},
230 {"SortByMTime", CK_SortByMTime},
231 {"CdParentSmart", CK_CdParentSmart},
233 /* dialog */
234 {"Ok", CK_Ok},
235 {"Cancel", CK_Cancel},
237 /* input line */
238 {"Yank", CK_Yank},
240 /* help */
241 {"Index", CK_Index},
242 {"Back", CK_Back},
243 {"LinkNext", CK_LinkNext},
244 {"LinkPrev", CK_LinkPrev},
245 {"NodeNext", CK_NodeNext},
246 {"NodePrev", CK_NodePrev},
248 /* tree */
249 {"Forget", CK_Forget},
251 #if defined (USE_INTERNAL_EDIT) || defined (USE_DIFF_VIEW)
252 {"ShowNumbers", CK_ShowNumbers},
253 #endif
255 #ifdef USE_INTERNAL_EDIT
256 {"Close", CK_Close},
257 {"Tab", CK_Tab},
258 {"Undo", CK_Undo},
259 {"ScrollUp", CK_ScrollUp},
260 {"ScrollDown", CK_ScrollDown},
261 {"Return", CK_Return},
262 {"ParagraphUp", CK_ParagraphUp},
263 {"ParagraphDown", CK_ParagraphDown},
264 {"EditFile", CK_EditFile},
265 {"MarkWord", CK_MarkWord},
266 {"MarkLine", CK_MarkLine},
267 {"MarkAll", CK_MarkAll},
268 {"Unmark", CK_Unmark},
269 {"MarkColumn", CK_MarkColumn},
270 {"BlockSave", CK_BlockSave},
271 {"InsertFile", CK_InsertFile},
272 {"InsertOverwrite", CK_InsertOverwrite},
273 {"Date", CK_Date},
274 {"DeleteLine", CK_DeleteLine},
275 {"EditMail", CK_Mail},
276 {"ParagraphFormat", CK_ParagraphFormat},
277 {"MatchBracket", CK_MatchBracket},
278 {"ExternalCommand", CK_ExternalCommand},
279 {"MacroStartRecord", CK_MacroStartRecord},
280 {"MacroStopRecord", CK_MacroStopRecord},
281 {"MacroStartStopRecord", CK_MacroStartStopRecord},
282 {"MacroDelete", CK_MacroDelete},
283 {"RepeatStartStopRecord", CK_RepeatStartStopRecord},
284 #ifdef HAVE_ASPELL
285 {"SpellCheck", CK_SpellCheck},
286 {"SpellCheckCurrentWord", CK_SpellCheckCurrentWord},
287 {"SpellCheckSelectLang", CK_SpellCheckSelectLang},
288 #endif /* HAVE_ASPELL */
289 {"BookmarkFlush", CK_BookmarkFlush},
290 {"BookmarkNext", CK_BookmarkNext},
291 {"BookmarkPrev", CK_BookmarkPrev},
292 {"MarkPageUp", CK_MarkPageUp},
293 {"MarkPageDown", CK_MarkPageDown},
294 {"MarkToFileBegin", CK_MarkToFileBegin},
295 {"MarkToFileEnd", CK_MarkToFileEnd},
296 {"MarkToPageBegin", CK_MarkToPageBegin},
297 {"MarkToPageEnd", CK_MarkToPageEnd},
298 {"MarkScrollUp", CK_MarkScrollUp},
299 {"MarkScrollDown", CK_MarkScrollDown},
300 {"MarkParagraphUp", CK_MarkParagraphUp},
301 {"MarkParagraphDown", CK_MarkParagraphDown},
302 {"MarkColumnPageUp", CK_MarkColumnPageUp},
303 {"MarkColumnPageDown", CK_MarkColumnPageDown},
304 {"MarkColumnLeft", CK_MarkColumnLeft},
305 {"MarkColumnRight", CK_MarkColumnRight},
306 {"MarkColumnUp", CK_MarkColumnUp},
307 {"MarkColumnDown", CK_MarkColumnDown},
308 {"MarkColumnScrollUp", CK_MarkColumnScrollUp},
309 {"MarkColumnScrollDown", CK_MarkColumnScrollDown},
310 {"MarkColumnParagraphUp", CK_MarkColumnParagraphUp},
311 {"MarkColumnParagraphDown", CK_MarkColumnParagraphDown},
312 {"BlockShiftLeft", CK_BlockShiftLeft},
313 {"BlockShiftRight", CK_BlockShiftRight},
314 {"InsertLiteral", CK_InsertLiteral},
315 {"ShowTabTws", CK_ShowTabTws},
316 {"SyntaxOnOff", CK_SyntaxOnOff},
317 {"SyntaxChoose", CK_SyntaxChoose},
318 {"ShowMargin", CK_ShowMargin},
319 {"OptionsSaveMode", CK_OptionsSaveMode},
320 {"About", CK_About},
321 /* An action to run external script from macro */
322 {"ExecuteScript", CK_PipeBlock (0)},
323 {"WindowMove", CK_WindowMove},
324 {"WindowResize", CK_WindowResize},
325 {"WindowFullscreen", CK_WindowFullscreen},
326 {"WindowList", CK_WindowList},
327 {"WindowNext", CK_WindowNext},
328 {"WindowPrev", CK_WindowPrev},
329 #endif /* USE_INTERNAL_EDIT */
331 /* viewer */
332 {"WrapMode", CK_WrapMode},
333 {"HexEditMode", CK_HexEditMode},
334 {"HexMode", CK_HexMode},
335 {"MagicMode", CK_MagicMode},
336 {"NroffMode", CK_NroffMode},
337 {"BookmarkGoto", CK_BookmarkGoto},
338 {"Ruler", CK_Ruler},
339 {"SearchForward", CK_SearchForward},
340 {"SearchBackward", CK_SearchBackward},
341 {"SearchForwardContinue", CK_SearchForwardContinue},
342 {"SearchBackwardContinue", CK_SearchBackwardContinue},
344 #ifdef USE_DIFF_VIEW
345 /* diff viewer */
346 {"ShowSymbols", CK_ShowSymbols},
347 {"SplitFull", CK_SplitFull},
348 {"Tab2", CK_Tab2},
349 {"Tab3", CK_Tab3},
350 {"Tab4", CK_Tab4},
351 {"Tab8", CK_Tab8},
352 {"HunkNext", CK_HunkNext},
353 {"HunkPrev", CK_HunkPrev},
354 {"EditOther", CK_EditOther},
355 {"Merge", CK_Merge},
356 {"MergeOther", CK_MergeOther},
357 #endif /* USE_DIFF_VIEW */
359 {NULL, CK_IgnoreKey}
362 /* *INDENT-OFF* */
363 static const size_t num_command_names = G_N_ELEMENTS (command_names) - 1;
364 /* *INDENT-ON* */
366 /*** file scope functions ************************************************************************/
367 /* --------------------------------------------------------------------------------------------- */
369 static int
370 name_keymap_comparator (const void *p1, const void *p2)
372 const name_keymap_t *m1 = (const name_keymap_t *) p1;
373 const name_keymap_t *m2 = (const name_keymap_t *) p2;
375 return g_ascii_strcasecmp (m1->name, m2->name);
378 /* --------------------------------------------------------------------------------------------- */
380 static inline void
381 sort_command_names (void)
383 static gboolean has_been_sorted = FALSE;
385 if (!has_been_sorted)
387 qsort (command_names, num_command_names,
388 sizeof (command_names[0]), &name_keymap_comparator);
389 has_been_sorted = TRUE;
393 /* --------------------------------------------------------------------------------------------- */
395 static void
396 keymap_add (GArray * keymap, long key, unsigned long cmd, const char *caption)
398 if (key != 0 && cmd != CK_IgnoreKey)
400 global_keymap_t new_bind;
402 new_bind.key = key;
403 new_bind.command = cmd;
404 g_snprintf (new_bind.caption, sizeof (new_bind.caption), "%s", caption);
405 g_array_append_val (keymap, new_bind);
409 /* --------------------------------------------------------------------------------------------- */
410 /*** public functions ****************************************************************************/
411 /* --------------------------------------------------------------------------------------------- */
413 void
414 keybind_cmd_bind (GArray * keymap, const char *keybind, unsigned long action)
416 char *caption = NULL;
417 long key;
419 key = lookup_key (keybind, &caption);
420 keymap_add (keymap, key, action, caption);
421 g_free (caption);
424 /* --------------------------------------------------------------------------------------------- */
426 unsigned long
427 keybind_lookup_action (const char *name)
429 const name_keymap_t key = { name, 0 };
430 name_keymap_t *res;
432 sort_command_names ();
434 res = bsearch (&key, command_names, num_command_names,
435 sizeof (command_names[0]), name_keymap_comparator);
437 return (res != NULL) ? res->val : CK_IgnoreKey;
440 /* --------------------------------------------------------------------------------------------- */
442 const char *
443 keybind_lookup_actionname (unsigned long action)
445 size_t i;
447 for (i = 0; command_names[i].name != NULL; i++)
448 if (command_names[i].val == action)
449 return command_names[i].name;
451 return NULL;
454 /* --------------------------------------------------------------------------------------------- */
456 const char *
457 keybind_lookup_keymap_shortcut (const global_keymap_t * keymap, unsigned long action)
459 size_t i;
461 if (keymap != NULL)
462 for (i = 0; keymap[i].key != 0; i++)
463 if (keymap[i].command == action)
464 return (keymap[i].caption[0] != '\0') ? keymap[i].caption : NULL;
466 return NULL;
469 /* --------------------------------------------------------------------------------------------- */
471 unsigned long
472 keybind_lookup_keymap_command (const global_keymap_t * keymap, long key)
474 size_t i;
476 if (keymap != NULL)
477 for (i = 0; keymap[i].key != 0; i++)
478 if (keymap[i].key == key)
479 return keymap[i].command;
481 return CK_IgnoreKey;
484 /* --------------------------------------------------------------------------------------------- */