comctl32: Use SetRect() instead of open coding it.
[wine.git] / dlls / kernel32 / editline.c
blobb436a5088b24d78c27dfc68c11cac9ac29d9988f
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 DWORD num_read;
103 if (ReadConsoleInputW(ctx->hConIn, ir, 1, &num_read)) return TRUE;
104 ctx->error = 1;
105 return FALSE;
108 static inline void WCEL_Beep(WCEL_Context* ctx)
110 Beep(400, 300);
113 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
115 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
118 static inline int WCEL_CharWidth(WCHAR wch)
120 return wch < ' ' ? 2 : 1;
123 static inline int WCEL_StringWidth(const WCHAR* str, int beg, int len)
125 int i, ofs;
127 for (i = 0, ofs = 0; i < len; i++)
128 ofs += WCEL_CharWidth(str[beg + i]);
129 return ofs;
132 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int strofs)
134 COORD c;
135 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
136 int ofs;
138 ofs = WCEL_StringWidth(ctx->line, 0, strofs);
140 c.Y = ctx->csbi.dwCursorPosition.Y;
141 if (ofs >= len)
143 ofs -= len;
144 c.X = ofs % ctx->csbi.dwSize.X;
145 c.Y += 1 + ofs / ctx->csbi.dwSize.X;
147 else c.X = ctx->csbi.dwCursorPosition.X + ofs;
148 return c;
151 static DWORD WCEL_WriteConsole(WCEL_Context* ctx, DWORD beg, DWORD len)
153 DWORD i, last, dw, ret = 0;
154 WCHAR tmp[2];
156 for (i = last = 0; i < len; i++)
158 if (ctx->line[beg + i] < ' ')
160 if (last != i)
162 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], i - last, &dw, NULL);
163 ret += dw;
165 tmp[0] = '^';
166 tmp[1] = '@' + ctx->line[beg + i];
167 WriteConsoleW(ctx->hConOut, tmp, 2, &dw, NULL);
168 last = i + 1;
169 ret += dw;
172 if (last != len)
174 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], len - last, &dw, NULL);
175 ret += dw;
177 return ret;
180 static inline void WCEL_WriteNChars(WCEL_Context* ctx, char ch, int count)
182 DWORD dw;
184 if (count > 0)
186 while (count--) WriteFile(ctx->hConOut, &ch, 1, &dw, NULL);
190 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
192 int i, last;
193 DWORD count;
194 WCHAR tmp[2];
196 /* bare console case is handled in CONSOLE_ReadLine (we always reprint the whole string) */
197 if (!ctx->shall_echo || !ctx->can_pos_cursor) return;
199 for (i = last = beg; i < beg + len; i++)
201 if (ctx->line[i] < ' ')
203 if (last != i)
205 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
206 WCEL_GetCoord(ctx, last), &count);
207 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
208 WCEL_GetCoord(ctx, last), &count);
210 tmp[0] = '^';
211 tmp[1] = '@' + ctx->line[i];
212 WriteConsoleOutputCharacterW(ctx->hConOut, tmp, 2,
213 WCEL_GetCoord(ctx, i), &count);
214 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, 2,
215 WCEL_GetCoord(ctx, i), &count);
216 last = i + 1;
219 if (last != beg + len)
221 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
222 WCEL_GetCoord(ctx, last), &count);
223 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
224 WCEL_GetCoord(ctx, last), &count);
228 /* ====================================================================
230 * context manipulation functions
232 * ====================================================================*/
234 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
236 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
238 FIXME("Mode doesn't allow wrapping. However, we should allow overwriting the current string\n");
239 return FALSE;
242 if (ctx->len + len >= ctx->alloc)
244 WCHAR* newline;
245 size_t newsize;
247 /* round up size to 32 byte-WCHAR boundary */
248 newsize = (ctx->len + len + 1 + 31) & ~31;
250 if (ctx->line)
251 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
252 else
253 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
255 if (!newline) return FALSE;
256 ctx->line = newline;
257 ctx->alloc = newsize;
259 return TRUE;
262 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
264 unsigned str_len = end - beg;
266 if (end < ctx->len)
267 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
268 /* we need to clean from ctx->len - str_len to ctx->len */
270 if (ctx->shall_echo)
272 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
273 COORD cend = WCEL_GetCoord(ctx, ctx->len);
274 CHAR_INFO ci;
276 ci.Char.UnicodeChar = ' ';
277 ci.Attributes = ctx->csbi.wAttributes;
279 if (cbeg.Y == cend.Y)
281 /* partial erase of sole line */
282 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
283 cend.X - cbeg.X, &ci);
285 else
287 int i;
288 /* erase til eol on first line */
289 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
290 ctx->csbi.dwSize.X - cbeg.X, &ci);
291 /* completely erase all the others (full lines) */
292 for (i = cbeg.Y + 1; i < cend.Y; i++)
293 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
294 /* erase from beginning of line until last pos on last line */
295 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
298 ctx->len -= str_len;
299 WCEL_Update(ctx, 0, ctx->len);
300 ctx->line[ctx->len] = 0;
303 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
305 size_t len = lstrlenW(str), updtlen;
307 if (!len) return;
308 if (ctx->insert)
310 if (!WCEL_Grow(ctx, len)) return;
311 if (ctx->len > ctx->ofs)
312 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
313 ctx->len += len;
314 updtlen = ctx->len - ctx->ofs;
316 else
318 if (ctx->ofs + len > ctx->len)
320 if (!WCEL_Grow(ctx, (ctx->ofs + len) - ctx->len)) return;
321 ctx->len = ctx->ofs + len;
323 updtlen = len;
325 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
326 ctx->line[ctx->len] = 0;
327 WCEL_Update(ctx, ctx->ofs, updtlen);
328 ctx->ofs += len;
331 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
333 WCHAR buffer[2];
335 buffer[0] = c;
336 buffer[1] = 0;
337 WCEL_InsertString(ctx, buffer);
340 static void WCEL_FreeYank(WCEL_Context* ctx)
342 HeapFree(GetProcessHeap(), 0, ctx->yanked);
343 ctx->yanked = NULL;
346 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
348 int len = end - beg;
349 if (len <= 0) return;
351 WCEL_FreeYank(ctx);
352 /* After WCEL_FreeYank ctx->yanked is empty */
353 ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
354 if (!ctx->yanked) return;
355 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
356 ctx->yanked[len] = 0;
359 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
360 * of the data lay in unicode lib
362 static inline BOOL WCEL_iswalnum(WCHAR wc)
364 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
367 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
369 ofs--;
370 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
371 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
372 if (ofs >= 0) ofs++;
373 return max(ofs, 0);
376 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
378 ofs++;
379 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
380 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
381 return min(ofs, ctx->len);
384 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
386 WCHAR* ptr;
388 if (idx == ctx->histSize - 1)
390 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
391 lstrcpyW(ptr, ctx->histCurr);
393 else
395 int len = CONSOLE_GetHistory(idx, NULL, 0);
397 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
399 CONSOLE_GetHistory(idx, ptr, len);
402 return ptr;
405 static void WCEL_HistoryInit(WCEL_Context* ctx)
407 ctx->histPos = CONSOLE_GetNumHistoryEntries();
408 ctx->histSize = ctx->histPos + 1;
409 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
412 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
414 WCHAR* data = WCEL_GetHistory(ctx, idx);
415 int len = lstrlenW(data) + 1;
417 /* save current line edition for recall when needed (FIXME seems broken to me) */
418 if (ctx->histPos == ctx->histSize - 1)
420 HeapFree(GetProcessHeap(), 0, ctx->histCurr);
421 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
422 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
424 /* need to clean also the screen if new string is shorter than old one */
425 WCEL_DeleteString(ctx, 0, ctx->len);
426 ctx->ofs = 0;
427 /* insert new string */
428 if (WCEL_Grow(ctx, len))
430 WCEL_InsertString(ctx, data);
431 HeapFree(GetProcessHeap(), 0, data);
432 ctx->histPos = idx;
436 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
438 int startPos = ctx->histPos;
439 WCHAR* data;
440 unsigned int len, oldofs;
442 if (ctx->histPos && ctx->histPos == ctx->histSize) {
443 startPos--;
444 ctx->histPos--;
447 do {
448 data = WCEL_GetHistory(ctx, ctx->histPos);
450 if (ctx->histPos) ctx->histPos--;
451 else ctx->histPos = (ctx->histSize-1);
453 len = lstrlenW(data) + 1;
454 if ((len >= ctx->ofs) &&
455 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
457 /* need to clean also the screen if new string is shorter than old one */
458 WCEL_DeleteString(ctx, 0, ctx->len);
460 if (WCEL_Grow(ctx, len))
462 oldofs = ctx->ofs;
463 ctx->ofs = 0;
464 WCEL_InsertString(ctx, data);
465 ctx->ofs = oldofs;
466 if (ctx->shall_echo)
467 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
468 HeapFree(GetProcessHeap(), 0, data);
469 return;
472 } while (ctx->histPos != startPos);
474 return;
477 /* ====================================================================
479 * basic edition functions
481 * ====================================================================*/
483 static void WCEL_Done(WCEL_Context* ctx)
485 WCHAR nl = '\n';
486 if (!WCEL_Grow(ctx, 2)) return;
487 ctx->line[ctx->len++] = '\r';
488 ctx->line[ctx->len++] = '\n';
489 ctx->line[ctx->len] = 0;
490 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
491 if (ctx->insertkey)
492 SetConsoleCursorInfo(ctx->hConOut, &ctx->cinfo);
493 ctx->done = 1;
496 static void WCEL_MoveLeft(WCEL_Context* ctx)
498 if (ctx->ofs > 0) ctx->ofs--;
501 static void WCEL_MoveRight(WCEL_Context* ctx)
503 if (ctx->ofs < ctx->len) ctx->ofs++;
506 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
508 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
509 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
512 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
514 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
515 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
518 static void WCEL_MoveToBeg(WCEL_Context* ctx)
520 ctx->ofs = 0;
523 static void WCEL_MoveToEnd(WCEL_Context* ctx)
525 ctx->ofs = ctx->len;
528 static void WCEL_SetMark(WCEL_Context* ctx)
530 ctx->mark = ctx->ofs;
533 static void WCEL_ExchangeMark(WCEL_Context* ctx)
535 unsigned tmp;
537 if (ctx->mark > ctx->len) return;
538 tmp = ctx->ofs;
539 ctx->ofs = ctx->mark;
540 ctx->mark = tmp;
543 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
545 unsigned beg, end;
547 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
548 if (ctx->mark > ctx->ofs)
550 beg = ctx->ofs; end = ctx->mark;
552 else
554 beg = ctx->mark; end = ctx->ofs;
556 WCEL_SaveYank(ctx, beg, end);
559 static void WCEL_TransposeChar(WCEL_Context* ctx)
561 WCHAR c;
563 if (!ctx->ofs || ctx->ofs == ctx->len) return;
565 c = ctx->line[ctx->ofs];
566 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
567 ctx->line[ctx->ofs - 1] = c;
569 WCEL_Update(ctx, ctx->ofs - 1, 2);
570 ctx->ofs++;
573 static void WCEL_TransposeWords(WCEL_Context* ctx)
575 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
576 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
577 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
579 unsigned len_r = right_ofs - ctx->ofs;
580 unsigned len_l = ctx->ofs - left_ofs;
582 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
583 if (!tmp) return;
585 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
586 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
587 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
589 HeapFree(GetProcessHeap(), 0, tmp);
590 WCEL_Update(ctx, left_ofs, len_l + len_r);
591 ctx->ofs = right_ofs;
595 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
597 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
598 if (new_ofs != ctx->ofs)
600 unsigned int i;
601 for (i = ctx->ofs; i <= new_ofs; i++)
602 ctx->line[i] = tolowerW(ctx->line[i]);
603 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
604 ctx->ofs = new_ofs;
608 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
610 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
611 if (new_ofs != ctx->ofs)
613 unsigned int i;
614 for (i = ctx->ofs; i <= new_ofs; i++)
615 ctx->line[i] = toupperW(ctx->line[i]);
616 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
617 ctx->ofs = new_ofs;
621 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
623 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
624 if (new_ofs != ctx->ofs)
626 unsigned int i;
628 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
629 for (i = ctx->ofs + 1; i <= new_ofs; i++)
630 ctx->line[i] = tolowerW(ctx->line[i]);
631 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
632 ctx->ofs = new_ofs;
636 static void WCEL_Yank(WCEL_Context* ctx)
638 WCEL_InsertString(ctx, ctx->yanked);
641 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
643 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
644 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
647 static void WCEL_KillFromBegOfLine(WCEL_Context* ctx)
649 if (ctx->ofs)
651 WCEL_SaveYank(ctx, 0, ctx->ofs);
652 WCEL_DeleteString(ctx, 0, ctx->ofs);
653 ctx->ofs = 0;
657 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
659 unsigned beg, end;
661 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
662 if (ctx->mark > ctx->ofs)
664 beg = ctx->ofs; end = ctx->mark;
666 else
668 beg = ctx->mark; end = ctx->ofs;
670 WCEL_SaveYank(ctx, beg, end);
671 WCEL_DeleteString(ctx, beg, end);
672 ctx->ofs = beg;
675 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
677 if (ctx->ofs)
679 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
680 ctx->ofs--;
684 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
686 if (ctx->ofs < ctx->len)
687 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
690 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
692 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
693 if (new_ofs != ctx->ofs)
695 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
696 ctx->ofs = new_ofs;
700 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
702 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
703 if (new_ofs != ctx->ofs)
705 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
709 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
711 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
714 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
716 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
719 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
721 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
724 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
726 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
729 static void WCEL_Redraw(WCEL_Context* ctx)
731 if (ctx->shall_echo)
733 COORD c = WCEL_GetCoord(ctx, ctx->len);
734 CHAR_INFO ci;
736 WCEL_Update(ctx, 0, ctx->len);
738 ci.Char.UnicodeChar = ' ';
739 ci.Attributes = ctx->csbi.wAttributes;
741 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
745 static void WCEL_RepeatCount(WCEL_Context* ctx)
747 #if 0
748 /* FIXME: wait until all console code is in kernel32 */
749 INPUT_RECORD ir;
750 unsigned repeat = 0;
752 while (WCEL_Get(ctx, &ir, FALSE))
754 if (ir.EventType != KEY_EVENT) break;
755 if (ir.Event.KeyEvent.bKeyDown)
757 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
758 break;
759 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
760 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
761 break;
762 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
764 WCEL_Get(ctx, &ir, TRUE);
766 FIXME("=> %u\n", repeat);
767 #endif
770 static void WCEL_ToggleInsert(WCEL_Context* ctx)
772 CONSOLE_CURSOR_INFO cinfo;
774 ctx->insertkey = !ctx->insertkey;
776 if (GetConsoleCursorInfo(ctx->hConOut, &cinfo))
778 cinfo.dwSize = ctx->insertkey ? 100 : 25;
779 SetConsoleCursorInfo(ctx->hConOut, &cinfo);
783 /* ====================================================================
785 * Key Maps
787 * ====================================================================*/
789 #define CTRL(x) ((x) - '@')
790 static const KeyEntry StdKeyMap[] =
792 {/*VK_BACK*/ 0x08, WCEL_DeletePrevChar },
793 {/*VK_RETURN*/0x0d, WCEL_Done },
794 {/*VK_DELETE*/0x2e, WCEL_DeleteCurrChar },
795 { 0, NULL }
798 static const KeyEntry EmacsKeyMapCtrl[] =
800 { CTRL('@'), WCEL_SetMark },
801 { CTRL('A'), WCEL_MoveToBeg },
802 { CTRL('B'), WCEL_MoveLeft },
803 /* C: done in server */
804 { CTRL('D'), WCEL_DeleteCurrChar },
805 { CTRL('E'), WCEL_MoveToEnd },
806 { CTRL('F'), WCEL_MoveRight },
807 { CTRL('G'), WCEL_Beep },
808 { CTRL('H'), WCEL_DeletePrevChar },
809 /* I: meaningless (or tab ???) */
810 { CTRL('J'), WCEL_Done },
811 { CTRL('K'), WCEL_KillToEndOfLine },
812 { CTRL('L'), WCEL_Redraw },
813 { CTRL('M'), WCEL_Done },
814 { CTRL('N'), WCEL_MoveToNextHist },
815 /* O; insert line... meaningless */
816 { CTRL('P'), WCEL_MoveToPrevHist },
817 /* Q: [NIY] quoting... */
818 /* R: [NIY] search backwards... */
819 /* S: [NIY] search forwards... */
820 { CTRL('T'), WCEL_TransposeChar },
821 { CTRL('U'), WCEL_RepeatCount },
822 /* V: paragraph down... meaningless */
823 { CTRL('W'), WCEL_KillMarkedZone },
824 { CTRL('X'), WCEL_ExchangeMark },
825 { CTRL('Y'), WCEL_Yank },
826 /* Z: meaningless */
827 { 0, NULL }
830 static const KeyEntry EmacsKeyMapAlt[] =
832 {/*DEL*/127, WCEL_DeleteLeftWord },
833 { '<', WCEL_MoveToFirstHist },
834 { '>', WCEL_MoveToLastHist },
835 { '?', WCEL_Beep },
836 { 'b', WCEL_MoveToLeftWord },
837 { 'c', WCEL_CapitalizeWord },
838 { 'd', WCEL_DeleteRightWord },
839 { 'f', WCEL_MoveToRightWord },
840 { 'l', WCEL_LowerCaseWord },
841 { 't', WCEL_TransposeWords },
842 { 'u', WCEL_UpperCaseWord },
843 { 'w', WCEL_CopyMarkedZone },
844 { 0, NULL }
847 static const KeyEntry EmacsStdKeyMap[] =
849 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
850 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
851 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
852 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
853 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
854 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
855 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert },
856 { 0, NULL }
859 static const KeyMap EmacsKeyMap[] =
861 {0, 0, StdKeyMap},
862 {0, 0, EmacsStdKeyMap},
863 {RIGHT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* right alt */
864 {LEFT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* left alt */
865 {RIGHT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* right ctrl */
866 {LEFT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* left ctrl */
867 {0, 0, NULL}
870 static const KeyEntry Win32StdKeyMap[] =
872 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
873 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
874 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
875 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
876 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
877 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
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, 0, StdKeyMap},
895 {SHIFT_PRESSED, 0, StdKeyMap},
896 {0, 0, Win32StdKeyMap},
897 {RIGHT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
898 {LEFT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
899 {0, 0, NULL}
901 #undef CTRL
903 /* ====================================================================
905 * Read line master function
907 * ====================================================================*/
909 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, BOOL can_pos_cursor)
911 WCEL_Context ctx;
912 INPUT_RECORD ir;
913 const KeyMap* km;
914 const KeyEntry* ke;
915 unsigned ofs;
916 void (*func)(struct WCEL_Context* ctx);
917 DWORD mode, input_mode, ks;
918 int use_emacs;
919 CONSOLE_SCREEN_BUFFER_INFO csbi;
921 memset(&ctx, 0, sizeof(ctx));
922 ctx.hConIn = hConsoleIn;
923 WCEL_HistoryInit(&ctx);
925 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
926 use_emacs = 0;
928 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
929 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
930 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
931 return NULL;
932 if (!GetConsoleMode(hConsoleIn, &mode)) mode = 0;
933 input_mode = mode;
934 ctx.shall_echo = (mode & ENABLE_ECHO_INPUT) ? 1 : 0;
935 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) == (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS) ? 1 : 0;
936 if (!GetConsoleMode(ctx.hConOut, &mode)) mode = 0;
937 ctx.can_wrap = (mode & ENABLE_WRAP_AT_EOL_OUTPUT) ? 1 : 0;
938 ctx.can_pos_cursor = can_pos_cursor;
939 GetConsoleCursorInfo(ctx.hConOut, &ctx.cinfo);
941 if (!WCEL_Grow(&ctx, 1))
943 CloseHandle(ctx.hConOut);
944 return NULL;
946 ctx.line[0] = 0;
948 /* EPP WCEL_Dump(&ctx, "init"); */
950 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
952 if (ir.EventType != KEY_EVENT) continue;
953 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
954 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
955 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
956 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
957 if (!ir.Event.KeyEvent.bKeyDown) continue;
959 /* EPP WCEL_Dump(&ctx, "before func"); */
960 ofs = ctx.ofs;
961 /* mask out some bits which don't interest us */
962 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
964 func = NULL;
965 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
967 if (km->keyState != ks)
968 continue;
969 if (km->chkChar)
971 for (ke = &km->entries[0]; ke->func != 0; ke++)
972 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
974 else
976 for (ke = &km->entries[0]; ke->func != 0; ke++)
977 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
980 if (ke->func)
982 func = ke->func;
983 break;
987 GetConsoleMode(hConsoleIn, &mode);
988 if (input_mode != mode)
990 input_mode = mode;
991 ctx.insertkey = 0;
993 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) ==
994 (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS);
995 if (ctx.insertkey)
996 ctx.insert = !ctx.insert;
998 GetConsoleScreenBufferInfo(ctx.hConOut, &csbi);
999 if (ctx.csbi.wAttributes != csbi.wAttributes)
1000 ctx.csbi.wAttributes = csbi.wAttributes;
1002 if (func)
1003 (func)(&ctx);
1004 else if (!(ir.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED))
1005 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
1006 else TRACE("Dropped event\n");
1008 /* EPP WCEL_Dump(&ctx, "after func"); */
1009 if (!ctx.shall_echo) continue;
1010 if (ctx.can_pos_cursor)
1012 if (ctx.ofs != ofs)
1013 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
1015 else if (!ctx.done && !ctx.error)
1017 DWORD last;
1018 /* erase previous chars */
1019 WCEL_WriteNChars(&ctx, '\b', ctx.last_rub);
1021 /* write chars up to cursor */
1022 ctx.last_rub = WCEL_WriteConsole(&ctx, 0, ctx.ofs);
1023 /* write chars past cursor */
1024 last = ctx.last_rub + WCEL_WriteConsole(&ctx, ctx.ofs, ctx.len - ctx.ofs);
1025 if (last < ctx.last_max) /* ctx.line has been shortened, erase */
1027 WCEL_WriteNChars(&ctx, ' ', ctx.last_max - last);
1028 WCEL_WriteNChars(&ctx, '\b', ctx.last_max - last);
1029 ctx.last_max = last;
1031 else ctx.last_max = last;
1032 /* reposition at cursor */
1033 WCEL_WriteNChars(&ctx, '\b', last - ctx.last_rub);
1036 if (ctx.error)
1038 HeapFree(GetProcessHeap(), 0, ctx.line);
1039 ctx.line = NULL;
1041 WCEL_FreeYank(&ctx);
1042 if (ctx.line)
1043 CONSOLE_AppendHistory(ctx.line);
1045 CloseHandle(ctx.hConOut);
1046 HeapFree(GetProcessHeap(), 0, ctx.histCurr);
1047 return ctx.line;