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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, 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 const 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 last_rub
; /* number of chars to rub to get to start
57 (for consoles that can't change cursor pos) */
58 unsigned last_max
; /* max number of chars written
59 (for consoles that can't change cursor pos) */
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 */
69 shall_echo
: 1, /* to 1 when characters should be echo:ed when keyed-in */
70 can_pos_cursor
: 1; /* to 1 when console can (re)position cursor */
77 static void WCEL_Dump(WCEL_Context
* ctx
, const char* pfx
)
79 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
80 "\t\thist=(size=%u pos=%u curr=%s)\n"
82 pfx
, debugstr_w(ctx
->line
), ctx
->alloc
, ctx
->ofs
, ctx
->len
,
83 ctx
->csbi
.dwCursorPosition
.X
, ctx
->csbi
.dwCursorPosition
.Y
,
84 ctx
->done
? 'D' : 'd', ctx
->error
? 'E' : 'e', ctx
->can_wrap
? 'W' : 'w',
85 ctx
->histSize
, ctx
->histPos
, debugstr_w(ctx
->histCurr
),
86 debugstr_w(ctx
->yanked
));
90 /* ====================================================================
92 * Console helper functions
94 * ====================================================================*/
96 static BOOL
WCEL_Get(WCEL_Context
* ctx
, INPUT_RECORD
* ir
)
98 if (ReadConsoleInputW(ctx
->hConIn
, ir
, 1, NULL
)) return TRUE
;
99 ERR("hmm bad situation\n");
104 static inline void WCEL_Beep(WCEL_Context
* ctx
)
109 static inline BOOL
WCEL_IsSingleLine(WCEL_Context
* ctx
, size_t len
)
111 return ctx
->csbi
.dwCursorPosition
.X
+ ctx
->len
+ len
<= ctx
->csbi
.dwSize
.X
;
114 static inline int WCEL_CharWidth(WCHAR wch
)
116 return wch
< ' ' ? 2 : 1;
119 static inline int WCEL_StringWidth(const WCHAR
* str
, int beg
, int len
)
123 for (i
= 0, ofs
= 0; i
< len
; i
++)
124 ofs
+= WCEL_CharWidth(str
[beg
+ i
]);
128 static inline COORD
WCEL_GetCoord(WCEL_Context
* ctx
, int strofs
)
131 int len
= ctx
->csbi
.dwSize
.X
- ctx
->csbi
.dwCursorPosition
.X
;
134 ofs
= WCEL_StringWidth(ctx
->line
, 0, strofs
);
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 DWORD
WCEL_WriteConsole(WCEL_Context
* ctx
, DWORD beg
, DWORD len
)
149 DWORD i
, last
, dw
, ret
= 0;
152 for (i
= last
= 0; i
< len
; i
++)
154 if (ctx
->line
[beg
+ i
] < ' ')
158 WriteConsoleW(ctx
->hConOut
, &ctx
->line
[beg
+ last
], i
- last
, &dw
, NULL
);
162 tmp
[1] = '@' + ctx
->line
[beg
+ i
];
163 WriteConsoleW(ctx
->hConOut
, tmp
, 2, &dw
, NULL
);
170 WriteConsoleW(ctx
->hConOut
, &ctx
->line
[beg
+ last
], len
- last
, &dw
, NULL
);
176 static inline void WCEL_WriteNChars(WCEL_Context
* ctx
, char ch
, int count
)
182 while (count
--) WriteFile(ctx
->hConOut
, &ch
, 1, &dw
, NULL
);
186 static inline void WCEL_Update(WCEL_Context
* ctx
, int beg
, int len
)
192 /* bare console case is handled in CONSOLE_ReadLine (we always reprint the whole string) */
193 if (!ctx
->shall_echo
|| !ctx
->can_pos_cursor
) return;
195 for (i
= last
= beg
; i
< beg
+ len
; i
++)
197 if (ctx
->line
[i
] < ' ')
201 WriteConsoleOutputCharacterW(ctx
->hConOut
, &ctx
->line
[last
], i
- last
,
202 WCEL_GetCoord(ctx
, last
), &count
);
203 FillConsoleOutputAttribute(ctx
->hConOut
, ctx
->csbi
.wAttributes
, i
- last
,
204 WCEL_GetCoord(ctx
, last
), &count
);
207 tmp
[1] = '@' + ctx
->line
[i
];
208 WriteConsoleOutputCharacterW(ctx
->hConOut
, tmp
, 2,
209 WCEL_GetCoord(ctx
, i
), &count
);
210 FillConsoleOutputAttribute(ctx
->hConOut
, ctx
->csbi
.wAttributes
, 2,
211 WCEL_GetCoord(ctx
, i
), &count
);
215 if (last
!= beg
+ len
)
217 WriteConsoleOutputCharacterW(ctx
->hConOut
, &ctx
->line
[last
], i
- last
,
218 WCEL_GetCoord(ctx
, last
), &count
);
219 FillConsoleOutputAttribute(ctx
->hConOut
, ctx
->csbi
.wAttributes
, i
- last
,
220 WCEL_GetCoord(ctx
, last
), &count
);
224 /* ====================================================================
226 * context manipulation functions
228 * ====================================================================*/
230 static BOOL
WCEL_Grow(WCEL_Context
* ctx
, size_t len
)
232 if (!WCEL_IsSingleLine(ctx
, len
) && !ctx
->can_wrap
)
234 FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n");
238 if (ctx
->len
+ len
>= ctx
->alloc
)
243 /* round up size to 32 byte-WCHAR boundary */
244 newsize
= (ctx
->len
+ len
+ 1 + 31) & ~31;
247 newline
= HeapReAlloc(GetProcessHeap(), 0, ctx
->line
, sizeof(WCHAR
) * newsize
);
249 newline
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * newsize
);
251 if (!newline
) return FALSE
;
253 ctx
->alloc
= newsize
;
258 static void WCEL_DeleteString(WCEL_Context
* ctx
, int beg
, int end
)
260 unsigned str_len
= end
- beg
;
263 memmove(&ctx
->line
[beg
], &ctx
->line
[end
], (ctx
->len
- end
) * sizeof(WCHAR
));
264 /* we need to clean from ctx->len - str_len to ctx->len */
268 COORD cbeg
= WCEL_GetCoord(ctx
, ctx
->len
- str_len
);
269 COORD cend
= WCEL_GetCoord(ctx
, ctx
->len
);
272 ci
.Char
.UnicodeChar
= ' ';
273 ci
.Attributes
= ctx
->csbi
.wAttributes
;
275 if (cbeg
.Y
== cend
.Y
)
277 /* partial erase of sole line */
278 CONSOLE_FillLineUniform(ctx
->hConOut
, cbeg
.X
, cbeg
.Y
,
279 cend
.X
- cbeg
.X
, &ci
);
284 /* erase til eol on first line */
285 CONSOLE_FillLineUniform(ctx
->hConOut
, cbeg
.X
, cbeg
.Y
,
286 ctx
->csbi
.dwSize
.X
- cbeg
.X
, &ci
);
287 /* completely erase all the others (full lines) */
288 for (i
= cbeg
.Y
+ 1; i
< cend
.Y
; i
++)
289 CONSOLE_FillLineUniform(ctx
->hConOut
, 0, i
, ctx
->csbi
.dwSize
.X
, &ci
);
290 /* erase from beginning of line until last pos on last line */
291 CONSOLE_FillLineUniform(ctx
->hConOut
, 0, cend
.Y
, cend
.X
, &ci
);
295 WCEL_Update(ctx
, 0, ctx
->len
);
296 ctx
->line
[ctx
->len
] = 0;
299 static void WCEL_InsertString(WCEL_Context
* ctx
, const WCHAR
* str
)
301 size_t len
= lstrlenW(str
);
303 if (!len
|| !WCEL_Grow(ctx
, len
)) return;
304 if (ctx
->len
> ctx
->ofs
)
305 memmove(&ctx
->line
[ctx
->ofs
+ len
], &ctx
->line
[ctx
->ofs
], (ctx
->len
- ctx
->ofs
) * sizeof(WCHAR
));
306 memcpy(&ctx
->line
[ctx
->ofs
], str
, len
* sizeof(WCHAR
));
308 ctx
->line
[ctx
->len
] = 0;
309 WCEL_Update(ctx
, ctx
->ofs
, ctx
->len
- ctx
->ofs
);
314 static void WCEL_InsertChar(WCEL_Context
* ctx
, WCHAR c
)
320 WCEL_InsertString(ctx
, buffer
);
323 static void WCEL_FreeYank(WCEL_Context
* ctx
)
325 HeapFree(GetProcessHeap(), 0, ctx
->yanked
);
329 static void WCEL_SaveYank(WCEL_Context
* ctx
, int beg
, int end
)
332 if (len
<= 0) return;
335 /* After WCEL_FreeYank ctx->yanked is empty */
336 ctx
->yanked
= HeapAlloc(GetProcessHeap(), 0, (len
+ 1) * sizeof(WCHAR
));
337 if (!ctx
->yanked
) return;
338 memcpy(ctx
->yanked
, &ctx
->line
[beg
], len
* sizeof(WCHAR
));
339 ctx
->yanked
[len
] = 0;
342 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
343 * of the data lay in unicode lib
345 static inline BOOL
WCEL_iswalnum(WCHAR wc
)
347 return get_char_typeW(wc
) & (C1_ALPHA
|C1_DIGIT
|C1_LOWER
|C1_UPPER
);
350 static int WCEL_GetLeftWordTransition(WCEL_Context
* ctx
, int ofs
)
353 while (ofs
>= 0 && !WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
354 while (ofs
>= 0 && WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
359 static int WCEL_GetRightWordTransition(WCEL_Context
* ctx
, int ofs
)
362 while (ofs
<= ctx
->len
&& WCEL_iswalnum(ctx
->line
[ofs
])) ofs
++;
363 while (ofs
<= ctx
->len
&& !WCEL_iswalnum(ctx
->line
[ofs
])) ofs
++;
364 return min(ofs
, ctx
->len
);
367 static WCHAR
* WCEL_GetHistory(WCEL_Context
* ctx
, int idx
)
371 if (idx
== ctx
->histSize
- 1)
373 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx
->histCurr
) + 1) * sizeof(WCHAR
));
374 lstrcpyW(ptr
, ctx
->histCurr
);
378 int len
= CONSOLE_GetHistory(idx
, NULL
, 0);
380 if ((ptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
382 CONSOLE_GetHistory(idx
, ptr
, len
);
388 static void WCEL_HistoryInit(WCEL_Context
* ctx
)
390 ctx
->histPos
= CONSOLE_GetNumHistoryEntries();
391 ctx
->histSize
= ctx
->histPos
+ 1;
392 ctx
->histCurr
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(WCHAR
));
395 static void WCEL_MoveToHist(WCEL_Context
* ctx
, int idx
)
397 WCHAR
* data
= WCEL_GetHistory(ctx
, idx
);
398 int len
= lstrlenW(data
) + 1;
400 /* save current line edition for recall when needed (FIXME seems broken to me) */
401 if (ctx
->histPos
== ctx
->histSize
- 1)
403 HeapFree(GetProcessHeap(), 0, ctx
->histCurr
);
404 ctx
->histCurr
= HeapAlloc(GetProcessHeap(), 0, (ctx
->len
+ 1) * sizeof(WCHAR
));
405 memcpy(ctx
->histCurr
, ctx
->line
, (ctx
->len
+ 1) * sizeof(WCHAR
));
407 /* need to clean also the screen if new string is shorter than old one */
408 WCEL_DeleteString(ctx
, 0, ctx
->len
);
410 /* insert new string */
411 if (WCEL_Grow(ctx
, len
))
413 WCEL_InsertString(ctx
, data
);
414 HeapFree(GetProcessHeap(), 0, data
);
419 static void WCEL_FindPrevInHist(WCEL_Context
* ctx
)
421 int startPos
= ctx
->histPos
;
423 unsigned int len
, oldofs
;
425 if (ctx
->histPos
&& ctx
->histPos
== ctx
->histSize
) {
431 data
= WCEL_GetHistory(ctx
, ctx
->histPos
);
433 if (ctx
->histPos
) ctx
->histPos
--;
434 else ctx
->histPos
= (ctx
->histSize
-1);
436 len
= lstrlenW(data
) + 1;
437 if ((len
>= ctx
->ofs
) &&
438 (memcmp(ctx
->line
, data
, ctx
->ofs
* sizeof(WCHAR
)) == 0)) {
440 /* need to clean also the screen if new string is shorter than old one */
441 WCEL_DeleteString(ctx
, 0, ctx
->len
);
443 if (WCEL_Grow(ctx
, len
))
447 WCEL_InsertString(ctx
, data
);
450 SetConsoleCursorPosition(ctx
->hConOut
, WCEL_GetCoord(ctx
, ctx
->ofs
));
451 HeapFree(GetProcessHeap(), 0, data
);
455 } while (ctx
->histPos
!= startPos
);
460 /* ====================================================================
462 * basic edition functions
464 * ====================================================================*/
466 static void WCEL_Done(WCEL_Context
* ctx
)
469 if (!WCEL_Grow(ctx
, 2)) return;
470 ctx
->line
[ctx
->len
++] = '\r';
471 ctx
->line
[ctx
->len
++] = '\n';
472 ctx
->line
[ctx
->len
] = 0;
473 WriteConsoleW(ctx
->hConOut
, &nl
, 1, NULL
, NULL
);
477 static void WCEL_MoveLeft(WCEL_Context
* ctx
)
479 if (ctx
->ofs
> 0) ctx
->ofs
--;
482 static void WCEL_MoveRight(WCEL_Context
* ctx
)
484 if (ctx
->ofs
< ctx
->len
) ctx
->ofs
++;
487 static void WCEL_MoveToLeftWord(WCEL_Context
* ctx
)
489 unsigned int new_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
);
490 if (new_ofs
!= ctx
->ofs
) ctx
->ofs
= new_ofs
;
493 static void WCEL_MoveToRightWord(WCEL_Context
* ctx
)
495 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
496 if (new_ofs
!= ctx
->ofs
) ctx
->ofs
= new_ofs
;
499 static void WCEL_MoveToBeg(WCEL_Context
* ctx
)
504 static void WCEL_MoveToEnd(WCEL_Context
* ctx
)
509 static void WCEL_SetMark(WCEL_Context
* ctx
)
511 ctx
->mark
= ctx
->ofs
;
514 static void WCEL_ExchangeMark(WCEL_Context
* ctx
)
518 if (ctx
->mark
> ctx
->len
) return;
520 ctx
->ofs
= ctx
->mark
;
524 static void WCEL_CopyMarkedZone(WCEL_Context
* ctx
)
528 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
529 if (ctx
->mark
> ctx
->ofs
)
531 beg
= ctx
->ofs
; end
= ctx
->mark
;
535 beg
= ctx
->mark
; end
= ctx
->ofs
;
537 WCEL_SaveYank(ctx
, beg
, end
);
540 static void WCEL_TransposeChar(WCEL_Context
* ctx
)
544 if (!ctx
->ofs
|| ctx
->ofs
== ctx
->len
) return;
546 c
= ctx
->line
[ctx
->ofs
];
547 ctx
->line
[ctx
->ofs
] = ctx
->line
[ctx
->ofs
- 1];
548 ctx
->line
[ctx
->ofs
- 1] = c
;
550 WCEL_Update(ctx
, ctx
->ofs
- 1, 2);
554 static void WCEL_TransposeWords(WCEL_Context
* ctx
)
556 unsigned int left_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
),
557 right_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
558 if (left_ofs
< ctx
->ofs
&& right_ofs
> ctx
->ofs
)
560 unsigned len_r
= right_ofs
- ctx
->ofs
;
561 unsigned len_l
= ctx
->ofs
- left_ofs
;
563 char* tmp
= HeapAlloc(GetProcessHeap(), 0, len_r
* sizeof(WCHAR
));
566 memcpy(tmp
, &ctx
->line
[ctx
->ofs
], len_r
* sizeof(WCHAR
));
567 memmove(&ctx
->line
[left_ofs
+ len_r
], &ctx
->line
[left_ofs
], len_l
* sizeof(WCHAR
));
568 memcpy(&ctx
->line
[left_ofs
], tmp
, len_r
* sizeof(WCHAR
));
570 HeapFree(GetProcessHeap(), 0, tmp
);
571 WCEL_Update(ctx
, left_ofs
, len_l
+ len_r
);
572 ctx
->ofs
= right_ofs
;
576 static void WCEL_LowerCaseWord(WCEL_Context
* ctx
)
578 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
579 if (new_ofs
!= ctx
->ofs
)
582 for (i
= ctx
->ofs
; i
<= new_ofs
; i
++)
583 ctx
->line
[i
] = tolowerW(ctx
->line
[i
]);
584 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
589 static void WCEL_UpperCaseWord(WCEL_Context
* ctx
)
591 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
592 if (new_ofs
!= ctx
->ofs
)
595 for (i
= ctx
->ofs
; i
<= new_ofs
; i
++)
596 ctx
->line
[i
] = toupperW(ctx
->line
[i
]);
597 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
602 static void WCEL_CapitalizeWord(WCEL_Context
* ctx
)
604 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
605 if (new_ofs
!= ctx
->ofs
)
609 ctx
->line
[ctx
->ofs
] = toupperW(ctx
->line
[ctx
->ofs
]);
610 for (i
= ctx
->ofs
+ 1; i
<= new_ofs
; i
++)
611 ctx
->line
[i
] = tolowerW(ctx
->line
[i
]);
612 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
617 static void WCEL_Yank(WCEL_Context
* ctx
)
619 WCEL_InsertString(ctx
, ctx
->yanked
);
622 static void WCEL_KillToEndOfLine(WCEL_Context
* ctx
)
624 WCEL_SaveYank(ctx
, ctx
->ofs
, ctx
->len
);
625 WCEL_DeleteString(ctx
, ctx
->ofs
, ctx
->len
);
628 static void WCEL_KillMarkedZone(WCEL_Context
* ctx
)
632 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
633 if (ctx
->mark
> ctx
->ofs
)
635 beg
= ctx
->ofs
; end
= ctx
->mark
;
639 beg
= ctx
->mark
; end
= ctx
->ofs
;
641 WCEL_SaveYank(ctx
, beg
, end
);
642 WCEL_DeleteString(ctx
, beg
, end
);
646 static void WCEL_DeletePrevChar(WCEL_Context
* ctx
)
650 WCEL_DeleteString(ctx
, ctx
->ofs
- 1, ctx
->ofs
);
655 static void WCEL_DeleteCurrChar(WCEL_Context
* ctx
)
657 if (ctx
->ofs
< ctx
->len
)
658 WCEL_DeleteString(ctx
, ctx
->ofs
, ctx
->ofs
+ 1);
661 static void WCEL_DeleteLeftWord(WCEL_Context
* ctx
)
663 unsigned int new_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
);
664 if (new_ofs
!= ctx
->ofs
)
666 WCEL_DeleteString(ctx
, new_ofs
, ctx
->ofs
);
671 static void WCEL_DeleteRightWord(WCEL_Context
* ctx
)
673 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
674 if (new_ofs
!= ctx
->ofs
)
676 WCEL_DeleteString(ctx
, ctx
->ofs
, new_ofs
);
680 static void WCEL_MoveToPrevHist(WCEL_Context
* ctx
)
682 if (ctx
->histPos
) WCEL_MoveToHist(ctx
, ctx
->histPos
- 1);
685 static void WCEL_MoveToNextHist(WCEL_Context
* ctx
)
687 if (ctx
->histPos
< ctx
->histSize
- 1) WCEL_MoveToHist(ctx
, ctx
->histPos
+ 1);
690 static void WCEL_MoveToFirstHist(WCEL_Context
* ctx
)
692 if (ctx
->histPos
!= 0) WCEL_MoveToHist(ctx
, 0);
695 static void WCEL_MoveToLastHist(WCEL_Context
* ctx
)
697 if (ctx
->histPos
!= ctx
->histSize
- 1) WCEL_MoveToHist(ctx
, ctx
->histSize
- 1);
700 static void WCEL_Redraw(WCEL_Context
* ctx
)
704 COORD c
= WCEL_GetCoord(ctx
, ctx
->len
);
707 WCEL_Update(ctx
, 0, ctx
->len
);
709 ci
.Char
.UnicodeChar
= ' ';
710 ci
.Attributes
= ctx
->csbi
.wAttributes
;
712 CONSOLE_FillLineUniform(ctx
->hConOut
, c
.X
, c
.Y
, ctx
->csbi
.dwSize
.X
- c
.X
, &ci
);
716 static void WCEL_RepeatCount(WCEL_Context
* ctx
)
719 /* FIXME: wait until all console code is in kernel32 */
723 while (WCEL_Get(ctx
, &ir
, FALSE
))
725 if (ir
.EventType
!= KEY_EVENT
) break;
726 if (ir
.Event
.KeyEvent
.bKeyDown
)
728 if ((ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
)) != 0)
730 if (ir
.Event
.KeyEvent
.uChar
.UnicodeChar
< '0' ||
731 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
> '9')
733 repeat
= repeat
* 10 + ir
.Event
.KeyEvent
.uChar
.UnicodeChar
- '0';
735 WCEL_Get(ctx
, &ir
, TRUE
);
737 FIXME("=> %u\n", repeat
);
741 /* ====================================================================
745 * ====================================================================*/
747 #define CTRL(x) ((x) - '@')
748 static const KeyEntry StdKeyMap
[] =
750 {/*BACK*/0x08, WCEL_DeletePrevChar
},
751 {/*RETURN*/0x0d, WCEL_Done
},
752 {/*DEL*/127, WCEL_DeleteCurrChar
},
756 static const KeyEntry EmacsKeyMapCtrl
[] =
758 { CTRL('@'), WCEL_SetMark
},
759 { CTRL('A'), WCEL_MoveToBeg
},
760 { CTRL('B'), WCEL_MoveLeft
},
761 /* C: done in server */
762 { CTRL('D'), WCEL_DeleteCurrChar
},
763 { CTRL('E'), WCEL_MoveToEnd
},
764 { CTRL('F'), WCEL_MoveRight
},
765 { CTRL('G'), WCEL_Beep
},
766 { CTRL('H'), WCEL_DeletePrevChar
},
767 /* I: meaningless (or tab ???) */
768 { CTRL('J'), WCEL_Done
},
769 { CTRL('K'), WCEL_KillToEndOfLine
},
770 { CTRL('L'), WCEL_Redraw
},
771 { CTRL('M'), WCEL_Done
},
772 { CTRL('N'), WCEL_MoveToNextHist
},
773 /* O; insert line... meaningless */
774 { CTRL('P'), WCEL_MoveToPrevHist
},
775 /* Q: [NIY] quoting... */
776 /* R: [NIY] search backwards... */
777 /* S: [NIY] search forwards... */
778 { CTRL('T'), WCEL_TransposeChar
},
779 { CTRL('U'), WCEL_RepeatCount
},
780 /* V: paragraph down... meaningless */
781 { CTRL('W'), WCEL_KillMarkedZone
},
782 { CTRL('X'), WCEL_ExchangeMark
},
783 { CTRL('Y'), WCEL_Yank
},
788 static const KeyEntry EmacsKeyMapAlt
[] =
790 {/*DEL*/127, WCEL_DeleteLeftWord
},
791 { '<', WCEL_MoveToFirstHist
},
792 { '>', WCEL_MoveToLastHist
},
794 { 'b', WCEL_MoveToLeftWord
},
795 { 'c', WCEL_CapitalizeWord
},
796 { 'd', WCEL_DeleteRightWord
},
797 { 'f', WCEL_MoveToRightWord
},
798 { 'l', WCEL_LowerCaseWord
},
799 { 't', WCEL_TransposeWords
},
800 { 'u', WCEL_UpperCaseWord
},
801 { 'w', WCEL_CopyMarkedZone
},
805 static const KeyEntry EmacsStdKeyMap
[] =
807 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist
},
808 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist
},
809 {/*VK_END*/ 0x23, WCEL_MoveToEnd
},
810 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg
},
811 {/*VK_RIGHT*/0x27, WCEL_MoveRight
},
812 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft
},
813 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar
},
817 static const KeyMap EmacsKeyMap
[] =
820 {0, 0, EmacsStdKeyMap
},
821 {RIGHT_ALT_PRESSED
, 1, EmacsKeyMapAlt
}, /* right alt */
822 {LEFT_ALT_PRESSED
, 1, EmacsKeyMapAlt
}, /* left alt */
823 {RIGHT_CTRL_PRESSED
, 1, EmacsKeyMapCtrl
}, /* right ctrl */
824 {LEFT_CTRL_PRESSED
, 1, EmacsKeyMapCtrl
}, /* left ctrl */
828 static const KeyEntry Win32StdKeyMap
[] =
830 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft
},
831 {/*VK_RIGHT*/0x27, WCEL_MoveRight
},
832 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg
},
833 {/*VK_END*/ 0x23, WCEL_MoveToEnd
},
834 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist
},
835 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist
},
836 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar
},
837 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist
},
841 static const KeyEntry Win32KeyMapCtrl
[] =
843 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord
},
844 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord
},
845 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine
},
849 static const KeyMap Win32KeyMap
[] =
852 {0, 0, Win32StdKeyMap
},
853 {RIGHT_CTRL_PRESSED
, 0, Win32KeyMapCtrl
},
854 {LEFT_CTRL_PRESSED
, 0, Win32KeyMapCtrl
},
859 /* ====================================================================
861 * Read line master function
863 * ====================================================================*/
865 WCHAR
* CONSOLE_Readline(HANDLE hConsoleIn
, BOOL can_pos_cursor
)
872 void (*func
)(struct WCEL_Context
* ctx
);
876 memset(&ctx
, 0, sizeof(ctx
));
877 ctx
.hConIn
= hConsoleIn
;
878 WCEL_HistoryInit(&ctx
);
880 if (!CONSOLE_GetEditionMode(hConsoleIn
, &use_emacs
))
883 if ((ctx
.hConOut
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
884 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
||
885 !GetConsoleScreenBufferInfo(ctx
.hConOut
, &ctx
.csbi
))
887 ctx
.shall_echo
= (GetConsoleMode(hConsoleIn
, &ks
) && (ks
& ENABLE_ECHO_INPUT
)) ? 1 : 0;
888 ctx
.can_wrap
= (GetConsoleMode(ctx
.hConOut
, &ks
) && (ks
& ENABLE_WRAP_AT_EOL_OUTPUT
)) ? 1 : 0;
889 ctx
.can_pos_cursor
= can_pos_cursor
;
891 if (!WCEL_Grow(&ctx
, 1))
893 CloseHandle(ctx
.hConOut
);
898 /* EPP WCEL_Dump(&ctx, "init"); */
900 while (!ctx
.done
&& !ctx
.error
&& WCEL_Get(&ctx
, &ir
))
902 if (ir
.EventType
!= KEY_EVENT
) continue;
903 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
904 ir
.Event
.KeyEvent
.bKeyDown
? "Down" : "Up ", ir
.Event
.KeyEvent
.wRepeatCount
,
905 ir
.Event
.KeyEvent
.wVirtualKeyCode
, ir
.Event
.KeyEvent
.wVirtualScanCode
,
906 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
, ir
.Event
.KeyEvent
.dwControlKeyState
);
907 if (!ir
.Event
.KeyEvent
.bKeyDown
) continue;
909 /* EPP WCEL_Dump(&ctx, "before func"); */
911 /* mask out some bits which don't interest us */
912 ks
= ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
|ENHANCED_KEY
);
915 for (km
= (use_emacs
) ? EmacsKeyMap
: Win32KeyMap
; km
->entries
!= NULL
; km
++)
917 if (km
->keyState
!= ks
)
921 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
922 if (ke
->val
== ir
.Event
.KeyEvent
.uChar
.UnicodeChar
) break;
926 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
927 if (ke
->val
== ir
.Event
.KeyEvent
.wVirtualKeyCode
) break;
939 else if (!(ir
.Event
.KeyEvent
.dwControlKeyState
& LEFT_ALT_PRESSED
))
940 WCEL_InsertChar(&ctx
, ir
.Event
.KeyEvent
.uChar
.UnicodeChar
);
941 else TRACE("Dropped event\n");
943 /* EPP WCEL_Dump(&ctx, "after func"); */
944 if (!ctx
.shall_echo
) continue;
945 if (ctx
.can_pos_cursor
)
948 SetConsoleCursorPosition(ctx
.hConOut
, WCEL_GetCoord(&ctx
, ctx
.ofs
));
950 else if (!ctx
.done
&& !ctx
.error
)
953 /* erase previous chars */
954 WCEL_WriteNChars(&ctx
, '\b', ctx
.last_rub
);
956 /* write chars up to cursor */
957 ctx
.last_rub
= WCEL_WriteConsole(&ctx
, 0, ctx
.ofs
);
958 /* write chars past cursor */
959 last
= ctx
.last_rub
+ WCEL_WriteConsole(&ctx
, ctx
.ofs
, ctx
.len
- ctx
.ofs
);
960 if (last
< ctx
.last_max
) /* ctx.line has been shortened, erase */
962 WCEL_WriteNChars(&ctx
, ' ', ctx
.last_max
- last
);
963 WCEL_WriteNChars(&ctx
, '\b', ctx
.last_max
- last
);
966 else ctx
.last_max
= last
;
967 /* reposition at cursor */
968 WCEL_WriteNChars(&ctx
, '\b', last
- ctx
.last_rub
);
973 HeapFree(GetProcessHeap(), 0, ctx
.line
);
978 CONSOLE_AppendHistory(ctx
.line
);
980 CloseHandle(ctx
.hConOut
);
981 HeapFree(GetProcessHeap(), 0, ctx
.histCurr
);