extfs helpers: Replace all 'gawk' occurences with @AWK@ meta variable.
[midnight-commander.git] / src / editor / editkeys.c
blob81c211d401d53e0cac7902cbe74066da9b8236af
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 /** \file
25 * \brief Source: editor key translation
28 #include <config.h>
30 #include <assert.h>
31 #include <stdio.h>
32 #include <stdarg.h>
33 #include <sys/types.h>
34 #include <unistd.h>
35 #include <string.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <sys/stat.h>
40 #include <stdlib.h>
42 #include "lib/global.h"
43 #include "lib/tty/tty.h" /* keys */
44 #include "lib/tty/key.h" /* KEY_M_SHIFT */
45 #include "lib/strutil.h" /* str_isutf8 () */
46 #include "lib/util.h" /* is_printable() */
47 #include "lib/charsets.h" /* convert_from_input_c() */
49 #include "edit-impl.h"
50 #include "edit-widget.h" /* edit->macro_i */
51 #include "editcmd_dialogs.h"
53 #include "src/keybind-defaults.h" /* keybind_lookup_keymap_command() */
54 #include "src/main.h" /* display_codepage */
56 /*** global variables ****************************************************************************/
58 /*** file scope macro definitions ****************************************************************/
60 /*** file scope type declarations ****************************************************************/
62 /*** file scope variables ************************************************************************/
64 /*** file scope functions ************************************************************************/
65 /* --------------------------------------------------------------------------------------------- */
67 /* --------------------------------------------------------------------------------------------- */
68 /*** public functions ****************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
72 * Translate the keycode into either 'command' or 'char_for_insertion'.
73 * 'command' is one of the editor commands from cmddef.h.
75 int
76 edit_translate_key (WEdit * edit, long x_key, int *cmd, int *ch)
78 unsigned long command = (unsigned long) CK_Insert_Char;
79 int char_for_insertion = -1;
80 int c;
82 /* an ordinary insertable character */
83 if (x_key < 256)
85 #ifdef HAVE_CHARSET
86 if (edit->charpoint >= 4)
88 edit->charpoint = 0;
89 edit->charbuf[edit->charpoint] = '\0';
91 if (edit->charpoint < 4)
93 edit->charbuf[edit->charpoint++] = x_key;
94 edit->charbuf[edit->charpoint] = '\0';
97 /* input from 8-bit locale */
98 if (!utf8_display)
100 /* source in 8-bit codeset */
101 if (!edit->utf8)
103 #endif /* HAVE_CHARSET */
104 c = convert_from_input_c (x_key);
105 if (is_printable (c))
107 char_for_insertion = c;
108 goto fin;
110 #ifdef HAVE_CHARSET
112 else
114 c = convert_from_input_c (x_key);
115 if (is_printable (c))
117 char_for_insertion = convert_from_8bit_to_utf_c2 ((unsigned char) x_key);
118 goto fin;
121 /* UTF-8 locale */
123 else
125 /* source in UTF-8 codeset */
126 if (edit->utf8)
128 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
129 if (res < 0)
131 if (res != -2)
133 edit->charpoint = 0; /* broken multibyte char, skip */
134 goto fin;
136 char_for_insertion = x_key;
137 goto fin;
139 else
141 edit->charbuf[edit->charpoint] = '\0';
142 edit->charpoint = 0;
143 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
145 char_for_insertion = x_key;
146 goto fin;
150 /* 8-bit source */
152 else
154 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
155 if (res < 0)
157 if (res != -2)
159 edit->charpoint = 0; /* broken multibyte char, skip */
160 goto fin;
162 /* not finised multibyte input (in meddle multibyte utf-8 char) */
163 goto fin;
165 else
167 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
169 c = convert_from_utf_to_current (edit->charbuf);
170 edit->charbuf[0] = '\0';
171 edit->charpoint = 0;
172 char_for_insertion = c;
173 goto fin;
175 /* unprinteble utf input, skip it */
176 edit->charbuf[0] = '\0';
177 edit->charpoint = 0;
181 #endif /* HAVE_CHARSET */
184 /* Commands specific to the key emulation */
185 if (edit->extmod)
187 edit->extmod = 0;
188 command = keybind_lookup_keymap_command (editor_x_map, x_key);
190 else
191 command = keybind_lookup_keymap_command (editor_map, x_key);
193 if (command == CK_Ignore_Key)
194 command = CK_Insert_Char;
196 fin:
197 *cmd = (int) command; /* FIXME */
198 *ch = char_for_insertion;
200 return (command == (unsigned long) CK_Insert_Char && char_for_insertion == -1) ? 0 : 1;
203 /* --------------------------------------------------------------------------------------------- */