blob: 5245aca9d4ad61469c4c9b1ad21b7dd5fafc4cf8 [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
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200283 int 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
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200600 argv += optind;
601 argc -= optind;
Eric Andersen3f980402001-04-04 17:31:15 +0000602 save_argc = argc;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200603 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{
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +0200651 unsigned c, r;
652 int err = get_terminal_width_height(STDIN_FILENO, &c, &r);
653 columns = c; rows = r;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700654 if (rows > MAX_SCR_ROWS)
655 rows = MAX_SCR_ROWS;
656 if (columns > MAX_SCR_COLS)
657 columns = MAX_SCR_COLS;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200658 return err;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700659}
660#else
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200661# define query_screen_dimensions() (0)
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700662#endif
663
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000664static void edit_file(char *fn)
Eric Andersen3f980402001-04-04 17:31:15 +0000665{
Denis Vlasenkob1759462008-06-20 20:20:54 +0000666#if ENABLE_FEATURE_VI_YANKMARK
667#define cur_line edit_file__cur_line
668#endif
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000669 int c;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000670#if ENABLE_FEATURE_VI_USE_SIGNALS
Eric Andersen3f980402001-04-04 17:31:15 +0000671 int sig;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000672#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000673
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000674 editing = 1; // 0 = exit, 1 = one file, 2 = multiple files
Eric Andersen3f980402001-04-04 17:31:15 +0000675 rawmode();
676 rows = 24;
677 columns = 80;
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200678 IF_FEATURE_VI_ASK_TERMINAL(G.get_rowcol_error =) query_screen_dimensions();
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700679#if ENABLE_FEATURE_VI_ASK_TERMINAL
680 if (G.get_rowcol_error /* TODO? && no input on stdin */) {
681 uint64_t k;
682 write1("\033[999;999H" "\033[6n");
683 fflush_all();
684 k = read_key(STDIN_FILENO, readbuffer, /*timeout_ms:*/ 100);
685 if ((int32_t)k == KEYCODE_CURSOR_POS) {
686 uint32_t rc = (k >> 32);
687 columns = (rc & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200688 if (columns > MAX_SCR_COLS)
689 columns = MAX_SCR_COLS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700690 rows = ((rc >> 16) & 0x7fff);
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +0200691 if (rows > MAX_SCR_ROWS)
692 rows = MAX_SCR_ROWS;
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700693 }
Denys Vlasenkoc175c462010-04-18 22:09:30 -0700694 }
695#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000696 new_screen(rows, columns); // get memory for virtual screen
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000697 init_text_buffer(fn);
Eric Andersen3f980402001-04-04 17:31:15 +0000698
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000699#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000700 YDreg = 26; // default Yank/Delete reg
701 Ureg = 27; // hold orig line for "U" cmd
Eric Andersen3f980402001-04-04 17:31:15 +0000702 mark[26] = mark[27] = text; // init "previous context"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000703#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000704
Eric Andersen3f980402001-04-04 17:31:15 +0000705 last_forward_char = last_input_char = '\0';
706 crow = 0;
707 ccol = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000708
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000709#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700710 signal(SIGINT, catch_sig);
Eric Andersen3f980402001-04-04 17:31:15 +0000711 signal(SIGWINCH, winch_sig);
712 signal(SIGTSTP, suspend_sig);
Paul Fox2724fa92008-03-17 15:28:07 +0000713 sig = sigsetjmp(restart, 1);
Eric Andersen3f980402001-04-04 17:31:15 +0000714 if (sig != 0) {
Eric Andersen1c0d3112001-04-16 15:46:44 +0000715 screenbegin = dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000716 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000717#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000718
Eric Andersen3f980402001-04-04 17:31:15 +0000719 cmd_mode = 0; // 0=command 1=insert 2='R'eplace
720 cmdcnt = 0;
721 tabstop = 8;
722 offset = 0; // no horizontal offset
723 c = '\0';
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000724#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmanna170e1c2002-11-28 11:27:31 +0000725 free(ioq_start);
Paul Foxc51fc7b2008-03-06 01:34:23 +0000726 ioq = ioq_start = NULL;
727 lmc_len = 0;
Eric Andersen3f980402001-04-04 17:31:15 +0000728 adding2q = 0;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000729#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000730
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000731#if ENABLE_FEATURE_VI_COLON
Denis Vlasenkof9234132007-03-21 00:03:42 +0000732 {
733 char *p, *q;
734 int n = 0;
735
Denys Vlasenko2bb651a2010-04-16 20:55:52 -0700736 while ((p = initial_cmds[n]) != NULL) {
Denis Vlasenkof9234132007-03-21 00:03:42 +0000737 do {
738 q = p;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000739 p = strchr(q, '\n');
Denis Vlasenkof9234132007-03-21 00:03:42 +0000740 if (p)
Denis Vlasenko51742f42007-04-12 00:32:05 +0000741 while (*p == '\n')
Denis Vlasenkof9234132007-03-21 00:03:42 +0000742 *p++ = '\0';
743 if (*q)
744 colon(q);
745 } while (p);
746 free(initial_cmds[n]);
747 initial_cmds[n] = NULL;
748 n++;
749 }
750 }
Denis Vlasenko58875ae2007-03-22 22:22:10 +0000751#endif
Paul Fox35e9c5d2008-03-06 16:26:12 +0000752 redraw(FALSE); // dont force every col re-draw
Eric Andersen3f980402001-04-04 17:31:15 +0000753 //------This is the main Vi cmd handling loop -----------------------
754 while (editing > 0) {
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000755#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000756 if (crashme > 0) {
757 if ((end - text) > 1) {
758 crash_dummy(); // generate a random command
759 } else {
760 crashme = 0;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +0000761 string_insert(text, "\n\n##### Ran out of text to work on. #####\n\n"); // insert the string
762 dot = text;
Eric Andersen3f980402001-04-04 17:31:15 +0000763 refresh(FALSE);
764 }
765 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000766#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000767 last_input_char = c = get_one_char(); // get a cmd from user
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000768#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +0000769 // save a copy of the current line- for the 'U" command
770 if (begin_line(dot) != cur_line) {
771 cur_line = begin_line(dot);
772 text_yank(begin_line(dot), end_line(dot), Ureg);
773 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000774#endif
775#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +0000776 // These are commands that change text[].
777 // Remember the input for the "." command
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000778 if (!adding2q && ioq_start == NULL
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +0000779 && cmd_mode == 0 // command mode
780 && c > '\0' // exclude NUL and non-ASCII chars
781 && c < 0x7f // (Unicode and such)
782 && strchr(modifying_cmds, c)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000783 ) {
Eric Andersen3f980402001-04-04 17:31:15 +0000784 start_new_cmd_q(c);
785 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000786#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000787 do_cmd(c); // execute the user command
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000788
Eric Andersen3f980402001-04-04 17:31:15 +0000789 // poll to see if there is input already waiting. if we are
790 // not able to display output fast enough to keep up, skip
791 // the display update until we catch up with input.
Denys Vlasenko020f4062009-05-17 16:44:54 +0200792 if (!readbuffer[0] && mysleep(0) == 0) {
Denis Vlasenko8ef801b2008-10-16 09:46:07 +0000793 // no input pending - so update output
Eric Andersen3f980402001-04-04 17:31:15 +0000794 refresh(FALSE);
795 show_status_line();
796 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000797#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen3f980402001-04-04 17:31:15 +0000798 if (crashme > 0)
799 crash_test(); // test editor variables
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000800#endif
Eric Andersen3f980402001-04-04 17:31:15 +0000801 }
802 //-------------------------------------------------------------------
803
Denis Vlasenko267e16c2008-10-14 10:34:41 +0000804 go_bottom_and_clear_to_eol();
Eric Andersen3f980402001-04-04 17:31:15 +0000805 cookmode();
Denis Vlasenkob1759462008-06-20 20:20:54 +0000806#undef cur_line
Eric Andersen3f980402001-04-04 17:31:15 +0000807}
808
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000809//----- The Colon commands -------------------------------------
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000810#if ENABLE_FEATURE_VI_COLON
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000811static char *get_one_address(char *p, int *addr) // get colon addr, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000812{
813 int st;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000814 char *q;
Denis Vlasenko5e34ff22009-04-21 11:09:40 +0000815 IF_FEATURE_VI_YANKMARK(char c;)
816 IF_FEATURE_VI_SEARCH(char *pat;)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000817
818 *addr = -1; // assume no addr
819 if (*p == '.') { // the current line
820 p++;
821 q = begin_line(dot);
822 *addr = count_lines(text, q);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000823 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000824#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000825 else if (*p == '\'') { // is this a mark addr
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000826 p++;
827 c = tolower(*p);
828 p++;
829 if (c >= 'a' && c <= 'z') {
830 // we have a mark
831 c = c - 'a';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000832 q = mark[(unsigned char) c];
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000833 if (q != NULL) { // is mark valid
Denis Vlasenko00d84172008-11-24 07:34:42 +0000834 *addr = count_lines(text, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000835 }
836 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000837 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000838#endif
839#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000840 else if (*p == '/') { // a search pattern
841 q = strchrnul(++p, '/');
842 pat = xstrndup(p, q - p); // save copy of pattern
843 p = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000844 if (*p == '/')
845 p++;
846 q = char_search(dot, pat, FORWARD, FULL);
847 if (q != NULL) {
848 *addr = count_lines(text, q);
849 }
850 free(pat);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000851 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000852#endif
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000853 else if (*p == '$') { // the last line in file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000854 p++;
855 q = begin_line(end - 1);
856 *addr = count_lines(text, q);
857 } else if (isdigit(*p)) { // specific line number
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000858 sscanf(p, "%d%n", addr, &st);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000859 p += st;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000860 } else {
Denys Vlasenkob22bbff2009-07-04 16:50:43 +0200861 // unrecognized address - assume -1
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000862 *addr = -1;
863 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000864 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000865}
866
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000867static char *get_address(char *p, int *b, int *e) // get two colon addrs, if present
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000868{
869 //----- get the address' i.e., 1,3 'a,'b -----
870 // get FIRST addr, if present
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000871 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000872 p++; // skip over leading spaces
873 if (*p == '%') { // alias for 1,$
874 p++;
875 *b = 1;
876 *e = count_lines(text, end-1);
877 goto ga0;
878 }
879 p = get_one_address(p, b);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000880 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000881 p++;
Eric Andersenaff114c2004-04-14 17:51:38 +0000882 if (*p == ',') { // is there a address separator
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000883 p++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000884 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000885 p++;
886 // get SECOND addr, if present
887 p = get_one_address(p, e);
888 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000889 ga0:
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000890 while (isblank(*p))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000891 p++; // skip over trailing spaces
Denis Vlasenko079f8af2006-11-27 16:49:31 +0000892 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000893}
894
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +0000895#if ENABLE_FEATURE_VI_SET && ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000896static void setops(const char *args, const char *opname, int flg_no,
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000897 const char *short_opname, int opt)
898{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000899 const char *a = args + flg_no;
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000900 int l = strlen(opname) - 1; /* opname have + ' ' */
901
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200902 // maybe strncmp? we had tons of erroneous strncasecmp's...
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000903 if (strncasecmp(a, opname, l) == 0
904 || strncasecmp(a, short_opname, 2) == 0
905 ) {
906 if (flg_no)
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000907 vi_setops &= ~opt;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000908 else
Glenn L McGrath09adaca2002-12-02 21:18:10 +0000909 vi_setops |= opt;
910 }
911}
912#endif
913
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000914// buf must be no longer than MAX_INPUT_LEN!
915static void colon(char *buf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000916{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000917 char c, *orig_buf, *buf1, *q, *r;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000918 char *fn, cmd[MAX_INPUT_LEN], args[MAX_INPUT_LEN];
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000919 int i, l, li, ch, b, e;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000920 int useforce, forced = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000921
922 // :3154 // if (-e line 3154) goto it else stay put
923 // :4,33w! foo // write a portion of buffer to file "foo"
924 // :w // write all of buffer to current file
925 // :q // quit
926 // :q! // quit- dont care about modified file
927 // :'a,'z!sort -u // filter block through sort
928 // :'f // goto mark "f"
929 // :'fl // list literal the mark "f" line
930 // :.r bar // read file "bar" into buffer before dot
931 // :/123/,/abc/d // delete lines from "123" line to "abc" line
932 // :/xyz/ // goto the "xyz" line
933 // :s/find/replace/ // substitute pattern "find" with "replace"
934 // :!<cmd> // run <cmd> then return
935 //
Eric Andersen165e8cb2004-07-20 06:44:46 +0000936
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000937 if (!buf[0])
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +0200938 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000939 if (*buf == ':')
940 buf++; // move past the ':'
941
Bernhard Reutner-Fischerd591a362006-08-20 17:35:13 +0000942 li = ch = i = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000943 b = e = -1;
944 q = text; // assume 1,$ for the range
945 r = end - 1;
946 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000947 fn = current_filename;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000948
949 // look for optional address(es) :. :1 :1,9 :'q,'a :%
950 buf = get_address(buf, &b, &e);
951
952 // remember orig command line
953 orig_buf = buf;
954
955 // get the COMMAND into cmd[]
956 buf1 = cmd;
957 while (*buf != '\0') {
958 if (isspace(*buf))
959 break;
960 *buf1++ = *buf++;
961 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000962 *buf1 = '\0';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000963 // get any ARGuments
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000964 while (isblank(*buf))
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000965 buf++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000966 strcpy(args, buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +0000967 useforce = FALSE;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000968 buf1 = last_char_is(cmd, '!');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000969 if (buf1) {
970 useforce = TRUE;
971 *buf1 = '\0'; // get rid of !
972 }
973 if (b >= 0) {
974 // if there is only one addr, then the addr
975 // is the line number of the single line the
976 // user wants. So, reset the end
977 // pointer to point at end of the "b" line
978 q = find_line(b); // what line is #b
979 r = end_line(q);
980 li = 1;
981 }
982 if (e >= 0) {
983 // we were given two addrs. change the
984 // end pointer to the addr given by user.
985 r = find_line(e); // what line is #e
986 r = end_line(r);
987 li = e - b + 1;
988 }
989 // ------------ now look for the command ------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +0000990 i = strlen(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +0000991 if (i == 0) { // :123CR goto line #123
992 if (b >= 0) {
993 dot = find_line(b); // what line is #b
994 dot_skip_over_ws();
995 }
Denis Vlasenko249fabf2006-12-19 00:29:22 +0000996 }
997#if ENABLE_FEATURE_ALLOW_EXEC
Denys Vlasenko6548edd2009-06-15 12:44:11 +0200998 else if (cmd[0] == '!') { // run a cmd
Denis Vlasenkoeaabf062007-07-17 23:14:07 +0000999 int retcode;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001000 // :!ls run the <cmd>
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001001 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001002 cookmode();
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001003 retcode = system(orig_buf + 1); // run the cmd
1004 if (retcode)
1005 printf("\nshell returned %i\n\n", retcode);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001006 rawmode();
1007 Hit_Return(); // let user see results
Denis Vlasenko249fabf2006-12-19 00:29:22 +00001008 }
1009#endif
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001010 else if (cmd[0] == '=' && !cmd[1]) { // where is the address
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001011 if (b < 0) { // no addr given- use defaults
1012 b = e = count_lines(text, dot);
1013 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001014 status_line("%d", b);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001015 } else if (strncmp(cmd, "delete", i) == 0) { // delete lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001016 if (b < 0) { // no addr given- use defaults
1017 q = begin_line(dot); // assume .,. for the range
1018 r = end_line(dot);
1019 }
1020 dot = yank_delete(q, r, 1, YANKDEL); // save, then delete lines
1021 dot_skip_over_ws();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001022 } else if (strncmp(cmd, "edit", i) == 0) { // Edit a file
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001023 // don't edit, if the current file has been modified
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001024 if (file_modified && !useforce) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001025 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001026 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001027 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001028 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001029 // the user supplied a file name
Denis Vlasenkoe8a07882007-06-10 15:08:44 +00001030 fn = args;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001031 } else if (current_filename && current_filename[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001032 // no user supplied name- use the current filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001033 // fn = current_filename; was set by default
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001034 } else {
1035 // no user file name, no current name- punt
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001036 status_line_bold("No current filename");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001037 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001038 }
1039
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001040 if (init_text_buffer(fn) < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001041 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001042
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001043#if ENABLE_FEATURE_VI_YANKMARK
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001044 if (Ureg >= 0 && Ureg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001045 free(reg[Ureg]); // free orig line reg- for 'U'
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001046 reg[Ureg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001047 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001048 if (YDreg >= 0 && YDreg < 28) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001049 free(reg[YDreg]); // free default yank/delete register
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001050 reg[YDreg] = NULL;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001051 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001052#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001053 // how many lines in text[]?
1054 li = count_lines(text, end - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001055 status_line("\"%s\"%s"
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001056 IF_FEATURE_VI_READONLY("%s")
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001057 " %dL, %dC", current_filename,
1058 (file_size(fn) < 0 ? " [New file]" : ""),
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001059 IF_FEATURE_VI_READONLY(
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001060 ((readonly_mode) ? " [Readonly]" : ""),
1061 )
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001062 li, ch);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001063 } else if (strncmp(cmd, "file", i) == 0) { // what File is this
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001064 if (b != -1 || e != -1) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01001065 status_line_bold("No address allowed on this command");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001066 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001067 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001068 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001069 // user wants a new filename
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001070 free(current_filename);
1071 current_filename = xstrdup(args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001072 } else {
1073 // user wants file status info
Paul Fox8552aec2005-09-16 12:20:05 +00001074 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001075 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001076 } else if (strncmp(cmd, "features", i) == 0) { // what features are available
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001077 // print out values of all features
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001078 go_bottom_and_clear_to_eol();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001079 cookmode();
1080 show_help();
1081 rawmode();
1082 Hit_Return();
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001083 } else if (strncmp(cmd, "list", i) == 0) { // literal print line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001084 if (b < 0) { // no addr given- use defaults
1085 q = begin_line(dot); // assume .,. for the range
1086 r = end_line(dot);
1087 }
Denis Vlasenko267e16c2008-10-14 10:34:41 +00001088 go_bottom_and_clear_to_eol();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001089 puts("\r");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001090 for (; q <= r; q++) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001091 int c_is_no_print;
1092
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001093 c = *q;
Denis Vlasenko2a51af22007-03-21 22:31:24 +00001094 c_is_no_print = (c & 0x80) && !Isprint(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001095 if (c_is_no_print) {
1096 c = '.';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001097 standout_start();
Denis Vlasenkod3c042f2007-12-30 01:59:53 +00001098 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001099 if (c == '\n') {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001100 write1("$\r");
1101 } else if (c < ' ' || c == 127) {
Denis Vlasenko4daad902007-09-27 10:20:47 +00001102 bb_putchar('^');
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001103 if (c == 127)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001104 c = '?';
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001105 else
1106 c += '@';
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001107 }
Denis Vlasenko4daad902007-09-27 10:20:47 +00001108 bb_putchar(c);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001109 if (c_is_no_print)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001110 standout_end();
1111 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001112 Hit_Return();
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001113 } else if (strncmp(cmd, "quit", i) == 0 // quit
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001114 || strncmp(cmd, "next", i) == 0 // edit next file
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001115 || strncmp(cmd, "prev", i) == 0 // edit previous file
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001116 ) {
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001117 int n;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001118 if (useforce) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001119 if (*cmd == 'q') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001120 // force end of argv list
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001121 optind = save_argc;
1122 }
1123 editing = 0;
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001124 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001125 }
1126 // don't exit if the file been modified
1127 if (file_modified) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001128 status_line_bold("No write since last change (:%s! overrides)", cmd);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001129 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001130 }
1131 // are there other file to edit
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001132 n = save_argc - optind - 1;
1133 if (*cmd == 'q' && n > 0) {
1134 status_line_bold("%d more file(s) to edit", n);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001135 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001136 }
Denys Vlasenko04cecd52010-04-16 22:13:55 -07001137 if (*cmd == 'n' && n <= 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001138 status_line_bold("No more files to edit");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001139 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001140 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001141 if (*cmd == 'p') {
1142 // are there previous files to edit
1143 if (optind < 1) {
1144 status_line_bold("No previous files to edit");
1145 goto ret;
1146 }
1147 optind -= 2;
1148 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001149 editing = 0;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001150 } else if (strncmp(cmd, "read", i) == 0) { // read file into text[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001151 fn = args;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001152 if (!fn[0]) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001153 status_line_bold("No filename given");
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001154 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001155 }
1156 if (b < 0) { // no addr given- use defaults
1157 q = begin_line(dot); // assume "dot"
1158 }
1159 // read after current line- unless user said ":0r foo"
1160 if (b != 0)
1161 q = next_line(q);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001162 { // dance around potentially-reallocated text[]
1163 uintptr_t ofs = q - text;
1164 ch = file_insert(fn, q, 0);
1165 q = text + ofs;
1166 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001167 if (ch < 0)
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001168 goto ret; // nothing was inserted
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001169 // how many lines in text[]?
1170 li = count_lines(q, q + ch - 1);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001171 status_line("\"%s\""
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001172 IF_FEATURE_VI_READONLY("%s")
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001173 " %dL, %dC", fn,
Denis Vlasenko5e34ff22009-04-21 11:09:40 +00001174 IF_FEATURE_VI_READONLY((readonly_mode ? " [Readonly]" : ""),)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001175 li, ch);
1176 if (ch > 0) {
1177 // if the insert is before "dot" then we need to update
1178 if (q <= dot)
1179 dot += ch;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001180 /*file_modified++; - done by file_insert */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001181 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001182 } else if (strncmp(cmd, "rewind", i) == 0) { // rewind cmd line args
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001183 if (file_modified && !useforce) {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001184 status_line_bold("No write since last change (:%s! overrides)", cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001185 } else {
1186 // reset the filenames to edit
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001187 optind = -1; /* start from 0th file */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001188 editing = 0;
1189 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001190#if ENABLE_FEATURE_VI_SET
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001191 } else if (strncmp(cmd, "set", i) == 0) { // set or clear features
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001192#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkof9234132007-03-21 00:03:42 +00001193 char *argp;
Denis Vlasenko58875ae2007-03-22 22:22:10 +00001194#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001195 i = 0; // offset into args
Denis Vlasenkof9234132007-03-21 00:03:42 +00001196 // only blank is regarded as args delmiter. What about tab '\t' ?
1197 if (!args[0] || strcasecmp(args, "all") == 0) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001198 // print out values of all options
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001199#if ENABLE_FEATURE_VI_SETOPTS
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001200 status_line_bold(
1201 "%sautoindent "
1202 "%sflash "
1203 "%signorecase "
1204 "%sshowmatch "
1205 "tabstop=%u",
1206 autoindent ? "" : "no",
1207 err_method ? "" : "no",
1208 ignorecase ? "" : "no",
1209 showmatch ? "" : "no",
1210 tabstop
1211 );
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001212#endif
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001213 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001214 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001215#if ENABLE_FEATURE_VI_SETOPTS
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001216 argp = args;
Denis Vlasenkoba2fb712007-04-01 09:39:03 +00001217 while (*argp) {
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001218 if (strncmp(argp, "no", 2) == 0)
Denis Vlasenkof9234132007-03-21 00:03:42 +00001219 i = 2; // ":set noautoindent"
1220 setops(argp, "autoindent ", i, "ai", VI_AUTOINDENT);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001221 setops(argp, "flash " , i, "fl", VI_ERR_METHOD);
Denis Vlasenkof9234132007-03-21 00:03:42 +00001222 setops(argp, "ignorecase ", i, "ic", VI_IGNORECASE);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001223 setops(argp, "showmatch " , i, "sm", VI_SHOWMATCH );
1224 if (strncmp(argp + i, "tabstop=", 8) == 0) {
1225 int t = 0;
1226 sscanf(argp + i+8, "%u", &t);
1227 if (t > 0 && t <= MAX_TABSTOP)
1228 tabstop = t;
Denis Vlasenkof9234132007-03-21 00:03:42 +00001229 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001230 argp = skip_non_whitespace(argp);
1231 argp = skip_whitespace(argp);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001232 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001233#endif /* FEATURE_VI_SETOPTS */
1234#endif /* FEATURE_VI_SET */
1235#if ENABLE_FEATURE_VI_SEARCH
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001236 } else if (cmd[0] == 's') { // substitute a pattern with a replacement pattern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001237 char *F, *R, *flags;
1238 size_t len_F, len_R;
1239 int gflag; // global replace flag
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001240
1241 // F points to the "find" pattern
1242 // R points to the "replace" pattern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001243 // replace the cmd line delimiters "/" with NULs
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001244 c = orig_buf[1]; // what is the delimiter
1245 F = orig_buf + 2; // start of "find"
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001246 R = strchr(F, c); // middle delimiter
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001247 if (!R)
1248 goto colon_s_fail;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001249 len_F = R - F;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001250 *R++ = '\0'; // terminate "find"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001251 flags = strchr(R, c);
1252 if (!flags)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001253 goto colon_s_fail;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001254 len_R = flags - R;
1255 *flags++ = '\0'; // terminate "replace"
1256 gflag = *flags;
1257
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001258 q = begin_line(q);
1259 if (b < 0) { // maybe :s/foo/bar/
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001260 q = begin_line(dot); // start with cur line
1261 b = count_lines(text, q); // cur line number
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001262 }
1263 if (e < 0)
1264 e = b; // maybe :.s/foo/bar/
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001265
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001266 for (i = b; i <= e; i++) { // so, :20,23 s \0 find \0 replace \0
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001267 char *ls = q; // orig line start
1268 char *found;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001269 vc4:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001270 found = char_search(q, F, FORWARD, LIMITED); // search cur line only for "find"
1271 if (found) {
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001272 uintptr_t bias;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001273 // we found the "find" pattern - delete it
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001274 text_hole_delete(found, found + len_F - 1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001275 // inset the "replace" patern
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001276 bias = string_insert(found, R); // insert the string
1277 found += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001278 ls += bias;
1279 /*q += bias; - recalculated anyway */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001280 // check for "global" :s/foo/bar/g
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02001281 if (gflag == 'g') {
1282 if ((found + len_R) < end_line(ls)) {
1283 q = found + len_R;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001284 goto vc4; // don't let q move past cur line
1285 }
1286 }
1287 }
1288 q = next_line(ls);
1289 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001290#endif /* FEATURE_VI_SEARCH */
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001291 } else if (strncmp(cmd, "version", i) == 0) { // show software version
Denis Vlasenko33875382008-06-21 20:31:50 +00001292 status_line(BB_VER " " BB_BT);
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001293 } else if (strncmp(cmd, "write", i) == 0 // write text to file
1294 || strncmp(cmd, "wq", i) == 0
1295 || strncmp(cmd, "wn", i) == 0
1296 || (cmd[0] == 'x' && !cmd[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001297 ) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001298 // is there a file name to write to?
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001299 if (args[0]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001300 fn = args;
1301 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001302#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001303 if (readonly_mode && !useforce) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001304 status_line_bold("\"%s\" File is read only", fn);
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001305 goto ret;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001306 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001307#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001308 // how many lines in text[]?
1309 li = count_lines(q, r);
1310 ch = r - q + 1;
1311 // see if file exists- if not, its just a new file request
1312 if (useforce) {
1313 // if "fn" is not write-able, chmod u+w
1314 // sprintf(syscmd, "chmod u+w %s", fn);
1315 // system(syscmd);
1316 forced = TRUE;
1317 }
1318 l = file_write(fn, q, r);
1319 if (useforce && forced) {
1320 // chmod u-w
1321 // sprintf(syscmd, "chmod u-w %s", fn);
1322 // system(syscmd);
1323 forced = FALSE;
1324 }
Paul Fox61e45db2005-10-09 14:43:22 +00001325 if (l < 0) {
1326 if (l == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001327 status_line_bold("\"%s\" %s", fn, strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00001328 } else {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001329 status_line("\"%s\" %dL, %dC", fn, li, l);
Paul Fox61e45db2005-10-09 14:43:22 +00001330 if (q == text && r == end - 1 && l == ch) {
1331 file_modified = 0;
1332 last_file_modified = -1;
1333 }
Denys Vlasenko6b9f1632010-01-28 02:24:24 +01001334 if ((cmd[0] == 'x' || cmd[1] == 'q' || cmd[1] == 'n'
1335 || cmd[0] == 'X' || cmd[1] == 'Q' || cmd[1] == 'N'
1336 )
1337 && l == ch
1338 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00001339 editing = 0;
1340 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001341 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001342#if ENABLE_FEATURE_VI_YANKMARK
Denys Vlasenko6548edd2009-06-15 12:44:11 +02001343 } else if (strncmp(cmd, "yank", i) == 0) { // yank lines
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001344 if (b < 0) { // no addr given- use defaults
1345 q = begin_line(dot); // assume .,. for the range
1346 r = end_line(dot);
1347 }
1348 text_yank(q, r, YDreg);
1349 li = count_lines(q, r);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001350 status_line("Yank %d lines (%d chars) into [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001351 li, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001352#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001353 } else {
1354 // cmd unknown
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001355 not_implemented(cmd);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001356 }
Denys Vlasenko9f82d0b2010-05-19 01:54:37 +02001357 ret:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001358 dot = bound_dot(dot); // make sure "dot" is valid
1359 return;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001360#if ENABLE_FEATURE_VI_SEARCH
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001361 colon_s_fail:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001362 status_line(":s expression missing delimiters");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001363#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001364}
Paul Fox61e45db2005-10-09 14:43:22 +00001365
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001366#endif /* FEATURE_VI_COLON */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001367
1368static void Hit_Return(void)
1369{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00001370 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001371
Denis Vlasenkof882f082007-12-23 02:36:51 +00001372 standout_start();
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001373 write1("[Hit return to continue]");
Denis Vlasenkof882f082007-12-23 02:36:51 +00001374 standout_end();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001375 while ((c = get_one_char()) != '\n' && c != '\r')
1376 continue;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001377 redraw(TRUE); // force redraw all
1378}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001379
Denis Vlasenko91afdf82007-07-17 23:22:49 +00001380static int next_tabstop(int col)
1381{
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001382 return col + ((tabstop - 1) - (col % tabstop));
1383}
1384
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001385//----- Synchronize the cursor to Dot --------------------------
Denys Vlasenkoadf922e2009-10-08 14:35:37 +02001386static NOINLINE void sync_cursor(char *d, int *row, int *col)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001387{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001388 char *beg_cur; // begin and end of "d" line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001389 char *tp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001390 int cnt, ro, co;
1391
1392 beg_cur = begin_line(d); // first char of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001393
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001394 if (beg_cur < screenbegin) {
Denis Vlasenkof882f082007-12-23 02:36:51 +00001395 // "d" is before top line on screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001396 // how many lines do we have to move
1397 cnt = count_lines(beg_cur, screenbegin);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001398 sc1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001399 screenbegin = beg_cur;
1400 if (cnt > (rows - 1) / 2) {
1401 // we moved too many lines. put "dot" in middle of screen
1402 for (cnt = 0; cnt < (rows - 1) / 2; cnt++) {
1403 screenbegin = prev_line(screenbegin);
1404 }
1405 }
Denis Vlasenkof882f082007-12-23 02:36:51 +00001406 } else {
1407 char *end_scr; // begin and end of screen
1408 end_scr = end_screen(); // last char of screen
1409 if (beg_cur > end_scr) {
1410 // "d" is after bottom line on screen
1411 // how many lines do we have to move
1412 cnt = count_lines(end_scr, beg_cur);
1413 if (cnt > (rows - 1) / 2)
1414 goto sc1; // too many lines
1415 for (ro = 0; ro < cnt - 1; ro++) {
1416 // move screen begin the same amount
1417 screenbegin = next_line(screenbegin);
1418 // now, move the end of screen
1419 end_scr = next_line(end_scr);
1420 end_scr = end_line(end_scr);
1421 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001422 }
1423 }
1424 // "d" is on screen- find out which row
1425 tp = screenbegin;
1426 for (ro = 0; ro < rows - 1; ro++) { // drive "ro" to correct row
1427 if (tp == beg_cur)
1428 break;
1429 tp = next_line(tp);
1430 }
1431
1432 // find out what col "d" is on
1433 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001434 while (tp < d) { // drive "co" to correct column
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001435 if (*tp == '\n') //vda || *tp == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001436 break;
1437 if (*tp == '\t') {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001438 // handle tabs like real vi
1439 if (d == tp && cmd_mode) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001440 break;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001441 }
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001442 co = next_tabstop(co);
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001443 } else if ((unsigned char)*tp < ' ' || *tp == 0x7f) {
1444 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001445 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001446 co++;
1447 tp++;
1448 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001449
1450 // "co" is the column where "dot" is.
1451 // The screen has "columns" columns.
1452 // The currently displayed columns are 0+offset -- columns+ofset
1453 // |-------------------------------------------------------------|
1454 // ^ ^ ^
1455 // offset | |------- columns ----------------|
1456 //
1457 // If "co" is already in this range then we do not have to adjust offset
1458 // but, we do have to subtract the "offset" bias from "co".
1459 // If "co" is outside this range then we have to change "offset".
1460 // If the first char of a line is a tab the cursor will try to stay
1461 // in column 7, but we have to set offset to 0.
1462
1463 if (co < 0 + offset) {
1464 offset = co;
1465 }
1466 if (co >= columns + offset) {
1467 offset = co - columns + 1;
1468 }
1469 // if the first char of the line is a tab, and "dot" is sitting on it
1470 // force offset to 0.
1471 if (d == beg_cur && *d == '\t') {
1472 offset = 0;
1473 }
1474 co -= offset;
1475
1476 *row = ro;
1477 *col = co;
1478}
1479
1480//----- Text Movement Routines ---------------------------------
Denis Vlasenkof882f082007-12-23 02:36:51 +00001481static char *begin_line(char *p) // return pointer to first char cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001482{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001483 if (p > text) {
1484 p = memrchr(text, '\n', p - text);
1485 if (!p)
1486 return text;
1487 return p + 1;
1488 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001489 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001490}
1491
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001492static char *end_line(char *p) // return pointer to NL of cur line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001493{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001494 if (p < end - 1) {
1495 p = memchr(p, '\n', end - p - 1);
1496 if (!p)
1497 return end - 1;
1498 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001499 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001500}
1501
Denis Vlasenkof882f082007-12-23 02:36:51 +00001502static char *dollar_line(char *p) // return pointer to just before NL line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001503{
Denis Vlasenkof882f082007-12-23 02:36:51 +00001504 p = end_line(p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001505 // Try to stay off of the Newline
1506 if (*p == '\n' && (p - begin_line(p)) > 0)
1507 p--;
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001508 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001509}
1510
Denis Vlasenkof882f082007-12-23 02:36:51 +00001511static char *prev_line(char *p) // return pointer first char prev line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001512{
1513 p = begin_line(p); // goto begining of cur line
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001514 if (p > text && p[-1] == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001515 p--; // step to prev line
1516 p = begin_line(p); // goto begining of prev line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001517 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001518}
1519
Denis Vlasenkof882f082007-12-23 02:36:51 +00001520static char *next_line(char *p) // return pointer first char next line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001521{
1522 p = end_line(p);
Denis Vlasenkoe3eae0d2008-06-22 16:38:53 +00001523 if (p < end - 1 && *p == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001524 p++; // step to next line
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001525 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001526}
1527
1528//----- Text Information Routines ------------------------------
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001529static char *end_screen(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001530{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001531 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001532 int cnt;
1533
1534 // find new bottom line
1535 q = screenbegin;
1536 for (cnt = 0; cnt < rows - 2; cnt++)
1537 q = next_line(q);
1538 q = end_line(q);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001539 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001540}
1541
Denis Vlasenkof882f082007-12-23 02:36:51 +00001542// count line from start to stop
1543static int count_lines(char *start, char *stop)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001544{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001545 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001546 int cnt;
1547
Denis Vlasenkof882f082007-12-23 02:36:51 +00001548 if (stop < start) { // start and stop are backwards- reverse them
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001549 q = start;
1550 start = stop;
1551 stop = q;
1552 }
1553 cnt = 0;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001554 stop = end_line(stop);
1555 while (start <= stop && start <= end - 1) {
1556 start = end_line(start);
1557 if (*start == '\n')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001558 cnt++;
Denis Vlasenkof882f082007-12-23 02:36:51 +00001559 start++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001560 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001561 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001562}
1563
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001564static char *find_line(int li) // find begining of line #li
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001565{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001566 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001567
1568 for (q = text; li > 1; li--) {
1569 q = next_line(q);
1570 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001571 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001572}
1573
1574//----- Dot Movement Routines ----------------------------------
1575static void dot_left(void)
1576{
1577 if (dot > text && dot[-1] != '\n')
1578 dot--;
1579}
1580
1581static void dot_right(void)
1582{
1583 if (dot < end - 1 && *dot != '\n')
1584 dot++;
1585}
1586
1587static void dot_begin(void)
1588{
1589 dot = begin_line(dot); // return pointer to first char cur line
1590}
1591
1592static void dot_end(void)
1593{
1594 dot = end_line(dot); // return pointer to last char cur line
1595}
1596
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001597static char *move_to_col(char *p, int l)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001598{
1599 int co;
1600
1601 p = begin_line(p);
1602 co = 0;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001603 while (co < l && p < end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001604 if (*p == '\n') //vda || *p == '\0')
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001605 break;
1606 if (*p == '\t') {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00001607 co = next_tabstop(co);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00001608 } else if (*p < ' ' || *p == 127) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001609 co++; // display as ^X, use 2 columns
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001610 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00001611 co++;
1612 p++;
1613 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001614 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001615}
1616
1617static void dot_next(void)
1618{
1619 dot = next_line(dot);
1620}
1621
1622static void dot_prev(void)
1623{
1624 dot = prev_line(dot);
1625}
1626
1627static void dot_scroll(int cnt, int dir)
1628{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001629 char *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001630
1631 for (; cnt > 0; cnt--) {
1632 if (dir < 0) {
1633 // scroll Backwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001634 // ctrl-Y scroll up one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001635 screenbegin = prev_line(screenbegin);
1636 } else {
1637 // scroll Forwards
Denis Vlasenkof882f082007-12-23 02:36:51 +00001638 // ctrl-E scroll down one line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001639 screenbegin = next_line(screenbegin);
1640 }
1641 }
1642 // make sure "dot" stays on the screen so we dont scroll off
1643 if (dot < screenbegin)
1644 dot = screenbegin;
1645 q = end_screen(); // find new bottom line
1646 if (dot > q)
1647 dot = begin_line(q); // is dot is below bottom line?
1648 dot_skip_over_ws();
1649}
1650
1651static void dot_skip_over_ws(void)
1652{
1653 // skip WS
1654 while (isspace(*dot) && *dot != '\n' && dot < end - 1)
1655 dot++;
1656}
1657
1658static void dot_delete(void) // delete the char at 'dot'
1659{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001660 text_hole_delete(dot, dot);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001661}
1662
Denis Vlasenkof882f082007-12-23 02:36:51 +00001663static char *bound_dot(char *p) // make sure text[0] <= P < "end"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001664{
1665 if (p >= end && end > text) {
1666 p = end - 1;
1667 indicate_error('1');
1668 }
1669 if (p < text) {
1670 p = text;
1671 indicate_error('2');
1672 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001673 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001674}
1675
1676//----- Helper Utility Routines --------------------------------
1677
1678//----------------------------------------------------------------
1679//----- Char Routines --------------------------------------------
1680/* Chars that are part of a word-
1681 * 0123456789_ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz
1682 * Chars that are Not part of a word (stoppers)
1683 * !"#$%&'()*+,-./:;<=>?@[\]^`{|}~
1684 * Chars that are WhiteSpace
1685 * TAB NEWLINE VT FF RETURN SPACE
1686 * DO NOT COUNT NEWLINE AS WHITESPACE
1687 */
1688
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001689static char *new_screen(int ro, int co)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001690{
1691 int li;
1692
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00001693 free(screen);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001694 screensize = ro * co + 8;
Denis Vlasenkob95636c2006-12-19 23:36:04 +00001695 screen = xmalloc(screensize);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001696 // initialize the new screen. assume this will be a empty file.
1697 screen_erase();
Eric Andersenaff114c2004-04-14 17:51:38 +00001698 // non-existent text[] lines start with a tilde (~).
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001699 for (li = 1; li < ro - 1; li++) {
1700 screen[(li * co) + 0] = '~';
1701 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001702 return screen;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001703}
1704
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001705#if ENABLE_FEATURE_VI_SEARCH
Walter Harmsb9ba5802011-06-27 02:59:37 +02001706
1707# if ENABLE_FEATURE_VI_REGEX_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001708
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001709// search for pattern starting at p
Denis Vlasenko33875382008-06-21 20:31:50 +00001710static char *char_search(char *p, const char *pat, int dir, int range)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001711{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001712 char *q;
1713 struct re_pattern_buffer preg;
1714 int i;
Walter Harmsb9ba5802011-06-27 02:59:37 +02001715 int size;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001716
1717 re_syntax_options = RE_SYNTAX_POSIX_EXTENDED;
1718 preg.translate = 0;
1719 preg.fastmap = 0;
1720 preg.buffer = 0;
1721 preg.allocated = 0;
1722
1723 // assume a LIMITED forward search
1724 q = next_line(p);
1725 q = end_line(q);
1726 q = end - 1;
1727 if (dir == BACK) {
1728 q = prev_line(p);
1729 q = text;
1730 }
1731 // count the number of chars to search over, forward or backward
1732 size = q - p;
1733 if (size < 0)
1734 size = p - q;
1735 // RANGE could be negative if we are searching backwards
1736 range = q - p;
1737
Walter Harmsb9ba5802011-06-27 02:59:37 +02001738 q = (char *)re_compile_pattern(pat, strlen(pat), (struct re_pattern_buffer *)&preg);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001739 if (q != 0) {
1740 // The pattern was not compiled
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00001741 status_line_bold("bad search pattern: \"%s\": %s", pat, q);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001742 i = 0; // return p if pattern not compiled
1743 goto cs1;
1744 }
1745
1746 q = p;
1747 if (range < 0) {
1748 q = p - size;
1749 if (q < text)
1750 q = text;
1751 }
1752 // search for the compiled pattern, preg, in p[]
1753 // range < 0- search backward
1754 // range > 0- search forward
1755 // 0 < start < size
1756 // re_search() < 0 not found or error
1757 // re_search() > 0 index of found pattern
1758 // struct pattern char int int int struct reg
1759 // re_search (*pattern_buffer, *string, size, start, range, *regs)
1760 i = re_search(&preg, q, size, 0, range, 0);
1761 if (i == -1) {
1762 p = 0;
1763 i = 0; // return NULL if pattern not found
1764 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001765 cs1:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001766 if (dir == FORWARD) {
1767 p = p + i;
1768 } else {
1769 p = p - i;
1770 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001771 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001772}
Walter Harmsb9ba5802011-06-27 02:59:37 +02001773
1774# else
1775
1776# if ENABLE_FEATURE_VI_SETOPTS
1777static int mycmp(const char *s1, const char *s2, int len)
1778{
1779 if (ignorecase) {
1780 return strncasecmp(s1, s2, len);
1781 }
1782 return strncmp(s1, s2, len);
1783}
1784# else
1785# define mycmp strncmp
1786# endif
1787
1788static char *char_search(char *p, const char *pat, int dir, int range)
1789{
1790 char *start, *stop;
1791 int len;
1792
1793 len = strlen(pat);
1794 if (dir == FORWARD) {
1795 stop = end - 1; // assume range is p - end-1
1796 if (range == LIMITED)
1797 stop = next_line(p); // range is to next line
1798 for (start = p; start < stop; start++) {
1799 if (mycmp(start, pat, len) == 0) {
1800 return start;
1801 }
1802 }
1803 } else if (dir == BACK) {
1804 stop = text; // assume range is text - p
1805 if (range == LIMITED)
1806 stop = prev_line(p); // range is to prev line
1807 for (start = p - len; start >= stop; start--) {
1808 if (mycmp(start, pat, len) == 0) {
1809 return start;
1810 }
1811 }
1812 }
1813 // pattern not found
1814 return NULL;
1815}
1816
1817# endif
1818
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001819#endif /* FEATURE_VI_SEARCH */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001820
Denis Vlasenko33875382008-06-21 20:31:50 +00001821static char *char_insert(char *p, char c) // insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001822{
1823 if (c == 22) { // Is this an ctrl-V?
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001824 p += stupid_insert(p, '^'); // use ^ to indicate literal next
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001825 refresh(FALSE); // show the ^
1826 c = get_one_char();
1827 *p = c;
1828 p++;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001829 file_modified++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001830 } else if (c == 27) { // Is this an ESC?
1831 cmd_mode = 0;
1832 cmdcnt = 0;
1833 end_cmd_q(); // stop adding to q
Paul Fox8552aec2005-09-16 12:20:05 +00001834 last_status_cksum = 0; // force status update
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001835 if ((p[-1] != '\n') && (dot > text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001836 p--;
1837 }
Paul Foxd13b90b2005-07-18 22:17:25 +00001838 } else if (c == erase_char || c == 8 || c == 127) { // Is this a BS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001839 // 123456789
Denis Vlasenkodefc1ea2008-06-27 02:52:20 +00001840 if ((p[-1] != '\n') && (dot>text)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001841 p--;
1842 p = text_hole_delete(p, p); // shrink buffer 1 char
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001843 }
1844 } else {
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001845#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001846 // insert a char into text[]
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001847 char *sp; // "save p"
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001848#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001849
1850 if (c == 13)
1851 c = '\n'; // translate \r to \n
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001852#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001853 sp = p; // remember addr of insert
Cristian Ionescu-Idbohrn9a296fb2011-05-16 03:53:43 +02001854#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001855 p += 1 + stupid_insert(p, c); // insert the char
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001856#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001857 if (showmatch && strchr(")]}", *sp) != NULL) {
1858 showmatching(sp);
1859 }
1860 if (autoindent && c == '\n') { // auto indent the new line
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001861 char *q;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001862 size_t len;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001863 q = prev_line(p); // use prev line as template
Denis Vlasenko00d84172008-11-24 07:34:42 +00001864 len = strspn(q, " \t"); // space or tab
1865 if (len) {
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001866 uintptr_t bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001867 bias = text_hole_make(p, len);
1868 p += bias;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001869 q += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00001870 memcpy(p, q, len);
Denis Vlasenko3266aa92009-04-01 11:24:04 +00001871 p += len;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001872 }
1873 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00001874#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001875 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00001876 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001877}
1878
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001879// might reallocate text[]! use p += stupid_insert(p, ...),
1880// and be careful to not use pointers into potentially freed text[]!
1881static uintptr_t stupid_insert(char *p, char c) // stupidly insert the char c at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001882{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001883 uintptr_t bias;
1884 bias = text_hole_make(p, 1);
1885 p += bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00001886 *p = c;
1887 //file_modified++; - done by text_hole_make()
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00001888 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001889}
1890
Denis Vlasenko33875382008-06-21 20:31:50 +00001891static int find_range(char **start, char **stop, char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001892{
Paul Foxc51fc7b2008-03-06 01:34:23 +00001893 char *save_dot, *p, *q, *t;
1894 int cnt, multiline = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001895
1896 save_dot = dot;
1897 p = q = dot;
1898
1899 if (strchr("cdy><", c)) {
1900 // these cmds operate on whole lines
1901 p = q = begin_line(p);
1902 for (cnt = 1; cnt < cmdcnt; cnt++) {
1903 q = next_line(q);
1904 }
1905 q = end_line(q);
Paul Foxc51fc7b2008-03-06 01:34:23 +00001906 } else if (strchr("^%$0bBeEfth\b\177", c)) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001907 // These cmds operate on char positions
1908 do_cmd(c); // execute movement cmd
1909 q = dot;
1910 } else if (strchr("wW", c)) {
1911 do_cmd(c); // execute movement cmd
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001912 // if we are at the next word's first char
1913 // step back one char
1914 // but check the possibilities when it is true
Eric Andersen5cc90ea2004-02-06 10:36:08 +00001915 if (dot > text && ((isspace(dot[-1]) && !isspace(dot[0]))
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00001916 || (ispunct(dot[-1]) && !ispunct(dot[0]))
1917 || (isalnum(dot[-1]) && !isalnum(dot[0]))))
1918 dot--; // move back off of next word
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001919 if (dot > text && *dot == '\n')
1920 dot--; // stay off NL
1921 q = dot;
1922 } else if (strchr("H-k{", c)) {
1923 // these operate on multi-lines backwards
1924 q = end_line(dot); // find NL
1925 do_cmd(c); // execute movement cmd
1926 dot_begin();
1927 p = dot;
1928 } else if (strchr("L+j}\r\n", c)) {
1929 // these operate on multi-lines forwards
1930 p = begin_line(dot);
1931 do_cmd(c); // execute movement cmd
1932 dot_end(); // find NL
1933 q = dot;
1934 } else {
Paul Foxc51fc7b2008-03-06 01:34:23 +00001935 // nothing -- this causes any other values of c to
1936 // represent the one-character range under the
1937 // cursor. this is correct for ' ' and 'l', but
1938 // perhaps no others.
1939 //
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001940 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00001941 if (q < p) {
1942 t = q;
1943 q = p;
1944 p = t;
1945 }
1946
Denis Vlasenko42cc3042008-03-24 02:05:58 +00001947 // backward char movements don't include start position
Paul Foxc51fc7b2008-03-06 01:34:23 +00001948 if (q > p && strchr("^0bBh\b\177", c)) q--;
1949
1950 multiline = 0;
1951 for (t = p; t <= q; t++) {
1952 if (*t == '\n') {
1953 multiline = 1;
1954 break;
1955 }
1956 }
1957
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001958 *start = p;
1959 *stop = q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001960 dot = save_dot;
Paul Foxc51fc7b2008-03-06 01:34:23 +00001961 return multiline;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001962}
1963
Denis Vlasenko33875382008-06-21 20:31:50 +00001964static int st_test(char *p, int type, int dir, char *tested)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001965{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00001966 char c, c0, ci;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001967 int test, inc;
1968
1969 inc = dir;
1970 c = c0 = p[0];
1971 ci = p[inc];
1972 test = 0;
1973
1974 if (type == S_BEFORE_WS) {
1975 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001976 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001977 }
1978 if (type == S_TO_WS) {
1979 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001980 test = (!isspace(c) || c == '\n');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001981 }
1982 if (type == S_OVER_WS) {
1983 c = c0;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001984 test = isspace(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001985 }
1986 if (type == S_END_PUNCT) {
1987 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001988 test = ispunct(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001989 }
1990 if (type == S_END_ALNUM) {
1991 c = ci;
Denys Vlasenkoc0dab372009-10-22 22:28:08 +02001992 test = (isalnum(c) || c == '_');
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001993 }
1994 *tested = c;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00001995 return test;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001996}
1997
Denis Vlasenko33875382008-06-21 20:31:50 +00001998static char *skip_thing(char *p, int linecnt, int dir, int type)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00001999{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002000 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002001
2002 while (st_test(p, type, dir, &c)) {
2003 // make sure we limit search to correct number of lines
2004 if (c == '\n' && --linecnt < 1)
2005 break;
2006 if (dir >= 0 && p >= end - 1)
2007 break;
2008 if (dir < 0 && p <= text)
2009 break;
2010 p += dir; // move to next char
2011 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002012 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002013}
2014
2015// find matching char of pair () [] {}
Denis Vlasenko33875382008-06-21 20:31:50 +00002016static char *find_pair(char *p, const char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002017{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002018 char match, *q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002019 int dir, level;
2020
2021 match = ')';
2022 level = 1;
2023 dir = 1; // assume forward
2024 switch (c) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002025 case '(': match = ')'; break;
2026 case '[': match = ']'; break;
2027 case '{': match = '}'; break;
2028 case ')': match = '('; dir = -1; break;
2029 case ']': match = '['; dir = -1; break;
2030 case '}': match = '{'; dir = -1; break;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002031 }
2032 for (q = p + dir; text <= q && q < end; q += dir) {
2033 // look for match, count levels of pairs (( ))
2034 if (*q == c)
2035 level++; // increase pair levels
2036 if (*q == match)
2037 level--; // reduce pair level
2038 if (level == 0)
2039 break; // found matching pair
2040 }
2041 if (level != 0)
2042 q = NULL; // indicate no match
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002043 return q;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002044}
2045
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002046#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002047// show the matching char of a pair, () [] {}
Denis Vlasenkof882f082007-12-23 02:36:51 +00002048static void showmatching(char *p)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002049{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002050 char *q, *save_dot;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002051
2052 // we found half of a pair
2053 q = find_pair(p, *p); // get loc of matching char
2054 if (q == NULL) {
2055 indicate_error('3'); // no matching char
2056 } else {
2057 // "q" now points to matching pair
2058 save_dot = dot; // remember where we are
2059 dot = q; // go to new loc
2060 refresh(FALSE); // let the user see it
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002061 mysleep(40); // give user some time
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002062 dot = save_dot; // go back to old loc
2063 refresh(FALSE);
2064 }
2065}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002066#endif /* FEATURE_VI_SETOPTS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002067
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002068// open a hole in text[]
2069// might reallocate text[]! use p += text_hole_make(p, ...),
2070// and be careful to not use pointers into potentially freed text[]!
2071static uintptr_t text_hole_make(char *p, int size) // at "p", make a 'size' byte hole
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002072{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002073 uintptr_t bias = 0;
2074
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002075 if (size <= 0)
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002076 return bias;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002077 end += size; // adjust the new END
2078 if (end >= (text + text_size)) {
2079 char *new_text;
2080 text_size += end - (text + text_size) + 10240;
2081 new_text = xrealloc(text, text_size);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002082 bias = (new_text - text);
2083 screenbegin += bias;
2084 dot += bias;
2085 end += bias;
2086 p += bias;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002087#if ENABLE_FEATURE_VI_YANKMARK
2088 {
Tanguy Pruvot6fef6a32012-05-05 15:26:43 +02002089 unsigned i;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002090 for (i = 0; i < ARRAY_SIZE(mark); i++)
2091 if (mark[i])
2092 mark[i] += bias;
2093 }
2094#endif
Denis Vlasenkob1759462008-06-20 20:20:54 +00002095 text = new_text;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002096 }
Denis Vlasenkod6995442008-06-27 04:06:13 +00002097 memmove(p + size, p, end - size - p);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002098 memset(p, ' ', size); // clear new hole
Denis Vlasenkob1759462008-06-20 20:20:54 +00002099 file_modified++;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002100 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002101}
2102
2103// close a hole in text[]
Denis Vlasenko33875382008-06-21 20:31:50 +00002104static char *text_hole_delete(char *p, char *q) // delete "p" through "q", inclusive
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002105{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002106 char *src, *dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002107 int cnt, hole_size;
2108
2109 // move forwards, from beginning
2110 // assume p <= q
2111 src = q + 1;
2112 dest = p;
2113 if (q < p) { // they are backward- swap them
2114 src = p + 1;
2115 dest = q;
2116 }
2117 hole_size = q - p + 1;
2118 cnt = end - src;
2119 if (src < text || src > end)
2120 goto thd0;
2121 if (dest < text || dest >= end)
2122 goto thd0;
2123 if (src >= end)
2124 goto thd_atend; // just delete the end of the buffer
Denis Vlasenkob1759462008-06-20 20:20:54 +00002125 memmove(dest, src, cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002126 thd_atend:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002127 end = end - hole_size; // adjust the new END
2128 if (dest >= end)
2129 dest = end - 1; // make sure dest in below end-1
2130 if (end <= text)
2131 dest = end = text; // keep pointers valid
Denis Vlasenkob1759462008-06-20 20:20:54 +00002132 file_modified++;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002133 thd0:
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002134 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002135}
2136
2137// copy text into register, then delete text.
2138// if dist <= 0, do not include, or go past, a NewLine
2139//
Denis Vlasenko33875382008-06-21 20:31:50 +00002140static char *yank_delete(char *start, char *stop, int dist, int yf)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002141{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002142 char *p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002143
2144 // make sure start <= stop
2145 if (start > stop) {
2146 // they are backwards, reverse them
2147 p = start;
2148 start = stop;
2149 stop = p;
2150 }
2151 if (dist <= 0) {
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +00002152 // we cannot cross NL boundaries
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002153 p = start;
2154 if (*p == '\n')
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002155 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002156 // dont go past a NewLine
2157 for (; p + 1 <= stop; p++) {
2158 if (p[1] == '\n') {
2159 stop = p; // "stop" just before NewLine
2160 break;
2161 }
2162 }
2163 }
2164 p = start;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002165#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002166 text_yank(start, stop, YDreg);
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002167#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002168 if (yf == YANKDEL) {
2169 p = text_hole_delete(start, stop);
2170 } // delete lines
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002171 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002172}
2173
2174static void show_help(void)
2175{
2176 puts("These features are available:"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002177#if ENABLE_FEATURE_VI_SEARCH
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002178 "\n\tPattern searches with / and ?"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002179#endif
2180#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002181 "\n\tLast command repeat with \'.\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002182#endif
2183#if ENABLE_FEATURE_VI_YANKMARK
Bernhard Reutner-Fischerd24d5c82007-10-01 18:04:42 +00002184 "\n\tLine marking with 'x"
2185 "\n\tNamed buffers with \"x"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002186#endif
2187#if ENABLE_FEATURE_VI_READONLY
Walter Harmsb9ba5802011-06-27 02:59:37 +02002188 //not implemented: "\n\tReadonly if vi is called as \"view\""
2189 //redundant: usage text says this too: "\n\tReadonly with -R command line arg"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002190#endif
2191#if ENABLE_FEATURE_VI_SET
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002192 "\n\tSome colon mode commands with \':\'"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002193#endif
2194#if ENABLE_FEATURE_VI_SETOPTS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002195 "\n\tSettable options with \":set\""
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002196#endif
2197#if ENABLE_FEATURE_VI_USE_SIGNALS
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002198 "\n\tSignal catching- ^C"
2199 "\n\tJob suspend and resume with ^Z"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002200#endif
2201#if ENABLE_FEATURE_VI_WIN_RESIZE
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002202 "\n\tAdapt to window re-sizes"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002203#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002204 );
2205}
2206
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002207#if ENABLE_FEATURE_VI_DOT_CMD
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002208static void start_new_cmd_q(char c)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002209{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002210 // get buffer for new cmd
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002211 // if there is a current cmd count put it in the buffer first
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002212 if (cmdcnt > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00002213 lmc_len = sprintf(last_modifying_cmd, "%d%c", cmdcnt, c);
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002214 } else { // just save char c onto queue
Paul Foxd957b952005-11-28 18:07:53 +00002215 last_modifying_cmd[0] = c;
Paul Foxc51fc7b2008-03-06 01:34:23 +00002216 lmc_len = 1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002217 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002218 adding2q = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002219}
2220
2221static void end_cmd_q(void)
2222{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002223#if ENABLE_FEATURE_VI_YANKMARK
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002224 YDreg = 26; // go back to default Yank/Delete reg
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002225#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002226 adding2q = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002227}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002228#endif /* FEATURE_VI_DOT_CMD */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002229
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002230#if ENABLE_FEATURE_VI_YANKMARK \
2231 || (ENABLE_FEATURE_VI_COLON && ENABLE_FEATURE_VI_SEARCH) \
2232 || ENABLE_FEATURE_VI_CRASHME
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002233// might reallocate text[]! use p += string_insert(p, ...),
2234// and be careful to not use pointers into potentially freed text[]!
2235static uintptr_t string_insert(char *p, const char *s) // insert the string at 'p'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002236{
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002237 uintptr_t bias;
2238 int i;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002239
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002240 i = strlen(s);
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002241 bias = text_hole_make(p, i);
2242 p += bias;
Denis Vlasenko00d84172008-11-24 07:34:42 +00002243 memcpy(p, s, i);
Denis Vlasenko33875382008-06-21 20:31:50 +00002244#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002245 {
2246 int cnt;
2247 for (cnt = 0; *s != '\0'; s++) {
2248 if (*s == '\n')
2249 cnt++;
2250 }
2251 status_line("Put %d lines (%d chars) from [%c]", cnt, i, what_reg());
2252 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002253#endif
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002254 return bias;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002255}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002256#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002257
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002258#if ENABLE_FEATURE_VI_YANKMARK
Denis Vlasenko33875382008-06-21 20:31:50 +00002259static char *text_yank(char *p, char *q, int dest) // copy text into a register
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002260{
Denis Vlasenko00d84172008-11-24 07:34:42 +00002261 int cnt = q - p;
2262 if (cnt < 0) { // they are backwards- reverse them
2263 p = q;
2264 cnt = -cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002265 }
Denis Vlasenko00d84172008-11-24 07:34:42 +00002266 free(reg[dest]); // if already a yank register, free it
2267 reg[dest] = xstrndup(p, cnt + 1);
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002268 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002269}
2270
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002271static char what_reg(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002272{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002273 char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002274
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002275 c = 'D'; // default to D-reg
2276 if (0 <= YDreg && YDreg <= 25)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002277 c = 'a' + (char) YDreg;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002278 if (YDreg == 26)
2279 c = 'D';
2280 if (YDreg == 27)
2281 c = 'U';
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002282 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002283}
2284
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002285static void check_context(char cmd)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002286{
2287 // A context is defined to be "modifying text"
2288 // Any modifying command establishes a new context.
2289
2290 if (dot < context_start || dot > context_end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002291 if (strchr(modifying_cmds, cmd) != NULL) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002292 // we are trying to modify text[]- make this the current context
2293 mark[27] = mark[26]; // move cur to prev
2294 mark[26] = dot; // move local to cur
2295 context_start = prev_line(prev_line(dot));
2296 context_end = next_line(next_line(dot));
2297 //loiter= start_loiter= now;
2298 }
2299 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002300}
2301
Denis Vlasenkof882f082007-12-23 02:36:51 +00002302static char *swap_context(char *p) // goto new context for '' command make this the current context
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002303{
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002304 char *tmp;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002305
2306 // the current context is in mark[26]
2307 // the previous context is in mark[27]
2308 // only swap context if other context is valid
2309 if (text <= mark[27] && mark[27] <= end - 1) {
2310 tmp = mark[27];
2311 mark[27] = mark[26];
2312 mark[26] = tmp;
2313 p = mark[26]; // where we are going- previous context
2314 context_start = prev_line(prev_line(prev_line(p)));
2315 context_end = next_line(next_line(next_line(p)));
2316 }
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002317 return p;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002318}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002319#endif /* FEATURE_VI_YANKMARK */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002320
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002321//----- Set terminal attributes --------------------------------
2322static void rawmode(void)
2323{
2324 tcgetattr(0, &term_orig);
2325 term_vi = term_orig;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02002326 term_vi.c_lflag &= (~ICANON & ~ECHO); // leave ISIG on - allow intr's
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002327 term_vi.c_iflag &= (~IXON & ~ICRNL);
2328 term_vi.c_oflag &= (~ONLCR);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002329 term_vi.c_cc[VMIN] = 1;
2330 term_vi.c_cc[VTIME] = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002331 erase_char = term_vi.c_cc[VERASE];
Denis Vlasenko202ac502008-11-05 13:20:58 +00002332 tcsetattr_stdin_TCSANOW(&term_vi);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002333}
2334
2335static void cookmode(void)
2336{
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002337 fflush_all();
Denis Vlasenko202ac502008-11-05 13:20:58 +00002338 tcsetattr_stdin_TCSANOW(&term_orig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002339}
2340
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002341#if ENABLE_FEATURE_VI_USE_SIGNALS
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002342//----- Come here when we get a window resize signal ---------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002343static void winch_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002344{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002345 int save_errno = errno;
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002346 // FIXME: do it in main loop!!!
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002347 signal(SIGWINCH, winch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002348 query_screen_dimensions();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002349 new_screen(rows, columns); // get memory for virtual screen
2350 redraw(TRUE); // re-draw the screen
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002351 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002352}
2353
2354//----- Come here when we get a continue signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002355static void cont_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002356{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002357 int save_errno = errno;
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002358 rawmode(); // terminal to "raw"
2359 last_status_cksum = 0; // force status update
2360 redraw(TRUE); // re-draw the screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002361
2362 signal(SIGTSTP, suspend_sig);
2363 signal(SIGCONT, SIG_DFL);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002364 //kill(my_pid, SIGCONT); // huh? why? we are already "continued"...
2365 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002366}
2367
2368//----- Come here when we get a Suspend signal -------------------
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +00002369static void suspend_sig(int sig UNUSED_PARAM)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002370{
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002371 int save_errno = errno;
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002372 go_bottom_and_clear_to_eol();
Denis Vlasenko30cfdf92008-09-21 15:29:29 +00002373 cookmode(); // terminal to "cooked"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002374
2375 signal(SIGCONT, cont_sig);
2376 signal(SIGTSTP, SIG_DFL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002377 kill(my_pid, SIGTSTP);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002378 errno = save_errno;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002379}
2380
2381//----- Come here when we get a signal ---------------------------
2382static void catch_sig(int sig)
2383{
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002384 signal(SIGINT, catch_sig);
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002385 siglongjmp(restart, sig);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002386}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002387#endif /* FEATURE_VI_USE_SIGNALS */
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002388
Denys Vlasenko606291b2009-09-23 23:15:43 +02002389static int mysleep(int hund) // sleep for 'hund' 1/100 seconds or stdin ready
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002390{
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002391 struct pollfd pfd[1];
Denis Vlasenkocd5c7862007-05-17 16:37:22 +00002392
Denys Vlasenko606291b2009-09-23 23:15:43 +02002393 pfd[0].fd = STDIN_FILENO;
Denis Vlasenko87f3b262007-09-07 13:43:28 +00002394 pfd[0].events = POLLIN;
Denis Vlasenko5d61e712007-09-27 10:09:59 +00002395 return safe_poll(pfd, 1, hund*10) > 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002396}
2397
2398//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002399static int readit(void) // read (maybe cursor) key from stdin
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002400{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002401 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002402
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002403 fflush_all();
Denys Vlasenko58f108e2010-03-11 21:17:55 +01002404 c = read_key(STDIN_FILENO, readbuffer, /*timeout off:*/ -2);
Denis Vlasenko0112ff52008-10-25 23:23:00 +00002405 if (c == -1) { // EOF/error
2406 go_bottom_and_clear_to_eol();
2407 cookmode(); // terminal to "cooked"
2408 bb_error_msg_and_die("can't read user input");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002409 }
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002410 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002411}
2412
2413//----- IO Routines --------------------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002414static int get_one_char(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002415{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002416 int c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002417
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002418#if ENABLE_FEATURE_VI_DOT_CMD
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002419 if (!adding2q) {
2420 // we are not adding to the q.
2421 // but, we may be reading from a q
2422 if (ioq == 0) {
2423 // there is no current q, read from STDIN
2424 c = readit(); // get the users input
2425 } else {
2426 // there is a queue to get chars from first
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002427 // careful with correct sign expansion!
2428 c = (unsigned char)*ioq++;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002429 if (c == '\0') {
2430 // the end of the q, read from STDIN
2431 free(ioq_start);
2432 ioq_start = ioq = 0;
2433 c = readit(); // get the users input
2434 }
2435 }
2436 } else {
2437 // adding STDIN chars to q
2438 c = readit(); // get the users input
Denis Vlasenkob1759462008-06-20 20:20:54 +00002439 if (lmc_len >= MAX_INPUT_LEN - 1) {
2440 status_line_bold("last_modifying_cmd overrun");
2441 } else {
2442 // add new char to q
2443 last_modifying_cmd[lmc_len++] = c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002444 }
2445 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002446#else
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002447 c = readit(); // get the users input
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002448#endif /* FEATURE_VI_DOT_CMD */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002449 return c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002450}
2451
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002452// Get input line (uses "status line" area)
2453static char *get_input_line(const char *prompt)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002454{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002455 // char [MAX_INPUT_LEN]
2456#define buf get_input_line__buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002457
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002458 int c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002459 int i;
2460
2461 strcpy(buf, prompt);
Paul Fox8552aec2005-09-16 12:20:05 +00002462 last_status_cksum = 0; // force status update
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002463 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002464 write1(prompt); // write out the :, /, or ? prompt
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002465
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002466 i = strlen(buf);
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002467 while (i < MAX_INPUT_LEN) {
2468 c = get_one_char();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002469 if (c == '\n' || c == '\r' || c == 27)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002470 break; // this is end of input
Paul Foxf2de0b72005-09-13 22:20:37 +00002471 if (c == erase_char || c == 8 || c == 127) {
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00002472 // user wants to erase prev char
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002473 buf[--i] = '\0';
2474 write1("\b \b"); // erase char on screen
2475 if (i <= 0) // user backs up before b-o-l, exit
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002476 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00002477 } else if (c > 0 && c < 256) { // exclude Unicode
2478 // (TODO: need to handle Unicode)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002479 buf[i] = c;
2480 buf[++i] = '\0';
2481 bb_putchar(c);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002482 }
2483 }
2484 refresh(FALSE);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002485 return buf;
Denis Vlasenkob1759462008-06-20 20:20:54 +00002486#undef buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002487}
2488
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002489static int file_size(const char *fn) // what is the byte size of "fn"
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002490{
2491 struct stat st_buf;
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002492 int cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002493
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002494 cnt = -1;
Denys Vlasenkodef47832010-04-18 20:39:41 -07002495 if (fn && stat(fn, &st_buf) == 0) // see if file exists
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002496 cnt = (int) st_buf.st_size;
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002497 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002498}
2499
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002500// might reallocate text[]!
2501static int file_insert(const char *fn, char *p, int update_ro_status)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002502{
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002503 int cnt = -1;
2504 int fd, size;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002505 struct stat statbuf;
2506
2507 /* Validate file */
2508 if (stat(fn, &statbuf) < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002509 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002510 goto fi0;
2511 }
Denis Vlasenko33875382008-06-21 20:31:50 +00002512 if (!S_ISREG(statbuf.st_mode)) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002513 // This is not a regular file
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002514 status_line_bold("\"%s\" Not a regular file", fn);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002515 goto fi0;
2516 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002517 if (p < text || p > end) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002518 status_line_bold("Trying to insert file outside of memory");
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002519 goto fi0;
2520 }
2521
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002522 // read file to buffer
2523 fd = open(fn, O_RDONLY);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002524 if (fd < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002525 status_line_bold("\"%s\" %s", fn, strerror(errno));
Denis Vlasenko59a1f302007-07-14 22:43:10 +00002526 goto fi0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002527 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002528 size = statbuf.st_size;
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00002529 p += text_hole_make(p, size);
Denis Vlasenko4f95e5a2007-10-11 10:10:15 +00002530 cnt = safe_read(fd, p, size);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002531 if (cnt < 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002532 status_line_bold("\"%s\" %s", fn, strerror(errno));
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002533 p = text_hole_delete(p, p + size - 1); // un-do buffer insert
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002534 } else if (cnt < size) {
2535 // There was a partial read, shrink unused space text[]
2536 p = text_hole_delete(p + cnt, p + (size - cnt) - 1); // un-do buffer insert
Denys Vlasenko6331cf02009-11-13 09:08:27 +01002537 status_line_bold("can't read all of file \"%s\"", fn);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002538 }
2539 if (cnt >= size)
Paul Fox8552aec2005-09-16 12:20:05 +00002540 file_modified++;
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002541 close(fd);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002542 fi0:
Denis Vlasenko856be772007-08-17 08:29:48 +00002543#if ENABLE_FEATURE_VI_READONLY
2544 if (update_ro_status
2545 && ((access(fn, W_OK) < 0) ||
2546 /* root will always have access()
2547 * so we check fileperms too */
2548 !(statbuf.st_mode & (S_IWUSR | S_IWGRP | S_IWOTH))
2549 )
2550 ) {
Denis Vlasenko2f6ae432007-07-19 22:50:47 +00002551 SET_READONLY_FILE(readonly_mode);
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002552 }
Denis Vlasenko856be772007-08-17 08:29:48 +00002553#endif
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002554 return cnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002555}
2556
Denis Vlasenko33875382008-06-21 20:31:50 +00002557static int file_write(char *fn, char *first, char *last)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002558{
2559 int fd, cnt, charcnt;
2560
2561 if (fn == 0) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002562 status_line_bold("No current filename");
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002563 return -2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002564 }
Denis Vlasenko8abae882008-05-03 11:35:59 +00002565 /* By popular request we do not open file with O_TRUNC,
2566 * but instead ftruncate() it _after_ successful write.
2567 * Might reduce amount of data lost on power fail etc.
2568 */
2569 fd = open(fn, (O_WRONLY | O_CREAT), 0666);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002570 if (fd < 0)
Denis Vlasenko079f8af2006-11-27 16:49:31 +00002571 return -1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002572 cnt = last - first + 1;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002573 charcnt = full_write(fd, first, cnt);
Denis Vlasenko8abae882008-05-03 11:35:59 +00002574 ftruncate(fd, charcnt);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002575 if (charcnt == cnt) {
2576 // good write
Denis Vlasenkob1759462008-06-20 20:20:54 +00002577 //file_modified = FALSE;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002578 } else {
2579 charcnt = 0;
2580 }
2581 close(fd);
Denis Vlasenkod9e15f22006-11-27 16:49:55 +00002582 return charcnt;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002583}
2584
2585//----- Terminal Drawing ---------------------------------------
2586// The terminal is made up of 'rows' line of 'columns' columns.
Eric Andersenaff114c2004-04-14 17:51:38 +00002587// classically this would be 24 x 80.
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002588// screen coordinates
2589// 0,0 ... 0,79
2590// 1,0 ... 1,79
2591// . ... .
2592// . ... .
2593// 22,0 ... 22,79
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002594// 23,0 ... 23,79 <- status line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002595
2596//----- Move the cursor to row x col (count from 0, not 1) -------
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002597static void place_cursor(int row, int col, int optimize)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002598{
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002599 char cm1[sizeof(CMrc) + sizeof(int)*3 * 2];
Denis Vlasenko7b54dc72008-07-17 21:32:32 +00002600#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
2601 enum {
2602 SZ_UP = sizeof(CMup),
2603 SZ_DN = sizeof(CMdown),
2604 SEQ_SIZE = SZ_UP > SZ_DN ? SZ_UP : SZ_DN,
2605 };
2606 char cm2[SEQ_SIZE * 5 + 32]; // bigger than worst case size
2607#endif
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002608 char *cm;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002609
2610 if (row < 0) row = 0;
2611 if (row >= rows) row = rows - 1;
2612 if (col < 0) col = 0;
2613 if (col >= columns) col = columns - 1;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002614
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002615 //----- 1. Try the standard terminal ESC sequence
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002616 sprintf(cm1, CMrc, row + 1, col + 1);
2617 cm = cm1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002618
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002619#if ENABLE_FEATURE_VI_OPTIMIZE_CURSOR
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002620 if (optimize && col < 16) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002621 char *screenp;
2622 int Rrow = last_row;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002623 int diff = Rrow - row;
2624
2625 if (diff < -5 || diff > 5)
2626 goto skip;
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002627
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002628 //----- find the minimum # of chars to move cursor -------------
2629 //----- 2. Try moving with discreet chars (Newline, [back]space, ...)
2630 cm2[0] = '\0';
2631
2632 // move to the correct row
2633 while (row < Rrow) {
2634 // the cursor has to move up
2635 strcat(cm2, CMup);
2636 Rrow--;
2637 }
2638 while (row > Rrow) {
2639 // the cursor has to move down
2640 strcat(cm2, CMdown);
2641 Rrow++;
2642 }
2643
2644 // now move to the correct column
2645 strcat(cm2, "\r"); // start at col 0
2646 // just send out orignal source char to get to correct place
2647 screenp = &screen[row * columns]; // start of screen line
2648 strncat(cm2, screenp, col);
2649
2650 // pick the shortest cursor motion to send out
2651 if (strlen(cm2) < strlen(cm)) {
2652 cm = cm2;
2653 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002654 skip: ;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002655 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002656 last_row = row;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002657#endif /* FEATURE_VI_OPTIMIZE_CURSOR */
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002658 write1(cm);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002659}
2660
2661//----- Erase from cursor to end of line -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002662static void clear_to_eol(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002663{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002664 write1(Ceol); // Erase from cursor to end of line
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002665}
2666
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002667static void go_bottom_and_clear_to_eol(void)
2668{
2669 place_cursor(rows - 1, 0, FALSE); // go to bottom of screen
2670 clear_to_eol(); // erase to end of line
2671}
2672
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002673//----- Erase from cursor to end of screen -----------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002674static void clear_to_eos(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002675{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002676 write1(Ceos); // Erase from cursor to end of screen
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002677}
2678
2679//----- Start standout mode ------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002680static void standout_start(void) // send "start reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002681{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002682 write1(SOs); // Start reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002683}
2684
2685//----- End standout mode --------------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002686static void standout_end(void) // send "end reverse video" sequence
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002687{
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002688 write1(SOn); // End reverse video mode
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002689}
2690
2691//----- Flash the screen --------------------------------------
2692static void flash(int h)
2693{
2694 standout_start(); // send "start reverse video" sequence
2695 redraw(TRUE);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002696 mysleep(h);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002697 standout_end(); // send "end reverse video" sequence
2698 redraw(TRUE);
2699}
2700
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002701static void Indicate_Error(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002702{
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002703#if ENABLE_FEATURE_VI_CRASHME
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002704 if (crashme > 0)
2705 return; // generate a random command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002706#endif
Glenn L McGrath09adaca2002-12-02 21:18:10 +00002707 if (!err_method) {
2708 write1(bell); // send out a bell character
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002709 } else {
2710 flash(10);
2711 }
2712}
2713
2714//----- Screen[] Routines --------------------------------------
2715//----- Erase the Screen[] memory ------------------------------
Mike Frysinger4e5936e2005-04-16 04:30:38 +00002716static void screen_erase(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002717{
2718 memset(screen, ' ', screensize); // clear new screen
2719}
2720
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002721static int bufsum(char *buf, int count)
Paul Fox8552aec2005-09-16 12:20:05 +00002722{
2723 int sum = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002724 char *e = buf + count;
2725
Paul Fox8552aec2005-09-16 12:20:05 +00002726 while (buf < e)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002727 sum += (unsigned char) *buf++;
Paul Fox8552aec2005-09-16 12:20:05 +00002728 return sum;
2729}
2730
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002731//----- Draw the status line at bottom of the screen -------------
2732static void show_status_line(void)
2733{
Paul Foxc3504852005-09-16 12:48:18 +00002734 int cnt = 0, cksum = 0;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002735
Paul Fox8552aec2005-09-16 12:20:05 +00002736 // either we already have an error or status message, or we
2737 // create one.
2738 if (!have_status_msg) {
2739 cnt = format_edit_status();
2740 cksum = bufsum(status_buffer, cnt);
2741 }
2742 if (have_status_msg || ((cnt > 0 && last_status_cksum != cksum))) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002743 last_status_cksum = cksum; // remember if we have seen this line
Denis Vlasenko267e16c2008-10-14 10:34:41 +00002744 go_bottom_and_clear_to_eol();
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002745 write1(status_buffer);
Paul Fox8552aec2005-09-16 12:20:05 +00002746 if (have_status_msg) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002747 if (((int)strlen(status_buffer) - (have_status_msg - 1)) >
Paul Fox8552aec2005-09-16 12:20:05 +00002748 (columns - 1) ) {
2749 have_status_msg = 0;
2750 Hit_Return();
2751 }
2752 have_status_msg = 0;
2753 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002754 place_cursor(crow, ccol, FALSE); // put cursor back in correct place
2755 }
Denys Vlasenko8131eea2009-11-02 14:19:51 +01002756 fflush_all();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002757}
2758
2759//----- format the status buffer, the bottom line of screen ------
Paul Fox8552aec2005-09-16 12:20:05 +00002760// format status buffer, with STANDOUT mode
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002761static void status_line_bold(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002762{
2763 va_list args;
2764
2765 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002766 strcpy(status_buffer, SOs); // Terminal standout mode on
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002767 vsprintf(status_buffer + sizeof(SOs)-1, format, args);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002768 strcat(status_buffer, SOn); // Terminal standout mode off
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002769 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002770
2771 have_status_msg = 1 + sizeof(SOs) + sizeof(SOn) - 2;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002772}
2773
Paul Fox8552aec2005-09-16 12:20:05 +00002774// format status buffer
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002775static void status_line(const char *format, ...)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002776{
2777 va_list args;
2778
2779 va_start(args, format);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002780 vsprintf(status_buffer, format, args);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002781 va_end(args);
Paul Fox8552aec2005-09-16 12:20:05 +00002782
2783 have_status_msg = 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002784}
2785
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002786// copy s to buf, convert unprintable
2787static void print_literal(char *buf, const char *s)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002788{
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002789 char *d;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002790 unsigned char c;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002791
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002792 buf[0] = '\0';
2793 if (!s[0])
2794 s = "(NULL)";
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002795
2796 d = buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002797 for (; *s; s++) {
2798 int c_is_no_print;
2799
2800 c = *s;
2801 c_is_no_print = (c & 0x80) && !Isprint(c);
2802 if (c_is_no_print) {
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002803 strcpy(d, SOn);
2804 d += sizeof(SOn)-1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002805 c = '.';
2806 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002807 if (c < ' ' || c == 0x7f) {
2808 *d++ = '^';
2809 c |= '@'; /* 0x40 */
2810 if (c == 0x7f)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002811 c = '?';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002812 }
Denys Vlasenkoc2704542009-11-20 19:14:19 +01002813 *d++ = c;
2814 *d = '\0';
2815 if (c_is_no_print) {
2816 strcpy(d, SOs);
2817 d += sizeof(SOs)-1;
2818 }
2819 if (*s == '\n') {
2820 *d++ = '$';
2821 *d = '\0';
2822 }
2823 if (d - buf > MAX_INPUT_LEN - 10) // paranoia
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002824 break;
2825 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002826}
2827
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002828static void not_implemented(const char *s)
2829{
2830 char buf[MAX_INPUT_LEN];
2831
2832 print_literal(buf, s);
2833 status_line_bold("\'%s\' is not implemented", buf);
2834}
2835
2836// show file status on status line
2837static int format_edit_status(void)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002838{
Denis Vlasenko6ca409e2007-08-12 20:58:27 +00002839 static const char cmd_mode_indicator[] ALIGN1 = "-IR-";
Denis Vlasenkob1759462008-06-20 20:20:54 +00002840
2841#define tot format_edit_status__tot
2842
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002843 int cur, percent, ret, trunc_at;
2844
Paul Fox8552aec2005-09-16 12:20:05 +00002845 // file_modified is now a counter rather than a flag. this
2846 // helps reduce the amount of line counting we need to do.
2847 // (this will cause a mis-reporting of modified status
2848 // once every MAXINT editing operations.)
2849
2850 // it would be nice to do a similar optimization here -- if
2851 // we haven't done a motion that could have changed which line
2852 // we're on, then we shouldn't have to do this count_lines()
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002853 cur = count_lines(text, dot);
Paul Fox8552aec2005-09-16 12:20:05 +00002854
2855 // reduce counting -- the total lines can't have
2856 // changed if we haven't done any edits.
2857 if (file_modified != last_file_modified) {
2858 tot = cur + count_lines(dot, end - 1) - 1;
2859 last_file_modified = file_modified;
2860 }
2861
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002862 // current line percent
2863 // ------------- ~~ ----------
2864 // total lines 100
2865 if (tot > 0) {
2866 percent = (100 * cur) / tot;
2867 } else {
2868 cur = tot = 0;
2869 percent = 100;
2870 }
Eric Andersen0ef24c62005-07-18 10:32:59 +00002871
Paul Fox8552aec2005-09-16 12:20:05 +00002872 trunc_at = columns < STATUS_BUFFER_LEN-1 ?
2873 columns : STATUS_BUFFER_LEN-1;
2874
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002875 ret = snprintf(status_buffer, trunc_at+1,
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002876#if ENABLE_FEATURE_VI_READONLY
Paul Fox8552aec2005-09-16 12:20:05 +00002877 "%c %s%s%s %d/%d %d%%",
2878#else
2879 "%c %s%s %d/%d %d%%",
2880#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002881 cmd_mode_indicator[cmd_mode & 3],
2882 (current_filename != NULL ? current_filename : "No file"),
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00002883#if ENABLE_FEATURE_VI_READONLY
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002884 (readonly_mode ? " [Readonly]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002885#endif
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00002886 (file_modified ? " [Modified]" : ""),
Paul Fox8552aec2005-09-16 12:20:05 +00002887 cur, tot, percent);
2888
2889 if (ret >= 0 && ret < trunc_at)
2890 return ret; /* it all fit */
2891
2892 return trunc_at; /* had to truncate */
Denis Vlasenkob1759462008-06-20 20:20:54 +00002893#undef tot
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002894}
2895
2896//----- Force refresh of all Lines -----------------------------
2897static void redraw(int full_screen)
2898{
2899 place_cursor(0, 0, FALSE); // put cursor in correct place
Denis Vlasenkob1759462008-06-20 20:20:54 +00002900 clear_to_eos(); // tell terminal to erase display
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002901 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00002902 last_status_cksum = 0; // force status update
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002903 refresh(full_screen); // this will redraw the entire display
Paul Fox8552aec2005-09-16 12:20:05 +00002904 show_status_line();
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002905}
2906
2907//----- Format a text[] line into a buffer ---------------------
Denis Vlasenko68404f12008-03-17 09:00:54 +00002908static char* format_line(char *src /*, int li*/)
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002909{
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002910 unsigned char c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002911 int co;
2912 int ofs = offset;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002913 char *dest = scr_out_buf; // [MAX_SCR_COLS + MAX_TABSTOP * 2]
Eric Andersenc7bda1c2004-03-15 08:29:22 +00002914
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002915 c = '~'; // char in col 0 in non-existent lines is '~'
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002916 co = 0;
2917 while (co < columns + tabstop) {
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002918 // have we gone past the end?
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002919 if (src < end) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002920 c = *src++;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002921 if (c == '\n')
2922 break;
2923 if ((c & 0x80) && !Isprint(c)) {
2924 c = '.';
2925 }
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00002926 if (c < ' ' || c == 0x7f) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002927 if (c == '\t') {
2928 c = ' ';
2929 // co % 8 != 7
2930 while ((co % tabstop) != (tabstop - 1)) {
2931 dest[co++] = c;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002932 }
2933 } else {
2934 dest[co++] = '^';
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002935 if (c == 0x7f)
2936 c = '?';
2937 else
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002938 c += '@'; // Ctrl-X -> 'X'
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002939 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002940 }
2941 }
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002942 dest[co++] = c;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002943 // discard scrolled-off-to-the-left portion,
2944 // in tabstop-sized pieces
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002945 if (ofs >= tabstop && co >= tabstop) {
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002946 memmove(dest, dest + tabstop, co);
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002947 co -= tabstop;
2948 ofs -= tabstop;
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00002949 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002950 if (src >= end)
2951 break;
2952 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002953 // check "short line, gigantic offset" case
2954 if (co < ofs)
Denis Vlasenko4baed3a2007-12-22 17:31:29 +00002955 ofs = co;
2956 // discard last scrolled off part
2957 co -= ofs;
2958 dest += ofs;
2959 // fill the rest with spaces
2960 if (co < columns)
2961 memset(&dest[co], ' ', columns - co);
2962 return dest;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002963}
2964
2965//----- Refresh the changed screen lines -----------------------
2966// Copy the source line from text[] into the buffer and note
2967// if the current screenline is different from the new buffer.
2968// If they differ then that line needs redrawing on the terminal.
2969//
2970static void refresh(int full_screen)
2971{
Denis Vlasenkob1759462008-06-20 20:20:54 +00002972#define old_offset refresh__old_offset
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002973
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002974 int li, changed;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00002975 char *tp, *sp; // pointer into text[] and screen[]
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002976
Denys Vlasenkoc5fb0ad2010-07-21 12:39:42 +02002977 if (ENABLE_FEATURE_VI_WIN_RESIZE IF_FEATURE_VI_ASK_TERMINAL(&& !G.get_rowcol_error) ) {
Denis Vlasenko55995022008-05-18 22:28:26 +00002978 unsigned c = columns, r = rows;
Denys Vlasenko2bb651a2010-04-16 20:55:52 -07002979 query_screen_dimensions();
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002980 full_screen |= (c - columns) | (r - rows);
2981 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002982 sync_cursor(dot, &crow, &ccol); // where cursor will be (on "dot")
2983 tp = screenbegin; // index into text[] of top line
2984
2985 // compare text[] to screen[] and mark screen[] lines that need updating
2986 for (li = 0; li < rows - 1; li++) {
2987 int cs, ce; // column start & end
Denis Vlasenkof882f082007-12-23 02:36:51 +00002988 char *out_buf;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002989 // format current text line
Denis Vlasenko68404f12008-03-17 09:00:54 +00002990 out_buf = format_line(tp /*, li*/);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002991
2992 // skip to the end of the current text[] line
Denis Vlasenkof882f082007-12-23 02:36:51 +00002993 if (tp < end) {
2994 char *t = memchr(tp, '\n', end - tp);
2995 if (!t) t = end - 1;
2996 tp = t + 1;
2997 }
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00002998
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00002999 // see if there are any changes between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003000 changed = FALSE; // assume no change
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003001 cs = 0;
3002 ce = columns - 1;
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003003 sp = &screen[li * columns]; // start of screen line
3004 if (full_screen) {
3005 // force re-draw of every single column from 0 - columns-1
3006 goto re0;
3007 }
3008 // compare newly formatted buffer with virtual screen
3009 // look forward for first difference between buf and screen
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003010 for (; cs <= ce; cs++) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003011 if (out_buf[cs] != sp[cs]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003012 changed = TRUE; // mark for redraw
3013 break;
3014 }
3015 }
3016
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003017 // look backward for last difference between out_buf and screen
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003018 for (; ce >= cs; ce--) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003019 if (out_buf[ce] != sp[ce]) {
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003020 changed = TRUE; // mark for redraw
3021 break;
3022 }
3023 }
3024 // now, cs is index of first diff, and ce is index of last diff
3025
3026 // if horz offset has changed, force a redraw
3027 if (offset != old_offset) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003028 re0:
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003029 changed = TRUE;
3030 }
3031
3032 // make a sanity check of columns indexes
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003033 if (cs < 0) cs = 0;
3034 if (ce > columns - 1) ce = columns - 1;
3035 if (cs > ce) { cs = 0; ce = columns - 1; }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003036 // is there a change between vitual screen and out_buf
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003037 if (changed) {
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003038 // copy changed part of buffer to virtual screen
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003039 memcpy(sp+cs, out_buf+cs, ce-cs+1);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003040
3041 // move cursor to column of first change
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003042 //if (offset != old_offset) {
3043 // // place_cursor is still too stupid
3044 // // to handle offsets correctly
3045 // place_cursor(li, cs, FALSE);
3046 //} else {
3047 place_cursor(li, cs, TRUE);
3048 //}
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003049
3050 // write line out to terminal
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003051 fwrite(&sp[cs], ce - cs + 1, 1, stdout);
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003052 }
3053 }
3054
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003055 place_cursor(crow, ccol, TRUE);
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003056
Denis Vlasenko26b6fba2007-12-21 21:34:37 +00003057 old_offset = offset;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003058#undef old_offset
Aaron Lehmann6fdacc72002-08-21 13:02:24 +00003059}
3060
Eric Andersen3f980402001-04-04 17:31:15 +00003061//---------------------------------------------------------------------
3062//----- the Ascii Chart -----------------------------------------------
3063//
3064// 00 nul 01 soh 02 stx 03 etx 04 eot 05 enq 06 ack 07 bel
3065// 08 bs 09 ht 0a nl 0b vt 0c np 0d cr 0e so 0f si
3066// 10 dle 11 dc1 12 dc2 13 dc3 14 dc4 15 nak 16 syn 17 etb
3067// 18 can 19 em 1a sub 1b esc 1c fs 1d gs 1e rs 1f us
3068// 20 sp 21 ! 22 " 23 # 24 $ 25 % 26 & 27 '
3069// 28 ( 29 ) 2a * 2b + 2c , 2d - 2e . 2f /
3070// 30 0 31 1 32 2 33 3 34 4 35 5 36 6 37 7
3071// 38 8 39 9 3a : 3b ; 3c < 3d = 3e > 3f ?
3072// 40 @ 41 A 42 B 43 C 44 D 45 E 46 F 47 G
3073// 48 H 49 I 4a J 4b K 4c L 4d M 4e N 4f O
3074// 50 P 51 Q 52 R 53 S 54 T 55 U 56 V 57 W
3075// 58 X 59 Y 5a Z 5b [ 5c \ 5d ] 5e ^ 5f _
3076// 60 ` 61 a 62 b 63 c 64 d 65 e 66 f 67 g
3077// 68 h 69 i 6a j 6b k 6c l 6d m 6e n 6f o
3078// 70 p 71 q 72 r 73 s 74 t 75 u 76 v 77 w
3079// 78 x 79 y 7a z 7b { 7c | 7d } 7e ~ 7f del
3080//---------------------------------------------------------------------
3081
3082//----- Execute a Vi Command -----------------------------------
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003083static void do_cmd(int c)
Eric Andersen3f980402001-04-04 17:31:15 +00003084{
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003085 char *p, *q, *save_dot;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003086 char buf[12];
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003087 int dir;
Paul Foxc51fc7b2008-03-06 01:34:23 +00003088 int cnt, i, j;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003089 int c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003090
Denis Vlasenkoe3cbfb92007-12-22 17:00:11 +00003091// c1 = c; // quiet the compiler
3092// cnt = yf = 0; // quiet the compiler
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003093// p = q = save_dot = buf; // quiet the compiler
3094 memset(buf, '\0', sizeof(buf));
Eric Andersenbff7a602001-11-17 07:15:43 +00003095
Paul Fox8552aec2005-09-16 12:20:05 +00003096 show_status_line();
3097
Eric Andersenbff7a602001-11-17 07:15:43 +00003098 /* if this is a cursor key, skip these checks */
3099 switch (c) {
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003100 case KEYCODE_UP:
3101 case KEYCODE_DOWN:
3102 case KEYCODE_LEFT:
3103 case KEYCODE_RIGHT:
3104 case KEYCODE_HOME:
3105 case KEYCODE_END:
3106 case KEYCODE_PAGEUP:
3107 case KEYCODE_PAGEDOWN:
3108 case KEYCODE_DELETE:
Eric Andersenbff7a602001-11-17 07:15:43 +00003109 goto key_cmd_mode;
3110 }
3111
Eric Andersen3f980402001-04-04 17:31:15 +00003112 if (cmd_mode == 2) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003113 // flip-flop Insert/Replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003114 if (c == KEYCODE_INSERT)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003115 goto dc_i;
Eric Andersen3f980402001-04-04 17:31:15 +00003116 // we are 'R'eplacing the current *dot with new char
3117 if (*dot == '\n') {
3118 // don't Replace past E-o-l
3119 cmd_mode = 1; // convert to insert
3120 } else {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003121 if (1 <= c || Isprint(c)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003122 if (c != 27)
3123 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3124 dot = char_insert(dot, c); // insert new char
3125 }
3126 goto dc1;
3127 }
3128 }
3129 if (cmd_mode == 1) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003130 // hitting "Insert" twice means "R" replace mode
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003131 if (c == KEYCODE_INSERT) goto dc5;
Eric Andersen3f980402001-04-04 17:31:15 +00003132 // insert the char c at "dot"
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003133 if (1 <= c || Isprint(c)) {
3134 dot = char_insert(dot, c);
Eric Andersen3f980402001-04-04 17:31:15 +00003135 }
3136 goto dc1;
3137 }
3138
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003139 key_cmd_mode:
Eric Andersen3f980402001-04-04 17:31:15 +00003140 switch (c) {
Eric Andersen822c3832001-05-07 17:37:43 +00003141 //case 0x01: // soh
3142 //case 0x09: // ht
3143 //case 0x0b: // vt
3144 //case 0x0e: // so
3145 //case 0x0f: // si
3146 //case 0x10: // dle
3147 //case 0x11: // dc1
3148 //case 0x13: // dc3
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003149#if ENABLE_FEATURE_VI_CRASHME
Eric Andersen1c0d3112001-04-16 15:46:44 +00003150 case 0x14: // dc4 ctrl-T
Eric Andersen3f980402001-04-04 17:31:15 +00003151 crashme = (crashme == 0) ? 1 : 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003152 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003153#endif
Eric Andersen822c3832001-05-07 17:37:43 +00003154 //case 0x16: // syn
3155 //case 0x17: // etb
3156 //case 0x18: // can
3157 //case 0x1c: // fs
3158 //case 0x1d: // gs
3159 //case 0x1e: // rs
3160 //case 0x1f: // us
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003161 //case '!': // !-
3162 //case '#': // #-
3163 //case '&': // &-
3164 //case '(': // (-
3165 //case ')': // )-
3166 //case '*': // *-
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003167 //case '=': // =-
3168 //case '@': // @-
3169 //case 'F': // F-
3170 //case 'K': // K-
3171 //case 'Q': // Q-
3172 //case 'S': // S-
3173 //case 'T': // T-
3174 //case 'V': // V-
3175 //case '[': // [-
3176 //case '\\': // \-
3177 //case ']': // ]-
3178 //case '_': // _-
3179 //case '`': // `-
Eric Andersen1c0d3112001-04-16 15:46:44 +00003180 //case 'u': // u- FIXME- there is no undo
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003181 //case 'v': // v-
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003182 default: // unrecognized command
Eric Andersen3f980402001-04-04 17:31:15 +00003183 buf[0] = c;
3184 buf[1] = '\0';
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003185 not_implemented(buf);
Eric Andersen3f980402001-04-04 17:31:15 +00003186 end_cmd_q(); // stop adding to q
3187 case 0x00: // nul- ignore
3188 break;
3189 case 2: // ctrl-B scroll up full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003190 case KEYCODE_PAGEUP: // Cursor Key Page Up
Eric Andersen3f980402001-04-04 17:31:15 +00003191 dot_scroll(rows - 2, -1);
3192 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003193 case 4: // ctrl-D scroll down half screen
3194 dot_scroll((rows - 2) / 2, 1);
3195 break;
3196 case 5: // ctrl-E scroll down one line
3197 dot_scroll(1, 1);
3198 break;
3199 case 6: // ctrl-F scroll down full screen
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003200 case KEYCODE_PAGEDOWN: // Cursor Key Page Down
Eric Andersen3f980402001-04-04 17:31:15 +00003201 dot_scroll(rows - 2, 1);
3202 break;
3203 case 7: // ctrl-G show current status
Paul Fox8552aec2005-09-16 12:20:05 +00003204 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003205 break;
3206 case 'h': // h- move left
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003207 case KEYCODE_LEFT: // cursor key Left
Paul Foxd13b90b2005-07-18 22:17:25 +00003208 case 8: // ctrl-H- move left (This may be ERASE char)
Denis Vlasenko2a51af22007-03-21 22:31:24 +00003209 case 0x7f: // DEL- move left (This may be ERASE char)
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003210 do {
3211 dot_left();
3212 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003213 break;
3214 case 10: // Newline ^J
3215 case 'j': // j- goto next line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003216 case KEYCODE_DOWN: // cursor key Down
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003217 do {
3218 dot_next(); // go to next B-o-l
3219 // try stay in same col
3220 dot = move_to_col(dot, ccol + offset);
3221 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003222 break;
3223 case 12: // ctrl-L force redraw whole screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003224 case 18: // ctrl-R force redraw
Eric Andersen822c3832001-05-07 17:37:43 +00003225 place_cursor(0, 0, FALSE); // put cursor in correct place
Eric Andersen3f980402001-04-04 17:31:15 +00003226 clear_to_eos(); // tel terminal to erase display
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003227 mysleep(10);
Eric Andersen3f980402001-04-04 17:31:15 +00003228 screen_erase(); // erase the internal screen buffer
Paul Fox8552aec2005-09-16 12:20:05 +00003229 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003230 refresh(TRUE); // this will redraw the entire display
3231 break;
3232 case 13: // Carriage Return ^M
3233 case '+': // +- goto next line
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003234 do {
3235 dot_next();
3236 dot_skip_over_ws();
3237 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003238 break;
3239 case 21: // ctrl-U scroll up half screen
3240 dot_scroll((rows - 2) / 2, -1);
3241 break;
3242 case 25: // ctrl-Y scroll up one line
3243 dot_scroll(1, -1);
3244 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003245 case 27: // esc
Eric Andersen3f980402001-04-04 17:31:15 +00003246 if (cmd_mode == 0)
3247 indicate_error(c);
3248 cmd_mode = 0; // stop insrting
3249 end_cmd_q();
Paul Fox8552aec2005-09-16 12:20:05 +00003250 last_status_cksum = 0; // force status update
Eric Andersen3f980402001-04-04 17:31:15 +00003251 break;
3252 case ' ': // move right
3253 case 'l': // move right
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003254 case KEYCODE_RIGHT: // Cursor Key Right
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003255 do {
3256 dot_right();
3257 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003258 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003259#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003260 case '"': // "- name a register to use for Delete/Yank
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003261 c1 = (get_one_char() | 0x20) - 'a'; // | 0x20 is tolower()
3262 if ((unsigned)c1 <= 25) { // a-z?
3263 YDreg = c1;
Eric Andersen3f980402001-04-04 17:31:15 +00003264 } else {
3265 indicate_error(c);
3266 }
3267 break;
3268 case '\'': // '- goto a specific mark
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003269 c1 = (get_one_char() | 0x20) - 'a';
3270 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003271 // get the b-o-l
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003272 q = mark[c1];
Eric Andersen3f980402001-04-04 17:31:15 +00003273 if (text <= q && q < end) {
3274 dot = q;
3275 dot_begin(); // go to B-o-l
3276 dot_skip_over_ws();
3277 }
3278 } else if (c1 == '\'') { // goto previous context
3279 dot = swap_context(dot); // swap current and previous context
3280 dot_begin(); // go to B-o-l
3281 dot_skip_over_ws();
3282 } else {
3283 indicate_error(c);
3284 }
3285 break;
3286 case 'm': // m- Mark a line
3287 // this is really stupid. If there are any inserts or deletes
3288 // between text[0] and dot then this mark will not point to the
3289 // correct location! It could be off by many lines!
3290 // Well..., at least its quick and dirty.
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003291 c1 = (get_one_char() | 0x20) - 'a';
3292 if ((unsigned)c1 <= 25) { // a-z?
Eric Andersen3f980402001-04-04 17:31:15 +00003293 // remember the line
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003294 mark[c1] = dot;
Eric Andersen3f980402001-04-04 17:31:15 +00003295 } else {
3296 indicate_error(c);
3297 }
3298 break;
3299 case 'P': // P- Put register before
3300 case 'p': // p- put register after
3301 p = reg[YDreg];
Denis Vlasenko00d84172008-11-24 07:34:42 +00003302 if (p == NULL) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003303 status_line_bold("Nothing in register %c", what_reg());
Eric Andersen3f980402001-04-04 17:31:15 +00003304 break;
3305 }
3306 // are we putting whole lines or strings
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003307 if (strchr(p, '\n') != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003308 if (c == 'P') {
3309 dot_begin(); // putting lines- Put above
3310 }
3311 if (c == 'p') {
3312 // are we putting after very last line?
3313 if (end_line(dot) == (end - 1)) {
3314 dot = end; // force dot to end of text[]
3315 } else {
3316 dot_next(); // next line, then put before
3317 }
3318 }
3319 } else {
3320 if (c == 'p')
3321 dot_right(); // move to right, can move to NL
3322 }
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003323 string_insert(dot, p); // insert the string
Eric Andersen3f980402001-04-04 17:31:15 +00003324 end_cmd_q(); // stop adding to q
3325 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003326 case 'U': // U- Undo; replace current line with original version
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003327 if (reg[Ureg] != NULL) {
Eric Andersen3f980402001-04-04 17:31:15 +00003328 p = begin_line(dot);
3329 q = end_line(dot);
3330 p = text_hole_delete(p, q); // delete cur line
Denis Vlasenko4ae1e132008-11-19 13:25:14 +00003331 p += string_insert(p, reg[Ureg]); // insert orig line
Eric Andersen3f980402001-04-04 17:31:15 +00003332 dot = p;
3333 dot_skip_over_ws();
3334 }
3335 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003336#endif /* FEATURE_VI_YANKMARK */
Eric Andersen3f980402001-04-04 17:31:15 +00003337 case '$': // $- goto end of line
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003338 case KEYCODE_END: // Cursor Key End
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003339 for (;;) {
3340 dot = end_line(dot);
3341 if (--cmdcnt <= 0)
3342 break;
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003343 dot_next();
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003344 }
Eric Andersen3f980402001-04-04 17:31:15 +00003345 break;
3346 case '%': // %- find matching char of pair () [] {}
3347 for (q = dot; q < end && *q != '\n'; q++) {
3348 if (strchr("()[]{}", *q) != NULL) {
3349 // we found half of a pair
3350 p = find_pair(q, *q);
3351 if (p == NULL) {
3352 indicate_error(c);
3353 } else {
3354 dot = p;
3355 }
3356 break;
3357 }
3358 }
3359 if (*q == '\n')
3360 indicate_error(c);
3361 break;
3362 case 'f': // f- forward to a user specified char
3363 last_forward_char = get_one_char(); // get the search char
3364 //
Eric Andersenaff114c2004-04-14 17:51:38 +00003365 // dont separate these two commands. 'f' depends on ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003366 //
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003367 //**** fall through to ... ';'
Eric Andersen3f980402001-04-04 17:31:15 +00003368 case ';': // ;- look at rest of line for last forward char
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003369 do {
3370 if (last_forward_char == 0)
3371 break;
3372 q = dot + 1;
3373 while (q < end - 1 && *q != '\n' && *q != last_forward_char) {
3374 q++;
3375 }
3376 if (*q == last_forward_char)
3377 dot = q;
3378 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003379 break;
Paul Foxb5ee8db2008-02-14 01:17:01 +00003380 case ',': // repeat latest 'f' in opposite direction
Paul Foxb5ee8db2008-02-14 01:17:01 +00003381 if (last_forward_char == 0)
3382 break;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003383 do {
3384 q = dot - 1;
3385 while (q >= text && *q != '\n' && *q != last_forward_char) {
3386 q--;
3387 }
3388 if (q >= text && *q == last_forward_char)
3389 dot = q;
3390 } while (--cmdcnt > 0);
Paul Foxb5ee8db2008-02-14 01:17:01 +00003391 break;
3392
Eric Andersen3f980402001-04-04 17:31:15 +00003393 case '-': // -- goto prev line
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003394 do {
3395 dot_prev();
3396 dot_skip_over_ws();
3397 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003398 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003399#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003400 case '.': // .- repeat the last modifying command
3401 // Stuff the last_modifying_cmd back into stdin
3402 // and let it be re-executed.
Denis Vlasenkob1759462008-06-20 20:20:54 +00003403 if (lmc_len > 0) {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003404 last_modifying_cmd[lmc_len] = 0;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003405 ioq = ioq_start = xstrdup(last_modifying_cmd);
Eric Andersen3f980402001-04-04 17:31:15 +00003406 }
3407 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003408#endif
3409#if ENABLE_FEATURE_VI_SEARCH
Eric Andersen3f980402001-04-04 17:31:15 +00003410 case '?': // /- search for a pattern
3411 case '/': // /- search for a pattern
3412 buf[0] = c;
3413 buf[1] = '\0';
3414 q = get_input_line(buf); // get input line- use "status line"
Paul Fox4917c112008-03-05 16:44:02 +00003415 if (q[0] && !q[1]) {
3416 if (last_search_pattern[0])
Denis Vlasenkoc3a9dc82008-10-29 00:58:04 +00003417 last_search_pattern[0] = c;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003418 goto dc3; // if no pat re-use old pat
Paul Fox4917c112008-03-05 16:44:02 +00003419 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003420 if (q[0]) { // strlen(q) > 1: new pat- save it and find
Eric Andersen3f980402001-04-04 17:31:15 +00003421 // there is a new pat
Aaron Lehmanna170e1c2002-11-28 11:27:31 +00003422 free(last_search_pattern);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003423 last_search_pattern = xstrdup(q);
Eric Andersen3f980402001-04-04 17:31:15 +00003424 goto dc3; // now find the pattern
3425 }
3426 // user changed mind and erased the "/"- do nothing
3427 break;
3428 case 'N': // N- backward search for last pattern
Eric Andersen3f980402001-04-04 17:31:15 +00003429 dir = BACK; // assume BACKWARD search
3430 p = dot - 1;
3431 if (last_search_pattern[0] == '?') {
3432 dir = FORWARD;
3433 p = dot + 1;
3434 }
3435 goto dc4; // now search for pattern
3436 break;
3437 case 'n': // n- repeat search for last pattern
3438 // search rest of text[] starting at next char
3439 // if search fails return orignal "p" not the "p+1" address
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003440 do {
3441 const char *msg;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003442 dc3:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003443 dir = FORWARD; // assume FORWARD search
3444 p = dot + 1;
3445 if (last_search_pattern[0] == '?') {
3446 dir = BACK;
3447 p = dot - 1;
Eric Andersen3f980402001-04-04 17:31:15 +00003448 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003449 dc4:
3450 q = char_search(p, last_search_pattern + 1, dir, FULL);
3451 if (q != NULL) {
3452 dot = q; // good search, update "dot"
3453 msg = NULL;
3454 goto dc2;
3455 }
3456 // no pattern found between "dot" and "end"- continue at top
3457 p = text;
3458 if (dir == BACK) {
3459 p = end - 1;
3460 }
3461 q = char_search(p, last_search_pattern + 1, dir, FULL);
3462 if (q != NULL) { // found something
3463 dot = q; // found new pattern- goto it
3464 msg = "search hit BOTTOM, continuing at TOP";
3465 if (dir == BACK) {
3466 msg = "search hit TOP, continuing at BOTTOM";
3467 }
3468 } else {
3469 msg = "Pattern not found";
3470 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003471 dc2:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003472 if (msg)
3473 status_line_bold("%s", msg);
3474 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003475 break;
3476 case '{': // {- move backward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003477 q = char_search(dot, "\n\n", BACK, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003478 if (q != NULL) { // found blank line
3479 dot = next_line(q); // move to next blank line
3480 }
3481 break;
3482 case '}': // }- move forward paragraph
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003483 q = char_search(dot, "\n\n", FORWARD, FULL);
Eric Andersen3f980402001-04-04 17:31:15 +00003484 if (q != NULL) { // found blank line
3485 dot = next_line(q); // move to next blank line
3486 }
3487 break;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003488#endif /* FEATURE_VI_SEARCH */
Eric Andersen3f980402001-04-04 17:31:15 +00003489 case '0': // 0- goto begining of line
Eric Andersenc7bda1c2004-03-15 08:29:22 +00003490 case '1': // 1-
3491 case '2': // 2-
3492 case '3': // 3-
3493 case '4': // 4-
3494 case '5': // 5-
3495 case '6': // 6-
3496 case '7': // 7-
3497 case '8': // 8-
3498 case '9': // 9-
Eric Andersen3f980402001-04-04 17:31:15 +00003499 if (c == '0' && cmdcnt < 1) {
3500 dot_begin(); // this was a standalone zero
3501 } else {
3502 cmdcnt = cmdcnt * 10 + (c - '0'); // this 0 is part of a number
3503 }
3504 break;
3505 case ':': // :- the colon mode commands
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003506 p = get_input_line(":"); // get input line- use "status line"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003507#if ENABLE_FEATURE_VI_COLON
Eric Andersen3f980402001-04-04 17:31:15 +00003508 colon(p); // execute the command
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003509#else
Eric Andersen822c3832001-05-07 17:37:43 +00003510 if (*p == ':')
3511 p++; // move past the ':'
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003512 cnt = strlen(p);
Eric Andersen822c3832001-05-07 17:37:43 +00003513 if (cnt <= 0)
3514 break;
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003515 if (strncmp(p, "quit", cnt) == 0
3516 || strncmp(p, "q!", cnt) == 0 // delete lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003517 ) {
Matt Kraai1f0c4362001-12-20 23:13:26 +00003518 if (file_modified && p[1] != '!') {
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003519 status_line_bold("No write since last change (:%s! overrides)", p);
Eric Andersen3f980402001-04-04 17:31:15 +00003520 } else {
3521 editing = 0;
3522 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003523 } else if (strncmp(p, "write", cnt) == 0
3524 || strncmp(p, "wq", cnt) == 0
3525 || strncmp(p, "wn", cnt) == 0
3526 || (p[0] == 'x' && !p[1])
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003527 ) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003528 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003529 if (cnt < 0) {
3530 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003531 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003532 } else {
3533 file_modified = 0;
3534 last_file_modified = -1;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003535 status_line("\"%s\" %dL, %dC", current_filename, count_lines(text, end - 1), cnt);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003536 if (p[0] == 'x' || p[1] == 'q' || p[1] == 'n'
3537 || p[0] == 'X' || p[1] == 'Q' || p[1] == 'N'
3538 ) {
Paul Fox61e45db2005-10-09 14:43:22 +00003539 editing = 0;
3540 }
Eric Andersen3f980402001-04-04 17:31:15 +00003541 }
Denys Vlasenko6548edd2009-06-15 12:44:11 +02003542 } else if (strncmp(p, "file", cnt) == 0) {
Paul Fox8552aec2005-09-16 12:20:05 +00003543 last_status_cksum = 0; // force status update
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003544 } else if (sscanf(p, "%d", &j) > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003545 dot = find_line(j); // go to line # j
3546 dot_skip_over_ws();
Denys Vlasenkob22bbff2009-07-04 16:50:43 +02003547 } else { // unrecognized cmd
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003548 not_implemented(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003549 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003550#endif /* !FEATURE_VI_COLON */
Eric Andersen3f980402001-04-04 17:31:15 +00003551 break;
3552 case '<': // <- Left shift something
3553 case '>': // >- Right shift something
3554 cnt = count_lines(text, dot); // remember what line we are on
3555 c1 = get_one_char(); // get the type of thing to delete
3556 find_range(&p, &q, c1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003557 yank_delete(p, q, 1, YANKONLY); // save copy before change
Eric Andersen3f980402001-04-04 17:31:15 +00003558 p = begin_line(p);
3559 q = end_line(q);
3560 i = count_lines(p, q); // # of lines we are shifting
3561 for ( ; i > 0; i--, p = next_line(p)) {
3562 if (c == '<') {
3563 // shift left- remove tab or 8 spaces
3564 if (*p == '\t') {
3565 // shrink buffer 1 char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003566 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003567 } else if (*p == ' ') {
3568 // we should be calculating columns, not just SPACE
3569 for (j = 0; *p == ' ' && j < tabstop; j++) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003570 text_hole_delete(p, p);
Eric Andersen3f980402001-04-04 17:31:15 +00003571 }
3572 }
3573 } else if (c == '>') {
3574 // shift right -- add tab or 8 spaces
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003575 char_insert(p, '\t');
Eric Andersen3f980402001-04-04 17:31:15 +00003576 }
3577 }
3578 dot = find_line(cnt); // what line were we on
3579 dot_skip_over_ws();
3580 end_cmd_q(); // stop adding to q
3581 break;
3582 case 'A': // A- append at e-o-l
3583 dot_end(); // go to e-o-l
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003584 //**** fall through to ... 'a'
Eric Andersen3f980402001-04-04 17:31:15 +00003585 case 'a': // a- append after current char
3586 if (*dot != '\n')
3587 dot++;
3588 goto dc_i;
3589 break;
3590 case 'B': // B- back a blank-delimited Word
3591 case 'E': // E- end of a blank-delimited word
3592 case 'W': // W- forward a blank-delimited word
Eric Andersen3f980402001-04-04 17:31:15 +00003593 dir = FORWARD;
3594 if (c == 'B')
3595 dir = BACK;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003596 do {
3597 if (c == 'W' || isspace(dot[dir])) {
3598 dot = skip_thing(dot, 1, dir, S_TO_WS);
3599 dot = skip_thing(dot, 2, dir, S_OVER_WS);
3600 }
3601 if (c != 'W')
3602 dot = skip_thing(dot, 1, dir, S_BEFORE_WS);
3603 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003604 break;
3605 case 'C': // C- Change to e-o-l
3606 case 'D': // D- delete to e-o-l
3607 save_dot = dot;
3608 dot = dollar_line(dot); // move to before NL
3609 // copy text into a register and delete
3610 dot = yank_delete(save_dot, dot, 0, YANKDEL); // delete to e-o-l
3611 if (c == 'C')
3612 goto dc_i; // start inserting
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003613#if ENABLE_FEATURE_VI_DOT_CMD
Eric Andersen3f980402001-04-04 17:31:15 +00003614 if (c == 'D')
3615 end_cmd_q(); // stop adding to q
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003616#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003617 break;
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003618 case 'g': // 'gg' goto a line number (vim) (default: very first line)
Paul Foxb5ee8db2008-02-14 01:17:01 +00003619 c1 = get_one_char();
3620 if (c1 != 'g') {
3621 buf[0] = 'g';
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003622 buf[1] = c1; // TODO: if Unicode?
Paul Foxb5ee8db2008-02-14 01:17:01 +00003623 buf[2] = '\0';
3624 not_implemented(buf);
3625 break;
3626 }
3627 if (cmdcnt == 0)
3628 cmdcnt = 1;
3629 /* fall through */
Eric Andersen822c3832001-05-07 17:37:43 +00003630 case 'G': // G- goto to a line number (default= E-O-F)
3631 dot = end - 1; // assume E-O-F
Eric Andersen1c0d3112001-04-16 15:46:44 +00003632 if (cmdcnt > 0) {
Eric Andersen822c3832001-05-07 17:37:43 +00003633 dot = find_line(cmdcnt); // what line is #cmdcnt
Eric Andersen1c0d3112001-04-16 15:46:44 +00003634 }
3635 dot_skip_over_ws();
3636 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003637 case 'H': // H- goto top line on screen
3638 dot = screenbegin;
3639 if (cmdcnt > (rows - 1)) {
3640 cmdcnt = (rows - 1);
3641 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003642 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003643 do_cmd('+');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003644 }
Eric Andersen3f980402001-04-04 17:31:15 +00003645 dot_skip_over_ws();
3646 break;
3647 case 'I': // I- insert before first non-blank
3648 dot_begin(); // 0
3649 dot_skip_over_ws();
Bernhard Reutner-Fischera985d302008-02-11 11:44:38 +00003650 //**** fall through to ... 'i'
Eric Andersen3f980402001-04-04 17:31:15 +00003651 case 'i': // i- insert before current char
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003652 case KEYCODE_INSERT: // Cursor Key Insert
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003653 dc_i:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003654 cmd_mode = 1; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003655 break;
3656 case 'J': // J- join current and next lines together
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003657 do {
3658 dot_end(); // move to NL
3659 if (dot < end - 1) { // make sure not last char in text[]
3660 *dot++ = ' '; // replace NL with space
3661 file_modified++;
3662 while (isblank(*dot)) { // delete leading WS
3663 dot_delete();
3664 }
Eric Andersen3f980402001-04-04 17:31:15 +00003665 }
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003666 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003667 end_cmd_q(); // stop adding to q
3668 break;
3669 case 'L': // L- goto bottom line on screen
3670 dot = end_screen();
3671 if (cmdcnt > (rows - 1)) {
3672 cmdcnt = (rows - 1);
3673 }
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003674 if (--cmdcnt > 0) {
Eric Andersen3f980402001-04-04 17:31:15 +00003675 do_cmd('-');
Denys Vlasenko35fdb1b2010-03-26 16:10:14 +01003676 }
Eric Andersen3f980402001-04-04 17:31:15 +00003677 dot_begin();
3678 dot_skip_over_ws();
3679 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003680 case 'M': // M- goto middle line on screen
Eric Andersen1c0d3112001-04-16 15:46:44 +00003681 dot = screenbegin;
3682 for (cnt = 0; cnt < (rows-1) / 2; cnt++)
3683 dot = next_line(dot);
3684 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003685 case 'O': // O- open a empty line above
Eric Andersen822c3832001-05-07 17:37:43 +00003686 // 0i\n ESC -i
Eric Andersen3f980402001-04-04 17:31:15 +00003687 p = begin_line(dot);
3688 if (p[-1] == '\n') {
3689 dot_prev();
3690 case 'o': // o- open a empty line below; Yes, I know it is in the middle of the "if (..."
3691 dot_end();
3692 dot = char_insert(dot, '\n');
3693 } else {
3694 dot_begin(); // 0
Eric Andersen822c3832001-05-07 17:37:43 +00003695 dot = char_insert(dot, '\n'); // i\n ESC
Eric Andersen3f980402001-04-04 17:31:15 +00003696 dot_prev(); // -
3697 }
3698 goto dc_i;
3699 break;
3700 case 'R': // R- continuous Replace char
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003701 dc5:
Eric Andersen3f980402001-04-04 17:31:15 +00003702 cmd_mode = 2;
Eric Andersen3f980402001-04-04 17:31:15 +00003703 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003704 case KEYCODE_DELETE:
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003705 c = 'x';
3706 // fall through
Eric Andersen3f980402001-04-04 17:31:15 +00003707 case 'X': // X- delete char before dot
3708 case 'x': // x- delete the current char
3709 case 's': // s- substitute the current char
Eric Andersen3f980402001-04-04 17:31:15 +00003710 dir = 0;
3711 if (c == 'X')
3712 dir = -1;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003713 do {
3714 if (dot[dir] != '\n') {
3715 if (c == 'X')
3716 dot--; // delete prev char
3717 dot = yank_delete(dot, dot, 0, YANKDEL); // delete char
3718 }
3719 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003720 end_cmd_q(); // stop adding to q
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003721 if (c == 's')
3722 goto dc_i; // start inserting
Eric Andersen3f980402001-04-04 17:31:15 +00003723 break;
3724 case 'Z': // Z- if modified, {write}; exit
3725 // ZZ means to save file (if necessary), then exit
3726 c1 = get_one_char();
3727 if (c1 != 'Z') {
3728 indicate_error(c);
3729 break;
3730 }
Paul Foxf0305b72006-03-28 14:18:21 +00003731 if (file_modified) {
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003732 if (ENABLE_FEATURE_VI_READONLY && readonly_mode) {
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003733 status_line_bold("\"%s\" File is read only", current_filename);
Denis Vlasenko92758142006-10-03 19:56:34 +00003734 break;
Paul Foxf0305b72006-03-28 14:18:21 +00003735 }
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003736 cnt = file_write(current_filename, text, end - 1);
Paul Fox61e45db2005-10-09 14:43:22 +00003737 if (cnt < 0) {
3738 if (cnt == -1)
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003739 status_line_bold("Write error: %s", strerror(errno));
Paul Fox61e45db2005-10-09 14:43:22 +00003740 } else if (cnt == (end - 1 - text + 1)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003741 editing = 0;
3742 }
3743 } else {
3744 editing = 0;
3745 }
3746 break;
3747 case '^': // ^- move to first non-blank on line
3748 dot_begin();
3749 dot_skip_over_ws();
3750 break;
3751 case 'b': // b- back a word
3752 case 'e': // e- end of word
Eric Andersen3f980402001-04-04 17:31:15 +00003753 dir = FORWARD;
3754 if (c == 'b')
3755 dir = BACK;
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003756 do {
3757 if ((dot + dir) < text || (dot + dir) > end - 1)
3758 break;
3759 dot += dir;
3760 if (isspace(*dot)) {
3761 dot = skip_thing(dot, (c == 'e') ? 2 : 1, dir, S_OVER_WS);
3762 }
3763 if (isalnum(*dot) || *dot == '_') {
3764 dot = skip_thing(dot, 1, dir, S_END_ALNUM);
3765 } else if (ispunct(*dot)) {
3766 dot = skip_thing(dot, 1, dir, S_END_PUNCT);
3767 }
3768 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003769 break;
3770 case 'c': // c- change something
3771 case 'd': // d- delete something
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003772#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003773 case 'y': // y- yank something
3774 case 'Y': // Y- Yank a line
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003775#endif
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003776 {
Paul Foxc51fc7b2008-03-06 01:34:23 +00003777 int yf, ml, whole = 0;
Eric Andersen3f980402001-04-04 17:31:15 +00003778 yf = YANKDEL; // assume either "c" or "d"
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003779#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003780 if (c == 'y' || c == 'Y')
3781 yf = YANKONLY;
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003782#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003783 c1 = 'y';
3784 if (c != 'Y')
3785 c1 = get_one_char(); // get the type of thing to delete
Paul Foxc51fc7b2008-03-06 01:34:23 +00003786 // determine range, and whether it spans lines
3787 ml = find_range(&p, &q, c1);
Eric Andersen3f980402001-04-04 17:31:15 +00003788 if (c1 == 27) { // ESC- user changed mind and wants out
3789 c = c1 = 27; // Escape- do nothing
3790 } else if (strchr("wW", c1)) {
3791 if (c == 'c') {
3792 // don't include trailing WS as part of word
Denis Vlasenkoeaabf062007-07-17 23:14:07 +00003793 while (isblank(*q)) {
Eric Andersen3f980402001-04-04 17:31:15 +00003794 if (q <= text || q[-1] == '\n')
3795 break;
3796 q--;
3797 }
3798 }
Paul Foxc51fc7b2008-03-06 01:34:23 +00003799 dot = yank_delete(p, q, ml, yf); // delete word
3800 } else if (strchr("^0bBeEft%$ lh\b\177", c1)) {
3801 // partial line copy text into a register and delete
3802 dot = yank_delete(p, q, ml, yf); // delete word
3803 } else if (strchr("cdykjHL+-{}\r\n", c1)) {
3804 // whole line copy text into a register and delete
3805 dot = yank_delete(p, q, ml, yf); // delete lines
3806 whole = 1;
3807 } else {
3808 // could not recognize object
3809 c = c1 = 27; // error-
3810 ml = 0;
3811 indicate_error(c);
3812 }
3813 if (ml && whole) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003814 if (c == 'c') {
3815 dot = char_insert(dot, '\n');
3816 // on the last line of file don't move to prev line
Paul Foxc51fc7b2008-03-06 01:34:23 +00003817 if (whole && dot != (end-1)) {
Eric Andersen1c0d3112001-04-16 15:46:44 +00003818 dot_prev();
3819 }
3820 } else if (c == 'd') {
Eric Andersen3f980402001-04-04 17:31:15 +00003821 dot_begin();
3822 dot_skip_over_ws();
3823 }
Eric Andersen3f980402001-04-04 17:31:15 +00003824 }
3825 if (c1 != 27) {
3826 // if CHANGING, not deleting, start inserting after the delete
3827 if (c == 'c') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003828 strcpy(buf, "Change");
Eric Andersen3f980402001-04-04 17:31:15 +00003829 goto dc_i; // start inserting
3830 }
3831 if (c == 'd') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003832 strcpy(buf, "Delete");
Eric Andersen3f980402001-04-04 17:31:15 +00003833 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003834#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003835 if (c == 'y' || c == 'Y') {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003836 strcpy(buf, "Yank");
Eric Andersen3f980402001-04-04 17:31:15 +00003837 }
3838 p = reg[YDreg];
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003839 q = p + strlen(p);
Eric Andersen3f980402001-04-04 17:31:15 +00003840 for (cnt = 0; p <= q; p++) {
3841 if (*p == '\n')
3842 cnt++;
3843 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003844 status_line("%s %d lines (%d chars) using [%c]",
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003845 buf, cnt, strlen(reg[YDreg]), what_reg());
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003846#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003847 end_cmd_q(); // stop adding to q
3848 }
3849 break;
Denis Vlasenkod44c1532008-10-14 12:59:42 +00003850 }
Eric Andersen3f980402001-04-04 17:31:15 +00003851 case 'k': // k- goto prev line, same col
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003852 case KEYCODE_UP: // cursor key Up
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003853 do {
3854 dot_prev();
3855 dot = move_to_col(dot, ccol + offset); // try stay in same col
3856 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003857 break;
3858 case 'r': // r- replace the current char with user input
3859 c1 = get_one_char(); // get the replacement char
3860 if (*dot != '\n') {
3861 *dot = c1;
Denis Vlasenkob1759462008-06-20 20:20:54 +00003862 file_modified++;
Eric Andersen3f980402001-04-04 17:31:15 +00003863 }
3864 end_cmd_q(); // stop adding to q
3865 break;
Eric Andersen822c3832001-05-07 17:37:43 +00003866 case 't': // t- move to char prior to next x
Tim Rikerc1ef7bd2006-01-25 00:08:53 +00003867 last_forward_char = get_one_char();
3868 do_cmd(';');
3869 if (*dot == last_forward_char)
3870 dot_left();
Denis Vlasenko4c9e9c42008-10-20 08:59:03 +00003871 last_forward_char = 0;
Eric Andersen822c3832001-05-07 17:37:43 +00003872 break;
Eric Andersen3f980402001-04-04 17:31:15 +00003873 case 'w': // w- forward a word
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003874 do {
3875 if (isalnum(*dot) || *dot == '_') { // we are on ALNUM
3876 dot = skip_thing(dot, 1, FORWARD, S_END_ALNUM);
3877 } else if (ispunct(*dot)) { // we are on PUNCT
3878 dot = skip_thing(dot, 1, FORWARD, S_END_PUNCT);
3879 }
3880 if (dot < end - 1)
3881 dot++; // move over word
3882 if (isspace(*dot)) {
3883 dot = skip_thing(dot, 2, FORWARD, S_OVER_WS);
3884 }
3885 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003886 break;
3887 case 'z': // z-
3888 c1 = get_one_char(); // get the replacement char
3889 cnt = 0;
3890 if (c1 == '.')
3891 cnt = (rows - 2) / 2; // put dot at center
3892 if (c1 == '-')
3893 cnt = rows - 2; // put dot at bottom
3894 screenbegin = begin_line(dot); // start dot at top
3895 dot_scroll(cnt, -1);
3896 break;
3897 case '|': // |- move to column "cmdcnt"
3898 dot = move_to_col(dot, cmdcnt - 1); // try to move to column
3899 break;
3900 case '~': // ~- flip the case of letters a-z -> A-Z
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +02003901 do {
3902 if (islower(*dot)) {
3903 *dot = toupper(*dot);
3904 file_modified++;
3905 } else if (isupper(*dot)) {
3906 *dot = tolower(*dot);
3907 file_modified++;
3908 }
3909 dot_right();
3910 } while (--cmdcnt > 0);
Eric Andersen3f980402001-04-04 17:31:15 +00003911 end_cmd_q(); // stop adding to q
3912 break;
3913 //----- The Cursor and Function Keys -----------------------------
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003914 case KEYCODE_HOME: // Cursor Key Home
Eric Andersen3f980402001-04-04 17:31:15 +00003915 dot_begin();
3916 break;
3917 // The Fn keys could point to do_macro which could translate them
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003918#if 0
3919 case KEYCODE_FUN1: // Function Key F1
3920 case KEYCODE_FUN2: // Function Key F2
3921 case KEYCODE_FUN3: // Function Key F3
3922 case KEYCODE_FUN4: // Function Key F4
3923 case KEYCODE_FUN5: // Function Key F5
3924 case KEYCODE_FUN6: // Function Key F6
3925 case KEYCODE_FUN7: // Function Key F7
3926 case KEYCODE_FUN8: // Function Key F8
3927 case KEYCODE_FUN9: // Function Key F9
3928 case KEYCODE_FUN10: // Function Key F10
3929 case KEYCODE_FUN11: // Function Key F11
3930 case KEYCODE_FUN12: // Function Key F12
Eric Andersen3f980402001-04-04 17:31:15 +00003931 break;
Denis Vlasenko0112ff52008-10-25 23:23:00 +00003932#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003933 }
3934
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003935 dc1:
Eric Andersen3f980402001-04-04 17:31:15 +00003936 // if text[] just became empty, add back an empty line
3937 if (end == text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003938 char_insert(text, '\n'); // start empty buf with dummy line
Eric Andersen3f980402001-04-04 17:31:15 +00003939 dot = text;
3940 }
3941 // it is OK for dot to exactly equal to end, otherwise check dot validity
3942 if (dot != end) {
3943 dot = bound_dot(dot); // make sure "dot" is valid
3944 }
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003945#if ENABLE_FEATURE_VI_YANKMARK
Eric Andersen3f980402001-04-04 17:31:15 +00003946 check_context(c); // update the current context
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003947#endif
Eric Andersen3f980402001-04-04 17:31:15 +00003948
3949 if (!isdigit(c))
3950 cmdcnt = 0; // cmd was not a number, reset cmdcnt
3951 cnt = dot - begin_line(dot);
3952 // Try to stay off of the Newline
3953 if (*dot == '\n' && cnt > 0 && cmd_mode == 0)
3954 dot--;
3955}
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003956
Paul Fox35e9c5d2008-03-06 16:26:12 +00003957/* NB! the CRASHME code is unmaintained, and doesn't currently build */
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00003958#if ENABLE_FEATURE_VI_CRASHME
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003959static int totalcmds = 0;
3960static int Mp = 85; // Movement command Probability
3961static int Np = 90; // Non-movement command Probability
3962static int Dp = 96; // Delete command Probability
3963static int Ip = 97; // Insert command Probability
3964static int Yp = 98; // Yank command Probability
3965static int Pp = 99; // Put command Probability
3966static int M = 0, N = 0, I = 0, D = 0, Y = 0, P = 0, U = 0;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003967static const char chars[20] = "\t012345 abcdABCD-=.$";
3968static const char *const words[20] = {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00003969 "this", "is", "a", "test",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003970 "broadcast", "the", "emergency", "of",
3971 "system", "quick", "brown", "fox",
3972 "jumped", "over", "lazy", "dogs",
3973 "back", "January", "Febuary", "March"
3974};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003975static const char *const lines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003976 "You should have received a copy of the GNU General Public License\n",
3977 "char c, cm, *cmd, *cmd1;\n",
3978 "generate a command by percentages\n",
3979 "Numbers may be typed as a prefix to some commands.\n",
3980 "Quit, discarding changes!\n",
3981 "Forced write, if permission originally not valid.\n",
3982 "In general, any ex or ed command (such as substitute or delete).\n",
3983 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
3984 "Please get w/ me and I will go over it with you.\n",
3985 "The following is a list of scheduled, committed changes.\n",
3986 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
3987 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
3988 "Any question about transactions please contact Sterling Huxley.\n",
3989 "I will try to get back to you by Friday, December 31.\n",
3990 "This Change will be implemented on Friday.\n",
3991 "Let me know if you have problems accessing this;\n",
3992 "Sterling Huxley recently added you to the access list.\n",
3993 "Would you like to go to lunch?\n",
3994 "The last command will be automatically run.\n",
3995 "This is too much english for a computer geek.\n",
3996};
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00003997static char *multilines[20] = {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00003998 "You should have received a copy of the GNU General Public License\n",
3999 "char c, cm, *cmd, *cmd1;\n",
4000 "generate a command by percentages\n",
4001 "Numbers may be typed as a prefix to some commands.\n",
4002 "Quit, discarding changes!\n",
4003 "Forced write, if permission originally not valid.\n",
4004 "In general, any ex or ed command (such as substitute or delete).\n",
4005 "I have tickets available for the Blazers vs LA Clippers for Monday, Janurary 1 at 1:00pm.\n",
4006 "Please get w/ me and I will go over it with you.\n",
4007 "The following is a list of scheduled, committed changes.\n",
4008 "1. Launch Norton Antivirus (Start, Programs, Norton Antivirus)\n",
4009 "Reminder....Town Meeting in Central Perk cafe today at 3:00pm.\n",
4010 "Any question about transactions please contact Sterling Huxley.\n",
4011 "I will try to get back to you by Friday, December 31.\n",
4012 "This Change will be implemented on Friday.\n",
4013 "Let me know if you have problems accessing this;\n",
4014 "Sterling Huxley recently added you to the access list.\n",
4015 "Would you like to go to lunch?\n",
4016 "The last command will be automatically run.\n",
4017 "This is too much english for a computer geek.\n",
4018};
4019
4020// create a random command to execute
4021static void crash_dummy()
4022{
4023 static int sleeptime; // how long to pause between commands
4024 char c, cm, *cmd, *cmd1;
4025 int i, cnt, thing, rbi, startrbi, percent;
4026
4027 // "dot" movement commands
4028 cmd1 = " \n\r\002\004\005\006\025\0310^$-+wWeEbBhjklHL";
4029
4030 // is there already a command running?
Denys Vlasenko020f4062009-05-17 16:44:54 +02004031 if (readbuffer[0] > 0)
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004032 goto cd1;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004033 cd0:
Denys Vlasenko020f4062009-05-17 16:44:54 +02004034 readbuffer[0] = 'X';
4035 startrbi = rbi = 1;
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004036 sleeptime = 0; // how long to pause between commands
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004037 memset(readbuffer, '\0', sizeof(readbuffer));
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004038 // generate a command by percentages
4039 percent = (int) lrand48() % 100; // get a number from 0-99
4040 if (percent < Mp) { // Movement commands
4041 // available commands
4042 cmd = cmd1;
4043 M++;
4044 } else if (percent < Np) { // non-movement commands
4045 cmd = "mz<>\'\""; // available commands
4046 N++;
4047 } else if (percent < Dp) { // Delete commands
4048 cmd = "dx"; // available commands
4049 D++;
4050 } else if (percent < Ip) { // Inset commands
4051 cmd = "iIaAsrJ"; // available commands
4052 I++;
4053 } else if (percent < Yp) { // Yank commands
4054 cmd = "yY"; // available commands
4055 Y++;
4056 } else if (percent < Pp) { // Put commands
4057 cmd = "pP"; // available commands
4058 P++;
4059 } else {
4060 // We do not know how to handle this command, try again
4061 U++;
4062 goto cd0;
4063 }
4064 // randomly pick one of the available cmds from "cmd[]"
4065 i = (int) lrand48() % strlen(cmd);
4066 cm = cmd[i];
4067 if (strchr(":\024", cm))
4068 goto cd0; // dont allow colon or ctrl-T commands
4069 readbuffer[rbi++] = cm; // put cmd into input buffer
4070
4071 // now we have the command-
4072 // there are 1, 2, and multi char commands
4073 // find out which and generate the rest of command as necessary
4074 if (strchr("dmryz<>\'\"", cm)) { // 2-char commands
4075 cmd1 = " \n\r0$^-+wWeEbBhjklHL";
4076 if (cm == 'm' || cm == '\'' || cm == '\"') { // pick a reg[]
4077 cmd1 = "abcdefghijklmnopqrstuvwxyz";
4078 }
4079 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4080 c = cmd1[thing];
4081 readbuffer[rbi++] = c; // add movement to input buffer
4082 }
4083 if (strchr("iIaAsc", cm)) { // multi-char commands
4084 if (cm == 'c') {
4085 // change some thing
4086 thing = (int) lrand48() % strlen(cmd1); // pick a movement command
4087 c = cmd1[thing];
4088 readbuffer[rbi++] = c; // add movement to input buffer
4089 }
4090 thing = (int) lrand48() % 4; // what thing to insert
4091 cnt = (int) lrand48() % 10; // how many to insert
4092 for (i = 0; i < cnt; i++) {
4093 if (thing == 0) { // insert chars
4094 readbuffer[rbi++] = chars[((int) lrand48() % strlen(chars))];
4095 } else if (thing == 1) { // insert words
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004096 strcat(readbuffer, words[(int) lrand48() % 20]);
4097 strcat(readbuffer, " ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004098 sleeptime = 0; // how fast to type
4099 } else if (thing == 2) { // insert lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004100 strcat(readbuffer, lines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004101 sleeptime = 0; // how fast to type
4102 } else { // insert multi-lines
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004103 strcat(readbuffer, multilines[(int) lrand48() % 20]);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004104 sleeptime = 0; // how fast to type
4105 }
4106 }
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004107 strcat(readbuffer, "\033");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004108 }
Denys Vlasenko020f4062009-05-17 16:44:54 +02004109 readbuffer[0] = strlen(readbuffer + 1);
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004110 cd1:
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004111 totalcmds++;
4112 if (sleeptime > 0)
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004113 mysleep(sleeptime); // sleep 1/100 sec
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004114}
4115
4116// test to see if there are any errors
4117static void crash_test()
4118{
4119 static time_t oldtim;
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004120
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004121 time_t tim;
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004122 char d[2], msg[80];
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004123
4124 msg[0] = '\0';
4125 if (end < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004126 strcat(msg, "end<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004127 }
4128 if (end > textend) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004129 strcat(msg, "end>textend ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004130 }
4131 if (dot < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004132 strcat(msg, "dot<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004133 }
4134 if (dot > end) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004135 strcat(msg, "dot>end ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004136 }
4137 if (screenbegin < text) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004138 strcat(msg, "screenbegin<text ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004139 }
4140 if (screenbegin > end - 1) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004141 strcat(msg, "screenbegin>end-1 ");
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004142 }
4143
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004144 if (msg[0]) {
Glenn L McGrath7127b582002-12-03 21:48:15 +00004145 printf("\n\n%d: \'%c\' %s\n\n\n%s[Hit return to continue]%s",
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004146 totalcmds, last_input_char, msg, SOs, SOn);
Denys Vlasenko8131eea2009-11-02 14:19:51 +01004147 fflush_all();
Bernhard Reutner-Fischer5e25ddb2008-05-19 09:48:17 +00004148 while (safe_read(STDIN_FILENO, d, 1) > 0) {
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004149 if (d[0] == '\n' || d[0] == '\r')
4150 break;
4151 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004152 }
Denis Vlasenko88adfcd2007-12-22 15:40:13 +00004153 tim = time(NULL);
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004154 if (tim >= (oldtim + 3)) {
Denis Vlasenkoafa37cf2007-03-21 00:05:35 +00004155 sprintf(status_buffer,
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004156 "Tot=%d: M=%d N=%d I=%d D=%d Y=%d P=%d U=%d size=%d",
4157 totalcmds, M, N, I, D, Y, P, U, end - text + 1);
4158 oldtim = tim;
4159 }
Glenn L McGrath09adaca2002-12-02 21:18:10 +00004160}
Denis Vlasenko6a5dc5d2006-12-30 18:42:29 +00004161#endif