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