Added support for MWM hints.
[wine/multimedia.git] / win32 / editline.c
blobbbace28b80a76c9f5ed0f6e287fa80521f7b6575
1 /*
2 * line edition function for Win32 console
4 * Copyright 2001 Eric Pouech
5 */
7 #include "config.h"
8 #include <string.h>
10 #include "windef.h"
11 #include "winbase.h"
12 #include "wincon.h"
13 #include "wine/unicode.h"
14 #include "winnls.h"
15 #include "debugtools.h"
17 DEFAULT_DEBUG_CHANNEL(console);
19 /* console.c */
20 extern int CONSOLE_GetHistory(int idx, WCHAR* buf, int buf_len);
21 extern BOOL CONSOLE_AppendHistory(const WCHAR *p);
22 extern unsigned int CONSOLE_GetNumHistoryEntries(void);
24 struct WCEL_Context;
26 typedef struct
28 WCHAR val; /* vk or unicode char */
29 void (*func)(struct WCEL_Context* ctx);
30 } KeyEntry;
32 typedef struct
34 DWORD keyState; /* keyState (from INPUT_RECORD) to match */
35 BOOL chkChar; /* check vk or char */
36 KeyEntry* entries; /* array of entries */
37 } KeyMap;
39 typedef struct WCEL_Context {
40 WCHAR* line; /* the line being edited */
41 size_t alloc; /* number of WCHAR in line */
42 unsigned len; /* number of chars in line */
43 unsigned ofs; /* offset for cursor in current line */
44 WCHAR* yanked; /* yanked line */
45 unsigned mark; /* marked point (emacs mode only) */
46 CONSOLE_SCREEN_BUFFER_INFO csbi; /* current state (initial cursor, window size, attribute) */
47 HANDLE hConIn;
48 HANDLE hConOut;
49 unsigned done : 1, /* to 1 when we're done with editing */
50 error : 1; /* to 1 when an error occurred in the editing */
51 unsigned histSize;
52 unsigned histPos;
53 WCHAR* histCurr;
54 } WCEL_Context;
56 #if 0
57 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
59 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c\n"
60 "\t\thist=(size=%u pos=%u curr=%s)\n",
61 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
62 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
63 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e',
64 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr));
66 #endif
68 /* ====================================================================
70 * Console helper functions
72 * ====================================================================*/
74 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
76 DWORD retv;
78 for (;;)
80 /* data available ? */
81 if (ReadConsoleInputW(ctx->hConIn, ir, 1, &retv) && retv == 1)
82 return TRUE;
83 /* then wait... */
84 switch (WaitForSingleObject(ctx->hConIn, INFINITE))
86 case WAIT_OBJECT_0:
87 break;
88 default:
89 /* we have checked that hConIn was a console handle (could be sb) */
90 ERR("Shouldn't happen\n");
91 /* fall thru */
92 case WAIT_ABANDONED:
93 case WAIT_TIMEOUT:
94 ctx->error = 1;
95 ERR("hmm bad situation\n");
96 return FALSE;
101 static inline void WCEL_Beep(WCEL_Context* ctx)
103 Beep(400, 300);
106 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int ofs)
108 COORD c;
109 c.X = ctx->csbi.dwCursorPosition.X + ofs;
110 c.Y = ctx->csbi.dwCursorPosition.Y;
111 return c;
114 static inline void WCEL_GetRect(WCEL_Context* ctx, LPSMALL_RECT sr, int beg, int end)
116 sr->Left = ctx->csbi.dwCursorPosition.X + beg;
117 sr->Top = ctx->csbi.dwCursorPosition.Y;
118 sr->Right = ctx->csbi.dwCursorPosition.X + end;
119 sr->Bottom = ctx->csbi.dwCursorPosition.Y;
122 /* ====================================================================
124 * context manipulation functions
126 * ====================================================================*/
128 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
130 if (ctx->csbi.dwCursorPosition.X + ctx->ofs + len >= ctx->csbi.dwSize.X)
132 FIXME("Current implementation doesn't allow edition to spray across several lines\n");
133 return FALSE;
136 if (ctx->len + len >= ctx->alloc)
138 WCHAR* newline;
139 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * (ctx->alloc + 32));
140 if (!newline) return FALSE;
141 ctx->line = newline;
142 ctx->alloc += 32;
144 return TRUE;
147 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
149 SMALL_RECT scl, clp;
150 CHAR_INFO ci;
152 if (end < ctx->len)
153 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
154 /* make the source rect bigger than the actual rect to that the part outside the clip
155 * rect (before the scroll) will get redrawn after the scroll
157 WCEL_GetRect(ctx, &scl, end, ctx->len + end - beg);
158 WCEL_GetRect(ctx, &clp, beg, ctx->len);
160 ci.Char.UnicodeChar = ' ';
161 ci.Attributes = ctx->csbi.wAttributes;
162 ScrollConsoleScreenBufferW(ctx->hConOut, &scl, &clp, WCEL_GetCoord(ctx, beg), &ci);
164 ctx->len -= end - beg;
165 ctx->line[ctx->len] = 0;
168 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
170 size_t len = lstrlenW(str);
172 if (!len || !WCEL_Grow(ctx, len)) return;
173 if (ctx->len > ctx->ofs)
174 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
175 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
176 ctx->len += len;
177 ctx->line[ctx->len] = 0;
178 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
179 WriteConsoleW(ctx->hConOut, &ctx->line[ctx->ofs], ctx->len - ctx->ofs, NULL, NULL);
180 ctx->ofs += len;
183 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
185 WCHAR buffer[2];
187 /* do not insert 0..31 control characters */
188 if (c < ' ')
190 if (c != '\t') return;
192 buffer[0] = c;
193 buffer[1] = 0;
194 WCEL_InsertString(ctx, buffer);
197 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
199 int len = end - beg;
200 ctx->yanked = HeapReAlloc(GetProcessHeap(), 0, ctx->yanked, (len + 1) * sizeof(WCHAR));
201 if (!ctx->yanked) return;
202 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
203 ctx->yanked[len] = 0;
206 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
207 * of the data lay in unicode lib
209 static inline BOOL WCEL_iswalnum(WCHAR wc)
211 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
214 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
216 ofs--;
217 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
218 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
219 if (ofs >= 0) ofs++;
220 return max(ofs, 0);
223 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
225 ofs++;
226 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
227 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
228 return min(ofs, ctx->len);
231 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
233 WCHAR* ptr;
235 if (idx == ctx->histSize - 1)
237 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
238 lstrcpyW(ptr, ctx->histCurr);
240 else
242 int len = CONSOLE_GetHistory(idx, NULL, 0);
244 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
246 CONSOLE_GetHistory(idx, ptr, len);
249 return ptr;
252 static void WCEL_HistoryInit(WCEL_Context* ctx)
254 ctx->histPos = CONSOLE_GetNumHistoryEntries();
255 ctx->histSize = ctx->histPos + 1;
256 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
259 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
261 WCHAR* data = WCEL_GetHistory(ctx, idx);
262 int len = lstrlenW(data) + 1;
264 /* save current line edition for recall when needed (FIXME seems broken to me) */
265 if (ctx->histPos == ctx->histSize - 1)
267 if (ctx->histCurr) HeapFree(GetProcessHeap(), 0, ctx->histCurr);
268 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
269 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
271 /* need to clean also the screen if new string is shorter than old one */
272 WCEL_DeleteString(ctx, 0, ctx->len);
273 ctx->ofs = 0;
274 /* insert new string */
275 if (WCEL_Grow(ctx, len))
277 WCEL_InsertString(ctx, data);
278 HeapFree(GetProcessHeap(), 0, data);
279 ctx->histPos = idx;
283 /* ====================================================================
285 * basic edition functions
287 * ====================================================================*/
289 static void WCEL_Done(WCEL_Context* ctx)
291 if (!WCEL_Grow(ctx, 1)) return;
292 ctx->line[ctx->len++] = '\n';
293 ctx->line[ctx->len] = 0;
294 WriteConsoleA(ctx->hConOut, "\n", 1, NULL, NULL);
295 ctx->done = 1;
298 static void WCEL_MoveLeft(WCEL_Context* ctx)
300 if (ctx->ofs > 0) ctx->ofs--;
303 static void WCEL_MoveRight(WCEL_Context* ctx)
305 if (ctx->ofs < ctx->len) ctx->ofs++;
308 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
310 int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
311 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
314 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
316 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
317 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
320 static void WCEL_MoveToBeg(WCEL_Context* ctx)
322 ctx->ofs = 0;
325 static void WCEL_MoveToEnd(WCEL_Context* ctx)
327 ctx->ofs = ctx->len;
330 static void WCEL_SetMark(WCEL_Context* ctx)
332 ctx->mark = ctx->ofs;
335 static void WCEL_ExchangeMark(WCEL_Context* ctx)
337 unsigned tmp;
339 if (ctx->mark > ctx->len) return;
340 tmp = ctx->ofs;
341 ctx->ofs = ctx->mark;
342 ctx->mark = tmp;
345 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
347 unsigned beg, end;
349 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
350 if (ctx->mark > ctx->ofs)
352 beg = ctx->ofs; end = ctx->mark;
354 else
356 beg = ctx->mark; end = ctx->ofs;
358 WCEL_SaveYank(ctx, beg, end);
361 static void WCEL_TransposeChar(WCEL_Context* ctx)
363 WCHAR c;
365 if (!ctx->ofs || ctx->ofs == ctx->len) return;
367 c = ctx->line[ctx->ofs];
368 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
369 ctx->line[ctx->ofs - 1] = c;
371 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[ctx->ofs - 1], 2, WCEL_GetCoord(ctx, ctx->ofs - 1), NULL);
372 ctx->ofs++;
375 static void WCEL_TransposeWords(WCEL_Context* ctx)
377 FIXME("NIY\n");
380 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
382 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
383 if (new_ofs != ctx->ofs)
385 int i;
386 for (i = ctx->ofs; i <= new_ofs; i++)
387 ctx->line[i] = tolowerW(ctx->line[i]);
388 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[ctx->ofs], new_ofs - ctx->ofs + 1,
389 WCEL_GetCoord(ctx, ctx->ofs), NULL);
390 ctx->ofs = new_ofs;
394 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
396 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
397 if (new_ofs != ctx->ofs)
399 int i;
400 for (i = ctx->ofs; i <= new_ofs; i++)
401 ctx->line[i] = toupperW(ctx->line[i]);
402 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[ctx->ofs], new_ofs - ctx->ofs + 1,
403 WCEL_GetCoord(ctx, ctx->ofs), NULL);
404 ctx->ofs = new_ofs;
408 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
410 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
411 if (new_ofs != ctx->ofs)
413 int i;
415 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
416 for (i = ctx->ofs + 1; i <= new_ofs; i++)
417 ctx->line[i] = tolowerW(ctx->line[i]);
418 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[ctx->ofs], new_ofs - ctx->ofs + 1,
419 WCEL_GetCoord(ctx, ctx->ofs), NULL);
420 ctx->ofs = new_ofs;
424 static void WCEL_Yank(WCEL_Context* ctx)
426 WCEL_InsertString(ctx, ctx->yanked);
427 HeapFree(GetProcessHeap(), 0, ctx->yanked);
428 ctx->yanked = NULL;
431 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
433 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
434 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
437 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
439 unsigned beg, end;
441 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
442 if (ctx->mark > ctx->ofs)
444 beg = ctx->ofs; end = ctx->mark;
446 else
448 beg = ctx->mark; end = ctx->ofs;
450 WCEL_SaveYank(ctx, beg, end);
451 WCEL_DeleteString(ctx, beg, end);
452 ctx->ofs = beg;
455 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
457 if (ctx->ofs)
459 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
460 ctx->ofs--;
464 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
466 if (ctx->ofs < ctx->len)
467 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
470 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
472 int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
473 if (new_ofs != ctx->ofs)
475 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
476 ctx->ofs = new_ofs;
480 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
482 int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
483 if (new_ofs != ctx->ofs)
485 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
489 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
491 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
494 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
496 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
499 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
501 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
504 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
506 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
509 /* ====================================================================
511 * Key Maps
513 * ====================================================================*/
515 #define CTRL(x) ((x) - '@')
516 static KeyEntry StdKeyMap[] =
518 {/*BACK*/0x08, WCEL_DeletePrevChar },
519 {/*RETURN*/0x0d, WCEL_Done },
520 {/*DEL*/127, WCEL_DeleteCurrChar },
521 { 0, NULL }
524 static KeyEntry EmacsKeyMapCtrl[] =
526 { CTRL('@'), WCEL_SetMark },
527 { CTRL('A'), WCEL_MoveToBeg },
528 { CTRL('B'), WCEL_MoveLeft },
529 /* C */
530 { CTRL('D'), WCEL_DeleteCurrChar },
531 { CTRL('E'), WCEL_MoveToEnd },
532 { CTRL('F'), WCEL_MoveRight },
533 { CTRL('G'), WCEL_Beep },
534 { CTRL('H'), WCEL_DeletePrevChar },
535 /* I: meaningless (or tab ???) */
536 { CTRL('J'), WCEL_Done },
537 { CTRL('K'), WCEL_KillToEndOfLine },
538 /* L: [NIY] redraw the whole stuff */
539 { CTRL('M'), WCEL_Done },
540 { CTRL('N'), WCEL_MoveToNextHist },
541 /* O; insert line... meaningless */
542 { CTRL('P'), WCEL_MoveToPrevHist },
543 /* Q: [NIY] quoting... */
544 /* R: [NIY] search backwards... */
545 /* S: [NIY] search forwards... */
546 { CTRL('T'), WCEL_TransposeChar },
547 /* U: [NIY] set repeat count... */
548 /* V: paragraph down... meaningless */
549 { CTRL('W'), WCEL_KillMarkedZone },
550 { CTRL('X'), WCEL_ExchangeMark },
551 { CTRL('Y'), WCEL_Yank },
552 /* Z: meaningless */
553 { 0, NULL }
556 static KeyEntry EmacsKeyMapAlt[] =
558 {/*DEL*/127, WCEL_DeleteLeftWord },
559 { '<', WCEL_MoveToFirstHist },
560 { '>', WCEL_MoveToLastHist },
561 { '?', WCEL_Beep },
562 { 'b', WCEL_MoveToLeftWord },
563 { 'c', WCEL_CapitalizeWord },
564 { 'd', WCEL_DeleteRightWord },
565 { 'f', WCEL_MoveToRightWord },
566 { 'l', WCEL_LowerCaseWord },
567 { 't', WCEL_TransposeWords },
568 { 'u', WCEL_UpperCaseWord },
569 { 'w', WCEL_CopyMarkedZone },
570 { 0, NULL }
573 static KeyEntry EmacsKeyMapExtended[] =
575 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
576 {/*VK_NEXT*/0x22, WCEL_MoveToNextHist },
577 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
578 {/*VK_LEFT*/0x25, WCEL_MoveLeft },
579 { 0, NULL }
582 static KeyMap EmacsKeyMap[] =
584 {0x00000000, 1, StdKeyMap},
585 {0x00000001, 1, EmacsKeyMapAlt}, /* left alt */
586 {0x00000002, 1, EmacsKeyMapAlt}, /* right alt */
587 {0x00000004, 1, EmacsKeyMapCtrl}, /* left ctrl */
588 {0x00000008, 1, EmacsKeyMapCtrl}, /* right ctrl */
589 {0x00000100, 0, EmacsKeyMapExtended},
590 {0, 0, 0}
593 static KeyEntry Win32KeyMapExtended[] =
595 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
596 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
597 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
598 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
599 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
600 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
601 { 0, NULL }
604 static KeyEntry Win32KeyMapCtrlExtended[] =
606 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
607 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
608 { 0, NULL }
611 KeyMap Win32KeyMap[] =
613 {0x00000000, 1, StdKeyMap},
614 {0x00000100, 0, Win32KeyMapExtended},
615 {0x00000104, 0, Win32KeyMapCtrlExtended},
616 {0x00000108, 0, Win32KeyMapCtrlExtended},
617 {0, 0, 0}
619 #undef CTRL
621 /* ====================================================================
623 * Read line master function
625 * ====================================================================*/
627 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, int use_emacs)
629 WCEL_Context ctx;
630 INPUT_RECORD ir;
631 KeyMap* km;
632 KeyEntry* ke;
633 unsigned ofs;
634 void (*func)(struct WCEL_Context* ctx);
635 DWORD ks;
637 memset(&ctx, 0, sizeof(ctx));
638 ctx.hConIn = hConsoleIn;
639 WCEL_HistoryInit(&ctx);
640 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
641 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
642 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
643 return NULL;
644 if (!WCEL_Grow(&ctx, 1))
646 CloseHandle(ctx.hConOut);
647 return NULL;
649 ctx.line[0] = 0;
651 /* EPP WCEL_Dump(&ctx, "init"); */
653 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
655 if (ir.EventType != KEY_EVENT || !ir.Event.KeyEvent.bKeyDown) continue;
656 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08lx\n",
657 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
658 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
659 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
661 /* EPP WCEL_Dump(&ctx, "before func"); */
662 ofs = ctx.ofs;
663 /* mask out some bits which don't interest us */
664 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON);
666 func = NULL;
667 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
669 if (km->keyState != ks)
670 continue;
671 if (km->chkChar)
673 for (ke = &km->entries[0]; ke->func != 0; ke++)
674 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
676 else
678 for (ke = &km->entries[0]; ke->func != 0; ke++)
679 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
682 if (ke->func)
684 func = ke->func;
685 break;
689 if (func)
690 (func)(&ctx);
691 else if (!(ir.Event.KeyEvent.dwControlKeyState & (ENHANCED_KEY|LEFT_ALT_PRESSED)))
692 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
693 else TRACE("Dropped event\n");
695 /* EPP WCEL_Dump(&ctx, "after func"); */
696 if (ctx.ofs != ofs)
697 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
699 if (ctx.error)
701 HeapFree(GetProcessHeap(), 0, ctx.line);
702 ctx.line = NULL;
704 if (ctx.line)
705 CONSOLE_AppendHistory(ctx.line);
707 CloseHandle(ctx.hConOut);
708 if (ctx.histCurr) HeapFree(GetProcessHeap(), 0, ctx.histCurr);
709 return ctx.line;