Update po/mc.pot and po/*.po files.
[midnight-commander.git] / src / editor / editkeys.c
blob45cacec1a029ddfdfd68a6b08dec112fbb8d00b4
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 #ifdef HAVE_CHARSET
51 #include "lib/charsets.h"
52 #endif
54 #include "edit-impl.h"
55 #include "editwidget.h" /* WEdit */
56 #include "editcmd_dialogs.h"
58 #include "src/keybind-defaults.h" /* keybind_lookup_keymap_command() */
60 /*** global variables ****************************************************************************/
62 /*** file scope macro definitions ****************************************************************/
64 /*** file scope type declarations ****************************************************************/
66 /*** file scope variables ************************************************************************/
68 /*** file scope functions ************************************************************************/
69 /* --------------------------------------------------------------------------------------------- */
71 /* --------------------------------------------------------------------------------------------- */
72 /*** public functions ****************************************************************************/
73 /* --------------------------------------------------------------------------------------------- */
76 * Translate the keycode into either 'command' or 'char_for_insertion'.
77 * 'command' is one of the editor commands from cmddef.h.
79 gboolean
80 edit_translate_key (WEdit * edit, long x_key, int *cmd, int *ch)
82 unsigned long command = (unsigned long) CK_InsertChar;
83 int char_for_insertion = -1;
85 /* an ordinary insertable character */
86 if (!edit->extmod && x_key < 256)
88 #ifndef HAVE_CHARSET
89 if (is_printable (x_key))
91 char_for_insertion = x_key;
92 goto fin;
94 #else
95 int c;
97 if (edit->charpoint >= 4)
99 edit->charpoint = 0;
100 edit->charbuf[edit->charpoint] = '\0';
102 if (edit->charpoint < 4)
104 edit->charbuf[edit->charpoint++] = x_key;
105 edit->charbuf[edit->charpoint] = '\0';
108 /* input from 8-bit locale */
109 if (!mc_global.utf8_display)
111 /* source in 8-bit codeset */
112 c = convert_from_input_c (x_key);
114 if (is_printable (c))
116 if (!edit->utf8)
117 char_for_insertion = c;
118 else
119 char_for_insertion = convert_from_8bit_to_utf_c2 ((unsigned char) x_key);
120 goto fin;
123 else
125 /* UTF-8 locale */
126 int res;
128 res = str_is_valid_char (edit->charbuf, edit->charpoint);
129 if (res < 0 && res != -2)
131 edit->charpoint = 0; /* broken multibyte char, skip */
132 goto fin;
135 if (edit->utf8)
137 /* source in UTF-8 codeset */
138 if (res < 0)
140 char_for_insertion = x_key;
141 goto fin;
144 edit->charbuf[edit->charpoint] = '\0';
145 edit->charpoint = 0;
146 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
148 char_for_insertion = x_key;
149 goto fin;
152 else
154 /* 8-bit source */
155 if (res < 0)
157 /* not finised multibyte input (in meddle multibyte utf-8 char) */
158 goto fin;
161 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
163 c = convert_from_utf_to_current (edit->charbuf);
164 edit->charbuf[0] = '\0';
165 edit->charpoint = 0;
166 char_for_insertion = c;
167 goto fin;
170 /* unprinteble utf input, skip it */
171 edit->charbuf[0] = '\0';
172 edit->charpoint = 0;
175 #endif /* HAVE_CHARSET */
178 /* Commands specific to the key emulation */
179 if (edit->extmod)
181 edit->extmod = FALSE;
182 command = keybind_lookup_keymap_command (editor_x_map, x_key);
184 else
185 command = keybind_lookup_keymap_command (editor_map, x_key);
187 if (command == CK_IgnoreKey)
188 command = CK_InsertChar;
190 fin:
191 *cmd = (int) command; /* FIXME */
192 *ch = char_for_insertion;
194 return !(command == (unsigned long) CK_InsertChar && char_for_insertion == -1);
197 /* --------------------------------------------------------------------------------------------- */