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