blob: 71d600834060ccfd12bec371e97460557d9dbffd [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.
139//config:
140//config:config FEATURE_VI_OPTIMIZE_CURSOR
141//config: bool "Optimize cursor movement"
142//config: default y
143//config: depends on VI
144//config: help
145//config: This will make the cursor movement faster, but requires more memory
146//config: and it makes the applet a tiny bit larger.
147
148//applet:IF_VI(APPLET(vi, BB_DIR_BIN, BB_SUID_DROP))
149
150//kbuild:lib-$(CONFIG_VI) += vi.o
151
Pere Orga6a3e01d2011-04-01 22:56:30 +0200152//usage:#define vi_trivial_usage
153//usage: "[OPTIONS] [FILE]..."
154//usage:#define vi_full_usage "\n\n"
155//usage: "Edit FILE\n"
Pere Orga6a3e01d2011-04-01 22:56:30 +0200156//usage: IF_FEATURE_VI_COLON(
157//usage: "\n -c Initial command to run ($EXINIT also available)"
158//usage: )
159//usage: IF_FEATURE_VI_READONLY(
160//usage: "\n -R Read-only"
161//usage: )
162//usage: "\n -H Short help regarding available features"
163
Denis Vlasenkob6adbf12007-05-26 19:00:18 +0000164#include "libbb.h"
Denys Vlasenko066f3992011-07-03 03:19:43 +0200165/* Should be after libbb.h: on some systems regex.h needs sys/types.h: */
166#if ENABLE_FEATURE_VI_REGEX_SEARCH
167# include <regex.h>
168#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000169
Paul Fox35e9c5d2008-03-06 16:26:12 +0000170/* the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000171#define ENABLE_FEATURE_VI_CRASHME 0
172
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000173
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000174#if ENABLE_LOCALE_SUPPORT
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000175
176#if ENABLE_FEATURE_VI_8BIT
Denys Vlasenkoc2704542009-11-20 19:14:19 +0100177//FIXME: this does not work properly for Unicode anyway
178# define Isprint(c) (isprint)(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000179#else
Denys Vlasenkoc2704542009-11-20 19:14:19 +0100180# define Isprint(c) isprint_asciionly(c)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000181#endif
182
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000183#else
184
185/* 0x9b is Meta-ESC */
186#if ENABLE_FEATURE_VI_8BIT
Denys Vlasenko066f3992011-07-03 03:19:43 +0200187# define Isprint(c) ((unsigned char)(c) >= ' ' && (c) != 0x7f && (unsigned char)(c) != 0x9b)
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000188#else
Denys Vlasenko066f3992011-07-03 03:19:43 +0200189# define Isprint(c) ((unsigned char)(c) >= ' ' && (unsigned char)(c) < 0x7f)
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +0000190#endif
191
192#endif
193
194
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000195enum {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000196 MAX_TABSTOP = 32, // sanity limit
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000197 // User input len. Need not be extra big.
198 // Lines in file being edited *can* be bigger than this.
199 MAX_INPUT_LEN = 128,
200 // Sanity limits. We have only one buffer of this size.
201 MAX_SCR_COLS = CONFIG_FEATURE_VI_MAX_LEN,
202 MAX_SCR_ROWS = CONFIG_FEATURE_VI_MAX_LEN,
Denis Vlasenkoe8a07882007-06-10 15:08:44 +0000203};
Eric Andersen3f980402001-04-04 17:31:15 +0000204
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000205/* vt102 typical ESC sequence */
206/* terminal standout start/normal ESC sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200207#define SOs "\033[7m"
208#define SOn "\033[0m"
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000209/* terminal bell sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200210#define bell "\007"
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000211/* Clear-end-of-line and Clear-end-of-screen ESC sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200212#define Ceol "\033[K"
213#define Ceos "\033[J"
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000214/* Cursor motion arbitrary destination ESC sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200215#define CMrc "\033[%u;%uH"
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000216/* Cursor motion up and down ESC sequence */
Denys Vlasenkod9a3e892010-05-16 23:42:13 +0200217#define CMup "\033[A"
218#define CMdown "\n"
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000219
Denis Vlasenkoded6ad32008-10-14 12:26:30 +0000220#if ENABLE_FEATURE_VI_DOT_CMD || ENABLE_FEATURE_VI_YANKMARK
221// cmds modifying text[]
222// vda: removed "aAiIs" as they switch us into insert mode
223// and remembering input for replay after them makes no sense
224static const char modifying_cmds[] = "cCdDJoOpPrRxX<>~";
225#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000226
Rob Landleybc68cd12006-03-10 19:22:06 +0000227enum {
228 YANKONLY = FALSE,
229 YANKDEL = TRUE,
230 FORWARD = 1, // code depends on "1" for array index
231 BACK = -1, // code depends on "-1" for array index
232 LIMITED = 0, // how much of text[] in char_search
233 FULL = 1, // how much of text[] in char_search
Eric Andersen3f980402001-04-04 17:31:15 +0000234
Rob Landleybc68cd12006-03-10 19:22:06 +0000235 S_BEFORE_WS = 1, // used in skip_thing() for moving "dot"
236 S_TO_WS = 2, // used in skip_thing() for moving "dot"
237 S_OVER_WS = 3, // used in skip_thing() for moving "dot"
238 S_END_PUNCT = 4, // used in skip_thing() for moving "dot"
Denis Vlasenko8e858e22007-03-07 09:35:43 +0000239 S_END_ALNUM = 5, // used in skip_thing() for moving "dot"
Rob Landleybc68cd12006-03-10 19:22:06 +0000240};
Eric Andersen3f980402001-04-04 17:31:15 +0000241
Denis Vlasenkob1759462008-06-20 20:20:54 +0000242
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000243/* vi.c expects chars to be unsigned. */
244/* busybox build system provides that, but it's better */
245/* to audit and fix the source */
Eric Andersen3f980402001-04-04 17:31:15 +0000246
Denis Vlasenkob1759462008-06-20 20:20:54 +0000247struct globals {
248 /* many references - keep near the top of globals */
249 char *text, *end; // pointers to the user data in memory
250 char *dot; // where all the action takes place
251 int text_size; // size of the allocated buffer
252
253 /* the rest */
254 smallint vi_setops;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000255#define VI_AUTOINDENT 1
256#define VI_SHOWMATCH 2
257#define VI_IGNORECASE 4
258#define VI_ERR_METHOD 8
259#define autoindent (vi_setops & VI_AUTOINDENT)
260#define showmatch (vi_setops & VI_SHOWMATCH )
261#define ignorecase (vi_setops & VI_IGNORECASE)
262/* indicate error with beep or flash */
263#define err_method (vi_setops & VI_ERR_METHOD)
264
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000265#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000266 smallint readonly_mode;
Denis Vlasenko6a2f7f42007-08-16 10:35:17 +0000267#define SET_READONLY_FILE(flags) ((flags) |= 0x01)
268#define SET_READONLY_MODE(flags) ((flags) |= 0x02)
269#define UNSET_READONLY_FILE(flags) ((flags) &= 0xfe)
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000270#else
Denis Vlasenkob1759462008-06-20 20:20:54 +0000271#define SET_READONLY_FILE(flags) ((void)0)
272#define SET_READONLY_MODE(flags) ((void)0)
273#define UNSET_READONLY_FILE(flags) ((void)0)
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000274#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000275
Denis Vlasenkob1759462008-06-20 20:20:54 +0000276 smallint editing; // >0 while we are editing a file
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000277 // [code audit says "can be 0, 1 or 2 only"]
Denis Vlasenkob1759462008-06-20 20:20:54 +0000278 smallint cmd_mode; // 0=command 1=insert 2=replace
279 int file_modified; // buffer contents changed (counter, not flag!)
Denis Vlasenko30cfdf92008-09-21 15:29:29 +0000280 int last_file_modified; // = -1;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000281 int fn_start; // index of first cmd line file name
282 int save_argc; // how many file names on cmd line
283 int cmdcnt; // repetition count
284 unsigned rows, columns; // the terminal screen is this size
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700285#if ENABLE_FEATURE_VI_ASK_TERMINAL
286 int get_rowcol_error;
287#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000288 int crow, ccol; // cursor is on Crow x Ccol
289 int offset; // chars scrolled off the screen to the left
290 int have_status_msg; // is default edit status needed?
291 // [don't make smallint!]
292 int last_status_cksum; // hash of current status line
293 char *current_filename;
294 char *screenbegin; // index into text[], of top line on the screen
295 char *screen; // pointer to the virtual screen buffer
296 int screensize; // and its size
297 int tabstop;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000298 int last_forward_char; // last char searched for with 'f' (int because of Unicode)
Denis Vlasenkob1759462008-06-20 20:20:54 +0000299 char erase_char; // the users erase character
300 char last_input_char; // last char read from user
Denis Vlasenkob1759462008-06-20 20:20:54 +0000301
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000302#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkob1759462008-06-20 20:20:54 +0000303 smallint adding2q; // are we currently adding user input to q
304 int lmc_len; // length of last_modifying_cmd
305 char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000306#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000307#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenkob1759462008-06-20 20:20:54 +0000308 int last_row; // where the cursor was last moved to
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000309#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000310#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkob1759462008-06-20 20:20:54 +0000311 int my_pid;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000312#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000313#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkob1759462008-06-20 20:20:54 +0000314 char *last_search_pattern; // last pattern from a '/' or '?' search
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000315#endif
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000316
Denis Vlasenkob1759462008-06-20 20:20:54 +0000317 /* former statics */
318#if ENABLE_FEATURE_VI_YANKMARK
319 char *edit_file__cur_line;
320#endif
321 int refresh__old_offset;
322 int format_edit_status__tot;
Eric Andersen3f980402001-04-04 17:31:15 +0000323
Denis Vlasenkob1759462008-06-20 20:20:54 +0000324 /* a few references only */
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000325#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000326 int YDreg, Ureg; // default delete register and orig line for "U"
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000327 char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000328 char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
329 char *context_start, *context_end;
330#endif
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000331#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkob1759462008-06-20 20:20:54 +0000332 sigjmp_buf restart; // catch_sig()
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000333#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000334 struct termios term_orig, term_vi; // remember what the cooked mode was
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000335#if ENABLE_FEATURE_VI_COLON
336 char *initial_cmds[3]; // currently 2 entries, NULL terminated
337#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000338 // Should be just enough to hold a key sequence,
Denis Vlasenko25497c12008-10-14 10:25:05 +0000339 // but CRASHME mode uses it as generated command buffer too
340#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko1dfeeeb2008-10-18 19:04:37 +0000341 char readbuffer[128];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000342#else
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000343 char readbuffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000344#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000345#define STATUS_BUFFER_LEN 200
346 char status_buffer[STATUS_BUFFER_LEN]; // messages to the user
347#if ENABLE_FEATURE_VI_DOT_CMD
348 char last_modifying_cmd[MAX_INPUT_LEN]; // last modifying cmd for "."
349#endif
350 char get_input_line__buf[MAX_INPUT_LEN]; /* former static */
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000351
352 char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000353};
354#define G (*ptr_to_globals)
355#define text (G.text )
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000356#define text_size (G.text_size )
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000357#define end (G.end )
358#define dot (G.dot )
359#define reg (G.reg )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000360
361#define vi_setops (G.vi_setops )
362#define editing (G.editing )
363#define cmd_mode (G.cmd_mode )
364#define file_modified (G.file_modified )
365#define last_file_modified (G.last_file_modified )
366#define fn_start (G.fn_start )
367#define save_argc (G.save_argc )
368#define cmdcnt (G.cmdcnt )
369#define rows (G.rows )
370#define columns (G.columns )
371#define crow (G.crow )
372#define ccol (G.ccol )
373#define offset (G.offset )
374#define status_buffer (G.status_buffer )
375#define have_status_msg (G.have_status_msg )
376#define last_status_cksum (G.last_status_cksum )
377#define current_filename (G.current_filename )
378#define screen (G.screen )
379#define screensize (G.screensize )
380#define screenbegin (G.screenbegin )
381#define tabstop (G.tabstop )
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000382#define last_forward_char (G.last_forward_char )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000383#define erase_char (G.erase_char )
384#define last_input_char (G.last_input_char )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000385#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000386#define readonly_mode (G.readonly_mode )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000387#else
388#define readonly_mode 0
389#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000390#define adding2q (G.adding2q )
391#define lmc_len (G.lmc_len )
392#define ioq (G.ioq )
393#define ioq_start (G.ioq_start )
394#define last_row (G.last_row )
395#define my_pid (G.my_pid )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000396#define last_search_pattern (G.last_search_pattern)
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000397
Denis Vlasenkob1759462008-06-20 20:20:54 +0000398#define edit_file__cur_line (G.edit_file__cur_line)
399#define refresh__old_offset (G.refresh__old_offset)
400#define format_edit_status__tot (G.format_edit_status__tot)
401
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000402#define YDreg (G.YDreg )
403#define Ureg (G.Ureg )
404#define mark (G.mark )
405#define context_start (G.context_start )
406#define context_end (G.context_end )
407#define restart (G.restart )
408#define term_orig (G.term_orig )
409#define term_vi (G.term_vi )
410#define initial_cmds (G.initial_cmds )
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000411#define readbuffer (G.readbuffer )
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000412#define scr_out_buf (G.scr_out_buf )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000413#define last_modifying_cmd (G.last_modifying_cmd )
414#define get_input_line__buf (G.get_input_line__buf)
415
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000416#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000417 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkob1759462008-06-20 20:20:54 +0000418 last_file_modified = -1; \
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000419 /* "" but has space for 2 chars: */ \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000420 IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000421} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000422
Denis Vlasenkob1759462008-06-20 20:20:54 +0000423
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000424static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000425static void edit_file(char *); // edit one file
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000426static void do_cmd(int); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000427static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000428static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
429static char *begin_line(char *); // return pointer to cur line B-o-l
430static char *end_line(char *); // return pointer to cur line E-o-l
431static char *prev_line(char *); // return pointer to prev line B-o-l
432static char *next_line(char *); // return pointer to next line B-o-l
433static char *end_screen(void); // get pointer to last char on screen
434static int count_lines(char *, char *); // count line from start to stop
435static char *find_line(int); // find begining of line #li
436static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000437static void dot_left(void); // move dot left- dont leave line
438static void dot_right(void); // move dot right- dont leave line
439static void dot_begin(void); // move dot to B-o-l
440static void dot_end(void); // move dot to E-o-l
441static void dot_next(void); // move dot to next line B-o-l
442static void dot_prev(void); // move dot to prev line B-o-l
443static void dot_scroll(int, int); // move the screen up or down
444static void dot_skip_over_ws(void); // move dot pat WS
445static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000446static char *bound_dot(char *); // make sure text[0] <= P < "end"
447static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000448static char *char_insert(char *, char); // insert the char c at 'p'
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000449// might reallocate text[]! use p += stupid_insert(p, ...),
450// and be careful to not use pointers into potentially freed text[]!
451static uintptr_t stupid_insert(char *, char); // stupidly insert the char c at 'p'
Paul Foxc51fc7b2008-03-06 01:34:23 +0000452static int find_range(char **, char **, char); // return pointers for an object
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000453static int st_test(char *, int, int, char *); // helper for skip_thing()
454static char *skip_thing(char *, int, int, int); // skip some object
455static char *find_pair(char *, char); // find matching pair () [] {}
456static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000457// might reallocate text[]! use p += text_hole_make(p, ...),
458// and be careful to not use pointers into potentially freed text[]!
459static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000460static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000461static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000462static void rawmode(void); // set "raw" mode on tty
463static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000464// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
465static int mysleep(int);
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000466static int readit(void); // read (maybe cursor) key from stdin
467static int get_one_char(void); // read 1 char from stdin
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000468static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000469#if !ENABLE_FEATURE_VI_READONLY
470#define file_insert(fn, p, update_ro_status) file_insert(fn, p)
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000471#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000472// file_insert might reallocate text[]!
473static int file_insert(const char *, char *, int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000474static int file_write(char *, char *, char *);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000475#if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
476#define place_cursor(a, b, optimize) place_cursor(a, b)
477#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000478static void place_cursor(int, int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000479static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000480static void clear_to_eol(void);
481static void clear_to_eos(void);
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000482static void go_bottom_and_clear_to_eol(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000483static void standout_start(void); // send "start reverse video" sequence
484static void standout_end(void); // send "end reverse video" sequence
485static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000486static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000487static void status_line(const char *, ...); // print to status buf
488static void status_line_bold(const char *, ...);
489static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000490static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000491static void redraw(int); // force a full screen refresh
Denis Vlasenko68404f12008-03-17 09:00:54 +0000492static char* format_line(char* /*, int*/);
Eric Andersen3f980402001-04-04 17:31:15 +0000493static void refresh(int); // update the terminal from screen[]
494
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000495static void Indicate_Error(void); // use flash or beep to indicate error
496#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000497static void Hit_Return(void);
498
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000499#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000500static char *char_search(char *, const char *, int, int); // search for pattern starting at p
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000501#endif
502#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000503static char *get_one_address(char *, int *); // get colon addr, if present
504static char *get_address(char *, int *, int *); // get two colon addrs, if present
505static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000506#endif
507#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000508static void winch_sig(int); // catch window size changes
509static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000510static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000511#endif
512#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000513static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000514static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000515#else
516#define end_cmd_q() ((void)0)
517#endif
518#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000519static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000520#endif
521#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000522// might reallocate text[]! use p += string_insert(p, ...),
523// and be careful to not use pointers into potentially freed text[]!
524static uintptr_t string_insert(char *, const char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000525#endif
526#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000527static char *text_yank(char *, char *, int); // save copy of "p" into a register
528static char what_reg(void); // what is letter of current YDreg
529static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000530#endif
531#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000532static void crash_dummy();
533static void crash_test();
534static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000535#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000536
537
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000538static void write1(const char *out)
539{
540 fputs(out, stdout);
541}
542
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000543int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000544int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000545{
Eric Andersend402edf2001-04-04 19:29:48 +0000546 int c;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000547
548 INIT_G();
Eric Andersen3f980402001-04-04 17:31:15 +0000549
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000550#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000551 my_pid = getpid();
552#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000553#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000554 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000555#endif
Denis Vlasenko2414a962007-07-18 22:03:40 +0000556#ifdef NO_SUCH_APPLET_YET
557 /* If we aren't "vi", we are "view" */
558 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000559 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000560 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000561#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000562
Bernhard Reutner-Fischer73f56bb2007-09-22 21:18:46 +0000563 vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenkof9234132007-03-21 00:03:42 +0000564 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000565 // 2- process EXINIT variable from environment
566 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000567#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000568 {
569 char *p = getenv("EXINIT");
570 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000571 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000572 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000573#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000574 while ((c = getopt(argc, argv, "hCRH" IF_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000575 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000576#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000577 case 'C':
578 crashme = 1;
579 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000580#endif
581#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000582 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000583 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000584 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000585#endif
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000586#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000587 case 'c': // cmd line vi command
588 if (*optarg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000589 initial_cmds[initial_cmds[0] != 0] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000590 break;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000591#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000592 case 'H':
Eric Andersen3f980402001-04-04 17:31:15 +0000593 show_help();
Paul Fox35e9c5d2008-03-06 16:26:12 +0000594 /* fall through */
Paul Fox35e9c5d2008-03-06 16:26:12 +0000595 default:
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000596 bb_show_usage();
Eric Andersendd8500b2001-07-02 18:06:14 +0000597 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000598 }
599 }
600
601 // The argv array can be used by the ":next" and ":rewind" commands
602 // save optind.
603 fn_start = optind; // remember first file name for :next and :rew
604 save_argc = argc;
605
606 //----- This is the main file handling loop --------------
Denys Vlasenko04cecd52010-04-16 22:13:55 -0700607 while (1) {
608 edit_file(argv[optind]); /* param might be NULL */
609 if (++optind >= argc)
610 break;
Eric Andersen3f980402001-04-04 17:31:15 +0000611 }
612 //-----------------------------------------------------------
613
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000614 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000615}
616
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000617/* read text from file or create an empty buf */
618/* will also update current_filename */
619static int init_text_buffer(char *fn)
620{
621 int rc;
622 int size = file_size(fn); // file size. -1 means does not exist.
623
624 /* allocate/reallocate text buffer */
625 free(text);
Denis Vlasenkob1759462008-06-20 20:20:54 +0000626 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000627 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000628
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000629 if (fn != current_filename) {
630 free(current_filename);
631 current_filename = xstrdup(fn);
632 }
633 if (size < 0) {
634 // file dont exist. Start empty buf with dummy line
635 char_insert(text, '\n');
636 rc = 0;
637 } else {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000638 rc = file_insert(fn, text, 1);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000639 }
640 file_modified = 0;
641 last_file_modified = -1;
642#if ENABLE_FEATURE_VI_YANKMARK
643 /* init the marks. */
644 memset(mark, 0, sizeof(mark));
645#endif
646 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000647}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000648
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700649#if ENABLE_FEATURE_VI_WIN_RESIZE
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200650static int query_screen_dimensions(void)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700651{
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200652 int err = get_terminal_width_height(STDIN_FILENO, &columns, &rows);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700653 if (rows > MAX_SCR_ROWS)
654 rows = MAX_SCR_ROWS;
655 if (columns > MAX_SCR_COLS)
656 columns = MAX_SCR_COLS;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200657 return err;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700658}
659#else
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200660# define query_screen_dimensions() (0)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700661#endif
662
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000663static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000664{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000665#if ENABLE_FEATURE_VI_YANKMARK
666#define cur_line edit_file__cur_line
667#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000668 int c;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000669#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000670 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000671#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000672
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000673 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000674 rawmode();
675 rows = 24;
676 columns = 80;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200677 IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions();
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700678#if ENABLE_FEATURE_VI_ASK_TERMINAL
679 if (G.get_rowcol_error /* TODO? && no input on stdin */) {
680 uint64_t k;
681 write1("\033[999;999H" "\033[6n");
682 fflush_all();
683 k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100);
684 if ((int32_t)k == KEYCODE_CURSOR_POS) {
685 uint32_t rc = (k >> 32);
686 columns = (rc & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200687 if (columns > MAX_SCR_COLS)
688 columns = MAX_SCR_COLS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700689 rows = ((rc >> 16) & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200690 if (rows > MAX_SCR_ROWS)
691 rows = MAX_SCR_ROWS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700692 }
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700693 }
694#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000695 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000696 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000697
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000698#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000699 YDreg = 26; // default Yank/Delete reg
700 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000701 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000702#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000703
Eric Andersen3f980402001-04-04 17:31:15 +0000704 last_forward_char = last_input_char = '\0';
705 crow = 0;
706 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000707
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000708#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700709 signal(SIGINT, catch_sig);
Eric Andersen3f980402001-04-04 17:31:15 +0000710 signal(SIGWINCH, winch_sig);
711 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000712 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000713 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000714 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000715 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000716#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000717
Eric Andersen3f980402001-04-04 17:31:15 +0000718 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
719 cmdcnt = 0;
720 tabstop = 8;
721 offset = 0; // no horizontal offset
722 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000723#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000724 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000725 ioq = ioq_start = NULL;
726 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000727 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000728#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000729
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000730#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000731 {
732 char *p, *q;
733 int n = 0;
734
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700735 while ((p = initial_cmds[n]) != NULL) {
Denis Vlasenkof9234132007-03-21 00:03:42 +0000736 do {
737 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000738 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000739 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000740 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000741 *p++ = '\0';
742 if (*q)
743 colon(q);
744 } while (p);
745 free(initial_cmds[n]);
746 initial_cmds[n] = NULL;
747 n++;
748 }
749 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000750#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000751 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000752 //------This is the main Vi cmd handling loop -----------------------
753 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000754#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000755 if (crashme > 0) {
756 if ((end - text) > 1) {
757 crash_dummy(); // generate a random command
758 } else {
759 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000760 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
761 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000762 refresh(FALSE);
763 }
764 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000765#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000766 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000767#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000768 // save a copy of the current line- for the 'U" command
769 if (begin_line(dot) != cur_line) {
770 cur_line = begin_line(dot);
771 text_yank(begin_line(dot), end_line(dot), Ureg);
772 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000773#endif
774#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000775 // These are commands that change text[].
776 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000777 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000778 && cmd_mode == 0 // command mode
779 && c > '\0' // exclude NUL and non-ASCII chars
780 && c < 0x7f // (Unicode and such)
781 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000782 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000783 start_new_cmd_q(c);
784 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000785#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000786 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000787
Eric Andersen3f980402001-04-04 17:31:15 +0000788 // poll to see if there is input already waiting. if we are
789 // not able to display output fast enough to keep up, skip
790 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200791 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000792 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000793 refresh(FALSE);
794 show_status_line();
795 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000796#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000797 if (crashme > 0)
798 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000799#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000800 }
801 //-------------------------------------------------------------------
802
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000803 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000804 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000805#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000806}
807
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000808//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000809#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000810static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000811{
812 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000813 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000814 IF_FEATURE_VI_YANKMARK(char c;)
815 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000816
817 *addr = -1; // assume no addr
818 if (*p == '.') { // the current line
819 p++;
820 q = begin_line(dot);
821 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000822 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000823#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000824 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000825 p++;
826 c = tolower(*p);
827 p++;
828 if (c >= 'a' && c <= 'z') {
829 // we have a mark
830 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000831 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000832 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000833 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000834 }
835 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000836 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000837#endif
838#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000839 else if (*p == '/') { // a search pattern
840 q = strchrnul(++p, '/');
841 pat = xstrndup(p, q - p); // save copy of pattern
842 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000843 if (*p == '/')
844 p++;
845 q = char_search(dot, pat, FORWARD, FULL);
846 if (q != NULL) {
847 *addr = count_lines(text, q);
848 }
849 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000850 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000851#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000852 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000853 p++;
854 q = begin_line(end - 1);
855 *addr = count_lines(text, q);
856 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000857 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000858 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000859 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200860 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000861 *addr = -1;
862 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000863 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000864}
865
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000866static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000867{
868 //----- get the address' i.e., 1,3 'a,'b -----
869 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000870 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000871 p++; // skip over leading spaces
872 if (*p == '%') { // alias for 1,$
873 p++;
874 *b = 1;
875 *e = count_lines(text, end-1);
876 goto ga0;
877 }
878 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000879 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000880 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000881 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000882 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000883 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000884 p++;
885 // get SECOND addr, if present
886 p = get_one_address(p, e);
887 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000888 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000889 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000890 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000891 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000892}
893
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000894#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000895static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000896 const char *short_opname, int opt)
897{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000898 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000899 int l = strlen(opname) - 1; /* opname have + ' ' */
900
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200901 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000902 if (strncasecmp(a, opname, l) == 0
903 || strncasecmp(a, short_opname, 2) == 0
904 ) {
905 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000906 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000907 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000908 vi_setops |= opt;
909 }
910}
911#endif
912
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000913// buf must be no longer than MAX_INPUT_LEN!
914static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000915{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000916 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000917 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000918 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000919 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000920
921 // :3154 // if (-e line 3154) goto it else stay put
922 // :4,33w! foo // write a portion of buffer to file "foo"
923 // :w // write all of buffer to current file
924 // :q // quit
925 // :q! // quit- dont care about modified file
926 // :'a,'z!sort -u // filter block through sort
927 // :'f // goto mark "f"
928 // :'fl // list literal the mark "f" line
929 // :.r bar // read file "bar" into buffer before dot
930 // :/123/,/abc/d // delete lines from "123" line to "abc" line
931 // :/xyz/ // goto the "xyz" line
932 // :s/find/replace/ // substitute pattern "find" with "replace"
933 // :!<cmd> // run <cmd> then return
934 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000935
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000936 if (!buf[0])
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200937 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000938 if (*buf == ':')
939 buf++; // move past the ':'
940
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000941 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000942 b = e = -1;
943 q = text; // assume 1,$ for the range
944 r = end - 1;
945 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000946 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000947
948 // look for optional address(es) :. :1 :1,9 :'q,'a :%
949 buf = get_address(buf, &b, &e);
950
951 // remember orig command line
952 orig_buf = buf;
953
954 // get the COMMAND into cmd[]
955 buf1 = cmd;
956 while (*buf != '\0') {
957 if (isspace(*buf))
958 break;
959 *buf1++ = *buf++;
960 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000961 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000962 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000963 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000964 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000965 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000966 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000967 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000968 if (buf1) {
969 useforce = TRUE;
970 *buf1 = '\0'; // get rid of !
971 }
972 if (b >= 0) {
973 // if there is only one addr, then the addr
974 // is the line number of the single line the
975 // user wants. So, reset the end
976 // pointer to point at end of the "b" line
977 q = find_line(b); // what line is #b
978 r = end_line(q);
979 li = 1;
980 }
981 if (e >= 0) {
982 // we were given two addrs. change the
983 // end pointer to the addr given by user.
984 r = find_line(e); // what line is #e
985 r = end_line(r);
986 li = e - b + 1;
987 }
988 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000989 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000990 if (i == 0) { // :123CR goto line #123
991 if (b >= 0) {
992 dot = find_line(b); // what line is #b
993 dot_skip_over_ws();
994 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000995 }
996#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200997 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000998 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000999 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001000 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001001 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001002 retcode = system(orig_buf + 1); // run the cmd
1003 if (retcode)
1004 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001005 rawmode();
1006 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +00001007 }
1008#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001009 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001010 if (b < 0) { // no addr given- use defaults
1011 b = e = count_lines(text, dot);
1012 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001013 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001014 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001015 if (b < 0) { // no addr given- use defaults
1016 q = begin_line(dot); // assume .,. for the range
1017 r = end_line(dot);
1018 }
1019 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
1020 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001021 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001022 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001023 if (file_modified && !useforce) {
1024 status_line_bold("No write since last change (:edit! overrides)");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001025 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001026 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001027 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001028 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001029 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001030 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001031 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001032 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001033 } else {
1034 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001035 status_line_bold("No current filename");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001036 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001037 }
1038
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001039 if (init_text_buffer(fn) < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001040 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001041
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001042#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001043 if (Ureg >= 0 && Ureg < 28 && reg[Ureg] != 0) {
1044 free(reg[Ureg]); // free orig line reg- for 'U'
1045 reg[Ureg]= 0;
1046 }
1047 if (YDreg >= 0 && YDreg < 28 && reg[YDreg] != 0) {
1048 free(reg[YDreg]); // free default yank/delete register
1049 reg[YDreg]= 0;
1050 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001051#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001052 // how many lines in text[]?
1053 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001054 status_line("\"%s\"%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001055 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001056 " %dL, %dC", current_filename,
1057 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001058 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001059 ((readonly_mode) ? " [Readonly]" : ""),
1060 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001061 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001062 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001063 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01001064 status_line_bold("No address allowed on this command");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001065 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001066 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001067 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001068 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001069 free(current_filename);
1070 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001071 } else {
1072 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +00001073 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001074 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001075 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001076 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001077 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001078 cookmode();
1079 show_help();
1080 rawmode();
1081 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001082 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001083 if (b < 0) { // no addr given- use defaults
1084 q = begin_line(dot); // assume .,. for the range
1085 r = end_line(dot);
1086 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001087 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001088 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001089 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001090 int c_is_no_print;
1091
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001092 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +00001093 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001094 if (c_is_no_print) {
1095 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001096 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +00001097 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001098 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001099 write1("$\r");
1100 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001101 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001102 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001103 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001104 else
1105 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001106 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001107 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001108 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001109 standout_end();
1110 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001111 Hit_Return();
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001112 } else if (strncmp(cmd, "quit", i) == 0 // quit
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001113 || strncmp(cmd, "next", i) == 0 // edit next file
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001114 ) {
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001115 int n;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001116 if (useforce) {
1117 // force end of argv list
1118 if (*cmd == 'q') {
1119 optind = save_argc;
1120 }
1121 editing = 0;
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001122 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001123 }
1124 // don't exit if the file been modified
1125 if (file_modified) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001126 status_line_bold("No write since last change (:%s! overrides)",
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001127 (*cmd == 'q' ? "quit" : "next"));
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001128 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001129 }
1130 // are there other file to edit
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001131 n = save_argc - optind - 1;
1132 if (*cmd == 'q' && n > 0) {
1133 status_line_bold("%d more file(s) to edit", n);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001134 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001135 }
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001136 if (*cmd == 'n' && n <= 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001137 status_line_bold("No more files to edit");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001138 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001139 }
1140 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001141 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001142 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001143 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001144 status_line_bold("No filename given");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001145 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001146 }
1147 if (b < 0) { // no addr given- use defaults
1148 q = begin_line(dot); // assume "dot"
1149 }
1150 // read after current line- unless user said ":0r foo"
1151 if (b != 0)
1152 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001153 { // dance around potentially-reallocated text[]
1154 uintptr_t ofs = q - text;
1155 ch = file_insert(fn, q, 0);
1156 q = text + ofs;
1157 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001158 if (ch < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001159 goto ret; // nothing was inserted
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001160 // how many lines in text[]?
1161 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001162 status_line("\"%s\""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001163 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001164 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001165 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001166 li, ch);
1167 if (ch > 0) {
1168 // if the insert is before "dot" then we need to update
1169 if (q <= dot)
1170 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001171 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001172 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001173 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001174 if (file_modified && !useforce) {
1175 status_line_bold("No write since last change (:rewind! overrides)");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001176 } else {
1177 // reset the filenames to edit
1178 optind = fn_start - 1;
1179 editing = 0;
1180 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001181#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001182 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001183#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001184 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001185#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001186 i = 0; // offset into args
Denis Vlasenkof9234132007-03-21 00:03:42 +00001187 // only blank is regarded as args delmiter. What about tab '\t' ?
1188 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001189 // print out values of all options
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001190#if ENABLE_FEATURE_VI_SETOPTS
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001191 status_line_bold(
1192 "%sautoindent "
1193 "%sflash "
1194 "%signorecase "
1195 "%sshowmatch "
1196 "tabstop=%u",
1197 autoindent ? "" : "no",
1198 err_method ? "" : "no",
1199 ignorecase ? "" : "no",
1200 showmatch ? "" : "no",
1201 tabstop
1202 );
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001203#endif
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001204 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001205 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001206#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001207 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001208 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001209 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001210 i = 2; // ":set noautoindent"
1211 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001212 setops(argp, "flash " , i, "fl", VI_ERR_METHOD);
Denis Vlasenkof9234132007-03-21 00:03:42 +00001213 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001214 setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH );
1215 if (strncmp(argp + i, "tabstop=", 8) == 0) {
1216 int t = 0;
1217 sscanf(argp + i+8, "%u", &t);
1218 if (t > 0 && t <= MAX_TABSTOP)
1219 tabstop = t;
Denis Vlasenkof9234132007-03-21 00:03:42 +00001220 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001221 argp = skip_non_whitespace(argp);
1222 argp = skip_whitespace(argp);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001223 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001224#endif /* FEATURE_VI_SETOPTS */
1225#endif /* FEATURE_VI_SET */
1226#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001227 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001228 char *ls, *F, *R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001229 int gflag;
1230
1231 // F points to the "find" pattern
1232 // R points to the "replace" pattern
1233 // replace the cmd line delimiters "/" with NULLs
1234 gflag = 0; // global replace flag
1235 c = orig_buf[1]; // what is the delimiter
1236 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001237 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001238 if (!R)
1239 goto colon_s_fail;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001240 *R++ = '\0'; // terminate "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001241 buf1 = strchr(R, c);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001242 if (!buf1)
1243 goto colon_s_fail;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001244 *buf1++ = '\0'; // terminate "replace"
1245 if (*buf1 == 'g') { // :s/foo/bar/g
1246 buf1++;
1247 gflag++; // turn on gflag
1248 }
1249 q = begin_line(q);
1250 if (b < 0) { // maybe :s/foo/bar/
1251 q = begin_line(dot); // start with cur line
1252 b = count_lines(text, q); // cur line number
1253 }
1254 if (e < 0)
1255 e = b; // maybe :.s/foo/bar/
1256 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
1257 ls = q; // orig line start
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001258 vc4:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001259 buf1 = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001260 if (buf1) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001261 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001262 // we found the "find" pattern - delete it
1263 text_hole_delete(buf1, buf1 + strlen(F) - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001264 // inset the "replace" patern
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001265 bias = string_insert(buf1, R); // insert the string
1266 buf1 += bias;
1267 ls += bias;
1268 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001269 // check for "global" :s/foo/bar/g
1270 if (gflag == 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001271 if ((buf1 + strlen(R)) < end_line(ls)) {
1272 q = buf1 + strlen(R);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001273 goto vc4; // don't let q move past cur line
1274 }
1275 }
1276 }
1277 q = next_line(ls);
1278 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001279#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001280 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001281 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001282 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1283 || strncmp(cmd, "wq", i) == 0
1284 || strncmp(cmd, "wn", i) == 0
1285 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001286 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001287 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001288 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001289 fn = args;
1290 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001291#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001292 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001293 status_line_bold("\"%s\" File is read only", fn);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001294 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001295 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001296#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001297 // how many lines in text[]?
1298 li = count_lines(q, r);
1299 ch = r - q + 1;
1300 // see if file exists- if not, its just a new file request
1301 if (useforce) {
1302 // if "fn" is not write-able, chmod u+w
1303 // sprintf(syscmd, "chmod u+w %s", fn);
1304 // system(syscmd);
1305 forced = TRUE;
1306 }
1307 l = file_write(fn, q, r);
1308 if (useforce && forced) {
1309 // chmod u-w
1310 // sprintf(syscmd, "chmod u-w %s", fn);
1311 // system(syscmd);
1312 forced = FALSE;
1313 }
Paul Fox61e45db2005-10-09 14:43:22 +00001314 if (l < 0) {
1315 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001316 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001317 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001318 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001319 if (q == text && r == end - 1 && l == ch) {
1320 file_modified = 0;
1321 last_file_modified = -1;
1322 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001323 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1324 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1325 )
1326 && l == ch
1327 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001328 editing = 0;
1329 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001330 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001331#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001332 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001333 if (b < 0) { // no addr given- use defaults
1334 q = begin_line(dot); // assume .,. for the range
1335 r = end_line(dot);
1336 }
1337 text_yank(q, r, YDreg);
1338 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001339 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001340 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001341#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001342 } else {
1343 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001344 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001345 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001346 ret:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001347 dot = bound_dot(dot); // make sure "dot" is valid
1348 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001349#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001350 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001351 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001352#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001353}
Paul Fox61e45db2005-10-09 14:43:22 +00001354
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001355#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001356
1357static void Hit_Return(void)
1358{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001359 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001360
Denis Vlasenkof882f082007-12-23 02:36:51 +00001361 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001362 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001363 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001364 while ((c = get_one_char()) != '\n' && c != '\r')
1365 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001366 redraw(TRUE); // force redraw all
1367}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001368
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001369static int next_tabstop(int col)
1370{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001371 return col + ((tabstop - 1) - (col % tabstop));
1372}
1373
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001374//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001375static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001376{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001377 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001378 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001379 int cnt, ro, co;
1380
1381 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001382
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001383 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001384 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001385 // how many lines do we have to move
1386 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001387 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001388 screenbegin = beg_cur;
1389 if (cnt > (rows - 1) / 2) {
1390 // we moved too many lines. put "dot" in middle of screen
1391 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1392 screenbegin = prev_line(screenbegin);
1393 }
1394 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001395 } else {
1396 char *end_scr; // begin and end of screen
1397 end_scr = end_screen(); // last char of screen
1398 if (beg_cur > end_scr) {
1399 // "d" is after bottom line on screen
1400 // how many lines do we have to move
1401 cnt = count_lines(end_scr, beg_cur);
1402 if (cnt > (rows - 1) / 2)
1403 goto sc1; // too many lines
1404 for (ro = 0; ro < cnt - 1; ro++) {
1405 // move screen begin the same amount
1406 screenbegin = next_line(screenbegin);
1407 // now, move the end of screen
1408 end_scr = next_line(end_scr);
1409 end_scr = end_line(end_scr);
1410 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001411 }
1412 }
1413 // "d" is on screen- find out which row
1414 tp = screenbegin;
1415 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1416 if (tp == beg_cur)
1417 break;
1418 tp = next_line(tp);
1419 }
1420
1421 // find out what col "d" is on
1422 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001423 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001424 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001425 break;
1426 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001427 // handle tabs like real vi
1428 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001429 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001430 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001431 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001432 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1433 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001434 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001435 co++;
1436 tp++;
1437 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001438
1439 // "co" is the column where "dot" is.
1440 // The screen has "columns" columns.
1441 // The currently displayed columns are 0+offset -- columns+ofset
1442 // |-------------------------------------------------------------|
1443 // ^ ^ ^
1444 // offset | |------- columns ----------------|
1445 //
1446 // If "co" is already in this range then we do not have to adjust offset
1447 // but, we do have to subtract the "offset" bias from "co".
1448 // If "co" is outside this range then we have to change "offset".
1449 // If the first char of a line is a tab the cursor will try to stay
1450 // in column 7, but we have to set offset to 0.
1451
1452 if (co < 0 + offset) {
1453 offset = co;
1454 }
1455 if (co >= columns + offset) {
1456 offset = co - columns + 1;
1457 }
1458 // if the first char of the line is a tab, and "dot" is sitting on it
1459 // force offset to 0.
1460 if (d == beg_cur && *d == '\t') {
1461 offset = 0;
1462 }
1463 co -= offset;
1464
1465 *row = ro;
1466 *col = co;
1467}
1468
1469//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001470static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001471{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001472 if (p > text) {
1473 p = memrchr(text, '\n', p - text);
1474 if (!p)
1475 return text;
1476 return p + 1;
1477 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001478 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001479}
1480
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001481static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001482{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001483 if (p < end - 1) {
1484 p = memchr(p, '\n', end - p - 1);
1485 if (!p)
1486 return end - 1;
1487 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001488 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001489}
1490
Denis Vlasenkof882f082007-12-23 02:36:51 +00001491static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001492{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001493 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001494 // Try to stay off of the Newline
1495 if (*p == '\n' && (p - begin_line(p)) > 0)
1496 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001497 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001498}
1499
Denis Vlasenkof882f082007-12-23 02:36:51 +00001500static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001501{
1502 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001503 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001504 p--; // step to prev line
1505 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001506 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001507}
1508
Denis Vlasenkof882f082007-12-23 02:36:51 +00001509static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001510{
1511 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001512 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001513 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001514 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001515}
1516
1517//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001518static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001519{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001520 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001521 int cnt;
1522
1523 // find new bottom line
1524 q = screenbegin;
1525 for (cnt = 0; cnt < rows - 2; cnt++)
1526 q = next_line(q);
1527 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001528 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001529}
1530
Denis Vlasenkof882f082007-12-23 02:36:51 +00001531// count line from start to stop
1532static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001533{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001534 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001535 int cnt;
1536
Denis Vlasenkof882f082007-12-23 02:36:51 +00001537 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001538 q = start;
1539 start = stop;
1540 stop = q;
1541 }
1542 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001543 stop = end_line(stop);
1544 while (start <= stop && start <= end - 1) {
1545 start = end_line(start);
1546 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001547 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001548 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001549 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001550 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001551}
1552
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001553static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001554{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001555 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001556
1557 for (q = text; li > 1; li--) {
1558 q = next_line(q);
1559 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001560 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001561}
1562
1563//----- Dot Movement Routines ----------------------------------
1564static void dot_left(void)
1565{
1566 if (dot > text && dot[-1] != '\n')
1567 dot--;
1568}
1569
1570static void dot_right(void)
1571{
1572 if (dot < end - 1 && *dot != '\n')
1573 dot++;
1574}
1575
1576static void dot_begin(void)
1577{
1578 dot = begin_line(dot); // return pointer to first char cur line
1579}
1580
1581static void dot_end(void)
1582{
1583 dot = end_line(dot); // return pointer to last char cur line
1584}
1585
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001586static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001587{
1588 int co;
1589
1590 p = begin_line(p);
1591 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001592 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001593 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001594 break;
1595 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001596 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001597 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001598 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001599 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001600 co++;
1601 p++;
1602 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001603 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001604}
1605
1606static void dot_next(void)
1607{
1608 dot = next_line(dot);
1609}
1610
1611static void dot_prev(void)
1612{
1613 dot = prev_line(dot);
1614}
1615
1616static void dot_scroll(int cnt, int dir)
1617{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001618 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001619
1620 for (; cnt > 0; cnt--) {
1621 if (dir < 0) {
1622 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001623 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001624 screenbegin = prev_line(screenbegin);
1625 } else {
1626 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001627 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001628 screenbegin = next_line(screenbegin);
1629 }
1630 }
1631 // make sure "dot" stays on the screen so we dont scroll off
1632 if (dot < screenbegin)
1633 dot = screenbegin;
1634 q = end_screen(); // find new bottom line
1635 if (dot > q)
1636 dot = begin_line(q); // is dot is below bottom line?
1637 dot_skip_over_ws();
1638}
1639
1640static void dot_skip_over_ws(void)
1641{
1642 // skip WS
1643 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1644 dot++;
1645}
1646
1647static void dot_delete(void) // delete the char at 'dot'
1648{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001649 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001650}
1651
Denis Vlasenkof882f082007-12-23 02:36:51 +00001652static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001653{
1654 if (p >= end && end > text) {
1655 p = end - 1;
1656 indicate_error('1');
1657 }
1658 if (p < text) {
1659 p = text;
1660 indicate_error('2');
1661 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001662 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001663}
1664
1665//----- Helper Utility Routines --------------------------------
1666
1667//----------------------------------------------------------------
1668//----- Char Routines --------------------------------------------
1669/* Chars that are part of a word-
1670 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1671 * Chars that are Not part of a word (stoppers)
1672 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1673 * Chars that are WhiteSpace
1674 * TAB NEWLINE VT FF RETURN SPACE
1675 * DO NOT COUNT NEWLINE AS WHITESPACE
1676 */
1677
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001678static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001679{
1680 int li;
1681
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001682 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001683 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001684 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001685 // initialize the new screen. assume this will be a empty file.
1686 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001687 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001688 for (li = 1; li < ro - 1; li++) {
1689 screen[(li * co) + 0] = '~';
1690 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001691 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001692}
1693
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001694#if ENABLE_FEATURE_VI_SEARCH
Walter Harmsb9ba5802011-06-27 02:59:37 +02001695
1696# if ENABLE_FEATURE_VI_REGEX_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001697
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001698// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001699static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001700{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001701 char *q;
1702 struct re_pattern_buffer preg;
1703 int i;
Walter Harmsb9ba5802011-06-27 02:59:37 +02001704 int size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001705
1706 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1707 preg.translate = 0;
1708 preg.fastmap = 0;
1709 preg.buffer = 0;
1710 preg.allocated = 0;
1711
1712 // assume a LIMITED forward search
1713 q = next_line(p);
1714 q = end_line(q);
1715 q = end - 1;
1716 if (dir == BACK) {
1717 q = prev_line(p);
1718 q = text;
1719 }
1720 // count the number of chars to search over, forward or backward
1721 size = q - p;
1722 if (size < 0)
1723 size = p - q;
1724 // RANGE could be negative if we are searching backwards
1725 range = q - p;
1726
Walter Harmsb9ba5802011-06-27 02:59:37 +02001727 q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001728 if (q != 0) {
1729 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001730 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001731 i = 0; // return p if pattern not compiled
1732 goto cs1;
1733 }
1734
1735 q = p;
1736 if (range < 0) {
1737 q = p - size;
1738 if (q < text)
1739 q = text;
1740 }
1741 // search for the compiled pattern, preg, in p[]
1742 // range < 0- search backward
1743 // range > 0- search forward
1744 // 0 < start < size
1745 // re_search() < 0 not found or error
1746 // re_search() > 0 index of found pattern
1747 // struct pattern char int int int struct reg
1748 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1749 i = re_search(&preg, q, size, 0, range, 0);
1750 if (i == -1) {
1751 p = 0;
1752 i = 0; // return NULL if pattern not found
1753 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001754 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001755 if (dir == FORWARD) {
1756 p = p + i;
1757 } else {
1758 p = p - i;
1759 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001760 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001761}
Walter Harmsb9ba5802011-06-27 02:59:37 +02001762
1763# else
1764
1765# if ENABLE_FEATURE_VI_SETOPTS
1766static int mycmp(const char *s1, const char *s2, int len)
1767{
1768 if (ignorecase) {
1769 return strncasecmp(s1, s2, len);
1770 }
1771 return strncmp(s1, s2, len);
1772}
1773# else
1774# define mycmp strncmp
1775# endif
1776
1777static char *char_search(char *p, const char *pat, int dir, int range)
1778{
1779 char *start, *stop;
1780 int len;
1781
1782 len = strlen(pat);
1783 if (dir == FORWARD) {
1784 stop = end - 1; // assume range is p - end-1
1785 if (range == LIMITED)
1786 stop = next_line(p); // range is to next line
1787 for (start = p; start < stop; start++) {
1788 if (mycmp(start, pat, len) == 0) {
1789 return start;
1790 }
1791 }
1792 } else if (dir == BACK) {
1793 stop = text; // assume range is text - p
1794 if (range == LIMITED)
1795 stop = prev_line(p); // range is to prev line
1796 for (start = p - len; start >= stop; start--) {
1797 if (mycmp(start, pat, len) == 0) {
1798 return start;
1799 }
1800 }
1801 }
1802 // pattern not found
1803 return NULL;
1804}
1805
1806# endif
1807
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001808#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001809
Denis Vlasenko33875382008-06-21 20:31:50 +00001810static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001811{
1812 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001813 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001814 refresh(FALSE); // show the ^
1815 c = get_one_char();
1816 *p = c;
1817 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001818 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001819 } else if (c == 27) { // Is this an ESC?
1820 cmd_mode = 0;
1821 cmdcnt = 0;
1822 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001823 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001824 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001825 p--;
1826 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001827 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001828 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001829 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001830 p--;
1831 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001832 }
1833 } else {
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001834#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001835 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001836 char *sp; // "save p"
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001837#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001838
1839 if (c == 13)
1840 c = '\n'; // translate \r to \n
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001841#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001842 sp = p; // remember addr of insert
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001843#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001844 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001845#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001846 if (showmatch && strchr(")]}", *sp) != NULL) {
1847 showmatching(sp);
1848 }
1849 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001850 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001851 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001852 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001853 len = strspn(q, " \t"); // space or tab
1854 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001855 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001856 bias = text_hole_make(p, len);
1857 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001858 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001859 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001860 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001861 }
1862 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001863#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001864 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001865 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001866}
1867
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001868// might reallocate text[]! use p += stupid_insert(p, ...),
1869// and be careful to not use pointers into potentially freed text[]!
1870static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001871{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001872 uintptr_t bias;
1873 bias = text_hole_make(p, 1);
1874 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001875 *p = c;
1876 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001877 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001878}
1879
Denis Vlasenko33875382008-06-21 20:31:50 +00001880static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001881{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001882 char *save_dot, *p, *q, *t;
1883 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001884
1885 save_dot = dot;
1886 p = q = dot;
1887
1888 if (strchr("cdy><", c)) {
1889 // these cmds operate on whole lines
1890 p = q = begin_line(p);
1891 for (cnt = 1; cnt < cmdcnt; cnt++) {
1892 q = next_line(q);
1893 }
1894 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001895 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001896 // These cmds operate on char positions
1897 do_cmd(c); // execute movement cmd
1898 q = dot;
1899 } else if (strchr("wW", c)) {
1900 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001901 // if we are at the next word's first char
1902 // step back one char
1903 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001904 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001905 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1906 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1907 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001908 if (dot > text && *dot == '\n')
1909 dot--; // stay off NL
1910 q = dot;
1911 } else if (strchr("H-k{", c)) {
1912 // these operate on multi-lines backwards
1913 q = end_line(dot); // find NL
1914 do_cmd(c); // execute movement cmd
1915 dot_begin();
1916 p = dot;
1917 } else if (strchr("L+j}\r\n", c)) {
1918 // these operate on multi-lines forwards
1919 p = begin_line(dot);
1920 do_cmd(c); // execute movement cmd
1921 dot_end(); // find NL
1922 q = dot;
1923 } else {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001924 // nothing -- this causes any other values of c to
1925 // represent the one-character range under the
1926 // cursor. this is correct for ' ' and 'l', but
1927 // perhaps no others.
1928 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001929 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001930 if (q < p) {
1931 t = q;
1932 q = p;
1933 p = t;
1934 }
1935
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001936 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001937 if (q > p && strchr("^0bBh\b\177", c)) q--;
1938
1939 multiline = 0;
1940 for (t = p; t <= q; t++) {
1941 if (*t == '\n') {
1942 multiline = 1;
1943 break;
1944 }
1945 }
1946
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001947 *start = p;
1948 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001949 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001950 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001951}
1952
Denis Vlasenko33875382008-06-21 20:31:50 +00001953static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001954{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001955 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001956 int test, inc;
1957
1958 inc = dir;
1959 c = c0 = p[0];
1960 ci = p[inc];
1961 test = 0;
1962
1963 if (type == S_BEFORE_WS) {
1964 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001965 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001966 }
1967 if (type == S_TO_WS) {
1968 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001969 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001970 }
1971 if (type == S_OVER_WS) {
1972 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001973 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001974 }
1975 if (type == S_END_PUNCT) {
1976 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001977 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001978 }
1979 if (type == S_END_ALNUM) {
1980 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001981 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001982 }
1983 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001984 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001985}
1986
Denis Vlasenko33875382008-06-21 20:31:50 +00001987static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001988{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001989 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001990
1991 while (st_test(p, type, dir, &c)) {
1992 // make sure we limit search to correct number of lines
1993 if (c == '\n' && --linecnt < 1)
1994 break;
1995 if (dir >= 0 && p >= end - 1)
1996 break;
1997 if (dir < 0 && p <= text)
1998 break;
1999 p += dir; // move to next char
2000 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002001 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002002}
2003
2004// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00002005static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002006{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002007 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002008 int dir, level;
2009
2010 match = ')';
2011 level = 1;
2012 dir = 1; // assume forward
2013 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002014 case '(': match = ')'; break;
2015 case '[': match = ']'; break;
2016 case '{': match = '}'; break;
2017 case ')': match = '('; dir = -1; break;
2018 case ']': match = '['; dir = -1; break;
2019 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002020 }
2021 for (q = p + dir; text <= q && q < end; q += dir) {
2022 // look for match, count levels of pairs (( ))
2023 if (*q == c)
2024 level++; // increase pair levels
2025 if (*q == match)
2026 level--; // reduce pair level
2027 if (level == 0)
2028 break; // found matching pair
2029 }
2030 if (level != 0)
2031 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002032 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002033}
2034
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002035#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002036// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00002037static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002038{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002039 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002040
2041 // we found half of a pair
2042 q = find_pair(p, *p); // get loc of matching char
2043 if (q == NULL) {
2044 indicate_error('3'); // no matching char
2045 } else {
2046 // "q" now points to matching pair
2047 save_dot = dot; // remember where we are
2048 dot = q; // go to new loc
2049 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002050 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002051 dot = save_dot; // go back to old loc
2052 refresh(FALSE);
2053 }
2054}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002055#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002056
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002057// open a hole in text[]
2058// might reallocate text[]! use p += text_hole_make(p, ...),
2059// and be careful to not use pointers into potentially freed text[]!
2060static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002061{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002062 uintptr_t bias = 0;
2063
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002064 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002065 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002066 end += size; // adjust the new END
2067 if (end >= (text + text_size)) {
2068 char *new_text;
2069 text_size += end - (text + text_size) + 10240;
2070 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002071 bias = (new_text - text);
2072 screenbegin += bias;
2073 dot += bias;
2074 end += bias;
2075 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002076 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002077 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00002078 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002079 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00002080 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002081 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002082}
2083
2084// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00002085static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002086{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002087 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002088 int cnt, hole_size;
2089
2090 // move forwards, from beginning
2091 // assume p <= q
2092 src = q + 1;
2093 dest = p;
2094 if (q < p) { // they are backward- swap them
2095 src = p + 1;
2096 dest = q;
2097 }
2098 hole_size = q - p + 1;
2099 cnt = end - src;
2100 if (src < text || src > end)
2101 goto thd0;
2102 if (dest < text || dest >= end)
2103 goto thd0;
2104 if (src >= end)
2105 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00002106 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002107 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002108 end = end - hole_size; // adjust the new END
2109 if (dest >= end)
2110 dest = end - 1; // make sure dest in below end-1
2111 if (end <= text)
2112 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00002113 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002114 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002115 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002116}
2117
2118// copy text into register, then delete text.
2119// if dist <= 0, do not include, or go past, a NewLine
2120//
Denis Vlasenko33875382008-06-21 20:31:50 +00002121static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002122{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002123 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002124
2125 // make sure start <= stop
2126 if (start > stop) {
2127 // they are backwards, reverse them
2128 p = start;
2129 start = stop;
2130 stop = p;
2131 }
2132 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00002133 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002134 p = start;
2135 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002136 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002137 // dont go past a NewLine
2138 for (; p + 1 <= stop; p++) {
2139 if (p[1] == '\n') {
2140 stop = p; // "stop" just before NewLine
2141 break;
2142 }
2143 }
2144 }
2145 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002146#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002147 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002148#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002149 if (yf == YANKDEL) {
2150 p = text_hole_delete(start, stop);
2151 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002152 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002153}
2154
2155static void show_help(void)
2156{
2157 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002158#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002159 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002160#endif
2161#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002162 "\n\tLast command repeat with \'.\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002163#endif
2164#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00002165 "\n\tLine marking with 'x"
2166 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002167#endif
2168#if ENABLE_FEATURE_VI_READONLY
Walter Harmsb9ba5802011-06-27 02:59:37 +02002169 //not implemented: "\n\tReadonly if vi is called as \"view\""
2170 //redundant: usage text says this too: "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002171#endif
2172#if ENABLE_FEATURE_VI_SET
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002173 "\n\tSome colon mode commands with \':\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002174#endif
2175#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002176 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002177#endif
2178#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002179 "\n\tSignal catching- ^C"
2180 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002181#endif
2182#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002183 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002184#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002185 );
2186}
2187
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002188#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002189static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002190{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002191 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002192 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002193 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002194 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002195 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002196 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002197 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002198 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002199 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002200}
2201
2202static void end_cmd_q(void)
2203{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002204#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002205 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002206#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002207 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002208}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002209#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002210
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002211#if ENABLE_FEATURE_VI_YANKMARK \
2212 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2213 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002214// might reallocate text[]! use p += string_insert(p, ...),
2215// and be careful to not use pointers into potentially freed text[]!
2216static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002217{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002218 uintptr_t bias;
2219 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002220
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002221 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002222 bias = text_hole_make(p, i);
2223 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002224 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002225#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002226 {
2227 int cnt;
2228 for (cnt = 0; *s != '\0'; s++) {
2229 if (*s == '\n')
2230 cnt++;
2231 }
2232 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2233 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002234#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002235 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002236}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002237#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002238
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002239#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002240static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002241{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002242 int cnt = q - p;
2243 if (cnt < 0) { // they are backwards- reverse them
2244 p = q;
2245 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002246 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002247 free(reg[dest]); // if already a yank register, free it
2248 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002249 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002250}
2251
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002252static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002253{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002254 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002255
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002256 c = 'D'; // default to D-reg
2257 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002258 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002259 if (YDreg == 26)
2260 c = 'D';
2261 if (YDreg == 27)
2262 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002263 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002264}
2265
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002266static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002267{
2268 // A context is defined to be "modifying text"
2269 // Any modifying command establishes a new context.
2270
2271 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002272 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002273 // we are trying to modify text[]- make this the current context
2274 mark[27] = mark[26]; // move cur to prev
2275 mark[26] = dot; // move local to cur
2276 context_start = prev_line(prev_line(dot));
2277 context_end = next_line(next_line(dot));
2278 //loiter= start_loiter= now;
2279 }
2280 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002281}
2282
Denis Vlasenkof882f082007-12-23 02:36:51 +00002283static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002284{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002285 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002286
2287 // the current context is in mark[26]
2288 // the previous context is in mark[27]
2289 // only swap context if other context is valid
2290 if (text <= mark[27] && mark[27] <= end - 1) {
2291 tmp = mark[27];
2292 mark[27] = mark[26];
2293 mark[26] = tmp;
2294 p = mark[26]; // where we are going- previous context
2295 context_start = prev_line(prev_line(prev_line(p)));
2296 context_end = next_line(next_line(next_line(p)));
2297 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002298 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002299}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002300#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002301
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002302//----- Set terminal attributes --------------------------------
2303static void rawmode(void)
2304{
2305 tcgetattr(0, &term_orig);
2306 term_vi = term_orig;
2307 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG ON- allow intr's
2308 term_vi.c_iflag &= (~IXON & ~ICRNL);
2309 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002310 term_vi.c_cc[VMIN] = 1;
2311 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002312 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002313 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002314}
2315
2316static void cookmode(void)
2317{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002318 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002319 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002320}
2321
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002322#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002323//----- Come here when we get a window resize signal ---------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002324static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002325{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002326 int save_errno = errno;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002327 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002328 signal(SIGWINCH, winch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002329 query_screen_dimensions();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002330 new_screen(rows, columns); // get memory for virtual screen
2331 redraw(TRUE); // re-draw the screen
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002332 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002333}
2334
2335//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002336static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002337{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002338 int save_errno = errno;
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002339 rawmode(); // terminal to "raw"
2340 last_status_cksum = 0; // force status update
2341 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002342
2343 signal(SIGTSTP, suspend_sig);
2344 signal(SIGCONT, SIG_DFL);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002345 //kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
2346 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002347}
2348
2349//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002350static void suspend_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 Vlasenko267e16c2008-10-14 10:34:41 +00002353 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002354 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002355
2356 signal(SIGCONT, cont_sig);
2357 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002358 kill(my_pid, SIGTSTP);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002359 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002360}
2361
2362//----- Come here when we get a signal ---------------------------
2363static void catch_sig(int sig)
2364{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002365 signal(SIGINT, catch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002366 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002367}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002368#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002369
Denys Vlasenko606291b2009-09-23 23:15:43 +02002370static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002371{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002372 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002373
Denys Vlasenko606291b2009-09-23 23:15:43 +02002374 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002375 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002376 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002377}
2378
2379//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002380static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002381{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002382 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002383
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002384 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002385 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002386 if (c == -1) { // EOF/error
2387 go_bottom_and_clear_to_eol();
2388 cookmode(); // terminal to "cooked"
2389 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002390 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002391 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002392}
2393
2394//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002395static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002396{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002397 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002398
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002399#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002400 if (!adding2q) {
2401 // we are not adding to the q.
2402 // but, we may be reading from a q
2403 if (ioq == 0) {
2404 // there is no current q, read from STDIN
2405 c = readit(); // get the users input
2406 } else {
2407 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002408 // careful with correct sign expansion!
2409 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002410 if (c == '\0') {
2411 // the end of the q, read from STDIN
2412 free(ioq_start);
2413 ioq_start = ioq = 0;
2414 c = readit(); // get the users input
2415 }
2416 }
2417 } else {
2418 // adding STDIN chars to q
2419 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002420 if (lmc_len >= MAX_INPUT_LEN - 1) {
2421 status_line_bold("last_modifying_cmd overrun");
2422 } else {
2423 // add new char to q
2424 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002425 }
2426 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002427#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002428 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002429#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002430 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002431}
2432
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002433// Get input line (uses "status line" area)
2434static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002435{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002436 // char [MAX_INPUT_LEN]
2437#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002438
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002439 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002440 int i;
2441
2442 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002443 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002444 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002445 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002446
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002447 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002448 while (i < MAX_INPUT_LEN) {
2449 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002450 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002451 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002452 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002453 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002454 buf[--i] = '\0';
2455 write1("\b \b"); // erase char on screen
2456 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002457 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002458 } else if (c > 0 && c < 256) { // exclude Unicode
2459 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002460 buf[i] = c;
2461 buf[++i] = '\0';
2462 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002463 }
2464 }
2465 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002466 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002467#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002468}
2469
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002470static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002471{
2472 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002473 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002474
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002475 cnt = -1;
Denys Vlasenkodef47832010-04-18 20:39:41 -07002476 if (fn && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002477 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002478 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002479}
2480
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002481// might reallocate text[]!
2482static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002483{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002484 int cnt = -1;
2485 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002486 struct stat statbuf;
2487
2488 /* Validate file */
2489 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002490 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002491 goto fi0;
2492 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002493 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002494 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002495 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002496 goto fi0;
2497 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002498 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002499 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002500 goto fi0;
2501 }
2502
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002503 // read file to buffer
2504 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002505 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002506 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002507 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002508 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002509 size = statbuf.st_size;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002510 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002511 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002512 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002513 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002514 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002515 } else if (cnt < size) {
2516 // There was a partial read, shrink unused space text[]
2517 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denys Vlasenko6331cf02009-11-13 09:08:27 +01002518 status_line_bold("can't read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002519 }
2520 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002521 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002522 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002523 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002524#if ENABLE_FEATURE_VI_READONLY
2525 if (update_ro_status
2526 && ((access(fn, W_OK) < 0) ||
2527 /* root will always have access()
2528 * so we check fileperms too */
2529 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2530 )
2531 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002532 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002533 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002534#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002535 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002536}
2537
Denis Vlasenko33875382008-06-21 20:31:50 +00002538static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002539{
2540 int fd, cnt, charcnt;
2541
2542 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002543 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002544 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002545 }
Denis Vlasenko8abae882008-05-03 11:35:59 +00002546 /* By popular request we do not open file with O_TRUNC,
2547 * but instead ftruncate() it _after_ successful write.
2548 * Might reduce amount of data lost on power fail etc.
2549 */
2550 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002551 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002552 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002553 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002554 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002555 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002556 if (charcnt == cnt) {
2557 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002558 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002559 } else {
2560 charcnt = 0;
2561 }
2562 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002563 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002564}
2565
2566//----- Terminal Drawing ---------------------------------------
2567// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002568// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002569// screen coordinates
2570// 0,0 ... 0,79
2571// 1,0 ... 1,79
2572// . ... .
2573// . ... .
2574// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002575// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002576
2577//----- Move the cursor to row x col (count from 0, not 1) -------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002578static void place_cursor(int row, int col, int optimize)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002579{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002580 char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
Denis Vlasenko7b54dc72008-07-17 21:32:32 +00002581#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2582 enum {
2583 SZ_UP = sizeof(CMup),
2584 SZ_DN = sizeof(CMdown),
2585 SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN,
2586 };
2587 char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size
2588#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002589 char *cm;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002590
2591 if (row < 0) row = 0;
2592 if (row >= rows) row = rows - 1;
2593 if (col < 0) col = 0;
2594 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002595
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002596 //----- 1. Try the standard terminal ESC sequence
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002597 sprintf(cm1, CMrc, row + 1, col + 1);
2598 cm = cm1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002599
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002600#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002601 if (optimize && col < 16) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002602 char *screenp;
2603 int Rrow = last_row;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002604 int diff = Rrow - row;
2605
2606 if (diff < -5 || diff > 5)
2607 goto skip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002608
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002609 //----- find the minimum # of chars to move cursor -------------
2610 //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
2611 cm2[0] = '\0';
2612
2613 // move to the correct row
2614 while (row < Rrow) {
2615 // the cursor has to move up
2616 strcat(cm2, CMup);
2617 Rrow--;
2618 }
2619 while (row > Rrow) {
2620 // the cursor has to move down
2621 strcat(cm2, CMdown);
2622 Rrow++;
2623 }
2624
2625 // now move to the correct column
2626 strcat(cm2, "\r"); // start at col 0
2627 // just send out orignal source char to get to correct place
2628 screenp = &screen[row * columns]; // start of screen line
2629 strncat(cm2, screenp, col);
2630
2631 // pick the shortest cursor motion to send out
2632 if (strlen(cm2) < strlen(cm)) {
2633 cm = cm2;
2634 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002635 skip: ;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002636 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002637 last_row = row;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002638#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002639 write1(cm);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002640}
2641
2642//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002643static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002644{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002645 write1(Ceol); // Erase from cursor to end of line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002646}
2647
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002648static void go_bottom_and_clear_to_eol(void)
2649{
2650 place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
2651 clear_to_eol(); // erase to end of line
2652}
2653
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002654//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002655static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002656{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002657 write1(Ceos); // Erase from cursor to end of screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002658}
2659
2660//----- Start standout mode ------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002661static void standout_start(void) // send "start reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002662{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002663 write1(SOs); // Start reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002664}
2665
2666//----- End standout mode --------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002667static void standout_end(void) // send "end reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002668{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002669 write1(SOn); // End reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002670}
2671
2672//----- Flash the screen --------------------------------------
2673static void flash(int h)
2674{
2675 standout_start(); // send "start reverse video" sequence
2676 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002677 mysleep(h);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002678 standout_end(); // send "end reverse video" sequence
2679 redraw(TRUE);
2680}
2681
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002682static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002683{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002684#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002685 if (crashme > 0)
2686 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002687#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002688 if (!err_method) {
2689 write1(bell); // send out a bell character
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002690 } else {
2691 flash(10);
2692 }
2693}
2694
2695//----- Screen[] Routines --------------------------------------
2696//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002697static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002698{
2699 memset(screen, ' ', screensize); // clear new screen
2700}
2701
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002702static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002703{
2704 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002705 char *e = buf + count;
2706
Paul Fox8552aec2005-09-16 12:20:05 +00002707 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002708 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002709 return sum;
2710}
2711
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002712//----- Draw the status line at bottom of the screen -------------
2713static void show_status_line(void)
2714{
Paul Foxc3504852005-09-16 12:48:18 +00002715 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002716
Paul Fox8552aec2005-09-16 12:20:05 +00002717 // either we already have an error or status message, or we
2718 // create one.
2719 if (!have_status_msg) {
2720 cnt = format_edit_status();
2721 cksum = bufsum(status_buffer, cnt);
2722 }
2723 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002724 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002725 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002726 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002727 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002728 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002729 (columns - 1) ) {
2730 have_status_msg = 0;
2731 Hit_Return();
2732 }
2733 have_status_msg = 0;
2734 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002735 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2736 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002737 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002738}
2739
2740//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002741// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002742static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002743{
2744 va_list args;
2745
2746 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002747 strcpy(status_buffer, SOs); // Terminal standout mode on
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002748 vsprintf(status_buffer + sizeof(SOs)-1, format, args);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002749 strcat(status_buffer, SOn); // Terminal standout mode off
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002750 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002751
2752 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002753}
2754
Paul Fox8552aec2005-09-16 12:20:05 +00002755// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002756static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002757{
2758 va_list args;
2759
2760 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002761 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002762 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002763
2764 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002765}
2766
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002767// copy s to buf, convert unprintable
2768static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002769{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002770 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002771 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002772
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002773 buf[0] = '\0';
2774 if (!s[0])
2775 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002776
2777 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002778 for (; *s; s++) {
2779 int c_is_no_print;
2780
2781 c = *s;
2782 c_is_no_print = (c & 0x80) && !Isprint(c);
2783 if (c_is_no_print) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002784 strcpy(d, SOn);
2785 d += sizeof(SOn)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002786 c = '.';
2787 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002788 if (c < ' ' || c == 0x7f) {
2789 *d++ = '^';
2790 c |= '@'; /* 0x40 */
2791 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002792 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002793 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002794 *d++ = c;
2795 *d = '\0';
2796 if (c_is_no_print) {
2797 strcpy(d, SOs);
2798 d += sizeof(SOs)-1;
2799 }
2800 if (*s == '\n') {
2801 *d++ = '$';
2802 *d = '\0';
2803 }
2804 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002805 break;
2806 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002807}
2808
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002809static void not_implemented(const char *s)
2810{
2811 char buf[MAX_INPUT_LEN];
2812
2813 print_literal(buf, s);
2814 status_line_bold("\'%s\' is not implemented", buf);
2815}
2816
2817// show file status on status line
2818static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002819{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002820 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002821
2822#define tot format_edit_status__tot
2823
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002824 int cur, percent, ret, trunc_at;
2825
Paul Fox8552aec2005-09-16 12:20:05 +00002826 // file_modified is now a counter rather than a flag. this
2827 // helps reduce the amount of line counting we need to do.
2828 // (this will cause a mis-reporting of modified status
2829 // once every MAXINT editing operations.)
2830
2831 // it would be nice to do a similar optimization here -- if
2832 // we haven't done a motion that could have changed which line
2833 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002834 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002835
2836 // reduce counting -- the total lines can't have
2837 // changed if we haven't done any edits.
2838 if (file_modified != last_file_modified) {
2839 tot = cur + count_lines(dot, end - 1) - 1;
2840 last_file_modified = file_modified;
2841 }
2842
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002843 // current line percent
2844 // ------------- ~~ ----------
2845 // total lines 100
2846 if (tot > 0) {
2847 percent = (100 * cur) / tot;
2848 } else {
2849 cur = tot = 0;
2850 percent = 100;
2851 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002852
Paul Fox8552aec2005-09-16 12:20:05 +00002853 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2854 columns : STATUS_BUFFER_LEN-1;
2855
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002856 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002857#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002858 "%c %s%s%s %d/%d %d%%",
2859#else
2860 "%c %s%s %d/%d %d%%",
2861#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002862 cmd_mode_indicator[cmd_mode & 3],
2863 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002864#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002865 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002866#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002867 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002868 cur, tot, percent);
2869
2870 if (ret >= 0 && ret < trunc_at)
2871 return ret; /* it all fit */
2872
2873 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002874#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002875}
2876
2877//----- Force refresh of all Lines -----------------------------
2878static void redraw(int full_screen)
2879{
2880 place_cursor(0, 0, FALSE); // put cursor in correct place
Denis Vlasenkob1759462008-06-20 20:20:54 +00002881 clear_to_eos(); // tell terminal to erase display
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002882 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002883 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002884 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002885 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002886}
2887
2888//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002889static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002890{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002891 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002892 int co;
2893 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002894 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002895
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002896 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002897 co = 0;
2898 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002899 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002900 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002901 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002902 if (c == '\n')
2903 break;
2904 if ((c & 0x80) && !Isprint(c)) {
2905 c = '.';
2906 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002907 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002908 if (c == '\t') {
2909 c = ' ';
2910 // co % 8 != 7
2911 while ((co % tabstop) != (tabstop - 1)) {
2912 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002913 }
2914 } else {
2915 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002916 if (c == 0x7f)
2917 c = '?';
2918 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002919 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002920 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002921 }
2922 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002923 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002924 // discard scrolled-off-to-the-left portion,
2925 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002926 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002927 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002928 co -= tabstop;
2929 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002930 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002931 if (src >= end)
2932 break;
2933 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002934 // check "short line, gigantic offset" case
2935 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002936 ofs = co;
2937 // discard last scrolled off part
2938 co -= ofs;
2939 dest += ofs;
2940 // fill the rest with spaces
2941 if (co < columns)
2942 memset(&dest[co], ' ', columns - co);
2943 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002944}
2945
2946//----- Refresh the changed screen lines -----------------------
2947// Copy the source line from text[] into the buffer and note
2948// if the current screenline is different from the new buffer.
2949// If they differ then that line needs redrawing on the terminal.
2950//
2951static void refresh(int full_screen)
2952{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002953#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002954
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002955 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002956 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002957
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +02002958 if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002959 unsigned c = columns, r = rows;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002960 query_screen_dimensions();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002961 full_screen |= (c - columns) | (r - rows);
2962 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002963 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2964 tp = screenbegin; // index into text[] of top line
2965
2966 // compare text[] to screen[] and mark screen[] lines that need updating
2967 for (li = 0; li < rows - 1; li++) {
2968 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002969 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002970 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002971 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002972
2973 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002974 if (tp < end) {
2975 char *t = memchr(tp, '\n', end - tp);
2976 if (!t) t = end - 1;
2977 tp = t + 1;
2978 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002979
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002980 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002981 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002982 cs = 0;
2983 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002984 sp = &screen[li * columns]; // start of screen line
2985 if (full_screen) {
2986 // force re-draw of every single column from 0 - columns-1
2987 goto re0;
2988 }
2989 // compare newly formatted buffer with virtual screen
2990 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002991 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002992 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002993 changed = TRUE; // mark for redraw
2994 break;
2995 }
2996 }
2997
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002998 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002999 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003000 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003001 changed = TRUE; // mark for redraw
3002 break;
3003 }
3004 }
3005 // now, cs is index of first diff, and ce is index of last diff
3006
3007 // if horz offset has changed, force a redraw
3008 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003009 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003010 changed = TRUE;
3011 }
3012
3013 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003014 if (cs < 0) cs = 0;
3015 if (ce > columns - 1) ce = columns - 1;
3016 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003017 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003018 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003019 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003020 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003021
3022 // move cursor to column of first change
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003023 //if (offset != old_offset) {
3024 // // place_cursor is still too stupid
3025 // // to handle offsets correctly
3026 // place_cursor(li, cs, FALSE);
3027 //} else {
3028 place_cursor(li, cs, TRUE);
3029 //}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003030
3031 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003032 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003033 }
3034 }
3035
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003036 place_cursor(crow, ccol, TRUE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003037
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003038 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003039#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003040}
3041
Eric Andersen3f980402001-04-04 17:31:15 +00003042//---------------------------------------------------------------------
3043//----- the Ascii Chart -----------------------------------------------
3044//
3045// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
3046// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
3047// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
3048// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
3049// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
3050// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
3051// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
3052// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
3053// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
3054// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
3055// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
3056// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
3057// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
3058// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
3059// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
3060// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
3061//---------------------------------------------------------------------
3062
3063//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003064static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00003065{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003066 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003067 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003068 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00003069 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003070 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003071
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00003072// c1 = c; // quiet the compiler
3073// cnt = yf = 0; // quiet the compiler
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003074// p = q = save_dot = buf; // quiet the compiler
3075 memset(buf, '\0', sizeof(buf));
Eric Andersenbff7a602001-11-17 07:15:43 +00003076
Paul Fox8552aec2005-09-16 12:20:05 +00003077 show_status_line();
3078
Eric Andersenbff7a602001-11-17 07:15:43 +00003079 /* if this is a cursor key, skip these checks */
3080 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003081 case KEYCODE_UP:
3082 case KEYCODE_DOWN:
3083 case KEYCODE_LEFT:
3084 case KEYCODE_RIGHT:
3085 case KEYCODE_HOME:
3086 case KEYCODE_END:
3087 case KEYCODE_PAGEUP:
3088 case KEYCODE_PAGEDOWN:
3089 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00003090 goto key_cmd_mode;
3091 }
3092
Eric Andersen3f980402001-04-04 17:31:15 +00003093 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003094 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003095 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003096 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00003097 // we are 'R'eplacing the current *dot with new char
3098 if (*dot == '\n') {
3099 // don't Replace past E-o-l
3100 cmd_mode = 1; // convert to insert
3101 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003102 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003103 if (c != 27)
3104 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3105 dot = char_insert(dot, c); // insert new char
3106 }
3107 goto dc1;
3108 }
3109 }
3110 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003111 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003112 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00003113 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003114 if (1 <= c || Isprint(c)) {
3115 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00003116 }
3117 goto dc1;
3118 }
3119
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003120 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00003121 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00003122 //case 0x01: // soh
3123 //case 0x09: // ht
3124 //case 0x0b: // vt
3125 //case 0x0e: // so
3126 //case 0x0f: // si
3127 //case 0x10: // dle
3128 //case 0x11: // dc1
3129 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003130#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00003131 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00003132 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003133 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003134#endif
Eric Andersen822c3832001-05-07 17:37:43 +00003135 //case 0x16: // syn
3136 //case 0x17: // etb
3137 //case 0x18: // can
3138 //case 0x1c: // fs
3139 //case 0x1d: // gs
3140 //case 0x1e: // rs
3141 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003142 //case '!': // !-
3143 //case '#': // #-
3144 //case '&': // &-
3145 //case '(': // (-
3146 //case ')': // )-
3147 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003148 //case '=': // =-
3149 //case '@': // @-
3150 //case 'F': // F-
3151 //case 'K': // K-
3152 //case 'Q': // Q-
3153 //case 'S': // S-
3154 //case 'T': // T-
3155 //case 'V': // V-
3156 //case '[': // [-
3157 //case '\\': // \-
3158 //case ']': // ]-
3159 //case '_': // _-
3160 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003161 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003162 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003163 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00003164 buf[0] = c;
3165 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003166 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003167 end_cmd_q(); // stop adding to q
3168 case 0x00: // nul- ignore
3169 break;
3170 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003171 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00003172 dot_scroll(rows - 2, -1);
3173 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003174 case 4: // ctrl-D scroll down half screen
3175 dot_scroll((rows - 2) / 2, 1);
3176 break;
3177 case 5: // ctrl-E scroll down one line
3178 dot_scroll(1, 1);
3179 break;
3180 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003181 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003182 dot_scroll(rows - 2, 1);
3183 break;
3184 case 7: // ctrl-G show current status
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 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003188 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003189 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003190 case 0x7f: // DEL- move left (This may be ERASE char)
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003191 do {
3192 dot_left();
3193 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003194 break;
3195 case 10: // Newline ^J
3196 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003197 case KEYCODE_DOWN: // cursor key Down
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003198 do {
3199 dot_next(); // go to next B-o-l
3200 // try stay in same col
3201 dot = move_to_col(dot, ccol + offset);
3202 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003203 break;
3204 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003205 case 18: // ctrl-R force redraw
Eric Andersen822c3832001-05-07 17:37:43 +00003206 place_cursor(0, 0, FALSE); // put cursor in correct place
Eric Andersen3f980402001-04-04 17:31:15 +00003207 clear_to_eos(); // tel terminal to erase display
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003208 mysleep(10);
Eric Andersen3f980402001-04-04 17:31:15 +00003209 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003210 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003211 refresh(TRUE); // this will redraw the entire display
3212 break;
3213 case 13: // Carriage Return ^M
3214 case '+': // +- goto next line
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003215 do {
3216 dot_next();
3217 dot_skip_over_ws();
3218 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003219 break;
3220 case 21: // ctrl-U scroll up half screen
3221 dot_scroll((rows - 2) / 2, -1);
3222 break;
3223 case 25: // ctrl-Y scroll up one line
3224 dot_scroll(1, -1);
3225 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003226 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003227 if (cmd_mode == 0)
3228 indicate_error(c);
3229 cmd_mode = 0; // stop insrting
3230 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003231 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003232 break;
3233 case ' ': // move right
3234 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003235 case KEYCODE_RIGHT: // Cursor Key Right
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003236 do {
3237 dot_right();
3238 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003239 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003240#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003241 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003242 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3243 if ((unsigned)c1 <= 25) { // a-z?
3244 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003245 } else {
3246 indicate_error(c);
3247 }
3248 break;
3249 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003250 c1 = (get_one_char() | 0x20) - 'a';
3251 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003252 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003253 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003254 if (text <= q && q < end) {
3255 dot = q;
3256 dot_begin(); // go to B-o-l
3257 dot_skip_over_ws();
3258 }
3259 } else if (c1 == '\'') { // goto previous context
3260 dot = swap_context(dot); // swap current and previous context
3261 dot_begin(); // go to B-o-l
3262 dot_skip_over_ws();
3263 } else {
3264 indicate_error(c);
3265 }
3266 break;
3267 case 'm': // m- Mark a line
3268 // this is really stupid. If there are any inserts or deletes
3269 // between text[0] and dot then this mark will not point to the
3270 // correct location! It could be off by many lines!
3271 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003272 c1 = (get_one_char() | 0x20) - 'a';
3273 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003274 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003275 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003276 } else {
3277 indicate_error(c);
3278 }
3279 break;
3280 case 'P': // P- Put register before
3281 case 'p': // p- put register after
3282 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003283 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003284 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003285 break;
3286 }
3287 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003288 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003289 if (c == 'P') {
3290 dot_begin(); // putting lines- Put above
3291 }
3292 if (c == 'p') {
3293 // are we putting after very last line?
3294 if (end_line(dot) == (end - 1)) {
3295 dot = end; // force dot to end of text[]
3296 } else {
3297 dot_next(); // next line, then put before
3298 }
3299 }
3300 } else {
3301 if (c == 'p')
3302 dot_right(); // move to right, can move to NL
3303 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003304 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003305 end_cmd_q(); // stop adding to q
3306 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003307 case 'U': // U- Undo; replace current line with original version
3308 if (reg[Ureg] != 0) {
3309 p = begin_line(dot);
3310 q = end_line(dot);
3311 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003312 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003313 dot = p;
3314 dot_skip_over_ws();
3315 }
3316 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003317#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003318 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003319 case KEYCODE_END: // Cursor Key End
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003320 for (;;) {
3321 dot = end_line(dot);
3322 if (--cmdcnt > 0)
3323 break;
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003324 dot_next();
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003325 }
Eric Andersen3f980402001-04-04 17:31:15 +00003326 break;
3327 case '%': // %- find matching char of pair () [] {}
3328 for (q = dot; q < end && *q != '\n'; q++) {
3329 if (strchr("()[]{}", *q) != NULL) {
3330 // we found half of a pair
3331 p = find_pair(q, *q);
3332 if (p == NULL) {
3333 indicate_error(c);
3334 } else {
3335 dot = p;
3336 }
3337 break;
3338 }
3339 }
3340 if (*q == '\n')
3341 indicate_error(c);
3342 break;
3343 case 'f': // f- forward to a user specified char
3344 last_forward_char = get_one_char(); // get the search char
3345 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003346 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003347 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003348 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003349 case ';': // ;- look at rest of line for last forward char
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003350 do {
3351 if (last_forward_char == 0)
3352 break;
3353 q = dot + 1;
3354 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3355 q++;
3356 }
3357 if (*q == last_forward_char)
3358 dot = q;
3359 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003360 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003361 case ',': // repeat latest 'f' in opposite direction
Paul Foxb5ee8db2008-02-14 01:17:01 +00003362 if (last_forward_char == 0)
3363 break;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003364 do {
3365 q = dot - 1;
3366 while (q >= text && *q != '\n' && *q != last_forward_char) {
3367 q--;
3368 }
3369 if (q >= text && *q == last_forward_char)
3370 dot = q;
3371 } while (--cmdcnt > 0);
Paul Foxb5ee8db2008-02-14 01:17:01 +00003372 break;
3373
Eric Andersen3f980402001-04-04 17:31:15 +00003374 case '-': // -- goto prev line
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003375 do {
3376 dot_prev();
3377 dot_skip_over_ws();
3378 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003379 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003380#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003381 case '.': // .- repeat the last modifying command
3382 // Stuff the last_modifying_cmd back into stdin
3383 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003384 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003385 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003386 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003387 }
3388 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003389#endif
3390#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003391 case '?': // /- search for a pattern
3392 case '/': // /- search for a pattern
3393 buf[0] = c;
3394 buf[1] = '\0';
3395 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003396 if (q[0] && !q[1]) {
3397 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003398 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003399 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003400 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003401 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003402 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003403 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003404 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003405 goto dc3; // now find the pattern
3406 }
3407 // user changed mind and erased the "/"- do nothing
3408 break;
3409 case 'N': // N- backward search for last pattern
Eric Andersen3f980402001-04-04 17:31:15 +00003410 dir = BACK; // assume BACKWARD search
3411 p = dot - 1;
3412 if (last_search_pattern[0] == '?') {
3413 dir = FORWARD;
3414 p = dot + 1;
3415 }
3416 goto dc4; // now search for pattern
3417 break;
3418 case 'n': // n- repeat search for last pattern
3419 // search rest of text[] starting at next char
3420 // if search fails return orignal "p" not the "p+1" address
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003421 do {
3422 const char *msg;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003423 dc3:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003424 dir = FORWARD; // assume FORWARD search
3425 p = dot + 1;
3426 if (last_search_pattern[0] == '?') {
3427 dir = BACK;
3428 p = dot - 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003429 }
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003430 dc4:
3431 q = char_search(p, last_search_pattern + 1, dir, FULL);
3432 if (q != NULL) {
3433 dot = q; // good search, update "dot"
3434 msg = NULL;
3435 goto dc2;
3436 }
3437 // no pattern found between "dot" and "end"- continue at top
3438 p = text;
3439 if (dir == BACK) {
3440 p = end - 1;
3441 }
3442 q = char_search(p, last_search_pattern + 1, dir, FULL);
3443 if (q != NULL) { // found something
3444 dot = q; // found new pattern- goto it
3445 msg = "search hit BOTTOM, continuing at TOP";
3446 if (dir == BACK) {
3447 msg = "search hit TOP, continuing at BOTTOM";
3448 }
3449 } else {
3450 msg = "Pattern not found";
3451 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003452 dc2:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003453 if (msg)
3454 status_line_bold("%s", msg);
3455 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003456 break;
3457 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003458 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003459 if (q != NULL) { // found blank line
3460 dot = next_line(q); // move to next blank line
3461 }
3462 break;
3463 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003464 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003465 if (q != NULL) { // found blank line
3466 dot = next_line(q); // move to next blank line
3467 }
3468 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003469#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003470 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003471 case '1': // 1-
3472 case '2': // 2-
3473 case '3': // 3-
3474 case '4': // 4-
3475 case '5': // 5-
3476 case '6': // 6-
3477 case '7': // 7-
3478 case '8': // 8-
3479 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003480 if (c == '0' && cmdcnt < 1) {
3481 dot_begin(); // this was a standalone zero
3482 } else {
3483 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3484 }
3485 break;
3486 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003487 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003488#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003489 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003490#else
Eric Andersen822c3832001-05-07 17:37:43 +00003491 if (*p == ':')
3492 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003493 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003494 if (cnt <= 0)
3495 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003496 if (strncmp(p, "quit", cnt) == 0
3497 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003498 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003499 if (file_modified && p[1] != '!') {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003500 status_line_bold("No write since last change (:quit! overrides)");
Eric Andersen3f980402001-04-04 17:31:15 +00003501 } else {
3502 editing = 0;
3503 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003504 } else if (strncmp(p, "write", cnt) == 0
3505 || strncmp(p, "wq", cnt) == 0
3506 || strncmp(p, "wn", cnt) == 0
3507 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003508 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003509 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003510 if (cnt < 0) {
3511 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003512 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003513 } else {
3514 file_modified = 0;
3515 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003516 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003517 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3518 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3519 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003520 editing = 0;
3521 }
Eric Andersen3f980402001-04-04 17:31:15 +00003522 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003523 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003524 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003525 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003526 dot = find_line(j); // go to line # j
3527 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003528 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003529 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003530 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003531#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003532 break;
3533 case '<': // <- Left shift something
3534 case '>': // >- Right shift something
3535 cnt = count_lines(text, dot); // remember what line we are on
3536 c1 = get_one_char(); // get the type of thing to delete
3537 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003538 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003539 p = begin_line(p);
3540 q = end_line(q);
3541 i = count_lines(p, q); // # of lines we are shifting
3542 for ( ; i > 0; i--, p = next_line(p)) {
3543 if (c == '<') {
3544 // shift left- remove tab or 8 spaces
3545 if (*p == '\t') {
3546 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003547 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003548 } else if (*p == ' ') {
3549 // we should be calculating columns, not just SPACE
3550 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003551 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003552 }
3553 }
3554 } else if (c == '>') {
3555 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003556 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003557 }
3558 }
3559 dot = find_line(cnt); // what line were we on
3560 dot_skip_over_ws();
3561 end_cmd_q(); // stop adding to q
3562 break;
3563 case 'A': // A- append at e-o-l
3564 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003565 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003566 case 'a': // a- append after current char
3567 if (*dot != '\n')
3568 dot++;
3569 goto dc_i;
3570 break;
3571 case 'B': // B- back a blank-delimited Word
3572 case 'E': // E- end of a blank-delimited word
3573 case 'W': // W- forward a blank-delimited word
Eric Andersen3f980402001-04-04 17:31:15 +00003574 dir = FORWARD;
3575 if (c == 'B')
3576 dir = BACK;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003577 do {
3578 if (c == 'W' || isspace(dot[dir])) {
3579 dot = skip_thing(dot, 1, dir, S_TO_WS);
3580 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3581 }
3582 if (c != 'W')
3583 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3584 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003585 break;
3586 case 'C': // C- Change to e-o-l
3587 case 'D': // D- delete to e-o-l
3588 save_dot = dot;
3589 dot = dollar_line(dot); // move to before NL
3590 // copy text into a register and delete
3591 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3592 if (c == 'C')
3593 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003594#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003595 if (c == 'D')
3596 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003597#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003598 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003599 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003600 c1 = get_one_char();
3601 if (c1 != 'g') {
3602 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003603 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003604 buf[2] = '\0';
3605 not_implemented(buf);
3606 break;
3607 }
3608 if (cmdcnt == 0)
3609 cmdcnt = 1;
3610 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003611 case 'G': // G- goto to a line number (default= E-O-F)
3612 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003613 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003614 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003615 }
3616 dot_skip_over_ws();
3617 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003618 case 'H': // H- goto top line on screen
3619 dot = screenbegin;
3620 if (cmdcnt > (rows - 1)) {
3621 cmdcnt = (rows - 1);
3622 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003623 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003624 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003625 }
Eric Andersen3f980402001-04-04 17:31:15 +00003626 dot_skip_over_ws();
3627 break;
3628 case 'I': // I- insert before first non-blank
3629 dot_begin(); // 0
3630 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003631 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003632 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003633 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003634 dc_i:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003635 cmd_mode = 1; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003636 break;
3637 case 'J': // J- join current and next lines together
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003638 do {
3639 dot_end(); // move to NL
3640 if (dot < end - 1) { // make sure not last char in text[]
3641 *dot++ = ' '; // replace NL with space
3642 file_modified++;
3643 while (isblank(*dot)) { // delete leading WS
3644 dot_delete();
3645 }
Eric Andersen3f980402001-04-04 17:31:15 +00003646 }
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003647 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003648 end_cmd_q(); // stop adding to q
3649 break;
3650 case 'L': // L- goto bottom line on screen
3651 dot = end_screen();
3652 if (cmdcnt > (rows - 1)) {
3653 cmdcnt = (rows - 1);
3654 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003655 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003656 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003657 }
Eric Andersen3f980402001-04-04 17:31:15 +00003658 dot_begin();
3659 dot_skip_over_ws();
3660 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003661 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003662 dot = screenbegin;
3663 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3664 dot = next_line(dot);
3665 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003666 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003667 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003668 p = begin_line(dot);
3669 if (p[-1] == '\n') {
3670 dot_prev();
3671 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3672 dot_end();
3673 dot = char_insert(dot, '\n');
3674 } else {
3675 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003676 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003677 dot_prev(); // -
3678 }
3679 goto dc_i;
3680 break;
3681 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003682 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003683 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003684 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003685 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003686 c = 'x';
3687 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003688 case 'X': // X- delete char before dot
3689 case 'x': // x- delete the current char
3690 case 's': // s- substitute the current char
Eric Andersen3f980402001-04-04 17:31:15 +00003691 dir = 0;
3692 if (c == 'X')
3693 dir = -1;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003694 do {
3695 if (dot[dir] != '\n') {
3696 if (c == 'X')
3697 dot--; // delete prev char
3698 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3699 }
3700 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003701 end_cmd_q(); // stop adding to q
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003702 if (c == 's')
3703 goto dc_i; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003704 break;
3705 case 'Z': // Z- if modified, {write}; exit
3706 // ZZ means to save file (if necessary), then exit
3707 c1 = get_one_char();
3708 if (c1 != 'Z') {
3709 indicate_error(c);
3710 break;
3711 }
Paul Foxf0305b72006-03-28 14:18:21 +00003712 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003713 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003714 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003715 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003716 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003717 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003718 if (cnt < 0) {
3719 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003720 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003721 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003722 editing = 0;
3723 }
3724 } else {
3725 editing = 0;
3726 }
3727 break;
3728 case '^': // ^- move to first non-blank on line
3729 dot_begin();
3730 dot_skip_over_ws();
3731 break;
3732 case 'b': // b- back a word
3733 case 'e': // e- end of word
Eric Andersen3f980402001-04-04 17:31:15 +00003734 dir = FORWARD;
3735 if (c == 'b')
3736 dir = BACK;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003737 do {
3738 if ((dot + dir) < text || (dot + dir) > end - 1)
3739 break;
3740 dot += dir;
3741 if (isspace(*dot)) {
3742 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3743 }
3744 if (isalnum(*dot) || *dot == '_') {
3745 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3746 } else if (ispunct(*dot)) {
3747 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3748 }
3749 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003750 break;
3751 case 'c': // c- change something
3752 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003753#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003754 case 'y': // y- yank something
3755 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003756#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003757 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003758 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003759 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003760#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003761 if (c == 'y' || c == 'Y')
3762 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003763#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003764 c1 = 'y';
3765 if (c != 'Y')
3766 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003767 // determine range, and whether it spans lines
3768 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003769 if (c1 == 27) { // ESC- user changed mind and wants out
3770 c = c1 = 27; // Escape- do nothing
3771 } else if (strchr("wW", c1)) {
3772 if (c == 'c') {
3773 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003774 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003775 if (q <= text || q[-1] == '\n')
3776 break;
3777 q--;
3778 }
3779 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003780 dot = yank_delete(p, q, ml, yf); // delete word
3781 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3782 // partial line copy text into a register and delete
3783 dot = yank_delete(p, q, ml, yf); // delete word
3784 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3785 // whole line copy text into a register and delete
3786 dot = yank_delete(p, q, ml, yf); // delete lines
3787 whole = 1;
3788 } else {
3789 // could not recognize object
3790 c = c1 = 27; // error-
3791 ml = 0;
3792 indicate_error(c);
3793 }
3794 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003795 if (c == 'c') {
3796 dot = char_insert(dot, '\n');
3797 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003798 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003799 dot_prev();
3800 }
3801 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003802 dot_begin();
3803 dot_skip_over_ws();
3804 }
Eric Andersen3f980402001-04-04 17:31:15 +00003805 }
3806 if (c1 != 27) {
3807 // if CHANGING, not deleting, start inserting after the delete
3808 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003809 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003810 goto dc_i; // start inserting
3811 }
3812 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003813 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003814 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003815#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003816 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003817 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003818 }
3819 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003820 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003821 for (cnt = 0; p <= q; p++) {
3822 if (*p == '\n')
3823 cnt++;
3824 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003825 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003826 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003827#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003828 end_cmd_q(); // stop adding to q
3829 }
3830 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003831 }
Eric Andersen3f980402001-04-04 17:31:15 +00003832 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003833 case KEYCODE_UP: // cursor key Up
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003834 do {
3835 dot_prev();
3836 dot = move_to_col(dot, ccol + offset); // try stay in same col
3837 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003838 break;
3839 case 'r': // r- replace the current char with user input
3840 c1 = get_one_char(); // get the replacement char
3841 if (*dot != '\n') {
3842 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003843 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003844 }
3845 end_cmd_q(); // stop adding to q
3846 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003847 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003848 last_forward_char = get_one_char();
3849 do_cmd(';');
3850 if (*dot == last_forward_char)
3851 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003852 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003853 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003854 case 'w': // w- forward a word
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003855 do {
3856 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3857 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3858 } else if (ispunct(*dot)) { // we are on PUNCT
3859 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3860 }
3861 if (dot < end - 1)
3862 dot++; // move over word
3863 if (isspace(*dot)) {
3864 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3865 }
3866 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003867 break;
3868 case 'z': // z-
3869 c1 = get_one_char(); // get the replacement char
3870 cnt = 0;
3871 if (c1 == '.')
3872 cnt = (rows - 2) / 2; // put dot at center
3873 if (c1 == '-')
3874 cnt = rows - 2; // put dot at bottom
3875 screenbegin = begin_line(dot); // start dot at top
3876 dot_scroll(cnt, -1);
3877 break;
3878 case '|': // |- move to column "cmdcnt"
3879 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3880 break;
3881 case '~': // ~- flip the case of letters a-z -> A-Z
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003882 do {
3883 if (islower(*dot)) {
3884 *dot = toupper(*dot);
3885 file_modified++;
3886 } else if (isupper(*dot)) {
3887 *dot = tolower(*dot);
3888 file_modified++;
3889 }
3890 dot_right();
3891 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003892 end_cmd_q(); // stop adding to q
3893 break;
3894 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003895 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003896 dot_begin();
3897 break;
3898 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003899#if 0
3900 case KEYCODE_FUN1: // Function Key F1
3901 case KEYCODE_FUN2: // Function Key F2
3902 case KEYCODE_FUN3: // Function Key F3
3903 case KEYCODE_FUN4: // Function Key F4
3904 case KEYCODE_FUN5: // Function Key F5
3905 case KEYCODE_FUN6: // Function Key F6
3906 case KEYCODE_FUN7: // Function Key F7
3907 case KEYCODE_FUN8: // Function Key F8
3908 case KEYCODE_FUN9: // Function Key F9
3909 case KEYCODE_FUN10: // Function Key F10
3910 case KEYCODE_FUN11: // Function Key F11
3911 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003912 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003913#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003914 }
3915
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003916 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003917 // if text[] just became empty, add back an empty line
3918 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003919 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003920 dot = text;
3921 }
3922 // it is OK for dot to exactly equal to end, otherwise check dot validity
3923 if (dot != end) {
3924 dot = bound_dot(dot); // make sure "dot" is valid
3925 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003926#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003927 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003928#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003929
3930 if (!isdigit(c))
3931 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3932 cnt = dot - begin_line(dot);
3933 // Try to stay off of the Newline
3934 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3935 dot--;
3936}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003937
Paul Fox35e9c5d2008-03-06 16:26:12 +00003938/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003939#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003940static int totalcmds = 0;
3941static int Mp = 85; // Movement command Probability
3942static int Np = 90; // Non-movement command Probability
3943static int Dp = 96; // Delete command Probability
3944static int Ip = 97; // Insert command Probability
3945static int Yp = 98; // Yank command Probability
3946static int Pp = 99; // Put command Probability
3947static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003948static const char chars[20] = "\t012345 abcdABCD-=.$";
3949static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003950 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003951 "broadcast", "the", "emergency", "of",
3952 "system", "quick", "brown", "fox",
3953 "jumped", "over", "lazy", "dogs",
3954 "back", "January", "Febuary", "March"
3955};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003956static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003957 "You should have received a copy of the GNU General Public License\n",
3958 "char c, cm, *cmd, *cmd1;\n",
3959 "generate a command by percentages\n",
3960 "Numbers may be typed as a prefix to some commands.\n",
3961 "Quit, discarding changes!\n",
3962 "Forced write, if permission originally not valid.\n",
3963 "In general, any ex or ed command (such as substitute or delete).\n",
3964 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3965 "Please get w/ me and I will go over it with you.\n",
3966 "The following is a list of scheduled, committed changes.\n",
3967 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3968 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3969 "Any question about transactions please contact Sterling Huxley.\n",
3970 "I will try to get back to you by Friday, December 31.\n",
3971 "This Change will be implemented on Friday.\n",
3972 "Let me know if you have problems accessing this;\n",
3973 "Sterling Huxley recently added you to the access list.\n",
3974 "Would you like to go to lunch?\n",
3975 "The last command will be automatically run.\n",
3976 "This is too much english for a computer geek.\n",
3977};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003978static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003979 "You should have received a copy of the GNU General Public License\n",
3980 "char c, cm, *cmd, *cmd1;\n",
3981 "generate a command by percentages\n",
3982 "Numbers may be typed as a prefix to some commands.\n",
3983 "Quit, discarding changes!\n",
3984 "Forced write, if permission originally not valid.\n",
3985 "In general, any ex or ed command (such as substitute or delete).\n",
3986 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3987 "Please get w/ me and I will go over it with you.\n",
3988 "The following is a list of scheduled, committed changes.\n",
3989 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3990 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3991 "Any question about transactions please contact Sterling Huxley.\n",
3992 "I will try to get back to you by Friday, December 31.\n",
3993 "This Change will be implemented on Friday.\n",
3994 "Let me know if you have problems accessing this;\n",
3995 "Sterling Huxley recently added you to the access list.\n",
3996 "Would you like to go to lunch?\n",
3997 "The last command will be automatically run.\n",
3998 "This is too much english for a computer geek.\n",
3999};
4000
4001// create a random command to execute
4002static void crash_dummy()
4003{
4004 static int sleeptime; // how long to pause between commands
4005 char c, cm, *cmd, *cmd1;
4006 int i, cnt, thing, rbi, startrbi, percent;
4007
4008 // "dot" movement commands
4009 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
4010
4011 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02004012 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004013 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004014 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02004015 readbuffer[0] = 'X';
4016 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004017 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004018 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004019 // generate a command by percentages
4020 percent = (int) lrand48() % 100; // get a number from 0-99
4021 if (percent < Mp) { // Movement commands
4022 // available commands
4023 cmd = cmd1;
4024 M++;
4025 } else if (percent < Np) { // non-movement commands
4026 cmd = "mz<>\'\""; // available commands
4027 N++;
4028 } else if (percent < Dp) { // Delete commands
4029 cmd = "dx"; // available commands
4030 D++;
4031 } else if (percent < Ip) { // Inset commands
4032 cmd = "iIaAsrJ"; // available commands
4033 I++;
4034 } else if (percent < Yp) { // Yank commands
4035 cmd = "yY"; // available commands
4036 Y++;
4037 } else if (percent < Pp) { // Put commands
4038 cmd = "pP"; // available commands
4039 P++;
4040 } else {
4041 // We do not know how to handle this command, try again
4042 U++;
4043 goto cd0;
4044 }
4045 // randomly pick one of the available cmds from "cmd[]"
4046 i = (int) lrand48() % strlen(cmd);
4047 cm = cmd[i];
4048 if (strchr(":\024", cm))
4049 goto cd0; // dont allow colon or ctrl-T commands
4050 readbuffer[rbi++] = cm; // put cmd into input buffer
4051
4052 // now we have the command-
4053 // there are 1, 2, and multi char commands
4054 // find out which and generate the rest of command as necessary
4055 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
4056 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
4057 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
4058 cmd1 = "abcdefghijklmnopqrstuvwxyz";
4059 }
4060 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4061 c = cmd1[thing];
4062 readbuffer[rbi++] = c; // add movement to input buffer
4063 }
4064 if (strchr("iIaAsc", cm)) { // multi-char commands
4065 if (cm == 'c') {
4066 // change some thing
4067 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4068 c = cmd1[thing];
4069 readbuffer[rbi++] = c; // add movement to input buffer
4070 }
4071 thing = (int) lrand48() % 4; // what thing to insert
4072 cnt = (int) lrand48() % 10; // how many to insert
4073 for (i = 0; i < cnt; i++) {
4074 if (thing == 0) { // insert chars
4075 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
4076 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004077 strcat(readbuffer, words[(int) lrand48() % 20]);
4078 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004079 sleeptime = 0; // how fast to type
4080 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004081 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004082 sleeptime = 0; // how fast to type
4083 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004084 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004085 sleeptime = 0; // how fast to type
4086 }
4087 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004088 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004089 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02004090 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004091 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004092 totalcmds++;
4093 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004094 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004095}
4096
4097// test to see if there are any errors
4098static void crash_test()
4099{
4100 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004101
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004102 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004103 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004104
4105 msg[0] = '\0';
4106 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004107 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004108 }
4109 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004110 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004111 }
4112 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004113 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004114 }
4115 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004116 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004117 }
4118 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004119 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004120 }
4121 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004122 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004123 }
4124
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004125 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00004126 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004127 totalcmds, last_input_char, msg, SOs, SOn);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01004128 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00004129 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004130 if (d[0] == '\n' || d[0] == '\r')
4131 break;
4132 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004133 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004134 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004135 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004136 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004137 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
4138 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
4139 oldtim = tim;
4140 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004141}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00004142#endif