Chris Allegretta | 11b0011 | 2000-08-06 21:13:45 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2 | /************************************************************************** |
| 3 | * winio.c * |
| 4 | * * |
David Lawrence Ramsey | f28f50e | 2004-01-09 23:04:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 Chris Allegretta * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
Chris Allegretta | 3a24f3f | 2001-10-24 11:33:54 +0000 | [diff] [blame] | 8 | * the Free Software Foundation; either version 2, or (at your option) * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 9 | * any later version. * |
| 10 | * * |
| 11 | * This program 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 * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 19 | * * |
| 20 | **************************************************************************/ |
| 21 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 24 | #include <stdarg.h> |
| 25 | #include <string.h> |
Chris Allegretta | dba37ae | 2000-07-07 05:13:09 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 27 | #include <unistd.h> |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 29 | #include <assert.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 30 | #include "proto.h" |
| 31 | #include "nano.h" |
| 32 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 33 | static int statblank = 0; /* Number of keystrokes left after |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 34 | we call statusbar(), before we |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 35 | actually blank the statusbar */ |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 36 | |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 37 | /* Control character compatibility: |
| 38 | * |
| 39 | * - NANO_BACKSPACE_KEY is Ctrl-H, which is Backspace under ASCII, ANSI, |
| 40 | * VT100, and VT220. |
| 41 | * - NANO_TAB_KEY is Ctrl-I, which is Tab under ASCII, ANSI, VT100, |
| 42 | * VT220, and VT320. |
| 43 | * - NANO_ENTER_KEY is Ctrl-M, which is Enter under ASCII, ANSI, VT100, |
| 44 | * VT220, and VT320. |
| 45 | * - NANO_XON_KEY is Ctrl-Q, which is XON under ASCII, ANSI, VT100, |
| 46 | * VT220, and VT320. |
| 47 | * - NANO_XOFF_KEY is Ctrl-S, which is XOFF under ASCII, ANSI, VT100, |
| 48 | * VT220, and VT320. |
David Lawrence Ramsey | a849ab1 | 2004-05-01 04:13:06 +0000 | [diff] [blame] | 49 | * - NANO_CONTROL_8 is Ctrl-8 (Ctrl-?), which is Delete under ASCII, |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 50 | * ANSI, VT100, and VT220, and which is Backspace under VT320. |
| 51 | * |
| 52 | * Note: VT220s and VT320s also generate Esc [ 3 ~ for Delete. By |
David Lawrence Ramsey | a849ab1 | 2004-05-01 04:13:06 +0000 | [diff] [blame] | 53 | * default, xterm assumes it's running on a VT320 and generates Ctrl-8 |
| 54 | * (Ctrl-?) for Backspace and Esc [ 3 ~ for Delete. This causes |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 55 | * problems for VT100-derived terminals such as the FreeBSD console, |
David Lawrence Ramsey | a849ab1 | 2004-05-01 04:13:06 +0000 | [diff] [blame] | 56 | * which expect Ctrl-H for Backspace and Ctrl-8 (Ctrl-?) for Delete, and |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 57 | * on which the VT320 sequences are translated by the keypad to KEY_DC |
| 58 | * and [nothing]. We work around this conflict via the REBIND_DELETE |
| 59 | * flag: if it's not set, we assume VT320 compatibility, and if it is, |
| 60 | * we assume VT100 compatibility. Thanks to Lee Nelson and Wouter van |
| 61 | * Hemel for helping work this conflict out. |
| 62 | * |
| 63 | * Escape sequence compatibility: |
| 64 | * |
| 65 | * We support escape sequences for ANSI, VT100, VT220, VT320, the Linux |
| 66 | * console, the FreeBSD console, the Hurd console (a.k.a. the Mach |
| 67 | * console), xterm, rxvt, and Eterm. Among these, there are several |
| 68 | * conflicts and omissions, outlined as follows: |
| 69 | * |
| 70 | * - Tab on ANSI == PageUp on FreeBSD console; the former is omitted. |
| 71 | * (Ctrl-I is also Tab on ANSI, which we already support.) |
| 72 | * - PageDown on FreeBSD console == Center (5) on numeric keypad with |
| 73 | * NumLock off on Linux console; the latter is omitted. (The editing |
| 74 | * keypad key is more important to have working than the numeric |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 75 | * keypad key, because the latter has no value when NumLock is off.) |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 76 | * - F1 on FreeBSD console == the mouse key on xterm/rxvt/Eterm; the |
| 77 | * latter is omitted. (Mouse input will only work properly if the |
| 78 | * extended keypad value KEY_MOUSE is generated on mouse events |
| 79 | * instead of the escape sequence.) |
| 80 | * - F9 on FreeBSD console == PageDown on Hurd console; the former is |
| 81 | * omitted. (The editing keypad is more important to have working |
| 82 | * than the function keys, because the functions of the former are not |
| 83 | * arbitrary and the functions of the latter are.) |
| 84 | * - F10 on FreeBSD console == PageUp on Hurd console; the former is |
| 85 | * omitted. (Same as above.) |
| 86 | * - F13 on FreeBSD console == End on Hurd console; the former is |
| 87 | * omitted. (Same as above.) |
| 88 | * - The Hurd console has no escape sequences for F11, F12, F13, F14, or |
| 89 | * Center (5) on the numeric keypad with NumLock off. |
| 90 | * |
| 91 | * Note that Center (5) on the numeric keypad with NumLock off can also |
| 92 | * be the Begin key. */ |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 93 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 94 | #ifndef NANO_SMALL |
| 95 | /* Reset all the input routines that rely on character sequences. */ |
| 96 | void reset_kbinput(void) |
| 97 | { |
| 98 | get_translated_kbinput(0, NULL, TRUE); |
| 99 | get_ascii_kbinput(0, 0, TRUE); |
| 100 | get_untranslated_kbinput(0, 0, FALSE, TRUE); |
| 101 | } |
| 102 | #endif |
| 103 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 104 | /* Read in a single input character. If it's ignored, swallow it and go |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 105 | * on. Otherwise, try to translate it from ASCII, extended keypad |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 106 | * values, and/or escape sequences. Set meta_key to TRUE when we get a |
| 107 | * meta sequence. Supported extended keypad values consist of [arrow |
| 108 | * key], Ctrl-[arrow key], Shift-[arrow key], Enter, Backspace, the |
| 109 | * editing keypad (Insert, Delete, Home, End, PageUp, and PageDown), the |
| 110 | * function keypad (F1-F14), and the numeric keypad with NumLock off. |
| 111 | * Assume nodelay(win) is FALSE. */ |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 112 | int get_kbinput(WINDOW *win, int *meta_key) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 113 | { |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 114 | int kbinput, es, retval = ERR; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 115 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 116 | #ifndef NANO_SMALL |
| 117 | allow_pending_sigwinch(TRUE); |
| 118 | #endif |
| 119 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 120 | *meta_key = FALSE; |
| 121 | |
| 122 | while (retval == ERR) { |
| 123 | /* Read a character using blocking input, since using |
| 124 | * non-blocking input will eat up all unused CPU. Then pass it |
| 125 | * to get_translated_kbinput(). Continue until we get a |
| 126 | * complete sequence. */ |
| 127 | kbinput = wgetch(win); |
| 128 | retval = get_translated_kbinput(kbinput, &es |
| 129 | #ifndef NANO_SMALL |
| 130 | , FALSE |
| 131 | #endif |
| 132 | ); |
| 133 | |
| 134 | /* If we got an escape sequence, read it in, including the |
| 135 | * initial non-escape, as verbatim input. */ |
| 136 | if (es) { |
| 137 | int *escape_seq = NULL; |
| 138 | size_t es_len; |
| 139 | |
| 140 | /* First, assume that we got a meta sequence. Set meta_key |
| 141 | * to TRUE and save the character we got as the result. We |
| 142 | * do this so that if there's a delay greater than nodelay() |
| 143 | * between Escape and the character we got (after we |
| 144 | * ungetch() it below), it'll still be properly interpreted |
| 145 | * as a meta sequence. */ |
| 146 | *meta_key = TRUE; |
| 147 | retval = tolower(kbinput); |
| 148 | |
| 149 | /* Next, send back the character we got and read in the |
| 150 | * complete escape sequence. */ |
| 151 | ungetch(kbinput); |
| 152 | escape_seq = get_verbatim_kbinput(win, escape_seq, &es_len, |
| 153 | FALSE); |
| 154 | |
| 155 | if (es_len > 1) { |
| 156 | /* The escape sequence is more than one character |
| 157 | * long. Set meta_key to FALSE, translate the escape |
| 158 | * sequence into the corresponding key value, and save |
| 159 | * that as the result. */ |
| 160 | *meta_key = FALSE; |
| 161 | if ((retval = get_escape_seq_kbinput(escape_seq, |
| 162 | es_len)) == ERR) { |
| 163 | /* This escape sequence is unrecognized. Send it |
| 164 | * back. */ |
| 165 | for (; es_len > 1; es_len--) |
| 166 | ungetch(escape_seq[es_len - 1]); |
| 167 | retval = escape_seq[0]; |
| 168 | } |
| 169 | } |
| 170 | free(escape_seq); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | #ifdef DEBUG |
| 175 | fprintf(stderr, "get_kbinput(): kbinput = %d, meta_key = %d\n", kbinput, *meta_key); |
| 176 | #endif |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 177 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 178 | #ifndef NANO_SMALL |
| 179 | allow_pending_sigwinch(FALSE); |
| 180 | #endif |
| 181 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 182 | return retval; |
| 183 | } |
| 184 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 185 | /* Translate acceptable ASCII, extended keypad values, and escape |
| 186 | * sequences into their corresponding key values. Set es to TRUE when |
| 187 | * we get an escape sequence. Assume nodelay(win) is FALSE. */ |
| 188 | int get_translated_kbinput(int kbinput, int *es |
| 189 | #ifndef NANO_SMALL |
| 190 | , int reset |
| 191 | #endif |
| 192 | ) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 193 | { |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 194 | static size_t escapes = 0, ascii_digits = 0; |
| 195 | int retval = ERR; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 196 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 197 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 198 | if (reset) { |
| 199 | escapes = 0; |
| 200 | ascii_digits = 0; |
| 201 | return ERR; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 202 | } |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 203 | #endif |
| 204 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 205 | *es = FALSE; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 206 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 207 | switch (kbinput) { |
| 208 | case ERR: |
| 209 | break; |
| 210 | case NANO_CONTROL_3: |
| 211 | /* Increment the escape counter. */ |
| 212 | escapes++; |
| 213 | switch (escapes) { |
| 214 | case 1: |
| 215 | /* One escape: wait for more input. */ |
| 216 | case 2: |
| 217 | /* Two escapes: wait for more input. */ |
| 218 | break; |
| 219 | default: |
| 220 | /* More than two escapes: reset the escape counter |
| 221 | * and wait for more input. */ |
| 222 | escapes = 0; |
| 223 | } |
| 224 | break; |
| 225 | #if !defined(NANO_SMALL) && defined(KEY_RESIZE) |
| 226 | /* Since we don't change the default SIGWINCH handler when |
| 227 | * NANO_SMALL is defined, KEY_RESIZE is never generated. Also, |
| 228 | * Slang and SunOS 5.7-5.9 don't support KEY_RESIZE. */ |
| 229 | case KEY_RESIZE: |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 230 | #endif |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 231 | #ifdef PDCURSES |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 232 | case KEY_SHIFT_L: |
| 233 | case KEY_SHIFT_R: |
| 234 | case KEY_CONTROL_L: |
| 235 | case KEY_CONTROL_R: |
| 236 | case KEY_ALT_L: |
| 237 | case KEY_ALT_R: |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 238 | #endif |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 239 | retval = ERR; |
| 240 | break; |
| 241 | default: |
| 242 | switch (escapes) { |
| 243 | case 0: |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 244 | switch (kbinput) { |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 245 | case NANO_CONTROL_8: |
| 246 | retval = ISSET(REBIND_DELETE) ? |
| 247 | NANO_DELETE_KEY : NANO_BACKSPACE_KEY; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 248 | break; |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 249 | case KEY_DOWN: |
| 250 | retval = NANO_NEXTLINE_KEY; |
| 251 | break; |
| 252 | case KEY_UP: |
| 253 | retval = NANO_PREVLINE_KEY; |
| 254 | break; |
| 255 | case KEY_LEFT: |
| 256 | retval = NANO_BACK_KEY; |
| 257 | break; |
| 258 | case KEY_RIGHT: |
| 259 | retval = NANO_FORWARD_KEY; |
| 260 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 261 | #ifdef KEY_HOME |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 262 | /* HP-UX 10 and 11 don't support KEY_HOME. */ |
| 263 | case KEY_HOME: |
| 264 | retval = NANO_HOME_KEY; |
| 265 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 266 | #endif |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 267 | case KEY_BACKSPACE: |
| 268 | retval = NANO_BACKSPACE_KEY; |
| 269 | break; |
| 270 | case KEY_DC: |
| 271 | retval = ISSET(REBIND_DELETE) ? |
| 272 | NANO_BACKSPACE_KEY : NANO_DELETE_KEY; |
| 273 | break; |
| 274 | case KEY_IC: |
| 275 | retval = NANO_INSERTFILE_KEY; |
| 276 | break; |
| 277 | case KEY_NPAGE: |
| 278 | retval = NANO_NEXTPAGE_KEY; |
| 279 | break; |
| 280 | case KEY_PPAGE: |
| 281 | retval = NANO_PREVPAGE_KEY; |
| 282 | break; |
| 283 | case KEY_ENTER: |
| 284 | retval = NANO_ENTER_KEY; |
| 285 | break; |
| 286 | case KEY_B2: /* Center (5) on numeric keypad |
| 287 | * with NumLock off on xterm. */ |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 288 | #ifdef KEY_BEG |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 289 | /* Slang doesn't support KEY_BEG. */ |
| 290 | case KEY_BEG: /* Center (5) on numeric keypad |
| 291 | * with NumLock off on Eterm. */ |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 292 | #endif |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 293 | retval = ERR; |
| 294 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 295 | #ifdef KEY_END |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 296 | /* HP-UX 10 and 11 don't support KEY_END. */ |
| 297 | case KEY_END: |
| 298 | retval = NANO_END_KEY; |
| 299 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 300 | #endif |
| 301 | #ifdef KEY_SUSPEND |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 302 | /* Slang doesn't support KEY_SUSPEND. */ |
| 303 | case KEY_SUSPEND: |
| 304 | retval = NANO_SUSPEND_KEY; |
| 305 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 306 | #endif |
| 307 | #ifdef KEY_SLEFT |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 308 | /* Slang doesn't support KEY_SLEFT. */ |
| 309 | case KEY_SLEFT: |
| 310 | retval = NANO_BACK_KEY; |
| 311 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 312 | #endif |
| 313 | #ifdef KEY_SRIGHT |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 314 | /* Slang doesn't support KEY_SRIGHT. */ |
| 315 | case KEY_SRIGHT: |
| 316 | retval = NANO_FORWARD_KEY; |
| 317 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 318 | #endif |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 319 | default: |
| 320 | retval = kbinput; |
| 321 | break; |
| 322 | } |
| 323 | break; |
| 324 | case 1: |
| 325 | /* One escape followed by a non-escape: escape |
| 326 | * sequence mode. Reset the escape counter and set |
| 327 | * es to TRUE. */ |
| 328 | escapes = 0; |
| 329 | *es = TRUE; |
| 330 | break; |
| 331 | case 2: |
| 332 | switch (kbinput) { |
| 333 | case '0': |
| 334 | case '1': |
| 335 | case '2': |
| 336 | case '3': |
| 337 | case '4': |
| 338 | case '5': |
| 339 | case '6': |
| 340 | case '7': |
| 341 | case '8': |
| 342 | case '9': |
| 343 | /* Two escapes followed by one or more |
| 344 | * digits: ASCII character sequence mode. |
| 345 | * If the digit sequence's range is limited |
| 346 | * to 2XX (the first digit is in the '0' to |
| 347 | * '2' range and it's the first digit, or if |
| 348 | * it's in the full digit range and it's not |
| 349 | * the first digit), increment the ASCII |
| 350 | * digit counter and interpret the digit. |
| 351 | * If the digit sequence's range is not |
| 352 | * limited to 2XX, fall through. */ |
| 353 | if (kbinput <= '2' || ascii_digits > 0) { |
| 354 | ascii_digits++; |
| 355 | kbinput = get_ascii_kbinput(kbinput, |
| 356 | ascii_digits |
| 357 | #ifndef NANO_SMALL |
| 358 | , FALSE |
| 359 | #endif |
| 360 | ); |
| 361 | |
| 362 | if (kbinput != ERR) { |
| 363 | /* If we've read in a complete ASCII |
| 364 | * digit sequence, reset the ASCII |
| 365 | * digit counter and the escape |
| 366 | * counter and save the corresponding |
| 367 | * ASCII character as the result. */ |
| 368 | ascii_digits = 0; |
| 369 | escapes = 0; |
| 370 | retval = kbinput; |
| 371 | } |
| 372 | break; |
| 373 | } |
| 374 | default: |
| 375 | /* Reset the escape counter. */ |
| 376 | escapes = 0; |
| 377 | if (ascii_digits == 0) |
| 378 | /* Two escapes followed by a non-digit |
| 379 | * or a digit that would create an ASCII |
| 380 | * digit sequence greater than 2XX, and |
| 381 | * we're not in the middle of an ASCII |
| 382 | * character sequence: control character |
| 383 | * sequence mode. Interpret the control |
| 384 | * sequence and save the corresponding |
| 385 | * control character as the result. */ |
| 386 | retval = get_control_kbinput(kbinput); |
| 387 | else { |
| 388 | /* If we were in the middle of an ASCII |
| 389 | * character sequence, reset the ASCII |
| 390 | * digit counter and save the character |
| 391 | * we got as the result. */ |
| 392 | ascii_digits = 0; |
| 393 | retval = kbinput; |
| 394 | } |
| 395 | } |
| 396 | } |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 397 | } |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 398 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 399 | #ifdef DEBUG |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 400 | fprintf(stderr, "get_translated_kbinput(): kbinput = %d, es = %d, escapes = %d, ascii_digits = %d, retval = %d\n", kbinput, *es, escapes, ascii_digits, retval); |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 401 | #endif |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 402 | |
| 403 | /* Return the result. */ |
| 404 | return retval; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 405 | } |
| 406 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 407 | /* Translate an ASCII character sequence: turn a three-digit decimal |
| 408 | * ASCII code from 000-255 into its corresponding ASCII character. */ |
| 409 | int get_ascii_kbinput(int kbinput, size_t ascii_digits |
| 410 | #ifndef NANO_SMALL |
| 411 | , int reset |
| 412 | #endif |
| 413 | ) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 414 | { |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 415 | static int ascii_kbinput = 0; |
| 416 | int retval = ERR; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 417 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 418 | #ifndef NANO_SMALL |
| 419 | if (reset) { |
| 420 | ascii_kbinput = 0; |
| 421 | return ERR; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 422 | } |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 423 | #endif |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 424 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 425 | switch (ascii_digits) { |
| 426 | case 1: |
| 427 | /* Read in the first of the three ASCII digits. */ |
| 428 | switch (kbinput) { |
| 429 | /* Add the digit we got to the 100's position of the |
| 430 | * ASCII character sequence holder. */ |
| 431 | case '0': |
| 432 | case '1': |
| 433 | case '2': |
| 434 | ascii_kbinput += (kbinput - '0') * 100; |
| 435 | break; |
| 436 | default: |
| 437 | retval = kbinput; |
| 438 | } |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 439 | break; |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 440 | case 2: |
| 441 | /* Read in the second of the three ASCII digits. */ |
| 442 | switch (kbinput) { |
| 443 | /* Add the digit we got to the 10's position of the |
| 444 | * ASCII character sequence holder. */ |
| 445 | case '0': |
| 446 | case '1': |
| 447 | case '2': |
| 448 | case '3': |
| 449 | case '4': |
| 450 | case '5': |
| 451 | ascii_kbinput += (kbinput - '0') * 10; |
| 452 | break; |
| 453 | case '6': |
| 454 | case '7': |
| 455 | case '8': |
| 456 | case '9': |
| 457 | if (ascii_kbinput < 200) { |
| 458 | ascii_kbinput += (kbinput - '0') * 10; |
| 459 | break; |
| 460 | } |
| 461 | default: |
| 462 | retval = kbinput; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 463 | } |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 464 | break; |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 465 | case 3: |
| 466 | /* Read in the third of the three ASCII digits. */ |
| 467 | switch (kbinput) { |
| 468 | /* Add the digit we got to the 1's position of the ASCII |
| 469 | * character sequence holder, and save the corresponding |
| 470 | * ASCII character as the result. */ |
| 471 | case '0': |
| 472 | case '1': |
| 473 | case '2': |
| 474 | case '3': |
| 475 | case '4': |
| 476 | case '5': |
| 477 | ascii_kbinput += (kbinput - '0'); |
| 478 | retval = ascii_kbinput; |
| 479 | break; |
| 480 | case '6': |
| 481 | case '7': |
| 482 | case '8': |
| 483 | case '9': |
| 484 | if (ascii_kbinput < 250) { |
| 485 | ascii_kbinput += (kbinput - '0'); |
| 486 | retval = ascii_kbinput; |
| 487 | break; |
| 488 | } |
| 489 | default: |
| 490 | retval = kbinput; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 491 | } |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 492 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 493 | } |
| 494 | |
| 495 | #ifdef DEBUG |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 496 | fprintf(stderr, "get_ascii_kbinput(): kbinput = %d, ascii_digits = %d, ascii_kbinput = %d, retval = %d\n", kbinput, ascii_digits, ascii_kbinput, retval); |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 497 | #endif |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 498 | |
| 499 | /* If the result is an ASCII character, reset the ASCII character |
| 500 | * sequence holder. */ |
| 501 | if (retval != ERR) |
| 502 | ascii_kbinput = 0; |
| 503 | |
| 504 | return retval; |
| 505 | } |
| 506 | |
| 507 | /* Translate a control character sequence: turn an ASCII non-control |
| 508 | * character into its corresponding control character. */ |
| 509 | int get_control_kbinput(int kbinput) |
| 510 | { |
| 511 | int retval = ERR; |
| 512 | |
| 513 | /* We don't handle Ctrl-2 here, since Esc Esc 2 could be the first |
| 514 | * part of an ASCII character sequence. */ |
| 515 | |
| 516 | /* Ctrl-2 (Ctrl-Space) == Ctrl-@ == Ctrl-` */ |
| 517 | if (kbinput == ' ' || kbinput == '@' || kbinput == '`') |
| 518 | retval = NANO_CONTROL_SPACE; |
| 519 | /* Ctrl-3 (Ctrl-[, Esc) to Ctrl-7 (Ctrl-_) */ |
| 520 | else if (kbinput >= '3' && kbinput <= '7') |
| 521 | retval = kbinput - 24; |
| 522 | /* Ctrl-8 (Ctrl-?) */ |
| 523 | else if (kbinput == '8' || kbinput == '?') |
| 524 | retval = NANO_CONTROL_8; |
| 525 | /* Ctrl-A to Ctrl-_ */ |
| 526 | else if (kbinput >= 'A' && kbinput <= '_') |
| 527 | retval = kbinput - 64; |
| 528 | /* Ctrl-a to Ctrl-~ */ |
| 529 | else if (kbinput >= 'a' && kbinput <= '~') |
| 530 | retval = kbinput - 96; |
| 531 | else |
| 532 | retval = kbinput; |
| 533 | |
| 534 | #ifdef DEBUG |
| 535 | fprintf(stderr, "get_control_kbinput(): kbinput = %d, retval = %d\n", kbinput, ascii_digits, *complete, retval); |
| 536 | #endif |
| 537 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 538 | return retval; |
| 539 | } |
| 540 | |
David Lawrence Ramsey | 58f6d83 | 2004-01-27 07:12:47 +0000 | [diff] [blame] | 541 | /* Translate escape sequences, most of which correspond to extended |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 542 | * keypad values, nto their corresponding key values. These sequences |
| 543 | * are generated when the keypad doesn't support the needed keys. |
| 544 | * Assume that Escape has already been read in. */ |
| 545 | int get_escape_seq_kbinput(int *escape_seq, size_t es_len) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 546 | { |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 547 | int retval = ERR; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 548 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 549 | if (es_len > 1) { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 550 | switch (escape_seq[0]) { |
| 551 | case 'O': |
| 552 | switch (escape_seq[1]) { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 553 | case '2': |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 554 | if (es_len >= 3) { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 555 | switch (escape_seq[2]) { |
| 556 | case 'P': /* Esc O 2 P == F13 on |
| 557 | * xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 558 | retval = KEY_F(13); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 559 | break; |
| 560 | case 'Q': /* Esc O 2 Q == F14 on |
| 561 | * xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 562 | retval = KEY_F(14); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 563 | break; |
| 564 | } |
| 565 | } |
| 566 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 567 | case 'A': /* Esc O A == Up on VT100/VT320/xterm. */ |
| 568 | case 'B': /* Esc O B == Down on |
| 569 | * VT100/VT320/xterm. */ |
| 570 | case 'C': /* Esc O C == Right on |
| 571 | * VT100/VT320/xterm. */ |
| 572 | case 'D': /* Esc O D == Left on |
| 573 | * VT100/VT320/xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 574 | retval = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 575 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 576 | case 'E': /* Esc O E == Center (5) on numeric keypad |
| 577 | * with NumLock off on xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 578 | retval = ERR; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 579 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 580 | case 'F': /* Esc O F == End on xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 581 | retval = NANO_END_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 582 | break; |
| 583 | case 'H': /* Esc O H == Home on xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 584 | retval = NANO_HOME_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 585 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 586 | case 'M': /* Esc O M == Enter on numeric keypad with |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 587 | * NumLock off on |
| 588 | * VT100/VT220/VT320/xterm/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 589 | retval = NANO_ENTER_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 590 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 591 | case 'P': /* Esc O P == F1 on VT100/VT220/VT320/Hurd |
| 592 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 593 | retval = KEY_F(1); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 594 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 595 | case 'Q': /* Esc O Q == F2 on VT100/VT220/VT320/Hurd |
| 596 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 597 | retval = KEY_F(2); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 598 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 599 | case 'R': /* Esc O R == F3 on VT100/VT220/VT320/Hurd |
| 600 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 601 | retval = KEY_F(3); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 602 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 603 | case 'S': /* Esc O S == F4 on VT100/VT220/VT320/Hurd |
| 604 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 605 | retval = KEY_F(4); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 606 | break; |
| 607 | case 'T': /* Esc O T == F5 on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 608 | retval = KEY_F(5); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 609 | break; |
| 610 | case 'U': /* Esc O U == F6 on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 611 | retval = KEY_F(6); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 612 | break; |
| 613 | case 'V': /* Esc O V == F7 on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 614 | retval = KEY_F(7); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 615 | break; |
| 616 | case 'W': /* Esc O W == F8 on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 617 | retval = KEY_F(8); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 618 | break; |
| 619 | case 'X': /* Esc O X == F9 on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 620 | retval = KEY_F(9); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 621 | break; |
| 622 | case 'Y': /* Esc O Y == F10 on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 623 | retval = KEY_F(10); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 624 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 625 | case 'a': /* Esc O a == Ctrl-Up on rxvt. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 626 | case 'b': /* Esc O b == Ctrl-Down on rxvt. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 627 | case 'c': /* Esc O c == Ctrl-Right on rxvt. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 628 | case 'd': /* Esc O d == Ctrl-Left on rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 629 | retval = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 630 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 631 | case 'j': /* Esc O j == '*' on numeric keypad with |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 632 | * NumLock off on |
| 633 | * VT100/VT220/VT320/xterm/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 634 | retval = '*'; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 635 | break; |
| 636 | case 'k': /* Esc O k == '+' on numeric keypad with |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 637 | * NumLock off on |
| 638 | * VT100/VT220/VT320/xterm/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 639 | retval = '+'; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 640 | break; |
| 641 | case 'l': /* Esc O l == ',' on numeric keypad with |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 642 | * NumLock off on |
| 643 | * VT100/VT220/VT320/xterm/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 644 | retval = '+'; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 645 | break; |
| 646 | case 'm': /* Esc O m == '-' on numeric keypad with |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 647 | * NumLock off on |
| 648 | * VT100/VT220/VT320/xterm/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 649 | retval = '-'; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 650 | break; |
| 651 | case 'n': /* Esc O n == Delete (.) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 652 | * with NumLock off on |
| 653 | * VT100/VT220/VT320/xterm/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 654 | retval = NANO_DELETE_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 655 | break; |
| 656 | case 'o': /* Esc O o == '/' on numeric keypad with |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 657 | * NumLock off on |
| 658 | * VT100/VT220/VT320/xterm/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 659 | retval = '/'; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 660 | break; |
| 661 | case 'p': /* Esc O p == Insert (0) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 662 | * with NumLock off on |
| 663 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 664 | retval = NANO_INSERTFILE_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 665 | break; |
| 666 | case 'q': /* Esc O q == End (1) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 667 | * with NumLock off on |
| 668 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 669 | retval = NANO_END_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 670 | break; |
| 671 | case 'r': /* Esc O r == Down (2) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 672 | * with NumLock off on |
| 673 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 674 | retval = NANO_NEXTLINE_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 675 | break; |
| 676 | case 's': /* Esc O s == PageDown (3) on numeric |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 677 | * keypad with NumLock off on |
| 678 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 679 | retval = NANO_NEXTPAGE_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 680 | break; |
| 681 | case 't': /* Esc O t == Left (4) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 682 | * with NumLock off on |
| 683 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 684 | retval = NANO_BACK_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 685 | break; |
| 686 | case 'u': /* Esc O u == Center (5) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 687 | * with NumLock off on |
| 688 | * VT100/VT220/VT320/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 689 | retval = ERR; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 690 | break; |
| 691 | case 'v': /* Esc O v == Right (6) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 692 | * with NumLock off on |
| 693 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 694 | retval = NANO_FORWARD_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 695 | break; |
| 696 | case 'w': /* Esc O w == Home (7) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 697 | * with NumLock off on |
| 698 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 699 | retval = NANO_HOME_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 700 | break; |
| 701 | case 'x': /* Esc O x == Up (8) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 702 | * with NumLock off on |
| 703 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 704 | retval = NANO_PREVLINE_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 705 | break; |
| 706 | case 'y': /* Esc O y == PageUp (9) on numeric keypad |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 707 | * with NumLock off on |
| 708 | * VT100/VT220/VT320/rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 709 | retval = NANO_PREVPAGE_KEY; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 710 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 711 | } |
| 712 | break; |
| 713 | case 'o': |
| 714 | switch (escape_seq[1]) { |
| 715 | case 'a': /* Esc o a == Ctrl-Up on Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 716 | case 'b': /* Esc o b == Ctrl-Down on Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 717 | case 'c': /* Esc o c == Ctrl-Right on Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 718 | case 'd': /* Esc o d == Ctrl-Left on Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 719 | retval = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 720 | break; |
| 721 | } |
| 722 | break; |
| 723 | case '[': |
| 724 | switch (escape_seq[1]) { |
| 725 | case '1': |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 726 | if (es_len >= 3) { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 727 | switch (escape_seq[2]) { |
| 728 | case '1': /* Esc [ 1 1 ~ == F1 on |
| 729 | * rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 730 | retval = KEY_F(1); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 731 | break; |
| 732 | case '2': /* Esc [ 1 2 ~ == F2 on |
| 733 | * rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 734 | retval = KEY_F(2); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 735 | break; |
| 736 | case '3': /* Esc [ 1 3 ~ == F3 on |
| 737 | * rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 738 | retval = KEY_F(3); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 739 | break; |
| 740 | case '4': /* Esc [ 1 4 ~ == F4 on |
| 741 | * rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 742 | retval = KEY_F(4); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 743 | break; |
| 744 | case '5': /* Esc [ 1 5 ~ == F5 on |
| 745 | * xterm/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 746 | retval = KEY_F(5); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 747 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 748 | case '7': /* Esc [ 1 7 ~ == F6 on |
| 749 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 750 | * console/xterm/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 751 | retval = KEY_F(6); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 752 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 753 | case '8': /* Esc [ 1 8 ~ == F7 on |
| 754 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 755 | * console/xterm/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 756 | retval = KEY_F(7); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 757 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 758 | case '9': /* Esc [ 1 9 ~ == F8 on |
| 759 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 760 | * console/xterm/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 761 | retval = KEY_F(8); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 762 | break; |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 763 | case ';': |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 764 | if (es_len >= 4) { |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 765 | switch (escape_seq[3]) { |
| 766 | case '2': |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 767 | if (es_len >= 5) { |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 768 | switch (escape_seq[4]) { |
| 769 | case 'A': /* Esc [ 1 ; 2 A == Shift-Up on |
| 770 | * xterm. */ |
| 771 | case 'B': /* Esc [ 1 ; 2 B == Shift-Down on |
| 772 | * xterm. */ |
| 773 | case 'C': /* Esc [ 1 ; 2 C == Shift-Right on |
| 774 | * xterm. */ |
| 775 | case 'D': /* Esc [ 1 ; 2 D == Shift-Left on |
| 776 | * xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 777 | retval = get_escape_seq_abcd(escape_seq[4]); |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 778 | break; |
| 779 | } |
| 780 | } |
| 781 | break; |
| 782 | case '5': |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 783 | if (es_len >= 5) { |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 784 | switch (escape_seq[4]) { |
| 785 | case 'A': /* Esc [ 1 ; 5 A == Ctrl-Up on |
| 786 | * xterm. */ |
| 787 | case 'B': /* Esc [ 1 ; 5 B == Ctrl-Down on |
| 788 | * xterm. */ |
| 789 | case 'C': /* Esc [ 1 ; 5 C == Ctrl-Right on |
| 790 | * xterm. */ |
| 791 | case 'D': /* Esc [ 1 ; 5 D == Ctrl-Left on |
| 792 | * xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 793 | retval = get_escape_seq_abcd(escape_seq[4]); |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 794 | break; |
| 795 | } |
| 796 | } |
| 797 | break; |
| 798 | } |
| 799 | } |
| 800 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 801 | default: /* Esc [ 1 ~ == Home on |
| 802 | * VT320/Linux console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 803 | retval = NANO_HOME_KEY; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 804 | break; |
| 805 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 806 | } |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 807 | break; |
| 808 | case '2': |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 809 | if (es_len >= 3) { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 810 | switch (escape_seq[2]) { |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 811 | case '0': /* Esc [ 2 0 ~ == F9 on |
| 812 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 813 | * console/xterm/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 814 | retval = KEY_F(9); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 815 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 816 | case '1': /* Esc [ 2 1 ~ == F10 on |
| 817 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 818 | * console/xterm/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 819 | retval = KEY_F(10); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 820 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 821 | case '3': /* Esc [ 2 3 ~ == F11 on |
| 822 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 823 | * console/xterm/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 824 | retval = KEY_F(11); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 825 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 826 | case '4': /* Esc [ 2 4 ~ == F12 on |
| 827 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 828 | * console/xterm/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 829 | retval = KEY_F(12); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 830 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 831 | case '5': /* Esc [ 2 5 ~ == F13 on |
| 832 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 833 | * console/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 834 | retval = KEY_F(13); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 835 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 836 | case '6': /* Esc [ 2 6 ~ == F14 on |
| 837 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 838 | * console/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 839 | retval = KEY_F(14); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 840 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 841 | default: /* Esc [ 2 ~ == Insert on |
| 842 | * VT220/VT320/Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 843 | * console/xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 844 | retval = NANO_INSERTFILE_KEY; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 845 | break; |
| 846 | } |
| 847 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 848 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 849 | case '3': /* Esc [ 3 ~ == Delete on |
| 850 | * VT220/VT320/Linux console/xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 851 | retval = NANO_DELETE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 852 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 853 | case '4': /* Esc [ 4 ~ == End on VT220/VT320/Linux |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 854 | * console/xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 855 | retval = NANO_END_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 856 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 857 | case '5': /* Esc [ 5 ~ == PageUp on |
| 858 | * VT220/VT320/Linux console/xterm; Esc [ |
| 859 | * 5 ^ == PageUp on Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 860 | retval = NANO_PREVPAGE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 861 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 862 | case '6': /* Esc [ 6 ~ == PageDown on |
| 863 | * VT220/VT320/Linux console/xterm; Esc [ |
| 864 | * 6 ^ == PageDown on Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 865 | retval = NANO_NEXTPAGE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 866 | break; |
| 867 | case '7': /* Esc [ 7 ~ == Home on rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 868 | retval = NANO_HOME_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 869 | break; |
| 870 | case '8': /* Esc [ 8 ~ == End on rxvt. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 871 | retval = NANO_END_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 872 | break; |
| 873 | case '9': /* Esc [ 9 == Delete on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 874 | retval = NANO_DELETE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 875 | break; |
| 876 | case '@': /* Esc [ @ == Insert on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 877 | retval = NANO_INSERTFILE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 878 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 879 | case 'A': /* Esc [ A == Up on ANSI/VT220/Linux |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 880 | * console/FreeBSD console/Hurd |
| 881 | * console/rxvt/Eterm. */ |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 882 | case 'B': /* Esc [ B == Down on ANSI/VT220/Linux |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 883 | * console/FreeBSD console/Hurd |
| 884 | * console/rxvt/Eterm. */ |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 885 | case 'C': /* Esc [ C == Right on ANSI/VT220/Linux |
| 886 | * console/FreeBSD console/Hurd |
| 887 | * console/rxvt/Eterm. */ |
| 888 | case 'D': /* Esc [ D == Left on ANSI/VT220/Linux |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 889 | * console/FreeBSD console/Hurd |
| 890 | * console/rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 891 | retval = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 892 | break; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 893 | case 'E': /* Esc [ E == Center (5) on numeric keypad |
| 894 | * with NumLock off on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 895 | retval = ERR; |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 896 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 897 | case 'F': /* Esc [ F == End on FreeBSD |
| 898 | * console/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 899 | retval = NANO_END_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 900 | break; |
| 901 | case 'G': /* Esc [ G == PageDown on FreeBSD |
David Lawrence Ramsey | 0a25808 | 2004-04-23 18:02:37 +0000 | [diff] [blame] | 902 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 903 | retval = NANO_NEXTPAGE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 904 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 905 | case 'H': /* Esc [ H == Home on ANSI/VT220/FreeBSD |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 906 | * console/Hurd console/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 907 | retval = NANO_HOME_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 908 | break; |
| 909 | case 'I': /* Esc [ I == PageUp on FreeBSD |
| 910 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 911 | retval = NANO_PREVPAGE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 912 | break; |
David Lawrence Ramsey | 0381c21 | 2004-05-01 01:21:38 +0000 | [diff] [blame] | 913 | case 'L': /* Esc [ L == Insert on ANSI/FreeBSD |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 914 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 915 | retval = NANO_INSERTFILE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 916 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 917 | case 'M': /* Esc [ M == F1 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 918 | retval = KEY_F(1); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 919 | break; |
| 920 | case 'N': /* Esc [ N == F2 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 921 | retval = KEY_F(2); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 922 | break; |
| 923 | case 'O': |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 924 | if (es_len >= 3) { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 925 | switch (escape_seq[2]) { |
| 926 | case 'P': /* Esc [ O P == F1 on |
| 927 | * xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 928 | retval = KEY_F(1); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 929 | break; |
| 930 | case 'Q': /* Esc [ O Q == F2 on |
| 931 | * xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 932 | retval = KEY_F(2); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 933 | break; |
| 934 | case 'R': /* Esc [ O R == F3 on |
| 935 | * xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 936 | retval = KEY_F(3); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 937 | break; |
| 938 | case 'S': /* Esc [ O S == F4 on |
| 939 | * xterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 940 | retval = KEY_F(4); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 941 | break; |
| 942 | default: /* Esc [ O == F3 on |
| 943 | * FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 944 | retval = KEY_F(3); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 945 | break; |
| 946 | } |
| 947 | } |
| 948 | break; |
| 949 | case 'P': /* Esc [ P == F4 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 950 | retval = KEY_F(4); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 951 | break; |
| 952 | case 'Q': /* Esc [ Q == F5 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 953 | retval = KEY_F(5); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 954 | break; |
| 955 | case 'R': /* Esc [ R == F6 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 956 | retval = KEY_F(6); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 957 | break; |
| 958 | case 'S': /* Esc [ S == F7 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 959 | retval = KEY_F(7); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 960 | break; |
| 961 | case 'T': /* Esc [ T == F8 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 962 | retval = KEY_F(8); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 963 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 964 | case 'U': /* Esc [ U == PageDown on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 965 | retval = NANO_NEXTPAGE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 966 | break; |
| 967 | case 'V': /* Esc [ V == PageUp on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 968 | retval = NANO_PREVPAGE_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 969 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 970 | case 'W': /* Esc [ W == F11 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 971 | retval = KEY_F(11); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 972 | break; |
| 973 | case 'X': /* Esc [ X == F12 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 974 | retval = KEY_F(12); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 975 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 976 | case 'Y': /* Esc [ Y == End on Hurd console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 977 | retval = NANO_END_KEY; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 978 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 979 | case 'Z': /* Esc [ Z == F14 on FreeBSD console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 980 | retval = KEY_F(14); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 981 | break; |
David Lawrence Ramsey | 9b5bd42 | 2004-01-06 01:45:04 +0000 | [diff] [blame] | 982 | case 'a': /* Esc [ a == Shift-Up on rxvt/Eterm. */ |
David Lawrence Ramsey | 9b5bd42 | 2004-01-06 01:45:04 +0000 | [diff] [blame] | 983 | case 'b': /* Esc [ b == Shift-Down on rxvt/Eterm. */ |
David Lawrence Ramsey | 9b5bd42 | 2004-01-06 01:45:04 +0000 | [diff] [blame] | 984 | case 'c': /* Esc [ c == Shift-Right on |
| 985 | * rxvt/Eterm. */ |
David Lawrence Ramsey | 9b5bd42 | 2004-01-06 01:45:04 +0000 | [diff] [blame] | 986 | case 'd': /* Esc [ d == Shift-Left on rxvt/Eterm. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 987 | retval = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 988 | break; |
| 989 | case '[': |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 990 | if (es_len >= 3) { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 991 | switch (escape_seq[2]) { |
| 992 | case 'A': /* Esc [ [ A == F1 on Linux |
| 993 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 994 | retval = KEY_F(1); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 995 | break; |
| 996 | case 'B': /* Esc [ [ B == F2 on Linux |
| 997 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 998 | retval = KEY_F(2); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 999 | break; |
| 1000 | case 'C': /* Esc [ [ C == F3 on Linux |
| 1001 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1002 | retval = KEY_F(3); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1003 | break; |
| 1004 | case 'D': /* Esc [ [ D == F4 on Linux |
| 1005 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1006 | retval = KEY_F(4); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1007 | break; |
| 1008 | case 'E': /* Esc [ [ E == F5 on Linux |
| 1009 | * console. */ |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1010 | retval = KEY_F(5); |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1011 | break; |
| 1012 | } |
| 1013 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1014 | break; |
| 1015 | } |
| 1016 | break; |
| 1017 | } |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1018 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1019 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1020 | #ifdef DEBUG |
| 1021 | fprintf(stderr, "get_escape_seq_kbinput(): retval = %d\n", retval); |
| 1022 | #endif |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1023 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1024 | return retval; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1025 | } |
| 1026 | |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1027 | /* Return the equivalent arrow key value for the case-insensitive |
David Lawrence Ramsey | b7e5cf6 | 2004-02-07 03:39:48 +0000 | [diff] [blame] | 1028 | * letters A (up), B (down), C (right), and D (left). These are common |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1029 | * to many escape sequences. */ |
| 1030 | int get_escape_seq_abcd(int kbinput) |
| 1031 | { |
| 1032 | switch (tolower(kbinput)) { |
| 1033 | case 'a': |
| 1034 | return NANO_PREVLINE_KEY; |
| 1035 | case 'b': |
| 1036 | return NANO_NEXTLINE_KEY; |
| 1037 | case 'c': |
| 1038 | return NANO_FORWARD_KEY; |
| 1039 | case 'd': |
| 1040 | return NANO_BACK_KEY; |
| 1041 | default: |
| 1042 | return ERR; |
| 1043 | } |
| 1044 | } |
| 1045 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1046 | /* Read in a string of input characters (e.g. an escape sequence) |
| 1047 | * verbatim. Store the string in v_kbinput and return the length |
| 1048 | * of the string in v_len. Assume nodelay(win) is FALSE. */ |
| 1049 | int *get_verbatim_kbinput(WINDOW *win, int *v_kbinput, size_t |
| 1050 | *v_len, int allow_ascii) |
| 1051 | { |
| 1052 | int kbinput; |
| 1053 | size_t i = 0, v_newlen = 0; |
| 1054 | |
| 1055 | #ifndef NANO_SMALL |
| 1056 | allow_pending_sigwinch(TRUE); |
| 1057 | #endif |
| 1058 | |
| 1059 | *v_len = 0; |
| 1060 | v_kbinput = (int *)nmalloc(sizeof(int)); |
| 1061 | |
| 1062 | /* Turn off flow control characters if necessary so that we can type |
| 1063 | * them in verbatim, and turn the keypad off so that we don't get |
| 1064 | * extended keypad values outside the ASCII range. */ |
| 1065 | if (ISSET(PRESERVE)) |
| 1066 | disable_flow_control(); |
| 1067 | keypad(win, FALSE); |
| 1068 | |
| 1069 | /* Read the first character using blocking input, since using |
| 1070 | * non-blocking input will eat up all unused CPU. Then increment |
| 1071 | * v_len and save the character in v_kbinput. */ |
| 1072 | kbinput = wgetch(win); |
| 1073 | (*v_len)++; |
| 1074 | v_kbinput[0] = kbinput; |
| 1075 | #ifdef DEBUG |
| 1076 | fprintf(stderr, "get_verbatim_kbinput(): kbinput = %d, v_len = %d\n", kbinput, *v_len); |
| 1077 | #endif |
| 1078 | |
| 1079 | /* Read any following characters using non-blocking input, until |
| 1080 | * there aren't any left to be read, and save the complete string of |
| 1081 | * characters in v_kbinput, incrementing v_len accordingly. We read |
| 1082 | * them all at once in order to minimize the chance that there might |
| 1083 | * be a delay greater than nodelay() provides for between them, in |
| 1084 | * which case we'll stop before all of them are read. */ |
| 1085 | nodelay(win, TRUE); |
| 1086 | while ((kbinput = wgetch(win)) != ERR) { |
| 1087 | (*v_len)++; |
| 1088 | v_kbinput = (int *)nrealloc(v_kbinput, *v_len * sizeof(int)); |
| 1089 | v_kbinput[*v_len - 1] = kbinput; |
| 1090 | #ifdef DEBUG |
| 1091 | fprintf(stderr, "get_verbatim_kbinput(): kbinput = %d, v_len = %d\n", kbinput, *v_len); |
| 1092 | #endif |
| 1093 | } |
| 1094 | nodelay(win, FALSE); |
| 1095 | |
| 1096 | /* Pass the string of characters to get_untranslated_kbinput(), one |
| 1097 | * by one, so it can handle them as ASCII character sequences and/or |
| 1098 | * escape sequences. Filter out ERR's from v_kbinput in the |
| 1099 | * process; they shouldn't occur in the string of characters unless |
| 1100 | * we're reading an incomplete sequence, in which case we only want |
| 1101 | * to keep the complete sequence. */ |
| 1102 | for (; i < *v_len; i++) { |
| 1103 | v_kbinput[v_newlen] = get_untranslated_kbinput(v_kbinput[i], i, |
| 1104 | allow_ascii |
| 1105 | #ifndef NANO_SMALL |
| 1106 | , FALSE |
| 1107 | #endif |
| 1108 | ); |
| 1109 | if (v_kbinput[i] != ERR && v_kbinput[v_newlen] != ERR) |
| 1110 | v_newlen++; |
| 1111 | } |
| 1112 | |
| 1113 | if (v_newlen == 0) { |
| 1114 | /* If there were no characters after the ERR's were filtered |
| 1115 | * out, set v_len and reallocate v_kbinput to account for |
| 1116 | * one character, and set that character to ERR. */ |
| 1117 | *v_len = 1; |
| 1118 | v_kbinput = (int *)nrealloc(v_kbinput, sizeof(int)); |
| 1119 | v_kbinput[0] = ERR; |
| 1120 | } else if (v_newlen != *v_len) { |
| 1121 | /* If there were fewer characters after the ERR's were filtered |
| 1122 | * out, set v_len and reallocate v_kbinput to account for |
| 1123 | * the new number of characters. */ |
| 1124 | *v_len = v_newlen; |
| 1125 | v_kbinput = (int *)nrealloc(v_kbinput, *v_len * sizeof(int)); |
| 1126 | } |
| 1127 | |
| 1128 | /* If allow_ascii is TRUE and v_kbinput[0] is ERR, we need to |
| 1129 | * complete an ASCII character sequence. Keep reading in characters |
| 1130 | * using blocking input until we get a complete sequence. */ |
| 1131 | if (allow_ascii && v_kbinput[0] == ERR) { |
| 1132 | while (v_kbinput[0] == ERR) { |
| 1133 | kbinput = wgetch(win); |
| 1134 | v_kbinput[0] = get_untranslated_kbinput(kbinput, i, |
| 1135 | allow_ascii |
| 1136 | #ifndef NANO_SMALL |
| 1137 | , FALSE |
| 1138 | #endif |
| 1139 | ); |
| 1140 | i++; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | /* Turn flow control characters back on if necessary and turn the |
| 1145 | * keypad back on now that we're done. */ |
| 1146 | if (ISSET(PRESERVE)) |
| 1147 | enable_flow_control(); |
| 1148 | keypad(win, TRUE); |
| 1149 | |
| 1150 | #ifndef NANO_SMALL |
| 1151 | allow_pending_sigwinch(FALSE); |
| 1152 | #endif |
| 1153 | |
| 1154 | return v_kbinput; |
| 1155 | } |
| 1156 | |
| 1157 | int get_untranslated_kbinput(int kbinput, size_t position, int |
| 1158 | allow_ascii |
| 1159 | #ifndef NANO_SMALL |
| 1160 | , int reset |
| 1161 | #endif |
| 1162 | ) |
| 1163 | { |
| 1164 | static size_t ascii_digits = 0; |
| 1165 | int retval; |
| 1166 | |
| 1167 | #ifndef NANO_SMALL |
| 1168 | if (reset) { |
| 1169 | ascii_digits = 0; |
| 1170 | return ERR; |
| 1171 | } |
| 1172 | #endif |
| 1173 | |
| 1174 | if (allow_ascii) { |
| 1175 | /* position is equal to the number of ASCII digits we've read so |
| 1176 | * far, and kbinput is a digit from '0' to '9': ASCII character |
| 1177 | * sequence mode. If the digit sequence's range is limited to |
| 1178 | * 2XX (the first digit is in the '0' to '2' range and it's the |
| 1179 | * first digit, or if it's in the full digit range and it's not |
| 1180 | * the first digit), increment the ASCII digit counter and |
| 1181 | * interpret the digit. If the digit sequence's range is not |
| 1182 | * limited to 2XX, fall through. */ |
| 1183 | if (position == ascii_digits && kbinput >= '0' && kbinput <= '9') { |
| 1184 | if (kbinput <= '2' || ascii_digits > 0) { |
| 1185 | ascii_digits++; |
| 1186 | kbinput = get_ascii_kbinput(kbinput, ascii_digits |
| 1187 | #ifndef NANO_SMALL |
| 1188 | , FALSE |
| 1189 | #endif |
| 1190 | ); |
| 1191 | if (kbinput != ERR) |
| 1192 | /* If we've read in a complete ASCII digit sequence, |
| 1193 | * reset the ASCII digit counter. */ |
| 1194 | ascii_digits = 0; |
| 1195 | } |
| 1196 | } else if (ascii_digits > 0) |
| 1197 | /* position is not equal to the number of ASCII digits we've |
| 1198 | * read or kbinput is a non-digit, and we're in the middle |
| 1199 | * of an ASCII character sequence. Reset the ASCII digit |
| 1200 | * counter. */ |
| 1201 | ascii_digits = 0; |
| 1202 | } |
| 1203 | |
| 1204 | /* Save the corresponding ASCII character as the result if we've |
| 1205 | * read in a complete ASCII digit sequence, or the passed-in |
| 1206 | * character if we haven't. */ |
| 1207 | retval = kbinput; |
| 1208 | |
| 1209 | #ifdef DEBUG |
| 1210 | fprintf(stderr, "get_untranslated_kbinput(): kbinput = %d, position = %d, ascii_digits = %d\n", kbinput, position, ascii_digits); |
| 1211 | #endif |
| 1212 | |
| 1213 | return retval; |
| 1214 | } |
| 1215 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1216 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1217 | /* Check for a mouse event, and if one's taken place, save the |
| 1218 | * coordinates where it took place in mouse_x and mouse_y. After that, |
| 1219 | * if allow_shortcuts is zero, return 0. Otherwise, if the mouse event |
| 1220 | * took place on the shortcut list on the bottom two lines of the screen |
| 1221 | * (assuming that the shortcut list is visible), figure out which |
| 1222 | * shortcut was clicked and ungetch() the equivalent keystroke(s). |
| 1223 | * Return 0 if no keystrokes were ungetch()ed, or 1 if at least one was. |
| 1224 | * Assume that KEY_MOUSE has already been read in. */ |
| 1225 | int get_mouseinput(int *mouse_x, int *mouse_y, int allow_shortcuts) |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1226 | { |
| 1227 | MEVENT mevent; |
| 1228 | |
| 1229 | *mouse_x = -1; |
| 1230 | *mouse_y = -1; |
| 1231 | |
| 1232 | /* First, get the actual mouse event. */ |
| 1233 | if (getmouse(&mevent) == ERR) |
| 1234 | return 0; |
| 1235 | |
| 1236 | /* Save the screen coordinates where the mouse event took place. */ |
| 1237 | *mouse_x = mevent.x; |
| 1238 | *mouse_y = mevent.y; |
| 1239 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1240 | /* If we're not allowing shortcuts' we're done now. */ |
| 1241 | if (!allow_shortcuts) |
| 1242 | return 0; |
| 1243 | |
| 1244 | /* Otherwise, if the current shortcut list is being displayed on the |
| 1245 | * last two lines of the screen and the mouse event took place |
| 1246 | * inside it, we need to figure out which shortcut was clicked and |
| 1247 | * ungetch() the equivalent keystroke(s) for it. */ |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1248 | if (!ISSET(NO_HELP) && wenclose(bottomwin, *mouse_y, *mouse_x)) { |
| 1249 | int i, j; |
| 1250 | int currslen; |
| 1251 | /* The number of shortcuts in the current shortcut list. */ |
| 1252 | const shortcut *s = currshortcut; |
| 1253 | /* The actual shortcut we clicked on, starting at the first |
| 1254 | * one in the current shortcut list. */ |
| 1255 | |
| 1256 | /* Get the shortcut lists' length. */ |
| 1257 | if (currshortcut == main_list) |
| 1258 | currslen = MAIN_VISIBLE; |
| 1259 | else |
| 1260 | currslen = length_of_list(currshortcut); |
| 1261 | |
| 1262 | /* Calculate the width of each shortcut in the list (it's the |
| 1263 | * same for all of them). */ |
| 1264 | if (currslen < 2) |
| 1265 | i = COLS / 6; |
| 1266 | else |
| 1267 | i = COLS / ((currslen / 2) + (currslen % 2)); |
| 1268 | |
| 1269 | /* Calculate the y-coordinates relative to the beginning of |
| 1270 | * bottomwin, i.e, the bottom three lines of the screen. */ |
| 1271 | j = *mouse_y - (editwinrows + 3); |
| 1272 | |
| 1273 | /* If we're on the statusbar, beyond the end of the shortcut |
| 1274 | * list, or beyond the end of a shortcut on the right side of |
| 1275 | * the screen, don't do anything. */ |
| 1276 | if (j < 0 || (*mouse_x / i) >= currslen) |
| 1277 | return 0; |
| 1278 | j = (*mouse_x / i) * 2 + j; |
| 1279 | if (j >= currslen) |
| 1280 | return 0; |
| 1281 | |
| 1282 | /* Go through the shortcut list to determine which shortcut was |
| 1283 | * clicked. */ |
| 1284 | for (; j > 0; j--) |
| 1285 | s = s->next; |
| 1286 | |
David Lawrence Ramsey | 32613fa | 2004-05-24 18:40:41 +0000 | [diff] [blame] | 1287 | /* And ungetch() the equivalent control key. If it's a meta key |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 1288 | * sequence, we need to ungetch() Escape too. Assume that the |
| 1289 | * shortcut has an equivalent control key, meta key sequence, or |
| 1290 | * both. */ |
| 1291 | if (s->ctrlval != NANO_NO_KEY) |
| 1292 | ungetch(s->ctrlval); |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1293 | else if (s->ctrlval != NANO_NO_KEY) { |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 1294 | ungetch(s->metaval); |
| 1295 | ungetch(NANO_CONTROL_3); |
| 1296 | } |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1297 | |
| 1298 | return 1; |
| 1299 | } |
| 1300 | return 0; |
| 1301 | } |
| 1302 | #endif |
| 1303 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1304 | /* Return the placewewant associated with current_x. That is, xplustabs |
| 1305 | * is the zero-based column position of the cursor. Value is no smaller |
| 1306 | * than current_x. */ |
| 1307 | size_t xplustabs(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1308 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1309 | return strnlenpt(current->data, current_x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1312 | /* actual_x() gives the index in str of the character displayed at |
| 1313 | * column xplus. That is, actual_x() is the largest value such that |
| 1314 | * strnlenpt(str, actual_x(str, xplus)) <= xplus. */ |
| 1315 | size_t actual_x(const char *str, size_t xplus) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1316 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1317 | size_t i = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1318 | /* the position in str, returned */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1319 | size_t length = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1320 | /* the screen display width to str[i] */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1321 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1322 | assert(str != NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1323 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1324 | for (; length < xplus && *str != '\0'; i++, str++) { |
| 1325 | if (*str == '\t') |
David Lawrence Ramsey | 0362c58 | 2003-09-30 03:31:56 +0000 | [diff] [blame] | 1326 | length += tabsize - (length % tabsize); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1327 | else if (is_cntrl_char((int)*str)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1328 | length += 2; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1329 | else |
| 1330 | length++; |
| 1331 | } |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1332 | assert(length == strnlenpt(str - i, i)); |
| 1333 | assert(i <= strlen(str - i)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1334 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1335 | if (length > xplus) |
| 1336 | i--; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1337 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1338 | return i; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 1339 | } |
| 1340 | |
David Lawrence Ramsey | a3370c4 | 2004-04-05 01:08:14 +0000 | [diff] [blame] | 1341 | /* A strlen() with tabs factored in, similar to xplustabs(). How many |
David Lawrence Ramsey | 5dcba30 | 2003-09-28 19:15:18 +0000 | [diff] [blame] | 1342 | * columns wide are the first size characters of buf? */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1343 | size_t strnlenpt(const char *buf, size_t size) |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 1344 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1345 | size_t length = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1346 | |
David Lawrence Ramsey | 5dcba30 | 2003-09-28 19:15:18 +0000 | [diff] [blame] | 1347 | assert(buf != NULL); |
| 1348 | for (; *buf != '\0' && size != 0; size--, buf++) { |
| 1349 | if (*buf == '\t') |
| 1350 | length += tabsize - (length % tabsize); |
| 1351 | else if (is_cntrl_char((int)*buf)) |
| 1352 | length += 2; |
| 1353 | else |
| 1354 | length++; |
| 1355 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1356 | return length; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1357 | } |
| 1358 | |
David Lawrence Ramsey | 5dcba30 | 2003-09-28 19:15:18 +0000 | [diff] [blame] | 1359 | /* How many columns wide is buf? */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1360 | size_t strlenpt(const char *buf) |
Chris Allegretta | d4fa0d3 | 2002-03-05 19:55:55 +0000 | [diff] [blame] | 1361 | { |
David Lawrence Ramsey | 0b047c5 | 2004-03-29 23:09:08 +0000 | [diff] [blame] | 1362 | return strnlenpt(buf, (size_t)-1); |
Chris Allegretta | d4fa0d3 | 2002-03-05 19:55:55 +0000 | [diff] [blame] | 1363 | } |
| 1364 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1365 | void blank_titlebar(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1366 | { |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1367 | mvwaddstr(topwin, 0, 0, hblank); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1368 | } |
| 1369 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1370 | void blank_edit(void) |
| 1371 | { |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1372 | size_t i; |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 1373 | for (i = 0; i < editwinrows; i++) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1374 | mvwaddstr(edit, i, 0, hblank); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1377 | void blank_statusbar(void) |
| 1378 | { |
| 1379 | mvwaddstr(bottomwin, 0, 0, hblank); |
| 1380 | } |
| 1381 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1382 | void check_statblank(void) |
| 1383 | { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1384 | if (statblank > 1) |
| 1385 | statblank--; |
| 1386 | else if (statblank == 1 && !ISSET(CONSTUPDATE)) { |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1387 | statblank = 0; |
| 1388 | blank_statusbar(); |
| 1389 | wnoutrefresh(bottomwin); |
| 1390 | reset_cursor(); |
| 1391 | wrefresh(edit); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1392 | } |
| 1393 | } |
| 1394 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1395 | void blank_bottombars(void) |
| 1396 | { |
| 1397 | if (!ISSET(NO_HELP)) { |
| 1398 | mvwaddstr(bottomwin, 1, 0, hblank); |
| 1399 | mvwaddstr(bottomwin, 2, 0, hblank); |
| 1400 | } |
| 1401 | } |
| 1402 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1403 | /* Convert buf into a string that can be displayed on screen. The |
| 1404 | * caller wants to display buf starting with column start_col, and |
| 1405 | * extending for at most len columns. start_col is zero-based. len is |
| 1406 | * one-based, so len == 0 means you get "" returned. The returned |
| 1407 | * string is dynamically allocated, and should be freed. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1408 | char *display_string(const char *buf, size_t start_col, size_t len) |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1409 | { |
| 1410 | size_t start_index; |
| 1411 | /* Index in buf of first character shown in return value. */ |
| 1412 | size_t column; |
| 1413 | /* Screen column start_index corresponds to. */ |
| 1414 | size_t end_index; |
| 1415 | /* Index in buf of last character shown in return value. */ |
| 1416 | size_t alloc_len; |
| 1417 | /* The length of memory allocated for converted. */ |
| 1418 | char *converted; |
| 1419 | /* The string we return. */ |
| 1420 | size_t index; |
| 1421 | /* Current position in converted. */ |
| 1422 | |
| 1423 | if (len == 0) |
| 1424 | return mallocstrcpy(NULL, ""); |
| 1425 | |
| 1426 | start_index = actual_x(buf, start_col); |
| 1427 | column = strnlenpt(buf, start_index); |
| 1428 | assert(column <= start_col); |
| 1429 | end_index = actual_x(buf, start_col + len - 1); |
| 1430 | alloc_len = strnlenpt(buf, end_index + 1) - column; |
| 1431 | if (len > alloc_len + column - start_col) |
| 1432 | len = alloc_len + column - start_col; |
| 1433 | converted = charalloc(alloc_len + 1); |
| 1434 | buf += start_index; |
| 1435 | index = 0; |
| 1436 | |
| 1437 | for (; index < alloc_len; buf++) { |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 1438 | if (*buf == '\t') { |
| 1439 | converted[index++] = |
| 1440 | #if !defined(NANO_SMALL) && defined(ENABLE_NANORC) |
| 1441 | ISSET(WHITESPACE_DISPLAY) ? whitespace[0] : |
| 1442 | #endif |
| 1443 | ' '; |
| 1444 | while ((column + index) % tabsize) |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1445 | converted[index++] = ' '; |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 1446 | } else if (is_cntrl_char(*buf)) { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1447 | converted[index++] = '^'; |
| 1448 | if (*buf == '\n') |
| 1449 | /* Treat newlines embedded in a line as encoded nulls; |
| 1450 | * the line in question should be run through unsunder() |
| 1451 | * before reaching here. */ |
| 1452 | converted[index++] = '@'; |
| 1453 | else if (*buf == NANO_CONTROL_8) |
| 1454 | converted[index++] = '?'; |
| 1455 | else |
| 1456 | converted[index++] = *buf + 64; |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 1457 | } else if (*buf == ' ') |
| 1458 | converted[index++] = |
| 1459 | #if !defined(NANO_SMALL) && defined(ENABLE_NANORC) |
| 1460 | ISSET(WHITESPACE_DISPLAY) ? whitespace[1] : |
| 1461 | #endif |
| 1462 | ' '; |
| 1463 | else |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1464 | converted[index++] = *buf; |
| 1465 | } |
| 1466 | assert(len <= alloc_len + column - start_col); |
| 1467 | charmove(converted, converted + start_col - column, len); |
| 1468 | null_at(&converted, len); |
| 1469 | |
| 1470 | return charealloc(converted, len + 1); |
| 1471 | } |
| 1472 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1473 | /* Repaint the statusbar when getting a character in nanogetstr(). buf |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1474 | * should be no longer than max(0, COLS - 4). |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1475 | * |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1476 | * Note that we must turn on A_REVERSE here, since do_help() turns it |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1477 | * off! */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1478 | void nanoget_repaint(const char *buf, const char *inputbuf, size_t x) |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 1479 | { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1480 | size_t x_real = strnlenpt(inputbuf, x); |
| 1481 | int wid = COLS - strlen(buf) - 2; |
Chris Allegretta | 0d1e8d6 | 2000-11-02 15:30:24 +0000 | [diff] [blame] | 1482 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1483 | assert(0 <= x && x <= strlen(inputbuf)); |
| 1484 | |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1485 | wattron(bottomwin, A_REVERSE); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 1486 | blank_statusbar(); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1487 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1488 | mvwaddstr(bottomwin, 0, 0, buf); |
| 1489 | waddch(bottomwin, ':'); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1490 | |
| 1491 | if (COLS > 1) |
| 1492 | waddch(bottomwin, x_real < wid ? ' ' : '$'); |
| 1493 | if (COLS > 2) { |
| 1494 | size_t page_start = x_real - x_real % wid; |
| 1495 | char *expanded = display_string(inputbuf, page_start, wid); |
| 1496 | |
| 1497 | assert(wid > 0); |
| 1498 | assert(strlen(expanded) <= wid); |
| 1499 | waddstr(bottomwin, expanded); |
| 1500 | free(expanded); |
| 1501 | wmove(bottomwin, 0, COLS - wid + x_real - page_start); |
| 1502 | } else |
| 1503 | wmove(bottomwin, 0, COLS - 1); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1504 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 1505 | } |
| 1506 | |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 1507 | /* Get the input from the keyboard; this should only be called from |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1508 | * statusq(). */ |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1509 | int nanogetstr(int allowtabs, const char *buf, const char *def, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1510 | #ifndef NANO_SMALL |
| 1511 | historyheadtype *history_list, |
| 1512 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1513 | const shortcut *s |
Rocco Corsi | 06aca1c | 2001-01-11 05:30:31 +0000 | [diff] [blame] | 1514 | #ifndef DISABLE_TABCOMP |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1515 | , int *list |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1516 | #endif |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1517 | ) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1518 | { |
| 1519 | int kbinput; |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 1520 | int meta_key; |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1521 | static int x = -1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1522 | /* the cursor position in 'answer' */ |
| 1523 | int xend; |
| 1524 | /* length of 'answer', the status bar text */ |
| 1525 | int tabbed = 0; |
| 1526 | /* used by input_tab() */ |
| 1527 | const shortcut *t; |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1528 | |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1529 | #ifndef NANO_SMALL |
| 1530 | /* for history */ |
| 1531 | char *history = NULL; |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1532 | char *currentbuf = NULL; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1533 | char *complete = NULL; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1534 | int last_kbinput = 0; |
| 1535 | |
| 1536 | /* This variable is used in the search history code. use_cb == 0 |
| 1537 | means that we're using the existing history and ignoring |
| 1538 | currentbuf. use_cb == 1 means that the entry in answer should be |
| 1539 | moved to currentbuf or restored from currentbuf to answer. |
| 1540 | use_cb == 2 means that the entry in currentbuf should be moved to |
| 1541 | answer or restored from answer to currentbuf. */ |
| 1542 | int use_cb = 0; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1543 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1544 | xend = strlen(def); |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1545 | |
| 1546 | /* Only put x at the end of the string if it's uninitialized or if |
| 1547 | it would be past the end of the string as it is. Otherwise, |
| 1548 | leave it alone. This is so the cursor position stays at the same |
| 1549 | place if a prompt-changing toggle is pressed. */ |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1550 | if (x == -1 || x > xend || resetstatuspos) |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1551 | x = xend; |
| 1552 | |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 1553 | answer = charealloc(answer, xend + 1); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1554 | if (xend > 0) |
| 1555 | strcpy(answer, def); |
| 1556 | else |
| 1557 | answer[0] = '\0'; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1558 | |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 1559 | #if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE) |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1560 | currshortcut = s; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1561 | #endif |
| 1562 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1563 | /* Get the input! */ |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 1564 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1565 | nanoget_repaint(buf, answer, x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1566 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1567 | /* Make sure any editor screen updates are displayed before getting |
| 1568 | input */ |
Chris Allegretta | 022b96f | 2000-11-14 17:47:58 +0000 | [diff] [blame] | 1569 | wrefresh(edit); |
| 1570 | |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 1571 | /* If we're using restricted mode, we aren't allowed to change the |
| 1572 | * name of a file once it has one because that would allow writing |
| 1573 | * to files not specified on the command line. In this case, |
| 1574 | * disable all keys that would change the text if the filename isn't |
| 1575 | * blank and we're at the "Write File" prompt. */ |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 1576 | while ((kbinput = get_kbinput(bottomwin, &meta_key)) != NANO_ENTER_KEY) { |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1577 | for (t = s; t != NULL; t = t->next) { |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1578 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 1579 | fprintf(stderr, "Aha! \'%c\' (%d)\n", kbinput, kbinput); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1580 | #endif |
| 1581 | |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 1582 | /* Temporary hack to interpret NANO_HELP_FKEY correctly. */ |
| 1583 | if (kbinput == t->funcval) |
| 1584 | kbinput = t->ctrlval; |
David Lawrence Ramsey | d7f5ad9 | 2004-03-04 19:30:53 +0000 | [diff] [blame] | 1585 | |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 1586 | if (kbinput == t->ctrlval && is_cntrl_char(kbinput)) { |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 1587 | |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1588 | #ifndef DISABLE_HELP |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1589 | /* Have to do this here, it would be too late to do it |
| 1590 | in statusq() */ |
David Lawrence Ramsey | d7f5ad9 | 2004-03-04 19:30:53 +0000 | [diff] [blame] | 1591 | if (kbinput == NANO_HELP_KEY) { |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1592 | do_help(); |
| 1593 | break; |
| 1594 | } |
| 1595 | #endif |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1596 | #ifndef NANO_SMALL |
| 1597 | /* Have to handle these here too, for the time being */ |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1598 | if (kbinput == NANO_PREVLINE_KEY || kbinput == NANO_NEXTLINE_KEY) |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1599 | break; |
| 1600 | #endif |
Chris Allegretta | 5af5889 | 2003-01-17 21:07:38 +0000 | [diff] [blame] | 1601 | |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 1602 | return t->ctrlval; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1603 | } |
| 1604 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1605 | assert(0 <= x && x <= xend && xend == strlen(answer)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1606 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1607 | if (kbinput != '\t') |
| 1608 | tabbed = 0; |
| 1609 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1610 | switch (kbinput) { |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 1611 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1612 | case KEY_MOUSE: |
| 1613 | do_mouse(); |
| 1614 | break; |
| 1615 | #endif |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1616 | case NANO_HOME_KEY: |
David Lawrence Ramsey | c7acf69 | 2004-05-22 20:15:20 +0000 | [diff] [blame] | 1617 | #ifndef NANO_SMALL |
| 1618 | if (ISSET(SMART_HOME)) { |
| 1619 | int old_x = x; |
| 1620 | |
| 1621 | for (x = 0; isblank(answer[x]) && x < xend; x++) |
| 1622 | ; |
| 1623 | |
| 1624 | if (x == old_x || x == xend) |
| 1625 | x = 0; |
| 1626 | } else |
| 1627 | #endif |
| 1628 | x = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1629 | break; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1630 | case NANO_END_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1631 | x = xend; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1632 | break; |
Chris Allegretta | 35dac58 | 2001-03-21 15:07:20 +0000 | [diff] [blame] | 1633 | case NANO_FORWARD_KEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1634 | if (x < xend) |
| 1635 | x++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1636 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1637 | case NANO_DELETE_KEY: |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 1638 | /* If we're using restricted mode, the filename isn't blank, |
| 1639 | * and we're at the "Write File" prompt, disable Delete. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 1640 | if (!ISSET(RESTRICTED) || filename[0] == '\0' || s != writefile_list) { |
| 1641 | if (x < xend) { |
| 1642 | charmove(answer + x, answer + x + 1, xend - x); |
| 1643 | xend--; |
| 1644 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1645 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1646 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1647 | case NANO_CUT_KEY: |
| 1648 | case NANO_UNCUT_KEY: |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 1649 | /* If we're using restricted mode, the filename isn't blank, |
| 1650 | * and we're at the "Write File" prompt, disable Cut and |
| 1651 | * UnCut. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 1652 | if (!ISSET(RESTRICTED) || filename[0] == '\0' || s != writefile_list) { |
| 1653 | null_at(&answer, 0); |
| 1654 | xend = 0; |
| 1655 | x = 0; |
| 1656 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1657 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1658 | case NANO_BACKSPACE_KEY: |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 1659 | /* If we're using restricted mode, the filename isn't blank, |
| 1660 | * and we're at the "Write File" prompt, disable |
| 1661 | * Backspace. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 1662 | if (!ISSET(RESTRICTED) || filename[0] == '\0' || s != writefile_list) { |
| 1663 | if (x > 0) { |
| 1664 | charmove(answer + x - 1, answer + x, xend - x + 1); |
| 1665 | x--; |
| 1666 | xend--; |
| 1667 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1668 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1669 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1670 | case NANO_TAB_KEY: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1671 | #ifndef NANO_SMALL |
| 1672 | /* tab history completion */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1673 | if (history_list != NULL) { |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1674 | if (!complete || last_kbinput != NANO_TAB_KEY) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1675 | history_list->current = (historytype *)history_list; |
| 1676 | history_list->len = strlen(answer); |
| 1677 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1678 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1679 | if (history_list->len > 0) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1680 | complete = get_history_completion(history_list, answer); |
| 1681 | xend = strlen(complete); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1682 | x = xend; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1683 | answer = mallocstrcpy(answer, complete); |
| 1684 | } |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 1685 | } |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1686 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 1687 | else |
| 1688 | #endif |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1689 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1690 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 1691 | if (allowtabs) { |
| 1692 | int shift = 0; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1693 | |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 1694 | answer = input_tab(answer, x, &tabbed, &shift, list); |
| 1695 | xend = strlen(answer); |
| 1696 | x += shift; |
| 1697 | if (x > xend) |
| 1698 | x = xend; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1699 | } |
| 1700 | #endif |
| 1701 | break; |
Chris Allegretta | 35dac58 | 2001-03-21 15:07:20 +0000 | [diff] [blame] | 1702 | case NANO_BACK_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1703 | if (x > 0) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1704 | x--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1705 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1706 | case NANO_PREVLINE_KEY: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1707 | #ifndef NANO_SMALL |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1708 | if (history_list != NULL) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1709 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1710 | /* if currentbuf is NULL, or if use_cb is 1, currentbuf |
| 1711 | isn't NULL, and currentbuf is different from answer, |
| 1712 | it means that we're scrolling up at the top of the |
| 1713 | search history, and we need to save the current |
| 1714 | answer in currentbuf; do this and reset use_cb to |
| 1715 | 0 */ |
| 1716 | if (currentbuf == NULL || (use_cb == 1 && strcmp(currentbuf, answer))) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1717 | currentbuf = mallocstrcpy(currentbuf, answer); |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1718 | use_cb = 0; |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1719 | } |
| 1720 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1721 | /* if currentbuf isn't NULL, use_cb is 2, and currentbuf |
| 1722 | is different from answer, it means that we're |
| 1723 | scrolling up at the bottom of the search history, and |
| 1724 | we need to make the string in currentbuf the current |
| 1725 | answer; do this, blow away currentbuf since we don't |
| 1726 | need it anymore, and reset use_cb to 0 */ |
| 1727 | if (currentbuf != NULL && use_cb == 2 && strcmp(currentbuf, answer)) { |
| 1728 | answer = mallocstrcpy(answer, currentbuf); |
| 1729 | free(currentbuf); |
| 1730 | currentbuf = NULL; |
| 1731 | xend = strlen(answer); |
| 1732 | use_cb = 0; |
| 1733 | |
| 1734 | /* else get older search from the history list and save |
| 1735 | it in answer; if there is no older search, blank out |
| 1736 | answer */ |
| 1737 | } else if ((history = get_history_older(history_list)) != NULL) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1738 | answer = mallocstrcpy(answer, history); |
| 1739 | xend = strlen(history); |
| 1740 | } else { |
| 1741 | answer = mallocstrcpy(answer, ""); |
| 1742 | xend = 0; |
| 1743 | } |
| 1744 | x = xend; |
| 1745 | } |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1746 | #endif |
Chris Allegretta | 54abd94 | 2003-01-09 23:43:12 +0000 | [diff] [blame] | 1747 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1748 | case NANO_NEXTLINE_KEY: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1749 | #ifndef NANO_SMALL |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1750 | if (history_list != NULL) { |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1751 | |
| 1752 | /* get newer search from the history list and save it |
| 1753 | in answer */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1754 | if ((history = get_history_newer(history_list)) != NULL) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1755 | answer = mallocstrcpy(answer, history); |
| 1756 | xend = strlen(history); |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1757 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1758 | /* if there is no newer search, we're here */ |
| 1759 | |
| 1760 | /* if currentbuf isn't NULL and use_cb isn't 2, it means |
| 1761 | that we're scrolling down at the bottom of the search |
| 1762 | history and we need to make the string in currentbuf |
| 1763 | the current answer; do this, blow away currentbuf |
| 1764 | since we don't need it anymore, and set use_cb to |
| 1765 | 1 */ |
| 1766 | } else if (currentbuf != NULL && use_cb != 2) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1767 | answer = mallocstrcpy(answer, currentbuf); |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1768 | free(currentbuf); |
| 1769 | currentbuf = NULL; |
| 1770 | xend = strlen(answer); |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1771 | use_cb = 1; |
| 1772 | |
| 1773 | /* otherwise, if currentbuf is NULL and use_cb isn't 2, |
| 1774 | it means that we're scrolling down at the bottom of |
Chris Allegretta | c30fc24 | 2003-08-11 00:32:45 +0000 | [diff] [blame] | 1775 | the search history and the current answer (if it's |
| 1776 | not blank) needs to be saved in currentbuf; do this, |
| 1777 | blank out answer (if necessary), and set use_cb to |
| 1778 | 2 */ |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1779 | } else if (use_cb != 2) { |
Chris Allegretta | c30fc24 | 2003-08-11 00:32:45 +0000 | [diff] [blame] | 1780 | if (answer[0] != '\0') { |
| 1781 | currentbuf = mallocstrcpy(currentbuf, answer); |
| 1782 | answer = mallocstrcpy(answer, ""); |
| 1783 | } |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1784 | xend = 0; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1785 | use_cb = 2; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1786 | } |
| 1787 | x = xend; |
| 1788 | } |
| 1789 | #endif |
| 1790 | break; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1791 | default: |
| 1792 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1793 | for (t = s; t != NULL; t = t->next) { |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1794 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 1795 | fprintf(stderr, "Aha! \'%c\' (%d)\n", kbinput, |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1796 | kbinput); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1797 | #endif |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 1798 | if (meta_key == TRUE && (kbinput == t->metaval || kbinput == t->miscval)) |
David Lawrence Ramsey | 32613fa | 2004-05-24 18:40:41 +0000 | [diff] [blame] | 1799 | /* We hit a meta key. Do like above. We don't |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1800 | * just ungetch() the letter and let it get |
| 1801 | * caught above cause that screws the |
| 1802 | * keypad... */ |
| 1803 | return kbinput; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1804 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1805 | |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 1806 | /* If we're using restricted mode, the filename isn't blank, |
| 1807 | * and we're at the "Write File" prompt, act as though the |
| 1808 | * unhandled character we got is a control character and |
| 1809 | * throw it away. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 1810 | if (is_cntrl_char(kbinput) || (ISSET(RESTRICTED) && filename[0] != '\0' && s == writefile_list)) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1811 | break; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1812 | answer = charealloc(answer, xend + 2); |
David Lawrence Ramsey | 9eff746 | 2003-09-16 02:04:00 +0000 | [diff] [blame] | 1813 | charmove(answer + x + 1, answer + x, xend - x + 1); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1814 | xend++; |
| 1815 | answer[x] = kbinput; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1816 | x++; |
| 1817 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1818 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 1819 | fprintf(stderr, "input \'%c\' (%d)\n", kbinput, kbinput); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1820 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1821 | } /* switch (kbinput) */ |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 1822 | #ifndef NANO_SMALL |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1823 | last_kbinput = kbinput; |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 1824 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1825 | nanoget_repaint(buf, answer, x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1826 | wrefresh(bottomwin); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1827 | } /* while (kbinput ...) */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1828 | |
Chris Allegretta | 5af5889 | 2003-01-17 21:07:38 +0000 | [diff] [blame] | 1829 | /* We finished putting in an answer; reset x */ |
| 1830 | x = -1; |
| 1831 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1832 | /* Just check for a blank answer here */ |
Chris Allegretta | 15c28f8 | 2003-01-05 21:47:06 +0000 | [diff] [blame] | 1833 | if (answer[0] == '\0') |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1834 | return -2; |
| 1835 | else |
| 1836 | return 0; |
| 1837 | } |
| 1838 | |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1839 | void titlebar(const char *path) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1840 | { |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1841 | size_t space; |
| 1842 | /* The space we have available for display. */ |
| 1843 | size_t verlen = strlen(VERMSG) + 1; |
| 1844 | /* The length of the version message. */ |
| 1845 | const char *prefix; |
| 1846 | /* "File:", "Dir:", or "New Buffer". Goes before filename. */ |
| 1847 | size_t prefixlen; |
| 1848 | /* strlen(prefix) + 1. */ |
| 1849 | const char *state; |
| 1850 | /* "Modified", "View", or spaces the length of "Modified". |
| 1851 | * Tells the state of this buffer. */ |
| 1852 | size_t statelen = 0; |
| 1853 | /* strlen(state) + 1. */ |
| 1854 | char *exppath = NULL; |
| 1855 | /* The file name, expanded for display. */ |
| 1856 | size_t explen = 0; |
| 1857 | /* strlen(exppath) + 1. */ |
| 1858 | int newbuffer = FALSE; |
| 1859 | /* Do we say "New Buffer"? */ |
| 1860 | int dots = FALSE; |
| 1861 | /* Do we put an ellipsis before the path? */ |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1862 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1863 | assert(path != NULL || filename != NULL); |
| 1864 | assert(COLS >= 0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1865 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1866 | wattron(topwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1867 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1868 | blank_titlebar(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1869 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1870 | if (COLS <= 5 || COLS - 5 < verlen) |
| 1871 | space = 0; |
| 1872 | else { |
| 1873 | space = COLS - 5 - verlen; |
David Lawrence Ramsey | 8328fc2 | 2004-05-25 23:34:43 +0000 | [diff] [blame] | 1874 | /* Reserve 2/3 of the screen plus one column for after the |
| 1875 | * version message. */ |
| 1876 | if (space < COLS - (COLS / 3) + 1) |
| 1877 | space = COLS - (COLS / 3) + 1; |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1878 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1879 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1880 | if (COLS > 4) { |
David Lawrence Ramsey | 8328fc2 | 2004-05-25 23:34:43 +0000 | [diff] [blame] | 1881 | /* The version message should only take up 1/3 of the screen |
| 1882 | * minus one column. */ |
| 1883 | mvwaddnstr(topwin, 0, 2, VERMSG, (COLS / 3) - 3); |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1884 | waddstr(topwin, " "); |
| 1885 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1886 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1887 | if (ISSET(MODIFIED)) |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1888 | state = _("Modified"); |
| 1889 | else if (path == NULL && ISSET(VIEW_MODE)) |
| 1890 | state = _("View"); |
| 1891 | else { |
| 1892 | if (space > 0) |
| 1893 | statelen = strnlen(_("Modified"), space - 1) + 1; |
| 1894 | state = &hblank[COLS - statelen]; |
| 1895 | } |
| 1896 | statelen = strnlen(state, COLS); |
| 1897 | /* We need a space before state. */ |
| 1898 | if ((ISSET(MODIFIED) || ISSET(VIEW_MODE)) && statelen < COLS) |
| 1899 | statelen++; |
| 1900 | |
| 1901 | assert(space >= 0); |
| 1902 | if (space == 0 || statelen >= space) |
| 1903 | goto the_end; |
| 1904 | |
| 1905 | #ifndef DISABLE_BROWSER |
| 1906 | if (path != NULL) |
| 1907 | prefix = _("DIR:"); |
| 1908 | else |
| 1909 | #endif |
| 1910 | if (filename[0] == '\0') { |
| 1911 | prefix = _("New Buffer"); |
| 1912 | newbuffer = TRUE; |
| 1913 | } else |
| 1914 | prefix = _("File:"); |
| 1915 | assert(statelen < space); |
| 1916 | prefixlen = strnlen(prefix, space - statelen); |
| 1917 | /* If newbuffer is FALSE, we need a space after prefix. */ |
| 1918 | if (!newbuffer && prefixlen + statelen < space) |
| 1919 | prefixlen++; |
| 1920 | |
| 1921 | if (path == NULL) |
| 1922 | path = filename; |
David Lawrence Ramsey | 1dcf36a | 2004-05-23 21:17:56 +0000 | [diff] [blame] | 1923 | space -= prefixlen + statelen; |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1924 | /* space is now the room we have for the file name. */ |
| 1925 | if (!newbuffer) { |
| 1926 | size_t lenpt = strlenpt(path), start_col; |
| 1927 | |
| 1928 | if (lenpt > space) |
| 1929 | start_col = actual_x(path, lenpt - space); |
| 1930 | else |
| 1931 | start_col = 0; |
| 1932 | exppath = display_string(path, start_col, space); |
| 1933 | dots = (lenpt > space); |
| 1934 | explen = strlen(exppath); |
| 1935 | } |
| 1936 | |
| 1937 | if (!dots) { |
| 1938 | /* There is room for the whole filename, so we center it. */ |
| 1939 | waddnstr(topwin, hblank, (space - explen) / 3); |
| 1940 | waddnstr(topwin, prefix, prefixlen); |
| 1941 | if (!newbuffer) { |
| 1942 | assert(strlen(prefix) + 1 == prefixlen); |
| 1943 | waddch(topwin, ' '); |
| 1944 | waddstr(topwin, exppath); |
| 1945 | } |
| 1946 | } else { |
| 1947 | /* We will say something like "File: ...ename". */ |
| 1948 | waddnstr(topwin, prefix, prefixlen); |
David Lawrence Ramsey | 1dcf36a | 2004-05-23 21:17:56 +0000 | [diff] [blame] | 1949 | if (space <= 0 || newbuffer) |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1950 | goto the_end; |
| 1951 | waddch(topwin, ' '); |
| 1952 | waddnstr(topwin, "...", space); |
| 1953 | if (space <= 3) |
| 1954 | goto the_end; |
| 1955 | space -= 3; |
| 1956 | assert(explen = space + 3); |
| 1957 | waddnstr(topwin, exppath + 3, space); |
| 1958 | } |
| 1959 | |
| 1960 | the_end: |
| 1961 | |
| 1962 | free(exppath); |
| 1963 | |
| 1964 | if (COLS <= 1 || statelen >= COLS - 1) |
| 1965 | mvwaddnstr(topwin, 0, 0, state, COLS); |
| 1966 | else { |
| 1967 | assert(COLS - statelen - 2 >= 0); |
| 1968 | mvwaddch(topwin, 0, COLS - statelen - 2, ' '); |
| 1969 | mvwaddnstr(topwin, 0, COLS - statelen - 1, state, statelen); |
| 1970 | } |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1971 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1972 | wattroff(topwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1973 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1974 | wnoutrefresh(topwin); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1975 | reset_cursor(); |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1976 | wrefresh(edit); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1977 | } |
| 1978 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1979 | /* If modified is not already set, set it and update titlebar. */ |
| 1980 | void set_modified(void) |
| 1981 | { |
| 1982 | if (!ISSET(MODIFIED)) { |
| 1983 | SET(MODIFIED); |
| 1984 | titlebar(NULL); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1985 | } |
| 1986 | } |
| 1987 | |
| 1988 | void statusbar(const char *msg, ...) |
| 1989 | { |
| 1990 | va_list ap; |
| 1991 | |
| 1992 | va_start(ap, msg); |
| 1993 | |
| 1994 | /* Curses mode is turned off. If we use wmove() now, it will muck |
| 1995 | * up the terminal settings. So we just use vfprintf(). */ |
| 1996 | if (curses_ended) { |
| 1997 | vfprintf(stderr, msg, ap); |
| 1998 | va_end(ap); |
| 1999 | return; |
| 2000 | } |
| 2001 | |
| 2002 | /* Blank out the line. */ |
| 2003 | blank_statusbar(); |
| 2004 | |
| 2005 | if (COLS >= 4) { |
| 2006 | char *bar; |
| 2007 | char *foo; |
| 2008 | size_t start_x = 0, foo_len; |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 2009 | #if !defined(NANO_SMALL) && defined(ENABLE_NANORC) |
| 2010 | int old_whitespace = ISSET(WHITESPACE_DISPLAY); |
| 2011 | UNSET(WHITESPACE_DISPLAY); |
| 2012 | #endif |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2013 | bar = charalloc(COLS - 3); |
| 2014 | vsnprintf(bar, COLS - 3, msg, ap); |
| 2015 | va_end(ap); |
| 2016 | foo = display_string(bar, 0, COLS - 4); |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 2017 | #if !defined(NANO_SMALL) && defined(ENABLE_NANORC) |
| 2018 | if (old_whitespace) |
| 2019 | SET(WHITESPACE_DISPLAY); |
| 2020 | #endif |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2021 | free(bar); |
| 2022 | foo_len = strlen(foo); |
| 2023 | start_x = (COLS - foo_len - 4) / 2; |
| 2024 | |
| 2025 | wmove(bottomwin, 0, start_x); |
| 2026 | wattron(bottomwin, A_REVERSE); |
| 2027 | |
| 2028 | waddstr(bottomwin, "[ "); |
| 2029 | waddstr(bottomwin, foo); |
| 2030 | free(foo); |
| 2031 | waddstr(bottomwin, " ]"); |
| 2032 | wattroff(bottomwin, A_REVERSE); |
| 2033 | wnoutrefresh(bottomwin); |
| 2034 | reset_cursor(); |
| 2035 | wrefresh(edit); |
| 2036 | /* Leave the cursor at its position in the edit window, not |
| 2037 | * in the statusbar. */ |
| 2038 | } |
| 2039 | |
| 2040 | SET(DISABLE_CURPOS); |
| 2041 | statblank = 26; |
| 2042 | } |
| 2043 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2044 | void bottombars(const shortcut *s) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2045 | { |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2046 | size_t i, colwidth, slen; |
| 2047 | char *keystr; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2048 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2049 | if (ISSET(NO_HELP)) |
| 2050 | return; |
| 2051 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2052 | if (s == main_list) { |
| 2053 | slen = MAIN_VISIBLE; |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2054 | assert(slen <= length_of_list(s)); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2055 | } else { |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2056 | slen = length_of_list(s); |
| 2057 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2058 | /* Don't show any more shortcuts than the main list does. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2059 | if (slen > MAIN_VISIBLE) |
| 2060 | slen = MAIN_VISIBLE; |
| 2061 | } |
| 2062 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2063 | /* There will be this many characters per column. We need at least |
| 2064 | * 3 to display anything properly.*/ |
| 2065 | colwidth = COLS / ((slen / 2) + (slen % 2)); |
| 2066 | keystr = charalloc(colwidth); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2067 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2068 | blank_bottombars(); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 2069 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2070 | for (i = 0; i < slen; i++, s = s->next) { |
| 2071 | wmove(bottomwin, 1 + i % 2, (i / 2) * colwidth); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 2072 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2073 | /* Yucky sentinel values we can't handle a better way. */ |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 2074 | #ifndef NANO_SMALL |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2075 | if (s->ctrlval == NANO_HISTORY_KEY) |
| 2076 | strncpy(keystr, _("Up"), colwidth); |
| 2077 | else |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2078 | #endif |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2079 | if (s->ctrlval == NANO_CONTROL_SPACE) |
| 2080 | strncpy(keystr, "^ ", colwidth); |
| 2081 | else if (s->ctrlval == NANO_CONTROL_8) |
| 2082 | strncpy(keystr, "^?", colwidth); |
| 2083 | /* Normal values. Assume that the shortcut has an equivalent |
| 2084 | * control key, meta key sequence, or both. */ |
| 2085 | else if (s->ctrlval != NANO_NO_KEY) |
| 2086 | snprintf(keystr, colwidth, "^%c", s->ctrlval + 64); |
| 2087 | else if (s->metaval != NANO_NO_KEY) |
| 2088 | snprintf(keystr, colwidth, "M-%c", toupper(s->metaval)); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 2089 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2090 | onekey(keystr, s->desc, colwidth); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2091 | } |
| 2092 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2093 | free(keystr); |
| 2094 | |
| 2095 | wnoutrefresh(bottomwin); |
| 2096 | reset_cursor(); |
| 2097 | wrefresh(edit); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2100 | /* Write a shortcut key to the help area at the bottom of the window. |
| 2101 | * keystroke is e.g. "^G" and desc is e.g. "Get Help". We are careful |
| 2102 | * to write at most len characters, even if len is very small and |
| 2103 | * keystroke and desc are long. Note that waddnstr(,,(size_t)-1) adds |
| 2104 | * the whole string! We do not bother padding the entry with blanks. */ |
| 2105 | void onekey(const char *keystroke, const char *desc, size_t len) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2106 | { |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2107 | assert(keystroke != NULL && desc != NULL && len >= 0); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2108 | wattron(bottomwin, A_REVERSE); |
| 2109 | waddnstr(bottomwin, keystroke, len); |
| 2110 | wattroff(bottomwin, A_REVERSE); |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2111 | len -= strlen(keystroke) + 1; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2112 | if (len > 0) { |
| 2113 | waddch(bottomwin, ' '); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2114 | waddnstr(bottomwin, desc, len); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2115 | } |
| 2116 | } |
| 2117 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2118 | /* And so start the display update routines. */ |
| 2119 | |
| 2120 | #ifndef NDEBUG |
| 2121 | int check_linenumbers(const filestruct *fileptr) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 2122 | { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2123 | int check_line = 0; |
| 2124 | const filestruct *filetmp; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 2125 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2126 | for (filetmp = edittop; filetmp != fileptr; filetmp = filetmp->next) |
| 2127 | check_line++; |
| 2128 | return check_line; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 2129 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2130 | #endif |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 2131 | |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 2132 | /* nano scrolls horizontally within a line in chunks. This function |
| 2133 | * returns the column number of the first character displayed in the |
| 2134 | * window when the cursor is at the given column. Note that |
| 2135 | * 0 <= column - get_page_start(column) < COLS. */ |
| 2136 | size_t get_page_start(size_t column) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2137 | { |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 2138 | assert(COLS > 0); |
| 2139 | if (column == 0 || column < COLS - 1) |
| 2140 | return 0; |
| 2141 | else if (COLS > 9) |
David Lawrence Ramsey | 66081d4 | 2004-01-22 07:25:31 +0000 | [diff] [blame] | 2142 | return column - 7 - (column - 7) % (COLS - 8); |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 2143 | else if (COLS > 2) |
| 2144 | return column - (COLS - 2); |
| 2145 | else |
| 2146 | return column - (COLS - 1); |
| 2147 | /* The parentheses are necessary to avoid overflow. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2150 | /* Resets current_y, based on the position of current, and puts the |
| 2151 | * cursor at (current_y, current_x). */ |
| 2152 | void reset_cursor(void) |
| 2153 | { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2154 | /* Yuck. This condition can be true after open_file() when opening |
| 2155 | * the first file. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2156 | if (edittop == NULL) |
| 2157 | return; |
| 2158 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 2159 | current_y = current->lineno - edittop->lineno; |
| 2160 | if (current_y < editwinrows) { |
| 2161 | size_t x = xplustabs(); |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2162 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 2163 | wmove(edit, current_y, x - get_page_start(x)); |
| 2164 | } |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2165 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2166 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2167 | /* edit_add() takes care of the job of actually painting a line into the |
| 2168 | * edit window. fileptr is the line to be painted, at row yval of the |
| 2169 | * window. converted is the actual string to be written to the window, |
| 2170 | * with tabs and control characters replaced by strings of regular |
David Lawrence Ramsey | 4178db0 | 2004-05-23 21:23:23 +0000 | [diff] [blame] | 2171 | * characters. start is the column number of the first character of |
| 2172 | * this page. That is, the first character of converted corresponds to |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2173 | * character number actual_x(fileptr->data, start) of the line. */ |
David Lawrence Ramsey | 07d3feb | 2004-04-16 05:15:11 +0000 | [diff] [blame] | 2174 | void edit_add(const filestruct *fileptr, const char *converted, int |
| 2175 | yval, size_t start) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2176 | { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2177 | #if defined(ENABLE_COLOR) || !defined(NANO_SMALL) |
| 2178 | size_t startpos = actual_x(fileptr->data, start); |
| 2179 | /* The position in fileptr->data of the leftmost character |
| 2180 | * that displays at least partially on the window. */ |
| 2181 | size_t endpos = actual_x(fileptr->data, start + COLS - 1) + 1; |
| 2182 | /* The position in fileptr->data of the first character that is |
| 2183 | * completely off the window to the right. |
| 2184 | * |
| 2185 | * Note that endpos might be beyond the null terminator of the |
| 2186 | * string. */ |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 2187 | #endif |
| 2188 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2189 | assert(fileptr != NULL && converted != NULL); |
| 2190 | assert(strlen(converted) <= COLS); |
| 2191 | |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 2192 | /* Just paint the string in any case (we'll add color or reverse on |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2193 | * just the text that needs it). */ |
| 2194 | mvwaddstr(edit, yval, 0, converted); |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 2195 | |
Chris Allegretta | 7dd7768 | 2001-12-08 19:52:28 +0000 | [diff] [blame] | 2196 | #ifdef ENABLE_COLOR |
Chris Allegretta | 1dd0bc9 | 2002-10-13 18:43:45 +0000 | [diff] [blame] | 2197 | if (colorstrings != NULL && ISSET(COLOR_SYNTAX)) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2198 | const colortype *tmpcolor = colorstrings; |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 2199 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2200 | for (; tmpcolor != NULL; tmpcolor = tmpcolor->next) { |
| 2201 | int x_start; |
| 2202 | /* Starting column for mvwaddnstr. Zero-based. */ |
| 2203 | int paintlen; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2204 | /* Number of chars to paint on this line. There are COLS |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2205 | * characters on a whole line. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2206 | regmatch_t startmatch; /* match position for start_regexp */ |
| 2207 | regmatch_t endmatch; /* match position for end_regexp */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2208 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2209 | if (tmpcolor->bright) |
| 2210 | wattron(edit, A_BOLD); |
| 2211 | wattron(edit, COLOR_PAIR(tmpcolor->pairnum)); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2212 | /* Two notes about regexec(). Return value 0 means there is |
| 2213 | * a match. Also, rm_eo is the first non-matching character |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2214 | * after the match. */ |
| 2215 | |
| 2216 | /* First case, tmpcolor is a single-line expression. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2217 | if (tmpcolor->end == NULL) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2218 | size_t k = 0; |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 2219 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2220 | /* We increment k by rm_eo, to move past the end of the |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2221 | * last match. Even though two matches may overlap, we |
| 2222 | * want to ignore them, so that we can highlight |
| 2223 | * C-strings correctly. */ |
| 2224 | while (k < endpos) { |
| 2225 | /* Note the fifth parameter to regexec(). It says |
| 2226 | * not to match the beginning-of-line character |
| 2227 | * unless k is 0. If regexec() returns REG_NOMATCH, |
| 2228 | * there are no more matches in the line. */ |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 2229 | if (regexec(&tmpcolor->start, &fileptr->data[k], 1, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2230 | &startmatch, k == 0 ? 0 : REG_NOTBOL) == REG_NOMATCH) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2231 | break; |
| 2232 | /* Translate the match to the beginning of the line. */ |
| 2233 | startmatch.rm_so += k; |
| 2234 | startmatch.rm_eo += k; |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 2235 | if (startmatch.rm_so == startmatch.rm_eo) { |
| 2236 | startmatch.rm_eo++; |
Chris Allegretta | 7c27be4 | 2002-05-05 23:03:54 +0000 | [diff] [blame] | 2237 | statusbar(_("Refusing 0 length regex match")); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2238 | } else if (startmatch.rm_so < endpos && |
| 2239 | startmatch.rm_eo > startpos) { |
| 2240 | if (startmatch.rm_so <= startpos) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2241 | x_start = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2242 | else |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2243 | x_start = strnlenpt(fileptr->data, |
| 2244 | startmatch.rm_so) - start; |
| 2245 | paintlen = strnlenpt(fileptr->data, |
| 2246 | startmatch.rm_eo) - start - x_start; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2247 | if (paintlen > COLS - x_start) |
| 2248 | paintlen = COLS - x_start; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2249 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2250 | assert(0 <= x_start && 0 < paintlen && |
| 2251 | x_start + paintlen <= COLS); |
| 2252 | mvwaddnstr(edit, yval, x_start, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2253 | converted + x_start, paintlen); |
| 2254 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2255 | k = startmatch.rm_eo; |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2256 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2257 | } else { |
| 2258 | /* This is a multi-line regexp. There are two steps. |
| 2259 | * First, we have to see if the beginning of the line is |
| 2260 | * colored by a start on an earlier line, and an end on |
| 2261 | * this line or later. |
| 2262 | * |
| 2263 | * We find the first line before fileptr matching the |
| 2264 | * start. If every match on that line is followed by an |
| 2265 | * end, then go to step two. Otherwise, find the next line |
| 2266 | * after start_line matching the end. If that line is not |
| 2267 | * before fileptr, then paint the beginning of this line. */ |
Chris Allegretta | 3674c1d | 2002-05-12 20:43:49 +0000 | [diff] [blame] | 2268 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2269 | const filestruct *start_line = fileptr->prev; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2270 | /* the first line before fileptr matching start */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2271 | regoff_t start_col; |
| 2272 | /* where it starts in that line */ |
| 2273 | const filestruct *end_line; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2274 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2275 | while (start_line != NULL && |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 2276 | regexec(&tmpcolor->start, start_line->data, 1, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2277 | &startmatch, 0) == REG_NOMATCH) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2278 | /* If there is an end on this line, there is no need |
| 2279 | * to look for starts on earlier lines. */ |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2280 | if (regexec(tmpcolor->end, start_line->data, 0, |
| 2281 | NULL, 0) == 0) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2282 | goto step_two; |
| 2283 | start_line = start_line->prev; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2284 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2285 | /* No start found, so skip to the next step. */ |
| 2286 | if (start_line == NULL) |
| 2287 | goto step_two; |
| 2288 | /* Now start_line is the first line before fileptr |
| 2289 | * containing a start match. Is there a start on this |
| 2290 | * line not followed by an end on this line? */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2291 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2292 | start_col = 0; |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 2293 | while (TRUE) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2294 | start_col += startmatch.rm_so; |
| 2295 | startmatch.rm_eo -= startmatch.rm_so; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2296 | if (regexec(tmpcolor->end, |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2297 | start_line->data + start_col + startmatch.rm_eo, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2298 | 0, NULL, start_col + startmatch.rm_eo == 0 ? 0 : |
| 2299 | REG_NOTBOL) == REG_NOMATCH) |
| 2300 | /* No end found after this start. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2301 | break; |
| 2302 | start_col++; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 2303 | if (regexec(&tmpcolor->start, |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2304 | start_line->data + start_col, 1, |
| 2305 | &startmatch, REG_NOTBOL) == REG_NOMATCH) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2306 | /* No later start on this line. */ |
| 2307 | goto step_two; |
| 2308 | } |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2309 | /* Indeed, there is a start not followed on this line by |
| 2310 | * an end. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2311 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2312 | /* We have already checked that there is no end before |
| 2313 | * fileptr and after the start. Is there an end after |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2314 | * the start at all? We don't paint unterminated |
| 2315 | * starts. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2316 | end_line = fileptr; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 2317 | while (end_line != NULL && |
| 2318 | regexec(tmpcolor->end, end_line->data, 1, &endmatch, 0)) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2319 | end_line = end_line->next; |
| 2320 | |
| 2321 | /* No end found, or it is too early. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2322 | if (end_line == NULL || |
| 2323 | (end_line == fileptr && endmatch.rm_eo <= startpos)) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2324 | goto step_two; |
| 2325 | |
| 2326 | /* Now paint the start of fileptr. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2327 | paintlen = end_line != fileptr ? COLS : |
| 2328 | strnlenpt(fileptr->data, endmatch.rm_eo) - start; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2329 | if (paintlen > COLS) |
| 2330 | paintlen = COLS; |
| 2331 | |
| 2332 | assert(0 < paintlen && paintlen <= COLS); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2333 | mvwaddnstr(edit, yval, 0, converted, paintlen); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2334 | |
| 2335 | /* We have already painted the whole line. */ |
| 2336 | if (paintlen == COLS) |
| 2337 | goto skip_step_two; |
| 2338 | |
David Lawrence Ramsey | 94e7094 | 2004-05-13 18:04:31 +0000 | [diff] [blame] | 2339 | step_two: |
| 2340 | /* Second step, we look for starts on this line. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2341 | start_col = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2342 | while (start_col < endpos) { |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2343 | if (regexec(&tmpcolor->start, |
| 2344 | fileptr->data + start_col, 1, &startmatch, |
| 2345 | start_col == 0 ? 0 : REG_NOTBOL) == REG_NOMATCH || |
| 2346 | start_col + startmatch.rm_so >= endpos) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2347 | /* No more starts on this line. */ |
| 2348 | break; |
| 2349 | /* Translate the match to be relative to the |
| 2350 | * beginning of the line. */ |
| 2351 | startmatch.rm_so += start_col; |
| 2352 | startmatch.rm_eo += start_col; |
| 2353 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2354 | if (startmatch.rm_so <= startpos) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2355 | x_start = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2356 | else |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2357 | x_start = strnlenpt(fileptr->data, |
| 2358 | startmatch.rm_so) - start; |
| 2359 | if (regexec(tmpcolor->end, |
| 2360 | fileptr->data + startmatch.rm_eo, 1, &endmatch, |
| 2361 | startmatch.rm_eo == 0 ? 0 : REG_NOTBOL) == 0) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2362 | /* Translate the end match to be relative to the |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2363 | * beginning of the line. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2364 | endmatch.rm_so += startmatch.rm_eo; |
| 2365 | endmatch.rm_eo += startmatch.rm_eo; |
| 2366 | /* There is an end on this line. But does it |
David Lawrence Ramsey | 94e7094 | 2004-05-13 18:04:31 +0000 | [diff] [blame] | 2367 | * appear on this page, and is the match more |
| 2368 | * than zero characters long? */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2369 | if (endmatch.rm_eo > startpos && |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2370 | endmatch.rm_eo > startmatch.rm_so) { |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2371 | paintlen = strnlenpt(fileptr->data, |
| 2372 | endmatch.rm_eo) - start - x_start; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2373 | if (x_start + paintlen > COLS) |
| 2374 | paintlen = COLS - x_start; |
| 2375 | |
| 2376 | assert(0 <= x_start && 0 < paintlen && |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2377 | x_start + paintlen <= COLS); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2378 | mvwaddnstr(edit, yval, x_start, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2379 | converted + x_start, paintlen); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2380 | } |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2381 | } else { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2382 | /* There is no end on this line. But we haven't |
| 2383 | * yet looked for one on later lines. */ |
| 2384 | end_line = fileptr->next; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2385 | while (end_line != NULL && |
| 2386 | regexec(tmpcolor->end, end_line->data, 0, |
| 2387 | NULL, 0) == REG_NOMATCH) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2388 | end_line = end_line->next; |
| 2389 | if (end_line != NULL) { |
| 2390 | assert(0 <= x_start && x_start < COLS); |
| 2391 | mvwaddnstr(edit, yval, x_start, |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2392 | converted + x_start, COLS - x_start); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2393 | /* We painted to the end of the line, so |
| 2394 | * don't bother checking any more starts. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2395 | break; |
Chris Allegretta | 3674c1d | 2002-05-12 20:43:49 +0000 | [diff] [blame] | 2396 | } |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2397 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2398 | start_col = startmatch.rm_so + 1; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2399 | } /* while start_col < endpos */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2400 | } /* if (tmp_color->end != NULL) */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 2401 | |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 2402 | skip_step_two: |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2403 | wattroff(edit, A_BOLD); |
| 2404 | wattroff(edit, COLOR_PAIR(tmpcolor->pairnum)); |
| 2405 | } /* for tmpcolor in colorstrings */ |
| 2406 | } |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2407 | #endif /* ENABLE_COLOR */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2408 | |
Chris Allegretta | 7dd7768 | 2001-12-08 19:52:28 +0000 | [diff] [blame] | 2409 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2410 | if (ISSET(MARK_ISSET) |
| 2411 | && (fileptr->lineno <= mark_beginbuf->lineno |
| 2412 | || fileptr->lineno <= current->lineno) |
| 2413 | && (fileptr->lineno >= mark_beginbuf->lineno |
| 2414 | || fileptr->lineno >= current->lineno)) { |
| 2415 | /* fileptr is at least partially selected. */ |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 2416 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2417 | const filestruct *top; |
| 2418 | /* Either current or mark_beginbuf, whichever is first. */ |
| 2419 | size_t top_x; |
| 2420 | /* current_x or mark_beginx, corresponding to top. */ |
| 2421 | const filestruct *bot; |
| 2422 | size_t bot_x; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2423 | int x_start; |
| 2424 | /* Starting column for mvwaddnstr. Zero-based. */ |
| 2425 | int paintlen; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2426 | /* Number of chars to paint on this line. There are COLS |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2427 | * characters on a whole line. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2428 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2429 | mark_order(&top, &top_x, &bot, &bot_x); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2430 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2431 | if (top->lineno < fileptr->lineno || top_x < startpos) |
| 2432 | top_x = startpos; |
| 2433 | if (bot->lineno > fileptr->lineno || bot_x > endpos) |
| 2434 | bot_x = endpos; |
| 2435 | |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 2436 | /* The selected bit of fileptr is on this page. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2437 | if (top_x < endpos && bot_x > startpos) { |
| 2438 | assert(startpos <= top_x); |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 2439 | |
| 2440 | /* x_start is the expanded location of the beginning of the |
| 2441 | * mark minus the beginning of the page. */ |
| 2442 | x_start = strnlenpt(fileptr->data, top_x) - start; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2443 | |
| 2444 | if (bot_x >= endpos) |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 2445 | /* If the end of the mark is off the page, paintlen is |
| 2446 | * -1, meaning that everything on the line gets |
| 2447 | * painted. */ |
| 2448 | paintlen = -1; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2449 | else |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 2450 | /* Otherwise, paintlen is the expanded location of the |
| 2451 | * end of the mark minus the expanded location of the |
| 2452 | * beginning of the mark. */ |
David Lawrence Ramsey | e5b2f83 | 2004-04-29 06:30:36 +0000 | [diff] [blame] | 2453 | paintlen = strnlenpt(fileptr->data, bot_x) |
| 2454 | - (x_start + start); |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 2455 | |
| 2456 | /* If x_start is before the beginning of the page, shift |
| 2457 | * paintlen x_start characters to compensate, and put |
| 2458 | * x_start at the beginning of the page. */ |
| 2459 | if (x_start < 0) { |
| 2460 | paintlen += x_start; |
| 2461 | x_start = 0; |
| 2462 | } |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2463 | |
| 2464 | assert(x_start >= 0 && x_start <= strlen(converted)); |
| 2465 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2466 | wattron(edit, A_REVERSE); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2467 | mvwaddnstr(edit, yval, x_start, converted + x_start, paintlen); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 2468 | wattroff(edit, A_REVERSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2469 | } |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 2470 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2471 | #endif /* !NANO_SMALL */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2472 | } |
| 2473 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2474 | /* Just update one line in the edit buffer. Basically a wrapper for |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2475 | * edit_add(). |
| 2476 | * |
David Lawrence Ramsey | 07d3feb | 2004-04-16 05:15:11 +0000 | [diff] [blame] | 2477 | * If fileptr != current, then index is considered 0. The line will be |
| 2478 | * displayed starting with fileptr->data[index]. Likely args are |
| 2479 | * current_x or 0. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2480 | void update_line(const filestruct *fileptr, size_t index) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2481 | { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2482 | int line; |
| 2483 | /* line in the edit window for CURSES calls */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2484 | char *converted; |
| 2485 | /* fileptr->data converted to have tabs and control characters |
| 2486 | * expanded. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2487 | size_t page_start; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2488 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2489 | assert(fileptr != NULL); |
Robert Siemborski | 53154a7 | 2000-06-18 00:11:03 +0000 | [diff] [blame] | 2490 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2491 | line = fileptr->lineno - edittop->lineno; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2492 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2493 | /* We assume the line numbers are valid. Is that really true? */ |
| 2494 | assert(line < 0 || line == check_linenumbers(fileptr)); |
| 2495 | |
| 2496 | if (line < 0 || line >= editwinrows) |
| 2497 | return; |
| 2498 | |
| 2499 | /* First, blank out the line (at a minimum) */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2500 | mvwaddstr(edit, line, 0, hblank); |
| 2501 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2502 | /* Next, convert variables that index the line to their equivalent |
| 2503 | * positions in the expanded line. */ |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 2504 | index = (fileptr == current) ? strnlenpt(fileptr->data, index) : 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2505 | page_start = get_page_start(index); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 2506 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2507 | /* Expand the line, replacing Tab by spaces, and control characters |
| 2508 | * by their display form. */ |
| 2509 | converted = display_string(fileptr->data, page_start, COLS); |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 2510 | |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 2511 | /* Now, paint the line */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2512 | edit_add(fileptr, converted, line, page_start); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2513 | free(converted); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2514 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2515 | if (page_start > 0) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2516 | mvwaddch(edit, line, 0, '$'); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2517 | if (strlenpt(fileptr->data) > page_start + COLS) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 2518 | mvwaddch(edit, line, COLS - 1, '$'); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 2521 | /* Return a nonzero value if we need an update after moving |
| 2522 | * horizontally. We need one if the mark is on or if old_pww and |
| 2523 | * placewewant are on different pages. Assume none of the text has |
| 2524 | * changed since the last update. */ |
| 2525 | int need_horizontal_update(int old_pww) |
| 2526 | { |
| 2527 | return |
| 2528 | #ifndef NANO_SMALL |
| 2529 | ISSET(MARK_ISSET) || |
| 2530 | #endif |
| 2531 | get_page_start(old_pww) != get_page_start(placewewant); |
| 2532 | } |
| 2533 | |
| 2534 | /* Return a nonzero value if we need an update after moving vertically. |
| 2535 | * We need one if the mark is on or if old_pww and placewewant |
| 2536 | * are on different pages. Assume none of the text has changed since |
| 2537 | * the last update. */ |
| 2538 | int need_vertical_update(int old_pww) |
| 2539 | { |
| 2540 | return |
| 2541 | #ifndef NANO_SMALL |
| 2542 | ISSET(MARK_ISSET) || |
| 2543 | #endif |
| 2544 | get_page_start(old_pww) != get_page_start(placewewant); |
| 2545 | } |
| 2546 | |
| 2547 | /* Scroll the edit window in the given direction and the given number |
| 2548 | * of lines, and draw new lines on the blank lines left after the |
| 2549 | * scrolling. direction is the direction to scroll, either UP or DOWN, |
| 2550 | * and nlines is the number of lines to scroll. Don't redraw the old |
| 2551 | * topmost or bottommost line (where we assume current is) before |
| 2552 | * scrolling or draw the new topmost or bottommost line after scrolling |
| 2553 | * (where we assume current will be), since we don't know where we are |
| 2554 | * on the page or whether we'll stay there. Assume none of the text has |
| 2555 | * changed since the last update. */ |
| 2556 | void edit_scroll(updown direction, int nlines) |
| 2557 | { |
| 2558 | filestruct *foo; |
| 2559 | int i, scroll_rows = 0; |
| 2560 | |
| 2561 | /* Scrolling less than one line or more than editwinrows lines is |
| 2562 | * redundant, so don't allow it. */ |
| 2563 | if (nlines < 1 || nlines > editwinrows) |
| 2564 | return; |
| 2565 | |
| 2566 | /* Move the top line of the edit window up or down (depending on the |
| 2567 | * value of direction) nlines lines. If there are fewer lines of |
| 2568 | * text than that left, move it to the top or bottom line of the |
| 2569 | * file (depending on the value of direction). Keep track of |
| 2570 | * how many lines we moved in scroll_rows. */ |
| 2571 | for (i = nlines; i > 0; i--) { |
| 2572 | if (direction == UP) { |
| 2573 | if (edittop->prev == NULL) |
| 2574 | break; |
| 2575 | edittop = edittop->prev; |
| 2576 | scroll_rows--; |
| 2577 | } else { |
| 2578 | if (edittop->next == NULL) |
| 2579 | break; |
| 2580 | edittop = edittop->next; |
| 2581 | scroll_rows++; |
| 2582 | } |
| 2583 | } |
| 2584 | |
| 2585 | /* Scroll the text on the screen up or down scroll_rows lines, |
| 2586 | * depending on the value of direction. */ |
| 2587 | scrollok(edit, TRUE); |
| 2588 | wscrl(edit, scroll_rows); |
| 2589 | scrollok(edit, FALSE); |
| 2590 | |
| 2591 | foo = edittop; |
| 2592 | if (direction != UP) { |
| 2593 | int slines = editwinrows - nlines; |
| 2594 | for (; slines > 0 && foo != NULL; slines--) |
| 2595 | foo = foo->next; |
| 2596 | } |
| 2597 | |
| 2598 | /* And draw new lines on the blank top or bottom lines of the edit |
| 2599 | * window, depending on the value of direction. Don't draw the new |
| 2600 | * topmost or new bottommost line. */ |
| 2601 | while (scroll_rows != 0 && foo != NULL) { |
| 2602 | if (foo->next != NULL) |
| 2603 | update_line(foo, 0); |
| 2604 | if (direction == UP) |
| 2605 | scroll_rows++; |
| 2606 | else |
| 2607 | scroll_rows--; |
| 2608 | foo = foo->next; |
| 2609 | } |
| 2610 | } |
| 2611 | |
| 2612 | /* Update any lines between old_current and current that need to be |
| 2613 | * updated. Note that we use placewewant to determine whether we need |
| 2614 | * updates and current_x to update current, so if placewewant needs to |
| 2615 | * be changed, it should be changed after calling this, and if current_x |
| 2616 | * needs to be changed, it should be changed before calling this. |
| 2617 | * Assume none of the text has changed since the last update. */ |
| 2618 | void edit_redraw(const filestruct *old_current) |
| 2619 | { |
| 2620 | int do_refresh = need_vertical_update(0); |
| 2621 | const filestruct *foo; |
| 2622 | |
| 2623 | /* If either old_current or current is offscreen, refresh the screen |
| 2624 | * and get out. */ |
| 2625 | if (old_current->lineno < edittop->lineno || old_current->lineno >= |
| 2626 | edittop->lineno + editwinrows || current->lineno < |
| 2627 | edittop->lineno || current->lineno >= edittop->lineno + |
| 2628 | editwinrows) { |
| 2629 | edit_refresh(); |
| 2630 | return; |
| 2631 | } |
| 2632 | |
| 2633 | /* Update old_current and current if we're not on the first page. |
| 2634 | * If the mark is on, update all the lines between old_current and |
| 2635 | * current too. */ |
| 2636 | foo = old_current; |
| 2637 | while (foo != current) { |
| 2638 | if (do_refresh) |
| 2639 | update_line(foo, 0); |
| 2640 | #ifndef NANO_SMALL |
| 2641 | if (!ISSET(MARK_ISSET)) |
| 2642 | #endif |
| 2643 | break; |
| 2644 | if (foo->lineno > current->lineno) |
| 2645 | foo = foo->prev; |
| 2646 | else |
| 2647 | foo = foo->next; |
| 2648 | } |
| 2649 | if (do_refresh) |
| 2650 | update_line(current, current_x); |
| 2651 | } |
| 2652 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2653 | /* Refresh the screen without changing the position of lines. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2654 | void edit_refresh(void) |
| 2655 | { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2656 | /* Neither of these conditions should occur, but they do. edittop |
| 2657 | * is NULL when you open an existing file on the command line, and |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2658 | * ENABLE_COLOR is defined. Yuck. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2659 | if (current == NULL) |
| 2660 | return; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2661 | if (edittop == NULL) |
| 2662 | edittop = current; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2663 | |
Chris Allegretta | 63d0b48 | 2003-01-26 19:47:10 +0000 | [diff] [blame] | 2664 | if (current->lineno < edittop->lineno || |
| 2665 | current->lineno >= edittop->lineno + editwinrows) |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2666 | /* Note that edit_update() changes edittop so that |
| 2667 | * current->lineno = edittop->lineno + editwinrows / 2. Thus |
| 2668 | * when it then calls edit_refresh(), there is no danger of |
| 2669 | * getting an infinite loop. */ |
Chris Allegretta | da721be | 2000-07-31 01:26:42 +0000 | [diff] [blame] | 2670 | edit_update(current, CENTER); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2671 | else { |
| 2672 | int nlines = 0; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 2673 | const filestruct *foo = edittop; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2674 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 2675 | #ifdef DEBUG |
| 2676 | fprintf(stderr, "edit_refresh(): edittop->lineno = %ld\n", edittop->lineno); |
| 2677 | #endif |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2678 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2679 | while (nlines < editwinrows) { |
David Lawrence Ramsey | 9d325a0 | 2004-05-29 03:03:52 +0000 | [diff] [blame] | 2680 | update_line(foo, foo == current ? current_x : 0); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2681 | nlines++; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 2682 | if (foo->next == NULL) |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2683 | break; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 2684 | foo = foo->next; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2685 | } |
| 2686 | while (nlines < editwinrows) { |
| 2687 | mvwaddstr(edit, nlines, 0, hblank); |
| 2688 | nlines++; |
| 2689 | } |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 2690 | reset_cursor(); |
David Lawrence Ramsey | 07d3feb | 2004-04-16 05:15:11 +0000 | [diff] [blame] | 2691 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2692 | /* What the hell are we expecting to update the screen if this |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2693 | * isn't here? Luck? */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2694 | wrefresh(edit); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2695 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2696 | } |
| 2697 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2698 | /* Nice generic routine to update the edit buffer, given a pointer to the |
| 2699 | * file struct =) */ |
David Lawrence Ramsey | 1356a0a | 2003-09-10 20:31:02 +0000 | [diff] [blame] | 2700 | void edit_update(filestruct *fileptr, topmidnone location) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2701 | { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2702 | if (fileptr == NULL) |
| 2703 | return; |
| 2704 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2705 | if (location != TOP) { |
David Lawrence Ramsey | 07d3feb | 2004-04-16 05:15:11 +0000 | [diff] [blame] | 2706 | int goal = (location == NONE) ? current_y : editwinrows / 2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2707 | |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 2708 | for (; goal > 0 && fileptr->prev != NULL; goal--) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2709 | fileptr = fileptr->prev; |
| 2710 | } |
| 2711 | edittop = fileptr; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2712 | edit_refresh(); |
| 2713 | } |
| 2714 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2715 | /* Ask a question on the statusbar. Answer will be stored in answer |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2716 | * global. Returns -1 on aborted enter, -2 on a blank string, and 0 |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 2717 | * otherwise, the valid shortcut key caught. Def is any editable text we |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2718 | * want to put up by default. |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 2719 | * |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2720 | * New arg tabs tells whether or not to allow tab completion. */ |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 2721 | int statusq(int allowtabs, const shortcut *s, const char *def, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 2722 | #ifndef NANO_SMALL |
| 2723 | historyheadtype *which_history, |
| 2724 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2725 | const char *msg, ...) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2726 | { |
| 2727 | va_list ap; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2728 | char *foo = charalloc(COLS - 3); |
Chris Allegretta | 9caa193 | 2002-02-15 20:08:05 +0000 | [diff] [blame] | 2729 | int ret; |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2730 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | a16e4e9 | 2002-01-05 18:59:54 +0000 | [diff] [blame] | 2731 | int list = 0; |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2732 | #endif |
| 2733 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2734 | bottombars(s); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2735 | |
| 2736 | va_start(ap, msg); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2737 | vsnprintf(foo, COLS - 4, msg, ap); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2738 | va_end(ap); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2739 | foo[COLS - 4] = '\0'; |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2740 | |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 2741 | ret = nanogetstr(allowtabs, foo, def, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 2742 | #ifndef NANO_SMALL |
| 2743 | which_history, |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2744 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 2745 | s |
| 2746 | #ifndef DISABLE_TABCOMP |
| 2747 | , &list |
| 2748 | #endif |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2749 | ); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2750 | free(foo); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2751 | resetstatuspos = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2752 | |
| 2753 | switch (ret) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2754 | case NANO_FIRSTLINE_KEY: |
David Lawrence Ramsey | d7f5ad9 | 2004-03-04 19:30:53 +0000 | [diff] [blame] | 2755 | case NANO_FIRSTLINE_FKEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2756 | do_first_line(); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2757 | resetstatuspos = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2758 | break; |
| 2759 | case NANO_LASTLINE_KEY: |
David Lawrence Ramsey | d7f5ad9 | 2004-03-04 19:30:53 +0000 | [diff] [blame] | 2760 | case NANO_LASTLINE_FKEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2761 | do_last_line(); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2762 | resetstatuspos = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2763 | break; |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2764 | #ifndef DISABLE_JUSTIFY |
| 2765 | case NANO_PARABEGIN_KEY: |
| 2766 | do_para_begin(); |
| 2767 | resetstatuspos = 1; |
| 2768 | break; |
| 2769 | case NANO_PARAEND_KEY: |
| 2770 | do_para_end(); |
| 2771 | resetstatuspos = 1; |
| 2772 | break; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2773 | case NANO_FULLJUSTIFY_KEY: |
David Lawrence Ramsey | d12fd4b | 2004-05-28 15:05:56 +0000 | [diff] [blame] | 2774 | if (!ISSET(VIEW_MODE)) |
| 2775 | do_full_justify(); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2776 | resetstatuspos = 1; |
| 2777 | break; |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2778 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2779 | case NANO_CANCEL_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2780 | ret = -1; |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2781 | resetstatuspos = 1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2782 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2783 | } |
Chris Allegretta | a90d0cf | 2003-02-10 02:55:03 +0000 | [diff] [blame] | 2784 | blank_statusbar(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2785 | |
| 2786 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 2787 | fprintf(stderr, "I got \"%s\"\n", answer); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2788 | #endif |
| 2789 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2790 | #ifndef DISABLE_TABCOMP |
| 2791 | /* if we've done tab completion, there might be a list of |
| 2792 | filename matches on the edit window at this point; make sure |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2793 | they're cleared off. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2794 | if (list) |
| 2795 | edit_refresh(); |
| 2796 | #endif |
| 2797 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2798 | return ret; |
| 2799 | } |
| 2800 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2801 | /* Ask a simple yes/no question on the statusbar. Returns 1 for Y, 0 |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2802 | * for N, 2 for All (if all is nonzero when passed in) and -1 for abort |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2803 | * (^C). */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2804 | int do_yesno(int all, const char *msg) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2805 | { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2806 | int ok = -2, width = 16; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2807 | const char *yesstr; /* String of yes characters accepted */ |
| 2808 | const char *nostr; /* Same for no */ |
| 2809 | const char *allstr; /* And all, surprise! */ |
Chris Allegretta | 235ab19 | 2001-04-12 13:24:40 +0000 | [diff] [blame] | 2810 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2811 | /* Yes, no and all are strings of any length. Each string consists |
| 2812 | * of all characters accepted as a valid character for that value. |
| 2813 | * The first value will be the one displayed in the shortcuts. */ |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2814 | yesstr = _("Yy"); |
| 2815 | nostr = _("Nn"); |
| 2816 | allstr = _("Aa"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2817 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2818 | /* Remove gettext() call for keybindings until we clear the thing |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2819 | * up. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2820 | if (!ISSET(NO_HELP)) { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2821 | char shortstr[3]; /* Temp string for Y, N, A. */ |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2822 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2823 | if (COLS < 32) |
| 2824 | width = COLS / 2; |
| 2825 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2826 | /* Write the bottom of the screen. */ |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2827 | blank_bottombars(); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2828 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2829 | sprintf(shortstr, " %c", yesstr[0]); |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2830 | wmove(bottomwin, 1, 0); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2831 | onekey(shortstr, _("Yes"), width); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2832 | |
| 2833 | if (all) { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2834 | wmove(bottomwin, 1, width); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2835 | shortstr[1] = allstr[0]; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2836 | onekey(shortstr, _("All"), width); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2837 | } |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2838 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2839 | wmove(bottomwin, 2, 0); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2840 | shortstr[1] = nostr[0]; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2841 | onekey(shortstr, _("No"), width); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2842 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2843 | wmove(bottomwin, 2, 16); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2844 | onekey("^C", _("Cancel"), width); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2845 | } |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2846 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2847 | wattron(bottomwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2848 | |
| 2849 | blank_statusbar(); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2850 | mvwaddnstr(bottomwin, 0, 0, msg, COLS - 1); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2851 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2852 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2853 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2854 | wrefresh(bottomwin); |
| 2855 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2856 | do { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2857 | int kbinput; |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 2858 | int meta_key; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2859 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2860 | int mouse_x, mouse_y; |
Chris Allegretta | 235ab19 | 2001-04-12 13:24:40 +0000 | [diff] [blame] | 2861 | #endif |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2862 | |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 2863 | kbinput = get_kbinput(edit, &meta_key); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2864 | |
| 2865 | if (kbinput == NANO_CANCEL_KEY) |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2866 | ok = -1; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2867 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2868 | /* Look ma! We get to duplicate lots of code from |
| 2869 | * do_mouse()!! */ |
| 2870 | else if (kbinput == KEY_MOUSE) { |
| 2871 | kbinput = get_mouseinput(&mouse_x, &mouse_y, 0); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2872 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2873 | if (mouse_x != -1 && mouse_y != -1 && !ISSET(NO_HELP) && |
| 2874 | wenclose(bottomwin, mouse_y, mouse_x) && mouse_x < |
| 2875 | (width * 2) && mouse_y >= editwinrows + 3) { |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2876 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2877 | int x = mouse_x / width; |
| 2878 | /* Did we click in the first column of shortcuts, or |
| 2879 | * the second? */ |
| 2880 | int y = mouse_y - editwinrows - 3; |
| 2881 | /* Did we click in the first row of shortcuts? */ |
| 2882 | |
| 2883 | assert(0 <= x && x <= 1 && 0 <= y && y <= 1); |
| 2884 | |
| 2885 | /* x = 0 means they clicked Yes or No. |
| 2886 | * y = 0 means Yes or All. */ |
| 2887 | ok = -2 * x * y + x - y + 1; |
| 2888 | |
| 2889 | if (ok == 2 && !all) |
| 2890 | ok = -2; |
| 2891 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2892 | } |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2893 | #endif |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2894 | /* Look for the kbinput in the yes, no and (optionally) all |
| 2895 | * str. */ |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2896 | else if (strchr(yesstr, kbinput) != NULL) |
| 2897 | ok = 1; |
| 2898 | else if (strchr(nostr, kbinput) != NULL) |
| 2899 | ok = 0; |
| 2900 | else if (all && strchr(allstr, kbinput) != NULL) |
| 2901 | ok = 2; |
| 2902 | } while (ok == -2); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2903 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2904 | return ok; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2905 | } |
| 2906 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2907 | int total_refresh(void) |
| 2908 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2909 | clearok(topwin, TRUE); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2910 | clearok(edit, TRUE); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2911 | clearok(bottomwin, TRUE); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2912 | wnoutrefresh(topwin); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2913 | wnoutrefresh(edit); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2914 | wnoutrefresh(bottomwin); |
| 2915 | doupdate(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2916 | clearok(topwin, FALSE); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2917 | clearok(edit, FALSE); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2918 | clearok(bottomwin, FALSE); |
| 2919 | edit_refresh(); |
| 2920 | titlebar(NULL); |
| 2921 | return 1; |
| 2922 | } |
| 2923 | |
| 2924 | void display_main_list(void) |
| 2925 | { |
| 2926 | bottombars(main_list); |
| 2927 | } |
| 2928 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2929 | /* If constant is FALSE, the user typed ^C so we unconditionally display |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2930 | * the cursor position. Otherwise, we display it only if the character |
| 2931 | * position changed, and DISABLE_CURPOS is not set. |
| 2932 | * |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2933 | * If constant is TRUE and DISABLE_CURPOS is set, we unset it and update |
| 2934 | * old_i and old_totsize. That way, we leave the current statusbar |
| 2935 | * alone, but next time we will display. */ |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2936 | int do_cursorpos(int constant) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2937 | { |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2938 | const filestruct *fileptr; |
| 2939 | unsigned long i = 0; |
| 2940 | static unsigned long old_i = 0; |
| 2941 | static long old_totsize = -1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2942 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2943 | assert(current != NULL && fileage != NULL && totlines != 0); |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2944 | |
| 2945 | if (old_totsize == -1) |
| 2946 | old_totsize = totsize; |
| 2947 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2948 | for (fileptr = fileage; fileptr != current; fileptr = fileptr->next) { |
| 2949 | assert(fileptr != NULL); |
Chris Allegretta | f27c697 | 2002-02-12 01:57:24 +0000 | [diff] [blame] | 2950 | i += strlen(fileptr->data) + 1; |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2951 | } |
Chris Allegretta | f27c697 | 2002-02-12 01:57:24 +0000 | [diff] [blame] | 2952 | i += current_x; |
Chris Allegretta | 14b3ca9 | 2002-01-25 21:59:02 +0000 | [diff] [blame] | 2953 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2954 | if (constant && ISSET(DISABLE_CURPOS)) { |
| 2955 | UNSET(DISABLE_CURPOS); |
| 2956 | old_i = i; |
| 2957 | old_totsize = totsize; |
| 2958 | return 0; |
| 2959 | } |
Chris Allegretta | 14b3ca9 | 2002-01-25 21:59:02 +0000 | [diff] [blame] | 2960 | |
David Lawrence Ramsey | 4178db0 | 2004-05-23 21:23:23 +0000 | [diff] [blame] | 2961 | /* If constant is FALSE, display the position on the statusbar |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2962 | * unconditionally; otherwise, only display the position when the |
| 2963 | * character values have changed. */ |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2964 | if (!constant || old_i != i || old_totsize != totsize) { |
| 2965 | unsigned long xpt = xplustabs() + 1; |
| 2966 | unsigned long cur_len = strlenpt(current->data) + 1; |
| 2967 | int linepct = 100 * current->lineno / totlines; |
| 2968 | int colpct = 100 * xpt / cur_len; |
| 2969 | int bytepct = totsize == 0 ? 0 : 100 * i / totsize; |
| 2970 | |
| 2971 | statusbar( |
| 2972 | _("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%ld (%d%%)"), |
| 2973 | current->lineno, totlines, linepct, |
| 2974 | xpt, cur_len, colpct, |
| 2975 | i, totsize, bytepct); |
| 2976 | UNSET(DISABLE_CURPOS); |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2977 | } |
| 2978 | |
| 2979 | old_i = i; |
| 2980 | old_totsize = totsize; |
| 2981 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2982 | reset_cursor(); |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2983 | return 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2984 | } |
| 2985 | |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2986 | int do_cursorpos_void(void) |
| 2987 | { |
| 2988 | return do_cursorpos(0); |
| 2989 | } |
| 2990 | |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 2991 | /* Calculate the next line of help_text, starting at ptr. */ |
| 2992 | int line_len(const char *ptr) |
| 2993 | { |
| 2994 | int j = 0; |
| 2995 | |
| 2996 | while (*ptr != '\n' && *ptr != '\0' && j < COLS - 5) { |
| 2997 | ptr++; |
| 2998 | j++; |
| 2999 | } |
| 3000 | if (j == COLS - 5) { |
| 3001 | /* Don't wrap at the first of two spaces following a period. */ |
| 3002 | if (*ptr == ' ' && *(ptr + 1) == ' ') |
| 3003 | j++; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3004 | /* Don't print half a word if we've run out of space. */ |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 3005 | while (*ptr != ' ' && j > 0) { |
| 3006 | ptr--; |
| 3007 | j--; |
| 3008 | } |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3009 | /* Word longer than COLS - 5 chars just gets broken. */ |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 3010 | if (j == 0) |
| 3011 | j = COLS - 5; |
| 3012 | } |
| 3013 | assert(j >= 0 && j <= COLS - 4 && (j > 0 || *ptr == '\n')); |
| 3014 | return j; |
| 3015 | } |
| 3016 | |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3017 | #ifndef DISABLE_HELP |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3018 | /* Our shortcut-list-compliant help function, which is better than |
| 3019 | * nothing, and dynamic! */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3020 | int do_help(void) |
| 3021 | { |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3022 | int line = 0; |
| 3023 | /* The line number in help_text of the first displayed help line. |
| 3024 | * This variable is zero-based. */ |
| 3025 | int no_more = 0; |
| 3026 | /* no_more means the end of the help text is shown, so don't go down |
| 3027 | * any more. */ |
| 3028 | int kbinput = ERR, meta_key; |
| 3029 | |
| 3030 | int no_help_flag = ISSET(NO_HELP); |
| 3031 | int old_cursor = curs_set(0); |
| 3032 | #ifndef DISABLE_MOUSE |
| 3033 | const shortcut *oldshortcut = currshortcut; |
| 3034 | /* We will set currshortcut to allow clicking on the help |
| 3035 | screen shortcut list. */ |
| 3036 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3037 | |
| 3038 | blank_edit(); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 3039 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3040 | blank_statusbar(); |
| 3041 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3042 | /* Set help_text as the string to display. */ |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 3043 | help_init(); |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 3044 | assert(help_text != NULL); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 3045 | |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3046 | #ifndef DISABLE_MOUSE |
| 3047 | /* Set currshortcut to allow clicking on the help screen shortcut |
| 3048 | * list, AFTER help_init(). */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 3049 | currshortcut = help_list; |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3050 | #endif |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 3051 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3052 | if (ISSET(NO_HELP)) { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3053 | /* Well, if we're going to do this, we should at least do it the |
| 3054 | * right way. */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 3055 | UNSET(NO_HELP); |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 3056 | window_init(); |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3057 | } |
| 3058 | bottombars(help_list); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3059 | |
| 3060 | do { |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3061 | int i; |
| 3062 | int old_line = line; |
| 3063 | /* We redisplay the help only if it moved. */ |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 3064 | const char *ptr = help_text; |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 3065 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3066 | switch (kbinput) { |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3067 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3068 | case KEY_MOUSE: |
| 3069 | do_mouse(); |
| 3070 | break; |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 3071 | #endif |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3072 | case NANO_NEXTPAGE_KEY: |
| 3073 | case NANO_NEXTPAGE_FKEY: |
| 3074 | if (!no_more) |
| 3075 | line += editwinrows - 2; |
| 3076 | break; |
| 3077 | case NANO_PREVPAGE_KEY: |
| 3078 | case NANO_PREVPAGE_FKEY: |
| 3079 | if (line > 0) { |
| 3080 | line -= editwinrows - 2; |
| 3081 | if (line < 0) |
| 3082 | line = 0; |
| 3083 | } |
| 3084 | break; |
| 3085 | case NANO_PREVLINE_KEY: |
| 3086 | if (line > 0) |
| 3087 | line--; |
| 3088 | break; |
| 3089 | case NANO_NEXTLINE_KEY: |
| 3090 | if (!no_more) |
| 3091 | line++; |
| 3092 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3093 | } |
| 3094 | |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3095 | if (line == old_line && kbinput != ERR) |
| 3096 | goto skip_redisplay; |
| 3097 | |
| 3098 | blank_edit(); |
| 3099 | |
| 3100 | assert(COLS > 5); |
| 3101 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3102 | /* Calculate where in the text we should be, based on the |
| 3103 | * page. */ |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3104 | for (i = 0; i < line; i++) { |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 3105 | ptr += line_len(ptr); |
| 3106 | if (*ptr == '\n') |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3107 | ptr++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3108 | } |
| 3109 | |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 3110 | for (i = 0; i < editwinrows && *ptr != '\0'; i++) { |
| 3111 | int j = line_len(ptr); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3112 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3113 | mvwaddnstr(edit, i, 0, ptr, j); |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 3114 | ptr += j; |
| 3115 | if (*ptr == '\n') |
| 3116 | ptr++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3117 | } |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3118 | no_more = (*ptr == '\0'); |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 3119 | |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3120 | skip_redisplay: |
| 3121 | kbinput = get_kbinput(edit, &meta_key); |
| 3122 | } while (kbinput != NANO_EXIT_KEY && kbinput != NANO_EXIT_FKEY); |
Chris Allegretta | d1627cf | 2000-12-18 05:03:16 +0000 | [diff] [blame] | 3123 | |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3124 | #ifndef DISABLE_MOUSE |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 3125 | currshortcut = oldshortcut; |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3126 | #endif |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 3127 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3128 | if (no_help_flag) { |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 3129 | blank_bottombars(); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 3130 | wrefresh(bottomwin); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3131 | SET(NO_HELP); |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 3132 | window_init(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3133 | } else |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 3134 | bottombars(currshortcut); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3135 | |
David Lawrence Ramsey | be26561 | 2004-05-29 20:38:08 +0000 | [diff] [blame^] | 3136 | curs_set(old_cursor); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3137 | edit_refresh(); |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 3138 | |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 3139 | /* The help_init() at the beginning allocated help_text, which has |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3140 | * now been written to the screen. */ |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 3141 | free(help_text); |
| 3142 | help_text = NULL; |
| 3143 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3144 | return 1; |
| 3145 | } |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3146 | #endif /* !DISABLE_HELP */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3147 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3148 | /* Highlight the current word being replaced or spell checked. We |
| 3149 | * expect word to have tabs and control characters expanded. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3150 | void do_replace_highlight(int highlight_flag, const char *word) |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 3151 | { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3152 | int y = xplustabs(); |
| 3153 | size_t word_len = strlen(word); |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 3154 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3155 | y = get_page_start(y) + COLS - y; |
| 3156 | /* Now y is the number of characters we can display on this |
| 3157 | * line. */ |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 3158 | |
| 3159 | reset_cursor(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3160 | |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 3161 | if (highlight_flag) |
| 3162 | wattron(edit, A_REVERSE); |
| 3163 | |
David Lawrence Ramsey | 2a4ab6d | 2003-12-24 08:29:49 +0000 | [diff] [blame] | 3164 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 76c4b33 | 2003-12-24 08:17:54 +0000 | [diff] [blame] | 3165 | /* This is so we can show zero-length regexes. */ |
| 3166 | if (word_len == 0) |
| 3167 | waddstr(edit, " "); |
| 3168 | else |
David Lawrence Ramsey | 2a4ab6d | 2003-12-24 08:29:49 +0000 | [diff] [blame] | 3169 | #endif |
David Lawrence Ramsey | 76c4b33 | 2003-12-24 08:17:54 +0000 | [diff] [blame] | 3170 | waddnstr(edit, word, y - 1); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3171 | |
| 3172 | if (word_len > y) |
| 3173 | waddch(edit, '$'); |
| 3174 | else if (word_len == y) |
| 3175 | waddch(edit, word[word_len - 1]); |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 3176 | |
| 3177 | if (highlight_flag) |
| 3178 | wattroff(edit, A_REVERSE); |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 3179 | } |
| 3180 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3181 | #ifdef DEBUG |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3182 | /* Dump the passed-in file structure to stderr. */ |
| 3183 | void dump_buffer(const filestruct *inptr) |
| 3184 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3185 | if (inptr == fileage) |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 3186 | fprintf(stderr, "Dumping file buffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3187 | else if (inptr == cutbuffer) |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 3188 | fprintf(stderr, "Dumping cutbuffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3189 | else |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 3190 | fprintf(stderr, "Dumping a buffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3191 | |
| 3192 | while (inptr != NULL) { |
| 3193 | fprintf(stderr, "(%d) %s\n", inptr->lineno, inptr->data); |
| 3194 | inptr = inptr->next; |
| 3195 | } |
| 3196 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3197 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 3198 | /* Dump the file structure to stderr in reverse. */ |
David Lawrence Ramsey | aaad3af | 2003-08-31 16:44:10 +0000 | [diff] [blame] | 3199 | void dump_buffer_reverse(void) |
| 3200 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 3201 | const filestruct *fileptr = filebot; |
| 3202 | |
| 3203 | while (fileptr != NULL) { |
| 3204 | fprintf(stderr, "(%d) %s\n", fileptr->lineno, fileptr->data); |
| 3205 | fileptr = fileptr->prev; |
| 3206 | } |
| 3207 | } |
| 3208 | #endif /* DEBUG */ |
| 3209 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3210 | #ifdef NANO_EXTRA |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 3211 | #define CREDIT_LEN 53 |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 3212 | #define XLCREDIT_LEN 8 |
| 3213 | |
David Lawrence Ramsey | fdece46 | 2004-01-19 18:15:03 +0000 | [diff] [blame] | 3214 | /* Easter egg: Display credits. Assume nodelay(edit) is FALSE. */ |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3215 | void do_credits(void) |
| 3216 | { |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3217 | int crpos = 0, xlpos = 0; |
| 3218 | const char *credits[CREDIT_LEN] = { |
| 3219 | NULL, /* "The nano text editor" */ |
| 3220 | NULL, /* "version" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3221 | VERSION, |
| 3222 | "", |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3223 | NULL, /* "Brought to you by:" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3224 | "Chris Allegretta", |
| 3225 | "Jordi Mallach", |
| 3226 | "Adam Rogoyski", |
| 3227 | "Rob Siemborski", |
| 3228 | "Rocco Corsi", |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 3229 | "David Lawrence Ramsey", |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 3230 | "David Benbennick", |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3231 | "Ken Tyler", |
| 3232 | "Sven Guckes", |
| 3233 | "Florian König", |
| 3234 | "Pauli Virtanen", |
| 3235 | "Daniele Medri", |
| 3236 | "Clement Laforet", |
| 3237 | "Tedi Heriyanto", |
| 3238 | "Bill Soudan", |
| 3239 | "Christian Weisgerber", |
| 3240 | "Erik Andersen", |
| 3241 | "Big Gaute", |
| 3242 | "Joshua Jensen", |
| 3243 | "Ryan Krebs", |
| 3244 | "Albert Chin", |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3245 | "", |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3246 | NULL, /* "Special thanks to:" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3247 | "Plattsburgh State University", |
| 3248 | "Benet Laboratories", |
| 3249 | "Amy Allegretta", |
| 3250 | "Linda Young", |
| 3251 | "Jeremy Robichaud", |
| 3252 | "Richard Kolb II", |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3253 | NULL, /* "The Free Software Foundation" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3254 | "Linus Torvalds", |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3255 | NULL, /* "For ncurses:" */ |
Chris Allegretta | dce44ab | 2002-03-16 01:03:41 +0000 | [diff] [blame] | 3256 | "Thomas Dickey", |
| 3257 | "Pavel Curtis", |
| 3258 | "Zeyd Ben-Halim", |
| 3259 | "Eric S. Raymond", |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3260 | NULL, /* "and anyone else we forgot..." */ |
| 3261 | NULL, /* "Thank you for using nano!" */ |
| 3262 | "", |
| 3263 | "", |
| 3264 | "", |
| 3265 | "", |
David Lawrence Ramsey | 6481c3f | 2004-01-09 23:06:54 +0000 | [diff] [blame] | 3266 | "(c) 1999-2004 Chris Allegretta", |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3267 | "", |
| 3268 | "", |
| 3269 | "", |
| 3270 | "", |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 3271 | "http://www.nano-editor.org/" |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3272 | }; |
| 3273 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3274 | const char *xlcredits[XLCREDIT_LEN] = { |
David Lawrence Ramsey | 837a02b | 2004-05-18 15:23:31 +0000 | [diff] [blame] | 3275 | "The nano text editor", |
| 3276 | "version", |
| 3277 | "Brought to you by:", |
| 3278 | "Special thanks to:", |
| 3279 | "The Free Software Foundation", |
| 3280 | "For ncurses:", |
| 3281 | "and anyone else we forgot...", |
| 3282 | "Thank you for using nano!" |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3283 | }; |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 3284 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3285 | curs_set(0); |
| 3286 | nodelay(edit, TRUE); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3287 | scrollok(edit, TRUE); |
| 3288 | blank_titlebar(); |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 3289 | blank_edit(); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3290 | blank_statusbar(); |
| 3291 | blank_bottombars(); |
| 3292 | wrefresh(topwin); |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 3293 | wrefresh(edit); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3294 | wrefresh(bottomwin); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3295 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3296 | for (crpos = 0; crpos < CREDIT_LEN + editwinrows / 2; crpos++) { |
| 3297 | if (wgetch(edit) != ERR) |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3298 | break; |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3299 | if (crpos < CREDIT_LEN) { |
| 3300 | const char *what = credits[crpos]; |
| 3301 | size_t start_x; |
| 3302 | |
| 3303 | if (what == NULL) { |
| 3304 | assert(0 <= xlpos && xlpos < XLCREDIT_LEN); |
David Lawrence Ramsey | 837a02b | 2004-05-18 15:23:31 +0000 | [diff] [blame] | 3305 | what = _(xlcredits[xlpos]); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3306 | xlpos++; |
| 3307 | } |
| 3308 | start_x = COLS / 2 - strlen(what) / 2 - 1; |
| 3309 | mvwaddstr(edit, editwinrows - 1 - editwinrows % 2, start_x, what); |
| 3310 | } |
| 3311 | napms(700); |
| 3312 | scroll(edit); |
| 3313 | wrefresh(edit); |
| 3314 | if (wgetch(edit) != ERR) |
| 3315 | break; |
| 3316 | napms(700); |
| 3317 | scroll(edit); |
| 3318 | wrefresh(edit); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3319 | } |
| 3320 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3321 | scrollok(edit, FALSE); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3322 | nodelay(edit, FALSE); |
| 3323 | curs_set(1); |
| 3324 | display_main_list(); |
| 3325 | total_refresh(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 3326 | } |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 3327 | #endif |