blob: 3656fee9596872adda132a4f732effa678875772 [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 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
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
Pere Orga6a3e01d2011-04-01 22:56:30 +020024//usage:#define vi_trivial_usage
25//usage: "[OPTIONS] [FILE]..."
26//usage:#define vi_full_usage "\n\n"
27//usage: "Edit FILE\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +020028//usage: IF_FEATURE_VI_COLON(
29//usage: "\n -c Initial command to run ($EXINIT also available)"
30//usage: )
31//usage: IF_FEATURE_VI_READONLY(
32//usage: "\n -R Read-only"
33//usage: )
34//usage: "\n -H Short help regarding available features"
35
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000036#include "libbb.h"
Eric Andersen3f980402001-04-04 17:31:15 +000037
Paul Fox35e9c5d2008-03-06 16:26:12 +000038/* the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +000039#define ENABLE_FEATURE_VI_CRASHME 0
40
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000041
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +000042#if ENABLE_LOCALE_SUPPORT
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000043
44#if ENABLE_FEATURE_VI_8BIT
Denys Vlasenkoc2704542009-11-20 19:14:19 +010045//FIXME: this does not work properly for Unicode anyway
46# define Isprint(c) (isprint)(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +000047#else
Denys Vlasenkoc2704542009-11-20 19:14:19 +010048# define Isprint(c) isprint_asciionly(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +000049#endif
50
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000051#else
52
53/* 0x9b is Meta-ESC */
54#if ENABLE_FEATURE_VI_8BIT
55#define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
56#else
57#define Isprint(c) ((unsigned char)(c) >= ' ' && (unsigned char)(c) < 0x7f)
58#endif
59
60#endif
61
62
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000063enum {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +000064 MAX_TABSTOP = 32, // sanity limit
Denis Vlasenko88adfcd2007-12-22 15:40:13 +000065 // User input len. Need not be extra big.
66 // Lines in file being edited *can* be bigger than this.
67 MAX_INPUT_LEN = 128,
68 // Sanity limits. We have only one buffer of this size.
69 MAX_SCR_COLS = CONFIG_FEATURE_VI_MAX_LEN,
70 MAX_SCR_ROWS = CONFIG_FEATURE_VI_MAX_LEN,
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000071};
Eric Andersen3f980402001-04-04 17:31:15 +000072
Glenn L McGrath09adaca2002-12-02 21:18:10 +000073/* vt102 typical ESC sequence */
74/* terminal standout start/normal ESC sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020075#define SOs "\033[7m"
76#define SOn "\033[0m"
Glenn L McGrath09adaca2002-12-02 21:18:10 +000077/* terminal bell sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020078#define bell "\007"
Glenn L McGrath09adaca2002-12-02 21:18:10 +000079/* Clear-end-of-line and Clear-end-of-screen ESC sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020080#define Ceol "\033[K"
81#define Ceos "\033[J"
Glenn L McGrath09adaca2002-12-02 21:18:10 +000082/* Cursor motion arbitrary destination ESC sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020083#define CMrc "\033[%u;%uH"
Glenn L McGrath09adaca2002-12-02 21:18:10 +000084/* Cursor motion up and down ESC sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +020085#define CMup "\033[A"
86#define CMdown "\n"
Glenn L McGrath09adaca2002-12-02 21:18:10 +000087
Denis Vlasenkoded6ad32008-10-14 12:26:30 +000088#if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
89// cmds modifying text[]
90// vda: removed "aAiIs" as they switch us into insert mode
91// and remembering input for replay after them makes no sense
92static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~";
93#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +000094
Rob Landleybc68cd12006-03-10 19:22:06 +000095enum {
96 YANKONLY = FALSE,
97 YANKDEL = TRUE,
98 FORWARD = 1, // code depends on "1" for array index
99 BACK = -1, // code depends on "-1" for array index
100 LIMITED = 0, // how much of text[] in char_search
101 FULL = 1, // how much of text[] in char_search
Eric Andersen3f980402001-04-04 17:31:15 +0000102
Rob Landleybc68cd12006-03-10 19:22:06 +0000103 S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
104 S_TO_WS = 2, // used in skip_thing() for moving "dot"
105 S_OVER_WS = 3, // used in skip_thing() for moving "dot"
106 S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000107 S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
Rob Landleybc68cd12006-03-10 19:22:06 +0000108};
Eric Andersen3f980402001-04-04 17:31:15 +0000109
Denis Vlasenkob1759462008-06-20 20:20:54 +0000110
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000111/* vi.c expects chars to be unsigned. */
112/* busybox build system provides that, but it's better */
113/* to audit and fix the source */
Eric Andersen3f980402001-04-04 17:31:15 +0000114
Denis Vlasenkob1759462008-06-20 20:20:54 +0000115struct globals {
116 /* many references - keep near the top of globals */
117 char *text, *end; // pointers to the user data in memory
118 char *dot; // where all the action takes place
119 int text_size; // size of the allocated buffer
120
121 /* the rest */
122 smallint vi_setops;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000123#define VI_AUTOINDENT 1
124#define VI_SHOWMATCH 2
125#define VI_IGNORECASE 4
126#define VI_ERR_METHOD 8
127#define autoindent (vi_setops & VI_AUTOINDENT)
128#define showmatch (vi_setops & VI_SHOWMATCH )
129#define ignorecase (vi_setops & VI_IGNORECASE)
130/* indicate error with beep or flash */
131#define err_method (vi_setops & VI_ERR_METHOD)
132
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000133#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000134 smallint readonly_mode;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000135#define SET_READONLY_FILE(flags) ((flags) |= 0x01)
136#define SET_READONLY_MODE(flags) ((flags) |= 0x02)
137#define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000138#else
Denis Vlasenkob1759462008-06-20 20:20:54 +0000139#define SET_READONLY_FILE(flags) ((void)0)
140#define SET_READONLY_MODE(flags) ((void)0)
141#define UNSET_READONLY_FILE(flags) ((void)0)
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000142#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000143
Denis Vlasenkob1759462008-06-20 20:20:54 +0000144 smallint editing; // >0 while we are editing a file
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000145 // [code audit says "can be 0, 1 or 2 only"]
Denis Vlasenkob1759462008-06-20 20:20:54 +0000146 smallint cmd_mode; // 0=command 1=insert 2=replace
147 int file_modified; // buffer contents changed (counter, not flag!)
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000148 int last_file_modified; // = -1;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000149 int fn_start; // index of first cmd line file name
150 int save_argc; // how many file names on cmd line
151 int cmdcnt; // repetition count
152 unsigned rows, columns; // the terminal screen is this size
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700153#if ENABLE_FEATURE_VI_ASK_TERMINAL
154 int get_rowcol_error;
155#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000156 int crow, ccol; // cursor is on Crow x Ccol
157 int offset; // chars scrolled off the screen to the left
158 int have_status_msg; // is default edit status needed?
159 // [don't make smallint!]
160 int last_status_cksum; // hash of current status line
161 char *current_filename;
162 char *screenbegin; // index into text[], of top line on the screen
163 char *screen; // pointer to the virtual screen buffer
164 int screensize; // and its size
165 int tabstop;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000166 int last_forward_char; // last char searched for with 'f' (int because of Unicode)
Denis Vlasenkob1759462008-06-20 20:20:54 +0000167 char erase_char; // the users erase character
168 char last_input_char; // last char read from user
Denis Vlasenkob1759462008-06-20 20:20:54 +0000169
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000170#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkob1759462008-06-20 20:20:54 +0000171 smallint adding2q; // are we currently adding user input to q
172 int lmc_len; // length of last_modifying_cmd
173 char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000174#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000175#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenkob1759462008-06-20 20:20:54 +0000176 int last_row; // where the cursor was last moved to
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000177#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000178#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkob1759462008-06-20 20:20:54 +0000179 int my_pid;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000180#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000181#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkob1759462008-06-20 20:20:54 +0000182 char *last_search_pattern; // last pattern from a '/' or '?' search
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000183#endif
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000184
Denis Vlasenkob1759462008-06-20 20:20:54 +0000185 /* former statics */
186#if ENABLE_FEATURE_VI_YANKMARK
187 char *edit_file__cur_line;
188#endif
189 int refresh__old_offset;
190 int format_edit_status__tot;
Eric Andersen3f980402001-04-04 17:31:15 +0000191
Denis Vlasenkob1759462008-06-20 20:20:54 +0000192 /* a few references only */
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000193#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000194 int YDreg, Ureg; // default delete register and orig line for "U"
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000195 char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000196 char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
197 char *context_start, *context_end;
198#endif
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000199#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkob1759462008-06-20 20:20:54 +0000200 sigjmp_buf restart; // catch_sig()
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000201#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000202 struct termios term_orig, term_vi; // remember what the cooked mode was
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000203#if ENABLE_FEATURE_VI_COLON
204 char *initial_cmds[3]; // currently 2 entries, NULL terminated
205#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000206 // Should be just enough to hold a key sequence,
Denis Vlasenko25497c12008-10-14 10:25:05 +0000207 // but CRASHME mode uses it as generated command buffer too
208#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko1dfeeeb2008-10-18 19:04:37 +0000209 char readbuffer[128];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000210#else
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000211 char readbuffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000212#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000213#define STATUS_BUFFER_LEN 200
214 char status_buffer[STATUS_BUFFER_LEN]; // messages to the user
215#if ENABLE_FEATURE_VI_DOT_CMD
216 char last_modifying_cmd[MAX_INPUT_LEN]; // last modifying cmd for "."
217#endif
218 char get_input_line__buf[MAX_INPUT_LEN]; /* former static */
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000219
220 char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000221};
222#define G (*ptr_to_globals)
223#define text (G.text )
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000224#define text_size (G.text_size )
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000225#define end (G.end )
226#define dot (G.dot )
227#define reg (G.reg )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000228
229#define vi_setops (G.vi_setops )
230#define editing (G.editing )
231#define cmd_mode (G.cmd_mode )
232#define file_modified (G.file_modified )
233#define last_file_modified (G.last_file_modified )
234#define fn_start (G.fn_start )
235#define save_argc (G.save_argc )
236#define cmdcnt (G.cmdcnt )
237#define rows (G.rows )
238#define columns (G.columns )
239#define crow (G.crow )
240#define ccol (G.ccol )
241#define offset (G.offset )
242#define status_buffer (G.status_buffer )
243#define have_status_msg (G.have_status_msg )
244#define last_status_cksum (G.last_status_cksum )
245#define current_filename (G.current_filename )
246#define screen (G.screen )
247#define screensize (G.screensize )
248#define screenbegin (G.screenbegin )
249#define tabstop (G.tabstop )
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000250#define last_forward_char (G.last_forward_char )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000251#define erase_char (G.erase_char )
252#define last_input_char (G.last_input_char )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000253#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000254#define readonly_mode (G.readonly_mode )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000255#else
256#define readonly_mode 0
257#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000258#define adding2q (G.adding2q )
259#define lmc_len (G.lmc_len )
260#define ioq (G.ioq )
261#define ioq_start (G.ioq_start )
262#define last_row (G.last_row )
263#define my_pid (G.my_pid )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000264#define last_search_pattern (G.last_search_pattern)
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000265
Denis Vlasenkob1759462008-06-20 20:20:54 +0000266#define edit_file__cur_line (G.edit_file__cur_line)
267#define refresh__old_offset (G.refresh__old_offset)
268#define format_edit_status__tot (G.format_edit_status__tot)
269
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000270#define YDreg (G.YDreg )
271#define Ureg (G.Ureg )
272#define mark (G.mark )
273#define context_start (G.context_start )
274#define context_end (G.context_end )
275#define restart (G.restart )
276#define term_orig (G.term_orig )
277#define term_vi (G.term_vi )
278#define initial_cmds (G.initial_cmds )
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000279#define readbuffer (G.readbuffer )
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000280#define scr_out_buf (G.scr_out_buf )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000281#define last_modifying_cmd (G.last_modifying_cmd )
282#define get_input_line__buf (G.get_input_line__buf)
283
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000284#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000285 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkob1759462008-06-20 20:20:54 +0000286 last_file_modified = -1; \
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000287 /* "" but has space for 2 chars: */ \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000288 IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000289} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000290
Denis Vlasenkob1759462008-06-20 20:20:54 +0000291
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000292static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000293static void edit_file(char *); // edit one file
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000294static void do_cmd(int); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000295static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000296static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
297static char *begin_line(char *); // return pointer to cur line B-o-l
298static char *end_line(char *); // return pointer to cur line E-o-l
299static char *prev_line(char *); // return pointer to prev line B-o-l
300static char *next_line(char *); // return pointer to next line B-o-l
301static char *end_screen(void); // get pointer to last char on screen
302static int count_lines(char *, char *); // count line from start to stop
303static char *find_line(int); // find begining of line #li
304static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000305static void dot_left(void); // move dot left- dont leave line
306static void dot_right(void); // move dot right- dont leave line
307static void dot_begin(void); // move dot to B-o-l
308static void dot_end(void); // move dot to E-o-l
309static void dot_next(void); // move dot to next line B-o-l
310static void dot_prev(void); // move dot to prev line B-o-l
311static void dot_scroll(int, int); // move the screen up or down
312static void dot_skip_over_ws(void); // move dot pat WS
313static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000314static char *bound_dot(char *); // make sure text[0] <= P < "end"
315static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000316static char *char_insert(char *, char); // insert the char c at 'p'
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000317// might reallocate text[]! use p += stupid_insert(p, ...),
318// and be careful to not use pointers into potentially freed text[]!
319static uintptr_t stupid_insert(char *, char); // stupidly insert the char c at 'p'
Paul Foxc51fc7b2008-03-06 01:34:23 +0000320static int find_range(char **, char **, char); // return pointers for an object
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000321static int st_test(char *, int, int, char *); // helper for skip_thing()
322static char *skip_thing(char *, int, int, int); // skip some object
323static char *find_pair(char *, char); // find matching pair () [] {}
324static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000325// might reallocate text[]! use p += text_hole_make(p, ...),
326// and be careful to not use pointers into potentially freed text[]!
327static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000328static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000329static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000330static void rawmode(void); // set "raw" mode on tty
331static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000332// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
333static int mysleep(int);
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000334static int readit(void); // read (maybe cursor) key from stdin
335static int get_one_char(void); // read 1 char from stdin
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000336static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000337#if !ENABLE_FEATURE_VI_READONLY
338#define file_insert(fn, p, update_ro_status) file_insert(fn, p)
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000339#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000340// file_insert might reallocate text[]!
341static int file_insert(const char *, char *, int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000342static int file_write(char *, char *, char *);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000343#if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
344#define place_cursor(a, b, optimize) place_cursor(a, b)
345#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000346static void place_cursor(int, int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000347static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000348static void clear_to_eol(void);
349static void clear_to_eos(void);
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000350static void go_bottom_and_clear_to_eol(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000351static void standout_start(void); // send "start reverse video" sequence
352static void standout_end(void); // send "end reverse video" sequence
353static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000354static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000355static void status_line(const char *, ...); // print to status buf
356static void status_line_bold(const char *, ...);
357static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000358static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000359static void redraw(int); // force a full screen refresh
Denis Vlasenko68404f12008-03-17 09:00:54 +0000360static char* format_line(char* /*, int*/);
Eric Andersen3f980402001-04-04 17:31:15 +0000361static void refresh(int); // update the terminal from screen[]
362
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000363static void Indicate_Error(void); // use flash or beep to indicate error
364#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000365static void Hit_Return(void);
366
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000367#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000368static char *char_search(char *, const char *, int, int); // search for pattern starting at p
369static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000370#endif
371#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000372static char *get_one_address(char *, int *); // get colon addr, if present
373static char *get_address(char *, int *, int *); // get two colon addrs, if present
374static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000375#endif
376#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000377static void winch_sig(int); // catch window size changes
378static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000379static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000380#endif
381#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000382static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000383static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000384#else
385#define end_cmd_q() ((void)0)
386#endif
387#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000388static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000389#endif
390#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000391// might reallocate text[]! use p += string_insert(p, ...),
392// and be careful to not use pointers into potentially freed text[]!
393static uintptr_t string_insert(char *, const char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000394#endif
395#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000396static char *text_yank(char *, char *, int); // save copy of "p" into a register
397static char what_reg(void); // what is letter of current YDreg
398static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000399#endif
400#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000401static void crash_dummy();
402static void crash_test();
403static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000404#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000405
406
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000407static void write1(const char *out)
408{
409 fputs(out, stdout);
410}
411
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000412int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000413int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000414{
Eric Andersend402edf2001-04-04 19:29:48 +0000415 int c;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000416
417 INIT_G();
Eric Andersen3f980402001-04-04 17:31:15 +0000418
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000419#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000420 my_pid = getpid();
421#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000422#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000423 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000424#endif
Denis Vlasenko2414a962007-07-18 22:03:40 +0000425#ifdef NO_SUCH_APPLET_YET
426 /* If we aren't "vi", we are "view" */
427 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000428 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000429 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000430#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000431
Bernhard Reutner-Fischer73f56bb2007-09-22 21:18:46 +0000432 vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenkof9234132007-03-21 00:03:42 +0000433 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000434 // 2- process EXINIT variable from environment
435 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000436#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000437 {
438 char *p = getenv("EXINIT");
439 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000440 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000441 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000442#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000443 while ((c = getopt(argc, argv, "hCRH" IF_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000444 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000445#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000446 case 'C':
447 crashme = 1;
448 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000449#endif
450#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000451 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000452 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000453 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000454#endif
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000455#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000456 case 'c': // cmd line vi command
457 if (*optarg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000458 initial_cmds[initial_cmds[0] != 0] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000459 break;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000460#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000461 case 'H':
Eric Andersen3f980402001-04-04 17:31:15 +0000462 show_help();
Paul Fox35e9c5d2008-03-06 16:26:12 +0000463 /* fall through */
Paul Fox35e9c5d2008-03-06 16:26:12 +0000464 default:
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000465 bb_show_usage();
Eric Andersendd8500b2001-07-02 18:06:14 +0000466 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000467 }
468 }
469
470 // The argv array can be used by the ":next" and ":rewind" commands
471 // save optind.
472 fn_start = optind; // remember first file name for :next and :rew
473 save_argc = argc;
474
475 //----- This is the main file handling loop --------------
Denys Vlasenko04cecd52010-04-16 22:13:55 -0700476 while (1) {
477 edit_file(argv[optind]); /* param might be NULL */
478 if (++optind >= argc)
479 break;
Eric Andersen3f980402001-04-04 17:31:15 +0000480 }
481 //-----------------------------------------------------------
482
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000483 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000484}
485
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000486/* read text from file or create an empty buf */
487/* will also update current_filename */
488static int init_text_buffer(char *fn)
489{
490 int rc;
491 int size = file_size(fn); // file size. -1 means does not exist.
492
493 /* allocate/reallocate text buffer */
494 free(text);
Denis Vlasenkob1759462008-06-20 20:20:54 +0000495 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000496 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000497
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000498 if (fn != current_filename) {
499 free(current_filename);
500 current_filename = xstrdup(fn);
501 }
502 if (size < 0) {
503 // file dont exist. Start empty buf with dummy line
504 char_insert(text, '\n');
505 rc = 0;
506 } else {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000507 rc = file_insert(fn, text, 1);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000508 }
509 file_modified = 0;
510 last_file_modified = -1;
511#if ENABLE_FEATURE_VI_YANKMARK
512 /* init the marks. */
513 memset(mark, 0, sizeof(mark));
514#endif
515 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000516}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000517
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700518#if ENABLE_FEATURE_VI_WIN_RESIZE
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200519static int query_screen_dimensions(void)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700520{
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200521 int err = get_terminal_width_height(STDIN_FILENO, &columns, &rows);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700522 if (rows > MAX_SCR_ROWS)
523 rows = MAX_SCR_ROWS;
524 if (columns > MAX_SCR_COLS)
525 columns = MAX_SCR_COLS;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200526 return err;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700527}
528#else
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200529# define query_screen_dimensions() (0)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700530#endif
531
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000532static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000533{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000534#if ENABLE_FEATURE_VI_YANKMARK
535#define cur_line edit_file__cur_line
536#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000537 int c;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000538#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000539 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000540#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000541
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000542 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000543 rawmode();
544 rows = 24;
545 columns = 80;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200546 IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions();
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700547#if ENABLE_FEATURE_VI_ASK_TERMINAL
548 if (G.get_rowcol_error /* TODO? && no input on stdin */) {
549 uint64_t k;
550 write1("\033[999;999H" "\033[6n");
551 fflush_all();
552 k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100);
553 if ((int32_t)k == KEYCODE_CURSOR_POS) {
554 uint32_t rc = (k >> 32);
555 columns = (rc & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200556 if (columns > MAX_SCR_COLS)
557 columns = MAX_SCR_COLS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700558 rows = ((rc >> 16) & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200559 if (rows > MAX_SCR_ROWS)
560 rows = MAX_SCR_ROWS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700561 }
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700562 }
563#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000564 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000565 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000566
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000567#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000568 YDreg = 26; // default Yank/Delete reg
569 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000570 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000571#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000572
Eric Andersen3f980402001-04-04 17:31:15 +0000573 last_forward_char = last_input_char = '\0';
574 crow = 0;
575 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000576
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000577#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700578 signal(SIGINT, catch_sig);
Eric Andersen3f980402001-04-04 17:31:15 +0000579 signal(SIGWINCH, winch_sig);
580 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000581 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000582 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000583 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000584 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000585#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000586
Eric Andersen3f980402001-04-04 17:31:15 +0000587 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
588 cmdcnt = 0;
589 tabstop = 8;
590 offset = 0; // no horizontal offset
591 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000592#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000593 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000594 ioq = ioq_start = NULL;
595 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000596 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000597#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000598
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000599#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000600 {
601 char *p, *q;
602 int n = 0;
603
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700604 while ((p = initial_cmds[n]) != NULL) {
Denis Vlasenkof9234132007-03-21 00:03:42 +0000605 do {
606 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000607 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000608 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000609 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000610 *p++ = '\0';
611 if (*q)
612 colon(q);
613 } while (p);
614 free(initial_cmds[n]);
615 initial_cmds[n] = NULL;
616 n++;
617 }
618 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000619#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000620 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000621 //------This is the main Vi cmd handling loop -----------------------
622 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000623#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000624 if (crashme > 0) {
625 if ((end - text) > 1) {
626 crash_dummy(); // generate a random command
627 } else {
628 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000629 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
630 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000631 refresh(FALSE);
632 }
633 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000634#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000635 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000636#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000637 // save a copy of the current line- for the 'U" command
638 if (begin_line(dot) != cur_line) {
639 cur_line = begin_line(dot);
640 text_yank(begin_line(dot), end_line(dot), Ureg);
641 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000642#endif
643#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000644 // These are commands that change text[].
645 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000646 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000647 && cmd_mode == 0 // command mode
648 && c > '\0' // exclude NUL and non-ASCII chars
649 && c < 0x7f // (Unicode and such)
650 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000651 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000652 start_new_cmd_q(c);
653 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000654#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000655 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000656
Eric Andersen3f980402001-04-04 17:31:15 +0000657 // poll to see if there is input already waiting. if we are
658 // not able to display output fast enough to keep up, skip
659 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200660 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000661 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000662 refresh(FALSE);
663 show_status_line();
664 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000665#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000666 if (crashme > 0)
667 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000668#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000669 }
670 //-------------------------------------------------------------------
671
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000672 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000673 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000674#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000675}
676
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000677//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000678#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000679static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000680{
681 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000682 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000683 IF_FEATURE_VI_YANKMARK(char c;)
684 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000685
686 *addr = -1; // assume no addr
687 if (*p == '.') { // the current line
688 p++;
689 q = begin_line(dot);
690 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000691 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000692#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000693 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000694 p++;
695 c = tolower(*p);
696 p++;
697 if (c >= 'a' && c <= 'z') {
698 // we have a mark
699 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000700 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000701 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000702 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000703 }
704 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000705 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000706#endif
707#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000708 else if (*p == '/') { // a search pattern
709 q = strchrnul(++p, '/');
710 pat = xstrndup(p, q - p); // save copy of pattern
711 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000712 if (*p == '/')
713 p++;
714 q = char_search(dot, pat, FORWARD, FULL);
715 if (q != NULL) {
716 *addr = count_lines(text, q);
717 }
718 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000719 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000720#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000721 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000722 p++;
723 q = begin_line(end - 1);
724 *addr = count_lines(text, q);
725 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000726 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000727 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000728 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200729 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000730 *addr = -1;
731 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000732 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000733}
734
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000735static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000736{
737 //----- get the address' i.e., 1,3 'a,'b -----
738 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000739 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000740 p++; // skip over leading spaces
741 if (*p == '%') { // alias for 1,$
742 p++;
743 *b = 1;
744 *e = count_lines(text, end-1);
745 goto ga0;
746 }
747 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000748 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000749 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000750 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000751 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000752 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000753 p++;
754 // get SECOND addr, if present
755 p = get_one_address(p, e);
756 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000757 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000758 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000759 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000760 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000761}
762
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000763#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000764static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000765 const char *short_opname, int opt)
766{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000767 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000768 int l = strlen(opname) - 1; /* opname have + ' ' */
769
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200770 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000771 if (strncasecmp(a, opname, l) == 0
772 || strncasecmp(a, short_opname, 2) == 0
773 ) {
774 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000775 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000776 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000777 vi_setops |= opt;
778 }
779}
780#endif
781
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000782// buf must be no longer than MAX_INPUT_LEN!
783static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000784{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000785 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000786 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000787 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000788 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000789
790 // :3154 // if (-e line 3154) goto it else stay put
791 // :4,33w! foo // write a portion of buffer to file "foo"
792 // :w // write all of buffer to current file
793 // :q // quit
794 // :q! // quit- dont care about modified file
795 // :'a,'z!sort -u // filter block through sort
796 // :'f // goto mark "f"
797 // :'fl // list literal the mark "f" line
798 // :.r bar // read file "bar" into buffer before dot
799 // :/123/,/abc/d // delete lines from "123" line to "abc" line
800 // :/xyz/ // goto the "xyz" line
801 // :s/find/replace/ // substitute pattern "find" with "replace"
802 // :!<cmd> // run <cmd> then return
803 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000804
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000805 if (!buf[0])
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200806 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000807 if (*buf == ':')
808 buf++; // move past the ':'
809
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000810 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000811 b = e = -1;
812 q = text; // assume 1,$ for the range
813 r = end - 1;
814 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000815 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000816
817 // look for optional address(es) :. :1 :1,9 :'q,'a :%
818 buf = get_address(buf, &b, &e);
819
820 // remember orig command line
821 orig_buf = buf;
822
823 // get the COMMAND into cmd[]
824 buf1 = cmd;
825 while (*buf != '\0') {
826 if (isspace(*buf))
827 break;
828 *buf1++ = *buf++;
829 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000830 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000831 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000832 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000833 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000834 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000835 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000836 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000837 if (buf1) {
838 useforce = TRUE;
839 *buf1 = '\0'; // get rid of !
840 }
841 if (b >= 0) {
842 // if there is only one addr, then the addr
843 // is the line number of the single line the
844 // user wants. So, reset the end
845 // pointer to point at end of the "b" line
846 q = find_line(b); // what line is #b
847 r = end_line(q);
848 li = 1;
849 }
850 if (e >= 0) {
851 // we were given two addrs. change the
852 // end pointer to the addr given by user.
853 r = find_line(e); // what line is #e
854 r = end_line(r);
855 li = e - b + 1;
856 }
857 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000858 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000859 if (i == 0) { // :123CR goto line #123
860 if (b >= 0) {
861 dot = find_line(b); // what line is #b
862 dot_skip_over_ws();
863 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000864 }
865#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200866 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000867 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000868 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000869 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000870 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000871 retcode = system(orig_buf + 1); // run the cmd
872 if (retcode)
873 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000874 rawmode();
875 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000876 }
877#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200878 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000879 if (b < 0) { // no addr given- use defaults
880 b = e = count_lines(text, dot);
881 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000882 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200883 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000884 if (b < 0) { // no addr given- use defaults
885 q = begin_line(dot); // assume .,. for the range
886 r = end_line(dot);
887 }
888 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
889 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200890 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000891 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000892 if (file_modified && !useforce) {
893 status_line_bold("No write since last change (:edit! overrides)");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200894 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000895 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000896 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000897 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000898 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000899 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000900 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000901 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000902 } else {
903 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000904 status_line_bold("No current filename");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200905 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000906 }
907
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000908 if (init_text_buffer(fn) < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200909 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000910
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000911#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000912 if (Ureg >= 0 && Ureg < 28 && reg[Ureg] != 0) {
913 free(reg[Ureg]); // free orig line reg- for 'U'
914 reg[Ureg]= 0;
915 }
916 if (YDreg >= 0 && YDreg < 28 && reg[YDreg] != 0) {
917 free(reg[YDreg]); // free default yank/delete register
918 reg[YDreg]= 0;
919 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000920#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000921 // how many lines in text[]?
922 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000923 status_line("\"%s\"%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000924 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000925 " %dL, %dC", current_filename,
926 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000927 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000928 ((readonly_mode) ? " [Readonly]" : ""),
929 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000930 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200931 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000932 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +0100933 status_line_bold("No address allowed on this command");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200934 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000935 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000936 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000937 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000938 free(current_filename);
939 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000940 } else {
941 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +0000942 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000943 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200944 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000945 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000946 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000947 cookmode();
948 show_help();
949 rawmode();
950 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200951 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000952 if (b < 0) { // no addr given- use defaults
953 q = begin_line(dot); // assume .,. for the range
954 r = end_line(dot);
955 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000956 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000957 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000958 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000959 int c_is_no_print;
960
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000961 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +0000962 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000963 if (c_is_no_print) {
964 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000965 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000966 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000967 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000968 write1("$\r");
969 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000970 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000971 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000972 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000973 else
974 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000975 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000976 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000977 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000978 standout_end();
979 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000980 Hit_Return();
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200981 } else if (strncmp(cmd, "quit", i) == 0 // quit
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200982 || strncmp(cmd, "next", i) == 0 // edit next file
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000983 ) {
Denys Vlasenko04cecd52010-04-16 22:13:55 -0700984 int n;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000985 if (useforce) {
986 // force end of argv list
987 if (*cmd == 'q') {
988 optind = save_argc;
989 }
990 editing = 0;
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200991 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000992 }
993 // don't exit if the file been modified
994 if (file_modified) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000995 status_line_bold("No write since last change (:%s! overrides)",
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000996 (*cmd == 'q' ? "quit" : "next"));
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200997 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000998 }
999 // are there other file to edit
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001000 n = save_argc - optind - 1;
1001 if (*cmd == 'q' && n > 0) {
1002 status_line_bold("%d more file(s) to edit", n);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001003 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001004 }
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001005 if (*cmd == 'n' && n <= 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001006 status_line_bold("No more files to edit");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001007 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001008 }
1009 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001010 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001011 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001012 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001013 status_line_bold("No filename given");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001014 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001015 }
1016 if (b < 0) { // no addr given- use defaults
1017 q = begin_line(dot); // assume "dot"
1018 }
1019 // read after current line- unless user said ":0r foo"
1020 if (b != 0)
1021 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001022 { // dance around potentially-reallocated text[]
1023 uintptr_t ofs = q - text;
1024 ch = file_insert(fn, q, 0);
1025 q = text + ofs;
1026 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001027 if (ch < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001028 goto ret; // nothing was inserted
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001029 // how many lines in text[]?
1030 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001031 status_line("\"%s\""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001032 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001033 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001034 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001035 li, ch);
1036 if (ch > 0) {
1037 // if the insert is before "dot" then we need to update
1038 if (q <= dot)
1039 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001040 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001041 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001042 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001043 if (file_modified && !useforce) {
1044 status_line_bold("No write since last change (:rewind! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001045 } else {
1046 // reset the filenames to edit
1047 optind = fn_start - 1;
1048 editing = 0;
1049 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001050#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001051 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001052#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001053 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001054#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001055 i = 0; // offset into args
Denis Vlasenkof9234132007-03-21 00:03:42 +00001056 // only blank is regarded as args delmiter. What about tab '\t' ?
1057 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001058 // print out values of all options
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001059#if ENABLE_FEATURE_VI_SETOPTS
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001060 status_line_bold(
1061 "%sautoindent "
1062 "%sflash "
1063 "%signorecase "
1064 "%sshowmatch "
1065 "tabstop=%u",
1066 autoindent ? "" : "no",
1067 err_method ? "" : "no",
1068 ignorecase ? "" : "no",
1069 showmatch ? "" : "no",
1070 tabstop
1071 );
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001072#endif
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001073 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001074 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001075#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001076 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001077 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001078 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001079 i = 2; // ":set noautoindent"
1080 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001081 setops(argp, "flash " , i, "fl", VI_ERR_METHOD);
Denis Vlasenkof9234132007-03-21 00:03:42 +00001082 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001083 setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH );
1084 if (strncmp(argp + i, "tabstop=", 8) == 0) {
1085 int t = 0;
1086 sscanf(argp + i+8, "%u", &t);
1087 if (t > 0 && t <= MAX_TABSTOP)
1088 tabstop = t;
Denis Vlasenkof9234132007-03-21 00:03:42 +00001089 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001090 argp = skip_non_whitespace(argp);
1091 argp = skip_whitespace(argp);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001092 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001093#endif /* FEATURE_VI_SETOPTS */
1094#endif /* FEATURE_VI_SET */
1095#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001096 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001097 char *ls, *F, *R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001098 int gflag;
1099
1100 // F points to the "find" pattern
1101 // R points to the "replace" pattern
1102 // replace the cmd line delimiters "/" with NULLs
1103 gflag = 0; // global replace flag
1104 c = orig_buf[1]; // what is the delimiter
1105 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001106 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001107 if (!R)
1108 goto colon_s_fail;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001109 *R++ = '\0'; // terminate "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001110 buf1 = strchr(R, c);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001111 if (!buf1)
1112 goto colon_s_fail;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001113 *buf1++ = '\0'; // terminate "replace"
1114 if (*buf1 == 'g') { // :s/foo/bar/g
1115 buf1++;
1116 gflag++; // turn on gflag
1117 }
1118 q = begin_line(q);
1119 if (b < 0) { // maybe :s/foo/bar/
1120 q = begin_line(dot); // start with cur line
1121 b = count_lines(text, q); // cur line number
1122 }
1123 if (e < 0)
1124 e = b; // maybe :.s/foo/bar/
1125 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
1126 ls = q; // orig line start
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001127 vc4:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001128 buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001129 if (buf1) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001130 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001131 // we found the "find" pattern - delete it
1132 text_hole_delete(buf1, buf1 + strlen(F) - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001133 // inset the "replace" patern
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001134 bias = string_insert(buf1, R); // insert the string
1135 buf1 += bias;
1136 ls += bias;
1137 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001138 // check for "global" :s/foo/bar/g
1139 if (gflag == 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001140 if ((buf1 + strlen(R)) < end_line(ls)) {
1141 q = buf1 + strlen(R);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001142 goto vc4; // don't let q move past cur line
1143 }
1144 }
1145 }
1146 q = next_line(ls);
1147 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001148#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001149 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001150 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001151 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1152 || strncmp(cmd, "wq", i) == 0
1153 || strncmp(cmd, "wn", i) == 0
1154 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001155 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001156 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001157 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001158 fn = args;
1159 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001160#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001161 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001162 status_line_bold("\"%s\" File is read only", fn);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001163 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001164 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001165#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001166 // how many lines in text[]?
1167 li = count_lines(q, r);
1168 ch = r - q + 1;
1169 // see if file exists- if not, its just a new file request
1170 if (useforce) {
1171 // if "fn" is not write-able, chmod u+w
1172 // sprintf(syscmd, "chmod u+w %s", fn);
1173 // system(syscmd);
1174 forced = TRUE;
1175 }
1176 l = file_write(fn, q, r);
1177 if (useforce && forced) {
1178 // chmod u-w
1179 // sprintf(syscmd, "chmod u-w %s", fn);
1180 // system(syscmd);
1181 forced = FALSE;
1182 }
Paul Fox61e45db2005-10-09 14:43:22 +00001183 if (l < 0) {
1184 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001185 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001186 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001187 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001188 if (q == text && r == end - 1 && l == ch) {
1189 file_modified = 0;
1190 last_file_modified = -1;
1191 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001192 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1193 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1194 )
1195 && l == ch
1196 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001197 editing = 0;
1198 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001199 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001200#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001201 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001202 if (b < 0) { // no addr given- use defaults
1203 q = begin_line(dot); // assume .,. for the range
1204 r = end_line(dot);
1205 }
1206 text_yank(q, r, YDreg);
1207 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001208 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001209 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001210#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001211 } else {
1212 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001213 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001214 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001215 ret:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001216 dot = bound_dot(dot); // make sure "dot" is valid
1217 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001218#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001219 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001220 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001221#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001222}
Paul Fox61e45db2005-10-09 14:43:22 +00001223
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001224#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001225
1226static void Hit_Return(void)
1227{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001228 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001229
Denis Vlasenkof882f082007-12-23 02:36:51 +00001230 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001231 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001232 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001233 while ((c = get_one_char()) != '\n' && c != '\r')
1234 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001235 redraw(TRUE); // force redraw all
1236}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001237
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001238static int next_tabstop(int col)
1239{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001240 return col + ((tabstop - 1) - (col % tabstop));
1241}
1242
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001243//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001244static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001245{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001246 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001247 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001248 int cnt, ro, co;
1249
1250 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001251
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001252 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001253 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001254 // how many lines do we have to move
1255 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001256 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001257 screenbegin = beg_cur;
1258 if (cnt > (rows - 1) / 2) {
1259 // we moved too many lines. put "dot" in middle of screen
1260 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1261 screenbegin = prev_line(screenbegin);
1262 }
1263 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001264 } else {
1265 char *end_scr; // begin and end of screen
1266 end_scr = end_screen(); // last char of screen
1267 if (beg_cur > end_scr) {
1268 // "d" is after bottom line on screen
1269 // how many lines do we have to move
1270 cnt = count_lines(end_scr, beg_cur);
1271 if (cnt > (rows - 1) / 2)
1272 goto sc1; // too many lines
1273 for (ro = 0; ro < cnt - 1; ro++) {
1274 // move screen begin the same amount
1275 screenbegin = next_line(screenbegin);
1276 // now, move the end of screen
1277 end_scr = next_line(end_scr);
1278 end_scr = end_line(end_scr);
1279 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001280 }
1281 }
1282 // "d" is on screen- find out which row
1283 tp = screenbegin;
1284 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1285 if (tp == beg_cur)
1286 break;
1287 tp = next_line(tp);
1288 }
1289
1290 // find out what col "d" is on
1291 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001292 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001293 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001294 break;
1295 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001296 // handle tabs like real vi
1297 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001298 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001299 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001300 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001301 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1302 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001303 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001304 co++;
1305 tp++;
1306 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001307
1308 // "co" is the column where "dot" is.
1309 // The screen has "columns" columns.
1310 // The currently displayed columns are 0+offset -- columns+ofset
1311 // |-------------------------------------------------------------|
1312 // ^ ^ ^
1313 // offset | |------- columns ----------------|
1314 //
1315 // If "co" is already in this range then we do not have to adjust offset
1316 // but, we do have to subtract the "offset" bias from "co".
1317 // If "co" is outside this range then we have to change "offset".
1318 // If the first char of a line is a tab the cursor will try to stay
1319 // in column 7, but we have to set offset to 0.
1320
1321 if (co < 0 + offset) {
1322 offset = co;
1323 }
1324 if (co >= columns + offset) {
1325 offset = co - columns + 1;
1326 }
1327 // if the first char of the line is a tab, and "dot" is sitting on it
1328 // force offset to 0.
1329 if (d == beg_cur && *d == '\t') {
1330 offset = 0;
1331 }
1332 co -= offset;
1333
1334 *row = ro;
1335 *col = co;
1336}
1337
1338//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001339static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001340{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001341 if (p > text) {
1342 p = memrchr(text, '\n', p - text);
1343 if (!p)
1344 return text;
1345 return p + 1;
1346 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001347 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001348}
1349
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001350static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001351{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001352 if (p < end - 1) {
1353 p = memchr(p, '\n', end - p - 1);
1354 if (!p)
1355 return end - 1;
1356 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001357 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001358}
1359
Denis Vlasenkof882f082007-12-23 02:36:51 +00001360static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001361{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001362 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001363 // Try to stay off of the Newline
1364 if (*p == '\n' && (p - begin_line(p)) > 0)
1365 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001366 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001367}
1368
Denis Vlasenkof882f082007-12-23 02:36:51 +00001369static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001370{
1371 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001372 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001373 p--; // step to prev line
1374 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001375 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001376}
1377
Denis Vlasenkof882f082007-12-23 02:36:51 +00001378static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001379{
1380 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001381 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001382 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001383 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001384}
1385
1386//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001387static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001388{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001389 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001390 int cnt;
1391
1392 // find new bottom line
1393 q = screenbegin;
1394 for (cnt = 0; cnt < rows - 2; cnt++)
1395 q = next_line(q);
1396 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001397 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001398}
1399
Denis Vlasenkof882f082007-12-23 02:36:51 +00001400// count line from start to stop
1401static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001402{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001403 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001404 int cnt;
1405
Denis Vlasenkof882f082007-12-23 02:36:51 +00001406 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001407 q = start;
1408 start = stop;
1409 stop = q;
1410 }
1411 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001412 stop = end_line(stop);
1413 while (start <= stop && start <= end - 1) {
1414 start = end_line(start);
1415 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001416 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001417 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001418 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001419 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001420}
1421
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001422static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001423{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001424 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001425
1426 for (q = text; li > 1; li--) {
1427 q = next_line(q);
1428 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001429 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001430}
1431
1432//----- Dot Movement Routines ----------------------------------
1433static void dot_left(void)
1434{
1435 if (dot > text && dot[-1] != '\n')
1436 dot--;
1437}
1438
1439static void dot_right(void)
1440{
1441 if (dot < end - 1 && *dot != '\n')
1442 dot++;
1443}
1444
1445static void dot_begin(void)
1446{
1447 dot = begin_line(dot); // return pointer to first char cur line
1448}
1449
1450static void dot_end(void)
1451{
1452 dot = end_line(dot); // return pointer to last char cur line
1453}
1454
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001455static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001456{
1457 int co;
1458
1459 p = begin_line(p);
1460 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001461 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001462 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001463 break;
1464 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001465 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001466 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001467 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001468 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001469 co++;
1470 p++;
1471 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001472 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001473}
1474
1475static void dot_next(void)
1476{
1477 dot = next_line(dot);
1478}
1479
1480static void dot_prev(void)
1481{
1482 dot = prev_line(dot);
1483}
1484
1485static void dot_scroll(int cnt, int dir)
1486{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001487 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001488
1489 for (; cnt > 0; cnt--) {
1490 if (dir < 0) {
1491 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001492 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001493 screenbegin = prev_line(screenbegin);
1494 } else {
1495 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001496 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001497 screenbegin = next_line(screenbegin);
1498 }
1499 }
1500 // make sure "dot" stays on the screen so we dont scroll off
1501 if (dot < screenbegin)
1502 dot = screenbegin;
1503 q = end_screen(); // find new bottom line
1504 if (dot > q)
1505 dot = begin_line(q); // is dot is below bottom line?
1506 dot_skip_over_ws();
1507}
1508
1509static void dot_skip_over_ws(void)
1510{
1511 // skip WS
1512 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1513 dot++;
1514}
1515
1516static void dot_delete(void) // delete the char at 'dot'
1517{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001518 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001519}
1520
Denis Vlasenkof882f082007-12-23 02:36:51 +00001521static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001522{
1523 if (p >= end && end > text) {
1524 p = end - 1;
1525 indicate_error('1');
1526 }
1527 if (p < text) {
1528 p = text;
1529 indicate_error('2');
1530 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001531 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001532}
1533
1534//----- Helper Utility Routines --------------------------------
1535
1536//----------------------------------------------------------------
1537//----- Char Routines --------------------------------------------
1538/* Chars that are part of a word-
1539 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1540 * Chars that are Not part of a word (stoppers)
1541 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1542 * Chars that are WhiteSpace
1543 * TAB NEWLINE VT FF RETURN SPACE
1544 * DO NOT COUNT NEWLINE AS WHITESPACE
1545 */
1546
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001547static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001548{
1549 int li;
1550
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001551 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001552 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001553 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001554 // initialize the new screen. assume this will be a empty file.
1555 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001556 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001557 for (li = 1; li < ro - 1; li++) {
1558 screen[(li * co) + 0] = '~';
1559 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001560 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001561}
1562
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001563#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko33875382008-06-21 20:31:50 +00001564static int mycmp(const char *s1, const char *s2, int len)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001565{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001566 if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001567 return strncasecmp(s1, s2, len);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001568 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001569 return strncmp(s1, s2, len);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001570}
1571
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001572// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001573static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001574{
1575#ifndef REGEX_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001576 char *start, *stop;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001577 int len;
1578
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001579 len = strlen(pat);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001580 if (dir == FORWARD) {
1581 stop = end - 1; // assume range is p - end-1
1582 if (range == LIMITED)
1583 stop = next_line(p); // range is to next line
1584 for (start = p; start < stop; start++) {
1585 if (mycmp(start, pat, len) == 0) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001586 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001587 }
1588 }
1589 } else if (dir == BACK) {
1590 stop = text; // assume range is text - p
1591 if (range == LIMITED)
1592 stop = prev_line(p); // range is to prev line
1593 for (start = p - len; start >= stop; start--) {
1594 if (mycmp(start, pat, len) == 0) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001595 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001596 }
1597 }
1598 }
1599 // pattern not found
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001600 return NULL;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001601#else /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001602 char *q;
1603 struct re_pattern_buffer preg;
1604 int i;
1605 int size, range;
1606
1607 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1608 preg.translate = 0;
1609 preg.fastmap = 0;
1610 preg.buffer = 0;
1611 preg.allocated = 0;
1612
1613 // assume a LIMITED forward search
1614 q = next_line(p);
1615 q = end_line(q);
1616 q = end - 1;
1617 if (dir == BACK) {
1618 q = prev_line(p);
1619 q = text;
1620 }
1621 // count the number of chars to search over, forward or backward
1622 size = q - p;
1623 if (size < 0)
1624 size = p - q;
1625 // RANGE could be negative if we are searching backwards
1626 range = q - p;
1627
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001628 q = re_compile_pattern(pat, strlen(pat), &preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001629 if (q != 0) {
1630 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001631 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001632 i = 0; // return p if pattern not compiled
1633 goto cs1;
1634 }
1635
1636 q = p;
1637 if (range < 0) {
1638 q = p - size;
1639 if (q < text)
1640 q = text;
1641 }
1642 // search for the compiled pattern, preg, in p[]
1643 // range < 0- search backward
1644 // range > 0- search forward
1645 // 0 < start < size
1646 // re_search() < 0 not found or error
1647 // re_search() > 0 index of found pattern
1648 // struct pattern char int int int struct reg
1649 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1650 i = re_search(&preg, q, size, 0, range, 0);
1651 if (i == -1) {
1652 p = 0;
1653 i = 0; // return NULL if pattern not found
1654 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001655 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001656 if (dir == FORWARD) {
1657 p = p + i;
1658 } else {
1659 p = p - i;
1660 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001661 return p;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001662#endif /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001663}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001664#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001665
Denis Vlasenko33875382008-06-21 20:31:50 +00001666static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001667{
1668 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001669 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001670 refresh(FALSE); // show the ^
1671 c = get_one_char();
1672 *p = c;
1673 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001674 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001675 } else if (c == 27) { // Is this an ESC?
1676 cmd_mode = 0;
1677 cmdcnt = 0;
1678 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001679 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001680 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001681 p--;
1682 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001683 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001684 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001685 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001686 p--;
1687 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001688 }
1689 } else {
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001690#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001691 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001692 char *sp; // "save p"
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001693#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001694
1695 if (c == 13)
1696 c = '\n'; // translate \r to \n
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001697#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001698 sp = p; // remember addr of insert
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001699#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001700 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001701#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001702 if (showmatch && strchr(")]}", *sp) != NULL) {
1703 showmatching(sp);
1704 }
1705 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001706 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001707 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001708 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001709 len = strspn(q, " \t"); // space or tab
1710 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001711 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001712 bias = text_hole_make(p, len);
1713 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001714 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001715 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001716 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001717 }
1718 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001719#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001720 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001721 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001722}
1723
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001724// might reallocate text[]! use p += stupid_insert(p, ...),
1725// and be careful to not use pointers into potentially freed text[]!
1726static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001727{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001728 uintptr_t bias;
1729 bias = text_hole_make(p, 1);
1730 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001731 *p = c;
1732 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001733 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001734}
1735
Denis Vlasenko33875382008-06-21 20:31:50 +00001736static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001737{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001738 char *save_dot, *p, *q, *t;
1739 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001740
1741 save_dot = dot;
1742 p = q = dot;
1743
1744 if (strchr("cdy><", c)) {
1745 // these cmds operate on whole lines
1746 p = q = begin_line(p);
1747 for (cnt = 1; cnt < cmdcnt; cnt++) {
1748 q = next_line(q);
1749 }
1750 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001751 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001752 // These cmds operate on char positions
1753 do_cmd(c); // execute movement cmd
1754 q = dot;
1755 } else if (strchr("wW", c)) {
1756 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001757 // if we are at the next word's first char
1758 // step back one char
1759 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001760 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001761 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1762 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1763 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001764 if (dot > text && *dot == '\n')
1765 dot--; // stay off NL
1766 q = dot;
1767 } else if (strchr("H-k{", c)) {
1768 // these operate on multi-lines backwards
1769 q = end_line(dot); // find NL
1770 do_cmd(c); // execute movement cmd
1771 dot_begin();
1772 p = dot;
1773 } else if (strchr("L+j}\r\n", c)) {
1774 // these operate on multi-lines forwards
1775 p = begin_line(dot);
1776 do_cmd(c); // execute movement cmd
1777 dot_end(); // find NL
1778 q = dot;
1779 } else {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001780 // nothing -- this causes any other values of c to
1781 // represent the one-character range under the
1782 // cursor. this is correct for ' ' and 'l', but
1783 // perhaps no others.
1784 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001785 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001786 if (q < p) {
1787 t = q;
1788 q = p;
1789 p = t;
1790 }
1791
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001792 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001793 if (q > p && strchr("^0bBh\b\177", c)) q--;
1794
1795 multiline = 0;
1796 for (t = p; t <= q; t++) {
1797 if (*t == '\n') {
1798 multiline = 1;
1799 break;
1800 }
1801 }
1802
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001803 *start = p;
1804 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001805 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001806 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001807}
1808
Denis Vlasenko33875382008-06-21 20:31:50 +00001809static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001810{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001811 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001812 int test, inc;
1813
1814 inc = dir;
1815 c = c0 = p[0];
1816 ci = p[inc];
1817 test = 0;
1818
1819 if (type == S_BEFORE_WS) {
1820 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001821 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001822 }
1823 if (type == S_TO_WS) {
1824 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001825 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001826 }
1827 if (type == S_OVER_WS) {
1828 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001829 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001830 }
1831 if (type == S_END_PUNCT) {
1832 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001833 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001834 }
1835 if (type == S_END_ALNUM) {
1836 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001837 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001838 }
1839 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001840 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001841}
1842
Denis Vlasenko33875382008-06-21 20:31:50 +00001843static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001844{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001845 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001846
1847 while (st_test(p, type, dir, &c)) {
1848 // make sure we limit search to correct number of lines
1849 if (c == '\n' && --linecnt < 1)
1850 break;
1851 if (dir >= 0 && p >= end - 1)
1852 break;
1853 if (dir < 0 && p <= text)
1854 break;
1855 p += dir; // move to next char
1856 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001857 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001858}
1859
1860// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00001861static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001862{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001863 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001864 int dir, level;
1865
1866 match = ')';
1867 level = 1;
1868 dir = 1; // assume forward
1869 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001870 case '(': match = ')'; break;
1871 case '[': match = ']'; break;
1872 case '{': match = '}'; break;
1873 case ')': match = '('; dir = -1; break;
1874 case ']': match = '['; dir = -1; break;
1875 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001876 }
1877 for (q = p + dir; text <= q && q < end; q += dir) {
1878 // look for match, count levels of pairs (( ))
1879 if (*q == c)
1880 level++; // increase pair levels
1881 if (*q == match)
1882 level--; // reduce pair level
1883 if (level == 0)
1884 break; // found matching pair
1885 }
1886 if (level != 0)
1887 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001888 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001889}
1890
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001891#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001892// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00001893static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001894{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001895 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001896
1897 // we found half of a pair
1898 q = find_pair(p, *p); // get loc of matching char
1899 if (q == NULL) {
1900 indicate_error('3'); // no matching char
1901 } else {
1902 // "q" now points to matching pair
1903 save_dot = dot; // remember where we are
1904 dot = q; // go to new loc
1905 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001906 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001907 dot = save_dot; // go back to old loc
1908 refresh(FALSE);
1909 }
1910}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001911#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001912
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001913// open a hole in text[]
1914// might reallocate text[]! use p += text_hole_make(p, ...),
1915// and be careful to not use pointers into potentially freed text[]!
1916static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001917{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001918 uintptr_t bias = 0;
1919
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001920 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001921 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001922 end += size; // adjust the new END
1923 if (end >= (text + text_size)) {
1924 char *new_text;
1925 text_size += end - (text + text_size) + 10240;
1926 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001927 bias = (new_text - text);
1928 screenbegin += bias;
1929 dot += bias;
1930 end += bias;
1931 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001932 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001933 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00001934 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001935 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00001936 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001937 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001938}
1939
1940// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00001941static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001942{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001943 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001944 int cnt, hole_size;
1945
1946 // move forwards, from beginning
1947 // assume p <= q
1948 src = q + 1;
1949 dest = p;
1950 if (q < p) { // they are backward- swap them
1951 src = p + 1;
1952 dest = q;
1953 }
1954 hole_size = q - p + 1;
1955 cnt = end - src;
1956 if (src < text || src > end)
1957 goto thd0;
1958 if (dest < text || dest >= end)
1959 goto thd0;
1960 if (src >= end)
1961 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00001962 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001963 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001964 end = end - hole_size; // adjust the new END
1965 if (dest >= end)
1966 dest = end - 1; // make sure dest in below end-1
1967 if (end <= text)
1968 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00001969 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001970 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001971 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001972}
1973
1974// copy text into register, then delete text.
1975// if dist <= 0, do not include, or go past, a NewLine
1976//
Denis Vlasenko33875382008-06-21 20:31:50 +00001977static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001978{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001979 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001980
1981 // make sure start <= stop
1982 if (start > stop) {
1983 // they are backwards, reverse them
1984 p = start;
1985 start = stop;
1986 stop = p;
1987 }
1988 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001989 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001990 p = start;
1991 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001992 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001993 // dont go past a NewLine
1994 for (; p + 1 <= stop; p++) {
1995 if (p[1] == '\n') {
1996 stop = p; // "stop" just before NewLine
1997 break;
1998 }
1999 }
2000 }
2001 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002002#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002003 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002004#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002005 if (yf == YANKDEL) {
2006 p = text_hole_delete(start, stop);
2007 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002008 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002009}
2010
2011static void show_help(void)
2012{
2013 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002014#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002015 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002016#endif
2017#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002018 "\n\tLast command repeat with \'.\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002019#endif
2020#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00002021 "\n\tLine marking with 'x"
2022 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002023#endif
2024#if ENABLE_FEATURE_VI_READONLY
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002025 "\n\tReadonly if vi is called as \"view\""
2026 "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002027#endif
2028#if ENABLE_FEATURE_VI_SET
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002029 "\n\tSome colon mode commands with \':\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002030#endif
2031#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002032 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002033#endif
2034#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002035 "\n\tSignal catching- ^C"
2036 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002037#endif
2038#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002039 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002040#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002041 );
2042}
2043
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002044#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002045static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002046{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002047 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002048 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002049 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002050 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002051 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002052 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002053 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002054 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002055 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002056}
2057
2058static void end_cmd_q(void)
2059{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002060#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002061 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002062#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002063 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002064}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002065#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002066
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002067#if ENABLE_FEATURE_VI_YANKMARK \
2068 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2069 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002070// might reallocate text[]! use p += string_insert(p, ...),
2071// and be careful to not use pointers into potentially freed text[]!
2072static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002073{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002074 uintptr_t bias;
2075 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002076
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002077 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002078 bias = text_hole_make(p, i);
2079 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002080 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002081#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002082 {
2083 int cnt;
2084 for (cnt = 0; *s != '\0'; s++) {
2085 if (*s == '\n')
2086 cnt++;
2087 }
2088 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2089 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002090#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002091 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002092}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002093#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002094
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002095#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002096static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002097{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002098 int cnt = q - p;
2099 if (cnt < 0) { // they are backwards- reverse them
2100 p = q;
2101 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002102 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002103 free(reg[dest]); // if already a yank register, free it
2104 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002105 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002106}
2107
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002108static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002109{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002110 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002111
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002112 c = 'D'; // default to D-reg
2113 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002114 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002115 if (YDreg == 26)
2116 c = 'D';
2117 if (YDreg == 27)
2118 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002119 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002120}
2121
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002122static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002123{
2124 // A context is defined to be "modifying text"
2125 // Any modifying command establishes a new context.
2126
2127 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002128 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002129 // we are trying to modify text[]- make this the current context
2130 mark[27] = mark[26]; // move cur to prev
2131 mark[26] = dot; // move local to cur
2132 context_start = prev_line(prev_line(dot));
2133 context_end = next_line(next_line(dot));
2134 //loiter= start_loiter= now;
2135 }
2136 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002137}
2138
Denis Vlasenkof882f082007-12-23 02:36:51 +00002139static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002140{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002141 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002142
2143 // the current context is in mark[26]
2144 // the previous context is in mark[27]
2145 // only swap context if other context is valid
2146 if (text <= mark[27] && mark[27] <= end - 1) {
2147 tmp = mark[27];
2148 mark[27] = mark[26];
2149 mark[26] = tmp;
2150 p = mark[26]; // where we are going- previous context
2151 context_start = prev_line(prev_line(prev_line(p)));
2152 context_end = next_line(next_line(next_line(p)));
2153 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002154 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002155}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002156#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002157
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002158//----- Set terminal attributes --------------------------------
2159static void rawmode(void)
2160{
2161 tcgetattr(0, &term_orig);
2162 term_vi = term_orig;
2163 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
2164 term_vi.c_iflag &= (~IXON & ~ICRNL);
2165 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002166 term_vi.c_cc[VMIN] = 1;
2167 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002168 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002169 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002170}
2171
2172static void cookmode(void)
2173{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002174 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002175 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002176}
2177
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002178#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002179//----- Come here when we get a window resize signal ---------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002180static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002181{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002182 int save_errno = errno;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002183 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002184 signal(SIGWINCH, winch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002185 query_screen_dimensions();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002186 new_screen(rows, columns); // get memory for virtual screen
2187 redraw(TRUE); // re-draw the screen
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002188 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002189}
2190
2191//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002192static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002193{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002194 int save_errno = errno;
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002195 rawmode(); // terminal to "raw"
2196 last_status_cksum = 0; // force status update
2197 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002198
2199 signal(SIGTSTP, suspend_sig);
2200 signal(SIGCONT, SIG_DFL);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002201 //kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
2202 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002203}
2204
2205//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002206static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002207{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002208 int save_errno = errno;
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002209 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002210 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002211
2212 signal(SIGCONT, cont_sig);
2213 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002214 kill(my_pid, SIGTSTP);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002215 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002216}
2217
2218//----- Come here when we get a signal ---------------------------
2219static void catch_sig(int sig)
2220{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002221 signal(SIGINT, catch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002222 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002223}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002224#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002225
Denys Vlasenko606291b2009-09-23 23:15:43 +02002226static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002227{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002228 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002229
Denys Vlasenko606291b2009-09-23 23:15:43 +02002230 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002231 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002232 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002233}
2234
2235//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002236static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002237{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002238 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002239
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002240 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002241 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002242 if (c == -1) { // EOF/error
2243 go_bottom_and_clear_to_eol();
2244 cookmode(); // terminal to "cooked"
2245 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002246 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002247 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002248}
2249
2250//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002251static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002252{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002253 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002254
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002255#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002256 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
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002264 // careful with correct sign expansion!
2265 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002266 if (c == '\0') {
2267 // the end of the q, read from STDIN
2268 free(ioq_start);
2269 ioq_start = ioq = 0;
2270 c = readit(); // get the users input
2271 }
2272 }
2273 } else {
2274 // adding STDIN chars to q
2275 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002276 if (lmc_len >= MAX_INPUT_LEN - 1) {
2277 status_line_bold("last_modifying_cmd overrun");
2278 } else {
2279 // add new char to q
2280 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002281 }
2282 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002283#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002284 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002285#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002286 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002287}
2288
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002289// Get input line (uses "status line" area)
2290static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002291{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002292 // char [MAX_INPUT_LEN]
2293#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002294
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002295 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002296 int i;
2297
2298 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002299 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002300 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002301 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002302
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002303 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002304 while (i < MAX_INPUT_LEN) {
2305 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002306 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002307 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002308 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002309 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002310 buf[--i] = '\0';
2311 write1("\b \b"); // erase char on screen
2312 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002313 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002314 } else if (c > 0 && c < 256) { // exclude Unicode
2315 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002316 buf[i] = c;
2317 buf[++i] = '\0';
2318 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002319 }
2320 }
2321 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002322 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002323#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002324}
2325
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002326static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002327{
2328 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002329 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002330
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002331 cnt = -1;
Denys Vlasenkodef47832010-04-18 20:39:41 -07002332 if (fn && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002333 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002334 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002335}
2336
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002337// might reallocate text[]!
2338static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002339{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002340 int cnt = -1;
2341 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002342 struct stat statbuf;
2343
2344 /* Validate file */
2345 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002346 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002347 goto fi0;
2348 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002349 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002350 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002351 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002352 goto fi0;
2353 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002354 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002355 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002356 goto fi0;
2357 }
2358
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002359 // read file to buffer
2360 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002361 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002362 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002363 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002364 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002365 size = statbuf.st_size;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002366 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002367 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002368 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002369 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002370 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002371 } else if (cnt < size) {
2372 // There was a partial read, shrink unused space text[]
2373 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denys Vlasenko6331cf02009-11-13 09:08:27 +01002374 status_line_bold("can't read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002375 }
2376 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002377 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002378 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002379 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002380#if ENABLE_FEATURE_VI_READONLY
2381 if (update_ro_status
2382 && ((access(fn, W_OK) < 0) ||
2383 /* root will always have access()
2384 * so we check fileperms too */
2385 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2386 )
2387 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002388 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002389 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002390#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002391 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002392}
2393
Denis Vlasenko33875382008-06-21 20:31:50 +00002394static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002395{
2396 int fd, cnt, charcnt;
2397
2398 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002399 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002400 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002401 }
Denis Vlasenko8abae882008-05-03 11:35:59 +00002402 /* By popular request we do not open file with O_TRUNC,
2403 * but instead ftruncate() it _after_ successful write.
2404 * Might reduce amount of data lost on power fail etc.
2405 */
2406 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002407 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002408 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002409 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002410 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002411 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002412 if (charcnt == cnt) {
2413 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002414 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002415 } else {
2416 charcnt = 0;
2417 }
2418 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002419 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002420}
2421
2422//----- Terminal Drawing ---------------------------------------
2423// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002424// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002425// screen coordinates
2426// 0,0 ... 0,79
2427// 1,0 ... 1,79
2428// . ... .
2429// . ... .
2430// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002431// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002432
2433//----- Move the cursor to row x col (count from 0, not 1) -------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002434static void place_cursor(int row, int col, int optimize)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002435{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002436 char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
Denis Vlasenko7b54dc72008-07-17 21:32:32 +00002437#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2438 enum {
2439 SZ_UP = sizeof(CMup),
2440 SZ_DN = sizeof(CMdown),
2441 SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN,
2442 };
2443 char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size
2444#endif
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 Vlasenko26b6fba2007-12-21 21:34:37 +00002458 char *screenp;
2459 int Rrow = last_row;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002460 int diff = Rrow - row;
2461
2462 if (diff < -5 || diff > 5)
2463 goto skip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002464
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002465 //----- find the minimum # of chars to move cursor -------------
2466 //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
2467 cm2[0] = '\0';
2468
2469 // move to the correct row
2470 while (row < Rrow) {
2471 // the cursor has to move up
2472 strcat(cm2, CMup);
2473 Rrow--;
2474 }
2475 while (row > Rrow) {
2476 // the cursor has to move down
2477 strcat(cm2, CMdown);
2478 Rrow++;
2479 }
2480
2481 // now move to the correct column
2482 strcat(cm2, "\r"); // start at col 0
2483 // just send out orignal source char to get to correct place
2484 screenp = &screen[row * columns]; // start of screen line
2485 strncat(cm2, screenp, col);
2486
2487 // pick the shortest cursor motion to send out
2488 if (strlen(cm2) < strlen(cm)) {
2489 cm = cm2;
2490 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002491 skip: ;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002492 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002493 last_row = row;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002494#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002495 write1(cm);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002496}
2497
2498//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002499static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002500{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002501 write1(Ceol); // Erase from cursor to end of line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002502}
2503
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002504static void go_bottom_and_clear_to_eol(void)
2505{
2506 place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
2507 clear_to_eol(); // erase to end of line
2508}
2509
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002510//----- 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
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002581 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002582 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002583 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002584 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002585 (columns - 1) ) {
2586 have_status_msg = 0;
2587 Hit_Return();
2588 }
2589 have_status_msg = 0;
2590 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002591 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2592 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002593 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002594}
2595
2596//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002597// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002598static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002599{
2600 va_list args;
2601
2602 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002603 strcpy(status_buffer, SOs); // Terminal standout mode on
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002604 vsprintf(status_buffer + sizeof(SOs)-1, format, args);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002605 strcat(status_buffer, SOn); // Terminal standout mode off
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002606 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002607
2608 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002609}
2610
Paul Fox8552aec2005-09-16 12:20:05 +00002611// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002612static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002613{
2614 va_list args;
2615
2616 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002617 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002618 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002619
2620 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002621}
2622
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002623// copy s to buf, convert unprintable
2624static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002625{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002626 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002627 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002628
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002629 buf[0] = '\0';
2630 if (!s[0])
2631 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002632
2633 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002634 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) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002640 strcpy(d, SOn);
2641 d += sizeof(SOn)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002642 c = '.';
2643 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002644 if (c < ' ' || c == 0x7f) {
2645 *d++ = '^';
2646 c |= '@'; /* 0x40 */
2647 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002648 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002649 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002650 *d++ = c;
2651 *d = '\0';
2652 if (c_is_no_print) {
2653 strcpy(d, SOs);
2654 d += sizeof(SOs)-1;
2655 }
2656 if (*s == '\n') {
2657 *d++ = '$';
2658 *d = '\0';
2659 }
2660 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002661 break;
2662 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002663}
2664
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002665static void not_implemented(const char *s)
2666{
2667 char buf[MAX_INPUT_LEN];
2668
2669 print_literal(buf, s);
2670 status_line_bold("\'%s\' is not implemented", buf);
2671}
2672
2673// show file status on status line
2674static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002675{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002676 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002677
2678#define tot format_edit_status__tot
2679
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002680 int cur, percent, ret, trunc_at;
2681
Paul Fox8552aec2005-09-16 12:20:05 +00002682 // file_modified is now a counter rather than a flag. this
2683 // helps reduce the amount of line counting we need to do.
2684 // (this will cause a mis-reporting of modified status
2685 // once every MAXINT editing operations.)
2686
2687 // it would be nice to do a similar optimization here -- if
2688 // we haven't done a motion that could have changed which line
2689 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002690 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002691
2692 // reduce counting -- the total lines can't have
2693 // changed if we haven't done any edits.
2694 if (file_modified != last_file_modified) {
2695 tot = cur + count_lines(dot, end - 1) - 1;
2696 last_file_modified = file_modified;
2697 }
2698
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002699 // current line percent
2700 // ------------- ~~ ----------
2701 // total lines 100
2702 if (tot > 0) {
2703 percent = (100 * cur) / tot;
2704 } else {
2705 cur = tot = 0;
2706 percent = 100;
2707 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002708
Paul Fox8552aec2005-09-16 12:20:05 +00002709 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2710 columns : STATUS_BUFFER_LEN-1;
2711
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002712 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002713#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002714 "%c %s%s%s %d/%d %d%%",
2715#else
2716 "%c %s%s %d/%d %d%%",
2717#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002718 cmd_mode_indicator[cmd_mode & 3],
2719 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002720#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002721 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002722#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002723 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002724 cur, tot, percent);
2725
2726 if (ret >= 0 && ret < trunc_at)
2727 return ret; /* it all fit */
2728
2729 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002730#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002731}
2732
2733//----- Force refresh of all Lines -----------------------------
2734static void redraw(int full_screen)
2735{
2736 place_cursor(0, 0, FALSE); // put cursor in correct place
Denis Vlasenkob1759462008-06-20 20:20:54 +00002737 clear_to_eos(); // tell terminal to erase display
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002738 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002739 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002740 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002741 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002742}
2743
2744//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002745static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002746{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002747 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002748 int co;
2749 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002750 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002751
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002752 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002753 co = 0;
2754 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002755 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002756 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002757 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002758 if (c == '\n')
2759 break;
2760 if ((c & 0x80) && !Isprint(c)) {
2761 c = '.';
2762 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002763 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002764 if (c == '\t') {
2765 c = ' ';
2766 // co % 8 != 7
2767 while ((co % tabstop) != (tabstop - 1)) {
2768 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002769 }
2770 } else {
2771 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002772 if (c == 0x7f)
2773 c = '?';
2774 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002775 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002776 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002777 }
2778 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002779 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002780 // discard scrolled-off-to-the-left portion,
2781 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002782 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002783 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002784 co -= tabstop;
2785 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002786 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002787 if (src >= end)
2788 break;
2789 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002790 // check "short line, gigantic offset" case
2791 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002792 ofs = co;
2793 // discard last scrolled off part
2794 co -= ofs;
2795 dest += ofs;
2796 // fill the rest with spaces
2797 if (co < columns)
2798 memset(&dest[co], ' ', columns - co);
2799 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002800}
2801
2802//----- Refresh the changed screen lines -----------------------
2803// Copy the source line from text[] into the buffer and note
2804// if the current screenline is different from the new buffer.
2805// If they differ then that line needs redrawing on the terminal.
2806//
2807static void refresh(int full_screen)
2808{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002809#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002810
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002811 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002812 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002813
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +02002814 if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002815 unsigned c = columns, r = rows;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002816 query_screen_dimensions();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002817 full_screen |= (c - columns) | (r - rows);
2818 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002819 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2820 tp = screenbegin; // index into text[] of top line
2821
2822 // compare text[] to screen[] and mark screen[] lines that need updating
2823 for (li = 0; li < rows - 1; li++) {
2824 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002825 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002826 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002827 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002828
2829 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002830 if (tp < end) {
2831 char *t = memchr(tp, '\n', end - tp);
2832 if (!t) t = end - 1;
2833 tp = t + 1;
2834 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002835
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002836 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002837 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002838 cs = 0;
2839 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002840 sp = &screen[li * columns]; // start of screen line
2841 if (full_screen) {
2842 // force re-draw of every single column from 0 - columns-1
2843 goto re0;
2844 }
2845 // compare newly formatted buffer with virtual screen
2846 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002847 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002848 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002849 changed = TRUE; // mark for redraw
2850 break;
2851 }
2852 }
2853
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002854 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002855 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002856 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002857 changed = TRUE; // mark for redraw
2858 break;
2859 }
2860 }
2861 // now, cs is index of first diff, and ce is index of last diff
2862
2863 // if horz offset has changed, force a redraw
2864 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002865 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002866 changed = TRUE;
2867 }
2868
2869 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002870 if (cs < 0) cs = 0;
2871 if (ce > columns - 1) ce = columns - 1;
2872 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002873 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002874 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002875 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002876 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002877
2878 // move cursor to column of first change
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002879 //if (offset != old_offset) {
2880 // // place_cursor is still too stupid
2881 // // to handle offsets correctly
2882 // place_cursor(li, cs, FALSE);
2883 //} else {
2884 place_cursor(li, cs, TRUE);
2885 //}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002886
2887 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002888 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002889 }
2890 }
2891
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002892 place_cursor(crow, ccol, TRUE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002893
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002894 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002895#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002896}
2897
Eric Andersen3f980402001-04-04 17:31:15 +00002898//---------------------------------------------------------------------
2899//----- the Ascii Chart -----------------------------------------------
2900//
2901// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
2902// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
2903// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
2904// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
2905// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
2906// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
2907// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
2908// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
2909// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
2910// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
2911// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
2912// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
2913// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
2914// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
2915// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
2916// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
2917//---------------------------------------------------------------------
2918
2919//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002920static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00002921{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002922 const char *msg = msg; // for compiler
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002923 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002924 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00002925 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002926 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002927 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00002928
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002929// c1 = c; // quiet the compiler
2930// cnt = yf = 0; // quiet the compiler
2931// msg = p = q = save_dot = buf; // quiet the compiler
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002932 memset(buf, '\0', 12);
Eric Andersenbff7a602001-11-17 07:15:43 +00002933
Paul Fox8552aec2005-09-16 12:20:05 +00002934 show_status_line();
2935
Eric Andersenbff7a602001-11-17 07:15:43 +00002936 /* if this is a cursor key, skip these checks */
2937 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002938 case KEYCODE_UP:
2939 case KEYCODE_DOWN:
2940 case KEYCODE_LEFT:
2941 case KEYCODE_RIGHT:
2942 case KEYCODE_HOME:
2943 case KEYCODE_END:
2944 case KEYCODE_PAGEUP:
2945 case KEYCODE_PAGEDOWN:
2946 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00002947 goto key_cmd_mode;
2948 }
2949
Eric Andersen3f980402001-04-04 17:31:15 +00002950 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002951 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002952 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00002953 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00002954 // we are 'R'eplacing the current *dot with new char
2955 if (*dot == '\n') {
2956 // don't Replace past E-o-l
2957 cmd_mode = 1; // convert to insert
2958 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002959 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00002960 if (c != 27)
2961 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
2962 dot = char_insert(dot, c); // insert new char
2963 }
2964 goto dc1;
2965 }
2966 }
2967 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00002968 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002969 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00002970 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002971 if (1 <= c || Isprint(c)) {
2972 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00002973 }
2974 goto dc1;
2975 }
2976
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002977 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00002978 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00002979 //case 0x01: // soh
2980 //case 0x09: // ht
2981 //case 0x0b: // vt
2982 //case 0x0e: // so
2983 //case 0x0f: // si
2984 //case 0x10: // dle
2985 //case 0x11: // dc1
2986 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002987#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00002988 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00002989 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00002990 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002991#endif
Eric Andersen822c3832001-05-07 17:37:43 +00002992 //case 0x16: // syn
2993 //case 0x17: // etb
2994 //case 0x18: // can
2995 //case 0x1c: // fs
2996 //case 0x1d: // gs
2997 //case 0x1e: // rs
2998 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002999 //case '!': // !-
3000 //case '#': // #-
3001 //case '&': // &-
3002 //case '(': // (-
3003 //case ')': // )-
3004 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003005 //case '=': // =-
3006 //case '@': // @-
3007 //case 'F': // F-
3008 //case 'K': // K-
3009 //case 'Q': // Q-
3010 //case 'S': // S-
3011 //case 'T': // T-
3012 //case 'V': // V-
3013 //case '[': // [-
3014 //case '\\': // \-
3015 //case ']': // ]-
3016 //case '_': // _-
3017 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003018 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003019 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003020 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00003021 buf[0] = c;
3022 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003023 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003024 end_cmd_q(); // stop adding to q
3025 case 0x00: // nul- ignore
3026 break;
3027 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003028 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00003029 dot_scroll(rows - 2, -1);
3030 break;
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
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003038 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003039 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
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003045 case KEYCODE_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)
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003048 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003049 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003050 }
Eric Andersen3f980402001-04-04 17:31:15 +00003051 dot_left();
3052 break;
3053 case 10: // Newline ^J
3054 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003055 case KEYCODE_DOWN: // cursor key Down
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003056 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003057 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003058 }
Eric Andersen3f980402001-04-04 17:31:15 +00003059 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
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003073 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003074 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003075 }
Eric Andersen3f980402001-04-04 17:31:15 +00003076 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
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003094 case KEYCODE_RIGHT: // Cursor Key Right
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003095 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003096 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003097 }
Eric Andersen3f980402001-04-04 17:31:15 +00003098 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
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003102 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3103 if ((unsigned)c1 <= 25) { // a-z?
3104 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003105 } else {
3106 indicate_error(c);
3107 }
3108 break;
3109 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003110 c1 = (get_one_char() | 0x20) - 'a';
3111 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003112 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003113 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003114 if (text <= q && q < end) {
3115 dot = q;
3116 dot_begin(); // go to B-o-l
3117 dot_skip_over_ws();
3118 }
3119 } else if (c1 == '\'') { // goto previous context
3120 dot = swap_context(dot); // swap current and previous context
3121 dot_begin(); // go to B-o-l
3122 dot_skip_over_ws();
3123 } else {
3124 indicate_error(c);
3125 }
3126 break;
3127 case 'm': // m- Mark a line
3128 // this is really stupid. If there are any inserts or deletes
3129 // between text[0] and dot then this mark will not point to the
3130 // correct location! It could be off by many lines!
3131 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003132 c1 = (get_one_char() | 0x20) - 'a';
3133 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003134 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003135 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003136 } else {
3137 indicate_error(c);
3138 }
3139 break;
3140 case 'P': // P- Put register before
3141 case 'p': // p- put register after
3142 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003143 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003144 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003145 break;
3146 }
3147 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003148 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003149 if (c == 'P') {
3150 dot_begin(); // putting lines- Put above
3151 }
3152 if (c == 'p') {
3153 // are we putting after very last line?
3154 if (end_line(dot) == (end - 1)) {
3155 dot = end; // force dot to end of text[]
3156 } else {
3157 dot_next(); // next line, then put before
3158 }
3159 }
3160 } else {
3161 if (c == 'p')
3162 dot_right(); // move to right, can move to NL
3163 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003164 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003165 end_cmd_q(); // stop adding to q
3166 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003167 case 'U': // U- Undo; replace current line with original version
3168 if (reg[Ureg] != 0) {
3169 p = begin_line(dot);
3170 q = end_line(dot);
3171 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003172 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003173 dot = p;
3174 dot_skip_over_ws();
3175 }
3176 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003177#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003178 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003179 case KEYCODE_END: // Cursor Key End
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003180 if (--cmdcnt > 0) {
3181 dot_next();
Eric Andersen3f980402001-04-04 17:31:15 +00003182 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003183 }
Glenn L McGrathee829062004-01-21 10:59:45 +00003184 dot = end_line(dot);
Eric Andersen3f980402001-04-04 17:31:15 +00003185 break;
3186 case '%': // %- find matching char of pair () [] {}
3187 for (q = dot; q < end && *q != '\n'; q++) {
3188 if (strchr("()[]{}", *q) != NULL) {
3189 // we found half of a pair
3190 p = find_pair(q, *q);
3191 if (p == NULL) {
3192 indicate_error(c);
3193 } else {
3194 dot = p;
3195 }
3196 break;
3197 }
3198 }
3199 if (*q == '\n')
3200 indicate_error(c);
3201 break;
3202 case 'f': // f- forward to a user specified char
3203 last_forward_char = get_one_char(); // get the search char
3204 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003205 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003206 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003207 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003208 case ';': // ;- look at rest of line for last forward char
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003209 if (--cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003210 do_cmd(';');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003211 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003212 if (last_forward_char == 0)
3213 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003214 q = dot + 1;
3215 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3216 q++;
3217 }
3218 if (*q == last_forward_char)
3219 dot = q;
3220 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003221 case ',': // repeat latest 'f' in opposite direction
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003222 if (--cmdcnt > 0) {
Paul Foxb5ee8db2008-02-14 01:17:01 +00003223 do_cmd(',');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003224 }
Paul Foxb5ee8db2008-02-14 01:17:01 +00003225 if (last_forward_char == 0)
3226 break;
3227 q = dot - 1;
3228 while (q >= text && *q != '\n' && *q != last_forward_char) {
3229 q--;
3230 }
3231 if (q >= text && *q == last_forward_char)
3232 dot = q;
3233 break;
3234
Eric Andersen3f980402001-04-04 17:31:15 +00003235 case '-': // -- goto prev line
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003236 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003237 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003238 }
Eric Andersen3f980402001-04-04 17:31:15 +00003239 dot_prev();
3240 dot_skip_over_ws();
3241 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003242#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003243 case '.': // .- repeat the last modifying command
3244 // Stuff the last_modifying_cmd back into stdin
3245 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003246 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003247 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003248 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003249 }
3250 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003251#endif
3252#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003253 case '?': // /- search for a pattern
3254 case '/': // /- search for a pattern
3255 buf[0] = c;
3256 buf[1] = '\0';
3257 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003258 if (q[0] && !q[1]) {
3259 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003260 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003261 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003262 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003263 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003264 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003265 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003266 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003267 goto dc3; // now find the pattern
3268 }
3269 // user changed mind and erased the "/"- do nothing
3270 break;
3271 case 'N': // N- backward search for last pattern
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003272 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003273 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003274 }
Eric Andersen3f980402001-04-04 17:31:15 +00003275 dir = BACK; // assume BACKWARD search
3276 p = dot - 1;
3277 if (last_search_pattern[0] == '?') {
3278 dir = FORWARD;
3279 p = dot + 1;
3280 }
3281 goto dc4; // now search for pattern
3282 break;
3283 case 'n': // n- repeat search for last pattern
3284 // search rest of text[] starting at next char
3285 // if search fails return orignal "p" not the "p+1" address
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003286 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003287 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003288 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003289 dc3:
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003290 dir = FORWARD; // assume FORWARD search
3291 p = dot + 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003292 if (last_search_pattern[0] == '?') {
3293 dir = BACK;
3294 p = dot - 1;
3295 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003296 dc4:
Eric Andersen3f980402001-04-04 17:31:15 +00003297 q = char_search(p, last_search_pattern + 1, dir, FULL);
3298 if (q != NULL) {
3299 dot = q; // good search, update "dot"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003300 msg = "";
Eric Andersen3f980402001-04-04 17:31:15 +00003301 goto dc2;
3302 }
3303 // no pattern found between "dot" and "end"- continue at top
3304 p = text;
3305 if (dir == BACK) {
3306 p = end - 1;
3307 }
3308 q = char_search(p, last_search_pattern + 1, dir, FULL);
3309 if (q != NULL) { // found something
3310 dot = q; // found new pattern- goto it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003311 msg = "search hit BOTTOM, continuing at TOP";
Eric Andersen3f980402001-04-04 17:31:15 +00003312 if (dir == BACK) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003313 msg = "search hit TOP, continuing at BOTTOM";
Eric Andersen3f980402001-04-04 17:31:15 +00003314 }
3315 } else {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003316 msg = "Pattern not found";
Eric Andersen3f980402001-04-04 17:31:15 +00003317 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003318 dc2:
3319 if (*msg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003320 status_line_bold("%s", msg);
Eric Andersen3f980402001-04-04 17:31:15 +00003321 break;
3322 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003323 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003324 if (q != NULL) { // found blank line
3325 dot = next_line(q); // move to next blank line
3326 }
3327 break;
3328 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003329 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003330 if (q != NULL) { // found blank line
3331 dot = next_line(q); // move to next blank line
3332 }
3333 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003334#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003335 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003336 case '1': // 1-
3337 case '2': // 2-
3338 case '3': // 3-
3339 case '4': // 4-
3340 case '5': // 5-
3341 case '6': // 6-
3342 case '7': // 7-
3343 case '8': // 8-
3344 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003345 if (c == '0' && cmdcnt < 1) {
3346 dot_begin(); // this was a standalone zero
3347 } else {
3348 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3349 }
3350 break;
3351 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003352 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003353#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003354 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003355#else
Eric Andersen822c3832001-05-07 17:37:43 +00003356 if (*p == ':')
3357 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003358 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003359 if (cnt <= 0)
3360 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003361 if (strncmp(p, "quit", cnt) == 0
3362 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003363 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003364 if (file_modified && p[1] != '!') {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003365 status_line_bold("No write since last change (:quit! overrides)");
Eric Andersen3f980402001-04-04 17:31:15 +00003366 } else {
3367 editing = 0;
3368 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003369 } else if (strncmp(p, "write", cnt) == 0
3370 || strncmp(p, "wq", cnt) == 0
3371 || strncmp(p, "wn", cnt) == 0
3372 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003373 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003374 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003375 if (cnt < 0) {
3376 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003377 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003378 } else {
3379 file_modified = 0;
3380 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003381 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003382 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3383 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3384 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003385 editing = 0;
3386 }
Eric Andersen3f980402001-04-04 17:31:15 +00003387 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003388 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003389 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003390 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003391 dot = find_line(j); // go to line # j
3392 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003393 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003394 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003395 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003396#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003397 break;
3398 case '<': // <- Left shift something
3399 case '>': // >- Right shift something
3400 cnt = count_lines(text, dot); // remember what line we are on
3401 c1 = get_one_char(); // get the type of thing to delete
3402 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003403 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003404 p = begin_line(p);
3405 q = end_line(q);
3406 i = count_lines(p, q); // # of lines we are shifting
3407 for ( ; i > 0; i--, p = next_line(p)) {
3408 if (c == '<') {
3409 // shift left- remove tab or 8 spaces
3410 if (*p == '\t') {
3411 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003412 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003413 } else if (*p == ' ') {
3414 // we should be calculating columns, not just SPACE
3415 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003416 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003417 }
3418 }
3419 } else if (c == '>') {
3420 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003421 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003422 }
3423 }
3424 dot = find_line(cnt); // what line were we on
3425 dot_skip_over_ws();
3426 end_cmd_q(); // stop adding to q
3427 break;
3428 case 'A': // A- append at e-o-l
3429 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003430 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003431 case 'a': // a- append after current char
3432 if (*dot != '\n')
3433 dot++;
3434 goto dc_i;
3435 break;
3436 case 'B': // B- back a blank-delimited Word
3437 case 'E': // E- end of a blank-delimited word
3438 case 'W': // W- forward a blank-delimited word
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003439 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003440 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003441 }
Eric Andersen3f980402001-04-04 17:31:15 +00003442 dir = FORWARD;
3443 if (c == 'B')
3444 dir = BACK;
3445 if (c == 'W' || isspace(dot[dir])) {
3446 dot = skip_thing(dot, 1, dir, S_TO_WS);
3447 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3448 }
3449 if (c != 'W')
3450 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3451 break;
3452 case 'C': // C- Change to e-o-l
3453 case 'D': // D- delete to e-o-l
3454 save_dot = dot;
3455 dot = dollar_line(dot); // move to before NL
3456 // copy text into a register and delete
3457 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3458 if (c == 'C')
3459 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003460#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003461 if (c == 'D')
3462 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003463#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003464 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003465 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003466 c1 = get_one_char();
3467 if (c1 != 'g') {
3468 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003469 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003470 buf[2] = '\0';
3471 not_implemented(buf);
3472 break;
3473 }
3474 if (cmdcnt == 0)
3475 cmdcnt = 1;
3476 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003477 case 'G': // G- goto to a line number (default= E-O-F)
3478 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003479 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003480 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003481 }
3482 dot_skip_over_ws();
3483 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003484 case 'H': // H- goto top line on screen
3485 dot = screenbegin;
3486 if (cmdcnt > (rows - 1)) {
3487 cmdcnt = (rows - 1);
3488 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003489 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003490 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003491 }
Eric Andersen3f980402001-04-04 17:31:15 +00003492 dot_skip_over_ws();
3493 break;
3494 case 'I': // I- insert before first non-blank
3495 dot_begin(); // 0
3496 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003497 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003498 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003499 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003500 dc_i:
Eric Andersen3f980402001-04-04 17:31:15 +00003501 cmd_mode = 1; // start insrting
Eric Andersen3f980402001-04-04 17:31:15 +00003502 break;
3503 case 'J': // J- join current and next lines together
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003504 if (--cmdcnt > 1) {
Eric Andersen3f980402001-04-04 17:31:15 +00003505 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003506 }
Eric Andersen3f980402001-04-04 17:31:15 +00003507 dot_end(); // move to NL
3508 if (dot < end - 1) { // make sure not last char in text[]
3509 *dot++ = ' '; // replace NL with space
Paul Fox8552aec2005-09-16 12:20:05 +00003510 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003511 while (isblank(*dot)) { // delete leading WS
Eric Andersen3f980402001-04-04 17:31:15 +00003512 dot_delete();
3513 }
3514 }
3515 end_cmd_q(); // stop adding to q
3516 break;
3517 case 'L': // L- goto bottom line on screen
3518 dot = end_screen();
3519 if (cmdcnt > (rows - 1)) {
3520 cmdcnt = (rows - 1);
3521 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003522 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003523 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003524 }
Eric Andersen3f980402001-04-04 17:31:15 +00003525 dot_begin();
3526 dot_skip_over_ws();
3527 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003528 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003529 dot = screenbegin;
3530 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3531 dot = next_line(dot);
3532 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003533 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003534 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003535 p = begin_line(dot);
3536 if (p[-1] == '\n') {
3537 dot_prev();
3538 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3539 dot_end();
3540 dot = char_insert(dot, '\n');
3541 } else {
3542 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003543 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003544 dot_prev(); // -
3545 }
3546 goto dc_i;
3547 break;
3548 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003549 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003550 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003551 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003552 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003553 c = 'x';
3554 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003555 case 'X': // X- delete char before dot
3556 case 'x': // x- delete the current char
3557 case 's': // s- substitute the current char
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003558 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003559 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003560 }
Eric Andersen3f980402001-04-04 17:31:15 +00003561 dir = 0;
3562 if (c == 'X')
3563 dir = -1;
3564 if (dot[dir] != '\n') {
3565 if (c == 'X')
3566 dot--; // delete prev char
3567 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3568 }
3569 if (c == 's')
3570 goto dc_i; // start insrting
3571 end_cmd_q(); // stop adding to q
3572 break;
3573 case 'Z': // Z- if modified, {write}; exit
3574 // ZZ means to save file (if necessary), then exit
3575 c1 = get_one_char();
3576 if (c1 != 'Z') {
3577 indicate_error(c);
3578 break;
3579 }
Paul Foxf0305b72006-03-28 14:18:21 +00003580 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003581 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003582 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003583 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003584 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003585 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003586 if (cnt < 0) {
3587 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003588 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003589 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003590 editing = 0;
3591 }
3592 } else {
3593 editing = 0;
3594 }
3595 break;
3596 case '^': // ^- move to first non-blank on line
3597 dot_begin();
3598 dot_skip_over_ws();
3599 break;
3600 case 'b': // b- back a word
3601 case 'e': // e- end of word
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003602 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003603 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003604 }
Eric Andersen3f980402001-04-04 17:31:15 +00003605 dir = FORWARD;
3606 if (c == 'b')
3607 dir = BACK;
3608 if ((dot + dir) < text || (dot + dir) > end - 1)
3609 break;
3610 dot += dir;
3611 if (isspace(*dot)) {
3612 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3613 }
3614 if (isalnum(*dot) || *dot == '_') {
3615 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3616 } else if (ispunct(*dot)) {
3617 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3618 }
3619 break;
3620 case 'c': // c- change something
3621 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003622#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003623 case 'y': // y- yank something
3624 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003625#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003626 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003627 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003628 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003629#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003630 if (c == 'y' || c == 'Y')
3631 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003632#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003633 c1 = 'y';
3634 if (c != 'Y')
3635 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003636 // determine range, and whether it spans lines
3637 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003638 if (c1 == 27) { // ESC- user changed mind and wants out
3639 c = c1 = 27; // Escape- do nothing
3640 } else if (strchr("wW", c1)) {
3641 if (c == 'c') {
3642 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003643 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003644 if (q <= text || q[-1] == '\n')
3645 break;
3646 q--;
3647 }
3648 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003649 dot = yank_delete(p, q, ml, yf); // delete word
3650 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3651 // partial line copy text into a register and delete
3652 dot = yank_delete(p, q, ml, yf); // delete word
3653 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3654 // whole line copy text into a register and delete
3655 dot = yank_delete(p, q, ml, yf); // delete lines
3656 whole = 1;
3657 } else {
3658 // could not recognize object
3659 c = c1 = 27; // error-
3660 ml = 0;
3661 indicate_error(c);
3662 }
3663 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003664 if (c == 'c') {
3665 dot = char_insert(dot, '\n');
3666 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003667 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003668 dot_prev();
3669 }
3670 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003671 dot_begin();
3672 dot_skip_over_ws();
3673 }
Eric Andersen3f980402001-04-04 17:31:15 +00003674 }
3675 if (c1 != 27) {
3676 // if CHANGING, not deleting, start inserting after the delete
3677 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003678 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003679 goto dc_i; // start inserting
3680 }
3681 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003682 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003683 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003684#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003685 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003686 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003687 }
3688 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003689 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003690 for (cnt = 0; p <= q; p++) {
3691 if (*p == '\n')
3692 cnt++;
3693 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003694 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003695 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003696#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003697 end_cmd_q(); // stop adding to q
3698 }
3699 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003700 }
Eric Andersen3f980402001-04-04 17:31:15 +00003701 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003702 case KEYCODE_UP: // cursor key Up
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003703 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003704 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003705 }
Eric Andersen3f980402001-04-04 17:31:15 +00003706 dot_prev();
3707 dot = move_to_col(dot, ccol + offset); // try stay in same col
3708 break;
3709 case 'r': // r- replace the current char with user input
3710 c1 = get_one_char(); // get the replacement char
3711 if (*dot != '\n') {
3712 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003713 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003714 }
3715 end_cmd_q(); // stop adding to q
3716 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003717 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003718 last_forward_char = get_one_char();
3719 do_cmd(';');
3720 if (*dot == last_forward_char)
3721 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003722 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003723 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003724 case 'w': // w- forward a word
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003725 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003726 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003727 }
Eric Andersen3f980402001-04-04 17:31:15 +00003728 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3729 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3730 } else if (ispunct(*dot)) { // we are on PUNCT
3731 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3732 }
3733 if (dot < end - 1)
3734 dot++; // move over word
3735 if (isspace(*dot)) {
3736 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3737 }
3738 break;
3739 case 'z': // z-
3740 c1 = get_one_char(); // get the replacement char
3741 cnt = 0;
3742 if (c1 == '.')
3743 cnt = (rows - 2) / 2; // put dot at center
3744 if (c1 == '-')
3745 cnt = rows - 2; // put dot at bottom
3746 screenbegin = begin_line(dot); // start dot at top
3747 dot_scroll(cnt, -1);
3748 break;
3749 case '|': // |- move to column "cmdcnt"
3750 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3751 break;
3752 case '~': // ~- flip the case of letters a-z -> A-Z
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003753 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003754 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003755 }
Eric Andersen3f980402001-04-04 17:31:15 +00003756 if (islower(*dot)) {
3757 *dot = toupper(*dot);
Denis Vlasenkob1759462008-06-20 20:20:54 +00003758 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003759 } else if (isupper(*dot)) {
3760 *dot = tolower(*dot);
Denis Vlasenkob1759462008-06-20 20:20:54 +00003761 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003762 }
3763 dot_right();
3764 end_cmd_q(); // stop adding to q
3765 break;
3766 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003767 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003768 dot_begin();
3769 break;
3770 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003771#if 0
3772 case KEYCODE_FUN1: // Function Key F1
3773 case KEYCODE_FUN2: // Function Key F2
3774 case KEYCODE_FUN3: // Function Key F3
3775 case KEYCODE_FUN4: // Function Key F4
3776 case KEYCODE_FUN5: // Function Key F5
3777 case KEYCODE_FUN6: // Function Key F6
3778 case KEYCODE_FUN7: // Function Key F7
3779 case KEYCODE_FUN8: // Function Key F8
3780 case KEYCODE_FUN9: // Function Key F9
3781 case KEYCODE_FUN10: // Function Key F10
3782 case KEYCODE_FUN11: // Function Key F11
3783 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003784 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003785#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003786 }
3787
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003788 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003789 // if text[] just became empty, add back an empty line
3790 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003791 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003792 dot = text;
3793 }
3794 // it is OK for dot to exactly equal to end, otherwise check dot validity
3795 if (dot != end) {
3796 dot = bound_dot(dot); // make sure "dot" is valid
3797 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003798#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003799 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003800#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003801
3802 if (!isdigit(c))
3803 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3804 cnt = dot - begin_line(dot);
3805 // Try to stay off of the Newline
3806 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3807 dot--;
3808}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003809
Paul Fox35e9c5d2008-03-06 16:26:12 +00003810/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003811#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003812static int totalcmds = 0;
3813static int Mp = 85; // Movement command Probability
3814static int Np = 90; // Non-movement command Probability
3815static int Dp = 96; // Delete command Probability
3816static int Ip = 97; // Insert command Probability
3817static int Yp = 98; // Yank command Probability
3818static int Pp = 99; // Put command Probability
3819static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003820static const char chars[20] = "\t012345 abcdABCD-=.$";
3821static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003822 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003823 "broadcast", "the", "emergency", "of",
3824 "system", "quick", "brown", "fox",
3825 "jumped", "over", "lazy", "dogs",
3826 "back", "January", "Febuary", "March"
3827};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003828static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003829 "You should have received a copy of the GNU General Public License\n",
3830 "char c, cm, *cmd, *cmd1;\n",
3831 "generate a command by percentages\n",
3832 "Numbers may be typed as a prefix to some commands.\n",
3833 "Quit, discarding changes!\n",
3834 "Forced write, if permission originally not valid.\n",
3835 "In general, any ex or ed command (such as substitute or delete).\n",
3836 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3837 "Please get w/ me and I will go over it with you.\n",
3838 "The following is a list of scheduled, committed changes.\n",
3839 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3840 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3841 "Any question about transactions please contact Sterling Huxley.\n",
3842 "I will try to get back to you by Friday, December 31.\n",
3843 "This Change will be implemented on Friday.\n",
3844 "Let me know if you have problems accessing this;\n",
3845 "Sterling Huxley recently added you to the access list.\n",
3846 "Would you like to go to lunch?\n",
3847 "The last command will be automatically run.\n",
3848 "This is too much english for a computer geek.\n",
3849};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003850static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003851 "You should have received a copy of the GNU General Public License\n",
3852 "char c, cm, *cmd, *cmd1;\n",
3853 "generate a command by percentages\n",
3854 "Numbers may be typed as a prefix to some commands.\n",
3855 "Quit, discarding changes!\n",
3856 "Forced write, if permission originally not valid.\n",
3857 "In general, any ex or ed command (such as substitute or delete).\n",
3858 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3859 "Please get w/ me and I will go over it with you.\n",
3860 "The following is a list of scheduled, committed changes.\n",
3861 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3862 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3863 "Any question about transactions please contact Sterling Huxley.\n",
3864 "I will try to get back to you by Friday, December 31.\n",
3865 "This Change will be implemented on Friday.\n",
3866 "Let me know if you have problems accessing this;\n",
3867 "Sterling Huxley recently added you to the access list.\n",
3868 "Would you like to go to lunch?\n",
3869 "The last command will be automatically run.\n",
3870 "This is too much english for a computer geek.\n",
3871};
3872
3873// create a random command to execute
3874static void crash_dummy()
3875{
3876 static int sleeptime; // how long to pause between commands
3877 char c, cm, *cmd, *cmd1;
3878 int i, cnt, thing, rbi, startrbi, percent;
3879
3880 // "dot" movement commands
3881 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3882
3883 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02003884 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003885 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003886 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02003887 readbuffer[0] = 'X';
3888 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003889 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003890 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003891 // generate a command by percentages
3892 percent = (int) lrand48() % 100; // get a number from 0-99
3893 if (percent < Mp) { // Movement commands
3894 // available commands
3895 cmd = cmd1;
3896 M++;
3897 } else if (percent < Np) { // non-movement commands
3898 cmd = "mz<>\'\""; // available commands
3899 N++;
3900 } else if (percent < Dp) { // Delete commands
3901 cmd = "dx"; // available commands
3902 D++;
3903 } else if (percent < Ip) { // Inset commands
3904 cmd = "iIaAsrJ"; // available commands
3905 I++;
3906 } else if (percent < Yp) { // Yank commands
3907 cmd = "yY"; // available commands
3908 Y++;
3909 } else if (percent < Pp) { // Put commands
3910 cmd = "pP"; // available commands
3911 P++;
3912 } else {
3913 // We do not know how to handle this command, try again
3914 U++;
3915 goto cd0;
3916 }
3917 // randomly pick one of the available cmds from "cmd[]"
3918 i = (int) lrand48() % strlen(cmd);
3919 cm = cmd[i];
3920 if (strchr(":\024", cm))
3921 goto cd0; // dont allow colon or ctrl-T commands
3922 readbuffer[rbi++] = cm; // put cmd into input buffer
3923
3924 // now we have the command-
3925 // there are 1, 2, and multi char commands
3926 // find out which and generate the rest of command as necessary
3927 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
3928 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
3929 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
3930 cmd1 = "abcdefghijklmnopqrstuvwxyz";
3931 }
3932 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3933 c = cmd1[thing];
3934 readbuffer[rbi++] = c; // add movement to input buffer
3935 }
3936 if (strchr("iIaAsc", cm)) { // multi-char commands
3937 if (cm == 'c') {
3938 // change some thing
3939 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3940 c = cmd1[thing];
3941 readbuffer[rbi++] = c; // add movement to input buffer
3942 }
3943 thing = (int) lrand48() % 4; // what thing to insert
3944 cnt = (int) lrand48() % 10; // how many to insert
3945 for (i = 0; i < cnt; i++) {
3946 if (thing == 0) { // insert chars
3947 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
3948 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003949 strcat(readbuffer, words[(int) lrand48() % 20]);
3950 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003951 sleeptime = 0; // how fast to type
3952 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003953 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003954 sleeptime = 0; // how fast to type
3955 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003956 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003957 sleeptime = 0; // how fast to type
3958 }
3959 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003960 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003961 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02003962 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003963 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003964 totalcmds++;
3965 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003966 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003967}
3968
3969// test to see if there are any errors
3970static void crash_test()
3971{
3972 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003973
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003974 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003975 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003976
3977 msg[0] = '\0';
3978 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003979 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003980 }
3981 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003982 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003983 }
3984 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003985 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003986 }
3987 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003988 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003989 }
3990 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003991 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003992 }
3993 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003994 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003995 }
3996
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003997 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00003998 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003999 totalcmds, last_input_char, msg, SOs, SOn);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01004000 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00004001 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004002 if (d[0] == '\n' || d[0] == '\r')
4003 break;
4004 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004005 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004006 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004007 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004008 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004009 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
4010 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
4011 oldtim = tim;
4012 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004013}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00004014#endif