dsound/tests: Allow more time for an event to be signalled.
[wine.git] / dlls / kernel32 / editline.c
blob7492604f044f2fa7f82c07d26b9bdad73ff758b9
1 /*
2 * line edition function for Win32 console
4 * Copyright 2001 Eric Pouech
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wincon.h"
30 #include "wine/unicode.h"
31 #include "winnls.h"
32 #include "wine/debug.h"
33 #include "console_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(console);
37 struct WCEL_Context;
39 typedef struct
41 WCHAR val; /* vk or unicode char */
42 void (*func)(struct WCEL_Context* ctx);
43 } KeyEntry;
45 typedef struct
47 DWORD keyState; /* keyState (from INPUT_RECORD) to match */
48 BOOL chkChar; /* check vk or char */
49 const KeyEntry* entries; /* array of entries */
50 } KeyMap;
52 typedef struct WCEL_Context {
53 WCHAR* line; /* the line being edited */
54 size_t alloc; /* number of WCHAR in line */
55 unsigned len; /* number of chars in line */
56 unsigned last_rub; /* number of chars to rub to get to start
57 (for consoles that can't change cursor pos) */
58 unsigned last_max; /* max number of chars written
59 (for consoles that can't change cursor pos) */
60 unsigned ofs; /* offset for cursor in current line */
61 WCHAR* yanked; /* yanked line */
62 unsigned mark; /* marked point (emacs mode only) */
63 CONSOLE_SCREEN_BUFFER_INFO csbi; /* current state (initial cursor, window size, attribute) */
64 CONSOLE_CURSOR_INFO cinfo; /* original cursor state (size, visibility) */
65 HANDLE hConIn;
66 HANDLE hConOut;
67 unsigned done : 1, /* to 1 when we're done with editing */
68 error : 1, /* to 1 when an error occurred in the editing */
69 can_wrap : 1, /* to 1 when multi-line edition can take place */
70 shall_echo : 1, /* to 1 when characters should be echo:ed when keyed-in */
71 insert : 1, /* to 1 when new characters are inserted (otherwise overwrite) */
72 insertkey : 1, /* to 1 when the Insert key toggle is active */
73 can_pos_cursor : 1; /* to 1 when console can (re)position cursor */
74 unsigned histSize;
75 unsigned histPos;
76 WCHAR* histCurr;
77 } WCEL_Context;
79 #if 0
80 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
82 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
83 "\t\thist=(size=%u pos=%u curr=%s)\n"
84 "\t\tyanked=%s\n",
85 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
86 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
87 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
88 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
89 debugstr_w(ctx->yanked));
91 #endif
93 /* ====================================================================
95 * Console helper functions
97 * ====================================================================*/
99 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
101 DWORD num_read;
103 if (ReadConsoleInputW(ctx->hConIn, ir, 1, &num_read)) return TRUE;
104 ctx->error = 1;
105 return FALSE;
108 static inline void WCEL_Beep(WCEL_Context* ctx)
110 Beep(400, 300);
113 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
115 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
118 static inline int WCEL_CharWidth(WCHAR wch)
120 return wch < ' ' ? 2 : 1;
123 static inline int WCEL_StringWidth(const WCHAR* str, int beg, int len)
125 int i, ofs;
127 for (i = 0, ofs = 0; i < len; i++)
128 ofs += WCEL_CharWidth(str[beg + i]);
129 return ofs;
132 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int strofs)
134 COORD c;
135 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
136 int ofs;
138 ofs = WCEL_StringWidth(ctx->line, 0, strofs);
140 c.Y = ctx->csbi.dwCursorPosition.Y;
141 if (ofs >= len)
143 ofs -= len;
144 c.X = ofs % ctx->csbi.dwSize.X;
145 c.Y += 1 + ofs / ctx->csbi.dwSize.X;
147 else c.X = ctx->csbi.dwCursorPosition.X + ofs;
148 return c;
151 static DWORD WCEL_WriteConsole(WCEL_Context* ctx, DWORD beg, DWORD len)
153 DWORD i, last, dw, ret = 0;
154 WCHAR tmp[2];
156 for (i = last = 0; i < len; i++)
158 if (ctx->line[beg + i] < ' ')
160 if (last != i)
162 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], i - last, &dw, NULL);
163 ret += dw;
165 tmp[0] = '^';
166 tmp[1] = '@' + ctx->line[beg + i];
167 WriteConsoleW(ctx->hConOut, tmp, 2, &dw, NULL);
168 last = i + 1;
169 ret += dw;
172 if (last != len)
174 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], len - last, &dw, NULL);
175 ret += dw;
177 return ret;
180 static inline void WCEL_WriteNChars(WCEL_Context* ctx, char ch, int count)
182 DWORD dw;
184 if (count > 0)
186 while (count--) WriteFile(ctx->hConOut, &ch, 1, &dw, NULL);
190 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
192 int i, last;
193 DWORD count;
194 WCHAR tmp[2];
196 /* bare console case is handled in CONSOLE_ReadLine (we always reprint the whole string) */
197 if (!ctx->shall_echo || !ctx->can_pos_cursor) return;
199 for (i = last = beg; i < beg + len; i++)
201 if (ctx->line[i] < ' ')
203 if (last != i)
205 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
206 WCEL_GetCoord(ctx, last), &count);
207 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
208 WCEL_GetCoord(ctx, last), &count);
210 tmp[0] = '^';
211 tmp[1] = '@' + ctx->line[i];
212 WriteConsoleOutputCharacterW(ctx->hConOut, tmp, 2,
213 WCEL_GetCoord(ctx, i), &count);
214 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, 2,
215 WCEL_GetCoord(ctx, i), &count);
216 last = i + 1;
219 if (last != beg + len)
221 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
222 WCEL_GetCoord(ctx, last), &count);
223 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
224 WCEL_GetCoord(ctx, last), &count);
228 /* ====================================================================
230 * context manipulation functions
232 * ====================================================================*/
234 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
236 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
238 FIXME("Mode doesn't allow wrapping. However, we should allow overwriting the current string\n");
239 return FALSE;
242 if (ctx->len + len >= ctx->alloc)
244 WCHAR* newline;
245 size_t newsize;
247 /* round up size to 32 byte-WCHAR boundary */
248 newsize = (ctx->len + len + 1 + 31) & ~31;
250 if (ctx->line)
251 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
252 else
253 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
255 if (!newline) return FALSE;
256 ctx->line = newline;
257 ctx->alloc = newsize;
259 return TRUE;
262 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
264 unsigned str_len = end - beg;
266 if (end < ctx->len)
267 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
268 /* we need to clean from ctx->len - str_len to ctx->len */
270 if (ctx->shall_echo)
272 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
273 COORD cend = WCEL_GetCoord(ctx, ctx->len);
274 CHAR_INFO ci;
276 ci.Char.UnicodeChar = ' ';
277 ci.Attributes = ctx->csbi.wAttributes;
279 if (cbeg.Y == cend.Y)
281 /* partial erase of sole line */
282 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
283 cend.X - cbeg.X, &ci);
285 else
287 int i;
288 /* erase til eol on first line */
289 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
290 ctx->csbi.dwSize.X - cbeg.X, &ci);
291 /* completely erase all the others (full lines) */
292 for (i = cbeg.Y + 1; i < cend.Y; i++)
293 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
294 /* erase from beginning of line until last pos on last line */
295 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
298 ctx->len -= str_len;
299 WCEL_Update(ctx, 0, ctx->len);
300 ctx->line[ctx->len] = 0;
303 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
305 size_t len = lstrlenW(str), updtlen;
307 if (!len) return;
308 if (ctx->insert)
310 if (!WCEL_Grow(ctx, len)) return;
311 if (ctx->len > ctx->ofs)
312 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
313 ctx->len += len;
314 updtlen = ctx->len - ctx->ofs;
316 else
318 if (ctx->ofs + len > ctx->len)
320 if (!WCEL_Grow(ctx, (ctx->ofs + len) - ctx->len)) return;
321 ctx->len = ctx->ofs + len;
323 updtlen = len;
325 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
326 ctx->line[ctx->len] = 0;
327 WCEL_Update(ctx, ctx->ofs, updtlen);
328 ctx->ofs += len;
331 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
333 WCHAR buffer[2];
335 buffer[0] = c;
336 buffer[1] = 0;
337 WCEL_InsertString(ctx, buffer);
340 static void WCEL_FreeYank(WCEL_Context* ctx)
342 HeapFree(GetProcessHeap(), 0, ctx->yanked);
343 ctx->yanked = NULL;
346 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
348 int len = end - beg;
349 if (len <= 0) return;
351 WCEL_FreeYank(ctx);
352 /* After WCEL_FreeYank ctx->yanked is empty */
353 ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
354 if (!ctx->yanked) return;
355 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
356 ctx->yanked[len] = 0;
359 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
360 * of the data lay in unicode lib
362 static inline BOOL WCEL_iswalnum(WCHAR wc)
364 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
367 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
369 ofs--;
370 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
371 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
372 if (ofs >= 0) ofs++;
373 return max(ofs, 0);
376 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
378 ofs++;
379 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
380 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
381 return min(ofs, ctx->len);
384 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
386 WCHAR* ptr;
388 if (idx == ctx->histSize - 1)
390 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
391 lstrcpyW(ptr, ctx->histCurr);
393 else
395 int len = CONSOLE_GetHistory(idx, NULL, 0);
397 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
399 CONSOLE_GetHistory(idx, ptr, len);
402 return ptr;
405 static void WCEL_HistoryInit(WCEL_Context* ctx)
407 ctx->histPos = CONSOLE_GetNumHistoryEntries();
408 ctx->histSize = ctx->histPos + 1;
409 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
412 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
414 WCHAR* data = WCEL_GetHistory(ctx, idx);
415 int len = lstrlenW(data) + 1;
417 /* save current line edition for recall when needed (FIXME seems broken to me) */
418 if (ctx->histPos == ctx->histSize - 1)
420 HeapFree(GetProcessHeap(), 0, ctx->histCurr);
421 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
422 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
424 /* need to clean also the screen if new string is shorter than old one */
425 WCEL_DeleteString(ctx, 0, ctx->len);
426 ctx->ofs = 0;
427 /* insert new string */
428 if (WCEL_Grow(ctx, len))
430 WCEL_InsertString(ctx, data);
431 ctx->histPos = idx;
433 HeapFree(GetProcessHeap(), 0, data);
436 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
438 int startPos = ctx->histPos;
439 WCHAR* data;
440 unsigned int len, oldofs;
442 if (ctx->histPos && ctx->histPos == ctx->histSize) {
443 startPos--;
444 ctx->histPos--;
447 do {
448 data = WCEL_GetHistory(ctx, ctx->histPos);
450 if (ctx->histPos) ctx->histPos--;
451 else ctx->histPos = (ctx->histSize-1);
453 len = lstrlenW(data) + 1;
454 if ((len >= ctx->ofs) &&
455 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
457 /* need to clean also the screen if new string is shorter than old one */
458 WCEL_DeleteString(ctx, 0, ctx->len);
460 if (WCEL_Grow(ctx, len))
462 oldofs = ctx->ofs;
463 ctx->ofs = 0;
464 WCEL_InsertString(ctx, data);
465 ctx->ofs = oldofs;
466 if (ctx->shall_echo)
467 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
468 HeapFree(GetProcessHeap(), 0, data);
469 return;
472 HeapFree(GetProcessHeap(), 0, data);
473 } while (ctx->histPos != startPos);
475 return;
478 /* ====================================================================
480 * basic edition functions
482 * ====================================================================*/
484 static void WCEL_Done(WCEL_Context* ctx)
486 WCHAR nl = '\n';
487 if (!WCEL_Grow(ctx, 2)) return;
488 ctx->line[ctx->len++] = '\r';
489 ctx->line[ctx->len++] = '\n';
490 ctx->line[ctx->len] = 0;
491 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
492 if (ctx->insertkey)
493 SetConsoleCursorInfo(ctx->hConOut, &ctx->cinfo);
494 ctx->done = 1;
497 static void WCEL_MoveLeft(WCEL_Context* ctx)
499 if (ctx->ofs > 0) ctx->ofs--;
502 static void WCEL_MoveRight(WCEL_Context* ctx)
504 if (ctx->ofs < ctx->len) ctx->ofs++;
507 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
509 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
510 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
513 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
515 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
516 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
519 static void WCEL_MoveToBeg(WCEL_Context* ctx)
521 ctx->ofs = 0;
524 static void WCEL_MoveToEnd(WCEL_Context* ctx)
526 ctx->ofs = ctx->len;
529 static void WCEL_SetMark(WCEL_Context* ctx)
531 ctx->mark = ctx->ofs;
534 static void WCEL_ExchangeMark(WCEL_Context* ctx)
536 unsigned tmp;
538 if (ctx->mark > ctx->len) return;
539 tmp = ctx->ofs;
540 ctx->ofs = ctx->mark;
541 ctx->mark = tmp;
544 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
546 unsigned beg, end;
548 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
549 if (ctx->mark > ctx->ofs)
551 beg = ctx->ofs; end = ctx->mark;
553 else
555 beg = ctx->mark; end = ctx->ofs;
557 WCEL_SaveYank(ctx, beg, end);
560 static void WCEL_TransposeChar(WCEL_Context* ctx)
562 WCHAR c;
564 if (!ctx->ofs || ctx->ofs == ctx->len) return;
566 c = ctx->line[ctx->ofs];
567 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
568 ctx->line[ctx->ofs - 1] = c;
570 WCEL_Update(ctx, ctx->ofs - 1, 2);
571 ctx->ofs++;
574 static void WCEL_TransposeWords(WCEL_Context* ctx)
576 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
577 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
578 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
580 unsigned len_r = right_ofs - ctx->ofs;
581 unsigned len_l = ctx->ofs - left_ofs;
583 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
584 if (!tmp) return;
586 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
587 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
588 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
590 HeapFree(GetProcessHeap(), 0, tmp);
591 WCEL_Update(ctx, left_ofs, len_l + len_r);
592 ctx->ofs = right_ofs;
596 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
598 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
599 if (new_ofs != ctx->ofs)
601 unsigned int i;
602 for (i = ctx->ofs; i <= new_ofs; i++)
603 ctx->line[i] = tolowerW(ctx->line[i]);
604 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
605 ctx->ofs = new_ofs;
609 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
611 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
612 if (new_ofs != ctx->ofs)
614 unsigned int i;
615 for (i = ctx->ofs; i <= new_ofs; i++)
616 ctx->line[i] = toupperW(ctx->line[i]);
617 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
618 ctx->ofs = new_ofs;
622 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
624 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
625 if (new_ofs != ctx->ofs)
627 unsigned int i;
629 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
630 for (i = ctx->ofs + 1; i <= new_ofs; i++)
631 ctx->line[i] = tolowerW(ctx->line[i]);
632 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
633 ctx->ofs = new_ofs;
637 static void WCEL_Yank(WCEL_Context* ctx)
639 if (ctx->yanked)
640 WCEL_InsertString(ctx, ctx->yanked);
643 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
645 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
646 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
649 static void WCEL_KillFromBegOfLine(WCEL_Context* ctx)
651 if (ctx->ofs)
653 WCEL_SaveYank(ctx, 0, ctx->ofs);
654 WCEL_DeleteString(ctx, 0, ctx->ofs);
655 ctx->ofs = 0;
659 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
661 unsigned beg, end;
663 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
664 if (ctx->mark > ctx->ofs)
666 beg = ctx->ofs; end = ctx->mark;
668 else
670 beg = ctx->mark; end = ctx->ofs;
672 WCEL_SaveYank(ctx, beg, end);
673 WCEL_DeleteString(ctx, beg, end);
674 ctx->ofs = beg;
677 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
679 if (ctx->ofs)
681 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
682 ctx->ofs--;
686 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
688 if (ctx->ofs < ctx->len)
689 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
692 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
694 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
695 if (new_ofs != ctx->ofs)
697 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
698 ctx->ofs = new_ofs;
702 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
704 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
705 if (new_ofs != ctx->ofs)
707 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
711 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
713 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
716 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
718 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
721 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
723 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
726 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
728 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
731 static void WCEL_Redraw(WCEL_Context* ctx)
733 if (ctx->shall_echo)
735 COORD c = WCEL_GetCoord(ctx, ctx->len);
736 CHAR_INFO ci;
738 WCEL_Update(ctx, 0, ctx->len);
740 ci.Char.UnicodeChar = ' ';
741 ci.Attributes = ctx->csbi.wAttributes;
743 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
747 static void WCEL_RepeatCount(WCEL_Context* ctx)
749 #if 0
750 /* FIXME: wait until all console code is in kernel32 */
751 INPUT_RECORD ir;
752 unsigned repeat = 0;
754 while (WCEL_Get(ctx, &ir, FALSE))
756 if (ir.EventType != KEY_EVENT) break;
757 if (ir.Event.KeyEvent.bKeyDown)
759 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
760 break;
761 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
762 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
763 break;
764 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
766 WCEL_Get(ctx, &ir, TRUE);
768 FIXME("=> %u\n", repeat);
769 #endif
772 static void WCEL_ToggleInsert(WCEL_Context* ctx)
774 CONSOLE_CURSOR_INFO cinfo;
776 ctx->insertkey = !ctx->insertkey;
778 if (GetConsoleCursorInfo(ctx->hConOut, &cinfo))
780 cinfo.dwSize = ctx->insertkey ? 100 : 25;
781 SetConsoleCursorInfo(ctx->hConOut, &cinfo);
785 /* ====================================================================
787 * Key Maps
789 * ====================================================================*/
791 #define CTRL(x) ((x) - '@')
792 static const KeyEntry StdKeyMap[] =
794 {/*VK_BACK*/ 0x08, WCEL_DeletePrevChar },
795 {/*VK_RETURN*/0x0d, WCEL_Done },
796 {/*VK_DELETE*/0x2e, WCEL_DeleteCurrChar },
797 { 0, NULL }
800 static const KeyEntry EmacsKeyMapCtrl[] =
802 { CTRL('@'), WCEL_SetMark },
803 { CTRL('A'), WCEL_MoveToBeg },
804 { CTRL('B'), WCEL_MoveLeft },
805 /* C: done in server */
806 { CTRL('D'), WCEL_DeleteCurrChar },
807 { CTRL('E'), WCEL_MoveToEnd },
808 { CTRL('F'), WCEL_MoveRight },
809 { CTRL('G'), WCEL_Beep },
810 { CTRL('H'), WCEL_DeletePrevChar },
811 /* I: meaningless (or tab ???) */
812 { CTRL('J'), WCEL_Done },
813 { CTRL('K'), WCEL_KillToEndOfLine },
814 { CTRL('L'), WCEL_Redraw },
815 { CTRL('M'), WCEL_Done },
816 { CTRL('N'), WCEL_MoveToNextHist },
817 /* O; insert line... meaningless */
818 { CTRL('P'), WCEL_MoveToPrevHist },
819 /* Q: [NIY] quoting... */
820 /* R: [NIY] search backwards... */
821 /* S: [NIY] search forwards... */
822 { CTRL('T'), WCEL_TransposeChar },
823 { CTRL('U'), WCEL_RepeatCount },
824 /* V: paragraph down... meaningless */
825 { CTRL('W'), WCEL_KillMarkedZone },
826 { CTRL('X'), WCEL_ExchangeMark },
827 { CTRL('Y'), WCEL_Yank },
828 /* Z: meaningless */
829 { 0, NULL }
832 static const KeyEntry EmacsKeyMapAlt[] =
834 {/*DEL*/127, WCEL_DeleteLeftWord },
835 { '<', WCEL_MoveToFirstHist },
836 { '>', WCEL_MoveToLastHist },
837 { '?', WCEL_Beep },
838 { 'b', WCEL_MoveToLeftWord },
839 { 'c', WCEL_CapitalizeWord },
840 { 'd', WCEL_DeleteRightWord },
841 { 'f', WCEL_MoveToRightWord },
842 { 'l', WCEL_LowerCaseWord },
843 { 't', WCEL_TransposeWords },
844 { 'u', WCEL_UpperCaseWord },
845 { 'w', WCEL_CopyMarkedZone },
846 { 0, NULL }
849 static const KeyEntry EmacsStdKeyMap[] =
851 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
852 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
853 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
854 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
855 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
856 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
857 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert },
858 { 0, NULL }
861 static const KeyMap EmacsKeyMap[] =
863 {0, 0, StdKeyMap},
864 {0, 0, EmacsStdKeyMap},
865 {RIGHT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* right alt */
866 {LEFT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* left alt */
867 {RIGHT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* right ctrl */
868 {LEFT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* left ctrl */
869 {0, 0, NULL}
872 static const KeyEntry Win32StdKeyMap[] =
874 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
875 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
876 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
877 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
878 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
879 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
880 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert },
881 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
882 { 0, NULL }
885 static const KeyEntry Win32KeyMapCtrl[] =
887 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
888 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
889 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
890 {/*VK_HOME*/ 0x24, WCEL_KillFromBegOfLine },
891 { 0, NULL }
894 static const KeyMap Win32KeyMap[] =
896 {0, 0, StdKeyMap},
897 {SHIFT_PRESSED, 0, StdKeyMap},
898 {0, 0, Win32StdKeyMap},
899 {RIGHT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
900 {LEFT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
901 {0, 0, NULL}
903 #undef CTRL
905 /* ====================================================================
907 * Read line master function
909 * ====================================================================*/
911 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, BOOL can_pos_cursor)
913 WCEL_Context ctx;
914 INPUT_RECORD ir;
915 const KeyMap* km;
916 const KeyEntry* ke;
917 unsigned ofs;
918 void (*func)(struct WCEL_Context* ctx);
919 DWORD mode, input_mode, ks;
920 int use_emacs;
921 CONSOLE_SCREEN_BUFFER_INFO csbi;
923 memset(&ctx, 0, sizeof(ctx));
924 ctx.hConIn = hConsoleIn;
925 WCEL_HistoryInit(&ctx);
927 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
928 use_emacs = 0;
930 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
931 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
932 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
933 return NULL;
934 if (!GetConsoleMode(hConsoleIn, &mode)) mode = 0;
935 input_mode = mode;
936 ctx.shall_echo = (mode & ENABLE_ECHO_INPUT) ? 1 : 0;
937 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) == (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS) ? 1 : 0;
938 if (!GetConsoleMode(ctx.hConOut, &mode)) mode = 0;
939 ctx.can_wrap = (mode & ENABLE_WRAP_AT_EOL_OUTPUT) ? 1 : 0;
940 ctx.can_pos_cursor = can_pos_cursor;
941 GetConsoleCursorInfo(ctx.hConOut, &ctx.cinfo);
943 if (!WCEL_Grow(&ctx, 1))
945 CloseHandle(ctx.hConOut);
946 return NULL;
948 ctx.line[0] = 0;
950 /* EPP WCEL_Dump(&ctx, "init"); */
952 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
954 if (ir.EventType != KEY_EVENT) continue;
955 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
956 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
957 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
958 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
959 if (!ir.Event.KeyEvent.bKeyDown) continue;
961 /* EPP WCEL_Dump(&ctx, "before func"); */
962 ofs = ctx.ofs;
963 /* mask out some bits which don't interest us */
964 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
966 func = NULL;
967 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
969 if (km->keyState != ks)
970 continue;
971 if (km->chkChar)
973 for (ke = &km->entries[0]; ke->func != 0; ke++)
974 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
976 else
978 for (ke = &km->entries[0]; ke->func != 0; ke++)
979 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
982 if (ke->func)
984 func = ke->func;
985 break;
989 CONSOLE_GetEditionMode(hConsoleIn, &use_emacs);
991 GetConsoleMode(hConsoleIn, &mode);
992 if (input_mode != mode)
994 input_mode = mode;
995 ctx.insertkey = 0;
997 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) ==
998 (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS);
999 if (ctx.insertkey)
1000 ctx.insert = !ctx.insert;
1002 GetConsoleScreenBufferInfo(ctx.hConOut, &csbi);
1003 ctx.csbi.wAttributes = csbi.wAttributes;
1005 if (func)
1006 (func)(&ctx);
1007 else if (!(ir.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED))
1008 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
1009 else TRACE("Dropped event\n");
1011 /* EPP WCEL_Dump(&ctx, "after func"); */
1012 if (!ctx.shall_echo) continue;
1013 if (ctx.can_pos_cursor)
1015 if (ctx.ofs != ofs)
1016 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
1018 else if (!ctx.done && !ctx.error)
1020 DWORD last;
1021 /* erase previous chars */
1022 WCEL_WriteNChars(&ctx, '\b', ctx.last_rub);
1024 /* write chars up to cursor */
1025 ctx.last_rub = WCEL_WriteConsole(&ctx, 0, ctx.ofs);
1026 /* write chars past cursor */
1027 last = ctx.last_rub + WCEL_WriteConsole(&ctx, ctx.ofs, ctx.len - ctx.ofs);
1028 if (last < ctx.last_max) /* ctx.line has been shortened, erase */
1030 WCEL_WriteNChars(&ctx, ' ', ctx.last_max - last);
1031 WCEL_WriteNChars(&ctx, '\b', ctx.last_max - last);
1032 ctx.last_max = last;
1034 else ctx.last_max = last;
1035 /* reposition at cursor */
1036 WCEL_WriteNChars(&ctx, '\b', last - ctx.last_rub);
1039 if (ctx.error)
1041 HeapFree(GetProcessHeap(), 0, ctx.line);
1042 ctx.line = NULL;
1044 WCEL_FreeYank(&ctx);
1045 if (ctx.line)
1046 CONSOLE_AppendHistory(ctx.line);
1048 CloseHandle(ctx.hConOut);
1049 HeapFree(GetProcessHeap(), 0, ctx.histCurr);
1050 return ctx.line;