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"
30 #include "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "console_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(console
);
41 WCHAR val
; /* vk or unicode char */
42 void (*func
)(struct WCEL_Context
* ctx
);
47 DWORD keyState
; /* keyState (from INPUT_RECORD) to match */
48 BOOL chkChar
; /* check vk or char */
49 KeyEntry
* entries
; /* array of entries */
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) */
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 */
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"
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
));
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");
98 static inline void WCEL_Beep(WCEL_Context
* ctx
)
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
)
111 unsigned len
= ctx
->csbi
.dwSize
.X
- ctx
->csbi
.dwCursorPosition
.X
;
113 c
.Y
= ctx
->csbi
.dwCursorPosition
.Y
;
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
;
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");
146 if (ctx
->len
+ len
>= ctx
->alloc
)
151 /* round up size to 32 byte-WCHAR boundary */
152 newsize
= (ctx
->len
+ len
+ 1 + 31) & ~31;
153 newline
= HeapReAlloc(GetProcessHeap(), 0, ctx
->line
, sizeof(WCHAR
) * newsize
);
154 if (!newline
) return FALSE
;
156 ctx
->alloc
= newsize
;
161 static void WCEL_DeleteString(WCEL_Context
* ctx
, int beg
, int end
)
163 unsigned str_len
= end
- beg
;
164 COORD cbeg
= WCEL_GetCoord(ctx
, ctx
->len
- str_len
);
165 COORD cend
= WCEL_GetCoord(ctx
, ctx
->len
);
169 memmove(&ctx
->line
[beg
], &ctx
->line
[end
], (ctx
->len
- end
) * sizeof(WCHAR
));
170 /* we need to clean from ctx->len - str_len to ctx->len */
172 ci
.Char
.UnicodeChar
= ' ';
173 ci
.Attributes
= ctx
->csbi
.wAttributes
;
175 if (cbeg
.Y
== cend
.Y
)
177 /* partial erase of sole line */
178 CONSOLE_FillLineUniform(ctx
->hConOut
, cbeg
.X
, cbeg
.Y
,
179 cend
.X
- cbeg
.X
, &ci
);
184 /* erase til eol on first line */
185 CONSOLE_FillLineUniform(ctx
->hConOut
, cbeg
.X
, cbeg
.Y
,
186 ctx
->csbi
.dwSize
.X
- cbeg
.X
, &ci
);
187 /* completly erase all the others (full lines) */
188 for (i
= cbeg
.Y
+ 1; i
< cend
.Y
; i
++)
189 CONSOLE_FillLineUniform(ctx
->hConOut
, 0, i
, ctx
->csbi
.dwSize
.X
, &ci
);
190 /* erase from beg of line until last pos on last line */
191 CONSOLE_FillLineUniform(ctx
->hConOut
, 0, cend
.Y
, cend
.X
, &ci
);
194 WCEL_Update(ctx
, 0, ctx
->len
);
195 ctx
->line
[ctx
->len
] = 0;
198 static void WCEL_InsertString(WCEL_Context
* ctx
, const WCHAR
* str
)
200 size_t len
= lstrlenW(str
);
202 if (!len
|| !WCEL_Grow(ctx
, len
)) return;
203 if (ctx
->len
> ctx
->ofs
)
204 memmove(&ctx
->line
[ctx
->ofs
+ len
], &ctx
->line
[ctx
->ofs
], (ctx
->len
- ctx
->ofs
) * sizeof(WCHAR
));
205 memcpy(&ctx
->line
[ctx
->ofs
], str
, len
* sizeof(WCHAR
));
207 ctx
->line
[ctx
->len
] = 0;
208 WCEL_Update(ctx
, ctx
->ofs
, ctx
->len
- ctx
->ofs
);
213 static void WCEL_InsertChar(WCEL_Context
* ctx
, WCHAR c
)
217 /* do not insert 0..31 control characters */
218 if (c
< ' ' && c
!= '\t') return;
222 WCEL_InsertString(ctx
, buffer
);
225 static void WCEL_FreeYank(WCEL_Context
* ctx
)
229 HeapFree(GetProcessHeap(), 0, ctx
->yanked
);
234 static void WCEL_SaveYank(WCEL_Context
* ctx
, int beg
, int end
)
237 if (len
<= 0) return;
240 ctx
->yanked
= HeapReAlloc(GetProcessHeap(), 0, ctx
->yanked
, (len
+ 1) * sizeof(WCHAR
));
241 if (!ctx
->yanked
) return;
242 memcpy(ctx
->yanked
, &ctx
->line
[beg
], len
* sizeof(WCHAR
));
243 ctx
->yanked
[len
] = 0;
246 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
247 * of the data lay in unicode lib
249 static inline BOOL
WCEL_iswalnum(WCHAR wc
)
251 return get_char_typeW(wc
) & (C1_ALPHA
|C1_DIGIT
|C1_LOWER
|C1_UPPER
);
254 static int WCEL_GetLeftWordTransition(WCEL_Context
* ctx
, int ofs
)
257 while (ofs
>= 0 && !WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
258 while (ofs
>= 0 && WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
263 static int WCEL_GetRightWordTransition(WCEL_Context
* ctx
, int ofs
)
266 while (ofs
<= ctx
->len
&& WCEL_iswalnum(ctx
->line
[ofs
])) ofs
++;
267 while (ofs
<= ctx
->len
&& !WCEL_iswalnum(ctx
->line
[ofs
])) ofs
++;
268 return min(ofs
, ctx
->len
);
271 static WCHAR
* WCEL_GetHistory(WCEL_Context
* ctx
, int idx
)
275 if (idx
== ctx
->histSize
- 1)
277 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx
->histCurr
) + 1) * sizeof(WCHAR
));
278 lstrcpyW(ptr
, ctx
->histCurr
);
282 int len
= CONSOLE_GetHistory(idx
, NULL
, 0);
284 if ((ptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
286 CONSOLE_GetHistory(idx
, ptr
, len
);
292 static void WCEL_HistoryInit(WCEL_Context
* ctx
)
294 ctx
->histPos
= CONSOLE_GetNumHistoryEntries();
295 ctx
->histSize
= ctx
->histPos
+ 1;
296 ctx
->histCurr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WCHAR
));
299 static void WCEL_MoveToHist(WCEL_Context
* ctx
, int idx
)
301 WCHAR
* data
= WCEL_GetHistory(ctx
, idx
);
302 int len
= lstrlenW(data
) + 1;
304 /* save current line edition for recall when needed (FIXME seems broken to me) */
305 if (ctx
->histPos
== ctx
->histSize
- 1)
307 if (ctx
->histCurr
) HeapFree(GetProcessHeap(), 0, ctx
->histCurr
);
308 ctx
->histCurr
= HeapAlloc(GetProcessHeap(), 0, (ctx
->len
+ 1) * sizeof(WCHAR
));
309 memcpy(ctx
->histCurr
, ctx
->line
, (ctx
->len
+ 1) * sizeof(WCHAR
));
311 /* need to clean also the screen if new string is shorter than old one */
312 WCEL_DeleteString(ctx
, 0, ctx
->len
);
314 /* insert new string */
315 if (WCEL_Grow(ctx
, len
))
317 WCEL_InsertString(ctx
, data
);
318 HeapFree(GetProcessHeap(), 0, data
);
323 static void WCEL_FindPrevInHist(WCEL_Context
* ctx
)
325 int startPos
= ctx
->histPos
;
329 if (ctx
->histPos
&& ctx
->histPos
== ctx
->histSize
) {
335 data
= WCEL_GetHistory(ctx
, ctx
->histPos
);
337 if (ctx
->histPos
) ctx
->histPos
--;
338 else ctx
->histPos
= (ctx
->histSize
-1);
340 len
= lstrlenW(data
) + 1;
341 if ((len
>= ctx
->ofs
) &&
342 (memcmp(ctx
->line
, data
, ctx
->ofs
* sizeof(WCHAR
)) == 0)) {
344 /* need to clean also the screen if new string is shorter than old one */
345 WCEL_DeleteString(ctx
, 0, ctx
->len
);
347 if (WCEL_Grow(ctx
, len
))
351 WCEL_InsertString(ctx
, data
);
353 SetConsoleCursorPosition(ctx
->hConOut
, WCEL_GetCoord(ctx
, ctx
->ofs
));
354 HeapFree(GetProcessHeap(), 0, data
);
358 } while (ctx
->histPos
!= startPos
);
363 /* ====================================================================
365 * basic edition functions
367 * ====================================================================*/
369 static void WCEL_Done(WCEL_Context
* ctx
)
372 if (!WCEL_Grow(ctx
, 1)) return;
373 ctx
->line
[ctx
->len
++] = '\n';
374 ctx
->line
[ctx
->len
] = 0;
375 WriteConsoleW(ctx
->hConOut
, &nl
, 1, NULL
, NULL
);
379 static void WCEL_MoveLeft(WCEL_Context
* ctx
)
381 if (ctx
->ofs
> 0) ctx
->ofs
--;
384 static void WCEL_MoveRight(WCEL_Context
* ctx
)
386 if (ctx
->ofs
< ctx
->len
) ctx
->ofs
++;
389 static void WCEL_MoveToLeftWord(WCEL_Context
* ctx
)
391 int new_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
);
392 if (new_ofs
!= ctx
->ofs
) ctx
->ofs
= new_ofs
;
395 static void WCEL_MoveToRightWord(WCEL_Context
* ctx
)
397 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
398 if (new_ofs
!= ctx
->ofs
) ctx
->ofs
= new_ofs
;
401 static void WCEL_MoveToBeg(WCEL_Context
* ctx
)
406 static void WCEL_MoveToEnd(WCEL_Context
* ctx
)
411 static void WCEL_SetMark(WCEL_Context
* ctx
)
413 ctx
->mark
= ctx
->ofs
;
416 static void WCEL_ExchangeMark(WCEL_Context
* ctx
)
420 if (ctx
->mark
> ctx
->len
) return;
422 ctx
->ofs
= ctx
->mark
;
426 static void WCEL_CopyMarkedZone(WCEL_Context
* ctx
)
430 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
431 if (ctx
->mark
> ctx
->ofs
)
433 beg
= ctx
->ofs
; end
= ctx
->mark
;
437 beg
= ctx
->mark
; end
= ctx
->ofs
;
439 WCEL_SaveYank(ctx
, beg
, end
);
442 static void WCEL_TransposeChar(WCEL_Context
* ctx
)
446 if (!ctx
->ofs
|| ctx
->ofs
== ctx
->len
) return;
448 c
= ctx
->line
[ctx
->ofs
];
449 ctx
->line
[ctx
->ofs
] = ctx
->line
[ctx
->ofs
- 1];
450 ctx
->line
[ctx
->ofs
- 1] = c
;
452 WCEL_Update(ctx
, ctx
->ofs
- 1, 2);
456 static void WCEL_TransposeWords(WCEL_Context
* ctx
)
458 int left_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
),
459 right_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
460 if (left_ofs
< ctx
->ofs
&& right_ofs
> ctx
->ofs
)
462 unsigned len_r
= right_ofs
- ctx
->ofs
;
463 unsigned len_l
= ctx
->ofs
- left_ofs
;
465 char* tmp
= HeapAlloc(GetProcessHeap(), 0, len_r
* sizeof(WCHAR
));
468 memcpy(tmp
, &ctx
->line
[ctx
->ofs
], len_r
* sizeof(WCHAR
));
469 memmove(&ctx
->line
[left_ofs
+ len_r
], &ctx
->line
[left_ofs
], len_l
* sizeof(WCHAR
));
470 memcpy(&ctx
->line
[left_ofs
], tmp
, len_r
* sizeof(WCHAR
));
472 HeapFree(GetProcessHeap(), 0, tmp
);
473 WCEL_Update(ctx
, left_ofs
, len_l
+ len_r
);
474 ctx
->ofs
= right_ofs
;
478 static void WCEL_LowerCaseWord(WCEL_Context
* ctx
)
480 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
481 if (new_ofs
!= ctx
->ofs
)
484 for (i
= ctx
->ofs
; i
<= new_ofs
; i
++)
485 ctx
->line
[i
] = tolowerW(ctx
->line
[i
]);
486 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
491 static void WCEL_UpperCaseWord(WCEL_Context
* ctx
)
493 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
494 if (new_ofs
!= ctx
->ofs
)
497 for (i
= ctx
->ofs
; i
<= new_ofs
; i
++)
498 ctx
->line
[i
] = toupperW(ctx
->line
[i
]);
499 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
504 static void WCEL_CapitalizeWord(WCEL_Context
* ctx
)
506 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
507 if (new_ofs
!= ctx
->ofs
)
511 ctx
->line
[ctx
->ofs
] = toupperW(ctx
->line
[ctx
->ofs
]);
512 for (i
= ctx
->ofs
+ 1; i
<= new_ofs
; i
++)
513 ctx
->line
[i
] = tolowerW(ctx
->line
[i
]);
514 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
519 static void WCEL_Yank(WCEL_Context
* ctx
)
521 WCEL_InsertString(ctx
, ctx
->yanked
);
524 static void WCEL_KillToEndOfLine(WCEL_Context
* ctx
)
526 WCEL_SaveYank(ctx
, ctx
->ofs
, ctx
->len
);
527 WCEL_DeleteString(ctx
, ctx
->ofs
, ctx
->len
);
530 static void WCEL_KillMarkedZone(WCEL_Context
* ctx
)
534 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
535 if (ctx
->mark
> ctx
->ofs
)
537 beg
= ctx
->ofs
; end
= ctx
->mark
;
541 beg
= ctx
->mark
; end
= ctx
->ofs
;
543 WCEL_SaveYank(ctx
, beg
, end
);
544 WCEL_DeleteString(ctx
, beg
, end
);
548 static void WCEL_DeletePrevChar(WCEL_Context
* ctx
)
552 WCEL_DeleteString(ctx
, ctx
->ofs
- 1, ctx
->ofs
);
557 static void WCEL_DeleteCurrChar(WCEL_Context
* ctx
)
559 if (ctx
->ofs
< ctx
->len
)
560 WCEL_DeleteString(ctx
, ctx
->ofs
, ctx
->ofs
+ 1);
563 static void WCEL_DeleteLeftWord(WCEL_Context
* ctx
)
565 int new_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
);
566 if (new_ofs
!= ctx
->ofs
)
568 WCEL_DeleteString(ctx
, new_ofs
, ctx
->ofs
);
573 static void WCEL_DeleteRightWord(WCEL_Context
* ctx
)
575 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
576 if (new_ofs
!= ctx
->ofs
)
578 WCEL_DeleteString(ctx
, ctx
->ofs
, new_ofs
);
582 static void WCEL_MoveToPrevHist(WCEL_Context
* ctx
)
584 if (ctx
->histPos
) WCEL_MoveToHist(ctx
, ctx
->histPos
- 1);
587 static void WCEL_MoveToNextHist(WCEL_Context
* ctx
)
589 if (ctx
->histPos
< ctx
->histSize
- 1) WCEL_MoveToHist(ctx
, ctx
->histPos
+ 1);
592 static void WCEL_MoveToFirstHist(WCEL_Context
* ctx
)
594 if (ctx
->histPos
!= 0) WCEL_MoveToHist(ctx
, 0);
597 static void WCEL_MoveToLastHist(WCEL_Context
* ctx
)
599 if (ctx
->histPos
!= ctx
->histSize
- 1) WCEL_MoveToHist(ctx
, ctx
->histSize
- 1);
602 static void WCEL_Redraw(WCEL_Context
* ctx
)
604 COORD c
= WCEL_GetCoord(ctx
, ctx
->len
);
607 WCEL_Update(ctx
, 0, ctx
->len
);
609 ci
.Char
.UnicodeChar
= ' ';
610 ci
.Attributes
= ctx
->csbi
.wAttributes
;
612 CONSOLE_FillLineUniform(ctx
->hConOut
, c
.X
, c
.Y
, ctx
->csbi
.dwSize
.X
- c
.X
, &ci
);
615 static void WCEL_RepeatCount(WCEL_Context
* ctx
)
618 /* FIXME: wait until all console code is in kernel32 */
622 while (WCEL_Get(ctx
, &ir
, FALSE
))
624 if (ir
.EventType
!= KEY_EVENT
) break;
625 if (ir
.Event
.KeyEvent
.bKeyDown
)
627 if ((ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
)) != 0)
629 if (ir
.Event
.KeyEvent
.uChar
.UnicodeChar
< '0' ||
630 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
> '9')
632 repeat
= repeat
* 10 + ir
.Event
.KeyEvent
.uChar
.UnicodeChar
- '0';
634 WCEL_Get(ctx
, &ir
, TRUE
);
636 FIXME("=> %u\n", repeat
);
640 /* ====================================================================
644 * ====================================================================*/
646 #define CTRL(x) ((x) - '@')
647 static KeyEntry StdKeyMap
[] =
649 {/*BACK*/0x08, WCEL_DeletePrevChar
},
650 {/*RETURN*/0x0d, WCEL_Done
},
651 {/*DEL*/127, WCEL_DeleteCurrChar
},
655 static KeyEntry Win32ExtraStdKeyMap
[] =
657 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist
},
662 static KeyEntry EmacsKeyMapCtrl
[] =
664 { CTRL('@'), WCEL_SetMark
},
665 { CTRL('A'), WCEL_MoveToBeg
},
666 { CTRL('B'), WCEL_MoveLeft
},
667 /* C: done in server */
668 { CTRL('D'), WCEL_DeleteCurrChar
},
669 { CTRL('E'), WCEL_MoveToEnd
},
670 { CTRL('F'), WCEL_MoveRight
},
671 { CTRL('G'), WCEL_Beep
},
672 { CTRL('H'), WCEL_DeletePrevChar
},
673 /* I: meaningless (or tab ???) */
674 { CTRL('J'), WCEL_Done
},
675 { CTRL('K'), WCEL_KillToEndOfLine
},
676 { CTRL('L'), WCEL_Redraw
},
677 { CTRL('M'), WCEL_Done
},
678 { CTRL('N'), WCEL_MoveToNextHist
},
679 /* O; insert line... meaningless */
680 { CTRL('P'), WCEL_MoveToPrevHist
},
681 /* Q: [NIY] quoting... */
682 /* R: [NIY] search backwards... */
683 /* S: [NIY] search forwards... */
684 { CTRL('T'), WCEL_TransposeChar
},
685 { CTRL('U'), WCEL_RepeatCount
},
686 /* V: paragraph down... meaningless */
687 { CTRL('W'), WCEL_KillMarkedZone
},
688 { CTRL('X'), WCEL_ExchangeMark
},
689 { CTRL('Y'), WCEL_Yank
},
694 static KeyEntry EmacsKeyMapAlt
[] =
696 {/*DEL*/127, WCEL_DeleteLeftWord
},
697 { '<', WCEL_MoveToFirstHist
},
698 { '>', WCEL_MoveToLastHist
},
700 { 'b', WCEL_MoveToLeftWord
},
701 { 'c', WCEL_CapitalizeWord
},
702 { 'd', WCEL_DeleteRightWord
},
703 { 'f', WCEL_MoveToRightWord
},
704 { 'l', WCEL_LowerCaseWord
},
705 { 't', WCEL_TransposeWords
},
706 { 'u', WCEL_UpperCaseWord
},
707 { 'w', WCEL_CopyMarkedZone
},
711 static KeyEntry EmacsKeyMapExtended
[] =
713 {/*RETURN*/ 0x0d, WCEL_Done
},
714 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist
},
715 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist
},
716 {/*VK_END*/ 0x23, WCEL_MoveToEnd
},
717 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg
},
718 {/*VK_RIGHT*/0x27, WCEL_MoveRight
},
719 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft
},
720 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar
},
724 static KeyMap EmacsKeyMap
[] =
726 {0x00000000, 1, StdKeyMap
},
727 {0x00000001, 1, EmacsKeyMapAlt
}, /* left alt */
728 {0x00000002, 1, EmacsKeyMapAlt
}, /* right alt */
729 {0x00000004, 1, EmacsKeyMapCtrl
}, /* left ctrl */
730 {0x00000008, 1, EmacsKeyMapCtrl
}, /* right ctrl */
731 {0x00000100, 0, EmacsKeyMapExtended
},
735 static KeyEntry Win32KeyMapExtended
[] =
737 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft
},
738 {/*VK_RIGHT*/0x27, WCEL_MoveRight
},
739 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg
},
740 {/*VK_END*/ 0x23, WCEL_MoveToEnd
},
741 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist
},
742 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist
},
743 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar
},
747 static KeyEntry Win32KeyMapCtrlExtended
[] =
749 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord
},
750 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord
},
751 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine
},
755 KeyMap Win32KeyMap
[] =
757 {0x00000000, 1, StdKeyMap
},
758 {0x00000000, 0, Win32ExtraStdKeyMap
},
759 {0x00000100, 0, Win32KeyMapExtended
},
760 {0x00000104, 0, Win32KeyMapCtrlExtended
},
761 {0x00000108, 0, Win32KeyMapCtrlExtended
},
766 /* ====================================================================
768 * Read line master function
770 * ====================================================================*/
772 WCHAR
* CONSOLE_Readline(HANDLE hConsoleIn
)
779 void (*func
)(struct WCEL_Context
* ctx
);
783 memset(&ctx
, 0, sizeof(ctx
));
784 ctx
.hConIn
= hConsoleIn
;
785 WCEL_HistoryInit(&ctx
);
787 if (!CONSOLE_GetEditionMode(hConsoleIn
, &use_emacs
))
790 if ((ctx
.hConOut
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
791 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
||
792 !GetConsoleScreenBufferInfo(ctx
.hConOut
, &ctx
.csbi
))
794 ctx
.can_wrap
= (GetConsoleMode(ctx
.hConOut
, &ks
) && (ks
& ENABLE_WRAP_AT_EOL_OUTPUT
)) ? 1 : 0;
796 if (!WCEL_Grow(&ctx
, 1))
798 CloseHandle(ctx
.hConOut
);
803 /* EPP WCEL_Dump(&ctx, "init"); */
805 while (!ctx
.done
&& !ctx
.error
&& WCEL_Get(&ctx
, &ir
))
807 if (ir
.EventType
!= KEY_EVENT
) continue;
808 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08lx\n",
809 ir
.Event
.KeyEvent
.bKeyDown
? "Down" : "Up ", ir
.Event
.KeyEvent
.wRepeatCount
,
810 ir
.Event
.KeyEvent
.wVirtualKeyCode
, ir
.Event
.KeyEvent
.wVirtualScanCode
,
811 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
, ir
.Event
.KeyEvent
.dwControlKeyState
);
812 if (!ir
.Event
.KeyEvent
.bKeyDown
) continue;
814 /* EPP WCEL_Dump(&ctx, "before func"); */
816 /* mask out some bits which don't interest us */
817 ks
= ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
);
820 for (km
= (use_emacs
) ? EmacsKeyMap
: Win32KeyMap
; km
->entries
!= NULL
; km
++)
822 if (km
->keyState
!= ks
)
826 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
827 if (ke
->val
== ir
.Event
.KeyEvent
.uChar
.UnicodeChar
) break;
831 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
832 if (ke
->val
== ir
.Event
.KeyEvent
.wVirtualKeyCode
) break;
844 else if (!(ir
.Event
.KeyEvent
.dwControlKeyState
& (ENHANCED_KEY
|LEFT_ALT_PRESSED
)))
845 WCEL_InsertChar(&ctx
, ir
.Event
.KeyEvent
.uChar
.UnicodeChar
);
846 else TRACE("Dropped event\n");
848 /* EPP WCEL_Dump(&ctx, "after func"); */
850 SetConsoleCursorPosition(ctx
.hConOut
, WCEL_GetCoord(&ctx
, ctx
.ofs
));
854 HeapFree(GetProcessHeap(), 0, ctx
.line
);
859 CONSOLE_AppendHistory(ctx
.line
);
861 CloseHandle(ctx
.hConOut
);
862 if (ctx
.histCurr
) HeapFree(GetProcessHeap(), 0, ctx
.histCurr
);