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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "wine/port.h"
30 #include "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "console_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(console);
41 WCHAR val; /* vk or unicode char */
42 void (*func)(struct WCEL_Context* ctx);
47 DWORD keyState; /* keyState (from INPUT_RECORD) to match */
48 BOOL chkChar; /* check vk or char */
49 KeyEntry* entries; /* array of entries */
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 ofs; /* offset for cursor in current line */
57 WCHAR* yanked; /* yanked line */
58 unsigned mark; /* marked point (emacs mode only) */
59 CONSOLE_SCREEN_BUFFER_INFO csbi; /* current state (initial cursor, window size, attribute) */
62 unsigned done : 1, /* to 1 when we're done with editing */
63 error : 1, /* to 1 when an error occurred in the editing */
64 can_wrap : 1; /* to 1 when multi-line edition can take place */
71 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
73 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
74 "\t\thist=(size=%u pos=%u curr=%s)\n"
76 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
77 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
78 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
79 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
80 debugstr_w(ctx->yanked));
84 /* ====================================================================
86 * Console helper functions
88 * ====================================================================*/
90 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
92 if (ReadConsoleInputW(ctx->hConIn, ir, 1, NULL)) return TRUE;
93 ERR("hmm bad situation\n");
98 static inline void WCEL_Beep(WCEL_Context* ctx)
103 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
105 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
108 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int ofs)
111 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
113 c.Y = ctx->csbi.dwCursorPosition.Y;
117 c.X = ofs % ctx->csbi.dwSize.X;
118 c.Y += 1 + ofs / ctx->csbi.dwSize.X;
120 else c.X = ctx->csbi.dwCursorPosition.X + ofs;
124 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
126 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[beg], len,
127 WCEL_GetCoord(ctx, beg), NULL);
128 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, len,
129 WCEL_GetCoord(ctx, beg), NULL);
132 /* ====================================================================
134 * context manipulation functions
136 * ====================================================================*/
138 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
140 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
142 FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n");
146 if (ctx->len + len >= ctx->alloc)
151 /* round up size to 32 byte-WCHAR boundary */
152 newsize = (ctx->len + len + 1 + 31) & ~31;
155 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
157 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
159 if (!newline) return FALSE;
161 ctx->alloc = newsize;
166 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
168 unsigned str_len = end - beg;
169 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
170 COORD cend = WCEL_GetCoord(ctx, ctx->len);
174 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
175 /* we need to clean from ctx->len - str_len to ctx->len */
177 ci.Char.UnicodeChar = ' ';
178 ci.Attributes = ctx->csbi.wAttributes;
180 if (cbeg.Y == cend.Y)
182 /* partial erase of sole line */
183 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
184 cend.X - cbeg.X, &ci);
189 /* erase til eol on first line */
190 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
191 ctx->csbi.dwSize.X - cbeg.X, &ci);
192 /* completely erase all the others (full lines) */
193 for (i = cbeg.Y + 1; i < cend.Y; i++)
194 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
195 /* erase from beginning of line until last pos on last line */
196 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
199 WCEL_Update(ctx, 0, ctx->len);
200 ctx->line[ctx->len] = 0;
203 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
205 size_t len = lstrlenW(str);
207 if (!len || !WCEL_Grow(ctx, len)) return;
208 if (ctx->len > ctx->ofs)
209 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
210 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
212 ctx->line[ctx->len] = 0;
213 WCEL_Update(ctx, ctx->ofs, ctx->len - ctx->ofs);
218 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
222 /* do not insert 0..31 control characters */
223 if (c < ' ' && c != '\t') return;
227 WCEL_InsertString(ctx, buffer);
230 static void WCEL_FreeYank(WCEL_Context* ctx)
232 HeapFree(GetProcessHeap(), 0, ctx->yanked);
236 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
239 if (len <= 0) return;
242 /* After WCEL_FreeYank ctx->yanked is empty */
243 ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
244 if (!ctx->yanked) return;
245 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
246 ctx->yanked[len] = 0;
249 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
250 * of the data lay in unicode lib
252 static inline BOOL WCEL_iswalnum(WCHAR wc)
254 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
257 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
260 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
261 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
266 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
269 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
270 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
271 return min(ofs, ctx->len);
274 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
278 if (idx == ctx->histSize - 1)
280 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
281 lstrcpyW(ptr, ctx->histCurr);
285 int len = CONSOLE_GetHistory(idx, NULL, 0);
287 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
289 CONSOLE_GetHistory(idx, ptr, len);
295 static void WCEL_HistoryInit(WCEL_Context* ctx)
297 ctx->histPos = CONSOLE_GetNumHistoryEntries();
298 ctx->histSize = ctx->histPos + 1;
299 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
302 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
304 WCHAR* data = WCEL_GetHistory(ctx, idx);
305 int len = lstrlenW(data) + 1;
307 /* save current line edition for recall when needed (FIXME seems broken to me) */
308 if (ctx->histPos == ctx->histSize - 1)
310 HeapFree(GetProcessHeap(), 0, ctx->histCurr);
311 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
312 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
314 /* need to clean also the screen if new string is shorter than old one */
315 WCEL_DeleteString(ctx, 0, ctx->len);
317 /* insert new string */
318 if (WCEL_Grow(ctx, len))
320 WCEL_InsertString(ctx, data);
321 HeapFree(GetProcessHeap(), 0, data);
326 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
328 int startPos = ctx->histPos;
330 unsigned int len, oldofs;
332 if (ctx->histPos && ctx->histPos == ctx->histSize) {
338 data = WCEL_GetHistory(ctx, ctx->histPos);
340 if (ctx->histPos) ctx->histPos--;
341 else ctx->histPos = (ctx->histSize-1);
343 len = lstrlenW(data) + 1;
344 if ((len >= ctx->ofs) &&
345 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
347 /* need to clean also the screen if new string is shorter than old one */
348 WCEL_DeleteString(ctx, 0, ctx->len);
350 if (WCEL_Grow(ctx, len))
354 WCEL_InsertString(ctx, data);
356 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
357 HeapFree(GetProcessHeap(), 0, data);
361 } while (ctx->histPos != startPos);
366 /* ====================================================================
368 * basic edition functions
370 * ====================================================================*/
372 static void WCEL_Done(WCEL_Context* ctx)
375 if (!WCEL_Grow(ctx, 1)) return;
376 ctx->line[ctx->len++] = '\n';
377 ctx->line[ctx->len] = 0;
378 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
382 static void WCEL_MoveLeft(WCEL_Context* ctx)
384 if (ctx->ofs > 0) ctx->ofs--;
387 static void WCEL_MoveRight(WCEL_Context* ctx)
389 if (ctx->ofs < ctx->len) ctx->ofs++;
392 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
394 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
395 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
398 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
400 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
401 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
404 static void WCEL_MoveToBeg(WCEL_Context* ctx)
409 static void WCEL_MoveToEnd(WCEL_Context* ctx)
414 static void WCEL_SetMark(WCEL_Context* ctx)
416 ctx->mark = ctx->ofs;
419 static void WCEL_ExchangeMark(WCEL_Context* ctx)
423 if (ctx->mark > ctx->len) return;
425 ctx->ofs = ctx->mark;
429 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
433 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
434 if (ctx->mark > ctx->ofs)
436 beg = ctx->ofs; end = ctx->mark;
440 beg = ctx->mark; end = ctx->ofs;
442 WCEL_SaveYank(ctx, beg, end);
445 static void WCEL_TransposeChar(WCEL_Context* ctx)
449 if (!ctx->ofs || ctx->ofs == ctx->len) return;
451 c = ctx->line[ctx->ofs];
452 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
453 ctx->line[ctx->ofs - 1] = c;
455 WCEL_Update(ctx, ctx->ofs - 1, 2);
459 static void WCEL_TransposeWords(WCEL_Context* ctx)
461 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
462 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
463 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
465 unsigned len_r = right_ofs - ctx->ofs;
466 unsigned len_l = ctx->ofs - left_ofs;
468 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
471 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
472 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
473 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
475 HeapFree(GetProcessHeap(), 0, tmp);
476 WCEL_Update(ctx, left_ofs, len_l + len_r);
477 ctx->ofs = right_ofs;
481 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
483 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
484 if (new_ofs != ctx->ofs)
487 for (i = ctx->ofs; i <= new_ofs; i++)
488 ctx->line[i] = tolowerW(ctx->line[i]);
489 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
494 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
496 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
497 if (new_ofs != ctx->ofs)
500 for (i = ctx->ofs; i <= new_ofs; i++)
501 ctx->line[i] = toupperW(ctx->line[i]);
502 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
507 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
509 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
510 if (new_ofs != ctx->ofs)
514 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
515 for (i = ctx->ofs + 1; i <= new_ofs; i++)
516 ctx->line[i] = tolowerW(ctx->line[i]);
517 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
522 static void WCEL_Yank(WCEL_Context* ctx)
524 WCEL_InsertString(ctx, ctx->yanked);
527 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
529 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
530 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
533 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
537 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
538 if (ctx->mark > ctx->ofs)
540 beg = ctx->ofs; end = ctx->mark;
544 beg = ctx->mark; end = ctx->ofs;
546 WCEL_SaveYank(ctx, beg, end);
547 WCEL_DeleteString(ctx, beg, end);
551 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
555 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
560 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
562 if (ctx->ofs < ctx->len)
563 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
566 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
568 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
569 if (new_ofs != ctx->ofs)
571 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
576 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
578 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
579 if (new_ofs != ctx->ofs)
581 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
585 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
587 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
590 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
592 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
595 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
597 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
600 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
602 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
605 static void WCEL_Redraw(WCEL_Context* ctx)
607 COORD c = WCEL_GetCoord(ctx, ctx->len);
610 WCEL_Update(ctx, 0, ctx->len);
612 ci.Char.UnicodeChar = ' ';
613 ci.Attributes = ctx->csbi.wAttributes;
615 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
618 static void WCEL_RepeatCount(WCEL_Context* ctx)
621 /* FIXME: wait until all console code is in kernel32 */
625 while (WCEL_Get(ctx, &ir, FALSE))
627 if (ir.EventType != KEY_EVENT) break;
628 if (ir.Event.KeyEvent.bKeyDown)
630 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
632 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
633 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
635 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
637 WCEL_Get(ctx, &ir, TRUE);
639 FIXME("=> %u\n", repeat);
643 /* ====================================================================
647 * ====================================================================*/
649 #define CTRL(x) ((x) - '@')
650 static KeyEntry StdKeyMap[] =
652 {/*BACK*/0x08, WCEL_DeletePrevChar },
653 {/*RETURN*/0x0d, WCEL_Done },
654 {/*DEL*/127, WCEL_DeleteCurrChar },
658 static KeyEntry Win32ExtraStdKeyMap[] =
660 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
665 static KeyEntry EmacsKeyMapCtrl[] =
667 { CTRL('@'), WCEL_SetMark },
668 { CTRL('A'), WCEL_MoveToBeg },
669 { CTRL('B'), WCEL_MoveLeft },
670 /* C: done in server */
671 { CTRL('D'), WCEL_DeleteCurrChar },
672 { CTRL('E'), WCEL_MoveToEnd },
673 { CTRL('F'), WCEL_MoveRight },
674 { CTRL('G'), WCEL_Beep },
675 { CTRL('H'), WCEL_DeletePrevChar },
676 /* I: meaningless (or tab ???) */
677 { CTRL('J'), WCEL_Done },
678 { CTRL('K'), WCEL_KillToEndOfLine },
679 { CTRL('L'), WCEL_Redraw },
680 { CTRL('M'), WCEL_Done },
681 { CTRL('N'), WCEL_MoveToNextHist },
682 /* O; insert line... meaningless */
683 { CTRL('P'), WCEL_MoveToPrevHist },
684 /* Q: [NIY] quoting... */
685 /* R: [NIY] search backwards... */
686 /* S: [NIY] search forwards... */
687 { CTRL('T'), WCEL_TransposeChar },
688 { CTRL('U'), WCEL_RepeatCount },
689 /* V: paragraph down... meaningless */
690 { CTRL('W'), WCEL_KillMarkedZone },
691 { CTRL('X'), WCEL_ExchangeMark },
692 { CTRL('Y'), WCEL_Yank },
697 static KeyEntry EmacsKeyMapAlt[] =
699 {/*DEL*/127, WCEL_DeleteLeftWord },
700 { '<', WCEL_MoveToFirstHist },
701 { '>', WCEL_MoveToLastHist },
703 { 'b', WCEL_MoveToLeftWord },
704 { 'c', WCEL_CapitalizeWord },
705 { 'd', WCEL_DeleteRightWord },
706 { 'f', WCEL_MoveToRightWord },
707 { 'l', WCEL_LowerCaseWord },
708 { 't', WCEL_TransposeWords },
709 { 'u', WCEL_UpperCaseWord },
710 { 'w', WCEL_CopyMarkedZone },
714 static KeyEntry EmacsKeyMapExtended[] =
716 {/*RETURN*/ 0x0d, WCEL_Done },
717 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
718 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
719 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
720 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
721 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
722 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
723 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
727 static KeyMap EmacsKeyMap[] =
729 {0x00000000, 1, StdKeyMap},
730 {0x00000001, 1, EmacsKeyMapAlt}, /* left alt */
731 {0x00000002, 1, EmacsKeyMapAlt}, /* right alt */
732 {0x00000004, 1, EmacsKeyMapCtrl}, /* left ctrl */
733 {0x00000008, 1, EmacsKeyMapCtrl}, /* right ctrl */
734 {0x00000100, 0, EmacsKeyMapExtended},
738 static KeyEntry Win32KeyMapExtended[] =
740 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
741 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
742 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
743 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
744 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
745 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
746 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
750 static KeyEntry Win32KeyMapCtrlExtended[] =
752 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
753 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
754 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
758 KeyMap Win32KeyMap[] =
760 {0x00000000, 1, StdKeyMap},
761 {0x00000000, 0, Win32ExtraStdKeyMap},
762 {0x00000100, 0, Win32KeyMapExtended},
763 {0x00000104, 0, Win32KeyMapCtrlExtended},
764 {0x00000108, 0, Win32KeyMapCtrlExtended},
769 /* ====================================================================
771 * Read line master function
773 * ====================================================================*/
775 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn)
782 void (*func)(struct WCEL_Context* ctx);
786 memset(&ctx, 0, sizeof(ctx));
787 ctx.hConIn = hConsoleIn;
788 WCEL_HistoryInit(&ctx);
790 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
793 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
794 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
795 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
797 ctx.can_wrap = (GetConsoleMode(ctx.hConOut, &ks) && (ks & ENABLE_WRAP_AT_EOL_OUTPUT)) ? 1 : 0;
799 if (!WCEL_Grow(&ctx, 1))
801 CloseHandle(ctx.hConOut);
806 /* EPP WCEL_Dump(&ctx, "init"); */
808 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
810 if (ir.EventType != KEY_EVENT) continue;
811 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08lx\n",
812 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
813 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
814 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
815 if (!ir.Event.KeyEvent.bKeyDown) continue;
817 /* EPP WCEL_Dump(&ctx, "before func"); */
819 /* mask out some bits which don't interest us */
820 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON);
823 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
825 if (km->keyState != ks)
829 for (ke = &km->entries[0]; ke->func != 0; ke++)
830 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
834 for (ke = &km->entries[0]; ke->func != 0; ke++)
835 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
847 else if (!(ir.Event.KeyEvent.dwControlKeyState & (ENHANCED_KEY|LEFT_ALT_PRESSED)))
848 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
849 else TRACE("Dropped event\n");
851 /* EPP WCEL_Dump(&ctx, "after func"); */
853 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
857 HeapFree(GetProcessHeap(), 0, ctx.line);
862 CONSOLE_AppendHistory(ctx.line);
864 CloseHandle(ctx.hConOut);
865 HeapFree(GetProcessHeap(), 0, ctx.histCurr);