wined3d: Pass a wined3d_gl_info structure to send_attribute().
[wine/multimedia.git] / dlls / kernel32 / editline.c
blobb79c5cbdedf7e8ac065c2a1e4b9530717b0038cf
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 HANDLE hConIn;
65 HANDLE hConOut;
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 can_pos_cursor : 1; /* to 1 when console can (re)position cursor */
70 unsigned histSize;
71 unsigned histPos;
72 WCHAR* histCurr;
73 } WCEL_Context;
75 #if 0
76 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
78 MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n"
79 "\t\thist=(size=%u pos=%u curr=%s)\n"
80 "\t\tyanked=%s\n",
81 pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len,
82 ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y,
83 ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w',
84 ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr),
85 debugstr_w(ctx->yanked));
87 #endif
89 /* ====================================================================
91 * Console helper functions
93 * ====================================================================*/
95 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
97 if (ReadConsoleInputW(ctx->hConIn, ir, 1, NULL)) return TRUE;
98 ERR("hmm bad situation\n");
99 ctx->error = 1;
100 return FALSE;
103 static inline void WCEL_Beep(WCEL_Context* ctx)
105 Beep(400, 300);
108 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
110 return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
113 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int ofs)
115 COORD c;
116 int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
118 c.Y = ctx->csbi.dwCursorPosition.Y;
119 if (ofs >= len)
121 ofs -= len;
122 c.X = ofs % ctx->csbi.dwSize.X;
123 c.Y += 1 + ofs / ctx->csbi.dwSize.X;
125 else c.X = ctx->csbi.dwCursorPosition.X + ofs;
126 return c;
129 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
131 if (ctx->can_pos_cursor)
133 WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[beg], len,
134 WCEL_GetCoord(ctx, beg), NULL);
135 FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, len,
136 WCEL_GetCoord(ctx, beg), NULL);
138 else
140 char ch;
141 unsigned i;
142 DWORD dw;
144 /* erase previous chars */
145 ch = '\b';
146 for (i = beg; i < ctx->last_rub; i++)
147 WriteFile(ctx->hConOut, &ch, 1, &dw, NULL);
148 beg = min(beg, ctx->last_rub);
150 /* write new chars */
151 WriteConsoleW(ctx->hConOut, &ctx->line[beg], ctx->len - beg, &dw, NULL);
152 /* clean rest of line (if any) */
153 ch = ' ';
154 for (i = ctx->len; i < ctx->last_max; i++)
155 WriteFile(ctx->hConOut, &ch, 1, &dw, NULL);
156 ctx->last_rub = max(ctx->last_max, ctx->len);
160 /* ====================================================================
162 * context manipulation functions
164 * ====================================================================*/
166 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
168 if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
170 FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n");
171 return FALSE;
174 if (ctx->len + len >= ctx->alloc)
176 WCHAR* newline;
177 size_t newsize;
179 /* round up size to 32 byte-WCHAR boundary */
180 newsize = (ctx->len + len + 1 + 31) & ~31;
182 if (ctx->line)
183 newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
184 else
185 newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
187 if (!newline) return FALSE;
188 ctx->line = newline;
189 ctx->alloc = newsize;
191 return TRUE;
194 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
196 unsigned str_len = end - beg;
197 COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len);
198 COORD cend = WCEL_GetCoord(ctx, ctx->len);
199 CHAR_INFO ci;
201 if (end < ctx->len)
202 memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR));
203 /* we need to clean from ctx->len - str_len to ctx->len */
205 ci.Char.UnicodeChar = ' ';
206 ci.Attributes = ctx->csbi.wAttributes;
208 if (cbeg.Y == cend.Y)
210 /* partial erase of sole line */
211 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
212 cend.X - cbeg.X, &ci);
214 else
216 int i;
217 /* erase til eol on first line */
218 CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
219 ctx->csbi.dwSize.X - cbeg.X, &ci);
220 /* completely erase all the others (full lines) */
221 for (i = cbeg.Y + 1; i < cend.Y; i++)
222 CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci);
223 /* erase from beginning of line until last pos on last line */
224 CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci);
226 ctx->len -= str_len;
227 WCEL_Update(ctx, 0, ctx->len);
228 ctx->line[ctx->len] = 0;
231 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
233 size_t len = lstrlenW(str);
235 if (!len || !WCEL_Grow(ctx, len)) return;
236 if (ctx->len > ctx->ofs)
237 memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR));
238 memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR));
239 ctx->len += len;
240 ctx->line[ctx->len] = 0;
241 WCEL_Update(ctx, ctx->ofs, ctx->len - ctx->ofs);
243 ctx->ofs += len;
246 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
248 WCHAR buffer[2];
250 /* do not insert 0..31 control characters */
251 if (c < ' ' && c != '\t') return;
253 buffer[0] = c;
254 buffer[1] = 0;
255 WCEL_InsertString(ctx, buffer);
258 static void WCEL_FreeYank(WCEL_Context* ctx)
260 HeapFree(GetProcessHeap(), 0, ctx->yanked);
261 ctx->yanked = NULL;
264 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
266 int len = end - beg;
267 if (len <= 0) return;
269 WCEL_FreeYank(ctx);
270 /* After WCEL_FreeYank ctx->yanked is empty */
271 ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR));
272 if (!ctx->yanked) return;
273 memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR));
274 ctx->yanked[len] = 0;
277 /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most
278 * of the data lay in unicode lib
280 static inline BOOL WCEL_iswalnum(WCHAR wc)
282 return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
285 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
287 ofs--;
288 while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--;
289 while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--;
290 if (ofs >= 0) ofs++;
291 return max(ofs, 0);
294 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
296 ofs++;
297 while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++;
298 while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++;
299 return min(ofs, ctx->len);
302 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
304 WCHAR* ptr;
306 if (idx == ctx->histSize - 1)
308 ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
309 lstrcpyW(ptr, ctx->histCurr);
311 else
313 int len = CONSOLE_GetHistory(idx, NULL, 0);
315 if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
317 CONSOLE_GetHistory(idx, ptr, len);
320 return ptr;
323 static void WCEL_HistoryInit(WCEL_Context* ctx)
325 ctx->histPos = CONSOLE_GetNumHistoryEntries();
326 ctx->histSize = ctx->histPos + 1;
327 ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
330 static void WCEL_MoveToHist(WCEL_Context* ctx, int idx)
332 WCHAR* data = WCEL_GetHistory(ctx, idx);
333 int len = lstrlenW(data) + 1;
335 /* save current line edition for recall when needed (FIXME seems broken to me) */
336 if (ctx->histPos == ctx->histSize - 1)
338 HeapFree(GetProcessHeap(), 0, ctx->histCurr);
339 ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR));
340 memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR));
342 /* need to clean also the screen if new string is shorter than old one */
343 WCEL_DeleteString(ctx, 0, ctx->len);
344 ctx->ofs = 0;
345 /* insert new string */
346 if (WCEL_Grow(ctx, len))
348 WCEL_InsertString(ctx, data);
349 HeapFree(GetProcessHeap(), 0, data);
350 ctx->histPos = idx;
354 static void WCEL_FindPrevInHist(WCEL_Context* ctx)
356 int startPos = ctx->histPos;
357 WCHAR* data;
358 unsigned int len, oldofs;
360 if (ctx->histPos && ctx->histPos == ctx->histSize) {
361 startPos--;
362 ctx->histPos--;
365 do {
366 data = WCEL_GetHistory(ctx, ctx->histPos);
368 if (ctx->histPos) ctx->histPos--;
369 else ctx->histPos = (ctx->histSize-1);
371 len = lstrlenW(data) + 1;
372 if ((len >= ctx->ofs) &&
373 (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
375 /* need to clean also the screen if new string is shorter than old one */
376 WCEL_DeleteString(ctx, 0, ctx->len);
378 if (WCEL_Grow(ctx, len))
380 oldofs = ctx->ofs;
381 ctx->ofs = 0;
382 WCEL_InsertString(ctx, data);
383 ctx->ofs = oldofs;
384 SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs));
385 HeapFree(GetProcessHeap(), 0, data);
386 return;
389 } while (ctx->histPos != startPos);
391 return;
394 /* ====================================================================
396 * basic edition functions
398 * ====================================================================*/
400 static void WCEL_Done(WCEL_Context* ctx)
402 WCHAR nl = '\n';
403 if (!WCEL_Grow(ctx, 2)) return;
404 ctx->line[ctx->len++] = '\r';
405 ctx->line[ctx->len++] = '\n';
406 ctx->line[ctx->len] = 0;
407 WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL);
408 ctx->done = 1;
411 static void WCEL_MoveLeft(WCEL_Context* ctx)
413 if (ctx->ofs > 0) ctx->ofs--;
416 static void WCEL_MoveRight(WCEL_Context* ctx)
418 if (ctx->ofs < ctx->len) ctx->ofs++;
421 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
423 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
424 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
427 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
429 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
430 if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
433 static void WCEL_MoveToBeg(WCEL_Context* ctx)
435 ctx->ofs = 0;
438 static void WCEL_MoveToEnd(WCEL_Context* ctx)
440 ctx->ofs = ctx->len;
443 static void WCEL_SetMark(WCEL_Context* ctx)
445 ctx->mark = ctx->ofs;
448 static void WCEL_ExchangeMark(WCEL_Context* ctx)
450 unsigned tmp;
452 if (ctx->mark > ctx->len) return;
453 tmp = ctx->ofs;
454 ctx->ofs = ctx->mark;
455 ctx->mark = tmp;
458 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
460 unsigned beg, end;
462 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
463 if (ctx->mark > ctx->ofs)
465 beg = ctx->ofs; end = ctx->mark;
467 else
469 beg = ctx->mark; end = ctx->ofs;
471 WCEL_SaveYank(ctx, beg, end);
474 static void WCEL_TransposeChar(WCEL_Context* ctx)
476 WCHAR c;
478 if (!ctx->ofs || ctx->ofs == ctx->len) return;
480 c = ctx->line[ctx->ofs];
481 ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
482 ctx->line[ctx->ofs - 1] = c;
484 WCEL_Update(ctx, ctx->ofs - 1, 2);
485 ctx->ofs++;
488 static void WCEL_TransposeWords(WCEL_Context* ctx)
490 unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs),
491 right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
492 if (left_ofs < ctx->ofs && right_ofs > ctx->ofs)
494 unsigned len_r = right_ofs - ctx->ofs;
495 unsigned len_l = ctx->ofs - left_ofs;
497 char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
498 if (!tmp) return;
500 memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR));
501 memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR));
502 memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR));
504 HeapFree(GetProcessHeap(), 0, tmp);
505 WCEL_Update(ctx, left_ofs, len_l + len_r);
506 ctx->ofs = right_ofs;
510 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
512 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
513 if (new_ofs != ctx->ofs)
515 unsigned int i;
516 for (i = ctx->ofs; i <= new_ofs; i++)
517 ctx->line[i] = tolowerW(ctx->line[i]);
518 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
519 ctx->ofs = new_ofs;
523 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
525 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
526 if (new_ofs != ctx->ofs)
528 unsigned int i;
529 for (i = ctx->ofs; i <= new_ofs; i++)
530 ctx->line[i] = toupperW(ctx->line[i]);
531 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
532 ctx->ofs = new_ofs;
536 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
538 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
539 if (new_ofs != ctx->ofs)
541 unsigned int i;
543 ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]);
544 for (i = ctx->ofs + 1; i <= new_ofs; i++)
545 ctx->line[i] = tolowerW(ctx->line[i]);
546 WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1);
547 ctx->ofs = new_ofs;
551 static void WCEL_Yank(WCEL_Context* ctx)
553 WCEL_InsertString(ctx, ctx->yanked);
556 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
558 WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
559 WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
562 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
564 unsigned beg, end;
566 if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
567 if (ctx->mark > ctx->ofs)
569 beg = ctx->ofs; end = ctx->mark;
571 else
573 beg = ctx->mark; end = ctx->ofs;
575 WCEL_SaveYank(ctx, beg, end);
576 WCEL_DeleteString(ctx, beg, end);
577 ctx->ofs = beg;
580 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
582 if (ctx->ofs)
584 WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
585 ctx->ofs--;
589 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
591 if (ctx->ofs < ctx->len)
592 WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
595 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
597 unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
598 if (new_ofs != ctx->ofs)
600 WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
601 ctx->ofs = new_ofs;
605 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
607 unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
608 if (new_ofs != ctx->ofs)
610 WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
614 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
616 if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
619 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
621 if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
624 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
626 if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
629 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
631 if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
634 static void WCEL_Redraw(WCEL_Context* ctx)
636 COORD c = WCEL_GetCoord(ctx, ctx->len);
637 CHAR_INFO ci;
639 WCEL_Update(ctx, 0, ctx->len);
641 ci.Char.UnicodeChar = ' ';
642 ci.Attributes = ctx->csbi.wAttributes;
644 CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
647 static void WCEL_RepeatCount(WCEL_Context* ctx)
649 #if 0
650 /* FIXME: wait until all console code is in kernel32 */
651 INPUT_RECORD ir;
652 unsigned repeat = 0;
654 while (WCEL_Get(ctx, &ir, FALSE))
656 if (ir.EventType != KEY_EVENT) break;
657 if (ir.Event.KeyEvent.bKeyDown)
659 if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0)
660 break;
661 if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' ||
662 ir.Event.KeyEvent.uChar.UnicodeChar > '9')
663 break;
664 repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0';
666 WCEL_Get(ctx, &ir, TRUE);
668 FIXME("=> %u\n", repeat);
669 #endif
672 /* ====================================================================
674 * Key Maps
676 * ====================================================================*/
678 #define CTRL(x) ((x) - '@')
679 static const KeyEntry StdKeyMap[] =
681 {/*BACK*/0x08, WCEL_DeletePrevChar },
682 {/*RETURN*/0x0d, WCEL_Done },
683 {/*DEL*/127, WCEL_DeleteCurrChar },
684 { 0, NULL }
687 static const KeyEntry EmacsKeyMapCtrl[] =
689 { CTRL('@'), WCEL_SetMark },
690 { CTRL('A'), WCEL_MoveToBeg },
691 { CTRL('B'), WCEL_MoveLeft },
692 /* C: done in server */
693 { CTRL('D'), WCEL_DeleteCurrChar },
694 { CTRL('E'), WCEL_MoveToEnd },
695 { CTRL('F'), WCEL_MoveRight },
696 { CTRL('G'), WCEL_Beep },
697 { CTRL('H'), WCEL_DeletePrevChar },
698 /* I: meaningless (or tab ???) */
699 { CTRL('J'), WCEL_Done },
700 { CTRL('K'), WCEL_KillToEndOfLine },
701 { CTRL('L'), WCEL_Redraw },
702 { CTRL('M'), WCEL_Done },
703 { CTRL('N'), WCEL_MoveToNextHist },
704 /* O; insert line... meaningless */
705 { CTRL('P'), WCEL_MoveToPrevHist },
706 /* Q: [NIY] quoting... */
707 /* R: [NIY] search backwards... */
708 /* S: [NIY] search forwards... */
709 { CTRL('T'), WCEL_TransposeChar },
710 { CTRL('U'), WCEL_RepeatCount },
711 /* V: paragraph down... meaningless */
712 { CTRL('W'), WCEL_KillMarkedZone },
713 { CTRL('X'), WCEL_ExchangeMark },
714 { CTRL('Y'), WCEL_Yank },
715 /* Z: meaningless */
716 { 0, NULL }
719 static const KeyEntry EmacsKeyMapAlt[] =
721 {/*DEL*/127, WCEL_DeleteLeftWord },
722 { '<', WCEL_MoveToFirstHist },
723 { '>', WCEL_MoveToLastHist },
724 { '?', WCEL_Beep },
725 { 'b', WCEL_MoveToLeftWord },
726 { 'c', WCEL_CapitalizeWord },
727 { 'd', WCEL_DeleteRightWord },
728 { 'f', WCEL_MoveToRightWord },
729 { 'l', WCEL_LowerCaseWord },
730 { 't', WCEL_TransposeWords },
731 { 'u', WCEL_UpperCaseWord },
732 { 'w', WCEL_CopyMarkedZone },
733 { 0, NULL }
736 static const KeyEntry EmacsStdKeyMap[] =
738 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist },
739 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist },
740 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
741 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
742 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
743 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
744 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
745 { 0, NULL }
748 static const KeyMap EmacsKeyMap[] =
750 {0, 1, StdKeyMap},
751 {0, 0, EmacsStdKeyMap},
752 {RIGHT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* right alt */
753 {LEFT_ALT_PRESSED, 1, EmacsKeyMapAlt}, /* left alt */
754 {RIGHT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* right ctrl */
755 {LEFT_CTRL_PRESSED, 1, EmacsKeyMapCtrl}, /* left ctrl */
756 {0, 0, NULL}
759 static const KeyEntry Win32StdKeyMap[] =
761 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft },
762 {/*VK_RIGHT*/0x27, WCEL_MoveRight },
763 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg },
764 {/*VK_END*/ 0x23, WCEL_MoveToEnd },
765 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist },
766 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist },
767 {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar },
768 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist },
769 { 0, NULL }
772 static const KeyEntry Win32KeyMapCtrl[] =
774 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord },
775 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord },
776 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine },
777 { 0, NULL }
780 static const KeyMap Win32KeyMap[] =
782 {0, 1, StdKeyMap},
783 {0, 0, Win32StdKeyMap},
784 {RIGHT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
785 {LEFT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
786 {0, 0, NULL}
788 #undef CTRL
790 /* ====================================================================
792 * Read line master function
794 * ====================================================================*/
796 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, BOOL can_pos_cursor)
798 WCEL_Context ctx;
799 INPUT_RECORD ir;
800 const KeyMap* km;
801 const KeyEntry* ke;
802 unsigned ofs;
803 void (*func)(struct WCEL_Context* ctx);
804 DWORD ks;
805 int use_emacs;
807 memset(&ctx, 0, sizeof(ctx));
808 ctx.hConIn = hConsoleIn;
809 WCEL_HistoryInit(&ctx);
811 if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
812 use_emacs = 0;
814 if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL,
815 OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE ||
816 !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi))
817 return NULL;
818 ctx.can_wrap = (GetConsoleMode(ctx.hConOut, &ks) && (ks & ENABLE_WRAP_AT_EOL_OUTPUT)) ? 1 : 0;
819 ctx.can_pos_cursor = can_pos_cursor;
821 if (!WCEL_Grow(&ctx, 1))
823 CloseHandle(ctx.hConOut);
824 return NULL;
826 ctx.line[0] = 0;
828 /* EPP WCEL_Dump(&ctx, "init"); */
830 while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
832 if (ir.EventType != KEY_EVENT) continue;
833 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
834 ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount,
835 ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode,
836 ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState);
837 if (!ir.Event.KeyEvent.bKeyDown) continue;
839 /* EPP WCEL_Dump(&ctx, "before func"); */
840 ofs = ctx.ofs;
841 /* mask out some bits which don't interest us */
842 ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON|ENHANCED_KEY);
844 func = NULL;
845 for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
847 if (km->keyState != ks)
848 continue;
849 if (km->chkChar)
851 for (ke = &km->entries[0]; ke->func != 0; ke++)
852 if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
854 else
856 for (ke = &km->entries[0]; ke->func != 0; ke++)
857 if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
860 if (ke->func)
862 func = ke->func;
863 break;
867 if (func)
868 (func)(&ctx);
869 else if (!(ir.Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED))
870 WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar);
871 else TRACE("Dropped event\n");
873 /* EPP WCEL_Dump(&ctx, "after func"); */
874 if (ctx.can_pos_cursor)
876 if (ctx.ofs != ofs)
877 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
879 else if (!ctx.done && !ctx.error)
881 char ch;
882 unsigned i;
883 DWORD dw;
885 /* erase previous chars */
886 ch = '\b';
887 for (i = 0; i < ctx.last_rub; i++)
888 WriteFile(ctx.hConOut, &ch, 1, &dw, NULL);
890 /* write chars up to cursor */
891 WriteConsoleW(ctx.hConOut, ctx.line, ctx.ofs, &dw, NULL);
892 if ((ctx.last_rub = ctx.ofs) > ctx.last_max) ctx.last_max = ctx.ofs;
895 if (ctx.error)
897 HeapFree(GetProcessHeap(), 0, ctx.line);
898 ctx.line = NULL;
900 WCEL_FreeYank(&ctx);
901 if (ctx.line)
902 CONSOLE_AppendHistory(ctx.line);
904 CloseHandle(ctx.hConOut);
905 HeapFree(GetProcessHeap(), 0, ctx.histCurr);
906 return ctx.line;