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 | |
David Lawrence Ramsey | f5300af | 2004-02-24 20:48:12 +0000 | [diff] [blame] | 33 | #ifdef NANO_EXTRA |
| 34 | #include <time.h> |
| 35 | #endif |
| 36 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 37 | static int statblank = 0; /* Number of keystrokes left after |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 38 | we call statusbar(), before we |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 39 | actually blank the statusbar */ |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 40 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 41 | /* 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] | 42 | * on. Otherwise, try to translate it from ASCII, extended keypad |
| 43 | * values, and/or escape sequences. Supported extended keypad values |
| 44 | * consist of [arrow key], Ctrl-[arrow key], Shift-[arrow key], Enter, |
| 45 | * Backspace, Insert, Delete, Home, End, PageUp, PageDown, and F1-F14. |
| 46 | * Assume nodelay(win) is FALSE. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 47 | int get_kbinput(WINDOW *win, int *meta) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 48 | { |
| 49 | int kbinput, retval; |
| 50 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 51 | #ifndef NANO_SMALL |
| 52 | allow_pending_sigwinch(TRUE); |
| 53 | #endif |
| 54 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 55 | kbinput = get_ignored_kbinput(win); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 56 | retval = get_accepted_kbinput(win, kbinput, meta); |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 57 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 58 | #ifndef NANO_SMALL |
| 59 | allow_pending_sigwinch(FALSE); |
| 60 | #endif |
| 61 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 62 | return retval; |
| 63 | } |
| 64 | |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 65 | /* Read in a string of input characters (e.g. an escape sequence) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 66 | * verbatim, and return the length of the string in kbinput_len. Assume |
| 67 | * nodelay(win) is FALSE. */ |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 68 | int *get_verbatim_kbinput(WINDOW *win, int *kbinput_len, int |
| 69 | allow_ascii) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 70 | { |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 71 | int kbinput, *verbatim_kbinput; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 72 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 73 | #ifndef NANO_SMALL |
| 74 | allow_pending_sigwinch(TRUE); |
| 75 | #endif |
| 76 | |
David Lawrence Ramsey | 58f6d83 | 2004-01-27 07:12:47 +0000 | [diff] [blame] | 77 | /* Turn the keypad off so that we don't get extended keypad values, |
David Lawrence Ramsey | d03216a | 2004-01-28 18:21:21 +0000 | [diff] [blame] | 78 | * all of which are outside the ASCII range, and switch to raw mode |
David Lawrence Ramsey | 1bc9219 | 2004-02-03 17:36:55 +0000 | [diff] [blame] | 79 | * so that we can type ^C, ^Q, ^S, ^Z, and ^\ (and ^Y on the Hurd) |
| 80 | * without getting interrupts. */ |
David Lawrence Ramsey | 58f6d83 | 2004-01-27 07:12:47 +0000 | [diff] [blame] | 81 | keypad(win, FALSE); |
David Lawrence Ramsey | 273d2ce | 2004-01-30 04:20:28 +0000 | [diff] [blame] | 82 | #ifdef _POSIX_VDISABLE |
David Lawrence Ramsey | d03216a | 2004-01-28 18:21:21 +0000 | [diff] [blame] | 83 | raw(); |
| 84 | #endif |
David Lawrence Ramsey | 58f6d83 | 2004-01-27 07:12:47 +0000 | [diff] [blame] | 85 | |
| 86 | kbinput = wgetch(win); |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 87 | verbatim_kbinput = (int *)nmalloc(sizeof(int)); |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 88 | verbatim_kbinput[0] = kbinput; |
| 89 | *kbinput_len = 1; |
| 90 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 91 | if (allow_ascii && kbinput >= '0' && kbinput <= '2') |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 92 | /* Entering a three-digit decimal ASCII code from 000-255 in |
| 93 | * verbatim mode will produce the corresponding ASCII |
| 94 | * character. */ |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 95 | verbatim_kbinput[0] = get_ascii_kbinput(win, kbinput); |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 96 | else { |
| 97 | nodelay(win, TRUE); |
| 98 | while ((kbinput = wgetch(win)) != ERR) { |
David Lawrence Ramsey | da8fd8f | 2003-09-16 01:22:31 +0000 | [diff] [blame] | 99 | (*kbinput_len)++; |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 100 | verbatim_kbinput = realloc(verbatim_kbinput, *kbinput_len * sizeof(int)); |
| 101 | verbatim_kbinput[*kbinput_len - 1] = kbinput; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 102 | } |
| 103 | nodelay(win, FALSE); |
| 104 | } |
| 105 | |
David Lawrence Ramsey | d03216a | 2004-01-28 18:21:21 +0000 | [diff] [blame] | 106 | /* Turn the keypad back on and switch back to cbreak mode now that |
| 107 | * we're done. */ |
David Lawrence Ramsey | 58f6d83 | 2004-01-27 07:12:47 +0000 | [diff] [blame] | 108 | keypad(win, TRUE); |
David Lawrence Ramsey | 273d2ce | 2004-01-30 04:20:28 +0000 | [diff] [blame] | 109 | #ifdef _POSIX_VDISABLE |
David Lawrence Ramsey | d03216a | 2004-01-28 18:21:21 +0000 | [diff] [blame] | 110 | cbreak(); |
| 111 | #endif |
David Lawrence Ramsey | 58f6d83 | 2004-01-27 07:12:47 +0000 | [diff] [blame] | 112 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 113 | #ifdef DEBUG |
| 114 | fprintf(stderr, "get_verbatim_kbinput(): verbatim_kbinput = %s\n", verbatim_kbinput); |
| 115 | #endif |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 116 | |
| 117 | #ifndef NANO_SMALL |
| 118 | allow_pending_sigwinch(FALSE); |
| 119 | #endif |
| 120 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 121 | return verbatim_kbinput; |
| 122 | } |
| 123 | |
| 124 | /* Swallow input characters that should be quietly ignored, and return |
| 125 | * the first input character that shouldn't be. */ |
| 126 | int get_ignored_kbinput(WINDOW *win) |
| 127 | { |
| 128 | int kbinput; |
| 129 | |
| 130 | while (1) { |
| 131 | kbinput = wgetch(win); |
| 132 | switch (kbinput) { |
| 133 | case ERR: |
| 134 | case KEY_RESIZE: |
| 135 | #ifdef PDCURSES |
| 136 | case KEY_SHIFT_L: |
| 137 | case KEY_SHIFT_R: |
| 138 | case KEY_CONTROL_L: |
| 139 | case KEY_CONTROL_R: |
| 140 | case KEY_ALT_L: |
| 141 | case KEY_ALT_R: |
| 142 | #endif |
| 143 | #ifdef DEBUG |
| 144 | fprintf(stderr, "get_ignored_kbinput(): kbinput = %d\n", kbinput); |
| 145 | #endif |
| 146 | break; |
| 147 | default: |
| 148 | return kbinput; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 153 | /* Translate acceptable ASCII, extended keypad values, and/or escape |
| 154 | * sequences. Set meta to 1 if we get a Meta sequence. Assume |
| 155 | * nodelay(win) is FALSE. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 156 | int get_accepted_kbinput(WINDOW *win, int kbinput, int *meta) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 157 | { |
| 158 | *meta = 0; |
| 159 | |
| 160 | switch (kbinput) { |
| 161 | case NANO_CONTROL_3: /* Escape */ |
David Lawrence Ramsey | 2506136 | 2004-01-16 19:12:46 +0000 | [diff] [blame] | 162 | kbinput = wgetch(win); |
| 163 | switch (kbinput) { |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 164 | case NANO_CONTROL_3: /* Escape */ |
| 165 | kbinput = wgetch(win); |
| 166 | /* Esc Esc [three-digit decimal ASCII code from |
| 167 | * 000-255] == [corresponding ASCII character]; |
David Lawrence Ramsey | d390dd8 | 2004-02-07 03:38:02 +0000 | [diff] [blame] | 168 | * Esc Esc 2 obviously can't be Ctrl-2 here */ |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 169 | if (kbinput >= '0' && kbinput <= '2') |
| 170 | kbinput = get_ascii_kbinput(win, kbinput); |
| 171 | /* Esc Esc [character] == Ctrl-[character]; |
| 172 | * Ctrl-Space (Ctrl-2) == Ctrl-@ == Ctrl-` */ |
| 173 | else if (kbinput == ' ' || kbinput == '@' || kbinput == '`') |
| 174 | kbinput = NANO_CONTROL_SPACE; |
| 175 | /* Ctrl-3 (Ctrl-[, Esc) to Ctrl-7 (Ctrl-_) */ |
| 176 | else if (kbinput >= '3' && kbinput <= '7') |
| 177 | kbinput -= 24; |
| 178 | /* Ctrl-8 (Ctrl-?) */ |
| 179 | else if (kbinput == '8' || kbinput == '?') |
| 180 | kbinput = NANO_CONTROL_8; |
| 181 | /* Ctrl-A to Ctrl-_ */ |
| 182 | else if (kbinput >= 'A' && kbinput <= '_') |
| 183 | kbinput -= 64; |
David Lawrence Ramsey | da8fd8f | 2003-09-16 01:22:31 +0000 | [diff] [blame] | 184 | /* Ctrl-A to Ctrl-~ */ |
| 185 | else if (kbinput >= 'a' && kbinput <= '~') |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 186 | kbinput -= 96; |
| 187 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 188 | case 'O': |
| 189 | case 'o': |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 190 | /* Terminal breakage, part 1: We shouldn't get an escape |
| 191 | * sequence here for terminals that support Delete, but |
| 192 | * we do sometimes on FreeBSD. Thank you, Wouter van |
| 193 | * Hemel. */ |
| 194 | case '[': |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 195 | { |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 196 | int old_kbinput = kbinput, *escape_seq, escape_seq_len; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 197 | nodelay(win, TRUE); |
| 198 | kbinput = wgetch(win); |
| 199 | switch (kbinput) { |
| 200 | case ERR: |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 201 | kbinput = tolower(old_kbinput); |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 202 | *meta = 1; |
| 203 | break; |
| 204 | default: |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 205 | ungetch(kbinput); |
| 206 | ungetch(old_kbinput); |
| 207 | escape_seq = get_verbatim_kbinput(win, &escape_seq_len, 0); |
| 208 | kbinput = get_escape_seq_kbinput(win, escape_seq, escape_seq_len); |
| 209 | free(escape_seq); |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 210 | } |
| 211 | nodelay(win, FALSE); |
| 212 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 213 | } |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 214 | default: |
| 215 | /* Esc [character] == Meta-[character] */ |
David Lawrence Ramsey | da8fd8f | 2003-09-16 01:22:31 +0000 | [diff] [blame] | 216 | kbinput = tolower(kbinput); |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 217 | *meta = 1; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 218 | } |
| 219 | break; |
David Lawrence Ramsey | 7776ef9 | 2003-11-04 18:32:35 +0000 | [diff] [blame] | 220 | case NANO_CONTROL_8: |
| 221 | /* Terminal breakage, part 2: We shouldn't get Ctrl-8 |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 222 | * (Ctrl-?) for Backspace or Delete, but we do sometimes. */ |
| 223 | kbinput = ISSET(REBIND_DELETE) ? NANO_DELETE_KEY : NANO_BACKSPACE_KEY; |
David Lawrence Ramsey | 7776ef9 | 2003-11-04 18:32:35 +0000 | [diff] [blame] | 224 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 225 | case KEY_DOWN: |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 226 | kbinput = NANO_NEXTLINE_KEY; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 227 | break; |
| 228 | case KEY_UP: |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 229 | kbinput = NANO_PREVLINE_KEY; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 230 | break; |
| 231 | case KEY_LEFT: |
| 232 | kbinput = NANO_BACK_KEY; |
| 233 | break; |
| 234 | case KEY_RIGHT: |
| 235 | kbinput = NANO_FORWARD_KEY; |
| 236 | break; |
| 237 | case KEY_HOME: |
| 238 | kbinput = NANO_HOME_KEY; |
| 239 | break; |
| 240 | case KEY_BACKSPACE: |
| 241 | kbinput = NANO_BACKSPACE_KEY; |
| 242 | break; |
| 243 | case KEY_DC: |
David Lawrence Ramsey | 7776ef9 | 2003-11-04 18:32:35 +0000 | [diff] [blame] | 244 | /* Terminal breakage, part 3: We should only get KEY_DC when |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 245 | * hitting Delete, but we get it when hitting Backspace |
| 246 | * sometimes on FreeBSD. Thank you, Lee Nelson. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 247 | kbinput = ISSET(REBIND_DELETE) ? NANO_BACKSPACE_KEY : NANO_DELETE_KEY; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 248 | break; |
| 249 | case KEY_IC: |
| 250 | kbinput = NANO_INSERTFILE_KEY; |
| 251 | break; |
| 252 | case KEY_NPAGE: |
| 253 | kbinput = NANO_NEXTPAGE_KEY; |
| 254 | break; |
| 255 | case KEY_PPAGE: |
| 256 | kbinput = NANO_PREVPAGE_KEY; |
| 257 | break; |
| 258 | case KEY_ENTER: |
| 259 | kbinput = NANO_ENTER_KEY; |
| 260 | break; |
| 261 | case KEY_END: |
| 262 | kbinput = NANO_END_KEY; |
| 263 | break; |
| 264 | case KEY_SUSPEND: |
| 265 | kbinput = NANO_SUSPEND_KEY; |
| 266 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 267 | case KEY_SLEFT: |
| 268 | kbinput = NANO_BACK_KEY; |
| 269 | break; |
| 270 | case KEY_SRIGHT: |
| 271 | kbinput = NANO_FORWARD_KEY; |
| 272 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 273 | } |
| 274 | #ifdef DEBUG |
| 275 | fprintf(stderr, "get_accepted_kbinput(): kbinput = %d, meta = %d\n", kbinput, *meta); |
| 276 | #endif |
| 277 | return kbinput; |
| 278 | } |
| 279 | |
| 280 | /* Translate a three-digit decimal ASCII code from 000-255 into the |
| 281 | * corresponding ASCII character. */ |
| 282 | int get_ascii_kbinput(WINDOW *win, int kbinput) |
| 283 | { |
| 284 | int retval; |
| 285 | |
| 286 | switch (kbinput) { |
| 287 | case '0': |
| 288 | case '1': |
| 289 | case '2': |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 290 | retval = (kbinput - '0') * 100; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 291 | break; |
| 292 | default: |
| 293 | return kbinput; |
| 294 | } |
| 295 | |
| 296 | kbinput = wgetch(win); |
| 297 | switch (kbinput) { |
| 298 | case '0': |
| 299 | case '1': |
| 300 | case '2': |
| 301 | case '3': |
| 302 | case '4': |
| 303 | case '5': |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 304 | retval += (kbinput - '0') * 10; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 305 | break; |
| 306 | case '6': |
| 307 | case '7': |
| 308 | case '8': |
| 309 | case '9': |
| 310 | if (retval < 200) { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 311 | retval += (kbinput - '0') * 10; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 312 | break; |
| 313 | } |
| 314 | default: |
| 315 | return kbinput; |
| 316 | } |
| 317 | |
| 318 | kbinput = wgetch(win); |
| 319 | switch (kbinput) { |
| 320 | case '0': |
| 321 | case '1': |
| 322 | case '2': |
| 323 | case '3': |
| 324 | case '4': |
| 325 | case '5': |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 326 | retval += kbinput - '0'; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 327 | break; |
| 328 | case '6': |
| 329 | case '7': |
| 330 | case '8': |
| 331 | case '9': |
| 332 | if (retval < 250) { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 333 | retval += kbinput - '0'; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | default: |
| 337 | return kbinput; |
| 338 | } |
| 339 | |
| 340 | #ifdef DEBUG |
| 341 | fprintf(stderr, "get_ascii_kbinput(): kbinput = %d\n", kbinput); |
| 342 | #endif |
| 343 | return retval; |
| 344 | } |
| 345 | |
David Lawrence Ramsey | 58f6d83 | 2004-01-27 07:12:47 +0000 | [diff] [blame] | 346 | /* Translate escape sequences, most of which correspond to extended |
| 347 | * keypad values. These sequences are generated when the terminal |
| 348 | * doesn't support the needed keys. Assume that Escape has already been |
| 349 | * read in, and that nodelay(win) is TRUE. |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 350 | * |
| 351 | * The supported terminals are the Linux console, the FreeBSD console, |
| 352 | * the Hurd console (a.k.a. the Mach console), xterm, rxvt, and Eterm. |
| 353 | * There are several escape sequence conflicts and omissions, outlined |
| 354 | * as follows: |
| 355 | * |
| 356 | * - F1 on FreeBSD console == kmous on xterm/rxvt/Eterm; the latter is |
| 357 | * omitted. (Mouse input will only work properly if the extended |
| 358 | * keypad value KEY_MOUSE is generated on mouse events instead of the |
| 359 | * kmous escape sequence.) |
| 360 | * - F9 on FreeBSD console == PageDown on Hurd console; the former is |
David Lawrence Ramsey | 0ee54d9 | 2004-01-26 20:18:52 +0000 | [diff] [blame] | 361 | * omitted. (The editing keypad, consisting of Insert, Delete, Home, |
| 362 | * End, PageUp, and PageDown, is more important to have working than |
| 363 | * the function keys, because the functions of the former are not |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 364 | * arbitrary and the functions of the latter are.) |
| 365 | * - F10 on FreeBSD console == PageUp on Hurd console; the former is |
| 366 | * omitted. (Same as above.) |
| 367 | * - F13 on FreeBSD console == End on Hurd console; the former is |
| 368 | * omitted. (Same as above.) |
| 369 | * - The Hurd console has no escape sequences for F11, F12, F13, or |
| 370 | * F14. */ |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 371 | int get_escape_seq_kbinput(WINDOW *win, int *escape_seq, int |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 372 | escape_seq_len) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 373 | { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 374 | int kbinput = ERR; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 375 | |
| 376 | if (escape_seq_len > 1) { |
| 377 | switch (escape_seq[0]) { |
| 378 | case 'O': |
| 379 | switch (escape_seq[1]) { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 380 | case '2': |
| 381 | if (escape_seq_len >= 3) { |
| 382 | switch (escape_seq[2]) { |
| 383 | case 'P': /* Esc O 2 P == F13 on |
| 384 | * xterm. */ |
| 385 | kbinput = KEY_F(13); |
| 386 | break; |
| 387 | case 'Q': /* Esc O 2 Q == F14 on |
| 388 | * xterm. */ |
| 389 | kbinput = KEY_F(14); |
| 390 | break; |
| 391 | } |
| 392 | } |
| 393 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 394 | case 'A': /* Esc O A == Up on xterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 395 | case 'B': /* Esc O B == Down on xterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 396 | case 'C': /* Esc O C == Right on xterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 397 | case 'D': /* Esc O D == Left on xterm. */ |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 398 | kbinput = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 399 | break; |
| 400 | case 'F': /* Esc O F == End on xterm. */ |
| 401 | kbinput = NANO_END_KEY; |
| 402 | break; |
| 403 | case 'H': /* Esc O H == Home on xterm. */ |
| 404 | kbinput = NANO_HOME_KEY; |
| 405 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 406 | case 'P': /* Esc O P == F1 on Hurd console. */ |
| 407 | kbinput = KEY_F(1); |
| 408 | break; |
| 409 | case 'Q': /* Esc O Q == F2 on Hurd console. */ |
| 410 | kbinput = KEY_F(2); |
| 411 | break; |
| 412 | case 'R': /* Esc O R == F3 on Hurd console. */ |
| 413 | kbinput = KEY_F(3); |
| 414 | break; |
| 415 | case 'S': /* Esc O S == F4 on Hurd console. */ |
| 416 | kbinput = KEY_F(4); |
| 417 | break; |
| 418 | case 'T': /* Esc O T == F5 on Hurd console. */ |
| 419 | kbinput = KEY_F(5); |
| 420 | break; |
| 421 | case 'U': /* Esc O U == F6 on Hurd console. */ |
| 422 | kbinput = KEY_F(6); |
| 423 | break; |
| 424 | case 'V': /* Esc O V == F7 on Hurd console. */ |
| 425 | kbinput = KEY_F(7); |
| 426 | break; |
| 427 | case 'W': /* Esc O W == F8 on Hurd console. */ |
| 428 | kbinput = KEY_F(8); |
| 429 | break; |
| 430 | case 'X': /* Esc O X == F9 on Hurd console. */ |
| 431 | kbinput = KEY_F(9); |
| 432 | break; |
| 433 | case 'Y': /* Esc O Y == F10 on Hurd console. */ |
| 434 | kbinput = KEY_F(10); |
| 435 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 436 | case 'a': /* Esc O a == Ctrl-Up on rxvt. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 437 | case 'b': /* Esc O b == Ctrl-Down on rxvt. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 438 | case 'c': /* Esc O c == Ctrl-Right on rxvt. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 439 | case 'd': /* Esc O d == Ctrl-Left on rxvt. */ |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 440 | kbinput = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 441 | break; |
| 442 | } |
| 443 | break; |
| 444 | case 'o': |
| 445 | switch (escape_seq[1]) { |
| 446 | case 'a': /* Esc o a == Ctrl-Up on Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 447 | case 'b': /* Esc o b == Ctrl-Down on Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 448 | case 'c': /* Esc o c == Ctrl-Right on Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 449 | case 'd': /* Esc o d == Ctrl-Left on Eterm. */ |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 450 | kbinput = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 451 | break; |
| 452 | } |
| 453 | break; |
| 454 | case '[': |
| 455 | switch (escape_seq[1]) { |
| 456 | case '1': |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 457 | if (escape_seq_len >= 3) { |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 458 | switch (escape_seq[2]) { |
| 459 | case '1': /* Esc [ 1 1 ~ == F1 on |
| 460 | * rxvt/Eterm. */ |
| 461 | kbinput = KEY_F(1); |
| 462 | break; |
| 463 | case '2': /* Esc [ 1 2 ~ == F2 on |
| 464 | * rxvt/Eterm. */ |
| 465 | kbinput = KEY_F(2); |
| 466 | break; |
| 467 | case '3': /* Esc [ 1 3 ~ == F3 on |
| 468 | * rxvt/Eterm. */ |
| 469 | kbinput = KEY_F(3); |
| 470 | break; |
| 471 | case '4': /* Esc [ 1 4 ~ == F4 on |
| 472 | * rxvt/Eterm. */ |
| 473 | kbinput = KEY_F(4); |
| 474 | break; |
| 475 | case '5': /* Esc [ 1 5 ~ == F5 on |
| 476 | * xterm/rxvt/Eterm. */ |
| 477 | kbinput = KEY_F(5); |
| 478 | break; |
| 479 | case '7': /* Esc [ 1 7 ~ == F6 on Linux |
| 480 | * console/xterm/rxvt/Eterm. */ |
| 481 | kbinput = KEY_F(6); |
| 482 | break; |
| 483 | case '8': /* Esc [ 1 8 ~ == F7 on Linux |
| 484 | * console/xterm/rxvt/Eterm. */ |
| 485 | kbinput = KEY_F(7); |
| 486 | break; |
| 487 | case '9': /* Esc [ 1 9 ~ == F8 on Linux |
| 488 | * console/xterm/rxvt/Eterm. */ |
| 489 | kbinput = KEY_F(8); |
| 490 | break; |
David Lawrence Ramsey | ee383db | 2004-02-06 03:07:10 +0000 | [diff] [blame] | 491 | case ';': |
| 492 | if (escape_seq_len >= 4) { |
| 493 | switch (escape_seq[3]) { |
| 494 | case '2': |
| 495 | if (escape_seq_len >= 5) { |
| 496 | switch (escape_seq[4]) { |
| 497 | case 'A': /* Esc [ 1 ; 2 A == Shift-Up on |
| 498 | * xterm. */ |
| 499 | case 'B': /* Esc [ 1 ; 2 B == Shift-Down on |
| 500 | * xterm. */ |
| 501 | case 'C': /* Esc [ 1 ; 2 C == Shift-Right on |
| 502 | * xterm. */ |
| 503 | case 'D': /* Esc [ 1 ; 2 D == Shift-Left on |
| 504 | * xterm. */ |
| 505 | kbinput = get_escape_seq_abcd(escape_seq[4]); |
| 506 | break; |
| 507 | } |
| 508 | } |
| 509 | break; |
| 510 | case '5': |
| 511 | if (escape_seq_len >= 5) { |
| 512 | switch (escape_seq[4]) { |
| 513 | case 'A': /* Esc [ 1 ; 5 A == Ctrl-Up on |
| 514 | * xterm. */ |
| 515 | case 'B': /* Esc [ 1 ; 5 B == Ctrl-Down on |
| 516 | * xterm. */ |
| 517 | case 'C': /* Esc [ 1 ; 5 C == Ctrl-Right on |
| 518 | * xterm. */ |
| 519 | case 'D': /* Esc [ 1 ; 5 D == Ctrl-Left on |
| 520 | * xterm. */ |
| 521 | kbinput = get_escape_seq_abcd(escape_seq[4]); |
| 522 | break; |
| 523 | } |
| 524 | } |
| 525 | break; |
| 526 | } |
| 527 | } |
| 528 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 529 | default: /* Esc [ 1 ~ == Home on Linux |
| 530 | * console. */ |
| 531 | kbinput = NANO_HOME_KEY; |
| 532 | break; |
| 533 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 534 | } |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 535 | break; |
| 536 | case '2': |
| 537 | if (escape_seq_len >= 3) { |
| 538 | switch (escape_seq[2]) { |
| 539 | case '0': /* Esc [ 2 0 ~ == F9 on Linux |
| 540 | * console/xterm/rxvt/Eterm. */ |
| 541 | kbinput = KEY_F(9); |
| 542 | break; |
| 543 | case '1': /* Esc [ 2 1 ~ == F10 on Linux |
| 544 | * console/xterm/rxvt/Eterm. */ |
| 545 | kbinput = KEY_F(10); |
| 546 | break; |
| 547 | case '3': /* Esc [ 2 3 ~ == F11 on Linux |
| 548 | * console/xterm/rxvt/Eterm. */ |
| 549 | kbinput = KEY_F(11); |
| 550 | break; |
| 551 | case '4': /* Esc [ 2 4 ~ == F12 on Linux |
| 552 | * console/xterm/rxvt/Eterm. */ |
| 553 | kbinput = KEY_F(12); |
| 554 | break; |
| 555 | case '5': /* Esc [ 2 5 ~ == F13 on Linux |
| 556 | * console/rxvt/Eterm. */ |
| 557 | kbinput = KEY_F(13); |
| 558 | break; |
| 559 | case '6': /* Esc [ 2 6 ~ == F14 on Linux |
| 560 | * console/rxvt/Eterm. */ |
| 561 | kbinput = KEY_F(14); |
| 562 | break; |
| 563 | default: /* Esc [ 2 ~ == Insert on Linux |
| 564 | * console/xterm. */ |
| 565 | kbinput = NANO_INSERTFILE_KEY; |
| 566 | break; |
| 567 | } |
| 568 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 569 | break; |
| 570 | case '3': /* Esc [ 3 ~ == Delete on Linux |
| 571 | * console/xterm. */ |
| 572 | kbinput = NANO_DELETE_KEY; |
| 573 | break; |
| 574 | case '4': /* Esc [ 4 ~ == End on Linux |
| 575 | * console/xterm. */ |
| 576 | kbinput = NANO_END_KEY; |
| 577 | break; |
| 578 | case '5': /* Esc [ 5 ~ == PageUp on Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 579 | * console/xterm; Esc [ 5 ^ == PageUp on |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 580 | * Eterm. */ |
| 581 | kbinput = NANO_PREVPAGE_KEY; |
| 582 | break; |
| 583 | case '6': /* Esc [ 6 ~ == PageDown on Linux |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 584 | * console/xterm; Esc [ 6 ^ == PageDown on |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 585 | * Eterm. */ |
| 586 | kbinput = NANO_NEXTPAGE_KEY; |
| 587 | break; |
| 588 | case '7': /* Esc [ 7 ~ == Home on rxvt. */ |
| 589 | kbinput = NANO_HOME_KEY; |
| 590 | break; |
| 591 | case '8': /* Esc [ 8 ~ == End on rxvt. */ |
| 592 | kbinput = NANO_END_KEY; |
| 593 | break; |
| 594 | case '9': /* Esc [ 9 == Delete on Hurd console. */ |
| 595 | kbinput = NANO_DELETE_KEY; |
| 596 | break; |
| 597 | case '@': /* Esc [ @ == Insert on Hurd console. */ |
| 598 | kbinput = NANO_INSERTFILE_KEY; |
| 599 | break; |
| 600 | case 'A': /* Esc [ A == Up on Linux console/FreeBSD |
| 601 | * console/Hurd console/rxvt/Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 602 | case 'B': /* Esc [ B == Down on Linux |
| 603 | * console/FreeBSD console/Hurd |
| 604 | * console/rxvt/Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 605 | case 'C': /* Esc [ C == Right on Linux |
| 606 | * console/FreeBSD console/Hurd |
| 607 | * console/rxvt/Eterm. */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 608 | case 'D': /* Esc [ D == Left on Linux |
| 609 | * console/FreeBSD console/Hurd |
| 610 | * console/rxvt/Eterm. */ |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 611 | kbinput = get_escape_seq_abcd(escape_seq[1]); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 612 | break; |
| 613 | case 'F': /* Esc [ F == End on FreeBSD |
| 614 | * console/Eterm. */ |
| 615 | kbinput = NANO_END_KEY; |
| 616 | break; |
| 617 | case 'G': /* Esc [ G == PageDown on FreeBSD |
| 618 | * console. */ |
| 619 | kbinput = NANO_NEXTPAGE_KEY; |
| 620 | break; |
| 621 | case 'H': /* Esc [ H == Home on FreeBSD |
| 622 | * console/Hurd console/Eterm. */ |
| 623 | kbinput = NANO_HOME_KEY; |
| 624 | break; |
| 625 | case 'I': /* Esc [ I == PageUp on FreeBSD |
| 626 | * console. */ |
| 627 | kbinput = NANO_PREVPAGE_KEY; |
| 628 | break; |
| 629 | case 'L': /* Esc [ L == Insert on FreeBSD |
| 630 | * console. */ |
| 631 | kbinput = NANO_INSERTFILE_KEY; |
| 632 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 633 | case 'M': /* Esc [ M == F1 on FreeBSD console. */ |
| 634 | kbinput = KEY_F(1); |
| 635 | break; |
| 636 | case 'N': /* Esc [ N == F2 on FreeBSD console. */ |
| 637 | kbinput = KEY_F(2); |
| 638 | break; |
| 639 | case 'O': |
| 640 | if (escape_seq_len >= 3) { |
| 641 | switch (escape_seq[2]) { |
| 642 | case 'P': /* Esc [ O P == F1 on |
| 643 | * xterm. */ |
| 644 | kbinput = KEY_F(1); |
| 645 | break; |
| 646 | case 'Q': /* Esc [ O Q == F2 on |
| 647 | * xterm. */ |
| 648 | kbinput = KEY_F(2); |
| 649 | break; |
| 650 | case 'R': /* Esc [ O R == F3 on |
| 651 | * xterm. */ |
| 652 | kbinput = KEY_F(3); |
| 653 | break; |
| 654 | case 'S': /* Esc [ O S == F4 on |
| 655 | * xterm. */ |
| 656 | kbinput = KEY_F(4); |
| 657 | break; |
| 658 | default: /* Esc [ O == F3 on |
| 659 | * FreeBSD console. */ |
| 660 | kbinput = KEY_F(3); |
| 661 | break; |
| 662 | } |
| 663 | } |
| 664 | break; |
| 665 | case 'P': /* Esc [ P == F4 on FreeBSD console. */ |
| 666 | kbinput = KEY_F(4); |
| 667 | break; |
| 668 | case 'Q': /* Esc [ Q == F5 on FreeBSD console. */ |
| 669 | kbinput = KEY_F(5); |
| 670 | break; |
| 671 | case 'R': /* Esc [ R == F6 on FreeBSD console. */ |
| 672 | kbinput = KEY_F(6); |
| 673 | break; |
| 674 | case 'S': /* Esc [ S == F7 on FreeBSD console. */ |
| 675 | kbinput = KEY_F(7); |
| 676 | break; |
| 677 | case 'T': /* Esc [ T == F8 on FreeBSD console. */ |
| 678 | kbinput = KEY_F(8); |
| 679 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 680 | case 'U': /* Esc [ U == PageDown on Hurd console. */ |
| 681 | kbinput = NANO_NEXTPAGE_KEY; |
| 682 | break; |
| 683 | case 'V': /* Esc [ V == PageUp on Hurd console. */ |
| 684 | kbinput = NANO_PREVPAGE_KEY; |
| 685 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 686 | case 'W': /* Esc [ W == F11 on FreeBSD console. */ |
| 687 | kbinput = KEY_F(11); |
| 688 | break; |
| 689 | case 'X': /* Esc [ X == F12 on FreeBSD console. */ |
| 690 | kbinput = KEY_F(12); |
| 691 | break; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 692 | case 'Y': /* Esc [ Y == End on Hurd console. */ |
| 693 | kbinput = NANO_END_KEY; |
| 694 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 695 | case 'Z': /* Esc [ Z == F14 on FreeBSD console. */ |
| 696 | kbinput = KEY_F(14); |
| 697 | break; |
David Lawrence Ramsey | 9b5bd42 | 2004-01-06 01:45:04 +0000 | [diff] [blame] | 698 | case 'a': /* Esc [ a == Shift-Up on rxvt/Eterm. */ |
David Lawrence Ramsey | 9b5bd42 | 2004-01-06 01:45:04 +0000 | [diff] [blame] | 699 | case 'b': /* Esc [ b == Shift-Down on rxvt/Eterm. */ |
David Lawrence Ramsey | 9b5bd42 | 2004-01-06 01:45:04 +0000 | [diff] [blame] | 700 | case 'c': /* Esc [ c == Shift-Right on |
| 701 | * rxvt/Eterm. */ |
David Lawrence Ramsey | 9b5bd42 | 2004-01-06 01:45:04 +0000 | [diff] [blame] | 702 | case 'd': /* Esc [ d == Shift-Left on rxvt/Eterm. */ |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 703 | kbinput = get_escape_seq_abcd(escape_seq[1]); |
| 704 | break; |
| 705 | case '[': |
| 706 | if (escape_seq_len >= 3) { |
| 707 | switch (escape_seq[2]) { |
| 708 | case 'A': /* Esc [ [ A == F1 on Linux |
| 709 | * console. */ |
| 710 | kbinput = KEY_F(1); |
| 711 | break; |
| 712 | case 'B': /* Esc [ [ B == F2 on Linux |
| 713 | * console. */ |
| 714 | kbinput = KEY_F(2); |
| 715 | break; |
| 716 | case 'C': /* Esc [ [ C == F3 on Linux |
| 717 | * console. */ |
| 718 | kbinput = KEY_F(3); |
| 719 | break; |
| 720 | case 'D': /* Esc [ [ D == F4 on Linux |
| 721 | * console. */ |
| 722 | kbinput = KEY_F(4); |
| 723 | break; |
| 724 | case 'E': /* Esc [ [ E == F5 on Linux |
| 725 | * console. */ |
| 726 | kbinput = KEY_F(5); |
| 727 | break; |
| 728 | } |
| 729 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 730 | break; |
| 731 | } |
| 732 | break; |
| 733 | } |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 734 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 735 | |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 736 | if (kbinput == ERR) { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 737 | /* This escape sequence is unrecognized; send it back. */ |
| 738 | for (; escape_seq_len > 1; escape_seq_len--) |
| 739 | ungetch(escape_seq[escape_seq_len - 1]); |
| 740 | kbinput = escape_seq[0]; |
| 741 | } |
| 742 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 743 | return kbinput; |
| 744 | } |
| 745 | |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 746 | /* Return the equivalent arrow key value for the case-insensitive |
David Lawrence Ramsey | b7e5cf6 | 2004-02-07 03:39:48 +0000 | [diff] [blame] | 747 | * 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] | 748 | * to many escape sequences. */ |
| 749 | int get_escape_seq_abcd(int kbinput) |
| 750 | { |
| 751 | switch (tolower(kbinput)) { |
| 752 | case 'a': |
| 753 | return NANO_PREVLINE_KEY; |
| 754 | case 'b': |
| 755 | return NANO_NEXTLINE_KEY; |
| 756 | case 'c': |
| 757 | return NANO_FORWARD_KEY; |
| 758 | case 'd': |
| 759 | return NANO_BACK_KEY; |
| 760 | default: |
| 761 | return ERR; |
| 762 | } |
| 763 | } |
| 764 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 765 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 766 | /* Check for a mouse event, and if one's taken place, save the |
| 767 | * coordinates where it took place in mouse_x and mouse_y. After that, |
| 768 | * if allow_shortcuts is zero, return 0. Otherwise, if the mouse event |
| 769 | * took place on the shortcut list on the bottom two lines of the screen |
| 770 | * (assuming that the shortcut list is visible), figure out which |
| 771 | * shortcut was clicked and ungetch() the equivalent keystroke(s). |
| 772 | * Return 0 if no keystrokes were ungetch()ed, or 1 if at least one was. |
| 773 | * Assume that KEY_MOUSE has already been read in. */ |
| 774 | 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] | 775 | { |
| 776 | MEVENT mevent; |
| 777 | |
| 778 | *mouse_x = -1; |
| 779 | *mouse_y = -1; |
| 780 | |
| 781 | /* First, get the actual mouse event. */ |
| 782 | if (getmouse(&mevent) == ERR) |
| 783 | return 0; |
| 784 | |
| 785 | /* Save the screen coordinates where the mouse event took place. */ |
| 786 | *mouse_x = mevent.x; |
| 787 | *mouse_y = mevent.y; |
| 788 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 789 | /* If we're not allowing shortcuts' we're done now. */ |
| 790 | if (!allow_shortcuts) |
| 791 | return 0; |
| 792 | |
| 793 | /* Otherwise, if the current shortcut list is being displayed on the |
| 794 | * last two lines of the screen and the mouse event took place |
| 795 | * inside it, we need to figure out which shortcut was clicked and |
| 796 | * ungetch() the equivalent keystroke(s) for it. */ |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 797 | if (!ISSET(NO_HELP) && wenclose(bottomwin, *mouse_y, *mouse_x)) { |
| 798 | int i, j; |
| 799 | int currslen; |
| 800 | /* The number of shortcuts in the current shortcut list. */ |
| 801 | const shortcut *s = currshortcut; |
| 802 | /* The actual shortcut we clicked on, starting at the first |
| 803 | * one in the current shortcut list. */ |
| 804 | |
| 805 | /* Get the shortcut lists' length. */ |
| 806 | if (currshortcut == main_list) |
| 807 | currslen = MAIN_VISIBLE; |
| 808 | else |
| 809 | currslen = length_of_list(currshortcut); |
| 810 | |
| 811 | /* Calculate the width of each shortcut in the list (it's the |
| 812 | * same for all of them). */ |
| 813 | if (currslen < 2) |
| 814 | i = COLS / 6; |
| 815 | else |
| 816 | i = COLS / ((currslen / 2) + (currslen % 2)); |
| 817 | |
| 818 | /* Calculate the y-coordinates relative to the beginning of |
| 819 | * bottomwin, i.e, the bottom three lines of the screen. */ |
| 820 | j = *mouse_y - (editwinrows + 3); |
| 821 | |
| 822 | /* If we're on the statusbar, beyond the end of the shortcut |
| 823 | * list, or beyond the end of a shortcut on the right side of |
| 824 | * the screen, don't do anything. */ |
| 825 | if (j < 0 || (*mouse_x / i) >= currslen) |
| 826 | return 0; |
| 827 | j = (*mouse_x / i) * 2 + j; |
| 828 | if (j >= currslen) |
| 829 | return 0; |
| 830 | |
| 831 | /* Go through the shortcut list to determine which shortcut was |
| 832 | * clicked. */ |
| 833 | for (; j > 0; j--) |
| 834 | s = s->next; |
| 835 | |
| 836 | /* And ungetch() the equivalent keystroke. */ |
| 837 | ungetch(s->val); |
| 838 | |
| 839 | /* If it's not a control character, assume it's a Meta key |
| 840 | * sequence, in which case we need to ungetch() Escape too. */ |
| 841 | if (!is_cntrl_char(s->val)) |
| 842 | ungetch(NANO_CONTROL_3); |
| 843 | |
| 844 | return 1; |
| 845 | } |
| 846 | return 0; |
| 847 | } |
| 848 | #endif |
| 849 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 850 | int do_first_line(void) |
| 851 | { |
| 852 | current = fileage; |
| 853 | placewewant = 0; |
| 854 | current_x = 0; |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 855 | edit_update(current, TOP); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 856 | return 1; |
| 857 | } |
| 858 | |
| 859 | int do_last_line(void) |
| 860 | { |
| 861 | current = filebot; |
| 862 | placewewant = 0; |
| 863 | current_x = 0; |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 864 | edit_update(current, CENTER); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 865 | return 1; |
| 866 | } |
| 867 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 868 | /* Return the placewewant associated with current_x. That is, xplustabs |
| 869 | * is the zero-based column position of the cursor. Value is no smaller |
| 870 | * than current_x. */ |
| 871 | size_t xplustabs(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 872 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 873 | return strnlenpt(current->data, current_x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 874 | } |
| 875 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 876 | /* actual_x() gives the index in str of the character displayed at |
| 877 | * column xplus. That is, actual_x() is the largest value such that |
| 878 | * strnlenpt(str, actual_x(str, xplus)) <= xplus. */ |
| 879 | size_t actual_x(const char *str, size_t xplus) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 880 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 881 | size_t i = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 882 | /* the position in str, returned */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 883 | size_t length = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 884 | /* the screen display width to str[i] */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 885 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 886 | assert(str != NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 887 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 888 | for (; length < xplus && *str != '\0'; i++, str++) { |
| 889 | if (*str == '\t') |
David Lawrence Ramsey | 0362c58 | 2003-09-30 03:31:56 +0000 | [diff] [blame] | 890 | length += tabsize - (length % tabsize); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 891 | else if (is_cntrl_char((int)*str)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 892 | length += 2; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 893 | else |
| 894 | length++; |
| 895 | } |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 896 | assert(length == strnlenpt(str - i, i)); |
| 897 | assert(i <= strlen(str - i)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 898 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 899 | if (length > xplus) |
| 900 | i--; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 901 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 902 | return i; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 903 | } |
| 904 | |
David Lawrence Ramsey | 5dcba30 | 2003-09-28 19:15:18 +0000 | [diff] [blame] | 905 | /* A strlen with tabs factored in, similar to xplustabs(). How many |
| 906 | * columns wide are the first size characters of buf? */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 907 | size_t strnlenpt(const char *buf, size_t size) |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 908 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 909 | size_t length = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 910 | |
David Lawrence Ramsey | 5dcba30 | 2003-09-28 19:15:18 +0000 | [diff] [blame] | 911 | assert(buf != NULL); |
| 912 | for (; *buf != '\0' && size != 0; size--, buf++) { |
| 913 | if (*buf == '\t') |
| 914 | length += tabsize - (length % tabsize); |
| 915 | else if (is_cntrl_char((int)*buf)) |
| 916 | length += 2; |
| 917 | else |
| 918 | length++; |
| 919 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 920 | return length; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 921 | } |
| 922 | |
David Lawrence Ramsey | 5dcba30 | 2003-09-28 19:15:18 +0000 | [diff] [blame] | 923 | /* How many columns wide is buf? */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 924 | size_t strlenpt(const char *buf) |
Chris Allegretta | d4fa0d3 | 2002-03-05 19:55:55 +0000 | [diff] [blame] | 925 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 926 | return strnlenpt(buf, -1); |
Chris Allegretta | d4fa0d3 | 2002-03-05 19:55:55 +0000 | [diff] [blame] | 927 | } |
| 928 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 929 | void blank_bottombars(void) |
| 930 | { |
David Lawrence Ramsey | 5dcba30 | 2003-09-28 19:15:18 +0000 | [diff] [blame] | 931 | if (!ISSET(NO_HELP)) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 932 | mvwaddstr(bottomwin, 1, 0, hblank); |
| 933 | mvwaddstr(bottomwin, 2, 0, hblank); |
| 934 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 935 | } |
| 936 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 937 | void blank_bottomwin(void) |
| 938 | { |
| 939 | if (ISSET(NO_HELP)) |
| 940 | return; |
| 941 | |
| 942 | mvwaddstr(bottomwin, 1, 0, hblank); |
| 943 | mvwaddstr(bottomwin, 2, 0, hblank); |
| 944 | } |
| 945 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 946 | void blank_edit(void) |
| 947 | { |
| 948 | int i; |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 949 | for (i = 0; i < editwinrows; i++) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 950 | mvwaddstr(edit, i, 0, hblank); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 951 | } |
| 952 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 953 | void blank_statusbar(void) |
| 954 | { |
| 955 | mvwaddstr(bottomwin, 0, 0, hblank); |
| 956 | } |
| 957 | |
| 958 | void blank_statusbar_refresh(void) |
| 959 | { |
| 960 | blank_statusbar(); |
| 961 | wrefresh(bottomwin); |
| 962 | } |
| 963 | |
| 964 | void check_statblank(void) |
| 965 | { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 966 | if (statblank > 1) |
| 967 | statblank--; |
| 968 | else if (statblank == 1 && !ISSET(CONSTUPDATE)) { |
| 969 | statblank--; |
| 970 | blank_statusbar_refresh(); |
| 971 | } |
| 972 | } |
| 973 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 974 | /* Convert buf into a string that can be displayed on screen. The |
| 975 | * caller wants to display buf starting with column start_col, and |
| 976 | * extending for at most len columns. start_col is zero-based. len is |
| 977 | * one-based, so len == 0 means you get "" returned. The returned |
| 978 | * string is dynamically allocated, and should be freed. */ |
| 979 | char *display_string(const char *buf, size_t start_col, int len) |
| 980 | { |
| 981 | size_t start_index; |
| 982 | /* Index in buf of first character shown in return value. */ |
| 983 | size_t column; |
| 984 | /* Screen column start_index corresponds to. */ |
| 985 | size_t end_index; |
| 986 | /* Index in buf of last character shown in return value. */ |
| 987 | size_t alloc_len; |
| 988 | /* The length of memory allocated for converted. */ |
| 989 | char *converted; |
| 990 | /* The string we return. */ |
| 991 | size_t index; |
| 992 | /* Current position in converted. */ |
| 993 | |
| 994 | if (len == 0) |
| 995 | return mallocstrcpy(NULL, ""); |
| 996 | |
| 997 | start_index = actual_x(buf, start_col); |
| 998 | column = strnlenpt(buf, start_index); |
| 999 | assert(column <= start_col); |
| 1000 | end_index = actual_x(buf, start_col + len - 1); |
| 1001 | alloc_len = strnlenpt(buf, end_index + 1) - column; |
| 1002 | if (len > alloc_len + column - start_col) |
| 1003 | len = alloc_len + column - start_col; |
| 1004 | converted = charalloc(alloc_len + 1); |
| 1005 | buf += start_index; |
| 1006 | index = 0; |
| 1007 | |
| 1008 | for (; index < alloc_len; buf++) { |
| 1009 | if (*buf == '\t') |
| 1010 | do { |
| 1011 | converted[index++] = ' '; |
| 1012 | } while ((column + index) % tabsize); |
| 1013 | else if (is_cntrl_char(*buf)) { |
| 1014 | converted[index++] = '^'; |
| 1015 | if (*buf == '\n') |
| 1016 | /* Treat newlines embedded in a line as encoded nulls; |
| 1017 | * the line in question should be run through unsunder() |
| 1018 | * before reaching here. */ |
| 1019 | converted[index++] = '@'; |
| 1020 | else if (*buf == NANO_CONTROL_8) |
| 1021 | converted[index++] = '?'; |
| 1022 | else |
| 1023 | converted[index++] = *buf + 64; |
| 1024 | } else |
| 1025 | converted[index++] = *buf; |
| 1026 | } |
| 1027 | assert(len <= alloc_len + column - start_col); |
| 1028 | charmove(converted, converted + start_col - column, len); |
| 1029 | null_at(&converted, len); |
| 1030 | |
| 1031 | return charealloc(converted, len + 1); |
| 1032 | } |
| 1033 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1034 | /* Repaint the statusbar when getting a character in nanogetstr(). buf |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1035 | * should be no longer than max(0, COLS - 4). |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1036 | * |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1037 | * 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] | 1038 | * off! */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1039 | void nanoget_repaint(const char *buf, const char *inputbuf, size_t x) |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 1040 | { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1041 | size_t x_real = strnlenpt(inputbuf, x); |
| 1042 | int wid = COLS - strlen(buf) - 2; |
Chris Allegretta | 0d1e8d6 | 2000-11-02 15:30:24 +0000 | [diff] [blame] | 1043 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1044 | assert(0 <= x && x <= strlen(inputbuf)); |
| 1045 | |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1046 | wattron(bottomwin, A_REVERSE); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 1047 | blank_statusbar(); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1048 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1049 | mvwaddstr(bottomwin, 0, 0, buf); |
| 1050 | waddch(bottomwin, ':'); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1051 | |
| 1052 | if (COLS > 1) |
| 1053 | waddch(bottomwin, x_real < wid ? ' ' : '$'); |
| 1054 | if (COLS > 2) { |
| 1055 | size_t page_start = x_real - x_real % wid; |
| 1056 | char *expanded = display_string(inputbuf, page_start, wid); |
| 1057 | |
| 1058 | assert(wid > 0); |
| 1059 | assert(strlen(expanded) <= wid); |
| 1060 | waddstr(bottomwin, expanded); |
| 1061 | free(expanded); |
| 1062 | wmove(bottomwin, 0, COLS - wid + x_real - page_start); |
| 1063 | } else |
| 1064 | wmove(bottomwin, 0, COLS - 1); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1065 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 1066 | } |
| 1067 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1068 | /* Get the input from the kb; this should only be called from |
| 1069 | * statusq(). */ |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1070 | int nanogetstr(int allowtabs, const char *buf, const char *def, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1071 | #ifndef NANO_SMALL |
| 1072 | historyheadtype *history_list, |
| 1073 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1074 | const shortcut *s |
Rocco Corsi | 06aca1c | 2001-01-11 05:30:31 +0000 | [diff] [blame] | 1075 | #ifndef DISABLE_TABCOMP |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1076 | , int *list |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1077 | #endif |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1078 | ) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1079 | { |
| 1080 | int kbinput; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1081 | int meta; |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1082 | static int x = -1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1083 | /* the cursor position in 'answer' */ |
| 1084 | int xend; |
| 1085 | /* length of 'answer', the status bar text */ |
| 1086 | int tabbed = 0; |
| 1087 | /* used by input_tab() */ |
| 1088 | const shortcut *t; |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1089 | |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1090 | #ifndef NANO_SMALL |
| 1091 | /* for history */ |
| 1092 | char *history = NULL; |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1093 | char *currentbuf = NULL; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1094 | char *complete = NULL; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1095 | int last_kbinput = 0; |
| 1096 | |
| 1097 | /* This variable is used in the search history code. use_cb == 0 |
| 1098 | means that we're using the existing history and ignoring |
| 1099 | currentbuf. use_cb == 1 means that the entry in answer should be |
| 1100 | moved to currentbuf or restored from currentbuf to answer. |
| 1101 | use_cb == 2 means that the entry in currentbuf should be moved to |
| 1102 | answer or restored from answer to currentbuf. */ |
| 1103 | int use_cb = 0; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1104 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1105 | xend = strlen(def); |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1106 | |
| 1107 | /* Only put x at the end of the string if it's uninitialized or if |
| 1108 | it would be past the end of the string as it is. Otherwise, |
| 1109 | leave it alone. This is so the cursor position stays at the same |
| 1110 | place if a prompt-changing toggle is pressed. */ |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1111 | if (x == -1 || x > xend || resetstatuspos) |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1112 | x = xend; |
| 1113 | |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 1114 | answer = charealloc(answer, xend + 1); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1115 | if (xend > 0) |
| 1116 | strcpy(answer, def); |
| 1117 | else |
| 1118 | answer[0] = '\0'; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1119 | |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 1120 | #if !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE) |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1121 | currshortcut = s; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1122 | #endif |
| 1123 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1124 | /* Get the input! */ |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 1125 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1126 | nanoget_repaint(buf, answer, x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1127 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1128 | /* Make sure any editor screen updates are displayed before getting |
| 1129 | input */ |
Chris Allegretta | 022b96f | 2000-11-14 17:47:58 +0000 | [diff] [blame] | 1130 | wrefresh(edit); |
| 1131 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1132 | while ((kbinput = get_kbinput(bottomwin, &meta)) != NANO_ENTER_KEY) { |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1133 | for (t = s; t != NULL; t = t->next) { |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1134 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 1135 | fprintf(stderr, "Aha! \'%c\' (%d)\n", kbinput, kbinput); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1136 | #endif |
| 1137 | |
David Lawrence Ramsey | d7f5ad9 | 2004-03-04 19:30:53 +0000 | [diff] [blame^] | 1138 | if (kbinput == t->func_key) |
| 1139 | kbinput = t->val; |
| 1140 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1141 | if (kbinput == t->val && is_cntrl_char(kbinput)) { |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 1142 | |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1143 | #ifndef DISABLE_HELP |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1144 | /* Have to do this here, it would be too late to do it |
| 1145 | in statusq() */ |
David Lawrence Ramsey | d7f5ad9 | 2004-03-04 19:30:53 +0000 | [diff] [blame^] | 1146 | if (kbinput == NANO_HELP_KEY) { |
| 1147 | kbinput = NANO_HELP_KEY; |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1148 | do_help(); |
| 1149 | break; |
| 1150 | } |
| 1151 | #endif |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1152 | #ifndef NANO_SMALL |
| 1153 | /* Have to handle these here too, for the time being */ |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1154 | if (kbinput == NANO_PREVLINE_KEY || kbinput == NANO_NEXTLINE_KEY) |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1155 | break; |
| 1156 | #endif |
Chris Allegretta | 5af5889 | 2003-01-17 21:07:38 +0000 | [diff] [blame] | 1157 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1158 | return t->val; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1159 | } |
| 1160 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1161 | assert(0 <= x && x <= xend && xend == strlen(answer)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1162 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1163 | if (kbinput != '\t') |
| 1164 | tabbed = 0; |
| 1165 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1166 | switch (kbinput) { |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 1167 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1168 | case KEY_MOUSE: |
| 1169 | do_mouse(); |
| 1170 | break; |
| 1171 | #endif |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1172 | case NANO_HOME_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1173 | x = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1174 | break; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1175 | case NANO_END_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1176 | x = xend; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1177 | break; |
Chris Allegretta | 35dac58 | 2001-03-21 15:07:20 +0000 | [diff] [blame] | 1178 | case NANO_FORWARD_KEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1179 | if (x < xend) |
| 1180 | x++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1181 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1182 | case NANO_DELETE_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1183 | if (x < xend) { |
David Lawrence Ramsey | 9eff746 | 2003-09-16 02:04:00 +0000 | [diff] [blame] | 1184 | charmove(answer + x, answer + x + 1, xend - x); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1185 | xend--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1186 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1187 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1188 | case NANO_CUT_KEY: |
| 1189 | case NANO_UNCUT_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1190 | null_at(&answer, 0); |
| 1191 | xend = 0; |
| 1192 | x = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1193 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1194 | case NANO_BACKSPACE_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1195 | if (x > 0) { |
David Lawrence Ramsey | 9eff746 | 2003-09-16 02:04:00 +0000 | [diff] [blame] | 1196 | charmove(answer + x - 1, answer + x, xend - x + 1); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1197 | x--; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1198 | xend--; |
| 1199 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 1200 | break; |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1201 | case NANO_TAB_KEY: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1202 | #ifndef NANO_SMALL |
| 1203 | /* tab history completion */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1204 | if (history_list != NULL) { |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 1205 | if (!complete || last_kbinput != NANO_TAB_KEY) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1206 | history_list->current = (historytype *)history_list; |
| 1207 | history_list->len = strlen(answer); |
| 1208 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1209 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1210 | if (history_list->len > 0) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1211 | complete = get_history_completion(history_list, answer); |
| 1212 | xend = strlen(complete); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1213 | x = xend; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1214 | answer = mallocstrcpy(answer, complete); |
| 1215 | } |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 1216 | } |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1217 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 1218 | else |
| 1219 | #endif |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 1220 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1221 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 1222 | if (allowtabs) { |
| 1223 | int shift = 0; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1224 | |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 1225 | answer = input_tab(answer, x, &tabbed, &shift, list); |
| 1226 | xend = strlen(answer); |
| 1227 | x += shift; |
| 1228 | if (x > xend) |
| 1229 | x = xend; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1230 | } |
| 1231 | #endif |
| 1232 | break; |
Chris Allegretta | 35dac58 | 2001-03-21 15:07:20 +0000 | [diff] [blame] | 1233 | case NANO_BACK_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1234 | if (x > 0) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1235 | x--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1236 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1237 | case NANO_PREVLINE_KEY: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1238 | #ifndef NANO_SMALL |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1239 | if (history_list != NULL) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1240 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1241 | /* if currentbuf is NULL, or if use_cb is 1, currentbuf |
| 1242 | isn't NULL, and currentbuf is different from answer, |
| 1243 | it means that we're scrolling up at the top of the |
| 1244 | search history, and we need to save the current |
| 1245 | answer in currentbuf; do this and reset use_cb to |
| 1246 | 0 */ |
| 1247 | if (currentbuf == NULL || (use_cb == 1 && strcmp(currentbuf, answer))) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1248 | currentbuf = mallocstrcpy(currentbuf, answer); |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1249 | use_cb = 0; |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1252 | /* if currentbuf isn't NULL, use_cb is 2, and currentbuf |
| 1253 | is different from answer, it means that we're |
| 1254 | scrolling up at the bottom of the search history, and |
| 1255 | we need to make the string in currentbuf the current |
| 1256 | answer; do this, blow away currentbuf since we don't |
| 1257 | need it anymore, and reset use_cb to 0 */ |
| 1258 | if (currentbuf != NULL && use_cb == 2 && strcmp(currentbuf, answer)) { |
| 1259 | answer = mallocstrcpy(answer, currentbuf); |
| 1260 | free(currentbuf); |
| 1261 | currentbuf = NULL; |
| 1262 | xend = strlen(answer); |
| 1263 | use_cb = 0; |
| 1264 | |
| 1265 | /* else get older search from the history list and save |
| 1266 | it in answer; if there is no older search, blank out |
| 1267 | answer */ |
| 1268 | } else if ((history = get_history_older(history_list)) != NULL) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1269 | answer = mallocstrcpy(answer, history); |
| 1270 | xend = strlen(history); |
| 1271 | } else { |
| 1272 | answer = mallocstrcpy(answer, ""); |
| 1273 | xend = 0; |
| 1274 | } |
| 1275 | x = xend; |
| 1276 | } |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1277 | #endif |
Chris Allegretta | 54abd94 | 2003-01-09 23:43:12 +0000 | [diff] [blame] | 1278 | break; |
David Lawrence Ramsey | c2c5a51 | 2004-01-23 19:26:17 +0000 | [diff] [blame] | 1279 | case NANO_NEXTLINE_KEY: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1280 | #ifndef NANO_SMALL |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1281 | if (history_list != NULL) { |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1282 | |
| 1283 | /* get newer search from the history list and save it |
| 1284 | in answer */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1285 | if ((history = get_history_newer(history_list)) != NULL) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1286 | answer = mallocstrcpy(answer, history); |
| 1287 | xend = strlen(history); |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1288 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1289 | /* if there is no newer search, we're here */ |
| 1290 | |
| 1291 | /* if currentbuf isn't NULL and use_cb isn't 2, it means |
| 1292 | that we're scrolling down at the bottom of the search |
| 1293 | history and we need to make the string in currentbuf |
| 1294 | the current answer; do this, blow away currentbuf |
| 1295 | since we don't need it anymore, and set use_cb to |
| 1296 | 1 */ |
| 1297 | } else if (currentbuf != NULL && use_cb != 2) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 1298 | answer = mallocstrcpy(answer, currentbuf); |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 1299 | free(currentbuf); |
| 1300 | currentbuf = NULL; |
| 1301 | xend = strlen(answer); |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1302 | use_cb = 1; |
| 1303 | |
| 1304 | /* otherwise, if currentbuf is NULL and use_cb isn't 2, |
| 1305 | it means that we're scrolling down at the bottom of |
Chris Allegretta | c30fc24 | 2003-08-11 00:32:45 +0000 | [diff] [blame] | 1306 | the search history and the current answer (if it's |
| 1307 | not blank) needs to be saved in currentbuf; do this, |
| 1308 | blank out answer (if necessary), and set use_cb to |
| 1309 | 2 */ |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1310 | } else if (use_cb != 2) { |
Chris Allegretta | c30fc24 | 2003-08-11 00:32:45 +0000 | [diff] [blame] | 1311 | if (answer[0] != '\0') { |
| 1312 | currentbuf = mallocstrcpy(currentbuf, answer); |
| 1313 | answer = mallocstrcpy(answer, ""); |
| 1314 | } |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1315 | xend = 0; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1316 | use_cb = 2; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1317 | } |
| 1318 | x = xend; |
| 1319 | } |
| 1320 | #endif |
| 1321 | break; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1322 | default: |
| 1323 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1324 | for (t = s; t != NULL; t = t->next) { |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1325 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 1326 | fprintf(stderr, "Aha! \'%c\' (%d)\n", kbinput, |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1327 | kbinput); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1328 | #endif |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1329 | if (meta == 1 && (kbinput == t->metaval || kbinput == t->misc)) |
| 1330 | /* We hit a Meta key. Do like above. We don't |
| 1331 | * just ungetch() the letter and let it get |
| 1332 | * caught above cause that screws the |
| 1333 | * keypad... */ |
| 1334 | return kbinput; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1335 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1336 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1337 | if (is_cntrl_char(kbinput)) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1338 | break; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1339 | answer = charealloc(answer, xend + 2); |
David Lawrence Ramsey | 9eff746 | 2003-09-16 02:04:00 +0000 | [diff] [blame] | 1340 | charmove(answer + x + 1, answer + x, xend - x + 1); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1341 | xend++; |
| 1342 | answer[x] = kbinput; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1343 | x++; |
| 1344 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1345 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 1346 | fprintf(stderr, "input \'%c\' (%d)\n", kbinput, kbinput); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1347 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1348 | } /* switch (kbinput) */ |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 1349 | #ifndef NANO_SMALL |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1350 | last_kbinput = kbinput; |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 1351 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1352 | nanoget_repaint(buf, answer, x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1353 | wrefresh(bottomwin); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1354 | } /* while (kbinput ...) */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1355 | |
Chris Allegretta | 5af5889 | 2003-01-17 21:07:38 +0000 | [diff] [blame] | 1356 | /* We finished putting in an answer; reset x */ |
| 1357 | x = -1; |
| 1358 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1359 | /* Just check for a blank answer here */ |
Chris Allegretta | 15c28f8 | 2003-01-05 21:47:06 +0000 | [diff] [blame] | 1360 | if (answer[0] == '\0') |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1361 | return -2; |
| 1362 | else |
| 1363 | return 0; |
| 1364 | } |
| 1365 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1366 | /* If modified is not already set, set it and update titlebar. */ |
| 1367 | void set_modified(void) |
| 1368 | { |
| 1369 | if (!ISSET(MODIFIED)) { |
| 1370 | SET(MODIFIED); |
| 1371 | titlebar(NULL); |
| 1372 | wrefresh(topwin); |
| 1373 | } |
| 1374 | } |
| 1375 | |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1376 | void titlebar(const char *path) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1377 | { |
| 1378 | int namelen, space; |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1379 | const char *what = path; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1380 | |
| 1381 | if (path == NULL) |
| 1382 | what = filename; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1383 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1384 | wattron(topwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1385 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1386 | mvwaddstr(topwin, 0, 0, hblank); |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1387 | mvwaddnstr(topwin, 0, 2, VERMSG, COLS - 3); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1388 | |
David Lawrence Ramsey | ec290f2 | 2003-08-30 19:05:40 +0000 | [diff] [blame] | 1389 | space = COLS - sizeof(VERMSG) - 23; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1390 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 1391 | namelen = strlen(what); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1392 | |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1393 | if (space > 0) { |
| 1394 | if (what[0] == '\0') |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1395 | mvwaddnstr(topwin, 0, COLS / 2 - 6, _("New Buffer"), |
| 1396 | COLS / 2 + COLS % 2 - 6); |
| 1397 | else if (namelen > space) { |
| 1398 | if (path == NULL) |
| 1399 | waddstr(topwin, _(" File: ...")); |
| 1400 | else |
| 1401 | waddstr(topwin, _(" DIR: ...")); |
| 1402 | waddstr(topwin, &what[namelen - space]); |
| 1403 | } else { |
| 1404 | if (path == NULL) |
| 1405 | mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1), |
| 1406 | _("File: ")); |
| 1407 | else |
| 1408 | mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1), |
| 1409 | _(" DIR: ")); |
| 1410 | waddstr(topwin, what); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1411 | } |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1412 | } /* If we don't have space, we shouldn't bother */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1413 | if (ISSET(MODIFIED)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1414 | mvwaddnstr(topwin, 0, COLS - 11, _(" Modified "), 11); |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 1415 | else if (ISSET(VIEW_MODE)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1416 | mvwaddnstr(topwin, 0, COLS - 11, _(" View "), 11); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1417 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1418 | wattroff(topwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1419 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1420 | wrefresh(topwin); |
| 1421 | reset_cursor(); |
| 1422 | } |
| 1423 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1424 | void bottombars(const shortcut *s) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1425 | { |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 1426 | int i, j, numcols; |
Chris Allegretta | 3bbc416 | 2003-01-23 00:46:12 +0000 | [diff] [blame] | 1427 | char keystr[9]; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1428 | int slen; |
| 1429 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1430 | if (ISSET(NO_HELP)) |
| 1431 | return; |
| 1432 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1433 | if (s == main_list) { |
| 1434 | slen = MAIN_VISIBLE; |
| 1435 | assert(MAIN_VISIBLE <= length_of_list(s)); |
| 1436 | } else |
| 1437 | slen = length_of_list(s); |
| 1438 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1439 | /* There will be this many columns of shortcuts */ |
| 1440 | numcols = (slen + (slen % 2)) / 2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1441 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1442 | blank_bottomwin(); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1443 | |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 1444 | for (i = 0; i < numcols; i++) { |
| 1445 | for (j = 0; j <= 1; j++) { |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1446 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1447 | wmove(bottomwin, 1 + j, i * (COLS / numcols)); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1448 | |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1449 | /* Yucky sentinel values we can't handle a better way */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1450 | if (s->val != NANO_NO_KEY) { |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 1451 | #ifndef NANO_SMALL |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1452 | if (s->val == NANO_HISTORY_KEY) |
| 1453 | strncpy(keystr, _("Up"), 8); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1454 | else |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 1455 | #endif |
| 1456 | if (s->val == NANO_CONTROL_SPACE) |
| 1457 | strcpy(keystr, "^ "); |
| 1458 | else if (s->val == NANO_CONTROL_8) |
| 1459 | strcpy(keystr, "^?"); |
| 1460 | else |
| 1461 | sprintf(keystr, "^%c", s->val + 64); |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 1462 | } else if (s->metaval != NANO_NO_KEY) |
| 1463 | sprintf(keystr, "M-%c", toupper(s->metaval)); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 1464 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1465 | onekey(keystr, s->desc, COLS / numcols); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1466 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1467 | s = s->next; |
| 1468 | if (s == NULL) |
| 1469 | goto break_completely_out; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1470 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1471 | } |
| 1472 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1473 | break_completely_out: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1474 | wrefresh(bottomwin); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1477 | /* Write a shortcut key to the help area at the bottom of the window. |
| 1478 | * keystroke is e.g. "^G" and desc is e.g. "Get Help". |
| 1479 | * We are careful to write exactly len characters, even if len is |
| 1480 | * very small and keystroke and desc are long. */ |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1481 | void onekey(const char *keystroke, const char *desc, int len) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1482 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1483 | wattron(bottomwin, A_REVERSE); |
| 1484 | waddnstr(bottomwin, keystroke, len); |
| 1485 | wattroff(bottomwin, A_REVERSE); |
| 1486 | len -= strlen(keystroke); |
| 1487 | if (len > 0) { |
| 1488 | waddch(bottomwin, ' '); |
| 1489 | len--; |
| 1490 | waddnstr(bottomwin, desc, len); |
| 1491 | len -= strlen(desc); |
| 1492 | for (; len > 0; len--) |
| 1493 | waddch(bottomwin, ' '); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1494 | } |
| 1495 | } |
| 1496 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1497 | /* And so start the display update routines. */ |
| 1498 | |
| 1499 | #ifndef NDEBUG |
| 1500 | int check_linenumbers(const filestruct *fileptr) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1501 | { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1502 | int check_line = 0; |
| 1503 | const filestruct *filetmp; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 1504 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1505 | for (filetmp = edittop; filetmp != fileptr; filetmp = filetmp->next) |
| 1506 | check_line++; |
| 1507 | return check_line; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 1508 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1509 | #endif |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 1510 | |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 1511 | /* nano scrolls horizontally within a line in chunks. This function |
| 1512 | * returns the column number of the first character displayed in the |
| 1513 | * window when the cursor is at the given column. Note that |
| 1514 | * 0 <= column - get_page_start(column) < COLS. */ |
| 1515 | size_t get_page_start(size_t column) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1516 | { |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 1517 | assert(COLS > 0); |
| 1518 | if (column == 0 || column < COLS - 1) |
| 1519 | return 0; |
| 1520 | else if (COLS > 9) |
David Lawrence Ramsey | 66081d4 | 2004-01-22 07:25:31 +0000 | [diff] [blame] | 1521 | return column - 7 - (column - 7) % (COLS - 8); |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 1522 | else if (COLS > 2) |
| 1523 | return column - (COLS - 2); |
| 1524 | else |
| 1525 | return column - (COLS - 1); |
| 1526 | /* The parentheses are necessary to avoid overflow. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1529 | /* Resets current_y, based on the position of current, and puts the |
| 1530 | * cursor at (current_y, current_x). */ |
| 1531 | void reset_cursor(void) |
| 1532 | { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1533 | /* Yuck. This condition can be true after open_file() when opening |
| 1534 | * the first file. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1535 | if (edittop == NULL) |
| 1536 | return; |
| 1537 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1538 | current_y = current->lineno - edittop->lineno; |
| 1539 | if (current_y < editwinrows) { |
| 1540 | size_t x = xplustabs(); |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1541 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1542 | wmove(edit, current_y, x - get_page_start(x)); |
| 1543 | } |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1544 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1545 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1546 | /* edit_add() takes care of the job of actually painting a line into the |
| 1547 | * edit window. fileptr is the line to be painted, at row yval of the |
| 1548 | * window. converted is the actual string to be written to the window, |
| 1549 | * with tabs and control characters replaced by strings of regular |
| 1550 | * characters. start is the column number of the first character |
| 1551 | * of this page. That is, the first character of converted corresponds to |
| 1552 | * character number actual_x(fileptr->data, start) of the line. */ |
| 1553 | void edit_add(const filestruct *fileptr, const char *converted, |
| 1554 | int yval, size_t start) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1555 | { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1556 | #if defined(ENABLE_COLOR) || !defined(NANO_SMALL) |
| 1557 | size_t startpos = actual_x(fileptr->data, start); |
| 1558 | /* The position in fileptr->data of the leftmost character |
| 1559 | * that displays at least partially on the window. */ |
| 1560 | size_t endpos = actual_x(fileptr->data, start + COLS - 1) + 1; |
| 1561 | /* The position in fileptr->data of the first character that is |
| 1562 | * completely off the window to the right. |
| 1563 | * |
| 1564 | * Note that endpos might be beyond the null terminator of the |
| 1565 | * string. */ |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 1566 | #endif |
| 1567 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1568 | assert(fileptr != NULL && converted != NULL); |
| 1569 | assert(strlen(converted) <= COLS); |
| 1570 | |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 1571 | /* 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] | 1572 | * just the text that needs it). */ |
| 1573 | mvwaddstr(edit, yval, 0, converted); |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 1574 | |
Chris Allegretta | 7dd7768 | 2001-12-08 19:52:28 +0000 | [diff] [blame] | 1575 | #ifdef ENABLE_COLOR |
Chris Allegretta | 1dd0bc9 | 2002-10-13 18:43:45 +0000 | [diff] [blame] | 1576 | if (colorstrings != NULL && ISSET(COLOR_SYNTAX)) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1577 | const colortype *tmpcolor = colorstrings; |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 1578 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1579 | for (; tmpcolor != NULL; tmpcolor = tmpcolor->next) { |
| 1580 | int x_start; |
| 1581 | /* Starting column for mvwaddnstr. Zero-based. */ |
| 1582 | int paintlen; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1583 | /* Number of chars to paint on this line. There are COLS |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1584 | * characters on a whole line. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1585 | regmatch_t startmatch; /* match position for start_regexp */ |
| 1586 | regmatch_t endmatch; /* match position for end_regexp */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1587 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1588 | if (tmpcolor->bright) |
| 1589 | wattron(edit, A_BOLD); |
| 1590 | wattron(edit, COLOR_PAIR(tmpcolor->pairnum)); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1591 | /* Two notes about regexec(). Return value 0 means there is |
| 1592 | * a match. Also, rm_eo is the first non-matching character |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1593 | * after the match. */ |
| 1594 | |
| 1595 | /* First case, tmpcolor is a single-line expression. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1596 | if (tmpcolor->end == NULL) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1597 | size_t k = 0; |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 1598 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1599 | /* 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] | 1600 | * last match. Even though two matches may overlap, we |
| 1601 | * want to ignore them, so that we can highlight |
| 1602 | * C-strings correctly. */ |
| 1603 | while (k < endpos) { |
| 1604 | /* Note the fifth parameter to regexec(). It says |
| 1605 | * not to match the beginning-of-line character |
| 1606 | * unless k is 0. If regexec() returns REG_NOMATCH, |
| 1607 | * there are no more matches in the line. */ |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 1608 | if (regexec(&tmpcolor->start, &fileptr->data[k], 1, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1609 | &startmatch, k == 0 ? 0 : REG_NOTBOL) == REG_NOMATCH) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1610 | break; |
| 1611 | /* Translate the match to the beginning of the line. */ |
| 1612 | startmatch.rm_so += k; |
| 1613 | startmatch.rm_eo += k; |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1614 | if (startmatch.rm_so == startmatch.rm_eo) { |
| 1615 | startmatch.rm_eo++; |
Chris Allegretta | 7c27be4 | 2002-05-05 23:03:54 +0000 | [diff] [blame] | 1616 | statusbar(_("Refusing 0 length regex match")); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1617 | } else if (startmatch.rm_so < endpos && |
| 1618 | startmatch.rm_eo > startpos) { |
| 1619 | if (startmatch.rm_so <= startpos) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1620 | x_start = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1621 | else |
| 1622 | x_start = strnlenpt(fileptr->data, startmatch.rm_so) |
| 1623 | - start; |
| 1624 | paintlen = strnlenpt(fileptr->data, startmatch.rm_eo) |
| 1625 | - start - x_start; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1626 | if (paintlen > COLS - x_start) |
| 1627 | paintlen = COLS - x_start; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1628 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1629 | assert(0 <= x_start && 0 < paintlen && |
| 1630 | x_start + paintlen <= COLS); |
| 1631 | mvwaddnstr(edit, yval, x_start, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1632 | converted + x_start, paintlen); |
| 1633 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1634 | k = startmatch.rm_eo; |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1635 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1636 | } else { |
| 1637 | /* This is a multi-line regexp. There are two steps. |
| 1638 | * First, we have to see if the beginning of the line is |
| 1639 | * colored by a start on an earlier line, and an end on |
| 1640 | * this line or later. |
| 1641 | * |
| 1642 | * We find the first line before fileptr matching the |
| 1643 | * start. If every match on that line is followed by an |
| 1644 | * end, then go to step two. Otherwise, find the next line |
| 1645 | * after start_line matching the end. If that line is not |
| 1646 | * before fileptr, then paint the beginning of this line. */ |
Chris Allegretta | 3674c1d | 2002-05-12 20:43:49 +0000 | [diff] [blame] | 1647 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1648 | const filestruct *start_line = fileptr->prev; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1649 | /* the first line before fileptr matching start */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1650 | regoff_t start_col; |
| 1651 | /* where it starts in that line */ |
| 1652 | const filestruct *end_line; |
| 1653 | int searched_later_lines = 0; |
| 1654 | /* Used in step 2. Have we looked for an end on |
| 1655 | * lines after fileptr? */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1656 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1657 | while (start_line != NULL && |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 1658 | regexec(&tmpcolor->start, start_line->data, 1, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1659 | &startmatch, 0) == REG_NOMATCH) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1660 | /* If there is an end on this line, there is no need |
| 1661 | * to look for starts on earlier lines. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1662 | if (regexec(tmpcolor->end, start_line->data, 0, NULL, 0) |
| 1663 | == 0) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1664 | goto step_two; |
| 1665 | start_line = start_line->prev; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1666 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1667 | /* No start found, so skip to the next step. */ |
| 1668 | if (start_line == NULL) |
| 1669 | goto step_two; |
| 1670 | /* Now start_line is the first line before fileptr |
| 1671 | * containing a start match. Is there a start on this |
| 1672 | * line not followed by an end on this line? */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1673 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1674 | start_col = 0; |
| 1675 | while (1) { |
| 1676 | start_col += startmatch.rm_so; |
| 1677 | startmatch.rm_eo -= startmatch.rm_so; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1678 | if (regexec(tmpcolor->end, |
| 1679 | start_line->data + start_col + startmatch.rm_eo, |
| 1680 | 0, NULL, start_col + startmatch.rm_eo == 0 ? 0 : |
| 1681 | REG_NOTBOL) == REG_NOMATCH) |
| 1682 | /* No end found after this start. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1683 | break; |
| 1684 | start_col++; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 1685 | if (regexec(&tmpcolor->start, |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1686 | start_line->data + start_col, 1, &startmatch, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1687 | REG_NOTBOL) == REG_NOMATCH) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1688 | /* No later start on this line. */ |
| 1689 | goto step_two; |
| 1690 | } |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1691 | /* Indeed, there is a start not followed on this line by |
| 1692 | * an end. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1693 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1694 | /* We have already checked that there is no end before |
| 1695 | * fileptr and after the start. Is there an end after |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1696 | * the start at all? We don't paint unterminated |
| 1697 | * starts. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1698 | end_line = fileptr; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 1699 | while (end_line != NULL && |
| 1700 | regexec(tmpcolor->end, end_line->data, 1, &endmatch, 0)) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1701 | end_line = end_line->next; |
| 1702 | |
| 1703 | /* No end found, or it is too early. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1704 | if (end_line == NULL || |
| 1705 | (end_line == fileptr && endmatch.rm_eo <= startpos)) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1706 | goto step_two; |
| 1707 | |
| 1708 | /* Now paint the start of fileptr. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1709 | paintlen = end_line != fileptr ? COLS : |
| 1710 | strnlenpt(fileptr->data, endmatch.rm_eo) - start; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1711 | if (paintlen > COLS) |
| 1712 | paintlen = COLS; |
| 1713 | |
| 1714 | assert(0 < paintlen && paintlen <= COLS); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1715 | mvwaddnstr(edit, yval, 0, converted, paintlen); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1716 | |
| 1717 | /* We have already painted the whole line. */ |
| 1718 | if (paintlen == COLS) |
| 1719 | goto skip_step_two; |
| 1720 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1721 | step_two: /* Second step, we look for starts on this line. */ |
| 1722 | start_col = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1723 | while (start_col < endpos) { |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 1724 | if (regexec(&tmpcolor->start, fileptr->data + start_col, 1, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1725 | &startmatch, start_col == 0 ? 0 : REG_NOTBOL) |
| 1726 | == REG_NOMATCH || start_col + startmatch.rm_so >= |
| 1727 | endpos) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1728 | /* No more starts on this line. */ |
| 1729 | break; |
| 1730 | /* Translate the match to be relative to the |
| 1731 | * beginning of the line. */ |
| 1732 | startmatch.rm_so += start_col; |
| 1733 | startmatch.rm_eo += start_col; |
| 1734 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1735 | if (startmatch.rm_so <= startpos) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1736 | x_start = 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1737 | else |
| 1738 | x_start = strnlenpt(fileptr->data, startmatch.rm_so) |
| 1739 | - start; |
| 1740 | if (regexec(tmpcolor->end, fileptr->data + startmatch.rm_eo, |
| 1741 | 1, &endmatch, startmatch.rm_eo == 0 ? 0 : |
| 1742 | REG_NOTBOL) == 0) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1743 | /* Translate the end match to be relative to the |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1744 | * beginning of the line. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1745 | endmatch.rm_so += startmatch.rm_eo; |
| 1746 | endmatch.rm_eo += startmatch.rm_eo; |
| 1747 | /* There is an end on this line. But does it |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1748 | * appear on this page, and is the match more than |
| 1749 | * zero characters long? */ |
| 1750 | if (endmatch.rm_eo > startpos && |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1751 | endmatch.rm_eo > startmatch.rm_so) { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1752 | paintlen = strnlenpt(fileptr->data, endmatch.rm_eo) |
| 1753 | - start - x_start; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1754 | if (x_start + paintlen > COLS) |
| 1755 | paintlen = COLS - x_start; |
| 1756 | |
| 1757 | assert(0 <= x_start && 0 < paintlen && |
| 1758 | x_start + paintlen <= COLS); |
| 1759 | mvwaddnstr(edit, yval, x_start, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1760 | converted + x_start, paintlen); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1761 | } |
| 1762 | } else if (!searched_later_lines) { |
| 1763 | searched_later_lines = 1; |
| 1764 | /* There is no end on this line. But we haven't |
| 1765 | * yet looked for one on later lines. */ |
| 1766 | end_line = fileptr->next; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1767 | while (end_line != NULL && |
| 1768 | regexec(tmpcolor->end, end_line->data, 0, |
| 1769 | NULL, 0) == REG_NOMATCH) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1770 | end_line = end_line->next; |
| 1771 | if (end_line != NULL) { |
| 1772 | assert(0 <= x_start && x_start < COLS); |
| 1773 | mvwaddnstr(edit, yval, x_start, |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1774 | converted + x_start, |
| 1775 | COLS - x_start); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1776 | /* We painted to the end of the line, so |
| 1777 | * don't bother checking any more starts. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1778 | break; |
Chris Allegretta | 3674c1d | 2002-05-12 20:43:49 +0000 | [diff] [blame] | 1779 | } |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1780 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1781 | start_col = startmatch.rm_so + 1; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1782 | } /* while start_col < endpos */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1783 | } /* if (tmp_color->end != NULL) */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 1784 | |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 1785 | skip_step_two: |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1786 | wattroff(edit, A_BOLD); |
| 1787 | wattroff(edit, COLOR_PAIR(tmpcolor->pairnum)); |
| 1788 | } /* for tmpcolor in colorstrings */ |
| 1789 | } |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1790 | #endif /* ENABLE_COLOR */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1791 | |
Chris Allegretta | 7dd7768 | 2001-12-08 19:52:28 +0000 | [diff] [blame] | 1792 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1793 | if (ISSET(MARK_ISSET) |
| 1794 | && (fileptr->lineno <= mark_beginbuf->lineno |
| 1795 | || fileptr->lineno <= current->lineno) |
| 1796 | && (fileptr->lineno >= mark_beginbuf->lineno |
| 1797 | || fileptr->lineno >= current->lineno)) { |
| 1798 | /* fileptr is at least partially selected. */ |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 1799 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1800 | const filestruct *top; |
| 1801 | /* Either current or mark_beginbuf, whichever is first. */ |
| 1802 | size_t top_x; |
| 1803 | /* current_x or mark_beginx, corresponding to top. */ |
| 1804 | const filestruct *bot; |
| 1805 | size_t bot_x; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1806 | int x_start; |
| 1807 | /* Starting column for mvwaddnstr. Zero-based. */ |
| 1808 | int paintlen; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1809 | /* Number of chars to paint on this line. There are COLS |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1810 | * characters on a whole line. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1811 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1812 | mark_order(&top, &top_x, &bot, &bot_x); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1813 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1814 | if (top->lineno < fileptr->lineno || top_x < startpos) |
| 1815 | top_x = startpos; |
| 1816 | if (bot->lineno > fileptr->lineno || bot_x > endpos) |
| 1817 | bot_x = endpos; |
| 1818 | |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 1819 | /* The selected bit of fileptr is on this page. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1820 | if (top_x < endpos && bot_x > startpos) { |
| 1821 | assert(startpos <= top_x); |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 1822 | |
| 1823 | /* x_start is the expanded location of the beginning of the |
| 1824 | * mark minus the beginning of the page. */ |
| 1825 | x_start = strnlenpt(fileptr->data, top_x) - start; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1826 | |
| 1827 | if (bot_x >= endpos) |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 1828 | /* If the end of the mark is off the page, paintlen is |
| 1829 | * -1, meaning that everything on the line gets |
| 1830 | * painted. */ |
| 1831 | paintlen = -1; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1832 | else |
David Lawrence Ramsey | 4dcd070 | 2003-10-03 04:20:28 +0000 | [diff] [blame] | 1833 | /* Otherwise, paintlen is the expanded location of the |
| 1834 | * end of the mark minus the expanded location of the |
| 1835 | * beginning of the mark. */ |
| 1836 | paintlen = strnlenpt(fileptr->data, bot_x) - (x_start + |
| 1837 | start); |
| 1838 | |
| 1839 | /* If x_start is before the beginning of the page, shift |
| 1840 | * paintlen x_start characters to compensate, and put |
| 1841 | * x_start at the beginning of the page. */ |
| 1842 | if (x_start < 0) { |
| 1843 | paintlen += x_start; |
| 1844 | x_start = 0; |
| 1845 | } |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1846 | |
| 1847 | assert(x_start >= 0 && x_start <= strlen(converted)); |
| 1848 | |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1849 | wattron(edit, A_REVERSE); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1850 | mvwaddnstr(edit, yval, x_start, converted + x_start, paintlen); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1851 | wattroff(edit, A_REVERSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1852 | } |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 1853 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1854 | #endif /* !NANO_SMALL */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1855 | } |
| 1856 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1857 | /* 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] | 1858 | * edit_add(). |
| 1859 | * |
| 1860 | * If fileptr != current, then index is considered 0. |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1861 | * The line will be displayed starting with fileptr->data[index]. |
| 1862 | * Likely args are current_x or 0. */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1863 | void update_line(const filestruct *fileptr, size_t index) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1864 | { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1865 | int line; |
| 1866 | /* line in the edit window for CURSES calls */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1867 | char *converted; |
| 1868 | /* fileptr->data converted to have tabs and control characters |
| 1869 | * expanded. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1870 | size_t page_start; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1871 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1872 | assert(fileptr != NULL); |
Robert Siemborski | 53154a7 | 2000-06-18 00:11:03 +0000 | [diff] [blame] | 1873 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1874 | line = fileptr->lineno - edittop->lineno; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1875 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1876 | /* We assume the line numbers are valid. Is that really true? */ |
| 1877 | assert(line < 0 || line == check_linenumbers(fileptr)); |
| 1878 | |
| 1879 | if (line < 0 || line >= editwinrows) |
| 1880 | return; |
| 1881 | |
| 1882 | /* First, blank out the line (at a minimum) */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1883 | mvwaddstr(edit, line, 0, hblank); |
| 1884 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1885 | /* Next, convert variables that index the line to their equivalent |
| 1886 | * positions in the expanded line. */ |
David Lawrence Ramsey | 2dd7ed1 | 2003-09-29 05:15:24 +0000 | [diff] [blame] | 1887 | index = (fileptr == current) ? strnlenpt(fileptr->data, index) : 0; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1888 | page_start = get_page_start(index); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1889 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1890 | /* Expand the line, replacing Tab by spaces, and control characters |
| 1891 | * by their display form. */ |
| 1892 | converted = display_string(fileptr->data, page_start, COLS); |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 1893 | |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 1894 | /* Now, paint the line */ |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1895 | edit_add(fileptr, converted, line, page_start); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1896 | free(converted); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1897 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1898 | if (page_start > 0) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1899 | mvwaddch(edit, line, 0, '$'); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1900 | if (strlenpt(fileptr->data) > page_start + COLS) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1901 | mvwaddch(edit, line, COLS - 1, '$'); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1904 | /* This function updates current, based on where current_y is; |
| 1905 | * reset_cursor() does the opposite. */ |
| 1906 | void update_cursor(void) |
| 1907 | { |
| 1908 | int i = 0; |
| 1909 | |
| 1910 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 1911 | fprintf(stderr, "Moved to (%d, %d) in edit buffer\n", current_y, |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1912 | current_x); |
| 1913 | #endif |
| 1914 | |
| 1915 | current = edittop; |
| 1916 | while (i < current_y && current->next != NULL) { |
| 1917 | current = current->next; |
| 1918 | i++; |
| 1919 | } |
| 1920 | |
| 1921 | #ifdef DEBUG |
Chris Allegretta | 0e86e60 | 2003-01-23 04:27:23 +0000 | [diff] [blame] | 1922 | fprintf(stderr, "current->data = \"%s\"\n", current->data); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1923 | #endif |
| 1924 | } |
| 1925 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1926 | void center_cursor(void) |
| 1927 | { |
| 1928 | current_y = editwinrows / 2; |
| 1929 | wmove(edit, current_y, current_x); |
| 1930 | } |
| 1931 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1932 | /* Refresh the screen without changing the position of lines. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1933 | void edit_refresh(void) |
| 1934 | { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1935 | /* Neither of these conditions should occur, but they do. edittop |
| 1936 | * 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] | 1937 | * ENABLE_COLOR is defined. Yuck. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1938 | if (current == NULL) |
| 1939 | return; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1940 | if (edittop == NULL) |
| 1941 | edittop = current; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1942 | |
Chris Allegretta | 63d0b48 | 2003-01-26 19:47:10 +0000 | [diff] [blame] | 1943 | if (current->lineno < edittop->lineno || |
| 1944 | current->lineno >= edittop->lineno + editwinrows) |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1945 | /* Note that edit_update() changes edittop so that |
| 1946 | * current->lineno = edittop->lineno + editwinrows / 2. Thus |
| 1947 | * when it then calls edit_refresh(), there is no danger of |
| 1948 | * getting an infinite loop. */ |
Chris Allegretta | da721be | 2000-07-31 01:26:42 +0000 | [diff] [blame] | 1949 | edit_update(current, CENTER); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1950 | else { |
| 1951 | int nlines = 0; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1952 | const filestruct *foo = edittop; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1953 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1954 | #ifdef DEBUG |
| 1955 | fprintf(stderr, "edit_refresh(): edittop->lineno = %ld\n", edittop->lineno); |
| 1956 | #endif |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1957 | |
David Lawrence Ramsey | b55e382 | 2004-02-25 06:25:12 +0000 | [diff] [blame] | 1958 | /* Don't let the cursor jump around the screen while |
| 1959 | * updating. */ |
| 1960 | leaveok(edit, TRUE); |
| 1961 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1962 | while (nlines < editwinrows) { |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1963 | update_line(foo, current_x); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1964 | nlines++; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1965 | if (foo->next == NULL) |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1966 | break; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1967 | foo = foo->next; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1968 | } |
| 1969 | while (nlines < editwinrows) { |
| 1970 | mvwaddstr(edit, nlines, 0, hblank); |
| 1971 | nlines++; |
| 1972 | } |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1973 | reset_cursor(); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1974 | /* 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] | 1975 | * isn't here? Luck? */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1976 | wrefresh(edit); |
David Lawrence Ramsey | b55e382 | 2004-02-25 06:25:12 +0000 | [diff] [blame] | 1977 | |
| 1978 | /* Let the cursor jump around the screen again. */ |
| 1979 | leaveok(edit, FALSE); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1980 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1981 | } |
| 1982 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1983 | /* Same as above, but touch the window first, so everything is |
| 1984 | * redrawn. */ |
Chris Allegretta | f1d33d3 | 2000-08-19 03:53:39 +0000 | [diff] [blame] | 1985 | void edit_refresh_clearok(void) |
| 1986 | { |
| 1987 | clearok(edit, TRUE); |
| 1988 | edit_refresh(); |
| 1989 | clearok(edit, FALSE); |
| 1990 | } |
| 1991 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 1992 | /* Nice generic routine to update the edit buffer, given a pointer to the |
| 1993 | * file struct =) */ |
David Lawrence Ramsey | 1356a0a | 2003-09-10 20:31:02 +0000 | [diff] [blame] | 1994 | void edit_update(filestruct *fileptr, topmidnone location) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1995 | { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1996 | if (fileptr == NULL) |
| 1997 | return; |
| 1998 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1999 | if (location != TOP) { |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 2000 | int goal = location == NONE ? current_y : editwinrows / 2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2001 | |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 2002 | for (; goal > 0 && fileptr->prev != NULL; goal--) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2003 | fileptr = fileptr->prev; |
| 2004 | } |
| 2005 | edittop = fileptr; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2006 | edit_refresh(); |
| 2007 | } |
| 2008 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2009 | /* Ask a question on the statusbar. Answer will be stored in answer |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2010 | * 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] | 2011 | * otherwise, the valid shortcut key caught. Def is any editable text we |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2012 | * want to put up by default. |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 2013 | * |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2014 | * New arg tabs tells whether or not to allow tab completion. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2015 | int statusq(int tabs, const shortcut *s, const char *def, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 2016 | #ifndef NANO_SMALL |
| 2017 | historyheadtype *which_history, |
| 2018 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2019 | const char *msg, ...) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2020 | { |
| 2021 | va_list ap; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2022 | char *foo = charalloc(COLS - 3); |
Chris Allegretta | 9caa193 | 2002-02-15 20:08:05 +0000 | [diff] [blame] | 2023 | int ret; |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2024 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | a16e4e9 | 2002-01-05 18:59:54 +0000 | [diff] [blame] | 2025 | int list = 0; |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2026 | #endif |
| 2027 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2028 | bottombars(s); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2029 | |
| 2030 | va_start(ap, msg); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2031 | vsnprintf(foo, COLS - 4, msg, ap); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2032 | va_end(ap); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2033 | foo[COLS - 4] = '\0'; |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2034 | |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 2035 | ret = nanogetstr(tabs, foo, def, |
| 2036 | #ifndef NANO_SMALL |
| 2037 | which_history, |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2038 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 2039 | s |
| 2040 | #ifndef DISABLE_TABCOMP |
| 2041 | , &list |
| 2042 | #endif |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2043 | ); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2044 | free(foo); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2045 | resetstatuspos = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2046 | |
| 2047 | switch (ret) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2048 | case NANO_FIRSTLINE_KEY: |
David Lawrence Ramsey | d7f5ad9 | 2004-03-04 19:30:53 +0000 | [diff] [blame^] | 2049 | case NANO_FIRSTLINE_FKEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2050 | do_first_line(); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2051 | resetstatuspos = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2052 | break; |
| 2053 | case NANO_LASTLINE_KEY: |
David Lawrence Ramsey | d7f5ad9 | 2004-03-04 19:30:53 +0000 | [diff] [blame^] | 2054 | case NANO_LASTLINE_FKEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2055 | do_last_line(); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2056 | resetstatuspos = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2057 | break; |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2058 | #ifndef DISABLE_JUSTIFY |
| 2059 | case NANO_PARABEGIN_KEY: |
David Lawrence Ramsey | 795a16b | 2004-02-07 05:03:25 +0000 | [diff] [blame] | 2060 | case NANO_PARABEGIN_ALTKEY: |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2061 | do_para_begin(); |
| 2062 | resetstatuspos = 1; |
| 2063 | break; |
| 2064 | case NANO_PARAEND_KEY: |
David Lawrence Ramsey | 795a16b | 2004-02-07 05:03:25 +0000 | [diff] [blame] | 2065 | case NANO_PARAEND_ALTKEY: |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2066 | do_para_end(); |
| 2067 | resetstatuspos = 1; |
| 2068 | break; |
| 2069 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2070 | case NANO_CANCEL_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2071 | ret = -1; |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 2072 | resetstatuspos = 1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2073 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2074 | } |
Chris Allegretta | a90d0cf | 2003-02-10 02:55:03 +0000 | [diff] [blame] | 2075 | blank_statusbar(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2076 | |
| 2077 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 2078 | fprintf(stderr, "I got \"%s\"\n", answer); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2079 | #endif |
| 2080 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2081 | #ifndef DISABLE_TABCOMP |
| 2082 | /* if we've done tab completion, there might be a list of |
| 2083 | filename matches on the edit window at this point; make sure |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2084 | they're cleared off. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2085 | if (list) |
| 2086 | edit_refresh(); |
| 2087 | #endif |
| 2088 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2089 | return ret; |
| 2090 | } |
| 2091 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2092 | /* 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] | 2093 | * 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] | 2094 | * (^C). */ |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2095 | int do_yesno(int all, const char *msg) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2096 | { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2097 | int ok = -2, width = 16; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2098 | const char *yesstr; /* String of yes characters accepted */ |
| 2099 | const char *nostr; /* Same for no */ |
| 2100 | const char *allstr; /* And all, surprise! */ |
Chris Allegretta | 235ab19 | 2001-04-12 13:24:40 +0000 | [diff] [blame] | 2101 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2102 | /* Yes, no and all are strings of any length. Each string consists |
| 2103 | * of all characters accepted as a valid character for that value. |
| 2104 | * The first value will be the one displayed in the shortcuts. */ |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2105 | yesstr = _("Yy"); |
| 2106 | nostr = _("Nn"); |
| 2107 | allstr = _("Aa"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2108 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2109 | /* Remove gettext call for keybindings until we clear the thing |
| 2110 | * up. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2111 | if (!ISSET(NO_HELP)) { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2112 | char shortstr[3]; /* Temp string for Y, N, A. */ |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2113 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2114 | if (COLS < 32) |
| 2115 | width = COLS / 2; |
| 2116 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2117 | /* Write the bottom of the screen. */ |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2118 | blank_bottombars(); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2119 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2120 | sprintf(shortstr, " %c", yesstr[0]); |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2121 | wmove(bottomwin, 1, 0); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2122 | onekey(shortstr, _("Yes"), width); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2123 | |
| 2124 | if (all) { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2125 | wmove(bottomwin, 1, width); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2126 | shortstr[1] = allstr[0]; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2127 | onekey(shortstr, _("All"), width); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2128 | } |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2129 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2130 | wmove(bottomwin, 2, 0); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 2131 | shortstr[1] = nostr[0]; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2132 | onekey(shortstr, _("No"), width); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2133 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2134 | wmove(bottomwin, 2, 16); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2135 | onekey("^C", _("Cancel"), width); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2136 | } |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2137 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2138 | wattron(bottomwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2139 | |
| 2140 | blank_statusbar(); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2141 | mvwaddnstr(bottomwin, 0, 0, msg, COLS - 1); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2142 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2143 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 2144 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2145 | wrefresh(bottomwin); |
| 2146 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2147 | do { |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2148 | int kbinput; |
| 2149 | int meta; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2150 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2151 | int mouse_x, mouse_y; |
Chris Allegretta | 235ab19 | 2001-04-12 13:24:40 +0000 | [diff] [blame] | 2152 | #endif |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2153 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2154 | kbinput = get_kbinput(edit, &meta); |
| 2155 | |
| 2156 | if (kbinput == NANO_CANCEL_KEY) |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2157 | ok = -1; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2158 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2159 | /* Look ma! We get to duplicate lots of code from |
| 2160 | * do_mouse()!! */ |
| 2161 | else if (kbinput == KEY_MOUSE) { |
| 2162 | kbinput = get_mouseinput(&mouse_x, &mouse_y, 0); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2163 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2164 | if (mouse_x != -1 && mouse_y != -1 && !ISSET(NO_HELP) && |
| 2165 | wenclose(bottomwin, mouse_y, mouse_x) && mouse_x < |
| 2166 | (width * 2) && mouse_y >= editwinrows + 3) { |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 2167 | |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2168 | int x = mouse_x / width; |
| 2169 | /* Did we click in the first column of shortcuts, or |
| 2170 | * the second? */ |
| 2171 | int y = mouse_y - editwinrows - 3; |
| 2172 | /* Did we click in the first row of shortcuts? */ |
| 2173 | |
| 2174 | assert(0 <= x && x <= 1 && 0 <= y && y <= 1); |
| 2175 | |
| 2176 | /* x = 0 means they clicked Yes or No. |
| 2177 | * y = 0 means Yes or All. */ |
| 2178 | ok = -2 * x * y + x - y + 1; |
| 2179 | |
| 2180 | if (ok == 2 && !all) |
| 2181 | ok = -2; |
| 2182 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2183 | } |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2184 | #endif |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2185 | /* Look for the kbinput in the yes, no and (optionally) all |
| 2186 | * str. */ |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2187 | else if (strchr(yesstr, kbinput) != NULL) |
| 2188 | ok = 1; |
| 2189 | else if (strchr(nostr, kbinput) != NULL) |
| 2190 | ok = 0; |
| 2191 | else if (all && strchr(allstr, kbinput) != NULL) |
| 2192 | ok = 2; |
| 2193 | } while (ok == -2); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2194 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 2195 | return ok; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2196 | } |
| 2197 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2198 | int total_refresh(void) |
| 2199 | { |
| 2200 | clearok(edit, TRUE); |
| 2201 | clearok(topwin, TRUE); |
| 2202 | clearok(bottomwin, TRUE); |
| 2203 | wnoutrefresh(edit); |
| 2204 | wnoutrefresh(topwin); |
| 2205 | wnoutrefresh(bottomwin); |
| 2206 | doupdate(); |
| 2207 | clearok(edit, FALSE); |
| 2208 | clearok(topwin, FALSE); |
| 2209 | clearok(bottomwin, FALSE); |
| 2210 | edit_refresh(); |
| 2211 | titlebar(NULL); |
| 2212 | return 1; |
| 2213 | } |
| 2214 | |
| 2215 | void display_main_list(void) |
| 2216 | { |
| 2217 | bottombars(main_list); |
| 2218 | } |
| 2219 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2220 | void statusbar(const char *msg, ...) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2221 | { |
| 2222 | va_list ap; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2223 | |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 2224 | va_start(ap, msg); |
| 2225 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2226 | /* Curses mode is turned off. If we use wmove() now, it will muck |
| 2227 | * up the terminal settings. So we just use vfprintf(). */ |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 2228 | if (curses_ended) { |
| 2229 | vfprintf(stderr, msg, ap); |
| 2230 | va_end(ap); |
| 2231 | return; |
| 2232 | } |
| 2233 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2234 | /* Blank out the line. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2235 | blank_statusbar(); |
| 2236 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2237 | if (COLS >= 4) { |
| 2238 | char *bar; |
| 2239 | char *foo; |
| 2240 | int start_x = 0; |
| 2241 | size_t foo_len; |
| 2242 | bar = charalloc(COLS - 3); |
| 2243 | vsnprintf(bar, COLS - 3, msg, ap); |
| 2244 | va_end(ap); |
| 2245 | foo = display_string(bar, 0, COLS - 4); |
| 2246 | free(bar); |
| 2247 | foo_len = strlen(foo); |
| 2248 | start_x = (COLS - foo_len - 4) / 2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2249 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2250 | wmove(bottomwin, 0, start_x); |
| 2251 | wattron(bottomwin, A_REVERSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2252 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2253 | waddstr(bottomwin, "[ "); |
| 2254 | waddstr(bottomwin, foo); |
| 2255 | free(foo); |
| 2256 | waddstr(bottomwin, " ]"); |
| 2257 | wattroff(bottomwin, A_REVERSE); |
| 2258 | wnoutrefresh(bottomwin); |
| 2259 | wrefresh(edit); |
| 2260 | /* Leave the cursor at its position in the edit window, not |
| 2261 | * in the statusbar. */ |
| 2262 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2263 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2264 | SET(DISABLE_CURPOS); |
| 2265 | statblank = 26; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2266 | } |
| 2267 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2268 | /* If constant is false, the user typed ^C so we unconditionally display |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2269 | * the cursor position. Otherwise, we display it only if the character |
| 2270 | * position changed, and DISABLE_CURPOS is not set. |
| 2271 | * |
| 2272 | * If constant and DISABLE_CURPOS is set, we unset it and update old_i and |
| 2273 | * old_totsize. That way, we leave the current statusbar alone, but next |
| 2274 | * time we will display. */ |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2275 | int do_cursorpos(int constant) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2276 | { |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2277 | const filestruct *fileptr; |
| 2278 | unsigned long i = 0; |
| 2279 | static unsigned long old_i = 0; |
| 2280 | static long old_totsize = -1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2281 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2282 | assert(current != NULL && fileage != NULL && totlines != 0); |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2283 | |
| 2284 | if (old_totsize == -1) |
| 2285 | old_totsize = totsize; |
| 2286 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2287 | for (fileptr = fileage; fileptr != current; fileptr = fileptr->next) { |
| 2288 | assert(fileptr != NULL); |
Chris Allegretta | f27c697 | 2002-02-12 01:57:24 +0000 | [diff] [blame] | 2289 | i += strlen(fileptr->data) + 1; |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2290 | } |
Chris Allegretta | f27c697 | 2002-02-12 01:57:24 +0000 | [diff] [blame] | 2291 | i += current_x; |
Chris Allegretta | 14b3ca9 | 2002-01-25 21:59:02 +0000 | [diff] [blame] | 2292 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2293 | if (constant && ISSET(DISABLE_CURPOS)) { |
| 2294 | UNSET(DISABLE_CURPOS); |
| 2295 | old_i = i; |
| 2296 | old_totsize = totsize; |
| 2297 | return 0; |
| 2298 | } |
Chris Allegretta | 14b3ca9 | 2002-01-25 21:59:02 +0000 | [diff] [blame] | 2299 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2300 | /* If constant is false, display the position on the statusbar |
| 2301 | * unconditionally; otherwise, only display the position when the |
| 2302 | * character values have changed. */ |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2303 | if (!constant || old_i != i || old_totsize != totsize) { |
| 2304 | unsigned long xpt = xplustabs() + 1; |
| 2305 | unsigned long cur_len = strlenpt(current->data) + 1; |
| 2306 | int linepct = 100 * current->lineno / totlines; |
| 2307 | int colpct = 100 * xpt / cur_len; |
| 2308 | int bytepct = totsize == 0 ? 0 : 100 * i / totsize; |
| 2309 | |
| 2310 | statusbar( |
| 2311 | _("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%ld (%d%%)"), |
| 2312 | current->lineno, totlines, linepct, |
| 2313 | xpt, cur_len, colpct, |
| 2314 | i, totsize, bytepct); |
| 2315 | UNSET(DISABLE_CURPOS); |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | old_i = i; |
| 2319 | old_totsize = totsize; |
| 2320 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2321 | reset_cursor(); |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2322 | return 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2323 | } |
| 2324 | |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 2325 | int do_cursorpos_void(void) |
| 2326 | { |
| 2327 | return do_cursorpos(0); |
| 2328 | } |
| 2329 | |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 2330 | /* Calculate the next line of help_text, starting at ptr. */ |
| 2331 | int line_len(const char *ptr) |
| 2332 | { |
| 2333 | int j = 0; |
| 2334 | |
| 2335 | while (*ptr != '\n' && *ptr != '\0' && j < COLS - 5) { |
| 2336 | ptr++; |
| 2337 | j++; |
| 2338 | } |
| 2339 | if (j == COLS - 5) { |
| 2340 | /* Don't wrap at the first of two spaces following a period. */ |
| 2341 | if (*ptr == ' ' && *(ptr + 1) == ' ') |
| 2342 | j++; |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2343 | /* 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] | 2344 | while (*ptr != ' ' && j > 0) { |
| 2345 | ptr--; |
| 2346 | j--; |
| 2347 | } |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2348 | /* Word longer than COLS - 5 chars just gets broken. */ |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 2349 | if (j == 0) |
| 2350 | j = COLS - 5; |
| 2351 | } |
| 2352 | assert(j >= 0 && j <= COLS - 4 && (j > 0 || *ptr == '\n')); |
| 2353 | return j; |
| 2354 | } |
| 2355 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2356 | /* Our shortcut-list-compliant help function, which is better than |
| 2357 | * nothing, and dynamic! */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2358 | int do_help(void) |
| 2359 | { |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 2360 | #ifndef DISABLE_HELP |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 2361 | int i, page = 0, kbinput = -1, meta, no_more = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2362 | int no_help_flag = 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2363 | const shortcut *oldshortcut; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2364 | |
| 2365 | blank_edit(); |
| 2366 | curs_set(0); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 2367 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2368 | blank_statusbar(); |
| 2369 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2370 | /* Set help_text as the string to display. */ |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 2371 | help_init(); |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 2372 | assert(help_text != NULL); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 2373 | |
| 2374 | oldshortcut = currshortcut; |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 2375 | |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 2376 | currshortcut = help_list; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 2377 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2378 | if (ISSET(NO_HELP)) { |
| 2379 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2380 | /* Well, if we're going to do this, we should at least do it the |
| 2381 | * right way. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2382 | no_help_flag = 1; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 2383 | UNSET(NO_HELP); |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 2384 | window_init(); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2385 | bottombars(help_list); |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 2386 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 2387 | } else |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2388 | bottombars(help_list); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2389 | |
| 2390 | do { |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 2391 | const char *ptr = help_text; |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 2392 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2393 | switch (kbinput) { |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2394 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2395 | case KEY_MOUSE: |
| 2396 | do_mouse(); |
| 2397 | break; |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 2398 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2399 | case NANO_NEXTPAGE_KEY: |
| 2400 | case NANO_NEXTPAGE_FKEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2401 | if (!no_more) { |
| 2402 | blank_edit(); |
| 2403 | page++; |
| 2404 | } |
| 2405 | break; |
| 2406 | case NANO_PREVPAGE_KEY: |
| 2407 | case NANO_PREVPAGE_FKEY: |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 2408 | if (page > 0) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2409 | no_more = 0; |
| 2410 | blank_edit(); |
| 2411 | page--; |
| 2412 | } |
| 2413 | break; |
| 2414 | } |
| 2415 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2416 | /* Calculate where in the text we should be, based on the |
| 2417 | * page. */ |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 2418 | for (i = 1; i < page * (editwinrows - 1); i++) { |
| 2419 | ptr += line_len(ptr); |
| 2420 | if (*ptr == '\n') |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2421 | ptr++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2422 | } |
| 2423 | |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 2424 | for (i = 0; i < editwinrows && *ptr != '\0'; i++) { |
| 2425 | int j = line_len(ptr); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2426 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2427 | mvwaddnstr(edit, i, 0, ptr, j); |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 2428 | ptr += j; |
| 2429 | if (*ptr == '\n') |
| 2430 | ptr++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2431 | } |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 2432 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2433 | if (*ptr == '\0') { |
| 2434 | no_more = 1; |
| 2435 | continue; |
| 2436 | } |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2437 | } while ((kbinput = get_kbinput(edit, &meta)) != NANO_EXIT_KEY && kbinput != NANO_EXIT_FKEY); |
Chris Allegretta | d1627cf | 2000-12-18 05:03:16 +0000 | [diff] [blame] | 2438 | |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 2439 | currshortcut = oldshortcut; |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 2440 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2441 | if (no_help_flag) { |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 2442 | blank_bottombars(); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 2443 | wrefresh(bottomwin); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2444 | SET(NO_HELP); |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 2445 | window_init(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2446 | } else |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2447 | bottombars(currshortcut); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2448 | |
| 2449 | curs_set(1); |
| 2450 | edit_refresh(); |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 2451 | |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 2452 | /* The help_init() at the beginning allocated help_text, which has |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2453 | * now been written to the screen. */ |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 2454 | free(help_text); |
| 2455 | help_text = NULL; |
| 2456 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2457 | #elif defined(DISABLE_HELP) |
| 2458 | nano_disabled_msg(); |
| 2459 | #endif |
| 2460 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2461 | return 1; |
| 2462 | } |
| 2463 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2464 | /* Highlight the current word being replaced or spell checked. We |
| 2465 | * expect word to have tabs and control characters expanded. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2466 | void do_replace_highlight(int highlight_flag, const char *word) |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 2467 | { |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2468 | int y = xplustabs(); |
| 2469 | size_t word_len = strlen(word); |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 2470 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2471 | y = get_page_start(y) + COLS - y; |
| 2472 | /* Now y is the number of characters we can display on this |
| 2473 | * line. */ |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 2474 | |
| 2475 | reset_cursor(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2476 | |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 2477 | if (highlight_flag) |
| 2478 | wattron(edit, A_REVERSE); |
| 2479 | |
David Lawrence Ramsey | 2a4ab6d | 2003-12-24 08:29:49 +0000 | [diff] [blame] | 2480 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 76c4b33 | 2003-12-24 08:17:54 +0000 | [diff] [blame] | 2481 | /* This is so we can show zero-length regexes. */ |
| 2482 | if (word_len == 0) |
| 2483 | waddstr(edit, " "); |
| 2484 | else |
David Lawrence Ramsey | 2a4ab6d | 2003-12-24 08:29:49 +0000 | [diff] [blame] | 2485 | #endif |
David Lawrence Ramsey | 76c4b33 | 2003-12-24 08:17:54 +0000 | [diff] [blame] | 2486 | waddnstr(edit, word, y - 1); |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2487 | |
| 2488 | if (word_len > y) |
| 2489 | waddch(edit, '$'); |
| 2490 | else if (word_len == y) |
| 2491 | waddch(edit, word[word_len - 1]); |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 2492 | |
| 2493 | if (highlight_flag) |
| 2494 | wattroff(edit, A_REVERSE); |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 2495 | } |
| 2496 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2497 | /* Fix editbot, based on the assumption that edittop is correct. */ |
| 2498 | void fix_editbot(void) |
| 2499 | { |
| 2500 | int i; |
| 2501 | |
| 2502 | editbot = edittop; |
| 2503 | for (i = 0; i < editwinrows && editbot->next != NULL; i++) |
| 2504 | editbot = editbot->next; |
| 2505 | } |
| 2506 | |
| 2507 | #ifdef DEBUG |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2508 | /* Dump the passed-in file structure to stderr. */ |
| 2509 | void dump_buffer(const filestruct *inptr) |
| 2510 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2511 | if (inptr == fileage) |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 2512 | fprintf(stderr, "Dumping file buffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2513 | else if (inptr == cutbuffer) |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 2514 | fprintf(stderr, "Dumping cutbuffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2515 | else |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 2516 | fprintf(stderr, "Dumping a buffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2517 | |
| 2518 | while (inptr != NULL) { |
| 2519 | fprintf(stderr, "(%d) %s\n", inptr->lineno, inptr->data); |
| 2520 | inptr = inptr->next; |
| 2521 | } |
| 2522 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2523 | |
David Lawrence Ramsey | 5ffbec5 | 2003-09-16 01:16:49 +0000 | [diff] [blame] | 2524 | /* Dump the file structure to stderr in reverse. */ |
David Lawrence Ramsey | aaad3af | 2003-08-31 16:44:10 +0000 | [diff] [blame] | 2525 | void dump_buffer_reverse(void) |
| 2526 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2527 | const filestruct *fileptr = filebot; |
| 2528 | |
| 2529 | while (fileptr != NULL) { |
| 2530 | fprintf(stderr, "(%d) %s\n", fileptr->lineno, fileptr->data); |
| 2531 | fileptr = fileptr->prev; |
| 2532 | } |
| 2533 | } |
| 2534 | #endif /* DEBUG */ |
| 2535 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2536 | #ifdef NANO_EXTRA |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 2537 | #define CREDIT_LEN 53 |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2538 | #define XLCREDIT_LEN 8 |
| 2539 | |
David Lawrence Ramsey | fdece46 | 2004-01-19 18:15:03 +0000 | [diff] [blame] | 2540 | /* Easter egg: Display credits. Assume nodelay(edit) is FALSE. */ |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2541 | void do_credits(void) |
| 2542 | { |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 2543 | int i, j = 0, k, place = 0, start_x; |
David Lawrence Ramsey | e97c8d5 | 2004-01-14 19:26:29 +0000 | [diff] [blame] | 2544 | struct timespec scrolldelay; |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2545 | |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 2546 | const char *what; |
| 2547 | const char *xlcredits[XLCREDIT_LEN]; |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2548 | |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 2549 | const char *credits[CREDIT_LEN] = { |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2550 | "0", /* "The nano text editor" */ |
| 2551 | "1", /* "version" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2552 | VERSION, |
| 2553 | "", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2554 | "2", /* "Brought to you by:" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2555 | "Chris Allegretta", |
| 2556 | "Jordi Mallach", |
| 2557 | "Adam Rogoyski", |
| 2558 | "Rob Siemborski", |
| 2559 | "Rocco Corsi", |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2560 | "David Lawrence Ramsey", |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 2561 | "David Benbennick", |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2562 | "Ken Tyler", |
| 2563 | "Sven Guckes", |
| 2564 | "Florian König", |
| 2565 | "Pauli Virtanen", |
| 2566 | "Daniele Medri", |
| 2567 | "Clement Laforet", |
| 2568 | "Tedi Heriyanto", |
| 2569 | "Bill Soudan", |
| 2570 | "Christian Weisgerber", |
| 2571 | "Erik Andersen", |
| 2572 | "Big Gaute", |
| 2573 | "Joshua Jensen", |
| 2574 | "Ryan Krebs", |
| 2575 | "Albert Chin", |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2576 | "", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2577 | "3", /* "Special thanks to:" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2578 | "Plattsburgh State University", |
| 2579 | "Benet Laboratories", |
| 2580 | "Amy Allegretta", |
| 2581 | "Linda Young", |
| 2582 | "Jeremy Robichaud", |
| 2583 | "Richard Kolb II", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2584 | "4", /* "The Free Software Foundation" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2585 | "Linus Torvalds", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2586 | "5", /* "For ncurses:" */ |
Chris Allegretta | dce44ab | 2002-03-16 01:03:41 +0000 | [diff] [blame] | 2587 | "Thomas Dickey", |
| 2588 | "Pavel Curtis", |
| 2589 | "Zeyd Ben-Halim", |
| 2590 | "Eric S. Raymond", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2591 | "6", /* "and anyone else we forgot..." */ |
| 2592 | "7", /* "Thank you for using nano!\n" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2593 | "", "", "", "", |
David Lawrence Ramsey | 6481c3f | 2004-01-09 23:06:54 +0000 | [diff] [blame] | 2594 | "(c) 1999-2004 Chris Allegretta", |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2595 | "", "", "", "", |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 2596 | "http://www.nano-editor.org/" |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2597 | }; |
| 2598 | |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2599 | xlcredits[0] = _("The nano text editor"); |
| 2600 | xlcredits[1] = _("version "); |
| 2601 | xlcredits[2] = _("Brought to you by:"); |
| 2602 | xlcredits[3] = _("Special thanks to:"); |
| 2603 | xlcredits[4] = _("The Free Software Foundation"); |
| 2604 | xlcredits[5] = _("For ncurses:"); |
| 2605 | xlcredits[6] = _("and anyone else we forgot..."); |
| 2606 | xlcredits[7] = _("Thank you for using nano!\n"); |
| 2607 | |
David Lawrence Ramsey | 9da0831 | 2004-01-14 22:40:38 +0000 | [diff] [blame] | 2608 | scrolldelay.tv_sec = 0; |
David Lawrence Ramsey | 62187d9 | 2004-01-14 22:45:05 +0000 | [diff] [blame] | 2609 | scrolldelay.tv_nsec = 700000000; |
David Lawrence Ramsey | e97c8d5 | 2004-01-14 19:26:29 +0000 | [diff] [blame] | 2610 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2611 | curs_set(0); |
| 2612 | nodelay(edit, TRUE); |
| 2613 | blank_bottombars(); |
| 2614 | mvwaddstr(topwin, 0, 0, hblank); |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 2615 | blank_edit(); |
| 2616 | wrefresh(edit); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2617 | wrefresh(bottomwin); |
| 2618 | wrefresh(topwin); |
| 2619 | |
| 2620 | while (wgetch(edit) == ERR) { |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 2621 | for (k = 0; k <= 1; k++) { |
| 2622 | blank_edit(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2623 | for (i = editwinrows / 2 - 1; i >= (editwinrows / 2 - 1 - j); |
| 2624 | i--) { |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 2625 | mvwaddstr(edit, i * 2 - k, 0, hblank); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2626 | |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2627 | if (place - (editwinrows / 2 - 1 - i) < CREDIT_LEN) { |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 2628 | what = credits[place - (editwinrows / 2 - 1 - i)]; |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 2629 | |
| 2630 | /* God I've missed hacking. If what is exactly |
| 2631 | 1 char long, it's a sentinel for a translated |
| 2632 | string, so use that instead. This means no |
| 2633 | thanking people with 1 character long names ;-) */ |
| 2634 | if (strlen(what) == 1) |
| 2635 | what = xlcredits[atoi(what)]; |
| 2636 | } else |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 2637 | what = ""; |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2638 | |
Chris Allegretta | 17dcb72 | 2001-01-20 21:40:07 +0000 | [diff] [blame] | 2639 | start_x = COLS / 2 - strlen(what) / 2 - 1; |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 2640 | mvwaddstr(edit, i * 2 - k, start_x, what); |
| 2641 | } |
David Lawrence Ramsey | e97c8d5 | 2004-01-14 19:26:29 +0000 | [diff] [blame] | 2642 | nanosleep(&scrolldelay, NULL); |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 2643 | wrefresh(edit); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2644 | } |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2645 | if (j < editwinrows / 2 - 1) |
| 2646 | j++; |
| 2647 | |
| 2648 | place++; |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2649 | |
| 2650 | if (place >= CREDIT_LEN + editwinrows / 2) |
| 2651 | break; |
| 2652 | } |
| 2653 | |
| 2654 | nodelay(edit, FALSE); |
| 2655 | curs_set(1); |
| 2656 | display_main_list(); |
| 2657 | total_refresh(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 2658 | } |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 2659 | #endif |