Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1 | /************************************************************************** |
| 2 | * winio.c * |
| 3 | * * |
| 4 | * Copyright (C) 1999 Chris Allegretta * |
| 5 | * This program is free software; you can redistribute it and/or modify * |
| 6 | * it under the terms of the GNU General Public License as published by * |
| 7 | * the Free Software Foundation; either version 1, or (at your option) * |
| 8 | * any later version. * |
| 9 | * * |
| 10 | * This program is distributed in the hope that it will be useful, * |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 13 | * GNU General Public License for more details. * |
| 14 | * * |
| 15 | * You should have received a copy of the GNU General Public License * |
| 16 | * along with this program; if not, write to the Free Software * |
| 17 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 18 | * * |
| 19 | **************************************************************************/ |
| 20 | |
| 21 | #include <stdarg.h> |
| 22 | #include <string.h> |
| 23 | #include "config.h" |
| 24 | #include "proto.h" |
| 25 | #include "nano.h" |
| 26 | |
| 27 | #ifndef NANO_SMALL |
| 28 | #include <libintl.h> |
| 29 | #define _(string) gettext(string) |
| 30 | #else |
| 31 | #define _(string) (string) |
| 32 | #endif |
| 33 | |
| 34 | static int statblank = 0; /* Number of keystrokes left after |
| 35 | we call statubar() before we |
| 36 | actually blank the statusbar */ |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 37 | |
| 38 | /* Local Function Prototypes for only winio.c */ |
| 39 | inline int get_page_from_virtual(int virtual); |
| 40 | inline int get_page_start_virtual(int page); |
| 41 | inline int get_page_end_virtual(int page); |
| 42 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 43 | /* Window I/O */ |
| 44 | |
| 45 | int do_first_line(void) |
| 46 | { |
| 47 | current = fileage; |
| 48 | placewewant = 0; |
| 49 | current_x = 0; |
| 50 | edit_update(current); |
| 51 | return 1; |
| 52 | } |
| 53 | |
| 54 | int do_last_line(void) |
| 55 | { |
| 56 | current = filebot; |
| 57 | placewewant = 0; |
| 58 | current_x = 0; |
| 59 | edit_update(current); |
| 60 | return 1; |
| 61 | } |
| 62 | |
| 63 | /* Like xplustabs, but for a specifc index of a speficific filestruct */ |
| 64 | int xpt(filestruct * fileptr, int index) |
| 65 | { |
| 66 | int i, tabs = 0; |
| 67 | |
| 68 | if (fileptr == NULL || fileptr->data == NULL) |
| 69 | return 0; |
| 70 | |
| 71 | for (i = 0; i < index && fileptr->data[i] != 0; i++) { |
| 72 | tabs++; |
| 73 | |
| 74 | if (fileptr->data[i] == NANO_CONTROL_I) { |
Chris Allegretta | 6724a7e | 2000-06-19 23:19:07 +0000 | [diff] [blame] | 75 | if (tabs % TABSIZE == 0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 76 | else |
Chris Allegretta | 6724a7e | 2000-06-19 23:19:07 +0000 | [diff] [blame] | 77 | tabs += TABSIZE - (tabs % TABSIZE); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 78 | } else if (fileptr->data[i] & 0x80) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 79 | /* Make 8 bit chars only 1 collumn! */ |
| 80 | ; |
| 81 | else if (fileptr->data[i] < 32) |
| 82 | tabs++; |
| 83 | } |
| 84 | |
| 85 | return tabs; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | /* Return the actual place on the screen of current->data[current_x], which |
| 90 | should always be > current_x */ |
| 91 | int xplustabs(void) |
| 92 | { |
| 93 | return xpt(current, current_x); |
| 94 | } |
| 95 | |
| 96 | |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 97 | /* Return what current_x should be, given xplustabs() for the line, |
| 98 | * given a start position in the filestruct's data */ |
| 99 | int actual_x_from_start(filestruct * fileptr, int xplus, int start) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 100 | { |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 101 | int i, tot = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 102 | |
| 103 | if (fileptr == NULL || fileptr->data == NULL) |
| 104 | return 0; |
| 105 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 106 | for (i = start; tot <= xplus && fileptr->data[i] != 0; i++, tot++) |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 107 | if (fileptr->data[i] == NANO_CONTROL_I) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 108 | if (tot % TABSIZE == 0) |
| 109 | tot++; |
| 110 | else |
| 111 | tot += TABSIZE - (tot % TABSIZE); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 112 | } else if (fileptr->data[i] & 0x80) |
| 113 | tot++; /* Make 8 bit chars only 1 column (again) */ |
| 114 | else if (fileptr->data[i] < 32) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 115 | tot += 2; |
| 116 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 117 | #ifdef DEBUG |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 118 | fprintf(stderr, _("actual_x_from_start for xplus=%d returned %d\n"), |
| 119 | xplus, i); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 120 | #endif |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 121 | return i - start; |
| 122 | } |
| 123 | |
| 124 | /* Opposite of xplustabs */ |
| 125 | inline int actual_x(filestruct * fileptr, int xplus) |
| 126 | { |
| 127 | return actual_x_from_start(fileptr, xplus, 0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 128 | } |
| 129 | |
| 130 | /* a strlen with tabs factored in, similar to xplustabs() */ |
| 131 | int strlenpt(char *buf) |
| 132 | { |
| 133 | int i, tabs = 0; |
| 134 | |
| 135 | if (buf == NULL) |
| 136 | return 0; |
| 137 | |
| 138 | for (i = 0; buf[i] != 0; i++) { |
| 139 | tabs++; |
| 140 | |
| 141 | if (buf[i] == NANO_CONTROL_I) { |
Chris Allegretta | 6724a7e | 2000-06-19 23:19:07 +0000 | [diff] [blame] | 142 | if (tabs % TABSIZE == 0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 143 | else |
Chris Allegretta | 6724a7e | 2000-06-19 23:19:07 +0000 | [diff] [blame] | 144 | tabs += TABSIZE - (tabs % TABSIZE); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 145 | } else if (buf[i] & 0x80) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 146 | /* Make 8 bit chars only 1 collumn! */ |
| 147 | ; |
| 148 | else if (buf[i] < 32) |
| 149 | tabs++; |
| 150 | } |
| 151 | |
| 152 | return tabs; |
| 153 | } |
| 154 | |
| 155 | |
| 156 | /* resets current_y based on the position of current and puts the cursor at |
| 157 | (current_y, current_x) */ |
| 158 | void reset_cursor(void) |
| 159 | { |
| 160 | filestruct *ptr = edittop; |
| 161 | int x; |
| 162 | |
| 163 | current_y = 0; |
| 164 | |
| 165 | while (ptr != current && ptr != editbot && ptr->next != NULL) { |
| 166 | ptr = ptr->next; |
| 167 | current_y++; |
| 168 | } |
| 169 | |
| 170 | x = xplustabs(); |
| 171 | if (x <= COLS - 2) |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 172 | wmove(edit, current_y, x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 173 | else |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 174 | wmove(edit, current_y, x - |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 175 | get_page_start_virtual(get_page_from_virtual(x))); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 176 | |
| 177 | } |
| 178 | |
| 179 | void blank_bottombars(void) |
| 180 | { |
| 181 | int i = no_help()? 3 : 1; |
| 182 | |
| 183 | for (; i <= 2; i++) |
| 184 | mvwaddstr(bottomwin, i, 0, hblank); |
| 185 | |
| 186 | } |
| 187 | |
| 188 | void blank_edit(void) |
| 189 | { |
| 190 | int i; |
| 191 | for (i = 0; i <= editwinrows - 1; i++) |
| 192 | mvwaddstr(edit, i, 0, hblank); |
| 193 | wrefresh(edit); |
| 194 | } |
| 195 | |
| 196 | |
| 197 | void blank_statusbar(void) |
| 198 | { |
| 199 | mvwaddstr(bottomwin, 0, 0, hblank); |
| 200 | } |
| 201 | |
| 202 | void blank_statusbar_refresh(void) |
| 203 | { |
| 204 | blank_statusbar(); |
| 205 | wrefresh(bottomwin); |
| 206 | } |
| 207 | |
| 208 | void check_statblank(void) |
| 209 | { |
| 210 | |
| 211 | if (statblank > 1) |
| 212 | statblank--; |
| 213 | else if (statblank == 1 && !ISSET(CONSTUPDATE)) { |
| 214 | statblank--; |
| 215 | blank_statusbar_refresh(); |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | /* Get the input from the kb, this should only be called from statusq */ |
| 220 | int nanogetstr(char *buf, char *def, shortcut s[], int slen, int start_x) |
| 221 | { |
| 222 | int kbinput = 0, j = 0, x = 0, xend; |
| 223 | int x_left = 0; |
| 224 | char inputstr[132], inputbuf[132] = ""; |
| 225 | |
| 226 | blank_statusbar(); |
| 227 | mvwaddstr(bottomwin, 0, 0, buf); |
| 228 | if (strlen(def) > 0) |
| 229 | waddstr(bottomwin, def); |
| 230 | wrefresh(bottomwin); |
| 231 | |
| 232 | x_left = strlen(buf); |
| 233 | x = strlen(def) + x_left; |
| 234 | |
| 235 | /* Get the input! */ |
| 236 | if (strlen(def) > 0) { |
| 237 | strcpy(answer, def); |
| 238 | strcpy(inputbuf, def); |
| 239 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 240 | |
| 241 | while ((kbinput = wgetch(bottomwin)) != 13) { |
| 242 | for (j = 0; j <= slen - 1; j++) { |
| 243 | if (kbinput == s[j].val) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 244 | strcpy(answer, ""); |
| 245 | return s[j].val; |
| 246 | } |
| 247 | } |
| 248 | xend = strlen(buf) + strlen(inputbuf); |
| 249 | |
| 250 | switch (kbinput) { |
| 251 | case KEY_HOME: |
| 252 | x = x_left; |
| 253 | blank_statusbar(); |
| 254 | mvwaddstr(bottomwin, 0, 0, buf); |
| 255 | waddstr(bottomwin, inputbuf); |
| 256 | wmove(bottomwin, 0, x); |
| 257 | break; |
| 258 | case KEY_END: |
| 259 | x = x_left + strlen(inputbuf); |
| 260 | blank_statusbar(); |
| 261 | mvwaddstr(bottomwin, 0, 0, buf); |
| 262 | waddstr(bottomwin, inputbuf); |
| 263 | wmove(bottomwin, 0, x); |
| 264 | break; |
| 265 | case KEY_RIGHT: |
| 266 | |
| 267 | if (x < xend) |
| 268 | x++; |
| 269 | wmove(bottomwin, 0, x); |
| 270 | break; |
| 271 | case NANO_CONTROL_D: |
| 272 | if (strlen(inputbuf) > 0 && (x - x_left) != strlen(inputbuf)) { |
| 273 | memmove(inputbuf + (x - x_left), |
| 274 | inputbuf + (x - x_left) + 1, |
| 275 | strlen(inputbuf) - (x - x_left) - 1); |
| 276 | inputbuf[strlen(inputbuf) - 1] = 0; |
| 277 | } |
| 278 | blank_statusbar(); |
| 279 | mvwaddstr(bottomwin, 0, 0, buf); |
| 280 | waddstr(bottomwin, inputbuf); |
| 281 | wmove(bottomwin, 0, x); |
| 282 | break; |
| 283 | case NANO_CONTROL_K: |
| 284 | case NANO_CONTROL_U: |
| 285 | *inputbuf = 0; |
| 286 | x = x_left; |
| 287 | blank_statusbar(); |
| 288 | mvwaddstr(bottomwin, 0, 0, buf); |
| 289 | waddstr(bottomwin, inputbuf); |
| 290 | wmove(bottomwin, 0, x); |
| 291 | break; |
| 292 | case KEY_BACKSPACE: |
| 293 | case KEY_DC: |
| 294 | case 127: |
| 295 | case NANO_CONTROL_H: |
| 296 | if (strlen(inputbuf) > 0) { |
| 297 | if (x == (x_left + strlen(inputbuf))) |
| 298 | inputbuf[strlen(inputbuf) - 1] = 0; |
| 299 | else if (x - x_left) { |
| 300 | memmove(inputbuf + (x - x_left) - 1, |
| 301 | inputbuf + (x - x_left), |
| 302 | strlen(inputbuf) - (x - x_left)); |
| 303 | inputbuf[strlen(inputbuf) - 1] = 0; |
| 304 | } |
| 305 | } |
| 306 | blank_statusbar(); |
| 307 | mvwaddstr(bottomwin, 0, 0, buf); |
| 308 | waddstr(bottomwin, inputbuf); |
| 309 | case KEY_LEFT: |
| 310 | if (x > strlen(buf)) |
| 311 | x--; |
| 312 | wmove(bottomwin, 0, x); |
| 313 | break; |
| 314 | case KEY_UP: |
| 315 | case KEY_DOWN: |
| 316 | break; |
| 317 | |
| 318 | case 27: |
| 319 | switch (kbinput = wgetch(edit)) { |
| 320 | case 79: |
| 321 | switch (kbinput = wgetch(edit)) { |
| 322 | case 70: |
| 323 | x = x_left + strlen(inputbuf); |
| 324 | blank_statusbar(); |
| 325 | mvwaddstr(bottomwin, 0, 0, buf); |
| 326 | waddstr(bottomwin, inputbuf); |
| 327 | wmove(bottomwin, 0, x); |
| 328 | break; |
| 329 | case 72: |
| 330 | x = x_left; |
| 331 | blank_statusbar(); |
| 332 | mvwaddstr(bottomwin, 0, 0, buf); |
| 333 | waddstr(bottomwin, inputbuf); |
| 334 | wmove(bottomwin, 0, x); |
| 335 | break; |
| 336 | } |
| 337 | break; |
| 338 | case 91: |
| 339 | switch (kbinput = wgetch(edit)) { |
| 340 | case 'C': |
| 341 | if (x < xend) |
| 342 | x++; |
| 343 | wmove(bottomwin, 0, x); |
| 344 | break; |
| 345 | case 'D': |
| 346 | if (x > strlen(buf)) |
| 347 | x--; |
| 348 | wmove(bottomwin, 0, x); |
| 349 | break; |
| 350 | case 49: |
| 351 | x = x_left; |
| 352 | blank_statusbar(); |
| 353 | mvwaddstr(bottomwin, 0, 0, buf); |
| 354 | waddstr(bottomwin, inputbuf); |
| 355 | wmove(bottomwin, 0, x); |
| 356 | goto skip_126; |
| 357 | case 51: |
| 358 | if (strlen(inputbuf) > 0 |
| 359 | && (x - x_left) != strlen(inputbuf)) { |
| 360 | memmove(inputbuf + (x - x_left), |
| 361 | inputbuf + (x - x_left) + 1, |
| 362 | strlen(inputbuf) - (x - x_left) - 1); |
| 363 | inputbuf[strlen(inputbuf) - 1] = 0; |
| 364 | } |
| 365 | blank_statusbar(); |
| 366 | mvwaddstr(bottomwin, 0, 0, buf); |
| 367 | waddstr(bottomwin, inputbuf); |
| 368 | wmove(bottomwin, 0, x); |
| 369 | goto skip_126; |
| 370 | case 52: |
| 371 | x = x_left + strlen(inputbuf); |
| 372 | blank_statusbar(); |
| 373 | mvwaddstr(bottomwin, 0, 0, buf); |
| 374 | waddstr(bottomwin, inputbuf); |
| 375 | wmove(bottomwin, 0, x); |
| 376 | goto skip_126; |
| 377 | skip_126: |
| 378 | nodelay(edit, TRUE); |
| 379 | kbinput = wgetch(edit); |
| 380 | if (kbinput == 126 || kbinput == ERR) |
| 381 | kbinput = -1; |
| 382 | nodelay(edit, FALSE); |
| 383 | break; |
| 384 | } |
| 385 | } |
| 386 | blank_statusbar(); |
| 387 | mvwaddstr(bottomwin, 0, 0, buf); |
| 388 | waddstr(bottomwin, inputbuf); |
| 389 | wmove(bottomwin, 0, x); |
| 390 | break; |
| 391 | |
| 392 | |
| 393 | default: |
| 394 | if (kbinput < 32) |
| 395 | break; |
| 396 | strcpy(inputstr, inputbuf); |
| 397 | inputstr[x - strlen(buf)] = kbinput; |
| 398 | strcpy(&inputstr[x - strlen(buf) + 1], |
| 399 | &inputbuf[x - strlen(buf)]); |
| 400 | strcpy(inputbuf, inputstr); |
| 401 | x++; |
| 402 | |
| 403 | mvwaddstr(bottomwin, 0, 0, buf); |
| 404 | waddstr(bottomwin, inputbuf); |
| 405 | wmove(bottomwin, 0, x); |
| 406 | |
| 407 | #ifdef DEBUG |
| 408 | fprintf(stderr, _("input \'%c\' (%d)\n"), kbinput, kbinput); |
| 409 | #endif |
| 410 | } |
| 411 | wrefresh(bottomwin); |
| 412 | } |
| 413 | |
| 414 | strncpy(answer, inputbuf, 132); |
| 415 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 416 | if (!strcmp(answer, "")) |
| 417 | return -2; |
| 418 | else |
| 419 | return 0; |
| 420 | } |
| 421 | |
| 422 | void horizbar(WINDOW * win, int y) |
| 423 | { |
| 424 | wattron(win, A_REVERSE); |
| 425 | mvwaddstr(win, 0, 0, hblank); |
| 426 | wattroff(win, A_REVERSE); |
| 427 | } |
| 428 | |
| 429 | void titlebar(void) |
| 430 | { |
| 431 | int namelen, space; |
| 432 | |
| 433 | horizbar(topwin, 0); |
| 434 | wattron(topwin, A_REVERSE); |
| 435 | mvwaddstr(topwin, 0, 3, VERMSG); |
| 436 | |
| 437 | space = COLS - strlen(VERMSG) - strlen(VERSION) - 21; |
| 438 | |
| 439 | namelen = strlen(filename); |
| 440 | |
| 441 | if (!strcmp(filename, "")) |
| 442 | mvwaddstr(topwin, 0, center_x - 6, _("New Buffer")); |
| 443 | else { |
| 444 | if (namelen > space) { |
| 445 | waddstr(topwin, _(" File: ...")); |
| 446 | waddstr(topwin, &filename[namelen - space]); |
| 447 | } else { |
| 448 | mvwaddstr(topwin, 0, center_x - (namelen / 2 + 1), "File: "); |
| 449 | waddstr(topwin, filename); |
| 450 | } |
| 451 | } |
| 452 | if (ISSET(MODIFIED)) |
| 453 | mvwaddstr(topwin, 0, COLS - 10, _("Modified")); |
| 454 | wattroff(topwin, A_REVERSE); |
| 455 | wrefresh(topwin); |
| 456 | reset_cursor(); |
| 457 | } |
| 458 | |
| 459 | void onekey(char *keystroke, char *desc) |
| 460 | { |
| 461 | char description[80]; |
| 462 | |
| 463 | snprintf(description, 12, " %-11s", desc); |
| 464 | wattron(bottomwin, A_REVERSE); |
| 465 | waddstr(bottomwin, keystroke); |
| 466 | wattroff(bottomwin, A_REVERSE); |
| 467 | waddstr(bottomwin, description); |
| 468 | } |
| 469 | |
| 470 | void clear_bottomwin(void) |
| 471 | { |
| 472 | if (ISSET(NO_HELP)) |
| 473 | return; |
| 474 | |
| 475 | mvwaddstr(bottomwin, 1, 0, hblank); |
| 476 | mvwaddstr(bottomwin, 2, 0, hblank); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 477 | } |
| 478 | |
| 479 | void bottombars(shortcut s[], int slen) |
| 480 | { |
| 481 | int i, j, k; |
| 482 | char keystr[10]; |
| 483 | |
| 484 | if (ISSET(NO_HELP)) |
| 485 | return; |
| 486 | |
| 487 | /* Determine how many extra spaces are needed to fill the bottom of the screen */ |
| 488 | k = COLS / 6 - 13; |
| 489 | |
| 490 | clear_bottomwin(); |
| 491 | wmove(bottomwin, 1, 0); |
| 492 | for (i = 0; i <= slen - 1; i += 2) { |
Robert Siemborski | 6af1431 | 2000-07-01 21:34:26 +0000 | [diff] [blame] | 493 | snprintf(keystr, 10, "^%c", s[i].val + 64); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 494 | onekey(keystr, s[i].desc); |
| 495 | |
| 496 | for (j = 0; j < k; j++) |
| 497 | waddch(bottomwin, ' '); |
| 498 | } |
| 499 | |
| 500 | wmove(bottomwin, 2, 0); |
| 501 | for (i = 1; i <= slen - 1; i += 2) { |
Robert Siemborski | 6af1431 | 2000-07-01 21:34:26 +0000 | [diff] [blame] | 502 | snprintf(keystr, 10, "^%c", s[i].val + 64); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 503 | onekey(keystr, s[i].desc); |
| 504 | |
| 505 | for (j = 0; j < k; j++) |
| 506 | waddch(bottomwin, ' '); |
| 507 | } |
| 508 | |
| 509 | wrefresh(bottomwin); |
| 510 | |
| 511 | } |
| 512 | |
| 513 | /* If modified is not already set, set it and update titlebar */ |
| 514 | void set_modified(void) |
| 515 | { |
| 516 | if (!ISSET(MODIFIED)) { |
| 517 | SET(MODIFIED); |
| 518 | titlebar(); |
| 519 | wrefresh(topwin); |
| 520 | } |
| 521 | } |
| 522 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 523 | inline int get_page_from_virtual(int virtual) |
| 524 | { |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 525 | int page = 2; |
| 526 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 527 | if (virtual <= COLS - 2) |
| 528 | return 1; |
Robert Siemborski | e8c6fd0 | 2000-06-07 04:40:09 +0000 | [diff] [blame] | 529 | virtual -= (COLS - 2); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 530 | |
| 531 | while (virtual > COLS - 2 - 7) { |
Robert Siemborski | e8c6fd0 | 2000-06-07 04:40:09 +0000 | [diff] [blame] | 532 | virtual -= (COLS - 2 - 7); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 533 | page++; |
| 534 | } |
| 535 | |
| 536 | return page; |
| 537 | } |
| 538 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 539 | inline int get_page_start_virtual(int page) |
| 540 | { |
Robert Siemborski | e8c6fd0 | 2000-06-07 04:40:09 +0000 | [diff] [blame] | 541 | int virtual; |
| 542 | virtual = --page * (COLS - 7); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 543 | if (page) |
| 544 | virtual -= 2 * page - 1; |
Robert Siemborski | e8c6fd0 | 2000-06-07 04:40:09 +0000 | [diff] [blame] | 545 | return virtual; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 546 | } |
| 547 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 548 | inline int get_page_end_virtual(int page) |
| 549 | { |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 550 | return get_page_start_virtual(page) + COLS - 1; |
| 551 | } |
| 552 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 553 | #ifndef NANO_SMALL |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 554 | /* Called only from edit_add, and therefore expects a |
| 555 | * converted-to-only-spaces line */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 556 | void add_marked_sameline(int begin, int end, filestruct * fileptr, int y, |
| 557 | int virt_cur_x, int this_page) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 558 | { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 559 | int this_page_start = get_page_start_virtual(this_page), |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 560 | this_page_end = get_page_end_virtual(this_page); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 561 | |
| 562 | /* 3 start points: 0 -> begin, begin->end, end->strlen(data) */ |
| 563 | /* in data : pre sel post */ |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 564 | /* likewise, 3 data lengths */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 565 | int pre_data_len = begin, sel_data_len = end - begin, post_data_len = 0; /* Determined from the other two */ |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 566 | |
| 567 | /* now fix the start locations & lengths according to the cursor's |
| 568 | * position (ie: our page) */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 569 | if (pre_data_len < this_page_start) |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 570 | pre_data_len = 0; |
| 571 | else |
| 572 | pre_data_len -= this_page_start; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 573 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 574 | if (begin < this_page_start) |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 575 | begin = this_page_start; |
| 576 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 577 | if (end < this_page_start) |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 578 | end = this_page_start; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 579 | |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 580 | /* we don't care about end, because it will just get cropped |
| 581 | * due to length */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 582 | if (begin > this_page_end) |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 583 | begin = this_page_end; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 584 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 585 | if (end > this_page_end) |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 586 | end = this_page_end; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 587 | |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 588 | sel_data_len = end - begin; |
| 589 | post_data_len = this_page_end - end; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 590 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 591 | mvwaddnstr(edit, y, 0, &fileptr->data[this_page_start], pre_data_len); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 592 | wattron(edit, A_REVERSE); |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 593 | mvwaddnstr(edit, y, begin - this_page_start, |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 594 | &fileptr->data[begin], sel_data_len); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 595 | wattroff(edit, A_REVERSE); |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 596 | mvwaddnstr(edit, y, end - this_page_start, |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 597 | &fileptr->data[end], post_data_len); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 598 | } |
| 599 | #endif |
| 600 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 601 | /* Called only from update_line. Expects a converted-to-not-have-tabs |
| 602 | * line */ |
| 603 | void edit_add(filestruct * fileptr, int yval, int start, int virt_cur_x, |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 604 | int virt_mark_beginx, int this_page) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 605 | { |
| 606 | #ifndef NANO_SMALL |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 607 | if (ISSET(MARK_ISSET)) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 608 | if ((fileptr->lineno > mark_beginbuf->lineno |
| 609 | && fileptr->lineno > current->lineno) |
| 610 | || (fileptr->lineno < mark_beginbuf->lineno |
| 611 | && fileptr->lineno < current->lineno)) { |
| 612 | |
| 613 | /* We're on a normal, unselected line */ |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 614 | mvwaddnstr(edit, yval, 0, fileptr->data, COLS); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 615 | } else { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 616 | if (fileptr != mark_beginbuf && fileptr != current) { |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 617 | /* We're on selected text */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 618 | wattron(edit, A_REVERSE); |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 619 | mvwaddnstr(edit, yval, 0, fileptr->data, COLS); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 620 | wattroff(edit, A_REVERSE); |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 621 | } else if (fileptr == mark_beginbuf && fileptr == current) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 622 | /* Special case, we're still on the same line we started |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 623 | * marking */ |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 624 | if (virt_cur_x < virt_mark_beginx) |
| 625 | add_marked_sameline(virt_cur_x, virt_mark_beginx, |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 626 | fileptr, yval, virt_cur_x, |
| 627 | this_page); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 628 | else |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 629 | add_marked_sameline(virt_mark_beginx, virt_cur_x, |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 630 | fileptr, yval, virt_cur_x, |
| 631 | this_page); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 632 | } else if (fileptr == mark_beginbuf) { |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 633 | /* we're updating the line that was first marked */ |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 634 | int target; |
| 635 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 636 | if (mark_beginbuf->lineno > current->lineno) |
| 637 | wattron(edit, A_REVERSE); |
| 638 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 639 | target = |
| 640 | (virt_mark_beginx < |
| 641 | COLS - 1) ? virt_mark_beginx : COLS - 1; |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 642 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 643 | mvwaddnstr(edit, yval, 0, fileptr->data, target); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 644 | |
| 645 | if (mark_beginbuf->lineno < current->lineno) |
| 646 | wattron(edit, A_REVERSE); |
| 647 | else |
| 648 | wattroff(edit, A_REVERSE); |
| 649 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 650 | target = (COLS - 1) - virt_mark_beginx; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 651 | if (target < 0) |
| 652 | target = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 653 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 654 | mvwaddnstr(edit, yval, virt_mark_beginx, |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 655 | &fileptr->data[virt_mark_beginx], target); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 656 | |
| 657 | if (mark_beginbuf->lineno < current->lineno) |
| 658 | wattroff(edit, A_REVERSE); |
| 659 | |
| 660 | } else if (fileptr == current) { |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 661 | /* we're on the cursors line, but it's not the first |
| 662 | * one we marked... */ |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 663 | int this_page_start = get_page_start_virtual(this_page), |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 664 | this_page_end = get_page_end_virtual(this_page); |
| 665 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 666 | if (mark_beginbuf->lineno < current->lineno) |
| 667 | wattron(edit, A_REVERSE); |
| 668 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 669 | if (virt_cur_x > COLS - 2) { |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 670 | mvwaddnstr(edit, yval, 0, |
| 671 | &fileptr->data[this_page_start], |
| 672 | virt_cur_x - this_page_start); |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 673 | } else { |
| 674 | mvwaddnstr(edit, yval, 0, fileptr->data, virt_cur_x); |
| 675 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 676 | |
| 677 | if (mark_beginbuf->lineno > current->lineno) |
| 678 | wattron(edit, A_REVERSE); |
| 679 | else |
| 680 | wattroff(edit, A_REVERSE); |
| 681 | |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 682 | if (virt_cur_x > COLS - 2) |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 683 | mvwaddnstr(edit, yval, virt_cur_x - this_page_start, |
| 684 | &fileptr->data[virt_cur_x], |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 685 | this_page_end - virt_cur_x); |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 686 | else |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 687 | mvwaddnstr(edit, yval, virt_cur_x, |
| 688 | &fileptr->data[virt_cur_x], |
| 689 | COLS - virt_cur_x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 690 | |
| 691 | if (mark_beginbuf->lineno > current->lineno) |
| 692 | wattroff(edit, A_REVERSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 693 | } |
| 694 | } |
| 695 | |
| 696 | } else |
| 697 | #endif |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 698 | mvwaddnstr(edit, yval, 0, &fileptr->data[start], |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 699 | get_page_end_virtual(this_page) - start); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 700 | } |
| 701 | |
| 702 | /* |
| 703 | * Just update one line in the edit buffer |
| 704 | * |
| 705 | * index gives is a place in the string to update starting from. |
| 706 | * Likely args are current_x or 0. |
| 707 | */ |
| 708 | void update_line(filestruct * fileptr, int index) |
| 709 | { |
| 710 | filestruct *filetmp; |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 711 | int line = 0, col = 0; |
| 712 | int virt_cur_x = current_x, virt_mark_beginx = mark_beginx; |
| 713 | char *realdata, *tmp; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 714 | int i, pos, len, page; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 715 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 716 | if (!fileptr) |
| 717 | return; |
Robert Siemborski | 53154a7 | 2000-06-18 00:11:03 +0000 | [diff] [blame] | 718 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 719 | /* First, blank out the line (at a minimum) */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 720 | for (filetmp = edittop; filetmp != fileptr && filetmp != editbot; |
| 721 | filetmp = filetmp->next) |
| 722 | line++; |
| 723 | |
| 724 | mvwaddstr(edit, line, 0, hblank); |
| 725 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 726 | /* Next, convert all the tabs to spaces so everything else is easy */ |
| 727 | index = xpt(fileptr, index); |
| 728 | |
| 729 | realdata = fileptr->data; |
| 730 | len = strlen(realdata); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 731 | fileptr->data = nmalloc(xpt(fileptr, len) + 1); |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 732 | |
| 733 | pos = 0; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 734 | for (i = 0; i < len; i++) { |
| 735 | if (realdata[i] == '\t') { |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 736 | do { |
| 737 | fileptr->data[pos++] = ' '; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 738 | if (i < current_x) |
| 739 | virt_cur_x++; |
| 740 | if (i < mark_beginx) |
| 741 | virt_mark_beginx++; |
| 742 | } while (pos % TABSIZE); |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 743 | /* must decrement once to account for tab-is-one-character */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 744 | if (i < current_x) |
| 745 | virt_cur_x--; |
| 746 | if (i < mark_beginx) |
| 747 | virt_mark_beginx--; |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 748 | } else { |
| 749 | fileptr->data[pos++] = realdata[i]; |
| 750 | } |
| 751 | } |
| 752 | |
| 753 | fileptr->data[pos] = '\0'; |
| 754 | |
| 755 | /* Now, Paint the line */ |
| 756 | if (current == fileptr && index > COLS - 2) { |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 757 | /* This handles when the current line is beyond COLS */ |
| 758 | /* It requires figureing out what page we're at */ |
| 759 | page = get_page_from_virtual(index); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 760 | col = get_page_start_virtual(page); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 761 | |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 762 | edit_add(filetmp, line, col, virt_cur_x, virt_mark_beginx, page); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 763 | mvwaddch(edit, line, 0, '$'); |
| 764 | |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 765 | if (strlenpt(fileptr->data) > get_page_end_virtual(page)) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 766 | mvwaddch(edit, line, COLS - 1, '$'); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 767 | } else { |
Robert Siemborski | a9addc7 | 2000-06-17 06:06:35 +0000 | [diff] [blame] | 768 | /* It's not the current line means that it's at x=0 and page=1 */ |
| 769 | /* If it is the current line, then we're in the same boat */ |
| 770 | edit_add(filetmp, line, 0, virt_cur_x, virt_mark_beginx, 1); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 771 | |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 772 | if (strlenpt(&filetmp->data[col]) > COLS) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 773 | mvwaddch(edit, line, COLS - 1, '$'); |
Robert Siemborski | d8510b2 | 2000-06-06 23:04:06 +0000 | [diff] [blame] | 774 | } |
Robert Siemborski | 5387591 | 2000-06-16 04:25:30 +0000 | [diff] [blame] | 775 | |
| 776 | /* Clean up our mess */ |
| 777 | tmp = fileptr->data; |
| 778 | fileptr->data = realdata; |
| 779 | free(tmp); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 780 | } |
| 781 | |
| 782 | void center_cursor(void) |
| 783 | { |
| 784 | current_y = editwinrows / 2; |
| 785 | wmove(edit, current_y, current_x); |
| 786 | } |
| 787 | |
| 788 | /* Refresh the screen without changing the position of lines */ |
| 789 | void edit_refresh(void) |
| 790 | { |
| 791 | int lines = 0, i = 0; |
| 792 | filestruct *temp, *hold = current; |
| 793 | |
| 794 | if (current == NULL) |
| 795 | return; |
| 796 | |
| 797 | temp = edittop; |
| 798 | |
| 799 | while (lines <= editwinrows - 1 && lines <= totlines && temp != NULL) { |
| 800 | hold = temp; |
| 801 | update_line(temp, current_x); |
| 802 | temp = temp->next; |
| 803 | lines++; |
| 804 | } |
| 805 | |
| 806 | if (lines <= editwinrows - 1) |
| 807 | while (lines <= editwinrows - 1) { |
| 808 | mvwaddstr(edit, lines, i, hblank); |
| 809 | lines++; |
| 810 | } |
| 811 | if (temp == NULL) |
| 812 | editbot = hold; |
| 813 | else |
| 814 | editbot = temp; |
| 815 | } |
| 816 | |
| 817 | /* |
| 818 | * Nice generic routine to update the edit buffer given a pointer to the |
| 819 | * file struct =) |
| 820 | */ |
| 821 | void edit_update(filestruct * fileptr) |
| 822 | { |
Robert Siemborski | 29e9a76 | 2000-07-05 03:16:04 +0000 | [diff] [blame^] | 823 | int i = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 824 | filestruct *temp; |
| 825 | |
| 826 | if (fileptr == NULL) |
| 827 | return; |
| 828 | |
| 829 | temp = fileptr; |
| 830 | while (i <= editwinrows / 2 && temp->prev != NULL) { |
| 831 | i++; |
| 832 | temp = temp->prev; |
| 833 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 834 | |
Robert Siemborski | 29e9a76 | 2000-07-05 03:16:04 +0000 | [diff] [blame^] | 835 | edittop = temp; |
| 836 | fix_editbot(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 837 | |
| 838 | edit_refresh(); |
| 839 | } |
| 840 | |
| 841 | /* Now we want to update the screen using this struct as the top of the edit buffer */ |
| 842 | void edit_update_top(filestruct * fileptr) |
| 843 | { |
| 844 | int i; |
| 845 | filestruct *temp = fileptr; |
| 846 | |
Chris Allegretta | 1e57e68 | 2000-07-03 04:24:39 +0000 | [diff] [blame] | 847 | if (fileptr == NULL) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 848 | return; |
| 849 | |
| 850 | i = 0; |
| 851 | while (i <= editwinrows / 2 && temp->next != NULL) { |
| 852 | i++; |
| 853 | temp = temp->next; |
| 854 | } |
| 855 | edit_update(temp); |
| 856 | } |
| 857 | |
| 858 | /* And also for the bottom... */ |
| 859 | void edit_update_bot(filestruct * fileptr) |
| 860 | { |
| 861 | int i; |
| 862 | filestruct *temp = fileptr; |
| 863 | |
| 864 | i = 0; |
| 865 | while (i <= editwinrows / 2 - 1 && temp->prev != NULL) { |
| 866 | i++; |
| 867 | temp = temp->prev; |
| 868 | } |
| 869 | edit_update(temp); |
| 870 | } |
| 871 | |
| 872 | /* This function updates current based on where current_y is, reset_cursor |
| 873 | does the opposite */ |
| 874 | void update_cursor(void) |
| 875 | { |
| 876 | int i = 0; |
| 877 | |
| 878 | #ifdef DEBUG |
| 879 | fprintf(stderr, _("Moved to (%d, %d) in edit buffer\n"), current_y, |
| 880 | current_x); |
| 881 | #endif |
| 882 | |
| 883 | current = edittop; |
| 884 | while (i <= current_y - 1 && current->next != NULL) { |
| 885 | current = current->next; |
| 886 | i++; |
| 887 | } |
| 888 | |
| 889 | #ifdef DEBUG |
| 890 | fprintf(stderr, _("current->data = \"%s\"\n"), current->data); |
| 891 | #endif |
| 892 | |
| 893 | } |
| 894 | |
| 895 | /* |
| 896 | * Ask a question on the statusbar. Answer will be stored in answer |
| 897 | * global. Returns -1 on aborted enter, -2 on a blank string, and 0 |
| 898 | * otherwise, the valid shortcut key caught, Def is any editable text we |
| 899 | * want to put up by default. |
| 900 | */ |
| 901 | int statusq(shortcut s[], int slen, char *def, char *msg, ...) |
| 902 | { |
| 903 | va_list ap; |
| 904 | char foo[133]; |
| 905 | int ret; |
| 906 | |
| 907 | bottombars(s, slen); |
| 908 | |
| 909 | va_start(ap, msg); |
| 910 | vsnprintf(foo, 132, msg, ap); |
| 911 | strncat(foo, ": ", 132); |
| 912 | va_end(ap); |
| 913 | |
| 914 | wattron(bottomwin, A_REVERSE); |
| 915 | ret = nanogetstr(foo, def, s, slen, (strlen(foo) + 3)); |
| 916 | wattroff(bottomwin, A_REVERSE); |
| 917 | |
| 918 | switch (ret) { |
| 919 | |
| 920 | case NANO_FIRSTLINE_KEY: |
| 921 | do_first_line(); |
| 922 | break; |
| 923 | case NANO_LASTLINE_KEY: |
| 924 | do_last_line(); |
| 925 | break; |
| 926 | case NANO_CANCEL_KEY: |
| 927 | return -1; |
| 928 | default: |
| 929 | blank_statusbar_refresh(); |
| 930 | } |
| 931 | |
| 932 | #ifdef DEBUG |
| 933 | fprintf(stderr, _("I got \"%s\"\n"), answer); |
| 934 | #endif |
| 935 | |
| 936 | return ret; |
| 937 | } |
| 938 | |
| 939 | /* |
| 940 | * Ask a simple yes/no question on the statusbar. Returns 1 for Y, 0 for |
| 941 | * N, 2 for All (if all is non-zero when passed in) and -1 for abort (^C) |
| 942 | */ |
| 943 | int do_yesno(int all, int leavecursor, char *msg, ...) |
| 944 | { |
| 945 | va_list ap; |
| 946 | char foo[133]; |
| 947 | int kbinput, ok = -1; |
| 948 | |
| 949 | /* Write the bottom of the screen */ |
| 950 | clear_bottomwin(); |
| 951 | wattron(bottomwin, A_REVERSE); |
| 952 | blank_statusbar_refresh(); |
| 953 | wattroff(bottomwin, A_REVERSE); |
| 954 | |
Jordi Mallach | 0b0fc49 | 2000-06-23 01:00:13 +0000 | [diff] [blame] | 955 | /* Remove gettext call for keybindings until we clear the thing up */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 956 | if (!ISSET(NO_HELP)) { |
| 957 | wmove(bottomwin, 1, 0); |
Jordi Mallach | 0b0fc49 | 2000-06-23 01:00:13 +0000 | [diff] [blame] | 958 | onekey(" Y", _("Yes")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 959 | if (all) |
Jordi Mallach | 0b0fc49 | 2000-06-23 01:00:13 +0000 | [diff] [blame] | 960 | onekey(" A", _("All")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 961 | wmove(bottomwin, 2, 0); |
Jordi Mallach | 0b0fc49 | 2000-06-23 01:00:13 +0000 | [diff] [blame] | 962 | onekey(" N", _("No")); |
| 963 | onekey("^C", _("Cancel")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 964 | } |
| 965 | va_start(ap, msg); |
| 966 | vsnprintf(foo, 132, msg, ap); |
| 967 | va_end(ap); |
| 968 | wattron(bottomwin, A_REVERSE); |
| 969 | mvwaddstr(bottomwin, 0, 0, foo); |
| 970 | wattroff(bottomwin, A_REVERSE); |
| 971 | wrefresh(bottomwin); |
| 972 | |
| 973 | if (leavecursor == 1) |
| 974 | reset_cursor(); |
| 975 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 976 | while (ok == -1) { |
| 977 | kbinput = wgetch(edit); |
| 978 | |
| 979 | switch (kbinput) { |
| 980 | case 'Y': |
| 981 | case 'y': |
| 982 | ok = 1; |
| 983 | break; |
| 984 | case 'N': |
| 985 | case 'n': |
| 986 | ok = 0; |
| 987 | break; |
| 988 | case 'A': |
| 989 | case 'a': |
| 990 | if (all) |
| 991 | ok = 2; |
| 992 | break; |
| 993 | case NANO_CONTROL_C: |
| 994 | ok = -2; |
| 995 | break; |
| 996 | } |
| 997 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 998 | |
| 999 | /* Then blank the screen */ |
| 1000 | blank_statusbar_refresh(); |
| 1001 | |
| 1002 | if (ok == -2) |
| 1003 | return -1; |
| 1004 | else |
| 1005 | return ok; |
| 1006 | } |
| 1007 | |
| 1008 | void statusbar(char *msg, ...) |
| 1009 | { |
| 1010 | va_list ap; |
| 1011 | char foo[133]; |
| 1012 | int start_x = 0; |
| 1013 | |
| 1014 | va_start(ap, msg); |
| 1015 | vsnprintf(foo, 132, msg, ap); |
| 1016 | va_end(ap); |
| 1017 | |
| 1018 | start_x = center_x - strlen(foo) / 2 - 1; |
| 1019 | |
| 1020 | /* Blank out line */ |
| 1021 | blank_statusbar(); |
| 1022 | |
| 1023 | wmove(bottomwin, 0, start_x); |
| 1024 | |
| 1025 | wattron(bottomwin, A_REVERSE); |
| 1026 | |
| 1027 | waddstr(bottomwin, "[ "); |
| 1028 | waddstr(bottomwin, foo); |
| 1029 | waddstr(bottomwin, " ]"); |
| 1030 | wattroff(bottomwin, A_REVERSE); |
| 1031 | wrefresh(bottomwin); |
| 1032 | |
| 1033 | if (ISSET(CONSTUPDATE)) |
| 1034 | statblank = 1; |
| 1035 | else |
| 1036 | statblank = 25; |
| 1037 | } |
| 1038 | |
| 1039 | void display_main_list(void) |
| 1040 | { |
| 1041 | bottombars(main_list, MAIN_VISIBLE); |
| 1042 | } |
| 1043 | |
| 1044 | int total_refresh(void) |
| 1045 | { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1046 | clearok(edit, TRUE); |
| 1047 | clearok(topwin, TRUE); |
| 1048 | clearok(bottomwin, TRUE); |
| 1049 | wnoutrefresh(edit); |
| 1050 | wnoutrefresh(topwin); |
| 1051 | wnoutrefresh(bottomwin); |
| 1052 | doupdate(); |
| 1053 | clearok(edit, FALSE); |
| 1054 | clearok(topwin, FALSE); |
| 1055 | clearok(bottomwin, FALSE); |
| 1056 | |
| 1057 | return 1; |
| 1058 | } |
| 1059 | |
| 1060 | void previous_line(void) |
| 1061 | { |
| 1062 | if (current_y > 0) |
| 1063 | current_y--; |
| 1064 | } |
| 1065 | |
| 1066 | int do_cursorpos(void) |
| 1067 | { |
| 1068 | filestruct *fileptr; |
| 1069 | float linepct, bytepct; |
| 1070 | int i, tot = 0; |
| 1071 | |
| 1072 | if (current == NULL || fileage == NULL) |
| 1073 | return 0; |
| 1074 | |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1075 | for (fileptr = fileage; fileptr != current && fileptr != NULL; |
| 1076 | fileptr = fileptr->next) |
| 1077 | tot += strlen(fileptr->data) + 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1078 | |
| 1079 | if (fileptr == NULL) |
| 1080 | return -1; |
| 1081 | |
| 1082 | i = tot + current_x;; |
| 1083 | |
| 1084 | for (fileptr = current->next; fileptr != NULL; fileptr = fileptr->next) |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1085 | tot += strlen(fileptr->data) + 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1086 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1087 | if (totlines > 0) |
| 1088 | linepct = 100 * current->lineno / totlines; |
| 1089 | else |
| 1090 | linepct = 0; |
| 1091 | |
| 1092 | if (totsize > 0) |
| 1093 | bytepct = 100 * i / totsize; |
| 1094 | else |
| 1095 | bytepct = 0; |
| 1096 | |
| 1097 | #ifdef DEBUG |
| 1098 | fprintf(stderr, _("do_cursorpos: linepct = %f, bytepct = %f\n"), |
| 1099 | linepct, bytepct); |
| 1100 | #endif |
| 1101 | |
| 1102 | statusbar(_("line %d of %d (%.0f%%), character %d of %d (%.0f%%)"), |
| 1103 | current->lineno, totlines, linepct, i, totsize, bytepct); |
| 1104 | reset_cursor(); |
| 1105 | return 1; |
| 1106 | } |
| 1107 | |
| 1108 | /* Our broken, non-shortcut list compliant help function. |
| 1109 | But hey, it's better than nothing, and it's dynamic! */ |
| 1110 | int do_help(void) |
| 1111 | { |
| 1112 | #ifndef NANO_SMALL |
| 1113 | char *ptr = help_text, *end; |
| 1114 | int i, j, row = 0, page = 1, kbinput = 0, no_more = 0; |
| 1115 | int no_help_flag = 0; |
| 1116 | |
| 1117 | blank_edit(); |
| 1118 | curs_set(0); |
| 1119 | blank_statusbar(); |
| 1120 | |
| 1121 | if (ISSET(NO_HELP)) { |
| 1122 | |
| 1123 | no_help_flag = 1; |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1124 | delwin(bottomwin); |
| 1125 | bottomwin = newwin(3, COLS, LINES - 3, 0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1126 | keypad(bottomwin, TRUE); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1127 | |
| 1128 | editwinrows -= no_help(); |
| 1129 | UNSET(NO_HELP); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1130 | bottombars(help_list, HELP_LIST_LEN); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1131 | } else |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1132 | bottombars(help_list, HELP_LIST_LEN); |
| 1133 | |
| 1134 | do { |
| 1135 | ptr = help_text; |
| 1136 | switch (kbinput) { |
| 1137 | case NANO_NEXTPAGE_KEY: |
| 1138 | case NANO_NEXTPAGE_FKEY: |
| 1139 | case KEY_NPAGE: |
| 1140 | if (!no_more) { |
| 1141 | blank_edit(); |
| 1142 | page++; |
| 1143 | } |
| 1144 | break; |
| 1145 | case NANO_PREVPAGE_KEY: |
| 1146 | case NANO_PREVPAGE_FKEY: |
| 1147 | case KEY_PPAGE: |
| 1148 | if (page > 1) { |
| 1149 | no_more = 0; |
| 1150 | blank_edit(); |
| 1151 | page--; |
| 1152 | } |
| 1153 | break; |
| 1154 | } |
| 1155 | |
| 1156 | /* Calculate where in the text we should be based on the page */ |
| 1157 | for (i = 1; i < page; i++) { |
| 1158 | row = 0; |
| 1159 | j = 0; |
| 1160 | while (row < editwinrows && *ptr != '\0') { |
| 1161 | if (*ptr == '\n' || j == COLS - 5) { |
| 1162 | j = 0; |
| 1163 | row++; |
| 1164 | } |
| 1165 | ptr++; |
| 1166 | j++; |
| 1167 | } |
| 1168 | } |
| 1169 | |
| 1170 | i = 0; |
| 1171 | j = 0; |
| 1172 | while (i < editwinrows && *ptr != '\0') { |
| 1173 | end = ptr; |
| 1174 | while (*end != '\n' && *end != '\0' && j != COLS - 5) { |
| 1175 | end++; |
| 1176 | j++; |
| 1177 | } |
| 1178 | if (j == COLS - 5) { |
| 1179 | |
| 1180 | /* Don't print half a word if we've run of of space */ |
| 1181 | while (*end != ' ' && *end != '\0') { |
| 1182 | end--; |
| 1183 | j--; |
| 1184 | } |
| 1185 | } |
| 1186 | mvwaddnstr(edit, i, 0, ptr, j); |
| 1187 | j = 0; |
| 1188 | i++; |
| 1189 | if (*end == '\n') |
| 1190 | end++; |
| 1191 | ptr = end; |
| 1192 | } |
| 1193 | if (*ptr == '\0') { |
| 1194 | no_more = 1; |
| 1195 | continue; |
| 1196 | } |
| 1197 | } while ((kbinput = wgetch(edit)) != NANO_EXIT_KEY); |
| 1198 | |
| 1199 | if (no_help_flag) { |
| 1200 | werase(bottomwin); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1201 | wrefresh(bottomwin); |
| 1202 | delwin(bottomwin); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1203 | SET(NO_HELP); |
| 1204 | bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0); |
| 1205 | keypad(bottomwin, TRUE); |
| 1206 | editwinrows += no_help(); |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 1207 | } else |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1208 | display_main_list(); |
| 1209 | |
| 1210 | curs_set(1); |
| 1211 | edit_refresh(); |
| 1212 | #else |
| 1213 | nano_small_msg(); |
| 1214 | #endif |
| 1215 | |
| 1216 | return 1; |
| 1217 | } |
| 1218 | |
| 1219 | /* Dump the current file structure to stderr */ |
| 1220 | void dump_buffer(filestruct * inptr) |
| 1221 | { |
| 1222 | #ifdef DEBUG |
| 1223 | filestruct *fileptr; |
| 1224 | |
| 1225 | if (inptr == fileage) |
| 1226 | fprintf(stderr, _("Dumping file buffer to stderr...\n")); |
| 1227 | else if (inptr == cutbuffer) |
| 1228 | fprintf(stderr, _("Dumping cutbuffer to stderr...\n")); |
| 1229 | else |
| 1230 | fprintf(stderr, _("Dumping a buffer to stderr...\n")); |
| 1231 | |
| 1232 | fileptr = inptr; |
| 1233 | while (fileptr != NULL) { |
| 1234 | fprintf(stderr, "(%ld) %s\n", fileptr->lineno, fileptr->data); |
| 1235 | fflush(stderr); |
| 1236 | fileptr = fileptr->next; |
| 1237 | } |
| 1238 | #endif /* DEBUG */ |
| 1239 | } |
| 1240 | |
| 1241 | void dump_buffer_reverse(filestruct * inptr) |
| 1242 | { |
| 1243 | #ifdef DEBUG |
| 1244 | filestruct *fileptr; |
| 1245 | |
| 1246 | fileptr = filebot; |
| 1247 | while (fileptr != NULL) { |
| 1248 | fprintf(stderr, "(%ld) %s\n", fileptr->lineno, fileptr->data); |
| 1249 | fflush(stderr); |
| 1250 | fileptr = fileptr->prev; |
| 1251 | } |
| 1252 | #endif /* DEBUG */ |
| 1253 | } |
Robert Siemborski | dd53ec2 | 2000-07-04 02:35:19 +0000 | [diff] [blame] | 1254 | |
| 1255 | /* Fix editbot based on the assumption that edittop is correct */ |
| 1256 | void fix_editbot(void) { |
| 1257 | int i; |
| 1258 | editbot = edittop; |
| 1259 | for(i = 0; (i <= editwinrows - 1) && (editbot->next != NULL) |
| 1260 | && (editbot != filebot); i++, editbot = editbot->next); |
| 1261 | } |
| 1262 | |