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