blob: ccf53a94b366faf529ee165d78a09f3d681474d1 [file] [log] [blame]
Rob Landleye5e1a102006-06-21 01:15:36 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3f980402001-04-04 17:31:15 +00002/*
3 * tiny vi.c: A small 'vi' clone
4 * Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com>
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen3f980402001-04-04 17:31:15 +00007 */
8
Eric Andersen3f980402001-04-04 17:31:15 +00009/*
Eric Andersen3f980402001-04-04 17:31:15 +000010 * Things To Do:
11 * EXINIT
Eric Andersen1c0d3112001-04-16 15:46:44 +000012 * $HOME/.exrc and ./.exrc
Eric Andersen3f980402001-04-04 17:31:15 +000013 * add magic to search /foo.*bar
14 * add :help command
15 * :map macros
Eric Andersen3f980402001-04-04 17:31:15 +000016 * if mark[] values were line numbers rather than pointers
Tanguy Pruvot823694d2012-11-18 13:20:29 +010017 * 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
Walter Harmsb9ba5802011-06-27 02:59:37 +020024//config:config VI
25//config: bool "vi"
26//config: default y
27//config: help
28//config: 'vi' is a text editor. More specifically, it is the One True
29//config: text editor <grin>. It does, however, have a rather steep
30//config: learning curve. If you are not already comfortable with 'vi'
31//config: you may wish to use something else.
32//config:
33//config:config FEATURE_VI_MAX_LEN
34//config: int "Maximum screen width in vi"
35//config: range 256 16384
36//config: default 4096
37//config: depends on VI
38//config: help
39//config: Contrary to what you may think, this is not eating much.
40//config: Make it smaller than 4k only if you are very limited on memory.
41//config:
42//config:config FEATURE_VI_8BIT
43//config: bool "Allow vi to display 8-bit chars (otherwise shows dots)"
44//config: default n
45//config: depends on VI
46//config: help
47//config: If your terminal can display characters with high bit set,
48//config: you may want to enable this. Note: vi is not Unicode-capable.
49//config: If your terminal combines several 8-bit bytes into one character
50//config: (as in Unicode mode), this will not work properly.
51//config:
52//config:config FEATURE_VI_COLON
53//config: bool "Enable \":\" colon commands (no \"ex\" mode)"
54//config: default y
55//config: depends on VI
56//config: help
57//config: Enable a limited set of colon commands for vi. This does not
58//config: provide an "ex" mode.
59//config:
60//config:config FEATURE_VI_YANKMARK
61//config: bool "Enable yank/put commands and mark cmds"
62//config: default y
63//config: depends on VI
64//config: help
65//config: This will enable you to use yank and put, as well as mark in
66//config: busybox vi.
67//config:
68//config:config FEATURE_VI_SEARCH
69//config: bool "Enable search and replace cmds"
70//config: default y
71//config: depends on VI
72//config: help
73//config: Select this if you wish to be able to do search and replace in
74//config: busybox vi.
75//config:
76//config:config FEATURE_VI_REGEX_SEARCH
77//config: bool "Enable regex in search and replace"
78//config: default n # Uses GNU regex, which may be unavailable. FIXME
79//config: depends on FEATURE_VI_SEARCH
80//config: help
81//config: Use extended regex search.
82//config:
83//config:config FEATURE_VI_USE_SIGNALS
84//config: bool "Catch signals"
85//config: default y
86//config: depends on VI
87//config: help
88//config: Selecting this option will make busybox vi signal aware. This will
89//config: make busybox vi support SIGWINCH to deal with Window Changes, catch
90//config: Ctrl-Z and Ctrl-C and alarms.
91//config:
92//config:config FEATURE_VI_DOT_CMD
93//config: bool "Remember previous cmd and \".\" cmd"
94//config: default y
95//config: depends on VI
96//config: help
97//config: Make busybox vi remember the last command and be able to repeat it.
98//config:
99//config:config FEATURE_VI_READONLY
100//config: bool "Enable -R option and \"view\" mode"
101//config: default y
102//config: depends on VI
103//config: help
104//config: Enable the read-only command line option, which allows the user to
105//config: open a file in read-only mode.
106//config:
107//config:config FEATURE_VI_SETOPTS
108//config: bool "Enable set-able options, ai ic showmatch"
109//config: default y
110//config: depends on VI
111//config: help
112//config: Enable the editor to set some (ai, ic, showmatch) options.
113//config:
114//config:config FEATURE_VI_SET
115//config: bool "Support for :set"
116//config: default y
117//config: depends on VI
118//config: help
119//config: Support for ":set".
120//config:
121//config:config FEATURE_VI_WIN_RESIZE
122//config: bool "Handle window resize"
123//config: default y
124//config: depends on VI
125//config: help
126//config: Make busybox vi behave nicely with terminals that get resized.
127//config:
128//config:config FEATURE_VI_ASK_TERMINAL
129//config: bool "Use 'tell me cursor position' ESC sequence to measure window"
130//config: default y
131//config: depends on VI
132//config: help
133//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
134//config: this option makes vi perform a last-ditch effort to find it:
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200135//config: position cursor to 999,999 and ask terminal to report real
136//config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
Walter Harmsb9ba5802011-06-27 02:59:37 +0200137//config:
138//config: This is not clean but helps a lot on serial lines and such.
Walter Harmsb9ba5802011-06-27 02:59:37 +0200139
140//applet:IF_VI(APPLET(vi, BB_DIR_BIN, BB_SUID_DROP))
141
142//kbuild:lib-$(CONFIG_VI) += vi.o
143
Pere Orga6a3e01d2011-04-01 22:56:30 +0200144//usage:#define vi_trivial_usage
145//usage: "[OPTIONS] [FILE]..."
146//usage:#define vi_full_usage "\n\n"
147//usage: "Edit FILE\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200148//usage: IF_FEATURE_VI_COLON(
Denys Vlasenko5f556fc2012-06-11 01:53:33 +0200149//usage: "\n -c CMD Initial command to run ($EXINIT also available)"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200150//usage: )
151//usage: IF_FEATURE_VI_READONLY(
152//usage: "\n -R Read-only"
153//usage: )
Denys Vlasenko5f556fc2012-06-11 01:53:33 +0200154//usage: "\n -H List available features"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200155
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000156#include "libbb.h"
Denys Vlasenko066f3992011-07-03 03:19:43 +0200157/* Should be after libbb.h: on some systems regex.h needs sys/types.h: */
158#if ENABLE_FEATURE_VI_REGEX_SEARCH
maxwen27116ba2015-08-14 21:41:28 +0200159# include "xregex.h"
Denys Vlasenko066f3992011-07-03 03:19:43 +0200160#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000161
Paul Fox35e9c5d2008-03-06 16:26:12 +0000162/* the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000163#define ENABLE_FEATURE_VI_CRASHME 0
164
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000165
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000166#if ENABLE_LOCALE_SUPPORT
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000167
168#if ENABLE_FEATURE_VI_8BIT
Denys Vlasenkoc2704542009-11-20 19:14:19 +0100169//FIXME: this does not work properly for Unicode anyway
170# define Isprint(c) (isprint)(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000171#else
Denys Vlasenkoc2704542009-11-20 19:14:19 +0100172# define Isprint(c) isprint_asciionly(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000173#endif
174
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000175#else
176
177/* 0x9b is Meta-ESC */
178#if ENABLE_FEATURE_VI_8BIT
Denys Vlasenko066f3992011-07-03 03:19:43 +0200179# define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000180#else
Denys Vlasenko066f3992011-07-03 03:19:43 +0200181# define Isprint(c) ((unsigned char)(c) >= ' ' && (unsigned char)(c) < 0x7f)
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000182#endif
183
184#endif
185
186
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000187enum {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000188 MAX_TABSTOP = 32, // sanity limit
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000189 // User input len. Need not be extra big.
190 // Lines in file being edited *can* be bigger than this.
191 MAX_INPUT_LEN = 128,
192 // Sanity limits. We have only one buffer of this size.
193 MAX_SCR_COLS = CONFIG_FEATURE_VI_MAX_LEN,
194 MAX_SCR_ROWS = CONFIG_FEATURE_VI_MAX_LEN,
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000195};
Eric Andersen3f980402001-04-04 17:31:15 +0000196
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +0200197/* VT102 ESC sequences.
198 * See "Xterm Control Sequences"
199 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
200 */
201/* Inverse/Normal text */
202#define ESC_BOLD_TEXT "\033[7m"
203#define ESC_NORM_TEXT "\033[0m"
204/* Bell */
205#define ESC_BELL "\007"
206/* Clear-to-end-of-line */
207#define ESC_CLEAR2EOL "\033[K"
208/* Clear-to-end-of-screen.
209 * (We use default param here.
210 * Full sequence is "ESC [ <num> J",
211 * <num> is 0/1/2 = "erase below/above/all".)
212 */
213#define ESC_CLEAR2EOS "\033[J"
214/* Cursor to given coordinate (1,1: top left) */
215#define ESC_SET_CURSOR_POS "\033[%u;%uH"
216//UNUSED
217///* Cursor up and down */
218//#define ESC_CURSOR_UP "\033[A"
219//#define ESC_CURSOR_DOWN "\n"
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000220
Denis Vlasenkoded6ad32008-10-14 12:26:30 +0000221#if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
222// cmds modifying text[]
223// vda: removed "aAiIs" as they switch us into insert mode
224// and remembering input for replay after them makes no sense
225static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~";
226#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000227
Rob Landleybc68cd12006-03-10 19:22:06 +0000228enum {
229 YANKONLY = FALSE,
230 YANKDEL = TRUE,
231 FORWARD = 1, // code depends on "1" for array index
232 BACK = -1, // code depends on "-1" for array index
233 LIMITED = 0, // how much of text[] in char_search
234 FULL = 1, // how much of text[] in char_search
Eric Andersen3f980402001-04-04 17:31:15 +0000235
Rob Landleybc68cd12006-03-10 19:22:06 +0000236 S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
237 S_TO_WS = 2, // used in skip_thing() for moving "dot"
238 S_OVER_WS = 3, // used in skip_thing() for moving "dot"
239 S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000240 S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
Rob Landleybc68cd12006-03-10 19:22:06 +0000241};
Eric Andersen3f980402001-04-04 17:31:15 +0000242
Denis Vlasenkob1759462008-06-20 20:20:54 +0000243
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000244/* vi.c expects chars to be unsigned. */
245/* busybox build system provides that, but it's better */
246/* to audit and fix the source */
Eric Andersen3f980402001-04-04 17:31:15 +0000247
Denis Vlasenkob1759462008-06-20 20:20:54 +0000248struct globals {
249 /* many references - keep near the top of globals */
250 char *text, *end; // pointers to the user data in memory
251 char *dot; // where all the action takes place
252 int text_size; // size of the allocated buffer
253
254 /* the rest */
255 smallint vi_setops;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000256#define VI_AUTOINDENT 1
257#define VI_SHOWMATCH 2
258#define VI_IGNORECASE 4
259#define VI_ERR_METHOD 8
260#define autoindent (vi_setops & VI_AUTOINDENT)
261#define showmatch (vi_setops & VI_SHOWMATCH )
262#define ignorecase (vi_setops & VI_IGNORECASE)
263/* indicate error with beep or flash */
264#define err_method (vi_setops & VI_ERR_METHOD)
265
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000266#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000267 smallint readonly_mode;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000268#define SET_READONLY_FILE(flags) ((flags) |= 0x01)
269#define SET_READONLY_MODE(flags) ((flags) |= 0x02)
270#define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000271#else
Denis Vlasenkob1759462008-06-20 20:20:54 +0000272#define SET_READONLY_FILE(flags) ((void)0)
273#define SET_READONLY_MODE(flags) ((void)0)
274#define UNSET_READONLY_FILE(flags) ((void)0)
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000275#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000276
Denis Vlasenkob1759462008-06-20 20:20:54 +0000277 smallint editing; // >0 while we are editing a file
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000278 // [code audit says "can be 0, 1 or 2 only"]
Denis Vlasenkob1759462008-06-20 20:20:54 +0000279 smallint cmd_mode; // 0=command 1=insert 2=replace
280 int file_modified; // buffer contents changed (counter, not flag!)
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000281 int last_file_modified; // = -1;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000282 int save_argc; // how many file names on cmd line
283 int cmdcnt; // repetition count
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200284 int rows, columns; // the terminal screen is this size
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700285#if ENABLE_FEATURE_VI_ASK_TERMINAL
286 int get_rowcol_error;
287#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000288 int crow, ccol; // cursor is on Crow x Ccol
289 int offset; // chars scrolled off the screen to the left
290 int have_status_msg; // is default edit status needed?
291 // [don't make smallint!]
292 int last_status_cksum; // hash of current status line
293 char *current_filename;
294 char *screenbegin; // index into text[], of top line on the screen
295 char *screen; // pointer to the virtual screen buffer
296 int screensize; // and its size
297 int tabstop;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000298 int last_forward_char; // last char searched for with 'f' (int because of Unicode)
Denis Vlasenkob1759462008-06-20 20:20:54 +0000299 char erase_char; // the users erase character
300 char last_input_char; // last char read from user
Denis Vlasenkob1759462008-06-20 20:20:54 +0000301
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000302#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkob1759462008-06-20 20:20:54 +0000303 smallint adding2q; // are we currently adding user input to q
304 int lmc_len; // length of last_modifying_cmd
305 char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000306#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000307#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkob1759462008-06-20 20:20:54 +0000308 int my_pid;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000309#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000310#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkob1759462008-06-20 20:20:54 +0000311 char *last_search_pattern; // last pattern from a '/' or '?' search
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000312#endif
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000313
Denis Vlasenkob1759462008-06-20 20:20:54 +0000314 /* former statics */
315#if ENABLE_FEATURE_VI_YANKMARK
316 char *edit_file__cur_line;
317#endif
318 int refresh__old_offset;
319 int format_edit_status__tot;
Eric Andersen3f980402001-04-04 17:31:15 +0000320
Denis Vlasenkob1759462008-06-20 20:20:54 +0000321 /* a few references only */
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000322#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000323 int YDreg, Ureg; // default delete register and orig line for "U"
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000324 char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000325 char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
326 char *context_start, *context_end;
327#endif
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000328#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkob1759462008-06-20 20:20:54 +0000329 sigjmp_buf restart; // catch_sig()
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000330#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000331 struct termios term_orig, term_vi; // remember what the cooked mode was
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000332#if ENABLE_FEATURE_VI_COLON
333 char *initial_cmds[3]; // currently 2 entries, NULL terminated
334#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000335 // Should be just enough to hold a key sequence,
Denis Vlasenko25497c12008-10-14 10:25:05 +0000336 // but CRASHME mode uses it as generated command buffer too
337#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko1dfeeeb2008-10-18 19:04:37 +0000338 char readbuffer[128];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000339#else
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000340 char readbuffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000341#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000342#define STATUS_BUFFER_LEN 200
343 char status_buffer[STATUS_BUFFER_LEN]; // messages to the user
344#if ENABLE_FEATURE_VI_DOT_CMD
345 char last_modifying_cmd[MAX_INPUT_LEN]; // last modifying cmd for "."
346#endif
347 char get_input_line__buf[MAX_INPUT_LEN]; /* former static */
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000348
349 char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000350};
351#define G (*ptr_to_globals)
352#define text (G.text )
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000353#define text_size (G.text_size )
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000354#define end (G.end )
355#define dot (G.dot )
356#define reg (G.reg )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000357
358#define vi_setops (G.vi_setops )
359#define editing (G.editing )
360#define cmd_mode (G.cmd_mode )
361#define file_modified (G.file_modified )
362#define last_file_modified (G.last_file_modified )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000363#define save_argc (G.save_argc )
364#define cmdcnt (G.cmdcnt )
365#define rows (G.rows )
366#define columns (G.columns )
367#define crow (G.crow )
368#define ccol (G.ccol )
369#define offset (G.offset )
370#define status_buffer (G.status_buffer )
371#define have_status_msg (G.have_status_msg )
372#define last_status_cksum (G.last_status_cksum )
373#define current_filename (G.current_filename )
374#define screen (G.screen )
375#define screensize (G.screensize )
376#define screenbegin (G.screenbegin )
377#define tabstop (G.tabstop )
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000378#define last_forward_char (G.last_forward_char )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000379#define erase_char (G.erase_char )
380#define last_input_char (G.last_input_char )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000381#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000382#define readonly_mode (G.readonly_mode )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000383#else
384#define readonly_mode 0
385#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000386#define adding2q (G.adding2q )
387#define lmc_len (G.lmc_len )
388#define ioq (G.ioq )
389#define ioq_start (G.ioq_start )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000390#define my_pid (G.my_pid )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000391#define last_search_pattern (G.last_search_pattern)
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000392
Denis Vlasenkob1759462008-06-20 20:20:54 +0000393#define edit_file__cur_line (G.edit_file__cur_line)
394#define refresh__old_offset (G.refresh__old_offset)
395#define format_edit_status__tot (G.format_edit_status__tot)
396
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000397#define YDreg (G.YDreg )
398#define Ureg (G.Ureg )
399#define mark (G.mark )
400#define context_start (G.context_start )
401#define context_end (G.context_end )
402#define restart (G.restart )
403#define term_orig (G.term_orig )
404#define term_vi (G.term_vi )
405#define initial_cmds (G.initial_cmds )
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000406#define readbuffer (G.readbuffer )
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000407#define scr_out_buf (G.scr_out_buf )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000408#define last_modifying_cmd (G.last_modifying_cmd )
409#define get_input_line__buf (G.get_input_line__buf)
410
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000411#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000412 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkob1759462008-06-20 20:20:54 +0000413 last_file_modified = -1; \
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000414 /* "" but has space for 2 chars: */ \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000415 IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000416} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000417
Denis Vlasenkob1759462008-06-20 20:20:54 +0000418
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000419static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000420static void edit_file(char *); // edit one file
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000421static void do_cmd(int); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000422static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000423static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
424static char *begin_line(char *); // return pointer to cur line B-o-l
425static char *end_line(char *); // return pointer to cur line E-o-l
426static char *prev_line(char *); // return pointer to prev line B-o-l
427static char *next_line(char *); // return pointer to next line B-o-l
428static char *end_screen(void); // get pointer to last char on screen
429static int count_lines(char *, char *); // count line from start to stop
430static char *find_line(int); // find begining of line #li
431static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000432static void dot_left(void); // move dot left- dont leave line
433static void dot_right(void); // move dot right- dont leave line
434static void dot_begin(void); // move dot to B-o-l
435static void dot_end(void); // move dot to E-o-l
436static void dot_next(void); // move dot to next line B-o-l
437static void dot_prev(void); // move dot to prev line B-o-l
438static void dot_scroll(int, int); // move the screen up or down
439static void dot_skip_over_ws(void); // move dot pat WS
440static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000441static char *bound_dot(char *); // make sure text[0] <= P < "end"
442static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000443static char *char_insert(char *, char); // insert the char c at 'p'
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000444// might reallocate text[]! use p += stupid_insert(p, ...),
445// and be careful to not use pointers into potentially freed text[]!
446static uintptr_t stupid_insert(char *, char); // stupidly insert the char c at 'p'
Paul Foxc51fc7b2008-03-06 01:34:23 +0000447static int find_range(char **, char **, char); // return pointers for an object
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000448static int st_test(char *, int, int, char *); // helper for skip_thing()
449static char *skip_thing(char *, int, int, int); // skip some object
450static char *find_pair(char *, char); // find matching pair () [] {}
451static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000452// might reallocate text[]! use p += text_hole_make(p, ...),
453// and be careful to not use pointers into potentially freed text[]!
454static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000455static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000456static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000457static void rawmode(void); // set "raw" mode on tty
458static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000459// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
460static int mysleep(int);
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000461static int readit(void); // read (maybe cursor) key from stdin
462static int get_one_char(void); // read 1 char from stdin
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000463static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000464#if !ENABLE_FEATURE_VI_READONLY
465#define file_insert(fn, p, update_ro_status) file_insert(fn, p)
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000466#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000467// file_insert might reallocate text[]!
468static int file_insert(const char *, char *, int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000469static int file_write(char *, char *, char *);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +0200470static void place_cursor(int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000471static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000472static void clear_to_eol(void);
473static void clear_to_eos(void);
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000474static void go_bottom_and_clear_to_eol(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000475static void standout_start(void); // send "start reverse video" sequence
476static void standout_end(void); // send "end reverse video" sequence
477static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000478static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000479static void status_line(const char *, ...); // print to status buf
480static void status_line_bold(const char *, ...);
maxwen27116ba2015-08-14 21:41:28 +0200481static void status_line_bold_errno(const char *fn);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000482static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000483static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000484static void redraw(int); // force a full screen refresh
Denis Vlasenko68404f12008-03-17 09:00:54 +0000485static char* format_line(char* /*, int*/);
Eric Andersen3f980402001-04-04 17:31:15 +0000486static void refresh(int); // update the terminal from screen[]
487
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000488static void Indicate_Error(void); // use flash or beep to indicate error
489#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000490static void Hit_Return(void);
491
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000492#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000493static char *char_search(char *, const char *, int, int); // search for pattern starting at p
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000494#endif
495#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000496static char *get_one_address(char *, int *); // get colon addr, if present
497static char *get_address(char *, int *, int *); // get two colon addrs, if present
498static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000499#endif
500#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000501static void winch_sig(int); // catch window size changes
502static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000503static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000504#endif
505#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000506static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000507static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000508#else
509#define end_cmd_q() ((void)0)
510#endif
511#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000512static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000513#endif
514#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000515// might reallocate text[]! use p += string_insert(p, ...),
516// and be careful to not use pointers into potentially freed text[]!
517static uintptr_t string_insert(char *, const char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000518#endif
519#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000520static char *text_yank(char *, char *, int); // save copy of "p" into a register
521static char what_reg(void); // what is letter of current YDreg
522static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000523#endif
524#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000525static void crash_dummy();
526static void crash_test();
527static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000528#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000529
530
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000531static void write1(const char *out)
532{
533 fputs(out, stdout);
534}
535
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000536int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000537int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000538{
Eric Andersend402edf2001-04-04 19:29:48 +0000539 int c;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000540
541 INIT_G();
Eric Andersen3f980402001-04-04 17:31:15 +0000542
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000543#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000544 my_pid = getpid();
545#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000546#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000547 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000548#endif
Denis Vlasenko2414a962007-07-18 22:03:40 +0000549#ifdef NO_SUCH_APPLET_YET
550 /* If we aren't "vi", we are "view" */
551 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000552 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000553 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000554#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000555
Denys Vlasenko5f556fc2012-06-11 01:53:33 +0200556 // autoindent is not default in vim 7.3
557 vi_setops = /*VI_AUTOINDENT |*/ VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenkof9234132007-03-21 00:03:42 +0000558 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000559 // 2- process EXINIT variable from environment
560 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000561#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000562 {
563 char *p = getenv("EXINIT");
564 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000565 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000566 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000567#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000568 while ((c = getopt(argc, argv, "hCRH" IF_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000569 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000570#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000571 case 'C':
572 crashme = 1;
573 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000574#endif
575#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000576 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000577 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000578 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000579#endif
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000580#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000581 case 'c': // cmd line vi command
582 if (*optarg)
Denys Vlasenko5f556fc2012-06-11 01:53:33 +0200583 initial_cmds[initial_cmds[0] != NULL] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000584 break;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000585#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000586 case 'H':
Eric Andersen3f980402001-04-04 17:31:15 +0000587 show_help();
Paul Fox35e9c5d2008-03-06 16:26:12 +0000588 /* fall through */
Paul Fox35e9c5d2008-03-06 16:26:12 +0000589 default:
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000590 bb_show_usage();
Eric Andersendd8500b2001-07-02 18:06:14 +0000591 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000592 }
593 }
594
595 // The argv array can be used by the ":next" and ":rewind" commands
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200596 argv += optind;
597 argc -= optind;
Eric Andersen3f980402001-04-04 17:31:15 +0000598
599 //----- This is the main file handling loop --------------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +0200600 save_argc = argc;
601 optind = 0;
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100602 // "Save cursor, use alternate screen buffer, clear screen"
603 write1("\033[?1049h");
Denys Vlasenko04cecd52010-04-16 22:13:55 -0700604 while (1) {
605 edit_file(argv[optind]); /* param might be NULL */
606 if (++optind >= argc)
607 break;
Eric Andersen3f980402001-04-04 17:31:15 +0000608 }
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100609 // "Use normal screen buffer, restore cursor"
610 write1("\033[?1049l");
Eric Andersen3f980402001-04-04 17:31:15 +0000611 //-----------------------------------------------------------
612
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000613 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000614}
615
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000616/* read text from file or create an empty buf */
617/* will also update current_filename */
618static int init_text_buffer(char *fn)
619{
620 int rc;
621 int size = file_size(fn); // file size. -1 means does not exist.
622
623 /* allocate/reallocate text buffer */
624 free(text);
Denis Vlasenkob1759462008-06-20 20:20:54 +0000625 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000626 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000627
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000628 if (fn != current_filename) {
629 free(current_filename);
630 current_filename = xstrdup(fn);
631 }
632 if (size < 0) {
633 // file dont exist. Start empty buf with dummy line
634 char_insert(text, '\n');
635 rc = 0;
636 } else {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000637 rc = file_insert(fn, text, 1);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000638 }
639 file_modified = 0;
640 last_file_modified = -1;
641#if ENABLE_FEATURE_VI_YANKMARK
642 /* init the marks. */
643 memset(mark, 0, sizeof(mark));
644#endif
645 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000646}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000647
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700648#if ENABLE_FEATURE_VI_WIN_RESIZE
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200649static int query_screen_dimensions(void)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700650{
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200651 unsigned c, r;
652 int err = get_terminal_width_height(STDIN_FILENO, &c, &r);
653 columns = c; rows = r;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700654 if (rows > MAX_SCR_ROWS)
655 rows = MAX_SCR_ROWS;
656 if (columns > MAX_SCR_COLS)
657 columns = MAX_SCR_COLS;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200658 return err;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700659}
660#else
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200661# define query_screen_dimensions() (0)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700662#endif
663
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000664static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000665{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000666#if ENABLE_FEATURE_VI_YANKMARK
667#define cur_line edit_file__cur_line
668#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000669 int c;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000670#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000671 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000672#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000673
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000674 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000675 rawmode();
676 rows = 24;
677 columns = 80;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200678 IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions();
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700679#if ENABLE_FEATURE_VI_ASK_TERMINAL
680 if (G.get_rowcol_error /* TODO? && no input on stdin */) {
681 uint64_t k;
682 write1("\033[999;999H" "\033[6n");
683 fflush_all();
684 k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100);
685 if ((int32_t)k == KEYCODE_CURSOR_POS) {
686 uint32_t rc = (k >> 32);
687 columns = (rc & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200688 if (columns > MAX_SCR_COLS)
689 columns = MAX_SCR_COLS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700690 rows = ((rc >> 16) & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200691 if (rows > MAX_SCR_ROWS)
692 rows = MAX_SCR_ROWS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700693 }
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700694 }
695#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000696 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000697 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000698
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000699#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000700 YDreg = 26; // default Yank/Delete reg
701 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000702 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000703#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000704
Eric Andersen3f980402001-04-04 17:31:15 +0000705 last_forward_char = last_input_char = '\0';
706 crow = 0;
707 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000708
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000709#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700710 signal(SIGINT, catch_sig);
Eric Andersen3f980402001-04-04 17:31:15 +0000711 signal(SIGWINCH, winch_sig);
712 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000713 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000714 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000715 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000716 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000717#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000718
Eric Andersen3f980402001-04-04 17:31:15 +0000719 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
720 cmdcnt = 0;
721 tabstop = 8;
722 offset = 0; // no horizontal offset
723 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000724#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000725 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000726 ioq = ioq_start = NULL;
727 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000728 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000729#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000730
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000731#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000732 {
733 char *p, *q;
734 int n = 0;
735
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700736 while ((p = initial_cmds[n]) != NULL) {
Denis Vlasenkof9234132007-03-21 00:03:42 +0000737 do {
738 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000739 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000740 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000741 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000742 *p++ = '\0';
743 if (*q)
744 colon(q);
745 } while (p);
746 free(initial_cmds[n]);
747 initial_cmds[n] = NULL;
748 n++;
749 }
750 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000751#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000752 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000753 //------This is the main Vi cmd handling loop -----------------------
754 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000755#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000756 if (crashme > 0) {
757 if ((end - text) > 1) {
758 crash_dummy(); // generate a random command
759 } else {
760 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000761 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
762 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000763 refresh(FALSE);
764 }
765 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000766#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000767 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000768#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000769 // save a copy of the current line- for the 'U" command
770 if (begin_line(dot) != cur_line) {
771 cur_line = begin_line(dot);
772 text_yank(begin_line(dot), end_line(dot), Ureg);
773 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000774#endif
775#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000776 // These are commands that change text[].
777 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000778 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000779 && cmd_mode == 0 // command mode
780 && c > '\0' // exclude NUL and non-ASCII chars
781 && c < 0x7f // (Unicode and such)
782 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000783 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000784 start_new_cmd_q(c);
785 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000786#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000787 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000788
Eric Andersen3f980402001-04-04 17:31:15 +0000789 // poll to see if there is input already waiting. if we are
790 // not able to display output fast enough to keep up, skip
791 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200792 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000793 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000794 refresh(FALSE);
795 show_status_line();
796 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000797#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000798 if (crashme > 0)
799 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000800#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000801 }
802 //-------------------------------------------------------------------
803
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000804 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000805 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000806#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000807}
808
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000809//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000810#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000811static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000812{
813 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000814 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000815 IF_FEATURE_VI_YANKMARK(char c;)
816 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000817
818 *addr = -1; // assume no addr
819 if (*p == '.') { // the current line
820 p++;
821 q = begin_line(dot);
822 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000823 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000824#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000825 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000826 p++;
827 c = tolower(*p);
828 p++;
829 if (c >= 'a' && c <= 'z') {
830 // we have a mark
831 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000832 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000833 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000834 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000835 }
836 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000837 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000838#endif
839#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000840 else if (*p == '/') { // a search pattern
841 q = strchrnul(++p, '/');
842 pat = xstrndup(p, q - p); // save copy of pattern
843 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000844 if (*p == '/')
845 p++;
846 q = char_search(dot, pat, FORWARD, FULL);
847 if (q != NULL) {
848 *addr = count_lines(text, q);
849 }
850 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000851 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000852#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000853 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000854 p++;
855 q = begin_line(end - 1);
856 *addr = count_lines(text, q);
857 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000858 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000859 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000860 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200861 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000862 *addr = -1;
863 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000864 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000865}
866
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000867static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000868{
869 //----- get the address' i.e., 1,3 'a,'b -----
870 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000871 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000872 p++; // skip over leading spaces
873 if (*p == '%') { // alias for 1,$
874 p++;
875 *b = 1;
876 *e = count_lines(text, end-1);
877 goto ga0;
878 }
879 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000880 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000881 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000882 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000883 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000884 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000885 p++;
886 // get SECOND addr, if present
887 p = get_one_address(p, e);
888 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000889 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000890 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000891 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000892 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000893}
894
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000895#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000896static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000897 const char *short_opname, int opt)
898{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000899 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000900 int l = strlen(opname) - 1; /* opname have + ' ' */
901
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200902 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000903 if (strncasecmp(a, opname, l) == 0
904 || strncasecmp(a, short_opname, 2) == 0
905 ) {
906 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000907 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000908 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000909 vi_setops |= opt;
910 }
911}
912#endif
913
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000914// buf must be no longer than MAX_INPUT_LEN!
915static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000916{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000917 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000918 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000919 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000920 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000921
922 // :3154 // if (-e line 3154) goto it else stay put
923 // :4,33w! foo // write a portion of buffer to file "foo"
924 // :w // write all of buffer to current file
925 // :q // quit
926 // :q! // quit- dont care about modified file
927 // :'a,'z!sort -u // filter block through sort
928 // :'f // goto mark "f"
929 // :'fl // list literal the mark "f" line
930 // :.r bar // read file "bar" into buffer before dot
931 // :/123/,/abc/d // delete lines from "123" line to "abc" line
932 // :/xyz/ // goto the "xyz" line
933 // :s/find/replace/ // substitute pattern "find" with "replace"
934 // :!<cmd> // run <cmd> then return
935 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000936
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000937 if (!buf[0])
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200938 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000939 if (*buf == ':')
940 buf++; // move past the ':'
941
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000942 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000943 b = e = -1;
944 q = text; // assume 1,$ for the range
945 r = end - 1;
946 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000947 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000948
949 // look for optional address(es) :. :1 :1,9 :'q,'a :%
950 buf = get_address(buf, &b, &e);
951
952 // remember orig command line
953 orig_buf = buf;
954
955 // get the COMMAND into cmd[]
956 buf1 = cmd;
957 while (*buf != '\0') {
958 if (isspace(*buf))
959 break;
960 *buf1++ = *buf++;
961 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000962 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000963 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000964 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000965 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000966 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000967 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000968 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000969 if (buf1) {
970 useforce = TRUE;
971 *buf1 = '\0'; // get rid of !
972 }
973 if (b >= 0) {
974 // if there is only one addr, then the addr
975 // is the line number of the single line the
976 // user wants. So, reset the end
977 // pointer to point at end of the "b" line
978 q = find_line(b); // what line is #b
979 r = end_line(q);
980 li = 1;
981 }
982 if (e >= 0) {
983 // we were given two addrs. change the
984 // end pointer to the addr given by user.
985 r = find_line(e); // what line is #e
986 r = end_line(r);
987 li = e - b + 1;
988 }
989 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000990 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000991 if (i == 0) { // :123CR goto line #123
992 if (b >= 0) {
993 dot = find_line(b); // what line is #b
994 dot_skip_over_ws();
995 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000996 }
997#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200998 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000999 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001000 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001001 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001002 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001003 retcode = system(orig_buf + 1); // run the cmd
1004 if (retcode)
1005 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001006 rawmode();
1007 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +00001008 }
1009#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001010 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001011 if (b < 0) { // no addr given- use defaults
1012 b = e = count_lines(text, dot);
1013 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001014 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001015 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001016 if (b < 0) { // no addr given- use defaults
1017 q = begin_line(dot); // assume .,. for the range
1018 r = end_line(dot);
1019 }
1020 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
1021 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001022 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001023 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001024 if (file_modified && !useforce) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001025 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001026 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001027 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001028 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001029 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001030 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001031 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001032 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001033 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001034 } else {
1035 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001036 status_line_bold("No current filename");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001037 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001038 }
1039
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001040 if (init_text_buffer(fn) < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001041 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001042
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001043#if ENABLE_FEATURE_VI_YANKMARK
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001044 if (Ureg >= 0 && Ureg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001045 free(reg[Ureg]); // free orig line reg- for 'U'
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001046 reg[Ureg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001047 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001048 if (YDreg >= 0 && YDreg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001049 free(reg[YDreg]); // free default yank/delete register
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001050 reg[YDreg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001051 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001052#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001053 // how many lines in text[]?
1054 li = count_lines(text, end - 1);
maxwen27116ba2015-08-14 21:41:28 +02001055 status_line("'%s'%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001056 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001057 " %dL, %dC", current_filename,
1058 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001059 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001060 ((readonly_mode) ? " [Readonly]" : ""),
1061 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001062 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001063 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001064 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01001065 status_line_bold("No address allowed on this command");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001066 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001067 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001068 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001069 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001070 free(current_filename);
1071 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001072 } else {
1073 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +00001074 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001075 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001076 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001077 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001078 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001079 cookmode();
1080 show_help();
1081 rawmode();
1082 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001083 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001084 if (b < 0) { // no addr given- use defaults
1085 q = begin_line(dot); // assume .,. for the range
1086 r = end_line(dot);
1087 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001088 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001089 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001090 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001091 int c_is_no_print;
1092
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001093 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +00001094 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001095 if (c_is_no_print) {
1096 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001097 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +00001098 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001099 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001100 write1("$\r");
1101 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001102 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001103 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001104 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001105 else
1106 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001107 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001108 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001109 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001110 standout_end();
1111 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001112 Hit_Return();
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001113 } else if (strncmp(cmd, "quit", i) == 0 // quit
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001114 || strncmp(cmd, "next", i) == 0 // edit next file
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001115 || strncmp(cmd, "prev", i) == 0 // edit previous file
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001116 ) {
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001117 int n;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001118 if (useforce) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001119 if (*cmd == 'q') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001120 // force end of argv list
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001121 optind = save_argc;
1122 }
1123 editing = 0;
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001124 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001125 }
1126 // don't exit if the file been modified
1127 if (file_modified) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001128 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001129 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001130 }
1131 // are there other file to edit
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001132 n = save_argc - optind - 1;
1133 if (*cmd == 'q' && n > 0) {
1134 status_line_bold("%d more file(s) to edit", n);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001135 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001136 }
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001137 if (*cmd == 'n' && n <= 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001138 status_line_bold("No more files to edit");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001139 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001140 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001141 if (*cmd == 'p') {
1142 // are there previous files to edit
1143 if (optind < 1) {
1144 status_line_bold("No previous files to edit");
1145 goto ret;
1146 }
1147 optind -= 2;
1148 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001149 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001150 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001151 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001152 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001153 status_line_bold("No filename given");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001154 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001155 }
1156 if (b < 0) { // no addr given- use defaults
1157 q = begin_line(dot); // assume "dot"
1158 }
1159 // read after current line- unless user said ":0r foo"
1160 if (b != 0)
1161 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001162 { // dance around potentially-reallocated text[]
1163 uintptr_t ofs = q - text;
1164 ch = file_insert(fn, q, 0);
1165 q = text + ofs;
1166 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001167 if (ch < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001168 goto ret; // nothing was inserted
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001169 // how many lines in text[]?
1170 li = count_lines(q, q + ch - 1);
maxwen27116ba2015-08-14 21:41:28 +02001171 status_line("'%s'"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001172 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001173 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001174 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001175 li, ch);
1176 if (ch > 0) {
1177 // if the insert is before "dot" then we need to update
1178 if (q <= dot)
1179 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001180 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001181 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001182 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001183 if (file_modified && !useforce) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001184 status_line_bold("No write since last change (:%s! overrides)", cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001185 } else {
1186 // reset the filenames to edit
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001187 optind = -1; /* start from 0th file */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001188 editing = 0;
1189 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001190#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001191 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001192#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001193 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001194#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001195 i = 0; // offset into args
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02001196 // only blank is regarded as args delimiter. What about tab '\t'?
Denis Vlasenkof9234132007-03-21 00:03:42 +00001197 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001198 // print out values of all options
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001199#if ENABLE_FEATURE_VI_SETOPTS
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001200 status_line_bold(
1201 "%sautoindent "
1202 "%sflash "
1203 "%signorecase "
1204 "%sshowmatch "
1205 "tabstop=%u",
1206 autoindent ? "" : "no",
1207 err_method ? "" : "no",
1208 ignorecase ? "" : "no",
1209 showmatch ? "" : "no",
1210 tabstop
1211 );
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001212#endif
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001213 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001214 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001215#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001216 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001217 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001218 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001219 i = 2; // ":set noautoindent"
1220 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001221 setops(argp, "flash " , i, "fl", VI_ERR_METHOD);
Denis Vlasenkof9234132007-03-21 00:03:42 +00001222 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001223 setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH );
1224 if (strncmp(argp + i, "tabstop=", 8) == 0) {
1225 int t = 0;
1226 sscanf(argp + i+8, "%u", &t);
1227 if (t > 0 && t <= MAX_TABSTOP)
1228 tabstop = t;
Denis Vlasenkof9234132007-03-21 00:03:42 +00001229 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001230 argp = skip_non_whitespace(argp);
1231 argp = skip_whitespace(argp);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001232 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001233#endif /* FEATURE_VI_SETOPTS */
1234#endif /* FEATURE_VI_SET */
1235#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001236 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001237 char *F, *R, *flags;
1238 size_t len_F, len_R;
1239 int gflag; // global replace flag
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001240
1241 // F points to the "find" pattern
1242 // R points to the "replace" pattern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001243 // replace the cmd line delimiters "/" with NULs
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001244 c = orig_buf[1]; // what is the delimiter
1245 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001246 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001247 if (!R)
1248 goto colon_s_fail;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001249 len_F = R - F;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001250 *R++ = '\0'; // terminate "find"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001251 flags = strchr(R, c);
1252 if (!flags)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001253 goto colon_s_fail;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001254 len_R = flags - R;
1255 *flags++ = '\0'; // terminate "replace"
1256 gflag = *flags;
1257
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001258 q = begin_line(q);
1259 if (b < 0) { // maybe :s/foo/bar/
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001260 q = begin_line(dot); // start with cur line
1261 b = count_lines(text, q); // cur line number
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001262 }
1263 if (e < 0)
1264 e = b; // maybe :.s/foo/bar/
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001265
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001266 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001267 char *ls = q; // orig line start
1268 char *found;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001269 vc4:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001270 found = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
1271 if (found) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001272 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001273 // we found the "find" pattern - delete it
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001274 text_hole_delete(found, found + len_F - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001275 // inset the "replace" patern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001276 bias = string_insert(found, R); // insert the string
1277 found += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001278 ls += bias;
1279 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001280 // check for "global" :s/foo/bar/g
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001281 if (gflag == 'g') {
1282 if ((found + len_R) < end_line(ls)) {
1283 q = found + len_R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001284 goto vc4; // don't let q move past cur line
1285 }
1286 }
1287 }
1288 q = next_line(ls);
1289 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001290#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001291 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001292 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001293 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1294 || strncmp(cmd, "wq", i) == 0
1295 || strncmp(cmd, "wn", i) == 0
1296 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001297 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001298 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001299 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001300 fn = args;
1301 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001302#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001303 if (readonly_mode && !useforce) {
maxwen27116ba2015-08-14 21:41:28 +02001304 status_line_bold("'%s' is read only", fn);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001305 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001306 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001307#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001308 // how many lines in text[]?
1309 li = count_lines(q, r);
1310 ch = r - q + 1;
1311 // see if file exists- if not, its just a new file request
1312 if (useforce) {
1313 // if "fn" is not write-able, chmod u+w
1314 // sprintf(syscmd, "chmod u+w %s", fn);
1315 // system(syscmd);
1316 forced = TRUE;
1317 }
1318 l = file_write(fn, q, r);
1319 if (useforce && forced) {
1320 // chmod u-w
1321 // sprintf(syscmd, "chmod u-w %s", fn);
1322 // system(syscmd);
1323 forced = FALSE;
1324 }
Paul Fox61e45db2005-10-09 14:43:22 +00001325 if (l < 0) {
1326 if (l == -1)
maxwen27116ba2015-08-14 21:41:28 +02001327 status_line_bold_errno(fn);
Paul Fox61e45db2005-10-09 14:43:22 +00001328 } else {
maxwen27116ba2015-08-14 21:41:28 +02001329 status_line("'%s' %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001330 if (q == text && r == end - 1 && l == ch) {
1331 file_modified = 0;
1332 last_file_modified = -1;
1333 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001334 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1335 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1336 )
1337 && l == ch
1338 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001339 editing = 0;
1340 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001341 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001342#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001343 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001344 if (b < 0) { // no addr given- use defaults
1345 q = begin_line(dot); // assume .,. for the range
1346 r = end_line(dot);
1347 }
1348 text_yank(q, r, YDreg);
1349 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001350 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001351 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001352#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001353 } else {
1354 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001355 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001356 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001357 ret:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001358 dot = bound_dot(dot); // make sure "dot" is valid
1359 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001360#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001361 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001362 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001363#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001364}
Paul Fox61e45db2005-10-09 14:43:22 +00001365
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001366#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001367
1368static void Hit_Return(void)
1369{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001370 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001371
Denis Vlasenkof882f082007-12-23 02:36:51 +00001372 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001373 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001374 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001375 while ((c = get_one_char()) != '\n' && c != '\r')
1376 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001377 redraw(TRUE); // force redraw all
1378}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001379
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001380static int next_tabstop(int col)
1381{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001382 return col + ((tabstop - 1) - (col % tabstop));
1383}
1384
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001385//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001386static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001387{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001388 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001389 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001390 int cnt, ro, co;
1391
1392 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001393
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001394 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001395 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001396 // how many lines do we have to move
1397 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001398 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001399 screenbegin = beg_cur;
1400 if (cnt > (rows - 1) / 2) {
1401 // we moved too many lines. put "dot" in middle of screen
1402 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1403 screenbegin = prev_line(screenbegin);
1404 }
1405 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001406 } else {
1407 char *end_scr; // begin and end of screen
1408 end_scr = end_screen(); // last char of screen
1409 if (beg_cur > end_scr) {
1410 // "d" is after bottom line on screen
1411 // how many lines do we have to move
1412 cnt = count_lines(end_scr, beg_cur);
1413 if (cnt > (rows - 1) / 2)
1414 goto sc1; // too many lines
1415 for (ro = 0; ro < cnt - 1; ro++) {
1416 // move screen begin the same amount
1417 screenbegin = next_line(screenbegin);
1418 // now, move the end of screen
1419 end_scr = next_line(end_scr);
1420 end_scr = end_line(end_scr);
1421 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001422 }
1423 }
1424 // "d" is on screen- find out which row
1425 tp = screenbegin;
1426 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1427 if (tp == beg_cur)
1428 break;
1429 tp = next_line(tp);
1430 }
1431
1432 // find out what col "d" is on
1433 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001434 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001435 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001436 break;
1437 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001438 // handle tabs like real vi
1439 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001440 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001441 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001442 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001443 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1444 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001445 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001446 co++;
1447 tp++;
1448 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001449
1450 // "co" is the column where "dot" is.
1451 // The screen has "columns" columns.
1452 // The currently displayed columns are 0+offset -- columns+ofset
1453 // |-------------------------------------------------------------|
1454 // ^ ^ ^
1455 // offset | |------- columns ----------------|
1456 //
1457 // If "co" is already in this range then we do not have to adjust offset
1458 // but, we do have to subtract the "offset" bias from "co".
1459 // If "co" is outside this range then we have to change "offset".
1460 // If the first char of a line is a tab the cursor will try to stay
1461 // in column 7, but we have to set offset to 0.
1462
1463 if (co < 0 + offset) {
1464 offset = co;
1465 }
1466 if (co >= columns + offset) {
1467 offset = co - columns + 1;
1468 }
1469 // if the first char of the line is a tab, and "dot" is sitting on it
1470 // force offset to 0.
1471 if (d == beg_cur && *d == '\t') {
1472 offset = 0;
1473 }
1474 co -= offset;
1475
1476 *row = ro;
1477 *col = co;
1478}
1479
1480//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001481static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001482{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001483 if (p > text) {
1484 p = memrchr(text, '\n', p - text);
1485 if (!p)
1486 return text;
1487 return p + 1;
1488 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001489 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001490}
1491
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001492static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001493{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001494 if (p < end - 1) {
1495 p = memchr(p, '\n', end - p - 1);
1496 if (!p)
1497 return end - 1;
1498 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001499 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001500}
1501
Denis Vlasenkof882f082007-12-23 02:36:51 +00001502static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001503{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001504 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001505 // Try to stay off of the Newline
1506 if (*p == '\n' && (p - begin_line(p)) > 0)
1507 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001508 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001509}
1510
Denis Vlasenkof882f082007-12-23 02:36:51 +00001511static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001512{
1513 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001514 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001515 p--; // step to prev line
1516 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001517 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001518}
1519
Denis Vlasenkof882f082007-12-23 02:36:51 +00001520static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001521{
1522 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001523 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001524 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001525 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001526}
1527
1528//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001529static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001530{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001531 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001532 int cnt;
1533
1534 // find new bottom line
1535 q = screenbegin;
1536 for (cnt = 0; cnt < rows - 2; cnt++)
1537 q = next_line(q);
1538 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001539 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001540}
1541
Denis Vlasenkof882f082007-12-23 02:36:51 +00001542// count line from start to stop
1543static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001544{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001545 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001546 int cnt;
1547
Denis Vlasenkof882f082007-12-23 02:36:51 +00001548 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001549 q = start;
1550 start = stop;
1551 stop = q;
1552 }
1553 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001554 stop = end_line(stop);
1555 while (start <= stop && start <= end - 1) {
1556 start = end_line(start);
1557 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001558 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001559 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001560 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001561 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001562}
1563
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001564static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001565{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001566 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001567
1568 for (q = text; li > 1; li--) {
1569 q = next_line(q);
1570 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001571 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001572}
1573
1574//----- Dot Movement Routines ----------------------------------
1575static void dot_left(void)
1576{
1577 if (dot > text && dot[-1] != '\n')
1578 dot--;
1579}
1580
1581static void dot_right(void)
1582{
1583 if (dot < end - 1 && *dot != '\n')
1584 dot++;
1585}
1586
1587static void dot_begin(void)
1588{
1589 dot = begin_line(dot); // return pointer to first char cur line
1590}
1591
1592static void dot_end(void)
1593{
1594 dot = end_line(dot); // return pointer to last char cur line
1595}
1596
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001597static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001598{
1599 int co;
1600
1601 p = begin_line(p);
1602 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001603 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001604 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001605 break;
1606 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001607 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001608 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001609 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001610 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001611 co++;
1612 p++;
1613 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001614 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001615}
1616
1617static void dot_next(void)
1618{
1619 dot = next_line(dot);
1620}
1621
1622static void dot_prev(void)
1623{
1624 dot = prev_line(dot);
1625}
1626
1627static void dot_scroll(int cnt, int dir)
1628{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001629 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001630
1631 for (; cnt > 0; cnt--) {
1632 if (dir < 0) {
1633 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001634 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001635 screenbegin = prev_line(screenbegin);
1636 } else {
1637 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001638 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001639 screenbegin = next_line(screenbegin);
1640 }
1641 }
1642 // make sure "dot" stays on the screen so we dont scroll off
1643 if (dot < screenbegin)
1644 dot = screenbegin;
1645 q = end_screen(); // find new bottom line
1646 if (dot > q)
1647 dot = begin_line(q); // is dot is below bottom line?
1648 dot_skip_over_ws();
1649}
1650
1651static void dot_skip_over_ws(void)
1652{
1653 // skip WS
1654 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1655 dot++;
1656}
1657
1658static void dot_delete(void) // delete the char at 'dot'
1659{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001660 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001661}
1662
Denis Vlasenkof882f082007-12-23 02:36:51 +00001663static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001664{
1665 if (p >= end && end > text) {
1666 p = end - 1;
1667 indicate_error('1');
1668 }
1669 if (p < text) {
1670 p = text;
1671 indicate_error('2');
1672 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001673 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001674}
1675
1676//----- Helper Utility Routines --------------------------------
1677
1678//----------------------------------------------------------------
1679//----- Char Routines --------------------------------------------
1680/* Chars that are part of a word-
1681 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1682 * Chars that are Not part of a word (stoppers)
1683 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1684 * Chars that are WhiteSpace
1685 * TAB NEWLINE VT FF RETURN SPACE
1686 * DO NOT COUNT NEWLINE AS WHITESPACE
1687 */
1688
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001689static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001690{
1691 int li;
1692
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001693 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001694 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001695 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001696 // initialize the new screen. assume this will be a empty file.
1697 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001698 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001699 for (li = 1; li < ro - 1; li++) {
1700 screen[(li * co) + 0] = '~';
1701 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001702 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001703}
1704
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001705#if ENABLE_FEATURE_VI_SEARCH
Walter Harmsb9ba5802011-06-27 02:59:37 +02001706
1707# if ENABLE_FEATURE_VI_REGEX_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001708
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001709// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001710static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001711{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001712 struct re_pattern_buffer preg;
maxwen27116ba2015-08-14 21:41:28 +02001713 const char *err;
1714 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001715 int i;
Walter Harmsb9ba5802011-06-27 02:59:37 +02001716 int size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001717
1718 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
maxwen27116ba2015-08-14 21:41:28 +02001719 if (ignorecase)
1720 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED | RE_ICASE;
1721
1722 memset(&preg, 0, sizeof(preg));
1723 err = re_compile_pattern(pat, strlen(pat), &preg);
1724 if (err != NULL) {
1725 status_line_bold("bad search pattern '%s': %s", pat, err);
1726 return p;
1727 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001728
1729 // assume a LIMITED forward search
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001730 q = end - 1;
maxwen27116ba2015-08-14 21:41:28 +02001731 if (dir == BACK)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001732 q = text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001733 // RANGE could be negative if we are searching backwards
1734 range = q - p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001735 q = p;
maxwen27116ba2015-08-14 21:41:28 +02001736 size = range;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001737 if (range < 0) {
maxwen27116ba2015-08-14 21:41:28 +02001738 size = -size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001739 q = p - size;
1740 if (q < text)
1741 q = text;
1742 }
1743 // search for the compiled pattern, preg, in p[]
maxwen27116ba2015-08-14 21:41:28 +02001744 // range < 0: search backward
1745 // range > 0: search forward
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001746 // 0 < start < size
maxwen27116ba2015-08-14 21:41:28 +02001747 // re_search() < 0: not found or error
1748 // re_search() >= 0: index of found pattern
1749 // struct pattern char int int int struct reg
1750 // re_search(*pattern_buffer, *string, size, start, range, *regs)
1751 i = re_search(&preg, q, size, /*start:*/ 0, range, /*struct re_registers*:*/ NULL);
1752 regfree(&preg);
1753 if (i < 0)
1754 return NULL;
1755 if (dir == FORWARD)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001756 p = p + i;
maxwen27116ba2015-08-14 21:41:28 +02001757 else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001758 p = p - i;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001759 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001760}
Walter Harmsb9ba5802011-06-27 02:59:37 +02001761
1762# else
1763
1764# if ENABLE_FEATURE_VI_SETOPTS
1765static int mycmp(const char *s1, const char *s2, int len)
1766{
1767 if (ignorecase) {
1768 return strncasecmp(s1, s2, len);
1769 }
1770 return strncmp(s1, s2, len);
1771}
1772# else
1773# define mycmp strncmp
1774# endif
1775
1776static char *char_search(char *p, const char *pat, int dir, int range)
1777{
1778 char *start, *stop;
1779 int len;
1780
1781 len = strlen(pat);
1782 if (dir == FORWARD) {
maxwen27116ba2015-08-14 21:41:28 +02001783 stop = end - 1; // assume range is p..end-1
Walter Harmsb9ba5802011-06-27 02:59:37 +02001784 if (range == LIMITED)
1785 stop = next_line(p); // range is to next line
1786 for (start = p; start < stop; start++) {
1787 if (mycmp(start, pat, len) == 0) {
1788 return start;
1789 }
1790 }
1791 } else if (dir == BACK) {
maxwen27116ba2015-08-14 21:41:28 +02001792 stop = text; // assume range is text..p
Walter Harmsb9ba5802011-06-27 02:59:37 +02001793 if (range == LIMITED)
1794 stop = prev_line(p); // range is to prev line
1795 for (start = p - len; start >= stop; start--) {
1796 if (mycmp(start, pat, len) == 0) {
1797 return start;
1798 }
1799 }
1800 }
1801 // pattern not found
1802 return NULL;
1803}
1804
1805# endif
1806
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001807#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001808
Denis Vlasenko33875382008-06-21 20:31:50 +00001809static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001810{
1811 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001812 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001813 refresh(FALSE); // show the ^
1814 c = get_one_char();
1815 *p = c;
1816 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001817 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001818 } else if (c == 27) { // Is this an ESC?
1819 cmd_mode = 0;
1820 cmdcnt = 0;
1821 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001822 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001823 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001824 p--;
1825 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001826 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001827 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001828 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001829 p--;
1830 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001831 }
1832 } else {
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001833#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001834 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001835 char *sp; // "save p"
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001836#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001837
1838 if (c == 13)
1839 c = '\n'; // translate \r to \n
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001840#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001841 sp = p; // remember addr of insert
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001842#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001843 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001844#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001845 if (showmatch && strchr(")]}", *sp) != NULL) {
1846 showmatching(sp);
1847 }
1848 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001849 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001850 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001851 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001852 len = strspn(q, " \t"); // space or tab
1853 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001854 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001855 bias = text_hole_make(p, len);
1856 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001857 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001858 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001859 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001860 }
1861 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001862#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001863 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001864 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001865}
1866
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001867// might reallocate text[]! use p += stupid_insert(p, ...),
1868// and be careful to not use pointers into potentially freed text[]!
1869static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001870{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001871 uintptr_t bias;
1872 bias = text_hole_make(p, 1);
1873 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001874 *p = c;
1875 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001876 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001877}
1878
Denis Vlasenko33875382008-06-21 20:31:50 +00001879static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001880{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001881 char *save_dot, *p, *q, *t;
1882 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001883
1884 save_dot = dot;
1885 p = q = dot;
1886
1887 if (strchr("cdy><", c)) {
1888 // these cmds operate on whole lines
1889 p = q = begin_line(p);
1890 for (cnt = 1; cnt < cmdcnt; cnt++) {
1891 q = next_line(q);
1892 }
1893 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001894 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001895 // These cmds operate on char positions
1896 do_cmd(c); // execute movement cmd
1897 q = dot;
1898 } else if (strchr("wW", c)) {
1899 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001900 // if we are at the next word's first char
1901 // step back one char
1902 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001903 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001904 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1905 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1906 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001907 if (dot > text && *dot == '\n')
1908 dot--; // stay off NL
1909 q = dot;
1910 } else if (strchr("H-k{", c)) {
1911 // these operate on multi-lines backwards
1912 q = end_line(dot); // find NL
1913 do_cmd(c); // execute movement cmd
1914 dot_begin();
1915 p = dot;
1916 } else if (strchr("L+j}\r\n", c)) {
1917 // these operate on multi-lines forwards
1918 p = begin_line(dot);
1919 do_cmd(c); // execute movement cmd
1920 dot_end(); // find NL
1921 q = dot;
1922 } else {
Tanguy Pruvot823694d2012-11-18 13:20:29 +01001923 // nothing -- this causes any other values of c to
1924 // represent the one-character range under the
1925 // cursor. this is correct for ' ' and 'l', but
1926 // perhaps no others.
1927 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001928 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001929 if (q < p) {
1930 t = q;
1931 q = p;
1932 p = t;
1933 }
1934
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001935 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001936 if (q > p && strchr("^0bBh\b\177", c)) q--;
1937
1938 multiline = 0;
1939 for (t = p; t <= q; t++) {
1940 if (*t == '\n') {
1941 multiline = 1;
1942 break;
1943 }
1944 }
1945
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001946 *start = p;
1947 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001948 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001949 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001950}
1951
Denis Vlasenko33875382008-06-21 20:31:50 +00001952static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001953{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001954 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001955 int test, inc;
1956
1957 inc = dir;
1958 c = c0 = p[0];
1959 ci = p[inc];
1960 test = 0;
1961
1962 if (type == S_BEFORE_WS) {
1963 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001964 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001965 }
1966 if (type == S_TO_WS) {
1967 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001968 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001969 }
1970 if (type == S_OVER_WS) {
1971 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001972 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001973 }
1974 if (type == S_END_PUNCT) {
1975 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001976 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001977 }
1978 if (type == S_END_ALNUM) {
1979 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001980 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001981 }
1982 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001983 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001984}
1985
Denis Vlasenko33875382008-06-21 20:31:50 +00001986static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001987{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001988 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001989
1990 while (st_test(p, type, dir, &c)) {
1991 // make sure we limit search to correct number of lines
1992 if (c == '\n' && --linecnt < 1)
1993 break;
1994 if (dir >= 0 && p >= end - 1)
1995 break;
1996 if (dir < 0 && p <= text)
1997 break;
1998 p += dir; // move to next char
1999 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002000 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002001}
2002
2003// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00002004static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002005{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002006 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002007 int dir, level;
2008
2009 match = ')';
2010 level = 1;
2011 dir = 1; // assume forward
2012 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002013 case '(': match = ')'; break;
2014 case '[': match = ']'; break;
2015 case '{': match = '}'; break;
2016 case ')': match = '('; dir = -1; break;
2017 case ']': match = '['; dir = -1; break;
2018 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002019 }
2020 for (q = p + dir; text <= q && q < end; q += dir) {
2021 // look for match, count levels of pairs (( ))
2022 if (*q == c)
2023 level++; // increase pair levels
2024 if (*q == match)
2025 level--; // reduce pair level
2026 if (level == 0)
2027 break; // found matching pair
2028 }
2029 if (level != 0)
2030 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002031 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002032}
2033
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002034#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002035// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00002036static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002037{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002038 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002039
2040 // we found half of a pair
2041 q = find_pair(p, *p); // get loc of matching char
2042 if (q == NULL) {
2043 indicate_error('3'); // no matching char
2044 } else {
2045 // "q" now points to matching pair
2046 save_dot = dot; // remember where we are
2047 dot = q; // go to new loc
2048 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002049 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002050 dot = save_dot; // go back to old loc
2051 refresh(FALSE);
2052 }
2053}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002054#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002055
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002056// open a hole in text[]
2057// might reallocate text[]! use p += text_hole_make(p, ...),
2058// and be careful to not use pointers into potentially freed text[]!
2059static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002060{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002061 uintptr_t bias = 0;
2062
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002063 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002064 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002065 end += size; // adjust the new END
2066 if (end >= (text + text_size)) {
2067 char *new_text;
2068 text_size += end - (text + text_size) + 10240;
2069 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002070 bias = (new_text - text);
2071 screenbegin += bias;
2072 dot += bias;
2073 end += bias;
2074 p += bias;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002075#if ENABLE_FEATURE_VI_YANKMARK
2076 {
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +02002077 unsigned i;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002078 for (i = 0; i < ARRAY_SIZE(mark); i++)
2079 if (mark[i])
2080 mark[i] += bias;
2081 }
2082#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +00002083 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002084 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00002085 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002086 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00002087 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002088 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002089}
2090
2091// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00002092static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002093{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002094 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002095 int cnt, hole_size;
2096
2097 // move forwards, from beginning
2098 // assume p <= q
2099 src = q + 1;
2100 dest = p;
2101 if (q < p) { // they are backward- swap them
2102 src = p + 1;
2103 dest = q;
2104 }
2105 hole_size = q - p + 1;
2106 cnt = end - src;
2107 if (src < text || src > end)
2108 goto thd0;
2109 if (dest < text || dest >= end)
2110 goto thd0;
2111 if (src >= end)
2112 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00002113 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002114 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002115 end = end - hole_size; // adjust the new END
2116 if (dest >= end)
2117 dest = end - 1; // make sure dest in below end-1
2118 if (end <= text)
2119 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00002120 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002121 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002122 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002123}
2124
2125// copy text into register, then delete text.
2126// if dist <= 0, do not include, or go past, a NewLine
2127//
Denis Vlasenko33875382008-06-21 20:31:50 +00002128static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002129{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002130 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002131
2132 // make sure start <= stop
2133 if (start > stop) {
2134 // they are backwards, reverse them
2135 p = start;
2136 start = stop;
2137 stop = p;
2138 }
2139 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00002140 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002141 p = start;
2142 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002143 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002144 // dont go past a NewLine
2145 for (; p + 1 <= stop; p++) {
2146 if (p[1] == '\n') {
2147 stop = p; // "stop" just before NewLine
2148 break;
2149 }
2150 }
2151 }
2152 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002153#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002154 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002155#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002156 if (yf == YANKDEL) {
2157 p = text_hole_delete(start, stop);
2158 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002159 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002160}
2161
2162static void show_help(void)
2163{
2164 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002165#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002166 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002167#endif
2168#if ENABLE_FEATURE_VI_DOT_CMD
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02002169 "\n\tLast command repeat with ."
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002170#endif
2171#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00002172 "\n\tLine marking with 'x"
2173 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002174#endif
2175#if ENABLE_FEATURE_VI_READONLY
Walter Harmsb9ba5802011-06-27 02:59:37 +02002176 //not implemented: "\n\tReadonly if vi is called as \"view\""
2177 //redundant: usage text says this too: "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002178#endif
2179#if ENABLE_FEATURE_VI_SET
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02002180 "\n\tSome colon mode commands with :"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002181#endif
2182#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002183 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002184#endif
2185#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002186 "\n\tSignal catching- ^C"
2187 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002188#endif
2189#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002190 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002191#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002192 );
2193}
2194
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002195#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002196static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002197{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002198 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002199 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002200 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002201 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002202 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002203 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002204 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002205 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002206 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002207}
2208
2209static void end_cmd_q(void)
2210{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002211#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002212 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002213#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002214 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002215}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002216#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002217
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002218#if ENABLE_FEATURE_VI_YANKMARK \
2219 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2220 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002221// might reallocate text[]! use p += string_insert(p, ...),
2222// and be careful to not use pointers into potentially freed text[]!
2223static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002224{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002225 uintptr_t bias;
2226 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002227
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002228 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002229 bias = text_hole_make(p, i);
2230 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002231 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002232#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002233 {
2234 int cnt;
2235 for (cnt = 0; *s != '\0'; s++) {
2236 if (*s == '\n')
2237 cnt++;
2238 }
2239 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2240 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002241#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002242 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002243}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002244#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002245
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002246#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002247static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002248{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002249 int cnt = q - p;
2250 if (cnt < 0) { // they are backwards- reverse them
2251 p = q;
2252 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002253 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002254 free(reg[dest]); // if already a yank register, free it
2255 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002256 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002257}
2258
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002259static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002260{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002261 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002262
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002263 c = 'D'; // default to D-reg
2264 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002265 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002266 if (YDreg == 26)
2267 c = 'D';
2268 if (YDreg == 27)
2269 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002270 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002271}
2272
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002273static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002274{
2275 // A context is defined to be "modifying text"
2276 // Any modifying command establishes a new context.
2277
2278 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002279 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002280 // we are trying to modify text[]- make this the current context
2281 mark[27] = mark[26]; // move cur to prev
2282 mark[26] = dot; // move local to cur
2283 context_start = prev_line(prev_line(dot));
2284 context_end = next_line(next_line(dot));
2285 //loiter= start_loiter= now;
2286 }
2287 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002288}
2289
Denis Vlasenkof882f082007-12-23 02:36:51 +00002290static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002291{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002292 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002293
2294 // the current context is in mark[26]
2295 // the previous context is in mark[27]
2296 // only swap context if other context is valid
2297 if (text <= mark[27] && mark[27] <= end - 1) {
2298 tmp = mark[27];
2299 mark[27] = mark[26];
2300 mark[26] = tmp;
2301 p = mark[26]; // where we are going- previous context
2302 context_start = prev_line(prev_line(prev_line(p)));
2303 context_end = next_line(next_line(next_line(p)));
2304 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002305 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002306}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002307#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002308
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002309//----- Set terminal attributes --------------------------------
2310static void rawmode(void)
2311{
2312 tcgetattr(0, &term_orig);
2313 term_vi = term_orig;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002314 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG on - allow intr's
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002315 term_vi.c_iflag &= (~IXON & ~ICRNL);
2316 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002317 term_vi.c_cc[VMIN] = 1;
2318 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002319 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002320 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002321}
2322
2323static void cookmode(void)
2324{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002325 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002326 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002327}
2328
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002329#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002330//----- Come here when we get a window resize signal ---------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002331static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002332{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002333 int save_errno = errno;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002334 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002335 signal(SIGWINCH, winch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002336 query_screen_dimensions();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002337 new_screen(rows, columns); // get memory for virtual screen
2338 redraw(TRUE); // re-draw the screen
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002339 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002340}
2341
2342//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002343static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002344{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002345 int save_errno = errno;
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002346 rawmode(); // terminal to "raw"
2347 last_status_cksum = 0; // force status update
2348 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002349
2350 signal(SIGTSTP, suspend_sig);
2351 signal(SIGCONT, SIG_DFL);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002352 //kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
2353 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002354}
2355
2356//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002357static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002358{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002359 int save_errno = errno;
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002360 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002361 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002362
2363 signal(SIGCONT, cont_sig);
2364 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002365 kill(my_pid, SIGTSTP);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002366 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002367}
2368
2369//----- Come here when we get a signal ---------------------------
2370static void catch_sig(int sig)
2371{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002372 signal(SIGINT, catch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002373 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002374}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002375#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002376
Denys Vlasenko606291b2009-09-23 23:15:43 +02002377static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002378{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002379 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002380
Denys Vlasenko606291b2009-09-23 23:15:43 +02002381 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002382 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002383 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002384}
2385
2386//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002387static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002388{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002389 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002390
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002391 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002392 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002393 if (c == -1) { // EOF/error
2394 go_bottom_and_clear_to_eol();
2395 cookmode(); // terminal to "cooked"
2396 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002397 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002398 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002399}
2400
2401//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002402static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002403{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002404 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002405
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002406#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002407 if (!adding2q) {
2408 // we are not adding to the q.
2409 // but, we may be reading from a q
2410 if (ioq == 0) {
2411 // there is no current q, read from STDIN
2412 c = readit(); // get the users input
2413 } else {
2414 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002415 // careful with correct sign expansion!
2416 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002417 if (c == '\0') {
2418 // the end of the q, read from STDIN
2419 free(ioq_start);
2420 ioq_start = ioq = 0;
2421 c = readit(); // get the users input
2422 }
2423 }
2424 } else {
2425 // adding STDIN chars to q
2426 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002427 if (lmc_len >= MAX_INPUT_LEN - 1) {
2428 status_line_bold("last_modifying_cmd overrun");
2429 } else {
2430 // add new char to q
2431 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002432 }
2433 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002434#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002435 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002436#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002437 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002438}
2439
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002440// Get input line (uses "status line" area)
2441static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002442{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002443 // char [MAX_INPUT_LEN]
2444#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002445
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002446 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002447 int i;
2448
2449 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002450 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002451 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002452 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002453
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002454 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002455 while (i < MAX_INPUT_LEN) {
2456 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002457 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002458 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002459 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002460 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002461 buf[--i] = '\0';
2462 write1("\b \b"); // erase char on screen
2463 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002464 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002465 } else if (c > 0 && c < 256) { // exclude Unicode
2466 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002467 buf[i] = c;
2468 buf[++i] = '\0';
2469 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002470 }
2471 }
2472 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002473 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002474#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002475}
2476
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002477static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002478{
2479 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002480 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002481
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002482 cnt = -1;
Denys Vlasenkodef47832010-04-18 20:39:41 -07002483 if (fn && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002484 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002485 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002486}
2487
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002488// might reallocate text[]!
2489static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002490{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002491 int cnt = -1;
2492 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002493 struct stat statbuf;
2494
2495 /* Validate file */
2496 if (stat(fn, &statbuf) < 0) {
maxwen27116ba2015-08-14 21:41:28 +02002497 status_line_bold_errno(fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002498 goto fi0;
2499 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002500 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002501 // This is not a regular file
maxwen27116ba2015-08-14 21:41:28 +02002502 status_line_bold("'%s' is not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002503 goto fi0;
2504 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002505 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002506 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002507 goto fi0;
2508 }
2509
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002510 // read file to buffer
2511 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002512 if (fd < 0) {
maxwen27116ba2015-08-14 21:41:28 +02002513 status_line_bold_errno(fn);
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002514 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002515 }
maxwen27116ba2015-08-14 21:41:28 +02002516 size = (statbuf.st_size < INT_MAX ? (int)statbuf.st_size : INT_MAX);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002517 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002518 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002519 if (cnt < 0) {
maxwen27116ba2015-08-14 21:41:28 +02002520 status_line_bold_errno(fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002521 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002522 } else if (cnt < size) {
2523 // There was a partial read, shrink unused space text[]
maxwen27116ba2015-08-14 21:41:28 +02002524 p = text_hole_delete(p + cnt, p + size - 1); // un-do buffer insert
2525 status_line_bold("can't read '%s'", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002526 }
2527 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002528 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002529 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002530 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002531#if ENABLE_FEATURE_VI_READONLY
2532 if (update_ro_status
2533 && ((access(fn, W_OK) < 0) ||
2534 /* root will always have access()
2535 * so we check fileperms too */
2536 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2537 )
2538 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002539 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002540 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002541#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002542 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002543}
2544
Denis Vlasenko33875382008-06-21 20:31:50 +00002545static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002546{
2547 int fd, cnt, charcnt;
2548
2549 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002550 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002551 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002552 }
Denis Vlasenko8abae882008-05-03 11:35:59 +00002553 /* By popular request we do not open file with O_TRUNC,
2554 * but instead ftruncate() it _after_ successful write.
2555 * Might reduce amount of data lost on power fail etc.
2556 */
2557 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002558 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002559 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002560 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002561 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002562 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002563 if (charcnt == cnt) {
2564 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002565 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002566 } else {
2567 charcnt = 0;
2568 }
2569 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002570 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002571}
2572
2573//----- Terminal Drawing ---------------------------------------
2574// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002575// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002576// screen coordinates
2577// 0,0 ... 0,79
2578// 1,0 ... 1,79
2579// . ... .
2580// . ... .
2581// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002582// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002583
2584//----- Move the cursor to row x col (count from 0, not 1) -------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002585static void place_cursor(int row, int col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002586{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002587 char cm1[sizeof(ESC_SET_CURSOR_POS) + sizeof(int)*3 * 2];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002588
2589 if (row < 0) row = 0;
2590 if (row >= rows) row = rows - 1;
2591 if (col < 0) col = 0;
2592 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002593
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002594 sprintf(cm1, ESC_SET_CURSOR_POS, row + 1, col + 1);
2595 write1(cm1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002596}
2597
2598//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002599static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002600{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002601 write1(ESC_CLEAR2EOL);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002602}
2603
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002604static void go_bottom_and_clear_to_eol(void)
2605{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002606 place_cursor(rows - 1, 0);
2607 clear_to_eol();
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002608}
2609
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002610//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002611static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002612{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002613 write1(ESC_CLEAR2EOS);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002614}
2615
2616//----- Start standout mode ------------------------------------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002617static void standout_start(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002618{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002619 write1(ESC_BOLD_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002620}
2621
2622//----- End standout mode --------------------------------------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002623static void standout_end(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002624{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002625 write1(ESC_NORM_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002626}
2627
2628//----- Flash the screen --------------------------------------
2629static void flash(int h)
2630{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002631 standout_start();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002632 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002633 mysleep(h);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002634 standout_end();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002635 redraw(TRUE);
2636}
2637
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002638static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002639{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002640#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002641 if (crashme > 0)
2642 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002643#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002644 if (!err_method) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002645 write1(ESC_BELL);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002646 } else {
2647 flash(10);
2648 }
2649}
2650
2651//----- Screen[] Routines --------------------------------------
2652//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002653static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002654{
2655 memset(screen, ' ', screensize); // clear new screen
2656}
2657
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002658static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002659{
2660 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002661 char *e = buf + count;
2662
Paul Fox8552aec2005-09-16 12:20:05 +00002663 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002664 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002665 return sum;
2666}
2667
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002668//----- Draw the status line at bottom of the screen -------------
2669static void show_status_line(void)
2670{
Paul Foxc3504852005-09-16 12:48:18 +00002671 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002672
Paul Fox8552aec2005-09-16 12:20:05 +00002673 // either we already have an error or status message, or we
2674 // create one.
2675 if (!have_status_msg) {
2676 cnt = format_edit_status();
2677 cksum = bufsum(status_buffer, cnt);
2678 }
2679 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002680 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002681 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002682 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002683 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002684 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002685 (columns - 1) ) {
2686 have_status_msg = 0;
2687 Hit_Return();
2688 }
2689 have_status_msg = 0;
2690 }
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002691 place_cursor(crow, ccol); // put cursor back in correct place
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002692 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002693 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002694}
2695
2696//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002697// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002698static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002699{
2700 va_list args;
2701
2702 va_start(args, format);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002703 strcpy(status_buffer, ESC_BOLD_TEXT);
2704 vsprintf(status_buffer + sizeof(ESC_BOLD_TEXT)-1, format, args);
2705 strcat(status_buffer, ESC_NORM_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002706 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002707
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002708 have_status_msg = 1 + sizeof(ESC_BOLD_TEXT) + sizeof(ESC_NORM_TEXT) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002709}
2710
maxwen27116ba2015-08-14 21:41:28 +02002711static void status_line_bold_errno(const char *fn)
2712{
2713 status_line_bold("'%s' %s", fn, strerror(errno));
2714}
2715
Paul Fox8552aec2005-09-16 12:20:05 +00002716// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002717static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002718{
2719 va_list args;
2720
2721 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002722 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002723 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002724
2725 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002726}
2727
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002728// copy s to buf, convert unprintable
2729static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002730{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002731 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002732 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002733
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002734 buf[0] = '\0';
2735 if (!s[0])
2736 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002737
2738 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002739 for (; *s; s++) {
2740 int c_is_no_print;
2741
2742 c = *s;
2743 c_is_no_print = (c & 0x80) && !Isprint(c);
2744 if (c_is_no_print) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002745 strcpy(d, ESC_NORM_TEXT);
2746 d += sizeof(ESC_NORM_TEXT)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002747 c = '.';
2748 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002749 if (c < ' ' || c == 0x7f) {
2750 *d++ = '^';
2751 c |= '@'; /* 0x40 */
2752 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002753 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002754 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002755 *d++ = c;
2756 *d = '\0';
2757 if (c_is_no_print) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002758 strcpy(d, ESC_BOLD_TEXT);
2759 d += sizeof(ESC_BOLD_TEXT)-1;
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002760 }
2761 if (*s == '\n') {
2762 *d++ = '$';
2763 *d = '\0';
2764 }
2765 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002766 break;
2767 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002768}
2769
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002770static void not_implemented(const char *s)
2771{
2772 char buf[MAX_INPUT_LEN];
2773
2774 print_literal(buf, s);
2775 status_line_bold("\'%s\' is not implemented", buf);
2776}
2777
2778// show file status on status line
2779static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002780{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002781 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002782
2783#define tot format_edit_status__tot
2784
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002785 int cur, percent, ret, trunc_at;
2786
Paul Fox8552aec2005-09-16 12:20:05 +00002787 // file_modified is now a counter rather than a flag. this
2788 // helps reduce the amount of line counting we need to do.
2789 // (this will cause a mis-reporting of modified status
2790 // once every MAXINT editing operations.)
2791
2792 // it would be nice to do a similar optimization here -- if
2793 // we haven't done a motion that could have changed which line
2794 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002795 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002796
2797 // reduce counting -- the total lines can't have
2798 // changed if we haven't done any edits.
2799 if (file_modified != last_file_modified) {
2800 tot = cur + count_lines(dot, end - 1) - 1;
2801 last_file_modified = file_modified;
2802 }
2803
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002804 // current line percent
2805 // ------------- ~~ ----------
2806 // total lines 100
2807 if (tot > 0) {
2808 percent = (100 * cur) / tot;
2809 } else {
2810 cur = tot = 0;
2811 percent = 100;
2812 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002813
Paul Fox8552aec2005-09-16 12:20:05 +00002814 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2815 columns : STATUS_BUFFER_LEN-1;
2816
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002817 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002818#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002819 "%c %s%s%s %d/%d %d%%",
2820#else
2821 "%c %s%s %d/%d %d%%",
2822#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002823 cmd_mode_indicator[cmd_mode & 3],
2824 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002825#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002826 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002827#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002828 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002829 cur, tot, percent);
2830
2831 if (ret >= 0 && ret < trunc_at)
2832 return ret; /* it all fit */
2833
2834 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002835#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002836}
2837
2838//----- Force refresh of all Lines -----------------------------
2839static void redraw(int full_screen)
2840{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002841 place_cursor(0, 0);
2842 clear_to_eos();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002843 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002844 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002845 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002846 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002847}
2848
2849//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002850static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002851{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002852 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002853 int co;
2854 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002855 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002856
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002857 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002858 co = 0;
2859 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002860 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002861 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002862 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002863 if (c == '\n')
2864 break;
2865 if ((c & 0x80) && !Isprint(c)) {
2866 c = '.';
2867 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002868 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002869 if (c == '\t') {
2870 c = ' ';
2871 // co % 8 != 7
2872 while ((co % tabstop) != (tabstop - 1)) {
2873 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002874 }
2875 } else {
2876 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002877 if (c == 0x7f)
2878 c = '?';
2879 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002880 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002881 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002882 }
2883 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002884 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002885 // discard scrolled-off-to-the-left portion,
2886 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002887 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002888 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002889 co -= tabstop;
2890 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002891 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002892 if (src >= end)
2893 break;
2894 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002895 // check "short line, gigantic offset" case
2896 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002897 ofs = co;
2898 // discard last scrolled off part
2899 co -= ofs;
2900 dest += ofs;
2901 // fill the rest with spaces
2902 if (co < columns)
2903 memset(&dest[co], ' ', columns - co);
2904 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002905}
2906
2907//----- Refresh the changed screen lines -----------------------
2908// Copy the source line from text[] into the buffer and note
2909// if the current screenline is different from the new buffer.
2910// If they differ then that line needs redrawing on the terminal.
2911//
2912static void refresh(int full_screen)
2913{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002914#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002915
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002916 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002917 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002918
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +02002919 if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002920 unsigned c = columns, r = rows;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002921 query_screen_dimensions();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002922 full_screen |= (c - columns) | (r - rows);
2923 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002924 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2925 tp = screenbegin; // index into text[] of top line
2926
2927 // compare text[] to screen[] and mark screen[] lines that need updating
2928 for (li = 0; li < rows - 1; li++) {
2929 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002930 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002931 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002932 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002933
2934 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002935 if (tp < end) {
2936 char *t = memchr(tp, '\n', end - tp);
2937 if (!t) t = end - 1;
2938 tp = t + 1;
2939 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002940
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002941 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002942 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002943 cs = 0;
2944 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002945 sp = &screen[li * columns]; // start of screen line
2946 if (full_screen) {
2947 // force re-draw of every single column from 0 - columns-1
2948 goto re0;
2949 }
2950 // compare newly formatted buffer with virtual screen
2951 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002952 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002953 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002954 changed = TRUE; // mark for redraw
2955 break;
2956 }
2957 }
2958
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002959 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002960 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002961 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002962 changed = TRUE; // mark for redraw
2963 break;
2964 }
2965 }
2966 // now, cs is index of first diff, and ce is index of last diff
2967
2968 // if horz offset has changed, force a redraw
2969 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002970 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002971 changed = TRUE;
2972 }
2973
2974 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002975 if (cs < 0) cs = 0;
2976 if (ce > columns - 1) ce = columns - 1;
2977 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002978 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002979 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002980 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002981 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002982 place_cursor(li, cs);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002983 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002984 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002985 }
2986 }
2987
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002988 place_cursor(crow, ccol);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002989
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002990 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002991#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002992}
2993
Eric Andersen3f980402001-04-04 17:31:15 +00002994//---------------------------------------------------------------------
2995//----- the Ascii Chart -----------------------------------------------
2996//
2997// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
2998// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
2999// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
3000// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
3001// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
3002// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
3003// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
3004// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
3005// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
3006// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
3007// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
3008// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
3009// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
3010// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
3011// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
3012// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
3013//---------------------------------------------------------------------
3014
3015//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003016static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00003017{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003018 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003019 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003020 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00003021 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003022 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003023
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00003024// c1 = c; // quiet the compiler
3025// cnt = yf = 0; // quiet the compiler
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003026// p = q = save_dot = buf; // quiet the compiler
3027 memset(buf, '\0', sizeof(buf));
Eric Andersenbff7a602001-11-17 07:15:43 +00003028
Paul Fox8552aec2005-09-16 12:20:05 +00003029 show_status_line();
3030
Eric Andersenbff7a602001-11-17 07:15:43 +00003031 /* if this is a cursor key, skip these checks */
3032 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003033 case KEYCODE_UP:
3034 case KEYCODE_DOWN:
3035 case KEYCODE_LEFT:
3036 case KEYCODE_RIGHT:
3037 case KEYCODE_HOME:
3038 case KEYCODE_END:
3039 case KEYCODE_PAGEUP:
3040 case KEYCODE_PAGEDOWN:
3041 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00003042 goto key_cmd_mode;
3043 }
3044
Eric Andersen3f980402001-04-04 17:31:15 +00003045 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003046 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003047 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003048 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00003049 // we are 'R'eplacing the current *dot with new char
3050 if (*dot == '\n') {
3051 // don't Replace past E-o-l
3052 cmd_mode = 1; // convert to insert
3053 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003054 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003055 if (c != 27)
3056 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3057 dot = char_insert(dot, c); // insert new char
3058 }
3059 goto dc1;
3060 }
3061 }
3062 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003063 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003064 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00003065 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003066 if (1 <= c || Isprint(c)) {
3067 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00003068 }
3069 goto dc1;
3070 }
3071
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003072 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00003073 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00003074 //case 0x01: // soh
3075 //case 0x09: // ht
3076 //case 0x0b: // vt
3077 //case 0x0e: // so
3078 //case 0x0f: // si
3079 //case 0x10: // dle
3080 //case 0x11: // dc1
3081 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003082#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00003083 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00003084 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003085 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003086#endif
Eric Andersen822c3832001-05-07 17:37:43 +00003087 //case 0x16: // syn
3088 //case 0x17: // etb
3089 //case 0x18: // can
3090 //case 0x1c: // fs
3091 //case 0x1d: // gs
3092 //case 0x1e: // rs
3093 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003094 //case '!': // !-
3095 //case '#': // #-
3096 //case '&': // &-
3097 //case '(': // (-
3098 //case ')': // )-
3099 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003100 //case '=': // =-
3101 //case '@': // @-
3102 //case 'F': // F-
3103 //case 'K': // K-
3104 //case 'Q': // Q-
3105 //case 'S': // S-
3106 //case 'T': // T-
3107 //case 'V': // V-
3108 //case '[': // [-
3109 //case '\\': // \-
3110 //case ']': // ]-
3111 //case '_': // _-
3112 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003113 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003114 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003115 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00003116 buf[0] = c;
3117 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003118 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003119 end_cmd_q(); // stop adding to q
3120 case 0x00: // nul- ignore
3121 break;
3122 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003123 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00003124 dot_scroll(rows - 2, -1);
3125 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003126 case 4: // ctrl-D scroll down half screen
3127 dot_scroll((rows - 2) / 2, 1);
3128 break;
3129 case 5: // ctrl-E scroll down one line
3130 dot_scroll(1, 1);
3131 break;
3132 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003133 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003134 dot_scroll(rows - 2, 1);
3135 break;
3136 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00003137 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003138 break;
3139 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003140 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003141 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003142 case 0x7f: // DEL- move left (This may be ERASE char)
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003143 do {
3144 dot_left();
3145 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003146 break;
3147 case 10: // Newline ^J
3148 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003149 case KEYCODE_DOWN: // cursor key Down
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003150 do {
3151 dot_next(); // go to next B-o-l
3152 // try stay in same col
3153 dot = move_to_col(dot, ccol + offset);
3154 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003155 break;
3156 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003157 case 18: // ctrl-R force redraw
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02003158 place_cursor(0, 0);
3159 clear_to_eos();
3160 //mysleep(10); // why???
Eric Andersen3f980402001-04-04 17:31:15 +00003161 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003162 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003163 refresh(TRUE); // this will redraw the entire display
3164 break;
3165 case 13: // Carriage Return ^M
3166 case '+': // +- goto next line
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003167 do {
3168 dot_next();
3169 dot_skip_over_ws();
3170 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003171 break;
3172 case 21: // ctrl-U scroll up half screen
3173 dot_scroll((rows - 2) / 2, -1);
3174 break;
3175 case 25: // ctrl-Y scroll up one line
3176 dot_scroll(1, -1);
3177 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003178 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003179 if (cmd_mode == 0)
3180 indicate_error(c);
3181 cmd_mode = 0; // stop insrting
3182 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003183 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003184 break;
3185 case ' ': // move right
3186 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003187 case KEYCODE_RIGHT: // Cursor Key Right
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003188 do {
3189 dot_right();
3190 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003191 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003192#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003193 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003194 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3195 if ((unsigned)c1 <= 25) { // a-z?
3196 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003197 } else {
3198 indicate_error(c);
3199 }
3200 break;
3201 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003202 c1 = (get_one_char() | 0x20) - 'a';
3203 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003204 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003205 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003206 if (text <= q && q < end) {
3207 dot = q;
3208 dot_begin(); // go to B-o-l
3209 dot_skip_over_ws();
3210 }
3211 } else if (c1 == '\'') { // goto previous context
3212 dot = swap_context(dot); // swap current and previous context
3213 dot_begin(); // go to B-o-l
3214 dot_skip_over_ws();
3215 } else {
3216 indicate_error(c);
3217 }
3218 break;
3219 case 'm': // m- Mark a line
3220 // this is really stupid. If there are any inserts or deletes
3221 // between text[0] and dot then this mark will not point to the
3222 // correct location! It could be off by many lines!
3223 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003224 c1 = (get_one_char() | 0x20) - 'a';
3225 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003226 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003227 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003228 } else {
3229 indicate_error(c);
3230 }
3231 break;
3232 case 'P': // P- Put register before
3233 case 'p': // p- put register after
3234 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003235 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003236 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003237 break;
3238 }
3239 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003240 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003241 if (c == 'P') {
3242 dot_begin(); // putting lines- Put above
3243 }
3244 if (c == 'p') {
3245 // are we putting after very last line?
3246 if (end_line(dot) == (end - 1)) {
3247 dot = end; // force dot to end of text[]
3248 } else {
3249 dot_next(); // next line, then put before
3250 }
3251 }
3252 } else {
3253 if (c == 'p')
3254 dot_right(); // move to right, can move to NL
3255 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003256 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003257 end_cmd_q(); // stop adding to q
3258 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003259 case 'U': // U- Undo; replace current line with original version
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003260 if (reg[Ureg] != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003261 p = begin_line(dot);
3262 q = end_line(dot);
3263 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003264 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003265 dot = p;
3266 dot_skip_over_ws();
3267 }
3268 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003269#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003270 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003271 case KEYCODE_END: // Cursor Key End
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003272 for (;;) {
3273 dot = end_line(dot);
3274 if (--cmdcnt <= 0)
3275 break;
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003276 dot_next();
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003277 }
Eric Andersen3f980402001-04-04 17:31:15 +00003278 break;
3279 case '%': // %- find matching char of pair () [] {}
3280 for (q = dot; q < end && *q != '\n'; q++) {
3281 if (strchr("()[]{}", *q) != NULL) {
3282 // we found half of a pair
3283 p = find_pair(q, *q);
3284 if (p == NULL) {
3285 indicate_error(c);
3286 } else {
3287 dot = p;
3288 }
3289 break;
3290 }
3291 }
3292 if (*q == '\n')
3293 indicate_error(c);
3294 break;
3295 case 'f': // f- forward to a user specified char
3296 last_forward_char = get_one_char(); // get the search char
3297 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003298 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003299 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003300 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003301 case ';': // ;- look at rest of line for last forward char
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003302 do {
3303 if (last_forward_char == 0)
3304 break;
3305 q = dot + 1;
3306 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3307 q++;
3308 }
3309 if (*q == last_forward_char)
3310 dot = q;
3311 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003312 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003313 case ',': // repeat latest 'f' in opposite direction
Paul Foxb5ee8db2008-02-14 01:17:01 +00003314 if (last_forward_char == 0)
3315 break;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003316 do {
3317 q = dot - 1;
3318 while (q >= text && *q != '\n' && *q != last_forward_char) {
3319 q--;
3320 }
3321 if (q >= text && *q == last_forward_char)
3322 dot = q;
3323 } while (--cmdcnt > 0);
Paul Foxb5ee8db2008-02-14 01:17:01 +00003324 break;
3325
Eric Andersen3f980402001-04-04 17:31:15 +00003326 case '-': // -- goto prev line
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003327 do {
3328 dot_prev();
3329 dot_skip_over_ws();
3330 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003331 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003332#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003333 case '.': // .- repeat the last modifying command
3334 // Stuff the last_modifying_cmd back into stdin
3335 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003336 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003337 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003338 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003339 }
3340 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003341#endif
3342#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003343 case '?': // /- search for a pattern
3344 case '/': // /- search for a pattern
3345 buf[0] = c;
3346 buf[1] = '\0';
3347 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003348 if (q[0] && !q[1]) {
3349 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003350 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003351 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003352 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003353 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003354 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003355 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003356 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003357 goto dc3; // now find the pattern
3358 }
3359 // user changed mind and erased the "/"- do nothing
3360 break;
3361 case 'N': // N- backward search for last pattern
Eric Andersen3f980402001-04-04 17:31:15 +00003362 dir = BACK; // assume BACKWARD search
3363 p = dot - 1;
3364 if (last_search_pattern[0] == '?') {
3365 dir = FORWARD;
3366 p = dot + 1;
3367 }
3368 goto dc4; // now search for pattern
3369 break;
3370 case 'n': // n- repeat search for last pattern
3371 // search rest of text[] starting at next char
3372 // if search fails return orignal "p" not the "p+1" address
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003373 do {
3374 const char *msg;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003375 dc3:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003376 dir = FORWARD; // assume FORWARD search
3377 p = dot + 1;
3378 if (last_search_pattern[0] == '?') {
3379 dir = BACK;
3380 p = dot - 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003381 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003382 dc4:
3383 q = char_search(p, last_search_pattern + 1, dir, FULL);
3384 if (q != NULL) {
3385 dot = q; // good search, update "dot"
3386 msg = NULL;
3387 goto dc2;
3388 }
3389 // no pattern found between "dot" and "end"- continue at top
3390 p = text;
3391 if (dir == BACK) {
3392 p = end - 1;
3393 }
3394 q = char_search(p, last_search_pattern + 1, dir, FULL);
3395 if (q != NULL) { // found something
3396 dot = q; // found new pattern- goto it
3397 msg = "search hit BOTTOM, continuing at TOP";
3398 if (dir == BACK) {
3399 msg = "search hit TOP, continuing at BOTTOM";
3400 }
3401 } else {
3402 msg = "Pattern not found";
3403 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003404 dc2:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003405 if (msg)
3406 status_line_bold("%s", msg);
3407 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003408 break;
3409 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003410 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003411 if (q != NULL) { // found blank line
3412 dot = next_line(q); // move to next blank line
3413 }
3414 break;
3415 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003416 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003417 if (q != NULL) { // found blank line
3418 dot = next_line(q); // move to next blank line
3419 }
3420 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003421#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003422 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003423 case '1': // 1-
3424 case '2': // 2-
3425 case '3': // 3-
3426 case '4': // 4-
3427 case '5': // 5-
3428 case '6': // 6-
3429 case '7': // 7-
3430 case '8': // 8-
3431 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003432 if (c == '0' && cmdcnt < 1) {
3433 dot_begin(); // this was a standalone zero
3434 } else {
3435 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3436 }
3437 break;
3438 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003439 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003440#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003441 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003442#else
Eric Andersen822c3832001-05-07 17:37:43 +00003443 if (*p == ':')
3444 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003445 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003446 if (cnt <= 0)
3447 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003448 if (strncmp(p, "quit", cnt) == 0
3449 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003450 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003451 if (file_modified && p[1] != '!') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003452 status_line_bold("No write since last change (:%s! overrides)", p);
Eric Andersen3f980402001-04-04 17:31:15 +00003453 } else {
3454 editing = 0;
3455 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003456 } else if (strncmp(p, "write", cnt) == 0
3457 || strncmp(p, "wq", cnt) == 0
3458 || strncmp(p, "wn", cnt) == 0
3459 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003460 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003461 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003462 if (cnt < 0) {
3463 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003464 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003465 } else {
3466 file_modified = 0;
3467 last_file_modified = -1;
maxwen27116ba2015-08-14 21:41:28 +02003468 status_line("'%s' %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003469 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3470 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3471 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003472 editing = 0;
3473 }
Eric Andersen3f980402001-04-04 17:31:15 +00003474 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003475 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003476 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003477 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003478 dot = find_line(j); // go to line # j
3479 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003480 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003481 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003482 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003483#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003484 break;
3485 case '<': // <- Left shift something
3486 case '>': // >- Right shift something
3487 cnt = count_lines(text, dot); // remember what line we are on
3488 c1 = get_one_char(); // get the type of thing to delete
3489 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003490 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003491 p = begin_line(p);
3492 q = end_line(q);
3493 i = count_lines(p, q); // # of lines we are shifting
3494 for ( ; i > 0; i--, p = next_line(p)) {
3495 if (c == '<') {
3496 // shift left- remove tab or 8 spaces
3497 if (*p == '\t') {
3498 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003499 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003500 } else if (*p == ' ') {
3501 // we should be calculating columns, not just SPACE
3502 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003503 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003504 }
3505 }
3506 } else if (c == '>') {
3507 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003508 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003509 }
3510 }
3511 dot = find_line(cnt); // what line were we on
3512 dot_skip_over_ws();
3513 end_cmd_q(); // stop adding to q
3514 break;
3515 case 'A': // A- append at e-o-l
3516 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003517 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003518 case 'a': // a- append after current char
3519 if (*dot != '\n')
3520 dot++;
3521 goto dc_i;
3522 break;
3523 case 'B': // B- back a blank-delimited Word
3524 case 'E': // E- end of a blank-delimited word
3525 case 'W': // W- forward a blank-delimited word
Eric Andersen3f980402001-04-04 17:31:15 +00003526 dir = FORWARD;
3527 if (c == 'B')
3528 dir = BACK;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003529 do {
3530 if (c == 'W' || isspace(dot[dir])) {
3531 dot = skip_thing(dot, 1, dir, S_TO_WS);
3532 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3533 }
3534 if (c != 'W')
3535 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3536 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003537 break;
3538 case 'C': // C- Change to e-o-l
3539 case 'D': // D- delete to e-o-l
3540 save_dot = dot;
3541 dot = dollar_line(dot); // move to before NL
3542 // copy text into a register and delete
3543 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3544 if (c == 'C')
3545 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003546#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003547 if (c == 'D')
3548 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003549#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003550 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003551 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003552 c1 = get_one_char();
3553 if (c1 != 'g') {
3554 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003555 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003556 buf[2] = '\0';
3557 not_implemented(buf);
3558 break;
3559 }
3560 if (cmdcnt == 0)
3561 cmdcnt = 1;
3562 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003563 case 'G': // G- goto to a line number (default= E-O-F)
3564 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003565 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003566 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003567 }
3568 dot_skip_over_ws();
3569 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003570 case 'H': // H- goto top line on screen
3571 dot = screenbegin;
3572 if (cmdcnt > (rows - 1)) {
3573 cmdcnt = (rows - 1);
3574 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003575 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003576 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003577 }
Eric Andersen3f980402001-04-04 17:31:15 +00003578 dot_skip_over_ws();
3579 break;
3580 case 'I': // I- insert before first non-blank
3581 dot_begin(); // 0
3582 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003583 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003584 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003585 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003586 dc_i:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003587 cmd_mode = 1; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003588 break;
3589 case 'J': // J- join current and next lines together
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003590 do {
3591 dot_end(); // move to NL
3592 if (dot < end - 1) { // make sure not last char in text[]
3593 *dot++ = ' '; // replace NL with space
3594 file_modified++;
3595 while (isblank(*dot)) { // delete leading WS
3596 dot_delete();
3597 }
Eric Andersen3f980402001-04-04 17:31:15 +00003598 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003599 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003600 end_cmd_q(); // stop adding to q
3601 break;
3602 case 'L': // L- goto bottom line on screen
3603 dot = end_screen();
3604 if (cmdcnt > (rows - 1)) {
3605 cmdcnt = (rows - 1);
3606 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003607 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003608 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003609 }
Eric Andersen3f980402001-04-04 17:31:15 +00003610 dot_begin();
3611 dot_skip_over_ws();
3612 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003613 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003614 dot = screenbegin;
3615 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3616 dot = next_line(dot);
3617 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003618 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003619 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003620 p = begin_line(dot);
3621 if (p[-1] == '\n') {
3622 dot_prev();
3623 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3624 dot_end();
3625 dot = char_insert(dot, '\n');
3626 } else {
3627 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003628 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003629 dot_prev(); // -
3630 }
3631 goto dc_i;
3632 break;
3633 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003634 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003635 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003636 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003637 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003638 c = 'x';
3639 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003640 case 'X': // X- delete char before dot
3641 case 'x': // x- delete the current char
3642 case 's': // s- substitute the current char
Eric Andersen3f980402001-04-04 17:31:15 +00003643 dir = 0;
3644 if (c == 'X')
3645 dir = -1;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003646 do {
3647 if (dot[dir] != '\n') {
3648 if (c == 'X')
3649 dot--; // delete prev char
3650 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3651 }
3652 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003653 end_cmd_q(); // stop adding to q
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003654 if (c == 's')
3655 goto dc_i; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003656 break;
3657 case 'Z': // Z- if modified, {write}; exit
3658 // ZZ means to save file (if necessary), then exit
3659 c1 = get_one_char();
3660 if (c1 != 'Z') {
3661 indicate_error(c);
3662 break;
3663 }
Paul Foxf0305b72006-03-28 14:18:21 +00003664 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003665 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
maxwen27116ba2015-08-14 21:41:28 +02003666 status_line_bold("'%s' is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003667 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003668 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003669 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003670 if (cnt < 0) {
3671 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003672 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003673 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003674 editing = 0;
3675 }
3676 } else {
3677 editing = 0;
3678 }
3679 break;
3680 case '^': // ^- move to first non-blank on line
3681 dot_begin();
3682 dot_skip_over_ws();
3683 break;
3684 case 'b': // b- back a word
3685 case 'e': // e- end of word
Eric Andersen3f980402001-04-04 17:31:15 +00003686 dir = FORWARD;
3687 if (c == 'b')
3688 dir = BACK;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003689 do {
3690 if ((dot + dir) < text || (dot + dir) > end - 1)
3691 break;
3692 dot += dir;
3693 if (isspace(*dot)) {
3694 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3695 }
3696 if (isalnum(*dot) || *dot == '_') {
3697 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3698 } else if (ispunct(*dot)) {
3699 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3700 }
3701 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003702 break;
3703 case 'c': // c- change something
3704 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003705#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003706 case 'y': // y- yank something
3707 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003708#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003709 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003710 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003711 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003712#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003713 if (c == 'y' || c == 'Y')
3714 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003715#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003716 c1 = 'y';
3717 if (c != 'Y')
3718 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003719 // determine range, and whether it spans lines
3720 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003721 if (c1 == 27) { // ESC- user changed mind and wants out
3722 c = c1 = 27; // Escape- do nothing
3723 } else if (strchr("wW", c1)) {
3724 if (c == 'c') {
3725 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003726 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003727 if (q <= text || q[-1] == '\n')
3728 break;
3729 q--;
3730 }
3731 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003732 dot = yank_delete(p, q, ml, yf); // delete word
3733 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3734 // partial line copy text into a register and delete
3735 dot = yank_delete(p, q, ml, yf); // delete word
3736 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3737 // whole line copy text into a register and delete
3738 dot = yank_delete(p, q, ml, yf); // delete lines
3739 whole = 1;
3740 } else {
3741 // could not recognize object
3742 c = c1 = 27; // error-
3743 ml = 0;
3744 indicate_error(c);
3745 }
3746 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003747 if (c == 'c') {
3748 dot = char_insert(dot, '\n');
3749 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003750 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003751 dot_prev();
3752 }
3753 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003754 dot_begin();
3755 dot_skip_over_ws();
3756 }
Eric Andersen3f980402001-04-04 17:31:15 +00003757 }
3758 if (c1 != 27) {
3759 // if CHANGING, not deleting, start inserting after the delete
3760 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003761 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003762 goto dc_i; // start inserting
3763 }
3764 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003765 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003766 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003767#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003768 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003769 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003770 }
3771 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003772 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003773 for (cnt = 0; p <= q; p++) {
3774 if (*p == '\n')
3775 cnt++;
3776 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003777 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003778 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003779#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003780 end_cmd_q(); // stop adding to q
3781 }
3782 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003783 }
Eric Andersen3f980402001-04-04 17:31:15 +00003784 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003785 case KEYCODE_UP: // cursor key Up
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003786 do {
3787 dot_prev();
3788 dot = move_to_col(dot, ccol + offset); // try stay in same col
3789 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003790 break;
3791 case 'r': // r- replace the current char with user input
3792 c1 = get_one_char(); // get the replacement char
3793 if (*dot != '\n') {
3794 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003795 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003796 }
3797 end_cmd_q(); // stop adding to q
3798 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003799 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003800 last_forward_char = get_one_char();
3801 do_cmd(';');
3802 if (*dot == last_forward_char)
3803 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003804 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003805 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003806 case 'w': // w- forward a word
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003807 do {
3808 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3809 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3810 } else if (ispunct(*dot)) { // we are on PUNCT
3811 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3812 }
3813 if (dot < end - 1)
3814 dot++; // move over word
3815 if (isspace(*dot)) {
3816 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3817 }
3818 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003819 break;
3820 case 'z': // z-
3821 c1 = get_one_char(); // get the replacement char
3822 cnt = 0;
3823 if (c1 == '.')
3824 cnt = (rows - 2) / 2; // put dot at center
3825 if (c1 == '-')
3826 cnt = rows - 2; // put dot at bottom
3827 screenbegin = begin_line(dot); // start dot at top
3828 dot_scroll(cnt, -1);
3829 break;
3830 case '|': // |- move to column "cmdcnt"
3831 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3832 break;
3833 case '~': // ~- flip the case of letters a-z -> A-Z
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003834 do {
3835 if (islower(*dot)) {
3836 *dot = toupper(*dot);
3837 file_modified++;
3838 } else if (isupper(*dot)) {
3839 *dot = tolower(*dot);
3840 file_modified++;
3841 }
3842 dot_right();
3843 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003844 end_cmd_q(); // stop adding to q
3845 break;
3846 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003847 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003848 dot_begin();
3849 break;
3850 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003851#if 0
3852 case KEYCODE_FUN1: // Function Key F1
3853 case KEYCODE_FUN2: // Function Key F2
3854 case KEYCODE_FUN3: // Function Key F3
3855 case KEYCODE_FUN4: // Function Key F4
3856 case KEYCODE_FUN5: // Function Key F5
3857 case KEYCODE_FUN6: // Function Key F6
3858 case KEYCODE_FUN7: // Function Key F7
3859 case KEYCODE_FUN8: // Function Key F8
3860 case KEYCODE_FUN9: // Function Key F9
3861 case KEYCODE_FUN10: // Function Key F10
3862 case KEYCODE_FUN11: // Function Key F11
3863 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003864 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003865#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003866 }
3867
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003868 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003869 // if text[] just became empty, add back an empty line
3870 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003871 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003872 dot = text;
3873 }
3874 // it is OK for dot to exactly equal to end, otherwise check dot validity
3875 if (dot != end) {
3876 dot = bound_dot(dot); // make sure "dot" is valid
3877 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003878#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003879 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003880#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003881
3882 if (!isdigit(c))
3883 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3884 cnt = dot - begin_line(dot);
3885 // Try to stay off of the Newline
3886 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3887 dot--;
3888}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003889
Paul Fox35e9c5d2008-03-06 16:26:12 +00003890/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003891#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003892static int totalcmds = 0;
3893static int Mp = 85; // Movement command Probability
3894static int Np = 90; // Non-movement command Probability
3895static int Dp = 96; // Delete command Probability
3896static int Ip = 97; // Insert command Probability
3897static int Yp = 98; // Yank command Probability
3898static int Pp = 99; // Put command Probability
3899static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003900static const char chars[20] = "\t012345 abcdABCD-=.$";
3901static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003902 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003903 "broadcast", "the", "emergency", "of",
3904 "system", "quick", "brown", "fox",
3905 "jumped", "over", "lazy", "dogs",
3906 "back", "January", "Febuary", "March"
3907};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003908static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003909 "You should have received a copy of the GNU General Public License\n",
3910 "char c, cm, *cmd, *cmd1;\n",
3911 "generate a command by percentages\n",
3912 "Numbers may be typed as a prefix to some commands.\n",
3913 "Quit, discarding changes!\n",
3914 "Forced write, if permission originally not valid.\n",
3915 "In general, any ex or ed command (such as substitute or delete).\n",
3916 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3917 "Please get w/ me and I will go over it with you.\n",
3918 "The following is a list of scheduled, committed changes.\n",
3919 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3920 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3921 "Any question about transactions please contact Sterling Huxley.\n",
3922 "I will try to get back to you by Friday, December 31.\n",
3923 "This Change will be implemented on Friday.\n",
3924 "Let me know if you have problems accessing this;\n",
3925 "Sterling Huxley recently added you to the access list.\n",
3926 "Would you like to go to lunch?\n",
3927 "The last command will be automatically run.\n",
3928 "This is too much english for a computer geek.\n",
3929};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003930static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003931 "You should have received a copy of the GNU General Public License\n",
3932 "char c, cm, *cmd, *cmd1;\n",
3933 "generate a command by percentages\n",
3934 "Numbers may be typed as a prefix to some commands.\n",
3935 "Quit, discarding changes!\n",
3936 "Forced write, if permission originally not valid.\n",
3937 "In general, any ex or ed command (such as substitute or delete).\n",
3938 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3939 "Please get w/ me and I will go over it with you.\n",
3940 "The following is a list of scheduled, committed changes.\n",
3941 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3942 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3943 "Any question about transactions please contact Sterling Huxley.\n",
3944 "I will try to get back to you by Friday, December 31.\n",
3945 "This Change will be implemented on Friday.\n",
3946 "Let me know if you have problems accessing this;\n",
3947 "Sterling Huxley recently added you to the access list.\n",
3948 "Would you like to go to lunch?\n",
3949 "The last command will be automatically run.\n",
3950 "This is too much english for a computer geek.\n",
3951};
3952
3953// create a random command to execute
3954static void crash_dummy()
3955{
3956 static int sleeptime; // how long to pause between commands
3957 char c, cm, *cmd, *cmd1;
3958 int i, cnt, thing, rbi, startrbi, percent;
3959
3960 // "dot" movement commands
3961 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3962
3963 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02003964 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003965 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003966 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02003967 readbuffer[0] = 'X';
3968 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003969 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003970 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003971 // generate a command by percentages
3972 percent = (int) lrand48() % 100; // get a number from 0-99
3973 if (percent < Mp) { // Movement commands
3974 // available commands
3975 cmd = cmd1;
3976 M++;
3977 } else if (percent < Np) { // non-movement commands
3978 cmd = "mz<>\'\""; // available commands
3979 N++;
3980 } else if (percent < Dp) { // Delete commands
3981 cmd = "dx"; // available commands
3982 D++;
3983 } else if (percent < Ip) { // Inset commands
3984 cmd = "iIaAsrJ"; // available commands
3985 I++;
3986 } else if (percent < Yp) { // Yank commands
3987 cmd = "yY"; // available commands
3988 Y++;
3989 } else if (percent < Pp) { // Put commands
3990 cmd = "pP"; // available commands
3991 P++;
3992 } else {
3993 // We do not know how to handle this command, try again
3994 U++;
3995 goto cd0;
3996 }
3997 // randomly pick one of the available cmds from "cmd[]"
3998 i = (int) lrand48() % strlen(cmd);
3999 cm = cmd[i];
4000 if (strchr(":\024", cm))
4001 goto cd0; // dont allow colon or ctrl-T commands
4002 readbuffer[rbi++] = cm; // put cmd into input buffer
4003
4004 // now we have the command-
4005 // there are 1, 2, and multi char commands
4006 // find out which and generate the rest of command as necessary
4007 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
4008 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
4009 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
4010 cmd1 = "abcdefghijklmnopqrstuvwxyz";
4011 }
4012 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4013 c = cmd1[thing];
4014 readbuffer[rbi++] = c; // add movement to input buffer
4015 }
4016 if (strchr("iIaAsc", cm)) { // multi-char commands
4017 if (cm == 'c') {
4018 // change some thing
4019 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4020 c = cmd1[thing];
4021 readbuffer[rbi++] = c; // add movement to input buffer
4022 }
4023 thing = (int) lrand48() % 4; // what thing to insert
4024 cnt = (int) lrand48() % 10; // how many to insert
4025 for (i = 0; i < cnt; i++) {
4026 if (thing == 0) { // insert chars
4027 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
4028 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004029 strcat(readbuffer, words[(int) lrand48() % 20]);
4030 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004031 sleeptime = 0; // how fast to type
4032 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004033 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004034 sleeptime = 0; // how fast to type
4035 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004036 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004037 sleeptime = 0; // how fast to type
4038 }
4039 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004040 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004041 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02004042 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004043 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004044 totalcmds++;
4045 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004046 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004047}
4048
4049// test to see if there are any errors
4050static void crash_test()
4051{
4052 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004053
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004054 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004055 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004056
4057 msg[0] = '\0';
4058 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004059 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004060 }
4061 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004062 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004063 }
4064 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004065 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004066 }
4067 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004068 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004069 }
4070 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004071 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004072 }
4073 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004074 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004075 }
4076
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004077 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00004078 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02004079 totalcmds, last_input_char, msg, ESC_BOLD_TEXT, ESC_NORM_TEXT);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01004080 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00004081 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004082 if (d[0] == '\n' || d[0] == '\r')
4083 break;
4084 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004085 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004086 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004087 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004088 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004089 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
4090 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
4091 oldtim = tim;
4092 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004093}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00004094#endif