Ticket #2846: code cleanup before 4.8.1.4 release.
[midnight-commander.git] / src / editor / editkeys.c
blob46f33ec956e8259ff99cda3bb1d32c5a46f4b970
1 /*
2 Editor key translation.
4 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file
28 * \brief Source: editor key translation
31 #include <config.h>
33 #include <stdio.h>
34 #include <stdarg.h>
35 #include <sys/types.h>
36 #include <unistd.h>
37 #include <string.h>
38 #include <ctype.h>
39 #include <errno.h>
40 #include <sys/stat.h>
42 #include <stdlib.h>
44 #include "lib/global.h"
45 #include "lib/tty/tty.h" /* keys */
46 #include "lib/tty/key.h" /* KEY_M_SHIFT */
47 #include "lib/strutil.h" /* str_isutf8 () */
48 #include "lib/util.h" /* is_printable() */
49 #ifdef HAVE_CHARSET
50 #include "lib/charsets.h"
51 #endif
53 #include "edit-impl.h"
54 #include "editwidget.h" /* WEdit */
55 #include "editcmd_dialogs.h"
57 #include "src/keybind-defaults.h" /* keybind_lookup_keymap_command() */
59 /*** global variables ****************************************************************************/
61 /*** file scope macro definitions ****************************************************************/
63 /*** file scope type declarations ****************************************************************/
65 /*** file scope variables ************************************************************************/
67 /*** file scope functions ************************************************************************/
68 /* --------------------------------------------------------------------------------------------- */
70 /* --------------------------------------------------------------------------------------------- */
71 /*** public functions ****************************************************************************/
72 /* --------------------------------------------------------------------------------------------- */
75 * Translate the keycode into either 'command' or 'char_for_insertion'.
76 * 'command' is one of the editor commands from cmddef.h.
78 int
79 edit_translate_key (WEdit * edit, long x_key, int *cmd, int *ch)
81 unsigned long command = (unsigned long) CK_InsertChar;
82 int char_for_insertion = -1;
84 /* an ordinary insertable character */
85 if (!edit->extmod && x_key < 256)
87 #ifndef HAVE_CHARSET
88 if (is_printable (x_key))
90 char_for_insertion = x_key;
91 goto fin;
93 #else
94 int c;
96 if (edit->charpoint >= 4)
98 edit->charpoint = 0;
99 edit->charbuf[edit->charpoint] = '\0';
101 if (edit->charpoint < 4)
103 edit->charbuf[edit->charpoint++] = x_key;
104 edit->charbuf[edit->charpoint] = '\0';
107 /* input from 8-bit locale */
108 if (!mc_global.utf8_display)
110 /* source in 8-bit codeset */
111 c = convert_from_input_c (x_key);
113 if (is_printable (c))
115 if (!edit->utf8)
116 char_for_insertion = c;
117 else
118 char_for_insertion = convert_from_8bit_to_utf_c2 ((unsigned char) x_key);
119 goto fin;
122 else
124 /* UTF-8 locale */
125 int res;
127 res = str_is_valid_char (edit->charbuf, edit->charpoint);
128 if (res < 0 && res != -2)
130 edit->charpoint = 0; /* broken multibyte char, skip */
131 goto fin;
134 if (edit->utf8)
136 /* source in UTF-8 codeset */
137 if (res < 0)
139 char_for_insertion = x_key;
140 goto fin;
143 edit->charbuf[edit->charpoint] = '\0';
144 edit->charpoint = 0;
145 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
147 char_for_insertion = x_key;
148 goto fin;
151 else
153 /* 8-bit source */
154 if (res < 0)
156 /* not finised multibyte input (in meddle multibyte utf-8 char) */
157 goto fin;
160 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
162 c = convert_from_utf_to_current (edit->charbuf);
163 edit->charbuf[0] = '\0';
164 edit->charpoint = 0;
165 char_for_insertion = c;
166 goto fin;
169 /* unprinteble utf input, skip it */
170 edit->charbuf[0] = '\0';
171 edit->charpoint = 0;
174 #endif /* HAVE_CHARSET */
177 /* Commands specific to the key emulation */
178 if (edit->extmod)
180 edit->extmod = FALSE;
181 command = keybind_lookup_keymap_command (editor_x_map, x_key);
183 else
184 command = keybind_lookup_keymap_command (editor_map, x_key);
186 if (command == CK_IgnoreKey)
187 command = CK_InsertChar;
189 fin:
190 *cmd = (int) command; /* FIXME */
191 *ch = char_for_insertion;
193 return (command == (unsigned long) CK_InsertChar && char_for_insertion == -1) ? 0 : 1;
196 /* --------------------------------------------------------------------------------------------- */