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
22 #include "wine/port.h"
30 #include "wine/unicode.h"
32 #include "wine/debug.h"
33 #include "console_private.h"
35 WINE_DEFAULT_DEBUG_CHANNEL(console
);
41 WCHAR val
; /* vk or unicode char */
42 void (*func
)(struct WCEL_Context
* ctx
);
47 DWORD keyState
; /* keyState (from INPUT_RECORD) to match */
48 BOOL chkChar
; /* check vk or char */
49 const KeyEntry
* entries
; /* array of entries */
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) */
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 */
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"
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
));
93 /* ====================================================================
95 * Console helper functions
97 * ====================================================================*/
99 static BOOL
WCEL_Get(WCEL_Context
* ctx
, INPUT_RECORD
* ir
)
103 if (ReadConsoleInputW(ctx
->hConIn
, ir
, 1, &num_read
)) return TRUE
;
108 static inline void WCEL_Beep(WCEL_Context
* ctx
)
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
)
127 for (i
= 0, ofs
= 0; i
< len
; i
++)
128 ofs
+= WCEL_CharWidth(str
[beg
+ i
]);
132 static inline COORD
WCEL_GetCoord(WCEL_Context
* ctx
, int strofs
)
135 int len
= ctx
->csbi
.dwSize
.X
- ctx
->csbi
.dwCursorPosition
.X
;
138 ofs
= WCEL_StringWidth(ctx
->line
, 0, strofs
);
140 c
.Y
= ctx
->csbi
.dwCursorPosition
.Y
;
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
;
151 static DWORD
WCEL_WriteConsole(WCEL_Context
* ctx
, DWORD beg
, DWORD len
)
153 DWORD i
, last
, dw
, ret
= 0;
156 for (i
= last
= 0; i
< len
; i
++)
158 if (ctx
->line
[beg
+ i
] < ' ')
162 WriteConsoleW(ctx
->hConOut
, &ctx
->line
[beg
+ last
], i
- last
, &dw
, NULL
);
166 tmp
[1] = '@' + ctx
->line
[beg
+ i
];
167 WriteConsoleW(ctx
->hConOut
, tmp
, 2, &dw
, NULL
);
174 WriteConsoleW(ctx
->hConOut
, &ctx
->line
[beg
+ last
], len
- last
, &dw
, NULL
);
180 static inline void WCEL_WriteNChars(WCEL_Context
* ctx
, char ch
, int count
)
186 while (count
--) WriteFile(ctx
->hConOut
, &ch
, 1, &dw
, NULL
);
190 static inline void WCEL_Update(WCEL_Context
* ctx
, int beg
, int len
)
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
] < ' ')
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
);
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
);
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");
242 if (ctx
->len
+ len
>= ctx
->alloc
)
247 /* round up size to 32 byte-WCHAR boundary */
248 newsize
= (ctx
->len
+ len
+ 1 + 31) & ~31;
251 newline
= HeapReAlloc(GetProcessHeap(), 0, ctx
->line
, sizeof(WCHAR
) * newsize
);
253 newline
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * newsize
);
255 if (!newline
) return FALSE
;
257 ctx
->alloc
= newsize
;
262 static void WCEL_DeleteString(WCEL_Context
* ctx
, int beg
, int end
)
264 unsigned str_len
= end
- beg
;
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 */
272 COORD cbeg
= WCEL_GetCoord(ctx
, ctx
->len
- str_len
);
273 COORD cend
= WCEL_GetCoord(ctx
, ctx
->len
);
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
);
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
);
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
;
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
));
314 updtlen
= ctx
->len
- ctx
->ofs
;
318 if (ctx
->ofs
+ len
> ctx
->len
)
320 if (!WCEL_Grow(ctx
, (ctx
->ofs
+ len
) - ctx
->len
)) return;
321 ctx
->len
= ctx
->ofs
+ len
;
325 memcpy(&ctx
->line
[ctx
->ofs
], str
, len
* sizeof(WCHAR
));
326 ctx
->line
[ctx
->len
] = 0;
327 WCEL_Update(ctx
, ctx
->ofs
, updtlen
);
331 static void WCEL_InsertChar(WCEL_Context
* ctx
, WCHAR c
)
337 WCEL_InsertString(ctx
, buffer
);
340 static void WCEL_FreeYank(WCEL_Context
* ctx
)
342 HeapFree(GetProcessHeap(), 0, ctx
->yanked
);
346 static void WCEL_SaveYank(WCEL_Context
* ctx
, int beg
, int end
)
349 if (len
<= 0) return;
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
)
370 while (ofs
>= 0 && !WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
371 while (ofs
>= 0 && WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
376 static int WCEL_GetRightWordTransition(WCEL_Context
* ctx
, int 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
)
388 if (idx
== ctx
->histSize
- 1)
390 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx
->histCurr
) + 1) * sizeof(WCHAR
));
391 lstrcpyW(ptr
, ctx
->histCurr
);
395 int len
= CONSOLE_GetHistory(idx
, NULL
, 0);
397 if ((ptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
399 CONSOLE_GetHistory(idx
, ptr
, len
);
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
);
427 /* insert new string */
428 if (WCEL_Grow(ctx
, len
))
430 WCEL_InsertString(ctx
, data
);
433 HeapFree(GetProcessHeap(), 0, data
);
436 static void WCEL_FindPrevInHist(WCEL_Context
* ctx
)
438 int startPos
= ctx
->histPos
;
440 unsigned int len
, oldofs
;
442 if (ctx
->histPos
&& ctx
->histPos
== ctx
->histSize
) {
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
))
464 WCEL_InsertString(ctx
, data
);
467 SetConsoleCursorPosition(ctx
->hConOut
, WCEL_GetCoord(ctx
, ctx
->ofs
));
468 HeapFree(GetProcessHeap(), 0, data
);
472 HeapFree(GetProcessHeap(), 0, data
);
473 } while (ctx
->histPos
!= startPos
);
478 /* ====================================================================
480 * basic edition functions
482 * ====================================================================*/
484 static void WCEL_Done(WCEL_Context
* ctx
)
487 if (!WCEL_Grow(ctx
, 2)) return;
488 ctx
->line
[ctx
->len
++] = '\r';
489 ctx
->line
[ctx
->len
++] = '\n';
490 ctx
->line
[ctx
->len
] = 0;
491 WriteConsoleW(ctx
->hConOut
, &nl
, 1, NULL
, NULL
);
493 SetConsoleCursorInfo(ctx
->hConOut
, &ctx
->cinfo
);
497 static void WCEL_MoveLeft(WCEL_Context
* ctx
)
499 if (ctx
->ofs
> 0) ctx
->ofs
--;
502 static void WCEL_MoveRight(WCEL_Context
* ctx
)
504 if (ctx
->ofs
< ctx
->len
) ctx
->ofs
++;
507 static void WCEL_MoveToLeftWord(WCEL_Context
* ctx
)
509 unsigned int new_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
);
510 if (new_ofs
!= ctx
->ofs
) ctx
->ofs
= new_ofs
;
513 static void WCEL_MoveToRightWord(WCEL_Context
* ctx
)
515 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
516 if (new_ofs
!= ctx
->ofs
) ctx
->ofs
= new_ofs
;
519 static void WCEL_MoveToBeg(WCEL_Context
* ctx
)
524 static void WCEL_MoveToEnd(WCEL_Context
* ctx
)
529 static void WCEL_SetMark(WCEL_Context
* ctx
)
531 ctx
->mark
= ctx
->ofs
;
534 static void WCEL_ExchangeMark(WCEL_Context
* ctx
)
538 if (ctx
->mark
> ctx
->len
) return;
540 ctx
->ofs
= ctx
->mark
;
544 static void WCEL_CopyMarkedZone(WCEL_Context
* ctx
)
548 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
549 if (ctx
->mark
> ctx
->ofs
)
551 beg
= ctx
->ofs
; end
= ctx
->mark
;
555 beg
= ctx
->mark
; end
= ctx
->ofs
;
557 WCEL_SaveYank(ctx
, beg
, end
);
560 static void WCEL_TransposeChar(WCEL_Context
* ctx
)
564 if (!ctx
->ofs
|| ctx
->ofs
== ctx
->len
) return;
566 c
= ctx
->line
[ctx
->ofs
];
567 ctx
->line
[ctx
->ofs
] = ctx
->line
[ctx
->ofs
- 1];
568 ctx
->line
[ctx
->ofs
- 1] = c
;
570 WCEL_Update(ctx
, ctx
->ofs
- 1, 2);
574 static void WCEL_TransposeWords(WCEL_Context
* ctx
)
576 unsigned int left_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
),
577 right_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
578 if (left_ofs
< ctx
->ofs
&& right_ofs
> ctx
->ofs
)
580 unsigned len_r
= right_ofs
- ctx
->ofs
;
581 unsigned len_l
= ctx
->ofs
- left_ofs
;
583 char* tmp
= HeapAlloc(GetProcessHeap(), 0, len_r
* sizeof(WCHAR
));
586 memcpy(tmp
, &ctx
->line
[ctx
->ofs
], len_r
* sizeof(WCHAR
));
587 memmove(&ctx
->line
[left_ofs
+ len_r
], &ctx
->line
[left_ofs
], len_l
* sizeof(WCHAR
));
588 memcpy(&ctx
->line
[left_ofs
], tmp
, len_r
* sizeof(WCHAR
));
590 HeapFree(GetProcessHeap(), 0, tmp
);
591 WCEL_Update(ctx
, left_ofs
, len_l
+ len_r
);
592 ctx
->ofs
= right_ofs
;
596 static void WCEL_LowerCaseWord(WCEL_Context
* ctx
)
598 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
599 if (new_ofs
!= ctx
->ofs
)
602 for (i
= ctx
->ofs
; i
<= new_ofs
; i
++)
603 ctx
->line
[i
] = tolowerW(ctx
->line
[i
]);
604 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
609 static void WCEL_UpperCaseWord(WCEL_Context
* ctx
)
611 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
612 if (new_ofs
!= ctx
->ofs
)
615 for (i
= ctx
->ofs
; i
<= new_ofs
; i
++)
616 ctx
->line
[i
] = toupperW(ctx
->line
[i
]);
617 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
622 static void WCEL_CapitalizeWord(WCEL_Context
* ctx
)
624 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
625 if (new_ofs
!= ctx
->ofs
)
629 ctx
->line
[ctx
->ofs
] = toupperW(ctx
->line
[ctx
->ofs
]);
630 for (i
= ctx
->ofs
+ 1; i
<= new_ofs
; i
++)
631 ctx
->line
[i
] = tolowerW(ctx
->line
[i
]);
632 WCEL_Update(ctx
, ctx
->ofs
, new_ofs
- ctx
->ofs
+ 1);
637 static void WCEL_Yank(WCEL_Context
* ctx
)
640 WCEL_InsertString(ctx
, ctx
->yanked
);
643 static void WCEL_KillToEndOfLine(WCEL_Context
* ctx
)
645 WCEL_SaveYank(ctx
, ctx
->ofs
, ctx
->len
);
646 WCEL_DeleteString(ctx
, ctx
->ofs
, ctx
->len
);
649 static void WCEL_KillFromBegOfLine(WCEL_Context
* ctx
)
653 WCEL_SaveYank(ctx
, 0, ctx
->ofs
);
654 WCEL_DeleteString(ctx
, 0, ctx
->ofs
);
659 static void WCEL_KillMarkedZone(WCEL_Context
* ctx
)
663 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
664 if (ctx
->mark
> ctx
->ofs
)
666 beg
= ctx
->ofs
; end
= ctx
->mark
;
670 beg
= ctx
->mark
; end
= ctx
->ofs
;
672 WCEL_SaveYank(ctx
, beg
, end
);
673 WCEL_DeleteString(ctx
, beg
, end
);
677 static void WCEL_DeletePrevChar(WCEL_Context
* ctx
)
681 WCEL_DeleteString(ctx
, ctx
->ofs
- 1, ctx
->ofs
);
686 static void WCEL_DeleteCurrChar(WCEL_Context
* ctx
)
688 if (ctx
->ofs
< ctx
->len
)
689 WCEL_DeleteString(ctx
, ctx
->ofs
, ctx
->ofs
+ 1);
692 static void WCEL_DeleteLeftWord(WCEL_Context
* ctx
)
694 unsigned int new_ofs
= WCEL_GetLeftWordTransition(ctx
, ctx
->ofs
);
695 if (new_ofs
!= ctx
->ofs
)
697 WCEL_DeleteString(ctx
, new_ofs
, ctx
->ofs
);
702 static void WCEL_DeleteRightWord(WCEL_Context
* ctx
)
704 unsigned int new_ofs
= WCEL_GetRightWordTransition(ctx
, ctx
->ofs
);
705 if (new_ofs
!= ctx
->ofs
)
707 WCEL_DeleteString(ctx
, ctx
->ofs
, new_ofs
);
711 static void WCEL_MoveToPrevHist(WCEL_Context
* ctx
)
713 if (ctx
->histPos
) WCEL_MoveToHist(ctx
, ctx
->histPos
- 1);
716 static void WCEL_MoveToNextHist(WCEL_Context
* ctx
)
718 if (ctx
->histPos
< ctx
->histSize
- 1) WCEL_MoveToHist(ctx
, ctx
->histPos
+ 1);
721 static void WCEL_MoveToFirstHist(WCEL_Context
* ctx
)
723 if (ctx
->histPos
!= 0) WCEL_MoveToHist(ctx
, 0);
726 static void WCEL_MoveToLastHist(WCEL_Context
* ctx
)
728 if (ctx
->histPos
!= ctx
->histSize
- 1) WCEL_MoveToHist(ctx
, ctx
->histSize
- 1);
731 static void WCEL_Redraw(WCEL_Context
* ctx
)
735 COORD c
= WCEL_GetCoord(ctx
, ctx
->len
);
738 WCEL_Update(ctx
, 0, ctx
->len
);
740 ci
.Char
.UnicodeChar
= ' ';
741 ci
.Attributes
= ctx
->csbi
.wAttributes
;
743 CONSOLE_FillLineUniform(ctx
->hConOut
, c
.X
, c
.Y
, ctx
->csbi
.dwSize
.X
- c
.X
, &ci
);
747 static void WCEL_RepeatCount(WCEL_Context
* ctx
)
750 /* FIXME: wait until all console code is in kernel32 */
754 while (WCEL_Get(ctx
, &ir
, FALSE
))
756 if (ir
.EventType
!= KEY_EVENT
) break;
757 if (ir
.Event
.KeyEvent
.bKeyDown
)
759 if ((ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
)) != 0)
761 if (ir
.Event
.KeyEvent
.uChar
.UnicodeChar
< '0' ||
762 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
> '9')
764 repeat
= repeat
* 10 + ir
.Event
.KeyEvent
.uChar
.UnicodeChar
- '0';
766 WCEL_Get(ctx
, &ir
, TRUE
);
768 FIXME("=> %u\n", repeat
);
772 static void WCEL_ToggleInsert(WCEL_Context
* ctx
)
774 CONSOLE_CURSOR_INFO cinfo
;
776 ctx
->insertkey
= !ctx
->insertkey
;
778 if (GetConsoleCursorInfo(ctx
->hConOut
, &cinfo
))
780 cinfo
.dwSize
= ctx
->insertkey
? 100 : 25;
781 SetConsoleCursorInfo(ctx
->hConOut
, &cinfo
);
785 /* ====================================================================
789 * ====================================================================*/
791 #define CTRL(x) ((x) - '@')
792 static const KeyEntry StdKeyMap
[] =
794 {/*VK_BACK*/ 0x08, WCEL_DeletePrevChar
},
795 {/*VK_RETURN*/0x0d, WCEL_Done
},
796 {/*VK_DELETE*/0x2e, WCEL_DeleteCurrChar
},
800 static const KeyEntry EmacsKeyMapCtrl
[] =
802 { CTRL('@'), WCEL_SetMark
},
803 { CTRL('A'), WCEL_MoveToBeg
},
804 { CTRL('B'), WCEL_MoveLeft
},
805 /* C: done in server */
806 { CTRL('D'), WCEL_DeleteCurrChar
},
807 { CTRL('E'), WCEL_MoveToEnd
},
808 { CTRL('F'), WCEL_MoveRight
},
809 { CTRL('G'), WCEL_Beep
},
810 { CTRL('H'), WCEL_DeletePrevChar
},
811 /* I: meaningless (or tab ???) */
812 { CTRL('J'), WCEL_Done
},
813 { CTRL('K'), WCEL_KillToEndOfLine
},
814 { CTRL('L'), WCEL_Redraw
},
815 { CTRL('M'), WCEL_Done
},
816 { CTRL('N'), WCEL_MoveToNextHist
},
817 /* O; insert line... meaningless */
818 { CTRL('P'), WCEL_MoveToPrevHist
},
819 /* Q: [NIY] quoting... */
820 /* R: [NIY] search backwards... */
821 /* S: [NIY] search forwards... */
822 { CTRL('T'), WCEL_TransposeChar
},
823 { CTRL('U'), WCEL_RepeatCount
},
824 /* V: paragraph down... meaningless */
825 { CTRL('W'), WCEL_KillMarkedZone
},
826 { CTRL('X'), WCEL_ExchangeMark
},
827 { CTRL('Y'), WCEL_Yank
},
832 static const KeyEntry EmacsKeyMapAlt
[] =
834 {/*DEL*/127, WCEL_DeleteLeftWord
},
835 { '<', WCEL_MoveToFirstHist
},
836 { '>', WCEL_MoveToLastHist
},
838 { 'b', WCEL_MoveToLeftWord
},
839 { 'c', WCEL_CapitalizeWord
},
840 { 'd', WCEL_DeleteRightWord
},
841 { 'f', WCEL_MoveToRightWord
},
842 { 'l', WCEL_LowerCaseWord
},
843 { 't', WCEL_TransposeWords
},
844 { 'u', WCEL_UpperCaseWord
},
845 { 'w', WCEL_CopyMarkedZone
},
849 static const KeyEntry EmacsStdKeyMap
[] =
851 {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist
},
852 {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist
},
853 {/*VK_END*/ 0x23, WCEL_MoveToEnd
},
854 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg
},
855 {/*VK_RIGHT*/0x27, WCEL_MoveRight
},
856 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft
},
857 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert
},
861 static const KeyMap EmacsKeyMap
[] =
864 {0, 0, EmacsStdKeyMap
},
865 {RIGHT_ALT_PRESSED
, 1, EmacsKeyMapAlt
}, /* right alt */
866 {LEFT_ALT_PRESSED
, 1, EmacsKeyMapAlt
}, /* left alt */
867 {RIGHT_CTRL_PRESSED
, 1, EmacsKeyMapCtrl
}, /* right ctrl */
868 {LEFT_CTRL_PRESSED
, 1, EmacsKeyMapCtrl
}, /* left ctrl */
872 static const KeyEntry Win32StdKeyMap
[] =
874 {/*VK_LEFT*/ 0x25, WCEL_MoveLeft
},
875 {/*VK_RIGHT*/0x27, WCEL_MoveRight
},
876 {/*VK_HOME*/ 0x24, WCEL_MoveToBeg
},
877 {/*VK_END*/ 0x23, WCEL_MoveToEnd
},
878 {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist
},
879 {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist
},
880 {/*VK_INSERT*/0x2d, WCEL_ToggleInsert
},
881 {/*VK_F8*/ 0x77, WCEL_FindPrevInHist
},
885 static const KeyEntry Win32KeyMapCtrl
[] =
887 {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord
},
888 {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord
},
889 {/*VK_END*/ 0x23, WCEL_KillToEndOfLine
},
890 {/*VK_HOME*/ 0x24, WCEL_KillFromBegOfLine
},
894 static const KeyMap Win32KeyMap
[] =
897 {SHIFT_PRESSED
, 0, StdKeyMap
},
898 {0, 0, Win32StdKeyMap
},
899 {RIGHT_CTRL_PRESSED
, 0, Win32KeyMapCtrl
},
900 {LEFT_CTRL_PRESSED
, 0, Win32KeyMapCtrl
},
905 /* ====================================================================
907 * Read line master function
909 * ====================================================================*/
911 WCHAR
* CONSOLE_Readline(HANDLE hConsoleIn
, BOOL can_pos_cursor
)
918 void (*func
)(struct WCEL_Context
* ctx
);
919 DWORD mode
, input_mode
, ks
;
921 CONSOLE_SCREEN_BUFFER_INFO csbi
;
923 memset(&ctx
, 0, sizeof(ctx
));
924 ctx
.hConIn
= hConsoleIn
;
925 WCEL_HistoryInit(&ctx
);
927 if (!CONSOLE_GetEditionMode(hConsoleIn
, &use_emacs
))
930 if ((ctx
.hConOut
= CreateFileA("CONOUT$", GENERIC_READ
|GENERIC_WRITE
, 0, NULL
,
931 OPEN_EXISTING
, 0, 0 )) == INVALID_HANDLE_VALUE
||
932 !GetConsoleScreenBufferInfo(ctx
.hConOut
, &ctx
.csbi
))
934 if (!GetConsoleMode(hConsoleIn
, &mode
)) mode
= 0;
936 ctx
.shall_echo
= (mode
& ENABLE_ECHO_INPUT
) ? 1 : 0;
937 ctx
.insert
= (mode
& (ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
)) == (ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
) ? 1 : 0;
938 if (!GetConsoleMode(ctx
.hConOut
, &mode
)) mode
= 0;
939 ctx
.can_wrap
= (mode
& ENABLE_WRAP_AT_EOL_OUTPUT
) ? 1 : 0;
940 ctx
.can_pos_cursor
= can_pos_cursor
;
941 GetConsoleCursorInfo(ctx
.hConOut
, &ctx
.cinfo
);
943 if (!WCEL_Grow(&ctx
, 1))
945 CloseHandle(ctx
.hConOut
);
950 /* EPP WCEL_Dump(&ctx, "init"); */
952 while (!ctx
.done
&& !ctx
.error
&& WCEL_Get(&ctx
, &ir
))
954 if (ir
.EventType
!= KEY_EVENT
) continue;
955 TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n",
956 ir
.Event
.KeyEvent
.bKeyDown
? "Down" : "Up ", ir
.Event
.KeyEvent
.wRepeatCount
,
957 ir
.Event
.KeyEvent
.wVirtualKeyCode
, ir
.Event
.KeyEvent
.wVirtualScanCode
,
958 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
, ir
.Event
.KeyEvent
.dwControlKeyState
);
959 if (!ir
.Event
.KeyEvent
.bKeyDown
) continue;
961 /* EPP WCEL_Dump(&ctx, "before func"); */
963 /* mask out some bits which don't interest us */
964 ks
= ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
|ENHANCED_KEY
);
967 for (km
= (use_emacs
) ? EmacsKeyMap
: Win32KeyMap
; km
->entries
!= NULL
; km
++)
969 if (km
->keyState
!= ks
)
973 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
974 if (ke
->val
== ir
.Event
.KeyEvent
.uChar
.UnicodeChar
) break;
978 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
979 if (ke
->val
== ir
.Event
.KeyEvent
.wVirtualKeyCode
) break;
989 CONSOLE_GetEditionMode(hConsoleIn
, &use_emacs
);
991 GetConsoleMode(hConsoleIn
, &mode
);
992 if (input_mode
!= mode
)
997 ctx
.insert
= (mode
& (ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
)) ==
998 (ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
);
1000 ctx
.insert
= !ctx
.insert
;
1002 GetConsoleScreenBufferInfo(ctx
.hConOut
, &csbi
);
1003 ctx
.csbi
.wAttributes
= csbi
.wAttributes
;
1007 else if (!(ir
.Event
.KeyEvent
.dwControlKeyState
& LEFT_ALT_PRESSED
))
1008 WCEL_InsertChar(&ctx
, ir
.Event
.KeyEvent
.uChar
.UnicodeChar
);
1009 else TRACE("Dropped event\n");
1011 /* EPP WCEL_Dump(&ctx, "after func"); */
1012 if (!ctx
.shall_echo
) continue;
1013 if (ctx
.can_pos_cursor
)
1016 SetConsoleCursorPosition(ctx
.hConOut
, WCEL_GetCoord(&ctx
, ctx
.ofs
));
1018 else if (!ctx
.done
&& !ctx
.error
)
1021 /* erase previous chars */
1022 WCEL_WriteNChars(&ctx
, '\b', ctx
.last_rub
);
1024 /* write chars up to cursor */
1025 ctx
.last_rub
= WCEL_WriteConsole(&ctx
, 0, ctx
.ofs
);
1026 /* write chars past cursor */
1027 last
= ctx
.last_rub
+ WCEL_WriteConsole(&ctx
, ctx
.ofs
, ctx
.len
- ctx
.ofs
);
1028 if (last
< ctx
.last_max
) /* ctx.line has been shortened, erase */
1030 WCEL_WriteNChars(&ctx
, ' ', ctx
.last_max
- last
);
1031 WCEL_WriteNChars(&ctx
, '\b', ctx
.last_max
- last
);
1032 ctx
.last_max
= last
;
1034 else ctx
.last_max
= last
;
1035 /* reposition at cursor */
1036 WCEL_WriteNChars(&ctx
, '\b', last
- ctx
.last_rub
);
1041 HeapFree(GetProcessHeap(), 0, ctx
.line
);
1044 WCEL_FreeYank(&ctx
);
1046 CONSOLE_AppendHistory(ctx
.line
);
1048 CloseHandle(ctx
.hConOut
);
1049 HeapFree(GetProcessHeap(), 0, ctx
.histCurr
);