comparison src/readline-2-event-hook.patch @ 6551:6090fc4102bb release

maint: Merge default to release.
author Markus Mützel <markus.muetzel@gmx.de>
date Wed, 30 Nov 2022 18:23:50 +0100
parents 54f336bbb833
children a6f1111a9f1d
comparison
equal deleted inserted replaced
6519:77267b0c5bc8 6551:6090fc4102bb
1 diff -ur readline-8.1.3/input.c readline-8.1.4/input.c 1 diff -urN readline-8.2/input.c.orig readline-8.2/input.c
2 --- readline-8.1.3/input.c 2020-12-19 07:56:01.572775578 -0500 2 --- readline-8.2/input.c.orig 2022-04-08 21:43:24.000000000 +0200
3 +++ readline-8.1.4/input.c 2020-12-19 07:57:24.608319024 -0500 3 +++ readline-8.2/input.c 2022-11-22 16:54:55.099070500 +0100
4 @@ -142,6 +142,11 @@ 4 @@ -176,6 +176,12 @@
5 static unsigned char ibuffer[512]; 5 static unsigned char ibuffer[512];
6 static int ibuffer_len = sizeof (ibuffer) - 1; 6 static int ibuffer_len = sizeof (ibuffer) - 1;
7 7
8 +#if defined (__MINGW32__) 8 +#if defined (__MINGW32__)
9 +static int _win32_getch (void); 9 +#include <windows.h>
10 +static int _win32_getch (int *const is_char);
10 +static int _win32_kbhit (void); 11 +static int _win32_kbhit (void);
11 +#endif 12 +#endif
12 + 13 +
13 #define any_typein (push_index != pop_index) 14 #define any_typein (push_index != pop_index)
14 15
15 int 16 int
16 @@ -268,7 +273,7 @@ 17 @@ -306,7 +311,7 @@
17 #if defined (__MINGW32__) 18 #if defined (__MINGW32__)
18 /* Use getch/_kbhit to check for available console input, in the same way 19 /* Use getch/_kbhit to check for available console input, in the same way
19 that we read it normally. */ 20 that we read it normally. */
20 - chars_avail = isatty (tty) ? _kbhit () : 0; 21 - chars_avail = isatty (tty) ? _kbhit () : 0;
21 + chars_avail = isatty (tty) ? _win32_kbhit () : 0; 22 + chars_avail = isatty (tty) ? _win32_kbhit () : 0;
22 result = 0; 23 result = 0;
23 #endif 24 #endif
24 25
25 @@ -520,6 +525,120 @@ 26 @@ -404,7 +409,7 @@
27
28 #if defined (__MINGW32__)
29 if (isatty (tty))
30 - return (_kbhit ());
31 + return (_win32_kbhit ());
32 #endif
33
34 return 0;
35 @@ -799,6 +804,139 @@
26 return (c); 36 return (c);
27 } 37 }
28 38
29 +#if defined (__MINGW32__) 39 +#if defined (__MINGW32__)
30 +#define _WIN32_READ_NOCHAR (-2) 40 +#define _WIN32_READ_NOCHAR (-2)
31 +static char _win32_buf[16] = {'0'}; 41 +static char _win32_buf[16] = {'0'};
32 +static int _win32_bufidx = 0; 42 +static int _win32_bufidx = 0;
33 + 43 +
34 +static int 44 +static int
35 +_win32_getch_internal (int block) 45 +_win32_getch_internal (int block, int *const is_char)
36 +{ 46 +{
37 + INPUT_RECORD rec; 47 + INPUT_RECORD rec;
38 + DWORD evRead, waitResult; 48 + DWORD evRead, waitResult;
39 + HANDLE hInput = (HANDLE) _get_osfhandle (fileno (rl_instream)); 49 + HANDLE hInput = (HANDLE) _get_osfhandle (fileno (rl_instream));
50 + *is_char = 1;
40 + 51 +
41 + if (_win32_bufidx > 0) 52 + if (_win32_bufidx > 0)
42 + return _win32_buf[--_win32_bufidx]; 53 + return _win32_buf[--_win32_bufidx];
43 + 54 +
44 + hInput = (HANDLE) _get_osfhandle (fileno (rl_instream)); 55 + hInput = (HANDLE) _get_osfhandle (fileno (rl_instream));
45 + 56 +
46 + do 57 + do
47 + { 58 + {
59 + *is_char = 0;
48 + if (! block) 60 + if (! block)
49 + { 61 + {
50 + if (WaitForSingleObject(hInput, _keyboard_input_timeout/1000) != WAIT_OBJECT_0) 62 + if (WaitForSingleObject (hInput, _keyboard_input_timeout/1000) != WAIT_OBJECT_0)
51 + return _WIN32_READ_NOCHAR; 63 + return _WIN32_READ_NOCHAR;
52 + } 64 + }
53 + 65 +
54 + if (!ReadConsoleInput(hInput, &rec, 1, &evRead) || evRead != 1) 66 + if (!ReadConsoleInputW (hInput, &rec, 1, &evRead) || evRead != 1)
55 + return EOF; 67 + return EOF;
56 + 68 +
69 + *is_char = 1;
57 + switch (rec.EventType) 70 + switch (rec.EventType)
58 + { 71 + {
59 + case KEY_EVENT: 72 + case KEY_EVENT:
60 + if ((rec.Event.KeyEvent.bKeyDown && 73 + if ((rec.Event.KeyEvent.bKeyDown &&
61 + (rec.Event.KeyEvent.wVirtualKeyCode < VK_SHIFT || 74 + (rec.Event.KeyEvent.wVirtualKeyCode < VK_SHIFT ||
62 + rec.Event.KeyEvent.wVirtualKeyCode > VK_MENU)) || 75 + rec.Event.KeyEvent.wVirtualKeyCode > VK_MENU)) ||
63 + (!rec.Event.KeyEvent.bKeyDown && 76 + (!rec.Event.KeyEvent.bKeyDown &&
64 + rec.Event.KeyEvent.wVirtualKeyCode == VK_MENU && 77 + rec.Event.KeyEvent.wVirtualKeyCode == VK_MENU &&
65 + rec.Event.KeyEvent.uChar.AsciiChar)) 78 + rec.Event.KeyEvent.uChar.UnicodeChar))
66 + { 79 + {
67 + if (rec.Event.KeyEvent.uChar.AsciiChar) 80 + if (rec.Event.KeyEvent.uChar.UnicodeChar)
81 + return (int) rec.Event.KeyEvent.uChar.UnicodeChar;
82 +
83 + *is_char = 0;
84 + switch (rec.Event.KeyEvent.wVirtualKeyCode)
68 + { 85 + {
69 + if (rec.Event.KeyEvent.uChar.AsciiChar < 0 || 86 + case VK_UP:
70 + (rec.Event.KeyEvent.uChar.AsciiChar < 32 && 87 + _win32_buf[_win32_bufidx++] = 'A';
71 + !(rec.Event.KeyEvent.dwControlKeyState & (RIGHT_CTRL_PRESSED|LEFT_CTRL_PRESSED)))) 88 + return 0340;
72 + { 89 + case VK_DOWN:
73 + char c = rec.Event.KeyEvent.uChar.AsciiChar; 90 + _win32_buf[_win32_bufidx++] = 'B';
74 + if (GetOEMCP () == GetConsoleCP ()) 91 + return 0340;
75 + OemToCharBuff (&c, &c, 1); 92 + case VK_RIGHT:
76 + return (int)(unsigned char)c; 93 + _win32_buf[_win32_bufidx++] = 'C';
77 + } 94 + return 0340;
78 + else 95 + case VK_LEFT:
79 + return (int)rec.Event.KeyEvent.uChar.UnicodeChar; 96 + _win32_buf[_win32_bufidx++] = 'D';
97 + return 0340;
98 + case VK_HOME:
99 + _win32_buf[_win32_bufidx++] = 'H';
100 + return 0340;
101 + case VK_END:
102 + _win32_buf[_win32_bufidx++] = 'F';
103 + return 0340;
104 + case VK_DELETE:
105 + _win32_buf[_win32_bufidx++] = 8;
106 + _win32_buf[_win32_bufidx++] = 'C';
107 + return 0340;
108 + default:
109 + break;
80 + } 110 + }
81 + else
82 + switch (rec.Event.KeyEvent.wVirtualKeyCode)
83 + {
84 + case VK_UP:
85 + _win32_buf[_win32_bufidx++] = 'H';
86 + return 0340;
87 + case VK_DOWN:
88 + _win32_buf[_win32_bufidx++] = 'P';
89 + return 0340;
90 + case VK_RIGHT:
91 + _win32_buf[_win32_bufidx++] = 'M';
92 + return 0340;
93 + case VK_LEFT:
94 + _win32_buf[_win32_bufidx++] = 'K';
95 + return 0340;
96 + case VK_HOME:
97 + _win32_buf[_win32_bufidx++] = 'G';
98 + return 0340;
99 + case VK_END:
100 + _win32_buf[_win32_bufidx++] = 'O';
101 + return 0340;
102 + case VK_DELETE:
103 + _win32_buf[_win32_bufidx++] = 'S';
104 + return 0340;
105 + default:
106 + break;
107 + }
108 + } 111 + }
109 + break; 112 + break;
110 + 113 +
111 + case WINDOW_BUFFER_SIZE_EVENT: 114 + case WINDOW_BUFFER_SIZE_EVENT:
112 + rl_resize_terminal (); 115 + rl_resize_terminal ();
113 + break; 116 + break;
114 + 117 +
115 + default: 118 + default:
116 + break; 119 + break;
117 + } 120 + }
118 + } 121 + }
119 + while (1); 122 + while (1);
120 +} 123 +}
121 + 124 +
122 +static int 125 +static int
123 +_win32_kbhit (void) 126 +_win32_kbhit (void)
124 +{ 127 +{
125 + int result; 128 + int result;
129 + int is_char;
126 + 130 +
127 + result = _win32_getch_internal (0); 131 + result = _win32_getch_internal (0, &is_char);
128 + if (result == _WIN32_READ_NOCHAR 132 + if (result == _WIN32_READ_NOCHAR
129 + || result == EOF) 133 + || result == EOF)
130 + return 0; 134 + return 0;
131 + _win32_buf[_win32_bufidx++] = result; 135 +
136 + if (is_char)
137 + {
138 + char charbuf[5] = {0};
139 + wchar_t unicode[2] = {result, 0};
140 + int utf16_code_units = 1;
141 + if ((unicode[0] & 0xF800) == 0xD800) /* outside BMP */
142 + {
143 + unicode[1] = _win32_getch_internal (0, &is_char);
144 + utf16_code_units++;
145 + }
146 + /* convert to UTF-8 byte sequence */
147 + int len = WideCharToMultiByte (CP_UTF8, 0,
148 + (wchar_t *) &unicode, utf16_code_units,
149 + charbuf, 5, NULL, NULL);
150 + for (int i=0; i<len; i++)
151 + {
152 + _win32_buf[_win32_bufidx++] = (unsigned char) charbuf[len-i-1];
153 + }
154 + }
155 + else
156 + {
157 + /* buffer is read from back to front */
158 + _win32_buf[_win32_bufidx++] = 0x5b; /* "[" */
159 + _win32_buf[_win32_bufidx++] = 0x1b; /* ESC */
160 + }
132 + 161 +
133 + return _win32_bufidx; 162 + return _win32_bufidx;
134 +} 163 +}
135 + 164 +
136 +static int 165 +static int
137 +_win32_getch (void) 166 +_win32_getch (int *const is_char)
138 +{ 167 +{
139 + return _win32_getch_internal (1); 168 + return _win32_getch_internal (1, is_char);
140 +} 169 +}
141 +#endif 170 +#endif
142 + 171 +
143 int 172 int
144 rl_getc (FILE *stream) 173 rl_getc (FILE *stream)
145 { 174 {
146 @@ -539,9 +658,9 @@ 175 @@ -818,8 +937,11 @@
176 /* We know at this point that _rl_caught_signal == 0 */
177
147 #if defined (__MINGW32__) 178 #if defined (__MINGW32__)
148 if (isatty (fileno (stream))) 179 - if (isatty (fd)
149 { 180 - return (_getch ()); /* "There is no error return." */
150 - int c = _getch (); 181 + if (isatty (fd))
151 + int c = _win32_getch (); 182 + {
152 if (c == 0xe0) 183 + int is_char;
153 - rl_execute_next (_getch ()); 184 + return (_win32_getch (&is_char));
154 + rl_execute_next (_win32_getch ()); 185 + }
155 return (c);
156 }
157 #endif 186 #endif
187 result = 0;
188 #if defined (HAVE_PSELECT) || defined (HAVE_SELECT)