blob: 5d214e299abfdb0bc6c317bf53c20ff3ce3b28aa [file] [log] [blame]
Rob Landleye5e1a102006-06-21 01:15:36 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3f980402001-04-04 17:31:15 +00002/*
3 * tiny vi.c: A small 'vi' clone
4 * Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com>
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen3f980402001-04-04 17:31:15 +00007 */
8
Eric Andersen3f980402001-04-04 17:31:15 +00009/*
Eric Andersen3f980402001-04-04 17:31:15 +000010 * Things To Do:
11 * EXINIT
Eric Andersen1c0d3112001-04-16 15:46:44 +000012 * $HOME/.exrc and ./.exrc
Eric Andersen3f980402001-04-04 17:31:15 +000013 * add magic to search /foo.*bar
14 * add :help command
15 * :map macros
Eric Andersen3f980402001-04-04 17:31:15 +000016 * if mark[] values were line numbers rather than pointers
Tanguy Pruvot823694d2012-11-18 13:20:29 +010017 * it would be easier to change the mark when add/delete lines
Eric Andersen1c0d3112001-04-16 15:46:44 +000018 * More intelligence in refresh()
19 * ":r !cmd" and "!cmd" to filter text through an external command
20 * A true "undo" facility
21 * An "ex" line oriented mode- maybe using "cmdedit"
Eric Andersen3f980402001-04-04 17:31:15 +000022 */
23
Walter Harmsb9ba5802011-06-27 02:59:37 +020024//config:config VI
25//config: bool "vi"
26//config: default y
27//config: help
28//config: 'vi' is a text editor. More specifically, it is the One True
29//config: text editor <grin>. It does, however, have a rather steep
30//config: learning curve. If you are not already comfortable with 'vi'
31//config: you may wish to use something else.
32//config:
33//config:config FEATURE_VI_MAX_LEN
34//config: int "Maximum screen width in vi"
35//config: range 256 16384
36//config: default 4096
37//config: depends on VI
38//config: help
39//config: Contrary to what you may think, this is not eating much.
40//config: Make it smaller than 4k only if you are very limited on memory.
41//config:
42//config:config FEATURE_VI_8BIT
43//config: bool "Allow vi to display 8-bit chars (otherwise shows dots)"
44//config: default n
45//config: depends on VI
46//config: help
47//config: If your terminal can display characters with high bit set,
48//config: you may want to enable this. Note: vi is not Unicode-capable.
49//config: If your terminal combines several 8-bit bytes into one character
50//config: (as in Unicode mode), this will not work properly.
51//config:
52//config:config FEATURE_VI_COLON
53//config: bool "Enable \":\" colon commands (no \"ex\" mode)"
54//config: default y
55//config: depends on VI
56//config: help
57//config: Enable a limited set of colon commands for vi. This does not
58//config: provide an "ex" mode.
59//config:
60//config:config FEATURE_VI_YANKMARK
61//config: bool "Enable yank/put commands and mark cmds"
62//config: default y
63//config: depends on VI
64//config: help
65//config: This will enable you to use yank and put, as well as mark in
66//config: busybox vi.
67//config:
68//config:config FEATURE_VI_SEARCH
69//config: bool "Enable search and replace cmds"
70//config: default y
71//config: depends on VI
72//config: help
73//config: Select this if you wish to be able to do search and replace in
74//config: busybox vi.
75//config:
76//config:config FEATURE_VI_REGEX_SEARCH
77//config: bool "Enable regex in search and replace"
78//config: default n # Uses GNU regex, which may be unavailable. FIXME
79//config: depends on FEATURE_VI_SEARCH
80//config: help
81//config: Use extended regex search.
82//config:
83//config:config FEATURE_VI_USE_SIGNALS
84//config: bool "Catch signals"
85//config: default y
86//config: depends on VI
87//config: help
88//config: Selecting this option will make busybox vi signal aware. This will
89//config: make busybox vi support SIGWINCH to deal with Window Changes, catch
90//config: Ctrl-Z and Ctrl-C and alarms.
91//config:
92//config:config FEATURE_VI_DOT_CMD
93//config: bool "Remember previous cmd and \".\" cmd"
94//config: default y
95//config: depends on VI
96//config: help
97//config: Make busybox vi remember the last command and be able to repeat it.
98//config:
99//config:config FEATURE_VI_READONLY
100//config: bool "Enable -R option and \"view\" mode"
101//config: default y
102//config: depends on VI
103//config: help
104//config: Enable the read-only command line option, which allows the user to
105//config: open a file in read-only mode.
106//config:
107//config:config FEATURE_VI_SETOPTS
108//config: bool "Enable set-able options, ai ic showmatch"
109//config: default y
110//config: depends on VI
111//config: help
112//config: Enable the editor to set some (ai, ic, showmatch) options.
113//config:
114//config:config FEATURE_VI_SET
115//config: bool "Support for :set"
116//config: default y
117//config: depends on VI
118//config: help
119//config: Support for ":set".
120//config:
121//config:config FEATURE_VI_WIN_RESIZE
122//config: bool "Handle window resize"
123//config: default y
124//config: depends on VI
125//config: help
126//config: Make busybox vi behave nicely with terminals that get resized.
127//config:
128//config:config FEATURE_VI_ASK_TERMINAL
129//config: bool "Use 'tell me cursor position' ESC sequence to measure window"
130//config: default y
131//config: depends on VI
132//config: help
133//config: If terminal size can't be retrieved and $LINES/$COLUMNS are not set,
134//config: this option makes vi perform a last-ditch effort to find it:
Denys Vlasenko4e552a72011-07-25 15:18:20 +0200135//config: position cursor to 999,999 and ask terminal to report real
136//config: cursor position using "ESC [ 6 n" escape sequence, then read stdin.
Walter Harmsb9ba5802011-06-27 02:59:37 +0200137//config:
138//config: This is not clean but helps a lot on serial lines and such.
Walter Harmsb9ba5802011-06-27 02:59:37 +0200139
140//applet:IF_VI(APPLET(vi, BB_DIR_BIN, BB_SUID_DROP))
141
142//kbuild:lib-$(CONFIG_VI) += vi.o
143
Pere Orga6a3e01d2011-04-01 22:56:30 +0200144//usage:#define vi_trivial_usage
145//usage: "[OPTIONS] [FILE]..."
146//usage:#define vi_full_usage "\n\n"
147//usage: "Edit FILE\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200148//usage: IF_FEATURE_VI_COLON(
Denys Vlasenko5f556fc2012-06-11 01:53:33 +0200149//usage: "\n -c CMD Initial command to run ($EXINIT also available)"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200150//usage: )
151//usage: IF_FEATURE_VI_READONLY(
152//usage: "\n -R Read-only"
153//usage: )
Denys Vlasenko5f556fc2012-06-11 01:53:33 +0200154//usage: "\n -H List available features"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200155
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000156#include "libbb.h"
Denys Vlasenko066f3992011-07-03 03:19:43 +0200157/* Should be after libbb.h: on some systems regex.h needs sys/types.h: */
158#if ENABLE_FEATURE_VI_REGEX_SEARCH
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 Vlasenko7af5f6b2012-06-11 13:51:38 +0200197/* VT102 ESC sequences.
198 * See "Xterm Control Sequences"
199 * http://invisible-island.net/xterm/ctlseqs/ctlseqs.html
200 */
201/* Inverse/Normal text */
202#define ESC_BOLD_TEXT "\033[7m"
203#define ESC_NORM_TEXT "\033[0m"
204/* Bell */
205#define ESC_BELL "\007"
206/* Clear-to-end-of-line */
207#define ESC_CLEAR2EOL "\033[K"
208/* Clear-to-end-of-screen.
209 * (We use default param here.
210 * Full sequence is "ESC [ <num> J",
211 * <num> is 0/1/2 = "erase below/above/all".)
212 */
213#define ESC_CLEAR2EOS "\033[J"
214/* Cursor to given coordinate (1,1: top left) */
215#define ESC_SET_CURSOR_POS "\033[%u;%uH"
216//UNUSED
217///* Cursor up and down */
218//#define ESC_CURSOR_UP "\033[A"
219//#define ESC_CURSOR_DOWN "\n"
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000220
Denis Vlasenkoded6ad32008-10-14 12:26:30 +0000221#if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
222// cmds modifying text[]
223// vda: removed "aAiIs" as they switch us into insert mode
224// and remembering input for replay after them makes no sense
225static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~";
226#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000227
Rob Landleybc68cd12006-03-10 19:22:06 +0000228enum {
229 YANKONLY = FALSE,
230 YANKDEL = TRUE,
231 FORWARD = 1, // code depends on "1" for array index
232 BACK = -1, // code depends on "-1" for array index
233 LIMITED = 0, // how much of text[] in char_search
234 FULL = 1, // how much of text[] in char_search
Eric Andersen3f980402001-04-04 17:31:15 +0000235
Rob Landleybc68cd12006-03-10 19:22:06 +0000236 S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
237 S_TO_WS = 2, // used in skip_thing() for moving "dot"
238 S_OVER_WS = 3, // used in skip_thing() for moving "dot"
239 S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000240 S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
Rob Landleybc68cd12006-03-10 19:22:06 +0000241};
Eric Andersen3f980402001-04-04 17:31:15 +0000242
Denis Vlasenkob1759462008-06-20 20:20:54 +0000243
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000244/* vi.c expects chars to be unsigned. */
245/* busybox build system provides that, but it's better */
246/* to audit and fix the source */
Eric Andersen3f980402001-04-04 17:31:15 +0000247
Denis Vlasenkob1759462008-06-20 20:20:54 +0000248struct globals {
249 /* many references - keep near the top of globals */
250 char *text, *end; // pointers to the user data in memory
251 char *dot; // where all the action takes place
252 int text_size; // size of the allocated buffer
253
254 /* the rest */
255 smallint vi_setops;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000256#define VI_AUTOINDENT 1
257#define VI_SHOWMATCH 2
258#define VI_IGNORECASE 4
259#define VI_ERR_METHOD 8
260#define autoindent (vi_setops & VI_AUTOINDENT)
261#define showmatch (vi_setops & VI_SHOWMATCH )
262#define ignorecase (vi_setops & VI_IGNORECASE)
263/* indicate error with beep or flash */
264#define err_method (vi_setops & VI_ERR_METHOD)
265
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000266#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000267 smallint readonly_mode;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000268#define SET_READONLY_FILE(flags) ((flags) |= 0x01)
269#define SET_READONLY_MODE(flags) ((flags) |= 0x02)
270#define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000271#else
Denis Vlasenkob1759462008-06-20 20:20:54 +0000272#define SET_READONLY_FILE(flags) ((void)0)
273#define SET_READONLY_MODE(flags) ((void)0)
274#define UNSET_READONLY_FILE(flags) ((void)0)
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000275#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000276
Denis Vlasenkob1759462008-06-20 20:20:54 +0000277 smallint editing; // >0 while we are editing a file
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000278 // [code audit says "can be 0, 1 or 2 only"]
Denis Vlasenkob1759462008-06-20 20:20:54 +0000279 smallint cmd_mode; // 0=command 1=insert 2=replace
280 int file_modified; // buffer contents changed (counter, not flag!)
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000281 int last_file_modified; // = -1;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000282 int save_argc; // how many file names on cmd line
283 int cmdcnt; // repetition count
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200284 int rows, columns; // the terminal screen is this size
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700285#if ENABLE_FEATURE_VI_ASK_TERMINAL
286 int get_rowcol_error;
287#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000288 int crow, ccol; // cursor is on Crow x Ccol
289 int offset; // chars scrolled off the screen to the left
290 int have_status_msg; // is default edit status needed?
291 // [don't make smallint!]
292 int last_status_cksum; // hash of current status line
293 char *current_filename;
294 char *screenbegin; // index into text[], of top line on the screen
295 char *screen; // pointer to the virtual screen buffer
296 int screensize; // and its size
297 int tabstop;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000298 int last_forward_char; // last char searched for with 'f' (int because of Unicode)
Denis Vlasenkob1759462008-06-20 20:20:54 +0000299 char erase_char; // the users erase character
300 char last_input_char; // last char read from user
Denis Vlasenkob1759462008-06-20 20:20:54 +0000301
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000302#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkob1759462008-06-20 20:20:54 +0000303 smallint adding2q; // are we currently adding user input to q
304 int lmc_len; // length of last_modifying_cmd
305 char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000306#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000307#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkob1759462008-06-20 20:20:54 +0000308 int my_pid;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000309#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000310#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkob1759462008-06-20 20:20:54 +0000311 char *last_search_pattern; // last pattern from a '/' or '?' search
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000312#endif
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000313
Denis Vlasenkob1759462008-06-20 20:20:54 +0000314 /* former statics */
315#if ENABLE_FEATURE_VI_YANKMARK
316 char *edit_file__cur_line;
317#endif
318 int refresh__old_offset;
319 int format_edit_status__tot;
Eric Andersen3f980402001-04-04 17:31:15 +0000320
Denis Vlasenkob1759462008-06-20 20:20:54 +0000321 /* a few references only */
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000322#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000323 int YDreg, Ureg; // default delete register and orig line for "U"
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000324 char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000325 char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
326 char *context_start, *context_end;
327#endif
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000328#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkob1759462008-06-20 20:20:54 +0000329 sigjmp_buf restart; // catch_sig()
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000330#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000331 struct termios term_orig, term_vi; // remember what the cooked mode was
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000332#if ENABLE_FEATURE_VI_COLON
333 char *initial_cmds[3]; // currently 2 entries, NULL terminated
334#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000335 // Should be just enough to hold a key sequence,
Denis Vlasenko25497c12008-10-14 10:25:05 +0000336 // but CRASHME mode uses it as generated command buffer too
337#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko1dfeeeb2008-10-18 19:04:37 +0000338 char readbuffer[128];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000339#else
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000340 char readbuffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000341#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000342#define STATUS_BUFFER_LEN 200
343 char status_buffer[STATUS_BUFFER_LEN]; // messages to the user
344#if ENABLE_FEATURE_VI_DOT_CMD
345 char last_modifying_cmd[MAX_INPUT_LEN]; // last modifying cmd for "."
346#endif
347 char get_input_line__buf[MAX_INPUT_LEN]; /* former static */
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000348
349 char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000350};
351#define G (*ptr_to_globals)
352#define text (G.text )
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000353#define text_size (G.text_size )
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000354#define end (G.end )
355#define dot (G.dot )
356#define reg (G.reg )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000357
358#define vi_setops (G.vi_setops )
359#define editing (G.editing )
360#define cmd_mode (G.cmd_mode )
361#define file_modified (G.file_modified )
362#define last_file_modified (G.last_file_modified )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000363#define save_argc (G.save_argc )
364#define cmdcnt (G.cmdcnt )
365#define rows (G.rows )
366#define columns (G.columns )
367#define crow (G.crow )
368#define ccol (G.ccol )
369#define offset (G.offset )
370#define status_buffer (G.status_buffer )
371#define have_status_msg (G.have_status_msg )
372#define last_status_cksum (G.last_status_cksum )
373#define current_filename (G.current_filename )
374#define screen (G.screen )
375#define screensize (G.screensize )
376#define screenbegin (G.screenbegin )
377#define tabstop (G.tabstop )
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000378#define last_forward_char (G.last_forward_char )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000379#define erase_char (G.erase_char )
380#define last_input_char (G.last_input_char )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000381#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000382#define readonly_mode (G.readonly_mode )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000383#else
384#define readonly_mode 0
385#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000386#define adding2q (G.adding2q )
387#define lmc_len (G.lmc_len )
388#define ioq (G.ioq )
389#define ioq_start (G.ioq_start )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000390#define my_pid (G.my_pid )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000391#define last_search_pattern (G.last_search_pattern)
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000392
Denis Vlasenkob1759462008-06-20 20:20:54 +0000393#define edit_file__cur_line (G.edit_file__cur_line)
394#define refresh__old_offset (G.refresh__old_offset)
395#define format_edit_status__tot (G.format_edit_status__tot)
396
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000397#define YDreg (G.YDreg )
398#define Ureg (G.Ureg )
399#define mark (G.mark )
400#define context_start (G.context_start )
401#define context_end (G.context_end )
402#define restart (G.restart )
403#define term_orig (G.term_orig )
404#define term_vi (G.term_vi )
405#define initial_cmds (G.initial_cmds )
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000406#define readbuffer (G.readbuffer )
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000407#define scr_out_buf (G.scr_out_buf )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000408#define last_modifying_cmd (G.last_modifying_cmd )
409#define get_input_line__buf (G.get_input_line__buf)
410
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000411#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000412 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkob1759462008-06-20 20:20:54 +0000413 last_file_modified = -1; \
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000414 /* "" but has space for 2 chars: */ \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000415 IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000416} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000417
Denis Vlasenkob1759462008-06-20 20:20:54 +0000418
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000419static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000420static void edit_file(char *); // edit one file
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000421static void do_cmd(int); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000422static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000423static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
424static char *begin_line(char *); // return pointer to cur line B-o-l
425static char *end_line(char *); // return pointer to cur line E-o-l
426static char *prev_line(char *); // return pointer to prev line B-o-l
427static char *next_line(char *); // return pointer to next line B-o-l
428static char *end_screen(void); // get pointer to last char on screen
429static int count_lines(char *, char *); // count line from start to stop
430static char *find_line(int); // find begining of line #li
431static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000432static void dot_left(void); // move dot left- dont leave line
433static void dot_right(void); // move dot right- dont leave line
434static void dot_begin(void); // move dot to B-o-l
435static void dot_end(void); // move dot to E-o-l
436static void dot_next(void); // move dot to next line B-o-l
437static void dot_prev(void); // move dot to prev line B-o-l
438static void dot_scroll(int, int); // move the screen up or down
439static void dot_skip_over_ws(void); // move dot pat WS
440static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000441static char *bound_dot(char *); // make sure text[0] <= P < "end"
442static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000443static char *char_insert(char *, char); // insert the char c at 'p'
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000444// might reallocate text[]! use p += stupid_insert(p, ...),
445// and be careful to not use pointers into potentially freed text[]!
446static uintptr_t stupid_insert(char *, char); // stupidly insert the char c at 'p'
Paul Foxc51fc7b2008-03-06 01:34:23 +0000447static int find_range(char **, char **, char); // return pointers for an object
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000448static int st_test(char *, int, int, char *); // helper for skip_thing()
449static char *skip_thing(char *, int, int, int); // skip some object
450static char *find_pair(char *, char); // find matching pair () [] {}
451static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000452// might reallocate text[]! use p += text_hole_make(p, ...),
453// and be careful to not use pointers into potentially freed text[]!
454static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000455static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000456static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000457static void rawmode(void); // set "raw" mode on tty
458static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000459// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
460static int mysleep(int);
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000461static int readit(void); // read (maybe cursor) key from stdin
462static int get_one_char(void); // read 1 char from stdin
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000463static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000464#if !ENABLE_FEATURE_VI_READONLY
465#define file_insert(fn, p, update_ro_status) file_insert(fn, p)
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000466#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000467// file_insert might reallocate text[]!
468static int file_insert(const char *, char *, int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000469static int file_write(char *, char *, char *);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +0200470static void place_cursor(int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000471static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000472static void clear_to_eol(void);
473static void clear_to_eos(void);
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000474static void go_bottom_and_clear_to_eol(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000475static void standout_start(void); // send "start reverse video" sequence
476static void standout_end(void); // send "end reverse video" sequence
477static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000478static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000479static void status_line(const char *, ...); // print to status buf
480static void status_line_bold(const char *, ...);
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 Vlasenko5f556fc2012-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 Vlasenko5f556fc2012-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
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200595 argv += optind;
596 argc -= optind;
Eric Andersen3f980402001-04-04 17:31:15 +0000597
598 //----- This is the main file handling loop --------------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +0200599 save_argc = argc;
600 optind = 0;
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100601 // "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 }
Tanguy Pruvot823694d2012-11-18 13:20:29 +0100608 // "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{
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200650 unsigned c, r;
651 int err = get_terminal_width_height(STDIN_FILENO, &c, &r);
652 columns = c; rows = r;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700653 if (rows > MAX_SCR_ROWS)
654 rows = MAX_SCR_ROWS;
655 if (columns > MAX_SCR_COLS)
656 columns = MAX_SCR_COLS;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200657 return err;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700658}
659#else
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200660# define query_screen_dimensions() (0)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700661#endif
662
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000663static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000664{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000665#if ENABLE_FEATURE_VI_YANKMARK
666#define cur_line edit_file__cur_line
667#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000668 int c;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000669#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000670 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000671#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000672
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000673 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000674 rawmode();
675 rows = 24;
676 columns = 80;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200677 IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions();
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700678#if ENABLE_FEATURE_VI_ASK_TERMINAL
679 if (G.get_rowcol_error /* TODO? && no input on stdin */) {
680 uint64_t k;
681 write1("\033[999;999H" "\033[6n");
682 fflush_all();
683 k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100);
684 if ((int32_t)k == KEYCODE_CURSOR_POS) {
685 uint32_t rc = (k >> 32);
686 columns = (rc & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200687 if (columns > MAX_SCR_COLS)
688 columns = MAX_SCR_COLS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700689 rows = ((rc >> 16) & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200690 if (rows > MAX_SCR_ROWS)
691 rows = MAX_SCR_ROWS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700692 }
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700693 }
694#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000695 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000696 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000697
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000698#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000699 YDreg = 26; // default Yank/Delete reg
700 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000701 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000702#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000703
Eric Andersen3f980402001-04-04 17:31:15 +0000704 last_forward_char = last_input_char = '\0';
705 crow = 0;
706 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000707
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000708#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700709 signal(SIGINT, catch_sig);
Eric Andersen3f980402001-04-04 17:31:15 +0000710 signal(SIGWINCH, winch_sig);
711 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000712 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000713 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000714 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000715 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000716#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000717
Eric Andersen3f980402001-04-04 17:31:15 +0000718 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
719 cmdcnt = 0;
720 tabstop = 8;
721 offset = 0; // no horizontal offset
722 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000723#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000724 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000725 ioq = ioq_start = NULL;
726 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000727 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000728#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000729
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000730#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000731 {
732 char *p, *q;
733 int n = 0;
734
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700735 while ((p = initial_cmds[n]) != NULL) {
Denis Vlasenkof9234132007-03-21 00:03:42 +0000736 do {
737 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000738 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000739 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000740 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000741 *p++ = '\0';
742 if (*q)
743 colon(q);
744 } while (p);
745 free(initial_cmds[n]);
746 initial_cmds[n] = NULL;
747 n++;
748 }
749 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000750#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000751 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000752 //------This is the main Vi cmd handling loop -----------------------
753 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000754#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000755 if (crashme > 0) {
756 if ((end - text) > 1) {
757 crash_dummy(); // generate a random command
758 } else {
759 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000760 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
761 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000762 refresh(FALSE);
763 }
764 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000765#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000766 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000767#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000768 // save a copy of the current line- for the 'U" command
769 if (begin_line(dot) != cur_line) {
770 cur_line = begin_line(dot);
771 text_yank(begin_line(dot), end_line(dot), Ureg);
772 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000773#endif
774#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000775 // These are commands that change text[].
776 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000777 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000778 && cmd_mode == 0 // command mode
779 && c > '\0' // exclude NUL and non-ASCII chars
780 && c < 0x7f // (Unicode and such)
781 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000782 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000783 start_new_cmd_q(c);
784 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000785#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000786 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000787
Eric Andersen3f980402001-04-04 17:31:15 +0000788 // poll to see if there is input already waiting. if we are
789 // not able to display output fast enough to keep up, skip
790 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200791 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000792 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000793 refresh(FALSE);
794 show_status_line();
795 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000796#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000797 if (crashme > 0)
798 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000799#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000800 }
801 //-------------------------------------------------------------------
802
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000803 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000804 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000805#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000806}
807
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000808//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000809#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000810static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000811{
812 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000813 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000814 IF_FEATURE_VI_YANKMARK(char c;)
815 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000816
817 *addr = -1; // assume no addr
818 if (*p == '.') { // the current line
819 p++;
820 q = begin_line(dot);
821 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000822 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000823#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000824 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000825 p++;
826 c = tolower(*p);
827 p++;
828 if (c >= 'a' && c <= 'z') {
829 // we have a mark
830 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000831 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000832 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000833 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000834 }
835 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000836 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000837#endif
838#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000839 else if (*p == '/') { // a search pattern
840 q = strchrnul(++p, '/');
841 pat = xstrndup(p, q - p); // save copy of pattern
842 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000843 if (*p == '/')
844 p++;
845 q = char_search(dot, pat, FORWARD, FULL);
846 if (q != NULL) {
847 *addr = count_lines(text, q);
848 }
849 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000850 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000851#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000852 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000853 p++;
854 q = begin_line(end - 1);
855 *addr = count_lines(text, q);
856 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000857 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000858 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000859 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200860 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000861 *addr = -1;
862 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000863 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000864}
865
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000866static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000867{
868 //----- get the address' i.e., 1,3 'a,'b -----
869 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000870 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000871 p++; // skip over leading spaces
872 if (*p == '%') { // alias for 1,$
873 p++;
874 *b = 1;
875 *e = count_lines(text, end-1);
876 goto ga0;
877 }
878 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000879 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000880 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000881 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000882 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000883 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000884 p++;
885 // get SECOND addr, if present
886 p = get_one_address(p, e);
887 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000888 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000889 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000890 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000891 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000892}
893
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000894#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000895static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000896 const char *short_opname, int opt)
897{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000898 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000899 int l = strlen(opname) - 1; /* opname have + ' ' */
900
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200901 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000902 if (strncasecmp(a, opname, l) == 0
903 || strncasecmp(a, short_opname, 2) == 0
904 ) {
905 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000906 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000907 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000908 vi_setops |= opt;
909 }
910}
911#endif
912
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000913// buf must be no longer than MAX_INPUT_LEN!
914static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000915{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000916 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000917 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000918 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000919 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000920
921 // :3154 // if (-e line 3154) goto it else stay put
922 // :4,33w! foo // write a portion of buffer to file "foo"
923 // :w // write all of buffer to current file
924 // :q // quit
925 // :q! // quit- dont care about modified file
926 // :'a,'z!sort -u // filter block through sort
927 // :'f // goto mark "f"
928 // :'fl // list literal the mark "f" line
929 // :.r bar // read file "bar" into buffer before dot
930 // :/123/,/abc/d // delete lines from "123" line to "abc" line
931 // :/xyz/ // goto the "xyz" line
932 // :s/find/replace/ // substitute pattern "find" with "replace"
933 // :!<cmd> // run <cmd> then return
934 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000935
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000936 if (!buf[0])
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200937 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000938 if (*buf == ':')
939 buf++; // move past the ':'
940
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000941 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000942 b = e = -1;
943 q = text; // assume 1,$ for the range
944 r = end - 1;
945 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000946 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000947
948 // look for optional address(es) :. :1 :1,9 :'q,'a :%
949 buf = get_address(buf, &b, &e);
950
951 // remember orig command line
952 orig_buf = buf;
953
954 // get the COMMAND into cmd[]
955 buf1 = cmd;
956 while (*buf != '\0') {
957 if (isspace(*buf))
958 break;
959 *buf1++ = *buf++;
960 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000961 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000962 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000963 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000964 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000965 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000966 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000967 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000968 if (buf1) {
969 useforce = TRUE;
970 *buf1 = '\0'; // get rid of !
971 }
972 if (b >= 0) {
973 // if there is only one addr, then the addr
974 // is the line number of the single line the
975 // user wants. So, reset the end
976 // pointer to point at end of the "b" line
977 q = find_line(b); // what line is #b
978 r = end_line(q);
979 li = 1;
980 }
981 if (e >= 0) {
982 // we were given two addrs. change the
983 // end pointer to the addr given by user.
984 r = find_line(e); // what line is #e
985 r = end_line(r);
986 li = e - b + 1;
987 }
988 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000989 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000990 if (i == 0) { // :123CR goto line #123
991 if (b >= 0) {
992 dot = find_line(b); // what line is #b
993 dot_skip_over_ws();
994 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000995 }
996#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200997 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000998 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000999 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001000 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001001 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001002 retcode = system(orig_buf + 1); // run the cmd
1003 if (retcode)
1004 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001005 rawmode();
1006 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +00001007 }
1008#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001009 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001010 if (b < 0) { // no addr given- use defaults
1011 b = e = count_lines(text, dot);
1012 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001013 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001014 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001015 if (b < 0) { // no addr given- use defaults
1016 q = begin_line(dot); // assume .,. for the range
1017 r = end_line(dot);
1018 }
1019 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
1020 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001021 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001022 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001023 if (file_modified && !useforce) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001024 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001025 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001026 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001027 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001028 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001029 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001030 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001031 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001032 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001033 } else {
1034 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001035 status_line_bold("No current filename");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001036 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001037 }
1038
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001039 if (init_text_buffer(fn) < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001040 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001041
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001042#if ENABLE_FEATURE_VI_YANKMARK
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001043 if (Ureg >= 0 && Ureg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001044 free(reg[Ureg]); // free orig line reg- for 'U'
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001045 reg[Ureg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001046 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001047 if (YDreg >= 0 && YDreg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001048 free(reg[YDreg]); // free default yank/delete register
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001049 reg[YDreg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001050 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001051#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001052 // how many lines in text[]?
1053 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001054 status_line("\"%s\"%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001055 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001056 " %dL, %dC", current_filename,
1057 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001058 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001059 ((readonly_mode) ? " [Readonly]" : ""),
1060 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001061 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001062 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001063 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01001064 status_line_bold("No address allowed on this command");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001065 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001066 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001067 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001068 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001069 free(current_filename);
1070 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001071 } else {
1072 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +00001073 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001074 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001075 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001076 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001077 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001078 cookmode();
1079 show_help();
1080 rawmode();
1081 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001082 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001083 if (b < 0) { // no addr given- use defaults
1084 q = begin_line(dot); // assume .,. for the range
1085 r = end_line(dot);
1086 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001087 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001088 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001089 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001090 int c_is_no_print;
1091
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001092 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +00001093 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001094 if (c_is_no_print) {
1095 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001096 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +00001097 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001098 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001099 write1("$\r");
1100 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001101 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001102 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001103 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001104 else
1105 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001106 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001107 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001108 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001109 standout_end();
1110 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001111 Hit_Return();
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001112 } else if (strncmp(cmd, "quit", i) == 0 // quit
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001113 || strncmp(cmd, "next", i) == 0 // edit next file
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001114 || strncmp(cmd, "prev", i) == 0 // edit previous file
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001115 ) {
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001116 int n;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001117 if (useforce) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001118 if (*cmd == 'q') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001119 // force end of argv list
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001120 optind = save_argc;
1121 }
1122 editing = 0;
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001123 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001124 }
1125 // don't exit if the file been modified
1126 if (file_modified) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001127 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001128 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001129 }
1130 // are there other file to edit
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001131 n = save_argc - optind - 1;
1132 if (*cmd == 'q' && n > 0) {
1133 status_line_bold("%d more file(s) to edit", n);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001134 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001135 }
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001136 if (*cmd == 'n' && n <= 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001137 status_line_bold("No more files to edit");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001138 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001139 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001140 if (*cmd == 'p') {
1141 // are there previous files to edit
1142 if (optind < 1) {
1143 status_line_bold("No previous files to edit");
1144 goto ret;
1145 }
1146 optind -= 2;
1147 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001148 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001149 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001150 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001151 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001152 status_line_bold("No filename given");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001153 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001154 }
1155 if (b < 0) { // no addr given- use defaults
1156 q = begin_line(dot); // assume "dot"
1157 }
1158 // read after current line- unless user said ":0r foo"
1159 if (b != 0)
1160 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001161 { // dance around potentially-reallocated text[]
1162 uintptr_t ofs = q - text;
1163 ch = file_insert(fn, q, 0);
1164 q = text + ofs;
1165 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001166 if (ch < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001167 goto ret; // nothing was inserted
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001168 // how many lines in text[]?
1169 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001170 status_line("\"%s\""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001171 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001172 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001173 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001174 li, ch);
1175 if (ch > 0) {
1176 // if the insert is before "dot" then we need to update
1177 if (q <= dot)
1178 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001179 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001180 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001181 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001182 if (file_modified && !useforce) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001183 status_line_bold("No write since last change (:%s! overrides)", cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001184 } else {
1185 // reset the filenames to edit
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001186 optind = -1; /* start from 0th file */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001187 editing = 0;
1188 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001189#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001190 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001191#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001192 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001193#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001194 i = 0; // offset into args
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02001195 // only blank is regarded as args delimiter. What about tab '\t'?
Denis Vlasenkof9234132007-03-21 00:03:42 +00001196 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001197 // print out values of all options
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001198#if ENABLE_FEATURE_VI_SETOPTS
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001199 status_line_bold(
1200 "%sautoindent "
1201 "%sflash "
1202 "%signorecase "
1203 "%sshowmatch "
1204 "tabstop=%u",
1205 autoindent ? "" : "no",
1206 err_method ? "" : "no",
1207 ignorecase ? "" : "no",
1208 showmatch ? "" : "no",
1209 tabstop
1210 );
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001211#endif
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001212 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001213 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001214#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001215 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001216 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001217 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001218 i = 2; // ":set noautoindent"
1219 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001220 setops(argp, "flash " , i, "fl", VI_ERR_METHOD);
Denis Vlasenkof9234132007-03-21 00:03:42 +00001221 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001222 setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH );
1223 if (strncmp(argp + i, "tabstop=", 8) == 0) {
1224 int t = 0;
1225 sscanf(argp + i+8, "%u", &t);
1226 if (t > 0 && t <= MAX_TABSTOP)
1227 tabstop = t;
Denis Vlasenkof9234132007-03-21 00:03:42 +00001228 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001229 argp = skip_non_whitespace(argp);
1230 argp = skip_whitespace(argp);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001231 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001232#endif /* FEATURE_VI_SETOPTS */
1233#endif /* FEATURE_VI_SET */
1234#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001235 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001236 char *F, *R, *flags;
1237 size_t len_F, len_R;
1238 int gflag; // global replace flag
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001239
1240 // F points to the "find" pattern
1241 // R points to the "replace" pattern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001242 // replace the cmd line delimiters "/" with NULs
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001243 c = orig_buf[1]; // what is the delimiter
1244 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001245 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001246 if (!R)
1247 goto colon_s_fail;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001248 len_F = R - F;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001249 *R++ = '\0'; // terminate "find"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001250 flags = strchr(R, c);
1251 if (!flags)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001252 goto colon_s_fail;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001253 len_R = flags - R;
1254 *flags++ = '\0'; // terminate "replace"
1255 gflag = *flags;
1256
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001257 q = begin_line(q);
1258 if (b < 0) { // maybe :s/foo/bar/
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001259 q = begin_line(dot); // start with cur line
1260 b = count_lines(text, q); // cur line number
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001261 }
1262 if (e < 0)
1263 e = b; // maybe :.s/foo/bar/
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001264
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001265 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001266 char *ls = q; // orig line start
1267 char *found;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001268 vc4:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001269 found = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
1270 if (found) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001271 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001272 // we found the "find" pattern - delete it
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001273 text_hole_delete(found, found + len_F - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001274 // inset the "replace" patern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001275 bias = string_insert(found, R); // insert the string
1276 found += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001277 ls += bias;
1278 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001279 // check for "global" :s/foo/bar/g
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001280 if (gflag == 'g') {
1281 if ((found + len_R) < end_line(ls)) {
1282 q = found + len_R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001283 goto vc4; // don't let q move past cur line
1284 }
1285 }
1286 }
1287 q = next_line(ls);
1288 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001289#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001290 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001291 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001292 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1293 || strncmp(cmd, "wq", i) == 0
1294 || strncmp(cmd, "wn", i) == 0
1295 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001296 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001297 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001298 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001299 fn = args;
1300 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001301#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001302 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001303 status_line_bold("\"%s\" File is read only", fn);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001304 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001305 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001306#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001307 // how many lines in text[]?
1308 li = count_lines(q, r);
1309 ch = r - q + 1;
1310 // see if file exists- if not, its just a new file request
1311 if (useforce) {
1312 // if "fn" is not write-able, chmod u+w
1313 // sprintf(syscmd, "chmod u+w %s", fn);
1314 // system(syscmd);
1315 forced = TRUE;
1316 }
1317 l = file_write(fn, q, r);
1318 if (useforce && forced) {
1319 // chmod u-w
1320 // sprintf(syscmd, "chmod u-w %s", fn);
1321 // system(syscmd);
1322 forced = FALSE;
1323 }
Paul Fox61e45db2005-10-09 14:43:22 +00001324 if (l < 0) {
1325 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001326 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001327 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001328 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001329 if (q == text && r == end - 1 && l == ch) {
1330 file_modified = 0;
1331 last_file_modified = -1;
1332 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001333 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1334 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1335 )
1336 && l == ch
1337 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001338 editing = 0;
1339 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001340 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001341#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001342 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001343 if (b < 0) { // no addr given- use defaults
1344 q = begin_line(dot); // assume .,. for the range
1345 r = end_line(dot);
1346 }
1347 text_yank(q, r, YDreg);
1348 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001349 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001350 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001351#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001352 } else {
1353 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001354 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001355 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001356 ret:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001357 dot = bound_dot(dot); // make sure "dot" is valid
1358 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001359#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001360 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001361 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001362#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001363}
Paul Fox61e45db2005-10-09 14:43:22 +00001364
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001365#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001366
1367static void Hit_Return(void)
1368{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001369 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001370
Denis Vlasenkof882f082007-12-23 02:36:51 +00001371 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001372 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001373 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001374 while ((c = get_one_char()) != '\n' && c != '\r')
1375 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001376 redraw(TRUE); // force redraw all
1377}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001378
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001379static int next_tabstop(int col)
1380{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001381 return col + ((tabstop - 1) - (col % tabstop));
1382}
1383
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001384//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001385static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001386{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001387 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001388 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001389 int cnt, ro, co;
1390
1391 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001392
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001393 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001394 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001395 // how many lines do we have to move
1396 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001397 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001398 screenbegin = beg_cur;
1399 if (cnt > (rows - 1) / 2) {
1400 // we moved too many lines. put "dot" in middle of screen
1401 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1402 screenbegin = prev_line(screenbegin);
1403 }
1404 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001405 } else {
1406 char *end_scr; // begin and end of screen
1407 end_scr = end_screen(); // last char of screen
1408 if (beg_cur > end_scr) {
1409 // "d" is after bottom line on screen
1410 // how many lines do we have to move
1411 cnt = count_lines(end_scr, beg_cur);
1412 if (cnt > (rows - 1) / 2)
1413 goto sc1; // too many lines
1414 for (ro = 0; ro < cnt - 1; ro++) {
1415 // move screen begin the same amount
1416 screenbegin = next_line(screenbegin);
1417 // now, move the end of screen
1418 end_scr = next_line(end_scr);
1419 end_scr = end_line(end_scr);
1420 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001421 }
1422 }
1423 // "d" is on screen- find out which row
1424 tp = screenbegin;
1425 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1426 if (tp == beg_cur)
1427 break;
1428 tp = next_line(tp);
1429 }
1430
1431 // find out what col "d" is on
1432 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001433 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001434 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001435 break;
1436 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001437 // handle tabs like real vi
1438 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001439 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001440 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001441 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001442 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1443 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001444 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001445 co++;
1446 tp++;
1447 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001448
1449 // "co" is the column where "dot" is.
1450 // The screen has "columns" columns.
1451 // The currently displayed columns are 0+offset -- columns+ofset
1452 // |-------------------------------------------------------------|
1453 // ^ ^ ^
1454 // offset | |------- columns ----------------|
1455 //
1456 // If "co" is already in this range then we do not have to adjust offset
1457 // but, we do have to subtract the "offset" bias from "co".
1458 // If "co" is outside this range then we have to change "offset".
1459 // If the first char of a line is a tab the cursor will try to stay
1460 // in column 7, but we have to set offset to 0.
1461
1462 if (co < 0 + offset) {
1463 offset = co;
1464 }
1465 if (co >= columns + offset) {
1466 offset = co - columns + 1;
1467 }
1468 // if the first char of the line is a tab, and "dot" is sitting on it
1469 // force offset to 0.
1470 if (d == beg_cur && *d == '\t') {
1471 offset = 0;
1472 }
1473 co -= offset;
1474
1475 *row = ro;
1476 *col = co;
1477}
1478
1479//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001480static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001481{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001482 if (p > text) {
1483 p = memrchr(text, '\n', p - text);
1484 if (!p)
1485 return text;
1486 return p + 1;
1487 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001488 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001489}
1490
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001491static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001492{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001493 if (p < end - 1) {
1494 p = memchr(p, '\n', end - p - 1);
1495 if (!p)
1496 return end - 1;
1497 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001498 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001499}
1500
Denis Vlasenkof882f082007-12-23 02:36:51 +00001501static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001502{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001503 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001504 // Try to stay off of the Newline
1505 if (*p == '\n' && (p - begin_line(p)) > 0)
1506 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001507 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001508}
1509
Denis Vlasenkof882f082007-12-23 02:36:51 +00001510static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001511{
1512 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001513 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001514 p--; // step to prev line
1515 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001516 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001517}
1518
Denis Vlasenkof882f082007-12-23 02:36:51 +00001519static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001520{
1521 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001522 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001523 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001524 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001525}
1526
1527//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001528static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001529{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001530 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001531 int cnt;
1532
1533 // find new bottom line
1534 q = screenbegin;
1535 for (cnt = 0; cnt < rows - 2; cnt++)
1536 q = next_line(q);
1537 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001538 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001539}
1540
Denis Vlasenkof882f082007-12-23 02:36:51 +00001541// count line from start to stop
1542static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001543{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001544 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001545 int cnt;
1546
Denis Vlasenkof882f082007-12-23 02:36:51 +00001547 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001548 q = start;
1549 start = stop;
1550 stop = q;
1551 }
1552 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001553 stop = end_line(stop);
1554 while (start <= stop && start <= end - 1) {
1555 start = end_line(start);
1556 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001557 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001558 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001559 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001560 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001561}
1562
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001563static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001564{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001565 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001566
1567 for (q = text; li > 1; li--) {
1568 q = next_line(q);
1569 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001570 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001571}
1572
1573//----- Dot Movement Routines ----------------------------------
1574static void dot_left(void)
1575{
1576 if (dot > text && dot[-1] != '\n')
1577 dot--;
1578}
1579
1580static void dot_right(void)
1581{
1582 if (dot < end - 1 && *dot != '\n')
1583 dot++;
1584}
1585
1586static void dot_begin(void)
1587{
1588 dot = begin_line(dot); // return pointer to first char cur line
1589}
1590
1591static void dot_end(void)
1592{
1593 dot = end_line(dot); // return pointer to last char cur line
1594}
1595
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001596static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001597{
1598 int co;
1599
1600 p = begin_line(p);
1601 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001602 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001603 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001604 break;
1605 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001606 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001607 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001608 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001609 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001610 co++;
1611 p++;
1612 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001613 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001614}
1615
1616static void dot_next(void)
1617{
1618 dot = next_line(dot);
1619}
1620
1621static void dot_prev(void)
1622{
1623 dot = prev_line(dot);
1624}
1625
1626static void dot_scroll(int cnt, int dir)
1627{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001628 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001629
1630 for (; cnt > 0; cnt--) {
1631 if (dir < 0) {
1632 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001633 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001634 screenbegin = prev_line(screenbegin);
1635 } else {
1636 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001637 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001638 screenbegin = next_line(screenbegin);
1639 }
1640 }
1641 // make sure "dot" stays on the screen so we dont scroll off
1642 if (dot < screenbegin)
1643 dot = screenbegin;
1644 q = end_screen(); // find new bottom line
1645 if (dot > q)
1646 dot = begin_line(q); // is dot is below bottom line?
1647 dot_skip_over_ws();
1648}
1649
1650static void dot_skip_over_ws(void)
1651{
1652 // skip WS
1653 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1654 dot++;
1655}
1656
1657static void dot_delete(void) // delete the char at 'dot'
1658{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001659 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001660}
1661
Denis Vlasenkof882f082007-12-23 02:36:51 +00001662static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001663{
1664 if (p >= end && end > text) {
1665 p = end - 1;
1666 indicate_error('1');
1667 }
1668 if (p < text) {
1669 p = text;
1670 indicate_error('2');
1671 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001672 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001673}
1674
1675//----- Helper Utility Routines --------------------------------
1676
1677//----------------------------------------------------------------
1678//----- Char Routines --------------------------------------------
1679/* Chars that are part of a word-
1680 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1681 * Chars that are Not part of a word (stoppers)
1682 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1683 * Chars that are WhiteSpace
1684 * TAB NEWLINE VT FF RETURN SPACE
1685 * DO NOT COUNT NEWLINE AS WHITESPACE
1686 */
1687
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001688static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001689{
1690 int li;
1691
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001692 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001693 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001694 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001695 // initialize the new screen. assume this will be a empty file.
1696 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001697 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001698 for (li = 1; li < ro - 1; li++) {
1699 screen[(li * co) + 0] = '~';
1700 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001701 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001702}
1703
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001704#if ENABLE_FEATURE_VI_SEARCH
Walter Harmsb9ba5802011-06-27 02:59:37 +02001705
1706# if ENABLE_FEATURE_VI_REGEX_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001707
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001708// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001709static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001710{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001711 char *q;
1712 struct re_pattern_buffer preg;
1713 int i;
Walter Harmsb9ba5802011-06-27 02:59:37 +02001714 int size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001715
1716 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1717 preg.translate = 0;
1718 preg.fastmap = 0;
1719 preg.buffer = 0;
1720 preg.allocated = 0;
1721
1722 // assume a LIMITED forward search
1723 q = next_line(p);
1724 q = end_line(q);
1725 q = end - 1;
1726 if (dir == BACK) {
1727 q = prev_line(p);
1728 q = text;
1729 }
1730 // count the number of chars to search over, forward or backward
1731 size = q - p;
1732 if (size < 0)
1733 size = p - q;
1734 // RANGE could be negative if we are searching backwards
1735 range = q - p;
1736
Walter Harmsb9ba5802011-06-27 02:59:37 +02001737 q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001738 if (q != 0) {
1739 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001740 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001741 i = 0; // return p if pattern not compiled
1742 goto cs1;
1743 }
1744
1745 q = p;
1746 if (range < 0) {
1747 q = p - size;
1748 if (q < text)
1749 q = text;
1750 }
1751 // search for the compiled pattern, preg, in p[]
1752 // range < 0- search backward
1753 // range > 0- search forward
1754 // 0 < start < size
1755 // re_search() < 0 not found or error
1756 // re_search() > 0 index of found pattern
1757 // struct pattern char int int int struct reg
1758 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1759 i = re_search(&preg, q, size, 0, range, 0);
1760 if (i == -1) {
1761 p = 0;
1762 i = 0; // return NULL if pattern not found
1763 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001764 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001765 if (dir == FORWARD) {
1766 p = p + i;
1767 } else {
1768 p = p - i;
1769 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001770 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001771}
Walter Harmsb9ba5802011-06-27 02:59:37 +02001772
1773# else
1774
1775# if ENABLE_FEATURE_VI_SETOPTS
1776static int mycmp(const char *s1, const char *s2, int len)
1777{
1778 if (ignorecase) {
1779 return strncasecmp(s1, s2, len);
1780 }
1781 return strncmp(s1, s2, len);
1782}
1783# else
1784# define mycmp strncmp
1785# endif
1786
1787static char *char_search(char *p, const char *pat, int dir, int range)
1788{
1789 char *start, *stop;
1790 int len;
1791
1792 len = strlen(pat);
1793 if (dir == FORWARD) {
1794 stop = end - 1; // assume range is p - end-1
1795 if (range == LIMITED)
1796 stop = next_line(p); // range is to next line
1797 for (start = p; start < stop; start++) {
1798 if (mycmp(start, pat, len) == 0) {
1799 return start;
1800 }
1801 }
1802 } else if (dir == BACK) {
1803 stop = text; // assume range is text - p
1804 if (range == LIMITED)
1805 stop = prev_line(p); // range is to prev line
1806 for (start = p - len; start >= stop; start--) {
1807 if (mycmp(start, pat, len) == 0) {
1808 return start;
1809 }
1810 }
1811 }
1812 // pattern not found
1813 return NULL;
1814}
1815
1816# endif
1817
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001818#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001819
Denis Vlasenko33875382008-06-21 20:31:50 +00001820static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001821{
1822 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001823 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001824 refresh(FALSE); // show the ^
1825 c = get_one_char();
1826 *p = c;
1827 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001828 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001829 } else if (c == 27) { // Is this an ESC?
1830 cmd_mode = 0;
1831 cmdcnt = 0;
1832 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001833 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001834 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001835 p--;
1836 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001837 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001838 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001839 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001840 p--;
1841 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001842 }
1843 } else {
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001844#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001845 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001846 char *sp; // "save p"
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001847#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001848
1849 if (c == 13)
1850 c = '\n'; // translate \r to \n
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001851#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001852 sp = p; // remember addr of insert
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001853#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001854 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001855#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001856 if (showmatch && strchr(")]}", *sp) != NULL) {
1857 showmatching(sp);
1858 }
1859 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001860 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001861 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001862 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001863 len = strspn(q, " \t"); // space or tab
1864 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001865 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001866 bias = text_hole_make(p, len);
1867 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001868 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001869 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001870 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001871 }
1872 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001873#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001874 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001875 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001876}
1877
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001878// might reallocate text[]! use p += stupid_insert(p, ...),
1879// and be careful to not use pointers into potentially freed text[]!
1880static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001881{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001882 uintptr_t bias;
1883 bias = text_hole_make(p, 1);
1884 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001885 *p = c;
1886 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001887 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001888}
1889
Denis Vlasenko33875382008-06-21 20:31:50 +00001890static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001891{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001892 char *save_dot, *p, *q, *t;
1893 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001894
1895 save_dot = dot;
1896 p = q = dot;
1897
1898 if (strchr("cdy><", c)) {
1899 // these cmds operate on whole lines
1900 p = q = begin_line(p);
1901 for (cnt = 1; cnt < cmdcnt; cnt++) {
1902 q = next_line(q);
1903 }
1904 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001905 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001906 // These cmds operate on char positions
1907 do_cmd(c); // execute movement cmd
1908 q = dot;
1909 } else if (strchr("wW", c)) {
1910 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001911 // if we are at the next word's first char
1912 // step back one char
1913 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001914 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001915 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1916 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1917 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001918 if (dot > text && *dot == '\n')
1919 dot--; // stay off NL
1920 q = dot;
1921 } else if (strchr("H-k{", c)) {
1922 // these operate on multi-lines backwards
1923 q = end_line(dot); // find NL
1924 do_cmd(c); // execute movement cmd
1925 dot_begin();
1926 p = dot;
1927 } else if (strchr("L+j}\r\n", c)) {
1928 // these operate on multi-lines forwards
1929 p = begin_line(dot);
1930 do_cmd(c); // execute movement cmd
1931 dot_end(); // find NL
1932 q = dot;
1933 } else {
Tanguy Pruvot823694d2012-11-18 13:20:29 +01001934 // nothing -- this causes any other values of c to
1935 // represent the one-character range under the
1936 // cursor. this is correct for ' ' and 'l', but
1937 // perhaps no others.
1938 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001939 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001940 if (q < p) {
1941 t = q;
1942 q = p;
1943 p = t;
1944 }
1945
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001946 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001947 if (q > p && strchr("^0bBh\b\177", c)) q--;
1948
1949 multiline = 0;
1950 for (t = p; t <= q; t++) {
1951 if (*t == '\n') {
1952 multiline = 1;
1953 break;
1954 }
1955 }
1956
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001957 *start = p;
1958 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001959 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001960 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001961}
1962
Denis Vlasenko33875382008-06-21 20:31:50 +00001963static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001964{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001965 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001966 int test, inc;
1967
1968 inc = dir;
1969 c = c0 = p[0];
1970 ci = p[inc];
1971 test = 0;
1972
1973 if (type == S_BEFORE_WS) {
1974 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001975 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001976 }
1977 if (type == S_TO_WS) {
1978 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001979 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001980 }
1981 if (type == S_OVER_WS) {
1982 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001983 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001984 }
1985 if (type == S_END_PUNCT) {
1986 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001987 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001988 }
1989 if (type == S_END_ALNUM) {
1990 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001991 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001992 }
1993 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001994 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001995}
1996
Denis Vlasenko33875382008-06-21 20:31:50 +00001997static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001998{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001999 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002000
2001 while (st_test(p, type, dir, &c)) {
2002 // make sure we limit search to correct number of lines
2003 if (c == '\n' && --linecnt < 1)
2004 break;
2005 if (dir >= 0 && p >= end - 1)
2006 break;
2007 if (dir < 0 && p <= text)
2008 break;
2009 p += dir; // move to next char
2010 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002011 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002012}
2013
2014// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00002015static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002016{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002017 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002018 int dir, level;
2019
2020 match = ')';
2021 level = 1;
2022 dir = 1; // assume forward
2023 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002024 case '(': match = ')'; break;
2025 case '[': match = ']'; break;
2026 case '{': match = '}'; break;
2027 case ')': match = '('; dir = -1; break;
2028 case ']': match = '['; dir = -1; break;
2029 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002030 }
2031 for (q = p + dir; text <= q && q < end; q += dir) {
2032 // look for match, count levels of pairs (( ))
2033 if (*q == c)
2034 level++; // increase pair levels
2035 if (*q == match)
2036 level--; // reduce pair level
2037 if (level == 0)
2038 break; // found matching pair
2039 }
2040 if (level != 0)
2041 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002042 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002043}
2044
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002045#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002046// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00002047static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002048{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002049 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002050
2051 // we found half of a pair
2052 q = find_pair(p, *p); // get loc of matching char
2053 if (q == NULL) {
2054 indicate_error('3'); // no matching char
2055 } else {
2056 // "q" now points to matching pair
2057 save_dot = dot; // remember where we are
2058 dot = q; // go to new loc
2059 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002060 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002061 dot = save_dot; // go back to old loc
2062 refresh(FALSE);
2063 }
2064}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002065#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002066
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002067// open a hole in text[]
2068// might reallocate text[]! use p += text_hole_make(p, ...),
2069// and be careful to not use pointers into potentially freed text[]!
2070static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002071{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002072 uintptr_t bias = 0;
2073
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002074 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002075 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002076 end += size; // adjust the new END
2077 if (end >= (text + text_size)) {
2078 char *new_text;
2079 text_size += end - (text + text_size) + 10240;
2080 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002081 bias = (new_text - text);
2082 screenbegin += bias;
2083 dot += bias;
2084 end += bias;
2085 p += bias;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002086#if ENABLE_FEATURE_VI_YANKMARK
2087 {
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +02002088 unsigned i;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002089 for (i = 0; i < ARRAY_SIZE(mark); i++)
2090 if (mark[i])
2091 mark[i] += bias;
2092 }
2093#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +00002094 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002095 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00002096 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002097 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00002098 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002099 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002100}
2101
2102// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00002103static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002104{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002105 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002106 int cnt, hole_size;
2107
2108 // move forwards, from beginning
2109 // assume p <= q
2110 src = q + 1;
2111 dest = p;
2112 if (q < p) { // they are backward- swap them
2113 src = p + 1;
2114 dest = q;
2115 }
2116 hole_size = q - p + 1;
2117 cnt = end - src;
2118 if (src < text || src > end)
2119 goto thd0;
2120 if (dest < text || dest >= end)
2121 goto thd0;
2122 if (src >= end)
2123 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00002124 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002125 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002126 end = end - hole_size; // adjust the new END
2127 if (dest >= end)
2128 dest = end - 1; // make sure dest in below end-1
2129 if (end <= text)
2130 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00002131 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002132 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002133 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002134}
2135
2136// copy text into register, then delete text.
2137// if dist <= 0, do not include, or go past, a NewLine
2138//
Denis Vlasenko33875382008-06-21 20:31:50 +00002139static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002140{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002141 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002142
2143 // make sure start <= stop
2144 if (start > stop) {
2145 // they are backwards, reverse them
2146 p = start;
2147 start = stop;
2148 stop = p;
2149 }
2150 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00002151 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002152 p = start;
2153 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002154 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002155 // dont go past a NewLine
2156 for (; p + 1 <= stop; p++) {
2157 if (p[1] == '\n') {
2158 stop = p; // "stop" just before NewLine
2159 break;
2160 }
2161 }
2162 }
2163 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002164#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002165 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002166#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002167 if (yf == YANKDEL) {
2168 p = text_hole_delete(start, stop);
2169 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002170 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002171}
2172
2173static void show_help(void)
2174{
2175 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002176#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002177 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002178#endif
2179#if ENABLE_FEATURE_VI_DOT_CMD
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02002180 "\n\tLast command repeat with ."
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002181#endif
2182#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00002183 "\n\tLine marking with 'x"
2184 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002185#endif
2186#if ENABLE_FEATURE_VI_READONLY
Walter Harmsb9ba5802011-06-27 02:59:37 +02002187 //not implemented: "\n\tReadonly if vi is called as \"view\""
2188 //redundant: usage text says this too: "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002189#endif
2190#if ENABLE_FEATURE_VI_SET
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02002191 "\n\tSome colon mode commands with :"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002192#endif
2193#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002194 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002195#endif
2196#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002197 "\n\tSignal catching- ^C"
2198 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002199#endif
2200#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002201 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002202#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002203 );
2204}
2205
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002206#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002207static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002208{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002209 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002210 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002211 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002212 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002213 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002214 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002215 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002216 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002217 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002218}
2219
2220static void end_cmd_q(void)
2221{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002222#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002223 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002224#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002225 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002226}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002227#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002228
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002229#if ENABLE_FEATURE_VI_YANKMARK \
2230 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2231 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002232// might reallocate text[]! use p += string_insert(p, ...),
2233// and be careful to not use pointers into potentially freed text[]!
2234static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002235{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002236 uintptr_t bias;
2237 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002238
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002239 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002240 bias = text_hole_make(p, i);
2241 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002242 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002243#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002244 {
2245 int cnt;
2246 for (cnt = 0; *s != '\0'; s++) {
2247 if (*s == '\n')
2248 cnt++;
2249 }
2250 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2251 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002252#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002253 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002254}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002255#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002256
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002257#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002258static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002259{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002260 int cnt = q - p;
2261 if (cnt < 0) { // they are backwards- reverse them
2262 p = q;
2263 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002264 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002265 free(reg[dest]); // if already a yank register, free it
2266 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002267 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002268}
2269
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002270static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002271{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002272 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002273
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002274 c = 'D'; // default to D-reg
2275 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002276 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002277 if (YDreg == 26)
2278 c = 'D';
2279 if (YDreg == 27)
2280 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002281 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002282}
2283
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002284static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002285{
2286 // A context is defined to be "modifying text"
2287 // Any modifying command establishes a new context.
2288
2289 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002290 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002291 // we are trying to modify text[]- make this the current context
2292 mark[27] = mark[26]; // move cur to prev
2293 mark[26] = dot; // move local to cur
2294 context_start = prev_line(prev_line(dot));
2295 context_end = next_line(next_line(dot));
2296 //loiter= start_loiter= now;
2297 }
2298 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002299}
2300
Denis Vlasenkof882f082007-12-23 02:36:51 +00002301static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002302{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002303 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002304
2305 // the current context is in mark[26]
2306 // the previous context is in mark[27]
2307 // only swap context if other context is valid
2308 if (text <= mark[27] && mark[27] <= end - 1) {
2309 tmp = mark[27];
2310 mark[27] = mark[26];
2311 mark[26] = tmp;
2312 p = mark[26]; // where we are going- previous context
2313 context_start = prev_line(prev_line(prev_line(p)));
2314 context_end = next_line(next_line(next_line(p)));
2315 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002316 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002317}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002318#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002319
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002320//----- Set terminal attributes --------------------------------
2321static void rawmode(void)
2322{
2323 tcgetattr(0, &term_orig);
2324 term_vi = term_orig;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002325 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG on - allow intr's
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002326 term_vi.c_iflag &= (~IXON & ~ICRNL);
2327 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002328 term_vi.c_cc[VMIN] = 1;
2329 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002330 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002331 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002332}
2333
2334static void cookmode(void)
2335{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002336 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002337 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002338}
2339
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002340#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002341//----- Come here when we get a window resize signal ---------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002342static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002343{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002344 int save_errno = errno;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002345 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002346 signal(SIGWINCH, winch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002347 query_screen_dimensions();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002348 new_screen(rows, columns); // get memory for virtual screen
2349 redraw(TRUE); // re-draw the screen
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002350 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002351}
2352
2353//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002354static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002355{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002356 int save_errno = errno;
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002357 rawmode(); // terminal to "raw"
2358 last_status_cksum = 0; // force status update
2359 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002360
2361 signal(SIGTSTP, suspend_sig);
2362 signal(SIGCONT, SIG_DFL);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002363 //kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
2364 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002365}
2366
2367//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002368static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002369{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002370 int save_errno = errno;
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002371 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002372 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002373
2374 signal(SIGCONT, cont_sig);
2375 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002376 kill(my_pid, SIGTSTP);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002377 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002378}
2379
2380//----- Come here when we get a signal ---------------------------
2381static void catch_sig(int sig)
2382{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002383 signal(SIGINT, catch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002384 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002385}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002386#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002387
Denys Vlasenko606291b2009-09-23 23:15:43 +02002388static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002389{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002390 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002391
Denys Vlasenko606291b2009-09-23 23:15:43 +02002392 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002393 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002394 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002395}
2396
2397//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002398static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002399{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002400 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002401
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002402 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002403 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002404 if (c == -1) { // EOF/error
2405 go_bottom_and_clear_to_eol();
2406 cookmode(); // terminal to "cooked"
2407 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002408 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002409 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002410}
2411
2412//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002413static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002414{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002415 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002416
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002417#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002418 if (!adding2q) {
2419 // we are not adding to the q.
2420 // but, we may be reading from a q
2421 if (ioq == 0) {
2422 // there is no current q, read from STDIN
2423 c = readit(); // get the users input
2424 } else {
2425 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002426 // careful with correct sign expansion!
2427 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002428 if (c == '\0') {
2429 // the end of the q, read from STDIN
2430 free(ioq_start);
2431 ioq_start = ioq = 0;
2432 c = readit(); // get the users input
2433 }
2434 }
2435 } else {
2436 // adding STDIN chars to q
2437 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002438 if (lmc_len >= MAX_INPUT_LEN - 1) {
2439 status_line_bold("last_modifying_cmd overrun");
2440 } else {
2441 // add new char to q
2442 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002443 }
2444 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002445#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002446 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002447#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002448 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002449}
2450
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002451// Get input line (uses "status line" area)
2452static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002453{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002454 // char [MAX_INPUT_LEN]
2455#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002456
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002457 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002458 int i;
2459
2460 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002461 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002462 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002463 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002464
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002465 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002466 while (i < MAX_INPUT_LEN) {
2467 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002468 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002469 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002470 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002471 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002472 buf[--i] = '\0';
2473 write1("\b \b"); // erase char on screen
2474 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002475 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002476 } else if (c > 0 && c < 256) { // exclude Unicode
2477 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002478 buf[i] = c;
2479 buf[++i] = '\0';
2480 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002481 }
2482 }
2483 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002484 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002485#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002486}
2487
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002488static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002489{
2490 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002491 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002492
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002493 cnt = -1;
Denys Vlasenkodef47832010-04-18 20:39:41 -07002494 if (fn && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002495 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002496 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002497}
2498
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002499// might reallocate text[]!
2500static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002501{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002502 int cnt = -1;
2503 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002504 struct stat statbuf;
2505
2506 /* Validate file */
2507 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002508 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002509 goto fi0;
2510 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002511 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002512 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002513 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002514 goto fi0;
2515 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002516 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002517 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002518 goto fi0;
2519 }
2520
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002521 // read file to buffer
2522 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002523 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002524 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002525 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002526 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002527 size = statbuf.st_size;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002528 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002529 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002530 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002531 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002532 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002533 } else if (cnt < size) {
2534 // There was a partial read, shrink unused space text[]
2535 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denys Vlasenko6331cf02009-11-13 09:08:27 +01002536 status_line_bold("can't read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002537 }
2538 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002539 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002540 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002541 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002542#if ENABLE_FEATURE_VI_READONLY
2543 if (update_ro_status
2544 && ((access(fn, W_OK) < 0) ||
2545 /* root will always have access()
2546 * so we check fileperms too */
2547 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2548 )
2549 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002550 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002551 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002552#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002553 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002554}
2555
Denis Vlasenko33875382008-06-21 20:31:50 +00002556static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002557{
2558 int fd, cnt, charcnt;
2559
2560 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002561 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002562 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002563 }
Denis Vlasenko8abae882008-05-03 11:35:59 +00002564 /* By popular request we do not open file with O_TRUNC,
2565 * but instead ftruncate() it _after_ successful write.
2566 * Might reduce amount of data lost on power fail etc.
2567 */
2568 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002569 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002570 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002571 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002572 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002573 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002574 if (charcnt == cnt) {
2575 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002576 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002577 } else {
2578 charcnt = 0;
2579 }
2580 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002581 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002582}
2583
2584//----- Terminal Drawing ---------------------------------------
2585// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002586// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002587// screen coordinates
2588// 0,0 ... 0,79
2589// 1,0 ... 1,79
2590// . ... .
2591// . ... .
2592// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002593// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002594
2595//----- Move the cursor to row x col (count from 0, not 1) -------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002596static void place_cursor(int row, int col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002597{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002598 char cm1[sizeof(ESC_SET_CURSOR_POS) + sizeof(int)*3 * 2];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002599
2600 if (row < 0) row = 0;
2601 if (row >= rows) row = rows - 1;
2602 if (col < 0) col = 0;
2603 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002604
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002605 sprintf(cm1, ESC_SET_CURSOR_POS, row + 1, col + 1);
2606 write1(cm1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002607}
2608
2609//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002610static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002611{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002612 write1(ESC_CLEAR2EOL);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002613}
2614
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002615static void go_bottom_and_clear_to_eol(void)
2616{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002617 place_cursor(rows - 1, 0);
2618 clear_to_eol();
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002619}
2620
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002621//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002622static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002623{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002624 write1(ESC_CLEAR2EOS);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002625}
2626
2627//----- Start standout mode ------------------------------------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002628static void standout_start(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002629{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002630 write1(ESC_BOLD_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002631}
2632
2633//----- End standout mode --------------------------------------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002634static void standout_end(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002635{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002636 write1(ESC_NORM_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002637}
2638
2639//----- Flash the screen --------------------------------------
2640static void flash(int h)
2641{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002642 standout_start();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002643 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002644 mysleep(h);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002645 standout_end();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002646 redraw(TRUE);
2647}
2648
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002649static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002650{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002651#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002652 if (crashme > 0)
2653 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002654#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002655 if (!err_method) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002656 write1(ESC_BELL);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002657 } else {
2658 flash(10);
2659 }
2660}
2661
2662//----- Screen[] Routines --------------------------------------
2663//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002664static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002665{
2666 memset(screen, ' ', screensize); // clear new screen
2667}
2668
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002669static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002670{
2671 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002672 char *e = buf + count;
2673
Paul Fox8552aec2005-09-16 12:20:05 +00002674 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002675 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002676 return sum;
2677}
2678
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002679//----- Draw the status line at bottom of the screen -------------
2680static void show_status_line(void)
2681{
Paul Foxc3504852005-09-16 12:48:18 +00002682 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002683
Paul Fox8552aec2005-09-16 12:20:05 +00002684 // either we already have an error or status message, or we
2685 // create one.
2686 if (!have_status_msg) {
2687 cnt = format_edit_status();
2688 cksum = bufsum(status_buffer, cnt);
2689 }
2690 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002691 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002692 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002693 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002694 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002695 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002696 (columns - 1) ) {
2697 have_status_msg = 0;
2698 Hit_Return();
2699 }
2700 have_status_msg = 0;
2701 }
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002702 place_cursor(crow, ccol); // put cursor back in correct place
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002703 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002704 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002705}
2706
2707//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002708// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002709static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002710{
2711 va_list args;
2712
2713 va_start(args, format);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002714 strcpy(status_buffer, ESC_BOLD_TEXT);
2715 vsprintf(status_buffer + sizeof(ESC_BOLD_TEXT)-1, format, args);
2716 strcat(status_buffer, ESC_NORM_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002717 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002718
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002719 have_status_msg = 1 + sizeof(ESC_BOLD_TEXT) + sizeof(ESC_NORM_TEXT) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002720}
2721
Paul Fox8552aec2005-09-16 12:20:05 +00002722// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002723static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002724{
2725 va_list args;
2726
2727 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002728 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002729 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002730
2731 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002732}
2733
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002734// copy s to buf, convert unprintable
2735static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002736{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002737 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002738 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002739
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002740 buf[0] = '\0';
2741 if (!s[0])
2742 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002743
2744 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002745 for (; *s; s++) {
2746 int c_is_no_print;
2747
2748 c = *s;
2749 c_is_no_print = (c & 0x80) && !Isprint(c);
2750 if (c_is_no_print) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002751 strcpy(d, ESC_NORM_TEXT);
2752 d += sizeof(ESC_NORM_TEXT)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002753 c = '.';
2754 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002755 if (c < ' ' || c == 0x7f) {
2756 *d++ = '^';
2757 c |= '@'; /* 0x40 */
2758 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002759 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002760 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002761 *d++ = c;
2762 *d = '\0';
2763 if (c_is_no_print) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002764 strcpy(d, ESC_BOLD_TEXT);
2765 d += sizeof(ESC_BOLD_TEXT)-1;
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002766 }
2767 if (*s == '\n') {
2768 *d++ = '$';
2769 *d = '\0';
2770 }
2771 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002772 break;
2773 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002774}
2775
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002776static void not_implemented(const char *s)
2777{
2778 char buf[MAX_INPUT_LEN];
2779
2780 print_literal(buf, s);
2781 status_line_bold("\'%s\' is not implemented", buf);
2782}
2783
2784// show file status on status line
2785static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002786{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002787 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002788
2789#define tot format_edit_status__tot
2790
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002791 int cur, percent, ret, trunc_at;
2792
Paul Fox8552aec2005-09-16 12:20:05 +00002793 // file_modified is now a counter rather than a flag. this
2794 // helps reduce the amount of line counting we need to do.
2795 // (this will cause a mis-reporting of modified status
2796 // once every MAXINT editing operations.)
2797
2798 // it would be nice to do a similar optimization here -- if
2799 // we haven't done a motion that could have changed which line
2800 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002801 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002802
2803 // reduce counting -- the total lines can't have
2804 // changed if we haven't done any edits.
2805 if (file_modified != last_file_modified) {
2806 tot = cur + count_lines(dot, end - 1) - 1;
2807 last_file_modified = file_modified;
2808 }
2809
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002810 // current line percent
2811 // ------------- ~~ ----------
2812 // total lines 100
2813 if (tot > 0) {
2814 percent = (100 * cur) / tot;
2815 } else {
2816 cur = tot = 0;
2817 percent = 100;
2818 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002819
Paul Fox8552aec2005-09-16 12:20:05 +00002820 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2821 columns : STATUS_BUFFER_LEN-1;
2822
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002823 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002824#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002825 "%c %s%s%s %d/%d %d%%",
2826#else
2827 "%c %s%s %d/%d %d%%",
2828#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002829 cmd_mode_indicator[cmd_mode & 3],
2830 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002831#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002832 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002833#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002834 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002835 cur, tot, percent);
2836
2837 if (ret >= 0 && ret < trunc_at)
2838 return ret; /* it all fit */
2839
2840 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002841#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002842}
2843
2844//----- Force refresh of all Lines -----------------------------
2845static void redraw(int full_screen)
2846{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002847 place_cursor(0, 0);
2848 clear_to_eos();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002849 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002850 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002851 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002852 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002853}
2854
2855//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002856static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002857{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002858 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002859 int co;
2860 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002861 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002862
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002863 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002864 co = 0;
2865 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002866 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002867 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002868 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002869 if (c == '\n')
2870 break;
2871 if ((c & 0x80) && !Isprint(c)) {
2872 c = '.';
2873 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002874 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002875 if (c == '\t') {
2876 c = ' ';
2877 // co % 8 != 7
2878 while ((co % tabstop) != (tabstop - 1)) {
2879 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002880 }
2881 } else {
2882 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002883 if (c == 0x7f)
2884 c = '?';
2885 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002886 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002887 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002888 }
2889 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002890 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002891 // discard scrolled-off-to-the-left portion,
2892 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002893 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002894 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002895 co -= tabstop;
2896 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002897 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002898 if (src >= end)
2899 break;
2900 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002901 // check "short line, gigantic offset" case
2902 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002903 ofs = co;
2904 // discard last scrolled off part
2905 co -= ofs;
2906 dest += ofs;
2907 // fill the rest with spaces
2908 if (co < columns)
2909 memset(&dest[co], ' ', columns - co);
2910 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002911}
2912
2913//----- Refresh the changed screen lines -----------------------
2914// Copy the source line from text[] into the buffer and note
2915// if the current screenline is different from the new buffer.
2916// If they differ then that line needs redrawing on the terminal.
2917//
2918static void refresh(int full_screen)
2919{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002920#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002921
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002922 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002923 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002924
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +02002925 if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002926 unsigned c = columns, r = rows;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002927 query_screen_dimensions();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002928 full_screen |= (c - columns) | (r - rows);
2929 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002930 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2931 tp = screenbegin; // index into text[] of top line
2932
2933 // compare text[] to screen[] and mark screen[] lines that need updating
2934 for (li = 0; li < rows - 1; li++) {
2935 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002936 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002937 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002938 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002939
2940 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002941 if (tp < end) {
2942 char *t = memchr(tp, '\n', end - tp);
2943 if (!t) t = end - 1;
2944 tp = t + 1;
2945 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002946
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002947 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002948 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002949 cs = 0;
2950 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002951 sp = &screen[li * columns]; // start of screen line
2952 if (full_screen) {
2953 // force re-draw of every single column from 0 - columns-1
2954 goto re0;
2955 }
2956 // compare newly formatted buffer with virtual screen
2957 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002958 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002959 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002960 changed = TRUE; // mark for redraw
2961 break;
2962 }
2963 }
2964
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002965 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002966 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002967 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002968 changed = TRUE; // mark for redraw
2969 break;
2970 }
2971 }
2972 // now, cs is index of first diff, and ce is index of last diff
2973
2974 // if horz offset has changed, force a redraw
2975 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002976 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002977 changed = TRUE;
2978 }
2979
2980 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002981 if (cs < 0) cs = 0;
2982 if (ce > columns - 1) ce = columns - 1;
2983 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002984 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002985 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002986 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002987 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002988 place_cursor(li, cs);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002989 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002990 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002991 }
2992 }
2993
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002994 place_cursor(crow, ccol);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002995
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002996 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002997#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002998}
2999
Eric Andersen3f980402001-04-04 17:31:15 +00003000//---------------------------------------------------------------------
3001//----- the Ascii Chart -----------------------------------------------
3002//
3003// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
3004// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
3005// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
3006// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
3007// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
3008// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
3009// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
3010// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
3011// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
3012// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
3013// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
3014// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
3015// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
3016// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
3017// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
3018// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
3019//---------------------------------------------------------------------
3020
3021//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003022static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00003023{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003024 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003025 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003026 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00003027 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003028 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003029
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00003030// c1 = c; // quiet the compiler
3031// cnt = yf = 0; // quiet the compiler
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003032// p = q = save_dot = buf; // quiet the compiler
3033 memset(buf, '\0', sizeof(buf));
Eric Andersenbff7a602001-11-17 07:15:43 +00003034
Paul Fox8552aec2005-09-16 12:20:05 +00003035 show_status_line();
3036
Eric Andersenbff7a602001-11-17 07:15:43 +00003037 /* if this is a cursor key, skip these checks */
3038 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003039 case KEYCODE_UP:
3040 case KEYCODE_DOWN:
3041 case KEYCODE_LEFT:
3042 case KEYCODE_RIGHT:
3043 case KEYCODE_HOME:
3044 case KEYCODE_END:
3045 case KEYCODE_PAGEUP:
3046 case KEYCODE_PAGEDOWN:
3047 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00003048 goto key_cmd_mode;
3049 }
3050
Eric Andersen3f980402001-04-04 17:31:15 +00003051 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003052 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003053 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003054 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00003055 // we are 'R'eplacing the current *dot with new char
3056 if (*dot == '\n') {
3057 // don't Replace past E-o-l
3058 cmd_mode = 1; // convert to insert
3059 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003060 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003061 if (c != 27)
3062 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3063 dot = char_insert(dot, c); // insert new char
3064 }
3065 goto dc1;
3066 }
3067 }
3068 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003069 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003070 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00003071 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003072 if (1 <= c || Isprint(c)) {
3073 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00003074 }
3075 goto dc1;
3076 }
3077
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003078 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00003079 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00003080 //case 0x01: // soh
3081 //case 0x09: // ht
3082 //case 0x0b: // vt
3083 //case 0x0e: // so
3084 //case 0x0f: // si
3085 //case 0x10: // dle
3086 //case 0x11: // dc1
3087 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003088#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00003089 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00003090 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003091 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003092#endif
Eric Andersen822c3832001-05-07 17:37:43 +00003093 //case 0x16: // syn
3094 //case 0x17: // etb
3095 //case 0x18: // can
3096 //case 0x1c: // fs
3097 //case 0x1d: // gs
3098 //case 0x1e: // rs
3099 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003100 //case '!': // !-
3101 //case '#': // #-
3102 //case '&': // &-
3103 //case '(': // (-
3104 //case ')': // )-
3105 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003106 //case '=': // =-
3107 //case '@': // @-
3108 //case 'F': // F-
3109 //case 'K': // K-
3110 //case 'Q': // Q-
3111 //case 'S': // S-
3112 //case 'T': // T-
3113 //case 'V': // V-
3114 //case '[': // [-
3115 //case '\\': // \-
3116 //case ']': // ]-
3117 //case '_': // _-
3118 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003119 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003120 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003121 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00003122 buf[0] = c;
3123 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003124 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003125 end_cmd_q(); // stop adding to q
3126 case 0x00: // nul- ignore
3127 break;
3128 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003129 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00003130 dot_scroll(rows - 2, -1);
3131 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003132 case 4: // ctrl-D scroll down half screen
3133 dot_scroll((rows - 2) / 2, 1);
3134 break;
3135 case 5: // ctrl-E scroll down one line
3136 dot_scroll(1, 1);
3137 break;
3138 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003139 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003140 dot_scroll(rows - 2, 1);
3141 break;
3142 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00003143 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003144 break;
3145 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003146 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003147 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003148 case 0x7f: // DEL- move left (This may be ERASE char)
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003149 do {
3150 dot_left();
3151 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003152 break;
3153 case 10: // Newline ^J
3154 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003155 case KEYCODE_DOWN: // cursor key Down
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003156 do {
3157 dot_next(); // go to next B-o-l
3158 // try stay in same col
3159 dot = move_to_col(dot, ccol + offset);
3160 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003161 break;
3162 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003163 case 18: // ctrl-R force redraw
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02003164 place_cursor(0, 0);
3165 clear_to_eos();
3166 //mysleep(10); // why???
Eric Andersen3f980402001-04-04 17:31:15 +00003167 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003168 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003169 refresh(TRUE); // this will redraw the entire display
3170 break;
3171 case 13: // Carriage Return ^M
3172 case '+': // +- goto next line
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003173 do {
3174 dot_next();
3175 dot_skip_over_ws();
3176 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003177 break;
3178 case 21: // ctrl-U scroll up half screen
3179 dot_scroll((rows - 2) / 2, -1);
3180 break;
3181 case 25: // ctrl-Y scroll up one line
3182 dot_scroll(1, -1);
3183 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003184 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003185 if (cmd_mode == 0)
3186 indicate_error(c);
3187 cmd_mode = 0; // stop insrting
3188 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003189 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003190 break;
3191 case ' ': // move right
3192 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003193 case KEYCODE_RIGHT: // Cursor Key Right
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003194 do {
3195 dot_right();
3196 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003197 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003198#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003199 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003200 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3201 if ((unsigned)c1 <= 25) { // a-z?
3202 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003203 } else {
3204 indicate_error(c);
3205 }
3206 break;
3207 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003208 c1 = (get_one_char() | 0x20) - 'a';
3209 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003210 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003211 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003212 if (text <= q && q < end) {
3213 dot = q;
3214 dot_begin(); // go to B-o-l
3215 dot_skip_over_ws();
3216 }
3217 } else if (c1 == '\'') { // goto previous context
3218 dot = swap_context(dot); // swap current and previous context
3219 dot_begin(); // go to B-o-l
3220 dot_skip_over_ws();
3221 } else {
3222 indicate_error(c);
3223 }
3224 break;
3225 case 'm': // m- Mark a line
3226 // this is really stupid. If there are any inserts or deletes
3227 // between text[0] and dot then this mark will not point to the
3228 // correct location! It could be off by many lines!
3229 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003230 c1 = (get_one_char() | 0x20) - 'a';
3231 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003232 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003233 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003234 } else {
3235 indicate_error(c);
3236 }
3237 break;
3238 case 'P': // P- Put register before
3239 case 'p': // p- put register after
3240 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003241 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003242 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003243 break;
3244 }
3245 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003246 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003247 if (c == 'P') {
3248 dot_begin(); // putting lines- Put above
3249 }
3250 if (c == 'p') {
3251 // are we putting after very last line?
3252 if (end_line(dot) == (end - 1)) {
3253 dot = end; // force dot to end of text[]
3254 } else {
3255 dot_next(); // next line, then put before
3256 }
3257 }
3258 } else {
3259 if (c == 'p')
3260 dot_right(); // move to right, can move to NL
3261 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003262 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003263 end_cmd_q(); // stop adding to q
3264 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003265 case 'U': // U- Undo; replace current line with original version
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003266 if (reg[Ureg] != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003267 p = begin_line(dot);
3268 q = end_line(dot);
3269 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003270 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003271 dot = p;
3272 dot_skip_over_ws();
3273 }
3274 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003275#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003276 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003277 case KEYCODE_END: // Cursor Key End
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003278 for (;;) {
3279 dot = end_line(dot);
3280 if (--cmdcnt <= 0)
3281 break;
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003282 dot_next();
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003283 }
Eric Andersen3f980402001-04-04 17:31:15 +00003284 break;
3285 case '%': // %- find matching char of pair () [] {}
3286 for (q = dot; q < end && *q != '\n'; q++) {
3287 if (strchr("()[]{}", *q) != NULL) {
3288 // we found half of a pair
3289 p = find_pair(q, *q);
3290 if (p == NULL) {
3291 indicate_error(c);
3292 } else {
3293 dot = p;
3294 }
3295 break;
3296 }
3297 }
3298 if (*q == '\n')
3299 indicate_error(c);
3300 break;
3301 case 'f': // f- forward to a user specified char
3302 last_forward_char = get_one_char(); // get the search char
3303 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003304 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003305 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003306 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003307 case ';': // ;- look at rest of line for last forward char
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003308 do {
3309 if (last_forward_char == 0)
3310 break;
3311 q = dot + 1;
3312 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3313 q++;
3314 }
3315 if (*q == last_forward_char)
3316 dot = q;
3317 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003318 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003319 case ',': // repeat latest 'f' in opposite direction
Paul Foxb5ee8db2008-02-14 01:17:01 +00003320 if (last_forward_char == 0)
3321 break;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003322 do {
3323 q = dot - 1;
3324 while (q >= text && *q != '\n' && *q != last_forward_char) {
3325 q--;
3326 }
3327 if (q >= text && *q == last_forward_char)
3328 dot = q;
3329 } while (--cmdcnt > 0);
Paul Foxb5ee8db2008-02-14 01:17:01 +00003330 break;
3331
Eric Andersen3f980402001-04-04 17:31:15 +00003332 case '-': // -- goto prev line
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003333 do {
3334 dot_prev();
3335 dot_skip_over_ws();
3336 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003337 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003338#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003339 case '.': // .- repeat the last modifying command
3340 // Stuff the last_modifying_cmd back into stdin
3341 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003342 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003343 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003344 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003345 }
3346 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003347#endif
3348#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003349 case '?': // /- search for a pattern
3350 case '/': // /- search for a pattern
3351 buf[0] = c;
3352 buf[1] = '\0';
3353 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003354 if (q[0] && !q[1]) {
3355 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003356 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003357 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003358 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003359 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003360 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003361 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003362 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003363 goto dc3; // now find the pattern
3364 }
3365 // user changed mind and erased the "/"- do nothing
3366 break;
3367 case 'N': // N- backward search for last pattern
Eric Andersen3f980402001-04-04 17:31:15 +00003368 dir = BACK; // assume BACKWARD search
3369 p = dot - 1;
3370 if (last_search_pattern[0] == '?') {
3371 dir = FORWARD;
3372 p = dot + 1;
3373 }
3374 goto dc4; // now search for pattern
3375 break;
3376 case 'n': // n- repeat search for last pattern
3377 // search rest of text[] starting at next char
3378 // if search fails return orignal "p" not the "p+1" address
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003379 do {
3380 const char *msg;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003381 dc3:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003382 dir = FORWARD; // assume FORWARD search
3383 p = dot + 1;
3384 if (last_search_pattern[0] == '?') {
3385 dir = BACK;
3386 p = dot - 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003387 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003388 dc4:
3389 q = char_search(p, last_search_pattern + 1, dir, FULL);
3390 if (q != NULL) {
3391 dot = q; // good search, update "dot"
3392 msg = NULL;
3393 goto dc2;
3394 }
3395 // no pattern found between "dot" and "end"- continue at top
3396 p = text;
3397 if (dir == BACK) {
3398 p = end - 1;
3399 }
3400 q = char_search(p, last_search_pattern + 1, dir, FULL);
3401 if (q != NULL) { // found something
3402 dot = q; // found new pattern- goto it
3403 msg = "search hit BOTTOM, continuing at TOP";
3404 if (dir == BACK) {
3405 msg = "search hit TOP, continuing at BOTTOM";
3406 }
3407 } else {
3408 msg = "Pattern not found";
3409 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003410 dc2:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003411 if (msg)
3412 status_line_bold("%s", msg);
3413 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003414 break;
3415 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003416 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003417 if (q != NULL) { // found blank line
3418 dot = next_line(q); // move to next blank line
3419 }
3420 break;
3421 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003422 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003423 if (q != NULL) { // found blank line
3424 dot = next_line(q); // move to next blank line
3425 }
3426 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003427#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003428 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003429 case '1': // 1-
3430 case '2': // 2-
3431 case '3': // 3-
3432 case '4': // 4-
3433 case '5': // 5-
3434 case '6': // 6-
3435 case '7': // 7-
3436 case '8': // 8-
3437 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003438 if (c == '0' && cmdcnt < 1) {
3439 dot_begin(); // this was a standalone zero
3440 } else {
3441 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3442 }
3443 break;
3444 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003445 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003446#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003447 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003448#else
Eric Andersen822c3832001-05-07 17:37:43 +00003449 if (*p == ':')
3450 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003451 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003452 if (cnt <= 0)
3453 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003454 if (strncmp(p, "quit", cnt) == 0
3455 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003456 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003457 if (file_modified && p[1] != '!') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003458 status_line_bold("No write since last change (:%s! overrides)", p);
Eric Andersen3f980402001-04-04 17:31:15 +00003459 } else {
3460 editing = 0;
3461 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003462 } else if (strncmp(p, "write", cnt) == 0
3463 || strncmp(p, "wq", cnt) == 0
3464 || strncmp(p, "wn", cnt) == 0
3465 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003466 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003467 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003468 if (cnt < 0) {
3469 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003470 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003471 } else {
3472 file_modified = 0;
3473 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003474 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003475 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3476 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3477 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003478 editing = 0;
3479 }
Eric Andersen3f980402001-04-04 17:31:15 +00003480 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003481 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003482 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003483 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003484 dot = find_line(j); // go to line # j
3485 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003486 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003487 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003488 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003489#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003490 break;
3491 case '<': // <- Left shift something
3492 case '>': // >- Right shift something
3493 cnt = count_lines(text, dot); // remember what line we are on
3494 c1 = get_one_char(); // get the type of thing to delete
3495 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003496 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003497 p = begin_line(p);
3498 q = end_line(q);
3499 i = count_lines(p, q); // # of lines we are shifting
3500 for ( ; i > 0; i--, p = next_line(p)) {
3501 if (c == '<') {
3502 // shift left- remove tab or 8 spaces
3503 if (*p == '\t') {
3504 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003505 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003506 } else if (*p == ' ') {
3507 // we should be calculating columns, not just SPACE
3508 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003509 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003510 }
3511 }
3512 } else if (c == '>') {
3513 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003514 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003515 }
3516 }
3517 dot = find_line(cnt); // what line were we on
3518 dot_skip_over_ws();
3519 end_cmd_q(); // stop adding to q
3520 break;
3521 case 'A': // A- append at e-o-l
3522 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003523 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003524 case 'a': // a- append after current char
3525 if (*dot != '\n')
3526 dot++;
3527 goto dc_i;
3528 break;
3529 case 'B': // B- back a blank-delimited Word
3530 case 'E': // E- end of a blank-delimited word
3531 case 'W': // W- forward a blank-delimited word
Eric Andersen3f980402001-04-04 17:31:15 +00003532 dir = FORWARD;
3533 if (c == 'B')
3534 dir = BACK;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003535 do {
3536 if (c == 'W' || isspace(dot[dir])) {
3537 dot = skip_thing(dot, 1, dir, S_TO_WS);
3538 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3539 }
3540 if (c != 'W')
3541 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3542 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003543 break;
3544 case 'C': // C- Change to e-o-l
3545 case 'D': // D- delete to e-o-l
3546 save_dot = dot;
3547 dot = dollar_line(dot); // move to before NL
3548 // copy text into a register and delete
3549 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3550 if (c == 'C')
3551 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003552#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003553 if (c == 'D')
3554 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003555#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003556 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003557 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003558 c1 = get_one_char();
3559 if (c1 != 'g') {
3560 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003561 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003562 buf[2] = '\0';
3563 not_implemented(buf);
3564 break;
3565 }
3566 if (cmdcnt == 0)
3567 cmdcnt = 1;
3568 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003569 case 'G': // G- goto to a line number (default= E-O-F)
3570 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003571 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003572 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003573 }
3574 dot_skip_over_ws();
3575 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003576 case 'H': // H- goto top line on screen
3577 dot = screenbegin;
3578 if (cmdcnt > (rows - 1)) {
3579 cmdcnt = (rows - 1);
3580 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003581 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003582 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003583 }
Eric Andersen3f980402001-04-04 17:31:15 +00003584 dot_skip_over_ws();
3585 break;
3586 case 'I': // I- insert before first non-blank
3587 dot_begin(); // 0
3588 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003589 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003590 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003591 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003592 dc_i:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003593 cmd_mode = 1; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003594 break;
3595 case 'J': // J- join current and next lines together
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003596 do {
3597 dot_end(); // move to NL
3598 if (dot < end - 1) { // make sure not last char in text[]
3599 *dot++ = ' '; // replace NL with space
3600 file_modified++;
3601 while (isblank(*dot)) { // delete leading WS
3602 dot_delete();
3603 }
Eric Andersen3f980402001-04-04 17:31:15 +00003604 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003605 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003606 end_cmd_q(); // stop adding to q
3607 break;
3608 case 'L': // L- goto bottom line on screen
3609 dot = end_screen();
3610 if (cmdcnt > (rows - 1)) {
3611 cmdcnt = (rows - 1);
3612 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003613 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003614 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003615 }
Eric Andersen3f980402001-04-04 17:31:15 +00003616 dot_begin();
3617 dot_skip_over_ws();
3618 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003619 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003620 dot = screenbegin;
3621 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3622 dot = next_line(dot);
3623 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003624 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003625 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003626 p = begin_line(dot);
3627 if (p[-1] == '\n') {
3628 dot_prev();
3629 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3630 dot_end();
3631 dot = char_insert(dot, '\n');
3632 } else {
3633 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003634 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003635 dot_prev(); // -
3636 }
3637 goto dc_i;
3638 break;
3639 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003640 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003641 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003642 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003643 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003644 c = 'x';
3645 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003646 case 'X': // X- delete char before dot
3647 case 'x': // x- delete the current char
3648 case 's': // s- substitute the current char
Eric Andersen3f980402001-04-04 17:31:15 +00003649 dir = 0;
3650 if (c == 'X')
3651 dir = -1;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003652 do {
3653 if (dot[dir] != '\n') {
3654 if (c == 'X')
3655 dot--; // delete prev char
3656 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3657 }
3658 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003659 end_cmd_q(); // stop adding to q
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003660 if (c == 's')
3661 goto dc_i; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003662 break;
3663 case 'Z': // Z- if modified, {write}; exit
3664 // ZZ means to save file (if necessary), then exit
3665 c1 = get_one_char();
3666 if (c1 != 'Z') {
3667 indicate_error(c);
3668 break;
3669 }
Paul Foxf0305b72006-03-28 14:18:21 +00003670 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003671 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003672 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003673 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003674 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003675 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003676 if (cnt < 0) {
3677 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003678 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003679 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003680 editing = 0;
3681 }
3682 } else {
3683 editing = 0;
3684 }
3685 break;
3686 case '^': // ^- move to first non-blank on line
3687 dot_begin();
3688 dot_skip_over_ws();
3689 break;
3690 case 'b': // b- back a word
3691 case 'e': // e- end of word
Eric Andersen3f980402001-04-04 17:31:15 +00003692 dir = FORWARD;
3693 if (c == 'b')
3694 dir = BACK;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003695 do {
3696 if ((dot + dir) < text || (dot + dir) > end - 1)
3697 break;
3698 dot += dir;
3699 if (isspace(*dot)) {
3700 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3701 }
3702 if (isalnum(*dot) || *dot == '_') {
3703 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3704 } else if (ispunct(*dot)) {
3705 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3706 }
3707 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003708 break;
3709 case 'c': // c- change something
3710 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003711#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003712 case 'y': // y- yank something
3713 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003714#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003715 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003716 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003717 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003718#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003719 if (c == 'y' || c == 'Y')
3720 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003721#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003722 c1 = 'y';
3723 if (c != 'Y')
3724 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003725 // determine range, and whether it spans lines
3726 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003727 if (c1 == 27) { // ESC- user changed mind and wants out
3728 c = c1 = 27; // Escape- do nothing
3729 } else if (strchr("wW", c1)) {
3730 if (c == 'c') {
3731 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003732 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003733 if (q <= text || q[-1] == '\n')
3734 break;
3735 q--;
3736 }
3737 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003738 dot = yank_delete(p, q, ml, yf); // delete word
3739 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3740 // partial line copy text into a register and delete
3741 dot = yank_delete(p, q, ml, yf); // delete word
3742 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3743 // whole line copy text into a register and delete
3744 dot = yank_delete(p, q, ml, yf); // delete lines
3745 whole = 1;
3746 } else {
3747 // could not recognize object
3748 c = c1 = 27; // error-
3749 ml = 0;
3750 indicate_error(c);
3751 }
3752 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003753 if (c == 'c') {
3754 dot = char_insert(dot, '\n');
3755 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003756 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003757 dot_prev();
3758 }
3759 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003760 dot_begin();
3761 dot_skip_over_ws();
3762 }
Eric Andersen3f980402001-04-04 17:31:15 +00003763 }
3764 if (c1 != 27) {
3765 // if CHANGING, not deleting, start inserting after the delete
3766 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003767 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003768 goto dc_i; // start inserting
3769 }
3770 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003771 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003772 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003773#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003774 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003775 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003776 }
3777 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003778 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003779 for (cnt = 0; p <= q; p++) {
3780 if (*p == '\n')
3781 cnt++;
3782 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003783 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003784 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003785#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003786 end_cmd_q(); // stop adding to q
3787 }
3788 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003789 }
Eric Andersen3f980402001-04-04 17:31:15 +00003790 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003791 case KEYCODE_UP: // cursor key Up
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003792 do {
3793 dot_prev();
3794 dot = move_to_col(dot, ccol + offset); // try stay in same col
3795 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003796 break;
3797 case 'r': // r- replace the current char with user input
3798 c1 = get_one_char(); // get the replacement char
3799 if (*dot != '\n') {
3800 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003801 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003802 }
3803 end_cmd_q(); // stop adding to q
3804 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003805 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003806 last_forward_char = get_one_char();
3807 do_cmd(';');
3808 if (*dot == last_forward_char)
3809 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003810 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003811 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003812 case 'w': // w- forward a word
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003813 do {
3814 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3815 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3816 } else if (ispunct(*dot)) { // we are on PUNCT
3817 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3818 }
3819 if (dot < end - 1)
3820 dot++; // move over word
3821 if (isspace(*dot)) {
3822 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3823 }
3824 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003825 break;
3826 case 'z': // z-
3827 c1 = get_one_char(); // get the replacement char
3828 cnt = 0;
3829 if (c1 == '.')
3830 cnt = (rows - 2) / 2; // put dot at center
3831 if (c1 == '-')
3832 cnt = rows - 2; // put dot at bottom
3833 screenbegin = begin_line(dot); // start dot at top
3834 dot_scroll(cnt, -1);
3835 break;
3836 case '|': // |- move to column "cmdcnt"
3837 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3838 break;
3839 case '~': // ~- flip the case of letters a-z -> A-Z
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003840 do {
3841 if (islower(*dot)) {
3842 *dot = toupper(*dot);
3843 file_modified++;
3844 } else if (isupper(*dot)) {
3845 *dot = tolower(*dot);
3846 file_modified++;
3847 }
3848 dot_right();
3849 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003850 end_cmd_q(); // stop adding to q
3851 break;
3852 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003853 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003854 dot_begin();
3855 break;
3856 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003857#if 0
3858 case KEYCODE_FUN1: // Function Key F1
3859 case KEYCODE_FUN2: // Function Key F2
3860 case KEYCODE_FUN3: // Function Key F3
3861 case KEYCODE_FUN4: // Function Key F4
3862 case KEYCODE_FUN5: // Function Key F5
3863 case KEYCODE_FUN6: // Function Key F6
3864 case KEYCODE_FUN7: // Function Key F7
3865 case KEYCODE_FUN8: // Function Key F8
3866 case KEYCODE_FUN9: // Function Key F9
3867 case KEYCODE_FUN10: // Function Key F10
3868 case KEYCODE_FUN11: // Function Key F11
3869 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003870 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003871#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003872 }
3873
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003874 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003875 // if text[] just became empty, add back an empty line
3876 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003877 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003878 dot = text;
3879 }
3880 // it is OK for dot to exactly equal to end, otherwise check dot validity
3881 if (dot != end) {
3882 dot = bound_dot(dot); // make sure "dot" is valid
3883 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003884#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003885 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003886#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003887
3888 if (!isdigit(c))
3889 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3890 cnt = dot - begin_line(dot);
3891 // Try to stay off of the Newline
3892 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3893 dot--;
3894}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003895
Paul Fox35e9c5d2008-03-06 16:26:12 +00003896/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003897#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003898static int totalcmds = 0;
3899static int Mp = 85; // Movement command Probability
3900static int Np = 90; // Non-movement command Probability
3901static int Dp = 96; // Delete command Probability
3902static int Ip = 97; // Insert command Probability
3903static int Yp = 98; // Yank command Probability
3904static int Pp = 99; // Put command Probability
3905static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003906static const char chars[20] = "\t012345 abcdABCD-=.$";
3907static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003908 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003909 "broadcast", "the", "emergency", "of",
3910 "system", "quick", "brown", "fox",
3911 "jumped", "over", "lazy", "dogs",
3912 "back", "January", "Febuary", "March"
3913};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003914static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003915 "You should have received a copy of the GNU General Public License\n",
3916 "char c, cm, *cmd, *cmd1;\n",
3917 "generate a command by percentages\n",
3918 "Numbers may be typed as a prefix to some commands.\n",
3919 "Quit, discarding changes!\n",
3920 "Forced write, if permission originally not valid.\n",
3921 "In general, any ex or ed command (such as substitute or delete).\n",
3922 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3923 "Please get w/ me and I will go over it with you.\n",
3924 "The following is a list of scheduled, committed changes.\n",
3925 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3926 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3927 "Any question about transactions please contact Sterling Huxley.\n",
3928 "I will try to get back to you by Friday, December 31.\n",
3929 "This Change will be implemented on Friday.\n",
3930 "Let me know if you have problems accessing this;\n",
3931 "Sterling Huxley recently added you to the access list.\n",
3932 "Would you like to go to lunch?\n",
3933 "The last command will be automatically run.\n",
3934 "This is too much english for a computer geek.\n",
3935};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003936static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003937 "You should have received a copy of the GNU General Public License\n",
3938 "char c, cm, *cmd, *cmd1;\n",
3939 "generate a command by percentages\n",
3940 "Numbers may be typed as a prefix to some commands.\n",
3941 "Quit, discarding changes!\n",
3942 "Forced write, if permission originally not valid.\n",
3943 "In general, any ex or ed command (such as substitute or delete).\n",
3944 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3945 "Please get w/ me and I will go over it with you.\n",
3946 "The following is a list of scheduled, committed changes.\n",
3947 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3948 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3949 "Any question about transactions please contact Sterling Huxley.\n",
3950 "I will try to get back to you by Friday, December 31.\n",
3951 "This Change will be implemented on Friday.\n",
3952 "Let me know if you have problems accessing this;\n",
3953 "Sterling Huxley recently added you to the access list.\n",
3954 "Would you like to go to lunch?\n",
3955 "The last command will be automatically run.\n",
3956 "This is too much english for a computer geek.\n",
3957};
3958
3959// create a random command to execute
3960static void crash_dummy()
3961{
3962 static int sleeptime; // how long to pause between commands
3963 char c, cm, *cmd, *cmd1;
3964 int i, cnt, thing, rbi, startrbi, percent;
3965
3966 // "dot" movement commands
3967 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3968
3969 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02003970 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003971 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003972 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02003973 readbuffer[0] = 'X';
3974 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003975 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003976 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003977 // generate a command by percentages
3978 percent = (int) lrand48() % 100; // get a number from 0-99
3979 if (percent < Mp) { // Movement commands
3980 // available commands
3981 cmd = cmd1;
3982 M++;
3983 } else if (percent < Np) { // non-movement commands
3984 cmd = "mz<>\'\""; // available commands
3985 N++;
3986 } else if (percent < Dp) { // Delete commands
3987 cmd = "dx"; // available commands
3988 D++;
3989 } else if (percent < Ip) { // Inset commands
3990 cmd = "iIaAsrJ"; // available commands
3991 I++;
3992 } else if (percent < Yp) { // Yank commands
3993 cmd = "yY"; // available commands
3994 Y++;
3995 } else if (percent < Pp) { // Put commands
3996 cmd = "pP"; // available commands
3997 P++;
3998 } else {
3999 // We do not know how to handle this command, try again
4000 U++;
4001 goto cd0;
4002 }
4003 // randomly pick one of the available cmds from "cmd[]"
4004 i = (int) lrand48() % strlen(cmd);
4005 cm = cmd[i];
4006 if (strchr(":\024", cm))
4007 goto cd0; // dont allow colon or ctrl-T commands
4008 readbuffer[rbi++] = cm; // put cmd into input buffer
4009
4010 // now we have the command-
4011 // there are 1, 2, and multi char commands
4012 // find out which and generate the rest of command as necessary
4013 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
4014 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
4015 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
4016 cmd1 = "abcdefghijklmnopqrstuvwxyz";
4017 }
4018 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4019 c = cmd1[thing];
4020 readbuffer[rbi++] = c; // add movement to input buffer
4021 }
4022 if (strchr("iIaAsc", cm)) { // multi-char commands
4023 if (cm == 'c') {
4024 // change some thing
4025 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4026 c = cmd1[thing];
4027 readbuffer[rbi++] = c; // add movement to input buffer
4028 }
4029 thing = (int) lrand48() % 4; // what thing to insert
4030 cnt = (int) lrand48() % 10; // how many to insert
4031 for (i = 0; i < cnt; i++) {
4032 if (thing == 0) { // insert chars
4033 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
4034 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004035 strcat(readbuffer, words[(int) lrand48() % 20]);
4036 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004037 sleeptime = 0; // how fast to type
4038 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004039 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004040 sleeptime = 0; // how fast to type
4041 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004042 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004043 sleeptime = 0; // how fast to type
4044 }
4045 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004046 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004047 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02004048 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004049 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004050 totalcmds++;
4051 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004052 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004053}
4054
4055// test to see if there are any errors
4056static void crash_test()
4057{
4058 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004059
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004060 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004061 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004062
4063 msg[0] = '\0';
4064 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004065 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004066 }
4067 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004068 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004069 }
4070 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004071 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004072 }
4073 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004074 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004075 }
4076 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004077 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004078 }
4079 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004080 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004081 }
4082
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004083 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00004084 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02004085 totalcmds, last_input_char, msg, ESC_BOLD_TEXT, ESC_NORM_TEXT);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01004086 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00004087 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004088 if (d[0] == '\n' || d[0] == '\r')
4089 break;
4090 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004091 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004092 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004093 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004094 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004095 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
4096 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
4097 oldtim = tim;
4098 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004099}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00004100#endif