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
22 #include "wine/port.h"
29 #include "wine/unicode.h"
31 #include "wine/debug.h"
33 WINE_DEFAULT_DEBUG_CHANNEL(console
);
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);
44 WCHAR val
; /* vk or unicode char */
45 void (*func
)(struct WCEL_Context
* ctx
);
50 DWORD keyState
; /* keyState (from INPUT_RECORD) to match */
51 BOOL chkChar
; /* check vk or char */
52 KeyEntry
* entries
; /* array of entries */
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) */
65 unsigned done
: 1, /* to 1 when we're done with editing */
66 error
: 1; /* to 1 when an error occurred in the editing */
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
));
84 /* ====================================================================
86 * Console helper functions
88 * ====================================================================*/
90 static BOOL
WCEL_Get(WCEL_Context
* ctx
, INPUT_RECORD
* ir
)
96 /* data available ? */
97 if (ReadConsoleInputW(ctx
->hConIn
, ir
, 1, &retv
) && retv
== 1)
100 switch (WaitForSingleObject(ctx
->hConIn
, INFINITE
))
105 /* we have checked that hConIn was a console handle (could be sb) */
106 ERR("Shouldn't happen\n");
111 ERR("hmm bad situation\n");
117 static inline void WCEL_Beep(WCEL_Context
* ctx
)
122 static inline COORD
WCEL_GetCoord(WCEL_Context
* ctx
, int ofs
)
125 c
.X
= ctx
->csbi
.dwCursorPosition
.X
+ ofs
;
126 c
.Y
= ctx
->csbi
.dwCursorPosition
.Y
;
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");
152 if (ctx
->len
+ len
>= ctx
->alloc
)
155 newline
= HeapReAlloc(GetProcessHeap(), 0, ctx
->line
, sizeof(WCHAR
) * (ctx
->alloc
+ 32));
156 if (!newline
) return FALSE
;
163 static void WCEL_DeleteString(WCEL_Context
* ctx
, int beg
, int end
)
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
));
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
);
199 static void WCEL_InsertChar(WCEL_Context
* ctx
, WCHAR c
)
203 /* do not insert 0..31 control characters */
206 if (c
!= '\t') return;
210 WCEL_InsertString(ctx
, buffer
);
213 static void WCEL_SaveYank(WCEL_Context
* ctx
, int beg
, int end
)
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
)
233 while (ofs
>= 0 && !WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
234 while (ofs
>= 0 && WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
239 static int WCEL_GetRightWordTransition(WCEL_Context
* ctx
, int 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
)
251 if (idx
== ctx
->histSize
- 1)
253 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx
->histCurr
) + 1) * sizeof(WCHAR
));
254 lstrcpyW(ptr
, ctx
->histCurr
);
258 int len
= CONSOLE_GetHistory(idx
, NULL
, 0);
260 if ((ptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
262 CONSOLE_GetHistory(idx
, ptr
, len
);
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
);
290 /* insert new string */
291 if (WCEL_Grow(ctx
, len
))
293 WCEL_InsertString(ctx
, data
);
294 HeapFree(GetProcessHeap(), 0, data
);
299 static void WCEL_FindPrevInHist(WCEL_Context
* ctx
)
301 int startPos
= ctx
->histPos
;
305 if (ctx
->histPos
&& ctx
->histPos
== ctx
->histSize
) {
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
))
327 WCEL_InsertString(ctx
, data
);
329 SetConsoleCursorPosition(ctx
->hConOut
, WCEL_GetCoord(ctx
, ctx
->ofs
));
330 HeapFree(GetProcessHeap(), 0, data
);
334 } while (ctx
->histPos
!= startPos
);
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
);
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
)
381 static void WCEL_MoveToEnd(WCEL_Context
* ctx
)
386 static void WCEL_SetMark(WCEL_Context
* ctx
)
388 ctx
->mark
= ctx
->ofs
;
391 static void WCEL_ExchangeMark(WCEL_Context
* ctx
)
395 if (ctx
->mark
> ctx
->len
) return;
397 ctx
->ofs
= ctx
->mark
;
401 static void WCEL_CopyMarkedZone(WCEL_Context
* ctx
)
405 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
406 if (ctx
->mark
> ctx
->ofs
)
408 beg
= ctx
->ofs
; end
= ctx
->mark
;
412 beg
= ctx
->mark
; end
= ctx
->ofs
;
414 WCEL_SaveYank(ctx
, beg
, end
);
417 static void WCEL_TransposeChar(WCEL_Context
* ctx
)
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
);
431 static void WCEL_TransposeWords(WCEL_Context
* ctx
)
436 static void WCEL_LowerCaseWord(WCEL_Context
* ctx
)
438 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
439 if (new_ofs
!= ctx
->ofs
)
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
);
450 static void WCEL_UpperCaseWord(WCEL_Context
* ctx
)
452 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
453 if (new_ofs
!= ctx
->ofs
)
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
);
464 static void WCEL_CapitalizeWord(WCEL_Context
* ctx
)
466 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
467 if (new_ofs
!= ctx
->ofs
)
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
);
480 static void WCEL_Yank(WCEL_Context
* ctx
)
482 WCEL_InsertString(ctx
, ctx
->yanked
);
483 HeapFree(GetProcessHeap(), 0, ctx
->yanked
);
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
)
497 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
498 if (ctx
->mark
> ctx
->ofs
)
500 beg
= ctx
->ofs
; end
= ctx
->mark
;
504 beg
= ctx
->mark
; end
= ctx
->ofs
;
506 WCEL_SaveYank(ctx
, beg
, end
);
507 WCEL_DeleteString(ctx
, beg
, end
);
511 static void WCEL_DeletePrevChar(WCEL_Context
* ctx
)
515 WCEL_DeleteString(ctx
, ctx
->ofs
- 1, 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
);
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 /* ====================================================================
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
},
580 static KeyEntry Win32ExtraStdKeyMap
[] =
582 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist
},
587 static KeyEntry EmacsKeyMapCtrl
[] =
589 { CTRL('@'), WCEL_SetMark
},
590 { CTRL('A'), WCEL_MoveToBeg
},
591 { CTRL('B'), WCEL_MoveLeft
},
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
},
619 static KeyEntry EmacsKeyMapAlt
[] =
621 {/*DEL*/127, WCEL_DeleteLeftWord
},
622 { '<', WCEL_MoveToFirstHist
},
623 { '>', WCEL_MoveToLastHist
},
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
},
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
},
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
},
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
},
671 static KeyEntry Win32KeyMapCtrlExtended
[] =
673 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord
},
674 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord
},
675 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine
},
679 KeyMap Win32KeyMap
[] =
681 {0x00000000, 1, StdKeyMap
},
682 {0x00000000, 0, Win32ExtraStdKeyMap
},
683 {0x00000100, 0, Win32KeyMapExtended
},
684 {0x00000104, 0, Win32KeyMapCtrlExtended
},
685 {0x00000108, 0, Win32KeyMapCtrlExtended
},
690 /* ====================================================================
692 * Read line master function
694 * ====================================================================*/
696 WCHAR
* CONSOLE_Readline(HANDLE hConsoleIn
, int use_emacs
)
703 void (*func
)(struct WCEL_Context
* ctx
);
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
))
713 if (!WCEL_Grow(&ctx
, 1))
715 CloseHandle(ctx
.hConOut
);
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"); */
732 /* mask out some bits which don't interest us */
733 ks
= ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
);
736 for (km
= (use_emacs
) ? EmacsKeyMap
: Win32KeyMap
; km
->entries
!= NULL
; km
++)
738 if (km
->keyState
!= ks
)
742 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
743 if (ke
->val
== ir
.Event
.KeyEvent
.uChar
.UnicodeChar
) break;
747 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
748 if (ke
->val
== ir
.Event
.KeyEvent
.wVirtualKeyCode
) break;
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"); */
766 SetConsoleCursorPosition(ctx
.hConOut
, WCEL_GetCoord(&ctx
, ctx
.ofs
));
770 HeapFree(GetProcessHeap(), 0, ctx
.line
);
774 CONSOLE_AppendHistory(ctx
.line
);
776 CloseHandle(ctx
.hConOut
);
777 if (ctx
.histCurr
) HeapFree(GetProcessHeap(), 0, ctx
.histCurr
);