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);
39 extern void CONSOLE_FillLineUniform(HANDLE hConsoleOutput
, int i
, int j
, int len
, LPCHAR_INFO lpFill
);
45 WCHAR val
; /* vk or unicode char */
46 void (*func
)(struct WCEL_Context
* ctx
);
51 DWORD keyState
; /* keyState (from INPUT_RECORD) to match */
52 BOOL chkChar
; /* check vk or char */
53 KeyEntry
* entries
; /* array of entries */
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) */
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 */
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"
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
));
88 /* ====================================================================
90 * Console helper functions
92 * ====================================================================*/
94 static BOOL
WCEL_Get(WCEL_Context
* ctx
, INPUT_RECORD
* ir
)
100 /* data available ? */
101 if (ReadConsoleInputW(ctx
->hConIn
, ir
, 1, &retv
) && retv
== 1)
104 switch (WaitForSingleObject(ctx
->hConIn
, INFINITE
))
109 /* we have checked that hConIn was a console handle (could be sb) */
110 ERR("Shouldn't happen\n");
115 ERR("hmm bad situation\n");
121 static inline void WCEL_Beep(WCEL_Context
* ctx
)
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
)
134 unsigned len
= ctx
->csbi
.dwSize
.X
- ctx
->csbi
.dwCursorPosition
.X
;
136 c
.Y
= ctx
->csbi
.dwCursorPosition
.Y
;
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
;
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");
167 if (ctx
->len
+ len
>= ctx
->alloc
)
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
;
177 ctx
->alloc
= newsize
;
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
);
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
);
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
);
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
));
228 ctx
->line
[ctx
->len
] = 0;
229 WCEL_Update(ctx
, ctx
->ofs
, ctx
->len
- ctx
->ofs
);
234 static void WCEL_InsertChar(WCEL_Context
* ctx
, WCHAR c
)
238 /* do not insert 0..31 control characters */
239 if (c
< ' ' && c
!= '\t') return;
243 WCEL_InsertString(ctx
, buffer
);
246 static void WCEL_FreeYank(WCEL_Context
* ctx
)
250 HeapFree(GetProcessHeap(), 0, ctx
->yanked
);
255 static void WCEL_SaveYank(WCEL_Context
* ctx
, int beg
, int end
)
258 if (len
<= 0) return;
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
)
278 while (ofs
>= 0 && !WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
279 while (ofs
>= 0 && WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
284 static int WCEL_GetRightWordTransition(WCEL_Context
* ctx
, int 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
)
296 if (idx
== ctx
->histSize
- 1)
298 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx
->histCurr
) + 1) * sizeof(WCHAR
));
299 lstrcpyW(ptr
, ctx
->histCurr
);
303 int len
= CONSOLE_GetHistory(idx
, NULL
, 0);
305 if ((ptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
307 CONSOLE_GetHistory(idx
, ptr
, len
);
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
);
335 /* insert new string */
336 if (WCEL_Grow(ctx
, len
))
338 WCEL_InsertString(ctx
, data
);
339 HeapFree(GetProcessHeap(), 0, data
);
344 static void WCEL_FindPrevInHist(WCEL_Context
* ctx
)
346 int startPos
= ctx
->histPos
;
350 if (ctx
->histPos
&& ctx
->histPos
== ctx
->histSize
) {
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
))
372 WCEL_InsertString(ctx
, data
);
374 SetConsoleCursorPosition(ctx
->hConOut
, WCEL_GetCoord(ctx
, ctx
->ofs
));
375 HeapFree(GetProcessHeap(), 0, data
);
379 } while (ctx
->histPos
!= startPos
);
384 /* ====================================================================
386 * basic edition functions
388 * ====================================================================*/
390 static void WCEL_Done(WCEL_Context
* ctx
)
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
);
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
)
427 static void WCEL_MoveToEnd(WCEL_Context
* ctx
)
432 static void WCEL_SetMark(WCEL_Context
* ctx
)
434 ctx
->mark
= ctx
->ofs
;
437 static void WCEL_ExchangeMark(WCEL_Context
* ctx
)
441 if (ctx
->mark
> ctx
->len
) return;
443 ctx
->ofs
= ctx
->mark
;
447 static void WCEL_CopyMarkedZone(WCEL_Context
* ctx
)
451 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
452 if (ctx
->mark
> ctx
->ofs
)
454 beg
= ctx
->ofs
; end
= ctx
->mark
;
458 beg
= ctx
->mark
; end
= ctx
->ofs
;
460 WCEL_SaveYank(ctx
, beg
, end
);
463 static void WCEL_TransposeChar(WCEL_Context
* ctx
)
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);
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
));
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
)
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);
512 static void WCEL_UpperCaseWord(WCEL_Context
* ctx
)
514 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
515 if (new_ofs
!= ctx
->ofs
)
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);
525 static void WCEL_CapitalizeWord(WCEL_Context
* ctx
)
527 int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
528 if (new_ofs
!= ctx
->ofs
)
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);
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
)
555 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
556 if (ctx
->mark
> ctx
->ofs
)
558 beg
= ctx
->ofs
; end
= ctx
->mark
;
562 beg
= ctx
->mark
; end
= ctx
->ofs
;
564 WCEL_SaveYank(ctx
, beg
, end
);
565 WCEL_DeleteString(ctx
, beg
, end
);
569 static void WCEL_DeletePrevChar(WCEL_Context
* ctx
)
573 WCEL_DeleteString(ctx
, ctx
->ofs
- 1, 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
);
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
);
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
)
639 /* FIXME: wait untill all console code is in kernel32 */
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)
650 if (ir
.Event
.KeyEvent
.uChar
.UnicodeChar
< '0' ||
651 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
> '9')
653 repeat
= repeat
* 10 + ir
.Event
.KeyEvent
.uChar
.UnicodeChar
- '0';
655 WCEL_Get(ctx
, &ir
, TRUE
);
657 FIXME("=> %u\n", repeat
);
661 /* ====================================================================
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
},
676 static KeyEntry Win32ExtraStdKeyMap
[] =
678 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist
},
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
},
715 static KeyEntry EmacsKeyMapAlt
[] =
717 {/*DEL*/127, WCEL_DeleteLeftWord
},
718 { '<', WCEL_MoveToFirstHist
},
719 { '>', WCEL_MoveToLastHist
},
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
},
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
},
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
},
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
},
768 static KeyEntry Win32KeyMapCtrlExtended
[] =
770 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord
},
771 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord
},
772 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine
},
776 KeyMap Win32KeyMap
[] =
778 {0x00000000, 1, StdKeyMap
},
779 {0x00000000, 0, Win32ExtraStdKeyMap
},
780 {0x00000100, 0, Win32KeyMapExtended
},
781 {0x00000104, 0, Win32KeyMapCtrlExtended
},
782 {0x00000108, 0, Win32KeyMapCtrlExtended
},
787 /* ====================================================================
789 * Read line master function
791 * ====================================================================*/
793 WCHAR
* CONSOLE_Readline(HANDLE hConsoleIn
, int use_emacs
)
800 void (*func
)(struct WCEL_Context
* ctx
);
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
))
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
);
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"); */
831 /* mask out some bits which don't interest us */
832 ks
= ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
);
835 for (km
= (use_emacs
) ? EmacsKeyMap
: Win32KeyMap
; km
->entries
!= NULL
; km
++)
837 if (km
->keyState
!= ks
)
841 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
842 if (ke
->val
== ir
.Event
.KeyEvent
.uChar
.UnicodeChar
) break;
846 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
847 if (ke
->val
== ir
.Event
.KeyEvent
.wVirtualKeyCode
) break;
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"); */
865 SetConsoleCursorPosition(ctx
.hConOut
, WCEL_GetCoord(&ctx
, ctx
.ofs
));
869 HeapFree(GetProcessHeap(), 0, ctx
.line
);
874 CONSOLE_AppendHistory(ctx
.line
);
876 CloseHandle(ctx
.hConOut
);
877 if (ctx
.histCurr
) HeapFree(GetProcessHeap(), 0, ctx
.histCurr
);