blob: 633d42a8ce22b6b64bfc0a810c3dc5f6b70f3cab [file] [log] [blame]
Rob Landleye5e1a102006-06-21 01:15:36 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3f980402001-04-04 17:31:15 +00002/*
3 * tiny vi.c: A small 'vi' clone
4 * Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com>
5 *
Paul Foxdbf935d2006-03-27 20:29:33 +00006 * Licensed under the GPL v2 or later, see the file LICENSE in this tarball.
Eric Andersen3f980402001-04-04 17:31:15 +00007 */
8
Eric Andersen3f980402001-04-04 17:31:15 +00009/*
Eric Andersen3f980402001-04-04 17:31:15 +000010 * Things To Do:
11 * EXINIT
Eric Andersen1c0d3112001-04-16 15:46:44 +000012 * $HOME/.exrc and ./.exrc
Eric Andersen3f980402001-04-04 17:31:15 +000013 * add magic to search /foo.*bar
14 * add :help command
15 * :map macros
Eric Andersen3f980402001-04-04 17:31:15 +000016 * if mark[] values were line numbers rather than pointers
17 * it would be easier to change the mark when add/delete lines
Eric Andersen1c0d3112001-04-16 15:46:44 +000018 * More intelligence in refresh()
19 * ":r !cmd" and "!cmd" to filter text through an external command
20 * A true "undo" facility
21 * An "ex" line oriented mode- maybe using "cmdedit"
Eric Andersen3f980402001-04-04 17:31:15 +000022 */
23
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000024#include "libbb.h"
Eric Andersen3f980402001-04-04 17:31:15 +000025
Paul Fox35e9c5d2008-03-06 16:26:12 +000026/* the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +000027#define ENABLE_FEATURE_VI_CRASHME 0
28
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000029
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +000030#if ENABLE_LOCALE_SUPPORT
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000031
32#if ENABLE_FEATURE_VI_8BIT
Denys Vlasenkoc2704542009-11-20 19:14:19 +010033//FIXME: this does not work properly for Unicode anyway
34# define Isprint(c) (isprint)(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +000035#else
Denys Vlasenkoc2704542009-11-20 19:14:19 +010036# define Isprint(c) isprint_asciionly(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +000037#endif
38
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +000039#else
40
41/* 0x9b is Meta-ESC */
42#if ENABLE_FEATURE_VI_8BIT
43#define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
44#else
45#define Isprint(c) ((unsigned char)(c) >= ' ' && (unsigned char)(c) < 0x7f)
46#endif
47
48#endif
49
50
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000051enum {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +000052 MAX_TABSTOP = 32, // sanity limit
Denis Vlasenko88adfcd2007-12-22 15:40:13 +000053 // User input len. Need not be extra big.
54 // Lines in file being edited *can* be bigger than this.
55 MAX_INPUT_LEN = 128,
56 // Sanity limits. We have only one buffer of this size.
57 MAX_SCR_COLS = CONFIG_FEATURE_VI_MAX_LEN,
58 MAX_SCR_ROWS = CONFIG_FEATURE_VI_MAX_LEN,
Denis Vlasenkoe8a07882007-06-10 15:08:44 +000059};
Eric Andersen3f980402001-04-04 17:31:15 +000060
Glenn L McGrath09adaca2002-12-02 21:18:10 +000061/* vt102 typical ESC sequence */
62/* terminal standout start/normal ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000063static const char SOs[] ALIGN1 = "\033[7m";
64static const char SOn[] ALIGN1 = "\033[0m";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000065/* terminal bell sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000066static const char bell[] ALIGN1 = "\007";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000067/* Clear-end-of-line and Clear-end-of-screen ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000068static const char Ceol[] ALIGN1 = "\033[0K";
69static const char Ceos[] ALIGN1 = "\033[0J";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000070/* Cursor motion arbitrary destination ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000071static const char CMrc[] ALIGN1 = "\033[%d;%dH";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000072/* Cursor motion up and down ESC sequence */
Denis Vlasenko6ca409e2007-08-12 20:58:27 +000073static const char CMup[] ALIGN1 = "\033[A";
74static const char CMdown[] ALIGN1 = "\n";
Glenn L McGrath09adaca2002-12-02 21:18:10 +000075
Denis Vlasenkoded6ad32008-10-14 12:26:30 +000076#if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
77// cmds modifying text[]
78// vda: removed "aAiIs" as they switch us into insert mode
79// and remembering input for replay after them makes no sense
80static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~";
81#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +000082
Rob Landleybc68cd12006-03-10 19:22:06 +000083enum {
84 YANKONLY = FALSE,
85 YANKDEL = TRUE,
86 FORWARD = 1, // code depends on "1" for array index
87 BACK = -1, // code depends on "-1" for array index
88 LIMITED = 0, // how much of text[] in char_search
89 FULL = 1, // how much of text[] in char_search
Eric Andersen3f980402001-04-04 17:31:15 +000090
Rob Landleybc68cd12006-03-10 19:22:06 +000091 S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
92 S_TO_WS = 2, // used in skip_thing() for moving "dot"
93 S_OVER_WS = 3, // used in skip_thing() for moving "dot"
94 S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
Denis Vlasenko8e858e22007-03-07 09:35:43 +000095 S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
Rob Landleybc68cd12006-03-10 19:22:06 +000096};
Eric Andersen3f980402001-04-04 17:31:15 +000097
Denis Vlasenkob1759462008-06-20 20:20:54 +000098
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +000099/* vi.c expects chars to be unsigned. */
100/* busybox build system provides that, but it's better */
101/* to audit and fix the source */
Eric Andersen3f980402001-04-04 17:31:15 +0000102
Denis Vlasenkob1759462008-06-20 20:20:54 +0000103struct globals {
104 /* many references - keep near the top of globals */
105 char *text, *end; // pointers to the user data in memory
106 char *dot; // where all the action takes place
107 int text_size; // size of the allocated buffer
108
109 /* the rest */
110 smallint vi_setops;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000111#define VI_AUTOINDENT 1
112#define VI_SHOWMATCH 2
113#define VI_IGNORECASE 4
114#define VI_ERR_METHOD 8
115#define autoindent (vi_setops & VI_AUTOINDENT)
116#define showmatch (vi_setops & VI_SHOWMATCH )
117#define ignorecase (vi_setops & VI_IGNORECASE)
118/* indicate error with beep or flash */
119#define err_method (vi_setops & VI_ERR_METHOD)
120
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000121#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000122 smallint readonly_mode;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000123#define SET_READONLY_FILE(flags) ((flags) |= 0x01)
124#define SET_READONLY_MODE(flags) ((flags) |= 0x02)
125#define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000126#else
Denis Vlasenkob1759462008-06-20 20:20:54 +0000127#define SET_READONLY_FILE(flags) ((void)0)
128#define SET_READONLY_MODE(flags) ((void)0)
129#define UNSET_READONLY_FILE(flags) ((void)0)
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000130#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000131
Denis Vlasenkob1759462008-06-20 20:20:54 +0000132 smallint editing; // >0 while we are editing a file
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000133 // [code audit says "can be 0, 1 or 2 only"]
Denis Vlasenkob1759462008-06-20 20:20:54 +0000134 smallint cmd_mode; // 0=command 1=insert 2=replace
135 int file_modified; // buffer contents changed (counter, not flag!)
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000136 int last_file_modified; // = -1;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000137 int fn_start; // index of first cmd line file name
138 int save_argc; // how many file names on cmd line
139 int cmdcnt; // repetition count
140 unsigned rows, columns; // the terminal screen is this size
141 int crow, ccol; // cursor is on Crow x Ccol
142 int offset; // chars scrolled off the screen to the left
143 int have_status_msg; // is default edit status needed?
144 // [don't make smallint!]
145 int last_status_cksum; // hash of current status line
146 char *current_filename;
147 char *screenbegin; // index into text[], of top line on the screen
148 char *screen; // pointer to the virtual screen buffer
149 int screensize; // and its size
150 int tabstop;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000151 int last_forward_char; // last char searched for with 'f' (int because of Unicode)
Denis Vlasenkob1759462008-06-20 20:20:54 +0000152 char erase_char; // the users erase character
153 char last_input_char; // last char read from user
Denis Vlasenkob1759462008-06-20 20:20:54 +0000154
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000155#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkob1759462008-06-20 20:20:54 +0000156 smallint adding2q; // are we currently adding user input to q
157 int lmc_len; // length of last_modifying_cmd
158 char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000159#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000160#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenkob1759462008-06-20 20:20:54 +0000161 int last_row; // where the cursor was last moved to
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000162#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000163#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkob1759462008-06-20 20:20:54 +0000164 int my_pid;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000165#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000166#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkob1759462008-06-20 20:20:54 +0000167 char *last_search_pattern; // last pattern from a '/' or '?' search
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000168#endif
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000169
Denis Vlasenkob1759462008-06-20 20:20:54 +0000170 /* former statics */
171#if ENABLE_FEATURE_VI_YANKMARK
172 char *edit_file__cur_line;
173#endif
174 int refresh__old_offset;
175 int format_edit_status__tot;
Eric Andersen3f980402001-04-04 17:31:15 +0000176
Denis Vlasenkob1759462008-06-20 20:20:54 +0000177 /* a few references only */
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000178#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000179 int YDreg, Ureg; // default delete register and orig line for "U"
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000180 char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000181 char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
182 char *context_start, *context_end;
183#endif
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000184#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkob1759462008-06-20 20:20:54 +0000185 sigjmp_buf restart; // catch_sig()
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000186#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000187 struct termios term_orig, term_vi; // remember what the cooked mode was
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000188#if ENABLE_FEATURE_VI_COLON
189 char *initial_cmds[3]; // currently 2 entries, NULL terminated
190#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000191 // Should be just enough to hold a key sequence,
Denis Vlasenko25497c12008-10-14 10:25:05 +0000192 // but CRASHME mode uses it as generated command buffer too
193#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko1dfeeeb2008-10-18 19:04:37 +0000194 char readbuffer[128];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000195#else
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000196 char readbuffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000197#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000198#define STATUS_BUFFER_LEN 200
199 char status_buffer[STATUS_BUFFER_LEN]; // messages to the user
200#if ENABLE_FEATURE_VI_DOT_CMD
201 char last_modifying_cmd[MAX_INPUT_LEN]; // last modifying cmd for "."
202#endif
203 char get_input_line__buf[MAX_INPUT_LEN]; /* former static */
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000204
205 char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000206};
207#define G (*ptr_to_globals)
208#define text (G.text )
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000209#define text_size (G.text_size )
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000210#define end (G.end )
211#define dot (G.dot )
212#define reg (G.reg )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000213
214#define vi_setops (G.vi_setops )
215#define editing (G.editing )
216#define cmd_mode (G.cmd_mode )
217#define file_modified (G.file_modified )
218#define last_file_modified (G.last_file_modified )
219#define fn_start (G.fn_start )
220#define save_argc (G.save_argc )
221#define cmdcnt (G.cmdcnt )
222#define rows (G.rows )
223#define columns (G.columns )
224#define crow (G.crow )
225#define ccol (G.ccol )
226#define offset (G.offset )
227#define status_buffer (G.status_buffer )
228#define have_status_msg (G.have_status_msg )
229#define last_status_cksum (G.last_status_cksum )
230#define current_filename (G.current_filename )
231#define screen (G.screen )
232#define screensize (G.screensize )
233#define screenbegin (G.screenbegin )
234#define tabstop (G.tabstop )
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000235#define last_forward_char (G.last_forward_char )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000236#define erase_char (G.erase_char )
237#define last_input_char (G.last_input_char )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000238#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000239#define readonly_mode (G.readonly_mode )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000240#else
241#define readonly_mode 0
242#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000243#define adding2q (G.adding2q )
244#define lmc_len (G.lmc_len )
245#define ioq (G.ioq )
246#define ioq_start (G.ioq_start )
247#define last_row (G.last_row )
248#define my_pid (G.my_pid )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000249#define last_search_pattern (G.last_search_pattern)
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000250
Denis Vlasenkob1759462008-06-20 20:20:54 +0000251#define edit_file__cur_line (G.edit_file__cur_line)
252#define refresh__old_offset (G.refresh__old_offset)
253#define format_edit_status__tot (G.format_edit_status__tot)
254
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000255#define YDreg (G.YDreg )
256#define Ureg (G.Ureg )
257#define mark (G.mark )
258#define context_start (G.context_start )
259#define context_end (G.context_end )
260#define restart (G.restart )
261#define term_orig (G.term_orig )
262#define term_vi (G.term_vi )
263#define initial_cmds (G.initial_cmds )
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000264#define readbuffer (G.readbuffer )
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000265#define scr_out_buf (G.scr_out_buf )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000266#define last_modifying_cmd (G.last_modifying_cmd )
267#define get_input_line__buf (G.get_input_line__buf)
268
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000269#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000270 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkob1759462008-06-20 20:20:54 +0000271 last_file_modified = -1; \
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000272 /* "" but has space for 2 chars: */ \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000273 IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000274} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000275
Denis Vlasenkob1759462008-06-20 20:20:54 +0000276
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000277static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000278static void edit_file(char *); // edit one file
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000279static void do_cmd(int); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000280static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000281static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
282static char *begin_line(char *); // return pointer to cur line B-o-l
283static char *end_line(char *); // return pointer to cur line E-o-l
284static char *prev_line(char *); // return pointer to prev line B-o-l
285static char *next_line(char *); // return pointer to next line B-o-l
286static char *end_screen(void); // get pointer to last char on screen
287static int count_lines(char *, char *); // count line from start to stop
288static char *find_line(int); // find begining of line #li
289static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000290static void dot_left(void); // move dot left- dont leave line
291static void dot_right(void); // move dot right- dont leave line
292static void dot_begin(void); // move dot to B-o-l
293static void dot_end(void); // move dot to E-o-l
294static void dot_next(void); // move dot to next line B-o-l
295static void dot_prev(void); // move dot to prev line B-o-l
296static void dot_scroll(int, int); // move the screen up or down
297static void dot_skip_over_ws(void); // move dot pat WS
298static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000299static char *bound_dot(char *); // make sure text[0] <= P < "end"
300static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000301static char *char_insert(char *, char); // insert the char c at 'p'
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000302// might reallocate text[]! use p += stupid_insert(p, ...),
303// and be careful to not use pointers into potentially freed text[]!
304static uintptr_t stupid_insert(char *, char); // stupidly insert the char c at 'p'
Paul Foxc51fc7b2008-03-06 01:34:23 +0000305static int find_range(char **, char **, char); // return pointers for an object
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000306static int st_test(char *, int, int, char *); // helper for skip_thing()
307static char *skip_thing(char *, int, int, int); // skip some object
308static char *find_pair(char *, char); // find matching pair () [] {}
309static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000310// might reallocate text[]! use p += text_hole_make(p, ...),
311// and be careful to not use pointers into potentially freed text[]!
312static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000313static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000314static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000315static void rawmode(void); // set "raw" mode on tty
316static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000317// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
318static int mysleep(int);
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000319static int readit(void); // read (maybe cursor) key from stdin
320static int get_one_char(void); // read 1 char from stdin
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000321static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000322#if !ENABLE_FEATURE_VI_READONLY
323#define file_insert(fn, p, update_ro_status) file_insert(fn, p)
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000324#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000325// file_insert might reallocate text[]!
326static int file_insert(const char *, char *, int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000327static int file_write(char *, char *, char *);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000328#if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
329#define place_cursor(a, b, optimize) place_cursor(a, b)
330#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000331static void place_cursor(int, int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000332static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000333static void clear_to_eol(void);
334static void clear_to_eos(void);
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000335static void go_bottom_and_clear_to_eol(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000336static void standout_start(void); // send "start reverse video" sequence
337static void standout_end(void); // send "end reverse video" sequence
338static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000339static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000340static void status_line(const char *, ...); // print to status buf
341static void status_line_bold(const char *, ...);
342static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000343static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000344static void redraw(int); // force a full screen refresh
Denis Vlasenko68404f12008-03-17 09:00:54 +0000345static char* format_line(char* /*, int*/);
Eric Andersen3f980402001-04-04 17:31:15 +0000346static void refresh(int); // update the terminal from screen[]
347
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000348static void Indicate_Error(void); // use flash or beep to indicate error
349#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000350static void Hit_Return(void);
351
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000352#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000353static char *char_search(char *, const char *, int, int); // search for pattern starting at p
354static int mycmp(const char *, const char *, int); // string cmp based in "ignorecase"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000355#endif
356#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000357static char *get_one_address(char *, int *); // get colon addr, if present
358static char *get_address(char *, int *, int *); // get two colon addrs, if present
359static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000360#endif
361#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000362static void winch_sig(int); // catch window size changes
363static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000364static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000365#endif
366#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000367static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000368static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000369#else
370#define end_cmd_q() ((void)0)
371#endif
372#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000373static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000374#endif
375#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000376// might reallocate text[]! use p += string_insert(p, ...),
377// and be careful to not use pointers into potentially freed text[]!
378static uintptr_t string_insert(char *, const char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000379#endif
380#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000381static char *text_yank(char *, char *, int); // save copy of "p" into a register
382static char what_reg(void); // what is letter of current YDreg
383static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000384#endif
385#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000386static void crash_dummy();
387static void crash_test();
388static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000389#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000390
391
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000392static void write1(const char *out)
393{
394 fputs(out, stdout);
395}
396
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000397int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000398int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000399{
Eric Andersend402edf2001-04-04 19:29:48 +0000400 int c;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000401
402 INIT_G();
Eric Andersen3f980402001-04-04 17:31:15 +0000403
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000404#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000405 my_pid = getpid();
406#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000407#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000408 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000409#endif
Denis Vlasenko2414a962007-07-18 22:03:40 +0000410#ifdef NO_SUCH_APPLET_YET
411 /* If we aren't "vi", we are "view" */
412 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000413 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000414 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000415#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000416
Bernhard Reutner-Fischer73f56bb2007-09-22 21:18:46 +0000417 vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenkof9234132007-03-21 00:03:42 +0000418 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000419 // 2- process EXINIT variable from environment
420 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000421#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000422 {
423 char *p = getenv("EXINIT");
424 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000425 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000426 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000427#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000428 while ((c = getopt(argc, argv, "hCRH" IF_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000429 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000430#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000431 case 'C':
432 crashme = 1;
433 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000434#endif
435#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000436 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000437 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000438 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000439#endif
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000440#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000441 case 'c': // cmd line vi command
442 if (*optarg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000443 initial_cmds[initial_cmds[0] != 0] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000444 break;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000445#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000446 case 'H':
Eric Andersen3f980402001-04-04 17:31:15 +0000447 show_help();
Paul Fox35e9c5d2008-03-06 16:26:12 +0000448 /* fall through */
Paul Fox35e9c5d2008-03-06 16:26:12 +0000449 default:
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000450 bb_show_usage();
Eric Andersendd8500b2001-07-02 18:06:14 +0000451 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000452 }
453 }
454
455 // The argv array can be used by the ":next" and ":rewind" commands
456 // save optind.
457 fn_start = optind; // remember first file name for :next and :rew
458 save_argc = argc;
459
460 //----- This is the main file handling loop --------------
461 if (optind >= argc) {
Eric Andersen3f980402001-04-04 17:31:15 +0000462 edit_file(0);
463 } else {
464 for (; optind < argc; optind++) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000465 edit_file(argv[optind]);
Eric Andersen3f980402001-04-04 17:31:15 +0000466 }
467 }
468 //-----------------------------------------------------------
469
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000470 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000471}
472
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000473/* read text from file or create an empty buf */
474/* will also update current_filename */
475static int init_text_buffer(char *fn)
476{
477 int rc;
478 int size = file_size(fn); // file size. -1 means does not exist.
479
480 /* allocate/reallocate text buffer */
481 free(text);
Denis Vlasenkob1759462008-06-20 20:20:54 +0000482 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000483 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000484
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000485 if (fn != current_filename) {
486 free(current_filename);
487 current_filename = xstrdup(fn);
488 }
489 if (size < 0) {
490 // file dont exist. Start empty buf with dummy line
491 char_insert(text, '\n');
492 rc = 0;
493 } else {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000494 rc = file_insert(fn, text, 1);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000495 }
496 file_modified = 0;
497 last_file_modified = -1;
498#if ENABLE_FEATURE_VI_YANKMARK
499 /* init the marks. */
500 memset(mark, 0, sizeof(mark));
501#endif
502 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000503}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000504
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000505static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000506{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000507#if ENABLE_FEATURE_VI_YANKMARK
508#define cur_line edit_file__cur_line
509#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000510 int c;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000511 int size;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000512#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000513 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000514#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000515
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000516 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000517 rawmode();
518 rows = 24;
519 columns = 80;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000520 size = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000521 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Rob Landleye5e1a102006-06-21 01:15:36 +0000522 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000523 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
524 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
525 }
Eric Andersen3f980402001-04-04 17:31:15 +0000526 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000527 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000528
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000529#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000530 YDreg = 26; // default Yank/Delete reg
531 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000532 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000533#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000534
Eric Andersen3f980402001-04-04 17:31:15 +0000535 last_forward_char = last_input_char = '\0';
536 crow = 0;
537 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000538
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000539#if ENABLE_FEATURE_VI_USE_SIGNALS
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000540 catch_sig(0);
Eric Andersen3f980402001-04-04 17:31:15 +0000541 signal(SIGWINCH, winch_sig);
542 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000543 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000544 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000545 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000546 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000547#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000548
Eric Andersen3f980402001-04-04 17:31:15 +0000549 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
550 cmdcnt = 0;
551 tabstop = 8;
552 offset = 0; // no horizontal offset
553 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000554#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000555 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000556 ioq = ioq_start = NULL;
557 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000558 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000559#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000560
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000561#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000562 {
563 char *p, *q;
564 int n = 0;
565
566 while ((p = initial_cmds[n])) {
567 do {
568 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000569 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000570 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000571 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000572 *p++ = '\0';
573 if (*q)
574 colon(q);
575 } while (p);
576 free(initial_cmds[n]);
577 initial_cmds[n] = NULL;
578 n++;
579 }
580 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000581#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000582 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000583 //------This is the main Vi cmd handling loop -----------------------
584 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000585#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000586 if (crashme > 0) {
587 if ((end - text) > 1) {
588 crash_dummy(); // generate a random command
589 } else {
590 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000591 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
592 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000593 refresh(FALSE);
594 }
595 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000596#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000597 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000598#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000599 // save a copy of the current line- for the 'U" command
600 if (begin_line(dot) != cur_line) {
601 cur_line = begin_line(dot);
602 text_yank(begin_line(dot), end_line(dot), Ureg);
603 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000604#endif
605#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000606 // These are commands that change text[].
607 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000608 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000609 && cmd_mode == 0 // command mode
610 && c > '\0' // exclude NUL and non-ASCII chars
611 && c < 0x7f // (Unicode and such)
612 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000613 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000614 start_new_cmd_q(c);
615 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000616#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000617 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000618
Eric Andersen3f980402001-04-04 17:31:15 +0000619 // poll to see if there is input already waiting. if we are
620 // not able to display output fast enough to keep up, skip
621 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200622 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000623 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000624 refresh(FALSE);
625 show_status_line();
626 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000627#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000628 if (crashme > 0)
629 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000630#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000631 }
632 //-------------------------------------------------------------------
633
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000634 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000635 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000636#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000637}
638
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000639//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000640#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000641static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000642{
643 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000644 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000645 IF_FEATURE_VI_YANKMARK(char c;)
646 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000647
648 *addr = -1; // assume no addr
649 if (*p == '.') { // the current line
650 p++;
651 q = begin_line(dot);
652 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000653 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000654#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000655 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000656 p++;
657 c = tolower(*p);
658 p++;
659 if (c >= 'a' && c <= 'z') {
660 // we have a mark
661 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000662 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000663 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000664 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000665 }
666 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000667 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000668#endif
669#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000670 else if (*p == '/') { // a search pattern
671 q = strchrnul(++p, '/');
672 pat = xstrndup(p, q - p); // save copy of pattern
673 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000674 if (*p == '/')
675 p++;
676 q = char_search(dot, pat, FORWARD, FULL);
677 if (q != NULL) {
678 *addr = count_lines(text, q);
679 }
680 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000681 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000682#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000683 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000684 p++;
685 q = begin_line(end - 1);
686 *addr = count_lines(text, q);
687 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000688 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000689 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000690 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200691 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000692 *addr = -1;
693 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000694 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000695}
696
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000697static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000698{
699 //----- get the address' i.e., 1,3 'a,'b -----
700 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000701 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000702 p++; // skip over leading spaces
703 if (*p == '%') { // alias for 1,$
704 p++;
705 *b = 1;
706 *e = count_lines(text, end-1);
707 goto ga0;
708 }
709 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000710 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000711 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000712 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000713 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000714 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000715 p++;
716 // get SECOND addr, if present
717 p = get_one_address(p, e);
718 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000719 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000720 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000721 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000722 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000723}
724
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000725#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000726static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000727 const char *short_opname, int opt)
728{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000729 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000730 int l = strlen(opname) - 1; /* opname have + ' ' */
731
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200732 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000733 if (strncasecmp(a, opname, l) == 0
734 || strncasecmp(a, short_opname, 2) == 0
735 ) {
736 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000737 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000738 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000739 vi_setops |= opt;
740 }
741}
742#endif
743
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000744// buf must be no longer than MAX_INPUT_LEN!
745static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000746{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000747 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000748 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000749 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000750 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000751
752 // :3154 // if (-e line 3154) goto it else stay put
753 // :4,33w! foo // write a portion of buffer to file "foo"
754 // :w // write all of buffer to current file
755 // :q // quit
756 // :q! // quit- dont care about modified file
757 // :'a,'z!sort -u // filter block through sort
758 // :'f // goto mark "f"
759 // :'fl // list literal the mark "f" line
760 // :.r bar // read file "bar" into buffer before dot
761 // :/123/,/abc/d // delete lines from "123" line to "abc" line
762 // :/xyz/ // goto the "xyz" line
763 // :s/find/replace/ // substitute pattern "find" with "replace"
764 // :!<cmd> // run <cmd> then return
765 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000766
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000767 if (!buf[0])
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000768 goto vc1;
769 if (*buf == ':')
770 buf++; // move past the ':'
771
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000772 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000773 b = e = -1;
774 q = text; // assume 1,$ for the range
775 r = end - 1;
776 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000777 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000778
779 // look for optional address(es) :. :1 :1,9 :'q,'a :%
780 buf = get_address(buf, &b, &e);
781
782 // remember orig command line
783 orig_buf = buf;
784
785 // get the COMMAND into cmd[]
786 buf1 = cmd;
787 while (*buf != '\0') {
788 if (isspace(*buf))
789 break;
790 *buf1++ = *buf++;
791 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000792 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000793 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000794 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000795 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000796 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000797 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000798 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000799 if (buf1) {
800 useforce = TRUE;
801 *buf1 = '\0'; // get rid of !
802 }
803 if (b >= 0) {
804 // if there is only one addr, then the addr
805 // is the line number of the single line the
806 // user wants. So, reset the end
807 // pointer to point at end of the "b" line
808 q = find_line(b); // what line is #b
809 r = end_line(q);
810 li = 1;
811 }
812 if (e >= 0) {
813 // we were given two addrs. change the
814 // end pointer to the addr given by user.
815 r = find_line(e); // what line is #e
816 r = end_line(r);
817 li = e - b + 1;
818 }
819 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000820 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000821 if (i == 0) { // :123CR goto line #123
822 if (b >= 0) {
823 dot = find_line(b); // what line is #b
824 dot_skip_over_ws();
825 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000826 }
827#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200828 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000829 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000830 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000831 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000832 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000833 retcode = system(orig_buf + 1); // run the cmd
834 if (retcode)
835 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000836 rawmode();
837 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000838 }
839#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200840 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000841 if (b < 0) { // no addr given- use defaults
842 b = e = count_lines(text, dot);
843 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000844 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200845 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000846 if (b < 0) { // no addr given- use defaults
847 q = begin_line(dot); // assume .,. for the range
848 r = end_line(dot);
849 }
850 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
851 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200852 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000853 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000854 if (file_modified && !useforce) {
855 status_line_bold("No write since last change (:edit! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000856 goto vc1;
857 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000858 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000859 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000860 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000861 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000862 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000863 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000864 } else {
865 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000866 status_line_bold("No current filename");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000867 goto vc1;
868 }
869
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000870 if (init_text_buffer(fn) < 0)
871 goto vc1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000872
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000873#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000874 if (Ureg >= 0 && Ureg < 28 && reg[Ureg] != 0) {
875 free(reg[Ureg]); // free orig line reg- for 'U'
876 reg[Ureg]= 0;
877 }
878 if (YDreg >= 0 && YDreg < 28 && reg[YDreg] != 0) {
879 free(reg[YDreg]); // free default yank/delete register
880 reg[YDreg]= 0;
881 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000882#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000883 // how many lines in text[]?
884 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000885 status_line("\"%s\"%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000886 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000887 " %dL, %dC", current_filename,
888 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000889 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000890 ((readonly_mode) ? " [Readonly]" : ""),
891 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000892 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200893 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000894 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +0100895 status_line_bold("No address allowed on this command");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000896 goto vc1;
897 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000898 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000899 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000900 free(current_filename);
901 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000902 } else {
903 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +0000904 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000905 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200906 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000907 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000908 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000909 cookmode();
910 show_help();
911 rawmode();
912 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200913 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000914 if (b < 0) { // no addr given- use defaults
915 q = begin_line(dot); // assume .,. for the range
916 r = end_line(dot);
917 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000918 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000919 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000920 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000921 int c_is_no_print;
922
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000923 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +0000924 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000925 if (c_is_no_print) {
926 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000927 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +0000928 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000929 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000930 write1("$\r");
931 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +0000932 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000933 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000934 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000935 else
936 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000937 }
Denis Vlasenko4daad902007-09-27 10:20:47 +0000938 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000939 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000940 standout_end();
941 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000942#if ENABLE_FEATURE_VI_SET
943 vc2:
944#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000945 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200946 } else if (strncmp(cmd, "quit", i) == 0 // Quit
947 || strncmp(cmd, "next", i) == 0 // edit next file
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000948 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000949 if (useforce) {
950 // force end of argv list
951 if (*cmd == 'q') {
952 optind = save_argc;
953 }
954 editing = 0;
955 goto vc1;
956 }
957 // don't exit if the file been modified
958 if (file_modified) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000959 status_line_bold("No write since last change (:%s! overrides)",
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000960 (*cmd == 'q' ? "quit" : "next"));
961 goto vc1;
962 }
963 // are there other file to edit
964 if (*cmd == 'q' && optind < save_argc - 1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000965 status_line_bold("%d more file to edit", (save_argc - optind - 1));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000966 goto vc1;
967 }
968 if (*cmd == 'n' && optind >= save_argc - 1) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000969 status_line_bold("No more files to edit");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000970 goto vc1;
971 }
972 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200973 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000974 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000975 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000976 status_line_bold("No filename given");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000977 goto vc1;
978 }
979 if (b < 0) { // no addr given- use defaults
980 q = begin_line(dot); // assume "dot"
981 }
982 // read after current line- unless user said ":0r foo"
983 if (b != 0)
984 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000985 { // dance around potentially-reallocated text[]
986 uintptr_t ofs = q - text;
987 ch = file_insert(fn, q, 0);
988 q = text + ofs;
989 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000990 if (ch < 0)
991 goto vc1; // nothing was inserted
992 // how many lines in text[]?
993 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000994 status_line("\"%s\""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000995 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000996 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000997 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000998 li, ch);
999 if (ch > 0) {
1000 // if the insert is before "dot" then we need to update
1001 if (q <= dot)
1002 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001003 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001004 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001005 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001006 if (file_modified && !useforce) {
1007 status_line_bold("No write since last change (:rewind! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001008 } else {
1009 // reset the filenames to edit
1010 optind = fn_start - 1;
1011 editing = 0;
1012 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001013#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001014 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001015#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001016 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001017#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001018 i = 0; // offset into args
Denis Vlasenkof9234132007-03-21 00:03:42 +00001019 // only blank is regarded as args delmiter. What about tab '\t' ?
1020 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001021 // print out values of all options
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001022 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001023 printf("----------------------------------------\r\n");
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001024#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001025 if (!autoindent)
1026 printf("no");
1027 printf("autoindent ");
1028 if (!err_method)
1029 printf("no");
1030 printf("flash ");
1031 if (!ignorecase)
1032 printf("no");
1033 printf("ignorecase ");
1034 if (!showmatch)
1035 printf("no");
1036 printf("showmatch ");
1037 printf("tabstop=%d ", tabstop);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001038#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001039 printf("\r\n");
1040 goto vc2;
1041 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001042#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001043 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001044 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001045 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001046 i = 2; // ":set noautoindent"
1047 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
1048 setops(argp, "flash ", i, "fl", VI_ERR_METHOD);
1049 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
1050 setops(argp, "showmatch ", i, "ic", VI_SHOWMATCH);
1051 /* tabstopXXXX */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001052 if (strncmp(argp + i, "tabstop=%d ", 7) == 0) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001053 sscanf(strchr(argp + i, '='), "tabstop=%d" + 7, &ch);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00001054 if (ch > 0 && ch <= MAX_TABSTOP)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001055 tabstop = ch;
1056 }
1057 while (*argp && *argp != ' ')
1058 argp++; // skip to arg delimiter (i.e. blank)
1059 while (*argp && *argp == ' ')
1060 argp++; // skip all delimiting blanks
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001061 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001062#endif /* FEATURE_VI_SETOPTS */
1063#endif /* FEATURE_VI_SET */
1064#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001065 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001066 char *ls, *F, *R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001067 int gflag;
1068
1069 // F points to the "find" pattern
1070 // R points to the "replace" pattern
1071 // replace the cmd line delimiters "/" with NULLs
1072 gflag = 0; // global replace flag
1073 c = orig_buf[1]; // what is the delimiter
1074 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001075 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001076 if (!R)
1077 goto colon_s_fail;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001078 *R++ = '\0'; // terminate "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001079 buf1 = strchr(R, c);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001080 if (!buf1)
1081 goto colon_s_fail;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001082 *buf1++ = '\0'; // terminate "replace"
1083 if (*buf1 == 'g') { // :s/foo/bar/g
1084 buf1++;
1085 gflag++; // turn on gflag
1086 }
1087 q = begin_line(q);
1088 if (b < 0) { // maybe :s/foo/bar/
1089 q = begin_line(dot); // start with cur line
1090 b = count_lines(text, q); // cur line number
1091 }
1092 if (e < 0)
1093 e = b; // maybe :.s/foo/bar/
1094 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
1095 ls = q; // orig line start
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001096 vc4:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001097 buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001098 if (buf1) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001099 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001100 // we found the "find" pattern - delete it
1101 text_hole_delete(buf1, buf1 + strlen(F) - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001102 // inset the "replace" patern
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001103 bias = string_insert(buf1, R); // insert the string
1104 buf1 += bias;
1105 ls += bias;
1106 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001107 // check for "global" :s/foo/bar/g
1108 if (gflag == 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001109 if ((buf1 + strlen(R)) < end_line(ls)) {
1110 q = buf1 + strlen(R);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001111 goto vc4; // don't let q move past cur line
1112 }
1113 }
1114 }
1115 q = next_line(ls);
1116 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001117#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001118 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001119 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001120 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1121 || strncmp(cmd, "wq", i) == 0
1122 || strncmp(cmd, "wn", i) == 0
1123 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001124 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001125 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001126 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001127 fn = args;
1128 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001129#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001130 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001131 status_line_bold("\"%s\" File is read only", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001132 goto vc3;
1133 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001134#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001135 // how many lines in text[]?
1136 li = count_lines(q, r);
1137 ch = r - q + 1;
1138 // see if file exists- if not, its just a new file request
1139 if (useforce) {
1140 // if "fn" is not write-able, chmod u+w
1141 // sprintf(syscmd, "chmod u+w %s", fn);
1142 // system(syscmd);
1143 forced = TRUE;
1144 }
1145 l = file_write(fn, q, r);
1146 if (useforce && forced) {
1147 // chmod u-w
1148 // sprintf(syscmd, "chmod u-w %s", fn);
1149 // system(syscmd);
1150 forced = FALSE;
1151 }
Paul Fox61e45db2005-10-09 14:43:22 +00001152 if (l < 0) {
1153 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001154 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001155 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001156 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001157 if (q == text && r == end - 1 && l == ch) {
1158 file_modified = 0;
1159 last_file_modified = -1;
1160 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001161 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1162 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1163 )
1164 && l == ch
1165 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001166 editing = 0;
1167 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001168 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001169#if ENABLE_FEATURE_VI_READONLY
1170 vc3:;
1171#endif
1172#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001173 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001174 if (b < 0) { // no addr given- use defaults
1175 q = begin_line(dot); // assume .,. for the range
1176 r = end_line(dot);
1177 }
1178 text_yank(q, r, YDreg);
1179 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001180 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001181 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001182#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001183 } else {
1184 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001185 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001186 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001187 vc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001188 dot = bound_dot(dot); // make sure "dot" is valid
1189 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001190#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001191 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001192 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001193#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001194}
Paul Fox61e45db2005-10-09 14:43:22 +00001195
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001196#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001197
1198static void Hit_Return(void)
1199{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001200 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001201
Denis Vlasenkof882f082007-12-23 02:36:51 +00001202 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001203 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001204 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001205 while ((c = get_one_char()) != '\n' && c != '\r')
1206 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001207 redraw(TRUE); // force redraw all
1208}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001209
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001210static int next_tabstop(int col)
1211{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001212 return col + ((tabstop - 1) - (col % tabstop));
1213}
1214
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001215//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001216static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001217{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001218 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001219 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001220 int cnt, ro, co;
1221
1222 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001223
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001224 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001225 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001226 // how many lines do we have to move
1227 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001228 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001229 screenbegin = beg_cur;
1230 if (cnt > (rows - 1) / 2) {
1231 // we moved too many lines. put "dot" in middle of screen
1232 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1233 screenbegin = prev_line(screenbegin);
1234 }
1235 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001236 } else {
1237 char *end_scr; // begin and end of screen
1238 end_scr = end_screen(); // last char of screen
1239 if (beg_cur > end_scr) {
1240 // "d" is after bottom line on screen
1241 // how many lines do we have to move
1242 cnt = count_lines(end_scr, beg_cur);
1243 if (cnt > (rows - 1) / 2)
1244 goto sc1; // too many lines
1245 for (ro = 0; ro < cnt - 1; ro++) {
1246 // move screen begin the same amount
1247 screenbegin = next_line(screenbegin);
1248 // now, move the end of screen
1249 end_scr = next_line(end_scr);
1250 end_scr = end_line(end_scr);
1251 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001252 }
1253 }
1254 // "d" is on screen- find out which row
1255 tp = screenbegin;
1256 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1257 if (tp == beg_cur)
1258 break;
1259 tp = next_line(tp);
1260 }
1261
1262 // find out what col "d" is on
1263 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001264 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001265 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001266 break;
1267 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001268 // handle tabs like real vi
1269 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001270 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001271 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001272 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001273 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1274 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001275 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001276 co++;
1277 tp++;
1278 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001279
1280 // "co" is the column where "dot" is.
1281 // The screen has "columns" columns.
1282 // The currently displayed columns are 0+offset -- columns+ofset
1283 // |-------------------------------------------------------------|
1284 // ^ ^ ^
1285 // offset | |------- columns ----------------|
1286 //
1287 // If "co" is already in this range then we do not have to adjust offset
1288 // but, we do have to subtract the "offset" bias from "co".
1289 // If "co" is outside this range then we have to change "offset".
1290 // If the first char of a line is a tab the cursor will try to stay
1291 // in column 7, but we have to set offset to 0.
1292
1293 if (co < 0 + offset) {
1294 offset = co;
1295 }
1296 if (co >= columns + offset) {
1297 offset = co - columns + 1;
1298 }
1299 // if the first char of the line is a tab, and "dot" is sitting on it
1300 // force offset to 0.
1301 if (d == beg_cur && *d == '\t') {
1302 offset = 0;
1303 }
1304 co -= offset;
1305
1306 *row = ro;
1307 *col = co;
1308}
1309
1310//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001311static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001312{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001313 if (p > text) {
1314 p = memrchr(text, '\n', p - text);
1315 if (!p)
1316 return text;
1317 return p + 1;
1318 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001319 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001320}
1321
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001322static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001323{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001324 if (p < end - 1) {
1325 p = memchr(p, '\n', end - p - 1);
1326 if (!p)
1327 return end - 1;
1328 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001329 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001330}
1331
Denis Vlasenkof882f082007-12-23 02:36:51 +00001332static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001333{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001334 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001335 // Try to stay off of the Newline
1336 if (*p == '\n' && (p - begin_line(p)) > 0)
1337 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001338 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001339}
1340
Denis Vlasenkof882f082007-12-23 02:36:51 +00001341static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001342{
1343 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001344 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001345 p--; // step to prev line
1346 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001347 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001348}
1349
Denis Vlasenkof882f082007-12-23 02:36:51 +00001350static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001351{
1352 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001353 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001354 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001355 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001356}
1357
1358//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001359static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001360{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001361 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001362 int cnt;
1363
1364 // find new bottom line
1365 q = screenbegin;
1366 for (cnt = 0; cnt < rows - 2; cnt++)
1367 q = next_line(q);
1368 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001369 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001370}
1371
Denis Vlasenkof882f082007-12-23 02:36:51 +00001372// count line from start to stop
1373static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001374{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001375 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001376 int cnt;
1377
Denis Vlasenkof882f082007-12-23 02:36:51 +00001378 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001379 q = start;
1380 start = stop;
1381 stop = q;
1382 }
1383 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001384 stop = end_line(stop);
1385 while (start <= stop && start <= end - 1) {
1386 start = end_line(start);
1387 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001388 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001389 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001390 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001391 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001392}
1393
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001394static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001395{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001396 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001397
1398 for (q = text; li > 1; li--) {
1399 q = next_line(q);
1400 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001401 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001402}
1403
1404//----- Dot Movement Routines ----------------------------------
1405static void dot_left(void)
1406{
1407 if (dot > text && dot[-1] != '\n')
1408 dot--;
1409}
1410
1411static void dot_right(void)
1412{
1413 if (dot < end - 1 && *dot != '\n')
1414 dot++;
1415}
1416
1417static void dot_begin(void)
1418{
1419 dot = begin_line(dot); // return pointer to first char cur line
1420}
1421
1422static void dot_end(void)
1423{
1424 dot = end_line(dot); // return pointer to last char cur line
1425}
1426
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001427static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001428{
1429 int co;
1430
1431 p = begin_line(p);
1432 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001433 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001434 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001435 break;
1436 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001437 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001438 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001439 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001440 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001441 co++;
1442 p++;
1443 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001444 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001445}
1446
1447static void dot_next(void)
1448{
1449 dot = next_line(dot);
1450}
1451
1452static void dot_prev(void)
1453{
1454 dot = prev_line(dot);
1455}
1456
1457static void dot_scroll(int cnt, int dir)
1458{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001459 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001460
1461 for (; cnt > 0; cnt--) {
1462 if (dir < 0) {
1463 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001464 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001465 screenbegin = prev_line(screenbegin);
1466 } else {
1467 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001468 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001469 screenbegin = next_line(screenbegin);
1470 }
1471 }
1472 // make sure "dot" stays on the screen so we dont scroll off
1473 if (dot < screenbegin)
1474 dot = screenbegin;
1475 q = end_screen(); // find new bottom line
1476 if (dot > q)
1477 dot = begin_line(q); // is dot is below bottom line?
1478 dot_skip_over_ws();
1479}
1480
1481static void dot_skip_over_ws(void)
1482{
1483 // skip WS
1484 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1485 dot++;
1486}
1487
1488static void dot_delete(void) // delete the char at 'dot'
1489{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001490 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001491}
1492
Denis Vlasenkof882f082007-12-23 02:36:51 +00001493static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001494{
1495 if (p >= end && end > text) {
1496 p = end - 1;
1497 indicate_error('1');
1498 }
1499 if (p < text) {
1500 p = text;
1501 indicate_error('2');
1502 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001503 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001504}
1505
1506//----- Helper Utility Routines --------------------------------
1507
1508//----------------------------------------------------------------
1509//----- Char Routines --------------------------------------------
1510/* Chars that are part of a word-
1511 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1512 * Chars that are Not part of a word (stoppers)
1513 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1514 * Chars that are WhiteSpace
1515 * TAB NEWLINE VT FF RETURN SPACE
1516 * DO NOT COUNT NEWLINE AS WHITESPACE
1517 */
1518
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001519static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001520{
1521 int li;
1522
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001523 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001524 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001525 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001526 // initialize the new screen. assume this will be a empty file.
1527 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001528 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001529 for (li = 1; li < ro - 1; li++) {
1530 screen[(li * co) + 0] = '~';
1531 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001532 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001533}
1534
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001535#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko33875382008-06-21 20:31:50 +00001536static int mycmp(const char *s1, const char *s2, int len)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001537{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001538 if (ENABLE_FEATURE_VI_SETOPTS && ignorecase) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001539 return strncasecmp(s1, s2, len);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001540 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001541 return strncmp(s1, s2, len);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001542}
1543
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001544// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001545static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001546{
1547#ifndef REGEX_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001548 char *start, *stop;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001549 int len;
1550
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001551 len = strlen(pat);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001552 if (dir == FORWARD) {
1553 stop = end - 1; // assume range is p - end-1
1554 if (range == LIMITED)
1555 stop = next_line(p); // range is to next line
1556 for (start = p; start < stop; start++) {
1557 if (mycmp(start, pat, len) == 0) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001558 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001559 }
1560 }
1561 } else if (dir == BACK) {
1562 stop = text; // assume range is text - p
1563 if (range == LIMITED)
1564 stop = prev_line(p); // range is to prev line
1565 for (start = p - len; start >= stop; start--) {
1566 if (mycmp(start, pat, len) == 0) {
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001567 return start;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001568 }
1569 }
1570 }
1571 // pattern not found
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001572 return NULL;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001573#else /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001574 char *q;
1575 struct re_pattern_buffer preg;
1576 int i;
1577 int size, range;
1578
1579 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1580 preg.translate = 0;
1581 preg.fastmap = 0;
1582 preg.buffer = 0;
1583 preg.allocated = 0;
1584
1585 // assume a LIMITED forward search
1586 q = next_line(p);
1587 q = end_line(q);
1588 q = end - 1;
1589 if (dir == BACK) {
1590 q = prev_line(p);
1591 q = text;
1592 }
1593 // count the number of chars to search over, forward or backward
1594 size = q - p;
1595 if (size < 0)
1596 size = p - q;
1597 // RANGE could be negative if we are searching backwards
1598 range = q - p;
1599
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001600 q = re_compile_pattern(pat, strlen(pat), &preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001601 if (q != 0) {
1602 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001603 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001604 i = 0; // return p if pattern not compiled
1605 goto cs1;
1606 }
1607
1608 q = p;
1609 if (range < 0) {
1610 q = p - size;
1611 if (q < text)
1612 q = text;
1613 }
1614 // search for the compiled pattern, preg, in p[]
1615 // range < 0- search backward
1616 // range > 0- search forward
1617 // 0 < start < size
1618 // re_search() < 0 not found or error
1619 // re_search() > 0 index of found pattern
1620 // struct pattern char int int int struct reg
1621 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1622 i = re_search(&preg, q, size, 0, range, 0);
1623 if (i == -1) {
1624 p = 0;
1625 i = 0; // return NULL if pattern not found
1626 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001627 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001628 if (dir == FORWARD) {
1629 p = p + i;
1630 } else {
1631 p = p - i;
1632 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001633 return p;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001634#endif /* REGEX_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001635}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001636#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001637
Denis Vlasenko33875382008-06-21 20:31:50 +00001638static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001639{
1640 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001641 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001642 refresh(FALSE); // show the ^
1643 c = get_one_char();
1644 *p = c;
1645 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001646 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001647 } else if (c == 27) { // Is this an ESC?
1648 cmd_mode = 0;
1649 cmdcnt = 0;
1650 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001651 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001652 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001653 p--;
1654 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001655 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001656 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001657 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001658 p--;
1659 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001660 }
1661 } else {
1662 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001663 char *sp; // "save p"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001664
1665 if (c == 13)
1666 c = '\n'; // translate \r to \n
1667 sp = p; // remember addr of insert
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001668 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001669#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001670 if (showmatch && strchr(")]}", *sp) != NULL) {
1671 showmatching(sp);
1672 }
1673 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001674 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001675 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001676 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001677 len = strspn(q, " \t"); // space or tab
1678 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001679 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001680 bias = text_hole_make(p, len);
1681 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001682 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001683 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001684 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001685 }
1686 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001687#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001688 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001689 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001690}
1691
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001692// might reallocate text[]! use p += stupid_insert(p, ...),
1693// and be careful to not use pointers into potentially freed text[]!
1694static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001695{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001696 uintptr_t bias;
1697 bias = text_hole_make(p, 1);
1698 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001699 *p = c;
1700 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001701 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001702}
1703
Denis Vlasenko33875382008-06-21 20:31:50 +00001704static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001705{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001706 char *save_dot, *p, *q, *t;
1707 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001708
1709 save_dot = dot;
1710 p = q = dot;
1711
1712 if (strchr("cdy><", c)) {
1713 // these cmds operate on whole lines
1714 p = q = begin_line(p);
1715 for (cnt = 1; cnt < cmdcnt; cnt++) {
1716 q = next_line(q);
1717 }
1718 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001719 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001720 // These cmds operate on char positions
1721 do_cmd(c); // execute movement cmd
1722 q = dot;
1723 } else if (strchr("wW", c)) {
1724 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001725 // if we are at the next word's first char
1726 // step back one char
1727 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001728 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001729 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1730 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1731 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001732 if (dot > text && *dot == '\n')
1733 dot--; // stay off NL
1734 q = dot;
1735 } else if (strchr("H-k{", c)) {
1736 // these operate on multi-lines backwards
1737 q = end_line(dot); // find NL
1738 do_cmd(c); // execute movement cmd
1739 dot_begin();
1740 p = dot;
1741 } else if (strchr("L+j}\r\n", c)) {
1742 // these operate on multi-lines forwards
1743 p = begin_line(dot);
1744 do_cmd(c); // execute movement cmd
1745 dot_end(); // find NL
1746 q = dot;
1747 } else {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001748 // nothing -- this causes any other values of c to
1749 // represent the one-character range under the
1750 // cursor. this is correct for ' ' and 'l', but
1751 // perhaps no others.
1752 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001753 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001754 if (q < p) {
1755 t = q;
1756 q = p;
1757 p = t;
1758 }
1759
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001760 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001761 if (q > p && strchr("^0bBh\b\177", c)) q--;
1762
1763 multiline = 0;
1764 for (t = p; t <= q; t++) {
1765 if (*t == '\n') {
1766 multiline = 1;
1767 break;
1768 }
1769 }
1770
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001771 *start = p;
1772 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001773 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001774 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001775}
1776
Denis Vlasenko33875382008-06-21 20:31:50 +00001777static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001778{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001779 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001780 int test, inc;
1781
1782 inc = dir;
1783 c = c0 = p[0];
1784 ci = p[inc];
1785 test = 0;
1786
1787 if (type == S_BEFORE_WS) {
1788 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001789 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001790 }
1791 if (type == S_TO_WS) {
1792 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001793 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001794 }
1795 if (type == S_OVER_WS) {
1796 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001797 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001798 }
1799 if (type == S_END_PUNCT) {
1800 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001801 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001802 }
1803 if (type == S_END_ALNUM) {
1804 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001805 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001806 }
1807 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001808 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001809}
1810
Denis Vlasenko33875382008-06-21 20:31:50 +00001811static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001812{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001813 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001814
1815 while (st_test(p, type, dir, &c)) {
1816 // make sure we limit search to correct number of lines
1817 if (c == '\n' && --linecnt < 1)
1818 break;
1819 if (dir >= 0 && p >= end - 1)
1820 break;
1821 if (dir < 0 && p <= text)
1822 break;
1823 p += dir; // move to next char
1824 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001825 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001826}
1827
1828// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00001829static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001830{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001831 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001832 int dir, level;
1833
1834 match = ')';
1835 level = 1;
1836 dir = 1; // assume forward
1837 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001838 case '(': match = ')'; break;
1839 case '[': match = ']'; break;
1840 case '{': match = '}'; break;
1841 case ')': match = '('; dir = -1; break;
1842 case ']': match = '['; dir = -1; break;
1843 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001844 }
1845 for (q = p + dir; text <= q && q < end; q += dir) {
1846 // look for match, count levels of pairs (( ))
1847 if (*q == c)
1848 level++; // increase pair levels
1849 if (*q == match)
1850 level--; // reduce pair level
1851 if (level == 0)
1852 break; // found matching pair
1853 }
1854 if (level != 0)
1855 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001856 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001857}
1858
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001859#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001860// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00001861static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001862{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001863 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001864
1865 // we found half of a pair
1866 q = find_pair(p, *p); // get loc of matching char
1867 if (q == NULL) {
1868 indicate_error('3'); // no matching char
1869 } else {
1870 // "q" now points to matching pair
1871 save_dot = dot; // remember where we are
1872 dot = q; // go to new loc
1873 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001874 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001875 dot = save_dot; // go back to old loc
1876 refresh(FALSE);
1877 }
1878}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001879#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001880
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001881// open a hole in text[]
1882// might reallocate text[]! use p += text_hole_make(p, ...),
1883// and be careful to not use pointers into potentially freed text[]!
1884static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001885{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001886 uintptr_t bias = 0;
1887
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001888 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001889 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001890 end += size; // adjust the new END
1891 if (end >= (text + text_size)) {
1892 char *new_text;
1893 text_size += end - (text + text_size) + 10240;
1894 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001895 bias = (new_text - text);
1896 screenbegin += bias;
1897 dot += bias;
1898 end += bias;
1899 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001900 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001901 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00001902 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001903 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00001904 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001905 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001906}
1907
1908// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00001909static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001910{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001911 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001912 int cnt, hole_size;
1913
1914 // move forwards, from beginning
1915 // assume p <= q
1916 src = q + 1;
1917 dest = p;
1918 if (q < p) { // they are backward- swap them
1919 src = p + 1;
1920 dest = q;
1921 }
1922 hole_size = q - p + 1;
1923 cnt = end - src;
1924 if (src < text || src > end)
1925 goto thd0;
1926 if (dest < text || dest >= end)
1927 goto thd0;
1928 if (src >= end)
1929 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00001930 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001931 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001932 end = end - hole_size; // adjust the new END
1933 if (dest >= end)
1934 dest = end - 1; // make sure dest in below end-1
1935 if (end <= text)
1936 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00001937 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001938 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001939 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001940}
1941
1942// copy text into register, then delete text.
1943// if dist <= 0, do not include, or go past, a NewLine
1944//
Denis Vlasenko33875382008-06-21 20:31:50 +00001945static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001946{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001947 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001948
1949 // make sure start <= stop
1950 if (start > stop) {
1951 // they are backwards, reverse them
1952 p = start;
1953 start = stop;
1954 stop = p;
1955 }
1956 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00001957 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001958 p = start;
1959 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001960 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001961 // dont go past a NewLine
1962 for (; p + 1 <= stop; p++) {
1963 if (p[1] == '\n') {
1964 stop = p; // "stop" just before NewLine
1965 break;
1966 }
1967 }
1968 }
1969 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001970#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001971 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001972#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001973 if (yf == YANKDEL) {
1974 p = text_hole_delete(start, stop);
1975 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001976 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001977}
1978
1979static void show_help(void)
1980{
1981 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001982#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001983 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001984#endif
1985#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001986 "\n\tLast command repeat with \'.\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001987#endif
1988#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00001989 "\n\tLine marking with 'x"
1990 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001991#endif
1992#if ENABLE_FEATURE_VI_READONLY
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001993 "\n\tReadonly if vi is called as \"view\""
1994 "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001995#endif
1996#if ENABLE_FEATURE_VI_SET
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001997 "\n\tSome colon mode commands with \':\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001998#endif
1999#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002000 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002001#endif
2002#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002003 "\n\tSignal catching- ^C"
2004 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002005#endif
2006#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002007 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002008#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002009 );
2010}
2011
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002012#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002013static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002014{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002015 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002016 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002017 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002018 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002019 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002020 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002021 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002022 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002023 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002024}
2025
2026static void end_cmd_q(void)
2027{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002028#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002029 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002030#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002031 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002032}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002033#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002034
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002035#if ENABLE_FEATURE_VI_YANKMARK \
2036 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2037 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002038// might reallocate text[]! use p += string_insert(p, ...),
2039// and be careful to not use pointers into potentially freed text[]!
2040static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002041{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002042 uintptr_t bias;
2043 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002044
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002045 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002046 bias = text_hole_make(p, i);
2047 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002048 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002049#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002050 {
2051 int cnt;
2052 for (cnt = 0; *s != '\0'; s++) {
2053 if (*s == '\n')
2054 cnt++;
2055 }
2056 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2057 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002058#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002059 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002060}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002061#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002062
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002063#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002064static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002065{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002066 int cnt = q - p;
2067 if (cnt < 0) { // they are backwards- reverse them
2068 p = q;
2069 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002070 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002071 free(reg[dest]); // if already a yank register, free it
2072 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002073 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002074}
2075
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002076static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002077{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002078 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002079
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002080 c = 'D'; // default to D-reg
2081 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002082 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002083 if (YDreg == 26)
2084 c = 'D';
2085 if (YDreg == 27)
2086 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002087 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002088}
2089
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002090static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002091{
2092 // A context is defined to be "modifying text"
2093 // Any modifying command establishes a new context.
2094
2095 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002096 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002097 // we are trying to modify text[]- make this the current context
2098 mark[27] = mark[26]; // move cur to prev
2099 mark[26] = dot; // move local to cur
2100 context_start = prev_line(prev_line(dot));
2101 context_end = next_line(next_line(dot));
2102 //loiter= start_loiter= now;
2103 }
2104 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002105}
2106
Denis Vlasenkof882f082007-12-23 02:36:51 +00002107static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002108{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002109 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002110
2111 // the current context is in mark[26]
2112 // the previous context is in mark[27]
2113 // only swap context if other context is valid
2114 if (text <= mark[27] && mark[27] <= end - 1) {
2115 tmp = mark[27];
2116 mark[27] = mark[26];
2117 mark[26] = tmp;
2118 p = mark[26]; // where we are going- previous context
2119 context_start = prev_line(prev_line(prev_line(p)));
2120 context_end = next_line(next_line(next_line(p)));
2121 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002122 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002123}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002124#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002125
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002126//----- Set terminal attributes --------------------------------
2127static void rawmode(void)
2128{
2129 tcgetattr(0, &term_orig);
2130 term_vi = term_orig;
2131 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
2132 term_vi.c_iflag &= (~IXON & ~ICRNL);
2133 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002134 term_vi.c_cc[VMIN] = 1;
2135 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002136 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002137 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002138}
2139
2140static void cookmode(void)
2141{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002142 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002143 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002144}
2145
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002146//----- Come here when we get a window resize signal ---------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002147#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002148static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002149{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002150 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002151 signal(SIGWINCH, winch_sig);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002152 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Denis Vlasenko621204b2006-10-27 09:03:24 +00002153 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002154 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
2155 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
2156 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002157 new_screen(rows, columns); // get memory for virtual screen
2158 redraw(TRUE); // re-draw the screen
2159}
2160
2161//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002162static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002163{
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002164 rawmode(); // terminal to "raw"
2165 last_status_cksum = 0; // force status update
2166 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002167
2168 signal(SIGTSTP, suspend_sig);
2169 signal(SIGCONT, SIG_DFL);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002170 kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002171}
2172
2173//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002174static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002175{
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002176 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002177 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002178
2179 signal(SIGCONT, cont_sig);
2180 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002181 kill(my_pid, SIGTSTP);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002182}
2183
2184//----- Come here when we get a signal ---------------------------
2185static void catch_sig(int sig)
2186{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002187 signal(SIGINT, catch_sig);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002188 if (sig)
Paul Fox2724fa92008-03-17 15:28:07 +00002189 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002190}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002191#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002192
Denys Vlasenko606291b2009-09-23 23:15:43 +02002193static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002194{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002195 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002196
Denys Vlasenko606291b2009-09-23 23:15:43 +02002197 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002198 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002199 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002200}
2201
2202//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002203static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002204{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002205 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002206
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002207 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002208 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002209 if (c == -1) { // EOF/error
2210 go_bottom_and_clear_to_eol();
2211 cookmode(); // terminal to "cooked"
2212 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002213 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002214 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002215}
2216
2217//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002218static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002219{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002220 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002221
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002222#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002223 if (!adding2q) {
2224 // we are not adding to the q.
2225 // but, we may be reading from a q
2226 if (ioq == 0) {
2227 // there is no current q, read from STDIN
2228 c = readit(); // get the users input
2229 } else {
2230 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002231 // careful with correct sign expansion!
2232 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002233 if (c == '\0') {
2234 // the end of the q, read from STDIN
2235 free(ioq_start);
2236 ioq_start = ioq = 0;
2237 c = readit(); // get the users input
2238 }
2239 }
2240 } else {
2241 // adding STDIN chars to q
2242 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002243 if (lmc_len >= MAX_INPUT_LEN - 1) {
2244 status_line_bold("last_modifying_cmd overrun");
2245 } else {
2246 // add new char to q
2247 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002248 }
2249 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002250#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002251 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002252#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002253 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002254}
2255
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002256// Get input line (uses "status line" area)
2257static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002258{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002259 // char [MAX_INPUT_LEN]
2260#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002261
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002262 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002263 int i;
2264
2265 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002266 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002267 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002268 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002269
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002270 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002271 while (i < MAX_INPUT_LEN) {
2272 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002273 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002274 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002275 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002276 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002277 buf[--i] = '\0';
2278 write1("\b \b"); // erase char on screen
2279 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002280 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002281 } else if (c > 0 && c < 256) { // exclude Unicode
2282 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002283 buf[i] = c;
2284 buf[++i] = '\0';
2285 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002286 }
2287 }
2288 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002289 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002290#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002291}
2292
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002293static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002294{
2295 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002296 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002297
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002298 cnt = -1;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002299 if (fn && fn[0] && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002300 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002301 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002302}
2303
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002304// might reallocate text[]!
2305static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002306{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002307 int cnt = -1;
2308 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002309 struct stat statbuf;
2310
2311 /* Validate file */
2312 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002313 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002314 goto fi0;
2315 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002316 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002317 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002318 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002319 goto fi0;
2320 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002321 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002322 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002323 goto fi0;
2324 }
2325
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002326 // read file to buffer
2327 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002328 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002329 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002330 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002331 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002332 size = statbuf.st_size;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002333 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002334 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002335 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002336 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002337 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002338 } else if (cnt < size) {
2339 // There was a partial read, shrink unused space text[]
2340 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denys Vlasenko6331cf02009-11-13 09:08:27 +01002341 status_line_bold("can't read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002342 }
2343 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002344 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002345 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002346 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002347#if ENABLE_FEATURE_VI_READONLY
2348 if (update_ro_status
2349 && ((access(fn, W_OK) < 0) ||
2350 /* root will always have access()
2351 * so we check fileperms too */
2352 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2353 )
2354 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002355 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002356 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002357#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002358 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002359}
2360
Denis Vlasenko33875382008-06-21 20:31:50 +00002361static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002362{
2363 int fd, cnt, charcnt;
2364
2365 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002366 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002367 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002368 }
2369 charcnt = 0;
Denis Vlasenko8abae882008-05-03 11:35:59 +00002370 /* By popular request we do not open file with O_TRUNC,
2371 * but instead ftruncate() it _after_ successful write.
2372 * Might reduce amount of data lost on power fail etc.
2373 */
2374 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002375 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002376 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002377 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002378 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002379 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002380 if (charcnt == cnt) {
2381 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002382 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002383 } else {
2384 charcnt = 0;
2385 }
2386 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002387 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002388}
2389
2390//----- Terminal Drawing ---------------------------------------
2391// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002392// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002393// screen coordinates
2394// 0,0 ... 0,79
2395// 1,0 ... 1,79
2396// . ... .
2397// . ... .
2398// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002399// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002400
2401//----- Move the cursor to row x col (count from 0, not 1) -------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002402static void place_cursor(int row, int col, int optimize)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002403{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002404 char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
Denis Vlasenko7b54dc72008-07-17 21:32:32 +00002405#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2406 enum {
2407 SZ_UP = sizeof(CMup),
2408 SZ_DN = sizeof(CMdown),
2409 SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN,
2410 };
2411 char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size
2412#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002413 char *cm;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002414
2415 if (row < 0) row = 0;
2416 if (row >= rows) row = rows - 1;
2417 if (col < 0) col = 0;
2418 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002419
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002420 //----- 1. Try the standard terminal ESC sequence
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002421 sprintf(cm1, CMrc, row + 1, col + 1);
2422 cm = cm1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002423
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002424#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002425 if (optimize && col < 16) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002426 char *screenp;
2427 int Rrow = last_row;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002428 int diff = Rrow - row;
2429
2430 if (diff < -5 || diff > 5)
2431 goto skip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002432
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002433 //----- find the minimum # of chars to move cursor -------------
2434 //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
2435 cm2[0] = '\0';
2436
2437 // move to the correct row
2438 while (row < Rrow) {
2439 // the cursor has to move up
2440 strcat(cm2, CMup);
2441 Rrow--;
2442 }
2443 while (row > Rrow) {
2444 // the cursor has to move down
2445 strcat(cm2, CMdown);
2446 Rrow++;
2447 }
2448
2449 // now move to the correct column
2450 strcat(cm2, "\r"); // start at col 0
2451 // just send out orignal source char to get to correct place
2452 screenp = &screen[row * columns]; // start of screen line
2453 strncat(cm2, screenp, col);
2454
2455 // pick the shortest cursor motion to send out
2456 if (strlen(cm2) < strlen(cm)) {
2457 cm = cm2;
2458 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002459 skip: ;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002460 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002461 last_row = row;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002462#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002463 write1(cm);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002464}
2465
2466//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002467static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002468{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002469 write1(Ceol); // Erase from cursor to end of line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002470}
2471
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002472static void go_bottom_and_clear_to_eol(void)
2473{
2474 place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
2475 clear_to_eol(); // erase to end of line
2476}
2477
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002478//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002479static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002480{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002481 write1(Ceos); // Erase from cursor to end of screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002482}
2483
2484//----- Start standout mode ------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002485static void standout_start(void) // send "start reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002486{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002487 write1(SOs); // Start reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002488}
2489
2490//----- End standout mode --------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002491static void standout_end(void) // send "end reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002492{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002493 write1(SOn); // End reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002494}
2495
2496//----- Flash the screen --------------------------------------
2497static void flash(int h)
2498{
2499 standout_start(); // send "start reverse video" sequence
2500 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002501 mysleep(h);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002502 standout_end(); // send "end reverse video" sequence
2503 redraw(TRUE);
2504}
2505
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002506static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002507{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002508#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002509 if (crashme > 0)
2510 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002511#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002512 if (!err_method) {
2513 write1(bell); // send out a bell character
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002514 } else {
2515 flash(10);
2516 }
2517}
2518
2519//----- Screen[] Routines --------------------------------------
2520//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002521static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002522{
2523 memset(screen, ' ', screensize); // clear new screen
2524}
2525
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002526static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002527{
2528 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002529 char *e = buf + count;
2530
Paul Fox8552aec2005-09-16 12:20:05 +00002531 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002532 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002533 return sum;
2534}
2535
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002536//----- Draw the status line at bottom of the screen -------------
2537static void show_status_line(void)
2538{
Paul Foxc3504852005-09-16 12:48:18 +00002539 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002540
Paul Fox8552aec2005-09-16 12:20:05 +00002541 // either we already have an error or status message, or we
2542 // create one.
2543 if (!have_status_msg) {
2544 cnt = format_edit_status();
2545 cksum = bufsum(status_buffer, cnt);
2546 }
2547 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002548 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002549 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002550 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002551 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002552 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002553 (columns - 1) ) {
2554 have_status_msg = 0;
2555 Hit_Return();
2556 }
2557 have_status_msg = 0;
2558 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002559 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2560 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002561 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002562}
2563
2564//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002565// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002566static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002567{
2568 va_list args;
2569
2570 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002571 strcpy(status_buffer, SOs); // Terminal standout mode on
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002572 vsprintf(status_buffer + sizeof(SOs)-1, format, args);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002573 strcat(status_buffer, SOn); // Terminal standout mode off
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002574 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002575
2576 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002577}
2578
Paul Fox8552aec2005-09-16 12:20:05 +00002579// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002580static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002581{
2582 va_list args;
2583
2584 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002585 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002586 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002587
2588 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002589}
2590
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002591// copy s to buf, convert unprintable
2592static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002593{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002594 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002595 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002596
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002597 buf[0] = '\0';
2598 if (!s[0])
2599 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002600
2601 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002602 for (; *s; s++) {
2603 int c_is_no_print;
2604
2605 c = *s;
2606 c_is_no_print = (c & 0x80) && !Isprint(c);
2607 if (c_is_no_print) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002608 strcpy(d, SOn);
2609 d += sizeof(SOn)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002610 c = '.';
2611 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002612 if (c < ' ' || c == 0x7f) {
2613 *d++ = '^';
2614 c |= '@'; /* 0x40 */
2615 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002616 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002617 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002618 *d++ = c;
2619 *d = '\0';
2620 if (c_is_no_print) {
2621 strcpy(d, SOs);
2622 d += sizeof(SOs)-1;
2623 }
2624 if (*s == '\n') {
2625 *d++ = '$';
2626 *d = '\0';
2627 }
2628 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002629 break;
2630 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002631}
2632
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002633static void not_implemented(const char *s)
2634{
2635 char buf[MAX_INPUT_LEN];
2636
2637 print_literal(buf, s);
2638 status_line_bold("\'%s\' is not implemented", buf);
2639}
2640
2641// show file status on status line
2642static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002643{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002644 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002645
2646#define tot format_edit_status__tot
2647
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002648 int cur, percent, ret, trunc_at;
2649
Paul Fox8552aec2005-09-16 12:20:05 +00002650 // file_modified is now a counter rather than a flag. this
2651 // helps reduce the amount of line counting we need to do.
2652 // (this will cause a mis-reporting of modified status
2653 // once every MAXINT editing operations.)
2654
2655 // it would be nice to do a similar optimization here -- if
2656 // we haven't done a motion that could have changed which line
2657 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002658 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002659
2660 // reduce counting -- the total lines can't have
2661 // changed if we haven't done any edits.
2662 if (file_modified != last_file_modified) {
2663 tot = cur + count_lines(dot, end - 1) - 1;
2664 last_file_modified = file_modified;
2665 }
2666
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002667 // current line percent
2668 // ------------- ~~ ----------
2669 // total lines 100
2670 if (tot > 0) {
2671 percent = (100 * cur) / tot;
2672 } else {
2673 cur = tot = 0;
2674 percent = 100;
2675 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002676
Paul Fox8552aec2005-09-16 12:20:05 +00002677 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2678 columns : STATUS_BUFFER_LEN-1;
2679
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002680 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002681#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002682 "%c %s%s%s %d/%d %d%%",
2683#else
2684 "%c %s%s %d/%d %d%%",
2685#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002686 cmd_mode_indicator[cmd_mode & 3],
2687 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002688#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002689 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002690#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002691 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002692 cur, tot, percent);
2693
2694 if (ret >= 0 && ret < trunc_at)
2695 return ret; /* it all fit */
2696
2697 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002698#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002699}
2700
2701//----- Force refresh of all Lines -----------------------------
2702static void redraw(int full_screen)
2703{
2704 place_cursor(0, 0, FALSE); // put cursor in correct place
Denis Vlasenkob1759462008-06-20 20:20:54 +00002705 clear_to_eos(); // tell terminal to erase display
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002706 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002707 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002708 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002709 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002710}
2711
2712//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002713static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002714{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002715 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002716 int co;
2717 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002718 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002719
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002720 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002721 co = 0;
2722 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002723 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002724 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002725 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002726 if (c == '\n')
2727 break;
2728 if ((c & 0x80) && !Isprint(c)) {
2729 c = '.';
2730 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002731 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002732 if (c == '\t') {
2733 c = ' ';
2734 // co % 8 != 7
2735 while ((co % tabstop) != (tabstop - 1)) {
2736 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002737 }
2738 } else {
2739 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002740 if (c == 0x7f)
2741 c = '?';
2742 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002743 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002744 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002745 }
2746 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002747 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002748 // discard scrolled-off-to-the-left portion,
2749 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002750 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002751 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002752 co -= tabstop;
2753 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002754 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002755 if (src >= end)
2756 break;
2757 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002758 // check "short line, gigantic offset" case
2759 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002760 ofs = co;
2761 // discard last scrolled off part
2762 co -= ofs;
2763 dest += ofs;
2764 // fill the rest with spaces
2765 if (co < columns)
2766 memset(&dest[co], ' ', columns - co);
2767 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002768}
2769
2770//----- Refresh the changed screen lines -----------------------
2771// Copy the source line from text[] into the buffer and note
2772// if the current screenline is different from the new buffer.
2773// If they differ then that line needs redrawing on the terminal.
2774//
2775static void refresh(int full_screen)
2776{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002777#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002778
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002779 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002780 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002781
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002782 if (ENABLE_FEATURE_VI_WIN_RESIZE) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002783 unsigned c = columns, r = rows;
Rob Landleye5e1a102006-06-21 01:15:36 +00002784 get_terminal_width_height(0, &columns, &rows);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002785 if (rows > MAX_SCR_ROWS) rows = MAX_SCR_ROWS;
2786 if (columns > MAX_SCR_COLS) columns = MAX_SCR_COLS;
2787 full_screen |= (c - columns) | (r - rows);
2788 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002789 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2790 tp = screenbegin; // index into text[] of top line
2791
2792 // compare text[] to screen[] and mark screen[] lines that need updating
2793 for (li = 0; li < rows - 1; li++) {
2794 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002795 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002796 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002797 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002798
2799 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002800 if (tp < end) {
2801 char *t = memchr(tp, '\n', end - tp);
2802 if (!t) t = end - 1;
2803 tp = t + 1;
2804 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002805
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002806 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002807 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002808 cs = 0;
2809 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002810 sp = &screen[li * columns]; // start of screen line
2811 if (full_screen) {
2812 // force re-draw of every single column from 0 - columns-1
2813 goto re0;
2814 }
2815 // compare newly formatted buffer with virtual screen
2816 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002817 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002818 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002819 changed = TRUE; // mark for redraw
2820 break;
2821 }
2822 }
2823
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002824 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002825 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002826 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002827 changed = TRUE; // mark for redraw
2828 break;
2829 }
2830 }
2831 // now, cs is index of first diff, and ce is index of last diff
2832
2833 // if horz offset has changed, force a redraw
2834 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002835 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002836 changed = TRUE;
2837 }
2838
2839 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002840 if (cs < 0) cs = 0;
2841 if (ce > columns - 1) ce = columns - 1;
2842 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002843 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002844 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002845 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002846 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002847
2848 // move cursor to column of first change
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002849 //if (offset != old_offset) {
2850 // // place_cursor is still too stupid
2851 // // to handle offsets correctly
2852 // place_cursor(li, cs, FALSE);
2853 //} else {
2854 place_cursor(li, cs, TRUE);
2855 //}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002856
2857 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002858 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002859 }
2860 }
2861
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002862 place_cursor(crow, ccol, TRUE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002863
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002864 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002865#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002866}
2867
Eric Andersen3f980402001-04-04 17:31:15 +00002868//---------------------------------------------------------------------
2869//----- the Ascii Chart -----------------------------------------------
2870//
2871// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
2872// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
2873// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
2874// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
2875// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
2876// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
2877// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
2878// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
2879// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
2880// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
2881// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
2882// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
2883// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
2884// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
2885// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
2886// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
2887//---------------------------------------------------------------------
2888
2889//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002890static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00002891{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002892 const char *msg = msg; // for compiler
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002893 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002894 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00002895 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002896 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002897 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00002898
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002899// c1 = c; // quiet the compiler
2900// cnt = yf = 0; // quiet the compiler
2901// msg = p = q = save_dot = buf; // quiet the compiler
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002902 memset(buf, '\0', 12);
Eric Andersenbff7a602001-11-17 07:15:43 +00002903
Paul Fox8552aec2005-09-16 12:20:05 +00002904 show_status_line();
2905
Eric Andersenbff7a602001-11-17 07:15:43 +00002906 /* if this is a cursor key, skip these checks */
2907 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002908 case KEYCODE_UP:
2909 case KEYCODE_DOWN:
2910 case KEYCODE_LEFT:
2911 case KEYCODE_RIGHT:
2912 case KEYCODE_HOME:
2913 case KEYCODE_END:
2914 case KEYCODE_PAGEUP:
2915 case KEYCODE_PAGEDOWN:
2916 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00002917 goto key_cmd_mode;
2918 }
2919
Eric Andersen3f980402001-04-04 17:31:15 +00002920 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002921 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002922 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00002923 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00002924 // we are 'R'eplacing the current *dot with new char
2925 if (*dot == '\n') {
2926 // don't Replace past E-o-l
2927 cmd_mode = 1; // convert to insert
2928 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002929 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00002930 if (c != 27)
2931 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
2932 dot = char_insert(dot, c); // insert new char
2933 }
2934 goto dc1;
2935 }
2936 }
2937 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00002938 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002939 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00002940 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002941 if (1 <= c || Isprint(c)) {
2942 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00002943 }
2944 goto dc1;
2945 }
2946
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002947 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00002948 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00002949 //case 0x01: // soh
2950 //case 0x09: // ht
2951 //case 0x0b: // vt
2952 //case 0x0e: // so
2953 //case 0x0f: // si
2954 //case 0x10: // dle
2955 //case 0x11: // dc1
2956 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002957#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00002958 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00002959 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00002960 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002961#endif
Eric Andersen822c3832001-05-07 17:37:43 +00002962 //case 0x16: // syn
2963 //case 0x17: // etb
2964 //case 0x18: // can
2965 //case 0x1c: // fs
2966 //case 0x1d: // gs
2967 //case 0x1e: // rs
2968 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002969 //case '!': // !-
2970 //case '#': // #-
2971 //case '&': // &-
2972 //case '(': // (-
2973 //case ')': // )-
2974 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002975 //case '=': // =-
2976 //case '@': // @-
2977 //case 'F': // F-
2978 //case 'K': // K-
2979 //case 'Q': // Q-
2980 //case 'S': // S-
2981 //case 'T': // T-
2982 //case 'V': // V-
2983 //case '[': // [-
2984 //case '\\': // \-
2985 //case ']': // ]-
2986 //case '_': // _-
2987 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00002988 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002989 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02002990 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00002991 buf[0] = c;
2992 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002993 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00002994 end_cmd_q(); // stop adding to q
2995 case 0x00: // nul- ignore
2996 break;
2997 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002998 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00002999 dot_scroll(rows - 2, -1);
3000 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003001 case 4: // ctrl-D scroll down half screen
3002 dot_scroll((rows - 2) / 2, 1);
3003 break;
3004 case 5: // ctrl-E scroll down one line
3005 dot_scroll(1, 1);
3006 break;
3007 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003008 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003009 dot_scroll(rows - 2, 1);
3010 break;
3011 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00003012 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003013 break;
3014 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003015 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003016 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003017 case 0x7f: // DEL- move left (This may be ERASE char)
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003018 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003019 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003020 }
Eric Andersen3f980402001-04-04 17:31:15 +00003021 dot_left();
3022 break;
3023 case 10: // Newline ^J
3024 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003025 case KEYCODE_DOWN: // cursor key Down
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003026 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003027 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003028 }
Eric Andersen3f980402001-04-04 17:31:15 +00003029 dot_next(); // go to next B-o-l
3030 dot = move_to_col(dot, ccol + offset); // try stay in same col
3031 break;
3032 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003033 case 18: // ctrl-R force redraw
Eric Andersen822c3832001-05-07 17:37:43 +00003034 place_cursor(0, 0, FALSE); // put cursor in correct place
Eric Andersen3f980402001-04-04 17:31:15 +00003035 clear_to_eos(); // tel terminal to erase display
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003036 mysleep(10);
Eric Andersen3f980402001-04-04 17:31:15 +00003037 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003038 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003039 refresh(TRUE); // this will redraw the entire display
3040 break;
3041 case 13: // Carriage Return ^M
3042 case '+': // +- goto next line
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003043 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003044 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003045 }
Eric Andersen3f980402001-04-04 17:31:15 +00003046 dot_next();
3047 dot_skip_over_ws();
3048 break;
3049 case 21: // ctrl-U scroll up half screen
3050 dot_scroll((rows - 2) / 2, -1);
3051 break;
3052 case 25: // ctrl-Y scroll up one line
3053 dot_scroll(1, -1);
3054 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003055 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003056 if (cmd_mode == 0)
3057 indicate_error(c);
3058 cmd_mode = 0; // stop insrting
3059 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003060 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003061 break;
3062 case ' ': // move right
3063 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003064 case KEYCODE_RIGHT: // Cursor Key Right
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003065 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003066 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003067 }
Eric Andersen3f980402001-04-04 17:31:15 +00003068 dot_right();
3069 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003070#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003071 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003072 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3073 if ((unsigned)c1 <= 25) { // a-z?
3074 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003075 } else {
3076 indicate_error(c);
3077 }
3078 break;
3079 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003080 c1 = (get_one_char() | 0x20) - 'a';
3081 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003082 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003083 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003084 if (text <= q && q < end) {
3085 dot = q;
3086 dot_begin(); // go to B-o-l
3087 dot_skip_over_ws();
3088 }
3089 } else if (c1 == '\'') { // goto previous context
3090 dot = swap_context(dot); // swap current and previous context
3091 dot_begin(); // go to B-o-l
3092 dot_skip_over_ws();
3093 } else {
3094 indicate_error(c);
3095 }
3096 break;
3097 case 'm': // m- Mark a line
3098 // this is really stupid. If there are any inserts or deletes
3099 // between text[0] and dot then this mark will not point to the
3100 // correct location! It could be off by many lines!
3101 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003102 c1 = (get_one_char() | 0x20) - 'a';
3103 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003104 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003105 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003106 } else {
3107 indicate_error(c);
3108 }
3109 break;
3110 case 'P': // P- Put register before
3111 case 'p': // p- put register after
3112 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003113 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003114 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003115 break;
3116 }
3117 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003118 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003119 if (c == 'P') {
3120 dot_begin(); // putting lines- Put above
3121 }
3122 if (c == 'p') {
3123 // are we putting after very last line?
3124 if (end_line(dot) == (end - 1)) {
3125 dot = end; // force dot to end of text[]
3126 } else {
3127 dot_next(); // next line, then put before
3128 }
3129 }
3130 } else {
3131 if (c == 'p')
3132 dot_right(); // move to right, can move to NL
3133 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003134 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003135 end_cmd_q(); // stop adding to q
3136 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003137 case 'U': // U- Undo; replace current line with original version
3138 if (reg[Ureg] != 0) {
3139 p = begin_line(dot);
3140 q = end_line(dot);
3141 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003142 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003143 dot = p;
3144 dot_skip_over_ws();
3145 }
3146 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003147#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003148 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003149 case KEYCODE_END: // Cursor Key End
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003150 if (--cmdcnt > 0) {
3151 dot_next();
Eric Andersen3f980402001-04-04 17:31:15 +00003152 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003153 }
Glenn L McGrathee829062004-01-21 10:59:45 +00003154 dot = end_line(dot);
Eric Andersen3f980402001-04-04 17:31:15 +00003155 break;
3156 case '%': // %- find matching char of pair () [] {}
3157 for (q = dot; q < end && *q != '\n'; q++) {
3158 if (strchr("()[]{}", *q) != NULL) {
3159 // we found half of a pair
3160 p = find_pair(q, *q);
3161 if (p == NULL) {
3162 indicate_error(c);
3163 } else {
3164 dot = p;
3165 }
3166 break;
3167 }
3168 }
3169 if (*q == '\n')
3170 indicate_error(c);
3171 break;
3172 case 'f': // f- forward to a user specified char
3173 last_forward_char = get_one_char(); // get the search char
3174 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003175 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003176 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003177 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003178 case ';': // ;- look at rest of line for last forward char
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003179 if (--cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003180 do_cmd(';');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003181 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003182 if (last_forward_char == 0)
3183 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003184 q = dot + 1;
3185 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3186 q++;
3187 }
3188 if (*q == last_forward_char)
3189 dot = q;
3190 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003191 case ',': // repeat latest 'f' in opposite direction
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003192 if (--cmdcnt > 0) {
Paul Foxb5ee8db2008-02-14 01:17:01 +00003193 do_cmd(',');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003194 }
Paul Foxb5ee8db2008-02-14 01:17:01 +00003195 if (last_forward_char == 0)
3196 break;
3197 q = dot - 1;
3198 while (q >= text && *q != '\n' && *q != last_forward_char) {
3199 q--;
3200 }
3201 if (q >= text && *q == last_forward_char)
3202 dot = q;
3203 break;
3204
Eric Andersen3f980402001-04-04 17:31:15 +00003205 case '-': // -- goto prev line
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003206 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003207 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003208 }
Eric Andersen3f980402001-04-04 17:31:15 +00003209 dot_prev();
3210 dot_skip_over_ws();
3211 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003212#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003213 case '.': // .- repeat the last modifying command
3214 // Stuff the last_modifying_cmd back into stdin
3215 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003216 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003217 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003218 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003219 }
3220 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003221#endif
3222#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003223 case '?': // /- search for a pattern
3224 case '/': // /- search for a pattern
3225 buf[0] = c;
3226 buf[1] = '\0';
3227 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003228 if (q[0] && !q[1]) {
3229 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003230 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003231 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003232 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003233 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003234 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003235 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003236 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003237 goto dc3; // now find the pattern
3238 }
3239 // user changed mind and erased the "/"- do nothing
3240 break;
3241 case 'N': // N- backward search for last pattern
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003242 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003243 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003244 }
Eric Andersen3f980402001-04-04 17:31:15 +00003245 dir = BACK; // assume BACKWARD search
3246 p = dot - 1;
3247 if (last_search_pattern[0] == '?') {
3248 dir = FORWARD;
3249 p = dot + 1;
3250 }
3251 goto dc4; // now search for pattern
3252 break;
3253 case 'n': // n- repeat search for last pattern
3254 // search rest of text[] starting at next char
3255 // if search fails return orignal "p" not the "p+1" address
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003256 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003257 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003258 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003259 dc3:
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003260 dir = FORWARD; // assume FORWARD search
3261 p = dot + 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003262 if (last_search_pattern[0] == '?') {
3263 dir = BACK;
3264 p = dot - 1;
3265 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003266 dc4:
Eric Andersen3f980402001-04-04 17:31:15 +00003267 q = char_search(p, last_search_pattern + 1, dir, FULL);
3268 if (q != NULL) {
3269 dot = q; // good search, update "dot"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003270 msg = "";
Eric Andersen3f980402001-04-04 17:31:15 +00003271 goto dc2;
3272 }
3273 // no pattern found between "dot" and "end"- continue at top
3274 p = text;
3275 if (dir == BACK) {
3276 p = end - 1;
3277 }
3278 q = char_search(p, last_search_pattern + 1, dir, FULL);
3279 if (q != NULL) { // found something
3280 dot = q; // found new pattern- goto it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003281 msg = "search hit BOTTOM, continuing at TOP";
Eric Andersen3f980402001-04-04 17:31:15 +00003282 if (dir == BACK) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003283 msg = "search hit TOP, continuing at BOTTOM";
Eric Andersen3f980402001-04-04 17:31:15 +00003284 }
3285 } else {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003286 msg = "Pattern not found";
Eric Andersen3f980402001-04-04 17:31:15 +00003287 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003288 dc2:
3289 if (*msg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003290 status_line_bold("%s", msg);
Eric Andersen3f980402001-04-04 17:31:15 +00003291 break;
3292 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003293 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003294 if (q != NULL) { // found blank line
3295 dot = next_line(q); // move to next blank line
3296 }
3297 break;
3298 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003299 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003300 if (q != NULL) { // found blank line
3301 dot = next_line(q); // move to next blank line
3302 }
3303 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003304#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003305 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003306 case '1': // 1-
3307 case '2': // 2-
3308 case '3': // 3-
3309 case '4': // 4-
3310 case '5': // 5-
3311 case '6': // 6-
3312 case '7': // 7-
3313 case '8': // 8-
3314 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003315 if (c == '0' && cmdcnt < 1) {
3316 dot_begin(); // this was a standalone zero
3317 } else {
3318 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3319 }
3320 break;
3321 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003322 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003323#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003324 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003325#else
Eric Andersen822c3832001-05-07 17:37:43 +00003326 if (*p == ':')
3327 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003328 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003329 if (cnt <= 0)
3330 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003331 if (strncmp(p, "quit", cnt) == 0
3332 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003333 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003334 if (file_modified && p[1] != '!') {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003335 status_line_bold("No write since last change (:quit! overrides)");
Eric Andersen3f980402001-04-04 17:31:15 +00003336 } else {
3337 editing = 0;
3338 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003339 } else if (strncmp(p, "write", cnt) == 0
3340 || strncmp(p, "wq", cnt) == 0
3341 || strncmp(p, "wn", cnt) == 0
3342 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003343 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003344 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003345 if (cnt < 0) {
3346 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003347 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003348 } else {
3349 file_modified = 0;
3350 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003351 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003352 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3353 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3354 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003355 editing = 0;
3356 }
Eric Andersen3f980402001-04-04 17:31:15 +00003357 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003358 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003359 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003360 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003361 dot = find_line(j); // go to line # j
3362 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003363 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003364 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003365 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003366#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003367 break;
3368 case '<': // <- Left shift something
3369 case '>': // >- Right shift something
3370 cnt = count_lines(text, dot); // remember what line we are on
3371 c1 = get_one_char(); // get the type of thing to delete
3372 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003373 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003374 p = begin_line(p);
3375 q = end_line(q);
3376 i = count_lines(p, q); // # of lines we are shifting
3377 for ( ; i > 0; i--, p = next_line(p)) {
3378 if (c == '<') {
3379 // shift left- remove tab or 8 spaces
3380 if (*p == '\t') {
3381 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003382 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003383 } else if (*p == ' ') {
3384 // we should be calculating columns, not just SPACE
3385 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003386 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003387 }
3388 }
3389 } else if (c == '>') {
3390 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003391 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003392 }
3393 }
3394 dot = find_line(cnt); // what line were we on
3395 dot_skip_over_ws();
3396 end_cmd_q(); // stop adding to q
3397 break;
3398 case 'A': // A- append at e-o-l
3399 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003400 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003401 case 'a': // a- append after current char
3402 if (*dot != '\n')
3403 dot++;
3404 goto dc_i;
3405 break;
3406 case 'B': // B- back a blank-delimited Word
3407 case 'E': // E- end of a blank-delimited word
3408 case 'W': // W- forward a blank-delimited word
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003409 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003410 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003411 }
Eric Andersen3f980402001-04-04 17:31:15 +00003412 dir = FORWARD;
3413 if (c == 'B')
3414 dir = BACK;
3415 if (c == 'W' || isspace(dot[dir])) {
3416 dot = skip_thing(dot, 1, dir, S_TO_WS);
3417 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3418 }
3419 if (c != 'W')
3420 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3421 break;
3422 case 'C': // C- Change to e-o-l
3423 case 'D': // D- delete to e-o-l
3424 save_dot = dot;
3425 dot = dollar_line(dot); // move to before NL
3426 // copy text into a register and delete
3427 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3428 if (c == 'C')
3429 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003430#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003431 if (c == 'D')
3432 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003433#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003434 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003435 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003436 c1 = get_one_char();
3437 if (c1 != 'g') {
3438 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003439 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003440 buf[2] = '\0';
3441 not_implemented(buf);
3442 break;
3443 }
3444 if (cmdcnt == 0)
3445 cmdcnt = 1;
3446 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003447 case 'G': // G- goto to a line number (default= E-O-F)
3448 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003449 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003450 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003451 }
3452 dot_skip_over_ws();
3453 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003454 case 'H': // H- goto top line on screen
3455 dot = screenbegin;
3456 if (cmdcnt > (rows - 1)) {
3457 cmdcnt = (rows - 1);
3458 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003459 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003460 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003461 }
Eric Andersen3f980402001-04-04 17:31:15 +00003462 dot_skip_over_ws();
3463 break;
3464 case 'I': // I- insert before first non-blank
3465 dot_begin(); // 0
3466 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003467 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003468 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003469 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003470 dc_i:
Eric Andersen3f980402001-04-04 17:31:15 +00003471 cmd_mode = 1; // start insrting
Eric Andersen3f980402001-04-04 17:31:15 +00003472 break;
3473 case 'J': // J- join current and next lines together
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003474 if (--cmdcnt > 1) {
Eric Andersen3f980402001-04-04 17:31:15 +00003475 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003476 }
Eric Andersen3f980402001-04-04 17:31:15 +00003477 dot_end(); // move to NL
3478 if (dot < end - 1) { // make sure not last char in text[]
3479 *dot++ = ' '; // replace NL with space
Paul Fox8552aec2005-09-16 12:20:05 +00003480 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003481 while (isblank(*dot)) { // delete leading WS
Eric Andersen3f980402001-04-04 17:31:15 +00003482 dot_delete();
3483 }
3484 }
3485 end_cmd_q(); // stop adding to q
3486 break;
3487 case 'L': // L- goto bottom line on screen
3488 dot = end_screen();
3489 if (cmdcnt > (rows - 1)) {
3490 cmdcnt = (rows - 1);
3491 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003492 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003493 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003494 }
Eric Andersen3f980402001-04-04 17:31:15 +00003495 dot_begin();
3496 dot_skip_over_ws();
3497 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003498 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003499 dot = screenbegin;
3500 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3501 dot = next_line(dot);
3502 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003503 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003504 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003505 p = begin_line(dot);
3506 if (p[-1] == '\n') {
3507 dot_prev();
3508 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3509 dot_end();
3510 dot = char_insert(dot, '\n');
3511 } else {
3512 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003513 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003514 dot_prev(); // -
3515 }
3516 goto dc_i;
3517 break;
3518 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003519 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003520 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003521 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003522 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003523 c = 'x';
3524 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003525 case 'X': // X- delete char before dot
3526 case 'x': // x- delete the current char
3527 case 's': // s- substitute the current char
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003528 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003529 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003530 }
Eric Andersen3f980402001-04-04 17:31:15 +00003531 dir = 0;
3532 if (c == 'X')
3533 dir = -1;
3534 if (dot[dir] != '\n') {
3535 if (c == 'X')
3536 dot--; // delete prev char
3537 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3538 }
3539 if (c == 's')
3540 goto dc_i; // start insrting
3541 end_cmd_q(); // stop adding to q
3542 break;
3543 case 'Z': // Z- if modified, {write}; exit
3544 // ZZ means to save file (if necessary), then exit
3545 c1 = get_one_char();
3546 if (c1 != 'Z') {
3547 indicate_error(c);
3548 break;
3549 }
Paul Foxf0305b72006-03-28 14:18:21 +00003550 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003551 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003552 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003553 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003554 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003555 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003556 if (cnt < 0) {
3557 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003558 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003559 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003560 editing = 0;
3561 }
3562 } else {
3563 editing = 0;
3564 }
3565 break;
3566 case '^': // ^- move to first non-blank on line
3567 dot_begin();
3568 dot_skip_over_ws();
3569 break;
3570 case 'b': // b- back a word
3571 case 'e': // e- end of word
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003572 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003573 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003574 }
Eric Andersen3f980402001-04-04 17:31:15 +00003575 dir = FORWARD;
3576 if (c == 'b')
3577 dir = BACK;
3578 if ((dot + dir) < text || (dot + dir) > end - 1)
3579 break;
3580 dot += dir;
3581 if (isspace(*dot)) {
3582 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3583 }
3584 if (isalnum(*dot) || *dot == '_') {
3585 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3586 } else if (ispunct(*dot)) {
3587 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3588 }
3589 break;
3590 case 'c': // c- change something
3591 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003592#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003593 case 'y': // y- yank something
3594 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003595#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003596 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003597 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003598 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003599#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003600 if (c == 'y' || c == 'Y')
3601 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003602#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003603 c1 = 'y';
3604 if (c != 'Y')
3605 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003606 // determine range, and whether it spans lines
3607 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003608 if (c1 == 27) { // ESC- user changed mind and wants out
3609 c = c1 = 27; // Escape- do nothing
3610 } else if (strchr("wW", c1)) {
3611 if (c == 'c') {
3612 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003613 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003614 if (q <= text || q[-1] == '\n')
3615 break;
3616 q--;
3617 }
3618 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003619 dot = yank_delete(p, q, ml, yf); // delete word
3620 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3621 // partial line copy text into a register and delete
3622 dot = yank_delete(p, q, ml, yf); // delete word
3623 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3624 // whole line copy text into a register and delete
3625 dot = yank_delete(p, q, ml, yf); // delete lines
3626 whole = 1;
3627 } else {
3628 // could not recognize object
3629 c = c1 = 27; // error-
3630 ml = 0;
3631 indicate_error(c);
3632 }
3633 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003634 if (c == 'c') {
3635 dot = char_insert(dot, '\n');
3636 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003637 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003638 dot_prev();
3639 }
3640 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003641 dot_begin();
3642 dot_skip_over_ws();
3643 }
Eric Andersen3f980402001-04-04 17:31:15 +00003644 }
3645 if (c1 != 27) {
3646 // if CHANGING, not deleting, start inserting after the delete
3647 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003648 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003649 goto dc_i; // start inserting
3650 }
3651 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003652 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003653 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003654#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003655 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003656 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003657 }
3658 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003659 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003660 for (cnt = 0; p <= q; p++) {
3661 if (*p == '\n')
3662 cnt++;
3663 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003664 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003665 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003666#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003667 end_cmd_q(); // stop adding to q
3668 }
3669 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003670 }
Eric Andersen3f980402001-04-04 17:31:15 +00003671 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003672 case KEYCODE_UP: // cursor key Up
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003673 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003674 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003675 }
Eric Andersen3f980402001-04-04 17:31:15 +00003676 dot_prev();
3677 dot = move_to_col(dot, ccol + offset); // try stay in same col
3678 break;
3679 case 'r': // r- replace the current char with user input
3680 c1 = get_one_char(); // get the replacement char
3681 if (*dot != '\n') {
3682 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003683 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003684 }
3685 end_cmd_q(); // stop adding to q
3686 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003687 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003688 last_forward_char = get_one_char();
3689 do_cmd(';');
3690 if (*dot == last_forward_char)
3691 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003692 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003693 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003694 case 'w': // w- forward a word
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003695 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003696 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003697 }
Eric Andersen3f980402001-04-04 17:31:15 +00003698 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3699 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3700 } else if (ispunct(*dot)) { // we are on PUNCT
3701 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3702 }
3703 if (dot < end - 1)
3704 dot++; // move over word
3705 if (isspace(*dot)) {
3706 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3707 }
3708 break;
3709 case 'z': // z-
3710 c1 = get_one_char(); // get the replacement char
3711 cnt = 0;
3712 if (c1 == '.')
3713 cnt = (rows - 2) / 2; // put dot at center
3714 if (c1 == '-')
3715 cnt = rows - 2; // put dot at bottom
3716 screenbegin = begin_line(dot); // start dot at top
3717 dot_scroll(cnt, -1);
3718 break;
3719 case '|': // |- move to column "cmdcnt"
3720 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3721 break;
3722 case '~': // ~- flip the case of letters a-z -> A-Z
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003723 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003724 do_cmd(c);
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003725 }
Eric Andersen3f980402001-04-04 17:31:15 +00003726 if (islower(*dot)) {
3727 *dot = toupper(*dot);
Denis Vlasenkob1759462008-06-20 20:20:54 +00003728 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003729 } else if (isupper(*dot)) {
3730 *dot = tolower(*dot);
Denis Vlasenkob1759462008-06-20 20:20:54 +00003731 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003732 }
3733 dot_right();
3734 end_cmd_q(); // stop adding to q
3735 break;
3736 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003737 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003738 dot_begin();
3739 break;
3740 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003741#if 0
3742 case KEYCODE_FUN1: // Function Key F1
3743 case KEYCODE_FUN2: // Function Key F2
3744 case KEYCODE_FUN3: // Function Key F3
3745 case KEYCODE_FUN4: // Function Key F4
3746 case KEYCODE_FUN5: // Function Key F5
3747 case KEYCODE_FUN6: // Function Key F6
3748 case KEYCODE_FUN7: // Function Key F7
3749 case KEYCODE_FUN8: // Function Key F8
3750 case KEYCODE_FUN9: // Function Key F9
3751 case KEYCODE_FUN10: // Function Key F10
3752 case KEYCODE_FUN11: // Function Key F11
3753 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003754 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003755#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003756 }
3757
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003758 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003759 // if text[] just became empty, add back an empty line
3760 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003761 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003762 dot = text;
3763 }
3764 // it is OK for dot to exactly equal to end, otherwise check dot validity
3765 if (dot != end) {
3766 dot = bound_dot(dot); // make sure "dot" is valid
3767 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003768#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003769 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003770#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003771
3772 if (!isdigit(c))
3773 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3774 cnt = dot - begin_line(dot);
3775 // Try to stay off of the Newline
3776 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3777 dot--;
3778}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003779
Paul Fox35e9c5d2008-03-06 16:26:12 +00003780/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003781#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003782static int totalcmds = 0;
3783static int Mp = 85; // Movement command Probability
3784static int Np = 90; // Non-movement command Probability
3785static int Dp = 96; // Delete command Probability
3786static int Ip = 97; // Insert command Probability
3787static int Yp = 98; // Yank command Probability
3788static int Pp = 99; // Put command Probability
3789static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003790static const char chars[20] = "\t012345 abcdABCD-=.$";
3791static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003792 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003793 "broadcast", "the", "emergency", "of",
3794 "system", "quick", "brown", "fox",
3795 "jumped", "over", "lazy", "dogs",
3796 "back", "January", "Febuary", "March"
3797};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003798static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003799 "You should have received a copy of the GNU General Public License\n",
3800 "char c, cm, *cmd, *cmd1;\n",
3801 "generate a command by percentages\n",
3802 "Numbers may be typed as a prefix to some commands.\n",
3803 "Quit, discarding changes!\n",
3804 "Forced write, if permission originally not valid.\n",
3805 "In general, any ex or ed command (such as substitute or delete).\n",
3806 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3807 "Please get w/ me and I will go over it with you.\n",
3808 "The following is a list of scheduled, committed changes.\n",
3809 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3810 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3811 "Any question about transactions please contact Sterling Huxley.\n",
3812 "I will try to get back to you by Friday, December 31.\n",
3813 "This Change will be implemented on Friday.\n",
3814 "Let me know if you have problems accessing this;\n",
3815 "Sterling Huxley recently added you to the access list.\n",
3816 "Would you like to go to lunch?\n",
3817 "The last command will be automatically run.\n",
3818 "This is too much english for a computer geek.\n",
3819};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003820static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003821 "You should have received a copy of the GNU General Public License\n",
3822 "char c, cm, *cmd, *cmd1;\n",
3823 "generate a command by percentages\n",
3824 "Numbers may be typed as a prefix to some commands.\n",
3825 "Quit, discarding changes!\n",
3826 "Forced write, if permission originally not valid.\n",
3827 "In general, any ex or ed command (such as substitute or delete).\n",
3828 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3829 "Please get w/ me and I will go over it with you.\n",
3830 "The following is a list of scheduled, committed changes.\n",
3831 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3832 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3833 "Any question about transactions please contact Sterling Huxley.\n",
3834 "I will try to get back to you by Friday, December 31.\n",
3835 "This Change will be implemented on Friday.\n",
3836 "Let me know if you have problems accessing this;\n",
3837 "Sterling Huxley recently added you to the access list.\n",
3838 "Would you like to go to lunch?\n",
3839 "The last command will be automatically run.\n",
3840 "This is too much english for a computer geek.\n",
3841};
3842
3843// create a random command to execute
3844static void crash_dummy()
3845{
3846 static int sleeptime; // how long to pause between commands
3847 char c, cm, *cmd, *cmd1;
3848 int i, cnt, thing, rbi, startrbi, percent;
3849
3850 // "dot" movement commands
3851 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3852
3853 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02003854 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003855 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003856 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02003857 readbuffer[0] = 'X';
3858 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003859 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003860 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003861 // generate a command by percentages
3862 percent = (int) lrand48() % 100; // get a number from 0-99
3863 if (percent < Mp) { // Movement commands
3864 // available commands
3865 cmd = cmd1;
3866 M++;
3867 } else if (percent < Np) { // non-movement commands
3868 cmd = "mz<>\'\""; // available commands
3869 N++;
3870 } else if (percent < Dp) { // Delete commands
3871 cmd = "dx"; // available commands
3872 D++;
3873 } else if (percent < Ip) { // Inset commands
3874 cmd = "iIaAsrJ"; // available commands
3875 I++;
3876 } else if (percent < Yp) { // Yank commands
3877 cmd = "yY"; // available commands
3878 Y++;
3879 } else if (percent < Pp) { // Put commands
3880 cmd = "pP"; // available commands
3881 P++;
3882 } else {
3883 // We do not know how to handle this command, try again
3884 U++;
3885 goto cd0;
3886 }
3887 // randomly pick one of the available cmds from "cmd[]"
3888 i = (int) lrand48() % strlen(cmd);
3889 cm = cmd[i];
3890 if (strchr(":\024", cm))
3891 goto cd0; // dont allow colon or ctrl-T commands
3892 readbuffer[rbi++] = cm; // put cmd into input buffer
3893
3894 // now we have the command-
3895 // there are 1, 2, and multi char commands
3896 // find out which and generate the rest of command as necessary
3897 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
3898 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
3899 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
3900 cmd1 = "abcdefghijklmnopqrstuvwxyz";
3901 }
3902 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3903 c = cmd1[thing];
3904 readbuffer[rbi++] = c; // add movement to input buffer
3905 }
3906 if (strchr("iIaAsc", cm)) { // multi-char commands
3907 if (cm == 'c') {
3908 // change some thing
3909 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
3910 c = cmd1[thing];
3911 readbuffer[rbi++] = c; // add movement to input buffer
3912 }
3913 thing = (int) lrand48() % 4; // what thing to insert
3914 cnt = (int) lrand48() % 10; // how many to insert
3915 for (i = 0; i < cnt; i++) {
3916 if (thing == 0) { // insert chars
3917 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
3918 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003919 strcat(readbuffer, words[(int) lrand48() % 20]);
3920 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003921 sleeptime = 0; // how fast to type
3922 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003923 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003924 sleeptime = 0; // how fast to type
3925 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003926 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003927 sleeptime = 0; // how fast to type
3928 }
3929 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003930 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003931 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02003932 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003933 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003934 totalcmds++;
3935 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003936 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003937}
3938
3939// test to see if there are any errors
3940static void crash_test()
3941{
3942 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003943
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003944 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003945 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003946
3947 msg[0] = '\0';
3948 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003949 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003950 }
3951 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003952 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003953 }
3954 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003955 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003956 }
3957 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003958 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003959 }
3960 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003961 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003962 }
3963 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003964 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003965 }
3966
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003967 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00003968 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003969 totalcmds, last_input_char, msg, SOs, SOn);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01003970 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00003971 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003972 if (d[0] == '\n' || d[0] == '\r')
3973 break;
3974 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003975 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003976 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003977 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003978 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003979 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
3980 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
3981 oldtim = tim;
3982 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003983}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003984#endif