home *** CD-ROM | disk | FTP | other *** search
/ Geek Gadgets 1 / ADE-1.bin / ade-dist / jove-4.16-src.tgz / tar.out / bsd / jove / commands.c < prev    next >
C/C++ Source or Header  |  1996-09-28  |  4KB  |  169 lines

  1. /************************************************************************
  2.  * This program is Copyright (C) 1986-1996 by Jonathan Payne.  JOVE is  *
  3.  * provided to you without charge, and with no warranty.  You may give  *
  4.  * away copies of JOVE, including sources, provided that this notice is *
  5.  * included in all the files.                                           *
  6.  ************************************************************************/
  7.  
  8. #include "jove.h"
  9. #include "jctype.h"
  10. #include "extend.h"
  11. #include "macros.h"
  12. #include "mouse.h"
  13.  
  14. /* included for command routine declarationss */
  15. #include "abbrev.h"
  16. /* #include "argcount.h */
  17. /* #include "buf.h" */
  18. #include "c.h"
  19. #include "case.h"
  20. #include "commands.h"
  21. #include "delete.h"
  22. #include "disp.h"
  23. /* #include "extend.h" */
  24. #include "insert.h"
  25. /* #include "io.h" */
  26. #include "sysprocs.h"    /* needed for iproc.h */
  27. #include "iproc.h"
  28. /* #include "jove.h" */
  29. /* #include "keymaps.h" */
  30. /* #include "macros.h" */
  31. #include "marks.h"
  32. #include "misc.h"
  33. #include "move.h"
  34. #include "paragraph.h"
  35. #include "proc.h"
  36. #include "reapp.h"
  37. #include "wind.h"
  38.  
  39. #define    PROC(p)    (p)
  40. #include "commands.tab"
  41.  
  42. data_obj    *LastCmd;
  43. char    *ProcFmt = ": %f ";
  44.  
  45. data_obj *
  46. findcom(prompt)
  47. const char    *prompt;
  48. {
  49.     if (InJoverc) {
  50.         /* This is for faster startup.  This just reads until a space or a
  51.            tab or a newline character is reached, and then does a
  52.            semi-hashed lookup on that string.  This should be much faster
  53.            than initializing the minibuffer for each line. */
  54.         char    cmdbuf[128];
  55.         register const struct cmd    *cmd;
  56.         register char    *cp = cmdbuf;
  57.         register ZXchar    c;
  58.         const struct cmd    *which = NULL;
  59.         size_t    cmdlen;
  60.         static const struct cmd    *cmdhash[26];
  61.         static bool    beenhere = NO;
  62.  
  63. /* special case for prefix commands--only upper case ones */
  64. #define hash(c)    ((c) - 'a')
  65.  
  66.         /* initialize the hash table */
  67.         if (!beenhere) {
  68.             char    lastc = '\0';
  69.  
  70.             for (cmd = commands; cmd->Name != NULL; cmd++) {
  71.                 if (lastc != cmd->Name[0]) {
  72.                     lastc = cmd->Name[0];
  73.                     cmdhash[hash(lastc)] = cmd;
  74.                 }
  75.             }
  76.             beenhere = YES;
  77.         }
  78. #ifdef MAC
  79.         /* ??? Is this necessary?  The input is comming from a file! */
  80.         menus_off();    /* Block menu choices during input */
  81. #endif
  82.         /* gather the cmd name */
  83.         while (jisprint(c = getch()) && c != ' ') {
  84.             *cp++ = CharDowncase(c);
  85.             if (cp == &cmdbuf[sizeof(cmdbuf)])
  86.                 complain("command too long");
  87.         }
  88.         *cp = '\0';
  89.         cmdlen = cp - cmdbuf;
  90.  
  91.         /* look it up (in the reduced search space) */
  92.         c = ZXC(cmdbuf[0]);
  93.         if ('a' <= c && c <= 'z'
  94.         && (cmd = cmdhash[hash(c)]) != NULL) {
  95.             for (; cmd->Name != NULL && cmd->Name[0] == cmdbuf[0]; cmd++) {
  96.             if (strncmp(cmd->Name, cmdbuf, cmdlen) == 0) {
  97.                 if (cmd->Name[cmdlen] == '\0')
  98.                     return (data_obj *) cmd;
  99.                 if (which != NULL)
  100.                     complain("[\"%s\" ambiguous]", cmdbuf);
  101.                 which = cmd;
  102.             }
  103.             }
  104.         }
  105.         if (which == NULL)
  106.             complain("[\"%s\" unknown]", cmdbuf);
  107.         return (data_obj *) which;
  108. #undef    hash
  109.     } else {
  110.         static char    *strings[elemsof(commands)];
  111.         static int    last = -1;
  112.  
  113.         if (strings[0] == NULL) {
  114.             register char    **strs = strings;
  115.             register const struct cmd    *c = commands;
  116.  
  117.             do ; while ((*strs++ = (*c++).Name) != NULL);
  118.         }
  119.         last = complete(strings, last >= 0? strings[last] : (char *)NULL,
  120.             prompt, CASEIND | ALLOW_OLD);
  121.         return (data_obj *) &commands[last];
  122.     }
  123. }
  124.  
  125. const struct cmd *
  126. FindCmd(proc)
  127. register void    (*proc) ptrproto((void));
  128. {
  129.     register const struct cmd    *cp;
  130.  
  131.     for (cp = commands; cp->Name; cp++)
  132.         if (cp->c_proc == proc)
  133.             return cp;
  134.     return NULL;
  135. }
  136.  
  137. void
  138. ExecCmd(cp)
  139. register data_obj    *cp;
  140. {
  141.     LastCmd = cp;
  142.     if (cp->Type & MAJOR_MODE) {
  143.         SetMajor((cp->Type >> MAJOR_SHIFT));
  144.     } else if (cp->Type & MINOR_MODE) {
  145.         TogMinor((cp->Type >> MAJOR_SHIFT));
  146.     } else    switch (cp->Type&TYPEMASK) {
  147.         case MACRO:
  148.             do_macro((struct macro *) cp);
  149.             break;
  150.  
  151.         case COMMAND:
  152.             {
  153.             register struct cmd    *cmd = (struct cmd *) cp;
  154.  
  155.             if (cmd->c_proc != NULL) {
  156.                 if ((cmd->Type & MODIFIER)
  157.                 && BufMinorMode(curbuf, ReadOnly))
  158.                 {
  159.                     rbell();
  160.                     message("[Buffer is read-only]");
  161.                 } else {
  162.                     (*cmd->c_proc)();
  163.                 }
  164.             }
  165.             }
  166.             break;
  167.     }
  168. }
  169.