editkeys.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /* Editor key translation.
  2. Copyright (C) 1996, 1997 the Free Software Foundation
  3. Authors: 1996, 1997 Paul Sheer
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 2 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  15. 02111-1307, USA.
  16. */
  17. #include <config.h>
  18. #include <stdio.h>
  19. #include <stdarg.h>
  20. #include <sys/types.h>
  21. #include <unistd.h>
  22. #include <string.h>
  23. #include <ctype.h>
  24. #include <errno.h>
  25. #include <sys/stat.h>
  26. #include <stdlib.h>
  27. #include "../src/global.h"
  28. #include "edit.h"
  29. #include "edit-widget.h" /* edit->macro_i */
  30. #include "editcmddef.h" /* list of commands */
  31. #include "../src/key.h" /* KEY_M_SHIFT */
  32. #include "../src/tty.h" /* keys */
  33. #include "../src/charsets.h" /* convert_from_input_c() */
  34. #include "../src/selcodepage.h" /* do_select_codepage() */
  35. /*
  36. * Ordinary translations. Note that the keys listed first take priority
  37. * when the key is assigned to more than one command.
  38. */
  39. static const long cooledit_key_map[] = {
  40. ALT ('b'), CK_Match_Bracket,
  41. ALT ('m'), CK_Mail,
  42. XCTRL ('f'), CK_Save_Block,
  43. XCTRL ('n'), CK_New,
  44. XCTRL ('p'), CK_Pipe_Block (1), /* spell check */
  45. XCTRL ('x'), CK_Word_Right,
  46. XCTRL ('y'), CK_Delete_Line,
  47. XCTRL ('z'), CK_Word_Left,
  48. 0, 0
  49. };
  50. static long const emacs_key_map[] = {
  51. ALT ('$'), CK_Pipe_Block (1), /* spell check */
  52. ALT ('b'), CK_Word_Left,
  53. ALT ('f'), CK_Word_Right,
  54. ALT ('v'), CK_Page_Up,
  55. ALT ('w'), CK_XStore,
  56. XCTRL ('@'), CK_Mark,
  57. XCTRL ('a'), CK_Home,
  58. XCTRL ('b'), CK_Left,
  59. XCTRL ('e'), CK_End,
  60. XCTRL ('f'), CK_Right,
  61. XCTRL ('g'), CK_Ignore_Key,
  62. XCTRL ('n'), CK_Down,
  63. XCTRL ('p'), CK_Up,
  64. XCTRL ('s'), CK_Find,
  65. XCTRL ('v'), CK_Page_Down,
  66. XCTRL ('w'), CK_XCut,
  67. XCTRL ('y'), CK_XPaste,
  68. 0, 0
  69. };
  70. static long const common_key_map[] = {
  71. '\n', CK_Enter,
  72. '\t', CK_Tab,
  73. ESC_CHAR, CK_Exit,
  74. KEY_BACKSPACE, CK_BackSpace,
  75. KEY_DC, CK_Delete,
  76. KEY_DOWN, CK_Down,
  77. KEY_END, CK_End,
  78. KEY_HOME, CK_Home,
  79. KEY_IC, CK_Toggle_Insert,
  80. KEY_LEFT, CK_Left,
  81. KEY_NPAGE, CK_Page_Down,
  82. KEY_PPAGE, CK_Page_Up,
  83. KEY_RIGHT, CK_Right,
  84. KEY_UP, CK_Up,
  85. ALT ('\n'), CK_Return,
  86. ALT ('\t'), CK_Complete_Word,
  87. ALT ('l'), CK_Goto,
  88. ALT ('L'), CK_Goto,
  89. ALT ('p'), CK_Paragraph_Format,
  90. ALT ('t'), CK_Sort,
  91. ALT ('u'), CK_ExtCmd,
  92. ALT ('<'), CK_Beginning_Of_Text,
  93. ALT ('>'), CK_End_Of_Text,
  94. ALT (KEY_BACKSPACE), CK_Delete_Word_Left,
  95. XCTRL ('k'), CK_Delete_To_Line_End,
  96. XCTRL ('l'), CK_Refresh,
  97. XCTRL ('o'), CK_Shell,
  98. XCTRL ('u'), CK_Undo,
  99. KEY_F (1), CK_Help,
  100. KEY_F (2), CK_Save,
  101. KEY_F (3), CK_Mark,
  102. KEY_F (4), CK_Replace,
  103. KEY_F (5), CK_Copy,
  104. KEY_F (6), CK_Move,
  105. KEY_F (7), CK_Find,
  106. KEY_F (8), CK_Remove,
  107. KEY_F (10), CK_Exit,
  108. KEY_F (11), CK_User_Menu, /* edit user menu */
  109. KEY_F (12), CK_Save_As,
  110. KEY_F (13), CK_Column_Mark,
  111. KEY_F (14), CK_Replace_Again,
  112. KEY_F (15), CK_Insert_File,
  113. KEY_F (17), CK_Find_Again,
  114. KEY_F (19), CK_Pipe_Block (0), /* C formatter */
  115. /* Shift */
  116. KEY_M_SHIFT | KEY_PPAGE, CK_Page_Up_Highlight,
  117. KEY_M_SHIFT | KEY_NPAGE, CK_Page_Down_Highlight,
  118. KEY_M_SHIFT | KEY_LEFT, CK_Left_Highlight,
  119. KEY_M_SHIFT | KEY_RIGHT, CK_Right_Highlight,
  120. KEY_M_SHIFT | KEY_UP, CK_Up_Highlight,
  121. KEY_M_SHIFT | KEY_DOWN, CK_Down_Highlight,
  122. KEY_M_SHIFT | KEY_HOME, CK_Home_Highlight,
  123. KEY_M_SHIFT | KEY_END, CK_End_Highlight,
  124. KEY_M_SHIFT | KEY_IC, CK_XPaste,
  125. KEY_M_SHIFT | KEY_DC, CK_XCut,
  126. KEY_M_SHIFT | '\n', CK_Return, /* useful for pasting multiline text */
  127. /* Ctrl */
  128. KEY_M_CTRL | (KEY_F (2)), CK_Save_As,
  129. KEY_M_CTRL | (KEY_F (4)), CK_Replace_Again,
  130. KEY_M_CTRL | (KEY_F (7)), CK_Find_Again,
  131. KEY_M_CTRL | KEY_BACKSPACE, CK_Undo,
  132. KEY_M_CTRL | KEY_PPAGE, CK_Beginning_Of_Text,
  133. KEY_M_CTRL | KEY_NPAGE, CK_End_Of_Text,
  134. KEY_M_CTRL | KEY_HOME, CK_Beginning_Of_Text,
  135. KEY_M_CTRL | KEY_END, CK_End_Of_Text,
  136. KEY_M_CTRL | KEY_UP, CK_Scroll_Up,
  137. KEY_M_CTRL | KEY_DOWN, CK_Scroll_Down,
  138. KEY_M_CTRL | KEY_LEFT, CK_Word_Left,
  139. KEY_M_CTRL | KEY_RIGHT, CK_Word_Right,
  140. KEY_M_CTRL | KEY_IC, CK_XStore,
  141. KEY_M_CTRL | KEY_DC, CK_Remove,
  142. /* Ctrl + Shift */
  143. KEY_M_SHIFT | KEY_M_CTRL | KEY_PPAGE, CK_Beginning_Of_Text_Highlight,
  144. KEY_M_SHIFT | KEY_M_CTRL | KEY_NPAGE, CK_End_Of_Text_Highlight,
  145. KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, CK_Word_Left_Highlight,
  146. KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, CK_Word_Right_Highlight,
  147. KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, CK_Scroll_Up_Highlight,
  148. KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, CK_Scroll_Down_Highlight,
  149. 0, 0
  150. };
  151. /*
  152. * Translate the keycode into either 'command' or 'char_for_insertion'.
  153. * 'command' is one of the editor commands from editcmddef.h.
  154. */
  155. int
  156. edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch)
  157. {
  158. int command = CK_Insert_Char;
  159. int char_for_insertion = -1;
  160. int i = 0;
  161. static const long *key_map;
  162. switch (edit_key_emulation) {
  163. case EDIT_KEY_EMULATION_NORMAL:
  164. key_map = cooledit_key_map;
  165. break;
  166. case EDIT_KEY_EMULATION_EMACS:
  167. key_map = emacs_key_map;
  168. if (x_key == XCTRL ('x')) {
  169. int ext_key;
  170. ext_key =
  171. edit_raw_key_query (" Ctrl-X ", _(" Emacs key: "), 0);
  172. switch (ext_key) {
  173. case 's':
  174. command = CK_Save;
  175. goto fin;
  176. case 'x':
  177. command = CK_Exit;
  178. goto fin;
  179. case 'k':
  180. command = CK_New;
  181. goto fin;
  182. case 'e':
  183. command =
  184. CK_Macro (edit_raw_key_query
  185. (_(" Execute Macro "),
  186. _(" Press macro hotkey: "), 1));
  187. if (command == CK_Macro (0))
  188. command = CK_Insert_Char;
  189. goto fin;
  190. }
  191. goto fin;
  192. }
  193. break;
  194. }
  195. #ifdef HAVE_CHARSET
  196. if (x_key == XCTRL ('t')) {
  197. do_select_codepage ();
  198. edit->force = REDRAW_COMPLETELY;
  199. command = CK_Refresh;
  200. goto fin;
  201. }
  202. #endif
  203. if (x_key == XCTRL ('q')) {
  204. char_for_insertion =
  205. edit_raw_key_query (_(" Insert Literal "),
  206. _(" Press any key: "), 0);
  207. goto fin;
  208. }
  209. if (x_key == XCTRL ('a')
  210. && edit_key_emulation != EDIT_KEY_EMULATION_EMACS) {
  211. command =
  212. CK_Macro (edit_raw_key_query
  213. (_(" Execute Macro "), _(" Press macro hotkey: "),
  214. 1));
  215. if (command == CK_Macro (0))
  216. command = CK_Insert_Char;
  217. goto fin;
  218. }
  219. /* edit is a pointer to the widget */
  220. if (edit)
  221. if (x_key == XCTRL ('r')) {
  222. command =
  223. edit->macro_i <
  224. 0 ? CK_Begin_Record_Macro : CK_End_Record_Macro;
  225. goto fin;
  226. }
  227. /* an ordinary insertable character */
  228. if (x_key < 256) {
  229. int c = convert_from_input_c (x_key);
  230. if (is_printable (c)) {
  231. char_for_insertion = c;
  232. goto fin;
  233. }
  234. }
  235. /* Commands specific to the key emulation */
  236. i = 0;
  237. while (key_map[i] && (key_map[i] != x_key))
  238. i += 2;
  239. if (key_map[i]) {
  240. command = key_map[i + 1];
  241. goto fin;
  242. }
  243. /* Commands common for the key emulations */
  244. key_map = common_key_map;
  245. i = 0;
  246. while (key_map[i] && (key_map[i] != x_key))
  247. i += 2;
  248. if (key_map[i]) {
  249. command = key_map[i + 1];
  250. goto fin;
  251. }
  252. /* Function still not found for this key, so try macros */
  253. /* This allows the same macro to be
  254. enabled by either eg "ALT('f')" or "XCTRL('f')" or "XCTRL('a'), 'f'" */
  255. if (x_key & ALT (0)) { /* is an alt key ? */
  256. command = CK_Macro (x_key - ALT (0));
  257. goto fin;
  258. }
  259. if (x_key < ' ') { /* is a ctrl key ? */
  260. command = CK_Macro (x_key);
  261. goto fin;
  262. }
  263. fin:
  264. *cmd = command;
  265. *ch = char_for_insertion;
  266. if (command == CK_Insert_Char && char_for_insertion == -1) {
  267. /* unchanged, key has no function here */
  268. return 0;
  269. }
  270. return 1;
  271. }