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"
31 #include "wine/unicode.h"
33 #include "wine/debug.h"
34 #include "console_private.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(console
);
42 WCHAR val
; /* vk or unicode char */
43 void (*func
)(struct WCEL_Context
* ctx
);
48 DWORD keyState
; /* keyState (from INPUT_RECORD) to match */
49 BOOL chkChar
; /* check vk or char */
50 const KeyEntry
* entries
; /* array of entries */
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) */
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 */
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"
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
));
94 /* ====================================================================
96 * Console helper functions
98 * ====================================================================*/
100 static BOOL
WCEL_Get(WCEL_Context
* ctx
, INPUT_RECORD
* ir
)
104 if (ReadConsoleInputW(ctx
->hConIn
, ir
, 1, &num_read
)) return TRUE
;
109 static inline void WCEL_Beep(WCEL_Context
* ctx
)
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
)
128 for (i
= 0, ofs
= 0; i
< len
; i
++)
129 ofs
+= WCEL_CharWidth(str
[beg
+ i
]);
133 static inline COORD
WCEL_GetCoord(WCEL_Context
* ctx
, int strofs
)
136 int len
= ctx
->csbi
.dwSize
.X
- ctx
->csbi
.dwCursorPosition
.X
;
139 ofs
= WCEL_StringWidth(ctx
->line
, 0, strofs
);
141 c
.Y
= ctx
->csbi
.dwCursorPosition
.Y
;
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
;
152 static DWORD
WCEL_WriteConsole(WCEL_Context
* ctx
, DWORD beg
, DWORD len
)
154 DWORD i
, last
, dw
, ret
= 0;
157 for (i
= last
= 0; i
< len
; i
++)
159 if (ctx
->line
[beg
+ i
] < ' ')
163 WriteConsoleW(ctx
->hConOut
, &ctx
->line
[beg
+ last
], i
- last
, &dw
, NULL
);
167 tmp
[1] = '@' + ctx
->line
[beg
+ i
];
168 WriteConsoleW(ctx
->hConOut
, tmp
, 2, &dw
, NULL
);
175 WriteConsoleW(ctx
->hConOut
, &ctx
->line
[beg
+ last
], len
- last
, &dw
, NULL
);
181 static inline void WCEL_WriteNChars(WCEL_Context
* ctx
, char ch
, int count
)
187 while (count
--) WriteFile(ctx
->hConOut
, &ch
, 1, &dw
, NULL
);
191 static inline void WCEL_Update(WCEL_Context
* ctx
, int beg
, int len
)
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
] < ' ')
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
);
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
);
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");
243 if (ctx
->len
+ len
>= ctx
->alloc
)
248 /* round up size to 32 byte-WCHAR boundary */
249 newsize
= (ctx
->len
+ len
+ 1 + 31) & ~31;
252 newline
= HeapReAlloc(GetProcessHeap(), 0, ctx
->line
, sizeof(WCHAR
) * newsize
);
254 newline
= HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR
) * newsize
);
256 if (!newline
) return FALSE
;
258 ctx
->alloc
= newsize
;
263 static void WCEL_DeleteString(WCEL_Context
* ctx
, int beg
, int end
)
265 unsigned str_len
= end
- beg
;
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 */
273 COORD cbeg
= WCEL_GetCoord(ctx
, ctx
->len
- str_len
);
274 COORD cend
= WCEL_GetCoord(ctx
, ctx
->len
);
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
);
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
);
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
;
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
));
315 updtlen
= ctx
->len
- ctx
->ofs
;
319 if (ctx
->ofs
+ len
> ctx
->len
)
321 if (!WCEL_Grow(ctx
, (ctx
->ofs
+ len
) - ctx
->len
)) return;
322 ctx
->len
= ctx
->ofs
+ len
;
326 memcpy(&ctx
->line
[ctx
->ofs
], str
, len
* sizeof(WCHAR
));
327 ctx
->line
[ctx
->len
] = 0;
328 WCEL_Update(ctx
, ctx
->ofs
, updtlen
);
332 static void WCEL_InsertChar(WCEL_Context
* ctx
, WCHAR c
)
338 WCEL_InsertString(ctx
, buffer
);
341 static void WCEL_FreeYank(WCEL_Context
* ctx
)
343 HeapFree(GetProcessHeap(), 0, ctx
->yanked
);
347 static void WCEL_SaveYank(WCEL_Context
* ctx
, int beg
, int end
)
350 if (len
<= 0) return;
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
)
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
)
374 while (ofs
>= 0 && !WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
375 while (ofs
>= 0 && WCEL_iswalnum(ctx
->line
[ofs
])) ofs
--;
380 static int WCEL_GetRightWordTransition(WCEL_Context
* ctx
, int 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
)
392 if (idx
== ctx
->histSize
- 1)
394 ptr
= HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx
->histCurr
) + 1) * sizeof(WCHAR
));
395 lstrcpyW(ptr
, ctx
->histCurr
);
399 int len
= CONSOLE_GetHistory(idx
, NULL
, 0);
401 if ((ptr
= HeapAlloc(GetProcessHeap(), 0, len
* sizeof(WCHAR
))))
403 CONSOLE_GetHistory(idx
, ptr
, len
);
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
);
431 /* insert new string */
432 if (WCEL_Grow(ctx
, len
))
434 WCEL_InsertString(ctx
, data
);
437 HeapFree(GetProcessHeap(), 0, data
);
440 static void WCEL_FindPrevInHist(WCEL_Context
* ctx
)
442 int startPos
= ctx
->histPos
;
444 unsigned int len
, oldofs
;
446 if (ctx
->histPos
&& ctx
->histPos
== ctx
->histSize
) {
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
))
468 WCEL_InsertString(ctx
, data
);
471 SetConsoleCursorPosition(ctx
->hConOut
, WCEL_GetCoord(ctx
, ctx
->ofs
));
472 HeapFree(GetProcessHeap(), 0, data
);
476 HeapFree(GetProcessHeap(), 0, data
);
477 } while (ctx
->histPos
!= startPos
);
482 /* ====================================================================
484 * basic edition functions
486 * ====================================================================*/
488 static void WCEL_Done(WCEL_Context
* ctx
)
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
);
497 SetConsoleCursorInfo(ctx
->hConOut
, &ctx
->cinfo
);
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
)
528 static void WCEL_MoveToEnd(WCEL_Context
* ctx
)
533 static void WCEL_SetMark(WCEL_Context
* ctx
)
535 ctx
->mark
= ctx
->ofs
;
538 static void WCEL_ExchangeMark(WCEL_Context
* ctx
)
542 if (ctx
->mark
> ctx
->len
) return;
544 ctx
->ofs
= ctx
->mark
;
548 static void WCEL_CopyMarkedZone(WCEL_Context
* ctx
)
552 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
553 if (ctx
->mark
> ctx
->ofs
)
555 beg
= ctx
->ofs
; end
= ctx
->mark
;
559 beg
= ctx
->mark
; end
= ctx
->ofs
;
561 WCEL_SaveYank(ctx
, beg
, end
);
564 static void WCEL_TransposeChar(WCEL_Context
* ctx
)
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);
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
));
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);
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);
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);
634 static void WCEL_Yank(WCEL_Context
* ctx
)
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
)
650 WCEL_SaveYank(ctx
, 0, ctx
->ofs
);
651 WCEL_DeleteString(ctx
, 0, ctx
->ofs
);
656 static void WCEL_KillMarkedZone(WCEL_Context
* ctx
)
660 if (ctx
->mark
> ctx
->len
|| ctx
->mark
== ctx
->ofs
) return;
661 if (ctx
->mark
> ctx
->ofs
)
663 beg
= ctx
->ofs
; end
= ctx
->mark
;
667 beg
= ctx
->mark
; end
= ctx
->ofs
;
669 WCEL_SaveYank(ctx
, beg
, end
);
670 WCEL_DeleteString(ctx
, beg
, end
);
674 static void WCEL_DeletePrevChar(WCEL_Context
* ctx
)
678 WCEL_DeleteString(ctx
, ctx
->ofs
- 1, 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
);
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
)
732 COORD c
= WCEL_GetCoord(ctx
, ctx
->len
);
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
)
747 /* FIXME: wait until all console code is in kernel32 */
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)
758 if (ir
.Event
.KeyEvent
.uChar
.UnicodeChar
< '0' ||
759 ir
.Event
.KeyEvent
.uChar
.UnicodeChar
> '9')
761 repeat
= repeat
* 10 + ir
.Event
.KeyEvent
.uChar
.UnicodeChar
- '0';
763 WCEL_Get(ctx
, &ir
, TRUE
);
765 FIXME("=> %u\n", repeat
);
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 /* ====================================================================
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
},
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
},
829 static const KeyEntry EmacsKeyMapAlt
[] =
831 {/*DEL*/127, WCEL_DeleteLeftWord
},
832 { '<', WCEL_MoveToFirstHist
},
833 { '>', WCEL_MoveToLastHist
},
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
},
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
},
858 static const KeyMap EmacsKeyMap
[] =
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 */
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
},
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
},
891 static const KeyMap Win32KeyMap
[] =
894 {SHIFT_PRESSED
, 0, StdKeyMap
},
895 {0, 0, Win32StdKeyMap
},
896 {RIGHT_CTRL_PRESSED
, 0, Win32KeyMapCtrl
},
897 {LEFT_CTRL_PRESSED
, 0, Win32KeyMapCtrl
},
902 /* ====================================================================
904 * Read line master function
906 * ====================================================================*/
908 WCHAR
* CONSOLE_Readline(HANDLE hConsoleIn
, BOOL can_pos_cursor
)
915 void (*func
)(struct WCEL_Context
* ctx
);
916 DWORD mode
, input_mode
, ks
;
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
))
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
))
931 if (!GetConsoleMode(hConsoleIn
, &mode
)) mode
= 0;
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
);
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"); */
960 /* mask out some bits which don't interest us */
961 ks
= ir
.Event
.KeyEvent
.dwControlKeyState
& ~(NUMLOCK_ON
|SCROLLLOCK_ON
|CAPSLOCK_ON
|ENHANCED_KEY
);
964 for (km
= (use_emacs
) ? EmacsKeyMap
: Win32KeyMap
; km
->entries
!= NULL
; km
++)
966 if (km
->keyState
!= ks
)
970 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
971 if (ke
->val
== ir
.Event
.KeyEvent
.uChar
.UnicodeChar
) break;
975 for (ke
= &km
->entries
[0]; ke
->func
!= 0; ke
++)
976 if (ke
->val
== ir
.Event
.KeyEvent
.wVirtualKeyCode
) break;
986 CONSOLE_GetEditionMode(hConsoleIn
, &use_emacs
);
988 GetConsoleMode(hConsoleIn
, &mode
);
989 if (input_mode
!= mode
)
994 ctx
.insert
= (mode
& (ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
)) ==
995 (ENABLE_INSERT_MODE
|ENABLE_EXTENDED_FLAGS
);
997 ctx
.insert
= !ctx
.insert
;
999 GetConsoleScreenBufferInfo(ctx
.hConOut
, &csbi
);
1000 ctx
.csbi
.wAttributes
= csbi
.wAttributes
;
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
)
1013 SetConsoleCursorPosition(ctx
.hConOut
, WCEL_GetCoord(&ctx
, ctx
.ofs
));
1015 else if (!ctx
.done
&& !ctx
.error
)
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
);
1038 HeapFree(GetProcessHeap(), 0, ctx
.line
);
1041 WCEL_FreeYank(&ctx
);
1043 CONSOLE_AppendHistory(ctx
.line
);
1045 CloseHandle(ctx
.hConOut
);
1046 HeapFree(GetProcessHeap(), 0, ctx
.histCurr
);