1 /* $OpenBSD: def.h,v 1.116 2011/01/23 00:45:03 kjell Exp $ */ 2 3 /* This file is in the public domain. */ 4 5 /* 6 * This file is the general header file for all parts 7 * of the Mg display editor. It contains all of the 8 * general definitions and macros. It also contains some 9 * conditional compilation flags. All of the per-system and 10 * per-terminal definitions are in special header files. 11 */ 12 13 #include "sysdef.h" /* Order is critical. */ 14 #include "ttydef.h" 15 #include "chrdef.h" 16 17 #ifdef NO_MACRO 18 #ifndef NO_STARTUP 19 #define NO_STARTUP /* NO_MACRO implies NO_STARTUP */ 20 #endif 21 #endif 22 23 typedef int (*PF)(int, int); /* generally useful type */ 24 25 /* 26 * Table sizes, etc. 27 */ 28 #define NFILEN 1024 /* Length, file name. */ 29 #define NBUFN NFILEN /* Length, buffer name. */ 30 #define NLINE 256 /* Length, line. */ 31 #define PBMODES 4 /* modes per buffer */ 32 #define NKBDM 256 /* Length, keyboard macro. */ 33 #define NPAT 80 /* Length, pattern. */ 34 #define HUGE 1000 /* A rather large number. */ 35 #define NSRCH 128 /* Undoable search commands. */ 36 #define NXNAME 64 /* Length, extended command. */ 37 #define NKNAME 20 /* Length, key names. */ 38 #define NTIME 50 /* Length, timestamp string. */ 39 /* 40 * Universal. 41 */ 42 #define FALSE 0 /* False, no, bad, etc. */ 43 #define TRUE 1 /* True, yes, good, etc. */ 44 #define ABORT 2 /* Death, ^G, abort, etc. */ 45 46 #define KCLEAR 2 /* clear echo area */ 47 48 /* 49 * These flag bits keep track of 50 * some aspects of the last command. The CFCPCN 51 * flag controls goal column setting. The CFKILL 52 * flag controls the clearing versus appending 53 * of data in the kill buffer. 54 */ 55 #define CFCPCN 0x0001 /* Last command was C-P, C-N */ 56 #define CFKILL 0x0002 /* Last command was a kill */ 57 #define CFINS 0x0004 /* Last command was self-insert */ 58 59 /* 60 * File I/O. 61 */ 62 #define FIOSUC 0 /* Success. */ 63 #define FIOFNF 1 /* File not found. */ 64 #define FIOEOF 2 /* End of file. */ 65 #define FIOERR 3 /* Error. */ 66 #define FIOLONG 4 /* long line partially read */ 67 #define FIODIR 5 /* File is a directory */ 68 69 /* 70 * Directory I/O. 71 */ 72 #define DIOSUC 0 /* Success. */ 73 #define DIOEOF 1 /* End of file. */ 74 #define DIOERR 2 /* Error. */ 75 76 /* 77 * Display colors. 78 */ 79 #define CNONE 0 /* Unknown color. */ 80 #define CTEXT 1 /* Text color. */ 81 #define CMODE 2 /* Mode line color. */ 82 83 /* 84 * Flags for keyboard invoked functions. 85 */ 86 #define FFUNIV 1 /* universal argument */ 87 #define FFNEGARG 2 /* negative only argument */ 88 #define FFOTHARG 4 /* other argument */ 89 #define FFARG 7 /* any argument */ 90 #define FFRAND 8 /* Called by other function */ 91 92 /* 93 * Flags for "eread". 94 */ 95 #define EFFUNC 0x0001 /* Autocomplete functions. */ 96 #define EFBUF 0x0002 /* Autocomplete buffers. */ 97 #define EFFILE 0x0004 /* " files (maybe someday) */ 98 #define EFAUTO 0x0007 /* Some autocompletion on */ 99 #define EFNEW 0x0008 /* New prompt. */ 100 #define EFCR 0x0010 /* Echo CR at end; last read. */ 101 #define EFDEF 0x0020 /* buffer contains default args */ 102 #define EFNUL 0x0040 /* Null Minibuffer OK */ 103 104 /* 105 * Direction of insert into kill ring 106 */ 107 #define KNONE 0x00 108 #define KFORW 0x01 /* forward insert into kill ring */ 109 #define KBACK 0x02 /* Backwards insert into kill ring */ 110 #define KREG 0x04 /* This is a region-based kill */ 111 112 /* 113 * This structure holds the starting position 114 * (as a line/offset pair) and the number of characters in a 115 * region of a buffer. This makes passing the specification 116 * of a region around a little bit easier. 117 */ 118 struct region { 119 struct line *r_linep; /* Origin line address. */ 120 int r_offset; /* Origin line offset. */ 121 int r_lineno; /* Origin line number */ 122 RSIZE r_size; /* Length in characters. */ 123 }; 124 125 126 /* 127 * All text is kept in circularly linked 128 * lists of "line" structures. These begin at the 129 * header line (which is the blank line beyond the 130 * end of the buffer). This line is pointed to by 131 * the "buffer" structure. Each line contains the number of 132 * bytes in the line (the "used" size), the size 133 * of the text array, and the text. The end of line 134 * is not stored as a byte; it's implied. Future 135 * additions will include update hints, and a 136 * list of marks into the line. 137 */ 138 struct line { 139 struct line *l_fp; /* Link to the next line */ 140 struct line *l_bp; /* Link to the previous line */ 141 int l_size; /* Allocated size */ 142 int l_used; /* Used size */ 143 char *l_text; /* Content of the line */ 144 }; 145 146 /* 147 * The rationale behind these macros is that you 148 * could (with some editing, like changing the type of a line 149 * link from a "struct line *" to a "REFLINE", and fixing the commands 150 * like file reading that break the rules) change the actual 151 * storage representation of lines to use something fancy on 152 * machines with small address spaces. 153 */ 154 #define lforw(lp) ((lp)->l_fp) 155 #define lback(lp) ((lp)->l_bp) 156 #define lgetc(lp, n) (CHARMASK((lp)->l_text[(n)])) 157 #define lputc(lp, n, c) ((lp)->l_text[(n)]=(c)) 158 #define llength(lp) ((lp)->l_used) 159 #define ltext(lp) ((lp)->l_text) 160 161 /* 162 * All repeated structures are kept as linked lists of structures. 163 * All of these start with a LIST structure (except lines, which 164 * have their own abstraction). This will allow for 165 * later conversion to generic list manipulation routines should 166 * I decide to do that. It does mean that there are four extra 167 * bytes per window. I feel that this is an acceptable price, 168 * considering that there are usually only one or two windows. 169 */ 170 struct list { 171 union { 172 struct mgwin *l_wp; 173 struct buffer *x_bp; /* l_bp is used by struct line */ 174 struct list *l_nxt; 175 } l_p; 176 char *l_name; 177 }; 178 179 /* 180 * Usual hack - to keep from uglifying the code with lotsa 181 * references through the union, we #define something for it. 182 */ 183 #define l_next l_p.l_nxt 184 185 /* 186 * There is a window structure allocated for 187 * every active display window. The windows are kept in a 188 * big list, in top to bottom screen order, with the listhead at 189 * "wheadp". Each window contains its own values of dot and mark. 190 * The flag field contains some bits that are set by commands 191 * to guide redisplay; although this is a bit of a compromise in 192 * terms of decoupling, the full blown redisplay is just too 193 * expensive to run for every input character. 194 */ 195 struct mgwin { 196 struct list w_list; /* List header */ 197 struct buffer *w_bufp; /* Buffer displayed in window */ 198 struct line *w_linep; /* Top line in the window */ 199 struct line *w_dotp; /* Line containing "." */ 200 struct line *w_markp; /* Line containing "mark" */ 201 int w_doto; /* Byte offset for "." */ 202 int w_marko; /* Byte offset for "mark" */ 203 char w_toprow; /* Origin 0 top row of window */ 204 char w_ntrows; /* # of rows of text in window */ 205 char w_frame; /* #lines to reframe by. */ 206 char w_rflag; /* Redisplay Flags. */ 207 char w_flag; /* Flags. */ 208 struct line *w_wrapline; 209 int w_dotline; /* current line number of dot */ 210 int w_markline; /* current line number of mark */ 211 }; 212 #define w_wndp w_list.l_p.l_wp 213 #define w_name w_list.l_name 214 215 /* 216 * Window redisplay flags are set by command processors to 217 * tell the display system what has happened to the buffer 218 * mapped by the window. Setting "WFFULL" is always a safe thing 219 * to do, but it may do more work than is necessary. Always try 220 * to set the simplest action that achieves the required update. 221 * Because commands set bits in the "w_flag", update will see 222 * all change flags, and do the most general one. 223 */ 224 #define WFFRAME 0x01 /* Force reframe. */ 225 #define WFMOVE 0x02 /* Movement from line to line. */ 226 #define WFEDIT 0x04 /* Editing within a line. */ 227 #define WFFULL 0x08 /* Do a full display. */ 228 #define WFMODE 0x10 /* Update mode line. */ 229 230 /* 231 * Window flags 232 */ 233 #define WNONE 0x00 /* No special window options. */ 234 #define WEPHEM 0x01 /* Window is ephemeral. */ 235 236 struct undo_rec; 237 TAILQ_HEAD(undoq, undo_rec); 238 239 /* 240 * Text is kept in buffers. A buffer header, described 241 * below, exists for every buffer in the system. The buffers are 242 * kept in a big list, so that commands that search for a buffer by 243 * name can find the buffer header. There is a safe store for the 244 * dot and mark in the header, but this is only valid if the buffer 245 * is not being displayed (that is, if "b_nwnd" is 0). The text for 246 * the buffer is kept in a circularly linked list of lines, with 247 * a pointer to the header line in "b_headp". 248 */ 249 struct buffer { 250 struct list b_list; /* buffer list pointer */ 251 struct buffer *b_altb; /* Link to alternate buffer */ 252 struct line *b_dotp; /* Link to "." line structure */ 253 struct line *b_markp; /* ditto for mark */ 254 struct line *b_headp; /* Link to the header line */ 255 struct maps_s *b_modes[PBMODES]; /* buffer modes */ 256 int b_doto; /* Offset of "." in above line */ 257 int b_marko; /* ditto for the "mark" */ 258 short b_nmodes; /* number of non-fundamental modes */ 259 char b_nwnd; /* Count of windows on buffer */ 260 char b_flag; /* Flags */ 261 char b_fname[NFILEN]; /* File name */ 262 char b_cwd[NFILEN]; /* working directory */ 263 struct fileinfo b_fi; /* File attributes */ 264 struct undoq b_undo; /* Undo actions list */ 265 int b_undopos; /* Where we were during last undo */ 266 struct undo_rec *b_undoptr; 267 int b_dotline; /* Line number of dot */ 268 int b_markline; /* Line number of mark */ 269 int b_lines; /* Number of lines in file */ 270 }; 271 #define b_bufp b_list.l_p.x_bp 272 #define b_bname b_list.l_name 273 274 /* Some helper macros, in case they ever change to functions */ 275 #define bfirstlp(buf) (lforw((buf)->b_headp)) 276 #define blastlp(buf) (lback((buf)->b_headp)) 277 278 #define BFCHG 0x01 /* Changed. */ 279 #define BFBAK 0x02 /* Need to make a backup. */ 280 #ifdef NOTAB 281 #define BFNOTAB 0x04 /* no tab mode */ 282 #endif 283 #define BFOVERWRITE 0x08 /* overwrite mode */ 284 #define BFREADONLY 0x10 /* read only mode */ 285 #define BFDIRTY 0x20 /* Buffer was modified elsewhere */ 286 #define BFIGNDIRTY 0x40 /* Ignore modifications */ 287 /* 288 * This structure holds information about recent actions for the Undo command. 289 */ 290 struct undo_rec { 291 TAILQ_ENTRY(undo_rec) next; 292 enum { 293 INSERT = 1, 294 DELETE, 295 BOUNDARY, 296 MODIFIED, 297 DELREG 298 } type; 299 struct region region; 300 int pos; 301 char *content; 302 }; 303 304 /* 305 * Prototypes. 306 */ 307 308 /* tty.c X */ 309 void ttinit(void); 310 void ttreinit(void); 311 void tttidy(void); 312 void ttmove(int, int); 313 void tteeol(void); 314 void tteeop(void); 315 void ttbeep(void); 316 void ttinsl(int, int, int); 317 void ttdell(int, int, int); 318 void ttwindow(int, int); 319 void ttnowindow(void); 320 void ttcolor(int); 321 void ttresize(void); 322 323 volatile sig_atomic_t winch_flag; 324 325 /* ttyio.c */ 326 void ttopen(void); 327 int ttraw(void); 328 void ttclose(void); 329 int ttcooked(void); 330 int ttputc(int); 331 void ttflush(void); 332 int ttgetc(void); 333 int ttwait(int); 334 int charswaiting(void); 335 336 /* dir.c */ 337 void dirinit(void); 338 int changedir(int, int); 339 int showcwdir(int, int); 340 int getcwdir(char *, size_t); 341 342 /* dired.c */ 343 struct buffer *dired_(char *); 344 345 /* file.c X */ 346 int fileinsert(int, int); 347 int filevisit(int, int); 348 int filevisitalt(int, int); 349 int filevisitro(int, int); 350 int poptofile(int, int); 351 struct buffer *findbuffer(char *); 352 int readin(char *); 353 int insertfile(char *, char *, int); 354 int filewrite(int, int); 355 int filesave(int, int); 356 int buffsave(struct buffer *); 357 int makebkfile(int, int); 358 int writeout(struct buffer *, char *); 359 void upmodes(struct buffer *); 360 size_t xbasename(char *, const char *, size_t); 361 362 /* line.c X */ 363 struct line *lalloc(int); 364 int lrealloc(struct line *, int); 365 void lfree(struct line *); 366 void lchange(int); 367 int linsert_str(const char *, int); 368 int linsert(int, int); 369 int lnewline_at(struct line *, int); 370 int lnewline(void); 371 int ldelete(RSIZE, int); 372 int ldelnewline(void); 373 int lreplace(RSIZE, char *); 374 char * linetostr(const struct line *); 375 376 /* yank.c X */ 377 378 void kdelete(void); 379 int kinsert(int, int); 380 int kremove(int); 381 int kchunk(char *, RSIZE, int); 382 int killline(int, int); 383 int yank(int, int); 384 385 /* window.c X */ 386 struct mgwin *new_window(struct buffer *); 387 void free_window(struct mgwin *); 388 int reposition(int, int); 389 int redraw(int, int); 390 int do_redraw(int, int, int); 391 int nextwind(int, int); 392 int prevwind(int, int); 393 int onlywind(int, int); 394 int splitwind(int, int); 395 int enlargewind(int, int); 396 int shrinkwind(int, int); 397 int delwind(int, int); 398 399 /* buffer.c */ 400 int togglereadonly(int, int); 401 struct buffer *bfind(const char *, int); 402 int poptobuffer(int, int); 403 int killbuffer(struct buffer *); 404 int killbuffer_cmd(int, int); 405 int savebuffers(int, int); 406 int listbuffers(int, int); 407 int addlinef(struct buffer *, char *, ...); 408 #define addline(bp, text) addlinef(bp, "%s", text) 409 int anycb(int); 410 int bclear(struct buffer *); 411 int showbuffer(struct buffer *, struct mgwin *, int); 412 int augbname(char *, const char *, size_t); 413 struct mgwin *popbuf(struct buffer *, int); 414 int bufferinsert(int, int); 415 int usebuffer(int, int); 416 int notmodified(int, int); 417 int popbuftop(struct buffer *, int); 418 int getbufcwd(char *, size_t); 419 int checkdirty(struct buffer *); 420 421 /* display.c */ 422 int vtresize(int, int, int); 423 void vtinit(void); 424 void vttidy(void); 425 void update(void); 426 int linenotoggle(int, int); 427 428 /* echo.c X */ 429 void eerase(void); 430 int eyorn(const char *); 431 int eyesno(const char *); 432 void ewprintf(const char *fmt, ...); 433 char *ereply(const char *, char *, size_t, ...); 434 char *eread(const char *, char *, size_t, int, ...); 435 int getxtra(struct list *, struct list *, int, int); 436 void free_file_list(struct list *); 437 438 /* fileio.c */ 439 int ffropen(const char *, struct buffer *); 440 void ffstat(struct buffer *); 441 int ffwopen(const char *, struct buffer *); 442 int ffclose(struct buffer *); 443 int ffputbuf(struct buffer *); 444 int ffgetline(char *, int, int *); 445 int fbackupfile(const char *); 446 char *adjustname(const char *, int); 447 char *startupfile(char *); 448 int copy(char *, char *); 449 struct list *make_file_list(char *); 450 int fisdir(const char *); 451 int fchecktime(struct buffer *); 452 int fupdstat(struct buffer *); 453 454 /* kbd.c X */ 455 int do_meta(int, int); 456 int bsmap(int, int); 457 void ungetkey(int); 458 int getkey(int); 459 int doin(void); 460 int rescan(int, int); 461 int universal_argument(int, int); 462 int digit_argument(int, int); 463 int negative_argument(int, int); 464 int selfinsert(int, int); 465 int quote(int, int); 466 467 /* main.c */ 468 int ctrlg(int, int); 469 int quit(int, int); 470 471 /* ttyio.c */ 472 void panic(char *); 473 474 /* cinfo.c */ 475 char *getkeyname(char *, size_t, int); 476 477 /* basic.c */ 478 int gotobol(int, int); 479 int backchar(int, int); 480 int gotoeol(int, int); 481 int forwchar(int, int); 482 int gotobob(int, int); 483 int gotoeob(int, int); 484 int forwline(int, int); 485 int backline(int, int); 486 void setgoal(void); 487 int getgoal(struct line *); 488 int forwpage(int, int); 489 int backpage(int, int); 490 int forw1page(int, int); 491 int back1page(int, int); 492 int pagenext(int, int); 493 void isetmark(void); 494 int setmark(int, int); 495 int clearmark(int, int); 496 int swapmark(int, int); 497 int gotoline(int, int); 498 499 /* random.c X */ 500 int showcpos(int, int); 501 int getcolpos(void); 502 int twiddle(int, int); 503 int openline(int, int); 504 int newline(int, int); 505 int deblank(int, int); 506 int justone(int, int); 507 int delwhite(int, int); 508 int delleadwhite(int, int); 509 int deltrailwhite(int, int); 510 int lfindent(int, int); 511 int indent(int, int); 512 int forwdel(int, int); 513 int backdel(int, int); 514 int space_to_tabstop(int, int); 515 int backtoindent(int, int); 516 int joinline(int, int); 517 518 /* extend.c X */ 519 int insert(int, int); 520 int bindtokey(int, int); 521 int localbind(int, int); 522 int redefine_key(int, int); 523 int unbindtokey(int, int); 524 int localunbind(int, int); 525 int extend(int, int); 526 int evalexpr(int, int); 527 int evalbuffer(int, int); 528 int evalfile(int, int); 529 int load(const char *); 530 int excline(char *); 531 532 /* help.c X */ 533 int desckey(int, int); 534 int wallchart(int, int); 535 int help_help(int, int); 536 int apropos_command(int, int); 537 538 /* paragraph.c X */ 539 int gotobop(int, int); 540 int gotoeop(int, int); 541 int fillpara(int, int); 542 int killpara(int, int); 543 int fillword(int, int); 544 int setfillcol(int, int); 545 546 /* word.c X */ 547 int backword(int, int); 548 int forwword(int, int); 549 int upperword(int, int); 550 int lowerword(int, int); 551 int capword(int, int); 552 int delfword(int, int); 553 int delbword(int, int); 554 int inword(void); 555 556 /* region.c X */ 557 int killregion(int, int); 558 int copyregion(int, int); 559 int lowerregion(int, int); 560 int upperregion(int, int); 561 int prefixregion(int, int); 562 int setprefix(int, int); 563 int region_get_data(struct region *, char *, int); 564 void region_put_data(const char *, int); 565 566 /* search.c X */ 567 int forwsearch(int, int); 568 int backsearch(int, int); 569 int searchagain(int, int); 570 int forwisearch(int, int); 571 int backisearch(int, int); 572 int queryrepl(int, int); 573 int forwsrch(void); 574 int backsrch(void); 575 int readpattern(char *); 576 577 /* spawn.c X */ 578 int spawncli(int, int); 579 580 /* ttykbd.c X */ 581 void ttykeymapinit(void); 582 void ttykeymaptidy(void); 583 584 /* match.c X */ 585 int showmatch(int, int); 586 587 /* version.c X */ 588 int showversion(int, int); 589 590 #ifndef NO_MACRO 591 /* macro.c X */ 592 int definemacro(int, int); 593 int finishmacro(int, int); 594 int executemacro(int, int); 595 #endif /* !NO_MACRO */ 596 597 /* modes.c X */ 598 int indentmode(int, int); 599 int fillmode(int, int); 600 int blinkparen(int, int); 601 #ifdef NOTAB 602 int notabmode(int, int); 603 #endif /* NOTAB */ 604 int overwrite_mode(int, int); 605 int set_default_mode(int,int); 606 607 #ifdef REGEX 608 /* re_search.c X */ 609 int re_forwsearch(int, int); 610 int re_backsearch(int, int); 611 int re_searchagain(int, int); 612 int re_queryrepl(int, int); 613 int replstr(int, int); 614 int setcasefold(int, int); 615 int delmatchlines(int, int); 616 int delnonmatchlines(int, int); 617 int cntmatchlines(int, int); 618 int cntnonmatchlines(int, int); 619 #endif /* REGEX */ 620 621 /* undo.c X */ 622 void free_undo_record(struct undo_rec *); 623 int undo_dump(int, int); 624 int undo_enabled(void); 625 int undo_enable(int, int); 626 int undo_add_boundary(int, int); 627 void undo_add_modified(void); 628 int undo_add_insert(struct line *, int, int); 629 int undo_add_delete(struct line *, int, int, int); 630 int undo_boundary_enable(int, int); 631 int undo_add_change(struct line *, int, int); 632 int undo(int, int); 633 634 /* autoexec.c X */ 635 int auto_execute(int, int); 636 PF *find_autoexec(const char *); 637 int add_autoexec(const char *, const char *); 638 639 /* mail.c X */ 640 void mail_init(void); 641 /* cmode.c X */ 642 int cmode(int, int); 643 int cc_brace(int, int); 644 int cc_char(int, int); 645 int cc_tab(int, int); 646 int cc_indent(int, int); 647 int cc_lfindent(int, int); 648 649 /* grep.c X */ 650 int next_error(int, int); 651 int globalwdtoggle(int, int); 652 int compile(int, int); 653 654 /* 655 * Externals. 656 */ 657 extern struct buffer *bheadp; 658 extern struct buffer *curbp; 659 extern struct mgwin *curwp; 660 extern struct mgwin *wheadp; 661 extern int thisflag; 662 extern int lastflag; 663 extern int curgoal; 664 extern int startrow; 665 extern int epresf; 666 extern int sgarbf; 667 extern int mode; 668 extern int nrow; 669 extern int ncol; 670 extern int ttrow; 671 extern int ttcol; 672 extern int tttop; 673 extern int ttbot; 674 extern int tthue; 675 extern int defb_nmodes; 676 extern int defb_flag; 677 extern const char cinfo[]; 678 extern char *keystrings[]; 679 extern char pat[NPAT]; 680 #ifndef NO_DPROMPT 681 extern char prompt[]; 682 #endif /* !NO_DPROMPT */ 683 684 /* 685 * Globals. 686 */ 687 int tceeol; 688 int tcinsl; 689 int tcdell; 690 int rptcount; /* successive invocation count */ 691