home *** CD-ROM | disk | FTP | other *** search
/ ftp.update.uu.se / ftp.update.uu.se.2014.03.zip / ftp.update.uu.se / pub / rainbow / cpm / emacs / emacssrc.lzh / ovudfb31.c < prev    next >
C/C++ Source or Header  |  1992-03-11  |  8KB  |  322 lines

  1. #include    "stdio.h"
  2. #include    "ed.h"
  3. /* ovudfb31.c: (read "up down forward back" for "udfb")
  4. ** overlay 31 source code.
  5. ** this large overlay is a portmanteau of frequently-used
  6. ** commands. One can hardly justify having some of them in
  7. ** overlays UNLESS the overlay in question is one which is
  8. ** likely to be in memory at all times -- so, this overlay 
  9. ** includes gobs of the most useful commands.
  10. */
  11.  
  12. extern int   wstyle;    /* Which kind of Word operations? */
  13. extern int   currow;    /* set by update() */
  14. extern int   tabsize;    /* if tabs are expanded to spaces. */
  15. extern char *kbufp;    /* Kill buffer data */
  16. extern int   kused;    /* # of bytes used in KB */
  17. extern int   ksize;    /* # of bytes allocated in KB */
  18.  
  19. #define TABCASE 40    /* case TABCASE and above are special. */
  20.     /* cases < TABCASE are numbered so that we can handle a negative
  21.     ** parameter with "casenumber ^= 1"
  22.     */
  23.  
  24. ovmain( x )
  25. #define cmdflag (*(int *)0x102)
  26. #define cmdparm (*(int *)0x104)
  27. {
  28.     register LINE    * mylp;
  29.     register WINDOW * mywp;
  30.  
  31. #define temp_int (*(int *)0xc0)
  32. #define mydoto   (*(int *)0xc2)
  33. #define xbufchar (*(int *)0xc4)
  34. #define limitp   (*(LINE **)0xc6)
  35. #define localx   (*(int *)0xc8)
  36. #define xinsct   (*(int *)0xca)
  37. #define mylen    (*(int *)0xcc)
  38. #define Wdoto    (*(int *)0xce)
  39.     /* the above #defines are temporary variables, in effect:
  40.     ** the compiler generates much smaller code for specific addresses
  41.     ** than it does for stack variables.
  42.     */
  43.  
  44.     xinsct = 1;
  45.     mywp = curwp;
  46.     mylp = mywp->w_dotp;
  47.     limitp = curbp->b_linep;
  48.     temp_int = mydoto =  0;
  49.     mylen = llength( mylp );
  50.     Wdoto = mywp->w_doto;
  51.  
  52.     if ( ( localx = x ) < TABCASE )
  53.     {    /* most commands here have negative-parameter counterparts. */
  54.  
  55.         if ( cmdparm < 0 )
  56.         {    localx ^= 1;
  57.             cmdparm = -cmdparm;
  58.         }
  59.     }
  60.  
  61.     switch ( localx )
  62.     {
  63.         case 13:    /* goto end of line. */
  64.             mydoto = mylen;
  65.             goto dotpset;
  66.         case 12:    /* goto start of line. */
  67.             if ( cmdflag && ( mydoto = cmdparm -1 ) > mylen )
  68.                 mydoto = mylen;
  69.             goto dotpset;
  70.         case 10:    /* page forwards */
  71.         case 11:    /* page back. */
  72.             cmdparm *= ( mywp->w_ntrows - 1 );
  73.             goto linemotion;
  74.         case 1:    /* up arrow.  */
  75.             ++cmdparm;
  76.             goto linemo2;    /* backward goes n+1 lines! */
  77.         case 9:    /* gotoeob */
  78.             if ( cmdparm ) --cmdparm;
  79.         case 8:    /* gotobob */
  80.             mylp = curbp->b_linep;
  81. linemotion:
  82.             /* if ( localx & 1 ) */
  83.             /* {    mylp = lforw( mylp ); */
  84.             /* } */
  85.             curgoal = curcol = 0;
  86.             /* mywp->w_flag |= WFHARD; */
  87. linemo2:        /* fall through. */
  88.         case 0:    /* down arrow */
  89.         {    /* up or down */
  90.             if ( ! ( lastflag & CFCPCN ))
  91.             {    /* Reset goal if last not up/down */
  92.                 curgoal = curcol;
  93.             }
  94.             thisflag |= CFCPCN;
  95.  
  96.             if ( localx & 1 )    /* backward */
  97.             {    while ( decparm()
  98.                     && ( mylp = lback( mylp )) != limitp
  99.                 );
  100.             }
  101.             else
  102.             {    while ( decparm()
  103.                     && ( mylp = lforw( mylp )) != limitp
  104.                 );
  105.             }    /* "--n >= 0" for n+1 lines. */
  106.  
  107.             while ( mydoto != llength( mylp ))/* NOT mylen! */
  108.             {    if ( ( xbufchar = lgetc( mylp, mydoto ))
  109.                     == '\t' )
  110.                         temp_int |= 0x07;
  111.                 else if ( ! isinsert( xbufchar ))
  112.                     plustemp();
  113.                 if ( ++temp_int > curgoal ) break;
  114.                 ++mydoto;
  115.             }
  116. dotpmove:        wfmove();
  117. dotpset:        mywp->w_dotp  = mylp;
  118.             mywp->w_doto  = mydoto;
  119.             goto rettrue;    /* return ( 1 ); */
  120.         }    /* end case 0 or 1 */
  121.         case 2:    /* word forward */
  122.         case 4:    /* del word forward */
  123.         {    /* mylp = curwp->w_dotp; at top (mylp) */
  124.             mydoto = Wdoto;
  125.             while ( decparm())
  126.             {    if ( ! ( wstyle & 2 ) && ! fwx())
  127.                 {    goto retfalse;
  128.                 }
  129.                 while ( inword( ))
  130.                 {    fc1();
  131.                     /* assert the above cannot fail. */
  132.                     plustemp();
  133.                 }
  134.                 if ( wstyle & 2 && ! fwx())
  135.                 {    goto retfalse;
  136.                 }
  137.             }
  138.             if ( localx == 2 ) goto rettrue;
  139.             mywp->w_dotp = mylp;
  140.             mywp->w_doto = mydoto;
  141.             goto retldel;    /* return ( ldelete( temp_int, 1 )); */
  142.         }    /* end case 2 or 4 */
  143.         case 3:    /* word backward */
  144.         case 5:    /* del word backward */
  145.         {
  146.             if ( ! bc1( )) goto retfalse; /* TOF */
  147.             /* temp_int++; */
  148.             while ( decparm())
  149.             {
  150.                 while ( ! inword( ))
  151.                 {    if ( ! bc1()) goto retfalse;
  152.                     plustemp();
  153.                 }
  154.                 while ( inword( ))
  155.                 {    if ( ! bc1())
  156.                     {    /* top of file. */
  157. retfalse:                    return ( 0 );
  158.                     }
  159.                     plustemp();
  160.                 }
  161.             }
  162.             /* temp_int--; */
  163.             fc1();    /* can't fail. */
  164.             if ( localx == 5 )
  165.             {    /* delword. */
  166. retldel:            cmdflag = 1;
  167. ret2ldel:            return ( ldelete( temp_int, cmdflag ));
  168.             }
  169. rettrue:        return ( 1 );
  170.         }    /* end case 3 or 5. */
  171.         case 6:    /* forwdel() */
  172.         case 7:    /* backdel() */
  173.         {
  174.             if ( cmdflag )
  175.             {    /* Really a kill.    */
  176.                 kdcheck();
  177.             }
  178.             if ( ( localx & 1 ) && ! backchar( cmdflag, cmdparm ))
  179.                 goto retfalse;
  180.             temp_int = cmdparm;
  181.             goto ret2ldel; /*return(ldelete(cmdparm,cmdflag));*/
  182.         }    /* end cases 6 and 7 */
  183.         case TABCASE:
  184.         {    /* with no parameter, perform tab function;
  185.             ** with one, set tabsize.
  186.             */
  187.             if ( cmdparm < 0 ) return ( 0 );
  188.             if ( cmdparm != 1 )
  189.             {    tabsize = cmdparm;
  190.                 return( 1 );
  191.             }
  192.             xbufchar = '\t';
  193.             if ( tabsize )
  194.             {    xinsct = tabsize - 
  195.                     ( ( curcol + 1 ) % tabsize );
  196.                 xbufchar = ' ';
  197.             }
  198.             return( xlinsc( ));
  199.         }    /* end case TABCASE */
  200.         case TABCASE+1:    /* openline() */
  201.         case TABCASE+2:    /* indent() */
  202.         {    /* openline() keeps cursor in same place,
  203.             ** inserts arg newlines.
  204.             ** indent() moves forward with its newlines,
  205.             ** adds whitespace to match the current line.
  206.             */
  207.             mylp = lback( mylp );
  208.             mydoto = Wdoto;
  209.             while ( decparm()) if ( ! lnewline()) goto retfalse;
  210.             mylp = lforw( mylp );
  211.                 /* lnewline() may delete the original
  212.                 ** target of mylp.
  213.                 */
  214.             if ( localx == ( TABCASE + 1 ))
  215.             {    /* openline(), done. */
  216.                 goto dotpset;
  217.             }
  218.             while ( ( xbufchar = lgetc( mylp, ++cmdparm )) == ' '
  219.                 || xbufchar == '\t' )
  220.             {    xlinsc( );
  221.             }
  222.             if ( xbufchar == '{' )
  223.             {    /* C indent */
  224.                 xbufchar = '\t';
  225.                 xlinsc();
  226.             }
  227.             goto rettrue;
  228.         }    /* end case indent() and openline() */
  229.         case TABCASE+7:    /* vi-style kill. */
  230.             cmdflag = 1;
  231.             mywp->w_doto = Wdoto = 0;    /* fall through. */
  232.         case TABCASE+3:    /* kill() */
  233.         {    kdcheck();
  234.             mydoto = Wdoto;
  235.             if ( ! cmdflag )
  236.             {    temp_int = mylen - mydoto;
  237.                 if ( temp_int == 0 ) temp_int = 1;
  238.             } else if ( ! cmdparm )
  239.             {    temp_int = mydoto;
  240.                 mywp->w_doto = 0;
  241.             } else if ( decparm())    /* "if n > 0" */
  242.             {    temp_int = mylen - mydoto + 1;
  243.                 mylp = lforw( mylp );
  244.                 while (decparm()) /* while --n */
  245.                 {    if ( mylp == limitp )
  246.                     goto retfalse;
  247.                     temp_int += llength( mylp ) + 1;
  248.                     mylp = lforw( mylp );
  249.                 }
  250.             } else
  251.             {    /* mlwrite("neg kill"); */
  252.                 ctrlg();
  253.                 goto retfalse;
  254.             }
  255.             goto retldel; /*return(ldelete(temp_int,TRUE));*/
  256.         }    /* end case "kill()" */
  257.         case TABCASE+4:    /* yank() */
  258.         {    if ( ! decparm()) goto retfalse;
  259.             do    /* make cmdparm copies. */
  260.             {    temp_int = 0;
  261.                 while ( temp_int < kused )
  262.                 {    if ( ( xbufchar = kbufp[ temp_int ])
  263.                         == '\n' )
  264.                     {    lnewline( );
  265.                     } else
  266.                     {    if ( ! xlinsc())
  267.                             goto retfalse;
  268.                     }
  269.                     plustemp();
  270.                 }
  271.             } while ( decparm( ));
  272.             goto rettrue;
  273.         }    /* end case yank() */
  274.         case TABCASE+5:    /* set mark */
  275.         {    mlwrite("[Mark]");
  276. markset:        mywp->w_markp = mylp;
  277.             mywp->w_marko = Wdoto;
  278.             goto rettrue;
  279.         }    /* end case set mark */
  280.         case TABCASE+6: /* exchange mark and cursor. */
  281.         {    if ( mywp->w_markp == NULL )
  282.             {    mlwrite("No mark");
  283.                 return ( 0 );
  284.             }
  285.             mywp->w_dotp  = mywp->w_markp;
  286.             mywp->w_doto  = mywp->w_marko;
  287.             wfmove();
  288.             goto markset;
  289.         }    /* end case ^X^X */
  290.     }
  291. }
  292.  
  293. fc1()    /* saves 10 bytes per call to it. */
  294. {    return( forwchar( 0, 1 ));
  295. }
  296. bc1()    /* saves 10 bytes per call to it. */
  297. {    return( backchar( 0, 1 ));
  298. }
  299. fwx()    /* loop used twice in forwword. */
  300. {    while ( ! inword( ))
  301.     {    if ( ! fc1( )) return ( 0 );
  302.         plustemp();
  303.     }
  304.     return ( 1 );
  305. }
  306. plustemp()    /* temp_int++ : saves 2 bytes per call. */
  307. {    ++temp_int;
  308. }
  309. decparm()    /* --cmdparm >= 0 */
  310. {    return ( --cmdparm >= 0 );
  311. }
  312. xlinsc()
  313. {    return( linsert( xinsct, xbufchar ));
  314. }
  315. kdcheck()
  316. {    if ( ! ( lastflag & CFKILL )) kdelete();
  317.     thisflag |= CFKILL;
  318. }
  319. wfmove()
  320. {    curwp->w_flag |= WFMOVE;
  321. }
  322.