Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1 | /************************************************************************** |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 2 | * nano.c -- This file is part of GNU nano. * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3 | * * |
Chris Allegretta | 8a07a96 | 2009-12-02 03:36:22 +0000 | [diff] [blame] | 4 | * Copyright (C) 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, * |
Benno Schulenberg | 7a9f4a4 | 2014-04-30 20:18:26 +0000 | [diff] [blame] | 5 | * 2008, 2009, 2010, 2011, 2013, 2014 Free Software Foundation, Inc. * |
Benno Schulenberg | 406e524 | 2016-08-29 15:14:18 +0200 | [diff] [blame] | 6 | * Copyright (C) 2014, 2015, 2016 Benno Schulenberg * |
| 7 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 8 | * GNU nano is free software: you can redistribute it and/or modify * |
| 9 | * it under the terms of the GNU General Public License as published * |
| 10 | * by the Free Software Foundation, either version 3 of the License, * |
| 11 | * or (at your option) any later version. * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 12 | * * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 13 | * GNU nano is distributed in the hope that it will be useful, * |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty * |
| 15 | * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. * |
| 16 | * See the GNU General Public License for more details. * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 17 | * * |
| 18 | * You should have received a copy of the GNU General Public License * |
Benno Schulenberg | 514cd9a | 2016-08-29 17:10:49 +0200 | [diff] [blame] | 19 | * along with this program. If not, see http://www.gnu.org/licenses/. * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 20 | * * |
| 21 | **************************************************************************/ |
| 22 | |
David Lawrence Ramsey | 034b994 | 2005-12-08 02:47:10 +0000 | [diff] [blame] | 23 | #include "proto.h" |
Benno Schulenberg | 2ae490c | 2016-05-20 12:59:57 +0200 | [diff] [blame] | 24 | #include "revision.h" |
Chris Allegretta | 6efda54 | 2001-04-28 18:03:52 +0000 | [diff] [blame] | 25 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 26 | #include <stdio.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 27 | #include <stdarg.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 28 | #include <unistd.h> |
| 29 | #include <string.h> |
| 30 | #include <fcntl.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 31 | #include <errno.h> |
| 32 | #include <ctype.h> |
| 33 | #include <locale.h> |
Chris Allegretta | b857678 | 2008-07-12 02:32:19 +0000 | [diff] [blame] | 34 | #ifdef ENABLE_UTF8 |
David Lawrence Ramsey | a78b435 | 2007-05-25 14:39:40 +0000 | [diff] [blame] | 35 | #include <langinfo.h> |
Chris Allegretta | b857678 | 2008-07-12 02:32:19 +0000 | [diff] [blame] | 36 | #endif |
David Lawrence Ramsey | ce029f2 | 2005-12-28 05:01:00 +0000 | [diff] [blame] | 37 | #include <termios.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 38 | #ifdef HAVE_GETOPT_H |
| 39 | #include <getopt.h> |
| 40 | #endif |
David Lawrence Ramsey | 82697f2 | 2006-07-12 18:57:04 +0000 | [diff] [blame] | 41 | #ifndef NANO_TINY |
| 42 | #include <sys/ioctl.h> |
| 43 | #endif |
| 44 | |
David Lawrence Ramsey | 9bd537f | 2007-12-04 20:49:09 +0000 | [diff] [blame] | 45 | #ifndef DISABLE_MOUSE |
| 46 | static int oldinterval = -1; |
| 47 | /* Used to store the user's original mouse click interval. */ |
| 48 | #endif |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 49 | #ifndef DISABLE_NANORC |
David Lawrence Ramsey | 18dae62 | 2006-04-24 18:54:29 +0000 | [diff] [blame] | 50 | static bool no_rcfiles = FALSE; |
| 51 | /* Should we ignore all rcfiles? */ |
| 52 | #endif |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 53 | static struct termios oldterm; |
| 54 | /* The user's original terminal settings. */ |
| 55 | static struct sigaction act; |
David Lawrence Ramsey | 6335fb5 | 2007-01-01 05:15:32 +0000 | [diff] [blame] | 56 | /* Used to set up all our fun signal handlers. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 57 | |
David Lawrence Ramsey | a1b6997 | 2006-08-29 21:18:24 +0000 | [diff] [blame] | 58 | /* Create a new filestruct node. Note that we do not set prevnode->next |
| 59 | * to the new line. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 60 | filestruct *make_new_node(filestruct *prevnode) |
| 61 | { |
| 62 | filestruct *newnode = (filestruct *)nmalloc(sizeof(filestruct)); |
| 63 | |
| 64 | newnode->data = NULL; |
| 65 | newnode->prev = prevnode; |
| 66 | newnode->next = NULL; |
| 67 | newnode->lineno = (prevnode != NULL) ? prevnode->lineno + 1 : 1; |
| 68 | |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 69 | #ifndef DISABLE_COLOR |
Chris Allegretta | 364763f | 2009-02-03 05:05:58 +0000 | [diff] [blame] | 70 | newnode->multidata = NULL; |
Chris Allegretta | a117163 | 2009-01-19 19:10:39 +0000 | [diff] [blame] | 71 | #endif |
| 72 | |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 73 | return newnode; |
| 74 | } |
| 75 | |
| 76 | /* Make a copy of a filestruct node. */ |
| 77 | filestruct *copy_node(const filestruct *src) |
| 78 | { |
| 79 | filestruct *dst; |
| 80 | |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 81 | dst = (filestruct *)nmalloc(sizeof(filestruct)); |
| 82 | |
| 83 | dst->data = mallocstrcpy(NULL, src->data); |
| 84 | dst->next = src->next; |
| 85 | dst->prev = src->prev; |
| 86 | dst->lineno = src->lineno; |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 87 | #ifndef DISABLE_COLOR |
Chris Allegretta | 364763f | 2009-02-03 05:05:58 +0000 | [diff] [blame] | 88 | dst->multidata = NULL; |
Chris Allegretta | d47d8cd | 2009-01-25 07:25:17 +0000 | [diff] [blame] | 89 | #endif |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 90 | |
| 91 | return dst; |
| 92 | } |
| 93 | |
Benno Schulenberg | e0d0ca4 | 2015-11-24 13:28:32 +0000 | [diff] [blame] | 94 | /* Splice a new node into an existing linked list of filestructs. */ |
| 95 | void splice_node(filestruct *afterthis, filestruct *newnode) |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 96 | { |
Benno Schulenberg | e0d0ca4 | 2015-11-24 13:28:32 +0000 | [diff] [blame] | 97 | newnode->next = afterthis->next; |
| 98 | newnode->prev = afterthis; |
| 99 | if (afterthis->next != NULL) |
| 100 | afterthis->next->prev = newnode; |
| 101 | afterthis->next = newnode; |
Benno Schulenberg | 77c0357 | 2015-12-08 15:29:56 +0000 | [diff] [blame] | 102 | |
| 103 | /* Update filebot when inserting a node at the end of file. */ |
| 104 | if (openfile && openfile->filebot == afterthis) |
| 105 | openfile->filebot = newnode; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 106 | } |
| 107 | |
Benno Schulenberg | a64bfbe | 2015-12-08 16:02:05 +0000 | [diff] [blame] | 108 | /* Disconnect a node from a linked list of filestructs and delete it. */ |
Benno Schulenberg | 72caa54 | 2015-11-22 16:14:42 +0000 | [diff] [blame] | 109 | void unlink_node(filestruct *fileptr) |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 110 | { |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 111 | if (fileptr->prev != NULL) |
| 112 | fileptr->prev->next = fileptr->next; |
| 113 | if (fileptr->next != NULL) |
| 114 | fileptr->next->prev = fileptr->prev; |
Benno Schulenberg | 72caa54 | 2015-11-22 16:14:42 +0000 | [diff] [blame] | 115 | |
Benno Schulenberg | a64bfbe | 2015-12-08 16:02:05 +0000 | [diff] [blame] | 116 | /* Update filebot when removing a node at the end of file. */ |
| 117 | if (openfile && openfile->filebot == fileptr) |
| 118 | openfile->filebot = fileptr->prev; |
| 119 | |
Benno Schulenberg | 72caa54 | 2015-11-22 16:14:42 +0000 | [diff] [blame] | 120 | delete_node(fileptr); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 121 | } |
| 122 | |
Benno Schulenberg | a64bfbe | 2015-12-08 16:02:05 +0000 | [diff] [blame] | 123 | /* Free the data structures in the given node. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 124 | void delete_node(filestruct *fileptr) |
| 125 | { |
Benno Schulenberg | c32a58a | 2015-06-14 19:14:41 +0000 | [diff] [blame] | 126 | free(fileptr->data); |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 127 | #ifndef DISABLE_COLOR |
Benno Schulenberg | c32a58a | 2015-06-14 19:14:41 +0000 | [diff] [blame] | 128 | free(fileptr->multidata); |
Chris Allegretta | d47d8cd | 2009-01-25 07:25:17 +0000 | [diff] [blame] | 129 | #endif |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 130 | free(fileptr); |
| 131 | } |
| 132 | |
| 133 | /* Duplicate a whole filestruct. */ |
| 134 | filestruct *copy_filestruct(const filestruct *src) |
| 135 | { |
| 136 | filestruct *head, *copy; |
| 137 | |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 138 | copy = copy_node(src); |
| 139 | copy->prev = NULL; |
| 140 | head = copy; |
| 141 | src = src->next; |
| 142 | |
| 143 | while (src != NULL) { |
| 144 | copy->next = copy_node(src); |
| 145 | copy->next->prev = copy; |
| 146 | copy = copy->next; |
| 147 | |
| 148 | src = src->next; |
| 149 | } |
David Lawrence Ramsey | 826fbea | 2005-07-19 04:50:55 +0000 | [diff] [blame] | 150 | |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 151 | copy->next = NULL; |
| 152 | |
| 153 | return head; |
| 154 | } |
| 155 | |
Benno Schulenberg | 9703934 | 2016-02-18 19:58:18 +0000 | [diff] [blame] | 156 | /* Free a whole linked list of filestructs. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 157 | void free_filestruct(filestruct *src) |
| 158 | { |
Benno Schulenberg | 9703934 | 2016-02-18 19:58:18 +0000 | [diff] [blame] | 159 | if (src == NULL) |
| 160 | return; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 161 | |
| 162 | while (src->next != NULL) { |
| 163 | src = src->next; |
| 164 | delete_node(src->prev); |
| 165 | } |
David Lawrence Ramsey | 826fbea | 2005-07-19 04:50:55 +0000 | [diff] [blame] | 166 | |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 167 | delete_node(src); |
| 168 | } |
| 169 | |
David Lawrence Ramsey | c104ef1 | 2005-07-17 01:44:35 +0000 | [diff] [blame] | 170 | /* Renumber all entries in a filestruct, starting with fileptr. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 171 | void renumber(filestruct *fileptr) |
| 172 | { |
David Lawrence Ramsey | dbcaa3b | 2005-07-08 20:59:24 +0000 | [diff] [blame] | 173 | ssize_t line; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 174 | |
Benno Schulenberg | 4d9b5e9 | 2015-03-14 15:59:01 +0000 | [diff] [blame] | 175 | if (fileptr == NULL) |
| 176 | return; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 177 | |
David Lawrence Ramsey | 26ee759 | 2005-07-15 00:08:23 +0000 | [diff] [blame] | 178 | line = (fileptr->prev == NULL) ? 0 : fileptr->prev->lineno; |
David Lawrence Ramsey | dbcaa3b | 2005-07-08 20:59:24 +0000 | [diff] [blame] | 179 | |
| 180 | assert(fileptr != fileptr->next); |
| 181 | |
| 182 | for (; fileptr != NULL; fileptr = fileptr->next) |
David Lawrence Ramsey | 0532296 | 2005-07-14 23:51:52 +0000 | [diff] [blame] | 183 | fileptr->lineno = ++line; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 184 | } |
| 185 | |
David Lawrence Ramsey | e44cd2d | 2007-01-11 22:54:55 +0000 | [diff] [blame] | 186 | /* Partition a filestruct so that it begins at (top, top_x) and ends at |
| 187 | * (bot, bot_x). */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 188 | partition *partition_filestruct(filestruct *top, size_t top_x, |
| 189 | filestruct *bot, size_t bot_x) |
| 190 | { |
| 191 | partition *p; |
| 192 | |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 193 | assert(top != NULL && bot != NULL && openfile->fileage != NULL && openfile->filebot != NULL); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 194 | |
| 195 | /* Initialize the partition. */ |
| 196 | p = (partition *)nmalloc(sizeof(partition)); |
| 197 | |
| 198 | /* If the top and bottom of the partition are different from the top |
| 199 | * and bottom of the filestruct, save the latter and then set them |
| 200 | * to top and bot. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 201 | if (top != openfile->fileage) { |
| 202 | p->fileage = openfile->fileage; |
| 203 | openfile->fileage = top; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 204 | } else |
| 205 | p->fileage = NULL; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 206 | if (bot != openfile->filebot) { |
| 207 | p->filebot = openfile->filebot; |
| 208 | openfile->filebot = bot; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 209 | } else |
| 210 | p->filebot = NULL; |
| 211 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 212 | /* Remember which line is above the top of the partition, detach the |
| 213 | * top of the partition from it, and save the text before top_x. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 214 | p->top_prev = top->prev; |
| 215 | top->prev = NULL; |
| 216 | p->top_data = mallocstrncpy(NULL, top->data, top_x + 1); |
| 217 | p->top_data[top_x] = '\0'; |
| 218 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 219 | /* Remember which line is below the bottom of the partition, detach the |
| 220 | * bottom of the partition from it, and save the text after bot_x. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 221 | p->bot_next = bot->next; |
| 222 | bot->next = NULL; |
| 223 | p->bot_data = mallocstrcpy(NULL, bot->data + bot_x); |
| 224 | |
| 225 | /* Remove all text after bot_x at the bottom of the partition. */ |
| 226 | null_at(&bot->data, bot_x); |
| 227 | |
| 228 | /* Remove all text before top_x at the top of the partition. */ |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 229 | charmove(top->data, top->data + top_x, strlen(top->data) - top_x + 1); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 230 | |
| 231 | /* Return the partition. */ |
| 232 | return p; |
| 233 | } |
| 234 | |
David Lawrence Ramsey | e44cd2d | 2007-01-11 22:54:55 +0000 | [diff] [blame] | 235 | /* Unpartition a filestruct so that it begins at (fileage, 0) and ends |
| 236 | * at (filebot, strlen(filebot->data)) again. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 237 | void unpartition_filestruct(partition **p) |
| 238 | { |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 239 | assert(p != NULL && openfile->fileage != NULL && openfile->filebot != NULL); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 240 | |
| 241 | /* Reattach the line above the top of the partition, and restore the |
| 242 | * text before top_x from top_data. Free top_data when we're done |
| 243 | * with it. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 244 | openfile->fileage->prev = (*p)->top_prev; |
| 245 | if (openfile->fileage->prev != NULL) |
| 246 | openfile->fileage->prev->next = openfile->fileage; |
| 247 | openfile->fileage->data = charealloc(openfile->fileage->data, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 248 | strlen((*p)->top_data) + strlen(openfile->fileage->data) + 1); |
Benno Schulenberg | 80552ae | 2016-06-14 12:06:58 +0200 | [diff] [blame] | 249 | charmove(openfile->fileage->data + strlen((*p)->top_data), |
| 250 | openfile->fileage->data, strlen(openfile->fileage->data) + 1); |
| 251 | strncpy(openfile->fileage->data, (*p)->top_data, strlen((*p)->top_data)); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 252 | free((*p)->top_data); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 253 | |
| 254 | /* Reattach the line below the bottom of the partition, and restore |
| 255 | * the text after bot_x from bot_data. Free bot_data when we're |
| 256 | * done with it. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 257 | openfile->filebot->next = (*p)->bot_next; |
| 258 | if (openfile->filebot->next != NULL) |
| 259 | openfile->filebot->next->prev = openfile->filebot; |
| 260 | openfile->filebot->data = charealloc(openfile->filebot->data, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 261 | strlen(openfile->filebot->data) + strlen((*p)->bot_data) + 1); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 262 | strcat(openfile->filebot->data, (*p)->bot_data); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 263 | free((*p)->bot_data); |
| 264 | |
| 265 | /* Restore the top and bottom of the filestruct, if they were |
| 266 | * different from the top and bottom of the partition. */ |
| 267 | if ((*p)->fileage != NULL) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 268 | openfile->fileage = (*p)->fileage; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 269 | if ((*p)->filebot != NULL) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 270 | openfile->filebot = (*p)->filebot; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 271 | |
| 272 | /* Uninitialize the partition. */ |
| 273 | free(*p); |
| 274 | *p = NULL; |
| 275 | } |
| 276 | |
| 277 | /* Move all the text between (top, top_x) and (bot, bot_x) in the |
| 278 | * current filestruct to a filestruct beginning with file_top and ending |
| 279 | * with file_bot. If no text is between (top, top_x) and (bot, bot_x), |
| 280 | * don't do anything. */ |
David Lawrence Ramsey | 1cb945f | 2017-02-14 21:35:01 -0600 | [diff] [blame] | 281 | void extract_buffer(filestruct **file_top, filestruct **file_bot, |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 282 | filestruct *top, size_t top_x, filestruct *bot, size_t bot_x) |
| 283 | { |
| 284 | filestruct *top_save; |
David Lawrence Ramsey | e4e6173 | 2005-09-02 04:35:58 +0000 | [diff] [blame] | 285 | bool edittop_inside; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 286 | #ifndef NANO_TINY |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 287 | bool mark_inside = FALSE; |
Benno Schulenberg | 7249507 | 2014-02-22 16:46:27 +0000 | [diff] [blame] | 288 | bool same_line = FALSE; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 289 | #endif |
| 290 | |
| 291 | assert(file_top != NULL && file_bot != NULL && top != NULL && bot != NULL); |
| 292 | |
| 293 | /* If (top, top_x)-(bot, bot_x) doesn't cover any text, get out. */ |
| 294 | if (top == bot && top_x == bot_x) |
| 295 | return; |
| 296 | |
| 297 | /* Partition the filestruct so that it contains only the text from |
| 298 | * (top, top_x) to (bot, bot_x), keep track of whether the top of |
David Lawrence Ramsey | e4e6173 | 2005-09-02 04:35:58 +0000 | [diff] [blame] | 299 | * the edit window is inside the partition, and keep track of |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 300 | * whether the mark begins inside the partition. */ |
| 301 | filepart = partition_filestruct(top, top_x, bot, bot_x); |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 302 | edittop_inside = (openfile->edittop->lineno >= openfile->fileage->lineno && |
| 303 | openfile->edittop->lineno <= openfile->filebot->lineno); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 304 | #ifndef NANO_TINY |
Benno Schulenberg | 7249507 | 2014-02-22 16:46:27 +0000 | [diff] [blame] | 305 | if (openfile->mark_set) { |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 306 | mark_inside = (openfile->mark_begin->lineno >= |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 307 | openfile->fileage->lineno && |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 308 | openfile->mark_begin->lineno <= |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 309 | openfile->filebot->lineno && |
David Lawrence Ramsey | 5128de8 | 2005-07-12 17:40:16 +0000 | [diff] [blame] | 310 | (openfile->mark_begin != openfile->fileage || |
| 311 | openfile->mark_begin_x >= top_x) && |
| 312 | (openfile->mark_begin != openfile->filebot || |
| 313 | openfile->mark_begin_x <= bot_x)); |
Benno Schulenberg | 7249507 | 2014-02-22 16:46:27 +0000 | [diff] [blame] | 314 | same_line = (openfile->mark_begin == openfile->fileage); |
| 315 | } |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 316 | #endif |
| 317 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 318 | /* Subtract the number of characters in the text from the file size. */ |
David Lawrence Ramsey | c104ef1 | 2005-07-17 01:44:35 +0000 | [diff] [blame] | 319 | openfile->totsize -= get_totsize(top, bot); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 320 | |
| 321 | if (*file_top == NULL) { |
| 322 | /* If file_top is empty, just move all the text directly into |
| 323 | * it. This is equivalent to tacking the text in top onto the |
| 324 | * (lack of) text at the end of file_top. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 325 | *file_top = openfile->fileage; |
| 326 | *file_bot = openfile->filebot; |
David Lawrence Ramsey | c104ef1 | 2005-07-17 01:44:35 +0000 | [diff] [blame] | 327 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 328 | /* Renumber, starting with file_top. */ |
David Lawrence Ramsey | c104ef1 | 2005-07-17 01:44:35 +0000 | [diff] [blame] | 329 | renumber(*file_top); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 330 | } else { |
David Lawrence Ramsey | c104ef1 | 2005-07-17 01:44:35 +0000 | [diff] [blame] | 331 | filestruct *file_bot_save = *file_bot; |
| 332 | |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 333 | /* Otherwise, tack the text in top onto the text at the end of |
| 334 | * file_bot. */ |
| 335 | (*file_bot)->data = charealloc((*file_bot)->data, |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 336 | strlen((*file_bot)->data) + |
| 337 | strlen(openfile->fileage->data) + 1); |
| 338 | strcat((*file_bot)->data, openfile->fileage->data); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 339 | |
| 340 | /* Attach the line after top to the line after file_bot. Then, |
| 341 | * if there's more than one line after top, move file_bot down |
| 342 | * to bot. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 343 | (*file_bot)->next = openfile->fileage->next; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 344 | if ((*file_bot)->next != NULL) { |
| 345 | (*file_bot)->next->prev = *file_bot; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 346 | *file_bot = openfile->filebot; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 347 | } |
David Lawrence Ramsey | c104ef1 | 2005-07-17 01:44:35 +0000 | [diff] [blame] | 348 | |
Benno Schulenberg | 9703934 | 2016-02-18 19:58:18 +0000 | [diff] [blame] | 349 | delete_node(openfile->fileage); |
Benno Schulenberg | 2394e52 | 2014-07-16 08:46:42 +0000 | [diff] [blame] | 350 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 351 | /* Renumber, starting with the line after the original file_bot. */ |
Benno Schulenberg | 4d9b5e9 | 2015-03-14 15:59:01 +0000 | [diff] [blame] | 352 | renumber(file_bot_save->next); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 353 | } |
| 354 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 355 | /* Since the text has now been saved, remove it from the filestruct. */ |
David Lawrence Ramsey | 17f5c05 | 2017-02-09 16:00:07 -0600 | [diff] [blame] | 356 | openfile->fileage = make_new_node(NULL); |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 357 | openfile->fileage->data = mallocstrcpy(NULL, ""); |
| 358 | openfile->filebot = openfile->fileage; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 359 | |
David Lawrence Ramsey | 884ca53 | 2007-08-07 19:55:06 +0000 | [diff] [blame] | 360 | /* Restore the current line and cursor position. If the mark begins |
| 361 | * inside the partition, set the beginning of the mark to where the |
| 362 | * saved text used to start. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 363 | openfile->current = openfile->fileage; |
| 364 | openfile->current_x = top_x; |
David Lawrence Ramsey | 884ca53 | 2007-08-07 19:55:06 +0000 | [diff] [blame] | 365 | #ifndef NANO_TINY |
| 366 | if (mark_inside) { |
| 367 | openfile->mark_begin = openfile->current; |
| 368 | openfile->mark_begin_x = openfile->current_x; |
Benno Schulenberg | 7249507 | 2014-02-22 16:46:27 +0000 | [diff] [blame] | 369 | } else if (same_line) |
Benno Schulenberg | 6007d62 | 2015-11-22 16:08:28 +0000 | [diff] [blame] | 370 | /* Update the pointer to this partially cut line. */ |
Benno Schulenberg | 7249507 | 2014-02-22 16:46:27 +0000 | [diff] [blame] | 371 | openfile->mark_begin = openfile->current; |
David Lawrence Ramsey | 884ca53 | 2007-08-07 19:55:06 +0000 | [diff] [blame] | 372 | #endif |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 373 | |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 374 | top_save = openfile->fileage; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 375 | |
| 376 | /* Unpartition the filestruct so that it contains all the text |
| 377 | * again, minus the saved text. */ |
| 378 | unpartition_filestruct(&filepart); |
| 379 | |
David Lawrence Ramsey | e4e6173 | 2005-09-02 04:35:58 +0000 | [diff] [blame] | 380 | /* If the top of the edit window was inside the old partition, put |
| 381 | * it in range of current. */ |
Benno Schulenberg | b97c36c | 2016-04-25 20:05:21 +0200 | [diff] [blame] | 382 | if (edittop_inside) { |
Benno Schulenberg | 01bbf7e | 2016-10-20 21:11:11 +0200 | [diff] [blame] | 383 | adjust_viewport(STATIONARY); |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 384 | refresh_needed = TRUE; |
Benno Schulenberg | b97c36c | 2016-04-25 20:05:21 +0200 | [diff] [blame] | 385 | } |
David Lawrence Ramsey | e4e6173 | 2005-09-02 04:35:58 +0000 | [diff] [blame] | 386 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 387 | /* Renumber, starting with the beginning line of the old partition. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 388 | renumber(top_save); |
| 389 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 390 | /* If the text doesn't end with a magicline, and it should, add one. */ |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 391 | if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0') |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 392 | new_magicline(); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 393 | } |
| 394 | |
David Lawrence Ramsey | a847d37 | 2017-02-10 00:17:33 -0600 | [diff] [blame] | 395 | /* Meld the given buffer into the current file buffer |
Benno Schulenberg | c8fbc7d | 2014-04-14 09:14:39 +0000 | [diff] [blame] | 396 | * at the current cursor position. */ |
David Lawrence Ramsey | a847d37 | 2017-02-10 00:17:33 -0600 | [diff] [blame] | 397 | void ingraft_buffer(filestruct *somebuffer) |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 398 | { |
| 399 | filestruct *top_save; |
David Lawrence Ramsey | ee89418 | 2007-08-16 03:23:30 +0000 | [diff] [blame] | 400 | size_t current_x_save = openfile->current_x; |
David Lawrence Ramsey | e4e6173 | 2005-09-02 04:35:58 +0000 | [diff] [blame] | 401 | bool edittop_inside; |
David Lawrence Ramsey | d1ab89a | 2007-08-07 20:21:39 +0000 | [diff] [blame] | 402 | #ifndef NANO_TINY |
David Lawrence Ramsey | 64b8f42 | 2007-08-16 14:45:17 +0000 | [diff] [blame] | 403 | bool right_side_up = FALSE, single_line = FALSE; |
David Lawrence Ramsey | d1ab89a | 2007-08-07 20:21:39 +0000 | [diff] [blame] | 404 | #endif |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 405 | |
Benno Schulenberg | c8fbc7d | 2014-04-14 09:14:39 +0000 | [diff] [blame] | 406 | assert(somebuffer != NULL); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 407 | |
David Lawrence Ramsey | 64b8f42 | 2007-08-16 14:45:17 +0000 | [diff] [blame] | 408 | #ifndef NANO_TINY |
| 409 | /* Keep track of whether the mark begins inside the partition and |
| 410 | * will need adjustment. */ |
| 411 | if (openfile->mark_set) { |
| 412 | filestruct *top, *bot; |
| 413 | size_t top_x, bot_x; |
| 414 | |
| 415 | mark_order((const filestruct **)&top, &top_x, |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 416 | (const filestruct **)&bot, &bot_x, &right_side_up); |
David Lawrence Ramsey | 64b8f42 | 2007-08-16 14:45:17 +0000 | [diff] [blame] | 417 | |
| 418 | single_line = (top == bot); |
| 419 | } |
| 420 | #endif |
| 421 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 422 | /* Partition the filestruct so that it contains no text, and remember |
Benno Schulenberg | 17fb6df | 2016-06-16 11:54:11 +0200 | [diff] [blame] | 423 | * whether the current line is at the top of the edit window. */ |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 424 | filepart = partition_filestruct(openfile->current, openfile->current_x, |
| 425 | openfile->current, openfile->current_x); |
David Lawrence Ramsey | d5a9800 | 2007-08-08 00:05:49 +0000 | [diff] [blame] | 426 | edittop_inside = (openfile->edittop == openfile->fileage); |
Benno Schulenberg | 17fb6df | 2016-06-16 11:54:11 +0200 | [diff] [blame] | 427 | free_filestruct(openfile->fileage); |
David Lawrence Ramsey | d1ab89a | 2007-08-07 20:21:39 +0000 | [diff] [blame] | 428 | |
Benno Schulenberg | c8fbc7d | 2014-04-14 09:14:39 +0000 | [diff] [blame] | 429 | /* Put the top and bottom of the current filestruct at the top and |
David Lawrence Ramsey | a847d37 | 2017-02-10 00:17:33 -0600 | [diff] [blame] | 430 | * bottom of the passed buffer. */ |
| 431 | openfile->fileage = somebuffer; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 432 | openfile->filebot = openfile->fileage; |
| 433 | while (openfile->filebot->next != NULL) |
| 434 | openfile->filebot = openfile->filebot->next; |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 435 | |
Benno Schulenberg | df80bfd | 2015-10-31 20:17:09 +0000 | [diff] [blame] | 436 | /* Put the cursor at the end of the pasted text. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 437 | openfile->current = openfile->filebot; |
| 438 | openfile->current_x = strlen(openfile->filebot->data); |
Benno Schulenberg | df80bfd | 2015-10-31 20:17:09 +0000 | [diff] [blame] | 439 | |
| 440 | /* Refresh the mark's pointer, and compensate the mark's |
| 441 | * x coordinate for the change in the current line. */ |
David Lawrence Ramsey | d1ab89a | 2007-08-07 20:21:39 +0000 | [diff] [blame] | 442 | if (openfile->fileage == openfile->filebot) { |
| 443 | #ifndef NANO_TINY |
Benno Schulenberg | 4a1835f | 2015-10-31 19:03:53 +0000 | [diff] [blame] | 444 | if (openfile->mark_set && single_line) { |
David Lawrence Ramsey | d1ab89a | 2007-08-07 20:21:39 +0000 | [diff] [blame] | 445 | openfile->mark_begin = openfile->current; |
David Lawrence Ramsey | 64b8f42 | 2007-08-16 14:45:17 +0000 | [diff] [blame] | 446 | if (!right_side_up) |
David Lawrence Ramsey | d1ab89a | 2007-08-07 20:21:39 +0000 | [diff] [blame] | 447 | openfile->mark_begin_x += openfile->current_x; |
| 448 | } |
| 449 | #endif |
Benno Schulenberg | df80bfd | 2015-10-31 20:17:09 +0000 | [diff] [blame] | 450 | /* When the pasted stuff contains no newline, adjust the cursor's |
| 451 | * x coordinate for the text that is before the pasted stuff. */ |
David Lawrence Ramsey | ee89418 | 2007-08-16 03:23:30 +0000 | [diff] [blame] | 452 | openfile->current_x += current_x_save; |
David Lawrence Ramsey | d1ab89a | 2007-08-07 20:21:39 +0000 | [diff] [blame] | 453 | } |
David Lawrence Ramsey | ee89418 | 2007-08-16 03:23:30 +0000 | [diff] [blame] | 454 | #ifndef NANO_TINY |
Benno Schulenberg | df80bfd | 2015-10-31 20:17:09 +0000 | [diff] [blame] | 455 | else if (openfile->mark_set && single_line) { |
| 456 | if (right_side_up) |
| 457 | openfile->mark_begin = openfile->fileage; |
| 458 | else { |
| 459 | openfile->mark_begin = openfile->current; |
| 460 | openfile->mark_begin_x += openfile->current_x - current_x_save; |
David Lawrence Ramsey | 64b8f42 | 2007-08-16 14:45:17 +0000 | [diff] [blame] | 461 | } |
| 462 | } |
David Lawrence Ramsey | ee89418 | 2007-08-16 03:23:30 +0000 | [diff] [blame] | 463 | #endif |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 464 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 465 | /* Add the number of characters in the copied text to the file size. */ |
| 466 | openfile->totsize += get_totsize(openfile->fileage, openfile->filebot); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 467 | |
Benno Schulenberg | 17fb6df | 2016-06-16 11:54:11 +0200 | [diff] [blame] | 468 | /* If we pasted onto the first line of the edit window, the corresponding |
| 469 | * struct has been freed, so... point at the start of the copied text. */ |
David Lawrence Ramsey | e4e6173 | 2005-09-02 04:35:58 +0000 | [diff] [blame] | 470 | if (edittop_inside) |
| 471 | openfile->edittop = openfile->fileage; |
| 472 | |
Benno Schulenberg | 17fb6df | 2016-06-16 11:54:11 +0200 | [diff] [blame] | 473 | top_save = openfile->fileage; |
| 474 | |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 475 | /* Unpartition the filestruct so that it contains all the text |
David Lawrence Ramsey | e4e6173 | 2005-09-02 04:35:58 +0000 | [diff] [blame] | 476 | * again, plus the copied text. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 477 | unpartition_filestruct(&filepart); |
| 478 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 479 | /* Renumber, starting with the beginning line of the old partition. */ |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 480 | renumber(top_save); |
| 481 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 482 | /* If the text doesn't end with a magicline, and it should, add one. */ |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 483 | if (!ISSET(NO_NEWLINES) && openfile->filebot->data[0] != '\0') |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 484 | new_magicline(); |
David Lawrence Ramsey | 9057329 | 2005-07-08 02:47:05 +0000 | [diff] [blame] | 485 | } |
| 486 | |
David Lawrence Ramsey | a847d37 | 2017-02-10 00:17:33 -0600 | [diff] [blame] | 487 | /* Meld a copy of the given buffer into the current file buffer. */ |
| 488 | void copy_from_buffer(filestruct *somebuffer) |
| 489 | { |
| 490 | filestruct *the_copy = copy_filestruct(somebuffer); |
| 491 | |
| 492 | ingraft_buffer(the_copy); |
| 493 | } |
| 494 | |
David Lawrence Ramsey | e99494f | 2005-07-20 21:08:38 +0000 | [diff] [blame] | 495 | /* Create a new openfilestruct node. */ |
| 496 | openfilestruct *make_new_opennode(void) |
| 497 | { |
Benno Schulenberg | 8925eab | 2015-08-16 15:21:32 +0000 | [diff] [blame] | 498 | return (openfilestruct *)nmalloc(sizeof(openfilestruct)); |
David Lawrence Ramsey | e99494f | 2005-07-20 21:08:38 +0000 | [diff] [blame] | 499 | } |
| 500 | |
David Lawrence Ramsey | e99494f | 2005-07-20 21:08:38 +0000 | [diff] [blame] | 501 | /* Unlink a node from the rest of the openfilestruct, and delete it. */ |
| 502 | void unlink_opennode(openfilestruct *fileptr) |
| 503 | { |
Benno Schulenberg | 0562d27 | 2016-12-15 20:26:47 +0100 | [diff] [blame] | 504 | assert(fileptr != fileptr->prev && fileptr != fileptr->next); |
David Lawrence Ramsey | e99494f | 2005-07-20 21:08:38 +0000 | [diff] [blame] | 505 | |
| 506 | fileptr->prev->next = fileptr->next; |
| 507 | fileptr->next->prev = fileptr->prev; |
| 508 | |
| 509 | delete_opennode(fileptr); |
| 510 | } |
| 511 | |
Benno Schulenberg | f4f9954 | 2016-02-22 09:02:58 +0000 | [diff] [blame] | 512 | /* Free all the memory in the given open-file node. */ |
David Lawrence Ramsey | e99494f | 2005-07-20 21:08:38 +0000 | [diff] [blame] | 513 | void delete_opennode(openfilestruct *fileptr) |
| 514 | { |
David Lawrence Ramsey | e99494f | 2005-07-20 21:08:38 +0000 | [diff] [blame] | 515 | free(fileptr->filename); |
| 516 | free_filestruct(fileptr->fileage); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 517 | #ifndef NANO_TINY |
Benno Schulenberg | c32a58a | 2015-06-14 19:14:41 +0000 | [diff] [blame] | 518 | free(fileptr->current_stat); |
Benno Schulenberg | 9fcde23 | 2015-07-10 15:54:06 +0000 | [diff] [blame] | 519 | free(fileptr->lock_filename); |
Benno Schulenberg | f845938 | 2016-01-15 16:44:50 +0000 | [diff] [blame] | 520 | /* Free the undo stack. */ |
| 521 | discard_until(NULL, fileptr); |
David Lawrence Ramsey | e99494f | 2005-07-20 21:08:38 +0000 | [diff] [blame] | 522 | #endif |
David Lawrence Ramsey | e99494f | 2005-07-20 21:08:38 +0000 | [diff] [blame] | 523 | free(fileptr); |
| 524 | } |
| 525 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 526 | /* Display a warning about a key disabled in view mode. */ |
David Lawrence Ramsey | 5040666 | 2005-01-19 19:52:42 +0000 | [diff] [blame] | 527 | void print_view_warning(void) |
| 528 | { |
Benno Schulenberg | 6847616 | 2015-07-30 18:10:16 +0000 | [diff] [blame] | 529 | statusbar(_("Key is invalid in view mode")); |
David Lawrence Ramsey | 5040666 | 2005-01-19 19:52:42 +0000 | [diff] [blame] | 530 | } |
| 531 | |
Benno Schulenberg | 6847616 | 2015-07-30 18:10:16 +0000 | [diff] [blame] | 532 | /* Indicate that something is disabled in restricted mode. */ |
| 533 | void show_restricted_warning(void) |
| 534 | { |
| 535 | statusbar(_("This function is disabled in restricted mode")); |
| 536 | beep(); |
| 537 | } |
| 538 | |
| 539 | #ifdef DISABLE_HELP |
| 540 | /* Indicate that help texts are unavailable. */ |
| 541 | void say_there_is_no_help(void) |
| 542 | { |
Benno Schulenberg | f5234e5 | 2015-08-09 18:12:32 +0000 | [diff] [blame] | 543 | statusbar(_("Help is not available")); |
Benno Schulenberg | 6847616 | 2015-07-30 18:10:16 +0000 | [diff] [blame] | 544 | } |
| 545 | #endif |
| 546 | |
David Lawrence Ramsey | cb04b56 | 2007-01-09 22:56:56 +0000 | [diff] [blame] | 547 | /* Make nano exit gracefully. */ |
David Lawrence Ramsey | da14106 | 2004-05-25 19:41:11 +0000 | [diff] [blame] | 548 | void finish(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 549 | { |
Benno Schulenberg | ea2b51a | 2015-08-09 18:10:13 +0000 | [diff] [blame] | 550 | /* Blank the statusbar and (if applicable) the shortcut list, |
| 551 | * and move the cursor to the last line of the screen. */ |
| 552 | blank_statusbar(); |
| 553 | blank_bottombars(); |
David Lawrence Ramsey | 3f4520b | 2005-07-29 03:28:09 +0000 | [diff] [blame] | 554 | wrefresh(bottomwin); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 555 | endwin(); |
| 556 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 557 | /* Restore the old terminal settings. */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 558 | tcsetattr(0, TCSANOW, &oldterm); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 559 | |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 560 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | fcb4c3f | 2014-06-19 15:09:23 +0000 | [diff] [blame] | 561 | if (ISSET(HISTORYLOG)) |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 562 | save_history(); |
Benno Schulenberg | fcb4c3f | 2014-06-19 15:09:23 +0000 | [diff] [blame] | 563 | if (ISSET(POS_HISTORY)) { |
Benno Schulenberg | 1de337d | 2014-06-04 16:02:51 +0000 | [diff] [blame] | 564 | update_poshistory(openfile->filename, openfile->current->lineno, xplustabs() + 1); |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 565 | save_poshistory(); |
Chris Allegretta | 8e2a930 | 2011-02-18 07:30:57 +0000 | [diff] [blame] | 566 | } |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 567 | #endif |
| 568 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 569 | #ifdef DEBUG |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 570 | thanks_for_all_the_fish(); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 571 | #endif |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 572 | |
David Lawrence Ramsey | cb04b56 | 2007-01-09 22:56:56 +0000 | [diff] [blame] | 573 | /* Get out. */ |
David Lawrence Ramsey | da14106 | 2004-05-25 19:41:11 +0000 | [diff] [blame] | 574 | exit(0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 575 | } |
| 576 | |
David Lawrence Ramsey | cb04b56 | 2007-01-09 22:56:56 +0000 | [diff] [blame] | 577 | /* Make nano die gracefully. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 578 | void die(const char *msg, ...) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 579 | { |
| 580 | va_list ap; |
| 581 | |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 582 | endwin(); |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 583 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 584 | /* Restore the old terminal settings. */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 585 | tcsetattr(0, TCSANOW, &oldterm); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 586 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 587 | va_start(ap, msg); |
| 588 | vfprintf(stderr, msg, ap); |
| 589 | va_end(ap); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 590 | |
Benno Schulenberg | a7ae176 | 2016-08-02 17:11:50 +0200 | [diff] [blame] | 591 | #ifndef NANO_TINY |
| 592 | /* If the current buffer has a lockfile, remove it. */ |
Benno Schulenberg | 68010d9 | 2016-08-16 16:56:25 +0200 | [diff] [blame] | 593 | if (openfile && ISSET(LOCKING) && openfile->lock_filename) |
Benno Schulenberg | a7ae176 | 2016-08-02 17:11:50 +0200 | [diff] [blame] | 594 | delete_lockfile(openfile->lock_filename); |
| 595 | #endif |
| 596 | |
Benno Schulenberg | f6dd0ad | 2016-08-02 17:26:25 +0200 | [diff] [blame] | 597 | /* If the current file buffer was modified, save it. */ |
Chris Allegretta | e67bf80 | 2008-05-31 22:23:16 +0000 | [diff] [blame] | 598 | if (openfile && openfile->modified) { |
Benno Schulenberg | f6dd0ad | 2016-08-02 17:26:25 +0200 | [diff] [blame] | 599 | /* If the filestruct is partitioned, unpartition it first. */ |
David Lawrence Ramsey | f4595bb | 2005-06-06 16:27:18 +0000 | [diff] [blame] | 600 | if (filepart != NULL) |
| 601 | unpartition_filestruct(&filepart); |
| 602 | |
Benno Schulenberg | f6dd0ad | 2016-08-02 17:26:25 +0200 | [diff] [blame] | 603 | die_save_file(openfile->filename, openfile->current_stat); |
David Lawrence Ramsey | f4595bb | 2005-06-06 16:27:18 +0000 | [diff] [blame] | 604 | } |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 605 | |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 606 | #ifndef DISABLE_MULTIBUFFER |
David Lawrence Ramsey | 3ece0b9 | 2004-12-01 15:11:27 +0000 | [diff] [blame] | 607 | /* Save all of the other modified file buffers, if any. */ |
David Lawrence Ramsey | 64661ac | 2005-07-08 19:57:25 +0000 | [diff] [blame] | 608 | if (openfile != NULL) { |
Benno Schulenberg | f6dd0ad | 2016-08-02 17:26:25 +0200 | [diff] [blame] | 609 | openfilestruct *firstone = openfile; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 610 | |
Benno Schulenberg | f6dd0ad | 2016-08-02 17:26:25 +0200 | [diff] [blame] | 611 | while (openfile->next != firstone) { |
David Lawrence Ramsey | 64661ac | 2005-07-08 19:57:25 +0000 | [diff] [blame] | 612 | openfile = openfile->next; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 613 | |
Benno Schulenberg | a7ae176 | 2016-08-02 17:11:50 +0200 | [diff] [blame] | 614 | #ifndef NANO_TINY |
| 615 | if (ISSET(LOCKING) && openfile->lock_filename) |
| 616 | delete_lockfile(openfile->lock_filename); |
| 617 | #endif |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 618 | if (openfile->modified) |
Benno Schulenberg | f6dd0ad | 2016-08-02 17:26:25 +0200 | [diff] [blame] | 619 | die_save_file(openfile->filename, openfile->current_stat); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 620 | } |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 621 | } |
| 622 | #endif |
| 623 | |
Benno Schulenberg | f6dd0ad | 2016-08-02 17:26:25 +0200 | [diff] [blame] | 624 | /* Abandon the building. */ |
David Lawrence Ramsey | 3ece0b9 | 2004-12-01 15:11:27 +0000 | [diff] [blame] | 625 | exit(1); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Benno Schulenberg | 3129ff6 | 2016-05-27 21:43:39 +0200 | [diff] [blame] | 628 | /* Save the current file under the name specified in die_filename, which |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 629 | * is modified to be unique if necessary. */ |
Benno Schulenberg | f6dd0ad | 2016-08-02 17:26:25 +0200 | [diff] [blame] | 630 | void die_save_file(const char *die_filename, struct stat *die_stat) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 631 | { |
Benno Schulenberg | 3129ff6 | 2016-05-27 21:43:39 +0200 | [diff] [blame] | 632 | char *targetname; |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 633 | bool failed = TRUE; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 634 | |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 635 | /* If we're using restricted mode, don't write any emergency backup |
| 636 | * files, since that would allow reading from or writing to files |
| 637 | * not specified on the command line. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 638 | if (ISSET(RESTRICTED)) |
| 639 | return; |
| 640 | |
David Lawrence Ramsey | cb04b56 | 2007-01-09 22:56:56 +0000 | [diff] [blame] | 641 | /* If we can't save, we have really bad problems, but we might as |
| 642 | * well try. */ |
David Lawrence Ramsey | 1904f51 | 2007-07-01 21:33:17 +0000 | [diff] [blame] | 643 | if (*die_filename == '\0') |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 644 | die_filename = "nano"; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 645 | |
Benno Schulenberg | 3129ff6 | 2016-05-27 21:43:39 +0200 | [diff] [blame] | 646 | targetname = get_next_filename(die_filename, ".save"); |
| 647 | |
| 648 | if (*targetname != '\0') |
| 649 | failed = !write_file(targetname, NULL, TRUE, OVERWRITE, TRUE); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 650 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 651 | if (!failed) |
Benno Schulenberg | 3129ff6 | 2016-05-27 21:43:39 +0200 | [diff] [blame] | 652 | fprintf(stderr, _("\nBuffer written to %s\n"), targetname); |
| 653 | else if (*targetname != '\0') |
| 654 | fprintf(stderr, _("\nBuffer not written to %s: %s\n"), targetname, |
David Lawrence Ramsey | dac3bca | 2005-06-01 04:23:52 +0000 | [diff] [blame] | 655 | strerror(errno)); |
| 656 | else |
| 657 | fprintf(stderr, _("\nBuffer not written: %s\n"), |
David Lawrence Ramsey | 54b74ce | 2005-05-29 23:03:28 +0000 | [diff] [blame] | 658 | _("Too many backup files?")); |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 659 | |
Chris Allegretta | 8b6f6fc | 2009-11-22 21:35:56 +0000 | [diff] [blame] | 660 | #ifndef NANO_TINY |
Benno Schulenberg | 0b6d6f4 | 2015-03-27 13:46:50 +0000 | [diff] [blame] | 661 | /* Try and chmod/chown the save file to the values of the original file, |
| 662 | * but don't worry if it fails because we're supposed to be bailing as |
| 663 | * fast as possible. */ |
Chris Allegretta | 123110c | 2009-11-20 05:09:12 +0000 | [diff] [blame] | 664 | if (die_stat) { |
Benno Schulenberg | e3e3f65 | 2016-05-27 21:48:38 +0200 | [diff] [blame] | 665 | IGNORE_CALL_RESULT(chmod(targetname, die_stat->st_mode)); |
| 666 | IGNORE_CALL_RESULT(chown(targetname, die_stat->st_uid, |
| 667 | die_stat->st_gid)); |
Chris Allegretta | 123110c | 2009-11-20 05:09:12 +0000 | [diff] [blame] | 668 | } |
Chris Allegretta | 8b6f6fc | 2009-11-22 21:35:56 +0000 | [diff] [blame] | 669 | #endif |
Chris Allegretta | 123110c | 2009-11-20 05:09:12 +0000 | [diff] [blame] | 670 | |
Benno Schulenberg | 3129ff6 | 2016-05-27 21:43:39 +0200 | [diff] [blame] | 671 | free(targetname); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 672 | } |
| 673 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 674 | /* Initialize the three window portions nano uses. */ |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 675 | void window_init(void) |
Chris Allegretta | e61e830 | 2001-01-14 05:18:27 +0000 | [diff] [blame] | 676 | { |
Benno Schulenberg | 97eb0e5 | 2016-08-15 14:55:59 +0200 | [diff] [blame] | 677 | /* First delete existing windows, in case of resizing. */ |
| 678 | delwin(topwin); |
Benno Schulenberg | 76a960d | 2016-08-15 19:44:49 +0200 | [diff] [blame] | 679 | topwin = NULL; |
Benno Schulenberg | 97eb0e5 | 2016-08-15 14:55:59 +0200 | [diff] [blame] | 680 | delwin(edit); |
| 681 | delwin(bottomwin); |
| 682 | |
Benno Schulenberg | 76a960d | 2016-08-15 19:44:49 +0200 | [diff] [blame] | 683 | /* If the terminal is very flat, don't set up a titlebar. */ |
| 684 | if (LINES < 3) { |
| 685 | editwinrows = 1; |
| 686 | /* Set up two subwindows. If the terminal is just one line, |
| 687 | * edit window and statusbar window will cover each other. */ |
| 688 | edit = newwin(1, COLS, 0, 0); |
| 689 | bottomwin = newwin(1, COLS, LINES - 1, 0); |
| 690 | } else { |
| 691 | int toprows = (ISSET(MORE_SPACE) ? 1 : (LINES < 6) ? 1 : 2); |
| 692 | int bottomrows = (ISSET(NO_HELP) ? 1 : (LINES < 5) ? 1 : 3); |
Benno Schulenberg | 0dd2a55 | 2016-08-15 12:55:03 +0200 | [diff] [blame] | 693 | |
Benno Schulenberg | 76a960d | 2016-08-15 19:44:49 +0200 | [diff] [blame] | 694 | editwinrows = LINES - toprows - bottomrows; |
David Lawrence Ramsey | b386a90 | 2005-07-10 02:37:38 +0000 | [diff] [blame] | 695 | |
Benno Schulenberg | 76a960d | 2016-08-15 19:44:49 +0200 | [diff] [blame] | 696 | /* Set up the normal three subwindows. */ |
| 697 | topwin = newwin(toprows, COLS, 0, 0); |
| 698 | edit = newwin(editwinrows, COLS, toprows, 0); |
| 699 | bottomwin = newwin(bottomrows, COLS, toprows + editwinrows, 0); |
| 700 | } |
| 701 | |
| 702 | /* In case the terminal shrunk, make sure the status line is clear. */ |
| 703 | blank_statusbar(); |
| 704 | wnoutrefresh(bottomwin); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 705 | |
David Lawrence Ramsey | 31de105 | 2005-08-14 19:25:16 +0000 | [diff] [blame] | 706 | /* Turn the keypad on for the windows, if necessary. */ |
David Lawrence Ramsey | 057edf7 | 2005-08-10 21:22:15 +0000 | [diff] [blame] | 707 | if (!ISSET(REBIND_KEYPAD)) { |
David Lawrence Ramsey | 31de105 | 2005-08-14 19:25:16 +0000 | [diff] [blame] | 708 | keypad(topwin, TRUE); |
David Lawrence Ramsey | 057edf7 | 2005-08-10 21:22:15 +0000 | [diff] [blame] | 709 | keypad(edit, TRUE); |
| 710 | keypad(bottomwin, TRUE); |
| 711 | } |
Benno Schulenberg | 97eb0e5 | 2016-08-15 14:55:59 +0200 | [diff] [blame] | 712 | |
| 713 | #ifndef DISABLE_WRAPJUSTIFY |
| 714 | /* Set up the wrapping point, accounting for screen width when negative. */ |
| 715 | fill = wrap_at; |
| 716 | if (fill <= 0) |
| 717 | fill += COLS; |
| 718 | if (fill < 0) |
| 719 | fill = 0; |
| 720 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 721 | } |
| 722 | |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 723 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 503bad0 | 2006-06-09 18:24:37 +0000 | [diff] [blame] | 724 | /* Disable mouse support. */ |
| 725 | void disable_mouse_support(void) |
| 726 | { |
| 727 | mousemask(0, NULL); |
David Lawrence Ramsey | 9bd537f | 2007-12-04 20:49:09 +0000 | [diff] [blame] | 728 | mouseinterval(oldinterval); |
David Lawrence Ramsey | 503bad0 | 2006-06-09 18:24:37 +0000 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | /* Enable mouse support. */ |
| 732 | void enable_mouse_support(void) |
| 733 | { |
David Lawrence Ramsey | 98e4d4f | 2006-06-28 21:54:55 +0000 | [diff] [blame] | 734 | mousemask(ALL_MOUSE_EVENTS, NULL); |
David Lawrence Ramsey | 9bd537f | 2007-12-04 20:49:09 +0000 | [diff] [blame] | 735 | oldinterval = mouseinterval(50); |
David Lawrence Ramsey | 503bad0 | 2006-06-09 18:24:37 +0000 | [diff] [blame] | 736 | } |
| 737 | |
| 738 | /* Initialize mouse support. Enable it if the USE_MOUSE flag is set, |
| 739 | * and disable it otherwise. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 740 | void mouse_init(void) |
| 741 | { |
David Lawrence Ramsey | 503bad0 | 2006-06-09 18:24:37 +0000 | [diff] [blame] | 742 | if (ISSET(USE_MOUSE)) |
| 743 | enable_mouse_support(); |
| 744 | else |
| 745 | disable_mouse_support(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 746 | } |
David Lawrence Ramsey | 503bad0 | 2006-06-09 18:24:37 +0000 | [diff] [blame] | 747 | #endif /* !DISABLE_MOUSE */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 748 | |
David Lawrence Ramsey | bc6a36e | 2005-03-22 20:17:38 +0000 | [diff] [blame] | 749 | #ifdef HAVE_GETOPT_LONG |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 750 | #define print_opt(shortflag, longflag, desc) print_opt_full(shortflag, longflag, desc) |
David Lawrence Ramsey | bc6a36e | 2005-03-22 20:17:38 +0000 | [diff] [blame] | 751 | #else |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 752 | #define print_opt(shortflag, longflag, desc) print_opt_full(shortflag, desc) |
David Lawrence Ramsey | bc6a36e | 2005-03-22 20:17:38 +0000 | [diff] [blame] | 753 | #endif |
| 754 | |
David Lawrence Ramsey | 22fac78 | 2004-08-05 15:16:19 +0000 | [diff] [blame] | 755 | /* Print one usage string to the screen. This cuts down on duplicate |
David Lawrence Ramsey | 39e8ce6 | 2005-03-21 07:24:47 +0000 | [diff] [blame] | 756 | * strings to translate, and leaves out the parts that shouldn't be |
David Lawrence Ramsey | cb04b56 | 2007-01-09 22:56:56 +0000 | [diff] [blame] | 757 | * translatable (i.e. the flag names). */ |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 758 | void print_opt_full(const char *shortflag |
David Lawrence Ramsey | 39e8ce6 | 2005-03-21 07:24:47 +0000 | [diff] [blame] | 759 | #ifdef HAVE_GETOPT_LONG |
| 760 | , const char *longflag |
| 761 | #endif |
| 762 | , const char *desc) |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 763 | { |
| 764 | printf(" %s\t", shortflag); |
David Lawrence Ramsey | 3fb62b8 | 2007-12-18 22:00:17 +0000 | [diff] [blame] | 765 | if (strlenpt(shortflag) < 8) |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 766 | printf("\t"); |
| 767 | |
| 768 | #ifdef HAVE_GETOPT_LONG |
| 769 | printf("%s\t", longflag); |
David Lawrence Ramsey | 3fb62b8 | 2007-12-18 22:00:17 +0000 | [diff] [blame] | 770 | if (strlenpt(longflag) < 8) |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 771 | printf("\t\t"); |
David Lawrence Ramsey | 3fb62b8 | 2007-12-18 22:00:17 +0000 | [diff] [blame] | 772 | else if (strlenpt(longflag) < 16) |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 773 | printf("\t"); |
| 774 | #endif |
| 775 | |
David Lawrence Ramsey | c2b0747 | 2005-04-14 03:13:49 +0000 | [diff] [blame] | 776 | if (desc != NULL) |
| 777 | printf("%s", _(desc)); |
| 778 | printf("\n"); |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Benno Schulenberg | 62de330 | 2014-05-28 15:44:11 +0000 | [diff] [blame] | 781 | /* Explain how to properly use nano and its command-line options. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 782 | void usage(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 783 | { |
David Lawrence Ramsey | 74d8ebc | 2006-06-26 04:39:31 +0000 | [diff] [blame] | 784 | printf(_("Usage: nano [OPTIONS] [[+LINE,COLUMN] FILE]...\n\n")); |
David Lawrence Ramsey | 560e83f | 2006-05-08 19:55:18 +0000 | [diff] [blame] | 785 | #ifdef HAVE_GETOPT_LONG |
Benno Schulenberg | ffeb0f9 | 2016-10-23 22:00:45 +0200 | [diff] [blame] | 786 | printf(_("Option\t\tGNU long option\t\tMeaning\n")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 787 | #else |
Benno Schulenberg | ffeb0f9 | 2016-10-23 22:00:45 +0200 | [diff] [blame] | 788 | printf(_("Option\t\tMeaning\n")); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 789 | #endif |
David Lawrence Ramsey | 74d8ebc | 2006-06-26 04:39:31 +0000 | [diff] [blame] | 790 | print_opt(_("+LINE,COLUMN"), "", |
Benno Schulenberg | 62de330 | 2014-05-28 15:44:11 +0000 | [diff] [blame] | 791 | /* TRANSLATORS: The next forty or so strings are option descriptions |
| 792 | * for the --help output. Try to keep them at most 40 characters. */ |
David Lawrence Ramsey | 86ce390 | 2005-05-21 17:15:46 +0000 | [diff] [blame] | 793 | N_("Start at line LINE, column COLUMN")); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 794 | #ifndef NANO_TINY |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 795 | print_opt("-A", "--smarthome", N_("Enable smart home key")); |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 796 | if (!ISSET(RESTRICTED)) { |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 797 | print_opt("-B", "--backup", N_("Save backups of existing files")); |
| 798 | print_opt(_("-C <dir>"), _("--backupdir=<dir>"), |
| 799 | N_("Directory for saving unique backup files")); |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 800 | } |
David Lawrence Ramsey | 9dbd0d2 | 2006-05-13 13:02:14 +0000 | [diff] [blame] | 801 | #endif |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 802 | print_opt("-D", "--boldtext", N_("Use bold instead of reverse video text")); |
David Lawrence Ramsey | 9dbd0d2 | 2006-05-13 13:02:14 +0000 | [diff] [blame] | 803 | #ifndef NANO_TINY |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 804 | print_opt("-E", "--tabstospaces", N_("Convert typed tabs to spaces")); |
Chris Allegretta | 7004c28 | 2001-09-22 00:42:10 +0000 | [diff] [blame] | 805 | #endif |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 806 | #ifndef DISABLE_MULTIBUFFER |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 807 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 808 | print_opt("-F", "--multibuffer", |
| 809 | N_("Read a file into a new buffer by default")); |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 810 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 811 | #ifndef NANO_TINY |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 812 | print_opt("-G", "--locking", N_("Use (vim-style) lock files")); |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 813 | #endif |
| 814 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 815 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 816 | print_opt("-H", "--historylog", |
| 817 | N_("Log & read search/replace string history")); |
Benno Schulenberg | 85ddc71 | 2014-07-02 19:57:23 +0000 | [diff] [blame] | 818 | #endif |
| 819 | #ifndef DISABLE_NANORC |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 820 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 821 | print_opt("-I", "--ignorercfiles", N_("Don't look at nanorc files")); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 822 | #endif |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 823 | print_opt("-K", "--rebindkeypad", |
David Lawrence Ramsey | 057edf7 | 2005-08-10 21:22:15 +0000 | [diff] [blame] | 824 | N_("Fix numeric keypad key confusion problem")); |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 825 | print_opt("-L", "--nonewlines", |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 826 | N_("Don't add newlines to the ends of files")); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 827 | #ifndef NANO_TINY |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 828 | print_opt("-N", "--noconvert", |
David Lawrence Ramsey | 86ce390 | 2005-05-21 17:15:46 +0000 | [diff] [blame] | 829 | N_("Don't convert files from DOS/Mac format")); |
Chris Allegretta | 8fa1e28 | 2001-09-22 04:20:25 +0000 | [diff] [blame] | 830 | #endif |
David Lawrence Ramsey | 7b0531a | 2006-07-31 01:30:31 +0000 | [diff] [blame] | 831 | print_opt("-O", "--morespace", N_("Use one more line for editing")); |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 832 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 833 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 834 | print_opt("-P", "--positionlog", |
| 835 | N_("Log & read location of cursor position")); |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 836 | #endif |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 837 | #ifndef DISABLE_JUSTIFY |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 838 | print_opt(_("-Q <str>"), _("--quotestr=<str>"), N_("Quoting string")); |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 839 | #endif |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 840 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 841 | print_opt("-R", "--restricted", N_("Restricted mode")); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 842 | #ifndef NANO_TINY |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 843 | print_opt("-S", "--smooth", N_("Scroll by line instead of half-screen")); |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 844 | #endif |
David Lawrence Ramsey | e77ebfa | 2006-05-14 15:19:38 +0000 | [diff] [blame] | 845 | print_opt(_("-T <#cols>"), _("--tabsize=<#cols>"), |
David Lawrence Ramsey | 7b0531a | 2006-07-31 01:30:31 +0000 | [diff] [blame] | 846 | N_("Set width of a tab to #cols columns")); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 847 | #ifndef NANO_TINY |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 848 | print_opt("-U", "--quickblank", N_("Do quick statusbar blanking")); |
David Lawrence Ramsey | 66357b5 | 2005-06-17 19:09:18 +0000 | [diff] [blame] | 849 | #endif |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 850 | print_opt("-V", "--version", N_("Print version information and exit")); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 851 | #ifndef NANO_TINY |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 852 | print_opt("-W", "--wordbounds", |
David Lawrence Ramsey | 4f03daf | 2005-08-10 22:12:28 +0000 | [diff] [blame] | 853 | N_("Detect word boundaries more accurately")); |
Benno Schulenberg | 0a387d9 | 2016-08-07 17:02:02 +0200 | [diff] [blame] | 854 | print_opt(_("-X <str>"), _("--wordchars=<str>"), |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 855 | N_("Which other characters are word parts")); |
David Lawrence Ramsey | 4f03daf | 2005-08-10 22:12:28 +0000 | [diff] [blame] | 856 | #endif |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 857 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 858 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 859 | print_opt(_("-Y <str>"), _("--syntax=<str>"), |
| 860 | N_("Syntax definition to use for coloring")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 861 | #endif |
Benno Schulenberg | 7952615 | 2015-07-15 19:40:37 +0000 | [diff] [blame] | 862 | print_opt("-c", "--constantshow", N_("Constantly show cursor position")); |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 863 | print_opt("-d", "--rebinddelete", |
David Lawrence Ramsey | 86ce390 | 2005-05-21 17:15:46 +0000 | [diff] [blame] | 864 | N_("Fix Backspace/Delete confusion problem")); |
Benno Schulenberg | ace1517 | 2015-08-01 08:47:38 +0000 | [diff] [blame] | 865 | print_opt("-h", "--help", N_("Show this help text and exit")); |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 866 | #ifndef NANO_TINY |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 867 | print_opt("-i", "--autoindent", N_("Automatically indent new lines")); |
David Lawrence Ramsey | 660e05b | 2006-05-12 19:30:28 +0000 | [diff] [blame] | 868 | print_opt("-k", "--cut", N_("Cut from cursor to end of line")); |
David Lawrence Ramsey | b80d49f | 2005-03-26 22:49:46 +0000 | [diff] [blame] | 869 | #endif |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 870 | #ifdef ENABLE_LINENUMBERS |
| 871 | print_opt("-l", "--linenumbers", N_("Show line numbers in front of the text")); |
| 872 | #endif |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 873 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 69e1ce5 | 2006-06-08 02:37:45 +0000 | [diff] [blame] | 874 | print_opt("-m", "--mouse", N_("Enable the use of the mouse")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 875 | #endif |
Benno Schulenberg | db7064b | 2014-04-08 18:59:30 +0000 | [diff] [blame] | 876 | print_opt("-n", "--noread", N_("Do not read the file (only write it)")); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 877 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | e77ebfa | 2006-05-14 15:19:38 +0000 | [diff] [blame] | 878 | print_opt(_("-o <dir>"), _("--operatingdir=<dir>"), |
David Lawrence Ramsey | 86ce390 | 2005-05-21 17:15:46 +0000 | [diff] [blame] | 879 | N_("Set operating directory")); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 880 | #endif |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 881 | print_opt("-p", "--preserve", N_("Preserve XON (^Q) and XOFF (^S) keys")); |
Benno Schulenberg | 85ddc71 | 2014-07-02 19:57:23 +0000 | [diff] [blame] | 882 | #ifndef DISABLE_NANORC |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 883 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 884 | print_opt("-q", "--quiet", |
| 885 | N_("Silently ignore startup issues like rc file errors")); |
Benno Schulenberg | 85ddc71 | 2014-07-02 19:57:23 +0000 | [diff] [blame] | 886 | #endif |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 887 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | e77ebfa | 2006-05-14 15:19:38 +0000 | [diff] [blame] | 888 | print_opt(_("-r <#cols>"), _("--fill=<#cols>"), |
Benno Schulenberg | 00db994 | 2014-04-23 20:42:55 +0000 | [diff] [blame] | 889 | N_("Set hard-wrapping point at column #cols")); |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 890 | #endif |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 891 | #ifndef DISABLE_SPELLER |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 892 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 893 | print_opt(_("-s <prog>"), _("--speller=<prog>"), |
| 894 | N_("Enable alternate speller")); |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 895 | #endif |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 896 | print_opt("-t", "--tempfile", N_("Auto save on exit, don't prompt")); |
Benno Schulenberg | eac0446 | 2015-08-09 16:31:01 +0000 | [diff] [blame] | 897 | #ifndef NANO_TINY |
| 898 | print_opt("-u", "--unix", N_("Save a file by default in Unix format")); |
| 899 | #endif |
David Lawrence Ramsey | 7b0531a | 2006-07-31 01:30:31 +0000 | [diff] [blame] | 900 | print_opt("-v", "--view", N_("View mode (read-only)")); |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 901 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | 00db994 | 2014-04-23 20:42:55 +0000 | [diff] [blame] | 902 | print_opt("-w", "--nowrap", N_("Don't hard-wrap long lines")); |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 903 | #endif |
David Lawrence Ramsey | 69e1ce5 | 2006-06-08 02:37:45 +0000 | [diff] [blame] | 904 | print_opt("-x", "--nohelp", N_("Don't show the two help lines")); |
Benno Schulenberg | 5885e34 | 2015-07-29 19:31:50 +0000 | [diff] [blame] | 905 | if (!ISSET(RESTRICTED)) |
Benno Schulenberg | 104ea6b | 2015-07-30 11:29:45 +0000 | [diff] [blame] | 906 | print_opt("-z", "--suspend", N_("Enable suspension")); |
Benno Schulenberg | c347ce4 | 2014-03-03 21:30:50 +0000 | [diff] [blame] | 907 | #ifndef NANO_TINY |
Chris Allegretta | 05417a2 | 2009-08-17 07:52:10 +0000 | [diff] [blame] | 908 | print_opt("-$", "--softwrap", N_("Enable soft line wrapping")); |
Benno Schulenberg | c347ce4 | 2014-03-03 21:30:50 +0000 | [diff] [blame] | 909 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 910 | } |
| 911 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 912 | /* Display the current version of nano, the date and time it was |
| 913 | * compiled, contact information for it, and the configuration options |
| 914 | * it was compiled with. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 915 | void version(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 916 | { |
Benno Schulenberg | ec295f5 | 2016-04-08 10:22:09 +0200 | [diff] [blame] | 917 | #ifdef REVISION |
Benno Schulenberg | 49fc528 | 2016-08-14 21:18:00 +0200 | [diff] [blame] | 918 | printf(" GNU nano from git, %s\n", REVISION); |
Benno Schulenberg | ec295f5 | 2016-04-08 10:22:09 +0200 | [diff] [blame] | 919 | #else |
Benno Schulenberg | 49fc528 | 2016-08-14 21:18:00 +0200 | [diff] [blame] | 920 | printf(_(" GNU nano, version %s\n"), VERSION); |
Benno Schulenberg | ec295f5 | 2016-04-08 10:22:09 +0200 | [diff] [blame] | 921 | #endif |
Benno Schulenberg | 1b293ff | 2016-01-10 16:18:43 +0000 | [diff] [blame] | 922 | printf(" (C) 1999..2016 Free Software Foundation, Inc.\n"); |
Benno Schulenberg | c118397 | 2017-02-21 20:27:49 +0100 | [diff] [blame] | 923 | printf(_(" (C) 2014..%s the contributors to nano\n"), "2017"); |
David Lawrence Ramsey | b1d9b41 | 2005-03-11 04:22:34 +0000 | [diff] [blame] | 924 | printf( |
Jordi Mallach | c2b199e | 2016-06-20 19:44:56 +0200 | [diff] [blame] | 925 | _(" Email: nano@nano-editor.org Web: https://nano-editor.org/")); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 926 | printf(_("\n Compiled options:")); |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 927 | |
Benno Schulenberg | 2d83e8d | 2014-04-05 19:57:17 +0000 | [diff] [blame] | 928 | #ifdef NANO_TINY |
| 929 | printf(" --enable-tiny"); |
| 930 | #ifndef DISABLE_BROWSER |
| 931 | printf(" --enable-browser"); |
| 932 | #endif |
| 933 | #ifndef DISABLE_COLOR |
| 934 | printf(" --enable-color"); |
| 935 | #endif |
| 936 | #ifndef DISABLE_EXTRA |
| 937 | printf(" --enable-extra"); |
| 938 | #endif |
| 939 | #ifndef DISABLE_HELP |
| 940 | printf(" --enable-help"); |
| 941 | #endif |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 942 | #ifndef DISABLE_HISTORIES |
| 943 | printf(" --enable-histories"); |
| 944 | #endif |
Benno Schulenberg | 2d83e8d | 2014-04-05 19:57:17 +0000 | [diff] [blame] | 945 | #ifndef DISABLE_JUSTIFY |
| 946 | printf(" --enable-justify"); |
| 947 | #endif |
Benno Schulenberg | b988132 | 2014-04-13 19:54:47 +0000 | [diff] [blame] | 948 | #ifdef HAVE_LIBMAGIC |
| 949 | printf(" --enable-libmagic"); |
| 950 | #endif |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 951 | #ifdef ENABLE_LINENUMBERS |
| 952 | printf(" --enable-linenumbers"); |
| 953 | #endif |
Benno Schulenberg | 2d83e8d | 2014-04-05 19:57:17 +0000 | [diff] [blame] | 954 | #ifndef DISABLE_MOUSE |
| 955 | printf(" --enable-mouse"); |
| 956 | #endif |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 957 | #ifndef DISABLE_NANORC |
| 958 | printf(" --enable-nanorc"); |
| 959 | #endif |
Benno Schulenberg | 2d83e8d | 2014-04-05 19:57:17 +0000 | [diff] [blame] | 960 | #ifndef DISABLE_MULTIBUFFER |
| 961 | printf(" --enable-multibuffer"); |
| 962 | #endif |
| 963 | #ifndef DISABLE_OPERATINGDIR |
| 964 | printf(" --enable-operatingdir"); |
| 965 | #endif |
| 966 | #ifndef DISABLE_SPELLER |
| 967 | printf(" --enable-speller"); |
| 968 | #endif |
| 969 | #ifndef DISABLE_TABCOMP |
| 970 | printf(" --enable-tabcomp"); |
| 971 | #endif |
| 972 | #ifndef DISABLE_WRAPPING |
| 973 | printf(" --enable-wrapping"); |
| 974 | #endif |
| 975 | #else /* !NANO_TINY */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 976 | #ifdef DISABLE_BROWSER |
Chris Allegretta | 6636dc3 | 2001-01-05 05:41:07 +0000 | [diff] [blame] | 977 | printf(" --disable-browser"); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 978 | #endif |
Benno Schulenberg | 2d83e8d | 2014-04-05 19:57:17 +0000 | [diff] [blame] | 979 | #ifdef DISABLE_COLOR |
| 980 | printf(" --disable-color"); |
| 981 | #endif |
Mike Scalora | 6a2032f | 2016-05-25 22:13:50 +0200 | [diff] [blame] | 982 | #ifndef ENABLE_COMMENT |
| 983 | printf(" --disable-comment"); |
| 984 | #endif |
Benno Schulenberg | d17438b | 2014-04-03 20:57:44 +0000 | [diff] [blame] | 985 | #ifdef DISABLE_EXTRA |
| 986 | printf(" --disable-extra"); |
| 987 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 988 | #ifdef DISABLE_HELP |
| 989 | printf(" --disable-help"); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 990 | #endif |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 991 | #ifdef DISABLE_HISTORIES |
| 992 | printf(" --disable-histories"); |
| 993 | #endif |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 994 | #ifdef DISABLE_JUSTIFY |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 995 | printf(" --disable-justify"); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 996 | #endif |
Benno Schulenberg | b988132 | 2014-04-13 19:54:47 +0000 | [diff] [blame] | 997 | #ifndef HAVE_LIBMAGIC |
| 998 | printf(" --disable-libmagic"); |
| 999 | #endif |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 1000 | #ifndef ENABLE_LINENUMBERS |
| 1001 | printf(" --disable-linenumbers"); |
| 1002 | #endif |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 1003 | #ifdef DISABLE_MOUSE |
Chris Allegretta | 84de552 | 2001-04-12 14:51:48 +0000 | [diff] [blame] | 1004 | printf(" --disable-mouse"); |
Chris Allegretta | b7d00ef | 2000-12-18 05:36:51 +0000 | [diff] [blame] | 1005 | #endif |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 1006 | #ifdef DISABLE_MULTIBUFFER |
| 1007 | printf(" --disable-multibuffer"); |
| 1008 | #endif |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 1009 | #ifdef DISABLE_NANORC |
| 1010 | printf(" --disable-nanorc"); |
| 1011 | #endif |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1012 | #ifdef DISABLE_OPERATINGDIR |
| 1013 | printf(" --disable-operatingdir"); |
| 1014 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1015 | #ifdef DISABLE_SPELLER |
| 1016 | printf(" --disable-speller"); |
| 1017 | #endif |
| 1018 | #ifdef DISABLE_TABCOMP |
| 1019 | printf(" --disable-tabcomp"); |
| 1020 | #endif |
Benno Schulenberg | 1df4115 | 2016-12-15 12:38:47 +0100 | [diff] [blame] | 1021 | #ifndef ENABLE_WORDCOMPLETION |
| 1022 | printf(" --disable-wordcomp"); |
| 1023 | #endif |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 1024 | #ifdef DISABLE_WRAPPING |
| 1025 | printf(" --disable-wrapping"); |
| 1026 | #endif |
Benno Schulenberg | 2d83e8d | 2014-04-05 19:57:17 +0000 | [diff] [blame] | 1027 | #endif /* !NANO_TINY */ |
| 1028 | |
David Lawrence Ramsey | e53e125 | 2006-07-19 15:50:19 +0000 | [diff] [blame] | 1029 | #ifdef DISABLE_ROOTWRAPPING |
David Lawrence Ramsey | dc60b72 | 2002-10-25 16:08:53 +0000 | [diff] [blame] | 1030 | printf(" --disable-wrapping-as-root"); |
| 1031 | #endif |
David Lawrence Ramsey | 85c0b3c | 2005-06-29 17:10:58 +0000 | [diff] [blame] | 1032 | #ifdef DEBUG |
| 1033 | printf(" --enable-debug"); |
| 1034 | #endif |
Benno Schulenberg | 2d83e8d | 2014-04-05 19:57:17 +0000 | [diff] [blame] | 1035 | #ifndef ENABLE_NLS |
| 1036 | printf(" --disable-nls"); |
David Lawrence Ramsey | 85c0b3c | 2005-06-29 17:10:58 +0000 | [diff] [blame] | 1037 | #endif |
David Lawrence Ramsey | 7eb30a8 | 2005-07-17 02:40:07 +0000 | [diff] [blame] | 1038 | #ifdef ENABLE_UTF8 |
David Lawrence Ramsey | 85c0b3c | 2005-06-29 17:10:58 +0000 | [diff] [blame] | 1039 | printf(" --enable-utf8"); |
Benno Schulenberg | 2d83e8d | 2014-04-05 19:57:17 +0000 | [diff] [blame] | 1040 | #else |
| 1041 | printf(" --disable-utf8"); |
David Lawrence Ramsey | 85c0b3c | 2005-06-29 17:10:58 +0000 | [diff] [blame] | 1042 | #endif |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1043 | #ifdef USE_SLANG |
| 1044 | printf(" --with-slang"); |
| 1045 | #endif |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 1046 | printf("\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1047 | } |
| 1048 | |
David Lawrence Ramsey | cb04b56 | 2007-01-09 22:56:56 +0000 | [diff] [blame] | 1049 | /* If the current file buffer has been modified, and the TEMP_FILE flag |
| 1050 | * isn't set, ask whether or not to save the file buffer. If the |
Benno Schulenberg | 4f3f976 | 2014-06-16 20:44:34 +0000 | [diff] [blame] | 1051 | * TEMP_FILE flag is set and the current file has a name, save it |
| 1052 | * unconditionally. Then, if more than one file buffer is open, close |
| 1053 | * the current file buffer and switch to the next one. If only one file |
| 1054 | * buffer is open, exit from nano. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1055 | void do_exit(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1056 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1057 | int i; |
Chris Allegretta | 13fd44b | 2002-01-02 13:59:11 +0000 | [diff] [blame] | 1058 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1059 | /* If the file hasn't been modified, pretend the user chose not to |
| 1060 | * save. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 1061 | if (!openfile->modified) |
David Lawrence Ramsey | e19449e | 2005-11-07 21:45:44 +0000 | [diff] [blame] | 1062 | i = 0; |
Benno Schulenberg | 4f3f976 | 2014-06-16 20:44:34 +0000 | [diff] [blame] | 1063 | /* If the TEMP_FILE flag is set and the current file has a name, |
| 1064 | * pretend the user chose to save. */ |
| 1065 | else if (openfile->filename[0] != '\0' && ISSET(TEMP_FILE)) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1066 | i = 1; |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1067 | /* Otherwise, ask the user whether or not to save. */ |
Benno Schulenberg | 4f3f976 | 2014-06-16 20:44:34 +0000 | [diff] [blame] | 1068 | else { |
| 1069 | /* If the TEMP_FILE flag is set, and the current file doesn't |
Benno Schulenberg | 0d9397e | 2015-10-29 10:36:07 +0000 | [diff] [blame] | 1070 | * have a name, warn the user before prompting for a name. */ |
| 1071 | if (ISSET(TEMP_FILE)) |
David Lawrence Ramsey | e4d4524 | 2016-11-27 15:01:54 -0600 | [diff] [blame] | 1072 | warn_and_shortly_pause(_("No file name")); |
Benno Schulenberg | 4f3f976 | 2014-06-16 20:44:34 +0000 | [diff] [blame] | 1073 | |
Benno Schulenberg | 17cf833 | 2016-05-30 09:09:36 +0200 | [diff] [blame] | 1074 | i = do_yesno_prompt(FALSE, _("Save modified buffer? " |
| 1075 | "(Answering \"No\" will DISCARD changes.) ")); |
Benno Schulenberg | 4f3f976 | 2014-06-16 20:44:34 +0000 | [diff] [blame] | 1076 | } |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1077 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1078 | #ifdef DEBUG |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 1079 | dump_filestruct(openfile->fileage); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1080 | #endif |
| 1081 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1082 | /* If the user chose not to save, or if the user chose to save and |
| 1083 | * the save succeeded, we're ready to exit. */ |
Benno Schulenberg | 8cc6308 | 2015-12-23 16:34:44 +0000 | [diff] [blame] | 1084 | if (i == 0 || (i == 1 && do_writeout(TRUE))) |
| 1085 | close_and_go(); |
| 1086 | else if (i != 1) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1087 | statusbar(_("Cancelled")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1088 | } |
| 1089 | |
Benno Schulenberg | 8cc6308 | 2015-12-23 16:34:44 +0000 | [diff] [blame] | 1090 | /* Close the current buffer, and terminate nano if it was the last. */ |
| 1091 | void close_and_go(void) |
| 1092 | { |
| 1093 | #ifndef NANO_TINY |
| 1094 | /* If there is a lockfile, remove it. */ |
| 1095 | if (ISSET(LOCKING) && openfile->lock_filename) |
| 1096 | delete_lockfile(openfile->lock_filename); |
| 1097 | #endif |
| 1098 | #ifndef DISABLE_MULTIBUFFER |
| 1099 | /* If there are no more open file buffers, jump off a cliff. */ |
Benno Schulenberg | 906ada8 | 2016-05-17 13:37:53 +0200 | [diff] [blame] | 1100 | if (!close_buffer()) |
Benno Schulenberg | 8cc6308 | 2015-12-23 16:34:44 +0000 | [diff] [blame] | 1101 | #endif |
| 1102 | finish(); |
| 1103 | } |
| 1104 | |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 1105 | /* Another placeholder for function mapping. */ |
Chris Allegretta | 637daa8 | 2011-02-07 14:45:56 +0000 | [diff] [blame] | 1106 | void do_cancel(void) |
| 1107 | { |
| 1108 | ; |
| 1109 | } |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1110 | |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 1111 | static struct sigaction pager_oldaction, pager_newaction; |
| 1112 | /* Original and temporary handlers for SIGINT. */ |
| 1113 | static bool pager_sig_failed = FALSE; |
| 1114 | /* Did sigaction() fail without changing the signal handlers? */ |
| 1115 | static bool pager_input_aborted = FALSE; |
| 1116 | /* Did someone invoke the pager and abort it via ^C? */ |
Chris Allegretta | 35b5976 | 2010-03-21 05:31:43 +0000 | [diff] [blame] | 1117 | |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1118 | /* Things which need to be run regardless of whether |
Benno Schulenberg | 499c6de | 2014-03-24 21:48:23 +0000 | [diff] [blame] | 1119 | * we finished the stdin pipe correctly or not. */ |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1120 | void finish_stdin_pager(void) |
| 1121 | { |
| 1122 | FILE *f; |
| 1123 | int ttystdin; |
| 1124 | |
Benno Schulenberg | 499c6de | 2014-03-24 21:48:23 +0000 | [diff] [blame] | 1125 | /* Read whatever we did get from stdin. */ |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1126 | f = fopen("/dev/stdin", "rb"); |
Benno Schulenberg | 3345e34 | 2015-03-20 11:18:22 +0000 | [diff] [blame] | 1127 | if (f == NULL) |
| 1128 | nperror("fopen"); |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1129 | |
Chris Allegretta | 2c7b506 | 2009-12-09 16:51:43 +0000 | [diff] [blame] | 1130 | read_file(f, 0, "stdin", TRUE, FALSE); |
Benno Schulenberg | 907ba3a | 2016-04-25 15:44:52 +0200 | [diff] [blame] | 1131 | openfile->edittop = openfile->fileage; |
| 1132 | |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1133 | ttystdin = open("/dev/tty", O_RDONLY); |
| 1134 | if (!ttystdin) |
Benno Schulenberg | 3345e34 | 2015-03-20 11:18:22 +0000 | [diff] [blame] | 1135 | die(_("Couldn't reopen stdin from keyboard, sorry\n")); |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1136 | |
| 1137 | dup2(ttystdin,0); |
| 1138 | close(ttystdin); |
Chris Allegretta | 35b5976 | 2010-03-21 05:31:43 +0000 | [diff] [blame] | 1139 | if (!pager_input_aborted) |
| 1140 | tcgetattr(0, &oldterm); |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1141 | if (!pager_sig_failed && sigaction(SIGINT, &pager_oldaction, NULL) == -1) |
Benno Schulenberg | 3345e34 | 2015-03-20 11:18:22 +0000 | [diff] [blame] | 1142 | nperror("sigaction"); |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1143 | terminal_init(); |
| 1144 | doupdate(); |
| 1145 | } |
| 1146 | |
Benno Schulenberg | 499c6de | 2014-03-24 21:48:23 +0000 | [diff] [blame] | 1147 | /* Cancel reading from stdin like a pager. */ |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1148 | RETSIGTYPE cancel_stdin_pager(int signal) |
| 1149 | { |
Chris Allegretta | 35b5976 | 2010-03-21 05:31:43 +0000 | [diff] [blame] | 1150 | pager_input_aborted = TRUE; |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Benno Schulenberg | 499c6de | 2014-03-24 21:48:23 +0000 | [diff] [blame] | 1153 | /* Let nano read stdin for the first file at least. */ |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1154 | void stdin_pager(void) |
| 1155 | { |
| 1156 | endwin(); |
Chris Allegretta | 35b5976 | 2010-03-21 05:31:43 +0000 | [diff] [blame] | 1157 | if (!pager_input_aborted) |
| 1158 | tcsetattr(0, TCSANOW, &oldterm); |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1159 | fprintf(stderr, _("Reading from stdin, ^C to abort\n")); |
| 1160 | |
Benno Schulenberg | 57d1755 | 2016-09-03 12:14:08 +0200 | [diff] [blame] | 1161 | #ifndef NANO_TINY |
Benno Schulenberg | 499c6de | 2014-03-24 21:48:23 +0000 | [diff] [blame] | 1162 | /* Enable interpretation of the special control keys so that |
| 1163 | * we get SIGINT when Ctrl-C is pressed. */ |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1164 | enable_signals(); |
Chris Allegretta | ecc245c | 2009-02-01 00:04:42 +0000 | [diff] [blame] | 1165 | #endif |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1166 | |
Benno Schulenberg | 3345e34 | 2015-03-20 11:18:22 +0000 | [diff] [blame] | 1167 | /* Set things up so that SIGINT will cancel the new process. */ |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1168 | if (sigaction(SIGINT, NULL, &pager_newaction) == -1) { |
| 1169 | pager_sig_failed = TRUE; |
| 1170 | nperror("sigaction"); |
| 1171 | } else { |
| 1172 | pager_newaction.sa_handler = cancel_stdin_pager; |
| 1173 | if (sigaction(SIGINT, &pager_newaction, &pager_oldaction) == -1) { |
| 1174 | pager_sig_failed = TRUE; |
| 1175 | nperror("sigaction"); |
| 1176 | } |
| 1177 | } |
Chris Allegretta | 35b5976 | 2010-03-21 05:31:43 +0000 | [diff] [blame] | 1178 | |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 1179 | open_buffer("", FALSE); |
| 1180 | finish_stdin_pager(); |
| 1181 | } |
| 1182 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1183 | /* Initialize the signal handlers. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1184 | void signal_init(void) |
| 1185 | { |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 1186 | /* Trap SIGINT and SIGQUIT because we want them to do useful things. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1187 | memset(&act, 0, sizeof(struct sigaction)); |
| 1188 | act.sa_handler = SIG_IGN; |
| 1189 | sigaction(SIGINT, &act, NULL); |
David Lawrence Ramsey | 2897d2b | 2004-01-26 20:56:20 +0000 | [diff] [blame] | 1190 | sigaction(SIGQUIT, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1191 | |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 1192 | /* Trap SIGHUP and SIGTERM because we want to write the file out. */ |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1193 | act.sa_handler = handle_hupterm; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1194 | sigaction(SIGHUP, &act, NULL); |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1195 | sigaction(SIGTERM, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1196 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1197 | #ifndef NANO_TINY |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 1198 | /* Trap SIGWINCH because we want to handle window resizes. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1199 | act.sa_handler = handle_sigwinch; |
| 1200 | sigaction(SIGWINCH, &act, NULL); |
| 1201 | #endif |
| 1202 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1203 | /* Trap normal suspend (^Z) so we can handle it ourselves. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1204 | if (!ISSET(SUSPEND)) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1205 | act.sa_handler = SIG_IGN; |
| 1206 | sigaction(SIGTSTP, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1207 | } else { |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 1208 | /* Block all other signals in the suspend and continue handlers. |
| 1209 | * If we don't do this, other stuff interrupts them! */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1210 | sigfillset(&act.sa_mask); |
| 1211 | |
| 1212 | act.sa_handler = do_suspend; |
| 1213 | sigaction(SIGTSTP, &act, NULL); |
| 1214 | |
David Lawrence Ramsey | 8befda6 | 2005-12-06 19:39:56 +0000 | [diff] [blame] | 1215 | act.sa_handler = do_continue; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1216 | sigaction(SIGCONT, &act, NULL); |
| 1217 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1218 | } |
| 1219 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1220 | /* Handler for SIGHUP (hangup) and SIGTERM (terminate). */ |
David Lawrence Ramsey | 8befda6 | 2005-12-06 19:39:56 +0000 | [diff] [blame] | 1221 | RETSIGTYPE handle_hupterm(int signal) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1222 | { |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 1223 | die(_("Received SIGHUP or SIGTERM\n")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1226 | /* Handler for SIGTSTP (suspend). */ |
David Lawrence Ramsey | 8befda6 | 2005-12-06 19:39:56 +0000 | [diff] [blame] | 1227 | RETSIGTYPE do_suspend(int signal) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1228 | { |
David Lawrence Ramsey | 503bad0 | 2006-06-09 18:24:37 +0000 | [diff] [blame] | 1229 | #ifndef DISABLE_MOUSE |
| 1230 | /* Turn mouse support off. */ |
| 1231 | disable_mouse_support(); |
| 1232 | #endif |
| 1233 | |
Chris Allegretta | b43f691 | 2009-11-29 06:24:10 +0000 | [diff] [blame] | 1234 | /* Move the cursor to the last line of the screen. */ |
David Lawrence Ramsey | 85c775a | 2006-06-09 16:04:19 +0000 | [diff] [blame] | 1235 | move(LINES - 1, 0); |
Chris Allegretta | daaf468 | 2009-11-28 03:01:30 +0000 | [diff] [blame] | 1236 | endwin(); |
David Lawrence Ramsey | f70f67b | 2006-06-03 17:31:52 +0000 | [diff] [blame] | 1237 | |
David Lawrence Ramsey | 85c775a | 2006-06-09 16:04:19 +0000 | [diff] [blame] | 1238 | /* Display our helpful message. */ |
| 1239 | printf(_("Use \"fg\" to return to nano.\n")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1240 | fflush(stdout); |
| 1241 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1242 | /* Restore the old terminal settings. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1243 | tcsetattr(0, TCSANOW, &oldterm); |
| 1244 | |
David Lawrence Ramsey | 99bede3 | 2003-09-29 07:21:11 +0000 | [diff] [blame] | 1245 | /* Trap SIGHUP and SIGTERM so we can properly deal with them while |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1246 | * suspended. */ |
David Lawrence Ramsey | 99bede3 | 2003-09-29 07:21:11 +0000 | [diff] [blame] | 1247 | act.sa_handler = handle_hupterm; |
| 1248 | sigaction(SIGHUP, &act, NULL); |
| 1249 | sigaction(SIGTERM, &act, NULL); |
| 1250 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1251 | /* Do what mutt does: send ourselves a SIGSTOP. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1252 | kill(0, SIGSTOP); |
| 1253 | } |
| 1254 | |
Benno Schulenberg | a338798 | 2015-05-03 13:56:51 +0000 | [diff] [blame] | 1255 | /* The version of above function that is bound to a key. */ |
Chris Allegretta | ab53864 | 2010-11-12 06:22:12 +0000 | [diff] [blame] | 1256 | void do_suspend_void(void) |
Chris Allegretta | 0018d8e | 2008-03-13 08:23:52 +0000 | [diff] [blame] | 1257 | { |
| 1258 | if (ISSET(SUSPEND)) |
| 1259 | do_suspend(0); |
Benno Schulenberg | 20ba559 | 2015-07-30 20:15:01 +0000 | [diff] [blame] | 1260 | else { |
| 1261 | statusbar(_("Suspension is not enabled")); |
| 1262 | beep(); |
| 1263 | } |
Chris Allegretta | 0018d8e | 2008-03-13 08:23:52 +0000 | [diff] [blame] | 1264 | } |
| 1265 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1266 | /* Handler for SIGCONT (continue after suspend). */ |
David Lawrence Ramsey | 8befda6 | 2005-12-06 19:39:56 +0000 | [diff] [blame] | 1267 | RETSIGTYPE do_continue(int signal) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1268 | { |
David Lawrence Ramsey | 503bad0 | 2006-06-09 18:24:37 +0000 | [diff] [blame] | 1269 | #ifndef DISABLE_MOUSE |
| 1270 | /* Turn mouse support back on if it was on before. */ |
| 1271 | if (ISSET(USE_MOUSE)) |
| 1272 | enable_mouse_support(); |
| 1273 | #endif |
| 1274 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1275 | #ifndef NANO_TINY |
Benno Schulenberg | 07a39e8 | 2016-06-29 15:55:38 +0200 | [diff] [blame] | 1276 | /* Perhaps the user resized the window while we slept. So act as if, |
| 1277 | * and restore the terminal to its previous state in the process. */ |
| 1278 | regenerate_screen(); |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 1279 | #else |
Benno Schulenberg | bfcce57 | 2016-12-04 12:27:12 +0100 | [diff] [blame] | 1280 | /* Restore the state of the terminal and redraw the whole screen. */ |
David Lawrence Ramsey | 85c775a | 2006-06-09 16:04:19 +0000 | [diff] [blame] | 1281 | terminal_init(); |
David Lawrence Ramsey | 85c775a | 2006-06-09 16:04:19 +0000 | [diff] [blame] | 1282 | total_refresh(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1283 | #endif |
| 1284 | } |
| 1285 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1286 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1287 | /* Handler for SIGWINCH (window size change). */ |
David Lawrence Ramsey | 8befda6 | 2005-12-06 19:39:56 +0000 | [diff] [blame] | 1288 | RETSIGTYPE handle_sigwinch(int signal) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1289 | { |
Benno Schulenberg | 75d64e6 | 2015-05-28 13:02:29 +0000 | [diff] [blame] | 1290 | /* Let the input routine know that a SIGWINCH has occurred. */ |
Benno Schulenberg | b77e6bd | 2016-12-14 20:37:03 +0100 | [diff] [blame] | 1291 | the_window_resized = TRUE; |
Benno Schulenberg | 75d64e6 | 2015-05-28 13:02:29 +0000 | [diff] [blame] | 1292 | } |
| 1293 | |
| 1294 | /* Reinitialize and redraw the screen completely. */ |
| 1295 | void regenerate_screen(void) |
| 1296 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1297 | const char *tty = ttyname(0); |
David Lawrence Ramsey | 9d69169 | 2005-03-08 16:41:53 +0000 | [diff] [blame] | 1298 | int fd, result = 0; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1299 | struct winsize win; |
| 1300 | |
Benno Schulenberg | b77e6bd | 2016-12-14 20:37:03 +0100 | [diff] [blame] | 1301 | /* Reset the trigger. */ |
| 1302 | the_window_resized = FALSE; |
| 1303 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1304 | if (tty == NULL) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1305 | return; |
| 1306 | fd = open(tty, O_RDWR); |
| 1307 | if (fd == -1) |
| 1308 | return; |
| 1309 | result = ioctl(fd, TIOCGWINSZ, &win); |
| 1310 | close(fd); |
| 1311 | if (result == -1) |
| 1312 | return; |
| 1313 | |
David Lawrence Ramsey | 9b33d51 | 2006-03-20 04:46:48 +0000 | [diff] [blame] | 1314 | /* We could check whether the COLS or LINES changed, and return |
| 1315 | * otherwise. However, COLS and LINES are curses global variables, |
| 1316 | * and in some cases curses has already updated them. But not in |
| 1317 | * all cases. Argh. */ |
Benno Schulenberg | 90ea21f | 2014-03-26 10:45:07 +0000 | [diff] [blame] | 1318 | #ifdef REDEFINING_MACROS_OK |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1319 | COLS = win.ws_col; |
| 1320 | LINES = win.ws_row; |
Chris Allegretta | 42bd871 | 2010-03-07 19:35:46 +0000 | [diff] [blame] | 1321 | #endif |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 1322 | editwincols = COLS - margin; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1323 | |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1324 | #ifdef USE_SLANG |
| 1325 | /* Slang curses emulation brain damage, part 1: If we just do what |
| 1326 | * curses does here, it'll only work properly if the resize made the |
| 1327 | * window smaller. Do what mutt does: Leave and immediately reenter |
| 1328 | * Slang screen management mode. */ |
| 1329 | SLsmg_reset_smg(); |
| 1330 | SLsmg_init_smg(); |
| 1331 | #else |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1332 | /* Do the equivalent of what Minimum Profit does: Leave and |
| 1333 | * immediately reenter curses mode. */ |
| 1334 | endwin(); |
David Lawrence Ramsey | 84fdb90 | 2005-08-14 20:08:49 +0000 | [diff] [blame] | 1335 | doupdate(); |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1336 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1337 | |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 1338 | /* Restore the terminal to its previous state. */ |
| 1339 | terminal_init(); |
| 1340 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1341 | /* Do the equivalent of what both mutt and Minimum Profit do: |
| 1342 | * Reinitialize all the windows based on the new screen |
| 1343 | * dimensions. */ |
| 1344 | window_init(); |
| 1345 | |
David Lawrence Ramsey | 907725f | 2004-11-12 00:09:20 +0000 | [diff] [blame] | 1346 | /* Redraw the contents of the windows that need it. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1347 | total_refresh(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1348 | } |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 1349 | |
Benno Schulenberg | 9e6b9a2 | 2016-01-04 10:37:11 +0000 | [diff] [blame] | 1350 | /* If allow is FALSE, block any SIGWINCH signal. If allow is TRUE, |
| 1351 | * unblock SIGWINCH so any pending ones can be dealt with. */ |
| 1352 | void allow_sigwinch(bool allow) |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 1353 | { |
| 1354 | sigset_t winch; |
Benno Schulenberg | bff6a90 | 2015-07-28 19:39:34 +0000 | [diff] [blame] | 1355 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 1356 | sigemptyset(&winch); |
| 1357 | sigaddset(&winch, SIGWINCH); |
David Lawrence Ramsey | 9c3249c | 2005-07-01 22:58:47 +0000 | [diff] [blame] | 1358 | sigprocmask(allow ? SIG_UNBLOCK : SIG_BLOCK, &winch, NULL); |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 1359 | } |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1360 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 1361 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1362 | #ifndef NANO_TINY |
Benno Schulenberg | 499c6de | 2014-03-24 21:48:23 +0000 | [diff] [blame] | 1363 | /* Handle the global toggle specified in flag. */ |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1364 | void do_toggle(int flag) |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1365 | { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1366 | bool enabled; |
Chris Allegretta | f0f63a8 | 2000-09-02 18:44:21 +0000 | [diff] [blame] | 1367 | |
Benno Schulenberg | efbf7b6 | 2015-07-29 20:21:45 +0000 | [diff] [blame] | 1368 | if (ISSET(RESTRICTED) && (flag == SUSPEND || flag == MULTIBUFFER || |
| 1369 | flag == BACKUP_FILE || flag == NO_COLOR_SYNTAX)) { |
Benno Schulenberg | 6847616 | 2015-07-30 18:10:16 +0000 | [diff] [blame] | 1370 | show_restricted_warning(); |
Benno Schulenberg | efbf7b6 | 2015-07-29 20:21:45 +0000 | [diff] [blame] | 1371 | return; |
| 1372 | } |
| 1373 | |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1374 | TOGGLE(flag); |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 1375 | |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1376 | switch (flag) { |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 1377 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1378 | case USE_MOUSE: |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 1379 | mouse_init(); |
| 1380 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1381 | #endif |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1382 | case MORE_SPACE: |
| 1383 | case NO_HELP: |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 1384 | window_init(); |
David Lawrence Ramsey | 637b8bb | 2005-01-17 05:06:55 +0000 | [diff] [blame] | 1385 | total_refresh(); |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 1386 | break; |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1387 | case SUSPEND: |
David Lawrence Ramsey | 637b8bb | 2005-01-17 05:06:55 +0000 | [diff] [blame] | 1388 | signal_init(); |
David Lawrence Ramsey | 62a217d | 2004-11-27 16:54:00 +0000 | [diff] [blame] | 1389 | break; |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1390 | case WHITESPACE_DISPLAY: |
Benno Schulenberg | f920e0d | 2016-12-03 17:00:28 +0100 | [diff] [blame] | 1391 | titlebar(NULL); /* Fall through. */ |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 1392 | #ifndef DISABLE_COLOR |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1393 | case NO_COLOR_SYNTAX: |
David Lawrence Ramsey | 637b8bb | 2005-01-17 05:06:55 +0000 | [diff] [blame] | 1394 | #endif |
Chris Allegretta | 05417a2 | 2009-08-17 07:52:10 +0000 | [diff] [blame] | 1395 | case SOFTWRAP: |
Benno Schulenberg | 2789bb0 | 2016-10-21 13:52:40 +0200 | [diff] [blame] | 1396 | refresh_needed = TRUE; |
Chris Allegretta | 05417a2 | 2009-08-17 07:52:10 +0000 | [diff] [blame] | 1397 | break; |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1398 | } |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 1399 | |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1400 | enabled = ISSET(flag); |
David Lawrence Ramsey | 53aca71 | 2005-06-09 04:00:03 +0000 | [diff] [blame] | 1401 | |
Benno Schulenberg | 4eac699 | 2016-12-03 20:25:16 +0100 | [diff] [blame] | 1402 | if (flag == NO_HELP || flag == NO_WRAP || flag == NO_COLOR_SYNTAX) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1403 | enabled = !enabled; |
David Lawrence Ramsey | 53aca71 | 2005-06-09 04:00:03 +0000 | [diff] [blame] | 1404 | |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 1405 | statusline(HUSH, "%s %s", _(flagtostr(flag)), |
| 1406 | enabled ? _("enabled") : _("disabled")); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1407 | } |
Chris Allegretta | 637daa8 | 2011-02-07 14:45:56 +0000 | [diff] [blame] | 1408 | |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 1409 | /* Bleh. */ |
Chris Allegretta | 637daa8 | 2011-02-07 14:45:56 +0000 | [diff] [blame] | 1410 | void do_toggle_void(void) |
| 1411 | { |
Benno Schulenberg | d19be5a | 2014-04-08 18:38:45 +0000 | [diff] [blame] | 1412 | ; |
Chris Allegretta | 637daa8 | 2011-02-07 14:45:56 +0000 | [diff] [blame] | 1413 | } |
Benno Schulenberg | 7b7d2bf | 2016-09-01 09:36:47 +0200 | [diff] [blame] | 1414 | #endif /* !NANO_TINY */ |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 1415 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1416 | /* Disable extended input and output processing in our terminal |
| 1417 | * settings. */ |
David Lawrence Ramsey | 8bcac8a | 2005-06-09 04:02:57 +0000 | [diff] [blame] | 1418 | void disable_extended_io(void) |
David Lawrence Ramsey | 013344c | 2004-09-22 22:45:08 +0000 | [diff] [blame] | 1419 | { |
| 1420 | struct termios term; |
| 1421 | |
| 1422 | tcgetattr(0, &term); |
| 1423 | term.c_lflag &= ~IEXTEN; |
David Lawrence Ramsey | 8bcac8a | 2005-06-09 04:02:57 +0000 | [diff] [blame] | 1424 | term.c_oflag &= ~OPOST; |
David Lawrence Ramsey | 013344c | 2004-09-22 22:45:08 +0000 | [diff] [blame] | 1425 | tcsetattr(0, TCSANOW, &term); |
| 1426 | } |
| 1427 | |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1428 | /* Disable interpretation of the special control keys in our terminal |
| 1429 | * settings. */ |
| 1430 | void disable_signals(void) |
| 1431 | { |
| 1432 | struct termios term; |
| 1433 | |
| 1434 | tcgetattr(0, &term); |
| 1435 | term.c_lflag &= ~ISIG; |
| 1436 | tcsetattr(0, TCSANOW, &term); |
| 1437 | } |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1438 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1439 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1440 | /* Enable interpretation of the special control keys in our terminal |
| 1441 | * settings. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1442 | void enable_signals(void) |
| 1443 | { |
| 1444 | struct termios term; |
| 1445 | |
| 1446 | tcgetattr(0, &term); |
| 1447 | term.c_lflag |= ISIG; |
| 1448 | tcsetattr(0, TCSANOW, &term); |
| 1449 | } |
| 1450 | #endif |
| 1451 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1452 | /* Disable interpretation of the flow control characters in our terminal |
| 1453 | * settings. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1454 | void disable_flow_control(void) |
| 1455 | { |
| 1456 | struct termios term; |
| 1457 | |
| 1458 | tcgetattr(0, &term); |
David Lawrence Ramsey | 5e50e2f | 2005-06-23 22:28:56 +0000 | [diff] [blame] | 1459 | term.c_iflag &= ~IXON; |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1460 | tcsetattr(0, TCSANOW, &term); |
| 1461 | } |
| 1462 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1463 | /* Enable interpretation of the flow control characters in our terminal |
| 1464 | * settings. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1465 | void enable_flow_control(void) |
| 1466 | { |
| 1467 | struct termios term; |
| 1468 | |
| 1469 | tcgetattr(0, &term); |
David Lawrence Ramsey | 5e50e2f | 2005-06-23 22:28:56 +0000 | [diff] [blame] | 1470 | term.c_iflag |= IXON; |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 1471 | tcsetattr(0, TCSANOW, &term); |
| 1472 | } |
| 1473 | |
David Lawrence Ramsey | 7f6f5a6 | 2007-12-18 02:03:00 +0000 | [diff] [blame] | 1474 | /* Set up the terminal state. Put the terminal in raw mode (read one |
| 1475 | * character at a time, disable the special control keys, and disable |
| 1476 | * the flow control characters), disable translation of carriage return |
| 1477 | * (^M) into newline (^J) so that we can tell the difference between the |
| 1478 | * Enter key and Ctrl-J, and disable echoing of characters as they're |
| 1479 | * typed. Finally, disable extended input and output processing, and, |
| 1480 | * if we're not in preserve mode, reenable interpretation of the flow |
| 1481 | * control characters. */ |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 1482 | void terminal_init(void) |
| 1483 | { |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1484 | #ifdef USE_SLANG |
| 1485 | /* Slang curses emulation brain damage, part 2: Slang doesn't |
| 1486 | * implement raw(), nonl(), or noecho() properly, so there's no way |
| 1487 | * to properly reinitialize the terminal using them. We have to |
David Lawrence Ramsey | bc66826 | 2007-12-18 16:51:48 +0000 | [diff] [blame] | 1488 | * disable the special control keys and interpretation of the flow |
| 1489 | * control characters using termios, save the terminal state after |
| 1490 | * the first call, and restore it on subsequent calls. */ |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1491 | static struct termios newterm; |
| 1492 | static bool newterm_set = FALSE; |
| 1493 | |
| 1494 | if (!newterm_set) { |
| 1495 | #endif |
| 1496 | |
| 1497 | raw(); |
| 1498 | nonl(); |
| 1499 | noecho(); |
| 1500 | disable_extended_io(); |
| 1501 | if (ISSET(PRESERVE)) |
| 1502 | enable_flow_control(); |
| 1503 | |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1504 | disable_signals(); |
Chris Allegretta | e137f12 | 2008-09-06 06:52:47 +0000 | [diff] [blame] | 1505 | #ifdef USE_SLANG |
David Lawrence Ramsey | 27be74c | 2007-12-18 16:47:06 +0000 | [diff] [blame] | 1506 | if (!ISSET(PRESERVE)) |
| 1507 | disable_flow_control(); |
David Lawrence Ramsey | f78bc85 | 2007-12-18 15:55:48 +0000 | [diff] [blame] | 1508 | |
| 1509 | tcgetattr(0, &newterm); |
| 1510 | newterm_set = TRUE; |
| 1511 | } else |
| 1512 | tcsetattr(0, TCSANOW, &newterm); |
| 1513 | #endif |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 1514 | } |
| 1515 | |
Benno Schulenberg | c661506 | 2016-08-16 10:49:55 +0200 | [diff] [blame] | 1516 | /* Ask ncurses for a keycode, or assign a default one. */ |
| 1517 | int get_keycode(const char *keyname, const int standard) |
| 1518 | { |
Benno Schulenberg | f2c72bf | 2016-11-17 17:32:28 +0100 | [diff] [blame] | 1519 | #ifdef HAVE_KEY_DEFINED |
Benno Schulenberg | c661506 | 2016-08-16 10:49:55 +0200 | [diff] [blame] | 1520 | const char *keyvalue = tigetstr(keyname); |
| 1521 | |
Benno Schulenberg | f2c72bf | 2016-11-17 17:32:28 +0100 | [diff] [blame] | 1522 | if (keyvalue != 0 && keyvalue != (char *)-1) |
Benno Schulenberg | c661506 | 2016-08-16 10:49:55 +0200 | [diff] [blame] | 1523 | return key_defined(keyvalue); |
Benno Schulenberg | f2c72bf | 2016-11-17 17:32:28 +0100 | [diff] [blame] | 1524 | else |
Benno Schulenberg | c661506 | 2016-08-16 10:49:55 +0200 | [diff] [blame] | 1525 | #endif |
Benno Schulenberg | f2c72bf | 2016-11-17 17:32:28 +0100 | [diff] [blame] | 1526 | return standard; |
| 1527 | } |
Benno Schulenberg | c661506 | 2016-08-16 10:49:55 +0200 | [diff] [blame] | 1528 | |
Benno Schulenberg | e0c4f9c | 2016-04-27 14:37:31 +0200 | [diff] [blame] | 1529 | /* Say that an unbound key was struck, and if possible which one. */ |
| 1530 | void unbound_key(int code) |
| 1531 | { |
Benno Schulenberg | cb10b2b | 2016-07-13 16:22:56 +0200 | [diff] [blame] | 1532 | if (!is_byte(code)) |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 1533 | statusline(ALERT, _("Unbound key")); |
Benno Schulenberg | e0c4f9c | 2016-04-27 14:37:31 +0200 | [diff] [blame] | 1534 | else if (meta_key) { |
Benno Schulenberg | b3fbb7c | 2016-07-14 22:01:55 +0200 | [diff] [blame] | 1535 | if (code == '[') |
| 1536 | statusline(ALERT, _("Unbindable key: M-[")); |
| 1537 | else |
| 1538 | statusline(ALERT, _("Unbound key: M-%c"), toupper(code)); |
Benno Schulenberg | e0c4f9c | 2016-04-27 14:37:31 +0200 | [diff] [blame] | 1539 | } else if (code < 0x20) |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 1540 | statusline(ALERT, _("Unbound key: ^%c"), code + 0x40); |
Benno Schulenberg | e0c4f9c | 2016-04-27 14:37:31 +0200 | [diff] [blame] | 1541 | else |
Benno Schulenberg | 2535f51 | 2016-04-30 17:31:43 +0200 | [diff] [blame] | 1542 | statusline(ALERT, _("Unbound key: %c"), code); |
Benno Schulenberg | e0c4f9c | 2016-04-27 14:37:31 +0200 | [diff] [blame] | 1543 | } |
| 1544 | |
Benno Schulenberg | ddd9c7a | 2016-06-24 22:45:41 +0200 | [diff] [blame] | 1545 | /* Read in a keystroke. Act on the keystroke if it is a shortcut or a toggle; |
| 1546 | * otherwise, insert it into the edit buffer. If allow_funcs is FALSE, don't |
| 1547 | * do anything with the keystroke -- just return it. */ |
Benno Schulenberg | 7e5324d | 2014-06-30 18:04:33 +0000 | [diff] [blame] | 1548 | int do_input(bool allow_funcs) |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1549 | { |
| 1550 | int input; |
Benno Schulenberg | ddd9c7a | 2016-06-24 22:45:41 +0200 | [diff] [blame] | 1551 | /* The keystroke we read in: a character or a shortcut. */ |
Benno Schulenberg | 067b0a3 | 2016-06-26 14:08:05 +0200 | [diff] [blame] | 1552 | static char *puddle = NULL; |
Benno Schulenberg | ddd9c7a | 2016-06-24 22:45:41 +0200 | [diff] [blame] | 1553 | /* The input buffer for actual characters. */ |
| 1554 | static size_t depth = 0; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1555 | /* The length of the input buffer. */ |
Benno Schulenberg | 3af22aa | 2014-06-13 12:28:33 +0000 | [diff] [blame] | 1556 | bool preserve = FALSE; |
Benno Schulenberg | ddd9c7a | 2016-06-24 22:45:41 +0200 | [diff] [blame] | 1557 | /* Whether to preserve the contents of the cutbuffer. */ |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1558 | const sc *s; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1559 | bool have_shortcut; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1560 | |
Benno Schulenberg | ddd9c7a | 2016-06-24 22:45:41 +0200 | [diff] [blame] | 1561 | /* Read in a keystroke. */ |
Benno Schulenberg | 7e5324d | 2014-06-30 18:04:33 +0000 | [diff] [blame] | 1562 | input = get_kbinput(edit); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1563 | |
Benno Schulenberg | 75d64e6 | 2015-05-28 13:02:29 +0000 | [diff] [blame] | 1564 | #ifndef NANO_TINY |
| 1565 | if (input == KEY_WINCH) |
| 1566 | return KEY_WINCH; |
| 1567 | #endif |
| 1568 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1569 | #ifndef DISABLE_MOUSE |
Benno Schulenberg | cb10b2b | 2016-07-13 16:22:56 +0200 | [diff] [blame] | 1570 | if (input == KEY_MOUSE) { |
Benno Schulenberg | 454563c | 2014-06-13 12:07:18 +0000 | [diff] [blame] | 1571 | /* We received a mouse click. */ |
Benno Schulenberg | 99a649d | 2014-06-16 12:30:35 +0000 | [diff] [blame] | 1572 | if (do_mouse() == 1) |
Benno Schulenberg | 454563c | 2014-06-13 12:07:18 +0000 | [diff] [blame] | 1573 | /* The click was on a shortcut -- read in the character |
| 1574 | * that it was converted into. */ |
Benno Schulenberg | 7e5324d | 2014-06-30 18:04:33 +0000 | [diff] [blame] | 1575 | input = get_kbinput(edit); |
Benno Schulenberg | 99a649d | 2014-06-16 12:30:35 +0000 | [diff] [blame] | 1576 | else |
| 1577 | /* The click was invalid or has been handled -- get out. */ |
Benno Schulenberg | fc35e20 | 2014-06-11 18:23:29 +0000 | [diff] [blame] | 1578 | return ERR; |
David Lawrence Ramsey | 8992d0a | 2006-05-10 12:48:47 +0000 | [diff] [blame] | 1579 | } |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1580 | #endif |
| 1581 | |
| 1582 | /* Check for a shortcut in the main list. */ |
Benno Schulenberg | 49816fe | 2014-07-01 10:41:10 +0000 | [diff] [blame] | 1583 | s = get_shortcut(&input); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1584 | |
| 1585 | /* If we got a shortcut from the main list, or a "universal" |
| 1586 | * edit window shortcut, set have_shortcut to TRUE. */ |
Chris Allegretta | 0018d8e | 2008-03-13 08:23:52 +0000 | [diff] [blame] | 1587 | have_shortcut = (s != NULL); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1588 | |
David Lawrence Ramsey | d6eb175 | 2006-05-25 21:39:25 +0000 | [diff] [blame] | 1589 | /* If we got a non-high-bit control key, a meta key sequence, or a |
| 1590 | * function key, and it's not a shortcut or toggle, throw it out. */ |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1591 | if (!have_shortcut) { |
Benno Schulenberg | cb10b2b | 2016-07-13 16:22:56 +0200 | [diff] [blame] | 1592 | if (is_ascii_cntrl_char(input) || meta_key || !is_byte(input)) { |
Benno Schulenberg | e0c4f9c | 2016-04-27 14:37:31 +0200 | [diff] [blame] | 1593 | unbound_key(input); |
David Lawrence Ramsey | 305d889 | 2006-05-24 19:48:03 +0000 | [diff] [blame] | 1594 | input = ERR; |
David Lawrence Ramsey | 8e341e1 | 2006-05-24 17:36:00 +0000 | [diff] [blame] | 1595 | } |
| 1596 | } |
| 1597 | |
Benno Schulenberg | e7f39bc | 2016-08-28 21:00:13 +0200 | [diff] [blame] | 1598 | if (!allow_funcs) |
| 1599 | return input; |
| 1600 | |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1601 | /* If the keystroke isn't a shortcut nor a toggle, it's a normal text |
| 1602 | * character: add the character to the input buffer -- or display a |
| 1603 | * warning when we're in view mode. */ |
| 1604 | if (input != ERR && !have_shortcut) { |
| 1605 | if (ISSET(VIEW_MODE)) |
| 1606 | print_view_warning(); |
| 1607 | else { |
| 1608 | /* Store the byte, and leave room for a terminating zero. */ |
| 1609 | puddle = charealloc(puddle, depth + 2); |
| 1610 | puddle[depth++] = (char)input; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1611 | } |
Benno Schulenberg | 96fbdfb | 2016-08-30 10:41:28 +0200 | [diff] [blame] | 1612 | #ifndef NANO_TINY |
| 1613 | if (openfile->mark_set && openfile->kind_of_mark == SOFTMARK) { |
| 1614 | openfile->mark_set = FALSE; |
| 1615 | refresh_needed = TRUE; |
| 1616 | } |
| 1617 | #endif |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1618 | } |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1619 | |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1620 | /* If we got a shortcut or toggle, or if there aren't any other |
| 1621 | * characters waiting after the one we read in, we need to output |
| 1622 | * all available characters in the input puddle. Note that this |
| 1623 | * puddle will be empty if we're in view mode. */ |
| 1624 | if (have_shortcut || get_key_buffer_len() == 0) { |
David Lawrence Ramsey | 9133cbe | 2006-05-22 15:45:24 +0000 | [diff] [blame] | 1625 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1626 | /* If we got a shortcut or toggle, and it's not the shortcut |
| 1627 | * for verbatim input, turn off prepending of wrapped text. */ |
| 1628 | if (have_shortcut && s->scfunc != do_verbatim_input) |
| 1629 | wrap_reset(); |
David Lawrence Ramsey | 9133cbe | 2006-05-22 15:45:24 +0000 | [diff] [blame] | 1630 | #endif |
David Lawrence Ramsey | ef0d5a7 | 2006-05-22 02:08:49 +0000 | [diff] [blame] | 1631 | |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1632 | if (puddle != NULL) { |
| 1633 | /* Insert all bytes in the input buffer into the edit buffer |
| 1634 | * at once, filtering out any low control codes. */ |
| 1635 | puddle[depth] = '\0'; |
| 1636 | do_output(puddle, depth, FALSE); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1637 | |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1638 | /* Empty the input buffer. */ |
| 1639 | free(puddle); |
| 1640 | puddle = NULL; |
| 1641 | depth = 0; |
| 1642 | } |
| 1643 | } |
| 1644 | |
Benno Schulenberg | 68a0314 | 2016-12-07 13:10:40 +0100 | [diff] [blame] | 1645 | if (!have_shortcut) |
| 1646 | pletion_line = NULL; |
| 1647 | else { |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1648 | const subnfunc *f = sctofunc(s); |
| 1649 | |
| 1650 | if (ISSET(VIEW_MODE) && f && !f->viewok) { |
| 1651 | print_view_warning(); |
| 1652 | return ERR; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1653 | } |
| 1654 | |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1655 | /* If the function associated with this shortcut is |
| 1656 | * cutting or copying text, remember this. */ |
| 1657 | if (s->scfunc == do_cut_text_void |
David Lawrence Ramsey | c87e0c0 | 2006-04-25 02:23:28 +0000 | [diff] [blame] | 1658 | #ifndef NANO_TINY |
Benno Schulenberg | 95e77a9 | 2014-06-30 20:39:27 +0000 | [diff] [blame] | 1659 | || s->scfunc == do_copy_text || s->scfunc == do_cut_till_eof |
David Lawrence Ramsey | c87e0c0 | 2006-04-25 02:23:28 +0000 | [diff] [blame] | 1660 | #endif |
Benno Schulenberg | 8cc8b08 | 2014-06-11 18:34:35 +0000 | [diff] [blame] | 1661 | ) |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1662 | preserve = TRUE; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1663 | |
Benno Schulenberg | 68a0314 | 2016-12-07 13:10:40 +0100 | [diff] [blame] | 1664 | #ifdef ENABLE_WORDCOMPLETION |
Sumedh Pendurkar | dca4ab5 | 2016-12-07 09:43:47 +0530 | [diff] [blame] | 1665 | if (s->scfunc != complete_a_word) |
| 1666 | pletion_line = NULL; |
Benno Schulenberg | 68a0314 | 2016-12-07 13:10:40 +0100 | [diff] [blame] | 1667 | #endif |
| 1668 | #ifndef NANO_TINY |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1669 | if (s->scfunc == do_toggle_void) { |
| 1670 | do_toggle(s->toggle); |
| 1671 | if (s->toggle != CUT_TO_END) |
| 1672 | preserve = TRUE; |
| 1673 | } else |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1674 | #endif |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1675 | { |
Benno Schulenberg | 382c9d7 | 2016-04-24 11:28:28 +0200 | [diff] [blame] | 1676 | #ifndef NANO_TINY |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1677 | /* If Shifted movement occurs, set the mark. */ |
| 1678 | if (shift_held && !openfile->mark_set) { |
| 1679 | openfile->mark_set = TRUE; |
| 1680 | openfile->mark_begin = openfile->current; |
| 1681 | openfile->mark_begin_x = openfile->current_x; |
| 1682 | openfile->kind_of_mark = SOFTMARK; |
| 1683 | } |
Benno Schulenberg | 382c9d7 | 2016-04-24 11:28:28 +0200 | [diff] [blame] | 1684 | #endif |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1685 | /* Execute the function of the shortcut. */ |
| 1686 | s->scfunc(); |
Benno Schulenberg | 382c9d7 | 2016-04-24 11:28:28 +0200 | [diff] [blame] | 1687 | #ifndef NANO_TINY |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1688 | /* If Shiftless movement occurred, discard a soft mark. */ |
| 1689 | if (openfile->mark_set && !shift_held && |
Benno Schulenberg | 382c9d7 | 2016-04-24 11:28:28 +0200 | [diff] [blame] | 1690 | openfile->kind_of_mark == SOFTMARK) { |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1691 | openfile->mark_set = FALSE; |
| 1692 | openfile->mark_begin = NULL; |
| 1693 | refresh_needed = TRUE; |
| 1694 | } |
Benno Schulenberg | 382c9d7 | 2016-04-24 11:28:28 +0200 | [diff] [blame] | 1695 | #endif |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 1696 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1697 | if (f && !f->viewok) |
Benno Schulenberg | 9ec546d | 2017-02-13 19:11:04 +0100 | [diff] [blame] | 1698 | check_the_multis(openfile->current); |
Chris Allegretta | 8b6f6fc | 2009-11-22 21:35:56 +0000 | [diff] [blame] | 1699 | #endif |
Benno Schulenberg | 2789bb0 | 2016-10-21 13:52:40 +0200 | [diff] [blame] | 1700 | if (!refresh_needed && (s->scfunc == do_delete || s->scfunc == do_backspace)) |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1701 | update_line(openfile->current, openfile->current_x); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1702 | } |
Benno Schulenberg | 5ad26f8 | 2016-08-28 21:18:34 +0200 | [diff] [blame] | 1703 | } |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1704 | |
Benno Schulenberg | 3af22aa | 2014-06-13 12:28:33 +0000 | [diff] [blame] | 1705 | /* If we aren't cutting or copying text, and the key wasn't a toggle, |
| 1706 | * blow away the text in the cutbuffer upon the next cutting action. */ |
| 1707 | if (!preserve) |
David Lawrence Ramsey | 41b8972 | 2006-05-21 20:03:43 +0000 | [diff] [blame] | 1708 | cutbuffer_reset(); |
| 1709 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1710 | return input; |
| 1711 | } |
| 1712 | |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 1713 | void xon_complaint(void) |
| 1714 | { |
| 1715 | statusbar(_("XON ignored, mumble mumble")); |
| 1716 | } |
| 1717 | |
| 1718 | void xoff_complaint(void) |
| 1719 | { |
| 1720 | statusbar(_("XOFF ignored, mumble mumble")); |
| 1721 | } |
| 1722 | |
| 1723 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1724 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 1725 | /* Handle a mouse click on the edit window or the shortcut list. */ |
David Lawrence Ramsey | 3a5eaeb | 2007-05-20 23:41:56 +0000 | [diff] [blame] | 1726 | int do_mouse(void) |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1727 | { |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1728 | int mouse_col, mouse_row; |
| 1729 | int retval = get_mouseinput(&mouse_col, &mouse_row, TRUE); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1730 | |
Benno Schulenberg | 1317679 | 2017-01-03 15:04:22 +0100 | [diff] [blame] | 1731 | /* If the click is wrong or already handled, we're done. */ |
Benno Schulenberg | 1d06455 | 2014-06-13 12:19:44 +0000 | [diff] [blame] | 1732 | if (retval != 0) |
Benno Schulenberg | 1d06455 | 2014-06-13 12:19:44 +0000 | [diff] [blame] | 1733 | return retval; |
| 1734 | |
Benno Schulenberg | 1317679 | 2017-01-03 15:04:22 +0100 | [diff] [blame] | 1735 | /* If the click was in the edit window, put the cursor in that spot. */ |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1736 | if (wmouse_trafo(edit, &mouse_row, &mouse_col, FALSE)) { |
| 1737 | bool sameline = (mouse_row == openfile->current_y); |
| 1738 | /* Whether the click was on the row where the cursor is. */ |
Chris Allegretta | d47d8cd | 2009-01-25 07:25:17 +0000 | [diff] [blame] | 1739 | filestruct *current_save = openfile->current; |
Benno Schulenberg | 8611e47 | 2014-04-04 16:06:27 +0000 | [diff] [blame] | 1740 | #ifndef NANO_TINY |
David Lawrence Ramsey | ebc38fd | 2007-06-28 16:00:50 +0000 | [diff] [blame] | 1741 | size_t current_x_save = openfile->current_x; |
Benno Schulenberg | 8611e47 | 2014-04-04 16:06:27 +0000 | [diff] [blame] | 1742 | #endif |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1743 | |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1744 | #ifdef DEBUG |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1745 | fprintf(stderr, "mouse_row = %d, current_y = %ld\n", mouse_row, (long)openfile->current_y); |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1746 | #endif |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1747 | |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 1748 | #ifndef NANO_TINY |
Benno Schulenberg | a65ef42 | 2014-03-17 21:36:37 +0000 | [diff] [blame] | 1749 | if (ISSET(SOFTWRAP)) { |
Benno Schulenberg | 1317679 | 2017-01-03 15:04:22 +0100 | [diff] [blame] | 1750 | ssize_t current_row = 0; |
Benno Schulenberg | 30591c5 | 2016-12-10 21:18:10 +0100 | [diff] [blame] | 1751 | |
| 1752 | openfile->current = openfile->edittop; |
| 1753 | |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1754 | while (openfile->current->next != NULL && current_row < mouse_row) { |
Benno Schulenberg | 1317679 | 2017-01-03 15:04:22 +0100 | [diff] [blame] | 1755 | current_row += strlenpt(openfile->current->data) / editwincols + 1; |
Benno Schulenberg | 30591c5 | 2016-12-10 21:18:10 +0100 | [diff] [blame] | 1756 | openfile->current = openfile->current->next; |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1757 | } |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1758 | |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1759 | if (current_row > mouse_row) { |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1760 | openfile->current = openfile->current->prev; |
Benno Schulenberg | 1317679 | 2017-01-03 15:04:22 +0100 | [diff] [blame] | 1761 | current_row -= strlenpt(openfile->current->data) / editwincols + 1; |
Benno Schulenberg | e9fde7d | 2016-12-10 19:38:03 +0100 | [diff] [blame] | 1762 | openfile->current_x = actual_x(openfile->current->data, |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1763 | ((mouse_row - current_row) * editwincols) + mouse_col); |
Benno Schulenberg | 30591c5 | 2016-12-10 21:18:10 +0100 | [diff] [blame] | 1764 | } else |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1765 | openfile->current_x = actual_x(openfile->current->data, mouse_col); |
Benno Schulenberg | 8d692a8 | 2017-01-03 15:26:05 +0100 | [diff] [blame] | 1766 | |
| 1767 | openfile->current_y = current_row; |
| 1768 | ensure_line_is_visible(); |
| 1769 | refresh_needed = TRUE; |
Benno Schulenberg | 1930e81 | 2014-06-20 18:06:22 +0000 | [diff] [blame] | 1770 | } else |
| 1771 | #endif /* NANO_TINY */ |
| 1772 | { |
David Lawrence Ramsey | d7fbc70 | 2017-01-02 13:41:27 -0600 | [diff] [blame] | 1773 | ssize_t current_row = openfile->current_y; |
| 1774 | |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1775 | /* Move to where the click occurred. */ |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1776 | while (current_row < mouse_row && openfile->current->next != NULL) { |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1777 | openfile->current = openfile->current->next; |
Benno Schulenberg | 1317679 | 2017-01-03 15:04:22 +0100 | [diff] [blame] | 1778 | current_row++; |
| 1779 | } |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1780 | while (current_row > mouse_row && openfile->current->prev != NULL) { |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1781 | openfile->current = openfile->current->prev; |
Benno Schulenberg | 1317679 | 2017-01-03 15:04:22 +0100 | [diff] [blame] | 1782 | current_row--; |
| 1783 | } |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1784 | |
| 1785 | openfile->current_x = actual_x(openfile->current->data, |
David Lawrence Ramsey | 3e22240 | 2017-02-01 21:10:49 -0600 | [diff] [blame] | 1786 | get_page_start(xplustabs()) + mouse_col); |
Chris Allegretta | f55ada7 | 2009-11-13 20:38:32 +0000 | [diff] [blame] | 1787 | } |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1788 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1789 | #ifndef NANO_TINY |
Benno Schulenberg | 1317679 | 2017-01-03 15:04:22 +0100 | [diff] [blame] | 1790 | /* Clicking where the cursor is toggles the mark, as does clicking |
| 1791 | * beyond the line length with the cursor at the end of the line. */ |
David Lawrence Ramsey | ebc38fd | 2007-06-28 16:00:50 +0000 | [diff] [blame] | 1792 | if (sameline && openfile->current_x == current_x_save) |
| 1793 | do_mark(); |
Benno Schulenberg | 99a649d | 2014-06-16 12:30:35 +0000 | [diff] [blame] | 1794 | else |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1795 | #endif |
Benno Schulenberg | 99a649d | 2014-06-16 12:30:35 +0000 | [diff] [blame] | 1796 | /* The cursor moved; clean the cutbuffer on the next cut. */ |
| 1797 | cutbuffer_reset(); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1798 | |
Benno Schulenberg | 8d692a8 | 2017-01-03 15:26:05 +0100 | [diff] [blame] | 1799 | if (!ISSET(SOFTWRAP)) |
| 1800 | edit_redraw(current_save); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1801 | } |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1802 | |
Benno Schulenberg | 99a649d | 2014-06-16 12:30:35 +0000 | [diff] [blame] | 1803 | /* No more handling is needed. */ |
Benno Schulenberg | 1d06455 | 2014-06-13 12:19:44 +0000 | [diff] [blame] | 1804 | return 2; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1805 | } |
| 1806 | #endif /* !DISABLE_MOUSE */ |
| 1807 | |
David Lawrence Ramsey | ef0d5a7 | 2006-05-22 02:08:49 +0000 | [diff] [blame] | 1808 | /* The user typed output_len multibyte characters. Add them to the edit |
David Lawrence Ramsey | 6fb6689 | 2006-05-27 17:39:19 +0000 | [diff] [blame] | 1809 | * buffer, filtering out all ASCII control characters if allow_cntrls is |
David Lawrence Ramsey | efec641 | 2005-03-17 03:52:08 +0000 | [diff] [blame] | 1810 | * TRUE. */ |
| 1811 | void do_output(char *output, size_t output_len, bool allow_cntrls) |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1812 | { |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 1813 | size_t current_len, i = 0; |
| 1814 | #ifndef NANO_TINY |
David Lawrence Ramsey | 83ff644 | 2017-01-19 19:58:37 -0600 | [diff] [blame] | 1815 | size_t orig_rows = 0; |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 1816 | #endif |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1817 | char *char_buf = charalloc(mb_cur_max()); |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1818 | int char_len; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1819 | |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 1820 | current_len = strlen(openfile->current->data); |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 1821 | |
| 1822 | #ifndef NANO_TINY |
Chris Allegretta | a8bc492 | 2009-12-12 22:21:20 +0000 | [diff] [blame] | 1823 | if (ISSET(SOFTWRAP)) |
David Lawrence Ramsey | 83ff644 | 2017-01-19 19:58:37 -0600 | [diff] [blame] | 1824 | orig_rows = strlenpt(openfile->current->data) / editwincols; |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 1825 | #endif |
David Lawrence Ramsey | 31b159c | 2005-05-26 05:17:13 +0000 | [diff] [blame] | 1826 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1827 | while (i < output_len) { |
Benno Schulenberg | 908663e | 2016-12-27 12:20:20 +0100 | [diff] [blame] | 1828 | /* Encode an embedded NUL byte as 0x0A. */ |
| 1829 | if (output[i] == '\0') |
| 1830 | output[i] = '\n'; |
David Lawrence Ramsey | abc9423 | 2004-12-08 23:24:31 +0000 | [diff] [blame] | 1831 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 1832 | /* Get the next multibyte character. */ |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1833 | char_len = parse_mbchar(output + i, char_buf, NULL); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1834 | |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1835 | i += char_len; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1836 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 1837 | /* If controls are not allowed, ignore an ASCII control character. */ |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1838 | if (!allow_cntrls && is_ascii_cntrl_char(*(output + i - char_len))) |
David Lawrence Ramsey | efec641 | 2005-03-17 03:52:08 +0000 | [diff] [blame] | 1839 | continue; |
| 1840 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 1841 | /* If we're adding to the magicline, create a new magicline. */ |
Benno Schulenberg | ae64877 | 2016-10-21 15:59:16 +0200 | [diff] [blame] | 1842 | if (!ISSET(NO_NEWLINES) && openfile->filebot == openfile->current) { |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1843 | new_magicline(); |
Benno Schulenberg | ae64877 | 2016-10-21 15:59:16 +0200 | [diff] [blame] | 1844 | if (margin > 0) |
| 1845 | refresh_needed = TRUE; |
| 1846 | } |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1847 | |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 1848 | assert(openfile->current_x <= current_len); |
David Lawrence Ramsey | 3e81914 | 2004-12-31 04:10:28 +0000 | [diff] [blame] | 1849 | |
Benno Schulenberg | 95f417f | 2016-06-14 11:06:04 +0200 | [diff] [blame] | 1850 | /* Make room for the new character and copy it into the line. */ |
| 1851 | openfile->current->data = charealloc(openfile->current->data, |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1852 | current_len + char_len + 1); |
| 1853 | charmove(openfile->current->data + openfile->current_x + char_len, |
Benno Schulenberg | f2da466 | 2015-12-04 21:11:10 +0000 | [diff] [blame] | 1854 | openfile->current->data + openfile->current_x, |
Benno Schulenberg | 7165bd5 | 2016-06-14 14:39:56 +0200 | [diff] [blame] | 1855 | current_len - openfile->current_x + 1); |
David Lawrence Ramsey | 4eeff48 | 2005-11-05 04:59:00 +0000 | [diff] [blame] | 1856 | strncpy(openfile->current->data + openfile->current_x, char_buf, |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1857 | char_len); |
| 1858 | current_len += char_len; |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 1859 | openfile->totsize++; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1860 | set_modified(); |
| 1861 | |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1862 | #ifndef NANO_TINY |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1863 | add_undo(ADD); |
Chris Allegretta | 07fcc4c | 2008-07-10 20:13:04 +0000 | [diff] [blame] | 1864 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1865 | /* Note that current_x has not yet been incremented. */ |
Benno Schulenberg | f2da466 | 2015-12-04 21:11:10 +0000 | [diff] [blame] | 1866 | if (openfile->mark_set && openfile->current == openfile->mark_begin && |
| 1867 | openfile->current_x < openfile->mark_begin_x) |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1868 | openfile->mark_begin_x += char_len; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1869 | #endif |
| 1870 | |
Benno Schulenberg | 79a4bf8 | 2016-12-19 19:58:15 +0100 | [diff] [blame] | 1871 | openfile->current_x += char_len; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1872 | |
Benno Schulenberg | 6081546 | 2014-05-15 20:00:46 +0000 | [diff] [blame] | 1873 | #ifndef NANO_TINY |
| 1874 | update_undo(ADD); |
| 1875 | #endif |
| 1876 | |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1877 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | 0f3e303 | 2016-12-02 17:37:11 +0100 | [diff] [blame] | 1878 | /* If text gets wrapped, the edit window needs a refresh. */ |
| 1879 | if (!ISSET(NO_WRAP) && do_wrap(openfile->current)) |
| 1880 | refresh_needed = TRUE; |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1881 | #endif |
Benno Schulenberg | 438cb11 | 2015-11-25 09:10:02 +0000 | [diff] [blame] | 1882 | } |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1883 | |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 1884 | #ifndef NANO_TINY |
Benno Schulenberg | 43f35fc | 2016-10-18 13:03:01 +0200 | [diff] [blame] | 1885 | ensure_line_is_visible(); |
| 1886 | |
Benno Schulenberg | 7598b77 | 2016-11-12 15:57:42 +0100 | [diff] [blame] | 1887 | /* If the number of screen rows that a softwrapped line occupies |
| 1888 | * has changed, we need a full refresh. */ |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 1889 | if (ISSET(SOFTWRAP) && refresh_needed == FALSE) |
David Lawrence Ramsey | 83ff644 | 2017-01-19 19:58:37 -0600 | [diff] [blame] | 1890 | if ((strlenpt(openfile->current->data) / editwincols) != orig_rows) |
Benno Schulenberg | 53f4a9f | 2016-04-25 21:14:18 +0200 | [diff] [blame] | 1891 | refresh_needed = TRUE; |
Benno Schulenberg | 1102aaa | 2014-06-09 20:26:54 +0000 | [diff] [blame] | 1892 | #endif |
Chris Allegretta | a8bc492 | 2009-12-12 22:21:20 +0000 | [diff] [blame] | 1893 | |
David Lawrence Ramsey | b54155c | 2005-01-12 03:25:57 +0000 | [diff] [blame] | 1894 | free(char_buf); |
David Lawrence Ramsey | 78ea5e4 | 2004-12-12 19:04:56 +0000 | [diff] [blame] | 1895 | |
David Lawrence Ramsey | 157ce91 | 2005-07-16 23:36:10 +0000 | [diff] [blame] | 1896 | openfile->placewewant = xplustabs(); |
| 1897 | |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 1898 | #ifndef DISABLE_COLOR |
Benno Schulenberg | 9ec546d | 2017-02-13 19:11:04 +0100 | [diff] [blame] | 1899 | check_the_multis(openfile->current); |
Chris Allegretta | 019998a | 2009-01-26 07:55:01 +0000 | [diff] [blame] | 1900 | #endif |
Benno Schulenberg | 37712fb | 2016-01-03 16:21:04 +0000 | [diff] [blame] | 1901 | |
Benno Schulenberg | 2789bb0 | 2016-10-21 13:52:40 +0200 | [diff] [blame] | 1902 | if (!refresh_needed) |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 1903 | update_line(openfile->current, openfile->current_x); |
David Lawrence Ramsey | 7483571 | 2004-12-04 17:41:52 +0000 | [diff] [blame] | 1904 | } |
| 1905 | |
David Lawrence Ramsey | a27bd65 | 2004-08-17 05:23:38 +0000 | [diff] [blame] | 1906 | int main(int argc, char **argv) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1907 | { |
| 1908 | int optchr; |
Benno Schulenberg | 3060ce3 | 2014-06-20 19:10:24 +0000 | [diff] [blame] | 1909 | ssize_t startline = 0, startcol = 0; |
| 1910 | /* Target line and column when specified on the command line. */ |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame] | 1911 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 1912 | bool fill_used = FALSE; |
Benno Schulenberg | 1f027a8 | 2015-11-21 19:44:27 +0000 | [diff] [blame] | 1913 | /* Was the fill option used on the command line? */ |
Benno Schulenberg | 04a230f | 2016-05-15 15:18:47 +0200 | [diff] [blame] | 1914 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | 1f027a8 | 2015-11-21 19:44:27 +0000 | [diff] [blame] | 1915 | bool forced_wrapping = FALSE; |
| 1916 | /* Should long lines be automatically hard wrapped? */ |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame] | 1917 | #endif |
Benno Schulenberg | 04a230f | 2016-05-15 15:18:47 +0200 | [diff] [blame] | 1918 | #endif |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 1919 | #ifndef DISABLE_MULTIBUFFER |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 1920 | bool old_multibuffer; |
| 1921 | /* The old value of the multibuffer option, restored after we |
| 1922 | * load all files on the command line. */ |
| 1923 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1924 | #ifdef HAVE_GETOPT_LONG |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1925 | const struct option long_options[] = { |
David Lawrence Ramsey | 4d72de7 | 2006-04-12 15:27:40 +0000 | [diff] [blame] | 1926 | {"boldtext", 0, NULL, 'D'}, |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 1927 | #ifndef DISABLE_MULTIBUFFER |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1928 | {"multibuffer", 0, NULL, 'F'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1929 | #endif |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 1930 | #ifndef DISABLE_NANORC |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1931 | {"ignorercfiles", 0, NULL, 'I'}, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1932 | #endif |
David Lawrence Ramsey | 057edf7 | 2005-08-10 21:22:15 +0000 | [diff] [blame] | 1933 | {"rebindkeypad", 0, NULL, 'K'}, |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 1934 | {"nonewlines", 0, NULL, 'L'}, |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1935 | {"morespace", 0, NULL, 'O'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1936 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1937 | {"quotestr", 1, NULL, 'Q'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1938 | #endif |
David Lawrence Ramsey | 84635cd | 2005-06-17 22:53:41 +0000 | [diff] [blame] | 1939 | {"restricted", 0, NULL, 'R'}, |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1940 | {"tabsize", 1, NULL, 'T'}, |
| 1941 | {"version", 0, NULL, 'V'}, |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 1942 | #ifndef DISABLE_COLOR |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1943 | {"syntax", 1, NULL, 'Y'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1944 | #endif |
Benno Schulenberg | 7952615 | 2015-07-15 19:40:37 +0000 | [diff] [blame] | 1945 | {"constantshow", 0, NULL, 'c'}, |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1946 | {"rebinddelete", 0, NULL, 'd'}, |
Benno Schulenberg | 10c9093 | 2016-10-23 12:56:47 +0200 | [diff] [blame] | 1947 | #ifndef DISABLE_BROWSER |
| 1948 | {"showcursor", 0, NULL, 'g'}, |
| 1949 | #endif |
Benno Schulenberg | 4499520 | 2015-06-20 18:48:43 +0000 | [diff] [blame] | 1950 | {"help", 0, NULL, 'h'}, |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 1951 | #ifdef ENABLE_LINENUMBERS |
| 1952 | {"linenumbers", 0, NULL, 'l'}, |
| 1953 | #endif |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 1954 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1955 | {"mouse", 0, NULL, 'm'}, |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1956 | #endif |
Benno Schulenberg | db7064b | 2014-04-08 18:59:30 +0000 | [diff] [blame] | 1957 | {"noread", 0, NULL, 'n'}, |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1958 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1959 | {"operatingdir", 1, NULL, 'o'}, |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1960 | #endif |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1961 | {"preserve", 0, NULL, 'p'}, |
Chris Allegretta | a30eb78 | 2009-02-09 04:03:20 +0000 | [diff] [blame] | 1962 | {"quiet", 0, NULL, 'q'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1963 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1964 | {"fill", 1, NULL, 'r'}, |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1965 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1966 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1967 | {"speller", 1, NULL, 's'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 1968 | #endif |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1969 | {"tempfile", 0, NULL, 't'}, |
| 1970 | {"view", 0, NULL, 'v'}, |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1971 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1972 | {"nowrap", 0, NULL, 'w'}, |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1973 | #endif |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1974 | {"nohelp", 0, NULL, 'x'}, |
| 1975 | {"suspend", 0, NULL, 'z'}, |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 1976 | #ifndef NANO_TINY |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1977 | {"smarthome", 0, NULL, 'A'}, |
| 1978 | {"backup", 0, NULL, 'B'}, |
David Lawrence Ramsey | 6f143c8 | 2005-06-15 23:20:56 +0000 | [diff] [blame] | 1979 | {"backupdir", 1, NULL, 'C'}, |
| 1980 | {"tabstospaces", 0, NULL, 'E'}, |
Chris Allegretta | bf88d27 | 2013-01-01 03:24:39 +0000 | [diff] [blame] | 1981 | {"locking", 0, NULL, 'G'}, |
David Lawrence Ramsey | 18a89a6 | 2005-06-17 17:37:46 +0000 | [diff] [blame] | 1982 | {"historylog", 0, NULL, 'H'}, |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1983 | {"noconvert", 0, NULL, 'N'}, |
Benno Schulenberg | cadb4f3 | 2015-07-15 19:50:55 +0000 | [diff] [blame] | 1984 | {"poslog", 0, NULL, 'P'}, /* deprecated form, remove in 2018 */ |
| 1985 | {"positionlog", 0, NULL, 'P'}, |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1986 | {"smooth", 0, NULL, 'S'}, |
David Lawrence Ramsey | e29111f | 2005-06-17 19:06:25 +0000 | [diff] [blame] | 1987 | {"quickblank", 0, NULL, 'U'}, |
David Lawrence Ramsey | 4f03daf | 2005-08-10 22:12:28 +0000 | [diff] [blame] | 1988 | {"wordbounds", 0, NULL, 'W'}, |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 1989 | {"wordchars", 1, NULL, 'X'}, |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1990 | {"autoindent", 0, NULL, 'i'}, |
| 1991 | {"cut", 0, NULL, 'k'}, |
Benno Schulenberg | eac0446 | 2015-08-09 16:31:01 +0000 | [diff] [blame] | 1992 | {"unix", 0, NULL, 'u'}, |
Chris Allegretta | 05417a2 | 2009-08-17 07:52:10 +0000 | [diff] [blame] | 1993 | {"softwrap", 0, NULL, '$'}, |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 1994 | #endif |
David Lawrence Ramsey | fe60524 | 2005-03-26 22:49:08 +0000 | [diff] [blame] | 1995 | {NULL, 0, NULL, 0} |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1996 | }; |
| 1997 | #endif |
| 1998 | |
Benno Schulenberg | aa09abe | 2016-06-12 11:13:45 +0200 | [diff] [blame] | 1999 | /* Back up the terminal settings so that they can be restored. */ |
| 2000 | tcgetattr(0, &oldterm); |
| 2001 | |
David Lawrence Ramsey | 7eb30a8 | 2005-07-17 02:40:07 +0000 | [diff] [blame] | 2002 | #ifdef ENABLE_UTF8 |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2003 | { |
David Lawrence Ramsey | a78b435 | 2007-05-25 14:39:40 +0000 | [diff] [blame] | 2004 | /* If the locale set exists and uses UTF-8, we should use |
| 2005 | * UTF-8. */ |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2006 | char *locale = setlocale(LC_ALL, ""); |
| 2007 | |
David Lawrence Ramsey | a78b435 | 2007-05-25 14:39:40 +0000 | [diff] [blame] | 2008 | if (locale != NULL && (strcmp(nl_langinfo(CODESET), |
| 2009 | "UTF-8") == 0)) { |
David Lawrence Ramsey | d15de73 | 2005-01-28 19:37:23 +0000 | [diff] [blame] | 2010 | #ifdef USE_SLANG |
David Lawrence Ramsey | 4ac129c | 2006-07-19 19:40:54 +0000 | [diff] [blame] | 2011 | SLutf8_enable(1); |
David Lawrence Ramsey | d15de73 | 2005-01-28 19:37:23 +0000 | [diff] [blame] | 2012 | #endif |
David Lawrence Ramsey | 4d72de7 | 2006-04-12 15:27:40 +0000 | [diff] [blame] | 2013 | utf8_init(); |
David Lawrence Ramsey | 7a4aaa5 | 2005-06-16 02:09:57 +0000 | [diff] [blame] | 2014 | } |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2015 | } |
| 2016 | #else |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2017 | setlocale(LC_ALL, ""); |
David Lawrence Ramsey | fc69321 | 2004-12-23 17:43:27 +0000 | [diff] [blame] | 2018 | #endif |
| 2019 | |
David Lawrence Ramsey | ad1fd0d | 2004-07-27 15:46:58 +0000 | [diff] [blame] | 2020 | #ifdef ENABLE_NLS |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2021 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 2022 | textdomain(PACKAGE); |
| 2023 | #endif |
| 2024 | |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 2025 | #if defined(DISABLE_NANORC) && defined(DISABLE_ROOTWRAPPING) |
Benno Schulenberg | 6f6ee84 | 2014-04-07 20:05:05 +0000 | [diff] [blame] | 2026 | /* If we don't have rcfile support, --disable-wrapping-as-root is |
| 2027 | * used, and we're root, turn wrapping off. */ |
David Lawrence Ramsey | a619ae6 | 2004-03-04 06:33:52 +0000 | [diff] [blame] | 2028 | if (geteuid() == NANO_ROOT_UID) |
David Lawrence Ramsey | dc60b72 | 2002-10-25 16:08:53 +0000 | [diff] [blame] | 2029 | SET(NO_WRAP); |
| 2030 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2031 | |
Benno Schulenberg | eb5968f | 2016-11-17 18:05:09 +0100 | [diff] [blame] | 2032 | /* If the executable's name starts with 'r', activate restricted mode. */ |
| 2033 | if (*(tail(argv[0])) == 'r') |
| 2034 | SET(RESTRICTED); |
| 2035 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2036 | while ((optchr = |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2037 | #ifdef HAVE_GETOPT_LONG |
David Lawrence Ramsey | 068edf4 | 2005-06-12 16:13:44 +0000 | [diff] [blame] | 2038 | getopt_long(argc, argv, |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 2039 | "ABC:DEFGHIKLNOPQ:RST:UVWX:Y:abcdefghijklmno:pqr:s:tuvwxz$", |
David Lawrence Ramsey | 068edf4 | 2005-06-12 16:13:44 +0000 | [diff] [blame] | 2040 | long_options, NULL) |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2041 | #else |
David Lawrence Ramsey | 068edf4 | 2005-06-12 16:13:44 +0000 | [diff] [blame] | 2042 | getopt(argc, argv, |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 2043 | "ABC:DEFGHIKLNOPQ:RST:UVWX:Y:abcdefghijklmno:pqr:s:tuvwxz$") |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2044 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2045 | ) != -1) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2046 | switch (optchr) { |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2047 | case 'a': |
| 2048 | case 'b': |
| 2049 | case 'e': |
| 2050 | case 'f': |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2051 | case 'j': |
| 2052 | /* Pico compatibility flags. */ |
| 2053 | break; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2054 | #ifndef NANO_TINY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2055 | case 'A': |
| 2056 | SET(SMART_HOME); |
| 2057 | break; |
| 2058 | case 'B': |
| 2059 | SET(BACKUP_FILE); |
| 2060 | break; |
David Lawrence Ramsey | 6f143c8 | 2005-06-15 23:20:56 +0000 | [diff] [blame] | 2061 | case 'C': |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2062 | backup_dir = mallocstrcpy(backup_dir, optarg); |
| 2063 | break; |
David Lawrence Ramsey | 4d72de7 | 2006-04-12 15:27:40 +0000 | [diff] [blame] | 2064 | #endif |
| 2065 | case 'D': |
| 2066 | SET(BOLD_TEXT); |
| 2067 | break; |
| 2068 | #ifndef NANO_TINY |
David Lawrence Ramsey | 6f143c8 | 2005-06-15 23:20:56 +0000 | [diff] [blame] | 2069 | case 'E': |
| 2070 | SET(TABS_TO_SPACES); |
| 2071 | break; |
Chris Allegretta | 7004c28 | 2001-09-22 00:42:10 +0000 | [diff] [blame] | 2072 | #endif |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 2073 | #ifndef DISABLE_MULTIBUFFER |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2074 | case 'F': |
| 2075 | SET(MULTIBUFFER); |
| 2076 | break; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 2077 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2078 | #ifndef NANO_TINY |
Chris Allegretta | bf88d27 | 2013-01-01 03:24:39 +0000 | [diff] [blame] | 2079 | case 'G': |
| 2080 | SET(LOCKING); |
| 2081 | break; |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2082 | #endif |
| 2083 | #ifndef DISABLE_HISTORIES |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2084 | case 'H': |
| 2085 | SET(HISTORYLOG); |
| 2086 | break; |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 2087 | #endif |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2088 | #ifndef DISABLE_NANORC |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2089 | case 'I': |
David Lawrence Ramsey | 18dae62 | 2006-04-24 18:54:29 +0000 | [diff] [blame] | 2090 | no_rcfiles = TRUE; |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2091 | break; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2092 | #endif |
David Lawrence Ramsey | 057edf7 | 2005-08-10 21:22:15 +0000 | [diff] [blame] | 2093 | case 'K': |
| 2094 | SET(REBIND_KEYPAD); |
| 2095 | break; |
David Lawrence Ramsey | a0168ca | 2005-11-05 17:35:44 +0000 | [diff] [blame] | 2096 | case 'L': |
| 2097 | SET(NO_NEWLINES); |
| 2098 | break; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2099 | #ifndef NANO_TINY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2100 | case 'N': |
| 2101 | SET(NO_CONVERT); |
| 2102 | break; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2103 | #endif |
David Lawrence Ramsey | 637b8bb | 2005-01-17 05:06:55 +0000 | [diff] [blame] | 2104 | case 'O': |
| 2105 | SET(MORE_SPACE); |
| 2106 | break; |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2107 | #ifndef DISABLE_HISTORIES |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 2108 | case 'P': |
| 2109 | SET(POS_HISTORY); |
| 2110 | break; |
| 2111 | #endif |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 2112 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2113 | case 'Q': |
| 2114 | quotestr = mallocstrcpy(quotestr, optarg); |
| 2115 | break; |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 2116 | #endif |
David Lawrence Ramsey | 84635cd | 2005-06-17 22:53:41 +0000 | [diff] [blame] | 2117 | case 'R': |
| 2118 | SET(RESTRICTED); |
| 2119 | break; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2120 | #ifndef NANO_TINY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2121 | case 'S': |
David Lawrence Ramsey | b2b6976 | 2005-06-16 02:13:10 +0000 | [diff] [blame] | 2122 | SET(SMOOTH_SCROLL); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2123 | break; |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 2124 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2125 | case 'T': |
| 2126 | if (!parse_num(optarg, &tabsize) || tabsize <= 0) { |
David Lawrence Ramsey | fed0195 | 2006-06-08 02:54:20 +0000 | [diff] [blame] | 2127 | fprintf(stderr, _("Requested tab size \"%s\" is invalid"), optarg); |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2128 | fprintf(stderr, "\n"); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2129 | exit(1); |
| 2130 | } |
| 2131 | break; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2132 | #ifndef NANO_TINY |
David Lawrence Ramsey | e29111f | 2005-06-17 19:06:25 +0000 | [diff] [blame] | 2133 | case 'U': |
| 2134 | SET(QUICK_BLANK); |
| 2135 | break; |
| 2136 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2137 | case 'V': |
| 2138 | version(); |
| 2139 | exit(0); |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2140 | #ifndef NANO_TINY |
David Lawrence Ramsey | 4f03daf | 2005-08-10 22:12:28 +0000 | [diff] [blame] | 2141 | case 'W': |
| 2142 | SET(WORD_BOUNDS); |
| 2143 | break; |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 2144 | case 'X': |
| 2145 | word_chars = mallocstrcpy(word_chars, optarg); |
| 2146 | break; |
David Lawrence Ramsey | 4f03daf | 2005-08-10 22:12:28 +0000 | [diff] [blame] | 2147 | #endif |
Benno Schulenberg | 0038992 | 2014-04-04 11:59:03 +0000 | [diff] [blame] | 2148 | #ifndef DISABLE_COLOR |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2149 | case 'Y': |
| 2150 | syntaxstr = mallocstrcpy(syntaxstr, optarg); |
| 2151 | break; |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 2152 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2153 | case 'c': |
David Lawrence Ramsey | 3435a0f | 2005-06-17 21:08:13 +0000 | [diff] [blame] | 2154 | SET(CONST_UPDATE); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2155 | break; |
| 2156 | case 'd': |
| 2157 | SET(REBIND_DELETE); |
| 2158 | break; |
Benno Schulenberg | 10c9093 | 2016-10-23 12:56:47 +0200 | [diff] [blame] | 2159 | case 'g': |
| 2160 | SET(SHOW_CURSOR); |
| 2161 | break; |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2162 | #ifndef NANO_TINY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2163 | case 'i': |
| 2164 | SET(AUTOINDENT); |
| 2165 | break; |
| 2166 | case 'k': |
| 2167 | SET(CUT_TO_END); |
| 2168 | break; |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 2169 | #endif |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2170 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2171 | case 'm': |
| 2172 | SET(USE_MOUSE); |
| 2173 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2174 | #endif |
Benno Schulenberg | db7064b | 2014-04-08 18:59:30 +0000 | [diff] [blame] | 2175 | case 'n': |
| 2176 | SET(NOREAD_MODE); |
| 2177 | break; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2178 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2179 | case 'o': |
| 2180 | operating_dir = mallocstrcpy(operating_dir, optarg); |
| 2181 | break; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 2182 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2183 | case 'p': |
| 2184 | SET(PRESERVE); |
| 2185 | break; |
Benno Schulenberg | 85ddc71 | 2014-07-02 19:57:23 +0000 | [diff] [blame] | 2186 | #ifndef DISABLE_NANORC |
Chris Allegretta | a30eb78 | 2009-02-09 04:03:20 +0000 | [diff] [blame] | 2187 | case 'q': |
| 2188 | SET(QUIET); |
| 2189 | break; |
Benno Schulenberg | 85ddc71 | 2014-07-02 19:57:23 +0000 | [diff] [blame] | 2190 | #endif |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 2191 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2192 | case 'r': |
| 2193 | if (!parse_num(optarg, &wrap_at)) { |
David Lawrence Ramsey | fed0195 | 2006-06-08 02:54:20 +0000 | [diff] [blame] | 2194 | fprintf(stderr, _("Requested fill size \"%s\" is invalid"), optarg); |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 2195 | fprintf(stderr, "\n"); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2196 | exit(1); |
| 2197 | } |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 2198 | fill_used = TRUE; |
Benno Schulenberg | 04a230f | 2016-05-15 15:18:47 +0200 | [diff] [blame] | 2199 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | 1f027a8 | 2015-11-21 19:44:27 +0000 | [diff] [blame] | 2200 | forced_wrapping = TRUE; |
Benno Schulenberg | 04a230f | 2016-05-15 15:18:47 +0200 | [diff] [blame] | 2201 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2202 | break; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 2203 | #endif |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 2204 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2205 | case 's': |
| 2206 | alt_speller = mallocstrcpy(alt_speller, optarg); |
| 2207 | break; |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 2208 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2209 | case 't': |
| 2210 | SET(TEMP_FILE); |
| 2211 | break; |
Benno Schulenberg | eac0446 | 2015-08-09 16:31:01 +0000 | [diff] [blame] | 2212 | #ifndef NANO_TINY |
Benno Schulenberg | 70cbbda | 2015-08-04 18:49:57 +0000 | [diff] [blame] | 2213 | case 'u': |
| 2214 | SET(MAKE_IT_UNIX); |
| 2215 | break; |
Benno Schulenberg | eac0446 | 2015-08-09 16:31:01 +0000 | [diff] [blame] | 2216 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2217 | case 'v': |
| 2218 | SET(VIEW_MODE); |
| 2219 | break; |
David Lawrence Ramsey | 95e0cf5 | 2003-01-02 16:32:20 +0000 | [diff] [blame] | 2220 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2221 | case 'w': |
| 2222 | SET(NO_WRAP); |
Benno Schulenberg | 499c6de | 2014-03-24 21:48:23 +0000 | [diff] [blame] | 2223 | /* If both --fill and --nowrap are given on the |
| 2224 | * command line, the last given option wins. */ |
Benno Schulenberg | 1f027a8 | 2015-11-21 19:44:27 +0000 | [diff] [blame] | 2225 | forced_wrapping = FALSE; |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2226 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2227 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2228 | case 'x': |
| 2229 | SET(NO_HELP); |
| 2230 | break; |
| 2231 | case 'z': |
| 2232 | SET(SUSPEND); |
| 2233 | break; |
Chris Allegretta | 05417a2 | 2009-08-17 07:52:10 +0000 | [diff] [blame] | 2234 | #ifndef NANO_TINY |
| 2235 | case '$': |
| 2236 | SET(SOFTWRAP); |
| 2237 | break; |
| 2238 | #endif |
Faissal Bensefia | de95ca6 | 2016-10-20 09:44:29 +0100 | [diff] [blame] | 2239 | #ifdef ENABLE_LINENUMBERS |
| 2240 | case 'l': |
| 2241 | SET(LINE_NUMBERS); |
| 2242 | break; |
| 2243 | #endif |
Benno Schulenberg | 4301918 | 2014-04-27 12:13:26 +0000 | [diff] [blame] | 2244 | case 'h': |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 2245 | usage(); |
Benno Schulenberg | 4301918 | 2014-04-27 12:13:26 +0000 | [diff] [blame] | 2246 | exit(0); |
| 2247 | default: |
| 2248 | printf(_("Type '%s -h' for a list of available options.\n"), argv[0]); |
| 2249 | exit(1); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2250 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2251 | } |
| 2252 | |
Benno Schulenberg | fcb4c3f | 2014-06-19 15:09:23 +0000 | [diff] [blame] | 2253 | /* If we're using restricted mode, disable suspending, backups, |
| 2254 | * rcfiles, and history files, since they all would allow reading |
| 2255 | * from or writing to files not specified on the command line. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 2256 | if (ISSET(RESTRICTED)) { |
| 2257 | UNSET(SUSPEND); |
| 2258 | UNSET(BACKUP_FILE); |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 2259 | #ifndef DISABLE_NANORC |
David Lawrence Ramsey | 18dae62 | 2006-04-24 18:54:29 +0000 | [diff] [blame] | 2260 | no_rcfiles = TRUE; |
Benno Schulenberg | fcb4c3f | 2014-06-19 15:09:23 +0000 | [diff] [blame] | 2261 | UNSET(HISTORYLOG); |
| 2262 | UNSET(POS_HISTORY); |
David Lawrence Ramsey | 9adc445 | 2006-04-24 20:17:50 +0000 | [diff] [blame] | 2263 | #endif |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 2264 | } |
| 2265 | |
Benno Schulenberg | a2dcd6e | 2014-04-07 09:24:10 +0000 | [diff] [blame] | 2266 | /* Set up the function and shortcut lists. This needs to be done |
| 2267 | * before reading the rcfile, to be able to rebind/unbind keys. */ |
| 2268 | shortcut_init(); |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 2269 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2270 | /* We've read through the command line options. Now back up the flags |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2271 | * and values that are set, and read the rcfile(s). If the values |
| 2272 | * haven't changed afterward, restore the backed-up values. */ |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 2273 | #ifndef DISABLE_NANORC |
David Lawrence Ramsey | 18dae62 | 2006-04-24 18:54:29 +0000 | [diff] [blame] | 2274 | if (!no_rcfiles) { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 2275 | #ifndef DISABLE_OPERATINGDIR |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2276 | char *operating_dir_cpy = operating_dir; |
| 2277 | #endif |
David Lawrence Ramsey | 7e8dd19 | 2004-08-12 20:06:20 +0000 | [diff] [blame] | 2278 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 2279 | ssize_t wrap_at_cpy = wrap_at; |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2280 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2281 | #ifndef NANO_TINY |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2282 | char *backup_dir_cpy = backup_dir; |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 2283 | char *word_chars_cpy = word_chars; |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2284 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2285 | #ifndef DISABLE_JUSTIFY |
| 2286 | char *quotestr_cpy = quotestr; |
| 2287 | #endif |
| 2288 | #ifndef DISABLE_SPELLER |
| 2289 | char *alt_speller_cpy = alt_speller; |
| 2290 | #endif |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 2291 | ssize_t tabsize_cpy = tabsize; |
Chris Allegretta | a48507d | 2009-08-14 03:18:29 +0000 | [diff] [blame] | 2292 | unsigned flags_cpy[sizeof(flags) / sizeof(flags[0])]; |
| 2293 | size_t i; |
| 2294 | |
| 2295 | memcpy(flags_cpy, flags, sizeof(flags_cpy)); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2296 | |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 2297 | #ifndef DISABLE_OPERATINGDIR |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2298 | operating_dir = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 2299 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2300 | #ifndef NANO_TINY |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2301 | backup_dir = NULL; |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 2302 | word_chars = NULL; |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2303 | #endif |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 2304 | #ifndef DISABLE_JUSTIFY |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2305 | quotestr = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 2306 | #endif |
| 2307 | #ifndef DISABLE_SPELLER |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2308 | alt_speller = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 2309 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2310 | |
Benno Schulenberg | c1a4842 | 2016-11-27 18:21:04 +0100 | [diff] [blame] | 2311 | do_rcfiles(); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2312 | |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 2313 | #ifdef DEBUG |
Benno Schulenberg | 0b6d6f4 | 2015-03-27 13:46:50 +0000 | [diff] [blame] | 2314 | fprintf(stderr, "After rebinding keys...\n"); |
| 2315 | print_sclist(); |
Chris Allegretta | 79a33bb | 2008-03-05 07:34:01 +0000 | [diff] [blame] | 2316 | #endif |
| 2317 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2318 | #ifndef DISABLE_OPERATINGDIR |
| 2319 | if (operating_dir_cpy != NULL) { |
| 2320 | free(operating_dir); |
| 2321 | operating_dir = operating_dir_cpy; |
| 2322 | } |
| 2323 | #endif |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame] | 2324 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 2325 | if (fill_used) |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2326 | wrap_at = wrap_at_cpy; |
| 2327 | #endif |
David Lawrence Ramsey | ebe3425 | 2005-11-15 03:17:35 +0000 | [diff] [blame] | 2328 | #ifndef NANO_TINY |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2329 | if (backup_dir_cpy != NULL) { |
| 2330 | free(backup_dir); |
| 2331 | backup_dir = backup_dir_cpy; |
| 2332 | } |
Benno Schulenberg | 6f12992 | 2016-06-30 18:02:45 +0200 | [diff] [blame] | 2333 | if (word_chars_cpy != NULL) { |
| 2334 | free(word_chars); |
| 2335 | word_chars = word_chars_cpy; |
| 2336 | } |
David Lawrence Ramsey | e68748a | 2007-12-10 17:59:26 +0000 | [diff] [blame] | 2337 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2338 | #ifndef DISABLE_JUSTIFY |
| 2339 | if (quotestr_cpy != NULL) { |
| 2340 | free(quotestr); |
| 2341 | quotestr = quotestr_cpy; |
| 2342 | } |
| 2343 | #endif |
| 2344 | #ifndef DISABLE_SPELLER |
| 2345 | if (alt_speller_cpy != NULL) { |
| 2346 | free(alt_speller); |
| 2347 | alt_speller = alt_speller_cpy; |
| 2348 | } |
| 2349 | #endif |
David Lawrence Ramsey | 04419b9 | 2004-07-18 18:13:54 +0000 | [diff] [blame] | 2350 | if (tabsize_cpy != -1) |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2351 | tabsize = tabsize_cpy; |
Chris Allegretta | a48507d | 2009-08-14 03:18:29 +0000 | [diff] [blame] | 2352 | |
| 2353 | for (i = 0; i < sizeof(flags) / sizeof(flags[0]); i++) |
| 2354 | flags[i] |= flags_cpy[i]; |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2355 | } |
Benno Schulenberg | 6f6ee84 | 2014-04-07 20:05:05 +0000 | [diff] [blame] | 2356 | #ifdef DISABLE_ROOTWRAPPING |
| 2357 | /* If we don't have any rcfiles, --disable-wrapping-as-root is used, |
| 2358 | * and we're root, turn wrapping off. */ |
| 2359 | else if (geteuid() == NANO_ROOT_UID) |
| 2360 | SET(NO_WRAP); |
| 2361 | #endif |
Benno Schulenberg | eea0908 | 2014-04-13 20:50:20 +0000 | [diff] [blame] | 2362 | #endif /* !DISABLE_NANORC */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2363 | |
Chris Allegretta | 8b6f6fc | 2009-11-22 21:35:56 +0000 | [diff] [blame] | 2364 | #ifndef DISABLE_WRAPPING |
Benno Schulenberg | 1f027a8 | 2015-11-21 19:44:27 +0000 | [diff] [blame] | 2365 | /* Override a "set nowrap" in an rcfile (or a --disable-wrapping-as-root) |
| 2366 | * if --fill was given on the command line and not undone by --nowrap. */ |
| 2367 | if (forced_wrapping) |
Chris Allegretta | 16e07d5 | 2009-11-18 12:39:47 +0000 | [diff] [blame] | 2368 | UNSET(NO_WRAP); |
Chris Allegretta | 8b6f6fc | 2009-11-22 21:35:56 +0000 | [diff] [blame] | 2369 | #endif |
Chris Allegretta | 16e07d5 | 2009-11-18 12:39:47 +0000 | [diff] [blame] | 2370 | |
David Lawrence Ramsey | 4d72de7 | 2006-04-12 15:27:40 +0000 | [diff] [blame] | 2371 | /* If we're using bold text instead of reverse video text, set it up |
| 2372 | * now. */ |
| 2373 | if (ISSET(BOLD_TEXT)) |
Benno Schulenberg | c970035 | 2014-05-04 08:53:06 +0000 | [diff] [blame] | 2374 | hilite_attribute = A_BOLD; |
David Lawrence Ramsey | 4d72de7 | 2006-04-12 15:27:40 +0000 | [diff] [blame] | 2375 | |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2376 | #ifndef DISABLE_HISTORIES |
David Lawrence Ramsey | 4d72de7 | 2006-04-12 15:27:40 +0000 | [diff] [blame] | 2377 | /* Set up the search/replace history. */ |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 2378 | history_init(); |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2379 | /* Verify that the home directory and ~/.nano subdir exist. */ |
Benno Schulenberg | fcb4c3f | 2014-06-19 15:09:23 +0000 | [diff] [blame] | 2380 | if (ISSET(HISTORYLOG) || ISSET(POS_HISTORY)) { |
| 2381 | get_homedir(); |
| 2382 | if (homedir == NULL || check_dotnano() == 0) { |
| 2383 | UNSET(HISTORYLOG); |
| 2384 | UNSET(POS_HISTORY); |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 2385 | } |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 2386 | } |
Benno Schulenberg | fcb4c3f | 2014-06-19 15:09:23 +0000 | [diff] [blame] | 2387 | if (ISSET(HISTORYLOG)) |
| 2388 | load_history(); |
| 2389 | if (ISSET(POS_HISTORY)) |
| 2390 | load_poshistory(); |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2391 | #endif /* !DISABLE_HISTORIES */ |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 2392 | |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2393 | #ifndef NANO_TINY |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 2394 | /* Set up the backup directory (unless we're using restricted mode, |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 2395 | * in which case backups are disabled, since they would allow |
| 2396 | * reading from or writing to files not specified on the command |
| 2397 | * line). This entails making sure it exists and is a directory, so |
| 2398 | * that backup files will be saved there. */ |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 2399 | if (!ISSET(RESTRICTED)) |
| 2400 | init_backup_dir(); |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2401 | #endif |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2402 | |
Benno Schulenberg | aa09abe | 2016-06-12 11:13:45 +0200 | [diff] [blame] | 2403 | #ifndef DISABLE_OPERATINGDIR |
| 2404 | /* Set up the operating directory. This entails chdir()ing there, |
| 2405 | * so that file reads and writes will be based there. */ |
| 2406 | init_operating_dir(); |
| 2407 | #endif |
| 2408 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2409 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 2410 | /* If punct wasn't specified, set its default value. */ |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 2411 | if (punct == NULL) |
David Lawrence Ramsey | 5329f23 | 2006-01-03 04:53:41 +0000 | [diff] [blame] | 2412 | punct = mallocstrcpy(NULL, "!.?"); |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 2413 | |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 2414 | /* If brackets wasn't specified, set its default value. */ |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 2415 | if (brackets == NULL) |
David Lawrence Ramsey | 5329f23 | 2006-01-03 04:53:41 +0000 | [diff] [blame] | 2416 | brackets = mallocstrcpy(NULL, "\"')>]}"); |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 2417 | |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 2418 | /* If quotestr wasn't specified, set its default value. */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2419 | if (quotestr == NULL) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 2420 | quotestr = mallocstrcpy(NULL, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2421 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 5329f23 | 2006-01-03 04:53:41 +0000 | [diff] [blame] | 2422 | "^([ \t]*[#:>|}])+" |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2423 | #else |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 2424 | "> " |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2425 | #endif |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 2426 | ); |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2427 | #ifdef HAVE_REGEX_H |
Benno Schulenberg | 2f817a6 | 2016-03-22 10:42:28 +0000 | [diff] [blame] | 2428 | quoterc = regcomp("ereg, quotestr, NANO_REG_EXTENDED); |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2429 | |
| 2430 | if (quoterc == 0) { |
| 2431 | /* We no longer need quotestr, just quotereg. */ |
| 2432 | free(quotestr); |
| 2433 | quotestr = NULL; |
| 2434 | } else { |
| 2435 | size_t size = regerror(quoterc, "ereg, NULL, 0); |
| 2436 | |
| 2437 | quoteerr = charalloc(size); |
| 2438 | regerror(quoterc, "ereg, quoteerr, size); |
| 2439 | } |
| 2440 | #else |
| 2441 | quotelen = strlen(quotestr); |
| 2442 | #endif /* !HAVE_REGEX_H */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2443 | #endif /* !DISABLE_JUSTIFY */ |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 2444 | |
David Lawrence Ramsey | 3aedb36 | 2004-05-28 22:42:41 +0000 | [diff] [blame] | 2445 | #ifndef DISABLE_SPELLER |
| 2446 | /* If we don't have an alternative spell checker after reading the |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 2447 | * command line and/or rcfile(s), check $SPELL for one, as Pico |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 2448 | * does (unless we're using restricted mode, in which case spell |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 2449 | * checking is disabled, since it would allow reading from or |
| 2450 | * writing to files not specified on the command line). */ |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 2451 | if (!ISSET(RESTRICTED) && alt_speller == NULL) { |
David Lawrence Ramsey | 3aedb36 | 2004-05-28 22:42:41 +0000 | [diff] [blame] | 2452 | char *spellenv = getenv("SPELL"); |
| 2453 | if (spellenv != NULL) |
| 2454 | alt_speller = mallocstrcpy(NULL, spellenv); |
| 2455 | } |
| 2456 | #endif |
| 2457 | |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 2458 | #ifndef NANO_TINY |
| 2459 | /* If matchbrackets wasn't specified, set its default value. */ |
| 2460 | if (matchbrackets == NULL) |
| 2461 | matchbrackets = mallocstrcpy(NULL, "(<[{)>]}"); |
David Lawrence Ramsey | d89617f | 2006-01-06 21:51:10 +0000 | [diff] [blame] | 2462 | |
Benno Schulenberg | 91c740e | 2014-04-08 12:24:52 +0000 | [diff] [blame] | 2463 | /* If whitespace wasn't specified, set its default value. If we're |
| 2464 | * using UTF-8, it's Unicode 00BB (Right-Pointing Double Angle |
| 2465 | * Quotation Mark) and Unicode 00B7 (Middle Dot). Otherwise, it's |
| 2466 | * ">" and ".". */ |
Benno Schulenberg | 55a0d76 | 2014-03-24 13:35:50 +0000 | [diff] [blame] | 2467 | if (whitespace == NULL) { |
Benno Schulenberg | cf71578 | 2014-03-27 11:06:16 +0000 | [diff] [blame] | 2468 | #ifdef ENABLE_UTF8 |
Benno Schulenberg | c85dc05 | 2014-03-16 13:19:41 +0000 | [diff] [blame] | 2469 | if (using_utf8()) { |
Benno Schulenberg | 91c740e | 2014-04-08 12:24:52 +0000 | [diff] [blame] | 2470 | whitespace = mallocstrcpy(NULL, "\xC2\xBB\xC2\xB7"); |
Benno Schulenberg | c85dc05 | 2014-03-16 13:19:41 +0000 | [diff] [blame] | 2471 | whitespace_len[0] = 2; |
| 2472 | whitespace_len[1] = 2; |
Benno Schulenberg | cf71578 | 2014-03-27 11:06:16 +0000 | [diff] [blame] | 2473 | } else |
| 2474 | #endif |
| 2475 | { |
Benno Schulenberg | c85dc05 | 2014-03-16 13:19:41 +0000 | [diff] [blame] | 2476 | whitespace = mallocstrcpy(NULL, ">."); |
| 2477 | whitespace_len[0] = 1; |
| 2478 | whitespace_len[1] = 1; |
| 2479 | } |
Benno Schulenberg | 55a0d76 | 2014-03-24 13:35:50 +0000 | [diff] [blame] | 2480 | } |
Benno Schulenberg | 70b2d08 | 2014-04-05 20:28:29 +0000 | [diff] [blame] | 2481 | #endif /* !NANO_TINY */ |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 2482 | |
Benno Schulenberg | 5174323 | 2016-03-28 19:14:33 +0000 | [diff] [blame] | 2483 | /* Initialize the search string. */ |
Benno Schulenberg | 1cf22d4 | 2015-04-21 18:42:11 +0000 | [diff] [blame] | 2484 | last_search = mallocstrcpy(NULL, ""); |
Benno Schulenberg | 1cf22d4 | 2015-04-21 18:42:11 +0000 | [diff] [blame] | 2485 | |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2486 | /* If tabsize wasn't specified, set its default value. */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2487 | if (tabsize == -1) |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2488 | tabsize = WIDTH_OF_TAB; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 2489 | |
David Lawrence Ramsey | e68748a | 2007-12-10 17:59:26 +0000 | [diff] [blame] | 2490 | /* Initialize curses mode. If this fails, get out. */ |
| 2491 | if (initscr() == NULL) |
| 2492 | exit(1); |
David Lawrence Ramsey | b386a90 | 2005-07-10 02:37:38 +0000 | [diff] [blame] | 2493 | |
| 2494 | /* Set up the terminal state. */ |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 2495 | terminal_init(); |
David Lawrence Ramsey | c53a92d | 2004-01-12 03:28:06 +0000 | [diff] [blame] | 2496 | |
Benno Schulenberg | 08cd197 | 2016-09-08 21:00:51 +0200 | [diff] [blame] | 2497 | #ifdef __linux__ |
Benno Schulenberg | 290d278 | 2016-07-29 09:15:07 +0200 | [diff] [blame] | 2498 | /* Check whether we're running on a Linux console. */ |
| 2499 | console = (getenv("DISPLAY") == NULL); |
Benno Schulenberg | 928a24c | 2016-08-11 12:37:11 +0200 | [diff] [blame] | 2500 | #endif |
Benno Schulenberg | e7f39bc | 2016-08-28 21:00:13 +0200 | [diff] [blame] | 2501 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2502 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 2503 | fprintf(stderr, "Main: set up windows\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2504 | #endif |
| 2505 | |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 2506 | /* Initialize all the windows based on the current screen |
| 2507 | * dimensions. */ |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2508 | window_init(); |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 2509 | |
Benno Schulenberg | 9c2e270 | 2016-10-28 10:50:06 +0200 | [diff] [blame] | 2510 | editwincols = COLS; |
| 2511 | |
David Lawrence Ramsey | 3f12ada | 2005-07-25 22:54:16 +0000 | [diff] [blame] | 2512 | /* Set up the signal handlers. */ |
| 2513 | signal_init(); |
| 2514 | |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2515 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 503bad0 | 2006-06-09 18:24:37 +0000 | [diff] [blame] | 2516 | /* Initialize mouse support. */ |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2517 | mouse_init(); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2518 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2519 | |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 2520 | #ifndef DISABLE_COLOR |
| 2521 | set_colorpairs(); |
| 2522 | #else |
Benno Schulenberg | 960e848 | 2016-07-12 09:35:48 +0200 | [diff] [blame] | 2523 | interface_color_pair[TITLE_BAR] = hilite_attribute; |
Benno Schulenberg | de2aa4f | 2016-10-20 10:07:48 +0200 | [diff] [blame] | 2524 | interface_color_pair[LINE_NUMBER] = hilite_attribute; |
Benno Schulenberg | 960e848 | 2016-07-12 09:35:48 +0200 | [diff] [blame] | 2525 | interface_color_pair[STATUS_BAR] = hilite_attribute; |
| 2526 | interface_color_pair[KEY_COMBO] = hilite_attribute; |
| 2527 | interface_color_pair[FUNCTION_TAG] = A_NORMAL; |
Benno Schulenberg | 1663994 | 2014-05-03 18:24:45 +0000 | [diff] [blame] | 2528 | #endif |
| 2529 | |
Benno Schulenberg | c6dbcf9 | 2016-06-25 15:16:52 +0200 | [diff] [blame] | 2530 | /* Ask ncurses for the key codes for Control+Left/Right/Up/Down. */ |
Benno Schulenberg | c661506 | 2016-08-16 10:49:55 +0200 | [diff] [blame] | 2531 | controlleft = get_keycode("kLFT5", CONTROL_LEFT); |
| 2532 | controlright = get_keycode("kRIT5", CONTROL_RIGHT); |
| 2533 | controlup = get_keycode("kUP5", CONTROL_UP); |
| 2534 | controldown = get_keycode("kDN5", CONTROL_DOWN); |
Benno Schulenberg | f2c72bf | 2016-11-17 17:32:28 +0100 | [diff] [blame] | 2535 | #ifndef NANO_TINY |
Benno Schulenberg | 382c9d7 | 2016-04-24 11:28:28 +0200 | [diff] [blame] | 2536 | /* Ask for the codes for Shift+Control+Left/Right/Up/Down. */ |
| 2537 | shiftcontrolleft = get_keycode("kLFT6", SHIFT_CONTROL_LEFT); |
| 2538 | shiftcontrolright = get_keycode("kRIT6", SHIFT_CONTROL_RIGHT); |
| 2539 | shiftcontrolup = get_keycode("kUP6", SHIFT_CONTROL_UP); |
| 2540 | shiftcontroldown = get_keycode("kDN6", SHIFT_CONTROL_DOWN); |
| 2541 | /* Ask for the codes for Shift+Alt+Left/Right/Up/Down. */ |
| 2542 | shiftaltleft = get_keycode("kLFT4", SHIFT_ALT_LEFT); |
| 2543 | shiftaltright = get_keycode("kRIT4", SHIFT_ALT_RIGHT); |
| 2544 | shiftaltup = get_keycode("kUP4", SHIFT_ALT_UP); |
| 2545 | shiftaltdown = get_keycode("kDN4", SHIFT_ALT_DOWN); |
Benno Schulenberg | f08d79d | 2015-11-23 08:52:23 +0000 | [diff] [blame] | 2546 | #endif |
| 2547 | |
Benno Schulenberg | f2150d3 | 2016-07-11 14:21:38 +0200 | [diff] [blame] | 2548 | #ifndef USE_SLANG |
| 2549 | /* Tell ncurses to pass the Esc key quickly. */ |
| 2550 | set_escdelay(50); |
| 2551 | #endif |
| 2552 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2553 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 2554 | fprintf(stderr, "Main: open file\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2555 | #endif |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2556 | |
David Lawrence Ramsey | 775eeba | 2005-05-16 18:38:16 +0000 | [diff] [blame] | 2557 | /* If there's a +LINE or +LINE,COLUMN flag here, it is the first |
| 2558 | * non-option argument, and it is followed by at least one other |
| 2559 | * argument, the filename it applies to. */ |
David Lawrence Ramsey | bf1346f | 2004-10-22 20:25:56 +0000 | [diff] [blame] | 2560 | if (0 < optind && optind < argc - 1 && argv[optind][0] == '+') { |
Benno Schulenberg | e8c7cf2 | 2017-01-17 14:17:42 +0100 | [diff] [blame] | 2561 | if (!parse_line_column(&argv[optind][1], &startline, &startcol)) |
| 2562 | statusline(ALERT, _("Invalid line or column number")); |
David Lawrence Ramsey | bf1346f | 2004-10-22 20:25:56 +0000 | [diff] [blame] | 2563 | optind++; |
| 2564 | } |
| 2565 | |
Benno Schulenberg | e7f39bc | 2016-08-28 21:00:13 +0200 | [diff] [blame] | 2566 | /* If one of the arguments is a dash, read text from standard input. */ |
Chris Allegretta | 25d459a | 2009-01-30 08:34:27 +0000 | [diff] [blame] | 2567 | if (optind < argc && !strcmp(argv[optind], "-")) { |
Chris Allegretta | b0f0b7f | 2009-01-30 17:37:44 +0000 | [diff] [blame] | 2568 | stdin_pager(); |
Chris Allegretta | 25d459a | 2009-01-30 08:34:27 +0000 | [diff] [blame] | 2569 | set_modified(); |
| 2570 | optind++; |
| 2571 | } |
| 2572 | |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 2573 | #ifndef DISABLE_MULTIBUFFER |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2574 | old_multibuffer = ISSET(MULTIBUFFER); |
| 2575 | SET(MULTIBUFFER); |
| 2576 | |
| 2577 | /* Read all the files after the first one on the command line into |
| 2578 | * new buffers. */ |
| 2579 | { |
David Lawrence Ramsey | 2cf6d71 | 2005-06-28 06:25:34 +0000 | [diff] [blame] | 2580 | int i = optind + 1; |
Benno Schulenberg | 3060ce3 | 2014-06-20 19:10:24 +0000 | [diff] [blame] | 2581 | ssize_t iline = 0, icol = 0; |
David Lawrence Ramsey | 775eeba | 2005-05-16 18:38:16 +0000 | [diff] [blame] | 2582 | |
David Lawrence Ramsey | bf1346f | 2004-10-22 20:25:56 +0000 | [diff] [blame] | 2583 | for (; i < argc; i++) { |
Benno Schulenberg | d52f108 | 2015-07-19 09:25:25 +0000 | [diff] [blame] | 2584 | /* If there's a +LINE or +LINE,COLUMN flag here, it is followed |
| 2585 | * by at least one other argument: the filename it applies to. */ |
Benno Schulenberg | e8c7cf2 | 2017-01-17 14:17:42 +0100 | [diff] [blame] | 2586 | if (i < argc - 1 && argv[i][0] == '+') { |
| 2587 | if (!parse_line_column(&argv[i][1], &iline, &icol)) |
| 2588 | statusline(ALERT, _("Invalid line or column number")); |
| 2589 | } else { |
Benno Schulenberg | 37d8ad8 | 2015-12-30 10:11:20 +0000 | [diff] [blame] | 2590 | /* If opening fails, don't try to position the cursor. */ |
| 2591 | if (!open_buffer(argv[i], FALSE)) |
| 2592 | continue; |
David Lawrence Ramsey | 775eeba | 2005-05-16 18:38:16 +0000 | [diff] [blame] | 2593 | |
Benno Schulenberg | d52f108 | 2015-07-19 09:25:25 +0000 | [diff] [blame] | 2594 | /* If a position was given on the command line, go there. */ |
Benno Schulenberg | 3060ce3 | 2014-06-20 19:10:24 +0000 | [diff] [blame] | 2595 | if (iline > 0 || icol > 0) { |
Benno Schulenberg | 46db638 | 2015-12-31 19:20:40 +0000 | [diff] [blame] | 2596 | do_gotolinecolumn(iline, icol, FALSE, FALSE); |
Benno Schulenberg | 9dc544a | 2015-07-19 09:01:59 +0000 | [diff] [blame] | 2597 | iline = 0; |
| 2598 | icol = 0; |
David Lawrence Ramsey | bf1346f | 2004-10-22 20:25:56 +0000 | [diff] [blame] | 2599 | } |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2600 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | f8ed6bb | 2016-01-29 16:20:28 +0000 | [diff] [blame] | 2601 | else if (ISSET(POS_HISTORY)) { |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 2602 | ssize_t savedposline, savedposcol; |
Benno Schulenberg | d52f108 | 2015-07-19 09:25:25 +0000 | [diff] [blame] | 2603 | /* If edited before, restore the last cursor position. */ |
Benno Schulenberg | dc18746 | 2016-12-23 13:49:14 +0100 | [diff] [blame] | 2604 | if (has_old_position(argv[i], &savedposline, &savedposcol)) |
Benno Schulenberg | 1592ca0 | 2015-12-31 17:20:46 +0000 | [diff] [blame] | 2605 | do_gotolinecolumn(savedposline, savedposcol, |
Benno Schulenberg | 46db638 | 2015-12-31 19:20:40 +0000 | [diff] [blame] | 2606 | FALSE, FALSE); |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 2607 | } |
Benno Schulenberg | d6e3972 | 2014-02-22 16:26:30 +0000 | [diff] [blame] | 2608 | #endif |
David Lawrence Ramsey | bf1346f | 2004-10-22 20:25:56 +0000 | [diff] [blame] | 2609 | } |
| 2610 | } |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 2611 | } |
Benno Schulenberg | 70b2d08 | 2014-04-05 20:28:29 +0000 | [diff] [blame] | 2612 | #endif /* !DISABLE_MULTIBUFFER */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2613 | |
Benno Schulenberg | d52f108 | 2015-07-19 09:25:25 +0000 | [diff] [blame] | 2614 | /* Now read the first file on the command line into a new buffer. */ |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2615 | if (optind < argc) |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 2616 | open_buffer(argv[optind], FALSE); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2617 | |
Benno Schulenberg | d52f108 | 2015-07-19 09:25:25 +0000 | [diff] [blame] | 2618 | /* If all the command-line arguments were invalid files like directories, |
| 2619 | * or if there were no filenames given, we didn't open any file. In this |
| 2620 | * case, load a blank buffer. Also, unset view mode to allow editing. */ |
David Lawrence Ramsey | 6ad59cd | 2005-07-08 20:09:16 +0000 | [diff] [blame] | 2621 | if (openfile == NULL) { |
Chris Allegretta | 14c8620 | 2008-08-03 04:48:05 +0000 | [diff] [blame] | 2622 | open_buffer("", FALSE); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2623 | UNSET(VIEW_MODE); |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2624 | } |
| 2625 | |
Benno Schulenberg | 0636d7b | 2014-04-03 20:23:07 +0000 | [diff] [blame] | 2626 | #ifndef DISABLE_MULTIBUFFER |
David Lawrence Ramsey | 02517e0 | 2004-09-05 21:40:31 +0000 | [diff] [blame] | 2627 | if (!old_multibuffer) |
| 2628 | UNSET(MULTIBUFFER); |
| 2629 | #endif |
| 2630 | |
Benno Schulenberg | d52f108 | 2015-07-19 09:25:25 +0000 | [diff] [blame] | 2631 | /* If a starting position was given on the command line, go there. */ |
Benno Schulenberg | 3060ce3 | 2014-06-20 19:10:24 +0000 | [diff] [blame] | 2632 | if (startline > 0 || startcol > 0) |
Benno Schulenberg | 46db638 | 2015-12-31 19:20:40 +0000 | [diff] [blame] | 2633 | do_gotolinecolumn(startline, startcol, FALSE, FALSE); |
Benno Schulenberg | b341f29 | 2014-06-19 20:05:24 +0000 | [diff] [blame] | 2634 | #ifndef DISABLE_HISTORIES |
Benno Schulenberg | 98e72f5 | 2016-01-29 17:18:27 +0000 | [diff] [blame] | 2635 | else if (ISSET(POS_HISTORY)) { |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 2636 | ssize_t savedposline, savedposcol; |
Benno Schulenberg | d52f108 | 2015-07-19 09:25:25 +0000 | [diff] [blame] | 2637 | /* If the file was edited before, restore the last cursor position. */ |
Benno Schulenberg | dc18746 | 2016-12-23 13:49:14 +0100 | [diff] [blame] | 2638 | if (has_old_position(argv[optind], &savedposline, &savedposcol)) |
Benno Schulenberg | 46db638 | 2015-12-31 19:20:40 +0000 | [diff] [blame] | 2639 | do_gotolinecolumn(savedposline, savedposcol, FALSE, FALSE); |
Chris Allegretta | 9bfda91 | 2011-02-16 06:52:30 +0000 | [diff] [blame] | 2640 | } |
Benno Schulenberg | d6e3972 | 2014-02-22 16:26:30 +0000 | [diff] [blame] | 2641 | #endif |
David Lawrence Ramsey | 775eeba | 2005-05-16 18:38:16 +0000 | [diff] [blame] | 2642 | |
Benno Schulenberg | f5eb316 | 2016-02-23 12:37:10 +0000 | [diff] [blame] | 2643 | #ifdef DEBUG |
Benno Schulenberg | e7f39bc | 2016-08-28 21:00:13 +0200 | [diff] [blame] | 2644 | fprintf(stderr, "Main: show buffer contents, and enter main loop\n"); |
Benno Schulenberg | f5eb316 | 2016-02-23 12:37:10 +0000 | [diff] [blame] | 2645 | #endif |
| 2646 | |
David Lawrence Ramsey | f3318a9 | 2007-12-03 18:40:33 +0000 | [diff] [blame] | 2647 | display_buffer(); |
Robert Siemborski | 6967eec | 2000-07-08 14:23:32 +0000 | [diff] [blame] | 2648 | |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 2649 | while (TRUE) { |
Benno Schulenberg | 023edff | 2016-10-21 14:31:37 +0200 | [diff] [blame] | 2650 | #ifdef ENABLE_LINENUMBERS |
| 2651 | int needed_margin = digits(openfile->filebot->lineno) + 1; |
| 2652 | |
| 2653 | /* Only enable line numbers when there is enough room for them. */ |
David Lawrence Ramsey | c24545f | 2017-02-09 10:20:04 -0600 | [diff] [blame] | 2654 | if (!ISSET(LINE_NUMBERS) || needed_margin > COLS - 4) |
| 2655 | needed_margin = 0; |
| 2656 | |
| 2657 | if (needed_margin != margin) { |
| 2658 | margin = needed_margin; |
| 2659 | editwincols = COLS - margin; |
| 2660 | /* The margin has changed -- schedule a full refresh. */ |
| 2661 | refresh_needed = TRUE; |
Benno Schulenberg | 023edff | 2016-10-21 14:31:37 +0200 | [diff] [blame] | 2662 | } |
David Lawrence Ramsey | c24545f | 2017-02-09 10:20:04 -0600 | [diff] [blame] | 2663 | #endif |
Benno Schulenberg | b38cbfe | 2016-10-24 17:38:24 +0200 | [diff] [blame] | 2664 | |
Benno Schulenberg | 9106cc8 | 2016-05-08 12:01:33 +0200 | [diff] [blame] | 2665 | if (currmenu != MMAIN) |
| 2666 | display_main_list(); |
| 2667 | |
Benno Schulenberg | ed16582 | 2016-11-13 20:00:31 +0100 | [diff] [blame] | 2668 | lastmessage = HUSH; |
Benno Schulenberg | eef7d10 | 2016-12-20 19:27:41 +0100 | [diff] [blame] | 2669 | as_an_at = TRUE; |
Benno Schulenberg | ed16582 | 2016-11-13 20:00:31 +0100 | [diff] [blame] | 2670 | |
| 2671 | /* Update the displayed current cursor position only when there |
| 2672 | * are no keys waiting in the input buffer. */ |
| 2673 | if (ISSET(CONST_UPDATE) && get_key_buffer_len() == 0) |
| 2674 | do_cursorpos(TRUE); |
| 2675 | |
Benno Schulenberg | 372bd0f | 2016-12-03 17:47:37 +0100 | [diff] [blame] | 2676 | /* Refresh just the cursor position or the entire edit window. */ |
Benno Schulenberg | a4132e2 | 2016-11-12 17:59:28 +0100 | [diff] [blame] | 2677 | if (!refresh_needed) { |
| 2678 | reset_cursor(); |
Benno Schulenberg | a4132e2 | 2016-11-12 17:59:28 +0100 | [diff] [blame] | 2679 | wnoutrefresh(edit); |
| 2680 | } else |
| 2681 | edit_refresh(); |
| 2682 | |
Benno Schulenberg | 372bd0f | 2016-12-03 17:47:37 +0100 | [diff] [blame] | 2683 | /* Make sure the cursor is visible. */ |
| 2684 | curs_set(1); |
| 2685 | |
Benno Schulenberg | 318ed6b | 2016-04-12 10:24:57 +0200 | [diff] [blame] | 2686 | focusing = TRUE; |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 2687 | |
Benno Schulenberg | 34a20f8 | 2016-04-17 15:24:05 +0200 | [diff] [blame] | 2688 | /* Forget any earlier statusbar x position. */ |
| 2689 | reinit_statusbar_x(); |
| 2690 | |
Benno Schulenberg | ddd9c7a | 2016-06-24 22:45:41 +0200 | [diff] [blame] | 2691 | /* Read in and interpret keystrokes. */ |
Benno Schulenberg | 7e5324d | 2014-06-30 18:04:33 +0000 | [diff] [blame] | 2692 | do_input(TRUE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2693 | } |
David Lawrence Ramsey | 5f9acfe | 2005-03-04 17:09:41 +0000 | [diff] [blame] | 2694 | |
David Lawrence Ramsey | 6d6a36c | 2005-12-08 07:09:08 +0000 | [diff] [blame] | 2695 | /* We should never get here. */ |
David Lawrence Ramsey | fd1768a | 2004-03-19 21:57:56 +0000 | [diff] [blame] | 2696 | assert(FALSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2697 | } |