ole32: Get rid of the ICOM_THIS_MULTI macro.
[wine] / dlls / kernel32 / editline.c
1 /*
2  * line edition function for Win32 console
3  *
4  * Copyright 2001 Eric Pouech
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22 #include "wine/port.h"
23
24 #include <stdarg.h>
25 #include <string.h>
26
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"
34
35 WINE_DEFAULT_DEBUG_CHANNEL(console);
36
37 struct WCEL_Context;
38
39 typedef struct
40 {
41     WCHAR                       val;            /* vk or unicode char */
42     void                        (*func)(struct WCEL_Context* ctx);
43 } KeyEntry;
44
45 typedef struct
46 {
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;
51
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;
74
75 #if 0
76 static void WCEL_Dump(WCEL_Context* ctx, const char* pfx)
77 {
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));
86 }
87 #endif
88
89 /* ====================================================================
90  *
91  * Console helper functions
92  *
93  * ====================================================================*/
94
95 static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir)
96 {
97     if (ReadConsoleInputW(ctx->hConIn, ir, 1, NULL)) return TRUE;
98     ERR("hmm bad situation\n");
99     ctx->error = 1;
100     return FALSE;
101 }
102
103 static inline void WCEL_Beep(WCEL_Context* ctx)
104 {
105     Beep(400, 300);
106 }
107
108 static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len)
109 {
110     return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X;
111 }
112
113 static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int ofs)
114 {
115     COORD       c;
116     int         len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X;
117
118     c.Y = ctx->csbi.dwCursorPosition.Y;
119     if (ofs >= len)
120     {
121         ofs -= len;
122         c.X = ofs % ctx->csbi.dwSize.X;
123         c.Y += 1 + ofs / ctx->csbi.dwSize.X;
124     }
125     else c.X = ctx->csbi.dwCursorPosition.X + ofs;
126     return c;
127 }
128
129 static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len)
130 {
131     if (ctx->can_pos_cursor)
132     {
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);
137     }
138     else
139     {
140         char ch;
141         unsigned i;
142         DWORD dw;
143
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);
149
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);
157     }
158 }
159
160 /* ====================================================================
161  *
162  * context manipulation functions
163  *
164  * ====================================================================*/
165
166 static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len)
167 {
168     if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap)
169     {
170         FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n");
171         return FALSE;
172     }
173
174     if (ctx->len + len >= ctx->alloc)
175     {
176         WCHAR*  newline;
177         size_t  newsize;
178
179         /* round up size to 32 byte-WCHAR boundary */
180         newsize = (ctx->len + len + 1 + 31) & ~31;
181
182         if (ctx->line)
183             newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize);
184         else
185             newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize);
186
187         if (!newline) return FALSE;
188         ctx->line = newline;
189         ctx->alloc = newsize;
190     }
191     return TRUE;
192 }
193
194 static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end)
195 {
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;
200
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 */
204
205     ci.Char.UnicodeChar = ' ';
206     ci.Attributes = ctx->csbi.wAttributes;
207
208     if (cbeg.Y == cend.Y)
209     {
210         /* partial erase of sole line */
211         CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y,
212                                 cend.X - cbeg.X, &ci);
213     }
214     else
215     {
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);
225     }
226     ctx->len -= str_len;
227     WCEL_Update(ctx, 0, ctx->len);
228     ctx->line[ctx->len] = 0;
229 }
230
231 static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str)
232 {
233     size_t      len = lstrlenW(str);
234
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);
242
243     ctx->ofs += len;
244 }
245
246 static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c)
247 {
248     WCHAR       buffer[2];
249
250     /* do not insert 0..31 control characters */
251     if (c < ' ' && c != '\t') return;
252
253     buffer[0] = c;
254     buffer[1] = 0;
255     WCEL_InsertString(ctx, buffer);
256 }
257
258 static void WCEL_FreeYank(WCEL_Context* ctx)
259 {
260     HeapFree(GetProcessHeap(), 0, ctx->yanked);
261     ctx->yanked = NULL;
262 }
263
264 static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end)
265 {
266     int len = end - beg;
267     if (len <= 0) return;
268
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;
275 }
276
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
279  */
280 static inline BOOL WCEL_iswalnum(WCHAR wc)
281 {
282     return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER);
283 }
284
285 static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs)
286 {
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);
292 }
293
294 static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs)
295 {
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);
300 }
301
302 static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx)
303 {
304     WCHAR*      ptr;
305
306     if (idx == ctx->histSize - 1)
307     {
308         ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR));
309         lstrcpyW(ptr, ctx->histCurr);
310     }
311     else
312     {
313         int     len = CONSOLE_GetHistory(idx, NULL, 0);
314
315         if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR))))
316         {
317             CONSOLE_GetHistory(idx, ptr, len);
318         }
319     }
320     return ptr;
321 }
322
323 static void     WCEL_HistoryInit(WCEL_Context* ctx)
324 {
325     ctx->histPos  = CONSOLE_GetNumHistoryEntries();
326     ctx->histSize = ctx->histPos + 1;
327     ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR));
328 }
329
330 static void    WCEL_MoveToHist(WCEL_Context* ctx, int idx)
331 {
332     WCHAR*      data = WCEL_GetHistory(ctx, idx);
333     int         len = lstrlenW(data) + 1;
334
335     /* save current line edition for recall when needed (FIXME seems broken to me) */
336     if (ctx->histPos == ctx->histSize - 1)
337     {
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));
341     }
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))
347     {
348         WCEL_InsertString(ctx, data);
349         HeapFree(GetProcessHeap(), 0, data);
350         ctx->histPos = idx;
351     }
352 }
353
354 static void    WCEL_FindPrevInHist(WCEL_Context* ctx)
355 {
356     int startPos = ctx->histPos;
357     WCHAR*      data;
358     unsigned int    len, oldofs;
359
360     if (ctx->histPos && ctx->histPos == ctx->histSize) {
361         startPos--;
362         ctx->histPos--;
363     }
364
365     do {
366        data = WCEL_GetHistory(ctx, ctx->histPos);
367
368        if (ctx->histPos) ctx->histPos--;
369        else ctx->histPos = (ctx->histSize-1);
370
371        len = lstrlenW(data) + 1;
372        if ((len >= ctx->ofs) &&
373            (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) {
374
375            /* need to clean also the screen if new string is shorter than old one */
376            WCEL_DeleteString(ctx, 0, ctx->len);
377
378            if (WCEL_Grow(ctx, len))
379            {
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;
387            }
388        }
389     } while (ctx->histPos != startPos);
390
391     return;
392 }
393
394 /* ====================================================================
395  *
396  * basic edition functions
397  *
398  * ====================================================================*/
399
400 static void WCEL_Done(WCEL_Context* ctx)
401 {
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;
409 }
410
411 static void WCEL_MoveLeft(WCEL_Context* ctx)
412 {
413     if (ctx->ofs > 0) ctx->ofs--;
414 }
415
416 static void WCEL_MoveRight(WCEL_Context* ctx)
417 {
418     if (ctx->ofs < ctx->len) ctx->ofs++;
419 }
420
421 static void WCEL_MoveToLeftWord(WCEL_Context* ctx)
422 {
423     unsigned int        new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
424     if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
425 }
426
427 static void WCEL_MoveToRightWord(WCEL_Context* ctx)
428 {
429     unsigned int        new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
430     if (new_ofs != ctx->ofs) ctx->ofs = new_ofs;
431 }
432
433 static void WCEL_MoveToBeg(WCEL_Context* ctx)
434 {
435     ctx->ofs = 0;
436 }
437
438 static void WCEL_MoveToEnd(WCEL_Context* ctx)
439 {
440     ctx->ofs = ctx->len;
441 }
442
443 static void WCEL_SetMark(WCEL_Context* ctx)
444 {
445     ctx->mark = ctx->ofs;
446 }
447
448 static void WCEL_ExchangeMark(WCEL_Context* ctx)
449 {
450     unsigned tmp;
451
452     if (ctx->mark > ctx->len) return;
453     tmp = ctx->ofs;
454     ctx->ofs = ctx->mark;
455     ctx->mark = tmp;
456 }
457
458 static void WCEL_CopyMarkedZone(WCEL_Context* ctx)
459 {
460     unsigned beg, end;
461
462     if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
463     if (ctx->mark > ctx->ofs)
464     {
465         beg = ctx->ofs;         end = ctx->mark;
466     }
467     else
468     {
469         beg = ctx->mark;        end = ctx->ofs;
470     }
471     WCEL_SaveYank(ctx, beg, end);
472 }
473
474 static void WCEL_TransposeChar(WCEL_Context* ctx)
475 {
476     WCHAR       c;
477
478     if (!ctx->ofs || ctx->ofs == ctx->len) return;
479
480     c = ctx->line[ctx->ofs];
481     ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1];
482     ctx->line[ctx->ofs - 1] = c;
483
484     WCEL_Update(ctx, ctx->ofs - 1, 2);
485     ctx->ofs++;
486 }
487
488 static void WCEL_TransposeWords(WCEL_Context* ctx)
489 {
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)
493     {
494         unsigned len_r = right_ofs - ctx->ofs;
495         unsigned len_l = ctx->ofs - left_ofs;
496
497         char*   tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR));
498         if (!tmp) return;
499
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));
503
504         HeapFree(GetProcessHeap(), 0, tmp);
505         WCEL_Update(ctx, left_ofs, len_l + len_r);
506         ctx->ofs = right_ofs;
507     }
508 }
509
510 static void WCEL_LowerCaseWord(WCEL_Context* ctx)
511 {
512     unsigned int        new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
513     if (new_ofs != ctx->ofs)
514     {
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;
520     }
521 }
522
523 static void WCEL_UpperCaseWord(WCEL_Context* ctx)
524 {
525     unsigned int        new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
526     if (new_ofs != ctx->ofs)
527     {
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;
533     }
534 }
535
536 static void WCEL_CapitalizeWord(WCEL_Context* ctx)
537 {
538     unsigned int        new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
539     if (new_ofs != ctx->ofs)
540     {
541         unsigned int    i;
542
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;
548     }
549 }
550
551 static void WCEL_Yank(WCEL_Context* ctx)
552 {
553     WCEL_InsertString(ctx, ctx->yanked);
554 }
555
556 static void WCEL_KillToEndOfLine(WCEL_Context* ctx)
557 {
558     WCEL_SaveYank(ctx, ctx->ofs, ctx->len);
559     WCEL_DeleteString(ctx, ctx->ofs, ctx->len);
560 }
561
562 static void WCEL_KillMarkedZone(WCEL_Context* ctx)
563 {
564     unsigned beg, end;
565
566     if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return;
567     if (ctx->mark > ctx->ofs)
568     {
569         beg = ctx->ofs;         end = ctx->mark;
570     }
571     else
572     {
573         beg = ctx->mark;        end = ctx->ofs;
574     }
575     WCEL_SaveYank(ctx, beg, end);
576     WCEL_DeleteString(ctx, beg, end);
577     ctx->ofs = beg;
578 }
579
580 static void WCEL_DeletePrevChar(WCEL_Context* ctx)
581 {
582     if (ctx->ofs)
583     {
584         WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs);
585         ctx->ofs--;
586     }
587 }
588
589 static void WCEL_DeleteCurrChar(WCEL_Context* ctx)
590 {
591     if (ctx->ofs < ctx->len)
592         WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1);
593 }
594
595 static void WCEL_DeleteLeftWord(WCEL_Context* ctx)
596 {
597     unsigned int        new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs);
598     if (new_ofs != ctx->ofs)
599     {
600         WCEL_DeleteString(ctx, new_ofs, ctx->ofs);
601         ctx->ofs = new_ofs;
602     }
603 }
604
605 static void WCEL_DeleteRightWord(WCEL_Context* ctx)
606 {
607     unsigned int        new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs);
608     if (new_ofs != ctx->ofs)
609     {
610         WCEL_DeleteString(ctx, ctx->ofs, new_ofs);
611     }
612 }
613
614 static void WCEL_MoveToPrevHist(WCEL_Context* ctx)
615 {
616     if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1);
617 }
618
619 static void WCEL_MoveToNextHist(WCEL_Context* ctx)
620 {
621     if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1);
622 }
623
624 static void WCEL_MoveToFirstHist(WCEL_Context* ctx)
625 {
626     if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0);
627 }
628
629 static void WCEL_MoveToLastHist(WCEL_Context* ctx)
630 {
631     if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1);
632 }
633
634 static void WCEL_Redraw(WCEL_Context* ctx)
635 {
636     COORD       c = WCEL_GetCoord(ctx, ctx->len);
637     CHAR_INFO   ci;
638
639     WCEL_Update(ctx, 0, ctx->len);
640
641     ci.Char.UnicodeChar = ' ';
642     ci.Attributes = ctx->csbi.wAttributes;
643
644     CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci);
645 }
646
647 static void WCEL_RepeatCount(WCEL_Context* ctx)
648 {
649 #if 0
650 /* FIXME: wait until all console code is in kernel32 */
651     INPUT_RECORD        ir;
652     unsigned            repeat = 0;
653
654     while (WCEL_Get(ctx, &ir, FALSE))
655     {
656         if (ir.EventType != KEY_EVENT) break;
657         if (ir.Event.KeyEvent.bKeyDown)
658         {
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';
665         }
666         WCEL_Get(ctx, &ir, TRUE);
667     }
668     FIXME("=> %u\n", repeat);
669 #endif
670 }
671
672 /* ====================================================================
673  *
674  *              Key Maps
675  *
676  * ====================================================================*/
677
678 #define CTRL(x) ((x) - '@')
679 static const KeyEntry StdKeyMap[] =
680 {
681     {/*BACK*/0x08,      WCEL_DeletePrevChar     },
682     {/*RETURN*/0x0d,    WCEL_Done               },
683     {/*DEL*/127,        WCEL_DeleteCurrChar     },
684     {   0,              NULL                    }
685 };
686
687 static const KeyEntry EmacsKeyMapCtrl[] =
688 {
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                    }
717 };
718
719 static const KeyEntry EmacsKeyMapAlt[] =
720 {
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                    }
734 };
735
736 static const KeyEntry EmacsStdKeyMap[] =
737 {
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                    }
746 };
747
748 static const KeyMap EmacsKeyMap[] =
749 {
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}
757 };
758
759 static const KeyEntry Win32StdKeyMap[] =
760 {
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                    }
770 };
771
772 static const KeyEntry Win32KeyMapCtrl[] =
773 {
774     {/*VK_LEFT*/ 0x25,  WCEL_MoveToLeftWord     },
775     {/*VK_RIGHT*/0x27,  WCEL_MoveToRightWord    },
776     {/*VK_END*/  0x23,  WCEL_KillToEndOfLine    },
777     {   0,              NULL                    }
778 };
779
780 static const KeyMap Win32KeyMap[] =
781 {
782     {0,                  1, StdKeyMap},
783     {0,                  0, Win32StdKeyMap},
784     {RIGHT_CTRL_PRESSED, 0, Win32KeyMapCtrl},
785     {LEFT_CTRL_PRESSED,  0, Win32KeyMapCtrl},
786     {0,                  0, NULL}
787 };
788 #undef CTRL
789
790 /* ====================================================================
791  *
792  *              Read line master function
793  *
794  * ====================================================================*/
795
796 WCHAR* CONSOLE_Readline(HANDLE hConsoleIn, BOOL can_pos_cursor)
797 {
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;
806
807     memset(&ctx, 0, sizeof(ctx));
808     ctx.hConIn = hConsoleIn;
809     WCEL_HistoryInit(&ctx);
810
811     if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs))
812         use_emacs = 0;
813
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;
820
821     if (!WCEL_Grow(&ctx, 1))
822     {
823         CloseHandle(ctx.hConOut);
824         return NULL;
825     }
826     ctx.line[0] = 0;
827
828 /* EPP     WCEL_Dump(&ctx, "init"); */
829
830     while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir))
831     {
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;
838
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);
843
844         func = NULL;
845         for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++)
846         {
847             if (km->keyState != ks)
848                 continue;
849             if (km->chkChar)
850             {
851                 for (ke = &km->entries[0]; ke->func != 0; ke++)
852                     if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break;
853             }
854             else
855             {
856                 for (ke = &km->entries[0]; ke->func != 0; ke++)
857                     if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break;
858
859             }
860             if (ke->func)
861             {
862                 func = ke->func;
863                 break;
864             }
865         }
866
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");
872
873 /* EPP         WCEL_Dump(&ctx, "after func"); */
874         if (ctx.can_pos_cursor)
875         {
876             if (ctx.ofs != ofs)
877                 SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs));
878         }
879         else if (!ctx.done && !ctx.error)
880         {
881             char        ch;
882             unsigned    i;
883             DWORD       dw;
884
885             /* erase previous chars */
886             ch = '\b';
887             for (i = 0; i < ctx.last_rub; i++)
888                 WriteFile(ctx.hConOut, &ch, 1, &dw, NULL);
889
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;
893         }
894     }
895     if (ctx.error)
896     {
897         HeapFree(GetProcessHeap(), 0, ctx.line);
898         ctx.line = NULL;
899     }
900     WCEL_FreeYank(&ctx);
901     if (ctx.line)
902         CONSOLE_AppendHistory(ctx.line);
903
904     CloseHandle(ctx.hConOut);
905     HeapFree(GetProcessHeap(), 0, ctx.histCurr);
906     return ctx.line;
907 }