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