Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 1 | /* |
| 2 | * line edition function for Win32 console |
| 3 | * |
| 4 | * Copyright 2001 Eric Pouech |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 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 |
Jonathan Ernst | 360a3f9 | 2006-05-18 14:49:52 +0200 | [diff] [blame] | 18 | * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 19 | */ |
| 20 | |
| 21 | #include "config.h" |
Alexandre Julliard | 5769d1d | 2002-04-26 19:05:15 +0000 | [diff] [blame] | 22 | #include "wine/port.h" |
| 23 | |
Alexandre Julliard | e37c6e1 | 2003-09-05 23:08:26 +0000 | [diff] [blame] | 24 | #include <stdarg.h> |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 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" |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 32 | #include "wine/debug.h" |
Eric Pouech | d7d9836 | 2002-09-04 18:41:03 +0000 | [diff] [blame] | 33 | #include "console_private.h" |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 34 | |
Alexandre Julliard | 0799c1a | 2002-03-09 23:29:33 +0000 | [diff] [blame] | 35 | WINE_DEFAULT_DEBUG_CHANNEL(console); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 36 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 37 | struct WCEL_Context; |
| 38 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 39 | typedef struct |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 40 | { |
| 41 | WCHAR val; /* vk or unicode char */ |
| 42 | void (*func)(struct WCEL_Context* ctx); |
| 43 | } KeyEntry; |
| 44 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 45 | typedef struct |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 46 | { |
| 47 | DWORD keyState; /* keyState (from INPUT_RECORD) to match */ |
| 48 | BOOL chkChar; /* check vk or char */ |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 49 | const KeyEntry* entries; /* array of entries */ |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 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 ofs; /* offset for cursor in current line */ |
| 57 | WCHAR* yanked; /* yanked line */ |
| 58 | unsigned mark; /* marked point (emacs mode only) */ |
| 59 | CONSOLE_SCREEN_BUFFER_INFO csbi; /* current state (initial cursor, window size, attribute) */ |
| 60 | HANDLE hConIn; |
| 61 | HANDLE hConOut; |
| 62 | unsigned done : 1, /* to 1 when we're done with editing */ |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 63 | error : 1, /* to 1 when an error occurred in the editing */ |
| 64 | can_wrap : 1; /* to 1 when multi-line edition can take place */ |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 65 | unsigned histSize; |
| 66 | unsigned histPos; |
| 67 | WCHAR* histCurr; |
| 68 | } WCEL_Context; |
| 69 | |
| 70 | #if 0 |
| 71 | static void WCEL_Dump(WCEL_Context* ctx, const char* pfx) |
| 72 | { |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 73 | MESSAGE("%s: [line=%s[alloc=%u] ofs=%u len=%u start=(%d,%d) mask=%c%c%c]\n" |
| 74 | "\t\thist=(size=%u pos=%u curr=%s)\n" |
| 75 | "\t\tyanked=%s\n", |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 76 | pfx, debugstr_w(ctx->line), ctx->alloc, ctx->ofs, ctx->len, |
| 77 | ctx->csbi.dwCursorPosition.X, ctx->csbi.dwCursorPosition.Y, |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 78 | ctx->done ? 'D' : 'd', ctx->error ? 'E' : 'e', ctx->can_wrap ? 'W' : 'w', |
| 79 | ctx->histSize, ctx->histPos, debugstr_w(ctx->histCurr), |
| 80 | debugstr_w(ctx->yanked)); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 81 | } |
| 82 | #endif |
| 83 | |
| 84 | /* ==================================================================== |
| 85 | * |
| 86 | * Console helper functions |
| 87 | * |
| 88 | * ====================================================================*/ |
| 89 | |
| 90 | static BOOL WCEL_Get(WCEL_Context* ctx, INPUT_RECORD* ir) |
| 91 | { |
Eric Pouech | 412d37f | 2003-06-21 02:07:10 +0000 | [diff] [blame] | 92 | if (ReadConsoleInputW(ctx->hConIn, ir, 1, NULL)) return TRUE; |
| 93 | ERR("hmm bad situation\n"); |
| 94 | ctx->error = 1; |
| 95 | return FALSE; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static inline void WCEL_Beep(WCEL_Context* ctx) |
| 99 | { |
| 100 | Beep(400, 300); |
| 101 | } |
| 102 | |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 103 | static inline BOOL WCEL_IsSingleLine(WCEL_Context* ctx, size_t len) |
| 104 | { |
| 105 | return ctx->csbi.dwCursorPosition.X + ctx->len + len <= ctx->csbi.dwSize.X; |
| 106 | } |
| 107 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 108 | static inline COORD WCEL_GetCoord(WCEL_Context* ctx, int ofs) |
| 109 | { |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 110 | COORD c; |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 111 | int len = ctx->csbi.dwSize.X - ctx->csbi.dwCursorPosition.X; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 112 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 113 | c.Y = ctx->csbi.dwCursorPosition.Y; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 114 | if (ofs >= len) |
| 115 | { |
| 116 | ofs -= len; |
| 117 | c.X = ofs % ctx->csbi.dwSize.X; |
| 118 | c.Y += 1 + ofs / ctx->csbi.dwSize.X; |
| 119 | } |
| 120 | else c.X = ctx->csbi.dwCursorPosition.X + ofs; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 121 | return c; |
| 122 | } |
| 123 | |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 124 | static inline void WCEL_Update(WCEL_Context* ctx, int beg, int len) |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 125 | { |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 126 | WriteConsoleOutputCharacterW(ctx->hConOut, &ctx->line[beg], len, |
| 127 | WCEL_GetCoord(ctx, beg), NULL); |
Jukka Heinonen | 1868b02 | 2003-02-19 03:42:06 +0000 | [diff] [blame] | 128 | FillConsoleOutputAttribute(ctx->hConOut, ctx->csbi.wAttributes, len, |
| 129 | WCEL_GetCoord(ctx, beg), NULL); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | /* ==================================================================== |
| 133 | * |
| 134 | * context manipulation functions |
| 135 | * |
| 136 | * ====================================================================*/ |
| 137 | |
| 138 | static BOOL WCEL_Grow(WCEL_Context* ctx, size_t len) |
| 139 | { |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 140 | if (!WCEL_IsSingleLine(ctx, len) && !ctx->can_wrap) |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 141 | { |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 142 | FIXME("Mode doesn't allow to wrap. However, we should allow to overwrite current string\n"); |
| 143 | return FALSE; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 144 | } |
| 145 | |
| 146 | if (ctx->len + len >= ctx->alloc) |
| 147 | { |
| 148 | WCHAR* newline; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 149 | size_t newsize; |
| 150 | |
| 151 | /* round up size to 32 byte-WCHAR boundary */ |
| 152 | newsize = (ctx->len + len + 1 + 31) & ~31; |
Oleg Prokhorov | de12a97 | 2003-10-14 05:24:20 +0000 | [diff] [blame] | 153 | |
| 154 | if (ctx->line) |
| 155 | newline = HeapReAlloc(GetProcessHeap(), 0, ctx->line, sizeof(WCHAR) * newsize); |
| 156 | else |
| 157 | newline = HeapAlloc(GetProcessHeap(), 0, sizeof(WCHAR) * newsize); |
| 158 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 159 | if (!newline) return FALSE; |
| 160 | ctx->line = newline; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 161 | ctx->alloc = newsize; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 162 | } |
| 163 | return TRUE; |
| 164 | } |
| 165 | |
| 166 | static void WCEL_DeleteString(WCEL_Context* ctx, int beg, int end) |
| 167 | { |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 168 | unsigned str_len = end - beg; |
| 169 | COORD cbeg = WCEL_GetCoord(ctx, ctx->len - str_len); |
| 170 | COORD cend = WCEL_GetCoord(ctx, ctx->len); |
| 171 | CHAR_INFO ci; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 172 | |
| 173 | if (end < ctx->len) |
| 174 | memmove(&ctx->line[beg], &ctx->line[end], (ctx->len - end) * sizeof(WCHAR)); |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 175 | /* we need to clean from ctx->len - str_len to ctx->len */ |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 176 | |
| 177 | ci.Char.UnicodeChar = ' '; |
| 178 | ci.Attributes = ctx->csbi.wAttributes; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 179 | |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 180 | if (cbeg.Y == cend.Y) |
| 181 | { |
| 182 | /* partial erase of sole line */ |
| 183 | CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y, |
| 184 | cend.X - cbeg.X, &ci); |
| 185 | } |
| 186 | else |
| 187 | { |
| 188 | int i; |
| 189 | /* erase til eol on first line */ |
| 190 | CONSOLE_FillLineUniform(ctx->hConOut, cbeg.X, cbeg.Y, |
| 191 | ctx->csbi.dwSize.X - cbeg.X, &ci); |
Francois Gouget | 17112ea | 2004-01-16 21:26:08 +0000 | [diff] [blame] | 192 | /* completely erase all the others (full lines) */ |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 193 | for (i = cbeg.Y + 1; i < cend.Y; i++) |
| 194 | CONSOLE_FillLineUniform(ctx->hConOut, 0, i, ctx->csbi.dwSize.X, &ci); |
Jon Griffiths | eb5bf7d | 2005-02-25 14:07:56 +0000 | [diff] [blame] | 195 | /* erase from beginning of line until last pos on last line */ |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 196 | CONSOLE_FillLineUniform(ctx->hConOut, 0, cend.Y, cend.X, &ci); |
| 197 | } |
| 198 | ctx->len -= str_len; |
| 199 | WCEL_Update(ctx, 0, ctx->len); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 200 | ctx->line[ctx->len] = 0; |
| 201 | } |
| 202 | |
| 203 | static void WCEL_InsertString(WCEL_Context* ctx, const WCHAR* str) |
| 204 | { |
| 205 | size_t len = lstrlenW(str); |
| 206 | |
| 207 | if (!len || !WCEL_Grow(ctx, len)) return; |
| 208 | if (ctx->len > ctx->ofs) |
| 209 | memmove(&ctx->line[ctx->ofs + len], &ctx->line[ctx->ofs], (ctx->len - ctx->ofs) * sizeof(WCHAR)); |
| 210 | memcpy(&ctx->line[ctx->ofs], str, len * sizeof(WCHAR)); |
| 211 | ctx->len += len; |
| 212 | ctx->line[ctx->len] = 0; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 213 | WCEL_Update(ctx, ctx->ofs, ctx->len - ctx->ofs); |
| 214 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 215 | ctx->ofs += len; |
| 216 | } |
| 217 | |
| 218 | static void WCEL_InsertChar(WCEL_Context* ctx, WCHAR c) |
| 219 | { |
| 220 | WCHAR buffer[2]; |
| 221 | |
| 222 | /* do not insert 0..31 control characters */ |
Eric Pouech | 1d86056 | 2002-06-02 21:19:28 +0000 | [diff] [blame] | 223 | if (c < ' ' && c != '\t') return; |
| 224 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 225 | buffer[0] = c; |
| 226 | buffer[1] = 0; |
| 227 | WCEL_InsertString(ctx, buffer); |
| 228 | } |
| 229 | |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 230 | static void WCEL_FreeYank(WCEL_Context* ctx) |
| 231 | { |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 232 | HeapFree(GetProcessHeap(), 0, ctx->yanked); |
| 233 | ctx->yanked = NULL; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 236 | static void WCEL_SaveYank(WCEL_Context* ctx, int beg, int end) |
| 237 | { |
| 238 | int len = end - beg; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 239 | if (len <= 0) return; |
| 240 | |
| 241 | WCEL_FreeYank(ctx); |
Oleg Prokhorov | de12a97 | 2003-10-14 05:24:20 +0000 | [diff] [blame] | 242 | /* After WCEL_FreeYank ctx->yanked is empty */ |
| 243 | ctx->yanked = HeapAlloc(GetProcessHeap(), 0, (len + 1) * sizeof(WCHAR)); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 244 | if (!ctx->yanked) return; |
| 245 | memcpy(ctx->yanked, &ctx->line[beg], len * sizeof(WCHAR)); |
| 246 | ctx->yanked[len] = 0; |
| 247 | } |
| 248 | |
| 249 | /* FIXME NTDLL doesn't export iswalnum, and I don't want to link in msvcrt when most |
| 250 | * of the data lay in unicode lib |
| 251 | */ |
| 252 | static inline BOOL WCEL_iswalnum(WCHAR wc) |
| 253 | { |
| 254 | return get_char_typeW(wc) & (C1_ALPHA|C1_DIGIT|C1_LOWER|C1_UPPER); |
| 255 | } |
| 256 | |
| 257 | static int WCEL_GetLeftWordTransition(WCEL_Context* ctx, int ofs) |
| 258 | { |
| 259 | ofs--; |
| 260 | while (ofs >= 0 && !WCEL_iswalnum(ctx->line[ofs])) ofs--; |
| 261 | while (ofs >= 0 && WCEL_iswalnum(ctx->line[ofs])) ofs--; |
| 262 | if (ofs >= 0) ofs++; |
| 263 | return max(ofs, 0); |
| 264 | } |
| 265 | |
| 266 | static int WCEL_GetRightWordTransition(WCEL_Context* ctx, int ofs) |
| 267 | { |
| 268 | ofs++; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 269 | while (ofs <= ctx->len && WCEL_iswalnum(ctx->line[ofs])) ofs++; |
Jason Edmeades | b7b75a8 | 2002-04-30 21:17:34 +0000 | [diff] [blame] | 270 | while (ofs <= ctx->len && !WCEL_iswalnum(ctx->line[ofs])) ofs++; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 271 | return min(ofs, ctx->len); |
| 272 | } |
| 273 | |
| 274 | static WCHAR* WCEL_GetHistory(WCEL_Context* ctx, int idx) |
| 275 | { |
| 276 | WCHAR* ptr; |
| 277 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 278 | if (idx == ctx->histSize - 1) |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 279 | { |
| 280 | ptr = HeapAlloc(GetProcessHeap(), 0, (lstrlenW(ctx->histCurr) + 1) * sizeof(WCHAR)); |
| 281 | lstrcpyW(ptr, ctx->histCurr); |
| 282 | } |
| 283 | else |
| 284 | { |
| 285 | int len = CONSOLE_GetHistory(idx, NULL, 0); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 286 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 287 | if ((ptr = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR)))) |
| 288 | { |
| 289 | CONSOLE_GetHistory(idx, ptr, len); |
| 290 | } |
| 291 | } |
| 292 | return ptr; |
| 293 | } |
| 294 | |
| 295 | static void WCEL_HistoryInit(WCEL_Context* ctx) |
| 296 | { |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 297 | ctx->histPos = CONSOLE_GetNumHistoryEntries(); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 298 | ctx->histSize = ctx->histPos + 1; |
| 299 | ctx->histCurr = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(WCHAR)); |
| 300 | } |
| 301 | |
| 302 | static void WCEL_MoveToHist(WCEL_Context* ctx, int idx) |
| 303 | { |
| 304 | WCHAR* data = WCEL_GetHistory(ctx, idx); |
| 305 | int len = lstrlenW(data) + 1; |
| 306 | |
| 307 | /* save current line edition for recall when needed (FIXME seems broken to me) */ |
| 308 | if (ctx->histPos == ctx->histSize - 1) |
| 309 | { |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 310 | HeapFree(GetProcessHeap(), 0, ctx->histCurr); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 311 | ctx->histCurr = HeapAlloc(GetProcessHeap(), 0, (ctx->len + 1) * sizeof(WCHAR)); |
| 312 | memcpy(ctx->histCurr, ctx->line, (ctx->len + 1) * sizeof(WCHAR)); |
| 313 | } |
| 314 | /* need to clean also the screen if new string is shorter than old one */ |
| 315 | WCEL_DeleteString(ctx, 0, ctx->len); |
| 316 | ctx->ofs = 0; |
| 317 | /* insert new string */ |
| 318 | if (WCEL_Grow(ctx, len)) |
| 319 | { |
| 320 | WCEL_InsertString(ctx, data); |
| 321 | HeapFree(GetProcessHeap(), 0, data); |
| 322 | ctx->histPos = idx; |
| 323 | } |
| 324 | } |
| 325 | |
Jason Edmeades | b7b75a8 | 2002-04-30 21:17:34 +0000 | [diff] [blame] | 326 | static void WCEL_FindPrevInHist(WCEL_Context* ctx) |
| 327 | { |
| 328 | int startPos = ctx->histPos; |
| 329 | WCHAR* data; |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 330 | unsigned int len, oldofs; |
Jason Edmeades | b7b75a8 | 2002-04-30 21:17:34 +0000 | [diff] [blame] | 331 | |
| 332 | if (ctx->histPos && ctx->histPos == ctx->histSize) { |
| 333 | startPos--; |
| 334 | ctx->histPos--; |
| 335 | } |
| 336 | |
| 337 | do { |
| 338 | data = WCEL_GetHistory(ctx, ctx->histPos); |
| 339 | |
| 340 | if (ctx->histPos) ctx->histPos--; |
| 341 | else ctx->histPos = (ctx->histSize-1); |
| 342 | |
| 343 | len = lstrlenW(data) + 1; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 344 | if ((len >= ctx->ofs) && |
Jason Edmeades | b7b75a8 | 2002-04-30 21:17:34 +0000 | [diff] [blame] | 345 | (memcmp(ctx->line, data, ctx->ofs * sizeof(WCHAR)) == 0)) { |
| 346 | |
| 347 | /* need to clean also the screen if new string is shorter than old one */ |
| 348 | WCEL_DeleteString(ctx, 0, ctx->len); |
| 349 | |
| 350 | if (WCEL_Grow(ctx, len)) |
| 351 | { |
| 352 | oldofs = ctx->ofs; |
| 353 | ctx->ofs = 0; |
| 354 | WCEL_InsertString(ctx, data); |
| 355 | ctx->ofs = oldofs; |
| 356 | SetConsoleCursorPosition(ctx->hConOut, WCEL_GetCoord(ctx, ctx->ofs)); |
| 357 | HeapFree(GetProcessHeap(), 0, data); |
| 358 | return; |
| 359 | } |
| 360 | } |
| 361 | } while (ctx->histPos != startPos); |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 362 | |
Jason Edmeades | b7b75a8 | 2002-04-30 21:17:34 +0000 | [diff] [blame] | 363 | return; |
| 364 | } |
| 365 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 366 | /* ==================================================================== |
| 367 | * |
| 368 | * basic edition functions |
| 369 | * |
| 370 | * ====================================================================*/ |
| 371 | |
| 372 | static void WCEL_Done(WCEL_Context* ctx) |
| 373 | { |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 374 | WCHAR nl = '\n'; |
Joshua Davies | dcba743 | 2005-10-06 11:35:24 +0000 | [diff] [blame] | 375 | if (!WCEL_Grow(ctx, 2)) return; |
| 376 | ctx->line[ctx->len++] = '\r'; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 377 | ctx->line[ctx->len++] = '\n'; |
| 378 | ctx->line[ctx->len] = 0; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 379 | WriteConsoleW(ctx->hConOut, &nl, 1, NULL, NULL); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 380 | ctx->done = 1; |
| 381 | } |
| 382 | |
| 383 | static void WCEL_MoveLeft(WCEL_Context* ctx) |
| 384 | { |
| 385 | if (ctx->ofs > 0) ctx->ofs--; |
| 386 | } |
| 387 | |
| 388 | static void WCEL_MoveRight(WCEL_Context* ctx) |
| 389 | { |
| 390 | if (ctx->ofs < ctx->len) ctx->ofs++; |
| 391 | } |
| 392 | |
| 393 | static void WCEL_MoveToLeftWord(WCEL_Context* ctx) |
| 394 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 395 | unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 396 | if (new_ofs != ctx->ofs) ctx->ofs = new_ofs; |
| 397 | } |
| 398 | |
| 399 | static void WCEL_MoveToRightWord(WCEL_Context* ctx) |
| 400 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 401 | unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 402 | if (new_ofs != ctx->ofs) ctx->ofs = new_ofs; |
| 403 | } |
| 404 | |
| 405 | static void WCEL_MoveToBeg(WCEL_Context* ctx) |
| 406 | { |
| 407 | ctx->ofs = 0; |
| 408 | } |
| 409 | |
| 410 | static void WCEL_MoveToEnd(WCEL_Context* ctx) |
| 411 | { |
| 412 | ctx->ofs = ctx->len; |
| 413 | } |
| 414 | |
| 415 | static void WCEL_SetMark(WCEL_Context* ctx) |
| 416 | { |
| 417 | ctx->mark = ctx->ofs; |
| 418 | } |
| 419 | |
| 420 | static void WCEL_ExchangeMark(WCEL_Context* ctx) |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 421 | { |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 422 | unsigned tmp; |
| 423 | |
| 424 | if (ctx->mark > ctx->len) return; |
| 425 | tmp = ctx->ofs; |
| 426 | ctx->ofs = ctx->mark; |
| 427 | ctx->mark = tmp; |
| 428 | } |
| 429 | |
| 430 | static void WCEL_CopyMarkedZone(WCEL_Context* ctx) |
| 431 | { |
| 432 | unsigned beg, end; |
| 433 | |
| 434 | if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return; |
| 435 | if (ctx->mark > ctx->ofs) |
| 436 | { |
| 437 | beg = ctx->ofs; end = ctx->mark; |
| 438 | } |
| 439 | else |
| 440 | { |
| 441 | beg = ctx->mark; end = ctx->ofs; |
| 442 | } |
| 443 | WCEL_SaveYank(ctx, beg, end); |
| 444 | } |
| 445 | |
| 446 | static void WCEL_TransposeChar(WCEL_Context* ctx) |
| 447 | { |
| 448 | WCHAR c; |
| 449 | |
| 450 | if (!ctx->ofs || ctx->ofs == ctx->len) return; |
| 451 | |
| 452 | c = ctx->line[ctx->ofs]; |
| 453 | ctx->line[ctx->ofs] = ctx->line[ctx->ofs - 1]; |
| 454 | ctx->line[ctx->ofs - 1] = c; |
| 455 | |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 456 | WCEL_Update(ctx, ctx->ofs - 1, 2); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 457 | ctx->ofs++; |
| 458 | } |
| 459 | |
| 460 | static void WCEL_TransposeWords(WCEL_Context* ctx) |
| 461 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 462 | unsigned int left_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs), |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 463 | right_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs); |
| 464 | if (left_ofs < ctx->ofs && right_ofs > ctx->ofs) |
| 465 | { |
| 466 | unsigned len_r = right_ofs - ctx->ofs; |
| 467 | unsigned len_l = ctx->ofs - left_ofs; |
| 468 | |
| 469 | char* tmp = HeapAlloc(GetProcessHeap(), 0, len_r * sizeof(WCHAR)); |
| 470 | if (!tmp) return; |
| 471 | |
| 472 | memcpy(tmp, &ctx->line[ctx->ofs], len_r * sizeof(WCHAR)); |
| 473 | memmove(&ctx->line[left_ofs + len_r], &ctx->line[left_ofs], len_l * sizeof(WCHAR)); |
| 474 | memcpy(&ctx->line[left_ofs], tmp, len_r * sizeof(WCHAR)); |
| 475 | |
| 476 | HeapFree(GetProcessHeap(), 0, tmp); |
| 477 | WCEL_Update(ctx, left_ofs, len_l + len_r); |
| 478 | ctx->ofs = right_ofs; |
| 479 | } |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | static void WCEL_LowerCaseWord(WCEL_Context* ctx) |
| 483 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 484 | unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 485 | if (new_ofs != ctx->ofs) |
| 486 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 487 | unsigned int i; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 488 | for (i = ctx->ofs; i <= new_ofs; i++) |
| 489 | ctx->line[i] = tolowerW(ctx->line[i]); |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 490 | WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 491 | ctx->ofs = new_ofs; |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | static void WCEL_UpperCaseWord(WCEL_Context* ctx) |
| 496 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 497 | unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 498 | if (new_ofs != ctx->ofs) |
| 499 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 500 | unsigned int i; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 501 | for (i = ctx->ofs; i <= new_ofs; i++) |
| 502 | ctx->line[i] = toupperW(ctx->line[i]); |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 503 | WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 504 | ctx->ofs = new_ofs; |
| 505 | } |
| 506 | } |
| 507 | |
| 508 | static void WCEL_CapitalizeWord(WCEL_Context* ctx) |
| 509 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 510 | unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 511 | if (new_ofs != ctx->ofs) |
| 512 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 513 | unsigned int i; |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 514 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 515 | ctx->line[ctx->ofs] = toupperW(ctx->line[ctx->ofs]); |
| 516 | for (i = ctx->ofs + 1; i <= new_ofs; i++) |
| 517 | ctx->line[i] = tolowerW(ctx->line[i]); |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 518 | WCEL_Update(ctx, ctx->ofs, new_ofs - ctx->ofs + 1); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 519 | ctx->ofs = new_ofs; |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | static void WCEL_Yank(WCEL_Context* ctx) |
| 524 | { |
| 525 | WCEL_InsertString(ctx, ctx->yanked); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | static void WCEL_KillToEndOfLine(WCEL_Context* ctx) |
| 529 | { |
| 530 | WCEL_SaveYank(ctx, ctx->ofs, ctx->len); |
| 531 | WCEL_DeleteString(ctx, ctx->ofs, ctx->len); |
| 532 | } |
| 533 | |
| 534 | static void WCEL_KillMarkedZone(WCEL_Context* ctx) |
| 535 | { |
| 536 | unsigned beg, end; |
| 537 | |
| 538 | if (ctx->mark > ctx->len || ctx->mark == ctx->ofs) return; |
| 539 | if (ctx->mark > ctx->ofs) |
| 540 | { |
| 541 | beg = ctx->ofs; end = ctx->mark; |
| 542 | } |
| 543 | else |
| 544 | { |
| 545 | beg = ctx->mark; end = ctx->ofs; |
| 546 | } |
| 547 | WCEL_SaveYank(ctx, beg, end); |
| 548 | WCEL_DeleteString(ctx, beg, end); |
| 549 | ctx->ofs = beg; |
| 550 | } |
| 551 | |
| 552 | static void WCEL_DeletePrevChar(WCEL_Context* ctx) |
| 553 | { |
| 554 | if (ctx->ofs) |
| 555 | { |
| 556 | WCEL_DeleteString(ctx, ctx->ofs - 1, ctx->ofs); |
| 557 | ctx->ofs--; |
| 558 | } |
| 559 | } |
| 560 | |
| 561 | static void WCEL_DeleteCurrChar(WCEL_Context* ctx) |
| 562 | { |
| 563 | if (ctx->ofs < ctx->len) |
| 564 | WCEL_DeleteString(ctx, ctx->ofs, ctx->ofs + 1); |
| 565 | } |
| 566 | |
| 567 | static void WCEL_DeleteLeftWord(WCEL_Context* ctx) |
| 568 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 569 | unsigned int new_ofs = WCEL_GetLeftWordTransition(ctx, ctx->ofs); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 570 | if (new_ofs != ctx->ofs) |
| 571 | { |
| 572 | WCEL_DeleteString(ctx, new_ofs, ctx->ofs); |
| 573 | ctx->ofs = new_ofs; |
| 574 | } |
| 575 | } |
| 576 | |
| 577 | static void WCEL_DeleteRightWord(WCEL_Context* ctx) |
| 578 | { |
Hans Leidekker | 9baafec | 2004-08-10 23:43:21 +0000 | [diff] [blame] | 579 | unsigned int new_ofs = WCEL_GetRightWordTransition(ctx, ctx->ofs); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 580 | if (new_ofs != ctx->ofs) |
| 581 | { |
| 582 | WCEL_DeleteString(ctx, ctx->ofs, new_ofs); |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | static void WCEL_MoveToPrevHist(WCEL_Context* ctx) |
| 587 | { |
| 588 | if (ctx->histPos) WCEL_MoveToHist(ctx, ctx->histPos - 1); |
| 589 | } |
| 590 | |
| 591 | static void WCEL_MoveToNextHist(WCEL_Context* ctx) |
| 592 | { |
| 593 | if (ctx->histPos < ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histPos + 1); |
| 594 | } |
| 595 | |
| 596 | static void WCEL_MoveToFirstHist(WCEL_Context* ctx) |
| 597 | { |
| 598 | if (ctx->histPos != 0) WCEL_MoveToHist(ctx, 0); |
| 599 | } |
| 600 | |
| 601 | static void WCEL_MoveToLastHist(WCEL_Context* ctx) |
| 602 | { |
| 603 | if (ctx->histPos != ctx->histSize - 1) WCEL_MoveToHist(ctx, ctx->histSize - 1); |
| 604 | } |
| 605 | |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 606 | static void WCEL_Redraw(WCEL_Context* ctx) |
| 607 | { |
| 608 | COORD c = WCEL_GetCoord(ctx, ctx->len); |
| 609 | CHAR_INFO ci; |
| 610 | |
| 611 | WCEL_Update(ctx, 0, ctx->len); |
| 612 | |
| 613 | ci.Char.UnicodeChar = ' '; |
| 614 | ci.Attributes = ctx->csbi.wAttributes; |
| 615 | |
| 616 | CONSOLE_FillLineUniform(ctx->hConOut, c.X, c.Y, ctx->csbi.dwSize.X - c.X, &ci); |
| 617 | } |
| 618 | |
| 619 | static void WCEL_RepeatCount(WCEL_Context* ctx) |
| 620 | { |
| 621 | #if 0 |
Eric Pouech | 412d37f | 2003-06-21 02:07:10 +0000 | [diff] [blame] | 622 | /* FIXME: wait until all console code is in kernel32 */ |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 623 | INPUT_RECORD ir; |
| 624 | unsigned repeat = 0; |
| 625 | |
| 626 | while (WCEL_Get(ctx, &ir, FALSE)) |
| 627 | { |
| 628 | if (ir.EventType != KEY_EVENT) break; |
| 629 | if (ir.Event.KeyEvent.bKeyDown) |
| 630 | { |
| 631 | if ((ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON)) != 0) |
| 632 | break; |
| 633 | if (ir.Event.KeyEvent.uChar.UnicodeChar < '0' || |
| 634 | ir.Event.KeyEvent.uChar.UnicodeChar > '9') |
| 635 | break; |
| 636 | repeat = repeat * 10 + ir.Event.KeyEvent.uChar.UnicodeChar - '0'; |
| 637 | } |
| 638 | WCEL_Get(ctx, &ir, TRUE); |
| 639 | } |
| 640 | FIXME("=> %u\n", repeat); |
| 641 | #endif |
| 642 | } |
| 643 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 644 | /* ==================================================================== |
| 645 | * |
| 646 | * Key Maps |
| 647 | * |
| 648 | * ====================================================================*/ |
| 649 | |
| 650 | #define CTRL(x) ((x) - '@') |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 651 | static const KeyEntry StdKeyMap[] = |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 652 | { |
| 653 | {/*BACK*/0x08, WCEL_DeletePrevChar }, |
| 654 | {/*RETURN*/0x0d, WCEL_Done }, |
| 655 | {/*DEL*/127, WCEL_DeleteCurrChar }, |
| 656 | { 0, NULL } |
| 657 | }; |
| 658 | |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 659 | static const KeyEntry Win32ExtraStdKeyMap[] = |
Jason Edmeades | b7b75a8 | 2002-04-30 21:17:34 +0000 | [diff] [blame] | 660 | { |
| 661 | {/*VK_F8*/ 0x77, WCEL_FindPrevInHist }, |
| 662 | { 0, NULL } |
| 663 | }; |
| 664 | |
| 665 | |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 666 | static const KeyEntry EmacsKeyMapCtrl[] = |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 667 | { |
| 668 | { CTRL('@'), WCEL_SetMark }, |
| 669 | { CTRL('A'), WCEL_MoveToBeg }, |
| 670 | { CTRL('B'), WCEL_MoveLeft }, |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 671 | /* C: done in server */ |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 672 | { CTRL('D'), WCEL_DeleteCurrChar }, |
| 673 | { CTRL('E'), WCEL_MoveToEnd }, |
| 674 | { CTRL('F'), WCEL_MoveRight }, |
| 675 | { CTRL('G'), WCEL_Beep }, |
| 676 | { CTRL('H'), WCEL_DeletePrevChar }, |
| 677 | /* I: meaningless (or tab ???) */ |
| 678 | { CTRL('J'), WCEL_Done }, |
| 679 | { CTRL('K'), WCEL_KillToEndOfLine }, |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 680 | { CTRL('L'), WCEL_Redraw }, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 681 | { CTRL('M'), WCEL_Done }, |
| 682 | { CTRL('N'), WCEL_MoveToNextHist }, |
| 683 | /* O; insert line... meaningless */ |
| 684 | { CTRL('P'), WCEL_MoveToPrevHist }, |
| 685 | /* Q: [NIY] quoting... */ |
| 686 | /* R: [NIY] search backwards... */ |
| 687 | /* S: [NIY] search forwards... */ |
| 688 | { CTRL('T'), WCEL_TransposeChar }, |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 689 | { CTRL('U'), WCEL_RepeatCount }, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 690 | /* V: paragraph down... meaningless */ |
| 691 | { CTRL('W'), WCEL_KillMarkedZone }, |
| 692 | { CTRL('X'), WCEL_ExchangeMark }, |
| 693 | { CTRL('Y'), WCEL_Yank }, |
| 694 | /* Z: meaningless */ |
| 695 | { 0, NULL } |
| 696 | }; |
| 697 | |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 698 | static const KeyEntry EmacsKeyMapAlt[] = |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 699 | { |
| 700 | {/*DEL*/127, WCEL_DeleteLeftWord }, |
| 701 | { '<', WCEL_MoveToFirstHist }, |
| 702 | { '>', WCEL_MoveToLastHist }, |
| 703 | { '?', WCEL_Beep }, |
| 704 | { 'b', WCEL_MoveToLeftWord }, |
| 705 | { 'c', WCEL_CapitalizeWord }, |
| 706 | { 'd', WCEL_DeleteRightWord }, |
| 707 | { 'f', WCEL_MoveToRightWord }, |
| 708 | { 'l', WCEL_LowerCaseWord }, |
| 709 | { 't', WCEL_TransposeWords }, |
| 710 | { 'u', WCEL_UpperCaseWord }, |
| 711 | { 'w', WCEL_CopyMarkedZone }, |
| 712 | { 0, NULL } |
| 713 | }; |
| 714 | |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 715 | static const KeyEntry EmacsKeyMapExtended[] = |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 716 | { |
Dmitry Timoshkov | dac9dfc | 2002-02-12 18:45:46 +0000 | [diff] [blame] | 717 | {/*RETURN*/ 0x0d, WCEL_Done }, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 718 | {/*VK_PRIOR*/0x21, WCEL_MoveToPrevHist }, |
Eric Pouech | 1d86056 | 2002-06-02 21:19:28 +0000 | [diff] [blame] | 719 | {/*VK_NEXT*/ 0x22, WCEL_MoveToNextHist }, |
Dmitry Timoshkov | dac9dfc | 2002-02-12 18:45:46 +0000 | [diff] [blame] | 720 | {/*VK_END*/ 0x23, WCEL_MoveToEnd }, |
| 721 | {/*VK_HOME*/ 0x24, WCEL_MoveToBeg }, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 722 | {/*VK_RIGHT*/0x27, WCEL_MoveRight }, |
Eric Pouech | 1d86056 | 2002-06-02 21:19:28 +0000 | [diff] [blame] | 723 | {/*VK_LEFT*/ 0x25, WCEL_MoveLeft }, |
| 724 | {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar }, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 725 | { 0, NULL } |
| 726 | }; |
| 727 | |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 728 | static const KeyMap EmacsKeyMap[] = |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 729 | { |
| 730 | {0x00000000, 1, StdKeyMap}, |
| 731 | {0x00000001, 1, EmacsKeyMapAlt}, /* left alt */ |
| 732 | {0x00000002, 1, EmacsKeyMapAlt}, /* right alt */ |
| 733 | {0x00000004, 1, EmacsKeyMapCtrl}, /* left ctrl */ |
| 734 | {0x00000008, 1, EmacsKeyMapCtrl}, /* right ctrl */ |
| 735 | {0x00000100, 0, EmacsKeyMapExtended}, |
| 736 | {0, 0, 0} |
| 737 | }; |
| 738 | |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 739 | static const KeyEntry Win32KeyMapExtended[] = |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 740 | { |
| 741 | {/*VK_LEFT*/ 0x25, WCEL_MoveLeft }, |
| 742 | {/*VK_RIGHT*/0x27, WCEL_MoveRight }, |
| 743 | {/*VK_HOME*/ 0x24, WCEL_MoveToBeg }, |
| 744 | {/*VK_END*/ 0x23, WCEL_MoveToEnd }, |
| 745 | {/*VK_UP*/ 0x26, WCEL_MoveToPrevHist }, |
| 746 | {/*VK_DOWN*/ 0x28, WCEL_MoveToNextHist }, |
Jason Edmeades | e6bdef2 | 2002-05-01 18:04:36 +0000 | [diff] [blame] | 747 | {/*VK_DEL*/ 0x2e, WCEL_DeleteCurrChar }, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 748 | { 0, NULL } |
| 749 | }; |
| 750 | |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 751 | static const KeyEntry Win32KeyMapCtrlExtended[] = |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 752 | { |
| 753 | {/*VK_LEFT*/ 0x25, WCEL_MoveToLeftWord }, |
| 754 | {/*VK_RIGHT*/0x27, WCEL_MoveToRightWord }, |
Jason Edmeades | b7b75a8 | 2002-04-30 21:17:34 +0000 | [diff] [blame] | 755 | {/*VK_END*/ 0x23, WCEL_KillToEndOfLine }, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 756 | { 0, NULL } |
| 757 | }; |
| 758 | |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 759 | static const KeyMap Win32KeyMap[] = |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 760 | { |
| 761 | {0x00000000, 1, StdKeyMap}, |
Jason Edmeades | b7b75a8 | 2002-04-30 21:17:34 +0000 | [diff] [blame] | 762 | {0x00000000, 0, Win32ExtraStdKeyMap}, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 763 | {0x00000100, 0, Win32KeyMapExtended}, |
| 764 | {0x00000104, 0, Win32KeyMapCtrlExtended}, |
| 765 | {0x00000108, 0, Win32KeyMapCtrlExtended}, |
| 766 | {0, 0, 0} |
| 767 | }; |
| 768 | #undef CTRL |
| 769 | |
| 770 | /* ==================================================================== |
| 771 | * |
| 772 | * Read line master function |
| 773 | * |
| 774 | * ====================================================================*/ |
| 775 | |
Eric Pouech | fa8b85a | 2003-01-09 06:01:32 +0000 | [diff] [blame] | 776 | WCHAR* CONSOLE_Readline(HANDLE hConsoleIn) |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 777 | { |
| 778 | WCEL_Context ctx; |
| 779 | INPUT_RECORD ir; |
Alexandre Julliard | ae1f698 | 2006-08-02 13:32:05 +0200 | [diff] [blame] | 780 | const KeyMap* km; |
| 781 | const KeyEntry* ke; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 782 | unsigned ofs; |
| 783 | void (*func)(struct WCEL_Context* ctx); |
Eric Pouech | 824a7f1 | 2002-01-10 18:20:51 +0000 | [diff] [blame] | 784 | DWORD ks; |
Eric Pouech | fa8b85a | 2003-01-09 06:01:32 +0000 | [diff] [blame] | 785 | int use_emacs; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 786 | |
| 787 | memset(&ctx, 0, sizeof(ctx)); |
| 788 | ctx.hConIn = hConsoleIn; |
| 789 | WCEL_HistoryInit(&ctx); |
Eric Pouech | fa8b85a | 2003-01-09 06:01:32 +0000 | [diff] [blame] | 790 | |
| 791 | if (!CONSOLE_GetEditionMode(hConsoleIn, &use_emacs)) |
| 792 | use_emacs = 0; |
| 793 | |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 794 | if ((ctx.hConOut = CreateFileA("CONOUT$", GENERIC_READ|GENERIC_WRITE, 0, NULL, |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 795 | OPEN_EXISTING, 0, 0 )) == INVALID_HANDLE_VALUE || |
| 796 | !GetConsoleScreenBufferInfo(ctx.hConOut, &ctx.csbi)) |
| 797 | return NULL; |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 798 | ctx.can_wrap = (GetConsoleMode(ctx.hConOut, &ks) && (ks & ENABLE_WRAP_AT_EOL_OUTPUT)) ? 1 : 0; |
| 799 | |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 800 | if (!WCEL_Grow(&ctx, 1)) |
| 801 | { |
| 802 | CloseHandle(ctx.hConOut); |
| 803 | return NULL; |
| 804 | } |
| 805 | ctx.line[0] = 0; |
| 806 | |
| 807 | /* EPP WCEL_Dump(&ctx, "init"); */ |
| 808 | |
| 809 | while (!ctx.done && !ctx.error && WCEL_Get(&ctx, &ir)) |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 810 | { |
Eric Pouech | 412d37f | 2003-06-21 02:07:10 +0000 | [diff] [blame] | 811 | if (ir.EventType != KEY_EVENT) continue; |
Michael Stefaniuc | 8c38b88 | 2006-10-12 22:56:29 +0200 | [diff] [blame] | 812 | TRACE("key%s repeatCount=%u, keyCode=%02x scanCode=%02x char=%02x keyState=%08x\n", |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 813 | ir.Event.KeyEvent.bKeyDown ? "Down" : "Up ", ir.Event.KeyEvent.wRepeatCount, |
| 814 | ir.Event.KeyEvent.wVirtualKeyCode, ir.Event.KeyEvent.wVirtualScanCode, |
| 815 | ir.Event.KeyEvent.uChar.UnicodeChar, ir.Event.KeyEvent.dwControlKeyState); |
Eric Pouech | 412d37f | 2003-06-21 02:07:10 +0000 | [diff] [blame] | 816 | if (!ir.Event.KeyEvent.bKeyDown) continue; |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 817 | |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 818 | /* EPP WCEL_Dump(&ctx, "before func"); */ |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 819 | ofs = ctx.ofs; |
Eric Pouech | 824a7f1 | 2002-01-10 18:20:51 +0000 | [diff] [blame] | 820 | /* mask out some bits which don't interest us */ |
| 821 | ks = ir.Event.KeyEvent.dwControlKeyState & ~(NUMLOCK_ON|SCROLLLOCK_ON|CAPSLOCK_ON); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 822 | |
| 823 | func = NULL; |
| 824 | for (km = (use_emacs) ? EmacsKeyMap : Win32KeyMap; km->entries != NULL; km++) |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 825 | { |
Eric Pouech | 824a7f1 | 2002-01-10 18:20:51 +0000 | [diff] [blame] | 826 | if (km->keyState != ks) |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 827 | continue; |
| 828 | if (km->chkChar) |
| 829 | { |
| 830 | for (ke = &km->entries[0]; ke->func != 0; ke++) |
| 831 | if (ke->val == ir.Event.KeyEvent.uChar.UnicodeChar) break; |
| 832 | } |
| 833 | else |
| 834 | { |
| 835 | for (ke = &km->entries[0]; ke->func != 0; ke++) |
| 836 | if (ke->val == ir.Event.KeyEvent.wVirtualKeyCode) break; |
| 837 | |
| 838 | } |
| 839 | if (ke->func) |
| 840 | { |
| 841 | func = ke->func; |
| 842 | break; |
| 843 | } |
| 844 | } |
| 845 | |
| 846 | if (func) |
| 847 | (func)(&ctx); |
| 848 | else if (!(ir.Event.KeyEvent.dwControlKeyState & (ENHANCED_KEY|LEFT_ALT_PRESSED))) |
| 849 | WCEL_InsertChar(&ctx, ir.Event.KeyEvent.uChar.UnicodeChar); |
| 850 | else TRACE("Dropped event\n"); |
| 851 | |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 852 | /* EPP WCEL_Dump(&ctx, "after func"); */ |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 853 | if (ctx.ofs != ofs) |
| 854 | SetConsoleCursorPosition(ctx.hConOut, WCEL_GetCoord(&ctx, ctx.ofs)); |
| 855 | } |
Vincent Béron | 9a62491 | 2002-05-31 23:06:46 +0000 | [diff] [blame] | 856 | if (ctx.error) |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 857 | { |
| 858 | HeapFree(GetProcessHeap(), 0, ctx.line); |
| 859 | ctx.line = NULL; |
| 860 | } |
Eric Pouech | 50ac3e5 | 2002-07-23 20:54:18 +0000 | [diff] [blame] | 861 | WCEL_FreeYank(&ctx); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 862 | if (ctx.line) |
| 863 | CONSOLE_AppendHistory(ctx.line); |
| 864 | |
| 865 | CloseHandle(ctx.hConOut); |
Michael Stefaniuc | 5ad7d85 | 2004-12-23 17:06:43 +0000 | [diff] [blame] | 866 | HeapFree(GetProcessHeap(), 0, ctx.histCurr); |
Eric Pouech | 0b83d4c | 2001-11-23 23:04:58 +0000 | [diff] [blame] | 867 | return ctx.line; |
| 868 | } |