Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 1 | /************************************************************************** |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 2 | * move.c -- This file is part of GNU nano. * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 3 | * * |
Chris Allegretta | 8a07a96 | 2009-12-02 03:36:22 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * |
Benno Schulenberg | 7a9f4a4 | 2014-04-30 20:18:26 +0000 | [diff] [blame] | 5 | * 2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc. * |
Benno Schulenberg | 406e524 | 2016-08-29 15:14:18 +0200 | [diff] [blame] | 6 | * Copyright (C) 2014, 2015, 2016 Benno Schulenberg * |
| 7 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 8 | * GNU nano is free software: you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published * |
| 10 | * by the Free Software Foundation, either version 3 of the License, * |
| 11 | * or (at your option) any later version. * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 12 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 13 | * GNU nano is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty * |
| 15 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 16 | * See the GNU General Public License for more details. * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 19 | * along with this program. If not, see http://www.gnu.org/licenses/. * |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 20 | * * |
| 21 | **************************************************************************/ |
| 22 | |
David Lawrence Ramsey | 034b994 | 2005-12-08 02:47:10 +0000 | [diff] [blame] | 23 | #include "proto.h" |
Chris Allegretta | 6efda54 | 2001-04-28 18:03:52 +0000 | [diff] [blame] | 24 | |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 25 | #include <string.h> |
David Lawrence Ramsey | c7acf69 | 2004-05-22 20:15:20 +0000 | [diff] [blame] | 26 | #include <ctype.h> |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 27 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 28 | /* Move to the first line of the file. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 29 | void do_first_line(void) |
David Lawrence Ramsey | 4be15f0 | 2004-05-23 21:33:23 +0000 | [diff] [blame] | 30 | { |
Benno Schulenberg | 925ad63 | 2016-10-12 21:07:16 +0200 | [diff] [blame] | 31 | openfile->current = openfile->fileage; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 32 | openfile->current_x = 0; |
David Lawrence Ramsey | f00c961 | 2005-07-14 18:01:08 +0000 | [diff] [blame] | 33 | openfile->placewewant = 0; |
| 34 | |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 35 | refresh_needed = TRUE; |
David Lawrence Ramsey | 4be15f0 | 2004-05-23 21:33:23 +0000 | [diff] [blame] | 36 | } |
| 37 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 38 | /* Move to the last line of the file. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 39 | void do_last_line(void) |
David Lawrence Ramsey | 4be15f0 | 2004-05-23 21:33:23 +0000 | [diff] [blame] | 40 | { |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 41 | openfile->current = openfile->filebot; |
David Lawrence Ramsey | 46f5665 | 2005-11-05 17:50:06 +0000 | [diff] [blame] | 42 | openfile->current_x = strlen(openfile->filebot->data); |
| 43 | openfile->placewewant = xplustabs(); |
Benno Schulenberg | 925ad63 | 2016-10-12 21:07:16 +0200 | [diff] [blame] | 44 | |
| 45 | /* Set the last line of the screen as the target for the cursor. */ |
David Lawrence Ramsey | a9b862c | 2005-08-16 01:27:05 +0000 | [diff] [blame] | 46 | openfile->current_y = editwinrows - 1; |
Benno Schulenberg | 58c3dd6 | 2017-01-09 15:21:15 +0100 | [diff] [blame] | 47 | ensure_line_is_visible(); |
David Lawrence Ramsey | f00c961 | 2005-07-14 18:01:08 +0000 | [diff] [blame] | 48 | |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 49 | refresh_needed = TRUE; |
Benno Schulenberg | 318ed6b | 2016-04-12 10:24:57 +0200 | [diff] [blame] | 50 | focusing = FALSE; |
David Lawrence Ramsey | 4be15f0 | 2004-05-23 21:33:23 +0000 | [diff] [blame] | 51 | } |
| 52 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 53 | /* Move up one page. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 54 | void do_page_up(void) |
Chris Allegretta | 1e57e68 | 2000-07-03 04:24:39 +0000 | [diff] [blame] | 55 | { |
Benno Schulenberg | bd1c5d7 | 2016-08-21 15:49:39 +0200 | [diff] [blame] | 56 | int i, mustmove, skipped = 0; |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 57 | |
Benno Schulenberg | 17ab9a2 | 2015-07-26 17:04:29 +0000 | [diff] [blame] | 58 | /* If the cursor is less than a page away from the top of the file, |
| 59 | * put it at the beginning of the first line. */ |
Benno Schulenberg | a4044a7 | 2016-12-09 13:31:04 +0100 | [diff] [blame] | 60 | if (openfile->current->lineno == 1 || (!ISSET(SOFTWRAP) && |
Benno Schulenberg | 00b293b | 2016-08-21 15:39:00 +0200 | [diff] [blame] | 61 | openfile->current->lineno <= editwinrows - 2)) { |
David Lawrence Ramsey | 4d46437 | 2005-07-16 22:50:30 +0000 | [diff] [blame] | 62 | do_first_line(); |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 63 | return; |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 64 | } |
Chris Allegretta | 1e57e68 | 2000-07-03 04:24:39 +0000 | [diff] [blame] | 65 | |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 66 | /* If we're not in smooth scrolling mode, put the cursor at the |
| 67 | * beginning of the top line of the edit window, as Pico does. */ |
Benno Schulenberg | a4044a7 | 2016-12-09 13:31:04 +0100 | [diff] [blame] | 68 | if (!ISSET(SMOOTH_SCROLL)) { |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 69 | openfile->current = openfile->edittop; |
Chris Allegretta | 08273aa | 2010-03-21 04:56:37 +0000 | [diff] [blame] | 70 | openfile->placewewant = openfile->current_y = 0; |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 71 | } |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 72 | |
Benno Schulenberg | bd1c5d7 | 2016-08-21 15:49:39 +0200 | [diff] [blame] | 73 | mustmove = (editwinrows < 3) ? 1 : editwinrows - 2; |
| 74 | |
| 75 | for (i = mustmove; i - skipped > 0 && openfile->current != openfile->fileage; i--) { |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 76 | openfile->current = openfile->current->prev; |
Benno Schulenberg | f80dcb2 | 2014-06-21 19:01:51 +0000 | [diff] [blame] | 77 | #ifndef NANO_TINY |
Chris Allegretta | c9199e9 | 2009-11-26 18:23:00 +0000 | [diff] [blame] | 78 | if (ISSET(SOFTWRAP) && openfile->current) { |
David Lawrence Ramsey | 6263416 | 2016-12-07 12:33:25 -0600 | [diff] [blame] | 79 | skipped += strlenpt(openfile->current->data) / editwincols; |
Chris Allegretta | c9199e9 | 2009-11-26 18:23:00 +0000 | [diff] [blame] | 80 | #ifdef DEBUG |
Benno Schulenberg | a4044a7 | 2016-12-09 13:31:04 +0100 | [diff] [blame] | 81 | fprintf(stderr, "paging up: i = %d, skipped = %d based on line %ld len %lu\n", |
Benno Schulenberg | f80dcb2 | 2014-06-21 19:01:51 +0000 | [diff] [blame] | 82 | i, skipped, (long)openfile->current->lineno, (unsigned long)strlenpt(openfile->current->data)); |
Chris Allegretta | c9199e9 | 2009-11-26 18:23:00 +0000 | [diff] [blame] | 83 | #endif |
| 84 | } |
Benno Schulenberg | f80dcb2 | 2014-06-21 19:01:51 +0000 | [diff] [blame] | 85 | #endif |
Chris Allegretta | e2df2c8 | 2009-11-24 17:15:53 +0000 | [diff] [blame] | 86 | } |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 87 | |
| 88 | openfile->current_x = actual_x(openfile->current->data, |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 89 | openfile->placewewant); |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 90 | |
| 91 | /* Scroll the edit window up a page. */ |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 92 | adjust_viewport(STATIONARY); |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 93 | refresh_needed = TRUE; |
Chris Allegretta | 1e57e68 | 2000-07-03 04:24:39 +0000 | [diff] [blame] | 94 | } |
| 95 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 96 | /* Move down one page. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 97 | void do_page_down(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 98 | { |
Benno Schulenberg | bd1c5d7 | 2016-08-21 15:49:39 +0200 | [diff] [blame] | 99 | int i, mustmove; |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 100 | |
Benno Schulenberg | 17ab9a2 | 2015-07-26 17:04:29 +0000 | [diff] [blame] | 101 | /* If the cursor is less than a page away from the bottom of the file, |
| 102 | * put it at the end of the last line. */ |
Benno Schulenberg | 0208ae7 | 2017-01-12 17:33:46 +0100 | [diff] [blame] | 103 | if (openfile->current->lineno + maxlines - 2 >= openfile->filebot->lineno) { |
David Lawrence Ramsey | 4d46437 | 2005-07-16 22:50:30 +0000 | [diff] [blame] | 104 | do_last_line(); |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 105 | return; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 106 | } |
| 107 | |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 108 | /* If we're not in smooth scrolling mode, put the cursor at the |
| 109 | * beginning of the top line of the edit window, as Pico does. */ |
Benno Schulenberg | a4044a7 | 2016-12-09 13:31:04 +0100 | [diff] [blame] | 110 | if (!ISSET(SMOOTH_SCROLL)) { |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 111 | openfile->current = openfile->edittop; |
Chris Allegretta | 08273aa | 2010-03-21 04:56:37 +0000 | [diff] [blame] | 112 | openfile->placewewant = openfile->current_y = 0; |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 113 | } |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 114 | |
Benno Schulenberg | 0208ae7 | 2017-01-12 17:33:46 +0100 | [diff] [blame] | 115 | mustmove = (maxlines < 3) ? 1 : maxlines - 2; |
Benno Schulenberg | bd1c5d7 | 2016-08-21 15:49:39 +0200 | [diff] [blame] | 116 | |
| 117 | for (i = mustmove; i > 0 && openfile->current != openfile->filebot; i--) { |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 118 | openfile->current = openfile->current->next; |
Chris Allegretta | 8c1edd1 | 2009-11-16 04:28:40 +0000 | [diff] [blame] | 119 | #ifdef DEBUG |
Benno Schulenberg | a4044a7 | 2016-12-09 13:31:04 +0100 | [diff] [blame] | 120 | fprintf(stderr, "paging down: moving to line %lu\n", (unsigned long)openfile->current->lineno); |
Chris Allegretta | 8c1edd1 | 2009-11-16 04:28:40 +0000 | [diff] [blame] | 121 | #endif |
| 122 | |
| 123 | } |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 124 | |
| 125 | openfile->current_x = actual_x(openfile->current->data, |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 126 | openfile->placewewant); |
David Lawrence Ramsey | 5e146e8 | 2005-07-17 00:01:18 +0000 | [diff] [blame] | 127 | |
| 128 | /* Scroll the edit window down a page. */ |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 129 | adjust_viewport(STATIONARY); |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 130 | refresh_needed = TRUE; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 131 | } |
| 132 | |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 133 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | 036a9d5 | 2005-11-10 05:26:37 +0000 | [diff] [blame] | 134 | /* Move up to the beginning of the last beginning-of-paragraph line |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 135 | * before the current line. If allow_update is TRUE, update the screen |
| 136 | * afterwards. */ |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 137 | void do_para_begin(bool allow_update) |
| 138 | { |
Benno Schulenberg | 7013039 | 2016-10-12 21:10:04 +0200 | [diff] [blame] | 139 | filestruct *was_current = openfile->current; |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 140 | |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 141 | if (openfile->current != openfile->fileage) { |
David Lawrence Ramsey | b1c2062 | 2016-12-22 13:01:27 -0600 | [diff] [blame] | 142 | do |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 143 | openfile->current = openfile->current->prev; |
David Lawrence Ramsey | b1c2062 | 2016-12-22 13:01:27 -0600 | [diff] [blame] | 144 | while (!begpar(openfile->current)); |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 145 | } |
| 146 | |
David Lawrence Ramsey | 31d7b36 | 2005-11-09 04:20:55 +0000 | [diff] [blame] | 147 | openfile->current_x = 0; |
David Lawrence Ramsey | 31d7b36 | 2005-11-09 04:20:55 +0000 | [diff] [blame] | 148 | |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 149 | if (allow_update) |
Benno Schulenberg | 7013039 | 2016-10-12 21:10:04 +0200 | [diff] [blame] | 150 | edit_redraw(was_current); |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 151 | } |
| 152 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 153 | /* Move up to the beginning of the last beginning-of-paragraph line |
| 154 | * before the current line, and update the screen afterwards. */ |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 155 | void do_para_begin_void(void) |
| 156 | { |
| 157 | do_para_begin(TRUE); |
| 158 | } |
| 159 | |
David Lawrence Ramsey | 036a9d5 | 2005-11-10 05:26:37 +0000 | [diff] [blame] | 160 | /* Move down to the beginning of the last line of the current paragraph. |
| 161 | * Then move down one line farther if there is such a line, or to the |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 162 | * end of the current line if not. If allow_update is TRUE, update the |
| 163 | * screen afterwards. A line is the last line of a paragraph if it is |
| 164 | * in a paragraph, and the next line either is the beginning line of a |
| 165 | * paragraph or isn't in a paragraph. */ |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 166 | void do_para_end(bool allow_update) |
| 167 | { |
Benno Schulenberg | 7013039 | 2016-10-12 21:10:04 +0200 | [diff] [blame] | 168 | filestruct *was_current = openfile->current; |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 169 | |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 170 | while (openfile->current != openfile->filebot && |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 171 | !inpar(openfile->current)) |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 172 | openfile->current = openfile->current->next; |
| 173 | |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 174 | while (openfile->current != openfile->filebot && |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 175 | inpar(openfile->current->next) && |
| 176 | !begpar(openfile->current->next)) { |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 177 | openfile->current = openfile->current->next; |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 178 | } |
| 179 | |
David Lawrence Ramsey | 31d7b36 | 2005-11-09 04:20:55 +0000 | [diff] [blame] | 180 | if (openfile->current != openfile->filebot) { |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 181 | openfile->current = openfile->current->next; |
David Lawrence Ramsey | 31d7b36 | 2005-11-09 04:20:55 +0000 | [diff] [blame] | 182 | openfile->current_x = 0; |
Benno Schulenberg | aa1ae0a | 2016-04-10 21:16:19 +0200 | [diff] [blame] | 183 | } else |
David Lawrence Ramsey | 31d7b36 | 2005-11-09 04:20:55 +0000 | [diff] [blame] | 184 | openfile->current_x = strlen(openfile->current->data); |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 185 | |
| 186 | if (allow_update) |
Benno Schulenberg | 7013039 | 2016-10-12 21:10:04 +0200 | [diff] [blame] | 187 | edit_redraw(was_current); |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 188 | } |
| 189 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 190 | /* Move down to the beginning of the last line of the current paragraph. |
| 191 | * Then move down one line farther if there is such a line, or to the |
| 192 | * end of the current line if not, and update the screen afterwards. */ |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 193 | void do_para_end_void(void) |
| 194 | { |
| 195 | do_para_end(TRUE); |
| 196 | } |
| 197 | #endif /* !DISABLE_JUSTIFY */ |
| 198 | |
Benno Schulenberg | c6dbcf9 | 2016-06-25 15:16:52 +0200 | [diff] [blame] | 199 | /* Move to the preceding block of text in the file. */ |
| 200 | void do_prev_block(void) |
| 201 | { |
| 202 | filestruct *was_current = openfile->current; |
| 203 | bool is_text = FALSE, seen_text = FALSE; |
| 204 | |
| 205 | /* Skip backward until first blank line after some nonblank line(s). */ |
| 206 | while (openfile->current->prev != NULL && (!seen_text || is_text)) { |
| 207 | openfile->current = openfile->current->prev; |
| 208 | is_text = !white_string(openfile->current->data); |
| 209 | seen_text = seen_text || is_text; |
| 210 | } |
| 211 | |
| 212 | /* Step forward one line again if this one is blank. */ |
| 213 | if (openfile->current->next != NULL && |
| 214 | white_string(openfile->current->data)) |
| 215 | openfile->current = openfile->current->next; |
| 216 | |
| 217 | openfile->current_x = 0; |
| 218 | edit_redraw(was_current); |
Benno Schulenberg | c6dbcf9 | 2016-06-25 15:16:52 +0200 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | /* Move to the next block of text in the file. */ |
| 222 | void do_next_block(void) |
| 223 | { |
| 224 | filestruct *was_current = openfile->current; |
| 225 | bool is_white = white_string(openfile->current->data); |
| 226 | bool seen_white = is_white; |
| 227 | |
| 228 | /* Skip forward until first nonblank line after some blank line(s). */ |
| 229 | while (openfile->current->next != NULL && (!seen_white || is_white)) { |
| 230 | openfile->current = openfile->current->next; |
| 231 | is_white = white_string(openfile->current->data); |
| 232 | seen_white = seen_white || is_white; |
| 233 | } |
| 234 | |
| 235 | openfile->current_x = 0; |
| 236 | edit_redraw(was_current); |
Benno Schulenberg | c6dbcf9 | 2016-06-25 15:16:52 +0200 | [diff] [blame] | 237 | } |
| 238 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 239 | /* Move to the previous word in the file. If allow_punct is TRUE, treat |
| 240 | * punctuation as part of a word. If allow_update is TRUE, update the |
Benno Schulenberg | 6620de0 | 2015-09-05 09:22:50 +0000 | [diff] [blame] | 241 | * screen afterwards. */ |
| 242 | void do_prev_word(bool allow_punct, bool allow_update) |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 243 | { |
Benno Schulenberg | 7013039 | 2016-10-12 21:10:04 +0200 | [diff] [blame] | 244 | filestruct *was_current = openfile->current; |
Benno Schulenberg | b410332 | 2015-09-05 09:40:09 +0000 | [diff] [blame] | 245 | bool seen_a_word = FALSE, step_forward = FALSE; |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 246 | |
| 247 | assert(openfile->current != NULL && openfile->current->data != NULL); |
| 248 | |
Benno Schulenberg | b410332 | 2015-09-05 09:40:09 +0000 | [diff] [blame] | 249 | /* Move backward until we pass over the start of a word. */ |
| 250 | while (TRUE) { |
| 251 | /* If at the head of a line, move to the end of the preceding one. */ |
| 252 | if (openfile->current_x == 0) { |
| 253 | if (openfile->current->prev == NULL) |
| 254 | break; |
| 255 | openfile->current = openfile->current->prev; |
| 256 | openfile->current_x = strlen(openfile->current->data); |
| 257 | } |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 258 | |
Benno Schulenberg | b410332 | 2015-09-05 09:40:09 +0000 | [diff] [blame] | 259 | /* Step back one character. */ |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 260 | openfile->current_x = move_mbleft(openfile->current->data, |
Benno Schulenberg | b410332 | 2015-09-05 09:40:09 +0000 | [diff] [blame] | 261 | openfile->current_x); |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 262 | |
Benno Schulenberg | b410332 | 2015-09-05 09:40:09 +0000 | [diff] [blame] | 263 | if (is_word_mbchar(openfile->current->data + openfile->current_x, |
| 264 | allow_punct)) { |
| 265 | seen_a_word = TRUE; |
| 266 | /* If at the head of a line now, this surely is a word start. */ |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 267 | if (openfile->current_x == 0) |
Benno Schulenberg | b410332 | 2015-09-05 09:40:09 +0000 | [diff] [blame] | 268 | break; |
| 269 | } else if (seen_a_word) { |
| 270 | /* This is space now: we've overshot the start of the word. */ |
| 271 | step_forward = TRUE; |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 272 | break; |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 273 | } |
| 274 | } |
| 275 | |
Benno Schulenberg | b410332 | 2015-09-05 09:40:09 +0000 | [diff] [blame] | 276 | if (step_forward) |
| 277 | /* Move one character forward again to sit on the start of the word. */ |
| 278 | openfile->current_x = move_mbright(openfile->current->data, |
| 279 | openfile->current_x); |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 280 | |
| 281 | /* If allow_update is TRUE, update the screen. */ |
Benno Schulenberg | 318ed6b | 2016-04-12 10:24:57 +0200 | [diff] [blame] | 282 | if (allow_update) { |
| 283 | focusing = FALSE; |
Benno Schulenberg | 7013039 | 2016-10-12 21:10:04 +0200 | [diff] [blame] | 284 | edit_redraw(was_current); |
Benno Schulenberg | 318ed6b | 2016-04-12 10:24:57 +0200 | [diff] [blame] | 285 | } |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 286 | } |
| 287 | |
Benno Schulenberg | c115166 | 2016-02-22 12:49:08 +0000 | [diff] [blame] | 288 | /* Move to the previous word in the file, treating punctuation as part of a |
| 289 | * word if the WORD_BOUNDS flag is set, and update the screen afterwards. */ |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 290 | void do_prev_word_void(void) |
| 291 | { |
David Lawrence Ramsey | 4f03daf | 2005-08-10 22:12:28 +0000 | [diff] [blame] | 292 | do_prev_word(ISSET(WORD_BOUNDS), TRUE); |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 293 | } |
Benno Schulenberg | c115166 | 2016-02-22 12:49:08 +0000 | [diff] [blame] | 294 | |
| 295 | /* Move to the next word in the file. If allow_punct is TRUE, treat |
| 296 | * punctuation as part of a word. If allow_update is TRUE, update the |
| 297 | * screen afterwards. Return TRUE if we started on a word, and FALSE |
| 298 | * otherwise. */ |
| 299 | bool do_next_word(bool allow_punct, bool allow_update) |
| 300 | { |
Benno Schulenberg | 7013039 | 2016-10-12 21:10:04 +0200 | [diff] [blame] | 301 | filestruct *was_current = openfile->current; |
Benno Schulenberg | c115166 | 2016-02-22 12:49:08 +0000 | [diff] [blame] | 302 | bool started_on_word = is_word_mbchar(openfile->current->data + |
| 303 | openfile->current_x, allow_punct); |
| 304 | bool seen_space = !started_on_word; |
| 305 | |
| 306 | assert(openfile->current != NULL && openfile->current->data != NULL); |
| 307 | |
| 308 | /* Move forward until we reach the start of a word. */ |
| 309 | while (TRUE) { |
| 310 | /* If at the end of a line, move to the beginning of the next one. */ |
| 311 | if (openfile->current->data[openfile->current_x] == '\0') { |
| 312 | /* If at the end of the file, stop. */ |
| 313 | if (openfile->current->next == NULL) |
| 314 | break; |
| 315 | openfile->current = openfile->current->next; |
| 316 | openfile->current_x = 0; |
| 317 | seen_space = TRUE; |
| 318 | } else { |
| 319 | /* Step forward one character. */ |
| 320 | openfile->current_x = move_mbright(openfile->current->data, |
| 321 | openfile->current_x); |
| 322 | } |
| 323 | |
| 324 | /* If this is not a word character, then it's a separator; else |
| 325 | * if we've already seen a separator, then it's a word start. */ |
| 326 | if (!is_word_mbchar(openfile->current->data + openfile->current_x, |
| 327 | allow_punct)) |
| 328 | seen_space = TRUE; |
| 329 | else if (seen_space) |
| 330 | break; |
| 331 | } |
| 332 | |
Benno Schulenberg | c115166 | 2016-02-22 12:49:08 +0000 | [diff] [blame] | 333 | /* If allow_update is TRUE, update the screen. */ |
Benno Schulenberg | 318ed6b | 2016-04-12 10:24:57 +0200 | [diff] [blame] | 334 | if (allow_update) { |
| 335 | focusing = FALSE; |
Benno Schulenberg | 7013039 | 2016-10-12 21:10:04 +0200 | [diff] [blame] | 336 | edit_redraw(was_current); |
Benno Schulenberg | 318ed6b | 2016-04-12 10:24:57 +0200 | [diff] [blame] | 337 | } |
Benno Schulenberg | c115166 | 2016-02-22 12:49:08 +0000 | [diff] [blame] | 338 | |
| 339 | /* Return whether we started on a word. */ |
| 340 | return started_on_word; |
| 341 | } |
| 342 | |
| 343 | /* Move to the next word in the file, treating punctuation as part of a word |
| 344 | * if the WORD_BOUNDS flag is set, and update the screen afterwards. */ |
| 345 | void do_next_word_void(void) |
| 346 | { |
| 347 | do_next_word(ISSET(WORD_BOUNDS), TRUE); |
| 348 | } |
David Lawrence Ramsey | c4037f3 | 2005-07-20 21:31:19 +0000 | [diff] [blame] | 349 | |
Benno Schulenberg | 43f35fc | 2016-10-18 13:03:01 +0200 | [diff] [blame] | 350 | /* Make sure that the current line, when it is partially scrolled off the |
| 351 | * screen in softwrap mode, is scrolled fully into view. */ |
| 352 | void ensure_line_is_visible(void) |
| 353 | { |
| 354 | #ifndef NANO_TINY |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 355 | if (ISSET(SOFTWRAP) && strlenpt(openfile->current->data) / editwincols + |
Benno Schulenberg | 43f35fc | 2016-10-18 13:03:01 +0200 | [diff] [blame] | 356 | openfile->current_y >= editwinrows) { |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 357 | adjust_viewport(ISSET(SMOOTH_SCROLL) ? FLOWING : CENTERING); |
Benno Schulenberg | 43f35fc | 2016-10-18 13:03:01 +0200 | [diff] [blame] | 358 | refresh_needed = TRUE; |
| 359 | } |
| 360 | #endif |
| 361 | } |
| 362 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 363 | /* Move to the beginning of the current line. If the SMART_HOME flag is |
| 364 | * set, move to the first non-whitespace character of the current line |
David Lawrence Ramsey | 9ea0590 | 2006-04-24 19:44:04 +0000 | [diff] [blame] | 365 | * if we aren't already there, or to the beginning of the current line |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 366 | * if we are. */ |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 367 | void do_home(void) |
| 368 | { |
Benno Schulenberg | c9e9964 | 2016-07-26 11:47:53 +0200 | [diff] [blame] | 369 | size_t was_column = xplustabs(); |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 370 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 371 | #ifndef NANO_TINY |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 372 | if (ISSET(SMART_HOME)) { |
| 373 | size_t current_x_save = openfile->current_x; |
| 374 | |
| 375 | openfile->current_x = indent_length(openfile->current->data); |
| 376 | |
| 377 | if (openfile->current_x == current_x_save || |
| 378 | openfile->current->data[openfile->current_x] == '\0') |
| 379 | openfile->current_x = 0; |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 380 | } else |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 381 | #endif |
| 382 | openfile->current_x = 0; |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 383 | |
| 384 | openfile->placewewant = xplustabs(); |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 385 | |
Benno Schulenberg | 2f66476 | 2016-07-26 19:47:00 +0200 | [diff] [blame] | 386 | if (need_horizontal_scroll(was_column, openfile->placewewant)) |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 387 | update_line(openfile->current, openfile->current_x); |
| 388 | } |
| 389 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 390 | /* Move to the end of the current line. */ |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 391 | void do_end(void) |
| 392 | { |
Benno Schulenberg | c9e9964 | 2016-07-26 11:47:53 +0200 | [diff] [blame] | 393 | size_t was_column = xplustabs(); |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 394 | |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 395 | openfile->current_x = strlen(openfile->current->data); |
| 396 | openfile->placewewant = xplustabs(); |
| 397 | |
Benno Schulenberg | 2f66476 | 2016-07-26 19:47:00 +0200 | [diff] [blame] | 398 | if (need_horizontal_scroll(was_column, openfile->placewewant)) |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 399 | update_line(openfile->current, openfile->current_x); |
Benno Schulenberg | 43f35fc | 2016-10-18 13:03:01 +0200 | [diff] [blame] | 400 | |
| 401 | ensure_line_is_visible(); |
David Lawrence Ramsey | ca62f9f | 2005-07-20 19:24:11 +0000 | [diff] [blame] | 402 | } |
| 403 | |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 404 | /* If scroll_only is FALSE, move up one line. If scroll_only is TRUE, |
| 405 | * scroll up one line without scrolling the cursor. */ |
Benno Schulenberg | 0a3a644 | 2016-07-25 17:23:45 +0200 | [diff] [blame] | 406 | void do_up(bool scroll_only) |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 407 | { |
Benno Schulenberg | c9e9964 | 2016-07-26 11:47:53 +0200 | [diff] [blame] | 408 | size_t was_column = xplustabs(); |
| 409 | |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 410 | /* If we're at the top of the file, or if scroll_only is TRUE and |
| 411 | * the top of the file is onscreen, get out. */ |
Benno Schulenberg | a4044a7 | 2016-12-09 13:31:04 +0100 | [diff] [blame] | 412 | if (openfile->current == openfile->fileage || |
| 413 | (scroll_only && openfile->edittop == openfile->fileage)) |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 414 | return; |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 415 | |
Chris Allegretta | bce7d40 | 2009-09-03 23:29:14 +0000 | [diff] [blame] | 416 | assert(ISSET(SOFTWRAP) || openfile->current_y == openfile->current->lineno - openfile->edittop->lineno); |
David Lawrence Ramsey | b199cae | 2005-03-09 20:35:10 +0000 | [diff] [blame] | 417 | |
David Lawrence Ramsey | 8f4762a | 2005-07-16 22:35:11 +0000 | [diff] [blame] | 418 | /* Move the current line of the edit window up. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 419 | openfile->current = openfile->current->prev; |
| 420 | openfile->current_x = actual_x(openfile->current->data, |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 421 | openfile->placewewant); |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 422 | |
Benno Schulenberg | 4c476bc | 2016-10-28 11:37:03 +0200 | [diff] [blame] | 423 | /* When the cursor was on the first line of the edit window (or when just |
| 424 | * scrolling without moving the cursor), scroll the edit window up -- one |
| 425 | * line if we're in smooth scrolling mode, and half a page otherwise. */ |
| 426 | if (openfile->current->next == openfile->edittop || scroll_only) |
Benno Schulenberg | 5416b9c | 2016-10-24 13:56:00 +0200 | [diff] [blame] | 427 | edit_scroll(UPWARD, (ISSET(SMOOTH_SCROLL) || scroll_only) ? |
| 428 | 1 : editwinrows / 2 + 1); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 429 | |
Benno Schulenberg | 03b168d | 2016-07-26 20:06:10 +0200 | [diff] [blame] | 430 | /* If the lines weren't already redrawn, see if they need to be. */ |
| 431 | if (openfile->current_y > 0) { |
| 432 | /* Redraw the prior line if it was horizontally scrolled. */ |
| 433 | if (need_horizontal_scroll(was_column, 0)) |
| 434 | update_line(openfile->current->next, 0); |
| 435 | /* Redraw the current line if it needs to be horizontally scrolled. */ |
| 436 | if (need_horizontal_scroll(0, xplustabs())) |
| 437 | update_line(openfile->current, openfile->current_x); |
| 438 | } |
David Lawrence Ramsey | 2de84c1 | 2005-10-24 02:12:09 +0000 | [diff] [blame] | 439 | } |
David Lawrence Ramsey | 2de84c1 | 2005-10-24 02:12:09 +0000 | [diff] [blame] | 440 | |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 441 | /* Move up one line. */ |
| 442 | void do_up_void(void) |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 443 | { |
Benno Schulenberg | 0a3a644 | 2016-07-25 17:23:45 +0200 | [diff] [blame] | 444 | do_up(FALSE); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 445 | } |
| 446 | |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 447 | /* If scroll_only is FALSE, move down one line. If scroll_only is TRUE, |
| 448 | * scroll down one line without scrolling the cursor. */ |
Benno Schulenberg | 0a3a644 | 2016-07-25 17:23:45 +0200 | [diff] [blame] | 449 | void do_down(bool scroll_only) |
David Lawrence Ramsey | 2de84c1 | 2005-10-24 02:12:09 +0000 | [diff] [blame] | 450 | { |
Benno Schulenberg | 54a1105 | 2014-03-03 13:24:09 +0000 | [diff] [blame] | 451 | #ifndef NANO_TINY |
Benno Schulenberg | 3278f83 | 2014-03-05 09:47:35 +0000 | [diff] [blame] | 452 | int amount = 0, enough; |
Benno Schulenberg | b1a7fdd | 2014-03-03 10:02:13 +0000 | [diff] [blame] | 453 | filestruct *topline; |
Benno Schulenberg | 54a1105 | 2014-03-03 13:24:09 +0000 | [diff] [blame] | 454 | #endif |
Benno Schulenberg | c9e9964 | 2016-07-26 11:47:53 +0200 | [diff] [blame] | 455 | size_t was_column = xplustabs(); |
Chris Allegretta | 139934a | 2009-08-30 03:50:16 +0000 | [diff] [blame] | 456 | |
David Lawrence Ramsey | 2de84c1 | 2005-10-24 02:12:09 +0000 | [diff] [blame] | 457 | /* If we're at the bottom of the file, get out. */ |
Benno Schulenberg | 5834638 | 2015-12-08 18:54:13 +0000 | [diff] [blame] | 458 | if (openfile->current == openfile->filebot) |
David Lawrence Ramsey | 2de84c1 | 2005-10-24 02:12:09 +0000 | [diff] [blame] | 459 | return; |
| 460 | |
Benno Schulenberg | 5834638 | 2015-12-08 18:54:13 +0000 | [diff] [blame] | 461 | assert(ISSET(SOFTWRAP) || openfile->current_y == |
| 462 | openfile->current->lineno - openfile->edittop->lineno); |
| 463 | assert(openfile->current->next != NULL); |
David Lawrence Ramsey | 2de84c1 | 2005-10-24 02:12:09 +0000 | [diff] [blame] | 464 | |
Benno Schulenberg | 54a1105 | 2014-03-03 13:24:09 +0000 | [diff] [blame] | 465 | #ifndef NANO_TINY |
Chris Allegretta | 139934a | 2009-08-30 03:50:16 +0000 | [diff] [blame] | 466 | if (ISSET(SOFTWRAP)) { |
Benno Schulenberg | 2d50c4f | 2016-04-15 14:39:54 +0200 | [diff] [blame] | 467 | /* Compute the number of lines to scroll. */ |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 468 | amount = strlenpt(openfile->current->data) / editwincols - |
| 469 | xplustabs() / editwincols + |
| 470 | strlenpt(openfile->current->next->data) / editwincols + |
Benno Schulenberg | 2d50c4f | 2016-04-15 14:39:54 +0200 | [diff] [blame] | 471 | openfile->current_y - editwinrows + 2; |
Benno Schulenberg | b1a7fdd | 2014-03-03 10:02:13 +0000 | [diff] [blame] | 472 | topline = openfile->edittop; |
| 473 | /* Reduce the amount when there are overlong lines at the top. */ |
| 474 | for (enough = 1; enough < amount; enough++) { |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 475 | amount -= strlenpt(topline->data) / editwincols; |
Benno Schulenberg | 97f6ae5 | 2016-09-14 12:40:58 +0200 | [diff] [blame] | 476 | if (amount > 0) |
| 477 | topline = topline->next; |
| 478 | if (amount < enough) { |
Benno Schulenberg | b1a7fdd | 2014-03-03 10:02:13 +0000 | [diff] [blame] | 479 | amount = enough; |
| 480 | break; |
| 481 | } |
Benno Schulenberg | b1a7fdd | 2014-03-03 10:02:13 +0000 | [diff] [blame] | 482 | } |
Chris Allegretta | 139934a | 2009-08-30 03:50:16 +0000 | [diff] [blame] | 483 | } |
Benno Schulenberg | 54a1105 | 2014-03-03 13:24:09 +0000 | [diff] [blame] | 484 | #endif |
Chris Allegretta | 139934a | 2009-08-30 03:50:16 +0000 | [diff] [blame] | 485 | |
Benno Schulenberg | 2d50c4f | 2016-04-15 14:39:54 +0200 | [diff] [blame] | 486 | /* Move to the next line in the file. */ |
| 487 | openfile->current = openfile->current->next; |
| 488 | openfile->current_x = actual_x(openfile->current->data, |
| 489 | openfile->placewewant); |
| 490 | |
David Lawrence Ramsey | f2ac201 | 2016-12-28 09:43:23 -0600 | [diff] [blame] | 491 | /* When the cursor was on the last line of the edit window (or when just |
| 492 | * scrolling without moving the cursor), scroll the edit window down -- one |
| 493 | * line if we're in smooth scrolling mode, and half a page otherwise. */ |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 494 | #ifndef NANO_TINY |
Benno Schulenberg | 77a1a5c | 2016-09-13 22:06:11 +0200 | [diff] [blame] | 495 | if (openfile->current_y == editwinrows - 1 || amount > 0 || scroll_only) { |
Benno Schulenberg | 54a1105 | 2014-03-03 13:24:09 +0000 | [diff] [blame] | 496 | if (amount < 1 || scroll_only) |
| 497 | amount = 1; |
Benno Schulenberg | 77a1a5c | 2016-09-13 22:06:11 +0200 | [diff] [blame] | 498 | |
| 499 | edit_scroll(DOWNWARD, (ISSET(SMOOTH_SCROLL) || scroll_only) ? |
| 500 | amount : editwinrows / 2 + 1); |
Benno Schulenberg | 503654e | 2016-04-12 14:00:36 +0200 | [diff] [blame] | 501 | |
| 502 | if (ISSET(SOFTWRAP)) { |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 503 | refresh_needed = TRUE; |
Benno Schulenberg | 503654e | 2016-04-12 14:00:36 +0200 | [diff] [blame] | 504 | return; |
| 505 | } |
Benno Schulenberg | 8476bf8 | 2016-10-15 18:05:01 +0200 | [diff] [blame] | 506 | } |
Benno Schulenberg | 77a1a5c | 2016-09-13 22:06:11 +0200 | [diff] [blame] | 507 | #else |
| 508 | if (openfile->current_y == editwinrows - 1) |
| 509 | edit_scroll(DOWNWARD, editwinrows / 2 + 1); |
| 510 | #endif |
Benno Schulenberg | e393a6c | 2016-04-12 13:29:51 +0200 | [diff] [blame] | 511 | |
Benno Schulenberg | 03b168d | 2016-07-26 20:06:10 +0200 | [diff] [blame] | 512 | /* If the lines weren't already redrawn, see if they need to be. */ |
| 513 | if (openfile->current_y < editwinrows - 1) { |
| 514 | /* Redraw the prior line if it was horizontally scrolled. */ |
| 515 | if (need_horizontal_scroll(was_column, 0)) |
| 516 | update_line(openfile->current->prev, 0); |
| 517 | /* Redraw the current line if it needs to be horizontally scrolled. */ |
| 518 | if (need_horizontal_scroll(0, xplustabs())) |
| 519 | update_line(openfile->current, openfile->current_x); |
| 520 | } |
David Lawrence Ramsey | 2de84c1 | 2005-10-24 02:12:09 +0000 | [diff] [blame] | 521 | } |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 522 | |
| 523 | /* Move down one line. */ |
| 524 | void do_down_void(void) |
| 525 | { |
Benno Schulenberg | 0a3a644 | 2016-07-25 17:23:45 +0200 | [diff] [blame] | 526 | do_down(FALSE); |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 527 | } |
| 528 | |
| 529 | #ifndef NANO_TINY |
Benno Schulenberg | 0a3a644 | 2016-07-25 17:23:45 +0200 | [diff] [blame] | 530 | /* Scroll up one line without scrolling the cursor. */ |
| 531 | void do_scroll_up(void) |
| 532 | { |
| 533 | do_up(TRUE); |
| 534 | } |
| 535 | |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 536 | /* Scroll down one line without scrolling the cursor. */ |
| 537 | void do_scroll_down(void) |
| 538 | { |
| 539 | do_down(TRUE); |
| 540 | } |
| 541 | #endif |
David Lawrence Ramsey | 2de84c1 | 2005-10-24 02:12:09 +0000 | [diff] [blame] | 542 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 543 | /* Move left one character. */ |
David Lawrence Ramsey | 1c3bfa9 | 2005-09-13 04:53:44 +0000 | [diff] [blame] | 544 | void do_left(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 545 | { |
Benno Schulenberg | c9e9964 | 2016-07-26 11:47:53 +0200 | [diff] [blame] | 546 | size_t was_column = xplustabs(); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 547 | |
| 548 | if (openfile->current_x > 0) |
| 549 | openfile->current_x = move_mbleft(openfile->current->data, |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 550 | openfile->current_x); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 551 | else if (openfile->current != openfile->fileage) { |
David Lawrence Ramsey | 2c36e2e | 2006-07-06 22:17:47 +0000 | [diff] [blame] | 552 | do_up_void(); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 553 | openfile->current_x = strlen(openfile->current->data); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 554 | } |
David Lawrence Ramsey | f00c961 | 2005-07-14 18:01:08 +0000 | [diff] [blame] | 555 | |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 556 | openfile->placewewant = xplustabs(); |
David Lawrence Ramsey | f00c961 | 2005-07-14 18:01:08 +0000 | [diff] [blame] | 557 | |
Benno Schulenberg | 2f66476 | 2016-07-26 19:47:00 +0200 | [diff] [blame] | 558 | if (need_horizontal_scroll(was_column, openfile->placewewant)) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 559 | update_line(openfile->current, openfile->current_x); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 560 | } |
| 561 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 562 | /* Move right one character. */ |
David Lawrence Ramsey | 1c3bfa9 | 2005-09-13 04:53:44 +0000 | [diff] [blame] | 563 | void do_right(void) |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 564 | { |
Benno Schulenberg | c9e9964 | 2016-07-26 11:47:53 +0200 | [diff] [blame] | 565 | size_t was_column = xplustabs(); |
David Lawrence Ramsey | f00c961 | 2005-07-14 18:01:08 +0000 | [diff] [blame] | 566 | |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 567 | assert(openfile->current_x <= strlen(openfile->current->data)); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 568 | |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 569 | if (openfile->current->data[openfile->current_x] != '\0') |
| 570 | openfile->current_x = move_mbright(openfile->current->data, |
Benno Schulenberg | 2fae87d | 2016-05-27 21:22:56 +0200 | [diff] [blame] | 571 | openfile->current_x); |
Benno Schulenberg | d66ea08 | 2016-10-18 11:46:15 +0200 | [diff] [blame] | 572 | else if (openfile->current != openfile->filebot) { |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 573 | openfile->current_x = 0; |
Benno Schulenberg | d66ea08 | 2016-10-18 11:46:15 +0200 | [diff] [blame] | 574 | #ifndef NANO_TINY |
| 575 | if (ISSET(SOFTWRAP)) |
David Lawrence Ramsey | 6263416 | 2016-12-07 12:33:25 -0600 | [diff] [blame] | 576 | openfile->current_y -= strlenpt(openfile->current->data) / editwincols; |
Benno Schulenberg | d66ea08 | 2016-10-18 11:46:15 +0200 | [diff] [blame] | 577 | #endif |
| 578 | } |
David Lawrence Ramsey | f00c961 | 2005-07-14 18:01:08 +0000 | [diff] [blame] | 579 | |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 580 | openfile->placewewant = xplustabs(); |
David Lawrence Ramsey | f00c961 | 2005-07-14 18:01:08 +0000 | [diff] [blame] | 581 | |
Benno Schulenberg | 2f66476 | 2016-07-26 19:47:00 +0200 | [diff] [blame] | 582 | if (need_horizontal_scroll(was_column, openfile->placewewant)) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 583 | update_line(openfile->current, openfile->current_x); |
Benno Schulenberg | 8041627 | 2016-09-20 12:53:12 +0200 | [diff] [blame] | 584 | |
| 585 | if (openfile->current_x == 0) |
| 586 | do_down_void(); |
Benno Schulenberg | 43f35fc | 2016-10-18 13:03:01 +0200 | [diff] [blame] | 587 | else |
| 588 | ensure_line_is_visible(); |
Chris Allegretta | bceb1b2 | 2000-06-19 04:22:15 +0000 | [diff] [blame] | 589 | } |