blob: 9f9a199fa153dbb5f38dbb2882539034ee54d6b7 [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 Vlasenkoc3a9dc82008-10-29 00:58:04 +0000273 last_search_pattern = xzalloc(2); /* "" but has space for 2 chars */ \
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000274} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000275
Denis Vlasenkob1759462008-06-20 20:20:54 +0000276
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000277static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000278static void edit_file(char *); // edit one file
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000279static void do_cmd(int); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000280static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000281static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
282static char *begin_line(char *); // return pointer to cur line B-o-l
283static char *end_line(char *); // return pointer to cur line E-o-l
284static char *prev_line(char *); // return pointer to prev line B-o-l
285static char *next_line(char *); // return pointer to next line B-o-l
286static char *end_screen(void); // get pointer to last char on screen
287static int count_lines(char *, char *); // count line from start to stop
288static char *find_line(int); // find begining of line #li
289static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000290static void dot_left(void); // move dot left- dont leave line
291static void dot_right(void); // move dot right- dont leave line
292static void dot_begin(void); // move dot to B-o-l
293static void dot_end(void); // move dot to E-o-l
294static void dot_next(void); // move dot to next line B-o-l
295static void dot_prev(void); // move dot to prev line B-o-l
296static void dot_scroll(int, int); // move the screen up or down
297static void dot_skip_over_ws(void); // move dot pat WS
298static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000299static char *bound_dot(char *); // make sure text[0] <= P < "end"
300static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000301static char *char_insert(char *, char); // insert the char c at 'p'
302static char *stupid_insert(char *, char); // stupidly insert the char c at 'p'
Paul Foxc51fc7b2008-03-06 01:34:23 +0000303static int find_range(char **, char **, char); // return pointers for an object
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000304static int st_test(char *, int, int, char *); // helper for skip_thing()
305static char *skip_thing(char *, int, int, int); // skip some object
306static char *find_pair(char *, char); // find matching pair () [] {}
307static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
308static char *text_hole_make(char *, int); // at "p", make a 'size' byte hole
309static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000310static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000311static void rawmode(void); // set "raw" mode on tty
312static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000313// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
314static int mysleep(int);
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000315static int readit(void); // read (maybe cursor) key from stdin
316static int get_one_char(void); // read 1 char from stdin
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000317static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000318#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000319static int file_insert(const char *, char *, int);
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000320#else
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000321static int file_insert(const char *, char *);
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000322#endif
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000323static int file_write(char *, char *, char *);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000324#if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
325#define place_cursor(a, b, optimize) place_cursor(a, b)
326#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000327static void place_cursor(int, int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000328static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000329static void clear_to_eol(void);
330static void clear_to_eos(void);
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000331static void go_bottom_and_clear_to_eol(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000332static void standout_start(void); // send "start reverse video" sequence
333static void standout_end(void); // send "end reverse video" sequence
334static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000335static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000336static void status_line(const char *, ...); // print to status buf
337static void status_line_bold(const char *, ...);
338static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000339static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000340static void redraw(int); // force a full screen refresh
Denis Vlasenko68404f12008-03-17 09:00:54 +0000341static char* format_line(char* /*, int*/);
Eric Andersen3f980402001-04-04 17:31:15 +0000342static void refresh(int); // update the terminal from screen[]
343
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000344static void Indicate_Error(void); // use flash or beep to indicate error
345#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000346static void Hit_Return(void);
347
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000348#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000349static char *char_search(char *, const char *, int, int); // search for pattern starting at p
350static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000351#endif
352#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000353static char *get_one_address(char *, int *); // get colon addr, if present
354static char *get_address(char *, int *, int *); // get two colon addrs, if present
355static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000356#endif
357#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000358static void winch_sig(int); // catch window size changes
359static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000360static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000361#endif
362#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000363static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000364static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000365#else
366#define end_cmd_q() ((void)0)
367#endif
368#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000369static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000370#endif
371#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000372static char *string_insert(char *, char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000373#endif
374#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000375static char *text_yank(char *, char *, int); // save copy of "p" into a register
376static char what_reg(void); // what is letter of current YDreg
377static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000378#endif
379#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000380static void crash_dummy();
381static void crash_test();
382static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000383#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000384
385
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000386static void write1(const char *out)
387{
388 fputs(out, stdout);
389}
390
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000391int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000392int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000393{
Eric Andersend402edf2001-04-04 19:29:48 +0000394 int c;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000395
396 INIT_G();
Eric Andersen3f980402001-04-04 17:31:15 +0000397
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000398#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000399 my_pid = getpid();
400#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000401#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000402 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000403#endif
Denis Vlasenko2414a962007-07-18 22:03:40 +0000404#ifdef NO_SUCH_APPLET_YET
405 /* If we aren't "vi", we are "view" */
406 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000407 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000408 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000409#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000410
Bernhard Reutner-Fischer73f56bb2007-09-22 21:18:46 +0000411 vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenkof9234132007-03-21 00:03:42 +0000412 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000413 // 2- process EXINIT variable from environment
414 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000415#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000416 {
417 char *p = getenv("EXINIT");
418 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000419 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000420 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000421#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000422 while ((c = getopt(argc, argv, "hCRH" USE_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000423 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000424#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000425 case 'C':
426 crashme = 1;
427 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000428#endif
429#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000430 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000431 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000432 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000433#endif
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000434#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000435 case 'c': // cmd line vi command
436 if (*optarg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000437 initial_cmds[initial_cmds[0] != 0] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000438 break;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000439#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000440 case 'H':
Eric Andersen3f980402001-04-04 17:31:15 +0000441 show_help();
Paul Fox35e9c5d2008-03-06 16:26:12 +0000442 /* fall through */
Paul Fox35e9c5d2008-03-06 16:26:12 +0000443 default:
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000444 bb_show_usage();
Eric Andersendd8500b2001-07-02 18:06:14 +0000445 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000446 }
447 }
448
449 // The argv array can be used by the ":next" and ":rewind" commands
450 // save optind.
451 fn_start = optind; // remember first file name for :next and :rew
452 save_argc = argc;
453
454 //----- This is the main file handling loop --------------
455 if (optind >= argc) {
Eric Andersen3f980402001-04-04 17:31:15 +0000456 edit_file(0);
457 } else {
458 for (; optind < argc; optind++) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000459 edit_file(argv[optind]);
Eric Andersen3f980402001-04-04 17:31:15 +0000460 }
461 }
462 //-----------------------------------------------------------
463
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000464 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000465}
466
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000467/* read text from file or create an empty buf */
468/* will also update current_filename */
469static int init_text_buffer(char *fn)
470{
471 int rc;
472 int size = file_size(fn); // file size. -1 means does not exist.
473
474 /* allocate/reallocate text buffer */
475 free(text);
Denis Vlasenkob1759462008-06-20 20:20:54 +0000476 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000477 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000478
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000479 if (fn != current_filename) {
480 free(current_filename);
481 current_filename = xstrdup(fn);
482 }
483 if (size < 0) {
484 // file dont exist. Start empty buf with dummy line
485 char_insert(text, '\n');
486 rc = 0;
487 } else {
488 rc = file_insert(fn, text
489 USE_FEATURE_VI_READONLY(, 1));
490 }
491 file_modified = 0;
492 last_file_modified = -1;
493#if ENABLE_FEATURE_VI_YANKMARK
494 /* init the marks. */
495 memset(mark, 0, sizeof(mark));
496#endif
497 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000498}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000499
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000500static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000501{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000502#if ENABLE_FEATURE_VI_YANKMARK
503#define cur_line edit_file__cur_line
504#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000505 int c;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000506 int size;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000507#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000508 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000509#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000510
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000511 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000512 rawmode();
513 rows = 24;
514 columns = 80;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000515 size = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000516 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Rob Landleye5e1a102006-06-21 01:15:36 +0000517 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000518 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
519 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
520 }
Eric Andersen3f980402001-04-04 17:31:15 +0000521 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000522 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000523
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000524#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000525 YDreg = 26; // default Yank/Delete reg
526 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000527 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000528#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000529
Eric Andersen3f980402001-04-04 17:31:15 +0000530 last_forward_char = last_input_char = '\0';
531 crow = 0;
532 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000533
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000534#if ENABLE_FEATURE_VI_USE_SIGNALS
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000535 catch_sig(0);
Eric Andersen3f980402001-04-04 17:31:15 +0000536 signal(SIGWINCH, winch_sig);
537 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000538 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000539 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000540 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000541 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000542#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000543
Eric Andersen3f980402001-04-04 17:31:15 +0000544 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
545 cmdcnt = 0;
546 tabstop = 8;
547 offset = 0; // no horizontal offset
548 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000549#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000550 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000551 ioq = ioq_start = NULL;
552 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000553 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000554#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000555
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000556#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000557 {
558 char *p, *q;
559 int n = 0;
560
561 while ((p = initial_cmds[n])) {
562 do {
563 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000564 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000565 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000566 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000567 *p++ = '\0';
568 if (*q)
569 colon(q);
570 } while (p);
571 free(initial_cmds[n]);
572 initial_cmds[n] = NULL;
573 n++;
574 }
575 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000576#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000577 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000578 //------This is the main Vi cmd handling loop -----------------------
579 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000580#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000581 if (crashme > 0) {
582 if ((end - text) > 1) {
583 crash_dummy(); // generate a random command
584 } else {
585 crashme = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000586 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 +0000587 refresh(FALSE);
588 }
589 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000590#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000591 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000592#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000593 // save a copy of the current line- for the 'U" command
594 if (begin_line(dot) != cur_line) {
595 cur_line = begin_line(dot);
596 text_yank(begin_line(dot), end_line(dot), Ureg);
597 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000598#endif
599#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000600 // These are commands that change text[].
601 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000602 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000603 && cmd_mode == 0 // command mode
604 && c > '\0' // exclude NUL and non-ASCII chars
605 && c < 0x7f // (Unicode and such)
606 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000607 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000608 start_new_cmd_q(c);
609 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000610#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000611 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000612
Eric Andersen3f980402001-04-04 17:31:15 +0000613 // poll to see if there is input already waiting. if we are
614 // not able to display output fast enough to keep up, skip
615 // the display update until we catch up with input.
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000616 if (!chars_to_parse && mysleep(0) == 0) {
617 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000618 refresh(FALSE);
619 show_status_line();
620 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000621#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000622 if (crashme > 0)
623 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000624#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000625 }
626 //-------------------------------------------------------------------
627
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000628 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000629 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000630#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000631}
632
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000633//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000634#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000635static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000636{
637 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000638 char *q;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000639 USE_FEATURE_VI_YANKMARK(char c;)
640 USE_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000641
642 *addr = -1; // assume no addr
643 if (*p == '.') { // the current line
644 p++;
645 q = begin_line(dot);
646 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000647 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000648#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000649 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000650 p++;
651 c = tolower(*p);
652 p++;
653 if (c >= 'a' && c <= 'z') {
654 // we have a mark
655 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000656 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000657 if (q != NULL) { // is mark valid
658 *addr = count_lines(text, q); // count lines
659 }
660 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000661 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000662#endif
663#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000664 else if (*p == '/') { // a search pattern
665 q = strchrnul(++p, '/');
666 pat = xstrndup(p, q - p); // save copy of pattern
667 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000668 if (*p == '/')
669 p++;
670 q = char_search(dot, pat, FORWARD, FULL);
671 if (q != NULL) {
672 *addr = count_lines(text, q);
673 }
674 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000675 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000676#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000677 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000678 p++;
679 q = begin_line(end - 1);
680 *addr = count_lines(text, q);
681 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000682 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000683 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000684 } else {
685 // unrecognised address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000686 *addr = -1;
687 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000688 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000689}
690
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000691static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000692{
693 //----- get the address' i.e., 1,3 'a,'b -----
694 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000695 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000696 p++; // skip over leading spaces
697 if (*p == '%') { // alias for 1,$
698 p++;
699 *b = 1;
700 *e = count_lines(text, end-1);
701 goto ga0;
702 }
703 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000704 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000705 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000706 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000707 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000708 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000709 p++;
710 // get SECOND addr, if present
711 p = get_one_address(p, e);
712 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000713 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000714 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000715 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000716 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000717}
718
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000719#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000720static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000721 const char *short_opname, int opt)
722{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000723 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000724 int l = strlen(opname) - 1; /* opname have + ' ' */
725
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000726 if (strncasecmp(a, opname, l) == 0
727 || strncasecmp(a, short_opname, 2) == 0
728 ) {
729 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000730 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000731 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000732 vi_setops |= opt;
733 }
734}
735#endif
736
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000737// buf must be no longer than MAX_INPUT_LEN!
738static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000739{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000740 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000741 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000742 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000743 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000744
745 // :3154 // if (-e line 3154) goto it else stay put
746 // :4,33w! foo // write a portion of buffer to file "foo"
747 // :w // write all of buffer to current file
748 // :q // quit
749 // :q! // quit- dont care about modified file
750 // :'a,'z!sort -u // filter block through sort
751 // :'f // goto mark "f"
752 // :'fl // list literal the mark "f" line
753 // :.r bar // read file "bar" into buffer before dot
754 // :/123/,/abc/d // delete lines from "123" line to "abc" line
755 // :/xyz/ // goto the "xyz" line
756 // :s/find/replace/ // substitute pattern "find" with "replace"
757 // :!<cmd> // run <cmd> then return
758 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000759
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000760 if (!buf[0])
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000761 goto vc1;
762 if (*buf == ':')
763 buf++; // move past the ':'
764
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000765 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000766 b = e = -1;
767 q = text; // assume 1,$ for the range
768 r = end - 1;
769 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000770 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000771
772 // look for optional address(es) :. :1 :1,9 :'q,'a :%
773 buf = get_address(buf, &b, &e);
774
775 // remember orig command line
776 orig_buf = buf;
777
778 // get the COMMAND into cmd[]
779 buf1 = cmd;
780 while (*buf != '\0') {
781 if (isspace(*buf))
782 break;
783 *buf1++ = *buf++;
784 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000785 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000786 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000787 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000788 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000789 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000790 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000791 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000792 if (buf1) {
793 useforce = TRUE;
794 *buf1 = '\0'; // get rid of !
795 }
796 if (b >= 0) {
797 // if there is only one addr, then the addr
798 // is the line number of the single line the
799 // user wants. So, reset the end
800 // pointer to point at end of the "b" line
801 q = find_line(b); // what line is #b
802 r = end_line(q);
803 li = 1;
804 }
805 if (e >= 0) {
806 // we were given two addrs. change the
807 // end pointer to the addr given by user.
808 r = find_line(e); // what line is #e
809 r = end_line(r);
810 li = e - b + 1;
811 }
812 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000813 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000814 if (i == 0) { // :123CR goto line #123
815 if (b >= 0) {
816 dot = find_line(b); // what line is #b
817 dot_skip_over_ws();
818 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000819 }
820#if ENABLE_FEATURE_ALLOW_EXEC
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000821 else if (strncmp(cmd, "!", 1) == 0) { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000822 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000823 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000824 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000825 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000826 retcode = system(orig_buf + 1); // run the cmd
827 if (retcode)
828 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000829 rawmode();
830 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000831 }
832#endif
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000833 else if (strncmp(cmd, "=", i) == 0) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000834 if (b < 0) { // no addr given- use defaults
835 b = e = count_lines(text, dot);
836 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000837 status_line("%d", b);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000838 } else if (strncasecmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000839 if (b < 0) { // no addr given- use defaults
840 q = begin_line(dot); // assume .,. for the range
841 r = end_line(dot);
842 }
843 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
844 dot_skip_over_ws();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000845 } else if (strncasecmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000846 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000847 if (file_modified && !useforce) {
848 status_line_bold("No write since last change (:edit! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000849 goto vc1;
850 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000851 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000852 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000853 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000854 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000855 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000856 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000857 } else {
858 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000859 status_line_bold("No current filename");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000860 goto vc1;
861 }
862
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000863 if (init_text_buffer(fn) < 0)
864 goto vc1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000865
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000866#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000867 if (Ureg >= 0 && Ureg < 28 && reg[Ureg] != 0) {
868 free(reg[Ureg]); // free orig line reg- for 'U'
869 reg[Ureg]= 0;
870 }
871 if (YDreg >= 0 && YDreg < 28 && reg[YDreg] != 0) {
872 free(reg[YDreg]); // free default yank/delete register
873 reg[YDreg]= 0;
874 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000875#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000876 // how many lines in text[]?
877 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000878 status_line("\"%s\"%s"
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000879 USE_FEATURE_VI_READONLY("%s")
880 " %dL, %dC", current_filename,
881 (file_size(fn) < 0 ? " [New file]" : ""),
882 USE_FEATURE_VI_READONLY(
883 ((readonly_mode) ? " [Readonly]" : ""),
884 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000885 li, ch);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000886 } else if (strncasecmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000887 if (b != -1 || e != -1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000888 not_implemented("No address allowed on this command");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000889 goto vc1;
890 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000891 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000892 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000893 free(current_filename);
894 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000895 } else {
896 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +0000897 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000898 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000899 } else if (strncasecmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000900 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000901 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000902 cookmode();
903 show_help();
904 rawmode();
905 Hit_Return();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000906 } else if (strncasecmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000907 if (b < 0) { // no addr given- use defaults
908 q = begin_line(dot); // assume .,. for the range
909 r = end_line(dot);
910 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000911 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000912 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000913 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000914 int c_is_no_print;
915
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000916 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +0000917 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000918 if (c_is_no_print) {
919 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000920 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000921 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000922 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000923 write1("$\r");
924 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000925 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000926 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000927 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000928 else
929 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000930 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000931 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000932 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000933 standout_end();
934 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000935#if ENABLE_FEATURE_VI_SET
936 vc2:
937#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000938 Hit_Return();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000939 } else if (strncasecmp(cmd, "quit", i) == 0 // Quit
940 || strncasecmp(cmd, "next", i) == 0 // edit next file
941 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000942 if (useforce) {
943 // force end of argv list
944 if (*cmd == 'q') {
945 optind = save_argc;
946 }
947 editing = 0;
948 goto vc1;
949 }
950 // don't exit if the file been modified
951 if (file_modified) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000952 status_line_bold("No write since last change (:%s! overrides)",
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000953 (*cmd == 'q' ? "quit" : "next"));
954 goto vc1;
955 }
956 // are there other file to edit
957 if (*cmd == 'q' && optind < save_argc - 1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000958 status_line_bold("%d more file to edit", (save_argc - optind - 1));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000959 goto vc1;
960 }
961 if (*cmd == 'n' && optind >= save_argc - 1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000962 status_line_bold("No more files to edit");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000963 goto vc1;
964 }
965 editing = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000966 } else if (strncasecmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000967 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000968 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000969 status_line_bold("No filename given");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000970 goto vc1;
971 }
972 if (b < 0) { // no addr given- use defaults
973 q = begin_line(dot); // assume "dot"
974 }
975 // read after current line- unless user said ":0r foo"
976 if (b != 0)
977 q = next_line(q);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000978 ch = file_insert(fn, q USE_FEATURE_VI_READONLY(, 0));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000979 if (ch < 0)
980 goto vc1; // nothing was inserted
981 // how many lines in text[]?
982 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000983 status_line("\"%s\""
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000984 USE_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000985 " %dL, %dC", fn,
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000986 USE_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000987 li, ch);
988 if (ch > 0) {
989 // if the insert is before "dot" then we need to update
990 if (q <= dot)
991 dot += ch;
Paul Fox8552aec2005-09-16 12:20:05 +0000992 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000993 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000994 } else if (strncasecmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000995 if (file_modified && !useforce) {
996 status_line_bold("No write since last change (:rewind! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000997 } else {
998 // reset the filenames to edit
999 optind = fn_start - 1;
1000 editing = 0;
1001 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001002#if ENABLE_FEATURE_VI_SET
Denis Vlasenkof9234132007-03-21 00:03:42 +00001003 } else if (strncasecmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001004#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001005 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001006#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001007 i = 0; // offset into args
Denis Vlasenkof9234132007-03-21 00:03:42 +00001008 // only blank is regarded as args delmiter. What about tab '\t' ?
1009 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001010 // print out values of all options
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001011 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001012 printf("----------------------------------------\r\n");
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001013#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001014 if (!autoindent)
1015 printf("no");
1016 printf("autoindent ");
1017 if (!err_method)
1018 printf("no");
1019 printf("flash ");
1020 if (!ignorecase)
1021 printf("no");
1022 printf("ignorecase ");
1023 if (!showmatch)
1024 printf("no");
1025 printf("showmatch ");
1026 printf("tabstop=%d ", tabstop);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001027#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001028 printf("\r\n");
1029 goto vc2;
1030 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001031#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001032 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001033 while (*argp) {
Denis Vlasenkof9234132007-03-21 00:03:42 +00001034 if (strncasecmp(argp, "no", 2) == 0)
1035 i = 2; // ":set noautoindent"
1036 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
1037 setops(argp, "flash ", i, "fl", VI_ERR_METHOD);
1038 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
1039 setops(argp, "showmatch ", i, "ic", VI_SHOWMATCH);
1040 /* tabstopXXXX */
1041 if (strncasecmp(argp + i, "tabstop=%d ", 7) == 0) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001042 sscanf(strchr(argp + i, '='), "tabstop=%d" + 7, &ch);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00001043 if (ch > 0 && ch <= MAX_TABSTOP)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001044 tabstop = ch;
1045 }
1046 while (*argp && *argp != ' ')
1047 argp++; // skip to arg delimiter (i.e. blank)
1048 while (*argp && *argp == ' ')
1049 argp++; // skip all delimiting blanks
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001050 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001051#endif /* FEATURE_VI_SETOPTS */
1052#endif /* FEATURE_VI_SET */
1053#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001054 } else if (strncasecmp(cmd, "s", 1) == 0) { // substitute a pattern with a replacement pattern
1055 char *ls, *F, *R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001056 int gflag;
1057
1058 // F points to the "find" pattern
1059 // R points to the "replace" pattern
1060 // replace the cmd line delimiters "/" with NULLs
1061 gflag = 0; // global replace flag
1062 c = orig_buf[1]; // what is the delimiter
1063 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001064 R = strchr(F, c); // middle delimiter
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001065 if (!R) goto colon_s_fail;
1066 *R++ = '\0'; // terminate "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001067 buf1 = strchr(R, c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001068 if (!buf1) goto colon_s_fail;
1069 *buf1++ = '\0'; // terminate "replace"
1070 if (*buf1 == 'g') { // :s/foo/bar/g
1071 buf1++;
1072 gflag++; // turn on gflag
1073 }
1074 q = begin_line(q);
1075 if (b < 0) { // maybe :s/foo/bar/
1076 q = begin_line(dot); // start with cur line
1077 b = count_lines(text, q); // cur line number
1078 }
1079 if (e < 0)
1080 e = b; // maybe :.s/foo/bar/
1081 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
1082 ls = q; // orig line start
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001083 vc4:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001084 buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001085 if (buf1) {
1086 // we found the "find" pattern - delete it
1087 text_hole_delete(buf1, buf1 + strlen(F) - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001088 // inset the "replace" patern
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001089 string_insert(buf1, R); // insert the string
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001090 // check for "global" :s/foo/bar/g
1091 if (gflag == 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001092 if ((buf1 + strlen(R)) < end_line(ls)) {
1093 q = buf1 + strlen(R);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001094 goto vc4; // don't let q move past cur line
1095 }
1096 }
1097 }
1098 q = next_line(ls);
1099 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001100#endif /* FEATURE_VI_SEARCH */
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001101 } else if (strncasecmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001102 status_line(BB_VER " " BB_BT);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001103 } else if (strncasecmp(cmd, "write", i) == 0 // write text to file
1104 || strncasecmp(cmd, "wq", i) == 0
1105 || strncasecmp(cmd, "wn", i) == 0
1106 || strncasecmp(cmd, "x", i) == 0
1107 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001108 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001109 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001110 fn = args;
1111 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001112#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001113 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001114 status_line_bold("\"%s\" File is read only", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001115 goto vc3;
1116 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001117#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001118 // how many lines in text[]?
1119 li = count_lines(q, r);
1120 ch = r - q + 1;
1121 // see if file exists- if not, its just a new file request
1122 if (useforce) {
1123 // if "fn" is not write-able, chmod u+w
1124 // sprintf(syscmd, "chmod u+w %s", fn);
1125 // system(syscmd);
1126 forced = TRUE;
1127 }
1128 l = file_write(fn, q, r);
1129 if (useforce && forced) {
1130 // chmod u-w
1131 // sprintf(syscmd, "chmod u-w %s", fn);
1132 // system(syscmd);
1133 forced = FALSE;
1134 }
Paul Fox61e45db2005-10-09 14:43:22 +00001135 if (l < 0) {
1136 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001137 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001138 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001139 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001140 if (q == text && r == end - 1 && l == ch) {
1141 file_modified = 0;
1142 last_file_modified = -1;
1143 }
Paul Fox9360f422006-03-27 21:51:16 +00001144 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' ||
1145 cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N')
1146 && l == ch) {
Paul Fox61e45db2005-10-09 14:43:22 +00001147 editing = 0;
1148 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001149 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001150#if ENABLE_FEATURE_VI_READONLY
1151 vc3:;
1152#endif
1153#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001154 } else if (strncasecmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001155 if (b < 0) { // no addr given- use defaults
1156 q = begin_line(dot); // assume .,. for the range
1157 r = end_line(dot);
1158 }
1159 text_yank(q, r, YDreg);
1160 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001161 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001162 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001163#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001164 } else {
1165 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001166 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001167 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001168 vc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001169 dot = bound_dot(dot); // make sure "dot" is valid
1170 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001171#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001172 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001173 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001174#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001175}
Paul Fox61e45db2005-10-09 14:43:22 +00001176
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001177#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001178
1179static void Hit_Return(void)
1180{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001181 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001182
Denis Vlasenkof882f082007-12-23 02:36:51 +00001183 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001184 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001185 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001186 while ((c = get_one_char()) != '\n' && c != '\r')
1187 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001188 redraw(TRUE); // force redraw all
1189}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001190
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001191static int next_tabstop(int col)
1192{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001193 return col + ((tabstop - 1) - (col % tabstop));
1194}
1195
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001196//----- Synchronize the cursor to Dot --------------------------
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001197static void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001198{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001199 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001200 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001201 int cnt, ro, co;
1202
1203 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001204
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001205 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001206 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001207 // how many lines do we have to move
1208 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001209 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001210 screenbegin = beg_cur;
1211 if (cnt > (rows - 1) / 2) {
1212 // we moved too many lines. put "dot" in middle of screen
1213 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1214 screenbegin = prev_line(screenbegin);
1215 }
1216 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001217 } else {
1218 char *end_scr; // begin and end of screen
1219 end_scr = end_screen(); // last char of screen
1220 if (beg_cur > end_scr) {
1221 // "d" is after bottom line on screen
1222 // how many lines do we have to move
1223 cnt = count_lines(end_scr, beg_cur);
1224 if (cnt > (rows - 1) / 2)
1225 goto sc1; // too many lines
1226 for (ro = 0; ro < cnt - 1; ro++) {
1227 // move screen begin the same amount
1228 screenbegin = next_line(screenbegin);
1229 // now, move the end of screen
1230 end_scr = next_line(end_scr);
1231 end_scr = end_line(end_scr);
1232 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001233 }
1234 }
1235 // "d" is on screen- find out which row
1236 tp = screenbegin;
1237 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1238 if (tp == beg_cur)
1239 break;
1240 tp = next_line(tp);
1241 }
1242
1243 // find out what col "d" is on
1244 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001245 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001246 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001247 break;
1248 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001249 // handle tabs like real vi
1250 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001251 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001252 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001253 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001254 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1255 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001256 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001257 co++;
1258 tp++;
1259 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001260
1261 // "co" is the column where "dot" is.
1262 // The screen has "columns" columns.
1263 // The currently displayed columns are 0+offset -- columns+ofset
1264 // |-------------------------------------------------------------|
1265 // ^ ^ ^
1266 // offset | |------- columns ----------------|
1267 //
1268 // If "co" is already in this range then we do not have to adjust offset
1269 // but, we do have to subtract the "offset" bias from "co".
1270 // If "co" is outside this range then we have to change "offset".
1271 // If the first char of a line is a tab the cursor will try to stay
1272 // in column 7, but we have to set offset to 0.
1273
1274 if (co < 0 + offset) {
1275 offset = co;
1276 }
1277 if (co >= columns + offset) {
1278 offset = co - columns + 1;
1279 }
1280 // if the first char of the line is a tab, and "dot" is sitting on it
1281 // force offset to 0.
1282 if (d == beg_cur && *d == '\t') {
1283 offset = 0;
1284 }
1285 co -= offset;
1286
1287 *row = ro;
1288 *col = co;
1289}
1290
1291//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001292static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001293{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001294 if (p > text) {
1295 p = memrchr(text, '\n', p - text);
1296 if (!p)
1297 return text;
1298 return p + 1;
1299 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001300 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001301}
1302
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001303static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001304{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001305 if (p < end - 1) {
1306 p = memchr(p, '\n', end - p - 1);
1307 if (!p)
1308 return end - 1;
1309 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001310 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001311}
1312
Denis Vlasenkof882f082007-12-23 02:36:51 +00001313static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001314{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001315 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001316 // Try to stay off of the Newline
1317 if (*p == '\n' && (p - begin_line(p)) > 0)
1318 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001319 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001320}
1321
Denis Vlasenkof882f082007-12-23 02:36:51 +00001322static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001323{
1324 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001325 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001326 p--; // step to prev line
1327 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001328 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001329}
1330
Denis Vlasenkof882f082007-12-23 02:36:51 +00001331static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001332{
1333 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001334 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001335 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001336 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001337}
1338
1339//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001340static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001341{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001342 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001343 int cnt;
1344
1345 // find new bottom line
1346 q = screenbegin;
1347 for (cnt = 0; cnt < rows - 2; cnt++)
1348 q = next_line(q);
1349 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001350 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001351}
1352
Denis Vlasenkof882f082007-12-23 02:36:51 +00001353// count line from start to stop
1354static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001355{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001356 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001357 int cnt;
1358
Denis Vlasenkof882f082007-12-23 02:36:51 +00001359 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001360 q = start;
1361 start = stop;
1362 stop = q;
1363 }
1364 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001365 stop = end_line(stop);
1366 while (start <= stop && start <= end - 1) {
1367 start = end_line(start);
1368 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001369 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001370 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001371 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001372 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001373}
1374
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001375static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001376{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001377 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001378
1379 for (q = text; li > 1; li--) {
1380 q = next_line(q);
1381 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001382 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001383}
1384
1385//----- Dot Movement Routines ----------------------------------
1386static void dot_left(void)
1387{
1388 if (dot > text && dot[-1] != '\n')
1389 dot--;
1390}
1391
1392static void dot_right(void)
1393{
1394 if (dot < end - 1 && *dot != '\n')
1395 dot++;
1396}
1397
1398static void dot_begin(void)
1399{
1400 dot = begin_line(dot); // return pointer to first char cur line
1401}
1402
1403static void dot_end(void)
1404{
1405 dot = end_line(dot); // return pointer to last char cur line
1406}
1407
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001408static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001409{
1410 int co;
1411
1412 p = begin_line(p);
1413 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001414 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001415 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001416 break;
1417 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001418 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001419 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001420 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001421 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001422 co++;
1423 p++;
1424 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001425 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001426}
1427
1428static void dot_next(void)
1429{
1430 dot = next_line(dot);
1431}
1432
1433static void dot_prev(void)
1434{
1435 dot = prev_line(dot);
1436}
1437
1438static void dot_scroll(int cnt, int dir)
1439{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001440 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001441
1442 for (; cnt > 0; cnt--) {
1443 if (dir < 0) {
1444 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001445 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001446 screenbegin = prev_line(screenbegin);
1447 } else {
1448 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001449 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001450 screenbegin = next_line(screenbegin);
1451 }
1452 }
1453 // make sure "dot" stays on the screen so we dont scroll off
1454 if (dot < screenbegin)
1455 dot = screenbegin;
1456 q = end_screen(); // find new bottom line
1457 if (dot > q)
1458 dot = begin_line(q); // is dot is below bottom line?
1459 dot_skip_over_ws();
1460}
1461
1462static void dot_skip_over_ws(void)
1463{
1464 // skip WS
1465 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1466 dot++;
1467}
1468
1469static void dot_delete(void) // delete the char at 'dot'
1470{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001471 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001472}
1473
Denis Vlasenkof882f082007-12-23 02:36:51 +00001474static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001475{
1476 if (p >= end && end > text) {
1477 p = end - 1;
1478 indicate_error('1');
1479 }
1480 if (p < text) {
1481 p = text;
1482 indicate_error('2');
1483 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001484 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001485}
1486
1487//----- Helper Utility Routines --------------------------------
1488
1489//----------------------------------------------------------------
1490//----- Char Routines --------------------------------------------
1491/* Chars that are part of a word-
1492 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1493 * Chars that are Not part of a word (stoppers)
1494 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1495 * Chars that are WhiteSpace
1496 * TAB NEWLINE VT FF RETURN SPACE
1497 * DO NOT COUNT NEWLINE AS WHITESPACE
1498 */
1499
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001500static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001501{
1502 int li;
1503
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001504 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001505 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001506 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001507 // initialize the new screen. assume this will be a empty file.
1508 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001509 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001510 for (li = 1; li < ro - 1; li++) {
1511 screen[(li * co) + 0] = '~';
1512 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001513 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001514}
1515
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001516#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko33875382008-06-21 20:31:50 +00001517static int mycmp(const char *s1, const char *s2, int len)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001518{
1519 int i;
1520
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001521 i = strncmp(s1, s2, len);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001522 if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001523 i = strncasecmp(s1, s2, len);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001524 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001525 return i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001526}
1527
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001528// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001529static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001530{
1531#ifndef REGEX_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001532 char *start, *stop;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001533 int len;
1534
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001535 len = strlen(pat);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001536 if (dir == FORWARD) {
1537 stop = end - 1; // assume range is p - end-1
1538 if (range == LIMITED)
1539 stop = next_line(p); // range is to next line
1540 for (start = p; start < stop; start++) {
1541 if (mycmp(start, pat, len) == 0) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001542 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001543 }
1544 }
1545 } else if (dir == BACK) {
1546 stop = text; // assume range is text - p
1547 if (range == LIMITED)
1548 stop = prev_line(p); // range is to prev line
1549 for (start = p - len; start >= stop; start--) {
1550 if (mycmp(start, pat, len) == 0) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001551 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001552 }
1553 }
1554 }
1555 // pattern not found
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001556 return NULL;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001557#else /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001558 char *q;
1559 struct re_pattern_buffer preg;
1560 int i;
1561 int size, range;
1562
1563 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1564 preg.translate = 0;
1565 preg.fastmap = 0;
1566 preg.buffer = 0;
1567 preg.allocated = 0;
1568
1569 // assume a LIMITED forward search
1570 q = next_line(p);
1571 q = end_line(q);
1572 q = end - 1;
1573 if (dir == BACK) {
1574 q = prev_line(p);
1575 q = text;
1576 }
1577 // count the number of chars to search over, forward or backward
1578 size = q - p;
1579 if (size < 0)
1580 size = p - q;
1581 // RANGE could be negative if we are searching backwards
1582 range = q - p;
1583
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001584 q = re_compile_pattern(pat, strlen(pat), &preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001585 if (q != 0) {
1586 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001587 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001588 i = 0; // return p if pattern not compiled
1589 goto cs1;
1590 }
1591
1592 q = p;
1593 if (range < 0) {
1594 q = p - size;
1595 if (q < text)
1596 q = text;
1597 }
1598 // search for the compiled pattern, preg, in p[]
1599 // range < 0- search backward
1600 // range > 0- search forward
1601 // 0 < start < size
1602 // re_search() < 0 not found or error
1603 // re_search() > 0 index of found pattern
1604 // struct pattern char int int int struct reg
1605 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1606 i = re_search(&preg, q, size, 0, range, 0);
1607 if (i == -1) {
1608 p = 0;
1609 i = 0; // return NULL if pattern not found
1610 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001611 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001612 if (dir == FORWARD) {
1613 p = p + i;
1614 } else {
1615 p = p - i;
1616 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001617 return p;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001618#endif /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001619}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001620#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001621
Denis Vlasenko33875382008-06-21 20:31:50 +00001622static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001623{
1624 if (c == 22) { // Is this an ctrl-V?
1625 p = stupid_insert(p, '^'); // use ^ to indicate literal next
1626 p--; // backup onto ^
1627 refresh(FALSE); // show the ^
1628 c = get_one_char();
1629 *p = c;
1630 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001631 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001632 } else if (c == 27) { // Is this an ESC?
1633 cmd_mode = 0;
1634 cmdcnt = 0;
1635 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001636 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001637 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001638 p--;
1639 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001640 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001641 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001642 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001643 p--;
1644 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001645 }
1646 } else {
1647 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001648 char *sp; // "save p"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001649
1650 if (c == 13)
1651 c = '\n'; // translate \r to \n
1652 sp = p; // remember addr of insert
1653 p = stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001654#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001655 if (showmatch && strchr(")]}", *sp) != NULL) {
1656 showmatching(sp);
1657 }
1658 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001659 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001660
1661 q = prev_line(p); // use prev line as templet
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001662 for (; isblank(*q); q++) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001663 p = stupid_insert(p, *q); // insert the char
1664 }
1665 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001666#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001667 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001668 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001669}
1670
Denis Vlasenko33875382008-06-21 20:31:50 +00001671static char *stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001672{
1673 p = text_hole_make(p, 1);
Denis Vlasenkob1759462008-06-20 20:20:54 +00001674 *p = c;
1675 //file_modified++; - done by text_hole_make()
1676 return p + 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001677}
1678
Denis Vlasenko33875382008-06-21 20:31:50 +00001679static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001680{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001681 char *save_dot, *p, *q, *t;
1682 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001683
1684 save_dot = dot;
1685 p = q = dot;
1686
1687 if (strchr("cdy><", c)) {
1688 // these cmds operate on whole lines
1689 p = q = begin_line(p);
1690 for (cnt = 1; cnt < cmdcnt; cnt++) {
1691 q = next_line(q);
1692 }
1693 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001694 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001695 // These cmds operate on char positions
1696 do_cmd(c); // execute movement cmd
1697 q = dot;
1698 } else if (strchr("wW", c)) {
1699 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001700 // if we are at the next word's first char
1701 // step back one char
1702 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001703 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001704 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1705 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1706 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001707 if (dot > text && *dot == '\n')
1708 dot--; // stay off NL
1709 q = dot;
1710 } else if (strchr("H-k{", c)) {
1711 // these operate on multi-lines backwards
1712 q = end_line(dot); // find NL
1713 do_cmd(c); // execute movement cmd
1714 dot_begin();
1715 p = dot;
1716 } else if (strchr("L+j}\r\n", c)) {
1717 // these operate on multi-lines forwards
1718 p = begin_line(dot);
1719 do_cmd(c); // execute movement cmd
1720 dot_end(); // find NL
1721 q = dot;
1722 } else {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001723 // nothing -- this causes any other values of c to
1724 // represent the one-character range under the
1725 // cursor. this is correct for ' ' and 'l', but
1726 // perhaps no others.
1727 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001728 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001729 if (q < p) {
1730 t = q;
1731 q = p;
1732 p = t;
1733 }
1734
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001735 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001736 if (q > p && strchr("^0bBh\b\177", c)) q--;
1737
1738 multiline = 0;
1739 for (t = p; t <= q; t++) {
1740 if (*t == '\n') {
1741 multiline = 1;
1742 break;
1743 }
1744 }
1745
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001746 *start = p;
1747 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001748 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001749 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001750}
1751
Denis Vlasenko33875382008-06-21 20:31:50 +00001752static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001753{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001754 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001755 int test, inc;
1756
1757 inc = dir;
1758 c = c0 = p[0];
1759 ci = p[inc];
1760 test = 0;
1761
1762 if (type == S_BEFORE_WS) {
1763 c = ci;
1764 test = ((!isspace(c)) || c == '\n');
1765 }
1766 if (type == S_TO_WS) {
1767 c = c0;
1768 test = ((!isspace(c)) || c == '\n');
1769 }
1770 if (type == S_OVER_WS) {
1771 c = c0;
1772 test = ((isspace(c)));
1773 }
1774 if (type == S_END_PUNCT) {
1775 c = ci;
1776 test = ((ispunct(c)));
1777 }
1778 if (type == S_END_ALNUM) {
1779 c = ci;
1780 test = ((isalnum(c)) || c == '_');
1781 }
1782 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001783 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001784}
1785
Denis Vlasenko33875382008-06-21 20:31:50 +00001786static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001787{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001788 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001789
1790 while (st_test(p, type, dir, &c)) {
1791 // make sure we limit search to correct number of lines
1792 if (c == '\n' && --linecnt < 1)
1793 break;
1794 if (dir >= 0 && p >= end - 1)
1795 break;
1796 if (dir < 0 && p <= text)
1797 break;
1798 p += dir; // move to next char
1799 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001800 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001801}
1802
1803// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00001804static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001805{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001806 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001807 int dir, level;
1808
1809 match = ')';
1810 level = 1;
1811 dir = 1; // assume forward
1812 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001813 case '(': match = ')'; break;
1814 case '[': match = ']'; break;
1815 case '{': match = '}'; break;
1816 case ')': match = '('; dir = -1; break;
1817 case ']': match = '['; dir = -1; break;
1818 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001819 }
1820 for (q = p + dir; text <= q && q < end; q += dir) {
1821 // look for match, count levels of pairs (( ))
1822 if (*q == c)
1823 level++; // increase pair levels
1824 if (*q == match)
1825 level--; // reduce pair level
1826 if (level == 0)
1827 break; // found matching pair
1828 }
1829 if (level != 0)
1830 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001831 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001832}
1833
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001834#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001835// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00001836static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001837{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001838 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001839
1840 // we found half of a pair
1841 q = find_pair(p, *p); // get loc of matching char
1842 if (q == NULL) {
1843 indicate_error('3'); // no matching char
1844 } else {
1845 // "q" now points to matching pair
1846 save_dot = dot; // remember where we are
1847 dot = q; // go to new loc
1848 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001849 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001850 dot = save_dot; // go back to old loc
1851 refresh(FALSE);
1852 }
1853}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001854#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001855
1856// open a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00001857static char *text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001858{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001859 if (size <= 0)
Denis Vlasenkob1759462008-06-20 20:20:54 +00001860 return p;
1861 end += size; // adjust the new END
1862 if (end >= (text + text_size)) {
1863 char *new_text;
1864 text_size += end - (text + text_size) + 10240;
1865 new_text = xrealloc(text, text_size);
1866 screenbegin = new_text + (screenbegin - text);
1867 dot = new_text + (dot - text);
1868 end = new_text + (end - text);
1869 p = new_text + (p - text);
1870 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001871 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00001872 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001873 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00001874 file_modified++;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001875 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001876}
1877
1878// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00001879static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001880{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001881 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001882 int cnt, hole_size;
1883
1884 // move forwards, from beginning
1885 // assume p <= q
1886 src = q + 1;
1887 dest = p;
1888 if (q < p) { // they are backward- swap them
1889 src = p + 1;
1890 dest = q;
1891 }
1892 hole_size = q - p + 1;
1893 cnt = end - src;
1894 if (src < text || src > end)
1895 goto thd0;
1896 if (dest < text || dest >= end)
1897 goto thd0;
1898 if (src >= end)
1899 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00001900 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001901 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001902 end = end - hole_size; // adjust the new END
1903 if (dest >= end)
1904 dest = end - 1; // make sure dest in below end-1
1905 if (end <= text)
1906 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00001907 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001908 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001909 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001910}
1911
1912// copy text into register, then delete text.
1913// if dist <= 0, do not include, or go past, a NewLine
1914//
Denis Vlasenko33875382008-06-21 20:31:50 +00001915static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001916{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001917 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001918
1919 // make sure start <= stop
1920 if (start > stop) {
1921 // they are backwards, reverse them
1922 p = start;
1923 start = stop;
1924 stop = p;
1925 }
1926 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001927 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001928 p = start;
1929 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001930 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001931 // dont go past a NewLine
1932 for (; p + 1 <= stop; p++) {
1933 if (p[1] == '\n') {
1934 stop = p; // "stop" just before NewLine
1935 break;
1936 }
1937 }
1938 }
1939 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001940#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001941 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001942#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001943 if (yf == YANKDEL) {
1944 p = text_hole_delete(start, stop);
1945 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001946 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001947}
1948
1949static void show_help(void)
1950{
1951 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001952#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001953 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001954#endif
1955#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001956 "\n\tLast command repeat with \'.\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001957#endif
1958#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00001959 "\n\tLine marking with 'x"
1960 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001961#endif
1962#if ENABLE_FEATURE_VI_READONLY
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001963 "\n\tReadonly if vi is called as \"view\""
1964 "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001965#endif
1966#if ENABLE_FEATURE_VI_SET
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001967 "\n\tSome colon mode commands with \':\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001968#endif
1969#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001970 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001971#endif
1972#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001973 "\n\tSignal catching- ^C"
1974 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001975#endif
1976#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001977 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001978#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001979 );
1980}
1981
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001982#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001983static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001984{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001985 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001986 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00001987 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001988 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00001989 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00001990 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001991 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001992 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001993 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001994}
1995
1996static void end_cmd_q(void)
1997{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001998#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001999 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002000#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002001 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002002}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002003#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002004
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002005#if ENABLE_FEATURE_VI_YANKMARK \
2006 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2007 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko33875382008-06-21 20:31:50 +00002008static char *string_insert(char *p, char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002009{
2010 int cnt, i;
2011
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002012 i = strlen(s);
Denis Vlasenko33875382008-06-21 20:31:50 +00002013 text_hole_make(p, i);
2014 strncpy(p, s, i);
2015 for (cnt = 0; *s != '\0'; s++) {
2016 if (*s == '\n')
2017 cnt++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002018 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002019#if ENABLE_FEATURE_VI_YANKMARK
2020 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2021#endif
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002022 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002023}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002024#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002025
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002026#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002027static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002028{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002029 char *t;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002030 int cnt;
2031
2032 if (q < p) { // they are backwards- reverse them
2033 t = q;
2034 q = p;
2035 p = t;
2036 }
2037 cnt = q - p + 1;
2038 t = reg[dest];
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00002039 free(t); // if already a yank register, free it
Denis Vlasenkob95636c2006-12-19 23:36:04 +00002040 t = xmalloc(cnt + 1); // get a new register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002041 memset(t, '\0', cnt + 1); // clear new text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002042 strncpy(t, p, cnt); // copy text[] into bufer
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002043 reg[dest] = t;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002044 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002045}
2046
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002047static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002048{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002049 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002050
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002051 c = 'D'; // default to D-reg
2052 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002053 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002054 if (YDreg == 26)
2055 c = 'D';
2056 if (YDreg == 27)
2057 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002058 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002059}
2060
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002061static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002062{
2063 // A context is defined to be "modifying text"
2064 // Any modifying command establishes a new context.
2065
2066 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002067 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002068 // we are trying to modify text[]- make this the current context
2069 mark[27] = mark[26]; // move cur to prev
2070 mark[26] = dot; // move local to cur
2071 context_start = prev_line(prev_line(dot));
2072 context_end = next_line(next_line(dot));
2073 //loiter= start_loiter= now;
2074 }
2075 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002076}
2077
Denis Vlasenkof882f082007-12-23 02:36:51 +00002078static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002079{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002080 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002081
2082 // the current context is in mark[26]
2083 // the previous context is in mark[27]
2084 // only swap context if other context is valid
2085 if (text <= mark[27] && mark[27] <= end - 1) {
2086 tmp = mark[27];
2087 mark[27] = mark[26];
2088 mark[26] = tmp;
2089 p = mark[26]; // where we are going- previous context
2090 context_start = prev_line(prev_line(prev_line(p)));
2091 context_end = next_line(next_line(next_line(p)));
2092 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002093 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002094}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002095#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002096
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002097//----- Set terminal attributes --------------------------------
2098static void rawmode(void)
2099{
2100 tcgetattr(0, &term_orig);
2101 term_vi = term_orig;
2102 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
2103 term_vi.c_iflag &= (~IXON & ~ICRNL);
2104 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002105 term_vi.c_cc[VMIN] = 1;
2106 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002107 erase_char = term_vi.c_cc[VERASE];
2108 tcsetattr(0, TCSANOW, &term_vi);
2109}
2110
2111static void cookmode(void)
2112{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002113 fflush(stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002114 tcsetattr(0, TCSANOW, &term_orig);
2115}
2116
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002117//----- Come here when we get a window resize signal ---------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002118#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002119static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002120{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002121 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002122 signal(SIGWINCH, winch_sig);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002123 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Denis Vlasenko621204b2006-10-27 09:03:24 +00002124 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002125 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
2126 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
2127 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002128 new_screen(rows, columns); // get memory for virtual screen
2129 redraw(TRUE); // re-draw the screen
2130}
2131
2132//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002133static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002134{
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002135 rawmode(); // terminal to "raw"
2136 last_status_cksum = 0; // force status update
2137 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002138
2139 signal(SIGTSTP, suspend_sig);
2140 signal(SIGCONT, SIG_DFL);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002141 kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002142}
2143
2144//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002145static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002146{
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002147 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002148 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002149
2150 signal(SIGCONT, cont_sig);
2151 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002152 kill(my_pid, SIGTSTP);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002153}
2154
2155//----- Come here when we get a signal ---------------------------
2156static void catch_sig(int sig)
2157{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002158 signal(SIGINT, catch_sig);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002159 if (sig)
Paul Fox2724fa92008-03-17 15:28:07 +00002160 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002161}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002162#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002163
2164static int mysleep(int hund) // sleep for 'h' 1/100 seconds
2165{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002166 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002167
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002168 pfd[0].fd = 0;
2169 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002170 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002171}
2172
2173//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002174static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002175{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002176 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002177
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002178 fflush(stdout);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002179 c = read_key(STDIN_FILENO, &chars_to_parse, readbuffer);
2180 if (c == -1) { // EOF/error
2181 go_bottom_and_clear_to_eol();
2182 cookmode(); // terminal to "cooked"
2183 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002184 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002185 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002186}
2187
2188//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002189static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002190{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002191 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002192
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002193#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002194 if (!adding2q) {
2195 // we are not adding to the q.
2196 // but, we may be reading from a q
2197 if (ioq == 0) {
2198 // there is no current q, read from STDIN
2199 c = readit(); // get the users input
2200 } else {
2201 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002202 // careful with correct sign expansion!
2203 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002204 if (c == '\0') {
2205 // the end of the q, read from STDIN
2206 free(ioq_start);
2207 ioq_start = ioq = 0;
2208 c = readit(); // get the users input
2209 }
2210 }
2211 } else {
2212 // adding STDIN chars to q
2213 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002214 if (lmc_len >= MAX_INPUT_LEN - 1) {
2215 status_line_bold("last_modifying_cmd overrun");
2216 } else {
2217 // add new char to q
2218 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002219 }
2220 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002221#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002222 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002223#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002224 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002225}
2226
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002227// Get input line (uses "status line" area)
2228static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002229{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002230 // char [MAX_INPUT_LEN]
2231#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002232
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002233 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002234 int i;
2235
2236 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002237 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002238 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002239 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002240
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002241 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002242 while (i < MAX_INPUT_LEN) {
2243 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002244 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002245 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002246 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002247 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002248 buf[--i] = '\0';
2249 write1("\b \b"); // erase char on screen
2250 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002251 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002252 } else if (c > 0 && c < 256) { // exclude Unicode
2253 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002254 buf[i] = c;
2255 buf[++i] = '\0';
2256 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002257 }
2258 }
2259 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002260 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002261#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002262}
2263
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002264static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002265{
2266 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002267 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002268
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002269 cnt = -1;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002270 if (fn && fn[0] && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002271 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002272 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002273}
2274
Denis Vlasenko33875382008-06-21 20:31:50 +00002275static int file_insert(const char *fn, char *p
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002276 USE_FEATURE_VI_READONLY(, int update_ro_status))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002277{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002278 int cnt = -1;
2279 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002280 struct stat statbuf;
2281
2282 /* Validate file */
2283 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002284 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002285 goto fi0;
2286 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002287 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002288 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002289 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002290 goto fi0;
2291 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002292 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002293 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002294 goto fi0;
2295 }
2296
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002297 // read file to buffer
2298 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002299 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002300 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002301 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002302 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002303 size = statbuf.st_size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002304 p = text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002305 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002306 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002307 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002308 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002309 } else if (cnt < size) {
2310 // There was a partial read, shrink unused space text[]
2311 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002312 status_line_bold("cannot read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002313 }
2314 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002315 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002316 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002317 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002318#if ENABLE_FEATURE_VI_READONLY
2319 if (update_ro_status
2320 && ((access(fn, W_OK) < 0) ||
2321 /* root will always have access()
2322 * so we check fileperms too */
2323 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2324 )
2325 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002326 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002327 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002328#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002329 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002330}
2331
Denis Vlasenko33875382008-06-21 20:31:50 +00002332static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002333{
2334 int fd, cnt, charcnt;
2335
2336 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002337 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002338 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002339 }
2340 charcnt = 0;
Denis Vlasenko8abae882008-05-03 11:35:59 +00002341 /* By popular request we do not open file with O_TRUNC,
2342 * but instead ftruncate() it _after_ successful write.
2343 * Might reduce amount of data lost on power fail etc.
2344 */
2345 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002346 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002347 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002348 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002349 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002350 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002351 if (charcnt == cnt) {
2352 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002353 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002354 } else {
2355 charcnt = 0;
2356 }
2357 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002358 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002359}
2360
2361//----- Terminal Drawing ---------------------------------------
2362// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002363// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002364// screen coordinates
2365// 0,0 ... 0,79
2366// 1,0 ... 1,79
2367// . ... .
2368// . ... .
2369// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002370// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002371
2372//----- Move the cursor to row x col (count from 0, not 1) -------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002373static void place_cursor(int row, int col, int optimize)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002374{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002375 char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
Denis Vlasenko7b54dc72008-07-17 21:32:32 +00002376#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2377 enum {
2378 SZ_UP = sizeof(CMup),
2379 SZ_DN = sizeof(CMdown),
2380 SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN,
2381 };
2382 char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size
2383#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002384 char *cm;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002385
2386 if (row < 0) row = 0;
2387 if (row >= rows) row = rows - 1;
2388 if (col < 0) col = 0;
2389 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002390
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002391 //----- 1. Try the standard terminal ESC sequence
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002392 sprintf(cm1, CMrc, row + 1, col + 1);
2393 cm = cm1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002394
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002395#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002396 if (optimize && col < 16) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002397 char *screenp;
2398 int Rrow = last_row;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002399 int diff = Rrow - row;
2400
2401 if (diff < -5 || diff > 5)
2402 goto skip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002403
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002404 //----- find the minimum # of chars to move cursor -------------
2405 //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
2406 cm2[0] = '\0';
2407
2408 // move to the correct row
2409 while (row < Rrow) {
2410 // the cursor has to move up
2411 strcat(cm2, CMup);
2412 Rrow--;
2413 }
2414 while (row > Rrow) {
2415 // the cursor has to move down
2416 strcat(cm2, CMdown);
2417 Rrow++;
2418 }
2419
2420 // now move to the correct column
2421 strcat(cm2, "\r"); // start at col 0
2422 // just send out orignal source char to get to correct place
2423 screenp = &screen[row * columns]; // start of screen line
2424 strncat(cm2, screenp, col);
2425
2426 // pick the shortest cursor motion to send out
2427 if (strlen(cm2) < strlen(cm)) {
2428 cm = cm2;
2429 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002430 skip: ;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002431 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002432 last_row = row;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002433#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002434 write1(cm);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002435}
2436
2437//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002438static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002439{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002440 write1(Ceol); // Erase from cursor to end of line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002441}
2442
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002443static void go_bottom_and_clear_to_eol(void)
2444{
2445 place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
2446 clear_to_eol(); // erase to end of line
2447}
2448
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002449//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002450static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002451{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002452 write1(Ceos); // Erase from cursor to end of screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002453}
2454
2455//----- Start standout mode ------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002456static void standout_start(void) // send "start reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002457{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002458 write1(SOs); // Start reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002459}
2460
2461//----- End standout mode --------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002462static void standout_end(void) // send "end reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002463{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002464 write1(SOn); // End reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002465}
2466
2467//----- Flash the screen --------------------------------------
2468static void flash(int h)
2469{
2470 standout_start(); // send "start reverse video" sequence
2471 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002472 mysleep(h);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002473 standout_end(); // send "end reverse video" sequence
2474 redraw(TRUE);
2475}
2476
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002477static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002478{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002479#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002480 if (crashme > 0)
2481 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002482#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002483 if (!err_method) {
2484 write1(bell); // send out a bell character
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002485 } else {
2486 flash(10);
2487 }
2488}
2489
2490//----- Screen[] Routines --------------------------------------
2491//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002492static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002493{
2494 memset(screen, ' ', screensize); // clear new screen
2495}
2496
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002497static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002498{
2499 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002500 char *e = buf + count;
2501
Paul Fox8552aec2005-09-16 12:20:05 +00002502 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002503 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002504 return sum;
2505}
2506
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002507//----- Draw the status line at bottom of the screen -------------
2508static void show_status_line(void)
2509{
Paul Foxc3504852005-09-16 12:48:18 +00002510 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002511
Paul Fox8552aec2005-09-16 12:20:05 +00002512 // either we already have an error or status message, or we
2513 // create one.
2514 if (!have_status_msg) {
2515 cnt = format_edit_status();
2516 cksum = bufsum(status_buffer, cnt);
2517 }
2518 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002519 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002520 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002521 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002522 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002523 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002524 (columns - 1) ) {
2525 have_status_msg = 0;
2526 Hit_Return();
2527 }
2528 have_status_msg = 0;
2529 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002530 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2531 }
Eric Andersena9eb33d2004-08-19 19:15:06 +00002532 fflush(stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002533}
2534
2535//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002536// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002537static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002538{
2539 va_list args;
2540
2541 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002542 strcpy(status_buffer, SOs); // Terminal standout mode on
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002543 vsprintf(status_buffer + sizeof(SOs)-1, format, args);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002544 strcat(status_buffer, SOn); // Terminal standout mode off
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002545 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002546
2547 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002548}
2549
Paul Fox8552aec2005-09-16 12:20:05 +00002550// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002551static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002552{
2553 va_list args;
2554
2555 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002556 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002557 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002558
2559 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002560}
2561
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002562// copy s to buf, convert unprintable
2563static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002564{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002565 unsigned char c;
2566 char b[2];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002567
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002568 b[1] = '\0';
2569 buf[0] = '\0';
2570 if (!s[0])
2571 s = "(NULL)";
2572 for (; *s; s++) {
2573 int c_is_no_print;
2574
2575 c = *s;
2576 c_is_no_print = (c & 0x80) && !Isprint(c);
2577 if (c_is_no_print) {
2578 strcat(buf, SOn);
2579 c = '.';
2580 }
2581 if (c < ' ' || c == 127) {
2582 strcat(buf, "^");
2583 if (c == 127)
2584 c = '?';
2585 else
2586 c += '@';
2587 }
2588 b[0] = c;
2589 strcat(buf, b);
2590 if (c_is_no_print)
2591 strcat(buf, SOs);
2592 if (*s == '\n')
2593 strcat(buf, "$");
2594 if (strlen(buf) > MAX_INPUT_LEN - 10) // paranoia
2595 break;
2596 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002597}
2598
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002599static void not_implemented(const char *s)
2600{
2601 char buf[MAX_INPUT_LEN];
2602
2603 print_literal(buf, s);
2604 status_line_bold("\'%s\' is not implemented", buf);
2605}
2606
2607// show file status on status line
2608static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002609{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002610 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002611
2612#define tot format_edit_status__tot
2613
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002614 int cur, percent, ret, trunc_at;
2615
Paul Fox8552aec2005-09-16 12:20:05 +00002616 // file_modified is now a counter rather than a flag. this
2617 // helps reduce the amount of line counting we need to do.
2618 // (this will cause a mis-reporting of modified status
2619 // once every MAXINT editing operations.)
2620
2621 // it would be nice to do a similar optimization here -- if
2622 // we haven't done a motion that could have changed which line
2623 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002624 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002625
2626 // reduce counting -- the total lines can't have
2627 // changed if we haven't done any edits.
2628 if (file_modified != last_file_modified) {
2629 tot = cur + count_lines(dot, end - 1) - 1;
2630 last_file_modified = file_modified;
2631 }
2632
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002633 // current line percent
2634 // ------------- ~~ ----------
2635 // total lines 100
2636 if (tot > 0) {
2637 percent = (100 * cur) / tot;
2638 } else {
2639 cur = tot = 0;
2640 percent = 100;
2641 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002642
Paul Fox8552aec2005-09-16 12:20:05 +00002643 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2644 columns : STATUS_BUFFER_LEN-1;
2645
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002646 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002647#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002648 "%c %s%s%s %d/%d %d%%",
2649#else
2650 "%c %s%s %d/%d %d%%",
2651#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002652 cmd_mode_indicator[cmd_mode & 3],
2653 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002654#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002655 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002656#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002657 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002658 cur, tot, percent);
2659
2660 if (ret >= 0 && ret < trunc_at)
2661 return ret; /* it all fit */
2662
2663 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002664#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002665}
2666
2667//----- Force refresh of all Lines -----------------------------
2668static void redraw(int full_screen)
2669{
2670 place_cursor(0, 0, FALSE); // put cursor in correct place
Denis Vlasenkob1759462008-06-20 20:20:54 +00002671 clear_to_eos(); // tell terminal to erase display
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002672 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002673 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002674 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002675 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002676}
2677
2678//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002679static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002680{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002681 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002682 int co;
2683 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002684 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002685
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002686 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002687 co = 0;
2688 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002689 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002690 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002691 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002692 if (c == '\n')
2693 break;
2694 if ((c & 0x80) && !Isprint(c)) {
2695 c = '.';
2696 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002697 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002698 if (c == '\t') {
2699 c = ' ';
2700 // co % 8 != 7
2701 while ((co % tabstop) != (tabstop - 1)) {
2702 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002703 }
2704 } else {
2705 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002706 if (c == 0x7f)
2707 c = '?';
2708 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002709 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002710 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002711 }
2712 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002713 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002714 // discard scrolled-off-to-the-left portion,
2715 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002716 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002717 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002718 co -= tabstop;
2719 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002720 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002721 if (src >= end)
2722 break;
2723 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002724 // check "short line, gigantic offset" case
2725 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002726 ofs = co;
2727 // discard last scrolled off part
2728 co -= ofs;
2729 dest += ofs;
2730 // fill the rest with spaces
2731 if (co < columns)
2732 memset(&dest[co], ' ', columns - co);
2733 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002734}
2735
2736//----- Refresh the changed screen lines -----------------------
2737// Copy the source line from text[] into the buffer and note
2738// if the current screenline is different from the new buffer.
2739// If they differ then that line needs redrawing on the terminal.
2740//
2741static void refresh(int full_screen)
2742{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002743#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002744
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002745 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002746 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002747
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002748 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002749 unsigned c = columns, r = rows;
Rob Landleye5e1a102006-06-21 01:15:36 +00002750 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002751 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
2752 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
2753 full_screen |= (c - columns) | (r - rows);
2754 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002755 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2756 tp = screenbegin; // index into text[] of top line
2757
2758 // compare text[] to screen[] and mark screen[] lines that need updating
2759 for (li = 0; li < rows - 1; li++) {
2760 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002761 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002762 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002763 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002764
2765 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002766 if (tp < end) {
2767 char *t = memchr(tp, '\n', end - tp);
2768 if (!t) t = end - 1;
2769 tp = t + 1;
2770 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002771
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002772 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002773 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002774 cs = 0;
2775 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002776 sp = &screen[li * columns]; // start of screen line
2777 if (full_screen) {
2778 // force re-draw of every single column from 0 - columns-1
2779 goto re0;
2780 }
2781 // compare newly formatted buffer with virtual screen
2782 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002783 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002784 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002785 changed = TRUE; // mark for redraw
2786 break;
2787 }
2788 }
2789
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002790 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002791 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002792 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002793 changed = TRUE; // mark for redraw
2794 break;
2795 }
2796 }
2797 // now, cs is index of first diff, and ce is index of last diff
2798
2799 // if horz offset has changed, force a redraw
2800 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002801 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002802 changed = TRUE;
2803 }
2804
2805 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002806 if (cs < 0) cs = 0;
2807 if (ce > columns - 1) ce = columns - 1;
2808 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002809 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002810 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002811 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002812 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002813
2814 // move cursor to column of first change
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002815 //if (offset != old_offset) {
2816 // // place_cursor is still too stupid
2817 // // to handle offsets correctly
2818 // place_cursor(li, cs, FALSE);
2819 //} else {
2820 place_cursor(li, cs, TRUE);
2821 //}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002822
2823 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002824 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002825 }
2826 }
2827
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002828 place_cursor(crow, ccol, TRUE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002829
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002830 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002831#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002832}
2833
Eric Andersen3f980402001-04-04 17:31:15 +00002834//---------------------------------------------------------------------
2835//----- the Ascii Chart -----------------------------------------------
2836//
2837// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
2838// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
2839// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
2840// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
2841// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
2842// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
2843// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
2844// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
2845// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
2846// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
2847// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
2848// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
2849// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
2850// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
2851// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
2852// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
2853//---------------------------------------------------------------------
2854
2855//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002856static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00002857{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002858 const char *msg = msg; // for compiler
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002859 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002860 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00002861 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002862 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002863 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00002864
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002865// c1 = c; // quiet the compiler
2866// cnt = yf = 0; // quiet the compiler
2867// msg = p = q = save_dot = buf; // quiet the compiler
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002868 memset(buf, '\0', 12);
Eric Andersenbff7a602001-11-17 07:15:43 +00002869
Paul Fox8552aec2005-09-16 12:20:05 +00002870 show_status_line();
2871
Eric Andersenbff7a602001-11-17 07:15:43 +00002872 /* if this is a cursor key, skip these checks */
2873 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002874 case KEYCODE_UP:
2875 case KEYCODE_DOWN:
2876 case KEYCODE_LEFT:
2877 case KEYCODE_RIGHT:
2878 case KEYCODE_HOME:
2879 case KEYCODE_END:
2880 case KEYCODE_PAGEUP:
2881 case KEYCODE_PAGEDOWN:
2882 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00002883 goto key_cmd_mode;
2884 }
2885
Eric Andersen3f980402001-04-04 17:31:15 +00002886 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002887 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002888 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00002889 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00002890 // we are 'R'eplacing the current *dot with new char
2891 if (*dot == '\n') {
2892 // don't Replace past E-o-l
2893 cmd_mode = 1; // convert to insert
2894 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002895 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00002896 if (c != 27)
2897 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
2898 dot = char_insert(dot, c); // insert new char
2899 }
2900 goto dc1;
2901 }
2902 }
2903 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00002904 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002905 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00002906 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002907 if (1 <= c || Isprint(c)) {
2908 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00002909 }
2910 goto dc1;
2911 }
2912
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002913 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00002914 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00002915 //case 0x01: // soh
2916 //case 0x09: // ht
2917 //case 0x0b: // vt
2918 //case 0x0e: // so
2919 //case 0x0f: // si
2920 //case 0x10: // dle
2921 //case 0x11: // dc1
2922 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002923#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00002924 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00002925 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00002926 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002927#endif
Eric Andersen822c3832001-05-07 17:37:43 +00002928 //case 0x16: // syn
2929 //case 0x17: // etb
2930 //case 0x18: // can
2931 //case 0x1c: // fs
2932 //case 0x1d: // gs
2933 //case 0x1e: // rs
2934 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002935 //case '!': // !-
2936 //case '#': // #-
2937 //case '&': // &-
2938 //case '(': // (-
2939 //case ')': // )-
2940 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002941 //case '=': // =-
2942 //case '@': // @-
2943 //case 'F': // F-
2944 //case 'K': // K-
2945 //case 'Q': // Q-
2946 //case 'S': // S-
2947 //case 'T': // T-
2948 //case 'V': // V-
2949 //case '[': // [-
2950 //case '\\': // \-
2951 //case ']': // ]-
2952 //case '_': // _-
2953 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00002954 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002955 //case 'v': // v-
Eric Andersen3f980402001-04-04 17:31:15 +00002956 default: // unrecognised command
2957 buf[0] = c;
2958 buf[1] = '\0';
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002959 if (c < ' ') {
Eric Andersen3f980402001-04-04 17:31:15 +00002960 buf[0] = '^';
2961 buf[1] = c + '@';
2962 buf[2] = '\0';
2963 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002964 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00002965 end_cmd_q(); // stop adding to q
2966 case 0x00: // nul- ignore
2967 break;
2968 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002969 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00002970 dot_scroll(rows - 2, -1);
2971 break;
Eric Andersen3f980402001-04-04 17:31:15 +00002972 case 4: // ctrl-D scroll down half screen
2973 dot_scroll((rows - 2) / 2, 1);
2974 break;
2975 case 5: // ctrl-E scroll down one line
2976 dot_scroll(1, 1);
2977 break;
2978 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002979 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00002980 dot_scroll(rows - 2, 1);
2981 break;
2982 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00002983 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00002984 break;
2985 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002986 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00002987 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00002988 case 0x7f: // DEL- move left (This may be ERASE char)
Eric Andersen3f980402001-04-04 17:31:15 +00002989 if (cmdcnt-- > 1) {
2990 do_cmd(c);
2991 } // repeat cnt
2992 dot_left();
2993 break;
2994 case 10: // Newline ^J
2995 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002996 case KEYCODE_DOWN: // cursor key Down
Eric Andersen3f980402001-04-04 17:31:15 +00002997 if (cmdcnt-- > 1) {
2998 do_cmd(c);
2999 } // repeat cnt
3000 dot_next(); // go to next B-o-l
3001 dot = move_to_col(dot, ccol + offset); // try stay in same col
3002 break;
3003 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003004 case 18: // ctrl-R force redraw
Eric Andersen822c3832001-05-07 17:37:43 +00003005 place_cursor(0, 0, FALSE); // put cursor in correct place
Eric Andersen3f980402001-04-04 17:31:15 +00003006 clear_to_eos(); // tel terminal to erase display
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003007 mysleep(10);
Eric Andersen3f980402001-04-04 17:31:15 +00003008 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003009 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003010 refresh(TRUE); // this will redraw the entire display
3011 break;
3012 case 13: // Carriage Return ^M
3013 case '+': // +- goto next line
3014 if (cmdcnt-- > 1) {
3015 do_cmd(c);
3016 } // repeat cnt
3017 dot_next();
3018 dot_skip_over_ws();
3019 break;
3020 case 21: // ctrl-U scroll up half screen
3021 dot_scroll((rows - 2) / 2, -1);
3022 break;
3023 case 25: // ctrl-Y scroll up one line
3024 dot_scroll(1, -1);
3025 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003026 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003027 if (cmd_mode == 0)
3028 indicate_error(c);
3029 cmd_mode = 0; // stop insrting
3030 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003031 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003032 break;
3033 case ' ': // move right
3034 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003035 case KEYCODE_RIGHT: // Cursor Key Right
Eric Andersen3f980402001-04-04 17:31:15 +00003036 if (cmdcnt-- > 1) {
3037 do_cmd(c);
3038 } // repeat cnt
3039 dot_right();
3040 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003041#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003042 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003043 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3044 if ((unsigned)c1 <= 25) { // a-z?
3045 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003046 } else {
3047 indicate_error(c);
3048 }
3049 break;
3050 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003051 c1 = (get_one_char() | 0x20) - 'a';
3052 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003053 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003054 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003055 if (text <= q && q < end) {
3056 dot = q;
3057 dot_begin(); // go to B-o-l
3058 dot_skip_over_ws();
3059 }
3060 } else if (c1 == '\'') { // goto previous context
3061 dot = swap_context(dot); // swap current and previous context
3062 dot_begin(); // go to B-o-l
3063 dot_skip_over_ws();
3064 } else {
3065 indicate_error(c);
3066 }
3067 break;
3068 case 'm': // m- Mark a line
3069 // this is really stupid. If there are any inserts or deletes
3070 // between text[0] and dot then this mark will not point to the
3071 // correct location! It could be off by many lines!
3072 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003073 c1 = (get_one_char() | 0x20) - 'a';
3074 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003075 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003076 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003077 } else {
3078 indicate_error(c);
3079 }
3080 break;
3081 case 'P': // P- Put register before
3082 case 'p': // p- put register after
3083 p = reg[YDreg];
3084 if (p == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003085 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003086 break;
3087 }
3088 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003089 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003090 if (c == 'P') {
3091 dot_begin(); // putting lines- Put above
3092 }
3093 if (c == 'p') {
3094 // are we putting after very last line?
3095 if (end_line(dot) == (end - 1)) {
3096 dot = end; // force dot to end of text[]
3097 } else {
3098 dot_next(); // next line, then put before
3099 }
3100 }
3101 } else {
3102 if (c == 'p')
3103 dot_right(); // move to right, can move to NL
3104 }
3105 dot = string_insert(dot, p); // insert the string
3106 end_cmd_q(); // stop adding to q
3107 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003108 case 'U': // U- Undo; replace current line with original version
3109 if (reg[Ureg] != 0) {
3110 p = begin_line(dot);
3111 q = end_line(dot);
3112 p = text_hole_delete(p, q); // delete cur line
3113 p = string_insert(p, reg[Ureg]); // insert orig line
3114 dot = p;
3115 dot_skip_over_ws();
3116 }
3117 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003118#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003119 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003120 case KEYCODE_END: // Cursor Key End
Eric Andersen3f980402001-04-04 17:31:15 +00003121 if (cmdcnt-- > 1) {
3122 do_cmd(c);
3123 } // repeat cnt
Glenn L McGrathee829062004-01-21 10:59:45 +00003124 dot = end_line(dot);
Eric Andersen3f980402001-04-04 17:31:15 +00003125 break;
3126 case '%': // %- find matching char of pair () [] {}
3127 for (q = dot; q < end && *q != '\n'; q++) {
3128 if (strchr("()[]{}", *q) != NULL) {
3129 // we found half of a pair
3130 p = find_pair(q, *q);
3131 if (p == NULL) {
3132 indicate_error(c);
3133 } else {
3134 dot = p;
3135 }
3136 break;
3137 }
3138 }
3139 if (*q == '\n')
3140 indicate_error(c);
3141 break;
3142 case 'f': // f- forward to a user specified char
3143 last_forward_char = get_one_char(); // get the search char
3144 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003145 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003146 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003147 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003148 case ';': // ;- look at rest of line for last forward char
3149 if (cmdcnt-- > 1) {
Eric Andersen822c3832001-05-07 17:37:43 +00003150 do_cmd(';');
Eric Andersen3f980402001-04-04 17:31:15 +00003151 } // repeat cnt
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003152 if (last_forward_char == 0)
3153 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003154 q = dot + 1;
3155 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3156 q++;
3157 }
3158 if (*q == last_forward_char)
3159 dot = q;
3160 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003161 case ',': // repeat latest 'f' in opposite direction
3162 if (cmdcnt-- > 1) {
3163 do_cmd(',');
3164 } // repeat cnt
3165 if (last_forward_char == 0)
3166 break;
3167 q = dot - 1;
3168 while (q >= text && *q != '\n' && *q != last_forward_char) {
3169 q--;
3170 }
3171 if (q >= text && *q == last_forward_char)
3172 dot = q;
3173 break;
3174
Eric Andersen3f980402001-04-04 17:31:15 +00003175 case '-': // -- goto prev line
3176 if (cmdcnt-- > 1) {
3177 do_cmd(c);
3178 } // repeat cnt
3179 dot_prev();
3180 dot_skip_over_ws();
3181 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003182#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003183 case '.': // .- repeat the last modifying command
3184 // Stuff the last_modifying_cmd back into stdin
3185 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003186 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003187 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003188 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003189 }
3190 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003191#endif
3192#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003193 case '?': // /- search for a pattern
3194 case '/': // /- search for a pattern
3195 buf[0] = c;
3196 buf[1] = '\0';
3197 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003198 if (q[0] && !q[1]) {
3199 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003200 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003201 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003202 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003203 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003204 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003205 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003206 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003207 goto dc3; // now find the pattern
3208 }
3209 // user changed mind and erased the "/"- do nothing
3210 break;
3211 case 'N': // N- backward search for last pattern
3212 if (cmdcnt-- > 1) {
3213 do_cmd(c);
3214 } // repeat cnt
3215 dir = BACK; // assume BACKWARD search
3216 p = dot - 1;
3217 if (last_search_pattern[0] == '?') {
3218 dir = FORWARD;
3219 p = dot + 1;
3220 }
3221 goto dc4; // now search for pattern
3222 break;
3223 case 'n': // n- repeat search for last pattern
3224 // search rest of text[] starting at next char
3225 // if search fails return orignal "p" not the "p+1" address
3226 if (cmdcnt-- > 1) {
3227 do_cmd(c);
3228 } // repeat cnt
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003229 dc3:
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003230 dir = FORWARD; // assume FORWARD search
3231 p = dot + 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003232 if (last_search_pattern[0] == '?') {
3233 dir = BACK;
3234 p = dot - 1;
3235 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003236 dc4:
Eric Andersen3f980402001-04-04 17:31:15 +00003237 q = char_search(p, last_search_pattern + 1, dir, FULL);
3238 if (q != NULL) {
3239 dot = q; // good search, update "dot"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003240 msg = "";
Eric Andersen3f980402001-04-04 17:31:15 +00003241 goto dc2;
3242 }
3243 // no pattern found between "dot" and "end"- continue at top
3244 p = text;
3245 if (dir == BACK) {
3246 p = end - 1;
3247 }
3248 q = char_search(p, last_search_pattern + 1, dir, FULL);
3249 if (q != NULL) { // found something
3250 dot = q; // found new pattern- goto it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003251 msg = "search hit BOTTOM, continuing at TOP";
Eric Andersen3f980402001-04-04 17:31:15 +00003252 if (dir == BACK) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003253 msg = "search hit TOP, continuing at BOTTOM";
Eric Andersen3f980402001-04-04 17:31:15 +00003254 }
3255 } else {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003256 msg = "Pattern not found";
Eric Andersen3f980402001-04-04 17:31:15 +00003257 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003258 dc2:
3259 if (*msg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003260 status_line_bold("%s", msg);
Eric Andersen3f980402001-04-04 17:31:15 +00003261 break;
3262 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003263 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003264 if (q != NULL) { // found blank line
3265 dot = next_line(q); // move to next blank line
3266 }
3267 break;
3268 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003269 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003270 if (q != NULL) { // found blank line
3271 dot = next_line(q); // move to next blank line
3272 }
3273 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003274#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003275 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003276 case '1': // 1-
3277 case '2': // 2-
3278 case '3': // 3-
3279 case '4': // 4-
3280 case '5': // 5-
3281 case '6': // 6-
3282 case '7': // 7-
3283 case '8': // 8-
3284 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003285 if (c == '0' && cmdcnt < 1) {
3286 dot_begin(); // this was a standalone zero
3287 } else {
3288 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3289 }
3290 break;
3291 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003292 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003293#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003294 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003295#else
Eric Andersen822c3832001-05-07 17:37:43 +00003296 if (*p == ':')
3297 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003298 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003299 if (cnt <= 0)
3300 break;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003301 if (strncasecmp(p, "quit", cnt) == 0
3302 || strncasecmp(p, "q!", cnt) == 0 // delete lines
3303 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003304 if (file_modified && p[1] != '!') {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003305 status_line_bold("No write since last change (:quit! overrides)");
Eric Andersen3f980402001-04-04 17:31:15 +00003306 } else {
3307 editing = 0;
3308 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003309 } else if (strncasecmp(p, "write", cnt) == 0
3310 || strncasecmp(p, "wq", cnt) == 0
3311 || strncasecmp(p, "wn", cnt) == 0
3312 || strncasecmp(p, "x", cnt) == 0
3313 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003314 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003315 if (cnt < 0) {
3316 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003317 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003318 } else {
3319 file_modified = 0;
3320 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003321 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003322 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3323 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3324 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003325 editing = 0;
3326 }
Eric Andersen3f980402001-04-04 17:31:15 +00003327 }
Denis Vlasenko219d14d2007-03-24 15:40:16 +00003328 } else if (strncasecmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003329 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003330 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003331 dot = find_line(j); // go to line # j
3332 dot_skip_over_ws();
Eric Andersen3f980402001-04-04 17:31:15 +00003333 } else { // unrecognised cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003334 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003335 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003336#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003337 break;
3338 case '<': // <- Left shift something
3339 case '>': // >- Right shift something
3340 cnt = count_lines(text, dot); // remember what line we are on
3341 c1 = get_one_char(); // get the type of thing to delete
3342 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003343 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003344 p = begin_line(p);
3345 q = end_line(q);
3346 i = count_lines(p, q); // # of lines we are shifting
3347 for ( ; i > 0; i--, p = next_line(p)) {
3348 if (c == '<') {
3349 // shift left- remove tab or 8 spaces
3350 if (*p == '\t') {
3351 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003352 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003353 } else if (*p == ' ') {
3354 // we should be calculating columns, not just SPACE
3355 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003356 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003357 }
3358 }
3359 } else if (c == '>') {
3360 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003361 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003362 }
3363 }
3364 dot = find_line(cnt); // what line were we on
3365 dot_skip_over_ws();
3366 end_cmd_q(); // stop adding to q
3367 break;
3368 case 'A': // A- append at e-o-l
3369 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003370 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003371 case 'a': // a- append after current char
3372 if (*dot != '\n')
3373 dot++;
3374 goto dc_i;
3375 break;
3376 case 'B': // B- back a blank-delimited Word
3377 case 'E': // E- end of a blank-delimited word
3378 case 'W': // W- forward a blank-delimited word
3379 if (cmdcnt-- > 1) {
3380 do_cmd(c);
3381 } // repeat cnt
3382 dir = FORWARD;
3383 if (c == 'B')
3384 dir = BACK;
3385 if (c == 'W' || isspace(dot[dir])) {
3386 dot = skip_thing(dot, 1, dir, S_TO_WS);
3387 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3388 }
3389 if (c != 'W')
3390 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3391 break;
3392 case 'C': // C- Change to e-o-l
3393 case 'D': // D- delete to e-o-l
3394 save_dot = dot;
3395 dot = dollar_line(dot); // move to before NL
3396 // copy text into a register and delete
3397 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3398 if (c == 'C')
3399 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003400#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003401 if (c == 'D')
3402 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003403#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003404 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003405 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003406 c1 = get_one_char();
3407 if (c1 != 'g') {
3408 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003409 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003410 buf[2] = '\0';
3411 not_implemented(buf);
3412 break;
3413 }
3414 if (cmdcnt == 0)
3415 cmdcnt = 1;
3416 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003417 case 'G': // G- goto to a line number (default= E-O-F)
3418 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003419 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003420 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003421 }
3422 dot_skip_over_ws();
3423 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003424 case 'H': // H- goto top line on screen
3425 dot = screenbegin;
3426 if (cmdcnt > (rows - 1)) {
3427 cmdcnt = (rows - 1);
3428 }
3429 if (cmdcnt-- > 1) {
3430 do_cmd('+');
3431 } // repeat cnt
3432 dot_skip_over_ws();
3433 break;
3434 case 'I': // I- insert before first non-blank
3435 dot_begin(); // 0
3436 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003437 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003438 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003439 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003440 dc_i:
Eric Andersen3f980402001-04-04 17:31:15 +00003441 cmd_mode = 1; // start insrting
Eric Andersen3f980402001-04-04 17:31:15 +00003442 break;
3443 case 'J': // J- join current and next lines together
3444 if (cmdcnt-- > 2) {
3445 do_cmd(c);
3446 } // repeat cnt
3447 dot_end(); // move to NL
3448 if (dot < end - 1) { // make sure not last char in text[]
3449 *dot++ = ' '; // replace NL with space
Paul Fox8552aec2005-09-16 12:20:05 +00003450 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003451 while (isblank(*dot)) { // delete leading WS
Eric Andersen3f980402001-04-04 17:31:15 +00003452 dot_delete();
3453 }
3454 }
3455 end_cmd_q(); // stop adding to q
3456 break;
3457 case 'L': // L- goto bottom line on screen
3458 dot = end_screen();
3459 if (cmdcnt > (rows - 1)) {
3460 cmdcnt = (rows - 1);
3461 }
3462 if (cmdcnt-- > 1) {
3463 do_cmd('-');
3464 } // repeat cnt
3465 dot_begin();
3466 dot_skip_over_ws();
3467 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003468 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003469 dot = screenbegin;
3470 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3471 dot = next_line(dot);
3472 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003473 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003474 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003475 p = begin_line(dot);
3476 if (p[-1] == '\n') {
3477 dot_prev();
3478 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3479 dot_end();
3480 dot = char_insert(dot, '\n');
3481 } else {
3482 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003483 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003484 dot_prev(); // -
3485 }
3486 goto dc_i;
3487 break;
3488 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003489 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003490 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003491 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003492 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003493 c = 'x';
3494 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003495 case 'X': // X- delete char before dot
3496 case 'x': // x- delete the current char
3497 case 's': // s- substitute the current char
3498 if (cmdcnt-- > 1) {
3499 do_cmd(c);
3500 } // repeat cnt
3501 dir = 0;
3502 if (c == 'X')
3503 dir = -1;
3504 if (dot[dir] != '\n') {
3505 if (c == 'X')
3506 dot--; // delete prev char
3507 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3508 }
3509 if (c == 's')
3510 goto dc_i; // start insrting
3511 end_cmd_q(); // stop adding to q
3512 break;
3513 case 'Z': // Z- if modified, {write}; exit
3514 // ZZ means to save file (if necessary), then exit
3515 c1 = get_one_char();
3516 if (c1 != 'Z') {
3517 indicate_error(c);
3518 break;
3519 }
Paul Foxf0305b72006-03-28 14:18:21 +00003520 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003521 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003522 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003523 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003524 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003525 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003526 if (cnt < 0) {
3527 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003528 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003529 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003530 editing = 0;
3531 }
3532 } else {
3533 editing = 0;
3534 }
3535 break;
3536 case '^': // ^- move to first non-blank on line
3537 dot_begin();
3538 dot_skip_over_ws();
3539 break;
3540 case 'b': // b- back a word
3541 case 'e': // e- end of word
3542 if (cmdcnt-- > 1) {
3543 do_cmd(c);
3544 } // repeat cnt
3545 dir = FORWARD;
3546 if (c == 'b')
3547 dir = BACK;
3548 if ((dot + dir) < text || (dot + dir) > end - 1)
3549 break;
3550 dot += dir;
3551 if (isspace(*dot)) {
3552 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3553 }
3554 if (isalnum(*dot) || *dot == '_') {
3555 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3556 } else if (ispunct(*dot)) {
3557 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3558 }
3559 break;
3560 case 'c': // c- change something
3561 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003562#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003563 case 'y': // y- yank something
3564 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003565#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003566 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003567 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003568 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003569#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003570 if (c == 'y' || c == 'Y')
3571 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003572#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003573 c1 = 'y';
3574 if (c != 'Y')
3575 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003576 // determine range, and whether it spans lines
3577 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003578 if (c1 == 27) { // ESC- user changed mind and wants out
3579 c = c1 = 27; // Escape- do nothing
3580 } else if (strchr("wW", c1)) {
3581 if (c == 'c') {
3582 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003583 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003584 if (q <= text || q[-1] == '\n')
3585 break;
3586 q--;
3587 }
3588 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003589 dot = yank_delete(p, q, ml, yf); // delete word
3590 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3591 // partial line copy text into a register and delete
3592 dot = yank_delete(p, q, ml, yf); // delete word
3593 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3594 // whole line copy text into a register and delete
3595 dot = yank_delete(p, q, ml, yf); // delete lines
3596 whole = 1;
3597 } else {
3598 // could not recognize object
3599 c = c1 = 27; // error-
3600 ml = 0;
3601 indicate_error(c);
3602 }
3603 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003604 if (c == 'c') {
3605 dot = char_insert(dot, '\n');
3606 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003607 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003608 dot_prev();
3609 }
3610 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003611 dot_begin();
3612 dot_skip_over_ws();
3613 }
Eric Andersen3f980402001-04-04 17:31:15 +00003614 }
3615 if (c1 != 27) {
3616 // if CHANGING, not deleting, start inserting after the delete
3617 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003618 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003619 goto dc_i; // start inserting
3620 }
3621 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003622 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003623 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003624#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003625 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003626 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003627 }
3628 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003629 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003630 for (cnt = 0; p <= q; p++) {
3631 if (*p == '\n')
3632 cnt++;
3633 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003634 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003635 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003636#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003637 end_cmd_q(); // stop adding to q
3638 }
3639 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003640 }
Eric Andersen3f980402001-04-04 17:31:15 +00003641 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003642 case KEYCODE_UP: // cursor key Up
Eric Andersen3f980402001-04-04 17:31:15 +00003643 if (cmdcnt-- > 1) {
3644 do_cmd(c);
3645 } // repeat cnt
3646 dot_prev();
3647 dot = move_to_col(dot, ccol + offset); // try stay in same col
3648 break;
3649 case 'r': // r- replace the current char with user input
3650 c1 = get_one_char(); // get the replacement char
3651 if (*dot != '\n') {
3652 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003653 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003654 }
3655 end_cmd_q(); // stop adding to q
3656 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003657 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003658 last_forward_char = get_one_char();
3659 do_cmd(';');
3660 if (*dot == last_forward_char)
3661 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003662 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003663 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003664 case 'w': // w- forward a word
3665 if (cmdcnt-- > 1) {
3666 do_cmd(c);
3667 } // repeat cnt
3668 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3669 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3670 } else if (ispunct(*dot)) { // we are on PUNCT
3671 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3672 }
3673 if (dot < end - 1)
3674 dot++; // move over word
3675 if (isspace(*dot)) {
3676 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3677 }
3678 break;
3679 case 'z': // z-
3680 c1 = get_one_char(); // get the replacement char
3681 cnt = 0;
3682 if (c1 == '.')
3683 cnt = (rows - 2) / 2; // put dot at center
3684 if (c1 == '-')
3685 cnt = rows - 2; // put dot at bottom
3686 screenbegin = begin_line(dot); // start dot at top
3687 dot_scroll(cnt, -1);
3688 break;
3689 case '|': // |- move to column "cmdcnt"
3690 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3691 break;
3692 case '~': // ~- flip the case of letters a-z -> A-Z
3693 if (cmdcnt-- > 1) {
3694 do_cmd(c);
3695 } // repeat cnt
3696 if (islower(*dot)) {
3697 *dot = toupper(*dot);
Denis Vlasenkob1759462008-06-20 20:20:54 +00003698 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003699 } else if (isupper(*dot)) {
3700 *dot = tolower(*dot);
Denis Vlasenkob1759462008-06-20 20:20:54 +00003701 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003702 }
3703 dot_right();
3704 end_cmd_q(); // stop adding to q
3705 break;
3706 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003707 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003708 dot_begin();
3709 break;
3710 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003711#if 0
3712 case KEYCODE_FUN1: // Function Key F1
3713 case KEYCODE_FUN2: // Function Key F2
3714 case KEYCODE_FUN3: // Function Key F3
3715 case KEYCODE_FUN4: // Function Key F4
3716 case KEYCODE_FUN5: // Function Key F5
3717 case KEYCODE_FUN6: // Function Key F6
3718 case KEYCODE_FUN7: // Function Key F7
3719 case KEYCODE_FUN8: // Function Key F8
3720 case KEYCODE_FUN9: // Function Key F9
3721 case KEYCODE_FUN10: // Function Key F10
3722 case KEYCODE_FUN11: // Function Key F11
3723 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003724 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003725#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003726 }
3727
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003728 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003729 // if text[] just became empty, add back an empty line
3730 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003731 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003732 dot = text;
3733 }
3734 // it is OK for dot to exactly equal to end, otherwise check dot validity
3735 if (dot != end) {
3736 dot = bound_dot(dot); // make sure "dot" is valid
3737 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003738#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003739 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003740#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003741
3742 if (!isdigit(c))
3743 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3744 cnt = dot - begin_line(dot);
3745 // Try to stay off of the Newline
3746 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3747 dot--;
3748}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003749
Paul Fox35e9c5d2008-03-06 16:26:12 +00003750/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003751#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003752static int totalcmds = 0;
3753static int Mp = 85; // Movement command Probability
3754static int Np = 90; // Non-movement command Probability
3755static int Dp = 96; // Delete command Probability
3756static int Ip = 97; // Insert command Probability
3757static int Yp = 98; // Yank command Probability
3758static int Pp = 99; // Put command Probability
3759static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003760static const char chars[20] = "\t012345 abcdABCD-=.$";
3761static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003762 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003763 "broadcast", "the", "emergency", "of",
3764 "system", "quick", "brown", "fox",
3765 "jumped", "over", "lazy", "dogs",
3766 "back", "January", "Febuary", "March"
3767};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003768static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003769 "You should have received a copy of the GNU General Public License\n",
3770 "char c, cm, *cmd, *cmd1;\n",
3771 "generate a command by percentages\n",
3772 "Numbers may be typed as a prefix to some commands.\n",
3773 "Quit, discarding changes!\n",
3774 "Forced write, if permission originally not valid.\n",
3775 "In general, any ex or ed command (such as substitute or delete).\n",
3776 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3777 "Please get w/ me and I will go over it with you.\n",
3778 "The following is a list of scheduled, committed changes.\n",
3779 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3780 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3781 "Any question about transactions please contact Sterling Huxley.\n",
3782 "I will try to get back to you by Friday, December 31.\n",
3783 "This Change will be implemented on Friday.\n",
3784 "Let me know if you have problems accessing this;\n",
3785 "Sterling Huxley recently added you to the access list.\n",
3786 "Would you like to go to lunch?\n",
3787 "The last command will be automatically run.\n",
3788 "This is too much english for a computer geek.\n",
3789};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003790static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003791 "You should have received a copy of the GNU General Public License\n",
3792 "char c, cm, *cmd, *cmd1;\n",
3793 "generate a command by percentages\n",
3794 "Numbers may be typed as a prefix to some commands.\n",
3795 "Quit, discarding changes!\n",
3796 "Forced write, if permission originally not valid.\n",
3797 "In general, any ex or ed command (such as substitute or delete).\n",
3798 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3799 "Please get w/ me and I will go over it with you.\n",
3800 "The following is a list of scheduled, committed changes.\n",
3801 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3802 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3803 "Any question about transactions please contact Sterling Huxley.\n",
3804 "I will try to get back to you by Friday, December 31.\n",
3805 "This Change will be implemented on Friday.\n",
3806 "Let me know if you have problems accessing this;\n",
3807 "Sterling Huxley recently added you to the access list.\n",
3808 "Would you like to go to lunch?\n",
3809 "The last command will be automatically run.\n",
3810 "This is too much english for a computer geek.\n",
3811};
3812
3813// create a random command to execute
3814static void crash_dummy()
3815{
3816 static int sleeptime; // how long to pause between commands
3817 char c, cm, *cmd, *cmd1;
3818 int i, cnt, thing, rbi, startrbi, percent;
3819
3820 // "dot" movement commands
3821 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3822
3823 // is there already a command running?
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003824 if (chars_to_parse > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003825 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003826 cd0:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003827 startrbi = rbi = 0;
3828 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003829 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003830 // generate a command by percentages
3831 percent = (int) lrand48() % 100; // get a number from 0-99
3832 if (percent < Mp) { // Movement commands
3833 // available commands
3834 cmd = cmd1;
3835 M++;
3836 } else if (percent < Np) { // non-movement commands
3837 cmd = "mz<>\'\""; // available commands
3838 N++;
3839 } else if (percent < Dp) { // Delete commands
3840 cmd = "dx"; // available commands
3841 D++;
3842 } else if (percent < Ip) { // Inset commands
3843 cmd = "iIaAsrJ"; // available commands
3844 I++;
3845 } else if (percent < Yp) { // Yank commands
3846 cmd = "yY"; // available commands
3847 Y++;
3848 } else if (percent < Pp) { // Put commands
3849 cmd = "pP"; // available commands
3850 P++;
3851 } else {
3852 // We do not know how to handle this command, try again
3853 U++;
3854 goto cd0;
3855 }
3856 // randomly pick one of the available cmds from "cmd[]"
3857 i = (int) lrand48() % strlen(cmd);
3858 cm = cmd[i];
3859 if (strchr(":\024", cm))
3860 goto cd0; // dont allow colon or ctrl-T commands
3861 readbuffer[rbi++] = cm; // put cmd into input buffer
3862
3863 // now we have the command-
3864 // there are 1, 2, and multi char commands
3865 // find out which and generate the rest of command as necessary
3866 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
3867 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
3868 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
3869 cmd1 = "abcdefghijklmnopqrstuvwxyz";
3870 }
3871 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3872 c = cmd1[thing];
3873 readbuffer[rbi++] = c; // add movement to input buffer
3874 }
3875 if (strchr("iIaAsc", cm)) { // multi-char commands
3876 if (cm == 'c') {
3877 // change some thing
3878 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3879 c = cmd1[thing];
3880 readbuffer[rbi++] = c; // add movement to input buffer
3881 }
3882 thing = (int) lrand48() % 4; // what thing to insert
3883 cnt = (int) lrand48() % 10; // how many to insert
3884 for (i = 0; i < cnt; i++) {
3885 if (thing == 0) { // insert chars
3886 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
3887 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003888 strcat(readbuffer, words[(int) lrand48() % 20]);
3889 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003890 sleeptime = 0; // how fast to type
3891 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003892 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003893 sleeptime = 0; // how fast to type
3894 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003895 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003896 sleeptime = 0; // how fast to type
3897 }
3898 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003899 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003900 }
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003901 chars_to_parse = strlen(readbuffer);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003902 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003903 totalcmds++;
3904 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003905 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003906}
3907
3908// test to see if there are any errors
3909static void crash_test()
3910{
3911 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003912
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003913 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003914 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003915
3916 msg[0] = '\0';
3917 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003918 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003919 }
3920 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003921 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003922 }
3923 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003924 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003925 }
3926 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003927 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003928 }
3929 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003930 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003931 }
3932 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003933 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003934 }
3935
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003936 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00003937 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003938 totalcmds, last_input_char, msg, SOs, SOn);
3939 fflush(stdout);
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00003940 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003941 if (d[0] == '\n' || d[0] == '\r')
3942 break;
3943 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003944 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003945 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003946 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003947 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003948 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
3949 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
3950 oldtim = tim;
3951 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003952}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003953#endif