push b8b0d21e31a2078e72f3e29b2ec7fde29873cc3e
[wine/hacks.git] / dlls / kernel32 / editline.c
blobab70916887909cd7158d702593f6f02adfe71cfc
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 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) */
60 HANDLE hConIn;
61 HANDLE hConOut;
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 */
65 unsigned histSize;
66 unsigned histPos;
67 WCHAR* histCurr;
68 } WCEL_Context;
70 #if 0
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"
75 "\t\tyanked=%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));
82 #endif
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");
94 ctx->error = 1;
95 return FALSE;
98 static inline void WCEL_Beep(WCEL_Context* ctx)
100 Beep(400, 300);
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)
110 COORD c;
111 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
113 c.Y = ctx->csbi.dwCursorPosition.Y;
114 if (ofs >= len)
116 ofs -= len;
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;
121 return c;
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");
143 return FALSE;
146 if (ctx->len + len >= ctx->alloc)
148 WCHAR* newline;
149 size_t newsize;
151 /* round up size to 32 byte-WCHAR boundary */
152 newsize = (ctx->len + len + 1 + 31) & ~31;
154 if (ctx->line)
155 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
156 else
157 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
159 if (!newline) return FALSE;
160 ctx->line = newline;
161 ctx->alloc = newsize;
163 return TRUE;
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);
171 CHAR_INFO ci;
173 if (end < 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);
186 else
188 int i;
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);
198 ctx->len -= str_len;
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));
211 ctx->len += len;
212 ctx->line[ctx->len] = 0;
213 WCEL_Update(ctx, ctx->ofs, ctx->len - ctx->ofs);
215 ctx->ofs += len;
218 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
220 WCHAR buffer[2];
222 /* do not insert 0..31 control characters */
223 if (c < ' ' && c != '\t') return;
225 buffer[0] = c;
226 buffer[1] = 0;
227 WCEL_InsertString(ctx, buffer);
230 static void WCEL_FreeYank(WCEL_Context* ctx)
232 HeapFree(GetProcessHeap(), 0, ctx->yanked);
233 ctx->yanked = NULL;
236 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
238 int len = end - beg;
239 if (len <= 0) return;
241 WCEL_FreeYank(ctx);
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)
259 ofs--;
260 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
261 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
262 if (ofs >= 0) ofs++;
263 return max(ofs, 0);
266 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
268 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)
276 WCHAR* ptr;
278 if (idx == ctx->histSize - 1)
280 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
281 lstrcpyW(ptr, ctx->histCurr);
283 else
285 int len = CONSOLE_GetHistory(idx, NULL, 0);
287 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
289 CONSOLE_GetHistory(idx, ptr, len);
292 return ptr;
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);
316 ctx->ofs = 0;
317 /* insert new string */
318 if (WCEL_Grow(ctx, len))
320 WCEL_InsertString(ctx, data);
321 HeapFree(GetProcessHeap(), 0, data);
322 ctx->histPos = idx;
326 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
328 int startPos = ctx->histPos;
329 WCHAR* data;
330 unsigned int len, oldofs;
332 if (ctx->histPos && ctx->histPos == ctx->histSize) {
333 startPos--;
334 ctx->histPos--;
337 do {
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))
352 oldofs = ctx->ofs;
353 ctx->ofs = 0;
354 WCEL_InsertString(ctx, data);
355 ctx->ofs = oldofs;
356 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
357 HeapFree(GetProcessHeap(), 0, data);
358 return;
361 } while (ctx->histPos != startPos);
363 return;
366 /* ====================================================================
368 * basic edition functions
370 * ====================================================================*/
372 static void WCEL_Done(WCEL_Context* ctx)
374 WCHAR nl = '\n';
375 if (!WCEL_Grow(ctx, 2)) return;
376 ctx->line[ctx->len++] = '\r';
377 ctx->line[ctx->len++] = '\n';
378 ctx->line[ctx->len] = 0;
379 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
380 ctx->done = 1;
383 static void WCEL_MoveLeft(WCEL_Context* ctx)
385 if (ctx->ofs > 0) ctx->ofs--;
388 static void WCEL_MoveRight(WCEL_Context* ctx)
390 if (ctx->ofs < ctx->len) ctx->ofs++;
393 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
395 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
396 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
399 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
401 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
402 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
405 static void WCEL_MoveToBeg(WCEL_Context* ctx)
407 ctx->ofs = 0;
410 static void WCEL_MoveToEnd(WCEL_Context* ctx)
412 ctx->ofs = ctx->len;
415 static void WCEL_SetMark(WCEL_Context* ctx)
417 ctx->mark = ctx->ofs;
420 static void WCEL_ExchangeMark(WCEL_Context* ctx)
422 unsigned tmp;
424 if (ctx->mark > ctx->len) return;
425 tmp = ctx->ofs;
426 ctx->ofs = ctx->mark;
427 ctx->mark = tmp;
430 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
432 unsigned beg, end;
434 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
435 if (ctx->mark > ctx->ofs)
437 beg = ctx->ofs; end = ctx->mark;
439 else
441 beg = ctx->mark; end = ctx->ofs;
443 WCEL_SaveYank(ctx, beg, end);
446 static void WCEL_TransposeChar(WCEL_Context* ctx)
448 WCHAR c;
450 if (!ctx->ofs || ctx->ofs == ctx->len) return;
452 c = ctx->line[ctx->ofs];
453 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
454 ctx->line[ctx->ofs - 1] = c;
456 WCEL_Update(ctx, ctx->ofs - 1, 2);
457 ctx->ofs++;
460 static void WCEL_TransposeWords(WCEL_Context* ctx)
462 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
463 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
464 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
466 unsigned len_r = right_ofs - ctx->ofs;
467 unsigned len_l = ctx->ofs - left_ofs;
469 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
470 if (!tmp) return;
472 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
473 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
474 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
476 HeapFree(GetProcessHeap(), 0, tmp);
477 WCEL_Update(ctx, left_ofs, len_l + len_r);
478 ctx->ofs = right_ofs;
482 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
484 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
485 if (new_ofs != ctx->ofs)
487 unsigned int i;
488 for (i = ctx->ofs; i <= new_ofs; i++)
489 ctx->line[i] = tolowerW(ctx->line[i]);
490 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
491 ctx->ofs = new_ofs;
495 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
497 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
498 if (new_ofs != ctx->ofs)
500 unsigned int i;
501 for (i = ctx->ofs; i <= new_ofs; i++)
502 ctx->line[i] = toupperW(ctx->line[i]);
503 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
504 ctx->ofs = new_ofs;
508 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
510 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
511 if (new_ofs != ctx->ofs)
513 unsigned int i;
515 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
516 for (i = ctx->ofs + 1; i <= new_ofs; i++)
517 ctx->line[i] = tolowerW(ctx->line[i]);
518 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
519 ctx->ofs = new_ofs;
523 static void WCEL_Yank(WCEL_Context* ctx)
525 WCEL_InsertString(ctx, ctx->yanked);
528 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
530 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
531 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
534 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
536 unsigned beg, end;
538 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
539 if (ctx->mark > ctx->ofs)
541 beg = ctx->ofs; end = ctx->mark;
543 else
545 beg = ctx->mark; end = ctx->ofs;
547 WCEL_SaveYank(ctx, beg, end);
548 WCEL_DeleteString(ctx, beg, end);
549 ctx->ofs = beg;
552 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
554 if (ctx->ofs)
556 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
557 ctx->ofs--;
561 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
563 if (ctx->ofs < ctx->len)
564 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
567 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
569 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
570 if (new_ofs != ctx->ofs)
572 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
573 ctx->ofs = new_ofs;
577 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
579 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
580 if (new_ofs != ctx->ofs)
582 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
586 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
588 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
591 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
593 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
596 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
598 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
601 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
603 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
606 static void WCEL_Redraw(WCEL_Context* ctx)
608 COORD c = WCEL_GetCoord(ctx, ctx->len);
609 CHAR_INFO ci;
611 WCEL_Update(ctx, 0, ctx->len);
613 ci.Char.UnicodeChar = ' ';
614 ci.Attributes = ctx->csbi.wAttributes;
616 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
619 static void WCEL_RepeatCount(WCEL_Context* ctx)
621 #if 0
622 /* FIXME: wait until all console code is in kernel32 */
623 INPUT_RECORD ir;
624 unsigned repeat = 0;
626 while (WCEL_Get(ctx, &ir, FALSE))
628 if (ir.EventType != KEY_EVENT) break;
629 if (ir.Event.KeyEvent.bKeyDown)
631 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
632 break;
633 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
634 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
635 break;
636 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
638 WCEL_Get(ctx, &ir, TRUE);
640 FIXME("=> %u\n", repeat);
641 #endif
644 /* ====================================================================
646 * Key Maps
648 * ====================================================================*/
650 #define CTRL(x) ((x) - '@')
651 static const KeyEntry StdKeyMap[] =
653 {/*BACK*/0x08, WCEL_DeletePrevChar },
654 {/*RETURN*/0x0d, WCEL_Done },
655 {/*DEL*/127, WCEL_DeleteCurrChar },
656 { 0, NULL }
659 static const KeyEntry EmacsKeyMapCtrl[] =
661 { CTRL('@'), WCEL_SetMark },
662 { CTRL('A'), WCEL_MoveToBeg },
663 { CTRL('B'), WCEL_MoveLeft },
664 /* C: done in server */
665 { CTRL('D'), WCEL_DeleteCurrChar },
666 { CTRL('E'), WCEL_MoveToEnd },
667 { CTRL('F'), WCEL_MoveRight },
668 { CTRL('G'), WCEL_Beep },
669 { CTRL('H'), WCEL_DeletePrevChar },
670 /* I: meaningless (or tab ???) */
671 { CTRL('J'), WCEL_Done },
672 { CTRL('K'), WCEL_KillToEndOfLine },
673 { CTRL('L'), WCEL_Redraw },
674 { CTRL('M'), WCEL_Done },
675 { CTRL('N'), WCEL_MoveToNextHist },
676 /* O; insert line... meaningless */
677 { CTRL('P'), WCEL_MoveToPrevHist },
678 /* Q: [NIY] quoting... */
679 /* R: [NIY] search backwards... */
680 /* S: [NIY] search forwards... */
681 { CTRL('T'), WCEL_TransposeChar },
682 { CTRL('U'), WCEL_RepeatCount },
683 /* V: paragraph down... meaningless */
684 { CTRL('W'), WCEL_KillMarkedZone },
685 { CTRL('X'), WCEL_ExchangeMark },
686 { CTRL('Y'), WCEL_Yank },
687 /* Z: meaningless */
688 { 0, NULL }
691 static const KeyEntry EmacsKeyMapAlt[] =
693 {/*DEL*/127, WCEL_DeleteLeftWord },
694 { '<', WCEL_MoveToFirstHist },
695 { '>', WCEL_MoveToLastHist },
696 { '?', WCEL_Beep },
697 { 'b', WCEL_MoveToLeftWord },
698 { 'c', WCEL_CapitalizeWord },
699 { 'd', WCEL_DeleteRightWord },
700 { 'f', WCEL_MoveToRightWord },
701 { 'l', WCEL_LowerCaseWord },
702 { 't', WCEL_TransposeWords },
703 { 'u', WCEL_UpperCaseWord },
704 { 'w', WCEL_CopyMarkedZone },
705 { 0, NULL }
708 static const KeyEntry EmacsStdKeyMap[] =
710 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
711 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
712 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
713 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
714 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
715 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
716 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
717 { 0, NULL }
720 static const KeyMap EmacsKeyMap[] =
722 {0, 1, StdKeyMap},
723 {0, 0, EmacsStdKeyMap},
724 {RIGHT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* right alt */
725 {LEFT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* left alt */
726 {RIGHT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* right ctrl */
727 {LEFT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* left ctrl */
728 {0, 0, NULL}
731 static const KeyEntry Win32StdKeyMap[] =
733 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
734 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
735 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
736 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
737 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
738 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
739 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
740 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
741 { 0, NULL }
744 static const KeyEntry Win32KeyMapCtrl[] =
746 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
747 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
748 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
749 { 0, NULL }
752 static const KeyMap Win32KeyMap[] =
754 {0, 1, StdKeyMap},
755 {0, 0, Win32StdKeyMap},
756 {RIGHT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
757 {LEFT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
758 {0, 0, NULL}
760 #undef CTRL
762 /* ====================================================================
764 * Read line master function
766 * ====================================================================*/
768 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn)
770 WCEL_Context ctx;
771 INPUT_RECORD ir;
772 const KeyMap* km;
773 const KeyEntry* ke;
774 unsigned ofs;
775 void (*func)(struct WCEL_Context* ctx);
776 DWORD ks;
777 int use_emacs;
779 memset(&ctx, 0, sizeof(ctx));
780 ctx.hConIn = hConsoleIn;
781 WCEL_HistoryInit(&ctx);
783 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
784 use_emacs = 0;
786 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
787 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
788 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
789 return NULL;
790 ctx.can_wrap = (GetConsoleMode(ctx.hConOut, &ks) && (ks & ENABLE_WRAP_AT_EOL_OUTPUT)) ? 1 : 0;
792 if (!WCEL_Grow(&ctx, 1))
794 CloseHandle(ctx.hConOut);
795 return NULL;
797 ctx.line[0] = 0;
799 /* EPP WCEL_Dump(&ctx, "init"); */
801 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
803 if (ir.EventType != KEY_EVENT) continue;
804 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
805 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
806 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
807 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
808 if (!ir.Event.KeyEvent.bKeyDown) continue;
810 /* EPP WCEL_Dump(&ctx, "before func"); */
811 ofs = ctx.ofs;
812 /* mask out some bits which don't interest us */
813 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
815 func = NULL;
816 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
818 if (km->keyState != ks)
819 continue;
820 if (km->chkChar)
822 for (ke = &km->entries[0]; ke->func != 0; ke++)
823 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
825 else
827 for (ke = &km->entries[0]; ke->func != 0; ke++)
828 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
831 if (ke->func)
833 func = ke->func;
834 break;
838 if (func)
839 (func)(&ctx);
840 else if (!(ir.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED))
841 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
842 else TRACE("Dropped event\n");
844 /* EPP WCEL_Dump(&ctx, "after func"); */
845 if (ctx.ofs != ofs)
846 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
848 if (ctx.error)
850 HeapFree(GetProcessHeap(), 0, ctx.line);
851 ctx.line = NULL;
853 WCEL_FreeYank(&ctx);
854 if (ctx.line)
855 CONSOLE_AppendHistory(ctx.line);
857 CloseHandle(ctx.hConOut);
858 HeapFree(GetProcessHeap(), 0, ctx.histCurr);
859 return ctx.line;