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
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 const 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 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) */
66 unsigned done : 1, /* to 1 when we're done with editing */
67 error : 1, /* to 1 when an error occurred in the editing */
68 can_wrap : 1, /* to 1 when multi-line edition can take place */
69 shall_echo : 1, /* to 1 when characters should be echo:ed when keyed-in */
70 can_pos_cursor : 1; /* to 1 when console can (re)position cursor */
77 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
79 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
80 "\t\thist=(size=%u pos=%u curr=%s)\n"
82 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
83 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
84 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
85 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
86 debugstr_w(ctx->yanked));
90 /* ====================================================================
92 * Console helper functions
94 * ====================================================================*/
96 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
98 if (ReadConsoleInputW(ctx->hConIn, ir, 1, NULL)) return TRUE;
99 ERR("hmm bad situation\n");
104 static inline void WCEL_Beep(WCEL_Context* ctx)
109 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
111 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
114 static inline int WCEL_CharWidth(WCHAR wch)
116 return wch < ' ' ? 2 : 1;
119 static inline int WCEL_StringWidth(const WCHAR* str, int beg, int len)
123 for (i = 0, ofs = 0; i < len; i++)
124 ofs += WCEL_CharWidth(str[beg + i]);
128 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int strofs)
131 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
134 ofs = WCEL_StringWidth(ctx->line, 0, strofs);
136 c.Y = ctx->csbi.dwCursorPosition.Y;
140 c.X = ofs % ctx->csbi.dwSize.X;
141 c.Y += 1 + ofs / ctx->csbi.dwSize.X;
143 else c.X = ctx->csbi.dwCursorPosition.X + ofs;
147 static DWORD WCEL_WriteConsole(WCEL_Context* ctx, DWORD beg, DWORD len)
149 DWORD i, last, dw, ret = 0;
152 for (i = last = 0; i < len; i++)
154 if (ctx->line[beg + i] < ' ')
158 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], i - last, &dw, NULL);
162 tmp[1] = '@' + ctx->line[beg + i];
163 WriteConsoleW(ctx->hConOut, tmp, 2, &dw, NULL);
170 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], len - last, &dw, NULL);
176 static inline void WCEL_WriteNChars(WCEL_Context* ctx, char ch, int count)
182 while (count--) WriteFile(ctx->hConOut, &ch, 1, &dw, NULL);
186 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
191 /* bare console case is handled in CONSOLE_ReadLine (we always reprint the whole string) */
192 if (!ctx->shall_echo || !ctx->can_pos_cursor) return;
194 for (i = last = beg; i < beg + len; i++)
196 if (ctx->line[i] < ' ')
200 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
201 WCEL_GetCoord(ctx, last), NULL);
202 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
203 WCEL_GetCoord(ctx, last), NULL);
206 tmp[1] = '@' + ctx->line[i];
207 WriteConsoleOutputCharacterW(ctx->hConOut, tmp, 2,
208 WCEL_GetCoord(ctx, i), NULL);
209 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, 2,
210 WCEL_GetCoord(ctx, i), NULL);
214 if (last != beg + len)
216 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
217 WCEL_GetCoord(ctx, last), NULL);
218 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
219 WCEL_GetCoord(ctx, last), NULL);
223 /* ====================================================================
225 * context manipulation functions
227 * ====================================================================*/
229 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
231 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
233 FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n");
237 if (ctx->len + len >= ctx->alloc)
242 /* round up size to 32 byte-WCHAR boundary */
243 newsize = (ctx->len + len + 1 + 31) & ~31;
246 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
248 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
250 if (!newline) return FALSE;
252 ctx->alloc = newsize;
257 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
259 unsigned str_len = end - beg;
262 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
263 /* we need to clean from ctx->len - str_len to ctx->len */
267 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
268 COORD cend = WCEL_GetCoord(ctx, ctx->len);
271 ci.Char.UnicodeChar = ' ';
272 ci.Attributes = ctx->csbi.wAttributes;
274 if (cbeg.Y == cend.Y)
276 /* partial erase of sole line */
277 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
278 cend.X - cbeg.X, &ci);
283 /* erase til eol on first line */
284 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
285 ctx->csbi.dwSize.X - cbeg.X, &ci);
286 /* completely erase all the others (full lines) */
287 for (i = cbeg.Y + 1; i < cend.Y; i++)
288 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
289 /* erase from beginning of line until last pos on last line */
290 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
294 WCEL_Update(ctx, 0, ctx->len);
295 ctx->line[ctx->len] = 0;
298 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
300 size_t len = lstrlenW(str);
302 if (!len || !WCEL_Grow(ctx, len)) return;
303 if (ctx->len > ctx->ofs)
304 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
305 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
307 ctx->line[ctx->len] = 0;
308 WCEL_Update(ctx, ctx->ofs, ctx->len - ctx->ofs);
313 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
319 WCEL_InsertString(ctx, buffer);
322 static void WCEL_FreeYank(WCEL_Context* ctx)
324 HeapFree(GetProcessHeap(), 0, ctx->yanked);
328 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
331 if (len <= 0) return;
334 /* After WCEL_FreeYank ctx->yanked is empty */
335 ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
336 if (!ctx->yanked) return;
337 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
338 ctx->yanked[len] = 0;
341 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
342 * of the data lay in unicode lib
344 static inline BOOL WCEL_iswalnum(WCHAR wc)
346 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
349 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
352 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
353 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
358 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
361 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
362 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
363 return min(ofs, ctx->len);
366 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
370 if (idx == ctx->histSize - 1)
372 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
373 lstrcpyW(ptr, ctx->histCurr);
377 int len = CONSOLE_GetHistory(idx, NULL, 0);
379 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
381 CONSOLE_GetHistory(idx, ptr, len);
387 static void WCEL_HistoryInit(WCEL_Context* ctx)
389 ctx->histPos = CONSOLE_GetNumHistoryEntries();
390 ctx->histSize = ctx->histPos + 1;
391 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
394 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
396 WCHAR* data = WCEL_GetHistory(ctx, idx);
397 int len = lstrlenW(data) + 1;
399 /* save current line edition for recall when needed (FIXME seems broken to me) */
400 if (ctx->histPos == ctx->histSize - 1)
402 HeapFree(GetProcessHeap(), 0, ctx->histCurr);
403 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
404 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
406 /* need to clean also the screen if new string is shorter than old one */
407 WCEL_DeleteString(ctx, 0, ctx->len);
409 /* insert new string */
410 if (WCEL_Grow(ctx, len))
412 WCEL_InsertString(ctx, data);
413 HeapFree(GetProcessHeap(), 0, data);
418 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
420 int startPos = ctx->histPos;
422 unsigned int len, oldofs;
424 if (ctx->histPos && ctx->histPos == ctx->histSize) {
430 data = WCEL_GetHistory(ctx, ctx->histPos);
432 if (ctx->histPos) ctx->histPos--;
433 else ctx->histPos = (ctx->histSize-1);
435 len = lstrlenW(data) + 1;
436 if ((len >= ctx->ofs) &&
437 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
439 /* need to clean also the screen if new string is shorter than old one */
440 WCEL_DeleteString(ctx, 0, ctx->len);
442 if (WCEL_Grow(ctx, len))
446 WCEL_InsertString(ctx, data);
449 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
450 HeapFree(GetProcessHeap(), 0, data);
454 } while (ctx->histPos != startPos);
459 /* ====================================================================
461 * basic edition functions
463 * ====================================================================*/
465 static void WCEL_Done(WCEL_Context* ctx)
468 if (!WCEL_Grow(ctx, 2)) return;
469 ctx->line[ctx->len++] = '\r';
470 ctx->line[ctx->len++] = '\n';
471 ctx->line[ctx->len] = 0;
472 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
476 static void WCEL_MoveLeft(WCEL_Context* ctx)
478 if (ctx->ofs > 0) ctx->ofs--;
481 static void WCEL_MoveRight(WCEL_Context* ctx)
483 if (ctx->ofs < ctx->len) ctx->ofs++;
486 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
488 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
489 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
492 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
494 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
495 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
498 static void WCEL_MoveToBeg(WCEL_Context* ctx)
503 static void WCEL_MoveToEnd(WCEL_Context* ctx)
508 static void WCEL_SetMark(WCEL_Context* ctx)
510 ctx->mark = ctx->ofs;
513 static void WCEL_ExchangeMark(WCEL_Context* ctx)
517 if (ctx->mark > ctx->len) return;
519 ctx->ofs = ctx->mark;
523 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
527 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
528 if (ctx->mark > ctx->ofs)
530 beg = ctx->ofs; end = ctx->mark;
534 beg = ctx->mark; end = ctx->ofs;
536 WCEL_SaveYank(ctx, beg, end);
539 static void WCEL_TransposeChar(WCEL_Context* ctx)
543 if (!ctx->ofs || ctx->ofs == ctx->len) return;
545 c = ctx->line[ctx->ofs];
546 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
547 ctx->line[ctx->ofs - 1] = c;
549 WCEL_Update(ctx, ctx->ofs - 1, 2);
553 static void WCEL_TransposeWords(WCEL_Context* ctx)
555 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
556 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
557 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
559 unsigned len_r = right_ofs - ctx->ofs;
560 unsigned len_l = ctx->ofs - left_ofs;
562 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
565 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
566 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
567 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
569 HeapFree(GetProcessHeap(), 0, tmp);
570 WCEL_Update(ctx, left_ofs, len_l + len_r);
571 ctx->ofs = right_ofs;
575 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
577 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
578 if (new_ofs != ctx->ofs)
581 for (i = ctx->ofs; i <= new_ofs; i++)
582 ctx->line[i] = tolowerW(ctx->line[i]);
583 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
588 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
590 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
591 if (new_ofs != ctx->ofs)
594 for (i = ctx->ofs; i <= new_ofs; i++)
595 ctx->line[i] = toupperW(ctx->line[i]);
596 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
601 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
603 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
604 if (new_ofs != ctx->ofs)
608 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
609 for (i = ctx->ofs + 1; i <= new_ofs; i++)
610 ctx->line[i] = tolowerW(ctx->line[i]);
611 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
616 static void WCEL_Yank(WCEL_Context* ctx)
618 WCEL_InsertString(ctx, ctx->yanked);
621 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
623 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
624 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
627 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
631 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
632 if (ctx->mark > ctx->ofs)
634 beg = ctx->ofs; end = ctx->mark;
638 beg = ctx->mark; end = ctx->ofs;
640 WCEL_SaveYank(ctx, beg, end);
641 WCEL_DeleteString(ctx, beg, end);
645 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
649 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
654 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
656 if (ctx->ofs < ctx->len)
657 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
660 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
662 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
663 if (new_ofs != ctx->ofs)
665 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
670 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
672 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
673 if (new_ofs != ctx->ofs)
675 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
679 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
681 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
684 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
686 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
689 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
691 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
694 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
696 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
699 static void WCEL_Redraw(WCEL_Context* ctx)
703 COORD c = WCEL_GetCoord(ctx, ctx->len);
706 WCEL_Update(ctx, 0, ctx->len);
708 ci.Char.UnicodeChar = ' ';
709 ci.Attributes = ctx->csbi.wAttributes;
711 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
715 static void WCEL_RepeatCount(WCEL_Context* ctx)
718 /* FIXME: wait until all console code is in kernel32 */
722 while (WCEL_Get(ctx, &ir, FALSE))
724 if (ir.EventType != KEY_EVENT) break;
725 if (ir.Event.KeyEvent.bKeyDown)
727 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
729 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
730 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
732 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
734 WCEL_Get(ctx, &ir, TRUE);
736 FIXME("=> %u\n", repeat);
740 /* ====================================================================
744 * ====================================================================*/
746 #define CTRL(x) ((x) - '@')
747 static const KeyEntry StdKeyMap[] =
749 {/*BACK*/0x08, WCEL_DeletePrevChar },
750 {/*RETURN*/0x0d, WCEL_Done },
751 {/*DEL*/127, WCEL_DeleteCurrChar },
755 static const KeyEntry EmacsKeyMapCtrl[] =
757 { CTRL('@'), WCEL_SetMark },
758 { CTRL('A'), WCEL_MoveToBeg },
759 { CTRL('B'), WCEL_MoveLeft },
760 /* C: done in server */
761 { CTRL('D'), WCEL_DeleteCurrChar },
762 { CTRL('E'), WCEL_MoveToEnd },
763 { CTRL('F'), WCEL_MoveRight },
764 { CTRL('G'), WCEL_Beep },
765 { CTRL('H'), WCEL_DeletePrevChar },
766 /* I: meaningless (or tab ???) */
767 { CTRL('J'), WCEL_Done },
768 { CTRL('K'), WCEL_KillToEndOfLine },
769 { CTRL('L'), WCEL_Redraw },
770 { CTRL('M'), WCEL_Done },
771 { CTRL('N'), WCEL_MoveToNextHist },
772 /* O; insert line... meaningless */
773 { CTRL('P'), WCEL_MoveToPrevHist },
774 /* Q: [NIY] quoting... */
775 /* R: [NIY] search backwards... */
776 /* S: [NIY] search forwards... */
777 { CTRL('T'), WCEL_TransposeChar },
778 { CTRL('U'), WCEL_RepeatCount },
779 /* V: paragraph down... meaningless */
780 { CTRL('W'), WCEL_KillMarkedZone },
781 { CTRL('X'), WCEL_ExchangeMark },
782 { CTRL('Y'), WCEL_Yank },
787 static const KeyEntry EmacsKeyMapAlt[] =
789 {/*DEL*/127, WCEL_DeleteLeftWord },
790 { '<', WCEL_MoveToFirstHist },
791 { '>', WCEL_MoveToLastHist },
793 { 'b', WCEL_MoveToLeftWord },
794 { 'c', WCEL_CapitalizeWord },
795 { 'd', WCEL_DeleteRightWord },
796 { 'f', WCEL_MoveToRightWord },
797 { 'l', WCEL_LowerCaseWord },
798 { 't', WCEL_TransposeWords },
799 { 'u', WCEL_UpperCaseWord },
800 { 'w', WCEL_CopyMarkedZone },
804 static const KeyEntry EmacsStdKeyMap[] =
806 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
807 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
808 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
809 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
810 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
811 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
812 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
816 static const KeyMap EmacsKeyMap[] =
819 {0, 0, EmacsStdKeyMap},
820 {RIGHT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* right alt */
821 {LEFT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* left alt */
822 {RIGHT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* right ctrl */
823 {LEFT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* left ctrl */
827 static const KeyEntry Win32StdKeyMap[] =
829 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
830 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
831 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
832 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
833 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
834 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
835 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
836 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
840 static const KeyEntry Win32KeyMapCtrl[] =
842 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
843 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
844 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
848 static const KeyMap Win32KeyMap[] =
851 {0, 0, Win32StdKeyMap},
852 {RIGHT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
853 {LEFT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
858 /* ====================================================================
860 * Read line master function
862 * ====================================================================*/
864 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, BOOL can_pos_cursor)
871 void (*func)(struct WCEL_Context* ctx);
875 memset(&ctx, 0, sizeof(ctx));
876 ctx.hConIn = hConsoleIn;
877 WCEL_HistoryInit(&ctx);
879 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
882 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
883 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
884 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
886 ctx.shall_echo = (GetConsoleMode(hConsoleIn, &ks) && (ks & ENABLE_ECHO_INPUT)) ? 1 : 0;
887 ctx.can_wrap = (GetConsoleMode(ctx.hConOut, &ks) && (ks & ENABLE_WRAP_AT_EOL_OUTPUT)) ? 1 : 0;
888 ctx.can_pos_cursor = can_pos_cursor;
890 if (!WCEL_Grow(&ctx, 1))
892 CloseHandle(ctx.hConOut);
897 /* EPP WCEL_Dump(&ctx, "init"); */
899 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
901 if (ir.EventType != KEY_EVENT) continue;
902 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
903 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
904 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
905 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
906 if (!ir.Event.KeyEvent.bKeyDown) continue;
908 /* EPP WCEL_Dump(&ctx, "before func"); */
910 /* mask out some bits which don't interest us */
911 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
914 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
916 if (km->keyState != ks)
920 for (ke = &km->entries[0]; ke->func != 0; ke++)
921 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
925 for (ke = &km->entries[0]; ke->func != 0; ke++)
926 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
938 else if (!(ir.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED))
939 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
940 else TRACE("Dropped event\n");
942 /* EPP WCEL_Dump(&ctx, "after func"); */
943 if (!ctx.shall_echo) continue;
944 if (ctx.can_pos_cursor)
947 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
949 else if (!ctx.done && !ctx.error)
952 /* erase previous chars */
953 WCEL_WriteNChars(&ctx, '\b', ctx.last_rub);
955 /* write chars up to cursor */
956 ctx.last_rub = WCEL_WriteConsole(&ctx, 0, ctx.ofs);
957 /* write chars past cursor */
958 last = ctx.last_rub + WCEL_WriteConsole(&ctx, ctx.ofs, ctx.len - ctx.ofs);
959 if (last < ctx.last_max) /* ctx.line has been shortened, erase */
961 WCEL_WriteNChars(&ctx, ' ', ctx.last_max - last);
962 WCEL_WriteNChars(&ctx, '\b', ctx.last_max - last);
965 else ctx.last_max = last;
966 /* reposition at cursor */
967 WCEL_WriteNChars(&ctx, '\b', last - ctx.last_rub);
972 HeapFree(GetProcessHeap(), 0, ctx.line);
977 CONSOLE_AppendHistory(ctx.line);
979 CloseHandle(ctx.hConOut);
980 HeapFree(GetProcessHeap(), 0, ctx.histCurr);