home *** CD-ROM | disk | FTP | other *** search
/ Frozen Fish 1: Amiga / FrozenFish-Apr94.iso / bbs / alib / d1xx / d131 / mg1b.lha / Mg1b / Source / symbol.c < prev    next >
C/C++ Source or Header  |  1988-03-14  |  16KB  |  522 lines

  1. /*
  2.  * Symbol tables, and keymap setup.
  3.  * The terminal specific parts of building the
  4.  * keymap has been moved to a better place.
  5.  */
  6. #include    "def.h"
  7.  
  8. #ifdef    HASH
  9. Since you're seeing this, you must have defined HASH to try and get the
  10. hashing code back. You're getting an error because I (mwm@ucbvax) want you
  11. to read this.
  12.  
  13. With the change in function completion, there is at least one linear search
  14. through the function list for every hash lookup (ignoring the startup code).
  15. Given that there are probably actually MANY more linear searches for
  16. completion than fullname lookups, some structure other than a hash table is
  17. better suited to this purpose. I suggest trying sorting the lists for more
  18. speed, then going to a binary search tree, and finally going to a trie.
  19. #endif    HASH
  20.  
  21. /*
  22.  * Defined here so to collect the #ifdef MEYN config stuff in one file
  23.  * If you set either MINDENT or MFILL, then you need to change the bindings
  24.  * in this file to match: KCTRL|'M' -> newline-and-indent and KCTRL|'J' ->
  25.  * insert-newline for MINDENT, and ' ' -> insert-with-wrap for MFILL.
  26.  * MEYN is used for compile-time customization of the system for micros.
  27.  */
  28. #ifndef    MEYN
  29. int    mode = 0;            /* All modes off        */
  30. #else
  31. int    mode = MBSMAP|MINDENT;
  32. #endif
  33.  
  34. /*
  35.  * Defined by "main.c".
  36.  */
  37. extern    int    ctrlg();        /* Abort out of things        */
  38. extern    int    quit();            /* Quit                */
  39. extern    int    ctlxlp();        /* Begin macro            */
  40. extern    int    ctlxrp();        /* End macro            */
  41. extern    int    ctlxe();        /* Execute macro        */
  42. extern  int    showversion();        /* Show version numbers, etc.    */
  43. /*
  44.  * Defined by "rexx.c".
  45.  */
  46. extern  int     rexxcommand();          /* Issue REXX command */
  47. extern    int    publishline();        /* Publish mark line */
  48. extern    int    windowfront();
  49. extern    int    windowback();
  50. /*
  51.  * Defined by "kdbmac.c".
  52.  */
  53. extern    int    bindmacrotokey();    /* bind a macro to a key */
  54. /*
  55.  * Defined by "search.c".
  56.  */
  57. extern    int    forwsearch();        /* Search forward        */
  58. extern    int    backsearch();        /* Search backwards        */
  59. extern  int    searchagain();        /* Repeat last search command    */
  60. extern  int    forwisearch();        /* Incremental search forward    */
  61. extern  int    backisearch();        /* Incremental search backwards    */
  62. extern  int    queryrepl();        /* Query replace        */
  63.  
  64. /*
  65.  * Defined by "basic.c".
  66.  */
  67. extern    int    gotobol();        /* Move to start of line    */
  68. extern    int    backchar();        /* Move backward by characters    */
  69. extern    int    gotoeol();        /* Move to end of line        */
  70. extern    int    forwchar();        /* Move forward by characters    */
  71. extern    int    gotobob();        /* Move to start of buffer    */
  72. extern    int    gotoeob();        /* Move to end of buffer    */
  73. extern    int    forwline();        /* Move forward by lines    */
  74. extern    int    backline();        /* Move backward by lines    */
  75. extern    int    forwpage();        /* Move forward by pages    */
  76. extern    int    backpage();        /* Move backward by pages    */
  77. extern    int    pagenext();        /* Page forward next window    */
  78. extern    int    setmark();        /* Set mark            */
  79. extern    int    swapmark();        /* Swap "." and mark        */
  80. extern    int    gotoline();        /* Go to a specified line.    */
  81.  
  82. /*
  83.  * Defined by "buffer.c".
  84.  */
  85. extern    int    listbuffers();        /* Display list of buffers    */
  86. extern    int    usebuffer();        /* Switch a window to a buffer    */
  87. extern    int    poptobuffer();        /* Other window to a buffer    */
  88. extern    int    killbuffer();        /* Make a buffer go away.    */
  89. extern    int    savebuffers();        /* Save unmodified buffers    */
  90. extern    int    bufferinsert();        /* Insert buffer into another    */
  91. extern    int    notmodified();        /* Reset modification flag    */
  92.  
  93. #ifdef    DIRLIST
  94. /*
  95.  * Defined by "dirlist.c".
  96.  */
  97. extern    int    dirlist();        /* Directory list.        */
  98. #endif
  99.  
  100. /*
  101.  * Defined by "file.c".
  102.  */
  103. extern    int    filevisit();        /* Get a file, read write    */
  104. extern    int    poptofile();        /* Get a file, other window    */
  105. extern    int    filewrite();        /* Write a file            */
  106. extern    int    filesave();        /* Save current file        */
  107. extern    int    fileinsert();        /* Insert file into buffer    */
  108. #ifdef    BACKUP
  109. extern    int    makebkfile();        /* Control backups on saves    */
  110. #endif
  111.  
  112. /*
  113.  * Defined by "match.c"
  114.  */
  115. extern    int    blinkparen();        /* Fake blink-matching-paren var */
  116. extern    int    showmatch();        /* Hack to show matching paren     */
  117.  
  118. /*
  119.  * Defined by "random.c".
  120.  */
  121. extern    int    selfinsert();        /* Insert character        */
  122. extern    int    showcpos();        /* Show the cursor position    */
  123. extern    int    twiddle();        /* Twiddle characters        */
  124. extern    int    quote();        /* Insert literal        */
  125. extern    int    openline();        /* Open up a blank line        */
  126. extern    int    newline();        /* Insert CR-LF            */
  127. extern    int    deblank();        /* Delete blank lines        */
  128. extern    int    justone();        /* Delete extra whitespace    */
  129. extern    int    delwhite();        /* Delete all whitespace    */
  130. extern    int    indent();        /* Insert CR-LF, then indent    */
  131. extern    int    forwdel();        /* Forward delete        */
  132. extern    int    backdel();        /* Backward delete in        */
  133. extern    int    killline();        /* Kill forward            */
  134. extern    int    yank();            /* Yank back from killbuffer.    */
  135. extern    int    bsmapmode();        /* set bsmap mode        */
  136. extern    int    flowmode();        /* set flow mode        */
  137. extern    int    indentmode();        /* set auto-indent mode        */
  138. extern    int    fillmode();        /* set word-wrap mode        */
  139.  
  140. /*
  141.  * Defined by "region.c".
  142.  */
  143. extern    int    killregion();        /* Kill region.            */
  144. extern    int    copyregion();        /* Copy region to kill buffer.    */
  145. extern    int    lowerregion();        /* Lower case region.        */
  146. extern    int    upperregion();        /* Upper case region.        */
  147. #ifdef    PREFIXREGION
  148. extern    int    prefixregion();        /* Prefix all lines in region    */
  149. extern    int    setprefix();        /* Set line prefix string    */
  150. #endif
  151.  
  152. /*
  153.  * Defined by "spawn.c".
  154.  */
  155. extern    int    spawncli();        /* Run CLI in a subjob.        */
  156. #ifdef    VMS
  157. extern    int    attachtoparent();    /* Attach to parent process    */
  158. #endif
  159.  
  160. /*
  161.  * Defined by "window.c".
  162.  */
  163. extern    int    reposition();        /* Reposition window        */
  164. extern    int    refresh();        /* Refresh the screen        */
  165. extern    int    nextwind();        /* Move to the next window    */
  166. extern  int    prevwind();        /* Move to the previous window    */
  167. extern    int    onlywind();        /* Make current window only one    */
  168. extern    int    splitwind();        /* Split current window        */
  169. extern    int    delwind();        /* Delete current window    */
  170. extern    int    enlargewind();        /* Enlarge display window.    */
  171. extern    int    shrinkwind();        /* Shrink window.        */
  172.  
  173. /*
  174.  * Defined by "word.c".
  175.  */
  176. extern    int    backword();        /* Backup by words        */
  177. extern    int    forwword();        /* Advance by words        */
  178. extern    int    upperword();        /* Upper case word.        */
  179. extern    int    lowerword();        /* Lower case word.        */
  180. extern    int    capword();        /* Initial capitalize word.    */
  181. extern    int    delfword();        /* Delete forward word.        */
  182. extern    int    delbword();        /* Delete backward word.    */
  183.  
  184. /*
  185.  * Defined by "extend.c".
  186.  */
  187. extern    int    extend();        /* Extended commands.        */
  188. extern    int    desckey();        /* Help key.            */
  189. extern    int    bindtokey();        /* Modify key bindings.        */
  190. extern    int    unsetkey();        /* Unbind a key.        */
  191. extern    int    wallchart();        /* Make wall chart.        */
  192. #ifdef    STARTUP
  193. extern    int    evalexpr();        /* Extended commands (again)    */
  194. extern    int    evalbuffer();        /* Evaluate current buffer    */
  195. extern    int    evalfile();        /* Evaluate a file        */
  196. #endif
  197.  
  198. /*
  199.  * defined by "paragraph.c" - the paragraph justification code.
  200.  */
  201. extern    int    gotobop();        /* Move to start of paragraph.    */
  202. extern    int    gotoeop();        /* Move to end of paragraph.    */
  203. extern    int    fillpara();        /* Justify a paragraph.        */
  204. extern    int    killpara();        /* Delete a paragraph.        */
  205. extern    int    setfillcol();        /* Set fill column for justify.    */
  206. extern    int    fillword();        /* Insert char with word wrap.    */
  207.  
  208. /*
  209.  * defined by prefix.c
  210.  */
  211. extern    int    help();            /* Parse help key.        */
  212. extern    int    ctlx4hack();        /* Parse a pop-to key.        */
  213.  
  214. typedef    struct    {
  215.     KEY    k_key;            /* Key to bind.            */
  216.     int    (*k_funcp)();        /* Function.            */
  217.     char    *k_name;        /* Function name string.    */
  218. }    KEYTAB;
  219.  
  220. /*
  221.  * Default key binding table. This contains
  222.  * the function names, the symbol table name, and (possibly)
  223.  * a key binding for the builtin functions. There are no
  224.  * bindings for C-U or C-X. These are done with special
  225.  * code, but should be done normally.
  226.  */
  227. KEYTAB    key[] = {
  228. #ifdef    MEYN        /* Add meyer's peculiar bindings */
  229.     KCTRL|'J',    newline,    "insert-newline",
  230.     KCTRL|'M',    indent,        "newline-and-indent",
  231.     KCTLX|'N',    nextwind,     "next-window",
  232.     KCTLX|'P',    prevwind,    "previous-window",
  233.     KMETA|KCTRL|'C',quit,        "save-buffers-kill-emacs",
  234.     KMETA|KCTRL|'L',refresh,    "redraw-display",
  235.     KMETA|'G',    gotoline,    "goto-line",
  236.     KMETA|'J',    fillpara,    "fill-paragraph",
  237.     KMETA|'Q',    queryrepl,    "query-replace",
  238. #endif
  239.     KCTRL|'@',    setmark,    "set-mark-command",
  240.     KCTRL|'A',    gotobol,    "beginning-of-line",
  241.     KCTRL|'B',    backchar,    "backward-char",
  242.     KCTRL|'D',    forwdel,    "delete-char",
  243.     KCTRL|'E',    gotoeol,    "end-of-line",
  244.     KCTRL|'F',    forwchar,    "forward-char",
  245.     KCTRL|'G',    ctrlg,        "keyboard-quit",
  246.     KCTRL|'H',    help,        "help",
  247.     KCTRL|'I',    selfinsert,    "self-insert-command",
  248. #ifndef    MEYN
  249.     KCTRL|'J',    indent,        "newline-and-indent",
  250. #endif
  251.     KCTRL|'L',    reposition,    "recenter",
  252.     KCTRL|'K',    killline,    "kill-line",
  253. #ifndef    MEYN
  254.     KCTRL|'M',    newline,    "insert-newline",
  255. #endif
  256.     KCTRL|'N',    forwline,    "next-line",
  257.     KCTRL|'O',    openline,    "open-line",
  258.     KCTRL|'P',    backline,    "previous-line",
  259.     KCTRL|'Q',    quote,        "quoted-insert",
  260.     KCTRL|'R',    backisearch,    "isearch-backward",
  261.     KCTRL|'S',    forwisearch,    "isearch-forward",
  262.     KCTRL|'T',    twiddle,    "transpose-chars",
  263.     KCTRL|'V',    forwpage,    "scroll-up",
  264.     KCTRL|'W',    killregion,    "kill-region",
  265.     KCTRL|'Y',    yank,        "yank",
  266. #ifdef    VMS
  267.     KCTRL|'Z',    attachtoparent,    "suspend-emacs",
  268. #else
  269.     KCTRL|'Z',    spawncli,    "suspend-emacs",
  270. #endif
  271.     KCTLX|KCTRL|'B',listbuffers,    "list-buffers",
  272. #ifndef    MEYN
  273.     KCTLX|KCTRL|'C',quit,        "save-buffers-kill-emacs",
  274. #endif
  275. #ifdef    DIRLIST
  276.     KCTLX|KCTRL|'D',dirlist,    "display-directory",
  277. #endif
  278.     KCTLX|KCTRL|'F',filevisit,    "find-file",
  279.     KCTLX|KCTRL|'L',lowerregion,    "downcase-region",
  280.     KCTLX|KCTRL|'O',deblank,    "delete-blank-lines",
  281.     KCTLX|KCTRL|'S',filesave,    "save-buffer",
  282.     KCTLX|KCTRL|'U',upperregion,    "upcase-region",
  283.     KCTLX|KCTRL|'W',filewrite,    "write-file",
  284.     KCTLX|KCTRL|'X',swapmark,    "exchange-point-and-mark",
  285.     KCTLX|'=',    showcpos,    "what-cursor-position",
  286.     KCTLX|'(',    ctlxlp,        "start-kbd-macro",
  287.     KCTLX|')',    ctlxrp,        "end-kbd-macro",
  288.     KCTLX|'^',    enlargewind,    "enlarge-window",
  289.     KCTLX|'0',    delwind,    "delete-window",
  290.     KCTLX|'1',    onlywind,    "delete-other-windows",
  291.     KCTLX|'2',    splitwind,    "split-window-vertically",
  292.     KCTLX|'4',    ctlx4hack,    "ctrlx-four-hack",
  293.     KCTLX|'B',    usebuffer,    "switch-to-buffer",
  294.     KCTLX|'E',    ctlxe,        "call-last-kbd-macro",
  295.     KCTLX|'F',    setfillcol,    "set-fill-column",
  296.     KCTLX|'I',    fileinsert,    "insert-file",
  297.     KCTLX|'K',    killbuffer,    "kill-buffer",
  298.     KCTLX|'S',    savebuffers,    "save-some-buffers",
  299. #ifndef    MEYN
  300.     KCTLX|'O',    nextwind,    "next-window",
  301.     KMETA|'%',    queryrepl,    "query-replace",
  302. #endif
  303.     KMETA|KCTRL|'V',pagenext,    "scroll-other-window",
  304.     KMETA|'>',    gotoeob,    "end-of-buffer",
  305.     KMETA|'<',    gotobob,    "beginning-of-buffer",
  306.     KMETA|'[',    gotobop,    "backward-paragraph",
  307.     KMETA|'\\',    delwhite,    "delete-horizontal-space",
  308.     KMETA|']',    gotoeop,    "forward-paragraph",
  309.     KMETA|' ',    justone,    "just-one-space",
  310.     KMETA|'B',    backword,    "backward-word",
  311.     KMETA|'C',    capword,    "capitalize-word",
  312.     KMETA|'D',    delfword,    "kill-word",
  313.     KMETA|'F',    forwword,    "forward-word",
  314.     KMETA|'L',    lowerword,    "downcase-word",
  315. #ifndef    MEYN
  316.     KMETA|'Q',    fillpara,    "fill-paragraph",
  317. #endif
  318.     KMETA|'R',    backsearch,    "search-backward",
  319.     KMETA|'S',    forwsearch,    "search-forward",
  320.     KMETA|'U',    upperword,    "upcase-word",
  321.     KMETA|'V',    backpage,    "scroll-down",
  322.     KMETA|'W',    copyregion,    "copy-region-as-kill",
  323.     KMETA|'X',    extend,        "execute-extended-command",
  324.     KMETA|'~',    notmodified,    "not-modified",
  325. #ifndef    MEYN
  326.     -1,        prevwind,    "previous-window",
  327.     -1,        refresh,    "redraw-display",
  328.     -1,        gotoline,    "goto-line",
  329. #endif
  330. #ifdef    STARTUP
  331.     -1,        evalexpr,    "eval-expression",
  332.     -1,        evalbuffer,    "eval-current-buffer",
  333.     -1,        evalfile,    "load",
  334. #endif
  335.     -1,        bsmapmode,    "bsmap-mode",
  336.     -1,        flowmode,    "flow-mode",
  337.     -1,        indentmode,    "auto-indent-mode",
  338.     -1,        fillmode,    "auto-fill-mode",
  339.     -1,        fillword,    "insert-with-wrap",
  340.     -1,        shrinkwind,    "shrink-window",
  341.     -1,        searchagain,    "search-again",
  342.     -1,        unsetkey,    "global-unset-key",
  343.     -1,        bindtokey,    "global-set-key",
  344.     -1,        bindmacrotokey, "global-set-key-macro",
  345.     -1,        killpara,    "kill-paragraph",
  346.     -1,        showversion,    "emacs-version",
  347.     -1,        blinkparen,    "blink-matching-paren",
  348.     -1,        showmatch,    "blink-matching-paren-hack",
  349.     -1,        bufferinsert,    "insert-buffer",
  350.     -1,        rexxcommand,    "rexx",
  351.     -1,        publishline,    "post-line",
  352.     -1,        windowfront,    "window-to-front",
  353.     -1,        windowback,    "window-to-back",
  354. #ifdef    VMS
  355.     -1,        spawncli,    "push-to-dcl",
  356. #endif
  357. #ifdef    PREFIXREGION
  358.     -1,        prefixregion,    "prefix-region",
  359.     -1,        setprefix,    "set-prefix-string",
  360. #endif
  361. #ifdef    BACKUP
  362.     -1,        makebkfile,    "make-backup-files",
  363. #endif
  364.     /* You can actually get to these with keystrokes. See prefix.c */
  365.     -1,        poptobuffer,    "switch-to-buffer-other-window",
  366.     -1,        poptofile,    "find-file-other-window",
  367.     -1,        desckey,    "describe-key-briefly",
  368.     -1,        wallchart,    "describe-bindings",
  369. };
  370.  
  371. #define    NKEY    (sizeof(key) / sizeof(key[0]))
  372.  
  373. /*
  374.  * Just some definitions, to keep ANSI compilers happy.
  375.  */
  376. VOID    keymapinit();
  377. VOID    keyadd();
  378. VOID    keydup();
  379.  
  380. /*
  381.  * Symbol table lookup.
  382.  * Return a pointer to the SYMBOL node, or NULL if
  383.  * the symbol is not found.
  384.  */
  385. SYMBOL    *
  386. symlookup(cp) register char *cp; {
  387.     register SYMBOL    *sp;
  388.  
  389. #ifdef    HASH
  390.     sp = symbol[symhash(cp)];
  391. #else
  392.     sp = symbol[0];
  393. #endif
  394.     while (sp != NULL) {
  395.         if (strcmp(cp, sp->s_name) == 0)
  396.             return (sp);
  397. #ifdef    HASH
  398.         if ((sp->s_flags&SFEND) != 0) break;
  399. #endif
  400.         sp = sp->s_symp;
  401.     }
  402.     return (NULL);
  403. }
  404.  
  405. #ifdef    HASH
  406. /*
  407.  * Take a string, and compute the symbol table
  408.  * bucket number. This is done by adding all of the characters
  409.  * together, and taking the sum mod NSHASH. The string probably
  410.  * should not contain any GR characters; if it does the "*cp"
  411.  * may get a nagative number on some machines, and the "%"
  412.  * will return a negative number!
  413.  */
  414. symhash(cp) register char *cp; {
  415.     register int    c;
  416.     register int    n;
  417.  
  418.     n = 0;
  419.     while ((c = *cp++) != 0)
  420.         n += c;
  421.     return (n % NSHASH);
  422. }
  423. #endif
  424.  
  425. /*
  426.  * Build initial keymap. The funny keys
  427.  * (commands, odd control characters) are mapped using
  428.  * a big table and calls to "keyadd". The printing characters
  429.  * are done with some do-it-yourself handwaving. The terminal
  430.  * specific keymap initialization code is called at the
  431.  * very end to finish up. All errors are fatal.
  432.  */
  433. VOID
  434. keymapinit() {
  435.     register SYMBOL    *sp;
  436.     register KEYTAB    *kp;
  437.     register int    i;
  438.  
  439.     for (i=0; i<NKEYS; ++i)
  440.         binding[i] = NULL;
  441.     for (kp = &key[0]; kp < &key[NKEY]; ++kp)
  442.         keyadd(kp->k_key, kp->k_funcp, kp->k_name);
  443.     keydup((KEY) (KCTLX|KCTRL|'G'),    "keyboard-quit");
  444.     keydup((KEY) (KMETA|KCTRL|'G'),    "keyboard-quit");
  445.     keyadd((KEY) (KMETA|0x7F), delbword,
  446.                 "backward-kill-word");
  447.     keyadd((KEY) 0x7F, backdel,    "backward-delete-char");
  448.     /*
  449.      * add duplicates (GNU-stuff)
  450.      */
  451.     keydup((KEY) (KCTLX|KCTRL|'Z'), "suspend-emacs");
  452.     /*
  453.      * Should be bound by "tab" already.
  454.      */
  455.     if ((sp=symlookup("self-insert-command")) == NULL)
  456.         panic("no self-insert-command in keymapinit");
  457.     for (i=0x20; i<0x7F; ++i) {
  458.         if (binding[i] != NULL)
  459.             panic("nonull binding in keymapinit");
  460.         binding[i] = sp;
  461.     }
  462.     ttykeymapinit();
  463. #ifdef    HASH
  464.     /* Link up the symbol table entries    */
  465.     for (sp = symbol[i = 0]; i < NSHASH-1; sp = sp->s_symp)
  466.         if (sp->s_symp == NULL) sp->s_symp = symbol[++i];
  467. #endif            
  468. }
  469.  
  470. /*
  471.  * Create a new builtin function "name"
  472.  * with function "funcp". If the "new" is a real
  473.  * key, bind it as a side effect. All errors
  474.  * are fatal.
  475.  */
  476. VOID
  477. keyadd(new, funcp, name) register KEY new; int (*funcp)(); char *name; {
  478.     register SYMBOL    *sp;
  479. #ifdef    HASH
  480.     register int    hash;
  481. #endif
  482.  
  483.     if ((sp=(SYMBOL *)malloc(sizeof(SYMBOL))) == NULL)
  484.         panic("No memory");
  485. #ifdef    HASH
  486.     hash = symhash(name);
  487.     if (symbol[hash] == NULL) sp->s_flags |= SFEND;
  488.     sp->s_symp = symbol[hash];
  489.     symbol[hash] = sp;
  490. #else
  491.     sp->s_symp = symbol[0];
  492.         symbol[0] = sp;
  493. #endif
  494.     sp->s_name = name;
  495.     sp->s_funcp = funcp;
  496.     if (new >= 0) {                /* Bind this key.    */
  497.         if (binding[new] != NULL)
  498.             panic("rebinding old symbol");
  499.         binding[new] = sp;
  500.     }
  501. }
  502.  
  503. /*
  504.  * Bind key "new" to the existing
  505.  * routine "name". If the name cannot be found,
  506.  * or the key is already bound, abort.
  507.  */
  508. VOID
  509. keydup(new, name) register KEY new; char *name; {
  510.     register SYMBOL    *sp;
  511.  
  512.     if (binding[new]!=NULL || (sp=symlookup(name))==NULL) {
  513. #ifdef    KEYDUP_ERROR
  514.         fprintf (stderr, "keydup: binding[%d] = %x",
  515.                 new, binding[new]);
  516.         fprintf (stderr, " and symlookup(%s) == %x\n", name, sp);
  517. #endif
  518.         panic("keydup");
  519.     }
  520.     binding[new] = sp;
  521. }
  522.