keybind.c 14 KB

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