blob: 7173415c888484b992eb343cb4f4b756f6d216db [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
Denys Vlasenko60cb48c2013-01-14 15:57:44 +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 Vlasenko605f2642012-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 Vlasenko605f2642012-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
159# include <regex.h>
160#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 Vlasenko04b52892012-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
284 unsigned 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 Vlasenko04b52892012-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 *, ...);
481static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000482static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000483static void redraw(int); // force a full screen refresh
Denis Vlasenko68404f12008-03-17 09:00:54 +0000484static char* format_line(char* /*, int*/);
Eric Andersen3f980402001-04-04 17:31:15 +0000485static void refresh(int); // update the terminal from screen[]
486
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000487static void Indicate_Error(void); // use flash or beep to indicate error
488#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000489static void Hit_Return(void);
490
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000491#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000492static char *char_search(char *, const char *, int, int); // search for pattern starting at p
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000493#endif
494#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000495static char *get_one_address(char *, int *); // get colon addr, if present
496static char *get_address(char *, int *, int *); // get two colon addrs, if present
497static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000498#endif
499#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000500static void winch_sig(int); // catch window size changes
501static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000502static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000503#endif
504#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000505static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000506static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000507#else
508#define end_cmd_q() ((void)0)
509#endif
510#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000511static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000512#endif
513#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000514// might reallocate text[]! use p += string_insert(p, ...),
515// and be careful to not use pointers into potentially freed text[]!
516static uintptr_t string_insert(char *, const char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000517#endif
518#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000519static char *text_yank(char *, char *, int); // save copy of "p" into a register
520static char what_reg(void); // what is letter of current YDreg
521static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000522#endif
523#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000524static void crash_dummy();
525static void crash_test();
526static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000527#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000528
529
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000530static void write1(const char *out)
531{
532 fputs(out, stdout);
533}
534
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000535int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000536int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000537{
Eric Andersend402edf2001-04-04 19:29:48 +0000538 int c;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000539
540 INIT_G();
Eric Andersen3f980402001-04-04 17:31:15 +0000541
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000542#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000543 my_pid = getpid();
544#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000545#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000546 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000547#endif
Denis Vlasenko2414a962007-07-18 22:03:40 +0000548#ifdef NO_SUCH_APPLET_YET
549 /* If we aren't "vi", we are "view" */
550 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000551 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000552 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000553#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000554
Denys Vlasenko605f2642012-06-11 01:53:33 +0200555 // autoindent is not default in vim 7.3
556 vi_setops = /*VI_AUTOINDENT |*/ VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenkof9234132007-03-21 00:03:42 +0000557 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000558 // 2- process EXINIT variable from environment
559 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000560#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000561 {
562 char *p = getenv("EXINIT");
563 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000564 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000565 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000566#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000567 while ((c = getopt(argc, argv, "hCRH" IF_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000568 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000569#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000570 case 'C':
571 crashme = 1;
572 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000573#endif
574#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000575 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000576 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000577 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000578#endif
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000579#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000580 case 'c': // cmd line vi command
581 if (*optarg)
Denys Vlasenko605f2642012-06-11 01:53:33 +0200582 initial_cmds[initial_cmds[0] != NULL] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000583 break;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000584#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000585 case 'H':
Eric Andersen3f980402001-04-04 17:31:15 +0000586 show_help();
Paul Fox35e9c5d2008-03-06 16:26:12 +0000587 /* fall through */
Paul Fox35e9c5d2008-03-06 16:26:12 +0000588 default:
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000589 bb_show_usage();
Eric Andersendd8500b2001-07-02 18:06:14 +0000590 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000591 }
592 }
593
594 // The argv array can be used by the ":next" and ":rewind" commands
Dennis Groenenc0657e02012-01-31 14:12:38 +0100595 argv += optind;
596 argc -= optind;
Eric Andersen3f980402001-04-04 17:31:15 +0000597
598 //----- This is the main file handling loop --------------
Denys Vlasenko04b52892012-06-11 13:51:38 +0200599 save_argc = argc;
600 optind = 0;
Denys Vlasenkod3dff872012-06-11 13:53:26 +0200601 // "Save cursor, use alternate screen buffer, clear screen"
602 write1("\033[?1049h");
Denys Vlasenko04cecd52010-04-16 22:13:55 -0700603 while (1) {
604 edit_file(argv[optind]); /* param might be NULL */
605 if (++optind >= argc)
606 break;
Eric Andersen3f980402001-04-04 17:31:15 +0000607 }
Denys Vlasenkod3dff872012-06-11 13:53:26 +0200608 // "Use normal screen buffer, restore cursor"
609 write1("\033[?1049l");
Eric Andersen3f980402001-04-04 17:31:15 +0000610 //-----------------------------------------------------------
611
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000612 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000613}
614
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000615/* read text from file or create an empty buf */
616/* will also update current_filename */
617static int init_text_buffer(char *fn)
618{
619 int rc;
620 int size = file_size(fn); // file size. -1 means does not exist.
621
622 /* allocate/reallocate text buffer */
623 free(text);
Denis Vlasenkob1759462008-06-20 20:20:54 +0000624 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000625 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000626
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000627 if (fn != current_filename) {
628 free(current_filename);
629 current_filename = xstrdup(fn);
630 }
631 if (size < 0) {
632 // file dont exist. Start empty buf with dummy line
633 char_insert(text, '\n');
634 rc = 0;
635 } else {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000636 rc = file_insert(fn, text, 1);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000637 }
638 file_modified = 0;
639 last_file_modified = -1;
640#if ENABLE_FEATURE_VI_YANKMARK
641 /* init the marks. */
642 memset(mark, 0, sizeof(mark));
643#endif
644 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000645}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000646
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700647#if ENABLE_FEATURE_VI_WIN_RESIZE
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200648static int query_screen_dimensions(void)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700649{
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200650 int err = get_terminal_width_height(STDIN_FILENO, &columns, &rows);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700651 if (rows > MAX_SCR_ROWS)
652 rows = MAX_SCR_ROWS;
653 if (columns > MAX_SCR_COLS)
654 columns = MAX_SCR_COLS;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200655 return err;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700656}
657#else
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200658# define query_screen_dimensions() (0)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700659#endif
660
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000661static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000662{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000663#if ENABLE_FEATURE_VI_YANKMARK
664#define cur_line edit_file__cur_line
665#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000666 int c;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000667#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000668 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000669#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000670
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000671 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000672 rawmode();
673 rows = 24;
674 columns = 80;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200675 IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions();
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700676#if ENABLE_FEATURE_VI_ASK_TERMINAL
677 if (G.get_rowcol_error /* TODO? && no input on stdin */) {
678 uint64_t k;
679 write1("\033[999;999H" "\033[6n");
680 fflush_all();
681 k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100);
682 if ((int32_t)k == KEYCODE_CURSOR_POS) {
683 uint32_t rc = (k >> 32);
684 columns = (rc & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200685 if (columns > MAX_SCR_COLS)
686 columns = MAX_SCR_COLS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700687 rows = ((rc >> 16) & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200688 if (rows > MAX_SCR_ROWS)
689 rows = MAX_SCR_ROWS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700690 }
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700691 }
692#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000693 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000694 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000695
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000696#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000697 YDreg = 26; // default Yank/Delete reg
698 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000699 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000700#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000701
Eric Andersen3f980402001-04-04 17:31:15 +0000702 last_forward_char = last_input_char = '\0';
703 crow = 0;
704 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000705
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000706#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700707 signal(SIGINT, catch_sig);
Eric Andersen3f980402001-04-04 17:31:15 +0000708 signal(SIGWINCH, winch_sig);
709 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000710 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000711 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000712 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000713 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000714#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000715
Eric Andersen3f980402001-04-04 17:31:15 +0000716 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
717 cmdcnt = 0;
718 tabstop = 8;
719 offset = 0; // no horizontal offset
720 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000721#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000722 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000723 ioq = ioq_start = NULL;
724 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000725 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000726#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000727
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000728#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000729 {
730 char *p, *q;
731 int n = 0;
732
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700733 while ((p = initial_cmds[n]) != NULL) {
Denis Vlasenkof9234132007-03-21 00:03:42 +0000734 do {
735 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000736 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000737 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000738 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000739 *p++ = '\0';
740 if (*q)
741 colon(q);
742 } while (p);
743 free(initial_cmds[n]);
744 initial_cmds[n] = NULL;
745 n++;
746 }
747 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000748#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000749 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000750 //------This is the main Vi cmd handling loop -----------------------
751 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000752#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000753 if (crashme > 0) {
754 if ((end - text) > 1) {
755 crash_dummy(); // generate a random command
756 } else {
757 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000758 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
759 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000760 refresh(FALSE);
761 }
762 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000763#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000764 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000765#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000766 // save a copy of the current line- for the 'U" command
767 if (begin_line(dot) != cur_line) {
768 cur_line = begin_line(dot);
769 text_yank(begin_line(dot), end_line(dot), Ureg);
770 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000771#endif
772#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000773 // These are commands that change text[].
774 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000775 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000776 && cmd_mode == 0 // command mode
777 && c > '\0' // exclude NUL and non-ASCII chars
778 && c < 0x7f // (Unicode and such)
779 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000780 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000781 start_new_cmd_q(c);
782 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000783#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000784 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000785
Eric Andersen3f980402001-04-04 17:31:15 +0000786 // poll to see if there is input already waiting. if we are
787 // not able to display output fast enough to keep up, skip
788 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200789 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000790 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000791 refresh(FALSE);
792 show_status_line();
793 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000794#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000795 if (crashme > 0)
796 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000797#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000798 }
799 //-------------------------------------------------------------------
800
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000801 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000802 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000803#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000804}
805
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000806//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000807#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000808static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000809{
810 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000811 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000812 IF_FEATURE_VI_YANKMARK(char c;)
813 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000814
815 *addr = -1; // assume no addr
816 if (*p == '.') { // the current line
817 p++;
818 q = begin_line(dot);
819 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000820 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000821#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000822 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000823 p++;
824 c = tolower(*p);
825 p++;
826 if (c >= 'a' && c <= 'z') {
827 // we have a mark
828 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000829 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000830 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000831 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000832 }
833 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000834 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000835#endif
836#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000837 else if (*p == '/') { // a search pattern
838 q = strchrnul(++p, '/');
839 pat = xstrndup(p, q - p); // save copy of pattern
840 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000841 if (*p == '/')
842 p++;
843 q = char_search(dot, pat, FORWARD, FULL);
844 if (q != NULL) {
845 *addr = count_lines(text, q);
846 }
847 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000848 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000849#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000850 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000851 p++;
852 q = begin_line(end - 1);
853 *addr = count_lines(text, q);
854 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000855 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000856 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000857 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200858 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000859 *addr = -1;
860 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000861 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000862}
863
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000864static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000865{
866 //----- get the address' i.e., 1,3 'a,'b -----
867 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000868 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000869 p++; // skip over leading spaces
870 if (*p == '%') { // alias for 1,$
871 p++;
872 *b = 1;
873 *e = count_lines(text, end-1);
874 goto ga0;
875 }
876 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000877 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000878 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000879 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000880 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000881 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000882 p++;
883 // get SECOND addr, if present
884 p = get_one_address(p, e);
885 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000886 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000887 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000888 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000889 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000890}
891
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000892#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000893static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000894 const char *short_opname, int opt)
895{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000896 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000897 int l = strlen(opname) - 1; /* opname have + ' ' */
898
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200899 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000900 if (strncasecmp(a, opname, l) == 0
901 || strncasecmp(a, short_opname, 2) == 0
902 ) {
903 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000904 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000905 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000906 vi_setops |= opt;
907 }
908}
909#endif
910
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000911// buf must be no longer than MAX_INPUT_LEN!
912static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000913{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000914 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000915 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000916 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000917 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000918
919 // :3154 // if (-e line 3154) goto it else stay put
920 // :4,33w! foo // write a portion of buffer to file "foo"
921 // :w // write all of buffer to current file
922 // :q // quit
923 // :q! // quit- dont care about modified file
924 // :'a,'z!sort -u // filter block through sort
925 // :'f // goto mark "f"
926 // :'fl // list literal the mark "f" line
927 // :.r bar // read file "bar" into buffer before dot
928 // :/123/,/abc/d // delete lines from "123" line to "abc" line
929 // :/xyz/ // goto the "xyz" line
930 // :s/find/replace/ // substitute pattern "find" with "replace"
931 // :!<cmd> // run <cmd> then return
932 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000933
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000934 if (!buf[0])
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200935 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000936 if (*buf == ':')
937 buf++; // move past the ':'
938
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000939 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000940 b = e = -1;
941 q = text; // assume 1,$ for the range
942 r = end - 1;
943 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000944 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000945
946 // look for optional address(es) :. :1 :1,9 :'q,'a :%
947 buf = get_address(buf, &b, &e);
948
949 // remember orig command line
950 orig_buf = buf;
951
952 // get the COMMAND into cmd[]
953 buf1 = cmd;
954 while (*buf != '\0') {
955 if (isspace(*buf))
956 break;
957 *buf1++ = *buf++;
958 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000959 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000960 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000961 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000962 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000963 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000964 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000965 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000966 if (buf1) {
967 useforce = TRUE;
968 *buf1 = '\0'; // get rid of !
969 }
970 if (b >= 0) {
971 // if there is only one addr, then the addr
972 // is the line number of the single line the
973 // user wants. So, reset the end
974 // pointer to point at end of the "b" line
975 q = find_line(b); // what line is #b
976 r = end_line(q);
977 li = 1;
978 }
979 if (e >= 0) {
980 // we were given two addrs. change the
981 // end pointer to the addr given by user.
982 r = find_line(e); // what line is #e
983 r = end_line(r);
984 li = e - b + 1;
985 }
986 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000987 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000988 if (i == 0) { // :123CR goto line #123
989 if (b >= 0) {
990 dot = find_line(b); // what line is #b
991 dot_skip_over_ws();
992 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000993 }
994#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200995 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000996 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000997 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000998 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000999 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001000 retcode = system(orig_buf + 1); // run the cmd
1001 if (retcode)
1002 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001003 rawmode();
1004 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +00001005 }
1006#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001007 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001008 if (b < 0) { // no addr given- use defaults
1009 b = e = count_lines(text, dot);
1010 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001011 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001012 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001013 if (b < 0) { // no addr given- use defaults
1014 q = begin_line(dot); // assume .,. for the range
1015 r = end_line(dot);
1016 }
1017 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
1018 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001019 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001020 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001021 if (file_modified && !useforce) {
Dennis Groenenc0657e02012-01-31 14:12:38 +01001022 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001023 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001024 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001025 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001026 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001027 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001028 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001029 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001030 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001031 } else {
1032 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001033 status_line_bold("No current filename");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001034 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001035 }
1036
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001037 if (init_text_buffer(fn) < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001038 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001039
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001040#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001041 if (Ureg >= 0 && Ureg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001042 free(reg[Ureg]); // free orig line reg- for 'U'
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001043 reg[Ureg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001044 }
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001045 if (YDreg >= 0 && YDreg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001046 free(reg[YDreg]); // free default yank/delete register
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001047 reg[YDreg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001048 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001049#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001050 // how many lines in text[]?
1051 li = count_lines(text, end - 1);
Denys Vlasenko778794d2013-01-22 10:13:52 +01001052 status_line("'%s'%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001053 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001054 " %dL, %dC", current_filename,
1055 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001056 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001057 ((readonly_mode) ? " [Readonly]" : ""),
1058 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001059 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001060 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001061 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01001062 status_line_bold("No address allowed on this command");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001063 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001064 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001065 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001066 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001067 free(current_filename);
1068 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001069 } else {
1070 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +00001071 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001072 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001073 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001074 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001075 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001076 cookmode();
1077 show_help();
1078 rawmode();
1079 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001080 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001081 if (b < 0) { // no addr given- use defaults
1082 q = begin_line(dot); // assume .,. for the range
1083 r = end_line(dot);
1084 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001085 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001086 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001087 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001088 int c_is_no_print;
1089
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001090 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +00001091 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001092 if (c_is_no_print) {
1093 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001094 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +00001095 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001096 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001097 write1("$\r");
1098 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001099 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001100 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001101 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001102 else
1103 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001104 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001105 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001106 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001107 standout_end();
1108 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001109 Hit_Return();
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001110 } else if (strncmp(cmd, "quit", i) == 0 // quit
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001111 || strncmp(cmd, "next", i) == 0 // edit next file
Dennis Groenenc0657e02012-01-31 14:12:38 +01001112 || strncmp(cmd, "prev", i) == 0 // edit previous file
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001113 ) {
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001114 int n;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001115 if (useforce) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001116 if (*cmd == 'q') {
Dennis Groenenc0657e02012-01-31 14:12:38 +01001117 // force end of argv list
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001118 optind = save_argc;
1119 }
1120 editing = 0;
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001121 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001122 }
1123 // don't exit if the file been modified
1124 if (file_modified) {
Dennis Groenenc0657e02012-01-31 14:12:38 +01001125 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001126 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001127 }
1128 // are there other file to edit
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001129 n = save_argc - optind - 1;
1130 if (*cmd == 'q' && n > 0) {
1131 status_line_bold("%d more file(s) to edit", n);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001132 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001133 }
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001134 if (*cmd == 'n' && n <= 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001135 status_line_bold("No more files to edit");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001136 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001137 }
Dennis Groenenc0657e02012-01-31 14:12:38 +01001138 if (*cmd == 'p') {
1139 // are there previous files to edit
1140 if (optind < 1) {
1141 status_line_bold("No previous files to edit");
1142 goto ret;
1143 }
1144 optind -= 2;
1145 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001146 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001147 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001148 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001149 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001150 status_line_bold("No filename given");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001151 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001152 }
1153 if (b < 0) { // no addr given- use defaults
1154 q = begin_line(dot); // assume "dot"
1155 }
1156 // read after current line- unless user said ":0r foo"
1157 if (b != 0)
1158 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001159 { // dance around potentially-reallocated text[]
1160 uintptr_t ofs = q - text;
1161 ch = file_insert(fn, q, 0);
1162 q = text + ofs;
1163 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001164 if (ch < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001165 goto ret; // nothing was inserted
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001166 // how many lines in text[]?
1167 li = count_lines(q, q + ch - 1);
Denys Vlasenko778794d2013-01-22 10:13:52 +01001168 status_line("'%s'"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001169 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001170 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001171 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001172 li, ch);
1173 if (ch > 0) {
1174 // if the insert is before "dot" then we need to update
1175 if (q <= dot)
1176 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001177 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001178 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001179 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001180 if (file_modified && !useforce) {
Dennis Groenenc0657e02012-01-31 14:12:38 +01001181 status_line_bold("No write since last change (:%s! overrides)", cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001182 } else {
1183 // reset the filenames to edit
Dennis Groenenc0657e02012-01-31 14:12:38 +01001184 optind = -1; /* start from 0th file */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001185 editing = 0;
1186 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001187#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001188 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001189#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001190 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001191#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001192 i = 0; // offset into args
Denys Vlasenko605f2642012-06-11 01:53:33 +02001193 // only blank is regarded as args delimiter. What about tab '\t'?
Denis Vlasenkof9234132007-03-21 00:03:42 +00001194 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001195 // print out values of all options
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001196#if ENABLE_FEATURE_VI_SETOPTS
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001197 status_line_bold(
1198 "%sautoindent "
1199 "%sflash "
1200 "%signorecase "
1201 "%sshowmatch "
1202 "tabstop=%u",
1203 autoindent ? "" : "no",
1204 err_method ? "" : "no",
1205 ignorecase ? "" : "no",
1206 showmatch ? "" : "no",
1207 tabstop
1208 );
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001209#endif
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001210 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001211 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001212#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001213 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001214 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001215 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001216 i = 2; // ":set noautoindent"
1217 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001218 setops(argp, "flash " , i, "fl", VI_ERR_METHOD);
Denis Vlasenkof9234132007-03-21 00:03:42 +00001219 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001220 setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH );
1221 if (strncmp(argp + i, "tabstop=", 8) == 0) {
1222 int t = 0;
1223 sscanf(argp + i+8, "%u", &t);
1224 if (t > 0 && t <= MAX_TABSTOP)
1225 tabstop = t;
Denis Vlasenkof9234132007-03-21 00:03:42 +00001226 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001227 argp = skip_non_whitespace(argp);
1228 argp = skip_whitespace(argp);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001229 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001230#endif /* FEATURE_VI_SETOPTS */
1231#endif /* FEATURE_VI_SET */
1232#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001233 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001234 char *F, *R, *flags;
1235 size_t len_F, len_R;
1236 int gflag; // global replace flag
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001237
1238 // F points to the "find" pattern
1239 // R points to the "replace" pattern
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001240 // replace the cmd line delimiters "/" with NULs
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001241 c = orig_buf[1]; // what is the delimiter
1242 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001243 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001244 if (!R)
1245 goto colon_s_fail;
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001246 len_F = R - F;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001247 *R++ = '\0'; // terminate "find"
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001248 flags = strchr(R, c);
1249 if (!flags)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001250 goto colon_s_fail;
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001251 len_R = flags - R;
1252 *flags++ = '\0'; // terminate "replace"
1253 gflag = *flags;
1254
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001255 q = begin_line(q);
1256 if (b < 0) { // maybe :s/foo/bar/
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001257 q = begin_line(dot); // start with cur line
1258 b = count_lines(text, q); // cur line number
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001259 }
1260 if (e < 0)
1261 e = b; // maybe :.s/foo/bar/
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001262
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001263 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001264 char *ls = q; // orig line start
1265 char *found;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001266 vc4:
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001267 found = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
1268 if (found) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001269 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001270 // we found the "find" pattern - delete it
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001271 text_hole_delete(found, found + len_F - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001272 // inset the "replace" patern
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001273 bias = string_insert(found, R); // insert the string
1274 found += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001275 ls += bias;
1276 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001277 // check for "global" :s/foo/bar/g
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001278 if (gflag == 'g') {
1279 if ((found + len_R) < end_line(ls)) {
1280 q = found + len_R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001281 goto vc4; // don't let q move past cur line
1282 }
1283 }
1284 }
1285 q = next_line(ls);
1286 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001287#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001288 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001289 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001290 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1291 || strncmp(cmd, "wq", i) == 0
1292 || strncmp(cmd, "wn", i) == 0
1293 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001294 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001295 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001296 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001297 fn = args;
1298 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001299#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001300 if (readonly_mode && !useforce) {
Denys Vlasenko778794d2013-01-22 10:13:52 +01001301 status_line_bold("'%s' is read only", fn);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001302 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001303 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001304#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001305 // how many lines in text[]?
1306 li = count_lines(q, r);
1307 ch = r - q + 1;
1308 // see if file exists- if not, its just a new file request
1309 if (useforce) {
1310 // if "fn" is not write-able, chmod u+w
1311 // sprintf(syscmd, "chmod u+w %s", fn);
1312 // system(syscmd);
1313 forced = TRUE;
1314 }
1315 l = file_write(fn, q, r);
1316 if (useforce && forced) {
1317 // chmod u-w
1318 // sprintf(syscmd, "chmod u-w %s", fn);
1319 // system(syscmd);
1320 forced = FALSE;
1321 }
Paul Fox61e45db2005-10-09 14:43:22 +00001322 if (l < 0) {
1323 if (l == -1)
Denys Vlasenko778794d2013-01-22 10:13:52 +01001324 status_line_bold("'%s' %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001325 } else {
Denys Vlasenko778794d2013-01-22 10:13:52 +01001326 status_line("'%s' %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001327 if (q == text && r == end - 1 && l == ch) {
1328 file_modified = 0;
1329 last_file_modified = -1;
1330 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001331 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1332 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1333 )
1334 && l == ch
1335 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001336 editing = 0;
1337 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001338 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001339#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001340 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001341 if (b < 0) { // no addr given- use defaults
1342 q = begin_line(dot); // assume .,. for the range
1343 r = end_line(dot);
1344 }
1345 text_yank(q, r, YDreg);
1346 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001347 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001348 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001349#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001350 } else {
1351 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001352 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001353 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001354 ret:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001355 dot = bound_dot(dot); // make sure "dot" is valid
1356 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001357#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001358 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001359 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001360#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001361}
Paul Fox61e45db2005-10-09 14:43:22 +00001362
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001363#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001364
1365static void Hit_Return(void)
1366{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001367 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001368
Denis Vlasenkof882f082007-12-23 02:36:51 +00001369 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001370 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001371 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001372 while ((c = get_one_char()) != '\n' && c != '\r')
1373 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001374 redraw(TRUE); // force redraw all
1375}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001376
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001377static int next_tabstop(int col)
1378{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001379 return col + ((tabstop - 1) - (col % tabstop));
1380}
1381
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001382//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001383static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001384{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001385 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001386 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001387 int cnt, ro, co;
1388
1389 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001390
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001391 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001392 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001393 // how many lines do we have to move
1394 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001395 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001396 screenbegin = beg_cur;
1397 if (cnt > (rows - 1) / 2) {
1398 // we moved too many lines. put "dot" in middle of screen
1399 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1400 screenbegin = prev_line(screenbegin);
1401 }
1402 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001403 } else {
1404 char *end_scr; // begin and end of screen
1405 end_scr = end_screen(); // last char of screen
1406 if (beg_cur > end_scr) {
1407 // "d" is after bottom line on screen
1408 // how many lines do we have to move
1409 cnt = count_lines(end_scr, beg_cur);
1410 if (cnt > (rows - 1) / 2)
1411 goto sc1; // too many lines
1412 for (ro = 0; ro < cnt - 1; ro++) {
1413 // move screen begin the same amount
1414 screenbegin = next_line(screenbegin);
1415 // now, move the end of screen
1416 end_scr = next_line(end_scr);
1417 end_scr = end_line(end_scr);
1418 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001419 }
1420 }
1421 // "d" is on screen- find out which row
1422 tp = screenbegin;
1423 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1424 if (tp == beg_cur)
1425 break;
1426 tp = next_line(tp);
1427 }
1428
1429 // find out what col "d" is on
1430 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001431 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001432 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001433 break;
1434 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001435 // handle tabs like real vi
1436 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001437 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001438 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001439 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001440 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1441 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001442 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001443 co++;
1444 tp++;
1445 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001446
1447 // "co" is the column where "dot" is.
1448 // The screen has "columns" columns.
1449 // The currently displayed columns are 0+offset -- columns+ofset
1450 // |-------------------------------------------------------------|
1451 // ^ ^ ^
1452 // offset | |------- columns ----------------|
1453 //
1454 // If "co" is already in this range then we do not have to adjust offset
1455 // but, we do have to subtract the "offset" bias from "co".
1456 // If "co" is outside this range then we have to change "offset".
1457 // If the first char of a line is a tab the cursor will try to stay
1458 // in column 7, but we have to set offset to 0.
1459
1460 if (co < 0 + offset) {
1461 offset = co;
1462 }
1463 if (co >= columns + offset) {
1464 offset = co - columns + 1;
1465 }
1466 // if the first char of the line is a tab, and "dot" is sitting on it
1467 // force offset to 0.
1468 if (d == beg_cur && *d == '\t') {
1469 offset = 0;
1470 }
1471 co -= offset;
1472
1473 *row = ro;
1474 *col = co;
1475}
1476
1477//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001478static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001479{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001480 if (p > text) {
1481 p = memrchr(text, '\n', p - text);
1482 if (!p)
1483 return text;
1484 return p + 1;
1485 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001486 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001487}
1488
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001489static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001490{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001491 if (p < end - 1) {
1492 p = memchr(p, '\n', end - p - 1);
1493 if (!p)
1494 return end - 1;
1495 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001496 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001497}
1498
Denis Vlasenkof882f082007-12-23 02:36:51 +00001499static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001500{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001501 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001502 // Try to stay off of the Newline
1503 if (*p == '\n' && (p - begin_line(p)) > 0)
1504 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001505 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001506}
1507
Denis Vlasenkof882f082007-12-23 02:36:51 +00001508static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001509{
1510 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001511 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001512 p--; // step to prev line
1513 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001514 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001515}
1516
Denis Vlasenkof882f082007-12-23 02:36:51 +00001517static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001518{
1519 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001520 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001521 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001522 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001523}
1524
1525//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001526static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001527{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001528 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001529 int cnt;
1530
1531 // find new bottom line
1532 q = screenbegin;
1533 for (cnt = 0; cnt < rows - 2; cnt++)
1534 q = next_line(q);
1535 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001536 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001537}
1538
Denis Vlasenkof882f082007-12-23 02:36:51 +00001539// count line from start to stop
1540static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001541{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001542 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001543 int cnt;
1544
Denis Vlasenkof882f082007-12-23 02:36:51 +00001545 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001546 q = start;
1547 start = stop;
1548 stop = q;
1549 }
1550 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001551 stop = end_line(stop);
1552 while (start <= stop && start <= end - 1) {
1553 start = end_line(start);
1554 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001555 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001556 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001557 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001558 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001559}
1560
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001561static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001562{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001563 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001564
1565 for (q = text; li > 1; li--) {
1566 q = next_line(q);
1567 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001568 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001569}
1570
1571//----- Dot Movement Routines ----------------------------------
1572static void dot_left(void)
1573{
1574 if (dot > text && dot[-1] != '\n')
1575 dot--;
1576}
1577
1578static void dot_right(void)
1579{
1580 if (dot < end - 1 && *dot != '\n')
1581 dot++;
1582}
1583
1584static void dot_begin(void)
1585{
1586 dot = begin_line(dot); // return pointer to first char cur line
1587}
1588
1589static void dot_end(void)
1590{
1591 dot = end_line(dot); // return pointer to last char cur line
1592}
1593
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001594static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001595{
1596 int co;
1597
1598 p = begin_line(p);
1599 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001600 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001601 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001602 break;
1603 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001604 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001605 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001606 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001607 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001608 co++;
1609 p++;
1610 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001611 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001612}
1613
1614static void dot_next(void)
1615{
1616 dot = next_line(dot);
1617}
1618
1619static void dot_prev(void)
1620{
1621 dot = prev_line(dot);
1622}
1623
1624static void dot_scroll(int cnt, int dir)
1625{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001626 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001627
1628 for (; cnt > 0; cnt--) {
1629 if (dir < 0) {
1630 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001631 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001632 screenbegin = prev_line(screenbegin);
1633 } else {
1634 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001635 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001636 screenbegin = next_line(screenbegin);
1637 }
1638 }
1639 // make sure "dot" stays on the screen so we dont scroll off
1640 if (dot < screenbegin)
1641 dot = screenbegin;
1642 q = end_screen(); // find new bottom line
1643 if (dot > q)
1644 dot = begin_line(q); // is dot is below bottom line?
1645 dot_skip_over_ws();
1646}
1647
1648static void dot_skip_over_ws(void)
1649{
1650 // skip WS
1651 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1652 dot++;
1653}
1654
1655static void dot_delete(void) // delete the char at 'dot'
1656{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001657 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001658}
1659
Denis Vlasenkof882f082007-12-23 02:36:51 +00001660static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001661{
1662 if (p >= end && end > text) {
1663 p = end - 1;
1664 indicate_error('1');
1665 }
1666 if (p < text) {
1667 p = text;
1668 indicate_error('2');
1669 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001670 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001671}
1672
1673//----- Helper Utility Routines --------------------------------
1674
1675//----------------------------------------------------------------
1676//----- Char Routines --------------------------------------------
1677/* Chars that are part of a word-
1678 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1679 * Chars that are Not part of a word (stoppers)
1680 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1681 * Chars that are WhiteSpace
1682 * TAB NEWLINE VT FF RETURN SPACE
1683 * DO NOT COUNT NEWLINE AS WHITESPACE
1684 */
1685
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001686static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001687{
1688 int li;
1689
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001690 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001691 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001692 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001693 // initialize the new screen. assume this will be a empty file.
1694 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001695 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001696 for (li = 1; li < ro - 1; li++) {
1697 screen[(li * co) + 0] = '~';
1698 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001699 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001700}
1701
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001702#if ENABLE_FEATURE_VI_SEARCH
Walter Harmsb9ba5802011-06-27 02:59:37 +02001703
1704# if ENABLE_FEATURE_VI_REGEX_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001705
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001706// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001707static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001708{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001709 char *q;
1710 struct re_pattern_buffer preg;
1711 int i;
Walter Harmsb9ba5802011-06-27 02:59:37 +02001712 int size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001713
1714 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1715 preg.translate = 0;
1716 preg.fastmap = 0;
1717 preg.buffer = 0;
1718 preg.allocated = 0;
1719
1720 // assume a LIMITED forward search
1721 q = next_line(p);
1722 q = end_line(q);
1723 q = end - 1;
1724 if (dir == BACK) {
1725 q = prev_line(p);
1726 q = text;
1727 }
1728 // count the number of chars to search over, forward or backward
1729 size = q - p;
1730 if (size < 0)
1731 size = p - q;
1732 // RANGE could be negative if we are searching backwards
1733 range = q - p;
1734
Walter Harmsb9ba5802011-06-27 02:59:37 +02001735 q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001736 if (q != 0) {
1737 // The pattern was not compiled
Denys Vlasenko778794d2013-01-22 10:13:52 +01001738 status_line_bold("bad search pattern: '%s': %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001739 i = 0; // return p if pattern not compiled
1740 goto cs1;
1741 }
1742
1743 q = p;
1744 if (range < 0) {
1745 q = p - size;
1746 if (q < text)
1747 q = text;
1748 }
1749 // search for the compiled pattern, preg, in p[]
1750 // range < 0- search backward
1751 // range > 0- search forward
1752 // 0 < start < size
1753 // re_search() < 0 not found or error
1754 // re_search() > 0 index of found pattern
1755 // struct pattern char int int int struct reg
1756 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1757 i = re_search(&preg, q, size, 0, range, 0);
1758 if (i == -1) {
1759 p = 0;
1760 i = 0; // return NULL if pattern not found
1761 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001762 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001763 if (dir == FORWARD) {
1764 p = p + i;
1765 } else {
1766 p = p - i;
1767 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001768 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001769}
Walter Harmsb9ba5802011-06-27 02:59:37 +02001770
1771# else
1772
1773# if ENABLE_FEATURE_VI_SETOPTS
1774static int mycmp(const char *s1, const char *s2, int len)
1775{
1776 if (ignorecase) {
1777 return strncasecmp(s1, s2, len);
1778 }
1779 return strncmp(s1, s2, len);
1780}
1781# else
1782# define mycmp strncmp
1783# endif
1784
1785static char *char_search(char *p, const char *pat, int dir, int range)
1786{
1787 char *start, *stop;
1788 int len;
1789
1790 len = strlen(pat);
1791 if (dir == FORWARD) {
1792 stop = end - 1; // assume range is p - end-1
1793 if (range == LIMITED)
1794 stop = next_line(p); // range is to next line
1795 for (start = p; start < stop; start++) {
1796 if (mycmp(start, pat, len) == 0) {
1797 return start;
1798 }
1799 }
1800 } else if (dir == BACK) {
1801 stop = text; // assume range is text - p
1802 if (range == LIMITED)
1803 stop = prev_line(p); // range is to prev line
1804 for (start = p - len; start >= stop; start--) {
1805 if (mycmp(start, pat, len) == 0) {
1806 return start;
1807 }
1808 }
1809 }
1810 // pattern not found
1811 return NULL;
1812}
1813
1814# endif
1815
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001816#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001817
Denis Vlasenko33875382008-06-21 20:31:50 +00001818static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001819{
1820 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001821 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001822 refresh(FALSE); // show the ^
1823 c = get_one_char();
1824 *p = c;
1825 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001826 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001827 } else if (c == 27) { // Is this an ESC?
1828 cmd_mode = 0;
1829 cmdcnt = 0;
1830 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001831 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001832 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001833 p--;
1834 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001835 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001836 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001837 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001838 p--;
1839 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001840 }
1841 } else {
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001842#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001843 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001844 char *sp; // "save p"
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001845#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001846
1847 if (c == 13)
1848 c = '\n'; // translate \r to \n
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001849#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001850 sp = p; // remember addr of insert
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001851#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001852 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001853#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001854 if (showmatch && strchr(")]}", *sp) != NULL) {
1855 showmatching(sp);
1856 }
1857 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001858 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001859 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001860 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001861 len = strspn(q, " \t"); // space or tab
1862 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001863 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001864 bias = text_hole_make(p, len);
1865 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001866 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001867 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001868 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001869 }
1870 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001871#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001872 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001873 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001874}
1875
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001876// might reallocate text[]! use p += stupid_insert(p, ...),
1877// and be careful to not use pointers into potentially freed text[]!
1878static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001879{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001880 uintptr_t bias;
1881 bias = text_hole_make(p, 1);
1882 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001883 *p = c;
1884 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001885 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001886}
1887
Denis Vlasenko33875382008-06-21 20:31:50 +00001888static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001889{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001890 char *save_dot, *p, *q, *t;
1891 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001892
1893 save_dot = dot;
1894 p = q = dot;
1895
1896 if (strchr("cdy><", c)) {
1897 // these cmds operate on whole lines
1898 p = q = begin_line(p);
1899 for (cnt = 1; cnt < cmdcnt; cnt++) {
1900 q = next_line(q);
1901 }
1902 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001903 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001904 // These cmds operate on char positions
1905 do_cmd(c); // execute movement cmd
1906 q = dot;
1907 } else if (strchr("wW", c)) {
1908 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001909 // if we are at the next word's first char
1910 // step back one char
1911 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001912 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001913 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1914 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1915 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001916 if (dot > text && *dot == '\n')
1917 dot--; // stay off NL
1918 q = dot;
1919 } else if (strchr("H-k{", c)) {
1920 // these operate on multi-lines backwards
1921 q = end_line(dot); // find NL
1922 do_cmd(c); // execute movement cmd
1923 dot_begin();
1924 p = dot;
1925 } else if (strchr("L+j}\r\n", c)) {
1926 // these operate on multi-lines forwards
1927 p = begin_line(dot);
1928 do_cmd(c); // execute movement cmd
1929 dot_end(); // find NL
1930 q = dot;
1931 } else {
Denys Vlasenko6830ade2013-01-15 13:58:01 +01001932 // nothing -- this causes any other values of c to
1933 // represent the one-character range under the
1934 // cursor. this is correct for ' ' and 'l', but
1935 // perhaps no others.
1936 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001937 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001938 if (q < p) {
1939 t = q;
1940 q = p;
1941 p = t;
1942 }
1943
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001944 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001945 if (q > p && strchr("^0bBh\b\177", c)) q--;
1946
1947 multiline = 0;
1948 for (t = p; t <= q; t++) {
1949 if (*t == '\n') {
1950 multiline = 1;
1951 break;
1952 }
1953 }
1954
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001955 *start = p;
1956 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001957 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001958 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001959}
1960
Denis Vlasenko33875382008-06-21 20:31:50 +00001961static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001962{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001963 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001964 int test, inc;
1965
1966 inc = dir;
1967 c = c0 = p[0];
1968 ci = p[inc];
1969 test = 0;
1970
1971 if (type == S_BEFORE_WS) {
1972 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001973 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001974 }
1975 if (type == S_TO_WS) {
1976 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001977 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001978 }
1979 if (type == S_OVER_WS) {
1980 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001981 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001982 }
1983 if (type == S_END_PUNCT) {
1984 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001985 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001986 }
1987 if (type == S_END_ALNUM) {
1988 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001989 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001990 }
1991 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001992 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001993}
1994
Denis Vlasenko33875382008-06-21 20:31:50 +00001995static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001996{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001997 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001998
1999 while (st_test(p, type, dir, &c)) {
2000 // make sure we limit search to correct number of lines
2001 if (c == '\n' && --linecnt < 1)
2002 break;
2003 if (dir >= 0 && p >= end - 1)
2004 break;
2005 if (dir < 0 && p <= text)
2006 break;
2007 p += dir; // move to next char
2008 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002009 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002010}
2011
2012// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00002013static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002014{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002015 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002016 int dir, level;
2017
2018 match = ')';
2019 level = 1;
2020 dir = 1; // assume forward
2021 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002022 case '(': match = ')'; break;
2023 case '[': match = ']'; break;
2024 case '{': match = '}'; break;
2025 case ')': match = '('; dir = -1; break;
2026 case ']': match = '['; dir = -1; break;
2027 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002028 }
2029 for (q = p + dir; text <= q && q < end; q += dir) {
2030 // look for match, count levels of pairs (( ))
2031 if (*q == c)
2032 level++; // increase pair levels
2033 if (*q == match)
2034 level--; // reduce pair level
2035 if (level == 0)
2036 break; // found matching pair
2037 }
2038 if (level != 0)
2039 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002040 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002041}
2042
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002043#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002044// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00002045static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002046{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002047 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002048
2049 // we found half of a pair
2050 q = find_pair(p, *p); // get loc of matching char
2051 if (q == NULL) {
2052 indicate_error('3'); // no matching char
2053 } else {
2054 // "q" now points to matching pair
2055 save_dot = dot; // remember where we are
2056 dot = q; // go to new loc
2057 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002058 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002059 dot = save_dot; // go back to old loc
2060 refresh(FALSE);
2061 }
2062}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002063#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002064
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002065// open a hole in text[]
2066// might reallocate text[]! use p += text_hole_make(p, ...),
2067// and be careful to not use pointers into potentially freed text[]!
2068static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002069{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002070 uintptr_t bias = 0;
2071
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002072 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002073 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002074 end += size; // adjust the new END
2075 if (end >= (text + text_size)) {
2076 char *new_text;
2077 text_size += end - (text + text_size) + 10240;
2078 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002079 bias = (new_text - text);
2080 screenbegin += bias;
2081 dot += bias;
2082 end += bias;
2083 p += bias;
Denys Vlasenko800a9a02012-01-31 14:10:26 +01002084#if ENABLE_FEATURE_VI_YANKMARK
2085 {
2086 int i;
2087 for (i = 0; i < ARRAY_SIZE(mark); i++)
2088 if (mark[i])
2089 mark[i] += bias;
2090 }
2091#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +00002092 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002093 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00002094 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002095 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00002096 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002097 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002098}
2099
2100// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00002101static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002102{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002103 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002104 int cnt, hole_size;
2105
2106 // move forwards, from beginning
2107 // assume p <= q
2108 src = q + 1;
2109 dest = p;
2110 if (q < p) { // they are backward- swap them
2111 src = p + 1;
2112 dest = q;
2113 }
2114 hole_size = q - p + 1;
2115 cnt = end - src;
2116 if (src < text || src > end)
2117 goto thd0;
2118 if (dest < text || dest >= end)
2119 goto thd0;
2120 if (src >= end)
2121 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00002122 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002123 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002124 end = end - hole_size; // adjust the new END
2125 if (dest >= end)
2126 dest = end - 1; // make sure dest in below end-1
2127 if (end <= text)
2128 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00002129 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002130 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002131 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002132}
2133
2134// copy text into register, then delete text.
2135// if dist <= 0, do not include, or go past, a NewLine
2136//
Denis Vlasenko33875382008-06-21 20:31:50 +00002137static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002138{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002139 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002140
2141 // make sure start <= stop
2142 if (start > stop) {
2143 // they are backwards, reverse them
2144 p = start;
2145 start = stop;
2146 stop = p;
2147 }
2148 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00002149 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002150 p = start;
2151 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002152 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002153 // dont go past a NewLine
2154 for (; p + 1 <= stop; p++) {
2155 if (p[1] == '\n') {
2156 stop = p; // "stop" just before NewLine
2157 break;
2158 }
2159 }
2160 }
2161 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002162#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002163 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002164#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002165 if (yf == YANKDEL) {
2166 p = text_hole_delete(start, stop);
2167 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002168 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002169}
2170
2171static void show_help(void)
2172{
2173 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002174#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002175 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002176#endif
2177#if ENABLE_FEATURE_VI_DOT_CMD
Denys Vlasenko605f2642012-06-11 01:53:33 +02002178 "\n\tLast command repeat with ."
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002179#endif
2180#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00002181 "\n\tLine marking with 'x"
2182 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002183#endif
2184#if ENABLE_FEATURE_VI_READONLY
Walter Harmsb9ba5802011-06-27 02:59:37 +02002185 //not implemented: "\n\tReadonly if vi is called as \"view\""
2186 //redundant: usage text says this too: "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002187#endif
2188#if ENABLE_FEATURE_VI_SET
Denys Vlasenko605f2642012-06-11 01:53:33 +02002189 "\n\tSome colon mode commands with :"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002190#endif
2191#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002192 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002193#endif
2194#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002195 "\n\tSignal catching- ^C"
2196 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002197#endif
2198#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002199 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002200#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002201 );
2202}
2203
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002204#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002205static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002206{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002207 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002208 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002209 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002210 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002211 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002212 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002213 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002214 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002215 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002216}
2217
2218static void end_cmd_q(void)
2219{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002220#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002221 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002222#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002223 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002224}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002225#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002226
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002227#if ENABLE_FEATURE_VI_YANKMARK \
2228 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2229 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002230// might reallocate text[]! use p += string_insert(p, ...),
2231// and be careful to not use pointers into potentially freed text[]!
2232static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002233{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002234 uintptr_t bias;
2235 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002236
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002237 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002238 bias = text_hole_make(p, i);
2239 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002240 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002241#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002242 {
2243 int cnt;
2244 for (cnt = 0; *s != '\0'; s++) {
2245 if (*s == '\n')
2246 cnt++;
2247 }
2248 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2249 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002250#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002251 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002252}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002253#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002254
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002255#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002256static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002257{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002258 int cnt = q - p;
2259 if (cnt < 0) { // they are backwards- reverse them
2260 p = q;
2261 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002262 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002263 free(reg[dest]); // if already a yank register, free it
2264 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002265 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002266}
2267
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002268static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002269{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002270 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002271
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002272 c = 'D'; // default to D-reg
2273 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002274 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002275 if (YDreg == 26)
2276 c = 'D';
2277 if (YDreg == 27)
2278 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002279 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002280}
2281
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002282static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002283{
2284 // A context is defined to be "modifying text"
2285 // Any modifying command establishes a new context.
2286
2287 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002288 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002289 // we are trying to modify text[]- make this the current context
2290 mark[27] = mark[26]; // move cur to prev
2291 mark[26] = dot; // move local to cur
2292 context_start = prev_line(prev_line(dot));
2293 context_end = next_line(next_line(dot));
2294 //loiter= start_loiter= now;
2295 }
2296 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002297}
2298
Denis Vlasenkof882f082007-12-23 02:36:51 +00002299static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002300{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002301 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002302
2303 // the current context is in mark[26]
2304 // the previous context is in mark[27]
2305 // only swap context if other context is valid
2306 if (text <= mark[27] && mark[27] <= end - 1) {
2307 tmp = mark[27];
2308 mark[27] = mark[26];
2309 mark[26] = tmp;
2310 p = mark[26]; // where we are going- previous context
2311 context_start = prev_line(prev_line(prev_line(p)));
2312 context_end = next_line(next_line(next_line(p)));
2313 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002314 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002315}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002316#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002317
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002318//----- Set terminal attributes --------------------------------
2319static void rawmode(void)
2320{
2321 tcgetattr(0, &term_orig);
2322 term_vi = term_orig;
Denys Vlasenko6e8861b2012-01-15 23:00:13 +01002323 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG on - allow intr's
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002324 term_vi.c_iflag &= (~IXON & ~ICRNL);
2325 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002326 term_vi.c_cc[VMIN] = 1;
2327 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002328 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002329 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002330}
2331
2332static void cookmode(void)
2333{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002334 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002335 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002336}
2337
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002338#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002339//----- Come here when we get a window resize signal ---------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002340static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002341{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002342 int save_errno = errno;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002343 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002344 signal(SIGWINCH, winch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002345 query_screen_dimensions();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002346 new_screen(rows, columns); // get memory for virtual screen
2347 redraw(TRUE); // re-draw the screen
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002348 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002349}
2350
2351//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002352static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002353{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002354 int save_errno = errno;
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002355 rawmode(); // terminal to "raw"
2356 last_status_cksum = 0; // force status update
2357 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002358
2359 signal(SIGTSTP, suspend_sig);
2360 signal(SIGCONT, SIG_DFL);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002361 //kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
2362 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002363}
2364
2365//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002366static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002367{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002368 int save_errno = errno;
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002369 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002370 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002371
2372 signal(SIGCONT, cont_sig);
2373 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002374 kill(my_pid, SIGTSTP);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002375 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002376}
2377
2378//----- Come here when we get a signal ---------------------------
2379static void catch_sig(int sig)
2380{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002381 signal(SIGINT, catch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002382 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002383}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002384#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002385
Denys Vlasenko606291b2009-09-23 23:15:43 +02002386static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002387{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002388 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002389
Denys Vlasenko606291b2009-09-23 23:15:43 +02002390 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002391 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002392 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002393}
2394
2395//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002396static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002397{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002398 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002399
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002400 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002401 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002402 if (c == -1) { // EOF/error
2403 go_bottom_and_clear_to_eol();
2404 cookmode(); // terminal to "cooked"
2405 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002406 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002407 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002408}
2409
2410//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002411static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002412{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002413 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002414
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002415#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002416 if (!adding2q) {
2417 // we are not adding to the q.
2418 // but, we may be reading from a q
2419 if (ioq == 0) {
2420 // there is no current q, read from STDIN
2421 c = readit(); // get the users input
2422 } else {
2423 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002424 // careful with correct sign expansion!
2425 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002426 if (c == '\0') {
2427 // the end of the q, read from STDIN
2428 free(ioq_start);
2429 ioq_start = ioq = 0;
2430 c = readit(); // get the users input
2431 }
2432 }
2433 } else {
2434 // adding STDIN chars to q
2435 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002436 if (lmc_len >= MAX_INPUT_LEN - 1) {
2437 status_line_bold("last_modifying_cmd overrun");
2438 } else {
2439 // add new char to q
2440 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002441 }
2442 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002443#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002444 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002445#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002446 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002447}
2448
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002449// Get input line (uses "status line" area)
2450static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002451{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002452 // char [MAX_INPUT_LEN]
2453#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002454
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002455 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002456 int i;
2457
2458 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002459 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002460 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002461 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002462
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002463 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002464 while (i < MAX_INPUT_LEN) {
2465 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002466 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002467 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002468 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002469 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002470 buf[--i] = '\0';
2471 write1("\b \b"); // erase char on screen
2472 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002473 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002474 } else if (c > 0 && c < 256) { // exclude Unicode
2475 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002476 buf[i] = c;
2477 buf[++i] = '\0';
2478 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002479 }
2480 }
2481 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002482 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002483#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002484}
2485
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002486static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002487{
2488 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002489 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002490
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002491 cnt = -1;
Denys Vlasenkodef47832010-04-18 20:39:41 -07002492 if (fn && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002493 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002494 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002495}
2496
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002497// might reallocate text[]!
2498static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002499{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002500 int cnt = -1;
2501 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002502 struct stat statbuf;
2503
2504 /* Validate file */
2505 if (stat(fn, &statbuf) < 0) {
Denys Vlasenko778794d2013-01-22 10:13:52 +01002506 status_line_bold("'%s' %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002507 goto fi0;
2508 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002509 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002510 // This is not a regular file
Denys Vlasenko778794d2013-01-22 10:13:52 +01002511 status_line_bold("'%s' is not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002512 goto fi0;
2513 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002514 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002515 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002516 goto fi0;
2517 }
2518
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002519 // read file to buffer
2520 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002521 if (fd < 0) {
Denys Vlasenko778794d2013-01-22 10:13:52 +01002522 status_line_bold("'%s' %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002523 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002524 }
Denys Vlasenko778794d2013-01-22 10:13:52 +01002525 size = (statbuf.st_size < INT_MAX ? (int)statbuf.st_size : INT_MAX);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002526 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002527 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002528 if (cnt < 0) {
Denys Vlasenko778794d2013-01-22 10:13:52 +01002529 status_line_bold("'%s' %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002530 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002531 } else if (cnt < size) {
2532 // There was a partial read, shrink unused space text[]
2533 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denys Vlasenko778794d2013-01-22 10:13:52 +01002534 status_line_bold("can't read '%s'", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002535 }
2536 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002537 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002538 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002539 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002540#if ENABLE_FEATURE_VI_READONLY
2541 if (update_ro_status
2542 && ((access(fn, W_OK) < 0) ||
2543 /* root will always have access()
2544 * so we check fileperms too */
2545 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2546 )
2547 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002548 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002549 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002550#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002551 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002552}
2553
Denis Vlasenko33875382008-06-21 20:31:50 +00002554static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002555{
2556 int fd, cnt, charcnt;
2557
2558 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002559 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002560 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002561 }
Denis Vlasenko8abae882008-05-03 11:35:59 +00002562 /* By popular request we do not open file with O_TRUNC,
2563 * but instead ftruncate() it _after_ successful write.
2564 * Might reduce amount of data lost on power fail etc.
2565 */
2566 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002567 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002568 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002569 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002570 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002571 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002572 if (charcnt == cnt) {
2573 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002574 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002575 } else {
2576 charcnt = 0;
2577 }
2578 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002579 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002580}
2581
2582//----- Terminal Drawing ---------------------------------------
2583// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002584// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002585// screen coordinates
2586// 0,0 ... 0,79
2587// 1,0 ... 1,79
2588// . ... .
2589// . ... .
2590// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002591// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002592
2593//----- Move the cursor to row x col (count from 0, not 1) -------
Denys Vlasenko04b52892012-06-11 13:51:38 +02002594static void place_cursor(int row, int col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002595{
Denys Vlasenko04b52892012-06-11 13:51:38 +02002596 char cm1[sizeof(ESC_SET_CURSOR_POS) + sizeof(int)*3 * 2];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002597
2598 if (row < 0) row = 0;
2599 if (row >= rows) row = rows - 1;
2600 if (col < 0) col = 0;
2601 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002602
Denys Vlasenko04b52892012-06-11 13:51:38 +02002603 sprintf(cm1, ESC_SET_CURSOR_POS, row + 1, col + 1);
2604 write1(cm1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002605}
2606
2607//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002608static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002609{
Denys Vlasenko04b52892012-06-11 13:51:38 +02002610 write1(ESC_CLEAR2EOL);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002611}
2612
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002613static void go_bottom_and_clear_to_eol(void)
2614{
Denys Vlasenko04b52892012-06-11 13:51:38 +02002615 place_cursor(rows - 1, 0);
2616 clear_to_eol();
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002617}
2618
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002619//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002620static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002621{
Denys Vlasenko04b52892012-06-11 13:51:38 +02002622 write1(ESC_CLEAR2EOS);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002623}
2624
2625//----- Start standout mode ------------------------------------
Denys Vlasenko04b52892012-06-11 13:51:38 +02002626static void standout_start(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002627{
Denys Vlasenko04b52892012-06-11 13:51:38 +02002628 write1(ESC_BOLD_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002629}
2630
2631//----- End standout mode --------------------------------------
Denys Vlasenko04b52892012-06-11 13:51:38 +02002632static void standout_end(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002633{
Denys Vlasenko04b52892012-06-11 13:51:38 +02002634 write1(ESC_NORM_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002635}
2636
2637//----- Flash the screen --------------------------------------
2638static void flash(int h)
2639{
Denys Vlasenko04b52892012-06-11 13:51:38 +02002640 standout_start();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002641 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002642 mysleep(h);
Denys Vlasenko04b52892012-06-11 13:51:38 +02002643 standout_end();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002644 redraw(TRUE);
2645}
2646
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002647static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002648{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002649#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002650 if (crashme > 0)
2651 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002652#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002653 if (!err_method) {
Denys Vlasenko04b52892012-06-11 13:51:38 +02002654 write1(ESC_BELL);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002655 } else {
2656 flash(10);
2657 }
2658}
2659
2660//----- Screen[] Routines --------------------------------------
2661//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002662static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002663{
2664 memset(screen, ' ', screensize); // clear new screen
2665}
2666
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002667static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002668{
2669 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002670 char *e = buf + count;
2671
Paul Fox8552aec2005-09-16 12:20:05 +00002672 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002673 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002674 return sum;
2675}
2676
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002677//----- Draw the status line at bottom of the screen -------------
2678static void show_status_line(void)
2679{
Paul Foxc3504852005-09-16 12:48:18 +00002680 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002681
Paul Fox8552aec2005-09-16 12:20:05 +00002682 // either we already have an error or status message, or we
2683 // create one.
2684 if (!have_status_msg) {
2685 cnt = format_edit_status();
2686 cksum = bufsum(status_buffer, cnt);
2687 }
2688 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002689 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002690 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002691 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002692 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002693 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002694 (columns - 1) ) {
2695 have_status_msg = 0;
2696 Hit_Return();
2697 }
2698 have_status_msg = 0;
2699 }
Denys Vlasenko04b52892012-06-11 13:51:38 +02002700 place_cursor(crow, ccol); // put cursor back in correct place
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002701 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002702 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002703}
2704
2705//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002706// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002707static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002708{
2709 va_list args;
2710
2711 va_start(args, format);
Denys Vlasenko04b52892012-06-11 13:51:38 +02002712 strcpy(status_buffer, ESC_BOLD_TEXT);
2713 vsprintf(status_buffer + sizeof(ESC_BOLD_TEXT)-1, format, args);
2714 strcat(status_buffer, ESC_NORM_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002715 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002716
Denys Vlasenko04b52892012-06-11 13:51:38 +02002717 have_status_msg = 1 + sizeof(ESC_BOLD_TEXT) + sizeof(ESC_NORM_TEXT) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002718}
2719
Paul Fox8552aec2005-09-16 12:20:05 +00002720// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002721static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002722{
2723 va_list args;
2724
2725 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002726 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002727 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002728
2729 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002730}
2731
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002732// copy s to buf, convert unprintable
2733static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002734{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002735 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002736 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002737
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002738 buf[0] = '\0';
2739 if (!s[0])
2740 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002741
2742 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002743 for (; *s; s++) {
2744 int c_is_no_print;
2745
2746 c = *s;
2747 c_is_no_print = (c & 0x80) && !Isprint(c);
2748 if (c_is_no_print) {
Denys Vlasenko04b52892012-06-11 13:51:38 +02002749 strcpy(d, ESC_NORM_TEXT);
2750 d += sizeof(ESC_NORM_TEXT)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002751 c = '.';
2752 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002753 if (c < ' ' || c == 0x7f) {
2754 *d++ = '^';
2755 c |= '@'; /* 0x40 */
2756 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002757 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002758 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002759 *d++ = c;
2760 *d = '\0';
2761 if (c_is_no_print) {
Denys Vlasenko04b52892012-06-11 13:51:38 +02002762 strcpy(d, ESC_BOLD_TEXT);
2763 d += sizeof(ESC_BOLD_TEXT)-1;
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002764 }
2765 if (*s == '\n') {
2766 *d++ = '$';
2767 *d = '\0';
2768 }
2769 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002770 break;
2771 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002772}
2773
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002774static void not_implemented(const char *s)
2775{
2776 char buf[MAX_INPUT_LEN];
2777
2778 print_literal(buf, s);
2779 status_line_bold("\'%s\' is not implemented", buf);
2780}
2781
2782// show file status on status line
2783static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002784{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002785 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002786
2787#define tot format_edit_status__tot
2788
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002789 int cur, percent, ret, trunc_at;
2790
Paul Fox8552aec2005-09-16 12:20:05 +00002791 // file_modified is now a counter rather than a flag. this
2792 // helps reduce the amount of line counting we need to do.
2793 // (this will cause a mis-reporting of modified status
2794 // once every MAXINT editing operations.)
2795
2796 // it would be nice to do a similar optimization here -- if
2797 // we haven't done a motion that could have changed which line
2798 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002799 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002800
2801 // reduce counting -- the total lines can't have
2802 // changed if we haven't done any edits.
2803 if (file_modified != last_file_modified) {
2804 tot = cur + count_lines(dot, end - 1) - 1;
2805 last_file_modified = file_modified;
2806 }
2807
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002808 // current line percent
2809 // ------------- ~~ ----------
2810 // total lines 100
2811 if (tot > 0) {
2812 percent = (100 * cur) / tot;
2813 } else {
2814 cur = tot = 0;
2815 percent = 100;
2816 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002817
Paul Fox8552aec2005-09-16 12:20:05 +00002818 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2819 columns : STATUS_BUFFER_LEN-1;
2820
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002821 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002822#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002823 "%c %s%s%s %d/%d %d%%",
2824#else
2825 "%c %s%s %d/%d %d%%",
2826#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002827 cmd_mode_indicator[cmd_mode & 3],
2828 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002829#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002830 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002831#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002832 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002833 cur, tot, percent);
2834
2835 if (ret >= 0 && ret < trunc_at)
2836 return ret; /* it all fit */
2837
2838 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002839#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002840}
2841
2842//----- Force refresh of all Lines -----------------------------
2843static void redraw(int full_screen)
2844{
Denys Vlasenko04b52892012-06-11 13:51:38 +02002845 place_cursor(0, 0);
2846 clear_to_eos();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002847 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002848 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002849 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002850 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002851}
2852
2853//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002854static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002855{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002856 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002857 int co;
2858 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002859 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002860
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002861 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002862 co = 0;
2863 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002864 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002865 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002866 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002867 if (c == '\n')
2868 break;
2869 if ((c & 0x80) && !Isprint(c)) {
2870 c = '.';
2871 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002872 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002873 if (c == '\t') {
2874 c = ' ';
2875 // co % 8 != 7
2876 while ((co % tabstop) != (tabstop - 1)) {
2877 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002878 }
2879 } else {
2880 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002881 if (c == 0x7f)
2882 c = '?';
2883 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002884 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002885 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002886 }
2887 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002888 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002889 // discard scrolled-off-to-the-left portion,
2890 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002891 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002892 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002893 co -= tabstop;
2894 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002895 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002896 if (src >= end)
2897 break;
2898 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002899 // check "short line, gigantic offset" case
2900 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002901 ofs = co;
2902 // discard last scrolled off part
2903 co -= ofs;
2904 dest += ofs;
2905 // fill the rest with spaces
2906 if (co < columns)
2907 memset(&dest[co], ' ', columns - co);
2908 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002909}
2910
2911//----- Refresh the changed screen lines -----------------------
2912// Copy the source line from text[] into the buffer and note
2913// if the current screenline is different from the new buffer.
2914// If they differ then that line needs redrawing on the terminal.
2915//
2916static void refresh(int full_screen)
2917{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002918#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002919
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002920 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002921 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002922
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +02002923 if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002924 unsigned c = columns, r = rows;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002925 query_screen_dimensions();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002926 full_screen |= (c - columns) | (r - rows);
2927 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002928 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2929 tp = screenbegin; // index into text[] of top line
2930
2931 // compare text[] to screen[] and mark screen[] lines that need updating
2932 for (li = 0; li < rows - 1; li++) {
2933 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002934 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002935 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002936 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002937
2938 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002939 if (tp < end) {
2940 char *t = memchr(tp, '\n', end - tp);
2941 if (!t) t = end - 1;
2942 tp = t + 1;
2943 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002944
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002945 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002946 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002947 cs = 0;
2948 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002949 sp = &screen[li * columns]; // start of screen line
2950 if (full_screen) {
2951 // force re-draw of every single column from 0 - columns-1
2952 goto re0;
2953 }
2954 // compare newly formatted buffer with virtual screen
2955 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002956 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002957 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002958 changed = TRUE; // mark for redraw
2959 break;
2960 }
2961 }
2962
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002963 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002964 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002965 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002966 changed = TRUE; // mark for redraw
2967 break;
2968 }
2969 }
2970 // now, cs is index of first diff, and ce is index of last diff
2971
2972 // if horz offset has changed, force a redraw
2973 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002974 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002975 changed = TRUE;
2976 }
2977
2978 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002979 if (cs < 0) cs = 0;
2980 if (ce > columns - 1) ce = columns - 1;
2981 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002982 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002983 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002984 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002985 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Denys Vlasenko04b52892012-06-11 13:51:38 +02002986 place_cursor(li, cs);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002987 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002988 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002989 }
2990 }
2991
Denys Vlasenko04b52892012-06-11 13:51:38 +02002992 place_cursor(crow, ccol);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002993
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002994 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002995#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002996}
2997
Eric Andersen3f980402001-04-04 17:31:15 +00002998//---------------------------------------------------------------------
2999//----- the Ascii Chart -----------------------------------------------
3000//
3001// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
3002// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
3003// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
3004// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
3005// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
3006// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
3007// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
3008// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
3009// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
3010// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
3011// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
3012// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
3013// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
3014// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
3015// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
3016// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
3017//---------------------------------------------------------------------
3018
3019//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003020static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00003021{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003022 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003023 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003024 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00003025 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003026 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003027
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00003028// c1 = c; // quiet the compiler
3029// cnt = yf = 0; // quiet the compiler
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003030// p = q = save_dot = buf; // quiet the compiler
3031 memset(buf, '\0', sizeof(buf));
Eric Andersenbff7a602001-11-17 07:15:43 +00003032
Paul Fox8552aec2005-09-16 12:20:05 +00003033 show_status_line();
3034
Eric Andersenbff7a602001-11-17 07:15:43 +00003035 /* if this is a cursor key, skip these checks */
3036 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003037 case KEYCODE_UP:
3038 case KEYCODE_DOWN:
3039 case KEYCODE_LEFT:
3040 case KEYCODE_RIGHT:
3041 case KEYCODE_HOME:
3042 case KEYCODE_END:
3043 case KEYCODE_PAGEUP:
3044 case KEYCODE_PAGEDOWN:
3045 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00003046 goto key_cmd_mode;
3047 }
3048
Eric Andersen3f980402001-04-04 17:31:15 +00003049 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003050 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003051 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003052 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00003053 // we are 'R'eplacing the current *dot with new char
3054 if (*dot == '\n') {
3055 // don't Replace past E-o-l
3056 cmd_mode = 1; // convert to insert
3057 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003058 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003059 if (c != 27)
3060 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3061 dot = char_insert(dot, c); // insert new char
3062 }
3063 goto dc1;
3064 }
3065 }
3066 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003067 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003068 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00003069 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003070 if (1 <= c || Isprint(c)) {
3071 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00003072 }
3073 goto dc1;
3074 }
3075
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003076 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00003077 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00003078 //case 0x01: // soh
3079 //case 0x09: // ht
3080 //case 0x0b: // vt
3081 //case 0x0e: // so
3082 //case 0x0f: // si
3083 //case 0x10: // dle
3084 //case 0x11: // dc1
3085 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003086#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00003087 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00003088 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003089 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003090#endif
Eric Andersen822c3832001-05-07 17:37:43 +00003091 //case 0x16: // syn
3092 //case 0x17: // etb
3093 //case 0x18: // can
3094 //case 0x1c: // fs
3095 //case 0x1d: // gs
3096 //case 0x1e: // rs
3097 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003098 //case '!': // !-
3099 //case '#': // #-
3100 //case '&': // &-
3101 //case '(': // (-
3102 //case ')': // )-
3103 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003104 //case '=': // =-
3105 //case '@': // @-
3106 //case 'F': // F-
3107 //case 'K': // K-
3108 //case 'Q': // Q-
3109 //case 'S': // S-
3110 //case 'T': // T-
3111 //case 'V': // V-
3112 //case '[': // [-
3113 //case '\\': // \-
3114 //case ']': // ]-
3115 //case '_': // _-
3116 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003117 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003118 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003119 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00003120 buf[0] = c;
3121 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003122 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003123 end_cmd_q(); // stop adding to q
3124 case 0x00: // nul- ignore
3125 break;
3126 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003127 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00003128 dot_scroll(rows - 2, -1);
3129 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003130 case 4: // ctrl-D scroll down half screen
3131 dot_scroll((rows - 2) / 2, 1);
3132 break;
3133 case 5: // ctrl-E scroll down one line
3134 dot_scroll(1, 1);
3135 break;
3136 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003137 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003138 dot_scroll(rows - 2, 1);
3139 break;
3140 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00003141 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003142 break;
3143 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003144 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003145 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003146 case 0x7f: // DEL- move left (This may be ERASE char)
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003147 do {
3148 dot_left();
3149 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003150 break;
3151 case 10: // Newline ^J
3152 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003153 case KEYCODE_DOWN: // cursor key Down
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003154 do {
3155 dot_next(); // go to next B-o-l
3156 // try stay in same col
3157 dot = move_to_col(dot, ccol + offset);
3158 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003159 break;
3160 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003161 case 18: // ctrl-R force redraw
Denys Vlasenko04b52892012-06-11 13:51:38 +02003162 place_cursor(0, 0);
3163 clear_to_eos();
3164 //mysleep(10); // why???
Eric Andersen3f980402001-04-04 17:31:15 +00003165 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003166 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003167 refresh(TRUE); // this will redraw the entire display
3168 break;
3169 case 13: // Carriage Return ^M
3170 case '+': // +- goto next line
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003171 do {
3172 dot_next();
3173 dot_skip_over_ws();
3174 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003175 break;
3176 case 21: // ctrl-U scroll up half screen
3177 dot_scroll((rows - 2) / 2, -1);
3178 break;
3179 case 25: // ctrl-Y scroll up one line
3180 dot_scroll(1, -1);
3181 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003182 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003183 if (cmd_mode == 0)
3184 indicate_error(c);
3185 cmd_mode = 0; // stop insrting
3186 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003187 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003188 break;
3189 case ' ': // move right
3190 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003191 case KEYCODE_RIGHT: // Cursor Key Right
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003192 do {
3193 dot_right();
3194 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003195 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003196#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003197 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003198 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3199 if ((unsigned)c1 <= 25) { // a-z?
3200 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003201 } else {
3202 indicate_error(c);
3203 }
3204 break;
3205 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003206 c1 = (get_one_char() | 0x20) - 'a';
3207 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003208 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003209 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003210 if (text <= q && q < end) {
3211 dot = q;
3212 dot_begin(); // go to B-o-l
3213 dot_skip_over_ws();
3214 }
3215 } else if (c1 == '\'') { // goto previous context
3216 dot = swap_context(dot); // swap current and previous context
3217 dot_begin(); // go to B-o-l
3218 dot_skip_over_ws();
3219 } else {
3220 indicate_error(c);
3221 }
3222 break;
3223 case 'm': // m- Mark a line
3224 // this is really stupid. If there are any inserts or deletes
3225 // between text[0] and dot then this mark will not point to the
3226 // correct location! It could be off by many lines!
3227 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003228 c1 = (get_one_char() | 0x20) - 'a';
3229 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003230 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003231 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003232 } else {
3233 indicate_error(c);
3234 }
3235 break;
3236 case 'P': // P- Put register before
3237 case 'p': // p- put register after
3238 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003239 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003240 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003241 break;
3242 }
3243 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003244 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003245 if (c == 'P') {
3246 dot_begin(); // putting lines- Put above
3247 }
3248 if (c == 'p') {
3249 // are we putting after very last line?
3250 if (end_line(dot) == (end - 1)) {
3251 dot = end; // force dot to end of text[]
3252 } else {
3253 dot_next(); // next line, then put before
3254 }
3255 }
3256 } else {
3257 if (c == 'p')
3258 dot_right(); // move to right, can move to NL
3259 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003260 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003261 end_cmd_q(); // stop adding to q
3262 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003263 case 'U': // U- Undo; replace current line with original version
Denys Vlasenko800a9a02012-01-31 14:10:26 +01003264 if (reg[Ureg] != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003265 p = begin_line(dot);
3266 q = end_line(dot);
3267 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003268 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003269 dot = p;
3270 dot_skip_over_ws();
3271 }
3272 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003273#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003274 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003275 case KEYCODE_END: // Cursor Key End
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003276 for (;;) {
3277 dot = end_line(dot);
Denys Vlasenko1fd71292011-11-28 04:55:48 +01003278 if (--cmdcnt <= 0)
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003279 break;
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003280 dot_next();
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003281 }
Eric Andersen3f980402001-04-04 17:31:15 +00003282 break;
3283 case '%': // %- find matching char of pair () [] {}
3284 for (q = dot; q < end && *q != '\n'; q++) {
3285 if (strchr("()[]{}", *q) != NULL) {
3286 // we found half of a pair
3287 p = find_pair(q, *q);
3288 if (p == NULL) {
3289 indicate_error(c);
3290 } else {
3291 dot = p;
3292 }
3293 break;
3294 }
3295 }
3296 if (*q == '\n')
3297 indicate_error(c);
3298 break;
3299 case 'f': // f- forward to a user specified char
3300 last_forward_char = get_one_char(); // get the search char
3301 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003302 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003303 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003304 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003305 case ';': // ;- look at rest of line for last forward char
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003306 do {
3307 if (last_forward_char == 0)
3308 break;
3309 q = dot + 1;
3310 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3311 q++;
3312 }
3313 if (*q == last_forward_char)
3314 dot = q;
3315 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003316 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003317 case ',': // repeat latest 'f' in opposite direction
Paul Foxb5ee8db2008-02-14 01:17:01 +00003318 if (last_forward_char == 0)
3319 break;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003320 do {
3321 q = dot - 1;
3322 while (q >= text && *q != '\n' && *q != last_forward_char) {
3323 q--;
3324 }
3325 if (q >= text && *q == last_forward_char)
3326 dot = q;
3327 } while (--cmdcnt > 0);
Paul Foxb5ee8db2008-02-14 01:17:01 +00003328 break;
3329
Eric Andersen3f980402001-04-04 17:31:15 +00003330 case '-': // -- goto prev line
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003331 do {
3332 dot_prev();
3333 dot_skip_over_ws();
3334 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003335 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003336#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003337 case '.': // .- repeat the last modifying command
3338 // Stuff the last_modifying_cmd back into stdin
3339 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003340 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003341 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003342 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003343 }
3344 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003345#endif
3346#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003347 case '?': // /- search for a pattern
3348 case '/': // /- search for a pattern
3349 buf[0] = c;
3350 buf[1] = '\0';
3351 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003352 if (q[0] && !q[1]) {
3353 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003354 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003355 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003356 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003357 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003358 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003359 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003360 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003361 goto dc3; // now find the pattern
3362 }
3363 // user changed mind and erased the "/"- do nothing
3364 break;
3365 case 'N': // N- backward search for last pattern
Eric Andersen3f980402001-04-04 17:31:15 +00003366 dir = BACK; // assume BACKWARD search
3367 p = dot - 1;
3368 if (last_search_pattern[0] == '?') {
3369 dir = FORWARD;
3370 p = dot + 1;
3371 }
3372 goto dc4; // now search for pattern
3373 break;
3374 case 'n': // n- repeat search for last pattern
3375 // search rest of text[] starting at next char
3376 // if search fails return orignal "p" not the "p+1" address
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003377 do {
3378 const char *msg;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003379 dc3:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003380 dir = FORWARD; // assume FORWARD search
3381 p = dot + 1;
3382 if (last_search_pattern[0] == '?') {
3383 dir = BACK;
3384 p = dot - 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003385 }
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003386 dc4:
3387 q = char_search(p, last_search_pattern + 1, dir, FULL);
3388 if (q != NULL) {
3389 dot = q; // good search, update "dot"
3390 msg = NULL;
3391 goto dc2;
3392 }
3393 // no pattern found between "dot" and "end"- continue at top
3394 p = text;
3395 if (dir == BACK) {
3396 p = end - 1;
3397 }
3398 q = char_search(p, last_search_pattern + 1, dir, FULL);
3399 if (q != NULL) { // found something
3400 dot = q; // found new pattern- goto it
3401 msg = "search hit BOTTOM, continuing at TOP";
3402 if (dir == BACK) {
3403 msg = "search hit TOP, continuing at BOTTOM";
3404 }
3405 } else {
3406 msg = "Pattern not found";
3407 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003408 dc2:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003409 if (msg)
3410 status_line_bold("%s", msg);
3411 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003412 break;
3413 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003414 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003415 if (q != NULL) { // found blank line
3416 dot = next_line(q); // move to next blank line
3417 }
3418 break;
3419 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003420 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003421 if (q != NULL) { // found blank line
3422 dot = next_line(q); // move to next blank line
3423 }
3424 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003425#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003426 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003427 case '1': // 1-
3428 case '2': // 2-
3429 case '3': // 3-
3430 case '4': // 4-
3431 case '5': // 5-
3432 case '6': // 6-
3433 case '7': // 7-
3434 case '8': // 8-
3435 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003436 if (c == '0' && cmdcnt < 1) {
3437 dot_begin(); // this was a standalone zero
3438 } else {
3439 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3440 }
3441 break;
3442 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003443 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003444#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003445 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003446#else
Eric Andersen822c3832001-05-07 17:37:43 +00003447 if (*p == ':')
3448 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003449 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003450 if (cnt <= 0)
3451 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003452 if (strncmp(p, "quit", cnt) == 0
3453 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003454 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003455 if (file_modified && p[1] != '!') {
Dennis Groenenc0657e02012-01-31 14:12:38 +01003456 status_line_bold("No write since last change (:%s! overrides)", p);
Eric Andersen3f980402001-04-04 17:31:15 +00003457 } else {
3458 editing = 0;
3459 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003460 } else if (strncmp(p, "write", cnt) == 0
3461 || strncmp(p, "wq", cnt) == 0
3462 || strncmp(p, "wn", cnt) == 0
3463 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003464 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003465 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003466 if (cnt < 0) {
3467 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003468 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003469 } else {
3470 file_modified = 0;
3471 last_file_modified = -1;
Denys Vlasenko778794d2013-01-22 10:13:52 +01003472 status_line("'%s' %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003473 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3474 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3475 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003476 editing = 0;
3477 }
Eric Andersen3f980402001-04-04 17:31:15 +00003478 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003479 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003480 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003481 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003482 dot = find_line(j); // go to line # j
3483 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003484 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003485 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003486 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003487#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003488 break;
3489 case '<': // <- Left shift something
3490 case '>': // >- Right shift something
3491 cnt = count_lines(text, dot); // remember what line we are on
3492 c1 = get_one_char(); // get the type of thing to delete
3493 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003494 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003495 p = begin_line(p);
3496 q = end_line(q);
3497 i = count_lines(p, q); // # of lines we are shifting
3498 for ( ; i > 0; i--, p = next_line(p)) {
3499 if (c == '<') {
3500 // shift left- remove tab or 8 spaces
3501 if (*p == '\t') {
3502 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003503 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003504 } else if (*p == ' ') {
3505 // we should be calculating columns, not just SPACE
3506 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003507 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003508 }
3509 }
3510 } else if (c == '>') {
3511 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003512 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003513 }
3514 }
3515 dot = find_line(cnt); // what line were we on
3516 dot_skip_over_ws();
3517 end_cmd_q(); // stop adding to q
3518 break;
3519 case 'A': // A- append at e-o-l
3520 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003521 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003522 case 'a': // a- append after current char
3523 if (*dot != '\n')
3524 dot++;
3525 goto dc_i;
3526 break;
3527 case 'B': // B- back a blank-delimited Word
3528 case 'E': // E- end of a blank-delimited word
3529 case 'W': // W- forward a blank-delimited word
Eric Andersen3f980402001-04-04 17:31:15 +00003530 dir = FORWARD;
3531 if (c == 'B')
3532 dir = BACK;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003533 do {
3534 if (c == 'W' || isspace(dot[dir])) {
3535 dot = skip_thing(dot, 1, dir, S_TO_WS);
3536 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3537 }
3538 if (c != 'W')
3539 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3540 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003541 break;
3542 case 'C': // C- Change to e-o-l
3543 case 'D': // D- delete to e-o-l
3544 save_dot = dot;
3545 dot = dollar_line(dot); // move to before NL
3546 // copy text into a register and delete
3547 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3548 if (c == 'C')
3549 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003550#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003551 if (c == 'D')
3552 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003553#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003554 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003555 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003556 c1 = get_one_char();
3557 if (c1 != 'g') {
3558 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003559 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003560 buf[2] = '\0';
3561 not_implemented(buf);
3562 break;
3563 }
3564 if (cmdcnt == 0)
3565 cmdcnt = 1;
3566 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003567 case 'G': // G- goto to a line number (default= E-O-F)
3568 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003569 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003570 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003571 }
3572 dot_skip_over_ws();
3573 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003574 case 'H': // H- goto top line on screen
3575 dot = screenbegin;
3576 if (cmdcnt > (rows - 1)) {
3577 cmdcnt = (rows - 1);
3578 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003579 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003580 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003581 }
Eric Andersen3f980402001-04-04 17:31:15 +00003582 dot_skip_over_ws();
3583 break;
3584 case 'I': // I- insert before first non-blank
3585 dot_begin(); // 0
3586 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003587 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003588 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003589 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003590 dc_i:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003591 cmd_mode = 1; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003592 break;
3593 case 'J': // J- join current and next lines together
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003594 do {
3595 dot_end(); // move to NL
3596 if (dot < end - 1) { // make sure not last char in text[]
3597 *dot++ = ' '; // replace NL with space
3598 file_modified++;
3599 while (isblank(*dot)) { // delete leading WS
3600 dot_delete();
3601 }
Eric Andersen3f980402001-04-04 17:31:15 +00003602 }
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003603 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003604 end_cmd_q(); // stop adding to q
3605 break;
3606 case 'L': // L- goto bottom line on screen
3607 dot = end_screen();
3608 if (cmdcnt > (rows - 1)) {
3609 cmdcnt = (rows - 1);
3610 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003611 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003612 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003613 }
Eric Andersen3f980402001-04-04 17:31:15 +00003614 dot_begin();
3615 dot_skip_over_ws();
3616 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003617 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003618 dot = screenbegin;
3619 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3620 dot = next_line(dot);
3621 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003622 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003623 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003624 p = begin_line(dot);
3625 if (p[-1] == '\n') {
3626 dot_prev();
3627 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3628 dot_end();
3629 dot = char_insert(dot, '\n');
3630 } else {
3631 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003632 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003633 dot_prev(); // -
3634 }
3635 goto dc_i;
3636 break;
3637 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003638 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003639 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003640 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003641 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003642 c = 'x';
3643 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003644 case 'X': // X- delete char before dot
3645 case 'x': // x- delete the current char
3646 case 's': // s- substitute the current char
Eric Andersen3f980402001-04-04 17:31:15 +00003647 dir = 0;
3648 if (c == 'X')
3649 dir = -1;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003650 do {
3651 if (dot[dir] != '\n') {
3652 if (c == 'X')
3653 dot--; // delete prev char
3654 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3655 }
3656 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003657 end_cmd_q(); // stop adding to q
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003658 if (c == 's')
3659 goto dc_i; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003660 break;
3661 case 'Z': // Z- if modified, {write}; exit
3662 // ZZ means to save file (if necessary), then exit
3663 c1 = get_one_char();
3664 if (c1 != 'Z') {
3665 indicate_error(c);
3666 break;
3667 }
Paul Foxf0305b72006-03-28 14:18:21 +00003668 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003669 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denys Vlasenko778794d2013-01-22 10:13:52 +01003670 status_line_bold("'%s' is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003671 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003672 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003673 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003674 if (cnt < 0) {
3675 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003676 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003677 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003678 editing = 0;
3679 }
3680 } else {
3681 editing = 0;
3682 }
3683 break;
3684 case '^': // ^- move to first non-blank on line
3685 dot_begin();
3686 dot_skip_over_ws();
3687 break;
3688 case 'b': // b- back a word
3689 case 'e': // e- end of word
Eric Andersen3f980402001-04-04 17:31:15 +00003690 dir = FORWARD;
3691 if (c == 'b')
3692 dir = BACK;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003693 do {
3694 if ((dot + dir) < text || (dot + dir) > end - 1)
3695 break;
3696 dot += dir;
3697 if (isspace(*dot)) {
3698 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3699 }
3700 if (isalnum(*dot) || *dot == '_') {
3701 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3702 } else if (ispunct(*dot)) {
3703 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3704 }
3705 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003706 break;
3707 case 'c': // c- change something
3708 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003709#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003710 case 'y': // y- yank something
3711 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003712#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003713 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003714 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003715 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003716#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003717 if (c == 'y' || c == 'Y')
3718 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003719#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003720 c1 = 'y';
3721 if (c != 'Y')
3722 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003723 // determine range, and whether it spans lines
3724 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003725 if (c1 == 27) { // ESC- user changed mind and wants out
3726 c = c1 = 27; // Escape- do nothing
3727 } else if (strchr("wW", c1)) {
3728 if (c == 'c') {
3729 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003730 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003731 if (q <= text || q[-1] == '\n')
3732 break;
3733 q--;
3734 }
3735 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003736 dot = yank_delete(p, q, ml, yf); // delete word
3737 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3738 // partial line copy text into a register and delete
3739 dot = yank_delete(p, q, ml, yf); // delete word
3740 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3741 // whole line copy text into a register and delete
3742 dot = yank_delete(p, q, ml, yf); // delete lines
3743 whole = 1;
3744 } else {
3745 // could not recognize object
3746 c = c1 = 27; // error-
3747 ml = 0;
3748 indicate_error(c);
3749 }
3750 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003751 if (c == 'c') {
3752 dot = char_insert(dot, '\n');
3753 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003754 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003755 dot_prev();
3756 }
3757 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003758 dot_begin();
3759 dot_skip_over_ws();
3760 }
Eric Andersen3f980402001-04-04 17:31:15 +00003761 }
3762 if (c1 != 27) {
3763 // if CHANGING, not deleting, start inserting after the delete
3764 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003765 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003766 goto dc_i; // start inserting
3767 }
3768 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003769 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003770 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003771#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003772 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003773 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003774 }
3775 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003776 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003777 for (cnt = 0; p <= q; p++) {
3778 if (*p == '\n')
3779 cnt++;
3780 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003781 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003782 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003783#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003784 end_cmd_q(); // stop adding to q
3785 }
3786 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003787 }
Eric Andersen3f980402001-04-04 17:31:15 +00003788 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003789 case KEYCODE_UP: // cursor key Up
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003790 do {
3791 dot_prev();
3792 dot = move_to_col(dot, ccol + offset); // try stay in same col
3793 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003794 break;
3795 case 'r': // r- replace the current char with user input
3796 c1 = get_one_char(); // get the replacement char
3797 if (*dot != '\n') {
3798 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003799 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003800 }
3801 end_cmd_q(); // stop adding to q
3802 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003803 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003804 last_forward_char = get_one_char();
3805 do_cmd(';');
3806 if (*dot == last_forward_char)
3807 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003808 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003809 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003810 case 'w': // w- forward a word
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003811 do {
3812 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3813 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3814 } else if (ispunct(*dot)) { // we are on PUNCT
3815 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3816 }
3817 if (dot < end - 1)
3818 dot++; // move over word
3819 if (isspace(*dot)) {
3820 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3821 }
3822 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003823 break;
3824 case 'z': // z-
3825 c1 = get_one_char(); // get the replacement char
3826 cnt = 0;
3827 if (c1 == '.')
3828 cnt = (rows - 2) / 2; // put dot at center
3829 if (c1 == '-')
3830 cnt = rows - 2; // put dot at bottom
3831 screenbegin = begin_line(dot); // start dot at top
3832 dot_scroll(cnt, -1);
3833 break;
3834 case '|': // |- move to column "cmdcnt"
3835 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3836 break;
3837 case '~': // ~- flip the case of letters a-z -> A-Z
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003838 do {
3839 if (islower(*dot)) {
3840 *dot = toupper(*dot);
3841 file_modified++;
3842 } else if (isupper(*dot)) {
3843 *dot = tolower(*dot);
3844 file_modified++;
3845 }
3846 dot_right();
3847 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003848 end_cmd_q(); // stop adding to q
3849 break;
3850 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003851 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003852 dot_begin();
3853 break;
3854 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003855#if 0
3856 case KEYCODE_FUN1: // Function Key F1
3857 case KEYCODE_FUN2: // Function Key F2
3858 case KEYCODE_FUN3: // Function Key F3
3859 case KEYCODE_FUN4: // Function Key F4
3860 case KEYCODE_FUN5: // Function Key F5
3861 case KEYCODE_FUN6: // Function Key F6
3862 case KEYCODE_FUN7: // Function Key F7
3863 case KEYCODE_FUN8: // Function Key F8
3864 case KEYCODE_FUN9: // Function Key F9
3865 case KEYCODE_FUN10: // Function Key F10
3866 case KEYCODE_FUN11: // Function Key F11
3867 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003868 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003869#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003870 }
3871
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003872 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003873 // if text[] just became empty, add back an empty line
3874 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003875 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003876 dot = text;
3877 }
3878 // it is OK for dot to exactly equal to end, otherwise check dot validity
3879 if (dot != end) {
3880 dot = bound_dot(dot); // make sure "dot" is valid
3881 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003882#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003883 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003884#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003885
3886 if (!isdigit(c))
3887 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3888 cnt = dot - begin_line(dot);
3889 // Try to stay off of the Newline
3890 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3891 dot--;
3892}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003893
Paul Fox35e9c5d2008-03-06 16:26:12 +00003894/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003895#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003896static int totalcmds = 0;
3897static int Mp = 85; // Movement command Probability
3898static int Np = 90; // Non-movement command Probability
3899static int Dp = 96; // Delete command Probability
3900static int Ip = 97; // Insert command Probability
3901static int Yp = 98; // Yank command Probability
3902static int Pp = 99; // Put command Probability
3903static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003904static const char chars[20] = "\t012345 abcdABCD-=.$";
3905static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003906 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003907 "broadcast", "the", "emergency", "of",
3908 "system", "quick", "brown", "fox",
3909 "jumped", "over", "lazy", "dogs",
3910 "back", "January", "Febuary", "March"
3911};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003912static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003913 "You should have received a copy of the GNU General Public License\n",
3914 "char c, cm, *cmd, *cmd1;\n",
3915 "generate a command by percentages\n",
3916 "Numbers may be typed as a prefix to some commands.\n",
3917 "Quit, discarding changes!\n",
3918 "Forced write, if permission originally not valid.\n",
3919 "In general, any ex or ed command (such as substitute or delete).\n",
3920 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3921 "Please get w/ me and I will go over it with you.\n",
3922 "The following is a list of scheduled, committed changes.\n",
3923 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3924 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3925 "Any question about transactions please contact Sterling Huxley.\n",
3926 "I will try to get back to you by Friday, December 31.\n",
3927 "This Change will be implemented on Friday.\n",
3928 "Let me know if you have problems accessing this;\n",
3929 "Sterling Huxley recently added you to the access list.\n",
3930 "Would you like to go to lunch?\n",
3931 "The last command will be automatically run.\n",
3932 "This is too much english for a computer geek.\n",
3933};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003934static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003935 "You should have received a copy of the GNU General Public License\n",
3936 "char c, cm, *cmd, *cmd1;\n",
3937 "generate a command by percentages\n",
3938 "Numbers may be typed as a prefix to some commands.\n",
3939 "Quit, discarding changes!\n",
3940 "Forced write, if permission originally not valid.\n",
3941 "In general, any ex or ed command (such as substitute or delete).\n",
3942 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3943 "Please get w/ me and I will go over it with you.\n",
3944 "The following is a list of scheduled, committed changes.\n",
3945 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3946 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3947 "Any question about transactions please contact Sterling Huxley.\n",
3948 "I will try to get back to you by Friday, December 31.\n",
3949 "This Change will be implemented on Friday.\n",
3950 "Let me know if you have problems accessing this;\n",
3951 "Sterling Huxley recently added you to the access list.\n",
3952 "Would you like to go to lunch?\n",
3953 "The last command will be automatically run.\n",
3954 "This is too much english for a computer geek.\n",
3955};
3956
3957// create a random command to execute
3958static void crash_dummy()
3959{
3960 static int sleeptime; // how long to pause between commands
3961 char c, cm, *cmd, *cmd1;
3962 int i, cnt, thing, rbi, startrbi, percent;
3963
3964 // "dot" movement commands
3965 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3966
3967 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02003968 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003969 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003970 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02003971 readbuffer[0] = 'X';
3972 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003973 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003974 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003975 // generate a command by percentages
3976 percent = (int) lrand48() % 100; // get a number from 0-99
3977 if (percent < Mp) { // Movement commands
3978 // available commands
3979 cmd = cmd1;
3980 M++;
3981 } else if (percent < Np) { // non-movement commands
3982 cmd = "mz<>\'\""; // available commands
3983 N++;
3984 } else if (percent < Dp) { // Delete commands
3985 cmd = "dx"; // available commands
3986 D++;
3987 } else if (percent < Ip) { // Inset commands
3988 cmd = "iIaAsrJ"; // available commands
3989 I++;
3990 } else if (percent < Yp) { // Yank commands
3991 cmd = "yY"; // available commands
3992 Y++;
3993 } else if (percent < Pp) { // Put commands
3994 cmd = "pP"; // available commands
3995 P++;
3996 } else {
3997 // We do not know how to handle this command, try again
3998 U++;
3999 goto cd0;
4000 }
4001 // randomly pick one of the available cmds from "cmd[]"
4002 i = (int) lrand48() % strlen(cmd);
4003 cm = cmd[i];
4004 if (strchr(":\024", cm))
4005 goto cd0; // dont allow colon or ctrl-T commands
4006 readbuffer[rbi++] = cm; // put cmd into input buffer
4007
4008 // now we have the command-
4009 // there are 1, 2, and multi char commands
4010 // find out which and generate the rest of command as necessary
4011 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
4012 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
4013 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
4014 cmd1 = "abcdefghijklmnopqrstuvwxyz";
4015 }
4016 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4017 c = cmd1[thing];
4018 readbuffer[rbi++] = c; // add movement to input buffer
4019 }
4020 if (strchr("iIaAsc", cm)) { // multi-char commands
4021 if (cm == 'c') {
4022 // change some thing
4023 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4024 c = cmd1[thing];
4025 readbuffer[rbi++] = c; // add movement to input buffer
4026 }
4027 thing = (int) lrand48() % 4; // what thing to insert
4028 cnt = (int) lrand48() % 10; // how many to insert
4029 for (i = 0; i < cnt; i++) {
4030 if (thing == 0) { // insert chars
4031 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
4032 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004033 strcat(readbuffer, words[(int) lrand48() % 20]);
4034 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004035 sleeptime = 0; // how fast to type
4036 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004037 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004038 sleeptime = 0; // how fast to type
4039 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004040 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004041 sleeptime = 0; // how fast to type
4042 }
4043 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004044 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004045 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02004046 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004047 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004048 totalcmds++;
4049 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004050 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004051}
4052
4053// test to see if there are any errors
4054static void crash_test()
4055{
4056 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004057
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004058 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004059 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004060
4061 msg[0] = '\0';
4062 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004063 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004064 }
4065 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004066 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004067 }
4068 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004069 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004070 }
4071 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004072 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004073 }
4074 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004075 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004076 }
4077 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004078 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004079 }
4080
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004081 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00004082 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Denys Vlasenko04b52892012-06-11 13:51:38 +02004083 totalcmds, last_input_char, msg, ESC_BOLD_TEXT, ESC_NORM_TEXT);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01004084 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00004085 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004086 if (d[0] == '\n' || d[0] == '\r')
4087 break;
4088 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004089 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004090 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004091 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004092 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004093 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
4094 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
4095 oldtim = tim;
4096 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004097}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00004098#endif