fix edit_delete, edit_backspace now work with utf-8
[free-mc.git] / edit / editkeys.c
blob503e7cda46b59ea3ca4b7269124b5ef886af9ad5
1 /* Editor key translation.
3 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 2005, 2006, 2007 Free Software Foundation, Inc.
6 Authors: 1996, 1997 Paul Sheer
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 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, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21 02110-1301, USA.
24 #include <config.h>
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdarg.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31 #include <string.h>
32 #include <ctype.h>
33 #include <errno.h>
34 #include <sys/stat.h>
36 #include <stdlib.h>
38 #include "../src/global.h"
40 #include "edit.h"
41 #include "edit-widget.h" /* edit->macro_i */
42 #include "editcmddef.h" /* list of commands */
43 #include "../src/key.h" /* KEY_M_SHIFT */
44 #include "../src/tty.h" /* keys */
45 #include "../src/charsets.h" /* convert_from_input_c() */
46 #include "../src/selcodepage.h" /* do_select_codepage() */
49 * Ordinary translations. Note that the keys listed first take priority
50 * when the key is assigned to more than one command.
52 static const edit_key_map_type cooledit_key_map[] = {
53 { ALT ('b'), CK_Match_Bracket },
54 { ALT ('m'), CK_Mail },
55 { XCTRL ('f'), CK_Save_Block },
56 { XCTRL ('n'), CK_New },
57 { XCTRL ('p'), CK_Pipe_Block (1) }, /* spell check */
58 { XCTRL ('x'), CK_Word_Right },
59 { XCTRL ('y'), CK_Delete_Line },
60 { XCTRL ('z'), CK_Word_Left },
61 { 0, 0 }
64 static const edit_key_map_type emacs_key_map[] = {
65 { ALT ('$'), CK_Pipe_Block (1) }, /* spell check */
66 { ALT ('b'), CK_Word_Left },
67 { ALT ('f'), CK_Word_Right },
68 { ALT ('v'), CK_Page_Up },
69 { ALT ('w'), CK_XStore },
70 { XCTRL ('@'), CK_Mark },
71 { XCTRL ('a'), CK_Home },
72 { XCTRL ('b'), CK_Left },
73 { XCTRL ('e'), CK_End },
74 { XCTRL ('f'), CK_Right },
75 { XCTRL ('g'), CK_Ignore_Key },
76 { XCTRL ('n'), CK_Down },
77 { XCTRL ('p'), CK_Up },
78 { XCTRL ('s'), CK_Find },
79 { XCTRL ('v'), CK_Page_Down },
80 { XCTRL ('w'), CK_XCut },
81 { XCTRL ('y'), CK_XPaste },
82 { 0, 0 }
85 static const edit_key_map_type common_key_map[] = {
86 { '\n', CK_Enter },
87 { '\t', CK_Tab },
89 { ESC_CHAR, CK_Exit },
90 { KEY_BACKSPACE, CK_BackSpace },
91 { KEY_DC, CK_Delete },
92 { KEY_DOWN, CK_Down },
93 { KEY_END, CK_End },
94 { KEY_HOME, CK_Home },
95 { KEY_IC, CK_Toggle_Insert },
96 { KEY_LEFT, CK_Left },
97 { KEY_NPAGE, CK_Page_Down },
98 { KEY_PPAGE, CK_Page_Up },
99 { KEY_RIGHT, CK_Right },
100 { KEY_UP, CK_Up },
102 { ALT ('\n'), CK_Find_Definition },
103 { ALT ('\t'), CK_Complete_Word },
104 { ALT ('l'), CK_Goto },
105 { ALT ('L'), CK_Goto },
106 { ALT ('p'), CK_Paragraph_Format },
107 { ALT ('t'), CK_Sort },
108 { ALT ('u'), CK_ExtCmd },
109 { ALT ('<'), CK_Beginning_Of_Text },
110 { ALT ('>'), CK_End_Of_Text },
111 { ALT ('-'), CK_Load_Prev_File },
112 { ALT ('='), CK_Load_Next_File },
113 { ALT (KEY_BACKSPACE), CK_Delete_Word_Left },
115 { XCTRL ('k'), CK_Delete_To_Line_End },
116 { XCTRL ('l'), CK_Refresh },
117 { XCTRL ('o'), CK_Shell },
118 { XCTRL ('s'), CK_Toggle_Syntax },
119 { XCTRL ('u'), CK_Undo },
120 { XCTRL ('t'), CK_Select_Codepage },
121 { XCTRL ('q'), CK_Insert_Literal },
122 { XCTRL ('a'), CK_Execute_Macro },
123 { XCTRL ('r'), CK_Begin_End_Macro },
125 { KEY_F (1), CK_Help },
126 { KEY_F (2), CK_Save },
127 { KEY_F (3), CK_Mark },
128 { KEY_F (4), CK_Replace },
129 { KEY_F (5), CK_Copy },
130 { KEY_F (6), CK_Move },
131 { KEY_F (7), CK_Find },
132 { KEY_F (8), CK_Remove },
133 { KEY_F (10), CK_Exit },
134 { KEY_F (11), CK_User_Menu }, /* edit user menu */
135 { KEY_F (12), CK_Save_As },
136 { KEY_F (13), CK_Column_Mark },
137 { KEY_F (14), CK_Replace_Again },
138 { KEY_F (15), CK_Insert_File },
139 { KEY_F (17), CK_Find_Again },
140 { KEY_F (19), CK_Pipe_Block (0) }, /* C formatter */
142 /* Shift */
143 { KEY_M_SHIFT | KEY_PPAGE, CK_Page_Up_Highlight },
144 { KEY_M_SHIFT | KEY_NPAGE, CK_Page_Down_Highlight },
145 { KEY_M_SHIFT | KEY_LEFT, CK_Left_Highlight },
146 { KEY_M_SHIFT | KEY_RIGHT, CK_Right_Highlight },
147 { KEY_M_SHIFT | KEY_UP, CK_Up_Highlight },
148 { KEY_M_SHIFT | KEY_DOWN, CK_Down_Highlight },
149 { KEY_M_SHIFT | KEY_HOME, CK_Home_Highlight },
150 { KEY_M_SHIFT | KEY_END, CK_End_Highlight },
151 { KEY_M_SHIFT | KEY_IC, CK_XPaste },
152 { KEY_M_SHIFT | KEY_DC, CK_XCut },
153 { KEY_M_SHIFT | '\n', CK_Return }, /* useful for pasting multiline text */
155 /* Ctrl */
156 { KEY_M_CTRL | (KEY_F (2)), CK_Save_As },
157 { KEY_M_CTRL | (KEY_F (4)), CK_Replace_Again },
158 { KEY_M_CTRL | (KEY_F (7)), CK_Find_Again },
159 { KEY_M_CTRL | KEY_BACKSPACE, CK_Undo },
160 { KEY_M_CTRL | KEY_PPAGE, CK_Beginning_Of_Text },
161 { KEY_M_CTRL | KEY_NPAGE, CK_End_Of_Text },
162 { KEY_M_CTRL | KEY_HOME, CK_Beginning_Of_Text },
163 { KEY_M_CTRL | KEY_END, CK_End_Of_Text },
164 { KEY_M_CTRL | KEY_UP, CK_Scroll_Up },
165 { KEY_M_CTRL | KEY_DOWN, CK_Scroll_Down },
166 { KEY_M_CTRL | KEY_LEFT, CK_Word_Left },
167 { KEY_M_CTRL | KEY_RIGHT, CK_Word_Right },
168 { KEY_M_CTRL | KEY_IC, CK_XStore },
169 { KEY_M_CTRL | KEY_DC, CK_Remove },
171 /* Ctrl + Shift */
172 { KEY_M_SHIFT | KEY_M_CTRL | KEY_PPAGE, CK_Beginning_Of_Text_Highlight },
173 { KEY_M_SHIFT | KEY_M_CTRL | KEY_NPAGE, CK_End_Of_Text_Highlight },
174 { KEY_M_SHIFT | KEY_M_CTRL | KEY_LEFT, CK_Word_Left_Highlight },
175 { KEY_M_SHIFT | KEY_M_CTRL | KEY_RIGHT, CK_Word_Right_Highlight },
176 { KEY_M_SHIFT | KEY_M_CTRL | KEY_UP, CK_Scroll_Up_Highlight },
177 { KEY_M_SHIFT | KEY_M_CTRL | KEY_DOWN, CK_Scroll_Down_Highlight },
179 { 0, 0 }
184 * Translate the keycode into either 'command' or 'char_for_insertion'.
185 * 'command' is one of the editor commands from editcmddef.h.
188 edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch)
190 int command = CK_Insert_Char;
191 int char_for_insertion = -1;
192 int i = 0;
193 int extmod = 0;
194 const edit_key_map_type *key_map = NULL;
195 switch (edit_key_emulation) {
196 case EDIT_KEY_EMULATION_NORMAL:
197 key_map = cooledit_key_map;
198 break;
199 case EDIT_KEY_EMULATION_EMACS:
200 key_map = emacs_key_map;
201 if (x_key == XCTRL ('x')) {
202 int ext_key;
203 ext_key =
204 edit_raw_key_query (" Ctrl-X ", _(" Emacs key: "), 0);
205 switch (ext_key) {
206 case 's':
207 command = CK_Save;
208 goto fin;
209 case 'x':
210 command = CK_Exit;
211 goto fin;
212 case 'k':
213 command = CK_New;
214 goto fin;
215 case 'e':
216 command =
217 CK_Macro (edit_raw_key_query
218 (_(" Execute Macro "),
219 _(" Press macro hotkey: "), 1));
220 if (command == CK_Macro (0))
221 command = CK_Insert_Char;
222 goto fin;
224 goto fin;
226 break;
228 case EDIT_KEY_EMULATION_USER:
229 if (edit->user_map != NULL) {
230 if (edit->extmod && edit->ext_map != NULL) {
231 key_map = edit->ext_map;
232 extmod = 1;
233 } else {
234 key_map = edit->user_map;
236 edit->extmod = 0;
237 } else {
238 key_map = edit->user_map = cooledit_key_map;
240 break;
242 assert (key_map != NULL);
244 /* an ordinary insertable character */
245 if (x_key < 256 && !extmod) {
246 if (!edit->utf8) {
247 int c = convert_from_input_c (x_key);
248 if (is_printable (c)) {
249 char_for_insertion = c;
250 goto fin;
252 } else {
253 if (edit->charpoint >= MB_LEN_MAX) {
254 goto fin;
255 edit->charpoint = 0;
258 edit->charbuf[edit->charpoint] = x_key;
259 edit->charpoint++;
261 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
262 mc_log("res:%i, edit->charpoint : %i\n",res, edit->charpoint);
263 if (res < 0) {
264 if (res != -2) {
265 edit->charpoint = 0; /* broken multibyte char, skip */
266 goto fin;
268 char_for_insertion = x_key;
269 goto fin;
270 } else {
271 edit->charbuf[edit->charpoint]='\0';
272 edit->charpoint = 0;
273 if ( g_unichar_isprint (g_utf8_get_char(edit->charbuf))) {
274 char_for_insertion = x_key;
275 goto fin;
278 //char_for_insertion = x_key;
279 //goto fin;
283 /* Commands specific to the key emulation */
284 for (i = 0; key_map[i].key != 0 && key_map[i].key != x_key; i++)
285 continue;
286 if (key_map[i].key != 0) {
287 command = key_map[i].command;
288 goto fin;
291 /* Commands common for the key emulations */
292 key_map = common_key_map;
293 for (i = 0; key_map[i].key != 0 && key_map[i].key != x_key; i++)
294 continue;
295 if (key_map[i].key != 0) {
296 command = key_map[i].command;
297 goto fin;
300 /* Function still not found for this key, so try macros */
301 /* This allows the same macro to be
302 enabled by either eg "ALT('f')" or "XCTRL('f')" or "XCTRL('a'), 'f'" */
304 if (x_key & ALT (0)) { /* is an alt key ? */
305 command = CK_Macro (x_key - ALT (0));
306 goto fin;
308 if (x_key < ' ') { /* is a ctrl key ? */
309 command = CK_Macro (x_key);
310 goto fin;
312 fin:
314 *cmd = command;
315 *ch = char_for_insertion;
317 if (command == CK_Insert_Char && char_for_insertion == -1) {
318 /* unchanged, key has no function here */
319 return 0;
322 return 1;