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/keybind.h"
  29. /*** global variables ****************************************************************************/
  30. /*** file scope macro definitions ****************************************************************/
  31. /*** file scope type declarations ****************************************************************/
  32. /*** file scope variables ************************************************************************/
  33. static name_keymap_t command_names[] = {
  34. /* common */
  35. {"Enter", CK_Enter},
  36. {"Up", CK_Up},
  37. {"Down", CK_Down},
  38. {"Left", CK_Left},
  39. {"Right", CK_Right},
  40. {"LeftQuick", CK_LeftQuick},
  41. {"RightQuick", CK_RightQuick},
  42. {"Home", CK_Home},
  43. {"End", CK_End},
  44. {"PageUp", CK_PageUp},
  45. {"PageDown", CK_PageDown},
  46. {"HalfPageUp", CK_HalfPageUp},
  47. {"HalfPageDown", CK_HalfPageDown},
  48. {"Top", CK_Top},
  49. {"Bottom", CK_Bottom},
  50. {"TopOnScreen", CK_TopOnScreen},
  51. {"MiddleOnScreen", CK_MiddleOnScreen},
  52. {"BottomOnScreen", CK_BottomOnScreen},
  53. {"WordLeft", CK_WordLeft},
  54. {"WordRight", CK_WordRight},
  55. {"Copy", CK_Copy},
  56. {"Move", CK_Move},
  57. {"Delete", CK_Delete},
  58. {"MakeDir", CK_MakeDir},
  59. {"ChangeMode", CK_ChangeMode},
  60. {"ChangeOwn", CK_ChangeOwn},
  61. {"ChangeOwnAdvanced", CK_ChangeOwnAdvanced},
  62. {"Remove", CK_Remove},
  63. {"BackSpace", CK_BackSpace},
  64. {"Undo", CK_Undo},
  65. {"Redo", CK_Redo},
  66. {"Clear", CK_Clear},
  67. {"Menu", CK_Menu},
  68. {"MenuLastSelected", CK_MenuLastSelected},
  69. {"UserMenu", CK_UserMenu},
  70. {"EditUserMenu", CK_EditUserMenu},
  71. {"Search", CK_Search},
  72. {"SearchContinue", CK_SearchContinue},
  73. {"Replace", CK_Replace},
  74. {"ReplaceContinue", CK_ReplaceContinue},
  75. {"Help", CK_Help},
  76. {"Shell", CK_Shell},
  77. {"Edit", CK_Edit},
  78. {"EditNew", CK_EditNew},
  79. #ifdef HAVE_CHARSET
  80. {"SelectCodepage", CK_SelectCodepage},
  81. #endif
  82. {"History", CK_History},
  83. {"HistoryNext", CK_HistoryNext},
  84. {"HistoryPrev", CK_HistoryPrev},
  85. {"Complete", CK_Complete},
  86. {"Save", CK_Save},
  87. {"SaveAs", CK_SaveAs},
  88. {"Goto", CK_Goto},
  89. {"Reread", CK_Reread},
  90. {"Refresh", CK_Refresh},
  91. {"Suspend", CK_Suspend},
  92. {"Swap", CK_Swap},
  93. {"HotList", CK_HotList},
  94. {"SelectInvert", CK_SelectInvert},
  95. {"ScreenList", CK_ScreenList},
  96. {"ScreenNext", CK_ScreenNext},
  97. {"ScreenPrev", CK_ScreenPrev},
  98. {"FileNext", CK_FileNext},
  99. {"FilePrev", CK_FilePrev},
  100. {"DeleteToWordBegin", CK_DeleteToWordBegin},
  101. {"DeleteToWordEnd", CK_DeleteToWordEnd},
  102. {"Cut", CK_Cut},
  103. {"Store", CK_Store},
  104. {"Paste", CK_Paste},
  105. {"Mark", CK_Mark},
  106. {"MarkLeft", CK_MarkLeft},
  107. {"MarkRight", CK_MarkRight},
  108. {"MarkUp", CK_MarkUp},
  109. {"MarkDown", CK_MarkDown},
  110. {"MarkToWordBegin", CK_MarkToWordBegin},
  111. {"MarkToWordEnd", CK_MarkToWordEnd},
  112. {"MarkToHome", CK_MarkToHome},
  113. {"MarkToEnd", CK_MarkToEnd},
  114. {"ToggleNavigation", CK_ToggleNavigation},
  115. {"Sort", CK_Sort},
  116. {"Options", CK_Options},
  117. {"LearnKeys", CK_LearnKeys},
  118. {"Bookmark", CK_Bookmark},
  119. {"Quit", CK_Quit},
  120. {"QuitQuiet", CK_QuitQuiet},
  121. {"ExtendedKeyMap", CK_ExtendedKeyMap},
  122. /* main commands */
  123. #ifdef USE_INTERNAL_EDIT
  124. {"EditForceInternal", CK_EditForceInternal},
  125. #endif
  126. {"View", CK_View},
  127. {"ViewRaw", CK_ViewRaw},
  128. {"ViewFile", CK_ViewFile},
  129. {"ViewFiltered", CK_ViewFiltered},
  130. {"Find", CK_Find},
  131. {"DirSize", CK_DirSize},
  132. {"PanelListingSwitch", CK_PanelListingSwitch},
  133. {"CompareDirs", CK_CompareDirs},
  134. #ifdef USE_DIFF_VIEW
  135. {"CompareFiles", CK_CompareFiles},
  136. #endif
  137. {"OptionsVfs", CK_OptionsVfs},
  138. {"OptionsConfirm", CK_OptionsConfirm},
  139. {"OptionsDisplayBits", CK_OptionsDisplayBits},
  140. {"EditExtensionsFile", CK_EditExtensionsFile},
  141. {"EditFileHighlightFile", CK_EditFileHighlightFile},
  142. {"LinkSymbolicEdit", CK_LinkSymbolicEdit},
  143. {"ExternalPanelize", CK_ExternalPanelize},
  144. {"Filter", CK_Filter},
  145. #ifdef ENABLE_VFS_FISH
  146. {"ConnectFish", CK_ConnectFish},
  147. #endif
  148. #ifdef ENABLE_VFS_FTP
  149. {"ConnectFtp", CK_ConnectFtp},
  150. #endif
  151. #ifdef ENABLE_VFS_SMB
  152. {"ConnectSmb", CK_ConnectSmb},
  153. #endif
  154. {"PanelInfo", CK_PanelInfo},
  155. #ifdef WITH_BACKGROUND
  156. {"Jobs", CK_Jobs},
  157. #endif
  158. {"OptionsLayout", CK_OptionsLayout},
  159. {"Link", CK_Link},
  160. {"PanelListingChange", CK_PanelListingChange},
  161. {"PanelListing", CK_PanelListing},
  162. #ifdef LISTMODE_EDITOR
  163. {"ListMode", CK_ListMode}.
  164. #endif
  165. {"OptionsPanel", CK_OptionsPanel},
  166. {"CdQuick", CK_CdQuick},
  167. {"PanelQuickView", CK_PanelQuickView},
  168. {"LinkSymbolicRelative", CK_LinkSymbolicRelative},
  169. {"VfsList", CK_VfsList},
  170. {"SaveSetup", CK_SaveSetup},
  171. {"LinkSymbolic", CK_LinkSymbolic},
  172. {"PanelTree", CK_PanelTree},
  173. {"Tree", CK_Tree},
  174. #ifdef ENABLE_VFS_UNDELFS
  175. {"Undelete", CK_Undelete},
  176. #endif
  177. {"PutCurrentLink", CK_PutCurrentLink},
  178. {"PutOtherLink", CK_PutOtherLink},
  179. {"HotListAdd", CK_HotListAdd},
  180. {"ShowHidden", CK_ShowHidden},
  181. {"SplitVertHoriz", CK_SplitVertHoriz},
  182. {"PutCurrentPath", CK_PutCurrentPath},
  183. {"PutOtherPath", CK_PutOtherPath},
  184. {"PutCurrentTagged", CK_PutCurrentTagged},
  185. {"PutOtherTagged", CK_PutOtherTagged},
  186. {"Select", CK_Select},
  187. {"Unselect", CK_Unselect},
  188. /* panel */
  189. {"PanelOtherCd", CK_PanelOtherCd},
  190. {"PanelOtherCdLink", CK_PanelOtherCdLink},
  191. {"CopySingle", CK_CopySingle},
  192. {"MoveSingle", CK_MoveSingle},
  193. {"DeleteSingle", CK_DeleteSingle},
  194. {"CdParent", CK_CdParent},
  195. {"CdChild", CK_CdChild},
  196. {"Panelize", CK_Panelize},
  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 g_ascii_strcasecmp (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. /* --------------------------------------------------------------------------------------------- */