unstack, sort: cleanup and improvement
[minix.git] / commands / elvis / vi.h
blob4c42dd2fc959a3fae2fb12336a2f50b79941e38e
1 /* vi.h */
3 /* Author:
4 * Steve Kirkendall
5 * 14407 SW Teal Blvd. #C
6 * Beaverton, OR 97005
7 * kirkenda@cs.pdx.edu
8 */
10 #define VERSION "ELVIS 1.5, by Steve Kirkendall (23 March 1992)"
11 #define COPYING "This version of ELVIS is freely redistributable."
13 #include <errno.h>
14 extern int errno;
15 #if TOS && !defined(__GNUC__)
16 #define ENOENT (-AEFILNF)
17 #endif
19 #if TOS || VMS
20 # include <types.h>
21 # define O_RDONLY 0
22 # define O_WRONLY 1
23 # define O_RDWR 2
24 # ifdef __GNUC__
25 # define S_IJDIR S_IFDIR
26 # endif
27 #else
28 # if OSK
29 # include <modes.h>
30 # define O_RDONLY S_IREAD
31 # define O_WRONLY S_IWRITE
32 # define O_RDWR (S_IREAD | S_IWRITE)
33 # define ENOENT E_PNNF
34 # define sprintf Sprintf
35 # else
36 # include <sys/types.h>
37 # if COHERENT
38 # include <sys/fcntl.h>
39 # else
40 # include <fcntl.h>
41 # endif
42 # endif
43 #endif
45 #ifndef O_BINARY
46 # define O_BINARY 0
47 #endif
49 #include "curses.h"
51 #include <signal.h>
53 /*------------------------------------------------------------------------*/
54 /* Miscellaneous constants. */
56 #define INFINITY 2000000001L /* a very large integer */
57 #define LONGKEY 10 /* longest possible raw :map key */
58 #ifndef MAXRCLEN
59 # define MAXRCLEN 1000 /* longest possible :@ command */
60 #endif
62 /*------------------------------------------------------------------------*/
63 /* These describe how temporary files are divided into blocks */
65 #define MAXBLKS (BLKSIZE / sizeof(unsigned short))
66 typedef union
68 char c[BLKSIZE]; /* for text blocks */
69 unsigned short n[MAXBLKS]; /* for the header block */
71 BLK;
73 /*------------------------------------------------------------------------*/
74 /* These are used manipulate BLK buffers. */
76 extern BLK hdr; /* buffer for the header block */
77 extern BLK *blkget(); /* given index into hdr.c[], reads block */
78 extern BLK *blkadd(); /* inserts a new block into hdr.c[] */
80 /*------------------------------------------------------------------------*/
81 /* These are used to keep track of various flags */
82 extern struct _viflags
84 short file; /* file flags */
86 viflags;
88 /* file flags */
89 #define NEWFILE 0x0001 /* the file was just created */
90 #define READONLY 0x0002 /* the file is read-only */
91 #define HADNUL 0x0004 /* the file contained NUL characters */
92 #define MODIFIED 0x0008 /* the file has been modified, but not saved */
93 #define NOFILE 0x0010 /* no name is known for the current text */
94 #define ADDEDNL 0x0020 /* newlines were added to the file */
95 #define HADBS 0x0040 /* backspace chars were lost from the file */
96 #define UNDOABLE 0x0080 /* file has been modified */
97 #define NOTEDITED 0x0100 /* the :file command has been used */
99 /* macros used to set/clear/test flags */
100 #define setflag(x,y) viflags.x |= y
101 #define clrflag(x,y) viflags.x &= ~y
102 #define tstflag(x,y) (viflags.x & y)
103 #define initflags() viflags.file = 0;
105 /* The options */
106 extern char o_autoindent[1];
107 extern char o_autoprint[1];
108 extern char o_autotab[1];
109 extern char o_autowrite[1];
110 extern char o_columns[3];
111 extern char o_directory[30];
112 extern char o_edcompatible[1];
113 extern char o_equalprg[80];
114 extern char o_errorbells[1];
115 extern char o_exrefresh[1];
116 extern char o_ignorecase[1];
117 extern char o_keytime[3];
118 extern char o_keywordprg[80];
119 extern char o_lines[3];
120 extern char o_list[1];
121 extern char o_number[1];
122 extern char o_readonly[1];
123 extern char o_remap[1];
124 extern char o_report[3];
125 extern char o_scroll[3];
126 extern char o_shell[60];
127 extern char o_shiftwidth[3];
128 extern char o_sidescroll[3];
129 extern char o_sync[1];
130 extern char o_tabstop[3];
131 extern char o_term[30];
132 extern char o_flash[1];
133 extern char o_warn[1];
134 extern char o_wrapscan[1];
136 #ifndef CRUNCH
137 extern char o_beautify[1];
138 extern char o_exrc[1];
139 extern char o_mesg[1];
140 extern char o_more[1];
141 extern char o_novice[1];
142 extern char o_prompt[1];
143 extern char o_taglength[3];
144 extern char o_terse[1];
145 extern char o_window[3];
146 extern char o_wrapmargin[3];
147 extern char o_writeany[1];
148 #endif
150 #ifndef NO_ERRLIST
151 extern char o_cc[30];
152 extern char o_make[30];
153 #endif
155 #ifndef NO_CHARATTR
156 extern char o_charattr[1];
157 #endif
159 #ifndef NO_DIGRAPH
160 extern char o_digraph[1];
161 extern char o_flipcase[80];
162 #endif
164 #ifndef NO_SENTENCE
165 extern char o_hideformat[1];
166 #endif
168 #ifndef NO_EXTENSIONS
169 extern char o_inputmode[1];
170 extern char o_ruler[1];
171 #endif
173 #ifndef NO_MAGIC
174 extern char o_magic[1];
175 #endif
177 #ifndef NO_MODELINES
178 extern char o_modelines[1];
179 #endif
181 #ifndef NO_SENTENCE
182 extern char o_paragraphs[30];
183 extern char o_sections[30];
184 #endif
186 #if MSDOS
187 extern char o_pcbios[1];
188 #endif
190 #ifndef NO_SHOWMATCH
191 extern char o_showmatch[1];
192 #endif
194 #ifndef NO_SHOWMODE
195 extern char o_smd[1];
196 #endif
198 /*------------------------------------------------------------------------*/
199 /* These help support the single-line multi-change "undo" -- shift-U */
201 extern char U_text[BLKSIZE];
202 extern long U_line;
204 /*------------------------------------------------------------------------*/
205 /* These are used to refer to places in the text */
207 typedef long MARK;
208 #define markline(x) (long)((x) / BLKSIZE)
209 #define markidx(x) (int)((x) & (BLKSIZE - 1))
210 #define MARK_UNSET ((MARK)0)
211 #define MARK_FIRST ((MARK)BLKSIZE)
212 #define MARK_LAST ((MARK)(nlines * BLKSIZE))
213 #define MARK_AT_LINE(x) ((MARK)(x) * BLKSIZE)
215 #define NMARKS 29
216 extern MARK mark[NMARKS]; /* marks a-z, plus mark ' and two temps */
217 extern MARK cursor; /* mark where line is */
219 /*------------------------------------------------------------------------*/
220 /* These are used to keep track of the current & previous files. */
222 extern long origtime; /* modification date&time of the current file */
223 extern char origname[256]; /* name of the current file */
224 extern char prevorig[256]; /* name of the preceding file */
225 extern long prevline; /* line number from preceding file */
227 /*------------------------------------------------------------------------*/
228 /* misc housekeeping variables & functions */
230 extern int tmpfd; /* fd used to access the tmp file */
231 extern int tmpnum; /* counter used to generate unique filenames */
232 extern long lnum[MAXBLKS]; /* last line# of each block */
233 extern long nlines; /* number of lines in the file */
234 extern char args[BLKSIZE]; /* file names given on the command line */
235 extern int argno; /* the current element of args[] */
236 extern int nargs; /* number of filenames in args */
237 extern long changes; /* counts changes, to prohibit short-cuts */
238 extern int significant; /* boolean: was a *REAL* change made? */
239 extern BLK tmpblk; /* a block used to accumulate changes */
240 extern long topline; /* file line number of top line */
241 extern int leftcol; /* column number of left col */
242 #define botline (topline + LINES - 2)
243 #define rightcol (leftcol + COLS - (*o_number ? 9 : 1))
244 extern int physcol; /* physical column number that cursor is on */
245 extern int physrow; /* physical row number that cursor is on */
246 extern int exwrote; /* used to detect verbose ex commands */
247 extern int doingdot; /* boolean: are we doing the "." command? */
248 extern int doingglobal; /* boolean: are doing a ":g" command? */
249 extern long rptlines; /* number of lines affected by a command */
250 extern char *rptlabel; /* description of how lines were affected */
251 extern char *fetchline(); /* read a given line from tmp file */
252 extern char *parseptrn(); /* isolate a regexp in a line */
253 extern MARK paste(); /* paste from cut buffer to a given point */
254 extern char *wildcard(); /* expand wildcards in filenames */
255 extern MARK input(); /* inserts characters from keyboard */
256 extern char *linespec(); /* finds the end of a /regexp/ string */
257 #define ctrl(ch) ((ch)&037)
258 #ifndef NO_RECYCLE
259 extern long allocate(); /* allocate a free block of the tmp file */
260 #endif
261 extern int trapint(); /* trap handler for SIGINT */
262 extern int deathtrap(); /* trap handler for deadly signals */
263 extern void blkdirty(); /* marks a block as being "dirty" */
264 extern void blkflush(); /* writes a single dirty block to the disk */
265 extern void blksync(); /* forces all "dirty" blocks to disk */
266 extern void blkinit(); /* resets the block cache to "empty" state */
267 extern void beep(); /* rings the terminal's bell */
268 extern void exrefresh(); /* writes text to the screen */
269 extern void msg(); /* writes a printf-style message to the screen */
270 extern void endmsgs(); /* if "manymsgs" is set, then scroll up 1 line */
271 extern void garbage(); /* reclaims any garbage blocks */
272 extern void redraw(); /* updates the screen after a change */
273 extern void resume_curses();/* puts the terminal in "cbreak" mode */
274 extern void beforedo(); /* saves current revision before a new change */
275 extern void afterdo(); /* marks end of a beforedo() change */
276 extern void abortdo(); /* like "afterdo()" followed by "undo()" */
277 extern int undo(); /* restores file to previous undo() */
278 extern void dumpkey(); /* lists key mappings to the screen */
279 extern void mapkey(); /* defines a new key mapping */
280 extern void savekeys(); /* lists key mappings to a file */
281 extern void redrawrange(); /* records clues from modify.c */
282 extern void cut(); /* saves text in a cut buffer */
283 extern void delete(); /* deletes text */
284 extern void add(); /* adds text */
285 extern void change(); /* deletes text, and then adds other text */
286 extern void cutswitch(); /* updates cut buffers when we switch files */
287 extern void do_abbr(); /* defines or lists abbreviations */
288 extern void do_digraph(); /* defines or lists digraphs */
289 extern void exstring(); /* execute a string as EX commands */
290 extern void dumpopts();
291 extern void setopts();
292 extern void saveopts();
293 extern void savedigs();
294 extern void saveabbr();
295 extern void savecolor();
296 extern void cutname();
297 extern void cutname();
298 extern void initopts();
299 extern void cutend();
300 #ifndef CRUNCH
301 extern int wset; /* boolean: has the "window" size been set? */
302 #endif
304 /*------------------------------------------------------------------------*/
305 /* macros that are used as control structures */
307 #define BeforeAfter(before, after) for((before),bavar=1;bavar;(after),bavar=0)
308 #define ChangeText BeforeAfter(beforedo(FALSE),afterdo())
310 extern int bavar; /* used only in BeforeAfter macros */
312 /*------------------------------------------------------------------------*/
313 /* These are the movement commands. Each accepts a mark for the starting */
314 /* location & number and returns a mark for the destination. */
316 extern MARK m_updnto(); /* k j G */
317 extern MARK m_right(); /* h */
318 extern MARK m_left(); /* l */
319 extern MARK m_tocol(); /* | */
320 extern MARK m_front(); /* ^ */
321 extern MARK m_rear(); /* $ */
322 extern MARK m_fword(); /* w */
323 extern MARK m_bword(); /* b */
324 extern MARK m_eword(); /* e */
325 extern MARK m_paragraph(); /* { } [[ ]] */
326 extern MARK m_match(); /* % */
327 #ifndef NO_SENTENCE
328 extern MARK m_sentence(); /* ( ) */
329 #endif
330 extern MARK m_tomark(); /* 'm */
331 #ifndef NO_EXTENSIONS
332 extern MARK m_wsrch(); /* ^A */
333 #endif
334 extern MARK m_nsrch(); /* n */
335 extern MARK m_Nsrch(); /* N */
336 extern MARK m_fsrch(); /* /regexp */
337 extern MARK m_bsrch(); /* ?regexp */
338 #ifndef NO_CHARSEARCH
339 extern MARK m__ch(); /* ; , */
340 extern MARK m_fch(); /* f */
341 extern MARK m_tch(); /* t */
342 extern MARK m_Fch(); /* F */
343 extern MARK m_Tch(); /* T */
344 #endif
345 extern MARK m_row(); /* H L M */
346 extern MARK m_z(); /* z */
347 extern MARK m_scroll(); /* ^B ^F ^E ^Y ^U ^D */
349 /* Some stuff that is used by movement functions... */
351 extern MARK adjmove(); /* a helper fn, used by move fns */
353 /* This macro is used to set the default value of cnt */
354 #define DEFAULT(val) if (cnt < 1) cnt = (val)
356 /* These are used to minimize calls to fetchline() */
357 extern int plen; /* length of the line */
358 extern long pline; /* line number that len refers to */
359 extern long pchgs; /* "changes" level that len refers to */
360 extern char *ptext; /* text of previous line, if valid */
361 extern void pfetch();
362 extern char digraph();
364 /* This is used to build a MARK that corresponds to a specific point in the
365 * line that was most recently pfetch'ed.
367 #define buildmark(text) (MARK)(BLKSIZE * pline + (int)((text) - ptext))
370 /*------------------------------------------------------------------------*/
371 /* These are used to handle EX commands. */
373 #define CMD_NULL 0 /* NOT A VALID COMMAND */
374 #define CMD_ABBR 1 /* "define an abbreviation" */
375 #define CMD_ARGS 2 /* "show me the args" */
376 #define CMD_APPEND 3 /* "insert lines after this line" */
377 #define CMD_AT 4 /* "execute a cut buffer's contents via EX" */
378 #define CMD_BANG 5 /* "run a single shell command" */
379 #define CMD_CC 6 /* "run `cc` and then do CMD_ERRLIST" */
380 #define CMD_CD 7 /* "change directories" */
381 #define CMD_CHANGE 8 /* "change some lines" */
382 #define CMD_COLOR 9 /* "change the default colors" */
383 #define CMD_COPY 10 /* "copy the selected text to a given place" */
384 #define CMD_DELETE 11 /* "delete the selected text" */
385 #define CMD_DIGRAPH 12 /* "add a digraph, or display them all" */
386 #define CMD_EDIT 13 /* "switch to a different file" */
387 #define CMD_EQUAL 14 /* "display a line number" */
388 #define CMD_ERRLIST 15 /* "locate the next error in a list" */
389 #define CMD_FILE 16 /* "show the file's status" */
390 #define CMD_GLOBAL 17 /* "globally search & do a command" */
391 #define CMD_INSERT 18 /* "insert lines before the current line" */
392 #define CMD_JOIN 19 /* "join the selected line & the one after" */
393 #define CMD_LIST 20 /* "print lines, making control chars visible" */
394 #define CMD_MAKE 21 /* "run `make` and then do CMD_ERRLIST" */
395 #define CMD_MAP 22 /* "adjust the keyboard map" */
396 #define CMD_MARK 23 /* "mark this line" */
397 #define CMD_MKEXRC 24 /* "make a .exrc file" */
398 #define CMD_MOVE 25 /* "move the selected text to a given place" */
399 #define CMD_NEXT 26 /* "switch to next file in args" */
400 #define CMD_NUMBER 27 /* "print lines from the file w/ line numbers" */
401 #define CMD_PRESERVE 28 /* "act as though vi crashed" */
402 #define CMD_PREVIOUS 29 /* "switch to the previous file in args" */
403 #define CMD_PRINT 30 /* "print the selected text" */
404 #define CMD_PUT 31 /* "insert any cut lines before this line" */
405 #define CMD_QUIT 32 /* "quit without writing the file" */
406 #define CMD_READ 33 /* "append the given file after this line */
407 #define CMD_RECOVER 34 /* "recover file after vi crashes" - USE -r FLAG */
408 #define CMD_REWIND 35 /* "rewind to first file" */
409 #define CMD_SET 36 /* "set a variable's value" */
410 #define CMD_SHELL 37 /* "run some lines through a command" */
411 #define CMD_SHIFTL 38 /* "shift lines left" */
412 #define CMD_SHIFTR 39 /* "shift lines right" */
413 #define CMD_SOURCE 40 /* "interpret a file's contents as ex commands" */
414 #define CMD_STOP 41 /* same as CMD_SUSPEND */
415 #define CMD_SUBAGAIN 42 /* "repeat the previous substitution" */
416 #define CMD_SUBSTITUTE 43 /* "substitute text in this line" */
417 #define CMD_SUSPEND 44 /* "suspend the vi session" */
418 #define CMD_TR 45 /* "transliterate chars in the selected lines" */
419 #define CMD_TAG 46 /* "go to a particular tag" */
420 #define CMD_UNABBR 47 /* "remove an abbreviation definition" */
421 #define CMD_UNDO 48 /* "undo the previous command" */
422 #define CMD_UNMAP 49 /* "remove a key sequence map */
423 #define CMD_VERSION 50 /* "describe which version this is" */
424 #define CMD_VGLOBAL 51 /* "apply a cmd to lines NOT containing an RE" */
425 #define CMD_VISUAL 52 /* "go into visual mode" */
426 #define CMD_WQUIT 53 /* "write this file out (any case) & quit" */
427 #define CMD_WRITE 54 /* "write the selected(?) text to a given file" */
428 #define CMD_XIT 55 /* "write this file out (if modified) & quit" */
429 #define CMD_YANK 56 /* "copy the selected text into the cut buffer" */
430 #ifdef DEBUG
431 # define CMD_DEBUG 57 /* access to internal data structures */
432 # define CMD_VALIDATE 58 /* check for internal consistency */
433 #endif
434 typedef int CMD;
436 extern void ex();
437 extern void vi();
438 extern void doexcmd();
440 extern void cmd_append();
441 extern void cmd_args();
442 #ifndef NO_AT
443 extern void cmd_at();
444 #endif
445 extern void cmd_cd();
446 #ifndef NO_COLOR
447 extern void cmd_color();
448 #endif
449 extern void cmd_delete();
450 #ifndef NO_DIGRAPH
451 extern void cmd_digraph();
452 #endif
453 extern void cmd_edit();
454 #ifndef NO_ERRLIST
455 extern void cmd_errlist();
456 #endif
457 extern void cmd_file();
458 extern void cmd_global();
459 extern void cmd_join();
460 extern void cmd_mark();
461 #ifndef NO_ERRLIST
462 extern void cmd_make();
463 #endif
464 extern void cmd_map();
465 #ifndef NO_MKEXRC
466 extern void cmd_mkexrc();
467 #endif
468 extern void cmd_next();
469 extern void cmd_print();
470 extern void cmd_put();
471 extern void cmd_read();
472 extern void cmd_set();
473 extern void cmd_shell();
474 extern void cmd_shift();
475 extern void cmd_source();
476 extern void cmd_substitute();
477 extern void cmd_tag();
478 extern void cmd_undo();
479 extern void cmd_version();
480 extern void cmd_write();
481 extern void cmd_xit();
482 extern void cmd_move();
483 #ifdef DEBUG
484 extern void cmd_debug();
485 extern void cmd_validate();
486 #endif
487 #ifdef SIGTSTP
488 extern void cmd_suspend();
489 #endif
491 /*----------------------------------------------------------------------*/
492 /* These are used to handle VI commands */
494 extern MARK v_1ex(); /* : */
495 extern MARK v_mark(); /* m */
496 extern MARK v_quit(); /* Q */
497 extern MARK v_redraw(); /* ^L ^R */
498 extern MARK v_ulcase(); /* ~ */
499 extern MARK v_undo(); /* u */
500 extern MARK v_xchar(); /* x X */
501 extern MARK v_replace(); /* r */
502 extern MARK v_overtype(); /* R */
503 extern MARK v_selcut(); /* " */
504 extern MARK v_paste(); /* p P */
505 extern MARK v_yank(); /* y Y */
506 extern MARK v_delete(); /* d D */
507 extern MARK v_join(); /* J */
508 extern MARK v_insert(); /* a A i I o O */
509 extern MARK v_change(); /* c C */
510 extern MARK v_subst(); /* s */
511 extern MARK v_lshift(); /* < */
512 extern MARK v_rshift(); /* > */
513 extern MARK v_reformat(); /* = */
514 extern MARK v_filter(); /* ! */
515 extern MARK v_status(); /* ^G */
516 extern MARK v_switch(); /* ^^ */
517 extern MARK v_tag(); /* ^] */
518 extern MARK v_xit(); /* ZZ */
519 extern MARK v_undoline(); /* U */
520 extern MARK v_again(); /* & */
521 #ifndef NO_EXTENSIONS
522 extern MARK v_keyword(); /* K */
523 extern MARK v_increment(); /* * */
524 #endif
525 #ifndef NO_ERRLIST
526 extern MARK v_errlist(); /* * */
527 #endif
528 #ifndef NO_AT
529 extern MARK v_at(); /* @ */
530 #endif
531 #ifdef SIGTSTP
532 extern MARK v_suspend(); /* ^Z */
533 #endif
534 #ifndef NO_POPUP
535 extern MARK v_popup(); /* \ */
536 #endif
538 /*----------------------------------------------------------------------*/
539 /* These flags describe the quirks of the individual visual commands */
540 #define NO_FLAGS 0x00
541 #define MVMT 0x01 /* this is a movement command */
542 #define PTMV 0x02 /* this can be *part* of a movement command */
543 #define FRNT 0x04 /* after move, go to front of line */
544 #define INCL 0x08 /* include last char when used with c/d/y */
545 #define LNMD 0x10 /* use line mode of c/d/y */
546 #define NCOL 0x20 /* this command can't change the column# */
547 #define NREL 0x40 /* this is "non-relative" -- set the '' mark */
548 #define SDOT 0x80 /* set the "dot" variables, for the "." cmd */
549 #ifndef NO_VISIBLE
550 # define VIZ 0x100 /* commands which can be used with 'v' */
551 #else
552 # define VIZ 0
553 #endif
555 /* This variable is zeroed before a command executes, and later ORed with the
556 * command's flags after the command has been executed. It is used to force
557 * certain flags to be TRUE for *some* invocations of a particular command.
558 * For example, "/regexp/+offset" forces the LNMD flag, and sometimes a "p"
559 * or "P" command will force FRNT.
561 extern int force_flags;
563 /*----------------------------------------------------------------------*/
564 /* These describe what mode we're in */
566 #define MODE_EX 1 /* executing ex commands */
567 #define MODE_VI 2 /* executing vi commands */
568 #define MODE_COLON 3 /* executing an ex command from vi mode */
569 #define MODE_QUIT 4
570 extern int mode;
572 #define WHEN_VICMD 1 /* getkey: we're reading a VI command */
573 #define WHEN_VIINP 2 /* getkey: we're in VI's INPUT mode */
574 #define WHEN_VIREP 4 /* getkey: we're in VI's REPLACE mode */
575 #define WHEN_EX 8 /* getkey: we're in EX mode */
576 #define WHEN_MSG 16 /* getkey: we're at a "more" prompt */
577 #define WHEN_POPUP 32 /* getkey: we're in the pop-up menu */
578 #define WHEN_REP1 64 /* getkey: we're getting a single char for 'r' */
579 #define WHEN_CUT 128 /* getkey: we're getting a cut buffer name */
580 #define WHEN_MARK 256 /* getkey: we're getting a mark name */
581 #define WHEN_CHAR 512 /* getkey: we're getting a destination for f/F/t/T */
582 #define WHEN_INMV 4096 /* in input mode, interpret the key in VICMD mode */
583 #define WHEN_FREE 8192 /* free the keymap after doing it once */
584 #define WHENMASK (WHEN_VICMD|WHEN_VIINP|WHEN_VIREP|WHEN_REP1|WHEN_CUT|WHEN_MARK|WHEN_CHAR)
586 #ifndef NO_VISIBLE
587 extern MARK V_from;
588 extern int V_linemd;
589 extern MARK v_start();
590 #endif
592 #ifdef DEBUG
593 # define malloc(size) dbmalloc(size, __FILE__, __LINE__)
594 # define free(ptr) dbfree(ptr, __FILE__, __LINE__)
595 extern char *dbmalloc();
596 #endif