home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / UE311C.ZIP / MAIN.C < prev    next >
C/C++ Source or Header  |  1991-10-10  |  24KB  |  1,044 lines

  1. /*
  2.  *    MicroEMACS 3.11
  3.  *        written by Daniel M. Lawrence
  4.  *        based on code by Dave G. Conroy.
  5.  *
  6.  *    (C)Copyright 1988,1989,1990,1991 by Daniel M. Lawrence
  7.  *    MicroEMACS 3.11 can be copied and distributed freely for any
  8.  *    non-commercial purposes. MicroEMACS 3.11 can only be incorporated
  9.  *    into commercial software with the permission of the current author.
  10.  *
  11.  * This file contains the main driving routine, and some keyboard processing
  12.  * code, for the MicroEMACS screen editor.
  13.  *
  14.  */
  15.  
  16. #include    <stdio.h>
  17.  
  18. /* make global definitions not external */
  19. #define maindef
  20.  
  21. #include    "estruct.h"    /* global structures and defines */
  22. #include    "eproto.h"    /* variable prototype definitions */
  23. #include    "efunc.h"    /* function declarations and name table */
  24. #include    "edef.h"    /* global definitions */
  25. #include    "elang.h"    /* human language definitions */
  26. #include    "ebind.h"    /* default key bindings */
  27.  
  28. /* for many different systems, increase the default stack space */
  29.  
  30. #if    MSDOS && LATTICE
  31. unsigned _stack = 20000;
  32. #endif
  33.  
  34. #if    MSDOS && (DTL | ZTC)
  35. int    _okbigbuf = 0;        /* Only allocate memory when needed.*/
  36. int    _stack = 20000;     /* Reset the ol' stack size.*/
  37. #endif
  38.  
  39. #if    TOS && MWC
  40. long _stksize = 20000L;     /* reset stack size (must be even) */
  41. #endif
  42.  
  43. #if    MSDOS && AZTEC
  44. int _STKSIZ = 20000/16;     /* stack size in paragraphs */
  45. int _STKRED = 1024;        /* stack checking limit */
  46. int _HEAPSIZ = 4096/16;     /* (in paragraphs) */
  47. /*int _STKLOW = 0;        default is stack above heap (small only) */
  48. #endif
  49.  
  50. #if    MSDOS && TURBO
  51. extern unsigned int _stklen = 10000;
  52. #endif
  53.  
  54. /*    make VMS happy...    */
  55.  
  56. #if    VMS
  57. #include    <ssdef.h>
  58. #define GOOD    (SS$_NORMAL)
  59. #endif
  60.  
  61. #ifndef GOOD
  62. #define GOOD    0
  63. #endif
  64.  
  65. /*
  66.     This is the primary entry point that is used by command line
  67.     invocation, and by applications that link with microemacs in
  68.     such a way that each invocation of Emacs is a fresh environment.
  69.  
  70.     There is another entry point in VMS.C that is used when
  71.     microemacs is "linked" (In quotes, because it is a run-time link
  72.     rather than a link-time link.) with applications that wish Emacs
  73.     to preserve it's context across invocations.  (For example,
  74.     EMACS.RC is only executed once per invocation of the
  75.     application, instead of once per invocation of Emacs.)
  76.  
  77.     Note that re-entering an Emacs that is saved in a kept
  78.     subprocess would require a similar entrypoint.
  79. */
  80.  
  81. #if    CALLED
  82. emacs(argc, argv)
  83. #else
  84. main(argc, argv)
  85. #endif
  86.  
  87. int argc;    /* # of arguments */
  88. char *argv[];    /* argument strings */
  89.  
  90. {
  91.     register int status;
  92.  
  93.     /* Initialize the editor */
  94.     eexitflag = FALSE;
  95.     vtinit();        /* Terminal */
  96.  
  97.     if (eexitflag)
  98.         goto abortrun;
  99.     edinit(mainbuf);      /* Buffers, windows, screens */
  100.  
  101.     varinit();        /* user variables */
  102.     initchars();        /* character set definitions */
  103.  
  104.     /* Process the command line and let the user edit */
  105. #if    VMS
  106.     expandargs(&argc, &argv);    /* expand VMS wildcards.*/
  107. #endif
  108.     dcline(argc, argv, TRUE);
  109.     status = editloop();
  110. abortrun:
  111.     vttidy();
  112. #if    CLEAN
  113.     clean();
  114. #endif
  115. #if    CALLED
  116.     return(status);
  117. #else
  118.     exit(status);
  119. #endif
  120. }
  121.  
  122. #if    CLEAN
  123. /*
  124.     On some primitive operation systems, and when emacs is used as
  125.     a subprogram to a larger project, emacs needs to de-alloc its
  126.     own used memory, otherwise we just exit.
  127. */
  128.  
  129. PASCAL NEAR clean()
  130. {
  131.     register BUFFER *bp;    /* buffer list pointer */
  132.     register SCREEN *scrp;    /* ptr to screen to dump */
  133.  
  134.     /* first clean up the screens */
  135.     scrp = first_screen;
  136.     while (scrp) {
  137.         first_screen = sp->s_next_screen;
  138.         free_screen(scrp);
  139.         scrp = first_screen;
  140.     }
  141.     wheadp = NULL;
  142.  
  143.     /* then the buffers */
  144.     bp = bheadp;
  145.     while (bp) {
  146.         bp->b_nwnd = 0;
  147.         bp->b_flag = 0; /* don't say anything about a changed buffer! */
  148.         zotbuf(bp);
  149.         bp = bheadp;
  150.     }
  151.  
  152.     /* and the kill buffer */
  153.     kdelete();
  154.  
  155.     /* clear some search variables */
  156. #if    MAGIC
  157.     mcclear();
  158.     rmcclear();
  159. #endif
  160.     if (patmatch != NULL) {
  161.         free(patmatch);
  162.         patmatch = NULL;
  163.     }
  164.  
  165.     /* dealloc the user variables */
  166.     varclean();
  167.  
  168.     /* and the video buffers */
  169.     vtfree();
  170. }
  171. #endif
  172.  
  173. /*    Process a command line.   May be called any time.    */
  174.  
  175. PASCAL NEAR dcline(argc, argv, firstflag)
  176.  
  177. int argc;
  178. char *argv[];
  179. int firstflag;    /* is this the first time in? */
  180.  
  181. {
  182.     register BUFFER *bp;        /* temp buffer pointer */
  183.     register int    firstfile;    /* first file flag */
  184.     register int    carg;        /* current arg to scan */
  185.     register int    startflag;    /* startup executed flag */
  186.     BUFFER *firstbp = NULL;     /* ptr to first buffer in cmd line */
  187.     int viewflag;            /* are we starting in view mode? */
  188.     int gotoflag;            /* do we need to goto a line at start? */
  189.     int gline;            /* if so, what line? */
  190.     int searchflag;         /* Do we need to search at start? */
  191.     int errflag;            /* C error processing? */
  192.     VDESC vd;            /* variable num/type */
  193.     char bname[NBUFN];        /* buffer name of file to read */
  194.  
  195. #if    CRYPT
  196.     int cryptflag;            /* encrypting on the way in? */
  197.     char ekey[NPAT];        /* startup encryption key */
  198. #endif
  199.     CONST NOSHARE extern *pathname[]; /* startup file path/name array */
  200.  
  201.     viewflag = FALSE;    /* view mode defaults off in command line */
  202.     gotoflag = FALSE;    /* set to off to begin with */
  203.     searchflag = FALSE;    /* set to off to begin with */
  204.     firstfile = TRUE;    /* no file to edit yet */
  205.     startflag = FALSE;    /* startup file not executed yet */
  206.     errflag = FALSE;    /* not doing C error parsing */
  207.     exec_error = FALSE;    /* no macro error pending */
  208. #if    CRYPT
  209.     cryptflag = FALSE;    /* no encryption by default */
  210. #endif
  211.     disphigh = FALSE;    /* don't escape high bit characters */
  212.     lterm[0] = 0;        /* standard line terminators */
  213.  
  214.     /* Parse a command line */
  215.     for (carg = 1; carg < argc; ++carg) {
  216.  
  217.         /* Process Switches */
  218. #if WMCS
  219.         if (argv[carg][0] == ':') {
  220. #else
  221.         if (argv[carg][0] == '-') {
  222. #endif
  223.             switch (argv[carg][1]) {
  224.                 /* Process Startup macroes */
  225.                 case 'c':    /* -c for changable file */
  226.                 case 'C':
  227.                     viewflag = FALSE;
  228.                     break;
  229.                 case 'e':    /* -e process error file */
  230.                 case 'E':
  231.                     errflag = TRUE;
  232.                     break;
  233.                 case 'g':    /* -g for initial goto */
  234.                 case 'G':
  235.                     gotoflag = TRUE;
  236.                     gline = asc_int(&argv[carg][2]);
  237.                     break;
  238.                 case 'i':    /* -i<var> <value> set an initial */
  239.                 case 'I':    /* value for a variable */
  240.                     bytecopy(bname, &argv[carg][2], NVSIZE);
  241.                     findvar(bname, &vd, NVSIZE + 1);
  242.                     if (vd.v_type == -1) {
  243.                         mlwrite(TEXT52, bname);
  244. /*                            "%%No such variable as '%s'" */
  245.                         break;
  246.                     }
  247.                     svar(&vd, argv[++carg]);
  248.                     break;
  249. #if    CRYPT
  250.                 case 'k':    /* -k<key> for code key */
  251.                 case 'K':
  252.                     cryptflag = TRUE;
  253.                     strcpy(ekey, &argv[carg][2]);
  254.                     break;
  255. #endif
  256.                 case 'r':    /* -r restrictive use */
  257.                 case 'R':
  258.                     restflag = TRUE;
  259.                     break;
  260.                 case 's':    /* -s for initial search string */
  261.                 case 'S':
  262.                     searchflag = TRUE;
  263.                     bytecopy(pat,&argv[carg][2],NPAT);
  264.                     setjtable();
  265.                     break;
  266.                 case 'v':    /* -v for View File */
  267.                 case 'V':
  268.                     viewflag = TRUE;
  269.                     break;
  270.                 default:    /* unknown switch */
  271.                     /* ignore this for now */
  272.                     break;
  273.             }
  274.  
  275.         } else if (argv[carg][0]== '@') {
  276.  
  277.             /* Process Startup macroes */
  278.             if (startup(&argv[carg][1]) == TRUE)
  279.                 /* don't execute emacs.rc */
  280.                 startflag = TRUE;
  281.  
  282.         } else {
  283.  
  284.             /* Process an input file */
  285.  
  286.             /* set up a buffer for this file */
  287.             makename(bname, argv[carg]);
  288.             unqname(bname);
  289.  
  290.             /* set this to inactive */
  291.             bp = bfind(bname, TRUE, 0);
  292.             strcpy(bp->b_fname, argv[carg]);
  293.             bp->b_active = FALSE;
  294.             if (firstfile) {
  295.                 firstbp = bp;
  296.                 firstfile = FALSE;
  297.             }
  298.  
  299.             /* set the modes appropriatly */
  300.             if (viewflag)
  301.                 bp->b_mode |= MDVIEW;
  302. #if    CRYPT
  303.             if (cryptflag) {
  304.                 bp->b_mode |= MDCRYPT;
  305.                 crypt((char *)NULL, 0);
  306.                 crypt(ekey, strlen(ekey));
  307.                 bytecopy(bp->b_key, ekey, NPAT);
  308.             }
  309. #endif
  310.         }
  311.     }
  312.  
  313.     /* if we are C error parsing... run it! */
  314.     if (errflag) {
  315.         if (startup("error.cmd") == TRUE)
  316.             startflag = TRUE;
  317.     }
  318.  
  319.     /* if invoked with no other startup files,
  320.        run the system startup file here */
  321.     if (firstflag && startflag == FALSE)
  322.         startup("");
  323.  
  324.     /* if there are any files to read, read the first one! */
  325.     if (firstflag) {
  326.         bp = bfind(mainbuf, FALSE, 0);
  327.         if (firstfile == FALSE && (gflags & GFREAD)) {
  328.             swbuffer(firstbp);
  329.             curbp->b_mode |= gmode;
  330.             update(TRUE);
  331.             mlwrite(lastmesg);
  332.             zotbuf(bp);
  333.         } else
  334.             bp->b_mode |= gmode;
  335.     } else {
  336.         swbuffer(firstbp);
  337.         curbp->b_mode |= gmode;
  338.         update(TRUE);
  339.         mlwrite(lastmesg);
  340.     }
  341.  
  342.  
  343.     /* Deal with startup gotos and searches */
  344.     if (gotoflag && searchflag) {
  345.         update(FALSE);
  346.         mlwrite(TEXT101);
  347. /*            "[Can not search and goto at the same time!]" */
  348.     }
  349.     else if (gotoflag) {
  350.         if (gotoline(TRUE,gline) == FALSE) {
  351.             update(FALSE);
  352.             mlwrite(TEXT102);
  353. /*                "[Bogus goto argument]" */
  354.         }
  355.     } else if (searchflag) {
  356.         if (forwhunt(FALSE, 0) == FALSE)
  357.             update(FALSE);
  358.     }
  359.  
  360. }
  361.  
  362. /*
  363.     This is called to let the user edit something.    Note that if you
  364.     arrange to be able to call this from a macro, you will have
  365.     invented the "recursive-edit" function.
  366. */
  367.  
  368. PASCAL NEAR editloop()
  369.  
  370. {
  371.     register int c;        /* command character */
  372.     register int f;     /* default flag */
  373.     register int n;     /* numeric repeat count */
  374.     register int mflag;    /* negative flag on repeat */
  375.     register int basec;    /* c stripped of meta character */
  376.     register int oldflag;    /* old last flag value */
  377.  
  378.     /* setup to process commands */
  379.     lastflag = 0;        /* Fake last flags.    */
  380.  
  381. loop:
  382.     /* if a macro error is pending, wait for a character */
  383.     if (exec_error) {
  384.         mlforce(TEXT227);
  385. /*            "\n--- Press any key to Continue ---" */
  386.         tgetc();
  387.         sgarbf = TRUE;
  388.         update(FALSE);
  389.         mlferase();
  390.         exec_error = FALSE;
  391.     }
  392.  
  393.     /* if we were called as a subroutine and want to leave, do so */
  394.     if (eexitflag)
  395.         return(eexitval);
  396.  
  397.     /* execute the "command" macro...normally null */
  398.     oldflag = lastflag;    /* preserve lastflag through this */
  399.     execkey(&cmdhook, FALSE, 1);
  400.     lastflag = oldflag;
  401.  
  402. #if    VMS
  403.     if (pending_msg) {
  404.         makelit(brdcstbuf);
  405.         mlwrite(brdcstbuf);
  406.         pending_msg = FALSE;
  407.     }
  408. #endif
  409.  
  410.     /* Fix up the screen    */
  411.     update(FALSE);
  412.  
  413.     /* get the next command from the keyboard */
  414.     discmd = TRUE;
  415.     disinp = TRUE;
  416.     c = getkey();
  417.  
  418.     /* if there is something on the command line, clear it */
  419.     if (mpresf != FALSE) {
  420.         mlerase();
  421.         update(FALSE);
  422.     }
  423.  
  424.     /* override the arguments if prefixed */
  425.     if (prefix) {
  426.         if (islower(c & 255))
  427.             c = (c & ~255) | upperc(c & 255);
  428.         c |= prefix;
  429.         f = predef;
  430.         n = prenum;
  431.         prefix = 0;
  432.     } else {
  433.         f = FALSE;
  434.         n = 1;
  435.     }
  436.  
  437.     /* do META-# processing if needed */
  438.  
  439.     basec = c & ~META;        /* strip meta char off if there */
  440.     if ((c & META) && ((basec >= '0' && basec <= '9') || basec == '-') &&
  441.         (getbind(c) == NULL)) {
  442.         f = TRUE;        /* there is a # arg */
  443.         n = 0;            /* start with a zero default */
  444.         mflag = 1;        /* current minus flag */
  445.         c = basec;        /* strip the META */
  446.         while ((c >= '0' && c <= '9') || (c == '-')) {
  447.             if (c == '-') {
  448.                 /* already hit a minus or digit? */
  449.                 if ((mflag == -1) || (n != 0))
  450.                     break;
  451.                 mflag = -1;
  452.             } else {
  453.                 n = n * 10 + (c - '0');
  454.             }
  455.             if ((n == 0) && (mflag == -1))    /* lonely - */
  456.                 mlwrite("Arg:");
  457.             else
  458.                 mlwrite("Arg: %d",n * mflag);
  459.  
  460.             c = getkey();    /* get the next key */
  461.         }
  462.         n = n * mflag;    /* figure in the sign */
  463.     }
  464.  
  465.     /* do ^U repeat argument processing */
  466.  
  467.     if (c == reptc) {           /* ^U, start argument   */
  468.         f = TRUE;
  469.         n = 4;                /* with argument of 4 */
  470.         mflag = 0;            /* that can be discarded. */
  471.         mlwrite("Arg: 4");
  472.         while ((c=getkey()) >='0' && c<='9' || c==reptc || c=='-') {
  473.             if (c == reptc)
  474.                 if ((n > 0) == ((n*4) > 0))
  475.                     n = n*4;
  476.                 else
  477.                     n = 1;
  478.             /*
  479.              * If dash, and start of argument string, set arg.
  480.              * to -1.  Otherwise, insert it.
  481.              */
  482.             else if (c == '-') {
  483.                 if (mflag)
  484.                     break;
  485.                 n = 0;
  486.                 mflag = -1;
  487.             }
  488.             /*
  489.              * If first digit entered, replace previous argument
  490.              * with digit and set sign.  Otherwise, append to arg.
  491.              */
  492.             else {
  493.                 if (!mflag) {
  494.                     n = 0;
  495.                     mflag = 1;
  496.                 }
  497.                 n = 10*n + c - '0';
  498.             }
  499.             mlwrite("Arg: %d", (mflag >=0) ? n : (n ? -n : -1));
  500.         }
  501.         /*
  502.          * Make arguments preceded by a minus sign negative and change
  503.          * the special argument "^U -" to an effective "^U -1".
  504.          */
  505.         if (mflag == -1) {
  506.             if (n == 0)
  507.                 n++;
  508.             n = -n;
  509.         }
  510.     }
  511.  
  512.     /* and execute the command */
  513.     execute(c, f, n);
  514.     goto loop;
  515. }
  516.  
  517. /*
  518.  * Initialize all of the buffers, windows and screens. The buffer name is
  519.  * passed down as an argument, because the main routine may have been told
  520.  * to read in a file by default, and we want the buffer name to be right.
  521.  */
  522.  
  523. PASCAL NEAR edinit(bname)
  524.  
  525. char bname[];    /* name of buffer to initialize */
  526.  
  527. {
  528.     register BUFFER *bp;
  529.  
  530.     /* initialize some important globals */
  531.  
  532.     readhook.k_ptr.fp = nullproc;    /* set internal hooks to OFF */
  533.     readhook.k_type = BINDFNC;
  534.     wraphook.k_ptr.fp = wrapword;
  535.     wraphook.k_type = BINDFNC;
  536.     cmdhook.k_ptr.fp = nullproc;
  537.     cmdhook.k_type = BINDFNC;
  538.     writehook.k_ptr.fp = nullproc;
  539.     writehook.k_type = BINDFNC;
  540.     bufhook.k_ptr.fp = nullproc;
  541.     bufhook.k_type = BINDFNC;
  542.     exbhook.k_ptr.fp = nullproc;
  543.     exbhook.k_type = BINDFNC;
  544.  
  545.     /* allocate the first buffer */
  546.     bp = bfind(bname, TRUE, 0);        /* First buffer     */
  547.     blistp = bfind("[List]", TRUE, BFINVS); /* Buffer list buffer    */
  548.     slistp = bfind("[Screens]", TRUE, BFINVS); /* Buffer list buffer    */
  549.     if (bp==NULL || blistp==NULL)
  550.         meexit(1);
  551.  
  552.     /* and allocate the default screen */
  553.     first_screen = (SCREEN *)NULL;
  554.     init_screen("MAIN", bp);
  555.     if (first_screen == (SCREEN *)NULL)
  556.         meexit(1);
  557.  
  558.     /* set the current default screen/buffer/window */
  559.     curbp = bp;
  560.     curwp = wheadp = first_screen->s_cur_window = first_screen->s_first_window;
  561. }
  562.  
  563. /*
  564.  * This is the general command execution routine. It handles the fake binding
  565.  * of all the keys to "self-insert". It also clears out the "thisflag" word,
  566.  * and arranges to move it to the "lastflag", so that the next command can
  567.  * look at it. Return the status of command.
  568.  */
  569. PASCAL NEAR execute(c, f, n)
  570.  
  571. int c;        /* key to execute */
  572. int f;        /* prefix argument flag */
  573. int n;        /* prefix value */
  574.  
  575. {
  576.     register int status;
  577.     KEYTAB *key;        /* key entry to execute */
  578. #if    DBCS
  579.     int schar;        /* second key in 2 byte sequence */
  580. #endif
  581.  
  582.     /* if the keystroke is a bound function...do it */
  583.     key = getbind(c);
  584.     if (key != NULL) {
  585.  
  586.         /* Don't reset the function type flags on a prefix */
  587.         if ((key->k_type == BINDFNC) &&
  588.             ((key->k_ptr.fp == meta) || (key->k_ptr.fp == cex)))
  589.             status = execkey(key, f, n);
  590.         else {
  591.             thisflag = 0;
  592.             status = execkey(key, f, n);
  593.             lastflag = thisflag;
  594.         }
  595.  
  596.         return(status);
  597.     }
  598.  
  599.     /*
  600.      * If a space was typed, fill column is defined, the argument is non-
  601.      * negative, wrap mode is enabled, and we are now past fill column,
  602.      * and we are not read-only, perform word wrap.
  603.      */
  604.     if (c == ' ' && (curwp->w_bufp->b_mode & MDWRAP) && fillcol > 0 &&
  605.         n >= 0 && getccol(FALSE) > fillcol &&
  606.         (curwp->w_bufp->b_mode & MDVIEW) == FALSE)
  607.         execkey(&wraphook, FALSE, 1);
  608.  
  609.     if ((c>=0x20 && c<=0xFF)) {    /* Self inserting.    */
  610.         if (n <= 0) {            /* Fenceposts.        */
  611.             lastflag = 0;
  612.             return(n<0 ? FALSE : TRUE);
  613.         }
  614.         thisflag = 0;            /* For the future.    */
  615.  
  616.         /* replace or overwrite mode, not at the end of a string */
  617.         if (curwp->w_bufp->b_mode & (MDREPL | MDOVER) &&
  618.             curwp->w_doto < curwp->w_dotp->l_used) {
  619.  
  620.             /* if we are in replace mode, or
  621.                (next char is not a tab or we are at a tab stop) */
  622.             if (curwp->w_bufp->b_mode & MDREPL ||
  623.                 (lgetc(curwp->w_dotp, curwp->w_doto) != '\t' ||
  624.                 getccol(FALSE) % tabsize == (tabsize - 1)))
  625. /*                (curwp->w_doto) % tabsize == (tabsize - 1)))
  626.                 ^^^^^^^^^^^^^ these needs to be the column,
  627.             DML          not the offset! */
  628.                         ldelete(1L, FALSE);
  629.         }
  630.  
  631.         /* do the appropriate insertion */
  632.         if (c == '}' && (curbp->b_mode & MDCMOD) != 0)
  633.             status = insbrace(n, c);
  634.         else if (c == '#' && (curbp->b_mode & MDCMOD) != 0)
  635.             status = inspound();
  636. #if    DBCS
  637.         else if (is2char(c)) {
  638.             schar = getkey();
  639.             status = TRUE;
  640.             while (n--) {
  641.                 if (linsert(1, c) == FALSE)
  642.                     status = FALSE;
  643.                 if (linsert(1, schar) == FALSE)
  644.                     status = FALSE;
  645.             }
  646.         }
  647. #endif
  648.  
  649.         else
  650.             status = linsert(n, c);
  651.  
  652.         /* check for CMODE fence matching */
  653.         if ((c == '}' || c == ')' || c == ']') &&
  654.                 (curbp->b_mode & MDCMOD) != 0)
  655.             fmatch(c);
  656.  
  657.         /* check auto-save mode */
  658.         if (curbp->b_mode & MDASAVE)
  659.             if (--gacount == 0) {
  660.                 /* and save the file if needed */
  661.                 upscreen(FALSE, 0);
  662.                 filesave(FALSE, 0);
  663.                 gacount = gasave;
  664.             }
  665.  
  666.         lastflag = thisflag;
  667.         return(status);
  668.     }
  669.     TTbeep();
  670.     mlwrite(TEXT19);        /* complain        */
  671. /*        "[Key not bound]" */
  672.     lastflag = 0;                /* Fake last flags.    */
  673.     return(FALSE);
  674. }
  675.  
  676. /*
  677.     Fancy quit command, as implemented by Norm. If the any buffer
  678. has changed do a write on that buffer and exit emacs, otherwise simply
  679. exit.
  680. */
  681.  
  682. PASCAL NEAR quickexit(f, n)
  683.  
  684. int f,n;    /* prefix flag and argument */
  685.  
  686. {
  687.     register BUFFER *bp;    /* scanning pointer to buffers */
  688.     register BUFFER *oldcb; /* original current buffer */
  689.     register int status;
  690.  
  691.     oldcb = curbp;                /* save in case we fail */
  692.  
  693. #if    TIPC
  694.     mlwrite("\n\n");
  695. #endif
  696.     bp = bheadp;
  697.     while (bp != NULL) {
  698.         if ((bp->b_flag&BFCHG) != 0    /* Changed.        */
  699.         && (bp->b_flag&BFINVS) == 0) {    /* Real.        */
  700.             curbp = bp;        /* make that buffer cur */
  701.             mlwrite(TEXT103,bp->b_fname);
  702. /*                "[Saving %s]" */
  703.             mlwrite("\n");
  704.             if ((status = filesave(f, n)) != TRUE) {
  705.                 curbp = oldcb;    /* restore curbp */
  706.                 return(status);
  707.             }
  708.         }
  709.     bp = bp->b_bufp;            /* on to the next buffer */
  710.     }
  711.     quit(f, n);                /* conditionally quit    */
  712.     return(TRUE);
  713. }
  714.  
  715. /*
  716.  * Quit command. If an argument, always quit. Otherwise confirm if a buffer
  717.  * has been changed and not written out. Normally bound to "C-X C-C".
  718.  */
  719.  
  720. PASCAL NEAR quit(f, n)
  721.  
  722. int f,n;    /* prefix flag and argument */
  723.  
  724. {
  725.     register int status;    /* return status */
  726.  
  727.     if (f != FALSE        /* Argument forces it.    */
  728.     || anycb() == FALSE    /* All buffers clean or user says it's OK. */
  729.     || (status = mlyesno(TEXT104)) == TRUE) {
  730. /*                 "Modified buffers exist. Leave anyway" */
  731. #if    FILOCK
  732.         if (lockrel() != TRUE) {
  733.             TTputc('\n');
  734.             TTputc('\r');
  735.             TTclose();
  736.             TTkclose();
  737.             status = meexit(1);
  738.         }
  739. #endif
  740.         if (f)
  741.             status = meexit(n);
  742.         else
  743.             status = meexit(GOOD);
  744.     }
  745.     mlerase();
  746.     return(status);
  747. }
  748.  
  749. PASCAL NEAR meexit(status)
  750. int status;    /* return status of emacs */
  751. {
  752.     eexitflag = TRUE;    /* flag a program exit */
  753.     eexitval = status;
  754.  
  755.     /* and now.. we leave and let the main loop kill us */
  756.     return(TRUE);
  757. }
  758.  
  759. /*
  760.  * Begin a keyboard macro.
  761.  * Error if not at the top level in keyboard processing. Set up variables and
  762.  * return.
  763.  */
  764.  
  765. PASCAL NEAR ctlxlp(f, n)
  766.  
  767. int f,n;    /* prefix flag and argument */
  768.  
  769. {
  770.     if (kbdmode != STOP) {
  771.         mlwrite(TEXT105);
  772. /*            "%%Macro already active" */
  773.         return(FALSE);
  774.     }
  775.     mlwrite(TEXT106);
  776. /*        "[Start macro]" */
  777.     kbdptr = &kbdm[0];
  778.     kbdend = kbdptr;
  779.     kbdmode = RECORD;
  780.     return(TRUE);
  781. }
  782.  
  783. /*
  784.  * End keyboard macro. Check for the same limit conditions as the above
  785.  * routine. Set up the variables and return to the caller.
  786.  */
  787.  
  788. PASCAL NEAR ctlxrp(f, n)
  789.  
  790. int f,n;    /* prefix flag and argument */
  791.  
  792. {
  793.     if (kbdmode == STOP) {
  794.         mlwrite(TEXT107);
  795. /*            "%%Macro not active" */
  796.         return(FALSE);
  797.     }
  798.     if (kbdmode == RECORD) {
  799.         mlwrite(TEXT108);
  800. /*            "[End macro]" */
  801.         kbdmode = STOP;
  802.     }
  803.     return(TRUE);
  804. }
  805.  
  806. /*
  807.  * Execute a macro.
  808.  * The command argument is the number of times to loop. Quit as soon as a
  809.  * command gets an error. Return TRUE if all ok, else FALSE.
  810.  */
  811.  
  812. PASCAL NEAR ctlxe(f, n)
  813.  
  814. int f,n;    /* prefix flag and argument */
  815.  
  816. {
  817.     if (kbdmode != STOP) {
  818.         mlwrite(TEXT105);
  819. /*            "%%Macro already active" */
  820.         return(FALSE);
  821.     }
  822.     if (n <= 0)
  823.         return(TRUE);
  824.     kbdrep = n;        /* remember how many times to execute */
  825.     kbdmode = PLAY;     /* start us in play mode */
  826.     kbdptr = &kbdm[0];    /*    at the beginning */
  827.     return(TRUE);
  828. }
  829.  
  830. /*
  831.  * Abort.
  832.  * Beep the beeper. Kill off any keyboard macro, etc., that is in progress.
  833.  * Sometimes called as a routine, to do general aborting of stuff.
  834.  */
  835.  
  836. PASCAL NEAR ctrlg(f, n)
  837.  
  838. int f,n;    /* prefix flag and argument */
  839.  
  840. {
  841.     TTbeep();
  842.     kbdmode = STOP;
  843.     mlwrite(TEXT8);
  844. /*        "[Aborted]" */
  845.     return(ABORT);
  846. }
  847.  
  848. /* tell the user that this command is illegal while we are in
  849.    VIEW (read-only) mode                */
  850.  
  851. PASCAL NEAR rdonly()
  852.  
  853. {
  854.     TTbeep();
  855.     mlwrite(TEXT109);
  856. /*        "[Key illegal in VIEW mode]" */
  857.     return(FALSE);
  858. }
  859.  
  860. PASCAL NEAR resterr()
  861.  
  862. {
  863.     TTbeep();
  864.     mlwrite(TEXT110);
  865. /*        "[That command is RESTRICTED]" */
  866.     return(FALSE);
  867. }
  868.  
  869. PASCAL NEAR nullproc(f, n)    /* user function that does NOTHING */
  870.  
  871. int n, f;    /* yes, these are default and never used.. but MUST be here */
  872.  
  873. {
  874. }
  875.  
  876. PASCAL NEAR meta(f, n)    /* set META prefixing pending */
  877.  
  878. int f, n;    /* prefix flag and argument */
  879.  
  880. {
  881.     prefix |= META;
  882.     prenum = n;
  883.     predef = f;
  884.     return(TRUE);
  885. }
  886.  
  887. PASCAL NEAR cex(f, n)    /* set ^X prefixing pending */
  888.  
  889. int f, n;    /* prefix flag and argument */
  890.  
  891. {
  892.     prefix |= CTLX;
  893.     prenum = n;
  894.     predef = f;
  895.     return(TRUE);
  896. }
  897.  
  898. PASCAL NEAR unarg()    /* dummy function for binding to universal-argument */
  899. {
  900. }
  901.  
  902. /*    bytecopy:    copy a string...with length restrictions
  903.             ALWAYS null terminate
  904. */
  905.  
  906. char *PASCAL NEAR bytecopy(dst, src, maxlen)
  907.  
  908. char *dst;    /* destination of copied string */
  909. char *src;    /* source */
  910. int maxlen;    /* maximum length */
  911.  
  912. {
  913.     char *dptr;    /* ptr into dst */
  914.  
  915.     dptr = dst;
  916.     while (*src && (maxlen-- > 0))
  917.         *dptr++ = *src++;
  918.     *dptr = 0;
  919.     return(dst);
  920. }
  921.  
  922. /*    copystr:    make another copy of the argument
  923.  
  924. */
  925.  
  926. char *PASCAL NEAR copystr(sp)
  927.  
  928. char *sp;    /* string to copy */
  929.  
  930. {
  931.     char *dp;    /* copy of string */
  932.  
  933.     /* make room! */
  934.     dp = malloc(strlen(sp)+1);
  935.     if (dp == NULL)
  936.         return(NULL);
  937.     strcpy(dp, sp);
  938.     return(dp);
  939. }
  940.  
  941. /*****        Compiler specific Library functions    ****/
  942.  
  943. #if    RAMSIZE
  944. /*    These routines will allow me to track memory usage by placing
  945.     a layer on top of the standard system malloc() and free() calls.
  946.     with this code defined, the environment variable, $RAM, will
  947.     report on the number of bytes allocated via malloc.
  948.  
  949.     with SHOWRAM defined, the number is also posted on the
  950.     end of the bottom mode line and is updated whenever it is changed.
  951. */
  952.  
  953. #undef    malloc
  954. #undef    free
  955.  
  956. #if     VMS & OPTMEM        /* these routines are faster! */
  957. #define    malloc    VAXC$MALLOC_OPT
  958. #define free    VAXC$FREE_OPT
  959. #endif
  960.  
  961. char *allocate(nbytes)    /* allocate nbytes and track */
  962.  
  963. unsigned nbytes;    /* # of bytes to allocate */
  964.  
  965. {
  966.     char *mp;    /* ptr returned from malloc */
  967.     char *malloc();
  968.     FILE *track;    /* malloc track file */
  969.  
  970.     mp = malloc(nbytes);
  971.  
  972. #if    RAMTRCK
  973.     track = fopen("malloc.dat", "a");
  974.     fprintf(track, "Allocating %u bytes at %u:%u\n", nbytes,
  975.             FP_SEG(mp), FP_OFF(mp));
  976.     fclose(track);
  977. #endif
  978.  
  979.     if (mp) {
  980. #if    0
  981.         envram += nbytes;
  982. #else
  983.         envram += 1;
  984. #endif
  985. #if    RAMSHOW
  986.         dspram();
  987. #endif
  988.     }
  989.  
  990.     return(mp);
  991. }
  992.  
  993. release(mp)    /* release malloced memory and track */
  994.  
  995. char *mp;    /* chunk of RAM to release */
  996.  
  997. {
  998.     unsigned *lp;    /* ptr to the long containing the block size */
  999. #if    RAMTRCK
  1000.     FILE *track;    /* malloc track file */
  1001.  
  1002.     track = fopen("malloc.dat", "a");
  1003.     fprintf(track, "Freeing %u:%u\n",
  1004.             FP_SEG(mp), FP_OFF(mp));
  1005.     fclose(track);
  1006. #endif
  1007.  
  1008.     if (mp) {
  1009.         /* update amount of ram currently malloced */
  1010. #if    0
  1011.         lp = ((unsigned *)mp) - 1;
  1012.         envram -= (long)*lp - 2;
  1013. #else
  1014.         envram -= 1;
  1015. #endif
  1016.         free(mp);
  1017. #if    RAMSHOW
  1018.         dspram();
  1019. #endif
  1020.     }
  1021. }
  1022.  
  1023. #if    RAMSHOW
  1024. dspram()    /* display the amount of RAM currently malloced */
  1025.  
  1026. {
  1027.     char mbuf[20];
  1028.     char *sp;
  1029.  
  1030.     TTmove(term.t_nrow - 1, 70);
  1031. #if    COLOR
  1032.     TTforg(7);
  1033.     TTbacg(0);
  1034. #endif
  1035.     sprintf(mbuf, "[%lu]", envram);
  1036.     sp = &mbuf[0];
  1037.     while (*sp)
  1038.         TTputc(*sp++);
  1039.     TTmove(term.t_nrow, 0);
  1040.     movecursor(term.t_nrow, 0);
  1041. }
  1042. #endif
  1043. #endif
  1044.