Chris Allegretta | 11b0011 | 2000-08-06 21:13:45 +0000 | [diff] [blame] | 1 | /* $Id$ */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2 | /************************************************************************** |
| 3 | * nano.c * |
| 4 | * * |
David Lawrence Ramsey | f28f50e | 2004-01-09 23:04:55 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 Chris Allegretta * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 6 | * This program is free software; you can redistribute it and/or modify * |
| 7 | * it under the terms of the GNU General Public License as published by * |
Chris Allegretta | 3a24f3f | 2001-10-24 11:33:54 +0000 | [diff] [blame] | 8 | * the Free Software Foundation; either version 2, or (at your option) * |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 9 | * any later version. * |
| 10 | * * |
| 11 | * This program is distributed in the hope that it will be useful, * |
| 12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of * |
| 13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * |
| 14 | * GNU General Public License for more details. * |
| 15 | * * |
| 16 | * You should have received a copy of the GNU General Public License * |
| 17 | * along with this program; if not, write to the Free Software * |
| 18 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. * |
| 19 | * * |
| 20 | **************************************************************************/ |
| 21 | |
Chris Allegretta | 6efda54 | 2001-04-28 18:03:52 +0000 | [diff] [blame] | 22 | #include "config.h" |
| 23 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <stdarg.h> |
| 27 | #include <signal.h> |
| 28 | #include <unistd.h> |
| 29 | #include <string.h> |
| 30 | #include <fcntl.h> |
| 31 | #include <sys/stat.h> |
| 32 | #include <sys/ioctl.h> |
| 33 | #include <sys/param.h> |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 34 | #include <sys/wait.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 35 | #include <errno.h> |
| 36 | #include <ctype.h> |
| 37 | #include <locale.h> |
| 38 | #include <limits.h> |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 39 | #include <assert.h> |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 40 | #include "proto.h" |
| 41 | #include "nano.h" |
| 42 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 43 | #ifdef HAVE_TERMIOS_H |
| 44 | #include <termios.h> |
| 45 | #endif |
| 46 | |
| 47 | #ifdef HAVE_TERMIO_H |
| 48 | #include <termio.h> |
| 49 | #endif |
| 50 | |
| 51 | #ifdef HAVE_GETOPT_H |
| 52 | #include <getopt.h> |
| 53 | #endif |
| 54 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 55 | #ifndef NANO_SMALL |
| 56 | #include <setjmp.h> |
| 57 | #endif |
| 58 | |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 59 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 60 | static ssize_t fill = 0; /* Fill - where to wrap lines, |
| 61 | basically */ |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 62 | #endif |
David Lawrence Ramsey | c97acfb | 2003-09-10 20:08:00 +0000 | [diff] [blame] | 63 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 64 | static bool same_line_wrap = FALSE; /* Whether wrapped text should |
David Lawrence Ramsey | 85529b3 | 2004-06-22 15:38:47 +0000 | [diff] [blame] | 65 | be prepended to the next |
| 66 | line */ |
David Lawrence Ramsey | c97acfb | 2003-09-10 20:08:00 +0000 | [diff] [blame] | 67 | #endif |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 68 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 69 | static struct termios oldterm; /* The user's original term settings */ |
Chris Allegretta | 88520c9 | 2001-05-05 17:45:54 +0000 | [diff] [blame] | 70 | static struct sigaction act; /* For all our fun signal handlers */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 71 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 72 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 85529b3 | 2004-06-22 15:38:47 +0000 | [diff] [blame] | 73 | static sigjmp_buf jmpbuf; /* Used to return to mainloop after |
| 74 | SIGWINCH */ |
David Lawrence Ramsey | 22fac78 | 2004-08-05 15:16:19 +0000 | [diff] [blame] | 75 | static int pid; /* The PID of the newly forked process |
| 76 | * in open_pipe(). It must be global |
| 77 | * because the signal handler needs |
| 78 | * it. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 79 | #endif |
Chris Allegretta | 0802088 | 2001-01-29 23:37:54 +0000 | [diff] [blame] | 80 | |
David Lawrence Ramsey | da14106 | 2004-05-25 19:41:11 +0000 | [diff] [blame] | 81 | /* What we do when we're all set to exit. */ |
| 82 | void finish(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 83 | { |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 84 | if (!ISSET(NO_HELP)) |
| 85 | blank_bottombars(); |
| 86 | else |
| 87 | blank_statusbar(); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 88 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 89 | wrefresh(bottomwin); |
| 90 | endwin(); |
| 91 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 92 | /* Restore the old terminal settings. */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 93 | tcsetattr(0, TCSANOW, &oldterm); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 94 | |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 95 | #if !defined(NANO_SMALL) && defined(ENABLE_NANORC) |
| 96 | if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG)) |
| 97 | save_history(); |
| 98 | #endif |
| 99 | |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 100 | #ifdef DEBUG |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 101 | thanks_for_all_the_fish(); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 102 | #endif |
Chris Allegretta | f5de33a | 2002-02-27 04:14:16 +0000 | [diff] [blame] | 103 | |
David Lawrence Ramsey | da14106 | 2004-05-25 19:41:11 +0000 | [diff] [blame] | 104 | exit(0); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 105 | } |
| 106 | |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 107 | /* Die (gracefully?). */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 108 | void die(const char *msg, ...) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 109 | { |
| 110 | va_list ap; |
| 111 | |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 112 | endwin(); |
| 113 | curses_ended = TRUE; |
| 114 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 115 | /* Restore the old terminal settings. */ |
Chris Allegretta | 4da1fc6 | 2000-06-21 03:00:43 +0000 | [diff] [blame] | 116 | tcsetattr(0, TCSANOW, &oldterm); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 117 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 118 | va_start(ap, msg); |
| 119 | vfprintf(stderr, msg, ap); |
| 120 | va_end(ap); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 121 | |
Chris Allegretta | 32da456 | 2002-01-02 15:12:21 +0000 | [diff] [blame] | 122 | /* save the currently loaded file if it's been modified */ |
| 123 | if (ISSET(MODIFIED)) |
| 124 | die_save_file(filename); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 125 | |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 126 | #ifdef ENABLE_MULTIBUFFER |
Chris Allegretta | 32da456 | 2002-01-02 15:12:21 +0000 | [diff] [blame] | 127 | /* then save all of the other modified loaded files, if any */ |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 128 | if (open_files != NULL) { |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 129 | openfilestruct *tmp; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 130 | |
| 131 | tmp = open_files; |
| 132 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 133 | while (open_files->prev != NULL) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 134 | open_files = open_files->prev; |
| 135 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 136 | while (open_files->next != NULL) { |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 137 | |
David Lawrence Ramsey | a3370c4 | 2004-04-05 01:08:14 +0000 | [diff] [blame] | 138 | /* if we already saved the file above (i.e, if it was the |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 139 | currently loaded file), don't save it again */ |
| 140 | if (tmp != open_files) { |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 141 | /* make sure open_files->fileage and fileage, and |
| 142 | open_files->filebot and filebot, are in sync; they |
| 143 | might not be if lines have been cut from the top or |
| 144 | bottom of the file */ |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 145 | fileage = open_files->fileage; |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 146 | filebot = open_files->filebot; |
Chris Allegretta | 32da456 | 2002-01-02 15:12:21 +0000 | [diff] [blame] | 147 | /* save the file if it's been modified */ |
Chris Allegretta | 4dc03d5 | 2002-05-11 03:04:44 +0000 | [diff] [blame] | 148 | if (open_files->file_flags & MODIFIED) |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 149 | die_save_file(open_files->filename); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 150 | } |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 151 | open_files = open_files->next; |
| 152 | } |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 153 | } |
| 154 | #endif |
| 155 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 156 | exit(1); /* We have a problem: exit w/ errorlevel(1) */ |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 157 | } |
| 158 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 159 | void die_save_file(const char *die_filename) |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 160 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 161 | char *ret; |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 162 | bool failed = TRUE; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 163 | |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 164 | /* If we're using restricted mode, don't write any emergency backup |
| 165 | * files, since that would allow reading from or writing to files |
| 166 | * not specified on the command line. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 167 | if (ISSET(RESTRICTED)) |
| 168 | return; |
| 169 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 170 | /* If we can't save, we have REAL bad problems, but we might as well |
| 171 | TRY. */ |
| 172 | if (die_filename[0] == '\0') |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 173 | die_filename = "nano"; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 174 | |
David Lawrence Ramsey | 049e4a5 | 2004-08-10 23:05:59 +0000 | [diff] [blame] | 175 | ret = get_next_filename(die_filename); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 176 | if (ret[0] != '\0') |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 177 | failed = -1 == write_file(ret, TRUE, FALSE, TRUE); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 178 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 179 | if (!failed) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 180 | fprintf(stderr, _("\nBuffer written to %s\n"), ret); |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 181 | else |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 182 | fprintf(stderr, _("\nNo %s written (too many backup files?)\n"), ret); |
Chris Allegretta | 48b0670 | 2002-02-22 04:30:50 +0000 | [diff] [blame] | 183 | |
| 184 | free(ret); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Chris Allegretta | e61e830 | 2001-01-14 05:18:27 +0000 | [diff] [blame] | 187 | /* Die with an error message that the screen was too small if, well, the |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 188 | * screen is too small. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 189 | void die_too_small(void) |
Chris Allegretta | e61e830 | 2001-01-14 05:18:27 +0000 | [diff] [blame] | 190 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 191 | die(_("Window size is too small for nano...\n")); |
Chris Allegretta | e61e830 | 2001-01-14 05:18:27 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 194 | void print_view_warning(void) |
| 195 | { |
| 196 | statusbar(_("Key illegal in VIEW mode")); |
| 197 | } |
| 198 | |
David Lawrence Ramsey | f28f50e | 2004-01-09 23:04:55 +0000 | [diff] [blame] | 199 | /* Initialize global variables -- no better way for now. If |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 200 | * save_cutbuffer is TRUE, don't set cutbuffer to NULL. */ |
| 201 | void global_init(bool save_cutbuffer) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 202 | { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 203 | current_x = 0; |
| 204 | current_y = 0; |
Chris Allegretta | e61e830 | 2001-01-14 05:18:27 +0000 | [diff] [blame] | 205 | |
Chris Allegretta | f8f2d58 | 2003-02-10 02:43:48 +0000 | [diff] [blame] | 206 | editwinrows = LINES - 5 + no_help(); |
| 207 | if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS) |
Chris Allegretta | e61e830 | 2001-01-14 05:18:27 +0000 | [diff] [blame] | 208 | die_too_small(); |
| 209 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 210 | fileage = NULL; |
Chris Allegretta | 56214c6 | 2001-09-27 02:46:53 +0000 | [diff] [blame] | 211 | if (!save_cutbuffer) |
| 212 | cutbuffer = NULL; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 213 | current = NULL; |
| 214 | edittop = NULL; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 215 | totlines = 0; |
Chris Allegretta | 56214c6 | 2001-09-27 02:46:53 +0000 | [diff] [blame] | 216 | totsize = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 217 | placewewant = 0; |
Chris Allegretta | e61e830 | 2001-01-14 05:18:27 +0000 | [diff] [blame] | 218 | |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 219 | #ifndef DISABLE_WRAPJUSTIFY |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 220 | fill = wrap_at; |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 221 | if (fill <= 0) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 222 | fill += COLS; |
Chris Allegretta | f8f2d58 | 2003-02-10 02:43:48 +0000 | [diff] [blame] | 223 | if (fill < 0) |
| 224 | fill = 0; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 225 | #endif |
Chris Allegretta | e61e830 | 2001-01-14 05:18:27 +0000 | [diff] [blame] | 226 | |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 227 | hblank = charalloc(COLS + 1); |
Chris Allegretta | 0a06e07 | 2001-01-23 02:35:04 +0000 | [diff] [blame] | 228 | memset(hblank, ' ', COLS); |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 229 | hblank[COLS] = '\0'; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 230 | } |
| 231 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 232 | void window_init(void) |
| 233 | { |
Chris Allegretta | f8f2d58 | 2003-02-10 02:43:48 +0000 | [diff] [blame] | 234 | editwinrows = LINES - 5 + no_help(); |
| 235 | if (editwinrows < MIN_EDITOR_ROWS) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 236 | die_too_small(); |
| 237 | |
Chris Allegretta | 1a128af | 2003-01-26 04:15:56 +0000 | [diff] [blame] | 238 | if (topwin != NULL) |
| 239 | delwin(topwin); |
David Lawrence Ramsey | ce991bb | 2004-03-29 18:36:39 +0000 | [diff] [blame] | 240 | if (edit != NULL) |
| 241 | delwin(edit); |
Chris Allegretta | 1a128af | 2003-01-26 04:15:56 +0000 | [diff] [blame] | 242 | if (bottomwin != NULL) |
| 243 | delwin(bottomwin); |
| 244 | |
David Lawrence Ramsey | ce991bb | 2004-03-29 18:36:39 +0000 | [diff] [blame] | 245 | /* Set up the windows. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 246 | topwin = newwin(2, COLS, 0, 0); |
David Lawrence Ramsey | ce991bb | 2004-03-29 18:36:39 +0000 | [diff] [blame] | 247 | edit = newwin(editwinrows, COLS, 2, 0); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 248 | bottomwin = newwin(3 - no_help(), COLS, LINES - 3 + no_help(), 0); |
| 249 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 250 | /* Turn the keypad back on. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 251 | keypad(edit, TRUE); |
| 252 | keypad(bottomwin, TRUE); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 253 | } |
| 254 | |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 255 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 256 | void mouse_init(void) |
| 257 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 258 | if (ISSET(USE_MOUSE)) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 259 | mousemask(BUTTON1_RELEASED, NULL); |
| 260 | mouseinterval(50); |
| 261 | } else |
| 262 | mousemask(0, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 263 | } |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 264 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 265 | |
| 266 | #ifndef DISABLE_HELP |
| 267 | /* This function allocates help_text, and stores the help string in it. |
| 268 | * help_text should be NULL initially. */ |
| 269 | void help_init(void) |
| 270 | { |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 271 | size_t allocsize = 1; /* Space needed for help_text. */ |
| 272 | const char *htx; /* Untranslated help message. */ |
| 273 | char *ptr; |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 274 | const shortcut *s; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 275 | #ifndef NANO_SMALL |
| 276 | const toggle *t; |
| 277 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 278 | |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 279 | /* First, set up the initial help text for the current function. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 280 | if (currshortcut == whereis_list || currshortcut == replace_list |
| 281 | || currshortcut == replace_list_2) |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 282 | htx = N_("Search Command Help Text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 283 | "Enter the words or characters you would like to search " |
| 284 | "for, then hit enter. If there is a match for the text you " |
| 285 | "entered, the screen will be updated to the location of the " |
| 286 | "nearest match for the search string.\n\n " |
Chris Allegretta | 37d594c | 2003-01-05 22:00:41 +0000 | [diff] [blame] | 287 | "The previous search string will be shown in brackets after " |
| 288 | "the Search: prompt. Hitting Enter without entering any text " |
| 289 | "will perform the previous search.\n\n The following function " |
| 290 | "keys are available in Search mode:\n\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 291 | else if (currshortcut == goto_list) |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 292 | htx = N_("Go To Line Help Text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 293 | "Enter the line number that you wish to go to and hit " |
| 294 | "Enter. If there are fewer lines of text than the " |
| 295 | "number you entered, you will be brought to the last line " |
| 296 | "of the file.\n\n The following function keys are " |
| 297 | "available in Go To Line mode:\n\n"); |
| 298 | else if (currshortcut == insertfile_list) |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 299 | htx = N_("Insert File Help Text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 300 | "Type in the name of a file to be inserted into the current " |
| 301 | "file buffer at the current cursor location.\n\n " |
| 302 | "If you have compiled nano with multiple file buffer " |
| 303 | "support, and enable multiple buffers with the -F " |
| 304 | "or --multibuffer command line flags, the Meta-F toggle, or " |
| 305 | "a nanorc file, inserting a file will cause it to be " |
| 306 | "loaded into a separate buffer (use Meta-< and > to switch " |
| 307 | "between file buffers).\n\n If you need another blank " |
| 308 | "buffer, do not enter any filename, or type in a " |
| 309 | "nonexistent filename at the prompt and press " |
| 310 | "Enter.\n\n The following function keys are " |
| 311 | "available in Insert File mode:\n\n"); |
| 312 | else if (currshortcut == writefile_list) |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 313 | htx = N_("Write File Help Text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 314 | "Type the name that you wish to save the current file " |
| 315 | "as and hit Enter to save the file.\n\n If you have " |
| 316 | "selected text with Ctrl-^, you will be prompted to " |
| 317 | "save only the selected portion to a separate file. To " |
| 318 | "reduce the chance of overwriting the current file with " |
| 319 | "just a portion of it, the current filename is not the " |
| 320 | "default in this mode.\n\n The following function keys " |
| 321 | "are available in Write File mode:\n\n"); |
| 322 | #ifndef DISABLE_BROWSER |
| 323 | else if (currshortcut == browser_list) |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 324 | htx = N_("File Browser Help Text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 325 | "The file browser is used to visually browse the " |
| 326 | "directory structure to select a file for reading " |
| 327 | "or writing. You may use the arrow keys or Page Up/" |
| 328 | "Down to browse through the files, and S or Enter to " |
| 329 | "choose the selected file or enter the selected " |
| 330 | "directory. To move up one level, select the directory " |
| 331 | "called \"..\" at the top of the file list.\n\n The " |
| 332 | "following function keys are available in the file " |
| 333 | "browser:\n\n"); |
| 334 | else if (currshortcut == gotodir_list) |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 335 | htx = N_("Browser Go To Directory Help Text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 336 | "Enter the name of the directory you would like to " |
| 337 | "browse to.\n\n If tab completion has not been disabled, " |
| 338 | "you can use the TAB key to (attempt to) automatically " |
| 339 | "complete the directory name.\n\n The following function " |
| 340 | "keys are available in Browser Go To Directory mode:\n\n"); |
| 341 | #endif |
Chris Allegretta | 201f1d9 | 2003-02-05 02:51:19 +0000 | [diff] [blame] | 342 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 343 | else if (currshortcut == spell_list) |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 344 | htx = N_("Spell Check Help Text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 345 | "The spell checker checks the spelling of all text " |
| 346 | "in the current file. When an unknown word is " |
| 347 | "encountered, it is highlighted and a replacement can " |
| 348 | "be edited. It will then prompt to replace every " |
| 349 | "instance of the given misspelled word in the " |
| 350 | "current file.\n\n The following other functions are " |
| 351 | "available in Spell Check mode:\n\n"); |
Chris Allegretta | 201f1d9 | 2003-02-05 02:51:19 +0000 | [diff] [blame] | 352 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 353 | #ifndef NANO_SMALL |
| 354 | else if (currshortcut == extcmd_list) |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 355 | htx = N_("External Command Help Text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 356 | "This menu allows you to insert the output of a command " |
| 357 | "run by the shell into the current buffer (or a new " |
| 358 | "buffer in multibuffer mode).\n\n The following keys are " |
| 359 | "available in this mode:\n\n"); |
| 360 | #endif |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 361 | else |
| 362 | /* Default to the main help list. */ |
| 363 | htx = N_(" nano help text\n\n " |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 364 | "The nano editor is designed to emulate the functionality and " |
| 365 | "ease-of-use of the UW Pico text editor. There are four main " |
| 366 | "sections of the editor: The top line shows the program " |
| 367 | "version, the current filename being edited, and whether " |
| 368 | "or not the file has been modified. Next is the main editor " |
| 369 | "window showing the file being edited. The status line is " |
| 370 | "the third line from the bottom and shows important messages. " |
| 371 | "The bottom two lines show the most commonly used shortcuts " |
| 372 | "in the editor.\n\n " |
| 373 | "The notation for shortcuts is as follows: Control-key " |
David Lawrence Ramsey | aaad3af | 2003-08-31 16:44:10 +0000 | [diff] [blame] | 374 | "sequences are notated with a caret (^) symbol and can be " |
| 375 | "entered either by using the Control (Ctrl) key or pressing the " |
| 376 | "Esc key twice. Escape-key sequences are notated with the Meta " |
| 377 | "(M) symbol and can be entered using either the Esc, Alt or " |
| 378 | "Meta key depending on your keyboard setup. Also, pressing Esc " |
| 379 | "twice and then typing a three-digit number from 000 to 255 " |
| 380 | "will enter the character with the corresponding ASCII code. " |
| 381 | "The following keystrokes are available in the main editor " |
| 382 | "window. Alternative keys are shown in parentheses:\n\n"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 383 | |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 384 | htx = _(htx); |
| 385 | |
| 386 | allocsize += strlen(htx); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 387 | |
| 388 | /* The space needed for the shortcut lists, at most COLS characters, |
| 389 | * plus '\n'. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 390 | allocsize += (COLS < 21 ? 21 : COLS + 1) * length_of_list(currshortcut); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 391 | |
| 392 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 393 | /* If we're on the main list, we also count the toggle help text. |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 394 | * Each line has "M-%c\t\t\t", which fills 24 columns, plus a space, |
| 395 | * plus translated text, plus '\n'. */ |
Chris Allegretta | 3a78406 | 2003-02-10 02:32:58 +0000 | [diff] [blame] | 396 | if (currshortcut == main_list) { |
| 397 | size_t endislen = strlen(_("enable/disable")); |
| 398 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 399 | for (t = toggles; t != NULL; t = t->next) |
Chris Allegretta | 3a78406 | 2003-02-10 02:32:58 +0000 | [diff] [blame] | 400 | allocsize += 8 + strlen(t->desc) + endislen; |
| 401 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 402 | #endif /* !NANO_SMALL */ |
| 403 | |
| 404 | /* help_text has been freed and set to NULL unless the user resized |
| 405 | * while in the help screen. */ |
| 406 | free(help_text); |
| 407 | |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 408 | /* Allocate space for the help text. */ |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 409 | help_text = charalloc(allocsize); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 410 | |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 411 | /* Now add the text we want. */ |
| 412 | strcpy(help_text, htx); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 413 | ptr = help_text + strlen(help_text); |
| 414 | |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 415 | /* Now add our shortcut info. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 416 | for (s = currshortcut; s != NULL; s = s->next) { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 417 | bool meta_shortcut = FALSE; |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 418 | /* TRUE if the character in s->metaval is shown in the |
| 419 | * first column. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 420 | |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 421 | if (s->ctrlval != NANO_NO_KEY) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 422 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 423 | if (s->ctrlval == NANO_HISTORY_KEY) |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 424 | ptr += sprintf(ptr, "%.7s", _("Up")); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 425 | else |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 426 | #endif |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 427 | if (s->ctrlval == NANO_CONTROL_SPACE) |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 428 | ptr += sprintf(ptr, "^%.6s", _("Space")); |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 429 | else if (s->ctrlval == NANO_CONTROL_8) |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 430 | ptr += sprintf(ptr, "^?"); |
| 431 | else |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 432 | ptr += sprintf(ptr, "^%c", s->ctrlval + 64); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 433 | } |
| 434 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 435 | else if (s->metaval != NANO_NO_KEY) { |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 436 | meta_shortcut = TRUE; |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 437 | if (s->metaval == NANO_ALT_SPACE) |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 438 | ptr += sprintf(ptr, "M-%.5s", _("Space")); |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 439 | else |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 440 | ptr += sprintf(ptr, "M-%c", toupper(s->metaval)); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 441 | } |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 442 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 443 | |
| 444 | *(ptr++) = '\t'; |
| 445 | |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 446 | if (s->funcval != NANO_NO_KEY) |
| 447 | ptr += sprintf(ptr, "(F%d)", s->funcval - KEY_F0); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 448 | |
| 449 | *(ptr++) = '\t'; |
| 450 | |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 451 | if (!meta_shortcut && s->metaval != NANO_NO_KEY) |
| 452 | ptr += sprintf(ptr, "(M-%c)", toupper(s->metaval)); |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 453 | else if (meta_shortcut && s->miscval != NANO_NO_KEY) |
| 454 | ptr += sprintf(ptr, "(M-%c)", toupper(s->miscval)); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 455 | |
| 456 | *(ptr++) = '\t'; |
| 457 | |
| 458 | assert(s->help != NULL); |
Chris Allegretta | 3a78406 | 2003-02-10 02:32:58 +0000 | [diff] [blame] | 459 | ptr += sprintf(ptr, "%.*s\n", COLS > 24 ? COLS - 24 : 0, s->help); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | #ifndef NANO_SMALL |
| 463 | /* And the toggles... */ |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 464 | if (currshortcut == main_list) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 465 | for (t = toggles; t != NULL; t = t->next) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 466 | assert(t->desc != NULL); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 467 | ptr += sprintf(ptr, "M-%c\t\t\t%s %s\n", toupper(t->val), t->desc, |
Chris Allegretta | 3a78406 | 2003-02-10 02:32:58 +0000 | [diff] [blame] | 468 | _("enable/disable")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 469 | } |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 470 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 471 | #endif /* !NANO_SMALL */ |
| 472 | |
| 473 | /* If all went well, we didn't overwrite the allocated space for |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 474 | * help_text. */ |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 475 | assert(strlen(help_text) < allocsize); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 476 | } |
| 477 | #endif |
| 478 | |
| 479 | /* Create a new filestruct node. Note that we specifically do not set |
| 480 | * prevnode->next equal to the new line. */ |
| 481 | filestruct *make_new_node(filestruct *prevnode) |
| 482 | { |
| 483 | filestruct *newnode = (filestruct *)nmalloc(sizeof(filestruct)); |
| 484 | |
| 485 | newnode->data = NULL; |
| 486 | newnode->prev = prevnode; |
| 487 | newnode->next = NULL; |
| 488 | newnode->lineno = prevnode != NULL ? prevnode->lineno + 1 : 1; |
| 489 | |
| 490 | return newnode; |
| 491 | } |
| 492 | |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 493 | /* Make a copy of a node to a pointer (space will be malloc()ed). */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 494 | filestruct *copy_node(const filestruct *src) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 495 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 496 | filestruct *dst = (filestruct *)nmalloc(sizeof(filestruct)); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 497 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 498 | assert(src != NULL); |
| 499 | |
Chris Allegretta | 88b0915 | 2001-05-17 11:35:43 +0000 | [diff] [blame] | 500 | dst->data = charalloc(strlen(src->data) + 1); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 501 | dst->next = src->next; |
| 502 | dst->prev = src->prev; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 503 | strcpy(dst->data, src->data); |
| 504 | dst->lineno = src->lineno; |
| 505 | |
| 506 | return dst; |
| 507 | } |
| 508 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 509 | /* Splice a node into an existing filestruct. */ |
David Lawrence Ramsey | f708037 | 2004-07-08 17:15:10 +0000 | [diff] [blame] | 510 | void splice_node(filestruct *begin, filestruct *newnode, filestruct |
| 511 | *end) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 512 | { |
| 513 | if (newnode != NULL) { |
| 514 | newnode->next = end; |
| 515 | newnode->prev = begin; |
| 516 | } |
| 517 | if (begin != NULL) |
| 518 | begin->next = newnode; |
| 519 | if (end != NULL) |
| 520 | end->prev = newnode; |
| 521 | } |
| 522 | |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 523 | /* Unlink a node from the rest of the filestruct. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 524 | void unlink_node(const filestruct *fileptr) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 525 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 526 | assert(fileptr != NULL); |
| 527 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 528 | if (fileptr->prev != NULL) |
| 529 | fileptr->prev->next = fileptr->next; |
| 530 | |
| 531 | if (fileptr->next != NULL) |
| 532 | fileptr->next->prev = fileptr->prev; |
| 533 | } |
| 534 | |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 535 | /* Delete a node from the filestruct. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 536 | void delete_node(filestruct *fileptr) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 537 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 538 | if (fileptr != NULL) { |
| 539 | if (fileptr->data != NULL) |
| 540 | free(fileptr->data); |
| 541 | free(fileptr); |
| 542 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 543 | } |
| 544 | |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 545 | /* Okay, now let's duplicate a whole struct! */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 546 | filestruct *copy_filestruct(const filestruct *src) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 547 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 548 | filestruct *head; /* copy of src, top of the copied list */ |
| 549 | filestruct *prev; /* temp that traverses the list */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 550 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 551 | assert(src != NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 552 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 553 | prev = copy_node(src); |
| 554 | prev->prev = NULL; |
| 555 | head = prev; |
| 556 | src = src->next; |
| 557 | while (src != NULL) { |
| 558 | prev->next = copy_node(src); |
| 559 | prev->next->prev = prev; |
| 560 | prev = prev->next; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 561 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 562 | src = src->next; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 563 | } |
| 564 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 565 | prev->next = NULL; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 566 | return head; |
| 567 | } |
| 568 | |
Chris Allegretta | f2387fb | 2002-04-10 02:31:20 +0000 | [diff] [blame] | 569 | /* Frees a filestruct. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 570 | void free_filestruct(filestruct *src) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 571 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 572 | if (src != NULL) { |
| 573 | while (src->next != NULL) { |
| 574 | src = src->next; |
| 575 | delete_node(src->prev); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 576 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 577 | fprintf(stderr, "%s: free'd a node, YAY!\n", "delete_node()"); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 578 | #endif |
| 579 | } |
| 580 | delete_node(src); |
| 581 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 582 | fprintf(stderr, "%s: free'd last node.\n", "delete_node()"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 583 | #endif |
| 584 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 587 | void renumber_all(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 588 | { |
| 589 | filestruct *temp; |
Chris Allegretta | bef1297 | 2002-03-06 03:30:40 +0000 | [diff] [blame] | 590 | int i = 1; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 591 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 592 | assert(fileage == NULL || fileage != fileage->next); |
| 593 | for (temp = fileage; temp != NULL; temp = temp->next) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 594 | temp->lineno = i++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 595 | } |
| 596 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 597 | void renumber(filestruct *fileptr) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 598 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 599 | if (fileptr == NULL || fileptr->prev == NULL || fileptr == fileage) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 600 | renumber_all(); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 601 | else { |
| 602 | int lineno = fileptr->prev->lineno; |
| 603 | |
| 604 | assert(fileptr != fileptr->next); |
| 605 | for (; fileptr != NULL; fileptr = fileptr->next) |
| 606 | fileptr->lineno = ++lineno; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 607 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 608 | } |
| 609 | |
David Lawrence Ramsey | 22fac78 | 2004-08-05 15:16:19 +0000 | [diff] [blame] | 610 | /* Print one usage string to the screen. This cuts down on duplicate |
| 611 | * strings to translate and leaves out the parts that shouldn't be |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 612 | * translatable (the flag names). */ |
David Lawrence Ramsey | f708037 | 2004-07-08 17:15:10 +0000 | [diff] [blame] | 613 | void print1opt(const char *shortflag, const char *longflag, const char |
| 614 | *desc) |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 615 | { |
| 616 | printf(" %s\t", shortflag); |
| 617 | if (strlen(shortflag) < 8) |
| 618 | printf("\t"); |
| 619 | |
| 620 | #ifdef HAVE_GETOPT_LONG |
| 621 | printf("%s\t", longflag); |
| 622 | if (strlen(longflag) < 8) |
| 623 | printf("\t\t"); |
| 624 | else if (strlen(longflag) < 16) |
| 625 | printf("\t"); |
| 626 | #endif |
| 627 | |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 628 | printf("%s\n", _(desc)); |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 629 | } |
| 630 | |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 631 | void usage(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 632 | { |
| 633 | #ifdef HAVE_GETOPT_LONG |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 634 | printf(_("Usage: nano [+LINE] [GNU long option] [option] [file]\n\n")); |
| 635 | printf(_("Option\t\tLong option\t\tMeaning\n")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 636 | #else |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 637 | printf(_("Usage: nano [+LINE] [option] [file]\n\n")); |
| 638 | printf(_("Option\t\tMeaning\n")); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 639 | #endif |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 640 | |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 641 | print1opt("-h, -?", "--help", N_("Show this message")); |
| 642 | print1opt(_("+LINE"), "", N_("Start at line number LINE")); |
Chris Allegretta | 7004c28 | 2001-09-22 00:42:10 +0000 | [diff] [blame] | 643 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 644 | print1opt("-A", "--smarthome", N_("Enable smart home key")); |
| 645 | print1opt("-B", "--backup", N_("Backup existing files on save")); |
| 646 | print1opt("-D", "--dos", N_("Write file in DOS format")); |
| 647 | print1opt(_("-E [dir]"), _("--backupdir=[dir]"), N_("Directory for writing backup files")); |
Chris Allegretta | 7004c28 | 2001-09-22 00:42:10 +0000 | [diff] [blame] | 648 | #endif |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 649 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 650 | print1opt("-F", "--multibuffer", N_("Enable multiple file buffers")); |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 651 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 652 | #ifdef ENABLE_NANORC |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 653 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 654 | print1opt("-H", "--historylog", N_("Log & read search/replace string history")); |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 655 | #endif |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 656 | print1opt("-I", "--ignorercfiles", N_("Don't look at nanorc files")); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 657 | #endif |
Chris Allegretta | 8fa1e28 | 2001-09-22 04:20:25 +0000 | [diff] [blame] | 658 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 659 | print1opt("-M", "--mac", N_("Write file in Mac format")); |
| 660 | print1opt("-N", "--noconvert", N_("Don't convert files from DOS/Mac format")); |
Chris Allegretta | 8fa1e28 | 2001-09-22 04:20:25 +0000 | [diff] [blame] | 661 | #endif |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 662 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 663 | print1opt(_("-Q [str]"), _("--quotestr=[str]"), N_("Quoting string, default \"> \"")); |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 664 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 665 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 666 | print1opt("-R", "--regexp", N_("Do regular expression searches")); |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 667 | #endif |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 668 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 669 | print1opt("-S", "--smooth", N_("Smooth scrolling")); |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 670 | #endif |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 671 | print1opt(_("-T [#cols]"), _("--tabsize=[#cols]"), N_("Set width of a tab in cols to #cols")); |
| 672 | print1opt("-V", "--version", N_("Print version information and exit")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 673 | #ifdef ENABLE_COLOR |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 674 | print1opt(_("-Y [str]"), _("--syntax [str]"), N_("Syntax definition to use")); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 675 | #endif |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 676 | print1opt("-Z", "--restricted", N_("Restricted mode")); |
| 677 | print1opt("-c", "--const", N_("Constantly show cursor position")); |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 678 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 679 | print1opt("-d", "--rebinddelete", N_("Fix Backspace/Delete confusion problem")); |
| 680 | print1opt("-i", "--autoindent", N_("Automatically indent new lines")); |
| 681 | print1opt("-k", "--cut", N_("Cut from cursor to end of line")); |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 682 | #endif |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 683 | print1opt("-l", "--nofollow", N_("Don't follow symbolic links, overwrite")); |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 684 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 685 | print1opt("-m", "--mouse", N_("Enable mouse")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 686 | #endif |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 687 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 688 | print1opt(_("-o [dir]"), _("--operatingdir=[dir]"), N_("Set operating directory")); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 689 | #endif |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 690 | print1opt("-p", "--preserve", N_("Preserve XON (^Q) and XOFF (^S) keys")); |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 691 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 692 | print1opt(_("-r [#cols]"), _("--fill=[#cols]"), N_("Set fill cols to (wrap lines at) #cols")); |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 693 | #endif |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 694 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 695 | print1opt(_("-s [prog]"), _("--speller=[prog]"), N_("Enable alternate speller")); |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 696 | #endif |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 697 | print1opt("-t", "--tempfile", N_("Auto save on exit, don't prompt")); |
| 698 | print1opt("-v", "--view", N_("View (read only) mode")); |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 699 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 700 | print1opt("-w", "--nowrap", N_("Don't wrap long lines")); |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 701 | #endif |
David Lawrence Ramsey | 76f63ff | 2004-08-05 15:45:09 +0000 | [diff] [blame] | 702 | print1opt("-x", "--nohelp", N_("Don't show help window")); |
| 703 | print1opt("-z", "--suspend", N_("Enable suspend")); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 704 | |
David Lawrence Ramsey | 22fac78 | 2004-08-05 15:16:19 +0000 | [diff] [blame] | 705 | /* This is a special case. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 706 | printf(" %s\t\t\t%s\n","-a, -b, -e, -f, -g, -j", _("(ignored, for Pico compatibility)")); |
Chris Allegretta | 3fc5d57 | 2002-03-09 18:51:58 +0000 | [diff] [blame] | 707 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 708 | exit(0); |
| 709 | } |
| 710 | |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 711 | void version(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 712 | { |
Chris Allegretta | c46dd81 | 2001-02-14 14:28:27 +0000 | [diff] [blame] | 713 | printf(_(" GNU nano version %s (compiled %s, %s)\n"), |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 714 | VERSION, __TIME__, __DATE__); |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 715 | printf(_ |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 716 | (" Email: nano@nano-editor.org Web: http://www.nano-editor.org/")); |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 717 | printf(_("\n Compiled options:")); |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 718 | |
Chris Allegretta | e660037 | 2003-01-17 03:39:41 +0000 | [diff] [blame] | 719 | #ifndef ENABLE_NLS |
| 720 | printf(" --disable-nls"); |
| 721 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 722 | #ifdef DEBUG |
| 723 | printf(" --enable-debug"); |
| 724 | #endif |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 725 | #ifdef NANO_EXTRA |
| 726 | printf(" --enable-extra"); |
Jordi Mallach | 5285ca9 | 2002-01-17 12:40:33 +0000 | [diff] [blame] | 727 | #endif |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 728 | #ifdef NANO_SMALL |
| 729 | printf(" --enable-tiny"); |
| 730 | #else |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 731 | #ifdef DISABLE_BROWSER |
Chris Allegretta | 6636dc3 | 2001-01-05 05:41:07 +0000 | [diff] [blame] | 732 | printf(" --disable-browser"); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 733 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 734 | #ifdef DISABLE_HELP |
| 735 | printf(" --disable-help"); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 736 | #endif |
| 737 | #ifdef DISABLE_JUSTIFY |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 738 | printf(" --disable-justify"); |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 739 | #endif |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 740 | #ifdef DISABLE_MOUSE |
Chris Allegretta | 84de552 | 2001-04-12 14:51:48 +0000 | [diff] [blame] | 741 | printf(" --disable-mouse"); |
Chris Allegretta | b7d00ef | 2000-12-18 05:36:51 +0000 | [diff] [blame] | 742 | #endif |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 743 | #ifdef DISABLE_OPERATINGDIR |
| 744 | printf(" --disable-operatingdir"); |
| 745 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 746 | #ifdef DISABLE_SPELLER |
| 747 | printf(" --disable-speller"); |
| 748 | #endif |
| 749 | #ifdef DISABLE_TABCOMP |
| 750 | printf(" --disable-tabcomp"); |
| 751 | #endif |
Chris Allegretta | 84de552 | 2001-04-12 14:51:48 +0000 | [diff] [blame] | 752 | #endif /* NANO_SMALL */ |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 753 | #ifdef DISABLE_WRAPPING |
| 754 | printf(" --disable-wrapping"); |
| 755 | #endif |
David Lawrence Ramsey | dc60b72 | 2002-10-25 16:08:53 +0000 | [diff] [blame] | 756 | #ifdef DISABLE_ROOTWRAP |
| 757 | printf(" --disable-wrapping-as-root"); |
| 758 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 759 | #ifdef ENABLE_COLOR |
| 760 | printf(" --enable-color"); |
| 761 | #endif |
| 762 | #ifdef ENABLE_MULTIBUFFER |
| 763 | printf(" --enable-multibuffer"); |
| 764 | #endif |
| 765 | #ifdef ENABLE_NANORC |
| 766 | printf(" --enable-nanorc"); |
| 767 | #endif |
Chris Allegretta | 8a0de3b | 2000-11-24 20:45:14 +0000 | [diff] [blame] | 768 | #ifdef USE_SLANG |
| 769 | printf(" --with-slang"); |
| 770 | #endif |
| 771 | printf("\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 772 | } |
| 773 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 774 | int no_help(void) |
| 775 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 776 | return ISSET(NO_HELP) ? 2 : 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 777 | } |
| 778 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 779 | void nano_disabled_msg(void) |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 780 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 781 | statusbar(_("Sorry, support for this function has been disabled")); |
Chris Allegretta | ff269f8 | 2000-12-01 18:46:01 +0000 | [diff] [blame] | 782 | } |
| 783 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 784 | #ifndef NANO_SMALL |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 785 | RETSIGTYPE cancel_fork(int signal) |
| 786 | { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 787 | if (kill(pid, SIGKILL) == -1) |
| 788 | nperror("kill"); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 789 | } |
| 790 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 791 | /* Return TRUE on success. */ |
| 792 | bool open_pipe(const char *command) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 793 | { |
| 794 | int fd[2]; |
| 795 | FILE *f; |
| 796 | struct sigaction oldaction, newaction; |
David Lawrence Ramsey | 22fac78 | 2004-08-05 15:16:19 +0000 | [diff] [blame] | 797 | /* Original and temporary handlers for |
| 798 | * SIGINT. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 799 | bool sig_failed = FALSE; |
| 800 | /* sig_failed means that sigaction() failed without changing the |
| 801 | * signal handlers. |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 802 | * |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 803 | * We use this variable since it is important to put things back |
| 804 | * when we finish, even if we get errors. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 805 | |
| 806 | /* Make our pipes. */ |
| 807 | |
| 808 | if (pipe(fd) == -1) { |
| 809 | statusbar(_("Could not pipe")); |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 810 | return FALSE; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | /* Fork a child. */ |
| 814 | |
| 815 | if ((pid = fork()) == 0) { |
| 816 | close(fd[0]); |
| 817 | dup2(fd[1], fileno(stdout)); |
| 818 | dup2(fd[1], fileno(stderr)); |
| 819 | /* If execl() returns at all, there was an error. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 820 | |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 821 | execl("/bin/sh", "sh", "-c", command, 0); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 822 | exit(0); |
| 823 | } |
| 824 | |
| 825 | /* Else continue as parent. */ |
| 826 | |
| 827 | close(fd[1]); |
| 828 | |
| 829 | if (pid == -1) { |
| 830 | close(fd[0]); |
| 831 | statusbar(_("Could not fork")); |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 832 | return FALSE; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | /* Before we start reading the forked command's output, we set |
| 836 | * things up so that ^C will cancel the new process. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 837 | |
David Lawrence Ramsey | e608f94 | 2004-05-19 16:04:27 +0000 | [diff] [blame] | 838 | /* Enable interpretation of the special control keys so that we get |
| 839 | * SIGINT when Ctrl-C is pressed. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 840 | enable_signals(); |
| 841 | |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 842 | if (sigaction(SIGINT, NULL, &newaction) == -1) { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 843 | sig_failed = TRUE; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 844 | nperror("sigaction"); |
| 845 | } else { |
| 846 | newaction.sa_handler = cancel_fork; |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 847 | if (sigaction(SIGINT, &newaction, &oldaction) == -1) { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 848 | sig_failed = TRUE; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 849 | nperror("sigaction"); |
| 850 | } |
| 851 | } |
| 852 | /* Note that now oldaction is the previous SIGINT signal handler, |
| 853 | * to be restored later. */ |
| 854 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 855 | f = fdopen(fd[0], "rb"); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 856 | if (f == NULL) |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 857 | nperror("fdopen"); |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 858 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 859 | read_file(f, "stdin", FALSE); |
David Lawrence Ramsey | 22fac78 | 2004-08-05 15:16:19 +0000 | [diff] [blame] | 860 | /* If multibuffer mode is on, we could be here in view mode. If so, |
| 861 | * don't set the modification flag. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 862 | if (!ISSET(VIEW_MODE)) |
| 863 | set_modified(); |
| 864 | |
| 865 | if (wait(NULL) == -1) |
| 866 | nperror("wait"); |
| 867 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 868 | if (!sig_failed && sigaction(SIGINT, &oldaction, NULL) == -1) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 869 | nperror("sigaction"); |
| 870 | |
David Lawrence Ramsey | e608f94 | 2004-05-19 16:04:27 +0000 | [diff] [blame] | 871 | /* Disable interpretation of the special control keys so that we can |
| 872 | * use Ctrl-C for other things. */ |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 873 | disable_signals(); |
| 874 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 875 | return TRUE; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 876 | } |
David Lawrence Ramsey | a9cd41c | 2004-03-05 19:54:58 +0000 | [diff] [blame] | 877 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 878 | |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 879 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 880 | void do_mouse(void) |
| 881 | { |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 882 | int mouse_x, mouse_y; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 883 | |
David Lawrence Ramsey | 4d44d2d | 2004-08-01 22:35:31 +0000 | [diff] [blame] | 884 | if (!get_mouseinput(&mouse_x, &mouse_y, TRUE)) { |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 885 | /* Click in the edit window to move the cursor, but only when |
| 886 | we're not in a subfunction. */ |
| 887 | if (wenclose(edit, mouse_y, mouse_x) && currshortcut == main_list) { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 888 | bool sameline; |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 889 | /* Did they click on the line with the cursor? If they |
| 890 | clicked on the cursor, we set the mark. */ |
| 891 | size_t xcur; |
| 892 | /* The character they clicked on. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 893 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 894 | /* Subtract out size of topwin. Perhaps we need a constant |
| 895 | somewhere? */ |
| 896 | mouse_y -= 2; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 897 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 898 | sameline = (mouse_y == current_y); |
Chris Allegretta | e92a7bc | 2003-01-28 01:36:38 +0000 | [diff] [blame] | 899 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 900 | /* Move to where the click occurred. */ |
| 901 | for (; current_y < mouse_y && current->next != NULL; current_y++) |
| 902 | current = current->next; |
| 903 | for (; current_y > mouse_y && current->prev != NULL; current_y--) |
| 904 | current = current->prev; |
Chris Allegretta | e92a7bc | 2003-01-28 01:36:38 +0000 | [diff] [blame] | 905 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 906 | xcur = actual_x(current->data, get_page_start(xplustabs()) + |
| 907 | mouse_x); |
Chris Allegretta | e92a7bc | 2003-01-28 01:36:38 +0000 | [diff] [blame] | 908 | |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 909 | #ifndef NANO_SMALL |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 910 | /* Selecting where the cursor is toggles the mark, as does |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 911 | selecting beyond the line length with the cursor at the |
| 912 | end of the line. */ |
| 913 | if (sameline && xcur == current_x) { |
| 914 | if (ISSET(VIEW_MODE)) { |
| 915 | print_view_warning(); |
| 916 | return; |
| 917 | } |
| 918 | do_mark(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 919 | } |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 920 | #endif |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 921 | |
| 922 | current_x = xcur; |
| 923 | placewewant = xplustabs(); |
| 924 | edit_refresh(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 925 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 926 | } |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 927 | /* FIXME: If we clicked on a location in the statusbar, the cursor |
| 928 | should move to the location we clicked on. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 929 | } |
| 930 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 931 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 932 | /* The user typed a character; add it to the edit buffer. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 933 | void do_char(char ch) |
| 934 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 935 | size_t current_len = strlen(current->data); |
| 936 | #if !defined(DISABLE_WRAPPING) || defined(ENABLE_COLOR) |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 937 | bool do_refresh = FALSE; |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 938 | /* Do we have to call edit_refresh(), or can we get away with |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 939 | * update_line()? */ |
| 940 | #endif |
| 941 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 942 | if (ch == '\0') /* Null to newline, if needed. */ |
| 943 | ch = '\n'; |
| 944 | else if (ch == '\n') { /* Newline to Enter, if needed. */ |
| 945 | do_enter(); |
| 946 | return; |
| 947 | } |
| 948 | |
| 949 | assert(current != NULL && current->data != NULL); |
| 950 | |
| 951 | /* When a character is inserted on the current magicline, it means |
| 952 | * we need a new one! */ |
David Lawrence Ramsey | b977515 | 2004-03-19 02:15:42 +0000 | [diff] [blame] | 953 | if (filebot == current) |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 954 | new_magicline(); |
Robert Siemborski | 63b3d7e | 2000-07-04 22:15:39 +0000 | [diff] [blame] | 955 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 956 | /* More dangerousness fun =) */ |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 957 | current->data = charealloc(current->data, current_len + 2); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 958 | assert(current_x <= current_len); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 959 | charmove(¤t->data[current_x + 1], ¤t->data[current_x], |
| 960 | current_len - current_x + 1); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 961 | current->data[current_x] = ch; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 962 | totsize++; |
| 963 | set_modified(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 964 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 965 | #ifndef NANO_SMALL |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 966 | /* Note that current_x has not yet been incremented. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 967 | if (current == mark_beginbuf && current_x < mark_beginx) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 968 | mark_beginx++; |
Chris Allegretta | b2cd10d | 2002-01-20 00:54:42 +0000 | [diff] [blame] | 969 | #endif |
| 970 | |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 971 | do_right(FALSE); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 972 | |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 973 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 974 | /* If we're wrapping text, we need to call edit_refresh(). */ |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 975 | if (!ISSET(NO_WRAP) && ch != '\t') |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 976 | do_refresh = do_wrap(current); |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 977 | #endif |
| 978 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 979 | #ifdef ENABLE_COLOR |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 980 | /* If color syntaxes are turned on, we need to call |
| 981 | * edit_refresh(). */ |
David Lawrence Ramsey | 760a2dc | 2004-01-14 06:38:00 +0000 | [diff] [blame] | 982 | if (ISSET(COLOR_SYNTAX)) |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 983 | do_refresh = TRUE; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 984 | #endif |
| 985 | |
| 986 | #if !defined(DISABLE_WRAPPING) || defined(ENABLE_COLOR) |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 987 | if (do_refresh) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 988 | edit_refresh(); |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 989 | else |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 990 | #endif |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 991 | update_line(current, current_x); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 992 | } |
| 993 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 994 | void do_verbatim_input(void) |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 995 | { |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 996 | int *v_kbinput = NULL; /* Used to hold verbatim input. */ |
| 997 | size_t v_len; /* Length of verbatim input. */ |
David Lawrence Ramsey | 805547f | 2004-04-22 03:41:04 +0000 | [diff] [blame] | 998 | size_t i; |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 999 | |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1000 | statusbar(_("Verbatim input")); |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1001 | |
| 1002 | v_kbinput = get_verbatim_kbinput(edit, v_kbinput, &v_len, TRUE); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1003 | |
| 1004 | /* Turn on DISABLE_CURPOS while inserting character(s) and turn it |
| 1005 | * off afterwards, so that if constant cursor position display is |
| 1006 | * on, it will be updated properly. */ |
| 1007 | SET(DISABLE_CURPOS); |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1008 | for (i = 0; i < v_len; i++) |
| 1009 | do_char((char)v_kbinput[i]); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1010 | UNSET(DISABLE_CURPOS); |
| 1011 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 1012 | free(v_kbinput); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1015 | void do_backspace(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1016 | { |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 1017 | if (current != fileage || current_x > 0) { |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1018 | do_left(FALSE); |
David Lawrence Ramsey | d91ab6e | 2003-09-07 23:57:24 +0000 | [diff] [blame] | 1019 | do_delete(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1020 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1021 | } |
| 1022 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1023 | void do_delete(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1024 | { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1025 | bool do_refresh = FALSE; |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1026 | /* Do we have to call edit_refresh(), or can we get away with |
| 1027 | * update_line()? */ |
| 1028 | |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1029 | assert(current != NULL && current->data != NULL && current_x <= |
| 1030 | strlen(current->data)); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1031 | |
| 1032 | placewewant = xplustabs(); |
| 1033 | |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1034 | if (current->data[current_x] != '\0') { |
| 1035 | size_t linelen = strlen(current->data + current_x); |
| 1036 | |
| 1037 | assert(current_x < strlen(current->data)); |
| 1038 | |
| 1039 | /* Let's get dangerous. */ |
David Lawrence Ramsey | 9eff746 | 2003-09-16 02:04:00 +0000 | [diff] [blame] | 1040 | charmove(¤t->data[current_x], ¤t->data[current_x + 1], |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1041 | linelen); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1042 | |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1043 | null_at(¤t->data, linelen + current_x - 1); |
| 1044 | #ifndef NANO_SMALL |
| 1045 | if (current_x < mark_beginx && mark_beginbuf == current) |
| 1046 | mark_beginx--; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1047 | #endif |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1048 | } else if (current != filebot && (current->next != filebot || |
| 1049 | current->data[0] == '\0')) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1050 | /* We can delete the line before filebot only if it is blank: it |
David Lawrence Ramsey | 32d19ce | 2004-05-24 05:05:07 +0000 | [diff] [blame] | 1051 | * becomes the new magicline then. */ |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1052 | filestruct *foo = current->next; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1053 | |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1054 | assert(current_x == strlen(current->data)); |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1055 | |
| 1056 | /* If we're deleting at the end of a line, we need to call |
| 1057 | * edit_refresh(). */ |
| 1058 | if (current->data[current_x] == '\0') |
| 1059 | do_refresh = TRUE; |
| 1060 | |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1061 | current->data = charealloc(current->data, current_x + |
| 1062 | strlen(foo->data) + 1); |
| 1063 | strcpy(current->data + current_x, foo->data); |
| 1064 | #ifndef NANO_SMALL |
| 1065 | if (mark_beginbuf == current->next) { |
| 1066 | mark_beginx += current_x; |
| 1067 | mark_beginbuf = current; |
| 1068 | } |
| 1069 | #endif |
David Lawrence Ramsey | b977515 | 2004-03-19 02:15:42 +0000 | [diff] [blame] | 1070 | if (filebot == foo) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1071 | filebot = current; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1072 | |
| 1073 | unlink_node(foo); |
| 1074 | delete_node(foo); |
| 1075 | renumber(current); |
| 1076 | totlines--; |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 1077 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 604caf3 | 2004-04-19 02:44:13 +0000 | [diff] [blame] | 1078 | wrap_reset(); |
David Lawrence Ramsey | 00d7798 | 2004-08-07 21:27:37 +0000 | [diff] [blame] | 1079 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1080 | } else |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1081 | return; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1082 | |
| 1083 | totsize--; |
| 1084 | set_modified(); |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1085 | |
| 1086 | #ifdef ENABLE_COLOR |
| 1087 | /* If color syntaxes are turned on, we need to call |
| 1088 | * edit_refresh(). */ |
| 1089 | if (ISSET(COLOR_SYNTAX)) |
| 1090 | do_refresh = TRUE; |
| 1091 | #endif |
| 1092 | |
| 1093 | if (do_refresh) |
| 1094 | edit_refresh(); |
| 1095 | else |
| 1096 | update_line(current, current_x); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1097 | } |
| 1098 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1099 | void do_tab(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1100 | { |
| 1101 | do_char('\t'); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1102 | } |
| 1103 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1104 | /* Someone hits return *gasp!* */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1105 | void do_enter(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1106 | { |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1107 | filestruct *newnode = make_new_node(current); |
| 1108 | size_t extra = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1109 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1110 | assert(current != NULL && current->data != NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1111 | |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 1112 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1113 | /* Do auto-indenting, like the neolithic Turbo Pascal editor. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1114 | if (ISSET(AUTOINDENT)) { |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1115 | /* If we are breaking the line in the indentation, the new |
| 1116 | * indentation should have only current_x characters, and |
| 1117 | * current_x should not change. */ |
| 1118 | extra = indent_length(current->data); |
| 1119 | if (extra > current_x) |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 1120 | extra = current_x; |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 1121 | totsize += extra; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1122 | } |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1123 | #endif |
| 1124 | newnode->data = charalloc(strlen(current->data + current_x) + |
| 1125 | extra + 1); |
| 1126 | strcpy(&newnode->data[extra], current->data + current_x); |
| 1127 | #ifndef NANO_SMALL |
| 1128 | if (ISSET(AUTOINDENT)) |
| 1129 | strncpy(newnode->data, current->data, extra); |
| 1130 | #endif |
| 1131 | null_at(¤t->data, current_x); |
| 1132 | #ifndef NANO_SMALL |
| 1133 | if (current == mark_beginbuf && current_x < mark_beginx) { |
| 1134 | mark_beginbuf = newnode; |
| 1135 | mark_beginx += extra - current_x; |
| 1136 | } |
| 1137 | #endif |
| 1138 | current_x = extra; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1139 | |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1140 | if (current == filebot) |
Chris Allegretta | e316773 | 2001-03-18 16:59:34 +0000 | [diff] [blame] | 1141 | filebot = newnode; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1142 | splice_node(current, newnode, current->next); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1143 | |
| 1144 | totsize++; |
| 1145 | renumber(current); |
Chris Allegretta | e316773 | 2001-03-18 16:59:34 +0000 | [diff] [blame] | 1146 | current = newnode; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1147 | |
David Lawrence Ramsey | 228148b | 2004-05-27 20:09:52 +0000 | [diff] [blame] | 1148 | edit_refresh(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1149 | |
| 1150 | totlines++; |
| 1151 | set_modified(); |
Chris Allegretta | b0ae393 | 2000-06-15 23:39:14 +0000 | [diff] [blame] | 1152 | placewewant = xplustabs(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1153 | } |
| 1154 | |
Chris Allegretta | ad3f478 | 2001-10-02 03:54:13 +0000 | [diff] [blame] | 1155 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1156 | void do_next_word(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1157 | { |
David Lawrence Ramsey | 86e851b | 2004-07-28 20:46:25 +0000 | [diff] [blame] | 1158 | size_t old_pww = placewewant; |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1159 | const filestruct *current_save = current; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1160 | assert(current != NULL && current->data != NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1161 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1162 | /* Skip letters in this word first. */ |
| 1163 | while (current->data[current_x] != '\0' && |
David Lawrence Ramsey | 86e851b | 2004-07-28 20:46:25 +0000 | [diff] [blame] | 1164 | isalnum((int)current->data[current_x])) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1165 | current_x++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1166 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1167 | for (; current != NULL; current = current->next) { |
| 1168 | while (current->data[current_x] != '\0' && |
| 1169 | !isalnum((int)current->data[current_x])) |
| 1170 | current_x++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1171 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1172 | if (current->data[current_x] != '\0') |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1173 | break; |
| 1174 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1175 | current_x = 0; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1176 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1177 | if (current == NULL) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1178 | current = filebot; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1179 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1180 | placewewant = xplustabs(); |
Chris Allegretta | 9e2934f | 2000-12-01 23:49:48 +0000 | [diff] [blame] | 1181 | |
David Lawrence Ramsey | bf3c93e | 2004-03-13 19:42:58 +0000 | [diff] [blame] | 1182 | /* Refresh the screen. If current has run off the bottom, this |
| 1183 | * call puts it at the center line. */ |
David Lawrence Ramsey | c21790d | 2004-05-30 03:19:52 +0000 | [diff] [blame] | 1184 | edit_redraw(current_save, old_pww); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1185 | } |
| 1186 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1187 | /* The same thing for backwards. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1188 | void do_prev_word(void) |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1189 | { |
David Lawrence Ramsey | 86e851b | 2004-07-28 20:46:25 +0000 | [diff] [blame] | 1190 | size_t old_pww = placewewant; |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1191 | const filestruct *current_save = current; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1192 | assert(current != NULL && current->data != NULL); |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1193 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1194 | /* Skip letters in this word first. */ |
| 1195 | while (current_x >= 0 && isalnum((int)current->data[current_x])) |
| 1196 | current_x--; |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1197 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1198 | for (; current != NULL; current = current->prev) { |
| 1199 | while (current_x >= 0 && !isalnum((int)current->data[current_x])) |
| 1200 | current_x--; |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1201 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1202 | if (current_x >= 0) |
| 1203 | break; |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1204 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1205 | if (current->prev != NULL) |
| 1206 | current_x = strlen(current->prev->data); |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1207 | } |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1208 | |
David Lawrence Ramsey | 1b525e9 | 2004-05-24 02:35:02 +0000 | [diff] [blame] | 1209 | if (current == NULL) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1210 | current = fileage; |
| 1211 | current_x = 0; |
David Lawrence Ramsey | 1b525e9 | 2004-05-24 02:35:02 +0000 | [diff] [blame] | 1212 | } else { |
| 1213 | while (current_x > 0 && isalnum((int)current->data[current_x - 1])) |
| 1214 | current_x--; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1215 | } |
| 1216 | |
Chris Allegretta | 76e291b | 2001-10-14 19:05:10 +0000 | [diff] [blame] | 1217 | placewewant = xplustabs(); |
| 1218 | |
David Lawrence Ramsey | b6aa428 | 2004-03-14 01:42:17 +0000 | [diff] [blame] | 1219 | /* Refresh the screen. If current has run off the top, this call |
| 1220 | * puts it at the center line. */ |
David Lawrence Ramsey | c21790d | 2004-05-30 03:19:52 +0000 | [diff] [blame] | 1221 | edit_redraw(current_save, old_pww); |
Chris Allegretta | 6232d66 | 2002-05-12 19:52:15 +0000 | [diff] [blame] | 1222 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1223 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1224 | void do_mark(void) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1225 | { |
David Lawrence Ramsey | f03c78b | 2003-09-28 21:26:49 +0000 | [diff] [blame] | 1226 | TOGGLE(MARK_ISSET); |
| 1227 | if (ISSET(MARK_ISSET)) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1228 | statusbar(_("Mark Set")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1229 | mark_beginbuf = current; |
| 1230 | mark_beginx = current_x; |
| 1231 | } else { |
| 1232 | statusbar(_("Mark UNset")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1233 | edit_refresh(); |
| 1234 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1235 | } |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 1236 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1237 | |
David Lawrence Ramsey | c97acfb | 2003-09-10 20:08:00 +0000 | [diff] [blame] | 1238 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1239 | void wrap_reset(void) |
| 1240 | { |
David Lawrence Ramsey | 85529b3 | 2004-06-22 15:38:47 +0000 | [diff] [blame] | 1241 | same_line_wrap = FALSE; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1242 | } |
David Lawrence Ramsey | c97acfb | 2003-09-10 20:08:00 +0000 | [diff] [blame] | 1243 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1244 | |
Chris Allegretta | cef7fbb | 2001-04-02 05:36:08 +0000 | [diff] [blame] | 1245 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1246 | /* We wrap the given line. Precondition: we assume the cursor has been |
| 1247 | * moved forward since the last typed character. Return value: whether |
| 1248 | * we wrapped. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1249 | bool do_wrap(filestruct *inptr) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1250 | { |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1251 | size_t len = strlen(inptr->data); |
| 1252 | /* Length of the line we wrap. */ |
| 1253 | size_t i = 0; |
| 1254 | /* Generic loop variable. */ |
| 1255 | int wrap_loc = -1; |
| 1256 | /* Index of inptr->data where we wrap. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1257 | int word_back = -1; |
| 1258 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1259 | const char *indentation = NULL; |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1260 | /* Indentation to prepend to the new line. */ |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1261 | size_t indent_len = 0; /* strlen(indentation) */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1262 | #endif |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1263 | const char *after_break; /* Text after the wrap point. */ |
| 1264 | size_t after_break_len; /* strlen(after_break) */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1265 | bool wrapping = FALSE; /* Do we prepend to the next line? */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1266 | const char *wrap_line = NULL; |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1267 | /* The next line, minus indentation. */ |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1268 | size_t wrap_line_len = 0; /* strlen(wrap_line) */ |
| 1269 | char *newline = NULL; /* The line we create. */ |
| 1270 | size_t new_line_len = 0; /* Eventual length of newline. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1271 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1272 | /* There are three steps. First, we decide where to wrap. Then, we |
| 1273 | * create the new wrap line. Finally, we clean up. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1274 | |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1275 | /* Step 1, finding where to wrap. We are going to add a new line |
David Lawrence Ramsey | 89bb937 | 2004-05-29 16:47:52 +0000 | [diff] [blame] | 1276 | * after a whitespace character. In this step, we set wrap_loc as the |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1277 | * location of this replacement. |
| 1278 | * |
| 1279 | * Where should we break the line? We need the last "legal wrap point" |
| 1280 | * such that the last word before it ended at or before fill. If there |
| 1281 | * is no such point, we settle for the first legal wrap point. |
| 1282 | * |
David Lawrence Ramsey | 8d91199 | 2004-05-29 17:05:52 +0000 | [diff] [blame] | 1283 | * A "legal wrap point" is a whitespace character that is not followed |
| 1284 | * by whitespace. |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1285 | * |
| 1286 | * If there is no legal wrap point or we found the last character of the |
| 1287 | * line, we should return without wrapping. |
| 1288 | * |
| 1289 | * Note that the initial indentation does not count as a legal wrap |
| 1290 | * point if we are going to auto-indent! |
| 1291 | * |
David Lawrence Ramsey | 8d91199 | 2004-05-29 17:05:52 +0000 | [diff] [blame] | 1292 | * Note that the code below could be optimized, by not calling |
| 1293 | * strnlenpt() so often. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1294 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1295 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1296 | if (ISSET(AUTOINDENT)) |
| 1297 | i = indent_length(inptr->data); |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1298 | #endif |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1299 | wrap_line = inptr->data + i; |
David Lawrence Ramsey | 4e25410 | 2003-11-05 22:04:08 +0000 | [diff] [blame] | 1300 | for (; i < len; i++, wrap_line++) { |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1301 | /* Record where the last word ended. */ |
David Lawrence Ramsey | 9830d75 | 2004-05-13 17:19:54 +0000 | [diff] [blame] | 1302 | if (!isblank(*wrap_line)) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1303 | word_back = i; |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1304 | /* If we have found a "legal wrap point" and the current word |
| 1305 | * extends too far, then we stop. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1306 | if (wrap_loc != -1 && strnlenpt(inptr->data, word_back + 1) > fill) |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1307 | break; |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1308 | /* We record the latest "legal wrap point". */ |
David Lawrence Ramsey | 9830d75 | 2004-05-13 17:19:54 +0000 | [diff] [blame] | 1309 | if (word_back != i && !isblank(wrap_line[1])) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1310 | wrap_loc = i; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1311 | } |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1312 | if (i == len) |
| 1313 | return FALSE; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1314 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1315 | /* Step 2, making the new wrap line. It will consist of indentation |
| 1316 | * + after_break + " " + wrap_line (although indentation and |
| 1317 | * wrap_line are conditional on flags and #defines). */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1318 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1319 | /* after_break is the text that will be moved to the next line. */ |
| 1320 | after_break = inptr->data + wrap_loc + 1; |
| 1321 | after_break_len = len - wrap_loc - 1; |
| 1322 | assert(after_break_len == strlen(after_break)); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1323 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1324 | /* new_line_len will later be increased by the lengths of indentation |
| 1325 | * and wrap_line. */ |
| 1326 | new_line_len = after_break_len; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1327 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1328 | /* We prepend the wrapped text to the next line, if the flag is set, |
| 1329 | * and there is a next line, and prepending would not make the line |
| 1330 | * too long. */ |
David Lawrence Ramsey | c97acfb | 2003-09-10 20:08:00 +0000 | [diff] [blame] | 1331 | if (same_line_wrap && inptr->next) { |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1332 | wrap_line = inptr->next->data; |
| 1333 | wrap_line_len = strlen(wrap_line); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1334 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1335 | /* +1 for the space between after_break and wrap_line. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1336 | if ((new_line_len + 1 + wrap_line_len) <= fill) { |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1337 | wrapping = TRUE; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1338 | new_line_len += (1 + wrap_line_len); |
| 1339 | } |
| 1340 | } |
Chris Allegretta | 56214c6 | 2001-09-27 02:46:53 +0000 | [diff] [blame] | 1341 | |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 1342 | #ifndef NANO_SMALL |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1343 | if (ISSET(AUTOINDENT)) { |
Chris Allegretta | 81dea02 | 2002-09-21 02:19:45 +0000 | [diff] [blame] | 1344 | /* Indentation comes from the next line if wrapping, else from |
| 1345 | * this line. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1346 | indentation = (wrapping ? wrap_line : inptr->data); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1347 | indent_len = indent_length(indentation); |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1348 | if (wrapping) |
Chris Allegretta | 81dea02 | 2002-09-21 02:19:45 +0000 | [diff] [blame] | 1349 | /* The wrap_line text should not duplicate indentation. |
| 1350 | * Note in this case we need not increase new_line_len. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1351 | wrap_line += indent_len; |
| 1352 | else |
| 1353 | new_line_len += indent_len; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1354 | } |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1355 | #endif |
Adam Rogoyski | 1e9183f | 2001-03-13 18:36:03 +0000 | [diff] [blame] | 1356 | |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1357 | /* Now we allocate the new line and copy into it. */ |
| 1358 | newline = charalloc(new_line_len + 1); /* +1 for \0 */ |
David Lawrence Ramsey | 85529b3 | 2004-06-22 15:38:47 +0000 | [diff] [blame] | 1359 | new_line_len = 0; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1360 | *newline = '\0'; |
| 1361 | |
| 1362 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1363 | if (ISSET(AUTOINDENT)) { |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1364 | strncpy(newline, indentation, indent_len); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1365 | newline[indent_len] = '\0'; |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1366 | new_line_len = indent_len; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1367 | } |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1368 | #endif |
| 1369 | strcat(newline, after_break); |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1370 | new_line_len += after_break_len; |
| 1371 | /* We end the old line after wrap_loc. Note that this does not eat |
| 1372 | * the space. */ |
Chris Allegretta | 67ca2aa | 2002-09-19 23:19:34 +0000 | [diff] [blame] | 1373 | null_at(&inptr->data, wrap_loc + 1); |
| 1374 | totsize++; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1375 | if (wrapping) { |
Chris Allegretta | 67ca2aa | 2002-09-19 23:19:34 +0000 | [diff] [blame] | 1376 | /* In this case, totsize increases by 1 since we add a space |
Chris Allegretta | 81dea02 | 2002-09-21 02:19:45 +0000 | [diff] [blame] | 1377 | * between after_break and wrap_line. If the line already ends |
Chris Allegretta | 4300092 | 2002-09-21 15:41:33 +0000 | [diff] [blame] | 1378 | * in a tab or a space, we don't add a space and decrement |
| 1379 | * totsize to account for that. */ |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1380 | if (!isblank(newline[new_line_len - 1])) |
Chris Allegretta | 81dea02 | 2002-09-21 02:19:45 +0000 | [diff] [blame] | 1381 | strcat(newline, " "); |
| 1382 | else |
| 1383 | totsize--; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1384 | strcat(newline, wrap_line); |
| 1385 | free(inptr->next->data); |
| 1386 | inptr->next->data = newline; |
| 1387 | } else { |
| 1388 | filestruct *temp = (filestruct *)nmalloc(sizeof(filestruct)); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1389 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1390 | /* In this case, the file size changes by +1 for the new line, |
| 1391 | * and +indent_len for the new indentation. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1392 | #ifndef NANO_SMALL |
| 1393 | totsize += indent_len; |
| 1394 | #endif |
| 1395 | totlines++; |
| 1396 | temp->data = newline; |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1397 | temp->prev = inptr; |
| 1398 | temp->next = inptr->next; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1399 | temp->prev->next = temp; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1400 | /* If temp->next is NULL, then temp is the last line of the |
| 1401 | * file, so we must set filebot. */ |
| 1402 | if (temp->next != NULL) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1403 | temp->next->prev = temp; |
| 1404 | else |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1405 | filebot = temp; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1406 | } |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1407 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1408 | /* Step 3, clean up. Here we reposition the cursor and mark, and do |
| 1409 | * some other sundry things. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1410 | |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1411 | /* Later wraps of this line will be prepended to the next line. */ |
David Lawrence Ramsey | 85529b3 | 2004-06-22 15:38:47 +0000 | [diff] [blame] | 1412 | same_line_wrap = TRUE; |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1413 | |
| 1414 | /* Each line knows its line number. We recalculate these if we |
| 1415 | * inserted a new line. */ |
| 1416 | if (!wrapping) |
| 1417 | renumber(inptr); |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1418 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1419 | /* If the cursor was after the break point, we must move it. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1420 | if (current_x > wrap_loc) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1421 | current = current->next; |
| 1422 | current_x -= |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 1423 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1424 | -indent_len + |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 1425 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1426 | wrap_loc + 1; |
| 1427 | wrap_reset(); |
| 1428 | placewewant = xplustabs(); |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1429 | } |
| 1430 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1431 | #ifndef NANO_SMALL |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1432 | /* If the mark was on this line after the wrap point, we move it |
| 1433 | * down. If it was on the next line and we wrapped, we move it |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1434 | * right. */ |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1435 | if (mark_beginbuf == inptr && mark_beginx > wrap_loc) { |
| 1436 | mark_beginbuf = inptr->next; |
Chris Allegretta | dffa207 | 2002-07-24 01:02:26 +0000 | [diff] [blame] | 1437 | mark_beginx -= wrap_loc - indent_len + 1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1438 | } else if (wrapping && mark_beginbuf == inptr->next) |
Chris Allegretta | 7162e3d | 2002-04-06 05:02:14 +0000 | [diff] [blame] | 1439 | mark_beginx += after_break_len; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1440 | #endif /* !NANO_SMALL */ |
Adam Rogoyski | 77f36de | 2000-06-07 03:56:54 +0000 | [diff] [blame] | 1441 | |
David Lawrence Ramsey | 9a527f5 | 2004-05-31 14:58:59 +0000 | [diff] [blame] | 1442 | return TRUE; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1443 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1444 | #endif /* !DISABLE_WRAPPING */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1445 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 1446 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | aaad3af | 2003-08-31 16:44:10 +0000 | [diff] [blame] | 1447 | /* A word is misspelled in the file. Let the user replace it. We |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1448 | * return FALSE if the user cancels. */ |
| 1449 | bool do_int_spell_fix(const char *word) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1450 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1451 | char *save_search; |
| 1452 | char *save_replace; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1453 | size_t current_x_save = current_x; |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 1454 | filestruct *current_save = current; |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1455 | filestruct *edittop_save = edittop; |
| 1456 | /* Save where we are. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1457 | bool accepted = TRUE; |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1458 | /* The return value. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1459 | bool reverse_search_set = ISSET(REVERSE_SEARCH); |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1460 | #ifndef NANO_SMALL |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1461 | bool case_sens_set = ISSET(CASE_SENSITIVE); |
| 1462 | bool mark_set = ISSET(MARK_ISSET); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1463 | |
Chris Allegretta | 1d8fa1f | 2002-12-10 00:53:21 +0000 | [diff] [blame] | 1464 | SET(CASE_SENSITIVE); |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1465 | /* Make sure the marking highlight is off during spell-check. */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1466 | UNSET(MARK_ISSET); |
| 1467 | #endif |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1468 | /* Make sure spell-check goes forward only. */ |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1469 | UNSET(REVERSE_SEARCH); |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1470 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1471 | /* Save the current search/replace strings. */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1472 | search_init_globals(); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1473 | save_search = last_search; |
| 1474 | save_replace = last_replace; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1475 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1476 | /* Set search/replace strings to misspelled word. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1477 | last_search = mallocstrcpy(NULL, word); |
| 1478 | last_replace = mallocstrcpy(NULL, word); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1479 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1480 | /* Start from the top of the file. */ |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1481 | current = fileage; |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1482 | current_x = -1; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1483 | |
| 1484 | search_last_line = FALSE; |
| 1485 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1486 | /* Find the first whole-word occurrence of word. */ |
| 1487 | while (findnextstr(TRUE, TRUE, fileage, 0, word, FALSE) != 0) |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1488 | if (is_whole_word(current_x, current->data, word)) { |
| 1489 | edit_refresh(); |
Chris Allegretta | d00e6df | 2000-11-29 04:33:26 +0000 | [diff] [blame] | 1490 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1491 | do_replace_highlight(TRUE, word); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1492 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1493 | /* Allow the replace word to be corrected. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1494 | accepted = -1 != statusq(FALSE, spell_list, word, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1495 | #ifndef NANO_SMALL |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1496 | NULL, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 1497 | #endif |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1498 | _("Edit a replacement")); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1499 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1500 | do_replace_highlight(FALSE, word); |
Chris Allegretta | 1bc0c7e | 2002-01-08 15:00:24 +0000 | [diff] [blame] | 1501 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1502 | if (accepted && strcmp(word, answer) != 0) { |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1503 | search_last_line = FALSE; |
| 1504 | current_x--; |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1505 | do_replace_loop(word, current_save, ¤t_x_save, TRUE); |
Chris Allegretta | 1bc0c7e | 2002-01-08 15:00:24 +0000 | [diff] [blame] | 1506 | } |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1507 | |
| 1508 | break; |
Chris Allegretta | 8083827 | 2001-12-02 06:03:22 +0000 | [diff] [blame] | 1509 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1510 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1511 | /* Restore the search/replace strings. */ |
| 1512 | free(last_search); |
| 1513 | last_search = save_search; |
| 1514 | free(last_replace); |
| 1515 | last_replace = save_replace; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1516 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1517 | /* Restore where we were. */ |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1518 | current = current_save; |
| 1519 | current_x = current_x_save; |
| 1520 | edittop = edittop_save; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1521 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1522 | /* Restore search/replace direction. */ |
Chris Allegretta | 23b74b2 | 2002-01-21 20:32:22 +0000 | [diff] [blame] | 1523 | if (reverse_search_set) |
| 1524 | SET(REVERSE_SEARCH); |
| 1525 | |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1526 | #ifndef NANO_SMALL |
Chris Allegretta | 1d8fa1f | 2002-12-10 00:53:21 +0000 | [diff] [blame] | 1527 | if (!case_sens_set) |
| 1528 | UNSET(CASE_SENSITIVE); |
| 1529 | |
David Lawrence Ramsey | 1044d74 | 2004-02-24 20:41:39 +0000 | [diff] [blame] | 1530 | /* Restore marking highlight. */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1531 | if (mark_set) |
| 1532 | SET(MARK_ISSET); |
| 1533 | #endif |
| 1534 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1535 | return accepted; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1536 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1537 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1538 | /* Integrated spell checking using 'spell' program. Return value: NULL |
| 1539 | * for normal termination, otherwise the error string. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1540 | const char *do_int_speller(const char *tempfile_name) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1541 | { |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1542 | char *read_buff, *read_buff_ptr, *read_buff_word; |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1543 | size_t pipe_buff_size, read_buff_size, read_buff_read, bytesread; |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1544 | int spell_fd[2], sort_fd[2], uniq_fd[2], tempfile_fd = -1; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1545 | pid_t pid_spell, pid_sort, pid_uniq; |
| 1546 | int spell_status, sort_status, uniq_status; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1547 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1548 | /* Create all three pipes up front. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1549 | if (pipe(spell_fd) == -1 || pipe(sort_fd) == -1 || pipe(uniq_fd) == -1) |
| 1550 | return _("Could not create pipe"); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1551 | |
Chris Allegretta | 2a7a9a2 | 2002-12-10 00:16:27 +0000 | [diff] [blame] | 1552 | statusbar(_("Creating misspelled word list, please wait...")); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1553 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1554 | /* A new process to run spell in. */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1555 | if ((pid_spell = fork()) == 0) { |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1556 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1557 | /* Child continues (i.e, future spell process). */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1558 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1559 | close(spell_fd[0]); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1560 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1561 | /* Replace the standard input with the temp file. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1562 | if ((tempfile_fd = open(tempfile_name, O_RDONLY)) == -1) |
| 1563 | goto close_pipes_and_exit; |
| 1564 | |
| 1565 | if (dup2(tempfile_fd, STDIN_FILENO) != STDIN_FILENO) |
| 1566 | goto close_pipes_and_exit; |
| 1567 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1568 | close(tempfile_fd); |
| 1569 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1570 | /* Send spell's standard output to the pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1571 | if (dup2(spell_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 1572 | goto close_pipes_and_exit; |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1573 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1574 | close(spell_fd[1]); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1575 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1576 | /* Start spell program; we are using PATH. */ |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1577 | execlp("spell", "spell", NULL); |
| 1578 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1579 | /* Should not be reached, if spell is found. */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1580 | exit(1); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1581 | } |
| 1582 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1583 | /* Parent continues here. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1584 | close(spell_fd[1]); |
| 1585 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1586 | /* A new process to run sort in. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1587 | if ((pid_sort = fork()) == 0) { |
| 1588 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1589 | /* Child continues (i.e, future spell process). Replace the |
| 1590 | * standard input with the standard output of the old pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1591 | if (dup2(spell_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 1592 | goto close_pipes_and_exit; |
| 1593 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1594 | close(spell_fd[0]); |
| 1595 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1596 | /* Send sort's standard output to the new pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1597 | if (dup2(sort_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 1598 | goto close_pipes_and_exit; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1599 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1600 | close(sort_fd[1]); |
| 1601 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1602 | /* Start sort program. Use -f to remove mixed case without |
| 1603 | * having to have ANOTHER pipe for tr. If this isn't portable, |
| 1604 | * let me know. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1605 | execlp("sort", "sort", "-f", NULL); |
| 1606 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1607 | /* Should not be reached, if sort is found. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1608 | exit(1); |
| 1609 | } |
| 1610 | |
Chris Allegretta | 2d5fc3a | 2003-01-16 03:11:23 +0000 | [diff] [blame] | 1611 | close(spell_fd[0]); |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1612 | close(sort_fd[1]); |
| 1613 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1614 | /* A new process to run uniq in. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1615 | if ((pid_uniq = fork()) == 0) { |
| 1616 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1617 | /* Child continues (i.e, future uniq process). Replace the |
| 1618 | * standard input with the standard output of the old pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1619 | if (dup2(sort_fd[0], STDIN_FILENO) != STDIN_FILENO) |
| 1620 | goto close_pipes_and_exit; |
| 1621 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1622 | close(sort_fd[0]); |
| 1623 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1624 | /* Send uniq's standard output to the new pipe. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1625 | if (dup2(uniq_fd[1], STDOUT_FILENO) != STDOUT_FILENO) |
| 1626 | goto close_pipes_and_exit; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1627 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1628 | close(uniq_fd[1]); |
| 1629 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1630 | /* Start uniq program; we are using PATH. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1631 | execlp("uniq", "uniq", NULL); |
| 1632 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1633 | /* Should not be reached, if uniq is found. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1634 | exit(1); |
| 1635 | } |
| 1636 | |
Chris Allegretta | 2d5fc3a | 2003-01-16 03:11:23 +0000 | [diff] [blame] | 1637 | close(sort_fd[0]); |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1638 | close(uniq_fd[1]); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1639 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1640 | /* Child process was not forked successfully. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1641 | if (pid_spell < 0 || pid_sort < 0 || pid_uniq < 0) { |
| 1642 | close(uniq_fd[0]); |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1643 | return _("Could not fork"); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1644 | } |
| 1645 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1646 | /* Get system pipe buffer size. */ |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1647 | if ((pipe_buff_size = fpathconf(uniq_fd[0], _PC_PIPE_BUF)) < 1) { |
| 1648 | close(uniq_fd[0]); |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1649 | return _("Could not get size of pipe buffer"); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1650 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1651 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1652 | /* Read in the returned spelling errors. */ |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1653 | read_buff_read = 0; |
| 1654 | read_buff_size = pipe_buff_size + 1; |
| 1655 | read_buff = read_buff_ptr = charalloc(read_buff_size); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1656 | |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1657 | while ((bytesread = read(uniq_fd[0], read_buff_ptr, pipe_buff_size)) > 0) { |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1658 | read_buff_read += bytesread; |
| 1659 | read_buff_size += pipe_buff_size; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1660 | read_buff = read_buff_ptr = charealloc(read_buff, read_buff_size); |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1661 | read_buff_ptr += read_buff_read; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1662 | |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1663 | } |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1664 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1665 | *read_buff_ptr = (char)NULL; |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1666 | close(uniq_fd[0]); |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1667 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1668 | /* Process the spelling errors. */ |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1669 | read_buff_word = read_buff_ptr = read_buff; |
| 1670 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1671 | while (*read_buff_ptr != '\0') { |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1672 | |
| 1673 | if ((*read_buff_ptr == '\n') || (*read_buff_ptr == '\r')) { |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1674 | *read_buff_ptr = (char)NULL; |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1675 | if (read_buff_word != read_buff_ptr) { |
| 1676 | if (!do_int_spell_fix(read_buff_word)) { |
| 1677 | read_buff_word = read_buff_ptr; |
| 1678 | break; |
| 1679 | } |
| 1680 | } |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1681 | read_buff_word = read_buff_ptr + 1; |
| 1682 | } |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1683 | read_buff_ptr++; |
| 1684 | } |
| 1685 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1686 | /* Special case where last word doesn't end with \n or \r. */ |
Chris Allegretta | f21f3fc | 2002-03-25 03:26:27 +0000 | [diff] [blame] | 1687 | if (read_buff_word != read_buff_ptr) |
| 1688 | do_int_spell_fix(read_buff_word); |
| 1689 | |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1690 | free(read_buff); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1691 | replace_abort(); |
Chris Allegretta | 64fc78c | 2003-01-26 19:57:44 +0000 | [diff] [blame] | 1692 | edit_refresh(); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1693 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1694 | /* Process end of spell process. */ |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1695 | waitpid(pid_spell, &spell_status, 0); |
| 1696 | waitpid(pid_sort, &sort_status, 0); |
| 1697 | waitpid(pid_uniq, &uniq_status, 0); |
Chris Allegretta | 5ad92ac | 2002-12-09 00:58:51 +0000 | [diff] [blame] | 1698 | |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1699 | if (WIFEXITED(spell_status) == 0 || WEXITSTATUS(spell_status)) |
| 1700 | return _("Error invoking \"spell\""); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1701 | |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1702 | if (WIFEXITED(sort_status) == 0 || WEXITSTATUS(sort_status)) |
| 1703 | return _("Error invoking \"sort -f\""); |
| 1704 | |
| 1705 | if (WIFEXITED(uniq_status) == 0 || WEXITSTATUS(uniq_status)) |
| 1706 | return _("Error invoking \"uniq\""); |
| 1707 | |
| 1708 | /* Otherwise... */ |
| 1709 | return NULL; |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1710 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1711 | close_pipes_and_exit: |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1712 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1713 | /* Don't leak any handles. */ |
Chris Allegretta | 3f1b685 | 2003-01-12 23:54:05 +0000 | [diff] [blame] | 1714 | close(tempfile_fd); |
| 1715 | close(spell_fd[0]); |
| 1716 | close(spell_fd[1]); |
| 1717 | close(sort_fd[0]); |
| 1718 | close(sort_fd[1]); |
| 1719 | close(uniq_fd[0]); |
| 1720 | close(uniq_fd[1]); |
| 1721 | exit(1); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1722 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1723 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 1724 | /* External spell checking. Return value: NULL for normal termination, |
| 1725 | * otherwise the error string. */ |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1726 | const char *do_alt_speller(char *tempfile_name) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1727 | { |
Chris Allegretta | b3655b4 | 2001-10-22 03:15:31 +0000 | [diff] [blame] | 1728 | int alt_spell_status, lineno_cur = current->lineno; |
David Lawrence Ramsey | 360093a | 2004-07-28 20:53:55 +0000 | [diff] [blame] | 1729 | int x_cur = current_x, y_cur = current_y; |
| 1730 | size_t pww_cur = placewewant; |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1731 | pid_t pid_spell; |
Chris Allegretta | 169ee84 | 2001-01-26 01:57:32 +0000 | [diff] [blame] | 1732 | char *ptr; |
| 1733 | static int arglen = 3; |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1734 | static char **spellargs = (char **)NULL; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1735 | #ifndef NANO_SMALL |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1736 | bool mark_set = ISSET(MARK_ISSET); |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1737 | int mbb_lineno_cur = 0; |
| 1738 | /* We're going to close the current file, and open the output of |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1739 | * the alternate spell command. The line that mark_beginbuf |
| 1740 | * points to will be freed, so we save the line number and |
| 1741 | * restore afterwards. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1742 | |
| 1743 | if (mark_set) { |
| 1744 | mbb_lineno_cur = mark_beginbuf->lineno; |
| 1745 | UNSET(MARK_ISSET); |
| 1746 | } |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1747 | #endif |
| 1748 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1749 | endwin(); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1750 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1751 | /* Set up an argument list to pass execvp(). */ |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 1752 | if (spellargs == NULL) { |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1753 | spellargs = (char **)nmalloc(arglen * sizeof(char *)); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1754 | |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 1755 | spellargs[0] = strtok(alt_speller, " "); |
| 1756 | while ((ptr = strtok(NULL, " ")) != NULL) { |
| 1757 | arglen++; |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 1758 | spellargs = (char **)nrealloc(spellargs, arglen * sizeof(char *)); |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 1759 | spellargs[arglen - 3] = ptr; |
Chris Allegretta | 169ee84 | 2001-01-26 01:57:32 +0000 | [diff] [blame] | 1760 | } |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 1761 | spellargs[arglen - 1] = NULL; |
| 1762 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1763 | spellargs[arglen - 2] = tempfile_name; |
Chris Allegretta | e434b45 | 2001-01-27 19:25:00 +0000 | [diff] [blame] | 1764 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1765 | /* Start a new process for the alternate speller. */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 1766 | if ((pid_spell = fork()) == 0) { |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1767 | /* Start alternate spell program; we are using PATH. */ |
Chris Allegretta | 169ee84 | 2001-01-26 01:57:32 +0000 | [diff] [blame] | 1768 | execvp(spellargs[0], spellargs); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1769 | |
| 1770 | /* Should not be reached, if alternate speller is found!!! */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1771 | exit(1); |
| 1772 | } |
| 1773 | |
| 1774 | /* Could not fork?? */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1775 | if (pid_spell < 0) |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1776 | return _("Could not fork"); |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1777 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1778 | /* Wait for alternate speller to complete. */ |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1779 | wait(&alt_spell_status); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1780 | |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1781 | if (!WIFEXITED(alt_spell_status) || WEXITSTATUS(alt_spell_status) != 0) { |
| 1782 | char *altspell_error = NULL; |
| 1783 | char *invoke_error = _("Could not invoke \"%s\""); |
| 1784 | int msglen = strlen(invoke_error) + strlen(alt_speller) + 2; |
| 1785 | |
| 1786 | altspell_error = charalloc(msglen); |
| 1787 | snprintf(altspell_error, msglen, invoke_error, alt_speller); |
| 1788 | return altspell_error; |
| 1789 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1790 | |
Chris Allegretta | 8f6c069 | 2000-07-19 01:16:18 +0000 | [diff] [blame] | 1791 | refresh(); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1792 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1793 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1794 | if (mark_set) { |
| 1795 | do_gotopos(mbb_lineno_cur, mark_beginx, y_cur, 0); |
| 1796 | mark_beginbuf = current; |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1797 | /* In case the line got shorter, assign mark_beginx. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1798 | mark_beginx = current_x; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1799 | SET(MARK_ISSET); |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1800 | } else { |
| 1801 | #endif |
| 1802 | /* Only reload the temp file if it isn't a marked selection. */ |
| 1803 | free_filestruct(fileage); |
| 1804 | terminal_init(); |
| 1805 | global_init(TRUE); |
| 1806 | open_file(tempfile_name, FALSE, TRUE); |
| 1807 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1808 | } |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 1809 | #endif |
| 1810 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1811 | /* Go back to the old position, mark the file as modified, and make |
| 1812 | * sure that the titlebar is refreshed. */ |
Chris Allegretta | 1b3381b | 2001-09-28 21:59:01 +0000 | [diff] [blame] | 1813 | do_gotopos(lineno_cur, x_cur, y_cur, pww_cur); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1814 | set_modified(); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1815 | clearok(topwin, FALSE); |
| 1816 | titlebar(NULL); |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 1817 | |
Chris Allegretta | 334a940 | 2002-12-16 04:25:53 +0000 | [diff] [blame] | 1818 | return NULL; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1819 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1820 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1821 | void do_spell(void) |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1822 | { |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1823 | int i; |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1824 | char *temp = safe_tempnam(0, "nano."); |
| 1825 | const char *spell_msg; |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1826 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1827 | if (temp == NULL) { |
| 1828 | statusbar(_("Could not create temp file: %s"), strerror(errno)); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1829 | return; |
Chris Allegretta | 271e972 | 2000-11-10 18:15:43 +0000 | [diff] [blame] | 1830 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1831 | |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1832 | #ifndef NANO_SMALL |
| 1833 | if (ISSET(MARK_ISSET)) |
David Lawrence Ramsey | 2f0d03b | 2004-05-28 00:15:28 +0000 | [diff] [blame] | 1834 | i = write_marked(temp, TRUE, FALSE); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1835 | else |
| 1836 | #endif |
David Lawrence Ramsey | 2f0d03b | 2004-05-28 00:15:28 +0000 | [diff] [blame] | 1837 | i = write_file(temp, TRUE, FALSE, FALSE); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1838 | |
| 1839 | if (i == -1) { |
David Lawrence Ramsey | 95e39e5 | 2004-08-12 02:52:14 +0000 | [diff] [blame] | 1840 | statusbar(_("Error writing temp file: %s"), strerror(errno)); |
Chris Allegretta | bef1297 | 2002-03-06 03:30:40 +0000 | [diff] [blame] | 1841 | free(temp); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1842 | return; |
Chris Allegretta | 3dbb278 | 2000-12-02 04:36:50 +0000 | [diff] [blame] | 1843 | } |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1844 | |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1845 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1846 | /* Update the current open_files entry before spell-checking, in |
| 1847 | * case any problems occur. */ |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 1848 | add_open_file(TRUE); |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 1849 | #endif |
| 1850 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1851 | spell_msg = alt_speller != NULL ? do_alt_speller(temp) : |
| 1852 | do_int_speller(temp); |
David Lawrence Ramsey | 35961c4 | 2004-01-23 19:34:03 +0000 | [diff] [blame] | 1853 | unlink(temp); |
Chris Allegretta | fdcb9e9 | 2003-02-12 02:52:04 +0000 | [diff] [blame] | 1854 | free(temp); |
Chris Allegretta | 27eb13f | 2000-11-05 16:52:21 +0000 | [diff] [blame] | 1855 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1856 | if (spell_msg != NULL) |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1857 | statusbar(_("Spell checking failed: %s: %s"), spell_msg, |
| 1858 | strerror(errno)); |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1859 | else |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 1860 | statusbar(_("Finished checking spelling")); |
Chris Allegretta | 67105eb | 2000-07-03 03:18:32 +0000 | [diff] [blame] | 1861 | } |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 1862 | #endif /* !DISABLE_SPELLER */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1863 | |
David Lawrence Ramsey | 44e7f82 | 2004-03-30 04:17:10 +0000 | [diff] [blame] | 1864 | #if !defined(NANO_SMALL) || !defined(DISABLE_JUSTIFY) |
David Lawrence Ramsey | 8d91199 | 2004-05-29 17:05:52 +0000 | [diff] [blame] | 1865 | /* The "indentation" of a line is the whitespace between the quote part |
| 1866 | * and the non-whitespace of the line. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 1867 | size_t indent_length(const char *line) |
| 1868 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1869 | size_t len = 0; |
| 1870 | |
| 1871 | assert(line != NULL); |
David Lawrence Ramsey | 9830d75 | 2004-05-13 17:19:54 +0000 | [diff] [blame] | 1872 | while (isblank(*line)) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1873 | line++; |
| 1874 | len++; |
| 1875 | } |
| 1876 | return len; |
| 1877 | } |
David Lawrence Ramsey | 44e7f82 | 2004-03-30 04:17:10 +0000 | [diff] [blame] | 1878 | #endif /* !NANO_SMALL || !DISABLE_JUSTIFY */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1879 | |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 1880 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | f613ca7 | 2004-05-13 22:23:58 +0000 | [diff] [blame] | 1881 | /* justify_format() replaces Tab by Space and multiple spaces by 1 |
| 1882 | * (except it maintains 2 after a . ! or ?). Note the terminating \0 |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1883 | * counts as a space. |
| 1884 | * |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 1885 | * justify_format() might make line->data shorter, and change the actual |
| 1886 | * pointer with null_at(). |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1887 | * |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 1888 | * justify_format() will not look at the first skip characters of line. |
David Lawrence Ramsey | f613ca7 | 2004-05-13 22:23:58 +0000 | [diff] [blame] | 1889 | * skip should be at most strlen(line->data). The character at |
| 1890 | * line[skip + 1] must not be whitespace. */ |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 1891 | void justify_format(filestruct *line, size_t skip) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1892 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1893 | char *back, *front; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1894 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1895 | /* These four asserts are assumptions about the input data. */ |
| 1896 | assert(line != NULL); |
| 1897 | assert(line->data != NULL); |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 1898 | assert(skip < strlen(line->data)); |
David Lawrence Ramsey | 9830d75 | 2004-05-13 17:19:54 +0000 | [diff] [blame] | 1899 | assert(!isblank(line->data[skip])); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1900 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1901 | back = line->data + skip; |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 1902 | for (front = back; ; front++) { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1903 | bool remove_space = FALSE; |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1904 | /* Do we want to remove this space? */ |
| 1905 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 1906 | if (*front == '\t') |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1907 | *front = ' '; |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 1908 | |
David Lawrence Ramsey | 021960d | 2004-05-13 23:19:01 +0000 | [diff] [blame] | 1909 | /* These tests are safe since line->data + skip is not a |
| 1910 | * space. */ |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 1911 | if ((*front == '\0' || *front == ' ') && *(front - 1) == ' ') { |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1912 | const char *bob = front - 2; |
| 1913 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 1914 | remove_space = TRUE; |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 1915 | for (bob = back - 2; bob >= line->data + skip; bob--) { |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1916 | if (strchr(punct, *bob) != NULL) { |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 1917 | remove_space = FALSE; |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1918 | break; |
| 1919 | } |
| 1920 | if (strchr(brackets, *bob) == NULL) |
| 1921 | break; |
| 1922 | } |
| 1923 | } |
| 1924 | |
| 1925 | if (remove_space) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1926 | /* Now *front is a space we want to remove. We do that by |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 1927 | * simply failing to assign it to *back. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1928 | #ifndef NANO_SMALL |
| 1929 | if (mark_beginbuf == line && back - line->data < mark_beginx) |
| 1930 | mark_beginx--; |
| 1931 | #endif |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 1932 | if (*front == '\0') |
| 1933 | *(back - 1) = '\0'; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1934 | } else { |
| 1935 | *back = *front; |
| 1936 | back++; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1937 | } |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 1938 | if (*front == '\0') |
| 1939 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1940 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1941 | |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 1942 | back--; |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 1943 | assert(*back == '\0' && *front == '\0'); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1944 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1945 | /* Now back is the new end of line->data. */ |
| 1946 | if (back != front) { |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 1947 | totsize -= front - back; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1948 | null_at(&line->data, back - line->data); |
| 1949 | #ifndef NANO_SMALL |
| 1950 | if (mark_beginbuf == line && back - line->data < mark_beginx) |
| 1951 | mark_beginx = back - line->data; |
| 1952 | #endif |
| 1953 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 1954 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1955 | |
| 1956 | /* The "quote part" of a line is the largest initial substring matching |
| 1957 | * the quote string. This function returns the length of the quote part |
| 1958 | * of the given line. |
| 1959 | * |
| 1960 | * Note that if !HAVE_REGEX_H then we match concatenated copies of |
| 1961 | * quotestr. */ |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1962 | size_t quote_length(const char *line) |
| 1963 | { |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 1964 | #ifdef HAVE_REGEX_H |
| 1965 | regmatch_t matches; |
| 1966 | int rc = regexec("ereg, line, 1, &matches, 0); |
| 1967 | |
| 1968 | if (rc == REG_NOMATCH || matches.rm_so == (regoff_t)-1) |
| 1969 | return 0; |
| 1970 | /* matches.rm_so should be 0, since the quote string should start |
| 1971 | * with the caret ^. */ |
| 1972 | return matches.rm_eo; |
| 1973 | #else /* !HAVE_REGEX_H */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1974 | size_t qdepth = 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1975 | |
David Lawrence Ramsey | fd3039a | 2004-07-17 19:49:12 +0000 | [diff] [blame] | 1976 | /* Compute quote depth level. */ |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 1977 | while (strncmp(line + qdepth, quotestr, quotelen) == 0) |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 1978 | qdepth += quotelen; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1979 | return qdepth; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1980 | #endif /* !HAVE_REGEX_H */ |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 1981 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1982 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1983 | /* a_line and b_line are lines of text. The quotation part of a_line is |
| 1984 | * the first a_quote characters. Check that the quotation part of |
| 1985 | * b_line is the same. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1986 | bool quotes_match(const char *a_line, size_t a_quote, const char |
| 1987 | *b_line) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1988 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1989 | /* Here is the assumption about a_quote: */ |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 1990 | assert(a_quote == quote_length(a_line)); |
| 1991 | return a_quote == quote_length(b_line) && |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 1992 | strncmp(a_line, b_line, a_quote) == 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 1993 | } |
| 1994 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 1995 | /* We assume a_line and b_line have no quote part. Then, we return |
| 1996 | * whether b_line could follow a_line in a paragraph. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 1997 | bool indents_match(const char *a_line, size_t a_indent, const char |
David Lawrence Ramsey | d4693cb | 2004-05-14 01:17:25 +0000 | [diff] [blame] | 1998 | *b_line, size_t b_indent) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 1999 | { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2000 | assert(a_indent == indent_length(a_line)); |
| 2001 | assert(b_indent == indent_length(b_line)); |
| 2002 | |
David Lawrence Ramsey | b8c479a | 2004-07-31 14:10:23 +0000 | [diff] [blame] | 2003 | return b_indent <= a_indent && |
| 2004 | strncmp(a_line, b_line, b_indent) == 0; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2005 | } |
| 2006 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2007 | /* Is foo the beginning of a paragraph? |
| 2008 | * |
| 2009 | * A line of text consists of a "quote part", followed by an |
| 2010 | * "indentation part", followed by text. The functions quote_length() |
| 2011 | * and indent_length() calculate these parts. |
| 2012 | * |
| 2013 | * A line is "part of a paragraph" if it has a part not in the quote |
| 2014 | * part or the indentation. |
| 2015 | * |
| 2016 | * A line is "the beginning of a paragraph" if it is part of a |
| 2017 | * paragraph and |
| 2018 | * 1) it is the top line of the file, or |
| 2019 | * 2) the line above it is not part of a paragraph, or |
| 2020 | * 3) the line above it does not have precisely the same quote |
| 2021 | * part, or |
| 2022 | * 4) the indentation of this line is not an initial substring of |
| 2023 | * the indentation of the previous line, or |
| 2024 | * 5) this line has no quote part and some indentation, and |
| 2025 | * AUTOINDENT is not set. |
| 2026 | * The reason for number 5) is that if AUTOINDENT is not set, then an |
| 2027 | * indented line is expected to start a paragraph, like in books. |
| 2028 | * Thus, nano can justify an indented paragraph only if AUTOINDENT is |
| 2029 | * turned on. */ |
| 2030 | bool begpar(const filestruct *const foo) |
| 2031 | { |
| 2032 | size_t quote_len; |
| 2033 | size_t indent_len; |
| 2034 | size_t temp_id_len; |
| 2035 | |
| 2036 | /* Case 1). */ |
| 2037 | if (foo->prev == NULL) |
| 2038 | return TRUE; |
| 2039 | |
| 2040 | quote_len = quote_length(foo->data); |
| 2041 | indent_len = indent_length(foo->data + quote_len); |
| 2042 | |
| 2043 | /* Not part of a paragraph. */ |
| 2044 | if (foo->data[quote_len + indent_len] == '\0') |
| 2045 | return FALSE; |
| 2046 | |
| 2047 | /* Case 3). */ |
| 2048 | if (!quotes_match(foo->data, quote_len, foo->prev->data)) |
| 2049 | return TRUE; |
| 2050 | |
| 2051 | temp_id_len = indent_length(foo->prev->data + quote_len); |
| 2052 | |
| 2053 | /* Case 2) or 5) or 4). */ |
| 2054 | if (foo->prev->data[quote_len + temp_id_len] == '\0' || |
| 2055 | (quote_len == 0 && indent_len > 0 |
| 2056 | #ifndef NANO_SMALL |
| 2057 | && !ISSET(AUTOINDENT) |
| 2058 | #endif |
| 2059 | ) || !indents_match(foo->prev->data + quote_len, temp_id_len, |
| 2060 | foo->data + quote_len, indent_len)) |
| 2061 | return TRUE; |
| 2062 | |
| 2063 | return FALSE; |
| 2064 | } |
| 2065 | |
| 2066 | /* We find the last beginning-of-paragraph line before the current |
| 2067 | * line. */ |
| 2068 | void do_para_begin(void) |
| 2069 | { |
| 2070 | const filestruct *old_current = current; |
| 2071 | const size_t old_pww = placewewant; |
| 2072 | |
| 2073 | current_x = 0; |
| 2074 | placewewant = 0; |
| 2075 | |
| 2076 | if (current->prev != NULL) { |
| 2077 | do { |
| 2078 | current = current->prev; |
| 2079 | } while (!begpar(current)); |
| 2080 | } |
| 2081 | |
| 2082 | edit_redraw(old_current, old_pww); |
| 2083 | } |
| 2084 | |
| 2085 | bool inpar(const char *str) |
| 2086 | { |
| 2087 | size_t quote_len = quote_length(str); |
| 2088 | |
| 2089 | return str[quote_len + indent_length(str + quote_len)] != '\0'; |
| 2090 | } |
| 2091 | |
| 2092 | /* A line is the last line of a paragraph if it is in a paragraph, and |
| 2093 | * the next line isn't, or is the beginning of a paragraph. We move |
| 2094 | * down to the end of a paragraph, then one line farther. */ |
| 2095 | void do_para_end(void) |
| 2096 | { |
| 2097 | const filestruct *const old_current = current; |
| 2098 | const size_t old_pww = placewewant; |
| 2099 | |
| 2100 | current_x = 0; |
| 2101 | placewewant = 0; |
| 2102 | |
| 2103 | while (current->next != NULL && !inpar(current->data)) |
| 2104 | current = current->next; |
| 2105 | |
| 2106 | while (current->next != NULL && inpar(current->next->data) && |
| 2107 | !begpar(current->next)) |
| 2108 | current = current->next; |
| 2109 | |
| 2110 | if (current->next != NULL) |
| 2111 | current = current->next; |
| 2112 | |
| 2113 | edit_redraw(old_current, old_pww); |
| 2114 | } |
| 2115 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2116 | /* Put the next par_len lines, starting with first_line, in the cut |
David Lawrence Ramsey | 44e7f82 | 2004-03-30 04:17:10 +0000 | [diff] [blame] | 2117 | * buffer, not allowing them to be concatenated. We assume there are |
| 2118 | * enough lines after first_line. We leave copies of the lines in |
| 2119 | * place, too. We return the new copy of first_line. */ |
David Lawrence Ramsey | d4693cb | 2004-05-14 01:17:25 +0000 | [diff] [blame] | 2120 | filestruct *backup_lines(filestruct *first_line, size_t par_len, size_t |
| 2121 | quote_len) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2122 | { |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2123 | /* We put the original lines, not copies, into the cutbuffer, just |
| 2124 | * out of a misguided sense of consistency, so if you uncut, you get |
| 2125 | * the actual same paragraph back, not a copy. */ |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 2126 | filestruct *alice = first_line; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2127 | |
| 2128 | set_modified(); |
| 2129 | cutbuffer = NULL; |
David Lawrence Ramsey | 4e25410 | 2003-11-05 22:04:08 +0000 | [diff] [blame] | 2130 | for (; par_len > 0; par_len--) { |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 2131 | filestruct *bob = copy_node(alice); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2132 | |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 2133 | if (alice == first_line) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2134 | first_line = bob; |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 2135 | if (alice == current) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2136 | current = bob; |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 2137 | if (alice == edittop) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2138 | edittop = bob; |
| 2139 | #ifndef NANO_SMALL |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 2140 | if (alice == mark_beginbuf) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2141 | mark_beginbuf = bob; |
| 2142 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2143 | |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 2144 | assert(alice != NULL && bob != NULL); |
David Lawrence Ramsey | 44e7f82 | 2004-03-30 04:17:10 +0000 | [diff] [blame] | 2145 | add_to_cutbuffer(alice, FALSE); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2146 | splice_node(bob->prev, bob, bob->next); |
Chris Allegretta | 908f770 | 2003-01-15 11:18:58 +0000 | [diff] [blame] | 2147 | alice = bob->next; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2148 | } |
| 2149 | return first_line; |
| 2150 | } |
| 2151 | |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2152 | /* Is it possible to break line at or before goal? */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2153 | bool breakable(const char *line, int goal) |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2154 | { |
David Lawrence Ramsey | 4e25410 | 2003-11-05 22:04:08 +0000 | [diff] [blame] | 2155 | for (; *line != '\0' && goal >= 0; line++) { |
David Lawrence Ramsey | 9830d75 | 2004-05-13 17:19:54 +0000 | [diff] [blame] | 2156 | if (isblank(*line)) |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2157 | return TRUE; |
| 2158 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2159 | if (is_cntrl_char(*line)) |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2160 | goal -= 2; |
| 2161 | else |
| 2162 | goal -= 1; |
| 2163 | } |
Chris Allegretta | 428f620 | 2003-02-12 03:21:45 +0000 | [diff] [blame] | 2164 | /* If goal is not negative, the whole line (one word) was short |
| 2165 | * enough. */ |
| 2166 | return goal >= 0; |
Chris Allegretta | cff6e6f | 2003-02-03 03:22:02 +0000 | [diff] [blame] | 2167 | } |
| 2168 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2169 | /* We are trying to break a chunk off line. We find the last space such |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2170 | * that the display length to there is at most goal + 1. If there is no |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2171 | * such space, and force is TRUE, then we find the first space. Anyway, |
| 2172 | * we then take the last space in that group of spaces. The terminating |
| 2173 | * '\0' counts as a space. */ |
| 2174 | int break_line(const char *line, int goal, bool force) |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2175 | { |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2176 | /* Note that we use int instead of size_t, since goal is at most |
| 2177 | * COLS, the screen width, which will always be reasonably small. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2178 | int space_loc = -1; |
| 2179 | /* Current tentative return value. Index of the last space we |
| 2180 | * found with short enough display width. */ |
| 2181 | int cur_loc = 0; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2182 | /* Current index in line. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2183 | |
| 2184 | assert(line != NULL); |
David Lawrence Ramsey | 4e25410 | 2003-11-05 22:04:08 +0000 | [diff] [blame] | 2185 | for (; *line != '\0' && goal >= 0; line++, cur_loc++) { |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2186 | if (*line == ' ') |
| 2187 | space_loc = cur_loc; |
| 2188 | assert(*line != '\t'); |
| 2189 | |
Chris Allegretta | cf287c8 | 2002-07-20 13:57:41 +0000 | [diff] [blame] | 2190 | if (is_cntrl_char(*line)) |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2191 | goal -= 2; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2192 | else |
| 2193 | goal--; |
| 2194 | } |
| 2195 | if (goal >= 0) |
| 2196 | /* In fact, the whole line displays shorter than goal. */ |
| 2197 | return cur_loc; |
| 2198 | if (space_loc == -1) { |
| 2199 | /* No space found short enough. */ |
| 2200 | if (force) |
David Lawrence Ramsey | 4e25410 | 2003-11-05 22:04:08 +0000 | [diff] [blame] | 2201 | for (; *line != '\0'; line++, cur_loc++) |
Chris Allegretta | a7a78de | 2003-02-19 22:27:53 +0000 | [diff] [blame] | 2202 | if (*line == ' ' && *(line + 1) != ' ' && *(line + 1) != '\0') |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2203 | return cur_loc; |
| 2204 | return -1; |
| 2205 | } |
| 2206 | /* Perhaps the character after space_loc is a space. But because |
David Lawrence Ramsey | 0084eaa | 2002-11-04 16:05:42 +0000 | [diff] [blame] | 2207 | * of justify_format(), there can be only two adjacent. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2208 | if (*(line - cur_loc + space_loc + 1) == ' ' || |
| 2209 | *(line - cur_loc + space_loc + 1) == '\0') |
| 2210 | space_loc++; |
| 2211 | return space_loc; |
| 2212 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2213 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2214 | /* Find the beginning of the current paragraph if we're in one, or the |
| 2215 | * beginning of the next paragraph if we're not. Afterwards, save the |
| 2216 | * quote length and paragraph length in *quote and *par. Return FALSE |
| 2217 | * if we found a paragraph, or TRUE if there was an error or we didn't |
| 2218 | * find a paragraph. |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2219 | * |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2220 | * See the comment at begpar() for more about when a line is the |
| 2221 | * beginning of a paragraph. */ |
| 2222 | bool do_para_search(size_t *const quote, size_t *const par) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2223 | { |
| 2224 | size_t quote_len; |
| 2225 | /* Length of the initial quotation of the paragraph we |
| 2226 | * search. */ |
| 2227 | size_t par_len; |
| 2228 | /* Number of lines in that paragraph. */ |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 2229 | size_t indent_len; |
| 2230 | /* Generic indentation length. */ |
| 2231 | filestruct *line; |
| 2232 | /* Generic line of text. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2233 | |
| 2234 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2235 | if (quoterc != 0) { |
| 2236 | statusbar(_("Bad quote string %s: %s"), quotestr, quoteerr); |
| 2237 | return TRUE; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2238 | } |
| 2239 | #endif |
| 2240 | |
| 2241 | /* Here is an assumption that is always true anyway. */ |
| 2242 | assert(current != NULL); |
| 2243 | |
| 2244 | current_x = 0; |
| 2245 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2246 | quote_len = quote_length(current->data); |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2247 | indent_len = indent_length(current->data + quote_len); |
| 2248 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2249 | /* Here we find the first line of the paragraph to search. If the |
| 2250 | * current line is in a paragraph, then we move back to the first |
| 2251 | * line of the paragraph. Otherwise, we move to the first line that |
| 2252 | * is in a paragraph. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2253 | if (current->data[quote_len + indent_len] != '\0') { |
| 2254 | /* This line is part of a paragraph. So we must search back to |
| 2255 | * the first line of this paragraph. First we check items 1) |
| 2256 | * and 3) above. */ |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2257 | while (current->prev != NULL && quotes_match(current->data, |
| 2258 | quote_len, current->prev->data)) { |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2259 | size_t temp_id_len = |
| 2260 | indent_length(current->prev->data + quote_len); |
| 2261 | /* The indentation length of the previous line. */ |
| 2262 | |
| 2263 | /* Is this line the beginning of a paragraph, according to |
| 2264 | * items 2), 5), or 4) above? If so, stop. */ |
| 2265 | if (current->prev->data[quote_len + temp_id_len] == '\0' || |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2266 | (quote_len == 0 && indent_len > 0 |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2267 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2268 | && !ISSET(AUTOINDENT) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2269 | #endif |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2270 | ) || !indents_match(current->prev->data + quote_len, |
| 2271 | temp_id_len, current->data + quote_len, indent_len)) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2272 | break; |
| 2273 | indent_len = temp_id_len; |
| 2274 | current = current->prev; |
| 2275 | current_y--; |
| 2276 | } |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2277 | } else { |
| 2278 | /* This line is not part of a paragraph. Move down until we get |
| 2279 | * to a non "blank" line. */ |
| 2280 | do { |
| 2281 | /* There is no next paragraph, so nothing to move to. */ |
| 2282 | if (current->next == NULL) { |
| 2283 | placewewant = 0; |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2284 | return TRUE; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2285 | } |
| 2286 | current = current->next; |
| 2287 | current_y++; |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2288 | quote_len = quote_length(current->data); |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2289 | indent_len = indent_length(current->data + quote_len); |
| 2290 | } while (current->data[quote_len + indent_len] == '\0'); |
| 2291 | } |
| 2292 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2293 | /* Now current is the first line of the paragraph, and quote_len is |
| 2294 | * the quotation length of that line. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2295 | |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2296 | /* Next step, compute par_len, the number of lines in this |
| 2297 | * paragraph. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2298 | line = current; |
| 2299 | par_len = 1; |
| 2300 | indent_len = indent_length(line->data + quote_len); |
| 2301 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2302 | while (line->next != NULL && |
| 2303 | quotes_match(current->data, quote_len, line->next->data)) { |
| 2304 | size_t temp_id_len = indent_length(line->next->data + quote_len); |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2305 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2306 | if (!indents_match(line->data + quote_len, indent_len, |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2307 | line->next->data + quote_len, temp_id_len) || |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2308 | line->next->data[quote_len + temp_id_len] == '\0' || |
| 2309 | (quote_len == 0 && temp_id_len > 0 |
| 2310 | #ifndef NANO_SMALL |
David Lawrence Ramsey | dbde9d7 | 2004-06-27 00:54:08 +0000 | [diff] [blame] | 2311 | && !ISSET(AUTOINDENT) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2312 | #endif |
| 2313 | )) |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2314 | break; |
| 2315 | indent_len = temp_id_len; |
| 2316 | line = line->next; |
| 2317 | par_len++; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2318 | } |
| 2319 | |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2320 | /* Now par_len is the number of lines in this paragraph. We should |
| 2321 | * never call quotes_match() or quote_length() again. */ |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2322 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2323 | /* Save the values of quote_len and par_len. */ |
| 2324 | assert(quote != NULL && par != NULL); |
| 2325 | *quote = quote_len; |
| 2326 | *par = par_len; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2327 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2328 | return FALSE; |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2329 | } |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2330 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2331 | /* If full_justify is TRUE, justify the entire file. Otherwise, justify |
| 2332 | * the current paragraph. */ |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2333 | void do_justify(bool full_justify) |
David Lawrence Ramsey | 281e056 | 2004-02-27 18:54:04 +0000 | [diff] [blame] | 2334 | { |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2335 | filestruct *first_par_line = NULL; |
| 2336 | /* Will be the first line of the resulting justified paragraph. |
| 2337 | * For restoring after uncut. */ |
David Lawrence Ramsey | 36e363f | 2004-05-17 20:38:00 +0000 | [diff] [blame] | 2338 | filestruct *last_par_line; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2339 | /* Will be the last line of the result, also for uncut. */ |
| 2340 | filestruct *cutbuffer_save = cutbuffer; |
| 2341 | /* When the paragraph gets modified, all lines from the changed |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2342 | * one down are stored in the cutbuffer. We back up the |
David Lawrence Ramsey | 4e25410 | 2003-11-05 22:04:08 +0000 | [diff] [blame] | 2343 | * original to restore it later. */ |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2344 | bool allow_respacing; |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2345 | /* Whether we should change the spacing at the end of a line |
David Lawrence Ramsey | f7b5d93 | 2004-07-05 14:27:29 +0000 | [diff] [blame] | 2346 | * after justifying it. This should be TRUE whenever we move |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2347 | * to the next line after justifying the current line. */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2348 | |
| 2349 | /* We save these global variables to be restored if the user |
| 2350 | * unjustifies. Note we don't need to save totlines. */ |
| 2351 | int current_x_save = current_x; |
| 2352 | int current_y_save = current_y; |
David Lawrence Ramsey | edab0cc | 2004-07-03 03:09:12 +0000 | [diff] [blame] | 2353 | long flags_save = flags; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2354 | long totsize_save = totsize; |
David Lawrence Ramsey | 2ed225f | 2004-05-28 20:44:09 +0000 | [diff] [blame] | 2355 | filestruct *current_save = current; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2356 | filestruct *edittop_save = edittop; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2357 | #ifndef NANO_SMALL |
| 2358 | filestruct *mark_beginbuf_save = mark_beginbuf; |
| 2359 | int mark_beginx_save = mark_beginx; |
| 2360 | #endif |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2361 | int kbinput; |
| 2362 | int meta_key; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2363 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2364 | /* If we're justifying the entire file, start at the beginning. */ |
| 2365 | if (full_justify) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2366 | current = fileage; |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2367 | |
| 2368 | last_par_line = current; |
David Lawrence Ramsey | 8faf305 | 2003-09-04 20:25:29 +0000 | [diff] [blame] | 2369 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2370 | while (TRUE) { |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2371 | size_t quote_len; |
| 2372 | /* Length of the initial quotation of the paragraph we |
| 2373 | * justify. */ |
| 2374 | size_t par_len; |
| 2375 | /* Number of lines in that paragraph. */ |
| 2376 | |
| 2377 | /* Find the first line of the paragraph to be justified. That |
| 2378 | * is the start of this paragraph if we're in one, or the start |
| 2379 | * of the next otherwise. Save the quote length and paragraph |
| 2380 | * length (number of lines). Don't refresh the screen yet |
| 2381 | * (since we'll do that after we justify). If the search failed |
| 2382 | * and we're justifying the whole file, move the last line of |
| 2383 | * the text we're justifying to just before the magicline, which |
| 2384 | * is where it'll be anyway if we've searched the entire file, |
| 2385 | * and break out of the loop; otherwise, refresh the screen and |
| 2386 | * get out. */ |
| 2387 | if (do_para_search("e_len, &par_len)) { |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2388 | if (full_justify) { |
| 2389 | /* This should be safe in the event of filebot->prev's |
| 2390 | * being NULL, since only last_par_line->next is used if |
| 2391 | * we eventually unjustify. */ |
| 2392 | last_par_line = filebot->prev; |
| 2393 | break; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2394 | } else { |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2395 | edit_refresh(); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2396 | return; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2397 | } |
| 2398 | } |
| 2399 | |
| 2400 | /* Next step, we loop through the lines of this paragraph, |
| 2401 | * justifying each one individually. */ |
| 2402 | for (; par_len > 0; current_y++, par_len--) { |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2403 | size_t indent_len; /* Generic indentation length. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2404 | size_t line_len; |
| 2405 | size_t display_len; |
| 2406 | /* The width of current in screen columns. */ |
| 2407 | int break_pos; |
| 2408 | /* Where we will break the line. */ |
| 2409 | |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2410 | /* We'll be moving to the next line after justifying the |
| 2411 | * current line in almost all cases, so allow changing the |
| 2412 | * spacing at the ends of justified lines by default. */ |
| 2413 | allow_respacing = TRUE; |
| 2414 | |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2415 | indent_len = quote_len + indent_length(current->data + |
| 2416 | quote_len); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2417 | |
David Lawrence Ramsey | 91bc83a | 2004-06-25 01:52:10 +0000 | [diff] [blame] | 2418 | /* If we haven't already done it, copy the original |
| 2419 | * paragraph to the cutbuffer for unjustification. */ |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2420 | if (first_par_line == NULL) |
| 2421 | first_par_line = backup_lines(current, full_justify ? |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2422 | filebot->lineno - current->lineno : par_len, quote_len); |
| 2423 | |
David Lawrence Ramsey | 91bc83a | 2004-06-25 01:52:10 +0000 | [diff] [blame] | 2424 | /* Now we call justify_format() on the current line of the |
| 2425 | * paragraph, which will remove excess spaces from it and |
| 2426 | * change tabs to spaces. */ |
| 2427 | justify_format(current, quote_len + |
| 2428 | indent_length(current->data + quote_len)); |
| 2429 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2430 | line_len = strlen(current->data); |
| 2431 | display_len = strlenpt(current->data); |
| 2432 | |
| 2433 | if (display_len > fill) { |
| 2434 | /* The line is too long. Try to wrap it to the next. */ |
| 2435 | break_pos = break_line(current->data + indent_len, |
| 2436 | fill - strnlenpt(current->data, indent_len), TRUE); |
| 2437 | if (break_pos == -1 || break_pos + indent_len == line_len) |
| 2438 | /* We can't break the line, or don't need to, so |
| 2439 | * just go on to the next. */ |
| 2440 | goto continue_loc; |
| 2441 | break_pos += indent_len; |
| 2442 | assert(break_pos < line_len); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2443 | if (par_len == 1) { |
| 2444 | /* There is no next line in this paragraph. We make |
| 2445 | * a new line and copy text after break_pos into |
| 2446 | * it. */ |
| 2447 | splice_node(current, make_new_node(current), current->next); |
| 2448 | /* In a non-quoted paragraph, we copy the indent |
| 2449 | * only if AUTOINDENT is turned on. */ |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2450 | if (quote_len == 0 |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2451 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2452 | && !ISSET(AUTOINDENT) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2453 | #endif |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2454 | ) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2455 | indent_len = 0; |
| 2456 | current->next->data = charalloc(indent_len + line_len - |
| 2457 | break_pos); |
| 2458 | strncpy(current->next->data, current->data, indent_len); |
| 2459 | strcpy(current->next->data + indent_len, |
| 2460 | current->data + break_pos + 1); |
| 2461 | assert(strlen(current->next->data) == |
| 2462 | indent_len + line_len - break_pos - 1); |
| 2463 | totlines++; |
| 2464 | totsize += indent_len; |
| 2465 | par_len++; |
| 2466 | } else { |
| 2467 | size_t next_line_len = strlen(current->next->data); |
| 2468 | |
| 2469 | indent_len = quote_len + |
| 2470 | indent_length(current->next->data + quote_len); |
| 2471 | current->next->data = charealloc(current->next->data, |
| 2472 | next_line_len + line_len - break_pos + 1); |
| 2473 | |
| 2474 | charmove(current->next->data + indent_len + line_len - |
| 2475 | break_pos, current->next->data + indent_len, |
| 2476 | next_line_len - indent_len + 1); |
| 2477 | strcpy(current->next->data + indent_len, |
| 2478 | current->data + break_pos + 1); |
David Lawrence Ramsey | 537a880 | 2004-06-24 22:39:24 +0000 | [diff] [blame] | 2479 | current->next->data[indent_len + line_len - |
| 2480 | break_pos - 1] = ' '; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2481 | #ifndef NANO_SMALL |
| 2482 | if (mark_beginbuf == current->next) { |
| 2483 | if (mark_beginx < indent_len) |
| 2484 | mark_beginx = indent_len; |
| 2485 | mark_beginx += line_len - break_pos; |
| 2486 | } |
| 2487 | #endif |
| 2488 | } |
| 2489 | #ifndef NANO_SMALL |
| 2490 | if (mark_beginbuf == current && mark_beginx > break_pos) { |
| 2491 | mark_beginbuf = current->next; |
| 2492 | mark_beginx -= break_pos + 1 - indent_len; |
| 2493 | } |
| 2494 | #endif |
| 2495 | null_at(¤t->data, break_pos); |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2496 | |
| 2497 | /* Go to the next line. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2498 | current = current->next; |
| 2499 | } else if (display_len < fill && par_len > 1) { |
| 2500 | size_t next_line_len; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2501 | |
| 2502 | indent_len = quote_len + |
| 2503 | indent_length(current->next->data + quote_len); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2504 | /* If we can't pull a word from the next line up to this |
| 2505 | * one, just go on. */ |
| 2506 | if (!breakable(current->next->data + indent_len, |
| 2507 | fill - display_len - 1)) |
| 2508 | goto continue_loc; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2509 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2510 | break_pos = break_line(current->next->data + indent_len, |
| 2511 | fill - display_len - 1, FALSE); |
| 2512 | assert(break_pos != -1); |
| 2513 | |
| 2514 | current->data = charealloc(current->data, |
| 2515 | line_len + break_pos + 2); |
| 2516 | current->data[line_len] = ' '; |
| 2517 | strncpy(current->data + line_len + 1, |
| 2518 | current->next->data + indent_len, break_pos); |
| 2519 | current->data[line_len + break_pos + 1] = '\0'; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2520 | #ifndef NANO_SMALL |
| 2521 | if (mark_beginbuf == current->next) { |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2522 | if (mark_beginx < indent_len + break_pos) { |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2523 | mark_beginbuf = current; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2524 | if (mark_beginx <= indent_len) |
| 2525 | mark_beginx = line_len + 1; |
| 2526 | else |
David Lawrence Ramsey | 2e3aeae | 2004-05-24 05:52:35 +0000 | [diff] [blame] | 2527 | mark_beginx = line_len + 1 + mark_beginx - |
| 2528 | indent_len; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2529 | } else |
| 2530 | mark_beginx -= break_pos + 1; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2531 | } |
| 2532 | #endif |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2533 | next_line_len = strlen(current->next->data); |
| 2534 | if (indent_len + break_pos == next_line_len) { |
| 2535 | filestruct *line = current->next; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2536 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2537 | /* Don't destroy edittop! */ |
| 2538 | if (line == edittop) |
| 2539 | edittop = current; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2540 | |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2541 | unlink_node(line); |
| 2542 | delete_node(line); |
| 2543 | totlines--; |
| 2544 | totsize -= indent_len; |
| 2545 | current_y--; |
David Lawrence Ramsey | 01a6bf4 | 2004-07-03 05:23:19 +0000 | [diff] [blame] | 2546 | |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2547 | /* Don't go to the next line. Accordingly, don't |
| 2548 | * allow changing the spacing at the end of the |
David Lawrence Ramsey | 309fbcb | 2004-07-03 14:34:03 +0000 | [diff] [blame] | 2549 | * previous justified line, so that we don't end up |
| 2550 | * doing it more than once on the same line. */ |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2551 | allow_respacing = FALSE; |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2552 | } else { |
| 2553 | charmove(current->next->data + indent_len, |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2554 | current->next->data + indent_len + break_pos + 1, |
| 2555 | next_line_len - break_pos - indent_len); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2556 | null_at(¤t->next->data, next_line_len - break_pos); |
David Lawrence Ramsey | 01a6bf4 | 2004-07-03 05:23:19 +0000 | [diff] [blame] | 2557 | |
| 2558 | /* Go to the next line. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2559 | current = current->next; |
| 2560 | } |
| 2561 | } else |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2562 | continue_loc: |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2563 | /* Go to the next line. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2564 | current = current->next; |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2565 | |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2566 | /* We've moved to the next line after justifying the |
David Lawrence Ramsey | 4693864 | 2004-07-03 14:15:58 +0000 | [diff] [blame] | 2567 | * current line. If the justified line was not the last |
| 2568 | * line of the paragraph, add a space to the end of it to |
| 2569 | * replace the one removed or left out by justify_format(). |
| 2570 | * If it was the last line of the paragraph, and |
| 2571 | * justify_format() left a space on the end of it, remove |
| 2572 | * the space. */ |
| 2573 | if (allow_respacing) { |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2574 | size_t prev_line_len = strlen(current->prev->data); |
David Lawrence Ramsey | a9a6ce0 | 2004-06-05 22:09:56 +0000 | [diff] [blame] | 2575 | |
| 2576 | if (par_len > 1) { |
| 2577 | current->prev->data = charealloc(current->prev->data, |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2578 | prev_line_len + 2); |
David Lawrence Ramsey | a9a6ce0 | 2004-06-05 22:09:56 +0000 | [diff] [blame] | 2579 | current->prev->data[prev_line_len] = ' '; |
| 2580 | current->prev->data[prev_line_len + 1] = '\0'; |
| 2581 | totsize++; |
| 2582 | } else if (par_len == 1 && |
| 2583 | current->prev->data[prev_line_len - 1] == ' ') { |
| 2584 | current->prev->data = charealloc(current->prev->data, |
| 2585 | prev_line_len); |
| 2586 | current->prev->data[prev_line_len - 1] = '\0'; |
| 2587 | totsize--; |
| 2588 | } |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2589 | } |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2590 | } |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 2591 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2592 | /* We've just justified a paragraph. If we're not justifying the |
| 2593 | * entire file, break out of the loop. Otherwise, continue the |
| 2594 | * loop so that we justify all the paragraphs in the file. */ |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2595 | if (!full_justify) |
| 2596 | break; |
| 2597 | |
| 2598 | } /* while (TRUE) */ |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2599 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2600 | /* We are now done justifying the paragraph or the file, so clean |
| 2601 | * up. totlines, totsize, and current_y have been maintained above. |
| 2602 | * Set last_par_line to the new end of the paragraph, update |
David Lawrence Ramsey | 8037fe0 | 2004-07-23 12:30:40 +0000 | [diff] [blame] | 2603 | * fileage, and renumber() since edit_refresh() needs the line |
| 2604 | * numbers to be right (but only do the last two if we actually |
| 2605 | * justified something). */ |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2606 | last_par_line = current->prev; |
David Lawrence Ramsey | 8037fe0 | 2004-07-23 12:30:40 +0000 | [diff] [blame] | 2607 | if (first_par_line != NULL) { |
| 2608 | if (first_par_line->prev == NULL) |
| 2609 | fileage = first_par_line; |
| 2610 | renumber(first_par_line); |
| 2611 | } |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2612 | |
David Lawrence Ramsey | 7097d7b | 2004-05-17 16:11:18 +0000 | [diff] [blame] | 2613 | edit_refresh(); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2614 | |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 2615 | statusbar(_("Can now UnJustify!")); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2616 | /* Display the shortcut list with UnJustify. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2617 | shortcut_init(TRUE); |
Chris Allegretta | 0779835 | 2000-11-27 22:58:23 +0000 | [diff] [blame] | 2618 | display_main_list(); |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 2619 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2620 | /* Now get a keystroke and see if it's unjustify; if not, unget the |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2621 | * keystroke and return. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2622 | kbinput = get_kbinput(edit, &meta_key); |
Chris Allegretta | 5f07180 | 2001-05-06 02:34:31 +0000 | [diff] [blame] | 2623 | |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2624 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 2625 | /* If it was a mouse click, parse it with do_mouse() and it might |
| 2626 | * become the unjustify key. Else give it back to the input |
| 2627 | * stream. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2628 | if (kbinput == KEY_MOUSE) { |
| 2629 | do_mouse(); |
| 2630 | kbinput = get_kbinput(edit, &meta_key); |
| 2631 | } |
| 2632 | #endif |
| 2633 | |
| 2634 | if (meta_key || (kbinput != NANO_UNJUSTIFY_KEY && |
| 2635 | kbinput != NANO_UNJUSTIFY_FKEY)) { |
| 2636 | ungetch(kbinput); |
| 2637 | if (meta_key) |
| 2638 | ungetch(NANO_CONTROL_3); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2639 | placewewant = 0; |
Chris Allegretta | 00ae5df | 2001-02-05 18:24:33 +0000 | [diff] [blame] | 2640 | } else { |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2641 | /* Else restore the justify we just did (ungrateful user!). */ |
| 2642 | filestruct *cutbottom = get_cutbottom(); |
| 2643 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2644 | current = current_save; |
| 2645 | current_x = current_x_save; |
| 2646 | current_y = current_y_save; |
| 2647 | edittop = edittop_save; |
Chris Allegretta | d022eac | 2000-11-27 02:50:49 +0000 | [diff] [blame] | 2648 | |
David Lawrence Ramsey | 8037fe0 | 2004-07-23 12:30:40 +0000 | [diff] [blame] | 2649 | /* Splice the cutbuffer back into the file, but only if we |
| 2650 | * actually justified something. */ |
| 2651 | if (first_par_line != NULL) { |
| 2652 | cutbottom->next = last_par_line->next; |
| 2653 | cutbottom->next->prev = cutbottom; |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2654 | /* The line numbers after the end of the paragraph have been |
| 2655 | * changed, so we change them back. */ |
David Lawrence Ramsey | 8037fe0 | 2004-07-23 12:30:40 +0000 | [diff] [blame] | 2656 | renumber(cutbottom->next); |
| 2657 | if (first_par_line->prev != NULL) { |
| 2658 | cutbuffer->prev = first_par_line->prev; |
| 2659 | cutbuffer->prev->next = cutbuffer; |
| 2660 | } else |
| 2661 | fileage = cutbuffer; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2662 | |
David Lawrence Ramsey | 8037fe0 | 2004-07-23 12:30:40 +0000 | [diff] [blame] | 2663 | last_par_line->next = NULL; |
| 2664 | free_filestruct(first_par_line); |
| 2665 | } |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2666 | |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2667 | /* Restore global variables from before the justify. */ |
| 2668 | totsize = totsize_save; |
| 2669 | totlines = filebot->lineno; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2670 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2671 | mark_beginbuf = mark_beginbuf_save; |
| 2672 | mark_beginx = mark_beginx_save; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2673 | #endif |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2674 | flags = flags_save; |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2675 | if (!ISSET(MODIFIED)) |
David Lawrence Ramsey | 1cadddd | 2004-05-17 20:32:51 +0000 | [diff] [blame] | 2676 | titlebar(NULL); |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2677 | edit_refresh(); |
Chris Allegretta | 9149e61 | 2000-11-27 00:23:41 +0000 | [diff] [blame] | 2678 | } |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2679 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2680 | cutbuffer = cutbuffer_save; |
David Lawrence Ramsey | fa39404 | 2004-05-23 21:11:14 +0000 | [diff] [blame] | 2681 | /* Note that now cutbottom is invalid, but that's okay. */ |
| 2682 | blank_statusbar(); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2683 | /* Display the shortcut list with UnCut. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 2684 | shortcut_init(FALSE); |
Chris Allegretta | 4a9c858 | 2000-11-27 22:59:40 +0000 | [diff] [blame] | 2685 | display_main_list(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2686 | } |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2687 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2688 | void do_justify_void(void) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2689 | { |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2690 | do_justify(FALSE); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2691 | } |
| 2692 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2693 | void do_full_justify(void) |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2694 | { |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2695 | do_justify(TRUE); |
David Lawrence Ramsey | 8d3e7f3 | 2004-05-13 17:28:03 +0000 | [diff] [blame] | 2696 | } |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 2697 | #endif /* !DISABLE_JUSTIFY */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2698 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2699 | void do_exit(void) |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 2700 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2701 | int i; |
Chris Allegretta | 13fd44b | 2002-01-02 13:59:11 +0000 | [diff] [blame] | 2702 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2703 | if (!ISSET(MODIFIED)) |
| 2704 | i = 0; /* Pretend the user chose not to save. */ |
David Lawrence Ramsey | edab0cc | 2004-07-03 03:09:12 +0000 | [diff] [blame] | 2705 | else if (ISSET(TEMP_FILE)) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2706 | i = 1; |
David Lawrence Ramsey | f427694 | 2003-12-24 03:33:09 +0000 | [diff] [blame] | 2707 | else |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2708 | i = do_yesno(FALSE, |
| 2709 | _("Save modified buffer (ANSWERING \"No\" WILL DESTROY CHANGES) ? ")); |
| 2710 | |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2711 | #ifdef DEBUG |
| 2712 | dump_buffer(fileage); |
| 2713 | #endif |
| 2714 | |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2715 | if (i == 0 || (i == 1 && do_writeout(TRUE) > 0)) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2716 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2717 | /* Exit only if there are no more open buffers. */ |
| 2718 | if (close_open_file() != 0) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2719 | #endif |
David Lawrence Ramsey | da14106 | 2004-05-25 19:41:11 +0000 | [diff] [blame] | 2720 | finish(); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 2721 | } else if (i != 1) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2722 | statusbar(_("Cancelled")); |
| 2723 | |
| 2724 | display_main_list(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | void signal_init(void) |
| 2728 | { |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 2729 | /* Trap SIGINT and SIGQUIT because we want them to do useful |
| 2730 | * things. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2731 | memset(&act, 0, sizeof(struct sigaction)); |
| 2732 | act.sa_handler = SIG_IGN; |
| 2733 | sigaction(SIGINT, &act, NULL); |
David Lawrence Ramsey | 2897d2b | 2004-01-26 20:56:20 +0000 | [diff] [blame] | 2734 | sigaction(SIGQUIT, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2735 | |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 2736 | /* 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] | 2737 | act.sa_handler = handle_hupterm; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2738 | sigaction(SIGHUP, &act, NULL); |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 2739 | sigaction(SIGTERM, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2740 | |
| 2741 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 2742 | /* Trap SIGWINCH because we want to handle window resizes. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2743 | act.sa_handler = handle_sigwinch; |
| 2744 | sigaction(SIGWINCH, &act, NULL); |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 2745 | allow_pending_sigwinch(FALSE); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2746 | #endif |
| 2747 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2748 | /* Trap normal suspend (^Z) so we can handle it ourselves. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2749 | if (!ISSET(SUSPEND)) { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2750 | act.sa_handler = SIG_IGN; |
| 2751 | sigaction(SIGTSTP, &act, NULL); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2752 | } else { |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 2753 | /* Block all other signals in the suspend and continue handlers. |
| 2754 | * If we don't do this, other stuff interrupts them! */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2755 | sigfillset(&act.sa_mask); |
| 2756 | |
| 2757 | act.sa_handler = do_suspend; |
| 2758 | sigaction(SIGTSTP, &act, NULL); |
| 2759 | |
| 2760 | act.sa_handler = do_cont; |
| 2761 | sigaction(SIGCONT, &act, NULL); |
| 2762 | } |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2763 | } |
| 2764 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2765 | /* Handler for SIGHUP (hangup) and SIGTERM (terminate). */ |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 2766 | RETSIGTYPE handle_hupterm(int signal) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2767 | { |
Chris Allegretta | a0d8997 | 2003-02-03 03:32:08 +0000 | [diff] [blame] | 2768 | die(_("Received SIGHUP or SIGTERM\n")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2769 | } |
| 2770 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2771 | /* Handler for SIGTSTP (suspend). */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2772 | RETSIGTYPE do_suspend(int signal) |
| 2773 | { |
| 2774 | endwin(); |
Chris Allegretta | 7641708 | 2003-02-12 23:54:34 +0000 | [diff] [blame] | 2775 | printf("\n\n\n\n\n%s\n", _("Use \"fg\" to return to nano")); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2776 | fflush(stdout); |
| 2777 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2778 | /* Restore the old terminal settings. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2779 | tcsetattr(0, TCSANOW, &oldterm); |
| 2780 | |
David Lawrence Ramsey | 99bede3 | 2003-09-29 07:21:11 +0000 | [diff] [blame] | 2781 | /* 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] | 2782 | * suspended. */ |
David Lawrence Ramsey | 99bede3 | 2003-09-29 07:21:11 +0000 | [diff] [blame] | 2783 | act.sa_handler = handle_hupterm; |
| 2784 | sigaction(SIGHUP, &act, NULL); |
| 2785 | sigaction(SIGTERM, &act, NULL); |
| 2786 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2787 | /* Do what mutt does: send ourselves a SIGSTOP. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2788 | kill(0, SIGSTOP); |
| 2789 | } |
| 2790 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2791 | /* Handler for SIGCONT (continue after suspend). */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2792 | RETSIGTYPE do_cont(int signal) |
| 2793 | { |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2794 | #ifndef NANO_SMALL |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2795 | /* Perhaps the user resized the window while we slept. Handle it |
| 2796 | * and update the screen in the process. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2797 | handle_sigwinch(0); |
David Lawrence Ramsey | 5520e85 | 2004-04-07 00:44:35 +0000 | [diff] [blame] | 2798 | #else |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2799 | /* Just update the screen. */ |
| 2800 | doupdate(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2801 | #endif |
| 2802 | } |
| 2803 | |
| 2804 | #ifndef NANO_SMALL |
| 2805 | void handle_sigwinch(int s) |
| 2806 | { |
| 2807 | const char *tty = ttyname(0); |
| 2808 | int fd; |
| 2809 | int result = 0; |
| 2810 | struct winsize win; |
| 2811 | |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2812 | if (tty == NULL) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2813 | return; |
| 2814 | fd = open(tty, O_RDWR); |
| 2815 | if (fd == -1) |
| 2816 | return; |
| 2817 | result = ioctl(fd, TIOCGWINSZ, &win); |
| 2818 | close(fd); |
| 2819 | if (result == -1) |
| 2820 | return; |
| 2821 | |
| 2822 | /* Could check whether the COLS or LINES changed, and return |
| 2823 | * otherwise. EXCEPT, that COLS and LINES are ncurses global |
| 2824 | * variables, and in some cases ncurses has already updated them. |
| 2825 | * But not in all cases, argh. */ |
| 2826 | COLS = win.ws_col; |
| 2827 | LINES = win.ws_row; |
Chris Allegretta | f8f2d58 | 2003-02-10 02:43:48 +0000 | [diff] [blame] | 2828 | editwinrows = LINES - 5 + no_help(); |
| 2829 | if (editwinrows < MIN_EDITOR_ROWS || COLS < MIN_EDITOR_COLS) |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2830 | die_too_small(); |
| 2831 | |
| 2832 | #ifndef DISABLE_WRAPJUSTIFY |
| 2833 | fill = wrap_at; |
| 2834 | if (fill <= 0) |
| 2835 | fill += COLS; |
Chris Allegretta | f8f2d58 | 2003-02-10 02:43:48 +0000 | [diff] [blame] | 2836 | if (fill < 0) |
| 2837 | fill = 0; |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2838 | #endif |
| 2839 | |
David Lawrence Ramsey | 70047ee | 2003-06-14 20:41:34 +0000 | [diff] [blame] | 2840 | hblank = charealloc(hblank, COLS + 1); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2841 | memset(hblank, ' ', COLS); |
| 2842 | hblank[COLS] = '\0'; |
| 2843 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2844 | #ifdef USE_SLANG |
| 2845 | /* Slang curses emulation brain damage, part 1: If we just do what |
| 2846 | * curses does here, it'll only work properly if the resize made the |
| 2847 | * window smaller. Do what mutt does: Leave and immediately reenter |
| 2848 | * Slang screen management mode. */ |
| 2849 | SLsmg_reset_smg(); |
| 2850 | SLsmg_init_smg(); |
| 2851 | #else |
| 2852 | /* Do the equivalent of what Minimum Profit does: Leave and |
| 2853 | * immediately reenter curses mode. */ |
| 2854 | endwin(); |
| 2855 | refresh(); |
| 2856 | #endif |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2857 | |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 2858 | /* Restore the terminal to its previous state. */ |
| 2859 | terminal_init(); |
| 2860 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2861 | /* Do the equivalent of what both mutt and Minimum Profit do: |
| 2862 | * Reinitialize all the windows based on the new screen |
| 2863 | * dimensions. */ |
| 2864 | window_init(); |
| 2865 | |
| 2866 | /* Redraw the contents of the windows that need it. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2867 | blank_statusbar(); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2868 | display_main_list(); |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2869 | total_refresh(); |
| 2870 | |
David Lawrence Ramsey | 273d2ce | 2004-01-30 04:20:28 +0000 | [diff] [blame] | 2871 | /* Turn cursor back on for sure. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2872 | curs_set(1); |
| 2873 | |
David Lawrence Ramsey | 48ae986 | 2004-05-28 17:23:33 +0000 | [diff] [blame] | 2874 | /* Reset all the input routines that rely on character sequences. */ |
| 2875 | reset_kbinput(); |
| 2876 | |
David Lawrence Ramsey | 273d2ce | 2004-01-30 04:20:28 +0000 | [diff] [blame] | 2877 | /* Jump back to the main loop. */ |
David Lawrence Ramsey | ad40fdb | 2002-09-06 20:35:28 +0000 | [diff] [blame] | 2878 | siglongjmp(jmpbuf, 1); |
| 2879 | } |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 2880 | |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2881 | void allow_pending_sigwinch(bool allow) |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 2882 | { |
| 2883 | sigset_t winch; |
| 2884 | sigemptyset(&winch); |
| 2885 | sigaddset(&winch, SIGWINCH); |
| 2886 | if (allow) |
| 2887 | sigprocmask(SIG_UNBLOCK, &winch, NULL); |
| 2888 | else |
| 2889 | sigprocmask(SIG_BLOCK, &winch, NULL); |
| 2890 | } |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 2891 | #endif /* !NANO_SMALL */ |
David Lawrence Ramsey | c596755 | 2002-06-21 03:20:06 +0000 | [diff] [blame] | 2892 | |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 2893 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 0341b58 | 2002-08-21 16:10:37 +0000 | [diff] [blame] | 2894 | void do_toggle(const toggle *which) |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2895 | { |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 2896 | bool enabled; |
Chris Allegretta | f0f63a8 | 2000-09-02 18:44:21 +0000 | [diff] [blame] | 2897 | |
Chris Allegretta | 658399a | 2001-06-14 02:54:22 +0000 | [diff] [blame] | 2898 | /* Even easier! */ |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2899 | TOGGLE(which->flag); |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2900 | |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 2901 | switch (which->val) { |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2902 | case TOGGLE_SUSPEND_KEY: |
| 2903 | signal_init(); |
| 2904 | break; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 2905 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2906 | case TOGGLE_MOUSE_KEY: |
| 2907 | mouse_init(); |
| 2908 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2909 | #endif |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2910 | case TOGGLE_NOHELP_KEY: |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2911 | blank_statusbar(); |
| 2912 | blank_bottombars(); |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2913 | wrefresh(bottomwin); |
| 2914 | window_init(); |
| 2915 | edit_refresh(); |
| 2916 | display_main_list(); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2917 | break; |
Chris Allegretta | 99e30e1 | 2001-09-23 02:45:27 +0000 | [diff] [blame] | 2918 | case TOGGLE_DOS_KEY: |
| 2919 | UNSET(MAC_FILE); |
| 2920 | break; |
| 2921 | case TOGGLE_MAC_KEY: |
| 2922 | UNSET(DOS_FILE); |
| 2923 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2924 | #ifdef ENABLE_COLOR |
Chris Allegretta | 1dd0bc9 | 2002-10-13 18:43:45 +0000 | [diff] [blame] | 2925 | case TOGGLE_SYNTAX_KEY: |
| 2926 | edit_refresh(); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 2927 | break; |
| 2928 | #endif |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 2929 | #ifdef ENABLE_NANORC |
| 2930 | case TOGGLE_WHITESPACE_KEY: |
| 2931 | edit_refresh(); |
| 2932 | break; |
| 2933 | #endif |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2934 | } |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 2935 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 2936 | /* We are assuming here that shortcut_init() above didn't free and |
| 2937 | * reallocate the toggles. */ |
| 2938 | enabled = ISSET(which->flag); |
| 2939 | if (which->val == TOGGLE_NOHELP_KEY || which->val == TOGGLE_WRAP_KEY) |
| 2940 | enabled = !enabled; |
| 2941 | statusbar("%s %s", which->desc, |
| 2942 | enabled ? _("enabled") : _("disabled")); |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2943 | } |
Chris Allegretta | dab017e | 2002-04-23 10:56:06 +0000 | [diff] [blame] | 2944 | #endif /* !NANO_SMALL */ |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 2945 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2946 | void disable_signals(void) |
| 2947 | { |
| 2948 | struct termios term; |
| 2949 | |
| 2950 | tcgetattr(0, &term); |
| 2951 | term.c_lflag &= ~ISIG; |
| 2952 | tcsetattr(0, TCSANOW, &term); |
| 2953 | } |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 2954 | |
| 2955 | #ifndef NANO_SMALL |
| 2956 | void enable_signals(void) |
| 2957 | { |
| 2958 | struct termios term; |
| 2959 | |
| 2960 | tcgetattr(0, &term); |
| 2961 | term.c_lflag |= ISIG; |
| 2962 | tcsetattr(0, TCSANOW, &term); |
| 2963 | } |
| 2964 | #endif |
| 2965 | |
| 2966 | void disable_flow_control(void) |
| 2967 | { |
| 2968 | struct termios term; |
| 2969 | |
| 2970 | tcgetattr(0, &term); |
| 2971 | term.c_iflag &= ~(IXON|IXOFF); |
| 2972 | tcsetattr(0, TCSANOW, &term); |
| 2973 | } |
| 2974 | |
| 2975 | void enable_flow_control(void) |
| 2976 | { |
| 2977 | struct termios term; |
| 2978 | |
| 2979 | tcgetattr(0, &term); |
| 2980 | term.c_iflag |= (IXON|IXOFF); |
| 2981 | tcsetattr(0, TCSANOW, &term); |
| 2982 | } |
| 2983 | |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 2984 | /* Set up the terminal state. Put the terminal in cbreak mode (read one |
| 2985 | * character at a time and interpret the special control keys), disable |
| 2986 | * translation of carriage return (^M) into newline (^J) so that we can |
| 2987 | * tell the difference between the Enter key and Ctrl-J, and disable |
| 2988 | * echoing of characters as they're typed. Finally, disable |
| 2989 | * interpretation of the special control keys, and if we're not in |
| 2990 | * preserve mode, disable interpretation of the flow control characters |
| 2991 | * too. */ |
| 2992 | void terminal_init(void) |
| 2993 | { |
| 2994 | cbreak(); |
| 2995 | nonl(); |
| 2996 | noecho(); |
| 2997 | disable_signals(); |
| 2998 | if (!ISSET(PRESERVE)) |
| 2999 | disable_flow_control(); |
| 3000 | } |
| 3001 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3002 | int main(int argc, char *argv[]) |
| 3003 | { |
| 3004 | int optchr; |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 3005 | int startline = 0; /* Line to try and start at */ |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame^] | 3006 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 3007 | bool fill_flag_used = FALSE; /* Was the fill option used? */ |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame^] | 3008 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3009 | const shortcut *s; |
David Lawrence Ramsey | ce62e82 | 2004-08-05 22:10:22 +0000 | [diff] [blame] | 3010 | bool keyhandled = FALSE; /* Have we handled the keystroke yet? */ |
David Lawrence Ramsey | f8ddf31 | 2004-01-09 22:38:09 +0000 | [diff] [blame] | 3011 | int kbinput; /* Input from keyboard */ |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 3012 | int meta_key; |
Chris Allegretta | fe1d072 | 2003-02-13 00:52:49 +0000 | [diff] [blame] | 3013 | |
Chris Allegretta | 40ecbad | 2002-03-06 15:27:44 +0000 | [diff] [blame] | 3014 | #ifndef NANO_SMALL |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3015 | const toggle *t; |
Chris Allegretta | 40ecbad | 2002-03-06 15:27:44 +0000 | [diff] [blame] | 3016 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3017 | #ifdef HAVE_GETOPT_LONG |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3018 | const struct option long_options[] = { |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3019 | {"help", 0, 0, 'h'}, |
| 3020 | #ifdef ENABLE_MULTIBUFFER |
| 3021 | {"multibuffer", 0, 0, 'F'}, |
| 3022 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3023 | #ifdef ENABLE_NANORC |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 3024 | #ifndef NANO_SMALL |
Chris Allegretta | f3de8b5 | 2003-01-16 23:44:46 +0000 | [diff] [blame] | 3025 | {"historylog", 0, 0, 'H'}, |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 3026 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3027 | {"ignorercfiles", 0, 0, 'I'}, |
| 3028 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3029 | #ifndef DISABLE_JUSTIFY |
| 3030 | {"quotestr", 1, 0, 'Q'}, |
| 3031 | #endif |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 3032 | #ifdef HAVE_REGEX_H |
Chris Allegretta | bd9e7c3 | 2000-10-26 01:44:42 +0000 | [diff] [blame] | 3033 | {"regexp", 0, 0, 'R'}, |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 3034 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3035 | {"tabsize", 1, 0, 'T'}, |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3036 | {"version", 0, 0, 'V'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3037 | #ifdef ENABLE_COLOR |
| 3038 | {"syntax", 1, 0, 'Y'}, |
| 3039 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3040 | {"const", 0, 0, 'c'}, |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 3041 | {"rebinddelete", 0, 0, 'd'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3042 | {"nofollow", 0, 0, 'l'}, |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3043 | #ifndef DISABLE_MOUSE |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3044 | {"mouse", 0, 0, 'm'}, |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3045 | #endif |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 3046 | #ifndef DISABLE_OPERATINGDIR |
| 3047 | {"operatingdir", 1, 0, 'o'}, |
| 3048 | #endif |
Chris Allegretta | 6cd143d | 2003-01-05 23:35:44 +0000 | [diff] [blame] | 3049 | {"preserve", 0, 0, 'p'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3050 | #ifndef DISABLE_WRAPJUSTIFY |
| 3051 | {"fill", 1, 0, 'r'}, |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 3052 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3053 | #ifndef DISABLE_SPELLER |
| 3054 | {"speller", 1, 0, 's'}, |
| 3055 | #endif |
| 3056 | {"tempfile", 0, 0, 't'}, |
| 3057 | {"view", 0, 0, 'v'}, |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 3058 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3059 | {"nowrap", 0, 0, 'w'}, |
David Lawrence Ramsey | 2ab03f6 | 2002-10-17 02:19:31 +0000 | [diff] [blame] | 3060 | #endif |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3061 | {"nohelp", 0, 0, 'x'}, |
| 3062 | {"suspend", 0, 0, 'z'}, |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3063 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7acf69 | 2004-05-22 20:15:20 +0000 | [diff] [blame] | 3064 | {"smarthome", 0, 0, 'A'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3065 | {"backup", 0, 0, 'B'}, |
| 3066 | {"dos", 0, 0, 'D'}, |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3067 | {"backupdir", 1, 0, 'E'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3068 | {"mac", 0, 0, 'M'}, |
| 3069 | {"noconvert", 0, 0, 'N'}, |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3070 | {"smooth", 0, 0, 'S'}, |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3071 | {"restricted", 0, 0, 'Z'}, |
David Lawrence Ramsey | 5db0cdc | 2002-06-28 22:45:14 +0000 | [diff] [blame] | 3072 | {"autoindent", 0, 0, 'i'}, |
| 3073 | {"cut", 0, 0, 'k'}, |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3074 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3075 | {0, 0, 0, 0} |
| 3076 | }; |
| 3077 | #endif |
| 3078 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3079 | setlocale(LC_ALL, ""); |
David Lawrence Ramsey | ad1fd0d | 2004-07-27 15:46:58 +0000 | [diff] [blame] | 3080 | #ifdef ENABLE_NLS |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3081 | bindtextdomain(PACKAGE, LOCALEDIR); |
| 3082 | textdomain(PACKAGE); |
| 3083 | #endif |
| 3084 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3085 | #if !defined(ENABLE_NANORC) && defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING) |
David Lawrence Ramsey | dc60b72 | 2002-10-25 16:08:53 +0000 | [diff] [blame] | 3086 | /* if we don't have rcfile support, we're root, and |
| 3087 | --disable-wrapping-as-root is used, turn wrapping off */ |
David Lawrence Ramsey | a619ae6 | 2004-03-04 06:33:52 +0000 | [diff] [blame] | 3088 | if (geteuid() == NANO_ROOT_UID) |
David Lawrence Ramsey | dc60b72 | 2002-10-25 16:08:53 +0000 | [diff] [blame] | 3089 | SET(NO_WRAP); |
| 3090 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3091 | |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3092 | while ((optchr = |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3093 | #ifdef HAVE_GETOPT_LONG |
| 3094 | getopt_long(argc, argv, "h?ABDE:FHIMNQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz", long_options, NULL) |
| 3095 | #else |
| 3096 | getopt(argc, argv, "h?ABDE:FHIMNQ:RST:VY:Zabcdefgijklmo:pr:s:tvwxz") |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3097 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3098 | ) != -1) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3099 | |
| 3100 | switch (optchr) { |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3101 | case 'a': |
| 3102 | case 'b': |
| 3103 | case 'e': |
| 3104 | case 'f': |
| 3105 | case 'g': |
| 3106 | case 'j': |
| 3107 | /* Pico compatibility flags. */ |
| 3108 | break; |
Chris Allegretta | 7004c28 | 2001-09-22 00:42:10 +0000 | [diff] [blame] | 3109 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3110 | case 'A': |
| 3111 | SET(SMART_HOME); |
| 3112 | break; |
| 3113 | case 'B': |
| 3114 | SET(BACKUP_FILE); |
| 3115 | break; |
| 3116 | case 'D': |
| 3117 | SET(DOS_FILE); |
| 3118 | break; |
| 3119 | case 'E': |
| 3120 | backup_dir = mallocstrcpy(backup_dir, optarg); |
| 3121 | break; |
Chris Allegretta | 7004c28 | 2001-09-22 00:42:10 +0000 | [diff] [blame] | 3122 | #endif |
Chris Allegretta | 355fbe5 | 2001-07-14 19:32:47 +0000 | [diff] [blame] | 3123 | #ifdef ENABLE_MULTIBUFFER |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3124 | case 'F': |
| 3125 | SET(MULTIBUFFER); |
| 3126 | break; |
Chris Allegretta | 2d7893d | 2001-07-11 02:08:33 +0000 | [diff] [blame] | 3127 | #endif |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3128 | #ifdef ENABLE_NANORC |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 3129 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3130 | case 'H': |
| 3131 | SET(HISTORYLOG); |
| 3132 | break; |
David Lawrence Ramsey | 417b03a | 2003-09-06 21:44:37 +0000 | [diff] [blame] | 3133 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3134 | case 'I': |
| 3135 | SET(NO_RCFILE); |
| 3136 | break; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3137 | #endif |
Chris Allegretta | 8fa1e28 | 2001-09-22 04:20:25 +0000 | [diff] [blame] | 3138 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3139 | case 'M': |
| 3140 | SET(MAC_FILE); |
| 3141 | break; |
| 3142 | case 'N': |
| 3143 | SET(NO_CONVERT); |
| 3144 | break; |
Chris Allegretta | a8c2257 | 2002-02-15 19:17:02 +0000 | [diff] [blame] | 3145 | #endif |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 3146 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3147 | case 'Q': |
| 3148 | quotestr = mallocstrcpy(quotestr, optarg); |
| 3149 | break; |
Chris Allegretta | e4f940d | 2002-03-03 22:36:36 +0000 | [diff] [blame] | 3150 | #endif |
Chris Allegretta | 805c26d | 2000-09-06 13:39:17 +0000 | [diff] [blame] | 3151 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3152 | case 'R': |
| 3153 | SET(USE_REGEXP); |
| 3154 | break; |
Chris Allegretta | 4780561 | 2000-07-07 02:35:34 +0000 | [diff] [blame] | 3155 | #endif |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3156 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3157 | case 'S': |
| 3158 | SET(SMOOTHSCROLL); |
| 3159 | break; |
Chris Allegretta | 3e3ae94 | 2001-09-22 19:02:04 +0000 | [diff] [blame] | 3160 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3161 | case 'T': |
| 3162 | if (!parse_num(optarg, &tabsize) || tabsize <= 0) { |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 3163 | fprintf(stderr, _("Requested tab size %s invalid"), optarg); |
| 3164 | fprintf(stderr, "\n"); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3165 | exit(1); |
| 3166 | } |
| 3167 | break; |
| 3168 | case 'V': |
| 3169 | version(); |
| 3170 | exit(0); |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 3171 | #ifdef ENABLE_COLOR |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3172 | case 'Y': |
| 3173 | syntaxstr = mallocstrcpy(syntaxstr, optarg); |
| 3174 | break; |
Chris Allegretta | 09900ff | 2002-05-04 04:23:30 +0000 | [diff] [blame] | 3175 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3176 | case 'Z': |
| 3177 | SET(RESTRICTED); |
| 3178 | break; |
| 3179 | case 'c': |
| 3180 | SET(CONSTUPDATE); |
| 3181 | break; |
| 3182 | case 'd': |
| 3183 | SET(REBIND_DELETE); |
| 3184 | break; |
Chris Allegretta | ff98983 | 2001-09-17 13:48:00 +0000 | [diff] [blame] | 3185 | #ifndef NANO_SMALL |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3186 | case 'i': |
| 3187 | SET(AUTOINDENT); |
| 3188 | break; |
| 3189 | case 'k': |
| 3190 | SET(CUT_TO_END); |
| 3191 | break; |
Chris Allegretta | d19e991 | 2000-07-12 18:14:51 +0000 | [diff] [blame] | 3192 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3193 | case 'l': |
| 3194 | SET(NOFOLLOW_SYMLINKS); |
| 3195 | break; |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3196 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3197 | case 'm': |
| 3198 | SET(USE_MOUSE); |
| 3199 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3200 | #endif |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 3201 | #ifndef DISABLE_OPERATINGDIR |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3202 | case 'o': |
| 3203 | operating_dir = mallocstrcpy(operating_dir, optarg); |
| 3204 | break; |
Chris Allegretta | e1f1452 | 2001-09-19 03:19:43 +0000 | [diff] [blame] | 3205 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3206 | case 'p': |
| 3207 | SET(PRESERVE); |
| 3208 | break; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 3209 | #ifndef DISABLE_WRAPJUSTIFY |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3210 | case 'r': |
| 3211 | if (!parse_num(optarg, &wrap_at)) { |
David Lawrence Ramsey | 6420d44 | 2004-08-11 05:13:08 +0000 | [diff] [blame] | 3212 | fprintf(stderr, _("Requested fill size %s invalid"), optarg); |
| 3213 | fprintf(stderr, "\n"); |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3214 | exit(1); |
| 3215 | } |
| 3216 | fill_flag_used = TRUE; |
| 3217 | break; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 3218 | #endif |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 3219 | #ifndef DISABLE_SPELLER |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3220 | case 's': |
| 3221 | alt_speller = mallocstrcpy(alt_speller, optarg); |
| 3222 | break; |
Rocco Corsi | af5c302 | 2001-01-12 07:51:05 +0000 | [diff] [blame] | 3223 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3224 | case 't': |
| 3225 | SET(TEMP_FILE); |
| 3226 | break; |
| 3227 | case 'v': |
| 3228 | SET(VIEW_MODE); |
| 3229 | break; |
David Lawrence Ramsey | 95e0cf5 | 2003-01-02 16:32:20 +0000 | [diff] [blame] | 3230 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3231 | case 'w': |
| 3232 | SET(NO_WRAP); |
| 3233 | break; |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3234 | #endif |
David Lawrence Ramsey | c7b6ac5 | 2004-08-07 22:00:02 +0000 | [diff] [blame] | 3235 | case 'x': |
| 3236 | SET(NO_HELP); |
| 3237 | break; |
| 3238 | case 'z': |
| 3239 | SET(SUSPEND); |
| 3240 | break; |
| 3241 | default: |
| 3242 | usage(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3243 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3244 | } |
| 3245 | |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 3246 | /* If the executable filename starts with 'r', we use restricted |
| 3247 | * mode. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3248 | if (*(tail(argv[0])) == 'r') |
| 3249 | SET(RESTRICTED); |
| 3250 | |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 3251 | /* If we're using restricted mode, disable suspending, backups, and |
| 3252 | * reading rcfiles, since they all would allow reading from or |
| 3253 | * writing to files not specified on the command line. */ |
David Lawrence Ramsey | d893fa9 | 2004-04-30 04:49:02 +0000 | [diff] [blame] | 3254 | if (ISSET(RESTRICTED)) { |
| 3255 | UNSET(SUSPEND); |
| 3256 | UNSET(BACKUP_FILE); |
| 3257 | SET(NO_RCFILE); |
| 3258 | } |
| 3259 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3260 | /* We've read through the command line options. Now back up the flags |
| 3261 | and values that are set, and read the rcfile(s). If the values |
| 3262 | haven't changed afterward, restore the backed-up values. */ |
| 3263 | #ifdef ENABLE_NANORC |
| 3264 | if (!ISSET(NO_RCFILE)) { |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 3265 | #ifndef DISABLE_OPERATINGDIR |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3266 | char *operating_dir_cpy = operating_dir; |
| 3267 | #endif |
| 3268 | #ifndef DISABLE_WRAPPING |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 3269 | ssize_t wrap_at_cpy = wrap_at; |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3270 | #endif |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3271 | #ifndef NANO_SMALL |
| 3272 | char *backup_dir_cpy = backup_dir; |
| 3273 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3274 | #ifndef DISABLE_JUSTIFY |
| 3275 | char *quotestr_cpy = quotestr; |
| 3276 | #endif |
| 3277 | #ifndef DISABLE_SPELLER |
| 3278 | char *alt_speller_cpy = alt_speller; |
| 3279 | #endif |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 3280 | ssize_t tabsize_cpy = tabsize; |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3281 | long flags_cpy = flags; |
| 3282 | |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 3283 | #ifndef DISABLE_OPERATINGDIR |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3284 | operating_dir = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 3285 | #endif |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3286 | #ifndef NANO_SMALL |
| 3287 | backup_dir = NULL; |
| 3288 | #endif |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 3289 | #ifndef DISABLE_JUSTIFY |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3290 | quotestr = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 3291 | #endif |
| 3292 | #ifndef DISABLE_SPELLER |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3293 | alt_speller = NULL; |
Chris Allegretta | 5ec6862 | 2003-02-05 02:39:34 +0000 | [diff] [blame] | 3294 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3295 | |
| 3296 | do_rcfile(); |
| 3297 | |
| 3298 | #ifndef DISABLE_OPERATINGDIR |
| 3299 | if (operating_dir_cpy != NULL) { |
| 3300 | free(operating_dir); |
| 3301 | operating_dir = operating_dir_cpy; |
| 3302 | } |
| 3303 | #endif |
David Lawrence Ramsey | df13e3b | 2004-08-12 19:48:21 +0000 | [diff] [blame^] | 3304 | #ifndef DISABLE_WRAPJUSTIFY |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3305 | if (fill_flag_used) |
| 3306 | wrap_at = wrap_at_cpy; |
| 3307 | #endif |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3308 | #ifndef NANO_SMALL |
| 3309 | if (backup_dir_cpy != NULL) { |
| 3310 | free(backup_dir); |
| 3311 | backup_dir = backup_dir_cpy; |
| 3312 | } |
| 3313 | #endif |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3314 | #ifndef DISABLE_JUSTIFY |
| 3315 | if (quotestr_cpy != NULL) { |
| 3316 | free(quotestr); |
| 3317 | quotestr = quotestr_cpy; |
| 3318 | } |
| 3319 | #endif |
| 3320 | #ifndef DISABLE_SPELLER |
| 3321 | if (alt_speller_cpy != NULL) { |
| 3322 | free(alt_speller); |
| 3323 | alt_speller = alt_speller_cpy; |
| 3324 | } |
| 3325 | #endif |
David Lawrence Ramsey | 04419b9 | 2004-07-18 18:13:54 +0000 | [diff] [blame] | 3326 | if (tabsize_cpy != -1) |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3327 | tabsize = tabsize_cpy; |
| 3328 | flags |= flags_cpy; |
| 3329 | } |
| 3330 | #if defined(DISABLE_ROOTWRAP) && !defined(DISABLE_WRAPPING) |
David Lawrence Ramsey | a619ae6 | 2004-03-04 06:33:52 +0000 | [diff] [blame] | 3331 | else if (geteuid() == NANO_ROOT_UID) |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3332 | SET(NO_WRAP); |
| 3333 | #endif |
| 3334 | #endif /* ENABLE_NANORC */ |
| 3335 | |
Chris Allegretta | d845193 | 2003-03-11 03:50:40 +0000 | [diff] [blame] | 3336 | #ifndef NANO_SMALL |
| 3337 | history_init(); |
| 3338 | #ifdef ENABLE_NANORC |
| 3339 | if (!ISSET(NO_RCFILE) && ISSET(HISTORYLOG)) |
| 3340 | load_history(); |
| 3341 | #endif |
| 3342 | #endif |
| 3343 | |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3344 | #ifndef NANO_SMALL |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 3345 | /* Set up the backup directory (unless we're using restricted mode, |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 3346 | * in which case backups are disabled, since they would allow |
| 3347 | * reading from or writing to files not specified on the command |
| 3348 | * line). This entails making sure it exists and is a directory, so |
| 3349 | * that backup files will be saved there. */ |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 3350 | if (!ISSET(RESTRICTED)) |
| 3351 | init_backup_dir(); |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3352 | #endif |
| 3353 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3354 | #ifndef DISABLE_OPERATINGDIR |
| 3355 | /* Set up the operating directory. This entails chdir()ing there, |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 3356 | * so that file reads and writes will be based there. */ |
David Lawrence Ramsey | e21adfa | 2002-09-13 18:14:04 +0000 | [diff] [blame] | 3357 | init_operating_dir(); |
| 3358 | #endif |
| 3359 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3360 | #ifndef DISABLE_JUSTIFY |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 3361 | if (punct == NULL) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3362 | punct = mallocstrcpy(punct, ".?!"); |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 3363 | |
| 3364 | if (brackets == NULL) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3365 | brackets = mallocstrcpy(brackets, "'\")}]>"); |
David Lawrence Ramsey | 2c62b07 | 2004-05-29 16:38:57 +0000 | [diff] [blame] | 3366 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3367 | if (quotestr == NULL) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3368 | quotestr = mallocstrcpy(NULL, |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3369 | #ifdef HAVE_REGEX_H |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3370 | "^([ \t]*[|>:}#])+" |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3371 | #else |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3372 | "> " |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3373 | #endif |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3374 | ); |
David Lawrence Ramsey | 819c7f0 | 2004-07-30 03:54:34 +0000 | [diff] [blame] | 3375 | #ifdef HAVE_REGEX_H |
| 3376 | quoterc = regcomp("ereg, quotestr, REG_EXTENDED); |
| 3377 | |
| 3378 | if (quoterc == 0) { |
| 3379 | /* We no longer need quotestr, just quotereg. */ |
| 3380 | free(quotestr); |
| 3381 | quotestr = NULL; |
| 3382 | } else { |
| 3383 | size_t size = regerror(quoterc, "ereg, NULL, 0); |
| 3384 | |
| 3385 | quoteerr = charalloc(size); |
| 3386 | regerror(quoterc, "ereg, quoteerr, size); |
| 3387 | } |
| 3388 | #else |
| 3389 | quotelen = strlen(quotestr); |
| 3390 | #endif /* !HAVE_REGEX_H */ |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3391 | #endif /* !DISABLE_JUSTIFY */ |
David Lawrence Ramsey | 04e42a6 | 2004-02-28 16:24:31 +0000 | [diff] [blame] | 3392 | |
David Lawrence Ramsey | 3aedb36 | 2004-05-28 22:42:41 +0000 | [diff] [blame] | 3393 | #ifndef DISABLE_SPELLER |
| 3394 | /* 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] | 3395 | * 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] | 3396 | * does (unless we're using restricted mode, in which case spell |
David Lawrence Ramsey | 32e3b88 | 2004-05-29 01:20:17 +0000 | [diff] [blame] | 3397 | * checking is disabled, since it would allow reading from or |
| 3398 | * writing to files not specified on the command line). */ |
David Lawrence Ramsey | dd7cc72 | 2004-05-28 23:45:25 +0000 | [diff] [blame] | 3399 | if (!ISSET(RESTRICTED) && alt_speller == NULL) { |
David Lawrence Ramsey | 3aedb36 | 2004-05-28 22:42:41 +0000 | [diff] [blame] | 3400 | char *spellenv = getenv("SPELL"); |
| 3401 | if (spellenv != NULL) |
| 3402 | alt_speller = mallocstrcpy(NULL, spellenv); |
| 3403 | } |
| 3404 | #endif |
| 3405 | |
David Lawrence Ramsey | 483ea32 | 2004-05-29 16:25:30 +0000 | [diff] [blame] | 3406 | #if !defined(NANO_SMALL) && defined(ENABLE_NANORC) |
| 3407 | if (whitespace == NULL) |
| 3408 | whitespace = mallocstrcpy(NULL, " "); |
| 3409 | #endif |
| 3410 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3411 | if (tabsize == -1) |
| 3412 | tabsize = 8; |
| 3413 | |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 3414 | /* Clear the filename we'll be using */ |
| 3415 | filename = charalloc(1); |
| 3416 | filename[0] = '\0'; |
| 3417 | |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3418 | /* If there's a +LINE flag, it is the first non-option argument. */ |
| 3419 | if (0 < optind && optind < argc && argv[optind][0] == '+') { |
| 3420 | startline = atoi(&argv[optind][1]); |
| 3421 | optind++; |
| 3422 | } |
| 3423 | if (0 < optind && optind < argc) |
| 3424 | filename = mallocstrcpy(filename, argv[optind]); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3425 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3426 | /* See if there's a non-option in argv (first non-option is the |
| 3427 | filename, if +LINE is not given) */ |
David Lawrence Ramsey | f21cd10 | 2002-06-13 00:40:19 +0000 | [diff] [blame] | 3428 | if (argc > 1 && argc > optind) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3429 | /* Look for the +line flag... */ |
| 3430 | if (argv[optind][0] == '+') { |
| 3431 | startline = atoi(&argv[optind][1]); |
| 3432 | optind++; |
Chris Allegretta | 6df90f5 | 2002-07-19 01:08:59 +0000 | [diff] [blame] | 3433 | if (argc > optind) |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 3434 | filename = mallocstrcpy(filename, argv[optind]); |
Chris Allegretta | 1a6e904 | 2000-12-14 13:56:28 +0000 | [diff] [blame] | 3435 | } else |
| 3436 | filename = mallocstrcpy(filename, argv[optind]); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3437 | } |
| 3438 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3439 | /* Back up the old terminal settings so that they can be restored. */ |
Chris Allegretta | 9b4055c | 2002-03-29 16:00:59 +0000 | [diff] [blame] | 3440 | tcgetattr(0, &oldterm); |
Chris Allegretta | 6ca01b1 | 2002-03-29 15:38:17 +0000 | [diff] [blame] | 3441 | |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 3442 | /* Curses initialization stuff: Start curses and set up the |
| 3443 | * terminal state. */ |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3444 | initscr(); |
David Lawrence Ramsey | 8aaf030 | 2004-07-27 16:46:35 +0000 | [diff] [blame] | 3445 | terminal_init(); |
David Lawrence Ramsey | c53a92d | 2004-01-12 03:28:06 +0000 | [diff] [blame] | 3446 | |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 3447 | /* Set up the global variables and the shortcuts. */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 3448 | global_init(FALSE); |
| 3449 | shortcut_init(FALSE); |
David Lawrence Ramsey | 369732f | 2004-02-16 20:32:40 +0000 | [diff] [blame] | 3450 | |
| 3451 | /* Set up the signal handlers. */ |
Chris Allegretta | 9b4055c | 2002-03-29 16:00:59 +0000 | [diff] [blame] | 3452 | signal_init(); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3453 | |
| 3454 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 3455 | fprintf(stderr, "Main: set up windows\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3456 | #endif |
| 3457 | |
Chris Allegretta | 2a42af1 | 2000-09-12 23:02:49 +0000 | [diff] [blame] | 3458 | window_init(); |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3459 | #ifndef DISABLE_MOUSE |
Chris Allegretta | 756f220 | 2000-09-01 13:32:47 +0000 | [diff] [blame] | 3460 | mouse_init(); |
David Lawrence Ramsey | 9b13ff3 | 2002-12-22 16:30:00 +0000 | [diff] [blame] | 3461 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3462 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3463 | #ifdef DEBUG |
David Lawrence Ramsey | 69a2812 | 2004-08-12 04:34:00 +0000 | [diff] [blame] | 3464 | fprintf(stderr, "Main: top and bottom win\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3465 | #endif |
David Lawrence Ramsey | 69a2812 | 2004-08-12 04:34:00 +0000 | [diff] [blame] | 3466 | titlebar(NULL); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3467 | display_main_list(); |
| 3468 | |
| 3469 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 3470 | fprintf(stderr, "Main: open file\n"); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3471 | #endif |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 3472 | open_file(filename, FALSE, FALSE); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3473 | #ifdef ENABLE_MULTIBUFFER |
| 3474 | /* If we're using multibuffers and more than one file is specified |
| 3475 | on the command line, load them all and switch to the first one |
David Lawrence Ramsey | 69a2812 | 2004-08-12 04:34:00 +0000 | [diff] [blame] | 3476 | afterward. */ |
David Lawrence Ramsey | 1d43db8 | 2004-05-14 17:57:00 +0000 | [diff] [blame] | 3477 | if (optind + 1 < argc) { |
David Lawrence Ramsey | 69a2812 | 2004-08-12 04:34:00 +0000 | [diff] [blame] | 3478 | bool old_multibuffer = ISSET(MULTIBUFFER), list = FALSE; |
David Lawrence Ramsey | 1d43db8 | 2004-05-14 17:57:00 +0000 | [diff] [blame] | 3479 | SET(MULTIBUFFER); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3480 | for (optind++; optind < argc; optind++) { |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3481 | add_open_file(TRUE); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3482 | new_file(); |
| 3483 | filename = mallocstrcpy(filename, argv[optind]); |
David Lawrence Ramsey | 6b24856 | 2004-08-07 22:33:14 +0000 | [diff] [blame] | 3484 | titlebar(NULL); |
David Lawrence Ramsey | 576bf33 | 2004-07-12 03:10:30 +0000 | [diff] [blame] | 3485 | open_file(filename, FALSE, FALSE); |
David Lawrence Ramsey | 72e51ab | 2004-07-02 14:31:03 +0000 | [diff] [blame] | 3486 | load_file(FALSE); |
David Lawrence Ramsey | 69a2812 | 2004-08-12 04:34:00 +0000 | [diff] [blame] | 3487 | /* Display the main list with "Close" if we haven't |
| 3488 | * already. */ |
| 3489 | if (!list) { |
| 3490 | shortcut_init(FALSE); |
| 3491 | list = TRUE; |
| 3492 | display_main_list(); |
| 3493 | } |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3494 | } |
| 3495 | open_nextfile_void(); |
David Lawrence Ramsey | 1d43db8 | 2004-05-14 17:57:00 +0000 | [diff] [blame] | 3496 | if (!old_multibuffer) |
| 3497 | UNSET(MULTIBUFFER); |
Chris Allegretta | 7662c86 | 2003-01-13 01:35:15 +0000 | [diff] [blame] | 3498 | } |
| 3499 | #endif |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3500 | |
| 3501 | if (startline > 0) |
David Lawrence Ramsey | 49c3f24 | 2004-07-12 16:07:14 +0000 | [diff] [blame] | 3502 | do_gotoline(startline, FALSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3503 | |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3504 | #ifndef NANO_SMALL |
| 3505 | /* Return here after a SIGWINCH. */ |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 3506 | sigsetjmp(jmpbuf, 1); |
David Lawrence Ramsey | d7fd200 | 2004-05-18 01:20:36 +0000 | [diff] [blame] | 3507 | #endif |
Chris Allegretta | 0802088 | 2001-01-29 23:37:54 +0000 | [diff] [blame] | 3508 | |
Robert Siemborski | 6967eec | 2000-07-08 14:23:32 +0000 | [diff] [blame] | 3509 | edit_refresh(); |
Robert Siemborski | 6967eec | 2000-07-08 14:23:32 +0000 | [diff] [blame] | 3510 | |
David Lawrence Ramsey | 6aec4b8 | 2004-03-15 20:26:30 +0000 | [diff] [blame] | 3511 | while (TRUE) { |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3512 | keyhandled = FALSE; |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 3513 | |
David Lawrence Ramsey | aea4dab | 2004-07-13 17:09:24 +0000 | [diff] [blame] | 3514 | reset_cursor(); |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 3515 | if (ISSET(CONSTUPDATE)) |
David Lawrence Ramsey | 74912c6 | 2004-07-01 19:41:09 +0000 | [diff] [blame] | 3516 | do_cursorpos(TRUE); |
Chris Allegretta | d26ab91 | 2003-01-28 01:16:47 +0000 | [diff] [blame] | 3517 | |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3518 | #if !defined(DISABLE_BROWSER) || !defined(DISABLE_HELP) || !defined(DISABLE_MOUSE) |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 3519 | currshortcut = main_list; |
Chris Allegretta | 6fe6149 | 2001-05-21 12:56:25 +0000 | [diff] [blame] | 3520 | #endif |
Chris Allegretta | 6b58acd | 2001-04-12 03:01:53 +0000 | [diff] [blame] | 3521 | |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 3522 | kbinput = get_kbinput(edit, &meta_key); |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 3523 | #ifdef DEBUG |
Jordi Mallach | f9390af | 2003-08-05 19:31:12 +0000 | [diff] [blame] | 3524 | fprintf(stderr, "AHA! %c (%d)\n", kbinput, kbinput); |
Chris Allegretta | c08f50d | 2001-01-06 18:12:43 +0000 | [diff] [blame] | 3525 | #endif |
David Lawrence Ramsey | 4d44d2d | 2004-08-01 22:35:31 +0000 | [diff] [blame] | 3526 | if (meta_key) { |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 3527 | /* Check for the metaval and miscval defs... */ |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3528 | for (s = |
| 3529 | #if !defined(DISABLE_BROWSER) || !defined (DISABLE_HELP) || !defined(DISABLE_MOUSE) |
| 3530 | currshortcut |
| 3531 | #else |
| 3532 | main_list |
| 3533 | #endif |
| 3534 | ; s != NULL && !keyhandled; s = s->next) { |
David Lawrence Ramsey | 8213850 | 2003-12-24 08:03:54 +0000 | [diff] [blame] | 3535 | if ((s->metaval != NANO_NO_KEY && kbinput == s->metaval) || |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 3536 | (s->miscval != NANO_NO_KEY && kbinput == s->miscval)) { |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 3537 | if (ISSET(VIEW_MODE) && !s->viewok) |
| 3538 | print_view_warning(); |
| 3539 | else { |
| 3540 | if (s->func != do_cut_text) |
David Lawrence Ramsey | c833d9e | 2004-05-29 15:36:58 +0000 | [diff] [blame] | 3541 | cutbuffer_reset(); |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 3542 | s->func(); |
| 3543 | } |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3544 | keyhandled = TRUE; |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3545 | } |
| 3546 | #ifndef NANO_SMALL |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3547 | if (!keyhandled) { |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3548 | /* And for toggle switches */ |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3549 | for (t = toggles; t != NULL; t = t->next) { |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3550 | if (kbinput == t->val) { |
David Lawrence Ramsey | c833d9e | 2004-05-29 15:36:58 +0000 | [diff] [blame] | 3551 | cutbuffer_reset(); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3552 | do_toggle(t); |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3553 | keyhandled = TRUE; |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3554 | } |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3555 | } |
| 3556 | } |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3557 | #endif |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3558 | } |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3559 | #ifdef DEBUG |
David Lawrence Ramsey | 331e659 | 2004-07-27 19:40:39 +0000 | [diff] [blame] | 3560 | fprintf(stderr, "I got Alt-%c! (%d)\n", kbinput, kbinput); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3561 | #endif |
| 3562 | } |
| 3563 | |
| 3564 | /* Look through the main shortcut list to see if we've hit a |
| 3565 | shortcut key or function key */ |
| 3566 | |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3567 | if (!keyhandled) { |
| 3568 | for (s = |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3569 | #if !defined(DISABLE_BROWSER) || !defined (DISABLE_HELP) || !defined(DISABLE_MOUSE) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3570 | currshortcut |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3571 | #else |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3572 | main_list |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3573 | #endif |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3574 | ; s != NULL && !keyhandled; s = s->next) { |
David Lawrence Ramsey | 1576d53 | 2004-03-19 21:46:34 +0000 | [diff] [blame] | 3575 | if ((s->ctrlval != NANO_NO_KEY && kbinput == s->ctrlval) || |
| 3576 | (s->funcval != NANO_NO_KEY && kbinput == s->funcval)) { |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3577 | if (ISSET(VIEW_MODE) && !s->viewok) |
| 3578 | print_view_warning(); |
| 3579 | else { |
| 3580 | if (s->func != do_cut_text) |
David Lawrence Ramsey | c833d9e | 2004-05-29 15:36:58 +0000 | [diff] [blame] | 3581 | cutbuffer_reset(); |
David Lawrence Ramsey | a593f53 | 2003-11-28 19:47:42 +0000 | [diff] [blame] | 3582 | s->func(); |
| 3583 | } |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3584 | keyhandled = TRUE; |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 3585 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3586 | } |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3587 | } |
Chris Allegretta | e1e0fd6 | 2003-04-15 01:15:09 +0000 | [diff] [blame] | 3588 | |
| 3589 | if (!keyhandled) |
David Lawrence Ramsey | c833d9e | 2004-05-29 15:36:58 +0000 | [diff] [blame] | 3590 | cutbuffer_reset(); |
Chris Allegretta | e42df73 | 2002-10-15 00:27:55 +0000 | [diff] [blame] | 3591 | |
Chris Allegretta | e42df73 | 2002-10-15 00:27:55 +0000 | [diff] [blame] | 3592 | /* Don't even think about changing this string */ |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 3593 | if (kbinput == NANO_XON_KEY) |
Chris Allegretta | 6cd143d | 2003-01-05 23:35:44 +0000 | [diff] [blame] | 3594 | statusbar(_("XON ignored, mumble mumble.")); |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 3595 | if (kbinput == NANO_XOFF_KEY) |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 3596 | statusbar(_("XOFF ignored, mumble mumble.")); |
David Lawrence Ramsey | ebd0d7c | 2004-07-01 18:59:52 +0000 | [diff] [blame] | 3597 | if (kbinput == NANO_XON_KEY || kbinput == NANO_XOFF_KEY) |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3598 | keyhandled = TRUE; |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 3599 | |
David Lawrence Ramsey | 4d7c260 | 2003-08-17 02:48:43 +0000 | [diff] [blame] | 3600 | /* Catch ^Z by hand when triggered also */ |
| 3601 | if (kbinput == NANO_SUSPEND_KEY) { |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 3602 | if (ISSET(SUSPEND)) |
| 3603 | do_suspend(0); |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3604 | keyhandled = TRUE; |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 3605 | } |
Chris Allegretta | 9239d74 | 2000-09-06 15:19:18 +0000 | [diff] [blame] | 3606 | |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3607 | /* Last gasp, stuff that's not in the main lists */ |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3608 | if (!keyhandled) { |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3609 | switch (kbinput) { |
David Lawrence Ramsey | f5b256b | 2003-10-03 20:26:25 +0000 | [diff] [blame] | 3610 | #ifndef DISABLE_MOUSE |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3611 | case KEY_MOUSE: |
| 3612 | do_mouse(); |
| 3613 | break; |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3614 | #endif |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3615 | case NANO_CONTROL_3: /* Ctrl-[ (Esc), which should |
| 3616 | * have been handled before we |
| 3617 | * got here */ |
| 3618 | case NANO_CONTROL_5: /* Ctrl-] */ |
| 3619 | break; |
| 3620 | default: |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3621 | #ifdef DEBUG |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3622 | fprintf(stderr, "I got %c (%d)!\n", kbinput, kbinput); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3623 | #endif |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3624 | /* We no longer stop unhandled sequences so that |
| 3625 | people with odd character sets can type... */ |
| 3626 | if (ISSET(VIEW_MODE)) |
| 3627 | print_view_warning(); |
| 3628 | else |
| 3629 | do_char((char)kbinput); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3630 | } |
David Lawrence Ramsey | 74af3a7 | 2004-06-12 21:03:14 +0000 | [diff] [blame] | 3631 | } |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3632 | } |
David Lawrence Ramsey | fd1768a | 2004-03-19 21:57:56 +0000 | [diff] [blame] | 3633 | assert(FALSE); |
Chris Allegretta | a2ea193 | 2000-06-06 05:53:49 +0000 | [diff] [blame] | 3634 | } |