blob: 92c069de05931755c7e25a1656381374ef36c508 [file] [log] [blame]
Rob Landleye5e1a102006-06-21 01:15:36 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3f980402001-04-04 17:31:15 +00002/*
3 * tiny vi.c: A small 'vi' clone
4 * Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com>
5 *
Paul Foxdbf935d2006-03-27 20:29:33 +00006 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen3f980402001-04-04 17:31:15 +00007 */
8
Eric Andersen3f980402001-04-04 17:31:15 +00009/*
Eric Andersen3f980402001-04-04 17:31:15 +000010 * Things To Do:
11 * EXINIT
Eric Andersen1c0d3112001-04-16 15:46:44 +000012 * $HOME/.exrc and ./.exrc
Eric Andersen3f980402001-04-04 17:31:15 +000013 * add magic to search /foo.*bar
14 * add :help command
15 * :map macros
Eric Andersen3f980402001-04-04 17:31:15 +000016 * if mark[] values were line numbers rather than pointers
17 * it would be easier to change the mark when add/delete lines
Eric Andersen1c0d3112001-04-16 15:46:44 +000018 * 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 Andersen3f980402001-04-04 17:31:15 +000022 */
23
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Eric Andersen3f980402001-04-04 17:31:15 +000025
Paul Fox35e9c5d2008-03-06 16:26:12 +000026/* the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +000027#define ENABLE_FEATURE_VI_CRASHME 0
28
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000029
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +000030#if ENABLE_LOCALE_SUPPORT
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000031
32#if ENABLE_FEATURE_VI_8BIT
33#define Isprint(c) isprint(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +000034#else
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000035#define Isprint(c) (isprint(c) && (unsigned char)(c) < 0x7f)
Glenn L McGrath09adaca2002-12-02 21:18:10 +000036#endif
37
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000038#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 Vlasenkoe8a07882007-06-10 15:08:44 +000050enum {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +000051 MAX_TABSTOP = 32, // sanity limit
Denis Vlasenko88adfcd2007-12-22 15:40:13 +000052 // 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 Vlasenkoe8a07882007-06-10 15:08:44 +000058};
Eric Andersen3f980402001-04-04 17:31:15 +000059
Glenn L McGrath09adaca2002-12-02 21:18:10 +000060/* vt102 typical ESC sequence */
61/* terminal standout start/normal ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000062static const char SOs[] ALIGN1 = "\033[7m";
63static const char SOn[] ALIGN1 = "\033[0m";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000064/* terminal bell sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000065static const char bell[] ALIGN1 = "\007";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000066/* Clear-end-of-line and Clear-end-of-screen ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000067static const char Ceol[] ALIGN1 = "\033[0K";
68static const char Ceos[] ALIGN1 = "\033[0J";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000069/* Cursor motion arbitrary destination ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000070static const char CMrc[] ALIGN1 = "\033[%d;%dH";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000071/* Cursor motion up and down ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000072static const char CMup[] ALIGN1 = "\033[A";
73static const char CMdown[] ALIGN1 = "\n";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000074
Denis Vlasenkoded6ad32008-10-14 12:26:30 +000075#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
79static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~";
80#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +000081
Rob Landleybc68cd12006-03-10 19:22:06 +000082enum {
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 Andersen3f980402001-04-04 17:31:15 +000089
Rob Landleybc68cd12006-03-10 19:22:06 +000090 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 Vlasenko8e858e22007-03-07 09:35:43 +000094 S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
Rob Landleybc68cd12006-03-10 19:22:06 +000095};
Eric Andersen3f980402001-04-04 17:31:15 +000096
Denis Vlasenkob1759462008-06-20 20:20:54 +000097
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +000098/* 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 Andersen3f980402001-04-04 17:31:15 +0000101
Denis Vlasenkob1759462008-06-20 20:20:54 +0000102struct 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 McGrath09adaca2002-12-02 21:18:10 +0000110#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 Vlasenko0b3b41b2007-05-30 02:01:40 +0000120#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000121 smallint readonly_mode;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000122#define SET_READONLY_FILE(flags) ((flags) |= 0x01)
123#define SET_READONLY_MODE(flags) ((flags) |= 0x02)
124#define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000125#else
Denis Vlasenkob1759462008-06-20 20:20:54 +0000126#define SET_READONLY_FILE(flags) ((void)0)
127#define SET_READONLY_MODE(flags) ((void)0)
128#define UNSET_READONLY_FILE(flags) ((void)0)
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000129#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000130
Denis Vlasenkob1759462008-06-20 20:20:54 +0000131 smallint editing; // >0 while we are editing a file
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000132 // [code audit says "can be 0, 1 or 2 only"]
Denis Vlasenkob1759462008-06-20 20:20:54 +0000133 smallint cmd_mode; // 0=command 1=insert 2=replace
134 int file_modified; // buffer contents changed (counter, not flag!)
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000135 int last_file_modified; // = -1;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000136 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 Vlasenko4c9e9c42008-10-20 08:59:03 +0000150 int last_forward_char; // last char searched for with 'f' (int because of Unicode)
Denis Vlasenkob1759462008-06-20 20:20:54 +0000151 char erase_char; // the users erase character
152 char last_input_char; // last char read from user
Denis Vlasenkob1759462008-06-20 20:20:54 +0000153
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000154 smalluint chars_to_parse;
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000155#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkob1759462008-06-20 20:20:54 +0000156 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 Vlasenko0b3b41b2007-05-30 02:01:40 +0000159#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000160#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenkob1759462008-06-20 20:20:54 +0000161 int last_row; // where the cursor was last moved to
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000162#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000163#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkob1759462008-06-20 20:20:54 +0000164 int my_pid;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000165#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000166#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkob1759462008-06-20 20:20:54 +0000167 char *last_search_pattern; // last pattern from a '/' or '?' search
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000168#endif
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000169
Denis Vlasenkob1759462008-06-20 20:20:54 +0000170 /* 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 Andersen3f980402001-04-04 17:31:15 +0000176
Denis Vlasenkob1759462008-06-20 20:20:54 +0000177 /* a few references only */
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000178#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000179 int YDreg, Ureg; // default delete register and orig line for "U"
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000180 char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000181 char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
182 char *context_start, *context_end;
183#endif
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000184#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkob1759462008-06-20 20:20:54 +0000185 sigjmp_buf restart; // catch_sig()
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000186#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000187 struct termios term_orig, term_vi; // remember what the cooked mode was
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000188#if ENABLE_FEATURE_VI_COLON
189 char *initial_cmds[3]; // currently 2 entries, NULL terminated
190#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000191 // Should be just enough to hold a key sequence,
Denis Vlasenko25497c12008-10-14 10:25:05 +0000192 // but CRASHME mode uses it as generated command buffer too
193#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko1dfeeeb2008-10-18 19:04:37 +0000194 char readbuffer[128];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000195#else
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000196 char readbuffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000197#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000198#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 Vlasenko88adfcd2007-12-22 15:40:13 +0000204
205 char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000206};
207#define G (*ptr_to_globals)
208#define text (G.text )
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000209#define text_size (G.text_size )
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000210#define end (G.end )
211#define dot (G.dot )
212#define reg (G.reg )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000213
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 Vlasenko0112ff52008-10-25 23:23:00 +0000235#define last_forward_char (G.last_forward_char )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000236#define erase_char (G.erase_char )
237#define last_input_char (G.last_input_char )
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000238#define chars_to_parse (G.chars_to_parse )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000239#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000240#define readonly_mode (G.readonly_mode )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000241#else
242#define readonly_mode 0
243#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000244#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 Vlasenkob1759462008-06-20 20:20:54 +0000250#define last_search_pattern (G.last_search_pattern)
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000251
Denis Vlasenkob1759462008-06-20 20:20:54 +0000252#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 Vlasenko0b3b41b2007-05-30 02:01:40 +0000256#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 Vlasenkoa96425f2007-12-09 04:13:43 +0000265#define readbuffer (G.readbuffer )
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000266#define scr_out_buf (G.scr_out_buf )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000267#define last_modifying_cmd (G.last_modifying_cmd )
268#define get_input_line__buf (G.get_input_line__buf)
269
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000270#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000271 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkob1759462008-06-20 20:20:54 +0000272 last_file_modified = -1; \
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000273 /* "" but has space for 2 chars: */ \
274 USE_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000275} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000276
Denis Vlasenkob1759462008-06-20 20:20:54 +0000277
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000278static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000279static void edit_file(char *); // edit one file
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000280static void do_cmd(int); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000281static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000282static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
283static char *begin_line(char *); // return pointer to cur line B-o-l
284static char *end_line(char *); // return pointer to cur line E-o-l
285static char *prev_line(char *); // return pointer to prev line B-o-l
286static char *next_line(char *); // return pointer to next line B-o-l
287static char *end_screen(void); // get pointer to last char on screen
288static int count_lines(char *, char *); // count line from start to stop
289static char *find_line(int); // find begining of line #li
290static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000291static void dot_left(void); // move dot left- dont leave line
292static void dot_right(void); // move dot right- dont leave line
293static void dot_begin(void); // move dot to B-o-l
294static void dot_end(void); // move dot to E-o-l
295static void dot_next(void); // move dot to next line B-o-l
296static void dot_prev(void); // move dot to prev line B-o-l
297static void dot_scroll(int, int); // move the screen up or down
298static void dot_skip_over_ws(void); // move dot pat WS
299static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000300static char *bound_dot(char *); // make sure text[0] <= P < "end"
301static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000302static char *char_insert(char *, char); // insert the char c at 'p'
303static char *stupid_insert(char *, char); // stupidly insert the char c at 'p'
Paul Foxc51fc7b2008-03-06 01:34:23 +0000304static int find_range(char **, char **, char); // return pointers for an object
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000305static int st_test(char *, int, int, char *); // helper for skip_thing()
306static char *skip_thing(char *, int, int, int); // skip some object
307static char *find_pair(char *, char); // find matching pair () [] {}
308static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
309static char *text_hole_make(char *, int); // at "p", make a 'size' byte hole
310static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000311static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000312static void rawmode(void); // set "raw" mode on tty
313static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000314// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
315static int mysleep(int);
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000316static int readit(void); // read (maybe cursor) key from stdin
317static int get_one_char(void); // read 1 char from stdin
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000318static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000319#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000320static int file_insert(const char *, char *, int);
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000321#else
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000322static int file_insert(const char *, char *);
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000323#endif
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000324static int file_write(char *, char *, char *);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000325#if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
326#define place_cursor(a, b, optimize) place_cursor(a, b)
327#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000328static void place_cursor(int, int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000329static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000330static void clear_to_eol(void);
331static void clear_to_eos(void);
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000332static void go_bottom_and_clear_to_eol(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000333static void standout_start(void); // send "start reverse video" sequence
334static void standout_end(void); // send "end reverse video" sequence
335static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000336static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000337static void status_line(const char *, ...); // print to status buf
338static void status_line_bold(const char *, ...);
339static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000340static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000341static void redraw(int); // force a full screen refresh
Denis Vlasenko68404f12008-03-17 09:00:54 +0000342static char* format_line(char* /*, int*/);
Eric Andersen3f980402001-04-04 17:31:15 +0000343static void refresh(int); // update the terminal from screen[]
344
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000345static void Indicate_Error(void); // use flash or beep to indicate error
346#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000347static void Hit_Return(void);
348
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000349#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000350static char *char_search(char *, const char *, int, int); // search for pattern starting at p
351static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000352#endif
353#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000354static char *get_one_address(char *, int *); // get colon addr, if present
355static char *get_address(char *, int *, int *); // get two colon addrs, if present
356static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000357#endif
358#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000359static void winch_sig(int); // catch window size changes
360static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000361static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000362#endif
363#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000364static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000365static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000366#else
367#define end_cmd_q() ((void)0)
368#endif
369#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000370static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000371#endif
372#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000373static char *string_insert(char *, char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000374#endif
375#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000376static char *text_yank(char *, char *, int); // save copy of "p" into a register
377static char what_reg(void); // what is letter of current YDreg
378static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000379#endif
380#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000381static void crash_dummy();
382static void crash_test();
383static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000384#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000385
386
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000387static void write1(const char *out)
388{
389 fputs(out, stdout);
390}
391
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000392int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000393int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000394{
Eric Andersend402edf2001-04-04 19:29:48 +0000395 int c;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000396
397 INIT_G();
Eric Andersen3f980402001-04-04 17:31:15 +0000398
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000399#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000400 my_pid = getpid();
401#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000402#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000403 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000404#endif
Denis Vlasenko2414a962007-07-18 22:03:40 +0000405#ifdef NO_SUCH_APPLET_YET
406 /* If we aren't "vi", we are "view" */
407 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000408 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000409 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000410#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000411
Bernhard Reutner-Fischer73f56bb2007-09-22 21:18:46 +0000412 vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenkof9234132007-03-21 00:03:42 +0000413 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000414 // 2- process EXINIT variable from environment
415 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000416#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000417 {
418 char *p = getenv("EXINIT");
419 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000420 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000421 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000422#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000423 while ((c = getopt(argc, argv, "hCRH" USE_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000424 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000425#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000426 case 'C':
427 crashme = 1;
428 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000429#endif
430#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000431 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000432 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000433 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000434#endif
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000435#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000436 case 'c': // cmd line vi command
437 if (*optarg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000438 initial_cmds[initial_cmds[0] != 0] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000439 break;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000440#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000441 case 'H':
Eric Andersen3f980402001-04-04 17:31:15 +0000442 show_help();
Paul Fox35e9c5d2008-03-06 16:26:12 +0000443 /* fall through */
Paul Fox35e9c5d2008-03-06 16:26:12 +0000444 default:
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000445 bb_show_usage();
Eric Andersendd8500b2001-07-02 18:06:14 +0000446 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000447 }
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 Andersen3f980402001-04-04 17:31:15 +0000457 edit_file(0);
458 } else {
459 for (; optind < argc; optind++) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000460 edit_file(argv[optind]);
Eric Andersen3f980402001-04-04 17:31:15 +0000461 }
462 }
463 //-----------------------------------------------------------
464
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000465 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000466}
467
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000468/* read text from file or create an empty buf */
469/* will also update current_filename */
470static 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 Vlasenkob1759462008-06-20 20:20:54 +0000477 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000478 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000479
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000480 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 Vlasenko2f6ae432007-07-19 22:50:47 +0000499}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000500
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000501static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000502{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000503#if ENABLE_FEATURE_VI_YANKMARK
504#define cur_line edit_file__cur_line
505#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000506 int c;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000507 int size;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000508#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000509 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000510#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000511
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000512 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000513 rawmode();
514 rows = 24;
515 columns = 80;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000516 size = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000517 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Rob Landleye5e1a102006-06-21 01:15:36 +0000518 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000519 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
520 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
521 }
Eric Andersen3f980402001-04-04 17:31:15 +0000522 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000523 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000524
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000525#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000526 YDreg = 26; // default Yank/Delete reg
527 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000528 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000529#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000530
Eric Andersen3f980402001-04-04 17:31:15 +0000531 last_forward_char = last_input_char = '\0';
532 crow = 0;
533 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000534
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000535#if ENABLE_FEATURE_VI_USE_SIGNALS
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000536 catch_sig(0);
Eric Andersen3f980402001-04-04 17:31:15 +0000537 signal(SIGWINCH, winch_sig);
538 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000539 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000540 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000541 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000542 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000543#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000544
Eric Andersen3f980402001-04-04 17:31:15 +0000545 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +0000550#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000551 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000552 ioq = ioq_start = NULL;
553 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000554 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000555#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000556
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000557#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000558 {
559 char *p, *q;
560 int n = 0;
561
562 while ((p = initial_cmds[n])) {
563 do {
564 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000565 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000566 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000567 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000568 *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 Vlasenko58875ae2007-03-22 22:22:10 +0000577#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000578 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000579 //------This is the main Vi cmd handling loop -----------------------
580 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000581#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000582 if (crashme > 0) {
583 if ((end - text) > 1) {
584 crash_dummy(); // generate a random command
585 } else {
586 crashme = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000587 dot = string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +0000588 refresh(FALSE);
589 }
590 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000591#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000592 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000593#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000594 // 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +0000599#endif
600#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000601 // These are commands that change text[].
602 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000603 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000604 && 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 Vlasenkoafa37cf2007-03-21 00:05:35 +0000608 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000609 start_new_cmd_q(c);
610 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000611#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000612 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000613
Eric Andersen3f980402001-04-04 17:31:15 +0000614 // 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 Vlasenko8ef801b2008-10-16 09:46:07 +0000617 if (!chars_to_parse && mysleep(0) == 0) {
618 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000619 refresh(FALSE);
620 show_status_line();
621 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000622#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000623 if (crashme > 0)
624 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000625#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000626 }
627 //-------------------------------------------------------------------
628
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000629 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000630 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000631#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000632}
633
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000634//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000635#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000636static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000637{
638 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000639 char *q;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000640 USE_FEATURE_VI_YANKMARK(char c;)
641 USE_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000642
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 Vlasenko88adfcd2007-12-22 15:40:13 +0000648 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000649#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000650 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000651 p++;
652 c = tolower(*p);
653 p++;
654 if (c >= 'a' && c <= 'z') {
655 // we have a mark
656 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000657 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000658 if (q != NULL) { // is mark valid
659 *addr = count_lines(text, q); // count lines
660 }
661 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000662 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000663#endif
664#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000665 else if (*p == '/') { // a search pattern
666 q = strchrnul(++p, '/');
667 pat = xstrndup(p, q - p); // save copy of pattern
668 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000669 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 Vlasenko88adfcd2007-12-22 15:40:13 +0000676 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000677#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000678 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000679 p++;
680 q = begin_line(end - 1);
681 *addr = count_lines(text, q);
682 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000683 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000684 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000685 } else {
686 // unrecognised address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000687 *addr = -1;
688 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000689 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000690}
691
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000692static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000693{
694 //----- get the address' i.e., 1,3 'a,'b -----
695 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000696 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000697 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 Vlasenkoeaabf062007-07-17 23:14:07 +0000705 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000706 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000707 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000708 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000709 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000710 p++;
711 // get SECOND addr, if present
712 p = get_one_address(p, e);
713 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000714 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000715 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000716 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000717 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000718}
719
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000720#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000721static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000722 const char *short_opname, int opt)
723{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000724 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000725 int l = strlen(opname) - 1; /* opname have + ' ' */
726
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000727 if (strncasecmp(a, opname, l) == 0
728 || strncasecmp(a, short_opname, 2) == 0
729 ) {
730 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000731 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000732 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000733 vi_setops |= opt;
734 }
735}
736#endif
737
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000738// buf must be no longer than MAX_INPUT_LEN!
739static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000740{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000741 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000742 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000743 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000744 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000745
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 Andersen165e8cb2004-07-20 06:44:46 +0000760
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000761 if (!buf[0])
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000762 goto vc1;
763 if (*buf == ':')
764 buf++; // move past the ':'
765
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000766 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000767 b = e = -1;
768 q = text; // assume 1,$ for the range
769 r = end - 1;
770 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000771 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000772
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 Vlasenko88adfcd2007-12-22 15:40:13 +0000786 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000787 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000788 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000789 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000790 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000791 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000792 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000793 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 Vlasenkoafa37cf2007-03-21 00:05:35 +0000814 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000815 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 Vlasenko249fabf2006-12-19 00:29:22 +0000820 }
821#if ENABLE_FEATURE_ALLOW_EXEC
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000822 else if (strncmp(cmd, "!", 1) == 0) { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000823 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000824 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000825 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000826 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000827 retcode = system(orig_buf + 1); // run the cmd
828 if (retcode)
829 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000830 rawmode();
831 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000832 }
833#endif
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000834 else if (strncmp(cmd, "=", i) == 0) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000835 if (b < 0) { // no addr given- use defaults
836 b = e = count_lines(text, dot);
837 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000838 status_line("%d", b);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000839 } else if (strncasecmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000840 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 Vlasenkoafa37cf2007-03-21 00:05:35 +0000846 } else if (strncasecmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000847 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000848 if (file_modified && !useforce) {
849 status_line_bold("No write since last change (:edit! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000850 goto vc1;
851 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000852 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000853 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000854 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000855 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000856 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000857 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000858 } else {
859 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000860 status_line_bold("No current filename");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000861 goto vc1;
862 }
863
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000864 if (init_text_buffer(fn) < 0)
865 goto vc1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000866
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000867#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000868 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +0000876#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000877 // how many lines in text[]?
878 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000879 status_line("\"%s\"%s"
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000880 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 Lehmann6fdacc72002-08-21 13:02:24 +0000886 li, ch);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000887 } else if (strncasecmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000888 if (b != -1 || e != -1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000889 not_implemented("No address allowed on this command");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000890 goto vc1;
891 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000892 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000893 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000894 free(current_filename);
895 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000896 } else {
897 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +0000898 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000899 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000900 } else if (strncasecmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000901 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000902 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000903 cookmode();
904 show_help();
905 rawmode();
906 Hit_Return();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000907 } else if (strncasecmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000908 if (b < 0) { // no addr given- use defaults
909 q = begin_line(dot); // assume .,. for the range
910 r = end_line(dot);
911 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000912 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000913 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000914 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000915 int c_is_no_print;
916
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000917 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +0000918 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000919 if (c_is_no_print) {
920 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000921 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000922 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000923 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000924 write1("$\r");
925 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000926 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000927 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000928 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000929 else
930 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000931 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000932 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000933 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000934 standout_end();
935 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000936#if ENABLE_FEATURE_VI_SET
937 vc2:
938#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000939 Hit_Return();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000940 } else if (strncasecmp(cmd, "quit", i) == 0 // Quit
941 || strncasecmp(cmd, "next", i) == 0 // edit next file
942 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000943 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 Vlasenko88adfcd2007-12-22 15:40:13 +0000953 status_line_bold("No write since last change (:%s! overrides)",
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000954 (*cmd == 'q' ? "quit" : "next"));
955 goto vc1;
956 }
957 // are there other file to edit
958 if (*cmd == 'q' && optind < save_argc - 1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000959 status_line_bold("%d more file to edit", (save_argc - optind - 1));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000960 goto vc1;
961 }
962 if (*cmd == 'n' && optind >= save_argc - 1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000963 status_line_bold("No more files to edit");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000964 goto vc1;
965 }
966 editing = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000967 } else if (strncasecmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000968 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000969 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000970 status_line_bold("No filename given");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000971 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 Vlasenkoeaabf062007-07-17 23:14:07 +0000979 ch = file_insert(fn, q USE_FEATURE_VI_READONLY(, 0));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000980 if (ch < 0)
981 goto vc1; // nothing was inserted
982 // how many lines in text[]?
983 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000984 status_line("\"%s\""
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000985 USE_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000986 " %dL, %dC", fn,
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000987 USE_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000988 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 Fox8552aec2005-09-16 12:20:05 +0000993 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000994 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000995 } else if (strncasecmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000996 if (file_modified && !useforce) {
997 status_line_bold("No write since last change (:rewind! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000998 } else {
999 // reset the filenames to edit
1000 optind = fn_start - 1;
1001 editing = 0;
1002 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001003#if ENABLE_FEATURE_VI_SET
Denis Vlasenkof9234132007-03-21 00:03:42 +00001004 } else if (strncasecmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001005#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001006 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001007#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001008 i = 0; // offset into args
Denis Vlasenkof9234132007-03-21 00:03:42 +00001009 // only blank is regarded as args delmiter. What about tab '\t' ?
1010 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001011 // print out values of all options
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001012 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001013 printf("----------------------------------------\r\n");
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001014#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001015 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00001028#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001029 printf("\r\n");
1030 goto vc2;
1031 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001032#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001033 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001034 while (*argp) {
Denis Vlasenkof9234132007-03-21 00:03:42 +00001035 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00001043 sscanf(strchr(argp + i, '='), "tabstop=%d" + 7, &ch);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00001044 if (ch > 0 && ch <= MAX_TABSTOP)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001045 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 Lehmann6fdacc72002-08-21 13:02:24 +00001051 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001052#endif /* FEATURE_VI_SETOPTS */
1053#endif /* FEATURE_VI_SET */
1054#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001055 } else if (strncasecmp(cmd, "s", 1) == 0) { // substitute a pattern with a replacement pattern
1056 char *ls, *F, *R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001057 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00001065 R = strchr(F, c); // middle delimiter
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001066 if (!R) goto colon_s_fail;
1067 *R++ = '\0'; // terminate "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001068 buf1 = strchr(R, c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001069 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00001084 vc4:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001085 buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001086 if (buf1) {
1087 // we found the "find" pattern - delete it
1088 text_hole_delete(buf1, buf1 + strlen(F) - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001089 // inset the "replace" patern
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001090 string_insert(buf1, R); // insert the string
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001091 // check for "global" :s/foo/bar/g
1092 if (gflag == 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001093 if ((buf1 + strlen(R)) < end_line(ls)) {
1094 q = buf1 + strlen(R);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001095 goto vc4; // don't let q move past cur line
1096 }
1097 }
1098 }
1099 q = next_line(ls);
1100 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001101#endif /* FEATURE_VI_SEARCH */
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001102 } else if (strncasecmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001103 status_line(BB_VER " " BB_BT);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001104 } 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 Lehmann6fdacc72002-08-21 13:02:24 +00001109 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001110 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001111 fn = args;
1112 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001113#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001114 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001115 status_line_bold("\"%s\" File is read only", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001116 goto vc3;
1117 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001118#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001119 // 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 Fox61e45db2005-10-09 14:43:22 +00001136 if (l < 0) {
1137 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001138 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001139 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001140 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001141 if (q == text && r == end - 1 && l == ch) {
1142 file_modified = 0;
1143 last_file_modified = -1;
1144 }
Paul Fox9360f422006-03-27 21:51:16 +00001145 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' ||
1146 cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N')
1147 && l == ch) {
Paul Fox61e45db2005-10-09 14:43:22 +00001148 editing = 0;
1149 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001150 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001151#if ENABLE_FEATURE_VI_READONLY
1152 vc3:;
1153#endif
1154#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001155 } else if (strncasecmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001156 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 Vlasenko88adfcd2007-12-22 15:40:13 +00001162 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001163 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001164#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001165 } else {
1166 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001167 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001168 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001169 vc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001170 dot = bound_dot(dot); // make sure "dot" is valid
1171 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001172#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001173 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001174 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001175#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001176}
Paul Fox61e45db2005-10-09 14:43:22 +00001177
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001178#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001179
1180static void Hit_Return(void)
1181{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001182 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001183
Denis Vlasenkof882f082007-12-23 02:36:51 +00001184 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001185 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001186 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001187 while ((c = get_one_char()) != '\n' && c != '\r')
1188 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001189 redraw(TRUE); // force redraw all
1190}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001191
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001192static int next_tabstop(int col)
1193{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001194 return col + ((tabstop - 1) - (col % tabstop));
1195}
1196
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001197//----- Synchronize the cursor to Dot --------------------------
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001198static void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001199{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001200 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001201 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001202 int cnt, ro, co;
1203
1204 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001205
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001206 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001207 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001208 // how many lines do we have to move
1209 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001210 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001211 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 Vlasenkof882f082007-12-23 02:36:51 +00001218 } 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 Lehmann6fdacc72002-08-21 13:02:24 +00001234 }
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 Vlasenkoe3cbfb92007-12-22 17:00:11 +00001246 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001247 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001248 break;
1249 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001250 // handle tabs like real vi
1251 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001252 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001253 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001254 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001255 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1256 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001257 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001258 co++;
1259 tp++;
1260 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001261
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 Vlasenkof882f082007-12-23 02:36:51 +00001293static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001294{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001295 if (p > text) {
1296 p = memrchr(text, '\n', p - text);
1297 if (!p)
1298 return text;
1299 return p + 1;
1300 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001301 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001302}
1303
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001304static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001305{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001306 if (p < end - 1) {
1307 p = memchr(p, '\n', end - p - 1);
1308 if (!p)
1309 return end - 1;
1310 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001311 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001312}
1313
Denis Vlasenkof882f082007-12-23 02:36:51 +00001314static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001315{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001316 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001317 // Try to stay off of the Newline
1318 if (*p == '\n' && (p - begin_line(p)) > 0)
1319 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001320 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001321}
1322
Denis Vlasenkof882f082007-12-23 02:36:51 +00001323static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001324{
1325 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001326 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001327 p--; // step to prev line
1328 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001329 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001330}
1331
Denis Vlasenkof882f082007-12-23 02:36:51 +00001332static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001333{
1334 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001335 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001336 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001337 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001338}
1339
1340//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001341static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001342{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001343 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001344 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 Vlasenko079f8af2006-11-27 16:49:31 +00001351 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001352}
1353
Denis Vlasenkof882f082007-12-23 02:36:51 +00001354// count line from start to stop
1355static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001356{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001357 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001358 int cnt;
1359
Denis Vlasenkof882f082007-12-23 02:36:51 +00001360 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001361 q = start;
1362 start = stop;
1363 stop = q;
1364 }
1365 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001366 stop = end_line(stop);
1367 while (start <= stop && start <= end - 1) {
1368 start = end_line(start);
1369 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001370 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001371 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001372 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001373 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001374}
1375
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001376static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001377{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001378 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001379
1380 for (q = text; li > 1; li--) {
1381 q = next_line(q);
1382 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001383 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001384}
1385
1386//----- Dot Movement Routines ----------------------------------
1387static void dot_left(void)
1388{
1389 if (dot > text && dot[-1] != '\n')
1390 dot--;
1391}
1392
1393static void dot_right(void)
1394{
1395 if (dot < end - 1 && *dot != '\n')
1396 dot++;
1397}
1398
1399static void dot_begin(void)
1400{
1401 dot = begin_line(dot); // return pointer to first char cur line
1402}
1403
1404static void dot_end(void)
1405{
1406 dot = end_line(dot); // return pointer to last char cur line
1407}
1408
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001409static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001410{
1411 int co;
1412
1413 p = begin_line(p);
1414 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001415 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001416 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001417 break;
1418 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001419 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001420 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001421 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001422 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001423 co++;
1424 p++;
1425 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001426 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001427}
1428
1429static void dot_next(void)
1430{
1431 dot = next_line(dot);
1432}
1433
1434static void dot_prev(void)
1435{
1436 dot = prev_line(dot);
1437}
1438
1439static void dot_scroll(int cnt, int dir)
1440{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001441 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001442
1443 for (; cnt > 0; cnt--) {
1444 if (dir < 0) {
1445 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001446 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001447 screenbegin = prev_line(screenbegin);
1448 } else {
1449 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001450 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001451 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
1463static void dot_skip_over_ws(void)
1464{
1465 // skip WS
1466 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1467 dot++;
1468}
1469
1470static void dot_delete(void) // delete the char at 'dot'
1471{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001472 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001473}
1474
Denis Vlasenkof882f082007-12-23 02:36:51 +00001475static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001476{
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 Vlasenko079f8af2006-11-27 16:49:31 +00001485 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001486}
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 Vlasenkoafa37cf2007-03-21 00:05:35 +00001501static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001502{
1503 int li;
1504
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001505 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001506 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001507 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001508 // initialize the new screen. assume this will be a empty file.
1509 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001510 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001511 for (li = 1; li < ro - 1; li++) {
1512 screen[(li * co) + 0] = '~';
1513 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001514 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001515}
1516
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001517#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko33875382008-06-21 20:31:50 +00001518static int mycmp(const char *s1, const char *s2, int len)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001519{
1520 int i;
1521
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001522 i = strncmp(s1, s2, len);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001523 if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001524 i = strncasecmp(s1, s2, len);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001525 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001526 return i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001527}
1528
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001529// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001530static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001531{
1532#ifndef REGEX_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001533 char *start, *stop;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001534 int len;
1535
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001536 len = strlen(pat);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001537 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 Vlasenkod9e15f22006-11-27 16:49:55 +00001543 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001544 }
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 Vlasenkod9e15f22006-11-27 16:49:55 +00001552 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001553 }
1554 }
1555 }
1556 // pattern not found
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001557 return NULL;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001558#else /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001559 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00001585 q = re_compile_pattern(pat, strlen(pat), &preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001586 if (q != 0) {
1587 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001588 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001589 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00001612 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001613 if (dir == FORWARD) {
1614 p = p + i;
1615 } else {
1616 p = p - i;
1617 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001618 return p;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001619#endif /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001620}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001621#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001622
Denis Vlasenko33875382008-06-21 20:31:50 +00001623static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001624{
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 Vlasenkob1759462008-06-20 20:20:54 +00001632 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001633 } else if (c == 27) { // Is this an ESC?
1634 cmd_mode = 0;
1635 cmdcnt = 0;
1636 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001637 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001638 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001639 p--;
1640 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001641 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001642 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001643 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001644 p--;
1645 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001646 }
1647 } else {
1648 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001649 char *sp; // "save p"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001650
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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00001655#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001656 if (showmatch && strchr(")]}", *sp) != NULL) {
1657 showmatching(sp);
1658 }
1659 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001660 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001661
1662 q = prev_line(p); // use prev line as templet
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001663 for (; isblank(*q); q++) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001664 p = stupid_insert(p, *q); // insert the char
1665 }
1666 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001667#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001668 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001669 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001670}
1671
Denis Vlasenko33875382008-06-21 20:31:50 +00001672static char *stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001673{
1674 p = text_hole_make(p, 1);
Denis Vlasenkob1759462008-06-20 20:20:54 +00001675 *p = c;
1676 //file_modified++; - done by text_hole_make()
1677 return p + 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001678}
1679
Denis Vlasenko33875382008-06-21 20:31:50 +00001680static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001681{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001682 char *save_dot, *p, *q, *t;
1683 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001684
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 Foxc51fc7b2008-03-06 01:34:23 +00001695 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001696 // 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 Rikerc1ef7bd2006-01-25 00:08:53 +00001701 // 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 Andersen5cc90ea2004-02-06 10:36:08 +00001704 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001705 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1706 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1707 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001708 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 Foxc51fc7b2008-03-06 01:34:23 +00001724 // 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 Lehmann6fdacc72002-08-21 13:02:24 +00001729 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001730 if (q < p) {
1731 t = q;
1732 q = p;
1733 p = t;
1734 }
1735
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001736 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001737 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 Lehmann6fdacc72002-08-21 13:02:24 +00001747 *start = p;
1748 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001749 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001750 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001751}
1752
Denis Vlasenko33875382008-06-21 20:31:50 +00001753static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001754{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001755 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001756 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 Vlasenkod9e15f22006-11-27 16:49:55 +00001784 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001785}
1786
Denis Vlasenko33875382008-06-21 20:31:50 +00001787static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001788{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001789 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001790
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 Vlasenko079f8af2006-11-27 16:49:31 +00001801 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001802}
1803
1804// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00001805static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001806{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001807 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001808 int dir, level;
1809
1810 match = ')';
1811 level = 1;
1812 dir = 1; // assume forward
1813 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001814 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 Lehmann6fdacc72002-08-21 13:02:24 +00001820 }
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 Vlasenko079f8af2006-11-27 16:49:31 +00001832 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001833}
1834
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001835#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001836// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00001837static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001838{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001839 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001840
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 Vlasenkoafa37cf2007-03-21 00:05:35 +00001850 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001851 dot = save_dot; // go back to old loc
1852 refresh(FALSE);
1853 }
1854}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001855#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001856
1857// open a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00001858static char *text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001859{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001860 if (size <= 0)
Denis Vlasenkob1759462008-06-20 20:20:54 +00001861 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 Lehmann6fdacc72002-08-21 13:02:24 +00001872 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00001873 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001874 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00001875 file_modified++;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001876 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001877}
1878
1879// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00001880static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001881{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001882 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001883 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 Vlasenkob1759462008-06-20 20:20:54 +00001901 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001902 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001903 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 Vlasenkob1759462008-06-20 20:20:54 +00001908 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001909 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001910 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001911}
1912
1913// copy text into register, then delete text.
1914// if dist <= 0, do not include, or go past, a NewLine
1915//
Denis Vlasenko33875382008-06-21 20:31:50 +00001916static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001917{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001918 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001919
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 Vlasenkoe1a0d482006-10-20 13:28:22 +00001928 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001929 p = start;
1930 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001931 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001932 // 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00001941#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001942 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001943#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001944 if (yf == YANKDEL) {
1945 p = text_hole_delete(start, stop);
1946 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001947 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001948}
1949
1950static void show_help(void)
1951{
1952 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001953#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001954 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001955#endif
1956#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001957 "\n\tLast command repeat with \'.\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001958#endif
1959#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00001960 "\n\tLine marking with 'x"
1961 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001962#endif
1963#if ENABLE_FEATURE_VI_READONLY
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001964 "\n\tReadonly if vi is called as \"view\""
1965 "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001966#endif
1967#if ENABLE_FEATURE_VI_SET
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001968 "\n\tSome colon mode commands with \':\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001969#endif
1970#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001971 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001972#endif
1973#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001974 "\n\tSignal catching- ^C"
1975 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001976#endif
1977#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001978 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001979#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001980 );
1981}
1982
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001983#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001984static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001985{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001986 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001987 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00001988 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001989 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00001990 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00001991 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001992 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001993 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001994 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001995}
1996
1997static void end_cmd_q(void)
1998{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001999#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002000 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002001#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002002 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002003}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002004#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002005
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002006#if ENABLE_FEATURE_VI_YANKMARK \
2007 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2008 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko33875382008-06-21 20:31:50 +00002009static char *string_insert(char *p, char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002010{
2011 int cnt, i;
2012
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002013 i = strlen(s);
Denis Vlasenko33875382008-06-21 20:31:50 +00002014 text_hole_make(p, i);
2015 strncpy(p, s, i);
2016 for (cnt = 0; *s != '\0'; s++) {
2017 if (*s == '\n')
2018 cnt++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002019 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002020#if ENABLE_FEATURE_VI_YANKMARK
2021 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2022#endif
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002023 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002024}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002025#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002026
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002027#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002028static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002029{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002030 char *t;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002031 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 Lehmanna170e1c2002-11-28 11:27:31 +00002040 free(t); // if already a yank register, free it
Denis Vlasenkob95636c2006-12-19 23:36:04 +00002041 t = xmalloc(cnt + 1); // get a new register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002042 memset(t, '\0', cnt + 1); // clear new text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002043 strncpy(t, p, cnt); // copy text[] into bufer
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002044 reg[dest] = t;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002045 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002046}
2047
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002048static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002049{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002050 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002051
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002052 c = 'D'; // default to D-reg
2053 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002054 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002055 if (YDreg == 26)
2056 c = 'D';
2057 if (YDreg == 27)
2058 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002059 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002060}
2061
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002062static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002063{
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 Vlasenkoafa37cf2007-03-21 00:05:35 +00002068 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002069 // 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 Lehmann6fdacc72002-08-21 13:02:24 +00002077}
2078
Denis Vlasenkof882f082007-12-23 02:36:51 +00002079static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002080{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002081 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002082
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 Vlasenko079f8af2006-11-27 16:49:31 +00002094 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002095}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002096#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002097
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002098//----- Set terminal attributes --------------------------------
2099static 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 Lehmann6fdacc72002-08-21 13:02:24 +00002106 term_vi.c_cc[VMIN] = 1;
2107 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002108 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002109 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002110}
2111
2112static void cookmode(void)
2113{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002114 fflush(stdout);
Denis Vlasenko202ac502008-11-05 13:20:58 +00002115 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002116}
2117
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002118//----- Come here when we get a window resize signal ---------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002119#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002120static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002121{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002122 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002123 signal(SIGWINCH, winch_sig);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002124 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Denis Vlasenko621204b2006-10-27 09:03:24 +00002125 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002126 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
2127 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
2128 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002129 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 Vlasenkoa60f84e2008-07-05 09:18:54 +00002134static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002135{
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002136 rawmode(); // terminal to "raw"
2137 last_status_cksum = 0; // force status update
2138 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002139
2140 signal(SIGTSTP, suspend_sig);
2141 signal(SIGCONT, SIG_DFL);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002142 kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002143}
2144
2145//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002146static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002147{
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002148 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002149 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002150
2151 signal(SIGCONT, cont_sig);
2152 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002153 kill(my_pid, SIGTSTP);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002154}
2155
2156//----- Come here when we get a signal ---------------------------
2157static void catch_sig(int sig)
2158{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002159 signal(SIGINT, catch_sig);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002160 if (sig)
Paul Fox2724fa92008-03-17 15:28:07 +00002161 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002162}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002163#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002164
2165static int mysleep(int hund) // sleep for 'h' 1/100 seconds
2166{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002167 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002168
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002169 pfd[0].fd = 0;
2170 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002171 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002172}
2173
2174//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002175static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002176{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002177 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002178
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002179 fflush(stdout);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002180 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 McGrath09adaca2002-12-02 21:18:10 +00002185 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002186 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002187}
2188
2189//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002190static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002191{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002192 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002193
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002194#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002195 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 Vlasenko4c9e9c42008-10-20 08:59:03 +00002203 // careful with correct sign expansion!
2204 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002205 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 Vlasenkob1759462008-06-20 20:20:54 +00002215 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 Lehmann6fdacc72002-08-21 13:02:24 +00002220 }
2221 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002222#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002223 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002224#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002225 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002226}
2227
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002228// Get input line (uses "status line" area)
2229static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002230{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002231 // char [MAX_INPUT_LEN]
2232#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002233
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002234 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002235 int i;
2236
2237 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002238 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002239 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002240 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002241
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002242 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002243 while (i < MAX_INPUT_LEN) {
2244 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002245 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002246 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002247 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002248 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002249 buf[--i] = '\0';
2250 write1("\b \b"); // erase char on screen
2251 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002252 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002253 } else if (c > 0 && c < 256) { // exclude Unicode
2254 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002255 buf[i] = c;
2256 buf[++i] = '\0';
2257 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002258 }
2259 }
2260 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002261 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002262#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002263}
2264
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002265static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002266{
2267 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002268 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002269
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002270 cnt = -1;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002271 if (fn && fn[0] && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002272 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002273 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002274}
2275
Denis Vlasenko33875382008-06-21 20:31:50 +00002276static int file_insert(const char *fn, char *p
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002277 USE_FEATURE_VI_READONLY(, int update_ro_status))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002278{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002279 int cnt = -1;
2280 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002281 struct stat statbuf;
2282
2283 /* Validate file */
2284 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002285 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002286 goto fi0;
2287 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002288 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002289 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002290 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002291 goto fi0;
2292 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002293 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002294 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002295 goto fi0;
2296 }
2297
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002298 // read file to buffer
2299 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002300 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002301 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002302 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002303 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002304 size = statbuf.st_size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002305 p = text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002306 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002307 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002308 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002309 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002310 } 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 Vlasenko88adfcd2007-12-22 15:40:13 +00002313 status_line_bold("cannot read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002314 }
2315 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002316 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002317 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002318 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002319#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 Vlasenko2f6ae432007-07-19 22:50:47 +00002327 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002328 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002329#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002330 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002331}
2332
Denis Vlasenko33875382008-06-21 20:31:50 +00002333static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002334{
2335 int fd, cnt, charcnt;
2336
2337 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002338 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002339 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002340 }
2341 charcnt = 0;
Denis Vlasenko8abae882008-05-03 11:35:59 +00002342 /* 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 Lehmann6fdacc72002-08-21 13:02:24 +00002347 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002348 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002349 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002350 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002351 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002352 if (charcnt == cnt) {
2353 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002354 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002355 } else {
2356 charcnt = 0;
2357 }
2358 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002359 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002360}
2361
2362//----- Terminal Drawing ---------------------------------------
2363// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002364// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002365// screen coordinates
2366// 0,0 ... 0,79
2367// 1,0 ... 1,79
2368// . ... .
2369// . ... .
2370// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002371// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002372
2373//----- Move the cursor to row x col (count from 0, not 1) -------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002374static void place_cursor(int row, int col, int optimize)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002375{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002376 char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
Denis Vlasenko7b54dc72008-07-17 21:32:32 +00002377#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 Lehmann6fdacc72002-08-21 13:02:24 +00002385 char *cm;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002386
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 Andersenc7bda1c2004-03-15 08:29:22 +00002391
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002392 //----- 1. Try the standard terminal ESC sequence
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002393 sprintf(cm1, CMrc, row + 1, col + 1);
2394 cm = cm1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002395
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002396#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002397 if (optimize && col < 16) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002398 char *screenp;
2399 int Rrow = last_row;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002400 int diff = Rrow - row;
2401
2402 if (diff < -5 || diff > 5)
2403 goto skip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002404
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002405 //----- 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 Vlasenko88adfcd2007-12-22 15:40:13 +00002431 skip: ;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002432 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002433 last_row = row;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002434#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002435 write1(cm);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002436}
2437
2438//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002439static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002440{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002441 write1(Ceol); // Erase from cursor to end of line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002442}
2443
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002444static 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 Lehmann6fdacc72002-08-21 13:02:24 +00002450//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002451static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002452{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002453 write1(Ceos); // Erase from cursor to end of screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002454}
2455
2456//----- Start standout mode ------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002457static void standout_start(void) // send "start reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002458{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002459 write1(SOs); // Start reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002460}
2461
2462//----- End standout mode --------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002463static void standout_end(void) // send "end reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002464{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002465 write1(SOn); // End reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002466}
2467
2468//----- Flash the screen --------------------------------------
2469static void flash(int h)
2470{
2471 standout_start(); // send "start reverse video" sequence
2472 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002473 mysleep(h);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002474 standout_end(); // send "end reverse video" sequence
2475 redraw(TRUE);
2476}
2477
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002478static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002479{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002480#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002481 if (crashme > 0)
2482 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002483#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002484 if (!err_method) {
2485 write1(bell); // send out a bell character
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002486 } else {
2487 flash(10);
2488 }
2489}
2490
2491//----- Screen[] Routines --------------------------------------
2492//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002493static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002494{
2495 memset(screen, ' ', screensize); // clear new screen
2496}
2497
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002498static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002499{
2500 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002501 char *e = buf + count;
2502
Paul Fox8552aec2005-09-16 12:20:05 +00002503 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002504 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002505 return sum;
2506}
2507
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002508//----- Draw the status line at bottom of the screen -------------
2509static void show_status_line(void)
2510{
Paul Foxc3504852005-09-16 12:48:18 +00002511 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002512
Paul Fox8552aec2005-09-16 12:20:05 +00002513 // 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 Vlasenko88adfcd2007-12-22 15:40:13 +00002520 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002521 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002522 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002523 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002524 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002525 (columns - 1) ) {
2526 have_status_msg = 0;
2527 Hit_Return();
2528 }
2529 have_status_msg = 0;
2530 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002531 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2532 }
Eric Andersena9eb33d2004-08-19 19:15:06 +00002533 fflush(stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002534}
2535
2536//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002537// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002538static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002539{
2540 va_list args;
2541
2542 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002543 strcpy(status_buffer, SOs); // Terminal standout mode on
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002544 vsprintf(status_buffer + sizeof(SOs)-1, format, args);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002545 strcat(status_buffer, SOn); // Terminal standout mode off
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002546 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002547
2548 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002549}
2550
Paul Fox8552aec2005-09-16 12:20:05 +00002551// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002552static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002553{
2554 va_list args;
2555
2556 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002557 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002558 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002559
2560 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002561}
2562
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002563// copy s to buf, convert unprintable
2564static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002565{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002566 unsigned char c;
2567 char b[2];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002568
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002569 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 Lehmann6fdacc72002-08-21 13:02:24 +00002598}
2599
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002600static 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
2609static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002610{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002611 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002612
2613#define tot format_edit_status__tot
2614
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002615 int cur, percent, ret, trunc_at;
2616
Paul Fox8552aec2005-09-16 12:20:05 +00002617 // 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 Lehmann6fdacc72002-08-21 13:02:24 +00002625 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002626
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 Lehmann6fdacc72002-08-21 13:02:24 +00002634 // 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 Andersen0ef24c62005-07-18 10:32:59 +00002643
Paul Fox8552aec2005-09-16 12:20:05 +00002644 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2645 columns : STATUS_BUFFER_LEN-1;
2646
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002647 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002648#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002649 "%c %s%s%s %d/%d %d%%",
2650#else
2651 "%c %s%s %d/%d %d%%",
2652#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002653 cmd_mode_indicator[cmd_mode & 3],
2654 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002655#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002656 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002657#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002658 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002659 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 Vlasenkob1759462008-06-20 20:20:54 +00002665#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002666}
2667
2668//----- Force refresh of all Lines -----------------------------
2669static void redraw(int full_screen)
2670{
2671 place_cursor(0, 0, FALSE); // put cursor in correct place
Denis Vlasenkob1759462008-06-20 20:20:54 +00002672 clear_to_eos(); // tell terminal to erase display
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002673 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002674 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002675 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002676 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002677}
2678
2679//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002680static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002681{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002682 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002683 int co;
2684 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002685 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002686
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002687 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002688 co = 0;
2689 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002690 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002691 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002692 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002693 if (c == '\n')
2694 break;
2695 if ((c & 0x80) && !Isprint(c)) {
2696 c = '.';
2697 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002698 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002699 if (c == '\t') {
2700 c = ' ';
2701 // co % 8 != 7
2702 while ((co % tabstop) != (tabstop - 1)) {
2703 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002704 }
2705 } else {
2706 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002707 if (c == 0x7f)
2708 c = '?';
2709 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002710 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002711 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002712 }
2713 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002714 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002715 // discard scrolled-off-to-the-left portion,
2716 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002717 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002718 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002719 co -= tabstop;
2720 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002721 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002722 if (src >= end)
2723 break;
2724 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002725 // check "short line, gigantic offset" case
2726 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002727 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 Lehmann6fdacc72002-08-21 13:02:24 +00002735}
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//
2742static void refresh(int full_screen)
2743{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002744#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002745
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002746 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002747 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002748
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002749 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002750 unsigned c = columns, r = rows;
Rob Landleye5e1a102006-06-21 01:15:36 +00002751 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002752 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 Lehmann6fdacc72002-08-21 13:02:24 +00002756 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 Vlasenkof882f082007-12-23 02:36:51 +00002762 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002763 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002764 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002765
2766 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002767 if (tp < end) {
2768 char *t = memchr(tp, '\n', end - tp);
2769 if (!t) t = end - 1;
2770 tp = t + 1;
2771 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002772
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002773 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002774 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002775 cs = 0;
2776 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002777 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00002784 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002785 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002786 changed = TRUE; // mark for redraw
2787 break;
2788 }
2789 }
2790
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002791 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002792 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002793 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002794 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00002802 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002803 changed = TRUE;
2804 }
2805
2806 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002807 if (cs < 0) cs = 0;
2808 if (ce > columns - 1) ce = columns - 1;
2809 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002810 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002811 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002812 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002813 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002814
2815 // move cursor to column of first change
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002816 //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 Lehmann6fdacc72002-08-21 13:02:24 +00002823
2824 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002825 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002826 }
2827 }
2828
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002829 place_cursor(crow, ccol, TRUE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002830
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002831 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002832#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002833}
2834
Eric Andersen3f980402001-04-04 17:31:15 +00002835//---------------------------------------------------------------------
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 Vlasenko4c9e9c42008-10-20 08:59:03 +00002857static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00002858{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002859 const char *msg = msg; // for compiler
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002860 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002861 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00002862 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002863 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002864 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00002865
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002866// c1 = c; // quiet the compiler
2867// cnt = yf = 0; // quiet the compiler
2868// msg = p = q = save_dot = buf; // quiet the compiler
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002869 memset(buf, '\0', 12);
Eric Andersenbff7a602001-11-17 07:15:43 +00002870
Paul Fox8552aec2005-09-16 12:20:05 +00002871 show_status_line();
2872
Eric Andersenbff7a602001-11-17 07:15:43 +00002873 /* if this is a cursor key, skip these checks */
2874 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002875 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 Andersenbff7a602001-11-17 07:15:43 +00002884 goto key_cmd_mode;
2885 }
2886
Eric Andersen3f980402001-04-04 17:31:15 +00002887 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002888 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002889 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00002890 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00002891 // 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 McGrath09adaca2002-12-02 21:18:10 +00002896 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00002897 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 Andersen1c0d3112001-04-16 15:46:44 +00002905 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002906 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00002907 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002908 if (1 <= c || Isprint(c)) {
2909 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00002910 }
2911 goto dc1;
2912 }
2913
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002914 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00002915 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00002916 //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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00002924#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00002925 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00002926 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00002927 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002928#endif
Eric Andersen822c3832001-05-07 17:37:43 +00002929 //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 Andersenc7bda1c2004-03-15 08:29:22 +00002936 //case '!': // !-
2937 //case '#': // #-
2938 //case '&': // &-
2939 //case '(': // (-
2940 //case ')': // )-
2941 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002942 //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 Andersen1c0d3112001-04-16 15:46:44 +00002955 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002956 //case 'v': // v-
Eric Andersen3f980402001-04-04 17:31:15 +00002957 default: // unrecognised command
2958 buf[0] = c;
2959 buf[1] = '\0';
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002960 if (c < ' ') {
Eric Andersen3f980402001-04-04 17:31:15 +00002961 buf[0] = '^';
2962 buf[1] = c + '@';
2963 buf[2] = '\0';
2964 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002965 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00002966 end_cmd_q(); // stop adding to q
2967 case 0x00: // nul- ignore
2968 break;
2969 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002970 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00002971 dot_scroll(rows - 2, -1);
2972 break;
Eric Andersen3f980402001-04-04 17:31:15 +00002973 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 Vlasenko0112ff52008-10-25 23:23:00 +00002980 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00002981 dot_scroll(rows - 2, 1);
2982 break;
2983 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00002984 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00002985 break;
2986 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002987 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00002988 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00002989 case 0x7f: // DEL- move left (This may be ERASE char)
Eric Andersen3f980402001-04-04 17:31:15 +00002990 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 Vlasenko0112ff52008-10-25 23:23:00 +00002997 case KEYCODE_DOWN: // cursor key Down
Eric Andersen3f980402001-04-04 17:31:15 +00002998 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 Andersen1c0d3112001-04-16 15:46:44 +00003005 case 18: // ctrl-R force redraw
Eric Andersen822c3832001-05-07 17:37:43 +00003006 place_cursor(0, 0, FALSE); // put cursor in correct place
Eric Andersen3f980402001-04-04 17:31:15 +00003007 clear_to_eos(); // tel terminal to erase display
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003008 mysleep(10);
Eric Andersen3f980402001-04-04 17:31:15 +00003009 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003010 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003011 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 Andersen822c3832001-05-07 17:37:43 +00003027 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003028 if (cmd_mode == 0)
3029 indicate_error(c);
3030 cmd_mode = 0; // stop insrting
3031 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003032 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003033 break;
3034 case ' ': // move right
3035 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003036 case KEYCODE_RIGHT: // Cursor Key Right
Eric Andersen3f980402001-04-04 17:31:15 +00003037 if (cmdcnt-- > 1) {
3038 do_cmd(c);
3039 } // repeat cnt
3040 dot_right();
3041 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003042#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003043 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003044 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3045 if ((unsigned)c1 <= 25) { // a-z?
3046 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003047 } else {
3048 indicate_error(c);
3049 }
3050 break;
3051 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003052 c1 = (get_one_char() | 0x20) - 'a';
3053 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003054 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003055 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003056 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 Vlasenko4c9e9c42008-10-20 08:59:03 +00003074 c1 = (get_one_char() | 0x20) - 'a';
3075 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003076 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003077 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003078 } 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 Vlasenko88adfcd2007-12-22 15:40:13 +00003086 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003087 break;
3088 }
3089 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003090 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003091 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 Andersen3f980402001-04-04 17:31:15 +00003109 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00003119#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003120 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003121 case KEYCODE_END: // Cursor Key End
Eric Andersen3f980402001-04-04 17:31:15 +00003122 if (cmdcnt-- > 1) {
3123 do_cmd(c);
3124 } // repeat cnt
Glenn L McGrathee829062004-01-21 10:59:45 +00003125 dot = end_line(dot);
Eric Andersen3f980402001-04-04 17:31:15 +00003126 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 Andersenaff114c2004-04-14 17:51:38 +00003146 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003147 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003148 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003149 case ';': // ;- look at rest of line for last forward char
3150 if (cmdcnt-- > 1) {
Eric Andersen822c3832001-05-07 17:37:43 +00003151 do_cmd(';');
Eric Andersen3f980402001-04-04 17:31:15 +00003152 } // repeat cnt
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003153 if (last_forward_char == 0)
3154 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003155 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 Foxb5ee8db2008-02-14 01:17:01 +00003162 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 Andersen3f980402001-04-04 17:31:15 +00003176 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00003183#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003184 case '.': // .- repeat the last modifying command
3185 // Stuff the last_modifying_cmd back into stdin
3186 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003187 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003188 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003189 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003190 }
3191 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003192#endif
3193#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003194 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 Fox4917c112008-03-05 16:44:02 +00003199 if (q[0] && !q[1]) {
3200 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003201 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003202 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003203 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003204 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003205 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003206 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003207 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003208 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00003230 dc3:
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003231 dir = FORWARD; // assume FORWARD search
3232 p = dot + 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003233 if (last_search_pattern[0] == '?') {
3234 dir = BACK;
3235 p = dot - 1;
3236 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003237 dc4:
Eric Andersen3f980402001-04-04 17:31:15 +00003238 q = char_search(p, last_search_pattern + 1, dir, FULL);
3239 if (q != NULL) {
3240 dot = q; // good search, update "dot"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003241 msg = "";
Eric Andersen3f980402001-04-04 17:31:15 +00003242 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00003252 msg = "search hit BOTTOM, continuing at TOP";
Eric Andersen3f980402001-04-04 17:31:15 +00003253 if (dir == BACK) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003254 msg = "search hit TOP, continuing at BOTTOM";
Eric Andersen3f980402001-04-04 17:31:15 +00003255 }
3256 } else {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003257 msg = "Pattern not found";
Eric Andersen3f980402001-04-04 17:31:15 +00003258 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003259 dc2:
3260 if (*msg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003261 status_line_bold("%s", msg);
Eric Andersen3f980402001-04-04 17:31:15 +00003262 break;
3263 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003264 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003265 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00003270 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003271 if (q != NULL) { // found blank line
3272 dot = next_line(q); // move to next blank line
3273 }
3274 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003275#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003276 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003277 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 Andersen3f980402001-04-04 17:31:15 +00003286 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00003293 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003294#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003295 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003296#else
Eric Andersen822c3832001-05-07 17:37:43 +00003297 if (*p == ':')
3298 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003299 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003300 if (cnt <= 0)
3301 break;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003302 if (strncasecmp(p, "quit", cnt) == 0
3303 || strncasecmp(p, "q!", cnt) == 0 // delete lines
3304 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003305 if (file_modified && p[1] != '!') {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003306 status_line_bold("No write since last change (:quit! overrides)");
Eric Andersen3f980402001-04-04 17:31:15 +00003307 } else {
3308 editing = 0;
3309 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003310 } 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 Vlasenkoeaabf062007-07-17 23:14:07 +00003315 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003316 if (cnt < 0) {
3317 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003318 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003319 } else {
3320 file_modified = 0;
3321 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003322 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003323 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3324 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3325 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003326 editing = 0;
3327 }
Eric Andersen3f980402001-04-04 17:31:15 +00003328 }
Denis Vlasenko219d14d2007-03-24 15:40:16 +00003329 } else if (strncasecmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003330 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003331 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003332 dot = find_line(j); // go to line # j
3333 dot_skip_over_ws();
Eric Andersen3f980402001-04-04 17:31:15 +00003334 } else { // unrecognised cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003335 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003336 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003337#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003338 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00003344 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003345 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00003353 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003354 } else if (*p == ' ') {
3355 // we should be calculating columns, not just SPACE
3356 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003357 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003358 }
3359 }
3360 } else if (c == '>') {
3361 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003362 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003363 }
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-Fischera985d302008-02-11 11:44:38 +00003371 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003372 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00003401#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003402 if (c == 'D')
3403 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003404#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003405 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003406 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003407 c1 = get_one_char();
3408 if (c1 != 'g') {
3409 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003410 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003411 buf[2] = '\0';
3412 not_implemented(buf);
3413 break;
3414 }
3415 if (cmdcnt == 0)
3416 cmdcnt = 1;
3417 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003418 case 'G': // G- goto to a line number (default= E-O-F)
3419 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003420 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003421 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003422 }
3423 dot_skip_over_ws();
3424 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003425 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-Fischera985d302008-02-11 11:44:38 +00003438 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003439 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003440 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003441 dc_i:
Eric Andersen3f980402001-04-04 17:31:15 +00003442 cmd_mode = 1; // start insrting
Eric Andersen3f980402001-04-04 17:31:15 +00003443 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 Fox8552aec2005-09-16 12:20:05 +00003451 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003452 while (isblank(*dot)) { // delete leading WS
Eric Andersen3f980402001-04-04 17:31:15 +00003453 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 Andersen822c3832001-05-07 17:37:43 +00003469 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003470 dot = screenbegin;
3471 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3472 dot = next_line(dot);
3473 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003474 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003475 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003476 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 Andersen822c3832001-05-07 17:37:43 +00003484 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003485 dot_prev(); // -
3486 }
3487 goto dc_i;
3488 break;
3489 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003490 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003491 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003492 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003493 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003494 c = 'x';
3495 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003496 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 Foxf0305b72006-03-28 14:18:21 +00003521 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003522 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003523 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003524 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003525 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003526 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003527 if (cnt < 0) {
3528 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003529 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003530 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003531 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00003563#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003564 case 'y': // y- yank something
3565 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003566#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003567 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003568 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003569 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003570#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003571 if (c == 'y' || c == 'Y')
3572 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003573#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003574 c1 = 'y';
3575 if (c != 'Y')
3576 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003577 // determine range, and whether it spans lines
3578 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003579 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 Vlasenkoeaabf062007-07-17 23:14:07 +00003584 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003585 if (q <= text || q[-1] == '\n')
3586 break;
3587 q--;
3588 }
3589 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003590 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 Andersen1c0d3112001-04-16 15:46:44 +00003605 if (c == 'c') {
3606 dot = char_insert(dot, '\n');
3607 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003608 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003609 dot_prev();
3610 }
3611 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003612 dot_begin();
3613 dot_skip_over_ws();
3614 }
Eric Andersen3f980402001-04-04 17:31:15 +00003615 }
3616 if (c1 != 27) {
3617 // if CHANGING, not deleting, start inserting after the delete
3618 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003619 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003620 goto dc_i; // start inserting
3621 }
3622 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003623 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003624 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003625#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003626 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003627 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003628 }
3629 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003630 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003631 for (cnt = 0; p <= q; p++) {
3632 if (*p == '\n')
3633 cnt++;
3634 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003635 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003636 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003637#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003638 end_cmd_q(); // stop adding to q
3639 }
3640 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003641 }
Eric Andersen3f980402001-04-04 17:31:15 +00003642 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003643 case KEYCODE_UP: // cursor key Up
Eric Andersen3f980402001-04-04 17:31:15 +00003644 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 Vlasenkob1759462008-06-20 20:20:54 +00003654 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003655 }
3656 end_cmd_q(); // stop adding to q
3657 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003658 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003659 last_forward_char = get_one_char();
3660 do_cmd(';');
3661 if (*dot == last_forward_char)
3662 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003663 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003664 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003665 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 Vlasenkob1759462008-06-20 20:20:54 +00003699 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003700 } else if (isupper(*dot)) {
3701 *dot = tolower(*dot);
Denis Vlasenkob1759462008-06-20 20:20:54 +00003702 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003703 }
3704 dot_right();
3705 end_cmd_q(); // stop adding to q
3706 break;
3707 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003708 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003709 dot_begin();
3710 break;
3711 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003712#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 Andersen3f980402001-04-04 17:31:15 +00003725 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003726#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003727 }
3728
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003729 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003730 // if text[] just became empty, add back an empty line
3731 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003732 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003733 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 Vlasenko6a5dc5d2006-12-30 18:42:29 +00003739#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003740 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003741#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003742
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 McGrath09adaca2002-12-02 21:18:10 +00003750
Paul Fox35e9c5d2008-03-06 16:26:12 +00003751/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003752#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003753static int totalcmds = 0;
3754static int Mp = 85; // Movement command Probability
3755static int Np = 90; // Non-movement command Probability
3756static int Dp = 96; // Delete command Probability
3757static int Ip = 97; // Insert command Probability
3758static int Yp = 98; // Yank command Probability
3759static int Pp = 99; // Put command Probability
3760static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003761static const char chars[20] = "\t012345 abcdABCD-=.$";
3762static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003763 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003764 "broadcast", "the", "emergency", "of",
3765 "system", "quick", "brown", "fox",
3766 "jumped", "over", "lazy", "dogs",
3767 "back", "January", "Febuary", "March"
3768};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003769static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003770 "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 Vlasenko88adfcd2007-12-22 15:40:13 +00003791static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003792 "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
3815static 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 Vlasenko26b6fba2007-12-21 21:34:37 +00003825 if (chars_to_parse > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003826 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003827 cd0:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003828 startrbi = rbi = 0;
3829 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003830 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003831 // 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 Vlasenkoafa37cf2007-03-21 00:05:35 +00003889 strcat(readbuffer, words[(int) lrand48() % 20]);
3890 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003891 sleeptime = 0; // how fast to type
3892 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003893 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003894 sleeptime = 0; // how fast to type
3895 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003896 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003897 sleeptime = 0; // how fast to type
3898 }
3899 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003900 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003901 }
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003902 chars_to_parse = strlen(readbuffer);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003903 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003904 totalcmds++;
3905 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003906 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003907}
3908
3909// test to see if there are any errors
3910static void crash_test()
3911{
3912 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003913
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003914 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003915 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003916
3917 msg[0] = '\0';
3918 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003919 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003920 }
3921 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003922 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003923 }
3924 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003925 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003926 }
3927 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003928 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003929 }
3930 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003931 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003932 }
3933 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003934 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003935 }
3936
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003937 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00003938 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003939 totalcmds, last_input_char, msg, SOs, SOn);
3940 fflush(stdout);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00003941 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003942 if (d[0] == '\n' || d[0] == '\r')
3943 break;
3944 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003945 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003946 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003947 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003948 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003949 "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 McGrath09adaca2002-12-02 21:18:10 +00003953}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003954#endif