editkeys.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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/charsets.h" /* convert_from_input_c() */
  40. #include "../src/main.h" /* display_codepage */
  41. #include "../src/strutil.h" /* str_isutf8 () */
  42. /*
  43. * Translate the keycode into either 'command' or 'char_for_insertion'.
  44. * 'command' is one of the editor commands from cmddef.h.
  45. */
  46. int
  47. edit_translate_key (WEdit *edit, long x_key, int *cmd, int *ch)
  48. {
  49. int command = CK_Insert_Char;
  50. int char_for_insertion = -1;
  51. int i = 0;
  52. int extmod = 0;
  53. int c;
  54. /* an ordinary insertable character */
  55. if (x_key < 256 && !extmod) {
  56. #ifdef HAVE_CHARSET
  57. if ( edit->charpoint >= 4 ) {
  58. edit->charpoint = 0;
  59. edit->charbuf[edit->charpoint] = '\0';
  60. }
  61. if ( edit->charpoint < 4 ) {
  62. edit->charbuf[edit->charpoint++] = x_key;
  63. edit->charbuf[edit->charpoint] = '\0';
  64. }
  65. /* input from 8-bit locale */
  66. if ( !utf8_display ) {
  67. /* source in 8-bit codeset */
  68. if (!edit->utf8) {
  69. #endif
  70. c = convert_from_input_c (x_key);
  71. if (is_printable (c)) {
  72. char_for_insertion = c;
  73. goto fin;
  74. }
  75. #ifdef HAVE_CHARSET
  76. } else {
  77. c = convert_from_input_c (x_key);
  78. if (is_printable (c)) {
  79. char_for_insertion = convert_from_8bit_to_utf_c2((unsigned char) x_key);
  80. goto fin;
  81. }
  82. }
  83. /* UTF-8 locale */
  84. } else {
  85. /* source in UTF-8 codeset */
  86. if ( edit->utf8 ) {
  87. int res = str_is_valid_char (edit->charbuf, edit->charpoint);
  88. if (res < 0) {
  89. if (res != -2) {
  90. edit->charpoint = 0; /* broken multibyte char, skip */
  91. goto fin;
  92. }
  93. char_for_insertion = x_key;
  94. goto fin;
  95. } else {
  96. edit->charbuf[edit->charpoint]='\0';
  97. edit->charpoint = 0;
  98. if ( g_unichar_isprint (g_utf8_get_char(edit->charbuf))) {
  99. char_for_insertion = x_key;
  100. goto fin;
  101. }
  102. }
  103. /* 8-bit source */
  104. } else {
  105. int res = str_is_valid_char (edit->charbuf, edit->charpoint);
  106. if (res < 0) {
  107. if (res != -2) {
  108. edit->charpoint = 0; /* broken multibyte char, skip */
  109. goto fin;
  110. }
  111. /* not finised multibyte input (in meddle multibyte utf-8 char) */
  112. goto fin;
  113. } else {
  114. if ( g_unichar_isprint (g_utf8_get_char(edit->charbuf)) ) {
  115. c = convert_from_utf_to_current ( edit->charbuf );
  116. edit->charbuf[0] = '\0';
  117. edit->charpoint = 0;
  118. char_for_insertion = c;
  119. goto fin;
  120. }
  121. /* unprinteble utf input, skip it */
  122. edit->charbuf[0] = '\0';
  123. edit->charpoint = 0;
  124. }
  125. }
  126. }
  127. #endif
  128. }
  129. /* Commands specific to the key emulation */
  130. for (i = 0; edit->user_map[i].key != 0; i++) {
  131. if (x_key == edit->user_map[i].key) {
  132. command = edit->user_map[i].command;
  133. }
  134. }
  135. fin:
  136. *cmd = command;
  137. *ch = char_for_insertion;
  138. if (command == CK_Insert_Char && char_for_insertion == -1) {
  139. /* unchanged, key has no function here */
  140. return 0;
  141. }
  142. return 1;
  143. }