blob: b4ad12e5c966cf3122b7226fdba758babc42be1d [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 save_argc; // how many file names on cmd line
282 int cmdcnt; // repetition count
283 unsigned rows, columns; // the terminal screen is this size
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700284#if ENABLE_FEATURE_VI_ASK_TERMINAL
285 int get_rowcol_error;
286#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000287 int crow, ccol; // cursor is on Crow x Ccol
288 int offset; // chars scrolled off the screen to the left
289 int have_status_msg; // is default edit status needed?
290 // [don't make smallint!]
291 int last_status_cksum; // hash of current status line
292 char *current_filename;
293 char *screenbegin; // index into text[], of top line on the screen
294 char *screen; // pointer to the virtual screen buffer
295 int screensize; // and its size
296 int tabstop;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000297 int last_forward_char; // last char searched for with 'f' (int because of Unicode)
Denis Vlasenkob1759462008-06-20 20:20:54 +0000298 char erase_char; // the users erase character
299 char last_input_char; // last char read from user
Denis Vlasenkob1759462008-06-20 20:20:54 +0000300
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000301#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkob1759462008-06-20 20:20:54 +0000302 smallint adding2q; // are we currently adding user input to q
303 int lmc_len; // length of last_modifying_cmd
304 char *ioq, *ioq_start; // pointer to string for get_one_char to "read"
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000305#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000306#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenkob1759462008-06-20 20:20:54 +0000307 int last_row; // where the cursor was last moved to
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000308#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000309#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkob1759462008-06-20 20:20:54 +0000310 int my_pid;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000311#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000312#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkob1759462008-06-20 20:20:54 +0000313 char *last_search_pattern; // last pattern from a '/' or '?' search
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000314#endif
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000315
Denis Vlasenkob1759462008-06-20 20:20:54 +0000316 /* former statics */
317#if ENABLE_FEATURE_VI_YANKMARK
318 char *edit_file__cur_line;
319#endif
320 int refresh__old_offset;
321 int format_edit_status__tot;
Eric Andersen3f980402001-04-04 17:31:15 +0000322
Denis Vlasenkob1759462008-06-20 20:20:54 +0000323 /* a few references only */
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000324#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000325 int YDreg, Ureg; // default delete register and orig line for "U"
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000326 char *reg[28]; // named register a-z, "D", and "U" 0-25,26,27
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000327 char *mark[28]; // user marks points somewhere in text[]- a-z and previous context ''
328 char *context_start, *context_end;
329#endif
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000330#if ENABLE_FEATURE_VI_USE_SIGNALS
Denis Vlasenkob1759462008-06-20 20:20:54 +0000331 sigjmp_buf restart; // catch_sig()
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000332#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000333 struct termios term_orig, term_vi; // remember what the cooked mode was
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000334#if ENABLE_FEATURE_VI_COLON
335 char *initial_cmds[3]; // currently 2 entries, NULL terminated
336#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000337 // Should be just enough to hold a key sequence,
Denis Vlasenko25497c12008-10-14 10:25:05 +0000338 // but CRASHME mode uses it as generated command buffer too
339#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko1dfeeeb2008-10-18 19:04:37 +0000340 char readbuffer[128];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000341#else
Denis Vlasenko5f6aaf32008-10-25 23:27:29 +0000342 char readbuffer[KEYCODE_BUFFER_SIZE];
Denis Vlasenko25497c12008-10-14 10:25:05 +0000343#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000344#define STATUS_BUFFER_LEN 200
345 char status_buffer[STATUS_BUFFER_LEN]; // messages to the user
346#if ENABLE_FEATURE_VI_DOT_CMD
347 char last_modifying_cmd[MAX_INPUT_LEN]; // last modifying cmd for "."
348#endif
349 char get_input_line__buf[MAX_INPUT_LEN]; /* former static */
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000350
351 char scr_out_buf[MAX_SCR_COLS + MAX_TABSTOP * 2];
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000352};
353#define G (*ptr_to_globals)
354#define text (G.text )
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000355#define text_size (G.text_size )
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000356#define end (G.end )
357#define dot (G.dot )
358#define reg (G.reg )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000359
360#define vi_setops (G.vi_setops )
361#define editing (G.editing )
362#define cmd_mode (G.cmd_mode )
363#define file_modified (G.file_modified )
364#define last_file_modified (G.last_file_modified )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000365#define save_argc (G.save_argc )
366#define cmdcnt (G.cmdcnt )
367#define rows (G.rows )
368#define columns (G.columns )
369#define crow (G.crow )
370#define ccol (G.ccol )
371#define offset (G.offset )
372#define status_buffer (G.status_buffer )
373#define have_status_msg (G.have_status_msg )
374#define last_status_cksum (G.last_status_cksum )
375#define current_filename (G.current_filename )
376#define screen (G.screen )
377#define screensize (G.screensize )
378#define screenbegin (G.screenbegin )
379#define tabstop (G.tabstop )
Denis Vlasenko0112ff52008-10-25 23:23:00 +0000380#define last_forward_char (G.last_forward_char )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000381#define erase_char (G.erase_char )
382#define last_input_char (G.last_input_char )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000383#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkob1759462008-06-20 20:20:54 +0000384#define readonly_mode (G.readonly_mode )
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000385#else
386#define readonly_mode 0
387#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +0000388#define adding2q (G.adding2q )
389#define lmc_len (G.lmc_len )
390#define ioq (G.ioq )
391#define ioq_start (G.ioq_start )
392#define last_row (G.last_row )
393#define my_pid (G.my_pid )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000394#define last_search_pattern (G.last_search_pattern)
Denis Vlasenko2a210e52008-06-22 13:20:42 +0000395
Denis Vlasenkob1759462008-06-20 20:20:54 +0000396#define edit_file__cur_line (G.edit_file__cur_line)
397#define refresh__old_offset (G.refresh__old_offset)
398#define format_edit_status__tot (G.format_edit_status__tot)
399
Denis Vlasenko0b3b41b2007-05-30 02:01:40 +0000400#define YDreg (G.YDreg )
401#define Ureg (G.Ureg )
402#define mark (G.mark )
403#define context_start (G.context_start )
404#define context_end (G.context_end )
405#define restart (G.restart )
406#define term_orig (G.term_orig )
407#define term_vi (G.term_vi )
408#define initial_cmds (G.initial_cmds )
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000409#define readbuffer (G.readbuffer )
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000410#define scr_out_buf (G.scr_out_buf )
Denis Vlasenkob1759462008-06-20 20:20:54 +0000411#define last_modifying_cmd (G.last_modifying_cmd )
412#define get_input_line__buf (G.get_input_line__buf)
413
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000414#define INIT_G() do { \
Denis Vlasenko574f2f42008-02-27 18:41:59 +0000415 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
Denis Vlasenkob1759462008-06-20 20:20:54 +0000416 last_file_modified = -1; \
Denis Vlasenko31d58e52008-10-29 13:16:28 +0000417 /* "" but has space for 2 chars: */ \
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000418 IF_FEATURE_VI_SEARCH(last_search_pattern = xzalloc(2);) \
Denis Vlasenkoa96425f2007-12-09 04:13:43 +0000419} while (0)
Eric Andersen3f980402001-04-04 17:31:15 +0000420
Denis Vlasenkob1759462008-06-20 20:20:54 +0000421
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000422static int init_text_buffer(char *); // init from file or create new
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000423static void edit_file(char *); // edit one file
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000424static void do_cmd(int); // execute a command
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000425static int next_tabstop(int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000426static void sync_cursor(char *, int *, int *); // synchronize the screen cursor to dot
427static char *begin_line(char *); // return pointer to cur line B-o-l
428static char *end_line(char *); // return pointer to cur line E-o-l
429static char *prev_line(char *); // return pointer to prev line B-o-l
430static char *next_line(char *); // return pointer to next line B-o-l
431static char *end_screen(void); // get pointer to last char on screen
432static int count_lines(char *, char *); // count line from start to stop
433static char *find_line(int); // find begining of line #li
434static char *move_to_col(char *, int); // move "p" to column l
Eric Andersen3f980402001-04-04 17:31:15 +0000435static void dot_left(void); // move dot left- dont leave line
436static void dot_right(void); // move dot right- dont leave line
437static void dot_begin(void); // move dot to B-o-l
438static void dot_end(void); // move dot to E-o-l
439static void dot_next(void); // move dot to next line B-o-l
440static void dot_prev(void); // move dot to prev line B-o-l
441static void dot_scroll(int, int); // move the screen up or down
442static void dot_skip_over_ws(void); // move dot pat WS
443static void dot_delete(void); // delete the char at 'dot'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000444static char *bound_dot(char *); // make sure text[0] <= P < "end"
445static char *new_screen(int, int); // malloc virtual screen memory
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000446static char *char_insert(char *, char); // insert the char c at 'p'
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000447// might reallocate text[]! use p += stupid_insert(p, ...),
448// and be careful to not use pointers into potentially freed text[]!
449static uintptr_t stupid_insert(char *, char); // stupidly insert the char c at 'p'
Paul Foxc51fc7b2008-03-06 01:34:23 +0000450static int find_range(char **, char **, char); // return pointers for an object
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000451static int st_test(char *, int, int, char *); // helper for skip_thing()
452static char *skip_thing(char *, int, int, int); // skip some object
453static char *find_pair(char *, char); // find matching pair () [] {}
454static char *text_hole_delete(char *, char *); // at "p", delete a 'size' byte hole
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000455// might reallocate text[]! use p += text_hole_make(p, ...),
456// and be careful to not use pointers into potentially freed text[]!
457static uintptr_t text_hole_make(char *, int); // at "p", make a 'size' byte hole
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000458static char *yank_delete(char *, char *, int, int); // yank text[] into register then delete
Eric Andersen3f980402001-04-04 17:31:15 +0000459static void show_help(void); // display some help info
Eric Andersen3f980402001-04-04 17:31:15 +0000460static void rawmode(void); // set "raw" mode on tty
461static void cookmode(void); // return to "cooked" mode on tty
Denis Vlasenko87f3b262007-09-07 13:43:28 +0000462// sleep for 'h' 1/100 seconds, return 1/0 if stdin is (ready for read)/(not ready)
463static int mysleep(int);
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000464static int readit(void); // read (maybe cursor) key from stdin
465static int get_one_char(void); // read 1 char from stdin
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000466static int file_size(const char *); // what is the byte size of "fn"
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000467#if !ENABLE_FEATURE_VI_READONLY
468#define file_insert(fn, p, update_ro_status) file_insert(fn, p)
Denis Vlasenko59a1f302007-07-14 22:43:10 +0000469#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000470// file_insert might reallocate text[]!
471static int file_insert(const char *, char *, int);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000472static int file_write(char *, char *, char *);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +0000473#if !ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
474#define place_cursor(a, b, optimize) place_cursor(a, b)
475#endif
Eric Andersen822c3832001-05-07 17:37:43 +0000476static void place_cursor(int, int, int);
Eric Andersenbff7a602001-11-17 07:15:43 +0000477static void screen_erase(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000478static void clear_to_eol(void);
479static void clear_to_eos(void);
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000480static void go_bottom_and_clear_to_eol(void);
Eric Andersen3f980402001-04-04 17:31:15 +0000481static void standout_start(void); // send "start reverse video" sequence
482static void standout_end(void); // send "end reverse video" sequence
483static void flash(int); // flash the terminal screen
Eric Andersen3f980402001-04-04 17:31:15 +0000484static void show_status_line(void); // put a message on the bottom line
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000485static void status_line(const char *, ...); // print to status buf
486static void status_line_bold(const char *, ...);
487static void not_implemented(const char *); // display "Not implemented" message
Paul Fox8552aec2005-09-16 12:20:05 +0000488static int format_edit_status(void); // format file status on status line
Eric Andersen3f980402001-04-04 17:31:15 +0000489static void redraw(int); // force a full screen refresh
Denis Vlasenko68404f12008-03-17 09:00:54 +0000490static char* format_line(char* /*, int*/);
Eric Andersen3f980402001-04-04 17:31:15 +0000491static void refresh(int); // update the terminal from screen[]
492
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000493static void Indicate_Error(void); // use flash or beep to indicate error
494#define indicate_error(c) Indicate_Error()
Paul Fox90372ed2005-10-09 14:26:26 +0000495static void Hit_Return(void);
496
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000497#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000498static char *char_search(char *, const char *, int, int); // search for pattern starting at p
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000499#endif
500#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000501static char *get_one_address(char *, int *); // get colon addr, if present
502static char *get_address(char *, int *, int *); // get two colon addrs, if present
503static void colon(char *); // execute the "colon" mode cmds
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000504#endif
505#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000506static void winch_sig(int); // catch window size changes
507static void suspend_sig(int); // catch ctrl-Z
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000508static void catch_sig(int); // catch ctrl-C and alarm time-outs
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000509#endif
510#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000511static void start_new_cmd_q(char); // new queue for command
Eric Andersenbff7a602001-11-17 07:15:43 +0000512static void end_cmd_q(void); // stop saving input chars
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000513#else
514#define end_cmd_q() ((void)0)
515#endif
516#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000517static void showmatching(char *); // show the matching pair () [] {}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000518#endif
519#if ENABLE_FEATURE_VI_YANKMARK || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000520// might reallocate text[]! use p += string_insert(p, ...),
521// and be careful to not use pointers into potentially freed text[]!
522static uintptr_t string_insert(char *, const char *); // insert the string at 'p'
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000523#endif
524#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000525static char *text_yank(char *, char *, int); // save copy of "p" into a register
526static char what_reg(void); // what is letter of current YDreg
527static void check_context(char); // remember context for '' command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000528#endif
529#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000530static void crash_dummy();
531static void crash_test();
532static int crashme = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000533#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000534
535
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000536static void write1(const char *out)
537{
538 fputs(out, stdout);
539}
540
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +0000541int vi_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Rob Landleydfba7412006-03-06 20:47:33 +0000542int vi_main(int argc, char **argv)
Eric Andersen3f980402001-04-04 17:31:15 +0000543{
Eric Andersend402edf2001-04-04 19:29:48 +0000544 int c;
Denis Vlasenkob1759462008-06-20 20:20:54 +0000545
546 INIT_G();
Eric Andersen3f980402001-04-04 17:31:15 +0000547
Denis Vlasenkocd5c7862007-05-17 16:37:22 +0000548#if ENABLE_FEATURE_VI_USE_SIGNALS || ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000549 my_pid = getpid();
550#endif
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000551#if ENABLE_FEATURE_VI_CRASHME
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000552 srand((long) my_pid);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000553#endif
Denis Vlasenko2414a962007-07-18 22:03:40 +0000554#ifdef NO_SUCH_APPLET_YET
555 /* If we aren't "vi", we are "view" */
556 if (ENABLE_FEATURE_VI_READONLY && applet_name[2]) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000557 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000558 }
Denis Vlasenko2414a962007-07-18 22:03:40 +0000559#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000560
Bernhard Reutner-Fischer73f56bb2007-09-22 21:18:46 +0000561 vi_setops = VI_AUTOINDENT | VI_SHOWMATCH | VI_IGNORECASE;
Denis Vlasenkof9234132007-03-21 00:03:42 +0000562 // 1- process $HOME/.exrc file (not inplemented yet)
Eric Andersen3f980402001-04-04 17:31:15 +0000563 // 2- process EXINIT variable from environment
564 // 3- process command line args
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000565#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000566 {
567 char *p = getenv("EXINIT");
568 if (p && *p)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000569 initial_cmds[0] = xstrndup(p, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000570 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000571#endif
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000572 while ((c = getopt(argc, argv, "hCRH" IF_FEATURE_VI_COLON("c:"))) != -1) {
Eric Andersen3f980402001-04-04 17:31:15 +0000573 switch (c) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000574#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000575 case 'C':
576 crashme = 1;
577 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000578#endif
579#if ENABLE_FEATURE_VI_READONLY
Eric Andersen3f980402001-04-04 17:31:15 +0000580 case 'R': // Read-only flag
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000581 SET_READONLY_MODE(readonly_mode);
Eric Andersen3f980402001-04-04 17:31:15 +0000582 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000583#endif
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000584#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000585 case 'c': // cmd line vi command
586 if (*optarg)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000587 initial_cmds[initial_cmds[0] != 0] = xstrndup(optarg, MAX_INPUT_LEN);
Denis Vlasenkof9234132007-03-21 00:03:42 +0000588 break;
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000589#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000590 case 'H':
Eric Andersen3f980402001-04-04 17:31:15 +0000591 show_help();
Paul Fox35e9c5d2008-03-06 16:26:12 +0000592 /* fall through */
Paul Fox35e9c5d2008-03-06 16:26:12 +0000593 default:
Denis Vlasenkoc6938402008-03-24 02:18:03 +0000594 bb_show_usage();
Eric Andersendd8500b2001-07-02 18:06:14 +0000595 return 1;
Eric Andersen3f980402001-04-04 17:31:15 +0000596 }
597 }
598
599 // The argv array can be used by the ":next" and ":rewind" commands
Dennis Groenenc0657e02012-01-31 14:12:38 +0100600 argv += optind;
601 argc -= optind;
Eric Andersen3f980402001-04-04 17:31:15 +0000602 save_argc = argc;
Dennis Groenenc0657e02012-01-31 14:12:38 +0100603 optind = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000604
605 //----- This is the main file handling loop --------------
Denys Vlasenko04cecd52010-04-16 22:13:55 -0700606 while (1) {
607 edit_file(argv[optind]); /* param might be NULL */
608 if (++optind >= argc)
609 break;
Eric Andersen3f980402001-04-04 17:31:15 +0000610 }
611 //-----------------------------------------------------------
612
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000613 return 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000614}
615
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000616/* read text from file or create an empty buf */
617/* will also update current_filename */
618static int init_text_buffer(char *fn)
619{
620 int rc;
621 int size = file_size(fn); // file size. -1 means does not exist.
622
623 /* allocate/reallocate text buffer */
624 free(text);
Denis Vlasenkob1759462008-06-20 20:20:54 +0000625 text_size = size + 10240;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000626 screenbegin = dot = end = text = xzalloc(text_size);
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000627
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000628 if (fn != current_filename) {
629 free(current_filename);
630 current_filename = xstrdup(fn);
631 }
632 if (size < 0) {
633 // file dont exist. Start empty buf with dummy line
634 char_insert(text, '\n');
635 rc = 0;
636 } else {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000637 rc = file_insert(fn, text, 1);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000638 }
639 file_modified = 0;
640 last_file_modified = -1;
641#if ENABLE_FEATURE_VI_YANKMARK
642 /* init the marks. */
643 memset(mark, 0, sizeof(mark));
644#endif
645 return rc;
Denis Vlasenko2f6ae432007-07-19 22:50:47 +0000646}
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000647
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700648#if ENABLE_FEATURE_VI_WIN_RESIZE
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200649static int query_screen_dimensions(void)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700650{
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200651 int err = get_terminal_width_height(STDIN_FILENO, &columns, &rows);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700652 if (rows > MAX_SCR_ROWS)
653 rows = MAX_SCR_ROWS;
654 if (columns > MAX_SCR_COLS)
655 columns = MAX_SCR_COLS;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200656 return err;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700657}
658#else
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200659# define query_screen_dimensions() (0)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700660#endif
661
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000662static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000663{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000664#if ENABLE_FEATURE_VI_YANKMARK
665#define cur_line edit_file__cur_line
666#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000667 int c;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000668#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000669 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000670#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000671
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000672 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000673 rawmode();
674 rows = 24;
675 columns = 80;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200676 IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions();
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700677#if ENABLE_FEATURE_VI_ASK_TERMINAL
678 if (G.get_rowcol_error /* TODO? && no input on stdin */) {
679 uint64_t k;
680 write1("\033[999;999H" "\033[6n");
681 fflush_all();
682 k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100);
683 if ((int32_t)k == KEYCODE_CURSOR_POS) {
684 uint32_t rc = (k >> 32);
685 columns = (rc & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200686 if (columns > MAX_SCR_COLS)
687 columns = MAX_SCR_COLS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700688 rows = ((rc >> 16) & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200689 if (rows > MAX_SCR_ROWS)
690 rows = MAX_SCR_ROWS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700691 }
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700692 }
693#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000694 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000695 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000696
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000697#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000698 YDreg = 26; // default Yank/Delete reg
699 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000700 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000701#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000702
Eric Andersen3f980402001-04-04 17:31:15 +0000703 last_forward_char = last_input_char = '\0';
704 crow = 0;
705 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000706
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000707#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700708 signal(SIGINT, catch_sig);
Eric Andersen3f980402001-04-04 17:31:15 +0000709 signal(SIGWINCH, winch_sig);
710 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000711 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000712 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000713 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000714 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000715#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000716
Eric Andersen3f980402001-04-04 17:31:15 +0000717 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
718 cmdcnt = 0;
719 tabstop = 8;
720 offset = 0; // no horizontal offset
721 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000722#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000723 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000724 ioq = ioq_start = NULL;
725 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000726 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000727#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000728
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000729#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000730 {
731 char *p, *q;
732 int n = 0;
733
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700734 while ((p = initial_cmds[n]) != NULL) {
Denis Vlasenkof9234132007-03-21 00:03:42 +0000735 do {
736 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000737 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000738 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000739 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000740 *p++ = '\0';
741 if (*q)
742 colon(q);
743 } while (p);
744 free(initial_cmds[n]);
745 initial_cmds[n] = NULL;
746 n++;
747 }
748 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000749#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000750 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000751 //------This is the main Vi cmd handling loop -----------------------
752 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000753#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000754 if (crashme > 0) {
755 if ((end - text) > 1) {
756 crash_dummy(); // generate a random command
757 } else {
758 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000759 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
760 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000761 refresh(FALSE);
762 }
763 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000764#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000765 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000766#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000767 // save a copy of the current line- for the 'U" command
768 if (begin_line(dot) != cur_line) {
769 cur_line = begin_line(dot);
770 text_yank(begin_line(dot), end_line(dot), Ureg);
771 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000772#endif
773#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000774 // These are commands that change text[].
775 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000776 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000777 && cmd_mode == 0 // command mode
778 && c > '\0' // exclude NUL and non-ASCII chars
779 && c < 0x7f // (Unicode and such)
780 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000781 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000782 start_new_cmd_q(c);
783 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000784#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000785 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000786
Eric Andersen3f980402001-04-04 17:31:15 +0000787 // poll to see if there is input already waiting. if we are
788 // not able to display output fast enough to keep up, skip
789 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200790 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000791 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000792 refresh(FALSE);
793 show_status_line();
794 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000795#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000796 if (crashme > 0)
797 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000798#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000799 }
800 //-------------------------------------------------------------------
801
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000802 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000803 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000804#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000805}
806
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000807//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000808#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000809static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000810{
811 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000812 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000813 IF_FEATURE_VI_YANKMARK(char c;)
814 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000815
816 *addr = -1; // assume no addr
817 if (*p == '.') { // the current line
818 p++;
819 q = begin_line(dot);
820 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000821 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000822#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000823 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000824 p++;
825 c = tolower(*p);
826 p++;
827 if (c >= 'a' && c <= 'z') {
828 // we have a mark
829 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000830 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000831 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000832 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000833 }
834 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000835 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000836#endif
837#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000838 else if (*p == '/') { // a search pattern
839 q = strchrnul(++p, '/');
840 pat = xstrndup(p, q - p); // save copy of pattern
841 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000842 if (*p == '/')
843 p++;
844 q = char_search(dot, pat, FORWARD, FULL);
845 if (q != NULL) {
846 *addr = count_lines(text, q);
847 }
848 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000849 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000850#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000851 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000852 p++;
853 q = begin_line(end - 1);
854 *addr = count_lines(text, q);
855 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000856 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000857 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000858 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200859 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000860 *addr = -1;
861 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000862 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000863}
864
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000865static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000866{
867 //----- get the address' i.e., 1,3 'a,'b -----
868 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000869 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000870 p++; // skip over leading spaces
871 if (*p == '%') { // alias for 1,$
872 p++;
873 *b = 1;
874 *e = count_lines(text, end-1);
875 goto ga0;
876 }
877 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000878 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000879 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000880 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000881 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000882 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000883 p++;
884 // get SECOND addr, if present
885 p = get_one_address(p, e);
886 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000887 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000888 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000889 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000890 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000891}
892
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000893#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000894static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000895 const char *short_opname, int opt)
896{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000897 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000898 int l = strlen(opname) - 1; /* opname have + ' ' */
899
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200900 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000901 if (strncasecmp(a, opname, l) == 0
902 || strncasecmp(a, short_opname, 2) == 0
903 ) {
904 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000905 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000906 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000907 vi_setops |= opt;
908 }
909}
910#endif
911
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000912// buf must be no longer than MAX_INPUT_LEN!
913static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000914{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000915 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000916 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000917 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000918 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000919
920 // :3154 // if (-e line 3154) goto it else stay put
921 // :4,33w! foo // write a portion of buffer to file "foo"
922 // :w // write all of buffer to current file
923 // :q // quit
924 // :q! // quit- dont care about modified file
925 // :'a,'z!sort -u // filter block through sort
926 // :'f // goto mark "f"
927 // :'fl // list literal the mark "f" line
928 // :.r bar // read file "bar" into buffer before dot
929 // :/123/,/abc/d // delete lines from "123" line to "abc" line
930 // :/xyz/ // goto the "xyz" line
931 // :s/find/replace/ // substitute pattern "find" with "replace"
932 // :!<cmd> // run <cmd> then return
933 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000934
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000935 if (!buf[0])
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200936 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000937 if (*buf == ':')
938 buf++; // move past the ':'
939
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000940 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000941 b = e = -1;
942 q = text; // assume 1,$ for the range
943 r = end - 1;
944 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000945 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000946
947 // look for optional address(es) :. :1 :1,9 :'q,'a :%
948 buf = get_address(buf, &b, &e);
949
950 // remember orig command line
951 orig_buf = buf;
952
953 // get the COMMAND into cmd[]
954 buf1 = cmd;
955 while (*buf != '\0') {
956 if (isspace(*buf))
957 break;
958 *buf1++ = *buf++;
959 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000960 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000961 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000962 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000963 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000964 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000965 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000966 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000967 if (buf1) {
968 useforce = TRUE;
969 *buf1 = '\0'; // get rid of !
970 }
971 if (b >= 0) {
972 // if there is only one addr, then the addr
973 // is the line number of the single line the
974 // user wants. So, reset the end
975 // pointer to point at end of the "b" line
976 q = find_line(b); // what line is #b
977 r = end_line(q);
978 li = 1;
979 }
980 if (e >= 0) {
981 // we were given two addrs. change the
982 // end pointer to the addr given by user.
983 r = find_line(e); // what line is #e
984 r = end_line(r);
985 li = e - b + 1;
986 }
987 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000988 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000989 if (i == 0) { // :123CR goto line #123
990 if (b >= 0) {
991 dot = find_line(b); // what line is #b
992 dot_skip_over_ws();
993 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000994 }
995#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200996 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000997 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000998 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000999 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001000 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001001 retcode = system(orig_buf + 1); // run the cmd
1002 if (retcode)
1003 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001004 rawmode();
1005 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +00001006 }
1007#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001008 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001009 if (b < 0) { // no addr given- use defaults
1010 b = e = count_lines(text, dot);
1011 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001012 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001013 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001014 if (b < 0) { // no addr given- use defaults
1015 q = begin_line(dot); // assume .,. for the range
1016 r = end_line(dot);
1017 }
1018 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
1019 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001020 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001021 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001022 if (file_modified && !useforce) {
Dennis Groenenc0657e02012-01-31 14:12:38 +01001023 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001024 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001025 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001026 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001027 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001028 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001029 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001030 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001031 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001032 } else {
1033 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001034 status_line_bold("No current filename");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001035 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001036 }
1037
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001038 if (init_text_buffer(fn) < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001039 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001040
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001041#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001042 if (Ureg >= 0 && Ureg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001043 free(reg[Ureg]); // free orig line reg- for 'U'
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001044 reg[Ureg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001045 }
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001046 if (YDreg >= 0 && YDreg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001047 free(reg[YDreg]); // free default yank/delete register
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001048 reg[YDreg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001049 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001050#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001051 // how many lines in text[]?
1052 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001053 status_line("\"%s\"%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001054 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001055 " %dL, %dC", current_filename,
1056 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001057 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001058 ((readonly_mode) ? " [Readonly]" : ""),
1059 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001060 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001061 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001062 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01001063 status_line_bold("No address allowed on this command");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001064 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001065 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001066 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001067 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001068 free(current_filename);
1069 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001070 } else {
1071 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +00001072 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001073 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001074 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001075 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001076 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001077 cookmode();
1078 show_help();
1079 rawmode();
1080 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001081 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001082 if (b < 0) { // no addr given- use defaults
1083 q = begin_line(dot); // assume .,. for the range
1084 r = end_line(dot);
1085 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001086 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001087 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001088 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001089 int c_is_no_print;
1090
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001091 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +00001092 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001093 if (c_is_no_print) {
1094 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001095 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +00001096 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001097 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001098 write1("$\r");
1099 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001100 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001101 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001102 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001103 else
1104 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001105 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001106 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001107 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001108 standout_end();
1109 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001110 Hit_Return();
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001111 } else if (strncmp(cmd, "quit", i) == 0 // quit
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001112 || strncmp(cmd, "next", i) == 0 // edit next file
Dennis Groenenc0657e02012-01-31 14:12:38 +01001113 || strncmp(cmd, "prev", i) == 0 // edit previous 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) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001117 if (*cmd == 'q') {
Dennis Groenenc0657e02012-01-31 14:12:38 +01001118 // force end of argv list
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001119 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) {
Dennis Groenenc0657e02012-01-31 14:12:38 +01001126 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001127 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001128 }
1129 // are there other file to edit
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001130 n = save_argc - optind - 1;
1131 if (*cmd == 'q' && n > 0) {
1132 status_line_bold("%d more file(s) to edit", n);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001133 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001134 }
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001135 if (*cmd == 'n' && n <= 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001136 status_line_bold("No more files to edit");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001137 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001138 }
Dennis Groenenc0657e02012-01-31 14:12:38 +01001139 if (*cmd == 'p') {
1140 // are there previous files to edit
1141 if (optind < 1) {
1142 status_line_bold("No previous files to edit");
1143 goto ret;
1144 }
1145 optind -= 2;
1146 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001147 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001148 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001149 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001150 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001151 status_line_bold("No filename given");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001152 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001153 }
1154 if (b < 0) { // no addr given- use defaults
1155 q = begin_line(dot); // assume "dot"
1156 }
1157 // read after current line- unless user said ":0r foo"
1158 if (b != 0)
1159 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001160 { // dance around potentially-reallocated text[]
1161 uintptr_t ofs = q - text;
1162 ch = file_insert(fn, q, 0);
1163 q = text + ofs;
1164 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001165 if (ch < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001166 goto ret; // nothing was inserted
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001167 // how many lines in text[]?
1168 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001169 status_line("\"%s\""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001170 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001171 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001172 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001173 li, ch);
1174 if (ch > 0) {
1175 // if the insert is before "dot" then we need to update
1176 if (q <= dot)
1177 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001178 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001179 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001180 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001181 if (file_modified && !useforce) {
Dennis Groenenc0657e02012-01-31 14:12:38 +01001182 status_line_bold("No write since last change (:%s! overrides)", cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001183 } else {
1184 // reset the filenames to edit
Dennis Groenenc0657e02012-01-31 14:12:38 +01001185 optind = -1; /* start from 0th file */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001186 editing = 0;
1187 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001188#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001189 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001190#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001191 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001192#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001193 i = 0; // offset into args
Denis Vlasenkof9234132007-03-21 00:03:42 +00001194 // only blank is regarded as args delmiter. What about tab '\t' ?
1195 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001196 // print out values of all options
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001197#if ENABLE_FEATURE_VI_SETOPTS
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001198 status_line_bold(
1199 "%sautoindent "
1200 "%sflash "
1201 "%signorecase "
1202 "%sshowmatch "
1203 "tabstop=%u",
1204 autoindent ? "" : "no",
1205 err_method ? "" : "no",
1206 ignorecase ? "" : "no",
1207 showmatch ? "" : "no",
1208 tabstop
1209 );
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001210#endif
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001211 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001212 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001213#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001214 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001215 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001216 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001217 i = 2; // ":set noautoindent"
1218 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001219 setops(argp, "flash " , i, "fl", VI_ERR_METHOD);
Denis Vlasenkof9234132007-03-21 00:03:42 +00001220 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001221 setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH );
1222 if (strncmp(argp + i, "tabstop=", 8) == 0) {
1223 int t = 0;
1224 sscanf(argp + i+8, "%u", &t);
1225 if (t > 0 && t <= MAX_TABSTOP)
1226 tabstop = t;
Denis Vlasenkof9234132007-03-21 00:03:42 +00001227 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001228 argp = skip_non_whitespace(argp);
1229 argp = skip_whitespace(argp);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001230 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001231#endif /* FEATURE_VI_SETOPTS */
1232#endif /* FEATURE_VI_SET */
1233#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001234 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001235 char *F, *R, *flags;
1236 size_t len_F, len_R;
1237 int gflag; // global replace flag
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001238
1239 // F points to the "find" pattern
1240 // R points to the "replace" pattern
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001241 // replace the cmd line delimiters "/" with NULs
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001242 c = orig_buf[1]; // what is the delimiter
1243 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001244 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001245 if (!R)
1246 goto colon_s_fail;
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001247 len_F = R - F;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001248 *R++ = '\0'; // terminate "find"
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001249 flags = strchr(R, c);
1250 if (!flags)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001251 goto colon_s_fail;
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001252 len_R = flags - R;
1253 *flags++ = '\0'; // terminate "replace"
1254 gflag = *flags;
1255
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001256 q = begin_line(q);
1257 if (b < 0) { // maybe :s/foo/bar/
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001258 q = begin_line(dot); // start with cur line
1259 b = count_lines(text, q); // cur line number
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001260 }
1261 if (e < 0)
1262 e = b; // maybe :.s/foo/bar/
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001263
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001264 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001265 char *ls = q; // orig line start
1266 char *found;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001267 vc4:
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001268 found = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
1269 if (found) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001270 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001271 // we found the "find" pattern - delete it
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001272 text_hole_delete(found, found + len_F - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001273 // inset the "replace" patern
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001274 bias = string_insert(found, R); // insert the string
1275 found += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001276 ls += bias;
1277 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001278 // check for "global" :s/foo/bar/g
Denys Vlasenko800a9a02012-01-31 14:10:26 +01001279 if (gflag == 'g') {
1280 if ((found + len_R) < end_line(ls)) {
1281 q = found + len_R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001282 goto vc4; // don't let q move past cur line
1283 }
1284 }
1285 }
1286 q = next_line(ls);
1287 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001288#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001289 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001290 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001291 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1292 || strncmp(cmd, "wq", i) == 0
1293 || strncmp(cmd, "wn", i) == 0
1294 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001295 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001296 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001297 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001298 fn = args;
1299 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001300#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001301 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001302 status_line_bold("\"%s\" File is read only", fn);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001303 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001304 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001305#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001306 // how many lines in text[]?
1307 li = count_lines(q, r);
1308 ch = r - q + 1;
1309 // see if file exists- if not, its just a new file request
1310 if (useforce) {
1311 // if "fn" is not write-able, chmod u+w
1312 // sprintf(syscmd, "chmod u+w %s", fn);
1313 // system(syscmd);
1314 forced = TRUE;
1315 }
1316 l = file_write(fn, q, r);
1317 if (useforce && forced) {
1318 // chmod u-w
1319 // sprintf(syscmd, "chmod u-w %s", fn);
1320 // system(syscmd);
1321 forced = FALSE;
1322 }
Paul Fox61e45db2005-10-09 14:43:22 +00001323 if (l < 0) {
1324 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001325 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001326 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001327 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001328 if (q == text && r == end - 1 && l == ch) {
1329 file_modified = 0;
1330 last_file_modified = -1;
1331 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001332 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1333 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1334 )
1335 && l == ch
1336 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001337 editing = 0;
1338 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001339 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001340#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001341 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001342 if (b < 0) { // no addr given- use defaults
1343 q = begin_line(dot); // assume .,. for the range
1344 r = end_line(dot);
1345 }
1346 text_yank(q, r, YDreg);
1347 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001348 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001349 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001350#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001351 } else {
1352 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001353 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001354 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001355 ret:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001356 dot = bound_dot(dot); // make sure "dot" is valid
1357 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001358#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001359 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001360 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001361#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001362}
Paul Fox61e45db2005-10-09 14:43:22 +00001363
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001364#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001365
1366static void Hit_Return(void)
1367{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001368 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001369
Denis Vlasenkof882f082007-12-23 02:36:51 +00001370 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001371 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001372 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001373 while ((c = get_one_char()) != '\n' && c != '\r')
1374 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001375 redraw(TRUE); // force redraw all
1376}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001377
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001378static int next_tabstop(int col)
1379{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001380 return col + ((tabstop - 1) - (col % tabstop));
1381}
1382
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001383//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001384static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001385{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001386 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001387 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001388 int cnt, ro, co;
1389
1390 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001391
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001392 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001393 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001394 // how many lines do we have to move
1395 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001396 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001397 screenbegin = beg_cur;
1398 if (cnt > (rows - 1) / 2) {
1399 // we moved too many lines. put "dot" in middle of screen
1400 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1401 screenbegin = prev_line(screenbegin);
1402 }
1403 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001404 } else {
1405 char *end_scr; // begin and end of screen
1406 end_scr = end_screen(); // last char of screen
1407 if (beg_cur > end_scr) {
1408 // "d" is after bottom line on screen
1409 // how many lines do we have to move
1410 cnt = count_lines(end_scr, beg_cur);
1411 if (cnt > (rows - 1) / 2)
1412 goto sc1; // too many lines
1413 for (ro = 0; ro < cnt - 1; ro++) {
1414 // move screen begin the same amount
1415 screenbegin = next_line(screenbegin);
1416 // now, move the end of screen
1417 end_scr = next_line(end_scr);
1418 end_scr = end_line(end_scr);
1419 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001420 }
1421 }
1422 // "d" is on screen- find out which row
1423 tp = screenbegin;
1424 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1425 if (tp == beg_cur)
1426 break;
1427 tp = next_line(tp);
1428 }
1429
1430 // find out what col "d" is on
1431 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001432 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001433 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001434 break;
1435 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001436 // handle tabs like real vi
1437 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001438 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001439 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001440 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001441 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1442 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001443 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001444 co++;
1445 tp++;
1446 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001447
1448 // "co" is the column where "dot" is.
1449 // The screen has "columns" columns.
1450 // The currently displayed columns are 0+offset -- columns+ofset
1451 // |-------------------------------------------------------------|
1452 // ^ ^ ^
1453 // offset | |------- columns ----------------|
1454 //
1455 // If "co" is already in this range then we do not have to adjust offset
1456 // but, we do have to subtract the "offset" bias from "co".
1457 // If "co" is outside this range then we have to change "offset".
1458 // If the first char of a line is a tab the cursor will try to stay
1459 // in column 7, but we have to set offset to 0.
1460
1461 if (co < 0 + offset) {
1462 offset = co;
1463 }
1464 if (co >= columns + offset) {
1465 offset = co - columns + 1;
1466 }
1467 // if the first char of the line is a tab, and "dot" is sitting on it
1468 // force offset to 0.
1469 if (d == beg_cur && *d == '\t') {
1470 offset = 0;
1471 }
1472 co -= offset;
1473
1474 *row = ro;
1475 *col = co;
1476}
1477
1478//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001479static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001480{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001481 if (p > text) {
1482 p = memrchr(text, '\n', p - text);
1483 if (!p)
1484 return text;
1485 return p + 1;
1486 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001487 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001488}
1489
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001490static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001491{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001492 if (p < end - 1) {
1493 p = memchr(p, '\n', end - p - 1);
1494 if (!p)
1495 return end - 1;
1496 }
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 *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001501{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001502 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001503 // Try to stay off of the Newline
1504 if (*p == '\n' && (p - begin_line(p)) > 0)
1505 p--;
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 *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001510{
1511 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001512 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001513 p--; // step to prev line
1514 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001515 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001516}
1517
Denis Vlasenkof882f082007-12-23 02:36:51 +00001518static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001519{
1520 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001521 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001522 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001523 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001524}
1525
1526//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001527static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001528{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001529 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001530 int cnt;
1531
1532 // find new bottom line
1533 q = screenbegin;
1534 for (cnt = 0; cnt < rows - 2; cnt++)
1535 q = next_line(q);
1536 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001537 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001538}
1539
Denis Vlasenkof882f082007-12-23 02:36:51 +00001540// count line from start to stop
1541static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001542{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001543 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001544 int cnt;
1545
Denis Vlasenkof882f082007-12-23 02:36:51 +00001546 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001547 q = start;
1548 start = stop;
1549 stop = q;
1550 }
1551 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001552 stop = end_line(stop);
1553 while (start <= stop && start <= end - 1) {
1554 start = end_line(start);
1555 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001556 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001557 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001558 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001559 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001560}
1561
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001562static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001563{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001564 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001565
1566 for (q = text; li > 1; li--) {
1567 q = next_line(q);
1568 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001569 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001570}
1571
1572//----- Dot Movement Routines ----------------------------------
1573static void dot_left(void)
1574{
1575 if (dot > text && dot[-1] != '\n')
1576 dot--;
1577}
1578
1579static void dot_right(void)
1580{
1581 if (dot < end - 1 && *dot != '\n')
1582 dot++;
1583}
1584
1585static void dot_begin(void)
1586{
1587 dot = begin_line(dot); // return pointer to first char cur line
1588}
1589
1590static void dot_end(void)
1591{
1592 dot = end_line(dot); // return pointer to last char cur line
1593}
1594
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001595static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001596{
1597 int co;
1598
1599 p = begin_line(p);
1600 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001601 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001602 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001603 break;
1604 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001605 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001606 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001607 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001608 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001609 co++;
1610 p++;
1611 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001612 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001613}
1614
1615static void dot_next(void)
1616{
1617 dot = next_line(dot);
1618}
1619
1620static void dot_prev(void)
1621{
1622 dot = prev_line(dot);
1623}
1624
1625static void dot_scroll(int cnt, int dir)
1626{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001627 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001628
1629 for (; cnt > 0; cnt--) {
1630 if (dir < 0) {
1631 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001632 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001633 screenbegin = prev_line(screenbegin);
1634 } else {
1635 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001636 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001637 screenbegin = next_line(screenbegin);
1638 }
1639 }
1640 // make sure "dot" stays on the screen so we dont scroll off
1641 if (dot < screenbegin)
1642 dot = screenbegin;
1643 q = end_screen(); // find new bottom line
1644 if (dot > q)
1645 dot = begin_line(q); // is dot is below bottom line?
1646 dot_skip_over_ws();
1647}
1648
1649static void dot_skip_over_ws(void)
1650{
1651 // skip WS
1652 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1653 dot++;
1654}
1655
1656static void dot_delete(void) // delete the char at 'dot'
1657{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001658 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001659}
1660
Denis Vlasenkof882f082007-12-23 02:36:51 +00001661static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001662{
1663 if (p >= end && end > text) {
1664 p = end - 1;
1665 indicate_error('1');
1666 }
1667 if (p < text) {
1668 p = text;
1669 indicate_error('2');
1670 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001671 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001672}
1673
1674//----- Helper Utility Routines --------------------------------
1675
1676//----------------------------------------------------------------
1677//----- Char Routines --------------------------------------------
1678/* Chars that are part of a word-
1679 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1680 * Chars that are Not part of a word (stoppers)
1681 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1682 * Chars that are WhiteSpace
1683 * TAB NEWLINE VT FF RETURN SPACE
1684 * DO NOT COUNT NEWLINE AS WHITESPACE
1685 */
1686
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001687static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001688{
1689 int li;
1690
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001691 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001692 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001693 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001694 // initialize the new screen. assume this will be a empty file.
1695 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001696 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001697 for (li = 1; li < ro - 1; li++) {
1698 screen[(li * co) + 0] = '~';
1699 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001700 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001701}
1702
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001703#if ENABLE_FEATURE_VI_SEARCH
Walter Harmsb9ba5802011-06-27 02:59:37 +02001704
1705# if ENABLE_FEATURE_VI_REGEX_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001706
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001707// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001708static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001709{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001710 char *q;
1711 struct re_pattern_buffer preg;
1712 int i;
Walter Harmsb9ba5802011-06-27 02:59:37 +02001713 int size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001714
1715 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1716 preg.translate = 0;
1717 preg.fastmap = 0;
1718 preg.buffer = 0;
1719 preg.allocated = 0;
1720
1721 // assume a LIMITED forward search
1722 q = next_line(p);
1723 q = end_line(q);
1724 q = end - 1;
1725 if (dir == BACK) {
1726 q = prev_line(p);
1727 q = text;
1728 }
1729 // count the number of chars to search over, forward or backward
1730 size = q - p;
1731 if (size < 0)
1732 size = p - q;
1733 // RANGE could be negative if we are searching backwards
1734 range = q - p;
1735
Walter Harmsb9ba5802011-06-27 02:59:37 +02001736 q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001737 if (q != 0) {
1738 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001739 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001740 i = 0; // return p if pattern not compiled
1741 goto cs1;
1742 }
1743
1744 q = p;
1745 if (range < 0) {
1746 q = p - size;
1747 if (q < text)
1748 q = text;
1749 }
1750 // search for the compiled pattern, preg, in p[]
1751 // range < 0- search backward
1752 // range > 0- search forward
1753 // 0 < start < size
1754 // re_search() < 0 not found or error
1755 // re_search() > 0 index of found pattern
1756 // struct pattern char int int int struct reg
1757 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1758 i = re_search(&preg, q, size, 0, range, 0);
1759 if (i == -1) {
1760 p = 0;
1761 i = 0; // return NULL if pattern not found
1762 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001763 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001764 if (dir == FORWARD) {
1765 p = p + i;
1766 } else {
1767 p = p - i;
1768 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001769 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001770}
Walter Harmsb9ba5802011-06-27 02:59:37 +02001771
1772# else
1773
1774# if ENABLE_FEATURE_VI_SETOPTS
1775static int mycmp(const char *s1, const char *s2, int len)
1776{
1777 if (ignorecase) {
1778 return strncasecmp(s1, s2, len);
1779 }
1780 return strncmp(s1, s2, len);
1781}
1782# else
1783# define mycmp strncmp
1784# endif
1785
1786static char *char_search(char *p, const char *pat, int dir, int range)
1787{
1788 char *start, *stop;
1789 int len;
1790
1791 len = strlen(pat);
1792 if (dir == FORWARD) {
1793 stop = end - 1; // assume range is p - end-1
1794 if (range == LIMITED)
1795 stop = next_line(p); // range is to next line
1796 for (start = p; start < stop; start++) {
1797 if (mycmp(start, pat, len) == 0) {
1798 return start;
1799 }
1800 }
1801 } else if (dir == BACK) {
1802 stop = text; // assume range is text - p
1803 if (range == LIMITED)
1804 stop = prev_line(p); // range is to prev line
1805 for (start = p - len; start >= stop; start--) {
1806 if (mycmp(start, pat, len) == 0) {
1807 return start;
1808 }
1809 }
1810 }
1811 // pattern not found
1812 return NULL;
1813}
1814
1815# endif
1816
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001817#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001818
Denis Vlasenko33875382008-06-21 20:31:50 +00001819static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001820{
1821 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001822 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001823 refresh(FALSE); // show the ^
1824 c = get_one_char();
1825 *p = c;
1826 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001827 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001828 } else if (c == 27) { // Is this an ESC?
1829 cmd_mode = 0;
1830 cmdcnt = 0;
1831 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001832 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001833 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001834 p--;
1835 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001836 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001837 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001838 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001839 p--;
1840 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001841 }
1842 } else {
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001843#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001844 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001845 char *sp; // "save p"
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001846#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001847
1848 if (c == 13)
1849 c = '\n'; // translate \r to \n
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001850#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001851 sp = p; // remember addr of insert
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001852#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001853 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001854#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001855 if (showmatch && strchr(")]}", *sp) != NULL) {
1856 showmatching(sp);
1857 }
1858 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001859 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001860 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001861 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001862 len = strspn(q, " \t"); // space or tab
1863 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001864 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001865 bias = text_hole_make(p, len);
1866 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001867 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001868 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001869 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001870 }
1871 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001872#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001873 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001874 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001875}
1876
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001877// might reallocate text[]! use p += stupid_insert(p, ...),
1878// and be careful to not use pointers into potentially freed text[]!
1879static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001880{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001881 uintptr_t bias;
1882 bias = text_hole_make(p, 1);
1883 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001884 *p = c;
1885 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001886 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001887}
1888
Denis Vlasenko33875382008-06-21 20:31:50 +00001889static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001890{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001891 char *save_dot, *p, *q, *t;
1892 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001893
1894 save_dot = dot;
1895 p = q = dot;
1896
1897 if (strchr("cdy><", c)) {
1898 // these cmds operate on whole lines
1899 p = q = begin_line(p);
1900 for (cnt = 1; cnt < cmdcnt; cnt++) {
1901 q = next_line(q);
1902 }
1903 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001904 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001905 // These cmds operate on char positions
1906 do_cmd(c); // execute movement cmd
1907 q = dot;
1908 } else if (strchr("wW", c)) {
1909 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001910 // if we are at the next word's first char
1911 // step back one char
1912 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001913 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001914 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1915 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1916 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001917 if (dot > text && *dot == '\n')
1918 dot--; // stay off NL
1919 q = dot;
1920 } else if (strchr("H-k{", c)) {
1921 // these operate on multi-lines backwards
1922 q = end_line(dot); // find NL
1923 do_cmd(c); // execute movement cmd
1924 dot_begin();
1925 p = dot;
1926 } else if (strchr("L+j}\r\n", c)) {
1927 // these operate on multi-lines forwards
1928 p = begin_line(dot);
1929 do_cmd(c); // execute movement cmd
1930 dot_end(); // find NL
1931 q = dot;
1932 } else {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001933 // nothing -- this causes any other values of c to
1934 // represent the one-character range under the
1935 // cursor. this is correct for ' ' and 'l', but
1936 // perhaps no others.
1937 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001938 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001939 if (q < p) {
1940 t = q;
1941 q = p;
1942 p = t;
1943 }
1944
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001945 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001946 if (q > p && strchr("^0bBh\b\177", c)) q--;
1947
1948 multiline = 0;
1949 for (t = p; t <= q; t++) {
1950 if (*t == '\n') {
1951 multiline = 1;
1952 break;
1953 }
1954 }
1955
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001956 *start = p;
1957 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001958 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001959 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001960}
1961
Denis Vlasenko33875382008-06-21 20:31:50 +00001962static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001963{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001964 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001965 int test, inc;
1966
1967 inc = dir;
1968 c = c0 = p[0];
1969 ci = p[inc];
1970 test = 0;
1971
1972 if (type == S_BEFORE_WS) {
1973 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001974 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001975 }
1976 if (type == S_TO_WS) {
1977 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001978 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001979 }
1980 if (type == S_OVER_WS) {
1981 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001982 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001983 }
1984 if (type == S_END_PUNCT) {
1985 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001986 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001987 }
1988 if (type == S_END_ALNUM) {
1989 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001990 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001991 }
1992 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001993 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001994}
1995
Denis Vlasenko33875382008-06-21 20:31:50 +00001996static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001997{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001998 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001999
2000 while (st_test(p, type, dir, &c)) {
2001 // make sure we limit search to correct number of lines
2002 if (c == '\n' && --linecnt < 1)
2003 break;
2004 if (dir >= 0 && p >= end - 1)
2005 break;
2006 if (dir < 0 && p <= text)
2007 break;
2008 p += dir; // move to next char
2009 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002010 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002011}
2012
2013// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00002014static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002015{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002016 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002017 int dir, level;
2018
2019 match = ')';
2020 level = 1;
2021 dir = 1; // assume forward
2022 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002023 case '(': match = ')'; break;
2024 case '[': match = ']'; break;
2025 case '{': match = '}'; break;
2026 case ')': match = '('; dir = -1; break;
2027 case ']': match = '['; dir = -1; break;
2028 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002029 }
2030 for (q = p + dir; text <= q && q < end; q += dir) {
2031 // look for match, count levels of pairs (( ))
2032 if (*q == c)
2033 level++; // increase pair levels
2034 if (*q == match)
2035 level--; // reduce pair level
2036 if (level == 0)
2037 break; // found matching pair
2038 }
2039 if (level != 0)
2040 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002041 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002042}
2043
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002044#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002045// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00002046static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002047{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002048 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002049
2050 // we found half of a pair
2051 q = find_pair(p, *p); // get loc of matching char
2052 if (q == NULL) {
2053 indicate_error('3'); // no matching char
2054 } else {
2055 // "q" now points to matching pair
2056 save_dot = dot; // remember where we are
2057 dot = q; // go to new loc
2058 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002059 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002060 dot = save_dot; // go back to old loc
2061 refresh(FALSE);
2062 }
2063}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002064#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002065
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002066// open a hole in text[]
2067// might reallocate text[]! use p += text_hole_make(p, ...),
2068// and be careful to not use pointers into potentially freed text[]!
2069static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002070{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002071 uintptr_t bias = 0;
2072
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002073 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002074 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002075 end += size; // adjust the new END
2076 if (end >= (text + text_size)) {
2077 char *new_text;
2078 text_size += end - (text + text_size) + 10240;
2079 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002080 bias = (new_text - text);
2081 screenbegin += bias;
2082 dot += bias;
2083 end += bias;
2084 p += bias;
Denys Vlasenko800a9a02012-01-31 14:10:26 +01002085#if ENABLE_FEATURE_VI_YANKMARK
2086 {
2087 int i;
2088 for (i = 0; i < ARRAY_SIZE(mark); i++)
2089 if (mark[i])
2090 mark[i] += bias;
2091 }
2092#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +00002093 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002094 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00002095 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002096 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00002097 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002098 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002099}
2100
2101// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00002102static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002103{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002104 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002105 int cnt, hole_size;
2106
2107 // move forwards, from beginning
2108 // assume p <= q
2109 src = q + 1;
2110 dest = p;
2111 if (q < p) { // they are backward- swap them
2112 src = p + 1;
2113 dest = q;
2114 }
2115 hole_size = q - p + 1;
2116 cnt = end - src;
2117 if (src < text || src > end)
2118 goto thd0;
2119 if (dest < text || dest >= end)
2120 goto thd0;
2121 if (src >= end)
2122 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00002123 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002124 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002125 end = end - hole_size; // adjust the new END
2126 if (dest >= end)
2127 dest = end - 1; // make sure dest in below end-1
2128 if (end <= text)
2129 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00002130 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002131 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002132 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002133}
2134
2135// copy text into register, then delete text.
2136// if dist <= 0, do not include, or go past, a NewLine
2137//
Denis Vlasenko33875382008-06-21 20:31:50 +00002138static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002139{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002140 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002141
2142 // make sure start <= stop
2143 if (start > stop) {
2144 // they are backwards, reverse them
2145 p = start;
2146 start = stop;
2147 stop = p;
2148 }
2149 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00002150 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002151 p = start;
2152 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002153 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002154 // dont go past a NewLine
2155 for (; p + 1 <= stop; p++) {
2156 if (p[1] == '\n') {
2157 stop = p; // "stop" just before NewLine
2158 break;
2159 }
2160 }
2161 }
2162 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002163#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002164 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002165#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002166 if (yf == YANKDEL) {
2167 p = text_hole_delete(start, stop);
2168 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002169 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002170}
2171
2172static void show_help(void)
2173{
2174 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002175#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002176 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002177#endif
2178#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002179 "\n\tLast command repeat with \'.\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002180#endif
2181#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00002182 "\n\tLine marking with 'x"
2183 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002184#endif
2185#if ENABLE_FEATURE_VI_READONLY
Walter Harmsb9ba5802011-06-27 02:59:37 +02002186 //not implemented: "\n\tReadonly if vi is called as \"view\""
2187 //redundant: usage text says this too: "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002188#endif
2189#if ENABLE_FEATURE_VI_SET
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002190 "\n\tSome colon mode commands with \':\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002191#endif
2192#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002193 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002194#endif
2195#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002196 "\n\tSignal catching- ^C"
2197 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002198#endif
2199#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002200 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002201#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002202 );
2203}
2204
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002205#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002206static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002207{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002208 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002209 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002210 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002211 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002212 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002213 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002214 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002215 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002216 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002217}
2218
2219static void end_cmd_q(void)
2220{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002221#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002222 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002223#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002224 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002225}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002226#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002227
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002228#if ENABLE_FEATURE_VI_YANKMARK \
2229 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2230 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002231// might reallocate text[]! use p += string_insert(p, ...),
2232// and be careful to not use pointers into potentially freed text[]!
2233static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002234{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002235 uintptr_t bias;
2236 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002237
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002238 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002239 bias = text_hole_make(p, i);
2240 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002241 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002242#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002243 {
2244 int cnt;
2245 for (cnt = 0; *s != '\0'; s++) {
2246 if (*s == '\n')
2247 cnt++;
2248 }
2249 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2250 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002251#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002252 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002253}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002254#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002255
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002256#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002257static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002258{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002259 int cnt = q - p;
2260 if (cnt < 0) { // they are backwards- reverse them
2261 p = q;
2262 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002263 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002264 free(reg[dest]); // if already a yank register, free it
2265 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002266 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002267}
2268
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002269static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002270{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002271 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002272
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002273 c = 'D'; // default to D-reg
2274 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002275 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002276 if (YDreg == 26)
2277 c = 'D';
2278 if (YDreg == 27)
2279 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002280 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002281}
2282
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002283static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002284{
2285 // A context is defined to be "modifying text"
2286 // Any modifying command establishes a new context.
2287
2288 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002289 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002290 // we are trying to modify text[]- make this the current context
2291 mark[27] = mark[26]; // move cur to prev
2292 mark[26] = dot; // move local to cur
2293 context_start = prev_line(prev_line(dot));
2294 context_end = next_line(next_line(dot));
2295 //loiter= start_loiter= now;
2296 }
2297 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002298}
2299
Denis Vlasenkof882f082007-12-23 02:36:51 +00002300static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002301{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002302 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002303
2304 // the current context is in mark[26]
2305 // the previous context is in mark[27]
2306 // only swap context if other context is valid
2307 if (text <= mark[27] && mark[27] <= end - 1) {
2308 tmp = mark[27];
2309 mark[27] = mark[26];
2310 mark[26] = tmp;
2311 p = mark[26]; // where we are going- previous context
2312 context_start = prev_line(prev_line(prev_line(p)));
2313 context_end = next_line(next_line(next_line(p)));
2314 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002315 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002316}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002317#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002318
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002319//----- Set terminal attributes --------------------------------
2320static void rawmode(void)
2321{
2322 tcgetattr(0, &term_orig);
2323 term_vi = term_orig;
Denys Vlasenko6e8861b2012-01-15 23:00:13 +01002324 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG on - allow intr's
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002325 term_vi.c_iflag &= (~IXON & ~ICRNL);
2326 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002327 term_vi.c_cc[VMIN] = 1;
2328 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002329 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002330 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002331}
2332
2333static void cookmode(void)
2334{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002335 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002336 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002337}
2338
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002339#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002340//----- Come here when we get a window resize signal ---------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002341static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002342{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002343 int save_errno = errno;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002344 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002345 signal(SIGWINCH, winch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002346 query_screen_dimensions();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002347 new_screen(rows, columns); // get memory for virtual screen
2348 redraw(TRUE); // re-draw the screen
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002349 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002350}
2351
2352//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002353static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002354{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002355 int save_errno = errno;
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002356 rawmode(); // terminal to "raw"
2357 last_status_cksum = 0; // force status update
2358 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002359
2360 signal(SIGTSTP, suspend_sig);
2361 signal(SIGCONT, SIG_DFL);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002362 //kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
2363 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002364}
2365
2366//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002367static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002368{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002369 int save_errno = errno;
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002370 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002371 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002372
2373 signal(SIGCONT, cont_sig);
2374 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002375 kill(my_pid, SIGTSTP);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002376 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002377}
2378
2379//----- Come here when we get a signal ---------------------------
2380static void catch_sig(int sig)
2381{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002382 signal(SIGINT, catch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002383 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002384}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002385#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002386
Denys Vlasenko606291b2009-09-23 23:15:43 +02002387static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002388{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002389 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002390
Denys Vlasenko606291b2009-09-23 23:15:43 +02002391 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002392 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002393 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002394}
2395
2396//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002397static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002398{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002399 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002400
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002401 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002402 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002403 if (c == -1) { // EOF/error
2404 go_bottom_and_clear_to_eol();
2405 cookmode(); // terminal to "cooked"
2406 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002407 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002408 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002409}
2410
2411//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002412static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002413{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002414 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002415
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002416#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002417 if (!adding2q) {
2418 // we are not adding to the q.
2419 // but, we may be reading from a q
2420 if (ioq == 0) {
2421 // there is no current q, read from STDIN
2422 c = readit(); // get the users input
2423 } else {
2424 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002425 // careful with correct sign expansion!
2426 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002427 if (c == '\0') {
2428 // the end of the q, read from STDIN
2429 free(ioq_start);
2430 ioq_start = ioq = 0;
2431 c = readit(); // get the users input
2432 }
2433 }
2434 } else {
2435 // adding STDIN chars to q
2436 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002437 if (lmc_len >= MAX_INPUT_LEN - 1) {
2438 status_line_bold("last_modifying_cmd overrun");
2439 } else {
2440 // add new char to q
2441 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002442 }
2443 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002444#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002445 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002446#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002447 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002448}
2449
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002450// Get input line (uses "status line" area)
2451static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002452{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002453 // char [MAX_INPUT_LEN]
2454#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002455
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002456 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002457 int i;
2458
2459 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002460 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002461 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002462 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002463
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002464 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002465 while (i < MAX_INPUT_LEN) {
2466 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002467 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002468 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002469 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002470 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002471 buf[--i] = '\0';
2472 write1("\b \b"); // erase char on screen
2473 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002474 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002475 } else if (c > 0 && c < 256) { // exclude Unicode
2476 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002477 buf[i] = c;
2478 buf[++i] = '\0';
2479 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002480 }
2481 }
2482 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002483 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002484#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002485}
2486
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002487static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002488{
2489 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002490 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002491
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002492 cnt = -1;
Denys Vlasenkodef47832010-04-18 20:39:41 -07002493 if (fn && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002494 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002495 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002496}
2497
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002498// might reallocate text[]!
2499static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002500{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002501 int cnt = -1;
2502 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002503 struct stat statbuf;
2504
2505 /* Validate file */
2506 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002507 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002508 goto fi0;
2509 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002510 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002511 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002512 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002513 goto fi0;
2514 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002515 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002516 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002517 goto fi0;
2518 }
2519
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002520 // read file to buffer
2521 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002522 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002523 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002524 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002525 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002526 size = statbuf.st_size;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002527 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002528 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002529 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002530 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002531 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002532 } else if (cnt < size) {
2533 // There was a partial read, shrink unused space text[]
2534 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denys Vlasenko6331cf02009-11-13 09:08:27 +01002535 status_line_bold("can't read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002536 }
2537 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002538 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002539 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002540 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002541#if ENABLE_FEATURE_VI_READONLY
2542 if (update_ro_status
2543 && ((access(fn, W_OK) < 0) ||
2544 /* root will always have access()
2545 * so we check fileperms too */
2546 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2547 )
2548 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002549 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002550 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002551#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002552 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002553}
2554
Denis Vlasenko33875382008-06-21 20:31:50 +00002555static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002556{
2557 int fd, cnt, charcnt;
2558
2559 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002560 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002561 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002562 }
Denis Vlasenko8abae882008-05-03 11:35:59 +00002563 /* By popular request we do not open file with O_TRUNC,
2564 * but instead ftruncate() it _after_ successful write.
2565 * Might reduce amount of data lost on power fail etc.
2566 */
2567 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002568 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002569 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002570 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002571 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002572 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002573 if (charcnt == cnt) {
2574 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002575 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002576 } else {
2577 charcnt = 0;
2578 }
2579 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002580 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002581}
2582
2583//----- Terminal Drawing ---------------------------------------
2584// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002585// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002586// screen coordinates
2587// 0,0 ... 0,79
2588// 1,0 ... 1,79
2589// . ... .
2590// . ... .
2591// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002592// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002593
2594//----- Move the cursor to row x col (count from 0, not 1) -------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002595static void place_cursor(int row, int col, int optimize)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002596{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002597 char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
Denis Vlasenko7b54dc72008-07-17 21:32:32 +00002598#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2599 enum {
2600 SZ_UP = sizeof(CMup),
2601 SZ_DN = sizeof(CMdown),
2602 SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN,
2603 };
2604 char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size
2605#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002606 char *cm;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002607
2608 if (row < 0) row = 0;
2609 if (row >= rows) row = rows - 1;
2610 if (col < 0) col = 0;
2611 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002612
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002613 //----- 1. Try the standard terminal ESC sequence
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002614 sprintf(cm1, CMrc, row + 1, col + 1);
2615 cm = cm1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002616
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002617#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002618 if (optimize && col < 16) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002619 char *screenp;
2620 int Rrow = last_row;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002621 int diff = Rrow - row;
2622
2623 if (diff < -5 || diff > 5)
2624 goto skip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002625
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002626 //----- find the minimum # of chars to move cursor -------------
2627 //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
2628 cm2[0] = '\0';
2629
2630 // move to the correct row
2631 while (row < Rrow) {
2632 // the cursor has to move up
2633 strcat(cm2, CMup);
2634 Rrow--;
2635 }
2636 while (row > Rrow) {
2637 // the cursor has to move down
2638 strcat(cm2, CMdown);
2639 Rrow++;
2640 }
2641
2642 // now move to the correct column
2643 strcat(cm2, "\r"); // start at col 0
2644 // just send out orignal source char to get to correct place
2645 screenp = &screen[row * columns]; // start of screen line
2646 strncat(cm2, screenp, col);
2647
2648 // pick the shortest cursor motion to send out
2649 if (strlen(cm2) < strlen(cm)) {
2650 cm = cm2;
2651 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002652 skip: ;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002653 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002654 last_row = row;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002655#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002656 write1(cm);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002657}
2658
2659//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002660static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002661{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002662 write1(Ceol); // Erase from cursor to end of line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002663}
2664
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002665static void go_bottom_and_clear_to_eol(void)
2666{
2667 place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
2668 clear_to_eol(); // erase to end of line
2669}
2670
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002671//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002672static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002673{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002674 write1(Ceos); // Erase from cursor to end of screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002675}
2676
2677//----- Start standout mode ------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002678static void standout_start(void) // send "start reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002679{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002680 write1(SOs); // Start reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002681}
2682
2683//----- End standout mode --------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002684static void standout_end(void) // send "end reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002685{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002686 write1(SOn); // End reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002687}
2688
2689//----- Flash the screen --------------------------------------
2690static void flash(int h)
2691{
2692 standout_start(); // send "start reverse video" sequence
2693 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002694 mysleep(h);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002695 standout_end(); // send "end reverse video" sequence
2696 redraw(TRUE);
2697}
2698
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002699static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002700{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002701#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002702 if (crashme > 0)
2703 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002704#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002705 if (!err_method) {
2706 write1(bell); // send out a bell character
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002707 } else {
2708 flash(10);
2709 }
2710}
2711
2712//----- Screen[] Routines --------------------------------------
2713//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002714static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002715{
2716 memset(screen, ' ', screensize); // clear new screen
2717}
2718
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002719static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002720{
2721 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002722 char *e = buf + count;
2723
Paul Fox8552aec2005-09-16 12:20:05 +00002724 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002725 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002726 return sum;
2727}
2728
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002729//----- Draw the status line at bottom of the screen -------------
2730static void show_status_line(void)
2731{
Paul Foxc3504852005-09-16 12:48:18 +00002732 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002733
Paul Fox8552aec2005-09-16 12:20:05 +00002734 // either we already have an error or status message, or we
2735 // create one.
2736 if (!have_status_msg) {
2737 cnt = format_edit_status();
2738 cksum = bufsum(status_buffer, cnt);
2739 }
2740 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002741 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002742 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002743 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002744 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002745 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002746 (columns - 1) ) {
2747 have_status_msg = 0;
2748 Hit_Return();
2749 }
2750 have_status_msg = 0;
2751 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002752 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2753 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002754 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002755}
2756
2757//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002758// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002759static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002760{
2761 va_list args;
2762
2763 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002764 strcpy(status_buffer, SOs); // Terminal standout mode on
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002765 vsprintf(status_buffer + sizeof(SOs)-1, format, args);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002766 strcat(status_buffer, SOn); // Terminal standout mode off
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002767 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002768
2769 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002770}
2771
Paul Fox8552aec2005-09-16 12:20:05 +00002772// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002773static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002774{
2775 va_list args;
2776
2777 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002778 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002779 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002780
2781 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002782}
2783
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002784// copy s to buf, convert unprintable
2785static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002786{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002787 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002788 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002789
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002790 buf[0] = '\0';
2791 if (!s[0])
2792 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002793
2794 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002795 for (; *s; s++) {
2796 int c_is_no_print;
2797
2798 c = *s;
2799 c_is_no_print = (c & 0x80) && !Isprint(c);
2800 if (c_is_no_print) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002801 strcpy(d, SOn);
2802 d += sizeof(SOn)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002803 c = '.';
2804 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002805 if (c < ' ' || c == 0x7f) {
2806 *d++ = '^';
2807 c |= '@'; /* 0x40 */
2808 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002809 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002810 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002811 *d++ = c;
2812 *d = '\0';
2813 if (c_is_no_print) {
2814 strcpy(d, SOs);
2815 d += sizeof(SOs)-1;
2816 }
2817 if (*s == '\n') {
2818 *d++ = '$';
2819 *d = '\0';
2820 }
2821 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002822 break;
2823 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002824}
2825
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002826static void not_implemented(const char *s)
2827{
2828 char buf[MAX_INPUT_LEN];
2829
2830 print_literal(buf, s);
2831 status_line_bold("\'%s\' is not implemented", buf);
2832}
2833
2834// show file status on status line
2835static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002836{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002837 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002838
2839#define tot format_edit_status__tot
2840
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002841 int cur, percent, ret, trunc_at;
2842
Paul Fox8552aec2005-09-16 12:20:05 +00002843 // file_modified is now a counter rather than a flag. this
2844 // helps reduce the amount of line counting we need to do.
2845 // (this will cause a mis-reporting of modified status
2846 // once every MAXINT editing operations.)
2847
2848 // it would be nice to do a similar optimization here -- if
2849 // we haven't done a motion that could have changed which line
2850 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002851 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002852
2853 // reduce counting -- the total lines can't have
2854 // changed if we haven't done any edits.
2855 if (file_modified != last_file_modified) {
2856 tot = cur + count_lines(dot, end - 1) - 1;
2857 last_file_modified = file_modified;
2858 }
2859
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002860 // current line percent
2861 // ------------- ~~ ----------
2862 // total lines 100
2863 if (tot > 0) {
2864 percent = (100 * cur) / tot;
2865 } else {
2866 cur = tot = 0;
2867 percent = 100;
2868 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002869
Paul Fox8552aec2005-09-16 12:20:05 +00002870 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2871 columns : STATUS_BUFFER_LEN-1;
2872
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002873 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002874#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002875 "%c %s%s%s %d/%d %d%%",
2876#else
2877 "%c %s%s %d/%d %d%%",
2878#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002879 cmd_mode_indicator[cmd_mode & 3],
2880 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002881#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002882 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002883#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002884 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002885 cur, tot, percent);
2886
2887 if (ret >= 0 && ret < trunc_at)
2888 return ret; /* it all fit */
2889
2890 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002891#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002892}
2893
2894//----- Force refresh of all Lines -----------------------------
2895static void redraw(int full_screen)
2896{
2897 place_cursor(0, 0, FALSE); // put cursor in correct place
Denis Vlasenkob1759462008-06-20 20:20:54 +00002898 clear_to_eos(); // tell terminal to erase display
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002899 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002900 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002901 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002902 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002903}
2904
2905//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002906static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002907{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002908 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002909 int co;
2910 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002911 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002912
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002913 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002914 co = 0;
2915 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002916 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002917 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002918 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002919 if (c == '\n')
2920 break;
2921 if ((c & 0x80) && !Isprint(c)) {
2922 c = '.';
2923 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002924 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002925 if (c == '\t') {
2926 c = ' ';
2927 // co % 8 != 7
2928 while ((co % tabstop) != (tabstop - 1)) {
2929 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002930 }
2931 } else {
2932 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002933 if (c == 0x7f)
2934 c = '?';
2935 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002936 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002937 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002938 }
2939 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002940 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002941 // discard scrolled-off-to-the-left portion,
2942 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002943 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002944 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002945 co -= tabstop;
2946 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002947 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002948 if (src >= end)
2949 break;
2950 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002951 // check "short line, gigantic offset" case
2952 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002953 ofs = co;
2954 // discard last scrolled off part
2955 co -= ofs;
2956 dest += ofs;
2957 // fill the rest with spaces
2958 if (co < columns)
2959 memset(&dest[co], ' ', columns - co);
2960 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002961}
2962
2963//----- Refresh the changed screen lines -----------------------
2964// Copy the source line from text[] into the buffer and note
2965// if the current screenline is different from the new buffer.
2966// If they differ then that line needs redrawing on the terminal.
2967//
2968static void refresh(int full_screen)
2969{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002970#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002971
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002972 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002973 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002974
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +02002975 if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002976 unsigned c = columns, r = rows;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002977 query_screen_dimensions();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002978 full_screen |= (c - columns) | (r - rows);
2979 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002980 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2981 tp = screenbegin; // index into text[] of top line
2982
2983 // compare text[] to screen[] and mark screen[] lines that need updating
2984 for (li = 0; li < rows - 1; li++) {
2985 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002986 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002987 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002988 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002989
2990 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002991 if (tp < end) {
2992 char *t = memchr(tp, '\n', end - tp);
2993 if (!t) t = end - 1;
2994 tp = t + 1;
2995 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002996
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002997 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002998 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002999 cs = 0;
3000 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003001 sp = &screen[li * columns]; // start of screen line
3002 if (full_screen) {
3003 // force re-draw of every single column from 0 - columns-1
3004 goto re0;
3005 }
3006 // compare newly formatted buffer with virtual screen
3007 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003008 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003009 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003010 changed = TRUE; // mark for redraw
3011 break;
3012 }
3013 }
3014
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003015 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003016 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003017 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003018 changed = TRUE; // mark for redraw
3019 break;
3020 }
3021 }
3022 // now, cs is index of first diff, and ce is index of last diff
3023
3024 // if horz offset has changed, force a redraw
3025 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003026 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003027 changed = TRUE;
3028 }
3029
3030 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003031 if (cs < 0) cs = 0;
3032 if (ce > columns - 1) ce = columns - 1;
3033 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003034 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003035 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003036 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003037 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003038
3039 // move cursor to column of first change
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003040 //if (offset != old_offset) {
3041 // // place_cursor is still too stupid
3042 // // to handle offsets correctly
3043 // place_cursor(li, cs, FALSE);
3044 //} else {
3045 place_cursor(li, cs, TRUE);
3046 //}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003047
3048 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003049 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003050 }
3051 }
3052
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003053 place_cursor(crow, ccol, TRUE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003054
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003055 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003056#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003057}
3058
Eric Andersen3f980402001-04-04 17:31:15 +00003059//---------------------------------------------------------------------
3060//----- the Ascii Chart -----------------------------------------------
3061//
3062// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
3063// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
3064// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
3065// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
3066// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
3067// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
3068// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
3069// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
3070// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
3071// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
3072// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
3073// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
3074// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
3075// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
3076// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
3077// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
3078//---------------------------------------------------------------------
3079
3080//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003081static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00003082{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003083 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003084 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003085 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00003086 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003087 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003088
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00003089// c1 = c; // quiet the compiler
3090// cnt = yf = 0; // quiet the compiler
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003091// p = q = save_dot = buf; // quiet the compiler
3092 memset(buf, '\0', sizeof(buf));
Eric Andersenbff7a602001-11-17 07:15:43 +00003093
Paul Fox8552aec2005-09-16 12:20:05 +00003094 show_status_line();
3095
Eric Andersenbff7a602001-11-17 07:15:43 +00003096 /* if this is a cursor key, skip these checks */
3097 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003098 case KEYCODE_UP:
3099 case KEYCODE_DOWN:
3100 case KEYCODE_LEFT:
3101 case KEYCODE_RIGHT:
3102 case KEYCODE_HOME:
3103 case KEYCODE_END:
3104 case KEYCODE_PAGEUP:
3105 case KEYCODE_PAGEDOWN:
3106 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00003107 goto key_cmd_mode;
3108 }
3109
Eric Andersen3f980402001-04-04 17:31:15 +00003110 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003111 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003112 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003113 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00003114 // we are 'R'eplacing the current *dot with new char
3115 if (*dot == '\n') {
3116 // don't Replace past E-o-l
3117 cmd_mode = 1; // convert to insert
3118 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003119 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003120 if (c != 27)
3121 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3122 dot = char_insert(dot, c); // insert new char
3123 }
3124 goto dc1;
3125 }
3126 }
3127 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003128 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003129 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00003130 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003131 if (1 <= c || Isprint(c)) {
3132 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00003133 }
3134 goto dc1;
3135 }
3136
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003137 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00003138 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00003139 //case 0x01: // soh
3140 //case 0x09: // ht
3141 //case 0x0b: // vt
3142 //case 0x0e: // so
3143 //case 0x0f: // si
3144 //case 0x10: // dle
3145 //case 0x11: // dc1
3146 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003147#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00003148 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00003149 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003150 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003151#endif
Eric Andersen822c3832001-05-07 17:37:43 +00003152 //case 0x16: // syn
3153 //case 0x17: // etb
3154 //case 0x18: // can
3155 //case 0x1c: // fs
3156 //case 0x1d: // gs
3157 //case 0x1e: // rs
3158 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003159 //case '!': // !-
3160 //case '#': // #-
3161 //case '&': // &-
3162 //case '(': // (-
3163 //case ')': // )-
3164 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003165 //case '=': // =-
3166 //case '@': // @-
3167 //case 'F': // F-
3168 //case 'K': // K-
3169 //case 'Q': // Q-
3170 //case 'S': // S-
3171 //case 'T': // T-
3172 //case 'V': // V-
3173 //case '[': // [-
3174 //case '\\': // \-
3175 //case ']': // ]-
3176 //case '_': // _-
3177 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003178 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003179 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003180 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00003181 buf[0] = c;
3182 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003183 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003184 end_cmd_q(); // stop adding to q
3185 case 0x00: // nul- ignore
3186 break;
3187 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003188 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00003189 dot_scroll(rows - 2, -1);
3190 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003191 case 4: // ctrl-D scroll down half screen
3192 dot_scroll((rows - 2) / 2, 1);
3193 break;
3194 case 5: // ctrl-E scroll down one line
3195 dot_scroll(1, 1);
3196 break;
3197 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003198 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003199 dot_scroll(rows - 2, 1);
3200 break;
3201 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00003202 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003203 break;
3204 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003205 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003206 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003207 case 0x7f: // DEL- move left (This may be ERASE char)
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003208 do {
3209 dot_left();
3210 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003211 break;
3212 case 10: // Newline ^J
3213 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003214 case KEYCODE_DOWN: // cursor key Down
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003215 do {
3216 dot_next(); // go to next B-o-l
3217 // try stay in same col
3218 dot = move_to_col(dot, ccol + offset);
3219 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003220 break;
3221 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003222 case 18: // ctrl-R force redraw
Eric Andersen822c3832001-05-07 17:37:43 +00003223 place_cursor(0, 0, FALSE); // put cursor in correct place
Eric Andersen3f980402001-04-04 17:31:15 +00003224 clear_to_eos(); // tel terminal to erase display
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003225 mysleep(10);
Eric Andersen3f980402001-04-04 17:31:15 +00003226 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003227 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003228 refresh(TRUE); // this will redraw the entire display
3229 break;
3230 case 13: // Carriage Return ^M
3231 case '+': // +- goto next line
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003232 do {
3233 dot_next();
3234 dot_skip_over_ws();
3235 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003236 break;
3237 case 21: // ctrl-U scroll up half screen
3238 dot_scroll((rows - 2) / 2, -1);
3239 break;
3240 case 25: // ctrl-Y scroll up one line
3241 dot_scroll(1, -1);
3242 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003243 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003244 if (cmd_mode == 0)
3245 indicate_error(c);
3246 cmd_mode = 0; // stop insrting
3247 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003248 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003249 break;
3250 case ' ': // move right
3251 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003252 case KEYCODE_RIGHT: // Cursor Key Right
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003253 do {
3254 dot_right();
3255 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003256 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003257#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003258 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003259 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3260 if ((unsigned)c1 <= 25) { // a-z?
3261 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003262 } else {
3263 indicate_error(c);
3264 }
3265 break;
3266 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003267 c1 = (get_one_char() | 0x20) - 'a';
3268 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003269 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003270 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003271 if (text <= q && q < end) {
3272 dot = q;
3273 dot_begin(); // go to B-o-l
3274 dot_skip_over_ws();
3275 }
3276 } else if (c1 == '\'') { // goto previous context
3277 dot = swap_context(dot); // swap current and previous context
3278 dot_begin(); // go to B-o-l
3279 dot_skip_over_ws();
3280 } else {
3281 indicate_error(c);
3282 }
3283 break;
3284 case 'm': // m- Mark a line
3285 // this is really stupid. If there are any inserts or deletes
3286 // between text[0] and dot then this mark will not point to the
3287 // correct location! It could be off by many lines!
3288 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003289 c1 = (get_one_char() | 0x20) - 'a';
3290 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003291 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003292 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003293 } else {
3294 indicate_error(c);
3295 }
3296 break;
3297 case 'P': // P- Put register before
3298 case 'p': // p- put register after
3299 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003300 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003301 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003302 break;
3303 }
3304 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003305 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003306 if (c == 'P') {
3307 dot_begin(); // putting lines- Put above
3308 }
3309 if (c == 'p') {
3310 // are we putting after very last line?
3311 if (end_line(dot) == (end - 1)) {
3312 dot = end; // force dot to end of text[]
3313 } else {
3314 dot_next(); // next line, then put before
3315 }
3316 }
3317 } else {
3318 if (c == 'p')
3319 dot_right(); // move to right, can move to NL
3320 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003321 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003322 end_cmd_q(); // stop adding to q
3323 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003324 case 'U': // U- Undo; replace current line with original version
Denys Vlasenko800a9a02012-01-31 14:10:26 +01003325 if (reg[Ureg] != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003326 p = begin_line(dot);
3327 q = end_line(dot);
3328 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003329 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003330 dot = p;
3331 dot_skip_over_ws();
3332 }
3333 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003334#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003335 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003336 case KEYCODE_END: // Cursor Key End
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003337 for (;;) {
3338 dot = end_line(dot);
Denys Vlasenko1fd71292011-11-28 04:55:48 +01003339 if (--cmdcnt <= 0)
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003340 break;
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003341 dot_next();
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003342 }
Eric Andersen3f980402001-04-04 17:31:15 +00003343 break;
3344 case '%': // %- find matching char of pair () [] {}
3345 for (q = dot; q < end && *q != '\n'; q++) {
3346 if (strchr("()[]{}", *q) != NULL) {
3347 // we found half of a pair
3348 p = find_pair(q, *q);
3349 if (p == NULL) {
3350 indicate_error(c);
3351 } else {
3352 dot = p;
3353 }
3354 break;
3355 }
3356 }
3357 if (*q == '\n')
3358 indicate_error(c);
3359 break;
3360 case 'f': // f- forward to a user specified char
3361 last_forward_char = get_one_char(); // get the search char
3362 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003363 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003364 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003365 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003366 case ';': // ;- look at rest of line for last forward char
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003367 do {
3368 if (last_forward_char == 0)
3369 break;
3370 q = dot + 1;
3371 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3372 q++;
3373 }
3374 if (*q == last_forward_char)
3375 dot = q;
3376 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003377 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003378 case ',': // repeat latest 'f' in opposite direction
Paul Foxb5ee8db2008-02-14 01:17:01 +00003379 if (last_forward_char == 0)
3380 break;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003381 do {
3382 q = dot - 1;
3383 while (q >= text && *q != '\n' && *q != last_forward_char) {
3384 q--;
3385 }
3386 if (q >= text && *q == last_forward_char)
3387 dot = q;
3388 } while (--cmdcnt > 0);
Paul Foxb5ee8db2008-02-14 01:17:01 +00003389 break;
3390
Eric Andersen3f980402001-04-04 17:31:15 +00003391 case '-': // -- goto prev line
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003392 do {
3393 dot_prev();
3394 dot_skip_over_ws();
3395 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003396 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003397#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003398 case '.': // .- repeat the last modifying command
3399 // Stuff the last_modifying_cmd back into stdin
3400 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003401 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003402 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003403 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003404 }
3405 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003406#endif
3407#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003408 case '?': // /- search for a pattern
3409 case '/': // /- search for a pattern
3410 buf[0] = c;
3411 buf[1] = '\0';
3412 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003413 if (q[0] && !q[1]) {
3414 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003415 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003416 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003417 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003418 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003419 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003420 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003421 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003422 goto dc3; // now find the pattern
3423 }
3424 // user changed mind and erased the "/"- do nothing
3425 break;
3426 case 'N': // N- backward search for last pattern
Eric Andersen3f980402001-04-04 17:31:15 +00003427 dir = BACK; // assume BACKWARD search
3428 p = dot - 1;
3429 if (last_search_pattern[0] == '?') {
3430 dir = FORWARD;
3431 p = dot + 1;
3432 }
3433 goto dc4; // now search for pattern
3434 break;
3435 case 'n': // n- repeat search for last pattern
3436 // search rest of text[] starting at next char
3437 // if search fails return orignal "p" not the "p+1" address
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003438 do {
3439 const char *msg;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003440 dc3:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003441 dir = FORWARD; // assume FORWARD search
3442 p = dot + 1;
3443 if (last_search_pattern[0] == '?') {
3444 dir = BACK;
3445 p = dot - 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003446 }
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003447 dc4:
3448 q = char_search(p, last_search_pattern + 1, dir, FULL);
3449 if (q != NULL) {
3450 dot = q; // good search, update "dot"
3451 msg = NULL;
3452 goto dc2;
3453 }
3454 // no pattern found between "dot" and "end"- continue at top
3455 p = text;
3456 if (dir == BACK) {
3457 p = end - 1;
3458 }
3459 q = char_search(p, last_search_pattern + 1, dir, FULL);
3460 if (q != NULL) { // found something
3461 dot = q; // found new pattern- goto it
3462 msg = "search hit BOTTOM, continuing at TOP";
3463 if (dir == BACK) {
3464 msg = "search hit TOP, continuing at BOTTOM";
3465 }
3466 } else {
3467 msg = "Pattern not found";
3468 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003469 dc2:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003470 if (msg)
3471 status_line_bold("%s", msg);
3472 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003473 break;
3474 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003475 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003476 if (q != NULL) { // found blank line
3477 dot = next_line(q); // move to next blank line
3478 }
3479 break;
3480 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003481 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003482 if (q != NULL) { // found blank line
3483 dot = next_line(q); // move to next blank line
3484 }
3485 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003486#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003487 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003488 case '1': // 1-
3489 case '2': // 2-
3490 case '3': // 3-
3491 case '4': // 4-
3492 case '5': // 5-
3493 case '6': // 6-
3494 case '7': // 7-
3495 case '8': // 8-
3496 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003497 if (c == '0' && cmdcnt < 1) {
3498 dot_begin(); // this was a standalone zero
3499 } else {
3500 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3501 }
3502 break;
3503 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003504 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003505#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003506 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003507#else
Eric Andersen822c3832001-05-07 17:37:43 +00003508 if (*p == ':')
3509 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003510 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003511 if (cnt <= 0)
3512 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003513 if (strncmp(p, "quit", cnt) == 0
3514 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003515 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003516 if (file_modified && p[1] != '!') {
Dennis Groenenc0657e02012-01-31 14:12:38 +01003517 status_line_bold("No write since last change (:%s! overrides)", p);
Eric Andersen3f980402001-04-04 17:31:15 +00003518 } else {
3519 editing = 0;
3520 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003521 } else if (strncmp(p, "write", cnt) == 0
3522 || strncmp(p, "wq", cnt) == 0
3523 || strncmp(p, "wn", cnt) == 0
3524 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003525 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003526 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003527 if (cnt < 0) {
3528 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003529 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003530 } else {
3531 file_modified = 0;
3532 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003533 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003534 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3535 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3536 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003537 editing = 0;
3538 }
Eric Andersen3f980402001-04-04 17:31:15 +00003539 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003540 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003541 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003542 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003543 dot = find_line(j); // go to line # j
3544 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003545 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003546 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003547 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003548#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003549 break;
3550 case '<': // <- Left shift something
3551 case '>': // >- Right shift something
3552 cnt = count_lines(text, dot); // remember what line we are on
3553 c1 = get_one_char(); // get the type of thing to delete
3554 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003555 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003556 p = begin_line(p);
3557 q = end_line(q);
3558 i = count_lines(p, q); // # of lines we are shifting
3559 for ( ; i > 0; i--, p = next_line(p)) {
3560 if (c == '<') {
3561 // shift left- remove tab or 8 spaces
3562 if (*p == '\t') {
3563 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003564 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003565 } else if (*p == ' ') {
3566 // we should be calculating columns, not just SPACE
3567 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003568 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003569 }
3570 }
3571 } else if (c == '>') {
3572 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003573 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003574 }
3575 }
3576 dot = find_line(cnt); // what line were we on
3577 dot_skip_over_ws();
3578 end_cmd_q(); // stop adding to q
3579 break;
3580 case 'A': // A- append at e-o-l
3581 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003582 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003583 case 'a': // a- append after current char
3584 if (*dot != '\n')
3585 dot++;
3586 goto dc_i;
3587 break;
3588 case 'B': // B- back a blank-delimited Word
3589 case 'E': // E- end of a blank-delimited word
3590 case 'W': // W- forward a blank-delimited word
Eric Andersen3f980402001-04-04 17:31:15 +00003591 dir = FORWARD;
3592 if (c == 'B')
3593 dir = BACK;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003594 do {
3595 if (c == 'W' || isspace(dot[dir])) {
3596 dot = skip_thing(dot, 1, dir, S_TO_WS);
3597 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3598 }
3599 if (c != 'W')
3600 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3601 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003602 break;
3603 case 'C': // C- Change to e-o-l
3604 case 'D': // D- delete to e-o-l
3605 save_dot = dot;
3606 dot = dollar_line(dot); // move to before NL
3607 // copy text into a register and delete
3608 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3609 if (c == 'C')
3610 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003611#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003612 if (c == 'D')
3613 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003614#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003615 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003616 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003617 c1 = get_one_char();
3618 if (c1 != 'g') {
3619 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003620 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003621 buf[2] = '\0';
3622 not_implemented(buf);
3623 break;
3624 }
3625 if (cmdcnt == 0)
3626 cmdcnt = 1;
3627 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003628 case 'G': // G- goto to a line number (default= E-O-F)
3629 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003630 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003631 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003632 }
3633 dot_skip_over_ws();
3634 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003635 case 'H': // H- goto top line on screen
3636 dot = screenbegin;
3637 if (cmdcnt > (rows - 1)) {
3638 cmdcnt = (rows - 1);
3639 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003640 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003641 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003642 }
Eric Andersen3f980402001-04-04 17:31:15 +00003643 dot_skip_over_ws();
3644 break;
3645 case 'I': // I- insert before first non-blank
3646 dot_begin(); // 0
3647 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003648 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003649 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003650 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003651 dc_i:
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003652 cmd_mode = 1; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003653 break;
3654 case 'J': // J- join current and next lines together
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003655 do {
3656 dot_end(); // move to NL
3657 if (dot < end - 1) { // make sure not last char in text[]
3658 *dot++ = ' '; // replace NL with space
3659 file_modified++;
3660 while (isblank(*dot)) { // delete leading WS
3661 dot_delete();
3662 }
Eric Andersen3f980402001-04-04 17:31:15 +00003663 }
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003664 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003665 end_cmd_q(); // stop adding to q
3666 break;
3667 case 'L': // L- goto bottom line on screen
3668 dot = end_screen();
3669 if (cmdcnt > (rows - 1)) {
3670 cmdcnt = (rows - 1);
3671 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003672 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003673 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003674 }
Eric Andersen3f980402001-04-04 17:31:15 +00003675 dot_begin();
3676 dot_skip_over_ws();
3677 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003678 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003679 dot = screenbegin;
3680 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3681 dot = next_line(dot);
3682 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003683 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003684 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003685 p = begin_line(dot);
3686 if (p[-1] == '\n') {
3687 dot_prev();
3688 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3689 dot_end();
3690 dot = char_insert(dot, '\n');
3691 } else {
3692 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003693 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003694 dot_prev(); // -
3695 }
3696 goto dc_i;
3697 break;
3698 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003699 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003700 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003701 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003702 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003703 c = 'x';
3704 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003705 case 'X': // X- delete char before dot
3706 case 'x': // x- delete the current char
3707 case 's': // s- substitute the current char
Eric Andersen3f980402001-04-04 17:31:15 +00003708 dir = 0;
3709 if (c == 'X')
3710 dir = -1;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003711 do {
3712 if (dot[dir] != '\n') {
3713 if (c == 'X')
3714 dot--; // delete prev char
3715 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3716 }
3717 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003718 end_cmd_q(); // stop adding to q
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003719 if (c == 's')
3720 goto dc_i; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003721 break;
3722 case 'Z': // Z- if modified, {write}; exit
3723 // ZZ means to save file (if necessary), then exit
3724 c1 = get_one_char();
3725 if (c1 != 'Z') {
3726 indicate_error(c);
3727 break;
3728 }
Paul Foxf0305b72006-03-28 14:18:21 +00003729 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003730 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003731 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003732 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003733 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003734 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003735 if (cnt < 0) {
3736 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003737 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003738 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003739 editing = 0;
3740 }
3741 } else {
3742 editing = 0;
3743 }
3744 break;
3745 case '^': // ^- move to first non-blank on line
3746 dot_begin();
3747 dot_skip_over_ws();
3748 break;
3749 case 'b': // b- back a word
3750 case 'e': // e- end of word
Eric Andersen3f980402001-04-04 17:31:15 +00003751 dir = FORWARD;
3752 if (c == 'b')
3753 dir = BACK;
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003754 do {
3755 if ((dot + dir) < text || (dot + dir) > end - 1)
3756 break;
3757 dot += dir;
3758 if (isspace(*dot)) {
3759 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3760 }
3761 if (isalnum(*dot) || *dot == '_') {
3762 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3763 } else if (ispunct(*dot)) {
3764 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3765 }
3766 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003767 break;
3768 case 'c': // c- change something
3769 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003770#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003771 case 'y': // y- yank something
3772 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003773#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003774 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003775 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003776 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003777#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003778 if (c == 'y' || c == 'Y')
3779 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003780#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003781 c1 = 'y';
3782 if (c != 'Y')
3783 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003784 // determine range, and whether it spans lines
3785 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003786 if (c1 == 27) { // ESC- user changed mind and wants out
3787 c = c1 = 27; // Escape- do nothing
3788 } else if (strchr("wW", c1)) {
3789 if (c == 'c') {
3790 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003791 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003792 if (q <= text || q[-1] == '\n')
3793 break;
3794 q--;
3795 }
3796 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003797 dot = yank_delete(p, q, ml, yf); // delete word
3798 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3799 // partial line copy text into a register and delete
3800 dot = yank_delete(p, q, ml, yf); // delete word
3801 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3802 // whole line copy text into a register and delete
3803 dot = yank_delete(p, q, ml, yf); // delete lines
3804 whole = 1;
3805 } else {
3806 // could not recognize object
3807 c = c1 = 27; // error-
3808 ml = 0;
3809 indicate_error(c);
3810 }
3811 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003812 if (c == 'c') {
3813 dot = char_insert(dot, '\n');
3814 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003815 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003816 dot_prev();
3817 }
3818 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003819 dot_begin();
3820 dot_skip_over_ws();
3821 }
Eric Andersen3f980402001-04-04 17:31:15 +00003822 }
3823 if (c1 != 27) {
3824 // if CHANGING, not deleting, start inserting after the delete
3825 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003826 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003827 goto dc_i; // start inserting
3828 }
3829 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003830 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003831 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003832#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003833 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003834 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003835 }
3836 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003837 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003838 for (cnt = 0; p <= q; p++) {
3839 if (*p == '\n')
3840 cnt++;
3841 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003842 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003843 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003844#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003845 end_cmd_q(); // stop adding to q
3846 }
3847 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003848 }
Eric Andersen3f980402001-04-04 17:31:15 +00003849 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003850 case KEYCODE_UP: // cursor key Up
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003851 do {
3852 dot_prev();
3853 dot = move_to_col(dot, ccol + offset); // try stay in same col
3854 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003855 break;
3856 case 'r': // r- replace the current char with user input
3857 c1 = get_one_char(); // get the replacement char
3858 if (*dot != '\n') {
3859 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003860 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003861 }
3862 end_cmd_q(); // stop adding to q
3863 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003864 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003865 last_forward_char = get_one_char();
3866 do_cmd(';');
3867 if (*dot == last_forward_char)
3868 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003869 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003870 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003871 case 'w': // w- forward a word
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003872 do {
3873 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3874 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3875 } else if (ispunct(*dot)) { // we are on PUNCT
3876 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3877 }
3878 if (dot < end - 1)
3879 dot++; // move over word
3880 if (isspace(*dot)) {
3881 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3882 }
3883 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003884 break;
3885 case 'z': // z-
3886 c1 = get_one_char(); // get the replacement char
3887 cnt = 0;
3888 if (c1 == '.')
3889 cnt = (rows - 2) / 2; // put dot at center
3890 if (c1 == '-')
3891 cnt = rows - 2; // put dot at bottom
3892 screenbegin = begin_line(dot); // start dot at top
3893 dot_scroll(cnt, -1);
3894 break;
3895 case '|': // |- move to column "cmdcnt"
3896 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3897 break;
3898 case '~': // ~- flip the case of letters a-z -> A-Z
Denys Vlasenko12e154f2011-09-09 12:35:49 +02003899 do {
3900 if (islower(*dot)) {
3901 *dot = toupper(*dot);
3902 file_modified++;
3903 } else if (isupper(*dot)) {
3904 *dot = tolower(*dot);
3905 file_modified++;
3906 }
3907 dot_right();
3908 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003909 end_cmd_q(); // stop adding to q
3910 break;
3911 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003912 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003913 dot_begin();
3914 break;
3915 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003916#if 0
3917 case KEYCODE_FUN1: // Function Key F1
3918 case KEYCODE_FUN2: // Function Key F2
3919 case KEYCODE_FUN3: // Function Key F3
3920 case KEYCODE_FUN4: // Function Key F4
3921 case KEYCODE_FUN5: // Function Key F5
3922 case KEYCODE_FUN6: // Function Key F6
3923 case KEYCODE_FUN7: // Function Key F7
3924 case KEYCODE_FUN8: // Function Key F8
3925 case KEYCODE_FUN9: // Function Key F9
3926 case KEYCODE_FUN10: // Function Key F10
3927 case KEYCODE_FUN11: // Function Key F11
3928 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003929 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003930#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003931 }
3932
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003933 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003934 // if text[] just became empty, add back an empty line
3935 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003936 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003937 dot = text;
3938 }
3939 // it is OK for dot to exactly equal to end, otherwise check dot validity
3940 if (dot != end) {
3941 dot = bound_dot(dot); // make sure "dot" is valid
3942 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003943#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003944 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003945#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003946
3947 if (!isdigit(c))
3948 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3949 cnt = dot - begin_line(dot);
3950 // Try to stay off of the Newline
3951 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3952 dot--;
3953}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003954
Paul Fox35e9c5d2008-03-06 16:26:12 +00003955/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003956#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003957static int totalcmds = 0;
3958static int Mp = 85; // Movement command Probability
3959static int Np = 90; // Non-movement command Probability
3960static int Dp = 96; // Delete command Probability
3961static int Ip = 97; // Insert command Probability
3962static int Yp = 98; // Yank command Probability
3963static int Pp = 99; // Put command Probability
3964static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003965static const char chars[20] = "\t012345 abcdABCD-=.$";
3966static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003967 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003968 "broadcast", "the", "emergency", "of",
3969 "system", "quick", "brown", "fox",
3970 "jumped", "over", "lazy", "dogs",
3971 "back", "January", "Febuary", "March"
3972};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003973static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003974 "You should have received a copy of the GNU General Public License\n",
3975 "char c, cm, *cmd, *cmd1;\n",
3976 "generate a command by percentages\n",
3977 "Numbers may be typed as a prefix to some commands.\n",
3978 "Quit, discarding changes!\n",
3979 "Forced write, if permission originally not valid.\n",
3980 "In general, any ex or ed command (such as substitute or delete).\n",
3981 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3982 "Please get w/ me and I will go over it with you.\n",
3983 "The following is a list of scheduled, committed changes.\n",
3984 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3985 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3986 "Any question about transactions please contact Sterling Huxley.\n",
3987 "I will try to get back to you by Friday, December 31.\n",
3988 "This Change will be implemented on Friday.\n",
3989 "Let me know if you have problems accessing this;\n",
3990 "Sterling Huxley recently added you to the access list.\n",
3991 "Would you like to go to lunch?\n",
3992 "The last command will be automatically run.\n",
3993 "This is too much english for a computer geek.\n",
3994};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003995static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003996 "You should have received a copy of the GNU General Public License\n",
3997 "char c, cm, *cmd, *cmd1;\n",
3998 "generate a command by percentages\n",
3999 "Numbers may be typed as a prefix to some commands.\n",
4000 "Quit, discarding changes!\n",
4001 "Forced write, if permission originally not valid.\n",
4002 "In general, any ex or ed command (such as substitute or delete).\n",
4003 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
4004 "Please get w/ me and I will go over it with you.\n",
4005 "The following is a list of scheduled, committed changes.\n",
4006 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
4007 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
4008 "Any question about transactions please contact Sterling Huxley.\n",
4009 "I will try to get back to you by Friday, December 31.\n",
4010 "This Change will be implemented on Friday.\n",
4011 "Let me know if you have problems accessing this;\n",
4012 "Sterling Huxley recently added you to the access list.\n",
4013 "Would you like to go to lunch?\n",
4014 "The last command will be automatically run.\n",
4015 "This is too much english for a computer geek.\n",
4016};
4017
4018// create a random command to execute
4019static void crash_dummy()
4020{
4021 static int sleeptime; // how long to pause between commands
4022 char c, cm, *cmd, *cmd1;
4023 int i, cnt, thing, rbi, startrbi, percent;
4024
4025 // "dot" movement commands
4026 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
4027
4028 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02004029 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004030 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004031 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02004032 readbuffer[0] = 'X';
4033 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004034 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004035 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004036 // generate a command by percentages
4037 percent = (int) lrand48() % 100; // get a number from 0-99
4038 if (percent < Mp) { // Movement commands
4039 // available commands
4040 cmd = cmd1;
4041 M++;
4042 } else if (percent < Np) { // non-movement commands
4043 cmd = "mz<>\'\""; // available commands
4044 N++;
4045 } else if (percent < Dp) { // Delete commands
4046 cmd = "dx"; // available commands
4047 D++;
4048 } else if (percent < Ip) { // Inset commands
4049 cmd = "iIaAsrJ"; // available commands
4050 I++;
4051 } else if (percent < Yp) { // Yank commands
4052 cmd = "yY"; // available commands
4053 Y++;
4054 } else if (percent < Pp) { // Put commands
4055 cmd = "pP"; // available commands
4056 P++;
4057 } else {
4058 // We do not know how to handle this command, try again
4059 U++;
4060 goto cd0;
4061 }
4062 // randomly pick one of the available cmds from "cmd[]"
4063 i = (int) lrand48() % strlen(cmd);
4064 cm = cmd[i];
4065 if (strchr(":\024", cm))
4066 goto cd0; // dont allow colon or ctrl-T commands
4067 readbuffer[rbi++] = cm; // put cmd into input buffer
4068
4069 // now we have the command-
4070 // there are 1, 2, and multi char commands
4071 // find out which and generate the rest of command as necessary
4072 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
4073 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
4074 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
4075 cmd1 = "abcdefghijklmnopqrstuvwxyz";
4076 }
4077 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4078 c = cmd1[thing];
4079 readbuffer[rbi++] = c; // add movement to input buffer
4080 }
4081 if (strchr("iIaAsc", cm)) { // multi-char commands
4082 if (cm == 'c') {
4083 // change some thing
4084 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4085 c = cmd1[thing];
4086 readbuffer[rbi++] = c; // add movement to input buffer
4087 }
4088 thing = (int) lrand48() % 4; // what thing to insert
4089 cnt = (int) lrand48() % 10; // how many to insert
4090 for (i = 0; i < cnt; i++) {
4091 if (thing == 0) { // insert chars
4092 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
4093 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004094 strcat(readbuffer, words[(int) lrand48() % 20]);
4095 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004096 sleeptime = 0; // how fast to type
4097 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004098 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004099 sleeptime = 0; // how fast to type
4100 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004101 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004102 sleeptime = 0; // how fast to type
4103 }
4104 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004105 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004106 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02004107 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004108 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004109 totalcmds++;
4110 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004111 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004112}
4113
4114// test to see if there are any errors
4115static void crash_test()
4116{
4117 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004118
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004119 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004120 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004121
4122 msg[0] = '\0';
4123 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004124 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004125 }
4126 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004127 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004128 }
4129 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004130 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004131 }
4132 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004133 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004134 }
4135 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004136 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004137 }
4138 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004139 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004140 }
4141
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004142 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00004143 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004144 totalcmds, last_input_char, msg, SOs, SOn);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01004145 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00004146 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004147 if (d[0] == '\n' || d[0] == '\r')
4148 break;
4149 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004150 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004151 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004152 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004153 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004154 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
4155 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
4156 oldtim = tim;
4157 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004158}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00004159#endif