Rob Landley | e5e1a10 | 2006-06-21 01:15:36 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 2 | /* |
| 3 | * tiny vi.c: A small 'vi' clone |
| 4 | * Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com> |
| 5 | * |
Denys Vlasenko | 0ef64bd | 2010-08-16 20:14:46 +0200 | [diff] [blame] | 6 | * Licensed under GPLv2 or later, see file LICENSE in this source tree. |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 9 | /* |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 10 | * Things To Do: |
| 11 | * EXINIT |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 12 | * $HOME/.exrc and ./.exrc |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 13 | * add magic to search /foo.*bar |
| 14 | * add :help command |
| 15 | * :map macros |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 16 | * if mark[] values were line numbers rather than pointers |
| 17 | * it would be easier to change the mark when add/delete lines |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 18 | * More intelligence in refresh() |
| 19 | * ":r !cmd" and "!cmd" to filter text through an external command |
| 20 | * A true "undo" facility |
| 21 | * An "ex" line oriented mode- maybe using "cmdedit" |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 22 | */ |
| 23 | |
Walter Harms | b9ba580 | 2011-06-27 02:59:37 +0200 | [diff] [blame] | 24 | //config:config VI |
| 25 | //config: bool "vi" |
| 26 | //config: default y |
| 27 | //config: help |
| 28 | //config: 'vi' is a text editor. More specifically, it is the One True |
| 29 | //config: text editor <grin>. It does, however, have a rather steep |
| 30 | //config: learning curve. If you are not already comfortable with 'vi' |
| 31 | //config: you may wish to use something else. |
| 32 | //config: |
| 33 | //config:config FEATURE_VI_MAX_LEN |
| 34 | //config: int "Maximum screen width in vi" |
| 35 | //config: range 256 16384 |
| 36 | //config: default 4096 |
| 37 | //config: depends on VI |
| 38 | //config: help |
| 39 | //config: Contrary to what you may think, this is not eating much. |
| 40 | //config: Make it smaller than 4k only if you are very limited on memory. |
| 41 | //config: |
| 42 | //config:config FEATURE_VI_8BIT |
| 43 | //config: bool "Allow vi to display 8-bit chars (otherwise shows dots)" |
| 44 | //config: default n |
| 45 | //config: depends on VI |
| 46 | //config: help |
| 47 | //config: If your terminal can display characters with high bit set, |
| 48 | //config: you may want to enable this. Note: vi is not Unicode-capable. |
| 49 | //config: If your terminal combines several 8-bit bytes into one character |
| 50 | //config: (as in Unicode mode), this will not work properly. |
| 51 | //config: |
| 52 | //config:config FEATURE_VI_COLON |
| 53 | //config: bool "Enable \":\" colon commands (no \"ex\" mode)" |
| 54 | //config: default y |
| 55 | //config: depends on VI |
| 56 | //config: help |
| 57 | //config: Enable a limited set of colon commands for vi. This does not |
| 58 | //config: provide an "ex" mode. |
| 59 | //config: |
| 60 | //config:config FEATURE_VI_YANKMARK |
| 61 | //config: bool "Enable yank/put commands and mark cmds" |
| 62 | //config: default y |
| 63 | //config: depends on VI |
| 64 | //config: help |
| 65 | //config: This will enable you to use yank and put, as well as mark in |
| 66 | //config: busybox vi. |
| 67 | //config: |
| 68 | //config:config FEATURE_VI_SEARCH |
| 69 | //config: bool "Enable search and replace cmds" |
| 70 | //config: default y |
| 71 | //config: depends on VI |
| 72 | //config: help |
| 73 | //config: Select this if you wish to be able to do search and replace in |
| 74 | //config: busybox vi. |
| 75 | //config: |
| 76 | //config:config FEATURE_VI_REGEX_SEARCH |
| 77 | //config: bool "Enable regex in search and replace" |
| 78 | //config: default n # Uses GNU regex, which may be unavailable. FIXME |
| 79 | //config: depends on FEATURE_VI_SEARCH |
| 80 | //config: help |
| 81 | //config: Use extended regex search. |
| 82 | //config: |
| 83 | //config:config FEATURE_VI_USE_SIGNALS |
| 84 | //config: bool "Catch signals" |
| 85 | //config: default y |
| 86 | //config: depends on VI |
| 87 | //config: help |
| 88 | //config: Selecting this option will make busybox vi signal aware. This will |
| 89 | //config: make busybox vi support SIGWINCH to deal with Window Changes, catch |
| 90 | //config: Ctrl-Z and Ctrl-C and alarms. |
| 91 | //config: |
| 92 | //config:config FEATURE_VI_DOT_CMD |
| 93 | //config: bool "Remember previous cmd and \".\" cmd" |
| 94 | //config: default y |
| 95 | //config: depends on VI |
| 96 | //config: help |
| 97 | //config: Make busybox vi remember the last command and be able to repeat it. |
| 98 | //config: |
| 99 | //config:config FEATURE_VI_READONLY |
| 100 | //config: bool "Enable -R option and \"view\" mode" |
| 101 | //config: default y |
| 102 | //config: depends on VI |
| 103 | //config: help |
| 104 | //config: Enable the read-only command line option, which allows the user to |
| 105 | //config: open a file in read-only mode. |
| 106 | //config: |
| 107 | //config:config FEATURE_VI_SETOPTS |
| 108 | //config: bool "Enable set-able options, ai ic showmatch" |
| 109 | //config: default y |
| 110 | //config: depends on VI |
| 111 | //config: help |
| 112 | //config: Enable the editor to set some (ai, ic, showmatch) options. |
| 113 | //config: |
| 114 | //config:config FEATURE_VI_SET |
| 115 | //config: bool "Support for :set" |
| 116 | //config: default y |
| 117 | //config: depends on VI |
| 118 | //config: help |
| 119 | //config: Support for ":set". |
| 120 | //config: |
| 121 | //config:config FEATURE_VI_WIN_RESIZE |
| 122 | //config: bool "Handle window resize" |
| 123 | //config: default y |
| 124 | //config: depends on VI |
| 125 | //config: help |
| 126 | //config: Make busybox vi behave nicely with terminals that get resized. |
| 127 | //config: |
| 128 | //config:config FEATURE_VI_ASK_TERMINAL |
| 129 | //config: bool "Use 'tell me cursor position' ESC sequence to measure window" |
| 130 | //config: default y |
| 131 | //config: depends on VI |
| 132 | //config: help |
| 133 | //config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set, |
| 134 | //config: this option makes vi perform a last-ditch effort to find it: |
Denys Vlasenko | 4e552a7 | 2011-07-25 15:18:20 +0200 | [diff] [blame] | 135 | //config: position cursor to 999,999 and ask terminal to report real |
| 136 | //config: cursor position using "ESC [ 6 n" escape sequence, then read stdin. |
Walter Harms | b9ba580 | 2011-06-27 02:59:37 +0200 | [diff] [blame] | 137 | //config: |
| 138 | //config: This is not clean but helps a lot on serial lines and such. |
| 139 | //config: |
| 140 | //config:config FEATURE_VI_OPTIMIZE_CURSOR |
| 141 | //config: bool "Optimize cursor movement" |
| 142 | //config: default y |
| 143 | //config: depends on VI |
| 144 | //config: help |
| 145 | //config: This will make the cursor movement faster, but requires more memory |
| 146 | //config: and it makes the applet a tiny bit larger. |
| 147 | |
| 148 | //applet:IF_VI(APPLET(vi, BB_DIR_BIN, BB_SUID_DROP)) |
| 149 | |
| 150 | //kbuild:lib-$(CONFIG_VI) += vi.o |
| 151 | |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 152 | //usage:#define vi_trivial_usage |
| 153 | //usage: "[OPTIONS] [FILE]..." |
| 154 | //usage:#define vi_full_usage "\n\n" |
| 155 | //usage: "Edit FILE\n" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 156 | //usage: IF_FEATURE_VI_COLON( |
Denys Vlasenko | 5f556fc | 2012-06-11 01:53:33 +0200 | [diff] [blame^] | 157 | //usage: "\n -c CMD Initial command to run ($EXINIT also available)" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 158 | //usage: ) |
| 159 | //usage: IF_FEATURE_VI_READONLY( |
| 160 | //usage: "\n -R Read-only" |
| 161 | //usage: ) |
Denys Vlasenko | 5f556fc | 2012-06-11 01:53:33 +0200 | [diff] [blame^] | 162 | //usage: "\n -H List available features" |
Pere Orga | 6a3e01d | 2011-04-01 22:56:30 +0200 | [diff] [blame] | 163 | |
Denis Vlasenko | b6adbf1 | 2007-05-26 19:00:18 +0000 | [diff] [blame] | 164 | #include "libbb.h" |
Denys Vlasenko | 066f399 | 2011-07-03 03:19:43 +0200 | [diff] [blame] | 165 | /* Should be after libbb.h: on some systems regex.h needs sys/types.h: */ |
| 166 | #if ENABLE_FEATURE_VI_REGEX_SEARCH |
| 167 | # include <regex.h> |
| 168 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 169 | |
Paul Fox | 35e9c5d | 2008-03-06 16:26:12 +0000 | [diff] [blame] | 170 | /* the CRASHME code is unmaintained, and doesn't currently build */ |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 171 | #define ENABLE_FEATURE_VI_CRASHME 0 |
| 172 | |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 173 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 174 | #if ENABLE_LOCALE_SUPPORT |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 175 | |
| 176 | #if ENABLE_FEATURE_VI_8BIT |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 177 | //FIXME: this does not work properly for Unicode anyway |
| 178 | # define Isprint(c) (isprint)(c) |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 179 | #else |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 180 | # define Isprint(c) isprint_asciionly(c) |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 181 | #endif |
| 182 | |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 183 | #else |
| 184 | |
| 185 | /* 0x9b is Meta-ESC */ |
| 186 | #if ENABLE_FEATURE_VI_8BIT |
Denys Vlasenko | 066f399 | 2011-07-03 03:19:43 +0200 | [diff] [blame] | 187 | # define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b) |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 188 | #else |
Denys Vlasenko | 066f399 | 2011-07-03 03:19:43 +0200 | [diff] [blame] | 189 | # define Isprint(c) ((unsigned char)(c) >= ' ' && (unsigned char)(c) < 0x7f) |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 190 | #endif |
| 191 | |
| 192 | #endif |
| 193 | |
| 194 | |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 195 | enum { |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 196 | MAX_TABSTOP = 32, // sanity limit |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 197 | // User input len. Need not be extra big. |
| 198 | // Lines in file being edited *can* be bigger than this. |
| 199 | MAX_INPUT_LEN = 128, |
| 200 | // Sanity limits. We have only one buffer of this size. |
| 201 | MAX_SCR_COLS = CONFIG_FEATURE_VI_MAX_LEN, |
| 202 | MAX_SCR_ROWS = CONFIG_FEATURE_VI_MAX_LEN, |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 203 | }; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 204 | |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 205 | /* vt102 typical ESC sequence */ |
| 206 | /* terminal standout start/normal ESC sequence */ |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 207 | #define SOs "\033[7m" |
| 208 | #define SOn "\033[0m" |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 209 | /* terminal bell sequence */ |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 210 | #define bell "\007" |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 211 | /* Clear-end-of-line and Clear-end-of-screen ESC sequence */ |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 212 | #define Ceol "\033[K" |
| 213 | #define Ceos "\033[J" |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 214 | /* Cursor motion arbitrary destination ESC sequence */ |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 215 | #define CMrc "\033[%u;%uH" |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 216 | /* Cursor motion up and down ESC sequence */ |
Denys Vlasenko | d9a3e89 | 2010-05-16 23:42:13 +0200 | [diff] [blame] | 217 | #define CMup "\033[A" |
| 218 | #define CMdown "\n" |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 219 | |
Denis Vlasenko | ded6ad3 | 2008-10-14 12:26:30 +0000 | [diff] [blame] | 220 | #if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK |
| 221 | // cmds modifying text[] |
| 222 | // vda: removed "aAiIs" as they switch us into insert mode |
| 223 | // and remembering input for replay after them makes no sense |
| 224 | static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~"; |
| 225 | #endif |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 226 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 227 | enum { |
| 228 | YANKONLY = FALSE, |
| 229 | YANKDEL = TRUE, |
| 230 | FORWARD = 1, // code depends on "1" for array index |
| 231 | BACK = -1, // code depends on "-1" for array index |
| 232 | LIMITED = 0, // how much of text[] in char_search |
| 233 | FULL = 1, // how much of text[] in char_search |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 234 | |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 235 | S_BEFORE_WS = 1, // used in skip_thing() for moving "dot" |
| 236 | S_TO_WS = 2, // used in skip_thing() for moving "dot" |
| 237 | S_OVER_WS = 3, // used in skip_thing() for moving "dot" |
| 238 | S_END_PUNCT = 4, // used in skip_thing() for moving "dot" |
Denis Vlasenko | 8e858e2 | 2007-03-07 09:35:43 +0000 | [diff] [blame] | 239 | S_END_ALNUM = 5, // used in skip_thing() for moving "dot" |
Rob Landley | bc68cd1 | 2006-03-10 19:22:06 +0000 | [diff] [blame] | 240 | }; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 241 | |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 242 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 243 | /* vi.c expects chars to be unsigned. */ |
| 244 | /* busybox build system provides that, but it's better */ |
| 245 | /* to audit and fix the source */ |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 246 | |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 247 | struct globals { |
| 248 | /* many references - keep near the top of globals */ |
| 249 | char *text, *end; // pointers to the user data in memory |
| 250 | char *dot; // where all the action takes place |
| 251 | int text_size; // size of the allocated buffer |
| 252 | |
| 253 | /* the rest */ |
| 254 | smallint vi_setops; |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 255 | #define VI_AUTOINDENT 1 |
| 256 | #define VI_SHOWMATCH 2 |
| 257 | #define VI_IGNORECASE 4 |
| 258 | #define VI_ERR_METHOD 8 |
| 259 | #define autoindent (vi_setops & VI_AUTOINDENT) |
| 260 | #define showmatch (vi_setops & VI_SHOWMATCH ) |
| 261 | #define ignorecase (vi_setops & VI_IGNORECASE) |
| 262 | /* indicate error with beep or flash */ |
| 263 | #define err_method (vi_setops & VI_ERR_METHOD) |
| 264 | |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 265 | #if ENABLE_FEATURE_VI_READONLY |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 266 | smallint readonly_mode; |
Denis Vlasenko | 6a2f7f4 | 2007-08-16 10:35:17 +0000 | [diff] [blame] | 267 | #define SET_READONLY_FILE(flags) ((flags) |= 0x01) |
| 268 | #define SET_READONLY_MODE(flags) ((flags) |= 0x02) |
| 269 | #define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe) |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 270 | #else |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 271 | #define SET_READONLY_FILE(flags) ((void)0) |
| 272 | #define SET_READONLY_MODE(flags) ((void)0) |
| 273 | #define UNSET_READONLY_FILE(flags) ((void)0) |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 274 | #endif |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 275 | |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 276 | smallint editing; // >0 while we are editing a file |
Denis Vlasenko | 30cfdf9 | 2008-09-21 15:29:29 +0000 | [diff] [blame] | 277 | // [code audit says "can be 0, 1 or 2 only"] |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 278 | smallint cmd_mode; // 0=command 1=insert 2=replace |
| 279 | int file_modified; // buffer contents changed (counter, not flag!) |
Denis Vlasenko | 30cfdf9 | 2008-09-21 15:29:29 +0000 | [diff] [blame] | 280 | int last_file_modified; // = -1; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 281 | int save_argc; // how many file names on cmd line |
| 282 | int cmdcnt; // repetition count |
Tanguy Pruvot | 6fef6a3 | 2012-05-05 15:26:43 +0200 | [diff] [blame] | 283 | int rows, columns; // the terminal screen is this size |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 284 | #if ENABLE_FEATURE_VI_ASK_TERMINAL |
| 285 | int get_rowcol_error; |
| 286 | #endif |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 287 | int crow, ccol; // cursor is on Crow x Ccol |
| 288 | int offset; // chars scrolled off the screen to the left |
| 289 | int have_status_msg; // is default edit status needed? |
| 290 | // [don't make smallint!] |
| 291 | int last_status_cksum; // hash of current status line |
| 292 | char *current_filename; |
| 293 | char *screenbegin; // index into text[], of top line on the screen |
| 294 | char *screen; // pointer to the virtual screen buffer |
| 295 | int screensize; // and its size |
| 296 | int tabstop; |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 297 | int last_forward_char; // last char searched for with 'f' (int because of Unicode) |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 298 | char erase_char; // the users erase character |
| 299 | char last_input_char; // last char read from user |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 300 | |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 301 | #if ENABLE_FEATURE_VI_DOT_CMD |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 302 | smallint adding2q; // are we currently adding user input to q |
| 303 | int lmc_len; // length of last_modifying_cmd |
| 304 | char *ioq, *ioq_start; // pointer to string for get_one_char to "read" |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 305 | #endif |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 306 | #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 307 | int last_row; // where the cursor was last moved to |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 308 | #endif |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 309 | #if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 310 | int my_pid; |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 311 | #endif |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 312 | #if ENABLE_FEATURE_VI_SEARCH |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 313 | char *last_search_pattern; // last pattern from a '/' or '?' search |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 314 | #endif |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 315 | |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 316 | /* former statics */ |
| 317 | #if ENABLE_FEATURE_VI_YANKMARK |
| 318 | char *edit_file__cur_line; |
| 319 | #endif |
| 320 | int refresh__old_offset; |
| 321 | int format_edit_status__tot; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 322 | |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 323 | /* a few references only */ |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 324 | #if ENABLE_FEATURE_VI_YANKMARK |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 325 | int YDreg, Ureg; // default delete register and orig line for "U" |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 326 | char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27 |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 327 | char *mark[28]; // user marks points somewhere in text[]- a-z and previous context '' |
| 328 | char *context_start, *context_end; |
| 329 | #endif |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 330 | #if ENABLE_FEATURE_VI_USE_SIGNALS |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 331 | sigjmp_buf restart; // catch_sig() |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 332 | #endif |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 333 | struct termios term_orig, term_vi; // remember what the cooked mode was |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 334 | #if ENABLE_FEATURE_VI_COLON |
| 335 | char *initial_cmds[3]; // currently 2 entries, NULL terminated |
| 336 | #endif |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 337 | // Should be just enough to hold a key sequence, |
Denis Vlasenko | 25497c1 | 2008-10-14 10:25:05 +0000 | [diff] [blame] | 338 | // but CRASHME mode uses it as generated command buffer too |
| 339 | #if ENABLE_FEATURE_VI_CRASHME |
Denis Vlasenko | 1dfeeeb | 2008-10-18 19:04:37 +0000 | [diff] [blame] | 340 | char readbuffer[128]; |
Denis Vlasenko | 25497c1 | 2008-10-14 10:25:05 +0000 | [diff] [blame] | 341 | #else |
Denis Vlasenko | 5f6aaf3 | 2008-10-25 23:27:29 +0000 | [diff] [blame] | 342 | char readbuffer[KEYCODE_BUFFER_SIZE]; |
Denis Vlasenko | 25497c1 | 2008-10-14 10:25:05 +0000 | [diff] [blame] | 343 | #endif |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 344 | #define STATUS_BUFFER_LEN 200 |
| 345 | char status_buffer[STATUS_BUFFER_LEN]; // messages to the user |
| 346 | #if ENABLE_FEATURE_VI_DOT_CMD |
| 347 | char last_modifying_cmd[MAX_INPUT_LEN]; // last modifying cmd for "." |
| 348 | #endif |
| 349 | char get_input_line__buf[MAX_INPUT_LEN]; /* former static */ |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 350 | |
| 351 | char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2]; |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 352 | }; |
| 353 | #define G (*ptr_to_globals) |
| 354 | #define text (G.text ) |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 355 | #define text_size (G.text_size ) |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 356 | #define end (G.end ) |
| 357 | #define dot (G.dot ) |
| 358 | #define reg (G.reg ) |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 359 | |
| 360 | #define vi_setops (G.vi_setops ) |
| 361 | #define editing (G.editing ) |
| 362 | #define cmd_mode (G.cmd_mode ) |
| 363 | #define file_modified (G.file_modified ) |
| 364 | #define last_file_modified (G.last_file_modified ) |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 365 | #define save_argc (G.save_argc ) |
| 366 | #define cmdcnt (G.cmdcnt ) |
| 367 | #define rows (G.rows ) |
| 368 | #define columns (G.columns ) |
| 369 | #define crow (G.crow ) |
| 370 | #define ccol (G.ccol ) |
| 371 | #define offset (G.offset ) |
| 372 | #define status_buffer (G.status_buffer ) |
| 373 | #define have_status_msg (G.have_status_msg ) |
| 374 | #define last_status_cksum (G.last_status_cksum ) |
| 375 | #define current_filename (G.current_filename ) |
| 376 | #define screen (G.screen ) |
| 377 | #define screensize (G.screensize ) |
| 378 | #define screenbegin (G.screenbegin ) |
| 379 | #define tabstop (G.tabstop ) |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 380 | #define last_forward_char (G.last_forward_char ) |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 381 | #define erase_char (G.erase_char ) |
| 382 | #define last_input_char (G.last_input_char ) |
Denis Vlasenko | 2a210e5 | 2008-06-22 13:20:42 +0000 | [diff] [blame] | 383 | #if ENABLE_FEATURE_VI_READONLY |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 384 | #define readonly_mode (G.readonly_mode ) |
Denis Vlasenko | 2a210e5 | 2008-06-22 13:20:42 +0000 | [diff] [blame] | 385 | #else |
| 386 | #define readonly_mode 0 |
| 387 | #endif |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 388 | #define adding2q (G.adding2q ) |
| 389 | #define lmc_len (G.lmc_len ) |
| 390 | #define ioq (G.ioq ) |
| 391 | #define ioq_start (G.ioq_start ) |
| 392 | #define last_row (G.last_row ) |
| 393 | #define my_pid (G.my_pid ) |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 394 | #define last_search_pattern (G.last_search_pattern) |
Denis Vlasenko | 2a210e5 | 2008-06-22 13:20:42 +0000 | [diff] [blame] | 395 | |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 396 | #define edit_file__cur_line (G.edit_file__cur_line) |
| 397 | #define refresh__old_offset (G.refresh__old_offset) |
| 398 | #define format_edit_status__tot (G.format_edit_status__tot) |
| 399 | |
Denis Vlasenko | 0b3b41b | 2007-05-30 02:01:40 +0000 | [diff] [blame] | 400 | #define YDreg (G.YDreg ) |
| 401 | #define Ureg (G.Ureg ) |
| 402 | #define mark (G.mark ) |
| 403 | #define context_start (G.context_start ) |
| 404 | #define context_end (G.context_end ) |
| 405 | #define restart (G.restart ) |
| 406 | #define term_orig (G.term_orig ) |
| 407 | #define term_vi (G.term_vi ) |
| 408 | #define initial_cmds (G.initial_cmds ) |
Denis Vlasenko | a96425f | 2007-12-09 04:13:43 +0000 | [diff] [blame] | 409 | #define readbuffer (G.readbuffer ) |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 410 | #define scr_out_buf (G.scr_out_buf ) |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 411 | #define last_modifying_cmd (G.last_modifying_cmd ) |
| 412 | #define get_input_line__buf (G.get_input_line__buf) |
| 413 | |
Denis Vlasenko | a96425f | 2007-12-09 04:13:43 +0000 | [diff] [blame] | 414 | #define INIT_G() do { \ |
Denis Vlasenko | 574f2f4 | 2008-02-27 18:41:59 +0000 | [diff] [blame] | 415 | SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \ |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 416 | last_file_modified = -1; \ |
Denis Vlasenko | 31d58e5 | 2008-10-29 13:16:28 +0000 | [diff] [blame] | 417 | /* "" but has space for 2 chars: */ \ |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 418 | IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \ |
Denis Vlasenko | a96425f | 2007-12-09 04:13:43 +0000 | [diff] [blame] | 419 | } while (0) |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 420 | |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 421 | |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 422 | static int init_text_buffer(char *); // init from file or create new |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 423 | static void edit_file(char *); // edit one file |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 424 | static void do_cmd(int); // execute a command |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 425 | static int next_tabstop(int); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 426 | static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot |
| 427 | static char *begin_line(char *); // return pointer to cur line B-o-l |
| 428 | static char *end_line(char *); // return pointer to cur line E-o-l |
| 429 | static char *prev_line(char *); // return pointer to prev line B-o-l |
| 430 | static char *next_line(char *); // return pointer to next line B-o-l |
| 431 | static char *end_screen(void); // get pointer to last char on screen |
| 432 | static int count_lines(char *, char *); // count line from start to stop |
| 433 | static char *find_line(int); // find begining of line #li |
| 434 | static char *move_to_col(char *, int); // move "p" to column l |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 435 | static void dot_left(void); // move dot left- dont leave line |
| 436 | static void dot_right(void); // move dot right- dont leave line |
| 437 | static void dot_begin(void); // move dot to B-o-l |
| 438 | static void dot_end(void); // move dot to E-o-l |
| 439 | static void dot_next(void); // move dot to next line B-o-l |
| 440 | static void dot_prev(void); // move dot to prev line B-o-l |
| 441 | static void dot_scroll(int, int); // move the screen up or down |
| 442 | static void dot_skip_over_ws(void); // move dot pat WS |
| 443 | static void dot_delete(void); // delete the char at 'dot' |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 444 | static char *bound_dot(char *); // make sure text[0] <= P < "end" |
| 445 | static char *new_screen(int, int); // malloc virtual screen memory |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 446 | static char *char_insert(char *, char); // insert the char c at 'p' |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 447 | // might reallocate text[]! use p += stupid_insert(p, ...), |
| 448 | // and be careful to not use pointers into potentially freed text[]! |
| 449 | static uintptr_t stupid_insert(char *, char); // stupidly insert the char c at 'p' |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 450 | static int find_range(char **, char **, char); // return pointers for an object |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 451 | static int st_test(char *, int, int, char *); // helper for skip_thing() |
| 452 | static char *skip_thing(char *, int, int, int); // skip some object |
| 453 | static char *find_pair(char *, char); // find matching pair () [] {} |
| 454 | static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 455 | // might reallocate text[]! use p += text_hole_make(p, ...), |
| 456 | // and be careful to not use pointers into potentially freed text[]! |
| 457 | static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 458 | static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 459 | static void show_help(void); // display some help info |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 460 | static void rawmode(void); // set "raw" mode on tty |
| 461 | static void cookmode(void); // return to "cooked" mode on tty |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 462 | // sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready) |
| 463 | static int mysleep(int); |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 464 | static int readit(void); // read (maybe cursor) key from stdin |
| 465 | static int get_one_char(void); // read 1 char from stdin |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 466 | static int file_size(const char *); // what is the byte size of "fn" |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 467 | #if !ENABLE_FEATURE_VI_READONLY |
| 468 | #define file_insert(fn, p, update_ro_status) file_insert(fn, p) |
Denis Vlasenko | 59a1f30 | 2007-07-14 22:43:10 +0000 | [diff] [blame] | 469 | #endif |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 470 | // file_insert might reallocate text[]! |
| 471 | static int file_insert(const char *, char *, int); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 472 | static int file_write(char *, char *, char *); |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 473 | #if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR |
| 474 | #define place_cursor(a, b, optimize) place_cursor(a, b) |
| 475 | #endif |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 476 | static void place_cursor(int, int, int); |
Eric Andersen | bff7a60 | 2001-11-17 07:15:43 +0000 | [diff] [blame] | 477 | static void screen_erase(void); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 478 | static void clear_to_eol(void); |
| 479 | static void clear_to_eos(void); |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 480 | static void go_bottom_and_clear_to_eol(void); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 481 | static void standout_start(void); // send "start reverse video" sequence |
| 482 | static void standout_end(void); // send "end reverse video" sequence |
| 483 | static void flash(int); // flash the terminal screen |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 484 | static void show_status_line(void); // put a message on the bottom line |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 485 | static void status_line(const char *, ...); // print to status buf |
| 486 | static void status_line_bold(const char *, ...); |
| 487 | static void not_implemented(const char *); // display "Not implemented" message |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 488 | static int format_edit_status(void); // format file status on status line |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 489 | static void redraw(int); // force a full screen refresh |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 490 | static char* format_line(char* /*, int*/); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 491 | static void refresh(int); // update the terminal from screen[] |
| 492 | |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 493 | static void Indicate_Error(void); // use flash or beep to indicate error |
| 494 | #define indicate_error(c) Indicate_Error() |
Paul Fox | 90372ed | 2005-10-09 14:26:26 +0000 | [diff] [blame] | 495 | static void Hit_Return(void); |
| 496 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 497 | #if ENABLE_FEATURE_VI_SEARCH |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 498 | static char *char_search(char *, const char *, int, int); // search for pattern starting at p |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 499 | #endif |
| 500 | #if ENABLE_FEATURE_VI_COLON |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 501 | static char *get_one_address(char *, int *); // get colon addr, if present |
| 502 | static char *get_address(char *, int *, int *); // get two colon addrs, if present |
| 503 | static void colon(char *); // execute the "colon" mode cmds |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 504 | #endif |
| 505 | #if ENABLE_FEATURE_VI_USE_SIGNALS |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 506 | static void winch_sig(int); // catch window size changes |
| 507 | static void suspend_sig(int); // catch ctrl-Z |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 508 | static void catch_sig(int); // catch ctrl-C and alarm time-outs |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 509 | #endif |
| 510 | #if ENABLE_FEATURE_VI_DOT_CMD |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 511 | static void start_new_cmd_q(char); // new queue for command |
Eric Andersen | bff7a60 | 2001-11-17 07:15:43 +0000 | [diff] [blame] | 512 | static void end_cmd_q(void); // stop saving input chars |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 513 | #else |
| 514 | #define end_cmd_q() ((void)0) |
| 515 | #endif |
| 516 | #if ENABLE_FEATURE_VI_SETOPTS |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 517 | static void showmatching(char *); // show the matching pair () [] {} |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 518 | #endif |
| 519 | #if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 520 | // might reallocate text[]! use p += string_insert(p, ...), |
| 521 | // and be careful to not use pointers into potentially freed text[]! |
| 522 | static uintptr_t string_insert(char *, const char *); // insert the string at 'p' |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 523 | #endif |
| 524 | #if ENABLE_FEATURE_VI_YANKMARK |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 525 | static char *text_yank(char *, char *, int); // save copy of "p" into a register |
| 526 | static char what_reg(void); // what is letter of current YDreg |
| 527 | static void check_context(char); // remember context for '' command |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 528 | #endif |
| 529 | #if ENABLE_FEATURE_VI_CRASHME |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 530 | static void crash_dummy(); |
| 531 | static void crash_test(); |
| 532 | static int crashme = 0; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 533 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 534 | |
| 535 | |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 536 | static void write1(const char *out) |
| 537 | { |
| 538 | fputs(out, stdout); |
| 539 | } |
| 540 | |
Denis Vlasenko | 9b49a5e | 2007-10-11 10:05:36 +0000 | [diff] [blame] | 541 | int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE; |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 542 | int vi_main(int argc, char **argv) |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 543 | { |
Eric Andersen | d402edf | 2001-04-04 19:29:48 +0000 | [diff] [blame] | 544 | int c; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 545 | |
| 546 | INIT_G(); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 547 | |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 548 | #if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 549 | my_pid = getpid(); |
| 550 | #endif |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 551 | #if ENABLE_FEATURE_VI_CRASHME |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 552 | srand((long) my_pid); |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 553 | #endif |
Denis Vlasenko | 2414a96 | 2007-07-18 22:03:40 +0000 | [diff] [blame] | 554 | #ifdef NO_SUCH_APPLET_YET |
| 555 | /* If we aren't "vi", we are "view" */ |
| 556 | if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) { |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 557 | SET_READONLY_MODE(readonly_mode); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 558 | } |
Denis Vlasenko | 2414a96 | 2007-07-18 22:03:40 +0000 | [diff] [blame] | 559 | #endif |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 560 | |
Denys Vlasenko | 5f556fc | 2012-06-11 01:53:33 +0200 | [diff] [blame^] | 561 | // autoindent is not default in vim 7.3 |
| 562 | vi_setops = /*VI_AUTOINDENT |*/ VI_SHOWMATCH | VI_IGNORECASE; |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 563 | // 1- process $HOME/.exrc file (not inplemented yet) |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 564 | // 2- process EXINIT variable from environment |
| 565 | // 3- process command line args |
Denis Vlasenko | 58875ae | 2007-03-22 22:22:10 +0000 | [diff] [blame] | 566 | #if ENABLE_FEATURE_VI_COLON |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 567 | { |
| 568 | char *p = getenv("EXINIT"); |
| 569 | if (p && *p) |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 570 | initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN); |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 571 | } |
Denis Vlasenko | 58875ae | 2007-03-22 22:22:10 +0000 | [diff] [blame] | 572 | #endif |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 573 | while ((c = getopt(argc, argv, "hCRH" IF_FEATURE_VI_COLON("c:"))) != -1) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 574 | switch (c) { |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 575 | #if ENABLE_FEATURE_VI_CRASHME |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 576 | case 'C': |
| 577 | crashme = 1; |
| 578 | break; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 579 | #endif |
| 580 | #if ENABLE_FEATURE_VI_READONLY |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 581 | case 'R': // Read-only flag |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 582 | SET_READONLY_MODE(readonly_mode); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 583 | break; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 584 | #endif |
Denis Vlasenko | 58875ae | 2007-03-22 22:22:10 +0000 | [diff] [blame] | 585 | #if ENABLE_FEATURE_VI_COLON |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 586 | case 'c': // cmd line vi command |
| 587 | if (*optarg) |
Denys Vlasenko | 5f556fc | 2012-06-11 01:53:33 +0200 | [diff] [blame^] | 588 | initial_cmds[initial_cmds[0] != NULL] = xstrndup(optarg, MAX_INPUT_LEN); |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 589 | break; |
Denis Vlasenko | 58875ae | 2007-03-22 22:22:10 +0000 | [diff] [blame] | 590 | #endif |
Paul Fox | 35e9c5d | 2008-03-06 16:26:12 +0000 | [diff] [blame] | 591 | case 'H': |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 592 | show_help(); |
Paul Fox | 35e9c5d | 2008-03-06 16:26:12 +0000 | [diff] [blame] | 593 | /* fall through */ |
Paul Fox | 35e9c5d | 2008-03-06 16:26:12 +0000 | [diff] [blame] | 594 | default: |
Denis Vlasenko | c693840 | 2008-03-24 02:18:03 +0000 | [diff] [blame] | 595 | bb_show_usage(); |
Eric Andersen | dd8500b | 2001-07-02 18:06:14 +0000 | [diff] [blame] | 596 | return 1; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 597 | } |
| 598 | } |
| 599 | |
| 600 | // The argv array can be used by the ":next" and ":rewind" commands |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 601 | argv += optind; |
| 602 | argc -= optind; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 603 | save_argc = argc; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 604 | optind = 0; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 605 | |
| 606 | //----- This is the main file handling loop -------------- |
Denys Vlasenko | 04cecd5 | 2010-04-16 22:13:55 -0700 | [diff] [blame] | 607 | while (1) { |
| 608 | edit_file(argv[optind]); /* param might be NULL */ |
| 609 | if (++optind >= argc) |
| 610 | break; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 611 | } |
| 612 | //----------------------------------------------------------- |
| 613 | |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 614 | return 0; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 615 | } |
| 616 | |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 617 | /* read text from file or create an empty buf */ |
| 618 | /* will also update current_filename */ |
| 619 | static int init_text_buffer(char *fn) |
| 620 | { |
| 621 | int rc; |
| 622 | int size = file_size(fn); // file size. -1 means does not exist. |
| 623 | |
| 624 | /* allocate/reallocate text buffer */ |
| 625 | free(text); |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 626 | text_size = size + 10240; |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 627 | screenbegin = dot = end = text = xzalloc(text_size); |
Denis Vlasenko | 2f6ae43 | 2007-07-19 22:50:47 +0000 | [diff] [blame] | 628 | |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 629 | if (fn != current_filename) { |
| 630 | free(current_filename); |
| 631 | current_filename = xstrdup(fn); |
| 632 | } |
| 633 | if (size < 0) { |
| 634 | // file dont exist. Start empty buf with dummy line |
| 635 | char_insert(text, '\n'); |
| 636 | rc = 0; |
| 637 | } else { |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 638 | rc = file_insert(fn, text, 1); |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 639 | } |
| 640 | file_modified = 0; |
| 641 | last_file_modified = -1; |
| 642 | #if ENABLE_FEATURE_VI_YANKMARK |
| 643 | /* init the marks. */ |
| 644 | memset(mark, 0, sizeof(mark)); |
| 645 | #endif |
| 646 | return rc; |
Denis Vlasenko | 2f6ae43 | 2007-07-19 22:50:47 +0000 | [diff] [blame] | 647 | } |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 648 | |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 649 | #if ENABLE_FEATURE_VI_WIN_RESIZE |
Denys Vlasenko | c5fb0ad | 2010-07-21 12:39:42 +0200 | [diff] [blame] | 650 | static int query_screen_dimensions(void) |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 651 | { |
Tanguy Pruvot | 6fef6a3 | 2012-05-05 15:26:43 +0200 | [diff] [blame] | 652 | unsigned c, r; |
| 653 | int err = get_terminal_width_height(STDIN_FILENO, &c, &r); |
| 654 | columns = c; rows = r; |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 655 | if (rows > MAX_SCR_ROWS) |
| 656 | rows = MAX_SCR_ROWS; |
| 657 | if (columns > MAX_SCR_COLS) |
| 658 | columns = MAX_SCR_COLS; |
Denys Vlasenko | c5fb0ad | 2010-07-21 12:39:42 +0200 | [diff] [blame] | 659 | return err; |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 660 | } |
| 661 | #else |
Denys Vlasenko | c5fb0ad | 2010-07-21 12:39:42 +0200 | [diff] [blame] | 662 | # define query_screen_dimensions() (0) |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 663 | #endif |
| 664 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 665 | static void edit_file(char *fn) |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 666 | { |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 667 | #if ENABLE_FEATURE_VI_YANKMARK |
| 668 | #define cur_line edit_file__cur_line |
| 669 | #endif |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 670 | int c; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 671 | #if ENABLE_FEATURE_VI_USE_SIGNALS |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 672 | int sig; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 673 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 674 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 675 | editing = 1; // 0 = exit, 1 = one file, 2 = multiple files |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 676 | rawmode(); |
| 677 | rows = 24; |
| 678 | columns = 80; |
Denys Vlasenko | c5fb0ad | 2010-07-21 12:39:42 +0200 | [diff] [blame] | 679 | IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions(); |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 680 | #if ENABLE_FEATURE_VI_ASK_TERMINAL |
| 681 | if (G.get_rowcol_error /* TODO? && no input on stdin */) { |
| 682 | uint64_t k; |
| 683 | write1("\033[999;999H" "\033[6n"); |
| 684 | fflush_all(); |
| 685 | k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100); |
| 686 | if ((int32_t)k == KEYCODE_CURSOR_POS) { |
| 687 | uint32_t rc = (k >> 32); |
| 688 | columns = (rc & 0x7fff); |
Denys Vlasenko | c5fb0ad | 2010-07-21 12:39:42 +0200 | [diff] [blame] | 689 | if (columns > MAX_SCR_COLS) |
| 690 | columns = MAX_SCR_COLS; |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 691 | rows = ((rc >> 16) & 0x7fff); |
Denys Vlasenko | c5fb0ad | 2010-07-21 12:39:42 +0200 | [diff] [blame] | 692 | if (rows > MAX_SCR_ROWS) |
| 693 | rows = MAX_SCR_ROWS; |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 694 | } |
Denys Vlasenko | c175c46 | 2010-04-18 22:09:30 -0700 | [diff] [blame] | 695 | } |
| 696 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 697 | new_screen(rows, columns); // get memory for virtual screen |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 698 | init_text_buffer(fn); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 699 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 700 | #if ENABLE_FEATURE_VI_YANKMARK |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 701 | YDreg = 26; // default Yank/Delete reg |
| 702 | Ureg = 27; // hold orig line for "U" cmd |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 703 | mark[26] = mark[27] = text; // init "previous context" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 704 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 705 | |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 706 | last_forward_char = last_input_char = '\0'; |
| 707 | crow = 0; |
| 708 | ccol = 0; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 709 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 710 | #if ENABLE_FEATURE_VI_USE_SIGNALS |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 711 | signal(SIGINT, catch_sig); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 712 | signal(SIGWINCH, winch_sig); |
| 713 | signal(SIGTSTP, suspend_sig); |
Paul Fox | 2724fa9 | 2008-03-17 15:28:07 +0000 | [diff] [blame] | 714 | sig = sigsetjmp(restart, 1); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 715 | if (sig != 0) { |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 716 | screenbegin = dot = text; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 717 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 718 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 719 | |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 720 | cmd_mode = 0; // 0=command 1=insert 2='R'eplace |
| 721 | cmdcnt = 0; |
| 722 | tabstop = 8; |
| 723 | offset = 0; // no horizontal offset |
| 724 | c = '\0'; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 725 | #if ENABLE_FEATURE_VI_DOT_CMD |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 726 | free(ioq_start); |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 727 | ioq = ioq_start = NULL; |
| 728 | lmc_len = 0; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 729 | adding2q = 0; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 730 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 731 | |
Denis Vlasenko | 58875ae | 2007-03-22 22:22:10 +0000 | [diff] [blame] | 732 | #if ENABLE_FEATURE_VI_COLON |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 733 | { |
| 734 | char *p, *q; |
| 735 | int n = 0; |
| 736 | |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 737 | while ((p = initial_cmds[n]) != NULL) { |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 738 | do { |
| 739 | q = p; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 740 | p = strchr(q, '\n'); |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 741 | if (p) |
Denis Vlasenko | 51742f4 | 2007-04-12 00:32:05 +0000 | [diff] [blame] | 742 | while (*p == '\n') |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 743 | *p++ = '\0'; |
| 744 | if (*q) |
| 745 | colon(q); |
| 746 | } while (p); |
| 747 | free(initial_cmds[n]); |
| 748 | initial_cmds[n] = NULL; |
| 749 | n++; |
| 750 | } |
| 751 | } |
Denis Vlasenko | 58875ae | 2007-03-22 22:22:10 +0000 | [diff] [blame] | 752 | #endif |
Paul Fox | 35e9c5d | 2008-03-06 16:26:12 +0000 | [diff] [blame] | 753 | redraw(FALSE); // dont force every col re-draw |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 754 | //------This is the main Vi cmd handling loop ----------------------- |
| 755 | while (editing > 0) { |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 756 | #if ENABLE_FEATURE_VI_CRASHME |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 757 | if (crashme > 0) { |
| 758 | if ((end - text) > 1) { |
| 759 | crash_dummy(); // generate a random command |
| 760 | } else { |
| 761 | crashme = 0; |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 762 | string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string |
| 763 | dot = text; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 764 | refresh(FALSE); |
| 765 | } |
| 766 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 767 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 768 | last_input_char = c = get_one_char(); // get a cmd from user |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 769 | #if ENABLE_FEATURE_VI_YANKMARK |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 770 | // save a copy of the current line- for the 'U" command |
| 771 | if (begin_line(dot) != cur_line) { |
| 772 | cur_line = begin_line(dot); |
| 773 | text_yank(begin_line(dot), end_line(dot), Ureg); |
| 774 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 775 | #endif |
| 776 | #if ENABLE_FEATURE_VI_DOT_CMD |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 777 | // These are commands that change text[]. |
| 778 | // Remember the input for the "." command |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 779 | if (!adding2q && ioq_start == NULL |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 780 | && cmd_mode == 0 // command mode |
| 781 | && c > '\0' // exclude NUL and non-ASCII chars |
| 782 | && c < 0x7f // (Unicode and such) |
| 783 | && strchr(modifying_cmds, c) |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 784 | ) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 785 | start_new_cmd_q(c); |
| 786 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 787 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 788 | do_cmd(c); // execute the user command |
Denis Vlasenko | 8ef801b | 2008-10-16 09:46:07 +0000 | [diff] [blame] | 789 | |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 790 | // poll to see if there is input already waiting. if we are |
| 791 | // not able to display output fast enough to keep up, skip |
| 792 | // the display update until we catch up with input. |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 793 | if (!readbuffer[0] && mysleep(0) == 0) { |
Denis Vlasenko | 8ef801b | 2008-10-16 09:46:07 +0000 | [diff] [blame] | 794 | // no input pending - so update output |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 795 | refresh(FALSE); |
| 796 | show_status_line(); |
| 797 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 798 | #if ENABLE_FEATURE_VI_CRASHME |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 799 | if (crashme > 0) |
| 800 | crash_test(); // test editor variables |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 801 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 802 | } |
| 803 | //------------------------------------------------------------------- |
| 804 | |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 805 | go_bottom_and_clear_to_eol(); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 806 | cookmode(); |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 807 | #undef cur_line |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 810 | //----- The Colon commands ------------------------------------- |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 811 | #if ENABLE_FEATURE_VI_COLON |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 812 | static char *get_one_address(char *p, int *addr) // get colon addr, if present |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 813 | { |
| 814 | int st; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 815 | char *q; |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 816 | IF_FEATURE_VI_YANKMARK(char c;) |
| 817 | IF_FEATURE_VI_SEARCH(char *pat;) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 818 | |
| 819 | *addr = -1; // assume no addr |
| 820 | if (*p == '.') { // the current line |
| 821 | p++; |
| 822 | q = begin_line(dot); |
| 823 | *addr = count_lines(text, q); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 824 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 825 | #if ENABLE_FEATURE_VI_YANKMARK |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 826 | else if (*p == '\'') { // is this a mark addr |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 827 | p++; |
| 828 | c = tolower(*p); |
| 829 | p++; |
| 830 | if (c >= 'a' && c <= 'z') { |
| 831 | // we have a mark |
| 832 | c = c - 'a'; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 833 | q = mark[(unsigned char) c]; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 834 | if (q != NULL) { // is mark valid |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 835 | *addr = count_lines(text, q); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 836 | } |
| 837 | } |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 838 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 839 | #endif |
| 840 | #if ENABLE_FEATURE_VI_SEARCH |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 841 | else if (*p == '/') { // a search pattern |
| 842 | q = strchrnul(++p, '/'); |
| 843 | pat = xstrndup(p, q - p); // save copy of pattern |
| 844 | p = q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 845 | if (*p == '/') |
| 846 | p++; |
| 847 | q = char_search(dot, pat, FORWARD, FULL); |
| 848 | if (q != NULL) { |
| 849 | *addr = count_lines(text, q); |
| 850 | } |
| 851 | free(pat); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 852 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 853 | #endif |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 854 | else if (*p == '$') { // the last line in file |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 855 | p++; |
| 856 | q = begin_line(end - 1); |
| 857 | *addr = count_lines(text, q); |
| 858 | } else if (isdigit(*p)) { // specific line number |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 859 | sscanf(p, "%d%n", addr, &st); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 860 | p += st; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 861 | } else { |
Denys Vlasenko | b22bbff | 2009-07-04 16:50:43 +0200 | [diff] [blame] | 862 | // unrecognized address - assume -1 |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 863 | *addr = -1; |
| 864 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 865 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 866 | } |
| 867 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 868 | static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 869 | { |
| 870 | //----- get the address' i.e., 1,3 'a,'b ----- |
| 871 | // get FIRST addr, if present |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 872 | while (isblank(*p)) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 873 | p++; // skip over leading spaces |
| 874 | if (*p == '%') { // alias for 1,$ |
| 875 | p++; |
| 876 | *b = 1; |
| 877 | *e = count_lines(text, end-1); |
| 878 | goto ga0; |
| 879 | } |
| 880 | p = get_one_address(p, b); |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 881 | while (isblank(*p)) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 882 | p++; |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 883 | if (*p == ',') { // is there a address separator |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 884 | p++; |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 885 | while (isblank(*p)) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 886 | p++; |
| 887 | // get SECOND addr, if present |
| 888 | p = get_one_address(p, e); |
| 889 | } |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 890 | ga0: |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 891 | while (isblank(*p)) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 892 | p++; // skip over trailing spaces |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 893 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 894 | } |
| 895 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 896 | #if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 897 | static void setops(const char *args, const char *opname, int flg_no, |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 898 | const char *short_opname, int opt) |
| 899 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 900 | const char *a = args + flg_no; |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 901 | int l = strlen(opname) - 1; /* opname have + ' ' */ |
| 902 | |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 903 | // maybe strncmp? we had tons of erroneous strncasecmp's... |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 904 | if (strncasecmp(a, opname, l) == 0 |
| 905 | || strncasecmp(a, short_opname, 2) == 0 |
| 906 | ) { |
| 907 | if (flg_no) |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 908 | vi_setops &= ~opt; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 909 | else |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 910 | vi_setops |= opt; |
| 911 | } |
| 912 | } |
| 913 | #endif |
| 914 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 915 | // buf must be no longer than MAX_INPUT_LEN! |
| 916 | static void colon(char *buf) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 917 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 918 | char c, *orig_buf, *buf1, *q, *r; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 919 | char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN]; |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 920 | int i, l, li, ch, b, e; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 921 | int useforce, forced = FALSE; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 922 | |
| 923 | // :3154 // if (-e line 3154) goto it else stay put |
| 924 | // :4,33w! foo // write a portion of buffer to file "foo" |
| 925 | // :w // write all of buffer to current file |
| 926 | // :q // quit |
| 927 | // :q! // quit- dont care about modified file |
| 928 | // :'a,'z!sort -u // filter block through sort |
| 929 | // :'f // goto mark "f" |
| 930 | // :'fl // list literal the mark "f" line |
| 931 | // :.r bar // read file "bar" into buffer before dot |
| 932 | // :/123/,/abc/d // delete lines from "123" line to "abc" line |
| 933 | // :/xyz/ // goto the "xyz" line |
| 934 | // :s/find/replace/ // substitute pattern "find" with "replace" |
| 935 | // :!<cmd> // run <cmd> then return |
| 936 | // |
Eric Andersen | 165e8cb | 2004-07-20 06:44:46 +0000 | [diff] [blame] | 937 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 938 | if (!buf[0]) |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 939 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 940 | if (*buf == ':') |
| 941 | buf++; // move past the ':' |
| 942 | |
Bernhard Reutner-Fischer | d591a36 | 2006-08-20 17:35:13 +0000 | [diff] [blame] | 943 | li = ch = i = 0; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 944 | b = e = -1; |
| 945 | q = text; // assume 1,$ for the range |
| 946 | r = end - 1; |
| 947 | li = count_lines(text, end - 1); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 948 | fn = current_filename; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 949 | |
| 950 | // look for optional address(es) :. :1 :1,9 :'q,'a :% |
| 951 | buf = get_address(buf, &b, &e); |
| 952 | |
| 953 | // remember orig command line |
| 954 | orig_buf = buf; |
| 955 | |
| 956 | // get the COMMAND into cmd[] |
| 957 | buf1 = cmd; |
| 958 | while (*buf != '\0') { |
| 959 | if (isspace(*buf)) |
| 960 | break; |
| 961 | *buf1++ = *buf++; |
| 962 | } |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 963 | *buf1 = '\0'; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 964 | // get any ARGuments |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 965 | while (isblank(*buf)) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 966 | buf++; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 967 | strcpy(args, buf); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 968 | useforce = FALSE; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 969 | buf1 = last_char_is(cmd, '!'); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 970 | if (buf1) { |
| 971 | useforce = TRUE; |
| 972 | *buf1 = '\0'; // get rid of ! |
| 973 | } |
| 974 | if (b >= 0) { |
| 975 | // if there is only one addr, then the addr |
| 976 | // is the line number of the single line the |
| 977 | // user wants. So, reset the end |
| 978 | // pointer to point at end of the "b" line |
| 979 | q = find_line(b); // what line is #b |
| 980 | r = end_line(q); |
| 981 | li = 1; |
| 982 | } |
| 983 | if (e >= 0) { |
| 984 | // we were given two addrs. change the |
| 985 | // end pointer to the addr given by user. |
| 986 | r = find_line(e); // what line is #e |
| 987 | r = end_line(r); |
| 988 | li = e - b + 1; |
| 989 | } |
| 990 | // ------------ now look for the command ------------ |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 991 | i = strlen(cmd); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 992 | if (i == 0) { // :123CR goto line #123 |
| 993 | if (b >= 0) { |
| 994 | dot = find_line(b); // what line is #b |
| 995 | dot_skip_over_ws(); |
| 996 | } |
Denis Vlasenko | 249fabf | 2006-12-19 00:29:22 +0000 | [diff] [blame] | 997 | } |
| 998 | #if ENABLE_FEATURE_ALLOW_EXEC |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 999 | else if (cmd[0] == '!') { // run a cmd |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1000 | int retcode; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1001 | // :!ls run the <cmd> |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 1002 | go_bottom_and_clear_to_eol(); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1003 | cookmode(); |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1004 | retcode = system(orig_buf + 1); // run the cmd |
| 1005 | if (retcode) |
| 1006 | printf("\nshell returned %i\n\n", retcode); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1007 | rawmode(); |
| 1008 | Hit_Return(); // let user see results |
Denis Vlasenko | 249fabf | 2006-12-19 00:29:22 +0000 | [diff] [blame] | 1009 | } |
| 1010 | #endif |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1011 | else if (cmd[0] == '=' && !cmd[1]) { // where is the address |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1012 | if (b < 0) { // no addr given- use defaults |
| 1013 | b = e = count_lines(text, dot); |
| 1014 | } |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1015 | status_line("%d", b); |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1016 | } else if (strncmp(cmd, "delete", i) == 0) { // delete lines |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1017 | if (b < 0) { // no addr given- use defaults |
| 1018 | q = begin_line(dot); // assume .,. for the range |
| 1019 | r = end_line(dot); |
| 1020 | } |
| 1021 | dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines |
| 1022 | dot_skip_over_ws(); |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1023 | } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1024 | // don't edit, if the current file has been modified |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1025 | if (file_modified && !useforce) { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1026 | status_line_bold("No write since last change (:%s! overrides)", cmd); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1027 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1028 | } |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1029 | if (args[0]) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1030 | // the user supplied a file name |
Denis Vlasenko | e8a0788 | 2007-06-10 15:08:44 +0000 | [diff] [blame] | 1031 | fn = args; |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1032 | } else if (current_filename && current_filename[0]) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1033 | // no user supplied name- use the current filename |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1034 | // fn = current_filename; was set by default |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1035 | } else { |
| 1036 | // no user file name, no current name- punt |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1037 | status_line_bold("No current filename"); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1038 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1039 | } |
| 1040 | |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1041 | if (init_text_buffer(fn) < 0) |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1042 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1043 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1044 | #if ENABLE_FEATURE_VI_YANKMARK |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1045 | if (Ureg >= 0 && Ureg < 28) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1046 | free(reg[Ureg]); // free orig line reg- for 'U' |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1047 | reg[Ureg] = NULL; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1048 | } |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1049 | if (YDreg >= 0 && YDreg < 28) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1050 | free(reg[YDreg]); // free default yank/delete register |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1051 | reg[YDreg] = NULL; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1052 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1053 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1054 | // how many lines in text[]? |
| 1055 | li = count_lines(text, end - 1); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1056 | status_line("\"%s\"%s" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1057 | IF_FEATURE_VI_READONLY("%s") |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1058 | " %dL, %dC", current_filename, |
| 1059 | (file_size(fn) < 0 ? " [New file]" : ""), |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1060 | IF_FEATURE_VI_READONLY( |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1061 | ((readonly_mode) ? " [Readonly]" : ""), |
| 1062 | ) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1063 | li, ch); |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1064 | } else if (strncmp(cmd, "file", i) == 0) { // what File is this |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1065 | if (b != -1 || e != -1) { |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 1066 | status_line_bold("No address allowed on this command"); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1067 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1068 | } |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1069 | if (args[0]) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1070 | // user wants a new filename |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1071 | free(current_filename); |
| 1072 | current_filename = xstrdup(args); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1073 | } else { |
| 1074 | // user wants file status info |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 1075 | last_status_cksum = 0; // force status update |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1076 | } |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1077 | } else if (strncmp(cmd, "features", i) == 0) { // what features are available |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1078 | // print out values of all features |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 1079 | go_bottom_and_clear_to_eol(); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1080 | cookmode(); |
| 1081 | show_help(); |
| 1082 | rawmode(); |
| 1083 | Hit_Return(); |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1084 | } else if (strncmp(cmd, "list", i) == 0) { // literal print line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1085 | if (b < 0) { // no addr given- use defaults |
| 1086 | q = begin_line(dot); // assume .,. for the range |
| 1087 | r = end_line(dot); |
| 1088 | } |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 1089 | go_bottom_and_clear_to_eol(); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 1090 | puts("\r"); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1091 | for (; q <= r; q++) { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 1092 | int c_is_no_print; |
| 1093 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1094 | c = *q; |
Denis Vlasenko | 2a51af2 | 2007-03-21 22:31:24 +0000 | [diff] [blame] | 1095 | c_is_no_print = (c & 0x80) && !Isprint(c); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 1096 | if (c_is_no_print) { |
| 1097 | c = '.'; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1098 | standout_start(); |
Denis Vlasenko | d3c042f | 2007-12-30 01:59:53 +0000 | [diff] [blame] | 1099 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1100 | if (c == '\n') { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 1101 | write1("$\r"); |
| 1102 | } else if (c < ' ' || c == 127) { |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1103 | bb_putchar('^'); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1104 | if (c == 127) |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 1105 | c = '?'; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1106 | else |
| 1107 | c += '@'; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1108 | } |
Denis Vlasenko | 4daad90 | 2007-09-27 10:20:47 +0000 | [diff] [blame] | 1109 | bb_putchar(c); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 1110 | if (c_is_no_print) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1111 | standout_end(); |
| 1112 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1113 | Hit_Return(); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1114 | } else if (strncmp(cmd, "quit", i) == 0 // quit |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1115 | || strncmp(cmd, "next", i) == 0 // edit next file |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1116 | || strncmp(cmd, "prev", i) == 0 // edit previous file |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1117 | ) { |
Denys Vlasenko | 04cecd5 | 2010-04-16 22:13:55 -0700 | [diff] [blame] | 1118 | int n; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1119 | if (useforce) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1120 | if (*cmd == 'q') { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1121 | // force end of argv list |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1122 | optind = save_argc; |
| 1123 | } |
| 1124 | editing = 0; |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1125 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1126 | } |
| 1127 | // don't exit if the file been modified |
| 1128 | if (file_modified) { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1129 | status_line_bold("No write since last change (:%s! overrides)", cmd); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1130 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1131 | } |
| 1132 | // are there other file to edit |
Denys Vlasenko | 04cecd5 | 2010-04-16 22:13:55 -0700 | [diff] [blame] | 1133 | n = save_argc - optind - 1; |
| 1134 | if (*cmd == 'q' && n > 0) { |
| 1135 | status_line_bold("%d more file(s) to edit", n); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1136 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1137 | } |
Denys Vlasenko | 04cecd5 | 2010-04-16 22:13:55 -0700 | [diff] [blame] | 1138 | if (*cmd == 'n' && n <= 0) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1139 | status_line_bold("No more files to edit"); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1140 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1141 | } |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1142 | if (*cmd == 'p') { |
| 1143 | // are there previous files to edit |
| 1144 | if (optind < 1) { |
| 1145 | status_line_bold("No previous files to edit"); |
| 1146 | goto ret; |
| 1147 | } |
| 1148 | optind -= 2; |
| 1149 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1150 | editing = 0; |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1151 | } else if (strncmp(cmd, "read", i) == 0) { // read file into text[] |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1152 | fn = args; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1153 | if (!fn[0]) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1154 | status_line_bold("No filename given"); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1155 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1156 | } |
| 1157 | if (b < 0) { // no addr given- use defaults |
| 1158 | q = begin_line(dot); // assume "dot" |
| 1159 | } |
| 1160 | // read after current line- unless user said ":0r foo" |
| 1161 | if (b != 0) |
| 1162 | q = next_line(q); |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1163 | { // dance around potentially-reallocated text[] |
| 1164 | uintptr_t ofs = q - text; |
| 1165 | ch = file_insert(fn, q, 0); |
| 1166 | q = text + ofs; |
| 1167 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1168 | if (ch < 0) |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1169 | goto ret; // nothing was inserted |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1170 | // how many lines in text[]? |
| 1171 | li = count_lines(q, q + ch - 1); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1172 | status_line("\"%s\"" |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1173 | IF_FEATURE_VI_READONLY("%s") |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1174 | " %dL, %dC", fn, |
Denis Vlasenko | 5e34ff2 | 2009-04-21 11:09:40 +0000 | [diff] [blame] | 1175 | IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1176 | li, ch); |
| 1177 | if (ch > 0) { |
| 1178 | // if the insert is before "dot" then we need to update |
| 1179 | if (q <= dot) |
| 1180 | dot += ch; |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1181 | /*file_modified++; - done by file_insert */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1182 | } |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1183 | } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1184 | if (file_modified && !useforce) { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1185 | status_line_bold("No write since last change (:%s! overrides)", cmd); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1186 | } else { |
| 1187 | // reset the filenames to edit |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1188 | optind = -1; /* start from 0th file */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1189 | editing = 0; |
| 1190 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1191 | #if ENABLE_FEATURE_VI_SET |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1192 | } else if (strncmp(cmd, "set", i) == 0) { // set or clear features |
Denis Vlasenko | 58875ae | 2007-03-22 22:22:10 +0000 | [diff] [blame] | 1193 | #if ENABLE_FEATURE_VI_SETOPTS |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 1194 | char *argp; |
Denis Vlasenko | 58875ae | 2007-03-22 22:22:10 +0000 | [diff] [blame] | 1195 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1196 | i = 0; // offset into args |
Denys Vlasenko | 5f556fc | 2012-06-11 01:53:33 +0200 | [diff] [blame^] | 1197 | // only blank is regarded as args delimiter. What about tab '\t'? |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 1198 | if (!args[0] || strcasecmp(args, "all") == 0) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1199 | // print out values of all options |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1200 | #if ENABLE_FEATURE_VI_SETOPTS |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1201 | status_line_bold( |
| 1202 | "%sautoindent " |
| 1203 | "%sflash " |
| 1204 | "%signorecase " |
| 1205 | "%sshowmatch " |
| 1206 | "tabstop=%u", |
| 1207 | autoindent ? "" : "no", |
| 1208 | err_method ? "" : "no", |
| 1209 | ignorecase ? "" : "no", |
| 1210 | showmatch ? "" : "no", |
| 1211 | tabstop |
| 1212 | ); |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1213 | #endif |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1214 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1215 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1216 | #if ENABLE_FEATURE_VI_SETOPTS |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1217 | argp = args; |
Denis Vlasenko | ba2fb71 | 2007-04-01 09:39:03 +0000 | [diff] [blame] | 1218 | while (*argp) { |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1219 | if (strncmp(argp, "no", 2) == 0) |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 1220 | i = 2; // ":set noautoindent" |
| 1221 | setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1222 | setops(argp, "flash " , i, "fl", VI_ERR_METHOD); |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 1223 | setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1224 | setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH ); |
| 1225 | if (strncmp(argp + i, "tabstop=", 8) == 0) { |
| 1226 | int t = 0; |
| 1227 | sscanf(argp + i+8, "%u", &t); |
| 1228 | if (t > 0 && t <= MAX_TABSTOP) |
| 1229 | tabstop = t; |
Denis Vlasenko | f923413 | 2007-03-21 00:03:42 +0000 | [diff] [blame] | 1230 | } |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1231 | argp = skip_non_whitespace(argp); |
| 1232 | argp = skip_whitespace(argp); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1233 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1234 | #endif /* FEATURE_VI_SETOPTS */ |
| 1235 | #endif /* FEATURE_VI_SET */ |
| 1236 | #if ENABLE_FEATURE_VI_SEARCH |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1237 | } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1238 | char *F, *R, *flags; |
| 1239 | size_t len_F, len_R; |
| 1240 | int gflag; // global replace flag |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1241 | |
| 1242 | // F points to the "find" pattern |
| 1243 | // R points to the "replace" pattern |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1244 | // replace the cmd line delimiters "/" with NULs |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1245 | c = orig_buf[1]; // what is the delimiter |
| 1246 | F = orig_buf + 2; // start of "find" |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1247 | R = strchr(F, c); // middle delimiter |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1248 | if (!R) |
| 1249 | goto colon_s_fail; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1250 | len_F = R - F; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1251 | *R++ = '\0'; // terminate "find" |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1252 | flags = strchr(R, c); |
| 1253 | if (!flags) |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1254 | goto colon_s_fail; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1255 | len_R = flags - R; |
| 1256 | *flags++ = '\0'; // terminate "replace" |
| 1257 | gflag = *flags; |
| 1258 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1259 | q = begin_line(q); |
| 1260 | if (b < 0) { // maybe :s/foo/bar/ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1261 | q = begin_line(dot); // start with cur line |
| 1262 | b = count_lines(text, q); // cur line number |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1263 | } |
| 1264 | if (e < 0) |
| 1265 | e = b; // maybe :.s/foo/bar/ |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1266 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1267 | for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0 |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1268 | char *ls = q; // orig line start |
| 1269 | char *found; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1270 | vc4: |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1271 | found = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find" |
| 1272 | if (found) { |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1273 | uintptr_t bias; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1274 | // we found the "find" pattern - delete it |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1275 | text_hole_delete(found, found + len_F - 1); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1276 | // inset the "replace" patern |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1277 | bias = string_insert(found, R); // insert the string |
| 1278 | found += bias; |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1279 | ls += bias; |
| 1280 | /*q += bias; - recalculated anyway */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1281 | // check for "global" :s/foo/bar/g |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 1282 | if (gflag == 'g') { |
| 1283 | if ((found + len_R) < end_line(ls)) { |
| 1284 | q = found + len_R; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1285 | goto vc4; // don't let q move past cur line |
| 1286 | } |
| 1287 | } |
| 1288 | } |
| 1289 | q = next_line(ls); |
| 1290 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1291 | #endif /* FEATURE_VI_SEARCH */ |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1292 | } else if (strncmp(cmd, "version", i) == 0) { // show software version |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 1293 | status_line(BB_VER " " BB_BT); |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1294 | } else if (strncmp(cmd, "write", i) == 0 // write text to file |
| 1295 | || strncmp(cmd, "wq", i) == 0 |
| 1296 | || strncmp(cmd, "wn", i) == 0 |
| 1297 | || (cmd[0] == 'x' && !cmd[1]) |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1298 | ) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1299 | // is there a file name to write to? |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1300 | if (args[0]) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1301 | fn = args; |
| 1302 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1303 | #if ENABLE_FEATURE_VI_READONLY |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1304 | if (readonly_mode && !useforce) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1305 | status_line_bold("\"%s\" File is read only", fn); |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1306 | goto ret; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1307 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1308 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1309 | // how many lines in text[]? |
| 1310 | li = count_lines(q, r); |
| 1311 | ch = r - q + 1; |
| 1312 | // see if file exists- if not, its just a new file request |
| 1313 | if (useforce) { |
| 1314 | // if "fn" is not write-able, chmod u+w |
| 1315 | // sprintf(syscmd, "chmod u+w %s", fn); |
| 1316 | // system(syscmd); |
| 1317 | forced = TRUE; |
| 1318 | } |
| 1319 | l = file_write(fn, q, r); |
| 1320 | if (useforce && forced) { |
| 1321 | // chmod u-w |
| 1322 | // sprintf(syscmd, "chmod u-w %s", fn); |
| 1323 | // system(syscmd); |
| 1324 | forced = FALSE; |
| 1325 | } |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 1326 | if (l < 0) { |
| 1327 | if (l == -1) |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1328 | status_line_bold("\"%s\" %s", fn, strerror(errno)); |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 1329 | } else { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1330 | status_line("\"%s\" %dL, %dC", fn, li, l); |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 1331 | if (q == text && r == end - 1 && l == ch) { |
| 1332 | file_modified = 0; |
| 1333 | last_file_modified = -1; |
| 1334 | } |
Denys Vlasenko | 6b9f163 | 2010-01-28 02:24:24 +0100 | [diff] [blame] | 1335 | if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' |
| 1336 | || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N' |
| 1337 | ) |
| 1338 | && l == ch |
| 1339 | ) { |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 1340 | editing = 0; |
| 1341 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1342 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1343 | #if ENABLE_FEATURE_VI_YANKMARK |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 1344 | } else if (strncmp(cmd, "yank", i) == 0) { // yank lines |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1345 | if (b < 0) { // no addr given- use defaults |
| 1346 | q = begin_line(dot); // assume .,. for the range |
| 1347 | r = end_line(dot); |
| 1348 | } |
| 1349 | text_yank(q, r, YDreg); |
| 1350 | li = count_lines(q, r); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1351 | status_line("Yank %d lines (%d chars) into [%c]", |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1352 | li, strlen(reg[YDreg]), what_reg()); |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1353 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1354 | } else { |
| 1355 | // cmd unknown |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1356 | not_implemented(cmd); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1357 | } |
Denys Vlasenko | 9f82d0b | 2010-05-19 01:54:37 +0200 | [diff] [blame] | 1358 | ret: |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1359 | dot = bound_dot(dot); // make sure "dot" is valid |
| 1360 | return; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1361 | #if ENABLE_FEATURE_VI_SEARCH |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1362 | colon_s_fail: |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1363 | status_line(":s expression missing delimiters"); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1364 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1365 | } |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 1366 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1367 | #endif /* FEATURE_VI_COLON */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1368 | |
| 1369 | static void Hit_Return(void) |
| 1370 | { |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 1371 | int c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1372 | |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1373 | standout_start(); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 1374 | write1("[Hit return to continue]"); |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1375 | standout_end(); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1376 | while ((c = get_one_char()) != '\n' && c != '\r') |
| 1377 | continue; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1378 | redraw(TRUE); // force redraw all |
| 1379 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1380 | |
Denis Vlasenko | 91afdf8 | 2007-07-17 23:22:49 +0000 | [diff] [blame] | 1381 | static int next_tabstop(int col) |
| 1382 | { |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1383 | return col + ((tabstop - 1) - (col % tabstop)); |
| 1384 | } |
| 1385 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1386 | //----- Synchronize the cursor to Dot -------------------------- |
Denys Vlasenko | adf922e | 2009-10-08 14:35:37 +0200 | [diff] [blame] | 1387 | static NOINLINE void sync_cursor(char *d, int *row, int *col) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1388 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1389 | char *beg_cur; // begin and end of "d" line |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1390 | char *tp; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1391 | int cnt, ro, co; |
| 1392 | |
| 1393 | beg_cur = begin_line(d); // first char of cur line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1394 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1395 | if (beg_cur < screenbegin) { |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1396 | // "d" is before top line on screen |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1397 | // how many lines do we have to move |
| 1398 | cnt = count_lines(beg_cur, screenbegin); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1399 | sc1: |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1400 | screenbegin = beg_cur; |
| 1401 | if (cnt > (rows - 1) / 2) { |
| 1402 | // we moved too many lines. put "dot" in middle of screen |
| 1403 | for (cnt = 0; cnt < (rows - 1) / 2; cnt++) { |
| 1404 | screenbegin = prev_line(screenbegin); |
| 1405 | } |
| 1406 | } |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1407 | } else { |
| 1408 | char *end_scr; // begin and end of screen |
| 1409 | end_scr = end_screen(); // last char of screen |
| 1410 | if (beg_cur > end_scr) { |
| 1411 | // "d" is after bottom line on screen |
| 1412 | // how many lines do we have to move |
| 1413 | cnt = count_lines(end_scr, beg_cur); |
| 1414 | if (cnt > (rows - 1) / 2) |
| 1415 | goto sc1; // too many lines |
| 1416 | for (ro = 0; ro < cnt - 1; ro++) { |
| 1417 | // move screen begin the same amount |
| 1418 | screenbegin = next_line(screenbegin); |
| 1419 | // now, move the end of screen |
| 1420 | end_scr = next_line(end_scr); |
| 1421 | end_scr = end_line(end_scr); |
| 1422 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1423 | } |
| 1424 | } |
| 1425 | // "d" is on screen- find out which row |
| 1426 | tp = screenbegin; |
| 1427 | for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row |
| 1428 | if (tp == beg_cur) |
| 1429 | break; |
| 1430 | tp = next_line(tp); |
| 1431 | } |
| 1432 | |
| 1433 | // find out what col "d" is on |
| 1434 | co = 0; |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 1435 | while (tp < d) { // drive "co" to correct column |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1436 | if (*tp == '\n') //vda || *tp == '\0') |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1437 | break; |
| 1438 | if (*tp == '\t') { |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 1439 | // handle tabs like real vi |
| 1440 | if (d == tp && cmd_mode) { |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1441 | break; |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1442 | } |
Denis Vlasenko | e3eae0d | 2008-06-22 16:38:53 +0000 | [diff] [blame] | 1443 | co = next_tabstop(co); |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 1444 | } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) { |
| 1445 | co++; // display as ^X, use 2 columns |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1446 | } |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 1447 | co++; |
| 1448 | tp++; |
| 1449 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1450 | |
| 1451 | // "co" is the column where "dot" is. |
| 1452 | // The screen has "columns" columns. |
| 1453 | // The currently displayed columns are 0+offset -- columns+ofset |
| 1454 | // |-------------------------------------------------------------| |
| 1455 | // ^ ^ ^ |
| 1456 | // offset | |------- columns ----------------| |
| 1457 | // |
| 1458 | // If "co" is already in this range then we do not have to adjust offset |
| 1459 | // but, we do have to subtract the "offset" bias from "co". |
| 1460 | // If "co" is outside this range then we have to change "offset". |
| 1461 | // If the first char of a line is a tab the cursor will try to stay |
| 1462 | // in column 7, but we have to set offset to 0. |
| 1463 | |
| 1464 | if (co < 0 + offset) { |
| 1465 | offset = co; |
| 1466 | } |
| 1467 | if (co >= columns + offset) { |
| 1468 | offset = co - columns + 1; |
| 1469 | } |
| 1470 | // if the first char of the line is a tab, and "dot" is sitting on it |
| 1471 | // force offset to 0. |
| 1472 | if (d == beg_cur && *d == '\t') { |
| 1473 | offset = 0; |
| 1474 | } |
| 1475 | co -= offset; |
| 1476 | |
| 1477 | *row = ro; |
| 1478 | *col = co; |
| 1479 | } |
| 1480 | |
| 1481 | //----- Text Movement Routines --------------------------------- |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1482 | static char *begin_line(char *p) // return pointer to first char cur line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1483 | { |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1484 | if (p > text) { |
| 1485 | p = memrchr(text, '\n', p - text); |
| 1486 | if (!p) |
| 1487 | return text; |
| 1488 | return p + 1; |
| 1489 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1490 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1491 | } |
| 1492 | |
Denis Vlasenko | e3eae0d | 2008-06-22 16:38:53 +0000 | [diff] [blame] | 1493 | static char *end_line(char *p) // return pointer to NL of cur line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1494 | { |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1495 | if (p < end - 1) { |
| 1496 | p = memchr(p, '\n', end - p - 1); |
| 1497 | if (!p) |
| 1498 | return end - 1; |
| 1499 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1500 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1501 | } |
| 1502 | |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1503 | static char *dollar_line(char *p) // return pointer to just before NL line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1504 | { |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1505 | p = end_line(p); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1506 | // Try to stay off of the Newline |
| 1507 | if (*p == '\n' && (p - begin_line(p)) > 0) |
| 1508 | p--; |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1509 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1510 | } |
| 1511 | |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1512 | static char *prev_line(char *p) // return pointer first char prev line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1513 | { |
| 1514 | p = begin_line(p); // goto begining of cur line |
Denis Vlasenko | e3eae0d | 2008-06-22 16:38:53 +0000 | [diff] [blame] | 1515 | if (p > text && p[-1] == '\n') |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1516 | p--; // step to prev line |
| 1517 | p = begin_line(p); // goto begining of prev line |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1518 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1519 | } |
| 1520 | |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1521 | static char *next_line(char *p) // return pointer first char next line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1522 | { |
| 1523 | p = end_line(p); |
Denis Vlasenko | e3eae0d | 2008-06-22 16:38:53 +0000 | [diff] [blame] | 1524 | if (p < end - 1 && *p == '\n') |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1525 | p++; // step to next line |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1526 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1527 | } |
| 1528 | |
| 1529 | //----- Text Information Routines ------------------------------ |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1530 | static char *end_screen(void) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1531 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1532 | char *q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1533 | int cnt; |
| 1534 | |
| 1535 | // find new bottom line |
| 1536 | q = screenbegin; |
| 1537 | for (cnt = 0; cnt < rows - 2; cnt++) |
| 1538 | q = next_line(q); |
| 1539 | q = end_line(q); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1540 | return q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1541 | } |
| 1542 | |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1543 | // count line from start to stop |
| 1544 | static int count_lines(char *start, char *stop) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1545 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1546 | char *q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1547 | int cnt; |
| 1548 | |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1549 | if (stop < start) { // start and stop are backwards- reverse them |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1550 | q = start; |
| 1551 | start = stop; |
| 1552 | stop = q; |
| 1553 | } |
| 1554 | cnt = 0; |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1555 | stop = end_line(stop); |
| 1556 | while (start <= stop && start <= end - 1) { |
| 1557 | start = end_line(start); |
| 1558 | if (*start == '\n') |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1559 | cnt++; |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1560 | start++; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1561 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1562 | return cnt; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1563 | } |
| 1564 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1565 | static char *find_line(int li) // find begining of line #li |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1566 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1567 | char *q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1568 | |
| 1569 | for (q = text; li > 1; li--) { |
| 1570 | q = next_line(q); |
| 1571 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1572 | return q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1573 | } |
| 1574 | |
| 1575 | //----- Dot Movement Routines ---------------------------------- |
| 1576 | static void dot_left(void) |
| 1577 | { |
| 1578 | if (dot > text && dot[-1] != '\n') |
| 1579 | dot--; |
| 1580 | } |
| 1581 | |
| 1582 | static void dot_right(void) |
| 1583 | { |
| 1584 | if (dot < end - 1 && *dot != '\n') |
| 1585 | dot++; |
| 1586 | } |
| 1587 | |
| 1588 | static void dot_begin(void) |
| 1589 | { |
| 1590 | dot = begin_line(dot); // return pointer to first char cur line |
| 1591 | } |
| 1592 | |
| 1593 | static void dot_end(void) |
| 1594 | { |
| 1595 | dot = end_line(dot); // return pointer to last char cur line |
| 1596 | } |
| 1597 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1598 | static char *move_to_col(char *p, int l) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1599 | { |
| 1600 | int co; |
| 1601 | |
| 1602 | p = begin_line(p); |
| 1603 | co = 0; |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 1604 | while (co < l && p < end) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1605 | if (*p == '\n') //vda || *p == '\0') |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1606 | break; |
| 1607 | if (*p == '\t') { |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 1608 | co = next_tabstop(co); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 1609 | } else if (*p < ' ' || *p == 127) { |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 1610 | co++; // display as ^X, use 2 columns |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1611 | } |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 1612 | co++; |
| 1613 | p++; |
| 1614 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1615 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1616 | } |
| 1617 | |
| 1618 | static void dot_next(void) |
| 1619 | { |
| 1620 | dot = next_line(dot); |
| 1621 | } |
| 1622 | |
| 1623 | static void dot_prev(void) |
| 1624 | { |
| 1625 | dot = prev_line(dot); |
| 1626 | } |
| 1627 | |
| 1628 | static void dot_scroll(int cnt, int dir) |
| 1629 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1630 | char *q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1631 | |
| 1632 | for (; cnt > 0; cnt--) { |
| 1633 | if (dir < 0) { |
| 1634 | // scroll Backwards |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1635 | // ctrl-Y scroll up one line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1636 | screenbegin = prev_line(screenbegin); |
| 1637 | } else { |
| 1638 | // scroll Forwards |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1639 | // ctrl-E scroll down one line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1640 | screenbegin = next_line(screenbegin); |
| 1641 | } |
| 1642 | } |
| 1643 | // make sure "dot" stays on the screen so we dont scroll off |
| 1644 | if (dot < screenbegin) |
| 1645 | dot = screenbegin; |
| 1646 | q = end_screen(); // find new bottom line |
| 1647 | if (dot > q) |
| 1648 | dot = begin_line(q); // is dot is below bottom line? |
| 1649 | dot_skip_over_ws(); |
| 1650 | } |
| 1651 | |
| 1652 | static void dot_skip_over_ws(void) |
| 1653 | { |
| 1654 | // skip WS |
| 1655 | while (isspace(*dot) && *dot != '\n' && dot < end - 1) |
| 1656 | dot++; |
| 1657 | } |
| 1658 | |
| 1659 | static void dot_delete(void) // delete the char at 'dot' |
| 1660 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1661 | text_hole_delete(dot, dot); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1662 | } |
| 1663 | |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 1664 | static char *bound_dot(char *p) // make sure text[0] <= P < "end" |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1665 | { |
| 1666 | if (p >= end && end > text) { |
| 1667 | p = end - 1; |
| 1668 | indicate_error('1'); |
| 1669 | } |
| 1670 | if (p < text) { |
| 1671 | p = text; |
| 1672 | indicate_error('2'); |
| 1673 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1674 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
| 1677 | //----- Helper Utility Routines -------------------------------- |
| 1678 | |
| 1679 | //---------------------------------------------------------------- |
| 1680 | //----- Char Routines -------------------------------------------- |
| 1681 | /* Chars that are part of a word- |
| 1682 | * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz |
| 1683 | * Chars that are Not part of a word (stoppers) |
| 1684 | * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~ |
| 1685 | * Chars that are WhiteSpace |
| 1686 | * TAB NEWLINE VT FF RETURN SPACE |
| 1687 | * DO NOT COUNT NEWLINE AS WHITESPACE |
| 1688 | */ |
| 1689 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1690 | static char *new_screen(int ro, int co) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1691 | { |
| 1692 | int li; |
| 1693 | |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 1694 | free(screen); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1695 | screensize = ro * co + 8; |
Denis Vlasenko | b95636c | 2006-12-19 23:36:04 +0000 | [diff] [blame] | 1696 | screen = xmalloc(screensize); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1697 | // initialize the new screen. assume this will be a empty file. |
| 1698 | screen_erase(); |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 1699 | // non-existent text[] lines start with a tilde (~). |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1700 | for (li = 1; li < ro - 1; li++) { |
| 1701 | screen[(li * co) + 0] = '~'; |
| 1702 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1703 | return screen; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1706 | #if ENABLE_FEATURE_VI_SEARCH |
Walter Harms | b9ba580 | 2011-06-27 02:59:37 +0200 | [diff] [blame] | 1707 | |
| 1708 | # if ENABLE_FEATURE_VI_REGEX_SEARCH |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1709 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1710 | // search for pattern starting at p |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 1711 | static char *char_search(char *p, const char *pat, int dir, int range) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1712 | { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1713 | char *q; |
| 1714 | struct re_pattern_buffer preg; |
| 1715 | int i; |
Walter Harms | b9ba580 | 2011-06-27 02:59:37 +0200 | [diff] [blame] | 1716 | int size; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1717 | |
| 1718 | re_syntax_options = RE_SYNTAX_POSIX_EXTENDED; |
| 1719 | preg.translate = 0; |
| 1720 | preg.fastmap = 0; |
| 1721 | preg.buffer = 0; |
| 1722 | preg.allocated = 0; |
| 1723 | |
| 1724 | // assume a LIMITED forward search |
| 1725 | q = next_line(p); |
| 1726 | q = end_line(q); |
| 1727 | q = end - 1; |
| 1728 | if (dir == BACK) { |
| 1729 | q = prev_line(p); |
| 1730 | q = text; |
| 1731 | } |
| 1732 | // count the number of chars to search over, forward or backward |
| 1733 | size = q - p; |
| 1734 | if (size < 0) |
| 1735 | size = p - q; |
| 1736 | // RANGE could be negative if we are searching backwards |
| 1737 | range = q - p; |
| 1738 | |
Walter Harms | b9ba580 | 2011-06-27 02:59:37 +0200 | [diff] [blame] | 1739 | q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1740 | if (q != 0) { |
| 1741 | // The pattern was not compiled |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 1742 | status_line_bold("bad search pattern: \"%s\": %s", pat, q); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1743 | i = 0; // return p if pattern not compiled |
| 1744 | goto cs1; |
| 1745 | } |
| 1746 | |
| 1747 | q = p; |
| 1748 | if (range < 0) { |
| 1749 | q = p - size; |
| 1750 | if (q < text) |
| 1751 | q = text; |
| 1752 | } |
| 1753 | // search for the compiled pattern, preg, in p[] |
| 1754 | // range < 0- search backward |
| 1755 | // range > 0- search forward |
| 1756 | // 0 < start < size |
| 1757 | // re_search() < 0 not found or error |
| 1758 | // re_search() > 0 index of found pattern |
| 1759 | // struct pattern char int int int struct reg |
| 1760 | // re_search (*pattern_buffer, *string, size, start, range, *regs) |
| 1761 | i = re_search(&preg, q, size, 0, range, 0); |
| 1762 | if (i == -1) { |
| 1763 | p = 0; |
| 1764 | i = 0; // return NULL if pattern not found |
| 1765 | } |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1766 | cs1: |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1767 | if (dir == FORWARD) { |
| 1768 | p = p + i; |
| 1769 | } else { |
| 1770 | p = p - i; |
| 1771 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1772 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1773 | } |
Walter Harms | b9ba580 | 2011-06-27 02:59:37 +0200 | [diff] [blame] | 1774 | |
| 1775 | # else |
| 1776 | |
| 1777 | # if ENABLE_FEATURE_VI_SETOPTS |
| 1778 | static int mycmp(const char *s1, const char *s2, int len) |
| 1779 | { |
| 1780 | if (ignorecase) { |
| 1781 | return strncasecmp(s1, s2, len); |
| 1782 | } |
| 1783 | return strncmp(s1, s2, len); |
| 1784 | } |
| 1785 | # else |
| 1786 | # define mycmp strncmp |
| 1787 | # endif |
| 1788 | |
| 1789 | static char *char_search(char *p, const char *pat, int dir, int range) |
| 1790 | { |
| 1791 | char *start, *stop; |
| 1792 | int len; |
| 1793 | |
| 1794 | len = strlen(pat); |
| 1795 | if (dir == FORWARD) { |
| 1796 | stop = end - 1; // assume range is p - end-1 |
| 1797 | if (range == LIMITED) |
| 1798 | stop = next_line(p); // range is to next line |
| 1799 | for (start = p; start < stop; start++) { |
| 1800 | if (mycmp(start, pat, len) == 0) { |
| 1801 | return start; |
| 1802 | } |
| 1803 | } |
| 1804 | } else if (dir == BACK) { |
| 1805 | stop = text; // assume range is text - p |
| 1806 | if (range == LIMITED) |
| 1807 | stop = prev_line(p); // range is to prev line |
| 1808 | for (start = p - len; start >= stop; start--) { |
| 1809 | if (mycmp(start, pat, len) == 0) { |
| 1810 | return start; |
| 1811 | } |
| 1812 | } |
| 1813 | } |
| 1814 | // pattern not found |
| 1815 | return NULL; |
| 1816 | } |
| 1817 | |
| 1818 | # endif |
| 1819 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1820 | #endif /* FEATURE_VI_SEARCH */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1821 | |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 1822 | static char *char_insert(char *p, char c) // insert the char c at 'p' |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1823 | { |
| 1824 | if (c == 22) { // Is this an ctrl-V? |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1825 | p += stupid_insert(p, '^'); // use ^ to indicate literal next |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1826 | refresh(FALSE); // show the ^ |
| 1827 | c = get_one_char(); |
| 1828 | *p = c; |
| 1829 | p++; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 1830 | file_modified++; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1831 | } else if (c == 27) { // Is this an ESC? |
| 1832 | cmd_mode = 0; |
| 1833 | cmdcnt = 0; |
| 1834 | end_cmd_q(); // stop adding to q |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 1835 | last_status_cksum = 0; // force status update |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 1836 | if ((p[-1] != '\n') && (dot > text)) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1837 | p--; |
| 1838 | } |
Paul Fox | d13b90b | 2005-07-18 22:17:25 +0000 | [diff] [blame] | 1839 | } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1840 | // 123456789 |
Denis Vlasenko | defc1ea | 2008-06-27 02:52:20 +0000 | [diff] [blame] | 1841 | if ((p[-1] != '\n') && (dot>text)) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1842 | p--; |
| 1843 | p = text_hole_delete(p, p); // shrink buffer 1 char |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1844 | } |
| 1845 | } else { |
Cristian Ionescu-Idbohrn | 9a296fb | 2011-05-16 03:53:43 +0200 | [diff] [blame] | 1846 | #if ENABLE_FEATURE_VI_SETOPTS |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1847 | // insert a char into text[] |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1848 | char *sp; // "save p" |
Cristian Ionescu-Idbohrn | 9a296fb | 2011-05-16 03:53:43 +0200 | [diff] [blame] | 1849 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1850 | |
| 1851 | if (c == 13) |
| 1852 | c = '\n'; // translate \r to \n |
Cristian Ionescu-Idbohrn | 9a296fb | 2011-05-16 03:53:43 +0200 | [diff] [blame] | 1853 | #if ENABLE_FEATURE_VI_SETOPTS |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1854 | sp = p; // remember addr of insert |
Cristian Ionescu-Idbohrn | 9a296fb | 2011-05-16 03:53:43 +0200 | [diff] [blame] | 1855 | #endif |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1856 | p += 1 + stupid_insert(p, c); // insert the char |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1857 | #if ENABLE_FEATURE_VI_SETOPTS |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1858 | if (showmatch && strchr(")]}", *sp) != NULL) { |
| 1859 | showmatching(sp); |
| 1860 | } |
| 1861 | if (autoindent && c == '\n') { // auto indent the new line |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1862 | char *q; |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 1863 | size_t len; |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1864 | q = prev_line(p); // use prev line as template |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 1865 | len = strspn(q, " \t"); // space or tab |
| 1866 | if (len) { |
Denis Vlasenko | 3266aa9 | 2009-04-01 11:24:04 +0000 | [diff] [blame] | 1867 | uintptr_t bias; |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 1868 | bias = text_hole_make(p, len); |
| 1869 | p += bias; |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1870 | q += bias; |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 1871 | memcpy(p, q, len); |
Denis Vlasenko | 3266aa9 | 2009-04-01 11:24:04 +0000 | [diff] [blame] | 1872 | p += len; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1873 | } |
| 1874 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 1875 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1876 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 1877 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1880 | // might reallocate text[]! use p += stupid_insert(p, ...), |
| 1881 | // and be careful to not use pointers into potentially freed text[]! |
| 1882 | static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p' |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1883 | { |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1884 | uintptr_t bias; |
| 1885 | bias = text_hole_make(p, 1); |
| 1886 | p += bias; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 1887 | *p = c; |
| 1888 | //file_modified++; - done by text_hole_make() |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 1889 | return bias; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 1892 | static int find_range(char **start, char **stop, char c) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1893 | { |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 1894 | char *save_dot, *p, *q, *t; |
| 1895 | int cnt, multiline = 0; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1896 | |
| 1897 | save_dot = dot; |
| 1898 | p = q = dot; |
| 1899 | |
| 1900 | if (strchr("cdy><", c)) { |
| 1901 | // these cmds operate on whole lines |
| 1902 | p = q = begin_line(p); |
| 1903 | for (cnt = 1; cnt < cmdcnt; cnt++) { |
| 1904 | q = next_line(q); |
| 1905 | } |
| 1906 | q = end_line(q); |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 1907 | } else if (strchr("^%$0bBeEfth\b\177", c)) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1908 | // These cmds operate on char positions |
| 1909 | do_cmd(c); // execute movement cmd |
| 1910 | q = dot; |
| 1911 | } else if (strchr("wW", c)) { |
| 1912 | do_cmd(c); // execute movement cmd |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1913 | // if we are at the next word's first char |
| 1914 | // step back one char |
| 1915 | // but check the possibilities when it is true |
Eric Andersen | 5cc90ea | 2004-02-06 10:36:08 +0000 | [diff] [blame] | 1916 | if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0])) |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 1917 | || (ispunct(dot[-1]) && !ispunct(dot[0])) |
| 1918 | || (isalnum(dot[-1]) && !isalnum(dot[0])))) |
| 1919 | dot--; // move back off of next word |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1920 | if (dot > text && *dot == '\n') |
| 1921 | dot--; // stay off NL |
| 1922 | q = dot; |
| 1923 | } else if (strchr("H-k{", c)) { |
| 1924 | // these operate on multi-lines backwards |
| 1925 | q = end_line(dot); // find NL |
| 1926 | do_cmd(c); // execute movement cmd |
| 1927 | dot_begin(); |
| 1928 | p = dot; |
| 1929 | } else if (strchr("L+j}\r\n", c)) { |
| 1930 | // these operate on multi-lines forwards |
| 1931 | p = begin_line(dot); |
| 1932 | do_cmd(c); // execute movement cmd |
| 1933 | dot_end(); // find NL |
| 1934 | q = dot; |
| 1935 | } else { |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 1936 | // nothing -- this causes any other values of c to |
| 1937 | // represent the one-character range under the |
| 1938 | // cursor. this is correct for ' ' and 'l', but |
| 1939 | // perhaps no others. |
| 1940 | // |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1941 | } |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 1942 | if (q < p) { |
| 1943 | t = q; |
| 1944 | q = p; |
| 1945 | p = t; |
| 1946 | } |
| 1947 | |
Denis Vlasenko | 42cc304 | 2008-03-24 02:05:58 +0000 | [diff] [blame] | 1948 | // backward char movements don't include start position |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 1949 | if (q > p && strchr("^0bBh\b\177", c)) q--; |
| 1950 | |
| 1951 | multiline = 0; |
| 1952 | for (t = p; t <= q; t++) { |
| 1953 | if (*t == '\n') { |
| 1954 | multiline = 1; |
| 1955 | break; |
| 1956 | } |
| 1957 | } |
| 1958 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1959 | *start = p; |
| 1960 | *stop = q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1961 | dot = save_dot; |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 1962 | return multiline; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1963 | } |
| 1964 | |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 1965 | static int st_test(char *p, int type, int dir, char *tested) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1966 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 1967 | char c, c0, ci; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1968 | int test, inc; |
| 1969 | |
| 1970 | inc = dir; |
| 1971 | c = c0 = p[0]; |
| 1972 | ci = p[inc]; |
| 1973 | test = 0; |
| 1974 | |
| 1975 | if (type == S_BEFORE_WS) { |
| 1976 | c = ci; |
Denys Vlasenko | c0dab37 | 2009-10-22 22:28:08 +0200 | [diff] [blame] | 1977 | test = (!isspace(c) || c == '\n'); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1978 | } |
| 1979 | if (type == S_TO_WS) { |
| 1980 | c = c0; |
Denys Vlasenko | c0dab37 | 2009-10-22 22:28:08 +0200 | [diff] [blame] | 1981 | test = (!isspace(c) || c == '\n'); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1982 | } |
| 1983 | if (type == S_OVER_WS) { |
| 1984 | c = c0; |
Denys Vlasenko | c0dab37 | 2009-10-22 22:28:08 +0200 | [diff] [blame] | 1985 | test = isspace(c); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1986 | } |
| 1987 | if (type == S_END_PUNCT) { |
| 1988 | c = ci; |
Denys Vlasenko | c0dab37 | 2009-10-22 22:28:08 +0200 | [diff] [blame] | 1989 | test = ispunct(c); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1990 | } |
| 1991 | if (type == S_END_ALNUM) { |
| 1992 | c = ci; |
Denys Vlasenko | c0dab37 | 2009-10-22 22:28:08 +0200 | [diff] [blame] | 1993 | test = (isalnum(c) || c == '_'); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1994 | } |
| 1995 | *tested = c; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 1996 | return test; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 1997 | } |
| 1998 | |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 1999 | static char *skip_thing(char *p, int linecnt, int dir, int type) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2000 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2001 | char c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2002 | |
| 2003 | while (st_test(p, type, dir, &c)) { |
| 2004 | // make sure we limit search to correct number of lines |
| 2005 | if (c == '\n' && --linecnt < 1) |
| 2006 | break; |
| 2007 | if (dir >= 0 && p >= end - 1) |
| 2008 | break; |
| 2009 | if (dir < 0 && p <= text) |
| 2010 | break; |
| 2011 | p += dir; // move to next char |
| 2012 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2013 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2014 | } |
| 2015 | |
| 2016 | // find matching char of pair () [] {} |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 2017 | static char *find_pair(char *p, const char c) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2018 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2019 | char match, *q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2020 | int dir, level; |
| 2021 | |
| 2022 | match = ')'; |
| 2023 | level = 1; |
| 2024 | dir = 1; // assume forward |
| 2025 | switch (c) { |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 2026 | case '(': match = ')'; break; |
| 2027 | case '[': match = ']'; break; |
| 2028 | case '{': match = '}'; break; |
| 2029 | case ')': match = '('; dir = -1; break; |
| 2030 | case ']': match = '['; dir = -1; break; |
| 2031 | case '}': match = '{'; dir = -1; break; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2032 | } |
| 2033 | for (q = p + dir; text <= q && q < end; q += dir) { |
| 2034 | // look for match, count levels of pairs (( )) |
| 2035 | if (*q == c) |
| 2036 | level++; // increase pair levels |
| 2037 | if (*q == match) |
| 2038 | level--; // reduce pair level |
| 2039 | if (level == 0) |
| 2040 | break; // found matching pair |
| 2041 | } |
| 2042 | if (level != 0) |
| 2043 | q = NULL; // indicate no match |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2044 | return q; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2045 | } |
| 2046 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2047 | #if ENABLE_FEATURE_VI_SETOPTS |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2048 | // show the matching char of a pair, () [] {} |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 2049 | static void showmatching(char *p) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2050 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2051 | char *q, *save_dot; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2052 | |
| 2053 | // we found half of a pair |
| 2054 | q = find_pair(p, *p); // get loc of matching char |
| 2055 | if (q == NULL) { |
| 2056 | indicate_error('3'); // no matching char |
| 2057 | } else { |
| 2058 | // "q" now points to matching pair |
| 2059 | save_dot = dot; // remember where we are |
| 2060 | dot = q; // go to new loc |
| 2061 | refresh(FALSE); // let the user see it |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2062 | mysleep(40); // give user some time |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2063 | dot = save_dot; // go back to old loc |
| 2064 | refresh(FALSE); |
| 2065 | } |
| 2066 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2067 | #endif /* FEATURE_VI_SETOPTS */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2068 | |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2069 | // open a hole in text[] |
| 2070 | // might reallocate text[]! use p += text_hole_make(p, ...), |
| 2071 | // and be careful to not use pointers into potentially freed text[]! |
| 2072 | static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2073 | { |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2074 | uintptr_t bias = 0; |
| 2075 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2076 | if (size <= 0) |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2077 | return bias; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2078 | end += size; // adjust the new END |
| 2079 | if (end >= (text + text_size)) { |
| 2080 | char *new_text; |
| 2081 | text_size += end - (text + text_size) + 10240; |
| 2082 | new_text = xrealloc(text, text_size); |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2083 | bias = (new_text - text); |
| 2084 | screenbegin += bias; |
| 2085 | dot += bias; |
| 2086 | end += bias; |
| 2087 | p += bias; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 2088 | #if ENABLE_FEATURE_VI_YANKMARK |
| 2089 | { |
Tanguy Pruvot | 6fef6a3 | 2012-05-05 15:26:43 +0200 | [diff] [blame] | 2090 | unsigned i; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 2091 | for (i = 0; i < ARRAY_SIZE(mark); i++) |
| 2092 | if (mark[i]) |
| 2093 | mark[i] += bias; |
| 2094 | } |
| 2095 | #endif |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2096 | text = new_text; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2097 | } |
Denis Vlasenko | d699544 | 2008-06-27 04:06:13 +0000 | [diff] [blame] | 2098 | memmove(p + size, p, end - size - p); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2099 | memset(p, ' ', size); // clear new hole |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2100 | file_modified++; |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2101 | return bias; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2102 | } |
| 2103 | |
| 2104 | // close a hole in text[] |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 2105 | static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2106 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2107 | char *src, *dest; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2108 | int cnt, hole_size; |
| 2109 | |
| 2110 | // move forwards, from beginning |
| 2111 | // assume p <= q |
| 2112 | src = q + 1; |
| 2113 | dest = p; |
| 2114 | if (q < p) { // they are backward- swap them |
| 2115 | src = p + 1; |
| 2116 | dest = q; |
| 2117 | } |
| 2118 | hole_size = q - p + 1; |
| 2119 | cnt = end - src; |
| 2120 | if (src < text || src > end) |
| 2121 | goto thd0; |
| 2122 | if (dest < text || dest >= end) |
| 2123 | goto thd0; |
| 2124 | if (src >= end) |
| 2125 | goto thd_atend; // just delete the end of the buffer |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2126 | memmove(dest, src, cnt); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2127 | thd_atend: |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2128 | end = end - hole_size; // adjust the new END |
| 2129 | if (dest >= end) |
| 2130 | dest = end - 1; // make sure dest in below end-1 |
| 2131 | if (end <= text) |
| 2132 | dest = end = text; // keep pointers valid |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2133 | file_modified++; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2134 | thd0: |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2135 | return dest; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | // copy text into register, then delete text. |
| 2139 | // if dist <= 0, do not include, or go past, a NewLine |
| 2140 | // |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 2141 | static char *yank_delete(char *start, char *stop, int dist, int yf) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2142 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2143 | char *p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2144 | |
| 2145 | // make sure start <= stop |
| 2146 | if (start > stop) { |
| 2147 | // they are backwards, reverse them |
| 2148 | p = start; |
| 2149 | start = stop; |
| 2150 | stop = p; |
| 2151 | } |
| 2152 | if (dist <= 0) { |
Denis Vlasenko | e1a0d48 | 2006-10-20 13:28:22 +0000 | [diff] [blame] | 2153 | // we cannot cross NL boundaries |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2154 | p = start; |
| 2155 | if (*p == '\n') |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2156 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2157 | // dont go past a NewLine |
| 2158 | for (; p + 1 <= stop; p++) { |
| 2159 | if (p[1] == '\n') { |
| 2160 | stop = p; // "stop" just before NewLine |
| 2161 | break; |
| 2162 | } |
| 2163 | } |
| 2164 | } |
| 2165 | p = start; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2166 | #if ENABLE_FEATURE_VI_YANKMARK |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2167 | text_yank(start, stop, YDreg); |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2168 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2169 | if (yf == YANKDEL) { |
| 2170 | p = text_hole_delete(start, stop); |
| 2171 | } // delete lines |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2172 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2173 | } |
| 2174 | |
| 2175 | static void show_help(void) |
| 2176 | { |
| 2177 | puts("These features are available:" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2178 | #if ENABLE_FEATURE_VI_SEARCH |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2179 | "\n\tPattern searches with / and ?" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2180 | #endif |
| 2181 | #if ENABLE_FEATURE_VI_DOT_CMD |
Denys Vlasenko | 5f556fc | 2012-06-11 01:53:33 +0200 | [diff] [blame^] | 2182 | "\n\tLast command repeat with ." |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2183 | #endif |
| 2184 | #if ENABLE_FEATURE_VI_YANKMARK |
Bernhard Reutner-Fischer | d24d5c8 | 2007-10-01 18:04:42 +0000 | [diff] [blame] | 2185 | "\n\tLine marking with 'x" |
| 2186 | "\n\tNamed buffers with \"x" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2187 | #endif |
| 2188 | #if ENABLE_FEATURE_VI_READONLY |
Walter Harms | b9ba580 | 2011-06-27 02:59:37 +0200 | [diff] [blame] | 2189 | //not implemented: "\n\tReadonly if vi is called as \"view\"" |
| 2190 | //redundant: usage text says this too: "\n\tReadonly with -R command line arg" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2191 | #endif |
| 2192 | #if ENABLE_FEATURE_VI_SET |
Denys Vlasenko | 5f556fc | 2012-06-11 01:53:33 +0200 | [diff] [blame^] | 2193 | "\n\tSome colon mode commands with :" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2194 | #endif |
| 2195 | #if ENABLE_FEATURE_VI_SETOPTS |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2196 | "\n\tSettable options with \":set\"" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2197 | #endif |
| 2198 | #if ENABLE_FEATURE_VI_USE_SIGNALS |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2199 | "\n\tSignal catching- ^C" |
| 2200 | "\n\tJob suspend and resume with ^Z" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2201 | #endif |
| 2202 | #if ENABLE_FEATURE_VI_WIN_RESIZE |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2203 | "\n\tAdapt to window re-sizes" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2204 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2205 | ); |
| 2206 | } |
| 2207 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2208 | #if ENABLE_FEATURE_VI_DOT_CMD |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2209 | static void start_new_cmd_q(char c) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2210 | { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2211 | // get buffer for new cmd |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2212 | // if there is a current cmd count put it in the buffer first |
Denis Vlasenko | 30cfdf9 | 2008-09-21 15:29:29 +0000 | [diff] [blame] | 2213 | if (cmdcnt > 0) { |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 2214 | lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c); |
Denis Vlasenko | 30cfdf9 | 2008-09-21 15:29:29 +0000 | [diff] [blame] | 2215 | } else { // just save char c onto queue |
Paul Fox | d957b95 | 2005-11-28 18:07:53 +0000 | [diff] [blame] | 2216 | last_modifying_cmd[0] = c; |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 2217 | lmc_len = 1; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2218 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2219 | adding2q = 1; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2220 | } |
| 2221 | |
| 2222 | static void end_cmd_q(void) |
| 2223 | { |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2224 | #if ENABLE_FEATURE_VI_YANKMARK |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2225 | YDreg = 26; // go back to default Yank/Delete reg |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2226 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2227 | adding2q = 0; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2228 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2229 | #endif /* FEATURE_VI_DOT_CMD */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2230 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2231 | #if ENABLE_FEATURE_VI_YANKMARK \ |
| 2232 | || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \ |
| 2233 | || ENABLE_FEATURE_VI_CRASHME |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2234 | // might reallocate text[]! use p += string_insert(p, ...), |
| 2235 | // and be careful to not use pointers into potentially freed text[]! |
| 2236 | static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p' |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2237 | { |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2238 | uintptr_t bias; |
| 2239 | int i; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2240 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2241 | i = strlen(s); |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2242 | bias = text_hole_make(p, i); |
| 2243 | p += bias; |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 2244 | memcpy(p, s, i); |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 2245 | #if ENABLE_FEATURE_VI_YANKMARK |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2246 | { |
| 2247 | int cnt; |
| 2248 | for (cnt = 0; *s != '\0'; s++) { |
| 2249 | if (*s == '\n') |
| 2250 | cnt++; |
| 2251 | } |
| 2252 | status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg()); |
| 2253 | } |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 2254 | #endif |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2255 | return bias; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2256 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2257 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2258 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2259 | #if ENABLE_FEATURE_VI_YANKMARK |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 2260 | static char *text_yank(char *p, char *q, int dest) // copy text into a register |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2261 | { |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 2262 | int cnt = q - p; |
| 2263 | if (cnt < 0) { // they are backwards- reverse them |
| 2264 | p = q; |
| 2265 | cnt = -cnt; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2266 | } |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 2267 | free(reg[dest]); // if already a yank register, free it |
| 2268 | reg[dest] = xstrndup(p, cnt + 1); |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2269 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2270 | } |
| 2271 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2272 | static char what_reg(void) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2273 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2274 | char c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2275 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2276 | c = 'D'; // default to D-reg |
| 2277 | if (0 <= YDreg && YDreg <= 25) |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2278 | c = 'a' + (char) YDreg; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2279 | if (YDreg == 26) |
| 2280 | c = 'D'; |
| 2281 | if (YDreg == 27) |
| 2282 | c = 'U'; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2283 | return c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2284 | } |
| 2285 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2286 | static void check_context(char cmd) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2287 | { |
| 2288 | // A context is defined to be "modifying text" |
| 2289 | // Any modifying command establishes a new context. |
| 2290 | |
| 2291 | if (dot < context_start || dot > context_end) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2292 | if (strchr(modifying_cmds, cmd) != NULL) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2293 | // we are trying to modify text[]- make this the current context |
| 2294 | mark[27] = mark[26]; // move cur to prev |
| 2295 | mark[26] = dot; // move local to cur |
| 2296 | context_start = prev_line(prev_line(dot)); |
| 2297 | context_end = next_line(next_line(dot)); |
| 2298 | //loiter= start_loiter= now; |
| 2299 | } |
| 2300 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2301 | } |
| 2302 | |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 2303 | static char *swap_context(char *p) // goto new context for '' command make this the current context |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2304 | { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2305 | char *tmp; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2306 | |
| 2307 | // the current context is in mark[26] |
| 2308 | // the previous context is in mark[27] |
| 2309 | // only swap context if other context is valid |
| 2310 | if (text <= mark[27] && mark[27] <= end - 1) { |
| 2311 | tmp = mark[27]; |
| 2312 | mark[27] = mark[26]; |
| 2313 | mark[26] = tmp; |
| 2314 | p = mark[26]; // where we are going- previous context |
| 2315 | context_start = prev_line(prev_line(prev_line(p))); |
| 2316 | context_end = next_line(next_line(next_line(p))); |
| 2317 | } |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2318 | return p; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2319 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2320 | #endif /* FEATURE_VI_YANKMARK */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2321 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2322 | //----- Set terminal attributes -------------------------------- |
| 2323 | static void rawmode(void) |
| 2324 | { |
| 2325 | tcgetattr(0, &term_orig); |
| 2326 | term_vi = term_orig; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 2327 | term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG on - allow intr's |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2328 | term_vi.c_iflag &= (~IXON & ~ICRNL); |
| 2329 | term_vi.c_oflag &= (~ONLCR); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2330 | term_vi.c_cc[VMIN] = 1; |
| 2331 | term_vi.c_cc[VTIME] = 0; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2332 | erase_char = term_vi.c_cc[VERASE]; |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 2333 | tcsetattr_stdin_TCSANOW(&term_vi); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2334 | } |
| 2335 | |
| 2336 | static void cookmode(void) |
| 2337 | { |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2338 | fflush_all(); |
Denis Vlasenko | 202ac50 | 2008-11-05 13:20:58 +0000 | [diff] [blame] | 2339 | tcsetattr_stdin_TCSANOW(&term_orig); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2340 | } |
| 2341 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2342 | #if ENABLE_FEATURE_VI_USE_SIGNALS |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2343 | //----- Come here when we get a window resize signal --------- |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2344 | static void winch_sig(int sig UNUSED_PARAM) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2345 | { |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2346 | int save_errno = errno; |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 2347 | // FIXME: do it in main loop!!! |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2348 | signal(SIGWINCH, winch_sig); |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2349 | query_screen_dimensions(); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2350 | new_screen(rows, columns); // get memory for virtual screen |
| 2351 | redraw(TRUE); // re-draw the screen |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2352 | errno = save_errno; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2353 | } |
| 2354 | |
| 2355 | //----- Come here when we get a continue signal ------------------- |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2356 | static void cont_sig(int sig UNUSED_PARAM) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2357 | { |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2358 | int save_errno = errno; |
Denis Vlasenko | 30cfdf9 | 2008-09-21 15:29:29 +0000 | [diff] [blame] | 2359 | rawmode(); // terminal to "raw" |
| 2360 | last_status_cksum = 0; // force status update |
| 2361 | redraw(TRUE); // re-draw the screen |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2362 | |
| 2363 | signal(SIGTSTP, suspend_sig); |
| 2364 | signal(SIGCONT, SIG_DFL); |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2365 | //kill(my_pid, SIGCONT); // huh? why? we are already "continued"... |
| 2366 | errno = save_errno; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2367 | } |
| 2368 | |
| 2369 | //----- Come here when we get a Suspend signal ------------------- |
Denis Vlasenko | a60f84e | 2008-07-05 09:18:54 +0000 | [diff] [blame] | 2370 | static void suspend_sig(int sig UNUSED_PARAM) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2371 | { |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2372 | int save_errno = errno; |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 2373 | go_bottom_and_clear_to_eol(); |
Denis Vlasenko | 30cfdf9 | 2008-09-21 15:29:29 +0000 | [diff] [blame] | 2374 | cookmode(); // terminal to "cooked" |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2375 | |
| 2376 | signal(SIGCONT, cont_sig); |
| 2377 | signal(SIGTSTP, SIG_DFL); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 2378 | kill(my_pid, SIGTSTP); |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2379 | errno = save_errno; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
| 2382 | //----- Come here when we get a signal --------------------------- |
| 2383 | static void catch_sig(int sig) |
| 2384 | { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2385 | signal(SIGINT, catch_sig); |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2386 | siglongjmp(restart, sig); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2387 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2388 | #endif /* FEATURE_VI_USE_SIGNALS */ |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2389 | |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 2390 | static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2391 | { |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 2392 | struct pollfd pfd[1]; |
Denis Vlasenko | cd5c786 | 2007-05-17 16:37:22 +0000 | [diff] [blame] | 2393 | |
Denys Vlasenko | 606291b | 2009-09-23 23:15:43 +0200 | [diff] [blame] | 2394 | pfd[0].fd = STDIN_FILENO; |
Denis Vlasenko | 87f3b26 | 2007-09-07 13:43:28 +0000 | [diff] [blame] | 2395 | pfd[0].events = POLLIN; |
Denis Vlasenko | 5d61e71 | 2007-09-27 10:09:59 +0000 | [diff] [blame] | 2396 | return safe_poll(pfd, 1, hund*10) > 0; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2397 | } |
| 2398 | |
| 2399 | //----- IO Routines -------------------------------------------- |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 2400 | static int readit(void) // read (maybe cursor) key from stdin |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2401 | { |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 2402 | int c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2403 | |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2404 | fflush_all(); |
Denys Vlasenko | 58f108e | 2010-03-11 21:17:55 +0100 | [diff] [blame] | 2405 | c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2); |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 2406 | if (c == -1) { // EOF/error |
| 2407 | go_bottom_and_clear_to_eol(); |
| 2408 | cookmode(); // terminal to "cooked" |
| 2409 | bb_error_msg_and_die("can't read user input"); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 2410 | } |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2411 | return c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2412 | } |
| 2413 | |
| 2414 | //----- IO Routines -------------------------------------------- |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 2415 | static int get_one_char(void) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2416 | { |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 2417 | int c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2418 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2419 | #if ENABLE_FEATURE_VI_DOT_CMD |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2420 | if (!adding2q) { |
| 2421 | // we are not adding to the q. |
| 2422 | // but, we may be reading from a q |
| 2423 | if (ioq == 0) { |
| 2424 | // there is no current q, read from STDIN |
| 2425 | c = readit(); // get the users input |
| 2426 | } else { |
| 2427 | // there is a queue to get chars from first |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 2428 | // careful with correct sign expansion! |
| 2429 | c = (unsigned char)*ioq++; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2430 | if (c == '\0') { |
| 2431 | // the end of the q, read from STDIN |
| 2432 | free(ioq_start); |
| 2433 | ioq_start = ioq = 0; |
| 2434 | c = readit(); // get the users input |
| 2435 | } |
| 2436 | } |
| 2437 | } else { |
| 2438 | // adding STDIN chars to q |
| 2439 | c = readit(); // get the users input |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2440 | if (lmc_len >= MAX_INPUT_LEN - 1) { |
| 2441 | status_line_bold("last_modifying_cmd overrun"); |
| 2442 | } else { |
| 2443 | // add new char to q |
| 2444 | last_modifying_cmd[lmc_len++] = c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2445 | } |
| 2446 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2447 | #else |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2448 | c = readit(); // get the users input |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2449 | #endif /* FEATURE_VI_DOT_CMD */ |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2450 | return c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2451 | } |
| 2452 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2453 | // Get input line (uses "status line" area) |
| 2454 | static char *get_input_line(const char *prompt) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2455 | { |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2456 | // char [MAX_INPUT_LEN] |
| 2457 | #define buf get_input_line__buf |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2458 | |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 2459 | int c; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2460 | int i; |
| 2461 | |
| 2462 | strcpy(buf, prompt); |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2463 | last_status_cksum = 0; // force status update |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 2464 | go_bottom_and_clear_to_eol(); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2465 | write1(prompt); // write out the :, /, or ? prompt |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2466 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2467 | i = strlen(buf); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2468 | while (i < MAX_INPUT_LEN) { |
| 2469 | c = get_one_char(); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2470 | if (c == '\n' || c == '\r' || c == 27) |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2471 | break; // this is end of input |
Paul Fox | f2de0b7 | 2005-09-13 22:20:37 +0000 | [diff] [blame] | 2472 | if (c == erase_char || c == 8 || c == 127) { |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 2473 | // user wants to erase prev char |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2474 | buf[--i] = '\0'; |
| 2475 | write1("\b \b"); // erase char on screen |
| 2476 | if (i <= 0) // user backs up before b-o-l, exit |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2477 | break; |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 2478 | } else if (c > 0 && c < 256) { // exclude Unicode |
| 2479 | // (TODO: need to handle Unicode) |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2480 | buf[i] = c; |
| 2481 | buf[++i] = '\0'; |
| 2482 | bb_putchar(c); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2483 | } |
| 2484 | } |
| 2485 | refresh(FALSE); |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2486 | return buf; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2487 | #undef buf |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2488 | } |
| 2489 | |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2490 | static int file_size(const char *fn) // what is the byte size of "fn" |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2491 | { |
| 2492 | struct stat st_buf; |
Denis Vlasenko | 59a1f30 | 2007-07-14 22:43:10 +0000 | [diff] [blame] | 2493 | int cnt; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2494 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2495 | cnt = -1; |
Denys Vlasenko | def4783 | 2010-04-18 20:39:41 -0700 | [diff] [blame] | 2496 | if (fn && stat(fn, &st_buf) == 0) // see if file exists |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2497 | cnt = (int) st_buf.st_size; |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2498 | return cnt; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2499 | } |
| 2500 | |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2501 | // might reallocate text[]! |
| 2502 | static int file_insert(const char *fn, char *p, int update_ro_status) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2503 | { |
Denis Vlasenko | 59a1f30 | 2007-07-14 22:43:10 +0000 | [diff] [blame] | 2504 | int cnt = -1; |
| 2505 | int fd, size; |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2506 | struct stat statbuf; |
| 2507 | |
| 2508 | /* Validate file */ |
| 2509 | if (stat(fn, &statbuf) < 0) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2510 | status_line_bold("\"%s\" %s", fn, strerror(errno)); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2511 | goto fi0; |
| 2512 | } |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 2513 | if (!S_ISREG(statbuf.st_mode)) { |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2514 | // This is not a regular file |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2515 | status_line_bold("\"%s\" Not a regular file", fn); |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2516 | goto fi0; |
| 2517 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2518 | if (p < text || p > end) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2519 | status_line_bold("Trying to insert file outside of memory"); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2520 | goto fi0; |
| 2521 | } |
| 2522 | |
Denis Vlasenko | 59a1f30 | 2007-07-14 22:43:10 +0000 | [diff] [blame] | 2523 | // read file to buffer |
| 2524 | fd = open(fn, O_RDONLY); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2525 | if (fd < 0) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2526 | status_line_bold("\"%s\" %s", fn, strerror(errno)); |
Denis Vlasenko | 59a1f30 | 2007-07-14 22:43:10 +0000 | [diff] [blame] | 2527 | goto fi0; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2528 | } |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2529 | size = statbuf.st_size; |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 2530 | p += text_hole_make(p, size); |
Denis Vlasenko | 4f95e5a | 2007-10-11 10:10:15 +0000 | [diff] [blame] | 2531 | cnt = safe_read(fd, p, size); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2532 | if (cnt < 0) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2533 | status_line_bold("\"%s\" %s", fn, strerror(errno)); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2534 | p = text_hole_delete(p, p + size - 1); // un-do buffer insert |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2535 | } else if (cnt < size) { |
| 2536 | // There was a partial read, shrink unused space text[] |
| 2537 | p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert |
Denys Vlasenko | 6331cf0 | 2009-11-13 09:08:27 +0100 | [diff] [blame] | 2538 | status_line_bold("can't read all of file \"%s\"", fn); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2539 | } |
| 2540 | if (cnt >= size) |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2541 | file_modified++; |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2542 | close(fd); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2543 | fi0: |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 2544 | #if ENABLE_FEATURE_VI_READONLY |
| 2545 | if (update_ro_status |
| 2546 | && ((access(fn, W_OK) < 0) || |
| 2547 | /* root will always have access() |
| 2548 | * so we check fileperms too */ |
| 2549 | !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH)) |
| 2550 | ) |
| 2551 | ) { |
Denis Vlasenko | 2f6ae43 | 2007-07-19 22:50:47 +0000 | [diff] [blame] | 2552 | SET_READONLY_FILE(readonly_mode); |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2553 | } |
Denis Vlasenko | 856be77 | 2007-08-17 08:29:48 +0000 | [diff] [blame] | 2554 | #endif |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2555 | return cnt; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2556 | } |
| 2557 | |
Denis Vlasenko | 3387538 | 2008-06-21 20:31:50 +0000 | [diff] [blame] | 2558 | static int file_write(char *fn, char *first, char *last) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2559 | { |
| 2560 | int fd, cnt, charcnt; |
| 2561 | |
| 2562 | if (fn == 0) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2563 | status_line_bold("No current filename"); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2564 | return -2; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2565 | } |
Denis Vlasenko | 8abae88 | 2008-05-03 11:35:59 +0000 | [diff] [blame] | 2566 | /* By popular request we do not open file with O_TRUNC, |
| 2567 | * but instead ftruncate() it _after_ successful write. |
| 2568 | * Might reduce amount of data lost on power fail etc. |
| 2569 | */ |
| 2570 | fd = open(fn, (O_WRONLY | O_CREAT), 0666); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2571 | if (fd < 0) |
Denis Vlasenko | 079f8af | 2006-11-27 16:49:31 +0000 | [diff] [blame] | 2572 | return -1; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2573 | cnt = last - first + 1; |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2574 | charcnt = full_write(fd, first, cnt); |
Denis Vlasenko | 8abae88 | 2008-05-03 11:35:59 +0000 | [diff] [blame] | 2575 | ftruncate(fd, charcnt); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2576 | if (charcnt == cnt) { |
| 2577 | // good write |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2578 | //file_modified = FALSE; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2579 | } else { |
| 2580 | charcnt = 0; |
| 2581 | } |
| 2582 | close(fd); |
Denis Vlasenko | d9e15f2 | 2006-11-27 16:49:55 +0000 | [diff] [blame] | 2583 | return charcnt; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2584 | } |
| 2585 | |
| 2586 | //----- Terminal Drawing --------------------------------------- |
| 2587 | // The terminal is made up of 'rows' line of 'columns' columns. |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 2588 | // classically this would be 24 x 80. |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2589 | // screen coordinates |
| 2590 | // 0,0 ... 0,79 |
| 2591 | // 1,0 ... 1,79 |
| 2592 | // . ... . |
| 2593 | // . ... . |
| 2594 | // 22,0 ... 22,79 |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2595 | // 23,0 ... 23,79 <- status line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2596 | |
| 2597 | //----- Move the cursor to row x col (count from 0, not 1) ------- |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2598 | static void place_cursor(int row, int col, int optimize) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2599 | { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2600 | char cm1[sizeof(CMrc) + sizeof(int)*3 * 2]; |
Denis Vlasenko | 7b54dc7 | 2008-07-17 21:32:32 +0000 | [diff] [blame] | 2601 | #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR |
| 2602 | enum { |
| 2603 | SZ_UP = sizeof(CMup), |
| 2604 | SZ_DN = sizeof(CMdown), |
| 2605 | SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN, |
| 2606 | }; |
| 2607 | char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size |
| 2608 | #endif |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2609 | char *cm; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2610 | |
| 2611 | if (row < 0) row = 0; |
| 2612 | if (row >= rows) row = rows - 1; |
| 2613 | if (col < 0) col = 0; |
| 2614 | if (col >= columns) col = columns - 1; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2615 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2616 | //----- 1. Try the standard terminal ESC sequence |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2617 | sprintf(cm1, CMrc, row + 1, col + 1); |
| 2618 | cm = cm1; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2619 | |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2620 | #if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2621 | if (optimize && col < 16) { |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2622 | char *screenp; |
| 2623 | int Rrow = last_row; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2624 | int diff = Rrow - row; |
| 2625 | |
| 2626 | if (diff < -5 || diff > 5) |
| 2627 | goto skip; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2628 | |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2629 | //----- find the minimum # of chars to move cursor ------------- |
| 2630 | //----- 2. Try moving with discreet chars (Newline, [back]space, ...) |
| 2631 | cm2[0] = '\0'; |
| 2632 | |
| 2633 | // move to the correct row |
| 2634 | while (row < Rrow) { |
| 2635 | // the cursor has to move up |
| 2636 | strcat(cm2, CMup); |
| 2637 | Rrow--; |
| 2638 | } |
| 2639 | while (row > Rrow) { |
| 2640 | // the cursor has to move down |
| 2641 | strcat(cm2, CMdown); |
| 2642 | Rrow++; |
| 2643 | } |
| 2644 | |
| 2645 | // now move to the correct column |
| 2646 | strcat(cm2, "\r"); // start at col 0 |
| 2647 | // just send out orignal source char to get to correct place |
| 2648 | screenp = &screen[row * columns]; // start of screen line |
| 2649 | strncat(cm2, screenp, col); |
| 2650 | |
| 2651 | // pick the shortest cursor motion to send out |
| 2652 | if (strlen(cm2) < strlen(cm)) { |
| 2653 | cm = cm2; |
| 2654 | } |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2655 | skip: ; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2656 | } |
Denis Vlasenko | 4baed3a | 2007-12-22 17:31:29 +0000 | [diff] [blame] | 2657 | last_row = row; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2658 | #endif /* FEATURE_VI_OPTIMIZE_CURSOR */ |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2659 | write1(cm); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2660 | } |
| 2661 | |
| 2662 | //----- Erase from cursor to end of line ----------------------- |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 2663 | static void clear_to_eol(void) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2664 | { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 2665 | write1(Ceol); // Erase from cursor to end of line |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2666 | } |
| 2667 | |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 2668 | static void go_bottom_and_clear_to_eol(void) |
| 2669 | { |
| 2670 | place_cursor(rows - 1, 0, FALSE); // go to bottom of screen |
| 2671 | clear_to_eol(); // erase to end of line |
| 2672 | } |
| 2673 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2674 | //----- Erase from cursor to end of screen ----------------------- |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 2675 | static void clear_to_eos(void) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2676 | { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 2677 | write1(Ceos); // Erase from cursor to end of screen |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2678 | } |
| 2679 | |
| 2680 | //----- Start standout mode ------------------------------------ |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 2681 | static void standout_start(void) // send "start reverse video" sequence |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2682 | { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 2683 | write1(SOs); // Start reverse video mode |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2684 | } |
| 2685 | |
| 2686 | //----- End standout mode -------------------------------------- |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 2687 | static void standout_end(void) // send "end reverse video" sequence |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2688 | { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 2689 | write1(SOn); // End reverse video mode |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | //----- Flash the screen -------------------------------------- |
| 2693 | static void flash(int h) |
| 2694 | { |
| 2695 | standout_start(); // send "start reverse video" sequence |
| 2696 | redraw(TRUE); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2697 | mysleep(h); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2698 | standout_end(); // send "end reverse video" sequence |
| 2699 | redraw(TRUE); |
| 2700 | } |
| 2701 | |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 2702 | static void Indicate_Error(void) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2703 | { |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2704 | #if ENABLE_FEATURE_VI_CRASHME |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2705 | if (crashme > 0) |
| 2706 | return; // generate a random command |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2707 | #endif |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 2708 | if (!err_method) { |
| 2709 | write1(bell); // send out a bell character |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2710 | } else { |
| 2711 | flash(10); |
| 2712 | } |
| 2713 | } |
| 2714 | |
| 2715 | //----- Screen[] Routines -------------------------------------- |
| 2716 | //----- Erase the Screen[] memory ------------------------------ |
Mike Frysinger | 4e5936e | 2005-04-16 04:30:38 +0000 | [diff] [blame] | 2717 | static void screen_erase(void) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2718 | { |
| 2719 | memset(screen, ' ', screensize); // clear new screen |
| 2720 | } |
| 2721 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2722 | static int bufsum(char *buf, int count) |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2723 | { |
| 2724 | int sum = 0; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2725 | char *e = buf + count; |
| 2726 | |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2727 | while (buf < e) |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2728 | sum += (unsigned char) *buf++; |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2729 | return sum; |
| 2730 | } |
| 2731 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2732 | //----- Draw the status line at bottom of the screen ------------- |
| 2733 | static void show_status_line(void) |
| 2734 | { |
Paul Fox | c350485 | 2005-09-16 12:48:18 +0000 | [diff] [blame] | 2735 | int cnt = 0, cksum = 0; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2736 | |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2737 | // either we already have an error or status message, or we |
| 2738 | // create one. |
| 2739 | if (!have_status_msg) { |
| 2740 | cnt = format_edit_status(); |
| 2741 | cksum = bufsum(status_buffer, cnt); |
| 2742 | } |
| 2743 | if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2744 | last_status_cksum = cksum; // remember if we have seen this line |
Denis Vlasenko | 267e16c | 2008-10-14 10:34:41 +0000 | [diff] [blame] | 2745 | go_bottom_and_clear_to_eol(); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2746 | write1(status_buffer); |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2747 | if (have_status_msg) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2748 | if (((int)strlen(status_buffer) - (have_status_msg - 1)) > |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2749 | (columns - 1) ) { |
| 2750 | have_status_msg = 0; |
| 2751 | Hit_Return(); |
| 2752 | } |
| 2753 | have_status_msg = 0; |
| 2754 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2755 | place_cursor(crow, ccol, FALSE); // put cursor back in correct place |
| 2756 | } |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 2757 | fflush_all(); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2758 | } |
| 2759 | |
| 2760 | //----- format the status buffer, the bottom line of screen ------ |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2761 | // format status buffer, with STANDOUT mode |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2762 | static void status_line_bold(const char *format, ...) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2763 | { |
| 2764 | va_list args; |
| 2765 | |
| 2766 | va_start(args, format); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2767 | strcpy(status_buffer, SOs); // Terminal standout mode on |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2768 | vsprintf(status_buffer + sizeof(SOs)-1, format, args); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2769 | strcat(status_buffer, SOn); // Terminal standout mode off |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2770 | va_end(args); |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2771 | |
| 2772 | have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2773 | } |
| 2774 | |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2775 | // format status buffer |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2776 | static void status_line(const char *format, ...) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2777 | { |
| 2778 | va_list args; |
| 2779 | |
| 2780 | va_start(args, format); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2781 | vsprintf(status_buffer, format, args); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2782 | va_end(args); |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2783 | |
| 2784 | have_status_msg = 1; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2785 | } |
| 2786 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2787 | // copy s to buf, convert unprintable |
| 2788 | static void print_literal(char *buf, const char *s) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2789 | { |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 2790 | char *d; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2791 | unsigned char c; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2792 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2793 | buf[0] = '\0'; |
| 2794 | if (!s[0]) |
| 2795 | s = "(NULL)"; |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 2796 | |
| 2797 | d = buf; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2798 | for (; *s; s++) { |
| 2799 | int c_is_no_print; |
| 2800 | |
| 2801 | c = *s; |
| 2802 | c_is_no_print = (c & 0x80) && !Isprint(c); |
| 2803 | if (c_is_no_print) { |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 2804 | strcpy(d, SOn); |
| 2805 | d += sizeof(SOn)-1; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2806 | c = '.'; |
| 2807 | } |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 2808 | if (c < ' ' || c == 0x7f) { |
| 2809 | *d++ = '^'; |
| 2810 | c |= '@'; /* 0x40 */ |
| 2811 | if (c == 0x7f) |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2812 | c = '?'; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2813 | } |
Denys Vlasenko | c270454 | 2009-11-20 19:14:19 +0100 | [diff] [blame] | 2814 | *d++ = c; |
| 2815 | *d = '\0'; |
| 2816 | if (c_is_no_print) { |
| 2817 | strcpy(d, SOs); |
| 2818 | d += sizeof(SOs)-1; |
| 2819 | } |
| 2820 | if (*s == '\n') { |
| 2821 | *d++ = '$'; |
| 2822 | *d = '\0'; |
| 2823 | } |
| 2824 | if (d - buf > MAX_INPUT_LEN - 10) // paranoia |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2825 | break; |
| 2826 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2827 | } |
| 2828 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2829 | static void not_implemented(const char *s) |
| 2830 | { |
| 2831 | char buf[MAX_INPUT_LEN]; |
| 2832 | |
| 2833 | print_literal(buf, s); |
| 2834 | status_line_bold("\'%s\' is not implemented", buf); |
| 2835 | } |
| 2836 | |
| 2837 | // show file status on status line |
| 2838 | static int format_edit_status(void) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2839 | { |
Denis Vlasenko | 6ca409e | 2007-08-12 20:58:27 +0000 | [diff] [blame] | 2840 | static const char cmd_mode_indicator[] ALIGN1 = "-IR-"; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2841 | |
| 2842 | #define tot format_edit_status__tot |
| 2843 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2844 | int cur, percent, ret, trunc_at; |
| 2845 | |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2846 | // file_modified is now a counter rather than a flag. this |
| 2847 | // helps reduce the amount of line counting we need to do. |
| 2848 | // (this will cause a mis-reporting of modified status |
| 2849 | // once every MAXINT editing operations.) |
| 2850 | |
| 2851 | // it would be nice to do a similar optimization here -- if |
| 2852 | // we haven't done a motion that could have changed which line |
| 2853 | // we're on, then we shouldn't have to do this count_lines() |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2854 | cur = count_lines(text, dot); |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2855 | |
| 2856 | // reduce counting -- the total lines can't have |
| 2857 | // changed if we haven't done any edits. |
| 2858 | if (file_modified != last_file_modified) { |
| 2859 | tot = cur + count_lines(dot, end - 1) - 1; |
| 2860 | last_file_modified = file_modified; |
| 2861 | } |
| 2862 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2863 | // current line percent |
| 2864 | // ------------- ~~ ---------- |
| 2865 | // total lines 100 |
| 2866 | if (tot > 0) { |
| 2867 | percent = (100 * cur) / tot; |
| 2868 | } else { |
| 2869 | cur = tot = 0; |
| 2870 | percent = 100; |
| 2871 | } |
Eric Andersen | 0ef24c6 | 2005-07-18 10:32:59 +0000 | [diff] [blame] | 2872 | |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2873 | trunc_at = columns < STATUS_BUFFER_LEN-1 ? |
| 2874 | columns : STATUS_BUFFER_LEN-1; |
| 2875 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2876 | ret = snprintf(status_buffer, trunc_at+1, |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2877 | #if ENABLE_FEATURE_VI_READONLY |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2878 | "%c %s%s%s %d/%d %d%%", |
| 2879 | #else |
| 2880 | "%c %s%s %d/%d %d%%", |
| 2881 | #endif |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2882 | cmd_mode_indicator[cmd_mode & 3], |
| 2883 | (current_filename != NULL ? current_filename : "No file"), |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 2884 | #if ENABLE_FEATURE_VI_READONLY |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2885 | (readonly_mode ? " [Readonly]" : ""), |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2886 | #endif |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 2887 | (file_modified ? " [Modified]" : ""), |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2888 | cur, tot, percent); |
| 2889 | |
| 2890 | if (ret >= 0 && ret < trunc_at) |
| 2891 | return ret; /* it all fit */ |
| 2892 | |
| 2893 | return trunc_at; /* had to truncate */ |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2894 | #undef tot |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2895 | } |
| 2896 | |
| 2897 | //----- Force refresh of all Lines ----------------------------- |
| 2898 | static void redraw(int full_screen) |
| 2899 | { |
| 2900 | place_cursor(0, 0, FALSE); // put cursor in correct place |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2901 | clear_to_eos(); // tell terminal to erase display |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2902 | screen_erase(); // erase the internal screen buffer |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2903 | last_status_cksum = 0; // force status update |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2904 | refresh(full_screen); // this will redraw the entire display |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 2905 | show_status_line(); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2906 | } |
| 2907 | |
| 2908 | //----- Format a text[] line into a buffer --------------------- |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 2909 | static char* format_line(char *src /*, int li*/) |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2910 | { |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 2911 | unsigned char c; |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2912 | int co; |
| 2913 | int ofs = offset; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2914 | char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2] |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 2915 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2916 | c = '~'; // char in col 0 in non-existent lines is '~' |
Denis Vlasenko | 4baed3a | 2007-12-22 17:31:29 +0000 | [diff] [blame] | 2917 | co = 0; |
| 2918 | while (co < columns + tabstop) { |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 2919 | // have we gone past the end? |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2920 | if (src < end) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2921 | c = *src++; |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2922 | if (c == '\n') |
| 2923 | break; |
| 2924 | if ((c & 0x80) && !Isprint(c)) { |
| 2925 | c = '.'; |
| 2926 | } |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 2927 | if (c < ' ' || c == 0x7f) { |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2928 | if (c == '\t') { |
| 2929 | c = ' '; |
| 2930 | // co % 8 != 7 |
| 2931 | while ((co % tabstop) != (tabstop - 1)) { |
| 2932 | dest[co++] = c; |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2933 | } |
| 2934 | } else { |
| 2935 | dest[co++] = '^'; |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2936 | if (c == 0x7f) |
| 2937 | c = '?'; |
| 2938 | else |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2939 | c += '@'; // Ctrl-X -> 'X' |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2940 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2941 | } |
| 2942 | } |
Denis Vlasenko | 4baed3a | 2007-12-22 17:31:29 +0000 | [diff] [blame] | 2943 | dest[co++] = c; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2944 | // discard scrolled-off-to-the-left portion, |
| 2945 | // in tabstop-sized pieces |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2946 | if (ofs >= tabstop && co >= tabstop) { |
Denis Vlasenko | 4baed3a | 2007-12-22 17:31:29 +0000 | [diff] [blame] | 2947 | memmove(dest, dest + tabstop, co); |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2948 | co -= tabstop; |
| 2949 | ofs -= tabstop; |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 2950 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2951 | if (src >= end) |
| 2952 | break; |
| 2953 | } |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2954 | // check "short line, gigantic offset" case |
| 2955 | if (co < ofs) |
Denis Vlasenko | 4baed3a | 2007-12-22 17:31:29 +0000 | [diff] [blame] | 2956 | ofs = co; |
| 2957 | // discard last scrolled off part |
| 2958 | co -= ofs; |
| 2959 | dest += ofs; |
| 2960 | // fill the rest with spaces |
| 2961 | if (co < columns) |
| 2962 | memset(&dest[co], ' ', columns - co); |
| 2963 | return dest; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
| 2966 | //----- Refresh the changed screen lines ----------------------- |
| 2967 | // Copy the source line from text[] into the buffer and note |
| 2968 | // if the current screenline is different from the new buffer. |
| 2969 | // If they differ then that line needs redrawing on the terminal. |
| 2970 | // |
| 2971 | static void refresh(int full_screen) |
| 2972 | { |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 2973 | #define old_offset refresh__old_offset |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2974 | |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2975 | int li, changed; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 2976 | char *tp, *sp; // pointer into text[] and screen[] |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2977 | |
Denys Vlasenko | c5fb0ad | 2010-07-21 12:39:42 +0200 | [diff] [blame] | 2978 | if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) { |
Denis Vlasenko | 5599502 | 2008-05-18 22:28:26 +0000 | [diff] [blame] | 2979 | unsigned c = columns, r = rows; |
Denys Vlasenko | 2bb651a | 2010-04-16 20:55:52 -0700 | [diff] [blame] | 2980 | query_screen_dimensions(); |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2981 | full_screen |= (c - columns) | (r - rows); |
| 2982 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2983 | sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot") |
| 2984 | tp = screenbegin; // index into text[] of top line |
| 2985 | |
| 2986 | // compare text[] to screen[] and mark screen[] lines that need updating |
| 2987 | for (li = 0; li < rows - 1; li++) { |
| 2988 | int cs, ce; // column start & end |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 2989 | char *out_buf; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 2990 | // format current text line |
Denis Vlasenko | 68404f1 | 2008-03-17 09:00:54 +0000 | [diff] [blame] | 2991 | out_buf = format_line(tp /*, li*/); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2992 | |
| 2993 | // skip to the end of the current text[] line |
Denis Vlasenko | f882f08 | 2007-12-23 02:36:51 +0000 | [diff] [blame] | 2994 | if (tp < end) { |
| 2995 | char *t = memchr(tp, '\n', end - tp); |
| 2996 | if (!t) t = end - 1; |
| 2997 | tp = t + 1; |
| 2998 | } |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 2999 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3000 | // see if there are any changes between vitual screen and out_buf |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3001 | changed = FALSE; // assume no change |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 3002 | cs = 0; |
| 3003 | ce = columns - 1; |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3004 | sp = &screen[li * columns]; // start of screen line |
| 3005 | if (full_screen) { |
| 3006 | // force re-draw of every single column from 0 - columns-1 |
| 3007 | goto re0; |
| 3008 | } |
| 3009 | // compare newly formatted buffer with virtual screen |
| 3010 | // look forward for first difference between buf and screen |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3011 | for (; cs <= ce; cs++) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3012 | if (out_buf[cs] != sp[cs]) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3013 | changed = TRUE; // mark for redraw |
| 3014 | break; |
| 3015 | } |
| 3016 | } |
| 3017 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3018 | // look backward for last difference between out_buf and screen |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 3019 | for (; ce >= cs; ce--) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3020 | if (out_buf[ce] != sp[ce]) { |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3021 | changed = TRUE; // mark for redraw |
| 3022 | break; |
| 3023 | } |
| 3024 | } |
| 3025 | // now, cs is index of first diff, and ce is index of last diff |
| 3026 | |
| 3027 | // if horz offset has changed, force a redraw |
| 3028 | if (offset != old_offset) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3029 | re0: |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3030 | changed = TRUE; |
| 3031 | } |
| 3032 | |
| 3033 | // make a sanity check of columns indexes |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 3034 | if (cs < 0) cs = 0; |
| 3035 | if (ce > columns - 1) ce = columns - 1; |
| 3036 | if (cs > ce) { cs = 0; ce = columns - 1; } |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3037 | // is there a change between vitual screen and out_buf |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3038 | if (changed) { |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 3039 | // copy changed part of buffer to virtual screen |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3040 | memcpy(sp+cs, out_buf+cs, ce-cs+1); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3041 | |
| 3042 | // move cursor to column of first change |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3043 | //if (offset != old_offset) { |
| 3044 | // // place_cursor is still too stupid |
| 3045 | // // to handle offsets correctly |
| 3046 | // place_cursor(li, cs, FALSE); |
| 3047 | //} else { |
| 3048 | place_cursor(li, cs, TRUE); |
| 3049 | //} |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3050 | |
| 3051 | // write line out to terminal |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3052 | fwrite(&sp[cs], ce - cs + 1, 1, stdout); |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3053 | } |
| 3054 | } |
| 3055 | |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3056 | place_cursor(crow, ccol, TRUE); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3057 | |
Denis Vlasenko | 26b6fba | 2007-12-21 21:34:37 +0000 | [diff] [blame] | 3058 | old_offset = offset; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 3059 | #undef old_offset |
Aaron Lehmann | 6fdacc7 | 2002-08-21 13:02:24 +0000 | [diff] [blame] | 3060 | } |
| 3061 | |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3062 | //--------------------------------------------------------------------- |
| 3063 | //----- the Ascii Chart ----------------------------------------------- |
| 3064 | // |
| 3065 | // 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel |
| 3066 | // 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si |
| 3067 | // 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb |
| 3068 | // 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us |
| 3069 | // 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 ' |
| 3070 | // 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f / |
| 3071 | // 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7 |
| 3072 | // 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ? |
| 3073 | // 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G |
| 3074 | // 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O |
| 3075 | // 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W |
| 3076 | // 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _ |
| 3077 | // 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g |
| 3078 | // 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o |
| 3079 | // 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w |
| 3080 | // 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del |
| 3081 | //--------------------------------------------------------------------- |
| 3082 | |
| 3083 | //----- Execute a Vi Command ----------------------------------- |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3084 | static void do_cmd(int c) |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3085 | { |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3086 | char *p, *q, *save_dot; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3087 | char buf[12]; |
Denis Vlasenko | c3a9dc8 | 2008-10-29 00:58:04 +0000 | [diff] [blame] | 3088 | int dir; |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 3089 | int cnt, i, j; |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3090 | int c1; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3091 | |
Denis Vlasenko | e3cbfb9 | 2007-12-22 17:00:11 +0000 | [diff] [blame] | 3092 | // c1 = c; // quiet the compiler |
| 3093 | // cnt = yf = 0; // quiet the compiler |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3094 | // p = q = save_dot = buf; // quiet the compiler |
| 3095 | memset(buf, '\0', sizeof(buf)); |
Eric Andersen | bff7a60 | 2001-11-17 07:15:43 +0000 | [diff] [blame] | 3096 | |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 3097 | show_status_line(); |
| 3098 | |
Eric Andersen | bff7a60 | 2001-11-17 07:15:43 +0000 | [diff] [blame] | 3099 | /* if this is a cursor key, skip these checks */ |
| 3100 | switch (c) { |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3101 | case KEYCODE_UP: |
| 3102 | case KEYCODE_DOWN: |
| 3103 | case KEYCODE_LEFT: |
| 3104 | case KEYCODE_RIGHT: |
| 3105 | case KEYCODE_HOME: |
| 3106 | case KEYCODE_END: |
| 3107 | case KEYCODE_PAGEUP: |
| 3108 | case KEYCODE_PAGEDOWN: |
| 3109 | case KEYCODE_DELETE: |
Eric Andersen | bff7a60 | 2001-11-17 07:15:43 +0000 | [diff] [blame] | 3110 | goto key_cmd_mode; |
| 3111 | } |
| 3112 | |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3113 | if (cmd_mode == 2) { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 3114 | // flip-flop Insert/Replace mode |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3115 | if (c == KEYCODE_INSERT) |
Denis Vlasenko | 2a51af2 | 2007-03-21 22:31:24 +0000 | [diff] [blame] | 3116 | goto dc_i; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3117 | // we are 'R'eplacing the current *dot with new char |
| 3118 | if (*dot == '\n') { |
| 3119 | // don't Replace past E-o-l |
| 3120 | cmd_mode = 1; // convert to insert |
| 3121 | } else { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 3122 | if (1 <= c || Isprint(c)) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3123 | if (c != 27) |
| 3124 | dot = yank_delete(dot, dot, 0, YANKDEL); // delete char |
| 3125 | dot = char_insert(dot, c); // insert new char |
| 3126 | } |
| 3127 | goto dc1; |
| 3128 | } |
| 3129 | } |
| 3130 | if (cmd_mode == 1) { |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3131 | // hitting "Insert" twice means "R" replace mode |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3132 | if (c == KEYCODE_INSERT) goto dc5; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3133 | // insert the char c at "dot" |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 3134 | if (1 <= c || Isprint(c)) { |
| 3135 | dot = char_insert(dot, c); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3136 | } |
| 3137 | goto dc1; |
| 3138 | } |
| 3139 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3140 | key_cmd_mode: |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3141 | switch (c) { |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3142 | //case 0x01: // soh |
| 3143 | //case 0x09: // ht |
| 3144 | //case 0x0b: // vt |
| 3145 | //case 0x0e: // so |
| 3146 | //case 0x0f: // si |
| 3147 | //case 0x10: // dle |
| 3148 | //case 0x11: // dc1 |
| 3149 | //case 0x13: // dc3 |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3150 | #if ENABLE_FEATURE_VI_CRASHME |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3151 | case 0x14: // dc4 ctrl-T |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3152 | crashme = (crashme == 0) ? 1 : 0; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3153 | break; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3154 | #endif |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3155 | //case 0x16: // syn |
| 3156 | //case 0x17: // etb |
| 3157 | //case 0x18: // can |
| 3158 | //case 0x1c: // fs |
| 3159 | //case 0x1d: // gs |
| 3160 | //case 0x1e: // rs |
| 3161 | //case 0x1f: // us |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3162 | //case '!': // !- |
| 3163 | //case '#': // #- |
| 3164 | //case '&': // &- |
| 3165 | //case '(': // (- |
| 3166 | //case ')': // )- |
| 3167 | //case '*': // *- |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3168 | //case '=': // =- |
| 3169 | //case '@': // @- |
| 3170 | //case 'F': // F- |
| 3171 | //case 'K': // K- |
| 3172 | //case 'Q': // Q- |
| 3173 | //case 'S': // S- |
| 3174 | //case 'T': // T- |
| 3175 | //case 'V': // V- |
| 3176 | //case '[': // [- |
| 3177 | //case '\\': // \- |
| 3178 | //case ']': // ]- |
| 3179 | //case '_': // _- |
| 3180 | //case '`': // `- |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3181 | //case 'u': // u- FIXME- there is no undo |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3182 | //case 'v': // v- |
Denys Vlasenko | b22bbff | 2009-07-04 16:50:43 +0200 | [diff] [blame] | 3183 | default: // unrecognized command |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3184 | buf[0] = c; |
| 3185 | buf[1] = '\0'; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3186 | not_implemented(buf); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3187 | end_cmd_q(); // stop adding to q |
| 3188 | case 0x00: // nul- ignore |
| 3189 | break; |
| 3190 | case 2: // ctrl-B scroll up full screen |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3191 | case KEYCODE_PAGEUP: // Cursor Key Page Up |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3192 | dot_scroll(rows - 2, -1); |
| 3193 | break; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3194 | case 4: // ctrl-D scroll down half screen |
| 3195 | dot_scroll((rows - 2) / 2, 1); |
| 3196 | break; |
| 3197 | case 5: // ctrl-E scroll down one line |
| 3198 | dot_scroll(1, 1); |
| 3199 | break; |
| 3200 | case 6: // ctrl-F scroll down full screen |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3201 | case KEYCODE_PAGEDOWN: // Cursor Key Page Down |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3202 | dot_scroll(rows - 2, 1); |
| 3203 | break; |
| 3204 | case 7: // ctrl-G show current status |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 3205 | last_status_cksum = 0; // force status update |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3206 | break; |
| 3207 | case 'h': // h- move left |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3208 | case KEYCODE_LEFT: // cursor key Left |
Paul Fox | d13b90b | 2005-07-18 22:17:25 +0000 | [diff] [blame] | 3209 | case 8: // ctrl-H- move left (This may be ERASE char) |
Denis Vlasenko | 2a51af2 | 2007-03-21 22:31:24 +0000 | [diff] [blame] | 3210 | case 0x7f: // DEL- move left (This may be ERASE char) |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3211 | do { |
| 3212 | dot_left(); |
| 3213 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3214 | break; |
| 3215 | case 10: // Newline ^J |
| 3216 | case 'j': // j- goto next line, same col |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3217 | case KEYCODE_DOWN: // cursor key Down |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3218 | do { |
| 3219 | dot_next(); // go to next B-o-l |
| 3220 | // try stay in same col |
| 3221 | dot = move_to_col(dot, ccol + offset); |
| 3222 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3223 | break; |
| 3224 | case 12: // ctrl-L force redraw whole screen |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3225 | case 18: // ctrl-R force redraw |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3226 | place_cursor(0, 0, FALSE); // put cursor in correct place |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3227 | clear_to_eos(); // tel terminal to erase display |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3228 | mysleep(10); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3229 | screen_erase(); // erase the internal screen buffer |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 3230 | last_status_cksum = 0; // force status update |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3231 | refresh(TRUE); // this will redraw the entire display |
| 3232 | break; |
| 3233 | case 13: // Carriage Return ^M |
| 3234 | case '+': // +- goto next line |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3235 | do { |
| 3236 | dot_next(); |
| 3237 | dot_skip_over_ws(); |
| 3238 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3239 | break; |
| 3240 | case 21: // ctrl-U scroll up half screen |
| 3241 | dot_scroll((rows - 2) / 2, -1); |
| 3242 | break; |
| 3243 | case 25: // ctrl-Y scroll up one line |
| 3244 | dot_scroll(1, -1); |
| 3245 | break; |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3246 | case 27: // esc |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3247 | if (cmd_mode == 0) |
| 3248 | indicate_error(c); |
| 3249 | cmd_mode = 0; // stop insrting |
| 3250 | end_cmd_q(); |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 3251 | last_status_cksum = 0; // force status update |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3252 | break; |
| 3253 | case ' ': // move right |
| 3254 | case 'l': // move right |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3255 | case KEYCODE_RIGHT: // Cursor Key Right |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3256 | do { |
| 3257 | dot_right(); |
| 3258 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3259 | break; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3260 | #if ENABLE_FEATURE_VI_YANKMARK |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3261 | case '"': // "- name a register to use for Delete/Yank |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3262 | c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower() |
| 3263 | if ((unsigned)c1 <= 25) { // a-z? |
| 3264 | YDreg = c1; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3265 | } else { |
| 3266 | indicate_error(c); |
| 3267 | } |
| 3268 | break; |
| 3269 | case '\'': // '- goto a specific mark |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3270 | c1 = (get_one_char() | 0x20) - 'a'; |
| 3271 | if ((unsigned)c1 <= 25) { // a-z? |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3272 | // get the b-o-l |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3273 | q = mark[c1]; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3274 | if (text <= q && q < end) { |
| 3275 | dot = q; |
| 3276 | dot_begin(); // go to B-o-l |
| 3277 | dot_skip_over_ws(); |
| 3278 | } |
| 3279 | } else if (c1 == '\'') { // goto previous context |
| 3280 | dot = swap_context(dot); // swap current and previous context |
| 3281 | dot_begin(); // go to B-o-l |
| 3282 | dot_skip_over_ws(); |
| 3283 | } else { |
| 3284 | indicate_error(c); |
| 3285 | } |
| 3286 | break; |
| 3287 | case 'm': // m- Mark a line |
| 3288 | // this is really stupid. If there are any inserts or deletes |
| 3289 | // between text[0] and dot then this mark will not point to the |
| 3290 | // correct location! It could be off by many lines! |
| 3291 | // Well..., at least its quick and dirty. |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3292 | c1 = (get_one_char() | 0x20) - 'a'; |
| 3293 | if ((unsigned)c1 <= 25) { // a-z? |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3294 | // remember the line |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3295 | mark[c1] = dot; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3296 | } else { |
| 3297 | indicate_error(c); |
| 3298 | } |
| 3299 | break; |
| 3300 | case 'P': // P- Put register before |
| 3301 | case 'p': // p- put register after |
| 3302 | p = reg[YDreg]; |
Denis Vlasenko | 00d8417 | 2008-11-24 07:34:42 +0000 | [diff] [blame] | 3303 | if (p == NULL) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3304 | status_line_bold("Nothing in register %c", what_reg()); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3305 | break; |
| 3306 | } |
| 3307 | // are we putting whole lines or strings |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3308 | if (strchr(p, '\n') != NULL) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3309 | if (c == 'P') { |
| 3310 | dot_begin(); // putting lines- Put above |
| 3311 | } |
| 3312 | if (c == 'p') { |
| 3313 | // are we putting after very last line? |
| 3314 | if (end_line(dot) == (end - 1)) { |
| 3315 | dot = end; // force dot to end of text[] |
| 3316 | } else { |
| 3317 | dot_next(); // next line, then put before |
| 3318 | } |
| 3319 | } |
| 3320 | } else { |
| 3321 | if (c == 'p') |
| 3322 | dot_right(); // move to right, can move to NL |
| 3323 | } |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 3324 | string_insert(dot, p); // insert the string |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3325 | end_cmd_q(); // stop adding to q |
| 3326 | break; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3327 | case 'U': // U- Undo; replace current line with original version |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3328 | if (reg[Ureg] != NULL) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3329 | p = begin_line(dot); |
| 3330 | q = end_line(dot); |
| 3331 | p = text_hole_delete(p, q); // delete cur line |
Denis Vlasenko | 4ae1e13 | 2008-11-19 13:25:14 +0000 | [diff] [blame] | 3332 | p += string_insert(p, reg[Ureg]); // insert orig line |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3333 | dot = p; |
| 3334 | dot_skip_over_ws(); |
| 3335 | } |
| 3336 | break; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3337 | #endif /* FEATURE_VI_YANKMARK */ |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3338 | case '$': // $- goto end of line |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3339 | case KEYCODE_END: // Cursor Key End |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3340 | for (;;) { |
| 3341 | dot = end_line(dot); |
| 3342 | if (--cmdcnt <= 0) |
| 3343 | break; |
Denys Vlasenko | 35fdb1b | 2010-03-26 16:10:14 +0100 | [diff] [blame] | 3344 | dot_next(); |
Denys Vlasenko | 35fdb1b | 2010-03-26 16:10:14 +0100 | [diff] [blame] | 3345 | } |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3346 | break; |
| 3347 | case '%': // %- find matching char of pair () [] {} |
| 3348 | for (q = dot; q < end && *q != '\n'; q++) { |
| 3349 | if (strchr("()[]{}", *q) != NULL) { |
| 3350 | // we found half of a pair |
| 3351 | p = find_pair(q, *q); |
| 3352 | if (p == NULL) { |
| 3353 | indicate_error(c); |
| 3354 | } else { |
| 3355 | dot = p; |
| 3356 | } |
| 3357 | break; |
| 3358 | } |
| 3359 | } |
| 3360 | if (*q == '\n') |
| 3361 | indicate_error(c); |
| 3362 | break; |
| 3363 | case 'f': // f- forward to a user specified char |
| 3364 | last_forward_char = get_one_char(); // get the search char |
| 3365 | // |
Eric Andersen | aff114c | 2004-04-14 17:51:38 +0000 | [diff] [blame] | 3366 | // dont separate these two commands. 'f' depends on ';' |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3367 | // |
Bernhard Reutner-Fischer | a985d30 | 2008-02-11 11:44:38 +0000 | [diff] [blame] | 3368 | //**** fall through to ... ';' |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3369 | case ';': // ;- look at rest of line for last forward char |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3370 | do { |
| 3371 | if (last_forward_char == 0) |
| 3372 | break; |
| 3373 | q = dot + 1; |
| 3374 | while (q < end - 1 && *q != '\n' && *q != last_forward_char) { |
| 3375 | q++; |
| 3376 | } |
| 3377 | if (*q == last_forward_char) |
| 3378 | dot = q; |
| 3379 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3380 | break; |
Paul Fox | b5ee8db | 2008-02-14 01:17:01 +0000 | [diff] [blame] | 3381 | case ',': // repeat latest 'f' in opposite direction |
Paul Fox | b5ee8db | 2008-02-14 01:17:01 +0000 | [diff] [blame] | 3382 | if (last_forward_char == 0) |
| 3383 | break; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3384 | do { |
| 3385 | q = dot - 1; |
| 3386 | while (q >= text && *q != '\n' && *q != last_forward_char) { |
| 3387 | q--; |
| 3388 | } |
| 3389 | if (q >= text && *q == last_forward_char) |
| 3390 | dot = q; |
| 3391 | } while (--cmdcnt > 0); |
Paul Fox | b5ee8db | 2008-02-14 01:17:01 +0000 | [diff] [blame] | 3392 | break; |
| 3393 | |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3394 | case '-': // -- goto prev line |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3395 | do { |
| 3396 | dot_prev(); |
| 3397 | dot_skip_over_ws(); |
| 3398 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3399 | break; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3400 | #if ENABLE_FEATURE_VI_DOT_CMD |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3401 | case '.': // .- repeat the last modifying command |
| 3402 | // Stuff the last_modifying_cmd back into stdin |
| 3403 | // and let it be re-executed. |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 3404 | if (lmc_len > 0) { |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 3405 | last_modifying_cmd[lmc_len] = 0; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3406 | ioq = ioq_start = xstrdup(last_modifying_cmd); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3407 | } |
| 3408 | break; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3409 | #endif |
| 3410 | #if ENABLE_FEATURE_VI_SEARCH |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3411 | case '?': // /- search for a pattern |
| 3412 | case '/': // /- search for a pattern |
| 3413 | buf[0] = c; |
| 3414 | buf[1] = '\0'; |
| 3415 | q = get_input_line(buf); // get input line- use "status line" |
Paul Fox | 4917c11 | 2008-03-05 16:44:02 +0000 | [diff] [blame] | 3416 | if (q[0] && !q[1]) { |
| 3417 | if (last_search_pattern[0]) |
Denis Vlasenko | c3a9dc8 | 2008-10-29 00:58:04 +0000 | [diff] [blame] | 3418 | last_search_pattern[0] = c; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3419 | goto dc3; // if no pat re-use old pat |
Paul Fox | 4917c11 | 2008-03-05 16:44:02 +0000 | [diff] [blame] | 3420 | } |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3421 | if (q[0]) { // strlen(q) > 1: new pat- save it and find |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3422 | // there is a new pat |
Aaron Lehmann | a170e1c | 2002-11-28 11:27:31 +0000 | [diff] [blame] | 3423 | free(last_search_pattern); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3424 | last_search_pattern = xstrdup(q); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3425 | goto dc3; // now find the pattern |
| 3426 | } |
| 3427 | // user changed mind and erased the "/"- do nothing |
| 3428 | break; |
| 3429 | case 'N': // N- backward search for last pattern |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3430 | dir = BACK; // assume BACKWARD search |
| 3431 | p = dot - 1; |
| 3432 | if (last_search_pattern[0] == '?') { |
| 3433 | dir = FORWARD; |
| 3434 | p = dot + 1; |
| 3435 | } |
| 3436 | goto dc4; // now search for pattern |
| 3437 | break; |
| 3438 | case 'n': // n- repeat search for last pattern |
| 3439 | // search rest of text[] starting at next char |
| 3440 | // if search fails return orignal "p" not the "p+1" address |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3441 | do { |
| 3442 | const char *msg; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3443 | dc3: |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3444 | dir = FORWARD; // assume FORWARD search |
| 3445 | p = dot + 1; |
| 3446 | if (last_search_pattern[0] == '?') { |
| 3447 | dir = BACK; |
| 3448 | p = dot - 1; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3449 | } |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3450 | dc4: |
| 3451 | q = char_search(p, last_search_pattern + 1, dir, FULL); |
| 3452 | if (q != NULL) { |
| 3453 | dot = q; // good search, update "dot" |
| 3454 | msg = NULL; |
| 3455 | goto dc2; |
| 3456 | } |
| 3457 | // no pattern found between "dot" and "end"- continue at top |
| 3458 | p = text; |
| 3459 | if (dir == BACK) { |
| 3460 | p = end - 1; |
| 3461 | } |
| 3462 | q = char_search(p, last_search_pattern + 1, dir, FULL); |
| 3463 | if (q != NULL) { // found something |
| 3464 | dot = q; // found new pattern- goto it |
| 3465 | msg = "search hit BOTTOM, continuing at TOP"; |
| 3466 | if (dir == BACK) { |
| 3467 | msg = "search hit TOP, continuing at BOTTOM"; |
| 3468 | } |
| 3469 | } else { |
| 3470 | msg = "Pattern not found"; |
| 3471 | } |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3472 | dc2: |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3473 | if (msg) |
| 3474 | status_line_bold("%s", msg); |
| 3475 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3476 | break; |
| 3477 | case '{': // {- move backward paragraph |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3478 | q = char_search(dot, "\n\n", BACK, FULL); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3479 | if (q != NULL) { // found blank line |
| 3480 | dot = next_line(q); // move to next blank line |
| 3481 | } |
| 3482 | break; |
| 3483 | case '}': // }- move forward paragraph |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3484 | q = char_search(dot, "\n\n", FORWARD, FULL); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3485 | if (q != NULL) { // found blank line |
| 3486 | dot = next_line(q); // move to next blank line |
| 3487 | } |
| 3488 | break; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3489 | #endif /* FEATURE_VI_SEARCH */ |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3490 | case '0': // 0- goto begining of line |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 3491 | case '1': // 1- |
| 3492 | case '2': // 2- |
| 3493 | case '3': // 3- |
| 3494 | case '4': // 4- |
| 3495 | case '5': // 5- |
| 3496 | case '6': // 6- |
| 3497 | case '7': // 7- |
| 3498 | case '8': // 8- |
| 3499 | case '9': // 9- |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3500 | if (c == '0' && cmdcnt < 1) { |
| 3501 | dot_begin(); // this was a standalone zero |
| 3502 | } else { |
| 3503 | cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number |
| 3504 | } |
| 3505 | break; |
| 3506 | case ':': // :- the colon mode commands |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3507 | p = get_input_line(":"); // get input line- use "status line" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3508 | #if ENABLE_FEATURE_VI_COLON |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3509 | colon(p); // execute the command |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3510 | #else |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3511 | if (*p == ':') |
| 3512 | p++; // move past the ':' |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3513 | cnt = strlen(p); |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3514 | if (cnt <= 0) |
| 3515 | break; |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 3516 | if (strncmp(p, "quit", cnt) == 0 |
| 3517 | || strncmp(p, "q!", cnt) == 0 // delete lines |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3518 | ) { |
Matt Kraai | 1f0c436 | 2001-12-20 23:13:26 +0000 | [diff] [blame] | 3519 | if (file_modified && p[1] != '!') { |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3520 | status_line_bold("No write since last change (:%s! overrides)", p); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3521 | } else { |
| 3522 | editing = 0; |
| 3523 | } |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 3524 | } else if (strncmp(p, "write", cnt) == 0 |
| 3525 | || strncmp(p, "wq", cnt) == 0 |
| 3526 | || strncmp(p, "wn", cnt) == 0 |
| 3527 | || (p[0] == 'x' && !p[1]) |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3528 | ) { |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 3529 | cnt = file_write(current_filename, text, end - 1); |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 3530 | if (cnt < 0) { |
| 3531 | if (cnt == -1) |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3532 | status_line_bold("Write error: %s", strerror(errno)); |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 3533 | } else { |
| 3534 | file_modified = 0; |
| 3535 | last_file_modified = -1; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3536 | status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3537 | if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n' |
| 3538 | || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N' |
| 3539 | ) { |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 3540 | editing = 0; |
| 3541 | } |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3542 | } |
Denys Vlasenko | 6548edd | 2009-06-15 12:44:11 +0200 | [diff] [blame] | 3543 | } else if (strncmp(p, "file", cnt) == 0) { |
Paul Fox | 8552aec | 2005-09-16 12:20:05 +0000 | [diff] [blame] | 3544 | last_status_cksum = 0; // force status update |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3545 | } else if (sscanf(p, "%d", &j) > 0) { |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3546 | dot = find_line(j); // go to line # j |
| 3547 | dot_skip_over_ws(); |
Denys Vlasenko | b22bbff | 2009-07-04 16:50:43 +0200 | [diff] [blame] | 3548 | } else { // unrecognized cmd |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3549 | not_implemented(p); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3550 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3551 | #endif /* !FEATURE_VI_COLON */ |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3552 | break; |
| 3553 | case '<': // <- Left shift something |
| 3554 | case '>': // >- Right shift something |
| 3555 | cnt = count_lines(text, dot); // remember what line we are on |
| 3556 | c1 = get_one_char(); // get the type of thing to delete |
| 3557 | find_range(&p, &q, c1); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3558 | yank_delete(p, q, 1, YANKONLY); // save copy before change |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3559 | p = begin_line(p); |
| 3560 | q = end_line(q); |
| 3561 | i = count_lines(p, q); // # of lines we are shifting |
| 3562 | for ( ; i > 0; i--, p = next_line(p)) { |
| 3563 | if (c == '<') { |
| 3564 | // shift left- remove tab or 8 spaces |
| 3565 | if (*p == '\t') { |
| 3566 | // shrink buffer 1 char |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3567 | text_hole_delete(p, p); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3568 | } else if (*p == ' ') { |
| 3569 | // we should be calculating columns, not just SPACE |
| 3570 | for (j = 0; *p == ' ' && j < tabstop; j++) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3571 | text_hole_delete(p, p); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3572 | } |
| 3573 | } |
| 3574 | } else if (c == '>') { |
| 3575 | // shift right -- add tab or 8 spaces |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3576 | char_insert(p, '\t'); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3577 | } |
| 3578 | } |
| 3579 | dot = find_line(cnt); // what line were we on |
| 3580 | dot_skip_over_ws(); |
| 3581 | end_cmd_q(); // stop adding to q |
| 3582 | break; |
| 3583 | case 'A': // A- append at e-o-l |
| 3584 | dot_end(); // go to e-o-l |
Bernhard Reutner-Fischer | a985d30 | 2008-02-11 11:44:38 +0000 | [diff] [blame] | 3585 | //**** fall through to ... 'a' |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3586 | case 'a': // a- append after current char |
| 3587 | if (*dot != '\n') |
| 3588 | dot++; |
| 3589 | goto dc_i; |
| 3590 | break; |
| 3591 | case 'B': // B- back a blank-delimited Word |
| 3592 | case 'E': // E- end of a blank-delimited word |
| 3593 | case 'W': // W- forward a blank-delimited word |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3594 | dir = FORWARD; |
| 3595 | if (c == 'B') |
| 3596 | dir = BACK; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3597 | do { |
| 3598 | if (c == 'W' || isspace(dot[dir])) { |
| 3599 | dot = skip_thing(dot, 1, dir, S_TO_WS); |
| 3600 | dot = skip_thing(dot, 2, dir, S_OVER_WS); |
| 3601 | } |
| 3602 | if (c != 'W') |
| 3603 | dot = skip_thing(dot, 1, dir, S_BEFORE_WS); |
| 3604 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3605 | break; |
| 3606 | case 'C': // C- Change to e-o-l |
| 3607 | case 'D': // D- delete to e-o-l |
| 3608 | save_dot = dot; |
| 3609 | dot = dollar_line(dot); // move to before NL |
| 3610 | // copy text into a register and delete |
| 3611 | dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l |
| 3612 | if (c == 'C') |
| 3613 | goto dc_i; // start inserting |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3614 | #if ENABLE_FEATURE_VI_DOT_CMD |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3615 | if (c == 'D') |
| 3616 | end_cmd_q(); // stop adding to q |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3617 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3618 | break; |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3619 | case 'g': // 'gg' goto a line number (vim) (default: very first line) |
Paul Fox | b5ee8db | 2008-02-14 01:17:01 +0000 | [diff] [blame] | 3620 | c1 = get_one_char(); |
| 3621 | if (c1 != 'g') { |
| 3622 | buf[0] = 'g'; |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3623 | buf[1] = c1; // TODO: if Unicode? |
Paul Fox | b5ee8db | 2008-02-14 01:17:01 +0000 | [diff] [blame] | 3624 | buf[2] = '\0'; |
| 3625 | not_implemented(buf); |
| 3626 | break; |
| 3627 | } |
| 3628 | if (cmdcnt == 0) |
| 3629 | cmdcnt = 1; |
| 3630 | /* fall through */ |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3631 | case 'G': // G- goto to a line number (default= E-O-F) |
| 3632 | dot = end - 1; // assume E-O-F |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3633 | if (cmdcnt > 0) { |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3634 | dot = find_line(cmdcnt); // what line is #cmdcnt |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3635 | } |
| 3636 | dot_skip_over_ws(); |
| 3637 | break; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3638 | case 'H': // H- goto top line on screen |
| 3639 | dot = screenbegin; |
| 3640 | if (cmdcnt > (rows - 1)) { |
| 3641 | cmdcnt = (rows - 1); |
| 3642 | } |
Denys Vlasenko | 35fdb1b | 2010-03-26 16:10:14 +0100 | [diff] [blame] | 3643 | if (--cmdcnt > 0) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3644 | do_cmd('+'); |
Denys Vlasenko | 35fdb1b | 2010-03-26 16:10:14 +0100 | [diff] [blame] | 3645 | } |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3646 | dot_skip_over_ws(); |
| 3647 | break; |
| 3648 | case 'I': // I- insert before first non-blank |
| 3649 | dot_begin(); // 0 |
| 3650 | dot_skip_over_ws(); |
Bernhard Reutner-Fischer | a985d30 | 2008-02-11 11:44:38 +0000 | [diff] [blame] | 3651 | //**** fall through to ... 'i' |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3652 | case 'i': // i- insert before current char |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3653 | case KEYCODE_INSERT: // Cursor Key Insert |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3654 | dc_i: |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3655 | cmd_mode = 1; // start inserting |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3656 | break; |
| 3657 | case 'J': // J- join current and next lines together |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3658 | do { |
| 3659 | dot_end(); // move to NL |
| 3660 | if (dot < end - 1) { // make sure not last char in text[] |
| 3661 | *dot++ = ' '; // replace NL with space |
| 3662 | file_modified++; |
| 3663 | while (isblank(*dot)) { // delete leading WS |
| 3664 | dot_delete(); |
| 3665 | } |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3666 | } |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3667 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3668 | end_cmd_q(); // stop adding to q |
| 3669 | break; |
| 3670 | case 'L': // L- goto bottom line on screen |
| 3671 | dot = end_screen(); |
| 3672 | if (cmdcnt > (rows - 1)) { |
| 3673 | cmdcnt = (rows - 1); |
| 3674 | } |
Denys Vlasenko | 35fdb1b | 2010-03-26 16:10:14 +0100 | [diff] [blame] | 3675 | if (--cmdcnt > 0) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3676 | do_cmd('-'); |
Denys Vlasenko | 35fdb1b | 2010-03-26 16:10:14 +0100 | [diff] [blame] | 3677 | } |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3678 | dot_begin(); |
| 3679 | dot_skip_over_ws(); |
| 3680 | break; |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3681 | case 'M': // M- goto middle line on screen |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3682 | dot = screenbegin; |
| 3683 | for (cnt = 0; cnt < (rows-1) / 2; cnt++) |
| 3684 | dot = next_line(dot); |
| 3685 | break; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3686 | case 'O': // O- open a empty line above |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3687 | // 0i\n ESC -i |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3688 | p = begin_line(dot); |
| 3689 | if (p[-1] == '\n') { |
| 3690 | dot_prev(); |
| 3691 | case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..." |
| 3692 | dot_end(); |
| 3693 | dot = char_insert(dot, '\n'); |
| 3694 | } else { |
| 3695 | dot_begin(); // 0 |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3696 | dot = char_insert(dot, '\n'); // i\n ESC |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3697 | dot_prev(); // - |
| 3698 | } |
| 3699 | goto dc_i; |
| 3700 | break; |
| 3701 | case 'R': // R- continuous Replace char |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3702 | dc5: |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3703 | cmd_mode = 2; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3704 | break; |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3705 | case KEYCODE_DELETE: |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3706 | c = 'x'; |
| 3707 | // fall through |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3708 | case 'X': // X- delete char before dot |
| 3709 | case 'x': // x- delete the current char |
| 3710 | case 's': // s- substitute the current char |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3711 | dir = 0; |
| 3712 | if (c == 'X') |
| 3713 | dir = -1; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3714 | do { |
| 3715 | if (dot[dir] != '\n') { |
| 3716 | if (c == 'X') |
| 3717 | dot--; // delete prev char |
| 3718 | dot = yank_delete(dot, dot, 0, YANKDEL); // delete char |
| 3719 | } |
| 3720 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3721 | end_cmd_q(); // stop adding to q |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3722 | if (c == 's') |
| 3723 | goto dc_i; // start inserting |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3724 | break; |
| 3725 | case 'Z': // Z- if modified, {write}; exit |
| 3726 | // ZZ means to save file (if necessary), then exit |
| 3727 | c1 = get_one_char(); |
| 3728 | if (c1 != 'Z') { |
| 3729 | indicate_error(c); |
| 3730 | break; |
| 3731 | } |
Paul Fox | f0305b7 | 2006-03-28 14:18:21 +0000 | [diff] [blame] | 3732 | if (file_modified) { |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 3733 | if (ENABLE_FEATURE_VI_READONLY && readonly_mode) { |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3734 | status_line_bold("\"%s\" File is read only", current_filename); |
Denis Vlasenko | 9275814 | 2006-10-03 19:56:34 +0000 | [diff] [blame] | 3735 | break; |
Paul Fox | f0305b7 | 2006-03-28 14:18:21 +0000 | [diff] [blame] | 3736 | } |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 3737 | cnt = file_write(current_filename, text, end - 1); |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 3738 | if (cnt < 0) { |
| 3739 | if (cnt == -1) |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3740 | status_line_bold("Write error: %s", strerror(errno)); |
Paul Fox | 61e45db | 2005-10-09 14:43:22 +0000 | [diff] [blame] | 3741 | } else if (cnt == (end - 1 - text + 1)) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3742 | editing = 0; |
| 3743 | } |
| 3744 | } else { |
| 3745 | editing = 0; |
| 3746 | } |
| 3747 | break; |
| 3748 | case '^': // ^- move to first non-blank on line |
| 3749 | dot_begin(); |
| 3750 | dot_skip_over_ws(); |
| 3751 | break; |
| 3752 | case 'b': // b- back a word |
| 3753 | case 'e': // e- end of word |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3754 | dir = FORWARD; |
| 3755 | if (c == 'b') |
| 3756 | dir = BACK; |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3757 | do { |
| 3758 | if ((dot + dir) < text || (dot + dir) > end - 1) |
| 3759 | break; |
| 3760 | dot += dir; |
| 3761 | if (isspace(*dot)) { |
| 3762 | dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS); |
| 3763 | } |
| 3764 | if (isalnum(*dot) || *dot == '_') { |
| 3765 | dot = skip_thing(dot, 1, dir, S_END_ALNUM); |
| 3766 | } else if (ispunct(*dot)) { |
| 3767 | dot = skip_thing(dot, 1, dir, S_END_PUNCT); |
| 3768 | } |
| 3769 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3770 | break; |
| 3771 | case 'c': // c- change something |
| 3772 | case 'd': // d- delete something |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3773 | #if ENABLE_FEATURE_VI_YANKMARK |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3774 | case 'y': // y- yank something |
| 3775 | case 'Y': // Y- Yank a line |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3776 | #endif |
Denis Vlasenko | d44c153 | 2008-10-14 12:59:42 +0000 | [diff] [blame] | 3777 | { |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 3778 | int yf, ml, whole = 0; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3779 | yf = YANKDEL; // assume either "c" or "d" |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3780 | #if ENABLE_FEATURE_VI_YANKMARK |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3781 | if (c == 'y' || c == 'Y') |
| 3782 | yf = YANKONLY; |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3783 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3784 | c1 = 'y'; |
| 3785 | if (c != 'Y') |
| 3786 | c1 = get_one_char(); // get the type of thing to delete |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 3787 | // determine range, and whether it spans lines |
| 3788 | ml = find_range(&p, &q, c1); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3789 | if (c1 == 27) { // ESC- user changed mind and wants out |
| 3790 | c = c1 = 27; // Escape- do nothing |
| 3791 | } else if (strchr("wW", c1)) { |
| 3792 | if (c == 'c') { |
| 3793 | // don't include trailing WS as part of word |
Denis Vlasenko | eaabf06 | 2007-07-17 23:14:07 +0000 | [diff] [blame] | 3794 | while (isblank(*q)) { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3795 | if (q <= text || q[-1] == '\n') |
| 3796 | break; |
| 3797 | q--; |
| 3798 | } |
| 3799 | } |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 3800 | dot = yank_delete(p, q, ml, yf); // delete word |
| 3801 | } else if (strchr("^0bBeEft%$ lh\b\177", c1)) { |
| 3802 | // partial line copy text into a register and delete |
| 3803 | dot = yank_delete(p, q, ml, yf); // delete word |
| 3804 | } else if (strchr("cdykjHL+-{}\r\n", c1)) { |
| 3805 | // whole line copy text into a register and delete |
| 3806 | dot = yank_delete(p, q, ml, yf); // delete lines |
| 3807 | whole = 1; |
| 3808 | } else { |
| 3809 | // could not recognize object |
| 3810 | c = c1 = 27; // error- |
| 3811 | ml = 0; |
| 3812 | indicate_error(c); |
| 3813 | } |
| 3814 | if (ml && whole) { |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3815 | if (c == 'c') { |
| 3816 | dot = char_insert(dot, '\n'); |
| 3817 | // on the last line of file don't move to prev line |
Paul Fox | c51fc7b | 2008-03-06 01:34:23 +0000 | [diff] [blame] | 3818 | if (whole && dot != (end-1)) { |
Eric Andersen | 1c0d311 | 2001-04-16 15:46:44 +0000 | [diff] [blame] | 3819 | dot_prev(); |
| 3820 | } |
| 3821 | } else if (c == 'd') { |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3822 | dot_begin(); |
| 3823 | dot_skip_over_ws(); |
| 3824 | } |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3825 | } |
| 3826 | if (c1 != 27) { |
| 3827 | // if CHANGING, not deleting, start inserting after the delete |
| 3828 | if (c == 'c') { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3829 | strcpy(buf, "Change"); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3830 | goto dc_i; // start inserting |
| 3831 | } |
| 3832 | if (c == 'd') { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3833 | strcpy(buf, "Delete"); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3834 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3835 | #if ENABLE_FEATURE_VI_YANKMARK |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3836 | if (c == 'y' || c == 'Y') { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3837 | strcpy(buf, "Yank"); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3838 | } |
| 3839 | p = reg[YDreg]; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3840 | q = p + strlen(p); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3841 | for (cnt = 0; p <= q; p++) { |
| 3842 | if (*p == '\n') |
| 3843 | cnt++; |
| 3844 | } |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3845 | status_line("%s %d lines (%d chars) using [%c]", |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3846 | buf, cnt, strlen(reg[YDreg]), what_reg()); |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3847 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3848 | end_cmd_q(); // stop adding to q |
| 3849 | } |
| 3850 | break; |
Denis Vlasenko | d44c153 | 2008-10-14 12:59:42 +0000 | [diff] [blame] | 3851 | } |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3852 | case 'k': // k- goto prev line, same col |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3853 | case KEYCODE_UP: // cursor key Up |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3854 | do { |
| 3855 | dot_prev(); |
| 3856 | dot = move_to_col(dot, ccol + offset); // try stay in same col |
| 3857 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3858 | break; |
| 3859 | case 'r': // r- replace the current char with user input |
| 3860 | c1 = get_one_char(); // get the replacement char |
| 3861 | if (*dot != '\n') { |
| 3862 | *dot = c1; |
Denis Vlasenko | b175946 | 2008-06-20 20:20:54 +0000 | [diff] [blame] | 3863 | file_modified++; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3864 | } |
| 3865 | end_cmd_q(); // stop adding to q |
| 3866 | break; |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3867 | case 't': // t- move to char prior to next x |
Tim Riker | c1ef7bd | 2006-01-25 00:08:53 +0000 | [diff] [blame] | 3868 | last_forward_char = get_one_char(); |
| 3869 | do_cmd(';'); |
| 3870 | if (*dot == last_forward_char) |
| 3871 | dot_left(); |
Denis Vlasenko | 4c9e9c4 | 2008-10-20 08:59:03 +0000 | [diff] [blame] | 3872 | last_forward_char = 0; |
Eric Andersen | 822c383 | 2001-05-07 17:37:43 +0000 | [diff] [blame] | 3873 | break; |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3874 | case 'w': // w- forward a word |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3875 | do { |
| 3876 | if (isalnum(*dot) || *dot == '_') { // we are on ALNUM |
| 3877 | dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM); |
| 3878 | } else if (ispunct(*dot)) { // we are on PUNCT |
| 3879 | dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT); |
| 3880 | } |
| 3881 | if (dot < end - 1) |
| 3882 | dot++; // move over word |
| 3883 | if (isspace(*dot)) { |
| 3884 | dot = skip_thing(dot, 2, FORWARD, S_OVER_WS); |
| 3885 | } |
| 3886 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3887 | break; |
| 3888 | case 'z': // z- |
| 3889 | c1 = get_one_char(); // get the replacement char |
| 3890 | cnt = 0; |
| 3891 | if (c1 == '.') |
| 3892 | cnt = (rows - 2) / 2; // put dot at center |
| 3893 | if (c1 == '-') |
| 3894 | cnt = rows - 2; // put dot at bottom |
| 3895 | screenbegin = begin_line(dot); // start dot at top |
| 3896 | dot_scroll(cnt, -1); |
| 3897 | break; |
| 3898 | case '|': // |- move to column "cmdcnt" |
| 3899 | dot = move_to_col(dot, cmdcnt - 1); // try to move to column |
| 3900 | break; |
| 3901 | case '~': // ~- flip the case of letters a-z -> A-Z |
Tanguy Pruvot | 8a6c2c2 | 2012-04-28 00:24:09 +0200 | [diff] [blame] | 3902 | do { |
| 3903 | if (islower(*dot)) { |
| 3904 | *dot = toupper(*dot); |
| 3905 | file_modified++; |
| 3906 | } else if (isupper(*dot)) { |
| 3907 | *dot = tolower(*dot); |
| 3908 | file_modified++; |
| 3909 | } |
| 3910 | dot_right(); |
| 3911 | } while (--cmdcnt > 0); |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3912 | end_cmd_q(); // stop adding to q |
| 3913 | break; |
| 3914 | //----- The Cursor and Function Keys ----------------------------- |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3915 | case KEYCODE_HOME: // Cursor Key Home |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3916 | dot_begin(); |
| 3917 | break; |
| 3918 | // The Fn keys could point to do_macro which could translate them |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3919 | #if 0 |
| 3920 | case KEYCODE_FUN1: // Function Key F1 |
| 3921 | case KEYCODE_FUN2: // Function Key F2 |
| 3922 | case KEYCODE_FUN3: // Function Key F3 |
| 3923 | case KEYCODE_FUN4: // Function Key F4 |
| 3924 | case KEYCODE_FUN5: // Function Key F5 |
| 3925 | case KEYCODE_FUN6: // Function Key F6 |
| 3926 | case KEYCODE_FUN7: // Function Key F7 |
| 3927 | case KEYCODE_FUN8: // Function Key F8 |
| 3928 | case KEYCODE_FUN9: // Function Key F9 |
| 3929 | case KEYCODE_FUN10: // Function Key F10 |
| 3930 | case KEYCODE_FUN11: // Function Key F11 |
| 3931 | case KEYCODE_FUN12: // Function Key F12 |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3932 | break; |
Denis Vlasenko | 0112ff5 | 2008-10-25 23:23:00 +0000 | [diff] [blame] | 3933 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3934 | } |
| 3935 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3936 | dc1: |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3937 | // if text[] just became empty, add back an empty line |
| 3938 | if (end == text) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3939 | char_insert(text, '\n'); // start empty buf with dummy line |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3940 | dot = text; |
| 3941 | } |
| 3942 | // it is OK for dot to exactly equal to end, otherwise check dot validity |
| 3943 | if (dot != end) { |
| 3944 | dot = bound_dot(dot); // make sure "dot" is valid |
| 3945 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3946 | #if ENABLE_FEATURE_VI_YANKMARK |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3947 | check_context(c); // update the current context |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3948 | #endif |
Eric Andersen | 3f98040 | 2001-04-04 17:31:15 +0000 | [diff] [blame] | 3949 | |
| 3950 | if (!isdigit(c)) |
| 3951 | cmdcnt = 0; // cmd was not a number, reset cmdcnt |
| 3952 | cnt = dot - begin_line(dot); |
| 3953 | // Try to stay off of the Newline |
| 3954 | if (*dot == '\n' && cnt > 0 && cmd_mode == 0) |
| 3955 | dot--; |
| 3956 | } |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 3957 | |
Paul Fox | 35e9c5d | 2008-03-06 16:26:12 +0000 | [diff] [blame] | 3958 | /* NB! the CRASHME code is unmaintained, and doesn't currently build */ |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 3959 | #if ENABLE_FEATURE_VI_CRASHME |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 3960 | static int totalcmds = 0; |
| 3961 | static int Mp = 85; // Movement command Probability |
| 3962 | static int Np = 90; // Non-movement command Probability |
| 3963 | static int Dp = 96; // Delete command Probability |
| 3964 | static int Ip = 97; // Insert command Probability |
| 3965 | static int Yp = 98; // Yank command Probability |
| 3966 | static int Pp = 99; // Put command Probability |
| 3967 | static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3968 | static const char chars[20] = "\t012345 abcdABCD-=.$"; |
| 3969 | static const char *const words[20] = { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 3970 | "this", "is", "a", "test", |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 3971 | "broadcast", "the", "emergency", "of", |
| 3972 | "system", "quick", "brown", "fox", |
| 3973 | "jumped", "over", "lazy", "dogs", |
| 3974 | "back", "January", "Febuary", "March" |
| 3975 | }; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3976 | static const char *const lines[20] = { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 3977 | "You should have received a copy of the GNU General Public License\n", |
| 3978 | "char c, cm, *cmd, *cmd1;\n", |
| 3979 | "generate a command by percentages\n", |
| 3980 | "Numbers may be typed as a prefix to some commands.\n", |
| 3981 | "Quit, discarding changes!\n", |
| 3982 | "Forced write, if permission originally not valid.\n", |
| 3983 | "In general, any ex or ed command (such as substitute or delete).\n", |
| 3984 | "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n", |
| 3985 | "Please get w/ me and I will go over it with you.\n", |
| 3986 | "The following is a list of scheduled, committed changes.\n", |
| 3987 | "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n", |
| 3988 | "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n", |
| 3989 | "Any question about transactions please contact Sterling Huxley.\n", |
| 3990 | "I will try to get back to you by Friday, December 31.\n", |
| 3991 | "This Change will be implemented on Friday.\n", |
| 3992 | "Let me know if you have problems accessing this;\n", |
| 3993 | "Sterling Huxley recently added you to the access list.\n", |
| 3994 | "Would you like to go to lunch?\n", |
| 3995 | "The last command will be automatically run.\n", |
| 3996 | "This is too much english for a computer geek.\n", |
| 3997 | }; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 3998 | static char *multilines[20] = { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 3999 | "You should have received a copy of the GNU General Public License\n", |
| 4000 | "char c, cm, *cmd, *cmd1;\n", |
| 4001 | "generate a command by percentages\n", |
| 4002 | "Numbers may be typed as a prefix to some commands.\n", |
| 4003 | "Quit, discarding changes!\n", |
| 4004 | "Forced write, if permission originally not valid.\n", |
| 4005 | "In general, any ex or ed command (such as substitute or delete).\n", |
| 4006 | "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n", |
| 4007 | "Please get w/ me and I will go over it with you.\n", |
| 4008 | "The following is a list of scheduled, committed changes.\n", |
| 4009 | "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n", |
| 4010 | "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n", |
| 4011 | "Any question about transactions please contact Sterling Huxley.\n", |
| 4012 | "I will try to get back to you by Friday, December 31.\n", |
| 4013 | "This Change will be implemented on Friday.\n", |
| 4014 | "Let me know if you have problems accessing this;\n", |
| 4015 | "Sterling Huxley recently added you to the access list.\n", |
| 4016 | "Would you like to go to lunch?\n", |
| 4017 | "The last command will be automatically run.\n", |
| 4018 | "This is too much english for a computer geek.\n", |
| 4019 | }; |
| 4020 | |
| 4021 | // create a random command to execute |
| 4022 | static void crash_dummy() |
| 4023 | { |
| 4024 | static int sleeptime; // how long to pause between commands |
| 4025 | char c, cm, *cmd, *cmd1; |
| 4026 | int i, cnt, thing, rbi, startrbi, percent; |
| 4027 | |
| 4028 | // "dot" movement commands |
| 4029 | cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL"; |
| 4030 | |
| 4031 | // is there already a command running? |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 4032 | if (readbuffer[0] > 0) |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4033 | goto cd1; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4034 | cd0: |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 4035 | readbuffer[0] = 'X'; |
| 4036 | startrbi = rbi = 1; |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4037 | sleeptime = 0; // how long to pause between commands |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 4038 | memset(readbuffer, '\0', sizeof(readbuffer)); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4039 | // generate a command by percentages |
| 4040 | percent = (int) lrand48() % 100; // get a number from 0-99 |
| 4041 | if (percent < Mp) { // Movement commands |
| 4042 | // available commands |
| 4043 | cmd = cmd1; |
| 4044 | M++; |
| 4045 | } else if (percent < Np) { // non-movement commands |
| 4046 | cmd = "mz<>\'\""; // available commands |
| 4047 | N++; |
| 4048 | } else if (percent < Dp) { // Delete commands |
| 4049 | cmd = "dx"; // available commands |
| 4050 | D++; |
| 4051 | } else if (percent < Ip) { // Inset commands |
| 4052 | cmd = "iIaAsrJ"; // available commands |
| 4053 | I++; |
| 4054 | } else if (percent < Yp) { // Yank commands |
| 4055 | cmd = "yY"; // available commands |
| 4056 | Y++; |
| 4057 | } else if (percent < Pp) { // Put commands |
| 4058 | cmd = "pP"; // available commands |
| 4059 | P++; |
| 4060 | } else { |
| 4061 | // We do not know how to handle this command, try again |
| 4062 | U++; |
| 4063 | goto cd0; |
| 4064 | } |
| 4065 | // randomly pick one of the available cmds from "cmd[]" |
| 4066 | i = (int) lrand48() % strlen(cmd); |
| 4067 | cm = cmd[i]; |
| 4068 | if (strchr(":\024", cm)) |
| 4069 | goto cd0; // dont allow colon or ctrl-T commands |
| 4070 | readbuffer[rbi++] = cm; // put cmd into input buffer |
| 4071 | |
| 4072 | // now we have the command- |
| 4073 | // there are 1, 2, and multi char commands |
| 4074 | // find out which and generate the rest of command as necessary |
| 4075 | if (strchr("dmryz<>\'\"", cm)) { // 2-char commands |
| 4076 | cmd1 = " \n\r0$^-+wWeEbBhjklHL"; |
| 4077 | if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[] |
| 4078 | cmd1 = "abcdefghijklmnopqrstuvwxyz"; |
| 4079 | } |
| 4080 | thing = (int) lrand48() % strlen(cmd1); // pick a movement command |
| 4081 | c = cmd1[thing]; |
| 4082 | readbuffer[rbi++] = c; // add movement to input buffer |
| 4083 | } |
| 4084 | if (strchr("iIaAsc", cm)) { // multi-char commands |
| 4085 | if (cm == 'c') { |
| 4086 | // change some thing |
| 4087 | thing = (int) lrand48() % strlen(cmd1); // pick a movement command |
| 4088 | c = cmd1[thing]; |
| 4089 | readbuffer[rbi++] = c; // add movement to input buffer |
| 4090 | } |
| 4091 | thing = (int) lrand48() % 4; // what thing to insert |
| 4092 | cnt = (int) lrand48() % 10; // how many to insert |
| 4093 | for (i = 0; i < cnt; i++) { |
| 4094 | if (thing == 0) { // insert chars |
| 4095 | readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))]; |
| 4096 | } else if (thing == 1) { // insert words |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4097 | strcat(readbuffer, words[(int) lrand48() % 20]); |
| 4098 | strcat(readbuffer, " "); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4099 | sleeptime = 0; // how fast to type |
| 4100 | } else if (thing == 2) { // insert lines |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4101 | strcat(readbuffer, lines[(int) lrand48() % 20]); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4102 | sleeptime = 0; // how fast to type |
| 4103 | } else { // insert multi-lines |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4104 | strcat(readbuffer, multilines[(int) lrand48() % 20]); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4105 | sleeptime = 0; // how fast to type |
| 4106 | } |
| 4107 | } |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4108 | strcat(readbuffer, "\033"); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4109 | } |
Denys Vlasenko | 020f406 | 2009-05-17 16:44:54 +0200 | [diff] [blame] | 4110 | readbuffer[0] = strlen(readbuffer + 1); |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4111 | cd1: |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4112 | totalcmds++; |
| 4113 | if (sleeptime > 0) |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4114 | mysleep(sleeptime); // sleep 1/100 sec |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4115 | } |
| 4116 | |
| 4117 | // test to see if there are any errors |
| 4118 | static void crash_test() |
| 4119 | { |
| 4120 | static time_t oldtim; |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4121 | |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4122 | time_t tim; |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 4123 | char d[2], msg[80]; |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4124 | |
| 4125 | msg[0] = '\0'; |
| 4126 | if (end < text) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4127 | strcat(msg, "end<text "); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4128 | } |
| 4129 | if (end > textend) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4130 | strcat(msg, "end>textend "); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4131 | } |
| 4132 | if (dot < text) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4133 | strcat(msg, "dot<text "); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4134 | } |
| 4135 | if (dot > end) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4136 | strcat(msg, "dot>end "); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4137 | } |
| 4138 | if (screenbegin < text) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4139 | strcat(msg, "screenbegin<text "); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4140 | } |
| 4141 | if (screenbegin > end - 1) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4142 | strcat(msg, "screenbegin>end-1 "); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4143 | } |
| 4144 | |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4145 | if (msg[0]) { |
Glenn L McGrath | 7127b58 | 2002-12-03 21:48:15 +0000 | [diff] [blame] | 4146 | printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s", |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4147 | totalcmds, last_input_char, msg, SOs, SOn); |
Denys Vlasenko | 8131eea | 2009-11-02 14:19:51 +0100 | [diff] [blame] | 4148 | fflush_all(); |
Bernhard Reutner-Fischer | 5e25ddb | 2008-05-19 09:48:17 +0000 | [diff] [blame] | 4149 | while (safe_read(STDIN_FILENO, d, 1) > 0) { |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4150 | if (d[0] == '\n' || d[0] == '\r') |
| 4151 | break; |
| 4152 | } |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4153 | } |
Denis Vlasenko | 88adfcd | 2007-12-22 15:40:13 +0000 | [diff] [blame] | 4154 | tim = time(NULL); |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4155 | if (tim >= (oldtim + 3)) { |
Denis Vlasenko | afa37cf | 2007-03-21 00:05:35 +0000 | [diff] [blame] | 4156 | sprintf(status_buffer, |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4157 | "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d", |
| 4158 | totalcmds, M, N, I, D, Y, P, U, end - text + 1); |
| 4159 | oldtim = tim; |
| 4160 | } |
Glenn L McGrath | 09adaca | 2002-12-02 21:18:10 +0000 | [diff] [blame] | 4161 | } |
Denis Vlasenko | 6a5dc5d | 2006-12-30 18:42:29 +0000 | [diff] [blame] | 4162 | #endif |