home *** CD-ROM | disk | FTP | other *** search
/ ftp.uv.es / 2014.11.ftp.uv.es.tar / ftp.uv.es / pub / unix / pine4.10.tar.gz / pine4.10.tar / pine4.10 / pico / random.c < prev    next >
C/C++ Source or Header  |  1998-04-02  |  9KB  |  380 lines

  1. #if    !defined(lint) && !defined(DOS)
  2. static char rcsid[] = "$Id: random.c,v 4.18 1998/04/02 06:00:51 mikes Exp $";
  3. #endif
  4. /*
  5.  * Program:    Random routines
  6.  *
  7.  *
  8.  * Michael Seibel
  9.  * Networks and Distributed Computing
  10.  * Computing and Communications
  11.  * University of Washington
  12.  * Administration Builiding, AG-44
  13.  * Seattle, Washington, 98195, USA
  14.  * Internet: mikes@cac.washington.edu
  15.  *
  16.  * Please address all bugs and comments to "pine-bugs@cac.washington.edu"
  17.  *
  18.  *
  19.  * Pine and Pico are registered trademarks of the University of Washington.
  20.  * No commercial use of these trademarks may be made without prior written
  21.  * permission of the University of Washington.
  22.  * 
  23.  * Pine, Pico, and Pilot software and its included text are Copyright
  24.  * 1989-1998 by the University of Washington.
  25.  * 
  26.  * The full text of our legal notices is contained in the file called
  27.  * CPYRIGHT, included with this distribution.
  28.  *
  29.  */
  30. /*
  31.  * This file contains the command processing functions for a number of random
  32.  * commands. There is no functional grouping here, for sure.
  33.  */
  34.  
  35. #include    "headers.h"
  36.  
  37. #ifdef    ANSI
  38.     int getccol(int);
  39. #else
  40.     int getccol();
  41. #endif
  42.  
  43. int     tabsize;                        /* Tab size (0: use real tabs)  */
  44.  
  45.  
  46. /*
  47.  * Display the current position of the cursor, in origin 1 X-Y coordinates,
  48.  * the character that is under the cursor (in octal), and the fraction of the
  49.  * text that is before the cursor. The displayed column is not the current
  50.  * column, but the column that would be used on an infinite width display.
  51.  * Normally this is bound to "C-X =".
  52.  */
  53. showcpos(f, n)
  54. int f, n;
  55. {
  56.     register LINE   *clp;
  57.     register long   nch;
  58.     register int    cbo;
  59.     register long   nbc;
  60.     register int    lines;
  61.     register int    thisline;
  62.     char     buffer[80];
  63.  
  64.     clp = lforw(curbp->b_linep);            /* Grovel the data.     */
  65.     cbo = 0;
  66.     nch = 0L;
  67.     lines = 0;
  68.     for (;;) {
  69.     if (clp==curwp->w_dotp && cbo==curwp->w_doto) {
  70.         thisline = lines;
  71.         nbc = nch;
  72.     }
  73.     if (cbo == llength(clp)) {
  74.         if (clp == curbp->b_linep)
  75.           break;
  76.         clp = lforw(clp);
  77.         cbo = 0;
  78.         lines++;
  79.     } else
  80.       ++cbo;
  81.     ++nch;
  82.     }
  83.  
  84.     sprintf(buffer,"line %d of %d (%d%%%%), character %ld of %ld (%d%%%%)",
  85.         thisline+1, lines+1, (int)((100L*(thisline+1))/(lines+1)),
  86.         nbc, nch, (nch) ? (int)((100L*nbc)/nch) : 0);
  87.  
  88.     emlwrite(buffer, NULL);
  89.     return (TRUE);
  90. }
  91.  
  92.  
  93. /*
  94.  * Return current column.  Stop at first non-blank given TRUE argument.
  95.  */
  96. getccol(bflg)
  97. int bflg;
  98. {
  99.     register int c, i, col;
  100.  
  101.     col = 0;
  102.     for (i=0; i<curwp->w_doto; ++i) {
  103.     c = lgetc(curwp->w_dotp, i).c;
  104.     if (c!=' ' && c!='\t' && bflg)
  105.       break;
  106.  
  107.     if (c == '\t')
  108.       col |= 0x07;
  109.     else if (c<0x20 || c==0x7F)
  110.       ++col;
  111.  
  112.     ++col;
  113.     }
  114.  
  115.     return(col);
  116. }
  117.  
  118.  
  119.  
  120. /*
  121.  * Set tab size if given non-default argument (n <> 1).  Otherwise, insert a
  122.  * tab into file.  If given argument, n, of zero, change to true tabs.
  123.  * If n > 1, simulate tab stop every n-characters using spaces. This has to be
  124.  * done in this slightly funny way because the tab (in ASCII) has been turned
  125.  * into "C-I" (in 10 bit code) already. Bound to "C-I".
  126.  */
  127. tab(f, n)
  128.     int f, n;
  129. {
  130.     if (n < 0)
  131.       return (FALSE);
  132.  
  133.     if (n == 0 || n > 1) {
  134.     tabsize = n;
  135.     return(TRUE);
  136.     }
  137.  
  138.     if (! tabsize)
  139.       return(linsert(1, '\t'));
  140.  
  141.     return(linsert(tabsize - (getccol(FALSE) % tabsize), ' '));
  142. }
  143.  
  144.  
  145. /*
  146.  * Insert a newline. Bound to "C-M".
  147.  */
  148. newline(f, n)
  149.     int f, n;
  150. {
  151.     register int    s;
  152.  
  153.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  154.       return(rdonly());    /* we are in read only mode    */
  155.  
  156.     if (n < 0)
  157.       return (FALSE);
  158.  
  159.     if(optimize && (curwp->w_dotp != curwp->w_bufp->b_linep)){
  160.     int l;
  161.  
  162.     if(worthit(&l)){
  163.         if(curwp->w_doto != 0)
  164.           l++;
  165.         scrolldown(curwp, l, n);
  166.     }
  167.     }
  168.  
  169.     /* if we are in C mode and this is a default <NL> */
  170.     /* pico's never in C mode */
  171.  
  172.     /* insert some lines */
  173.     while (n--) {
  174.     if ((s=lnewline()) != TRUE)
  175.       return (s);
  176.     }
  177.     return (TRUE);
  178. }
  179.  
  180.  
  181.  
  182. /*
  183.  * Delete forward. This is real easy, because the basic delete routine does
  184.  * all of the work. Watches for negative arguments, and does the right thing.
  185.  * If any argument is present, it kills rather than deletes, to prevent loss
  186.  * of text if typed with a big argument. Normally bound to "C-D".
  187.  */
  188. forwdel(f, n)
  189.     int f, n;
  190. {
  191.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  192.       return(rdonly());    /* we are in read only mode    */
  193.  
  194.     if (n < 0)
  195.       return (backdel(f, -n));
  196.  
  197.     if(optimize && (curwp->w_dotp != curwp->w_bufp->b_linep)){
  198.     int l;
  199.  
  200.     if(worthit(&l) && curwp->w_doto == llength(curwp->w_dotp))
  201.       scrollup(curwp, l+1, 1);
  202.     }
  203.  
  204.     if (f != FALSE) {                       /* Really a kill.       */
  205.     if ((lastflag&CFKILL) == 0)
  206.       kdelete();
  207.     thisflag |= CFKILL;
  208.     }
  209.  
  210.     return (ldelete((long) n, f ? kinsert : NULL));
  211. }
  212.  
  213.  
  214.  
  215. /*
  216.  * Delete backwards. This is quite easy too, because it's all done with other
  217.  * functions. Just move the cursor back, and delete forwards. Like delete
  218.  * forward, this actually does a kill if presented with an argument. Bound to
  219.  * both "RUBOUT" and "C-H".
  220.  */
  221. backdel(f, n)
  222.     int f, n;
  223. {
  224.     register int    s;
  225.  
  226.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  227.       return(rdonly());    /* we are in read only mode    */
  228.  
  229.     if (n < 0)
  230.       return (forwdel(f, -n));
  231.  
  232.     if(optimize && curwp->w_dotp != curwp->w_bufp->b_linep){
  233.     int l;
  234.     
  235.     if(worthit(&l) && curwp->w_doto == 0 &&
  236.        lback(curwp->w_dotp) != curwp->w_bufp->b_linep){
  237.         if(l == curwp->w_toprow)
  238.           scrollup(curwp, l+1, 1);
  239.         else if(llength(lback(curwp->w_dotp)) == 0)
  240.           scrollup(curwp, l-1, 1);
  241.         else
  242.           scrollup(curwp, l, 1);
  243.     }
  244.     }
  245.  
  246.     if (f != FALSE) {                       /* Really a kill.       */
  247.     if ((lastflag&CFKILL) == 0)
  248.       kdelete();
  249.  
  250.     thisflag |= CFKILL;
  251.     }
  252.  
  253.     if ((s=backchar(f, n)) == TRUE)
  254.       s = ldelete((long) n, f ? kinsert : NULL);
  255.  
  256.     return (s);
  257. }
  258.  
  259.  
  260.  
  261. /*
  262.  * killtext - delete the line that the cursor is currently in.
  263.  *          a greatly pared down version of its former self.
  264.  */
  265. killtext(f, n)
  266. int f, n;
  267. {
  268.     register int chunk;
  269.     int         opt_scroll = 0;
  270.  
  271.     if (curbp->b_mode&MDVIEW)        /* don't allow this command if  */
  272.       return(rdonly());            /* we are in read only mode     */
  273.  
  274.     if ((lastflag&CFKILL) == 0)        /* Clear kill buffer if */
  275.       kdelete();            /* last wasn't a kill.  */
  276.  
  277.     if(gmode & MDDTKILL){        /*  */
  278.     if((chunk = llength(curwp->w_dotp) - curwp->w_doto) == 0){
  279.         chunk = 1;
  280.         if(optimize)
  281.           opt_scroll = 1;
  282.     }
  283.     }
  284.     else{
  285.     gotobol(FALSE, 1);        /* wack from bol past newline */
  286.     chunk = llength(curwp->w_dotp) + 1;
  287.     if(optimize)
  288.       opt_scroll = 1;
  289.     }
  290.  
  291.     /* optimize what motion we can */
  292.     if(opt_scroll && (curwp->w_dotp != curwp->w_bufp->b_linep)){
  293.     int l;
  294.  
  295.     if(worthit(&l))
  296.       scrollup(curwp, l, 1);
  297.     }
  298.  
  299.     thisflag |= CFKILL;
  300.     return(ldelete((long) chunk, kinsert));
  301. }
  302.  
  303.  
  304. /*
  305.  * Yank text back from the kill buffer. This is really easy. All of the work
  306.  * is done by the standard insert routines. All you do is run the loop, and
  307.  * check for errors. Bound to "C-Y".
  308.  */
  309. yank(f, n)
  310. int f, n;
  311. {
  312.     register int    c;
  313.     register int    i;
  314.  
  315.     if (curbp->b_mode&MDVIEW)    /* don't allow this command if    */
  316.       return(rdonly());    /* we are in read only mode    */
  317.  
  318.     if (n < 0)
  319.       return (FALSE);
  320.  
  321.     if(optimize && (curwp->w_dotp != curwp->w_bufp->b_linep)){
  322.     int l;
  323.  
  324.     if(worthit(&l) && !(lastflag&CFFILL)){
  325.         register int  t = 0; 
  326.         register int  i = 0;
  327.         register int  ch;
  328.  
  329.         while((ch=fremove(i++)) >= 0)
  330.           if(ch == '\n')
  331.         t++;
  332.         if(t+l < curwp->w_toprow+curwp->w_ntrows)
  333.           scrolldown(curwp, l, t);
  334.     }
  335.     }
  336.  
  337.     if(lastflag & CFFILL){        /* if last command was fillpara() */
  338.     REGION region;
  339.     LINE *dotp;
  340.  
  341.     backchar(FALSE, 1);
  342.     dotp = curwp->w_dotp;
  343.     gotobop(FALSE, 1);        /* then go to the top of the para */
  344.  
  345.     curwp->w_doto = 0;
  346.     getregion(®ion, dotp, llength(dotp));
  347.     if(!ldelete(region.r_size, NULL))
  348.       return(FALSE);
  349.     }                    /* then splat out the saved buffer */
  350.  
  351.     while (n--) {
  352.     i = 0;
  353.     while ((c = ((lastflag&CFFILL) ? fremove(i) : kremove(i))) >= 0) {
  354.         if (c == '\n') {
  355.         if (lnewline() == FALSE)
  356.           return (FALSE);
  357.         } else {
  358.         if (linsert(1, c) == FALSE)
  359.           return (FALSE);
  360.         }
  361.  
  362.         ++i;
  363.     }
  364.     }
  365.  
  366.     if(lastflag&CFFILL){            /* if last command was fillpara() */
  367.     curwp->w_dotp = lforw(curwp->w_dotp);
  368.     curwp->w_doto = 0;
  369.  
  370.     curwp->w_flag |= WFMODE;
  371.     
  372.     if(!Pmaster){
  373.         sgarbk = TRUE;
  374.         emlwrite("", NULL);
  375.     }
  376.     }
  377.  
  378.     return (TRUE);
  379. }
  380.