TOOLTIPS_GetTipText: Accept null module instance, Loadstring can
[wine/multimedia.git] / win32 / editline.c
bloba96e6ab54fbb1506441576db2019b69262b75a4e
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);
40 struct WCEL_Context;
42 typedef struct
44 WCHAR val; /* vk or unicode char */
45 void (*func)(struct WCEL_Context* ctx);
46 } KeyEntry;
48 typedef struct
50 DWORD keyState; /* keyState (from INPUT_RECORD) to match */
51 BOOL chkChar; /* check vk or char */
52 KeyEntry* entries; /* array of entries */
53 } KeyMap;
55 typedef struct WCEL_Context {
56 WCHAR* line; /* the line being edited */
57 size_t alloc; /* number of WCHAR in line */
58 unsigned len; /* number of chars in line */
59 unsigned ofs; /* offset for cursor in current line */
60 WCHAR* yanked; /* yanked line */
61 unsigned mark; /* marked point (emacs mode only) */
62 CONSOLE_SCREEN_BUFFER_INFO csbi; /* current state (initial cursor, window size, attribute) */
63 HANDLE hConIn;
64 HANDLE hConOut;
65 unsigned done : 1, /* to 1 when we're done with editing */
66 error : 1; /* to 1 when an error occurred in the editing */
67 unsigned histSize;
68 unsigned histPos;
69 WCHAR* histCurr;
70 } WCEL_Context;
72 #if 0
73 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
75 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c\n"
76 "\t\thist=(size=%u pos=%u curr=%s)\n",
77 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
78 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
79 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e',
80 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr));
82 #endif
84 /* ====================================================================
86 * Console helper functions
88 * ====================================================================*/
90 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
92 DWORD retv;
94 for (;;)
96 /* data available ? */
97 if (ReadConsoleInputW(ctx->hConIn, ir, 1, &retv) && retv == 1)
98 return TRUE;
99 /* then wait... */
100 switch (WaitForSingleObject(ctx->hConIn, INFINITE))
102 case WAIT_OBJECT_0:
103 break;
104 default:
105 /* we have checked that hConIn was a console handle (could be sb) */
106 ERR("Shouldn't happen\n");
107 /* fall thru */
108 case WAIT_ABANDONED:
109 case WAIT_TIMEOUT:
110 ctx->error = 1;
111 ERR("hmm bad situation\n");
112 return FALSE;
117 static inline void WCEL_Beep(WCEL_Context* ctx)
119 Beep(400, 300);
122 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int ofs)
124 COORD c;
125 c.X = ctx->csbi.dwCursorPosition.X + ofs;
126 c.Y = ctx->csbi.dwCursorPosition.Y;
127 return c;
130 static inline void WCEL_GetRect(WCEL_Context* ctx, LPSMALL_RECT sr, int beg, int end)
132 sr->Left = ctx->csbi.dwCursorPosition.X + beg;
133 sr->Top = ctx->csbi.dwCursorPosition.Y;
134 sr->Right = ctx->csbi.dwCursorPosition.X + end;
135 sr->Bottom = ctx->csbi.dwCursorPosition.Y;
138 /* ====================================================================
140 * context manipulation functions
142 * ====================================================================*/
144 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
146 if (ctx->csbi.dwCursorPosition.X + ctx->ofs + len >= ctx->csbi.dwSize.X)
148 FIXME("Current implementation doesn't allow edition to spray across several lines\n");
149 return FALSE;
152 if (ctx->len + len >= ctx->alloc)
154 WCHAR* newline;
155 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * (ctx->alloc + 32));
156 if (!newline) return FALSE;
157 ctx->line = newline;
158 ctx->alloc += 32;
160 return TRUE;
163 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
165 SMALL_RECT scl, clp;
166 CHAR_INFO ci;
168 if (end < ctx->len)
169 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
170 /* make the source rect bigger than the actual rect to that the part outside the clip
171 * rect (before the scroll) will get redrawn after the scroll
173 WCEL_GetRect(ctx, &scl, end, ctx->len + end - beg);
174 WCEL_GetRect(ctx, &clp, beg, ctx->len);
176 ci.Char.UnicodeChar = ' ';
177 ci.Attributes = ctx->csbi.wAttributes;
178 ScrollConsoleScreenBufferW(ctx->hConOut, &scl, &clp, WCEL_GetCoord(ctx, beg), &ci);
180 ctx->len -= end - beg;
181 ctx->line[ctx->len] = 0;
184 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
186 size_t len = lstrlenW(str);
188 if (!len || !WCEL_Grow(ctx, len)) return;
189 if (ctx->len > ctx->ofs)
190 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
191 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
192 ctx->len += len;
193 ctx->line[ctx->len] = 0;
194 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
195 WriteConsoleW(ctx->hConOut, &ctx->line[ctx->ofs], ctx->len - ctx->ofs, NULL, NULL);
196 ctx->ofs += len;
199 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
201 WCHAR buffer[2];
203 /* do not insert 0..31 control characters */
204 if (c < ' ')
206 if (c != '\t') return;
208 buffer[0] = c;
209 buffer[1] = 0;
210 WCEL_InsertString(ctx, buffer);
213 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
215 int len = end - beg;
216 ctx->yanked = HeapReAlloc(GetProcessHeap(), 0, ctx->yanked, (len + 1) * sizeof(WCHAR));
217 if (!ctx->yanked) return;
218 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
219 ctx->yanked[len] = 0;
222 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
223 * of the data lay in unicode lib
225 static inline BOOL WCEL_iswalnum(WCHAR wc)
227 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
230 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
232 ofs--;
233 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
234 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
235 if (ofs >= 0) ofs++;
236 return max(ofs, 0);
239 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
241 ofs++;
242 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
243 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
244 return min(ofs, ctx->len);
247 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
249 WCHAR* ptr;
251 if (idx == ctx->histSize - 1)
253 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
254 lstrcpyW(ptr, ctx->histCurr);
256 else
258 int len = CONSOLE_GetHistory(idx, NULL, 0);
260 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
262 CONSOLE_GetHistory(idx, ptr, len);
265 return ptr;
268 static void WCEL_HistoryInit(WCEL_Context* ctx)
270 ctx->histPos = CONSOLE_GetNumHistoryEntries();
271 ctx->histSize = ctx->histPos + 1;
272 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
275 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
277 WCHAR* data = WCEL_GetHistory(ctx, idx);
278 int len = lstrlenW(data) + 1;
280 /* save current line edition for recall when needed (FIXME seems broken to me) */
281 if (ctx->histPos == ctx->histSize - 1)
283 if (ctx->histCurr) HeapFree(GetProcessHeap(), 0, ctx->histCurr);
284 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
285 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
287 /* need to clean also the screen if new string is shorter than old one */
288 WCEL_DeleteString(ctx, 0, ctx->len);
289 ctx->ofs = 0;
290 /* insert new string */
291 if (WCEL_Grow(ctx, len))
293 WCEL_InsertString(ctx, data);
294 HeapFree(GetProcessHeap(), 0, data);
295 ctx->histPos = idx;
299 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
301 int startPos = ctx->histPos;
302 WCHAR* data;
303 int len, oldofs;
305 if (ctx->histPos && ctx->histPos == ctx->histSize) {
306 startPos--;
307 ctx->histPos--;
310 do {
311 data = WCEL_GetHistory(ctx, ctx->histPos);
313 if (ctx->histPos) ctx->histPos--;
314 else ctx->histPos = (ctx->histSize-1);
316 len = lstrlenW(data) + 1;
317 if ((len >= ctx->ofs) &&
318 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
320 /* need to clean also the screen if new string is shorter than old one */
321 WCEL_DeleteString(ctx, 0, ctx->len);
323 if (WCEL_Grow(ctx, len))
325 oldofs = ctx->ofs;
326 ctx->ofs = 0;
327 WCEL_InsertString(ctx, data);
328 ctx->ofs = oldofs;
329 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
330 HeapFree(GetProcessHeap(), 0, data);
331 return;
334 } while (ctx->histPos != startPos);
336 return;
339 /* ====================================================================
341 * basic edition functions
343 * ====================================================================*/
345 static void WCEL_Done(WCEL_Context* ctx)
347 if (!WCEL_Grow(ctx, 1)) return;
348 ctx->line[ctx->len++] = '\n';
349 ctx->line[ctx->len] = 0;
350 WriteConsoleA(ctx->hConOut, "\n", 1, NULL, NULL);
351 ctx->done = 1;
354 static void WCEL_MoveLeft(WCEL_Context* ctx)
356 if (ctx->ofs > 0) ctx->ofs--;
359 static void WCEL_MoveRight(WCEL_Context* ctx)
361 if (ctx->ofs < ctx->len) ctx->ofs++;
364 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
366 int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
367 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
370 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
372 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
373 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
376 static void WCEL_MoveToBeg(WCEL_Context* ctx)
378 ctx->ofs = 0;
381 static void WCEL_MoveToEnd(WCEL_Context* ctx)
383 ctx->ofs = ctx->len;
386 static void WCEL_SetMark(WCEL_Context* ctx)
388 ctx->mark = ctx->ofs;
391 static void WCEL_ExchangeMark(WCEL_Context* ctx)
393 unsigned tmp;
395 if (ctx->mark > ctx->len) return;
396 tmp = ctx->ofs;
397 ctx->ofs = ctx->mark;
398 ctx->mark = tmp;
401 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
403 unsigned beg, end;
405 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
406 if (ctx->mark > ctx->ofs)
408 beg = ctx->ofs; end = ctx->mark;
410 else
412 beg = ctx->mark; end = ctx->ofs;
414 WCEL_SaveYank(ctx, beg, end);
417 static void WCEL_TransposeChar(WCEL_Context* ctx)
419 WCHAR c;
421 if (!ctx->ofs || ctx->ofs == ctx->len) return;
423 c = ctx->line[ctx->ofs];
424 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
425 ctx->line[ctx->ofs - 1] = c;
427 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[ctx->ofs - 1], 2, WCEL_GetCoord(ctx, ctx->ofs - 1), NULL);
428 ctx->ofs++;
431 static void WCEL_TransposeWords(WCEL_Context* ctx)
433 FIXME("NIY\n");
436 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
438 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
439 if (new_ofs != ctx->ofs)
441 int i;
442 for (i = ctx->ofs; i <= new_ofs; i++)
443 ctx->line[i] = tolowerW(ctx->line[i]);
444 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[ctx->ofs], new_ofs - ctx->ofs + 1,
445 WCEL_GetCoord(ctx, ctx->ofs), NULL);
446 ctx->ofs = new_ofs;
450 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
452 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
453 if (new_ofs != ctx->ofs)
455 int i;
456 for (i = ctx->ofs; i <= new_ofs; i++)
457 ctx->line[i] = toupperW(ctx->line[i]);
458 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[ctx->ofs], new_ofs - ctx->ofs + 1,
459 WCEL_GetCoord(ctx, ctx->ofs), NULL);
460 ctx->ofs = new_ofs;
464 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
466 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
467 if (new_ofs != ctx->ofs)
469 int i;
471 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
472 for (i = ctx->ofs + 1; i <= new_ofs; i++)
473 ctx->line[i] = tolowerW(ctx->line[i]);
474 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[ctx->ofs], new_ofs - ctx->ofs + 1,
475 WCEL_GetCoord(ctx, ctx->ofs), NULL);
476 ctx->ofs = new_ofs;
480 static void WCEL_Yank(WCEL_Context* ctx)
482 WCEL_InsertString(ctx, ctx->yanked);
483 HeapFree(GetProcessHeap(), 0, ctx->yanked);
484 ctx->yanked = NULL;
487 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
489 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
490 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
493 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
495 unsigned beg, end;
497 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
498 if (ctx->mark > ctx->ofs)
500 beg = ctx->ofs; end = ctx->mark;
502 else
504 beg = ctx->mark; end = ctx->ofs;
506 WCEL_SaveYank(ctx, beg, end);
507 WCEL_DeleteString(ctx, beg, end);
508 ctx->ofs = beg;
511 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
513 if (ctx->ofs)
515 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
516 ctx->ofs--;
520 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
522 if (ctx->ofs < ctx->len)
523 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
526 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
528 int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
529 if (new_ofs != ctx->ofs)
531 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
532 ctx->ofs = new_ofs;
536 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
538 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
539 if (new_ofs != ctx->ofs)
541 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
545 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
547 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
550 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
552 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
555 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
557 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
560 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
562 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
565 /* ====================================================================
567 * Key Maps
569 * ====================================================================*/
571 #define CTRL(x) ((x) - '@')
572 static KeyEntry StdKeyMap[] =
574 {/*BACK*/0x08, WCEL_DeletePrevChar },
575 {/*RETURN*/0x0d, WCEL_Done },
576 {/*DEL*/127, WCEL_DeleteCurrChar },
577 { 0, NULL }
580 static KeyEntry Win32ExtraStdKeyMap[] =
582 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
583 { 0, NULL }
587 static KeyEntry EmacsKeyMapCtrl[] =
589 { CTRL('@'), WCEL_SetMark },
590 { CTRL('A'), WCEL_MoveToBeg },
591 { CTRL('B'), WCEL_MoveLeft },
592 /* C */
593 { CTRL('D'), WCEL_DeleteCurrChar },
594 { CTRL('E'), WCEL_MoveToEnd },
595 { CTRL('F'), WCEL_MoveRight },
596 { CTRL('G'), WCEL_Beep },
597 { CTRL('H'), WCEL_DeletePrevChar },
598 /* I: meaningless (or tab ???) */
599 { CTRL('J'), WCEL_Done },
600 { CTRL('K'), WCEL_KillToEndOfLine },
601 /* L: [NIY] redraw the whole stuff */
602 { CTRL('M'), WCEL_Done },
603 { CTRL('N'), WCEL_MoveToNextHist },
604 /* O; insert line... meaningless */
605 { CTRL('P'), WCEL_MoveToPrevHist },
606 /* Q: [NIY] quoting... */
607 /* R: [NIY] search backwards... */
608 /* S: [NIY] search forwards... */
609 { CTRL('T'), WCEL_TransposeChar },
610 /* U: [NIY] set repeat count... */
611 /* V: paragraph down... meaningless */
612 { CTRL('W'), WCEL_KillMarkedZone },
613 { CTRL('X'), WCEL_ExchangeMark },
614 { CTRL('Y'), WCEL_Yank },
615 /* Z: meaningless */
616 { 0, NULL }
619 static KeyEntry EmacsKeyMapAlt[] =
621 {/*DEL*/127, WCEL_DeleteLeftWord },
622 { '<', WCEL_MoveToFirstHist },
623 { '>', WCEL_MoveToLastHist },
624 { '?', WCEL_Beep },
625 { 'b', WCEL_MoveToLeftWord },
626 { 'c', WCEL_CapitalizeWord },
627 { 'd', WCEL_DeleteRightWord },
628 { 'f', WCEL_MoveToRightWord },
629 { 'l', WCEL_LowerCaseWord },
630 { 't', WCEL_TransposeWords },
631 { 'u', WCEL_UpperCaseWord },
632 { 'w', WCEL_CopyMarkedZone },
633 { 0, NULL }
636 static KeyEntry EmacsKeyMapExtended[] =
638 {/*RETURN*/ 0x0d, WCEL_Done },
639 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
640 {/*VK_NEXT*/0x22, WCEL_MoveToNextHist },
641 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
642 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
643 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
644 {/*VK_LEFT*/0x25, WCEL_MoveLeft },
645 { 0, NULL }
648 static KeyMap EmacsKeyMap[] =
650 {0x00000000, 1, StdKeyMap},
651 {0x00000001, 1, EmacsKeyMapAlt}, /* left alt */
652 {0x00000002, 1, EmacsKeyMapAlt}, /* right alt */
653 {0x00000004, 1, EmacsKeyMapCtrl}, /* left ctrl */
654 {0x00000008, 1, EmacsKeyMapCtrl}, /* right ctrl */
655 {0x00000100, 0, EmacsKeyMapExtended},
656 {0, 0, 0}
659 static KeyEntry Win32KeyMapExtended[] =
661 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
662 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
663 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
664 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
665 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
666 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
667 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
668 { 0, NULL }
671 static KeyEntry Win32KeyMapCtrlExtended[] =
673 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
674 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
675 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
676 { 0, NULL }
679 KeyMap Win32KeyMap[] =
681 {0x00000000, 1, StdKeyMap},
682 {0x00000000, 0, Win32ExtraStdKeyMap},
683 {0x00000100, 0, Win32KeyMapExtended},
684 {0x00000104, 0, Win32KeyMapCtrlExtended},
685 {0x00000108, 0, Win32KeyMapCtrlExtended},
686 {0, 0, 0}
688 #undef CTRL
690 /* ====================================================================
692 * Read line master function
694 * ====================================================================*/
696 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, int use_emacs)
698 WCEL_Context ctx;
699 INPUT_RECORD ir;
700 KeyMap* km;
701 KeyEntry* ke;
702 unsigned ofs;
703 void (*func)(struct WCEL_Context* ctx);
704 DWORD ks;
706 memset(&ctx, 0, sizeof(ctx));
707 ctx.hConIn = hConsoleIn;
708 WCEL_HistoryInit(&ctx);
709 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
710 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
711 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
712 return NULL;
713 if (!WCEL_Grow(&ctx, 1))
715 CloseHandle(ctx.hConOut);
716 return NULL;
718 ctx.line[0] = 0;
720 /* EPP WCEL_Dump(&ctx, "init"); */
722 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
724 if (ir.EventType != KEY_EVENT || !ir.Event.KeyEvent.bKeyDown) continue;
725 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08lx\n",
726 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
727 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
728 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
730 /* EPP WCEL_Dump(&ctx, "before func"); */
731 ofs = ctx.ofs;
732 /* mask out some bits which don't interest us */
733 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON);
735 func = NULL;
736 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
738 if (km->keyState != ks)
739 continue;
740 if (km->chkChar)
742 for (ke = &km->entries[0]; ke->func != 0; ke++)
743 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
745 else
747 for (ke = &km->entries[0]; ke->func != 0; ke++)
748 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
751 if (ke->func)
753 func = ke->func;
754 break;
758 if (func)
759 (func)(&ctx);
760 else if (!(ir.Event.KeyEvent.dwControlKeyState & (ENHANCED_KEY|LEFT_ALT_PRESSED)))
761 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
762 else TRACE("Dropped event\n");
764 /* EPP WCEL_Dump(&ctx, "after func"); */
765 if (ctx.ofs != ofs)
766 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
768 if (ctx.error)
770 HeapFree(GetProcessHeap(), 0, ctx.line);
771 ctx.line = NULL;
773 if (ctx.line)
774 CONSOLE_AppendHistory(ctx.line);
776 CloseHandle(ctx.hConOut);
777 if (ctx.histCurr) HeapFree(GetProcessHeap(), 0, ctx.histCurr);
778 return ctx.line;