home *** CD-ROM | disk | FTP | other *** search
/ Fresh Fish 8 / FreshFishVol8-CD1.bin / gnu / src / baseline / jove-4.14.6.lha / jove-4.14.6 / paragraph.c < prev    next >
C/C++ Source or Header  |  1992-01-10  |  15KB  |  553 lines

  1. /***************************************************************************
  2.  * This program is Copyright (C) 1986, 1987, 1988 by Jonathan Payne.  JOVE *
  3.  * is 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 "disp.h"
  10.  
  11. private int    get_indent proto((Line *));
  12.  
  13. /* Thanks to Brian Harvey for this paragraph boundery finding algorithm.
  14.    It's really quite hairy figuring it out.  This deals with paragraphs that
  15.    are seperated by blank lines, lines beginning with a Period (assumed to
  16.    be an nroff command), lines beginning with BackSlash (assumed to be Tex
  17.    commands).  Also handles paragraphs that are separated by lines of
  18.    different indent; and it deals with outdented paragraphs, too.  It's
  19.    really quite nice.  Here's Brian's algorithm.
  20.  
  21.    Definitions:
  22.  
  23.    THIS means the line containing the cursor.
  24.    PREV means the line above THIS.
  25.    NEXT means the line below THIS.
  26.  
  27.    BLANK means empty, empty except for spaces and tabs, starts with a period
  28.    or a backslash, or nonexistent (because the edge of the buffer is
  29.    reached).  ((BH 12/24/85 A line starting with backslash is blank only if
  30.    the following line also starts with backslash.  This is so that \noindent
  31.    is part of a paragraph, but long strings of TeX commands don't get
  32.    rearranged.  It still isn't perfect but it's better.))
  33.  
  34.    BSBLANK means BLANK or starts with a backslash.  (BH 12/24/85)
  35.  
  36.    HEAD means the first (nonblank) line of the paragraph containing THIS.
  37.    BODY means all other (nonblank) lines of the paragraph.
  38.    TAIL means the last (nb) line of the paragraph.  (TAIL is part of BODY.)
  39.  
  40.    HEAD INDENT means the indentation of HEAD.  M-J should preserve this.
  41.    BODY INDENT means the indentation of BODY.  Ditto.
  42.  
  43.    Subprocedures:
  44.  
  45.    TAILRULE(BODYLINE)
  46.    If BODYLINE is BLANK, the paragraph has only one line, and there is no
  47.    BODY and therefore no TAIL.  Return.  Otherwise, starting from BODYLINE,
  48.    move down until you find a line that either is BSBLANK or has a different
  49.    indentation from BODYLINE.  The line above that different line is TAIL.
  50.    Return.
  51.  
  52.    Rules:
  53.  
  54.    1.  If THIS is BLANK, which command are you doing?  If M-J or M-[, then go
  55.    up to the first non-BLANK line and start over.  (If there is no non-BLANK
  56.    line before THIS, ring the bell.)  If M-], then the first non-BLANK line
  57.    below THIS is HEAD, and the second consecutive non-BSBLANK line (if any) is
  58.    the beginning of BODY.  (If there is no non-BLANK line after THIS, ring
  59.    the bell.)  Do TAILRULE(beginning-of-BODY).  Go to rule A.
  60.  
  61.    2.  If PREV is BLANK or THIS is BSBLANK, then THIS is HEAD, and NEXT (if
  62.    not BSBLANK) is in BODY.  Do TAILRULE(NEXT).  Go to rule A.
  63.  
  64.    3.  If NEXT is BSBLANK, then THIS is TAIL, therefore part of BODY.  Go to
  65.    rule 5 to find HEAD.
  66.  
  67.    4.  If either NEXT or PREV has the same indentation as THIS, then THIS is
  68.    part of BODY.  Do TAILRULE(THIS).  Go to rule 5 to find HEAD.  Otherwise,
  69.    go to rule 6.
  70.  
  71.    5.  Go up until you find a line that is either BSBLANK or has a different
  72.    indentation from THIS.  If that line is BLANK, the line below it is HEAD;
  73.    If that line is non-BLANK, then call that new line THIS for what follows.
  74.    If THIS is BSBLANK (that is, THIS starts with backslash), THIS is HEAD;
  75.    otherwise, if (the new) PREV has the same indent as THIS, then (the new)
  76.    NEXT is HEAD; if PREV has a different indent from THIS, then THIS is
  77.    HEAD.  Go to rule A.
  78.  
  79.    6.  If you got here, then both NEXT and PREV are nonblank and are
  80.    differently indented from THIS.  This is a tricky case and there is no
  81.    guarantee that you're going to win.  The most straightforward thing to do
  82.    is assume that we are not using hanging indentation.  In that case:
  83.    whichever of PREV and THIS is indented further is HEAD.  Do
  84.    TAILRULE(HEAD+1).  Go to rule A.
  85.  
  86.    6+.  A more complicated variant would be this: if THIS is indented further
  87.    than PREV, we are using regular indentation and rule 6 applies.  If PREV
  88.    is indented further than THIS, look at both NEXT and the line after NEXT.
  89.    If those two lines are indented equally, and more than THIS, then we are
  90.    using hanging indent, THIS is HEAD, and NEXT is the first line of BODY.
  91.    Do TAILRULE(NEXT).  Otherwise, rule 6 applies.
  92.  
  93.    A.  You now know where HEAD and TAIL are.  The indentation of HEAD is HEAD
  94.    INDENT; the indentation of TAIL is BODY INDENT.
  95.  
  96.    B.  If you are trying to M-J, you are now ready to do it.
  97.  
  98.    C.  If you are trying to M-], leave point after the newline that ends
  99.    TAIL.  In other words, leave the cursor at the beginning of the line
  100.    after TAIL.  It is not possible for this to leave point where it started
  101.    unless it was already at the end of the buffer.
  102.  
  103.    D.  If you are trying to M-[, if the line before HEAD is not BLANK, then
  104.    leave point just before HEAD.  That is, leave the cursor at the beginning
  105.    of HEAD.  If the line before HEAD is BLANK, then leave the cursor at the
  106.    beginning of that line.  If the cursor didn't move, go up to the first
  107.    earlier non-BLANK line and start over.
  108.  
  109.  
  110.    End of Algorithm.  I implemented rule 6+ because it seemed nicer.  */
  111.  
  112. /*
  113.  *    Altered to include '--' at the start of the line to mean
  114.  *    beginning of paragraph this makes formatting the first line
  115.  *    of mail for mh easier
  116.  *    Peter C UKC
  117.  */
  118. int    RMargin = 78,
  119.     LMargin = 0;
  120. private Line    *para_head,
  121.     *para_tail;
  122. private int    head_indent,
  123.     body_indent;
  124. private bool    use_lmargin;
  125.  
  126. /* some defines for paragraph boundery checking */
  127. #define I_EMPTY        (-1)    /* line "looks" empty (spaces and tabs) */
  128. #define I_PERIOD    (-2)    /* line begins with "." or "\" */
  129. #define I_BUFEDGE    (-3)    /* line is nonexistent (edge of buffer) */
  130.  
  131. static bool    bslash;        /* Nonzero if get_indent finds line starting
  132.                    with backslash */
  133.  
  134. private int
  135. i_blank(lp)
  136. Line    *lp;
  137. {
  138.     return get_indent(lp) < 0;
  139. }
  140.  
  141. private bool
  142. i_bsblank(lp)
  143. Line    *lp;
  144. {
  145.     return i_blank(lp) || bslash;
  146. }
  147.  
  148. private int
  149. get_indent(lp)
  150. register Line    *lp;
  151. {
  152.     Bufpos    save;
  153.     register int    indent;
  154.  
  155.     bslash = NO;
  156.     if (lp == NULL)
  157.         return I_BUFEDGE;
  158.     DOTsave(&save);
  159.     SetLine(lp);
  160.     if (blnkp(linebuf))
  161.         indent = I_EMPTY;
  162.     else if (linebuf[0] == '.' || (linebuf[0] == '-' && linebuf[1] == '-'))
  163.         indent = I_PERIOD;
  164.     else if (linebuf[0] == '\\') {
  165.         /* BH 12/24/85.  Backslash is BLANK only if next line
  166.            also starts with Backslash. */
  167.         bslash = YES;
  168.         SetLine(lp->l_next);
  169.         if (linebuf[0] == '\\')
  170.             indent = I_PERIOD;
  171.         else
  172.             indent = 0;
  173.     } else {
  174.         ToIndent();
  175.         indent = calc_pos(linebuf, curchar);
  176.     }
  177.     SetDot(&save);
  178.  
  179.     return indent;
  180. }
  181.  
  182. private Line *
  183. tailrule(lp)
  184. register Line    *lp;
  185. {
  186.     int    i;
  187.  
  188.     i = get_indent(lp);
  189.     if (i < 0)
  190.         return lp;    /* one line paragraph */
  191.     do {
  192.         if ((get_indent(lp->l_next) != i) || bslash)
  193.             /* BH line with backslash is head of next para */
  194.             break;
  195.     } while ((lp = lp->l_next) != NULL);
  196.     if (lp == NULL)
  197.         complain((char *) NULL);
  198.     return lp;
  199. }
  200.  
  201. /* Finds the beginning, end and indent of the current paragraph, and sets
  202.    the above global variables.  HOW says how to behave when we're between
  203.    paragraphs.  That is, it's either FORWARD or BACKWARD depending on which
  204.    way we're favoring. */
  205.  
  206. private void
  207. find_para(how)
  208. int    how;
  209. {
  210.     Line    *this,
  211.         *prev,
  212.         *next,
  213.         *head = NULL,
  214.         *body = NULL,
  215.         *tail = NULL;
  216.     int    this_indent;
  217.     Bufpos    orig;        /* remember where we were when we started */
  218.  
  219.     DOTsave(&orig);
  220. strt:
  221.     this = curline;
  222.     prev = curline->l_prev;
  223.     next = curline->l_next;
  224.     this_indent = get_indent(this);
  225.  
  226.     if (i_blank(this)) {        /* rule 1 */
  227.         if (how == BACKWARD) {
  228.             while (i_blank(curline))
  229.                 if (firstp(curline))
  230.                     complain((char *)NULL);
  231.                 else
  232.                     line_move(BACKWARD, 1, NO);
  233.             goto strt;
  234.         } else {
  235.             while (i_blank(curline))
  236.                 if (lastp(curline))
  237.                     complain((char *)NULL);
  238.                 else
  239.                     line_move(FORWARD, 1, NO);
  240.             head = curline;
  241.             next = curline->l_next;
  242.             body = !i_bsblank(next)? next : head;
  243.         }
  244.     } else if (i_bsblank(t