dwrite/tests: Some tests for TranslateColorGlyphRun().
[wine/multimedia.git] / dlls / kernel32 / editline.c
blobb3308698b2e0a2acaf1c2a96893ad4059e504fcf
1 /*
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
21 #include "config.h"
22 #include "wine/port.h"
24 #include <stdarg.h>
25 #include <string.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "wincon.h"
30 #include "wine/unicode.h"
31 #include "winnls.h"
32 #include "wine/debug.h"
33 #include "console_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(console);
37 struct WCEL_Context;
39 typedef struct
41 WCHAR val; /* vk or unicode char */
42 void (*func)(struct WCEL_Context* ctx);
43 } KeyEntry;
45 typedef struct
47 DWORD keyState; /* keyState (from INPUT_RECORD) to match */
48 BOOL chkChar; /* check vk or char */
49 const KeyEntry* entries; /* array of entries */
50 } KeyMap;
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) */
64 CONSOLE_CURSOR_INFO cinfo; /* original cursor state (size, visibility) */
65 HANDLE hConIn;
66 HANDLE hConOut;
67 unsigned done : 1, /* to 1 when we're done with editing */
68 error : 1, /* to 1 when an error occurred in the editing */
69 can_wrap : 1, /* to 1 when multi-line edition can take place */
70 shall_echo : 1, /* to 1 when characters should be echo:ed when keyed-in */
71 insert : 1, /* to 1 when new characters are inserted (otherwise overwrite) */
72 insertkey : 1, /* to 1 when the Insert key toggle is active */
73 can_pos_cursor : 1; /* to 1 when console can (re)position cursor */
74 unsigned histSize;
75 unsigned histPos;
76 WCHAR* histCurr;
77 } WCEL_Context;
79 #if 0
80 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
82 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
83 "\t\thist=(size=%u pos=%u curr=%s)\n"
84 "\t\tyanked=%s\n",
85 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
86 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
87 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
88 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
89 debugstr_w(ctx->yanked));
91 #endif
93 /* ====================================================================
95 * Console helper functions
97 * ====================================================================*/
99 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
101 if (ReadConsoleInputW(ctx->hConIn, ir, 1, NULL)) return TRUE;
102 ctx->error = 1;
103 return FALSE;
106 static inline void WCEL_Beep(WCEL_Context* ctx)
108 Beep(400, 300);
111 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
113 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
116 static inline int WCEL_CharWidth(WCHAR wch)
118 return wch < ' ' ? 2 : 1;
121 static inline int WCEL_StringWidth(const WCHAR* str, int beg, int len)
123 int i, ofs;
125 for (i = 0, ofs = 0; i < len; i++)
126 ofs += WCEL_CharWidth(str[beg + i]);
127 return ofs;
130 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int strofs)
132 COORD c;
133 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
134 int ofs;
136 ofs = WCEL_StringWidth(ctx->line, 0, strofs);
138 c.Y = ctx->csbi.dwCursorPosition.Y;
139 if (ofs >= len)
141 ofs -= len;
142 c.X = ofs % ctx->csbi.dwSize.X;
143 c.Y += 1 + ofs / ctx->csbi.dwSize.X;
145 else c.X = ctx->csbi.dwCursorPosition.X + ofs;
146 return c;
149 static DWORD WCEL_WriteConsole(WCEL_Context* ctx, DWORD beg, DWORD len)
151 DWORD i, last, dw, ret = 0;
152 WCHAR tmp[2];
154 for (i = last = 0; i < len; i++)
156 if (ctx->line[beg + i] < ' ')
158 if (last != i)
160 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], i - last, &dw, NULL);
161 ret += dw;
163 tmp[0] = '^';
164 tmp[1] = '@' + ctx->line[beg + i];
165 WriteConsoleW(ctx->hConOut, tmp, 2, &dw, NULL);
166 last = i + 1;
167 ret += dw;
170 if (last != len)
172 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], len - last, &dw, NULL);
173 ret += dw;
175 return ret;
178 static inline void WCEL_WriteNChars(WCEL_Context* ctx, char ch, int count)
180 DWORD dw;
182 if (count > 0)
184 while (count--) WriteFile(ctx->hConOut, &ch, 1, &dw, NULL);
188 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
190 int i, last;
191 DWORD count;
192 WCHAR tmp[2];
194 /* bare console case is handled in CONSOLE_ReadLine (we always reprint the whole string) */
195 if (!ctx->shall_echo || !ctx->can_pos_cursor) return;
197 for (i = last = beg; i < beg + len; i++)
199 if (ctx->line[i] < ' ')
201 if (last != i)
203 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
204 WCEL_GetCoord(ctx, last), &count);
205 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
206 WCEL_GetCoord(ctx, last), &count);
208 tmp[0] = '^';
209 tmp[1] = '@' + ctx->line[i];
210 WriteConsoleOutputCharacterW(ctx->hConOut, tmp, 2,
211 WCEL_GetCoord(ctx, i), &count);
212 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, 2,
213 WCEL_GetCoord(ctx, i), &count);
214 last = i + 1;
217 if (last != beg + len)
219 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
220 WCEL_GetCoord(ctx, last), &count);
221 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
222 WCEL_GetCoord(ctx, last), &count);
226 /* ====================================================================
228 * context manipulation functions
230 * ====================================================================*/
232 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
234 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
236 FIXME("Mode doesn't allow wrapping. However, we should allow overwriting the current string\n");
237 return FALSE;
240 if (ctx->len + len >= ctx->alloc)
242 WCHAR* newline;
243 size_t newsize;
245 /* round up size to 32 byte-WCHAR boundary */
246 newsize = (ctx->len + len + 1 + 31) & ~31;
248 if (ctx->line)
249 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
250 else
251 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
253 if (!newline) return FALSE;
254 ctx->line = newline;
255 ctx->alloc = newsize;
257 return TRUE;
260 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
262 unsigned str_len = end - beg;
264 if (end < ctx->len)
265 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
266 /* we need to clean from ctx->len - str_len to ctx->len */
268 if (ctx->shall_echo)
270 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
271 COORD cend = WCEL_GetCoord(ctx, ctx->len);
272 CHAR_INFO ci;
274 ci.Char.UnicodeChar = ' ';
275 ci.Attributes = ctx->csbi.wAttributes;
277 if (cbeg.Y == cend.Y)
279 /* partial erase of sole line */
280 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
281 cend.X - cbeg.X, &ci);
283 else
285 int i;
286 /* erase til eol on first line */
287 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
288 ctx->csbi.dwSize.X - cbeg.X, &ci);
289 /* completely erase all the others (full lines) */
290 for (i = cbeg.Y + 1; i < cend.Y; i++)
291 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
292 /* erase from beginning of line until last pos on last line */
293 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
296 ctx->len -= str_len;
297 WCEL_Update(ctx, 0, ctx->len);
298 ctx->line[ctx->len] = 0;
301 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
303 size_t len = lstrlenW(str), updtlen;
305 if (!len) return;
306 if (ctx->insert)
308 if (!WCEL_Grow(ctx, len)) return;
309 if (ctx->len > ctx->ofs)
310 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
311 ctx->len += len;
312 updtlen = ctx->len - ctx->ofs;
314 else
316 if (ctx->ofs + len > ctx->len)
318 if (!WCEL_Grow(ctx, (ctx->ofs + len) - ctx->len)) return;
319 ctx->len = ctx->ofs + len;
321 updtlen = len;
323 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
324 ctx->line[ctx->len] = 0;
325 WCEL_Update(ctx, ctx->ofs, updtlen);
326 ctx->ofs += len;
329 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
331 WCHAR buffer[2];
333 buffer[0] = c;
334 buffer[1] = 0;
335 WCEL_InsertString(ctx, buffer);
338 static void WCEL_FreeYank(WCEL_Context* ctx)
340 HeapFree(GetProcessHeap(), 0, ctx->yanked);
341 ctx->yanked = NULL;
344 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
346 int len = end - beg;
347 if (len <= 0) return;
349 WCEL_FreeYank(ctx);
350 /* After WCEL_FreeYank ctx->yanked is empty */
351 ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
352 if (!ctx->yanked) return;
353 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
354 ctx->yanked[len] = 0;
357 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
358 * of the data lay in unicode lib
360 static inline BOOL WCEL_iswalnum(WCHAR wc)
362 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
365 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
367 ofs--;
368 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
369 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
370 if (ofs >= 0) ofs++;
371 return max(ofs, 0);
374 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
376 ofs++;
377 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
378 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
379 return min(ofs, ctx->len);
382 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
384 WCHAR* ptr;
386 if (idx == ctx->histSize - 1)
388 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
389 lstrcpyW(ptr, ctx->histCurr);
391 else
393 int len = CONSOLE_GetHistory(idx, NULL, 0);
395 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
397 CONSOLE_GetHistory(idx, ptr, len);
400 return ptr;
403 static void WCEL_HistoryInit(WCEL_Context* ctx)
405 ctx->histPos = CONSOLE_GetNumHistoryEntries();
406 ctx->histSize = ctx->histPos + 1;
407 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
410 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
412 WCHAR* data = WCEL_GetHistory(ctx, idx);
413 int len = lstrlenW(data) + 1;
415 /* save current line edition for recall when needed (FIXME seems broken to me) */
416 if (ctx->histPos == ctx->histSize - 1)
418 HeapFree(GetProcessHeap(), 0, ctx->histCurr);
419 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
420 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
422 /* need to clean also the screen if new string is shorter than old one */
423 WCEL_DeleteString(ctx, 0, ctx->len);
424 ctx->ofs = 0;
425 /* insert new string */
426 if (WCEL_Grow(ctx, len))
428 WCEL_InsertString(ctx, data);
429 HeapFree(GetProcessHeap(), 0, data);
430 ctx->histPos = idx;
434 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
436 int startPos = ctx->histPos;
437 WCHAR* data;
438 unsigned int len, oldofs;
440 if (ctx->histPos && ctx->histPos == ctx->histSize) {
441 startPos--;
442 ctx->histPos--;
445 do {
446 data = WCEL_GetHistory(ctx, ctx->histPos);
448 if (ctx->histPos) ctx->histPos--;
449 else ctx->histPos = (ctx->histSize-1);
451 len = lstrlenW(data) + 1;
452 if ((len >= ctx->ofs) &&
453 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
455 /* need to clean also the screen if new string is shorter than old one */
456 WCEL_DeleteString(ctx, 0, ctx->len);
458 if (WCEL_Grow(ctx, len))
460 oldofs = ctx->ofs;
461 ctx->ofs = 0;
462 WCEL_InsertString(ctx, data);
463 ctx->ofs = oldofs;
464 if (ctx->shall_echo)
465 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
466 HeapFree(GetProcessHeap(), 0, data);
467 return;
470 } while (ctx->histPos != startPos);
472 return;
475 /* ====================================================================
477 * basic edition functions
479 * ====================================================================*/
481 static void WCEL_Done(WCEL_Context* ctx)
483 WCHAR nl = '\n';
484 if (!WCEL_Grow(ctx, 2)) return;
485 ctx->line[ctx->len++] = '\r';
486 ctx->line[ctx->len++] = '\n';
487 ctx->line[ctx->len] = 0;
488 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
489 if (ctx->insertkey)
490 SetConsoleCursorInfo(ctx->hConOut, &ctx->cinfo);
491 ctx->done = 1;
494 static void WCEL_MoveLeft(WCEL_Context* ctx)
496 if (ctx->ofs > 0) ctx->ofs--;
499 static void WCEL_MoveRight(WCEL_Context* ctx)
501 if (ctx->ofs < ctx->len) ctx->ofs++;
504 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
506 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
507 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
510 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
512 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
513 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
516 static void WCEL_MoveToBeg(WCEL_Context* ctx)
518 ctx->ofs = 0;
521 static void WCEL_MoveToEnd(WCEL_Context* ctx)
523 ctx->ofs = ctx->len;
526 static void WCEL_SetMark(WCEL_Context* ctx)
528 ctx->mark = ctx->ofs;
531 static void WCEL_ExchangeMark(WCEL_Context* ctx)
533 unsigned tmp;
535 if (ctx->mark > ctx->len) return;
536 tmp = ctx->ofs;
537 ctx->ofs = ctx->mark;
538 ctx->mark = tmp;
541 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
543 unsigned beg, end;
545 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
546 if (ctx->mark > ctx->ofs)
548 beg = ctx->ofs; end = ctx->mark;
550 else
552 beg = ctx->mark; end = ctx->ofs;
554 WCEL_SaveYank(ctx, beg, end);
557 static void WCEL_TransposeChar(WCEL_Context* ctx)
559 WCHAR c;
561 if (!ctx->ofs || ctx->ofs == ctx->len) return;
563 c = ctx->line[ctx->ofs];
564 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
565 ctx->line[ctx->ofs - 1] = c;
567 WCEL_Update(ctx, ctx->ofs - 1, 2);
568 ctx->ofs++;
571 static void WCEL_TransposeWords(WCEL_Context* ctx)
573 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
574 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
575 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
577 unsigned len_r = right_ofs - ctx->ofs;
578 unsigned len_l = ctx->ofs - left_ofs;
580 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
581 if (!tmp) return;
583 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
584 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
585 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
587 HeapFree(GetProcessHeap(), 0, tmp);
588 WCEL_Update(ctx, left_ofs, len_l + len_r);
589 ctx->ofs = right_ofs;
593 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
595 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
596 if (new_ofs != ctx->ofs)
598 unsigned int i;
599 for (i = ctx->ofs; i <= new_ofs; i++)
600 ctx->line[i] = tolowerW(ctx->line[i]);
601 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
602 ctx->ofs = new_ofs;
606 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
608 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
609 if (new_ofs != ctx->ofs)
611 unsigned int i;
612 for (i = ctx->ofs; i <= new_ofs; i++)
613 ctx->line[i] = toupperW(ctx->line[i]);
614 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
615 ctx->ofs = new_ofs;
619 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
621 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
622 if (new_ofs != ctx->ofs)
624 unsigned int i;
626 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
627 for (i = ctx->ofs + 1; i <= new_ofs; i++)
628 ctx->line[i] = tolowerW(ctx->line[i]);
629 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
630 ctx->ofs = new_ofs;
634 static void WCEL_Yank(WCEL_Context* ctx)
636 WCEL_InsertString(ctx, ctx->yanked);
639 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
641 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
642 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
645 static void WCEL_KillFromBegOfLine(WCEL_Context* ctx)
647 if (ctx->ofs)
649 WCEL_SaveYank(ctx, 0, ctx->ofs);
650 WCEL_DeleteString(ctx, 0, ctx->ofs);
651 ctx->ofs = 0;
655 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
657 unsigned beg, end;
659 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
660 if (ctx->mark > ctx->ofs)
662 beg = ctx->ofs; end = ctx->mark;
664 else
666 beg = ctx->mark; end = ctx->ofs;
668 WCEL_SaveYank(ctx, beg, end);
669 WCEL_DeleteString(ctx, beg, end);
670 ctx->ofs = beg;
673 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
675 if (ctx->ofs)
677 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
678 ctx->ofs--;
682 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
684 if (ctx->ofs < ctx->len)
685 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
688 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
690 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
691 if (new_ofs != ctx->ofs)
693 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
694 ctx->ofs = new_ofs;
698 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
700 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
701 if (new_ofs != ctx->ofs)
703 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
707 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
709 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
712 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
714 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
717 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
719 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
722 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
724 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
727 static void WCEL_Redraw(WCEL_Context* ctx)
729 if (ctx->shall_echo)
731 COORD c = WCEL_GetCoord(ctx, ctx->len);
732 CHAR_INFO ci;
734 WCEL_Update(ctx, 0, ctx->len);
736 ci.Char.UnicodeChar = ' ';
737 ci.Attributes = ctx->csbi.wAttributes;
739 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
743 static void WCEL_RepeatCount(WCEL_Context* ctx)
745 #if 0
746 /* FIXME: wait until all console code is in kernel32 */
747 INPUT_RECORD ir;
748 unsigned repeat = 0;
750 while (WCEL_Get(ctx, &ir, FALSE))
752 if (ir.EventType != KEY_EVENT) break;
753 if (ir.Event.KeyEvent.bKeyDown)
755 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
756 break;
757 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
758 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
759 break;
760 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
762 WCEL_Get(ctx, &ir, TRUE);
764 FIXME("=> %u\n", repeat);
765 #endif
768 static void WCEL_ToggleInsert(WCEL_Context* ctx)
770 CONSOLE_CURSOR_INFO cinfo;
772 ctx->insertkey = !ctx->insertkey;
774 if (GetConsoleCursorInfo(ctx->hConOut, &cinfo))
776 cinfo.dwSize = ctx->insertkey ? 100 : 25;
777 SetConsoleCursorInfo(ctx->hConOut, &cinfo);
781 /* ====================================================================
783 * Key Maps
785 * ====================================================================*/
787 #define CTRL(x) ((x) - '@')
788 static const KeyEntry StdKeyMap[] =
790 {/*BACK*/0x08, WCEL_DeletePrevChar },
791 {/*RETURN*/0x0d, WCEL_Done },
792 {/*DEL*/127, WCEL_DeleteCurrChar },
793 { 0, NULL }
796 static const KeyEntry EmacsKeyMapCtrl[] =
798 { CTRL('@'), WCEL_SetMark },
799 { CTRL('A'), WCEL_MoveToBeg },
800 { CTRL('B'), WCEL_MoveLeft },
801 /* C: done in server */
802 { CTRL('D'), WCEL_DeleteCurrChar },
803 { CTRL('E'), WCEL_MoveToEnd },
804 { CTRL('F'), WCEL_MoveRight },
805 { CTRL('G'), WCEL_Beep },
806 { CTRL('H'), WCEL_DeletePrevChar },
807 /* I: meaningless (or tab ???) */
808 { CTRL('J'), WCEL_Done },
809 { CTRL('K'), WCEL_KillToEndOfLine },
810 { CTRL('L'), WCEL_Redraw },
811 { CTRL('M'), WCEL_Done },
812 { CTRL('N'), WCEL_MoveToNextHist },
813 /* O; insert line... meaningless */
814 { CTRL('P'), WCEL_MoveToPrevHist },
815 /* Q: [NIY] quoting... */
816 /* R: [NIY] search backwards... */
817 /* S: [NIY] search forwards... */
818 { CTRL('T'), WCEL_TransposeChar },
819 { CTRL('U'), WCEL_RepeatCount },
820 /* V: paragraph down... meaningless */
821 { CTRL('W'), WCEL_KillMarkedZone },
822 { CTRL('X'), WCEL_ExchangeMark },
823 { CTRL('Y'), WCEL_Yank },
824 /* Z: meaningless */
825 { 0, NULL }
828 static const KeyEntry EmacsKeyMapAlt[] =
830 {/*DEL*/127, WCEL_DeleteLeftWord },
831 { '<', WCEL_MoveToFirstHist },
832 { '>', WCEL_MoveToLastHist },
833 { '?', WCEL_Beep },
834 { 'b', WCEL_MoveToLeftWord },
835 { 'c', WCEL_CapitalizeWord },
836 { 'd', WCEL_DeleteRightWord },
837 { 'f', WCEL_MoveToRightWord },
838 { 'l', WCEL_LowerCaseWord },
839 { 't', WCEL_TransposeWords },
840 { 'u', WCEL_UpperCaseWord },
841 { 'w', WCEL_CopyMarkedZone },
842 { 0, NULL }
845 static const KeyEntry EmacsStdKeyMap[] =
847 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
848 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
849 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
850 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
851 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
852 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
853 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
854 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert },
855 { 0, NULL }
858 static const KeyMap EmacsKeyMap[] =
860 {0, 1, StdKeyMap},
861 {0, 0, EmacsStdKeyMap},
862 {RIGHT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* right alt */
863 {LEFT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* left alt */
864 {RIGHT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* right ctrl */
865 {LEFT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* left ctrl */
866 {0, 0, NULL}
869 static const KeyEntry Win32StdKeyMap[] =
871 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
872 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
873 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
874 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
875 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
876 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
877 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
878 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert },
879 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
880 { 0, NULL }
883 static const KeyEntry Win32KeyMapCtrl[] =
885 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
886 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
887 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
888 {/*VK_HOME*/ 0x24, WCEL_KillFromBegOfLine },
889 { 0, NULL }
892 static const KeyMap Win32KeyMap[] =
894 {0, 1, StdKeyMap},
895 {0, 0, Win32StdKeyMap},
896 {RIGHT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
897 {LEFT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
898 {0, 0, NULL}
900 #undef CTRL
902 /* ====================================================================
904 * Read line master function
906 * ====================================================================*/
908 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, BOOL can_pos_cursor)
910 WCEL_Context ctx;
911 INPUT_RECORD ir;
912 const KeyMap* km;
913 const KeyEntry* ke;
914 unsigned ofs;
915 void (*func)(struct WCEL_Context* ctx);
916 DWORD mode, input_mode, ks;
917 int use_emacs;
919 memset(&ctx, 0, sizeof(ctx));
920 ctx.hConIn = hConsoleIn;
921 WCEL_HistoryInit(&ctx);
923 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
924 use_emacs = 0;
926 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
927 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
928 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
929 return NULL;
930 if (!GetConsoleMode(hConsoleIn, &mode)) mode = 0;
931 input_mode = mode;
932 ctx.shall_echo = (mode & ENABLE_ECHO_INPUT) ? 1 : 0;
933 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) == (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS) ? 1 : 0;
934 if (!GetConsoleMode(ctx.hConOut, &mode)) mode = 0;
935 ctx.can_wrap = (mode & ENABLE_WRAP_AT_EOL_OUTPUT) ? 1 : 0;
936 ctx.can_pos_cursor = can_pos_cursor;
937 GetConsoleCursorInfo(ctx.hConOut, &ctx.cinfo);
939 if (!WCEL_Grow(&ctx, 1))
941 CloseHandle(ctx.hConOut);
942 return NULL;
944 ctx.line[0] = 0;
946 /* EPP WCEL_Dump(&ctx, "init"); */
948 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
950 if (ir.EventType != KEY_EVENT) continue;
951 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
952 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
953 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
954 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
955 if (!ir.Event.KeyEvent.bKeyDown) continue;
957 /* EPP WCEL_Dump(&ctx, "before func"); */
958 ofs = ctx.ofs;
959 /* mask out some bits which don't interest us */
960 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
962 func = NULL;
963 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
965 if (km->keyState != ks)
966 continue;
967 if (km->chkChar)
969 for (ke = &km->entries[0]; ke->func != 0; ke++)
970 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
972 else
974 for (ke = &km->entries[0]; ke->func != 0; ke++)
975 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
978 if (ke->func)
980 func = ke->func;
981 break;
985 GetConsoleMode(hConsoleIn, &mode);
986 if (input_mode != mode)
988 input_mode = mode;
989 ctx.insertkey = 0;
991 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) ==
992 (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS);
993 if (ctx.insertkey)
994 ctx.insert = !ctx.insert;
996 if (func)
997 (func)(&ctx);
998 else if (!(ir.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED))
999 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
1000 else TRACE("Dropped event\n");
1002 /* EPP WCEL_Dump(&ctx, "after func"); */
1003 if (!ctx.shall_echo) continue;
1004 if (ctx.can_pos_cursor)
1006 if (ctx.ofs != ofs)
1007 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
1009 else if (!ctx.done && !ctx.error)
1011 DWORD last;
1012 /* erase previous chars */
1013 WCEL_WriteNChars(&ctx, '\b', ctx.last_rub);
1015 /* write chars up to cursor */
1016 ctx.last_rub = WCEL_WriteConsole(&ctx, 0, ctx.ofs);
1017 /* write chars past cursor */
1018 last = ctx.last_rub + WCEL_WriteConsole(&ctx, ctx.ofs, ctx.len - ctx.ofs);
1019 if (last < ctx.last_max) /* ctx.line has been shortened, erase */
1021 WCEL_WriteNChars(&ctx, ' ', ctx.last_max - last);
1022 WCEL_WriteNChars(&ctx, '\b', ctx.last_max - last);
1023 ctx.last_max = last;
1025 else ctx.last_max = last;
1026 /* reposition at cursor */
1027 WCEL_WriteNChars(&ctx, '\b', last - ctx.last_rub);
1030 if (ctx.error)
1032 HeapFree(GetProcessHeap(), 0, ctx.line);
1033 ctx.line = NULL;
1035 WCEL_FreeYank(&ctx);
1036 if (ctx.line)
1037 CONSOLE_AppendHistory(ctx.line);
1039 CloseHandle(ctx.hConOut);
1040 HeapFree(GetProcessHeap(), 0, ctx.histCurr);
1041 return ctx.line;