Add edit_add_window() function.
[midnight-commander.git] / src / editor / editkeys.c
blobd9512909b586bb216c9a8078e340a09094c6828c
1 /*
2 Editor key translation.
4 Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2011, 2012
6 The Free Software Foundation, Inc.
8 Written by:
9 Paul Sheer, 1996, 1997
10 Andrew Borodin <aborodin@vmail.ru> 2012
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file
29 * \brief Source: editor key translation
32 #include <config.h>
34 #include <stdio.h>
35 #include <stdarg.h>
36 #include <sys/types.h>
37 #include <unistd.h>
38 #include <string.h>
39 #include <ctype.h>
40 #include <errno.h>
41 #include <sys/stat.h>
43 #include <stdlib.h>
45 #include "lib/global.h"
46 #include "lib/tty/tty.h" /* keys */
47 #include "lib/tty/key.h" /* KEY_M_SHIFT */
48 #include "lib/strutil.h" /* str_isutf8 () */
49 #include "lib/util.h" /* is_printable() */
50 #include "lib/charsets.h" /* convert_from_input_c() */
52 #include "edit-impl.h"
53 #include "editwidget.h" /* WEdit */
54 #include "editcmd_dialogs.h"
56 #include "src/keybind-defaults.h" /* keybind_lookup_keymap_command() */
58 /*** global variables ****************************************************************************/
60 /*** file scope macro definitions ****************************************************************/
62 /*** file scope type declarations ****************************************************************/
64 /*** file scope variables ************************************************************************/
66 /*** file scope functions ************************************************************************/
67 /* --------------------------------------------------------------------------------------------- */
69 /* --------------------------------------------------------------------------------------------- */
70 /*** public functions ****************************************************************************/
71 /* --------------------------------------------------------------------------------------------- */
74 * Translate the keycode into either 'command' or 'char_for_insertion'.
75 * 'command' is one of the editor commands from cmddef.h.
77 gboolean
78 edit_translate_key (WEdit * edit, long x_key, int *cmd, int *ch)
80 unsigned long command = (unsigned long) CK_InsertChar;
81 int char_for_insertion = -1;
82 int c;
84 /* an ordinary insertable character */
85 if (!edit->extmod && x_key < 256)
87 #ifdef HAVE_CHARSET
88 if (edit->charpoint >= 4)
90 edit->charpoint = 0;
91 edit->charbuf[edit->charpoint] = '\0';
93 if (edit->charpoint < 4)
95 edit->charbuf[edit->charpoint++] = x_key;
96 edit->charbuf[edit->charpoint] = '\0';
99 /* input from 8-bit locale */
100 if (!mc_global.utf8_display)
102 /* source in 8-bit codeset */
103 if (!edit->utf8)
105 #endif /* HAVE_CHARSET */
106 c = convert_from_input_c (x_key);
107 if (is_printable (c))
109 char_for_insertion = c;
110 goto fin;
112 #ifdef HAVE_CHARSET
114 else
116 c = convert_from_input_c (x_key);
117 if (is_printable (c))
119 char_for_insertion = convert_from_8bit_to_utf_c2 ((unsigned char) x_key);
120 goto fin;
123 /* UTF-8 locale */
125 else
127 /* source in UTF-8 codeset */
128 if (edit->utf8)
130 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
131 if (res < 0)
133 if (res != -2)
135 edit->charpoint = 0; /* broken multibyte char, skip */
136 goto fin;
138 char_for_insertion = x_key;
139 goto fin;
141 else
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;
152 /* 8-bit source */
154 else
156 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
157 if (res < 0)
159 if (res != -2)
161 edit->charpoint = 0; /* broken multibyte char, skip */
162 goto fin;
164 /* not finised multibyte input (in meddle multibyte utf-8 char) */
165 goto fin;
167 else
169 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
171 c = convert_from_utf_to_current (edit->charbuf);
172 edit->charbuf[0] = '\0';
173 edit->charpoint = 0;
174 char_for_insertion = c;
175 goto fin;
177 /* unprinteble utf input, skip it */
178 edit->charbuf[0] = '\0';
179 edit->charpoint = 0;
183 #endif /* HAVE_CHARSET */
186 /* Commands specific to the key emulation */
187 if (edit->extmod)
189 edit->extmod = FALSE;
190 command = keybind_lookup_keymap_command (editor_x_map, x_key);
192 else
193 command = keybind_lookup_keymap_command (editor_map, x_key);
195 if (command == CK_IgnoreKey)
196 command = CK_InsertChar;
198 fin:
199 *cmd = (int) command; /* FIXME */
200 *ch = char_for_insertion;
202 return !(command == (unsigned long) CK_InsertChar && char_for_insertion == -1);
205 /* --------------------------------------------------------------------------------------------- */