blob: e1aabab011b39cdb6de7bc3d3c1a37d823f8c97c [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
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +000026#define ENABLE_FEATURE_VI_CRASHME 0
27
28#if ENABLE_LOCALE_SUPPORT
Glenn L McGrath09adaca2002-12-02 21:18:10 +000029#define Isprint(c) isprint((c))
30#else
Denis Vlasenko2a51af22007-03-21 22:31:24 +000031/* 0x9b is Meta-ESC */
32#define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
Glenn L McGrath09adaca2002-12-02 21:18:10 +000033#endif
34
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000035enum {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +000036 MAX_TABSTOP = 32, // sanity limit
Denis Vlasenko88adfcd2007-12-22 15:40:13 +000037 // User input len. Need not be extra big.
38 // Lines in file being edited *can* be bigger than this.
39 MAX_INPUT_LEN = 128,
40 // Sanity limits. We have only one buffer of this size.
41 MAX_SCR_COLS = CONFIG_FEATURE_VI_MAX_LEN,
42 MAX_SCR_ROWS = CONFIG_FEATURE_VI_MAX_LEN,
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000043};
Eric Andersen3f980402001-04-04 17:31:15 +000044
45// Misc. non-Ascii keys that report an escape sequence
Denis Vlasenko2a51af22007-03-21 22:31:24 +000046#define VI_K_UP (char)128 // cursor key Up
47#define VI_K_DOWN (char)129 // cursor key Down
48#define VI_K_RIGHT (char)130 // Cursor Key Right
49#define VI_K_LEFT (char)131 // cursor key Left
50#define VI_K_HOME (char)132 // Cursor Key Home
51#define VI_K_END (char)133 // Cursor Key End
52#define VI_K_INSERT (char)134 // Cursor Key Insert
Denis Vlasenko88adfcd2007-12-22 15:40:13 +000053#define VI_K_DELETE (char)135 // Cursor Key Insert
54#define VI_K_PAGEUP (char)136 // Cursor Key Page Up
55#define VI_K_PAGEDOWN (char)137 // Cursor Key Page Down
56#define VI_K_FUN1 (char)138 // Function Key F1
57#define VI_K_FUN2 (char)139 // Function Key F2
58#define VI_K_FUN3 (char)140 // Function Key F3
59#define VI_K_FUN4 (char)141 // Function Key F4
60#define VI_K_FUN5 (char)142 // Function Key F5
61#define VI_K_FUN6 (char)143 // Function Key F6
62#define VI_K_FUN7 (char)144 // Function Key F7
63#define VI_K_FUN8 (char)145 // Function Key F8
64#define VI_K_FUN9 (char)146 // Function Key F9
65#define VI_K_FUN10 (char)147 // Function Key F10
66#define VI_K_FUN11 (char)148 // Function Key F11
67#define VI_K_FUN12 (char)149 // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +000068
Glenn L McGrath09adaca2002-12-02 21:18:10 +000069/* vt102 typical ESC sequence */
70/* terminal standout start/normal ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000071static const char SOs[] ALIGN1 = "\033[7m";
72static const char SOn[] ALIGN1 = "\033[0m";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000073/* terminal bell sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000074static const char bell[] ALIGN1 = "\007";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000075/* Clear-end-of-line and Clear-end-of-screen ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000076static const char Ceol[] ALIGN1 = "\033[0K";
77static const char Ceos[] ALIGN1 = "\033[0J";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000078/* Cursor motion arbitrary destination ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000079static const char CMrc[] ALIGN1 = "\033[%d;%dH";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000080/* Cursor motion up and down ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000081static const char CMup[] ALIGN1 = "\033[A";
82static const char CMdown[] ALIGN1 = "\n";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000083
84
Rob Landleybc68cd12006-03-10 19:22:06 +000085enum {
86 YANKONLY = FALSE,
87 YANKDEL = TRUE,
88 FORWARD = 1, // code depends on "1" for array index
89 BACK = -1, // code depends on "-1" for array index
90 LIMITED = 0, // how much of text[] in char_search
91 FULL = 1, // how much of text[] in char_search
Eric Andersen3f980402001-04-04 17:31:15 +000092
Rob Landleybc68cd12006-03-10 19:22:06 +000093 S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
94 S_TO_WS = 2, // used in skip_thing() for moving "dot"
95 S_OVER_WS = 3, // used in skip_thing() for moving "dot"
96 S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
Denis Vlasenko8e858e22007-03-07 09:35:43 +000097 S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
Rob Landleybc68cd12006-03-10 19:22:06 +000098};
Eric Andersen3f980402001-04-04 17:31:15 +000099
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000100/* vi.c expects chars to be unsigned. */
101/* busybox build system provides that, but it's better */
102/* to audit and fix the source */
Eric Andersen3f980402001-04-04 17:31:15 +0000103
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000104static smallint vi_setops;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000105#define VI_AUTOINDENT 1
106#define VI_SHOWMATCH 2
107#define VI_IGNORECASE 4
108#define VI_ERR_METHOD 8
109#define autoindent (vi_setops & VI_AUTOINDENT)
110#define showmatch (vi_setops & VI_SHOWMATCH )
111#define ignorecase (vi_setops & VI_IGNORECASE)
112/* indicate error with beep or flash */
113#define err_method (vi_setops & VI_ERR_METHOD)
114
Eric Andersen3f980402001-04-04 17:31:15 +0000115
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000116static smallint editing; // >0 while we are editing a file
117 // [code audit says "can be 0 or 1 only"]
118static smallint cmd_mode; // 0=command 1=insert 2=replace
119static smallint file_modified; // buffer contents changed
120static smallint last_file_modified = -1;
121static int fn_start; // index of first cmd line file name
122static int save_argc; // how many file names on cmd line
123static int cmdcnt; // repetition count
124static int rows, columns; // the terminal screen is this size
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000125static int crow, ccol; // cursor is on Crow x Ccol
126static int offset; // chars scrolled off the screen to the left
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000127static char *status_buffer; // mesages to the user
Paul Fox8552aec2005-09-16 12:20:05 +0000128#define STATUS_BUFFER_LEN 200
129static int have_status_msg; // is default edit status needed?
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000130 // [don't make smallint!]
Paul Fox8552aec2005-09-16 12:20:05 +0000131static int last_status_cksum; // hash of current status line
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000132static char *current_filename; // current file name
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000133//static char *text, *end; // pointers to the user data in memory
134static char *screen; // pointer to the virtual screen buffer
135static int screensize; // and its size
136static char *screenbegin; // index into text[], of top line on the screen
137//static char *dot; // where all the action takes place
Eric Andersen3f980402001-04-04 17:31:15 +0000138static int tabstop;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000139static char erase_char; // the users erase character
140static char last_input_char; // last char read from user
141static char last_forward_char; // last char searched for with 'f'
Eric Andersen3f980402001-04-04 17:31:15 +0000142
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000143#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000144//static smallint vi_readonly, readonly;
145static smallint readonly_mode = 0;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000146#define SET_READONLY_FILE(flags) ((flags) |= 0x01)
147#define SET_READONLY_MODE(flags) ((flags) |= 0x02)
148#define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000149#else
150#define readonly_mode 0
151#define SET_READONLY_FILE(flags)
152#define SET_READONLY_MODE(flags)
153#define UNSET_READONLY_FILE(flags)
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000154#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000155
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000156#if ENABLE_FEATURE_VI_DOT_CMD
157static smallint adding2q; // are we currently adding user input to q
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000158static char *last_modifying_cmd; // [MAX_INPUT_LEN] last modifying cmd for "."
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000159static char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
160#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000161#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Eric Andersen822c3832001-05-07 17:37:43 +0000162static int last_row; // where the cursor was last moved to
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000163#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000164#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000165static int my_pid;
166#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000167#if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000168static char *modifying_cmds; // cmds that modify text[]
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000169#endif
170#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000171static char *last_search_pattern; // last pattern from a '/' or '?' search
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000172#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000173
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000174/* Moving biggest data to malloced space... */
175struct globals {
176 /* many references - keep near the top of globals */
177 char *text, *end; // pointers to the user data in memory
178 char *dot; // where all the action takes place
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000179 int text_size; // size of the allocated buffer
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000180#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000181 int YDreg, Ureg; // default delete register and orig line for "U"
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000182 char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000183 char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
184 char *context_start, *context_end;
185#endif
186 /* a few references only */
187#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000188 jmp_buf restart; // catch_sig()
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000189#endif
190 struct termios term_orig, term_vi; // remember what the cooked mode was
191#if ENABLE_FEATURE_VI_COLON
192 char *initial_cmds[3]; // currently 2 entries, NULL terminated
193#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000194 // Should be just enough to hold a key sequence,
195 // but CRASME mode uses it as generated command buffer too
196#if ENABLE_FEATURE_VI_CRASHME
197 char readbuffer[128];
198#else
199 char readbuffer[32];
200#endif
201
202 char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000203};
204#define G (*ptr_to_globals)
205#define text (G.text )
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000206#define text_size (G.text_size )
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000207#define end (G.end )
208#define dot (G.dot )
209#define reg (G.reg )
210#define YDreg (G.YDreg )
211#define Ureg (G.Ureg )
212#define mark (G.mark )
213#define context_start (G.context_start )
214#define context_end (G.context_end )
215#define restart (G.restart )
216#define term_orig (G.term_orig )
217#define term_vi (G.term_vi )
218#define initial_cmds (G.initial_cmds )
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000219#define readbuffer (G.readbuffer )
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000220#define scr_out_buf (G.scr_out_buf )
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000221#define INIT_G() do { \
222 PTR_TO_GLOBALS = xzalloc(sizeof(G)); \
223} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000224
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000225static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000226static void edit_file(char *); // edit one file
227static void do_cmd(char); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000228static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000229static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
230static char *begin_line(char *); // return pointer to cur line B-o-l
231static char *end_line(char *); // return pointer to cur line E-o-l
232static char *prev_line(char *); // return pointer to prev line B-o-l
233static char *next_line(char *); // return pointer to next line B-o-l
234static char *end_screen(void); // get pointer to last char on screen
235static int count_lines(char *, char *); // count line from start to stop
236static char *find_line(int); // find begining of line #li
237static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000238static void dot_left(void); // move dot left- dont leave line
239static void dot_right(void); // move dot right- dont leave line
240static void dot_begin(void); // move dot to B-o-l
241static void dot_end(void); // move dot to E-o-l
242static void dot_next(void); // move dot to next line B-o-l
243static void dot_prev(void); // move dot to prev line B-o-l
244static void dot_scroll(int, int); // move the screen up or down
245static void dot_skip_over_ws(void); // move dot pat WS
246static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000247static char *bound_dot(char *); // make sure text[0] <= P < "end"
248static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000249static char *char_insert(char *, char); // insert the char c at 'p'
250static char *stupid_insert(char *, char); // stupidly insert the char c at 'p'
251static char find_range(char **, char **, char); // return pointers for an object
252static int st_test(char *, int, int, char *); // helper for skip_thing()
253static char *skip_thing(char *, int, int, int); // skip some object
254static char *find_pair(char *, char); // find matching pair () [] {}
255static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
256static char *text_hole_make(char *, int); // at "p", make a 'size' byte hole
257static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000258static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000259static void rawmode(void); // set "raw" mode on tty
260static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000261// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
262static int mysleep(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000263static char readit(void); // read (maybe cursor) key from stdin
264static char get_one_char(void); // read 1 char from stdin
265static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000266#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000267static int file_insert(const char *, char *, int);
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000268#else
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000269static int file_insert(const char *, char *);
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000270#endif
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000271static int file_write(char *, char *, char *);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000272#if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
273#define place_cursor(a, b, optimize) place_cursor(a, b)
274#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000275static void place_cursor(int, int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000276static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000277static void clear_to_eol(void);
278static void clear_to_eos(void);
279static void standout_start(void); // send "start reverse video" sequence
280static void standout_end(void); // send "end reverse video" sequence
281static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000282static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000283static void status_line(const char *, ...); // print to status buf
284static void status_line_bold(const char *, ...);
285static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000286static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000287static void redraw(int); // force a full screen refresh
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000288static char* format_line(char*, int);
Eric Andersen3f980402001-04-04 17:31:15 +0000289static void refresh(int); // update the terminal from screen[]
290
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000291static void Indicate_Error(void); // use flash or beep to indicate error
292#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000293static void Hit_Return(void);
294
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000295#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000296static char *char_search(char *, const char *, int, int); // search for pattern starting at p
297static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000298#endif
299#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000300static char *get_one_address(char *, int *); // get colon addr, if present
301static char *get_address(char *, int *, int *); // get two colon addrs, if present
302static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000303#endif
304#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000305static void winch_sig(int); // catch window size changes
306static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000307static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000308#endif
309#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000310static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000311static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000312#else
313#define end_cmd_q() ((void)0)
314#endif
315#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000316static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000317#endif
318#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000319static char *string_insert(char *, char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000320#endif
321#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000322static char *text_yank(char *, char *, int); // save copy of "p" into a register
323static char what_reg(void); // what is letter of current YDreg
324static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000325#endif
326#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000327static void crash_dummy();
328static void crash_test();
329static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000330#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000331
332
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000333static void write1(const char *out)
334{
335 fputs(out, stdout);
336}
337
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000338int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000339int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000340{
Eric Andersend402edf2001-04-04 19:29:48 +0000341 int c;
Paul Fox8552aec2005-09-16 12:20:05 +0000342 RESERVE_CONFIG_BUFFER(STATUS_BUFFER, STATUS_BUFFER_LEN);
Eric Andersen3f980402001-04-04 17:31:15 +0000343
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000344#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000345 my_pid = getpid();
346#endif
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000347
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000348 INIT_G();
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000349
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000350#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000351 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000352#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000353
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000354 status_buffer = STATUS_BUFFER;
Paul Fox8552aec2005-09-16 12:20:05 +0000355 last_status_cksum = 0;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000356 text = NULL;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000357
Denis Vlasenko2414a962007-07-18 22:03:40 +0000358#ifdef NO_SUCH_APPLET_YET
359 /* If we aren't "vi", we are "view" */
360 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000361 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000362 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000363#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000364
Bernhard Reutner-Fischer73f56bb2007-09-22 21:18:46 +0000365 vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000366#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko2414a962007-07-18 22:03:40 +0000367 memset(reg, 0, sizeof(reg)); // init the yank regs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000368#endif
369#if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000370 modifying_cmds = (char *) "aAcCdDiIJoOpPrRsxX<>~"; // cmds modifying text[]
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000371#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000372
Denis Vlasenkof9234132007-03-21 00:03:42 +0000373 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000374 // 2- process EXINIT variable from environment
375 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000376#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000377 {
378 char *p = getenv("EXINIT");
379 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000380 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000381 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000382#endif
383 while ((c = getopt(argc, argv, "hCR" USE_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000384 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000385#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000386 case 'C':
387 crashme = 1;
388 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000389#endif
390#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000391 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000392 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000393 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000394#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000395 //case 'r': // recover flag- ignore- we don't use tmp file
396 //case 'x': // encryption flag- ignore
397 //case 'c': // execute command first
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000398#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000399 case 'c': // cmd line vi command
400 if (*optarg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000401 initial_cmds[initial_cmds[0] != 0] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000402 break;
Eric Andersen822c3832001-05-07 17:37:43 +0000403 //case 'h': // help -- just use default
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000404#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000405 default:
406 show_help();
Eric Andersendd8500b2001-07-02 18:06:14 +0000407 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000408 }
409 }
410
411 // The argv array can be used by the ":next" and ":rewind" commands
412 // save optind.
413 fn_start = optind; // remember first file name for :next and :rew
414 save_argc = argc;
415
416 //----- This is the main file handling loop --------------
417 if (optind >= argc) {
Eric Andersen3f980402001-04-04 17:31:15 +0000418 edit_file(0);
419 } else {
420 for (; optind < argc; optind++) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000421 edit_file(argv[optind]);
Eric Andersen3f980402001-04-04 17:31:15 +0000422 }
423 }
424 //-----------------------------------------------------------
425
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000426 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000427}
428
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000429/* read text from file or create an empty buf */
430/* will also update current_filename */
431static int init_text_buffer(char *fn)
432{
433 int rc;
434 int size = file_size(fn); // file size. -1 means does not exist.
435
436 /* allocate/reallocate text buffer */
437 free(text);
438 text_size = size * 2;
439 if (text_size < 10240)
440 text_size = 10240; // have a minimum size for new files
441 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000442
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000443 if (fn != current_filename) {
444 free(current_filename);
445 current_filename = xstrdup(fn);
446 }
447 if (size < 0) {
448 // file dont exist. Start empty buf with dummy line
449 char_insert(text, '\n');
450 rc = 0;
451 } else {
452 rc = file_insert(fn, text
453 USE_FEATURE_VI_READONLY(, 1));
454 }
455 file_modified = 0;
456 last_file_modified = -1;
457#if ENABLE_FEATURE_VI_YANKMARK
458 /* init the marks. */
459 memset(mark, 0, sizeof(mark));
460#endif
461 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000462}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000463
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000464static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000465{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000466 char c;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000467 int size;
Eric Andersen3f980402001-04-04 17:31:15 +0000468
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000469#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000470 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000471#endif
472#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000473 static char *cur_line;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000474#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000475
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000476 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000477 rawmode();
478 rows = 24;
479 columns = 80;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000480 size = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000481 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Rob Landleye5e1a102006-06-21 01:15:36 +0000482 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000483 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
484 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
485 }
Eric Andersen3f980402001-04-04 17:31:15 +0000486 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000487 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000488
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000489#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000490 YDreg = 26; // default Yank/Delete reg
491 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000492 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000493#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000494
Eric Andersen3f980402001-04-04 17:31:15 +0000495 last_forward_char = last_input_char = '\0';
496 crow = 0;
497 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000498
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000499#if ENABLE_FEATURE_VI_USE_SIGNALS
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000500 catch_sig(0);
Eric Andersen3f980402001-04-04 17:31:15 +0000501 signal(SIGWINCH, winch_sig);
502 signal(SIGTSTP, suspend_sig);
503 sig = setjmp(restart);
504 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000505 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000506 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000507#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000508
Eric Andersen3f980402001-04-04 17:31:15 +0000509 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
510 cmdcnt = 0;
511 tabstop = 8;
512 offset = 0; // no horizontal offset
513 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000514#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000515 free(last_modifying_cmd);
516 free(ioq_start);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000517 ioq = ioq_start = last_modifying_cmd = NULL;
Eric Andersen3f980402001-04-04 17:31:15 +0000518 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000519#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000520 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000521
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000522#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000523 {
524 char *p, *q;
525 int n = 0;
526
527 while ((p = initial_cmds[n])) {
528 do {
529 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000530 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000531 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000532 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000533 *p++ = '\0';
534 if (*q)
535 colon(q);
536 } while (p);
537 free(initial_cmds[n]);
538 initial_cmds[n] = NULL;
539 n++;
540 }
541 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000542#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000543 //------This is the main Vi cmd handling loop -----------------------
544 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000545#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000546 if (crashme > 0) {
547 if ((end - text) > 1) {
548 crash_dummy(); // generate a random command
549 } else {
550 crashme = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000551 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 +0000552 refresh(FALSE);
553 }
554 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000555#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000556 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000557#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000558 // save a copy of the current line- for the 'U" command
559 if (begin_line(dot) != cur_line) {
560 cur_line = begin_line(dot);
561 text_yank(begin_line(dot), end_line(dot), Ureg);
562 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000563#endif
564#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000565 // These are commands that change text[].
566 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000567 if (!adding2q && ioq_start == NULL
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000568 && strchr(modifying_cmds, c)
569 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000570 start_new_cmd_q(c);
571 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000572#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000573 do_cmd(c); // execute the user command
574 //
575 // poll to see if there is input already waiting. if we are
576 // not able to display output fast enough to keep up, skip
577 // the display update until we catch up with input.
578 if (mysleep(0) == 0) {
579 // no input pending- so update output
580 refresh(FALSE);
581 show_status_line();
582 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000583#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000584 if (crashme > 0)
585 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000586#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000587 }
588 //-------------------------------------------------------------------
589
Eric Andersen822c3832001-05-07 17:37:43 +0000590 place_cursor(rows, 0, FALSE); // go to bottom of screen
Eric Andersen3f980402001-04-04 17:31:15 +0000591 clear_to_eol(); // Erase to end of line
592 cookmode();
593}
594
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000595//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000596#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000597static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000598{
599 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000600 char *q;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000601 USE_FEATURE_VI_YANKMARK(char c;)
602 USE_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000603
604 *addr = -1; // assume no addr
605 if (*p == '.') { // the current line
606 p++;
607 q = begin_line(dot);
608 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000609 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000610#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000611 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000612 p++;
613 c = tolower(*p);
614 p++;
615 if (c >= 'a' && c <= 'z') {
616 // we have a mark
617 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000618 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000619 if (q != NULL) { // is mark valid
620 *addr = count_lines(text, q); // count lines
621 }
622 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000623 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000624#endif
625#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000626 else if (*p == '/') { // a search pattern
627 q = strchrnul(++p, '/');
628 pat = xstrndup(p, q - p); // save copy of pattern
629 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000630 if (*p == '/')
631 p++;
632 q = char_search(dot, pat, FORWARD, FULL);
633 if (q != NULL) {
634 *addr = count_lines(text, q);
635 }
636 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000637 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000638#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000639 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000640 p++;
641 q = begin_line(end - 1);
642 *addr = count_lines(text, q);
643 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000644 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000645 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000646 } else {
647 // unrecognised address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000648 *addr = -1;
649 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000650 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000651}
652
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000653static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000654{
655 //----- get the address' i.e., 1,3 'a,'b -----
656 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000657 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000658 p++; // skip over leading spaces
659 if (*p == '%') { // alias for 1,$
660 p++;
661 *b = 1;
662 *e = count_lines(text, end-1);
663 goto ga0;
664 }
665 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000666 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000667 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000668 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000669 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000670 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000671 p++;
672 // get SECOND addr, if present
673 p = get_one_address(p, e);
674 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000675 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000676 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000677 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000678 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000679}
680
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000681#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000682static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000683 const char *short_opname, int opt)
684{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000685 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000686 int l = strlen(opname) - 1; /* opname have + ' ' */
687
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000688 if (strncasecmp(a, opname, l) == 0
689 || strncasecmp(a, short_opname, 2) == 0
690 ) {
691 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000692 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000693 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000694 vi_setops |= opt;
695 }
696}
697#endif
698
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000699// buf must be no longer than MAX_INPUT_LEN!
700static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000701{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000702 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000703 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000704 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000705 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000706
707 // :3154 // if (-e line 3154) goto it else stay put
708 // :4,33w! foo // write a portion of buffer to file "foo"
709 // :w // write all of buffer to current file
710 // :q // quit
711 // :q! // quit- dont care about modified file
712 // :'a,'z!sort -u // filter block through sort
713 // :'f // goto mark "f"
714 // :'fl // list literal the mark "f" line
715 // :.r bar // read file "bar" into buffer before dot
716 // :/123/,/abc/d // delete lines from "123" line to "abc" line
717 // :/xyz/ // goto the "xyz" line
718 // :s/find/replace/ // substitute pattern "find" with "replace"
719 // :!<cmd> // run <cmd> then return
720 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000721
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000722 if (!buf[0])
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000723 goto vc1;
724 if (*buf == ':')
725 buf++; // move past the ':'
726
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000727 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000728 b = e = -1;
729 q = text; // assume 1,$ for the range
730 r = end - 1;
731 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000732 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000733
734 // look for optional address(es) :. :1 :1,9 :'q,'a :%
735 buf = get_address(buf, &b, &e);
736
737 // remember orig command line
738 orig_buf = buf;
739
740 // get the COMMAND into cmd[]
741 buf1 = cmd;
742 while (*buf != '\0') {
743 if (isspace(*buf))
744 break;
745 *buf1++ = *buf++;
746 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000747 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000748 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000749 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000750 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000751 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000752 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000753 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000754 if (buf1) {
755 useforce = TRUE;
756 *buf1 = '\0'; // get rid of !
757 }
758 if (b >= 0) {
759 // if there is only one addr, then the addr
760 // is the line number of the single line the
761 // user wants. So, reset the end
762 // pointer to point at end of the "b" line
763 q = find_line(b); // what line is #b
764 r = end_line(q);
765 li = 1;
766 }
767 if (e >= 0) {
768 // we were given two addrs. change the
769 // end pointer to the addr given by user.
770 r = find_line(e); // what line is #e
771 r = end_line(r);
772 li = e - b + 1;
773 }
774 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000775 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000776 if (i == 0) { // :123CR goto line #123
777 if (b >= 0) {
778 dot = find_line(b); // what line is #b
779 dot_skip_over_ws();
780 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000781 }
782#if ENABLE_FEATURE_ALLOW_EXEC
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000783 else if (strncmp(cmd, "!", 1) == 0) { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000784 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000785 // :!ls run the <cmd>
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000786 alarm(0); // wait for input- no alarms
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000787 place_cursor(rows - 1, 0, FALSE); // go to Status line
788 clear_to_eol(); // clear the line
789 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000790 retcode = system(orig_buf + 1); // run the cmd
791 if (retcode)
792 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000793 rawmode();
794 Hit_Return(); // let user see results
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000795 alarm(3); // done waiting for input
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000796 }
797#endif
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000798 else if (strncmp(cmd, "=", i) == 0) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000799 if (b < 0) { // no addr given- use defaults
800 b = e = count_lines(text, dot);
801 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000802 status_line("%d", b);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000803 } else if (strncasecmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000804 if (b < 0) { // no addr given- use defaults
805 q = begin_line(dot); // assume .,. for the range
806 r = end_line(dot);
807 }
808 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
809 dot_skip_over_ws();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000810 } else if (strncasecmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000811 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000812 if (file_modified && !useforce) {
813 status_line_bold("No write since last change (:edit! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000814 goto vc1;
815 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000816 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000817 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000818 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000819 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000820 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000821 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000822 } else {
823 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000824 status_line_bold("No current filename");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000825 goto vc1;
826 }
827
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000828 if (init_text_buffer(fn) < 0)
829 goto vc1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000830
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000831#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000832 if (Ureg >= 0 && Ureg < 28 && reg[Ureg] != 0) {
833 free(reg[Ureg]); // free orig line reg- for 'U'
834 reg[Ureg]= 0;
835 }
836 if (YDreg >= 0 && YDreg < 28 && reg[YDreg] != 0) {
837 free(reg[YDreg]); // free default yank/delete register
838 reg[YDreg]= 0;
839 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000840#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000841 // how many lines in text[]?
842 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000843 status_line("\"%s\"%s"
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000844 USE_FEATURE_VI_READONLY("%s")
845 " %dL, %dC", current_filename,
846 (file_size(fn) < 0 ? " [New file]" : ""),
847 USE_FEATURE_VI_READONLY(
848 ((readonly_mode) ? " [Readonly]" : ""),
849 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000850 li, ch);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000851 } else if (strncasecmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000852 if (b != -1 || e != -1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000853 not_implemented("No address allowed on this command");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000854 goto vc1;
855 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000856 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000857 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000858 free(current_filename);
859 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000860 } else {
861 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +0000862 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000863 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000864 } else if (strncasecmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000865 // print out values of all features
866 place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
867 clear_to_eol(); // clear the line
868 cookmode();
869 show_help();
870 rawmode();
871 Hit_Return();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000872 } else if (strncasecmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000873 if (b < 0) { // no addr given- use defaults
874 q = begin_line(dot); // assume .,. for the range
875 r = end_line(dot);
876 }
877 place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
878 clear_to_eol(); // clear the line
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000879 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000880 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000881 int c_is_no_print;
882
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000883 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +0000884 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000885 if (c_is_no_print) {
886 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000887 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000888 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000889 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000890 write1("$\r");
891 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000892 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000893 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000894 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000895 else
896 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000897 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000898 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000899 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000900 standout_end();
901 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000902#if ENABLE_FEATURE_VI_SET
903 vc2:
904#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000905 Hit_Return();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000906 } else if (strncasecmp(cmd, "quit", i) == 0 // Quit
907 || strncasecmp(cmd, "next", i) == 0 // edit next file
908 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000909 if (useforce) {
910 // force end of argv list
911 if (*cmd == 'q') {
912 optind = save_argc;
913 }
914 editing = 0;
915 goto vc1;
916 }
917 // don't exit if the file been modified
918 if (file_modified) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000919 status_line_bold("No write since last change (:%s! overrides)",
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000920 (*cmd == 'q' ? "quit" : "next"));
921 goto vc1;
922 }
923 // are there other file to edit
924 if (*cmd == 'q' && optind < save_argc - 1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000925 status_line_bold("%d more file to edit", (save_argc - optind - 1));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000926 goto vc1;
927 }
928 if (*cmd == 'n' && optind >= save_argc - 1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000929 status_line_bold("No more files to edit");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000930 goto vc1;
931 }
932 editing = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000933 } else if (strncasecmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000934 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000935 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000936 status_line_bold("No filename given");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000937 goto vc1;
938 }
939 if (b < 0) { // no addr given- use defaults
940 q = begin_line(dot); // assume "dot"
941 }
942 // read after current line- unless user said ":0r foo"
943 if (b != 0)
944 q = next_line(q);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000945 ch = file_insert(fn, q USE_FEATURE_VI_READONLY(, 0));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000946 if (ch < 0)
947 goto vc1; // nothing was inserted
948 // how many lines in text[]?
949 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000950 status_line("\"%s\""
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000951 USE_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000952 " %dL, %dC", fn,
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000953 USE_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000954 li, ch);
955 if (ch > 0) {
956 // if the insert is before "dot" then we need to update
957 if (q <= dot)
958 dot += ch;
Paul Fox8552aec2005-09-16 12:20:05 +0000959 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000960 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000961 } else if (strncasecmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000962 if (file_modified && !useforce) {
963 status_line_bold("No write since last change (:rewind! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000964 } else {
965 // reset the filenames to edit
966 optind = fn_start - 1;
967 editing = 0;
968 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000969#if ENABLE_FEATURE_VI_SET
Denis Vlasenkof9234132007-03-21 00:03:42 +0000970 } else if (strncasecmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000971#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +0000972 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000973#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000974 i = 0; // offset into args
Denis Vlasenkof9234132007-03-21 00:03:42 +0000975 // only blank is regarded as args delmiter. What about tab '\t' ?
976 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000977 // print out values of all options
978 place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
979 clear_to_eol(); // clear the line
980 printf("----------------------------------------\r\n");
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000981#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000982 if (!autoindent)
983 printf("no");
984 printf("autoindent ");
985 if (!err_method)
986 printf("no");
987 printf("flash ");
988 if (!ignorecase)
989 printf("no");
990 printf("ignorecase ");
991 if (!showmatch)
992 printf("no");
993 printf("showmatch ");
994 printf("tabstop=%d ", tabstop);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000995#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000996 printf("\r\n");
997 goto vc2;
998 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000999#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001000 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001001 while (*argp) {
Denis Vlasenkof9234132007-03-21 00:03:42 +00001002 if (strncasecmp(argp, "no", 2) == 0)
1003 i = 2; // ":set noautoindent"
1004 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
1005 setops(argp, "flash ", i, "fl", VI_ERR_METHOD);
1006 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
1007 setops(argp, "showmatch ", i, "ic", VI_SHOWMATCH);
1008 /* tabstopXXXX */
1009 if (strncasecmp(argp + i, "tabstop=%d ", 7) == 0) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001010 sscanf(strchr(argp + i, '='), "tabstop=%d" + 7, &ch);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00001011 if (ch > 0 && ch <= MAX_TABSTOP)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001012 tabstop = ch;
1013 }
1014 while (*argp && *argp != ' ')
1015 argp++; // skip to arg delimiter (i.e. blank)
1016 while (*argp && *argp == ' ')
1017 argp++; // skip all delimiting blanks
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001018 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001019#endif /* FEATURE_VI_SETOPTS */
1020#endif /* FEATURE_VI_SET */
1021#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001022 } else if (strncasecmp(cmd, "s", 1) == 0) { // substitute a pattern with a replacement pattern
1023 char *ls, *F, *R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001024 int gflag;
1025
1026 // F points to the "find" pattern
1027 // R points to the "replace" pattern
1028 // replace the cmd line delimiters "/" with NULLs
1029 gflag = 0; // global replace flag
1030 c = orig_buf[1]; // what is the delimiter
1031 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001032 R = strchr(F, c); // middle delimiter
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001033 if (!R) goto colon_s_fail;
1034 *R++ = '\0'; // terminate "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001035 buf1 = strchr(R, c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001036 if (!buf1) goto colon_s_fail;
1037 *buf1++ = '\0'; // terminate "replace"
1038 if (*buf1 == 'g') { // :s/foo/bar/g
1039 buf1++;
1040 gflag++; // turn on gflag
1041 }
1042 q = begin_line(q);
1043 if (b < 0) { // maybe :s/foo/bar/
1044 q = begin_line(dot); // start with cur line
1045 b = count_lines(text, q); // cur line number
1046 }
1047 if (e < 0)
1048 e = b; // maybe :.s/foo/bar/
1049 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
1050 ls = q; // orig line start
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001051 vc4:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001052 buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001053 if (buf1) {
1054 // we found the "find" pattern - delete it
1055 text_hole_delete(buf1, buf1 + strlen(F) - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001056 // inset the "replace" patern
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001057 string_insert(buf1, R); // insert the string
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001058 // check for "global" :s/foo/bar/g
1059 if (gflag == 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001060 if ((buf1 + strlen(R)) < end_line(ls)) {
1061 q = buf1 + strlen(R);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001062 goto vc4; // don't let q move past cur line
1063 }
1064 }
1065 }
1066 q = next_line(ls);
1067 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001068#endif /* FEATURE_VI_SEARCH */
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001069 } else if (strncasecmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001070 status_line("%s", BB_VER " " BB_BT);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001071 } else if (strncasecmp(cmd, "write", i) == 0 // write text to file
1072 || strncasecmp(cmd, "wq", i) == 0
1073 || strncasecmp(cmd, "wn", i) == 0
1074 || strncasecmp(cmd, "x", i) == 0
1075 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001076 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001077 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001078 fn = args;
1079 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001080#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001081 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001082 status_line_bold("\"%s\" File is read only", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001083 goto vc3;
1084 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001085#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001086 // how many lines in text[]?
1087 li = count_lines(q, r);
1088 ch = r - q + 1;
1089 // see if file exists- if not, its just a new file request
1090 if (useforce) {
1091 // if "fn" is not write-able, chmod u+w
1092 // sprintf(syscmd, "chmod u+w %s", fn);
1093 // system(syscmd);
1094 forced = TRUE;
1095 }
1096 l = file_write(fn, q, r);
1097 if (useforce && forced) {
1098 // chmod u-w
1099 // sprintf(syscmd, "chmod u-w %s", fn);
1100 // system(syscmd);
1101 forced = FALSE;
1102 }
Paul Fox61e45db2005-10-09 14:43:22 +00001103 if (l < 0) {
1104 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001105 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001106 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001107 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001108 if (q == text && r == end - 1 && l == ch) {
1109 file_modified = 0;
1110 last_file_modified = -1;
1111 }
Paul Fox9360f422006-03-27 21:51:16 +00001112 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n' ||
1113 cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N')
1114 && l == ch) {
Paul Fox61e45db2005-10-09 14:43:22 +00001115 editing = 0;
1116 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001117 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001118#if ENABLE_FEATURE_VI_READONLY
1119 vc3:;
1120#endif
1121#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001122 } else if (strncasecmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001123 if (b < 0) { // no addr given- use defaults
1124 q = begin_line(dot); // assume .,. for the range
1125 r = end_line(dot);
1126 }
1127 text_yank(q, r, YDreg);
1128 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001129 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001130 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001131#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001132 } else {
1133 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001134 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001135 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001136 vc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001137 dot = bound_dot(dot); // make sure "dot" is valid
1138 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001139#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001140 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001141 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001142#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001143}
Paul Fox61e45db2005-10-09 14:43:22 +00001144
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001145#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001146
1147static void Hit_Return(void)
1148{
1149 char c;
1150
1151 standout_start(); // start reverse video
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001152 write1("[Hit return to continue]");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001153 standout_end(); // end reverse video
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001154 while ((c = get_one_char()) != '\n' && c != '\r')
1155 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001156 redraw(TRUE); // force redraw all
1157}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001158
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001159static int next_tabstop(int col)
1160{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001161 return col + ((tabstop - 1) - (col % tabstop));
1162}
1163
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001164//----- Synchronize the cursor to Dot --------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001165static void sync_cursor(char * d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001166{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001167 char *beg_cur; // begin and end of "d" line
1168 char *end_scr; // begin and end of screen
1169 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001170 int cnt, ro, co;
1171
1172 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001173
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001174 end_scr = end_screen(); // last char of screen
1175
1176 if (beg_cur < screenbegin) {
1177 // "d" is before top line on screen
1178 // how many lines do we have to move
1179 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001180 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001181 screenbegin = beg_cur;
1182 if (cnt > (rows - 1) / 2) {
1183 // we moved too many lines. put "dot" in middle of screen
1184 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1185 screenbegin = prev_line(screenbegin);
1186 }
1187 }
1188 } else if (beg_cur > end_scr) {
1189 // "d" is after bottom line on screen
1190 // how many lines do we have to move
1191 cnt = count_lines(end_scr, beg_cur);
1192 if (cnt > (rows - 1) / 2)
1193 goto sc1; // too many lines
1194 for (ro = 0; ro < cnt - 1; ro++) {
1195 // move screen begin the same amount
1196 screenbegin = next_line(screenbegin);
1197 // now, move the end of screen
1198 end_scr = next_line(end_scr);
1199 end_scr = end_line(end_scr);
1200 }
1201 }
1202 // "d" is on screen- find out which row
1203 tp = screenbegin;
1204 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1205 if (tp == beg_cur)
1206 break;
1207 tp = next_line(tp);
1208 }
1209
1210 // find out what col "d" is on
1211 co = 0;
1212 do { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001213 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001214 break;
1215 if (*tp == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001216 if (d == tp && cmd_mode) { /* handle tabs like real vi */
1217 break;
1218 } else {
1219 co = next_tabstop(co);
1220 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001221 } else if (*tp < ' ' || *tp == 127) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001222 co++; // display as ^X, use 2 columns
1223 }
1224 } while (tp++ < d && ++co);
1225
1226 // "co" is the column where "dot" is.
1227 // The screen has "columns" columns.
1228 // The currently displayed columns are 0+offset -- columns+ofset
1229 // |-------------------------------------------------------------|
1230 // ^ ^ ^
1231 // offset | |------- columns ----------------|
1232 //
1233 // If "co" is already in this range then we do not have to adjust offset
1234 // but, we do have to subtract the "offset" bias from "co".
1235 // If "co" is outside this range then we have to change "offset".
1236 // If the first char of a line is a tab the cursor will try to stay
1237 // in column 7, but we have to set offset to 0.
1238
1239 if (co < 0 + offset) {
1240 offset = co;
1241 }
1242 if (co >= columns + offset) {
1243 offset = co - columns + 1;
1244 }
1245 // if the first char of the line is a tab, and "dot" is sitting on it
1246 // force offset to 0.
1247 if (d == beg_cur && *d == '\t') {
1248 offset = 0;
1249 }
1250 co -= offset;
1251
1252 *row = ro;
1253 *col = co;
1254}
1255
1256//----- Text Movement Routines ---------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001257static char *begin_line(char * p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001258{
1259 while (p > text && p[-1] != '\n')
1260 p--; // go to cur line B-o-l
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001261 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001262}
1263
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001264static char *end_line(char * p) // return pointer to NL of cur line line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001265{
1266 while (p < end - 1 && *p != '\n')
1267 p++; // go to cur line E-o-l
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001268 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001269}
1270
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00001271static char *dollar_line(char * p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001272{
1273 while (p < end - 1 && *p != '\n')
1274 p++; // go to cur line E-o-l
1275 // Try to stay off of the Newline
1276 if (*p == '\n' && (p - begin_line(p)) > 0)
1277 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001278 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001279}
1280
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001281static char *prev_line(char * p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001282{
1283 p = begin_line(p); // goto begining of cur line
1284 if (p[-1] == '\n' && p > text)
1285 p--; // step to prev line
1286 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001287 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001288}
1289
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001290static char *next_line(char * p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001291{
1292 p = end_line(p);
1293 if (*p == '\n' && p < end - 1)
1294 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001295 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001296}
1297
1298//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001299static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001300{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001301 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001302 int cnt;
1303
1304 // find new bottom line
1305 q = screenbegin;
1306 for (cnt = 0; cnt < rows - 2; cnt++)
1307 q = next_line(q);
1308 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001309 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001310}
1311
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001312static int count_lines(char * start, char * stop) // count line from start to stop
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001313{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001314 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001315 int cnt;
1316
1317 if (stop < start) { // start and stop are backwards- reverse them
1318 q = start;
1319 start = stop;
1320 stop = q;
1321 }
1322 cnt = 0;
1323 stop = end_line(stop); // get to end of this line
1324 for (q = start; q <= stop && q <= end - 1; q++) {
1325 if (*q == '\n')
1326 cnt++;
1327 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001328 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001329}
1330
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001331static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001332{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001333 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001334
1335 for (q = text; li > 1; li--) {
1336 q = next_line(q);
1337 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001338 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001339}
1340
1341//----- Dot Movement Routines ----------------------------------
1342static void dot_left(void)
1343{
1344 if (dot > text && dot[-1] != '\n')
1345 dot--;
1346}
1347
1348static void dot_right(void)
1349{
1350 if (dot < end - 1 && *dot != '\n')
1351 dot++;
1352}
1353
1354static void dot_begin(void)
1355{
1356 dot = begin_line(dot); // return pointer to first char cur line
1357}
1358
1359static void dot_end(void)
1360{
1361 dot = end_line(dot); // return pointer to last char cur line
1362}
1363
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001364static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001365{
1366 int co;
1367
1368 p = begin_line(p);
1369 co = 0;
1370 do {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001371 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001372 break;
1373 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001374 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001375 } else if (*p < ' ' || *p == 127) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001376 co++; // display as ^X, use 2 columns
1377 }
1378 } while (++co <= l && p++ < end);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001379 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001380}
1381
1382static void dot_next(void)
1383{
1384 dot = next_line(dot);
1385}
1386
1387static void dot_prev(void)
1388{
1389 dot = prev_line(dot);
1390}
1391
1392static void dot_scroll(int cnt, int dir)
1393{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001394 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001395
1396 for (; cnt > 0; cnt--) {
1397 if (dir < 0) {
1398 // scroll Backwards
1399 // ctrl-Y scroll up one line
1400 screenbegin = prev_line(screenbegin);
1401 } else {
1402 // scroll Forwards
1403 // ctrl-E scroll down one line
1404 screenbegin = next_line(screenbegin);
1405 }
1406 }
1407 // make sure "dot" stays on the screen so we dont scroll off
1408 if (dot < screenbegin)
1409 dot = screenbegin;
1410 q = end_screen(); // find new bottom line
1411 if (dot > q)
1412 dot = begin_line(q); // is dot is below bottom line?
1413 dot_skip_over_ws();
1414}
1415
1416static void dot_skip_over_ws(void)
1417{
1418 // skip WS
1419 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1420 dot++;
1421}
1422
1423static void dot_delete(void) // delete the char at 'dot'
1424{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001425 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001426}
1427
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001428static char *bound_dot(char * p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001429{
1430 if (p >= end && end > text) {
1431 p = end - 1;
1432 indicate_error('1');
1433 }
1434 if (p < text) {
1435 p = text;
1436 indicate_error('2');
1437 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001438 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001439}
1440
1441//----- Helper Utility Routines --------------------------------
1442
1443//----------------------------------------------------------------
1444//----- Char Routines --------------------------------------------
1445/* Chars that are part of a word-
1446 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1447 * Chars that are Not part of a word (stoppers)
1448 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1449 * Chars that are WhiteSpace
1450 * TAB NEWLINE VT FF RETURN SPACE
1451 * DO NOT COUNT NEWLINE AS WHITESPACE
1452 */
1453
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001454static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001455{
1456 int li;
1457
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001458 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001459 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001460 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001461 // initialize the new screen. assume this will be a empty file.
1462 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001463 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001464 for (li = 1; li < ro - 1; li++) {
1465 screen[(li * co) + 0] = '~';
1466 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001467 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001468}
1469
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001470#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001471static int mycmp(const char * s1, const char * s2, int len)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001472{
1473 int i;
1474
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001475 i = strncmp(s1, s2, len);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001476 if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001477 i = strncasecmp(s1, s2, len);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001478 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001479 return i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001480}
1481
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001482// search for pattern starting at p
1483static char *char_search(char * p, const char * pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001484{
1485#ifndef REGEX_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001486 char *start, *stop;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001487 int len;
1488
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001489 len = strlen(pat);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001490 if (dir == FORWARD) {
1491 stop = end - 1; // assume range is p - end-1
1492 if (range == LIMITED)
1493 stop = next_line(p); // range is to next line
1494 for (start = p; start < stop; start++) {
1495 if (mycmp(start, pat, len) == 0) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001496 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001497 }
1498 }
1499 } else if (dir == BACK) {
1500 stop = text; // assume range is text - p
1501 if (range == LIMITED)
1502 stop = prev_line(p); // range is to prev line
1503 for (start = p - len; start >= stop; start--) {
1504 if (mycmp(start, pat, len) == 0) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001505 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001506 }
1507 }
1508 }
1509 // pattern not found
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001510 return NULL;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001511#else /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001512 char *q;
1513 struct re_pattern_buffer preg;
1514 int i;
1515 int size, range;
1516
1517 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1518 preg.translate = 0;
1519 preg.fastmap = 0;
1520 preg.buffer = 0;
1521 preg.allocated = 0;
1522
1523 // assume a LIMITED forward search
1524 q = next_line(p);
1525 q = end_line(q);
1526 q = end - 1;
1527 if (dir == BACK) {
1528 q = prev_line(p);
1529 q = text;
1530 }
1531 // count the number of chars to search over, forward or backward
1532 size = q - p;
1533 if (size < 0)
1534 size = p - q;
1535 // RANGE could be negative if we are searching backwards
1536 range = q - p;
1537
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001538 q = re_compile_pattern(pat, strlen(pat), &preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001539 if (q != 0) {
1540 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001541 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001542 i = 0; // return p if pattern not compiled
1543 goto cs1;
1544 }
1545
1546 q = p;
1547 if (range < 0) {
1548 q = p - size;
1549 if (q < text)
1550 q = text;
1551 }
1552 // search for the compiled pattern, preg, in p[]
1553 // range < 0- search backward
1554 // range > 0- search forward
1555 // 0 < start < size
1556 // re_search() < 0 not found or error
1557 // re_search() > 0 index of found pattern
1558 // struct pattern char int int int struct reg
1559 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1560 i = re_search(&preg, q, size, 0, range, 0);
1561 if (i == -1) {
1562 p = 0;
1563 i = 0; // return NULL if pattern not found
1564 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001565 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001566 if (dir == FORWARD) {
1567 p = p + i;
1568 } else {
1569 p = p - i;
1570 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001571 return p;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001572#endif /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001573}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001574#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001575
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001576static char *char_insert(char * p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001577{
1578 if (c == 22) { // Is this an ctrl-V?
1579 p = stupid_insert(p, '^'); // use ^ to indicate literal next
1580 p--; // backup onto ^
1581 refresh(FALSE); // show the ^
1582 c = get_one_char();
1583 *p = c;
1584 p++;
Paul Fox8552aec2005-09-16 12:20:05 +00001585 file_modified++; // has the file been modified
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001586 } else if (c == 27) { // Is this an ESC?
1587 cmd_mode = 0;
1588 cmdcnt = 0;
1589 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001590 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001591 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001592 p--;
1593 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001594 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001595 // 123456789
1596 if ((p[-1] != '\n') && (dot>text)) {
1597 p--;
1598 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001599 }
1600 } else {
1601 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001602 char *sp; // "save p"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001603
1604 if (c == 13)
1605 c = '\n'; // translate \r to \n
1606 sp = p; // remember addr of insert
1607 p = stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001608#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001609 if (showmatch && strchr(")]}", *sp) != NULL) {
1610 showmatching(sp);
1611 }
1612 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001613 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001614
1615 q = prev_line(p); // use prev line as templet
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001616 for (; isblank(*q); q++) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001617 p = stupid_insert(p, *q); // insert the char
1618 }
1619 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001620#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001621 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001622 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001623}
1624
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001625static char *stupid_insert(char * p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001626{
1627 p = text_hole_make(p, 1);
1628 if (p != 0) {
1629 *p = c;
Paul Fox8552aec2005-09-16 12:20:05 +00001630 file_modified++; // has the file been modified
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001631 p++;
1632 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001633 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001634}
1635
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001636static char find_range(char ** start, char ** stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001637{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001638 char *save_dot, *p, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001639 int cnt;
1640
1641 save_dot = dot;
1642 p = q = dot;
1643
1644 if (strchr("cdy><", c)) {
1645 // these cmds operate on whole lines
1646 p = q = begin_line(p);
1647 for (cnt = 1; cnt < cmdcnt; cnt++) {
1648 q = next_line(q);
1649 }
1650 q = end_line(q);
1651 } else if (strchr("^%$0bBeEft", c)) {
1652 // These cmds operate on char positions
1653 do_cmd(c); // execute movement cmd
1654 q = dot;
1655 } else if (strchr("wW", c)) {
1656 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001657 // if we are at the next word's first char
1658 // step back one char
1659 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001660 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001661 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1662 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1663 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001664 if (dot > text && *dot == '\n')
1665 dot--; // stay off NL
1666 q = dot;
1667 } else if (strchr("H-k{", c)) {
1668 // these operate on multi-lines backwards
1669 q = end_line(dot); // find NL
1670 do_cmd(c); // execute movement cmd
1671 dot_begin();
1672 p = dot;
1673 } else if (strchr("L+j}\r\n", c)) {
1674 // these operate on multi-lines forwards
1675 p = begin_line(dot);
1676 do_cmd(c); // execute movement cmd
1677 dot_end(); // find NL
1678 q = dot;
1679 } else {
1680 c = 27; // error- return an ESC char
1681 //break;
1682 }
1683 *start = p;
1684 *stop = q;
1685 if (q < p) {
1686 *start = q;
1687 *stop = p;
1688 }
1689 dot = save_dot;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001690 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001691}
1692
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001693static int st_test(char * p, int type, int dir, char * tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001694{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001695 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001696 int test, inc;
1697
1698 inc = dir;
1699 c = c0 = p[0];
1700 ci = p[inc];
1701 test = 0;
1702
1703 if (type == S_BEFORE_WS) {
1704 c = ci;
1705 test = ((!isspace(c)) || c == '\n');
1706 }
1707 if (type == S_TO_WS) {
1708 c = c0;
1709 test = ((!isspace(c)) || c == '\n');
1710 }
1711 if (type == S_OVER_WS) {
1712 c = c0;
1713 test = ((isspace(c)));
1714 }
1715 if (type == S_END_PUNCT) {
1716 c = ci;
1717 test = ((ispunct(c)));
1718 }
1719 if (type == S_END_ALNUM) {
1720 c = ci;
1721 test = ((isalnum(c)) || c == '_');
1722 }
1723 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001724 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001725}
1726
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001727static char *skip_thing(char * p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001728{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001729 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001730
1731 while (st_test(p, type, dir, &c)) {
1732 // make sure we limit search to correct number of lines
1733 if (c == '\n' && --linecnt < 1)
1734 break;
1735 if (dir >= 0 && p >= end - 1)
1736 break;
1737 if (dir < 0 && p <= text)
1738 break;
1739 p += dir; // move to next char
1740 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001741 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001742}
1743
1744// find matching char of pair () [] {}
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00001745static char *find_pair(char * p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001746{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001747 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001748 int dir, level;
1749
1750 match = ')';
1751 level = 1;
1752 dir = 1; // assume forward
1753 switch (c) {
1754 case '(':
1755 match = ')';
1756 break;
1757 case '[':
1758 match = ']';
1759 break;
1760 case '{':
1761 match = '}';
1762 break;
1763 case ')':
1764 match = '(';
1765 dir = -1;
1766 break;
1767 case ']':
1768 match = '[';
1769 dir = -1;
1770 break;
1771 case '}':
1772 match = '{';
1773 dir = -1;
1774 break;
1775 }
1776 for (q = p + dir; text <= q && q < end; q += dir) {
1777 // look for match, count levels of pairs (( ))
1778 if (*q == c)
1779 level++; // increase pair levels
1780 if (*q == match)
1781 level--; // reduce pair level
1782 if (level == 0)
1783 break; // found matching pair
1784 }
1785 if (level != 0)
1786 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001787 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001788}
1789
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001790#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001791// show the matching char of a pair, () [] {}
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001792static void showmatching(char * p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001793{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001794 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001795
1796 // we found half of a pair
1797 q = find_pair(p, *p); // get loc of matching char
1798 if (q == NULL) {
1799 indicate_error('3'); // no matching char
1800 } else {
1801 // "q" now points to matching pair
1802 save_dot = dot; // remember where we are
1803 dot = q; // go to new loc
1804 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001805 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001806 dot = save_dot; // go back to old loc
1807 refresh(FALSE);
1808 }
1809}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001810#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001811
1812// open a hole in text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001813static char *text_hole_make(char * p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001814{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001815 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001816 int cnt;
1817
1818 if (size <= 0)
1819 goto thm0;
1820 src = p;
1821 dest = p + size;
1822 cnt = end - src; // the rest of buffer
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001823 if ( ((end + size) >= (text + text_size)) // TODO: realloc here
1824 || memmove(dest, src, cnt) != dest) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001825 status_line_bold("can't create room for new characters");
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001826 p = NULL;
1827 goto thm0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001828 }
1829 memset(p, ' ', size); // clear new hole
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001830 end += size; // adjust the new END
Paul Fox8552aec2005-09-16 12:20:05 +00001831 file_modified++; // has the file been modified
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001832 thm0:
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001833 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001834}
1835
1836// close a hole in text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001837static char *text_hole_delete(char * p, char * q) // delete "p" thru "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001838{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001839 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001840 int cnt, hole_size;
1841
1842 // move forwards, from beginning
1843 // assume p <= q
1844 src = q + 1;
1845 dest = p;
1846 if (q < p) { // they are backward- swap them
1847 src = p + 1;
1848 dest = q;
1849 }
1850 hole_size = q - p + 1;
1851 cnt = end - src;
1852 if (src < text || src > end)
1853 goto thd0;
1854 if (dest < text || dest >= end)
1855 goto thd0;
1856 if (src >= end)
1857 goto thd_atend; // just delete the end of the buffer
1858 if (memmove(dest, src, cnt) != dest) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001859 status_line_bold("can't delete the character");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001860 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001861 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001862 end = end - hole_size; // adjust the new END
1863 if (dest >= end)
1864 dest = end - 1; // make sure dest in below end-1
1865 if (end <= text)
1866 dest = end = text; // keep pointers valid
Paul Fox8552aec2005-09-16 12:20:05 +00001867 file_modified++; // has the file been modified
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001868 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001869 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001870}
1871
1872// copy text into register, then delete text.
1873// if dist <= 0, do not include, or go past, a NewLine
1874//
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001875static char *yank_delete(char * start, char * stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001876{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001877 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001878
1879 // make sure start <= stop
1880 if (start > stop) {
1881 // they are backwards, reverse them
1882 p = start;
1883 start = stop;
1884 stop = p;
1885 }
1886 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001887 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001888 p = start;
1889 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001890 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001891 // dont go past a NewLine
1892 for (; p + 1 <= stop; p++) {
1893 if (p[1] == '\n') {
1894 stop = p; // "stop" just before NewLine
1895 break;
1896 }
1897 }
1898 }
1899 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001900#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001901 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001902#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001903 if (yf == YANKDEL) {
1904 p = text_hole_delete(start, stop);
1905 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001906 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001907}
1908
1909static void show_help(void)
1910{
1911 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001912#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001913 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001914#endif
1915#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001916 "\n\tLast command repeat with \'.\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001917#endif
1918#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00001919 "\n\tLine marking with 'x"
1920 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001921#endif
1922#if ENABLE_FEATURE_VI_READONLY
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001923 "\n\tReadonly if vi is called as \"view\""
1924 "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001925#endif
1926#if ENABLE_FEATURE_VI_SET
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001927 "\n\tSome colon mode commands with \':\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001928#endif
1929#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001930 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001931#endif
1932#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001933 "\n\tSignal catching- ^C"
1934 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001935#endif
1936#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001937 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001938#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001939 );
1940}
1941
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001942#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001943static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001944{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001945 // get buffer for new cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001946 if (!last_modifying_cmd)
1947 last_modifying_cmd = xzalloc(MAX_INPUT_LEN);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001948 // if there is a current cmd count put it in the buffer first
1949 if (cmdcnt > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001950 sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001951 else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00001952 last_modifying_cmd[0] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001953 last_modifying_cmd[1] = '\0';
1954 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001955 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001956}
1957
1958static void end_cmd_q(void)
1959{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001960#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001961 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001962#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001963 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001964}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001965#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001966
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001967#if ENABLE_FEATURE_VI_YANKMARK \
1968 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
1969 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001970static char *string_insert(char * p, char * s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001971{
1972 int cnt, i;
1973
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001974 i = strlen(s);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001975 if (text_hole_make(p, i)) {
1976 strncpy(p, s, i);
1977 for (cnt = 0; *s != '\0'; s++) {
1978 if (*s == '\n')
1979 cnt++;
1980 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001981#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001982 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001983#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001984 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001985 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001986}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001987#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001988
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001989#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001990static char *text_yank(char * p, char * q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001991{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001992 char *t;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001993 int cnt;
1994
1995 if (q < p) { // they are backwards- reverse them
1996 t = q;
1997 q = p;
1998 p = t;
1999 }
2000 cnt = q - p + 1;
2001 t = reg[dest];
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00002002 free(t); // if already a yank register, free it
Denis Vlasenkob95636c2006-12-19 23:36:04 +00002003 t = xmalloc(cnt + 1); // get a new register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002004 memset(t, '\0', cnt + 1); // clear new text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002005 strncpy(t, p, cnt); // copy text[] into bufer
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002006 reg[dest] = t;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002007 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002008}
2009
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002010static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002011{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002012 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002013
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002014 c = 'D'; // default to D-reg
2015 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002016 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002017 if (YDreg == 26)
2018 c = 'D';
2019 if (YDreg == 27)
2020 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002021 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002022}
2023
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002024static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002025{
2026 // A context is defined to be "modifying text"
2027 // Any modifying command establishes a new context.
2028
2029 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002030 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002031 // we are trying to modify text[]- make this the current context
2032 mark[27] = mark[26]; // move cur to prev
2033 mark[26] = dot; // move local to cur
2034 context_start = prev_line(prev_line(dot));
2035 context_end = next_line(next_line(dot));
2036 //loiter= start_loiter= now;
2037 }
2038 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002039}
2040
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002041static char *swap_context(char * p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002042{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002043 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002044
2045 // the current context is in mark[26]
2046 // the previous context is in mark[27]
2047 // only swap context if other context is valid
2048 if (text <= mark[27] && mark[27] <= end - 1) {
2049 tmp = mark[27];
2050 mark[27] = mark[26];
2051 mark[26] = tmp;
2052 p = mark[26]; // where we are going- previous context
2053 context_start = prev_line(prev_line(prev_line(p)));
2054 context_end = next_line(next_line(next_line(p)));
2055 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002056 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002057}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002058#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002059
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002060//----- Set terminal attributes --------------------------------
2061static void rawmode(void)
2062{
2063 tcgetattr(0, &term_orig);
2064 term_vi = term_orig;
2065 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
2066 term_vi.c_iflag &= (~IXON & ~ICRNL);
2067 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002068 term_vi.c_cc[VMIN] = 1;
2069 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002070 erase_char = term_vi.c_cc[VERASE];
2071 tcsetattr(0, TCSANOW, &term_vi);
2072}
2073
2074static void cookmode(void)
2075{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002076 fflush(stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002077 tcsetattr(0, TCSANOW, &term_orig);
2078}
2079
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002080//----- Come here when we get a window resize signal ---------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002081#if ENABLE_FEATURE_VI_USE_SIGNALS
"Vladimir N. Oleynik"cd473dd2006-01-30 13:41:53 +00002082static void winch_sig(int sig ATTRIBUTE_UNUSED)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002083{
2084 signal(SIGWINCH, winch_sig);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002085 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Denis Vlasenko621204b2006-10-27 09:03:24 +00002086 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002087 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
2088 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
2089 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002090 new_screen(rows, columns); // get memory for virtual screen
2091 redraw(TRUE); // re-draw the screen
2092}
2093
2094//----- Come here when we get a continue signal -------------------
"Vladimir N. Oleynik"cd473dd2006-01-30 13:41:53 +00002095static void cont_sig(int sig ATTRIBUTE_UNUSED)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002096{
2097 rawmode(); // terminal to "raw"
Paul Fox8552aec2005-09-16 12:20:05 +00002098 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002099 redraw(TRUE); // re-draw the screen
2100
2101 signal(SIGTSTP, suspend_sig);
2102 signal(SIGCONT, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002103 kill(my_pid, SIGCONT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002104}
2105
2106//----- Come here when we get a Suspend signal -------------------
"Vladimir N. Oleynik"cd473dd2006-01-30 13:41:53 +00002107static void suspend_sig(int sig ATTRIBUTE_UNUSED)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002108{
2109 place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
2110 clear_to_eol(); // Erase to end of line
2111 cookmode(); // terminal to "cooked"
2112
2113 signal(SIGCONT, cont_sig);
2114 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002115 kill(my_pid, SIGTSTP);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002116}
2117
2118//----- Come here when we get a signal ---------------------------
2119static void catch_sig(int sig)
2120{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002121 signal(SIGINT, catch_sig);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002122 if (sig)
"Vladimir N. Oleynik"cd473dd2006-01-30 13:41:53 +00002123 longjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002124}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002125#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002126
2127static int mysleep(int hund) // sleep for 'h' 1/100 seconds
2128{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002129 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002130
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002131 pfd[0].fd = 0;
2132 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002133 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002134}
2135
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002136static int chars_to_parse;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002137
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002138//----- IO Routines --------------------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002139static char readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002140{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002141 char c;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002142 int n;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002143 struct esc_cmds {
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002144 const char seq[4];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002145 char val;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002146 };
2147
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002148 static const struct esc_cmds esccmds[] = {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002149 {"OA" , VI_K_UP }, // cursor key Up
2150 {"OB" , VI_K_DOWN }, // cursor key Down
2151 {"OC" , VI_K_RIGHT }, // Cursor Key Right
2152 {"OD" , VI_K_LEFT }, // cursor key Left
2153 {"OH" , VI_K_HOME }, // Cursor Key Home
2154 {"OF" , VI_K_END }, // Cursor Key End
2155 {"[A" , VI_K_UP }, // cursor key Up
2156 {"[B" , VI_K_DOWN }, // cursor key Down
2157 {"[C" , VI_K_RIGHT }, // Cursor Key Right
2158 {"[D" , VI_K_LEFT }, // cursor key Left
2159 {"[H" , VI_K_HOME }, // Cursor Key Home
2160 {"[F" , VI_K_END }, // Cursor Key End
2161 {"[1~" , VI_K_HOME }, // Cursor Key Home
2162 {"[2~" , VI_K_INSERT }, // Cursor Key Insert
2163 {"[3~" , VI_K_DELETE }, // Cursor Key Delete
2164 {"[4~" , VI_K_END }, // Cursor Key End
2165 {"[5~" , VI_K_PAGEUP }, // Cursor Key Page Up
2166 {"[6~" , VI_K_PAGEDOWN}, // Cursor Key Page Down
2167 {"OP" , VI_K_FUN1 }, // Function Key F1
2168 {"OQ" , VI_K_FUN2 }, // Function Key F2
2169 {"OR" , VI_K_FUN3 }, // Function Key F3
2170 {"OS" , VI_K_FUN4 }, // Function Key F4
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002171 // careful: these have no terminating NUL!
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002172 {"[11~", VI_K_FUN1 }, // Function Key F1
2173 {"[12~", VI_K_FUN2 }, // Function Key F2
2174 {"[13~", VI_K_FUN3 }, // Function Key F3
2175 {"[14~", VI_K_FUN4 }, // Function Key F4
2176 {"[15~", VI_K_FUN5 }, // Function Key F5
2177 {"[17~", VI_K_FUN6 }, // Function Key F6
2178 {"[18~", VI_K_FUN7 }, // Function Key F7
2179 {"[19~", VI_K_FUN8 }, // Function Key F8
2180 {"[20~", VI_K_FUN9 }, // Function Key F9
2181 {"[21~", VI_K_FUN10 }, // Function Key F10
2182 {"[23~", VI_K_FUN11 }, // Function Key F11
2183 {"[24~", VI_K_FUN12 }, // Function Key F12
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002184 };
Denis Vlasenko80b8b392007-06-25 10:55:35 +00002185 enum { ESCCMDS_COUNT = ARRAY_SIZE(esccmds) };
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002186
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002187 alarm(0); // turn alarm OFF while we wait for input
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002188 fflush(stdout);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002189 n = chars_to_parse;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002190 // get input from User- are there already input chars in Q?
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002191 if (n <= 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002192 // the Q is empty, wait for a typed char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002193 n = safe_read(0, readbuffer, sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002194 if (n < 0) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002195 if (errno == EBADF || errno == EFAULT || errno == EINVAL
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002196 || errno == EIO)
2197 editing = 0; // want to exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002198 errno = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002199 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002200 if (n <= 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002201 return 0; // error
2202 if (readbuffer[0] == 27) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002203 // This is an ESC char. Is this Esc sequence?
2204 // Could be bare Esc key. See if there are any
2205 // more chars to read after the ESC. This would
2206 // be a Function or Cursor Key sequence.
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002207 struct pollfd pfd[1];
2208 pfd[0].fd = 0;
2209 pfd[0].events = POLLIN;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002210 // keep reading while there are input chars, and room in buffer
2211 // for a complete ESC sequence (assuming 8 chars is enough)
2212 while (safe_poll(pfd, 1, 0) > 0 && n <= (sizeof(readbuffer) - 8)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002213 // read the rest of the ESC string
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002214 int r = safe_read(0, readbuffer + n, sizeof(readbuffer) - n);
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002215 if (r > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002216 n += r;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002217 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002218 }
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002219 chars_to_parse = n;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002220 }
2221 c = readbuffer[0];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002222 if (c == 27 && n > 1) {
2223 // Maybe cursor or function key?
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002224 const struct esc_cmds *eindex;
2225
2226 for (eindex = esccmds; eindex < &esccmds[ESCCMDS_COUNT]; eindex++) {
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002227 int cnt = strnlen(eindex->seq, 4);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002228 if (n <= cnt)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002229 continue;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002230 if (strncmp(eindex->seq, readbuffer + 1, cnt) != 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002231 continue;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002232 c = eindex->val; // magic char value
2233 n = cnt + 1; // squeeze out the ESC sequence
2234 goto found;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002235 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002236 // defined ESC sequence not found
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002237 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002238 n = 1;
2239 found:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002240 // remove key sequence from Q
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002241 chars_to_parse -= n;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002242 memmove(readbuffer, readbuffer + n, sizeof(readbuffer) - n);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002243 alarm(3); // we are done waiting for input, turn alarm ON
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002244 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002245}
2246
2247//----- IO Routines --------------------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002248static char get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002249{
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002250 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002251
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002252#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002253 // ! adding2q && ioq == 0 read()
2254 // ! adding2q && ioq != 0 *ioq
2255 // adding2q *last_modifying_cmd= read()
2256 if (!adding2q) {
2257 // we are not adding to the q.
2258 // but, we may be reading from a q
2259 if (ioq == 0) {
2260 // there is no current q, read from STDIN
2261 c = readit(); // get the users input
2262 } else {
2263 // there is a queue to get chars from first
2264 c = *ioq++;
2265 if (c == '\0') {
2266 // the end of the q, read from STDIN
2267 free(ioq_start);
2268 ioq_start = ioq = 0;
2269 c = readit(); // get the users input
2270 }
2271 }
2272 } else {
2273 // adding STDIN chars to q
2274 c = readit(); // get the users input
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002275 if (last_modifying_cmd != NULL) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002276 int len = strlen(last_modifying_cmd);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002277 if (len >= MAX_INPUT_LEN - 1) {
2278 status_line_bold("last_modifying_cmd overrun");
Eric Andersenfda2b7f2002-10-26 10:19:19 +00002279 } else {
2280 // add new char to q
2281 last_modifying_cmd[len] = c;
2282 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002283 }
2284 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002285#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002286 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002287#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002288 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002289}
2290
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002291// Get input line (uses "status line" area)
2292static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002293{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002294 static char *buf; // [MAX_INPUT_LEN]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002295
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002296 char c;
2297 int i;
2298
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002299 if (!buf) buf = xmalloc(MAX_INPUT_LEN);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002300
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002301 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002302 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002303 place_cursor(rows - 1, 0, FALSE); // go to Status line, bottom of screen
2304 clear_to_eol(); // clear the line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002305 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002306
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002307 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002308 while (i < MAX_INPUT_LEN) {
2309 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002310 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002311 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002312 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002313 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002314 buf[--i] = '\0';
2315 write1("\b \b"); // erase char on screen
2316 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002317 break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002318 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002319 buf[i] = c;
2320 buf[++i] = '\0';
2321 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002322 }
2323 }
2324 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002325 return buf;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002326}
2327
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002328static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002329{
2330 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002331 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002332
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002333 cnt = -1;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002334 if (fn && fn[0] && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002335 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002336 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002337}
2338
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002339static int file_insert(const char * fn, char *p
2340 USE_FEATURE_VI_READONLY(, int update_ro_status))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002341{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002342 int cnt = -1;
2343 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002344 struct stat statbuf;
2345
2346 /* Validate file */
2347 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002348 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002349 goto fi0;
2350 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002351 if ((statbuf.st_mode & S_IFREG) == 0) {
2352 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002353 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002354 goto fi0;
2355 }
2356 /* // this check is done by open()
2357 if ((statbuf.st_mode & (S_IRUSR | S_IRGRP | S_IROTH)) == 0) {
2358 // dont have any read permissions
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002359 status_line_bold("\"%s\" Not readable", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002360 goto fi0;
2361 }
2362 */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002363 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002364 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002365 goto fi0;
2366 }
2367
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002368 // read file to buffer
2369 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002370 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002371 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002372 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002373 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002374 size = statbuf.st_size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002375 p = text_hole_make(p, size);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002376 if (p == NULL)
2377 goto fi0;
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002378 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002379 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002380 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002381 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002382 } else if (cnt < size) {
2383 // There was a partial read, shrink unused space text[]
2384 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002385 status_line_bold("cannot read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002386 }
2387 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002388 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002389 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002390 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002391#if ENABLE_FEATURE_VI_READONLY
2392 if (update_ro_status
2393 && ((access(fn, W_OK) < 0) ||
2394 /* root will always have access()
2395 * so we check fileperms too */
2396 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2397 )
2398 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002399 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002400 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002401#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002402 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002403}
2404
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002405
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002406static int file_write(char * fn, char * first, char * last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002407{
2408 int fd, cnt, charcnt;
2409
2410 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002411 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002412 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002413 }
2414 charcnt = 0;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002415 fd = open(fn, (O_WRONLY | O_CREAT | O_TRUNC), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002416 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002417 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002418 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002419 charcnt = full_write(fd, first, cnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002420 if (charcnt == cnt) {
2421 // good write
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002422 //file_modified = FALSE; // the file has not been modified
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002423 } else {
2424 charcnt = 0;
2425 }
2426 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002427 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002428}
2429
2430//----- Terminal Drawing ---------------------------------------
2431// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002432// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002433// screen coordinates
2434// 0,0 ... 0,79
2435// 1,0 ... 1,79
2436// . ... .
2437// . ... .
2438// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002439// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002440
2441//----- Move the cursor to row x col (count from 0, not 1) -------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002442static void place_cursor(int row, int col, int optimize)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002443{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002444 char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002445 char *cm;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002446
2447 if (row < 0) row = 0;
2448 if (row >= rows) row = rows - 1;
2449 if (col < 0) col = 0;
2450 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002451
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002452 //----- 1. Try the standard terminal ESC sequence
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002453 sprintf(cm1, CMrc, row + 1, col + 1);
2454 cm = cm1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002455
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002456#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002457 if (optimize && col < 16) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002458 enum {
2459 SZ_UP = sizeof(CMup),
2460 SZ_DN = sizeof(CMdown),
2461 SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN,
2462 };
2463 char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002464 char *screenp;
2465 int Rrow = last_row;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002466 int diff = Rrow - row;
2467
2468 if (diff < -5 || diff > 5)
2469 goto skip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002470
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002471 //----- find the minimum # of chars to move cursor -------------
2472 //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
2473 cm2[0] = '\0';
2474
2475 // move to the correct row
2476 while (row < Rrow) {
2477 // the cursor has to move up
2478 strcat(cm2, CMup);
2479 Rrow--;
2480 }
2481 while (row > Rrow) {
2482 // the cursor has to move down
2483 strcat(cm2, CMdown);
2484 Rrow++;
2485 }
2486
2487 // now move to the correct column
2488 strcat(cm2, "\r"); // start at col 0
2489 // just send out orignal source char to get to correct place
2490 screenp = &screen[row * columns]; // start of screen line
2491 strncat(cm2, screenp, col);
2492
2493 // pick the shortest cursor motion to send out
2494 if (strlen(cm2) < strlen(cm)) {
2495 cm = cm2;
2496 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002497 skip: ;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002498 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002499#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002500 write1(cm);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002501 last_row = row;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002502}
2503
2504//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002505static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002506{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002507 write1(Ceol); // Erase from cursor to end of line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002508}
2509
2510//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002511static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002512{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002513 write1(Ceos); // Erase from cursor to end of screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002514}
2515
2516//----- Start standout mode ------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002517static void standout_start(void) // send "start reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002518{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002519 write1(SOs); // Start reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002520}
2521
2522//----- End standout mode --------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002523static void standout_end(void) // send "end reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002524{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002525 write1(SOn); // End reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002526}
2527
2528//----- Flash the screen --------------------------------------
2529static void flash(int h)
2530{
2531 standout_start(); // send "start reverse video" sequence
2532 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002533 mysleep(h);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002534 standout_end(); // send "end reverse video" sequence
2535 redraw(TRUE);
2536}
2537
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002538static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002539{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002540#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002541 if (crashme > 0)
2542 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002543#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002544 if (!err_method) {
2545 write1(bell); // send out a bell character
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002546 } else {
2547 flash(10);
2548 }
2549}
2550
2551//----- Screen[] Routines --------------------------------------
2552//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002553static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002554{
2555 memset(screen, ' ', screensize); // clear new screen
2556}
2557
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002558static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002559{
2560 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002561 char *e = buf + count;
2562
Paul Fox8552aec2005-09-16 12:20:05 +00002563 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002564 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002565 return sum;
2566}
2567
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002568//----- Draw the status line at bottom of the screen -------------
2569static void show_status_line(void)
2570{
Paul Foxc3504852005-09-16 12:48:18 +00002571 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002572
Paul Fox8552aec2005-09-16 12:20:05 +00002573 // either we already have an error or status message, or we
2574 // create one.
2575 if (!have_status_msg) {
2576 cnt = format_edit_status();
2577 cksum = bufsum(status_buffer, cnt);
2578 }
2579 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002580 last_status_cksum = cksum; // remember if we have seen this line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002581 place_cursor(rows - 1, 0, FALSE); // put cursor on status line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002582 write1(status_buffer);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002583 clear_to_eol();
Paul Fox8552aec2005-09-16 12:20:05 +00002584 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002585 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002586 (columns - 1) ) {
2587 have_status_msg = 0;
2588 Hit_Return();
2589 }
2590 have_status_msg = 0;
2591 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002592 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2593 }
Eric Andersena9eb33d2004-08-19 19:15:06 +00002594 fflush(stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002595}
2596
2597//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002598// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002599static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002600{
2601 va_list args;
2602
2603 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002604 strcpy(status_buffer, SOs); // Terminal standout mode on
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002605 vsprintf(status_buffer + sizeof(SOs)-1, format, args);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002606 strcat(status_buffer, SOn); // Terminal standout mode off
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002607 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002608
2609 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002610}
2611
Paul Fox8552aec2005-09-16 12:20:05 +00002612// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002613static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002614{
2615 va_list args;
2616
2617 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002618 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002619 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002620
2621 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002622}
2623
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002624// copy s to buf, convert unprintable
2625static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002626{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002627 unsigned char c;
2628 char b[2];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002629
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002630 b[1] = '\0';
2631 buf[0] = '\0';
2632 if (!s[0])
2633 s = "(NULL)";
2634 for (; *s; s++) {
2635 int c_is_no_print;
2636
2637 c = *s;
2638 c_is_no_print = (c & 0x80) && !Isprint(c);
2639 if (c_is_no_print) {
2640 strcat(buf, SOn);
2641 c = '.';
2642 }
2643 if (c < ' ' || c == 127) {
2644 strcat(buf, "^");
2645 if (c == 127)
2646 c = '?';
2647 else
2648 c += '@';
2649 }
2650 b[0] = c;
2651 strcat(buf, b);
2652 if (c_is_no_print)
2653 strcat(buf, SOs);
2654 if (*s == '\n')
2655 strcat(buf, "$");
2656 if (strlen(buf) > MAX_INPUT_LEN - 10) // paranoia
2657 break;
2658 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002659}
2660
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002661static void not_implemented(const char *s)
2662{
2663 char buf[MAX_INPUT_LEN];
2664
2665 print_literal(buf, s);
2666 status_line_bold("\'%s\' is not implemented", buf);
2667}
2668
2669// show file status on status line
2670static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002671{
Paul Fox8552aec2005-09-16 12:20:05 +00002672 static int tot;
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002673 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002674 int cur, percent, ret, trunc_at;
2675
Paul Fox8552aec2005-09-16 12:20:05 +00002676 // file_modified is now a counter rather than a flag. this
2677 // helps reduce the amount of line counting we need to do.
2678 // (this will cause a mis-reporting of modified status
2679 // once every MAXINT editing operations.)
2680
2681 // it would be nice to do a similar optimization here -- if
2682 // we haven't done a motion that could have changed which line
2683 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002684 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002685
2686 // reduce counting -- the total lines can't have
2687 // changed if we haven't done any edits.
2688 if (file_modified != last_file_modified) {
2689 tot = cur + count_lines(dot, end - 1) - 1;
2690 last_file_modified = file_modified;
2691 }
2692
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002693 // current line percent
2694 // ------------- ~~ ----------
2695 // total lines 100
2696 if (tot > 0) {
2697 percent = (100 * cur) / tot;
2698 } else {
2699 cur = tot = 0;
2700 percent = 100;
2701 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002702
Paul Fox8552aec2005-09-16 12:20:05 +00002703 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2704 columns : STATUS_BUFFER_LEN-1;
2705
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002706 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002707#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002708 "%c %s%s%s %d/%d %d%%",
2709#else
2710 "%c %s%s %d/%d %d%%",
2711#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002712 cmd_mode_indicator[cmd_mode & 3],
2713 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002714#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002715 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002716#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002717 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002718 cur, tot, percent);
2719
2720 if (ret >= 0 && ret < trunc_at)
2721 return ret; /* it all fit */
2722
2723 return trunc_at; /* had to truncate */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002724}
2725
2726//----- Force refresh of all Lines -----------------------------
2727static void redraw(int full_screen)
2728{
2729 place_cursor(0, 0, FALSE); // put cursor in correct place
2730 clear_to_eos(); // tel terminal to erase display
2731 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002732 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002733 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002734 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002735}
2736
2737//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002738// Returns number of leading chars which should be ignored
2739// (return value is always <= offset)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002740static char* format_line(char *src, int li)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002741{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002742 char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002743 int co;
2744 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002745 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002746
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002747 memset(dest, ' ', MAX_SCR_COLS + MAX_TABSTOP * 2);
2748
2749 c = '~'; // char in col 0 in non-existent lines is '~'
2750 for (co = 0; co < MAX_SCR_COLS + MAX_TABSTOP; co++) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002751 // are there chars in text[] and have we gone past the end
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002752 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002753 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002754 if (c == '\n')
2755 break;
2756 if ((c & 0x80) && !Isprint(c)) {
2757 c = '.';
2758 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002759 if ((unsigned char)c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002760 if (c == '\t') {
2761 c = ' ';
2762 // co % 8 != 7
2763 while ((co % tabstop) != (tabstop - 1)) {
2764 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002765 }
2766 } else {
2767 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002768 if (c == 0x7f)
2769 c = '?';
2770 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002771 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002772 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002773 }
2774 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002775 dest[co] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002776 // discard scrolled-off-to-the-left portion,
2777 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002778 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002779 memmove(dest, dest + tabstop, co + 1);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002780 co -= tabstop;
2781 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002782 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002783 if (src >= end)
2784 break;
2785 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002786 // check "short line, gigantic offset" case
2787 if (co < ofs)
2788 ofs = co + 1;
2789 dest[ofs + MAX_SCR_COLS] = '\0';
2790 return &dest[ofs];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002791}
2792
2793//----- Refresh the changed screen lines -----------------------
2794// Copy the source line from text[] into the buffer and note
2795// if the current screenline is different from the new buffer.
2796// If they differ then that line needs redrawing on the terminal.
2797//
2798static void refresh(int full_screen)
2799{
2800 static int old_offset;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002801
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002802 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002803 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002804
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002805 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
2806 int c = columns, r = rows;
Rob Landleye5e1a102006-06-21 01:15:36 +00002807 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002808 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
2809 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
2810 full_screen |= (c - columns) | (r - rows);
2811 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002812 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2813 tp = screenbegin; // index into text[] of top line
2814
2815 // compare text[] to screen[] and mark screen[] lines that need updating
2816 for (li = 0; li < rows - 1; li++) {
2817 int cs, ce; // column start & end
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002818 // format current text line
2819 char *out_buf = format_line(tp, li);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002820
2821 // skip to the end of the current text[] line
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002822 while (tp < end && *tp++ != '\n')
2823 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002824
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002825 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002826 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002827 cs = 0;
2828 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002829 sp = &screen[li * columns]; // start of screen line
2830 if (full_screen) {
2831 // force re-draw of every single column from 0 - columns-1
2832 goto re0;
2833 }
2834 // compare newly formatted buffer with virtual screen
2835 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002836 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002837 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002838 changed = TRUE; // mark for redraw
2839 break;
2840 }
2841 }
2842
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002843 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002844 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002845 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002846 changed = TRUE; // mark for redraw
2847 break;
2848 }
2849 }
2850 // now, cs is index of first diff, and ce is index of last diff
2851
2852 // if horz offset has changed, force a redraw
2853 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002854 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002855 changed = TRUE;
2856 }
2857
2858 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002859 if (cs < 0) cs = 0;
2860 if (ce > columns - 1) ce = columns - 1;
2861 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002862 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002863 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002864 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002865 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002866
2867 // move cursor to column of first change
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002868 //if (offset != old_offset) {
2869 // // place_cursor is still too stupid
2870 // // to handle offsets correctly
2871 // place_cursor(li, cs, FALSE);
2872 //} else {
2873 place_cursor(li, cs, TRUE);
2874 //}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002875
2876 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002877 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002878 }
2879 }
2880
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002881 place_cursor(crow, ccol, TRUE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002882
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002883 old_offset = offset;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002884}
2885
Eric Andersen3f980402001-04-04 17:31:15 +00002886//---------------------------------------------------------------------
2887//----- the Ascii Chart -----------------------------------------------
2888//
2889// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
2890// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
2891// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
2892// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
2893// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
2894// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
2895// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
2896// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
2897// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
2898// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
2899// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
2900// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
2901// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
2902// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
2903// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
2904// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
2905//---------------------------------------------------------------------
2906
2907//----- Execute a Vi Command -----------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002908static void do_cmd(char c)
Eric Andersen3f980402001-04-04 17:31:15 +00002909{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002910 const char *msg;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002911 char c1, *p, *q, *save_dot;
2912 char buf[12];
Eric Andersen3f980402001-04-04 17:31:15 +00002913 int cnt, i, j, dir, yf;
2914
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002915// c1 = c; // quiet the compiler
2916// cnt = yf = dir = 0; // quiet the compiler
2917// msg = p = q = save_dot = buf; // quiet the compiler
2918 memset(buf, '\0', 12);
Eric Andersenbff7a602001-11-17 07:15:43 +00002919
Paul Fox8552aec2005-09-16 12:20:05 +00002920 show_status_line();
2921
Eric Andersenbff7a602001-11-17 07:15:43 +00002922 /* if this is a cursor key, skip these checks */
2923 switch (c) {
2924 case VI_K_UP:
2925 case VI_K_DOWN:
2926 case VI_K_LEFT:
2927 case VI_K_RIGHT:
2928 case VI_K_HOME:
2929 case VI_K_END:
2930 case VI_K_PAGEUP:
2931 case VI_K_PAGEDOWN:
2932 goto key_cmd_mode;
2933 }
2934
Eric Andersen3f980402001-04-04 17:31:15 +00002935 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002936 // flip-flop Insert/Replace mode
Denis Vlasenko2a51af22007-03-21 22:31:24 +00002937 if (c == VI_K_INSERT)
2938 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00002939 // we are 'R'eplacing the current *dot with new char
2940 if (*dot == '\n') {
2941 // don't Replace past E-o-l
2942 cmd_mode = 1; // convert to insert
2943 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002944 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00002945 if (c != 27)
2946 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
2947 dot = char_insert(dot, c); // insert new char
2948 }
2949 goto dc1;
2950 }
2951 }
2952 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00002953 // hitting "Insert" twice means "R" replace mode
2954 if (c == VI_K_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00002955 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002956 if (1 <= c || Isprint(c)) {
2957 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00002958 }
2959 goto dc1;
2960 }
2961
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002962 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00002963 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00002964 //case 0x01: // soh
2965 //case 0x09: // ht
2966 //case 0x0b: // vt
2967 //case 0x0e: // so
2968 //case 0x0f: // si
2969 //case 0x10: // dle
2970 //case 0x11: // dc1
2971 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002972#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00002973 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00002974 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00002975 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002976#endif
Eric Andersen822c3832001-05-07 17:37:43 +00002977 //case 0x16: // syn
2978 //case 0x17: // etb
2979 //case 0x18: // can
2980 //case 0x1c: // fs
2981 //case 0x1d: // gs
2982 //case 0x1e: // rs
2983 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002984 //case '!': // !-
2985 //case '#': // #-
2986 //case '&': // &-
2987 //case '(': // (-
2988 //case ')': // )-
2989 //case '*': // *-
2990 //case ',': // ,-
2991 //case '=': // =-
2992 //case '@': // @-
2993 //case 'F': // F-
2994 //case 'K': // K-
2995 //case 'Q': // Q-
2996 //case 'S': // S-
2997 //case 'T': // T-
2998 //case 'V': // V-
2999 //case '[': // [-
3000 //case '\\': // \-
3001 //case ']': // ]-
3002 //case '_': // _-
3003 //case '`': // `-
3004 //case 'g': // g-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003005 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003006 //case 'v': // v-
Eric Andersen3f980402001-04-04 17:31:15 +00003007 default: // unrecognised command
3008 buf[0] = c;
3009 buf[1] = '\0';
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003010 if (c < ' ') {
Eric Andersen3f980402001-04-04 17:31:15 +00003011 buf[0] = '^';
3012 buf[1] = c + '@';
3013 buf[2] = '\0';
3014 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003015 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003016 end_cmd_q(); // stop adding to q
3017 case 0x00: // nul- ignore
3018 break;
3019 case 2: // ctrl-B scroll up full screen
3020 case VI_K_PAGEUP: // Cursor Key Page Up
3021 dot_scroll(rows - 2, -1);
3022 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003023#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +00003024 case 0x03: // ctrl-C interrupt
3025 longjmp(restart, 1);
3026 break;
3027 case 26: // ctrl-Z suspend
3028 suspend_sig(SIGTSTP);
3029 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003030#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003031 case 4: // ctrl-D scroll down half screen
3032 dot_scroll((rows - 2) / 2, 1);
3033 break;
3034 case 5: // ctrl-E scroll down one line
3035 dot_scroll(1, 1);
3036 break;
3037 case 6: // ctrl-F scroll down full screen
3038 case VI_K_PAGEDOWN: // Cursor Key Page Down
3039 dot_scroll(rows - 2, 1);
3040 break;
3041 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00003042 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003043 break;
3044 case 'h': // h- move left
3045 case VI_K_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003046 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003047 case 0x7f: // DEL- move left (This may be ERASE char)
Eric Andersen3f980402001-04-04 17:31:15 +00003048 if (cmdcnt-- > 1) {
3049 do_cmd(c);
3050 } // repeat cnt
3051 dot_left();
3052 break;
3053 case 10: // Newline ^J
3054 case 'j': // j- goto next line, same col
3055 case VI_K_DOWN: // cursor key Down
3056 if (cmdcnt-- > 1) {
3057 do_cmd(c);
3058 } // repeat cnt
3059 dot_next(); // go to next B-o-l
3060 dot = move_to_col(dot, ccol + offset); // try stay in same col
3061 break;
3062 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003063 case 18: // ctrl-R force redraw
Eric Andersen822c3832001-05-07 17:37:43 +00003064 place_cursor(0, 0, FALSE); // put cursor in correct place
Eric Andersen3f980402001-04-04 17:31:15 +00003065 clear_to_eos(); // tel terminal to erase display
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003066 mysleep(10);
Eric Andersen3f980402001-04-04 17:31:15 +00003067 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003068 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003069 refresh(TRUE); // this will redraw the entire display
3070 break;
3071 case 13: // Carriage Return ^M
3072 case '+': // +- goto next line
3073 if (cmdcnt-- > 1) {
3074 do_cmd(c);
3075 } // repeat cnt
3076 dot_next();
3077 dot_skip_over_ws();
3078 break;
3079 case 21: // ctrl-U scroll up half screen
3080 dot_scroll((rows - 2) / 2, -1);
3081 break;
3082 case 25: // ctrl-Y scroll up one line
3083 dot_scroll(1, -1);
3084 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003085 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003086 if (cmd_mode == 0)
3087 indicate_error(c);
3088 cmd_mode = 0; // stop insrting
3089 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003090 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003091 break;
3092 case ' ': // move right
3093 case 'l': // move right
3094 case VI_K_RIGHT: // Cursor Key Right
3095 if (cmdcnt-- > 1) {
3096 do_cmd(c);
3097 } // repeat cnt
3098 dot_right();
3099 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003100#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003101 case '"': // "- name a register to use for Delete/Yank
3102 c1 = get_one_char();
3103 c1 = tolower(c1);
3104 if (islower(c1)) {
3105 YDreg = c1 - 'a';
3106 } else {
3107 indicate_error(c);
3108 }
3109 break;
3110 case '\'': // '- goto a specific mark
3111 c1 = get_one_char();
3112 c1 = tolower(c1);
3113 if (islower(c1)) {
3114 c1 = c1 - 'a';
3115 // get the b-o-l
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003116 q = mark[(unsigned char) c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003117 if (text <= q && q < end) {
3118 dot = q;
3119 dot_begin(); // go to B-o-l
3120 dot_skip_over_ws();
3121 }
3122 } else if (c1 == '\'') { // goto previous context
3123 dot = swap_context(dot); // swap current and previous context
3124 dot_begin(); // go to B-o-l
3125 dot_skip_over_ws();
3126 } else {
3127 indicate_error(c);
3128 }
3129 break;
3130 case 'm': // m- Mark a line
3131 // this is really stupid. If there are any inserts or deletes
3132 // between text[0] and dot then this mark will not point to the
3133 // correct location! It could be off by many lines!
3134 // Well..., at least its quick and dirty.
3135 c1 = get_one_char();
3136 c1 = tolower(c1);
3137 if (islower(c1)) {
3138 c1 = c1 - 'a';
3139 // remember the line
3140 mark[(int) c1] = dot;
3141 } else {
3142 indicate_error(c);
3143 }
3144 break;
3145 case 'P': // P- Put register before
3146 case 'p': // p- put register after
3147 p = reg[YDreg];
3148 if (p == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003149 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003150 break;
3151 }
3152 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003153 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003154 if (c == 'P') {
3155 dot_begin(); // putting lines- Put above
3156 }
3157 if (c == 'p') {
3158 // are we putting after very last line?
3159 if (end_line(dot) == (end - 1)) {
3160 dot = end; // force dot to end of text[]
3161 } else {
3162 dot_next(); // next line, then put before
3163 }
3164 }
3165 } else {
3166 if (c == 'p')
3167 dot_right(); // move to right, can move to NL
3168 }
3169 dot = string_insert(dot, p); // insert the string
3170 end_cmd_q(); // stop adding to q
3171 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003172 case 'U': // U- Undo; replace current line with original version
3173 if (reg[Ureg] != 0) {
3174 p = begin_line(dot);
3175 q = end_line(dot);
3176 p = text_hole_delete(p, q); // delete cur line
3177 p = string_insert(p, reg[Ureg]); // insert orig line
3178 dot = p;
3179 dot_skip_over_ws();
3180 }
3181 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003182#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003183 case '$': // $- goto end of line
3184 case VI_K_END: // Cursor Key End
3185 if (cmdcnt-- > 1) {
3186 do_cmd(c);
3187 } // repeat cnt
Glenn L McGrathee829062004-01-21 10:59:45 +00003188 dot = end_line(dot);
Eric Andersen3f980402001-04-04 17:31:15 +00003189 break;
3190 case '%': // %- find matching char of pair () [] {}
3191 for (q = dot; q < end && *q != '\n'; q++) {
3192 if (strchr("()[]{}", *q) != NULL) {
3193 // we found half of a pair
3194 p = find_pair(q, *q);
3195 if (p == NULL) {
3196 indicate_error(c);
3197 } else {
3198 dot = p;
3199 }
3200 break;
3201 }
3202 }
3203 if (*q == '\n')
3204 indicate_error(c);
3205 break;
3206 case 'f': // f- forward to a user specified char
3207 last_forward_char = get_one_char(); // get the search char
3208 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003209 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003210 //
Paul Foxd13b90b2005-07-18 22:17:25 +00003211 //**** fall thru to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003212 case ';': // ;- look at rest of line for last forward char
3213 if (cmdcnt-- > 1) {
Eric Andersen822c3832001-05-07 17:37:43 +00003214 do_cmd(';');
Eric Andersen3f980402001-04-04 17:31:15 +00003215 } // repeat cnt
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003216 if (last_forward_char == 0)
3217 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003218 q = dot + 1;
3219 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3220 q++;
3221 }
3222 if (*q == last_forward_char)
3223 dot = q;
3224 break;
3225 case '-': // -- goto prev line
3226 if (cmdcnt-- > 1) {
3227 do_cmd(c);
3228 } // repeat cnt
3229 dot_prev();
3230 dot_skip_over_ws();
3231 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003232#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003233 case '.': // .- repeat the last modifying command
3234 // Stuff the last_modifying_cmd back into stdin
3235 // and let it be re-executed.
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003236 if (last_modifying_cmd != NULL) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003237 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003238 }
3239 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003240#endif
3241#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003242 case '?': // /- search for a pattern
3243 case '/': // /- search for a pattern
3244 buf[0] = c;
3245 buf[1] = '\0';
3246 q = get_input_line(buf); // get input line- use "status line"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003247 if (q[0] && !q[1])
3248 goto dc3; // if no pat re-use old pat
3249 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003250 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003251 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003252 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003253 goto dc3; // now find the pattern
3254 }
3255 // user changed mind and erased the "/"- do nothing
3256 break;
3257 case 'N': // N- backward search for last pattern
3258 if (cmdcnt-- > 1) {
3259 do_cmd(c);
3260 } // repeat cnt
3261 dir = BACK; // assume BACKWARD search
3262 p = dot - 1;
3263 if (last_search_pattern[0] == '?') {
3264 dir = FORWARD;
3265 p = dot + 1;
3266 }
3267 goto dc4; // now search for pattern
3268 break;
3269 case 'n': // n- repeat search for last pattern
3270 // search rest of text[] starting at next char
3271 // if search fails return orignal "p" not the "p+1" address
3272 if (cmdcnt-- > 1) {
3273 do_cmd(c);
3274 } // repeat cnt
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003275 dc3:
Eric Andersen3f980402001-04-04 17:31:15 +00003276 if (last_search_pattern == 0) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003277 msg = "No previous regular expression";
Eric Andersen3f980402001-04-04 17:31:15 +00003278 goto dc2;
3279 }
3280 if (last_search_pattern[0] == '/') {
3281 dir = FORWARD; // assume FORWARD search
3282 p = dot + 1;
3283 }
3284 if (last_search_pattern[0] == '?') {
3285 dir = BACK;
3286 p = dot - 1;
3287 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003288 dc4:
Eric Andersen3f980402001-04-04 17:31:15 +00003289 q = char_search(p, last_search_pattern + 1, dir, FULL);
3290 if (q != NULL) {
3291 dot = q; // good search, update "dot"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003292 msg = "";
Eric Andersen3f980402001-04-04 17:31:15 +00003293 goto dc2;
3294 }
3295 // no pattern found between "dot" and "end"- continue at top
3296 p = text;
3297 if (dir == BACK) {
3298 p = end - 1;
3299 }
3300 q = char_search(p, last_search_pattern + 1, dir, FULL);
3301 if (q != NULL) { // found something
3302 dot = q; // found new pattern- goto it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003303 msg = "search hit BOTTOM, continuing at TOP";
Eric Andersen3f980402001-04-04 17:31:15 +00003304 if (dir == BACK) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003305 msg = "search hit TOP, continuing at BOTTOM";
Eric Andersen3f980402001-04-04 17:31:15 +00003306 }
3307 } else {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003308 msg = "Pattern not found";
Eric Andersen3f980402001-04-04 17:31:15 +00003309 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003310 dc2:
3311 if (*msg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003312 status_line_bold("%s", msg);
Eric Andersen3f980402001-04-04 17:31:15 +00003313 break;
3314 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003315 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003316 if (q != NULL) { // found blank line
3317 dot = next_line(q); // move to next blank line
3318 }
3319 break;
3320 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003321 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003322 if (q != NULL) { // found blank line
3323 dot = next_line(q); // move to next blank line
3324 }
3325 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003326#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003327 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003328 case '1': // 1-
3329 case '2': // 2-
3330 case '3': // 3-
3331 case '4': // 4-
3332 case '5': // 5-
3333 case '6': // 6-
3334 case '7': // 7-
3335 case '8': // 8-
3336 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003337 if (c == '0' && cmdcnt < 1) {
3338 dot_begin(); // this was a standalone zero
3339 } else {
3340 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3341 }
3342 break;
3343 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003344 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003345#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003346 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003347#else
Eric Andersen822c3832001-05-07 17:37:43 +00003348 if (*p == ':')
3349 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003350 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003351 if (cnt <= 0)
3352 break;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003353 if (strncasecmp(p, "quit", cnt) == 0
3354 || strncasecmp(p, "q!", cnt) == 0 // delete lines
3355 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003356 if (file_modified && p[1] != '!') {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003357 status_line_bold("No write since last change (:quit! overrides)");
Eric Andersen3f980402001-04-04 17:31:15 +00003358 } else {
3359 editing = 0;
3360 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003361 } else if (strncasecmp(p, "write", cnt) == 0
3362 || strncasecmp(p, "wq", cnt) == 0
3363 || strncasecmp(p, "wn", cnt) == 0
3364 || strncasecmp(p, "x", cnt) == 0
3365 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003366 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003367 if (cnt < 0) {
3368 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003369 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003370 } else {
3371 file_modified = 0;
3372 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003373 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003374 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3375 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3376 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003377 editing = 0;
3378 }
Eric Andersen3f980402001-04-04 17:31:15 +00003379 }
Denis Vlasenko219d14d2007-03-24 15:40:16 +00003380 } else if (strncasecmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003381 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003382 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003383 dot = find_line(j); // go to line # j
3384 dot_skip_over_ws();
Eric Andersen3f980402001-04-04 17:31:15 +00003385 } else { // unrecognised cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003386 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003387 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003388#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003389 break;
3390 case '<': // <- Left shift something
3391 case '>': // >- Right shift something
3392 cnt = count_lines(text, dot); // remember what line we are on
3393 c1 = get_one_char(); // get the type of thing to delete
3394 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003395 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003396 p = begin_line(p);
3397 q = end_line(q);
3398 i = count_lines(p, q); // # of lines we are shifting
3399 for ( ; i > 0; i--, p = next_line(p)) {
3400 if (c == '<') {
3401 // shift left- remove tab or 8 spaces
3402 if (*p == '\t') {
3403 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003404 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003405 } else if (*p == ' ') {
3406 // we should be calculating columns, not just SPACE
3407 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003408 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003409 }
3410 }
3411 } else if (c == '>') {
3412 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003413 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003414 }
3415 }
3416 dot = find_line(cnt); // what line were we on
3417 dot_skip_over_ws();
3418 end_cmd_q(); // stop adding to q
3419 break;
3420 case 'A': // A- append at e-o-l
3421 dot_end(); // go to e-o-l
3422 //**** fall thru to ... 'a'
3423 case 'a': // a- append after current char
3424 if (*dot != '\n')
3425 dot++;
3426 goto dc_i;
3427 break;
3428 case 'B': // B- back a blank-delimited Word
3429 case 'E': // E- end of a blank-delimited word
3430 case 'W': // W- forward a blank-delimited word
3431 if (cmdcnt-- > 1) {
3432 do_cmd(c);
3433 } // repeat cnt
3434 dir = FORWARD;
3435 if (c == 'B')
3436 dir = BACK;
3437 if (c == 'W' || isspace(dot[dir])) {
3438 dot = skip_thing(dot, 1, dir, S_TO_WS);
3439 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3440 }
3441 if (c != 'W')
3442 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3443 break;
3444 case 'C': // C- Change to e-o-l
3445 case 'D': // D- delete to e-o-l
3446 save_dot = dot;
3447 dot = dollar_line(dot); // move to before NL
3448 // copy text into a register and delete
3449 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3450 if (c == 'C')
3451 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003452#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003453 if (c == 'D')
3454 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003455#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003456 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003457 case 'G': // G- goto to a line number (default= E-O-F)
3458 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003459 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003460 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003461 }
3462 dot_skip_over_ws();
3463 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003464 case 'H': // H- goto top line on screen
3465 dot = screenbegin;
3466 if (cmdcnt > (rows - 1)) {
3467 cmdcnt = (rows - 1);
3468 }
3469 if (cmdcnt-- > 1) {
3470 do_cmd('+');
3471 } // repeat cnt
3472 dot_skip_over_ws();
3473 break;
3474 case 'I': // I- insert before first non-blank
3475 dot_begin(); // 0
3476 dot_skip_over_ws();
3477 //**** fall thru to ... 'i'
3478 case 'i': // i- insert before current char
3479 case VI_K_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003480 dc_i:
Eric Andersen3f980402001-04-04 17:31:15 +00003481 cmd_mode = 1; // start insrting
Eric Andersen3f980402001-04-04 17:31:15 +00003482 break;
3483 case 'J': // J- join current and next lines together
3484 if (cmdcnt-- > 2) {
3485 do_cmd(c);
3486 } // repeat cnt
3487 dot_end(); // move to NL
3488 if (dot < end - 1) { // make sure not last char in text[]
3489 *dot++ = ' '; // replace NL with space
Paul Fox8552aec2005-09-16 12:20:05 +00003490 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003491 while (isblank(*dot)) { // delete leading WS
Eric Andersen3f980402001-04-04 17:31:15 +00003492 dot_delete();
3493 }
3494 }
3495 end_cmd_q(); // stop adding to q
3496 break;
3497 case 'L': // L- goto bottom line on screen
3498 dot = end_screen();
3499 if (cmdcnt > (rows - 1)) {
3500 cmdcnt = (rows - 1);
3501 }
3502 if (cmdcnt-- > 1) {
3503 do_cmd('-');
3504 } // repeat cnt
3505 dot_begin();
3506 dot_skip_over_ws();
3507 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003508 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003509 dot = screenbegin;
3510 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3511 dot = next_line(dot);
3512 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003513 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003514 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003515 p = begin_line(dot);
3516 if (p[-1] == '\n') {
3517 dot_prev();
3518 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3519 dot_end();
3520 dot = char_insert(dot, '\n');
3521 } else {
3522 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003523 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003524 dot_prev(); // -
3525 }
3526 goto dc_i;
3527 break;
3528 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003529 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003530 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003531 break;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003532 case VI_K_DELETE:
3533 c = 'x';
3534 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003535 case 'X': // X- delete char before dot
3536 case 'x': // x- delete the current char
3537 case 's': // s- substitute the current char
3538 if (cmdcnt-- > 1) {
3539 do_cmd(c);
3540 } // repeat cnt
3541 dir = 0;
3542 if (c == 'X')
3543 dir = -1;
3544 if (dot[dir] != '\n') {
3545 if (c == 'X')
3546 dot--; // delete prev char
3547 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3548 }
3549 if (c == 's')
3550 goto dc_i; // start insrting
3551 end_cmd_q(); // stop adding to q
3552 break;
3553 case 'Z': // Z- if modified, {write}; exit
3554 // ZZ means to save file (if necessary), then exit
3555 c1 = get_one_char();
3556 if (c1 != 'Z') {
3557 indicate_error(c);
3558 break;
3559 }
Paul Foxf0305b72006-03-28 14:18:21 +00003560 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003561 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003562 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003563 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003564 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003565 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003566 if (cnt < 0) {
3567 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003568 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003569 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003570 editing = 0;
3571 }
3572 } else {
3573 editing = 0;
3574 }
3575 break;
3576 case '^': // ^- move to first non-blank on line
3577 dot_begin();
3578 dot_skip_over_ws();
3579 break;
3580 case 'b': // b- back a word
3581 case 'e': // e- end of word
3582 if (cmdcnt-- > 1) {
3583 do_cmd(c);
3584 } // repeat cnt
3585 dir = FORWARD;
3586 if (c == 'b')
3587 dir = BACK;
3588 if ((dot + dir) < text || (dot + dir) > end - 1)
3589 break;
3590 dot += dir;
3591 if (isspace(*dot)) {
3592 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3593 }
3594 if (isalnum(*dot) || *dot == '_') {
3595 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3596 } else if (ispunct(*dot)) {
3597 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3598 }
3599 break;
3600 case 'c': // c- change something
3601 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003602#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003603 case 'y': // y- yank something
3604 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003605#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003606 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003607#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003608 if (c == 'y' || c == 'Y')
3609 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003610#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003611 c1 = 'y';
3612 if (c != 'Y')
3613 c1 = get_one_char(); // get the type of thing to delete
3614 find_range(&p, &q, c1);
3615 if (c1 == 27) { // ESC- user changed mind and wants out
3616 c = c1 = 27; // Escape- do nothing
3617 } else if (strchr("wW", c1)) {
3618 if (c == 'c') {
3619 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003620 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003621 if (q <= text || q[-1] == '\n')
3622 break;
3623 q--;
3624 }
3625 }
3626 dot = yank_delete(p, q, 0, yf); // delete word
Eric Andersen822c3832001-05-07 17:37:43 +00003627 } else if (strchr("^0bBeEft$", c1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003628 // single line copy text into a register and delete
3629 dot = yank_delete(p, q, 0, yf); // delete word
Eric Andersen1c0d3112001-04-16 15:46:44 +00003630 } else if (strchr("cdykjHL%+-{}\r\n", c1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003631 // multiple line copy text into a register and delete
3632 dot = yank_delete(p, q, 1, yf); // delete lines
Eric Andersen1c0d3112001-04-16 15:46:44 +00003633 if (c == 'c') {
3634 dot = char_insert(dot, '\n');
3635 // on the last line of file don't move to prev line
3636 if (dot != (end-1)) {
3637 dot_prev();
3638 }
3639 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003640 dot_begin();
3641 dot_skip_over_ws();
3642 }
3643 } else {
3644 // could not recognize object
3645 c = c1 = 27; // error-
3646 indicate_error(c);
3647 }
3648 if (c1 != 27) {
3649 // if CHANGING, not deleting, start inserting after the delete
3650 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003651 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003652 goto dc_i; // start inserting
3653 }
3654 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003655 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003656 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003657#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003658 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003659 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003660 }
3661 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003662 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003663 for (cnt = 0; p <= q; p++) {
3664 if (*p == '\n')
3665 cnt++;
3666 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003667 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003668 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003669#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003670 end_cmd_q(); // stop adding to q
3671 }
3672 break;
3673 case 'k': // k- goto prev line, same col
3674 case VI_K_UP: // cursor key Up
3675 if (cmdcnt-- > 1) {
3676 do_cmd(c);
3677 } // repeat cnt
3678 dot_prev();
3679 dot = move_to_col(dot, ccol + offset); // try stay in same col
3680 break;
3681 case 'r': // r- replace the current char with user input
3682 c1 = get_one_char(); // get the replacement char
3683 if (*dot != '\n') {
3684 *dot = c1;
Paul Fox8552aec2005-09-16 12:20:05 +00003685 file_modified++; // has the file been modified
Eric Andersen3f980402001-04-04 17:31:15 +00003686 }
3687 end_cmd_q(); // stop adding to q
3688 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003689 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003690 last_forward_char = get_one_char();
3691 do_cmd(';');
3692 if (*dot == last_forward_char)
3693 dot_left();
3694 last_forward_char= 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003695 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003696 case 'w': // w- forward a word
3697 if (cmdcnt-- > 1) {
3698 do_cmd(c);
3699 } // repeat cnt
3700 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3701 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3702 } else if (ispunct(*dot)) { // we are on PUNCT
3703 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3704 }
3705 if (dot < end - 1)
3706 dot++; // move over word
3707 if (isspace(*dot)) {
3708 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3709 }
3710 break;
3711 case 'z': // z-
3712 c1 = get_one_char(); // get the replacement char
3713 cnt = 0;
3714 if (c1 == '.')
3715 cnt = (rows - 2) / 2; // put dot at center
3716 if (c1 == '-')
3717 cnt = rows - 2; // put dot at bottom
3718 screenbegin = begin_line(dot); // start dot at top
3719 dot_scroll(cnt, -1);
3720 break;
3721 case '|': // |- move to column "cmdcnt"
3722 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3723 break;
3724 case '~': // ~- flip the case of letters a-z -> A-Z
3725 if (cmdcnt-- > 1) {
3726 do_cmd(c);
3727 } // repeat cnt
3728 if (islower(*dot)) {
3729 *dot = toupper(*dot);
Paul Fox8552aec2005-09-16 12:20:05 +00003730 file_modified++; // has the file been modified
Eric Andersen3f980402001-04-04 17:31:15 +00003731 } else if (isupper(*dot)) {
3732 *dot = tolower(*dot);
Paul Fox8552aec2005-09-16 12:20:05 +00003733 file_modified++; // has the file been modified
Eric Andersen3f980402001-04-04 17:31:15 +00003734 }
3735 dot_right();
3736 end_cmd_q(); // stop adding to q
3737 break;
3738 //----- The Cursor and Function Keys -----------------------------
3739 case VI_K_HOME: // Cursor Key Home
3740 dot_begin();
3741 break;
3742 // The Fn keys could point to do_macro which could translate them
3743 case VI_K_FUN1: // Function Key F1
3744 case VI_K_FUN2: // Function Key F2
3745 case VI_K_FUN3: // Function Key F3
3746 case VI_K_FUN4: // Function Key F4
3747 case VI_K_FUN5: // Function Key F5
3748 case VI_K_FUN6: // Function Key F6
3749 case VI_K_FUN7: // Function Key F7
3750 case VI_K_FUN8: // Function Key F8
3751 case VI_K_FUN9: // Function Key F9
3752 case VI_K_FUN10: // Function Key F10
3753 case VI_K_FUN11: // Function Key F11
3754 case VI_K_FUN12: // Function Key F12
3755 break;
3756 }
3757
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003758 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003759 // if text[] just became empty, add back an empty line
3760 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003761 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003762 dot = text;
3763 }
3764 // it is OK for dot to exactly equal to end, otherwise check dot validity
3765 if (dot != end) {
3766 dot = bound_dot(dot); // make sure "dot" is valid
3767 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003768#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003769 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003770#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003771
3772 if (!isdigit(c))
3773 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3774 cnt = dot - begin_line(dot);
3775 // Try to stay off of the Newline
3776 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3777 dot--;
3778}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003779
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003780#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003781static int totalcmds = 0;
3782static int Mp = 85; // Movement command Probability
3783static int Np = 90; // Non-movement command Probability
3784static int Dp = 96; // Delete command Probability
3785static int Ip = 97; // Insert command Probability
3786static int Yp = 98; // Yank command Probability
3787static int Pp = 99; // Put command Probability
3788static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003789static const char chars[20] = "\t012345 abcdABCD-=.$";
3790static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003791 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003792 "broadcast", "the", "emergency", "of",
3793 "system", "quick", "brown", "fox",
3794 "jumped", "over", "lazy", "dogs",
3795 "back", "January", "Febuary", "March"
3796};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003797static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003798 "You should have received a copy of the GNU General Public License\n",
3799 "char c, cm, *cmd, *cmd1;\n",
3800 "generate a command by percentages\n",
3801 "Numbers may be typed as a prefix to some commands.\n",
3802 "Quit, discarding changes!\n",
3803 "Forced write, if permission originally not valid.\n",
3804 "In general, any ex or ed command (such as substitute or delete).\n",
3805 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3806 "Please get w/ me and I will go over it with you.\n",
3807 "The following is a list of scheduled, committed changes.\n",
3808 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3809 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3810 "Any question about transactions please contact Sterling Huxley.\n",
3811 "I will try to get back to you by Friday, December 31.\n",
3812 "This Change will be implemented on Friday.\n",
3813 "Let me know if you have problems accessing this;\n",
3814 "Sterling Huxley recently added you to the access list.\n",
3815 "Would you like to go to lunch?\n",
3816 "The last command will be automatically run.\n",
3817 "This is too much english for a computer geek.\n",
3818};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003819static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003820 "You should have received a copy of the GNU General Public License\n",
3821 "char c, cm, *cmd, *cmd1;\n",
3822 "generate a command by percentages\n",
3823 "Numbers may be typed as a prefix to some commands.\n",
3824 "Quit, discarding changes!\n",
3825 "Forced write, if permission originally not valid.\n",
3826 "In general, any ex or ed command (such as substitute or delete).\n",
3827 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3828 "Please get w/ me and I will go over it with you.\n",
3829 "The following is a list of scheduled, committed changes.\n",
3830 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3831 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3832 "Any question about transactions please contact Sterling Huxley.\n",
3833 "I will try to get back to you by Friday, December 31.\n",
3834 "This Change will be implemented on Friday.\n",
3835 "Let me know if you have problems accessing this;\n",
3836 "Sterling Huxley recently added you to the access list.\n",
3837 "Would you like to go to lunch?\n",
3838 "The last command will be automatically run.\n",
3839 "This is too much english for a computer geek.\n",
3840};
3841
3842// create a random command to execute
3843static void crash_dummy()
3844{
3845 static int sleeptime; // how long to pause between commands
3846 char c, cm, *cmd, *cmd1;
3847 int i, cnt, thing, rbi, startrbi, percent;
3848
3849 // "dot" movement commands
3850 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3851
3852 // is there already a command running?
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003853 if (chars_to_parse > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003854 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003855 cd0:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003856 startrbi = rbi = 0;
3857 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003858 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003859 // generate a command by percentages
3860 percent = (int) lrand48() % 100; // get a number from 0-99
3861 if (percent < Mp) { // Movement commands
3862 // available commands
3863 cmd = cmd1;
3864 M++;
3865 } else if (percent < Np) { // non-movement commands
3866 cmd = "mz<>\'\""; // available commands
3867 N++;
3868 } else if (percent < Dp) { // Delete commands
3869 cmd = "dx"; // available commands
3870 D++;
3871 } else if (percent < Ip) { // Inset commands
3872 cmd = "iIaAsrJ"; // available commands
3873 I++;
3874 } else if (percent < Yp) { // Yank commands
3875 cmd = "yY"; // available commands
3876 Y++;
3877 } else if (percent < Pp) { // Put commands
3878 cmd = "pP"; // available commands
3879 P++;
3880 } else {
3881 // We do not know how to handle this command, try again
3882 U++;
3883 goto cd0;
3884 }
3885 // randomly pick one of the available cmds from "cmd[]"
3886 i = (int) lrand48() % strlen(cmd);
3887 cm = cmd[i];
3888 if (strchr(":\024", cm))
3889 goto cd0; // dont allow colon or ctrl-T commands
3890 readbuffer[rbi++] = cm; // put cmd into input buffer
3891
3892 // now we have the command-
3893 // there are 1, 2, and multi char commands
3894 // find out which and generate the rest of command as necessary
3895 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
3896 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
3897 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
3898 cmd1 = "abcdefghijklmnopqrstuvwxyz";
3899 }
3900 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3901 c = cmd1[thing];
3902 readbuffer[rbi++] = c; // add movement to input buffer
3903 }
3904 if (strchr("iIaAsc", cm)) { // multi-char commands
3905 if (cm == 'c') {
3906 // change some thing
3907 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3908 c = cmd1[thing];
3909 readbuffer[rbi++] = c; // add movement to input buffer
3910 }
3911 thing = (int) lrand48() % 4; // what thing to insert
3912 cnt = (int) lrand48() % 10; // how many to insert
3913 for (i = 0; i < cnt; i++) {
3914 if (thing == 0) { // insert chars
3915 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
3916 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003917 strcat(readbuffer, words[(int) lrand48() % 20]);
3918 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003919 sleeptime = 0; // how fast to type
3920 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003921 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003922 sleeptime = 0; // how fast to type
3923 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003924 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003925 sleeptime = 0; // how fast to type
3926 }
3927 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003928 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003929 }
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003930 chars_to_parse = strlen(readbuffer);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003931 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003932 totalcmds++;
3933 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003934 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003935}
3936
3937// test to see if there are any errors
3938static void crash_test()
3939{
3940 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003941
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003942 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003943 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003944
3945 msg[0] = '\0';
3946 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003947 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003948 }
3949 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003950 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003951 }
3952 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003953 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003954 }
3955 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003956 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003957 }
3958 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003959 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003960 }
3961 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003962 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003963 }
3964
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003965 if (msg[0]) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003966 alarm(0);
Glenn L McGrath7127b582002-12-03 21:48:15 +00003967 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003968 totalcmds, last_input_char, msg, SOs, SOn);
3969 fflush(stdout);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00003970 while (safe_read(0, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003971 if (d[0] == '\n' || d[0] == '\r')
3972 break;
3973 }
3974 alarm(3);
3975 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003976 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003977 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003978 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003979 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
3980 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
3981 oldtim = tim;
3982 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003983}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003984#endif