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 insert : 1, /* to 1 when new characters are inserted (otherwise overwrite) */
71 can_pos_cursor : 1; /* to 1 when console can (re)position cursor */
78 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
80 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
81 "\t\thist=(size=%u pos=%u curr=%s)\n"
83 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
84 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
85 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
86 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
87 debugstr_w(ctx->yanked));
91 /* ====================================================================
93 * Console helper functions
95 * ====================================================================*/
97 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
99 if (ReadConsoleInputW(ctx->hConIn, ir, 1, NULL)) return TRUE;
100 ERR("hmm bad situation\n");
105 static inline void WCEL_Beep(WCEL_Context* ctx)
110 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
112 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
115 static inline int WCEL_CharWidth(WCHAR wch)
117 return wch < ' ' ? 2 : 1;
120 static inline int WCEL_StringWidth(const WCHAR* str, int beg, int len)
124 for (i = 0, ofs = 0; i < len; i++)
125 ofs += WCEL_CharWidth(str[beg + i]);
129 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int strofs)
132 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
135 ofs = WCEL_StringWidth(ctx->line, 0, strofs);
137 c.Y = ctx->csbi.dwCursorPosition.Y;
141 c.X = ofs % ctx->csbi.dwSize.X;
142 c.Y += 1 + ofs / ctx->csbi.dwSize.X;
144 else c.X = ctx->csbi.dwCursorPosition.X + ofs;
148 static DWORD WCEL_WriteConsole(WCEL_Context* ctx, DWORD beg, DWORD len)
150 DWORD i, last, dw, ret = 0;
153 for (i = last = 0; i < len; i++)
155 if (ctx->line[beg + i] < ' ')
159 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], i - last, &dw, NULL);
163 tmp[1] = '@' + ctx->line[beg + i];
164 WriteConsoleW(ctx->hConOut, tmp, 2, &dw, NULL);
171 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], len - last, &dw, NULL);
177 static inline void WCEL_WriteNChars(WCEL_Context* ctx, char ch, int count)
183 while (count--) WriteFile(ctx->hConOut, &ch, 1, &dw, NULL);
187 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
193 /* bare console case is handled in CONSOLE_ReadLine (we always reprint the whole string) */
194 if (!ctx->shall_echo || !ctx->can_pos_cursor) return;
196 for (i = last = beg; i < beg + len; i++)
198 if (ctx->line[i] < ' ')
202 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
203 WCEL_GetCoord(ctx, last), &count);
204 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
205 WCEL_GetCoord(ctx, last), &count);
208 tmp[1] = '@' + ctx->line[i];
209 WriteConsoleOutputCharacterW(ctx->hConOut, tmp, 2,
210 WCEL_GetCoord(ctx, i), &count);
211 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, 2,
212 WCEL_GetCoord(ctx, i), &count);
216 if (last != beg + len)
218 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
219 WCEL_GetCoord(ctx, last), &count);
220 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
221 WCEL_GetCoord(ctx, last), &count);
225 /* ====================================================================
227 * context manipulation functions
229 * ====================================================================*/
231 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
233 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
235 FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n");
239 if (ctx->len + len >= ctx->alloc)
244 /* round up size to 32 byte-WCHAR boundary */
245 newsize = (ctx->len + len + 1 + 31) & ~31;
248 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
250 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
252 if (!newline) return FALSE;
254 ctx->alloc = newsize;
259 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
261 unsigned str_len = end - beg;
264 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
265 /* we need to clean from ctx->len - str_len to ctx->len */
269 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
270 COORD cend = WCEL_GetCoord(ctx, ctx->len);
273 ci.Char.UnicodeChar = ' ';
274 ci.Attributes = ctx->csbi.wAttributes;
276 if (cbeg.Y == cend.Y)
278 /* partial erase of sole line */
279 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
280 cend.X - cbeg.X, &ci);
285 /* erase til eol on first line */
286 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
287 ctx->csbi.dwSize.X - cbeg.X, &ci);
288 /* completely erase all the others (full lines) */
289 for (i = cbeg.Y + 1; i < cend.Y; i++)
290 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
291 /* erase from beginning of line until last pos on last line */
292 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
296 WCEL_Update(ctx, 0, ctx->len);
297 ctx->line[ctx->len] = 0;
300 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
302 size_t len = lstrlenW(str), updtlen;
307 if (!WCEL_Grow(ctx, len)) return;
308 if (ctx->len > ctx->ofs)
309 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
311 updtlen = ctx->len - ctx->ofs;
315 if (ctx->ofs + len > ctx->len)
317 if (!WCEL_Grow(ctx, (ctx->ofs + len) - ctx->len)) return;
318 ctx->len = ctx->ofs + len;
322 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
323 ctx->line[ctx->len] = 0;
324 WCEL_Update(ctx, ctx->ofs, updtlen);
328 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
334 WCEL_InsertString(ctx, buffer);
337 static void WCEL_FreeYank(WCEL_Context* ctx)
339 HeapFree(GetProcessHeap(), 0, ctx->yanked);
343 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
346 if (len <= 0) return;
349 /* After WCEL_FreeYank ctx->yanked is empty */
350 ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
351 if (!ctx->yanked) return;
352 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
353 ctx->yanked[len] = 0;
356 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
357 * of the data lay in unicode lib
359 static inline BOOL WCEL_iswalnum(WCHAR wc)
361 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
364 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
367 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
368 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
373 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
376 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
377 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
378 return min(ofs, ctx->len);
381 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
385 if (idx == ctx->histSize - 1)
387 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
388 lstrcpyW(ptr, ctx->histCurr);
392 int len = CONSOLE_GetHistory(idx, NULL, 0);
394 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
396 CONSOLE_GetHistory(idx, ptr, len);
402 static void WCEL_HistoryInit(WCEL_Context* ctx)
404 ctx->histPos = CONSOLE_GetNumHistoryEntries();
405 ctx->histSize = ctx->histPos + 1;
406 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
409 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
411 WCHAR* data = WCEL_GetHistory(ctx, idx);
412 int len = lstrlenW(data) + 1;
414 /* save current line edition for recall when needed (FIXME seems broken to me) */
415 if (ctx->histPos == ctx->histSize - 1)
417 HeapFree(GetProcessHeap(), 0, ctx->histCurr);
418 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
419 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
421 /* need to clean also the screen if new string is shorter than old one */
422 WCEL_DeleteString(ctx, 0, ctx->len);
424 /* insert new string */
425 if (WCEL_Grow(ctx, len))
427 WCEL_InsertString(ctx, data);
428 HeapFree(GetProcessHeap(), 0, data);
433 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
435 int startPos = ctx->histPos;
437 unsigned int len, oldofs;
439 if (ctx->histPos && ctx->histPos == ctx->histSize) {
445 data = WCEL_GetHistory(ctx, ctx->histPos);
447 if (ctx->histPos) ctx->histPos--;
448 else ctx->histPos = (ctx->histSize-1);
450 len = lstrlenW(data) + 1;
451 if ((len >= ctx->ofs) &&
452 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
454 /* need to clean also the screen if new string is shorter than old one */
455 WCEL_DeleteString(ctx, 0, ctx->len);
457 if (WCEL_Grow(ctx, len))
461 WCEL_InsertString(ctx, data);
464 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
465 HeapFree(GetProcessHeap(), 0, data);
469 } while (ctx->histPos != startPos);
474 /* ====================================================================
476 * basic edition functions
478 * ====================================================================*/
480 static void WCEL_Done(WCEL_Context* ctx)
483 if (!WCEL_Grow(ctx, 2)) return;
484 ctx->line[ctx->len++] = '\r';
485 ctx->line[ctx->len++] = '\n';
486 ctx->line[ctx->len] = 0;
487 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
491 static void WCEL_MoveLeft(WCEL_Context* ctx)
493 if (ctx->ofs > 0) ctx->ofs--;
496 static void WCEL_MoveRight(WCEL_Context* ctx)
498 if (ctx->ofs < ctx->len) ctx->ofs++;
501 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
503 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
504 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
507 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
509 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
510 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
513 static void WCEL_MoveToBeg(WCEL_Context* ctx)
518 static void WCEL_MoveToEnd(WCEL_Context* ctx)
523 static void WCEL_SetMark(WCEL_Context* ctx)
525 ctx->mark = ctx->ofs;
528 static void WCEL_ExchangeMark(WCEL_Context* ctx)
532 if (ctx->mark > ctx->len) return;
534 ctx->ofs = ctx->mark;
538 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
542 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
543 if (ctx->mark > ctx->ofs)
545 beg = ctx->ofs; end = ctx->mark;
549 beg = ctx->mark; end = ctx->ofs;
551 WCEL_SaveYank(ctx, beg, end);
554 static void WCEL_TransposeChar(WCEL_Context* ctx)
558 if (!ctx->ofs || ctx->ofs == ctx->len) return;
560 c = ctx->line[ctx->ofs];
561 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
562 ctx->line[ctx->ofs - 1] = c;
564 WCEL_Update(ctx, ctx->ofs - 1, 2);
568 static void WCEL_TransposeWords(WCEL_Context* ctx)
570 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
571 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
572 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
574 unsigned len_r = right_ofs - ctx->ofs;
575 unsigned len_l = ctx->ofs - left_ofs;
577 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
580 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
581 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
582 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
584 HeapFree(GetProcessHeap(), 0, tmp);
585 WCEL_Update(ctx, left_ofs, len_l + len_r);
586 ctx->ofs = right_ofs;
590 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
592 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
593 if (new_ofs != ctx->ofs)
596 for (i = ctx->ofs; i <= new_ofs; i++)
597 ctx->line[i] = tolowerW(ctx->line[i]);
598 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
603 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
605 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
606 if (new_ofs != ctx->ofs)
609 for (i = ctx->ofs; i <= new_ofs; i++)
610 ctx->line[i] = toupperW(ctx->line[i]);
611 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
616 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
618 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
619 if (new_ofs != ctx->ofs)
623 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
624 for (i = ctx->ofs + 1; i <= new_ofs; i++)
625 ctx->line[i] = tolowerW(ctx->line[i]);
626 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
631 static void WCEL_Yank(WCEL_Context* ctx)
633 WCEL_InsertString(ctx, ctx->yanked);
636 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
638 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
639 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
642 static void WCEL_KillFromBegOfLine(WCEL_Context* ctx)
646 WCEL_SaveYank(ctx, 0, ctx->ofs);
647 WCEL_DeleteString(ctx, 0, ctx->ofs);
652 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
656 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
657 if (ctx->mark > ctx->ofs)
659 beg = ctx->ofs; end = ctx->mark;
663 beg = ctx->mark; end = ctx->ofs;
665 WCEL_SaveYank(ctx, beg, end);
666 WCEL_DeleteString(ctx, beg, end);
670 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
674 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
679 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
681 if (ctx->ofs < ctx->len)
682 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
685 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
687 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
688 if (new_ofs != ctx->ofs)
690 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
695 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
697 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
698 if (new_ofs != ctx->ofs)
700 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
704 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
706 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
709 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
711 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
714 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
716 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
719 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
721 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
724 static void WCEL_Redraw(WCEL_Context* ctx)
728 COORD c = WCEL_GetCoord(ctx, ctx->len);
731 WCEL_Update(ctx, 0, ctx->len);
733 ci.Char.UnicodeChar = ' ';
734 ci.Attributes = ctx->csbi.wAttributes;
736 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
740 static void WCEL_RepeatCount(WCEL_Context* ctx)
743 /* FIXME: wait until all console code is in kernel32 */
747 while (WCEL_Get(ctx, &ir, FALSE))
749 if (ir.EventType != KEY_EVENT) break;
750 if (ir.Event.KeyEvent.bKeyDown)
752 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
754 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
755 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
757 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
759 WCEL_Get(ctx, &ir, TRUE);
761 FIXME("=> %u\n", repeat);
765 static void WCEL_ToggleInsert(WCEL_Context* ctx)
768 CONSOLE_CURSOR_INFO cinfo;
770 if (GetConsoleMode(ctx->hConIn, &mode) && GetConsoleCursorInfo(ctx->hConOut, &cinfo))
772 if ((mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) == (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS))
774 mode &= ~ENABLE_INSERT_MODE;
780 mode |= ENABLE_INSERT_MODE | ENABLE_EXTENDED_FLAGS;
784 SetConsoleMode(ctx->hConIn, mode);
785 SetConsoleCursorInfo(ctx->hConOut, &cinfo);
789 /* ====================================================================
793 * ====================================================================*/
795 #define CTRL(x) ((x) - '@')
796 static const KeyEntry StdKeyMap[] =
798 {/*BACK*/0x08, WCEL_DeletePrevChar },
799 {/*RETURN*/0x0d, WCEL_Done },
800 {/*DEL*/127, WCEL_DeleteCurrChar },
804 static const KeyEntry EmacsKeyMapCtrl[] =
806 { CTRL('@'), WCEL_SetMark },
807 { CTRL('A'), WCEL_MoveToBeg },
808 { CTRL('B'), WCEL_MoveLeft },
809 /* C: done in server */
810 { CTRL('D'), WCEL_DeleteCurrChar },
811 { CTRL('E'), WCEL_MoveToEnd },
812 { CTRL('F'), WCEL_MoveRight },
813 { CTRL('G'), WCEL_Beep },
814 { CTRL('H'), WCEL_DeletePrevChar },
815 /* I: meaningless (or tab ???) */
816 { CTRL('J'), WCEL_Done },
817 { CTRL('K'), WCEL_KillToEndOfLine },
818 { CTRL('L'), WCEL_Redraw },
819 { CTRL('M'), WCEL_Done },
820 { CTRL('N'), WCEL_MoveToNextHist },
821 /* O; insert line... meaningless */
822 { CTRL('P'), WCEL_MoveToPrevHist },
823 /* Q: [NIY] quoting... */
824 /* R: [NIY] search backwards... */
825 /* S: [NIY] search forwards... */
826 { CTRL('T'), WCEL_TransposeChar },
827 { CTRL('U'), WCEL_RepeatCount },
828 /* V: paragraph down... meaningless */
829 { CTRL('W'), WCEL_KillMarkedZone },
830 { CTRL('X'), WCEL_ExchangeMark },
831 { CTRL('Y'), WCEL_Yank },
836 static const KeyEntry EmacsKeyMapAlt[] =
838 {/*DEL*/127, WCEL_DeleteLeftWord },
839 { '<', WCEL_MoveToFirstHist },
840 { '>', WCEL_MoveToLastHist },
842 { 'b', WCEL_MoveToLeftWord },
843 { 'c', WCEL_CapitalizeWord },
844 { 'd', WCEL_DeleteRightWord },
845 { 'f', WCEL_MoveToRightWord },
846 { 'l', WCEL_LowerCaseWord },
847 { 't', WCEL_TransposeWords },
848 { 'u', WCEL_UpperCaseWord },
849 { 'w', WCEL_CopyMarkedZone },
853 static const KeyEntry EmacsStdKeyMap[] =
855 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
856 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
857 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
858 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
859 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
860 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
861 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
862 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert },
866 static const KeyMap EmacsKeyMap[] =
869 {0, 0, EmacsStdKeyMap},
870 {RIGHT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* right alt */
871 {LEFT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* left alt */
872 {RIGHT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* right ctrl */
873 {LEFT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* left ctrl */
877 static const KeyEntry Win32StdKeyMap[] =
879 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
880 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
881 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
882 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
883 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
884 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
885 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
886 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert },
887 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
891 static const KeyEntry Win32KeyMapCtrl[] =
893 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
894 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
895 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
896 {/*VK_HOME*/ 0x24, WCEL_KillFromBegOfLine },
900 static const KeyMap Win32KeyMap[] =
903 {0, 0, Win32StdKeyMap},
904 {RIGHT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
905 {LEFT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
910 /* ====================================================================
912 * Read line master function
914 * ====================================================================*/
916 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, BOOL can_pos_cursor)
923 void (*func)(struct WCEL_Context* ctx);
927 memset(&ctx, 0, sizeof(ctx));
928 ctx.hConIn = hConsoleIn;
929 WCEL_HistoryInit(&ctx);
931 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
934 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
935 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
936 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
938 if (!GetConsoleMode(hConsoleIn, &mode)) mode = 0;
939 ctx.shall_echo = (mode & ENABLE_ECHO_INPUT) ? 1 : 0;
940 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) == (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS) ? 1 : 0;
941 if (!GetConsoleMode(ctx.hConOut, &mode)) mode = 0;
942 ctx.can_wrap = (mode & ENABLE_WRAP_AT_EOL_OUTPUT) ? 1 : 0;
943 ctx.can_pos_cursor = can_pos_cursor;
945 if (!WCEL_Grow(&ctx, 1))
947 CloseHandle(ctx.hConOut);
952 /* EPP WCEL_Dump(&ctx, "init"); */
954 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
956 if (ir.EventType != KEY_EVENT) continue;
957 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
958 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
959 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
960 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
961 if (!ir.Event.KeyEvent.bKeyDown) continue;
963 /* EPP WCEL_Dump(&ctx, "before func"); */
965 /* mask out some bits which don't interest us */
966 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
969 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
971 if (km->keyState != ks)
975 for (ke = &km->entries[0]; ke->func != 0; ke++)
976 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
980 for (ke = &km->entries[0]; ke->func != 0; ke++)
981 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
993 else if (!(ir.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED))
994 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
995 else TRACE("Dropped event\n");
997 /* EPP WCEL_Dump(&ctx, "after func"); */
998 if (!ctx.shall_echo) continue;
999 if (ctx.can_pos_cursor)
1002 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
1004 else if (!ctx.done && !ctx.error)
1007 /* erase previous chars */
1008 WCEL_WriteNChars(&ctx, '\b', ctx.last_rub);
1010 /* write chars up to cursor */
1011 ctx.last_rub = WCEL_WriteConsole(&ctx, 0, ctx.ofs);
1012 /* write chars past cursor */
1013 last = ctx.last_rub + WCEL_WriteConsole(&ctx, ctx.ofs, ctx.len - ctx.ofs);
1014 if (last < ctx.last_max) /* ctx.line has been shortened, erase */
1016 WCEL_WriteNChars(&ctx, ' ', ctx.last_max - last);
1017 WCEL_WriteNChars(&ctx, '\b', ctx.last_max - last);
1018 ctx.last_max = last;
1020 else ctx.last_max = last;
1021 /* reposition at cursor */
1022 WCEL_WriteNChars(&ctx, '\b', last - ctx.last_rub);
1027 HeapFree(GetProcessHeap(), 0, ctx.line);
1030 WCEL_FreeYank(&ctx);
1032 CONSOLE_AppendHistory(ctx.line);
1034 CloseHandle(ctx.hConOut);
1035 HeapFree(GetProcessHeap(), 0, ctx.histCurr);