rework initial error handling -- since we check for any screens on
[nvi.git] / ex / ex_cmd.c
blob23ab0f169f86808ffa8709809eb85c855c07ee96
1 /*-
2 * Copyright (c) 1992, 1993
3 * The Regents of the University of California. All rights reserved.
5 * %sccs.include.redist.c%
6 */
8 #ifndef lint
9 static char sccsid[] = "$Id: ex_cmd.c,v 8.36 1994/01/22 13:45:44 bostic Exp $ (Berkeley) $Date: 1994/01/22 13:45:44 $";
10 #endif /* not lint */
12 #include <sys/types.h>
14 #include "vi.h"
15 #include "excmd.h"
18 * This array maps ex command names to command functions.
20 * The order in which command names are listed below is important --
21 * ambiguous abbreviations are resolved to be the first possible match,
22 * e.g. "r" means "read", not "rewind", because "read" is listed before
23 * "rewind".
25 * The syntax of the ex commands is unbelievably irregular, and a special
26 * case from beginning to end. Each command has an associated "syntax
27 * script" which describes the "arguments" that are possible. The script
28 * syntax is as follows:
30 * ! -- ! flag
31 * 1 -- flags: [+-]*[pl#][+-]*
32 * 2 -- flags: [-.+^]
33 * 3 -- flags: [-.+^=]
34 * b -- buffer
35 * c[01+a] -- count (0-N, 1-N, signed 1-N, address offset)
36 * f[N#][or] -- file (a number or N, optional or required)
37 * l -- line
38 * S -- string with file name expansion
39 * s -- string
40 * W -- word string
41 * w[N#][or] -- word (a number or N, optional or required)
43 EXCMDLIST const cmds[] = {
44 /* C_BANG */
45 {"!", ex_bang, E_ADDR2_NONE|E_NORC,
46 "S",
47 "[line [,line]] ! command",
48 "filter lines through commands or run commands"},
49 /* C_HASH */
50 {"#", ex_number, E_ADDR2|E_F_PRCLEAR|E_NORC|E_SETLAST,
51 "ca1",
52 "[line [,line]] # [count] [l]",
53 "display numbered lines"},
54 /* C_SUBAGAIN */
55 {"&", ex_subagain, E_ADDR2|E_NORC,
56 "s",
57 "[line [,line]] & [cgr] [count] [#lp]",
58 "repeat the last subsitution"},
59 /* C_STAR */
60 {"*", ex_at, 0,
61 "b",
62 "* [buffer]",
63 "execute a buffer"},
64 /* C_SHIFTL */
65 {"<", ex_shiftl, E_ADDR2|E_AUTOPRINT|E_NORC,
66 "ca1",
67 "[line [,line]] <[<...] [count] [flags]",
68 "shift lines left"},
69 /* C_EQUAL */
70 {"=", ex_equal, E_ADDR1|E_NORC,
71 "1",
72 "[line] = [flags]",
73 "display line number"},
74 /* C_SHIFTR */
75 {">", ex_shiftr, E_ADDR2|E_AUTOPRINT|E_NORC,
76 "ca1",
77 "[line [,line]] >[>...] [count] [flags]",
78 "shift lines right"},
79 /* C_AT */
80 {"@", ex_at, 0,
81 "b",
82 "@ [buffer]",
83 "execute a buffer"},
84 /* C_APPEND */
85 {"append", ex_append, E_ADDR1|E_NORC|E_ZERO|E_ZERODEF,
86 "!",
87 "[line] a[ppend][!]",
88 "append input to a line"},
89 /* C_ABBR */
90 {"abbreviate", ex_abbr, E_NOGLOBAL,
91 "W",
92 "ab[brev] word replace",
93 "specify an input abbreviation"},
94 /* C_ARGS */
95 {"args", ex_args, E_NOGLOBAL|E_NORC,
96 "",
97 "ar[gs]",
98 "display file argument list"},
99 /* C_BG */
100 {"bg", ex_bg, E_NOGLOBAL|E_NORC,
102 "bg",
103 "background the current screen"},
104 /* C_CHANGE */
105 {"change", ex_change, E_ADDR2|E_NORC|E_ZERODEF,
106 "!ca",
107 "[line [,line]] c[hange][!] [count]",
108 "change lines to input"},
109 /* C_CD */
110 {"cd", ex_cd, E_NOGLOBAL,
111 "!f1o",
112 "cd[!] [directory]",
113 "change the current directory"},
114 /* C_CHDIR */
115 {"chdir", ex_cd, E_NOGLOBAL,
116 "!f1o",
117 "chd[ir][!] [directory]",
118 "change the current directory"},
119 /* C_COPY */
120 {"copy", ex_copy, E_ADDR2|E_AUTOPRINT|E_NORC,
121 "l1",
122 "[line [,line]] co[py] line [flags]",
123 "copy lines elsewhere in the file"},
124 /* C_DELETE */
125 {"delete", ex_delete, E_ADDR2|E_AUTOPRINT|E_NORC,
126 "bca1",
127 "[line [,line]] d[elete] [buffer] [count] [flags]",
128 "delete lines from the file"},
129 /* C_DISPLAY */
130 {"display", ex_display, E_NOGLOBAL|E_NORC,
131 "w1r",
132 "display b[uffers] | s[creens] | t[ags]",
133 "display buffers, screens or tags"},
134 /* C_DIGRAPH */
135 {"digraph", ex_digraph, E_NOGLOBAL|E_NOPERM|E_NORC,
136 "",
137 "digraph",
138 "specify digraphs (not implemented)"},
139 /* C_EDIT */
140 {"edit", ex_edit, E_NOGLOBAL|E_NORC,
141 "!f1o",
142 "e[dit][!] [+cmd] [file]",
143 "begin editing another file"},
144 /* C_EX */
145 {"ex", ex_edit, E_NOGLOBAL|E_NORC,
146 "!f1o",
147 "ex[!] [+cmd] [file]",
148 "begin editing another file"},
149 /* C_EXUSAGE */
150 {"exusage", ex_usage, E_NOGLOBAL|E_NORC,
151 "w1o",
152 "[exu]sage [command]",
153 "display ex command usage statement"},
154 /* C_FILE */
155 {"file", ex_file, E_NOGLOBAL|E_NORC,
156 "f1o",
157 "f[ile] [name]",
158 "display (and optionally set) file name"},
159 /* C_FG */
160 {"fg", ex_fg, E_NOGLOBAL|E_NORC,
161 "f1o",
162 "fg [file]",
163 "switch the current screen and a backgrounded screen"},
164 /* C_GLOBAL */
165 {"global", ex_global, E_ADDR2_ALL|E_NOGLOBAL|E_NORC,
166 "!s",
167 "[line [,line]] g[lobal][!] [;/]pattern[;/] [commands]",
168 "execute a global command on lines matching a pattern"},
169 /* C_HELP */
170 {"help", ex_help, E_NOGLOBAL|E_NORC,
172 "he[lp]",
173 "display help statement"},
174 /* C_INSERT */
175 {"insert", ex_insert, E_ADDR1|E_NORC,
176 "!",
177 "[line] i[nsert][!]",
178 "insert input before a line"},
179 /* C_JOIN */
180 {"join", ex_join, E_ADDR2|E_AUTOPRINT|E_NORC,
181 "!ca1",
182 "[line [,line]] j[oin][!] [count] [flags]",
183 "join lines into a single line"},
184 /* C_K */
185 {"k", ex_mark, E_ADDR1|E_NORC,
186 "w1r",
187 "[line] k key",
188 "mark a line position"},
189 /* C_LIST */
190 {"list", ex_list, E_ADDR2|E_F_PRCLEAR|E_NORC|E_SETLAST,
191 "ca1",
192 "[line [,line]] l[ist] [count] [#]",
193 "display lines in an unambiguous form"},
194 /* C_MOVE */
195 {"move", ex_move, E_ADDR2|E_AUTOPRINT|E_NORC,
196 "l",
197 "[line [,line]] m[ove] line",
198 "move lines elsewhere in the file"},
199 /* C_MARK */
200 {"mark", ex_mark, E_ADDR1|E_NORC,
201 "w1r",
202 "[line] ma[rk] key",
203 "mark a line position"},
204 /* C_MAP */
205 {"map", ex_map, 0,
206 "!W",
207 "map[!] [keys replace]",
208 "map input or commands to one or more keys"},
209 /* C_MKEXRC */
210 {"mkexrc", ex_mkexrc, E_NOGLOBAL|E_NORC,
211 "!f1r",
212 "mkexrc[!] file",
213 "write a .exrc file"},
214 /* C_NEXT */
215 {"next", ex_next, E_NOGLOBAL|E_NORC,
216 "!fN",
217 "n[ext][!] [file ...]",
218 "edit (and optionally specify) the next file"},
219 /* C_NUMBER */
220 {"number", ex_number, E_ADDR2|E_F_PRCLEAR|E_NORC|E_SETLAST,
221 "ca1",
222 "[line [,line]] nu[mber] [count] [l]",
223 "change display to number lines"},
224 /* C_OPEN */
225 {"open", ex_open, E_ADDR1,
226 "s",
227 "[line] o[pen] [/pattern/] [flags]",
228 "enter \"open\" mode (not implemented)"},
229 /* C_PRINT */
230 {"print", ex_pr, E_ADDR2|E_F_PRCLEAR|E_NORC|E_SETLAST,
231 "ca1",
232 "[line [,line]] p[rint] [count] [#l]",
233 "display lines"},
234 /* C_PRESERVE */
235 {"preserve", ex_preserve, E_NOGLOBAL|E_NORC,
236 "",
237 "pre[serve]",
238 "preserve an edit session for recovery"},
239 /* C_PREVIOUS */
240 {"previous", ex_prev, E_NOGLOBAL|E_NORC,
241 "!",
242 "prev[ious][!]",
243 "edit the previous file in the file argument list"},
244 /* C_PUT */
245 {"put", ex_put, E_ADDR1|E_AUTOPRINT|E_NORC|E_ZERO,
246 "b",
247 "[line] pu[t] [buffer]",
248 "append a cut buffer to the line"},
249 /* C_QUIT */
250 {"quit", ex_quit, E_NOGLOBAL,
251 "!",
252 "q[uit][!]",
253 "exit ex/vi"},
254 /* C_READ */
255 {"read", ex_read, E_ADDR1|E_NORC|E_ZERO|E_ZERODEF,
256 "!s",
257 "[line] r[ead] [!cmd | [file]]",
258 "append input from a command or file to the line"},
259 /* C_RESIZE */
260 {"resize", ex_resize, E_NOGLOBAL|E_NORC,
261 "c+",
262 "resize [change]",
263 "grow or shrink the current screen"},
264 /* C_REWIND */
265 {"rewind", ex_rew, E_NOGLOBAL|E_NORC,
266 "!",
267 "rew[ind][!]",
268 "re-edit all the files in the file argument list"},
269 /* C_SUBSTITUTE */
270 {"substitute", ex_substitute, E_ADDR2|E_NORC,
271 "s",
272 "[line [,line]] s[ubstitute] [[/;]pat[/;]/repl[/;] [cgr] [count] [#lp]]",
273 "substitute on lines matching a pattern"},
274 /* C_SCRIPT */
275 {"script", ex_script, E_NOGLOBAL|E_NORC,
276 "!f1o",
277 "sc[ript][!] [file]",
278 "run a shell in a screen"},
279 /* C_SET */
280 {"set", ex_set, E_NOGLOBAL,
281 "wN",
282 "se[t] [option[=[value]]...] [nooption ...] [option? ...] [all]",
283 "set options (use \":set all\" to see all options)"},
284 /* C_SHELL */
285 {"shell", ex_shell, E_NOGLOBAL|E_NORC,
286 "",
287 "sh[ell]",
288 "suspend editing and run a shell"},
289 /* C_SOURCE */
290 {"source", ex_source, E_NOGLOBAL,
291 "f1r",
292 "so[urce] file",
293 "read a file of ex commands"},
294 /* C_SPLIT */
295 {"split", ex_split, E_NOGLOBAL|E_NORC,
296 "fNo",
297 "sp[lit] [file ...]",
298 "split the current screen into two screens"},
299 /* C_STOP */
300 {"stop", ex_stop, E_NOGLOBAL|E_NORC,
301 "!",
302 "st[op][!]",
303 "suspend the edit session"},
304 /* C_SUSPEND */
305 {"suspend", ex_stop, E_NOGLOBAL|E_NORC,
306 "!",
307 "su[spend][!]",
308 "suspend the edit session"},
309 /* C_T */
310 {"t", ex_copy, E_ADDR2|E_AUTOPRINT|E_NORC,
311 "l1",
312 "[line [,line]] t line [flags]",
313 "move lines elsewhere in the file"},
314 /* C_TAG */
315 {"tag", ex_tagpush, E_NOGLOBAL,
316 "!w1o",
317 "ta[g][!] [string]",
318 "edit the file containing the tag"},
319 /* C_TAGPOP */
320 {"tagpop", ex_tagpop, E_NOGLOBAL|E_NORC,
321 "!w1o",
322 "tagp[op][!] [number | file]",
323 "return to a previous tag"},
324 /* C_TAGTOP */
325 {"tagtop", ex_tagtop, E_NOGLOBAL|E_NORC,
326 "!",
327 "tagt[op][!]",
328 "return to the first tag"},
329 /* C_UNDOL */
330 {"Undo", ex_undol, E_AUTOPRINT|E_NOGLOBAL|E_NORC,
331 "",
332 "U[ndo]",
333 "undo all the changes to this line"},
334 /* C_UNDO */
335 {"undo", ex_undo, E_AUTOPRINT|E_NOGLOBAL|E_NORC,
336 "",
337 "u[ndo]",
338 "undo the most recent change"},
339 /* C_UNABBREVIATE */
340 {"unabbreviate",ex_unabbr, E_NOGLOBAL,
341 "w1r",
342 "una[bbrev] word",
343 "delete an abbreviation"},
344 /* C_UNMAP */
345 {"unmap", ex_unmap, E_NOGLOBAL,
346 "!w1r",
347 "unm[ap][!] word",
348 "delete an input or command map"},
349 /* C_VGLOBAL */
350 {"vglobal", ex_vglobal, E_ADDR2_ALL|E_NOGLOBAL|E_NORC,
351 "s",
352 "[line [,line]] v[global] [;/]pattern[;/] [commands]",
353 "execute a global command on lines NOT matching a pattern"},
354 /* C_VERSION */
355 {"version", ex_version, E_NOGLOBAL|E_NORC,
356 "",
357 "version",
358 "display the program version information"},
359 /* C_VISUAL_EX */
360 {"visual", ex_visual, E_ADDR1|E_NOGLOBAL|E_NORC|E_ZERODEF,
361 "2c11",
362 "[line] vi[sual] [-|.|+|^] [window_size] [flags]",
363 "enter visual (vi) mode from ex mode"},
364 /* C_VISUAL_VI */
365 {"visual", ex_edit, E_NOGLOBAL|E_NORC,
366 "!f1o",
367 "vi[sual][!] [+cmd] [file]",
368 "edit another file (from vi mode only)"},
369 /* C_VIUSAGE */
370 {"viusage", ex_viusage, E_NOGLOBAL|E_NORC,
371 "w1o",
372 "[viu]sage [key]",
373 "display vi key usage statement"},
374 /* C_WRITE */
375 {"write", ex_write, E_ADDR2_ALL|E_NOGLOBAL|E_NORC|E_ZERODEF,
376 "!s",
377 "[line [,line]] w[rite][!] [!cmd | [>>] [file]]",
378 "write the file"},
379 /* C_WQ */
380 {"wq", ex_wq, E_ADDR2_ALL|E_NOGLOBAL|E_NORC|E_ZERODEF,
381 "!s",
382 "[line [,line]] wq[!] [>>] [file]",
383 "write the file and exit"},
384 /* C_XIT */
385 {"xit", ex_xit, E_ADDR2_ALL|E_NOGLOBAL|E_NORC|E_ZERODEF,
386 "!f1o",
387 "[line [,line]] x[it][!] [file]",
388 "exit"},
389 /* C_YANK */
390 {"yank", ex_yank, E_ADDR2|E_NORC,
391 "bca",
392 "[line [,line]] ya[nk] [buffer] [count]",
393 "copy lines to a cut buffer"},
394 /* C_Z */
395 {"z", ex_z, E_ADDR1|E_NOGLOBAL|E_NORC,
396 "3c01",
397 "[line] z [-|.|+|^|=] [count] [flags]",
398 "display different screens of the file"},
399 /* C_SUBTILDE */
400 {"~", ex_subtilde, E_ADDR2|E_NORC,
401 "s",
402 "[line [,line]] ~ [cgr] [count] [#lp]",
403 "replace previous RE with previous replacement string,"},
404 {NULL},