blob: 4112095452cb1f801937452cafe4c5d0272a1701 [file] [log] [blame]
Rob Landleye5e1a102006-06-21 01:15:36 +00001/* vi: set sw=4 ts=4: */
Eric Andersen3f980402001-04-04 17:31:15 +00002/*
3 * tiny vi.c: A small 'vi' clone
4 * Copyright (C) 2000, 2001 Sterling Huxley <sterling@europa.com>
5 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02006 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen3f980402001-04-04 17:31:15 +00007 */
8
Eric Andersen3f980402001-04-04 17:31:15 +00009/*
Eric Andersen3f980402001-04-04 17:31:15 +000010 * Things To Do:
11 * EXINIT
Eric Andersen1c0d3112001-04-16 15:46:44 +000012 * $HOME/.exrc and ./.exrc
Eric Andersen3f980402001-04-04 17:31:15 +000013 * add magic to search /foo.*bar
14 * add :help command
15 * :map macros
Eric Andersen3f980402001-04-04 17:31:15 +000016 * if mark[] values were line numbers rather than pointers
17 * it would be easier to change the mark when add/delete lines
Eric Andersen1c0d3112001-04-16 15:46:44 +000018 * More intelligence in refresh()
19 * ":r !cmd" and "!cmd" to filter text through an external command
20 * A true "undo" facility
21 * An "ex" line oriented mode- maybe using "cmdedit"
Eric Andersen3f980402001-04-04 17:31:15 +000022 */
23
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;
Denys Vlasenko04cecd52010-04-16 22:13:55 -0700601 while (1) {
602 edit_file(argv[optind]); /* param might be NULL */
603 if (++optind >= argc)
604 break;
Eric Andersen3f980402001-04-04 17:31:15 +0000605 }
606 //-----------------------------------------------------------
607
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000608 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000609}
610
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000611/* read text from file or create an empty buf */
612/* will also update current_filename */
613static int init_text_buffer(char *fn)
614{
615 int rc;
616 int size = file_size(fn); // file size. -1 means does not exist.
617
618 /* allocate/reallocate text buffer */
619 free(text);
Denis Vlasenkob1759462008-06-20 20:20:54 +0000620 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000621 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000622
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000623 if (fn != current_filename) {
624 free(current_filename);
625 current_filename = xstrdup(fn);
626 }
627 if (size < 0) {
628 // file dont exist. Start empty buf with dummy line
629 char_insert(text, '\n');
630 rc = 0;
631 } else {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000632 rc = file_insert(fn, text, 1);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000633 }
634 file_modified = 0;
635 last_file_modified = -1;
636#if ENABLE_FEATURE_VI_YANKMARK
637 /* init the marks. */
638 memset(mark, 0, sizeof(mark));
639#endif
640 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000641}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000642
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700643#if ENABLE_FEATURE_VI_WIN_RESIZE
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200644static int query_screen_dimensions(void)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700645{
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200646 unsigned c, r;
647 int err = get_terminal_width_height(STDIN_FILENO, &c, &r);
648 columns = c; rows = r;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700649 if (rows > MAX_SCR_ROWS)
650 rows = MAX_SCR_ROWS;
651 if (columns > MAX_SCR_COLS)
652 columns = MAX_SCR_COLS;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200653 return err;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700654}
655#else
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200656# define query_screen_dimensions() (0)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700657#endif
658
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000659static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000660{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000661#if ENABLE_FEATURE_VI_YANKMARK
662#define cur_line edit_file__cur_line
663#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000664 int c;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000665#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000666 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000667#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000668
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000669 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000670 rawmode();
671 rows = 24;
672 columns = 80;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200673 IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions();
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700674#if ENABLE_FEATURE_VI_ASK_TERMINAL
675 if (G.get_rowcol_error /* TODO? && no input on stdin */) {
676 uint64_t k;
677 write1("\033[999;999H" "\033[6n");
678 fflush_all();
679 k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100);
680 if ((int32_t)k == KEYCODE_CURSOR_POS) {
681 uint32_t rc = (k >> 32);
682 columns = (rc & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200683 if (columns > MAX_SCR_COLS)
684 columns = MAX_SCR_COLS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700685 rows = ((rc >> 16) & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200686 if (rows > MAX_SCR_ROWS)
687 rows = MAX_SCR_ROWS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700688 }
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700689 }
690#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000691 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000692 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000693
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000694#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000695 YDreg = 26; // default Yank/Delete reg
696 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000697 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000698#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000699
Eric Andersen3f980402001-04-04 17:31:15 +0000700 last_forward_char = last_input_char = '\0';
701 crow = 0;
702 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000703
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000704#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700705 signal(SIGINT, catch_sig);
Eric Andersen3f980402001-04-04 17:31:15 +0000706 signal(SIGWINCH, winch_sig);
707 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000708 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000709 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000710 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000711 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000712#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000713
Eric Andersen3f980402001-04-04 17:31:15 +0000714 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
715 cmdcnt = 0;
716 tabstop = 8;
717 offset = 0; // no horizontal offset
718 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000719#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000720 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000721 ioq = ioq_start = NULL;
722 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000723 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000724#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000725
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000726#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000727 {
728 char *p, *q;
729 int n = 0;
730
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700731 while ((p = initial_cmds[n]) != NULL) {
Denis Vlasenkof9234132007-03-21 00:03:42 +0000732 do {
733 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000734 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000735 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000736 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000737 *p++ = '\0';
738 if (*q)
739 colon(q);
740 } while (p);
741 free(initial_cmds[n]);
742 initial_cmds[n] = NULL;
743 n++;
744 }
745 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000746#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000747 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000748 //------This is the main Vi cmd handling loop -----------------------
749 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000750#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000751 if (crashme > 0) {
752 if ((end - text) > 1) {
753 crash_dummy(); // generate a random command
754 } else {
755 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000756 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
757 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000758 refresh(FALSE);
759 }
760 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000761#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000762 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000763#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000764 // save a copy of the current line- for the 'U" command
765 if (begin_line(dot) != cur_line) {
766 cur_line = begin_line(dot);
767 text_yank(begin_line(dot), end_line(dot), Ureg);
768 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000769#endif
770#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000771 // These are commands that change text[].
772 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000773 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000774 && cmd_mode == 0 // command mode
775 && c > '\0' // exclude NUL and non-ASCII chars
776 && c < 0x7f // (Unicode and such)
777 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000778 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000779 start_new_cmd_q(c);
780 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000781#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000782 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000783
Eric Andersen3f980402001-04-04 17:31:15 +0000784 // poll to see if there is input already waiting. if we are
785 // not able to display output fast enough to keep up, skip
786 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200787 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000788 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000789 refresh(FALSE);
790 show_status_line();
791 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000792#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000793 if (crashme > 0)
794 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000795#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000796 }
797 //-------------------------------------------------------------------
798
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000799 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000800 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000801#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000802}
803
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000804//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000805#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000806static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000807{
808 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000809 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000810 IF_FEATURE_VI_YANKMARK(char c;)
811 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000812
813 *addr = -1; // assume no addr
814 if (*p == '.') { // the current line
815 p++;
816 q = begin_line(dot);
817 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000818 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000819#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000820 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000821 p++;
822 c = tolower(*p);
823 p++;
824 if (c >= 'a' && c <= 'z') {
825 // we have a mark
826 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000827 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000828 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000829 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000830 }
831 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000832 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000833#endif
834#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000835 else if (*p == '/') { // a search pattern
836 q = strchrnul(++p, '/');
837 pat = xstrndup(p, q - p); // save copy of pattern
838 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000839 if (*p == '/')
840 p++;
841 q = char_search(dot, pat, FORWARD, FULL);
842 if (q != NULL) {
843 *addr = count_lines(text, q);
844 }
845 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000846 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000847#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000848 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000849 p++;
850 q = begin_line(end - 1);
851 *addr = count_lines(text, q);
852 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000853 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000854 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000855 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200856 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000857 *addr = -1;
858 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000859 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000860}
861
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000862static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000863{
864 //----- get the address' i.e., 1,3 'a,'b -----
865 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000866 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000867 p++; // skip over leading spaces
868 if (*p == '%') { // alias for 1,$
869 p++;
870 *b = 1;
871 *e = count_lines(text, end-1);
872 goto ga0;
873 }
874 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000875 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000876 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000877 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000878 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000879 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000880 p++;
881 // get SECOND addr, if present
882 p = get_one_address(p, e);
883 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000884 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000885 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000886 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000887 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000888}
889
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000890#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000891static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000892 const char *short_opname, int opt)
893{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000894 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000895 int l = strlen(opname) - 1; /* opname have + ' ' */
896
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200897 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000898 if (strncasecmp(a, opname, l) == 0
899 || strncasecmp(a, short_opname, 2) == 0
900 ) {
901 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000902 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000903 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000904 vi_setops |= opt;
905 }
906}
907#endif
908
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000909// buf must be no longer than MAX_INPUT_LEN!
910static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000911{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000912 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000913 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000914 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000915 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000916
917 // :3154 // if (-e line 3154) goto it else stay put
918 // :4,33w! foo // write a portion of buffer to file "foo"
919 // :w // write all of buffer to current file
920 // :q // quit
921 // :q! // quit- dont care about modified file
922 // :'a,'z!sort -u // filter block through sort
923 // :'f // goto mark "f"
924 // :'fl // list literal the mark "f" line
925 // :.r bar // read file "bar" into buffer before dot
926 // :/123/,/abc/d // delete lines from "123" line to "abc" line
927 // :/xyz/ // goto the "xyz" line
928 // :s/find/replace/ // substitute pattern "find" with "replace"
929 // :!<cmd> // run <cmd> then return
930 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000931
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000932 if (!buf[0])
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200933 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000934 if (*buf == ':')
935 buf++; // move past the ':'
936
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000937 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000938 b = e = -1;
939 q = text; // assume 1,$ for the range
940 r = end - 1;
941 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000942 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000943
944 // look for optional address(es) :. :1 :1,9 :'q,'a :%
945 buf = get_address(buf, &b, &e);
946
947 // remember orig command line
948 orig_buf = buf;
949
950 // get the COMMAND into cmd[]
951 buf1 = cmd;
952 while (*buf != '\0') {
953 if (isspace(*buf))
954 break;
955 *buf1++ = *buf++;
956 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000957 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000958 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000959 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000960 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000961 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000962 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000963 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000964 if (buf1) {
965 useforce = TRUE;
966 *buf1 = '\0'; // get rid of !
967 }
968 if (b >= 0) {
969 // if there is only one addr, then the addr
970 // is the line number of the single line the
971 // user wants. So, reset the end
972 // pointer to point at end of the "b" line
973 q = find_line(b); // what line is #b
974 r = end_line(q);
975 li = 1;
976 }
977 if (e >= 0) {
978 // we were given two addrs. change the
979 // end pointer to the addr given by user.
980 r = find_line(e); // what line is #e
981 r = end_line(r);
982 li = e - b + 1;
983 }
984 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000985 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000986 if (i == 0) { // :123CR goto line #123
987 if (b >= 0) {
988 dot = find_line(b); // what line is #b
989 dot_skip_over_ws();
990 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000991 }
992#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200993 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000994 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000995 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000996 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000997 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000998 retcode = system(orig_buf + 1); // run the cmd
999 if (retcode)
1000 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001001 rawmode();
1002 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +00001003 }
1004#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001005 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001006 if (b < 0) { // no addr given- use defaults
1007 b = e = count_lines(text, dot);
1008 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001009 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001010 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001011 if (b < 0) { // no addr given- use defaults
1012 q = begin_line(dot); // assume .,. for the range
1013 r = end_line(dot);
1014 }
1015 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
1016 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001017 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001018 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001019 if (file_modified && !useforce) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001020 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001021 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001022 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001023 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001024 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001025 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001026 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001027 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001028 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001029 } else {
1030 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001031 status_line_bold("No current filename");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001032 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001033 }
1034
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001035 if (init_text_buffer(fn) < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001036 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001037
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001038#if ENABLE_FEATURE_VI_YANKMARK
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001039 if (Ureg >= 0 && Ureg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001040 free(reg[Ureg]); // free orig line reg- for 'U'
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001041 reg[Ureg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001042 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001043 if (YDreg >= 0 && YDreg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001044 free(reg[YDreg]); // free default yank/delete register
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001045 reg[YDreg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001046 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001047#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001048 // how many lines in text[]?
1049 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001050 status_line("\"%s\"%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001051 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001052 " %dL, %dC", current_filename,
1053 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001054 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001055 ((readonly_mode) ? " [Readonly]" : ""),
1056 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001057 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001058 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001059 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01001060 status_line_bold("No address allowed on this command");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001061 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001062 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001063 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001064 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001065 free(current_filename);
1066 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001067 } else {
1068 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +00001069 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001070 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001071 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001072 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001073 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001074 cookmode();
1075 show_help();
1076 rawmode();
1077 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001078 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001079 if (b < 0) { // no addr given- use defaults
1080 q = begin_line(dot); // assume .,. for the range
1081 r = end_line(dot);
1082 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001083 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001084 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001085 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001086 int c_is_no_print;
1087
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001088 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +00001089 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001090 if (c_is_no_print) {
1091 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001092 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +00001093 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001094 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001095 write1("$\r");
1096 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001097 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001098 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001099 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001100 else
1101 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001102 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001103 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001104 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001105 standout_end();
1106 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001107 Hit_Return();
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001108 } else if (strncmp(cmd, "quit", i) == 0 // quit
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001109 || strncmp(cmd, "next", i) == 0 // edit next file
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001110 || strncmp(cmd, "prev", i) == 0 // edit previous file
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001111 ) {
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001112 int n;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001113 if (useforce) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001114 if (*cmd == 'q') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001115 // force end of argv list
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001116 optind = save_argc;
1117 }
1118 editing = 0;
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001119 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001120 }
1121 // don't exit if the file been modified
1122 if (file_modified) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001123 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001124 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001125 }
1126 // are there other file to edit
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001127 n = save_argc - optind - 1;
1128 if (*cmd == 'q' && n > 0) {
1129 status_line_bold("%d more file(s) to edit", n);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001130 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001131 }
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001132 if (*cmd == 'n' && n <= 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001133 status_line_bold("No more files to edit");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001134 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001135 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001136 if (*cmd == 'p') {
1137 // are there previous files to edit
1138 if (optind < 1) {
1139 status_line_bold("No previous files to edit");
1140 goto ret;
1141 }
1142 optind -= 2;
1143 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001144 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001145 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001146 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001147 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001148 status_line_bold("No filename given");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001149 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001150 }
1151 if (b < 0) { // no addr given- use defaults
1152 q = begin_line(dot); // assume "dot"
1153 }
1154 // read after current line- unless user said ":0r foo"
1155 if (b != 0)
1156 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001157 { // dance around potentially-reallocated text[]
1158 uintptr_t ofs = q - text;
1159 ch = file_insert(fn, q, 0);
1160 q = text + ofs;
1161 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001162 if (ch < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001163 goto ret; // nothing was inserted
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001164 // how many lines in text[]?
1165 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001166 status_line("\"%s\""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001167 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001168 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001169 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001170 li, ch);
1171 if (ch > 0) {
1172 // if the insert is before "dot" then we need to update
1173 if (q <= dot)
1174 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001175 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001176 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001177 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001178 if (file_modified && !useforce) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001179 status_line_bold("No write since last change (:%s! overrides)", cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001180 } else {
1181 // reset the filenames to edit
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001182 optind = -1; /* start from 0th file */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001183 editing = 0;
1184 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001185#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001186 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001187#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001188 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001189#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001190 i = 0; // offset into args
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02001191 // only blank is regarded as args delimiter. What about tab '\t'?
Denis Vlasenkof9234132007-03-21 00:03:42 +00001192 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001193 // print out values of all options
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001194#if ENABLE_FEATURE_VI_SETOPTS
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001195 status_line_bold(
1196 "%sautoindent "
1197 "%sflash "
1198 "%signorecase "
1199 "%sshowmatch "
1200 "tabstop=%u",
1201 autoindent ? "" : "no",
1202 err_method ? "" : "no",
1203 ignorecase ? "" : "no",
1204 showmatch ? "" : "no",
1205 tabstop
1206 );
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001207#endif
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001208 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001209 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001210#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001211 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001212 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001213 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001214 i = 2; // ":set noautoindent"
1215 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001216 setops(argp, "flash " , i, "fl", VI_ERR_METHOD);
Denis Vlasenkof9234132007-03-21 00:03:42 +00001217 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001218 setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH );
1219 if (strncmp(argp + i, "tabstop=", 8) == 0) {
1220 int t = 0;
1221 sscanf(argp + i+8, "%u", &t);
1222 if (t > 0 && t <= MAX_TABSTOP)
1223 tabstop = t;
Denis Vlasenkof9234132007-03-21 00:03:42 +00001224 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001225 argp = skip_non_whitespace(argp);
1226 argp = skip_whitespace(argp);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001227 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001228#endif /* FEATURE_VI_SETOPTS */
1229#endif /* FEATURE_VI_SET */
1230#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001231 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001232 char *F, *R, *flags;
1233 size_t len_F, len_R;
1234 int gflag; // global replace flag
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001235
1236 // F points to the "find" pattern
1237 // R points to the "replace" pattern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001238 // replace the cmd line delimiters "/" with NULs
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001239 c = orig_buf[1]; // what is the delimiter
1240 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001241 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001242 if (!R)
1243 goto colon_s_fail;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001244 len_F = R - F;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001245 *R++ = '\0'; // terminate "find"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001246 flags = strchr(R, c);
1247 if (!flags)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001248 goto colon_s_fail;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001249 len_R = flags - R;
1250 *flags++ = '\0'; // terminate "replace"
1251 gflag = *flags;
1252
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001253 q = begin_line(q);
1254 if (b < 0) { // maybe :s/foo/bar/
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001255 q = begin_line(dot); // start with cur line
1256 b = count_lines(text, q); // cur line number
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001257 }
1258 if (e < 0)
1259 e = b; // maybe :.s/foo/bar/
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001260
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001261 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001262 char *ls = q; // orig line start
1263 char *found;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001264 vc4:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001265 found = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
1266 if (found) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001267 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001268 // we found the "find" pattern - delete it
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001269 text_hole_delete(found, found + len_F - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001270 // inset the "replace" patern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001271 bias = string_insert(found, R); // insert the string
1272 found += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001273 ls += bias;
1274 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001275 // check for "global" :s/foo/bar/g
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001276 if (gflag == 'g') {
1277 if ((found + len_R) < end_line(ls)) {
1278 q = found + len_R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001279 goto vc4; // don't let q move past cur line
1280 }
1281 }
1282 }
1283 q = next_line(ls);
1284 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001285#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001286 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001287 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001288 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1289 || strncmp(cmd, "wq", i) == 0
1290 || strncmp(cmd, "wn", i) == 0
1291 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001292 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001293 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001294 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001295 fn = args;
1296 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001297#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001298 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001299 status_line_bold("\"%s\" File is read only", fn);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001300 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001301 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001302#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001303 // how many lines in text[]?
1304 li = count_lines(q, r);
1305 ch = r - q + 1;
1306 // see if file exists- if not, its just a new file request
1307 if (useforce) {
1308 // if "fn" is not write-able, chmod u+w
1309 // sprintf(syscmd, "chmod u+w %s", fn);
1310 // system(syscmd);
1311 forced = TRUE;
1312 }
1313 l = file_write(fn, q, r);
1314 if (useforce && forced) {
1315 // chmod u-w
1316 // sprintf(syscmd, "chmod u-w %s", fn);
1317 // system(syscmd);
1318 forced = FALSE;
1319 }
Paul Fox61e45db2005-10-09 14:43:22 +00001320 if (l < 0) {
1321 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001322 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001323 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001324 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001325 if (q == text && r == end - 1 && l == ch) {
1326 file_modified = 0;
1327 last_file_modified = -1;
1328 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001329 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1330 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1331 )
1332 && l == ch
1333 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001334 editing = 0;
1335 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001336 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001337#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001338 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001339 if (b < 0) { // no addr given- use defaults
1340 q = begin_line(dot); // assume .,. for the range
1341 r = end_line(dot);
1342 }
1343 text_yank(q, r, YDreg);
1344 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001345 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001346 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001347#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001348 } else {
1349 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001350 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001351 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001352 ret:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001353 dot = bound_dot(dot); // make sure "dot" is valid
1354 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001355#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001356 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001357 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001358#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001359}
Paul Fox61e45db2005-10-09 14:43:22 +00001360
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001361#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001362
1363static void Hit_Return(void)
1364{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001365 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001366
Denis Vlasenkof882f082007-12-23 02:36:51 +00001367 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001368 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001369 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001370 while ((c = get_one_char()) != '\n' && c != '\r')
1371 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001372 redraw(TRUE); // force redraw all
1373}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001374
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001375static int next_tabstop(int col)
1376{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001377 return col + ((tabstop - 1) - (col % tabstop));
1378}
1379
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001380//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001381static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001382{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001383 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001384 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001385 int cnt, ro, co;
1386
1387 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001388
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001389 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001390 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001391 // how many lines do we have to move
1392 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001393 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001394 screenbegin = beg_cur;
1395 if (cnt > (rows - 1) / 2) {
1396 // we moved too many lines. put "dot" in middle of screen
1397 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1398 screenbegin = prev_line(screenbegin);
1399 }
1400 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001401 } else {
1402 char *end_scr; // begin and end of screen
1403 end_scr = end_screen(); // last char of screen
1404 if (beg_cur > end_scr) {
1405 // "d" is after bottom line on screen
1406 // how many lines do we have to move
1407 cnt = count_lines(end_scr, beg_cur);
1408 if (cnt > (rows - 1) / 2)
1409 goto sc1; // too many lines
1410 for (ro = 0; ro < cnt - 1; ro++) {
1411 // move screen begin the same amount
1412 screenbegin = next_line(screenbegin);
1413 // now, move the end of screen
1414 end_scr = next_line(end_scr);
1415 end_scr = end_line(end_scr);
1416 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001417 }
1418 }
1419 // "d" is on screen- find out which row
1420 tp = screenbegin;
1421 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1422 if (tp == beg_cur)
1423 break;
1424 tp = next_line(tp);
1425 }
1426
1427 // find out what col "d" is on
1428 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001429 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001430 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001431 break;
1432 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001433 // handle tabs like real vi
1434 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001435 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001436 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001437 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001438 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1439 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001440 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001441 co++;
1442 tp++;
1443 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001444
1445 // "co" is the column where "dot" is.
1446 // The screen has "columns" columns.
1447 // The currently displayed columns are 0+offset -- columns+ofset
1448 // |-------------------------------------------------------------|
1449 // ^ ^ ^
1450 // offset | |------- columns ----------------|
1451 //
1452 // If "co" is already in this range then we do not have to adjust offset
1453 // but, we do have to subtract the "offset" bias from "co".
1454 // If "co" is outside this range then we have to change "offset".
1455 // If the first char of a line is a tab the cursor will try to stay
1456 // in column 7, but we have to set offset to 0.
1457
1458 if (co < 0 + offset) {
1459 offset = co;
1460 }
1461 if (co >= columns + offset) {
1462 offset = co - columns + 1;
1463 }
1464 // if the first char of the line is a tab, and "dot" is sitting on it
1465 // force offset to 0.
1466 if (d == beg_cur && *d == '\t') {
1467 offset = 0;
1468 }
1469 co -= offset;
1470
1471 *row = ro;
1472 *col = co;
1473}
1474
1475//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001476static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001477{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001478 if (p > text) {
1479 p = memrchr(text, '\n', p - text);
1480 if (!p)
1481 return text;
1482 return p + 1;
1483 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001484 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001485}
1486
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001487static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001488{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001489 if (p < end - 1) {
1490 p = memchr(p, '\n', end - p - 1);
1491 if (!p)
1492 return end - 1;
1493 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001494 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001495}
1496
Denis Vlasenkof882f082007-12-23 02:36:51 +00001497static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001498{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001499 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001500 // Try to stay off of the Newline
1501 if (*p == '\n' && (p - begin_line(p)) > 0)
1502 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001503 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001504}
1505
Denis Vlasenkof882f082007-12-23 02:36:51 +00001506static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001507{
1508 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001509 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001510 p--; // step to prev line
1511 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001512 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001513}
1514
Denis Vlasenkof882f082007-12-23 02:36:51 +00001515static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001516{
1517 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001518 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001519 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001520 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001521}
1522
1523//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001524static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001525{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001526 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001527 int cnt;
1528
1529 // find new bottom line
1530 q = screenbegin;
1531 for (cnt = 0; cnt < rows - 2; cnt++)
1532 q = next_line(q);
1533 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001534 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001535}
1536
Denis Vlasenkof882f082007-12-23 02:36:51 +00001537// count line from start to stop
1538static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001539{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001540 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001541 int cnt;
1542
Denis Vlasenkof882f082007-12-23 02:36:51 +00001543 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001544 q = start;
1545 start = stop;
1546 stop = q;
1547 }
1548 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001549 stop = end_line(stop);
1550 while (start <= stop && start <= end - 1) {
1551 start = end_line(start);
1552 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001553 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001554 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001555 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001556 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001557}
1558
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001559static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001560{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001561 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001562
1563 for (q = text; li > 1; li--) {
1564 q = next_line(q);
1565 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001566 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001567}
1568
1569//----- Dot Movement Routines ----------------------------------
1570static void dot_left(void)
1571{
1572 if (dot > text && dot[-1] != '\n')
1573 dot--;
1574}
1575
1576static void dot_right(void)
1577{
1578 if (dot < end - 1 && *dot != '\n')
1579 dot++;
1580}
1581
1582static void dot_begin(void)
1583{
1584 dot = begin_line(dot); // return pointer to first char cur line
1585}
1586
1587static void dot_end(void)
1588{
1589 dot = end_line(dot); // return pointer to last char cur line
1590}
1591
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001592static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001593{
1594 int co;
1595
1596 p = begin_line(p);
1597 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001598 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001599 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001600 break;
1601 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001602 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001603 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001604 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001605 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001606 co++;
1607 p++;
1608 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001609 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001610}
1611
1612static void dot_next(void)
1613{
1614 dot = next_line(dot);
1615}
1616
1617static void dot_prev(void)
1618{
1619 dot = prev_line(dot);
1620}
1621
1622static void dot_scroll(int cnt, int dir)
1623{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001624 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001625
1626 for (; cnt > 0; cnt--) {
1627 if (dir < 0) {
1628 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001629 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001630 screenbegin = prev_line(screenbegin);
1631 } else {
1632 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001633 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001634 screenbegin = next_line(screenbegin);
1635 }
1636 }
1637 // make sure "dot" stays on the screen so we dont scroll off
1638 if (dot < screenbegin)
1639 dot = screenbegin;
1640 q = end_screen(); // find new bottom line
1641 if (dot > q)
1642 dot = begin_line(q); // is dot is below bottom line?
1643 dot_skip_over_ws();
1644}
1645
1646static void dot_skip_over_ws(void)
1647{
1648 // skip WS
1649 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1650 dot++;
1651}
1652
1653static void dot_delete(void) // delete the char at 'dot'
1654{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001655 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001656}
1657
Denis Vlasenkof882f082007-12-23 02:36:51 +00001658static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001659{
1660 if (p >= end && end > text) {
1661 p = end - 1;
1662 indicate_error('1');
1663 }
1664 if (p < text) {
1665 p = text;
1666 indicate_error('2');
1667 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001668 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001669}
1670
1671//----- Helper Utility Routines --------------------------------
1672
1673//----------------------------------------------------------------
1674//----- Char Routines --------------------------------------------
1675/* Chars that are part of a word-
1676 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1677 * Chars that are Not part of a word (stoppers)
1678 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1679 * Chars that are WhiteSpace
1680 * TAB NEWLINE VT FF RETURN SPACE
1681 * DO NOT COUNT NEWLINE AS WHITESPACE
1682 */
1683
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001684static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001685{
1686 int li;
1687
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001688 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001689 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001690 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001691 // initialize the new screen. assume this will be a empty file.
1692 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001693 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001694 for (li = 1; li < ro - 1; li++) {
1695 screen[(li * co) + 0] = '~';
1696 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001697 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001698}
1699
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001700#if ENABLE_FEATURE_VI_SEARCH
Walter Harmsb9ba5802011-06-27 02:59:37 +02001701
1702# if ENABLE_FEATURE_VI_REGEX_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001703
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001704// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001705static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001706{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001707 char *q;
1708 struct re_pattern_buffer preg;
1709 int i;
Walter Harmsb9ba5802011-06-27 02:59:37 +02001710 int size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001711
1712 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1713 preg.translate = 0;
1714 preg.fastmap = 0;
1715 preg.buffer = 0;
1716 preg.allocated = 0;
1717
1718 // assume a LIMITED forward search
1719 q = next_line(p);
1720 q = end_line(q);
1721 q = end - 1;
1722 if (dir == BACK) {
1723 q = prev_line(p);
1724 q = text;
1725 }
1726 // count the number of chars to search over, forward or backward
1727 size = q - p;
1728 if (size < 0)
1729 size = p - q;
1730 // RANGE could be negative if we are searching backwards
1731 range = q - p;
1732
Walter Harmsb9ba5802011-06-27 02:59:37 +02001733 q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001734 if (q != 0) {
1735 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001736 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001737 i = 0; // return p if pattern not compiled
1738 goto cs1;
1739 }
1740
1741 q = p;
1742 if (range < 0) {
1743 q = p - size;
1744 if (q < text)
1745 q = text;
1746 }
1747 // search for the compiled pattern, preg, in p[]
1748 // range < 0- search backward
1749 // range > 0- search forward
1750 // 0 < start < size
1751 // re_search() < 0 not found or error
1752 // re_search() > 0 index of found pattern
1753 // struct pattern char int int int struct reg
1754 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1755 i = re_search(&preg, q, size, 0, range, 0);
1756 if (i == -1) {
1757 p = 0;
1758 i = 0; // return NULL if pattern not found
1759 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001760 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001761 if (dir == FORWARD) {
1762 p = p + i;
1763 } else {
1764 p = p - i;
1765 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001766 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001767}
Walter Harmsb9ba5802011-06-27 02:59:37 +02001768
1769# else
1770
1771# if ENABLE_FEATURE_VI_SETOPTS
1772static int mycmp(const char *s1, const char *s2, int len)
1773{
1774 if (ignorecase) {
1775 return strncasecmp(s1, s2, len);
1776 }
1777 return strncmp(s1, s2, len);
1778}
1779# else
1780# define mycmp strncmp
1781# endif
1782
1783static char *char_search(char *p, const char *pat, int dir, int range)
1784{
1785 char *start, *stop;
1786 int len;
1787
1788 len = strlen(pat);
1789 if (dir == FORWARD) {
1790 stop = end - 1; // assume range is p - end-1
1791 if (range == LIMITED)
1792 stop = next_line(p); // range is to next line
1793 for (start = p; start < stop; start++) {
1794 if (mycmp(start, pat, len) == 0) {
1795 return start;
1796 }
1797 }
1798 } else if (dir == BACK) {
1799 stop = text; // assume range is text - p
1800 if (range == LIMITED)
1801 stop = prev_line(p); // range is to prev line
1802 for (start = p - len; start >= stop; start--) {
1803 if (mycmp(start, pat, len) == 0) {
1804 return start;
1805 }
1806 }
1807 }
1808 // pattern not found
1809 return NULL;
1810}
1811
1812# endif
1813
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001814#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001815
Denis Vlasenko33875382008-06-21 20:31:50 +00001816static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001817{
1818 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001819 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001820 refresh(FALSE); // show the ^
1821 c = get_one_char();
1822 *p = c;
1823 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001824 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001825 } else if (c == 27) { // Is this an ESC?
1826 cmd_mode = 0;
1827 cmdcnt = 0;
1828 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001829 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001830 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001831 p--;
1832 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001833 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001834 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001835 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001836 p--;
1837 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001838 }
1839 } else {
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001840#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001841 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001842 char *sp; // "save p"
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001843#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001844
1845 if (c == 13)
1846 c = '\n'; // translate \r to \n
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001847#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001848 sp = p; // remember addr of insert
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001849#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001850 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001851#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001852 if (showmatch && strchr(")]}", *sp) != NULL) {
1853 showmatching(sp);
1854 }
1855 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001856 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001857 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001858 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001859 len = strspn(q, " \t"); // space or tab
1860 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001861 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001862 bias = text_hole_make(p, len);
1863 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001864 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001865 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001866 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001867 }
1868 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001869#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001870 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001871 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001872}
1873
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001874// might reallocate text[]! use p += stupid_insert(p, ...),
1875// and be careful to not use pointers into potentially freed text[]!
1876static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001877{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001878 uintptr_t bias;
1879 bias = text_hole_make(p, 1);
1880 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001881 *p = c;
1882 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001883 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001884}
1885
Denis Vlasenko33875382008-06-21 20:31:50 +00001886static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001887{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001888 char *save_dot, *p, *q, *t;
1889 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001890
1891 save_dot = dot;
1892 p = q = dot;
1893
1894 if (strchr("cdy><", c)) {
1895 // these cmds operate on whole lines
1896 p = q = begin_line(p);
1897 for (cnt = 1; cnt < cmdcnt; cnt++) {
1898 q = next_line(q);
1899 }
1900 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001901 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001902 // These cmds operate on char positions
1903 do_cmd(c); // execute movement cmd
1904 q = dot;
1905 } else if (strchr("wW", c)) {
1906 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001907 // if we are at the next word's first char
1908 // step back one char
1909 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001910 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001911 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1912 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1913 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001914 if (dot > text && *dot == '\n')
1915 dot--; // stay off NL
1916 q = dot;
1917 } else if (strchr("H-k{", c)) {
1918 // these operate on multi-lines backwards
1919 q = end_line(dot); // find NL
1920 do_cmd(c); // execute movement cmd
1921 dot_begin();
1922 p = dot;
1923 } else if (strchr("L+j}\r\n", c)) {
1924 // these operate on multi-lines forwards
1925 p = begin_line(dot);
1926 do_cmd(c); // execute movement cmd
1927 dot_end(); // find NL
1928 q = dot;
1929 } else {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001930 // nothing -- this causes any other values of c to
1931 // represent the one-character range under the
1932 // cursor. this is correct for ' ' and 'l', but
1933 // perhaps no others.
1934 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001935 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001936 if (q < p) {
1937 t = q;
1938 q = p;
1939 p = t;
1940 }
1941
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001942 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001943 if (q > p && strchr("^0bBh\b\177", c)) q--;
1944
1945 multiline = 0;
1946 for (t = p; t <= q; t++) {
1947 if (*t == '\n') {
1948 multiline = 1;
1949 break;
1950 }
1951 }
1952
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001953 *start = p;
1954 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001955 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001956 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001957}
1958
Denis Vlasenko33875382008-06-21 20:31:50 +00001959static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001960{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001961 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001962 int test, inc;
1963
1964 inc = dir;
1965 c = c0 = p[0];
1966 ci = p[inc];
1967 test = 0;
1968
1969 if (type == S_BEFORE_WS) {
1970 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001971 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001972 }
1973 if (type == S_TO_WS) {
1974 c = c0;
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_OVER_WS) {
1978 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001979 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001980 }
1981 if (type == S_END_PUNCT) {
1982 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001983 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001984 }
1985 if (type == S_END_ALNUM) {
1986 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001987 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001988 }
1989 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001990 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001991}
1992
Denis Vlasenko33875382008-06-21 20:31:50 +00001993static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001994{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001995 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001996
1997 while (st_test(p, type, dir, &c)) {
1998 // make sure we limit search to correct number of lines
1999 if (c == '\n' && --linecnt < 1)
2000 break;
2001 if (dir >= 0 && p >= end - 1)
2002 break;
2003 if (dir < 0 && p <= text)
2004 break;
2005 p += dir; // move to next char
2006 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002007 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002008}
2009
2010// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00002011static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002012{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002013 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002014 int dir, level;
2015
2016 match = ')';
2017 level = 1;
2018 dir = 1; // assume forward
2019 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002020 case '(': match = ')'; break;
2021 case '[': match = ']'; break;
2022 case '{': match = '}'; break;
2023 case ')': match = '('; dir = -1; break;
2024 case ']': match = '['; dir = -1; break;
2025 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002026 }
2027 for (q = p + dir; text <= q && q < end; q += dir) {
2028 // look for match, count levels of pairs (( ))
2029 if (*q == c)
2030 level++; // increase pair levels
2031 if (*q == match)
2032 level--; // reduce pair level
2033 if (level == 0)
2034 break; // found matching pair
2035 }
2036 if (level != 0)
2037 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002038 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002039}
2040
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002041#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002042// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00002043static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002044{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002045 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002046
2047 // we found half of a pair
2048 q = find_pair(p, *p); // get loc of matching char
2049 if (q == NULL) {
2050 indicate_error('3'); // no matching char
2051 } else {
2052 // "q" now points to matching pair
2053 save_dot = dot; // remember where we are
2054 dot = q; // go to new loc
2055 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002056 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002057 dot = save_dot; // go back to old loc
2058 refresh(FALSE);
2059 }
2060}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002061#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002062
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002063// open a hole in text[]
2064// might reallocate text[]! use p += text_hole_make(p, ...),
2065// and be careful to not use pointers into potentially freed text[]!
2066static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002067{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002068 uintptr_t bias = 0;
2069
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002070 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002071 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002072 end += size; // adjust the new END
2073 if (end >= (text + text_size)) {
2074 char *new_text;
2075 text_size += end - (text + text_size) + 10240;
2076 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002077 bias = (new_text - text);
2078 screenbegin += bias;
2079 dot += bias;
2080 end += bias;
2081 p += bias;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002082#if ENABLE_FEATURE_VI_YANKMARK
2083 {
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +02002084 unsigned i;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002085 for (i = 0; i < ARRAY_SIZE(mark); i++)
2086 if (mark[i])
2087 mark[i] += bias;
2088 }
2089#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +00002090 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002091 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00002092 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002093 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00002094 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002095 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002096}
2097
2098// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00002099static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002100{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002101 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002102 int cnt, hole_size;
2103
2104 // move forwards, from beginning
2105 // assume p <= q
2106 src = q + 1;
2107 dest = p;
2108 if (q < p) { // they are backward- swap them
2109 src = p + 1;
2110 dest = q;
2111 }
2112 hole_size = q - p + 1;
2113 cnt = end - src;
2114 if (src < text || src > end)
2115 goto thd0;
2116 if (dest < text || dest >= end)
2117 goto thd0;
2118 if (src >= end)
2119 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00002120 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002121 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002122 end = end - hole_size; // adjust the new END
2123 if (dest >= end)
2124 dest = end - 1; // make sure dest in below end-1
2125 if (end <= text)
2126 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00002127 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002128 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002129 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002130}
2131
2132// copy text into register, then delete text.
2133// if dist <= 0, do not include, or go past, a NewLine
2134//
Denis Vlasenko33875382008-06-21 20:31:50 +00002135static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002136{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002137 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002138
2139 // make sure start <= stop
2140 if (start > stop) {
2141 // they are backwards, reverse them
2142 p = start;
2143 start = stop;
2144 stop = p;
2145 }
2146 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00002147 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002148 p = start;
2149 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002150 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002151 // dont go past a NewLine
2152 for (; p + 1 <= stop; p++) {
2153 if (p[1] == '\n') {
2154 stop = p; // "stop" just before NewLine
2155 break;
2156 }
2157 }
2158 }
2159 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002160#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002161 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002162#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002163 if (yf == YANKDEL) {
2164 p = text_hole_delete(start, stop);
2165 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002166 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002167}
2168
2169static void show_help(void)
2170{
2171 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002172#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002173 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002174#endif
2175#if ENABLE_FEATURE_VI_DOT_CMD
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02002176 "\n\tLast command repeat with ."
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002177#endif
2178#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00002179 "\n\tLine marking with 'x"
2180 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002181#endif
2182#if ENABLE_FEATURE_VI_READONLY
Walter Harmsb9ba5802011-06-27 02:59:37 +02002183 //not implemented: "\n\tReadonly if vi is called as \"view\""
2184 //redundant: usage text says this too: "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002185#endif
2186#if ENABLE_FEATURE_VI_SET
Denys Vlasenko5f556fc2012-06-11 01:53:33 +02002187 "\n\tSome colon mode commands with :"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002188#endif
2189#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002190 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002191#endif
2192#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002193 "\n\tSignal catching- ^C"
2194 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002195#endif
2196#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002197 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002198#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002199 );
2200}
2201
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002202#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002203static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002204{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002205 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002206 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002207 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002208 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002209 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002210 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002211 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002212 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002213 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002214}
2215
2216static void end_cmd_q(void)
2217{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002218#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002219 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002220#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002221 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002222}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002223#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002224
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002225#if ENABLE_FEATURE_VI_YANKMARK \
2226 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2227 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002228// might reallocate text[]! use p += string_insert(p, ...),
2229// and be careful to not use pointers into potentially freed text[]!
2230static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002231{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002232 uintptr_t bias;
2233 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002234
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002235 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002236 bias = text_hole_make(p, i);
2237 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002238 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002239#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002240 {
2241 int cnt;
2242 for (cnt = 0; *s != '\0'; s++) {
2243 if (*s == '\n')
2244 cnt++;
2245 }
2246 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2247 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002248#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002249 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002250}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002251#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002252
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002253#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002254static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002255{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002256 int cnt = q - p;
2257 if (cnt < 0) { // they are backwards- reverse them
2258 p = q;
2259 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002260 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002261 free(reg[dest]); // if already a yank register, free it
2262 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002263 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002264}
2265
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002266static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002267{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002268 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002269
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002270 c = 'D'; // default to D-reg
2271 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002272 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002273 if (YDreg == 26)
2274 c = 'D';
2275 if (YDreg == 27)
2276 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002277 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002278}
2279
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002280static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002281{
2282 // A context is defined to be "modifying text"
2283 // Any modifying command establishes a new context.
2284
2285 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002286 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002287 // we are trying to modify text[]- make this the current context
2288 mark[27] = mark[26]; // move cur to prev
2289 mark[26] = dot; // move local to cur
2290 context_start = prev_line(prev_line(dot));
2291 context_end = next_line(next_line(dot));
2292 //loiter= start_loiter= now;
2293 }
2294 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002295}
2296
Denis Vlasenkof882f082007-12-23 02:36:51 +00002297static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002298{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002299 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002300
2301 // the current context is in mark[26]
2302 // the previous context is in mark[27]
2303 // only swap context if other context is valid
2304 if (text <= mark[27] && mark[27] <= end - 1) {
2305 tmp = mark[27];
2306 mark[27] = mark[26];
2307 mark[26] = tmp;
2308 p = mark[26]; // where we are going- previous context
2309 context_start = prev_line(prev_line(prev_line(p)));
2310 context_end = next_line(next_line(next_line(p)));
2311 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002312 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002313}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002314#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002315
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002316//----- Set terminal attributes --------------------------------
2317static void rawmode(void)
2318{
2319 tcgetattr(0, &term_orig);
2320 term_vi = term_orig;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002321 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG on - allow intr's
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002322 term_vi.c_iflag &= (~IXON & ~ICRNL);
2323 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002324 term_vi.c_cc[VMIN] = 1;
2325 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002326 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002327 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002328}
2329
2330static void cookmode(void)
2331{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002332 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002333 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002334}
2335
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002336#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002337//----- Come here when we get a window resize signal ---------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002338static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002339{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002340 int save_errno = errno;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002341 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002342 signal(SIGWINCH, winch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002343 query_screen_dimensions();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002344 new_screen(rows, columns); // get memory for virtual screen
2345 redraw(TRUE); // re-draw the screen
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002346 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002347}
2348
2349//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002350static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002351{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002352 int save_errno = errno;
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002353 rawmode(); // terminal to "raw"
2354 last_status_cksum = 0; // force status update
2355 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002356
2357 signal(SIGTSTP, suspend_sig);
2358 signal(SIGCONT, SIG_DFL);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002359 //kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
2360 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002361}
2362
2363//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002364static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002365{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002366 int save_errno = errno;
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002367 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002368 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002369
2370 signal(SIGCONT, cont_sig);
2371 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002372 kill(my_pid, SIGTSTP);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002373 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002374}
2375
2376//----- Come here when we get a signal ---------------------------
2377static void catch_sig(int sig)
2378{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002379 signal(SIGINT, catch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002380 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002381}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002382#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002383
Denys Vlasenko606291b2009-09-23 23:15:43 +02002384static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002385{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002386 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002387
Denys Vlasenko606291b2009-09-23 23:15:43 +02002388 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002389 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002390 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002391}
2392
2393//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002394static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002395{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002396 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002397
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002398 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002399 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002400 if (c == -1) { // EOF/error
2401 go_bottom_and_clear_to_eol();
2402 cookmode(); // terminal to "cooked"
2403 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002404 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002405 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002406}
2407
2408//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002409static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002410{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002411 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002412
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002413#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002414 if (!adding2q) {
2415 // we are not adding to the q.
2416 // but, we may be reading from a q
2417 if (ioq == 0) {
2418 // there is no current q, read from STDIN
2419 c = readit(); // get the users input
2420 } else {
2421 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002422 // careful with correct sign expansion!
2423 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002424 if (c == '\0') {
2425 // the end of the q, read from STDIN
2426 free(ioq_start);
2427 ioq_start = ioq = 0;
2428 c = readit(); // get the users input
2429 }
2430 }
2431 } else {
2432 // adding STDIN chars to q
2433 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002434 if (lmc_len >= MAX_INPUT_LEN - 1) {
2435 status_line_bold("last_modifying_cmd overrun");
2436 } else {
2437 // add new char to q
2438 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002439 }
2440 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002441#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002442 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002443#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002444 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002445}
2446
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002447// Get input line (uses "status line" area)
2448static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002449{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002450 // char [MAX_INPUT_LEN]
2451#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002452
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002453 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002454 int i;
2455
2456 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002457 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002458 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002459 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002460
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002461 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002462 while (i < MAX_INPUT_LEN) {
2463 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002464 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002465 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002466 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002467 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002468 buf[--i] = '\0';
2469 write1("\b \b"); // erase char on screen
2470 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002471 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002472 } else if (c > 0 && c < 256) { // exclude Unicode
2473 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002474 buf[i] = c;
2475 buf[++i] = '\0';
2476 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002477 }
2478 }
2479 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002480 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002481#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002482}
2483
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002484static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002485{
2486 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002487 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002488
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002489 cnt = -1;
Denys Vlasenkodef47832010-04-18 20:39:41 -07002490 if (fn && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002491 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002492 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002493}
2494
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002495// might reallocate text[]!
2496static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002497{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002498 int cnt = -1;
2499 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002500 struct stat statbuf;
2501
2502 /* Validate file */
2503 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002504 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002505 goto fi0;
2506 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002507 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002508 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002509 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002510 goto fi0;
2511 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002512 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002513 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002514 goto fi0;
2515 }
2516
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002517 // read file to buffer
2518 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002519 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002520 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002521 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002522 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002523 size = statbuf.st_size;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002524 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002525 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002526 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002527 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002528 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002529 } else if (cnt < size) {
2530 // There was a partial read, shrink unused space text[]
2531 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denys Vlasenko6331cf02009-11-13 09:08:27 +01002532 status_line_bold("can't read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002533 }
2534 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002535 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002536 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002537 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002538#if ENABLE_FEATURE_VI_READONLY
2539 if (update_ro_status
2540 && ((access(fn, W_OK) < 0) ||
2541 /* root will always have access()
2542 * so we check fileperms too */
2543 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2544 )
2545 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002546 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002547 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002548#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002549 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002550}
2551
Denis Vlasenko33875382008-06-21 20:31:50 +00002552static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002553{
2554 int fd, cnt, charcnt;
2555
2556 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002557 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002558 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002559 }
Denis Vlasenko8abae882008-05-03 11:35:59 +00002560 /* By popular request we do not open file with O_TRUNC,
2561 * but instead ftruncate() it _after_ successful write.
2562 * Might reduce amount of data lost on power fail etc.
2563 */
2564 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002565 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002566 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002567 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002568 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002569 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002570 if (charcnt == cnt) {
2571 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002572 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002573 } else {
2574 charcnt = 0;
2575 }
2576 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002577 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002578}
2579
2580//----- Terminal Drawing ---------------------------------------
2581// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002582// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002583// screen coordinates
2584// 0,0 ... 0,79
2585// 1,0 ... 1,79
2586// . ... .
2587// . ... .
2588// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002589// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002590
2591//----- Move the cursor to row x col (count from 0, not 1) -------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002592static void place_cursor(int row, int col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002593{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002594 char cm1[sizeof(ESC_SET_CURSOR_POS) + sizeof(int)*3 * 2];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002595
2596 if (row < 0) row = 0;
2597 if (row >= rows) row = rows - 1;
2598 if (col < 0) col = 0;
2599 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002600
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002601 sprintf(cm1, ESC_SET_CURSOR_POS, row + 1, col + 1);
2602 write1(cm1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002603}
2604
2605//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002606static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002607{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002608 write1(ESC_CLEAR2EOL);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002609}
2610
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002611static void go_bottom_and_clear_to_eol(void)
2612{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002613 place_cursor(rows - 1, 0);
2614 clear_to_eol();
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002615}
2616
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002617//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002618static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002619{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002620 write1(ESC_CLEAR2EOS);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002621}
2622
2623//----- Start standout mode ------------------------------------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002624static void standout_start(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002625{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002626 write1(ESC_BOLD_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002627}
2628
2629//----- End standout mode --------------------------------------
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002630static void standout_end(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002631{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002632 write1(ESC_NORM_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002633}
2634
2635//----- Flash the screen --------------------------------------
2636static void flash(int h)
2637{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002638 standout_start();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002639 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002640 mysleep(h);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002641 standout_end();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002642 redraw(TRUE);
2643}
2644
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002645static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002646{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002647#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002648 if (crashme > 0)
2649 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002650#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002651 if (!err_method) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002652 write1(ESC_BELL);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002653 } else {
2654 flash(10);
2655 }
2656}
2657
2658//----- Screen[] Routines --------------------------------------
2659//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002660static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002661{
2662 memset(screen, ' ', screensize); // clear new screen
2663}
2664
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002665static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002666{
2667 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002668 char *e = buf + count;
2669
Paul Fox8552aec2005-09-16 12:20:05 +00002670 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002671 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002672 return sum;
2673}
2674
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002675//----- Draw the status line at bottom of the screen -------------
2676static void show_status_line(void)
2677{
Paul Foxc3504852005-09-16 12:48:18 +00002678 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002679
Paul Fox8552aec2005-09-16 12:20:05 +00002680 // either we already have an error or status message, or we
2681 // create one.
2682 if (!have_status_msg) {
2683 cnt = format_edit_status();
2684 cksum = bufsum(status_buffer, cnt);
2685 }
2686 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002687 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002688 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002689 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002690 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002691 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002692 (columns - 1) ) {
2693 have_status_msg = 0;
2694 Hit_Return();
2695 }
2696 have_status_msg = 0;
2697 }
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002698 place_cursor(crow, ccol); // put cursor back in correct place
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002699 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002700 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002701}
2702
2703//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002704// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002705static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002706{
2707 va_list args;
2708
2709 va_start(args, format);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002710 strcpy(status_buffer, ESC_BOLD_TEXT);
2711 vsprintf(status_buffer + sizeof(ESC_BOLD_TEXT)-1, format, args);
2712 strcat(status_buffer, ESC_NORM_TEXT);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002713 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002714
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002715 have_status_msg = 1 + sizeof(ESC_BOLD_TEXT) + sizeof(ESC_NORM_TEXT) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002716}
2717
Paul Fox8552aec2005-09-16 12:20:05 +00002718// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002719static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002720{
2721 va_list args;
2722
2723 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002724 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002725 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002726
2727 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002728}
2729
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002730// copy s to buf, convert unprintable
2731static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002732{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002733 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002734 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002735
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002736 buf[0] = '\0';
2737 if (!s[0])
2738 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002739
2740 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002741 for (; *s; s++) {
2742 int c_is_no_print;
2743
2744 c = *s;
2745 c_is_no_print = (c & 0x80) && !Isprint(c);
2746 if (c_is_no_print) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002747 strcpy(d, ESC_NORM_TEXT);
2748 d += sizeof(ESC_NORM_TEXT)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002749 c = '.';
2750 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002751 if (c < ' ' || c == 0x7f) {
2752 *d++ = '^';
2753 c |= '@'; /* 0x40 */
2754 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002755 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002756 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002757 *d++ = c;
2758 *d = '\0';
2759 if (c_is_no_print) {
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002760 strcpy(d, ESC_BOLD_TEXT);
2761 d += sizeof(ESC_BOLD_TEXT)-1;
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002762 }
2763 if (*s == '\n') {
2764 *d++ = '$';
2765 *d = '\0';
2766 }
2767 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002768 break;
2769 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002770}
2771
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002772static void not_implemented(const char *s)
2773{
2774 char buf[MAX_INPUT_LEN];
2775
2776 print_literal(buf, s);
2777 status_line_bold("\'%s\' is not implemented", buf);
2778}
2779
2780// show file status on status line
2781static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002782{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002783 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002784
2785#define tot format_edit_status__tot
2786
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002787 int cur, percent, ret, trunc_at;
2788
Paul Fox8552aec2005-09-16 12:20:05 +00002789 // file_modified is now a counter rather than a flag. this
2790 // helps reduce the amount of line counting we need to do.
2791 // (this will cause a mis-reporting of modified status
2792 // once every MAXINT editing operations.)
2793
2794 // it would be nice to do a similar optimization here -- if
2795 // we haven't done a motion that could have changed which line
2796 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002797 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002798
2799 // reduce counting -- the total lines can't have
2800 // changed if we haven't done any edits.
2801 if (file_modified != last_file_modified) {
2802 tot = cur + count_lines(dot, end - 1) - 1;
2803 last_file_modified = file_modified;
2804 }
2805
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002806 // current line percent
2807 // ------------- ~~ ----------
2808 // total lines 100
2809 if (tot > 0) {
2810 percent = (100 * cur) / tot;
2811 } else {
2812 cur = tot = 0;
2813 percent = 100;
2814 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002815
Paul Fox8552aec2005-09-16 12:20:05 +00002816 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2817 columns : STATUS_BUFFER_LEN-1;
2818
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002819 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002820#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002821 "%c %s%s%s %d/%d %d%%",
2822#else
2823 "%c %s%s %d/%d %d%%",
2824#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002825 cmd_mode_indicator[cmd_mode & 3],
2826 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002827#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002828 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002829#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002830 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002831 cur, tot, percent);
2832
2833 if (ret >= 0 && ret < trunc_at)
2834 return ret; /* it all fit */
2835
2836 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002837#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002838}
2839
2840//----- Force refresh of all Lines -----------------------------
2841static void redraw(int full_screen)
2842{
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002843 place_cursor(0, 0);
2844 clear_to_eos();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002845 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002846 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002847 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002848 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002849}
2850
2851//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002852static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002853{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002854 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002855 int co;
2856 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002857 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002858
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002859 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002860 co = 0;
2861 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002862 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002863 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002864 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002865 if (c == '\n')
2866 break;
2867 if ((c & 0x80) && !Isprint(c)) {
2868 c = '.';
2869 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002870 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002871 if (c == '\t') {
2872 c = ' ';
2873 // co % 8 != 7
2874 while ((co % tabstop) != (tabstop - 1)) {
2875 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002876 }
2877 } else {
2878 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002879 if (c == 0x7f)
2880 c = '?';
2881 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002882 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002883 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002884 }
2885 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002886 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002887 // discard scrolled-off-to-the-left portion,
2888 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002889 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002890 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002891 co -= tabstop;
2892 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002893 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002894 if (src >= end)
2895 break;
2896 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002897 // check "short line, gigantic offset" case
2898 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002899 ofs = co;
2900 // discard last scrolled off part
2901 co -= ofs;
2902 dest += ofs;
2903 // fill the rest with spaces
2904 if (co < columns)
2905 memset(&dest[co], ' ', columns - co);
2906 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002907}
2908
2909//----- Refresh the changed screen lines -----------------------
2910// Copy the source line from text[] into the buffer and note
2911// if the current screenline is different from the new buffer.
2912// If they differ then that line needs redrawing on the terminal.
2913//
2914static void refresh(int full_screen)
2915{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002916#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002917
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002918 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002919 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002920
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +02002921 if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002922 unsigned c = columns, r = rows;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002923 query_screen_dimensions();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002924 full_screen |= (c - columns) | (r - rows);
2925 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002926 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2927 tp = screenbegin; // index into text[] of top line
2928
2929 // compare text[] to screen[] and mark screen[] lines that need updating
2930 for (li = 0; li < rows - 1; li++) {
2931 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002932 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002933 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002934 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002935
2936 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002937 if (tp < end) {
2938 char *t = memchr(tp, '\n', end - tp);
2939 if (!t) t = end - 1;
2940 tp = t + 1;
2941 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002942
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002943 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002944 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002945 cs = 0;
2946 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002947 sp = &screen[li * columns]; // start of screen line
2948 if (full_screen) {
2949 // force re-draw of every single column from 0 - columns-1
2950 goto re0;
2951 }
2952 // compare newly formatted buffer with virtual screen
2953 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002954 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002955 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002956 changed = TRUE; // mark for redraw
2957 break;
2958 }
2959 }
2960
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002961 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002962 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002963 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002964 changed = TRUE; // mark for redraw
2965 break;
2966 }
2967 }
2968 // now, cs is index of first diff, and ce is index of last diff
2969
2970 // if horz offset has changed, force a redraw
2971 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002972 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002973 changed = TRUE;
2974 }
2975
2976 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002977 if (cs < 0) cs = 0;
2978 if (ce > columns - 1) ce = columns - 1;
2979 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002980 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002981 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002982 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002983 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002984 place_cursor(li, cs);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002985 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002986 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002987 }
2988 }
2989
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02002990 place_cursor(crow, ccol);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002991
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002992 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002993#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002994}
2995
Eric Andersen3f980402001-04-04 17:31:15 +00002996//---------------------------------------------------------------------
2997//----- the Ascii Chart -----------------------------------------------
2998//
2999// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
3000// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
3001// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
3002// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
3003// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
3004// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
3005// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
3006// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
3007// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
3008// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
3009// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
3010// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
3011// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
3012// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
3013// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
3014// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
3015//---------------------------------------------------------------------
3016
3017//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003018static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00003019{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003020 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003021 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003022 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00003023 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003024 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003025
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00003026// c1 = c; // quiet the compiler
3027// cnt = yf = 0; // quiet the compiler
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003028// p = q = save_dot = buf; // quiet the compiler
3029 memset(buf, '\0', sizeof(buf));
Eric Andersenbff7a602001-11-17 07:15:43 +00003030
Paul Fox8552aec2005-09-16 12:20:05 +00003031 show_status_line();
3032
Eric Andersenbff7a602001-11-17 07:15:43 +00003033 /* if this is a cursor key, skip these checks */
3034 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003035 case KEYCODE_UP:
3036 case KEYCODE_DOWN:
3037 case KEYCODE_LEFT:
3038 case KEYCODE_RIGHT:
3039 case KEYCODE_HOME:
3040 case KEYCODE_END:
3041 case KEYCODE_PAGEUP:
3042 case KEYCODE_PAGEDOWN:
3043 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00003044 goto key_cmd_mode;
3045 }
3046
Eric Andersen3f980402001-04-04 17:31:15 +00003047 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003048 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003049 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003050 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00003051 // we are 'R'eplacing the current *dot with new char
3052 if (*dot == '\n') {
3053 // don't Replace past E-o-l
3054 cmd_mode = 1; // convert to insert
3055 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003056 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003057 if (c != 27)
3058 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3059 dot = char_insert(dot, c); // insert new char
3060 }
3061 goto dc1;
3062 }
3063 }
3064 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003065 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003066 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00003067 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003068 if (1 <= c || Isprint(c)) {
3069 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00003070 }
3071 goto dc1;
3072 }
3073
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003074 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00003075 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00003076 //case 0x01: // soh
3077 //case 0x09: // ht
3078 //case 0x0b: // vt
3079 //case 0x0e: // so
3080 //case 0x0f: // si
3081 //case 0x10: // dle
3082 //case 0x11: // dc1
3083 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003084#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00003085 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00003086 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003087 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003088#endif
Eric Andersen822c3832001-05-07 17:37:43 +00003089 //case 0x16: // syn
3090 //case 0x17: // etb
3091 //case 0x18: // can
3092 //case 0x1c: // fs
3093 //case 0x1d: // gs
3094 //case 0x1e: // rs
3095 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003096 //case '!': // !-
3097 //case '#': // #-
3098 //case '&': // &-
3099 //case '(': // (-
3100 //case ')': // )-
3101 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003102 //case '=': // =-
3103 //case '@': // @-
3104 //case 'F': // F-
3105 //case 'K': // K-
3106 //case 'Q': // Q-
3107 //case 'S': // S-
3108 //case 'T': // T-
3109 //case 'V': // V-
3110 //case '[': // [-
3111 //case '\\': // \-
3112 //case ']': // ]-
3113 //case '_': // _-
3114 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003115 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003116 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003117 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00003118 buf[0] = c;
3119 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003120 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003121 end_cmd_q(); // stop adding to q
3122 case 0x00: // nul- ignore
3123 break;
3124 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003125 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00003126 dot_scroll(rows - 2, -1);
3127 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003128 case 4: // ctrl-D scroll down half screen
3129 dot_scroll((rows - 2) / 2, 1);
3130 break;
3131 case 5: // ctrl-E scroll down one line
3132 dot_scroll(1, 1);
3133 break;
3134 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003135 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003136 dot_scroll(rows - 2, 1);
3137 break;
3138 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00003139 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003140 break;
3141 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003142 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003143 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003144 case 0x7f: // DEL- move left (This may be ERASE char)
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003145 do {
3146 dot_left();
3147 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003148 break;
3149 case 10: // Newline ^J
3150 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003151 case KEYCODE_DOWN: // cursor key Down
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003152 do {
3153 dot_next(); // go to next B-o-l
3154 // try stay in same col
3155 dot = move_to_col(dot, ccol + offset);
3156 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003157 break;
3158 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003159 case 18: // ctrl-R force redraw
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02003160 place_cursor(0, 0);
3161 clear_to_eos();
3162 //mysleep(10); // why???
Eric Andersen3f980402001-04-04 17:31:15 +00003163 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003164 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003165 refresh(TRUE); // this will redraw the entire display
3166 break;
3167 case 13: // Carriage Return ^M
3168 case '+': // +- goto next line
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003169 do {
3170 dot_next();
3171 dot_skip_over_ws();
3172 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003173 break;
3174 case 21: // ctrl-U scroll up half screen
3175 dot_scroll((rows - 2) / 2, -1);
3176 break;
3177 case 25: // ctrl-Y scroll up one line
3178 dot_scroll(1, -1);
3179 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003180 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003181 if (cmd_mode == 0)
3182 indicate_error(c);
3183 cmd_mode = 0; // stop insrting
3184 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003185 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003186 break;
3187 case ' ': // move right
3188 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003189 case KEYCODE_RIGHT: // Cursor Key Right
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003190 do {
3191 dot_right();
3192 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003193 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003194#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003195 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003196 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3197 if ((unsigned)c1 <= 25) { // a-z?
3198 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003199 } else {
3200 indicate_error(c);
3201 }
3202 break;
3203 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003204 c1 = (get_one_char() | 0x20) - 'a';
3205 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003206 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003207 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003208 if (text <= q && q < end) {
3209 dot = q;
3210 dot_begin(); // go to B-o-l
3211 dot_skip_over_ws();
3212 }
3213 } else if (c1 == '\'') { // goto previous context
3214 dot = swap_context(dot); // swap current and previous context
3215 dot_begin(); // go to B-o-l
3216 dot_skip_over_ws();
3217 } else {
3218 indicate_error(c);
3219 }
3220 break;
3221 case 'm': // m- Mark a line
3222 // this is really stupid. If there are any inserts or deletes
3223 // between text[0] and dot then this mark will not point to the
3224 // correct location! It could be off by many lines!
3225 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003226 c1 = (get_one_char() | 0x20) - 'a';
3227 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003228 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003229 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003230 } else {
3231 indicate_error(c);
3232 }
3233 break;
3234 case 'P': // P- Put register before
3235 case 'p': // p- put register after
3236 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003237 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003238 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003239 break;
3240 }
3241 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003242 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003243 if (c == 'P') {
3244 dot_begin(); // putting lines- Put above
3245 }
3246 if (c == 'p') {
3247 // are we putting after very last line?
3248 if (end_line(dot) == (end - 1)) {
3249 dot = end; // force dot to end of text[]
3250 } else {
3251 dot_next(); // next line, then put before
3252 }
3253 }
3254 } else {
3255 if (c == 'p')
3256 dot_right(); // move to right, can move to NL
3257 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003258 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003259 end_cmd_q(); // stop adding to q
3260 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003261 case 'U': // U- Undo; replace current line with original version
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003262 if (reg[Ureg] != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003263 p = begin_line(dot);
3264 q = end_line(dot);
3265 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003266 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003267 dot = p;
3268 dot_skip_over_ws();
3269 }
3270 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003271#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003272 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003273 case KEYCODE_END: // Cursor Key End
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003274 for (;;) {
3275 dot = end_line(dot);
3276 if (--cmdcnt <= 0)
3277 break;
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003278 dot_next();
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003279 }
Eric Andersen3f980402001-04-04 17:31:15 +00003280 break;
3281 case '%': // %- find matching char of pair () [] {}
3282 for (q = dot; q < end && *q != '\n'; q++) {
3283 if (strchr("()[]{}", *q) != NULL) {
3284 // we found half of a pair
3285 p = find_pair(q, *q);
3286 if (p == NULL) {
3287 indicate_error(c);
3288 } else {
3289 dot = p;
3290 }
3291 break;
3292 }
3293 }
3294 if (*q == '\n')
3295 indicate_error(c);
3296 break;
3297 case 'f': // f- forward to a user specified char
3298 last_forward_char = get_one_char(); // get the search char
3299 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003300 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003301 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003302 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003303 case ';': // ;- look at rest of line for last forward char
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003304 do {
3305 if (last_forward_char == 0)
3306 break;
3307 q = dot + 1;
3308 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3309 q++;
3310 }
3311 if (*q == last_forward_char)
3312 dot = q;
3313 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003314 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003315 case ',': // repeat latest 'f' in opposite direction
Paul Foxb5ee8db2008-02-14 01:17:01 +00003316 if (last_forward_char == 0)
3317 break;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003318 do {
3319 q = dot - 1;
3320 while (q >= text && *q != '\n' && *q != last_forward_char) {
3321 q--;
3322 }
3323 if (q >= text && *q == last_forward_char)
3324 dot = q;
3325 } while (--cmdcnt > 0);
Paul Foxb5ee8db2008-02-14 01:17:01 +00003326 break;
3327
Eric Andersen3f980402001-04-04 17:31:15 +00003328 case '-': // -- goto prev line
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003329 do {
3330 dot_prev();
3331 dot_skip_over_ws();
3332 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003333 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003334#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003335 case '.': // .- repeat the last modifying command
3336 // Stuff the last_modifying_cmd back into stdin
3337 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003338 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003339 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003340 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003341 }
3342 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003343#endif
3344#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003345 case '?': // /- search for a pattern
3346 case '/': // /- search for a pattern
3347 buf[0] = c;
3348 buf[1] = '\0';
3349 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003350 if (q[0] && !q[1]) {
3351 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003352 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003353 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003354 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003355 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003356 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003357 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003358 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003359 goto dc3; // now find the pattern
3360 }
3361 // user changed mind and erased the "/"- do nothing
3362 break;
3363 case 'N': // N- backward search for last pattern
Eric Andersen3f980402001-04-04 17:31:15 +00003364 dir = BACK; // assume BACKWARD search
3365 p = dot - 1;
3366 if (last_search_pattern[0] == '?') {
3367 dir = FORWARD;
3368 p = dot + 1;
3369 }
3370 goto dc4; // now search for pattern
3371 break;
3372 case 'n': // n- repeat search for last pattern
3373 // search rest of text[] starting at next char
3374 // if search fails return orignal "p" not the "p+1" address
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003375 do {
3376 const char *msg;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003377 dc3:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003378 dir = FORWARD; // assume FORWARD search
3379 p = dot + 1;
3380 if (last_search_pattern[0] == '?') {
3381 dir = BACK;
3382 p = dot - 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003383 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003384 dc4:
3385 q = char_search(p, last_search_pattern + 1, dir, FULL);
3386 if (q != NULL) {
3387 dot = q; // good search, update "dot"
3388 msg = NULL;
3389 goto dc2;
3390 }
3391 // no pattern found between "dot" and "end"- continue at top
3392 p = text;
3393 if (dir == BACK) {
3394 p = end - 1;
3395 }
3396 q = char_search(p, last_search_pattern + 1, dir, FULL);
3397 if (q != NULL) { // found something
3398 dot = q; // found new pattern- goto it
3399 msg = "search hit BOTTOM, continuing at TOP";
3400 if (dir == BACK) {
3401 msg = "search hit TOP, continuing at BOTTOM";
3402 }
3403 } else {
3404 msg = "Pattern not found";
3405 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003406 dc2:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003407 if (msg)
3408 status_line_bold("%s", msg);
3409 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003410 break;
3411 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003412 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003413 if (q != NULL) { // found blank line
3414 dot = next_line(q); // move to next blank line
3415 }
3416 break;
3417 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003418 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003419 if (q != NULL) { // found blank line
3420 dot = next_line(q); // move to next blank line
3421 }
3422 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003423#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003424 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003425 case '1': // 1-
3426 case '2': // 2-
3427 case '3': // 3-
3428 case '4': // 4-
3429 case '5': // 5-
3430 case '6': // 6-
3431 case '7': // 7-
3432 case '8': // 8-
3433 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003434 if (c == '0' && cmdcnt < 1) {
3435 dot_begin(); // this was a standalone zero
3436 } else {
3437 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3438 }
3439 break;
3440 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003441 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003442#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003443 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003444#else
Eric Andersen822c3832001-05-07 17:37:43 +00003445 if (*p == ':')
3446 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003447 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003448 if (cnt <= 0)
3449 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003450 if (strncmp(p, "quit", cnt) == 0
3451 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003452 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003453 if (file_modified && p[1] != '!') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003454 status_line_bold("No write since last change (:%s! overrides)", p);
Eric Andersen3f980402001-04-04 17:31:15 +00003455 } else {
3456 editing = 0;
3457 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003458 } else if (strncmp(p, "write", cnt) == 0
3459 || strncmp(p, "wq", cnt) == 0
3460 || strncmp(p, "wn", cnt) == 0
3461 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003462 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003463 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003464 if (cnt < 0) {
3465 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003466 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003467 } else {
3468 file_modified = 0;
3469 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003470 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003471 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3472 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3473 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003474 editing = 0;
3475 }
Eric Andersen3f980402001-04-04 17:31:15 +00003476 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003477 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003478 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003479 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003480 dot = find_line(j); // go to line # j
3481 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003482 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003483 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003484 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003485#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003486 break;
3487 case '<': // <- Left shift something
3488 case '>': // >- Right shift something
3489 cnt = count_lines(text, dot); // remember what line we are on
3490 c1 = get_one_char(); // get the type of thing to delete
3491 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003492 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003493 p = begin_line(p);
3494 q = end_line(q);
3495 i = count_lines(p, q); // # of lines we are shifting
3496 for ( ; i > 0; i--, p = next_line(p)) {
3497 if (c == '<') {
3498 // shift left- remove tab or 8 spaces
3499 if (*p == '\t') {
3500 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003501 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003502 } else if (*p == ' ') {
3503 // we should be calculating columns, not just SPACE
3504 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003505 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003506 }
3507 }
3508 } else if (c == '>') {
3509 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003510 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003511 }
3512 }
3513 dot = find_line(cnt); // what line were we on
3514 dot_skip_over_ws();
3515 end_cmd_q(); // stop adding to q
3516 break;
3517 case 'A': // A- append at e-o-l
3518 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003519 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003520 case 'a': // a- append after current char
3521 if (*dot != '\n')
3522 dot++;
3523 goto dc_i;
3524 break;
3525 case 'B': // B- back a blank-delimited Word
3526 case 'E': // E- end of a blank-delimited word
3527 case 'W': // W- forward a blank-delimited word
Eric Andersen3f980402001-04-04 17:31:15 +00003528 dir = FORWARD;
3529 if (c == 'B')
3530 dir = BACK;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003531 do {
3532 if (c == 'W' || isspace(dot[dir])) {
3533 dot = skip_thing(dot, 1, dir, S_TO_WS);
3534 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3535 }
3536 if (c != 'W')
3537 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3538 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003539 break;
3540 case 'C': // C- Change to e-o-l
3541 case 'D': // D- delete to e-o-l
3542 save_dot = dot;
3543 dot = dollar_line(dot); // move to before NL
3544 // copy text into a register and delete
3545 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3546 if (c == 'C')
3547 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003548#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003549 if (c == 'D')
3550 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003551#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003552 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003553 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003554 c1 = get_one_char();
3555 if (c1 != 'g') {
3556 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003557 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003558 buf[2] = '\0';
3559 not_implemented(buf);
3560 break;
3561 }
3562 if (cmdcnt == 0)
3563 cmdcnt = 1;
3564 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003565 case 'G': // G- goto to a line number (default= E-O-F)
3566 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003567 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003568 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003569 }
3570 dot_skip_over_ws();
3571 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003572 case 'H': // H- goto top line on screen
3573 dot = screenbegin;
3574 if (cmdcnt > (rows - 1)) {
3575 cmdcnt = (rows - 1);
3576 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003577 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003578 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003579 }
Eric Andersen3f980402001-04-04 17:31:15 +00003580 dot_skip_over_ws();
3581 break;
3582 case 'I': // I- insert before first non-blank
3583 dot_begin(); // 0
3584 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003585 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003586 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003587 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003588 dc_i:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003589 cmd_mode = 1; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003590 break;
3591 case 'J': // J- join current and next lines together
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003592 do {
3593 dot_end(); // move to NL
3594 if (dot < end - 1) { // make sure not last char in text[]
3595 *dot++ = ' '; // replace NL with space
3596 file_modified++;
3597 while (isblank(*dot)) { // delete leading WS
3598 dot_delete();
3599 }
Eric Andersen3f980402001-04-04 17:31:15 +00003600 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003601 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003602 end_cmd_q(); // stop adding to q
3603 break;
3604 case 'L': // L- goto bottom line on screen
3605 dot = end_screen();
3606 if (cmdcnt > (rows - 1)) {
3607 cmdcnt = (rows - 1);
3608 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003609 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003610 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003611 }
Eric Andersen3f980402001-04-04 17:31:15 +00003612 dot_begin();
3613 dot_skip_over_ws();
3614 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003615 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003616 dot = screenbegin;
3617 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3618 dot = next_line(dot);
3619 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003620 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003621 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003622 p = begin_line(dot);
3623 if (p[-1] == '\n') {
3624 dot_prev();
3625 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3626 dot_end();
3627 dot = char_insert(dot, '\n');
3628 } else {
3629 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003630 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003631 dot_prev(); // -
3632 }
3633 goto dc_i;
3634 break;
3635 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003636 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003637 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003638 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003639 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003640 c = 'x';
3641 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003642 case 'X': // X- delete char before dot
3643 case 'x': // x- delete the current char
3644 case 's': // s- substitute the current char
Eric Andersen3f980402001-04-04 17:31:15 +00003645 dir = 0;
3646 if (c == 'X')
3647 dir = -1;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003648 do {
3649 if (dot[dir] != '\n') {
3650 if (c == 'X')
3651 dot--; // delete prev char
3652 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3653 }
3654 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003655 end_cmd_q(); // stop adding to q
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003656 if (c == 's')
3657 goto dc_i; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003658 break;
3659 case 'Z': // Z- if modified, {write}; exit
3660 // ZZ means to save file (if necessary), then exit
3661 c1 = get_one_char();
3662 if (c1 != 'Z') {
3663 indicate_error(c);
3664 break;
3665 }
Paul Foxf0305b72006-03-28 14:18:21 +00003666 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003667 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003668 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003669 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003670 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003671 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003672 if (cnt < 0) {
3673 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003674 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003675 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003676 editing = 0;
3677 }
3678 } else {
3679 editing = 0;
3680 }
3681 break;
3682 case '^': // ^- move to first non-blank on line
3683 dot_begin();
3684 dot_skip_over_ws();
3685 break;
3686 case 'b': // b- back a word
3687 case 'e': // e- end of word
Eric Andersen3f980402001-04-04 17:31:15 +00003688 dir = FORWARD;
3689 if (c == 'b')
3690 dir = BACK;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003691 do {
3692 if ((dot + dir) < text || (dot + dir) > end - 1)
3693 break;
3694 dot += dir;
3695 if (isspace(*dot)) {
3696 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3697 }
3698 if (isalnum(*dot) || *dot == '_') {
3699 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3700 } else if (ispunct(*dot)) {
3701 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3702 }
3703 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003704 break;
3705 case 'c': // c- change something
3706 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003707#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003708 case 'y': // y- yank something
3709 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003710#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003711 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003712 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003713 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003714#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003715 if (c == 'y' || c == 'Y')
3716 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003717#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003718 c1 = 'y';
3719 if (c != 'Y')
3720 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003721 // determine range, and whether it spans lines
3722 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003723 if (c1 == 27) { // ESC- user changed mind and wants out
3724 c = c1 = 27; // Escape- do nothing
3725 } else if (strchr("wW", c1)) {
3726 if (c == 'c') {
3727 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003728 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003729 if (q <= text || q[-1] == '\n')
3730 break;
3731 q--;
3732 }
3733 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003734 dot = yank_delete(p, q, ml, yf); // delete word
3735 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3736 // partial line copy text into a register and delete
3737 dot = yank_delete(p, q, ml, yf); // delete word
3738 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3739 // whole line copy text into a register and delete
3740 dot = yank_delete(p, q, ml, yf); // delete lines
3741 whole = 1;
3742 } else {
3743 // could not recognize object
3744 c = c1 = 27; // error-
3745 ml = 0;
3746 indicate_error(c);
3747 }
3748 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003749 if (c == 'c') {
3750 dot = char_insert(dot, '\n');
3751 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003752 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003753 dot_prev();
3754 }
3755 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003756 dot_begin();
3757 dot_skip_over_ws();
3758 }
Eric Andersen3f980402001-04-04 17:31:15 +00003759 }
3760 if (c1 != 27) {
3761 // if CHANGING, not deleting, start inserting after the delete
3762 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003763 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003764 goto dc_i; // start inserting
3765 }
3766 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003767 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003768 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003769#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003770 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003771 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003772 }
3773 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003774 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003775 for (cnt = 0; p <= q; p++) {
3776 if (*p == '\n')
3777 cnt++;
3778 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003779 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003780 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003781#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003782 end_cmd_q(); // stop adding to q
3783 }
3784 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003785 }
Eric Andersen3f980402001-04-04 17:31:15 +00003786 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003787 case KEYCODE_UP: // cursor key Up
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003788 do {
3789 dot_prev();
3790 dot = move_to_col(dot, ccol + offset); // try stay in same col
3791 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003792 break;
3793 case 'r': // r- replace the current char with user input
3794 c1 = get_one_char(); // get the replacement char
3795 if (*dot != '\n') {
3796 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003797 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003798 }
3799 end_cmd_q(); // stop adding to q
3800 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003801 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003802 last_forward_char = get_one_char();
3803 do_cmd(';');
3804 if (*dot == last_forward_char)
3805 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003806 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003807 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003808 case 'w': // w- forward a word
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003809 do {
3810 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3811 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3812 } else if (ispunct(*dot)) { // we are on PUNCT
3813 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3814 }
3815 if (dot < end - 1)
3816 dot++; // move over word
3817 if (isspace(*dot)) {
3818 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3819 }
3820 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003821 break;
3822 case 'z': // z-
3823 c1 = get_one_char(); // get the replacement char
3824 cnt = 0;
3825 if (c1 == '.')
3826 cnt = (rows - 2) / 2; // put dot at center
3827 if (c1 == '-')
3828 cnt = rows - 2; // put dot at bottom
3829 screenbegin = begin_line(dot); // start dot at top
3830 dot_scroll(cnt, -1);
3831 break;
3832 case '|': // |- move to column "cmdcnt"
3833 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3834 break;
3835 case '~': // ~- flip the case of letters a-z -> A-Z
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003836 do {
3837 if (islower(*dot)) {
3838 *dot = toupper(*dot);
3839 file_modified++;
3840 } else if (isupper(*dot)) {
3841 *dot = tolower(*dot);
3842 file_modified++;
3843 }
3844 dot_right();
3845 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003846 end_cmd_q(); // stop adding to q
3847 break;
3848 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003849 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003850 dot_begin();
3851 break;
3852 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003853#if 0
3854 case KEYCODE_FUN1: // Function Key F1
3855 case KEYCODE_FUN2: // Function Key F2
3856 case KEYCODE_FUN3: // Function Key F3
3857 case KEYCODE_FUN4: // Function Key F4
3858 case KEYCODE_FUN5: // Function Key F5
3859 case KEYCODE_FUN6: // Function Key F6
3860 case KEYCODE_FUN7: // Function Key F7
3861 case KEYCODE_FUN8: // Function Key F8
3862 case KEYCODE_FUN9: // Function Key F9
3863 case KEYCODE_FUN10: // Function Key F10
3864 case KEYCODE_FUN11: // Function Key F11
3865 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003866 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003867#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003868 }
3869
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003870 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003871 // if text[] just became empty, add back an empty line
3872 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003873 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003874 dot = text;
3875 }
3876 // it is OK for dot to exactly equal to end, otherwise check dot validity
3877 if (dot != end) {
3878 dot = bound_dot(dot); // make sure "dot" is valid
3879 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003880#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003881 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003882#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003883
3884 if (!isdigit(c))
3885 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3886 cnt = dot - begin_line(dot);
3887 // Try to stay off of the Newline
3888 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3889 dot--;
3890}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003891
Paul Fox35e9c5d2008-03-06 16:26:12 +00003892/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003893#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003894static int totalcmds = 0;
3895static int Mp = 85; // Movement command Probability
3896static int Np = 90; // Non-movement command Probability
3897static int Dp = 96; // Delete command Probability
3898static int Ip = 97; // Insert command Probability
3899static int Yp = 98; // Yank command Probability
3900static int Pp = 99; // Put command Probability
3901static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003902static const char chars[20] = "\t012345 abcdABCD-=.$";
3903static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003904 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003905 "broadcast", "the", "emergency", "of",
3906 "system", "quick", "brown", "fox",
3907 "jumped", "over", "lazy", "dogs",
3908 "back", "January", "Febuary", "March"
3909};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003910static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003911 "You should have received a copy of the GNU General Public License\n",
3912 "char c, cm, *cmd, *cmd1;\n",
3913 "generate a command by percentages\n",
3914 "Numbers may be typed as a prefix to some commands.\n",
3915 "Quit, discarding changes!\n",
3916 "Forced write, if permission originally not valid.\n",
3917 "In general, any ex or ed command (such as substitute or delete).\n",
3918 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3919 "Please get w/ me and I will go over it with you.\n",
3920 "The following is a list of scheduled, committed changes.\n",
3921 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3922 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3923 "Any question about transactions please contact Sterling Huxley.\n",
3924 "I will try to get back to you by Friday, December 31.\n",
3925 "This Change will be implemented on Friday.\n",
3926 "Let me know if you have problems accessing this;\n",
3927 "Sterling Huxley recently added you to the access list.\n",
3928 "Would you like to go to lunch?\n",
3929 "The last command will be automatically run.\n",
3930 "This is too much english for a computer geek.\n",
3931};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003932static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003933 "You should have received a copy of the GNU General Public License\n",
3934 "char c, cm, *cmd, *cmd1;\n",
3935 "generate a command by percentages\n",
3936 "Numbers may be typed as a prefix to some commands.\n",
3937 "Quit, discarding changes!\n",
3938 "Forced write, if permission originally not valid.\n",
3939 "In general, any ex or ed command (such as substitute or delete).\n",
3940 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3941 "Please get w/ me and I will go over it with you.\n",
3942 "The following is a list of scheduled, committed changes.\n",
3943 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3944 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3945 "Any question about transactions please contact Sterling Huxley.\n",
3946 "I will try to get back to you by Friday, December 31.\n",
3947 "This Change will be implemented on Friday.\n",
3948 "Let me know if you have problems accessing this;\n",
3949 "Sterling Huxley recently added you to the access list.\n",
3950 "Would you like to go to lunch?\n",
3951 "The last command will be automatically run.\n",
3952 "This is too much english for a computer geek.\n",
3953};
3954
3955// create a random command to execute
3956static void crash_dummy()
3957{
3958 static int sleeptime; // how long to pause between commands
3959 char c, cm, *cmd, *cmd1;
3960 int i, cnt, thing, rbi, startrbi, percent;
3961
3962 // "dot" movement commands
3963 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
3964
3965 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02003966 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003967 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003968 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02003969 readbuffer[0] = 'X';
3970 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003971 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003972 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003973 // generate a command by percentages
3974 percent = (int) lrand48() % 100; // get a number from 0-99
3975 if (percent < Mp) { // Movement commands
3976 // available commands
3977 cmd = cmd1;
3978 M++;
3979 } else if (percent < Np) { // non-movement commands
3980 cmd = "mz<>\'\""; // available commands
3981 N++;
3982 } else if (percent < Dp) { // Delete commands
3983 cmd = "dx"; // available commands
3984 D++;
3985 } else if (percent < Ip) { // Inset commands
3986 cmd = "iIaAsrJ"; // available commands
3987 I++;
3988 } else if (percent < Yp) { // Yank commands
3989 cmd = "yY"; // available commands
3990 Y++;
3991 } else if (percent < Pp) { // Put commands
3992 cmd = "pP"; // available commands
3993 P++;
3994 } else {
3995 // We do not know how to handle this command, try again
3996 U++;
3997 goto cd0;
3998 }
3999 // randomly pick one of the available cmds from "cmd[]"
4000 i = (int) lrand48() % strlen(cmd);
4001 cm = cmd[i];
4002 if (strchr(":\024", cm))
4003 goto cd0; // dont allow colon or ctrl-T commands
4004 readbuffer[rbi++] = cm; // put cmd into input buffer
4005
4006 // now we have the command-
4007 // there are 1, 2, and multi char commands
4008 // find out which and generate the rest of command as necessary
4009 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
4010 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
4011 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
4012 cmd1 = "abcdefghijklmnopqrstuvwxyz";
4013 }
4014 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4015 c = cmd1[thing];
4016 readbuffer[rbi++] = c; // add movement to input buffer
4017 }
4018 if (strchr("iIaAsc", cm)) { // multi-char commands
4019 if (cm == 'c') {
4020 // change some thing
4021 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4022 c = cmd1[thing];
4023 readbuffer[rbi++] = c; // add movement to input buffer
4024 }
4025 thing = (int) lrand48() % 4; // what thing to insert
4026 cnt = (int) lrand48() % 10; // how many to insert
4027 for (i = 0; i < cnt; i++) {
4028 if (thing == 0) { // insert chars
4029 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
4030 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004031 strcat(readbuffer, words[(int) lrand48() % 20]);
4032 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004033 sleeptime = 0; // how fast to type
4034 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004035 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004036 sleeptime = 0; // how fast to type
4037 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004038 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004039 sleeptime = 0; // how fast to type
4040 }
4041 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004042 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004043 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02004044 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004045 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004046 totalcmds++;
4047 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004048 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004049}
4050
4051// test to see if there are any errors
4052static void crash_test()
4053{
4054 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004055
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004056 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004057 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004058
4059 msg[0] = '\0';
4060 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004061 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004062 }
4063 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004064 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004065 }
4066 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004067 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004068 }
4069 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004070 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004071 }
4072 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004073 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004074 }
4075 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004076 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004077 }
4078
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004079 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00004080 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Denys Vlasenko7af5f6b2012-06-11 13:51:38 +02004081 totalcmds, last_input_char, msg, ESC_BOLD_TEXT, ESC_NORM_TEXT);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01004082 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00004083 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004084 if (d[0] == '\n' || d[0] == '\r')
4085 break;
4086 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004087 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004088 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004089 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004090 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004091 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
4092 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
4093 oldtim = tim;
4094 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004095}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00004096#endif