editkeys.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /* Editor key translation.
  2. Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
  3. 2005, 2006, 2007 Free Software Foundation, Inc.
  4. Authors: 1996, 1997 Paul Sheer
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 2 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. 02110-1301, USA.
  17. */
  18. /** \file
  19. * \brief Source: editor key translation
  20. */
  21. #include <config.h>
  22. #include <assert.h>
  23. #include <stdio.h>
  24. #include <stdarg.h>
  25. #include <sys/types.h>
  26. #include <unistd.h>
  27. #include <string.h>
  28. #include <ctype.h>
  29. #include <errno.h>
  30. #include <sys/stat.h>
  31. #include <stdlib.h>
  32. #include "../src/global.h"
  33. #include "edit-impl.h"
  34. #include "edit-widget.h" /* edit->macro_i */
  35. #include "editcmd_dialogs.h"
  36. #include "../src/tty/tty.h" /* keys */
  37. #include "../src/tty/key.h" /* KEY_M_SHIFT */
  38. #include "../src/cmddef.h" /* list of commands */
  39. #include "../src/keybind.h" /* lookup_keymap_command() */
  40. #include "../src/charsets.h" /* convert_from_input_c() */
  41. #include "../src/main.h" /* display_codepage */
  42. #include "../src/strutil.h" /* str_isutf8 () */
  43. /*
  44. * Translate the keycode into either 'command' or 'char_for_insertion'.
  45. * 'command' is one of the editor commands from cmddef.h.
  46. */
  47. int
  48. edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch)
  49. {
  50. unsigned long command = (unsigned long) CK_Insert_Char;
  51. int char_for_insertion = -1;
  52. int c;
  53. /* an ordinary insertable character */
  54. if (x_key < 256) {
  55. #ifdef HAVE_CHARSET
  56. if ( edit->charpoint >= 4 ) {
  57. edit->charpoint = 0;
  58. edit->charbuf[edit->charpoint] = '\0';
  59. }
  60. if ( edit->charpoint < 4 ) {
  61. edit->charbuf[edit->charpoint++] = x_key;
  62. edit->charbuf[edit->charpoint] = '\0';
  63. }
  64. /* input from 8-bit locale */
  65. if ( !utf8_display ) {
  66. /* source in 8-bit codeset */
  67. if (!edit->utf8) {
  68. #endif /* HAVE_CHARSET */
  69. c = convert_from_input_c (x_key);
  70. if (is_printable (c)) {
  71. char_for_insertion = c;
  72. goto fin;
  73. }
  74. #ifdef HAVE_CHARSET
  75. } else {
  76. c = convert_from_input_c (x_key);
  77. if (is_printable (c)) {
  78. char_for_insertion = convert_from_8bit_to_utf_c2((unsigned char) x_key);
  79. goto fin;
  80. }
  81. }
  82. /* UTF-8 locale */
  83. } else {
  84. /* source in UTF-8 codeset */
  85. if ( edit->utf8 ) {
  86. int res = str_is_valid_char (edit->charbuf, edit->charpoint);
  87. if (res < 0) {
  88. if (res != -2) {
  89. edit->charpoint = 0; /* broken multibyte char, skip */
  90. goto fin;
  91. }
  92. char_for_insertion = x_key;
  93. goto fin;
  94. } else {
  95. edit->charbuf[edit->charpoint]='\0';
  96. edit->charpoint = 0;
  97. if ( g_unichar_isprint (g_utf8_get_char(edit->charbuf))) {
  98. char_for_insertion = x_key;
  99. goto fin;
  100. }
  101. }
  102. /* 8-bit source */
  103. } else {
  104. int res = str_is_valid_char (edit->charbuf, edit->charpoint);
  105. if (res < 0) {
  106. if (res != -2) {
  107. edit->charpoint = 0; /* broken multibyte char, skip */
  108. goto fin;
  109. }
  110. /* not finised multibyte input (in meddle multibyte utf-8 char) */
  111. goto fin;
  112. } else {
  113. if ( g_unichar_isprint (g_utf8_get_char(edit->charbuf)) ) {
  114. c = convert_from_utf_to_current ( edit->charbuf );
  115. edit->charbuf[0] = '\0';
  116. edit->charpoint = 0;
  117. char_for_insertion = c;
  118. goto fin;
  119. }
  120. /* unprinteble utf input, skip it */
  121. edit->charbuf[0] = '\0';
  122. edit->charpoint = 0;
  123. }
  124. }
  125. }
  126. #endif /* HAVE_CHARSET */
  127. }
  128. /* Commands specific to the key emulation */
  129. if (edit->extmod) {
  130. edit->extmod = 0;
  131. command = lookup_keymap_command (editor_x_map, x_key);
  132. } else
  133. command = lookup_keymap_command (editor_map, x_key);
  134. if (command == CK_Ignore_Key)
  135. command = CK_Insert_Char;
  136. fin:
  137. *cmd = (int) command; /* FIXME */
  138. *ch = char_for_insertion;
  139. return (command == (unsigned long) CK_Insert_Char && char_for_insertion == -1) ? 0 : 1;
  140. }