xactengine3_7: Add new dll.
[wine.git] / dlls / kernel32 / editline.c
blob489960aba6efc0123625ed43245caf3015aba77f
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 "winuser.h"
31 #include "wine/unicode.h"
32 #include "winnls.h"
33 #include "wine/debug.h"
34 #include "console_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(console);
38 struct WCEL_Context;
40 typedef struct
42 WCHAR val; /* vk or unicode char */
43 void (*func)(struct WCEL_Context* ctx);
44 } KeyEntry;
46 typedef struct
48 DWORD keyState; /* keyState (from INPUT_RECORD) to match */
49 BOOL chkChar; /* check vk or char */
50 const KeyEntry* entries; /* array of entries */
51 } KeyMap;
53 typedef struct WCEL_Context {
54 WCHAR* line; /* the line being edited */
55 size_t alloc; /* number of WCHAR in line */
56 unsigned len; /* number of chars in line */
57 unsigned last_rub; /* number of chars to rub to get to start
58 (for consoles that can't change cursor pos) */
59 unsigned last_max; /* max number of chars written
60 (for consoles that can't change cursor pos) */
61 unsigned ofs; /* offset for cursor in current line */
62 WCHAR* yanked; /* yanked line */
63 unsigned mark; /* marked point (emacs mode only) */
64 CONSOLE_SCREEN_BUFFER_INFO csbi; /* current state (initial cursor, window size, attribute) */
65 CONSOLE_CURSOR_INFO cinfo; /* original cursor state (size, visibility) */
66 HANDLE hConIn;
67 HANDLE hConOut;
68 unsigned done : 1, /* to 1 when we're done with editing */
69 error : 1, /* to 1 when an error occurred in the editing */
70 can_wrap : 1, /* to 1 when multi-line edition can take place */
71 shall_echo : 1, /* to 1 when characters should be echo:ed when keyed-in */
72 insert : 1, /* to 1 when new characters are inserted (otherwise overwrite) */
73 insertkey : 1, /* to 1 when the Insert key toggle is active */
74 can_pos_cursor : 1; /* to 1 when console can (re)position cursor */
75 unsigned histSize;
76 unsigned histPos;
77 WCHAR* histCurr;
78 } WCEL_Context;
80 #if 0
81 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
83 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
84 "\t\thist=(size=%u pos=%u curr=%s)\n"
85 "\t\tyanked=%s\n",
86 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
87 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
88 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
89 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
90 debugstr_w(ctx->yanked));
92 #endif
94 /* ====================================================================
96 * Console helper functions
98 * ====================================================================*/
100 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
102 DWORD num_read;
104 if (ReadConsoleInputW(ctx->hConIn, ir, 1, &num_read)) return TRUE;
105 ctx->error = 1;
106 return FALSE;
109 static inline void WCEL_Beep(WCEL_Context* ctx)
111 Beep(400, 300);
114 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
116 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
119 static inline int WCEL_CharWidth(WCHAR wch)
121 return wch < ' ' ? 2 : 1;
124 static inline int WCEL_StringWidth(const WCHAR* str, int beg, int len)
126 int i, ofs;
128 for (i = 0, ofs = 0; i < len; i++)
129 ofs += WCEL_CharWidth(str[beg + i]);
130 return ofs;
133 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int strofs)
135 COORD c;
136 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
137 int ofs;
139 ofs = WCEL_StringWidth(ctx->line, 0, strofs);
141 c.Y = ctx->csbi.dwCursorPosition.Y;
142 if (ofs >= len)
144 ofs -= len;
145 c.X = ofs % ctx->csbi.dwSize.X;
146 c.Y += 1 + ofs / ctx->csbi.dwSize.X;
148 else c.X = ctx->csbi.dwCursorPosition.X + ofs;
149 return c;
152 static DWORD WCEL_WriteConsole(WCEL_Context* ctx, DWORD beg, DWORD len)
154 DWORD i, last, dw, ret = 0;
155 WCHAR tmp[2];
157 for (i = last = 0; i < len; i++)
159 if (ctx->line[beg + i] < ' ')
161 if (last != i)
163 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], i - last, &dw, NULL);
164 ret += dw;
166 tmp[0] = '^';
167 tmp[1] = '@' + ctx->line[beg + i];
168 WriteConsoleW(ctx->hConOut, tmp, 2, &dw, NULL);
169 last = i + 1;
170 ret += dw;
173 if (last != len)
175 WriteConsoleW(ctx->hConOut, &ctx->line[beg + last], len - last, &dw, NULL);
176 ret += dw;
178 return ret;
181 static inline void WCEL_WriteNChars(WCEL_Context* ctx, char ch, int count)
183 DWORD dw;
185 if (count > 0)
187 while (count--) WriteFile(ctx->hConOut, &ch, 1, &dw, NULL);
191 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
193 int i, last;
194 DWORD count;
195 WCHAR tmp[2];
197 /* bare console case is handled in CONSOLE_ReadLine (we always reprint the whole string) */
198 if (!ctx->shall_echo || !ctx->can_pos_cursor) return;
200 for (i = last = beg; i < beg + len; i++)
202 if (ctx->line[i] < ' ')
204 if (last != i)
206 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
207 WCEL_GetCoord(ctx, last), &count);
208 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
209 WCEL_GetCoord(ctx, last), &count);
211 tmp[0] = '^';
212 tmp[1] = '@' + ctx->line[i];
213 WriteConsoleOutputCharacterW(ctx->hConOut, tmp, 2,
214 WCEL_GetCoord(ctx, i), &count);
215 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, 2,
216 WCEL_GetCoord(ctx, i), &count);
217 last = i + 1;
220 if (last != beg + len)
222 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[last], i - last,
223 WCEL_GetCoord(ctx, last), &count);
224 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, i - last,
225 WCEL_GetCoord(ctx, last), &count);
229 /* ====================================================================
231 * context manipulation functions
233 * ====================================================================*/
235 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
237 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
239 FIXME("Mode doesn't allow wrapping. However, we should allow overwriting the current string\n");
240 return FALSE;
243 if (ctx->len + len >= ctx->alloc)
245 WCHAR* newline;
246 size_t newsize;
248 /* round up size to 32 byte-WCHAR boundary */
249 newsize = (ctx->len + len + 1 + 31) & ~31;
251 if (ctx->line)
252 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
253 else
254 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
256 if (!newline) return FALSE;
257 ctx->line = newline;
258 ctx->alloc = newsize;
260 return TRUE;
263 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
265 unsigned str_len = end - beg;
267 if (end < ctx->len)
268 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
269 /* we need to clean from ctx->len - str_len to ctx->len */
271 if (ctx->shall_echo)
273 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
274 COORD cend = WCEL_GetCoord(ctx, ctx->len);
275 CHAR_INFO ci;
277 ci.Char.UnicodeChar = ' ';
278 ci.Attributes = ctx->csbi.wAttributes;
280 if (cbeg.Y == cend.Y)
282 /* partial erase of sole line */
283 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
284 cend.X - cbeg.X, &ci);
286 else
288 int i;
289 /* erase til eol on first line */
290 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
291 ctx->csbi.dwSize.X - cbeg.X, &ci);
292 /* completely erase all the others (full lines) */
293 for (i = cbeg.Y + 1; i < cend.Y; i++)
294 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
295 /* erase from beginning of line until last pos on last line */
296 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
299 ctx->len -= str_len;
300 WCEL_Update(ctx, 0, ctx->len);
301 ctx->line[ctx->len] = 0;
304 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
306 size_t len = lstrlenW(str), updtlen;
308 if (!len) return;
309 if (ctx->insert)
311 if (!WCEL_Grow(ctx, len)) return;
312 if (ctx->len > ctx->ofs)
313 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
314 ctx->len += len;
315 updtlen = ctx->len - ctx->ofs;
317 else
319 if (ctx->ofs + len > ctx->len)
321 if (!WCEL_Grow(ctx, (ctx->ofs + len) - ctx->len)) return;
322 ctx->len = ctx->ofs + len;
324 updtlen = len;
326 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
327 ctx->line[ctx->len] = 0;
328 WCEL_Update(ctx, ctx->ofs, updtlen);
329 ctx->ofs += len;
332 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
334 WCHAR buffer[2];
336 buffer[0] = c;
337 buffer[1] = 0;
338 WCEL_InsertString(ctx, buffer);
341 static void WCEL_FreeYank(WCEL_Context* ctx)
343 HeapFree(GetProcessHeap(), 0, ctx->yanked);
344 ctx->yanked = NULL;
347 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
349 int len = end - beg;
350 if (len <= 0) return;
352 WCEL_FreeYank(ctx);
353 /* After WCEL_FreeYank ctx->yanked is empty */
354 ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
355 if (!ctx->yanked) return;
356 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
357 ctx->yanked[len] = 0;
360 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
361 * of the data lay in unicode lib
363 static inline BOOL WCEL_iswalnum(WCHAR wc)
365 WORD type;
367 GetStringTypeW( CT_CTYPE1, &wc, 1, &type );
368 return type & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
371 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
373 ofs--;
374 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
375 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
376 if (ofs >= 0) ofs++;
377 return max(ofs, 0);
380 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
382 ofs++;
383 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
384 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
385 return min(ofs, ctx->len);
388 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
390 WCHAR* ptr;
392 if (idx == ctx->histSize - 1)
394 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
395 lstrcpyW(ptr, ctx->histCurr);
397 else
399 int len = CONSOLE_GetHistory(idx, NULL, 0);
401 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
403 CONSOLE_GetHistory(idx, ptr, len);
406 return ptr;
409 static void WCEL_HistoryInit(WCEL_Context* ctx)
411 ctx->histPos = CONSOLE_GetNumHistoryEntries();
412 ctx->histSize = ctx->histPos + 1;
413 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
416 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
418 WCHAR* data = WCEL_GetHistory(ctx, idx);
419 int len = lstrlenW(data) + 1;
421 /* save current line edition for recall when needed (FIXME seems broken to me) */
422 if (ctx->histPos == ctx->histSize - 1)
424 HeapFree(GetProcessHeap(), 0, ctx->histCurr);
425 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
426 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
428 /* need to clean also the screen if new string is shorter than old one */
429 WCEL_DeleteString(ctx, 0, ctx->len);
430 ctx->ofs = 0;
431 /* insert new string */
432 if (WCEL_Grow(ctx, len))
434 WCEL_InsertString(ctx, data);
435 ctx->histPos = idx;
437 HeapFree(GetProcessHeap(), 0, data);
440 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
442 int startPos = ctx->histPos;
443 WCHAR* data;
444 unsigned int len, oldofs;
446 if (ctx->histPos && ctx->histPos == ctx->histSize) {
447 startPos--;
448 ctx->histPos--;
451 do {
452 data = WCEL_GetHistory(ctx, ctx->histPos);
454 if (ctx->histPos) ctx->histPos--;
455 else ctx->histPos = (ctx->histSize-1);
457 len = lstrlenW(data) + 1;
458 if ((len >= ctx->ofs) &&
459 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
461 /* need to clean also the screen if new string is shorter than old one */
462 WCEL_DeleteString(ctx, 0, ctx->len);
464 if (WCEL_Grow(ctx, len))
466 oldofs = ctx->ofs;
467 ctx->ofs = 0;
468 WCEL_InsertString(ctx, data);
469 ctx->ofs = oldofs;
470 if (ctx->shall_echo)
471 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
472 HeapFree(GetProcessHeap(), 0, data);
473 return;
476 HeapFree(GetProcessHeap(), 0, data);
477 } while (ctx->histPos != startPos);
479 return;
482 /* ====================================================================
484 * basic edition functions
486 * ====================================================================*/
488 static void WCEL_Done(WCEL_Context* ctx)
490 WCHAR nl = '\n';
491 if (!WCEL_Grow(ctx, 2)) return;
492 ctx->line[ctx->len++] = '\r';
493 ctx->line[ctx->len++] = '\n';
494 ctx->line[ctx->len] = 0;
495 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
496 if (ctx->insertkey)
497 SetConsoleCursorInfo(ctx->hConOut, &ctx->cinfo);
498 ctx->done = 1;
501 static void WCEL_MoveLeft(WCEL_Context* ctx)
503 if (ctx->ofs > 0) ctx->ofs--;
506 static void WCEL_MoveRight(WCEL_Context* ctx)
508 if (ctx->ofs < ctx->len) ctx->ofs++;
511 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
513 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
514 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
517 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
519 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
520 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
523 static void WCEL_MoveToBeg(WCEL_Context* ctx)
525 ctx->ofs = 0;
528 static void WCEL_MoveToEnd(WCEL_Context* ctx)
530 ctx->ofs = ctx->len;
533 static void WCEL_SetMark(WCEL_Context* ctx)
535 ctx->mark = ctx->ofs;
538 static void WCEL_ExchangeMark(WCEL_Context* ctx)
540 unsigned tmp;
542 if (ctx->mark > ctx->len) return;
543 tmp = ctx->ofs;
544 ctx->ofs = ctx->mark;
545 ctx->mark = tmp;
548 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
550 unsigned beg, end;
552 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
553 if (ctx->mark > ctx->ofs)
555 beg = ctx->ofs; end = ctx->mark;
557 else
559 beg = ctx->mark; end = ctx->ofs;
561 WCEL_SaveYank(ctx, beg, end);
564 static void WCEL_TransposeChar(WCEL_Context* ctx)
566 WCHAR c;
568 if (!ctx->ofs || ctx->ofs == ctx->len) return;
570 c = ctx->line[ctx->ofs];
571 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
572 ctx->line[ctx->ofs - 1] = c;
574 WCEL_Update(ctx, ctx->ofs - 1, 2);
575 ctx->ofs++;
578 static void WCEL_TransposeWords(WCEL_Context* ctx)
580 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
581 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
582 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
584 unsigned len_r = right_ofs - ctx->ofs;
585 unsigned len_l = ctx->ofs - left_ofs;
587 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
588 if (!tmp) return;
590 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
591 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
592 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
594 HeapFree(GetProcessHeap(), 0, tmp);
595 WCEL_Update(ctx, left_ofs, len_l + len_r);
596 ctx->ofs = right_ofs;
600 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
602 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
603 if (new_ofs != ctx->ofs)
605 CharLowerBuffW( ctx->line + ctx->ofs, new_ofs - ctx->ofs + 1 );
606 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
607 ctx->ofs = new_ofs;
611 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
613 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
614 if (new_ofs != ctx->ofs)
616 CharUpperBuffW( ctx->line + ctx->ofs, new_ofs - ctx->ofs + 1 );
617 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
618 ctx->ofs = new_ofs;
622 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
624 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
625 if (new_ofs != ctx->ofs)
627 CharUpperBuffW( ctx->line + ctx->ofs, 1 );
628 CharLowerBuffW( ctx->line + ctx->ofs + 1, new_ofs - ctx->ofs );
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 if (ctx->yanked)
637 WCEL_InsertString(ctx, ctx->yanked);
640 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
642 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
643 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
646 static void WCEL_KillFromBegOfLine(WCEL_Context* ctx)
648 if (ctx->ofs)
650 WCEL_SaveYank(ctx, 0, ctx->ofs);
651 WCEL_DeleteString(ctx, 0, ctx->ofs);
652 ctx->ofs = 0;
656 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
658 unsigned beg, end;
660 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
661 if (ctx->mark > ctx->ofs)
663 beg = ctx->ofs; end = ctx->mark;
665 else
667 beg = ctx->mark; end = ctx->ofs;
669 WCEL_SaveYank(ctx, beg, end);
670 WCEL_DeleteString(ctx, beg, end);
671 ctx->ofs = beg;
674 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
676 if (ctx->ofs)
678 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
679 ctx->ofs--;
683 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
685 if (ctx->ofs < ctx->len)
686 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
689 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
691 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
692 if (new_ofs != ctx->ofs)
694 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
695 ctx->ofs = new_ofs;
699 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
701 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
702 if (new_ofs != ctx->ofs)
704 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
708 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
710 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
713 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
715 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
718 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
720 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
723 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
725 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
728 static void WCEL_Redraw(WCEL_Context* ctx)
730 if (ctx->shall_echo)
732 COORD c = WCEL_GetCoord(ctx, ctx->len);
733 CHAR_INFO ci;
735 WCEL_Update(ctx, 0, ctx->len);
737 ci.Char.UnicodeChar = ' ';
738 ci.Attributes = ctx->csbi.wAttributes;
740 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
744 static void WCEL_RepeatCount(WCEL_Context* ctx)
746 #if 0
747 /* FIXME: wait until all console code is in kernel32 */
748 INPUT_RECORD ir;
749 unsigned repeat = 0;
751 while (WCEL_Get(ctx, &ir, FALSE))
753 if (ir.EventType != KEY_EVENT) break;
754 if (ir.Event.KeyEvent.bKeyDown)
756 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
757 break;
758 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
759 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
760 break;
761 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
763 WCEL_Get(ctx, &ir, TRUE);
765 FIXME("=> %u\n", repeat);
766 #endif
769 static void WCEL_ToggleInsert(WCEL_Context* ctx)
771 CONSOLE_CURSOR_INFO cinfo;
773 ctx->insertkey = !ctx->insertkey;
775 if (GetConsoleCursorInfo(ctx->hConOut, &cinfo))
777 cinfo.dwSize = ctx->insertkey ? 100 : 25;
778 SetConsoleCursorInfo(ctx->hConOut, &cinfo);
782 /* ====================================================================
784 * Key Maps
786 * ====================================================================*/
788 #define CTRL(x) ((x) - '@')
789 static const KeyEntry StdKeyMap[] =
791 {/*VK_BACK*/ 0x08, WCEL_DeletePrevChar },
792 {/*VK_RETURN*/0x0d, WCEL_Done },
793 {/*VK_DELETE*/0x2e, WCEL_DeleteCurrChar },
794 { 0, NULL }
797 static const KeyEntry EmacsKeyMapCtrl[] =
799 { CTRL('@'), WCEL_SetMark },
800 { CTRL('A'), WCEL_MoveToBeg },
801 { CTRL('B'), WCEL_MoveLeft },
802 /* C: done in server */
803 { CTRL('D'), WCEL_DeleteCurrChar },
804 { CTRL('E'), WCEL_MoveToEnd },
805 { CTRL('F'), WCEL_MoveRight },
806 { CTRL('G'), WCEL_Beep },
807 { CTRL('H'), WCEL_DeletePrevChar },
808 /* I: meaningless (or tab ???) */
809 { CTRL('J'), WCEL_Done },
810 { CTRL('K'), WCEL_KillToEndOfLine },
811 { CTRL('L'), WCEL_Redraw },
812 { CTRL('M'), WCEL_Done },
813 { CTRL('N'), WCEL_MoveToNextHist },
814 /* O; insert line... meaningless */
815 { CTRL('P'), WCEL_MoveToPrevHist },
816 /* Q: [NIY] quoting... */
817 /* R: [NIY] search backwards... */
818 /* S: [NIY] search forwards... */
819 { CTRL('T'), WCEL_TransposeChar },
820 { CTRL('U'), WCEL_RepeatCount },
821 /* V: paragraph down... meaningless */
822 { CTRL('W'), WCEL_KillMarkedZone },
823 { CTRL('X'), WCEL_ExchangeMark },
824 { CTRL('Y'), WCEL_Yank },
825 /* Z: meaningless */
826 { 0, NULL }
829 static const KeyEntry EmacsKeyMapAlt[] =
831 {/*DEL*/127, WCEL_DeleteLeftWord },
832 { '<', WCEL_MoveToFirstHist },
833 { '>', WCEL_MoveToLastHist },
834 { '?', WCEL_Beep },
835 { 'b', WCEL_MoveToLeftWord },
836 { 'c', WCEL_CapitalizeWord },
837 { 'd', WCEL_DeleteRightWord },
838 { 'f', WCEL_MoveToRightWord },
839 { 'l', WCEL_LowerCaseWord },
840 { 't', WCEL_TransposeWords },
841 { 'u', WCEL_UpperCaseWord },
842 { 'w', WCEL_CopyMarkedZone },
843 { 0, NULL }
846 static const KeyEntry EmacsStdKeyMap[] =
848 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
849 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
850 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
851 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
852 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
853 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
854 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert },
855 { 0, NULL }
858 static const KeyMap EmacsKeyMap[] =
860 {0, 0, 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_INSERT*/0x2d, WCEL_ToggleInsert },
878 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
879 { 0, NULL }
882 static const KeyEntry Win32KeyMapCtrl[] =
884 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
885 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
886 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
887 {/*VK_HOME*/ 0x24, WCEL_KillFromBegOfLine },
888 { 0, NULL }
891 static const KeyMap Win32KeyMap[] =
893 {0, 0, StdKeyMap},
894 {SHIFT_PRESSED, 0, 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;
918 CONSOLE_SCREEN_BUFFER_INFO csbi;
920 memset(&ctx, 0, sizeof(ctx));
921 ctx.hConIn = hConsoleIn;
922 WCEL_HistoryInit(&ctx);
924 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
925 use_emacs = 0;
927 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
928 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
929 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
930 return NULL;
931 if (!GetConsoleMode(hConsoleIn, &mode)) mode = 0;
932 input_mode = mode;
933 ctx.shall_echo = (mode & ENABLE_ECHO_INPUT) ? 1 : 0;
934 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) == (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS) ? 1 : 0;
935 if (!GetConsoleMode(ctx.hConOut, &mode)) mode = 0;
936 ctx.can_wrap = (mode & ENABLE_WRAP_AT_EOL_OUTPUT) ? 1 : 0;
937 ctx.can_pos_cursor = can_pos_cursor;
938 GetConsoleCursorInfo(ctx.hConOut, &ctx.cinfo);
940 if (!WCEL_Grow(&ctx, 1))
942 CloseHandle(ctx.hConOut);
943 return NULL;
945 ctx.line[0] = 0;
947 /* EPP WCEL_Dump(&ctx, "init"); */
949 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
951 if (ir.EventType != KEY_EVENT) continue;
952 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
953 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
954 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
955 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
956 if (!ir.Event.KeyEvent.bKeyDown) continue;
958 /* EPP WCEL_Dump(&ctx, "before func"); */
959 ofs = ctx.ofs;
960 /* mask out some bits which don't interest us */
961 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
963 func = NULL;
964 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
966 if (km->keyState != ks)
967 continue;
968 if (km->chkChar)
970 for (ke = &km->entries[0]; ke->func != 0; ke++)
971 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
973 else
975 for (ke = &km->entries[0]; ke->func != 0; ke++)
976 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
979 if (ke->func)
981 func = ke->func;
982 break;
986 CONSOLE_GetEditionMode(hConsoleIn, &use_emacs);
988 GetConsoleMode(hConsoleIn, &mode);
989 if (input_mode != mode)
991 input_mode = mode;
992 ctx.insertkey = 0;
994 ctx.insert = (mode & (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS)) ==
995 (ENABLE_INSERT_MODE|ENABLE_EXTENDED_FLAGS);
996 if (ctx.insertkey)
997 ctx.insert = !ctx.insert;
999 GetConsoleScreenBufferInfo(ctx.hConOut, &csbi);
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;