Multi-line edit is now enabled.
[wine/multimedia.git] / win32 / editline.c
blob650779e3099a0a1e75ca71f86166ab5febc60f12
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <string.h>
26 #include "windef.h"
27 #include "winbase.h"
28 #include "wincon.h"
29 #include "wine/unicode.h"
30 #include "winnls.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(console);
35 /* console.c */
36 extern int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len);
37 extern BOOL CONSOLE_AppendHistory(const WCHAR *p);
38 extern unsigned int CONSOLE_GetNumHistoryEntries(void);
39 extern void CONSOLE_FillLineUniform(HANDLE hConsoleOutput, int i, int j, int len, LPCHAR_INFO lpFill);
41 struct WCEL_Context;
43 typedef struct
45 WCHAR val; /* vk or unicode char */
46 void (*func)(struct WCEL_Context* ctx);
47 } KeyEntry;
49 typedef struct
51 DWORD keyState; /* keyState (from INPUT_RECORD) to match */
52 BOOL chkChar; /* check vk or char */
53 KeyEntry* entries; /* array of entries */
54 } KeyMap;
56 typedef struct WCEL_Context {
57 WCHAR* line; /* the line being edited */
58 size_t alloc; /* number of WCHAR in line */
59 unsigned len; /* number of chars in line */
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 HANDLE hConIn;
65 HANDLE hConOut;
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 unsigned histSize;
70 unsigned histPos;
71 WCHAR* histCurr;
72 } WCEL_Context;
74 #if 0
75 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
77 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
78 "\t\thist=(size=%u pos=%u curr=%s)\n"
79 "\t\tyanked=%s\n",
80 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
81 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
82 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
83 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
84 debugstr_w(ctx->yanked));
86 #endif
88 /* ====================================================================
90 * Console helper functions
92 * ====================================================================*/
94 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
96 DWORD retv;
98 for (;;)
100 /* data available ? */
101 if (ReadConsoleInputW(ctx->hConIn, ir, 1, &retv) && retv == 1)
102 return TRUE;
103 /* then wait... */
104 switch (WaitForSingleObject(ctx->hConIn, INFINITE))
106 case WAIT_OBJECT_0:
107 break;
108 default:
109 /* we have checked that hConIn was a console handle (could be sb) */
110 ERR("Shouldn't happen\n");
111 /* fall thru */
112 case WAIT_ABANDONED:
113 case WAIT_TIMEOUT:
114 ctx->error = 1;
115 ERR("hmm bad situation\n");
116 return FALSE;
121 static inline void WCEL_Beep(WCEL_Context* ctx)
123 Beep(400, 300);
126 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
128 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
131 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int ofs)
133 COORD c;
134 unsigned len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
136 c.Y = ctx->csbi.dwCursorPosition.Y;
137 if (ofs >= len)
139 ofs -= len;
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;
144 return c;
147 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
149 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[beg], len,
150 WCEL_GetCoord(ctx, beg), NULL);
153 /* ====================================================================
155 * context manipulation functions
157 * ====================================================================*/
159 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
161 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
163 FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n");
164 return FALSE;
167 if (ctx->len + len >= ctx->alloc)
169 WCHAR* newline;
170 size_t newsize;
172 /* round up size to 32 byte-WCHAR boundary */
173 newsize = (ctx->len + len + 1 + 31) & ~31;
174 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
175 if (!newline) return FALSE;
176 ctx->line = newline;
177 ctx->alloc = newsize;
179 return TRUE;
182 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
184 unsigned str_len = end - beg;
185 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
186 COORD cend = WCEL_GetCoord(ctx, ctx->len);
187 CHAR_INFO ci;
189 if (end < ctx->len)
190 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
191 /* we need to clean from ctx->len - str_len to ctx->len */
193 ci.Char.UnicodeChar = ' ';
194 ci.Attributes = ctx->csbi.wAttributes;
196 if (cbeg.Y == cend.Y)
198 /* partial erase of sole line */
199 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
200 cend.X - cbeg.X, &ci);
202 else
204 int i;
205 /* erase til eol on first line */
206 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
207 ctx->csbi.dwSize.X - cbeg.X, &ci);
208 /* completly erase all the others (full lines) */
209 for (i = cbeg.Y + 1; i < cend.Y; i++)
210 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
211 /* erase from beg of line until last pos on last line */
212 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
214 ctx->len -= str_len;
215 WCEL_Update(ctx, 0, ctx->len);
216 ctx->line[ctx->len] = 0;
219 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
221 size_t len = lstrlenW(str);
223 if (!len || !WCEL_Grow(ctx, len)) return;
224 if (ctx->len > ctx->ofs)
225 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
226 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
227 ctx->len += len;
228 ctx->line[ctx->len] = 0;
229 WCEL_Update(ctx, ctx->ofs, ctx->len - ctx->ofs);
231 ctx->ofs += len;
234 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
236 WCHAR buffer[2];
238 /* do not insert 0..31 control characters */
239 if (c < ' ' && c != '\t') return;
241 buffer[0] = c;
242 buffer[1] = 0;
243 WCEL_InsertString(ctx, buffer);
246 static void WCEL_FreeYank(WCEL_Context* ctx)
248 if (ctx->yanked)
250 HeapFree(GetProcessHeap(), 0, ctx->yanked);
251 ctx->yanked = NULL;
255 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
257 int len = end - beg;
258 if (len <= 0) return;
260 WCEL_FreeYank(ctx);
261 ctx->yanked = HeapReAlloc(GetProcessHeap(), 0, ctx->yanked, (len + 1) * sizeof(WCHAR));
262 if (!ctx->yanked) return;
263 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
264 ctx->yanked[len] = 0;
267 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
268 * of the data lay in unicode lib
270 static inline BOOL WCEL_iswalnum(WCHAR wc)
272 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
275 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
277 ofs--;
278 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
279 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
280 if (ofs >= 0) ofs++;
281 return max(ofs, 0);
284 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
286 ofs++;
287 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
288 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
289 return min(ofs, ctx->len);
292 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
294 WCHAR* ptr;
296 if (idx == ctx->histSize - 1)
298 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
299 lstrcpyW(ptr, ctx->histCurr);
301 else
303 int len = CONSOLE_GetHistory(idx, NULL, 0);
305 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
307 CONSOLE_GetHistory(idx, ptr, len);
310 return ptr;
313 static void WCEL_HistoryInit(WCEL_Context* ctx)
315 ctx->histPos = CONSOLE_GetNumHistoryEntries();
316 ctx->histSize = ctx->histPos + 1;
317 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
320 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
322 WCHAR* data = WCEL_GetHistory(ctx, idx);
323 int len = lstrlenW(data) + 1;
325 /* save current line edition for recall when needed (FIXME seems broken to me) */
326 if (ctx->histPos == ctx->histSize - 1)
328 if (ctx->histCurr) HeapFree(GetProcessHeap(), 0, ctx->histCurr);
329 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
330 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
332 /* need to clean also the screen if new string is shorter than old one */
333 WCEL_DeleteString(ctx, 0, ctx->len);
334 ctx->ofs = 0;
335 /* insert new string */
336 if (WCEL_Grow(ctx, len))
338 WCEL_InsertString(ctx, data);
339 HeapFree(GetProcessHeap(), 0, data);
340 ctx->histPos = idx;
344 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
346 int startPos = ctx->histPos;
347 WCHAR* data;
348 int len, oldofs;
350 if (ctx->histPos && ctx->histPos == ctx->histSize) {
351 startPos--;
352 ctx->histPos--;
355 do {
356 data = WCEL_GetHistory(ctx, ctx->histPos);
358 if (ctx->histPos) ctx->histPos--;
359 else ctx->histPos = (ctx->histSize-1);
361 len = lstrlenW(data) + 1;
362 if ((len >= ctx->ofs) &&
363 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
365 /* need to clean also the screen if new string is shorter than old one */
366 WCEL_DeleteString(ctx, 0, ctx->len);
368 if (WCEL_Grow(ctx, len))
370 oldofs = ctx->ofs;
371 ctx->ofs = 0;
372 WCEL_InsertString(ctx, data);
373 ctx->ofs = oldofs;
374 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
375 HeapFree(GetProcessHeap(), 0, data);
376 return;
379 } while (ctx->histPos != startPos);
381 return;
384 /* ====================================================================
386 * basic edition functions
388 * ====================================================================*/
390 static void WCEL_Done(WCEL_Context* ctx)
392 WCHAR nl = '\n';
393 if (!WCEL_Grow(ctx, 1)) return;
394 ctx->line[ctx->len++] = '\n';
395 ctx->line[ctx->len] = 0;
396 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
397 ctx->done = 1;
400 static void WCEL_MoveLeft(WCEL_Context* ctx)
402 if (ctx->ofs > 0) ctx->ofs--;
405 static void WCEL_MoveRight(WCEL_Context* ctx)
407 if (ctx->ofs < ctx->len) ctx->ofs++;
410 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
412 int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
413 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
416 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
418 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
419 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
422 static void WCEL_MoveToBeg(WCEL_Context* ctx)
424 ctx->ofs = 0;
427 static void WCEL_MoveToEnd(WCEL_Context* ctx)
429 ctx->ofs = ctx->len;
432 static void WCEL_SetMark(WCEL_Context* ctx)
434 ctx->mark = ctx->ofs;
437 static void WCEL_ExchangeMark(WCEL_Context* ctx)
439 unsigned tmp;
441 if (ctx->mark > ctx->len) return;
442 tmp = ctx->ofs;
443 ctx->ofs = ctx->mark;
444 ctx->mark = tmp;
447 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
449 unsigned beg, end;
451 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
452 if (ctx->mark > ctx->ofs)
454 beg = ctx->ofs; end = ctx->mark;
456 else
458 beg = ctx->mark; end = ctx->ofs;
460 WCEL_SaveYank(ctx, beg, end);
463 static void WCEL_TransposeChar(WCEL_Context* ctx)
465 WCHAR c;
467 if (!ctx->ofs || ctx->ofs == ctx->len) return;
469 c = ctx->line[ctx->ofs];
470 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
471 ctx->line[ctx->ofs - 1] = c;
473 WCEL_Update(ctx, ctx->ofs - 1, 2);
474 ctx->ofs++;
477 static void WCEL_TransposeWords(WCEL_Context* ctx)
479 int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
480 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
481 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
483 unsigned len_r = right_ofs - ctx->ofs;
484 unsigned len_l = ctx->ofs - left_ofs;
486 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
487 if (!tmp) return;
489 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
490 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
491 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
493 HeapFree(GetProcessHeap(), 0, tmp);
494 WCEL_Update(ctx, left_ofs, len_l + len_r);
495 ctx->ofs = right_ofs;
499 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
501 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
502 if (new_ofs != ctx->ofs)
504 int i;
505 for (i = ctx->ofs; i <= new_ofs; i++)
506 ctx->line[i] = tolowerW(ctx->line[i]);
507 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
508 ctx->ofs = new_ofs;
512 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
514 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
515 if (new_ofs != ctx->ofs)
517 int i;
518 for (i = ctx->ofs; i <= new_ofs; i++)
519 ctx->line[i] = toupperW(ctx->line[i]);
520 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
521 ctx->ofs = new_ofs;
525 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
527 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
528 if (new_ofs != ctx->ofs)
530 int i;
532 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
533 for (i = ctx->ofs + 1; i <= new_ofs; i++)
534 ctx->line[i] = tolowerW(ctx->line[i]);
535 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
536 ctx->ofs = new_ofs;
540 static void WCEL_Yank(WCEL_Context* ctx)
542 WCEL_InsertString(ctx, ctx->yanked);
545 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
547 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
548 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
551 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
553 unsigned beg, end;
555 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
556 if (ctx->mark > ctx->ofs)
558 beg = ctx->ofs; end = ctx->mark;
560 else
562 beg = ctx->mark; end = ctx->ofs;
564 WCEL_SaveYank(ctx, beg, end);
565 WCEL_DeleteString(ctx, beg, end);
566 ctx->ofs = beg;
569 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
571 if (ctx->ofs)
573 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
574 ctx->ofs--;
578 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
580 if (ctx->ofs < ctx->len)
581 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
584 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
586 int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
587 if (new_ofs != ctx->ofs)
589 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
590 ctx->ofs = new_ofs;
594 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
596 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
597 if (new_ofs != ctx->ofs)
599 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
603 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
605 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
608 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
610 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
613 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
615 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
618 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
620 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
623 static void WCEL_Redraw(WCEL_Context* ctx)
625 COORD c = WCEL_GetCoord(ctx, ctx->len);
626 CHAR_INFO ci;
628 WCEL_Update(ctx, 0, ctx->len);
630 ci.Char.UnicodeChar = ' ';
631 ci.Attributes = ctx->csbi.wAttributes;
633 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
636 static void WCEL_RepeatCount(WCEL_Context* ctx)
638 #if 0
639 /* FIXME: wait untill all console code is in kernel32 */
640 INPUT_RECORD ir;
641 unsigned repeat = 0;
643 while (WCEL_Get(ctx, &ir, FALSE))
645 if (ir.EventType != KEY_EVENT) break;
646 if (ir.Event.KeyEvent.bKeyDown)
648 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
649 break;
650 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
651 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
652 break;
653 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
655 WCEL_Get(ctx, &ir, TRUE);
657 FIXME("=> %u\n", repeat);
658 #endif
661 /* ====================================================================
663 * Key Maps
665 * ====================================================================*/
667 #define CTRL(x) ((x) - '@')
668 static KeyEntry StdKeyMap[] =
670 {/*BACK*/0x08, WCEL_DeletePrevChar },
671 {/*RETURN*/0x0d, WCEL_Done },
672 {/*DEL*/127, WCEL_DeleteCurrChar },
673 { 0, NULL }
676 static KeyEntry Win32ExtraStdKeyMap[] =
678 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
679 { 0, NULL }
683 static KeyEntry EmacsKeyMapCtrl[] =
685 { CTRL('@'), WCEL_SetMark },
686 { CTRL('A'), WCEL_MoveToBeg },
687 { CTRL('B'), WCEL_MoveLeft },
688 /* C: done in server */
689 { CTRL('D'), WCEL_DeleteCurrChar },
690 { CTRL('E'), WCEL_MoveToEnd },
691 { CTRL('F'), WCEL_MoveRight },
692 { CTRL('G'), WCEL_Beep },
693 { CTRL('H'), WCEL_DeletePrevChar },
694 /* I: meaningless (or tab ???) */
695 { CTRL('J'), WCEL_Done },
696 { CTRL('K'), WCEL_KillToEndOfLine },
697 { CTRL('L'), WCEL_Redraw },
698 { CTRL('M'), WCEL_Done },
699 { CTRL('N'), WCEL_MoveToNextHist },
700 /* O; insert line... meaningless */
701 { CTRL('P'), WCEL_MoveToPrevHist },
702 /* Q: [NIY] quoting... */
703 /* R: [NIY] search backwards... */
704 /* S: [NIY] search forwards... */
705 { CTRL('T'), WCEL_TransposeChar },
706 { CTRL('U'), WCEL_RepeatCount },
707 /* V: paragraph down... meaningless */
708 { CTRL('W'), WCEL_KillMarkedZone },
709 { CTRL('X'), WCEL_ExchangeMark },
710 { CTRL('Y'), WCEL_Yank },
711 /* Z: meaningless */
712 { 0, NULL }
715 static KeyEntry EmacsKeyMapAlt[] =
717 {/*DEL*/127, WCEL_DeleteLeftWord },
718 { '<', WCEL_MoveToFirstHist },
719 { '>', WCEL_MoveToLastHist },
720 { '?', WCEL_Beep },
721 { 'b', WCEL_MoveToLeftWord },
722 { 'c', WCEL_CapitalizeWord },
723 { 'd', WCEL_DeleteRightWord },
724 { 'f', WCEL_MoveToRightWord },
725 { 'l', WCEL_LowerCaseWord },
726 { 't', WCEL_TransposeWords },
727 { 'u', WCEL_UpperCaseWord },
728 { 'w', WCEL_CopyMarkedZone },
729 { 0, NULL }
732 static KeyEntry EmacsKeyMapExtended[] =
734 {/*RETURN*/ 0x0d, WCEL_Done },
735 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
736 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
737 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
738 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
739 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
740 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
741 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
742 { 0, NULL }
745 static KeyMap EmacsKeyMap[] =
747 {0x00000000, 1, StdKeyMap},
748 {0x00000001, 1, EmacsKeyMapAlt}, /* left alt */
749 {0x00000002, 1, EmacsKeyMapAlt}, /* right alt */
750 {0x00000004, 1, EmacsKeyMapCtrl}, /* left ctrl */
751 {0x00000008, 1, EmacsKeyMapCtrl}, /* right ctrl */
752 {0x00000100, 0, EmacsKeyMapExtended},
753 {0, 0, 0}
756 static KeyEntry Win32KeyMapExtended[] =
758 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
759 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
760 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
761 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
762 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
763 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
764 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
765 { 0, NULL }
768 static KeyEntry Win32KeyMapCtrlExtended[] =
770 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
771 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
772 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
773 { 0, NULL }
776 KeyMap Win32KeyMap[] =
778 {0x00000000, 1, StdKeyMap},
779 {0x00000000, 0, Win32ExtraStdKeyMap},
780 {0x00000100, 0, Win32KeyMapExtended},
781 {0x00000104, 0, Win32KeyMapCtrlExtended},
782 {0x00000108, 0, Win32KeyMapCtrlExtended},
783 {0, 0, 0}
785 #undef CTRL
787 /* ====================================================================
789 * Read line master function
791 * ====================================================================*/
793 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, int use_emacs)
795 WCEL_Context ctx;
796 INPUT_RECORD ir;
797 KeyMap* km;
798 KeyEntry* ke;
799 unsigned ofs;
800 void (*func)(struct WCEL_Context* ctx);
801 DWORD ks;
803 memset(&ctx, 0, sizeof(ctx));
804 ctx.hConIn = hConsoleIn;
805 WCEL_HistoryInit(&ctx);
806 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
807 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
808 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
809 return NULL;
810 ctx.can_wrap = (GetConsoleMode(ctx.hConOut, &ks) && (ks & ENABLE_WRAP_AT_EOL_OUTPUT)) ? 1 : 0;
812 if (!WCEL_Grow(&ctx, 1))
814 CloseHandle(ctx.hConOut);
815 return NULL;
817 ctx.line[0] = 0;
819 /* EPP WCEL_Dump(&ctx, "init"); */
821 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
823 if (ir.EventType != KEY_EVENT || !ir.Event.KeyEvent.bKeyDown) continue;
824 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08lx\n",
825 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
826 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
827 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
829 /* EPP WCEL_Dump(&ctx, "before func"); */
830 ofs = ctx.ofs;
831 /* mask out some bits which don't interest us */
832 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON);
834 func = NULL;
835 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
837 if (km->keyState != ks)
838 continue;
839 if (km->chkChar)
841 for (ke = &km->entries[0]; ke->func != 0; ke++)
842 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
844 else
846 for (ke = &km->entries[0]; ke->func != 0; ke++)
847 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
850 if (ke->func)
852 func = ke->func;
853 break;
857 if (func)
858 (func)(&ctx);
859 else if (!(ir.Event.KeyEvent.dwControlKeyState & (ENHANCED_KEY|LEFT_ALT_PRESSED)))
860 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
861 else TRACE("Dropped event\n");
863 /* EPP WCEL_Dump(&ctx, "after func"); */
864 if (ctx.ofs != ofs)
865 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
867 if (ctx.error)
869 HeapFree(GetProcessHeap(), 0, ctx.line);
870 ctx.line = NULL;
872 WCEL_FreeYank(&ctx);
873 if (ctx.line)
874 CONSOLE_AppendHistory(ctx.line);
876 CloseHandle(ctx.hConOut);
877 if (ctx.histCurr) HeapFree(GetProcessHeap(), 0, ctx.histCurr);
878 return ctx.line;