Merge branch '1904_spec_bump_epoch'
[midnight-commander.git] / edit / editkeys.c
blob4d7ee6779f8be6e92fe38be31603388c7077af4f
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 "../src/global.h"
44 #include "edit-impl.h"
45 #include "edit-widget.h" /* edit->macro_i */
46 #include "editcmd_dialogs.h"
48 #include "../src/tty/tty.h" /* keys */
49 #include "../src/tty/key.h" /* KEY_M_SHIFT */
51 #include "../src/cmddef.h" /* list of commands */
52 #include "../src/keybind.h" /* lookup_keymap_command() */
53 #include "../src/charsets.h" /* convert_from_input_c() */
54 #include "../src/main.h" /* display_codepage */
55 #include "../src/strutil.h" /* str_isutf8 () */
58 * Translate the keycode into either 'command' or 'char_for_insertion'.
59 * 'command' is one of the editor commands from cmddef.h.
61 int
62 edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch)
64 unsigned long command = (unsigned long) CK_Insert_Char;
65 int char_for_insertion = -1;
66 int c;
68 /* an ordinary insertable character */
69 if (x_key < 256) {
70 #ifdef HAVE_CHARSET
71 if ( edit->charpoint >= 4 ) {
72 edit->charpoint = 0;
73 edit->charbuf[edit->charpoint] = '\0';
75 if ( edit->charpoint < 4 ) {
76 edit->charbuf[edit->charpoint++] = x_key;
77 edit->charbuf[edit->charpoint] = '\0';
80 /* input from 8-bit locale */
81 if ( !utf8_display ) {
82 /* source in 8-bit codeset */
83 if (!edit->utf8) {
84 #endif /* HAVE_CHARSET */
85 c = convert_from_input_c (x_key);
86 if (is_printable (c)) {
87 char_for_insertion = c;
88 goto fin;
90 #ifdef HAVE_CHARSET
91 } else {
92 c = convert_from_input_c (x_key);
93 if (is_printable (c)) {
94 char_for_insertion = convert_from_8bit_to_utf_c2((unsigned char) x_key);
95 goto fin;
98 /* UTF-8 locale */
99 } else {
100 /* source in UTF-8 codeset */
101 if ( edit->utf8 ) {
102 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
103 if (res < 0) {
104 if (res != -2) {
105 edit->charpoint = 0; /* broken multibyte char, skip */
106 goto fin;
108 char_for_insertion = x_key;
109 goto fin;
110 } else {
111 edit->charbuf[edit->charpoint]='\0';
112 edit->charpoint = 0;
113 if ( g_unichar_isprint (g_utf8_get_char(edit->charbuf))) {
114 char_for_insertion = x_key;
115 goto fin;
119 /* 8-bit source */
120 } else {
121 int res = str_is_valid_char (edit->charbuf, edit->charpoint);
122 if (res < 0) {
123 if (res != -2) {
124 edit->charpoint = 0; /* broken multibyte char, skip */
125 goto fin;
127 /* not finised multibyte input (in meddle multibyte utf-8 char) */
128 goto fin;
129 } else {
130 if ( g_unichar_isprint (g_utf8_get_char(edit->charbuf)) ) {
131 c = convert_from_utf_to_current ( edit->charbuf );
132 edit->charbuf[0] = '\0';
133 edit->charpoint = 0;
134 char_for_insertion = c;
135 goto fin;
137 /* unprinteble utf input, skip it */
138 edit->charbuf[0] = '\0';
139 edit->charpoint = 0;
143 #endif /* HAVE_CHARSET */
146 /* Commands specific to the key emulation */
147 if (edit->extmod) {
148 edit->extmod = 0;
149 command = lookup_keymap_command (editor_x_map, x_key);
150 } else
151 command = lookup_keymap_command (editor_map, x_key);
153 if (command == CK_Ignore_Key)
154 command = CK_Insert_Char;
156 fin:
157 *cmd = (int) command; /* FIXME */
158 *ch = char_for_insertion;
160 return (command == (unsigned long) CK_Insert_Char && char_for_insertion == -1) ? 0 : 1;