2 Copyright (C) 2009 The Free Software Foundation, Inc.
4 Written by: 2005 Vitja Makarov
6 2009, 2010 Andrew Borodin
8 This file is part of the Midnight Commander.
10 The Midnight Commander is free software; you can redistribute it
11 and/or modify it under the terms of the GNU General Public License as
12 published by the Free Software Foundation; either version 2 of the
13 License, or (at your option) any later version.
15 The Midnight Commander is distributed in the hope that it will be
16 useful, but WITHOUT ANY WARRANTY; without even the implied warranty
17 of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 General Public License for more details.
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
31 #include <sys/types.h>
33 #include "lib/global.h"
34 #include "lib/tty/key.h" /* KEY_M_ */
35 #include "lib/strutil.h" /* str_casecmp() */
36 #include "lib/keybind.h"
38 /*** global variables ****************************************************************************/
40 /*** file scope macro definitions ****************************************************************/
42 /*** file scope type declarations ****************************************************************/
44 /*** file scope variables ************************************************************************/
46 static name_keymap_t command_names
[] = {
53 {"LeftQuick", CK_LeftQuick
},
54 {"RightQuick", CK_RightQuick
},
57 {"PageUp", CK_PageUp
},
58 {"PageDown", CK_PageDown
},
59 {"HalfPageUp", CK_HalfPageUp
},
60 {"HalfPageDown", CK_HalfPageDown
},
62 {"Bottom", CK_Bottom
},
63 {"TopOnScreen", CK_TopOnScreen
},
64 {"MiddleOnScreen", CK_MiddleOnScreen
},
65 {"BottomOnScreen", CK_BottomOnScreen
},
66 {"WordLeft", CK_WordLeft
},
67 {"WordRight", CK_WordRight
},
70 {"Delete", CK_Delete
},
71 {"MakeDir", CK_MakeDir
},
72 {"ChangeMode", CK_ChangeMode
},
73 {"ChangeOwn", CK_ChangeOwn
},
74 {"ChangeOwnAdvanced", CK_ChangeOwnAdvanced
},
75 {"Remove", CK_Remove
},
76 {"BackSpace", CK_BackSpace
},
81 {"MenuLastSelected", CK_MenuLastSelected
},
82 {"UserMenu", CK_UserMenu
},
83 {"EditUserMenu", CK_EditUserMenu
},
84 {"Search", CK_Search
},
85 {"SearchContinue", CK_SearchContinue
},
86 {"Replace", CK_Replace
},
87 {"ReplaceContinue", CK_ReplaceContinue
},
91 {"EditNew", CK_EditNew
},
93 {"SelectCodepage", CK_SelectCodepage
},
95 {"History", CK_History
},
96 {"HistoryNext", CK_HistoryNext
},
97 {"HistoryPrev", CK_HistoryPrev
},
98 {"Complete", CK_Complete
},
100 {"SaveAs", CK_SaveAs
},
102 {"Reread", CK_Reread
},
103 {"Refresh", CK_Refresh
},
104 {"Suspend", CK_Suspend
},
106 {"HotList", CK_HotList
},
107 {"SelectInvert", CK_SelectInvert
},
108 {"ScreenList", CK_ScreenList
},
109 {"ScreenNext", CK_ScreenNext
},
110 {"ScreenPrev", CK_ScreenPrev
},
111 {"FileNext", CK_FileNext
},
112 {"FilePrev", CK_FilePrev
},
113 {"DeleteToWordBegin", CK_DeleteToWordBegin
},
114 {"DeleteToWordEnd", CK_DeleteToWordEnd
},
119 {"MarkLeft", CK_MarkLeft
},
120 {"MarkRight", CK_MarkRight
},
121 {"MarkUp", CK_MarkUp
},
122 {"MarkDown", CK_MarkDown
},
123 {"MarkToWordBegin", CK_MarkToWordBegin
},
124 {"MarkToWordEnd", CK_MarkToWordEnd
},
125 {"MarkToHome", CK_MarkToHome
},
126 {"MarkToEnd", CK_MarkToEnd
},
127 {"ToggleNavigation", CK_ToggleNavigation
},
129 {"Options", CK_Options
},
130 {"LearnKeys", CK_LearnKeys
},
131 {"Bookmark", CK_Bookmark
},
133 {"QuitQuiet", CK_QuitQuiet
},
134 {"ExtendedKeyMap", CK_ExtendedKeyMap
},
137 #ifdef USE_INTERNAL_EDIT
138 {"EditForceInternal", CK_EditForceInternal
},
141 {"ViewRaw", CK_ViewRaw
},
142 {"ViewFile", CK_ViewFile
},
143 {"ViewFiltered", CK_ViewFiltered
},
145 {"DirSize", CK_DirSize
},
146 {"PanelListingSwitch", CK_PanelListingSwitch
},
147 {"CompareDirs", CK_CompareDirs
},
149 {"CompareFiles", CK_CompareFiles
},
151 {"OptionsVfs", CK_OptionsVfs
},
152 {"OptionsConfirm", CK_OptionsConfirm
},
153 {"OptionsDisplayBits", CK_OptionsDisplayBits
},
154 {"EditExtensionsFile", CK_EditExtensionsFile
},
155 {"EditFileHighlightFile", CK_EditFileHighlightFile
},
156 {"LinkSymbolicEdit", CK_LinkSymbolicEdit
},
157 {"ExternalPanelize", CK_ExternalPanelize
},
158 {"Filter", CK_Filter
},
159 #ifdef ENABLE_VFS_FISH
160 {"ConnectFish", CK_ConnectFish
},
162 #ifdef ENABLE_VFS_FTP
163 {"ConnectFtp", CK_ConnectFtp
},
165 #ifdef ENABLE_VFS_SMB
166 {"ConnectSmb", CK_ConnectSmb
},
168 {"PanelInfo", CK_PanelInfo
},
169 #ifdef WITH_BACKGROUND
172 {"OptionsLayout", CK_OptionsLayout
},
174 {"PanelListingChange", CK_PanelListingChange
},
175 {"PanelListing", CK_PanelListing
},
176 #ifdef LISTMODE_EDITOR
177 {"ListMode", CK_ListMode
}.
179 {"OptionsPanel", CK_OptionsPanel
},
180 {"CdQuick", CK_CdQuick
},
181 {"PanelQuickView", CK_PanelQuickView
},
182 {"LinkSymbolicRelative", CK_LinkSymbolicRelative
},
183 {"VfsList", CK_VfsList
},
184 {"SaveSetup", CK_SaveSetup
},
185 {"LinkSymbolic", CK_LinkSymbolic
},
186 {"PanelTree", CK_PanelTree
},
188 #ifdef ENABLE_VFS_UNDELFS
189 {"Undelete", CK_Undelete
},
191 {"PutCurrentLink", CK_PutCurrentLink
},
192 {"PutOtherLink", CK_PutOtherLink
},
193 {"HotListAdd", CK_HotListAdd
},
194 {"ShowHidden", CK_ShowHidden
},
195 {"SplitVertHoriz", CK_SplitVertHoriz
},
196 {"PutCurrentPath", CK_PutCurrentPath
},
197 {"PutOtherPath", CK_PutOtherPath
},
198 {"PutCurrentTagged", CK_PutCurrentTagged
},
199 {"PutOtherTagged", CK_PutOtherTagged
},
200 {"Select", CK_Select
},
201 {"Unselect", CK_Unselect
},
204 {"PanelOtherCd", CK_PanelOtherCd
},
205 {"PanelOtherCdLink", CK_PanelOtherCdLink
},
206 {"CopySingle", CK_CopySingle
},
207 {"MoveSingle", CK_MoveSingle
},
208 {"DeleteSingle", CK_DeleteSingle
},
209 {"CdParent", CK_CdParent
},
210 {"CdChild", CK_CdChild
},
211 {"PanelOtherSync", CK_PanelOtherSync
},
212 {"SortNext", CK_SortNext
},
213 {"SortPrev", CK_SortPrev
},
214 {"SortReverse", CK_SortReverse
},
215 {"SortByName", CK_SortByName
},
216 {"SortByExt", CK_SortByExt
},
217 {"SortBySize", CK_SortBySize
},
218 {"SortByMTime", CK_SortByMTime
},
219 {"CdParentSmart", CK_CdParentSmart
},
223 {"Cancel", CK_Cancel
},
231 {"LinkNext", CK_LinkNext
},
232 {"LinkPrev", CK_LinkPrev
},
233 {"NodeNext", CK_NodeNext
},
234 {"NodePrev", CK_NodePrev
},
237 {"Forget", CK_Forget
},
239 #if defined (USE_INTERNAL_EDIT) || defined (USE_DIFF_VIEW)
240 {"ShowNumbers", CK_ShowNumbers
},
243 #ifdef USE_INTERNAL_EDIT
245 {"ScrollUp", CK_ScrollUp
},
246 {"ScrollDown", CK_ScrollDown
},
247 {"Return", CK_Return
},
248 {"ParagraphUp", CK_ParagraphUp
},
249 {"ParagraphDown", CK_ParagraphDown
},
250 {"EditFile", CK_EditFile
},
251 {"MarkWord", CK_MarkWord
},
252 {"MarkLine", CK_MarkLine
},
253 {"MarkAll", CK_MarkAll
},
254 {"Unmark", CK_Unmark
},
255 {"MarkColumn", CK_MarkColumn
},
256 {"BlockSave", CK_BlockSave
},
257 {"InsertFile", CK_InsertFile
},
258 {"InsertOverwrite", CK_InsertOverwrite
},
260 {"DeleteLine", CK_DeleteLine
},
261 {"DeleteToHome", CK_DeleteToHome
},
262 {"DeleteToEnd", CK_DeleteToEnd
},
263 {"EditMail", CK_Mail
},
264 {"ParagraphFormat", CK_ParagraphFormat
},
265 {"MatchBracket", CK_MatchBracket
},
266 {"ExternalCommand", CK_ExternalCommand
},
267 {"MacroStartRecord", CK_MacroStartRecord
},
268 {"MacroStopRecord", CK_MacroStopRecord
},
269 {"MacroStartStopRecord", CK_MacroStartStopRecord
},
270 {"MacroDelete", CK_MacroDelete
},
271 {"RepeatStartStopRecord", CK_RepeatStartStopRecord
},
272 {"BookmarkFlush", CK_BookmarkFlush
},
273 {"BookmarkNext", CK_BookmarkNext
},
274 {"BookmarkPrev", CK_BookmarkPrev
},
275 {"MarkPageUp", CK_MarkPageUp
},
276 {"MarkPageDown", CK_MarkPageDown
},
277 {"MarkToFileBegin", CK_MarkToFileBegin
},
278 {"MarkToFileEnd", CK_MarkToFileEnd
},
279 {"MarkToPageBegin", CK_MarkToPageBegin
},
280 {"MarkToPageEnd", CK_MarkToPageEnd
},
281 {"MarkScrollUp", CK_MarkScrollUp
},
282 {"MarkScrollDown", CK_MarkScrollDown
},
283 {"MarkParagraphUp", CK_MarkParagraphUp
},
284 {"MarkParagraphDown", CK_MarkParagraphDown
},
285 {"MarkColumnPageUp", CK_MarkColumnPageUp
},
286 {"MarkColumnPageDown", CK_MarkColumnPageDown
},
287 {"MarkColumnLeft", CK_MarkColumnLeft
},
288 {"MarkColumnRight", CK_MarkColumnRight
},
289 {"MarkColumnUp", CK_MarkColumnUp
},
290 {"MarkColumnDown", CK_MarkColumnDown
},
291 {"MarkColumnScrollUp", CK_MarkColumnScrollUp
},
292 {"MarkColumnScrollDown", CK_MarkColumnScrollDown
},
293 {"MarkColumnParagraphUp", CK_MarkColumnParagraphUp
},
294 {"MarkColumnParagraphDown", CK_MarkColumnParagraphDown
},
295 {"BlockShiftLeft", CK_BlockShiftLeft
},
296 {"BlockShiftRight", CK_BlockShiftRight
},
297 {"InsertLiteral", CK_InsertLiteral
},
298 {"ShowTabTws", CK_ShowTabTws
},
299 {"SyntaxOnOff", CK_SyntaxOnOff
},
300 {"SyntaxChoose", CK_SyntaxChoose
},
301 {"ShowMargin", CK_ShowMargin
},
302 {"OptionsSaveMode", CK_OptionsSaveMode
},
304 /* An action to run external script from macro */
305 {"ExecuteScript", CK_PipeBlock (0)},
306 #endif /* USE_INTERNAL_EDIT */
309 {"WrapMode", CK_WrapMode
},
310 {"HexEditMode", CK_HexEditMode
},
311 {"HexMode", CK_HexMode
},
312 {"MagicMode", CK_MagicMode
},
313 {"NroffMode", CK_NroffMode
},
314 {"BookmarkGoto", CK_BookmarkGoto
},
319 {"ShowSymbols", CK_ShowSymbols
},
320 {"SplitFull", CK_SplitFull
},
321 {"SplitEqual", CK_SplitEqual
},
322 {"SplitMore", CK_SplitMore
},
323 {"SplitLess", CK_SplitLess
},
328 {"HunkNext", CK_HunkNext
},
329 {"HunkPrev", CK_HunkPrev
},
330 {"EditOther", CK_EditOther
},
332 #endif /* USE_DIFF_VIEW */
337 static const size_t num_command_names
= G_N_ELEMENTS (command_names
) - 1;
339 /*** file scope functions ************************************************************************/
340 /* --------------------------------------------------------------------------------------------- */
343 name_keymap_comparator (const void *p1
, const void *p2
)
345 const name_keymap_t
*m1
= (const name_keymap_t
*) p1
;
346 const name_keymap_t
*m2
= (const name_keymap_t
*) p2
;
348 return str_casecmp (m1
->name
, m2
->name
);
351 /* --------------------------------------------------------------------------------------------- */
354 sort_command_names (void)
356 static gboolean has_been_sorted
= FALSE
;
358 if (!has_been_sorted
)
360 qsort (command_names
, num_command_names
,
361 sizeof (command_names
[0]), &name_keymap_comparator
);
362 has_been_sorted
= TRUE
;
366 /* --------------------------------------------------------------------------------------------- */
369 keymap_add (GArray
* keymap
, long key
, unsigned long cmd
, const char *caption
)
371 if (key
!= 0 && cmd
!= CK_IgnoreKey
)
373 global_keymap_t new_bind
;
376 new_bind
.command
= cmd
;
377 g_snprintf (new_bind
.caption
, sizeof (new_bind
.caption
), "%s", caption
);
378 g_array_append_val (keymap
, new_bind
);
382 /* --------------------------------------------------------------------------------------------- */
383 /*** public functions ****************************************************************************/
384 /* --------------------------------------------------------------------------------------------- */
387 keybind_cmd_bind (GArray
* keymap
, const char *keybind
, unsigned long action
)
389 char *caption
= NULL
;
392 key
= lookup_key (keybind
, &caption
);
393 keymap_add (keymap
, key
, action
, caption
);
397 /* --------------------------------------------------------------------------------------------- */
400 keybind_lookup_action (const char *name
)
402 const name_keymap_t key
= { name
, 0 };
405 sort_command_names ();
407 res
= bsearch (&key
, command_names
, num_command_names
,
408 sizeof (command_names
[0]), name_keymap_comparator
);
410 return (res
!= NULL
) ? res
->val
: CK_IgnoreKey
;
413 /* --------------------------------------------------------------------------------------------- */
416 keybind_lookup_actionname (unsigned long action
)
420 for (i
= 0; command_names
[i
].name
!= NULL
; i
++)
421 if (command_names
[i
].val
== action
)
422 return command_names
[i
].name
;
427 /* --------------------------------------------------------------------------------------------- */
430 keybind_lookup_keymap_shortcut (const global_keymap_t
* keymap
, unsigned long action
)
435 for (i
= 0; keymap
[i
].key
!= 0; i
++)
436 if (keymap
[i
].command
== action
)
437 return (keymap
[i
].caption
[0] != '\0') ? keymap
[i
].caption
: NULL
;
442 /* --------------------------------------------------------------------------------------------- */
445 keybind_lookup_keymap_command (const global_keymap_t
* keymap
, long key
)
450 for (i
= 0; keymap
[i
].key
!= 0; i
++)
451 if (keymap
[i
].key
== key
)
452 return keymap
[i
].command
;
457 /* --------------------------------------------------------------------------------------------- */