David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1 | /************************************************************************** |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 2 | * text.c -- This file is part of GNU nano. * |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +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 Mark Majeres * |
| 7 | * Copyright (C) 2016 Mike Scalora * |
| 8 | * Copyright (C) 2015, 2016 Benno Schulenberg * |
Benno Schulenberg | 7531b71 | 2016-12-07 20:37:36 +0100 | [diff] [blame] | 9 | * Copyright (C) 2016 Sumedh Pendurkar * |
Benno Schulenberg | 406e524 | 2016-08-29 15:14:18 +0200 | [diff] [blame] | 10 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 11 | * GNU nano is free software: you can redistribute it and/or modify * |
| 12 | * it under the terms of the GNU General Public License as published * |
| 13 | * by the Free Software Foundation, either version 3 of the License, * |
| 14 | * or (at your option) any later version. * |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 15 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 16 | * GNU nano is distributed in the hope that it will be useful, * |
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty * |
| 18 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 19 | * See the GNU General Public License for more details. * |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 20 | * * |
| 21 | * You should have received a copy of the GNU General Public License * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 22 | * along with this program. If not, see http://www.gnu.org/licenses/. * |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 23 | * * |
| 24 | **************************************************************************/ |
| 25 | |
David Lawrence Ramsey | 034b994 | 2005-12-08 02:47:10 +0000 | [diff] [blame] | 26 | #include "proto.h" |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 27 | |
David Lawrence Ramsey | ee11c6a | 2005-11-02 19:42:02 +0000 | [diff] [blame] | 28 | #include <stdio.h> |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 29 | #include <signal.h> |
| 30 | #include <unistd.h> |
| 31 | #include <string.h> |
| 32 | #include <fcntl.h> |
| 33 | #include <sys/wait.h> |
| 34 | #include <errno.h> |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 35 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 36 | #ifndef NANO_TINY |
David Lawrence Ramsey | 8779a17 | 2005-11-08 16:45:22 +0000 | [diff] [blame] | 37 | static pid_t pid = -1; |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 38 | /* The PID of the forked process in execute_command(), for use |
| 39 | * with the cancel_command() signal handler. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 40 | #endif |
| 41 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 42 | static bool prepend_wrap = FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 43 | /* Should we prepend wrapped text to the next line? */ |
| 44 | #endif |
| 45 | #ifndef DISABLE_JUSTIFY |
Benno Schulenberg | 3264d0c | 2016-10-12 19:59:26 +0200 | [diff] [blame] | 46 | static filestruct *jusbuffer = NULL; |
| 47 | /* The buffer where we store unjustified text. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 48 | static filestruct *jusbottom = NULL; |
Benno Schulenberg | 3264d0c | 2016-10-12 19:59:26 +0200 | [diff] [blame] | 49 | /* A pointer to the end of the buffer with unjustified text. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 50 | #endif |
| 51 | |
Benno Schulenberg | 68a0314 | 2016-12-07 13:10:40 +0100 | [diff] [blame] | 52 | #ifdef ENABLE_WORDCOMPLETION |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 53 | static int pletion_x = 0; |
| 54 | /* The x position in pletion_line of the last found completion. */ |
| 55 | static completion_word *list_of_completions; |
| 56 | /* A linked list of the completions that have been attepmted. */ |
| 57 | #endif |
| 58 | |
| 59 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 60 | /* Toggle the mark. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 61 | void do_mark(void) |
| 62 | { |
| 63 | openfile->mark_set = !openfile->mark_set; |
Benno Schulenberg | 382c9d7 | 2016-04-24 11:28:28 +0200 | [diff] [blame] | 64 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 65 | if (openfile->mark_set) { |
| 66 | statusbar(_("Mark Set")); |
| 67 | openfile->mark_begin = openfile->current; |
| 68 | openfile->mark_begin_x = openfile->current_x; |
Benno Schulenberg | 382c9d7 | 2016-04-24 11:28:28 +0200 | [diff] [blame] | 69 | openfile->kind_of_mark = HARDMARK; |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 70 | } else { |
David Lawrence Ramsey | 7b0531a | 2006-07-31 01:30:31 +0000 | [diff] [blame] | 71 | statusbar(_("Mark Unset")); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 72 | openfile->mark_begin = NULL; |
| 73 | openfile->mark_begin_x = 0; |
Benno Schulenberg | f920e0d | 2016-12-03 17:00:28 +0100 | [diff] [blame] | 74 | refresh_needed = TRUE; |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 75 | } |
| 76 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 77 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 78 | |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 79 | #if !defined(DISABLE_COLOR) || !defined(DISABLE_SPELLER) |
| 80 | /* Return an error message containing the given name. */ |
| 81 | char *invocation_error(const char *name) |
| 82 | { |
| 83 | char *message, *invoke_error = _("Error invoking \"%s\""); |
| 84 | |
| 85 | message = charalloc(strlen(invoke_error) + strlen(name) + 1); |
| 86 | sprintf(message, invoke_error, name); |
| 87 | return message; |
| 88 | } |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 89 | #endif |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 90 | |
David Lawrence Ramsey | f1982f0 | 2006-12-10 17:57:09 +0000 | [diff] [blame] | 91 | /* Delete the character under the cursor. */ |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 92 | void do_deletion(undo_type action) |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 93 | { |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 94 | #ifndef NANO_TINY |
David Lawrence Ramsey | 83ff644 | 2017-01-19 19:58:37 -0600 | [diff] [blame] | 95 | size_t orig_rows = 0; |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 96 | #endif |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 97 | |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 98 | assert(openfile->current != NULL && openfile->current->data != NULL && |
| 99 | openfile->current_x <= strlen(openfile->current->data)); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 100 | |
| 101 | openfile->placewewant = xplustabs(); |
| 102 | |
| 103 | if (openfile->current->data[openfile->current_x] != '\0') { |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 104 | /* We're in the middle of a line: delete the current character. */ |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 105 | int char_len = parse_mbchar(openfile->current->data + |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 106 | openfile->current_x, NULL, NULL); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 107 | size_t line_len = strlen(openfile->current->data + |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 108 | openfile->current_x); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 109 | |
| 110 | assert(openfile->current_x < strlen(openfile->current->data)); |
| 111 | |
Benno Schulenberg | ff36b05 | 2014-05-29 18:50:13 +0000 | [diff] [blame] | 112 | #ifndef NANO_TINY |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 113 | update_undo(action); |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 114 | |
Chris Allegretta | a8bc492 | 2009-12-12 22:21:20 +0000 | [diff] [blame] | 115 | if (ISSET(SOFTWRAP)) |
David Lawrence Ramsey | 83ff644 | 2017-01-19 19:58:37 -0600 | [diff] [blame] | 116 | orig_rows = strlenpt(openfile->current->data) / editwincols; |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 117 | #endif |
Chris Allegretta | a8bc492 | 2009-12-12 22:21:20 +0000 | [diff] [blame] | 118 | |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 119 | /* Move the remainder of the line "in", over the current character. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 120 | charmove(&openfile->current->data[openfile->current_x], |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 121 | &openfile->current->data[openfile->current_x + char_len], |
| 122 | line_len - char_len + 1); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 123 | null_at(&openfile->current->data, openfile->current_x + |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 124 | line_len - char_len); |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 125 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 126 | #ifndef NANO_TINY |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 127 | /* Adjust the mark if it is after the cursor on the current line. */ |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 128 | if (openfile->mark_set && openfile->mark_begin == openfile->current && |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 129 | openfile->mark_begin_x > openfile->current_x) |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 130 | openfile->mark_begin_x -= char_len; |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 131 | #endif |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 132 | /* Adjust the file size. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 133 | openfile->totsize--; |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 134 | } else if (openfile->current != openfile->filebot) { |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 135 | /* We're at the end of a line and not at the end of the file: join |
| 136 | * this line with the next. */ |
| 137 | filestruct *joining = openfile->current->next; |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 138 | |
| 139 | assert(openfile->current_x == strlen(openfile->current->data)); |
| 140 | |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 141 | /* If there is a magic line, and we're before it: don't eat it. */ |
| 142 | if (joining == openfile->filebot && openfile->current_x != 0 && |
| 143 | !ISSET(NO_NEWLINES)) { |
Benno Schulenberg | fc7825d | 2015-07-06 19:08:13 +0000 | [diff] [blame] | 144 | #ifndef NANO_TINY |
| 145 | if (action == BACK) |
| 146 | add_undo(BACK); |
| 147 | #endif |
Benno Schulenberg | 8f5fa24 | 2015-07-06 18:48:15 +0000 | [diff] [blame] | 148 | return; |
Benno Schulenberg | fc7825d | 2015-07-06 19:08:13 +0000 | [diff] [blame] | 149 | } |
Benno Schulenberg | 8f5fa24 | 2015-07-06 18:48:15 +0000 | [diff] [blame] | 150 | |
Benno Schulenberg | ff36b05 | 2014-05-29 18:50:13 +0000 | [diff] [blame] | 151 | #ifndef NANO_TINY |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 152 | add_undo(action); |
Benno Schulenberg | ff36b05 | 2014-05-29 18:50:13 +0000 | [diff] [blame] | 153 | #endif |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 154 | /* Add the contents of the next line to those of the current one. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 155 | openfile->current->data = charealloc(openfile->current->data, |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 156 | strlen(openfile->current->data) + strlen(joining->data) + 1); |
| 157 | strcat(openfile->current->data, joining->data); |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 158 | |
Benno Schulenberg | 5f4d759 | 2016-06-06 19:57:51 +0200 | [diff] [blame] | 159 | /* Adjust the file size. */ |
| 160 | openfile->totsize--; |
Benno Schulenberg | edf230b | 2016-06-04 11:35:11 +0200 | [diff] [blame] | 161 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 162 | #ifndef NANO_TINY |
Benno Schulenberg | 5f4d759 | 2016-06-06 19:57:51 +0200 | [diff] [blame] | 163 | /* Remember the new file size for a possible redo. */ |
| 164 | openfile->current_undo->newsize = openfile->totsize; |
| 165 | |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 166 | /* Adjust the mark if it was on the line that was "eaten". */ |
| 167 | if (openfile->mark_set && openfile->mark_begin == joining) { |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 168 | openfile->mark_begin = openfile->current; |
| 169 | openfile->mark_begin_x += openfile->current_x; |
| 170 | } |
| 171 | #endif |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 172 | unlink_node(joining); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 173 | renumber(openfile->current); |
Benno Schulenberg | 57f52a8 | 2015-06-20 09:42:26 +0000 | [diff] [blame] | 174 | |
Benno Schulenberg | 296ff1e | 2015-06-28 14:04:03 +0000 | [diff] [blame] | 175 | /* Two lines were joined, so we need to refresh the screen. */ |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 176 | refresh_needed = TRUE; |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 177 | } else |
Benno Schulenberg | f0c65bc | 2016-06-06 20:29:53 +0200 | [diff] [blame] | 178 | /* We're at the end-of-file: nothing to do. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 179 | return; |
| 180 | |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 181 | #ifndef NANO_TINY |
Benno Schulenberg | 43f35fc | 2016-10-18 13:03:01 +0200 | [diff] [blame] | 182 | ensure_line_is_visible(); |
| 183 | |
Benno Schulenberg | 7598b77 | 2016-11-12 15:57:42 +0100 | [diff] [blame] | 184 | /* If the number of screen rows that a softwrapped line occupies |
| 185 | * has changed, we need a full refresh. */ |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 186 | if (ISSET(SOFTWRAP) && refresh_needed == FALSE) |
David Lawrence Ramsey | 83ff644 | 2017-01-19 19:58:37 -0600 | [diff] [blame] | 187 | if ((strlenpt(openfile->current->data) / editwincols) != orig_rows) |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 188 | refresh_needed = TRUE; |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 189 | #endif |
Chris Allegretta | a8bc492 | 2009-12-12 22:21:20 +0000 | [diff] [blame] | 190 | |
David Lawrence Ramsey | 0f6236f | 2005-11-09 00:08:29 +0000 | [diff] [blame] | 191 | set_modified(); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 194 | /* Delete the character under the cursor. */ |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 195 | void do_delete(void) |
| 196 | { |
| 197 | do_deletion(DEL); |
| 198 | } |
| 199 | |
David Lawrence Ramsey | 5b7b3e3 | 2005-12-31 21:22:54 +0000 | [diff] [blame] | 200 | /* Backspace over one character. That is, move the cursor left one |
David Lawrence Ramsey | 84d22e3 | 2006-11-08 13:05:50 +0000 | [diff] [blame] | 201 | * character, and then delete the character under the cursor. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 202 | void do_backspace(void) |
| 203 | { |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 204 | if (openfile->current != openfile->fileage || openfile->current_x > 0) { |
David Lawrence Ramsey | 1c3bfa9 | 2005-09-13 04:53:44 +0000 | [diff] [blame] | 205 | do_left(); |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 206 | do_deletion(BACK); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 207 | } |
| 208 | } |
| 209 | |
Benno Schulenberg | b3e4051 | 2015-07-31 11:52:26 +0000 | [diff] [blame] | 210 | #ifndef NANO_TINY |
| 211 | /* Delete text from the cursor until the first start of a word to |
| 212 | * the right, or to the left when backward is true. */ |
| 213 | void do_cutword(bool backward) |
| 214 | { |
| 215 | /* Remember the current cursor position. */ |
| 216 | filestruct *is_current = openfile->current; |
| 217 | size_t is_current_x = openfile->current_x; |
| 218 | |
Benno Schulenberg | e2b6572 | 2016-03-20 11:10:31 +0000 | [diff] [blame] | 219 | /* Remember where the cutbuffer is and then make it seem blank. */ |
| 220 | filestruct *is_cutbuffer = cutbuffer; |
| 221 | filestruct *is_cutbottom = cutbottom; |
| 222 | cutbuffer = NULL; |
| 223 | cutbottom = NULL; |
| 224 | |
Benno Schulenberg | b3e4051 | 2015-07-31 11:52:26 +0000 | [diff] [blame] | 225 | /* Move the cursor to a word start, to the left or to the right. */ |
| 226 | if (backward) |
| 227 | do_prev_word(ISSET(WORD_BOUNDS), FALSE); |
| 228 | else |
| 229 | do_next_word(ISSET(WORD_BOUNDS), FALSE); |
| 230 | |
| 231 | /* Set the mark at the start of that word. */ |
| 232 | openfile->mark_begin = openfile->current; |
| 233 | openfile->mark_begin_x = openfile->current_x; |
| 234 | openfile->mark_set = TRUE; |
| 235 | |
| 236 | /* Put the cursor back where it was, so an undo will put it there too. */ |
| 237 | openfile->current = is_current; |
| 238 | openfile->current_x = is_current_x; |
| 239 | |
| 240 | /* Now kill the marked region and a word is gone. */ |
| 241 | do_cut_text_void(); |
Benno Schulenberg | e2b6572 | 2016-03-20 11:10:31 +0000 | [diff] [blame] | 242 | |
| 243 | /* Discard the cut word and restore the cutbuffer. */ |
| 244 | free_filestruct(cutbuffer); |
| 245 | cutbuffer = is_cutbuffer; |
| 246 | cutbottom = is_cutbottom; |
Benno Schulenberg | b3e4051 | 2015-07-31 11:52:26 +0000 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /* Delete a word leftward. */ |
| 250 | void do_cut_prev_word(void) |
| 251 | { |
Benno Schulenberg | 17cf833 | 2016-05-30 09:09:36 +0200 | [diff] [blame] | 252 | do_cutword(TRUE); |
Benno Schulenberg | b3e4051 | 2015-07-31 11:52:26 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* Delete a word rightward. */ |
| 256 | void do_cut_next_word(void) |
| 257 | { |
Benno Schulenberg | 17cf833 | 2016-05-30 09:09:36 +0200 | [diff] [blame] | 258 | do_cutword(FALSE); |
Benno Schulenberg | b3e4051 | 2015-07-31 11:52:26 +0000 | [diff] [blame] | 259 | } |
| 260 | #endif /* !NANO_TINY */ |
| 261 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 262 | /* Insert a tab. If the TABS_TO_SPACES flag is set, insert the number |
| 263 | * of spaces that a tab would normally take up. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 264 | void do_tab(void) |
| 265 | { |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 266 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 267 | if (ISSET(TABS_TO_SPACES)) { |
Benno Schulenberg | 68acc1d | 2016-06-24 10:17:06 +0200 | [diff] [blame] | 268 | char *spaces = charalloc(tabsize + 1); |
| 269 | size_t length = tabsize - (xplustabs() % tabsize); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 270 | |
Benno Schulenberg | 68acc1d | 2016-06-24 10:17:06 +0200 | [diff] [blame] | 271 | charset(spaces, ' ', length); |
| 272 | spaces[length] = '\0'; |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 273 | |
Benno Schulenberg | 68acc1d | 2016-06-24 10:17:06 +0200 | [diff] [blame] | 274 | do_output(spaces, length, TRUE); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 275 | |
Benno Schulenberg | 68acc1d | 2016-06-24 10:17:06 +0200 | [diff] [blame] | 276 | free(spaces); |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 277 | } else |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 278 | #endif |
Benno Schulenberg | 68acc1d | 2016-06-24 10:17:06 +0200 | [diff] [blame] | 279 | do_output((char *)"\t", 1, TRUE); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 280 | } |
| 281 | |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 282 | #ifndef NANO_TINY |
David Lawrence Ramsey | e44cd2d | 2007-01-11 22:54:55 +0000 | [diff] [blame] | 283 | /* Indent or unindent the current line (or, if the mark is on, all lines |
| 284 | * covered by the mark) len columns, depending on whether len is |
| 285 | * positive or negative. If the TABS_TO_SPACES flag is set, indent or |
| 286 | * unindent by len spaces. Otherwise, indent or unindent by (len / |
| 287 | * tabsize) tabs and (len % tabsize) spaces. */ |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 288 | void do_indent(ssize_t cols) |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 289 | { |
| 290 | bool indent_changed = FALSE; |
| 291 | /* Whether any indenting or unindenting was done. */ |
| 292 | bool unindent = FALSE; |
| 293 | /* Whether we're unindenting text. */ |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 294 | char *line_indent = NULL; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 295 | /* The text added to each line in order to indent it. */ |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 296 | size_t line_indent_len = 0; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 297 | /* The length of the text added to each line in order to indent |
| 298 | * it. */ |
| 299 | filestruct *top, *bot, *f; |
| 300 | size_t top_x, bot_x; |
| 301 | |
| 302 | assert(openfile->current != NULL && openfile->current->data != NULL); |
| 303 | |
David Lawrence Ramsey | af9052d | 2006-05-01 17:14:25 +0000 | [diff] [blame] | 304 | /* If cols is zero, get out. */ |
| 305 | if (cols == 0) |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 306 | return; |
| 307 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 308 | /* If cols is negative, make it positive and set unindent to TRUE. */ |
David Lawrence Ramsey | af9052d | 2006-05-01 17:14:25 +0000 | [diff] [blame] | 309 | if (cols < 0) { |
| 310 | cols = -cols; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 311 | unindent = TRUE; |
| 312 | /* Otherwise, we're indenting, in which case the file will always be |
| 313 | * modified, so set indent_changed to TRUE. */ |
| 314 | } else |
| 315 | indent_changed = TRUE; |
| 316 | |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 317 | /* If the mark is on, use all lines covered by the mark. */ |
| 318 | if (openfile->mark_set) |
| 319 | mark_order((const filestruct **)&top, &top_x, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 320 | (const filestruct **)&bot, &bot_x, NULL); |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 321 | /* Otherwise, use the current line. */ |
| 322 | else { |
| 323 | top = openfile->current; |
| 324 | bot = top; |
| 325 | } |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 326 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 327 | if (!unindent) { |
| 328 | /* Set up the text we'll be using as indentation. */ |
| 329 | line_indent = charalloc(cols + 1); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 330 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 331 | if (ISSET(TABS_TO_SPACES)) { |
| 332 | /* Set the indentation to cols spaces. */ |
| 333 | charset(line_indent, ' ', cols); |
| 334 | line_indent_len = cols; |
| 335 | } else { |
| 336 | /* Set the indentation to (cols / tabsize) tabs and (cols % |
| 337 | * tabsize) spaces. */ |
| 338 | size_t num_tabs = cols / tabsize; |
| 339 | size_t num_spaces = cols % tabsize; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 340 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 341 | charset(line_indent, '\t', num_tabs); |
| 342 | charset(line_indent + num_tabs, ' ', num_spaces); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 343 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 344 | line_indent_len = num_tabs + num_spaces; |
| 345 | } |
| 346 | |
| 347 | line_indent[line_indent_len] = '\0'; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 348 | } |
| 349 | |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 350 | /* Go through each line of the text. */ |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 351 | for (f = top; f != bot->next; f = f->next) { |
| 352 | size_t line_len = strlen(f->data); |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 353 | size_t indent_len = indent_length(f->data); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 354 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 355 | if (!unindent) { |
| 356 | /* If we're indenting, add the characters in line_indent to |
| 357 | * the beginning of the non-whitespace text of this line. */ |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 358 | f->data = charealloc(f->data, line_len + line_indent_len + 1); |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 359 | charmove(&f->data[indent_len + line_indent_len], |
| 360 | &f->data[indent_len], line_len - indent_len + 1); |
| 361 | strncpy(f->data + indent_len, line_indent, line_indent_len); |
| 362 | openfile->totsize += line_indent_len; |
| 363 | |
| 364 | /* Keep track of the change in the current line. */ |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 365 | if (openfile->mark_set && f == openfile->mark_begin && |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 366 | openfile->mark_begin_x >= indent_len) |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 367 | openfile->mark_begin_x += line_indent_len; |
| 368 | |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 369 | if (f == openfile->current && openfile->current_x >= indent_len) |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 370 | openfile->current_x += line_indent_len; |
| 371 | |
| 372 | /* If the NO_NEWLINES flag isn't set, and this is the |
| 373 | * magicline, add a new magicline. */ |
| 374 | if (!ISSET(NO_NEWLINES) && f == openfile->filebot) |
| 375 | new_magicline(); |
| 376 | } else { |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 377 | size_t indent_col = strnlenpt(f->data, indent_len); |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 378 | /* The length in columns of the indentation on this line. */ |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 379 | |
David Lawrence Ramsey | af9052d | 2006-05-01 17:14:25 +0000 | [diff] [blame] | 380 | if (cols <= indent_col) { |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 381 | size_t indent_new = actual_x(f->data, indent_col - cols); |
David Lawrence Ramsey | 5bb7727 | 2006-05-06 14:37:33 +0000 | [diff] [blame] | 382 | /* The length of the indentation remaining on |
| 383 | * this line after we unindent. */ |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 384 | size_t indent_shift = indent_len - indent_new; |
David Lawrence Ramsey | 5bb7727 | 2006-05-06 14:37:33 +0000 | [diff] [blame] | 385 | /* The change in the indentation on this line |
| 386 | * after we unindent. */ |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 387 | |
David Lawrence Ramsey | af9052d | 2006-05-01 17:14:25 +0000 | [diff] [blame] | 388 | /* If we're unindenting, and there's at least cols |
David Lawrence Ramsey | 2ca3fc9 | 2006-05-01 16:48:12 +0000 | [diff] [blame] | 389 | * columns' worth of indentation at the beginning of the |
| 390 | * non-whitespace text of this line, remove it. */ |
| 391 | charmove(&f->data[indent_new], &f->data[indent_len], |
| 392 | line_len - indent_shift - indent_new + 1); |
| 393 | null_at(&f->data, line_len - indent_shift + 1); |
| 394 | openfile->totsize -= indent_shift; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 395 | |
David Lawrence Ramsey | 2e8fac6 | 2006-04-29 15:44:58 +0000 | [diff] [blame] | 396 | /* Keep track of the change in the current line. */ |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 397 | if (openfile->mark_set && f == openfile->mark_begin && |
David Lawrence Ramsey | eb4f90e | 2006-05-05 14:22:42 +0000 | [diff] [blame] | 398 | openfile->mark_begin_x > indent_new) { |
| 399 | if (openfile->mark_begin_x <= indent_len) |
| 400 | openfile->mark_begin_x = indent_new; |
| 401 | else |
| 402 | openfile->mark_begin_x -= indent_shift; |
| 403 | } |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 404 | |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 405 | if (f == openfile->current && |
| 406 | openfile->current_x > indent_new) { |
David Lawrence Ramsey | eb4f90e | 2006-05-05 14:22:42 +0000 | [diff] [blame] | 407 | if (openfile->current_x <= indent_len) |
| 408 | openfile->current_x = indent_new; |
| 409 | else |
| 410 | openfile->current_x -= indent_shift; |
| 411 | } |
David Lawrence Ramsey | 7194a61 | 2006-04-29 16:11:21 +0000 | [diff] [blame] | 412 | |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 413 | /* We've unindented, so the indentation changed. */ |
| 414 | indent_changed = TRUE; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 415 | } |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 419 | if (!unindent) |
David Lawrence Ramsey | 2545686 | 2006-05-05 15:43:52 +0000 | [diff] [blame] | 420 | /* Clean up. */ |
David Lawrence Ramsey | 80669c3 | 2006-05-05 15:41:43 +0000 | [diff] [blame] | 421 | free(line_indent); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 422 | |
| 423 | if (indent_changed) { |
Benno Schulenberg | 4bc8ff1 | 2015-12-03 09:17:06 +0000 | [diff] [blame] | 424 | /* Throw away the undo stack, to prevent making mistakes when |
| 425 | * the user tries to undo something in the reindented text. */ |
Benno Schulenberg | f845938 | 2016-01-15 16:44:50 +0000 | [diff] [blame] | 426 | discard_until(NULL, openfile); |
Benno Schulenberg | 4bc8ff1 | 2015-12-03 09:17:06 +0000 | [diff] [blame] | 427 | |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 428 | /* Mark the file as modified. */ |
| 429 | set_modified(); |
| 430 | |
| 431 | /* Update the screen. */ |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 432 | refresh_needed = TRUE; |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 433 | } |
| 434 | } |
| 435 | |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 436 | /* Indent the current line, or all lines covered by the mark if the mark |
| 437 | * is on, tabsize columns. */ |
| 438 | void do_indent_void(void) |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 439 | { |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 440 | do_indent(tabsize); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 441 | } |
| 442 | |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 443 | /* Unindent the current line, or all lines covered by the mark if the |
| 444 | * mark is on, tabsize columns. */ |
| 445 | void do_unindent(void) |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 446 | { |
David Lawrence Ramsey | aee00d4 | 2006-07-05 18:42:22 +0000 | [diff] [blame] | 447 | do_indent(-tabsize); |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 448 | } |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 449 | #endif /* !NANO_TINY */ |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 450 | |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 451 | /* Test whether the string is empty or consists of only blanks. */ |
| 452 | bool white_string(const char *s) |
| 453 | { |
| 454 | while (*s != '\0' && (is_blank_mbchar(s) || *s == '\r')) |
| 455 | s += move_mbright(s, 0); |
| 456 | |
| 457 | return !*s; |
| 458 | } |
| 459 | |
Benno Schulenberg | 08cd197 | 2016-09-08 21:00:51 +0200 | [diff] [blame] | 460 | #ifdef ENABLE_COMMENT |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 461 | /* Comment or uncomment the current line or the marked lines. */ |
| 462 | void do_comment() |
| 463 | { |
| 464 | const char *comment_seq = "#"; |
| 465 | undo_type action = UNCOMMENT; |
| 466 | filestruct *top, *bot, *f; |
Benno Schulenberg | 9192b7f | 2016-06-01 09:36:05 +0200 | [diff] [blame] | 467 | size_t top_x, bot_x; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 468 | bool empty, all_empty = TRUE; |
| 469 | |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 470 | assert(openfile->current != NULL && openfile->current->data != NULL); |
| 471 | |
| 472 | #ifndef DISABLE_COLOR |
| 473 | if (openfile->syntax && openfile->syntax->comment) |
| 474 | comment_seq = openfile->syntax->comment; |
| 475 | |
| 476 | /* Does the syntax not allow comments? */ |
| 477 | if (strlen(comment_seq) == 0) { |
| 478 | statusbar(_("Commenting is not supported for this file type")); |
| 479 | return; |
| 480 | } |
| 481 | #endif |
| 482 | |
| 483 | /* Determine which lines to work on. */ |
| 484 | if (openfile->mark_set) |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 485 | mark_order((const filestruct **)&top, &top_x, |
| 486 | (const filestruct **)&bot, &bot_x, NULL); |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 487 | else { |
| 488 | top = openfile->current; |
| 489 | bot = top; |
| 490 | } |
| 491 | |
Benno Schulenberg | 9192b7f | 2016-06-01 09:36:05 +0200 | [diff] [blame] | 492 | /* If only the magic line is selected, don't do anything. */ |
| 493 | if (top == bot && bot == openfile->filebot && !ISSET(NO_NEWLINES)) { |
| 494 | statusbar(_("Cannot comment past end of file")); |
| 495 | return; |
| 496 | } |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 497 | |
| 498 | /* Figure out whether to comment or uncomment the selected line or lines. */ |
| 499 | for (f = top; f != bot->next; f = f->next) { |
| 500 | empty = white_string(f->data); |
| 501 | |
| 502 | /* If this line is not blank and not commented, we comment all. */ |
| 503 | if (!empty && !comment_line(PREFLIGHT, f, comment_seq)) { |
| 504 | action = COMMENT; |
| 505 | break; |
| 506 | } |
| 507 | all_empty = all_empty && empty; |
| 508 | } |
| 509 | |
| 510 | /* If all selected lines are blank, we comment them. */ |
| 511 | action = all_empty ? COMMENT : action; |
| 512 | |
Benno Schulenberg | 9192b7f | 2016-06-01 09:36:05 +0200 | [diff] [blame] | 513 | add_undo(action); |
| 514 | |
| 515 | /* Store the comment sequence used for the operation, because it could |
| 516 | * change when the file name changes; we need to know what it was. */ |
| 517 | openfile->current_undo->strdata = mallocstrcpy(NULL, comment_seq); |
| 518 | |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 519 | /* Process the selected line or lines. */ |
| 520 | for (f = top; f != bot->next; f = f->next) { |
Benno Schulenberg | 9192b7f | 2016-06-01 09:36:05 +0200 | [diff] [blame] | 521 | /* Comment/uncomment a line, and add undo data when line changed. */ |
| 522 | if (comment_line(action, f, comment_seq)) |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 523 | update_comment_undo(f->lineno); |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 524 | } |
| 525 | |
Benno Schulenberg | 9192b7f | 2016-06-01 09:36:05 +0200 | [diff] [blame] | 526 | set_modified(); |
| 527 | refresh_needed = TRUE; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 528 | } |
| 529 | |
| 530 | /* Test whether the given line can be uncommented, or add or remove a comment, |
| 531 | * depending on action. Return TRUE if the line is uncommentable, or when |
| 532 | * anything was added or removed; FALSE otherwise. */ |
| 533 | bool comment_line(undo_type action, filestruct *f, const char *comment_seq) |
| 534 | { |
| 535 | size_t comment_seq_len = strlen(comment_seq); |
| 536 | const char *post_seq = strchr(comment_seq, '|'); |
| 537 | /* The postfix, if this is a bracketing type comment sequence. */ |
| 538 | size_t pre_len = post_seq ? post_seq++ - comment_seq : comment_seq_len; |
| 539 | /* Length of prefix. */ |
| 540 | size_t post_len = post_seq ? comment_seq_len - pre_len - 1 : 0; |
| 541 | /* Length of postfix. */ |
| 542 | size_t line_len = strlen(f->data); |
| 543 | |
| 544 | if (!ISSET(NO_NEWLINES) && f == openfile->filebot) |
| 545 | return FALSE; |
| 546 | |
| 547 | if (action == COMMENT) { |
| 548 | /* Make room for the comment sequence(s), move the text right and |
| 549 | * copy them in. */ |
| 550 | f->data = charealloc(f->data, line_len + pre_len + post_len + 1); |
| 551 | charmove(&f->data[pre_len], f->data, line_len); |
| 552 | charmove(f->data, comment_seq, pre_len); |
| 553 | if (post_len) |
| 554 | charmove(&f->data[pre_len + line_len], post_seq, post_len); |
| 555 | f->data[pre_len + line_len + post_len] = '\0'; |
| 556 | |
| 557 | openfile->totsize += pre_len + post_len; |
| 558 | |
| 559 | /* If needed, adjust the position of the mark and of the cursor. */ |
| 560 | if (openfile->mark_set && f == openfile->mark_begin) |
| 561 | openfile->mark_begin_x += pre_len; |
| 562 | if (f == openfile->current) { |
| 563 | openfile->current_x += pre_len; |
| 564 | openfile->placewewant = xplustabs(); |
| 565 | } |
| 566 | |
| 567 | return TRUE; |
| 568 | } |
| 569 | |
| 570 | /* If the line is commented, report it as uncommentable, or uncomment it. */ |
| 571 | if (strncmp(f->data, comment_seq, pre_len) == 0 && (post_len == 0 || |
| 572 | strcmp(&f->data[line_len - post_len], post_seq) == 0)) { |
| 573 | |
| 574 | if (action == PREFLIGHT) |
| 575 | return TRUE; |
| 576 | |
| 577 | /* Erase the comment prefix by moving the non-comment part. */ |
| 578 | charmove(f->data, &f->data[pre_len], line_len - pre_len); |
| 579 | /* Truncate the postfix if there was one. */ |
| 580 | f->data[line_len - pre_len - post_len] = '\0'; |
| 581 | |
| 582 | openfile->totsize -= pre_len + post_len; |
| 583 | |
| 584 | /* If needed, adjust the position of the mark and then the cursor. */ |
| 585 | if (openfile->mark_set && f == openfile->mark_begin) { |
| 586 | if (openfile->mark_begin_x < pre_len) |
| 587 | openfile->mark_begin_x = 0; |
| 588 | else |
| 589 | openfile->mark_begin_x -= pre_len; |
| 590 | } |
| 591 | if (f == openfile->current) { |
| 592 | if (openfile->current_x < pre_len) |
| 593 | openfile->current_x = 0; |
| 594 | else |
| 595 | openfile->current_x -= pre_len; |
| 596 | openfile->placewewant = xplustabs(); |
| 597 | } |
| 598 | |
| 599 | return TRUE; |
| 600 | } |
| 601 | |
| 602 | return FALSE; |
| 603 | } |
| 604 | |
| 605 | /* Perform an undo or redo for a comment or uncomment action. */ |
| 606 | void handle_comment_action(undo *u, bool undoing, bool add_comment) |
| 607 | { |
| 608 | undo_group *group = u->grouping; |
| 609 | |
| 610 | /* When redoing, reposition the cursor and let the commenter adjust it. */ |
| 611 | if (!undoing) |
| 612 | goto_line_posx(u->lineno, u->begin); |
| 613 | |
| 614 | while (group) { |
| 615 | filestruct *f = fsfromline(group->top_line); |
| 616 | |
| 617 | while (f && f->lineno <= group->bottom_line) { |
| 618 | comment_line(undoing ^ add_comment ? |
| 619 | COMMENT : UNCOMMENT, f, u->strdata); |
| 620 | f = f->next; |
| 621 | } |
| 622 | group = group->next; |
| 623 | } |
| 624 | |
| 625 | /* When undoing, reposition the cursor to the recorded location. */ |
| 626 | if (undoing) |
| 627 | goto_line_posx(u->lineno, u->begin); |
| 628 | |
| 629 | refresh_needed = TRUE; |
| 630 | } |
| 631 | #endif /* ENABLE_COMMENT */ |
| 632 | |
| 633 | #ifndef NANO_TINY |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 634 | #define redo_paste undo_cut |
| 635 | #define undo_paste redo_cut |
| 636 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 637 | /* Undo a cut, or redo an uncut. */ |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 638 | void undo_cut(undo *u) |
| 639 | { |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 640 | /* If we cut the magicline, we may as well not crash. :/ */ |
Chris Allegretta | 42726f7 | 2009-07-27 04:16:44 +0000 | [diff] [blame] | 641 | if (!u->cutbuffer) |
| 642 | return; |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 643 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 644 | /* Get to where we need to uncut from. */ |
Benno Schulenberg | f0bb503 | 2015-06-27 09:27:19 +0000 | [diff] [blame] | 645 | if (u->xflags == WAS_WHOLE_LINE) |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 646 | goto_line_posx(u->mark_begin_lineno, 0); |
| 647 | else |
| 648 | goto_line_posx(u->mark_begin_lineno, u->mark_begin_x); |
Chris Allegretta | 42726f7 | 2009-07-27 04:16:44 +0000 | [diff] [blame] | 649 | |
David Lawrence Ramsey | 1cb945f | 2017-02-14 21:35:01 -0600 | [diff] [blame] | 650 | copy_from_buffer(u->cutbuffer); |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 651 | |
Benno Schulenberg | f0bb503 | 2015-06-27 09:27:19 +0000 | [diff] [blame] | 652 | if (u->xflags != WAS_MARKED_FORWARD && u->type != PASTE) |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 653 | goto_line_posx(u->mark_begin_lineno, u->mark_begin_x); |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 656 | /* Redo a cut, or undo an uncut. */ |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 657 | void redo_cut(undo *u) |
| 658 | { |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 659 | /* If we cut the magicline, we may as well not crash. :/ */ |
Chris Allegretta | 42726f7 | 2009-07-27 04:16:44 +0000 | [diff] [blame] | 660 | if (!u->cutbuffer) |
| 661 | return; |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 662 | |
Benno Schulenberg | 637b76b | 2014-07-02 20:52:27 +0000 | [diff] [blame] | 663 | filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom; |
Benno Schulenberg | ae22fe0 | 2015-12-01 11:51:10 +0000 | [diff] [blame] | 664 | cutbuffer = cutbottom = NULL; |
Benno Schulenberg | 637b76b | 2014-07-02 20:52:27 +0000 | [diff] [blame] | 665 | |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 666 | goto_line_posx(u->lineno, u->begin); |
| 667 | |
Benno Schulenberg | 92213f9 | 2014-07-02 20:29:57 +0000 | [diff] [blame] | 668 | openfile->mark_set = TRUE; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 669 | openfile->mark_begin = fsfromline(u->mark_begin_lineno); |
Benno Schulenberg | f0bb503 | 2015-06-27 09:27:19 +0000 | [diff] [blame] | 670 | openfile->mark_begin_x = (u->xflags == WAS_WHOLE_LINE) ? 0 : u->mark_begin_x; |
Benno Schulenberg | 0166691 | 2014-06-22 11:03:49 +0000 | [diff] [blame] | 671 | |
Benno Schulenberg | 452066b | 2015-11-02 13:46:40 +0000 | [diff] [blame] | 672 | do_cut_text(FALSE, FALSE); |
Benno Schulenberg | 0166691 | 2014-06-22 11:03:49 +0000 | [diff] [blame] | 673 | |
Benno Schulenberg | 9703934 | 2016-02-18 19:58:18 +0000 | [diff] [blame] | 674 | free_filestruct(cutbuffer); |
Benno Schulenberg | 637b76b | 2014-07-02 20:52:27 +0000 | [diff] [blame] | 675 | cutbuffer = oldcutbuffer; |
| 676 | cutbottom = oldcutbottom; |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 677 | } |
| 678 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 679 | /* Undo the last thing(s) we did. */ |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 680 | void do_undo(void) |
| 681 | { |
| 682 | undo *u = openfile->current_undo; |
Benno Schulenberg | 06b449b | 2016-05-08 10:51:40 +0200 | [diff] [blame] | 683 | filestruct *f, *t = NULL; |
Benno Schulenberg | 5dde918 | 2014-07-11 19:14:25 +0000 | [diff] [blame] | 684 | char *data, *undidmsg = NULL; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 685 | |
| 686 | if (!u) { |
| 687 | statusbar(_("Nothing in undo buffer!")); |
| 688 | return; |
| 689 | } |
| 690 | |
Benno Schulenberg | 06b449b | 2016-05-08 10:51:40 +0200 | [diff] [blame] | 691 | f = fsfromline(u->mark_begin_lineno); |
| 692 | if (!f) |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 693 | return; |
Benno Schulenberg | 06b449b | 2016-05-08 10:51:40 +0200 | [diff] [blame] | 694 | |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 695 | #ifdef DEBUG |
Benno Schulenberg | 3021a04 | 2015-06-17 15:17:09 +0000 | [diff] [blame] | 696 | fprintf(stderr, " >> Undoing a type %d...\n", u->type); |
| 697 | fprintf(stderr, " >> Data we're about to undo = \"%s\"\n", f->data); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 698 | #endif |
| 699 | |
Chris Allegretta | fa40694 | 2008-07-13 16:44:19 +0000 | [diff] [blame] | 700 | openfile->current_x = u->begin; |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 701 | switch (u->type) { |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 702 | case ADD: |
Benno Schulenberg | cca22bb | 2016-02-05 12:27:54 +0000 | [diff] [blame] | 703 | /* TRANSLATORS: Eight of the next nine strings describe actions |
| 704 | * that are undone or redone. It are all nouns, not verbs. */ |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 705 | undidmsg = _("text add"); |
Benno Schulenberg | 81f3177 | 2015-11-22 16:09:15 +0000 | [diff] [blame] | 706 | data = charalloc(strlen(f->data) - strlen(u->strdata) + 1); |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 707 | strncpy(data, f->data, u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 708 | strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]); |
| 709 | free(f->data); |
| 710 | f->data = data; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 711 | goto_line_posx(u->lineno, u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 712 | break; |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 713 | case BACK: |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 714 | case DEL: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 715 | undidmsg = _("text delete"); |
Benno Schulenberg | 81f3177 | 2015-11-22 16:09:15 +0000 | [diff] [blame] | 716 | data = charalloc(strlen(f->data) + strlen(u->strdata) + 1); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 717 | strncpy(data, f->data, u->begin); |
| 718 | strcpy(&data[u->begin], u->strdata); |
| 719 | strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]); |
| 720 | free(f->data); |
| 721 | f->data = data; |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 722 | goto_line_posx(u->mark_begin_lineno, u->mark_begin_x); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 723 | break; |
Chris Allegretta | c9f0799 | 2009-12-03 03:12:00 +0000 | [diff] [blame] | 724 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 725 | case SPLIT_END: |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 726 | goto_line_posx(u->lineno, u->begin); |
| 727 | openfile->current_undo = openfile->current_undo->next; |
| 728 | openfile->last_action = OTHER; |
| 729 | while (openfile->current_undo->type != SPLIT_BEGIN) |
| 730 | do_undo(); |
| 731 | u = openfile->current_undo; |
| 732 | f = openfile->current; |
Benno Schulenberg | 5dde918 | 2014-07-11 19:14:25 +0000 | [diff] [blame] | 733 | case SPLIT_BEGIN: |
| 734 | undidmsg = _("text add"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 735 | break; |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 736 | #endif |
Benno Schulenberg | 45fe2ad | 2014-06-18 20:11:52 +0000 | [diff] [blame] | 737 | case JOIN: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 738 | undidmsg = _("line join"); |
Benno Schulenberg | 82373d8 | 2015-06-17 10:41:57 +0000 | [diff] [blame] | 739 | /* When the join was done by a Backspace at the tail of the file, |
Benno Schulenberg | 6196664 | 2015-07-06 18:03:14 +0000 | [diff] [blame] | 740 | * and the nonewlines flag isn't set, do not re-add a newline that |
| 741 | * wasn't actually deleted; just position the cursor. */ |
Benno Schulenberg | f2da466 | 2015-12-04 21:11:10 +0000 | [diff] [blame] | 742 | if (u->xflags == WAS_FINAL_BACKSPACE && !ISSET(NO_NEWLINES)) { |
Benno Schulenberg | 6196664 | 2015-07-06 18:03:14 +0000 | [diff] [blame] | 743 | goto_line_posx(openfile->filebot->lineno, 0); |
| 744 | break; |
| 745 | } |
Benno Schulenberg | 6095ff3 | 2015-07-06 19:17:27 +0000 | [diff] [blame] | 746 | t = make_new_node(f); |
| 747 | t->data = mallocstrcpy(NULL, u->strdata); |
| 748 | data = mallocstrncpy(NULL, f->data, u->mark_begin_x + 1); |
| 749 | data[u->mark_begin_x] = '\0'; |
| 750 | free(f->data); |
| 751 | f->data = data; |
Benno Schulenberg | fbe4376 | 2015-11-24 13:24:01 +0000 | [diff] [blame] | 752 | splice_node(f, t); |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 753 | goto_line_posx(u->lineno, u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 754 | break; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 755 | case CUT_EOF: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 756 | case CUT: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 757 | undidmsg = _("text cut"); |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 758 | undo_cut(u); |
Benno Schulenberg | 2394e52 | 2014-07-16 08:46:42 +0000 | [diff] [blame] | 759 | f = fsfromline(u->lineno); |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 760 | break; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 761 | case PASTE: |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 762 | undidmsg = _("text uncut"); |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 763 | undo_paste(u); |
Benno Schulenberg | 883373c | 2016-06-14 16:01:52 +0200 | [diff] [blame] | 764 | f = fsfromline(u->mark_begin_lineno); |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 765 | break; |
Chris Allegretta | b843a51 | 2009-04-21 05:34:08 +0000 | [diff] [blame] | 766 | case ENTER: |
Benno Schulenberg | b255b01 | 2015-11-25 09:49:27 +0000 | [diff] [blame] | 767 | if (f->next == NULL) { |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 768 | statusline(ALERT, _("Internal error: line is missing. " |
| 769 | "Please save your work.")); |
Benno Schulenberg | b255b01 | 2015-11-25 09:49:27 +0000 | [diff] [blame] | 770 | break; |
Chris Allegretta | b843a51 | 2009-04-21 05:34:08 +0000 | [diff] [blame] | 771 | } |
Benno Schulenberg | b255b01 | 2015-11-25 09:49:27 +0000 | [diff] [blame] | 772 | undidmsg = _("line break"); |
Benno Schulenberg | 1b69dd6 | 2015-11-25 09:27:25 +0000 | [diff] [blame] | 773 | f->data = charealloc(f->data, strlen(f->data) + |
| 774 | strlen(&f->next->data[u->mark_begin_x]) + 1); |
| 775 | strcat(f->data, &f->next->data[u->mark_begin_x]); |
Benno Schulenberg | b255b01 | 2015-11-25 09:49:27 +0000 | [diff] [blame] | 776 | unlink_node(f->next); |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 777 | goto_line_posx(u->lineno, u->begin); |
Chris Allegretta | b843a51 | 2009-04-21 05:34:08 +0000 | [diff] [blame] | 778 | break; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 779 | #ifdef ENABLE_COMMENT |
| 780 | case COMMENT: |
| 781 | handle_comment_action(u, TRUE, TRUE); |
| 782 | undidmsg = _("comment"); |
| 783 | break; |
| 784 | case UNCOMMENT: |
| 785 | handle_comment_action(u, TRUE, FALSE); |
| 786 | undidmsg = _("uncomment"); |
| 787 | break; |
| 788 | #endif |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 789 | case INSERT: |
| 790 | undidmsg = _("text insert"); |
Benno Schulenberg | 0166691 | 2014-06-22 11:03:49 +0000 | [diff] [blame] | 791 | filestruct *oldcutbuffer = cutbuffer, *oldcutbottom = cutbottom; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 792 | cutbuffer = NULL; |
| 793 | cutbottom = NULL; |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 794 | /* Instead of a line number, u->mark_begin_lineno contains the number |
| 795 | * of lines of the inserted segment, because the file was partitioned |
| 796 | * when update_undo() was called; so, calculate the end-line number. */ |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 797 | openfile->mark_begin = fsfromline(u->lineno + u->mark_begin_lineno - 1); |
Benno Schulenberg | ec26fd5 | 2015-11-12 19:01:57 +0000 | [diff] [blame] | 798 | openfile->mark_begin_x = u->mark_begin_x; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 799 | openfile->mark_set = TRUE; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 800 | goto_line_posx(u->lineno, u->begin); |
Benno Schulenberg | d3429a7 | 2016-12-31 16:36:14 +0100 | [diff] [blame] | 801 | cut_marked(NULL); |
Benno Schulenberg | 9703934 | 2016-02-18 19:58:18 +0000 | [diff] [blame] | 802 | free_filestruct(u->cutbuffer); |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 803 | u->cutbuffer = cutbuffer; |
| 804 | u->cutbottom = cutbottom; |
| 805 | cutbuffer = oldcutbuffer; |
| 806 | cutbottom = oldcutbottom; |
| 807 | openfile->mark_set = FALSE; |
| 808 | break; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 809 | case REPLACE: |
| 810 | undidmsg = _("text replace"); |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 811 | goto_line_posx(u->lineno, u->begin); |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 812 | data = u->strdata; |
| 813 | u->strdata = f->data; |
| 814 | f->data = data; |
| 815 | break; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 816 | default: |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 817 | statusline(ALERT, _("Internal error: unknown type. " |
| 818 | "Please save your work.")); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 819 | break; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 820 | } |
Benno Schulenberg | 8f61511 | 2014-04-14 09:57:06 +0000 | [diff] [blame] | 821 | |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 822 | if (undidmsg && !pletion_line) |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 823 | statusline(HUSH, _("Undid action (%s)"), undidmsg); |
Benno Schulenberg | 38cb8fc | 2014-06-17 15:37:34 +0000 | [diff] [blame] | 824 | |
| 825 | renumber(f); |
Benno Schulenberg | c0aa5ad | 2017-01-09 18:25:25 +0100 | [diff] [blame] | 826 | |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 827 | openfile->current_undo = openfile->current_undo->next; |
Chris Allegretta | 6f681c1 | 2008-08-08 03:02:03 +0000 | [diff] [blame] | 828 | openfile->last_action = OTHER; |
Benno Schulenberg | ccffc54 | 2016-06-07 13:04:51 +0200 | [diff] [blame] | 829 | openfile->mark_set = FALSE; |
Benno Schulenberg | e96022b | 2014-06-17 15:50:34 +0000 | [diff] [blame] | 830 | openfile->placewewant = xplustabs(); |
Benno Schulenberg | c0aa5ad | 2017-01-09 18:25:25 +0100 | [diff] [blame] | 831 | |
Benno Schulenberg | 66e2141 | 2015-11-30 16:21:51 +0000 | [diff] [blame] | 832 | openfile->totsize = u->wassize; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 833 | set_modified(); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 836 | /* Redo the last thing(s) we undid. */ |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 837 | void do_redo(void) |
| 838 | { |
Benno Schulenberg | 06b449b | 2016-05-08 10:51:40 +0200 | [diff] [blame] | 839 | filestruct *f; |
Benno Schulenberg | 5dde918 | 2014-07-11 19:14:25 +0000 | [diff] [blame] | 840 | char *data, *redidmsg = NULL; |
Benno Schulenberg | 26eed9d | 2015-10-28 20:49:16 +0000 | [diff] [blame] | 841 | undo *u = openfile->undotop; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 842 | |
Benno Schulenberg | bf64514 | 2015-10-29 17:02:13 +0000 | [diff] [blame] | 843 | if (u == NULL || u == openfile->current_undo) { |
| 844 | statusbar(_("Nothing to re-do!")); |
| 845 | return; |
| 846 | } |
| 847 | |
Benno Schulenberg | 26eed9d | 2015-10-28 20:49:16 +0000 | [diff] [blame] | 848 | /* Get the previous undo item. */ |
Benno Schulenberg | d0b7255 | 2015-10-29 09:04:30 +0000 | [diff] [blame] | 849 | while (u != NULL && u->next != openfile->current_undo) |
Benno Schulenberg | 26eed9d | 2015-10-28 20:49:16 +0000 | [diff] [blame] | 850 | u = u->next; |
| 851 | |
Benno Schulenberg | bf64514 | 2015-10-29 17:02:13 +0000 | [diff] [blame] | 852 | if (u->next != openfile->current_undo) { |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 853 | statusline(ALERT, _("Internal error: cannot set up redo. " |
| 854 | "Please save your work.")); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 855 | return; |
| 856 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 857 | |
Benno Schulenberg | 06b449b | 2016-05-08 10:51:40 +0200 | [diff] [blame] | 858 | f = fsfromline(u->type == INSERT ? 1 : u->mark_begin_lineno); |
| 859 | if (!f) |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 860 | return; |
Benno Schulenberg | 06b449b | 2016-05-08 10:51:40 +0200 | [diff] [blame] | 861 | |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 862 | #ifdef DEBUG |
Benno Schulenberg | f2da466 | 2015-12-04 21:11:10 +0000 | [diff] [blame] | 863 | fprintf(stderr, " >> Redo running for type %d\n", u->type); |
| 864 | fprintf(stderr, " >> Data we're about to redo = \"%s\"\n", f->data); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 865 | #endif |
| 866 | |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 867 | switch (u->type) { |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 868 | case ADD: |
Benno Schulenberg | 4cf39e4 | 2014-06-22 21:26:56 +0000 | [diff] [blame] | 869 | redidmsg = _("text add"); |
Benno Schulenberg | 81f3177 | 2015-11-22 16:09:15 +0000 | [diff] [blame] | 870 | data = charalloc(strlen(f->data) + strlen(u->strdata) + 1); |
Chris Allegretta | 1f37c45 | 2008-08-01 04:11:57 +0000 | [diff] [blame] | 871 | strncpy(data, f->data, u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 872 | strcpy(&data[u->begin], u->strdata); |
| 873 | strcpy(&data[u->begin + strlen(u->strdata)], &f->data[u->begin]); |
| 874 | free(f->data); |
| 875 | f->data = data; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 876 | goto_line_posx(u->mark_begin_lineno, u->mark_begin_x); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 877 | break; |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 878 | case BACK: |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 879 | case DEL: |
Benno Schulenberg | 4cf39e4 | 2014-06-22 21:26:56 +0000 | [diff] [blame] | 880 | redidmsg = _("text delete"); |
Benno Schulenberg | 81f3177 | 2015-11-22 16:09:15 +0000 | [diff] [blame] | 881 | data = charalloc(strlen(f->data) + strlen(u->strdata) + 1); |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 882 | strncpy(data, f->data, u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 883 | strcpy(&data[u->begin], &f->data[u->begin + strlen(u->strdata)]); |
| 884 | free(f->data); |
| 885 | f->data = data; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 886 | goto_line_posx(u->lineno, u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 887 | break; |
Chris Allegretta | b843a51 | 2009-04-21 05:34:08 +0000 | [diff] [blame] | 888 | case ENTER: |
Benno Schulenberg | 4cf39e4 | 2014-06-22 21:26:56 +0000 | [diff] [blame] | 889 | redidmsg = _("line break"); |
Benno Schulenberg | f1d9fcc | 2015-11-11 18:51:39 +0000 | [diff] [blame] | 890 | filestruct *shoveline = make_new_node(f); |
| 891 | shoveline->data = mallocstrcpy(NULL, u->strdata); |
| 892 | data = mallocstrncpy(NULL, f->data, u->begin + 1); |
| 893 | data[u->begin] = '\0'; |
| 894 | free(f->data); |
| 895 | f->data = data; |
Benno Schulenberg | fbe4376 | 2015-11-24 13:24:01 +0000 | [diff] [blame] | 896 | splice_node(f, shoveline); |
Benno Schulenberg | f1d9fcc | 2015-11-11 18:51:39 +0000 | [diff] [blame] | 897 | renumber(shoveline); |
| 898 | goto_line_posx(u->lineno + 1, u->mark_begin_x); |
Chris Allegretta | b843a51 | 2009-04-21 05:34:08 +0000 | [diff] [blame] | 899 | break; |
Chris Allegretta | c9f0799 | 2009-12-03 03:12:00 +0000 | [diff] [blame] | 900 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 901 | case SPLIT_BEGIN: |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 902 | goto_line_posx(u->lineno, u->begin); |
| 903 | openfile->current_undo = u; |
| 904 | openfile->last_action = OTHER; |
| 905 | while (openfile->current_undo->type != SPLIT_END) |
| 906 | do_redo(); |
| 907 | u = openfile->current_undo; |
| 908 | goto_line_posx(u->lineno, u->begin); |
Benno Schulenberg | 5dde918 | 2014-07-11 19:14:25 +0000 | [diff] [blame] | 909 | case SPLIT_END: |
| 910 | redidmsg = _("text add"); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 911 | break; |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 912 | #endif |
Benno Schulenberg | 45fe2ad | 2014-06-18 20:11:52 +0000 | [diff] [blame] | 913 | case JOIN: |
Benno Schulenberg | 4a59b12 | 2015-11-26 09:31:33 +0000 | [diff] [blame] | 914 | if (f->next == NULL) { |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 915 | statusline(ALERT, _("Internal error: line is missing. " |
| 916 | "Please save your work.")); |
Benno Schulenberg | 4a59b12 | 2015-11-26 09:31:33 +0000 | [diff] [blame] | 917 | break; |
| 918 | } |
Benno Schulenberg | 4cf39e4 | 2014-06-22 21:26:56 +0000 | [diff] [blame] | 919 | redidmsg = _("line join"); |
Benno Schulenberg | 793d849 | 2015-11-26 08:45:22 +0000 | [diff] [blame] | 920 | /* When the join was done by a Backspace at the tail of the file, |
| 921 | * and the nonewlines flag isn't set, do not join anything, as |
| 922 | * nothing was actually deleted; just position the cursor. */ |
| 923 | if (u->xflags == WAS_FINAL_BACKSPACE && !ISSET(NO_NEWLINES)) { |
| 924 | goto_line_posx(u->mark_begin_lineno, u->mark_begin_x); |
| 925 | break; |
| 926 | } |
Benno Schulenberg | 81f3177 | 2015-11-22 16:09:15 +0000 | [diff] [blame] | 927 | f->data = charealloc(f->data, strlen(f->data) + strlen(u->strdata) + 1); |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 928 | strcat(f->data, u->strdata); |
Benno Schulenberg | 4a59b12 | 2015-11-26 09:31:33 +0000 | [diff] [blame] | 929 | unlink_node(f->next); |
Chris Allegretta | 0b499d4 | 2008-07-14 07:18:22 +0000 | [diff] [blame] | 930 | renumber(f); |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 931 | goto_line_posx(u->mark_begin_lineno, u->mark_begin_x); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 932 | break; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 933 | case CUT_EOF: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 934 | case CUT: |
Benno Schulenberg | 4cf39e4 | 2014-06-22 21:26:56 +0000 | [diff] [blame] | 935 | redidmsg = _("text cut"); |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 936 | redo_cut(u); |
| 937 | break; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 938 | case PASTE: |
Benno Schulenberg | 4cf39e4 | 2014-06-22 21:26:56 +0000 | [diff] [blame] | 939 | redidmsg = _("text uncut"); |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 940 | redo_paste(u); |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 941 | break; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 942 | case REPLACE: |
Benno Schulenberg | 4cf39e4 | 2014-06-22 21:26:56 +0000 | [diff] [blame] | 943 | redidmsg = _("text replace"); |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 944 | data = u->strdata; |
| 945 | u->strdata = f->data; |
| 946 | f->data = data; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 947 | goto_line_posx(u->lineno, u->begin); |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 948 | break; |
Chris Allegretta | b549f37 | 2008-09-16 21:35:19 +0000 | [diff] [blame] | 949 | case INSERT: |
Benno Schulenberg | 4cf39e4 | 2014-06-22 21:26:56 +0000 | [diff] [blame] | 950 | redidmsg = _("text insert"); |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 951 | goto_line_posx(u->lineno, u->begin); |
David Lawrence Ramsey | 1cb945f | 2017-02-14 21:35:01 -0600 | [diff] [blame] | 952 | copy_from_buffer(u->cutbuffer); |
Benno Schulenberg | 637b76b | 2014-07-02 20:52:27 +0000 | [diff] [blame] | 953 | free_filestruct(u->cutbuffer); |
| 954 | u->cutbuffer = NULL; |
Chris Allegretta | ea57787 | 2008-08-03 20:19:42 +0000 | [diff] [blame] | 955 | break; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 956 | #ifdef ENABLE_COMMENT |
| 957 | case COMMENT: |
| 958 | handle_comment_action(u, FALSE, TRUE); |
| 959 | redidmsg = _("comment"); |
| 960 | break; |
| 961 | case UNCOMMENT: |
| 962 | handle_comment_action(u, FALSE, FALSE); |
| 963 | redidmsg = _("uncomment"); |
| 964 | break; |
| 965 | #endif |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 966 | default: |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 967 | statusline(ALERT, _("Internal error: unknown type. " |
| 968 | "Please save your work.")); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 969 | break; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 970 | } |
Benno Schulenberg | 8f61511 | 2014-04-14 09:57:06 +0000 | [diff] [blame] | 971 | |
Benno Schulenberg | 5dde918 | 2014-07-11 19:14:25 +0000 | [diff] [blame] | 972 | if (redidmsg) |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 973 | statusline(HUSH, _("Redid action (%s)"), redidmsg); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 974 | |
| 975 | openfile->current_undo = u; |
Chris Allegretta | 6f681c1 | 2008-08-08 03:02:03 +0000 | [diff] [blame] | 976 | openfile->last_action = OTHER; |
Benno Schulenberg | ccffc54 | 2016-06-07 13:04:51 +0200 | [diff] [blame] | 977 | openfile->mark_set = FALSE; |
Benno Schulenberg | e96022b | 2014-06-17 15:50:34 +0000 | [diff] [blame] | 978 | openfile->placewewant = xplustabs(); |
Benno Schulenberg | c0aa5ad | 2017-01-09 18:25:25 +0100 | [diff] [blame] | 979 | |
Benno Schulenberg | 66e2141 | 2015-11-30 16:21:51 +0000 | [diff] [blame] | 980 | openfile->totsize = u->newsize; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 981 | set_modified(); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 982 | } |
David Lawrence Ramsey | f85001a | 2006-04-28 13:19:56 +0000 | [diff] [blame] | 983 | #endif /* !NANO_TINY */ |
| 984 | |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 985 | /* Break the current line at the cursor position. */ |
Benno Schulenberg | 9fa95a3 | 2016-12-15 13:04:52 +0100 | [diff] [blame] | 986 | void do_enter(void) |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 987 | { |
| 988 | filestruct *newnode = make_new_node(openfile->current); |
| 989 | size_t extra = 0; |
| 990 | |
Chris Allegretta | dc7136a | 2008-08-21 04:24:25 +0000 | [diff] [blame] | 991 | assert(openfile->current != NULL && openfile->current->data != NULL); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 992 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 993 | #ifndef NANO_TINY |
Benno Schulenberg | 7be4930 | 2015-11-11 19:15:36 +0000 | [diff] [blame] | 994 | add_undo(ENTER); |
| 995 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 996 | if (ISSET(AUTOINDENT)) { |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 997 | extra = indent_length(openfile->current->data); |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 998 | |
| 999 | /* If we are breaking the line in the indentation, limit the new |
| 1000 | * indentation to the current x position. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1001 | if (extra > openfile->current_x) |
| 1002 | extra = openfile->current_x; |
| 1003 | } |
| 1004 | #endif |
| 1005 | newnode->data = charalloc(strlen(openfile->current->data + |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 1006 | openfile->current_x) + extra + 1); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1007 | strcpy(&newnode->data[extra], openfile->current->data + |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 1008 | openfile->current_x); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1009 | #ifndef NANO_TINY |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1010 | if (ISSET(AUTOINDENT)) { |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 1011 | /* Copy the whitespace from the current line to the new one. */ |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1012 | strncpy(newnode->data, openfile->current->data, extra); |
Chris Allegretta | daeab05 | 2011-05-10 05:43:08 +0000 | [diff] [blame] | 1013 | openfile->totsize += extra; |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1014 | } |
| 1015 | #endif |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 1016 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1017 | null_at(&openfile->current->data, openfile->current_x); |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 1018 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1019 | #ifndef NANO_TINY |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 1020 | /* Adjust the mark if it was on the current line after the cursor. */ |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 1021 | if (openfile->mark_set && openfile->current == openfile->mark_begin && |
| 1022 | openfile->current_x < openfile->mark_begin_x) { |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1023 | openfile->mark_begin = newnode; |
| 1024 | openfile->mark_begin_x += extra - openfile->current_x; |
| 1025 | } |
| 1026 | #endif |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1027 | |
Benno Schulenberg | fbe4376 | 2015-11-24 13:24:01 +0000 | [diff] [blame] | 1028 | splice_node(openfile->current, newnode); |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 1029 | renumber(newnode); |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1030 | |
Benno Schulenberg | ea9f62f | 2016-12-13 19:27:33 +0100 | [diff] [blame] | 1031 | openfile->current = newnode; |
| 1032 | openfile->current_x = extra; |
| 1033 | openfile->placewewant = xplustabs(); |
| 1034 | |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1035 | openfile->totsize++; |
| 1036 | set_modified(); |
David Lawrence Ramsey | feb89db | 2005-09-13 04:45:46 +0000 | [diff] [blame] | 1037 | |
Benno Schulenberg | 7be4930 | 2015-11-11 19:15:36 +0000 | [diff] [blame] | 1038 | #ifndef NANO_TINY |
| 1039 | update_undo(ENTER); |
| 1040 | #endif |
| 1041 | |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 1042 | refresh_needed = TRUE; |
David Lawrence Ramsey | 7ea09e5 | 2005-07-25 02:41:59 +0000 | [diff] [blame] | 1043 | } |
| 1044 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1045 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1046 | /* Send a SIGKILL (unconditional kill) to the forked process in |
| 1047 | * execute_command(). */ |
David Lawrence Ramsey | 8befda6 | 2005-12-06 19:39:56 +0000 | [diff] [blame] | 1048 | RETSIGTYPE cancel_command(int signal) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1049 | { |
| 1050 | if (kill(pid, SIGKILL) == -1) |
| 1051 | nperror("kill"); |
| 1052 | } |
| 1053 | |
David Lawrence Ramsey | 0ed7171 | 2005-11-08 23:09:47 +0000 | [diff] [blame] | 1054 | /* Execute command in a shell. Return TRUE on success. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1055 | bool execute_command(const char *command) |
| 1056 | { |
| 1057 | int fd[2]; |
| 1058 | FILE *f; |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 1059 | char *shellenv; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1060 | struct sigaction oldaction, newaction; |
| 1061 | /* Original and temporary handlers for SIGINT. */ |
| 1062 | bool sig_failed = FALSE; |
| 1063 | /* Did sigaction() fail without changing the signal handlers? */ |
| 1064 | |
| 1065 | /* Make our pipes. */ |
| 1066 | if (pipe(fd) == -1) { |
Benno Schulenberg | 46fccb2 | 2014-02-28 11:49:12 +0000 | [diff] [blame] | 1067 | statusbar(_("Could not create pipe")); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1068 | return FALSE; |
| 1069 | } |
| 1070 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 1071 | /* Check $SHELL for the shell to use. If it isn't set, use /bin/sh. |
| 1072 | * Note that $SHELL should contain only a path, with no arguments. */ |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 1073 | shellenv = getenv("SHELL"); |
| 1074 | if (shellenv == NULL) |
Chris Allegretta | 5a018f0 | 2009-11-29 06:13:22 +0000 | [diff] [blame] | 1075 | shellenv = (char *) "/bin/sh"; |
David Lawrence Ramsey | eae8571 | 2005-11-29 05:48:06 +0000 | [diff] [blame] | 1076 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1077 | /* Fork a child. */ |
| 1078 | if ((pid = fork()) == 0) { |
| 1079 | close(fd[0]); |
| 1080 | dup2(fd[1], fileno(stdout)); |
| 1081 | dup2(fd[1], fileno(stderr)); |
| 1082 | |
| 1083 | /* If execl() returns at all, there was an error. */ |
David Lawrence Ramsey | 5da68ee | 2005-11-29 05:21:06 +0000 | [diff] [blame] | 1084 | execl(shellenv, tail(shellenv), "-c", command, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1085 | exit(0); |
| 1086 | } |
| 1087 | |
David Lawrence Ramsey | c838a4c | 2006-04-26 18:33:50 +0000 | [diff] [blame] | 1088 | /* Continue as parent. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1089 | close(fd[1]); |
| 1090 | |
| 1091 | if (pid == -1) { |
| 1092 | close(fd[0]); |
| 1093 | statusbar(_("Could not fork")); |
| 1094 | return FALSE; |
| 1095 | } |
| 1096 | |
| 1097 | /* Before we start reading the forked command's output, we set |
| 1098 | * things up so that Ctrl-C will cancel the new process. */ |
| 1099 | |
| 1100 | /* Enable interpretation of the special control keys so that we get |
| 1101 | * SIGINT when Ctrl-C is pressed. */ |
| 1102 | enable_signals(); |
| 1103 | |
| 1104 | if (sigaction(SIGINT, NULL, &newaction) == -1) { |
| 1105 | sig_failed = TRUE; |
| 1106 | nperror("sigaction"); |
| 1107 | } else { |
| 1108 | newaction.sa_handler = cancel_command; |
| 1109 | if (sigaction(SIGINT, &newaction, &oldaction) == -1) { |
| 1110 | sig_failed = TRUE; |
| 1111 | nperror("sigaction"); |
| 1112 | } |
| 1113 | } |
| 1114 | |
| 1115 | /* Note that now oldaction is the previous SIGINT signal handler, |
| 1116 | * to be restored later. */ |
| 1117 | |
| 1118 | f = fdopen(fd[0], "rb"); |
| 1119 | if (f == NULL) |
| 1120 | nperror("fdopen"); |
| 1121 | |
Chris Allegretta | 2c7b506 | 2009-12-09 16:51:43 +0000 | [diff] [blame] | 1122 | read_file(f, 0, "stdin", TRUE, FALSE); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1123 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1124 | if (wait(NULL) == -1) |
| 1125 | nperror("wait"); |
| 1126 | |
| 1127 | if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1) |
| 1128 | nperror("sigaction"); |
| 1129 | |
David Lawrence Ramsey | 8b9c91b | 2007-12-18 01:28:53 +0000 | [diff] [blame] | 1130 | /* Restore the terminal to its previous state. In the process, |
| 1131 | * disable interpretation of the special control keys so that we can |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1132 | * use Ctrl-C for other things. */ |
David Lawrence Ramsey | 8b9c91b | 2007-12-18 01:28:53 +0000 | [diff] [blame] | 1133 | terminal_init(); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1134 | |
| 1135 | return TRUE; |
| 1136 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1137 | |
Benno Schulenberg | f845938 | 2016-01-15 16:44:50 +0000 | [diff] [blame] | 1138 | /* Discard undo items that are newer than the given one, or all if NULL. */ |
| 1139 | void discard_until(const undo *thisitem, openfilestruct *thefile) |
Benno Schulenberg | ee5cdcb | 2015-12-03 08:50:34 +0000 | [diff] [blame] | 1140 | { |
Benno Schulenberg | f845938 | 2016-01-15 16:44:50 +0000 | [diff] [blame] | 1141 | undo *dropit = thefile->undotop; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 1142 | undo_group *group; |
Benno Schulenberg | ee5cdcb | 2015-12-03 08:50:34 +0000 | [diff] [blame] | 1143 | |
Benno Schulenberg | f845938 | 2016-01-15 16:44:50 +0000 | [diff] [blame] | 1144 | while (dropit != NULL && dropit != thisitem) { |
| 1145 | thefile->undotop = dropit->next; |
Benno Schulenberg | ee5cdcb | 2015-12-03 08:50:34 +0000 | [diff] [blame] | 1146 | free(dropit->strdata); |
Benno Schulenberg | 9703934 | 2016-02-18 19:58:18 +0000 | [diff] [blame] | 1147 | free_filestruct(dropit->cutbuffer); |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 1148 | #ifdef ENABLE_COMMENT |
| 1149 | group = dropit->grouping; |
| 1150 | while (group != NULL) { |
| 1151 | undo_group *next = group->next; |
| 1152 | free(group); |
| 1153 | group = next; |
| 1154 | } |
| 1155 | #endif |
Benno Schulenberg | ee5cdcb | 2015-12-03 08:50:34 +0000 | [diff] [blame] | 1156 | free(dropit); |
Benno Schulenberg | f845938 | 2016-01-15 16:44:50 +0000 | [diff] [blame] | 1157 | dropit = thefile->undotop; |
Benno Schulenberg | ee5cdcb | 2015-12-03 08:50:34 +0000 | [diff] [blame] | 1158 | } |
Benno Schulenberg | ef2cfa3 | 2016-02-10 08:49:23 +0000 | [diff] [blame] | 1159 | |
Benno Schulenberg | d21a9c8 | 2016-12-23 22:12:48 +0100 | [diff] [blame] | 1160 | /* Adjust the pointer to the top of the undo stack. */ |
| 1161 | thefile->current_undo = (undo *)thisitem; |
| 1162 | |
Benno Schulenberg | ef2cfa3 | 2016-02-10 08:49:23 +0000 | [diff] [blame] | 1163 | /* Prevent a chain of editing actions from continuing. */ |
| 1164 | thefile->last_action = OTHER; |
Benno Schulenberg | ee5cdcb | 2015-12-03 08:50:34 +0000 | [diff] [blame] | 1165 | } |
| 1166 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 1167 | /* Add a new undo struct to the top of the current pile. */ |
Benno Schulenberg | 86cbd95 | 2015-06-17 11:18:20 +0000 | [diff] [blame] | 1168 | void add_undo(undo_type action) |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1169 | { |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1170 | undo *u = openfile->current_undo; |
Benno Schulenberg | 6404101 | 2015-06-17 10:59:16 +0000 | [diff] [blame] | 1171 | /* The thing we did previously. */ |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1172 | |
Benno Schulenberg | 6404101 | 2015-06-17 10:59:16 +0000 | [diff] [blame] | 1173 | /* When doing contiguous adds or contiguous cuts -- which means: with |
| 1174 | * no cursor movement in between -- don't add a new undo item. */ |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1175 | if (u && u->mark_begin_lineno == openfile->current->lineno && action == openfile->last_action && |
| 1176 | ((action == ADD && u->type == ADD && u->mark_begin_x == openfile->current_x) || |
Benno Schulenberg | 83b89a4 | 2016-06-07 11:52:57 +0200 | [diff] [blame] | 1177 | (action == CUT && u->type == CUT && u->xflags < MARK_WAS_SET && keeping_cutbuffer()))) |
Chris Allegretta | 8f76112 | 2008-08-01 06:52:15 +0000 | [diff] [blame] | 1178 | return; |
| 1179 | |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 1180 | /* Blow away newer undo items if we add somewhere in the middle. */ |
Benno Schulenberg | f845938 | 2016-01-15 16:44:50 +0000 | [diff] [blame] | 1181 | discard_until(u, openfile); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1182 | |
Benno Schulenberg | 3021a04 | 2015-06-17 15:17:09 +0000 | [diff] [blame] | 1183 | #ifdef DEBUG |
| 1184 | fprintf(stderr, " >> Adding an undo...\n"); |
| 1185 | #endif |
| 1186 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 1187 | /* Allocate and initialize a new undo type. */ |
Chris Allegretta | 5a018f0 | 2009-11-29 06:13:22 +0000 | [diff] [blame] | 1188 | u = (undo *) nmalloc(sizeof(undo)); |
Benno Schulenberg | 86cbd95 | 2015-06-17 11:18:20 +0000 | [diff] [blame] | 1189 | u->type = action; |
Benno Schulenberg | c35eb5a | 2014-06-09 10:35:44 +0000 | [diff] [blame] | 1190 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1191 | if (u->type == SPLIT_BEGIN) { |
| 1192 | /* Some action, most likely an ADD, was performed that invoked |
| 1193 | * do_wrap(). Rearrange the undo order so that this previous |
| 1194 | * action is after the SPLIT_BEGIN undo. */ |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1195 | u->next = openfile->undotop->next; |
| 1196 | openfile->undotop->next = u; |
Benno Schulenberg | c35eb5a | 2014-06-09 10:35:44 +0000 | [diff] [blame] | 1197 | } else |
| 1198 | #endif |
| 1199 | { |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1200 | u->next = openfile->undotop; |
| 1201 | openfile->undotop = u; |
| 1202 | openfile->current_undo = u; |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1203 | } |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 1204 | u->strdata = NULL; |
| 1205 | u->cutbuffer = NULL; |
Benno Schulenberg | 52e3533 | 2014-05-16 11:03:04 +0000 | [diff] [blame] | 1206 | u->cutbottom = NULL; |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 1207 | u->lineno = openfile->current->lineno; |
| 1208 | u->begin = openfile->current_x; |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1209 | u->mark_begin_lineno = openfile->current->lineno; |
| 1210 | u->mark_begin_x = openfile->current_x; |
Benno Schulenberg | 66e2141 | 2015-11-30 16:21:51 +0000 | [diff] [blame] | 1211 | u->wassize = openfile->totsize; |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 1212 | u->xflags = 0; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 1213 | u->grouping = NULL; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1214 | |
| 1215 | switch (u->type) { |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 1216 | /* We need to start copying data into the undo buffer |
| 1217 | * or we won't be able to restore it later. */ |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1218 | case ADD: |
Benno Schulenberg | 66e2141 | 2015-11-30 16:21:51 +0000 | [diff] [blame] | 1219 | u->wassize--; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1220 | break; |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 1221 | case BACK: |
Benno Schulenberg | 412b9fc | 2015-06-27 09:17:36 +0000 | [diff] [blame] | 1222 | /* If the next line is the magic line, don't ever undo this |
| 1223 | * backspace, as it won't actually have deleted anything. */ |
Benno Schulenberg | 964c10d | 2016-12-07 19:56:27 +0100 | [diff] [blame] | 1224 | if (openfile->current->next == openfile->filebot && |
| 1225 | openfile->current->data[0] != '\0') |
Benno Schulenberg | f0bb503 | 2015-06-27 09:27:19 +0000 | [diff] [blame] | 1226 | u->xflags = WAS_FINAL_BACKSPACE; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1227 | case DEL: |
Benno Schulenberg | 6620acb | 2016-12-20 10:03:53 +0100 | [diff] [blame] | 1228 | if (openfile->current->data[openfile->current_x] != '\0') { |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1229 | char *char_buf = charalloc(mb_cur_max() + 1); |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1230 | int char_len = parse_mbchar(&openfile->current->data[u->begin], |
Benno Schulenberg | 964c10d | 2016-12-07 19:56:27 +0100 | [diff] [blame] | 1231 | char_buf, NULL); |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1232 | null_at(&char_buf, char_len); |
Benno Schulenberg | 1eb23d4 | 2014-06-04 16:30:11 +0000 | [diff] [blame] | 1233 | u->strdata = char_buf; |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 1234 | if (u->type == BACK) |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1235 | u->mark_begin_x += char_len; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1236 | break; |
| 1237 | } |
Benno Schulenberg | 45fe2ad | 2014-06-18 20:11:52 +0000 | [diff] [blame] | 1238 | /* Else purposely fall into the line-joining code. */ |
| 1239 | case JOIN: |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1240 | if (openfile->current->next) { |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 1241 | if (u->type == BACK) { |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1242 | u->lineno = openfile->current->next->lineno; |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 1243 | u->begin = 0; |
| 1244 | } |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1245 | u->strdata = mallocstrcpy(NULL, openfile->current->next->data); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1246 | } |
Benno Schulenberg | 86cbd95 | 2015-06-17 11:18:20 +0000 | [diff] [blame] | 1247 | action = u->type = JOIN; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1248 | break; |
Chris Allegretta | c9f0799 | 2009-12-03 03:12:00 +0000 | [diff] [blame] | 1249 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1250 | case SPLIT_BEGIN: |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1251 | action = openfile->undotop->type; |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1252 | break; |
| 1253 | case SPLIT_END: |
Chris Allegretta | 12ba557 | 2009-03-26 01:01:48 +0000 | [diff] [blame] | 1254 | break; |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 1255 | #endif |
Chris Allegretta | 12ba557 | 2009-03-26 01:01:48 +0000 | [diff] [blame] | 1256 | case INSERT: |
Benno Schulenberg | 637b76b | 2014-07-02 20:52:27 +0000 | [diff] [blame] | 1257 | break; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 1258 | case REPLACE: |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1259 | u->strdata = mallocstrcpy(NULL, openfile->current->data); |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 1260 | break; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1261 | case CUT_EOF: |
Benno Schulenberg | 76e150b | 2014-06-21 19:32:17 +0000 | [diff] [blame] | 1262 | cutbuffer_reset(); |
| 1263 | break; |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 1264 | case CUT: |
Benno Schulenberg | a872709 | 2014-06-18 19:04:35 +0000 | [diff] [blame] | 1265 | cutbuffer_reset(); |
Benno Schulenberg | 83b89a4 | 2016-06-07 11:52:57 +0200 | [diff] [blame] | 1266 | if (openfile->mark_set) { |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 1267 | u->mark_begin_lineno = openfile->mark_begin->lineno; |
| 1268 | u->mark_begin_x = openfile->mark_begin_x; |
Benno Schulenberg | 83b89a4 | 2016-06-07 11:52:57 +0200 | [diff] [blame] | 1269 | u->xflags = MARK_WAS_SET; |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 1270 | } else if (!ISSET(CUT_TO_END)) { |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1271 | /* The entire line is being cut regardless of the cursor position. */ |
| 1272 | u->begin = 0; |
Benno Schulenberg | f0bb503 | 2015-06-27 09:27:19 +0000 | [diff] [blame] | 1273 | u->xflags = WAS_WHOLE_LINE; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1274 | } |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1275 | break; |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1276 | case PASTE: |
Benno Schulenberg | d6bd276 | 2015-11-22 16:07:23 +0000 | [diff] [blame] | 1277 | u->cutbuffer = copy_filestruct(cutbuffer); |
| 1278 | u->lineno += cutbottom->lineno - cutbuffer->lineno; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 1279 | break; |
Chris Allegretta | b843a51 | 2009-04-21 05:34:08 +0000 | [diff] [blame] | 1280 | case ENTER: |
| 1281 | break; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 1282 | #ifdef ENABLE_COMMENT |
| 1283 | case COMMENT: |
| 1284 | case UNCOMMENT: |
| 1285 | break; |
| 1286 | #endif |
Benno Schulenberg | 530c786 | 2015-11-25 10:11:54 +0000 | [diff] [blame] | 1287 | default: |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 1288 | statusline(ALERT, _("Internal error: unknown type. " |
| 1289 | "Please save your work.")); |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 1290 | break; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | #ifdef DEBUG |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1294 | fprintf(stderr, " >> openfile->current->data = \"%s\", current_x = %lu, u->begin = %lu, type = %d\n", |
| 1295 | openfile->current->data, (unsigned long)openfile->current_x, (unsigned long)u->begin, action); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1296 | #endif |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1297 | openfile->last_action = action; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1298 | } |
| 1299 | |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 1300 | #ifdef ENABLE_COMMENT |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 1301 | /* Update a comment undo item. This should be called once for each line |
| 1302 | * affected by the comment/uncomment feature. */ |
| 1303 | void update_comment_undo(ssize_t lineno) |
| 1304 | { |
| 1305 | undo *u = openfile->current_undo; |
| 1306 | |
| 1307 | /* If there already is a group and the current line is contiguous with it, |
| 1308 | * extend the group; otherwise, create a new group. */ |
| 1309 | if (u->grouping && u->grouping->bottom_line + 1 == lineno) |
| 1310 | u->grouping->bottom_line++; |
| 1311 | else { |
| 1312 | undo_group *born = (undo_group *)nmalloc(sizeof(undo_group)); |
| 1313 | |
| 1314 | born->next = u->grouping; |
| 1315 | u->grouping = born; |
| 1316 | born->top_line = lineno; |
| 1317 | born->bottom_line = lineno; |
| 1318 | } |
Mike Scalora | 115aeda | 2016-05-31 21:19:50 +0200 | [diff] [blame] | 1319 | |
| 1320 | /* Store the file size after the change, to be used when redoing. */ |
| 1321 | u->newsize = openfile->totsize; |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 1322 | } |
| 1323 | #endif /* ENABLE_COMMENT */ |
| 1324 | |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 1325 | /* Update an undo item, or determine whether a new one is really needed |
| 1326 | * and bounce the data to add_undo instead. The latter functionality |
| 1327 | * just feels gimmicky and may just be more hassle than it's worth, |
| 1328 | * so it should be axed if needed. */ |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 1329 | void update_undo(undo_type action) |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1330 | { |
| 1331 | undo *u; |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 1332 | |
| 1333 | #ifdef DEBUG |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1334 | fprintf(stderr, " >> Updating... action = %d, openfile->last_action = %d, openfile->current->lineno = %ld", |
| 1335 | action, openfile->last_action, (long)openfile->current->lineno); |
| 1336 | if (openfile->current_undo) |
| 1337 | fprintf(stderr, ", openfile->current_undo->lineno = %ld\n", (long)openfile->current_undo->lineno); |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 1338 | else |
| 1339 | fprintf(stderr, "\n"); |
Chris Allegretta | 91a1862 | 2008-08-01 03:50:20 +0000 | [diff] [blame] | 1340 | #endif |
| 1341 | |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 1342 | /* Change to an add if we're not using the same undo struct |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 1343 | * that we should be using. */ |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 1344 | if (action != openfile->last_action || |
| 1345 | (action != ENTER && action != CUT && action != INSERT && |
| 1346 | openfile->current->lineno != openfile->current_undo->lineno)) { |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 1347 | add_undo(action); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1348 | return; |
| 1349 | } |
| 1350 | |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1351 | assert(openfile->undotop != NULL); |
| 1352 | u = openfile->undotop; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1353 | |
Benno Schulenberg | 66e2141 | 2015-11-30 16:21:51 +0000 | [diff] [blame] | 1354 | u->newsize = openfile->totsize; |
| 1355 | |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1356 | switch (u->type) { |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1357 | case ADD: { |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1358 | #ifdef DEBUG |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1359 | fprintf(stderr, " >> openfile->current->data = \"%s\", current_x = %lu, u->begin = %lu\n", |
| 1360 | openfile->current->data, (unsigned long)openfile->current_x, (unsigned long)u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1361 | #endif |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1362 | char *char_buf = charalloc(mb_cur_max()); |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1363 | int char_len = parse_mbchar(&openfile->current->data[u->mark_begin_x], char_buf, NULL); |
| 1364 | u->strdata = addstrings(u->strdata, u->strdata ? strlen(u->strdata) : 0, char_buf, char_len); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1365 | #ifdef DEBUG |
Benno Schulenberg | 08d9f57 | 2015-07-10 17:25:51 +0000 | [diff] [blame] | 1366 | fprintf(stderr, " >> current undo data is \"%s\"\n", u->strdata); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1367 | #endif |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1368 | u->mark_begin_lineno = openfile->current->lineno; |
| 1369 | u->mark_begin_x = openfile->current_x; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1370 | break; |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1371 | } |
Benno Schulenberg | 64896ba | 2014-06-08 19:02:12 +0000 | [diff] [blame] | 1372 | case BACK: |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1373 | case DEL: { |
| 1374 | char *char_buf = charalloc(mb_cur_max()); |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1375 | int char_len = parse_mbchar(&openfile->current->data[openfile->current_x], char_buf, NULL); |
Benno Schulenberg | 322120c | 2017-01-08 11:05:52 +0100 | [diff] [blame] | 1376 | if (openfile->current_x == u->begin) { |
| 1377 | /* They deleted more: add removed character after earlier stuff. */ |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1378 | u->strdata = addstrings(u->strdata, strlen(u->strdata), char_buf, char_len); |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1379 | u->mark_begin_x = openfile->current_x; |
Benno Schulenberg | 322120c | 2017-01-08 11:05:52 +0100 | [diff] [blame] | 1380 | } else if (openfile->current_x == u->begin - char_len) { |
| 1381 | /* They backspaced further: add removed character before earlier. */ |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1382 | u->strdata = addstrings(char_buf, char_len, u->strdata, strlen(u->strdata)); |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1383 | u->begin = openfile->current_x; |
Benno Schulenberg | 322120c | 2017-01-08 11:05:52 +0100 | [diff] [blame] | 1384 | } else { |
| 1385 | /* They deleted *elsewhere* on the line: start a new undo item. */ |
| 1386 | free(char_buf); |
| 1387 | add_undo(u->type); |
| 1388 | return; |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1389 | } |
| 1390 | #ifdef DEBUG |
Benno Schulenberg | 3021a04 | 2015-06-17 15:17:09 +0000 | [diff] [blame] | 1391 | fprintf(stderr, " >> current undo data is \"%s\"\nu->begin = %lu\n", u->strdata, (unsigned long)u->begin); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1392 | #endif |
| 1393 | break; |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1394 | } |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1395 | case CUT_EOF: |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 1396 | case CUT: |
Chris Allegretta | c84e765 | 2008-10-14 01:14:12 +0000 | [diff] [blame] | 1397 | if (!cutbuffer) |
| 1398 | break; |
Benno Schulenberg | 9703934 | 2016-02-18 19:58:18 +0000 | [diff] [blame] | 1399 | free_filestruct(u->cutbuffer); |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 1400 | u->cutbuffer = copy_filestruct(cutbuffer); |
Benno Schulenberg | 83b89a4 | 2016-06-07 11:52:57 +0200 | [diff] [blame] | 1401 | if (u->xflags == MARK_WAS_SET) { |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1402 | /* If the "marking" operation was from right-->left or |
| 1403 | * bottom-->top, then swap the mark points. */ |
| 1404 | if ((u->lineno == u->mark_begin_lineno && u->begin < u->mark_begin_x) |
| 1405 | || u->lineno < u->mark_begin_lineno) { |
| 1406 | size_t x_loc = u->begin; |
| 1407 | u->begin = u->mark_begin_x; |
| 1408 | u->mark_begin_x = x_loc; |
| 1409 | |
| 1410 | ssize_t line = u->lineno; |
| 1411 | u->lineno = u->mark_begin_lineno; |
| 1412 | u->mark_begin_lineno = line; |
Benno Schulenberg | 92213f9 | 2014-07-02 20:29:57 +0000 | [diff] [blame] | 1413 | } else |
Benno Schulenberg | f0bb503 | 2015-06-27 09:27:19 +0000 | [diff] [blame] | 1414 | u->xflags = WAS_MARKED_FORWARD; |
Benno Schulenberg | 92213f9 | 2014-07-02 20:29:57 +0000 | [diff] [blame] | 1415 | } else { |
Benno Schulenberg | f2da466 | 2015-12-04 21:11:10 +0000 | [diff] [blame] | 1416 | /* Compute the end of the cut for the undo, using our copy. */ |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1417 | u->cutbottom = u->cutbuffer; |
| 1418 | while (u->cutbottom->next != NULL) |
| 1419 | u->cutbottom = u->cutbottom->next; |
Benno Schulenberg | f2da466 | 2015-12-04 21:11:10 +0000 | [diff] [blame] | 1420 | u->lineno = u->mark_begin_lineno + u->cutbottom->lineno - |
| 1421 | u->cutbuffer->lineno; |
Benno Schulenberg | 92213f9 | 2014-07-02 20:29:57 +0000 | [diff] [blame] | 1422 | if (ISSET(CUT_TO_END) || u->type == CUT_EOF) { |
| 1423 | u->begin = strlen(u->cutbottom->data); |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 1424 | if (u->lineno == u->mark_begin_lineno) |
| 1425 | u->begin += u->mark_begin_x; |
Benno Schulenberg | 6b15660 | 2015-11-30 15:49:37 +0000 | [diff] [blame] | 1426 | } else if (openfile->current == openfile->filebot && |
| 1427 | ISSET(NO_NEWLINES)) |
| 1428 | u->begin = strlen(u->cutbottom->data); |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1429 | } |
Chris Allegretta | 12dc8ca | 2008-07-31 04:24:04 +0000 | [diff] [blame] | 1430 | break; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 1431 | case REPLACE: |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1432 | case PASTE: |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1433 | u->begin = openfile->current_x; |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1434 | u->lineno = openfile->current->lineno; |
Chris Allegretta | 3c1131a | 2008-08-02 22:31:01 +0000 | [diff] [blame] | 1435 | break; |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 1436 | case INSERT: |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 1437 | /* Store the number of lines (plus one) of the insertion. */ |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 1438 | u->mark_begin_lineno = openfile->current->lineno; |
Benno Schulenberg | ec26fd5 | 2015-11-12 19:01:57 +0000 | [diff] [blame] | 1439 | /* When the insertion contains no newline, store the adjusted |
| 1440 | * x position; otherwise, store the length of the last line. */ |
| 1441 | if (openfile->fileage == openfile->filebot) |
| 1442 | u->mark_begin_x = openfile->current_x; |
| 1443 | else |
| 1444 | u->mark_begin_x = strlen(openfile->filebot->data); |
Chris Allegretta | 12ba557 | 2009-03-26 01:01:48 +0000 | [diff] [blame] | 1445 | break; |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1446 | case ENTER: |
Benno Schulenberg | f9274a5 | 2015-11-12 19:50:33 +0000 | [diff] [blame] | 1447 | u->strdata = mallocstrcpy(NULL, openfile->current->data); |
| 1448 | u->mark_begin_x = openfile->current_x; |
Benno Schulenberg | f5ac8c1 | 2014-05-25 19:41:49 +0000 | [diff] [blame] | 1449 | break; |
Chris Allegretta | c9f0799 | 2009-12-03 03:12:00 +0000 | [diff] [blame] | 1450 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1451 | case SPLIT_BEGIN: |
| 1452 | case SPLIT_END: |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 1453 | #endif |
Benno Schulenberg | 45fe2ad | 2014-06-18 20:11:52 +0000 | [diff] [blame] | 1454 | case JOIN: |
Benno Schulenberg | 189fff4 | 2014-04-04 20:45:28 +0000 | [diff] [blame] | 1455 | /* These cases are handled by the earlier check for a new line and action. */ |
Benno Schulenberg | 530c786 | 2015-11-25 10:11:54 +0000 | [diff] [blame] | 1456 | break; |
| 1457 | default: |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 1458 | statusline(ALERT, _("Internal error: unknown type. " |
| 1459 | "Please save your work.")); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | } |
| 1462 | |
| 1463 | #ifdef DEBUG |
Benno Schulenberg | 3021a04 | 2015-06-17 15:17:09 +0000 | [diff] [blame] | 1464 | fprintf(stderr, " >> Done in update_undo (type was %d)\n", action); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1465 | #endif |
Benno Schulenberg | a81e141 | 2014-06-18 21:23:50 +0000 | [diff] [blame] | 1466 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1467 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1468 | |
| 1469 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | ef0d5a7 | 2006-05-22 02:08:49 +0000 | [diff] [blame] | 1470 | /* Unset the prepend_wrap flag. We need to do this as soon as we do |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1471 | * something other than type text. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1472 | void wrap_reset(void) |
| 1473 | { |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1474 | prepend_wrap = FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1475 | } |
| 1476 | |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1477 | /* Try wrapping the given line. Return TRUE if wrapped, FALSE otherwise. */ |
| 1478 | bool do_wrap(filestruct *line) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1479 | { |
| 1480 | size_t line_len; |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1481 | /* The length of the line we wrap. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1482 | ssize_t wrap_loc; |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1483 | /* The index of line->data where we wrap. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1484 | const char *after_break; |
| 1485 | /* The text after the wrap point. */ |
| 1486 | size_t after_break_len; |
| 1487 | /* The length of after_break. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1488 | const char *next_line = NULL; |
| 1489 | /* The next line, minus indentation. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1490 | size_t next_line_len = 0; |
| 1491 | /* The length of next_line. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1492 | |
| 1493 | /* There are three steps. First, we decide where to wrap. Then, we |
| 1494 | * create the new wrap line. Finally, we clean up. */ |
| 1495 | |
| 1496 | /* Step 1, finding where to wrap. We are going to add a new line |
| 1497 | * after a blank character. In this step, we call break_line() to |
| 1498 | * get the location of the last blank we can break the line at, and |
David Lawrence Ramsey | d4686b8 | 2006-06-11 19:14:14 +0000 | [diff] [blame] | 1499 | * set wrap_loc to the location of the character after it, so that |
| 1500 | * the blank is preserved at the end of the line. |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1501 | * |
| 1502 | * If there is no legal wrap point, or we reach the last character |
| 1503 | * of the line while trying to find one, we should return without |
| 1504 | * wrapping. Note that if autoindent is turned on, we don't break |
| 1505 | * at the end of it! */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1506 | assert(line != NULL && line->data != NULL); |
| 1507 | |
| 1508 | /* Save the length of the line. */ |
| 1509 | line_len = strlen(line->data); |
| 1510 | |
| 1511 | /* Find the last blank where we can break the line. */ |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1512 | wrap_loc = break_line(line->data, fill |
| 1513 | #ifndef DISABLE_HELP |
| 1514 | , FALSE |
| 1515 | #endif |
| 1516 | ); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1517 | |
| 1518 | /* If we couldn't break the line, or we've reached the end of it, we |
| 1519 | * don't wrap. */ |
| 1520 | if (wrap_loc == -1 || line->data[wrap_loc] == '\0') |
| 1521 | return FALSE; |
| 1522 | |
| 1523 | /* Otherwise, move forward to the character just after the blank. */ |
| 1524 | wrap_loc += move_mbright(line->data + wrap_loc, 0); |
| 1525 | |
| 1526 | /* If we've reached the end of the line, we don't wrap. */ |
| 1527 | if (line->data[wrap_loc] == '\0') |
| 1528 | return FALSE; |
| 1529 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1530 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1531 | /* If autoindent is turned on, and we're on the character just after |
| 1532 | * the indentation, we don't wrap. */ |
Benno Schulenberg | d0e8887 | 2015-11-02 11:24:22 +0000 | [diff] [blame] | 1533 | if (ISSET(AUTOINDENT) && wrap_loc == indent_length(line->data)) |
| 1534 | return FALSE; |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1535 | |
| 1536 | add_undo(SPLIT_BEGIN); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1537 | #endif |
| 1538 | |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1539 | size_t old_x = openfile->current_x; |
| 1540 | filestruct * oldLine = openfile->current; |
| 1541 | openfile->current = line; |
| 1542 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1543 | /* Step 2, making the new wrap line. It will consist of indentation |
| 1544 | * followed by the text after the wrap point, optionally followed by |
| 1545 | * a space (if the text after the wrap point doesn't end in a blank) |
David Lawrence Ramsey | 03979d7 | 2006-06-11 19:15:59 +0000 | [diff] [blame] | 1546 | * and the text of the next line, if they can fit without wrapping, |
| 1547 | * the next line exists, and the prepend_wrap flag is set. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1548 | |
| 1549 | /* after_break is the text that will be wrapped to the next line. */ |
| 1550 | after_break = line->data + wrap_loc; |
| 1551 | after_break_len = line_len - wrap_loc; |
| 1552 | |
| 1553 | assert(strlen(after_break) == after_break_len); |
| 1554 | |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1555 | /* We prepend the wrapped text to the next line, if the prepend_wrap |
| 1556 | * flag is set, there is a next line, and prepending would not make |
| 1557 | * the line too long. */ |
| 1558 | if (prepend_wrap && line != openfile->filebot) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1559 | const char *end = after_break + move_mbleft(after_break, |
| 1560 | after_break_len); |
| 1561 | |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1562 | /* Go to the end of the line. */ |
| 1563 | openfile->current_x = line_len; |
| 1564 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1565 | /* If after_break doesn't end in a blank, make sure it ends in a |
| 1566 | * space. */ |
Chris Allegretta | 9f98333 | 2016-02-25 21:04:45 +0000 | [diff] [blame] | 1567 | if (!is_blank_mbchar(end) && !ISSET(JUSTIFY_TRIM)) { |
Benno Schulenberg | 3b47ff7 | 2014-06-20 16:13:54 +0000 | [diff] [blame] | 1568 | #ifndef NANO_TINY |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1569 | add_undo(ADD); |
Benno Schulenberg | 3b47ff7 | 2014-06-20 16:13:54 +0000 | [diff] [blame] | 1570 | #endif |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1571 | line_len++; |
| 1572 | line->data = charealloc(line->data, line_len + 1); |
| 1573 | line->data[line_len - 1] = ' '; |
| 1574 | line->data[line_len] = '\0'; |
| 1575 | after_break = line->data + wrap_loc; |
| 1576 | after_break_len++; |
| 1577 | openfile->totsize++; |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1578 | openfile->current_x++; |
Benno Schulenberg | 3b47ff7 | 2014-06-20 16:13:54 +0000 | [diff] [blame] | 1579 | #ifndef NANO_TINY |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1580 | update_undo(ADD); |
Benno Schulenberg | 3b47ff7 | 2014-06-20 16:13:54 +0000 | [diff] [blame] | 1581 | #endif |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | next_line = line->next->data; |
| 1585 | next_line_len = strlen(next_line); |
| 1586 | |
| 1587 | if (after_break_len + next_line_len <= fill) { |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1588 | /* Delete the LF to join the two lines. */ |
| 1589 | do_delete(); |
| 1590 | /* Delete any leading blanks from the joined-on line. */ |
| 1591 | while (is_blank_mbchar(&line->data[openfile->current_x])) |
| 1592 | do_delete(); |
| 1593 | renumber(line); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1594 | } |
| 1595 | } |
| 1596 | |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1597 | /* Go to the wrap location and split the line there. */ |
| 1598 | openfile->current_x = wrap_loc; |
Benno Schulenberg | 54c2f6b | 2015-11-11 19:04:31 +0000 | [diff] [blame] | 1599 | do_enter(); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1600 | |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1601 | if (old_x < wrap_loc) { |
| 1602 | openfile->current_x = old_x; |
| 1603 | openfile->current = oldLine; |
| 1604 | prepend_wrap = TRUE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1605 | } else { |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1606 | openfile->current_x += (old_x - wrap_loc); |
David Lawrence Ramsey | b4e5c02 | 2005-11-25 13:48:09 +0000 | [diff] [blame] | 1607 | prepend_wrap = FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1608 | } |
| 1609 | |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1610 | openfile->placewewant = xplustabs(); |
| 1611 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1612 | #ifndef NANO_TINY |
Benno Schulenberg | be10c2a | 2014-06-09 10:01:54 +0000 | [diff] [blame] | 1613 | add_undo(SPLIT_END); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1614 | #endif |
| 1615 | |
| 1616 | return TRUE; |
| 1617 | } |
| 1618 | #endif /* !DISABLE_WRAPPING */ |
| 1619 | |
David Lawrence Ramsey | c7c04bb | 2005-11-29 21:30:00 +0000 | [diff] [blame] | 1620 | #if !defined(DISABLE_HELP) || !defined(DISABLE_WRAPJUSTIFY) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1621 | /* We are trying to break a chunk off line. We find the last blank such |
David Lawrence Ramsey | cd9a5f0 | 2005-09-20 06:12:54 +0000 | [diff] [blame] | 1622 | * that the display length to there is at most (goal + 1). If there is |
| 1623 | * no such blank, then we find the first blank. We then take the last |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1624 | * blank in that group of blanks. The terminating '\0' counts as a |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1625 | * blank, as does a '\n' if newln is TRUE. */ |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1626 | ssize_t break_line(const char *line, ssize_t goal |
| 1627 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1628 | , bool newln |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1629 | #endif |
| 1630 | ) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1631 | { |
| 1632 | ssize_t blank_loc = -1; |
| 1633 | /* Current tentative return value. Index of the last blank we |
Benno Schulenberg | 8f61511 | 2014-04-14 09:57:06 +0000 | [diff] [blame] | 1634 | * found with short enough display width. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1635 | ssize_t cur_loc = 0; |
| 1636 | /* Current index in line. */ |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 1637 | size_t cur_pos = 0; |
| 1638 | /* Current column position in line. */ |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1639 | int char_len = 0; |
| 1640 | /* Length of current character, in bytes. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1641 | |
| 1642 | assert(line != NULL); |
| 1643 | |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 1644 | while (*line != '\0' && goal >= cur_pos) { |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1645 | char_len = parse_mbchar(line, NULL, &cur_pos); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1646 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1647 | if (is_blank_mbchar(line) |
| 1648 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1649 | || (newln && *line == '\n') |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1650 | #endif |
| 1651 | ) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1652 | blank_loc = cur_loc; |
| 1653 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1654 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1655 | if (newln && *line == '\n') |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1656 | break; |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1657 | #endif |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1660 | line += char_len; |
| 1661 | cur_loc += char_len; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
David Lawrence Ramsey | 2d3d1e9 | 2006-05-18 17:28:16 +0000 | [diff] [blame] | 1664 | if (goal >= cur_pos) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1665 | /* In fact, the whole line displays shorter than goal. */ |
| 1666 | return cur_loc; |
| 1667 | |
Chris Allegretta | 09b8124 | 2008-07-12 01:54:49 +0000 | [diff] [blame] | 1668 | #ifndef DISABLE_HELP |
| 1669 | if (newln && blank_loc <= 0) { |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 1670 | /* If no blank was found, or was found only as the first |
| 1671 | * character, force a line break. */ |
| 1672 | cur_loc -= char_len; |
| 1673 | return cur_loc; |
Chris Allegretta | 09b8124 | 2008-07-12 01:54:49 +0000 | [diff] [blame] | 1674 | } |
| 1675 | #endif |
| 1676 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1677 | if (blank_loc == -1) { |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1678 | /* No blank was found within the goal width, |
| 1679 | * so now try and find a blank beyond it. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1680 | bool found_blank = FALSE; |
David Lawrence Ramsey | 5ab12ca | 2005-09-20 17:52:52 +0000 | [diff] [blame] | 1681 | ssize_t found_blank_loc = 0; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1682 | |
| 1683 | while (*line != '\0') { |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1684 | char_len = parse_mbchar(line, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1685 | |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1686 | if (is_blank_mbchar(line) |
| 1687 | #ifndef DISABLE_HELP |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 1688 | || (newln && *line == '\n') |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 1689 | #endif |
| 1690 | ) { |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1691 | found_blank = TRUE; |
David Lawrence Ramsey | bdc1b9b | 2005-09-20 16:36:08 +0000 | [diff] [blame] | 1692 | found_blank_loc = cur_loc; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1693 | } else if (found_blank) |
David Lawrence Ramsey | bdc1b9b | 2005-09-20 16:36:08 +0000 | [diff] [blame] | 1694 | return found_blank_loc; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1695 | |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1696 | line += char_len; |
| 1697 | cur_loc += char_len; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1698 | } |
| 1699 | |
| 1700 | return -1; |
| 1701 | } |
| 1702 | |
| 1703 | /* Move to the last blank after blank_loc, if there is one. */ |
| 1704 | line -= cur_loc; |
| 1705 | line += blank_loc; |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1706 | char_len = parse_mbchar(line, NULL, NULL); |
| 1707 | line += char_len; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1708 | |
Benno Schulenberg | a81e141 | 2014-06-18 21:23:50 +0000 | [diff] [blame] | 1709 | while (*line != '\0' && is_blank_mbchar(line)) { |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1710 | char_len = parse_mbchar(line, NULL, NULL); |
David Lawrence Ramsey | 39bd1b3 | 2006-05-20 13:11:56 +0000 | [diff] [blame] | 1711 | |
Benno Schulenberg | cd634e0 | 2014-04-14 13:02:43 +0000 | [diff] [blame] | 1712 | line += char_len; |
| 1713 | blank_loc += char_len; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1714 | } |
| 1715 | |
| 1716 | return blank_loc; |
| 1717 | } |
David Lawrence Ramsey | c7c04bb | 2005-11-29 21:30:00 +0000 | [diff] [blame] | 1718 | #endif /* !DISABLE_HELP || !DISABLE_WRAPJUSTIFY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1719 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1720 | #if !defined(NANO_TINY) || !defined(DISABLE_JUSTIFY) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1721 | /* The "indentation" of a line is the whitespace between the quote part |
| 1722 | * and the non-whitespace of the line. */ |
| 1723 | size_t indent_length(const char *line) |
| 1724 | { |
| 1725 | size_t len = 0; |
| 1726 | char *blank_mb; |
| 1727 | int blank_mb_len; |
| 1728 | |
| 1729 | assert(line != NULL); |
| 1730 | |
| 1731 | blank_mb = charalloc(mb_cur_max()); |
| 1732 | |
| 1733 | while (*line != '\0') { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1734 | blank_mb_len = parse_mbchar(line, blank_mb, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1735 | |
| 1736 | if (!is_blank_mbchar(blank_mb)) |
| 1737 | break; |
| 1738 | |
| 1739 | line += blank_mb_len; |
| 1740 | len += blank_mb_len; |
| 1741 | } |
| 1742 | |
| 1743 | free(blank_mb); |
| 1744 | |
| 1745 | return len; |
| 1746 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1747 | #endif /* !NANO_TINY || !DISABLE_JUSTIFY */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1748 | |
| 1749 | #ifndef DISABLE_JUSTIFY |
| 1750 | /* justify_format() replaces blanks with spaces and multiple spaces by 1 |
| 1751 | * (except it maintains up to 2 after a character in punct optionally |
| 1752 | * followed by a character in brackets, and removes all from the end). |
| 1753 | * |
| 1754 | * justify_format() might make paragraph->data shorter, and change the |
| 1755 | * actual pointer with null_at(). |
| 1756 | * |
| 1757 | * justify_format() will not look at the first skip characters of |
| 1758 | * paragraph. skip should be at most strlen(paragraph->data). The |
| 1759 | * character at paragraph[skip + 1] must not be blank. */ |
| 1760 | void justify_format(filestruct *paragraph, size_t skip) |
| 1761 | { |
| 1762 | char *end, *new_end, *new_paragraph_data; |
| 1763 | size_t shift = 0; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1764 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1765 | size_t mark_shift = 0; |
| 1766 | #endif |
| 1767 | |
| 1768 | /* These four asserts are assumptions about the input data. */ |
| 1769 | assert(paragraph != NULL); |
| 1770 | assert(paragraph->data != NULL); |
| 1771 | assert(skip < strlen(paragraph->data)); |
| 1772 | assert(!is_blank_mbchar(paragraph->data + skip)); |
| 1773 | |
| 1774 | end = paragraph->data + skip; |
| 1775 | new_paragraph_data = charalloc(strlen(paragraph->data) + 1); |
| 1776 | strncpy(new_paragraph_data, paragraph->data, skip); |
| 1777 | new_end = new_paragraph_data + skip; |
| 1778 | |
| 1779 | while (*end != '\0') { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1780 | int end_len; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1781 | |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1782 | /* If this character is blank, change it to a space if |
| 1783 | * necessary, and skip over all blanks after it. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1784 | if (is_blank_mbchar(end)) { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1785 | end_len = parse_mbchar(end, NULL, NULL); |
| 1786 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1787 | *new_end = ' '; |
| 1788 | new_end++; |
| 1789 | end += end_len; |
| 1790 | |
| 1791 | while (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1792 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1793 | |
| 1794 | end += end_len; |
| 1795 | shift += end_len; |
| 1796 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1797 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1798 | /* Keep track of the change in the current line. */ |
| 1799 | if (openfile->mark_set && openfile->mark_begin == |
| 1800 | paragraph && openfile->mark_begin_x >= end - |
| 1801 | paragraph->data) |
| 1802 | mark_shift += end_len; |
| 1803 | #endif |
| 1804 | } |
| 1805 | /* If this character is punctuation optionally followed by a |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1806 | * bracket and then followed by blanks, change no more than two |
| 1807 | * of the blanks to spaces if necessary, and skip over all |
| 1808 | * blanks after them. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1809 | } else if (mbstrchr(punct, end) != NULL) { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1810 | end_len = parse_mbchar(end, NULL, NULL); |
| 1811 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1812 | while (end_len > 0) { |
| 1813 | *new_end = *end; |
| 1814 | new_end++; |
| 1815 | end++; |
| 1816 | end_len--; |
| 1817 | } |
| 1818 | |
| 1819 | if (*end != '\0' && mbstrchr(brackets, end) != NULL) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1820 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1821 | |
| 1822 | while (end_len > 0) { |
| 1823 | *new_end = *end; |
| 1824 | new_end++; |
| 1825 | end++; |
| 1826 | end_len--; |
| 1827 | } |
| 1828 | } |
| 1829 | |
| 1830 | if (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1831 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1832 | |
| 1833 | *new_end = ' '; |
| 1834 | new_end++; |
| 1835 | end += end_len; |
| 1836 | } |
| 1837 | |
| 1838 | if (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1839 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1840 | |
| 1841 | *new_end = ' '; |
| 1842 | new_end++; |
| 1843 | end += end_len; |
| 1844 | } |
| 1845 | |
| 1846 | while (*end != '\0' && is_blank_mbchar(end)) { |
David Lawrence Ramsey | 96452cb | 2005-07-26 06:13:45 +0000 | [diff] [blame] | 1847 | end_len = parse_mbchar(end, NULL, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1848 | |
| 1849 | end += end_len; |
| 1850 | shift += end_len; |
| 1851 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1852 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1853 | /* Keep track of the change in the current line. */ |
| 1854 | if (openfile->mark_set && openfile->mark_begin == |
| 1855 | paragraph && openfile->mark_begin_x >= end - |
| 1856 | paragraph->data) |
| 1857 | mark_shift += end_len; |
| 1858 | #endif |
| 1859 | } |
| 1860 | /* If this character is neither blank nor punctuation, leave it |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1861 | * unchanged. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1862 | } else { |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1863 | end_len = parse_mbchar(end, NULL, NULL); |
| 1864 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1865 | while (end_len > 0) { |
| 1866 | *new_end = *end; |
| 1867 | new_end++; |
| 1868 | end++; |
| 1869 | end_len--; |
| 1870 | } |
| 1871 | } |
| 1872 | } |
| 1873 | |
| 1874 | assert(*end == '\0'); |
| 1875 | |
| 1876 | *new_end = *end; |
| 1877 | |
David Lawrence Ramsey | 30bdadd | 2005-12-31 21:08:10 +0000 | [diff] [blame] | 1878 | /* If there are spaces at the end of the line, remove them. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1879 | while (new_end > new_paragraph_data + skip && |
| 1880 | *(new_end - 1) == ' ') { |
| 1881 | new_end--; |
| 1882 | shift++; |
| 1883 | } |
| 1884 | |
| 1885 | if (shift > 0) { |
| 1886 | openfile->totsize -= shift; |
| 1887 | null_at(&new_paragraph_data, new_end - new_paragraph_data); |
| 1888 | free(paragraph->data); |
| 1889 | paragraph->data = new_paragraph_data; |
| 1890 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1891 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1892 | /* Adjust the mark coordinates to compensate for the change in |
| 1893 | * the current line. */ |
| 1894 | if (openfile->mark_set && openfile->mark_begin == paragraph) { |
| 1895 | openfile->mark_begin_x -= mark_shift; |
| 1896 | if (openfile->mark_begin_x > new_end - new_paragraph_data) |
| 1897 | openfile->mark_begin_x = new_end - new_paragraph_data; |
| 1898 | } |
| 1899 | #endif |
| 1900 | } else |
| 1901 | free(new_paragraph_data); |
| 1902 | } |
| 1903 | |
| 1904 | /* The "quote part" of a line is the largest initial substring matching |
| 1905 | * the quote string. This function returns the length of the quote part |
| 1906 | * of the given line. |
| 1907 | * |
| 1908 | * Note that if !HAVE_REGEX_H then we match concatenated copies of |
| 1909 | * quotestr. */ |
| 1910 | size_t quote_length(const char *line) |
| 1911 | { |
| 1912 | #ifdef HAVE_REGEX_H |
| 1913 | regmatch_t matches; |
| 1914 | int rc = regexec("ereg, line, 1, &matches, 0); |
| 1915 | |
| 1916 | if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1) |
| 1917 | return 0; |
| 1918 | /* matches.rm_so should be 0, since the quote string should start |
| 1919 | * with the caret ^. */ |
| 1920 | return matches.rm_eo; |
| 1921 | #else /* !HAVE_REGEX_H */ |
| 1922 | size_t qdepth = 0; |
| 1923 | |
| 1924 | /* Compute quote depth level. */ |
| 1925 | while (strncmp(line + qdepth, quotestr, quotelen) == 0) |
| 1926 | qdepth += quotelen; |
| 1927 | return qdepth; |
| 1928 | #endif /* !HAVE_REGEX_H */ |
| 1929 | } |
| 1930 | |
| 1931 | /* a_line and b_line are lines of text. The quotation part of a_line is |
| 1932 | * the first a_quote characters. Check that the quotation part of |
| 1933 | * b_line is the same. */ |
| 1934 | bool quotes_match(const char *a_line, size_t a_quote, const char |
| 1935 | *b_line) |
| 1936 | { |
| 1937 | /* Here is the assumption about a_quote. */ |
| 1938 | assert(a_quote == quote_length(a_line)); |
| 1939 | |
| 1940 | return (a_quote == quote_length(b_line) && |
| 1941 | strncmp(a_line, b_line, a_quote) == 0); |
| 1942 | } |
| 1943 | |
| 1944 | /* We assume a_line and b_line have no quote part. Then, we return |
| 1945 | * whether b_line could follow a_line in a paragraph. */ |
| 1946 | bool indents_match(const char *a_line, size_t a_indent, const char |
| 1947 | *b_line, size_t b_indent) |
| 1948 | { |
| 1949 | assert(a_indent == indent_length(a_line)); |
| 1950 | assert(b_indent == indent_length(b_line)); |
| 1951 | |
| 1952 | return (b_indent <= a_indent && |
| 1953 | strncmp(a_line, b_line, b_indent) == 0); |
| 1954 | } |
| 1955 | |
| 1956 | /* Is foo the beginning of a paragraph? |
| 1957 | * |
| 1958 | * A line of text consists of a "quote part", followed by an |
| 1959 | * "indentation part", followed by text. The functions quote_length() |
| 1960 | * and indent_length() calculate these parts. |
| 1961 | * |
| 1962 | * A line is "part of a paragraph" if it has a part not in the quote |
| 1963 | * part or the indentation. |
| 1964 | * |
| 1965 | * A line is "the beginning of a paragraph" if it is part of a |
| 1966 | * paragraph and |
| 1967 | * 1) it is the top line of the file, or |
| 1968 | * 2) the line above it is not part of a paragraph, or |
| 1969 | * 3) the line above it does not have precisely the same quote |
| 1970 | * part, or |
| 1971 | * 4) the indentation of this line is not an initial substring of |
| 1972 | * the indentation of the previous line, or |
| 1973 | * 5) this line has no quote part and some indentation, and |
| 1974 | * autoindent isn't turned on. |
| 1975 | * The reason for number 5) is that if autoindent isn't turned on, |
| 1976 | * then an indented line is expected to start a paragraph, as in |
| 1977 | * books. Thus, nano can justify an indented paragraph only if |
| 1978 | * autoindent is turned on. */ |
| 1979 | bool begpar(const filestruct *const foo) |
| 1980 | { |
David Lawrence Ramsey | 0083bd2 | 2005-11-09 18:26:44 +0000 | [diff] [blame] | 1981 | size_t quote_len, indent_len, temp_id_len; |
| 1982 | |
| 1983 | if (foo == NULL) |
| 1984 | return FALSE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1985 | |
| 1986 | /* Case 1). */ |
David Lawrence Ramsey | 0083bd2 | 2005-11-09 18:26:44 +0000 | [diff] [blame] | 1987 | if (foo == openfile->fileage) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 1988 | return TRUE; |
| 1989 | |
| 1990 | quote_len = quote_length(foo->data); |
| 1991 | indent_len = indent_length(foo->data + quote_len); |
| 1992 | |
| 1993 | /* Not part of a paragraph. */ |
| 1994 | if (foo->data[quote_len + indent_len] == '\0') |
| 1995 | return FALSE; |
| 1996 | |
| 1997 | /* Case 3). */ |
| 1998 | if (!quotes_match(foo->data, quote_len, foo->prev->data)) |
| 1999 | return TRUE; |
| 2000 | |
| 2001 | temp_id_len = indent_length(foo->prev->data + quote_len); |
| 2002 | |
| 2003 | /* Case 2) or 5) or 4). */ |
| 2004 | if (foo->prev->data[quote_len + temp_id_len] == '\0' || |
Benno Schulenberg | e4b8d6f | 2016-11-30 11:46:05 +0100 | [diff] [blame] | 2005 | (quote_len == 0 && indent_len > 0 && !ISSET(AUTOINDENT)) || |
| 2006 | !indents_match(foo->prev->data + quote_len, temp_id_len, |
| 2007 | foo->data + quote_len, indent_len)) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2008 | return TRUE; |
| 2009 | |
| 2010 | return FALSE; |
| 2011 | } |
| 2012 | |
| 2013 | /* Is foo inside a paragraph? */ |
| 2014 | bool inpar(const filestruct *const foo) |
| 2015 | { |
| 2016 | size_t quote_len; |
| 2017 | |
| 2018 | if (foo == NULL) |
| 2019 | return FALSE; |
| 2020 | |
| 2021 | quote_len = quote_length(foo->data); |
| 2022 | |
David Lawrence Ramsey | 2101403 | 2005-11-09 20:33:42 +0000 | [diff] [blame] | 2023 | return (foo->data[quote_len + indent_length(foo->data + |
| 2024 | quote_len)] != '\0'); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2025 | } |
| 2026 | |
David Lawrence Ramsey | af5a999 | 2005-11-09 23:06:44 +0000 | [diff] [blame] | 2027 | /* Move the next par_len lines, starting with first_line, into the |
David Lawrence Ramsey | 8bd960b | 2005-11-09 18:49:16 +0000 | [diff] [blame] | 2028 | * justify buffer, leaving copies of those lines in place. Assume that |
| 2029 | * par_len is greater than zero, and that there are enough lines after |
David Lawrence Ramsey | cd8f735 | 2005-11-10 21:20:32 +0000 | [diff] [blame] | 2030 | * first_line. */ |
| 2031 | void backup_lines(filestruct *first_line, size_t par_len) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2032 | { |
| 2033 | filestruct *top = first_line; |
| 2034 | /* The top of the paragraph we're backing up. */ |
| 2035 | filestruct *bot = first_line; |
| 2036 | /* The bottom of the paragraph we're backing up. */ |
| 2037 | size_t i; |
| 2038 | /* Generic loop variable. */ |
| 2039 | size_t current_x_save = openfile->current_x; |
| 2040 | ssize_t fl_lineno_save = first_line->lineno; |
| 2041 | ssize_t edittop_lineno_save = openfile->edittop->lineno; |
| 2042 | ssize_t current_lineno_save = openfile->current->lineno; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2043 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2044 | bool old_mark_set = openfile->mark_set; |
| 2045 | ssize_t mb_lineno_save = 0; |
| 2046 | size_t mark_begin_x_save = 0; |
| 2047 | |
| 2048 | if (old_mark_set) { |
| 2049 | mb_lineno_save = openfile->mark_begin->lineno; |
| 2050 | mark_begin_x_save = openfile->mark_begin_x; |
| 2051 | } |
| 2052 | #endif |
| 2053 | |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 2054 | /* par_len will be one greater than the number of lines between |
| 2055 | * current and filebot if filebot is the last line in the |
| 2056 | * paragraph. */ |
David Lawrence Ramsey | 8bd960b | 2005-11-09 18:49:16 +0000 | [diff] [blame] | 2057 | assert(par_len > 0 && openfile->current->lineno + par_len <= |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 2058 | openfile->filebot->lineno + 1); |
David Lawrence Ramsey | 8bd960b | 2005-11-09 18:49:16 +0000 | [diff] [blame] | 2059 | |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 2060 | /* Move bot down par_len lines to the line after the last line of |
| 2061 | * the paragraph, if there is one. */ |
| 2062 | for (i = par_len; i > 0 && bot != openfile->filebot; i--) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2063 | bot = bot->next; |
| 2064 | |
| 2065 | /* Move the paragraph from the current buffer's filestruct to the |
| 2066 | * justify buffer. */ |
David Lawrence Ramsey | 1cb945f | 2017-02-14 21:35:01 -0600 | [diff] [blame] | 2067 | extract_buffer(&jusbuffer, &jusbottom, top, 0, bot, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 2068 | (i == 1 && bot == openfile->filebot) ? strlen(bot->data) : 0); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2069 | |
| 2070 | /* Copy the paragraph back to the current buffer's filestruct from |
| 2071 | * the justify buffer. */ |
David Lawrence Ramsey | 1cb945f | 2017-02-14 21:35:01 -0600 | [diff] [blame] | 2072 | copy_from_buffer(jusbuffer); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2073 | |
| 2074 | /* Move upward from the last line of the paragraph to the first |
| 2075 | * line, putting first_line, edittop, current, and mark_begin at the |
| 2076 | * same lines in the copied paragraph that they had in the original |
| 2077 | * paragraph. */ |
David Lawrence Ramsey | ee43ea6 | 2007-04-22 15:04:05 +0000 | [diff] [blame] | 2078 | if (openfile->current != openfile->fileage) { |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 2079 | top = openfile->current->prev; |
David Lawrence Ramsey | ee43ea6 | 2007-04-22 15:04:05 +0000 | [diff] [blame] | 2080 | #ifndef NANO_TINY |
| 2081 | if (old_mark_set && |
| 2082 | openfile->current->lineno == mb_lineno_save) { |
| 2083 | openfile->mark_begin = openfile->current; |
| 2084 | openfile->mark_begin_x = mark_begin_x_save; |
| 2085 | } |
| 2086 | #endif |
| 2087 | } else |
David Lawrence Ramsey | 5c33e88 | 2005-11-09 18:58:04 +0000 | [diff] [blame] | 2088 | top = openfile->current; |
David Lawrence Ramsey | e8d505b | 2005-11-10 03:40:45 +0000 | [diff] [blame] | 2089 | for (i = par_len; i > 0 && top != NULL; i--) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2090 | if (top->lineno == fl_lineno_save) |
| 2091 | first_line = top; |
| 2092 | if (top->lineno == edittop_lineno_save) |
| 2093 | openfile->edittop = top; |
| 2094 | if (top->lineno == current_lineno_save) |
| 2095 | openfile->current = top; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2096 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2097 | if (old_mark_set && top->lineno == mb_lineno_save) { |
| 2098 | openfile->mark_begin = top; |
| 2099 | openfile->mark_begin_x = mark_begin_x_save; |
| 2100 | } |
| 2101 | #endif |
| 2102 | top = top->prev; |
| 2103 | } |
| 2104 | |
| 2105 | /* Put current_x at the same place in the copied paragraph that it |
| 2106 | * had in the original paragraph. */ |
| 2107 | openfile->current_x = current_x_save; |
| 2108 | |
| 2109 | set_modified(); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2110 | } |
| 2111 | |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 2112 | /* Find the beginning of the current paragraph if we're in one, or the |
| 2113 | * beginning of the next paragraph if we're not. Afterwards, save the |
| 2114 | * quote length and paragraph length in *quote and *par. Return TRUE if |
David Lawrence Ramsey | 139fa65 | 2006-05-22 01:26:24 +0000 | [diff] [blame] | 2115 | * we found a paragraph, and FALSE if there was an error or we didn't |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 2116 | * find a paragraph. |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2117 | * |
| 2118 | * See the comment at begpar() for more about when a line is the |
| 2119 | * beginning of a paragraph. */ |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 2120 | bool find_paragraph(size_t *const quote, size_t *const par) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2121 | { |
| 2122 | size_t quote_len; |
| 2123 | /* Length of the initial quotation of the paragraph we search |
| 2124 | * for. */ |
| 2125 | size_t par_len; |
| 2126 | /* Number of lines in the paragraph we search for. */ |
| 2127 | filestruct *current_save; |
| 2128 | /* The line at the beginning of the paragraph we search for. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2129 | |
| 2130 | #ifdef HAVE_REGEX_H |
| 2131 | if (quoterc != 0) { |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 2132 | statusline(ALERT, _("Bad quote string %s: %s"), quotestr, quoteerr); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2133 | return FALSE; |
| 2134 | } |
| 2135 | #endif |
| 2136 | |
| 2137 | assert(openfile->current != NULL); |
| 2138 | |
David Lawrence Ramsey | 1be131a | 2005-11-11 03:55:52 +0000 | [diff] [blame] | 2139 | /* If we're at the end of the last line of the file, it means that |
| 2140 | * there aren't any paragraphs left, so get out. */ |
| 2141 | if (openfile->current == openfile->filebot && openfile->current_x == |
| 2142 | strlen(openfile->filebot->data)) |
| 2143 | return FALSE; |
| 2144 | |
| 2145 | /* If the current line isn't in a paragraph, move forward to the |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 2146 | * last line of the next paragraph, if any. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2147 | if (!inpar(openfile->current)) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2148 | do_para_end(FALSE); |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 2149 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 2150 | /* If we end up past the beginning of the line, it means that |
| 2151 | * we're at the end of the last line of the file, and the line |
| 2152 | * isn't blank, in which case the last line of the file is the |
| 2153 | * last line of the next paragraph. |
| 2154 | * |
| 2155 | * Otherwise, if we end up on a line that's in a paragraph, it |
| 2156 | * means that we're on the line after the last line of the next |
| 2157 | * paragraph, in which case we should move back to the last line |
| 2158 | * of the next paragraph. */ |
| 2159 | if (openfile->current_x == 0) { |
| 2160 | if (!inpar(openfile->current->prev)) |
| 2161 | return FALSE; |
| 2162 | if (openfile->current != openfile->fileage) |
| 2163 | openfile->current = openfile->current->prev; |
| 2164 | } |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2165 | } |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 2166 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 2167 | /* If the current line isn't the first line of the paragraph, move |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 2168 | * back to the first line of the paragraph. */ |
| 2169 | if (!begpar(openfile->current)) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2170 | do_para_begin(FALSE); |
| 2171 | |
| 2172 | /* Now current is the first line of the paragraph. Set quote_len to |
| 2173 | * the quotation length of that line, and set par_len to the number |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 2174 | * of lines in this paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2175 | quote_len = quote_length(openfile->current->data); |
| 2176 | current_save = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2177 | do_para_end(FALSE); |
| 2178 | par_len = openfile->current->lineno - current_save->lineno; |
David Lawrence Ramsey | 9a065c0 | 2005-11-29 18:25:53 +0000 | [diff] [blame] | 2179 | |
David Lawrence Ramsey | d82dae0 | 2005-11-11 05:13:28 +0000 | [diff] [blame] | 2180 | /* If we end up past the beginning of the line, it means that we're |
| 2181 | * at the end of the last line of the file, and the line isn't |
| 2182 | * blank, in which case the last line of the file is part of the |
| 2183 | * paragraph. */ |
David Lawrence Ramsey | bdff665 | 2005-11-11 04:14:33 +0000 | [diff] [blame] | 2184 | if (openfile->current_x > 0) |
| 2185 | par_len++; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2186 | openfile->current = current_save; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2187 | |
| 2188 | /* Save the values of quote_len and par_len. */ |
| 2189 | assert(quote != NULL && par != NULL); |
| 2190 | |
| 2191 | *quote = quote_len; |
| 2192 | *par = par_len; |
| 2193 | |
| 2194 | return TRUE; |
| 2195 | } |
| 2196 | |
| 2197 | /* If full_justify is TRUE, justify the entire file. Otherwise, justify |
| 2198 | * the current paragraph. */ |
| 2199 | void do_justify(bool full_justify) |
| 2200 | { |
| 2201 | filestruct *first_par_line = NULL; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2202 | /* Will be the first line of the justified paragraph(s), if any. |
| 2203 | * For restoring after unjustify. */ |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 2204 | filestruct *last_par_line = NULL; |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 2205 | /* Will be the line after the last line of the justified |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2206 | * paragraph(s), if any. Also for restoring after unjustify. */ |
David Lawrence Ramsey | 82b5deb | 2005-11-10 06:07:57 +0000 | [diff] [blame] | 2207 | bool filebot_inpar = FALSE; |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 2208 | /* Whether the text at filebot is part of the current |
| 2209 | * paragraph. */ |
Benno Schulenberg | 97a5d12 | 2014-08-29 20:03:58 +0000 | [diff] [blame] | 2210 | int kbinput; |
| 2211 | /* The first keystroke after a justification. */ |
| 2212 | functionptrtype func; |
| 2213 | /* The function associated with that keystroke. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2214 | |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 2215 | /* We save these variables to be restored if the user |
| 2216 | * unjustifies. */ |
David Lawrence Ramsey | 52161ee | 2005-11-10 19:56:26 +0000 | [diff] [blame] | 2217 | filestruct *edittop_save = openfile->edittop; |
| 2218 | filestruct *current_save = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2219 | size_t current_x_save = openfile->current_x; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2220 | size_t totsize_save = openfile->totsize; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2221 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2222 | filestruct *mark_begin_save = openfile->mark_begin; |
| 2223 | size_t mark_begin_x_save = openfile->mark_begin_x; |
| 2224 | #endif |
David Lawrence Ramsey | 52161ee | 2005-11-10 19:56:26 +0000 | [diff] [blame] | 2225 | bool modified_save = openfile->modified; |
| 2226 | |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 2227 | /* Move to the beginning of the current line, so that justifying at |
David Lawrence Ramsey | bdff665 | 2005-11-11 04:14:33 +0000 | [diff] [blame] | 2228 | * the end of the last line of the file, if that line isn't blank, |
| 2229 | * will work the first time through. */ |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 2230 | openfile->current_x = 0; |
| 2231 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2232 | /* If we're justifying the entire file, start at the beginning. */ |
| 2233 | if (full_justify) |
| 2234 | openfile->current = openfile->fileage; |
| 2235 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2236 | while (TRUE) { |
| 2237 | size_t i; |
| 2238 | /* Generic loop variable. */ |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2239 | filestruct *curr_first_par_line; |
| 2240 | /* The first line of the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2241 | size_t quote_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2242 | /* Length of the initial quotation of the current |
| 2243 | * paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2244 | size_t indent_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2245 | /* Length of the initial indentation of the current |
| 2246 | * paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2247 | size_t par_len; |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2248 | /* Number of lines in the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2249 | ssize_t break_pos; |
| 2250 | /* Where we will break lines. */ |
| 2251 | char *indent_string; |
| 2252 | /* The first indentation that doesn't match the initial |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2253 | * indentation of the current paragraph. This is put at the |
| 2254 | * beginning of every line broken off the first justified |
| 2255 | * line of the paragraph. Note that this works because a |
| 2256 | * paragraph can only contain two indentations at most: the |
| 2257 | * initial one, and a different one starting on a line after |
| 2258 | * the first. See the comment at begpar() for more about |
| 2259 | * when a line is part of a paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2260 | |
| 2261 | /* Find the first line of the paragraph to be justified. That |
| 2262 | * is the start of this paragraph if we're in one, or the start |
| 2263 | * of the next otherwise. Save the quote length and paragraph |
| 2264 | * length (number of lines). Don't refresh the screen yet, |
| 2265 | * since we'll do that after we justify. |
| 2266 | * |
| 2267 | * If the search failed, we do one of two things. If we're |
David Lawrence Ramsey | 8b203d6 | 2005-11-11 03:17:44 +0000 | [diff] [blame] | 2268 | * justifying the whole file, and we've found at least one |
| 2269 | * paragraph, it means that we should justify all the way to the |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2270 | * last line of the file, so set the last line of the text to be |
| 2271 | * justified to the last line of the file and break out of the |
| 2272 | * loop. Otherwise, it means that there are no paragraph(s) to |
| 2273 | * justify, so refresh the screen and get out. */ |
David Lawrence Ramsey | 79383be | 2005-11-29 18:34:45 +0000 | [diff] [blame] | 2274 | if (!find_paragraph("e_len, &par_len)) { |
David Lawrence Ramsey | 8b203d6 | 2005-11-11 03:17:44 +0000 | [diff] [blame] | 2275 | if (full_justify && first_par_line != NULL) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2276 | last_par_line = openfile->filebot; |
| 2277 | break; |
| 2278 | } else { |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 2279 | refresh_needed = TRUE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2280 | return; |
| 2281 | } |
| 2282 | } |
| 2283 | |
David Lawrence Ramsey | b2d1c5f | 2005-11-10 06:01:41 +0000 | [diff] [blame] | 2284 | /* par_len will be one greater than the number of lines between |
| 2285 | * current and filebot if filebot is the last line in the |
| 2286 | * paragraph. Set filebot_inpar to TRUE if this is the case. */ |
| 2287 | filebot_inpar = (openfile->current->lineno + par_len == |
| 2288 | openfile->filebot->lineno + 1); |
| 2289 | |
David Lawrence Ramsey | cd8f735 | 2005-11-10 21:20:32 +0000 | [diff] [blame] | 2290 | /* If we haven't already done it, move the original paragraph(s) |
| 2291 | * to the justify buffer, splice a copy of the original |
| 2292 | * paragraph(s) into the file in the same place, and set |
| 2293 | * first_par_line to the first line of the copy. */ |
| 2294 | if (first_par_line == NULL) { |
| 2295 | backup_lines(openfile->current, full_justify ? |
David Lawrence Ramsey | 53f641f | 2005-11-10 21:57:56 +0000 | [diff] [blame] | 2296 | openfile->filebot->lineno - openfile->current->lineno + |
Benno Schulenberg | 964c10d | 2016-12-07 19:56:27 +0100 | [diff] [blame] | 2297 | ((openfile->filebot->data[0] != '\0') ? 1 : 0) : par_len); |
David Lawrence Ramsey | cd8f735 | 2005-11-10 21:20:32 +0000 | [diff] [blame] | 2298 | first_par_line = openfile->current; |
| 2299 | } |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2300 | |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2301 | /* Set curr_first_par_line to the first line of the current |
| 2302 | * paragraph. */ |
| 2303 | curr_first_par_line = openfile->current; |
| 2304 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2305 | /* Initialize indent_string to a blank string. */ |
| 2306 | indent_string = mallocstrcpy(NULL, ""); |
| 2307 | |
| 2308 | /* Find the first indentation in the paragraph that doesn't |
David Lawrence Ramsey | 8602fd6 | 2006-05-28 18:43:21 +0000 | [diff] [blame] | 2309 | * match the indentation of the first line, and save it in |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2310 | * indent_string. If all the indentations are the same, save |
| 2311 | * the indentation of the first line in indent_string. */ |
| 2312 | { |
| 2313 | const filestruct *indent_line = openfile->current; |
| 2314 | bool past_first_line = FALSE; |
| 2315 | |
| 2316 | for (i = 0; i < par_len; i++) { |
| 2317 | indent_len = quote_len + |
| 2318 | indent_length(indent_line->data + quote_len); |
| 2319 | |
| 2320 | if (indent_len != strlen(indent_string)) { |
| 2321 | indent_string = mallocstrncpy(indent_string, |
| 2322 | indent_line->data, indent_len + 1); |
| 2323 | indent_string[indent_len] = '\0'; |
| 2324 | |
| 2325 | if (past_first_line) |
| 2326 | break; |
| 2327 | } |
| 2328 | |
| 2329 | if (indent_line == openfile->current) |
| 2330 | past_first_line = TRUE; |
| 2331 | |
| 2332 | indent_line = indent_line->next; |
| 2333 | } |
| 2334 | } |
| 2335 | |
| 2336 | /* Now tack all the lines of the paragraph together, skipping |
| 2337 | * the quoting and indentation on all lines after the first. */ |
| 2338 | for (i = 0; i < par_len - 1; i++) { |
| 2339 | filestruct *next_line = openfile->current->next; |
| 2340 | size_t line_len = strlen(openfile->current->data); |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2341 | size_t next_line_len = strlen(openfile->current->next->data); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2342 | |
| 2343 | indent_len = quote_len + |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2344 | indent_length(openfile->current->next->data + quote_len); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2345 | |
| 2346 | next_line_len -= indent_len; |
| 2347 | openfile->totsize -= indent_len; |
| 2348 | |
| 2349 | /* We're just about to tack the next line onto this one. If |
| 2350 | * this line isn't empty, make sure it ends in a space. */ |
Benno Schulenberg | 964c10d | 2016-12-07 19:56:27 +0100 | [diff] [blame] | 2351 | if (line_len > 0 && openfile->current->data[line_len - 1] != ' ') { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2352 | line_len++; |
| 2353 | openfile->current->data = |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2354 | charealloc(openfile->current->data, line_len + 1); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2355 | openfile->current->data[line_len - 1] = ' '; |
| 2356 | openfile->current->data[line_len] = '\0'; |
| 2357 | openfile->totsize++; |
| 2358 | } |
| 2359 | |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2360 | openfile->current->data = charealloc(openfile->current->data, |
| 2361 | line_len + next_line_len + 1); |
| 2362 | strcat(openfile->current->data, next_line->data + indent_len); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2363 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2364 | #ifndef NANO_TINY |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2365 | /* If needed, adjust the coordinates of the mark. */ |
Benno Schulenberg | 964c10d | 2016-12-07 19:56:27 +0100 | [diff] [blame] | 2366 | if (openfile->mark_set && openfile->mark_begin == next_line) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2367 | openfile->mark_begin = openfile->current; |
| 2368 | openfile->mark_begin_x += line_len - indent_len; |
| 2369 | } |
| 2370 | #endif |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2371 | /* Don't destroy edittop! */ |
| 2372 | if (next_line == openfile->edittop) |
| 2373 | openfile->edittop = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2374 | |
| 2375 | unlink_node(next_line); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2376 | |
| 2377 | /* If we've removed the next line, we need to go through |
| 2378 | * this line again. */ |
| 2379 | i--; |
| 2380 | |
| 2381 | par_len--; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2382 | openfile->totsize--; |
| 2383 | } |
| 2384 | |
| 2385 | /* Call justify_format() on the paragraph, which will remove |
| 2386 | * excess spaces from it and change all blank characters to |
| 2387 | * spaces. */ |
| 2388 | justify_format(openfile->current, quote_len + |
| 2389 | indent_length(openfile->current->data + quote_len)); |
| 2390 | |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2391 | while (par_len > 0 && strlenpt(openfile->current->data) > fill) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2392 | size_t line_len = strlen(openfile->current->data); |
| 2393 | |
| 2394 | indent_len = strlen(indent_string); |
| 2395 | |
| 2396 | /* If this line is too long, try to wrap it to the next line |
| 2397 | * to make it short enough. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 2398 | break_pos = break_line(openfile->current->data + indent_len, |
David Lawrence Ramsey | b9b2fd5 | 2005-11-22 21:13:36 +0000 | [diff] [blame] | 2399 | fill - strnlenpt(openfile->current->data, indent_len) |
| 2400 | #ifndef DISABLE_HELP |
| 2401 | , FALSE |
| 2402 | #endif |
| 2403 | ); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2404 | |
| 2405 | /* We can't break the line, or don't need to, so get out. */ |
| 2406 | if (break_pos == -1 || break_pos + indent_len == line_len) |
| 2407 | break; |
| 2408 | |
| 2409 | /* Move forward to the character after the indentation and |
| 2410 | * just after the space. */ |
| 2411 | break_pos += indent_len + 1; |
| 2412 | |
| 2413 | assert(break_pos <= line_len); |
| 2414 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2415 | /* If this paragraph is non-quoted, and autoindent isn't |
| 2416 | * turned on, set the indentation length to zero so that the |
| 2417 | * indentation is treated as part of the line. */ |
Benno Schulenberg | e4b8d6f | 2016-11-30 11:46:05 +0100 | [diff] [blame] | 2418 | if (quote_len == 0 && !ISSET(AUTOINDENT)) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2419 | indent_len = 0; |
| 2420 | |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2421 | /* Insert a new line after the current one. */ |
| 2422 | splice_node(openfile->current, make_new_node(openfile->current)); |
| 2423 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2424 | /* Copy the text after where we're going to break the |
| 2425 | * current line to the next line. */ |
| 2426 | openfile->current->next->data = charalloc(indent_len + 1 + |
| 2427 | line_len - break_pos); |
| 2428 | strncpy(openfile->current->next->data, indent_string, |
| 2429 | indent_len); |
| 2430 | strcpy(openfile->current->next->data + indent_len, |
| 2431 | openfile->current->data + break_pos); |
| 2432 | |
| 2433 | par_len++; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2434 | openfile->totsize += indent_len + 1; |
| 2435 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2436 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2437 | /* Adjust the mark coordinates to compensate for the change |
| 2438 | * in the current line. */ |
Benno Schulenberg | 9a6f62f | 2015-12-08 19:09:14 +0000 | [diff] [blame] | 2439 | if (openfile->mark_set && |
| 2440 | openfile->mark_begin == openfile->current && |
| 2441 | openfile->mark_begin_x > break_pos) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2442 | openfile->mark_begin = openfile->current->next; |
| 2443 | openfile->mark_begin_x -= break_pos - indent_len; |
| 2444 | } |
| 2445 | #endif |
| 2446 | |
| 2447 | /* Break the current line. */ |
Benno Schulenberg | de3260c | 2017-02-22 17:24:10 +0100 | [diff] [blame] | 2448 | if (ISSET(JUSTIFY_TRIM)) { |
Benno Schulenberg | cdcd365 | 2016-05-17 11:33:21 +0200 | [diff] [blame] | 2449 | while (break_pos > 0 && |
| 2450 | is_blank_mbchar(&openfile->current->data[break_pos - 1])) { |
Chris Allegretta | 16c037f | 2016-02-24 04:46:44 +0000 | [diff] [blame] | 2451 | break_pos--; |
Benno Schulenberg | de3260c | 2017-02-22 17:24:10 +0100 | [diff] [blame] | 2452 | openfile->totsize--; |
Chris Allegretta | 16c037f | 2016-02-24 04:46:44 +0000 | [diff] [blame] | 2453 | } |
| 2454 | } |
| 2455 | null_at(&openfile->current->data, break_pos); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2456 | |
| 2457 | /* Go to the next line. */ |
| 2458 | par_len--; |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 2459 | openfile->current = openfile->current->next; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2460 | } |
| 2461 | |
| 2462 | /* We're done breaking lines, so we don't need indent_string |
| 2463 | * anymore. */ |
| 2464 | free(indent_string); |
| 2465 | |
David Lawrence Ramsey | 5455b6a | 2005-11-09 20:17:12 +0000 | [diff] [blame] | 2466 | /* Go to the next line, if possible. If there is no next line, |
| 2467 | * move to the end of the current line. */ |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 2468 | if (openfile->current != openfile->filebot) { |
David Lawrence Ramsey | 2c5d0ec | 2005-11-09 19:06:01 +0000 | [diff] [blame] | 2469 | openfile->current = openfile->current->next; |
| 2470 | } else |
| 2471 | openfile->current_x = strlen(openfile->current->data); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2472 | |
Benno Schulenberg | 0f3e303 | 2016-12-02 17:37:11 +0100 | [diff] [blame] | 2473 | /* Renumber the now-justified paragraph, since both refreshing the |
| 2474 | * edit window and finding a paragraph need correct line numbers. */ |
David Lawrence Ramsey | a685468 | 2005-11-30 21:19:42 +0000 | [diff] [blame] | 2475 | renumber(curr_first_par_line); |
David Lawrence Ramsey | ad1b64c | 2005-11-29 19:00:09 +0000 | [diff] [blame] | 2476 | |
| 2477 | /* We've just finished justifying the paragraph. If we're not |
| 2478 | * justifying the entire file, break out of the loop. |
| 2479 | * Otherwise, continue the loop so that we justify all the |
| 2480 | * paragraphs in the file. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2481 | if (!full_justify) |
| 2482 | break; |
| 2483 | } |
| 2484 | |
| 2485 | /* We are now done justifying the paragraph or the file, so clean |
David Lawrence Ramsey | b1c2062 | 2016-12-22 13:01:27 -0600 | [diff] [blame] | 2486 | * up. totsize has been maintained above. If we actually justified |
| 2487 | * something, set last_par_line to the new end of the paragraph. */ |
David Lawrence Ramsey | ad1b64c | 2005-11-29 19:00:09 +0000 | [diff] [blame] | 2488 | if (first_par_line != NULL) |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 2489 | last_par_line = openfile->current; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2490 | |
| 2491 | edit_refresh(); |
| 2492 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2493 | /* Display the shortcut list with UnJustify. */ |
Benno Schulenberg | bc6e9aa | 2014-04-07 09:02:22 +0000 | [diff] [blame] | 2494 | uncutfunc->desc = unjust_tag; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2495 | display_main_list(); |
| 2496 | |
| 2497 | /* Now get a keystroke and see if it's unjustify. If not, put back |
| 2498 | * the keystroke and return. */ |
Benno Schulenberg | 75d64e6 | 2015-05-28 13:02:29 +0000 | [diff] [blame] | 2499 | #ifndef NANO_TINY |
| 2500 | do { |
| 2501 | #endif |
| 2502 | statusbar(_("Can now UnJustify!")); |
Benno Schulenberg | f45a293 | 2016-03-29 14:09:17 +0000 | [diff] [blame] | 2503 | reset_cursor(); |
Benno Schulenberg | e085ebb | 2016-02-22 17:15:28 +0000 | [diff] [blame] | 2504 | curs_set(1); |
Benno Schulenberg | 75d64e6 | 2015-05-28 13:02:29 +0000 | [diff] [blame] | 2505 | kbinput = do_input(FALSE); |
| 2506 | #ifndef NANO_TINY |
| 2507 | } while (kbinput == KEY_WINCH); |
| 2508 | #endif |
| 2509 | |
Benno Schulenberg | 158388c | 2016-06-16 20:40:09 +0200 | [diff] [blame] | 2510 | /* If needed, unset the cursor-position suppression flag, so the cursor |
| 2511 | * position /will/ be displayed upon a return to the main loop. */ |
| 2512 | if (ISSET(CONST_UPDATE)) |
| 2513 | do_cursorpos(TRUE); |
| 2514 | |
Benno Schulenberg | 97a5d12 | 2014-08-29 20:03:58 +0000 | [diff] [blame] | 2515 | func = func_from_key(&kbinput); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2516 | |
Benno Schulenberg | 40e1fd3 | 2015-12-05 10:16:26 +0000 | [diff] [blame] | 2517 | if (func == do_uncut_text |
| 2518 | #ifndef NANO_TINY |
Benno Schulenberg | 17cf833 | 2016-05-30 09:09:36 +0200 | [diff] [blame] | 2519 | || func == do_undo |
Benno Schulenberg | 40e1fd3 | 2015-12-05 10:16:26 +0000 | [diff] [blame] | 2520 | #endif |
| 2521 | ) { |
Benno Schulenberg | 3264d0c | 2016-10-12 19:59:26 +0200 | [diff] [blame] | 2522 | /* If we actually justified something, then splice the preserved |
| 2523 | * unjustified text back into the file, */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2524 | if (first_par_line != NULL) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2525 | /* Partition the filestruct so that it contains only the |
| 2526 | * text of the justified paragraph. */ |
| 2527 | filepart = partition_filestruct(first_par_line, 0, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 2528 | last_par_line, filebot_inpar ? |
| 2529 | strlen(last_par_line->data) : 0); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2530 | |
Benno Schulenberg | 3264d0c | 2016-10-12 19:59:26 +0200 | [diff] [blame] | 2531 | /* Throw away the justified paragraph, and replace it with |
| 2532 | * the preserved unjustified text. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2533 | free_filestruct(openfile->fileage); |
| 2534 | openfile->fileage = jusbuffer; |
| 2535 | openfile->filebot = jusbottom; |
| 2536 | |
Benno Schulenberg | 3264d0c | 2016-10-12 19:59:26 +0200 | [diff] [blame] | 2537 | /* Unpartition the filestruct, to contain the entire text again. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2538 | unpartition_filestruct(&filepart); |
| 2539 | |
Benno Schulenberg | d9148e7 | 2016-10-12 19:20:39 +0200 | [diff] [blame] | 2540 | /* Renumber, from the beginning of the unjustified part. */ |
| 2541 | renumber(jusbuffer); |
| 2542 | |
Benno Schulenberg | 3264d0c | 2016-10-12 19:59:26 +0200 | [diff] [blame] | 2543 | /* Restore the old position, the size, and the mark. */ |
David Lawrence Ramsey | 874ec8f | 2005-11-10 19:28:27 +0000 | [diff] [blame] | 2544 | openfile->edittop = edittop_save; |
| 2545 | openfile->current = current_save; |
| 2546 | openfile->current_x = current_x_save; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2547 | openfile->totsize = totsize_save; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2548 | #ifndef NANO_TINY |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2549 | if (openfile->mark_set) { |
| 2550 | openfile->mark_begin = mark_begin_save; |
| 2551 | openfile->mark_begin_x = mark_begin_x_save; |
| 2552 | } |
| 2553 | #endif |
| 2554 | openfile->modified = modified_save; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2555 | if (!openfile->modified) |
| 2556 | titlebar(NULL); |
Benno Schulenberg | d9148e7 | 2016-10-12 19:20:39 +0200 | [diff] [blame] | 2557 | |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 2558 | refresh_needed = TRUE; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2559 | } |
| 2560 | } else { |
Benno Schulenberg | 8651fef | 2015-12-03 09:03:45 +0000 | [diff] [blame] | 2561 | /* Put the keystroke back into the queue. */ |
Benno Schulenberg | 91951ab | 2016-07-23 14:01:38 +0200 | [diff] [blame] | 2562 | unget_kbinput(kbinput, meta_key); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2563 | |
Benno Schulenberg | aa1ae0a | 2016-04-10 21:16:19 +0200 | [diff] [blame] | 2564 | /* Set the desired screen column (always zero, except at EOF). */ |
| 2565 | openfile->placewewant = xplustabs(); |
| 2566 | |
Benno Schulenberg | 40e1fd3 | 2015-12-05 10:16:26 +0000 | [diff] [blame] | 2567 | #ifndef NANO_TINY |
Benno Schulenberg | 8651fef | 2015-12-03 09:03:45 +0000 | [diff] [blame] | 2568 | /* Throw away the entire undo stack, to prevent a crash when |
| 2569 | * the user tries to undo something in the justified text. */ |
Benno Schulenberg | f845938 | 2016-01-15 16:44:50 +0000 | [diff] [blame] | 2570 | discard_until(NULL, openfile); |
Benno Schulenberg | 40e1fd3 | 2015-12-05 10:16:26 +0000 | [diff] [blame] | 2571 | #endif |
Benno Schulenberg | 3264d0c | 2016-10-12 19:59:26 +0200 | [diff] [blame] | 2572 | /* Blow away the unjustified text. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2573 | free_filestruct(jusbuffer); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2574 | } |
| 2575 | |
Benno Schulenberg | 3264d0c | 2016-10-12 19:59:26 +0200 | [diff] [blame] | 2576 | /* Mark the buffer for unjustified text as empty. */ |
| 2577 | jusbuffer = NULL; |
| 2578 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2579 | blank_statusbar(); |
| 2580 | |
| 2581 | /* Display the shortcut list with UnCut. */ |
Benno Schulenberg | bc6e9aa | 2014-04-07 09:02:22 +0000 | [diff] [blame] | 2582 | uncutfunc->desc = uncut_tag; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2583 | display_main_list(); |
| 2584 | } |
| 2585 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2586 | /* Justify the current paragraph. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2587 | void do_justify_void(void) |
| 2588 | { |
| 2589 | do_justify(FALSE); |
| 2590 | } |
| 2591 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2592 | /* Justify the entire file. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 2593 | void do_full_justify(void) |
| 2594 | { |
| 2595 | do_justify(TRUE); |
| 2596 | } |
| 2597 | #endif /* !DISABLE_JUSTIFY */ |
| 2598 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2599 | #ifndef DISABLE_SPELLER |
| 2600 | /* A word is misspelled in the file. Let the user replace it. We |
| 2601 | * return FALSE if the user cancels. */ |
| 2602 | bool do_int_spell_fix(const char *word) |
| 2603 | { |
Benno Schulenberg | 5022e47 | 2016-03-31 11:14:25 +0000 | [diff] [blame] | 2604 | char *save_search, *exp_word; |
Benno Schulenberg | 5d1752c | 2016-04-04 17:53:26 +0200 | [diff] [blame] | 2605 | size_t current_x_save = openfile->current_x; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2606 | filestruct *edittop_save = openfile->edittop; |
| 2607 | filestruct *current_save = openfile->current; |
| 2608 | /* Save where we are. */ |
Benno Schulenberg | 523bc0f | 2016-04-04 17:43:31 +0200 | [diff] [blame] | 2609 | bool proceed = FALSE; |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2610 | /* The return value of this function. */ |
Benno Schulenberg | 5022e47 | 2016-03-31 11:14:25 +0000 | [diff] [blame] | 2611 | bool result; |
| 2612 | /* The return value of searching for a misspelled word. */ |
Benno Schulenberg | 583a30e | 2015-04-21 17:37:59 +0000 | [diff] [blame] | 2613 | unsigned stash[sizeof(flags) / sizeof(flags[0])]; |
| 2614 | /* A storage place for the current flag settings. */ |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2615 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2616 | bool old_mark_set = openfile->mark_set; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2617 | bool right_side_up = FALSE; |
| 2618 | /* TRUE if (mark_begin, mark_begin_x) is the top of the mark, |
| 2619 | * FALSE if (current, current_x) is. */ |
| 2620 | filestruct *top, *bot; |
| 2621 | size_t top_x, bot_x; |
| 2622 | #endif |
| 2623 | |
Benno Schulenberg | 583a30e | 2015-04-21 17:37:59 +0000 | [diff] [blame] | 2624 | /* Save the settings of the global flags. */ |
| 2625 | memcpy(stash, flags, sizeof(flags)); |
| 2626 | |
Benno Schulenberg | 588daf9 | 2016-12-25 13:11:17 +0100 | [diff] [blame] | 2627 | /* Do the spell checking case sensitive, forward, and without regexes. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2628 | SET(CASE_SENSITIVE); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2629 | UNSET(BACKWARDS_SEARCH); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2630 | UNSET(USE_REGEXP); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2631 | |
Benno Schulenberg | 5174323 | 2016-03-28 19:14:33 +0000 | [diff] [blame] | 2632 | /* Save the current search string, then set it to the misspelled word. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2633 | save_search = last_search; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2634 | last_search = mallocstrcpy(NULL, word); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2635 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2636 | #ifndef NANO_TINY |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2637 | /* If the mark is on, start at the beginning of the marked region. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2638 | if (old_mark_set) { |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2639 | mark_order((const filestruct **)&top, &top_x, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 2640 | (const filestruct **)&bot, &bot_x, &right_side_up); |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2641 | /* If the region is marked normally, swap the end points, so that |
| 2642 | * (current, current_x) (where searching starts) is at the top. */ |
| 2643 | if (right_side_up) { |
| 2644 | openfile->current = top; |
Benno Schulenberg | 94e5632 | 2017-01-26 20:05:21 +0100 | [diff] [blame] | 2645 | openfile->current_x = top_x; |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2646 | openfile->mark_begin = bot; |
| 2647 | openfile->mark_begin_x = bot_x; |
Benno Schulenberg | 94e5632 | 2017-01-26 20:05:21 +0100 | [diff] [blame] | 2648 | } |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2649 | openfile->mark_set = FALSE; |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2650 | } else |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2651 | #endif |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2652 | /* Otherwise, start from the top of the file. */ |
| 2653 | { |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2654 | openfile->current = openfile->fileage; |
Benno Schulenberg | 94e5632 | 2017-01-26 20:05:21 +0100 | [diff] [blame] | 2655 | openfile->current_x = 0; |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2656 | } |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2657 | |
Benno Schulenberg | 5022e47 | 2016-03-31 11:14:25 +0000 | [diff] [blame] | 2658 | /* Find the first whole occurrence of word. */ |
Benno Schulenberg | 8f10e36 | 2017-02-10 13:51:51 +0100 | [diff] [blame] | 2659 | result = findnextstr(word, TRUE, TRUE, NULL, FALSE, NULL, 0); |
Benno Schulenberg | 5022e47 | 2016-03-31 11:14:25 +0000 | [diff] [blame] | 2660 | |
Benno Schulenberg | 14ac475 | 2016-08-03 12:50:56 +0200 | [diff] [blame] | 2661 | /* If the word isn't found, alert the user; if it is, allow correction. */ |
| 2662 | if (result == 0) { |
| 2663 | statusline(ALERT, _("Unfindable word: %s"), word); |
| 2664 | lastmessage = HUSH; |
| 2665 | proceed = TRUE; |
| 2666 | napms(2800); |
| 2667 | } else if (result == 1) { |
Benno Schulenberg | 5022e47 | 2016-03-31 11:14:25 +0000 | [diff] [blame] | 2668 | exp_word = display_string(openfile->current->data, xplustabs(), |
Benno Schulenberg | 5d1752c | 2016-04-04 17:53:26 +0200 | [diff] [blame] | 2669 | strlenpt(word), FALSE); |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2670 | edit_refresh(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2671 | |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2672 | spotlight(TRUE, exp_word); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2673 | |
Benno Schulenberg | 5022e47 | 2016-03-31 11:14:25 +0000 | [diff] [blame] | 2674 | /* Let the user supply a correctly spelled alternative. */ |
Benno Schulenberg | fd0589d | 2017-01-02 21:12:44 +0100 | [diff] [blame] | 2675 | proceed = (do_prompt(FALSE, FALSE, MSPELL, word, |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2676 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2677 | NULL, |
David Lawrence Ramsey | 9d8c284 | 2006-02-07 21:11:05 +0000 | [diff] [blame] | 2678 | #endif |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2679 | edit_refresh, _("Edit a replacement")) != -1); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2680 | |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2681 | spotlight(FALSE, exp_word); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2682 | |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2683 | free(exp_word); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2684 | |
Benno Schulenberg | 5022e47 | 2016-03-31 11:14:25 +0000 | [diff] [blame] | 2685 | /* If a replacement was given, go through all occurrences. */ |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2686 | if (proceed && strcmp(word, answer) != 0) { |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2687 | #ifndef NANO_TINY |
| 2688 | /* Replacements should happen only in the marked region. */ |
| 2689 | openfile->mark_set = old_mark_set; |
| 2690 | #endif |
Benno Schulenberg | 2cd8ca4 | 2016-10-23 17:59:26 +0200 | [diff] [blame] | 2691 | do_replace_loop(word, TRUE, current_save, ¤t_x_save); |
Benno Schulenberg | be4a3f8 | 2016-05-04 19:31:59 +0200 | [diff] [blame] | 2692 | |
| 2693 | /* TRANSLATORS: Shown after fixing misspellings in one word. */ |
Benno Schulenberg | f7c3e1c | 2016-05-18 10:25:16 +0200 | [diff] [blame] | 2694 | statusbar(_("Next word...")); |
Benno Schulenberg | be4a3f8 | 2016-05-04 19:31:59 +0200 | [diff] [blame] | 2695 | napms(400); |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2696 | } |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2697 | } |
| 2698 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2699 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2700 | if (old_mark_set) { |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2701 | /* Restore the (compensated) end points of the marked region. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2702 | if (right_side_up) { |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2703 | openfile->current = openfile->mark_begin; |
| 2704 | openfile->current_x = openfile->mark_begin_x; |
| 2705 | openfile->mark_begin = top; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2706 | openfile->mark_begin_x = top_x; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2707 | } else { |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2708 | openfile->current = top; |
| 2709 | openfile->current_x = top_x; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2710 | } |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2711 | openfile->mark_set = TRUE; |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2712 | } else |
| 2713 | #endif |
| 2714 | { |
| 2715 | /* Restore the (compensated) cursor position. */ |
| 2716 | openfile->current = current_save; |
| 2717 | openfile->current_x = current_x_save; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2718 | } |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2719 | |
Benno Schulenberg | 5174323 | 2016-03-28 19:14:33 +0000 | [diff] [blame] | 2720 | /* Restore the string that was last searched for. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2721 | free(last_search); |
| 2722 | last_search = save_search; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2723 | |
Benno Schulenberg | 433c7e5 | 2016-05-04 17:49:37 +0200 | [diff] [blame] | 2724 | /* Restore the viewport to where it was. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2725 | openfile->edittop = edittop_save; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2726 | |
Benno Schulenberg | 583a30e | 2015-04-21 17:37:59 +0000 | [diff] [blame] | 2727 | /* Restore the settings of the global flags. */ |
| 2728 | memcpy(flags, stash, sizeof(flags)); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2729 | |
Benno Schulenberg | 06ea93b | 2016-03-31 11:27:16 +0000 | [diff] [blame] | 2730 | return proceed; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2731 | } |
| 2732 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2733 | /* Internal (integrated) spell checking using the spell program, |
| 2734 | * filtered through the sort and uniq programs. Return NULL for normal |
| 2735 | * termination, and the error string otherwise. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2736 | const char *do_int_speller(const char *tempfile_name) |
| 2737 | { |
| 2738 | char *read_buff, *read_buff_ptr, *read_buff_word; |
| 2739 | size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread; |
| 2740 | int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1; |
| 2741 | pid_t pid_spell, pid_sort, pid_uniq; |
| 2742 | int spell_status, sort_status, uniq_status; |
| 2743 | |
| 2744 | /* Create all three pipes up front. */ |
Benno Schulenberg | c709c10 | 2016-03-30 12:30:14 +0000 | [diff] [blame] | 2745 | if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || pipe(uniq_fd) == -1) |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2746 | return _("Could not create pipe"); |
| 2747 | |
| 2748 | statusbar(_("Creating misspelled word list, please wait...")); |
| 2749 | |
| 2750 | /* A new process to run spell in. */ |
| 2751 | if ((pid_spell = fork()) == 0) { |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 2752 | /* Child continues (i.e. future spell process). */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2753 | close(spell_fd[0]); |
| 2754 | |
| 2755 | /* Replace the standard input with the temp file. */ |
| 2756 | if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1) |
| 2757 | goto close_pipes_and_exit; |
| 2758 | |
| 2759 | if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) |
| 2760 | goto close_pipes_and_exit; |
| 2761 | |
| 2762 | close(tempfile_fd); |
| 2763 | |
| 2764 | /* Send spell's standard output to the pipe. */ |
| 2765 | if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 2766 | goto close_pipes_and_exit; |
| 2767 | |
| 2768 | close(spell_fd[1]); |
| 2769 | |
David Lawrence Ramsey | 3239ff2 | 2005-11-29 20:01:06 +0000 | [diff] [blame] | 2770 | /* Start the spell program; we are using $PATH. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2771 | execlp("spell", "spell", NULL); |
| 2772 | |
| 2773 | /* This should not be reached if spell is found. */ |
| 2774 | exit(1); |
| 2775 | } |
| 2776 | |
| 2777 | /* Parent continues here. */ |
| 2778 | close(spell_fd[1]); |
| 2779 | |
| 2780 | /* A new process to run sort in. */ |
| 2781 | if ((pid_sort = fork()) == 0) { |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 2782 | /* Child continues (i.e. future sort process). Replace the |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2783 | * standard input with the standard output of the old pipe. */ |
| 2784 | if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 2785 | goto close_pipes_and_exit; |
| 2786 | |
| 2787 | close(spell_fd[0]); |
| 2788 | |
| 2789 | /* Send sort's standard output to the new pipe. */ |
| 2790 | if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 2791 | goto close_pipes_and_exit; |
| 2792 | |
| 2793 | close(sort_fd[1]); |
| 2794 | |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 2795 | /* Start the sort program. Use -f to ignore case. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2796 | execlp("sort", "sort", "-f", NULL); |
| 2797 | |
| 2798 | /* This should not be reached if sort is found. */ |
| 2799 | exit(1); |
| 2800 | } |
| 2801 | |
| 2802 | close(spell_fd[0]); |
| 2803 | close(sort_fd[1]); |
| 2804 | |
| 2805 | /* A new process to run uniq in. */ |
| 2806 | if ((pid_uniq = fork()) == 0) { |
David Lawrence Ramsey | b159f94 | 2006-07-28 17:06:27 +0000 | [diff] [blame] | 2807 | /* Child continues (i.e. future uniq process). Replace the |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2808 | * standard input with the standard output of the old pipe. */ |
| 2809 | if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 2810 | goto close_pipes_and_exit; |
| 2811 | |
| 2812 | close(sort_fd[0]); |
| 2813 | |
| 2814 | /* Send uniq's standard output to the new pipe. */ |
| 2815 | if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 2816 | goto close_pipes_and_exit; |
| 2817 | |
| 2818 | close(uniq_fd[1]); |
| 2819 | |
| 2820 | /* Start the uniq program; we are using PATH. */ |
| 2821 | execlp("uniq", "uniq", NULL); |
| 2822 | |
| 2823 | /* This should not be reached if uniq is found. */ |
| 2824 | exit(1); |
| 2825 | } |
| 2826 | |
| 2827 | close(sort_fd[0]); |
| 2828 | close(uniq_fd[1]); |
| 2829 | |
| 2830 | /* The child process was not forked successfully. */ |
| 2831 | if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) { |
| 2832 | close(uniq_fd[0]); |
| 2833 | return _("Could not fork"); |
| 2834 | } |
| 2835 | |
| 2836 | /* Get the system pipe buffer size. */ |
| 2837 | if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) { |
| 2838 | close(uniq_fd[0]); |
| 2839 | return _("Could not get size of pipe buffer"); |
| 2840 | } |
| 2841 | |
| 2842 | /* Read in the returned spelling errors. */ |
| 2843 | read_buff_read = 0; |
| 2844 | read_buff_size = pipe_buff_size + 1; |
| 2845 | read_buff = read_buff_ptr = charalloc(read_buff_size); |
| 2846 | |
Benno Schulenberg | c709c10 | 2016-03-30 12:30:14 +0000 | [diff] [blame] | 2847 | while ((bytesread = read(uniq_fd[0], read_buff_ptr, pipe_buff_size)) > 0) { |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2848 | read_buff_read += bytesread; |
| 2849 | read_buff_size += pipe_buff_size; |
Benno Schulenberg | c709c10 | 2016-03-30 12:30:14 +0000 | [diff] [blame] | 2850 | read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2851 | read_buff_ptr += read_buff_read; |
| 2852 | } |
| 2853 | |
| 2854 | *read_buff_ptr = '\0'; |
| 2855 | close(uniq_fd[0]); |
| 2856 | |
| 2857 | /* Process the spelling errors. */ |
| 2858 | read_buff_word = read_buff_ptr = read_buff; |
| 2859 | |
| 2860 | while (*read_buff_ptr != '\0') { |
| 2861 | if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) { |
| 2862 | *read_buff_ptr = '\0'; |
| 2863 | if (read_buff_word != read_buff_ptr) { |
| 2864 | if (!do_int_spell_fix(read_buff_word)) { |
| 2865 | read_buff_word = read_buff_ptr; |
| 2866 | break; |
| 2867 | } |
| 2868 | } |
| 2869 | read_buff_word = read_buff_ptr + 1; |
| 2870 | } |
| 2871 | read_buff_ptr++; |
| 2872 | } |
| 2873 | |
| 2874 | /* Special case: the last word doesn't end with '\r' or '\n'. */ |
| 2875 | if (read_buff_word != read_buff_ptr) |
| 2876 | do_int_spell_fix(read_buff_word); |
| 2877 | |
| 2878 | free(read_buff); |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2879 | search_replace_abort(); |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 2880 | refresh_needed = TRUE; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2881 | |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 2882 | /* Process the end of the three processes. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2883 | waitpid(pid_spell, &spell_status, 0); |
| 2884 | waitpid(pid_sort, &sort_status, 0); |
| 2885 | waitpid(pid_uniq, &uniq_status, 0); |
| 2886 | |
| 2887 | if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status)) |
| 2888 | return _("Error invoking \"spell\""); |
| 2889 | |
Benno Schulenberg | 5bd359d | 2014-04-15 15:02:43 +0000 | [diff] [blame] | 2890 | if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status)) |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2891 | return _("Error invoking \"sort -f\""); |
| 2892 | |
| 2893 | if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status)) |
| 2894 | return _("Error invoking \"uniq\""); |
| 2895 | |
Benno Schulenberg | c709c10 | 2016-03-30 12:30:14 +0000 | [diff] [blame] | 2896 | /* When all went okay. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2897 | return NULL; |
| 2898 | |
| 2899 | close_pipes_and_exit: |
| 2900 | /* Don't leak any handles. */ |
| 2901 | close(tempfile_fd); |
| 2902 | close(spell_fd[0]); |
| 2903 | close(spell_fd[1]); |
| 2904 | close(sort_fd[0]); |
| 2905 | close(sort_fd[1]); |
| 2906 | close(uniq_fd[0]); |
| 2907 | close(uniq_fd[1]); |
| 2908 | exit(1); |
| 2909 | } |
| 2910 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2911 | /* External (alternate) spell checking. Return NULL for normal |
| 2912 | * termination, and the error string otherwise. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2913 | const char *do_alt_speller(char *tempfile_name) |
| 2914 | { |
| 2915 | int alt_spell_status; |
| 2916 | size_t current_x_save = openfile->current_x; |
Benno Schulenberg | d8031af | 2016-08-11 18:24:59 +0200 | [diff] [blame] | 2917 | size_t pww_save = openfile->placewewant; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2918 | ssize_t lineno_save = openfile->current->lineno; |
Benno Schulenberg | 52d1c20 | 2015-03-17 20:10:59 +0000 | [diff] [blame] | 2919 | struct stat spellfileinfo; |
Chris Allegretta | a0a0556 | 2015-03-23 04:32:45 +0000 | [diff] [blame] | 2920 | time_t timestamp; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2921 | pid_t pid_spell; |
| 2922 | char *ptr; |
| 2923 | static int arglen = 3; |
| 2924 | static char **spellargs = NULL; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2925 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2926 | bool old_mark_set = openfile->mark_set; |
Benno Schulenberg | 674ab83 | 2015-07-13 05:32:17 +0000 | [diff] [blame] | 2927 | bool added_magicline = FALSE; |
| 2928 | /* Whether we added a magicline after filebot. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2929 | filestruct *top, *bot; |
Benno Schulenberg | d60e7d3 | 2017-02-16 11:44:04 +0100 | [diff] [blame] | 2930 | size_t top_x, bot_x, was_x, new_x; |
Benno Schulenberg | 6f2da6b | 2015-03-27 20:16:36 +0000 | [diff] [blame] | 2931 | bool right_side_up = FALSE; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2932 | ssize_t mb_lineno_save = 0; |
| 2933 | /* We're going to close the current file, and open the output of |
| 2934 | * the alternate spell command. The line that mark_begin points |
| 2935 | * to will be freed, so we save the line number and restore it |
| 2936 | * afterwards. */ |
Benno Schulenberg | 6039cd7 | 2015-07-17 21:11:32 +0000 | [diff] [blame] | 2937 | size_t size_of_surrounding = 0; |
Benno Schulenberg | aee8699 | 2015-07-12 19:27:26 +0000 | [diff] [blame] | 2938 | /* The size of the text outside of a marked region. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2939 | #endif |
| 2940 | |
Benno Schulenberg | 92142be | 2015-03-23 17:01:25 +0000 | [diff] [blame] | 2941 | /* Get the timestamp and the size of the temporary file. */ |
Benno Schulenberg | 52d1c20 | 2015-03-17 20:10:59 +0000 | [diff] [blame] | 2942 | stat(tempfile_name, &spellfileinfo); |
| 2943 | timestamp = spellfileinfo.st_mtime; |
| 2944 | |
Benno Schulenberg | 92142be | 2015-03-23 17:01:25 +0000 | [diff] [blame] | 2945 | /* If the number of bytes to check is zero, get out. */ |
| 2946 | if (spellfileinfo.st_size == 0) |
| 2947 | return NULL; |
| 2948 | |
Benno Schulenberg | a37ebcf | 2015-03-23 17:12:49 +0000 | [diff] [blame] | 2949 | #ifndef NANO_TINY |
| 2950 | if (old_mark_set) { |
| 2951 | /* If the mark is on, save the number of the line it starts on, |
| 2952 | * and then turn the mark off. */ |
| 2953 | mb_lineno_save = openfile->mark_begin->lineno; |
| 2954 | openfile->mark_set = FALSE; |
| 2955 | } |
| 2956 | #endif |
| 2957 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2958 | endwin(); |
| 2959 | |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 2960 | /* Set up an argument list to pass to execvp(). */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2961 | if (spellargs == NULL) { |
| 2962 | spellargs = (char **)nmalloc(arglen * sizeof(char *)); |
| 2963 | |
| 2964 | spellargs[0] = strtok(alt_speller, " "); |
| 2965 | while ((ptr = strtok(NULL, " ")) != NULL) { |
| 2966 | arglen++; |
| 2967 | spellargs = (char **)nrealloc(spellargs, arglen * |
| 2968 | sizeof(char *)); |
| 2969 | spellargs[arglen - 3] = ptr; |
| 2970 | } |
| 2971 | spellargs[arglen - 1] = NULL; |
| 2972 | } |
| 2973 | spellargs[arglen - 2] = tempfile_name; |
| 2974 | |
| 2975 | /* Start a new process for the alternate speller. */ |
| 2976 | if ((pid_spell = fork()) == 0) { |
David Lawrence Ramsey | 2e2112c | 2005-11-29 05:39:31 +0000 | [diff] [blame] | 2977 | /* Start alternate spell program; we are using $PATH. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2978 | execvp(spellargs[0], spellargs); |
| 2979 | |
| 2980 | /* Should not be reached, if alternate speller is found!!! */ |
| 2981 | exit(1); |
| 2982 | } |
| 2983 | |
| 2984 | /* If we couldn't fork, get out. */ |
| 2985 | if (pid_spell < 0) |
| 2986 | return _("Could not fork"); |
| 2987 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2988 | #ifndef NANO_TINY |
Benno Schulenberg | 9e6b9a2 | 2016-01-04 10:37:11 +0000 | [diff] [blame] | 2989 | /* Block SIGWINCHes so the spell checker doesn't get any. */ |
| 2990 | allow_sigwinch(FALSE); |
David Lawrence Ramsey | b18482e | 2005-07-26 00:06:34 +0000 | [diff] [blame] | 2991 | #endif |
| 2992 | |
| 2993 | /* Wait for the alternate spell checker to finish. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2994 | wait(&alt_spell_status); |
| 2995 | |
David Lawrence Ramsey | 84fdb90 | 2005-08-14 20:08:49 +0000 | [diff] [blame] | 2996 | /* Reenter curses mode. */ |
| 2997 | doupdate(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 2998 | |
| 2999 | /* Restore the terminal to its previous state. */ |
| 3000 | terminal_init(); |
| 3001 | |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3002 | if (!WIFEXITED(alt_spell_status) || WEXITSTATUS(alt_spell_status) != 0) { |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 3003 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3004 | /* Turn the mark back on if it was on before. */ |
| 3005 | openfile->mark_set = old_mark_set; |
| 3006 | #endif |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3007 | return invocation_error(alt_speller); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3008 | } |
| 3009 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 3010 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3011 | if (old_mark_set) { |
Benno Schulenberg | 74e7521 | 2015-07-18 10:32:01 +0000 | [diff] [blame] | 3012 | /* Trim the filestruct so that it contains only the marked text. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3013 | mark_order((const filestruct **)&top, &top_x, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 3014 | (const filestruct **)&bot, &bot_x, &right_side_up); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3015 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
Benno Schulenberg | 74e7521 | 2015-07-18 10:32:01 +0000 | [diff] [blame] | 3016 | |
| 3017 | /* Foresay whether a magicline will be added when the |
| 3018 | * spell-checked text is read back in. */ |
Benno Schulenberg | 674ab83 | 2015-07-13 05:32:17 +0000 | [diff] [blame] | 3019 | if (!ISSET(NO_NEWLINES)) |
| 3020 | added_magicline = (openfile->filebot->data[0] != '\0'); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3021 | |
Benno Schulenberg | aee8699 | 2015-07-12 19:27:26 +0000 | [diff] [blame] | 3022 | /* Compute the size of the text outside of the marked region. */ |
| 3023 | size_of_surrounding = openfile->totsize - get_totsize(top, bot); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3024 | } |
| 3025 | #endif |
| 3026 | |
Benno Schulenberg | 17cf833 | 2016-05-30 09:09:36 +0200 | [diff] [blame] | 3027 | /* Replace the text of the current buffer with the spell-checked text. */ |
David Lawrence Ramsey | 9c984e8 | 2005-11-08 19:15:58 +0000 | [diff] [blame] | 3028 | replace_buffer(tempfile_name); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3029 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 3030 | #ifndef NANO_TINY |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3031 | if (old_mark_set) { |
| 3032 | filestruct *top_save = openfile->fileage; |
Benno Schulenberg | 13ec5d8 | 2016-12-10 13:20:06 +0100 | [diff] [blame] | 3033 | |
| 3034 | /* If a magicline was added, remove it again. */ |
| 3035 | if (added_magicline) |
| 3036 | remove_magicline(); |
| 3037 | |
Benno Schulenberg | 6f2da6b | 2015-03-27 20:16:36 +0000 | [diff] [blame] | 3038 | /* Adjust the end point of the marked region for any change in |
Benno Schulenberg | 17cf833 | 2016-05-30 09:09:36 +0200 | [diff] [blame] | 3039 | * length of the region's last line. */ |
Benno Schulenberg | 6f2da6b | 2015-03-27 20:16:36 +0000 | [diff] [blame] | 3040 | if (right_side_up) |
Benno Schulenberg | d60e7d3 | 2017-02-16 11:44:04 +0100 | [diff] [blame] | 3041 | was_x = current_x_save; |
Benno Schulenberg | 6f2da6b | 2015-03-27 20:16:36 +0000 | [diff] [blame] | 3042 | else |
Benno Schulenberg | d60e7d3 | 2017-02-16 11:44:04 +0100 | [diff] [blame] | 3043 | was_x = openfile->mark_begin_x; |
| 3044 | if (top == bot) |
| 3045 | new_x = was_x - bot_x + top_x + strlen(openfile->filebot->data); |
| 3046 | else |
| 3047 | new_x = strlen(openfile->filebot->data); |
| 3048 | if (right_side_up) |
| 3049 | current_x_save = new_x; |
| 3050 | else |
| 3051 | openfile->mark_begin_x = new_x; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3052 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3053 | /* Unpartition the filestruct so that it contains all the text |
| 3054 | * again. Note that we've replaced the marked text originally |
| 3055 | * in the partition with the spell-checked marked text in the |
| 3056 | * temp file. */ |
| 3057 | unpartition_filestruct(&filepart); |
| 3058 | |
Benno Schulenberg | aee8699 | 2015-07-12 19:27:26 +0000 | [diff] [blame] | 3059 | /* Renumber, starting with the beginning line of the old partition. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3060 | renumber(top_save); |
Benno Schulenberg | aee8699 | 2015-07-12 19:27:26 +0000 | [diff] [blame] | 3061 | |
| 3062 | /* Add back the size of the text surrounding the marked region. */ |
| 3063 | openfile->totsize += size_of_surrounding; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3064 | |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 3065 | /* Restore the position of the mark, and turn it back on. */ |
Benno Schulenberg | 6f2da6b | 2015-03-27 20:16:36 +0000 | [diff] [blame] | 3066 | openfile->mark_begin = fsfromline(mb_lineno_save); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3067 | openfile->mark_set = TRUE; |
| 3068 | } |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 3069 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3070 | |
Benno Schulenberg | 52d1c20 | 2015-03-17 20:10:59 +0000 | [diff] [blame] | 3071 | /* Go back to the old position. */ |
Benno Schulenberg | 82d737e | 2015-03-21 21:02:13 +0000 | [diff] [blame] | 3072 | goto_line_posx(lineno_save, current_x_save); |
Benno Schulenberg | edbc1e5 | 2016-12-24 12:09:47 +0100 | [diff] [blame] | 3073 | if (openfile->current_x > strlen(openfile->current->data)) |
| 3074 | openfile->current_x = strlen(openfile->current->data); |
Benno Schulenberg | d8031af | 2016-08-11 18:24:59 +0200 | [diff] [blame] | 3075 | openfile->placewewant = pww_save; |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 3076 | adjust_viewport(STATIONARY); |
Benno Schulenberg | 52d1c20 | 2015-03-17 20:10:59 +0000 | [diff] [blame] | 3077 | |
| 3078 | /* Stat the temporary file again, and mark the buffer as modified only |
| 3079 | * if this file was changed since it was written. */ |
| 3080 | stat(tempfile_name, &spellfileinfo); |
Benno Schulenberg | d21a9c8 | 2016-12-23 22:12:48 +0100 | [diff] [blame] | 3081 | if (spellfileinfo.st_mtime != timestamp) { |
Benno Schulenberg | 52d1c20 | 2015-03-17 20:10:59 +0000 | [diff] [blame] | 3082 | set_modified(); |
Felix Janda | fc89ac1 | 2016-12-29 13:43:06 -0500 | [diff] [blame] | 3083 | #ifndef NANO_TINY |
Benno Schulenberg | d21a9c8 | 2016-12-23 22:12:48 +0100 | [diff] [blame] | 3084 | /* Flush the undo stack, to avoid making a mess when the user |
| 3085 | * tries to undo things in spell-corrected lines. */ |
| 3086 | discard_until(NULL, openfile); |
Felix Janda | fc89ac1 | 2016-12-29 13:43:06 -0500 | [diff] [blame] | 3087 | #endif |
Benno Schulenberg | d21a9c8 | 2016-12-23 22:12:48 +0100 | [diff] [blame] | 3088 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 3089 | #ifndef NANO_TINY |
Benno Schulenberg | 9e6b9a2 | 2016-01-04 10:37:11 +0000 | [diff] [blame] | 3090 | /* Unblock SIGWINCHes again. */ |
| 3091 | allow_sigwinch(TRUE); |
David Lawrence Ramsey | 3fe08ac | 2005-07-26 01:17:16 +0000 | [diff] [blame] | 3092 | #endif |
| 3093 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3094 | return NULL; |
| 3095 | } |
| 3096 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 3097 | /* Spell check the current file. If an alternate spell checker is |
| 3098 | * specified, use it. Otherwise, use the internal spell checker. */ |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3099 | void do_spell(void) |
| 3100 | { |
David Lawrence Ramsey | 9e7b2d5 | 2007-01-11 22:46:22 +0000 | [diff] [blame] | 3101 | bool status; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3102 | FILE *temp_file; |
Benno Schulenberg | 9eca195 | 2016-01-02 16:01:04 +0000 | [diff] [blame] | 3103 | char *temp; |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3104 | const char *spell_msg; |
| 3105 | |
Chris Allegretta | ab53864 | 2010-11-12 06:22:12 +0000 | [diff] [blame] | 3106 | if (ISSET(RESTRICTED)) { |
Benno Schulenberg | 6847616 | 2015-07-30 18:10:16 +0000 | [diff] [blame] | 3107 | show_restricted_warning(); |
Chris Allegretta | ab53864 | 2010-11-12 06:22:12 +0000 | [diff] [blame] | 3108 | return; |
| 3109 | } |
| 3110 | |
Benno Schulenberg | 9eca195 | 2016-01-02 16:01:04 +0000 | [diff] [blame] | 3111 | temp = safe_tempfile(&temp_file); |
| 3112 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3113 | if (temp == NULL) { |
Benno Schulenberg | 24b1017 | 2016-06-27 09:27:49 +0200 | [diff] [blame] | 3114 | statusline(ALERT, _("Error writing temp file: %s"), strerror(errno)); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3115 | return; |
| 3116 | } |
| 3117 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 3118 | #ifndef NANO_TINY |
Benno Schulenberg | 21edf7b | 2016-05-08 13:03:47 +0200 | [diff] [blame] | 3119 | if (openfile->mark_set) |
| 3120 | status = write_marked_file(temp, temp_file, TRUE, OVERWRITE); |
| 3121 | else |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3122 | #endif |
Benno Schulenberg | 21edf7b | 2016-05-08 13:03:47 +0200 | [diff] [blame] | 3123 | status = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3124 | |
David Lawrence Ramsey | 9e7b2d5 | 2007-01-11 22:46:22 +0000 | [diff] [blame] | 3125 | if (!status) { |
Benno Schulenberg | 24b1017 | 2016-06-27 09:27:49 +0200 | [diff] [blame] | 3126 | statusline(ALERT, _("Error writing temp file: %s"), strerror(errno)); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3127 | free(temp); |
| 3128 | return; |
| 3129 | } |
| 3130 | |
Benno Schulenberg | 85c2c2a | 2014-05-12 11:50:58 +0000 | [diff] [blame] | 3131 | blank_bottombars(); |
| 3132 | statusbar(_("Invoking spell checker, please wait")); |
Benno Schulenberg | 85c2c2a | 2014-05-12 11:50:58 +0000 | [diff] [blame] | 3133 | |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3134 | spell_msg = (alt_speller != NULL) ? do_alt_speller(temp) : |
Benno Schulenberg | 21edf7b | 2016-05-08 13:03:47 +0200 | [diff] [blame] | 3135 | do_int_speller(temp); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3136 | unlink(temp); |
| 3137 | free(temp); |
| 3138 | |
| 3139 | /* If the spell-checker printed any error messages onscreen, make |
| 3140 | * sure that they're cleared off. */ |
David Lawrence Ramsey | f32e1dd | 2006-06-09 17:09:51 +0000 | [diff] [blame] | 3141 | total_refresh(); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3142 | |
| 3143 | if (spell_msg != NULL) { |
| 3144 | if (errno == 0) |
| 3145 | /* Don't display an error message of "Success". */ |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 3146 | statusline(ALERT, _("Spell checking failed: %s"), spell_msg); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3147 | else |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 3148 | statusline(ALERT, _("Spell checking failed: %s: %s"), spell_msg, |
Benno Schulenberg | 21edf7b | 2016-05-08 13:03:47 +0200 | [diff] [blame] | 3149 | strerror(errno)); |
David Lawrence Ramsey | cc8185f | 2005-07-25 02:33:45 +0000 | [diff] [blame] | 3150 | } else |
| 3151 | statusbar(_("Finished checking spelling")); |
| 3152 | } |
| 3153 | #endif /* !DISABLE_SPELLER */ |
| 3154 | |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 3155 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 0dd351a | 2016-01-04 10:05:52 +0000 | [diff] [blame] | 3156 | /* Run a linting program on the current buffer. Return NULL for normal |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3157 | * termination, and the error string otherwise. */ |
| 3158 | void do_linter(void) |
| 3159 | { |
| 3160 | char *read_buff, *read_buff_ptr, *read_buff_word, *ptr; |
| 3161 | size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread; |
| 3162 | size_t parsesuccess = 0; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3163 | int lint_status, lint_fd[2]; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3164 | pid_t pid_lint; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3165 | static int arglen = 3; |
| 3166 | static char **lintargs = NULL; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3167 | char *lintcopy, *convendptr = NULL; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3168 | lintstruct *lints = NULL, *tmplint = NULL, *curlint = NULL; |
| 3169 | |
Benno Schulenberg | e4294a2 | 2015-07-29 17:36:39 +0000 | [diff] [blame] | 3170 | if (ISSET(RESTRICTED)) { |
Benno Schulenberg | 6847616 | 2015-07-30 18:10:16 +0000 | [diff] [blame] | 3171 | show_restricted_warning(); |
| 3172 | return; |
Benno Schulenberg | e4294a2 | 2015-07-29 17:36:39 +0000 | [diff] [blame] | 3173 | } |
| 3174 | |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3175 | if (!openfile->syntax || !openfile->syntax->linter) { |
Benno Schulenberg | 127ce15 | 2014-05-13 08:12:52 +0000 | [diff] [blame] | 3176 | statusbar(_("No linter defined for this type of file!")); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3177 | return; |
| 3178 | } |
| 3179 | |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3180 | if (openfile->modified) { |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 3181 | int i = do_yesno_prompt(FALSE, _("Save modified buffer before linting?")); |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3182 | |
Chris Allegretta | 61523be | 2014-05-11 03:09:00 +0000 | [diff] [blame] | 3183 | if (i == -1) { |
| 3184 | statusbar(_("Cancelled")); |
Benno Schulenberg | 9106cc8 | 2016-05-08 12:01:33 +0200 | [diff] [blame] | 3185 | return; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3186 | } else if (i == 1 && (do_writeout(FALSE) != TRUE)) |
Benno Schulenberg | 9106cc8 | 2016-05-08 12:01:33 +0200 | [diff] [blame] | 3187 | return; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3188 | } |
| 3189 | |
| 3190 | lintcopy = mallocstrcpy(NULL, openfile->syntax->linter); |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3191 | /* Create a pipe up front. */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3192 | if (pipe(lint_fd) == -1) { |
| 3193 | statusbar(_("Could not create pipe")); |
Benno Schulenberg | 9106cc8 | 2016-05-08 12:01:33 +0200 | [diff] [blame] | 3194 | return; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3195 | } |
| 3196 | |
Chris Allegretta | 61523be | 2014-05-11 03:09:00 +0000 | [diff] [blame] | 3197 | blank_bottombars(); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3198 | statusbar(_("Invoking linter, please wait")); |
| 3199 | |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 3200 | /* Set up an argument list to pass to execvp(). */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3201 | if (lintargs == NULL) { |
| 3202 | lintargs = (char **)nmalloc(arglen * sizeof(char *)); |
| 3203 | |
| 3204 | lintargs[0] = strtok(lintcopy, " "); |
| 3205 | while ((ptr = strtok(NULL, " ")) != NULL) { |
| 3206 | arglen++; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3207 | lintargs = (char **)nrealloc(lintargs, arglen * sizeof(char *)); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3208 | lintargs[arglen - 3] = ptr; |
| 3209 | } |
| 3210 | lintargs[arglen - 1] = NULL; |
| 3211 | } |
| 3212 | lintargs[arglen - 2] = openfile->filename; |
| 3213 | |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3214 | /* Start a new process to run the linter in. */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3215 | if ((pid_lint = fork()) == 0) { |
| 3216 | |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3217 | /* Child continues here (i.e. the future linting process). */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3218 | close(lint_fd[0]); |
| 3219 | |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 3220 | /* Send the linter's standard output + err to the pipe. */ |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 3221 | if (dup2(lint_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3222 | exit(9); |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 3223 | if (dup2(lint_fd[1], STDERR_FILENO) != STDERR_FILENO) |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3224 | exit(9); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3225 | |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 3226 | close(lint_fd[1]); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3227 | |
| 3228 | /* Start the linter program; we are using $PATH. */ |
| 3229 | execvp(lintargs[0], lintargs); |
| 3230 | |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3231 | /* This is only reached when the linter is not found. */ |
| 3232 | exit(9); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3233 | } |
| 3234 | |
| 3235 | /* Parent continues here. */ |
| 3236 | close(lint_fd[1]); |
| 3237 | |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3238 | /* If the child process was not forked successfully... */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3239 | if (pid_lint < 0) { |
| 3240 | close(lint_fd[0]); |
| 3241 | statusbar(_("Could not fork")); |
Benno Schulenberg | 9106cc8 | 2016-05-08 12:01:33 +0200 | [diff] [blame] | 3242 | return; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | /* Get the system pipe buffer size. */ |
| 3246 | if ((pipe_buff_size = fpathconf(lint_fd[0], _PC_PIPE_BUF)) < 1) { |
| 3247 | close(lint_fd[0]); |
Benno Schulenberg | a65ef42 | 2014-03-17 21:36:37 +0000 | [diff] [blame] | 3248 | statusbar(_("Could not get size of pipe buffer")); |
Benno Schulenberg | 9106cc8 | 2016-05-08 12:01:33 +0200 | [diff] [blame] | 3249 | return; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3250 | } |
| 3251 | |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 3252 | /* Read in the returned syntax errors. */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3253 | read_buff_read = 0; |
| 3254 | read_buff_size = pipe_buff_size + 1; |
| 3255 | read_buff = read_buff_ptr = charalloc(read_buff_size); |
| 3256 | |
Benno Schulenberg | c709c10 | 2016-03-30 12:30:14 +0000 | [diff] [blame] | 3257 | while ((bytesread = read(lint_fd[0], read_buff_ptr, pipe_buff_size)) > 0) { |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3258 | #ifdef DEBUG |
Benno Schulenberg | acda255 | 2014-06-09 15:08:59 +0000 | [diff] [blame] | 3259 | fprintf(stderr, "text.c:do_linter:%ld bytes (%s)\n", (long)bytesread, read_buff_ptr); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3260 | #endif |
| 3261 | read_buff_read += bytesread; |
| 3262 | read_buff_size += pipe_buff_size; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3263 | read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3264 | read_buff_ptr += read_buff_read; |
| 3265 | } |
| 3266 | |
| 3267 | *read_buff_ptr = '\0'; |
| 3268 | close(lint_fd[0]); |
| 3269 | |
| 3270 | #ifdef DEBUG |
| 3271 | fprintf(stderr, "text.c:do_lint:Raw output: %s\n", read_buff); |
| 3272 | #endif |
| 3273 | |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3274 | /* Process the linter output. */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3275 | read_buff_word = read_buff_ptr = read_buff; |
| 3276 | |
| 3277 | while (*read_buff_ptr != '\0') { |
| 3278 | if ((*read_buff_ptr == '\r') || (*read_buff_ptr == '\n')) { |
| 3279 | *read_buff_ptr = '\0'; |
| 3280 | if (read_buff_word != read_buff_ptr) { |
| 3281 | char *filename = NULL, *linestr = NULL, *maybecol = NULL; |
| 3282 | char *message = mallocstrcpy(NULL, read_buff_word); |
| 3283 | |
| 3284 | /* At the moment we're assuming the following formats: |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 3285 | * |
| 3286 | * filenameorcategory:line:column:message (e.g. splint) |
| 3287 | * filenameorcategory:line:message (e.g. pyflakes) |
| 3288 | * filenameorcategory:line,col:message (e.g. pylint) |
| 3289 | * |
| 3290 | * This could be turned into some scanf() based parser, |
| 3291 | * but ugh. */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3292 | if ((filename = strtok(read_buff_word, ":")) != NULL) { |
| 3293 | if ((linestr = strtok(NULL, ":")) != NULL) { |
| 3294 | if ((maybecol = strtok(NULL, ":")) != NULL) { |
| 3295 | ssize_t tmplineno = 0, tmpcolno = 0; |
| 3296 | char *tmplinecol; |
| 3297 | |
| 3298 | tmplineno = strtol(linestr, NULL, 10); |
| 3299 | if (tmplineno <= 0) { |
| 3300 | read_buff_ptr++; |
| 3301 | free(message); |
| 3302 | continue; |
| 3303 | } |
| 3304 | |
Benno Schulenberg | 52e3533 | 2014-05-16 11:03:04 +0000 | [diff] [blame] | 3305 | tmpcolno = strtol(maybecol, &convendptr, 10); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3306 | if (*convendptr != '\0') { |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 3307 | /* Previous field might still be |
| 3308 | * line,col format. */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3309 | strtok(linestr, ","); |
| 3310 | if ((tmplinecol = strtok(NULL, ",")) != NULL) |
| 3311 | tmpcolno = strtol(tmplinecol, NULL, 10); |
| 3312 | } |
| 3313 | |
| 3314 | #ifdef DEBUG |
Benno Schulenberg | acda255 | 2014-06-09 15:08:59 +0000 | [diff] [blame] | 3315 | fprintf(stderr, "text.c:do_lint:Successful parse! %ld:%ld:%s\n", (long)tmplineno, (long)tmpcolno, message); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3316 | #endif |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 3317 | /* Nice. We have a lint message we can use. */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3318 | parsesuccess++; |
| 3319 | tmplint = curlint; |
| 3320 | curlint = nmalloc(sizeof(lintstruct)); |
| 3321 | curlint->next = NULL; |
| 3322 | curlint->prev = tmplint; |
| 3323 | if (curlint->prev != NULL) |
| 3324 | curlint->prev->next = curlint; |
| 3325 | curlint->msg = mallocstrcpy(NULL, message); |
| 3326 | curlint->lineno = tmplineno; |
| 3327 | curlint->colno = tmpcolno; |
| 3328 | curlint->filename = mallocstrcpy(NULL, filename); |
| 3329 | |
| 3330 | if (lints == NULL) |
| 3331 | lints = curlint; |
| 3332 | } |
| 3333 | } |
| 3334 | } else |
| 3335 | free(message); |
| 3336 | } |
| 3337 | read_buff_word = read_buff_ptr + 1; |
| 3338 | } |
| 3339 | read_buff_ptr++; |
| 3340 | } |
| 3341 | |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3342 | /* Process the end of the linting process. */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3343 | waitpid(pid_lint, &lint_status, 0); |
| 3344 | |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3345 | if (!WIFEXITED(lint_status) || WEXITSTATUS(lint_status) > 2) { |
| 3346 | statusbar(invocation_error(openfile->syntax->linter)); |
Benno Schulenberg | 9106cc8 | 2016-05-08 12:01:33 +0200 | [diff] [blame] | 3347 | return; |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3350 | free(read_buff); |
| 3351 | |
| 3352 | if (parsesuccess == 0) { |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 3353 | statusline(HUSH, _("Got 0 parsable lines from command: %s"), |
| 3354 | openfile->syntax->linter); |
Benno Schulenberg | 9106cc8 | 2016-05-08 12:01:33 +0200 | [diff] [blame] | 3355 | return; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3356 | } |
| 3357 | |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3358 | bottombars(MLINTER); |
| 3359 | tmplint = NULL; |
| 3360 | curlint = lints; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3361 | |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3362 | while (TRUE) { |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3363 | int kbinput; |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3364 | functionptrtype func; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3365 | |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3366 | if (tmplint != curlint) { |
Benno Schulenberg | b8b29ff | 2014-04-03 21:06:30 +0000 | [diff] [blame] | 3367 | #ifndef NANO_TINY |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3368 | struct stat lintfileinfo; |
| 3369 | |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3370 | new_lint_loop: |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3371 | if (stat(curlint->filename, &lintfileinfo) != -1) { |
| 3372 | if (openfile->current_stat->st_ino != lintfileinfo.st_ino) { |
| 3373 | openfilestruct *tmpof = openfile; |
| 3374 | while (tmpof != openfile->next) { |
| 3375 | if (tmpof->current_stat->st_ino == lintfileinfo.st_ino) |
| 3376 | break; |
| 3377 | tmpof = tmpof->next; |
| 3378 | } |
| 3379 | if (tmpof->current_stat->st_ino != lintfileinfo.st_ino) { |
| 3380 | char *msg = charalloc(1024 + strlen(curlint->filename)); |
| 3381 | int i; |
| 3382 | |
Benno Schulenberg | 568d2a3 | 2016-02-13 19:41:12 +0000 | [diff] [blame] | 3383 | sprintf(msg, _("This message is for unopened file %s," |
| 3384 | " open it in a new buffer?"), |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3385 | curlint->filename); |
| 3386 | i = do_yesno_prompt(FALSE, msg); |
| 3387 | free(msg); |
Chris Allegretta | 61523be | 2014-05-11 03:09:00 +0000 | [diff] [blame] | 3388 | if (i == -1) { |
| 3389 | statusbar(_("Cancelled")); |
| 3390 | goto free_lints_and_return; |
| 3391 | } else if (i == 1) { |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3392 | SET(MULTIBUFFER); |
| 3393 | open_buffer(curlint->filename, FALSE); |
| 3394 | } else { |
| 3395 | char *dontwantfile = curlint->filename; |
| 3396 | |
| 3397 | while (curlint != NULL && !strcmp(curlint->filename, dontwantfile)) |
| 3398 | curlint = curlint->next; |
| 3399 | if (curlint == NULL) { |
Benno Schulenberg | 9d72efa | 2016-01-04 11:10:07 +0000 | [diff] [blame] | 3400 | statusbar(_("No more errors in unopened files, cancelling")); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3401 | break; |
| 3402 | } else |
| 3403 | goto new_lint_loop; |
| 3404 | } |
| 3405 | } else |
| 3406 | openfile = tmpof; |
| 3407 | } |
| 3408 | } |
Benno Schulenberg | e4c34c3 | 2014-03-17 14:15:57 +0000 | [diff] [blame] | 3409 | #endif /* !NANO_TINY */ |
Benno Schulenberg | 6a002f5 | 2016-02-22 19:49:45 +0000 | [diff] [blame] | 3410 | do_gotolinecolumn(curlint->lineno, curlint->colno, FALSE, FALSE); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3411 | titlebar(NULL); |
| 3412 | edit_refresh(); |
| 3413 | statusbar(curlint->msg); |
| 3414 | bottombars(MLINTER); |
| 3415 | } |
| 3416 | |
Benno Schulenberg | 79e3eaf | 2016-06-21 16:10:16 +0200 | [diff] [blame] | 3417 | /* Place and show the cursor to indicate the affected line. */ |
Benno Schulenberg | 7dd1030 | 2016-02-22 18:34:04 +0000 | [diff] [blame] | 3418 | reset_cursor(); |
Benno Schulenberg | 79e3eaf | 2016-06-21 16:10:16 +0200 | [diff] [blame] | 3419 | wnoutrefresh(edit); |
Benno Schulenberg | 568d2a3 | 2016-02-13 19:41:12 +0000 | [diff] [blame] | 3420 | curs_set(1); |
| 3421 | |
Benno Schulenberg | 7e5324d | 2014-06-30 18:04:33 +0000 | [diff] [blame] | 3422 | kbinput = get_kbinput(bottomwin); |
Benno Schulenberg | 75d64e6 | 2015-05-28 13:02:29 +0000 | [diff] [blame] | 3423 | |
| 3424 | #ifndef NANO_TINY |
| 3425 | if (kbinput == KEY_WINCH) |
| 3426 | continue; |
| 3427 | #endif |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3428 | func = func_from_key(&kbinput); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3429 | tmplint = curlint; |
| 3430 | |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3431 | if (func == do_cancel) |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3432 | break; |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3433 | else if (func == do_help_void) { |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3434 | tmplint = NULL; |
| 3435 | do_help_void(); |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3436 | } else if (func == do_page_down) { |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3437 | if (curlint->next != NULL) |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 3438 | curlint = curlint->next; |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3439 | else |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 3440 | statusbar(_("At last message")); |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3441 | } else if (func == do_page_up) { |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3442 | if (curlint->prev != NULL) |
| 3443 | curlint = curlint->prev; |
Benno Schulenberg | 47dffa4 | 2014-07-27 20:16:28 +0000 | [diff] [blame] | 3444 | else |
Benno Schulenberg | a9fdfd0 | 2014-07-16 08:53:16 +0000 | [diff] [blame] | 3445 | statusbar(_("At first message")); |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3446 | } |
| 3447 | } |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3448 | |
Chris Allegretta | 61523be | 2014-05-11 03:09:00 +0000 | [diff] [blame] | 3449 | blank_statusbar(); |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3450 | |
Benno Schulenberg | c3c2c66 | 2014-05-26 07:53:20 +0000 | [diff] [blame] | 3451 | #ifndef NANO_TINY |
Benno Schulenberg | 953ccc9 | 2015-06-28 14:12:25 +0000 | [diff] [blame] | 3452 | free_lints_and_return: |
Benno Schulenberg | c3c2c66 | 2014-05-26 07:53:20 +0000 | [diff] [blame] | 3453 | #endif |
Benno Schulenberg | d4118ef | 2016-02-11 08:50:11 +0000 | [diff] [blame] | 3454 | for (curlint = lints; curlint != NULL;) { |
| 3455 | tmplint = curlint; |
| 3456 | curlint = curlint->next; |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3457 | free(tmplint->msg); |
| 3458 | free(tmplint->filename); |
| 3459 | free(tmplint); |
| 3460 | } |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3461 | } |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3462 | |
Benno Schulenberg | 14a9c8a | 2015-03-27 16:55:49 +0000 | [diff] [blame] | 3463 | #ifndef DISABLE_SPELLER |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 3464 | /* Run a formatter for the current syntax. This expects the formatter |
| 3465 | * to be non-interactive and operate on a file in-place, which we'll |
| 3466 | * pass it on the command line. */ |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3467 | void do_formatter(void) |
| 3468 | { |
| 3469 | bool status; |
| 3470 | FILE *temp_file; |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3471 | int format_status; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3472 | ssize_t lineno_save = openfile->current->lineno; |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3473 | size_t current_x_save = openfile->current_x; |
| 3474 | size_t pww_save = openfile->placewewant; |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3475 | pid_t pid_format; |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3476 | static int arglen = 3; |
| 3477 | static char **formatargs = NULL; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3478 | char *temp, *ptr, *finalstatus = NULL; |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3479 | |
Benno Schulenberg | 9eca195 | 2016-01-02 16:01:04 +0000 | [diff] [blame] | 3480 | if (openfile->totsize == 0) { |
| 3481 | statusbar(_("Finished")); |
| 3482 | return; |
| 3483 | } |
| 3484 | |
| 3485 | temp = safe_tempfile(&temp_file); |
| 3486 | |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3487 | if (temp == NULL) { |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 3488 | statusline(ALERT, _("Error writing temp file: %s"), strerror(errno)); |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3489 | return; |
| 3490 | } |
| 3491 | |
Benno Schulenberg | 69d26c3 | 2015-03-14 20:17:21 +0000 | [diff] [blame] | 3492 | /* We're not supporting partial formatting, oi vey. */ |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3493 | openfile->mark_set = FALSE; |
| 3494 | status = write_file(temp, temp_file, TRUE, OVERWRITE, FALSE); |
| 3495 | |
| 3496 | if (!status) { |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 3497 | statusline(ALERT, _("Error writing temp file: %s"), strerror(errno)); |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3498 | free(temp); |
| 3499 | return; |
| 3500 | } |
| 3501 | |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3502 | blank_bottombars(); |
| 3503 | statusbar(_("Invoking formatter, please wait")); |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3504 | |
Benno Schulenberg | ce48ca2 | 2015-04-08 19:57:31 +0000 | [diff] [blame] | 3505 | /* Set up an argument list to pass to execvp(). */ |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3506 | if (formatargs == NULL) { |
| 3507 | formatargs = (char **)nmalloc(arglen * sizeof(char *)); |
| 3508 | |
| 3509 | formatargs[0] = strtok(openfile->syntax->formatter, " "); |
| 3510 | while ((ptr = strtok(NULL, " ")) != NULL) { |
| 3511 | arglen++; |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3512 | formatargs = (char **)nrealloc(formatargs, arglen * sizeof(char *)); |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3513 | formatargs[arglen - 3] = ptr; |
| 3514 | } |
| 3515 | formatargs[arglen - 1] = NULL; |
| 3516 | } |
| 3517 | formatargs[arglen - 2] = temp; |
| 3518 | |
| 3519 | /* Start a new process for the formatter. */ |
| 3520 | if ((pid_format = fork()) == 0) { |
Benno Schulenberg | 69d26c3 | 2015-03-14 20:17:21 +0000 | [diff] [blame] | 3521 | /* Start the formatting program; we are using $PATH. */ |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3522 | execvp(formatargs[0], formatargs); |
| 3523 | |
Benno Schulenberg | 69d26c3 | 2015-03-14 20:17:21 +0000 | [diff] [blame] | 3524 | /* Should not be reached, if the formatter is found! */ |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3525 | exit(1); |
| 3526 | } |
| 3527 | |
| 3528 | /* If we couldn't fork, get out. */ |
| 3529 | if (pid_format < 0) { |
| 3530 | statusbar(_("Could not fork")); |
Benno Schulenberg | 9eca195 | 2016-01-02 16:01:04 +0000 | [diff] [blame] | 3531 | unlink(temp); |
| 3532 | free(temp); |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3533 | return; |
| 3534 | } |
| 3535 | |
| 3536 | #ifndef NANO_TINY |
Benno Schulenberg | 9e6b9a2 | 2016-01-04 10:37:11 +0000 | [diff] [blame] | 3537 | /* Block SIGWINCHes so the formatter doesn't get any. */ |
| 3538 | allow_sigwinch(FALSE); |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3539 | #endif |
| 3540 | |
| 3541 | /* Wait for the formatter to finish. */ |
| 3542 | wait(&format_status); |
| 3543 | |
Benno Schulenberg | f225991 | 2015-04-17 09:24:17 +0000 | [diff] [blame] | 3544 | if (!WIFEXITED(format_status) || WEXITSTATUS(format_status) != 0) |
| 3545 | finalstatus = invocation_error(openfile->syntax->formatter); |
| 3546 | else { |
Benno Schulenberg | 69d26c3 | 2015-03-14 20:17:21 +0000 | [diff] [blame] | 3547 | /* Replace the text of the current buffer with the formatted text. */ |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3548 | replace_buffer(temp); |
| 3549 | |
Benno Schulenberg | edbc1e5 | 2016-12-24 12:09:47 +0100 | [diff] [blame] | 3550 | /* Restore the cursor position. */ |
Benno Schulenberg | 33bc96a | 2015-12-31 16:44:32 +0000 | [diff] [blame] | 3551 | goto_line_posx(lineno_save, current_x_save); |
Benno Schulenberg | edbc1e5 | 2016-12-24 12:09:47 +0100 | [diff] [blame] | 3552 | if (openfile->current_x > strlen(openfile->current->data)) |
| 3553 | openfile->current_x = strlen(openfile->current->data); |
Benno Schulenberg | 33bc96a | 2015-12-31 16:44:32 +0000 | [diff] [blame] | 3554 | openfile->placewewant = pww_save; |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 3555 | adjust_viewport(STATIONARY); |
Benno Schulenberg | 33bc96a | 2015-12-31 16:44:32 +0000 | [diff] [blame] | 3556 | |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3557 | set_modified(); |
| 3558 | |
Benno Schulenberg | d21a9c8 | 2016-12-23 22:12:48 +0100 | [diff] [blame] | 3559 | /* Flush the undo stack, to avoid a mess or crash when |
| 3560 | * the user tries to undo things in reformatted lines. */ |
| 3561 | discard_until(NULL, openfile); |
| 3562 | |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3563 | finalstatus = _("Finished formatting"); |
| 3564 | } |
| 3565 | |
| 3566 | unlink(temp); |
| 3567 | free(temp); |
| 3568 | |
Benno Schulenberg | bff6a90 | 2015-07-28 19:39:34 +0000 | [diff] [blame] | 3569 | #ifndef NANO_TINY |
Benno Schulenberg | 9e6b9a2 | 2016-01-04 10:37:11 +0000 | [diff] [blame] | 3570 | /* Unblock SIGWINCHes again. */ |
| 3571 | allow_sigwinch(TRUE); |
Benno Schulenberg | bff6a90 | 2015-07-28 19:39:34 +0000 | [diff] [blame] | 3572 | #endif |
| 3573 | |
Benno Schulenberg | e39938c | 2016-02-11 16:57:52 +0000 | [diff] [blame] | 3574 | statusbar(finalstatus); |
| 3575 | |
| 3576 | /* If there were error messages, allow the user some time to read them. */ |
| 3577 | if (WIFEXITED(format_status) && WEXITSTATUS(format_status) == 2) |
| 3578 | sleep(4); |
| 3579 | |
Benno Schulenberg | de5b263 | 2016-02-11 17:25:37 +0000 | [diff] [blame] | 3580 | /* If there were any messages, clear them off. */ |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3581 | total_refresh(); |
Chris Allegretta | 4b3f277 | 2015-01-03 07:24:17 +0000 | [diff] [blame] | 3582 | } |
Benno Schulenberg | 14a9c8a | 2015-03-27 16:55:49 +0000 | [diff] [blame] | 3583 | #endif /* !DISABLE_SPELLER */ |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 3584 | #endif /* !DISABLE_COLOR */ |
Chris Allegretta | 5575bfa | 2014-02-24 10:18:15 +0000 | [diff] [blame] | 3585 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 3586 | #ifndef NANO_TINY |
David Lawrence Ramsey | d7f0fe9 | 2005-08-10 22:51:49 +0000 | [diff] [blame] | 3587 | /* Our own version of "wc". Note that its character counts are in |
| 3588 | * multibyte characters instead of single-byte characters. */ |
David Lawrence Ramsey | 8e94234 | 2005-07-25 04:21:46 +0000 | [diff] [blame] | 3589 | void do_wordlinechar_count(void) |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3590 | { |
David Lawrence Ramsey | 520a90c | 2005-07-25 21:23:11 +0000 | [diff] [blame] | 3591 | size_t words = 0, chars = 0; |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 3592 | ssize_t nlines = 0; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 3593 | size_t current_x_save = openfile->current_x; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3594 | size_t pww_save = openfile->placewewant; |
| 3595 | filestruct *current_save = openfile->current; |
| 3596 | bool old_mark_set = openfile->mark_set; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3597 | filestruct *top, *bot; |
| 3598 | size_t top_x, bot_x; |
| 3599 | |
Benno Schulenberg | 74e7521 | 2015-07-18 10:32:01 +0000 | [diff] [blame] | 3600 | /* If the mark is on, partition the filestruct so that it |
| 3601 | * contains only the marked text, and turn the mark off. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3602 | if (old_mark_set) { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3603 | mark_order((const filestruct **)&top, &top_x, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 3604 | (const filestruct **)&bot, &bot_x, NULL); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3605 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3606 | openfile->mark_set = FALSE; |
| 3607 | } |
| 3608 | |
| 3609 | /* Start at the top of the file. */ |
| 3610 | openfile->current = openfile->fileage; |
| 3611 | openfile->current_x = 0; |
| 3612 | openfile->placewewant = 0; |
| 3613 | |
| 3614 | /* Keep moving to the next word (counting punctuation characters as |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 3615 | * part of a word, as "wc -w" does), without updating the screen, |
| 3616 | * until we reach the end of the file, incrementing the total word |
| 3617 | * count whenever we're on a word just before moving. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3618 | while (openfile->current != openfile->filebot || |
David Lawrence Ramsey | 2ffdea4 | 2005-11-03 21:08:39 +0000 | [diff] [blame] | 3619 | openfile->current->data[openfile->current_x] != '\0') { |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3620 | if (do_next_word(TRUE, FALSE)) |
| 3621 | words++; |
| 3622 | } |
| 3623 | |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 3624 | /* Get the total line and character counts, as "wc -l" and "wc -c" |
| 3625 | * do, but get the latter in multibyte characters. */ |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3626 | if (old_mark_set) { |
Benno Schulenberg | 74e7521 | 2015-07-18 10:32:01 +0000 | [diff] [blame] | 3627 | nlines = openfile->filebot->lineno - openfile->fileage->lineno + 1; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 3628 | chars = get_totsize(openfile->fileage, openfile->filebot); |
| 3629 | |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3630 | /* Unpartition the filestruct so that it contains all the text |
| 3631 | * again, and turn the mark back on. */ |
| 3632 | unpartition_filestruct(&filepart); |
| 3633 | openfile->mark_set = TRUE; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 3634 | } else { |
Chris Allegretta | 8b6461f | 2008-05-31 23:09:40 +0000 | [diff] [blame] | 3635 | nlines = openfile->filebot->lineno; |
David Lawrence Ramsey | 7293685 | 2005-07-25 03:47:08 +0000 | [diff] [blame] | 3636 | chars = openfile->totsize; |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3637 | } |
| 3638 | |
| 3639 | /* Restore where we were. */ |
| 3640 | openfile->current = current_save; |
| 3641 | openfile->current_x = current_x_save; |
| 3642 | openfile->placewewant = pww_save; |
| 3643 | |
Benno Schulenberg | 74e7521 | 2015-07-18 10:32:01 +0000 | [diff] [blame] | 3644 | /* Display the total word, line, and character counts on the statusbar. */ |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 3645 | statusline(HUSH, _("%sWords: %lu Lines: %ld Chars: %lu"), old_mark_set ? |
| 3646 | _("In Selection: ") : "", (unsigned long)words, (long)nlines, |
| 3647 | (unsigned long)chars); |
David Lawrence Ramsey | 691698a | 2005-07-24 19:57:51 +0000 | [diff] [blame] | 3648 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 3649 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 3650 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 3651 | /* Get verbatim input. */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 3652 | void do_verbatim_input(void) |
| 3653 | { |
| 3654 | int *kbinput; |
| 3655 | size_t kbinput_len, i; |
| 3656 | char *output; |
| 3657 | |
David Lawrence Ramsey | f451d6a | 2006-05-27 16:02:48 +0000 | [diff] [blame] | 3658 | /* TRANSLATORS: This is displayed when the next keystroke will be |
| 3659 | * inserted verbatim. */ |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 3660 | statusbar(_("Verbatim Input")); |
Benno Schulenberg | f45a293 | 2016-03-29 14:09:17 +0000 | [diff] [blame] | 3661 | reset_cursor(); |
| 3662 | curs_set(1); |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 3663 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 3664 | /* Read in all the verbatim characters. */ |
| 3665 | kbinput = get_verbatim_kbinput(edit, &kbinput_len); |
| 3666 | |
David Lawrence Ramsey | a620e68 | 2006-05-27 18:19:03 +0000 | [diff] [blame] | 3667 | /* If constant cursor position display is on, make sure the current |
| 3668 | * cursor position will be properly displayed on the statusbar. |
| 3669 | * Otherwise, blank the statusbar. */ |
| 3670 | if (ISSET(CONST_UPDATE)) |
| 3671 | do_cursorpos(TRUE); |
| 3672 | else { |
| 3673 | blank_statusbar(); |
| 3674 | wnoutrefresh(bottomwin); |
| 3675 | } |
David Lawrence Ramsey | 6fb6689 | 2006-05-27 17:39:19 +0000 | [diff] [blame] | 3676 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 3677 | /* Display all the verbatim characters at once, not filtering out |
| 3678 | * control characters. */ |
| 3679 | output = charalloc(kbinput_len + 1); |
| 3680 | |
| 3681 | for (i = 0; i < kbinput_len; i++) |
| 3682 | output[i] = (char)kbinput[i]; |
| 3683 | output[i] = '\0'; |
| 3684 | |
David Lawrence Ramsey | ad36bdc | 2006-12-02 17:22:21 +0000 | [diff] [blame] | 3685 | free(kbinput); |
| 3686 | |
David Lawrence Ramsey | 37ddfa9 | 2005-11-07 06:06:05 +0000 | [diff] [blame] | 3687 | do_output(output, kbinput_len, TRUE); |
| 3688 | |
| 3689 | free(output); |
| 3690 | } |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3691 | |
Benno Schulenberg | 68a0314 | 2016-12-07 13:10:40 +0100 | [diff] [blame] | 3692 | #ifdef ENABLE_WORDCOMPLETION |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3693 | /* Copy the found completion candidate. */ |
| 3694 | char *copy_completion(char *check_line, int start) |
| 3695 | { |
| 3696 | char *word; |
| 3697 | int position = start, len_of_word = 0, index = 0; |
| 3698 | |
| 3699 | /* Find the length of the word by travelling to its end. */ |
| 3700 | while (is_word_mbchar(&check_line[position], FALSE)) { |
| 3701 | int next = move_mbright(check_line, position); |
| 3702 | len_of_word += next - position; |
| 3703 | position = next; |
| 3704 | } |
| 3705 | |
| 3706 | word = (char *)nmalloc((len_of_word + 1) * sizeof(char)); |
| 3707 | |
| 3708 | /* Simply copy the word. */ |
| 3709 | while (index < len_of_word) |
| 3710 | word[index++] = check_line[start++]; |
| 3711 | |
| 3712 | word[index] = '\0'; |
| 3713 | return word; |
| 3714 | } |
| 3715 | |
| 3716 | /* Look at the fragment the user has typed, then search the current buffer for |
| 3717 | * the first word that starts with this fragment, and tentatively complete the |
| 3718 | * fragment. If the user types 'Complete' again, search and paste the next |
| 3719 | * possible completion. */ |
| 3720 | void complete_a_word(void) |
| 3721 | { |
| 3722 | char *shard, *completion = NULL; |
| 3723 | int start_of_shard, shard_length = 0; |
| 3724 | int i = 0, j = 0; |
| 3725 | completion_word *some_word; |
Benno Schulenberg | 7373e4c | 2017-01-01 16:33:40 +0100 | [diff] [blame] | 3726 | #ifndef DISABLE_WRAPPING |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3727 | bool was_set_wrapping = !ISSET(NO_WRAP); |
Benno Schulenberg | 7373e4c | 2017-01-01 16:33:40 +0100 | [diff] [blame] | 3728 | #endif |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3729 | |
| 3730 | /* If this is a fresh completion attempt... */ |
| 3731 | if (pletion_line == NULL) { |
| 3732 | /* Clear the list of words of a previous completion run. */ |
| 3733 | while (list_of_completions != NULL) { |
| 3734 | completion_word *dropit = list_of_completions; |
| 3735 | list_of_completions = list_of_completions->next; |
| 3736 | free(dropit->word); |
| 3737 | free(dropit); |
| 3738 | } |
| 3739 | |
| 3740 | /* Prevent a completion from being merged with typed text. */ |
| 3741 | openfile->last_action = OTHER; |
| 3742 | |
| 3743 | /* Initialize the starting point for searching. */ |
| 3744 | pletion_line = openfile->fileage; |
| 3745 | pletion_x = 0; |
| 3746 | |
| 3747 | /* Wipe the "No further matches" message. */ |
| 3748 | blank_statusbar(); |
| 3749 | wnoutrefresh(bottomwin); |
| 3750 | } else { |
| 3751 | /* Remove the attempted completion from the buffer. */ |
| 3752 | do_undo(); |
| 3753 | } |
| 3754 | |
| 3755 | /* Find the start of the fragment that the user typed. */ |
| 3756 | start_of_shard = openfile->current_x; |
| 3757 | while (start_of_shard > 0) { |
| 3758 | int step_left = move_mbleft(openfile->current->data, start_of_shard); |
| 3759 | |
| 3760 | if (!is_word_mbchar(&openfile->current->data[step_left], FALSE)) |
| 3761 | break; |
| 3762 | start_of_shard = step_left; |
| 3763 | } |
| 3764 | |
| 3765 | /* If there is no word fragment before the cursor, do nothing. */ |
| 3766 | if (start_of_shard == openfile->current_x) { |
| 3767 | pletion_line = NULL; |
| 3768 | return; |
| 3769 | } |
| 3770 | |
| 3771 | shard = (char *)nmalloc((openfile->current_x - start_of_shard + 1) * sizeof(char)); |
| 3772 | |
| 3773 | /* Copy the fragment that has to be searched for. */ |
| 3774 | while (start_of_shard < openfile->current_x) |
| 3775 | shard[shard_length++] = openfile->current->data[start_of_shard++]; |
| 3776 | shard[shard_length] = '\0'; |
| 3777 | |
| 3778 | /* Run through all of the lines in the buffer, looking for shard. */ |
| 3779 | while (pletion_line != NULL) { |
| 3780 | int threshold = strlen(pletion_line->data) - shard_length - 1; |
| 3781 | /* The point where we can stop searching for shard. */ |
| 3782 | |
| 3783 | /* Traverse the whole line, looking for shard. */ |
| 3784 | for (i = pletion_x; i < threshold; i++) { |
| 3785 | /* If the first byte doesn't match, run on. */ |
| 3786 | if (pletion_line->data[i] != shard[0]) |
| 3787 | continue; |
| 3788 | |
| 3789 | /* Compare the rest of the bytes in shard. */ |
| 3790 | for (j = 1; j < shard_length; j++) |
| 3791 | if (pletion_line->data[i + j] != shard[j]) |
| 3792 | break; |
| 3793 | |
| 3794 | /* If not all of the bytes matched, continue searching. */ |
| 3795 | if (j < shard_length) |
| 3796 | continue; |
| 3797 | |
| 3798 | /* If the found match is not /longer/ than shard, skip it. */ |
| 3799 | if (!is_word_mbchar(&pletion_line->data[i + j], FALSE)) |
| 3800 | continue; |
| 3801 | |
| 3802 | /* If the match is not a separate word, skip it. */ |
| 3803 | if (i > 0 && is_word_mbchar(&pletion_line->data[ |
| 3804 | move_mbleft(pletion_line->data, i)], FALSE)) |
| 3805 | continue; |
| 3806 | |
| 3807 | /* If this match is the shard itself, ignore it. */ |
| 3808 | if (pletion_line == openfile->current && |
| 3809 | i == openfile->current_x - shard_length) |
| 3810 | continue; |
| 3811 | |
| 3812 | completion = copy_completion(pletion_line->data, i); |
| 3813 | |
| 3814 | /* Look among earlier attempted completions for a duplicate. */ |
| 3815 | some_word = list_of_completions; |
| 3816 | while (some_word && strcmp(some_word->word, completion) != 0) |
| 3817 | some_word = some_word->next; |
| 3818 | |
| 3819 | /* If we've already tried this word, skip it. */ |
| 3820 | if (some_word != NULL) { |
| 3821 | free(completion); |
| 3822 | continue; |
| 3823 | } |
| 3824 | |
| 3825 | /* Add the found word to the list of completions. */ |
| 3826 | some_word = (completion_word *)nmalloc(sizeof(completion_word)); |
| 3827 | some_word->word = completion; |
| 3828 | some_word->next = list_of_completions; |
| 3829 | list_of_completions = some_word; |
| 3830 | |
Benno Schulenberg | 7373e4c | 2017-01-01 16:33:40 +0100 | [diff] [blame] | 3831 | #ifndef DISABLE_WRAPPING |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3832 | /* Temporarily disable wrapping so only one undo item is added. */ |
| 3833 | SET(NO_WRAP); |
Benno Schulenberg | 7373e4c | 2017-01-01 16:33:40 +0100 | [diff] [blame] | 3834 | #endif |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3835 | /* Inject the completion into the buffer. */ |
| 3836 | do_output(&completion[shard_length], |
| 3837 | strlen(completion) - shard_length, FALSE); |
Benno Schulenberg | 7373e4c | 2017-01-01 16:33:40 +0100 | [diff] [blame] | 3838 | #ifndef DISABLE_WRAPPING |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3839 | /* If needed, reenable wrapping and wrap the current line. */ |
| 3840 | if (was_set_wrapping) { |
| 3841 | UNSET(NO_WRAP); |
| 3842 | do_wrap(openfile->current); |
| 3843 | } |
Benno Schulenberg | 7373e4c | 2017-01-01 16:33:40 +0100 | [diff] [blame] | 3844 | #endif |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3845 | /* Set the position for a possible next search attempt. */ |
| 3846 | pletion_x = ++i; |
| 3847 | |
| 3848 | free(shard); |
| 3849 | return; |
| 3850 | } |
| 3851 | |
| 3852 | pletion_line = pletion_line->next; |
| 3853 | pletion_x = 0; |
| 3854 | } |
| 3855 | |
| 3856 | /* The search has reached the end of the file. */ |
| 3857 | if (list_of_completions != NULL) { |
| 3858 | statusline(ALERT, _("No further matches")); |
| 3859 | refresh_needed = TRUE; |
| 3860 | } else |
Benno Schulenberg | 173bbe1 | 2016-12-07 20:56:28 +0100 | [diff] [blame] | 3861 | /* TRANSLATORS: Shown when there are zero possible completions. */ |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 3862 | statusline(ALERT, _("No matches")); |
| 3863 | |
| 3864 | free(shard); |
| 3865 | } |
Benno Schulenberg | 68a0314 | 2016-12-07 13:10:40 +0100 | [diff] [blame] | 3866 | #endif /* ENABLE_WORDCOMPLETION */ |