keybind.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  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. {"Panelize", CK_Panelize},
  198. {"PanelOtherSync", CK_PanelOtherSync},
  199. {"SortNext", CK_SortNext},
  200. {"SortPrev", CK_SortPrev},
  201. {"SortReverse", CK_SortReverse},
  202. {"SortByName", CK_SortByName},
  203. {"SortByExt", CK_SortByExt},
  204. {"SortBySize", CK_SortBySize},
  205. {"SortByMTime", CK_SortByMTime},
  206. {"CdParentSmart", CK_CdParentSmart},
  207. /* dialog */
  208. {"Ok", CK_Ok},
  209. {"Cancel", CK_Cancel},
  210. /* input line */
  211. {"Yank", CK_Yank},
  212. /* help */
  213. {"Index", CK_Index},
  214. {"Back", CK_Back},
  215. {"LinkNext", CK_LinkNext},
  216. {"LinkPrev", CK_LinkPrev},
  217. {"NodeNext", CK_NodeNext},
  218. {"NodePrev", CK_NodePrev},
  219. /* tree */
  220. {"Forget", CK_Forget},
  221. #if defined (USE_INTERNAL_EDIT) || defined (USE_DIFF_VIEW)
  222. {"ShowNumbers", CK_ShowNumbers},
  223. #endif
  224. #ifdef USE_INTERNAL_EDIT
  225. {"Tab", CK_Tab},
  226. {"ScrollUp", CK_ScrollUp},
  227. {"ScrollDown", CK_ScrollDown},
  228. {"Return", CK_Return},
  229. {"ParagraphUp", CK_ParagraphUp},
  230. {"ParagraphDown", CK_ParagraphDown},
  231. {"EditFile", CK_EditFile},
  232. {"MarkWord", CK_MarkWord},
  233. {"MarkLine", CK_MarkLine},
  234. {"MarkAll", CK_MarkAll},
  235. {"Unmark", CK_Unmark},
  236. {"MarkColumn", CK_MarkColumn},
  237. {"BlockSave", CK_BlockSave},
  238. {"InsertFile", CK_InsertFile},
  239. {"InsertOverwrite", CK_InsertOverwrite},
  240. {"Date", CK_Date},
  241. {"DeleteLine", CK_DeleteLine},
  242. {"DeleteToHome", CK_DeleteToHome},
  243. {"DeleteToEnd", CK_DeleteToEnd},
  244. {"EditMail", CK_Mail},
  245. {"ParagraphFormat", CK_ParagraphFormat},
  246. {"MatchBracket", CK_MatchBracket},
  247. {"ExternalCommand", CK_ExternalCommand},
  248. {"MacroStartRecord", CK_MacroStartRecord},
  249. {"MacroStopRecord", CK_MacroStopRecord},
  250. {"MacroStartStopRecord", CK_MacroStartStopRecord},
  251. {"MacroDelete", CK_MacroDelete},
  252. {"RepeatStartStopRecord", CK_RepeatStartStopRecord},
  253. {"BookmarkFlush", CK_BookmarkFlush},
  254. {"BookmarkNext", CK_BookmarkNext},
  255. {"BookmarkPrev", CK_BookmarkPrev},
  256. {"MarkPageUp", CK_MarkPageUp},
  257. {"MarkPageDown", CK_MarkPageDown},
  258. {"MarkToFileBegin", CK_MarkToFileBegin},
  259. {"MarkToFileEnd", CK_MarkToFileEnd},
  260. {"MarkToPageBegin", CK_MarkToPageBegin},
  261. {"MarkToPageEnd", CK_MarkToPageEnd},
  262. {"MarkScrollUp", CK_MarkScrollUp},
  263. {"MarkScrollDown", CK_MarkScrollDown},
  264. {"MarkParagraphUp", CK_MarkParagraphUp},
  265. {"MarkParagraphDown", CK_MarkParagraphDown},
  266. {"MarkColumnPageUp", CK_MarkColumnPageUp},
  267. {"MarkColumnPageDown", CK_MarkColumnPageDown},
  268. {"MarkColumnLeft", CK_MarkColumnLeft},
  269. {"MarkColumnRight", CK_MarkColumnRight},
  270. {"MarkColumnUp", CK_MarkColumnUp},
  271. {"MarkColumnDown", CK_MarkColumnDown},
  272. {"MarkColumnScrollUp", CK_MarkColumnScrollUp},
  273. {"MarkColumnScrollDown", CK_MarkColumnScrollDown},
  274. {"MarkColumnParagraphUp", CK_MarkColumnParagraphUp},
  275. {"MarkColumnParagraphDown", CK_MarkColumnParagraphDown},
  276. {"BlockShiftLeft", CK_BlockShiftLeft},
  277. {"BlockShiftRight", CK_BlockShiftRight},
  278. {"InsertLiteral", CK_InsertLiteral},
  279. {"ShowTabTws", CK_ShowTabTws},
  280. {"SyntaxOnOff", CK_SyntaxOnOff},
  281. {"SyntaxChoose", CK_SyntaxChoose},
  282. {"ShowMargin", CK_ShowMargin},
  283. {"OptionsSaveMode", CK_OptionsSaveMode},
  284. {"About", CK_About},
  285. /* An action to run external script from macro */
  286. {"ExecuteScript", CK_PipeBlock (0)},
  287. #endif /* USE_INTERNAL_EDIT */
  288. /* viewer */
  289. {"WrapMode", CK_WrapMode},
  290. {"HexEditMode", CK_HexEditMode},
  291. {"HexMode", CK_HexMode},
  292. {"MagicMode", CK_MagicMode},
  293. {"NroffMode", CK_NroffMode},
  294. {"BookmarkGoto", CK_BookmarkGoto},
  295. {"Ruler", CK_Ruler},
  296. #ifdef USE_DIFF_VIEW
  297. /* diff viewer */
  298. {"ShowSymbols", CK_ShowSymbols},
  299. {"SplitFull", CK_SplitFull},
  300. {"SplitEqual", CK_SplitEqual},
  301. {"SplitMore", CK_SplitMore},
  302. {"SplitLess", CK_SplitLess},
  303. {"Tab2", CK_Tab2},
  304. {"Tab3", CK_Tab3},
  305. {"Tab4", CK_Tab4},
  306. {"Tab8", CK_Tab8},
  307. {"HunkNext", CK_HunkNext},
  308. {"HunkPrev", CK_HunkPrev},
  309. {"EditOther", CK_EditOther},
  310. {"Merge", CK_Merge},
  311. #endif /* USE_DIFF_VIEW */
  312. {NULL, CK_IgnoreKey}
  313. };
  314. /* *INDENT-OFF* */
  315. static const size_t num_command_names = G_N_ELEMENTS (command_names) - 1;
  316. /* *INDENT-ON* */
  317. /*** file scope functions ************************************************************************/
  318. /* --------------------------------------------------------------------------------------------- */
  319. static int
  320. name_keymap_comparator (const void *p1, const void *p2)
  321. {
  322. const name_keymap_t *m1 = (const name_keymap_t *) p1;
  323. const name_keymap_t *m2 = (const name_keymap_t *) p2;
  324. return str_casecmp (m1->name, m2->name);
  325. }
  326. /* --------------------------------------------------------------------------------------------- */
  327. static inline void
  328. sort_command_names (void)
  329. {
  330. static gboolean has_been_sorted = FALSE;
  331. if (!has_been_sorted)
  332. {
  333. qsort (command_names, num_command_names,
  334. sizeof (command_names[0]), &name_keymap_comparator);
  335. has_been_sorted = TRUE;
  336. }
  337. }
  338. /* --------------------------------------------------------------------------------------------- */
  339. static void
  340. keymap_add (GArray * keymap, long key, unsigned long cmd, const char *caption)
  341. {
  342. if (key != 0 && cmd != CK_IgnoreKey)
  343. {
  344. global_keymap_t new_bind;
  345. new_bind.key = key;
  346. new_bind.command = cmd;
  347. g_snprintf (new_bind.caption, sizeof (new_bind.caption), "%s", caption);
  348. g_array_append_val (keymap, new_bind);
  349. }
  350. }
  351. /* --------------------------------------------------------------------------------------------- */
  352. /*** public functions ****************************************************************************/
  353. /* --------------------------------------------------------------------------------------------- */
  354. void
  355. keybind_cmd_bind (GArray * keymap, const char *keybind, unsigned long action)
  356. {
  357. char *caption = NULL;
  358. long key;
  359. key = lookup_key (keybind, &caption);
  360. keymap_add (keymap, key, action, caption);
  361. g_free (caption);
  362. }
  363. /* --------------------------------------------------------------------------------------------- */
  364. unsigned long
  365. keybind_lookup_action (const char *name)
  366. {
  367. const name_keymap_t key = { name, 0 };
  368. name_keymap_t *res;
  369. sort_command_names ();
  370. res = bsearch (&key, command_names, num_command_names,
  371. sizeof (command_names[0]), name_keymap_comparator);
  372. return (res != NULL) ? res->val : CK_IgnoreKey;
  373. }
  374. /* --------------------------------------------------------------------------------------------- */
  375. const char *
  376. keybind_lookup_actionname (unsigned long action)
  377. {
  378. size_t i;
  379. for (i = 0; command_names[i].name != NULL; i++)
  380. if (command_names[i].val == action)
  381. return command_names[i].name;
  382. return NULL;
  383. }
  384. /* --------------------------------------------------------------------------------------------- */
  385. const char *
  386. keybind_lookup_keymap_shortcut (const global_keymap_t * keymap, unsigned long action)
  387. {
  388. size_t i;
  389. if (keymap != NULL)
  390. for (i = 0; keymap[i].key != 0; i++)
  391. if (keymap[i].command == action)
  392. return (keymap[i].caption[0] != '\0') ? keymap[i].caption : NULL;
  393. return NULL;
  394. }
  395. /* --------------------------------------------------------------------------------------------- */
  396. unsigned long
  397. keybind_lookup_keymap_command (const global_keymap_t * keymap, long key)
  398. {
  399. size_t i;
  400. if (keymap != NULL)
  401. for (i = 0; keymap[i].key != 0; i++)
  402. if (keymap[i].key == key)
  403. return keymap[i].command;
  404. return CK_IgnoreKey;
  405. }
  406. /* --------------------------------------------------------------------------------------------- */