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 | * * |
Chris Allegretta | d757e25 | 2003-01-15 19:33:27 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2003 Chris Allegretta * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
Chris Allegretta | 3a24f3f | 2001-10-24 11:33:54 +0000 | [diff] [blame] | 8 | * the Free Software Foundation; either version 2, or (at your option) * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 9 | * any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 19 | * * |
| 20 | **************************************************************************/ |
| 21 | |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 24 | #include <stdarg.h> |
| 25 | #include <string.h> |
Chris Allegretta | dba37ae | 2000-07-07 05:13:09 +0000 | [diff] [blame] | 26 | #include <stdlib.h> |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 27 | #include <unistd.h> |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 28 | #include <ctype.h> |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 29 | #include <assert.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 30 | #include "proto.h" |
| 31 | #include "nano.h" |
| 32 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 33 | static int statblank = 0; /* Number of keystrokes left after |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 34 | we call statusbar(), before we |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 35 | actually blank the statusbar */ |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 36 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 37 | int do_first_line(void) |
| 38 | { |
| 39 | current = fileage; |
| 40 | placewewant = 0; |
| 41 | current_x = 0; |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 42 | edit_update(current, CENTER); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 43 | return 1; |
| 44 | } |
| 45 | |
| 46 | int do_last_line(void) |
| 47 | { |
| 48 | current = filebot; |
| 49 | placewewant = 0; |
| 50 | current_x = 0; |
Chris Allegretta | 234a34d | 2000-07-29 04:33:38 +0000 | [diff] [blame] | 51 | edit_update(current, CENTER); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 52 | return 1; |
| 53 | } |
| 54 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 55 | /* Return the placewewant associated with current_x. That is, xplustabs |
| 56 | * is the zero-based column position of the cursor. Value is no smaller |
| 57 | * than current_x. */ |
| 58 | size_t xplustabs(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 59 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 60 | return strnlenpt(current->data, current_x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 63 | /* Return what current_x should be, given xplustabs() for the line. */ |
| 64 | size_t actual_x(const filestruct *fileptr, size_t xplus) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 65 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 66 | size_t i = 0; |
| 67 | /* the position in fileptr->data, returned */ |
| 68 | size_t length = 0; |
| 69 | /* the screen display width to data[i] */ |
| 70 | char *c; |
| 71 | /* fileptr->data + i */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 72 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 73 | assert(fileptr != NULL && fileptr->data != NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 74 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 75 | for (c = fileptr->data; length < xplus && *c != '\0'; i++, c++) { |
| 76 | if (*c == '\t') |
| 77 | length += tabsize - length % tabsize; |
Chris Allegretta | cf287c8 | 2002-07-20 13:57:41 +0000 | [diff] [blame] | 78 | else if (is_cntrl_char((int)*c)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 79 | length += 2; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 80 | else |
| 81 | length++; |
| 82 | } |
| 83 | assert(length == strnlenpt(fileptr->data, i)); |
| 84 | assert(i <= strlen(fileptr->data)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 85 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 86 | if (length > xplus) |
| 87 | i--; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 88 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 89 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 90 | fprintf(stderr, "actual_x for xplus=%d returns %d\n", xplus, i); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 91 | #endif |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 92 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 93 | return i; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 96 | /* A strlen with tabs factored in, similar to xplustabs(). */ |
| 97 | size_t strnlenpt(const char *buf, size_t size) |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 98 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 99 | size_t length = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 100 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 101 | if (buf != NULL) |
| 102 | for (; *buf != '\0' && size != 0; size--, buf++) { |
| 103 | if (*buf == '\t') |
| 104 | length += tabsize - (length % tabsize); |
Chris Allegretta | cf287c8 | 2002-07-20 13:57:41 +0000 | [diff] [blame] | 105 | else if (is_cntrl_char((int)*buf)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 106 | length += 2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 107 | else |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 108 | length++; |
| 109 | } |
| 110 | return length; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 113 | size_t strlenpt(const char *buf) |
Chris Allegretta | d4fa0d3 | 2002-03-05 19:55:55 +0000 | [diff] [blame] | 114 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 115 | return strnlenpt(buf, -1); |
Chris Allegretta | d4fa0d3 | 2002-03-05 19:55:55 +0000 | [diff] [blame] | 116 | } |
| 117 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 118 | void blank_bottombars(void) |
| 119 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 120 | if (!no_help()) { |
| 121 | mvwaddstr(bottomwin, 1, 0, hblank); |
| 122 | mvwaddstr(bottomwin, 2, 0, hblank); |
| 123 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 124 | } |
| 125 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 126 | void blank_bottomwin(void) |
| 127 | { |
| 128 | if (ISSET(NO_HELP)) |
| 129 | return; |
| 130 | |
| 131 | mvwaddstr(bottomwin, 1, 0, hblank); |
| 132 | mvwaddstr(bottomwin, 2, 0, hblank); |
| 133 | } |
| 134 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 135 | void blank_edit(void) |
| 136 | { |
| 137 | int i; |
| 138 | for (i = 0; i <= editwinrows - 1; i++) |
| 139 | mvwaddstr(edit, i, 0, hblank); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | |
| 143 | void blank_statusbar(void) |
| 144 | { |
| 145 | mvwaddstr(bottomwin, 0, 0, hblank); |
| 146 | } |
| 147 | |
| 148 | void blank_statusbar_refresh(void) |
| 149 | { |
| 150 | blank_statusbar(); |
| 151 | wrefresh(bottomwin); |
| 152 | } |
| 153 | |
| 154 | void check_statblank(void) |
| 155 | { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 156 | if (statblank > 1) |
| 157 | statblank--; |
| 158 | else if (statblank == 1 && !ISSET(CONSTUPDATE)) { |
| 159 | statblank--; |
| 160 | blank_statusbar_refresh(); |
| 161 | } |
| 162 | } |
| 163 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 164 | /* Repaint the statusbar when getting a character in nanogetstr(). buf |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 165 | * should be no longer than COLS - 4. |
| 166 | * |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 167 | * 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] | 168 | * off! */ |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 169 | void nanoget_repaint(const char *buf, const char *inputbuf, int x) |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 170 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 171 | int len = strlen(buf) + 2; |
Chris Allegretta | 0d1e8d6 | 2000-11-02 15:30:24 +0000 | [diff] [blame] | 172 | int wid = COLS - len; |
| 173 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 174 | assert(wid >= 2); |
| 175 | assert(0 <= x && x <= strlen(inputbuf)); |
| 176 | |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 177 | wattron(bottomwin, A_REVERSE); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 178 | blank_statusbar(); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 179 | mvwaddstr(bottomwin, 0, 0, buf); |
| 180 | waddch(bottomwin, ':'); |
| 181 | waddch(bottomwin, x < wid ? ' ' : '$'); |
| 182 | waddnstr(bottomwin, &inputbuf[wid * (x / wid)], wid); |
| 183 | wmove(bottomwin, 0, (x % wid) + len); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 184 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | a0e957b | 2000-10-24 22:25:36 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 187 | /* Get the input from the kb; this should only be called from |
| 188 | * statusq(). */ |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 189 | int nanogetstr(int allowtabs, const char *buf, const char *def, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 190 | #ifndef NANO_SMALL |
| 191 | historyheadtype *history_list, |
| 192 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 193 | const shortcut *s |
Rocco Corsi | 06aca1c | 2001-01-11 05:30:31 +0000 | [diff] [blame] | 194 | #ifndef DISABLE_TABCOMP |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 195 | , int *list |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 196 | #endif |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 197 | ) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 198 | { |
| 199 | int kbinput; |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 200 | static int x = -1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 201 | /* the cursor position in 'answer' */ |
| 202 | int xend; |
| 203 | /* length of 'answer', the status bar text */ |
| 204 | int tabbed = 0; |
| 205 | /* used by input_tab() */ |
| 206 | const shortcut *t; |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 207 | |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 208 | #ifndef NANO_SMALL |
| 209 | /* for history */ |
| 210 | char *history = NULL; |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 211 | char *currentbuf = NULL; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 212 | char *complete = NULL; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 213 | int last_kbinput = 0; |
| 214 | |
| 215 | /* This variable is used in the search history code. use_cb == 0 |
| 216 | means that we're using the existing history and ignoring |
| 217 | currentbuf. use_cb == 1 means that the entry in answer should be |
| 218 | moved to currentbuf or restored from currentbuf to answer. |
| 219 | use_cb == 2 means that the entry in currentbuf should be moved to |
| 220 | answer or restored from answer to currentbuf. */ |
| 221 | int use_cb = 0; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 222 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 223 | xend = strlen(def); |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 224 | |
| 225 | /* Only put x at the end of the string if it's uninitialized or if |
| 226 | it would be past the end of the string as it is. Otherwise, |
| 227 | leave it alone. This is so the cursor position stays at the same |
| 228 | place if a prompt-changing toggle is pressed. */ |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 229 | if (x == -1 || x > xend || resetstatuspos) |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 230 | x = xend; |
| 231 | |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 232 | answer = charealloc(answer, xend + 1); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 233 | if (xend > 0) |
| 234 | strcpy(answer, def); |
| 235 | else |
| 236 | answer[0] = '\0'; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 237 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 238 | #if !defined(DISABLE_HELP) || (!defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION)) |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 239 | currshortcut = s; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 240 | #endif |
| 241 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 242 | /* Get the input! */ |
Chris Allegretta | 31925e4 | 2000-11-02 04:40:39 +0000 | [diff] [blame] | 243 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 244 | nanoget_repaint(buf, answer, x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 245 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 246 | /* Make sure any editor screen updates are displayed before getting |
| 247 | input */ |
Chris Allegretta | 022b96f | 2000-11-14 17:47:58 +0000 | [diff] [blame] | 248 | wrefresh(edit); |
| 249 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 250 | while ((kbinput = wgetch(bottomwin)) != 13) { |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 251 | for (t = s; t != NULL; t = t->next) { |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 252 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 253 | fprintf(stderr, "Aha! \'%c\' (%d)\n", kbinput, kbinput); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 254 | #endif |
| 255 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 256 | if (kbinput == t->val && kbinput < 32) { |
Chris Allegretta | 5bf51d3 | 2000-11-16 06:01:10 +0000 | [diff] [blame] | 257 | |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 258 | #ifndef DISABLE_HELP |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 259 | /* Have to do this here, it would be too late to do it |
| 260 | in statusq() */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 261 | if (kbinput == NANO_HELP_KEY || kbinput == NANO_HELP_FKEY) { |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 262 | do_help(); |
| 263 | break; |
| 264 | } |
| 265 | #endif |
Chris Allegretta | 5af5889 | 2003-01-17 21:07:38 +0000 | [diff] [blame] | 266 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 267 | return t->val; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 268 | } |
| 269 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 270 | assert(0 <= x && x <= xend && xend == strlen(answer)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 271 | |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 272 | if (kbinput != '\t') |
| 273 | tabbed = 0; |
| 274 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 275 | switch (kbinput) { |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 276 | |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 277 | /* Stuff we want to equate with <enter>, ASCII 13 */ |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 278 | case 343: |
Chris Allegretta | f9b6c9b | 2000-10-18 19:35:59 +0000 | [diff] [blame] | 279 | ungetch(13); /* Enter on iris-ansi $TERM, sometimes */ |
| 280 | break; |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 281 | /* Stuff we want to ignore */ |
Chris Allegretta | 5b1faac | 2000-11-16 19:55:30 +0000 | [diff] [blame] | 282 | #ifdef PDCURSES |
Chris Allegretta | 5b1faac | 2000-11-16 19:55:30 +0000 | [diff] [blame] | 283 | case 541: |
| 284 | case 542: |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 285 | case 543: /* Right ctrl again */ |
Chris Allegretta | 5b1faac | 2000-11-16 19:55:30 +0000 | [diff] [blame] | 286 | case 544: |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 287 | case 545: /* Right alt again */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 288 | break; |
Chris Allegretta | 5b1faac | 2000-11-16 19:55:30 +0000 | [diff] [blame] | 289 | #endif |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 290 | #if !defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION) |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 291 | case KEY_MOUSE: |
| 292 | do_mouse(); |
| 293 | break; |
| 294 | #endif |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 295 | case NANO_HOME_KEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 296 | case KEY_HOME: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 297 | x = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 298 | break; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 299 | case NANO_END_KEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 300 | case KEY_END: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 301 | x = xend; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 302 | break; |
| 303 | case KEY_RIGHT: |
Chris Allegretta | 35dac58 | 2001-03-21 15:07:20 +0000 | [diff] [blame] | 304 | case NANO_FORWARD_KEY: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 305 | if (x < xend) |
| 306 | x++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 307 | break; |
| 308 | case NANO_CONTROL_D: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 309 | if (x < xend) { |
| 310 | memmove(answer + x, answer + x + 1, xend - x); |
| 311 | xend--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 312 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 313 | break; |
| 314 | case NANO_CONTROL_K: |
| 315 | case NANO_CONTROL_U: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 316 | null_at(&answer, 0); |
| 317 | xend = 0; |
| 318 | x = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 319 | break; |
| 320 | case KEY_BACKSPACE: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 321 | case 127: |
| 322 | case NANO_CONTROL_H: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 323 | if (x > 0) { |
| 324 | memmove(answer + x - 1, answer + x, xend - x + 1); |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 325 | x--; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 326 | xend--; |
| 327 | } |
Chris Allegretta | 04d848e | 2000-11-05 17:54:41 +0000 | [diff] [blame] | 328 | break; |
| 329 | case NANO_CONTROL_I: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 330 | #ifndef NANO_SMALL |
| 331 | /* tab history completion */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 332 | if (history_list != NULL) { |
| 333 | if (!complete || last_kbinput != NANO_CONTROL_I) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 334 | history_list->current = (historytype *)history_list; |
| 335 | history_list->len = strlen(answer); |
| 336 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 337 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 338 | if (history_list->len > 0) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 339 | complete = get_history_completion(history_list, answer); |
| 340 | xend = strlen(complete); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 341 | x = xend; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 342 | answer = mallocstrcpy(answer, complete); |
| 343 | } |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 344 | } |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 345 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 346 | else |
| 347 | #endif |
Chris Allegretta | be77c61 | 2000-11-24 14:00:16 +0000 | [diff] [blame] | 348 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 349 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 350 | if (allowtabs) { |
| 351 | int shift = 0; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 352 | |
Chris Allegretta | 327abda | 2003-01-17 05:04:17 +0000 | [diff] [blame] | 353 | answer = input_tab(answer, x, &tabbed, &shift, list); |
| 354 | xend = strlen(answer); |
| 355 | x += shift; |
| 356 | if (x > xend) |
| 357 | x = xend; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 358 | } |
| 359 | #endif |
| 360 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 361 | case KEY_LEFT: |
Chris Allegretta | 35dac58 | 2001-03-21 15:07:20 +0000 | [diff] [blame] | 362 | case NANO_BACK_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 363 | if (x > 0) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 364 | x--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 365 | break; |
| 366 | case KEY_UP: |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 367 | case NANO_UP_KEY: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 368 | #ifndef NANO_SMALL |
Chris Allegretta | 4b376a4 | 2003-04-16 02:08:23 +0000 | [diff] [blame] | 369 | do_upkey: |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 370 | if (history_list != NULL) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 371 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 372 | /* if currentbuf is NULL, or if use_cb is 1, currentbuf |
| 373 | isn't NULL, and currentbuf is different from answer, |
| 374 | it means that we're scrolling up at the top of the |
| 375 | search history, and we need to save the current |
| 376 | answer in currentbuf; do this and reset use_cb to |
| 377 | 0 */ |
| 378 | if (currentbuf == NULL || (use_cb == 1 && strcmp(currentbuf, answer))) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 379 | currentbuf = mallocstrcpy(currentbuf, answer); |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 380 | use_cb = 0; |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 381 | } |
| 382 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 383 | /* if currentbuf isn't NULL, use_cb is 2, and currentbuf |
| 384 | is different from answer, it means that we're |
| 385 | scrolling up at the bottom of the search history, and |
| 386 | we need to make the string in currentbuf the current |
| 387 | answer; do this, blow away currentbuf since we don't |
| 388 | need it anymore, and reset use_cb to 0 */ |
| 389 | if (currentbuf != NULL && use_cb == 2 && strcmp(currentbuf, answer)) { |
| 390 | answer = mallocstrcpy(answer, currentbuf); |
| 391 | free(currentbuf); |
| 392 | currentbuf = NULL; |
| 393 | xend = strlen(answer); |
| 394 | use_cb = 0; |
| 395 | |
| 396 | /* else get older search from the history list and save |
| 397 | it in answer; if there is no older search, blank out |
| 398 | answer */ |
| 399 | } else if ((history = get_history_older(history_list)) != NULL) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 400 | answer = mallocstrcpy(answer, history); |
| 401 | xend = strlen(history); |
| 402 | } else { |
| 403 | answer = mallocstrcpy(answer, ""); |
| 404 | xend = 0; |
| 405 | } |
| 406 | x = xend; |
| 407 | } |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 408 | #endif |
Chris Allegretta | 54abd94 | 2003-01-09 23:43:12 +0000 | [diff] [blame] | 409 | break; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 410 | case KEY_DOWN: |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 411 | case NANO_DOWN_KEY: |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 412 | #ifndef NANO_SMALL |
Chris Allegretta | 4b376a4 | 2003-04-16 02:08:23 +0000 | [diff] [blame] | 413 | do_downkey: |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 414 | if (history_list != NULL) { |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 415 | |
| 416 | /* get newer search from the history list and save it |
| 417 | in answer */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 418 | if ((history = get_history_newer(history_list)) != NULL) { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 419 | answer = mallocstrcpy(answer, history); |
| 420 | xend = strlen(history); |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 421 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 422 | /* if there is no newer search, we're here */ |
| 423 | |
| 424 | /* if currentbuf isn't NULL and use_cb isn't 2, it means |
| 425 | that we're scrolling down at the bottom of the search |
| 426 | history and we need to make the string in currentbuf |
| 427 | the current answer; do this, blow away currentbuf |
| 428 | since we don't need it anymore, and set use_cb to |
| 429 | 1 */ |
| 430 | } else if (currentbuf != NULL && use_cb != 2) { |
Chris Allegretta | 8031f83 | 2003-01-09 05:29:58 +0000 | [diff] [blame] | 431 | answer = mallocstrcpy(answer, currentbuf); |
Chris Allegretta | 09fc430 | 2003-01-16 22:16:38 +0000 | [diff] [blame] | 432 | free(currentbuf); |
| 433 | currentbuf = NULL; |
| 434 | xend = strlen(answer); |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 435 | use_cb = 1; |
| 436 | |
| 437 | /* otherwise, if currentbuf is NULL and use_cb isn't 2, |
| 438 | it means that we're scrolling down at the bottom of |
| 439 | the search history and the current answer needs to be |
| 440 | saved in currentbuf; do this, blank out answer, and |
| 441 | set use_cb to 2 */ |
| 442 | } else if (use_cb != 2) { |
| 443 | currentbuf = mallocstrcpy(currentbuf, answer); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 444 | answer = mallocstrcpy(answer, ""); |
| 445 | xend = 0; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 446 | use_cb = 2; |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 447 | } |
| 448 | x = xend; |
| 449 | } |
| 450 | #endif |
| 451 | break; |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 452 | case KEY_DC: |
| 453 | goto do_deletekey; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 454 | case 27: |
| 455 | switch (kbinput = wgetch(edit)) { |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 456 | case 'O': |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 457 | switch (kbinput = wgetch(edit)) { |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 458 | case 'F': |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 459 | x = xend; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 460 | break; |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 461 | case 'H': |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 462 | x = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 463 | break; |
| 464 | } |
| 465 | break; |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 466 | case '[': |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 467 | switch (kbinput = wgetch(edit)) { |
Chris Allegretta | 4b376a4 | 2003-04-16 02:08:23 +0000 | [diff] [blame] | 468 | case 'A': |
| 469 | #ifndef NANO_SMALL |
| 470 | goto do_upkey; |
| 471 | #else |
| 472 | break; |
| 473 | #endif |
| 474 | case 'B': |
| 475 | #ifndef NANO_SMALL |
| 476 | goto do_downkey; |
| 477 | #else |
| 478 | break; |
| 479 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 480 | case 'C': |
| 481 | if (x < xend) |
| 482 | x++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 483 | break; |
| 484 | case 'D': |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 485 | if (x > 0) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 486 | x--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 487 | break; |
Chris Allegretta | 13f094e | 2003-08-04 00:59:27 +0000 | [diff] [blame] | 488 | case 'F': |
Chris Allegretta | 653d614 | 2003-08-04 02:12:03 +0000 | [diff] [blame] | 489 | x = xend; |
Chris Allegretta | 13f094e | 2003-08-04 00:59:27 +0000 | [diff] [blame] | 490 | break; |
| 491 | case 'H': |
Chris Allegretta | 653d614 | 2003-08-04 02:12:03 +0000 | [diff] [blame] | 492 | x = 0; |
Chris Allegretta | 13f094e | 2003-08-04 00:59:27 +0000 | [diff] [blame] | 493 | break; |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 494 | case '1': |
| 495 | case '7': |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 496 | x = 0; |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 497 | goto skip_tilde; |
| 498 | case '3': |
| 499 | do_deletekey: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 500 | if (x < xend) { |
| 501 | memmove(answer + x, answer + x + 1, xend - x); |
| 502 | xend--; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 503 | } |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 504 | goto skip_tilde; |
| 505 | case '4': |
| 506 | case '8': |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 507 | x = xend; |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 508 | skip_tilde: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 509 | nodelay(edit, TRUE); |
| 510 | kbinput = wgetch(edit); |
Chris Allegretta | ce78c1e | 2001-09-23 01:18:03 +0000 | [diff] [blame] | 511 | if (kbinput == '~' || kbinput == ERR) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 512 | kbinput = -1; |
| 513 | nodelay(edit, FALSE); |
| 514 | break; |
| 515 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 516 | break; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 517 | default: |
| 518 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 519 | for (t = s; t != NULL; t = t->next) { |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 520 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 521 | fprintf(stderr, "Aha! \'%c\' (%d)\n", kbinput, |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 522 | kbinput); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 523 | #endif |
Chris Allegretta | 5af5889 | 2003-01-17 21:07:38 +0000 | [diff] [blame] | 524 | if (kbinput == t->val || kbinput == t->val - 32) |
| 525 | /* We hit an Alt key. Do like above. We don't |
| 526 | just ungetch() the letter and let it get |
| 527 | caught above cause that screws the |
| 528 | keypad... */ |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 529 | return t->val; |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 530 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 531 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 532 | break; |
| 533 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 534 | default: |
| 535 | if (kbinput < 32) |
| 536 | break; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 537 | answer = charealloc(answer, xend + 2); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 538 | memmove(answer + x + 1, answer + x, xend - x + 1); |
| 539 | xend++; |
| 540 | answer[x] = kbinput; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 541 | x++; |
| 542 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 543 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 544 | fprintf(stderr, "input \'%c\' (%d)\n", kbinput, kbinput); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 545 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 546 | } /* switch (kbinput) */ |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 547 | #ifndef NANO_SMALL |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 548 | last_kbinput = kbinput; |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 549 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 550 | nanoget_repaint(buf, answer, x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 551 | wrefresh(bottomwin); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 552 | } /* while (kbinput ...) */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 553 | |
Chris Allegretta | 5af5889 | 2003-01-17 21:07:38 +0000 | [diff] [blame] | 554 | /* We finished putting in an answer; reset x */ |
| 555 | x = -1; |
| 556 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 557 | /* Just check for a blank answer here */ |
Chris Allegretta | 15c28f8 | 2003-01-05 21:47:06 +0000 | [diff] [blame] | 558 | if (answer[0] == '\0') |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 559 | return -2; |
| 560 | else |
| 561 | return 0; |
| 562 | } |
| 563 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 564 | /* If modified is not already set, set it and update titlebar. */ |
| 565 | void set_modified(void) |
| 566 | { |
| 567 | if (!ISSET(MODIFIED)) { |
| 568 | SET(MODIFIED); |
| 569 | titlebar(NULL); |
| 570 | wrefresh(topwin); |
| 571 | } |
| 572 | } |
| 573 | |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 574 | void titlebar(const char *path) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 575 | { |
| 576 | int namelen, space; |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 577 | const char *what = path; |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 578 | |
| 579 | if (path == NULL) |
| 580 | what = filename; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 581 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 582 | wattron(topwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 583 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 584 | mvwaddstr(topwin, 0, 0, hblank); |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 585 | mvwaddnstr(topwin, 0, 2, VERMSG, COLS - 3); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 586 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 587 | space = COLS - sizeof(VERMSG) - 22; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 588 | |
Chris Allegretta | f4b9601 | 2001-01-03 07:11:47 +0000 | [diff] [blame] | 589 | namelen = strlen(what); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 590 | |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 591 | if (space > 0) { |
| 592 | if (what[0] == '\0') |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 593 | mvwaddnstr(topwin, 0, COLS / 2 - 6, _("New Buffer"), |
| 594 | COLS / 2 + COLS % 2 - 6); |
| 595 | else if (namelen > space) { |
| 596 | if (path == NULL) |
| 597 | waddstr(topwin, _(" File: ...")); |
| 598 | else |
| 599 | waddstr(topwin, _(" DIR: ...")); |
| 600 | waddstr(topwin, &what[namelen - space]); |
| 601 | } else { |
| 602 | if (path == NULL) |
| 603 | mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1), |
| 604 | _("File: ")); |
| 605 | else |
| 606 | mvwaddstr(topwin, 0, COLS / 2 - (namelen / 2 + 1), |
| 607 | _(" DIR: ")); |
| 608 | waddstr(topwin, what); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 609 | } |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 610 | } /* If we don't have space, we shouldn't bother */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 611 | if (ISSET(MODIFIED)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 612 | mvwaddnstr(topwin, 0, COLS - 11, _(" Modified "), 11); |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 613 | else if (ISSET(VIEW_MODE)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 614 | mvwaddnstr(topwin, 0, COLS - 11, _(" View "), 11); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 615 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 616 | wattroff(topwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 617 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 618 | wrefresh(topwin); |
| 619 | reset_cursor(); |
| 620 | } |
| 621 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 622 | void bottombars(const shortcut *s) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 623 | { |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 624 | int i, j, numcols; |
Chris Allegretta | 3bbc416 | 2003-01-23 00:46:12 +0000 | [diff] [blame] | 625 | char keystr[9]; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 626 | int slen; |
| 627 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 628 | if (ISSET(NO_HELP)) |
| 629 | return; |
| 630 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 631 | if (s == main_list) { |
| 632 | slen = MAIN_VISIBLE; |
| 633 | assert(MAIN_VISIBLE <= length_of_list(s)); |
| 634 | } else |
| 635 | slen = length_of_list(s); |
| 636 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 637 | /* There will be this many columns of shortcuts */ |
| 638 | numcols = (slen + (slen % 2)) / 2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 639 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 640 | blank_bottomwin(); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 641 | |
Chris Allegretta | bc72e36 | 2002-02-16 20:03:44 +0000 | [diff] [blame] | 642 | for (i = 0; i < numcols; i++) { |
| 643 | for (j = 0; j <= 1; j++) { |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 644 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 645 | wmove(bottomwin, 1 + j, i * (COLS / numcols)); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 646 | |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 647 | /* Yucky sentinel values we can't handle a better way */ |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 648 | if (s->val == NANO_CONTROL_SPACE) |
| 649 | strcpy(keystr, "^ "); |
Chris Allegretta | a65ba51 | 2003-01-05 20:57:07 +0000 | [diff] [blame] | 650 | #ifndef NANO_SMALL |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 651 | else if (s->val == KEY_UP) |
Chris Allegretta | 3bbc416 | 2003-01-23 00:46:12 +0000 | [diff] [blame] | 652 | strncpy(keystr, _("Up"), 8); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 653 | #endif /* NANO_SMALL */ |
| 654 | else if (s->val > 0) { |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 655 | if (s->val < 64) |
| 656 | sprintf(keystr, "^%c", s->val + 64); |
| 657 | else |
| 658 | sprintf(keystr, "M-%c", s->val - 32); |
| 659 | } else if (s->altval > 0) |
| 660 | sprintf(keystr, "M-%c", s->altval); |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 661 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 662 | onekey(keystr, s->desc, COLS / numcols); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 663 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 664 | s = s->next; |
| 665 | if (s == NULL) |
| 666 | goto break_completely_out; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 667 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 670 | break_completely_out: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 671 | wrefresh(bottomwin); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 672 | } |
| 673 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 674 | /* Write a shortcut key to the help area at the bottom of the window. |
| 675 | * keystroke is e.g. "^G" and desc is e.g. "Get Help". |
| 676 | * We are careful to write exactly len characters, even if len is |
| 677 | * very small and keystroke and desc are long. */ |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 678 | void onekey(const char *keystroke, const char *desc, int len) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 679 | { |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 680 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 681 | wattron(bottomwin, A_REVERSE); |
| 682 | waddnstr(bottomwin, keystroke, len); |
| 683 | wattroff(bottomwin, A_REVERSE); |
| 684 | len -= strlen(keystroke); |
| 685 | if (len > 0) { |
| 686 | waddch(bottomwin, ' '); |
| 687 | len--; |
| 688 | waddnstr(bottomwin, desc, len); |
| 689 | len -= strlen(desc); |
| 690 | for (; len > 0; len--) |
| 691 | waddch(bottomwin, ' '); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 692 | } |
| 693 | } |
| 694 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 695 | /* And so start the display update routines. */ |
| 696 | |
| 697 | #ifndef NDEBUG |
| 698 | int check_linenumbers(const filestruct *fileptr) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 699 | { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 700 | int check_line = 0; |
| 701 | const filestruct *filetmp; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 702 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 703 | for (filetmp = edittop; filetmp != fileptr; filetmp = filetmp->next) |
| 704 | check_line++; |
| 705 | return check_line; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 706 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 707 | #endif |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 708 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 709 | /* nano scrolls horizontally within a line in chunks. This function |
| 710 | * returns the column number of the first character displayed in the |
| 711 | * window when the cursor is at the given column. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 712 | int get_page_start(int column) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 713 | { |
| 714 | assert(COLS > 9); |
| 715 | return column < COLS - 1 ? 0 : column - 7 - (column - 8) % (COLS - 9); |
| 716 | } |
| 717 | |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 718 | /* Resets current_y, based on the position of current, and puts the |
| 719 | * cursor at (current_y, current_x). */ |
| 720 | void reset_cursor(void) |
| 721 | { |
| 722 | const filestruct *ptr = edittop; |
| 723 | size_t x; |
| 724 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 725 | /* Yuck. This condition can be true after open_file() when opening |
| 726 | * the first file. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 727 | if (edittop == NULL) |
| 728 | return; |
| 729 | |
| 730 | current_y = 0; |
| 731 | |
| 732 | while (ptr != current && ptr != editbot && ptr->next != NULL) { |
| 733 | ptr = ptr->next; |
| 734 | current_y++; |
| 735 | } |
| 736 | |
| 737 | x = xplustabs(); |
| 738 | wmove(edit, current_y, x - get_page_start(x)); |
| 739 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 740 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 741 | /* edit_add() takes care of the job of actually painting a line into |
| 742 | * the edit window. Called only from update_line(). Expects a |
| 743 | * converted-to-not-have-tabs line. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 744 | void edit_add(const filestruct *fileptr, int yval, int start |
| 745 | #ifndef NANO_SMALL |
| 746 | , int virt_mark_beginx, int virt_cur_x |
| 747 | #endif |
| 748 | ) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 749 | { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 750 | #ifdef DEBUG |
| 751 | fprintf(stderr, "Painting line %d, current is %d\n", fileptr->lineno, |
| 752 | current->lineno); |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 753 | #endif |
| 754 | |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 755 | /* Just paint the string in any case (we'll add color or reverse on |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 756 | just the text that needs it */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 757 | mvwaddnstr(edit, yval, 0, &fileptr->data[start], COLS); |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 758 | |
Chris Allegretta | 7dd7768 | 2001-12-08 19:52:28 +0000 | [diff] [blame] | 759 | #ifdef ENABLE_COLOR |
Chris Allegretta | 1dd0bc9 | 2002-10-13 18:43:45 +0000 | [diff] [blame] | 760 | if (colorstrings != NULL && ISSET(COLOR_SYNTAX)) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 761 | const colortype *tmpcolor = colorstrings; |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 762 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 763 | for (; tmpcolor != NULL; tmpcolor = tmpcolor->next) { |
| 764 | int x_start; |
| 765 | /* Starting column for mvwaddnstr. Zero-based. */ |
| 766 | int paintlen; |
| 767 | /* number of chars to paint on this line. There are COLS |
| 768 | * characters on a whole line. */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 769 | regmatch_t startmatch; /* match position for start_regexp*/ |
| 770 | regmatch_t endmatch; /* match position for end_regexp*/ |
| 771 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 772 | if (tmpcolor->bright) |
| 773 | wattron(edit, A_BOLD); |
| 774 | wattron(edit, COLOR_PAIR(tmpcolor->pairnum)); |
| 775 | /* Two notes about regexec. Return value 0 means there is a |
| 776 | * match. Also, rm_eo is the first non-matching character |
| 777 | * after the match. */ |
| 778 | |
| 779 | /* First case, tmpcolor is a single-line expression. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 780 | if (tmpcolor->end == NULL) { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 781 | size_t k = 0; |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 782 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 783 | /* We increment k by rm_eo, to move past the end of the |
| 784 | last match. Even though two matches may overlap, we |
| 785 | want to ignore them, so that we can highlight C-strings |
| 786 | correctly. */ |
| 787 | while (k < start + COLS) { |
| 788 | /* Note the fifth parameter to regexec. It says not to |
| 789 | * match the beginning-of-line character unless |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 790 | * k == 0. If regexec returns nonzero, there are no |
| 791 | * more matches in the line. */ |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 792 | if (regexec(&tmpcolor->start, &fileptr->data[k], 1, |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 793 | &startmatch, k == 0 ? 0 : REG_NOTBOL)) |
| 794 | break; |
| 795 | /* Translate the match to the beginning of the line. */ |
| 796 | startmatch.rm_so += k; |
| 797 | startmatch.rm_eo += k; |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 798 | if (startmatch.rm_so == startmatch.rm_eo) { |
| 799 | startmatch.rm_eo++; |
Chris Allegretta | 7c27be4 | 2002-05-05 23:03:54 +0000 | [diff] [blame] | 800 | statusbar(_("Refusing 0 length regex match")); |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 801 | } else if (startmatch.rm_so < start + COLS && |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 802 | startmatch.rm_eo > start) { |
| 803 | x_start = startmatch.rm_so - start; |
| 804 | if (x_start < 0) |
| 805 | x_start = 0; |
| 806 | paintlen = startmatch.rm_eo - start - x_start; |
| 807 | if (paintlen > COLS - x_start) |
| 808 | paintlen = COLS - x_start; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 809 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 810 | assert(0 <= x_start && 0 < paintlen && |
| 811 | x_start + paintlen <= COLS); |
| 812 | mvwaddnstr(edit, yval, x_start, |
| 813 | fileptr->data + start + x_start, paintlen); |
| 814 | } |
| 815 | k = startmatch.rm_eo; |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 816 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 817 | } else { |
| 818 | /* This is a multi-line regexp. There are two steps. |
| 819 | * First, we have to see if the beginning of the line is |
| 820 | * colored by a start on an earlier line, and an end on |
| 821 | * this line or later. |
| 822 | * |
| 823 | * We find the first line before fileptr matching the |
| 824 | * start. If every match on that line is followed by an |
| 825 | * end, then go to step two. Otherwise, find the next line |
| 826 | * after start_line matching the end. If that line is not |
| 827 | * before fileptr, then paint the beginning of this line. */ |
Chris Allegretta | 3674c1d | 2002-05-12 20:43:49 +0000 | [diff] [blame] | 828 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 829 | const filestruct *start_line = fileptr->prev; |
| 830 | /* the first line before fileptr matching start*/ |
| 831 | regoff_t start_col; |
| 832 | /* where it starts in that line */ |
| 833 | const filestruct *end_line; |
| 834 | int searched_later_lines = 0; |
| 835 | /* Used in step 2. Have we looked for an end on |
| 836 | * lines after fileptr? */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 837 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 838 | while (start_line != NULL && |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 839 | regexec(&tmpcolor->start, start_line->data, 1, |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 840 | &startmatch, 0)) { |
| 841 | /* If there is an end on this line, there is no need |
| 842 | * to look for starts on earlier lines. */ |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 843 | if (!regexec(tmpcolor->end, start_line->data, 1, |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 844 | &endmatch, 0)) |
| 845 | goto step_two; |
| 846 | start_line = start_line->prev; |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 847 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 848 | /* No start found, so skip to the next step. */ |
| 849 | if (start_line == NULL) |
| 850 | goto step_two; |
| 851 | /* Now start_line is the first line before fileptr |
| 852 | * containing a start match. Is there a start on this |
| 853 | * line not followed by an end on this line? */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 854 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 855 | start_col = 0; |
| 856 | while (1) { |
| 857 | start_col += startmatch.rm_so; |
| 858 | startmatch.rm_eo -= startmatch.rm_so; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 859 | if (regexec(tmpcolor->end, |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 860 | start_line->data + start_col + startmatch.rm_eo, |
| 861 | 1, &endmatch, |
| 862 | start_col + startmatch.rm_eo == 0 ? 0 : REG_NOTBOL)) |
| 863 | /* No end found after this start */ |
| 864 | break; |
| 865 | start_col++; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 866 | if (regexec(&tmpcolor->start, |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 867 | start_line->data + start_col, 1, &startmatch, |
| 868 | REG_NOTBOL)) |
| 869 | /* No later start on this line. */ |
| 870 | goto step_two; |
| 871 | } |
| 872 | /* Indeed, there is a start not followed on this line by an |
| 873 | * end. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 874 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 875 | /* We have already checked that there is no end before |
| 876 | * fileptr and after the start. Is there an end after |
| 877 | * the start at all? We don't paint unterminated starts. */ |
| 878 | end_line = fileptr; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 879 | while (end_line != NULL && |
| 880 | regexec(tmpcolor->end, end_line->data, 1, &endmatch, 0)) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 881 | end_line = end_line->next; |
| 882 | |
| 883 | /* No end found, or it is too early. */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 884 | if (end_line == NULL || end_line->lineno < fileptr->lineno || |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 885 | (end_line == fileptr && endmatch.rm_eo <= start)) |
| 886 | goto step_two; |
| 887 | |
| 888 | /* Now paint the start of fileptr. */ |
| 889 | paintlen = end_line != fileptr |
| 890 | ? COLS : endmatch.rm_eo - start; |
| 891 | if (paintlen > COLS) |
| 892 | paintlen = COLS; |
| 893 | |
| 894 | assert(0 < paintlen && paintlen <= COLS); |
| 895 | mvwaddnstr(edit, yval, 0, fileptr->data + start, paintlen); |
| 896 | |
| 897 | /* We have already painted the whole line. */ |
| 898 | if (paintlen == COLS) |
| 899 | goto skip_step_two; |
| 900 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 901 | step_two: /* Second step, we look for starts on this line. */ |
| 902 | start_col = 0; |
| 903 | while (start_col < start + COLS) { |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 904 | if (regexec(&tmpcolor->start, fileptr->data + start_col, 1, |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 905 | &startmatch, start_col == 0 ? 0 : REG_NOTBOL) |
| 906 | || start_col + startmatch.rm_so >= start + COLS) |
| 907 | /* No more starts on this line. */ |
| 908 | break; |
| 909 | /* Translate the match to be relative to the |
| 910 | * beginning of the line. */ |
| 911 | startmatch.rm_so += start_col; |
| 912 | startmatch.rm_eo += start_col; |
| 913 | |
| 914 | x_start = startmatch.rm_so - start; |
| 915 | if (x_start < 0) { |
| 916 | x_start = 0; |
| 917 | startmatch.rm_so = start; |
| 918 | } |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 919 | if (!regexec(tmpcolor->end, fileptr->data + startmatch.rm_eo, |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 920 | 1, &endmatch, |
| 921 | startmatch.rm_eo == 0 ? 0 : REG_NOTBOL)) { |
| 922 | /* Translate the end match to be relative to the |
| 923 | beginning of the line. */ |
| 924 | endmatch.rm_so += startmatch.rm_eo; |
| 925 | endmatch.rm_eo += startmatch.rm_eo; |
| 926 | /* There is an end on this line. But does it |
| 927 | appear on this page, and is the match more than |
| 928 | zero characters long? */ |
| 929 | if (endmatch.rm_eo > start && |
| 930 | endmatch.rm_eo > startmatch.rm_so) { |
| 931 | paintlen = endmatch.rm_eo - start - x_start; |
| 932 | if (x_start + paintlen > COLS) |
| 933 | paintlen = COLS - x_start; |
| 934 | |
| 935 | assert(0 <= x_start && 0 < paintlen && |
| 936 | x_start + paintlen <= COLS); |
| 937 | mvwaddnstr(edit, yval, x_start, |
| 938 | fileptr->data + start + x_start, paintlen); |
| 939 | } |
| 940 | } else if (!searched_later_lines) { |
| 941 | searched_later_lines = 1; |
| 942 | /* There is no end on this line. But we haven't |
| 943 | * yet looked for one on later lines. */ |
| 944 | end_line = fileptr->next; |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 945 | while (end_line != NULL && regexec(tmpcolor->end, |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 946 | end_line->data, 1, &endmatch, 0)) |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 947 | end_line = end_line->next; |
| 948 | if (end_line != NULL) { |
| 949 | assert(0 <= x_start && x_start < COLS); |
| 950 | mvwaddnstr(edit, yval, x_start, |
| 951 | fileptr->data + start + x_start, |
| 952 | COLS - x_start); |
| 953 | /* We painted to the end of the line, so |
| 954 | * don't bother checking any more starts. */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 955 | break; |
Chris Allegretta | 3674c1d | 2002-05-12 20:43:49 +0000 | [diff] [blame] | 956 | } |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 957 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 958 | start_col = startmatch.rm_so + 1; |
| 959 | } /* while start_col < start + COLS */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 960 | } /* if (tmp_color->end != NULL) */ |
Chris Allegretta | 6c1e661 | 2002-01-19 16:52:34 +0000 | [diff] [blame] | 961 | |
Chris Allegretta | ce452fb | 2003-02-03 02:56:44 +0000 | [diff] [blame] | 962 | skip_step_two: |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 963 | wattroff(edit, A_BOLD); |
| 964 | wattroff(edit, COLOR_PAIR(tmpcolor->pairnum)); |
| 965 | } /* for tmpcolor in colorstrings */ |
| 966 | } |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 967 | #endif /* ENABLE_COLOR */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 968 | |
Chris Allegretta | 7dd7768 | 2001-12-08 19:52:28 +0000 | [diff] [blame] | 969 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 970 | if (ISSET(MARK_ISSET) |
| 971 | && (fileptr->lineno <= mark_beginbuf->lineno |
| 972 | || fileptr->lineno <= current->lineno) |
| 973 | && (fileptr->lineno >= mark_beginbuf->lineno |
| 974 | || fileptr->lineno >= current->lineno)) { |
| 975 | /* fileptr is at least partially selected. */ |
Chris Allegretta | 2fa11b8 | 2001-12-02 04:55:44 +0000 | [diff] [blame] | 976 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 977 | int x_start; |
| 978 | /* Starting column for mvwaddnstr. Zero-based. */ |
| 979 | int paintlen; |
| 980 | /* number of chars to paint on this line. There are COLS |
| 981 | * characters on a whole line. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 982 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 983 | if (mark_beginbuf == fileptr && current == fileptr) { |
| 984 | x_start = virt_mark_beginx < virt_cur_x ? virt_mark_beginx |
| 985 | : virt_cur_x; |
| 986 | paintlen = abs(virt_mark_beginx - virt_cur_x); |
| 987 | } else { |
| 988 | if (mark_beginbuf->lineno < fileptr->lineno || |
| 989 | current->lineno < fileptr->lineno) |
| 990 | x_start = 0; |
| 991 | else |
| 992 | x_start = mark_beginbuf == fileptr ? virt_mark_beginx |
| 993 | : virt_cur_x; |
| 994 | |
| 995 | if (mark_beginbuf->lineno > fileptr->lineno || |
| 996 | current->lineno > fileptr->lineno) |
| 997 | paintlen = start + COLS; |
| 998 | else |
| 999 | paintlen = mark_beginbuf == fileptr ? virt_mark_beginx |
| 1000 | : virt_cur_x; |
| 1001 | } |
| 1002 | x_start -= start; |
| 1003 | if (x_start < 0) { |
| 1004 | paintlen += x_start; |
| 1005 | x_start = 0; |
| 1006 | } |
| 1007 | if (x_start + paintlen > COLS) |
| 1008 | paintlen = COLS - x_start; |
| 1009 | if (paintlen > 0) { |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1010 | wattron(edit, A_REVERSE); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1011 | assert(x_start >= 0 && paintlen > 0 && x_start + paintlen <= COLS); |
| 1012 | mvwaddnstr(edit, yval, x_start, |
| 1013 | fileptr->data + start + x_start, paintlen); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1014 | wattroff(edit, A_REVERSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1015 | } |
Chris Allegretta | 08893e0 | 2001-11-29 02:42:27 +0000 | [diff] [blame] | 1016 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1017 | #endif /* !NANO_SMALL */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1018 | } |
| 1019 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1020 | /* Just update one line in the edit buffer. Basically a wrapper for |
| 1021 | * edit_add(). If fileptr != current, then index is considered 0. |
| 1022 | * The line will be displayed starting with fileptr->data[index]. |
| 1023 | * Likely args are current_x or 0. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1024 | void update_line(filestruct *fileptr, int index) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1025 | { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1026 | int line; |
| 1027 | /* line in the edit window for CURSES calls */ |
| 1028 | #ifndef NANO_SMALL |
| 1029 | int virt_cur_x; |
| 1030 | int virt_mark_beginx; |
| 1031 | #endif |
| 1032 | char *original; |
| 1033 | /* The original string fileptr->data. */ |
| 1034 | char *converted; |
| 1035 | /* fileptr->data converted to have tabs and control characters |
| 1036 | * expanded. */ |
| 1037 | size_t pos; |
| 1038 | size_t page_start; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1039 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1040 | if (fileptr == NULL) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1041 | return; |
Robert Siemborski | 53154a7 | 2000-06-18 00:11:03 +0000 | [diff] [blame] | 1042 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1043 | line = fileptr->lineno - edittop->lineno; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1044 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1045 | /* We assume the line numbers are valid. Is that really true? */ |
| 1046 | assert(line < 0 || line == check_linenumbers(fileptr)); |
| 1047 | |
| 1048 | if (line < 0 || line >= editwinrows) |
| 1049 | return; |
| 1050 | |
| 1051 | /* First, blank out the line (at a minimum) */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1052 | mvwaddstr(edit, line, 0, hblank); |
| 1053 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1054 | original = fileptr->data; |
| 1055 | converted = charalloc(strlenpt(original) + 1); |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1056 | |
| 1057 | /* Next, convert all the tabs to spaces, so everything else is easy. |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1058 | * Note the internal speller sends us index == -1. */ |
| 1059 | index = fileptr == current && index > 0 ? strnlenpt(original, index) : 0; |
| 1060 | #ifndef NANO_SMALL |
| 1061 | virt_cur_x = fileptr == current ? strnlenpt(original, current_x) : current_x; |
| 1062 | virt_mark_beginx = fileptr == mark_beginbuf ? strnlenpt(original, mark_beginx) : mark_beginx; |
| 1063 | #endif |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 1064 | |
| 1065 | pos = 0; |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1066 | for (; *original != '\0'; original++) { |
| 1067 | if (*original == '\t') |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 1068 | do { |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1069 | converted[pos++] = ' '; |
Chris Allegretta | 6d690a3 | 2000-08-03 22:51:21 +0000 | [diff] [blame] | 1070 | } while (pos % tabsize); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1071 | else if (is_cntrl_char(*original)) { |
| 1072 | converted[pos++] = '^'; |
| 1073 | if (*original == 127) |
| 1074 | converted[pos++] = '?'; |
| 1075 | else if (*original == '\n') |
| 1076 | /* Treat newlines (ASCII 10's) embedded in a line as encoded |
| 1077 | * nulls (ASCII 0's); the line in question should be run |
| 1078 | * through unsunder() before reaching here */ |
| 1079 | converted[pos++] = '@'; |
| 1080 | else |
| 1081 | converted[pos++] = *original + 64; |
| 1082 | } else |
| 1083 | converted[pos++] = *original; |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 1084 | } |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1085 | converted[pos] = '\0'; |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 1086 | |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 1087 | /* Now, paint the line */ |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1088 | original = fileptr->data; |
| 1089 | fileptr->data = converted; |
| 1090 | page_start = get_page_start(index); |
| 1091 | edit_add(fileptr, line, page_start |
| 1092 | #ifndef NANO_SMALL |
| 1093 | , virt_mark_beginx, virt_cur_x |
| 1094 | #endif |
| 1095 | ); |
| 1096 | free(converted); |
| 1097 | fileptr->data = original; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1098 | |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1099 | if (page_start > 0) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1100 | mvwaddch(edit, line, 0, '$'); |
David Lawrence Ramsey | 1f28b8f | 2002-09-27 14:21:59 +0000 | [diff] [blame] | 1101 | if (pos > page_start + COLS) |
| 1102 | mvwaddch(edit, line, COLS - 1, '$'); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1103 | } |
| 1104 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1105 | /* This function updates current, based on where current_y is; |
| 1106 | * reset_cursor() does the opposite. */ |
| 1107 | void update_cursor(void) |
| 1108 | { |
| 1109 | int i = 0; |
| 1110 | |
| 1111 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 1112 | 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] | 1113 | current_x); |
| 1114 | #endif |
| 1115 | |
| 1116 | current = edittop; |
| 1117 | while (i < current_y && current->next != NULL) { |
| 1118 | current = current->next; |
| 1119 | i++; |
| 1120 | } |
| 1121 | |
| 1122 | #ifdef DEBUG |
Chris Allegretta | 0e86e60 | 2003-01-23 04:27:23 +0000 | [diff] [blame] | 1123 | fprintf(stderr, "current->data = \"%s\"\n", current->data); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1124 | #endif |
| 1125 | } |
| 1126 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1127 | void center_cursor(void) |
| 1128 | { |
| 1129 | current_y = editwinrows / 2; |
| 1130 | wmove(edit, current_y, current_x); |
| 1131 | } |
| 1132 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1133 | /* Refresh the screen without changing the position of lines. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1134 | void edit_refresh(void) |
| 1135 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1136 | /* Neither of these conditions should occur, but they do. edittop is |
| 1137 | * NULL when you open an existing file on the command line, and |
| 1138 | * ENABLE_COLOR is defined. Yuck. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1139 | if (current == NULL) |
| 1140 | return; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1141 | if (edittop == NULL) |
| 1142 | edittop = current; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1143 | |
Chris Allegretta | 63d0b48 | 2003-01-26 19:47:10 +0000 | [diff] [blame] | 1144 | if (current->lineno < edittop->lineno || |
| 1145 | current->lineno >= edittop->lineno + editwinrows) |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1146 | /* Note that edit_update() changes edittop so that |
| 1147 | * current->lineno = edittop->lineno + editwinrows / 2. Thus |
| 1148 | * when it then calls edit_refresh(), there is no danger of |
| 1149 | * getting an infinite loop. */ |
Chris Allegretta | da721be | 2000-07-31 01:26:42 +0000 | [diff] [blame] | 1150 | edit_update(current, CENTER); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1151 | else { |
| 1152 | int nlines = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1153 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1154 | /* Don't make the cursor jump around the screen whilst updating */ |
| 1155 | leaveok(edit, TRUE); |
| 1156 | |
| 1157 | editbot = edittop; |
| 1158 | while (nlines < editwinrows) { |
| 1159 | update_line(editbot, current_x); |
| 1160 | nlines++; |
| 1161 | if (editbot->next == NULL) |
| 1162 | break; |
| 1163 | editbot = editbot->next; |
| 1164 | } |
| 1165 | while (nlines < editwinrows) { |
| 1166 | mvwaddstr(edit, nlines, 0, hblank); |
| 1167 | nlines++; |
| 1168 | } |
| 1169 | /* What the hell are we expecting to update the screen if this |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1170 | isn't here? Luck? */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1171 | wrefresh(edit); |
| 1172 | leaveok(edit, FALSE); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1173 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1174 | } |
| 1175 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1176 | /* Same as above, but touch the window first, so everything is |
| 1177 | * redrawn. */ |
Chris Allegretta | f1d33d3 | 2000-08-19 03:53:39 +0000 | [diff] [blame] | 1178 | void edit_refresh_clearok(void) |
| 1179 | { |
| 1180 | clearok(edit, TRUE); |
| 1181 | edit_refresh(); |
| 1182 | clearok(edit, FALSE); |
| 1183 | } |
| 1184 | |
| 1185 | /* |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1186 | * Nice generic routine to update the edit buffer, given a pointer to the |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1187 | * file struct =) |
| 1188 | */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1189 | void edit_update(filestruct *fileptr, topmidbotnone location) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1190 | { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1191 | if (fileptr == NULL) |
| 1192 | return; |
| 1193 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1194 | if (location != TOP) { |
| 1195 | int goal = location == NONE ? current_y - 1 : editwinrows / 2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1196 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1197 | for (; goal >= 0 && fileptr->prev != NULL; goal--) |
| 1198 | fileptr = fileptr->prev; |
| 1199 | } |
| 1200 | edittop = fileptr; |
Robert Siemborski | 29e9a76 | 2000-07-05 03:16:04 +0000 | [diff] [blame] | 1201 | fix_editbot(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1202 | |
| 1203 | edit_refresh(); |
| 1204 | } |
| 1205 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1206 | /* |
| 1207 | * Ask a question on the statusbar. Answer will be stored in answer |
| 1208 | * 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] | 1209 | * otherwise, the valid shortcut key caught. Def is any editable text we |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1210 | * want to put up by default. |
Chris Allegretta | 7da4e9f | 2000-11-06 02:57:22 +0000 | [diff] [blame] | 1211 | * |
| 1212 | * New arg tabs tells whether or not to allow tab completion. |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1213 | */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1214 | int statusq(int tabs, const shortcut *s, const char *def, |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1215 | #ifndef NANO_SMALL |
| 1216 | historyheadtype *which_history, |
| 1217 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1218 | const char *msg, ...) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1219 | { |
| 1220 | va_list ap; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1221 | char *foo = charalloc(COLS - 3); |
Chris Allegretta | 9caa193 | 2002-02-15 20:08:05 +0000 | [diff] [blame] | 1222 | int ret; |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 1223 | #ifndef DISABLE_TABCOMP |
Chris Allegretta | a16e4e9 | 2002-01-05 18:59:54 +0000 | [diff] [blame] | 1224 | int list = 0; |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 1225 | #endif |
| 1226 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1227 | bottombars(s); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1228 | |
| 1229 | va_start(ap, msg); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1230 | vsnprintf(foo, COLS - 4, msg, ap); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1231 | va_end(ap); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1232 | foo[COLS - 4] = '\0'; |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1233 | |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1234 | ret = nanogetstr(tabs, foo, def, |
| 1235 | #ifndef NANO_SMALL |
| 1236 | which_history, |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 1237 | #endif |
Chris Allegretta | 5beed50 | 2003-01-05 20:41:21 +0000 | [diff] [blame] | 1238 | s |
| 1239 | #ifndef DISABLE_TABCOMP |
| 1240 | , &list |
| 1241 | #endif |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1242 | ); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1243 | free(foo); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1244 | resetstatuspos = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1245 | |
| 1246 | switch (ret) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1247 | case NANO_FIRSTLINE_KEY: |
| 1248 | do_first_line(); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1249 | resetstatuspos = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1250 | break; |
| 1251 | case NANO_LASTLINE_KEY: |
| 1252 | do_last_line(); |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1253 | resetstatuspos = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1254 | break; |
| 1255 | case NANO_CANCEL_KEY: |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1256 | ret = -1; |
Chris Allegretta | 65f075d | 2003-02-13 03:03:49 +0000 | [diff] [blame] | 1257 | resetstatuspos = 1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1258 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1259 | } |
Chris Allegretta | a90d0cf | 2003-02-10 02:55:03 +0000 | [diff] [blame] | 1260 | blank_statusbar(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1261 | |
| 1262 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 1263 | fprintf(stderr, "I got \"%s\"\n", answer); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1264 | #endif |
| 1265 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1266 | #ifndef DISABLE_TABCOMP |
| 1267 | /* if we've done tab completion, there might be a list of |
| 1268 | filename matches on the edit window at this point; make sure |
| 1269 | they're cleared off */ |
| 1270 | if (list) |
| 1271 | edit_refresh(); |
| 1272 | #endif |
| 1273 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1274 | return ret; |
| 1275 | } |
| 1276 | |
| 1277 | /* |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1278 | * 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] | 1279 | * for N, 2 for All (if all is nonzero when passed in) and -1 for abort |
| 1280 | * (^C). |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1281 | */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1282 | int do_yesno(int all, int leavecursor, const char *msg, ...) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1283 | { |
| 1284 | va_list ap; |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1285 | char *foo; |
| 1286 | int ok = -2; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1287 | const char *yesstr; /* String of yes characters accepted */ |
| 1288 | const char *nostr; /* Same for no */ |
| 1289 | const char *allstr; /* And all, surprise! */ |
Chris Allegretta | 235ab19 | 2001-04-12 13:24:40 +0000 | [diff] [blame] | 1290 | |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1291 | /* Yes, no and all are strings of any length. Each string consists of |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1292 | all characters accepted as a valid character for that value. |
| 1293 | The first value will be the one displayed in the shortcuts. */ |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1294 | yesstr = _("Yy"); |
| 1295 | nostr = _("Nn"); |
| 1296 | allstr = _("Aa"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1297 | |
Jordi Mallach | 0b0fc49 | 2000-06-23 01:00:13 +0000 | [diff] [blame] | 1298 | /* Remove gettext call for keybindings until we clear the thing up */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1299 | if (!ISSET(NO_HELP)) { |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1300 | char shortstr[3]; /* Temp string for Y, N, A */ |
| 1301 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1302 | /* Write the bottom of the screen */ |
| 1303 | blank_bottombars(); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1304 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1305 | sprintf(shortstr, " %c", yesstr[0]); |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1306 | wmove(bottomwin, 1, 0); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1307 | onekey(shortstr, _("Yes"), 16); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1308 | |
| 1309 | if (all) { |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1310 | wmove(bottomwin, 1, 16); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1311 | shortstr[1] = allstr[0]; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1312 | onekey(shortstr, _("All"), 16); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1313 | } |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1314 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1315 | wmove(bottomwin, 2, 0); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1316 | shortstr[1] = nostr[0]; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1317 | onekey(shortstr, _("No"), 16); |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1318 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1319 | wmove(bottomwin, 2, 16); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1320 | onekey("^C", _("Cancel"), 16); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1321 | } |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1322 | |
| 1323 | foo = charalloc(COLS); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1324 | va_start(ap, msg); |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1325 | vsnprintf(foo, COLS, msg, ap); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1326 | va_end(ap); |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1327 | foo[COLS - 1] = '\0'; |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1328 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1329 | wattron(bottomwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1330 | |
| 1331 | blank_statusbar(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1332 | mvwaddstr(bottomwin, 0, 0, foo); |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1333 | free(foo); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1334 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1335 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1336 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1337 | wrefresh(bottomwin); |
| 1338 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1339 | do { |
| 1340 | int kbinput = wgetch(edit); |
Chris Allegretta | b2cd248 | 2003-02-12 23:18:19 +0000 | [diff] [blame] | 1341 | #if !defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION) |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1342 | MEVENT mevent; |
Chris Allegretta | 235ab19 | 2001-04-12 13:24:40 +0000 | [diff] [blame] | 1343 | #endif |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1344 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1345 | if (kbinput == NANO_CONTROL_C) |
| 1346 | ok = -1; |
Chris Allegretta | b2cd248 | 2003-02-12 23:18:19 +0000 | [diff] [blame] | 1347 | #if !defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION) |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1348 | /* Look ma! We get to duplicate lots of code from do_mouse!! */ |
| 1349 | else if (kbinput == KEY_MOUSE && getmouse(&mevent) != ERR && |
| 1350 | wenclose(bottomwin, mevent.y, mevent.x) && |
| 1351 | !ISSET(NO_HELP) && mevent.x < 32 && |
| 1352 | mevent.y >= editwinrows + 3) { |
| 1353 | int x = mevent.x /= 16; |
| 1354 | /* Did we click in the first column of shortcuts, or the |
| 1355 | second? */ |
| 1356 | int y = mevent.y - editwinrows - 3; |
| 1357 | /* Did we click in the first row of shortcuts? */ |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1358 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1359 | assert(0 <= x && x <= 1 && 0 <= y && y <= 1); |
| 1360 | /* x = 0 means they clicked Yes or No. |
| 1361 | y = 0 means Yes or All. */ |
| 1362 | ok = -2 * x * y + x - y + 1; |
Chris Allegretta | 4ce8e3b | 2001-02-16 01:49:31 +0000 | [diff] [blame] | 1363 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1364 | if (ok == 2 && !all) |
| 1365 | ok = -2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1366 | } |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1367 | #endif |
| 1368 | /* Look for the kbinput in the yes, no and (optionally) all str */ |
| 1369 | else if (strchr(yesstr, kbinput) != NULL) |
| 1370 | ok = 1; |
| 1371 | else if (strchr(nostr, kbinput) != NULL) |
| 1372 | ok = 0; |
| 1373 | else if (all && strchr(allstr, kbinput) != NULL) |
| 1374 | ok = 2; |
| 1375 | } while (ok == -2); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1376 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1377 | /* Then blank the statusbar. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1378 | blank_statusbar_refresh(); |
| 1379 | |
Chris Allegretta | db28e96 | 2003-01-28 01:23:40 +0000 | [diff] [blame] | 1380 | return ok; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1383 | int total_refresh(void) |
| 1384 | { |
| 1385 | clearok(edit, TRUE); |
| 1386 | clearok(topwin, TRUE); |
| 1387 | clearok(bottomwin, TRUE); |
| 1388 | wnoutrefresh(edit); |
| 1389 | wnoutrefresh(topwin); |
| 1390 | wnoutrefresh(bottomwin); |
| 1391 | doupdate(); |
| 1392 | clearok(edit, FALSE); |
| 1393 | clearok(topwin, FALSE); |
| 1394 | clearok(bottomwin, FALSE); |
| 1395 | edit_refresh(); |
| 1396 | titlebar(NULL); |
| 1397 | return 1; |
| 1398 | } |
| 1399 | |
| 1400 | void display_main_list(void) |
| 1401 | { |
| 1402 | bottombars(main_list); |
| 1403 | } |
| 1404 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1405 | void statusbar(const char *msg, ...) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1406 | { |
| 1407 | va_list ap; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1408 | char *foo; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1409 | int start_x = 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1410 | size_t foo_len; |
| 1411 | |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 1412 | va_start(ap, msg); |
| 1413 | |
| 1414 | /* Curses mode is turned off. If we use wmove() now, it will muck up |
| 1415 | the terminal settings. So we just use vfprintf(). */ |
| 1416 | if (curses_ended) { |
| 1417 | vfprintf(stderr, msg, ap); |
| 1418 | va_end(ap); |
| 1419 | return; |
| 1420 | } |
| 1421 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1422 | assert(COLS >= 4); |
| 1423 | foo = charalloc(COLS - 3); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1424 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1425 | vsnprintf(foo, COLS - 3, msg, ap); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1426 | va_end(ap); |
| 1427 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1428 | foo[COLS - 4] = '\0'; |
| 1429 | foo_len = strlen(foo); |
| 1430 | start_x = (COLS - foo_len - 4) / 2; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1431 | |
| 1432 | /* Blank out line */ |
| 1433 | blank_statusbar(); |
| 1434 | |
| 1435 | wmove(bottomwin, 0, start_x); |
| 1436 | |
| 1437 | wattron(bottomwin, A_REVERSE); |
| 1438 | |
| 1439 | waddstr(bottomwin, "[ "); |
| 1440 | waddstr(bottomwin, foo); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1441 | free(foo); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1442 | waddstr(bottomwin, " ]"); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1443 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1444 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | 8ce2413 | 2001-04-30 11:28:46 +0000 | [diff] [blame] | 1445 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1446 | wrefresh(bottomwin); |
| 1447 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1448 | SET(DISABLE_CURPOS); |
| 1449 | statblank = 26; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1450 | } |
| 1451 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1452 | /* |
| 1453 | * If constant is false, the user typed ^C so we unconditionally display |
| 1454 | * the cursor position. Otherwise, we display it only if the character |
| 1455 | * position changed, and DISABLE_CURPOS is not set. |
| 1456 | * |
| 1457 | * If constant and DISABLE_CURPOS is set, we unset it and update old_i and |
| 1458 | * old_totsize. That way, we leave the current statusbar alone, but next |
| 1459 | * time we will display. */ |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 1460 | int do_cursorpos(int constant) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1461 | { |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1462 | const filestruct *fileptr; |
| 1463 | unsigned long i = 0; |
| 1464 | static unsigned long old_i = 0; |
| 1465 | static long old_totsize = -1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1466 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1467 | assert(current != NULL && fileage != NULL && totlines != 0); |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 1468 | |
| 1469 | if (old_totsize == -1) |
| 1470 | old_totsize = totsize; |
| 1471 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1472 | for (fileptr = fileage; fileptr != current; fileptr = fileptr->next) { |
| 1473 | assert(fileptr != NULL); |
Chris Allegretta | f27c697 | 2002-02-12 01:57:24 +0000 | [diff] [blame] | 1474 | i += strlen(fileptr->data) + 1; |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1475 | } |
Chris Allegretta | f27c697 | 2002-02-12 01:57:24 +0000 | [diff] [blame] | 1476 | i += current_x; |
Chris Allegretta | 14b3ca9 | 2002-01-25 21:59:02 +0000 | [diff] [blame] | 1477 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1478 | if (constant && ISSET(DISABLE_CURPOS)) { |
| 1479 | UNSET(DISABLE_CURPOS); |
| 1480 | old_i = i; |
| 1481 | old_totsize = totsize; |
| 1482 | return 0; |
| 1483 | } |
Chris Allegretta | 14b3ca9 | 2002-01-25 21:59:02 +0000 | [diff] [blame] | 1484 | |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1485 | /* if constant is false, display the position on the statusbar |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 1486 | unconditionally; otherwise, only display the position when the |
| 1487 | character values have changed */ |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1488 | if (!constant || old_i != i || old_totsize != totsize) { |
| 1489 | unsigned long xpt = xplustabs() + 1; |
| 1490 | unsigned long cur_len = strlenpt(current->data) + 1; |
| 1491 | int linepct = 100 * current->lineno / totlines; |
| 1492 | int colpct = 100 * xpt / cur_len; |
| 1493 | int bytepct = totsize == 0 ? 0 : 100 * i / totsize; |
| 1494 | |
| 1495 | statusbar( |
| 1496 | _("line %ld/%ld (%d%%), col %lu/%lu (%d%%), char %lu/%ld (%d%%)"), |
| 1497 | current->lineno, totlines, linepct, |
| 1498 | xpt, cur_len, colpct, |
| 1499 | i, totsize, bytepct); |
| 1500 | UNSET(DISABLE_CURPOS); |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
| 1503 | old_i = i; |
| 1504 | old_totsize = totsize; |
| 1505 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1506 | reset_cursor(); |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 1507 | return 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1508 | } |
| 1509 | |
Chris Allegretta | 2084acc | 2001-11-29 03:43:08 +0000 | [diff] [blame] | 1510 | int do_cursorpos_void(void) |
| 1511 | { |
| 1512 | return do_cursorpos(0); |
| 1513 | } |
| 1514 | |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 1515 | /* Calculate the next line of help_text, starting at ptr. */ |
| 1516 | int line_len(const char *ptr) |
| 1517 | { |
| 1518 | int j = 0; |
| 1519 | |
| 1520 | while (*ptr != '\n' && *ptr != '\0' && j < COLS - 5) { |
| 1521 | ptr++; |
| 1522 | j++; |
| 1523 | } |
| 1524 | if (j == COLS - 5) { |
| 1525 | /* Don't wrap at the first of two spaces following a period. */ |
| 1526 | if (*ptr == ' ' && *(ptr + 1) == ' ') |
| 1527 | j++; |
| 1528 | /* Don't print half a word if we've run out of space */ |
| 1529 | while (*ptr != ' ' && j > 0) { |
| 1530 | ptr--; |
| 1531 | j--; |
| 1532 | } |
| 1533 | /* Word longer than COLS - 5 chars just gets broken */ |
| 1534 | if (j == 0) |
| 1535 | j = COLS - 5; |
| 1536 | } |
| 1537 | assert(j >= 0 && j <= COLS - 4 && (j > 0 || *ptr == '\n')); |
| 1538 | return j; |
| 1539 | } |
| 1540 | |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1541 | /* Our shortcut-list-compliant help function, which is |
| 1542 | * better than nothing, and dynamic! */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1543 | int do_help(void) |
| 1544 | { |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 1545 | #ifndef DISABLE_HELP |
Chris Allegretta | 653d614 | 2003-08-04 02:12:03 +0000 | [diff] [blame] | 1546 | int i, page = 0, kbinput = 0, no_more = 0, kp; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1547 | int no_help_flag = 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1548 | const shortcut *oldshortcut; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1549 | |
| 1550 | blank_edit(); |
| 1551 | curs_set(0); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1552 | wattroff(bottomwin, A_REVERSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1553 | blank_statusbar(); |
| 1554 | |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1555 | /* set help_text as the string to display */ |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1556 | help_init(); |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1557 | assert(help_text != NULL); |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1558 | |
| 1559 | oldshortcut = currshortcut; |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1560 | |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1561 | currshortcut = help_list; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 1562 | |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 1563 | kp = keypad_on(edit, 1); |
| 1564 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1565 | if (ISSET(NO_HELP)) { |
| 1566 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1567 | /* Well, if we're going to do this, we should at least |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 1568 | do it the right way */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1569 | no_help_flag = 1; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1570 | UNSET(NO_HELP); |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 1571 | window_init(); |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1572 | bottombars(help_list); |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 1573 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1574 | } else |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1575 | bottombars(help_list); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1576 | |
| 1577 | do { |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1578 | const char *ptr = help_text; |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1579 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1580 | switch (kbinput) { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1581 | #if !defined(DISABLE_MOUSE) && defined(NCURSES_MOUSE_VERSION) |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1582 | case KEY_MOUSE: |
| 1583 | do_mouse(); |
| 1584 | break; |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1585 | #endif |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1586 | case 27: |
| 1587 | kbinput = wgetch(edit); |
| 1588 | switch(kbinput) { |
| 1589 | case '[': |
| 1590 | kbinput = wgetch(edit); |
| 1591 | switch(kbinput) { |
| 1592 | case '5': /* Alt-[-5 = Page Up */ |
| 1593 | wgetch(edit); |
| 1594 | goto do_pageupkey; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1595 | case 'V': /* Alt-[-V = Page Up in Hurd Console */ |
| 1596 | case 'I': /* Alt-[-I = Page Up - FreeBSD Console */ |
| 1597 | goto do_pageupkey; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1598 | case '6': /* Alt-[-6 = Page Down */ |
| 1599 | wgetch(edit); |
| 1600 | goto do_pagedownkey; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1601 | case 'U': /* Alt-[-U = Page Down in Hurd Console */ |
| 1602 | case 'G': /* Alt-[-G = Page Down - FreeBSD Console */ |
| 1603 | goto do_pagedownkey; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1604 | } |
| 1605 | break; |
| 1606 | } |
| 1607 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1608 | case NANO_NEXTPAGE_KEY: |
| 1609 | case NANO_NEXTPAGE_FKEY: |
| 1610 | case KEY_NPAGE: |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1611 | do_pagedownkey: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1612 | if (!no_more) { |
| 1613 | blank_edit(); |
| 1614 | page++; |
| 1615 | } |
| 1616 | break; |
| 1617 | case NANO_PREVPAGE_KEY: |
| 1618 | case NANO_PREVPAGE_FKEY: |
| 1619 | case KEY_PPAGE: |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1620 | do_pageupkey: |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 1621 | if (page > 0) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1622 | no_more = 0; |
| 1623 | blank_edit(); |
| 1624 | page--; |
| 1625 | } |
| 1626 | break; |
| 1627 | } |
| 1628 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1629 | /* Calculate where in the text we should be, based on the page */ |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 1630 | for (i = 1; i < page * (editwinrows - 1); i++) { |
| 1631 | ptr += line_len(ptr); |
| 1632 | if (*ptr == '\n') |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1633 | ptr++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1634 | } |
| 1635 | |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 1636 | for (i = 0; i < editwinrows && *ptr != '\0'; i++) { |
| 1637 | int j = line_len(ptr); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1638 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1639 | mvwaddnstr(edit, i, 0, ptr, j); |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 1640 | ptr += j; |
| 1641 | if (*ptr == '\n') |
| 1642 | ptr++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1643 | } |
Chris Allegretta | 4640fe3 | 2003-02-10 03:10:03 +0000 | [diff] [blame] | 1644 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1645 | if (*ptr == '\0') { |
| 1646 | no_more = 1; |
| 1647 | continue; |
| 1648 | } |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1649 | } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY && |
| 1650 | kbinput != NANO_EXIT_FKEY); |
Chris Allegretta | d1627cf | 2000-12-18 05:03:16 +0000 | [diff] [blame] | 1651 | |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1652 | currshortcut = oldshortcut; |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1653 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1654 | if (no_help_flag) { |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 1655 | blank_bottombars(); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1656 | wrefresh(bottomwin); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1657 | SET(NO_HELP); |
Chris Allegretta | 7044489 | 2001-01-07 23:02:02 +0000 | [diff] [blame] | 1658 | window_init(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1659 | } else |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1660 | bottombars(currshortcut); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1661 | |
| 1662 | curs_set(1); |
| 1663 | edit_refresh(); |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 1664 | kp = keypad_on(edit, kp); |
| 1665 | |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1666 | /* The help_init() at the beginning allocated help_text, which has |
| 1667 | now been written to screen. */ |
| 1668 | free(help_text); |
| 1669 | help_text = NULL; |
| 1670 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1671 | #elif defined(DISABLE_HELP) |
| 1672 | nano_disabled_msg(); |
| 1673 | #endif |
| 1674 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1675 | return 1; |
| 1676 | } |
| 1677 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1678 | int keypad_on(WINDOW *win, int newval) |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 1679 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1680 | /* This is taken right from aumix. Don't sue me. */ |
| 1681 | #ifdef HAVE_USEKEYPAD |
| 1682 | int old = win->_use_keypad; |
| 1683 | keypad(win, newval); |
| 1684 | return old; |
| 1685 | #else |
| 1686 | keypad(win, newval); |
| 1687 | return 1; |
| 1688 | #endif /* HAVE_USEKEYPAD */ |
Robert Siemborski | dd53ec2 | 2000-07-04 02:35:19 +0000 | [diff] [blame] | 1689 | } |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1690 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1691 | /* Highlight the current word being replaced or spell checked. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1692 | void do_replace_highlight(int highlight_flag, const char *word) |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 1693 | { |
| 1694 | char *highlight_word = NULL; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1695 | int x, y, word_len; |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 1696 | |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1697 | highlight_word = |
| 1698 | mallocstrcpy(highlight_word, ¤t->data[current_x]); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1699 | |
| 1700 | #ifdef HAVE_REGEX_H |
| 1701 | if (ISSET(USE_REGEXP)) |
| 1702 | /* if we're using regexps, the highlight is the length of the |
| 1703 | search result, not the length of the regexp string */ |
| 1704 | word_len = regmatches[0].rm_eo - regmatches[0].rm_so; |
| 1705 | else |
| 1706 | #endif |
| 1707 | word_len = strlen(word); |
| 1708 | |
| 1709 | highlight_word[word_len] = '\0'; |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 1710 | |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 1711 | /* adjust output when word extends beyond screen */ |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 1712 | |
| 1713 | x = xplustabs(); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1714 | y = get_page_start(x) + COLS; |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 1715 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1716 | if ((COLS - (y - x) + word_len) > COLS) { |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 1717 | highlight_word[y - x - 1] = '$'; |
| 1718 | highlight_word[y - x] = '\0'; |
| 1719 | } |
| 1720 | |
| 1721 | /* OK display the output */ |
| 1722 | |
| 1723 | reset_cursor(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1724 | |
Chris Allegretta | fb62f73 | 2000-12-05 11:36:41 +0000 | [diff] [blame] | 1725 | if (highlight_flag) |
| 1726 | wattron(edit, A_REVERSE); |
| 1727 | |
| 1728 | waddstr(edit, highlight_word); |
| 1729 | |
| 1730 | if (highlight_flag) |
| 1731 | wattroff(edit, A_REVERSE); |
| 1732 | |
| 1733 | free(highlight_word); |
| 1734 | } |
| 1735 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1736 | /* Fix editbot, based on the assumption that edittop is correct. */ |
| 1737 | void fix_editbot(void) |
| 1738 | { |
| 1739 | int i; |
| 1740 | |
| 1741 | editbot = edittop; |
| 1742 | for (i = 0; i < editwinrows && editbot->next != NULL; i++) |
| 1743 | editbot = editbot->next; |
| 1744 | } |
| 1745 | |
| 1746 | #ifdef DEBUG |
| 1747 | /* Dump the current file structure to stderr */ |
| 1748 | void dump_buffer(const filestruct *inptr) { |
| 1749 | if (inptr == fileage) |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 1750 | fprintf(stderr, "Dumping file buffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1751 | else if (inptr == cutbuffer) |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 1752 | fprintf(stderr, "Dumping cutbuffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1753 | else |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame^] | 1754 | fprintf(stderr, "Dumping a buffer to stderr...\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1755 | |
| 1756 | while (inptr != NULL) { |
| 1757 | fprintf(stderr, "(%d) %s\n", inptr->lineno, inptr->data); |
| 1758 | inptr = inptr->next; |
| 1759 | } |
| 1760 | } |
| 1761 | #endif /* DEBUG */ |
| 1762 | |
| 1763 | #ifdef DEBUG |
| 1764 | void dump_buffer_reverse(void) { |
| 1765 | const filestruct *fileptr = filebot; |
| 1766 | |
| 1767 | while (fileptr != NULL) { |
| 1768 | fprintf(stderr, "(%d) %s\n", fileptr->lineno, fileptr->data); |
| 1769 | fileptr = fileptr->prev; |
| 1770 | } |
| 1771 | } |
| 1772 | #endif /* DEBUG */ |
| 1773 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1774 | #ifdef NANO_EXTRA |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 1775 | #define CREDIT_LEN 53 |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1776 | #define XLCREDIT_LEN 8 |
| 1777 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1778 | void do_credits(void) |
| 1779 | { |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 1780 | int i, j = 0, k, place = 0, start_x; |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1781 | |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1782 | const char *what; |
| 1783 | const char *xlcredits[XLCREDIT_LEN]; |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1784 | |
Chris Allegretta | f717f98 | 2003-02-13 22:25:01 +0000 | [diff] [blame] | 1785 | const char *credits[CREDIT_LEN] = { |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1786 | "0", /* "The nano text editor" */ |
| 1787 | "1", /* "version" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1788 | VERSION, |
| 1789 | "", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1790 | "2", /* "Brought to you by:" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1791 | "Chris Allegretta", |
| 1792 | "Jordi Mallach", |
| 1793 | "Adam Rogoyski", |
| 1794 | "Rob Siemborski", |
| 1795 | "Rocco Corsi", |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 1796 | "David Lawrence Ramsey", |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 1797 | "David Benbennick", |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1798 | "Ken Tyler", |
| 1799 | "Sven Guckes", |
| 1800 | "Florian König", |
| 1801 | "Pauli Virtanen", |
| 1802 | "Daniele Medri", |
| 1803 | "Clement Laforet", |
| 1804 | "Tedi Heriyanto", |
| 1805 | "Bill Soudan", |
| 1806 | "Christian Weisgerber", |
| 1807 | "Erik Andersen", |
| 1808 | "Big Gaute", |
| 1809 | "Joshua Jensen", |
| 1810 | "Ryan Krebs", |
| 1811 | "Albert Chin", |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1812 | "", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1813 | "3", /* "Special thanks to:" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1814 | "Plattsburgh State University", |
| 1815 | "Benet Laboratories", |
| 1816 | "Amy Allegretta", |
| 1817 | "Linda Young", |
| 1818 | "Jeremy Robichaud", |
| 1819 | "Richard Kolb II", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1820 | "4", /* "The Free Software Foundation" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1821 | "Linus Torvalds", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1822 | "5", /* "For ncurses:" */ |
Chris Allegretta | dce44ab | 2002-03-16 01:03:41 +0000 | [diff] [blame] | 1823 | "Thomas Dickey", |
| 1824 | "Pavel Curtis", |
| 1825 | "Zeyd Ben-Halim", |
| 1826 | "Eric S. Raymond", |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1827 | "6", /* "and anyone else we forgot..." */ |
| 1828 | "7", /* "Thank you for using nano!\n" */ |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1829 | "", "", "", "", |
| 1830 | "(c) 1999-2002 Chris Allegretta", |
| 1831 | "", "", "", "", |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 1832 | "http://www.nano-editor.org/" |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1833 | }; |
| 1834 | |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1835 | xlcredits[0] = _("The nano text editor"); |
| 1836 | xlcredits[1] = _("version "); |
| 1837 | xlcredits[2] = _("Brought to you by:"); |
| 1838 | xlcredits[3] = _("Special thanks to:"); |
| 1839 | xlcredits[4] = _("The Free Software Foundation"); |
| 1840 | xlcredits[5] = _("For ncurses:"); |
| 1841 | xlcredits[6] = _("and anyone else we forgot..."); |
| 1842 | xlcredits[7] = _("Thank you for using nano!\n"); |
| 1843 | |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1844 | curs_set(0); |
| 1845 | nodelay(edit, TRUE); |
| 1846 | blank_bottombars(); |
| 1847 | mvwaddstr(topwin, 0, 0, hblank); |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 1848 | blank_edit(); |
| 1849 | wrefresh(edit); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1850 | wrefresh(bottomwin); |
| 1851 | wrefresh(topwin); |
| 1852 | |
| 1853 | while (wgetch(edit) == ERR) { |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 1854 | for (k = 0; k <= 1; k++) { |
| 1855 | blank_edit(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1856 | for (i = editwinrows / 2 - 1; i >= (editwinrows / 2 - 1 - j); |
| 1857 | i--) { |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 1858 | mvwaddstr(edit, i * 2 - k, 0, hblank); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1859 | |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1860 | if (place - (editwinrows / 2 - 1 - i) < CREDIT_LEN) { |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 1861 | what = credits[place - (editwinrows / 2 - 1 - i)]; |
Chris Allegretta | c4e3d9e | 2002-07-21 15:44:13 +0000 | [diff] [blame] | 1862 | |
| 1863 | /* God I've missed hacking. If what is exactly |
| 1864 | 1 char long, it's a sentinel for a translated |
| 1865 | string, so use that instead. This means no |
| 1866 | thanking people with 1 character long names ;-) */ |
| 1867 | if (strlen(what) == 1) |
| 1868 | what = xlcredits[atoi(what)]; |
| 1869 | } else |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 1870 | what = ""; |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1871 | |
Chris Allegretta | 17dcb72 | 2001-01-20 21:40:07 +0000 | [diff] [blame] | 1872 | start_x = COLS / 2 - strlen(what) / 2 - 1; |
Chris Allegretta | 8b4ca4a | 2000-11-25 18:21:37 +0000 | [diff] [blame] | 1873 | mvwaddstr(edit, i * 2 - k, start_x, what); |
| 1874 | } |
| 1875 | usleep(700000); |
| 1876 | wrefresh(edit); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1877 | } |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1878 | if (j < editwinrows / 2 - 1) |
| 1879 | j++; |
| 1880 | |
| 1881 | place++; |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1882 | |
| 1883 | if (place >= CREDIT_LEN + editwinrows / 2) |
| 1884 | break; |
| 1885 | } |
| 1886 | |
| 1887 | nodelay(edit, FALSE); |
| 1888 | curs_set(1); |
| 1889 | display_main_list(); |
| 1890 | total_refresh(); |
Chris Allegretta | 598106e | 2002-01-19 01:59:37 +0000 | [diff] [blame] | 1891 | } |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1892 | #endif |