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
25 * \brief Source: editor key translation
33 #include <sys/types.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" /* WEdit */
51 #include "editcmd_dialogs.h"
53 #include "src/keybind-defaults.h" /* keybind_lookup_keymap_command() */
55 /*** global variables ****************************************************************************/
57 /*** file scope macro definitions ****************************************************************/
59 /*** file scope type declarations ****************************************************************/
61 /*** file scope variables ************************************************************************/
63 /*** file scope functions ************************************************************************/
64 /* --------------------------------------------------------------------------------------------- */
66 /* --------------------------------------------------------------------------------------------- */
67 /*** public functions ****************************************************************************/
68 /* --------------------------------------------------------------------------------------------- */
71 * Translate the keycode into either 'command' or 'char_for_insertion'.
72 * 'command' is one of the editor commands from cmddef.h.
75 edit_translate_key (WEdit
* edit
, long x_key
, int *cmd
, int *ch
)
77 unsigned long command
= (unsigned long) CK_InsertChar
;
78 int char_for_insertion
= -1;
81 /* an ordinary insertable character */
82 if (!edit
->extmod
&& x_key
< 256)
85 if (edit
->charpoint
>= 4)
88 edit
->charbuf
[edit
->charpoint
] = '\0';
90 if (edit
->charpoint
< 4)
92 edit
->charbuf
[edit
->charpoint
++] = x_key
;
93 edit
->charbuf
[edit
->charpoint
] = '\0';
96 /* input from 8-bit locale */
97 if (!mc_global
.utf8_display
)
99 /* source in 8-bit codeset */
102 #endif /* HAVE_CHARSET */
103 c
= convert_from_input_c (x_key
);
104 if (is_printable (c
))
106 char_for_insertion
= c
;
113 c
= convert_from_input_c (x_key
);
114 if (is_printable (c
))
116 char_for_insertion
= convert_from_8bit_to_utf_c2 ((unsigned char) x_key
);
124 /* source in UTF-8 codeset */
127 int res
= str_is_valid_char (edit
->charbuf
, edit
->charpoint
);
132 edit
->charpoint
= 0; /* broken multibyte char, skip */
135 char_for_insertion
= x_key
;
140 edit
->charbuf
[edit
->charpoint
] = '\0';
142 if (g_unichar_isprint (g_utf8_get_char (edit
->charbuf
)))
144 char_for_insertion
= x_key
;
153 int res
= str_is_valid_char (edit
->charbuf
, edit
->charpoint
);
158 edit
->charpoint
= 0; /* broken multibyte char, skip */
161 /* not finised multibyte input (in meddle multibyte utf-8 char) */
166 if (g_unichar_isprint (g_utf8_get_char (edit
->charbuf
)))
168 c
= convert_from_utf_to_current (edit
->charbuf
);
169 edit
->charbuf
[0] = '\0';
171 char_for_insertion
= c
;
174 /* unprinteble utf input, skip it */
175 edit
->charbuf
[0] = '\0';
180 #endif /* HAVE_CHARSET */
183 /* Commands specific to the key emulation */
186 edit
->extmod
= FALSE
;
187 command
= keybind_lookup_keymap_command (editor_x_map
, x_key
);
190 command
= keybind_lookup_keymap_command (editor_map
, x_key
);
192 if (command
== CK_IgnoreKey
)
193 command
= CK_InsertChar
;
196 *cmd
= (int) command
; /* FIXME */
197 *ch
= char_for_insertion
;
199 return (command
== (unsigned long) CK_InsertChar
&& char_for_insertion
== -1) ? 0 : 1;
202 /* --------------------------------------------------------------------------------------------- */