Renamed keybind-related functions:
[pantumic.git] / src / editor / editkeys.c
blob8eccbe61aed64909bb486d38090cf01a30334ed2
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/cmddef.h" /* list of commands */
54 #include "src/keybind.h" /* keybind_lookup_keymap_command() */
55 #include "src/main.h" /* display_codepage */
57 /*** global variables ****************************************************************************/
59 /*** file scope macro definitions ****************************************************************/
61 /*** file scope type declarations ****************************************************************/
63 /*** file scope variables ************************************************************************/
65 /*** file scope functions ************************************************************************/
66 /* --------------------------------------------------------------------------------------------- */
68 /* --------------------------------------------------------------------------------------------- */
69 /*** public functions ****************************************************************************/
70 /* --------------------------------------------------------------------------------------------- */
73 * Translate the keycode into either 'command' or 'char_for_insertion'.
74 * 'command' is one of the editor commands from cmddef.h.
76 int
77 edit_translate_key (WEdit * edit, long x_key, int *cmd, int *ch)
79 unsigned long command = (unsigned long) CK_Insert_Char;
80 int char_for_insertion = -1;
81 int c;
83 /* an ordinary insertable character */
84 if (x_key < 256)
86 #ifdef HAVE_CHARSET
87 if (edit->charpoint >= 4)
89 edit->charpoint = 0;
90 edit->charbuf[edit->charpoint] = '\0';
92 if (edit->charpoint < 4)
94 edit->charbuf[edit->charpoint++] = x_key;
95 edit->charbuf[edit->charpoint] = '\0';
98 /* input from 8-bit locale */
99 if (!utf8_display)
101 /* source in 8-bit codeset */
102 if (!edit->utf8)
104 #endif /* HAVE_CHARSET */
105 c = convert_from_input_c (x_key);
106 if (is_printable (c))
108 char_for_insertion = c;
109 goto fin;
111 #ifdef HAVE_CHARSET
113 else
115 c = convert_from_input_c (x_key);
116 if (is_printable (c))
118 char_for_insertion = convert_from_8bit_to_utf_c2 ((unsigned char) x_key);
119 goto fin;
122 /* UTF-8 locale */
124 else
126 /* source in UTF-8 codeset */
127 if (edit->utf8)
129 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
130 if (res < 0)
132 if (res != -2)
134 edit->charpoint = 0; /* broken multibyte char, skip */
135 goto fin;
137 char_for_insertion = x_key;
138 goto fin;
140 else
142 edit->charbuf[edit->charpoint] = '\0';
143 edit->charpoint = 0;
144 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
146 char_for_insertion = x_key;
147 goto fin;
151 /* 8-bit source */
153 else
155 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
156 if (res < 0)
158 if (res != -2)
160 edit->charpoint = 0; /* broken multibyte char, skip */
161 goto fin;
163 /* not finised multibyte input (in meddle multibyte utf-8 char) */
164 goto fin;
166 else
168 if (g_unichar_isprint (g_utf8_get_char (edit->charbuf)))
170 c = convert_from_utf_to_current (edit->charbuf);
171 edit->charbuf[0] = '\0';
172 edit->charpoint = 0;
173 char_for_insertion = c;
174 goto fin;
176 /* unprinteble utf input, skip it */
177 edit->charbuf[0] = '\0';
178 edit->charpoint = 0;
182 #endif /* HAVE_CHARSET */
185 /* Commands specific to the key emulation */
186 if (edit->extmod)
188 edit->extmod = 0;
189 command = keybind_lookup_keymap_command (editor_x_map, x_key);
191 else
192 command = keybind_lookup_keymap_command (editor_map, x_key);
194 if (command == CK_Ignore_Key)
195 command = CK_Insert_Char;
197 fin:
198 *cmd = (int) command; /* FIXME */
199 *ch = char_for_insertion;
201 return (command == (unsigned long) CK_Insert_Char && char_for_insertion == -1) ? 0 : 1;
204 /* --------------------------------------------------------------------------------------------- */