usermap.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633
  1. /* cooledit.bindings file parser
  2. Authors: 2005 Vitja Makarov
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 2 of the License, or
  6. (at your option) any later version.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software
  13. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307, USA.
  15. */
  16. #include <config.h>
  17. #include <ctype.h>
  18. #include <errno.h>
  19. #include <stdarg.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/types.h>
  24. #include <sys/stat.h>
  25. #include <unistd.h>
  26. #include <mhl/types.h>
  27. #include <mhl/string.h>
  28. #include "../src/global.h"
  29. #include "edit.h"
  30. #include "edit-widget.h"
  31. #include "editcmddef.h" /* list of commands */
  32. #include "usermap.h"
  33. #include "../src/key.h" /* KEY_M_* */
  34. #include "../src/tty.h" /* keys */
  35. #include "../src/wtools.h"
  36. #include "../src/widget.h" /* buttonbar_redraw() */
  37. typedef struct NameMap {
  38. const char *name;
  39. int val;
  40. } name_map_t;
  41. typedef struct Config {
  42. time_t mtime; /* mtime at the moment we read config file */
  43. GArray *keymap;
  44. GArray *ext_keymap;
  45. gchar *labels[10];
  46. } config_t;
  47. typedef struct Command {
  48. const char *name;
  49. bool (*handler) (config_t *cfg, int argc, char *argv[]);
  50. } command_t;
  51. static char error_msg[200] = "Nobody see this";
  52. static const name_map_t key_names[] = {
  53. {"backspace", KEY_BACKSPACE},
  54. {"end", KEY_END},
  55. {"up", KEY_UP},
  56. {"down", KEY_DOWN},
  57. {"left", KEY_LEFT},
  58. {"right", KEY_RIGHT},
  59. {"home", KEY_HOME},
  60. {"a1", KEY_A1},
  61. {"c1", KEY_C1},
  62. {"npage", KEY_NPAGE},
  63. {"ppage", KEY_PPAGE},
  64. {"ic", KEY_IC},
  65. {"enter", KEY_ENTER},
  66. {"dc", KEY_DC},
  67. {"scancel", KEY_SCANCEL},
  68. {"btab", KEY_BTAB},
  69. {"f11", KEY_F(11)},
  70. {"f12", KEY_F(12)},
  71. {"f13", KEY_F(13)},
  72. {"f14", KEY_F(14)},
  73. {"f15", KEY_F(15)},
  74. {"f16", KEY_F(16)},
  75. {"f17", KEY_F(17)},
  76. {"f18", KEY_F(18)},
  77. {"f19", KEY_F(19)},
  78. {"f20", KEY_F(20)},
  79. {"tab", '\t'},
  80. {"return", '\n'},
  81. {NULL, 0}
  82. };
  83. static const name_map_t command_names[] = {
  84. {"No-Command", CK_Ignore_Key},
  85. {"Ignore-Key", CK_Ignore_Key},
  86. {"BackSpace", CK_BackSpace},
  87. {"Delete", CK_Delete},
  88. {"Enter", CK_Enter},
  89. {"Page-Up", CK_Page_Up},
  90. {"Page-Down", CK_Page_Down},
  91. {"Left", CK_Left},
  92. {"Right", CK_Right},
  93. {"Word-Left", CK_Word_Left},
  94. {"Word-Right", CK_Word_Right},
  95. {"Up", CK_Up},
  96. {"Down", CK_Down},
  97. {"Home", CK_Home},
  98. {"End", CK_End},
  99. {"Tab", CK_Tab},
  100. {"Undo", CK_Undo},
  101. {"Beginning-Of-Text", CK_Beginning_Of_Text},
  102. {"End-Of-Text", CK_End_Of_Text},
  103. {"Scroll-Up", CK_Scroll_Up},
  104. {"Scroll-Down", CK_Scroll_Down},
  105. {"Return", CK_Return},
  106. {"Begin-Page", CK_Begin_Page},
  107. {"End-Page", CK_End_Page},
  108. {"Delete-Word-Left", CK_Delete_Word_Left},
  109. {"Delete-Word-Right", CK_Delete_Word_Right},
  110. {"Paragraph-Up", CK_Paragraph_Up},
  111. {"Paragraph-Down", CK_Paragraph_Down},
  112. {"Save", CK_Save},
  113. {"Load", CK_Load},
  114. {"New", CK_New},
  115. {"Save-as", CK_Save_As},
  116. {"Mark", CK_Mark},
  117. {"Copy", CK_Copy},
  118. {"Move", CK_Move},
  119. {"Remove", CK_Remove},
  120. {"Unmark", CK_Unmark},
  121. {"Save-Block", CK_Save_Block},
  122. {"Column-Mark", CK_Column_Mark},
  123. {"Find", CK_Find},
  124. {"Find-Again", CK_Find_Again},
  125. {"Replace", CK_Replace},
  126. {"Replace-Again", CK_Replace_Again},
  127. {"Complete-Word", CK_Complete_Word},
  128. {"Debug-Start", CK_Debug_Start},
  129. {"Debug-Stop", CK_Debug_Stop},
  130. {"Debug-Toggle-Break", CK_Debug_Toggle_Break},
  131. {"Debug-Clear", CK_Debug_Clear},
  132. {"Debug-Next", CK_Debug_Next},
  133. {"Debug-Step", CK_Debug_Step},
  134. {"Debug-Back-Trace", CK_Debug_Back_Trace},
  135. {"Debug-Continue", CK_Debug_Continue},
  136. {"Debug-Enter-Command", CK_Debug_Enter_Command},
  137. {"Debug-Until-Curser", CK_Debug_Until_Curser},
  138. {"Insert-File", CK_Insert_File},
  139. {"Exit", CK_Exit},
  140. {"Toggle-Insert", CK_Toggle_Insert},
  141. {"Help", CK_Help},
  142. {"Date", CK_Date},
  143. {"Refresh", CK_Refresh},
  144. {"Goto", CK_Goto},
  145. {"Delete-Line", CK_Delete_Line},
  146. {"Delete-To-Line-End", CK_Delete_To_Line_End},
  147. {"Delete-To-Line-Begin", CK_Delete_To_Line_Begin},
  148. {"Man-Page", CK_Man_Page},
  149. {"Sort", CK_Sort},
  150. {"Mail", CK_Mail},
  151. {"Cancel", CK_Cancel},
  152. {"Complete", CK_Complete},
  153. {"Paragraph-Format", CK_Paragraph_Format},
  154. {"Util", CK_Util},
  155. {"Type-Load-Python", CK_Type_Load_Python},
  156. {"Find-File", CK_Find_File},
  157. {"Ctags", CK_Ctags},
  158. {"Match-Bracket", CK_Match_Bracket},
  159. {"Terminal", CK_Terminal},
  160. {"Terminal-App", CK_Terminal_App},
  161. {"ExtCmd", CK_ExtCmd},
  162. {"User-Menu", CK_User_Menu},
  163. {"Save-Desktop", CK_Save_Desktop},
  164. {"New-Window", CK_New_Window},
  165. {"Cycle", CK_Cycle},
  166. {"Menu", CK_Menu},
  167. {"Save-And-Quit", CK_Save_And_Quit},
  168. {"Run-Another", CK_Run_Another},
  169. {"Check-Save-And-Quit", CK_Check_Save_And_Quit},
  170. {"Maximize", CK_Maximize},
  171. {"Begin-Record-Macro", CK_Begin_Record_Macro},
  172. {"End-Record-Macro", CK_End_Record_Macro},
  173. {"Delete-Macro", CK_Delete_Macro},
  174. {"Toggle-Bookmark", CK_Toggle_Bookmark},
  175. {"Flush-Bookmarks", CK_Flush_Bookmarks},
  176. {"Next-Bookmark", CK_Next_Bookmark},
  177. {"Prev-Bookmark", CK_Prev_Bookmark},
  178. {"Page-Up-Highlight", CK_Page_Up_Highlight},
  179. {"Page-Down-Highlight", CK_Page_Down_Highlight},
  180. {"Left-Highlight", CK_Left_Highlight},
  181. {"Right-Highlight", CK_Right_Highlight},
  182. {"Word-Left-Highlight", CK_Word_Left_Highlight},
  183. {"Word-Right-Highlight", CK_Word_Right_Highlight},
  184. {"Up-Highlight", CK_Up_Highlight},
  185. {"Down-Highlight", CK_Down_Highlight},
  186. {"Home-Highlight", CK_Home_Highlight},
  187. {"End-Highlight", CK_End_Highlight},
  188. {"Beginning-Of-Text-Highlight", CK_Beginning_Of_Text_Highlight},
  189. {"End-Of-Text_Highlight", CK_End_Of_Text_Highlight},
  190. {"Begin-Page-Highlight", CK_Begin_Page_Highlight},
  191. {"End-Page-Highlight", CK_End_Page_Highlight},
  192. {"Scroll-Up-Highlight", CK_Scroll_Up_Highlight},
  193. {"Scroll-Down-Highlight", CK_Scroll_Down_Highlight},
  194. {"Paragraph-Up-Highlight", CK_Paragraph_Up_Highlight},
  195. {"Paragraph-Down-Highlight", CK_Paragraph_Down_Highlight},
  196. {"XStore", CK_XStore},
  197. {"XCut", CK_XCut},
  198. {"XPaste", CK_XPaste},
  199. {"Selection-History", CK_Selection_History},
  200. {"Shell", CK_Shell},
  201. {"Select-Codepage", CK_Select_Codepage},
  202. {"Insert-Literal", CK_Insert_Literal},
  203. {"Execute-Macro", CK_Execute_Macro},
  204. {"Begin-or-End-Macro", CK_Begin_End_Macro},
  205. {"Ext-mode", CK_Ext_Mode},
  206. #if 0
  207. {"Focus-Next", CK_Focus_Next},
  208. {"Focus-Prev", CK_Focus_Prev},
  209. {"Height-Inc", CK_Height_Inc},
  210. {"Height-Dec", CK_Height_Dec},
  211. {"Make", CK_Make},
  212. {"Error-Next", CK_Error_Next},
  213. {"Error-Prev", CK_Error_Prev},
  214. #endif
  215. {NULL, 0}
  216. };
  217. static void
  218. cfg_free_maps(config_t *cfg)
  219. {
  220. int i;
  221. if (cfg->keymap)
  222. g_array_free(cfg->keymap, TRUE);
  223. cfg->keymap = NULL;
  224. if (cfg->ext_keymap)
  225. g_array_free(cfg->ext_keymap, TRUE);
  226. cfg->ext_keymap = NULL;
  227. for (i = 0; i < 10; i++)
  228. g_free(cfg->labels[i]);
  229. }
  230. /* Returns an array containing the words in str. WARNING: As long as
  231. * the result is used, str[...] must be valid memory. This function
  232. * modifies str[...] and uses it, so the caller should not use it until
  233. * g_ptr_array_free() is called for the returned result.
  234. */
  235. static GPtrArray *
  236. split_line(char *str)
  237. {
  238. bool inside_quote = false;
  239. int move = 0;
  240. GPtrArray *args;
  241. args = g_ptr_array_new();
  242. /* skip spaces */
  243. while (isspace((unsigned char) *str))
  244. str++;
  245. g_ptr_array_add(args, str);
  246. for (;; str++) {
  247. switch (*str) {
  248. case '#': /* cut off comments */
  249. if (inside_quote)
  250. break;
  251. /* FALLTHROUGH */
  252. case '\n': /* end of line */
  253. case '\r':
  254. case '\0':
  255. if (str == g_ptr_array_index(args, args->len - 1))
  256. g_ptr_array_remove_index(args, args->len - 1);
  257. else
  258. *(str - move) = '\0';
  259. return args;
  260. case '"': /* quote */
  261. case '\'':
  262. inside_quote = !inside_quote;
  263. move++;
  264. continue;
  265. case ' ': /* spaces */
  266. case '\t':
  267. if (inside_quote)
  268. break;
  269. *(str++ - move) = '\0';
  270. move = 0;
  271. /* skip spaces */
  272. while (isspace((unsigned char) *str))
  273. str++;
  274. g_ptr_array_add(args, str--);
  275. break;
  276. case '\\':
  277. switch (*(++str)) {
  278. case 'n':
  279. *str = '\n';
  280. break;
  281. case 'r':
  282. *str = '\r';
  283. break;
  284. case 't':
  285. *str = '\t';
  286. break;
  287. }
  288. move++;
  289. break;
  290. }
  291. if (move != 0)
  292. *(str - move) = *str;
  293. }
  294. /* never be here */
  295. }
  296. static void
  297. keymap_add(GArray *keymap, int key, int cmd)
  298. {
  299. edit_key_map_type new_one, *map;
  300. guint i;
  301. map = &(g_array_index(keymap, edit_key_map_type, 0));
  302. for (i = 0; i < keymap->len; i++) {
  303. if (map[i].key == key) {
  304. map[i].command = cmd;
  305. return;
  306. }
  307. }
  308. new_one.key = key;
  309. new_one.command = cmd;
  310. g_array_append_val(keymap, new_one);
  311. }
  312. /* bind <key> <command> */
  313. static bool
  314. cmd_bind(config_t *cfg, int argc, char *argv[])
  315. {
  316. char *keyname, *command;
  317. const name_map_t *key = key_names;
  318. const name_map_t *cmd = command_names;
  319. int mod = 0, k = -1, m = 0;
  320. if (argc != 3) {
  321. snprintf(error_msg, sizeof(error_msg),
  322. _("bind: Wrong argument number, bind <key> <command>"));
  323. return false;
  324. }
  325. keyname = argv[1];
  326. command = argv[2];
  327. while (*keyname) {
  328. switch (*keyname++) {
  329. case 'C':
  330. m = KEY_M_CTRL;
  331. continue;
  332. case 'M':
  333. case 'A':
  334. m = KEY_M_ALT;
  335. continue;
  336. case 'S':
  337. m = KEY_M_SHIFT;
  338. continue;
  339. case '-':
  340. if (!m) { /* incorrect key */
  341. snprintf(error_msg, sizeof(error_msg),
  342. _("bind: Bad key value `%s'"), keyname);
  343. return false;
  344. }
  345. mod |= m;
  346. m = 0;
  347. continue;
  348. }
  349. keyname--;
  350. break;
  351. }
  352. /* no key */
  353. if (keyname[0] == '\0') {
  354. snprintf(error_msg, sizeof(error_msg), _("bind: Ehh...no key?"));
  355. return false;
  356. }
  357. /* ordinary key */
  358. if (keyname[1] == '\0') {
  359. k = keyname[0];
  360. } else {
  361. while (key->name && strcasecmp(key->name, keyname) != 0)
  362. key++;
  363. }
  364. if (k < 0 && !key->name) {
  365. snprintf(error_msg, sizeof(error_msg),
  366. _("bind: Unknown key: `%s'"), keyname);
  367. return false;
  368. }
  369. while (cmd->name && strcasecmp(cmd->name, command) != 0)
  370. cmd++;
  371. if (!cmd->name) {
  372. snprintf(error_msg, sizeof(error_msg),
  373. _("bind: Unknown command: `%s'"), command);
  374. return false;
  375. }
  376. if (mod & KEY_M_CTRL) {
  377. if (k < 256)
  378. k = XCTRL(k);
  379. else
  380. k |= KEY_M_CTRL;
  381. }
  382. if (mod & KEY_M_ALT)
  383. k |= KEY_M_ALT;
  384. if (mod & KEY_M_SHIFT)
  385. k |= KEY_M_SHIFT;
  386. if (!strcasecmp("bind-ext", argv[0]))
  387. keymap_add(cfg->ext_keymap, k, cmd->val);
  388. else
  389. keymap_add(cfg->keymap, k, cmd->val);
  390. return true;
  391. }
  392. #if 0
  393. #define CMD_F(x) \
  394. static void cmd_F ## x (WEdit * edit) \
  395. { \
  396. send_message ((Widget *) edit, WIDGET_KEY, KEY_F (x)); \
  397. }
  398. CMD_F(1)
  399. CMD_F(2)
  400. CMD_F(3)
  401. CMD_F(4)
  402. CMD_F(5)
  403. CMD_F(6)
  404. CMD_F(7)
  405. CMD_F(8)
  406. CMD_F(9)
  407. CMD_F(10)
  408. void (*cmd_Fx[]) (WEdit *) = {
  409. cmd_F1, cmd_F2, cmd_F3, cmd_F4, cmd_F5,
  410. cmd_F6, cmd_F7, cmd_F8, cmd_F9, cmd_F10
  411. } ;
  412. /* move me! */
  413. static void edit_my_define (Dlg_head * h, int idx, const char *text,
  414. void (*fn) (WEdit *), WEdit * edit)
  415. {
  416. /* function-cast ok */
  417. buttonbar_set_label_data (h, idx, text, (buttonbarfn) fn, edit);
  418. }
  419. #endif
  420. /* label <number> <command> <label> */
  421. static bool
  422. cmd_label(config_t *cfg, int argc, char *argv[])
  423. {
  424. const name_map_t *cmd = command_names;
  425. const char *command, *label;
  426. int fn;
  427. if (argc != 4) {
  428. snprintf(error_msg, sizeof(error_msg),
  429. _("%s: Syntax: %s <n> <command> <label>"),
  430. argv[0], argv[0]);
  431. return false;
  432. }
  433. fn = strtol(argv[1], NULL, 0);
  434. command = argv[2];
  435. label = argv[3];
  436. while (cmd->name && strcasecmp(cmd->name, command) != 0)
  437. cmd++;
  438. if (!cmd->name) {
  439. snprintf(error_msg, sizeof(error_msg),
  440. _("%s: Unknown command: `%s'"), argv[0], command);
  441. return false;
  442. }
  443. if (fn < 1 || fn > 10) {
  444. snprintf(error_msg, sizeof(error_msg),
  445. _("%s: fn should be 1-10"), argv[0]);
  446. return false;
  447. }
  448. keymap_add(cfg->keymap, KEY_F(fn), cmd->val);
  449. cfg->labels[fn - 1] = mhl_str_dup(label);
  450. return true;
  451. }
  452. static bool
  453. parse_file(config_t *cfg, const char *file, const command_t *cmd)
  454. {
  455. char buf[200];
  456. int line = 0;
  457. FILE *fp = fopen(file, "r");
  458. if (!fp) {
  459. snprintf(error_msg, sizeof(error_msg), _("%s: fopen(): %s"),
  460. file, strerror(errno));
  461. return false;
  462. }
  463. while (fgets(buf, sizeof(buf), fp)) {
  464. const command_t *c = cmd;
  465. GPtrArray *args;
  466. char **argv;
  467. line++;
  468. args = split_line(buf);
  469. argv = (char **) args->pdata;
  470. if (args->len == 0) {
  471. g_ptr_array_free(args, TRUE);
  472. continue;
  473. }
  474. while (c->name != NULL && strcasecmp(c->name, argv[0]) != 0)
  475. c++;
  476. if (c->name == NULL) {
  477. snprintf(error_msg, sizeof(error_msg),
  478. _("%s:%d: unknown command `%s'"), file, line,
  479. argv[0]);
  480. g_ptr_array_free(args, TRUE);
  481. fclose(fp);
  482. return false;
  483. }
  484. if (!(c->handler(cfg, args->len, argv))) {
  485. char *ss = mhl_str_dup(error_msg);
  486. snprintf(error_msg, sizeof(error_msg),
  487. _("%s:%d: %s"), file, line, ss);
  488. g_free(ss);
  489. g_ptr_array_free(args, TRUE);
  490. fclose(fp);
  491. return false;
  492. }
  493. g_ptr_array_free(args, TRUE);
  494. }
  495. fclose(fp);
  496. return true;
  497. }
  498. static bool
  499. load_user_keymap(config_t *cfg, const char *file)
  500. {
  501. const command_t cmd[] = {
  502. {"bind", cmd_bind},
  503. {"bind-ext", cmd_bind},
  504. {"label", cmd_label},
  505. {0, 0}
  506. };
  507. cfg->keymap = g_array_new(TRUE, FALSE, sizeof(edit_key_map_type));
  508. cfg->ext_keymap = g_array_new(TRUE, FALSE, sizeof(edit_key_map_type));
  509. if (!parse_file(cfg, file, cmd)) {
  510. return false;
  511. }
  512. return true;
  513. }
  514. bool
  515. edit_load_user_map(WEdit *edit)
  516. {
  517. static config_t cfg;
  518. config_t new_cfg;
  519. char *file;
  520. struct stat s;
  521. if (edit_key_emulation != EDIT_KEY_EMULATION_USER)
  522. return true;
  523. file = mhl_str_dir_plus_file(home_dir, MC_USERMAP);
  524. if (stat(file, &s) < 0) {
  525. char *msg = g_strdup_printf(_("%s not found!"), file);
  526. edit_error_dialog(_("Error"), msg);
  527. g_free(msg);
  528. g_free(file);
  529. return false;
  530. }
  531. if (s.st_mtime != cfg.mtime) {
  532. memset(&new_cfg, 0, sizeof(new_cfg));
  533. new_cfg.mtime = s.st_mtime;
  534. if (!load_user_keymap(&new_cfg, file)) {
  535. edit_error_dialog(_("Error"), error_msg);
  536. cfg_free_maps(&new_cfg);
  537. g_free(file);
  538. return false;
  539. } else {
  540. cfg_free_maps(&cfg);
  541. cfg = new_cfg;
  542. }
  543. }
  544. edit->user_map = (edit_key_map_type *) cfg.keymap->data;
  545. edit->ext_map = (edit_key_map_type *) cfg.ext_keymap->data;
  546. memcpy(edit->labels, cfg.labels, sizeof(edit->labels));
  547. g_free(file);
  548. return true;
  549. }